[control] Domi CA and UI sign-in: intermediate renewal by CSR with manual offline signing, domi host certificates, device requests approved by code, one-time PKCS#12 installs, revocation; Remote-User trusted only with edge's per-boot proof

This commit is contained in:
2026-11-07 23:21:49 +01:00
parent 211b851c3e
commit c18ae154ad
17 changed files with 1300 additions and 54 deletions
+169
View File
@@ -9,6 +9,11 @@
//! ianitor-control token list (--config <c> | --state-dir <d>)
//! ianitor-control token revoke <id> (--config <c> | --state-dir <d>)
//! ianitor-control openapi print the OpenAPI document
//! ianitor-control pki approve|deny <code> (--config <c> | --state-dir <d>) approve a device request
//! ianitor-control pki install <intermediate.crt> (--config <c>) install a signed intermediate
//! ianitor-control pki status (--config <c>) open requests
//! ianitor-control ca init --out <dir> [--passphrase-file <f>] the offline root (never on the server)
//! ianitor-control ca sign --root <dir> --csr <f> --out <f> [--yes] sign an intermediate offline
//! ianitor-control check --config <control.toml> check the config and exit
//! ```
//! The log level comes from `$IANITOR_LOG`. As `GIT_ASKPASS` (with `$IANITOR_ASKPASS=1`),
@@ -197,6 +202,168 @@ fn token(args: &[String]) -> Result<(), String> {
}
}
fn arg_after(args: &[String], name: &str) -> Option<String> {
let mut it = args.iter();
while let Some(a) = it.next() {
if a == name {
return it.next().cloned();
}
}
None
}
/// A passphrase from `--passphrase-file`, or asked on the terminal without echo.
fn passphrase(args: &[String], confirm: bool) -> Result<String, String> {
if let Some(f) = arg_after(args, "--passphrase-file") {
let v = std::fs::read_to_string(&f).map_err(|e| format!("cannot read {f}: {e}"))?;
let v = v.trim_end_matches(['\r', '\n']).to_owned();
if v.chars().count() < 12 {
return Err("the passphrase must have at least 12 characters".into());
}
return Ok(v);
}
let p = rpassword::prompt_password("Root key passphrase: ").map_err(|e| e.to_string())?;
if p.chars().count() < 12 {
return Err("the passphrase must have at least 12 characters".into());
}
if confirm {
let again = rpassword::prompt_password("Again: ").map_err(|e| e.to_string())?;
if again != p {
return Err("the two passphrases differ".into());
}
}
Ok(p)
}
/// `ca init` and `ca sign`: the offline root (ROADMAP §1.2; docs/pki.md). Never run on the
/// server.
fn ca(args: &[String]) -> Result<(), String> {
use ianitor_core::pki;
let now = ianitor_control::util::now() as i64;
match args.first().map(String::as_str) {
Some("init") => {
let out = PathBuf::from(arg_after(args, "--out").ok_or("ca init --out <dir>")?);
let crt = out.join("domi-root.crt");
let key = out.join("domi-root.key");
if crt.exists() || key.exists() {
return Err(format!(
"{} already has a root; refusing to replace it",
out.display()
));
}
std::fs::create_dir_all(&out).map_err(|e| e.to_string())?;
let pass = passphrase(args, true)?;
let (cert, k) = pki::create_root(now).map_err(|e| e.to_string())?;
let sealed = pki::encrypt_key(&k, pass.as_bytes()).map_err(|e| e.to_string())?;
ianitor_control::util::write_atomic(&key, sealed.as_bytes(), 0o600)
.map_err(|e| e.to_string())?;
ianitor_control::util::write_atomic(&crt, cert.as_bytes(), 0o644)
.map_err(|e| e.to_string())?;
let info = pki::inspect(&cert).map_err(|e| e.to_string())?;
eprintln!(
"Domi root created in {}: {} (valid until {}).\nSHA-256 {}\n\
Copy domi-root.crt to the server's configuration (services.ianitor.pki.rootCertificate).\n\
Keep domi-root.key and its passphrase offline (Custos, once it can hold it).",
out.display(),
info.subject_cn,
ianitor_core::time::long_date(info.not_after.max(0) as u64),
info.sha256_hex
);
Ok(())
}
Some("sign") => {
let root = PathBuf::from(
arg_after(args, "--root")
.ok_or("ca sign --root <dir> --csr <file> --out <file>")?,
);
let csr_path = arg_after(args, "--csr").ok_or("--csr <file> is required")?;
let out = PathBuf::from(arg_after(args, "--out").ok_or("--out <file> is required")?);
let csr = std::fs::read_to_string(&csr_path)
.map_err(|e| format!("cannot read {csr_path}: {e}"))?;
let code = pki::match_code(&pki::csr_der(&csr).map_err(|e| e.to_string())?);
eprintln!("This request's code is {code}. It must be the code Ianitor shows.");
if !args.iter().any(|a| a == "--yes") {
let typed = rpassword::prompt_password("Type the code to sign (no echo): ")
.map_err(|e| e.to_string())?;
if ianitor_control::pki::normalise_code(&typed)
!= ianitor_control::pki::normalise_code(&code)
{
return Err("the code does not match; nothing was signed".into());
}
}
let cert =
std::fs::read_to_string(root.join("domi-root.crt")).map_err(|e| e.to_string())?;
let sealed =
std::fs::read_to_string(root.join("domi-root.key")).map_err(|e| e.to_string())?;
let pass = passphrase(args, false)?;
let key = pki::decrypt_key(&sealed, pass.as_bytes()).map_err(|e| e.to_string())?;
let signed =
pki::sign_intermediate(&cert, &key, &csr, now).map_err(|e| e.to_string())?;
ianitor_control::util::write_atomic(&out, signed.as_bytes(), 0o644)
.map_err(|e| e.to_string())?;
let info = pki::inspect(&signed).map_err(|e| e.to_string())?;
eprintln!(
"Signed {} until {}. Install it on the server: ianitor-control pki install {}",
info.subject_cn,
ianitor_core::time::long_date(info.not_after.max(0) as u64),
out.display()
);
Ok(())
}
_ => Err("ca init --out <dir> | ca sign --root <dir> --csr <file> --out <file>".into()),
}
}
/// `pki approve|deny|install|status` on the server, as the user control runs as.
fn pki_cli(args: &[String]) -> Result<(), String> {
let dir = state_dir(args)?;
same_owner(&dir)?;
let now = ianitor_control::util::now();
match args.first().map(String::as_str) {
Some(verb @ ("approve" | "deny")) => {
// The code may be given as one word or three (47 19 83).
let code: String = args
.iter()
.skip(1)
.take_while(|a| !a.starts_with("--"))
.cloned()
.collect::<Vec<_>>()
.join("");
let r = ianitor_control::pki::cli_approve(&dir, &code, verb == "deny", now)?;
eprintln!(
"{} the request for \"{}\" ({}). Control acts on it within a few seconds.",
if verb == "deny" { "Turned down" } else { "Approved" },
r.name.unwrap_or_default(),
r.id
);
Ok(())
}
Some("install") => {
let f = args.get(1).filter(|a| !a.starts_with("--")).ok_or("pki install <intermediate.crt>")?;
let pem = std::fs::read_to_string(f).map_err(|e| format!("cannot read {f}: {e}"))?;
let r = ianitor_control::pki::cli_install(&dir, &pem, now)?;
eprintln!("Handed to renewal {}. Control installs it within a few seconds.", r.id);
Ok(())
}
Some("status") => {
let rs: Vec<ianitor_control::pki::Request> =
ianitor_control::util::read_json(&ianitor_control::pki::requests_path(&dir))?.unwrap_or_default();
for r in rs.iter().filter(|r| r.state == "waiting" && r.expires_at > now) {
println!(
"{} {} {} code {} until {}",
r.id,
r.kind,
r.name.clone().unwrap_or_default(),
r.match_code,
ianitor_control::util::rfc3339(r.expires_at)
);
}
Ok(())
}
_ => Err("pki approve <code> | pki deny <code> | pki install <cert> | pki status (--config <c> | --state-dir <d>)".into()),
}
}
fn main() -> ExitCode {
if std::env::var("IANITOR_ASKPASS").as_deref() == Ok("1") {
return askpass();
@@ -215,6 +382,8 @@ fn main() -> ExitCode {
};
let result = match args.first().map(String::as_str) {
Some("token") => token(&args[1..]),
Some("pki") => pki_cli(&args[1..]),
Some("ca") => ca(&args[1..]),
Some("openapi") => {
println!("{}", ianitor_control::api::openapi_json());
Ok(())