keystore_aidl_hidl_marshalling_utils.cpp 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  1. /*
  2. **
  3. ** Copyright 2016, The Android Open Source Project
  4. **
  5. ** Licensed under the Apache License, Version 2.0 (the "License");
  6. ** you may not use this file except in compliance with the License.
  7. ** You may obtain a copy of the License at
  8. **
  9. ** http://www.apache.org/licenses/LICENSE-2.0
  10. **
  11. ** Unless required by applicable law or agreed to in writing, software
  12. ** distributed under the License is distributed on an "AS IS" BASIS,
  13. ** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. ** See the License for the specific language governing permissions and
  15. ** limitations under the License.
  16. */
  17. #include "keystore_aidl_hidl_marshalling_utils.h"
  18. #include <keystore/ExportResult.h>
  19. #include <keystore/KeyCharacteristics.h>
  20. #include <keystore/KeymasterBlob.h>
  21. #include <keystore/KeymasterCertificateChain.h>
  22. #include <keystore/keymaster_types.h>
  23. #include <keystore/keystore_hidl_support.h>
  24. namespace keystore {
  25. // reads byte[]
  26. hidl_vec<uint8_t> readKeymasterBlob(const android::Parcel& in) {
  27. ssize_t length = in.readInt32();
  28. if (length <= 0) {
  29. return {};
  30. }
  31. const void* buf = in.readInplace(length);
  32. if (!buf) return {};
  33. return blob2hidlVec(reinterpret_cast<const uint8_t*>(buf), size_t(length));
  34. }
  35. android::status_t writeKeymasterBlob(const hidl_vec<uint8_t>& blob, android::Parcel* out) {
  36. int32_t size = int32_t(std::min<size_t>(blob.size(), std::numeric_limits<int32_t>::max()));
  37. auto rc = out->writeInt32(size);
  38. if (rc != ::android::OK) return rc;
  39. if (!size) return ::android::OK;
  40. return out->write(blob.data(), size);
  41. }
  42. android::status_t writeKeymasterBlob(const ::std::vector<int32_t>& blob, android::Parcel* out) {
  43. int32_t size = int32_t(std::min<size_t>(blob.size(), std::numeric_limits<int32_t>::max()));
  44. auto rc = out->writeInt32(size);
  45. if (rc != ::android::OK) return rc;
  46. if (!size) return ::android::OK;
  47. return out->write(blob.data(), size);
  48. }
  49. NullOr<KeyParameter> readKeyParameterFromParcel(const android::Parcel& in) {
  50. // Method must be in sync with KeymasterArgument.java
  51. if (in.readInt32() == 0) {
  52. return {};
  53. }
  54. KeyParameter result;
  55. Tag tag = static_cast<Tag>(in.readInt32());
  56. result.tag = tag;
  57. switch (typeFromTag(tag)) {
  58. case TagType::ENUM:
  59. case TagType::ENUM_REP:
  60. case TagType::UINT:
  61. case TagType::UINT_REP:
  62. result.f.integer = in.readInt32();
  63. break;
  64. case TagType::ULONG:
  65. case TagType::ULONG_REP:
  66. case TagType::DATE:
  67. result.f.longInteger = in.readInt64();
  68. break;
  69. case TagType::BOOL:
  70. result.f.boolValue = true;
  71. break;
  72. case TagType::BIGNUM:
  73. case TagType::BYTES:
  74. result.blob = readKeymasterBlob(in); // byte array
  75. break;
  76. default:
  77. ALOGE("Unsupported KeyParameter tag %d", tag);
  78. return {};
  79. }
  80. return result;
  81. }
  82. android::status_t writeKeyParameterToParcel(const KeyParameter& param, android::Parcel* out) {
  83. // Method must be in sync with with KeymasterArgument.java
  84. // Presence flag must be written by caller.
  85. auto tag = param.tag;
  86. auto rc = out->writeInt32(uint32_t(tag));
  87. if (rc != ::android::OK) return rc;
  88. switch (typeFromTag(param.tag)) {
  89. case TagType::ENUM:
  90. case TagType::ENUM_REP:
  91. case TagType::UINT:
  92. case TagType::UINT_REP:
  93. rc = out->writeInt32(param.f.integer);
  94. break;
  95. case TagType::ULONG:
  96. case TagType::ULONG_REP:
  97. case TagType::DATE:
  98. rc = out->writeInt64(param.f.longInteger);
  99. break;
  100. case TagType::BOOL:
  101. // nothing to do here presence indicates true
  102. break;
  103. case TagType::BIGNUM:
  104. case TagType::BYTES:
  105. rc = writeKeymasterBlob(param.blob, out);
  106. break;
  107. default:
  108. ALOGE("Failed to write KeyParameter: Unsupported tag %d", param.tag);
  109. rc = android::BAD_VALUE;
  110. break;
  111. }
  112. return rc;
  113. }
  114. hidl_vec<KeyParameter> readParamSetFromParcel(const android::Parcel& in) {
  115. ssize_t length = in.readInt32(); // -1 for null
  116. size_t ulength = (size_t)length;
  117. if (length < 0) {
  118. ulength = 0;
  119. }
  120. hidl_vec<KeyParameter> result;
  121. result.resize(ulength);
  122. for (size_t i = 0; i < ulength; ++i) {
  123. auto param = readKeyParameterFromParcel(in);
  124. if (!param.isOk()) {
  125. ALOGE("Error reading KeyParameter from parcel");
  126. return {};
  127. }
  128. result[i] = param.value();
  129. }
  130. return result;
  131. }
  132. android::status_t writeParamSetToParcel(const hidl_vec<KeyParameter>& params,
  133. android::Parcel* out) {
  134. int32_t size = int32_t(std::min<size_t>(params.size(), std::numeric_limits<int32_t>::max()));
  135. auto rc = out->writeInt32(size);
  136. if (rc != ::android::OK) return rc;
  137. for (int32_t i = 0; i < size; ++i) {
  138. rc = out->writeInt32(1); // writeTypedObject presence flag.
  139. if (rc != ::android::OK) return rc;
  140. rc = writeKeyParameterToParcel(params[i], out);
  141. if (rc != ::android::OK) return rc;
  142. }
  143. return rc;
  144. }
  145. hidl_vec<hidl_vec<uint8_t>> readCertificateChainFromParcel(const android::Parcel& in) {
  146. hidl_vec<hidl_vec<uint8_t>> result;
  147. ssize_t count = in.readInt32();
  148. size_t ucount = count;
  149. if (count <= 0) {
  150. return result;
  151. }
  152. result.resize(ucount);
  153. for (size_t i = 0; i < ucount; ++i) {
  154. result[i] = readKeymasterBlob(in);
  155. }
  156. return result;
  157. };
  158. android::status_t writeCertificateChainToParcel(const hidl_vec<hidl_vec<uint8_t>>& certs,
  159. android::Parcel* out) {
  160. int32_t count = int32_t(std::min<size_t>(certs.size(), std::numeric_limits<int32_t>::max()));
  161. auto rc = out->writeInt32(count);
  162. for (int32_t i = 0; i < count; ++i) {
  163. rc = writeKeymasterBlob(certs[i], out);
  164. if (rc != ::android::OK) return rc;
  165. }
  166. return rc;
  167. }
  168. }; // namespace keystore
  169. // Implementation for keystore parcelables.
  170. // TODO: split implementation into separate classes
  171. namespace android {
  172. namespace security {
  173. namespace keymaster {
  174. using ::android::status_t;
  175. using ::keystore::keymaster::ErrorCode;
  176. ExportResult::ExportResult() : resultCode() {}
  177. ExportResult::~ExportResult() {}
  178. status_t ExportResult::readFromParcel(const Parcel* inn) {
  179. const Parcel& in = *inn;
  180. resultCode = ErrorCode(in.readInt32());
  181. exportData = keystore::readKeymasterBlob(in);
  182. return OK;
  183. }
  184. status_t ExportResult::writeToParcel(Parcel* out) const {
  185. out->writeInt32(resultCode.getErrorCode());
  186. return keystore::writeKeymasterBlob(exportData, out);
  187. }
  188. status_t KeyCharacteristics::readFromParcel(const Parcel* in) {
  189. softwareEnforced.readFromParcel(in);
  190. return hardwareEnforced.readFromParcel(in);
  191. }
  192. status_t KeyCharacteristics::writeToParcel(Parcel* out) const {
  193. softwareEnforced.writeToParcel(out);
  194. return hardwareEnforced.writeToParcel(out);
  195. }
  196. status_t KeymasterBlob::readFromParcel(const Parcel* in) {
  197. data_ = keystore::readKeymasterBlob(*in);
  198. return OK;
  199. }
  200. status_t KeymasterBlob::writeToParcel(Parcel* out) const {
  201. return keystore::writeKeymasterBlob(data_, out);
  202. }
  203. status_t KeymasterCertificateChain::readFromParcel(const Parcel* in) {
  204. chain = keystore::readCertificateChainFromParcel(*in);
  205. return OK;
  206. }
  207. status_t KeymasterCertificateChain::writeToParcel(Parcel* out) const {
  208. return keystore::writeCertificateChainToParcel(chain, out);
  209. }
  210. } // namespace keymaster
  211. } // namespace security
  212. } // namespace android