crypto-profile 0.10.1

Named cryptographic profiles over the AWS-LC FIPS module: algorithm types, per-module capabilities and a conformance suite
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
//! Test infrastructure: in-memory certificates and an in-process handshake. Not shipped behaviour.
use crate::signature::KeyPair;
use crate::tls::{
    CertificateKey, Policy, client_builder, server_builder, server_cert_verifier_builder,
};
use crate::{
    CipherSuite, CryptoError, KxGroup, Options, Profile, Resolved, SignatureAlgorithm,
    TlsSignatureScheme, TlsVersion, admit, resolve,
};
use rustls::client::danger::{HandshakeSignatureValid, ServerCertVerified, ServerCertVerifier};
use rustls::pki_types::{CertificateDer, ServerName, UnixTime};
use rustls::sign::SingleCertAndKey;
use rustls::{DigitallySignedStruct, DistinguishedName, SignatureScheme};
use std::sync::{Arc, Mutex};

/// The FIPS module's crypto library name, from aws-lc-rs's link metadata; a dynamic build loads
/// `lib{FIPS_LIBRARY}.so` (Linux) or `lib{FIPS_LIBRARY}.dylib` (macOS).
pub const FIPS_LIBRARY: &str = env!("CRYPTO_PROFILE_FIPS_LIBRARY");

/// The raw ECDH shared secret between `key` and `peer`, for tests that check it against published
/// vectors. Never compiled outside `test` and `test-support`.
pub fn ecdh_shared_secret(
    key: &crate::ecdh::StaticKey,
    peer: &[u8],
) -> Result<zeroize::Zeroizing<Vec<u8>>, crate::CryptoError> {
    key.shared_secret(peer)
}

/// Sets the number of seals `key` has made, for tests that reach the 2^32 limit without sealing
/// 2^32 times. Never compiled outside `test` and `test-support`: it can reset a key's count.
pub fn aead_set_invocations(key: &crate::aead::Key, invocations: u64) {
    key.set_invocations(invocations);
}

/// What a completed handshake negotiated.
#[derive(Debug, Clone, Copy, PartialEq)]
#[non_exhaustive]
pub struct Negotiated {
    /// The protocol version.
    pub version: TlsVersion,
    /// The cipher suite.
    pub suite: CipherSuite,
    /// The key-exchange group.
    pub kx: KxGroup,
    /// The scheme the server signed the handshake with.
    pub scheme: TlsSignatureScheme,
    /// The client's [`rustls::CommonState::handshake_kind`].
    pub kind: rustls::HandshakeKind,
}

/// A server verifier that records the scheme of the handshake signature it checks.
#[derive(Debug)]
struct SchemeRecorder {
    inner: Arc<dyn ServerCertVerifier>,
    seen: Arc<Mutex<Option<SignatureScheme>>>,
}

impl SchemeRecorder {
    fn record(&self, scheme: SignatureScheme) {
        *self.seen.lock().unwrap_or_else(|e| e.into_inner()) = Some(scheme);
    }
}

impl ServerCertVerifier for SchemeRecorder {
    fn verify_server_cert(
        &self,
        end_entity: &CertificateDer<'_>,
        intermediates: &[CertificateDer<'_>],
        server_name: &ServerName<'_>,
        ocsp_response: &[u8],
        now: UnixTime,
    ) -> Result<ServerCertVerified, rustls::Error> {
        self.inner
            .verify_server_cert(end_entity, intermediates, server_name, ocsp_response, now)
    }

    fn verify_tls12_signature(
        &self,
        message: &[u8],
        cert: &CertificateDer<'_>,
        dss: &DigitallySignedStruct,
    ) -> Result<HandshakeSignatureValid, rustls::Error> {
        self.record(dss.scheme);
        self.inner.verify_tls12_signature(message, cert, dss)
    }

    fn verify_tls13_signature(
        &self,
        message: &[u8],
        cert: &CertificateDer<'_>,
        dss: &DigitallySignedStruct,
    ) -> Result<HandshakeSignatureValid, rustls::Error> {
        self.record(dss.scheme);
        self.inner.verify_tls13_signature(message, cert, dss)
    }

    fn supported_verify_schemes(&self) -> Vec<SignatureScheme> {
        self.inner.supported_verify_schemes()
    }

    fn requires_raw_public_keys(&self) -> bool {
        self.inner.requires_raw_public_keys()
    }

    fn root_hint_subjects(&self) -> Option<&[DistinguishedName]> {
        self.inner.root_hint_subjects()
    }
}

fn deliver<D: std::ops::DerefMut<Target = rustls::ConnectionCommon<S>>, S: rustls::SideData>(
    to: &mut D,
    mut bytes: &[u8],
) -> Result<(), String> {
    // `read_tls` takes at most one 4096-byte read per call, so a larger flight is fed in a loop.
    while !bytes.is_empty() {
        if to.read_tls(&mut bytes).map_err(|x| x.to_string())? == 0 {
            return Err("peer stopped reading".to_string());
        }
        to.process_new_packets().map_err(|x| x.to_string())?;
    }
    Ok(())
}

fn pump(
    client: &mut rustls::ClientConnection,
    server: &mut rustls::ServerConnection,
) -> Result<(), String> {
    while client.is_handshaking() || server.is_handshaking() {
        let mut to_server = Vec::new();
        client
            .write_tls(&mut to_server)
            .map_err(|x| x.to_string())?;
        deliver(server, &to_server)?;
        let mut to_client = Vec::new();
        server
            .write_tls(&mut to_client)
            .map_err(|x| x.to_string())?;
        deliver(client, &to_client)?;
        if to_server.is_empty() && to_client.is_empty() {
            return Err("handshake stalled".to_string());
        }
    }
    Ok(())
}

/// Runs a full in-process handshake: a P-384 CA issues a `localhost` leaf whose key is a fresh
/// `key`; the server is built on `server`, the client on `client`.
pub fn handshake(
    client: &Policy,
    server: &Policy,
    key: SignatureAlgorithm,
) -> Result<Negotiated, String> {
    handshake_with_ca(client, server, SignatureAlgorithm::EcdsaP384Sha384, key)
}

/// Runs a full in-process handshake: a CA whose key is a fresh `ca` issues a `localhost` leaf
/// whose key is a fresh `key`; the server is built on `server`, the client on `client`.
pub fn handshake_with_ca(
    client: &Policy,
    server: &Policy,
    ca: SignatureAlgorithm,
    key: SignatureAlgorithm,
) -> Result<Negotiated, String> {
    run(client, pki(ca, key)?, |chain, leaf_key| {
        let der = rustls::pki_types::PrivateKeyDer::Pkcs8(
            leaf_key.to_pkcs8_der().map_err(e)?.to_vec().into(),
        );
        server_builder(server)
            .map_err(|x| x.to_string())?
            .with_no_client_auth()
            .with_single_cert(chain, der)
            .map_err(|x| x.to_string())
    })
}

/// Runs a full in-process handshake whose server installs its `key` leaf through
/// [`Resolved::certified_key`]: a P-384 CA issues a `localhost` leaf whose key is a fresh
/// `CertificateKey` of `key`, generated under `server`; the server is built on `server.tls`, the
/// client on `client`.
pub fn handshake_certified(
    client: &Policy,
    server: &Resolved,
    key: SignatureAlgorithm,
) -> Result<Negotiated, String> {
    let key = CertificateKey::generate(server, key).map_err(e)?;
    run(
        client,
        issue(SignatureAlgorithm::EcdsaP384Sha384, key)?,
        |chain, leaf_key| {
            let certified = server
                .certified_key(chain, leaf_key)
                .map_err(|x| x.to_string())?;
            Ok(server_builder(&server.tls)
                .map_err(|x| x.to_string())?
                .with_no_client_auth()
                .with_cert_resolver(Arc::new(SingleCertAndKey::from(certified))))
        },
    )
}

/// A CA and a `localhost` leaf it issued.
pub struct Pki {
    /// A store holding the CA alone.
    pub roots: rustls::RootCertStore,
    /// The CA's certificate.
    pub ca: CertificateDer<'static>,
    /// The leaf, then the CA.
    pub chain: Vec<CertificateDer<'static>>,
    /// The leaf's key.
    pub key: CertificateKey,
}

fn e(x: impl std::fmt::Display) -> String {
    x.to_string()
}

pub(crate) fn key_pair(a: SignatureAlgorithm) -> Result<KeyPair, CryptoError> {
    KeyPair::generate(&admit(Profile::Unrestricted, Options::default()), a)
}

pub(crate) fn certificate_key(a: SignatureAlgorithm) -> Result<CertificateKey, CryptoError> {
    let mut r = resolve(Profile::Unrestricted, Options::default())?;
    r.keys = Profile::Unrestricted.spec().key_algorithms.to_vec();
    CertificateKey::generate(&r, a)
}

/// A fresh CA whose key is `ca` and a `localhost` leaf whose key is a fresh `CertificateKey` of
/// `key`, admitted against `unrestricted`'s TLS key list as published, so a key the module line
/// lacks is `CryptoError::Unavailable`.
pub fn pki(ca: SignatureAlgorithm, key: SignatureAlgorithm) -> Result<Pki, String> {
    issue(ca, certificate_key(key).map_err(e)?)
}

/// A fresh CA whose key is `ca` and a `localhost` leaf it issues for `key`.
pub fn issue(ca: SignatureAlgorithm, key: CertificateKey) -> Result<Pki, String> {
    let ca_key = key_pair(ca).map_err(e)?;
    let mut ca_params = rcgen::CertificateParams::new(Vec::<String>::new()).map_err(e)?;
    ca_params
        .distinguished_name
        .push(rcgen::DnType::CommonName, "crypto-profile test CA");
    ca_params.is_ca = rcgen::IsCa::Ca(rcgen::BasicConstraints::Unconstrained);
    let ca_cert = ca_params.self_signed(&ca_key).map_err(e)?;
    let issuer = rcgen::Issuer::new(ca_params, &ca_key);
    let leaf = rcgen::CertificateParams::new(vec!["localhost".to_string()])
        .map_err(e)?
        .signed_by(key.key_pair(), &issuer)
        .map_err(e)?;
    let mut roots = rustls::RootCertStore::empty();
    roots.add(ca_cert.der().clone()).map_err(e)?;
    Ok(Pki {
        roots,
        ca: ca_cert.der().clone(),
        chain: vec![leaf.der().clone(), ca_cert.der().clone()],
        key,
    })
}

/// `inner`, recording the scheme of each handshake signature it checks into the returned cell.
pub fn recording(
    inner: Arc<dyn ServerCertVerifier>,
) -> (
    Arc<dyn ServerCertVerifier>,
    Arc<Mutex<Option<SignatureScheme>>>,
) {
    let seen = Arc::new(Mutex::new(None));
    let recorder = SchemeRecorder {
        inner,
        seen: Arc::clone(&seen),
    };
    (Arc::new(recorder), seen)
}

/// An in-memory handshake; the wire codepoints of the version, suite, group and the server's
/// handshake-signature scheme recorded in `seen`, and the client's handshake kind.
pub fn drive(
    client: rustls::ClientConfig,
    server: rustls::ServerConfig,
    seen: &Mutex<Option<SignatureScheme>>,
) -> Result<([u16; 4], rustls::HandshakeKind), String> {
    let name = rustls::pki_types::ServerName::try_from("localhost").map_err(e)?;
    let mut c = rustls::ClientConnection::new(Arc::new(client), name).map_err(e)?;
    let mut s = rustls::ServerConnection::new(Arc::new(server)).map_err(e)?;
    pump(&mut c, &mut s)?;
    let version = u16::from_be_bytes(c.protocol_version().ok_or("no version")?.to_array());
    let suite = u16::from_be_bytes(
        c.negotiated_cipher_suite()
            .ok_or("no suite")?
            .suite()
            .to_array(),
    );
    let group = u16::from_be_bytes(
        c.negotiated_key_exchange_group()
            .ok_or("no group")?
            .name()
            .to_array(),
    );
    let scheme = seen
        .lock()
        .unwrap_or_else(|e| e.into_inner())
        .map(|s| u16::from_be_bytes(s.to_array()))
        .ok_or("no handshake signature")?;
    let kind = c.handshake_kind().ok_or("no handshake kind")?;
    Ok(([version, suite, group, scheme], kind))
}

fn run(
    client: &Policy,
    p: Pki,
    serve: impl FnOnce(
        Vec<CertificateDer<'static>>,
        &CertificateKey,
    ) -> Result<rustls::ServerConfig, String>,
) -> Result<Negotiated, String> {
    let server_cfg = serve(p.chain.clone(), &p.key)?;
    let (verifier, seen) = recording(
        server_cert_verifier_builder(Arc::new(p.roots), client)
            .map_err(e)?
            .build()
            .map_err(e)?,
    );
    let client_cfg = client_builder(client)
        .map_err(e)?
        .dangerous()
        .with_custom_certificate_verifier(verifier)
        .with_no_client_auth();
    let ([version_cp, suite_cp, group_cp, scheme_cp], kind) = drive(client_cfg, server_cfg, &seen)?;
    let version = TlsVersion::ALL
        .iter()
        .copied()
        .find(|x| x.iana() == version_cp)
        .ok_or(format!("unexpected version {version_cp:#06x}"))?;
    let suite = CipherSuite::ALL
        .iter()
        .copied()
        .find(|x| x.iana() == suite_cp)
        .ok_or(format!("unknown suite {suite_cp:#06x}"))?;
    let kx = KxGroup::ALL
        .iter()
        .copied()
        .find(|x| x.iana() == group_cp)
        .ok_or(format!("unknown group {group_cp:#06x}"))?;
    let scheme = TlsSignatureScheme::ALL
        .iter()
        .copied()
        .find(|x| x.iana() == scheme_cp)
        .ok_or(format!("unknown scheme {scheme_cp:#06x}"))?;
    Ok(Negotiated {
        version,
        suite,
        kx,
        scheme,
        kind,
    })
}

/// A configurable certification path and private key used by the shared conformance suite.
pub struct PeerCertificatePki {
    opposite: Option<Box<PeerCertificatePki>>,
    /// Trust anchors for this path.
    pub roots: rustls::RootCertStore,
    /// Leaf followed by supplied intermediates.
    pub chain: Vec<CertificateDer<'static>>,
    /// Private key corresponding to the leaf.
    pub key: rustls::pki_types::PrivateKeyDer<'static>,
    /// Baseline revocation information for the path.
    pub crls: Vec<rustls::pki_types::CertificateRevocationListDer<'static>>,
}

impl PeerCertificatePki {
    /// This path, with `opposite` as the chain the other side presents.
    pub fn with_opposite(mut self, opposite: PeerCertificatePki) -> PeerCertificatePki {
        self.opposite = Some(Box::new(opposite));
        self
    }
}

/// Generates a path with independently selected root, optional intermediate and leaf keys.
pub fn peer_certificate_pki(
    root: SignatureAlgorithm,
    intermediate: Option<SignatureAlgorithm>,
    leaf: SignatureAlgorithm,
) -> Result<PeerCertificatePki, String> {
    peer_pki(root, intermediate, leaf, None)
}

fn peer_pki(
    root: SignatureAlgorithm,
    intermediate: Option<SignatureAlgorithm>,
    leaf: SignatureAlgorithm,
    fixture: Option<PeerCertificateFixture>,
) -> Result<PeerCertificatePki, String> {
    let root_key = key_pair(root).map_err(e)?;
    let mut params = rcgen::CertificateParams::new(Vec::<String>::new()).map_err(e)?;
    params
        .distinguished_name
        .push(rcgen::DnType::CommonName, "conformance root");
    params.is_ca = rcgen::IsCa::Ca(rcgen::BasicConstraints::Unconstrained);
    let sha256 = PeerSha256(&root_key);
    let root_cert = if matches!(fixture, Some(PeerCertificateFixture::RootSha256)) {
        params.self_signed(&sha256).map_err(e)?
    } else {
        params.self_signed(&root_key).map_err(e)?
    };
    let issuer = rcgen::Issuer::new(params.clone(), &root_key);
    let intermediate_key = intermediate.map(key_pair).transpose().map_err(e)?;
    let mut chain = Vec::new();
    let intermediate_params = intermediate_key
        .as_ref()
        .map(|_| {
            let mut p = rcgen::CertificateParams::new(Vec::<String>::new()).map_err(e)?;
            p.distinguished_name
                .push(rcgen::DnType::CommonName, "conformance intermediate");
            p.is_ca = rcgen::IsCa::Ca(rcgen::BasicConstraints::Unconstrained);
            Ok::<_, String>(p)
        })
        .transpose()?;
    let intermediate_issuer = intermediate_params
        .as_ref()
        .zip(intermediate_key.as_ref())
        .map(|(p, k)| rcgen::Issuer::new(p.clone(), k));
    let leaf_key = certificate_key(leaf).map_err(e)?;
    let name = if matches!(fixture, Some(PeerCertificateFixture::WrongName)) {
        "other.invalid"
    } else {
        "localhost"
    };
    let mut leaf_params = rcgen::CertificateParams::new(vec![name.into()]).map_err(e)?;
    leaf_params.serial_number = Some(rcgen::SerialNumber::from(42));
    if matches!(fixture, Some(PeerCertificateFixture::Expired)) {
        leaf_params.not_before = rcgen::date_time_ymd(2000, 1, 1);
        leaf_params.not_after = rcgen::date_time_ymd(2001, 1, 1);
    }
    let leaf_cert = leaf_params
        .signed_by(
            leaf_key.key_pair(),
            intermediate_issuer.as_ref().unwrap_or(&issuer),
        )
        .map_err(e)?;
    let mut der = leaf_cert.der().to_vec();
    if matches!(fixture, Some(PeerCertificateFixture::Tampered)) {
        let last = der.len() - 1;
        der[last] ^= 1;
    }
    chain.push(CertificateDer::from(der));
    if let Some((p, k)) = intermediate_params.zip(intermediate_key.as_ref()) {
        chain.push(p.signed_by(k, &issuer).map_err(e)?.der().clone());
    }
    let mut roots = rustls::RootCertStore::empty();
    if matches!(fixture, Some(PeerCertificateFixture::Untrusted)) {
        roots = pki(
            SignatureAlgorithm::EcdsaP384Sha384,
            SignatureAlgorithm::EcdsaP384Sha384,
        )?
        .roots;
    } else {
        roots.add(root_cert.der().clone()).map_err(e)?;
    }
    let mut crls = Vec::new();
    if matches!(
        fixture,
        Some(PeerCertificateFixture::Sha256Crl | PeerCertificateFixture::RevokedSha256Crl)
    ) {
        let crl_issuer = rcgen::Issuer::new(params, &sha256);
        crls.push(
            rcgen::CertificateRevocationListParams {
                this_update: rcgen::date_time_ymd(2020, 1, 1),
                next_update: rcgen::date_time_ymd(2040, 1, 1),
                crl_number: rcgen::SerialNumber::from(1),
                issuing_distribution_point: None,
                revoked_certs: if matches!(fixture, Some(PeerCertificateFixture::RevokedSha256Crl))
                {
                    vec![rcgen::RevokedCertParams {
                        serial_number: rcgen::SerialNumber::from(42),
                        revocation_time: rcgen::date_time_ymd(2020, 1, 1),
                        reason_code: None,
                        invalidity_date: None,
                    }]
                } else {
                    vec![]
                },
                key_identifier_method: rcgen::KeyIdMethod::Sha256,
            }
            .signed_by(&crl_issuer)
            .map_err(e)?
            .der()
            .clone(),
        );
    }
    Ok(PeerCertificatePki {
        opposite: None,
        roots,
        chain,
        key: rustls::pki_types::PrivateKeyDer::Pkcs8(
            leaf_key.to_pkcs8_der().map_err(e)?.to_vec().into(),
        ),
        crls,
    })
}

/// Special paths for baseline and strict certificate-validation regressions.
#[derive(Clone, Copy)]
pub enum PeerCertificateFixture {
    /// A compliant root key with a SHA-256 self-signature.
    RootSha256,
    /// An unrevoked SHA-384 path with a SHA-256 CRL.
    Sha256Crl,
    /// A revoked SHA-384 path with a SHA-256 CRL.
    RevokedSha256Crl,
    /// A separately generated 8192-bit RSA leaf.
    Rsa8192Leaf,
    /// A selected noncompliant path with a compliant alternative.
    SelectedBadPath,
    /// A selected compliant path with an unused noncompliant alternative.
    SelectedGoodPath,
    /// A leaf naming a different server.
    WrongName,
    /// An expired leaf.
    Expired,
    /// A path outside the configured trust anchors.
    Untrusted,
    /// A leaf with a corrupted signature.
    Tampered,
}

/// Generates a named regression path with a 3072-bit RSA root and P-384 leaf.
pub fn peer_certificate_fixture(
    fixture: PeerCertificateFixture,
) -> Result<PeerCertificatePki, String> {
    if matches!(
        fixture,
        PeerCertificateFixture::SelectedBadPath | PeerCertificateFixture::SelectedGoodPath
    ) {
        return peer_selected_path(matches!(fixture, PeerCertificateFixture::SelectedBadPath));
    }
    if matches!(fixture, PeerCertificateFixture::Rsa8192Leaf) {
        let mut p = peer_certificate_pki(
            SignatureAlgorithm::Rsa3072Sha384,
            None,
            SignatureAlgorithm::Rsa3072Sha384,
        )?;
        let issuer_key = key_pair(SignatureAlgorithm::Rsa3072Sha384).map_err(e)?;
        let mut params = rcgen::CertificateParams::new(Vec::<String>::new()).map_err(e)?;
        params.is_ca = rcgen::IsCa::Ca(rcgen::BasicConstraints::Unconstrained);
        let root = params.self_signed(&issuer_key).map_err(e)?;
        let issuer = rcgen::Issuer::new(params, &issuer_key);
        let der = rustls::pki_types::PrivatePkcs8KeyDer::from(
            crypto_profile::__test_data::RSA8192_PKCS8.to_vec(),
        );
        let key = rcgen::KeyPair::from_pkcs8_der_and_sign_algo(&der, &rcgen::PKCS_RSA_SHA384)
            .map_err(e)?;
        let leaf = rcgen::CertificateParams::new(vec!["localhost".into()])
            .map_err(e)?
            .signed_by(&key, &issuer)
            .map_err(e)?;
        p.roots = rustls::RootCertStore::empty();
        p.roots.add(root.der().clone()).map_err(e)?;
        p.chain = vec![leaf.der().clone()];
        p.key = rustls::pki_types::PrivateKeyDer::Pkcs8(der);
        return Ok(p);
    }
    let mut pki = peer_pki(
        SignatureAlgorithm::Rsa3072Sha384,
        None,
        SignatureAlgorithm::EcdsaP384Sha384,
        Some(fixture),
    )?;
    if matches!(
        fixture,
        PeerCertificateFixture::RevokedSha256Crl
            | PeerCertificateFixture::WrongName
            | PeerCertificateFixture::Expired
            | PeerCertificateFixture::Untrusted
            | PeerCertificateFixture::Tampered
    ) {
        pki.opposite = Some(Box::new(peer_certificate_pki(
            SignatureAlgorithm::EcdsaP384Sha384,
            None,
            SignatureAlgorithm::EcdsaP384Sha384,
        )?));
    }
    Ok(pki)
}

struct PeerSha256<'a>(&'a KeyPair);
impl rcgen::PublicKeyData for PeerSha256<'_> {
    fn der_bytes(&self) -> &[u8] {
        rcgen::PublicKeyData::der_bytes(self.0)
    }
    fn algorithm(&self) -> &'static rcgen::SignatureAlgorithm {
        &rcgen::PKCS_RSA_SHA256
    }
}
impl rcgen::SigningKey for PeerSha256<'_> {
    fn sign(&self, msg: &[u8]) -> Result<Vec<u8>, rcgen::Error> {
        let der = self
            .0
            .to_pkcs8_der()
            .map_err(|_| rcgen::Error::CouldNotParseKeyPair)?;
        let key = aws_lc_rs::signature::RsaKeyPair::from_pkcs8(&der)
            .map_err(|_| rcgen::Error::CouldNotParseKeyPair)?;
        let mut signature = vec![0; key.public_modulus_len()];
        key.sign(
            &aws_lc_rs::signature::RSA_PKCS1_SHA256,
            &aws_lc_rs::rand::SystemRandom::new(),
            msg,
            &mut signature,
        )
        .map_err(|_| rcgen::Error::RemoteKeyError)?;
        Ok(signature)
    }
}

/// Builds fresh mutual-authentication configurations, restricting only the detecting endpoint.
/// Each call creates independent session stores; permissive sessions are never reused by strict configurations.
pub fn peer_certificate_configs(
    policy: &Policy,
    server_checks_client: bool,
    pki: &PeerCertificatePki,
) -> Result<(rustls::ClientConfig, rustls::ServerConfig), String> {
    let (mut client, mut server) =
        peer_certificate_default_session_configs(policy, server_checks_client, pki)?;
    client.resumption = rustls::client::Resumption::disabled();
    server.session_storage = Arc::new(rustls::server::NoServerSessionStorage {});
    server.send_tls13_tickets = 0;
    Ok((client, server))
}

/// Builds mutual-authentication configurations with the default session storage and ticketer.
///
/// The key provider refuses an RSA key above 4,096 bits, so the peer presenting the
/// `Rsa8192Leaf` fixture signs through the module: with RSA-PSS and SHA-384 whenever its peer
/// offers it, without the policy's scheme narrowing.
pub fn peer_certificate_default_session_configs(
    policy: &Policy,
    server_checks_client: bool,
    pki: &PeerCertificatePki,
) -> Result<(rustls::ClientConfig, rustls::ServerConfig), String> {
    use crate::tls::{client_cert_verifier_builder, strict_client_builder, strict_server_builder};
    let mut baseline = policy.clone();
    baseline.peer_certificate_rule = None;
    baseline.cipher_suites = CipherSuite::ALL
        .iter()
        .copied()
        .filter(|s| policy.versions.contains(&s.version()))
        .collect();
    let cp = if server_checks_client {
        &baseline
    } else {
        policy
    };
    let sp = if server_checks_client {
        policy
    } else {
        &baseline
    };
    let opposite = pki.opposite.as_deref().unwrap_or(pki);
    let (client_pki, server_pki) = if server_checks_client {
        (pki, opposite)
    } else {
        (opposite, pki)
    };
    let cv = server_cert_verifier_builder(Arc::new(server_pki.roots.clone()), cp)
        .map_err(e)?
        .with_crls(server_pki.crls.clone())
        .build()
        .map_err(e)?;
    let sv = client_cert_verifier_builder(Arc::new(client_pki.roots.clone()), sp)
        .map_err(e)?
        .with_crls(client_pki.crls.clone())
        .build()
        .map_err(e)?;
    let cb = if cp.peer_certificate_rule.is_some() {
        strict_client_builder(cp, cv).map_err(e)?
    } else {
        client_builder(cp)
            .map_err(e)?
            .dangerous()
            .with_custom_certificate_verifier(cv)
    };
    let sb = if sp.peer_certificate_rule.is_some() {
        strict_server_builder(sp, sv).map_err(e)?
    } else {
        server_builder(sp).map_err(e)?.with_client_cert_verifier(sv)
    };
    let certified = |pki: &PeerCertificatePki| -> Result<_, String> {
        let key = match &pki.key {
            rustls::pki_types::PrivateKeyDer::Pkcs8(der)
                if der.secret_pkcs8_der() == crypto_profile::__test_data::RSA8192_PKCS8 =>
            {
                rsa8192_signing_key(der.secret_pkcs8_der())?
            }
            key => crate::tls::load_private_key(&baseline, key.clone_key()).map_err(e)?,
        };
        Ok(Arc::new(rustls::sign::CertifiedKey::new(
            pki.chain.clone(),
            key,
        )))
    };
    let client =
        cb.with_client_cert_resolver(Arc::new(SingleCertAndKey::from(certified(client_pki)?)));
    let server = sb.with_cert_resolver(Arc::new(SingleCertAndKey::from(certified(server_pki)?)));
    Ok((client, server))
}

fn rsa8192_signing_key(der: &[u8]) -> Result<Arc<dyn rustls::sign::SigningKey>, String> {
    let key = aws_lc_rs::signature::RsaKeyPair::from_pkcs8(der).map_err(e)?;
    Ok(Arc::new(Rsa8192Key(Arc::new(key))))
}

struct Rsa8192Key(Arc<aws_lc_rs::signature::RsaKeyPair>);

impl std::fmt::Debug for Rsa8192Key {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.write_str("Rsa8192Key")
    }
}

impl rustls::sign::SigningKey for Rsa8192Key {
    fn choose_scheme(&self, offered: &[SignatureScheme]) -> Option<Box<dyn rustls::sign::Signer>> {
        offered
            .contains(&SignatureScheme::RSA_PSS_SHA384)
            .then(|| Box::new(Rsa8192Key(self.0.clone())) as Box<dyn rustls::sign::Signer>)
    }

    fn algorithm(&self) -> rustls::SignatureAlgorithm {
        rustls::SignatureAlgorithm::RSA
    }
}

impl rustls::sign::Signer for Rsa8192Key {
    fn sign(&self, message: &[u8]) -> Result<Vec<u8>, rustls::Error> {
        let mut signature = vec![0; self.0.public_modulus_len()];
        self.0
            .sign(
                &aws_lc_rs::signature::RSA_PSS_SHA384,
                &aws_lc_rs::rand::SystemRandom::new(),
                message,
                &mut signature,
            )
            .map_err(|_| rustls::Error::General("signing failed".into()))?;
        Ok(signature)
    }

    fn scheme(&self) -> SignatureScheme {
        SignatureScheme::RSA_PSS_SHA384
    }
}

/// Drives mutual authentication and retains the local detecting endpoint's typed error.
/// The boolean is true for errors detected by the server.
pub fn drive_typed(
    client: rustls::ClientConfig,
    server: rustls::ServerConfig,
) -> Result<(), (bool, rustls::Error)> {
    let mut c = rustls::ClientConnection::new(
        Arc::new(client),
        ServerName::try_from("localhost")
            .map_err(|error| (false, rustls::Error::General(error.to_string())))?,
    )
    .map_err(|e| (false, e))?;
    let mut s = rustls::ServerConnection::new(Arc::new(server)).map_err(|e| (true, e))?;
    for _flight in 0..100 {
        if !c.is_handshaking() && !s.is_handshaking() {
            return Ok(());
        }
        let mut to_server = Vec::new();
        c.write_tls(&mut to_server)
            .map_err(|e| (false, rustls::Error::General(e.to_string())))?;
        let mut bytes = to_server.as_slice();
        while !bytes.is_empty() {
            s.read_tls(&mut bytes)
                .map_err(|e| (true, rustls::Error::General(e.to_string())))?;
            s.process_new_packets().map_err(|e| (true, e))?;
        }
        let mut to_client = Vec::new();
        s.write_tls(&mut to_client)
            .map_err(|e| (true, rustls::Error::General(e.to_string())))?;
        let mut bytes = to_client.as_slice();
        while !bytes.is_empty() {
            c.read_tls(&mut bytes)
                .map_err(|e| (false, rustls::Error::General(e.to_string())))?;
            c.process_new_packets().map_err(|e| (false, e))?;
        }
        if to_server.is_empty() && to_client.is_empty() {
            break;
        }
    }
    Err((false, rustls::Error::General("handshake stalled".into())))
}

fn peer_selected_path(bad_first: bool) -> Result<PeerCertificatePki, String> {
    let ca_params = |name: &str| {
        let mut p = rcgen::CertificateParams::new(Vec::<String>::new()).map_err(e)?;
        p.is_ca = rcgen::IsCa::Ca(rcgen::BasicConstraints::Unconstrained);
        p.distinguished_name.push(rcgen::DnType::CommonName, name);
        Ok::<_, String>(p)
    };
    let good_key = key_pair(SignatureAlgorithm::EcdsaP384Sha384).map_err(e)?;
    let bad_key = key_pair(SignatureAlgorithm::EcdsaP256Sha256).map_err(e)?;
    let gp = ca_params("good root")?;
    let bp = ca_params("bad root")?;
    let gr = gp.self_signed(&good_key).map_err(e)?;
    let br = bp.self_signed(&bad_key).map_err(e)?;
    let gi = rcgen::Issuer::new(gp, &good_key);
    let bi = rcgen::Issuer::new(bp, &bad_key);
    let ik = key_pair(SignatureAlgorithm::EcdsaP384Sha384).map_err(e)?;
    let ip = ca_params("shared intermediate")?;
    let good = ip.signed_by(&ik, &gi).map_err(e)?;
    let bad = ip.signed_by(&ik, &bi).map_err(e)?;
    let ii = rcgen::Issuer::new(ip, &ik);
    let key = certificate_key(SignatureAlgorithm::EcdsaP384Sha384).map_err(e)?;
    let leaf = rcgen::CertificateParams::new(vec!["localhost".into()])
        .map_err(e)?
        .signed_by(key.key_pair(), &ii)
        .map_err(e)?;
    let mut roots = rustls::RootCertStore::empty();
    roots.add(gr.der().clone()).map_err(e)?;
    roots.add(br.der().clone()).map_err(e)?;
    let chain = if bad_first {
        vec![leaf.der().clone(), bad.der().clone(), good.der().clone()]
    } else {
        vec![leaf.der().clone(), good.der().clone(), bad.der().clone()]
    };
    let crls = [&gi, &bi, &ii]
        .into_iter()
        .map(|issuer| {
            rcgen::CertificateRevocationListParams {
                this_update: rcgen::date_time_ymd(2020, 1, 1),
                next_update: rcgen::date_time_ymd(2040, 1, 1),
                crl_number: rcgen::SerialNumber::from(1),
                issuing_distribution_point: None,
                revoked_certs: vec![],
                key_identifier_method: rcgen::KeyIdMethod::Sha256,
            }
            .signed_by(issuer)
            .map(|crl| crl.der().clone())
            .map_err(e)
        })
        .collect::<Result<Vec<_>, _>>()?;
    Ok(PeerCertificatePki {
        opposite: None,
        roots,
        chain,
        key: rustls::pki_types::PrivateKeyDer::Pkcs8(
            key.to_pkcs8_der().map_err(e)?.to_vec().into(),
        ),
        crls,
    })
}

static IDENTITY_CAS: std::sync::atomic::AtomicUsize = std::sync::atomic::AtomicUsize::new(0);

/// A path for identity-check tests: a fresh P-384 CA and a leaf with serial 42.
pub struct IdentityPki {
    /// The CA alone.
    pub roots: rustls::RootCertStore,
    /// The leaf, then the CA.
    pub chain: Vec<CertificateDer<'static>>,
    /// The leaf's key.
    pub key: rustls::pki_types::PrivateKeyDer<'static>,
    /// One CRL from the CA, revoking serial 42 when asked.
    pub crls: Vec<rustls::pki_types::CertificateRevocationListDer<'static>>,
}

/// A leaf whose key is `leaf`, carrying exactly `sans` and `ekus` (no EKU extension when empty).
/// Each call's CA has a distinct name.
pub fn identity_pki(
    leaf: SignatureAlgorithm,
    sans: Vec<rcgen::SanType>,
    ekus: Vec<rcgen::ExtendedKeyUsagePurpose>,
    revoked: bool,
) -> Result<IdentityPki, String> {
    let ca_key = key_pair(SignatureAlgorithm::EcdsaP384Sha384).map_err(e)?;
    let mut ca_params = rcgen::CertificateParams::new(Vec::<String>::new()).map_err(e)?;
    let n = IDENTITY_CAS.fetch_add(1, std::sync::atomic::Ordering::Relaxed);
    ca_params.distinguished_name.push(
        rcgen::DnType::CommonName,
        format!("crypto-profile identity CA {n}"),
    );
    ca_params.is_ca = rcgen::IsCa::Ca(rcgen::BasicConstraints::Unconstrained);
    ca_params.key_usages = vec![
        rcgen::KeyUsagePurpose::KeyCertSign,
        rcgen::KeyUsagePurpose::CrlSign,
    ];
    let ca_cert = ca_params.self_signed(&ca_key).map_err(e)?;
    let issuer = rcgen::Issuer::new(ca_params, &ca_key);
    let leaf_key = certificate_key(leaf).map_err(e)?;
    let mut params = rcgen::CertificateParams::new(Vec::<String>::new()).map_err(e)?;
    params.subject_alt_names = sans;
    params.extended_key_usages = ekus;
    params.serial_number = Some(rcgen::SerialNumber::from(42));
    let leaf_cert = params.signed_by(leaf_key.key_pair(), &issuer).map_err(e)?;
    let crl = rcgen::CertificateRevocationListParams {
        this_update: rcgen::date_time_ymd(2020, 1, 1),
        next_update: rcgen::date_time_ymd(2040, 1, 1),
        crl_number: rcgen::SerialNumber::from(1),
        issuing_distribution_point: None,
        revoked_certs: if revoked {
            vec![rcgen::RevokedCertParams {
                serial_number: rcgen::SerialNumber::from(42),
                revocation_time: rcgen::date_time_ymd(2020, 1, 1),
                reason_code: None,
                invalidity_date: None,
            }]
        } else {
            vec![]
        },
        key_identifier_method: rcgen::KeyIdMethod::Sha256,
    }
    .signed_by(&issuer)
    .map_err(e)?;
    let mut roots = rustls::RootCertStore::empty();
    roots.add(ca_cert.der().clone()).map_err(e)?;
    Ok(IdentityPki {
        roots,
        chain: vec![leaf_cert.der().clone(), ca_cert.der().clone()],
        key: rustls::pki_types::PrivateKeyDer::Pkcs8(
            leaf_key.to_pkcs8_der().map_err(e)?.to_vec().into(),
        ),
        crls: vec![crl.der().clone()],
    })
}