blob: c91f86fdd94406d193f1e5a59c40eb1897a2e0a1 [file] [log] [blame]
Janis Danisevskis18f27ad2016-06-01 13:57:40 -07001/*
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 Zeuthenf2a28672020-01-30 16:20:07 -050016
17#include <keystore/keystore_attestation_id.h>
Janis Danisevskis18f27ad2016-06-01 13:57:40 -070018
19#define LOG_TAG "keystore_att_id"
20
Logan Chiencdc813f2018-04-23 13:52:28 +080021#include <log/log.h>
Janis Danisevskis18f27ad2016-06-01 13:57:40 -070022
23#include <memory>
David Drysdaleb0be7ed2024-05-28 10:19:03 +010024#include <mutex>
Janis Danisevskis18f27ad2016-06-01 13:57:40 -070025#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 Johnsonc67296e2024-01-22 19:13:01 +000033#include <aidl/android/system/keystore2/ResponseCode.h>
Rajesh Nyamagoud3f6c15c2023-03-11 01:26:48 +000034#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 Danisevskis18f27ad2016-06-01 13:57:40 -070039
Eran Messeriea47d3f2017-12-06 12:01:42 +000040#include <private/android_filesystem_config.h> /* for AID_SYSTEM */
41
Janis Danisevskis18f27ad2016-06-01 13:57:40 -070042#include <openssl/asn1t.h>
Eran Messeriabaf4d82017-12-28 21:19:50 +000043#include <openssl/bn.h>
Janis Danisevskis18f27ad2016-06-01 13:57:40 -070044#include <openssl/sha.h>
45
46#include <utils/String8.h>
47
48namespace android {
49
50namespace {
51
Shawn Willden9e8edcf2017-12-18 15:11:46 -070052constexpr const char* kAttestationSystemPackageName = "AndroidSystem";
Shaquille Johnsonc67296e2024-01-22 19:13:01 +000053constexpr const size_t kMaxAttempts = 3;
54constexpr const unsigned long kRetryIntervalUsecs = 500000; // sleep for 500 ms
David Drysdaleb0be7ed2024-05-28 10:19:03 +010055constexpr const char* kProviderServiceName = "sec_key_att_app_id_provider";
Shawn Willden9e8edcf2017-12-18 15:11:46 -070056
Rajesh Nyamagoud3f6c15c2023-03-11 01:26:48 +000057std::vector<uint8_t> signature2SHA256(const security::keystore::Signature& sig) {
Janis Danisevskis18f27ad2016-06-01 13:57:40 -070058 std::vector<uint8_t> digest_buffer(SHA256_DIGEST_LENGTH);
Rajesh Nyamagoud3f6c15c2023-03-11 01:26:48 +000059 SHA256(sig.data.data(), sig.data.size(), digest_buffer.data());
Janis Danisevskis18f27ad2016-06-01 13:57:40 -070060 return digest_buffer;
61}
62
Shaquille Johnsonc67296e2024-01-22 19:13:01 +000063using ::aidl::android::system::keystore2::ResponseCode;
Rajesh Nyamagoud3f6c15c2023-03-11 01:26:48 +000064using ::android::security::keystore::BpKeyAttestationApplicationIdProvider;
Janis Danisevskis18f27ad2016-06-01 13:57:40 -070065
David Drysdaleb0be7ed2024-05-28 10:19:03 +010066[[clang::no_destroy]] std::mutex gServiceMu;
67[[clang::no_destroy]] std::shared_ptr<BpKeyAttestationApplicationIdProvider>
68 gService; // GUARDED_BY gServiceMu
Janis Danisevskis18f27ad2016-06-01 13:57:40 -070069
David Drysdaleb0be7ed2024-05-28 10:19:03 +010070std::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 Danisevskis18f27ad2016-06-01 13:57:40 -070077}
78
David Drysdaleb0be7ed2024-05-28 10:19:03 +010079void 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 Danisevskis18f27ad2016-06-01 13:57:40 -070084
85DECLARE_STACK_OF(ASN1_OCTET_STRING);
86
87typedef struct km_attestation_package_info {
88 ASN1_OCTET_STRING* package_name;
89 ASN1_INTEGER* version;
Janis Danisevskis18f27ad2016-06-01 13:57:40 -070090} KM_ATTESTATION_PACKAGE_INFO;
91
Eran Messeri03fc4c82018-08-16 18:53:15 +010092// 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).
95constexpr size_t AAID_PKG_INFO_OVERHEAD = 15;
Janis Danisevskis18f27ad2016-06-01 13:57:40 -070096ASN1_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 Danisevskis18f27ad2016-06-01 13:57:40 -070099} ASN1_SEQUENCE_END(KM_ATTESTATION_PACKAGE_INFO);
100IMPLEMENT_ASN1_FUNCTIONS(KM_ATTESTATION_PACKAGE_INFO);
101
102DECLARE_STACK_OF(KM_ATTESTATION_PACKAGE_INFO);
103
Eran Messeri03fc4c82018-08-16 18:53:15 +0100104// Estimated size:
105// See estimate above for the stack of package infos.
106// 34 (32 + 2) bytes for each signature digest.
107constexpr size_t AAID_SIGNATURE_SIZE = 34;
Janis Danisevskis18f27ad2016-06-01 13:57:40 -0700108typedef struct km_attestation_application_id {
109 STACK_OF(KM_ATTESTATION_PACKAGE_INFO) * package_infos;
Janis Danisevskis011675d2016-09-01 11:41:29 +0100110 STACK_OF(ASN1_OCTET_STRING) * signature_digests;
Janis Danisevskis18f27ad2016-06-01 13:57:40 -0700111} KM_ATTESTATION_APPLICATION_ID;
112
Eran Messeri03fc4c82018-08-16 18:53:15 +0100113// 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.
118constexpr size_t AAID_GENERAL_OVERHEAD = 16;
Janis Danisevskis18f27ad2016-06-01 13:57:40 -0700119ASN1_SEQUENCE(KM_ATTESTATION_APPLICATION_ID) = {
120 ASN1_SET_OF(KM_ATTESTATION_APPLICATION_ID, package_infos, KM_ATTESTATION_PACKAGE_INFO),
Janis Danisevskis011675d2016-09-01 11:41:29 +0100121 ASN1_SET_OF(KM_ATTESTATION_APPLICATION_ID, signature_digests, ASN1_OCTET_STRING),
Janis Danisevskis18f27ad2016-06-01 13:57:40 -0700122} ASN1_SEQUENCE_END(KM_ATTESTATION_APPLICATION_ID);
123IMPLEMENT_ASN1_FUNCTIONS(KM_ATTESTATION_APPLICATION_ID);
Shawn Willden9e8edcf2017-12-18 15:11:46 -0700124
125} // namespace
Janis Danisevskis18f27ad2016-06-01 13:57:40 -0700126
127} // namespace android
128
129namespace std {
130template <> 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};
135template <> struct default_delete<ASN1_OCTET_STRING> {
136 void operator()(ASN1_OCTET_STRING* p) { ASN1_OCTET_STRING_free(p); }
137};
138template <> 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
145namespace android {
146namespace security {
147namespace {
148
Rajesh Nyamagoud3f6c15c2023-03-11 01:26:48 +0000149using ::android::security::keystore::KeyAttestationApplicationId;
150using ::android::security::keystore::KeyAttestationPackageInfo;
Janis Danisevskis18f27ad2016-06-01 13:57:40 -0700151
Janis Danisevskis011675d2016-09-01 11:41:29 +0100152status_t build_attestation_package_info(const KeyAttestationPackageInfo& pinfo,
Janis Danisevskis18f27ad2016-06-01 13:57:40 -0700153 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 Nyamagoud3f6c15c2023-03-11 01:26:48 +0000161 if (!pinfo.packageName) {
Janis Danisevskis011675d2016-09-01 11:41:29 +0100162 ALOGE("Key attestation package info lacks package name");
163 return BAD_VALUE;
164 }
165
Rajesh Nyamagoud3f6c15c2023-03-11 01:26:48 +0000166 std::string pkg_name(String8(pinfo.packageName).c_str());
Janis Danisevskis18f27ad2016-06-01 13:57:40 -0700167 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 Messeriabaf4d82017-12-28 21:19:50 +0000173 BIGNUM* bn_version = BN_new();
174 if (bn_version == nullptr) {
175 return NO_MEMORY;
176 }
Rajesh Nyamagoud3f6c15c2023-03-11 01:26:48 +0000177 if (BN_set_u64(bn_version, static_cast<uint64_t>(pinfo.versionCode)) != 1) {
Eran Messeriabaf4d82017-12-28 21:19:50 +0000178 BN_free(bn_version);
Janis Danisevskis011675d2016-09-01 11:41:29 +0100179 return UNKNOWN_ERROR;
180 }
Eran Messeriabaf4d82017-12-28 21:19:50 +0000181 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 Danisevskis011675d2016-09-01 11:41:29 +0100187}
Janis Danisevskis18f27ad2016-06-01 13:57:40 -0700188
Eran Messeri03fc4c82018-08-16 18:53:15 +0100189/* The following function are not used. They are mentioned here to silence
190 * warnings about them not being used.
191 */
192void unused_functions_silencer() __attribute__((unused));
193void 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 Danisevskis011675d2016-09-01 11:41:29 +0100201StatusOr<std::vector<uint8_t>>
202build_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 Messeri03fc4c82018-08-16 18:53:15 +0100205 size_t estimated_encoded_size = AAID_GENERAL_OVERHEAD;
Janis Danisevskis18f27ad2016-06-01 13:57:40 -0700206
Janis Danisevskis011675d2016-09-01 11:41:29 +0100207 auto attestation_pinfo_stack = reinterpret_cast<_STACK*>(attestation_id->package_infos);
208
Rajesh Nyamagoud3f6c15c2023-03-11 01:26:48 +0000209 if (key_attestation_id.packageInfos.begin() == key_attestation_id.packageInfos.end())
210 return BAD_VALUE;
Janis Danisevskis011675d2016-09-01 11:41:29 +0100211
Rajesh Nyamagoud3f6c15c2023-03-11 01:26:48 +0000212 for (auto pinfo = key_attestation_id.packageInfos.begin();
213 pinfo != key_attestation_id.packageInfos.end(); ++pinfo) {
214 if (!pinfo->packageName) {
Janis Danisevskis011675d2016-09-01 11:41:29 +0100215 ALOGE("Key attestation package info lacks package name");
216 return BAD_VALUE;
217 }
Rajesh Nyamagoud3f6c15c2023-03-11 01:26:48 +0000218 std::string package_name(String8(pinfo->packageName).c_str());
Janis Danisevskis011675d2016-09-01 11:41:29 +0100219 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 Messeri03fc4c82018-08-16 18:53:15 +0100225 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 Danisevskis011675d2016-09-01 11:41:29 +0100229 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 Nyamagoud3f6c15c2023-03-11 01:26:48 +0000240 const auto& pinfo = *key_attestation_id.packageInfos.begin();
Janis Danisevskis011675d2016-09-01 11:41:29 +0100241 std::vector<std::vector<uint8_t>> signature_digests;
242
Rajesh Nyamagoud3f6c15c2023-03-11 01:26:48 +0000243 for (auto sig = pinfo.signatures.begin(); sig != pinfo.signatures.end(); ++sig) {
Janis Danisevskis011675d2016-09-01 11:41:29 +0100244 signature_digests.push_back(signature2SHA256(*sig));
245 }
246
247 auto signature_digest_stack = reinterpret_cast<_STACK*>(attestation_id->signature_digests);
Janis Danisevskis18f27ad2016-06-01 13:57:40 -0700248 for (auto si : signature_digests) {
Eran Messeri03fc4c82018-08-16 18:53:15 +0100249 estimated_encoded_size += AAID_SIGNATURE_SIZE;
250 if (estimated_encoded_size > KEY_ATTESTATION_APPLICATION_ID_MAX_SIZE) {
251 break;
252 }
Janis Danisevskis18f27ad2016-06-01 13:57:40 -0700253 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 Danisevskis18f27ad2016-06-01 13:57:40 -0700264 int len = i2d_KM_ATTESTATION_APPLICATION_ID(attestation_id.get(), nullptr);
Janis Danisevskis011675d2016-09-01 11:41:29 +0100265 if (len < 0) return UNKNOWN_ERROR;
266
267 std::vector<uint8_t> result(len);
268 uint8_t* p = result.data();
Janis Danisevskis18f27ad2016-06-01 13:57:40 -0700269 len = i2d_KM_ATTESTATION_APPLICATION_ID(attestation_id.get(), &p);
Janis Danisevskis011675d2016-09-01 11:41:29 +0100270 if (len < 0) return UNKNOWN_ERROR;
Janis Danisevskis18f27ad2016-06-01 13:57:40 -0700271
272 return result;
273}
274
Janis Danisevskis011675d2016-09-01 11:41:29 +0100275StatusOr<std::vector<uint8_t>> gather_attestation_application_id(uid_t uid) {
Shawn Willden9e8edcf2017-12-18 15:11:46 -0700276 KeyAttestationApplicationId key_attestation_id;
Janis Danisevskis18f27ad2016-06-01 13:57:40 -0700277
Rajesh Nyamagoud41e98ee2024-06-26 01:11:49 +0000278 if (uid == AID_SYSTEM || uid == AID_ROOT) {
Shawn Willden9e8edcf2017-12-18 15:11:46 -0700279 /* Use a fixed ID for system callers */
Rajesh Nyamagoud3f6c15c2023-03-11 01:26:48 +0000280 auto pinfo = KeyAttestationPackageInfo();
281 pinfo.packageName = String16(kAttestationSystemPackageName);
282 pinfo.versionCode = 1;
283 key_attestation_id.packageInfos.push_back(std::move(pinfo));
Eran Messeriea47d3f2017-12-06 12:01:42 +0000284 } else {
Shawn Willden9e8edcf2017-12-18 15:11:46 -0700285 /* Get the attestation application ID from package manager */
Shaquille Johnsonc67296e2024-01-22 19:13:01 +0000286 ::android::binder::Status status;
287
David Drysdaleb0be7ed2024-05-28 10:19:03 +0100288 // Retry on failure.
Shaquille Johnsonc67296e2024-01-22 19:13:01 +0000289 for (size_t attempt{0}; attempt < kMaxAttempts; ++attempt) {
David Drysdaleb0be7ed2024-05-28 10:19:03 +0100290 auto pm = get_service();
291 status = pm->getKeyAttestationApplicationId(uid, &key_attestation_id);
Shaquille Johnsonc67296e2024-01-22 19:13:01 +0000292 if (status.isOk()) {
293 break;
Shaquille Johnsonc67296e2024-01-22 19:13:01 +0000294 }
David Drysdaleb0be7ed2024-05-28 10:19:03 +0100295
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 Johnsonc67296e2024-01-22 19:13:01 +0000311 }
312
Eran Messeriea47d3f2017-12-06 12:01:42 +0000313 if (!status.isOk()) {
Eran Messeri03fc4c82018-08-16 18:53:15 +0100314 ALOGW("package manager request for key attestation ID failed with: %s %d",
Tomasz Wasilczyk102d33a2023-08-11 16:10:37 +0000315 status.exceptionMessage().c_str(), status.exceptionCode());
Rajesh Nyamagoud3f6c15c2023-03-11 01:26:48 +0000316
Shaquille Johnsonc67296e2024-01-22 19:13:01 +0000317 return int32_t(ResponseCode::GET_ATTESTATION_APPLICATION_ID_FAILED);
Eran Messeriea47d3f2017-12-06 12:01:42 +0000318 }
Janis Danisevskis18f27ad2016-06-01 13:57:40 -0700319 }
320
321 /* DER encode the attestation application ID */
Shawn Willden9e8edcf2017-12-18 15:11:46 -0700322 return build_attestation_application_id(key_attestation_id);
Janis Danisevskis18f27ad2016-06-01 13:57:40 -0700323}
324
325} // namespace security
326} // namespace android