From 5cda7b4d15c71b24812a4d03259b8a8494ebb13e Mon Sep 17 00:00:00 2001 From: losfair Date: Wed, 28 Oct 2026 06:55:55 -0400 Subject: [PATCH] [verified] fix: allow TLS handshakes without SNI --- README.md | 27 ++++++++++++---------- src/e2e_test.rs | 61 +++++++++++++++++++++++++++++++++++++++++++++++++ src/ech_test.rs | 2 ++ src/quic.rs | 10 ++++---- 4 files changed, 83 insertions(+), 17 deletions(-) diff --git a/README.md b/README.md index ee36c48..8537a62 100644 --- a/README.md +++ b/README.md @@ -71,11 +71,14 @@ CLANG=clang-22 CLANGXX=clang++-22 .github/scripts/cross-openbsd.sh arm64 out/rap The forwarding **target is pinned on the server** — clients cannot choose a destination. The server loads a **rule table** mapping endpoints to targets, and the rule authorities also define the allowed TLS SNI hostnames. Connections with -missing or non-matching SNI are rejected during the TLS handshake; accepted -CONNECT-UDP requests are then matched on `:authority` + path (the query string -is ignored, so generic MASQUE clients that carry `target_host`/`target_port` in -the query still interoperate; the server ignores those and uses the rule's -target). +non-matching SNI are rejected during the TLS handshake. Connections without SNI +are accepted, so IP-literal endpoints can use certificates with IP subject +alternative names (including Let's Encrypt IP certificates). Clients still verify +the certificate against the endpoint's IP address; no `--insecure` is needed. +All CONNECT-UDP requests, with or without SNI, are matched on `:authority` + path +(the query string is ignored, so generic MASQUE clients that carry +`target_host`/`target_port` in the query still interoperate; the server ignores +those and uses the rule's target). The **client is a local UDP proxy**: it binds a UDP socket and tunnels every datagram received there through the proxy, opening one CONNECT-UDP flow per local @@ -491,19 +494,19 @@ cleartext cover identity; it only needs a valid certificate for the ECH with the server's fresh `retry_configs`. A cert covering both names handles both paths. -The server rejects ClientHellos whose SNI does not match any configured rule -authority host. With ECH accepted, this check is applied to the decrypted inner -SNI; rejected ECH fallbacks and ordinary non-ECH handshakes are checked against -the cleartext SNI. +The server rejects ClientHellos whose SNI is present but does not match any +configured rule authority host. Missing SNI is allowed for IP-literal endpoints. +With ECH accepted, this check is applied to the decrypted inner SNI; rejected ECH +fallbacks and ordinary non-ECH handshakes are checked against the cleartext SNI. Both ends log whether ECH was accepted on each connection (`ech=true`). ### Access control By default the proxy performs **no client authentication**: any peer that can -reach it, complete TLS with a configured SNI host, and send a CONNECT-UDP request -matching a rule's authority + path is granted a tunnel. (The client still -authenticates the *server* via TLS unless `--insecure`.) +reach it, complete TLS (with a configured SNI host or no SNI), and send a +CONNECT-UDP request matching a rule's authority + path is granted a tunnel. (The +client still authenticates the *server* via TLS unless `--insecure`.) A secret can be embedded in a rule's endpoint **path** as a lightweight shared-secret gate. The server matches on the path component (the query string is diff --git a/src/e2e_test.rs b/src/e2e_test.rs index 0a08551..e111689 100644 --- a/src/e2e_test.rs +++ b/src/e2e_test.rs @@ -158,6 +158,67 @@ async fn ech_accepted_over_quic() { assert_eq!(server_hs.server_name.as_deref(), Some(HOST)); } +#[tokio::test(flavor = "multi_thread")] +async fn missing_sni_is_accepted_with_ip_certificate() { + let fx = Fixture::new("no-sni"); + // The SNI allowlist must not reject a client that sends no SNI at all. + let server = fx.server(HOST); + let addr = server.local_addr().unwrap(); + let accept = tokio::spawn(async move { + for _ in 0..2 { + let conn = server.accept().await.unwrap().await.unwrap(); + assert_eq!(handshake_data(&conn).server_name, None); + conn.closed().await; + } + }); + + let client = client_endpoint(false); + for ip in ["127.0.0.1", "::1"] { + // Production ClientTls omits SNI for IP literals and verifies IP SANs. + let conn = tokio::time::timeout( + Duration::from_secs(5), + client + .connect_with(fx.client_config(false), addr, ip) + .unwrap(), + ) + .await + .expect("IP-literal handshake must not time out") + .expect("IP-literal handshake without SNI must succeed"); + conn.close(0u32.into(), b""); + } + accept.await.unwrap(); +} + +#[tokio::test(flavor = "multi_thread")] +async fn missing_sni_does_not_bypass_certificate_verification() { + let fx = Fixture::new("no-sni-verify"); + let server = fx.server(HOST); + let addr = server.local_addr().unwrap(); + tokio::spawn(async move { + while let Some(incoming) = server.accept().await { + let _ = incoming.await; + } + }); + + let other = Fixture::new("no-sni-verify-other"); + let client = client_endpoint(false); + for (config, ip) in [ + (fx.client_config(false), "127.0.0.2"), + (other.client_config(false), "127.0.0.1"), + ] { + let res = tokio::time::timeout( + Duration::from_secs(5), + client.connect_with(config, addr, ip).unwrap(), + ) + .await + .expect("certificate rejection must not time out"); + assert!( + res.is_err(), + "a mismatched IP SAN or untrusted certificate must still fail" + ); + } +} + #[tokio::test(flavor = "multi_thread")] async fn unmatched_sni_is_rejected() { let fx = Fixture::new("sni"); diff --git a/src/ech_test.rs b/src/ech_test.rs index 8f8cca9..2192497 100644 --- a/src/ech_test.rs +++ b/src/ech_test.rs @@ -139,6 +139,8 @@ pub(crate) fn self_signed() -> ( let san = SubjectAlternativeName::new() .dns("secret.internal") .dns("public.example.com") + .ip("127.0.0.1") + .ip("::1") .build(&b.x509v3_context(None, None)) .unwrap(); b.append_extension(san).unwrap(); diff --git a/src/quic.rs b/src/quic.rs index b092237..bc93000 100644 --- a/src/quic.rs +++ b/src/quic.rs @@ -373,14 +373,14 @@ pub(crate) fn install_ech_keys(builder: &mut SslContextBuilder, ech: &EchKeySet) Ok(()) } -/// Reject handshakes whose ClientHello SNI is missing or not one of the -/// configured proxy endpoint hosts. With ECH accepted, BoringSSL exposes the -/// protected inner SNI here; without ECH this checks the ordinary SNI. +/// Reject handshakes whose ClientHello SNI is present but not one of the +/// configured proxy endpoint hosts. Clients connecting to an IP literal omit +/// SNI and authenticate the server using its certificate's IP SAN instead. +/// With ECH accepted, BoringSSL exposes the protected inner SNI here. pub(crate) fn install_sni_guard(builder: &mut SslContextBuilder, allowed_hosts: Vec) { builder.set_select_certificate_callback(move |client_hello| { let Some(sni) = client_hello.servername(NameType::HOST_NAME) else { - log::debug!("rejecting TLS ClientHello without SNI"); - return Err(SelectCertError::ERROR); + return Ok(()); }; if allowed_hosts.iter().any(|h| h.eq_ignore_ascii_case(sni)) { return Ok(());