clerk-report 0.5.0

Verification of attested YAXI system versions
Documentation
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
use std::{
    collections::HashMap,
    io::{BufReader, Read, Seek},
};

use ecdsa::signature::Verifier as EcdsaVerifier;
use rsa::pkcs8::DecodePublicKey;
use rsa::sha2::Sha384;
use rsa::signature::Verifier as RsaVerifier;
use sev::certs::snp::ecdsa::Signature;
use sev::firmware::guest::{AttestationReport, GuestPolicy, KeyInfo, PlatformInfo, Version};
use sev::firmware::host::TcbVersion;
use x509_parser::{
    certificate::X509Certificate, error::PEMError, num_bigint::BigUint, pem::Pem,
    prelude::parse_x509_pem,
};

use crate::ReportSignature;

mod vcek {
    //! This module contains the code for verifying a Versioned Chip Endorsement Key's certificate
    //! chain (VCEK in short).
    //!
    //! Our verification code can make assumptions that a general web PKI can't do:
    //!
    //! - According to <https://www.amd.com/content/dam/amd/en/documents/epyc-technical-docs/specifications/57230.pdf> 2.1, we always have a
    //!   certificate chain of length 3: AMD Root Key (ARK), AMD SEV Signing Key (ASK), VCEK
    //! - AMD gives us the first two certs from the chain, so we always only have to deal with
    //!   one unknown cert
    //! - We know some things about the VCEK (e.g. uses P-384 with SHA-384)

    use der::{asn1::Ia5String, Decode};
    use oid_registry::{asn1_rs::oid, Oid, OID_KEY_TYPE_EC_PUBLIC_KEY, OID_PKCS1_RSASSAPSS};
    use p384::pkcs8::DecodePublicKey;
    use sev::firmware::host::TcbVersion;
    use x509_parser::{
        num_bigint::BigUint,
        prelude::{PEMError, Pem},
    };

    use super::{Error, RootStore};

    const OID_PRODUCT_NAME: Oid<'static> = oid!(1.3.6 .1 .4 .1 .3704 .1 .2);
    const OID_BOOT_LOADER: Oid<'static> = oid!(1.3.6 .1 .4 .1 .3704 .1 .3 .1);
    const OID_TEE: Oid<'static> = oid!(1.3.6 .1 .4 .1 .3704 .1 .3 .2);
    const OID_SNP: Oid<'static> = oid!(1.3.6 .1 .4 .1 .3704 .1 .3 .3);
    const OID_UCODE: Oid<'static> = oid!(1.3.6 .1 .4 .1 .3704 .1 .3 .8);
    const OID_FMC: Oid<'static> = oid!(1.3.6 .1 .4 .1 .3704 .1 .3 .9);

    #[derive(Debug, Clone, Copy)]
    pub enum Product {
        Milan,
        Genoa,
        Turin,
    }

    struct VcekExpected {}
    struct LooksLikeVcek {
        possible_verifying_key: p384::ecdsa::VerifyingKey,
        product_in_cert: Product,
        cert_data: Vec<u8>,
        signature: Vec<u8>,
        tcb_version: TcbVersion,
    }

    struct SignedByKnownAmdRoot {
        product: Product,
        verifying_key: p384::ecdsa::VerifyingKey,
    }

    // Handy alias so we don't have to write out the iterator constraint all the time
    trait Chain: Iterator<Item = Result<Pem, PEMError>> {}
    impl<I: Iterator<Item = Result<Pem, PEMError>>> Chain for I {}

    struct VcekChainVerifier<I: Chain, S> {
        chain: I,
        state: S,
    }

    impl<I: Chain, S> VcekChainVerifier<I, S> {
        fn next_pem(&mut self) -> Result<Pem, Error> {
            self.chain
                .next()
                .map(|result| result.map_err(Error::DecodeError))
                .ok_or(Error::ChainBroken)?
        }
    }

    impl<I: Chain> VcekChainVerifier<I, VcekExpected> {
        fn new(chain: I) -> Self {
            Self {
                chain,
                state: VcekExpected {},
            }
        }
    }

    impl<I: Chain> VcekChainVerifier<I, LooksLikeVcek> {
        fn from(mut current: VcekChainVerifier<I, VcekExpected>) -> Result<Self, Error> {
            let pem = current.next_pem()?;
            if pem.label != "CERTIFICATE" {
                return Err(Error::NotACertificate);
            }
            let cert = pem.parse_x509().map_err(|_| Error::ParseError)?;

            // See also https://www.amd.com/content/dam/amd/en/documents/epyc-technical-docs/specifications/57230.pdf chapter 3 ("VCEK Certificate Format")

            // VCEK should not be a CA certificate
            if cert.is_ca() {
                return Err(Error::WrongBasicConstraints);
            }

            // Should have serial number 0
            if cert.serial != BigUint::from(0usize) {
                return Err(Error::ChainBroken);
            }

            if cert.signature_algorithm.algorithm != OID_PKCS1_RSASSAPSS {
                return Err(Error::ChainBroken);
            }

            let Some(product_name) = cert
                .get_extension_unique(&OID_PRODUCT_NAME)
                .ok()
                .flatten()
                .and_then(|ext| Ia5String::from_der(ext.value).ok())
            else {
                return Err(Error::ChainBroken);
            };

            // `product_name` includes silicon stepping, hence the prefix match (N.B. but only
            // seems to be the case for Milan)
            let product = match product_name.as_str() {
                s if s.starts_with("Genoa") => Product::Genoa,
                s if s.starts_with("Milan") => Product::Milan,
                s if s.starts_with("Turin") => Product::Turin,
                _ => return Err(Error::UnknownProductName(product_name.to_string())),
            };

            let get_u8 = |oid: Oid| {
                cert.get_extension_unique(&oid)
                    .ok()
                    .flatten()
                    .and_then(|ext| u8::from_der(ext.value).ok())
                    .ok_or(Error::ChainBroken)
            };

            let bootloader = get_u8(OID_BOOT_LOADER)?;
            let tee = get_u8(OID_TEE)?;
            let snp = get_u8(OID_SNP)?;
            let microcode = get_u8(OID_UCODE)?;

            let tcb_version = match product {
                Product::Milan | Product::Genoa => TcbVersion {
                    fmc: None,
                    bootloader,
                    tee,
                    snp,
                    microcode,
                },
                Product::Turin => {
                    let fmc = get_u8(OID_FMC)?;
                    TcbVersion {
                        fmc: Some(fmc),
                        bootloader,
                        tee,
                        snp,
                        microcode,
                    }
                }
            };

            if cert
                .subject
                .iter_common_name()
                .next()
                .map(|cn| cn.as_str().map_err(|_| Error::ChainBroken))
                .ok_or(Error::ChainBroken)??
                != "SEV-VCEK"
            {
                return Err(Error::ChainBroken);
            }

            // An EC key is expected
            if cert.public_key().algorithm.algorithm != OID_KEY_TYPE_EC_PUBLIC_KEY {
                return Err(Error::ChainBroken);
            }
            let possible_verifying_key =
                p384::ecdsa::VerifyingKey::from_public_key_der(cert.public_key().raw)
                    .map_err(|_| Error::ChainBroken)?;

            Ok(Self {
                chain: current.chain,
                state: LooksLikeVcek {
                    possible_verifying_key,
                    product_in_cert: product,
                    cert_data: cert.tbs_certificate.as_ref().to_vec(),
                    signature: cert.signature_value.data.to_vec(),
                    tcb_version,
                },
            })
        }
    }

    impl<I: Chain> VcekChainVerifier<I, SignedByKnownAmdRoot> {
        fn from(
            mut current: VcekChainVerifier<I, LooksLikeVcek>,
            root_store: &RootStore,
        ) -> Result<Self, Error> {
            let pem = current.next_pem()?;
            if pem.label != "CERTIFICATE" {
                return Err(Error::NotACertificate);
            }
            let cert = pem.parse_x509().map_err(|_| Error::ParseError)?;

            if root_store
                .find_anchor_by_serial(&cert.serial)
                .is_some_and(|anchor| {
                    anchor.has_signed(&current.state.cert_data, &current.state.signature)
                })
            {
                return Ok(Self {
                    chain: current.chain,
                    state: SignedByKnownAmdRoot {
                        product: current.state.product_in_cert,
                        verifying_key: current.state.possible_verifying_key,
                    },
                });
            }

            Err(Error::ChainBroken)
        }
    }

    #[allow(clippy::missing_errors_doc)]
    pub fn verify_vcek_chain<I: Iterator<Item = Result<Pem, PEMError>>>(
        root_store: &RootStore,
        chain: I,
    ) -> Result<(Product, p384::ecdsa::VerifyingKey, TcbVersion), Error> {
        let vcek_expected = VcekChainVerifier::new(chain);
        let looks_like_vcek = VcekChainVerifier::<I, LooksLikeVcek>::from(vcek_expected)?;
        let tcb_version = looks_like_vcek.state.tcb_version;

        let signed_by_known_amd_root =
            VcekChainVerifier::<I, SignedByKnownAmdRoot>::from(looks_like_vcek, root_store)?;

        Ok((
            signed_by_known_amd_root.state.product,
            signed_by_known_amd_root.state.verifying_key,
            tcb_version,
        ))
    }
}

// Siena and Bergamo use the same root chain as Genoa
static GENOA_ROOT_CHAIN: &[u8] = include_bytes!("../fixtures/Genoa.pem");
static MILAN_ROOT_CHAIN: &[u8] = include_bytes!("../fixtures/Milan.pem");
static TURIN_ROOT_CHAIN: &[u8] = include_bytes!("../fixtures/Turin.pem");

#[allow(clippy::enum_variant_names)]
#[derive(Debug)]
pub enum Error {
    // VCEK validation errors
    DecodeError(PEMError),
    ParseError,
    /// Something else than a certificate was seen
    NotACertificate,
    WrongKeyUsage,
    WrongBasicConstraints,
    UnsupportedSignatureAlgorithm,
    ChainBroken,
    UnknownProductName(String),

    // Other report validation errors
    ReportParseError,
    ReportSignatureParseError,
    ReportSignatureMismatch,
    ReportTcbVersionMismatch,
    /// The report didn't satisfy the requested verifications
    RequirementsNotSatisfied(&'static str),
}

/// A trusted certificate. Every certificate in a chain above this one is considered trusted.
#[derive(Debug)]
struct TrustAnchor {
    verifying_key: rsa::pss::VerifyingKey<Sha384>,
}

impl TrustAnchor {
    pub fn has_signed(&self, cert: &[u8], signature: &[u8]) -> bool {
        let Ok(sig) = signature.try_into() else {
            return false;
        };
        self.verifying_key.verify(cert, &sig).is_ok()
    }
}

impl<'a> TryFrom<&X509Certificate<'a>> for TrustAnchor {
    type Error = String;

    fn try_from(value: &X509Certificate<'a>) -> Result<Self, Self::Error> {
        let public_key = rsa::RsaPublicKey::from_public_key_der(value.public_key().raw)
            .map_err(|_| "Could not create RSA public key from certificat public key")?;
        Ok(Self {
            verifying_key: public_key.into(),
        })
    }
}

pub struct RootStore {
    anchors: HashMap<BigUint, TrustAnchor>,
}

impl RootStore {
    /// Creates a new, empty [`RootStore`].
    #[must_use]
    pub fn new() -> Self {
        Self {
            anchors: HashMap::new(),
        }
    }

    #[allow(clippy::missing_errors_doc)]
    pub fn add_chain(&mut self, contents: &[u8]) -> Result<(), String> {
        // N.B. only the leaf of the chain is stored as trust anchor
        let (_, pem) = parse_x509_pem(contents).map_err(|_| "Could not decode entry")?;
        let cert = pem
            .parse_x509()
            .map_err(|_| "Could not parse certificate")?;

        if !cert.is_ca() {
            return Err("Certificates in root store should be CA certificates".to_string());
        }

        // Leaf should have a pathlen of 0
        if cert
            .basic_constraints()
            .unwrap_or(None)
            .and_then(|ext| ext.value.path_len_constraint)
            != Some(0)
        {
            return Err("Leaf in root chain should have pathlen of 0".to_string());
        }

        let anchor = TrustAnchor::try_from(&cert)?;
        if self.anchors.insert(cert.serial.clone(), anchor).is_some() {
            return Err("Multiple certs with same serial".to_string());
        }

        Ok(())
    }

    fn find_anchor_by_serial(&self, serial: &BigUint) -> Option<&TrustAnchor> {
        self.anchors.get(serial)
    }
}

impl Default for RootStore {
    fn default() -> Self {
        let mut store = Self::new();
        store
            .add_chain(MILAN_ROOT_CHAIN)
            .expect("Root chain should be valid");
        store
            .add_chain(GENOA_ROOT_CHAIN)
            .expect("Root chain should be valid");
        store
            .add_chain(TURIN_ROOT_CHAIN)
            .expect("Root chain should be valid");
        store
    }
}

type FnMicrocode = fn(vcek::Product, &AttestationReport) -> Result<(), &'static str>;

/// A builder for [`Requirements`].
///
/// # [`std::default::Default`] implementation
///
/// Note that the builder starts without any requirements. In particular,
/// `RequirementsBuilder::default().build()` is different to `Requirements::default()`.
#[derive(Debug, Clone, Default)]
#[must_use]
pub struct RequirementsBuilder {
    genoa: ProductRequirements,
    milan: ProductRequirements,
    turin: ProductRequirements,
    require_alias_check: bool,
    check_microcode: Option<FnMicrocode>,
}

impl RequirementsBuilder {
    /// Consumes this builder and returns corresponding [`Requirements`].
    #[must_use]
    pub fn build(self) -> Requirements {
        Requirements {
            genoa: self.genoa,
            milan: self.milan,
            turin: self.turin,
            require_alias_check: self.require_alias_check,
            check_microcode: self.check_microcode,
        }
    }

    /// Requires that the report contains that alias detection has completed and that there are no
    /// aliasing addresses. Also sets the required SNP firmware versions for Milan and Genoa as
    /// well as SNP security version number if no higher versions are already set.
    pub fn require_alias_check(mut self) -> Self {
        self.require_alias_check = true;
        if self.genoa.min_committed_version.unwrap_or_default()
            < Requirements::SB_3015_MIN_GENOA_VERSION
        {
            self.genoa.min_committed_version = Some(Requirements::SB_3015_MIN_GENOA_VERSION);
        }
        if self.genoa.min_committed_tcb_snp.unwrap_or_default()
            < Requirements::SB_3015_MIN_GENOA_SNP_SVN
        {
            self.genoa.min_committed_tcb_snp = Some(Requirements::SB_3015_MIN_GENOA_SNP_SVN);
        }

        if self.milan.min_committed_version.unwrap_or_default()
            < Requirements::SB_3015_MIN_MILAN_VERSION
        {
            self.milan.min_committed_version = Some(Requirements::SB_3015_MIN_MILAN_VERSION);
        }
        if self.milan.min_committed_tcb_snp.unwrap_or_default()
            < Requirements::SB_3015_MIN_MILAN_SNP_SVN
        {
            self.milan.min_committed_tcb_snp = Some(Requirements::SB_3015_MIN_MILAN_SNP_SVN);
        }

        self
    }

    /// Requires that the report's committed SNP firmware version is at least the given version for
    /// Genoa products. Does not lower the required version if a higher version is already
    /// required.
    pub fn min_committed_version_for_genoa(mut self, version: (u8, u8, u8)) -> Self {
        if version > self.genoa.min_committed_version.unwrap_or_default() {
            self.genoa.min_committed_version = Some(version);
        }
        self
    }

    /// Requires that the report's committed SNP firmware version is at least the given version for
    /// Milan products. Does not lower the required version if a higher version is already
    /// required.
    pub fn min_committed_version_for_milan(mut self, version: (u8, u8, u8)) -> Self {
        if version > self.milan.min_committed_version.unwrap_or_default() {
            self.milan.min_committed_version = Some(version);
        }
        self
    }

    /// Requires that the report's committed SNP firmware version is at least the given version for
    /// Turin products. Does not lower the required version if a higher version is already
    /// required.
    pub fn min_committed_version_for_turin(mut self, version: (u8, u8, u8)) -> Self {
        if version > self.turin.min_committed_version.unwrap_or_default() {
            self.turin.min_committed_version = Some(version);
        }
        self
    }

    /// Requires that the report's committed SNP firmware security version number (SVN) is at least
    /// the given version number for Milan products. Does not lower the required version number if
    /// a higher version number is already required.
    pub fn min_committed_snp_svn_for_milan(mut self, snp: u8) -> Self {
        if snp > self.milan.min_committed_tcb_snp.unwrap_or_default() {
            self.milan.min_committed_tcb_snp = Some(snp);
        }
        self
    }

    /// Requires that the report's committed SNP firmware security version number (SVN) is at least
    /// the given version number for Genoa products. Does not lower the required version number if
    /// a higher version number is already required.
    pub fn min_committed_snp_svn_for_genoa(mut self, snp: u8) -> Self {
        if snp > self.genoa.min_committed_tcb_snp.unwrap_or_default() {
            self.genoa.min_committed_tcb_snp = Some(snp);
        }
        self
    }

    /// Requires that the report's committed SNP firmware security version number (SVN) is at least
    /// the given version number for Turin products. Does not lower the required version number if
    /// a higher version number is already required.
    pub fn min_committed_snp_svn_for_turin(mut self, snp: u8) -> Self {
        if snp > self.turin.min_committed_tcb_snp.unwrap_or_default() {
            self.turin.min_committed_tcb_snp = Some(snp);
        }
        self
    }

    pub fn require_sb_7033_mitigations(mut self) -> Self {
        self.check_microcode = Some(Requirements::sb_7033_microcode_check);
        self
    }
}

#[derive(Debug, Clone, Default)]
#[allow(clippy::struct_field_names)]
struct ProductRequirements {
    // Inclusive
    min_committed_version: Option<(u8, u8, u8)>,
    // Inclusive
    min_committed_tcb_snp: Option<u8>,
    // Bitmask: all listed bits must be set in both current_mit_vector and launch_mit_vector
    min_mit_vector: Option<u64>,
}

/// Additional requirements for [`AttestationReport`] verification.
///
/// See also: [`RequirementsBuilder`], [`verify_report`]
#[derive(Debug, Clone)]
pub struct Requirements {
    genoa: ProductRequirements,
    milan: ProductRequirements,
    turin: ProductRequirements,
    require_alias_check: bool,
    check_microcode: Option<FnMicrocode>,
}

impl Default for Requirements {
    fn default() -> Self {
        Self::sb_3023_mitigations()
    }
}

macro_rules! microcode_check {
    ($name:ident, $(($product:pat, $cpu_family:pat) => $min_rev:expr),+ $(,)?) => {
        fn $name(
            product: vcek::Product,
            report: &AttestationReport,
        ) -> Result<(), &'static str> {
            if let Some(cpu_family) = report.cpuid_mod_id.zip(report.cpuid_step) {
                let min_rev = match (product, cpu_family) {
                    $(($product, $cpu_family) => $min_rev,)+
                    _ => return Err("Report doesn't match any known CPU family"),
                };
                if report.committed_tcb.microcode < min_rev {
                    return Err("Committed TCB: Microcode version too small");
                }
            } else {
                return Err("Could not verify microcode version: Missing values for CPU family");
            }
            Ok(())
        }
    };
}

impl Requirements {
    const SB_3023_MIN_GENOA_MIT_VEC: u64 = (1 << 0) | (1 << 1);
    const SB_3023_MIN_TURIN_MIT_VEC: u64 = (1 << 0) | (1 << 1) | (1 << 2) | (1 << 4) | (1 << 5);

    const SB_3020_MIN_GENOA_VERSION: (u8, u8, u8) = (0x1, 0x37, 0x31);
    const SB_3020_MIN_GENOA_SNP_SVN: u8 = 0x1B;
    const SB_3020_MIN_GENOA_MIT_VEC: u64 = 1 << 1;
    const SB_3020_MIN_MILAN_VERSION: (u8, u8, u8) = (0x1, 0x37, 0x23);
    const SB_3020_MIN_MILAN_SNP_SVN: u8 = 0x1B;
    const SB_3020_MIN_MILAN_MIT_VEC: u64 = 1 << 1;
    const SB_3020_MIN_TURIN_VERSION: (u8, u8, u8) = (0x1, 0x37, 0x41);
    const SB_3020_MIN_TURIN_SNP_SVN: u8 = 0x04;
    const SB_3020_MIN_TURIN_MIT_VEC: u64 = 1 << 0;

    const SB_3019_MIN_GENOA_VERSION: (u8, u8, u8) = (0x1, 0x37, 0x28);
    const SB_3019_MIN_GENOA_SNP_SVN: u8 = 0x17;
    const SB_3019_MIN_MILAN_VERSION: (u8, u8, u8) = (0x1, 0x37, 0x18);
    const SB_3019_MIN_MILAN_SNP_SVN: u8 = 0x18;
    const SB_3019_MIN_TURIN_VERSION: (u8, u8, u8) = (0x1, 0x37, 0x3b);

    const SB_3015_MIN_GENOA_VERSION: (u8, u8, u8) = (0x1, 0x37, 0x26);
    const SB_3015_MIN_GENOA_SNP_SVN: u8 = 0x16;
    const SB_3015_MIN_MILAN_VERSION: (u8, u8, u8) = (0x1, 0x37, 0x16);
    const SB_3015_MIN_MILAN_SNP_SVN: u8 = 0x17;

    /// Checks for mitigations against [AMD-SB-3023](https://www.amd.com/en/resources/product-security/bulletin/amd-sb-3023.html)
    #[must_use]
    pub fn sb_3023_mitigations() -> Self {
        let sb_3020 = Self::sb_3020_mitigations();
        Self {
            genoa: ProductRequirements {
                min_mit_vector: Some(Self::SB_3023_MIN_GENOA_MIT_VEC),
                ..sb_3020.genoa
            },
            turin: ProductRequirements {
                min_mit_vector: Some(Self::SB_3023_MIN_TURIN_MIT_VEC),
                ..sb_3020.turin
            },
            check_microcode: Some(Self::sb_3023_microcode_check),
            ..sb_3020
        }
    }

    /// Checks for mitigations against [AMD-SB-3020](https://www.amd.com/en/resources/product-security/bulletin/amd-sb-3020.html)
    #[must_use]
    pub fn sb_3020_mitigations() -> Self {
        Self {
            genoa: ProductRequirements {
                min_committed_version: Some(Self::SB_3020_MIN_GENOA_VERSION),
                min_committed_tcb_snp: Some(Self::SB_3020_MIN_GENOA_SNP_SVN),
                min_mit_vector: Some(Self::SB_3020_MIN_GENOA_MIT_VEC),
            },
            milan: ProductRequirements {
                min_committed_version: Some(Self::SB_3020_MIN_MILAN_VERSION),
                min_committed_tcb_snp: Some(Self::SB_3020_MIN_MILAN_SNP_SVN),
                min_mit_vector: Some(Self::SB_3020_MIN_MILAN_MIT_VEC),
            },
            turin: ProductRequirements {
                min_committed_version: Some(Self::SB_3020_MIN_TURIN_VERSION),
                min_committed_tcb_snp: Some(Self::SB_3020_MIN_TURIN_SNP_SVN),
                min_mit_vector: Some(Self::SB_3020_MIN_TURIN_MIT_VEC),
            },
            require_alias_check: true,
            check_microcode: Some(Self::sb_3020_microcode_check),
        }
    }

    /// Checks for mitigations against [AMD-SB-3019](https://www.amd.com/en/resources/product-security/bulletin/amd-sb-3019.html)
    #[must_use]
    pub fn sb_3019_mitigations() -> Self {
        Self {
            genoa: ProductRequirements {
                min_committed_version: Some(Self::SB_3019_MIN_GENOA_VERSION),
                min_committed_tcb_snp: Some(Self::SB_3019_MIN_GENOA_SNP_SVN),
                ..Default::default()
            },
            milan: ProductRequirements {
                min_committed_version: Some(Self::SB_3019_MIN_MILAN_VERSION),
                min_committed_tcb_snp: Some(Self::SB_3019_MIN_MILAN_SNP_SVN),
                ..Default::default()
            },
            turin: ProductRequirements {
                min_committed_version: Some(Self::SB_3019_MIN_TURIN_VERSION),
                ..Default::default()
            },
            require_alias_check: true,
            check_microcode: None,
        }
    }

    /// Checks for mitigations against [AMD-SB-3015](https://www.amd.com/en/resources/product-security/bulletin/amd-sb-3015.html)
    #[must_use]
    pub fn sb_3015_mitigations() -> Self {
        Self {
            genoa: ProductRequirements {
                min_committed_version: Some(Self::SB_3015_MIN_GENOA_VERSION),
                min_committed_tcb_snp: Some(Self::SB_3015_MIN_GENOA_SNP_SVN),
                ..Default::default()
            },
            milan: ProductRequirements {
                min_committed_version: Some(Self::SB_3015_MIN_MILAN_VERSION),
                min_committed_tcb_snp: Some(Self::SB_3015_MIN_MILAN_SNP_SVN),
                ..Default::default()
            },
            // Turin not affected by SB-3015
            turin: ProductRequirements::default(),
            require_alias_check: true,
            check_microcode: None,
        }
    }

    /// Checks for mitigations against [AMD-SB-3011](https://www.amd.com/en/resources/product-security/bulletin/amd-sb-3011.html)
    #[must_use]
    pub fn sb_3011_mitigations() -> Self {
        Self {
            genoa: ProductRequirements {
                min_committed_version: Some((0x1, 0x37, 0x24)),
                min_committed_tcb_snp: Some(0x16),
                ..Default::default()
            },
            milan: ProductRequirements {
                min_committed_version: Some((0x1, 0x37, 0x14)),
                min_committed_tcb_snp: Some(0x17),
                ..Default::default()
            },
            // Turin not affected by SB-3011
            turin: ProductRequirements::default(),
            require_alias_check: false,
            check_microcode: None,
        }
    }

    microcode_check!(sb_3023_microcode_check,
        // Milan
        (vcek::Product::Milan, (1, 1)) => 0xDE,
        // Milan-X
        (vcek::Product::Milan, (1, 2)) => 0x47,
        // Genoa
        (vcek::Product::Genoa, (0x11, 1)) => 0x56,
        // Genoa-X
        (vcek::Product::Genoa, (0x11, 2)) => 0x51,
        // Bergamo/Siena
        (vcek::Product::Genoa, (0xA0, 2)) => 0x1B,
        // Turin Classic
        (vcek::Product::Turin, (2, 1)) => 0x51,
        // Turin Dense
        (vcek::Product::Turin, (0x11, 0)) => 0x4E,
    );

    microcode_check!(sb_3020_microcode_check,
        // Milan
        (vcek::Product::Milan, (1, 1)) => 0xDE,
        // Milan-X
        (vcek::Product::Milan, (1, 2)) => 0x45,
        // Genoa
        (vcek::Product::Genoa, (0x11, 1)) => 0x56,
        // Genoa-X
        (vcek::Product::Genoa, (0x11, 2)) => 0x51,
        // Bergamo/Siena
        (vcek::Product::Genoa, (0xA0, 2)) => 0x1B,
        // Turin Classic
        (vcek::Product::Turin, (2, 1)) => 0x50,
        // Turin Dense
        (vcek::Product::Turin, (0x11, 0)) => 0x4D,
    );

    microcode_check!(sb_7033_microcode_check,
        // Milan
        (vcek::Product::Milan, (1, 1)) => 0xDB,
        // Milan-X
        (vcek::Product::Milan, (1, 2)) => 0x44,
        // Genoa
        (vcek::Product::Genoa, (0x11, 1)) => 0x54,
        // Genoa-X
        (vcek::Product::Genoa, (0x11, 2)) => 0x4F,
        // Bergamo/Siena
        (vcek::Product::Genoa, (0xA0, 2)) => 0x19,
        // Turin Classic
        (vcek::Product::Turin, (2, 1)) => 0x47,
    );

    fn verify(
        &self,
        product: vcek::Product,
        report: &AttestationReport,
    ) -> Result<(), &'static str> {
        if report.policy.debug_allowed() {
            return Err("Debug is enabled");
        }

        if report.policy.migrate_ma_allowed() {
            return Err("Guest policy allows a migration agent");
        }

        if report.vmpl > 3 {
            return Err("VMPL is not <= 3");
        }

        if report.key_info.signing_key() != 0 {
            return Err("Signing key is not VCEK");
        }

        if self.require_alias_check && !report.plat_info.alias_check_complete() {
            return Err("Alias check complete is false");
        }

        let requirements = match product {
            vcek::Product::Milan => &self.milan,
            vcek::Product::Genoa => &self.genoa,
            vcek::Product::Turin => &self.turin,
        };

        if let Some(min_wanted) = requirements.min_committed_version {
            if (
                report.committed.major,
                report.committed.minor,
                report.committed.build,
            ) < min_wanted
            {
                return Err("Firmware version too small");
            }
        }

        if let Some(min_tcb_snp) = requirements.min_committed_tcb_snp {
            if report.committed_tcb.snp < min_tcb_snp {
                return Err("Committed TCB: SNP patch level too small");
            }
        }

        if let Some(min_mit_vec) = requirements.min_mit_vector {
            match report.current_mit_vector {
                None => return Err("Current mitigation vector: not present in report"),
                Some(v) if v & min_mit_vec != min_mit_vec => {
                    return Err("Current mitigation vector: required mitigation bits not set")
                }
                Some(_) => {}
            }
            match report.launch_mit_vector {
                None => return Err("Launch mitigation vector: not present in report"),
                Some(v) if v & min_mit_vec != min_mit_vec => {
                    return Err("Launch mitigation vector: required mitigation bits not set")
                }
                Some(_) => {}
            }
        }

        if let Some(check) = self.check_microcode {
            check(product, report)?;
        }

        Ok(())
    }
}

fn parse_report(bytes: &[u8]) -> Option<AttestationReport> {
    if bytes.len() < 1184 {
        return None;
    }

    let read_u32 = |off: usize| u32::from_le_bytes(bytes[off..off + 4].try_into().unwrap());
    let read_u64 = |off: usize| u64::from_le_bytes(bytes[off..off + 8].try_into().unwrap());

    let version = read_u32(0x00);

    let turin_like = if version >= 3 {
        // Turin-like if CPU_FAM_ID is not the one for Genoa and Milan
        bytes[0x188] != 0x19
    } else {
        let chip_id = &bytes[0x1A0..0x1E0];
        if chip_id == [0u8; 64] {
            return None;
        }

        // Turin-like if the last 8 bytes of CHIP_ID are zero
        chip_id[8..] == [0u8; 56]
    };

    let parse_tcb = |off: usize| -> TcbVersion {
        let b = &bytes[off..off + 8];
        if turin_like {
            TcbVersion {
                fmc: Some(b[0]),
                bootloader: b[1],
                tee: b[2],
                snp: b[3],
                microcode: b[7],
            }
        } else {
            TcbVersion {
                fmc: None,
                bootloader: b[0],
                tee: b[1],
                snp: b[6],
                microcode: b[7],
            }
        }
    };

    let (cpuid_fam_id, cpuid_mod_id, cpuid_step) = if version >= 3 {
        (Some(bytes[0x188]), Some(bytes[0x189]), Some(bytes[0x18A]))
    } else {
        (None, None, None)
    };

    let parse_version = |off: usize| Version {
        build: bytes[off],
        minor: bytes[off + 1],
        major: bytes[off + 2],
    };

    let r: [u8; 72] = bytes[0x2A0..0x2E8].try_into().ok()?;
    let s: [u8; 72] = bytes[0x2E8..0x330].try_into().ok()?;

    Some(AttestationReport {
        version,
        guest_svn: read_u32(0x04),
        policy: GuestPolicy(read_u64(0x08)),
        family_id: bytes[0x10..0x20].try_into().ok()?,
        image_id: bytes[0x20..0x30].try_into().ok()?,
        vmpl: read_u32(0x30),
        sig_algo: read_u32(0x34),
        current_tcb: parse_tcb(0x38),
        plat_info: PlatformInfo(read_u64(0x40)),
        key_info: KeyInfo(read_u32(0x48)),
        report_data: bytes[0x50..0x90].try_into().ok()?,
        measurement: bytes[0x90..0xC0].try_into().ok()?,
        host_data: bytes[0xC0..0xE0].try_into().ok()?,
        id_key_digest: bytes[0xE0..0x110].try_into().ok()?,
        author_key_digest: bytes[0x110..0x140].try_into().ok()?,
        report_id: bytes[0x140..0x160].try_into().ok()?,
        report_id_ma: bytes[0x160..0x180].try_into().ok()?,
        reported_tcb: parse_tcb(0x180),
        cpuid_fam_id,
        cpuid_mod_id,
        cpuid_step,
        chip_id: bytes[0x1A0..0x1E0].try_into().ok()?,
        committed_tcb: parse_tcb(0x1E0),
        current: parse_version(0x1E8),
        committed: parse_version(0x1EC),
        launch_tcb: parse_tcb(0x1F0),
        launch_mit_vector: (version >= 5).then(|| read_u64(0x1F8)),
        current_mit_vector: (version >= 5).then(|| read_u64(0x200)),
        signature: Signature::new(r, s),
    })
}

#[allow(clippy::missing_errors_doc)]
pub fn verify_report<R: Read + Seek>(
    report_contents: &[u8],
    vcek_chain: R,
    root_store: &RootStore,
    verifications: &Requirements,
) -> Result<AttestationReport, Error> {
    if report_contents.len() < 1184 {
        return Err(Error::ReportParseError);
    }

    let report = parse_report(report_contents).ok_or(Error::ReportParseError)?;
    let sig = ReportSignature::try_from(&report).map_err(|_| Error::ReportParseError)?;
    let ecdsa_sig =
        p384::ecdsa::Signature::try_from(&sig).map_err(|_| Error::ReportSignatureParseError)?;
    let (product, key, tcb_version) = vcek::verify_vcek_chain(
        root_store,
        Pem::iter_from_reader(BufReader::new(vcek_chain)),
    )?;

    if report.reported_tcb != tcb_version {
        return Err(Error::ReportTcbVersionMismatch);
    }

    if key.verify(&report_contents[..0x2a0], &ecdsa_sig).is_ok() {
        verifications
            .verify(product, &report)
            .map(|()| report)
            .map_err(Error::RequirementsNotSatisfied)
    } else {
        Err(Error::ReportSignatureMismatch)
    }
}

#[cfg(test)]
mod tests {
    use std::{fs::File, io::Cursor, path::PathBuf};

    use ecdsa::{elliptic_curve::Generate, signature::Signer};
    use p384::{ecdsa::SigningKey, NistP384};
    use rand::{rand_core::UnwrapErr, rngs::SysRng};
    use sev::{
        certs::snp::ecdsa::Signature,
        firmware::guest::{AttestationReport, GuestPolicy},
        parser::ByteParser,
    };
    use x509_parser::{num_bigint::BigUint, prelude::Pem};

    use crate::verification::{Error, Requirements, RootStore};

    use super::{vcek, vcek::verify_vcek_chain, verify_report, RequirementsBuilder};

    // Re-implementation of std's `assert_matches!` which is unfortunately still unstable
    macro_rules! assert_m {
        ($left:expr, $(|)? $( $pattern:pat_param )|+ $( if $guard: expr )? $(,)?) => {
            match $left {
                $( $pattern )|+ $( if $guard )? => {}
                ref left_val => {
                    panic!(
                        "Expected match where there is none\nleft: {left_val:?}\nright: {}",
                        stringify!($($pattern)|+ $(if $guard)?),
                    );
                }
            }
        };
    }

    #[test]
    fn verify_report_returns_ok_for_valid_report_and_chain() {
        let result = verify_report(
            &hex::decode(include_bytes!("../fixtures/tests/report.data")).unwrap(),
            File::open(PathBuf::from("./fixtures/tests/valid_vcek_chain.data")).unwrap(),
            &RootStore::default(),
            // N.B. doesn't use `Requirements::default` because the fixtures are reports without
            // the required mitigations
            &RequirementsBuilder::default().build(),
        );

        assert!(result.is_ok());
    }

    #[test]
    fn verify_report_returns_ok_for_valid_report_and_chain_with_sb_3015_mitigations() {
        let result = verify_report(
            &genoa_report_alias_check_fixture_bytes(),
            genoa_vcek_chain_alias_check_fixture(),
            &RootStore::default(),
            &Requirements::sb_3015_mitigations(),
        );
        assert!(result.is_ok());
    }

    #[test]
    fn default_requirements_require_sb_3023_mitigations() {
        let result = verify_report(
            &hex::decode(include_bytes!("../fixtures/tests/report.data")).unwrap(),
            File::open(PathBuf::from("./fixtures/tests/valid_vcek_chain.data")).unwrap(),
            &RootStore::default(),
            &Requirements::default(),
        );

        assert_m!(
            result,
            Err(Error::RequirementsNotSatisfied(
                "Alias check complete is false"
            ))
        );
    }

    #[test]
    fn verify_vcek_chain_returns_ok_for_valid_chain() {
        let result = verify_vcek_chain(
            &RootStore::default(),
            Pem::iter_from_buffer(include_bytes!("../fixtures/tests/valid_vcek_chain.data")),
        );

        assert!(result.is_ok());
    }

    #[test]
    fn verify_vcek_chain_returns_err_for_selfsigned_chain() {
        let result = verify_vcek_chain(
            &RootStore::default(),
            Pem::iter_from_buffer(include_bytes!(
                "../fixtures/tests/selfsigned_vcek_chain.data"
            )),
        );

        assert!(result.is_err());
    }

    #[test]
    fn verify_vcek_chain_returns_err_for_invalid_chain() {
        let result = verify_vcek_chain(
            &RootStore::default(),
            Pem::iter_from_buffer(include_bytes!("../fixtures/tests/vcek_wrong_order.data")),
        );

        assert!(result.is_err());
    }

    #[test]
    fn default_root_store_finds_milan_serial() {
        let root_store = RootStore::default();

        assert!(root_store
            .find_anchor_by_serial(&BigUint::from(0x10001u32))
            .is_some());
    }

    #[test]
    fn root_store_returns_none_for_unknown_serial() {
        let root_store = RootStore::new();

        assert!(root_store
            .find_anchor_by_serial(&BigUint::from(0x1234u32))
            .is_none());
    }

    #[test]
    fn invalid_report_signature_returns_err() {
        let mut report = AttestationReport::from_bytes(&report_fixture_bytes()).unwrap();
        // 0 values result in a signature error
        report.signature = Signature::new([0u8; 72], [0u8; 72]);

        let result = verify_report(
            &report.to_bytes().unwrap(),
            File::open(PathBuf::from("./fixtures/tests/valid_vcek_chain.data")).unwrap(),
            &RootStore::default(),
            &Requirements::default(),
        );

        assert_m!(result, Err(Error::ReportSignatureParseError));
    }

    #[test]
    fn wrong_report_signature_returns_err() {
        let mut report = AttestationReport::from_bytes(&report_fixture_bytes()).unwrap();
        let signing_key = SigningKey::generate_from_rng(&mut UnwrapErr(SysRng));
        let signature = signing_key.sign(b"wurzelpfropf");
        report.signature = create_signature_from_signature(&signature);

        let result = verify_report(
            &report.to_bytes().unwrap(),
            vcek_chain_fixture(),
            &RootStore::default(),
            &Requirements::default(),
        );

        assert_m!(result, Err(Error::ReportSignatureMismatch));
    }

    #[test]
    fn wrong_tcb_version_returns_err() {
        let mut report = AttestationReport::from_bytes(&report_fixture_bytes()).unwrap();
        let signing_key = SigningKey::generate_from_rng(&mut UnwrapErr(SysRng));
        let signature = signing_key.sign(b"wurzelpfropf");
        report.signature = create_signature_from_signature(&signature);

        let result = verify_report(
            &report.to_bytes().unwrap(),
            File::open(PathBuf::from(
                "./fixtures/tests/valid_vcek_wrong_tcb_version.data",
            ))
            .unwrap(),
            &RootStore::default(),
            &Requirements::default(),
        );

        assert_m!(result, Err(Error::ReportTcbVersionMismatch));
    }

    #[test]
    fn debug_enabled_returns_err() {
        let mut report = AttestationReport::from_bytes(&report_fixture_bytes()).unwrap();
        report.policy.set_debug_allowed(true);

        assert_m!(
            RequirementsBuilder::default()
                .build()
                .verify(vcek::Product::Milan, &report),
            Err("Debug is enabled")
        );
    }

    /// The guest policy a stock hypervisor launches with: reserved bit 17 and SMT.
    fn default_guest_policy() -> GuestPolicy {
        GuestPolicy(0x3_0000)
    }

    #[test]
    fn default_guest_policy_is_accepted() {
        let mut report = AttestationReport::from_bytes(&report_fixture_bytes()).unwrap();
        report.policy = default_guest_policy();

        assert_m!(
            RequirementsBuilder::default()
                .build()
                .verify(vcek::Product::Milan, &report),
            Ok(())
        );
    }

    #[test]
    fn migration_agent_allowed_returns_err() {
        let mut report = AttestationReport::from_bytes(&report_fixture_bytes()).unwrap();
        report.policy = default_guest_policy();
        report.policy.set_migrate_ma_allowed(true);

        assert_m!(
            RequirementsBuilder::default()
                .build()
                .verify(vcek::Product::Milan, &report),
            Err("Guest policy allows a migration agent")
        );
    }

    #[test]
    fn non_guest_vmpl_returns_err() {
        let mut report = AttestationReport::from_bytes(&report_fixture_bytes()).unwrap();
        report.vmpl = 4;

        assert_m!(
            RequirementsBuilder::default()
                .build()
                .verify(vcek::Product::Milan, &report),
            Err("VMPL is not <= 3")
        );
    }

    #[test]
    fn signing_key_not_vcek_returns_err() {
        let mut report_bytes = report_fixture_bytes();
        // KEY_INFO is at offset 0x48 in the attestation report structure.
        // SIGNING_KEY is bits 4:2; set to 1 (VLEK).
        report_bytes[0x48] |= 1 << 2;
        let report = AttestationReport::from_bytes(&report_bytes).unwrap();

        assert_m!(
            RequirementsBuilder::default()
                .build()
                .verify(vcek::Product::Milan, &report),
            Err("Signing key is not VCEK")
        );
    }

    #[test]
    fn requirements_check_for_minimal_committed_snp_version_milan() {
        fn verify(version: (u8, u8, u8)) -> Result<AttestationReport, Error> {
            verify_report(
                &report_fixture_bytes(),
                vcek_chain_fixture(),
                &RootStore::default(),
                &RequirementsBuilder::default()
                    .min_committed_version_for_milan(version)
                    .build(),
            )
        }

        fn version_too_small(version: (u8, u8, u8)) {
            assert_m!(
                verify(version),
                Err(Error::RequirementsNotSatisfied(
                    "Firmware version too small"
                ))
            );
        }

        version_too_small((1, 52, 5));
        version_too_small((1, 53, 4));
        version_too_small((2, 0, 0));

        assert_m!(verify((1, 52, 4)), Ok(_));
        assert_m!(verify((1, 50, 0)), Ok(_));
    }

    #[test]
    fn requirements_check_for_minimal_committed_snp_version_genoa() {
        fn verify(version: (u8, u8, u8)) -> Result<AttestationReport, Error> {
            verify_report(
                &genoa_report_fixture_bytes(),
                genoa_vcek_chain_fixture(),
                &RootStore::default(),
                &RequirementsBuilder::default()
                    .min_committed_version_for_genoa(version)
                    .build(),
            )
        }

        fn version_too_small(version: (u8, u8, u8)) {
            assert_m!(
                verify(version),
                Err(Error::RequirementsNotSatisfied(
                    "Firmware version too small"
                ))
            );
        }

        version_too_small((1, 55, 22));
        version_too_small((1, 56, 21));
        version_too_small((2, 0, 0));

        assert_m!(verify((1, 55, 21)), Ok(_));
        assert_m!(verify((1, 54, 0)), Ok(_));
    }

    #[test]
    fn requirements_check_for_minimal_committed_snp_svn_milan() {
        fn verify(svn: u8) -> Result<AttestationReport, Error> {
            verify_report(
                &report_fixture_bytes(),
                vcek_chain_fixture(),
                &RootStore::default(),
                &RequirementsBuilder::default()
                    .min_committed_snp_svn_for_milan(svn)
                    .build(),
            )
        }

        assert_m!(verify(8), Ok(_));
        assert_m!(
            verify(9),
            Err(Error::RequirementsNotSatisfied(
                "Committed TCB: SNP patch level too small"
            ))
        );
    }

    #[test]
    fn requirements_check_for_minimal_committed_snp_svn_genoa() {
        fn verify(svn: u8) -> Result<AttestationReport, Error> {
            verify_report(
                &genoa_report_fixture_bytes(),
                genoa_vcek_chain_fixture(),
                &RootStore::default(),
                &RequirementsBuilder::default()
                    .min_committed_snp_svn_for_genoa(svn)
                    .build(),
            )
        }

        assert_m!(verify(14), Ok(_));
        assert_m!(
            verify(15),
            Err(Error::RequirementsNotSatisfied(
                "Committed TCB: SNP patch level too small"
            ))
        );
    }

    #[test]
    fn requirements_check_for_alias_check_milan() {
        let result = verify_report(
            &report_fixture_bytes(),
            vcek_chain_fixture(),
            &RootStore::default(),
            &RequirementsBuilder::default().require_alias_check().build(),
        );
        assert_m!(
            result,
            Err(Error::RequirementsNotSatisfied(
                "Alias check complete is false"
            ))
        );
    }

    #[test]
    fn requirements_check_for_alias_check_genoa() {
        let result = verify_report(
            &genoa_report_fixture_bytes(),
            genoa_vcek_chain_fixture(),
            &RootStore::default(),
            &RequirementsBuilder::default().require_alias_check().build(),
        );
        assert_m!(
            result,
            Err(Error::RequirementsNotSatisfied(
                "Alias check complete is false"
            ))
        );
    }

    #[test]
    fn requirements_check_for_alias_check_genoa_ok() {
        let result = verify_report(
            &genoa_report_alias_check_fixture_bytes(),
            genoa_vcek_chain_alias_check_fixture(),
            &RootStore::default(),
            &RequirementsBuilder::default().require_alias_check().build(),
        );
        assert!(result.is_ok());
    }

    #[test]
    fn requirements_check_microcode_missing_cpu_fields() {
        let result = verify_report(
            &report_fixture_bytes(),
            vcek_chain_fixture(),
            &RootStore::default(),
            &RequirementsBuilder::default()
                .require_sb_7033_mitigations()
                .build(),
        );
        assert_m!(
            result,
            Err(Error::RequirementsNotSatisfied(
                "Could not verify microcode version: Missing values for CPU family"
            )),
        );
    }

    #[test]
    fn verify_report_passes_for_authentic_genoa_v5_report() {
        let result = verify_report(
            &hex::decode(include_str!("../fixtures/tests/report_v5.data").trim()).unwrap(),
            File::open(PathBuf::from("./fixtures/tests/valid_vcek_chain_v5.data")).unwrap(),
            &RootStore::default(),
            &Requirements::default(),
        );
        assert!(result.is_ok());
    }

    #[test]
    fn verify_vcek_chain_returns_ok_for_milan_with_microcode_above_128() {
        let (product, _key, tcb_version) = verify_vcek_chain(
            &RootStore::default(),
            Pem::iter_from_buffer(include_bytes!(
                "../fixtures/tests/valid_vcek_chain_milan.data"
            )),
        )
        .unwrap();

        assert_m!(product, vcek::Product::Milan);
        assert_eq!(tcb_version.microcode, 0xDB);
    }

    #[test]
    fn verify_report_returns_ok_for_milan_with_microcode_above_128() {
        let result = verify_report(
            &hex::decode(include_str!("../fixtures/tests/report_milan.data").trim()).unwrap(),
            Cursor::new(include_str!(
                "../fixtures/tests/valid_vcek_chain_milan.data"
            )),
            &RootStore::default(),
            &RequirementsBuilder::default().build(),
        );

        assert!(result.is_ok());
    }

    #[test]
    fn verify_vcek_chain_returns_ok_for_turin() {
        let (product, _key, tcb_version) = verify_vcek_chain(
            &RootStore::default(),
            Pem::iter_from_buffer(include_bytes!(
                "../fixtures/tests/valid_vcek_chain_turin.data"
            )),
        )
        .unwrap();

        assert_m!(product, vcek::Product::Turin);
        assert_eq!(tcb_version.microcode, 0x47);
    }

    #[test]
    fn verify_report_returns_ok_for_turin() {
        let result = verify_report(
            &hex::decode(include_str!("../fixtures/tests/report_turin.data").trim()).unwrap(),
            Cursor::new(include_str!(
                "../fixtures/tests/valid_vcek_chain_turin.data"
            )),
            &RootStore::default(),
            &RequirementsBuilder::default().build(),
        );

        assert!(result.is_ok());
    }

    #[test]
    fn requirements_builder_doesnt_lower_version() {
        let requirements = RequirementsBuilder::default()
            .min_committed_version_for_genoa((1, 52, 1))
            .min_committed_version_for_genoa((1, 50, 0))
            .min_committed_version_for_milan((1, 52, 1))
            .min_committed_version_for_milan((1, 50, 0))
            .min_committed_version_for_turin((1, 52, 1))
            .min_committed_version_for_turin((1, 50, 0))
            .build();

        assert_eq!(requirements.genoa.min_committed_version, Some((1, 52, 1)));
        assert_eq!(requirements.milan.min_committed_version, Some((1, 52, 1)));
        assert_eq!(requirements.turin.min_committed_version, Some((1, 52, 1)));
    }

    #[test]
    fn requirements_builder_sets_higher_version() {
        let requirements = RequirementsBuilder::default()
            .min_committed_version_for_genoa((1, 52, 1))
            .min_committed_version_for_genoa((1, 52, 2))
            .min_committed_version_for_milan((1, 52, 1))
            .min_committed_version_for_milan((1, 52, 2))
            .min_committed_version_for_turin((1, 52, 1))
            .min_committed_version_for_turin((1, 52, 2))
            .build();

        assert_eq!(requirements.genoa.min_committed_version, Some((1, 52, 2)));
        assert_eq!(requirements.milan.min_committed_version, Some((1, 52, 2)));
        assert_eq!(requirements.turin.min_committed_version, Some((1, 52, 2)));
    }

    fn create_signature_from_signature(signature: &ecdsa::Signature<NistP384>) -> Signature {
        // p384 uses big endian, but SNP firmware uses little endian for the signature,
        // hence reverse to match endianess. Additionally, the scalars in p384 ECDSA signatures are
        // 48 bytes, but the report's fields are 72 bytes, so the remaining bytes are filled with
        // zeros.
        // See table 115: "Format for an ECDSA P-384 with SHA-384 Signature" in
        // "SEV Secure Nested Paging Firmware ABI Specification" (publication #56860)
        // https://www.amd.com/system/files/TechDocs/56860.pdf
        let mut r: [u8; 72] = [0u8; 72];
        r[24..].copy_from_slice(&signature.r().to_bytes());
        r.reverse();
        let mut s: [u8; 72] = [0u8; 72];
        s[24..].copy_from_slice(&signature.s().to_bytes());
        s.reverse();

        Signature::new(r, s)
    }

    fn genoa_report_fixture_bytes() -> Vec<u8> {
        hex::decode(include_bytes!("../fixtures/tests/report_genoa.data")).unwrap()
    }

    fn genoa_report_alias_check_fixture_bytes() -> Vec<u8> {
        hex::decode(include_bytes!(
            "../fixtures/tests/report_genoa_alias_check.data"
        ))
        .unwrap()
    }

    fn report_fixture_bytes() -> Vec<u8> {
        hex::decode(include_bytes!("../fixtures/tests/report.data")).unwrap()
    }

    fn genoa_vcek_chain_fixture() -> impl std::io::Read + std::io::Seek {
        File::open(PathBuf::from(
            "./fixtures/tests/valid_vcek_chain_genoa.data",
        ))
        .unwrap()
    }

    fn genoa_vcek_chain_alias_check_fixture() -> impl std::io::Read + std::io::Seek {
        File::open(PathBuf::from(
            "./fixtures/tests/valid_vcek_chain_genoa_alias_check.data",
        ))
        .unwrap()
    }

    fn vcek_chain_fixture() -> impl std::io::Read + std::io::Seek {
        File::open(PathBuf::from("./fixtures/tests/valid_vcek_chain.data")).unwrap()
    }

    #[test]
    fn parser_matches_sev_parser_milan() {
        let bytes =
            hex::decode(include_str!("../fixtures/tests/report_milan.data").trim()).unwrap();
        let expected = AttestationReport::from_bytes(&bytes).unwrap();
        let actual = super::parse_report(&bytes).unwrap();
        assert_eq!(actual, expected);
    }

    #[test]
    fn parser_matches_sev_parser_genoa() {
        let bytes = genoa_report_fixture_bytes();
        let expected = AttestationReport::from_bytes(&bytes).unwrap();
        let actual = super::parse_report(&bytes).unwrap();
        assert_eq!(actual, expected);
    }

    #[test]
    fn parser_matches_sev_parser_v5() {
        let bytes = hex::decode(include_str!("../fixtures/tests/report_v5.data").trim()).unwrap();
        let expected = AttestationReport::from_bytes(&bytes).unwrap();
        let actual = super::parse_report(&bytes).unwrap();
        assert_eq!(actual, expected);
    }

    #[test]
    fn parser_matches_sev_parser_turin() {
        let bytes =
            hex::decode(include_str!("../fixtures/tests/report_turin.data").trim()).unwrap();
        let expected = AttestationReport::from_bytes(&bytes).unwrap();
        let actual = super::parse_report(&bytes).unwrap();
        assert_eq!(actual, expected);
    }

    #[test]
    fn parse_report_ignores_reserved_bytes_for_unknown_version() {
        // Simulate a hypothetical future version that uses the currently reserved
        // area between current_mit_vector and the signature (0x208..0x2A0).
        let mut bytes =
            hex::decode(include_str!("../fixtures/tests/report_milan.data").trim()).unwrap();
        bytes[0..4].copy_from_slice(&42u32.to_le_bytes());
        bytes[0x208..0x2A0].fill(0xAB);

        let report = super::parse_report(&bytes).unwrap();
        assert_eq!(report.version, 42);
        // Fields that precede the reserved area must be unaffected.
        assert!(report.current_mit_vector.is_some());
    }

    #[test]
    fn parse_report_truncated() {
        let bytes =
            hex::decode(include_str!("../fixtures/tests/report_milan.data").trim()).unwrap();
        assert!(super::parse_report(&bytes[..1183]).is_none());
    }

    #[test]
    fn microcode_check_macro_accepts_above_minimum() {
        let mut report = AttestationReport::from_bytes(&report_fixture_bytes()).unwrap();
        report.cpuid_mod_id = Some(1);
        report.cpuid_step = Some(1);
        // All three checks for Milan (1,1) require at most 0xDE
        report.committed_tcb.microcode = 0xDE;
        assert!(Requirements::sb_3023_microcode_check(vcek::Product::Milan, &report).is_ok());
        assert!(Requirements::sb_3020_microcode_check(vcek::Product::Milan, &report).is_ok());
        assert!(Requirements::sb_7033_microcode_check(vcek::Product::Milan, &report).is_ok());
    }

    #[test]
    fn microcode_check_macro_rejects_below_minimum() {
        let mut report = AttestationReport::from_bytes(&report_fixture_bytes()).unwrap();
        report.cpuid_mod_id = Some(1);
        report.cpuid_step = Some(1);
        // SB-3020 Milan (1,1) minimum is 0xDE
        report.committed_tcb.microcode = 0xDD;
        assert_eq!(
            Requirements::sb_3020_microcode_check(vcek::Product::Milan, &report),
            Err("Committed TCB: Microcode version too small"),
        );
    }

    #[test]
    fn microcode_check_macro_rejects_unknown_cpu_family() {
        let mut report = AttestationReport::from_bytes(&report_fixture_bytes()).unwrap();
        report.cpuid_mod_id = Some(0xFF);
        report.cpuid_step = Some(0xFF);
        assert_eq!(
            Requirements::sb_3020_microcode_check(vcek::Product::Milan, &report),
            Err("Report doesn't match any known CPU family"),
        );
    }

    #[test]
    fn microcode_check_macro_rejects_missing_cpu_fields() {
        let mut report = AttestationReport::from_bytes(&report_fixture_bytes()).unwrap();
        report.cpuid_mod_id = None;
        report.cpuid_step = None;
        assert_eq!(
            Requirements::sb_3020_microcode_check(vcek::Product::Milan, &report),
            Err("Could not verify microcode version: Missing values for CPU family"),
        );
    }

    fn sb_3020_passing_genoa_report() -> AttestationReport {
        use sev::firmware::{
            guest::{PlatformInfo, Version},
            host::TcbVersion,
        };
        AttestationReport {
            policy: default_guest_policy(),
            plat_info: PlatformInfo(1 << 5), // alias_check_complete
            committed: Version::new(1, 0x37, 0x31),
            committed_tcb: TcbVersion {
                snp: 0x1b,
                microcode: 0x56,
                ..Default::default()
            },
            cpuid_mod_id: Some(0x11),
            cpuid_step: Some(1),
            current_mit_vector: Some(Requirements::SB_3020_MIN_GENOA_MIT_VEC),
            launch_mit_vector: Some(Requirements::SB_3020_MIN_GENOA_MIT_VEC),
            ..Default::default()
        }
    }

    #[test]
    fn requirements_check_mit_vector_missing() {
        let mut report = sb_3020_passing_genoa_report();
        report.current_mit_vector = None;
        assert_eq!(
            Requirements::sb_3020_mitigations().verify(vcek::Product::Genoa, &report),
            Err("Current mitigation vector: not present in report"),
        );
    }

    #[test]
    fn requirements_check_mit_vector_insufficient() {
        let mut report = sb_3020_passing_genoa_report();
        report.current_mit_vector = Some(0);
        assert_eq!(
            Requirements::sb_3020_mitigations().verify(vcek::Product::Genoa, &report),
            Err("Current mitigation vector: required mitigation bits not set"),
        );
    }

    #[test]
    fn requirements_check_mit_vector_valid() {
        let report = sb_3020_passing_genoa_report();
        assert!(Requirements::sb_3020_mitigations()
            .verify(vcek::Product::Genoa, &report)
            .is_ok());
    }

    #[test]
    fn requirements_check_launch_mit_vector_missing() {
        let mut report = sb_3020_passing_genoa_report();
        report.launch_mit_vector = None;
        assert_eq!(
            Requirements::sb_3020_mitigations().verify(vcek::Product::Genoa, &report),
            Err("Launch mitigation vector: not present in report"),
        );
    }

    #[test]
    fn requirements_check_launch_mit_vector_insufficient() {
        let mut report = sb_3020_passing_genoa_report();
        report.launch_mit_vector = Some(0);
        assert_eq!(
            Requirements::sb_3020_mitigations().verify(vcek::Product::Genoa, &report),
            Err("Launch mitigation vector: required mitigation bits not set"),
        );
    }

    #[test]
    fn requirements_check_sb_3023_mit_vector_requires_extra_genoa_bits() {
        let mut report = sb_3020_passing_genoa_report();
        report.committed_tcb.microcode = 0x58;
        assert_eq!(
            Requirements::sb_3023_mitigations().verify(vcek::Product::Genoa, &report),
            Err("Current mitigation vector: required mitigation bits not set"),
        );
        report.current_mit_vector = Some(Requirements::SB_3023_MIN_GENOA_MIT_VEC);
        report.launch_mit_vector = Some(Requirements::SB_3023_MIN_GENOA_MIT_VEC);
        assert!(Requirements::sb_3023_mitigations()
            .verify(vcek::Product::Genoa, &report)
            .is_ok());
    }

    #[test]
    fn requirements_check_sb_3023_launch_mit_vector_checked_independently_of_current() {
        let mut report = sb_3020_passing_genoa_report();
        report.committed_tcb.microcode = 0x58;
        report.current_mit_vector = Some(Requirements::SB_3023_MIN_GENOA_MIT_VEC);
        assert_eq!(
            Requirements::sb_3023_mitigations().verify(vcek::Product::Genoa, &report),
            Err("Launch mitigation vector: required mitigation bits not set"),
        );
    }
}