[edge] Device certificates as Caddy does them: no certificate on a device host fails the handshake, a Host other than the SNI name is 421; diff corpus gains six device cases
This commit is contained in:
@@ -121,10 +121,11 @@ pub fn server(shared: Arc<Shared>, opts: &Options) -> Result<Server, EdgeError>
|
||||
connector: Connector::new(None),
|
||||
},
|
||||
);
|
||||
let mut tls = TlsSettings::intermediate("", "").map_err(|e| EdgeError::Listen {
|
||||
address: rt.cfg.https_listen.clone(),
|
||||
reason: e.to_string(),
|
||||
})?;
|
||||
let mut tls =
|
||||
TlsSettings::with_callbacks(Box::new(tls::SniRecorder)).map_err(|e| EdgeError::Listen {
|
||||
address: rt.cfg.https_listen.clone(),
|
||||
reason: e.to_string(),
|
||||
})?;
|
||||
tls.set_cert_resolver(shared.certs.clone());
|
||||
tls.set_client_cert_verifier(shared.devices.clone());
|
||||
tls.enable_h2();
|
||||
|
||||
@@ -131,6 +131,13 @@ impl ProxyHttp for EdgeProxy {
|
||||
let rt = self.shared.current();
|
||||
ctx.rt = Some(rt.clone());
|
||||
ctx.host_header = host_header(session.req_header());
|
||||
if rt.device_trust.is_some() && sni_mismatch(session, &hostname(&ctx.host_header)) {
|
||||
// Like Caddy once client certificates are configured: a request for another host
|
||||
// than the one the TLS connection was opened for is refused, so a connection
|
||||
// that was never asked for a certificate cannot reach a device host.
|
||||
respond_empty(session, 421).await?;
|
||||
return Ok(true);
|
||||
}
|
||||
let Some(host) = rt.host(&hostname(&ctx.host_header)).cloned() else {
|
||||
respond_empty(session, 404).await?;
|
||||
return Ok(true);
|
||||
@@ -419,6 +426,18 @@ impl EdgeProxy {
|
||||
}
|
||||
}
|
||||
|
||||
/// Was this TLS connection opened (by SNI) for another host than the request names?
|
||||
fn sni_mismatch(session: &Session, host: &str) -> bool {
|
||||
let Some(s) = session.digest().and_then(|d| d.ssl_digest.as_ref()) else {
|
||||
return false;
|
||||
};
|
||||
match s.extension.get::<crate::tls::ConnSni>() {
|
||||
Some(crate::tls::ConnSni(Some(sni))) => sni != host,
|
||||
// TLS without a recorded name: refuse rather than guess.
|
||||
_ => true,
|
||||
}
|
||||
}
|
||||
|
||||
/// Did this connection present a device certificate that the current config allows? The
|
||||
/// handshake already checked the chain and the list; this checks again against the runtime
|
||||
/// serving the request, so a revocation also ends requests on connections made before it.
|
||||
|
||||
+24
-3
@@ -220,9 +220,10 @@ impl ClientCertVerifier for DeviceVerifier {
|
||||
}
|
||||
|
||||
fn client_auth_mandatory(&self) -> bool {
|
||||
// A missing certificate is a 403 from the proxy, like Caddy's route, not a failed
|
||||
// handshake.
|
||||
false
|
||||
// Only asked when a certificate was requested, i.e. on a device host: there a
|
||||
// handshake without one fails, as with Caddy (its allow-list check refuses an empty
|
||||
// certificate list in the handshake too).
|
||||
self.current().is_some()
|
||||
}
|
||||
|
||||
fn root_hint_subjects(&self) -> &[rustls::DistinguishedName] {
|
||||
@@ -276,3 +277,23 @@ impl ClientCertVerifier for DeviceVerifier {
|
||||
fn same_names(a: &[rustls::DistinguishedName], b: &[rustls::DistinguishedName]) -> bool {
|
||||
a.len() == b.len() && a.iter().zip(b).all(|(x, y)| x.as_ref() == y.as_ref())
|
||||
}
|
||||
|
||||
/// The server name a TLS connection was opened for, kept in the connection's digest so the
|
||||
/// proxy can compare it with the `Host` header.
|
||||
#[derive(Debug, Clone)]
|
||||
pub struct ConnSni(pub Option<String>);
|
||||
|
||||
/// Records [`ConnSni`] when a handshake completes.
|
||||
pub struct SniRecorder;
|
||||
|
||||
#[async_trait::async_trait]
|
||||
impl pingora::listeners::TlsAccept for SniRecorder {
|
||||
async fn handshake_complete_callback(
|
||||
&self,
|
||||
tls: &pingora::protocols::tls::TlsRef,
|
||||
) -> Option<Arc<dyn std::any::Any + Send + Sync>> {
|
||||
Some(Arc::new(ConnSni(
|
||||
tls.server_name().map(|s| s.to_ascii_lowercase()),
|
||||
)))
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user