Janis Danisevskis | 18f27ad | 2016-06-01 13:57:40 -0700 | [diff] [blame] | 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 | */ |
David Zeuthen | f2a2867 | 2020-01-30 16:20:07 -0500 | [diff] [blame] | 16 | |
| 17 | #include <keystore/keystore_attestation_id.h> |
Janis Danisevskis | 18f27ad | 2016-06-01 13:57:40 -0700 | [diff] [blame] | 18 | |
| 19 | #define LOG_TAG "keystore_att_id" |
| 20 | |
Logan Chien | cdc813f | 2018-04-23 13:52:28 +0800 | [diff] [blame] | 21 | #include <log/log.h> |
Janis Danisevskis | 18f27ad | 2016-06-01 13:57:40 -0700 | [diff] [blame] | 22 | |
| 23 | #include <memory> |
| 24 | #include <string> |
| 25 | #include <vector> |
| 26 | |
| 27 | #include <binder/IServiceManager.h> |
| 28 | #include <binder/Parcel.h> |
| 29 | #include <binder/Parcelable.h> |
| 30 | #include <binder/PersistableBundle.h> |
| 31 | |
Shaquille Johnson | c67296e | 2024-01-22 19:13:01 +0000 | [diff] [blame] | 32 | #include <aidl/android/system/keystore2/ResponseCode.h> |
Rajesh Nyamagoud | 3f6c15c | 2023-03-11 01:26:48 +0000 | [diff] [blame] | 33 | #include <android/security/keystore/BpKeyAttestationApplicationIdProvider.h> |
| 34 | #include <android/security/keystore/IKeyAttestationApplicationIdProvider.h> |
| 35 | #include <android/security/keystore/KeyAttestationApplicationId.h> |
| 36 | #include <android/security/keystore/KeyAttestationPackageInfo.h> |
| 37 | #include <android/security/keystore/Signature.h> |
Janis Danisevskis | 18f27ad | 2016-06-01 13:57:40 -0700 | [diff] [blame] | 38 | |
Eran Messeri | ea47d3f | 2017-12-06 12:01:42 +0000 | [diff] [blame] | 39 | #include <private/android_filesystem_config.h> /* for AID_SYSTEM */ |
| 40 | |
Janis Danisevskis | 18f27ad | 2016-06-01 13:57:40 -0700 | [diff] [blame] | 41 | #include <openssl/asn1t.h> |
Eran Messeri | abaf4d8 | 2017-12-28 21:19:50 +0000 | [diff] [blame] | 42 | #include <openssl/bn.h> |
Janis Danisevskis | 18f27ad | 2016-06-01 13:57:40 -0700 | [diff] [blame] | 43 | #include <openssl/sha.h> |
| 44 | |
| 45 | #include <utils/String8.h> |
| 46 | |
| 47 | namespace android { |
| 48 | |
| 49 | namespace { |
| 50 | |
Shawn Willden | 9e8edcf | 2017-12-18 15:11:46 -0700 | [diff] [blame] | 51 | constexpr const char* kAttestationSystemPackageName = "AndroidSystem"; |
Shaquille Johnson | c67296e | 2024-01-22 19:13:01 +0000 | [diff] [blame] | 52 | constexpr const size_t kMaxAttempts = 3; |
| 53 | constexpr const unsigned long kRetryIntervalUsecs = 500000; // sleep for 500 ms |
Shawn Willden | 9e8edcf | 2017-12-18 15:11:46 -0700 | [diff] [blame] | 54 | |
Rajesh Nyamagoud | 3f6c15c | 2023-03-11 01:26:48 +0000 | [diff] [blame] | 55 | std::vector<uint8_t> signature2SHA256(const security::keystore::Signature& sig) { |
Janis Danisevskis | 18f27ad | 2016-06-01 13:57:40 -0700 | [diff] [blame] | 56 | std::vector<uint8_t> digest_buffer(SHA256_DIGEST_LENGTH); |
Rajesh Nyamagoud | 3f6c15c | 2023-03-11 01:26:48 +0000 | [diff] [blame] | 57 | SHA256(sig.data.data(), sig.data.size(), digest_buffer.data()); |
Janis Danisevskis | 18f27ad | 2016-06-01 13:57:40 -0700 | [diff] [blame] | 58 | return digest_buffer; |
| 59 | } |
| 60 | |
Shaquille Johnson | c67296e | 2024-01-22 19:13:01 +0000 | [diff] [blame] | 61 | using ::aidl::android::system::keystore2::ResponseCode; |
Rajesh Nyamagoud | 3f6c15c | 2023-03-11 01:26:48 +0000 | [diff] [blame] | 62 | using ::android::security::keystore::BpKeyAttestationApplicationIdProvider; |
Janis Danisevskis | 18f27ad | 2016-06-01 13:57:40 -0700 | [diff] [blame] | 63 | |
| 64 | class KeyAttestationApplicationIdProvider : public BpKeyAttestationApplicationIdProvider { |
| 65 | public: |
| 66 | KeyAttestationApplicationIdProvider(); |
| 67 | |
| 68 | static KeyAttestationApplicationIdProvider& get(); |
| 69 | |
| 70 | private: |
| 71 | android::sp<android::IServiceManager> service_manager_; |
| 72 | }; |
| 73 | |
| 74 | KeyAttestationApplicationIdProvider& KeyAttestationApplicationIdProvider::get() { |
| 75 | static KeyAttestationApplicationIdProvider mpm; |
| 76 | return mpm; |
| 77 | } |
| 78 | |
| 79 | KeyAttestationApplicationIdProvider::KeyAttestationApplicationIdProvider() |
Tri Vo | 8d08cd9 | 2023-07-18 14:53:39 -0400 | [diff] [blame] | 80 | : BpKeyAttestationApplicationIdProvider(android::defaultServiceManager()->waitForService( |
| 81 | String16("sec_key_att_app_id_provider"))) {} |
Janis Danisevskis | 18f27ad | 2016-06-01 13:57:40 -0700 | [diff] [blame] | 82 | |
| 83 | DECLARE_STACK_OF(ASN1_OCTET_STRING); |
| 84 | |
| 85 | typedef struct km_attestation_package_info { |
| 86 | ASN1_OCTET_STRING* package_name; |
| 87 | ASN1_INTEGER* version; |
Janis Danisevskis | 18f27ad | 2016-06-01 13:57:40 -0700 | [diff] [blame] | 88 | } KM_ATTESTATION_PACKAGE_INFO; |
| 89 | |
Eran Messeri | 03fc4c8 | 2018-08-16 18:53:15 +0100 | [diff] [blame] | 90 | // Estimated size: |
| 91 | // 4 bytes for the package name + package_name length |
| 92 | // 11 bytes for the version (2 bytes header and up to 9 bytes of data). |
| 93 | constexpr size_t AAID_PKG_INFO_OVERHEAD = 15; |
Janis Danisevskis | 18f27ad | 2016-06-01 13:57:40 -0700 | [diff] [blame] | 94 | ASN1_SEQUENCE(KM_ATTESTATION_PACKAGE_INFO) = { |
| 95 | ASN1_SIMPLE(KM_ATTESTATION_PACKAGE_INFO, package_name, ASN1_OCTET_STRING), |
| 96 | ASN1_SIMPLE(KM_ATTESTATION_PACKAGE_INFO, version, ASN1_INTEGER), |
Janis Danisevskis | 18f27ad | 2016-06-01 13:57:40 -0700 | [diff] [blame] | 97 | } ASN1_SEQUENCE_END(KM_ATTESTATION_PACKAGE_INFO); |
| 98 | IMPLEMENT_ASN1_FUNCTIONS(KM_ATTESTATION_PACKAGE_INFO); |
| 99 | |
| 100 | DECLARE_STACK_OF(KM_ATTESTATION_PACKAGE_INFO); |
| 101 | |
Eran Messeri | 03fc4c8 | 2018-08-16 18:53:15 +0100 | [diff] [blame] | 102 | // Estimated size: |
| 103 | // See estimate above for the stack of package infos. |
| 104 | // 34 (32 + 2) bytes for each signature digest. |
| 105 | constexpr size_t AAID_SIGNATURE_SIZE = 34; |
Janis Danisevskis | 18f27ad | 2016-06-01 13:57:40 -0700 | [diff] [blame] | 106 | typedef struct km_attestation_application_id { |
| 107 | STACK_OF(KM_ATTESTATION_PACKAGE_INFO) * package_infos; |
Janis Danisevskis | 011675d | 2016-09-01 11:41:29 +0100 | [diff] [blame] | 108 | STACK_OF(ASN1_OCTET_STRING) * signature_digests; |
Janis Danisevskis | 18f27ad | 2016-06-01 13:57:40 -0700 | [diff] [blame] | 109 | } KM_ATTESTATION_APPLICATION_ID; |
| 110 | |
Eran Messeri | 03fc4c8 | 2018-08-16 18:53:15 +0100 | [diff] [blame] | 111 | // Estimated overhead: |
| 112 | // 4 for the header of the octet string containing the fully-encoded data. |
| 113 | // 4 for the sequence header. |
| 114 | // 4 for the header of the package info set. |
| 115 | // 4 for the header of the signature set. |
| 116 | constexpr size_t AAID_GENERAL_OVERHEAD = 16; |
Janis Danisevskis | 18f27ad | 2016-06-01 13:57:40 -0700 | [diff] [blame] | 117 | ASN1_SEQUENCE(KM_ATTESTATION_APPLICATION_ID) = { |
| 118 | ASN1_SET_OF(KM_ATTESTATION_APPLICATION_ID, package_infos, KM_ATTESTATION_PACKAGE_INFO), |
Janis Danisevskis | 011675d | 2016-09-01 11:41:29 +0100 | [diff] [blame] | 119 | ASN1_SET_OF(KM_ATTESTATION_APPLICATION_ID, signature_digests, ASN1_OCTET_STRING), |
Janis Danisevskis | 18f27ad | 2016-06-01 13:57:40 -0700 | [diff] [blame] | 120 | } ASN1_SEQUENCE_END(KM_ATTESTATION_APPLICATION_ID); |
| 121 | IMPLEMENT_ASN1_FUNCTIONS(KM_ATTESTATION_APPLICATION_ID); |
Shawn Willden | 9e8edcf | 2017-12-18 15:11:46 -0700 | [diff] [blame] | 122 | |
| 123 | } // namespace |
Janis Danisevskis | 18f27ad | 2016-06-01 13:57:40 -0700 | [diff] [blame] | 124 | |
| 125 | } // namespace android |
| 126 | |
| 127 | namespace std { |
| 128 | template <> struct default_delete<android::KM_ATTESTATION_PACKAGE_INFO> { |
| 129 | void operator()(android::KM_ATTESTATION_PACKAGE_INFO* p) { |
| 130 | android::KM_ATTESTATION_PACKAGE_INFO_free(p); |
| 131 | } |
| 132 | }; |
| 133 | template <> struct default_delete<ASN1_OCTET_STRING> { |
| 134 | void operator()(ASN1_OCTET_STRING* p) { ASN1_OCTET_STRING_free(p); } |
| 135 | }; |
| 136 | template <> struct default_delete<android::KM_ATTESTATION_APPLICATION_ID> { |
| 137 | void operator()(android::KM_ATTESTATION_APPLICATION_ID* p) { |
| 138 | android::KM_ATTESTATION_APPLICATION_ID_free(p); |
| 139 | } |
| 140 | }; |
| 141 | } // namespace std |
| 142 | |
| 143 | namespace android { |
| 144 | namespace security { |
| 145 | namespace { |
| 146 | |
Rajesh Nyamagoud | 3f6c15c | 2023-03-11 01:26:48 +0000 | [diff] [blame] | 147 | using ::android::security::keystore::KeyAttestationApplicationId; |
| 148 | using ::android::security::keystore::KeyAttestationPackageInfo; |
Janis Danisevskis | 18f27ad | 2016-06-01 13:57:40 -0700 | [diff] [blame] | 149 | |
Janis Danisevskis | 011675d | 2016-09-01 11:41:29 +0100 | [diff] [blame] | 150 | status_t build_attestation_package_info(const KeyAttestationPackageInfo& pinfo, |
Janis Danisevskis | 18f27ad | 2016-06-01 13:57:40 -0700 | [diff] [blame] | 151 | std::unique_ptr<KM_ATTESTATION_PACKAGE_INFO>* attestation_package_info_ptr) { |
| 152 | |
| 153 | if (!attestation_package_info_ptr) return BAD_VALUE; |
| 154 | auto& attestation_package_info = *attestation_package_info_ptr; |
| 155 | |
| 156 | attestation_package_info.reset(KM_ATTESTATION_PACKAGE_INFO_new()); |
| 157 | if (!attestation_package_info.get()) return NO_MEMORY; |
| 158 | |
Rajesh Nyamagoud | 3f6c15c | 2023-03-11 01:26:48 +0000 | [diff] [blame] | 159 | if (!pinfo.packageName) { |
Janis Danisevskis | 011675d | 2016-09-01 11:41:29 +0100 | [diff] [blame] | 160 | ALOGE("Key attestation package info lacks package name"); |
| 161 | return BAD_VALUE; |
| 162 | } |
| 163 | |
Rajesh Nyamagoud | 3f6c15c | 2023-03-11 01:26:48 +0000 | [diff] [blame] | 164 | std::string pkg_name(String8(pinfo.packageName).c_str()); |
Janis Danisevskis | 18f27ad | 2016-06-01 13:57:40 -0700 | [diff] [blame] | 165 | if (!ASN1_OCTET_STRING_set(attestation_package_info->package_name, |
| 166 | reinterpret_cast<const unsigned char*>(pkg_name.data()), |
| 167 | pkg_name.size())) { |
| 168 | return UNKNOWN_ERROR; |
| 169 | } |
| 170 | |
Eran Messeri | abaf4d8 | 2017-12-28 21:19:50 +0000 | [diff] [blame] | 171 | BIGNUM* bn_version = BN_new(); |
| 172 | if (bn_version == nullptr) { |
| 173 | return NO_MEMORY; |
| 174 | } |
Rajesh Nyamagoud | 3f6c15c | 2023-03-11 01:26:48 +0000 | [diff] [blame] | 175 | if (BN_set_u64(bn_version, static_cast<uint64_t>(pinfo.versionCode)) != 1) { |
Eran Messeri | abaf4d8 | 2017-12-28 21:19:50 +0000 | [diff] [blame] | 176 | BN_free(bn_version); |
Janis Danisevskis | 011675d | 2016-09-01 11:41:29 +0100 | [diff] [blame] | 177 | return UNKNOWN_ERROR; |
| 178 | } |
Eran Messeri | abaf4d8 | 2017-12-28 21:19:50 +0000 | [diff] [blame] | 179 | status_t retval = NO_ERROR; |
| 180 | if (BN_to_ASN1_INTEGER(bn_version, attestation_package_info->version) == nullptr) { |
| 181 | retval = UNKNOWN_ERROR; |
| 182 | } |
| 183 | BN_free(bn_version); |
| 184 | return retval; |
Janis Danisevskis | 011675d | 2016-09-01 11:41:29 +0100 | [diff] [blame] | 185 | } |
Janis Danisevskis | 18f27ad | 2016-06-01 13:57:40 -0700 | [diff] [blame] | 186 | |
Eran Messeri | 03fc4c8 | 2018-08-16 18:53:15 +0100 | [diff] [blame] | 187 | /* The following function are not used. They are mentioned here to silence |
| 188 | * warnings about them not being used. |
| 189 | */ |
| 190 | void unused_functions_silencer() __attribute__((unused)); |
| 191 | void unused_functions_silencer() { |
| 192 | i2d_KM_ATTESTATION_PACKAGE_INFO(nullptr, nullptr); |
| 193 | d2i_KM_ATTESTATION_APPLICATION_ID(nullptr, nullptr, 0); |
| 194 | d2i_KM_ATTESTATION_PACKAGE_INFO(nullptr, nullptr, 0); |
| 195 | } |
| 196 | |
| 197 | } // namespace |
| 198 | |
Janis Danisevskis | 011675d | 2016-09-01 11:41:29 +0100 | [diff] [blame] | 199 | StatusOr<std::vector<uint8_t>> |
| 200 | build_attestation_application_id(const KeyAttestationApplicationId& key_attestation_id) { |
| 201 | auto attestation_id = |
| 202 | std::unique_ptr<KM_ATTESTATION_APPLICATION_ID>(KM_ATTESTATION_APPLICATION_ID_new()); |
Eran Messeri | 03fc4c8 | 2018-08-16 18:53:15 +0100 | [diff] [blame] | 203 | size_t estimated_encoded_size = AAID_GENERAL_OVERHEAD; |
Janis Danisevskis | 18f27ad | 2016-06-01 13:57:40 -0700 | [diff] [blame] | 204 | |
Janis Danisevskis | 011675d | 2016-09-01 11:41:29 +0100 | [diff] [blame] | 205 | auto attestation_pinfo_stack = reinterpret_cast<_STACK*>(attestation_id->package_infos); |
| 206 | |
Rajesh Nyamagoud | 3f6c15c | 2023-03-11 01:26:48 +0000 | [diff] [blame] | 207 | if (key_attestation_id.packageInfos.begin() == key_attestation_id.packageInfos.end()) |
| 208 | return BAD_VALUE; |
Janis Danisevskis | 011675d | 2016-09-01 11:41:29 +0100 | [diff] [blame] | 209 | |
Rajesh Nyamagoud | 3f6c15c | 2023-03-11 01:26:48 +0000 | [diff] [blame] | 210 | for (auto pinfo = key_attestation_id.packageInfos.begin(); |
| 211 | pinfo != key_attestation_id.packageInfos.end(); ++pinfo) { |
| 212 | if (!pinfo->packageName) { |
Janis Danisevskis | 011675d | 2016-09-01 11:41:29 +0100 | [diff] [blame] | 213 | ALOGE("Key attestation package info lacks package name"); |
| 214 | return BAD_VALUE; |
| 215 | } |
Rajesh Nyamagoud | 3f6c15c | 2023-03-11 01:26:48 +0000 | [diff] [blame] | 216 | std::string package_name(String8(pinfo->packageName).c_str()); |
Janis Danisevskis | 011675d | 2016-09-01 11:41:29 +0100 | [diff] [blame] | 217 | std::unique_ptr<KM_ATTESTATION_PACKAGE_INFO> attestation_package_info; |
| 218 | auto rc = build_attestation_package_info(*pinfo, &attestation_package_info); |
| 219 | if (rc != NO_ERROR) { |
| 220 | ALOGE("Building DER attestation package info failed %d", rc); |
| 221 | return rc; |
| 222 | } |
Eran Messeri | 03fc4c8 | 2018-08-16 18:53:15 +0100 | [diff] [blame] | 223 | estimated_encoded_size += AAID_PKG_INFO_OVERHEAD + package_name.size(); |
| 224 | if (estimated_encoded_size > KEY_ATTESTATION_APPLICATION_ID_MAX_SIZE) { |
| 225 | break; |
| 226 | } |
Janis Danisevskis | 011675d | 2016-09-01 11:41:29 +0100 | [diff] [blame] | 227 | if (!sk_push(attestation_pinfo_stack, attestation_package_info.get())) { |
| 228 | return NO_MEMORY; |
| 229 | } |
| 230 | // if push succeeded, the stack takes ownership |
| 231 | attestation_package_info.release(); |
| 232 | } |
| 233 | |
| 234 | /** Apps can only share a uid iff they were signed with the same certificate(s). Because the |
| 235 | * signature field actually holds the signing certificate, rather than a signature, we can |
| 236 | * simply use the set of signature digests of the first package info. |
| 237 | */ |
Rajesh Nyamagoud | 3f6c15c | 2023-03-11 01:26:48 +0000 | [diff] [blame] | 238 | const auto& pinfo = *key_attestation_id.packageInfos.begin(); |
Janis Danisevskis | 011675d | 2016-09-01 11:41:29 +0100 | [diff] [blame] | 239 | std::vector<std::vector<uint8_t>> signature_digests; |
| 240 | |
Rajesh Nyamagoud | 3f6c15c | 2023-03-11 01:26:48 +0000 | [diff] [blame] | 241 | for (auto sig = pinfo.signatures.begin(); sig != pinfo.signatures.end(); ++sig) { |
Janis Danisevskis | 011675d | 2016-09-01 11:41:29 +0100 | [diff] [blame] | 242 | signature_digests.push_back(signature2SHA256(*sig)); |
| 243 | } |
| 244 | |
| 245 | auto signature_digest_stack = reinterpret_cast<_STACK*>(attestation_id->signature_digests); |
Janis Danisevskis | 18f27ad | 2016-06-01 13:57:40 -0700 | [diff] [blame] | 246 | for (auto si : signature_digests) { |
Eran Messeri | 03fc4c8 | 2018-08-16 18:53:15 +0100 | [diff] [blame] | 247 | estimated_encoded_size += AAID_SIGNATURE_SIZE; |
| 248 | if (estimated_encoded_size > KEY_ATTESTATION_APPLICATION_ID_MAX_SIZE) { |
| 249 | break; |
| 250 | } |
Janis Danisevskis | 18f27ad | 2016-06-01 13:57:40 -0700 | [diff] [blame] | 251 | auto asn1_item = std::unique_ptr<ASN1_OCTET_STRING>(ASN1_OCTET_STRING_new()); |
| 252 | if (!asn1_item) return NO_MEMORY; |
| 253 | if (!ASN1_OCTET_STRING_set(asn1_item.get(), si.data(), si.size())) { |
| 254 | return UNKNOWN_ERROR; |
| 255 | } |
| 256 | if (!sk_push(signature_digest_stack, asn1_item.get())) { |
| 257 | return NO_MEMORY; |
| 258 | } |
| 259 | asn1_item.release(); // if push succeeded, the stack takes ownership |
| 260 | } |
| 261 | |
Janis Danisevskis | 18f27ad | 2016-06-01 13:57:40 -0700 | [diff] [blame] | 262 | int len = i2d_KM_ATTESTATION_APPLICATION_ID(attestation_id.get(), nullptr); |
Janis Danisevskis | 011675d | 2016-09-01 11:41:29 +0100 | [diff] [blame] | 263 | if (len < 0) return UNKNOWN_ERROR; |
| 264 | |
| 265 | std::vector<uint8_t> result(len); |
| 266 | uint8_t* p = result.data(); |
Janis Danisevskis | 18f27ad | 2016-06-01 13:57:40 -0700 | [diff] [blame] | 267 | len = i2d_KM_ATTESTATION_APPLICATION_ID(attestation_id.get(), &p); |
Janis Danisevskis | 011675d | 2016-09-01 11:41:29 +0100 | [diff] [blame] | 268 | if (len < 0) return UNKNOWN_ERROR; |
Janis Danisevskis | 18f27ad | 2016-06-01 13:57:40 -0700 | [diff] [blame] | 269 | |
| 270 | return result; |
| 271 | } |
| 272 | |
Janis Danisevskis | 011675d | 2016-09-01 11:41:29 +0100 | [diff] [blame] | 273 | StatusOr<std::vector<uint8_t>> gather_attestation_application_id(uid_t uid) { |
Shawn Willden | 9e8edcf | 2017-12-18 15:11:46 -0700 | [diff] [blame] | 274 | KeyAttestationApplicationId key_attestation_id; |
Janis Danisevskis | 18f27ad | 2016-06-01 13:57:40 -0700 | [diff] [blame] | 275 | |
Rajesh Nyamagoud | 41e98ee | 2024-06-26 01:11:49 +0000 | [diff] [blame] | 276 | if (uid == AID_SYSTEM || uid == AID_ROOT) { |
Shawn Willden | 9e8edcf | 2017-12-18 15:11:46 -0700 | [diff] [blame] | 277 | /* Use a fixed ID for system callers */ |
Rajesh Nyamagoud | 3f6c15c | 2023-03-11 01:26:48 +0000 | [diff] [blame] | 278 | auto pinfo = KeyAttestationPackageInfo(); |
| 279 | pinfo.packageName = String16(kAttestationSystemPackageName); |
| 280 | pinfo.versionCode = 1; |
| 281 | key_attestation_id.packageInfos.push_back(std::move(pinfo)); |
Eran Messeri | ea47d3f | 2017-12-06 12:01:42 +0000 | [diff] [blame] | 282 | } else { |
Shawn Willden | 9e8edcf | 2017-12-18 15:11:46 -0700 | [diff] [blame] | 283 | /* Get the attestation application ID from package manager */ |
| 284 | auto& pm = KeyAttestationApplicationIdProvider::get(); |
Shaquille Johnson | c67296e | 2024-01-22 19:13:01 +0000 | [diff] [blame] | 285 | ::android::binder::Status status; |
| 286 | |
| 287 | // Retry on failure if a service specific error code. |
| 288 | for (size_t attempt{0}; attempt < kMaxAttempts; ++attempt) { |
| 289 | status = pm.getKeyAttestationApplicationId(uid, &key_attestation_id); |
| 290 | if (status.isOk()) { |
| 291 | break; |
| 292 | } else if (status.exceptionCode() != binder::Status::EX_SERVICE_SPECIFIC) { |
| 293 | ALOGW("Retry: key attestation ID failed with service specific error: %s %d", |
| 294 | status.exceptionMessage().c_str(), status.serviceSpecificErrorCode()); |
| 295 | usleep(kRetryIntervalUsecs); |
| 296 | } else { |
| 297 | ALOGW("Retry: key attestation ID failed with error: %s %d", |
| 298 | status.exceptionMessage().c_str(), status.exceptionCode()); |
| 299 | usleep(kRetryIntervalUsecs); |
| 300 | } |
| 301 | } |
| 302 | |
Eran Messeri | ea47d3f | 2017-12-06 12:01:42 +0000 | [diff] [blame] | 303 | if (!status.isOk()) { |
Eran Messeri | 03fc4c8 | 2018-08-16 18:53:15 +0100 | [diff] [blame] | 304 | ALOGW("package manager request for key attestation ID failed with: %s %d", |
Tomasz Wasilczyk | 102d33a | 2023-08-11 16:10:37 +0000 | [diff] [blame] | 305 | status.exceptionMessage().c_str(), status.exceptionCode()); |
Rajesh Nyamagoud | 3f6c15c | 2023-03-11 01:26:48 +0000 | [diff] [blame] | 306 | |
Shaquille Johnson | c67296e | 2024-01-22 19:13:01 +0000 | [diff] [blame] | 307 | return int32_t(ResponseCode::GET_ATTESTATION_APPLICATION_ID_FAILED); |
Eran Messeri | ea47d3f | 2017-12-06 12:01:42 +0000 | [diff] [blame] | 308 | } |
Janis Danisevskis | 18f27ad | 2016-06-01 13:57:40 -0700 | [diff] [blame] | 309 | } |
| 310 | |
| 311 | /* DER encode the attestation application ID */ |
Shawn Willden | 9e8edcf | 2017-12-18 15:11:46 -0700 | [diff] [blame] | 312 | return build_attestation_application_id(key_attestation_id); |
Janis Danisevskis | 18f27ad | 2016-06-01 13:57:40 -0700 | [diff] [blame] | 313 | } |
| 314 | |
| 315 | } // namespace security |
| 316 | } // namespace android |