Files
CustosPublic/crates/custos-keys/tests/mandatory.rs
T

112 lines
5.7 KiB
Rust

//! Mandatory tests for key pairs (docs/KEYS.md): every format parses back to the same key, the
//! fingerprint is ssh-keygen's, Ed25519 signs and verifies, nothing private prints.
use custos_keys::{Algorithm, generate};
#[test]
fn every_algorithm_round_trips_through_every_format() {
for alg in [Algorithm::Ed25519, Algorithm::EcdsaP256, Algorithm::Rsa3072] {
let k = generate(alg, "deploy@home").unwrap();
assert_eq!(k.algorithm, alg.name());
// The OpenSSH private key parses back and carries the same public key and comment.
let private = ssh_key::PrivateKey::from_openssh(k.private_openssh.expose()).unwrap();
assert_eq!(private.public_key().to_openssh().unwrap(), k.public_openssh);
assert_eq!(private.comment(), "deploy@home");
assert!(k.public_openssh.ends_with(" deploy@home"));
// The fingerprint is the one ssh-keygen -l prints.
assert_eq!(private.public_key().fingerprint(ssh_key::HashAlg::Sha256).to_string(), k.fingerprint);
assert!(k.fingerprint.starts_with("SHA256:"));
// PKCS#8 and SPKI PEM have the right headers and parse back to the same public key.
assert!(k.private_pem.expose().starts_with("-----BEGIN PRIVATE KEY-----"));
assert!(k.public_pem.starts_with("-----BEGIN PUBLIC KEY-----"));
assert!(custos_keys::pem_matches(alg, k.private_pem.expose(), &k.public_pem).unwrap(), "{alg:?}");
}
}
#[test]
fn ed25519_signs_and_verifies() {
let k = generate(Algorithm::Ed25519, "").unwrap();
let sig = custos_keys::sign_ed25519(k.private_pem.expose(), b"release 1.0").unwrap();
assert!(custos_keys::verify_ed25519(&k.public_pem, b"release 1.0", &sig).unwrap());
assert!(!custos_keys::verify_ed25519(&k.public_pem, b"release 1.1", &sig).unwrap());
}
#[test]
fn keys_are_unique_and_nothing_private_prints() {
let a = generate(Algorithm::Ed25519, "a").unwrap();
let b = generate(Algorithm::Ed25519, "a").unwrap();
assert_ne!(a.public_openssh, b.public_openssh);
let printed = format!("{a:?}");
assert!(!printed.contains("PRIVATE"), "{printed}");
assert!(!printed.contains(a.private_openssh.expose().lines().nth(1).unwrap()), "{printed}");
}
#[test]
fn algorithm_names_parse() {
for alg in Algorithm::ALL {
assert_eq!(Algorithm::from_name(alg.name()), Some(alg));
}
assert_eq!(Algorithm::from_name("dsa"), None);
}
// ---- importing (docs/KEYS.md, "Importing") ----
/// A throwaway key made by `ssh-keygen -t ed25519 -C fixture@test` for these tests only.
const SSH_KEYGEN_ED25519: &str = include_str!("fixtures/ssh-keygen-ed25519");
/// The same kind of key with the passphrase `fixture-pass`.
const SSH_KEYGEN_ENCRYPTED: &str = include_str!("fixtures/ssh-keygen-ed25519-passphrase");
#[test]
fn an_ssh_keygen_key_imports_with_its_own_fingerprint_and_comment() {
let k = custos_keys::import(SSH_KEYGEN_ED25519, "fallback").unwrap();
assert_eq!(k.algorithm, "ed25519");
// What `ssh-keygen -l -f` printed for the fixture.
assert_eq!(k.fingerprint, "SHA256:IGwHpAF8aVpXvaBckNayqTMlHUvRgN9+w1uS7iyLPGE");
assert!(k.public_openssh.ends_with(" fixture@test"));
assert!(custos_keys::pem_matches(Algorithm::Ed25519, k.private_pem.expose(), &k.public_pem).unwrap());
}
#[test]
fn every_private_key_format_imports_to_the_same_key() {
use p256::pkcs8::DecodePrivateKey as _;
use rsa::pkcs1::EncodeRsaPrivateKey;
for alg in [Algorithm::Ed25519, Algorithm::EcdsaP256, Algorithm::Rsa3072] {
let k = generate(alg, "deploy").unwrap();
let mut inputs = vec![k.private_openssh.expose().to_owned(), k.private_pem.expose().to_owned()];
match alg {
Algorithm::EcdsaP256 => {
let sk = p256::SecretKey::from_pkcs8_pem(k.private_pem.expose()).unwrap();
inputs.push(sk.to_sec1_pem(p256::pkcs8::LineEnding::LF).unwrap().to_string());
}
Algorithm::Rsa3072 => {
let sk = rsa::RsaPrivateKey::from_pkcs8_pem(k.private_pem.expose()).unwrap();
inputs.push(sk.to_pkcs1_pem(rsa::pkcs8::LineEnding::LF).unwrap().to_string());
}
_ => {}
}
for text in inputs {
// Surrounding whitespace (a paste, a file with CRLF line ends) does not matter.
let pasted = format!("\r\n {}\r\n", text.replace('\n', "\r\n"));
let i = custos_keys::import(&pasted, "deploy").unwrap_or_else(|e| panic!("{alg:?} {}: {e:?}", text.lines().next().unwrap()));
assert_eq!((i.algorithm.as_str(), i.fingerprint.as_str()), (alg.name(), k.fingerprint.as_str()), "{alg:?}");
assert_eq!(i.public_pem, k.public_pem);
}
}
}
#[test]
fn protected_and_unknown_keys_are_refused() {
use custos_keys::KeyError;
assert!(matches!(custos_keys::import(SSH_KEYGEN_ENCRYPTED, "x"), Err(KeyError::Encrypted)));
let pkcs8_encrypted = "-----BEGIN ENCRYPTED PRIVATE KEY-----\nMIIB\n-----END ENCRYPTED PRIVATE KEY-----";
assert!(matches!(custos_keys::import(pkcs8_encrypted, "x"), Err(KeyError::Encrypted)));
let pkcs1_encrypted = "-----BEGIN RSA PRIVATE KEY-----\nProc-Type: 4,ENCRYPTED\nDEK-Info: AES-128-CBC,00\n\nAAAA\n-----END RSA PRIVATE KEY-----";
assert!(matches!(custos_keys::import(pkcs1_encrypted, "x"), Err(KeyError::Encrypted)));
// A public key, a password, garbage: nothing to import, and the error never repeats the input.
let public = generate(Algorithm::Ed25519, "p").unwrap().public_openssh;
for text in [public.as_str(), "hunter2-not-a-key", "-----BEGIN PRIVATE KEY-----\nAAAA\n-----END PRIVATE KEY-----"] {
let e = custos_keys::import(text, "x").unwrap_err();
assert!(!e.to_string().contains("hunter2") && !e.to_string().contains("AAAA"), "{e}");
}
}