51 lines
2.4 KiB
Rust
51 lines
2.4 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);
|
|
}
|