[pki] Tests: chains, expiry, wrong issuer, agent certificates, the root signer (red: the code follows)
This commit is contained in:
+227
@@ -0,0 +1,227 @@
|
||||
// This Source Code Form is subject to the terms of the Mozilla Public
|
||||
// License, v. 2.0. If a copy of the MPL was not distributed with this
|
||||
// file, You can obtain one at https://mozilla.org/MPL/2.0/.
|
||||
|
||||
//! Chains, expiry and wrong issuers (tests first, docs/01): issued chains validate to the root,
|
||||
//! a leaf cannot sign, pathLen 0 holds, expired and not-yet-valid certificates are refused, and a
|
||||
//! certificate of another root is refused even when every name matches.
|
||||
|
||||
#![allow(clippy::unwrap_used, clippy::expect_used, clippy::panic)]
|
||||
|
||||
mod common;
|
||||
|
||||
use common::*;
|
||||
use domi_pki::{
|
||||
self as pki, DEVICE_DAYS, INTERMEDIATE_DAYS, Intermediate, PkiError, ROOT_DAYS, SERVER_DAYS,
|
||||
Usage, VerifyError,
|
||||
};
|
||||
|
||||
#[test]
|
||||
fn root_is_a_ten_year_ca_with_path_length_one() {
|
||||
let c = ca();
|
||||
let info = pki::inspect(&c.root_pem).unwrap();
|
||||
assert!(info.is_ca);
|
||||
assert_eq!(info.path_len, Some(1));
|
||||
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");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn both_intermediates_are_one_year_path_length_zero_under_the_root() {
|
||||
let c = ca();
|
||||
for int in [&c.ianitor, &c.agents] {
|
||||
let info = pki::inspect(int.cert_pem()).unwrap();
|
||||
assert!(info.is_ca);
|
||||
assert_eq!(info.path_len, Some(0));
|
||||
assert_eq!((info.not_after - NOW) / DAY, INTERMEDIATE_DAYS as i64);
|
||||
assert!(pki::issued_by(int.cert_pem(), &c.root_pem).unwrap());
|
||||
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),
|
||||
None
|
||||
);
|
||||
assert_eq!(
|
||||
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),
|
||||
Some("not_ca")
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn server_chain_validates_for_its_name_only() {
|
||||
let c = ca();
|
||||
let leaf = pki::issue_server(&c.agents, "fabrica.home.test", NOW).unwrap();
|
||||
let chain = ders(&leaf.chain_pem);
|
||||
pki::verify_chain(&chain, &c.root_der(), NOW + DAY, Usage::Server).unwrap();
|
||||
pki::verify_server(&chain, &c.root_der(), "fabrica.home.test", NOW + DAY).unwrap();
|
||||
assert!(matches!(
|
||||
pki::verify_server(&chain, &c.root_der(), "other.home.test", NOW + DAY),
|
||||
Err(VerifyError::WrongName)
|
||||
));
|
||||
assert_eq!(
|
||||
pki::verify_chain(&chain, &c.root_der(), NOW + DAY, Usage::Client),
|
||||
Err(VerifyError::WrongUsage)
|
||||
);
|
||||
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());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn device_chain_validates_for_client_auth_only() {
|
||||
let c = ca();
|
||||
let dev = pki::issue_device(&c.ianitor, "Mark's phone", NOW).unwrap();
|
||||
let chain = ders(&dev.chain_pem);
|
||||
pki::verify_chain(&chain, &c.root_der(), NOW + DAY, Usage::Client).unwrap();
|
||||
assert_eq!(
|
||||
pki::verify_chain(&chain, &c.root_der(), NOW + DAY, Usage::Server),
|
||||
Err(VerifyError::WrongUsage)
|
||||
);
|
||||
let info = pki::inspect(&dev.cert_pem).unwrap();
|
||||
assert_eq!(info.subject_cn, "Mark's phone");
|
||||
assert_eq!((info.not_after - NOW) / DAY, DEVICE_DAYS as i64);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_leaf_cannot_sign() {
|
||||
let c = ca();
|
||||
let leaf = pki::issue_server(&c.ianitor, "wiki.home.test", NOW).unwrap();
|
||||
let leaf_key = pki::KeyPair::from_pem(&leaf.key_pem).unwrap();
|
||||
assert!(matches!(
|
||||
Intermediate::new(&leaf.cert_pem, leaf_key),
|
||||
Err(PkiError::NotCa)
|
||||
));
|
||||
let leaf_key = pki::KeyPair::from_pem(&leaf.key_pem).unwrap();
|
||||
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:?}");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn an_intermediate_cannot_make_another_ca() {
|
||||
let c = ca();
|
||||
let sub_key = pki::generate_key().unwrap();
|
||||
let sub = pki::testing::sub_ca(&c.ianitor, &sub_key, NOW).unwrap();
|
||||
let leaf = pki::testing::sign_as(&sub, sub_key, "deep.home.test", NOW).unwrap();
|
||||
let chain = ders(&format!("{leaf}{sub}{}", c.ianitor.cert_pem()));
|
||||
assert!(pki::verify_chain(&chain, &c.root_der(), NOW + DAY, Usage::Server).is_err());
|
||||
}
|
||||
|
||||
// ------------------------------------------------------------------ expiry
|
||||
|
||||
#[test]
|
||||
fn expired_and_not_yet_valid_leaves_are_refused() {
|
||||
let c = ca();
|
||||
let leaf = pki::issue_server(&c.agents, "fabrica.home.test", NOW).unwrap();
|
||||
let chain = ders(&leaf.chain_pem);
|
||||
let after = NOW + (SERVER_DAYS as i64 + 1) * DAY;
|
||||
assert_eq!(
|
||||
pki::verify_chain(&chain, &c.root_der(), after, Usage::Server),
|
||||
Err(VerifyError::Expired)
|
||||
);
|
||||
assert_eq!(
|
||||
pki::verify_chain(&chain, &c.root_der(), NOW - DAY, Usage::Server),
|
||||
Err(VerifyError::NotYetValid)
|
||||
);
|
||||
// The backdating covers a client clock five minutes late, not more.
|
||||
pki::verify_chain(&chain, &c.root_der(), NOW - 200, Usage::Server).unwrap();
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn an_expired_intermediate_fails_the_chain_and_issues_nothing() {
|
||||
let c = ca();
|
||||
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");
|
||||
let gone = NOW + (INTERMEDIATE_DAYS as i64 + 1) * DAY;
|
||||
assert!(matches!(
|
||||
pki::issue_server(&c.agents, "fabrica.home.test", gone),
|
||||
Err(PkiError::IssuerExpired)
|
||||
));
|
||||
// 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),
|
||||
Err(VerifyError::Expired)
|
||||
);
|
||||
// The root refuses to sign once it has expired.
|
||||
let key = pki::generate_key().unwrap();
|
||||
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),
|
||||
Err(PkiError::IssuerExpired)
|
||||
));
|
||||
}
|
||||
|
||||
// ------------------------------------------------------------------ wrong issuer
|
||||
|
||||
#[test]
|
||||
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:?}");
|
||||
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),
|
||||
Some("not_root")
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_chain_without_its_intermediate_is_refused() {
|
||||
let c = ca();
|
||||
let leaf = pki::issue_server(&c.agents, "fabrica.home.test", NOW).unwrap();
|
||||
let only_leaf = vec![ders(&leaf.cert_pem).remove(0)];
|
||||
assert_eq!(
|
||||
pki::verify_chain(&only_leaf, &c.root_der(), NOW + DAY, Usage::Server),
|
||||
Err(VerifyError::UnknownIssuer)
|
||||
);
|
||||
// Nor does the wrong intermediate help.
|
||||
let wrong = vec![only_leaf[0].clone(), ders(c.ianitor.cert_pem()).remove(0)];
|
||||
assert!(pki::verify_chain(&wrong, &c.root_der(), NOW + DAY, Usage::Server).is_err());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_self_signed_leaf_is_refused() {
|
||||
let c = ca();
|
||||
let placeholder = pki::placeholder_leaf("nobody", NOW).unwrap();
|
||||
assert_eq!(
|
||||
pki::verify_chain(&ders(&placeholder), &c.root_der(), NOW + DAY, Usage::Client),
|
||||
Err(VerifyError::UnknownIssuer)
|
||||
);
|
||||
let info = pki::inspect(&placeholder).unwrap();
|
||||
assert_eq!(info.subject_cn, "nobody");
|
||||
assert!(!info.is_ca);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn an_empty_chain_or_garbage_root_is_an_error() {
|
||||
let c = ca();
|
||||
assert_eq!(
|
||||
pki::verify_chain(&[], &c.root_der(), NOW, Usage::Server),
|
||||
Err(VerifyError::BadCertificate("the chain is empty".into()))
|
||||
);
|
||||
let leaf = pki::issue_server(&c.agents, "fabrica.home.test", NOW).unwrap();
|
||||
assert!(matches!(
|
||||
pki::verify_chain(&ders(&leaf.chain_pem), b"not a root", NOW, Usage::Server),
|
||||
Err(VerifyError::BadCertificate(_))
|
||||
));
|
||||
assert!(pki::verify_chain(&[b"junk".to_vec()], &c.root_der(), NOW, Usage::Server).is_err());
|
||||
// Time before 1970 is refused, not wrapped.
|
||||
assert!(pki::verify_chain(&ders(&leaf.chain_pem), &c.root_der(), -5, Usage::Server).is_err());
|
||||
}
|
||||
Reference in New Issue
Block a user