Debian Codegraph @

crt/s2n/bindings/rust/standard/integration/src/network/tls_client.rs

raw symbols SCIP
1 // Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
2 // SPDX-License-Identifier: Apache-2.0
3
4 use s2n_tls::{
5 config::Config,
6 enums::Version,
7 security::{self, Policy},
8 };
9 use s2n_tls_tokio::{TlsConnector, TlsStream};
10 use tokio::net::TcpStream;
11
12 /// Perform a TLS handshake with port 443 of `domain`.
13 ///
14 /// * `domain`: The domain to perform the handshake with
15 /// * `security_policy`: The security policy to set on the handshaking client.
16 ///
17 /// Returns an open `TlsStream` if the handshake was successful, otherwise an
18 /// `Err``.
19 async fn handshake_with_domain(
20 domain: &str,
21 security_policy: &Policy,
22 ) -> Result<TlsStream<TcpStream>, Box<dyn std::error::Error>> {
23 tracing::info!("querying {domain} with {:?}", security_policy);
24 const PORT: u16 = 443;
25
26 let mut config = Config::builder();
27 config.set_security_policy(security_policy)?;
28
29 let client = TlsConnector::new(config.build()?);
30 // open the TCP stream
31 let stream = TcpStream::connect((domain, PORT)).await?;
32 // complete the TLS handshake
33 Ok(client.connect(domain, stream).await?)
34 }
35
36 #[cfg(feature = "pq")]
37 mod kms_pq {
38 use super::*;
39
40 const DOMAIN: &str = "kms.us-east-1.amazonaws.com";
41
42 // confirm that we successfully negotiate a supported PQ key exchange.
43 #[test_log::test(tokio::test)]
44 async fn pq_handshake() -> Result<(), Box<dyn std::error::Error>> {
45 let policy = Policy::from_version("PQ-TLS-1-2-2024-10-09")?;
46 let tls = handshake_with_domain(DOMAIN, &policy).await?;
47
48 assert_eq!(tls.as_ref().cipher_suite()?, "TLS_AES_256_GCM_SHA384");
49
50 // As of 2/5/25, some KMS hosts support ML-KEM, while other hosts still only support earlier
51 // draft PQ KEM groups. As such, we currently assert that any KEM group was negotiated.
52 // After ML-KEM is fully supported, this test should be updated to assert that ML-KEM was
53 // negotiated: https://github.com/aws/s2n-tls/issues/5086.
54 let _ = tls.as_ref().kem_group_name().unwrap();
55
56 Ok(())
57 }
58 }
59
60 #[test_log::test(tokio::test)]
61 async fn tls_client() -> Result<(), Box<dyn std::error::Error>> {
62 // The akamai request should be in internet_https_client.rs but Akamai
63 // http requests hang indefinitely. This behavior is also observed with
64 // curl and chrome. https://github.com/aws/s2n-tls/issues/4883
65 const DOMAINS: &[&str] = &["www.akamai.com"];
66
67 for domain in DOMAINS {
68 tracing::info!("querying {domain}");
69
70 let tls12 = handshake_with_domain(domain, &security::TESTING_TLS12).await?;
71 assert_eq!(tls12.as_ref().actual_protocol_version()?, Version::TLS12);
72
73 let tls13 = handshake_with_domain(domain, &security::DEFAULT_TLS13).await?;
74 assert_eq!(tls13.as_ref().actual_protocol_version()?, Version::TLS13);
75 }
76
77 Ok(())
78 }