blob: bcd331846ef9c60e4506007f107ce4a067def522 [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>
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 Johnsonc67296e2024-01-22 19:13:01 +000032#include <aidl/android/system/keystore2/ResponseCode.h>
Rajesh Nyamagoud3f6c15c2023-03-11 01:26:48 +000033#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 Danisevskis18f27ad2016-06-01 13:57:40 -070038
Eran Messeriea47d3f2017-12-06 12:01:42 +000039#include <private/android_filesystem_config.h> /* for AID_SYSTEM */
40
Janis Danisevskis18f27ad2016-06-01 13:57:40 -070041#include <openssl/asn1t.h>
Eran Messeriabaf4d82017-12-28 21:19:50 +000042#include <openssl/bn.h>
Janis Danisevskis18f27ad2016-06-01 13:57:40 -070043#include <openssl/sha.h>
44
45#include <utils/String8.h>
46
47namespace android {
48
49namespace {
50
Shawn Willden9e8edcf2017-12-18 15:11:46 -070051constexpr const char* kAttestationSystemPackageName = "AndroidSystem";
Shaquille Johnsonc67296e2024-01-22 19:13:01 +000052constexpr const size_t kMaxAttempts = 3;
53constexpr const unsigned long kRetryIntervalUsecs = 500000; // sleep for 500 ms
Shawn Willden9e8edcf2017-12-18 15:11:46 -070054
Rajesh Nyamagoud3f6c15c2023-03-11 01:26:48 +000055std::vector<uint8_t> signature2SHA256(const security::keystore::Signature& sig) {
Janis Danisevskis18f27ad2016-06-01 13:57:40 -070056 std::vector<uint8_t> digest_buffer(SHA256_DIGEST_LENGTH);
Rajesh Nyamagoud3f6c15c2023-03-11 01:26:48 +000057 SHA256(sig.data.data(), sig.data.size(), digest_buffer.data());
Janis Danisevskis18f27ad2016-06-01 13:57:40 -070058 return digest_buffer;
59}
60
Shaquille Johnsonc67296e2024-01-22 19:13:01 +000061using ::aidl::android::system::keystore2::ResponseCode;
Rajesh Nyamagoud3f6c15c2023-03-11 01:26:48 +000062using ::android::security::keystore::BpKeyAttestationApplicationIdProvider;
Janis Danisevskis18f27ad2016-06-01 13:57:40 -070063
64class KeyAttestationApplicationIdProvider : public BpKeyAttestationApplicationIdProvider {
65 public:
66 KeyAttestationApplicationIdProvider();
67
68 static KeyAttestationApplicationIdProvider& get();
69
70 private:
71 android::sp<android::IServiceManager> service_manager_;
72};
73
74KeyAttestationApplicationIdProvider& KeyAttestationApplicationIdProvider::get() {
75 static KeyAttestationApplicationIdProvider mpm;
76 return mpm;
77}
78
79KeyAttestationApplicationIdProvider::KeyAttestationApplicationIdProvider()
Tri Vo8d08cd92023-07-18 14:53:39 -040080 : BpKeyAttestationApplicationIdProvider(android::defaultServiceManager()->waitForService(
81 String16("sec_key_att_app_id_provider"))) {}
Janis Danisevskis18f27ad2016-06-01 13:57:40 -070082
83DECLARE_STACK_OF(ASN1_OCTET_STRING);
84
85typedef struct km_attestation_package_info {
86 ASN1_OCTET_STRING* package_name;
87 ASN1_INTEGER* version;
Janis Danisevskis18f27ad2016-06-01 13:57:40 -070088} KM_ATTESTATION_PACKAGE_INFO;
89
Eran Messeri03fc4c82018-08-16 18:53:15 +010090// 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).
93constexpr size_t AAID_PKG_INFO_OVERHEAD = 15;
Janis Danisevskis18f27ad2016-06-01 13:57:40 -070094ASN1_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 Danisevskis18f27ad2016-06-01 13:57:40 -070097} ASN1_SEQUENCE_END(KM_ATTESTATION_PACKAGE_INFO);
98IMPLEMENT_ASN1_FUNCTIONS(KM_ATTESTATION_PACKAGE_INFO);
99
100DECLARE_STACK_OF(KM_ATTESTATION_PACKAGE_INFO);
101
Eran Messeri03fc4c82018-08-16 18:53:15 +0100102// Estimated size:
103// See estimate above for the stack of package infos.
104// 34 (32 + 2) bytes for each signature digest.
105constexpr size_t AAID_SIGNATURE_SIZE = 34;
Janis Danisevskis18f27ad2016-06-01 13:57:40 -0700106typedef struct km_attestation_application_id {
107 STACK_OF(KM_ATTESTATION_PACKAGE_INFO) * package_infos;
Janis Danisevskis011675d2016-09-01 11:41:29 +0100108 STACK_OF(ASN1_OCTET_STRING) * signature_digests;
Janis Danisevskis18f27ad2016-06-01 13:57:40 -0700109} KM_ATTESTATION_APPLICATION_ID;
110
Eran Messeri03fc4c82018-08-16 18:53:15 +0100111// 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.
116constexpr size_t AAID_GENERAL_OVERHEAD = 16;
Janis Danisevskis18f27ad2016-06-01 13:57:40 -0700117ASN1_SEQUENCE(KM_ATTESTATION_APPLICATION_ID) = {
118 ASN1_SET_OF(KM_ATTESTATION_APPLICATION_ID, package_infos, KM_ATTESTATION_PACKAGE_INFO),
Janis Danisevskis011675d2016-09-01 11:41:29 +0100119 ASN1_SET_OF(KM_ATTESTATION_APPLICATION_ID, signature_digests, ASN1_OCTET_STRING),
Janis Danisevskis18f27ad2016-06-01 13:57:40 -0700120} ASN1_SEQUENCE_END(KM_ATTESTATION_APPLICATION_ID);
121IMPLEMENT_ASN1_FUNCTIONS(KM_ATTESTATION_APPLICATION_ID);
Shawn Willden9e8edcf2017-12-18 15:11:46 -0700122
123} // namespace
Janis Danisevskis18f27ad2016-06-01 13:57:40 -0700124
125} // namespace android
126
127namespace std {
128template <> 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};
133template <> struct default_delete<ASN1_OCTET_STRING> {
134 void operator()(ASN1_OCTET_STRING* p) { ASN1_OCTET_STRING_free(p); }
135};
136template <> 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
143namespace android {
144namespace security {
145namespace {
146
Rajesh Nyamagoud3f6c15c2023-03-11 01:26:48 +0000147using ::android::security::keystore::KeyAttestationApplicationId;
148using ::android::security::keystore::KeyAttestationPackageInfo;
Janis Danisevskis18f27ad2016-06-01 13:57:40 -0700149
Janis Danisevskis011675d2016-09-01 11:41:29 +0100150status_t build_attestation_package_info(const KeyAttestationPackageInfo& pinfo,
Janis Danisevskis18f27ad2016-06-01 13:57:40 -0700151 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 Nyamagoud3f6c15c2023-03-11 01:26:48 +0000159 if (!pinfo.packageName) {
Janis Danisevskis011675d2016-09-01 11:41:29 +0100160 ALOGE("Key attestation package info lacks package name");
161 return BAD_VALUE;
162 }
163
Rajesh Nyamagoud3f6c15c2023-03-11 01:26:48 +0000164 std::string pkg_name(String8(pinfo.packageName).c_str());
Janis Danisevskis18f27ad2016-06-01 13:57:40 -0700165 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 Messeriabaf4d82017-12-28 21:19:50 +0000171 BIGNUM* bn_version = BN_new();
172 if (bn_version == nullptr) {
173 return NO_MEMORY;
174 }
Rajesh Nyamagoud3f6c15c2023-03-11 01:26:48 +0000175 if (BN_set_u64(bn_version, static_cast<uint64_t>(pinfo.versionCode)) != 1) {
Eran Messeriabaf4d82017-12-28 21:19:50 +0000176 BN_free(bn_version);
Janis Danisevskis011675d2016-09-01 11:41:29 +0100177 return UNKNOWN_ERROR;
178 }
Eran Messeriabaf4d82017-12-28 21:19:50 +0000179 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 Danisevskis011675d2016-09-01 11:41:29 +0100185}
Janis Danisevskis18f27ad2016-06-01 13:57:40 -0700186
Eran Messeri03fc4c82018-08-16 18:53:15 +0100187/* The following function are not used. They are mentioned here to silence
188 * warnings about them not being used.
189 */
190void unused_functions_silencer() __attribute__((unused));
191void 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 Danisevskis011675d2016-09-01 11:41:29 +0100199StatusOr<std::vector<uint8_t>>
200build_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 Messeri03fc4c82018-08-16 18:53:15 +0100203 size_t estimated_encoded_size = AAID_GENERAL_OVERHEAD;
Janis Danisevskis18f27ad2016-06-01 13:57:40 -0700204
Janis Danisevskis011675d2016-09-01 11:41:29 +0100205 auto attestation_pinfo_stack = reinterpret_cast<_STACK*>(attestation_id->package_infos);
206
Rajesh Nyamagoud3f6c15c2023-03-11 01:26:48 +0000207 if (key_attestation_id.packageInfos.begin() == key_attestation_id.packageInfos.end())
208 return BAD_VALUE;
Janis Danisevskis011675d2016-09-01 11:41:29 +0100209
Rajesh Nyamagoud3f6c15c2023-03-11 01:26:48 +0000210 for (auto pinfo = key_attestation_id.packageInfos.begin();
211 pinfo != key_attestation_id.packageInfos.end(); ++pinfo) {
212 if (!pinfo->packageName) {
Janis Danisevskis011675d2016-09-01 11:41:29 +0100213 ALOGE("Key attestation package info lacks package name");
214 return BAD_VALUE;
215 }
Rajesh Nyamagoud3f6c15c2023-03-11 01:26:48 +0000216 std::string package_name(String8(pinfo->packageName).c_str());
Janis Danisevskis011675d2016-09-01 11:41:29 +0100217 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 Messeri03fc4c82018-08-16 18:53:15 +0100223 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 Danisevskis011675d2016-09-01 11:41:29 +0100227 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 Nyamagoud3f6c15c2023-03-11 01:26:48 +0000238 const auto& pinfo = *key_attestation_id.packageInfos.begin();
Janis Danisevskis011675d2016-09-01 11:41:29 +0100239 std::vector<std::vector<uint8_t>> signature_digests;
240
Rajesh Nyamagoud3f6c15c2023-03-11 01:26:48 +0000241 for (auto sig = pinfo.signatures.begin(); sig != pinfo.signatures.end(); ++sig) {
Janis Danisevskis011675d2016-09-01 11:41:29 +0100242 signature_digests.push_back(signature2SHA256(*sig));
243 }
244
245 auto signature_digest_stack = reinterpret_cast<_STACK*>(attestation_id->signature_digests);
Janis Danisevskis18f27ad2016-06-01 13:57:40 -0700246 for (auto si : signature_digests) {
Eran Messeri03fc4c82018-08-16 18:53:15 +0100247 estimated_encoded_size += AAID_SIGNATURE_SIZE;
248 if (estimated_encoded_size > KEY_ATTESTATION_APPLICATION_ID_MAX_SIZE) {
249 break;
250 }
Janis Danisevskis18f27ad2016-06-01 13:57:40 -0700251 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 Danisevskis18f27ad2016-06-01 13:57:40 -0700262 int len = i2d_KM_ATTESTATION_APPLICATION_ID(attestation_id.get(), nullptr);
Janis Danisevskis011675d2016-09-01 11:41:29 +0100263 if (len < 0) return UNKNOWN_ERROR;
264
265 std::vector<uint8_t> result(len);
266 uint8_t* p = result.data();
Janis Danisevskis18f27ad2016-06-01 13:57:40 -0700267 len = i2d_KM_ATTESTATION_APPLICATION_ID(attestation_id.get(), &p);
Janis Danisevskis011675d2016-09-01 11:41:29 +0100268 if (len < 0) return UNKNOWN_ERROR;
Janis Danisevskis18f27ad2016-06-01 13:57:40 -0700269
270 return result;
271}
272
Janis Danisevskis011675d2016-09-01 11:41:29 +0100273StatusOr<std::vector<uint8_t>> gather_attestation_application_id(uid_t uid) {
Shawn Willden9e8edcf2017-12-18 15:11:46 -0700274 KeyAttestationApplicationId key_attestation_id;
Janis Danisevskis18f27ad2016-06-01 13:57:40 -0700275
Rajesh Nyamagoud41e98ee2024-06-26 01:11:49 +0000276 if (uid == AID_SYSTEM || uid == AID_ROOT) {
Shawn Willden9e8edcf2017-12-18 15:11:46 -0700277 /* Use a fixed ID for system callers */
Rajesh Nyamagoud3f6c15c2023-03-11 01:26:48 +0000278 auto pinfo = KeyAttestationPackageInfo();
279 pinfo.packageName = String16(kAttestationSystemPackageName);
280 pinfo.versionCode = 1;
281 key_attestation_id.packageInfos.push_back(std::move(pinfo));
Eran Messeriea47d3f2017-12-06 12:01:42 +0000282 } else {
Shawn Willden9e8edcf2017-12-18 15:11:46 -0700283 /* Get the attestation application ID from package manager */
284 auto& pm = KeyAttestationApplicationIdProvider::get();
Shaquille Johnsonc67296e2024-01-22 19:13:01 +0000285 ::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 Messeriea47d3f2017-12-06 12:01:42 +0000303 if (!status.isOk()) {
Eran Messeri03fc4c82018-08-16 18:53:15 +0100304 ALOGW("package manager request for key attestation ID failed with: %s %d",
Tomasz Wasilczyk102d33a2023-08-11 16:10:37 +0000305 status.exceptionMessage().c_str(), status.exceptionCode());
Rajesh Nyamagoud3f6c15c2023-03-11 01:26:48 +0000306
Shaquille Johnsonc67296e2024-01-22 19:13:01 +0000307 return int32_t(ResponseCode::GET_ATTESTATION_APPLICATION_ID_FAILED);
Eran Messeriea47d3f2017-12-06 12:01:42 +0000308 }
Janis Danisevskis18f27ad2016-06-01 13:57:40 -0700309 }
310
311 /* DER encode the attestation application ID */
Shawn Willden9e8edcf2017-12-18 15:11:46 -0700312 return build_attestation_application_id(key_attestation_id);
Janis Danisevskis18f27ad2016-06-01 13:57:40 -0700313}
314
315} // namespace security
316} // namespace android