[pki] CA: the Domi CA from Ianitor core behind a Signer, agent certificates, webpki chain checks (34 tests)
This commit is contained in:
+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