keystore_attestation_id.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298
  1. /*
  2. * Copyright (C) 2016 The Android Open Source Project
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. #include "keystore_attestation_id.h"
  17. #define LOG_TAG "keystore_att_id"
  18. #include <log/log.h>
  19. #include <memory>
  20. #include <string>
  21. #include <vector>
  22. #include <binder/IServiceManager.h>
  23. #include <binder/Parcel.h>
  24. #include <binder/Parcelable.h>
  25. #include <binder/PersistableBundle.h>
  26. #include <android/security/keymaster/BpKeyAttestationApplicationIdProvider.h>
  27. #include <android/security/keymaster/IKeyAttestationApplicationIdProvider.h>
  28. #include <keystore/KeyAttestationApplicationId.h>
  29. #include <keystore/KeyAttestationPackageInfo.h>
  30. #include <keystore/Signature.h>
  31. #include <private/android_filesystem_config.h> /* for AID_SYSTEM */
  32. #include <openssl/asn1t.h>
  33. #include <openssl/bn.h>
  34. #include <openssl/sha.h>
  35. #include <utils/String8.h>
  36. namespace android {
  37. namespace {
  38. constexpr const char* kAttestationSystemPackageName = "AndroidSystem";
  39. constexpr const char* kUnknownPackageName = "UnknownPackage";
  40. std::vector<uint8_t> signature2SHA256(const content::pm::Signature& sig) {
  41. std::vector<uint8_t> digest_buffer(SHA256_DIGEST_LENGTH);
  42. SHA256(sig.data().data(), sig.data().size(), digest_buffer.data());
  43. return digest_buffer;
  44. }
  45. using ::android::security::keymaster::BpKeyAttestationApplicationIdProvider;
  46. class KeyAttestationApplicationIdProvider : public BpKeyAttestationApplicationIdProvider {
  47. public:
  48. KeyAttestationApplicationIdProvider();
  49. static KeyAttestationApplicationIdProvider& get();
  50. private:
  51. android::sp<android::IServiceManager> service_manager_;
  52. };
  53. KeyAttestationApplicationIdProvider& KeyAttestationApplicationIdProvider::get() {
  54. static KeyAttestationApplicationIdProvider mpm;
  55. return mpm;
  56. }
  57. KeyAttestationApplicationIdProvider::KeyAttestationApplicationIdProvider()
  58. : BpKeyAttestationApplicationIdProvider(
  59. android::defaultServiceManager()->getService(String16("sec_key_att_app_id_provider"))) {}
  60. DECLARE_STACK_OF(ASN1_OCTET_STRING);
  61. typedef struct km_attestation_package_info {
  62. ASN1_OCTET_STRING* package_name;
  63. ASN1_INTEGER* version;
  64. } KM_ATTESTATION_PACKAGE_INFO;
  65. // Estimated size:
  66. // 4 bytes for the package name + package_name length
  67. // 11 bytes for the version (2 bytes header and up to 9 bytes of data).
  68. constexpr size_t AAID_PKG_INFO_OVERHEAD = 15;
  69. ASN1_SEQUENCE(KM_ATTESTATION_PACKAGE_INFO) = {
  70. ASN1_SIMPLE(KM_ATTESTATION_PACKAGE_INFO, package_name, ASN1_OCTET_STRING),
  71. ASN1_SIMPLE(KM_ATTESTATION_PACKAGE_INFO, version, ASN1_INTEGER),
  72. } ASN1_SEQUENCE_END(KM_ATTESTATION_PACKAGE_INFO);
  73. IMPLEMENT_ASN1_FUNCTIONS(KM_ATTESTATION_PACKAGE_INFO);
  74. DECLARE_STACK_OF(KM_ATTESTATION_PACKAGE_INFO);
  75. // Estimated size:
  76. // See estimate above for the stack of package infos.
  77. // 34 (32 + 2) bytes for each signature digest.
  78. constexpr size_t AAID_SIGNATURE_SIZE = 34;
  79. typedef struct km_attestation_application_id {
  80. STACK_OF(KM_ATTESTATION_PACKAGE_INFO) * package_infos;
  81. STACK_OF(ASN1_OCTET_STRING) * signature_digests;
  82. } KM_ATTESTATION_APPLICATION_ID;
  83. // Estimated overhead:
  84. // 4 for the header of the octet string containing the fully-encoded data.
  85. // 4 for the sequence header.
  86. // 4 for the header of the package info set.
  87. // 4 for the header of the signature set.
  88. constexpr size_t AAID_GENERAL_OVERHEAD = 16;
  89. ASN1_SEQUENCE(KM_ATTESTATION_APPLICATION_ID) = {
  90. ASN1_SET_OF(KM_ATTESTATION_APPLICATION_ID, package_infos, KM_ATTESTATION_PACKAGE_INFO),
  91. ASN1_SET_OF(KM_ATTESTATION_APPLICATION_ID, signature_digests, ASN1_OCTET_STRING),
  92. } ASN1_SEQUENCE_END(KM_ATTESTATION_APPLICATION_ID);
  93. IMPLEMENT_ASN1_FUNCTIONS(KM_ATTESTATION_APPLICATION_ID);
  94. } // namespace
  95. } // namespace android
  96. namespace std {
  97. template <> struct default_delete<android::KM_ATTESTATION_PACKAGE_INFO> {
  98. void operator()(android::KM_ATTESTATION_PACKAGE_INFO* p) {
  99. android::KM_ATTESTATION_PACKAGE_INFO_free(p);
  100. }
  101. };
  102. template <> struct default_delete<ASN1_OCTET_STRING> {
  103. void operator()(ASN1_OCTET_STRING* p) { ASN1_OCTET_STRING_free(p); }
  104. };
  105. template <> struct default_delete<android::KM_ATTESTATION_APPLICATION_ID> {
  106. void operator()(android::KM_ATTESTATION_APPLICATION_ID* p) {
  107. android::KM_ATTESTATION_APPLICATION_ID_free(p);
  108. }
  109. };
  110. } // namespace std
  111. namespace android {
  112. namespace security {
  113. namespace {
  114. using ::android::security::keymaster::KeyAttestationApplicationId;
  115. using ::android::security::keymaster::KeyAttestationPackageInfo;
  116. status_t build_attestation_package_info(const KeyAttestationPackageInfo& pinfo,
  117. std::unique_ptr<KM_ATTESTATION_PACKAGE_INFO>* attestation_package_info_ptr) {
  118. if (!attestation_package_info_ptr) return BAD_VALUE;
  119. auto& attestation_package_info = *attestation_package_info_ptr;
  120. attestation_package_info.reset(KM_ATTESTATION_PACKAGE_INFO_new());
  121. if (!attestation_package_info.get()) return NO_MEMORY;
  122. if (!pinfo.package_name()) {
  123. ALOGE("Key attestation package info lacks package name");
  124. return BAD_VALUE;
  125. }
  126. std::string pkg_name(String8(*pinfo.package_name()).string());
  127. if (!ASN1_OCTET_STRING_set(attestation_package_info->package_name,
  128. reinterpret_cast<const unsigned char*>(pkg_name.data()),
  129. pkg_name.size())) {
  130. return UNKNOWN_ERROR;
  131. }
  132. BIGNUM* bn_version = BN_new();
  133. if (bn_version == nullptr) {
  134. return NO_MEMORY;
  135. }
  136. if (BN_set_u64(bn_version, static_cast<uint64_t>(pinfo.version_code())) != 1) {
  137. BN_free(bn_version);
  138. return UNKNOWN_ERROR;
  139. }
  140. status_t retval = NO_ERROR;
  141. if (BN_to_ASN1_INTEGER(bn_version, attestation_package_info->version) == nullptr) {
  142. retval = UNKNOWN_ERROR;
  143. }
  144. BN_free(bn_version);
  145. return retval;
  146. }
  147. /* The following function are not used. They are mentioned here to silence
  148. * warnings about them not being used.
  149. */
  150. void unused_functions_silencer() __attribute__((unused));
  151. void unused_functions_silencer() {
  152. i2d_KM_ATTESTATION_PACKAGE_INFO(nullptr, nullptr);
  153. d2i_KM_ATTESTATION_APPLICATION_ID(nullptr, nullptr, 0);
  154. d2i_KM_ATTESTATION_PACKAGE_INFO(nullptr, nullptr, 0);
  155. }
  156. } // namespace
  157. StatusOr<std::vector<uint8_t>>
  158. build_attestation_application_id(const KeyAttestationApplicationId& key_attestation_id) {
  159. auto attestation_id =
  160. std::unique_ptr<KM_ATTESTATION_APPLICATION_ID>(KM_ATTESTATION_APPLICATION_ID_new());
  161. size_t estimated_encoded_size = AAID_GENERAL_OVERHEAD;
  162. auto attestation_pinfo_stack = reinterpret_cast<_STACK*>(attestation_id->package_infos);
  163. if (key_attestation_id.pinfos_begin() == key_attestation_id.pinfos_end()) return BAD_VALUE;
  164. for (auto pinfo = key_attestation_id.pinfos_begin(); pinfo != key_attestation_id.pinfos_end();
  165. ++pinfo) {
  166. if (!pinfo->package_name()) {
  167. ALOGE("Key attestation package info lacks package name");
  168. return BAD_VALUE;
  169. }
  170. std::string package_name(String8(*pinfo->package_name()).string());
  171. std::unique_ptr<KM_ATTESTATION_PACKAGE_INFO> attestation_package_info;
  172. auto rc = build_attestation_package_info(*pinfo, &attestation_package_info);
  173. if (rc != NO_ERROR) {
  174. ALOGE("Building DER attestation package info failed %d", rc);
  175. return rc;
  176. }
  177. estimated_encoded_size += AAID_PKG_INFO_OVERHEAD + package_name.size();
  178. if (estimated_encoded_size > KEY_ATTESTATION_APPLICATION_ID_MAX_SIZE) {
  179. break;
  180. }
  181. if (!sk_push(attestation_pinfo_stack, attestation_package_info.get())) {
  182. return NO_MEMORY;
  183. }
  184. // if push succeeded, the stack takes ownership
  185. attestation_package_info.release();
  186. }
  187. /** Apps can only share a uid iff they were signed with the same certificate(s). Because the
  188. * signature field actually holds the signing certificate, rather than a signature, we can
  189. * simply use the set of signature digests of the first package info.
  190. */
  191. const auto& pinfo = *key_attestation_id.pinfos_begin();
  192. std::vector<std::vector<uint8_t>> signature_digests;
  193. for (auto sig = pinfo.sigs_begin(); sig != pinfo.sigs_end(); ++sig) {
  194. signature_digests.push_back(signature2SHA256(*sig));
  195. }
  196. auto signature_digest_stack = reinterpret_cast<_STACK*>(attestation_id->signature_digests);
  197. for (auto si : signature_digests) {
  198. estimated_encoded_size += AAID_SIGNATURE_SIZE;
  199. if (estimated_encoded_size > KEY_ATTESTATION_APPLICATION_ID_MAX_SIZE) {
  200. break;
  201. }
  202. auto asn1_item = std::unique_ptr<ASN1_OCTET_STRING>(ASN1_OCTET_STRING_new());
  203. if (!asn1_item) return NO_MEMORY;
  204. if (!ASN1_OCTET_STRING_set(asn1_item.get(), si.data(), si.size())) {
  205. return UNKNOWN_ERROR;
  206. }
  207. if (!sk_push(signature_digest_stack, asn1_item.get())) {
  208. return NO_MEMORY;
  209. }
  210. asn1_item.release(); // if push succeeded, the stack takes ownership
  211. }
  212. int len = i2d_KM_ATTESTATION_APPLICATION_ID(attestation_id.get(), nullptr);
  213. if (len < 0) return UNKNOWN_ERROR;
  214. std::vector<uint8_t> result(len);
  215. uint8_t* p = result.data();
  216. len = i2d_KM_ATTESTATION_APPLICATION_ID(attestation_id.get(), &p);
  217. if (len < 0) return UNKNOWN_ERROR;
  218. return result;
  219. }
  220. StatusOr<std::vector<uint8_t>> gather_attestation_application_id(uid_t uid) {
  221. KeyAttestationApplicationId key_attestation_id;
  222. if (uid == AID_SYSTEM) {
  223. /* Use a fixed ID for system callers */
  224. auto pinfo = std::make_unique<KeyAttestationPackageInfo>(
  225. String16(kAttestationSystemPackageName), 1 /* version code */,
  226. std::make_shared<KeyAttestationPackageInfo::SignaturesVector>());
  227. key_attestation_id = KeyAttestationApplicationId(std::move(pinfo));
  228. } else {
  229. /* Get the attestation application ID from package manager */
  230. auto& pm = KeyAttestationApplicationIdProvider::get();
  231. auto status = pm.getKeyAttestationApplicationId(uid, &key_attestation_id);
  232. // Package Manager call has failed, perform attestation but indicate that the
  233. // caller is unknown.
  234. if (!status.isOk()) {
  235. ALOGW("package manager request for key attestation ID failed with: %s %d",
  236. status.exceptionMessage().string(), status.exceptionCode());
  237. auto pinfo = std::make_unique<KeyAttestationPackageInfo>(
  238. String16(kUnknownPackageName), 1 /* version code */,
  239. std::make_shared<KeyAttestationPackageInfo::SignaturesVector>());
  240. key_attestation_id = KeyAttestationApplicationId(std::move(pinfo));
  241. }
  242. }
  243. /* DER encode the attestation application ID */
  244. return build_attestation_application_id(key_attestation_id);
  245. }
  246. } // namespace security
  247. } // namespace android