diff --git a/noq-proto/src/connection/mod.rs b/noq-proto/src/connection/mod.rs index b6c602d5c..ec42d2562 100644 --- a/noq-proto/src/connection/mod.rs +++ b/noq-proto/src/connection/mod.rs @@ -7283,9 +7283,12 @@ impl Connection { /// your TLS Finished message. /// /// Implementation-wise this is the point at which the handshake crypto keys are - /// discarded. So we can use this to know if the handshake is confirmed. + /// discarded. The Initial keys must be gone too: a connection that closed in the Initial + /// space never had Handshake keys to discard, and was never confirmed. fn is_handshake_confirmed(&self) -> bool { - !self.is_handshaking() && !self.crypto_state.has_keys(EncryptionLevel::Handshake) + !self.is_handshaking() + && !self.crypto_state.has_keys(EncryptionLevel::Initial) + && !self.crypto_state.has_keys(EncryptionLevel::Handshake) } } @@ -7890,4 +7893,151 @@ mod tests { assert!(t.contains(&PathId(3))); assert_eq!(t.max(), Some(PathId(3))); } + + #[cfg(all(feature = "rustls", any(feature = "aws-lc-rs", feature = "ring")))] + #[test] + fn closed_in_initial_space_is_not_handshake_confirmed() { + use crate::crypto::rustls::{QuicClientConfig, QuicServerConfig}; + use crate::{DatagramEvent, Endpoint}; + use alloc::{format, string::ToString, vec}; + use core::net::{IpAddr, Ipv4Addr}; + + #[cfg(feature = "ring")] + let provider = Arc::new(crate::rustls::crypto::ring::default_provider()); + #[cfg(all(not(feature = "ring"), feature = "aws-lc-rs"))] + let provider = Arc::new(crate::rustls::crypto::aws_lc_rs::default_provider()); + + let key_pair = rcgen::KeyPair::generate().unwrap(); + let cert = rcgen::CertificateParams::new(vec!["localhost".to_string()]) + .unwrap() + .self_signed(&key_pair) + .unwrap(); + let key = crate::rustls::pki_types::PrivateKeyDer::Pkcs8( + crate::rustls::pki_types::PrivatePkcs8KeyDer::from(key_pair.serialize_der()), + ); + let mut server_crypto = + crate::rustls::ServerConfig::builder_with_provider(provider.clone()) + .with_protocol_versions(&[&crate::rustls::version::TLS13]) + .unwrap() + .with_no_client_auth() + .with_single_cert(vec![cert.der().clone()], key) + .unwrap(); + server_crypto.alpn_protocols = vec![b"other-proto".to_vec()]; + let mut server_config = + ServerConfig::with_crypto(Arc::new(QuicServerConfig::try_from(server_crypto).unwrap())); + server_config.migration = true; + + let mut roots = crate::rustls::RootCertStore::empty(); + roots.add(cert.der().clone()).unwrap(); + let mut client_crypto = crate::rustls::ClientConfig::builder_with_provider(provider) + .with_protocol_versions(&[&crate::rustls::version::TLS13]) + .unwrap() + .with_root_certificates(roots) + .with_no_client_auth(); + // No ALPN in common, and a ClientHello that takes two Initial datagrams + client_crypto.alpn_protocols = vec![b"h3".to_vec()]; + for i in 0..8u8 { + let mut proto = format!("pad-alpn-{i:02}-").into_bytes(); + proto.resize(200, b'a'); + client_crypto.alpn_protocols.push(proto); + } + let client_config = + crate::ClientConfig::new(Arc::new(QuicClientConfig::try_from(client_crypto).unwrap())); + + let mut server_ep = Endpoint::new( + Arc::new(EndpointConfig::default()), + Some(Arc::new(server_config)), + false, + ); + let mut client_ep = Endpoint::new(Arc::new(EndpointConfig::default()), None, false); + let now = Instant::now(); + let client_addr = SocketAddr::from((Ipv4Addr::LOCALHOST, 10002)); + let server_addr = SocketAddr::from((Ipv4Addr::LOCALHOST, 20002)); + let other_addr = SocketAddr::from((Ipv4Addr::LOCALHOST, 19998)); + + let (_, mut client_conn) = client_ep + .connect(now, client_config, server_addr, "localhost") + .unwrap(); + let mut datagrams = Vec::new(); + let mut buf = Vec::new(); + let mut sent_at = now; + loop { + while let Some(t) = client_conn.poll_transmit(sent_at, NonZeroUsize::MIN, &mut buf) { + datagrams.push(BytesMut::from(&buf[..t.size])); + buf.clear(); + } + if datagrams.len() >= 2 { + break; + } + // Before an RTT sample the pacer may hold the ClientHello's second datagram + sent_at = client_conn.poll_timeout().expect("pacing timer armed"); + client_conn.handle_timeout(sent_at); + } + + let mut response_buf = Vec::new(); + let Some(DatagramEvent::NewConnection(incoming)) = server_ep.handle( + now, + FourTuple::from_remote(client_addr), + None, + datagrams[0].clone(), + &mut response_buf, + ) else { + panic!("expected a new connection for the first Initial"); + }; + for datagram in &datagrams[1..] { + let event = server_ep.handle( + now, + FourTuple::from_remote(client_addr), + None, + datagram.clone(), + &mut response_buf, + ); + assert!(event.is_none()); + } + let (server_ch, mut server_conn) = server_ep + .accept(incoming, now, &mut response_buf, None) + .expect("the refused connection is returned closed"); + + // The server refused the ClientHello in the Initial space + assert!(server_conn.is_closed()); + assert!( + !server_conn + .crypto_state + .has_keys(EncryptionLevel::Handshake) + ); + assert!(!server_conn.is_handshake_confirmed()); + assert!(!server_conn.peer_may_probe()); + assert!(!server_conn.peer_may_migrate()); + + // So a datagram from another address is not taken as a migration + let pto = client_conn.poll_timeout().expect("client PTO armed"); + client_conn.handle_timeout(pto); + buf.clear(); + let t = client_conn + .poll_transmit(pto, NonZeroUsize::MIN, &mut buf) + .expect("client PTO probe"); + let recvd = server_conn.total_recvd(); + let Some(DatagramEvent::ConnectionEvent(ch, event)) = server_ep.handle( + pto, + FourTuple::from_remote(other_addr), + None, + BytesMut::from(&buf[..t.size]), + &mut response_buf, + ) else { + panic!("expected the probe to reach the server connection"); + }; + assert_eq!(ch, server_ch); + server_conn.handle_event(event); + assert_eq!(server_conn.total_recvd(), recvd); + + // A client that closes in the Initial space is not confirmed either + client_conn + .path_data_mut(PathId::ZERO) + .network_path + .local_ip = Some(IpAddr::V4(Ipv4Addr::LOCALHOST)); + client_conn.close(pto, 0u32.into(), Bytes::new()); + assert!(client_conn.is_closed()); + assert!(!client_conn.is_handshake_confirmed()); + assert!(!client_conn.local_ip_may_migrate()); + } }