[control] Fix round 1: changes apply while Caddy serves (admin socket, watch through Caddy), revocations that fail are errors and catch up, proof only on the forward-auth UI host, root keys found by content, intermediates checked before install, stale SNI cleared before each handshake

This commit is contained in:
2026-11-08 10:49:31 +01:00
parent 0749119f19
commit e84eb1ba74
36 changed files with 934 additions and 131 deletions
+51 -18
View File
@@ -212,10 +212,32 @@ fn bundle_password() -> Result<String, PkiFail> {
Ok(s)
}
/// Is there anything that looks like a root CA key in `dir` (recursively, shallow)? Control
/// refuses to run with one in its state directory (ROADMAP §1.2: the root is never on the
/// server).
/// Private keys in `dir` that are not control's own: control refuses to run with one in its
/// state directory (ROADMAP §1.2: the root is never on the server). Found by content (any
/// `PRIVATE KEY` PEM block, plain or encrypted) and by name (`*root*key*`, `*root*.pem` with
/// a key); the only keys allowed are `pki/intermediate.key`, `pki/pending-*.key` and
/// `pki/devices/*.key`, which control writes encrypted itself.
pub fn root_key_files(dir: &Path) -> Vec<PathBuf> {
let allowed = |p: &Path| -> bool {
let Ok(rel) = p.strip_prefix(dir) else {
return false;
};
let parts: Vec<String> = rel
.components()
.map(|c| c.as_os_str().to_string_lossy().into_owned())
.collect();
match parts
.iter()
.map(String::as_str)
.collect::<Vec<_>>()
.as_slice()
{
["pki", "intermediate.key"] => true,
["pki", f] => f.starts_with("pending-") && f.ends_with(".key"),
["pki", "devices", f] => f.ends_with(".key"),
_ => false,
}
};
let mut found = Vec::new();
let mut stack = vec![(dir.to_path_buf(), 0)];
while let Some((d, depth)) = stack.pop() {
@@ -224,12 +246,22 @@ pub fn root_key_files(dir: &Path) -> Vec<PathBuf> {
};
for e in rd.flatten() {
let p = e.path();
let name = e.file_name().to_string_lossy().to_ascii_lowercase();
if p.is_dir() {
if depth < 3 {
let Ok(meta) = e.metadata() else { continue };
if meta.is_dir() {
if depth < 8 {
stack.push((p, depth + 1));
}
} else if name.starts_with("root") && (name.ends_with(".key") || name.contains("key")) {
continue;
}
if !meta.is_file() || allowed(&p) {
continue;
}
let name = e.file_name().to_string_lossy().to_ascii_lowercase();
let by_name = name.contains("root") && name.contains("key");
let by_content = meta.len() <= 1 << 20
&& std::fs::read(&p)
.is_ok_and(|b| b.windows(KEY_MARK.len()).any(|w| w == KEY_MARK.as_bytes()));
if by_name || by_content {
found.push(p);
}
}
@@ -237,6 +269,9 @@ pub fn root_key_files(dir: &Path) -> Vec<PathBuf> {
found
}
// Split so that this file does not match the pre-commit hook itself.
const KEY_MARK: &str = concat!("PRIVATE ", "KEY-----");
impl Pki {
/// Open the CA state. The root certificate must be a CA and self-signed; the password
/// credential may be missing (then nothing can be issued, and the UI says why).
@@ -477,17 +512,15 @@ impl Pki {
}
let first = pki::first_pem(cert_pem).map_err(|_| PkiFail::Code("pki.bad_certificate"))?;
let info = pki::inspect(&first).map_err(|_| PkiFail::Code("pki.bad_certificate"))?;
if !pki::issued_by(&first, &self.root_pem).unwrap_or(false) {
return Err(PkiFail::Code("pki.not_our_root"));
}
if !info.is_ca
|| info.path_len != Some(0)
|| !info.subject_cn.starts_with(pki::INTERMEDIATE_CN)
{
return Err(PkiFail::Code("pki.bad_certificate"));
}
if info.not_before > now as i64 + 300 || info.not_after <= now as i64 {
return Err(PkiFail::Code("pki.bad_certificate"));
// Everything a hand-signed certificate can get wrong, before anything is reissued from it.
if let Some(problem) = pki::intermediate_problem(&first, &self.root_pem, now as i64) {
return Err(PkiFail::Code(match problem {
"not_root" => "pki.not_our_root",
"not_ca" => "pki.manual.not_ca",
"eku" => "pki.manual.eku",
"dates" => "pki.manual.dates",
_ => "pki.bad_certificate",
}));
}
let pending = self.dir.join(format!("pending-{request_id}.key"));
let key = self.open_key(&pending)?;