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