1 /*
2 * Copyright 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 */
16
17 #include <keymaster/km_openssl/attestation_record.h>
18
19 #include <assert.h>
20 #include <math.h>
21
22 #include <unordered_map>
23
24 #include <cppbor_parse.h>
25 #include <openssl/asn1t.h>
26
27 #include <keymaster/android_keymaster_utils.h>
28 #include <keymaster/attestation_context.h>
29 #include <keymaster/km_openssl/hmac.h>
30 #include <keymaster/km_openssl/openssl_err.h>
31 #include <keymaster/km_openssl/openssl_utils.h>
32
33 #define ASSERT_OR_RETURN_ERROR(stmt, error) \
34 do { \
35 assert(stmt); \
36 if (!(stmt)) { \
37 return error; \
38 } \
39 } while (0)
40
41 namespace keymaster {
42
43 constexpr size_t kMaximumAttestationChallengeLength = 128;
44
45 IMPLEMENT_ASN1_FUNCTIONS(KM_ROOT_OF_TRUST);
46 IMPLEMENT_ASN1_FUNCTIONS(KM_AUTH_LIST);
47 IMPLEMENT_ASN1_FUNCTIONS(KM_KEY_DESCRIPTION);
48
49 static const keymaster_tag_t kDeviceAttestationTags[] = {
50 KM_TAG_ATTESTATION_ID_BRAND, KM_TAG_ATTESTATION_ID_DEVICE, KM_TAG_ATTESTATION_ID_PRODUCT,
51 KM_TAG_ATTESTATION_ID_SERIAL, KM_TAG_ATTESTATION_ID_IMEI, KM_TAG_ATTESTATION_ID_MEID,
52 KM_TAG_ATTESTATION_ID_MANUFACTURER, KM_TAG_ATTESTATION_ID_MODEL,
53 };
54
55 struct KM_AUTH_LIST_Delete {
operator ()keymaster::KM_AUTH_LIST_Delete56 void operator()(KM_AUTH_LIST* p) { KM_AUTH_LIST_free(p); }
57 };
58
59 struct KM_KEY_DESCRIPTION_Delete {
operator ()keymaster::KM_KEY_DESCRIPTION_Delete60 void operator()(KM_KEY_DESCRIPTION* p) { KM_KEY_DESCRIPTION_free(p); }
61 };
62
63 struct KM_ROOT_OF_TRUST_Delete {
operator ()keymaster::KM_ROOT_OF_TRUST_Delete64 void operator()(KM_ROOT_OF_TRUST* p) { KM_ROOT_OF_TRUST_free(p); }
65 };
66
blob_to_bstr(const keymaster_blob_t & blob)67 static cppbor::Bstr blob_to_bstr(const keymaster_blob_t& blob) {
68 return cppbor::Bstr(std::pair(blob.data, blob.data_length));
69 }
70
bstr_to_blob(const cppbor::Bstr * bstr,keymaster_blob_t * blob)71 static keymaster_error_t bstr_to_blob(const cppbor::Bstr* bstr, keymaster_blob_t* blob) {
72 ASSERT_OR_RETURN_ERROR(bstr, KM_ERROR_INVALID_TAG);
73 const std::vector<uint8_t>& vec = bstr->value();
74 uint8_t* data = (uint8_t*)calloc(vec.size(), sizeof(uint8_t));
75 if (data == nullptr) {
76 return KM_ERROR_MEMORY_ALLOCATION_FAILED;
77 }
78
79 std::copy(vec.begin(), vec.end(), data);
80 blob->data = data;
81 blob->data_length = vec.size();
82
83 return KM_ERROR_OK;
84 }
85
get_uint32_value(const keymaster_key_param_t & param)86 static uint32_t get_uint32_value(const keymaster_key_param_t& param) {
87 switch (keymaster_tag_get_type(param.tag)) {
88 case KM_ENUM:
89 case KM_ENUM_REP:
90 return param.enumerated;
91 case KM_UINT:
92 case KM_UINT_REP:
93 return param.integer;
94 default:
95 ASSERT_OR_RETURN_ERROR(false, 0xFFFFFFFF);
96 }
97 }
98
get_uint32_value(EatSecurityLevel level)99 static int64_t get_uint32_value(EatSecurityLevel level) {
100 return static_cast<int64_t>(level);
101 }
102
103 // Insert value in either the dest_integer or the dest_integer_set, whichever is provided.
insert_integer(ASN1_INTEGER * value,ASN1_INTEGER ** dest_integer,ASN1_INTEGER_SET ** dest_integer_set)104 static keymaster_error_t insert_integer(ASN1_INTEGER* value, ASN1_INTEGER** dest_integer,
105 ASN1_INTEGER_SET** dest_integer_set) {
106 ASSERT_OR_RETURN_ERROR((dest_integer == nullptr) ^ (dest_integer_set == nullptr),
107 KM_ERROR_UNEXPECTED_NULL_POINTER);
108 ASSERT_OR_RETURN_ERROR(value, KM_ERROR_INVALID_ARGUMENT);
109
110 if (dest_integer_set) {
111 if (!*dest_integer_set) {
112 *dest_integer_set = sk_ASN1_INTEGER_new_null();
113 }
114 if (!*dest_integer_set) {
115 return KM_ERROR_MEMORY_ALLOCATION_FAILED;
116 }
117 if (!sk_ASN1_INTEGER_push(*dest_integer_set, value)) {
118 return KM_ERROR_MEMORY_ALLOCATION_FAILED;
119 }
120 return KM_ERROR_OK;
121
122 } else if (dest_integer) {
123 if (*dest_integer) {
124 ASN1_INTEGER_free(*dest_integer);
125 }
126 *dest_integer = value;
127 return KM_ERROR_OK;
128 }
129
130 ASSERT_OR_RETURN_ERROR(false, KM_ERROR_UNKNOWN_ERROR); // Should never get here.
131 }
132
133 // Add a repeating enum to a map going mapping its key to list of values.
add_repeating_enum(EatClaim key,uint32_t value,std::unordered_map<EatClaim,cppbor::Array> * fields_map)134 static void add_repeating_enum(EatClaim key, uint32_t value,
135 std::unordered_map<EatClaim, cppbor::Array>* fields_map) {
136 auto field = fields_map->find(key);
137 if (field != fields_map->end()) {
138 field->second.add(value);
139 } else {
140 fields_map->insert({key, cppbor::Array().add(value)});
141 }
142 }
143
144 static keymaster_error_t
insert_unknown_tag(const keymaster_key_param_t & param,cppbor::Map * dest_map,std::unordered_map<EatClaim,cppbor::Array> * fields_map)145 insert_unknown_tag(const keymaster_key_param_t& param, cppbor::Map* dest_map,
146 std::unordered_map<EatClaim, cppbor::Array>* fields_map) {
147 EatClaim private_eat_tag = static_cast<EatClaim>(convert_to_eat_claim(param.tag));
148 switch (keymaster_tag_get_type(param.tag)) {
149 case KM_ENUM:
150 dest_map->add(private_eat_tag, param.enumerated);
151 break;
152 case KM_ENUM_REP:
153 add_repeating_enum(private_eat_tag, param.enumerated, fields_map);
154 break;
155 case KM_UINT:
156 dest_map->add(private_eat_tag, param.integer);
157 break;
158 case KM_UINT_REP:
159 add_repeating_enum(private_eat_tag, param.integer, fields_map);
160 break;
161 case KM_ULONG:
162 dest_map->add(private_eat_tag, param.long_integer);
163 break;
164 case KM_ULONG_REP:
165 add_repeating_enum(private_eat_tag, param.long_integer, fields_map);
166 break;
167 case KM_DATE:
168 dest_map->add(private_eat_tag, param.date_time);
169 break;
170 case KM_BOOL:
171 dest_map->add(private_eat_tag, true);
172 break;
173 case KM_BIGNUM:
174 case KM_BYTES:
175 dest_map->add(private_eat_tag, blob_to_bstr(param.blob));
176 break;
177 default:
178 ASSERT_OR_RETURN_ERROR(false, KM_ERROR_INVALID_TAG);
179 }
180 return KM_ERROR_OK;
181 }
182
183 /**
184 * Convert an IMEI encoded as a string of numbers into the UEID format defined in
185 * https://tools.ietf.org/html/draft-ietf-rats-eat.
186 * The resulting format is a bstr encoded as follows:
187 * - Type byte: 0x03
188 * - IMEI (without check digit), encoded as byte string of length 14 with each byte as the digit's
189 * value. The IMEI value encoded SHALL NOT include Luhn checksum or SVN information.
190 */
imei_to_ueid(const keymaster_blob_t & imei_blob,cppbor::Bstr * out)191 keymaster_error_t imei_to_ueid(const keymaster_blob_t& imei_blob, cppbor::Bstr* out) {
192 ASSERT_OR_RETURN_ERROR(imei_blob.data_length == kImeiBlobLength, KM_ERROR_INVALID_TAG);
193
194 uint8_t ueid[kUeidLength];
195 ueid[0] = kImeiTypeByte;
196 // imei_blob corresponds to android.telephony.TelephonyManager#getDeviceId(), which is the
197 // 15-digit IMEI (including the check digit), encoded as a string.
198 for (size_t i = 1; i < kUeidLength; i++) {
199 // Convert each character to its numeric value.
200 ueid[i] = imei_blob.data[i - 1] - '0'; // Intentionally skip check digit at last position.
201 }
202
203 *out = cppbor::Bstr(std::pair(ueid, sizeof(ueid)));
204 return KM_ERROR_OK;
205 }
206
ueid_to_imei_blob(const cppbor::Bstr * ueid,keymaster_blob_t * out)207 keymaster_error_t ueid_to_imei_blob(const cppbor::Bstr* ueid, keymaster_blob_t* out) {
208 ASSERT_OR_RETURN_ERROR(ueid, KM_ERROR_INVALID_TAG);
209 const std::vector<uint8_t>& ueid_vec = ueid->value();
210 ASSERT_OR_RETURN_ERROR(ueid_vec.size() == kUeidLength, KM_ERROR_INVALID_TAG);
211 ASSERT_OR_RETURN_ERROR(ueid_vec[0] == kImeiTypeByte, KM_ERROR_INVALID_TAG);
212
213 uint8_t* imei_string = (uint8_t*)calloc(kImeiBlobLength, sizeof(uint8_t));
214 // Fill string from left to right, and calculate Luhn check digit.
215 int luhn_digit_sum = 0;
216 for (size_t i = 0; i < kImeiBlobLength - 1; i++) {
217 uint8_t digit_i = ueid_vec[i + 1];
218 // Convert digit to its string value.
219 imei_string[i] = '0' + digit_i;
220 luhn_digit_sum += i % 2 == 0 ? digit_i : digit_i * 2 / 10 + (digit_i * 2) % 10;
221 }
222 imei_string[kImeiBlobLength - 1] = '0' + (10 - luhn_digit_sum % 10) % 10;
223
224 *out = {.data = imei_string, .data_length = kImeiBlobLength};
225 return KM_ERROR_OK;
226 }
227
ec_key_size_to_eat_curve(uint32_t key_size_bits,int * curve)228 keymaster_error_t ec_key_size_to_eat_curve(uint32_t key_size_bits, int* curve) {
229 switch (key_size_bits) {
230 default:
231 return KM_ERROR_UNSUPPORTED_KEY_SIZE;
232
233 case 224:
234 *curve = (int)EatEcCurve::P_224;
235 break;
236
237 case 256:
238 *curve = (int)EatEcCurve::P_256;
239 break;
240
241 case 384:
242 *curve = (int)EatEcCurve::P_384;
243 break;
244
245 case 521:
246 *curve = (int)EatEcCurve::P_521;
247 break;
248 }
249
250 return KM_ERROR_OK;
251 }
252
is_valid_attestation_challenge(const keymaster_blob_t & attestation_challenge)253 bool is_valid_attestation_challenge(const keymaster_blob_t& attestation_challenge) {
254 // TODO(171864369): Limit apps targeting >= API 30 to attestations in the range of
255 // [0, 128] bytes.
256 return (attestation_challenge.data_length <= kMaximumAttestationChallengeLength);
257 }
258
259 // Put the contents of the keymaster AuthorizationSet auth_list into the EAT record structure.
build_eat_submod(const AuthorizationSet & auth_list,const EatSecurityLevel security_level,cppbor::Map * submod)260 keymaster_error_t build_eat_submod(const AuthorizationSet& auth_list,
261 const EatSecurityLevel security_level, cppbor::Map* submod) {
262 ASSERT_OR_RETURN_ERROR(submod, KM_ERROR_UNEXPECTED_NULL_POINTER);
263
264 if (auth_list.empty()) return KM_ERROR_OK;
265
266 submod->add(EatClaim::SECURITY_LEVEL, get_uint32_value(security_level));
267
268 // Keep repeating fields in a separate map for easy lookup.
269 // Add them to submod map in postprocessing.
270 std::unordered_map<EatClaim, cppbor::Array> repeating_fields =
271 std::unordered_map<EatClaim, cppbor::Array>();
272
273 for (auto entry : auth_list) {
274
275 switch (entry.tag) {
276
277 default:
278 // Unknown tags should only be included if they're software-enforced.
279 if (security_level == EatSecurityLevel::UNRESTRICTED) {
280 keymaster_error_t error = insert_unknown_tag(entry, submod, &repeating_fields);
281 if (error != KM_ERROR_OK) {
282 return error;
283 }
284 }
285 break;
286
287 /* Tags ignored because they should never exist */
288 case KM_TAG_INVALID:
289
290 /* Tags ignored because they're not used. */
291 case KM_TAG_ALL_USERS:
292 case KM_TAG_EXPORTABLE:
293 case KM_TAG_ECIES_SINGLE_HASH_MODE:
294 case KM_TAG_KDF:
295
296 /* Tags ignored because they're used only to provide information to operations */
297 case KM_TAG_ASSOCIATED_DATA:
298 case KM_TAG_NONCE:
299 case KM_TAG_AUTH_TOKEN:
300 case KM_TAG_MAC_LENGTH:
301 case KM_TAG_ATTESTATION_CHALLENGE:
302 case KM_TAG_RESET_SINCE_ID_ROTATION:
303
304 /* Tags ignored because they have no meaning off-device */
305 case KM_TAG_USER_ID:
306 case KM_TAG_USER_SECURE_ID:
307 case KM_TAG_BLOB_USAGE_REQUIREMENTS:
308
309 /* Tags ignored because they're not usable by app keys */
310 case KM_TAG_BOOTLOADER_ONLY:
311 case KM_TAG_INCLUDE_UNIQUE_ID:
312 case KM_TAG_MAX_USES_PER_BOOT:
313 case KM_TAG_MIN_SECONDS_BETWEEN_OPS:
314 case KM_TAG_UNIQUE_ID:
315
316 /* Tags ignored because they contain data that should not be exported */
317 case KM_TAG_APPLICATION_DATA:
318 case KM_TAG_APPLICATION_ID:
319 case KM_TAG_ROOT_OF_TRUST:
320 continue;
321
322 /* Non-repeating enumerations */
323 case KM_TAG_ALGORITHM:
324 submod->add(EatClaim::ALGORITHM, get_uint32_value(entry));
325 break;
326 case KM_TAG_EC_CURVE:
327 submod->add(EatClaim::EC_CURVE, get_uint32_value(entry));
328 break;
329 case KM_TAG_USER_AUTH_TYPE:
330 submod->add(EatClaim::USER_AUTH_TYPE, get_uint32_value(entry));
331 break;
332 case KM_TAG_ORIGIN:
333 submod->add(EatClaim::ORIGIN, get_uint32_value(entry));
334 break;
335
336 /* Repeating enumerations */
337 case KM_TAG_PURPOSE:
338 add_repeating_enum(EatClaim::PURPOSE, get_uint32_value(entry), &repeating_fields);
339 break;
340 case KM_TAG_PADDING:
341 add_repeating_enum(EatClaim::PADDING, get_uint32_value(entry), &repeating_fields);
342 break;
343 case KM_TAG_DIGEST:
344 add_repeating_enum(EatClaim::DIGEST, get_uint32_value(entry), &repeating_fields);
345 break;
346 case KM_TAG_BLOCK_MODE:
347 add_repeating_enum(EatClaim::BLOCK_MODE, get_uint32_value(entry), &repeating_fields);
348 break;
349
350 /* Non-repeating unsigned integers */
351 case KM_TAG_KEY_SIZE:
352 submod->add(EatClaim::KEY_SIZE, get_uint32_value(entry));
353 break;
354 case KM_TAG_AUTH_TIMEOUT:
355 submod->add(EatClaim::AUTH_TIMEOUT, get_uint32_value(entry));
356 break;
357 case KM_TAG_OS_VERSION:
358 submod->add(EatClaim::OS_VERSION, get_uint32_value(entry));
359 break;
360 case KM_TAG_OS_PATCHLEVEL:
361 submod->add(EatClaim::OS_PATCHLEVEL, get_uint32_value(entry));
362 break;
363 case KM_TAG_VENDOR_PATCHLEVEL:
364 submod->add(EatClaim::VENDOR_PATCHLEVEL, get_uint32_value(entry));
365 break;
366 case KM_TAG_BOOT_PATCHLEVEL:
367 submod->add(EatClaim::BOOT_PATCHLEVEL, get_uint32_value(entry));
368 break;
369 case KM_TAG_MIN_MAC_LENGTH:
370 submod->add(EatClaim::MIN_MAC_LENGTH, get_uint32_value(entry));
371 break;
372
373 /* Non-repeating long unsigned integers */
374 case KM_TAG_RSA_PUBLIC_EXPONENT:
375 submod->add(EatClaim::RSA_PUBLIC_EXPONENT, entry.long_integer);
376 break;
377
378 /* Dates */
379 case KM_TAG_ACTIVE_DATETIME:
380 submod->add(EatClaim::ACTIVE_DATETIME, entry.date_time);
381 break;
382 case KM_TAG_ORIGINATION_EXPIRE_DATETIME:
383 submod->add(EatClaim::ORIGINATION_EXPIRE_DATETIME, entry.date_time);
384 break;
385 case KM_TAG_USAGE_EXPIRE_DATETIME:
386 submod->add(EatClaim::USAGE_EXPIRE_DATETIME, entry.date_time);
387 break;
388 case KM_TAG_CREATION_DATETIME:
389 submod->add(EatClaim::IAT, entry.date_time);
390 break;
391
392 /* Booleans */
393 case KM_TAG_NO_AUTH_REQUIRED:
394 submod->add(EatClaim::NO_AUTH_REQUIRED, true);
395 break;
396 case KM_TAG_ALL_APPLICATIONS:
397 submod->add(EatClaim::ALL_APPLICATIONS, true);
398 break;
399 case KM_TAG_ROLLBACK_RESISTANT:
400 submod->add(EatClaim::ROLLBACK_RESISTANT, true);
401 break;
402 case KM_TAG_ALLOW_WHILE_ON_BODY:
403 submod->add(EatClaim::ALLOW_WHILE_ON_BODY, true);
404 break;
405 case KM_TAG_UNLOCKED_DEVICE_REQUIRED:
406 submod->add(EatClaim::UNLOCKED_DEVICE_REQUIRED, true);
407 break;
408 case KM_TAG_CALLER_NONCE:
409 submod->add(EatClaim::CALLER_NONCE, true);
410 break;
411 case KM_TAG_TRUSTED_CONFIRMATION_REQUIRED:
412 submod->add(EatClaim::TRUSTED_CONFIRMATION_REQUIRED, true);
413 break;
414 case KM_TAG_EARLY_BOOT_ONLY:
415 submod->add(EatClaim::EARLY_BOOT_ONLY, true);
416 break;
417 case KM_TAG_DEVICE_UNIQUE_ATTESTATION:
418 submod->add(EatClaim::DEVICE_UNIQUE_ATTESTATION, true);
419 break;
420 case KM_TAG_IDENTITY_CREDENTIAL_KEY:
421 submod->add(EatClaim::IDENTITY_CREDENTIAL_KEY, true);
422 break;
423 case KM_TAG_TRUSTED_USER_PRESENCE_REQUIRED:
424 submod->add(EatClaim::TRUSTED_USER_PRESENCE_REQUIRED, true);
425 break;
426 case KM_TAG_STORAGE_KEY:
427 submod->add(EatClaim::STORAGE_KEY, true);
428 break;
429
430 /* Byte arrays*/
431 case KM_TAG_ATTESTATION_APPLICATION_ID:
432 submod->add(EatClaim::ATTESTATION_APPLICATION_ID, blob_to_bstr(entry.blob));
433 break;
434 case KM_TAG_ATTESTATION_ID_BRAND:
435 submod->add(EatClaim::ATTESTATION_ID_BRAND, blob_to_bstr(entry.blob));
436 break;
437 case KM_TAG_ATTESTATION_ID_DEVICE:
438 submod->add(EatClaim::ATTESTATION_ID_DEVICE, blob_to_bstr(entry.blob));
439 break;
440 case KM_TAG_ATTESTATION_ID_PRODUCT:
441 submod->add(EatClaim::ATTESTATION_ID_PRODUCT, blob_to_bstr(entry.blob));
442 break;
443 case KM_TAG_ATTESTATION_ID_SERIAL:
444 submod->add(EatClaim::ATTESTATION_ID_SERIAL, blob_to_bstr(entry.blob));
445 break;
446 case KM_TAG_ATTESTATION_ID_IMEI: {
447 cppbor::Bstr ueid("");
448 keymaster_error_t error = imei_to_ueid(entry.blob, &ueid);
449 if (error != KM_ERROR_OK) return error;
450 submod->add(EatClaim::UEID, ueid);
451 break;
452 }
453 case KM_TAG_ATTESTATION_ID_MEID:
454 submod->add(EatClaim::ATTESTATION_ID_MEID, blob_to_bstr(entry.blob));
455 break;
456 case KM_TAG_ATTESTATION_ID_MANUFACTURER:
457 submod->add(EatClaim::ATTESTATION_ID_MANUFACTURER, blob_to_bstr(entry.blob));
458 break;
459 case KM_TAG_ATTESTATION_ID_MODEL:
460 submod->add(EatClaim::ATTESTATION_ID_MODEL, blob_to_bstr(entry.blob));
461 break;
462 case KM_TAG_CONFIRMATION_TOKEN:
463 submod->add(EatClaim::CONFIRMATION_TOKEN, blob_to_bstr(entry.blob));
464 break;
465 }
466 }
467
468 // Move values from repeating enums into the submod map.
469 for (auto const& repeating_field : repeating_fields) {
470 EatClaim key = static_cast<EatClaim>(repeating_field.first);
471 submod->add(key, std::move(repeating_fields.at(key)));
472 }
473
474 int ec_curve;
475 uint32_t key_size;
476 if (auth_list.Contains(TAG_ALGORITHM, KM_ALGORITHM_EC) && !auth_list.Contains(TAG_EC_CURVE) &&
477 auth_list.GetTagValue(TAG_KEY_SIZE, &key_size)) {
478 // This must be a keymaster1 key. It's an EC key with no curve. Insert the curve.
479
480 keymaster_error_t error = ec_key_size_to_eat_curve(key_size, &ec_curve);
481 if (error != KM_ERROR_OK) return error;
482
483 submod->add(EatClaim::EC_CURVE, ec_curve);
484 }
485
486 return KM_ERROR_OK;
487 }
488
489 // Put the contents of the keymaster AuthorizationSet auth_list into the ASN.1 record structure,
490 // record.
build_auth_list(const AuthorizationSet & auth_list,KM_AUTH_LIST * record)491 keymaster_error_t build_auth_list(const AuthorizationSet& auth_list, KM_AUTH_LIST* record) {
492 ASSERT_OR_RETURN_ERROR(record, KM_ERROR_UNEXPECTED_NULL_POINTER);
493
494 if (auth_list.empty()) return KM_ERROR_OK;
495
496 for (auto entry : auth_list) {
497
498 ASN1_INTEGER_SET** integer_set = nullptr;
499 ASN1_INTEGER** integer_ptr = nullptr;
500 ASN1_OCTET_STRING** string_ptr = nullptr;
501 ASN1_NULL** bool_ptr = nullptr;
502
503 switch (entry.tag) {
504
505 /* Tags ignored because they should never exist */
506 case KM_TAG_INVALID:
507
508 /* Tags ignored because they're not used. */
509 case KM_TAG_ALL_USERS:
510 case KM_TAG_EXPORTABLE:
511 case KM_TAG_ECIES_SINGLE_HASH_MODE:
512
513 /* Tags ignored because they're used only to provide information to operations */
514 case KM_TAG_ASSOCIATED_DATA:
515 case KM_TAG_NONCE:
516 case KM_TAG_AUTH_TOKEN:
517 case KM_TAG_MAC_LENGTH:
518 case KM_TAG_ATTESTATION_CHALLENGE:
519 case KM_TAG_KDF:
520
521 /* Tags ignored because they're used only to provide for certificate generation */
522 case KM_TAG_CERTIFICATE_SERIAL:
523 case KM_TAG_CERTIFICATE_SUBJECT:
524 case KM_TAG_CERTIFICATE_NOT_BEFORE:
525 case KM_TAG_CERTIFICATE_NOT_AFTER:
526 case KM_TAG_INCLUDE_UNIQUE_ID:
527 case KM_TAG_RESET_SINCE_ID_ROTATION:
528
529 /* Tags ignored because they have no meaning off-device */
530 case KM_TAG_USER_ID:
531 case KM_TAG_USER_SECURE_ID:
532 case KM_TAG_BLOB_USAGE_REQUIREMENTS:
533
534 /* Tags ignored because they're not usable by app keys */
535 case KM_TAG_BOOTLOADER_ONLY:
536 case KM_TAG_MAX_BOOT_LEVEL:
537 case KM_TAG_MAX_USES_PER_BOOT:
538 case KM_TAG_MIN_SECONDS_BETWEEN_OPS:
539 case KM_TAG_STORAGE_KEY:
540 case KM_TAG_UNIQUE_ID:
541
542 /* Tags ignored because they contain data that should not be exported */
543 case KM_TAG_APPLICATION_DATA:
544 case KM_TAG_APPLICATION_ID:
545 case KM_TAG_CONFIRMATION_TOKEN:
546 case KM_TAG_ROOT_OF_TRUST:
547 continue;
548
549 /* Non-repeating enumerations */
550 case KM_TAG_ALGORITHM:
551 integer_ptr = &record->algorithm;
552 break;
553 case KM_TAG_EC_CURVE:
554 integer_ptr = &record->ec_curve;
555 break;
556 case KM_TAG_USER_AUTH_TYPE:
557 integer_ptr = &record->user_auth_type;
558 break;
559 case KM_TAG_ORIGIN:
560 integer_ptr = &record->origin;
561 break;
562
563 /* Repeating enumerations */
564 case KM_TAG_PURPOSE:
565 integer_set = &record->purpose;
566 break;
567 case KM_TAG_PADDING:
568 integer_set = &record->padding;
569 break;
570 case KM_TAG_DIGEST:
571 integer_set = &record->digest;
572 break;
573 case KM_TAG_BLOCK_MODE:
574 integer_set = &record->block_mode;
575 break;
576 case KM_TAG_RSA_OAEP_MGF_DIGEST:
577 integer_set = &record->mgf_digest;
578 break;
579
580 /* Non-repeating unsigned integers */
581 case KM_TAG_KEY_SIZE:
582 integer_ptr = &record->key_size;
583 break;
584 case KM_TAG_AUTH_TIMEOUT:
585 integer_ptr = &record->auth_timeout;
586 break;
587 case KM_TAG_OS_VERSION:
588 integer_ptr = &record->os_version;
589 break;
590 case KM_TAG_OS_PATCHLEVEL:
591 integer_ptr = &record->os_patchlevel;
592 break;
593 case KM_TAG_MIN_MAC_LENGTH:
594 integer_ptr = &record->min_mac_length;
595 break;
596 case KM_TAG_BOOT_PATCHLEVEL:
597 integer_ptr = &record->boot_patch_level;
598 break;
599 case KM_TAG_VENDOR_PATCHLEVEL:
600 integer_ptr = &record->vendor_patchlevel;
601 break;
602 case KM_TAG_USAGE_COUNT_LIMIT:
603 integer_ptr = &record->usage_count_limit;
604 break;
605
606 /* Non-repeating long unsigned integers */
607 case KM_TAG_RSA_PUBLIC_EXPONENT:
608 integer_ptr = &record->rsa_public_exponent;
609 break;
610
611 /* Dates */
612 case KM_TAG_ACTIVE_DATETIME:
613 integer_ptr = &record->active_date_time;
614 break;
615 case KM_TAG_ORIGINATION_EXPIRE_DATETIME:
616 integer_ptr = &record->origination_expire_date_time;
617 break;
618 case KM_TAG_USAGE_EXPIRE_DATETIME:
619 integer_ptr = &record->usage_expire_date_time;
620 break;
621 case KM_TAG_CREATION_DATETIME:
622 integer_ptr = &record->creation_date_time;
623 break;
624
625 /* Booleans */
626 case KM_TAG_NO_AUTH_REQUIRED:
627 bool_ptr = &record->no_auth_required;
628 break;
629 case KM_TAG_ALL_APPLICATIONS:
630 bool_ptr = &record->all_applications;
631 break;
632 case KM_TAG_ROLLBACK_RESISTANT:
633 bool_ptr = &record->rollback_resistant;
634 break;
635 case KM_TAG_ROLLBACK_RESISTANCE:
636 bool_ptr = &record->rollback_resistance;
637 break;
638 case KM_TAG_ALLOW_WHILE_ON_BODY:
639 bool_ptr = &record->allow_while_on_body;
640 break;
641 case KM_TAG_UNLOCKED_DEVICE_REQUIRED:
642 bool_ptr = &record->unlocked_device_required;
643 break;
644 case KM_TAG_CALLER_NONCE:
645 bool_ptr = &record->caller_nonce;
646 break;
647 case KM_TAG_TRUSTED_CONFIRMATION_REQUIRED:
648 bool_ptr = &record->trusted_confirmation_required;
649 break;
650 case KM_TAG_EARLY_BOOT_ONLY:
651 bool_ptr = &record->early_boot_only;
652 break;
653 case KM_TAG_DEVICE_UNIQUE_ATTESTATION:
654 bool_ptr = &record->device_unique_attestation;
655 break;
656 case KM_TAG_IDENTITY_CREDENTIAL_KEY:
657 bool_ptr = &record->identity_credential_key;
658 break;
659 case KM_TAG_TRUSTED_USER_PRESENCE_REQUIRED:
660 bool_ptr = &record->trusted_user_presence_required;
661 break;
662
663 /* Byte arrays*/
664 case KM_TAG_ATTESTATION_APPLICATION_ID:
665 string_ptr = &record->attestation_application_id;
666 break;
667 case KM_TAG_ATTESTATION_ID_BRAND:
668 string_ptr = &record->attestation_id_brand;
669 break;
670 case KM_TAG_ATTESTATION_ID_DEVICE:
671 string_ptr = &record->attestation_id_device;
672 break;
673 case KM_TAG_ATTESTATION_ID_PRODUCT:
674 string_ptr = &record->attestation_id_product;
675 break;
676 case KM_TAG_ATTESTATION_ID_SERIAL:
677 string_ptr = &record->attestation_id_serial;
678 break;
679 case KM_TAG_ATTESTATION_ID_IMEI:
680 string_ptr = &record->attestation_id_imei;
681 break;
682 case KM_TAG_ATTESTATION_ID_MEID:
683 string_ptr = &record->attestation_id_meid;
684 break;
685 case KM_TAG_ATTESTATION_ID_MANUFACTURER:
686 string_ptr = &record->attestation_id_manufacturer;
687 break;
688 case KM_TAG_ATTESTATION_ID_MODEL:
689 string_ptr = &record->attestation_id_model;
690 break;
691 }
692
693 keymaster_tag_type_t type = keymaster_tag_get_type(entry.tag);
694 switch (type) {
695 case KM_ENUM:
696 case KM_ENUM_REP:
697 case KM_UINT:
698 case KM_UINT_REP: {
699 ASSERT_OR_RETURN_ERROR((keymaster_tag_repeatable(entry.tag) && integer_set) ||
700 (!keymaster_tag_repeatable(entry.tag) && integer_ptr),
701 KM_ERROR_INVALID_TAG);
702
703 UniquePtr<ASN1_INTEGER, ASN1_INTEGER_Delete> value(ASN1_INTEGER_new());
704 if (!value.get()) {
705 return KM_ERROR_MEMORY_ALLOCATION_FAILED;
706 }
707 if (!ASN1_INTEGER_set(value.get(), get_uint32_value(entry))) {
708 return TranslateLastOpenSslError();
709 }
710
711 insert_integer(value.release(), integer_ptr, integer_set);
712 break;
713 }
714
715 case KM_ULONG:
716 case KM_ULONG_REP:
717 case KM_DATE: {
718 ASSERT_OR_RETURN_ERROR((keymaster_tag_repeatable(entry.tag) && integer_set) ||
719 (!keymaster_tag_repeatable(entry.tag) && integer_ptr),
720 KM_ERROR_INVALID_TAG);
721
722 UniquePtr<BIGNUM, BIGNUM_Delete> bn_value(BN_new());
723 if (!bn_value.get()) {
724 return KM_ERROR_MEMORY_ALLOCATION_FAILED;
725 }
726
727 if (type == KM_DATE) {
728 if (!BN_set_u64(bn_value.get(), entry.date_time)) {
729 return TranslateLastOpenSslError();
730 }
731 } else {
732 if (!BN_set_u64(bn_value.get(), entry.long_integer)) {
733 return TranslateLastOpenSslError();
734 }
735 }
736
737 UniquePtr<ASN1_INTEGER, ASN1_INTEGER_Delete> value(
738 BN_to_ASN1_INTEGER(bn_value.get(), nullptr));
739 if (!value.get()) {
740 return KM_ERROR_MEMORY_ALLOCATION_FAILED;
741 }
742
743 insert_integer(value.release(), integer_ptr, integer_set);
744 break;
745 }
746
747 case KM_BOOL:
748 ASSERT_OR_RETURN_ERROR(bool_ptr, KM_ERROR_INVALID_TAG);
749 if (!*bool_ptr) *bool_ptr = ASN1_NULL_new();
750 if (!*bool_ptr) return KM_ERROR_MEMORY_ALLOCATION_FAILED;
751 break;
752
753 /* Byte arrays*/
754 case KM_BYTES:
755 ASSERT_OR_RETURN_ERROR(string_ptr, KM_ERROR_INVALID_TAG);
756 if (!*string_ptr) {
757 *string_ptr = ASN1_OCTET_STRING_new();
758 }
759 if (!*string_ptr) {
760 return KM_ERROR_MEMORY_ALLOCATION_FAILED;
761 }
762 if (!ASN1_OCTET_STRING_set(*string_ptr, entry.blob.data, entry.blob.data_length)) {
763 return TranslateLastOpenSslError();
764 }
765 break;
766
767 default:
768 return KM_ERROR_UNIMPLEMENTED;
769 }
770 }
771
772 keymaster_ec_curve_t ec_curve;
773 uint32_t key_size;
774 if (auth_list.Contains(TAG_ALGORITHM, KM_ALGORITHM_EC) && //
775 !auth_list.Contains(TAG_EC_CURVE) && //
776 auth_list.GetTagValue(TAG_KEY_SIZE, &key_size)) {
777 // This must be a keymaster1 key. It's an EC key with no curve. Insert the curve if we
778 // can unambiguously figure it out.
779
780 keymaster_error_t error = EcKeySizeToCurve(key_size, &ec_curve);
781 if (error != KM_ERROR_OK) return error;
782
783 UniquePtr<ASN1_INTEGER, ASN1_INTEGER_Delete> value(ASN1_INTEGER_new());
784 if (!value.get()) {
785 return KM_ERROR_MEMORY_ALLOCATION_FAILED;
786 }
787
788 if (!ASN1_INTEGER_set(value.get(), ec_curve)) {
789 return TranslateLastOpenSslError();
790 }
791
792 insert_integer(value.release(), &record->ec_curve, nullptr);
793 }
794
795 return KM_ERROR_OK;
796 }
797
798 // Construct a CBOR-encoded attestation record containing the values from sw_enforced
799 // and tee_enforced.
build_eat_record(const AuthorizationSet & attestation_params,AuthorizationSet sw_enforced,AuthorizationSet tee_enforced,const AttestationContext & context,std::vector<uint8_t> * eat_token)800 keymaster_error_t build_eat_record(const AuthorizationSet& attestation_params,
801 AuthorizationSet sw_enforced, AuthorizationSet tee_enforced,
802 const AttestationContext& context,
803 std::vector<uint8_t>* eat_token) {
804 ASSERT_OR_RETURN_ERROR(eat_token, KM_ERROR_UNEXPECTED_NULL_POINTER);
805
806 cppbor::Map eat_record;
807 switch (context.GetSecurityLevel()) {
808 case KM_SECURITY_LEVEL_SOFTWARE:
809 eat_record.add(EatClaim::SECURITY_LEVEL, get_uint32_value(EatSecurityLevel::UNRESTRICTED));
810 break;
811 case KM_SECURITY_LEVEL_TRUSTED_ENVIRONMENT:
812 eat_record.add(EatClaim::SECURITY_LEVEL,
813 get_uint32_value(EatSecurityLevel::SECURE_RESTRICTED));
814 break;
815 case KM_SECURITY_LEVEL_STRONGBOX:
816 eat_record.add(EatClaim::SECURITY_LEVEL, get_uint32_value(EatSecurityLevel::HARDWARE));
817 break;
818 default:
819 return KM_ERROR_UNKNOWN_ERROR;
820 }
821
822 keymaster_error_t error;
823 const AttestationContext::VerifiedBootParams* vb_params = context.GetVerifiedBootParams(&error);
824 if (error != KM_ERROR_OK) return error;
825
826 if (vb_params->verified_boot_key.data_length) {
827 eat_record.add(EatClaim::VERIFIED_BOOT_KEY, blob_to_bstr(vb_params->verified_boot_key));
828 }
829 if (vb_params->verified_boot_hash.data_length) {
830 eat_record.add(EatClaim::VERIFIED_BOOT_HASH, blob_to_bstr(vb_params->verified_boot_hash));
831 }
832 if (vb_params->device_locked) {
833 eat_record.add(EatClaim::DEVICE_LOCKED, vb_params->device_locked);
834 }
835
836 bool verified_or_self_signed = (vb_params->verified_boot_state == KM_VERIFIED_BOOT_VERIFIED ||
837 vb_params->verified_boot_state == KM_VERIFIED_BOOT_SELF_SIGNED);
838 auto eat_boot_state = cppbor::Array()
839 .add(verified_or_self_signed) // secure-boot-enabled
840 .add(verified_or_self_signed) // debug-disabled
841 .add(verified_or_self_signed) // debug-disabled-since-boot
842 .add(verified_or_self_signed) // debug-permanent-disable
843 .add(false); // debug-full-permanent-disable (no way to verify)
844 eat_record.add(EatClaim::BOOT_STATE, std::move(eat_boot_state));
845 eat_record.add(EatClaim::OFFICIAL_BUILD,
846 vb_params->verified_boot_state == KM_VERIFIED_BOOT_VERIFIED);
847
848 eat_record.add(EatClaim::ATTESTATION_VERSION,
849 version_to_attestation_version(context.GetKmVersion()));
850 eat_record.add(EatClaim::KEYMASTER_VERSION,
851 version_to_attestation_km_version(context.GetKmVersion()));
852
853 keymaster_blob_t attestation_challenge = {nullptr, 0};
854 if (!attestation_params.GetTagValue(TAG_ATTESTATION_CHALLENGE, &attestation_challenge)) {
855 return KM_ERROR_ATTESTATION_CHALLENGE_MISSING;
856 }
857
858 if (!is_valid_attestation_challenge(attestation_challenge)) {
859 return KM_ERROR_INVALID_INPUT_LENGTH;
860 }
861
862 eat_record.add(EatClaim::NONCE, blob_to_bstr(attestation_challenge));
863
864 keymaster_blob_t attestation_app_id;
865 if (!attestation_params.GetTagValue(TAG_ATTESTATION_APPLICATION_ID, &attestation_app_id)) {
866 return KM_ERROR_ATTESTATION_APPLICATION_ID_MISSING;
867 }
868 // TODO: what should happen when sw_enforced already contains TAG_ATTESTATION_APPLICATION_ID?
869 // (as is the case in android_keymaster_test.cpp). For now, we will ignore the provided one in
870 // attestation_params if that's the case.
871 keymaster_blob_t existing_app_id;
872 if (!sw_enforced.GetTagValue(TAG_ATTESTATION_APPLICATION_ID, &existing_app_id)) {
873 sw_enforced.push_back(TAG_ATTESTATION_APPLICATION_ID, attestation_app_id);
874 }
875
876 error = context.VerifyAndCopyDeviceIds(
877 attestation_params,
878 context.GetSecurityLevel() == KM_SECURITY_LEVEL_SOFTWARE ? &sw_enforced : &tee_enforced);
879 if (error == KM_ERROR_UNIMPLEMENTED) {
880 // The KeymasterContext implementation does not support device ID attestation. Bail out if
881 // device ID attestation is being attempted.
882 for (const auto& tag : kDeviceAttestationTags) {
883 if (attestation_params.find(tag) != -1) {
884 return KM_ERROR_CANNOT_ATTEST_IDS;
885 }
886 }
887 } else if (error != KM_ERROR_OK) {
888 return error;
889 }
890
891 if (attestation_params.Contains(TAG_DEVICE_UNIQUE_ATTESTATION) &&
892 context.GetSecurityLevel() == KM_SECURITY_LEVEL_STRONGBOX) {
893 eat_record.add(EatClaim::DEVICE_UNIQUE_ATTESTATION, true);
894 }
895
896 cppbor::Map software_submod;
897 error = build_eat_submod(sw_enforced, EatSecurityLevel::UNRESTRICTED, &software_submod);
898 if (error != KM_ERROR_OK) return error;
899
900 cppbor::Map tee_submod;
901 error = build_eat_submod(tee_enforced, EatSecurityLevel::SECURE_RESTRICTED, &tee_submod);
902 if (error != KM_ERROR_OK) return error;
903
904 if (software_submod.size() + tee_submod.size() > 0) {
905 cppbor::Map submods;
906 if (software_submod.size() > 0) {
907 submods.add(kEatSubmodNameSoftware, std::move(software_submod));
908 }
909 if (tee_submod.size() > 0) {
910 submods.add(kEatSubmodNameTee, std::move(tee_submod));
911 }
912
913 eat_record.add(EatClaim::SUBMODS, std::move(submods));
914 }
915
916 if (attestation_params.GetTagValue(TAG_INCLUDE_UNIQUE_ID)) {
917 uint64_t creation_datetime;
918 // Only check sw_enforced for TAG_CREATION_DATETIME, since it shouldn't be in tee_enforced,
919 // since this implementation has no secure wall clock.
920 if (!sw_enforced.GetTagValue(TAG_CREATION_DATETIME, &creation_datetime)) {
921 LOG_E("Unique ID cannot be created without creation datetime", 0);
922 return KM_ERROR_INVALID_KEY_BLOB;
923 }
924
925 Buffer unique_id = context.GenerateUniqueId(
926 creation_datetime, attestation_app_id,
927 attestation_params.GetTagValue(TAG_RESET_SINCE_ID_ROTATION), &error);
928 if (error != KM_ERROR_OK) return error;
929
930 eat_record.add(EatClaim::CTI,
931 cppbor::Bstr(std::pair(unique_id.begin(), unique_id.available_read())));
932 }
933
934 *eat_token = eat_record.encode();
935
936 return KM_ERROR_OK;
937 }
938
build_unique_id_input(uint64_t creation_date_time,const keymaster_blob_t & application_id,bool reset_since_rotation)939 std::vector<uint8_t> build_unique_id_input(uint64_t creation_date_time,
940 const keymaster_blob_t& application_id,
941 bool reset_since_rotation) {
942 uint64_t rounded_date = creation_date_time / 2592000000LLU;
943 uint8_t* serialized_date = reinterpret_cast<uint8_t*>(&rounded_date);
944
945 std::vector<uint8_t> input;
946 input.insert(input.end(), serialized_date, serialized_date + sizeof(rounded_date));
947 input.insert(input.end(), application_id.data,
948 application_id.data + application_id.data_length);
949 input.push_back(reset_since_rotation ? 1 : 0);
950 return input;
951 }
952
generate_unique_id(const std::vector<uint8_t> & hbk,uint64_t creation_date_time,const keymaster_blob_t & application_id,bool reset_since_rotation)953 Buffer generate_unique_id(const std::vector<uint8_t>& hbk, uint64_t creation_date_time,
954 const keymaster_blob_t& application_id, bool reset_since_rotation) {
955 HmacSha256 hmac;
956 hmac.Init(hbk.data(), hbk.size());
957
958 std::vector<uint8_t> input =
959 build_unique_id_input(creation_date_time, application_id, reset_since_rotation);
960 Buffer unique_id(UNIQUE_ID_SIZE);
961 hmac.Sign(input.data(), input.size(), unique_id.peek_write(), unique_id.available_write());
962 unique_id.advance_write(UNIQUE_ID_SIZE);
963 return unique_id;
964 }
965
966 // Construct an ASN1.1 DER-encoded attestation record containing the values from sw_enforced and
967 // tee_enforced.
build_attestation_record(const AuthorizationSet & attestation_params,AuthorizationSet sw_enforced,AuthorizationSet tee_enforced,const AttestationContext & context,UniquePtr<uint8_t[]> * asn1_key_desc,size_t * asn1_key_desc_len)968 keymaster_error_t build_attestation_record(const AuthorizationSet& attestation_params, //
969 AuthorizationSet sw_enforced,
970 AuthorizationSet tee_enforced,
971 const AttestationContext& context,
972 UniquePtr<uint8_t[]>* asn1_key_desc,
973 size_t* asn1_key_desc_len) {
974 ASSERT_OR_RETURN_ERROR(asn1_key_desc && asn1_key_desc_len, KM_ERROR_UNEXPECTED_NULL_POINTER);
975
976 UniquePtr<KM_KEY_DESCRIPTION, KM_KEY_DESCRIPTION_Delete> key_desc(KM_KEY_DESCRIPTION_new());
977 if (!key_desc.get()) return KM_ERROR_MEMORY_ALLOCATION_FAILED;
978
979 KM_ROOT_OF_TRUST* root_of_trust = nullptr;
980 if (context.GetSecurityLevel() == KM_SECURITY_LEVEL_SOFTWARE) {
981 key_desc->software_enforced->root_of_trust = KM_ROOT_OF_TRUST_new();
982 root_of_trust = key_desc->software_enforced->root_of_trust;
983 } else {
984 key_desc->tee_enforced->root_of_trust = KM_ROOT_OF_TRUST_new();
985 root_of_trust = key_desc->tee_enforced->root_of_trust;
986 }
987
988 keymaster_error_t error;
989 auto vb_params = context.GetVerifiedBootParams(&error);
990 if (error != KM_ERROR_OK) return error;
991 if (vb_params->verified_boot_key.data_length &&
992 !ASN1_OCTET_STRING_set(root_of_trust->verified_boot_key, vb_params->verified_boot_key.data,
993 vb_params->verified_boot_key.data_length)) {
994 return TranslateLastOpenSslError();
995 }
996 if (vb_params->verified_boot_hash.data_length &&
997 !ASN1_OCTET_STRING_set(root_of_trust->verified_boot_hash,
998 vb_params->verified_boot_hash.data,
999 vb_params->verified_boot_hash.data_length)) {
1000 return TranslateLastOpenSslError();
1001 }
1002
1003 root_of_trust->device_locked = vb_params->device_locked ? 0xFF : 0x00;
1004 if (!ASN1_ENUMERATED_set(root_of_trust->verified_boot_state, vb_params->verified_boot_state)) {
1005 return TranslateLastOpenSslError();
1006 }
1007
1008 if (!ASN1_INTEGER_set(key_desc->attestation_version,
1009 version_to_attestation_version(context.GetKmVersion())) ||
1010 !ASN1_ENUMERATED_set(key_desc->attestation_security_level, context.GetSecurityLevel()) ||
1011 !ASN1_INTEGER_set(key_desc->keymaster_version,
1012 version_to_attestation_km_version(context.GetKmVersion())) ||
1013 !ASN1_ENUMERATED_set(key_desc->keymaster_security_level, context.GetSecurityLevel())) {
1014 return TranslateLastOpenSslError();
1015 }
1016
1017 keymaster_blob_t attestation_challenge = {nullptr, 0};
1018 if (!attestation_params.GetTagValue(TAG_ATTESTATION_CHALLENGE, &attestation_challenge)) {
1019 return KM_ERROR_ATTESTATION_CHALLENGE_MISSING;
1020 }
1021
1022 if (!is_valid_attestation_challenge(attestation_challenge)) {
1023 return KM_ERROR_INVALID_INPUT_LENGTH;
1024 }
1025
1026 if (!ASN1_OCTET_STRING_set(key_desc->attestation_challenge, attestation_challenge.data,
1027 attestation_challenge.data_length)) {
1028 return TranslateLastOpenSslError();
1029 }
1030
1031 keymaster_blob_t attestation_app_id;
1032 if (!attestation_params.GetTagValue(TAG_ATTESTATION_APPLICATION_ID, &attestation_app_id)) {
1033 return KM_ERROR_ATTESTATION_APPLICATION_ID_MISSING;
1034 }
1035 sw_enforced.push_back(TAG_ATTESTATION_APPLICATION_ID, attestation_app_id);
1036
1037 error = context.VerifyAndCopyDeviceIds(
1038 attestation_params,
1039 context.GetSecurityLevel() == KM_SECURITY_LEVEL_SOFTWARE ? &sw_enforced : &tee_enforced);
1040 if (error == KM_ERROR_UNIMPLEMENTED) {
1041 // The KeymasterContext implementation does not support device ID attestation. Bail out if
1042 // device ID attestation is being attempted.
1043 for (const auto& tag : kDeviceAttestationTags) {
1044 if (attestation_params.find(tag) != -1) {
1045 return KM_ERROR_CANNOT_ATTEST_IDS;
1046 }
1047 }
1048 } else if (error != KM_ERROR_OK) {
1049 return error;
1050 }
1051
1052 if (attestation_params.Contains(TAG_DEVICE_UNIQUE_ATTESTATION) &&
1053 context.GetSecurityLevel() == KM_SECURITY_LEVEL_STRONGBOX) {
1054 tee_enforced.push_back(TAG_DEVICE_UNIQUE_ATTESTATION);
1055 };
1056
1057 error = build_auth_list(sw_enforced, key_desc->software_enforced);
1058 if (error != KM_ERROR_OK) return error;
1059
1060 error = build_auth_list(tee_enforced, key_desc->tee_enforced);
1061 if (error != KM_ERROR_OK) return error;
1062
1063 if (attestation_params.GetTagValue(TAG_INCLUDE_UNIQUE_ID)) {
1064 uint64_t creation_datetime;
1065 // Only check sw_enforced for TAG_CREATION_DATETIME, since it shouldn't be in tee_enforced,
1066 // since this implementation has no secure wall clock.
1067 if (!sw_enforced.GetTagValue(TAG_CREATION_DATETIME, &creation_datetime)) {
1068 LOG_E("Unique ID cannot be created without creation datetime", 0);
1069 return KM_ERROR_INVALID_KEY_BLOB;
1070 }
1071
1072 Buffer unique_id = context.GenerateUniqueId(
1073 creation_datetime, attestation_app_id,
1074 attestation_params.GetTagValue(TAG_RESET_SINCE_ID_ROTATION), &error);
1075 if (error != KM_ERROR_OK) return error;
1076
1077 key_desc->unique_id = ASN1_OCTET_STRING_new();
1078 if (!key_desc->unique_id ||
1079 !ASN1_OCTET_STRING_set(key_desc->unique_id, unique_id.peek_read(),
1080 unique_id.available_read()))
1081 return TranslateLastOpenSslError();
1082 }
1083
1084 int len = i2d_KM_KEY_DESCRIPTION(key_desc.get(), nullptr);
1085 if (len < 0) return TranslateLastOpenSslError();
1086 *asn1_key_desc_len = len;
1087 asn1_key_desc->reset(new (std::nothrow) uint8_t[*asn1_key_desc_len]);
1088 if (!asn1_key_desc->get()) return KM_ERROR_MEMORY_ALLOCATION_FAILED;
1089 uint8_t* p = asn1_key_desc->get();
1090 len = i2d_KM_KEY_DESCRIPTION(key_desc.get(), &p);
1091 if (len < 0) return TranslateLastOpenSslError();
1092
1093 return KM_ERROR_OK;
1094 }
1095
1096 // Copy all enumerated values with the specified tag from stack to auth_list.
get_repeated_enums(const ASN1_INTEGER_SET * stack,keymaster_tag_t tag,AuthorizationSet * auth_list)1097 static bool get_repeated_enums(const ASN1_INTEGER_SET* stack, keymaster_tag_t tag,
1098 AuthorizationSet* auth_list) {
1099 ASSERT_OR_RETURN_ERROR(keymaster_tag_get_type(tag) == KM_ENUM_REP, KM_ERROR_INVALID_TAG);
1100 for (size_t i = 0; i < sk_ASN1_INTEGER_num(stack); ++i) {
1101 if (!auth_list->push_back(
1102 keymaster_param_enum(tag, ASN1_INTEGER_get(sk_ASN1_INTEGER_value(stack, i)))))
1103 return false;
1104 }
1105 return true;
1106 }
1107
1108 // Add the specified integer tag/value pair to auth_list.
1109 template <keymaster_tag_type_t Type, keymaster_tag_t Tag, typename KeymasterEnum>
get_enum(const ASN1_INTEGER * asn1_int,TypedEnumTag<Type,Tag,KeymasterEnum> tag,AuthorizationSet * auth_list)1110 static bool get_enum(const ASN1_INTEGER* asn1_int, TypedEnumTag<Type, Tag, KeymasterEnum> tag,
1111 AuthorizationSet* auth_list) {
1112 if (!asn1_int) return true;
1113 return auth_list->push_back(tag, static_cast<KeymasterEnum>(ASN1_INTEGER_get(asn1_int)));
1114 }
1115
1116 // Add the specified ulong tag/value pair to auth_list.
get_ulong(const ASN1_INTEGER * asn1_int,keymaster_tag_t tag,AuthorizationSet * auth_list)1117 static bool get_ulong(const ASN1_INTEGER* asn1_int, keymaster_tag_t tag,
1118 AuthorizationSet* auth_list) {
1119 if (!asn1_int) return true;
1120 UniquePtr<BIGNUM, BIGNUM_Delete> bn(ASN1_INTEGER_to_BN(asn1_int, nullptr));
1121 if (!bn.get()) return false;
1122 uint64_t ulong = 0;
1123 BN_get_u64(bn.get(), &ulong);
1124 return auth_list->push_back(keymaster_param_long(tag, ulong));
1125 }
1126
1127 // 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)1128 keymaster_error_t extract_auth_list(const KM_AUTH_LIST* record, AuthorizationSet* auth_list) {
1129 if (!record) return KM_ERROR_OK;
1130
1131 // Purpose
1132 if (!get_repeated_enums(record->purpose, TAG_PURPOSE, auth_list)) {
1133 return KM_ERROR_MEMORY_ALLOCATION_FAILED;
1134 }
1135
1136 // Algorithm
1137 if (!get_enum(record->algorithm, TAG_ALGORITHM, auth_list)) {
1138 return KM_ERROR_MEMORY_ALLOCATION_FAILED;
1139 }
1140
1141 // Key size
1142 if (record->key_size &&
1143 !auth_list->push_back(TAG_KEY_SIZE, ASN1_INTEGER_get(record->key_size))) {
1144 return KM_ERROR_MEMORY_ALLOCATION_FAILED;
1145 }
1146
1147 // Block mode
1148 if (!get_repeated_enums(record->block_mode, TAG_BLOCK_MODE, auth_list)) {
1149 return KM_ERROR_MEMORY_ALLOCATION_FAILED;
1150 }
1151
1152 // Digest
1153 if (!get_repeated_enums(record->digest, TAG_DIGEST, auth_list)) {
1154 return KM_ERROR_MEMORY_ALLOCATION_FAILED;
1155 }
1156
1157 // Padding
1158 if (!get_repeated_enums(record->padding, TAG_PADDING, auth_list)) {
1159 return KM_ERROR_MEMORY_ALLOCATION_FAILED;
1160 }
1161
1162 // Caller nonce
1163 if (record->caller_nonce && !auth_list->push_back(TAG_CALLER_NONCE)) {
1164 return KM_ERROR_MEMORY_ALLOCATION_FAILED;
1165 }
1166
1167 // Min mac length
1168 if (!get_ulong(record->min_mac_length, TAG_MIN_MAC_LENGTH, auth_list)) {
1169 return KM_ERROR_MEMORY_ALLOCATION_FAILED;
1170 }
1171
1172 // EC curve
1173 if (!get_enum(record->ec_curve, TAG_EC_CURVE, auth_list)) {
1174 return KM_ERROR_MEMORY_ALLOCATION_FAILED;
1175 }
1176
1177 // RSA public exponent
1178 if (!get_ulong(record->rsa_public_exponent, TAG_RSA_PUBLIC_EXPONENT, auth_list)) {
1179 return KM_ERROR_MEMORY_ALLOCATION_FAILED;
1180 }
1181
1182 // Rsa Oaep Mgf Digest
1183 if (!get_repeated_enums(record->mgf_digest, TAG_RSA_OAEP_MGF_DIGEST, auth_list)) {
1184 return KM_ERROR_MEMORY_ALLOCATION_FAILED;
1185 }
1186
1187 // Rollback resistance
1188 if (record->rollback_resistance && !auth_list->push_back(TAG_ROLLBACK_RESISTANCE)) {
1189 return KM_ERROR_MEMORY_ALLOCATION_FAILED;
1190 }
1191
1192 // Early boot only
1193 if (record->early_boot_only && !auth_list->push_back(TAG_EARLY_BOOT_ONLY)) {
1194 return KM_ERROR_MEMORY_ALLOCATION_FAILED;
1195 }
1196
1197 // Active date time
1198 if (!get_ulong(record->active_date_time, TAG_ACTIVE_DATETIME, auth_list)) {
1199 return KM_ERROR_MEMORY_ALLOCATION_FAILED;
1200 }
1201
1202 // Origination expire date time
1203 if (!get_ulong(record->origination_expire_date_time, TAG_ORIGINATION_EXPIRE_DATETIME,
1204 auth_list)) {
1205 return KM_ERROR_MEMORY_ALLOCATION_FAILED;
1206 }
1207
1208 // Usage Expire date time
1209 if (!get_ulong(record->usage_expire_date_time, TAG_USAGE_EXPIRE_DATETIME, auth_list)) {
1210 return KM_ERROR_MEMORY_ALLOCATION_FAILED;
1211 }
1212
1213 // Usage count limit
1214 if (record->usage_count_limit &&
1215 !auth_list->push_back(TAG_USAGE_COUNT_LIMIT, ASN1_INTEGER_get(record->usage_count_limit))) {
1216 return KM_ERROR_MEMORY_ALLOCATION_FAILED;
1217 }
1218
1219 // No auth required
1220 if (record->no_auth_required && !auth_list->push_back(TAG_NO_AUTH_REQUIRED)) {
1221 return KM_ERROR_MEMORY_ALLOCATION_FAILED;
1222 }
1223
1224 // User auth type
1225 if (!get_enum(record->user_auth_type, TAG_USER_AUTH_TYPE, auth_list)) {
1226 return KM_ERROR_MEMORY_ALLOCATION_FAILED;
1227 }
1228
1229 // Auth timeout
1230 if (record->auth_timeout &&
1231 !auth_list->push_back(TAG_AUTH_TIMEOUT, ASN1_INTEGER_get(record->auth_timeout))) {
1232 return KM_ERROR_MEMORY_ALLOCATION_FAILED;
1233 }
1234
1235 // Allow while on body
1236 if (record->allow_while_on_body && !auth_list->push_back(TAG_ALLOW_WHILE_ON_BODY)) {
1237 return KM_ERROR_MEMORY_ALLOCATION_FAILED;
1238 }
1239
1240 // trusted user presence required
1241 if (record->trusted_user_presence_required &&
1242 !auth_list->push_back(TAG_TRUSTED_USER_PRESENCE_REQUIRED)) {
1243 return KM_ERROR_MEMORY_ALLOCATION_FAILED;
1244 }
1245
1246 // trusted confirmation required
1247 if (record->trusted_confirmation_required &&
1248 !auth_list->push_back(TAG_TRUSTED_CONFIRMATION_REQUIRED)) {
1249 return KM_ERROR_MEMORY_ALLOCATION_FAILED;
1250 }
1251
1252 // Unlocked device required
1253 if (record->unlocked_device_required && !auth_list->push_back(TAG_UNLOCKED_DEVICE_REQUIRED)) {
1254 return KM_ERROR_MEMORY_ALLOCATION_FAILED;
1255 }
1256
1257 // All applications
1258 if (record->all_applications && !auth_list->push_back(TAG_ALL_APPLICATIONS)) {
1259 return KM_ERROR_MEMORY_ALLOCATION_FAILED;
1260 }
1261
1262 // Application ID
1263 if (record->application_id &&
1264 !auth_list->push_back(TAG_APPLICATION_ID, record->application_id->data,
1265 record->application_id->length)) {
1266 return KM_ERROR_MEMORY_ALLOCATION_FAILED;
1267 }
1268
1269 // Creation date time
1270 if (!get_ulong(record->creation_date_time, TAG_CREATION_DATETIME, auth_list)) {
1271 return KM_ERROR_MEMORY_ALLOCATION_FAILED;
1272 }
1273
1274 // Origin
1275 if (!get_enum(record->origin, TAG_ORIGIN, auth_list)) {
1276 return KM_ERROR_MEMORY_ALLOCATION_FAILED;
1277 }
1278
1279 // Rollback resistant
1280 if (record->rollback_resistant && !auth_list->push_back(TAG_ROLLBACK_RESISTANT)) {
1281 return KM_ERROR_MEMORY_ALLOCATION_FAILED;
1282 }
1283
1284 // Root of trust
1285 if (record->root_of_trust) {
1286 KM_ROOT_OF_TRUST* rot = record->root_of_trust;
1287 if (!rot->verified_boot_key) return KM_ERROR_INVALID_KEY_BLOB;
1288
1289 // Other root of trust fields are not mapped to auth set entries.
1290 }
1291
1292 // OS Version
1293 if (record->os_version &&
1294 !auth_list->push_back(TAG_OS_VERSION, ASN1_INTEGER_get(record->os_version))) {
1295 return KM_ERROR_MEMORY_ALLOCATION_FAILED;
1296 }
1297
1298 // OS Patch level
1299 if (record->os_patchlevel &&
1300 !auth_list->push_back(TAG_OS_PATCHLEVEL, ASN1_INTEGER_get(record->os_patchlevel))) {
1301 return KM_ERROR_MEMORY_ALLOCATION_FAILED;
1302 }
1303
1304 // attestation application Id
1305 if (record->attestation_application_id &&
1306 !auth_list->push_back(TAG_ATTESTATION_APPLICATION_ID,
1307 record->attestation_application_id->data,
1308 record->attestation_application_id->length)) {
1309 return KM_ERROR_MEMORY_ALLOCATION_FAILED;
1310 }
1311
1312 // Brand name
1313 if (record->attestation_id_brand &&
1314 !auth_list->push_back(TAG_ATTESTATION_ID_BRAND, record->attestation_id_brand->data,
1315 record->attestation_id_brand->length)) {
1316 return KM_ERROR_MEMORY_ALLOCATION_FAILED;
1317 }
1318
1319 // Device name
1320 if (record->attestation_id_device &&
1321 !auth_list->push_back(TAG_ATTESTATION_ID_DEVICE, record->attestation_id_device->data,
1322 record->attestation_id_device->length)) {
1323 return KM_ERROR_MEMORY_ALLOCATION_FAILED;
1324 }
1325
1326 // Product name
1327 if (record->attestation_id_product &&
1328 !auth_list->push_back(TAG_ATTESTATION_ID_PRODUCT, record->attestation_id_product->data,
1329 record->attestation_id_product->length)) {
1330 return KM_ERROR_MEMORY_ALLOCATION_FAILED;
1331 }
1332
1333 // Serial number
1334 if (record->attestation_id_serial &&
1335 !auth_list->push_back(TAG_ATTESTATION_ID_SERIAL, record->attestation_id_serial->data,
1336 record->attestation_id_serial->length)) {
1337 return KM_ERROR_MEMORY_ALLOCATION_FAILED;
1338 }
1339
1340 // IMEI
1341 if (record->attestation_id_imei &&
1342 !auth_list->push_back(TAG_ATTESTATION_ID_IMEI, record->attestation_id_imei->data,
1343 record->attestation_id_imei->length)) {
1344 return KM_ERROR_MEMORY_ALLOCATION_FAILED;
1345 }
1346
1347 // MEID
1348 if (record->attestation_id_meid &&
1349 !auth_list->push_back(TAG_ATTESTATION_ID_MEID, record->attestation_id_meid->data,
1350 record->attestation_id_meid->length)) {
1351 return KM_ERROR_MEMORY_ALLOCATION_FAILED;
1352 }
1353
1354 // Manufacturer name
1355 if (record->attestation_id_manufacturer &&
1356 !auth_list->push_back(TAG_ATTESTATION_ID_MANUFACTURER,
1357 record->attestation_id_manufacturer->data,
1358 record->attestation_id_manufacturer->length)) {
1359 return KM_ERROR_MEMORY_ALLOCATION_FAILED;
1360 }
1361
1362 // Model name
1363 if (record->attestation_id_model &&
1364 !auth_list->push_back(TAG_ATTESTATION_ID_MODEL, record->attestation_id_model->data,
1365 record->attestation_id_model->length)) {
1366 return KM_ERROR_MEMORY_ALLOCATION_FAILED;
1367 }
1368
1369 // vendor patch level
1370 if (record->vendor_patchlevel &&
1371 !auth_list->push_back(TAG_VENDOR_PATCHLEVEL, ASN1_INTEGER_get(record->vendor_patchlevel))) {
1372 return KM_ERROR_MEMORY_ALLOCATION_FAILED;
1373 }
1374
1375 // boot patch level
1376 if (record->boot_patch_level &&
1377 !auth_list->push_back(TAG_BOOT_PATCHLEVEL, ASN1_INTEGER_get(record->boot_patch_level))) {
1378 return KM_ERROR_MEMORY_ALLOCATION_FAILED;
1379 }
1380
1381 // device unique attestation
1382 if (record->device_unique_attestation && !auth_list->push_back(TAG_DEVICE_UNIQUE_ATTESTATION)) {
1383 return KM_ERROR_MEMORY_ALLOCATION_FAILED;
1384 }
1385
1386 // identity credential key
1387 if (record->identity_credential_key && !auth_list->push_back(TAG_IDENTITY_CREDENTIAL_KEY)) {
1388 return KM_ERROR_MEMORY_ALLOCATION_FAILED;
1389 }
1390
1391 return KM_ERROR_OK;
1392 }
1393
1394 // Parse the DER-encoded attestation record, placing the results in keymaster_version,
1395 // attestation_challenge, software_enforced, tee_enforced and unique_id.
parse_attestation_record(const uint8_t * asn1_key_desc,size_t asn1_key_desc_len,uint32_t * attestation_version,keymaster_security_level_t * attestation_security_level,uint32_t * keymaster_version,keymaster_security_level_t * keymaster_security_level,keymaster_blob_t * attestation_challenge,AuthorizationSet * software_enforced,AuthorizationSet * tee_enforced,keymaster_blob_t * unique_id)1396 keymaster_error_t parse_attestation_record(const uint8_t* asn1_key_desc, size_t asn1_key_desc_len,
1397 uint32_t* attestation_version, //
1398 keymaster_security_level_t* attestation_security_level,
1399 uint32_t* keymaster_version,
1400 keymaster_security_level_t* keymaster_security_level,
1401 keymaster_blob_t* attestation_challenge,
1402 AuthorizationSet* software_enforced,
1403 AuthorizationSet* tee_enforced,
1404 keymaster_blob_t* unique_id) {
1405 const uint8_t* p = asn1_key_desc;
1406 UniquePtr<KM_KEY_DESCRIPTION, KM_KEY_DESCRIPTION_Delete> record(
1407 d2i_KM_KEY_DESCRIPTION(nullptr, &p, asn1_key_desc_len));
1408 if (!record.get()) return TranslateLastOpenSslError();
1409
1410 *attestation_version = ASN1_INTEGER_get(record->attestation_version);
1411 *attestation_security_level = static_cast<keymaster_security_level_t>(
1412 ASN1_ENUMERATED_get(record->attestation_security_level));
1413 *keymaster_version = ASN1_INTEGER_get(record->keymaster_version);
1414 *keymaster_security_level = static_cast<keymaster_security_level_t>(
1415 ASN1_ENUMERATED_get(record->keymaster_security_level));
1416
1417 attestation_challenge->data =
1418 dup_buffer(record->attestation_challenge->data, record->attestation_challenge->length);
1419 attestation_challenge->data_length = record->attestation_challenge->length;
1420
1421 unique_id->data = dup_buffer(record->unique_id->data, record->unique_id->length);
1422 unique_id->data_length = record->unique_id->length;
1423
1424 keymaster_error_t error = extract_auth_list(record->software_enforced, software_enforced);
1425 if (error != KM_ERROR_OK) return error;
1426
1427 return extract_auth_list(record->tee_enforced, tee_enforced);
1428 }
1429
parse_root_of_trust(const uint8_t * asn1_key_desc,size_t asn1_key_desc_len,keymaster_blob_t * verified_boot_key,keymaster_verified_boot_t * verified_boot_state,bool * device_locked)1430 keymaster_error_t parse_root_of_trust(const uint8_t* asn1_key_desc, size_t asn1_key_desc_len,
1431 keymaster_blob_t* verified_boot_key,
1432 keymaster_verified_boot_t* verified_boot_state,
1433 bool* device_locked) {
1434 const uint8_t* p = asn1_key_desc;
1435 UniquePtr<KM_KEY_DESCRIPTION, KM_KEY_DESCRIPTION_Delete> record(
1436 d2i_KM_KEY_DESCRIPTION(nullptr, &p, asn1_key_desc_len));
1437 if (!record.get()) {
1438 return TranslateLastOpenSslError();
1439 }
1440 if (!record->tee_enforced) {
1441 return KM_ERROR_INVALID_ARGUMENT;
1442 }
1443 if (!record->tee_enforced->root_of_trust) {
1444 return KM_ERROR_INVALID_ARGUMENT;
1445 }
1446 if (!record->tee_enforced->root_of_trust->verified_boot_key) {
1447 return KM_ERROR_INVALID_ARGUMENT;
1448 }
1449 KM_ROOT_OF_TRUST* root_of_trust = record->tee_enforced->root_of_trust;
1450 verified_boot_key->data = dup_buffer(root_of_trust->verified_boot_key->data,
1451 root_of_trust->verified_boot_key->length);
1452 verified_boot_key->data_length = root_of_trust->verified_boot_key->length;
1453 *verified_boot_state = static_cast<keymaster_verified_boot_t>(
1454 ASN1_ENUMERATED_get(root_of_trust->verified_boot_state));
1455 *device_locked = root_of_trust->device_locked;
1456 return KM_ERROR_OK;
1457 }
1458
1459 // Parse the EAT-encoded attestation record, placing the results in keymaster_version,
1460 // attestation_challenge, software_enforced, tee_enforced and unique_id.
parse_eat_record(const uint8_t * eat_key_desc,size_t eat_key_desc_len,uint32_t * attestation_version,keymaster_security_level_t * attestation_security_level,uint32_t * keymaster_version,keymaster_security_level_t * keymaster_security_level,keymaster_blob_t * attestation_challenge,AuthorizationSet * software_enforced,AuthorizationSet * tee_enforced,keymaster_blob_t * unique_id,keymaster_blob_t * verified_boot_key,keymaster_verified_boot_t * verified_boot_state,bool * device_locked,std::vector<int64_t> * unexpected_claims)1461 keymaster_error_t parse_eat_record(
1462 const uint8_t* eat_key_desc, size_t eat_key_desc_len, uint32_t* attestation_version,
1463 keymaster_security_level_t* attestation_security_level, uint32_t* keymaster_version,
1464 keymaster_security_level_t* keymaster_security_level, keymaster_blob_t* attestation_challenge,
1465 AuthorizationSet* software_enforced, AuthorizationSet* tee_enforced,
1466 keymaster_blob_t* unique_id, keymaster_blob_t* verified_boot_key,
1467 keymaster_verified_boot_t* verified_boot_state, bool* device_locked,
1468 std::vector<int64_t>* unexpected_claims) {
1469 auto [top_level_item, next_pos, error] = cppbor::parse(eat_key_desc, eat_key_desc_len);
1470 ASSERT_OR_RETURN_ERROR(top_level_item, KM_ERROR_INVALID_TAG);
1471 const cppbor::Map* eat_map = top_level_item->asMap();
1472 ASSERT_OR_RETURN_ERROR(eat_map, KM_ERROR_INVALID_TAG);
1473 bool verified_or_self_signed = false;
1474
1475 for (size_t i = 0; i < eat_map->size(); i++) {
1476 auto& [key_item, value_item] = (*eat_map)[i];
1477 const cppbor::Int* key = key_item->asInt();
1478 ASSERT_OR_RETURN_ERROR(key, (KM_ERROR_INVALID_TAG));
1479
1480 // The following values will either hold the typed value, or be null (if not the right
1481 // type).
1482 const cppbor::Int* int_value = value_item->asInt();
1483 const cppbor::Bstr* bstr_value = value_item->asBstr();
1484 const cppbor::Simple* simple_value = value_item->asSimple();
1485 const cppbor::Array* array_value = value_item->asArray();
1486 const cppbor::Map* map_value = value_item->asMap();
1487
1488 keymaster_error_t error;
1489 switch ((EatClaim)key->value()) {
1490 default:
1491 unexpected_claims->push_back(key->value());
1492 break;
1493 case EatClaim::ATTESTATION_VERSION:
1494 ASSERT_OR_RETURN_ERROR(int_value, KM_ERROR_INVALID_TAG);
1495 *attestation_version = int_value->value();
1496 break;
1497 case EatClaim::SECURITY_LEVEL:
1498 ASSERT_OR_RETURN_ERROR(int_value, KM_ERROR_INVALID_TAG);
1499 switch ((EatSecurityLevel)int_value->value()) {
1500 // TODO: Is my assumption correct that the security level of the attestation data should
1501 // always be equal to the security level of keymint, as the attestation data always
1502 // lives in the top-level module?
1503 case EatSecurityLevel::UNRESTRICTED:
1504 *keymaster_security_level = *attestation_security_level =
1505 KM_SECURITY_LEVEL_SOFTWARE;
1506 break;
1507 case EatSecurityLevel::SECURE_RESTRICTED:
1508 *keymaster_security_level = *attestation_security_level =
1509 KM_SECURITY_LEVEL_TRUSTED_ENVIRONMENT;
1510 break;
1511 case EatSecurityLevel::HARDWARE:
1512 *keymaster_security_level = *attestation_security_level =
1513 KM_SECURITY_LEVEL_STRONGBOX;
1514 break;
1515 default:
1516 return KM_ERROR_INVALID_TAG;
1517 }
1518 break;
1519 case EatClaim::KEYMASTER_VERSION:
1520 ASSERT_OR_RETURN_ERROR(int_value, KM_ERROR_INVALID_TAG);
1521 *keymaster_version = int_value->value();
1522 break;
1523 case EatClaim::SUBMODS:
1524 ASSERT_OR_RETURN_ERROR(map_value, KM_ERROR_INVALID_TAG);
1525 for (size_t j = 0; j < map_value->size(); j++) {
1526 auto& [submod_key, submod_value] = (*map_value)[j];
1527 const cppbor::Map* submod_map = submod_value->asMap();
1528 ASSERT_OR_RETURN_ERROR(submod_map, KM_ERROR_INVALID_TAG);
1529 error = parse_eat_submod(submod_map, software_enforced, tee_enforced);
1530 if (error != KM_ERROR_OK) return error;
1531 }
1532 break;
1533 case EatClaim::CTI:
1534 error = bstr_to_blob(bstr_value, unique_id);
1535 if (error != KM_ERROR_OK) return error;
1536 break;
1537 case EatClaim::NONCE:
1538 error = bstr_to_blob(bstr_value, attestation_challenge);
1539 if (error != KM_ERROR_OK) return error;
1540 break;
1541 case EatClaim::VERIFIED_BOOT_KEY:
1542 error = bstr_to_blob(bstr_value, verified_boot_key);
1543 if (error != KM_ERROR_OK) return error;
1544 break;
1545 case EatClaim::VERIFIED_BOOT_HASH:
1546 // Not parsing this for now.
1547 break;
1548 case EatClaim::DEVICE_UNIQUE_ATTESTATION:
1549 if (value_item->asSimple() == nullptr || !value_item->asSimple()->asBool()->value()) {
1550 return KM_ERROR_INVALID_TAG;
1551 }
1552 // Not parsing this for now.
1553 break;
1554 case EatClaim::DEVICE_LOCKED:
1555 ASSERT_OR_RETURN_ERROR(simple_value->asBool(), KM_ERROR_INVALID_TAG);
1556 *device_locked = simple_value->asBool()->value();
1557 break;
1558 case EatClaim::BOOT_STATE:
1559 ASSERT_OR_RETURN_ERROR(array_value, KM_ERROR_INVALID_TAG);
1560 ASSERT_OR_RETURN_ERROR(array_value->size() == 5, KM_ERROR_INVALID_TAG);
1561 ASSERT_OR_RETURN_ERROR((*array_value)[4]->asSimple()->asBool()->value() == false,
1562 KM_ERROR_INVALID_TAG);
1563 verified_or_self_signed = (*array_value)[0]->asSimple()->asBool()->value();
1564 ASSERT_OR_RETURN_ERROR(verified_or_self_signed ==
1565 (*array_value)[1]->asSimple()->asBool()->value(),
1566 KM_ERROR_INVALID_TAG);
1567 ASSERT_OR_RETURN_ERROR(verified_or_self_signed ==
1568 (*array_value)[2]->asSimple()->asBool()->value(),
1569 KM_ERROR_INVALID_TAG);
1570 ASSERT_OR_RETURN_ERROR(verified_or_self_signed ==
1571 (*array_value)[3]->asSimple()->asBool()->value(),
1572 KM_ERROR_INVALID_TAG);
1573 break;
1574 case EatClaim::OFFICIAL_BUILD:
1575 *verified_boot_state = KM_VERIFIED_BOOT_VERIFIED;
1576 break;
1577 }
1578 }
1579
1580 if (*verified_boot_state == KM_VERIFIED_BOOT_VERIFIED) {
1581 (void)(verified_boot_state);
1582 // TODO: re-enable this
1583 // ASSERT_OR_RETURN_ERROR(verified_or_self_signed, KM_ERROR_INVALID_TAG);
1584 } else {
1585 *verified_boot_state =
1586 verified_or_self_signed ? KM_VERIFIED_BOOT_SELF_SIGNED : KM_VERIFIED_BOOT_UNVERIFIED;
1587 }
1588
1589 return KM_ERROR_OK;
1590 }
1591
parse_submod_values(AuthorizationSetBuilder * set_builder,int * auth_set_security_level,const cppbor::Map * submod_map)1592 keymaster_error_t parse_submod_values(AuthorizationSetBuilder* set_builder,
1593 int* auth_set_security_level, const cppbor::Map* submod_map) {
1594 ASSERT_OR_RETURN_ERROR(set_builder, KM_ERROR_UNEXPECTED_NULL_POINTER);
1595 for (size_t i = 0; i < submod_map->size(); i++) {
1596 auto& [key_item, value_item] = (*submod_map)[i];
1597 const cppbor::Int* key_int = key_item->asInt();
1598 ASSERT_OR_RETURN_ERROR(key_int, KM_ERROR_INVALID_TAG);
1599 int key = key_int->value();
1600 keymaster_error_t error;
1601 keymaster_blob_t blob;
1602
1603 switch ((EatClaim)key) {
1604 default:
1605 return KM_ERROR_INVALID_TAG;
1606 case EatClaim::ALGORITHM:
1607 ASSERT_OR_RETURN_ERROR(value_item->asInt(), KM_ERROR_INVALID_TAG);
1608 set_builder->Authorization(
1609 TAG_ALGORITHM, static_cast<keymaster_algorithm_t>(value_item->asInt()->value()));
1610 break;
1611 case EatClaim::EC_CURVE:
1612 ASSERT_OR_RETURN_ERROR(value_item->asInt(), KM_ERROR_INVALID_TAG);
1613 set_builder->Authorization(
1614 TAG_EC_CURVE, static_cast<keymaster_ec_curve_t>(value_item->asInt()->value()));
1615 break;
1616 case EatClaim::USER_AUTH_TYPE:
1617 ASSERT_OR_RETURN_ERROR(value_item->asInt(), KM_ERROR_INVALID_TAG);
1618 set_builder->Authorization(TAG_USER_AUTH_TYPE, static_cast<hw_authenticator_type_t>(
1619 value_item->asInt()->value()));
1620 break;
1621 case EatClaim::ORIGIN:
1622 ASSERT_OR_RETURN_ERROR(value_item->asInt(), KM_ERROR_INVALID_TAG);
1623 set_builder->Authorization(
1624 TAG_ORIGIN, static_cast<keymaster_key_origin_t>(value_item->asInt()->value()));
1625 break;
1626 case EatClaim::PURPOSE:
1627 for (size_t j = 0; j < value_item->asArray()->size(); j++) {
1628 set_builder->Authorization(TAG_PURPOSE,
1629 static_cast<keymaster_purpose_t>(
1630 (*value_item->asArray())[j]->asInt()->value()));
1631 }
1632 break;
1633 case EatClaim::PADDING:
1634 for (size_t j = 0; j < value_item->asArray()->size(); j++) {
1635 set_builder->Authorization(TAG_PADDING,
1636 static_cast<keymaster_padding_t>(
1637 (*value_item->asArray())[j]->asInt()->value()));
1638 }
1639 break;
1640 case EatClaim::DIGEST:
1641 for (size_t j = 0; j < value_item->asArray()->size(); j++) {
1642 set_builder->Authorization(
1643 TAG_DIGEST,
1644 static_cast<keymaster_digest_t>((*value_item->asArray())[j]->asInt()->value()));
1645 }
1646 break;
1647 case EatClaim::BLOCK_MODE:
1648 for (size_t j = 0; j < value_item->asArray()->size(); j++) {
1649 set_builder->Authorization(TAG_BLOCK_MODE,
1650 static_cast<keymaster_block_mode_t>(
1651 (*value_item->asArray())[j]->asInt()->value()));
1652 }
1653 break;
1654 case EatClaim::KEY_SIZE:
1655 ASSERT_OR_RETURN_ERROR(value_item->asInt(), KM_ERROR_INVALID_TAG);
1656 set_builder->Authorization(TAG_KEY_SIZE, value_item->asInt()->value());
1657 break;
1658 case EatClaim::AUTH_TIMEOUT:
1659 ASSERT_OR_RETURN_ERROR(value_item->asInt(), KM_ERROR_INVALID_TAG);
1660 set_builder->Authorization(TAG_AUTH_TIMEOUT, value_item->asInt()->value());
1661 break;
1662 case EatClaim::OS_VERSION:
1663 ASSERT_OR_RETURN_ERROR(value_item->asInt(), KM_ERROR_INVALID_TAG);
1664 set_builder->Authorization(TAG_OS_VERSION, value_item->asInt()->value());
1665 break;
1666 case EatClaim::OS_PATCHLEVEL:
1667 ASSERT_OR_RETURN_ERROR(value_item->asInt(), KM_ERROR_INVALID_TAG);
1668 set_builder->Authorization(TAG_OS_PATCHLEVEL, value_item->asInt()->value());
1669 break;
1670 case EatClaim::MIN_MAC_LENGTH:
1671 ASSERT_OR_RETURN_ERROR(value_item->asInt(), KM_ERROR_INVALID_TAG);
1672 set_builder->Authorization(TAG_MIN_MAC_LENGTH, value_item->asInt()->value());
1673 break;
1674 case EatClaim::BOOT_PATCHLEVEL:
1675 ASSERT_OR_RETURN_ERROR(value_item->asInt(), KM_ERROR_INVALID_TAG);
1676 set_builder->Authorization(TAG_BOOT_PATCHLEVEL, value_item->asInt()->value());
1677 break;
1678 case EatClaim::VENDOR_PATCHLEVEL:
1679 ASSERT_OR_RETURN_ERROR(value_item->asInt(), KM_ERROR_INVALID_TAG);
1680 set_builder->Authorization(TAG_VENDOR_PATCHLEVEL, value_item->asInt()->value());
1681 break;
1682 case EatClaim::RSA_PUBLIC_EXPONENT:
1683 ASSERT_OR_RETURN_ERROR(value_item->asInt(), KM_ERROR_INVALID_TAG);
1684 set_builder->Authorization(TAG_RSA_PUBLIC_EXPONENT, value_item->asInt()->value());
1685 break;
1686 case EatClaim::ACTIVE_DATETIME:
1687 ASSERT_OR_RETURN_ERROR(value_item->asInt(), KM_ERROR_INVALID_TAG);
1688 set_builder->Authorization(TAG_ACTIVE_DATETIME, value_item->asInt()->value());
1689 break;
1690 case EatClaim::ORIGINATION_EXPIRE_DATETIME:
1691 ASSERT_OR_RETURN_ERROR(value_item->asInt(), KM_ERROR_INVALID_TAG);
1692 set_builder->Authorization(TAG_ORIGINATION_EXPIRE_DATETIME,
1693 value_item->asInt()->value());
1694 break;
1695 case EatClaim::USAGE_EXPIRE_DATETIME:
1696 ASSERT_OR_RETURN_ERROR(value_item->asInt(), KM_ERROR_INVALID_TAG);
1697 set_builder->Authorization(TAG_USAGE_EXPIRE_DATETIME, value_item->asInt()->value());
1698 break;
1699 case EatClaim::IAT:
1700 ASSERT_OR_RETURN_ERROR(value_item->asInt(), KM_ERROR_INVALID_TAG);
1701 set_builder->Authorization(TAG_CREATION_DATETIME, value_item->asInt()->value());
1702 break;
1703 case EatClaim::NO_AUTH_REQUIRED:
1704 if (value_item->asSimple() == nullptr || !value_item->asSimple()->asBool()->value()) {
1705 return KM_ERROR_INVALID_TAG;
1706 }
1707 set_builder->Authorization(TAG_NO_AUTH_REQUIRED);
1708 break;
1709 case EatClaim::ALL_APPLICATIONS:
1710 if (value_item->asSimple() == nullptr || !value_item->asSimple()->asBool()->value()) {
1711 return KM_ERROR_INVALID_TAG;
1712 }
1713 set_builder->Authorization(TAG_ALL_APPLICATIONS);
1714 break;
1715 case EatClaim::ROLLBACK_RESISTANT:
1716 if (value_item->asSimple() == nullptr || !value_item->asSimple()->asBool()->value()) {
1717 return KM_ERROR_INVALID_TAG;
1718 }
1719 set_builder->Authorization(TAG_ROLLBACK_RESISTANT);
1720 break;
1721 case EatClaim::ALLOW_WHILE_ON_BODY:
1722 if (value_item->asSimple() == nullptr || !value_item->asSimple()->asBool()->value()) {
1723 return KM_ERROR_INVALID_TAG;
1724 }
1725 set_builder->Authorization(TAG_ALLOW_WHILE_ON_BODY);
1726 break;
1727 case EatClaim::UNLOCKED_DEVICE_REQUIRED:
1728 if (value_item->asSimple() == nullptr || !value_item->asSimple()->asBool()->value()) {
1729 return KM_ERROR_INVALID_TAG;
1730 }
1731 set_builder->Authorization(TAG_UNLOCKED_DEVICE_REQUIRED);
1732 break;
1733 case EatClaim::CALLER_NONCE:
1734 if (value_item->asSimple() == nullptr || !value_item->asSimple()->asBool()->value()) {
1735 return KM_ERROR_INVALID_TAG;
1736 }
1737 set_builder->Authorization(TAG_CALLER_NONCE);
1738 break;
1739 case EatClaim::TRUSTED_CONFIRMATION_REQUIRED:
1740 if (value_item->asSimple() == nullptr || !value_item->asSimple()->asBool()->value()) {
1741 return KM_ERROR_INVALID_TAG;
1742 }
1743 set_builder->Authorization(TAG_TRUSTED_CONFIRMATION_REQUIRED);
1744 break;
1745 case EatClaim::EARLY_BOOT_ONLY:
1746 if (value_item->asSimple() == nullptr || !value_item->asSimple()->asBool()->value()) {
1747 return KM_ERROR_INVALID_TAG;
1748 }
1749 set_builder->Authorization(TAG_EARLY_BOOT_ONLY);
1750 break;
1751 case EatClaim::IDENTITY_CREDENTIAL_KEY:
1752 if (value_item->asSimple() == nullptr || !value_item->asSimple()->asBool()->value()) {
1753 return KM_ERROR_INVALID_TAG;
1754 }
1755 set_builder->Authorization(TAG_IDENTITY_CREDENTIAL_KEY);
1756 break;
1757 case EatClaim::STORAGE_KEY:
1758 if (value_item->asSimple() == nullptr || !value_item->asSimple()->asBool()->value()) {
1759 return KM_ERROR_INVALID_TAG;
1760 }
1761 set_builder->Authorization(TAG_STORAGE_KEY);
1762 break;
1763 case EatClaim::TRUSTED_USER_PRESENCE_REQUIRED:
1764 if (value_item->asSimple() == nullptr || !value_item->asSimple()->asBool()->value()) {
1765 return KM_ERROR_INVALID_TAG;
1766 }
1767 set_builder->Authorization(TAG_TRUSTED_USER_PRESENCE_REQUIRED);
1768 break;
1769 case EatClaim::DEVICE_UNIQUE_ATTESTATION:
1770 if (value_item->asSimple() == nullptr || !value_item->asSimple()->asBool()->value()) {
1771 return KM_ERROR_INVALID_TAG;
1772 }
1773 set_builder->Authorization(TAG_DEVICE_UNIQUE_ATTESTATION);
1774 break;
1775 case EatClaim::APPLICATION_ID:
1776 error = bstr_to_blob(value_item->asBstr(), &blob);
1777 if (error != KM_ERROR_OK) return error;
1778 set_builder->Authorization(TAG_APPLICATION_ID, blob);
1779 break;
1780 case EatClaim::ATTESTATION_APPLICATION_ID:
1781 error = bstr_to_blob(value_item->asBstr(), &blob);
1782 if (error != KM_ERROR_OK) return error;
1783 set_builder->Authorization(TAG_ATTESTATION_APPLICATION_ID, blob);
1784 break;
1785 case EatClaim::ATTESTATION_ID_BRAND:
1786 error = bstr_to_blob(value_item->asBstr(), &blob);
1787 if (error != KM_ERROR_OK) return error;
1788 set_builder->Authorization(TAG_ATTESTATION_ID_BRAND, blob);
1789 break;
1790 case EatClaim::ATTESTATION_ID_DEVICE:
1791 error = bstr_to_blob(value_item->asBstr(), &blob);
1792 if (error != KM_ERROR_OK) return error;
1793 set_builder->Authorization(TAG_ATTESTATION_ID_DEVICE, blob);
1794 break;
1795 case EatClaim::ATTESTATION_ID_PRODUCT:
1796 error = bstr_to_blob(value_item->asBstr(), &blob);
1797 if (error != KM_ERROR_OK) return error;
1798 set_builder->Authorization(TAG_ATTESTATION_ID_PRODUCT, blob);
1799 break;
1800 case EatClaim::ATTESTATION_ID_SERIAL:
1801 error = bstr_to_blob(value_item->asBstr(), &blob);
1802 if (error != KM_ERROR_OK) return error;
1803 set_builder->Authorization(TAG_ATTESTATION_ID_SERIAL, blob);
1804 break;
1805 case EatClaim::UEID:
1806 error = ueid_to_imei_blob(value_item->asBstr(), &blob);
1807 if (error != KM_ERROR_OK) return error;
1808 set_builder->Authorization(TAG_ATTESTATION_ID_IMEI, blob);
1809 break;
1810 case EatClaim::ATTESTATION_ID_MEID:
1811 error = bstr_to_blob(value_item->asBstr(), &blob);
1812 if (error != KM_ERROR_OK) return error;
1813 set_builder->Authorization(TAG_ATTESTATION_ID_MEID, blob);
1814 break;
1815 case EatClaim::ATTESTATION_ID_MANUFACTURER:
1816 error = bstr_to_blob(value_item->asBstr(), &blob);
1817 if (error != KM_ERROR_OK) return error;
1818 set_builder->Authorization(TAG_ATTESTATION_ID_MANUFACTURER, blob);
1819 break;
1820 case EatClaim::ATTESTATION_ID_MODEL:
1821 error = bstr_to_blob(value_item->asBstr(), &blob);
1822 if (error != KM_ERROR_OK) return error;
1823 set_builder->Authorization(TAG_ATTESTATION_ID_MODEL, blob);
1824 break;
1825 case EatClaim::CONFIRMATION_TOKEN:
1826 error = bstr_to_blob(value_item->asBstr(), &blob);
1827 if (error != KM_ERROR_OK) return error;
1828 set_builder->Authorization(TAG_CONFIRMATION_TOKEN, blob);
1829 break;
1830 case EatClaim::SECURITY_LEVEL:
1831 ASSERT_OR_RETURN_ERROR(value_item->asInt(), KM_ERROR_INVALID_TAG);
1832 *auth_set_security_level = value_item->asInt()->value();
1833 }
1834 }
1835
1836 return KM_ERROR_OK;
1837 }
1838
parse_eat_submod(const cppbor::Map * submod_values,AuthorizationSet * software_enforced,AuthorizationSet * tee_enforced)1839 keymaster_error_t parse_eat_submod(const cppbor::Map* submod_values,
1840 AuthorizationSet* software_enforced,
1841 AuthorizationSet* tee_enforced) {
1842 AuthorizationSetBuilder auth_set_builder;
1843 int auth_set_security_level = 0;
1844 keymaster_error_t error =
1845 parse_submod_values(&auth_set_builder, &auth_set_security_level, submod_values);
1846 if (error) return error;
1847 switch ((EatSecurityLevel)auth_set_security_level) {
1848 case EatSecurityLevel::HARDWARE:
1849 // Hardware attestation should never occur in a submod of another EAT.
1850 [[fallthrough]];
1851 default:
1852 return KM_ERROR_INVALID_TAG;
1853 case EatSecurityLevel::UNRESTRICTED:
1854 *software_enforced = AuthorizationSet(auth_set_builder);
1855 break;
1856 case EatSecurityLevel::SECURE_RESTRICTED:
1857 *tee_enforced = AuthorizationSet(auth_set_builder);
1858 break;
1859 }
1860
1861 return KM_ERROR_OK;
1862 }
1863 } // namespace keymaster
1864