Cotton code
101 lines · 4.0 KB · RustRaw
//! The recovery key, read as a wrap recipient.//!//! **Every key in this file is generated inside the test**, by the `age` crate//! itself, and none of them outlives the run. There is no committed identity//! and there must never be one — a real recovery key in a public tree is the//! whole vault ([nothing-real-ever-goes-in-cotton-s-public-tree]).//!//! Generating with `age` rather than by hand is the point of the round trip://! what these functions parse has to be what `age-keygen` writes, and the only//! way to assert that is to let age write one.use super::*;use crate::vault::wrap::{unwrap, wrap};/// A fresh age identity and its recipient, in the strings a person sees.fn an_identity() -> (String, String) {    let identity = age::x25519::Identity::generate();    let recipient = identity.to_public().to_string();    (identity.to_string().expose_secret().to_string(), recipient)}use secrecy::ExposeSecret as _;#[test]fn a_freshly_generated_age_key_parses_at_both_ends_and_the_halves_agree() {    let (identity, recipient) = an_identity();    assert!(identity.starts_with("AGE-SECRET-KEY-1"), "got {identity}");    assert!(recipient.starts_with("age1"), "got {recipient}");    let public = recipient_from_age_public(&recipient).expect("the recipient parses");    let secret = secret_from_age_identity(&identity).expect("the identity parses");    assert_eq!(        recipient_of_identity(&identity),        Some(public),        "the public half this crate derives must be the one age published, or a wrap made for the \         recovery row is a wrap the recovery key cannot open"    );    assert_eq!(crate::vault::wrap::public_of(&secret), public);}#[test]fn the_vault_master_key_wrapped_to_a_recovery_recipient_opens_with_its_identity() {    let (identity, recipient) = an_identity();    let public = recipient_from_age_public(&recipient).expect("the recipient parses");    let wrapped = wrap(&public, b"thirty-two bytes of vault master").expect("wraps");    let secret = secret_from_age_identity(&identity).expect("the identity parses");    assert_eq!(        unwrap(&secret, &wrapped).as_deref(),        Some(&b"thirty-two bytes of vault master"[..]),        "this is the whole of the recovery story: one key, kept off the Mac, opens the vault"    );}#[test]fn another_persons_identity_opens_nothing() {    let (_, recipient) = an_identity();    let (other_identity, _) = an_identity();    let public = recipient_from_age_public(&recipient).expect("parses");    let wrapped = wrap(&public, b"not for that key").expect("wraps");    let other = secret_from_age_identity(&other_identity).expect("parses");    assert_eq!(unwrap(&other, &wrapped), None);}#[test]fn a_case_folded_identity_still_parses_because_bech32_is_case_insensitive() {    let (identity, _) = an_identity();    assert_eq!(        secret_from_age_identity(&identity.to_lowercase())            .expect("parses")            .as_slice(),        secret_from_age_identity(&identity).expect("parses").as_slice(),    );}#[test]fn the_two_halves_are_not_interchangeable() {    // An identity is not a recipient and a recipient is not an identity: each    // is refused in the other's place, by the human-readable part, before a    // single byte is read.    let (identity, recipient) = an_identity();    assert_eq!(recipient_from_age_public(&identity), None);    assert_eq!(secret_from_age_identity(&recipient), None);}#[test]fn anything_that_is_not_an_age_key_is_refused_rather_than_half_read() {    for value in [        "",        "age1",        "AGE-SECRET-KEY-1",        "not a key at all",        // A valid bech32 string under the wrong human-readable part.        "bc1qw508d6qejxtdg4y5r3zarvary0c5xw7kv8f3t4",        // The right prefix, a broken checksum.        "age1qqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqq",    ] {        assert_eq!(recipient_from_age_public(value), None, "read {value:?}");        assert!(secret_from_age_identity(value).is_none(), "read {value:?}");    }}