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