blob: d607fbfaac25e6c3638177d4c92c8866b172b97b [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";
Eran Messeri03fc4c82018-08-16 18:53:15 +010052constexpr const char* kUnknownPackageName = "UnknownPackage";
Shaquille Johnsonc67296e2024-01-22 19:13:01 +000053constexpr const size_t kMaxAttempts = 3;
54constexpr const unsigned long kRetryIntervalUsecs = 500000; // sleep for 500 ms
Shawn Willden9e8edcf2017-12-18 15:11:46 -070055
Rajesh Nyamagoud3f6c15c2023-03-11 01:26:48 +000056std::vector<uint8_t> signature2SHA256(const security::keystore::Signature& sig) {
Janis Danisevskis18f27ad2016-06-01 13:57:40 -070057 std::vector<uint8_t> digest_buffer(SHA256_DIGEST_LENGTH);
Rajesh Nyamagoud3f6c15c2023-03-11 01:26:48 +000058 SHA256(sig.data.data(), sig.data.size(), digest_buffer.data());
Janis Danisevskis18f27ad2016-06-01 13:57:40 -070059 return digest_buffer;
60}
61
Shaquille Johnsonc67296e2024-01-22 19:13:01 +000062using ::aidl::android::system::keystore2::ResponseCode;
Rajesh Nyamagoud3f6c15c2023-03-11 01:26:48 +000063using ::android::security::keystore::BpKeyAttestationApplicationIdProvider;
Janis Danisevskis18f27ad2016-06-01 13:57:40 -070064
65class KeyAttestationApplicationIdProvider : public BpKeyAttestationApplicationIdProvider {
66 public:
67 KeyAttestationApplicationIdProvider();
68
69 static KeyAttestationApplicationIdProvider& get();
70
71 private:
72 android::sp<android::IServiceManager> service_manager_;
73};
74
75KeyAttestationApplicationIdProvider& KeyAttestationApplicationIdProvider::get() {
76 static KeyAttestationApplicationIdProvider mpm;
77 return mpm;
78}
79
80KeyAttestationApplicationIdProvider::KeyAttestationApplicationIdProvider()
Tri Vo8d08cd92023-07-18 14:53:39 -040081 : BpKeyAttestationApplicationIdProvider(android::defaultServiceManager()->waitForService(
82 String16("sec_key_att_app_id_provider"))) {}
Janis Danisevskis18f27ad2016-06-01 13:57:40 -070083
84DECLARE_STACK_OF(ASN1_OCTET_STRING);
85
86typedef struct km_attestation_package_info {
87 ASN1_OCTET_STRING* package_name;
88 ASN1_INTEGER* version;
Janis Danisevskis18f27ad2016-06-01 13:57:40 -070089} KM_ATTESTATION_PACKAGE_INFO;
90
Eran Messeri03fc4c82018-08-16 18:53:15 +010091// Estimated size:
92// 4 bytes for the package name + package_name length
93// 11 bytes for the version (2 bytes header and up to 9 bytes of data).
94constexpr size_t AAID_PKG_INFO_OVERHEAD = 15;
Janis Danisevskis18f27ad2016-06-01 13:57:40 -070095ASN1_SEQUENCE(KM_ATTESTATION_PACKAGE_INFO) = {
96 ASN1_SIMPLE(KM_ATTESTATION_PACKAGE_INFO, package_name, ASN1_OCTET_STRING),
97 ASN1_SIMPLE(KM_ATTESTATION_PACKAGE_INFO, version, ASN1_INTEGER),
Janis Danisevskis18f27ad2016-06-01 13:57:40 -070098} ASN1_SEQUENCE_END(KM_ATTESTATION_PACKAGE_INFO);
99IMPLEMENT_ASN1_FUNCTIONS(KM_ATTESTATION_PACKAGE_INFO);
100
101DECLARE_STACK_OF(KM_ATTESTATION_PACKAGE_INFO);
102
Eran Messeri03fc4c82018-08-16 18:53:15 +0100103// Estimated size:
104// See estimate above for the stack of package infos.
105// 34 (32 + 2) bytes for each signature digest.
106constexpr size_t AAID_SIGNATURE_SIZE = 34;
Janis Danisevskis18f27ad2016-06-01 13:57:40 -0700107typedef struct km_attestation_application_id {
108 STACK_OF(KM_ATTESTATION_PACKAGE_INFO) * package_infos;
Janis Danisevskis011675d2016-09-01 11:41:29 +0100109 STACK_OF(ASN1_OCTET_STRING) * signature_digests;
Janis Danisevskis18f27ad2016-06-01 13:57:40 -0700110} KM_ATTESTATION_APPLICATION_ID;
111
Eran Messeri03fc4c82018-08-16 18:53:15 +0100112// Estimated overhead:
113// 4 for the header of the octet string containing the fully-encoded data.
114// 4 for the sequence header.
115// 4 for the header of the package info set.
116// 4 for the header of the signature set.
117constexpr size_t AAID_GENERAL_OVERHEAD = 16;
Janis Danisevskis18f27ad2016-06-01 13:57:40 -0700118ASN1_SEQUENCE(KM_ATTESTATION_APPLICATION_ID) = {
119 ASN1_SET_OF(KM_ATTESTATION_APPLICATION_ID, package_infos, KM_ATTESTATION_PACKAGE_INFO),
Janis Danisevskis011675d2016-09-01 11:41:29 +0100120 ASN1_SET_OF(KM_ATTESTATION_APPLICATION_ID, signature_digests, ASN1_OCTET_STRING),
Janis Danisevskis18f27ad2016-06-01 13:57:40 -0700121} ASN1_SEQUENCE_END(KM_ATTESTATION_APPLICATION_ID);
122IMPLEMENT_ASN1_FUNCTIONS(KM_ATTESTATION_APPLICATION_ID);
Shawn Willden9e8edcf2017-12-18 15:11:46 -0700123
124} // namespace
Janis Danisevskis18f27ad2016-06-01 13:57:40 -0700125
126} // namespace android
127
128namespace std {
129template <> struct default_delete<android::KM_ATTESTATION_PACKAGE_INFO> {
130 void operator()(android::KM_ATTESTATION_PACKAGE_INFO* p) {
131 android::KM_ATTESTATION_PACKAGE_INFO_free(p);
132 }
133};
134template <> struct default_delete<ASN1_OCTET_STRING> {
135 void operator()(ASN1_OCTET_STRING* p) { ASN1_OCTET_STRING_free(p); }
136};
137template <> struct default_delete<android::KM_ATTESTATION_APPLICATION_ID> {
138 void operator()(android::KM_ATTESTATION_APPLICATION_ID* p) {
139 android::KM_ATTESTATION_APPLICATION_ID_free(p);
140 }
141};
142} // namespace std
143
144namespace android {
145namespace security {
146namespace {
147
Rajesh Nyamagoud3f6c15c2023-03-11 01:26:48 +0000148using ::android::security::keystore::KeyAttestationApplicationId;
149using ::android::security::keystore::KeyAttestationPackageInfo;
Janis Danisevskis18f27ad2016-06-01 13:57:40 -0700150
Janis Danisevskis011675d2016-09-01 11:41:29 +0100151status_t build_attestation_package_info(const KeyAttestationPackageInfo& pinfo,
Janis Danisevskis18f27ad2016-06-01 13:57:40 -0700152 std::unique_ptr<KM_ATTESTATION_PACKAGE_INFO>* attestation_package_info_ptr) {
153
154 if (!attestation_package_info_ptr) return BAD_VALUE;
155 auto& attestation_package_info = *attestation_package_info_ptr;
156
157 attestation_package_info.reset(KM_ATTESTATION_PACKAGE_INFO_new());
158 if (!attestation_package_info.get()) return NO_MEMORY;
159
Rajesh Nyamagoud3f6c15c2023-03-11 01:26:48 +0000160 if (!pinfo.packageName) {
Janis Danisevskis011675d2016-09-01 11:41:29 +0100161 ALOGE("Key attestation package info lacks package name");
162 return BAD_VALUE;
163 }
164
Rajesh Nyamagoud3f6c15c2023-03-11 01:26:48 +0000165 std::string pkg_name(String8(pinfo.packageName).c_str());
Janis Danisevskis18f27ad2016-06-01 13:57:40 -0700166 if (!ASN1_OCTET_STRING_set(attestation_package_info->package_name,
167 reinterpret_cast<const unsigned char*>(pkg_name.data()),
168 pkg_name.size())) {
169 return UNKNOWN_ERROR;
170 }
171
Eran Messeriabaf4d82017-12-28 21:19:50 +0000172 BIGNUM* bn_version = BN_new();
173 if (bn_version == nullptr) {
174 return NO_MEMORY;
175 }
Rajesh Nyamagoud3f6c15c2023-03-11 01:26:48 +0000176 if (BN_set_u64(bn_version, static_cast<uint64_t>(pinfo.versionCode)) != 1) {
Eran Messeriabaf4d82017-12-28 21:19:50 +0000177 BN_free(bn_version);
Janis Danisevskis011675d2016-09-01 11:41:29 +0100178 return UNKNOWN_ERROR;
179 }
Eran Messeriabaf4d82017-12-28 21:19:50 +0000180 status_t retval = NO_ERROR;
181 if (BN_to_ASN1_INTEGER(bn_version, attestation_package_info->version) == nullptr) {
182 retval = UNKNOWN_ERROR;
183 }
184 BN_free(bn_version);
185 return retval;
Janis Danisevskis011675d2016-09-01 11:41:29 +0100186}
Janis Danisevskis18f27ad2016-06-01 13:57:40 -0700187
Eran Messeri03fc4c82018-08-16 18:53:15 +0100188/* The following function are not used. They are mentioned here to silence
189 * warnings about them not being used.
190 */
191void unused_functions_silencer() __attribute__((unused));
192void unused_functions_silencer() {
193 i2d_KM_ATTESTATION_PACKAGE_INFO(nullptr, nullptr);
194 d2i_KM_ATTESTATION_APPLICATION_ID(nullptr, nullptr, 0);
195 d2i_KM_ATTESTATION_PACKAGE_INFO(nullptr, nullptr, 0);
196}
197
198} // namespace
199
Janis Danisevskis011675d2016-09-01 11:41:29 +0100200StatusOr<std::vector<uint8_t>>
201build_attestation_application_id(const KeyAttestationApplicationId& key_attestation_id) {
202 auto attestation_id =
203 std::unique_ptr<KM_ATTESTATION_APPLICATION_ID>(KM_ATTESTATION_APPLICATION_ID_new());
Eran Messeri03fc4c82018-08-16 18:53:15 +0100204 size_t estimated_encoded_size = AAID_GENERAL_OVERHEAD;
Janis Danisevskis18f27ad2016-06-01 13:57:40 -0700205
Janis Danisevskis011675d2016-09-01 11:41:29 +0100206 auto attestation_pinfo_stack = reinterpret_cast<_STACK*>(attestation_id->package_infos);
207
Rajesh Nyamagoud3f6c15c2023-03-11 01:26:48 +0000208 if (key_attestation_id.packageInfos.begin() == key_attestation_id.packageInfos.end())
209 return BAD_VALUE;
Janis Danisevskis011675d2016-09-01 11:41:29 +0100210
Rajesh Nyamagoud3f6c15c2023-03-11 01:26:48 +0000211 for (auto pinfo = key_attestation_id.packageInfos.begin();
212 pinfo != key_attestation_id.packageInfos.end(); ++pinfo) {
213 if (!pinfo->packageName) {
Janis Danisevskis011675d2016-09-01 11:41:29 +0100214 ALOGE("Key attestation package info lacks package name");
215 return BAD_VALUE;
216 }
Rajesh Nyamagoud3f6c15c2023-03-11 01:26:48 +0000217 std::string package_name(String8(pinfo->packageName).c_str());
Janis Danisevskis011675d2016-09-01 11:41:29 +0100218 std::unique_ptr<KM_ATTESTATION_PACKAGE_INFO> attestation_package_info;
219 auto rc = build_attestation_package_info(*pinfo, &attestation_package_info);
220 if (rc != NO_ERROR) {
221 ALOGE("Building DER attestation package info failed %d", rc);
222 return rc;
223 }
Eran Messeri03fc4c82018-08-16 18:53:15 +0100224 estimated_encoded_size += AAID_PKG_INFO_OVERHEAD + package_name.size();
225 if (estimated_encoded_size > KEY_ATTESTATION_APPLICATION_ID_MAX_SIZE) {
226 break;
227 }
Janis Danisevskis011675d2016-09-01 11:41:29 +0100228 if (!sk_push(attestation_pinfo_stack, attestation_package_info.get())) {
229 return NO_MEMORY;
230 }
231 // if push succeeded, the stack takes ownership
232 attestation_package_info.release();
233 }
234
235 /** Apps can only share a uid iff they were signed with the same certificate(s). Because the
236 * signature field actually holds the signing certificate, rather than a signature, we can
237 * simply use the set of signature digests of the first package info.
238 */
Rajesh Nyamagoud3f6c15c2023-03-11 01:26:48 +0000239 const auto& pinfo = *key_attestation_id.packageInfos.begin();
Janis Danisevskis011675d2016-09-01 11:41:29 +0100240 std::vector<std::vector<uint8_t>> signature_digests;
241
Rajesh Nyamagoud3f6c15c2023-03-11 01:26:48 +0000242 for (auto sig = pinfo.signatures.begin(); sig != pinfo.signatures.end(); ++sig) {
Janis Danisevskis011675d2016-09-01 11:41:29 +0100243 signature_digests.push_back(signature2SHA256(*sig));
244 }
245
246 auto signature_digest_stack = reinterpret_cast<_STACK*>(attestation_id->signature_digests);
Janis Danisevskis18f27ad2016-06-01 13:57:40 -0700247 for (auto si : signature_digests) {
Eran Messeri03fc4c82018-08-16 18:53:15 +0100248 estimated_encoded_size += AAID_SIGNATURE_SIZE;
249 if (estimated_encoded_size > KEY_ATTESTATION_APPLICATION_ID_MAX_SIZE) {
250 break;
251 }
Janis Danisevskis18f27ad2016-06-01 13:57:40 -0700252 auto asn1_item = std::unique_ptr<ASN1_OCTET_STRING>(ASN1_OCTET_STRING_new());
253 if (!asn1_item) return NO_MEMORY;
254 if (!ASN1_OCTET_STRING_set(asn1_item.get(), si.data(), si.size())) {
255 return UNKNOWN_ERROR;
256 }
257 if (!sk_push(signature_digest_stack, asn1_item.get())) {
258 return NO_MEMORY;
259 }
260 asn1_item.release(); // if push succeeded, the stack takes ownership
261 }
262
Janis Danisevskis18f27ad2016-06-01 13:57:40 -0700263 int len = i2d_KM_ATTESTATION_APPLICATION_ID(attestation_id.get(), nullptr);
Janis Danisevskis011675d2016-09-01 11:41:29 +0100264 if (len < 0) return UNKNOWN_ERROR;
265
266 std::vector<uint8_t> result(len);
267 uint8_t* p = result.data();
Janis Danisevskis18f27ad2016-06-01 13:57:40 -0700268 len = i2d_KM_ATTESTATION_APPLICATION_ID(attestation_id.get(), &p);
Janis Danisevskis011675d2016-09-01 11:41:29 +0100269 if (len < 0) return UNKNOWN_ERROR;
Janis Danisevskis18f27ad2016-06-01 13:57:40 -0700270
271 return result;
272}
273
Janis Danisevskis011675d2016-09-01 11:41:29 +0100274StatusOr<std::vector<uint8_t>> gather_attestation_application_id(uid_t uid) {
Shawn Willden9e8edcf2017-12-18 15:11:46 -0700275 KeyAttestationApplicationId key_attestation_id;
Janis Danisevskis18f27ad2016-06-01 13:57:40 -0700276
Eran Messeriea47d3f2017-12-06 12:01:42 +0000277 if (uid == AID_SYSTEM) {
Shawn Willden9e8edcf2017-12-18 15:11:46 -0700278 /* Use a fixed ID for system callers */
Rajesh Nyamagoud3f6c15c2023-03-11 01:26:48 +0000279 auto pinfo = KeyAttestationPackageInfo();
280 pinfo.packageName = String16(kAttestationSystemPackageName);
281 pinfo.versionCode = 1;
282 key_attestation_id.packageInfos.push_back(std::move(pinfo));
Eran Messeriea47d3f2017-12-06 12:01:42 +0000283 } else {
Shawn Willden9e8edcf2017-12-18 15:11:46 -0700284 /* Get the attestation application ID from package manager */
285 auto& pm = KeyAttestationApplicationIdProvider::get();
Shaquille Johnsonc67296e2024-01-22 19:13:01 +0000286 ::android::binder::Status status;
287
288 // Retry on failure if a service specific error code.
289 for (size_t attempt{0}; attempt < kMaxAttempts; ++attempt) {
290 status = pm.getKeyAttestationApplicationId(uid, &key_attestation_id);
291 if (status.isOk()) {
292 break;
293 } else if (status.exceptionCode() != binder::Status::EX_SERVICE_SPECIFIC) {
294 ALOGW("Retry: key attestation ID failed with service specific error: %s %d",
295 status.exceptionMessage().c_str(), status.serviceSpecificErrorCode());
296 usleep(kRetryIntervalUsecs);
297 } else {
298 ALOGW("Retry: key attestation ID failed with error: %s %d",
299 status.exceptionMessage().c_str(), status.exceptionCode());
300 usleep(kRetryIntervalUsecs);
301 }
302 }
303
Eran Messeri03fc4c82018-08-16 18:53:15 +0100304 // Package Manager call has failed, perform attestation but indicate that the
305 // caller is unknown.
Eran Messeriea47d3f2017-12-06 12:01:42 +0000306 if (!status.isOk()) {
Eran Messeri03fc4c82018-08-16 18:53:15 +0100307 ALOGW("package manager request for key attestation ID failed with: %s %d",
Tomasz Wasilczyk102d33a2023-08-11 16:10:37 +0000308 status.exceptionMessage().c_str(), status.exceptionCode());
Rajesh Nyamagoud3f6c15c2023-03-11 01:26:48 +0000309
Shaquille Johnsonc67296e2024-01-22 19:13:01 +0000310 return int32_t(ResponseCode::GET_ATTESTATION_APPLICATION_ID_FAILED);
Eran Messeriea47d3f2017-12-06 12:01:42 +0000311 }
Janis Danisevskis18f27ad2016-06-01 13:57:40 -0700312 }
313
314 /* DER encode the attestation application ID */
Shawn Willden9e8edcf2017-12-18 15:11:46 -0700315 return build_attestation_application_id(key_attestation_id);
Janis Danisevskis18f27ad2016-06-01 13:57:40 -0700316}
317
318} // namespace security
319} // namespace android