Files
mnovak acfae4b6e8 [repo] Licence: CPAL-1.0 file headers instead of the MPL-2.0 notice
Each source file's three-line MPL-2.0 notice becomes an SPDX CPAL-1.0 header with the copyright
line; the Exhibit A notice itself is in LICENSE (CPAL 3.5). Shell scripts' SPDX ids follow.
Comments only: no code changes.
2026-11-23 00:19:31 +01:00

279 lines
9.1 KiB
Rust

// SPDX-License-Identifier: CPAL-1.0
// Copyright (c) 2026 Marek Novák. Exhibit A notice: LICENSE at the repository root.
//! The root signs only through `Signer` (tests first, docs/01): once per operation, a signer whose
//! key is not the root's is refused, a lying signer is caught, and CSRs never steer the profile.
#![allow(clippy::unwrap_used, clippy::expect_used, clippy::panic)]
mod common;
use common::*;
use domi_pki::{self as pki, INTERMEDIATE_DAYS, PkiError, Signer, SignerError};
#[test]
fn the_root_signs_once_per_operation_through_the_signer() {
let root = CustosLike::new();
let root_pem = pki::create_root(&root, NOW).unwrap();
assert_eq!(root.signed.get(), 1, "the self-signature");
let _ = intermediate(&root, &root_pem, pki::AGENTS_INTERMEDIATE_CN);
assert_eq!(root.signed.get(), 2, "one signature per intermediate");
assert_eq!(pki::public_key_der(&root_pem).unwrap(), root.spki_der());
}
#[test]
fn issuing_leaves_never_asks_the_root() {
let c = ca();
let before = c.root.signed.get();
let (_, csr) = agent_csr("a1");
pki::issue_agent(&c.agents, &csr, "acme", "a1", NOW).unwrap();
pki::issue_server(&c.agents, "fabrica.home.test", NOW).unwrap();
pki::issue_device(&c.ianitor, "phone", NOW).unwrap();
assert_eq!(c.root.signed.get(), before);
}
#[test]
fn a_signer_that_is_not_the_root_is_refused_before_signing() {
let c = ca();
let impostor = CustosLike::new();
let key = pki::generate_key().unwrap();
let csr = pki::intermediate_csr(&key, pki::AGENTS_INTERMEDIATE_CN).unwrap();
assert!(matches!(
pki::sign_intermediate(
&c.root_pem,
&impostor,
&csr,
pki::AGENTS_INTERMEDIATE_CN,
NOW
),
Err(PkiError::KeyMismatch)
));
assert_eq!(impostor.signed.get(), 0);
}
struct Liar(CustosLike);
impl Signer for Liar {
fn spki_der(&self) -> Vec<u8> {
self.0.spki_der()
}
fn sign(&self, msg: &[u8]) -> Result<Vec<u8>, SignerError> {
let mut s = self.0.sign(msg)?;
if let Some(b) = s.last_mut() {
*b ^= 1;
}
Ok(s)
}
}
struct Refuses(CustosLike);
impl Signer for Refuses {
fn spki_der(&self) -> Vec<u8> {
self.0.spki_der()
}
fn sign(&self, _: &[u8]) -> Result<Vec<u8>, SignerError> {
Err(SignerError::new("the approval was declined"))
}
}
#[test]
fn a_bad_signature_from_the_signer_is_caught() {
let liar = Liar(CustosLike::new());
assert!(matches!(
pki::create_root(&liar, NOW),
Err(PkiError::Signer(_))
));
let c = ca();
let liar = Liar(c.root);
let key = pki::generate_key().unwrap();
let csr = pki::intermediate_csr(&key, pki::AGENTS_INTERMEDIATE_CN).unwrap();
assert!(matches!(
pki::sign_intermediate(&c.root_pem, &liar, &csr, pki::AGENTS_INTERMEDIATE_CN, NOW),
Err(PkiError::Signer(_))
));
}
#[test]
fn a_declined_signature_is_reported_as_such() {
let r = Refuses(CustosLike::new());
match pki::create_root(&r, NOW) {
Err(PkiError::Signer(m)) => assert!(m.contains("declined"), "{m}"),
other => panic!("{other:?}"),
}
}
#[test]
fn a_signer_with_an_unsupported_key_is_refused() {
struct Garbage;
impl Signer for Garbage {
fn spki_der(&self) -> Vec<u8> {
b"not a key".to_vec()
}
fn sign(&self, _: &[u8]) -> Result<Vec<u8>, SignerError> {
Ok(vec![0; 64])
}
}
assert!(matches!(
pki::create_root(&Garbage, NOW),
Err(PkiError::UnsupportedKey)
));
}
#[test]
fn the_intermediate_ignores_what_the_csr_asks_for() {
let c = ca();
let key = pki::generate_key().unwrap();
let greedy = pki::testing::greedy_csr(&key, pki::AGENTS_INTERMEDIATE_CN).unwrap();
let pem = pki::sign_intermediate(
&c.root_pem,
&c.root,
&greedy,
pki::AGENTS_INTERMEDIATE_CN,
NOW,
)
.unwrap();
let info = pki::inspect(&pem).unwrap();
assert_eq!(info.path_len, Some(0));
assert!(info.dns_names.is_empty());
assert_eq!((info.not_after - NOW) / DAY, INTERMEDIATE_DAYS as i64);
// The wrong subject is refused: not this intermediate's name, nor the other's.
for wrong in ["Some Other CA", pki::IANITOR_INTERMEDIATE_CN] {
let csr = pki::intermediate_csr(&key, wrong).unwrap();
assert!(matches!(
pki::sign_intermediate(&c.root_pem, &c.root, &csr, pki::AGENTS_INTERMEDIATE_CN, NOW),
Err(PkiError::CsrSubject)
));
}
let csr = pki::intermediate_csr(&key, pki::AGENTS_INTERMEDIATE_CN).unwrap();
assert!(
pki::sign_intermediate(
&c.root_pem,
&c.root,
&pki::testing::tamper_csr(&csr),
pki::AGENTS_INTERMEDIATE_CN,
NOW
)
.is_err()
);
assert!(
pki::sign_intermediate(
&c.root_pem,
&c.root,
"not a csr",
pki::AGENTS_INTERMEDIATE_CN,
NOW
)
.is_err()
);
}
#[test]
fn an_intermediate_must_match_its_key_and_be_a_ca() {
let c = ca();
let other_key = pki::generate_key().unwrap();
assert!(matches!(
pki::Intermediate::new(c.agents.cert_pem(), other_key),
Err(PkiError::KeyMismatch)
));
assert!(matches!(
pki::Intermediate::new(&c.root_pem, pki::generate_key().unwrap()),
Err(PkiError::KeyMismatch)
));
}
#[test]
fn a_local_root_works_for_the_offline_machine_and_tests() {
let (root_pem, root_key) = pki::create_root_local(NOW).unwrap();
let key = pki::generate_key().unwrap();
let csr = pki::intermediate_csr(&key, pki::IANITOR_INTERMEDIATE_CN).unwrap();
let pem = pki::sign_intermediate(
&root_pem,
&root_key,
&csr,
pki::IANITOR_INTERMEDIATE_CN,
NOW,
)
.unwrap();
assert!(pki::issued_by(&pem, &root_pem).unwrap());
// The openssl profile for the manual step matches.
assert!(pki::INTERMEDIATE_OPENSSL_EXT.contains("pathlen:0"));
assert!(pki::openssl_sign_command("int-2027").contains("-days 365"));
}
#[test]
fn keys_and_match_codes() {
let k = pki::generate_key().unwrap();
assert_eq!(format!("{k:?}"), "KeyPair(…)", "never printed");
let back = pki::KeyPair::from_der(&k.serialize_der()).unwrap();
assert_eq!(back.public_key_der(), k.public_key_der());
assert!(pki::KeyPair::from_pem("nope").is_err());
let a = pki::match_code(b"some csr");
assert_eq!(a, pki::match_code(b"some csr"));
assert_ne!(a, pki::match_code(b"other csr"));
let parts: Vec<&str> = a.split(' ').collect();
assert_eq!(parts.len(), 3);
assert!(
parts
.iter()
.all(|p| p.len() == 2 && p.bytes().all(|b| b.is_ascii_digit()))
);
}
#[test]
fn hostile_pem_and_der_never_panic() {
let c = ca();
for bad in [
"",
"-----BEGIN CERTIFICATE-----\n-----END CERTIFICATE-----\n",
"-----BEGIN CERTIFICATE-----\nAAAA\n-----END CERTIFICATE-----\n",
// Split so that this file does not match the pre-commit hook.
concat!(
"-----BEGIN PRIVATE",
" KEY-----\nAAAA\n-----END PRIVATE",
" KEY-----\n"
),
"\u{0}\u{1}",
] {
assert!(pki::inspect(bad).is_err());
assert!(pki::issued_by(bad, bad).is_err());
assert!(pki::public_key_der(bad).is_err());
assert!(pki::csr_public_key_der(bad).is_err());
assert!(pki::Intermediate::new(bad, pki::generate_key().unwrap()).is_err());
assert!(pki::issue_agent(&c.agents, bad, "acme", "a1", NOW).is_err());
}
for junk in [&b""[..], b"\x30\x03\x02\x01", &[0xff; 300]] {
assert!(pki::inspect_der(junk).is_err());
assert!(pki::verify_agent(&[junk.to_vec()], &c.root_der(), NOW).is_err());
}
}
/// A root held as Ed25519 (Custos's minisign-style keys are Ed25519).
struct EdRoot(ring::signature::Ed25519KeyPair);
impl Signer for EdRoot {
fn spki_der(&self) -> Vec<u8> {
use ring::signature::KeyPair as _;
// SubjectPublicKeyInfo: SEQUENCE { SEQUENCE { OID 1.3.101.112 }, BIT STRING key }.
let mut d = vec![
0x30, 0x2a, 0x30, 0x05, 0x06, 0x03, 0x2b, 0x65, 0x70, 0x03, 0x21, 0x00,
];
d.extend_from_slice(self.0.public_key().as_ref());
d
}
fn sign(&self, msg: &[u8]) -> Result<Vec<u8>, SignerError> {
Ok(self.0.sign(msg).as_ref().to_vec())
}
}
#[test]
fn an_ed25519_root_signer_works_end_to_end() {
let rng = ring::rand::SystemRandom::new();
let pkcs8 = ring::signature::Ed25519KeyPair::generate_pkcs8(&rng).unwrap();
let root = EdRoot(ring::signature::Ed25519KeyPair::from_pkcs8(pkcs8.as_ref()).unwrap());
let root_pem = pki::create_root(&root, NOW).unwrap();
let agents = intermediate(&root, &root_pem, pki::AGENTS_INTERMEDIATE_CN);
let (_, csr) = agent_csr("a1");
let a = pki::issue_agent(&agents, &csr, "acme", "a1", NOW).unwrap();
let root_der = pki::chain_der(&root_pem).unwrap().remove(0);
let id = pki::verify_agent(&ders(&a.chain_pem), &root_der, NOW + DAY).unwrap();
assert_eq!((id.team.as_str(), id.agent.as_str()), ("acme", "a1"));
}