[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:
2026-11-07 22:55:37 +01:00
parent 3d8f524d00
commit a5190acc3b
7 changed files with 213 additions and 16 deletions
+1
View File
@@ -14,6 +14,7 @@ ianitor-testkit.workspace = true
tokio.workspace = true
serde_json.workspace = true
rustls.workspace = true
rcgen.workspace = true
[dev-dependencies]
ianitor-control = { path = "../../crates/control" }
+157 -3
View File
@@ -113,6 +113,10 @@ impl Env {
"rr.test",
&format!("upstreams = [\"{}\", \"{}\"]\n", rr_a.url(), rr_b.url()),
),
host(
"device.test",
&format!("upstreams = [\"{}\"]\ndevice_certs = true\n", app.url()),
),
];
let sources: Vec<SourceFile> = hosts
.iter()
@@ -145,6 +149,17 @@ impl Env {
std::fs::write(dir.join(&format!("certs/{}.key", h.hostname)), leaf.key_pem).unwrap();
}
std::fs::write(dir.join("creds/api_key"), format!("{SECRET}\n")).unwrap();
let d = devices();
std::fs::write(
dir.join(&format!("certs/{}", compile::DEVICE_CA_FILE)),
&d.ca_pem,
)
.unwrap();
std::fs::write(
dir.join(&format!("certs/{}", compile::DEVICE_ALLOW_FILE)),
&d.allow_pem,
)
.unwrap();
Env {
dir,
ca,
@@ -294,6 +309,12 @@ pub enum Action {
sni: &'static str,
req: Req,
},
/// HTTPS presenting a client certificate when asked.
HttpsDevice {
sni: &'static str,
req: Req,
who: Device,
},
Http {
req: Req,
},
@@ -578,6 +599,34 @@ pub fn corpus() -> Vec<Case> {
),
https("round robin 1", "rr.test", Req::get("rr.test", "/")),
https("round robin 2", "rr.test", Req::get("rr.test", "/")),
device("no device certificate", "device.test", Device::None),
device(
"Domi device certificate on the allow list",
"device.test",
Device::Allowed,
),
device(
"Domi device certificate not on the allow list",
"device.test",
Device::Unlisted,
),
device(
"self-signed certificate with a device's subject",
"device.test",
Device::SelfSigned,
),
device(
"client certificate on a host that does not ask for one",
"plain.test",
Device::SelfSigned,
),
Case {
name: "Host header naming the device host on another connection",
action: Action::Https {
sni: "plain.test",
req: Req::get("device.test", "/"),
},
},
]
}
@@ -644,6 +693,22 @@ pub async fn run_case(case: &Case, t: &Target, roots: Arc<RootCertStore>) -> Obs
Action::Https { sni, req } => client::https(t.https, sni, roots, req)
.await
.map_or_else(failed, |r| observe(&r, t)),
Action::HttpsDevice { sni, req, who } => {
let answer = match who {
Device::None => client::https(t.https, sni, roots, req).await,
_ => client::https_with_cert(t.https, sni, roots, &devices().cert(*who), req).await,
};
match answer {
Ok(r) => observe(&r, t),
// The two TLS stacks word their alerts differently; a refused handshake is
// what is compared (495, nginx's "SSL certificate error", as a marker).
Err(_) => Observation {
status: 495,
headers: BTreeMap::new(),
body: "handshake refused".into(),
},
}
}
Action::Http { req } => client::http(t.http, req)
.await
.map_or_else(failed, |r| observe(&r, t)),
@@ -764,9 +829,9 @@ pub const KNOWN_DIVERGENCES: &[Divergence] = &[
rule: Rule::Status {
case: "absolute-form target, other host",
ianitor: 400,
caddy: 404,
caddy: 421,
},
reason: "Pingora rejects a request whose target authority differs from Host (400); Go routes by the target (404 here)",
reason: "Pingora rejects a request whose target authority differs from Host (400); Go routes by the target, which is not the SNI name (421 once a host checks device certificates; 404 without)",
},
Divergence {
id: "KD-9",
@@ -908,7 +973,9 @@ fn short(o: &Observation) -> Observation {
/// The host a case is about (its SNI, or the Host header of plain-HTTP cases).
pub fn case_host(case: &Case) -> String {
match &case.action {
Action::Https { sni, .. } | Action::Websocket { sni, .. } => (*sni).to_owned(),
Action::Https { sni, .. }
| Action::HttpsDevice { sni, .. }
| Action::Websocket { sni, .. } => (*sni).to_owned(),
Action::Http { req } => req
.headers
.iter()
@@ -1008,3 +1075,90 @@ fn differences(i: &Observation, c: &Observation) -> Vec<String> {
}
out
}
/// Which client certificate a device case presents.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum Device {
/// No client certificate at all.
None,
/// Issued by the Domi intermediate and on the allow list.
Allowed,
/// Issued by the Domi intermediate, not on the allow list (revoked or never listed).
Unlisted,
/// Self-signed, with the same subject as the allowed one.
SelfSigned,
}
fn device(name: &'static str, host: &'static str, who: Device) -> Case {
Case {
name,
action: Action::HttpsDevice {
sni: host,
req: Req::get(host, "/"),
who,
},
}
}
/// One Domi PKI per test process, shared by every [`Env`] (each writes its files).
pub struct DevicePki {
pub ca_pem: String,
pub allow_pem: String,
allowed: client::ClientCert,
unlisted: client::ClientCert,
self_signed: client::ClientCert,
}
impl DevicePki {
pub fn cert(&self, who: Device) -> client::ClientCert {
match who {
Device::Allowed => self.allowed.clone(),
Device::Unlisted => self.unlisted.clone(),
Device::SelfSigned | Device::None => self.self_signed.clone(),
}
}
}
pub fn devices() -> &'static DevicePki {
use ianitor_core::pki;
static PKI: std::sync::OnceLock<DevicePki> = std::sync::OnceLock::new();
PKI.get_or_init(|| {
let now = std::time::SystemTime::now()
.duration_since(std::time::UNIX_EPOCH)
.unwrap()
.as_secs() as i64;
let (root, root_key) = pki::create_root(now).unwrap();
let k = pki::generate_key().unwrap();
let csr = pki::intermediate_csr(&k, pki::INTERMEDIATE_CN).unwrap();
let int_pem = pki::sign_intermediate(&root, &root_key, &csr, now).unwrap();
let int = pki::Intermediate::new(&int_pem, k).unwrap();
let cc = |i: pki::Issued| client::ClientCert {
chain_pem: i.chain_pem,
key_pem: i.key_pem,
tls12_only: false,
};
let phone = pki::issue_device(&int, "phone", now).unwrap();
let allow_pem = phone.cert_pem.clone();
let unlisted = pki::issue_device(&int, "laptop", now).unwrap();
let fake_key = rcgen::KeyPair::generate().unwrap();
let mut p = rcgen::CertificateParams::default();
let mut dn = rcgen::DistinguishedName::new();
dn.push(rcgen::DnType::CommonName, "phone");
dn.push(rcgen::DnType::OrganizationName, pki::ORG);
dn.push(rcgen::DnType::OrganizationalUnitName, "Domi devices");
p.distinguished_name = dn;
p.extended_key_usages = vec![rcgen::ExtendedKeyUsagePurpose::ClientAuth];
let fake = p.self_signed(&fake_key).unwrap();
DevicePki {
ca_pem: int.cert_pem().to_owned(),
allow_pem,
allowed: cc(phone),
unlisted: cc(unlisted),
self_signed: client::ClientCert {
chain_pem: fake.pem(),
key_pem: fake_key.serialize_pem(),
tls12_only: false,
},
}
})
}