-
-
Notifications
You must be signed in to change notification settings - Fork 394
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add a failing test with post quantum key exchange
- Loading branch information
1 parent
816d802
commit efb41ef
Showing
4 changed files
with
115 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,109 @@ | ||
#![cfg(feature = "rustls-aws-lc-rs")] | ||
use quinn::{ | ||
crypto::rustls::{HandshakeData, QuicClientConfig, QuicServerConfig}, | ||
Endpoint, | ||
}; | ||
use rustls::{ | ||
pki_types::{CertificateDer, PrivatePkcs8KeyDer}, | ||
NamedGroup, | ||
}; | ||
use std::{ | ||
error::Error, | ||
net::{Ipv4Addr, SocketAddr}, | ||
sync::Arc, | ||
}; | ||
use tracing::info; | ||
|
||
#[tokio::test] | ||
async fn post_quantum_key_exchange() -> Result<(), Box<dyn Error + Send + Sync + 'static>> { | ||
let _ = tracing_subscriber::FmtSubscriber::builder() | ||
.with_env_filter(tracing_subscriber::EnvFilter::from_default_env()) | ||
.with_test_writer() | ||
.try_init(); | ||
|
||
rustls_post_quantum::provider().install_default().unwrap(); | ||
let server_addr = (Ipv4Addr::LOCALHOST, 8080).into(); | ||
|
||
let (endpoint, server_cert) = make_server_endpoint(server_addr)?; | ||
// accept a single connection | ||
tokio::spawn(async move { | ||
let incoming_conn = endpoint.accept().await.unwrap(); | ||
let conn = incoming_conn.await.unwrap(); | ||
info!( | ||
"[server] connection accepted: addr={}", | ||
conn.remote_address() | ||
); | ||
assert_eq!( | ||
conn.handshake_data() | ||
.unwrap() | ||
.downcast::<HandshakeData>() | ||
.unwrap() | ||
.negotiated_key_exchange_group, | ||
X25519_KYBER768_DRAFT00 | ||
) | ||
// Dropping all handles associated with a connection implicitly closes it | ||
}); | ||
|
||
let endpoint = make_client_endpoint((Ipv4Addr::UNSPECIFIED, 0).into(), &[&server_cert])?; | ||
// connect to server | ||
let connection = endpoint | ||
.connect(server_addr, "localhost") | ||
.unwrap() | ||
.await | ||
.unwrap(); | ||
info!("[client] connected: addr={}", connection.remote_address()); | ||
|
||
// Waiting for a stream will complete with an error when the server closes the connection | ||
let _ = connection.accept_uni().await; | ||
|
||
// Make sure the server has a chance to clean up | ||
endpoint.wait_idle().await; | ||
|
||
Ok(()) | ||
} | ||
|
||
fn make_client_endpoint( | ||
bind_addr: SocketAddr, | ||
server_certs: &[&[u8]], | ||
) -> Result<Endpoint, Box<dyn Error + Send + Sync + 'static>> { | ||
let mut certs = rustls::RootCertStore::empty(); | ||
for cert in server_certs { | ||
certs.add(CertificateDer::from(*cert))?; | ||
} | ||
let rustls_config = rustls::ClientConfig::builder() | ||
.with_root_certificates(certs) | ||
.with_no_client_auth(); | ||
|
||
let client_cfg = | ||
quinn::ClientConfig::new(Arc::new(QuicClientConfig::try_from(rustls_config).unwrap())); | ||
let mut endpoint = Endpoint::client(bind_addr)?; | ||
endpoint.set_default_client_config(client_cfg); | ||
Ok(endpoint) | ||
} | ||
|
||
fn make_server_endpoint( | ||
bind_addr: SocketAddr, | ||
) -> Result<(Endpoint, CertificateDer<'static>), Box<dyn Error + Send + Sync + 'static>> { | ||
let cert = rcgen::generate_simple_self_signed(vec!["localhost".into()]).unwrap(); | ||
let server_cert = CertificateDer::from(cert.cert); | ||
let priv_key = PrivatePkcs8KeyDer::from(cert.key_pair.serialize_der()); | ||
let mut server_config = quinn::ServerConfig::with_crypto(Arc::new( | ||
QuicServerConfig::try_from( | ||
rustls::ServerConfig::builder() | ||
.with_no_client_auth() | ||
.with_single_cert(vec![server_cert.clone()], priv_key.into()) | ||
.unwrap(), | ||
) | ||
.unwrap(), | ||
)); | ||
|
||
let transport_config = Arc::get_mut(&mut server_config.transport).unwrap(); | ||
transport_config.max_concurrent_uni_streams(0_u8.into()); | ||
transport_config.min_mtu(1274); | ||
|
||
let endpoint = Endpoint::server(server_config, bind_addr)?; | ||
Ok((endpoint, server_cert)) | ||
} | ||
|
||
/// <https://datatracker.ietf.org/doc/html/draft-tls-westerbaan-xyber768d00-02#name-iana-considerations-23> | ||
const X25519_KYBER768_DRAFT00: NamedGroup = NamedGroup::Unknown(0x06399); |