• 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 #pragma once
18 
19 #include <hardware/keymaster_defs.h>
20 
21 #include <keymaster/authorization_set.h>
22 #include <keymaster/km_version.h>
23 
24 #include <cppbor.h>
25 #include <openssl/asn1t.h>
26 
27 #include <vector>
28 
29 #define remove_type_mask(tag) ((tag)&0x0FFFFFFF)
30 
31 namespace keymaster {
32 
33 class AttestationContext;
34 
35 constexpr KmVersion kCurrentKmVersion = KmVersion::KEYMASTER_4_1;
36 
37 constexpr int EAT_CLAIM_PRIVATE_BASE = -80000;
38 constexpr int EAT_CLAIM_PRIVATE_NON_KM_BASE = EAT_CLAIM_PRIVATE_BASE - 2000;
39 
convert_to_eat_claim(keymaster_tag_t tag)40 constexpr int64_t convert_to_eat_claim(keymaster_tag_t tag) {
41     return EAT_CLAIM_PRIVATE_BASE - remove_type_mask(tag);
42 }
43 
44 struct stack_st_ASN1_TYPE_Delete {
operatorstack_st_ASN1_TYPE_Delete45     void operator()(stack_st_ASN1_TYPE* p) { sk_ASN1_TYPE_free(p); }
46 };
47 
48 struct ASN1_STRING_Delete {
operatorASN1_STRING_Delete49     void operator()(ASN1_STRING* p) { ASN1_STRING_free(p); }
50 };
51 
52 struct ASN1_TYPE_Delete {
operatorASN1_TYPE_Delete53     void operator()(ASN1_TYPE* p) { ASN1_TYPE_free(p); }
54 };
55 
56 #define ASN1_INTEGER_SET STACK_OF(ASN1_INTEGER)
57 
58 typedef struct km_root_of_trust {
59     ASN1_OCTET_STRING* verified_boot_key;
60     ASN1_BOOLEAN device_locked;
61     ASN1_ENUMERATED* verified_boot_state;
62     ASN1_OCTET_STRING* verified_boot_hash;
63 } KM_ROOT_OF_TRUST;
64 
65 ASN1_SEQUENCE(KM_ROOT_OF_TRUST) = {
66     ASN1_SIMPLE(KM_ROOT_OF_TRUST, verified_boot_key, ASN1_OCTET_STRING),
67     ASN1_SIMPLE(KM_ROOT_OF_TRUST, device_locked, ASN1_BOOLEAN),
68     ASN1_SIMPLE(KM_ROOT_OF_TRUST, verified_boot_state, ASN1_ENUMERATED),
69     ASN1_SIMPLE(KM_ROOT_OF_TRUST, verified_boot_hash, ASN1_OCTET_STRING),
70 } ASN1_SEQUENCE_END(KM_ROOT_OF_TRUST);
71 DECLARE_ASN1_FUNCTIONS(KM_ROOT_OF_TRUST);
72 
73 // Fields ordered in tag order.
74 typedef struct km_auth_list {
75     ASN1_INTEGER_SET* purpose;
76     ASN1_INTEGER* algorithm;
77     ASN1_INTEGER* key_size;
78     ASN1_INTEGER_SET* block_mode;
79     ASN1_INTEGER_SET* digest;
80     ASN1_INTEGER_SET* padding;
81     ASN1_NULL* caller_nonce;
82     ASN1_INTEGER* min_mac_length;
83     ASN1_INTEGER_SET* kdf;
84     ASN1_INTEGER* ec_curve;
85     ASN1_INTEGER* rsa_public_exponent;
86     ASN1_INTEGER_SET* mgf_digest;
87     ASN1_NULL* rollback_resistance;
88     ASN1_NULL* early_boot_only;
89     ASN1_INTEGER* active_date_time;
90     ASN1_INTEGER* origination_expire_date_time;
91     ASN1_INTEGER* usage_expire_date_time;
92     ASN1_INTEGER* usage_count_limit;
93     ASN1_NULL* no_auth_required;
94     ASN1_INTEGER* user_auth_type;
95     ASN1_INTEGER* auth_timeout;
96     ASN1_NULL* allow_while_on_body;
97     ASN1_NULL* trusted_user_presence_required;
98     ASN1_NULL* trusted_confirmation_required;
99     ASN1_NULL* unlocked_device_required;
100     ASN1_NULL* all_applications;
101     ASN1_OCTET_STRING* application_id;
102     ASN1_INTEGER* creation_date_time;
103     ASN1_INTEGER* origin;
104     ASN1_NULL* rollback_resistant;
105     KM_ROOT_OF_TRUST* root_of_trust;
106     ASN1_INTEGER* os_version;
107     ASN1_INTEGER* os_patchlevel;
108     ASN1_OCTET_STRING* attestation_application_id;
109     ASN1_OCTET_STRING* attestation_id_brand;
110     ASN1_OCTET_STRING* attestation_id_device;
111     ASN1_OCTET_STRING* attestation_id_product;
112     ASN1_OCTET_STRING* attestation_id_serial;
113     ASN1_OCTET_STRING* attestation_id_imei;
114     ASN1_OCTET_STRING* attestation_id_meid;
115     ASN1_OCTET_STRING* attestation_id_manufacturer;
116     ASN1_OCTET_STRING* attestation_id_model;
117     ASN1_INTEGER* vendor_patchlevel;
118     ASN1_INTEGER* boot_patch_level;
119     ASN1_NULL* device_unique_attestation;
120     ASN1_NULL* identity_credential_key;
121 } KM_AUTH_LIST;
122 
123 ASN1_SEQUENCE(KM_AUTH_LIST) = {
124     ASN1_EXP_SET_OF_OPT(KM_AUTH_LIST, purpose, ASN1_INTEGER, TAG_PURPOSE.masked_tag()),
125     ASN1_EXP_OPT(KM_AUTH_LIST, algorithm, ASN1_INTEGER, TAG_ALGORITHM.masked_tag()),
126     ASN1_EXP_OPT(KM_AUTH_LIST, key_size, ASN1_INTEGER, TAG_KEY_SIZE.masked_tag()),
127     ASN1_EXP_SET_OF_OPT(KM_AUTH_LIST, block_mode, ASN1_INTEGER, TAG_BLOCK_MODE.masked_tag()),
128     ASN1_EXP_SET_OF_OPT(KM_AUTH_LIST, digest, ASN1_INTEGER, TAG_DIGEST.masked_tag()),
129     ASN1_EXP_SET_OF_OPT(KM_AUTH_LIST, padding, ASN1_INTEGER, TAG_PADDING.masked_tag()),
130     ASN1_EXP_OPT(KM_AUTH_LIST, caller_nonce, ASN1_NULL, TAG_CALLER_NONCE.masked_tag()),
131     ASN1_EXP_OPT(KM_AUTH_LIST, min_mac_length, ASN1_INTEGER, TAG_MIN_MAC_LENGTH.masked_tag()),
132     ASN1_EXP_SET_OF_OPT(KM_AUTH_LIST, kdf, ASN1_INTEGER, TAG_KDF.masked_tag()),
133     ASN1_EXP_OPT(KM_AUTH_LIST, ec_curve, ASN1_INTEGER, TAG_EC_CURVE.masked_tag()),
134     ASN1_EXP_OPT(KM_AUTH_LIST, rsa_public_exponent, ASN1_INTEGER,
135                  TAG_RSA_PUBLIC_EXPONENT.masked_tag()),
136     ASN1_EXP_SET_OF_OPT(KM_AUTH_LIST, mgf_digest, ASN1_INTEGER,
137                         TAG_RSA_OAEP_MGF_DIGEST.masked_tag()),
138     ASN1_EXP_OPT(KM_AUTH_LIST, rollback_resistance, ASN1_NULL,
139                  TAG_ROLLBACK_RESISTANCE.masked_tag()),
140     ASN1_EXP_OPT(KM_AUTH_LIST, early_boot_only, ASN1_NULL, TAG_EARLY_BOOT_ONLY.masked_tag()),
141     ASN1_EXP_OPT(KM_AUTH_LIST, active_date_time, ASN1_INTEGER, TAG_ACTIVE_DATETIME.masked_tag()),
142     ASN1_EXP_OPT(KM_AUTH_LIST, origination_expire_date_time, ASN1_INTEGER,
143                  TAG_ORIGINATION_EXPIRE_DATETIME.masked_tag()),
144     ASN1_EXP_OPT(KM_AUTH_LIST, usage_expire_date_time, ASN1_INTEGER,
145                  TAG_USAGE_EXPIRE_DATETIME.masked_tag()),
146     ASN1_EXP_OPT(KM_AUTH_LIST, usage_count_limit, ASN1_INTEGER, TAG_USAGE_COUNT_LIMIT.masked_tag()),
147     ASN1_EXP_OPT(KM_AUTH_LIST, no_auth_required, ASN1_NULL, TAG_NO_AUTH_REQUIRED.masked_tag()),
148     ASN1_EXP_OPT(KM_AUTH_LIST, user_auth_type, ASN1_INTEGER, TAG_USER_AUTH_TYPE.masked_tag()),
149     ASN1_EXP_OPT(KM_AUTH_LIST, auth_timeout, ASN1_INTEGER, TAG_AUTH_TIMEOUT.masked_tag()),
150     ASN1_EXP_OPT(KM_AUTH_LIST, allow_while_on_body, ASN1_NULL,
151                  TAG_ALLOW_WHILE_ON_BODY.masked_tag()),
152     ASN1_EXP_OPT(KM_AUTH_LIST, trusted_user_presence_required, ASN1_NULL,
153                  TAG_TRUSTED_USER_PRESENCE_REQUIRED.masked_tag()),
154     ASN1_EXP_OPT(KM_AUTH_LIST, trusted_confirmation_required, ASN1_NULL,
155                  TAG_TRUSTED_CONFIRMATION_REQUIRED.masked_tag()),
156     ASN1_EXP_OPT(KM_AUTH_LIST, unlocked_device_required, ASN1_NULL,
157                  TAG_UNLOCKED_DEVICE_REQUIRED.masked_tag()),
158     ASN1_EXP_OPT(KM_AUTH_LIST, all_applications, ASN1_NULL, TAG_ALL_APPLICATIONS.masked_tag()),
159     ASN1_EXP_OPT(KM_AUTH_LIST, application_id, ASN1_OCTET_STRING, TAG_APPLICATION_ID.masked_tag()),
160     ASN1_EXP_OPT(KM_AUTH_LIST, creation_date_time, ASN1_INTEGER,
161                  TAG_CREATION_DATETIME.masked_tag()),
162     ASN1_EXP_OPT(KM_AUTH_LIST, origin, ASN1_INTEGER, TAG_ORIGIN.masked_tag()),
163     ASN1_EXP_OPT(KM_AUTH_LIST, rollback_resistant, ASN1_NULL, TAG_ROLLBACK_RESISTANT.masked_tag()),
164     ASN1_EXP_OPT(KM_AUTH_LIST, root_of_trust, KM_ROOT_OF_TRUST, TAG_ROOT_OF_TRUST.masked_tag()),
165     ASN1_EXP_OPT(KM_AUTH_LIST, os_version, ASN1_INTEGER, TAG_OS_VERSION.masked_tag()),
166     ASN1_EXP_OPT(KM_AUTH_LIST, os_patchlevel, ASN1_INTEGER, TAG_OS_PATCHLEVEL.masked_tag()),
167     ASN1_EXP_OPT(KM_AUTH_LIST, attestation_application_id, ASN1_OCTET_STRING,
168                  TAG_ATTESTATION_APPLICATION_ID.masked_tag()),
169     ASN1_EXP_OPT(KM_AUTH_LIST, attestation_id_brand, ASN1_OCTET_STRING,
170                  TAG_ATTESTATION_ID_BRAND.masked_tag()),
171     ASN1_EXP_OPT(KM_AUTH_LIST, attestation_id_device, ASN1_OCTET_STRING,
172                  TAG_ATTESTATION_ID_DEVICE.masked_tag()),
173     ASN1_EXP_OPT(KM_AUTH_LIST, attestation_id_product, ASN1_OCTET_STRING,
174                  TAG_ATTESTATION_ID_PRODUCT.masked_tag()),
175     ASN1_EXP_OPT(KM_AUTH_LIST, attestation_id_serial, ASN1_OCTET_STRING,
176                  TAG_ATTESTATION_ID_SERIAL.masked_tag()),
177     ASN1_EXP_OPT(KM_AUTH_LIST, attestation_id_imei, ASN1_OCTET_STRING,
178                  TAG_ATTESTATION_ID_IMEI.masked_tag()),
179     ASN1_EXP_OPT(KM_AUTH_LIST, attestation_id_meid, ASN1_OCTET_STRING,
180                  TAG_ATTESTATION_ID_MEID.masked_tag()),
181     ASN1_EXP_OPT(KM_AUTH_LIST, attestation_id_manufacturer, ASN1_OCTET_STRING,
182                  TAG_ATTESTATION_ID_MANUFACTURER.masked_tag()),
183     ASN1_EXP_OPT(KM_AUTH_LIST, attestation_id_model, ASN1_OCTET_STRING,
184                  TAG_ATTESTATION_ID_MODEL.masked_tag()),
185     ASN1_EXP_OPT(KM_AUTH_LIST, vendor_patchlevel, ASN1_INTEGER, TAG_VENDOR_PATCHLEVEL.masked_tag()),
186     ASN1_EXP_OPT(KM_AUTH_LIST, boot_patch_level, ASN1_INTEGER, TAG_BOOT_PATCHLEVEL.masked_tag()),
187     ASN1_EXP_OPT(KM_AUTH_LIST, device_unique_attestation, ASN1_NULL,
188                  TAG_DEVICE_UNIQUE_ATTESTATION.masked_tag()),
189     ASN1_EXP_OPT(KM_AUTH_LIST, identity_credential_key, ASN1_NULL,
190                  TAG_IDENTITY_CREDENTIAL_KEY.masked_tag()),
191 } ASN1_SEQUENCE_END(KM_AUTH_LIST);
192 DECLARE_ASN1_FUNCTIONS(KM_AUTH_LIST);
193 
194 typedef struct km_key_description {
195     ASN1_INTEGER* attestation_version;
196     ASN1_ENUMERATED* attestation_security_level;
197     ASN1_INTEGER* keymaster_version;
198     ASN1_ENUMERATED* keymaster_security_level;
199     ASN1_OCTET_STRING* attestation_challenge;
200     KM_AUTH_LIST* software_enforced;
201     KM_AUTH_LIST* tee_enforced;
202     ASN1_INTEGER* unique_id;
203 } KM_KEY_DESCRIPTION;
204 
205 ASN1_SEQUENCE(KM_KEY_DESCRIPTION) = {
206     ASN1_SIMPLE(KM_KEY_DESCRIPTION, attestation_version, ASN1_INTEGER),
207     ASN1_SIMPLE(KM_KEY_DESCRIPTION, attestation_security_level, ASN1_ENUMERATED),
208     ASN1_SIMPLE(KM_KEY_DESCRIPTION, keymaster_version, ASN1_INTEGER),
209     ASN1_SIMPLE(KM_KEY_DESCRIPTION, keymaster_security_level, ASN1_ENUMERATED),
210     ASN1_SIMPLE(KM_KEY_DESCRIPTION, attestation_challenge, ASN1_OCTET_STRING),
211     ASN1_SIMPLE(KM_KEY_DESCRIPTION, unique_id, ASN1_OCTET_STRING),
212     ASN1_SIMPLE(KM_KEY_DESCRIPTION, software_enforced, KM_AUTH_LIST),
213     ASN1_SIMPLE(KM_KEY_DESCRIPTION, tee_enforced, KM_AUTH_LIST),
214 } ASN1_SEQUENCE_END(KM_KEY_DESCRIPTION);
215 DECLARE_ASN1_FUNCTIONS(KM_KEY_DESCRIPTION);
216 
217 enum class EatClaim {
218 
219     // Official CWT claims, as defined in https://www.iana.org/assignments/cwt/cwt.xhtml
220 
221     IAT = 6,
222     CTI = 7,
223 
224     // Claims defined in
225     // https://github.com/laurencelundblade/ctoken/blob/master/inc/ctoken_eat_labels.h, by the
226     // author of the EAT standard, and in the ARM PSA. They should be considered stable at least
227     // until the standard is officially published.
228 
229     UEID = -75009,
230     NONCE = -75008,
231     SECURITY_LEVEL = -76002,
232     BOOT_STATE = -76003,
233     SUBMODS = -76000,
234 
235     // Claims specific to Android, and not part of the official EAT standard.
236 
237     PURPOSE = convert_to_eat_claim(KM_TAG_PURPOSE),
238     ALGORITHM = convert_to_eat_claim(KM_TAG_ALGORITHM),
239     KEY_SIZE = convert_to_eat_claim(KM_TAG_KEY_SIZE),
240     BLOCK_MODE = convert_to_eat_claim(KM_TAG_BLOCK_MODE),
241     DIGEST = convert_to_eat_claim(KM_TAG_DIGEST),
242     PADDING = convert_to_eat_claim(KM_TAG_PADDING),
243     // TODO: Check if CALLER_NONCE is needed (see go/keymint-eat)
244     CALLER_NONCE = convert_to_eat_claim(KM_TAG_CALLER_NONCE),
245     MIN_MAC_LENGTH = convert_to_eat_claim(KM_TAG_MIN_MAC_LENGTH),
246     EC_CURVE = convert_to_eat_claim(KM_TAG_EC_CURVE),
247     RSA_PUBLIC_EXPONENT = convert_to_eat_claim(KM_TAG_RSA_PUBLIC_EXPONENT),
248     EARLY_BOOT_ONLY = convert_to_eat_claim(KM_TAG_EARLY_BOOT_ONLY),
249     ACTIVE_DATETIME = convert_to_eat_claim(KM_TAG_ACTIVE_DATETIME),
250     ORIGINATION_EXPIRE_DATETIME = convert_to_eat_claim(KM_TAG_ORIGINATION_EXPIRE_DATETIME),
251     USAGE_EXPIRE_DATETIME = convert_to_eat_claim(KM_TAG_USAGE_EXPIRE_DATETIME),
252     NO_AUTH_REQUIRED = convert_to_eat_claim(KM_TAG_NO_AUTH_REQUIRED),
253     USER_AUTH_TYPE = convert_to_eat_claim(KM_TAG_USER_AUTH_TYPE),
254     AUTH_TIMEOUT = convert_to_eat_claim(KM_TAG_AUTH_TIMEOUT),
255     ALLOW_WHILE_ON_BODY = convert_to_eat_claim(KM_TAG_ALLOW_WHILE_ON_BODY),
256     TRUSTED_USER_PRESENCE_REQUIRED = convert_to_eat_claim(KM_TAG_TRUSTED_USER_PRESENCE_REQUIRED),
257     TRUSTED_CONFIRMATION_REQUIRED = convert_to_eat_claim(KM_TAG_TRUSTED_CONFIRMATION_REQUIRED),
258     UNLOCKED_DEVICE_REQUIRED = convert_to_eat_claim(KM_TAG_UNLOCKED_DEVICE_REQUIRED),
259     ALL_APPLICATIONS = convert_to_eat_claim(KM_TAG_ALL_APPLICATIONS),
260     APPLICATION_ID = convert_to_eat_claim(KM_TAG_APPLICATION_ID),
261     ORIGIN = convert_to_eat_claim(KM_TAG_ORIGIN),
262     ROLLBACK_RESISTANT = convert_to_eat_claim(KM_TAG_ROLLBACK_RESISTANT),
263     OS_VERSION = convert_to_eat_claim(KM_TAG_OS_VERSION),
264     OS_PATCHLEVEL = convert_to_eat_claim(KM_TAG_OS_PATCHLEVEL),
265     ATTESTATION_APPLICATION_ID = convert_to_eat_claim(KM_TAG_ATTESTATION_APPLICATION_ID),
266     ATTESTATION_ID_BRAND = convert_to_eat_claim(KM_TAG_ATTESTATION_ID_BRAND),
267     ATTESTATION_ID_DEVICE = convert_to_eat_claim(KM_TAG_ATTESTATION_ID_DEVICE),
268     ATTESTATION_ID_PRODUCT = convert_to_eat_claim(KM_TAG_ATTESTATION_ID_PRODUCT),
269     ATTESTATION_ID_SERIAL = convert_to_eat_claim(KM_TAG_ATTESTATION_ID_SERIAL),
270     ATTESTATION_ID_MEID = convert_to_eat_claim(KM_TAG_ATTESTATION_ID_MEID),
271     ATTESTATION_ID_MANUFACTURER = convert_to_eat_claim(KM_TAG_ATTESTATION_ID_MANUFACTURER),
272     VENDOR_PATCHLEVEL = convert_to_eat_claim(KM_TAG_VENDOR_PATCHLEVEL),
273     BOOT_PATCHLEVEL = convert_to_eat_claim(KM_TAG_BOOT_PATCHLEVEL),
274     ATTESTATION_ID_MODEL = convert_to_eat_claim(KM_TAG_ATTESTATION_ID_MODEL),
275     DEVICE_UNIQUE_ATTESTATION = convert_to_eat_claim(KM_TAG_DEVICE_UNIQUE_ATTESTATION),
276     IDENTITY_CREDENTIAL_KEY = convert_to_eat_claim(KM_TAG_IDENTITY_CREDENTIAL_KEY),
277     STORAGE_KEY = convert_to_eat_claim(KM_TAG_STORAGE_KEY),
278     CONFIRMATION_TOKEN = convert_to_eat_claim(KM_TAG_CONFIRMATION_TOKEN),
279 
280     // Claims specific to Android that do not exist as Keymint tags.
281 
282     VERIFIED_BOOT_KEY = EAT_CLAIM_PRIVATE_NON_KM_BASE - 1,
283     DEVICE_LOCKED = EAT_CLAIM_PRIVATE_NON_KM_BASE - 2,
284     VERIFIED_BOOT_HASH = EAT_CLAIM_PRIVATE_NON_KM_BASE - 3,
285     ATTESTATION_VERSION = EAT_CLAIM_PRIVATE_NON_KM_BASE - 4,
286     KEYMASTER_VERSION = EAT_CLAIM_PRIVATE_NON_KM_BASE - 5,
287     OFFICIAL_BUILD = EAT_CLAIM_PRIVATE_NON_KM_BASE - 6,
288 };
289 
290 enum class EatSecurityLevel {
291     UNRESTRICTED = 1,
292     RESTRICTED = 2,
293     SECURE_RESTRICTED = 3,
294     HARDWARE = 4,
295 };
296 
297 enum class EatEcCurve {
298     P_224 = KM_EC_CURVE_P_224,
299     P_256 = KM_EC_CURVE_P_256,
300     P_384 = KM_EC_CURVE_P_384,
301     P_521 = KM_EC_CURVE_P_521,
302 };
303 
304 static const char kEatSubmodNameSoftware[] = "software";
305 static const char kEatSubmodNameTee[] = "tee";
306 
307 constexpr size_t kImeiBlobLength = 15;
308 constexpr size_t kUeidLength = 15;
309 constexpr uint8_t kImeiTypeByte = 0x03;
310 
311 /**
312  * The OID for Android attestation records.  For the curious, it breaks down as follows:
313  *
314  * 1 = ISO
315  * 3 = org
316  * 6 = DoD (Huh? OIDs are weird.)
317  * 1 = IANA
318  * 4 = Private
319  * 1 = Enterprises
320  * 11129 = Google
321  * 2 = Google security
322  * 1 = certificate extension
323  * 17 / 25 = ASN.1 attestation extension / EAT attestation extension
324  */
325 static const char kAsn1TokenOid[] = "1.3.6.1.4.1.11129.2.1.17";
326 static const char kEatTokenOid[] = "1.3.6.1.4.1.11129.2.1.25";
327 
328 // This build_attestation_record sets the keymaster version to the default
329 // value, and chooses the correct attestation extension (ASN.1 or EAT) based
330 // on that value.
331 keymaster_error_t build_attestation_record(const AuthorizationSet& attestation_params,
332                                            AuthorizationSet software_enforced,
333                                            AuthorizationSet tee_enforced,
334                                            const AttestationContext& context,
335                                            UniquePtr<uint8_t[]>* asn1_key_desc,
336                                            size_t* asn1_key_desc_len);
337 
338 // Builds EAT record, with the same values as the attestation record above,
339 // but encoded as a CBOR (EAT) structure rather than an ASN.1 structure.
340 keymaster_error_t build_eat_record(const AuthorizationSet& attestation_params,
341                                    AuthorizationSet software_enforced,
342                                    AuthorizationSet tee_enforced,      //
343                                    const AttestationContext& context,  //
344                                    std::vector<uint8_t>* eat_token);
345 
346 /**
347  * Helper functions for attestation record tests. Caller takes ownership of
348  * |attestation_challenge->data| and |unique_id->data|, deallocate using delete[].
349  */
350 keymaster_error_t parse_attestation_record(const uint8_t* asn1_key_desc, size_t asn1_key_desc_len,
351                                            uint32_t* attestation_version,  //
352                                            keymaster_security_level_t* attestation_security_level,
353                                            uint32_t* keymaster_version,
354                                            keymaster_security_level_t* keymaster_security_level,
355                                            keymaster_blob_t* attestation_challenge,
356                                            AuthorizationSet* software_enforced,
357                                            AuthorizationSet* tee_enforced,
358                                            keymaster_blob_t* unique_id);
359 
360 /**
361  * Caller takes ownership of |verified_boot_key->data|, deallocate using delete[].
362  */
363 keymaster_error_t parse_root_of_trust(const uint8_t* asn1_key_desc, size_t asn1_key_desc_len,
364                                       keymaster_blob_t* verified_boot_key,
365                                       keymaster_verified_boot_t* verified_boot_state,
366                                       bool* device_locked);
367 
368 keymaster_error_t build_eat_submod(const AuthorizationSet& auth_list,
369                                    EatSecurityLevel security_level, cppbor::Map* submod);
370 
371 keymaster_error_t build_auth_list(const AuthorizationSet& auth_list, KM_AUTH_LIST* record);
372 
373 keymaster_error_t parse_eat_record(
374     const uint8_t* eat_key_desc, size_t eat_key_desc_len, uint32_t* attestation_version,
375     keymaster_security_level_t* attestation_security_level, uint32_t* keymaster_version,
376     keymaster_security_level_t* keymaster_security_level, keymaster_blob_t* attestation_challenge,
377     AuthorizationSet* software_enforced, AuthorizationSet* tee_enforced,
378     keymaster_blob_t* unique_id, keymaster_blob_t* verified_boot_key,
379     keymaster_verified_boot_t* verified_boot_state, bool* device_locked,
380     std::vector<int64_t>* unexpected_claims);
381 
382 keymaster_error_t parse_eat_submod(const cppbor::Map* submod_values,
383                                    AuthorizationSet* software_enforced,
384                                    AuthorizationSet* tee_enforced);
385 
386 keymaster_error_t extract_auth_list(const KM_AUTH_LIST* record, AuthorizationSet* auth_list);
387 
388 /**
389  * Convert a KeymasterContext::Version to the keymaster version number used in attestations.
390  */
version_to_attestation_km_version(KmVersion version)391 inline static uint version_to_attestation_km_version(KmVersion version) {
392     switch (version) {
393     default:
394     case KmVersion::KEYMASTER_1:
395     case KmVersion::KEYMASTER_1_1:
396         return 0;  // Attestation not actually supported.
397     case KmVersion::KEYMASTER_2:
398         return 2;
399     case KmVersion::KEYMASTER_3:
400         return 3;
401     case KmVersion::KEYMASTER_4:
402         return 4;
403     case KmVersion::KEYMASTER_4_1:
404         return 41;
405     case KmVersion::KEYMINT_1:
406         return 100;
407     }
408 }
409 
410 /**
411  * Convert a KeymasterContext::Version to the corresponding attestation format version number.
412  */
version_to_attestation_version(KmVersion version)413 inline static uint version_to_attestation_version(KmVersion version) {
414     switch (version) {
415     default:
416     case KmVersion::KEYMASTER_1:
417         return 0;  // Attestation not actually supported.
418     case KmVersion::KEYMASTER_2:
419         return 1;
420     case KmVersion::KEYMASTER_3:
421         return 2;
422     case KmVersion::KEYMASTER_4:
423         return 3;
424     case KmVersion::KEYMASTER_4_1:
425         return 4;
426     case KmVersion::KEYMINT_1:
427         return 100;
428     }
429 }
430 
431 }  // namespace keymaster
432