[pki] CA: the Domi CA from Ianitor core behind a Signer, agent certificates, webpki chain checks (34 tests)
This commit is contained in:
+42
-10
@@ -17,7 +17,8 @@ fn an_agent_certificate_names_the_agent_and_its_team() {
|
||||
let c = ca();
|
||||
let (key, csr) = agent_csr("build-box-1");
|
||||
let a = pki::issue_agent(&c.agents, &csr, "acme", "build-box-1", NOW).unwrap();
|
||||
let id: AgentIdentity = pki::verify_agent(&ders(&a.chain_pem), &c.root_der(), NOW + DAY).unwrap();
|
||||
let id: AgentIdentity =
|
||||
pki::verify_agent(&ders(&a.chain_pem), &c.root_der(), NOW + DAY).unwrap();
|
||||
assert_eq!(id.team, "acme");
|
||||
assert_eq!(id.agent, "build-box-1");
|
||||
assert_eq!(id.serial_hex, a.serial_hex);
|
||||
@@ -25,12 +26,21 @@ fn an_agent_certificate_names_the_agent_and_its_team() {
|
||||
assert_eq!(id.not_after, a.not_after);
|
||||
assert_eq!((a.not_after - NOW) / DAY, AGENT_DAYS as i64);
|
||||
// The certificate carries the agent's own key: the CA never made or saw a private key.
|
||||
assert_eq!(pki::public_key_der(&a.cert_pem).unwrap(), key.public_key_der());
|
||||
assert_eq!(
|
||||
pki::public_key_der(&a.cert_pem).unwrap(),
|
||||
key.public_key_der()
|
||||
);
|
||||
let info = pki::inspect(&a.cert_pem).unwrap();
|
||||
assert_eq!(info.subject_cn, "build-box-1");
|
||||
assert!(!info.is_ca);
|
||||
assert_eq!(info.eku.map(|e| (e.client_auth, e.server_auth)), Some((true, false)));
|
||||
assert_eq!(pki::agent_uri("acme", "build-box-1"), "urn:domi:agent:acme:build-box-1");
|
||||
assert_eq!(
|
||||
info.eku.map(|e| (e.client_auth, e.server_auth)),
|
||||
Some((true, false))
|
||||
);
|
||||
assert_eq!(
|
||||
pki::agent_uri("acme", "build-box-1"),
|
||||
"urn:domi:agent:acme:build-box-1"
|
||||
);
|
||||
// Client auth only.
|
||||
assert_eq!(
|
||||
pki::verify_chain(&ders(&a.chain_pem), &c.root_der(), NOW + DAY, Usage::Server),
|
||||
@@ -59,7 +69,13 @@ fn a_device_or_server_certificate_is_not_an_agent() {
|
||||
let c = ca();
|
||||
// A phone's device certificate: same root, client auth, but Ianitor's intermediate.
|
||||
let dev = pki::issue_device(&c.ianitor, "phone", NOW).unwrap();
|
||||
pki::verify_chain(&ders(&dev.chain_pem), &c.root_der(), NOW + DAY, Usage::Client).unwrap();
|
||||
pki::verify_chain(
|
||||
&ders(&dev.chain_pem),
|
||||
&c.root_der(),
|
||||
NOW + DAY,
|
||||
Usage::Client,
|
||||
)
|
||||
.unwrap();
|
||||
assert_eq!(
|
||||
pki::verify_agent(&ders(&dev.chain_pem), &c.root_der(), NOW + DAY),
|
||||
Err(VerifyError::NotAnAgent)
|
||||
@@ -102,7 +118,10 @@ fn an_agent_of_another_root_is_foreign() {
|
||||
let (_, csr) = agent_csr("a1");
|
||||
let a = pki::issue_agent(&other.agents, &csr, "acme", "a1", NOW).unwrap();
|
||||
let e = pki::verify_agent(&ders(&a.chain_pem), &c.root_der(), NOW + DAY).unwrap_err();
|
||||
assert!(matches!(e, VerifyError::UnknownIssuer | VerifyError::BadSignature), "{e:?}");
|
||||
assert!(
|
||||
matches!(e, VerifyError::UnknownIssuer | VerifyError::BadSignature),
|
||||
"{e:?}"
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
@@ -120,12 +139,17 @@ fn an_agent_chain_must_be_exactly_leaf_and_agents_intermediate() {
|
||||
chain.push(ders(c.ianitor.cert_pem()).remove(0));
|
||||
assert_eq!(
|
||||
pki::verify_agent(&chain, &c.root_der(), NOW + DAY),
|
||||
Err(VerifyError::BadCertificate("an agent presents its certificate and the agents intermediate".into()))
|
||||
Err(VerifyError::BadCertificate(
|
||||
"an agent presents its certificate and the agents intermediate".into()
|
||||
))
|
||||
);
|
||||
// The agents intermediate presented, but a different one (another Fabrica server) signed the leaf.
|
||||
let other_agents = intermediate(&c.root, &c.root_pem, pki::AGENTS_INTERMEDIATE_CN);
|
||||
let b = pki::issue_agent(&other_agents, &csr, "acme", "a1", NOW).unwrap();
|
||||
let mixed = vec![ders(&b.cert_pem).remove(0), ders(c.agents.cert_pem()).remove(0)];
|
||||
let mixed = vec![
|
||||
ders(&b.cert_pem).remove(0),
|
||||
ders(c.agents.cert_pem()).remove(0),
|
||||
];
|
||||
assert!(pki::verify_agent(&mixed, &c.root_der(), NOW + DAY).is_err());
|
||||
}
|
||||
|
||||
@@ -180,7 +204,10 @@ fn agent_and_team_ids_are_checked() {
|
||||
("ac/me", "a1"),
|
||||
] {
|
||||
assert!(
|
||||
matches!(pki::issue_agent(&c.agents, &csr, team, agent, NOW), Err(PkiError::BadName(_))),
|
||||
matches!(
|
||||
pki::issue_agent(&c.agents, &csr, team, agent, NOW),
|
||||
Err(PkiError::BadName(_))
|
||||
),
|
||||
"{team:?} {agent:?}"
|
||||
);
|
||||
}
|
||||
@@ -190,7 +217,12 @@ fn agent_and_team_ids_are_checked() {
|
||||
pki::parse_agent_uri("urn:domi:agent:acme:a1"),
|
||||
Some(("acme".to_owned(), "a1".to_owned()))
|
||||
);
|
||||
for bad in ["urn:domi:agent:acme", "urn:domi:agent:acme:a1:x", "urn:domi:device:acme:a1", "urn:domi:agent::a1"] {
|
||||
for bad in [
|
||||
"urn:domi:agent:acme",
|
||||
"urn:domi:agent:acme:a1:x",
|
||||
"urn:domi:device:acme:a1",
|
||||
"urn:domi:agent::a1",
|
||||
] {
|
||||
assert_eq!(pki::parse_agent_uri(bad), None, "{bad}");
|
||||
}
|
||||
}
|
||||
|
||||
+68
-14
@@ -25,8 +25,14 @@ fn root_is_a_ten_year_ca_with_path_length_one() {
|
||||
assert_eq!(info.subject_cn, pki::ROOT_CN);
|
||||
assert_eq!(info.issuer_cn, pki::ROOT_CN);
|
||||
let days = (info.not_after - NOW) / DAY;
|
||||
assert!((ROOT_DAYS as i64 - 1..=ROOT_DAYS as i64).contains(&days), "{days}");
|
||||
assert!(pki::issued_by(&c.root_pem, &c.root_pem).unwrap(), "self-signed");
|
||||
assert!(
|
||||
(ROOT_DAYS as i64 - 1..=ROOT_DAYS as i64).contains(&days),
|
||||
"{days}"
|
||||
);
|
||||
assert!(
|
||||
pki::issued_by(&c.root_pem, &c.root_pem).unwrap(),
|
||||
"self-signed"
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
@@ -41,16 +47,31 @@ fn both_intermediates_are_one_year_path_length_zero_under_the_root() {
|
||||
assert!(!pki::issued_by(&c.root_pem, int.cert_pem()).unwrap());
|
||||
}
|
||||
assert_eq!(
|
||||
pki::intermediate_problem(c.agents.cert_pem(), &c.root_pem, pki::AGENTS_INTERMEDIATE_CN, NOW),
|
||||
pki::intermediate_problem(
|
||||
c.agents.cert_pem(),
|
||||
&c.root_pem,
|
||||
pki::AGENTS_INTERMEDIATE_CN,
|
||||
NOW
|
||||
),
|
||||
None
|
||||
);
|
||||
assert_eq!(
|
||||
pki::intermediate_problem(c.ianitor.cert_pem(), &c.root_pem, pki::IANITOR_INTERMEDIATE_CN, NOW),
|
||||
pki::intermediate_problem(
|
||||
c.ianitor.cert_pem(),
|
||||
&c.root_pem,
|
||||
pki::IANITOR_INTERMEDIATE_CN,
|
||||
NOW
|
||||
),
|
||||
None
|
||||
);
|
||||
// Each is only the intermediate it is named for.
|
||||
assert_eq!(
|
||||
pki::intermediate_problem(c.ianitor.cert_pem(), &c.root_pem, pki::AGENTS_INTERMEDIATE_CN, NOW),
|
||||
pki::intermediate_problem(
|
||||
c.ianitor.cert_pem(),
|
||||
&c.root_pem,
|
||||
pki::AGENTS_INTERMEDIATE_CN,
|
||||
NOW
|
||||
),
|
||||
Some("not_ca")
|
||||
);
|
||||
}
|
||||
@@ -73,7 +94,10 @@ fn server_chain_validates_for_its_name_only() {
|
||||
let info = pki::inspect(&leaf.cert_pem).unwrap();
|
||||
assert_eq!((info.not_after - NOW) / DAY, SERVER_DAYS as i64);
|
||||
let k = pki::KeyPair::from_pem(&leaf.key_pem).unwrap();
|
||||
assert_eq!(pki::public_key_der(&leaf.cert_pem).unwrap(), k.public_key_der());
|
||||
assert_eq!(
|
||||
pki::public_key_der(&leaf.cert_pem).unwrap(),
|
||||
k.public_key_der()
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
@@ -104,7 +128,10 @@ fn a_leaf_cannot_sign() {
|
||||
let forged = pki::testing::sign_as(&leaf.cert_pem, leaf_key, "evil.home.test", NOW).unwrap();
|
||||
let chain = ders(&format!("{forged}{}", leaf.chain_pem));
|
||||
let e = pki::verify_chain(&chain, &c.root_der(), NOW + DAY, Usage::Server).unwrap_err();
|
||||
assert!(matches!(e, VerifyError::NotCa | VerifyError::UnknownIssuer), "{e:?}");
|
||||
assert!(
|
||||
matches!(e, VerifyError::NotCa | VerifyError::UnknownIssuer),
|
||||
"{e:?}"
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
@@ -143,7 +170,10 @@ fn an_expired_intermediate_fails_the_chain_and_issues_nothing() {
|
||||
let late = NOW + (INTERMEDIATE_DAYS as i64 - 10) * DAY;
|
||||
let dev = pki::issue_device(&c.ianitor, "tablet", late).unwrap();
|
||||
let info = pki::inspect(&dev.cert_pem).unwrap();
|
||||
assert!(info.not_after <= c.ianitor.not_after(), "no leaf outlives its issuer");
|
||||
assert!(
|
||||
info.not_after <= c.ianitor.not_after(),
|
||||
"no leaf outlives its issuer"
|
||||
);
|
||||
let gone = NOW + (INTERMEDIATE_DAYS as i64 + 1) * DAY;
|
||||
assert!(matches!(
|
||||
pki::issue_server(&c.agents, "fabrica.home.test", gone),
|
||||
@@ -151,7 +181,12 @@ fn an_expired_intermediate_fails_the_chain_and_issues_nothing() {
|
||||
));
|
||||
// A chain whose intermediate expired is refused even inside the leaf's own validity.
|
||||
assert_eq!(
|
||||
pki::verify_chain(&ders(&dev.chain_pem), &c.root_der(), c.ianitor.not_after() + 1, Usage::Client),
|
||||
pki::verify_chain(
|
||||
&ders(&dev.chain_pem),
|
||||
&c.root_der(),
|
||||
c.ianitor.not_after() + 1,
|
||||
Usage::Client
|
||||
),
|
||||
Err(VerifyError::Expired)
|
||||
);
|
||||
// The root refuses to sign once it has expired.
|
||||
@@ -159,7 +194,13 @@ fn an_expired_intermediate_fails_the_chain_and_issues_nothing() {
|
||||
let csr = pki::intermediate_csr(&key, pki::AGENTS_INTERMEDIATE_CN).unwrap();
|
||||
let past_root = NOW + (ROOT_DAYS as i64 + 1) * DAY;
|
||||
assert!(matches!(
|
||||
pki::sign_intermediate(&c.root_pem, &c.root, &csr, pki::AGENTS_INTERMEDIATE_CN, past_root),
|
||||
pki::sign_intermediate(
|
||||
&c.root_pem,
|
||||
&c.root,
|
||||
&csr,
|
||||
pki::AGENTS_INTERMEDIATE_CN,
|
||||
past_root
|
||||
),
|
||||
Err(PkiError::IssuerExpired)
|
||||
));
|
||||
}
|
||||
@@ -171,13 +212,26 @@ fn another_root_with_the_same_names_is_refused() {
|
||||
let c = ca();
|
||||
let other = ca();
|
||||
let leaf = pki::issue_server(&other.agents, "fabrica.home.test", NOW).unwrap();
|
||||
let e = pki::verify_chain(&ders(&leaf.chain_pem), &c.root_der(), NOW + DAY, Usage::Server)
|
||||
.unwrap_err();
|
||||
assert!(matches!(e, VerifyError::UnknownIssuer | VerifyError::BadSignature), "{e:?}");
|
||||
let e = pki::verify_chain(
|
||||
&ders(&leaf.chain_pem),
|
||||
&c.root_der(),
|
||||
NOW + DAY,
|
||||
Usage::Server,
|
||||
)
|
||||
.unwrap_err();
|
||||
assert!(
|
||||
matches!(e, VerifyError::UnknownIssuer | VerifyError::BadSignature),
|
||||
"{e:?}"
|
||||
);
|
||||
assert!(!pki::issued_by(&leaf.cert_pem, other.ianitor.cert_pem()).unwrap());
|
||||
assert!(!pki::issued_by(other.agents.cert_pem(), &c.root_pem).unwrap());
|
||||
assert_eq!(
|
||||
pki::intermediate_problem(other.agents.cert_pem(), &c.root_pem, pki::AGENTS_INTERMEDIATE_CN, NOW),
|
||||
pki::intermediate_problem(
|
||||
other.agents.cert_pem(),
|
||||
&c.root_pem,
|
||||
pki::AGENTS_INTERMEDIATE_CN,
|
||||
NOW
|
||||
),
|
||||
Some("not_root")
|
||||
);
|
||||
}
|
||||
|
||||
+92
-9
@@ -40,7 +40,13 @@ fn a_signer_that_is_not_the_root_is_refused_before_signing() {
|
||||
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),
|
||||
pki::sign_intermediate(
|
||||
&c.root_pem,
|
||||
&impostor,
|
||||
&csr,
|
||||
pki::AGENTS_INTERMEDIATE_CN,
|
||||
NOW
|
||||
),
|
||||
Err(PkiError::KeyMismatch)
|
||||
));
|
||||
assert_eq!(impostor.signed.get(), 0);
|
||||
@@ -73,7 +79,10 @@ impl Signer for Refuses {
|
||||
#[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(_))));
|
||||
assert!(matches!(
|
||||
pki::create_root(&liar, NOW),
|
||||
Err(PkiError::Signer(_))
|
||||
));
|
||||
let c = ca();
|
||||
let liar = Liar(c.root);
|
||||
let key = pki::generate_key().unwrap();
|
||||
@@ -104,7 +113,10 @@ fn a_signer_with_an_unsupported_key_is_refused() {
|
||||
Ok(vec![0; 64])
|
||||
}
|
||||
}
|
||||
assert!(matches!(pki::create_root(&Garbage, NOW), Err(PkiError::UnsupportedKey)));
|
||||
assert!(matches!(
|
||||
pki::create_root(&Garbage, NOW),
|
||||
Err(PkiError::UnsupportedKey)
|
||||
));
|
||||
}
|
||||
|
||||
#[test]
|
||||
@@ -112,7 +124,14 @@ 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 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());
|
||||
@@ -126,8 +145,26 @@ fn the_intermediate_ignores_what_the_csr_asks_for() {
|
||||
));
|
||||
}
|
||||
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());
|
||||
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]
|
||||
@@ -149,7 +186,14 @@ 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();
|
||||
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"));
|
||||
@@ -168,7 +212,11 @@ fn keys_and_match_codes() {
|
||||
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())));
|
||||
assert!(
|
||||
parts
|
||||
.iter()
|
||||
.all(|p| p.len() == 2 && p.bytes().all(|b| b.is_ascii_digit()))
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
@@ -179,7 +227,11 @@ fn hostile_pem_and_der_never_panic() {
|
||||
"-----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"),
|
||||
concat!(
|
||||
"-----BEGIN PRIVATE",
|
||||
" KEY-----\nAAAA\n-----END PRIVATE",
|
||||
" KEY-----\n"
|
||||
),
|
||||
"\u{0}\u{1}",
|
||||
] {
|
||||
assert!(pki::inspect(bad).is_err());
|
||||
@@ -194,3 +246,34 @@ fn hostile_pem_and_der_never_panic() {
|
||||
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"));
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user