Debian Codegraph @

crt/s2n/bindings/rust/standard/integration/src/handshake/cert_aware_sig_selection.rs

raw symbols SCIP
1 // Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
2 // SPDX-License-Identifier: Apache-2.0
3
4 //! TLS 1.3 Signature Negotiation changes rather significantly from TLS 1.2
5 //!
6 //! In TLS 1.2, a client would indicate support for e.g. ECDSA+SHA256 & ECDSA+SHA384.
7 //! These support any ECDSA cert.
8 //!
9 //! In TLS 1.3, a client indicates support for a specific scheme like `ecdsa_secp256r1_sha256`
10 //! or `ecdsa_secp384r1_sha384`. A server can only choose that scheme if it's ECDSA
11 //! cert matches the curve specified in the signature scheme.
12 //!
13 //! s2n-tls previously has a bug in this selection logic (https://github.com/aws/s2n-tls/pull/5713).
14 //! This test protects against regressions in that behavior.
15
16 use crate::{
17 capability_check::{required_capability, Capability},
18 utilities::certs::{self, CertMaterials},
19 };
20 use brass_aphid_wire_decryption::decryption::{key_manager::KeyManager, Mode};
21 use brass_aphid_wire_messages::{
22 iana::{self, SignatureScheme},
23 protocol::content_value::{ContentValue, HandshakeMessageValue},
24 };
25 use openssl::ssl::SslContextBuilder;
26 use s2n_tls::security::{Policy, DEFAULT_PQ};
27 use tls_harness::{
28 cohort::{OpenSslConnection, S2NConnection},
29 harness::TlsConfigBuilderPair,
30 TlsConnPair,
31 };
32
33 /// Handshake `server_policy` with `cert_materials`, and return the SignatureScheme
34 /// from the server's CertVerify message.
35 fn trial(server_policy: &Policy, cert_materials: &CertMaterials) -> SignatureScheme {
36 let key_manager = KeyManager::new();
37
38 let mut pair: TlsConnPair<OpenSslConnection, S2NConnection> = {
39 let mut configs =
40 TlsConfigBuilderPair::<SslContextBuilder, s2n_tls::config::Builder>::default_without_certs();
41 // Setup OpenSSL client
42 configs.client.set_ca_file(&cert_materials.ca_path).unwrap();
43
44 configs.server.set_security_policy(server_policy).unwrap();
45 configs.server.set_max_blinding_delay(0).unwrap();
46 configs
47 .server
48 .load_pem(&cert_materials.server_chain(), &cert_materials.server_key())
49 .unwrap();
50 key_manager.enable_s2n_logging(&mut configs.server);
51
52 configs.connection_pair()
53 };
54 pair.io.enable_decryption(key_manager.clone());
55
56 pair.handshake().unwrap();
57 pair.shutdown().unwrap();
58
59 let cert_verify = {
60 let transcript = pair.io.decrypter.borrow().as_ref().unwrap().transcript();
61 let transcript = transcript.content_transcript.lock().unwrap().clone();
62 transcript
63 .into_iter()
64 .find_map(|(sender, message)| {
65 if let ContentValue::Handshake(HandshakeMessageValue::CertVerifyTls13(verify)) =
66 message
67 {
68 assert_eq!(sender, Mode::Server);
69 Some(verify)
70 } else {
71 None
72 }
73 })
74 .unwrap()
75 };
76 cert_verify.algorithm
77 }
78
79 /// We use the same security policy and client configuration, forcing s2n-tls to
80 /// go through its own server preference list and correctly skip signatures incompatible
81 /// with its certificate
82 #[test]
83 fn signature_selection() {
84 // ECDSA
85 {
86 let secp256r1 = CertMaterials::from_permutation("ec_ecdsa_p256_sha256");
87 let secp521r1 = CertMaterials::from_permutation("ec_ecdsa_p521_sha512");
88
89 // arbitrary policy which supports TLS 1.3 and ECDSA
90 let policy = Policy::from_version("20240503").unwrap();
91
92 assert_eq!(
93 trial(&policy, &secp256r1),
94 iana::constants::ecdsa_secp256r1_sha256
95 );
96 assert_eq!(
97 trial(&policy, &secp521r1),
98 iana::constants::ecdsa_secp521r1_sha512
99 );
100 }
101
102 // MLDSA
103 required_capability(&[Capability::MLDsa], || {
104 assert_eq!(
105 trial(&DEFAULT_PQ, &certs::ML_DSA_87),
106 iana::constants::mldsa87
107 );
108 assert_eq!(
109 trial(&DEFAULT_PQ, &certs::ML_DSA_44),
110 iana::constants::mldsa44
111 );
112 });
113 }