1 /*
2 * Copyright 2020 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 */
16
17 #include <keymasterV4_1/attestation_record.h>
18
19 #include <android-base/logging.h>
20 #include <assert.h>
21
22 #include <openssl/asn1t.h>
23 #include <openssl/bn.h>
24 #include <openssl/evp.h>
25 #include <openssl/x509.h>
26
27 #include <keymasterV4_0/authorization_set.h>
28 #include <keymasterV4_0/openssl_utils.h>
29
30 #define AT __FILE__ ":" << __LINE__
31
32 /*
33 * NOTE: The contents of this file are *extremely* similar to the contents of the V4_0 copy of the
34 * same support file. Unfortunately, small changes in the scheme mean that the schema types have to
35 * be distinct, which drives almost everything else to be different as well. In the next version we
36 * plan to abandon not just this openssl mechanism for parsing ASN.1, but ASN.1 entirely, so
37 * eventually all of this duplication can be removed.
38 */
39
40 namespace android {
41 namespace hardware {
42 namespace keymaster {
43 namespace V4_1 {
44
45 struct stack_st_ASN1_TYPE_Delete {
operator ()android::hardware::keymaster::V4_1::stack_st_ASN1_TYPE_Delete46 void operator()(stack_st_ASN1_TYPE* p) { sk_ASN1_TYPE_free(p); }
47 };
48
49 struct ASN1_STRING_Delete {
operator ()android::hardware::keymaster::V4_1::ASN1_STRING_Delete50 void operator()(ASN1_STRING* p) { ASN1_STRING_free(p); }
51 };
52
53 struct ASN1_TYPE_Delete {
operator ()android::hardware::keymaster::V4_1::ASN1_TYPE_Delete54 void operator()(ASN1_TYPE* p) { ASN1_TYPE_free(p); }
55 };
56
57 #define ASN1_INTEGER_SET STACK_OF(ASN1_INTEGER)
58
59 typedef struct km_root_of_trust {
60 ASN1_OCTET_STRING* verified_boot_key;
61 ASN1_BOOLEAN device_locked;
62 ASN1_ENUMERATED* verified_boot_state;
63 ASN1_OCTET_STRING* verified_boot_hash;
64 } KM_ROOT_OF_TRUST;
65
66 ASN1_SEQUENCE(KM_ROOT_OF_TRUST) = {
67 ASN1_SIMPLE(KM_ROOT_OF_TRUST, verified_boot_key, ASN1_OCTET_STRING),
68 ASN1_SIMPLE(KM_ROOT_OF_TRUST, device_locked, ASN1_BOOLEAN),
69 ASN1_SIMPLE(KM_ROOT_OF_TRUST, verified_boot_state, ASN1_ENUMERATED),
70 ASN1_SIMPLE(KM_ROOT_OF_TRUST, verified_boot_hash, ASN1_OCTET_STRING),
71 } ASN1_SEQUENCE_END(KM_ROOT_OF_TRUST);
72 IMPLEMENT_ASN1_FUNCTIONS(KM_ROOT_OF_TRUST);
73
74 typedef struct km_auth_list {
75 ASN1_INTEGER_SET* purpose;
76 ASN1_INTEGER* algorithm;
77 ASN1_INTEGER* key_size;
78 ASN1_INTEGER_SET* digest;
79 ASN1_INTEGER_SET* padding;
80 ASN1_INTEGER* ec_curve;
81 ASN1_INTEGER* rsa_public_exponent;
82 ASN1_NULL* rollback_resistance;
83 ASN1_NULL* early_boot_only;
84 ASN1_INTEGER* active_date_time;
85 ASN1_INTEGER* origination_expire_date_time;
86 ASN1_INTEGER* usage_expire_date_time;
87 ASN1_NULL* no_auth_required;
88 ASN1_INTEGER* user_auth_type;
89 ASN1_INTEGER* auth_timeout;
90 ASN1_NULL* allow_while_on_body;
91 ASN1_NULL* trusted_user_presence_required;
92 ASN1_NULL* trusted_confirmation_required;
93 ASN1_NULL* unlocked_device_required;
94 ASN1_NULL* all_applications;
95 ASN1_OCTET_STRING* application_id;
96 ASN1_INTEGER* creation_date_time;
97 ASN1_INTEGER* origin;
98 KM_ROOT_OF_TRUST* root_of_trust;
99 ASN1_INTEGER* os_version;
100 ASN1_INTEGER* os_patchlevel;
101 ASN1_OCTET_STRING* attestation_application_id;
102 ASN1_OCTET_STRING* attestation_id_brand;
103 ASN1_OCTET_STRING* attestation_id_device;
104 ASN1_OCTET_STRING* attestation_id_product;
105 ASN1_OCTET_STRING* attestation_id_serial;
106 ASN1_OCTET_STRING* attestation_id_imei;
107 ASN1_OCTET_STRING* attestation_id_meid;
108 ASN1_OCTET_STRING* attestation_id_manufacturer;
109 ASN1_OCTET_STRING* attestation_id_model;
110 ASN1_INTEGER* vendor_patchlevel;
111 ASN1_INTEGER* boot_patchlevel;
112 ASN1_NULL* device_unique_attestation;
113 ASN1_NULL* identity_credential_key;
114 } KM_AUTH_LIST;
115
116 ASN1_SEQUENCE(KM_AUTH_LIST) = {
117 ASN1_EXP_SET_OF_OPT(KM_AUTH_LIST, purpose, ASN1_INTEGER, TAG_PURPOSE.maskedTag()),
118 ASN1_EXP_OPT(KM_AUTH_LIST, algorithm, ASN1_INTEGER, TAG_ALGORITHM.maskedTag()),
119 ASN1_EXP_OPT(KM_AUTH_LIST, key_size, ASN1_INTEGER, TAG_KEY_SIZE.maskedTag()),
120 ASN1_EXP_SET_OF_OPT(KM_AUTH_LIST, digest, ASN1_INTEGER, TAG_DIGEST.maskedTag()),
121 ASN1_EXP_SET_OF_OPT(KM_AUTH_LIST, padding, ASN1_INTEGER, TAG_PADDING.maskedTag()),
122 ASN1_EXP_OPT(KM_AUTH_LIST, ec_curve, ASN1_INTEGER, TAG_EC_CURVE.maskedTag()),
123 ASN1_EXP_OPT(KM_AUTH_LIST, rsa_public_exponent, ASN1_INTEGER,
124 TAG_RSA_PUBLIC_EXPONENT.maskedTag()),
125 ASN1_EXP_OPT(KM_AUTH_LIST, rollback_resistance, ASN1_NULL,
126 TAG_ROLLBACK_RESISTANCE.maskedTag()),
127 ASN1_EXP_OPT(KM_AUTH_LIST, early_boot_only, ASN1_NULL, TAG_EARLY_BOOT_ONLY.maskedTag()),
128 ASN1_EXP_OPT(KM_AUTH_LIST, active_date_time, ASN1_INTEGER, TAG_ACTIVE_DATETIME.maskedTag()),
129 ASN1_EXP_OPT(KM_AUTH_LIST, origination_expire_date_time, ASN1_INTEGER,
130 TAG_ORIGINATION_EXPIRE_DATETIME.maskedTag()),
131 ASN1_EXP_OPT(KM_AUTH_LIST, usage_expire_date_time, ASN1_INTEGER,
132 TAG_USAGE_EXPIRE_DATETIME.maskedTag()),
133 ASN1_EXP_OPT(KM_AUTH_LIST, no_auth_required, ASN1_NULL, TAG_NO_AUTH_REQUIRED.maskedTag()),
134 ASN1_EXP_OPT(KM_AUTH_LIST, user_auth_type, ASN1_INTEGER, TAG_USER_AUTH_TYPE.maskedTag()),
135 ASN1_EXP_OPT(KM_AUTH_LIST, auth_timeout, ASN1_INTEGER, TAG_AUTH_TIMEOUT.maskedTag()),
136 ASN1_EXP_OPT(KM_AUTH_LIST, allow_while_on_body, ASN1_NULL,
137 TAG_ALLOW_WHILE_ON_BODY.maskedTag()),
138 ASN1_EXP_OPT(KM_AUTH_LIST, trusted_user_presence_required, ASN1_NULL,
139 TAG_TRUSTED_USER_PRESENCE_REQUIRED.maskedTag()),
140 ASN1_EXP_OPT(KM_AUTH_LIST, trusted_confirmation_required, ASN1_NULL,
141 TAG_TRUSTED_CONFIRMATION_REQUIRED.maskedTag()),
142 ASN1_EXP_OPT(KM_AUTH_LIST, unlocked_device_required, ASN1_NULL,
143 TAG_UNLOCKED_DEVICE_REQUIRED.maskedTag()),
144 ASN1_EXP_OPT(KM_AUTH_LIST, creation_date_time, ASN1_INTEGER,
145 TAG_CREATION_DATETIME.maskedTag()),
146 ASN1_EXP_OPT(KM_AUTH_LIST, origin, ASN1_INTEGER, TAG_ORIGIN.maskedTag()),
147 ASN1_EXP_OPT(KM_AUTH_LIST, root_of_trust, KM_ROOT_OF_TRUST, TAG_ROOT_OF_TRUST.maskedTag()),
148 ASN1_EXP_OPT(KM_AUTH_LIST, os_version, ASN1_INTEGER, TAG_OS_VERSION.maskedTag()),
149 ASN1_EXP_OPT(KM_AUTH_LIST, os_patchlevel, ASN1_INTEGER, TAG_OS_PATCHLEVEL.maskedTag()),
150 ASN1_EXP_OPT(KM_AUTH_LIST, attestation_application_id, ASN1_OCTET_STRING,
151 TAG_ATTESTATION_APPLICATION_ID.maskedTag()),
152 ASN1_EXP_OPT(KM_AUTH_LIST, attestation_id_brand, ASN1_OCTET_STRING,
153 TAG_ATTESTATION_ID_BRAND.maskedTag()),
154 ASN1_EXP_OPT(KM_AUTH_LIST, attestation_id_device, ASN1_OCTET_STRING,
155 TAG_ATTESTATION_ID_DEVICE.maskedTag()),
156 ASN1_EXP_OPT(KM_AUTH_LIST, attestation_id_product, ASN1_OCTET_STRING,
157 TAG_ATTESTATION_ID_PRODUCT.maskedTag()),
158 ASN1_EXP_OPT(KM_AUTH_LIST, attestation_id_serial, ASN1_OCTET_STRING,
159 TAG_ATTESTATION_ID_SERIAL.maskedTag()),
160 ASN1_EXP_OPT(KM_AUTH_LIST, attestation_id_imei, ASN1_OCTET_STRING,
161 TAG_ATTESTATION_ID_IMEI.maskedTag()),
162 ASN1_EXP_OPT(KM_AUTH_LIST, attestation_id_meid, ASN1_OCTET_STRING,
163 TAG_ATTESTATION_ID_MEID.maskedTag()),
164 ASN1_EXP_OPT(KM_AUTH_LIST, attestation_id_manufacturer, ASN1_OCTET_STRING,
165 TAG_ATTESTATION_ID_MANUFACTURER.maskedTag()),
166 ASN1_EXP_OPT(KM_AUTH_LIST, attestation_id_model, ASN1_OCTET_STRING,
167 TAG_ATTESTATION_ID_MODEL.maskedTag()),
168 ASN1_EXP_OPT(KM_AUTH_LIST, vendor_patchlevel, ASN1_INTEGER,
169 TAG_VENDOR_PATCHLEVEL.maskedTag()),
170 ASN1_EXP_OPT(KM_AUTH_LIST, boot_patchlevel, ASN1_INTEGER, TAG_BOOT_PATCHLEVEL.maskedTag()),
171 ASN1_EXP_OPT(KM_AUTH_LIST, device_unique_attestation, ASN1_NULL,
172 TAG_DEVICE_UNIQUE_ATTESTATION.maskedTag()),
173 ASN1_EXP_OPT(KM_AUTH_LIST, identity_credential_key, ASN1_NULL,
174 TAG_IDENTITY_CREDENTIAL_KEY.maskedTag()),
175 } ASN1_SEQUENCE_END(KM_AUTH_LIST);
176 IMPLEMENT_ASN1_FUNCTIONS(KM_AUTH_LIST);
177
178 typedef struct km_key_description {
179 ASN1_INTEGER* attestation_version;
180 ASN1_ENUMERATED* attestation_security_level;
181 ASN1_INTEGER* keymaster_version;
182 ASN1_ENUMERATED* keymaster_security_level;
183 ASN1_OCTET_STRING* attestation_challenge;
184 KM_AUTH_LIST* software_enforced;
185 KM_AUTH_LIST* tee_enforced;
186 ASN1_INTEGER* unique_id;
187 } KM_KEY_DESCRIPTION;
188
189 ASN1_SEQUENCE(KM_KEY_DESCRIPTION) = {
190 ASN1_SIMPLE(KM_KEY_DESCRIPTION, attestation_version, ASN1_INTEGER),
191 ASN1_SIMPLE(KM_KEY_DESCRIPTION, attestation_security_level, ASN1_ENUMERATED),
192 ASN1_SIMPLE(KM_KEY_DESCRIPTION, keymaster_version, ASN1_INTEGER),
193 ASN1_SIMPLE(KM_KEY_DESCRIPTION, keymaster_security_level, ASN1_ENUMERATED),
194 ASN1_SIMPLE(KM_KEY_DESCRIPTION, attestation_challenge, ASN1_OCTET_STRING),
195 ASN1_SIMPLE(KM_KEY_DESCRIPTION, unique_id, ASN1_OCTET_STRING),
196 ASN1_SIMPLE(KM_KEY_DESCRIPTION, software_enforced, KM_AUTH_LIST),
197 ASN1_SIMPLE(KM_KEY_DESCRIPTION, tee_enforced, KM_AUTH_LIST),
198 } ASN1_SEQUENCE_END(KM_KEY_DESCRIPTION);
199 IMPLEMENT_ASN1_FUNCTIONS(KM_KEY_DESCRIPTION);
200
201 template <V4_0::Tag tag>
copyAuthTag(const stack_st_ASN1_INTEGER * stack,TypedTag<TagType::ENUM_REP,tag> ttag,AuthorizationSet * auth_list)202 void copyAuthTag(const stack_st_ASN1_INTEGER* stack, TypedTag<TagType::ENUM_REP, tag> ttag,
203 AuthorizationSet* auth_list) {
204 typedef typename V4_0::TypedTag2ValueType<decltype(ttag)>::type ValueT;
205 for (size_t i = 0; i < sk_ASN1_INTEGER_num(stack); ++i) {
206 auth_list->push_back(
207 ttag, static_cast<ValueT>(ASN1_INTEGER_get(sk_ASN1_INTEGER_value(stack, i))));
208 }
209 }
210
211 template <V4_0::Tag tag>
copyAuthTag(const ASN1_INTEGER * asn1_int,TypedTag<TagType::ENUM,tag> ttag,AuthorizationSet * auth_list)212 void copyAuthTag(const ASN1_INTEGER* asn1_int, TypedTag<TagType::ENUM, tag> ttag,
213 AuthorizationSet* auth_list) {
214 typedef typename V4_0::TypedTag2ValueType<decltype(ttag)>::type ValueT;
215 if (!asn1_int) return;
216 auth_list->push_back(ttag, static_cast<ValueT>(ASN1_INTEGER_get(asn1_int)));
217 }
218
219 template <V4_0::Tag tag>
copyAuthTag(const ASN1_INTEGER * asn1_int,TypedTag<TagType::UINT,tag> ttag,AuthorizationSet * auth_list)220 void copyAuthTag(const ASN1_INTEGER* asn1_int, TypedTag<TagType::UINT, tag> ttag,
221 AuthorizationSet* auth_list) {
222 if (!asn1_int) return;
223 auth_list->push_back(ttag, ASN1_INTEGER_get(asn1_int));
224 }
225
construct_uint_max()226 BIGNUM* construct_uint_max() {
227 BIGNUM* value = BN_new();
228 BIGNUM_Ptr one(BN_new());
229 BN_one(one.get());
230 BN_lshift(value, one.get(), 32);
231 return value;
232 }
233
BignumToUint64(BIGNUM * num)234 uint64_t BignumToUint64(BIGNUM* num) {
235 static_assert((sizeof(BN_ULONG) == sizeof(uint32_t)) || (sizeof(BN_ULONG) == sizeof(uint64_t)),
236 "This implementation only supports 32 and 64-bit BN_ULONG");
237 if (sizeof(BN_ULONG) == sizeof(uint32_t)) {
238 BIGNUM_Ptr uint_max(construct_uint_max());
239 BIGNUM_Ptr hi(BN_new()), lo(BN_new());
240 BN_CTX_Ptr ctx(BN_CTX_new());
241 BN_div(hi.get(), lo.get(), num, uint_max.get(), ctx.get());
242 return static_cast<uint64_t>(BN_get_word(hi.get())) << 32 | BN_get_word(lo.get());
243 } else if (sizeof(BN_ULONG) == sizeof(uint64_t)) {
244 return BN_get_word(num);
245 } else {
246 return 0;
247 }
248 }
249
250 template <V4_0::Tag tag>
copyAuthTag(const ASN1_INTEGER * asn1_int,TypedTag<TagType::ULONG,tag> ttag,AuthorizationSet * auth_list)251 void copyAuthTag(const ASN1_INTEGER* asn1_int, TypedTag<TagType::ULONG, tag> ttag,
252 AuthorizationSet* auth_list) {
253 if (!asn1_int) return;
254 BIGNUM_Ptr num(ASN1_INTEGER_to_BN(asn1_int, nullptr));
255 auth_list->push_back(ttag, BignumToUint64(num.get()));
256 }
257
258 template <V4_0::Tag tag>
copyAuthTag(const ASN1_INTEGER * asn1_int,TypedTag<TagType::DATE,tag> ttag,AuthorizationSet * auth_list)259 void copyAuthTag(const ASN1_INTEGER* asn1_int, TypedTag<TagType::DATE, tag> ttag,
260 AuthorizationSet* auth_list) {
261 if (!asn1_int) return;
262 BIGNUM_Ptr num(ASN1_INTEGER_to_BN(asn1_int, nullptr));
263 auth_list->push_back(ttag, BignumToUint64(num.get()));
264 }
265
266 template <V4_0::Tag tag>
copyAuthTag(const ASN1_NULL * asn1_null,TypedTag<TagType::BOOL,tag> ttag,AuthorizationSet * auth_list)267 void copyAuthTag(const ASN1_NULL* asn1_null, TypedTag<TagType::BOOL, tag> ttag,
268 AuthorizationSet* auth_list) {
269 if (!asn1_null) return;
270 auth_list->push_back(ttag);
271 }
272
273 template <V4_0::Tag tag>
copyAuthTag(const ASN1_OCTET_STRING * asn1_string,TypedTag<TagType::BYTES,tag> ttag,AuthorizationSet * auth_list)274 void copyAuthTag(const ASN1_OCTET_STRING* asn1_string, TypedTag<TagType::BYTES, tag> ttag,
275 AuthorizationSet* auth_list) {
276 if (!asn1_string) return;
277 hidl_vec<uint8_t> buf;
278 buf.setToExternal(asn1_string->data, asn1_string->length);
279 auth_list->push_back(ttag, buf);
280 }
281
282 // Extract the values from the specified ASN.1 record and place them in auth_list.
extract_auth_list(const KM_AUTH_LIST * record,AuthorizationSet * auth_list)283 static ErrorCode extract_auth_list(const KM_AUTH_LIST* record, AuthorizationSet* auth_list) {
284 if (!record) return ErrorCode::OK;
285
286 copyAuthTag(record->active_date_time, TAG_ACTIVE_DATETIME, auth_list);
287 copyAuthTag(record->algorithm, TAG_ALGORITHM, auth_list);
288 copyAuthTag(record->application_id, TAG_APPLICATION_ID, auth_list);
289 copyAuthTag(record->auth_timeout, TAG_AUTH_TIMEOUT, auth_list);
290 copyAuthTag(record->creation_date_time, TAG_CREATION_DATETIME, auth_list);
291 copyAuthTag(record->digest, TAG_DIGEST, auth_list);
292 copyAuthTag(record->ec_curve, TAG_EC_CURVE, auth_list);
293 copyAuthTag(record->key_size, TAG_KEY_SIZE, auth_list);
294 copyAuthTag(record->no_auth_required, TAG_NO_AUTH_REQUIRED, auth_list);
295 copyAuthTag(record->origin, TAG_ORIGIN, auth_list);
296 copyAuthTag(record->origination_expire_date_time, TAG_ORIGINATION_EXPIRE_DATETIME, auth_list);
297 copyAuthTag(record->os_patchlevel, TAG_OS_PATCHLEVEL, auth_list);
298 copyAuthTag(record->os_version, TAG_OS_VERSION, auth_list);
299 copyAuthTag(record->padding, TAG_PADDING, auth_list);
300 copyAuthTag(record->purpose, TAG_PURPOSE, auth_list);
301 copyAuthTag(record->rollback_resistance, TAG_ROLLBACK_RESISTANCE, auth_list);
302 copyAuthTag(record->rsa_public_exponent, TAG_RSA_PUBLIC_EXPONENT, auth_list);
303 copyAuthTag(record->usage_expire_date_time, TAG_USAGE_EXPIRE_DATETIME, auth_list);
304 copyAuthTag(record->user_auth_type, TAG_USER_AUTH_TYPE, auth_list);
305 copyAuthTag(record->attestation_application_id, TAG_ATTESTATION_APPLICATION_ID, auth_list);
306 copyAuthTag(record->attestation_id_brand, TAG_ATTESTATION_ID_BRAND, auth_list);
307 copyAuthTag(record->attestation_id_device, TAG_ATTESTATION_ID_DEVICE, auth_list);
308 copyAuthTag(record->attestation_id_product, TAG_ATTESTATION_ID_PRODUCT, auth_list);
309 copyAuthTag(record->attestation_id_serial, TAG_ATTESTATION_ID_SERIAL, auth_list);
310 copyAuthTag(record->attestation_id_imei, TAG_ATTESTATION_ID_IMEI, auth_list);
311 copyAuthTag(record->attestation_id_meid, TAG_ATTESTATION_ID_MEID, auth_list);
312 copyAuthTag(record->attestation_id_manufacturer, TAG_ATTESTATION_ID_MANUFACTURER, auth_list);
313 copyAuthTag(record->attestation_id_model, TAG_ATTESTATION_ID_MODEL, auth_list);
314 copyAuthTag(record->vendor_patchlevel, TAG_VENDOR_PATCHLEVEL, auth_list);
315 copyAuthTag(record->boot_patchlevel, TAG_BOOT_PATCHLEVEL, auth_list);
316 copyAuthTag(record->trusted_user_presence_required, TAG_TRUSTED_USER_PRESENCE_REQUIRED,
317 auth_list);
318 copyAuthTag(record->trusted_confirmation_required, TAG_TRUSTED_CONFIRMATION_REQUIRED,
319 auth_list);
320 copyAuthTag(record->unlocked_device_required, TAG_UNLOCKED_DEVICE_REQUIRED, auth_list);
321 copyAuthTag(record->early_boot_only, TAG_EARLY_BOOT_ONLY, auth_list);
322 copyAuthTag(record->device_unique_attestation, TAG_DEVICE_UNIQUE_ATTESTATION, auth_list);
323 copyAuthTag(record->identity_credential_key, TAG_IDENTITY_CREDENTIAL_KEY, auth_list);
324
325 return ErrorCode::OK;
326 }
327
MAKE_OPENSSL_PTR_TYPE(KM_KEY_DESCRIPTION)328 MAKE_OPENSSL_PTR_TYPE(KM_KEY_DESCRIPTION)
329
330 // Parse the DER-encoded attestation record, placing the results in keymaster_version,
331 // attestation_challenge, software_enforced, tee_enforced and unique_id.
332 std::tuple<ErrorCode, AttestationRecord> parse_attestation_record(const hidl_vec<uint8_t>& cert) {
333 const uint8_t* p = cert.data();
334 X509_Ptr x509(d2i_X509(nullptr, &p, cert.size()));
335 if (!x509.get()) {
336 LOG(ERROR) << "Error converting DER";
337 return {ErrorCode::INVALID_ARGUMENT, {}};
338 }
339
340 ASN1_OBJECT_Ptr oid(OBJ_txt2obj(kAttestionRecordOid, 1 /* dotted string format */));
341 if (!oid.get()) {
342 LOG(ERROR) << "Error parsing OID";
343 return {ErrorCode::UNKNOWN_ERROR, {}};
344 }
345
346 int location = X509_get_ext_by_OBJ(x509.get(), oid.get(), -1 /* search from beginning */);
347 if (location == -1) {
348 LOG(ERROR) << "Attestation extension not found in certificate";
349 return {ErrorCode::UNKNOWN_ERROR, {}};
350 }
351
352 X509_EXTENSION* attest_rec_ext = X509_get_ext(x509.get(), location);
353 if (!attest_rec_ext) {
354 LOG(ERROR) << "Found extension but couldn't retrieve it. Probably BoringSSL bug.";
355 return {ErrorCode::UNKNOWN_ERROR, {}};
356 }
357
358 ASN1_OCTET_STRING* attest_rec = X509_EXTENSION_get_data(attest_rec_ext);
359 if (!attest_rec_ext) {
360 LOG(ERROR) << "Attestation extension contained no data";
361 return {ErrorCode::UNKNOWN_ERROR, {}};
362 }
363
364 p = attest_rec->data;
365 KM_KEY_DESCRIPTION_Ptr record(d2i_KM_KEY_DESCRIPTION(nullptr, &p, attest_rec->length));
366 if (!record.get()) {
367 LOG(ERROR) << "Unable to get key description";
368 return {ErrorCode::UNKNOWN_ERROR, {}};
369 }
370
371 AttestationRecord result;
372
373 result.attestation_version = ASN1_INTEGER_get(record->attestation_version);
374 result.attestation_security_level =
375 static_cast<SecurityLevel>(ASN1_ENUMERATED_get(record->attestation_security_level));
376 result.keymaster_version = ASN1_INTEGER_get(record->keymaster_version);
377 result.keymaster_security_level =
378 static_cast<SecurityLevel>(ASN1_ENUMERATED_get(record->keymaster_security_level));
379
380 auto& chall = record->attestation_challenge;
381 result.attestation_challenge.resize(chall->length);
382 memcpy(result.attestation_challenge.data(), chall->data, chall->length);
383 auto& uid = record->unique_id;
384 result.unique_id.resize(uid->length);
385 memcpy(result.unique_id.data(), uid->data, uid->length);
386
387 ErrorCode error = extract_auth_list(record->software_enforced, &result.software_enforced);
388 if (error != ErrorCode::OK) return {error, {}};
389
390 error = extract_auth_list(record->tee_enforced, &result.hardware_enforced);
391 if (error != ErrorCode::OK) return {error, {}};
392
393 KM_ROOT_OF_TRUST* root_of_trust = nullptr;
394 SecurityLevel root_of_trust_security_level = SecurityLevel::TRUSTED_ENVIRONMENT;
395 if (record->tee_enforced && record->tee_enforced->root_of_trust) {
396 root_of_trust = record->tee_enforced->root_of_trust;
397 } else if (record->software_enforced && record->software_enforced->root_of_trust) {
398 root_of_trust = record->software_enforced->root_of_trust;
399 root_of_trust_security_level = SecurityLevel::SOFTWARE;
400 } else {
401 LOG(ERROR) << AT << " Failed root of trust parsing";
402 return {ErrorCode::INVALID_ARGUMENT, {}};
403 }
404 if (!root_of_trust->verified_boot_key) {
405 LOG(ERROR) << AT << " Failed verified boot key parsing";
406 return {ErrorCode::INVALID_ARGUMENT, {}};
407 }
408
409 RootOfTrust& rot = result.root_of_trust;
410 auto& vb_key = root_of_trust->verified_boot_key;
411 rot.verified_boot_key.resize(vb_key->length);
412 memcpy(rot.verified_boot_key.data(), vb_key->data, vb_key->length);
413
414 rot.verified_boot_state = static_cast<keymaster_verified_boot_t>(
415 ASN1_ENUMERATED_get(root_of_trust->verified_boot_state));
416 rot.device_locked = root_of_trust->device_locked;
417 rot.security_level = root_of_trust_security_level;
418
419 auto& vb_hash = root_of_trust->verified_boot_hash;
420 if (!vb_hash) {
421 LOG(ERROR) << AT << " Failed verified boot hash parsing";
422 return {ErrorCode::INVALID_ARGUMENT, {}};
423 }
424 rot.verified_boot_hash.resize(vb_hash->length);
425 memcpy(rot.verified_boot_hash.data(), vb_hash->data, vb_hash->length);
426
427 return {ErrorCode::OK, result};
428 }
429
430 } // namespace V4_1
431 } // namespace keymaster
432 } // namespace hardware
433 } // namespace android
434