summaryrefslogtreecommitdiff
path: root/km_openssl/attestation_record.cpp
blob: f413064764a16c150ef4e96e632e5efd2771968d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
/*
 * Copyright 2016 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include <keymaster/km_openssl/attestation_record.h>

#include <assert.h>
#include <math.h>

#include <unordered_map>

#include <cppbor_parse.h>
#include <openssl/asn1t.h>

#include <keymaster/android_keymaster_utils.h>
#include <keymaster/attestation_context.h>
#include <keymaster/km_openssl/hmac.h>
#include <keymaster/km_openssl/openssl_err.h>
#include <keymaster/km_openssl/openssl_utils.h>

#define ASSERT_OR_RETURN_ERROR(stmt, error)                                                        \
    do {                                                                                           \
        assert(stmt);                                                                              \
        if (!(stmt)) {                                                                             \
            return error;                                                                          \
        }                                                                                          \
    } while (0)

namespace keymaster {

constexpr size_t kMaximumAttestationChallengeLength = 128;

IMPLEMENT_ASN1_FUNCTIONS(KM_ROOT_OF_TRUST);
IMPLEMENT_ASN1_FUNCTIONS(KM_AUTH_LIST);
IMPLEMENT_ASN1_FUNCTIONS(KM_KEY_DESCRIPTION);

static const keymaster_tag_t kDeviceAttestationTags[] = {
    KM_TAG_ATTESTATION_ID_BRAND,        KM_TAG_ATTESTATION_ID_DEVICE, KM_TAG_ATTESTATION_ID_PRODUCT,
    KM_TAG_ATTESTATION_ID_SERIAL,       KM_TAG_ATTESTATION_ID_IMEI,   KM_TAG_ATTESTATION_ID_MEID,
    KM_TAG_ATTESTATION_ID_MANUFACTURER, KM_TAG_ATTESTATION_ID_MODEL,
};

struct KM_AUTH_LIST_Delete {
    void operator()(KM_AUTH_LIST* p) { KM_AUTH_LIST_free(p); }
};

struct KM_KEY_DESCRIPTION_Delete {
    void operator()(KM_KEY_DESCRIPTION* p) { KM_KEY_DESCRIPTION_free(p); }
};

struct KM_ROOT_OF_TRUST_Delete {
    void operator()(KM_ROOT_OF_TRUST* p) { KM_ROOT_OF_TRUST_free(p); }
};

static cppbor::Bstr blob_to_bstr(const keymaster_blob_t& blob) {
    return cppbor::Bstr(std::pair(blob.data, blob.data_length));
}

static keymaster_error_t bstr_to_blob(const cppbor::Bstr* bstr, keymaster_blob_t* blob) {
    ASSERT_OR_RETURN_ERROR(bstr, KM_ERROR_INVALID_TAG);
    const std::vector<uint8_t>& vec = bstr->value();
    uint8_t* data = (uint8_t*)calloc(vec.size(), sizeof(uint8_t));
    if (data == nullptr) {
        return KM_ERROR_MEMORY_ALLOCATION_FAILED;
    }

    std::copy(vec.begin(), vec.end(), data);
    blob->data = data;
    blob->data_length = vec.size();

    return KM_ERROR_OK;
}

static uint32_t get_uint32_value(const keymaster_key_param_t& param) {
    switch (keymaster_tag_get_type(param.tag)) {
    case KM_ENUM:
    case KM_ENUM_REP:
        return param.enumerated;
    case KM_UINT:
    case KM_UINT_REP:
        return param.integer;
    default:
        ASSERT_OR_RETURN_ERROR(false, 0xFFFFFFFF);
    }
}

static int64_t get_uint32_value(EatSecurityLevel level) {
    return static_cast<int64_t>(level);
}

// Insert value in either the dest_integer or the dest_integer_set, whichever is provided.
static keymaster_error_t insert_integer(ASN1_INTEGER* value, ASN1_INTEGER** dest_integer,
                                        ASN1_INTEGER_SET** dest_integer_set) {
    ASSERT_OR_RETURN_ERROR((dest_integer == nullptr) ^ (dest_integer_set == nullptr),
                           KM_ERROR_UNEXPECTED_NULL_POINTER);
    ASSERT_OR_RETURN_ERROR(value, KM_ERROR_INVALID_ARGUMENT);

    if (dest_integer_set) {
        if (!*dest_integer_set) {
            *dest_integer_set = sk_ASN1_INTEGER_new_null();
        }
        if (!*dest_integer_set) {
            return KM_ERROR_MEMORY_ALLOCATION_FAILED;
        }
        if (!sk_ASN1_INTEGER_push(*dest_integer_set, value)) {
            return KM_ERROR_MEMORY_ALLOCATION_FAILED;
        }
        return KM_ERROR_OK;

    } else if (dest_integer) {
        if (*dest_integer) {
            ASN1_INTEGER_free(*dest_integer);
        }
        *dest_integer = value;
        return KM_ERROR_OK;
    }

    ASSERT_OR_RETURN_ERROR(false, KM_ERROR_UNKNOWN_ERROR);  // Should never get here.
}

// Add a repeating enum to a map going mapping its key to list of values.
static void add_repeating_enum(EatClaim key, uint32_t value,
                               std::unordered_map<EatClaim, cppbor::Array>* fields_map) {
    auto field = fields_map->find(key);
    if (field != fields_map->end()) {
        field->second.add(value);
    } else {
        fields_map->insert({key, cppbor::Array().add(value)});
    }
}

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) {
    EatClaim private_eat_tag = static_cast<EatClaim>(convert_to_eat_claim(param.tag));
    switch (keymaster_tag_get_type(param.tag)) {
    case KM_ENUM:
        dest_map->add(private_eat_tag, param.enumerated);
        break;
    case KM_ENUM_REP:
        add_repeating_enum(private_eat_tag, param.enumerated, fields_map);
        break;
    case KM_UINT:
        dest_map->add(private_eat_tag, param.integer);
        break;
    case KM_UINT_REP:
        add_repeating_enum(private_eat_tag, param.integer, fields_map);
        break;
    case KM_ULONG:
        dest_map->add(private_eat_tag, param.long_integer);
        break;
    case KM_ULONG_REP:
        add_repeating_enum(private_eat_tag, param.long_integer, fields_map);
        break;
    case KM_DATE:
        dest_map->add(private_eat_tag, param.date_time);
        break;
    case KM_BOOL:
        dest_map->add(private_eat_tag, true);
        break;
    case KM_BIGNUM:
    case KM_BYTES:
        dest_map->add(private_eat_tag, blob_to_bstr(param.blob));
        break;
    default:
        ASSERT_OR_RETURN_ERROR(false, KM_ERROR_INVALID_TAG);
    }
    return KM_ERROR_OK;
}

/**
 * Convert an IMEI encoded as a string of numbers into the UEID format defined in
 * https://tools.ietf.org/html/draft-ietf-rats-eat.
 * The resulting format is a bstr encoded as follows:
 * - Type byte: 0x03
 * - IMEI (without check digit), encoded as byte string of length 14 with each byte as the digit's
 *   value. The IMEI value encoded SHALL NOT include Luhn checksum or SVN information.
 */
keymaster_error_t imei_to_ueid(const keymaster_blob_t& imei_blob, cppbor::Bstr* out) {
    ASSERT_OR_RETURN_ERROR(imei_blob.data_length == kImeiBlobLength, KM_ERROR_INVALID_TAG);

    uint8_t ueid[kUeidLength];
    ueid[0] = kImeiTypeByte;
    // imei_blob corresponds to android.telephony.TelephonyManager#getDeviceId(), which is the
    // 15-digit IMEI (including the check digit), encoded as a string.
    for (size_t i = 1; i < kUeidLength; i++) {
        // Convert each character to its numeric value.
        ueid[i] = imei_blob.data[i - 1] - '0';  // Intentionally skip check digit at last position.
    }

    *out = cppbor::Bstr(std::pair(ueid, sizeof(ueid)));
    return KM_ERROR_OK;
}

keymaster_error_t ueid_to_imei_blob(const cppbor::Bstr* ueid, keymaster_blob_t* out) {
    ASSERT_OR_RETURN_ERROR(ueid, KM_ERROR_INVALID_TAG);
    const std::vector<uint8_t>& ueid_vec = ueid->value();
    ASSERT_OR_RETURN_ERROR(ueid_vec.size() == kUeidLength, KM_ERROR_INVALID_TAG);
    ASSERT_OR_RETURN_ERROR(ueid_vec[0] == kImeiTypeByte, KM_ERROR_INVALID_TAG);

    uint8_t* imei_string = (uint8_t*)calloc(kImeiBlobLength, sizeof(uint8_t));
    // Fill string from left to right, and calculate Luhn check digit.
    int luhn_digit_sum = 0;
    for (size_t i = 0; i < kImeiBlobLength - 1; i++) {
        uint8_t digit_i = ueid_vec[i + 1];
        // Convert digit to its string value.
        imei_string[i] = '0' + digit_i;
        luhn_digit_sum += i % 2 == 0 ? digit_i : digit_i * 2 / 10 + (digit_i * 2) % 10;
    }
    imei_string[kImeiBlobLength - 1] = '0' + (10 - luhn_digit_sum % 10) % 10;

    *out = {.data = imei_string, .data_length = kImeiBlobLength};
    return KM_ERROR_OK;
}

keymaster_error_t ec_key_size_to_eat_curve(uint32_t key_size_bits, int* curve) {
    switch (key_size_bits) {
    default:
        return KM_ERROR_UNSUPPORTED_KEY_SIZE;

    case 224:
        *curve = (int)EatEcCurve::P_224;
        break;

    case 256:
        *curve = (int)EatEcCurve::P_256;
        break;

    case 384:
        *curve = (int)EatEcCurve::P_384;
        break;

    case 521:
        *curve = (int)EatEcCurve::P_521;
        break;
    }

    return KM_ERROR_OK;
}

bool is_valid_attestation_challenge(const keymaster_blob_t& attestation_challenge) {
    // TODO(171864369): Limit apps targeting >= API 30 to attestations in the range of
    // [0, 128] bytes.
    return (attestation_challenge.data_length <= kMaximumAttestationChallengeLength);
}

// Put the contents of the keymaster AuthorizationSet auth_list into the EAT record structure.
keymaster_error_t build_eat_submod(const AuthorizationSet& auth_list,
                                   const EatSecurityLevel security_level, cppbor::Map* submod) {
    ASSERT_OR_RETURN_ERROR(submod, KM_ERROR_UNEXPECTED_NULL_POINTER);

    if (auth_list.empty()) return KM_ERROR_OK;

    submod->add(EatClaim::SECURITY_LEVEL, get_uint32_value(security_level));

    // Keep repeating fields in a separate map for easy lookup.
    // Add them to submod map in postprocessing.
    std::unordered_map<EatClaim, cppbor::Array> repeating_fields =
        std::unordered_map<EatClaim, cppbor::Array>();

    for (auto entry : auth_list) {

        switch (entry.tag) {

        default:
            // Unknown tags should only be included if they're software-enforced.
            if (security_level == EatSecurityLevel::UNRESTRICTED) {
                keymaster_error_t error = insert_unknown_tag(entry, submod, &repeating_fields);
                if (error != KM_ERROR_OK) {
                    return error;
                }
            }
            break;

        /* Tags ignored because they should never exist */
        case KM_TAG_INVALID:

        /* Tags ignored because they're not used. */
        case KM_TAG_ALL_USERS:
        case KM_TAG_EXPORTABLE:
        case KM_TAG_ECIES_SINGLE_HASH_MODE:
        case KM_TAG_KDF:

        /* Tags ignored because they're used only to provide information to operations */
        case KM_TAG_ASSOCIATED_DATA:
        case KM_TAG_NONCE:
        case KM_TAG_AUTH_TOKEN:
        case KM_TAG_MAC_LENGTH:
        case KM_TAG_ATTESTATION_CHALLENGE:
        case KM_TAG_RESET_SINCE_ID_ROTATION:

        /* Tags ignored because they have no meaning off-device */
        case KM_TAG_USER_ID:
        case KM_TAG_USER_SECURE_ID:
        case KM_TAG_BLOB_USAGE_REQUIREMENTS:

        /* Tags ignored because they're not usable by app keys */
        case KM_TAG_BOOTLOADER_ONLY:
        case KM_TAG_INCLUDE_UNIQUE_ID:
        case KM_TAG_MAX_USES_PER_BOOT:
        case KM_TAG_MIN_SECONDS_BETWEEN_OPS:
        case KM_TAG_UNIQUE_ID:

        /* Tags ignored because they contain data that should not be exported */
        case KM_TAG_APPLICATION_DATA:
        case KM_TAG_APPLICATION_ID:
        case KM_TAG_ROOT_OF_TRUST:
            continue;

        /* Non-repeating enumerations */
        case KM_TAG_ALGORITHM:
            submod->add(EatClaim::ALGORITHM, get_uint32_value(entry));
            break;
        case KM_TAG_EC_CURVE:
            submod->add(EatClaim::EC_CURVE, get_uint32_value(entry));
            break;
        case KM_TAG_USER_AUTH_TYPE:
            submod->add(EatClaim::USER_AUTH_TYPE, get_uint32_value(entry));
            break;
        case KM_TAG_ORIGIN:
            submod->add(EatClaim::ORIGIN, get_uint32_value(entry));
            break;

        /* Repeating enumerations */
        case KM_TAG_PURPOSE:
            add_repeating_enum(EatClaim::PURPOSE, get_uint32_value(entry), &repeating_fields);
            break;
        case KM_TAG_PADDING:
            add_repeating_enum(EatClaim::PADDING, get_uint32_value(entry), &repeating_fields);
            break;
        case KM_TAG_DIGEST:
            add_repeating_enum(EatClaim::DIGEST, get_uint32_value(entry), &repeating_fields);
            break;
        case KM_TAG_BLOCK_MODE:
            add_repeating_enum(EatClaim::BLOCK_MODE, get_uint32_value(entry), &repeating_fields);
            break;

        /* Non-repeating unsigned integers */
        case KM_TAG_KEY_SIZE:
            submod->add(EatClaim::KEY_SIZE, get_uint32_value(entry));
            break;
        case KM_TAG_AUTH_TIMEOUT:
            submod->add(EatClaim::AUTH_TIMEOUT, get_uint32_value(entry));
            break;
        case KM_TAG_OS_VERSION:
            submod->add(EatClaim::OS_VERSION, get_uint32_value(entry));
            break;
        case KM_TAG_OS_PATCHLEVEL:
            submod->add(EatClaim::OS_PATCHLEVEL, get_uint32_value(entry));
            break;
        case KM_TAG_VENDOR_PATCHLEVEL:
            submod->add(EatClaim::VENDOR_PATCHLEVEL, get_uint32_value(entry));
            break;
        case KM_TAG_BOOT_PATCHLEVEL:
            submod->add(EatClaim::BOOT_PATCHLEVEL, get_uint32_value(entry));
            break;
        case KM_TAG_MIN_MAC_LENGTH:
            submod->add(EatClaim::MIN_MAC_LENGTH, get_uint32_value(entry));
            break;

        /* Non-repeating long unsigned integers */
        case KM_TAG_RSA_PUBLIC_EXPONENT:
            submod->add(EatClaim::RSA_PUBLIC_EXPONENT, entry.long_integer);
            break;

        /* Dates */
        case KM_TAG_ACTIVE_DATETIME:
            submod->add(EatClaim::ACTIVE_DATETIME, entry.date_time);
            break;
        case KM_TAG_ORIGINATION_EXPIRE_DATETIME:
            submod->add(EatClaim::ORIGINATION_EXPIRE_DATETIME, entry.date_time);
            break;
        case KM_TAG_USAGE_EXPIRE_DATETIME:
            submod->add(EatClaim::USAGE_EXPIRE_DATETIME, entry.date_time);
            break;
        case KM_TAG_CREATION_DATETIME:
            submod->add(EatClaim::IAT, entry.date_time);
            break;

        /* Booleans */
        case KM_TAG_NO_AUTH_REQUIRED:
            submod->add(EatClaim::NO_AUTH_REQUIRED, true);
            break;
        case KM_TAG_ALL_APPLICATIONS:
            submod->add(EatClaim::ALL_APPLICATIONS, true);
            break;
        case KM_TAG_ROLLBACK_RESISTANT:
            submod->add(EatClaim::ROLLBACK_RESISTANT, true);
            break;
        case KM_TAG_ALLOW_WHILE_ON_BODY:
            submod->add(EatClaim::ALLOW_WHILE_ON_BODY, true);
            break;
        case KM_TAG_UNLOCKED_DEVICE_REQUIRED:
            submod->add(EatClaim::UNLOCKED_DEVICE_REQUIRED, true);
            break;
        case KM_TAG_CALLER_NONCE:
            submod->add(EatClaim::CALLER_NONCE, true);
            break;
        case KM_TAG_TRUSTED_CONFIRMATION_REQUIRED:
            submod->add(EatClaim::TRUSTED_CONFIRMATION_REQUIRED, true);
            break;
        case KM_TAG_EARLY_BOOT_ONLY:
            submod->add(EatClaim::EARLY_BOOT_ONLY, true);
            break;
        case KM_TAG_DEVICE_UNIQUE_ATTESTATION:
            submod->add(EatClaim::DEVICE_UNIQUE_ATTESTATION, true);
            break;
        case KM_TAG_IDENTITY_CREDENTIAL_KEY:
            submod->add(EatClaim::IDENTITY_CREDENTIAL_KEY, true);
            break;
        case KM_TAG_TRUSTED_USER_PRESENCE_REQUIRED:
            submod->add(EatClaim::TRUSTED_USER_PRESENCE_REQUIRED, true);
            break;
        case KM_TAG_STORAGE_KEY:
            submod->add(EatClaim::STORAGE_KEY, true);
            break;

        /* Byte arrays*/
        case KM_TAG_ATTESTATION_APPLICATION_ID:
            submod->add(EatClaim::ATTESTATION_APPLICATION_ID, blob_to_bstr(entry.blob));
            break;
        case KM_TAG_ATTESTATION_ID_BRAND:
            submod->add(EatClaim::ATTESTATION_ID_BRAND, blob_to_bstr(entry.blob));
            break;
        case KM_TAG_ATTESTATION_ID_DEVICE:
            submod->add(EatClaim::ATTESTATION_ID_DEVICE, blob_to_bstr(entry.blob));
            break;
        case KM_TAG_ATTESTATION_ID_PRODUCT:
            submod->add(EatClaim::ATTESTATION_ID_PRODUCT, blob_to_bstr(entry.blob));
            break;
        case KM_TAG_ATTESTATION_ID_SERIAL:
            submod->add(EatClaim::ATTESTATION_ID_SERIAL, blob_to_bstr(entry.blob));
            break;
        case KM_TAG_ATTESTATION_ID_IMEI: {
            cppbor::Bstr ueid("");
            keymaster_error_t error = imei_to_ueid(entry.blob, &ueid);
            if (error != KM_ERROR_OK) return error;
            submod->add(EatClaim::UEID, ueid);
            break;
        }
        case KM_TAG_ATTESTATION_ID_MEID:
            submod->add(EatClaim::ATTESTATION_ID_MEID, blob_to_bstr(entry.blob));
            break;
        case KM_TAG_ATTESTATION_ID_MANUFACTURER:
            submod->add(EatClaim::ATTESTATION_ID_MANUFACTURER, blob_to_bstr(entry.blob));
            break;
        case KM_TAG_ATTESTATION_ID_MODEL:
            submod->add(EatClaim::ATTESTATION_ID_MODEL, blob_to_bstr(entry.blob));
            break;
        case KM_TAG_CONFIRMATION_TOKEN:
            submod->add(EatClaim::CONFIRMATION_TOKEN, blob_to_bstr(entry.blob));
            break;
        }
    }

    // Move values from repeating enums into the submod map.
    for (auto const& repeating_field : repeating_fields) {
        EatClaim key = static_cast<EatClaim>(repeating_field.first);
        submod->add(key, std::move(repeating_fields.at(key)));
    }

    int ec_curve;
    uint32_t key_size;
    if (auth_list.Contains(TAG_ALGORITHM, KM_ALGORITHM_EC) && !auth_list.Contains(TAG_EC_CURVE) &&
        auth_list.GetTagValue(TAG_KEY_SIZE, &key_size)) {
        // This must be a keymaster1 key. It's an EC key with no curve.  Insert the curve.

        keymaster_error_t error = ec_key_size_to_eat_curve(key_size, &ec_curve);
        if (error != KM_ERROR_OK) return error;

        submod->add(EatClaim::EC_CURVE, ec_curve);
    }

    return KM_ERROR_OK;
}

// Put the contents of the keymaster AuthorizationSet auth_list into the ASN.1 record structure,
// record.
keymaster_error_t build_auth_list(const AuthorizationSet& auth_list, KM_AUTH_LIST* record) {
    ASSERT_OR_RETURN_ERROR(record, KM_ERROR_UNEXPECTED_NULL_POINTER);

    if (auth_list.empty()) return KM_ERROR_OK;

    for (auto entry : auth_list) {

        ASN1_INTEGER_SET** integer_set = nullptr;
        ASN1_INTEGER** integer_ptr = nullptr;
        ASN1_OCTET_STRING** string_ptr = nullptr;
        ASN1_NULL** bool_ptr = nullptr;

        switch (entry.tag) {

        /* Tags ignored because they should never exist */
        case KM_TAG_INVALID:

        /* Tags ignored because they're not used. */
        case KM_TAG_ALL_USERS:
        case KM_TAG_EXPORTABLE:
        case KM_TAG_ECIES_SINGLE_HASH_MODE:

        /* Tags ignored because they're used only to provide information to operations */
        case KM_TAG_ASSOCIATED_DATA:
        case KM_TAG_NONCE:
        case KM_TAG_AUTH_TOKEN:
        case KM_TAG_MAC_LENGTH:
        case KM_TAG_ATTESTATION_CHALLENGE:
        case KM_TAG_KDF:

        /* Tags ignored because they're used only to provide for certificate generation */
        case KM_TAG_CERTIFICATE_SERIAL:
        case KM_TAG_CERTIFICATE_SUBJECT:
        case KM_TAG_CERTIFICATE_NOT_BEFORE:
        case KM_TAG_CERTIFICATE_NOT_AFTER:
        case KM_TAG_INCLUDE_UNIQUE_ID:
        case KM_TAG_RESET_SINCE_ID_ROTATION:

        /* Tags ignored because they have no meaning off-device */
        case KM_TAG_USER_ID:
        case KM_TAG_USER_SECURE_ID:
        case KM_TAG_BLOB_USAGE_REQUIREMENTS:

        /* Tags ignored because they're not usable by app keys */
        case KM_TAG_BOOTLOADER_ONLY:
        case KM_TAG_MAX_BOOT_LEVEL:
        case KM_TAG_MAX_USES_PER_BOOT:
        case KM_TAG_MIN_SECONDS_BETWEEN_OPS:
        case KM_TAG_STORAGE_KEY:
        case KM_TAG_UNIQUE_ID:

        /* Tags ignored because they contain data that should not be exported */
        case KM_TAG_APPLICATION_DATA:
        case KM_TAG_APPLICATION_ID:
        case KM_TAG_CONFIRMATION_TOKEN:
        case KM_TAG_ROOT_OF_TRUST:
            continue;

        /* Non-repeating enumerations */
        case KM_TAG_ALGORITHM:
            integer_ptr = &record->algorithm;
            break;
        case KM_TAG_EC_CURVE:
            integer_ptr = &record->ec_curve;
            break;
        case KM_TAG_USER_AUTH_TYPE:
            integer_ptr = &record->user_auth_type;
            break;
        case KM_TAG_ORIGIN:
            integer_ptr = &record->origin;
            break;

        /* Repeating enumerations */
        case KM_TAG_PURPOSE:
            integer_set = &record->purpose;
            break;
        case KM_TAG_PADDING:
            integer_set = &record->padding;
            break;
        case KM_TAG_DIGEST:
            integer_set = &record->digest;
            break;
        case KM_TAG_BLOCK_MODE:
            integer_set = &record->block_mode;
            break;
        case KM_TAG_RSA_OAEP_MGF_DIGEST:
            integer_set = &record->mgf_digest;
            break;

        /* Non-repeating unsigned integers */
        case KM_TAG_KEY_SIZE:
            integer_ptr = &record->key_size;
            break;
        case KM_TAG_AUTH_TIMEOUT:
            integer_ptr = &record->auth_timeout;
            break;
        case KM_TAG_OS_VERSION:
            integer_ptr = &record->os_version;
            break;
        case KM_TAG_OS_PATCHLEVEL:
            integer_ptr = &record->os_patchlevel;
            break;
        case KM_TAG_MIN_MAC_LENGTH:
            integer_ptr = &record->min_mac_length;
            break;
        case KM_TAG_BOOT_PATCHLEVEL:
            integer_ptr = &record->boot_patch_level;
            break;
        case KM_TAG_VENDOR_PATCHLEVEL:
            integer_ptr = &record->vendor_patchlevel;
            break;
        case KM_TAG_USAGE_COUNT_LIMIT:
            integer_ptr = &record->usage_count_limit;
            break;

        /* Non-repeating long unsigned integers */
        case KM_TAG_RSA_PUBLIC_EXPONENT:
            integer_ptr = &record->rsa_public_exponent;
            break;

        /* Dates */
        case KM_TAG_ACTIVE_DATETIME:
            integer_ptr = &record->active_date_time;
            break;
        case KM_TAG_ORIGINATION_EXPIRE_DATETIME:
            integer_ptr = &record->origination_expire_date_time;
            break;
        case KM_TAG_USAGE_EXPIRE_DATETIME:
            integer_ptr = &record->usage_expire_date_time;
            break;
        case KM_TAG_CREATION_DATETIME:
            integer_ptr = &record->creation_date_time;
            break;

        /* Booleans */
        case KM_TAG_NO_AUTH_REQUIRED:
            bool_ptr = &record->no_auth_required;
            break;
        case KM_TAG_ALL_APPLICATIONS:
            bool_ptr = &record->all_applications;
            break;
        case KM_TAG_ROLLBACK_RESISTANT:
            bool_ptr = &record->rollback_resistant;
            break;
        case KM_TAG_ROLLBACK_RESISTANCE:
            bool_ptr = &record->rollback_resistance;
            break;
        case KM_TAG_ALLOW_WHILE_ON_BODY:
            bool_ptr = &record->allow_while_on_body;
            break;
        case KM_TAG_UNLOCKED_DEVICE_REQUIRED:
            bool_ptr = &record->unlocked_device_required;
            break;
        case KM_TAG_CALLER_NONCE:
            bool_ptr = &record->caller_nonce;
            break;
        case KM_TAG_TRUSTED_CONFIRMATION_REQUIRED:
            bool_ptr = &record->trusted_confirmation_required;
            break;
        case KM_TAG_EARLY_BOOT_ONLY:
            bool_ptr = &record->early_boot_only;
            break;
        case KM_TAG_DEVICE_UNIQUE_ATTESTATION:
            bool_ptr = &record->device_unique_attestation;
            break;
        case KM_TAG_IDENTITY_CREDENTIAL_KEY:
            bool_ptr = &record->identity_credential_key;
            break;
        case KM_TAG_TRUSTED_USER_PRESENCE_REQUIRED:
            bool_ptr = &record->trusted_user_presence_required;
            break;

        /* Byte arrays*/
        case KM_TAG_ATTESTATION_APPLICATION_ID:
            string_ptr = &record->attestation_application_id;
            break;
        case KM_TAG_ATTESTATION_ID_BRAND:
            string_ptr = &record->attestation_id_brand;
            break;
        case KM_TAG_ATTESTATION_ID_DEVICE:
            string_ptr = &record->attestation_id_device;
            break;
        case KM_TAG_ATTESTATION_ID_PRODUCT:
            string_ptr = &record->attestation_id_product;
            break;
        case KM_TAG_ATTESTATION_ID_SERIAL:
            string_ptr = &record->attestation_id_serial;
            break;
        case KM_TAG_ATTESTATION_ID_IMEI:
            string_ptr = &record->attestation_id_imei;
            break;
        case KM_TAG_ATTESTATION_ID_MEID:
            string_ptr = &record->attestation_id_meid;
            break;
        case KM_TAG_ATTESTATION_ID_MANUFACTURER:
            string_ptr = &record->attestation_id_manufacturer;
            break;
        case KM_TAG_ATTESTATION_ID_MODEL:
            string_ptr = &record->attestation_id_model;
            break;
        }

        keymaster_tag_type_t type = keymaster_tag_get_type(entry.tag);
        switch (type) {
        case KM_ENUM:
        case KM_ENUM_REP:
        case KM_UINT:
        case KM_UINT_REP: {
            ASSERT_OR_RETURN_ERROR((keymaster_tag_repeatable(entry.tag) && integer_set) ||
                                       (!keymaster_tag_repeatable(entry.tag) && integer_ptr),
                                   KM_ERROR_INVALID_TAG);

            UniquePtr<ASN1_INTEGER, ASN1_INTEGER_Delete> value(ASN1_INTEGER_new());
            if (!value.get()) {
                return KM_ERROR_MEMORY_ALLOCATION_FAILED;
            }
            if (!ASN1_INTEGER_set(value.get(), get_uint32_value(entry))) {
                return TranslateLastOpenSslError();
            }

            insert_integer(value.release(), integer_ptr, integer_set);
            break;
        }

        case KM_ULONG:
        case KM_ULONG_REP:
        case KM_DATE: {
            ASSERT_OR_RETURN_ERROR((keymaster_tag_repeatable(entry.tag) && integer_set) ||
                                       (!keymaster_tag_repeatable(entry.tag) && integer_ptr),
                                   KM_ERROR_INVALID_TAG);

            UniquePtr<BIGNUM, BIGNUM_Delete> bn_value(BN_new());
            if (!bn_value.get()) {
                return KM_ERROR_MEMORY_ALLOCATION_FAILED;
            }

            if (type == KM_DATE) {
                if (!BN_set_u64(bn_value.get(), entry.date_time)) {
                    return TranslateLastOpenSslError();
                }
            } else {
                if (!BN_set_u64(bn_value.get(), entry.long_integer)) {
                    return TranslateLastOpenSslError();
                }
            }

            UniquePtr<ASN1_INTEGER, ASN1_INTEGER_Delete> value(
                BN_to_ASN1_INTEGER(bn_value.get(), nullptr));
            if (!value.get()) {
                return KM_ERROR_MEMORY_ALLOCATION_FAILED;
            }

            insert_integer(value.release(), integer_ptr, integer_set);
            break;
        }

        case KM_BOOL:
            ASSERT_OR_RETURN_ERROR(bool_ptr, KM_ERROR_INVALID_TAG);
            if (!*bool_ptr) *bool_ptr = ASN1_NULL_new();
            if (!*bool_ptr) return KM_ERROR_MEMORY_ALLOCATION_FAILED;
            break;

        /* Byte arrays*/
        case KM_BYTES:
            ASSERT_OR_RETURN_ERROR(string_ptr, KM_ERROR_INVALID_TAG);
            if (!*string_ptr) {
                *string_ptr = ASN1_OCTET_STRING_new();
            }
            if (!*string_ptr) {
                return KM_ERROR_MEMORY_ALLOCATION_FAILED;
            }
            if (!ASN1_OCTET_STRING_set(*string_ptr, entry.blob.data, entry.blob.data_length)) {
                return TranslateLastOpenSslError();
            }
            break;

        default:
            return KM_ERROR_UNIMPLEMENTED;
        }
    }

    keymaster_ec_curve_t ec_curve;
    uint32_t key_size;
    if (auth_list.Contains(TAG_ALGORITHM, KM_ALGORITHM_EC) &&  //
        !auth_list.Contains(TAG_EC_CURVE) &&                   //
        auth_list.GetTagValue(TAG_KEY_SIZE, &key_size)) {
        // This must be a keymaster1 key. It's an EC key with no curve.  Insert the curve if we
        // can unambiguously figure it out.

        keymaster_error_t error = EcKeySizeToCurve(key_size, &ec_curve);
        if (error != KM_ERROR_OK) return error;

        UniquePtr<ASN1_INTEGER, ASN1_INTEGER_Delete> value(ASN1_INTEGER_new());
        if (!value.get()) {
            return KM_ERROR_MEMORY_ALLOCATION_FAILED;
        }

        if (!ASN1_INTEGER_set(value.get(), ec_curve)) {
            return TranslateLastOpenSslError();
        }

        insert_integer(value.release(), &record->ec_curve, nullptr);
    }

    return KM_ERROR_OK;
}

// Construct a CBOR-encoded attestation record containing the values from sw_enforced
// and tee_enforced.
keymaster_error_t build_eat_record(const AuthorizationSet& attestation_params,
                                   AuthorizationSet sw_enforced, AuthorizationSet tee_enforced,
                                   const AttestationContext& context,
                                   std::vector<uint8_t>* eat_token) {
    ASSERT_OR_RETURN_ERROR(eat_token, KM_ERROR_UNEXPECTED_NULL_POINTER);

    cppbor::Map eat_record;
    switch (context.GetSecurityLevel()) {
    case KM_SECURITY_LEVEL_SOFTWARE:
        eat_record.add(EatClaim::SECURITY_LEVEL, get_uint32_value(EatSecurityLevel::UNRESTRICTED));
        break;
    case KM_SECURITY_LEVEL_TRUSTED_ENVIRONMENT:
        eat_record.add(EatClaim::SECURITY_LEVEL,
                       get_uint32_value(EatSecurityLevel::SECURE_RESTRICTED));
        break;
    case KM_SECURITY_LEVEL_STRONGBOX:
        eat_record.add(EatClaim::SECURITY_LEVEL, get_uint32_value(EatSecurityLevel::HARDWARE));
        break;
    default:
        return KM_ERROR_UNKNOWN_ERROR;
    }

    keymaster_error_t error;
    const AttestationContext::VerifiedBootParams* vb_params = context.GetVerifiedBootParams(&error);
    if (error != KM_ERROR_OK) return error;

    if (vb_params->verified_boot_key.data_length) {
        eat_record.add(EatClaim::VERIFIED_BOOT_KEY, blob_to_bstr(vb_params->verified_boot_key));
    }
    if (vb_params->verified_boot_hash.data_length) {
        eat_record.add(EatClaim::VERIFIED_BOOT_HASH, blob_to_bstr(vb_params->verified_boot_hash));
    }
    if (vb_params->device_locked) {
        eat_record.add(EatClaim::DEVICE_LOCKED, vb_params->device_locked);
    }

    bool verified_or_self_signed = (vb_params->verified_boot_state == KM_VERIFIED_BOOT_VERIFIED ||
                                    vb_params->verified_boot_state == KM_VERIFIED_BOOT_SELF_SIGNED);
    auto eat_boot_state = cppbor::Array()
                              .add(verified_or_self_signed)  // secure-boot-enabled
                              .add(verified_or_self_signed)  // debug-disabled
                              .add(verified_or_self_signed)  // debug-disabled-since-boot
                              .add(verified_or_self_signed)  // debug-permanent-disable
                              .add(false);  // debug-full-permanent-disable (no way to verify)
    eat_record.add(EatClaim::BOOT_STATE, std::move(eat_boot_state));
    eat_record.add(EatClaim::OFFICIAL_BUILD,
                   vb_params->verified_boot_state == KM_VERIFIED_BOOT_VERIFIED);

    eat_record.add(EatClaim::ATTESTATION_VERSION,
                   version_to_attestation_version(context.GetKmVersion()));
    eat_record.add(EatClaim::KEYMASTER_VERSION,
                   version_to_attestation_km_version(context.GetKmVersion()));

    keymaster_blob_t attestation_challenge = {nullptr, 0};
    if (!attestation_params.GetTagValue(TAG_ATTESTATION_CHALLENGE, &attestation_challenge)) {
        return KM_ERROR_ATTESTATION_CHALLENGE_MISSING;
    }

    if (!is_valid_attestation_challenge(attestation_challenge)) {
        return KM_ERROR_INVALID_INPUT_LENGTH;
    }

    eat_record.add(EatClaim::NONCE, blob_to_bstr(attestation_challenge));

    keymaster_blob_t attestation_app_id;
    if (!attestation_params.GetTagValue(TAG_ATTESTATION_APPLICATION_ID, &attestation_app_id)) {
        return KM_ERROR_ATTESTATION_APPLICATION_ID_MISSING;
    }
    // TODO: what should happen when sw_enforced already contains TAG_ATTESTATION_APPLICATION_ID?
    // (as is the case in android_keymaster_test.cpp). For now, we will ignore the provided one in
    // attestation_params if that's the case.
    keymaster_blob_t existing_app_id;
    if (!sw_enforced.GetTagValue(TAG_ATTESTATION_APPLICATION_ID, &existing_app_id)) {
        sw_enforced.push_back(TAG_ATTESTATION_APPLICATION_ID, attestation_app_id);
    }

    error = context.VerifyAndCopyDeviceIds(
        attestation_params,
        context.GetSecurityLevel() == KM_SECURITY_LEVEL_SOFTWARE ? &sw_enforced : &tee_enforced);
    if (error == KM_ERROR_UNIMPLEMENTED) {
        // The KeymasterContext implementation does not support device ID attestation. Bail out if
        // device ID attestation is being attempted.
        for (const auto& tag : kDeviceAttestationTags) {
            if (attestation_params.find(tag) != -1) {
                return KM_ERROR_CANNOT_ATTEST_IDS;
            }
        }
    } else if (error != KM_ERROR_OK) {
        return error;
    }

    if (attestation_params.Contains(TAG_DEVICE_UNIQUE_ATTESTATION) &&
        context.GetSecurityLevel() == KM_SECURITY_LEVEL_STRONGBOX) {
        eat_record.add(EatClaim::DEVICE_UNIQUE_ATTESTATION, true);
    }

    cppbor::Map software_submod;
    error = build_eat_submod(sw_enforced, EatSecurityLevel::UNRESTRICTED, &software_submod);
    if (error != KM_ERROR_OK) return error;

    cppbor::Map tee_submod;
    error = build_eat_submod(tee_enforced, EatSecurityLevel::SECURE_RESTRICTED, &tee_submod);
    if (error != KM_ERROR_OK) return error;

    if (software_submod.size() + tee_submod.size() > 0) {
        cppbor::Map submods;
        if (software_submod.size() > 0) {
            submods.add(kEatSubmodNameSoftware, std::move(software_submod));
        }
        if (tee_submod.size() > 0) {
            submods.add(kEatSubmodNameTee, std::move(tee_submod));
        }

        eat_record.add(EatClaim::SUBMODS, std::move(submods));
    }

    if (attestation_params.GetTagValue(TAG_INCLUDE_UNIQUE_ID)) {
        uint64_t creation_datetime;
        // Only check sw_enforced for TAG_CREATION_DATETIME, since it shouldn't be in tee_enforced,
        // since this implementation has no secure wall clock.
        if (!sw_enforced.GetTagValue(TAG_CREATION_DATETIME, &creation_datetime)) {
            LOG_E("Unique ID cannot be created without creation datetime", 0);
            return KM_ERROR_INVALID_KEY_BLOB;
        }

        Buffer unique_id = context.GenerateUniqueId(
            creation_datetime, attestation_app_id,
            attestation_params.GetTagValue(TAG_RESET_SINCE_ID_ROTATION), &error);
        if (error != KM_ERROR_OK) return error;

        eat_record.add(EatClaim::CTI,
                       cppbor::Bstr(std::pair(unique_id.begin(), unique_id.available_read())));
    }

    *eat_token = eat_record.encode();

    return KM_ERROR_OK;
}

std::vector<uint8_t> build_unique_id_input(uint64_t creation_date_time,
                                           const keymaster_blob_t& application_id,
                                           bool reset_since_rotation) {
    uint64_t rounded_date = creation_date_time / 2592000000LLU;
    uint8_t* serialized_date = reinterpret_cast<uint8_t*>(&rounded_date);

    std::vector<uint8_t> input;
    input.insert(input.end(), serialized_date, serialized_date + sizeof(rounded_date));
    input.insert(input.end(), application_id.data,
                 application_id.data + application_id.data_length);
    input.push_back(reset_since_rotation ? 1 : 0);
    return input;
}

Buffer generate_unique_id(const std::vector<uint8_t>& hbk, uint64_t creation_date_time,
                          const keymaster_blob_t& application_id, bool reset_since_rotation) {
    HmacSha256 hmac;
    hmac.Init(hbk.data(), hbk.size());

    std::vector<uint8_t> input =
        build_unique_id_input(creation_date_time, application_id, reset_since_rotation);
    Buffer unique_id(UNIQUE_ID_SIZE);
    hmac.Sign(input.data(), input.size(), unique_id.peek_write(), unique_id.available_write());
    unique_id.advance_write(UNIQUE_ID_SIZE);
    return unique_id;
}

// Construct an ASN1.1 DER-encoded attestation record containing the values from sw_enforced and
// tee_enforced.
keymaster_error_t 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) {
    ASSERT_OR_RETURN_ERROR(asn1_key_desc && asn1_key_desc_len, KM_ERROR_UNEXPECTED_NULL_POINTER);

    UniquePtr<KM_KEY_DESCRIPTION, KM_KEY_DESCRIPTION_Delete> key_desc(KM_KEY_DESCRIPTION_new());
    if (!key_desc.get()) return KM_ERROR_MEMORY_ALLOCATION_FAILED;

    KM_ROOT_OF_TRUST* root_of_trust = nullptr;
    if (context.GetSecurityLevel() == KM_SECURITY_LEVEL_SOFTWARE) {
        key_desc->software_enforced->root_of_trust = KM_ROOT_OF_TRUST_new();
        root_of_trust = key_desc->software_enforced->root_of_trust;
    } else {
        key_desc->tee_enforced->root_of_trust = KM_ROOT_OF_TRUST_new();
        root_of_trust = key_desc->tee_enforced->root_of_trust;
    }

    keymaster_error_t error;
    auto vb_params = context.GetVerifiedBootParams(&error);
    if (error != KM_ERROR_OK) return error;
    if (vb_params->verified_boot_key.data_length &&
        !ASN1_OCTET_STRING_set(root_of_trust->verified_boot_key, vb_params->verified_boot_key.data,
                               vb_params->verified_boot_key.data_length)) {
        return TranslateLastOpenSslError();
    }
    if (vb_params->verified_boot_hash.data_length &&
        !ASN1_OCTET_STRING_set(root_of_trust->verified_boot_hash,
                               vb_params->verified_boot_hash.data,
                               vb_params->verified_boot_hash.data_length)) {
        return TranslateLastOpenSslError();
    }

    root_of_trust->device_locked = vb_params->device_locked ? 0xFF : 0x00;
    if (!ASN1_ENUMERATED_set(root_of_trust->verified_boot_state, vb_params->verified_boot_state)) {
        return TranslateLastOpenSslError();
    }

    if (!ASN1_INTEGER_set(key_desc->attestation_version,
                          version_to_attestation_version(context.GetKmVersion())) ||
        !ASN1_ENUMERATED_set(key_desc->attestation_security_level, context.GetSecurityLevel()) ||
        !ASN1_INTEGER_set(key_desc->keymaster_version,
                          version_to_attestation_km_version(context.GetKmVersion())) ||
        !ASN1_ENUMERATED_set(key_desc->keymaster_security_level, context.GetSecurityLevel())) {
        return TranslateLastOpenSslError();
    }

    keymaster_blob_t attestation_challenge = {nullptr, 0};
    if (!attestation_params.GetTagValue(TAG_ATTESTATION_CHALLENGE, &attestation_challenge)) {
        return KM_ERROR_ATTESTATION_CHALLENGE_MISSING;
    }

    if (!is_valid_attestation_challenge(attestation_challenge)) {
        return KM_ERROR_INVALID_INPUT_LENGTH;
    }

    if (!ASN1_OCTET_STRING_set(key_desc->attestation_challenge, attestation_challenge.data,
                               attestation_challenge.data_length)) {
        return TranslateLastOpenSslError();
    }

    keymaster_blob_t attestation_app_id;
    if (!attestation_params.GetTagValue(TAG_ATTESTATION_APPLICATION_ID, &attestation_app_id)) {
        return KM_ERROR_ATTESTATION_APPLICATION_ID_MISSING;
    }
    sw_enforced.push_back(TAG_ATTESTATION_APPLICATION_ID, attestation_app_id);

    error = context.VerifyAndCopyDeviceIds(
        attestation_params,
        context.GetSecurityLevel() == KM_SECURITY_LEVEL_SOFTWARE ? &sw_enforced : &tee_enforced);
    if (error == KM_ERROR_UNIMPLEMENTED) {
        // The KeymasterContext implementation does not support device ID attestation. Bail out if
        // device ID attestation is being attempted.
        for (const auto& tag : kDeviceAttestationTags) {
            if (attestation_params.find(tag) != -1) {
                return KM_ERROR_CANNOT_ATTEST_IDS;
            }
        }
    } else if (error != KM_ERROR_OK) {
        return error;
    }

    if (attestation_params.Contains(TAG_DEVICE_UNIQUE_ATTESTATION) &&
        context.GetSecurityLevel() == KM_SECURITY_LEVEL_STRONGBOX) {
        tee_enforced.push_back(TAG_DEVICE_UNIQUE_ATTESTATION);
    };

    error = build_auth_list(sw_enforced, key_desc->software_enforced);
    if (error != KM_ERROR_OK) return error;

    error = build_auth_list(tee_enforced, key_desc->tee_enforced);
    if (error != KM_ERROR_OK) return error;

    if (attestation_params.GetTagValue(TAG_INCLUDE_UNIQUE_ID)) {
        uint64_t creation_datetime;
        // Only check sw_enforced for TAG_CREATION_DATETIME, since it shouldn't be in tee_enforced,
        // since this implementation has no secure wall clock.
        if (!sw_enforced.GetTagValue(TAG_CREATION_DATETIME, &creation_datetime)) {
            LOG_E("Unique ID cannot be created without creation datetime", 0);
            return KM_ERROR_INVALID_KEY_BLOB;
        }

        Buffer unique_id = context.GenerateUniqueId(
            creation_datetime, attestation_app_id,
            attestation_params.GetTagValue(TAG_RESET_SINCE_ID_ROTATION), &error);
        if (error != KM_ERROR_OK) return error;

        key_desc->unique_id = ASN1_OCTET_STRING_new();
        if (!key_desc->unique_id ||
            !ASN1_OCTET_STRING_set(key_desc->unique_id, unique_id.peek_read(),
                                   unique_id.available_read()))
            return TranslateLastOpenSslError();
    }

    int len = i2d_KM_KEY_DESCRIPTION(key_desc.get(), nullptr);
    if (len < 0) return TranslateLastOpenSslError();
    *asn1_key_desc_len = len;
    asn1_key_desc->reset(new (std::nothrow) uint8_t[*asn1_key_desc_len]);
    if (!asn1_key_desc->get()) return KM_ERROR_MEMORY_ALLOCATION_FAILED;
    uint8_t* p = asn1_key_desc->get();
    len = i2d_KM_KEY_DESCRIPTION(key_desc.get(), &p);
    if (len < 0) return TranslateLastOpenSslError();

    return KM_ERROR_OK;
}

// Copy all enumerated values with the specified tag from stack to auth_list.
static bool get_repeated_enums(const ASN1_INTEGER_SET* stack, keymaster_tag_t tag,
                               AuthorizationSet* auth_list) {
    ASSERT_OR_RETURN_ERROR(keymaster_tag_get_type(tag) == KM_ENUM_REP, KM_ERROR_INVALID_TAG);
    for (size_t i = 0; i < sk_ASN1_INTEGER_num(stack); ++i) {
        if (!auth_list->push_back(
                keymaster_param_enum(tag, ASN1_INTEGER_get(sk_ASN1_INTEGER_value(stack, i)))))
            return false;
    }
    return true;
}

// Add the specified integer tag/value pair to auth_list.
template <keymaster_tag_type_t Type, keymaster_tag_t Tag, typename KeymasterEnum>
static bool get_enum(const ASN1_INTEGER* asn1_int, TypedEnumTag<Type, Tag, KeymasterEnum> tag,
                     AuthorizationSet* auth_list) {
    if (!asn1_int) return true;
    return auth_list->push_back(tag, static_cast<KeymasterEnum>(ASN1_INTEGER_get(asn1_int)));
}

// Add the specified ulong tag/value pair to auth_list.
static bool get_ulong(const ASN1_INTEGER* asn1_int, keymaster_tag_t tag,
                      AuthorizationSet* auth_list) {
    if (!asn1_int) return true;
    UniquePtr<BIGNUM, BIGNUM_Delete> bn(ASN1_INTEGER_to_BN(asn1_int, nullptr));
    if (!bn.get()) return false;
    uint64_t ulong = 0;
    BN_get_u64(bn.get(), &ulong);
    return auth_list->push_back(keymaster_param_long(tag, ulong));
}

// Extract the values from the specified ASN.1 record and place them in auth_list.
keymaster_error_t extract_auth_list(const KM_AUTH_LIST* record, AuthorizationSet* auth_list) {
    if (!record) return KM_ERROR_OK;

    // Purpose
    if (!get_repeated_enums(record->purpose, TAG_PURPOSE, auth_list)) {
        return KM_ERROR_MEMORY_ALLOCATION_FAILED;
    }

    // Algorithm
    if (!get_enum(record->algorithm, TAG_ALGORITHM, auth_list)) {
        return KM_ERROR_MEMORY_ALLOCATION_FAILED;
    }

    // Key size
    if (record->key_size &&
        !auth_list->push_back(TAG_KEY_SIZE, ASN1_INTEGER_get(record->key_size))) {
        return KM_ERROR_MEMORY_ALLOCATION_FAILED;
    }

    // Block mode
    if (!get_repeated_enums(record->block_mode, TAG_BLOCK_MODE, auth_list)) {
        return KM_ERROR_MEMORY_ALLOCATION_FAILED;
    }

    // Digest
    if (!get_repeated_enums(record->digest, TAG_DIGEST, auth_list)) {
        return KM_ERROR_MEMORY_ALLOCATION_FAILED;
    }

    // Padding
    if (!get_repeated_enums(record->padding, TAG_PADDING, auth_list)) {
        return KM_ERROR_MEMORY_ALLOCATION_FAILED;
    }

    // Caller nonce
    if (record->caller_nonce && !auth_list->push_back(TAG_CALLER_NONCE)) {
        return KM_ERROR_MEMORY_ALLOCATION_FAILED;
    }

    // Min mac length
    if (!get_ulong(record->min_mac_length, TAG_MIN_MAC_LENGTH, auth_list)) {
        return KM_ERROR_MEMORY_ALLOCATION_FAILED;
    }

    // EC curve
    if (!get_enum(record->ec_curve, TAG_EC_CURVE, auth_list)) {
        return KM_ERROR_MEMORY_ALLOCATION_FAILED;
    }

    // RSA public exponent
    if (!get_ulong(record->rsa_public_exponent, TAG_RSA_PUBLIC_EXPONENT, auth_list)) {
        return KM_ERROR_MEMORY_ALLOCATION_FAILED;
    }

    // Rsa Oaep Mgf Digest
    if (!get_repeated_enums(record->mgf_digest, TAG_RSA_OAEP_MGF_DIGEST, auth_list)) {
        return KM_ERROR_MEMORY_ALLOCATION_FAILED;
    }

    // Rollback resistance
    if (record->rollback_resistance && !auth_list->push_back(TAG_ROLLBACK_RESISTANCE)) {
        return KM_ERROR_MEMORY_ALLOCATION_FAILED;
    }

    // Early boot only
    if (record->early_boot_only && !auth_list->push_back(TAG_EARLY_BOOT_ONLY)) {
        return KM_ERROR_MEMORY_ALLOCATION_FAILED;
    }

    // Active date time
    if (!get_ulong(record->active_date_time, TAG_ACTIVE_DATETIME, auth_list)) {
        return KM_ERROR_MEMORY_ALLOCATION_FAILED;
    }

    // Origination expire date time
    if (!get_ulong(record->origination_expire_date_time, TAG_ORIGINATION_EXPIRE_DATETIME,
                   auth_list)) {
        return KM_ERROR_MEMORY_ALLOCATION_FAILED;
    }

    // Usage Expire date time
    if (!get_ulong(record->usage_expire_date_time, TAG_USAGE_EXPIRE_DATETIME, auth_list)) {
        return KM_ERROR_MEMORY_ALLOCATION_FAILED;
    }

    // Usage count limit
    if (record->usage_count_limit &&
        !auth_list->push_back(TAG_USAGE_COUNT_LIMIT, ASN1_INTEGER_get(record->usage_count_limit))) {
        return KM_ERROR_MEMORY_ALLOCATION_FAILED;
    }

    // No auth required
    if (record->no_auth_required && !auth_list->push_back(TAG_NO_AUTH_REQUIRED)) {
        return KM_ERROR_MEMORY_ALLOCATION_FAILED;
    }

    // User auth type
    if (!get_enum(record->user_auth_type, TAG_USER_AUTH_TYPE, auth_list)) {
        return KM_ERROR_MEMORY_ALLOCATION_FAILED;
    }

    // Auth timeout
    if (record->auth_timeout &&
        !auth_list->push_back(TAG_AUTH_TIMEOUT, ASN1_INTEGER_get(record->auth_timeout))) {
        return KM_ERROR_MEMORY_ALLOCATION_FAILED;
    }

    // Allow while on body
    if (record->allow_while_on_body && !auth_list->push_back(TAG_ALLOW_WHILE_ON_BODY)) {
        return KM_ERROR_MEMORY_ALLOCATION_FAILED;
    }

    // trusted user presence required
    if (record->trusted_user_presence_required &&
        !auth_list->push_back(TAG_TRUSTED_USER_PRESENCE_REQUIRED)) {
        return KM_ERROR_MEMORY_ALLOCATION_FAILED;
    }

    // trusted confirmation required
    if (record->trusted_confirmation_required &&
        !auth_list->push_back(TAG_TRUSTED_CONFIRMATION_REQUIRED)) {
        return KM_ERROR_MEMORY_ALLOCATION_FAILED;
    }

    // Unlocked device required
    if (record->unlocked_device_required && !auth_list->push_back(TAG_UNLOCKED_DEVICE_REQUIRED)) {
        return KM_ERROR_MEMORY_ALLOCATION_FAILED;
    }

    // All applications
    if (record->all_applications && !auth_list->push_back(TAG_ALL_APPLICATIONS)) {
        return KM_ERROR_MEMORY_ALLOCATION_FAILED;
    }

    // Application ID
    if (record->application_id &&
        !auth_list->push_back(TAG_APPLICATION_ID, record->application_id->data,
                              record->application_id->length)) {
        return KM_ERROR_MEMORY_ALLOCATION_FAILED;
    }

    // Creation date time
    if (!get_ulong(record->creation_date_time, TAG_CREATION_DATETIME, auth_list)) {
        return KM_ERROR_MEMORY_ALLOCATION_FAILED;
    }

    // Origin
    if (!get_enum(record->origin, TAG_ORIGIN, auth_list)) {
        return KM_ERROR_MEMORY_ALLOCATION_FAILED;
    }

    // Rollback resistant
    if (record->rollback_resistant && !auth_list->push_back(TAG_ROLLBACK_RESISTANT)) {
        return KM_ERROR_MEMORY_ALLOCATION_FAILED;
    }

    // Root of trust
    if (record->root_of_trust) {
        KM_ROOT_OF_TRUST* rot = record->root_of_trust;
        if (!rot->verified_boot_key) return KM_ERROR_INVALID_KEY_BLOB;

        // Other root of trust fields are not mapped to auth set entries.
    }

    // OS Version
    if (record->os_version &&
        !auth_list->push_back(TAG_OS_VERSION, ASN1_INTEGER_get(record->os_version))) {
        return KM_ERROR_MEMORY_ALLOCATION_FAILED;
    }

    // OS Patch level
    if (record->os_patchlevel &&
        !auth_list->push_back(TAG_OS_PATCHLEVEL, ASN1_INTEGER_get(record->os_patchlevel))) {
        return KM_ERROR_MEMORY_ALLOCATION_FAILED;
    }

    // attestation application Id
    if (record->attestation_application_id &&
        !auth_list->push_back(TAG_ATTESTATION_APPLICATION_ID,
                              record->attestation_application_id->data,
                              record->attestation_application_id->length)) {
        return KM_ERROR_MEMORY_ALLOCATION_FAILED;
    }

    // Brand name
    if (record->attestation_id_brand &&
        !auth_list->push_back(TAG_ATTESTATION_ID_BRAND, record->attestation_id_brand->data,
                              record->attestation_id_brand->length)) {
        return KM_ERROR_MEMORY_ALLOCATION_FAILED;
    }

    // Device name
    if (record->attestation_id_device &&
        !auth_list->push_back(TAG_ATTESTATION_ID_DEVICE, record->attestation_id_device->data,
                              record->attestation_id_device->length)) {
        return KM_ERROR_MEMORY_ALLOCATION_FAILED;
    }

    // Product name
    if (record->attestation_id_product &&
        !auth_list->push_back(TAG_ATTESTATION_ID_PRODUCT, record->attestation_id_product->data,
                              record->attestation_id_product->length)) {
        return KM_ERROR_MEMORY_ALLOCATION_FAILED;
    }

    // Serial number
    if (record->attestation_id_serial &&
        !auth_list->push_back(TAG_ATTESTATION_ID_SERIAL, record->attestation_id_serial->data,
                              record->attestation_id_serial->length)) {
        return KM_ERROR_MEMORY_ALLOCATION_FAILED;
    }

    // IMEI
    if (record->attestation_id_imei &&
        !auth_list->push_back(TAG_ATTESTATION_ID_IMEI, record->attestation_id_imei->data,
                              record->attestation_id_imei->length)) {
        return KM_ERROR_MEMORY_ALLOCATION_FAILED;
    }

    // MEID
    if (record->attestation_id_meid &&
        !auth_list->push_back(TAG_ATTESTATION_ID_MEID, record->attestation_id_meid->data,
                              record->attestation_id_meid->length)) {
        return KM_ERROR_MEMORY_ALLOCATION_FAILED;
    }

    // Manufacturer name
    if (record->attestation_id_manufacturer &&
        !auth_list->push_back(TAG_ATTESTATION_ID_MANUFACTURER,
                              record->attestation_id_manufacturer->data,
                              record->attestation_id_manufacturer->length)) {
        return KM_ERROR_MEMORY_ALLOCATION_FAILED;
    }

    // Model name
    if (record->attestation_id_model &&
        !auth_list->push_back(TAG_ATTESTATION_ID_MODEL, record->attestation_id_model->data,
                              record->attestation_id_model->length)) {
        return KM_ERROR_MEMORY_ALLOCATION_FAILED;
    }

    // vendor patch level
    if (record->vendor_patchlevel &&
        !auth_list->push_back(TAG_VENDOR_PATCHLEVEL, ASN1_INTEGER_get(record->vendor_patchlevel))) {
        return KM_ERROR_MEMORY_ALLOCATION_FAILED;
    }

    // boot patch level
    if (record->boot_patch_level &&
        !auth_list->push_back(TAG_BOOT_PATCHLEVEL, ASN1_INTEGER_get(record->boot_patch_level))) {
        return KM_ERROR_MEMORY_ALLOCATION_FAILED;
    }

    // device unique attestation
    if (record->device_unique_attestation && !auth_list->push_back(TAG_DEVICE_UNIQUE_ATTESTATION)) {
        return KM_ERROR_MEMORY_ALLOCATION_FAILED;
    }

    // identity credential key
    if (record->identity_credential_key && !auth_list->push_back(TAG_IDENTITY_CREDENTIAL_KEY)) {
        return KM_ERROR_MEMORY_ALLOCATION_FAILED;
    }

    return KM_ERROR_OK;
}

// Parse the DER-encoded attestation record, placing the results in keymaster_version,
// attestation_challenge, software_enforced, tee_enforced and unique_id.
keymaster_error_t 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) {
    const uint8_t* p = asn1_key_desc;
    UniquePtr<KM_KEY_DESCRIPTION, KM_KEY_DESCRIPTION_Delete> record(
        d2i_KM_KEY_DESCRIPTION(nullptr, &p, asn1_key_desc_len));
    if (!record.get()) return TranslateLastOpenSslError();

    *attestation_version = ASN1_INTEGER_get(record->attestation_version);
    *attestation_security_level = static_cast<keymaster_security_level_t>(
        ASN1_ENUMERATED_get(record->attestation_security_level));
    *keymaster_version = ASN1_INTEGER_get(record->keymaster_version);
    *keymaster_security_level = static_cast<keymaster_security_level_t>(
        ASN1_ENUMERATED_get(record->keymaster_security_level));

    attestation_challenge->data =
        dup_buffer(record->attestation_challenge->data, record->attestation_challenge->length);
    attestation_challenge->data_length = record->attestation_challenge->length;

    unique_id->data = dup_buffer(record->unique_id->data, record->unique_id->length);
    unique_id->data_length = record->unique_id->length;

    keymaster_error_t error = extract_auth_list(record->software_enforced, software_enforced);
    if (error != KM_ERROR_OK) return error;

    return extract_auth_list(record->tee_enforced, tee_enforced);
}

keymaster_error_t 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) {
    const uint8_t* p = asn1_key_desc;
    UniquePtr<KM_KEY_DESCRIPTION, KM_KEY_DESCRIPTION_Delete> record(
        d2i_KM_KEY_DESCRIPTION(nullptr, &p, asn1_key_desc_len));
    if (!record.get()) {
        return TranslateLastOpenSslError();
    }
    if (!record->tee_enforced) {
        return KM_ERROR_INVALID_ARGUMENT;
    }
    if (!record->tee_enforced->root_of_trust) {
        return KM_ERROR_INVALID_ARGUMENT;
    }
    if (!record->tee_enforced->root_of_trust->verified_boot_key) {
        return KM_ERROR_INVALID_ARGUMENT;
    }
    KM_ROOT_OF_TRUST* root_of_trust = record->tee_enforced->root_of_trust;
    verified_boot_key->data = dup_buffer(root_of_trust->verified_boot_key->data,
                                         root_of_trust->verified_boot_key->length);
    verified_boot_key->data_length = root_of_trust->verified_boot_key->length;
    *verified_boot_state = static_cast<keymaster_verified_boot_t>(
        ASN1_ENUMERATED_get(root_of_trust->verified_boot_state));
    *device_locked = root_of_trust->device_locked;
    return KM_ERROR_OK;
}

// Parse the EAT-encoded attestation record, placing the results in keymaster_version,
// attestation_challenge, software_enforced, tee_enforced and unique_id.
keymaster_error_t 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) {
    auto [top_level_item, next_pos, error] = cppbor::parse(eat_key_desc, eat_key_desc_len);
    ASSERT_OR_RETURN_ERROR(top_level_item, KM_ERROR_INVALID_TAG);
    const cppbor::Map* eat_map = top_level_item->asMap();
    ASSERT_OR_RETURN_ERROR(eat_map, KM_ERROR_INVALID_TAG);
    bool verified_or_self_signed = false;

    for (size_t i = 0; i < eat_map->size(); i++) {
        auto& [key_item, value_item] = (*eat_map)[i];
        const cppbor::Int* key = key_item->asInt();
        ASSERT_OR_RETURN_ERROR(key, (KM_ERROR_INVALID_TAG));

        // The following values will either hold the typed value, or be null (if not the right
        // type).
        const cppbor::Int* int_value = value_item->asInt();
        const cppbor::Bstr* bstr_value = value_item->asBstr();
        const cppbor::Simple* simple_value = value_item->asSimple();
        const cppbor::Array* array_value = value_item->asArray();
        const cppbor::Map* map_value = value_item->asMap();

        keymaster_error_t error;
        switch ((EatClaim)key->value()) {
        default:
            unexpected_claims->push_back(key->value());
            break;
        case EatClaim::ATTESTATION_VERSION:
            ASSERT_OR_RETURN_ERROR(int_value, KM_ERROR_INVALID_TAG);
            *attestation_version = int_value->value();
            break;
        case EatClaim::SECURITY_LEVEL:
            ASSERT_OR_RETURN_ERROR(int_value, KM_ERROR_INVALID_TAG);
            switch ((EatSecurityLevel)int_value->value()) {
            // TODO: Is my assumption correct that the security level of the attestation data should
            // always be equal to the security level of keymint, as the attestation data always
            // lives in the top-level module?
            case EatSecurityLevel::UNRESTRICTED:
                *keymaster_security_level = *attestation_security_level =
                    KM_SECURITY_LEVEL_SOFTWARE;
                break;
            case EatSecurityLevel::SECURE_RESTRICTED:
                *keymaster_security_level = *attestation_security_level =
                    KM_SECURITY_LEVEL_TRUSTED_ENVIRONMENT;
                break;
            case EatSecurityLevel::HARDWARE:
                *keymaster_security_level = *attestation_security_level =
                    KM_SECURITY_LEVEL_STRONGBOX;
                break;
            default:
                return KM_ERROR_INVALID_TAG;
            }
            break;
        case EatClaim::KEYMASTER_VERSION:
            ASSERT_OR_RETURN_ERROR(int_value, KM_ERROR_INVALID_TAG);
            *keymaster_version = int_value->value();
            break;
        case EatClaim::SUBMODS:
            ASSERT_OR_RETURN_ERROR(map_value, KM_ERROR_INVALID_TAG);
            for (size_t j = 0; j < map_value->size(); j++) {
                auto& [submod_key, submod_value] = (*map_value)[j];
                const cppbor::Map* submod_map = submod_value->asMap();
                ASSERT_OR_RETURN_ERROR(submod_map, KM_ERROR_INVALID_TAG);
                error = parse_eat_submod(submod_map, software_enforced, tee_enforced);
                if (error != KM_ERROR_OK) return error;
            }
            break;
        case EatClaim::CTI:
            error = bstr_to_blob(bstr_value, unique_id);
            if (error != KM_ERROR_OK) return error;
            break;
        case EatClaim::NONCE:
            error = bstr_to_blob(bstr_value, attestation_challenge);
            if (error != KM_ERROR_OK) return error;
            break;
        case EatClaim::VERIFIED_BOOT_KEY:
            error = bstr_to_blob(bstr_value, verified_boot_key);
            if (error != KM_ERROR_OK) return error;
            break;
        case EatClaim::VERIFIED_BOOT_HASH:
            // Not parsing this for now.
            break;
        case EatClaim::DEVICE_UNIQUE_ATTESTATION:
            if (value_item->asSimple() == nullptr || !value_item->asSimple()->asBool()->value()) {
                return KM_ERROR_INVALID_TAG;
            }
            // Not parsing this for now.
            break;
        case EatClaim::DEVICE_LOCKED:
            ASSERT_OR_RETURN_ERROR(simple_value->asBool(), KM_ERROR_INVALID_TAG);
            *device_locked = simple_value->asBool()->value();
            break;
        case EatClaim::BOOT_STATE:
            ASSERT_OR_RETURN_ERROR(array_value, KM_ERROR_INVALID_TAG);
            ASSERT_OR_RETURN_ERROR(array_value->size() == 5, KM_ERROR_INVALID_TAG);
            ASSERT_OR_RETURN_ERROR((*array_value)[4]->asSimple()->asBool()->value() == false,
                                   KM_ERROR_INVALID_TAG);
            verified_or_self_signed = (*array_value)[0]->asSimple()->asBool()->value();
            ASSERT_OR_RETURN_ERROR(verified_or_self_signed ==
                                       (*array_value)[1]->asSimple()->asBool()->value(),
                                   KM_ERROR_INVALID_TAG);
            ASSERT_OR_RETURN_ERROR(verified_or_self_signed ==
                                       (*array_value)[2]->asSimple()->asBool()->value(),
                                   KM_ERROR_INVALID_TAG);
            ASSERT_OR_RETURN_ERROR(verified_or_self_signed ==
                                       (*array_value)[3]->asSimple()->asBool()->value(),
                                   KM_ERROR_INVALID_TAG);
            break;
        case EatClaim::OFFICIAL_BUILD:
            *verified_boot_state = KM_VERIFIED_BOOT_VERIFIED;
            break;
        }
    }

    if (*verified_boot_state == KM_VERIFIED_BOOT_VERIFIED) {
        (void)(verified_boot_state);
        // TODO: re-enable this
        // ASSERT_OR_RETURN_ERROR(verified_or_self_signed, KM_ERROR_INVALID_TAG);
    } else {
        *verified_boot_state =
            verified_or_self_signed ? KM_VERIFIED_BOOT_SELF_SIGNED : KM_VERIFIED_BOOT_UNVERIFIED;
    }

    return KM_ERROR_OK;
}

keymaster_error_t parse_submod_values(AuthorizationSetBuilder* set_builder,
                                      int* auth_set_security_level, const cppbor::Map* submod_map) {
    ASSERT_OR_RETURN_ERROR(set_builder, KM_ERROR_UNEXPECTED_NULL_POINTER);
    for (size_t i = 0; i < submod_map->size(); i++) {
        auto& [key_item, value_item] = (*submod_map)[i];
        const cppbor::Int* key_int = key_item->asInt();
        ASSERT_OR_RETURN_ERROR(key_int, KM_ERROR_INVALID_TAG);
        int key = key_int->value();
        keymaster_error_t error;
        keymaster_blob_t blob;

        switch ((EatClaim)key) {
        default:
            return KM_ERROR_INVALID_TAG;
        case EatClaim::ALGORITHM:
            ASSERT_OR_RETURN_ERROR(value_item->asInt(), KM_ERROR_INVALID_TAG);
            set_builder->Authorization(
                TAG_ALGORITHM, static_cast<keymaster_algorithm_t>(value_item->asInt()->value()));
            break;
        case EatClaim::EC_CURVE:
            ASSERT_OR_RETURN_ERROR(value_item->asInt(), KM_ERROR_INVALID_TAG);
            set_builder->Authorization(
                TAG_EC_CURVE, static_cast<keymaster_ec_curve_t>(value_item->asInt()->value()));
            break;
        case EatClaim::USER_AUTH_TYPE:
            ASSERT_OR_RETURN_ERROR(value_item->asInt(), KM_ERROR_INVALID_TAG);
            set_builder->Authorization(TAG_USER_AUTH_TYPE, static_cast<hw_authenticator_type_t>(
                                                               value_item->asInt()->value()));
            break;
        case EatClaim::ORIGIN:
            ASSERT_OR_RETURN_ERROR(value_item->asInt(), KM_ERROR_INVALID_TAG);
            set_builder->Authorization(
                TAG_ORIGIN, static_cast<keymaster_key_origin_t>(value_item->asInt()->value()));
            break;
        case EatClaim::PURPOSE:
            for (size_t j = 0; j < value_item->asArray()->size(); j++) {
                set_builder->Authorization(TAG_PURPOSE,
                                           static_cast<keymaster_purpose_t>(
                                               (*value_item->asArray())[j]->asInt()->value()));
            }
            break;
        case EatClaim::PADDING:
            for (size_t j = 0; j < value_item->asArray()->size(); j++) {
                set_builder->Authorization(TAG_PADDING,
                                           static_cast<keymaster_padding_t>(
                                               (*value_item->asArray())[j]->asInt()->value()));
            }
            break;
        case EatClaim::DIGEST:
            for (size_t j = 0; j < value_item->asArray()->size(); j++) {
                set_builder->Authorization(
                    TAG_DIGEST,
                    static_cast<keymaster_digest_t>((*value_item->asArray())[j]->asInt()->value()));
            }
            break;
        case EatClaim::BLOCK_MODE:
            for (size_t j = 0; j < value_item->asArray()->size(); j++) {
                set_builder->Authorization(TAG_BLOCK_MODE,
                                           static_cast<keymaster_block_mode_t>(
                                               (*value_item->asArray())[j]->asInt()->value()));
            }
            break;
        case EatClaim::KEY_SIZE:
            ASSERT_OR_RETURN_ERROR(value_item->asInt(), KM_ERROR_INVALID_TAG);
            set_builder->Authorization(TAG_KEY_SIZE, value_item->asInt()->value());
            break;
        case EatClaim::AUTH_TIMEOUT:
            ASSERT_OR_RETURN_ERROR(value_item->asInt(), KM_ERROR_INVALID_TAG);
            set_builder->Authorization(TAG_AUTH_TIMEOUT, value_item->asInt()->value());
            break;
        case EatClaim::OS_VERSION:
            ASSERT_OR_RETURN_ERROR(value_item->asInt(), KM_ERROR_INVALID_TAG);
            set_builder->Authorization(TAG_OS_VERSION, value_item->asInt()->value());
            break;
        case EatClaim::OS_PATCHLEVEL:
            ASSERT_OR_RETURN_ERROR(value_item->asInt(), KM_ERROR_INVALID_TAG);
            set_builder->Authorization(TAG_OS_PATCHLEVEL, value_item->asInt()->value());
            break;
        case EatClaim::MIN_MAC_LENGTH:
            ASSERT_OR_RETURN_ERROR(value_item->asInt(), KM_ERROR_INVALID_TAG);
            set_builder->Authorization(TAG_MIN_MAC_LENGTH, value_item->asInt()->value());
            break;
        case EatClaim::BOOT_PATCHLEVEL:
            ASSERT_OR_RETURN_ERROR(value_item->asInt(), KM_ERROR_INVALID_TAG);
            set_builder->Authorization(TAG_BOOT_PATCHLEVEL, value_item->asInt()->value());
            break;
        case EatClaim::VENDOR_PATCHLEVEL:
            ASSERT_OR_RETURN_ERROR(value_item->asInt(), KM_ERROR_INVALID_TAG);
            set_builder->Authorization(TAG_VENDOR_PATCHLEVEL, value_item->asInt()->value());
            break;
        case EatClaim::RSA_PUBLIC_EXPONENT:
            ASSERT_OR_RETURN_ERROR(value_item->asInt(), KM_ERROR_INVALID_TAG);
            set_builder->Authorization(TAG_RSA_PUBLIC_EXPONENT, value_item->asInt()->value());
            break;
        case EatClaim::ACTIVE_DATETIME:
            ASSERT_OR_RETURN_ERROR(value_item->asInt(), KM_ERROR_INVALID_TAG);
            set_builder->Authorization(TAG_ACTIVE_DATETIME, value_item->asInt()->value());
            break;
        case EatClaim::ORIGINATION_EXPIRE_DATETIME:
            ASSERT_OR_RETURN_ERROR(value_item->asInt(), KM_ERROR_INVALID_TAG);
            set_builder->Authorization(TAG_ORIGINATION_EXPIRE_DATETIME,
                                       value_item->asInt()->value());
            break;
        case EatClaim::USAGE_EXPIRE_DATETIME:
            ASSERT_OR_RETURN_ERROR(value_item->asInt(), KM_ERROR_INVALID_TAG);
            set_builder->Authorization(TAG_USAGE_EXPIRE_DATETIME, value_item->asInt()->value());
            break;
        case EatClaim::IAT:
            ASSERT_OR_RETURN_ERROR(value_item->asInt(), KM_ERROR_INVALID_TAG);
            set_builder->Authorization(TAG_CREATION_DATETIME, value_item->asInt()->value());
            break;
        case EatClaim::NO_AUTH_REQUIRED:
            if (value_item->asSimple() == nullptr || !value_item->asSimple()->asBool()->value()) {
                return KM_ERROR_INVALID_TAG;
            }
            set_builder->Authorization(TAG_NO_AUTH_REQUIRED);
            break;
        case EatClaim::ALL_APPLICATIONS:
            if (value_item->asSimple() == nullptr || !value_item->asSimple()->asBool()->value()) {
                return KM_ERROR_INVALID_TAG;
            }
            set_builder->Authorization(TAG_ALL_APPLICATIONS);
            break;
        case EatClaim::ROLLBACK_RESISTANT:
            if (value_item->asSimple() == nullptr || !value_item->asSimple()->asBool()->value()) {
                return KM_ERROR_INVALID_TAG;
            }
            set_builder->Authorization(TAG_ROLLBACK_RESISTANT);
            break;
        case EatClaim::ALLOW_WHILE_ON_BODY:
            if (value_item->asSimple() == nullptr || !value_item->asSimple()->asBool()->value()) {
                return KM_ERROR_INVALID_TAG;
            }
            set_builder->Authorization(TAG_ALLOW_WHILE_ON_BODY);
            break;
        case EatClaim::UNLOCKED_DEVICE_REQUIRED:
            if (value_item->asSimple() == nullptr || !value_item->asSimple()->asBool()->value()) {
                return KM_ERROR_INVALID_TAG;
            }
            set_builder->Authorization(TAG_UNLOCKED_DEVICE_REQUIRED);
            break;
        case EatClaim::CALLER_NONCE:
            if (value_item->asSimple() == nullptr || !value_item->asSimple()->asBool()->value()) {
                return KM_ERROR_INVALID_TAG;
            }
            set_builder->Authorization(TAG_CALLER_NONCE);
            break;
        case EatClaim::TRUSTED_CONFIRMATION_REQUIRED:
            if (value_item->asSimple() == nullptr || !value_item->asSimple()->asBool()->value()) {
                return KM_ERROR_INVALID_TAG;
            }
            set_builder->Authorization(TAG_TRUSTED_CONFIRMATION_REQUIRED);
            break;
        case EatClaim::EARLY_BOOT_ONLY:
            if (value_item->asSimple() == nullptr || !value_item->asSimple()->asBool()->value()) {
                return KM_ERROR_INVALID_TAG;
            }
            set_builder->Authorization(TAG_EARLY_BOOT_ONLY);
            break;
        case EatClaim::IDENTITY_CREDENTIAL_KEY:
            if (value_item->asSimple() == nullptr || !value_item->asSimple()->asBool()->value()) {
                return KM_ERROR_INVALID_TAG;
            }
            set_builder->Authorization(TAG_IDENTITY_CREDENTIAL_KEY);
            break;
        case EatClaim::STORAGE_KEY:
            if (value_item->asSimple() == nullptr || !value_item->asSimple()->asBool()->value()) {
                return KM_ERROR_INVALID_TAG;
            }
            set_builder->Authorization(TAG_STORAGE_KEY);
            break;
        case EatClaim::TRUSTED_USER_PRESENCE_REQUIRED:
            if (value_item->asSimple() == nullptr || !value_item->asSimple()->asBool()->value()) {
                return KM_ERROR_INVALID_TAG;
            }
            set_builder->Authorization(TAG_TRUSTED_USER_PRESENCE_REQUIRED);
            break;
        case EatClaim::DEVICE_UNIQUE_ATTESTATION:
            if (value_item->asSimple() == nullptr || !value_item->asSimple()->asBool()->value()) {
                return KM_ERROR_INVALID_TAG;
            }
            set_builder->Authorization(TAG_DEVICE_UNIQUE_ATTESTATION);
            break;
        case EatClaim::APPLICATION_ID:
            error = bstr_to_blob(value_item->asBstr(), &blob);
            if (error != KM_ERROR_OK) return error;
            set_builder->Authorization(TAG_APPLICATION_ID, blob);
            break;
        case EatClaim::ATTESTATION_APPLICATION_ID:
            error = bstr_to_blob(value_item->asBstr(), &blob);
            if (error != KM_ERROR_OK) return error;
            set_builder->Authorization(TAG_ATTESTATION_APPLICATION_ID, blob);
            break;
        case EatClaim::ATTESTATION_ID_BRAND:
            error = bstr_to_blob(value_item->asBstr(), &blob);
            if (error != KM_ERROR_OK) return error;
            set_builder->Authorization(TAG_ATTESTATION_ID_BRAND, blob);
            break;
        case EatClaim::ATTESTATION_ID_DEVICE:
            error = bstr_to_blob(value_item->asBstr(), &blob);
            if (error != KM_ERROR_OK) return error;
            set_builder->Authorization(TAG_ATTESTATION_ID_DEVICE, blob);
            break;
        case EatClaim::ATTESTATION_ID_PRODUCT:
            error = bstr_to_blob(value_item->asBstr(), &blob);
            if (error != KM_ERROR_OK) return error;
            set_builder->Authorization(TAG_ATTESTATION_ID_PRODUCT, blob);
            break;
        case EatClaim::ATTESTATION_ID_SERIAL:
            error = bstr_to_blob(value_item->asBstr(), &blob);
            if (error != KM_ERROR_OK) return error;
            set_builder->Authorization(TAG_ATTESTATION_ID_SERIAL, blob);
            break;
        case EatClaim::UEID:
            error = ueid_to_imei_blob(value_item->asBstr(), &blob);
            if (error != KM_ERROR_OK) return error;
            set_builder->Authorization(TAG_ATTESTATION_ID_IMEI, blob);
            break;
        case EatClaim::ATTESTATION_ID_MEID:
            error = bstr_to_blob(value_item->asBstr(), &blob);
            if (error != KM_ERROR_OK) return error;
            set_builder->Authorization(TAG_ATTESTATION_ID_MEID, blob);
            break;
        case EatClaim::ATTESTATION_ID_MANUFACTURER:
            error = bstr_to_blob(value_item->asBstr(), &blob);
            if (error != KM_ERROR_OK) return error;
            set_builder->Authorization(TAG_ATTESTATION_ID_MANUFACTURER, blob);
            break;
        case EatClaim::ATTESTATION_ID_MODEL:
            error = bstr_to_blob(value_item->asBstr(), &blob);
            if (error != KM_ERROR_OK) return error;
            set_builder->Authorization(TAG_ATTESTATION_ID_MODEL, blob);
            break;
        case EatClaim::CONFIRMATION_TOKEN:
            error = bstr_to_blob(value_item->asBstr(), &blob);
            if (error != KM_ERROR_OK) return error;
            set_builder->Authorization(TAG_CONFIRMATION_TOKEN, blob);
            break;
        case EatClaim::SECURITY_LEVEL:
            ASSERT_OR_RETURN_ERROR(value_item->asInt(), KM_ERROR_INVALID_TAG);
            *auth_set_security_level = value_item->asInt()->value();
        }
    }

    return KM_ERROR_OK;
}

keymaster_error_t parse_eat_submod(const cppbor::Map* submod_values,
                                   AuthorizationSet* software_enforced,
                                   AuthorizationSet* tee_enforced) {
    AuthorizationSetBuilder auth_set_builder;
    int auth_set_security_level = 0;
    keymaster_error_t error =
        parse_submod_values(&auth_set_builder, &auth_set_security_level, submod_values);
    if (error) return error;
    switch ((EatSecurityLevel)auth_set_security_level) {
    case EatSecurityLevel::HARDWARE:
        // Hardware attestation should never occur in a submod of another EAT.
        [[fallthrough]];
    default:
        return KM_ERROR_INVALID_TAG;
    case EatSecurityLevel::UNRESTRICTED:
        *software_enforced = AuthorizationSet(auth_set_builder);
        break;
    case EatSecurityLevel::SECURE_RESTRICTED:
        *tee_enforced = AuthorizationSet(auth_set_builder);
        break;
    }

    return KM_ERROR_OK;
}
}  // namespace keymaster