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