396 lines
17 KiB
Rust
396 lines
17 KiB
Rust
//! Mandatory tests for the Domi identity (docs/IDENTITY.md): the mnemonic order, the vector shared with
|
||
//! Fundamentum, signing only certs, persistence, and no way to get the root key out.
|
||
|
||
use custos_vault::identity::{Enrollment, Issued};
|
||
use custos_vault::{Error, FactorKind, KdfParams, SecretString, Unlock, Vault};
|
||
use ed25519_dalek::{Signature, VerifyingKey};
|
||
|
||
const FAST: KdfParams = KdfParams { m_cost_kib: 64, t_cost: 1, p_cost: 1 };
|
||
/// The root public key of `abandon` × 23 + `art` (Fundamentum `crates/tessera/tests/mandatory.rs`).
|
||
const ROOT_PUB_ZERO: &str = "vCrwX9p44un1ftQLgJtuaVQ0qPWbFfzNjgrlDkL4TKs";
|
||
const CERT_DOMAIN: &[u8] = b"tessera cert v1\0";
|
||
|
||
fn vault() -> Vault {
|
||
Vault::create(&SecretString::from("pw"), FAST).unwrap()
|
||
}
|
||
|
||
fn b64url(s: &str) -> Vec<u8> {
|
||
data_encoding::BASE64URL_NOPAD.decode(s.as_bytes()).unwrap()
|
||
}
|
||
|
||
/// Runs the whole enrolment with known entropy, typing the right words. Returns the words shown.
|
||
fn enrol(v: &mut Vault, entropy: [u8; 32]) -> Vec<String> {
|
||
let mut words = Vec::new();
|
||
let shown = Enrollment::with_entropy(entropy).show(|w| words = w.iter().map(|s| s.to_string()).collect());
|
||
let pick = shown.challenge().map(|i| words[i - 1].clone());
|
||
let confirmed = shown.confirm(&[&pick[0], &pick[1], &pick[2]]).map_err(|(_, e)| e).unwrap();
|
||
confirmed.import(v).unwrap();
|
||
words
|
||
}
|
||
|
||
#[test]
|
||
fn the_zero_vector_matches_fundamentum() {
|
||
let mut v = vault();
|
||
let words = enrol(&mut v, [0; 32]);
|
||
assert_eq!(words.len(), 24);
|
||
assert!(words[..23].iter().all(|w| w == "abandon") && words[23] == "art");
|
||
assert_eq!(v.domi_root_public().unwrap().to_vec(), b64url(ROOT_PUB_ZERO));
|
||
}
|
||
|
||
#[test]
|
||
fn the_same_mnemonic_gives_the_same_root() {
|
||
let (mut a, mut b) = (vault(), vault());
|
||
enrol(&mut a, [42; 32]);
|
||
enrol(&mut b, [42; 32]);
|
||
assert_eq!(a.domi_root_public(), b.domi_root_public());
|
||
let mut c = vault();
|
||
enrol(&mut c, [43; 32]);
|
||
assert_ne!(a.domi_root_public(), c.domi_root_public());
|
||
}
|
||
|
||
#[test]
|
||
fn the_mnemonic_is_shown_before_anything_is_imported() {
|
||
// The order trap at run time; at compile time see the compile_fail doctests in src/identity.rs.
|
||
let v = vault();
|
||
let mut log = Vec::new();
|
||
let shown = Enrollment::begin().show(|w| {
|
||
assert_eq!(w.len(), 24);
|
||
log.push("shown");
|
||
});
|
||
assert_eq!(log, ["shown"]);
|
||
assert!(v.domi_root_public().is_none(), "nothing in the vault before confirm + import");
|
||
// Wrong words: refused, and the same Shown comes back to try again.
|
||
let (shown, e) = shown.confirm(&["zoo", "zoo", "zoo"]).expect_err("wrong words are refused");
|
||
assert!(matches!(e, Error::WrongWords));
|
||
let c = shown.challenge();
|
||
assert!(c.iter().all(|&i| (1..=24).contains(&i)) && c[0] != c[1] && c[1] != c[2] && c[0] != c[2]);
|
||
assert!(v.domi_root_public().is_none());
|
||
}
|
||
|
||
#[test]
|
||
fn the_challenge_accepts_any_spacing_and_case() {
|
||
let mut v = vault();
|
||
let mut words = Vec::new();
|
||
let shown = Enrollment::with_entropy([5; 32]).show(|w| words = w.iter().map(|s| s.to_string()).collect());
|
||
let pick = shown.challenge().map(|i| format!(" {} ", words[i - 1].to_uppercase()));
|
||
shown.confirm(&[&pick[0], &pick[1], &pick[2]]).map_err(|(_, e)| e).unwrap().import(&mut v).unwrap();
|
||
assert!(v.domi_root_public().is_some());
|
||
}
|
||
|
||
#[test]
|
||
fn an_identity_is_created_once() {
|
||
let mut v = vault();
|
||
enrol(&mut v, [1; 32]);
|
||
let first = v.domi_root_public();
|
||
let mut words = Vec::new();
|
||
let shown = Enrollment::with_entropy([2; 32]).show(|w| words = w.iter().map(|s| s.to_string()).collect());
|
||
let pick = shown.challenge().map(|i| words[i - 1].clone());
|
||
let c = shown.confirm(&[&pick[0], &pick[1], &pick[2]]).map_err(|(_, e)| e).unwrap();
|
||
assert!(matches!(c.import(&mut v), Err(Error::IdentityExists)));
|
||
assert_eq!(v.domi_root_public(), first);
|
||
}
|
||
|
||
/// A cert's to-be-signed bytes as tessera makes them: TSC1 ‖ root ‖ subkey ‖ ids ‖ validity.
|
||
fn tbs(root: &[u8; 32]) -> Vec<u8> {
|
||
let mut t = b"TSC1".to_vec();
|
||
t.extend_from_slice(root);
|
||
t.extend_from_slice(&[9; 32]);
|
||
t.extend_from_slice(b"\x06fiscus\x04sync");
|
||
t.extend_from_slice(&1u64.to_be_bytes());
|
||
t.extend_from_slice(&2u64.to_be_bytes());
|
||
t
|
||
}
|
||
|
||
#[test]
|
||
fn the_root_signs_certs_and_nothing_else() {
|
||
let mut v = vault();
|
||
assert!(matches!(v.domi_sign_cert(&tbs(&[0; 32])), Err(Error::NoIdentity)));
|
||
enrol(&mut v, [0; 32]);
|
||
let root = v.domi_root_public().unwrap();
|
||
let t = tbs(&root);
|
||
let sig = v.domi_sign_cert(&t).unwrap();
|
||
let vk = VerifyingKey::from_bytes(&root).unwrap();
|
||
vk.verify_strict(&[CERT_DOMAIN, &t].concat(), &Signature::from_bytes(&sig)).unwrap();
|
||
// Not a cert, a cert for another root, the bare prefix: refused.
|
||
assert!(matches!(v.domi_sign_cert(b"pay 100 CZK to mallory"), Err(Error::NotACert)));
|
||
assert!(matches!(v.domi_sign_cert(&tbs(&[1; 32])), Err(Error::NotACert)));
|
||
assert!(matches!(v.domi_sign_cert(&t[..36]), Err(Error::NotACert)));
|
||
}
|
||
|
||
#[test]
|
||
fn the_identity_survives_saving_in_both_versions() {
|
||
let issued = Issued {
|
||
kind: "app".into(),
|
||
app_id: "fiscus".into(),
|
||
purpose: "sync".into(),
|
||
subkey: "CQkJCQkJCQkJCQkJCQkJCQkJCQkJCQkJCQkJCQkJCQk".into(),
|
||
label: "fiscus.exe".into(),
|
||
not_before: 1,
|
||
not_after: 2,
|
||
issued: 3,
|
||
revoked_at: Some(4),
|
||
revocation: vec!["VFJWMQ".into()],
|
||
};
|
||
// Version 1.
|
||
let mut v = vault();
|
||
enrol(&mut v, [0; 32]);
|
||
v.domi_record(issued.clone()).unwrap();
|
||
let back = Vault::open(&v.to_bytes().unwrap(), &SecretString::from("pw")).unwrap();
|
||
assert_eq!(back.domi_root_public(), v.domi_root_public());
|
||
assert_eq!(back.domi_issued(), std::slice::from_ref(&issued));
|
||
assert!(back.domi_created().is_some());
|
||
let t = tbs(&back.domi_root_public().unwrap());
|
||
assert_eq!(back.domi_sign_cert(&t).unwrap(), v.domi_sign_cert(&t).unwrap(), "the same key came back");
|
||
// Version 2.
|
||
let pw = SecretString::from("pw");
|
||
let mut v2 =
|
||
Vault::create_with(Unlock::Password(&pw), "pw", Unlock::Material(FactorKind::Device, &[7; 32]), "pc", FAST)
|
||
.unwrap();
|
||
enrol(&mut v2, [3; 32]);
|
||
let bytes = v2.to_bytes().unwrap();
|
||
let back = Vault::open_with(&bytes, Unlock::Password(&pw), Unlock::Material(FactorKind::Device, &[7; 32])).unwrap();
|
||
assert_eq!(back.domi_root_public(), v2.domi_root_public());
|
||
// A vault without an identity still opens and has none.
|
||
let plain = Vault::open(&vault().to_bytes().unwrap(), &SecretString::from("pw")).unwrap();
|
||
assert!(plain.domi_root_public().is_none() && plain.domi_issued().is_empty());
|
||
}
|
||
|
||
#[test]
|
||
fn a_record_needs_an_identity() {
|
||
let mut v = vault();
|
||
let i = Issued {
|
||
kind: "app".into(),
|
||
app_id: "x".into(),
|
||
purpose: "y".into(),
|
||
subkey: String::new(),
|
||
label: String::new(),
|
||
not_before: 0,
|
||
not_after: 1,
|
||
issued: 0,
|
||
..Issued::default()
|
||
};
|
||
assert!(matches!(v.domi_record(i), Err(Error::NoIdentity)));
|
||
assert!(matches!(v.domi_mark_revoked("x", 1), Err(Error::NoIdentity)));
|
||
assert!(matches!(v.domi_backup_secret("fiscus"), Err(Error::NoIdentity)));
|
||
}
|
||
|
||
// ---- v1.1: restoring, revoking, the backup secret (docs/IDENTITY.md) ----
|
||
|
||
const ZERO_PHRASE: &str = "abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon \
|
||
abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon \
|
||
abandon art";
|
||
const REVOKE_DOMAIN: &[u8] = b"tessera revoke v1\0";
|
||
const SUBKEY_9: &str = "CQkJCQkJCQkJCQkJCQkJCQkJCQkJCQkJCQkJCQkJCQk"; // [9; 32]
|
||
|
||
#[test]
|
||
fn the_24_words_restore_the_same_root() {
|
||
let mut v = vault();
|
||
Enrollment::restore(ZERO_PHRASE).unwrap().import(&mut v).unwrap();
|
||
assert_eq!(v.domi_root_public().unwrap().to_vec(), b64url(ROOT_PUB_ZERO));
|
||
// Any case, any spacing, as long as the words are right.
|
||
let mut w = vault();
|
||
let messy = format!(" {}\n", ZERO_PHRASE.to_uppercase().replace(' ', " \t"));
|
||
Enrollment::restore(&messy).unwrap().import(&mut w).unwrap();
|
||
assert_eq!(w.domi_root_public(), v.domi_root_public());
|
||
// And from words that were shown: the same root as enrolling.
|
||
let mut e = vault();
|
||
let words = enrol(&mut e, [42; 32]);
|
||
let mut r = vault();
|
||
Enrollment::restore(&words.join(" ")).unwrap().import(&mut r).unwrap();
|
||
assert_eq!(r.domi_root_public(), e.domi_root_public());
|
||
}
|
||
|
||
#[test]
|
||
fn a_wrong_phrase_restores_nothing_and_names_no_word() {
|
||
let bad_checksum = ZERO_PHRASE.replace("art", "abandon");
|
||
let short = "abandon ".repeat(11) + "about";
|
||
let typo = ZERO_PHRASE.replacen("abandon", "abandn", 1);
|
||
for phrase in [bad_checksum.as_str(), short.as_str(), typo.as_str(), "", "art"] {
|
||
let e = Enrollment::restore(phrase).map(|_| ()).unwrap_err();
|
||
assert!(matches!(e, Error::NotAMnemonic), "{phrase}");
|
||
let shown = format!("{e} {e:?}");
|
||
assert!(!shown.contains("abandon") && !shown.contains("art "), "{shown}");
|
||
}
|
||
// Into a vault that has one already: refused, the old one stays.
|
||
let mut v = vault();
|
||
enrol(&mut v, [5; 32]);
|
||
let before = v.domi_root_public();
|
||
assert!(matches!(Enrollment::restore(ZERO_PHRASE).unwrap().import(&mut v), Err(Error::IdentityExists)));
|
||
assert_eq!(v.domi_root_public(), before);
|
||
}
|
||
|
||
/// A revocation's to-be-signed bytes as tessera makes them: TRV1 ‖ root ‖ subkey ‖ revoked_at ‖ part ‖
|
||
/// parts ‖ count ‖ hashes.
|
||
fn trv1(root: &[u8; 32], subkey: &[u8; 32], revoked_at: u64) -> Vec<u8> {
|
||
let mut t = b"TRV1".to_vec();
|
||
t.extend_from_slice(root);
|
||
t.extend_from_slice(subkey);
|
||
t.extend_from_slice(&revoked_at.to_be_bytes());
|
||
t.extend_from_slice(&0u16.to_be_bytes());
|
||
t.extend_from_slice(&1u16.to_be_bytes());
|
||
t.extend_from_slice(&1u32.to_be_bytes());
|
||
t.extend_from_slice(&[7; 32]);
|
||
t
|
||
}
|
||
|
||
fn issued_to(subkey: &str, app_id: &str) -> Issued {
|
||
Issued {
|
||
kind: "app".into(),
|
||
app_id: app_id.into(),
|
||
purpose: "sync".into(),
|
||
subkey: subkey.into(),
|
||
label: "fiscus.exe".into(),
|
||
not_before: 1,
|
||
not_after: 2,
|
||
issued: 3,
|
||
..Issued::default()
|
||
}
|
||
}
|
||
|
||
#[test]
|
||
fn the_root_signs_a_revocation_only_for_a_key_the_person_revoked() {
|
||
let mut v = vault();
|
||
enrol(&mut v, [0; 32]);
|
||
let root = v.domi_root_public().unwrap();
|
||
v.domi_record(issued_to(SUBKEY_9, "fiscus")).unwrap();
|
||
// Issued, but not revoked yet: refused.
|
||
let t = trv1(&root, &[9; 32], 1000);
|
||
assert!(matches!(v.domi_sign_revocation(&t), Err(Error::NotARevocation)));
|
||
// An unknown key cannot be marked.
|
||
assert!(matches!(v.domi_mark_revoked("CgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgo", 1000), Err(Error::UnknownKey)));
|
||
|
||
// A renewal issued a second cert for the same key: marking covers both, and the first time stays.
|
||
v.domi_record(issued_to(SUBKEY_9, "fiscus")).unwrap();
|
||
assert_eq!(v.domi_mark_revoked(SUBKEY_9, 1000).unwrap(), 1000);
|
||
assert_eq!(v.domi_mark_revoked(SUBKEY_9, 2000).unwrap(), 1000, "the first revocation time stays");
|
||
assert!(v.domi_issued().iter().all(|i| i.revoked_at == Some(1000)));
|
||
|
||
let sig = v.domi_sign_revocation(&t).unwrap();
|
||
let vk = VerifyingKey::from_bytes(&root).unwrap();
|
||
vk.verify_strict(&[REVOKE_DOMAIN, &t].concat(), &Signature::from_bytes(&sig)).unwrap();
|
||
assert!(
|
||
vk.verify_strict(&[CERT_DOMAIN, &t].concat(), &Signature::from_bytes(&sig)).is_err(),
|
||
"not a cert signature"
|
||
);
|
||
|
||
// Another time, another key, another root, a cert, garbage, the bare prefix: refused.
|
||
for bad in [
|
||
trv1(&root, &[9; 32], 999),
|
||
trv1(&root, &[10; 32], 1000),
|
||
trv1(&[1; 32], &[9; 32], 1000),
|
||
tbs(&root),
|
||
b"pay 100 CZK to mallory".to_vec(),
|
||
t[..76].to_vec(),
|
||
] {
|
||
assert!(matches!(v.domi_sign_revocation(&bad), Err(Error::NotARevocation)), "{bad:?}");
|
||
}
|
||
// And the cert signer never signs a revocation.
|
||
assert!(matches!(v.domi_sign_cert(&t), Err(Error::NotACert)));
|
||
|
||
// The statements are kept with every cert of the key, and survive a save.
|
||
v.domi_record_revocation(SUBKEY_9, vec!["first".into(), "second".into()]).unwrap();
|
||
assert!(matches!(
|
||
v.domi_record_revocation("CgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgo", vec![]),
|
||
Err(Error::UnknownKey)
|
||
));
|
||
let back = Vault::open(&v.to_bytes().unwrap(), &SecretString::from("pw")).unwrap();
|
||
assert!(back.domi_issued().iter().all(|i| i.revoked_at == Some(1000) && i.revocation == ["first", "second"]));
|
||
assert_eq!(back.domi_sign_revocation(&t).unwrap(), sig);
|
||
}
|
||
|
||
#[test]
|
||
fn a_cert_without_a_revocation_is_saved_as_before() {
|
||
let mut v = vault();
|
||
enrol(&mut v, [0; 32]);
|
||
v.domi_record(issued_to(SUBKEY_9, "fiscus")).unwrap();
|
||
let json = serde_json::to_string(&v.domi_issued()[0]).unwrap();
|
||
assert!(!json.contains("revoked_at") && !json.contains("revocation"), "{json}");
|
||
// And a record from core 0.7.0, without the two members, reads.
|
||
let old: Issued = serde_json::from_str(
|
||
r#"{"kind":"app","app_id":"fiscus","purpose":"sync","subkey":"x","label":"l","not_before":1,"not_after":2,"issued":3}"#,
|
||
)
|
||
.unwrap();
|
||
assert_eq!((old.revoked_at, old.revocation.len()), (None, 0));
|
||
}
|
||
|
||
#[test]
|
||
fn the_backup_secret_matches_fundamentum() {
|
||
use chacha20poly1305::aead::{Aead, KeyInit, Payload};
|
||
let mut v = vault();
|
||
enrol(&mut v, [0; 32]);
|
||
let secret = v.domi_backup_secret("fiscus").unwrap();
|
||
let expand = |info: &[u8]| {
|
||
let mut out = [0u8; 32];
|
||
hkdf::Hkdf::<sha2::Sha256>::from_prk(secret.as_ref()).unwrap().expand(info, &mut out).unwrap();
|
||
out
|
||
};
|
||
// Fundamentum docs/TESSERA.md §12.
|
||
assert_eq!(expand(b"mailbox").to_vec(), b64url("IygCOrh-ibO6-KY5AXQ9Prl2SyPWQIyZZ-anffcbMCU"));
|
||
let key = expand(b"seal");
|
||
let tag = chacha20poly1305::XChaCha20Poly1305::new_from_slice(&key)
|
||
.unwrap()
|
||
.encrypt(&chacha20poly1305::XNonce::try_from(&[0u8; 24][..]).unwrap(), Payload { msg: b"", aad: b"" })
|
||
.unwrap();
|
||
assert_eq!(tag, b64url("ci_JRbtsx5prR83KT9Y7LA"));
|
||
// One per app, the same from the restored words.
|
||
assert_ne!(*v.domi_backup_secret("calenda").unwrap(), *secret);
|
||
let mut r = vault();
|
||
Enrollment::restore(ZERO_PHRASE).unwrap().import(&mut r).unwrap();
|
||
assert_eq!(*r.domi_backup_secret("fiscus").unwrap(), *secret);
|
||
}
|
||
|
||
#[test]
|
||
fn nothing_prints_the_root() {
|
||
let mut v = vault();
|
||
let mut words = Vec::new();
|
||
let e = Enrollment::with_entropy([0; 32]);
|
||
assert_eq!(format!("{e:?}"), "Enrollment([redacted])");
|
||
let shown = e.show(|w| words = w.iter().map(|s| s.to_string()).collect());
|
||
assert_eq!(format!("{shown:?}"), "Shown([redacted])");
|
||
let pick = shown.challenge().map(|i| words[i - 1].clone());
|
||
let c = shown.confirm(&[&pick[0], &pick[1], &pick[2]]).map_err(|(_, e)| e).unwrap();
|
||
assert_eq!(format!("{c:?}"), "Confirmed([redacted])");
|
||
c.import(&mut v).unwrap();
|
||
let dbg = format!("{v:?}");
|
||
assert!(!dbg.contains("abandon") && !dbg.to_lowercase().contains("root"), "{dbg}");
|
||
}
|
||
|
||
/// No key export: the public surface of the identity module is locked. A new public function fails this
|
||
/// test until it has been checked to return no secret.
|
||
#[test]
|
||
fn the_identity_surface_is_locked() {
|
||
let dir = std::path::Path::new(env!("CARGO_MANIFEST_DIR")).join("src");
|
||
let mut got = Vec::new();
|
||
for file in ["identity.rs", "lib.rs"] {
|
||
let text = std::fs::read_to_string(dir.join(file)).unwrap();
|
||
for line in text.lines() {
|
||
let l = line.trim();
|
||
if l.starts_with("pub fn ") && (file == "identity.rs" || l.starts_with("pub fn domi_")) {
|
||
got.push(format!("{file}: {}", l.trim_end_matches(" {")));
|
||
}
|
||
}
|
||
}
|
||
got.sort();
|
||
let mut want = vec![
|
||
"identity.rs: pub fn begin() -> Enrollment",
|
||
"identity.rs: pub fn challenge(&self) -> [usize; 3]",
|
||
"identity.rs: pub fn confirm(self, words: &[&str; 3]) -> Result<Confirmed, (Shown, Error)>",
|
||
"identity.rs: pub fn import(self, vault: &mut Vault) -> Result<[u8; 32], Error>",
|
||
"identity.rs: pub fn restore(phrase: &str) -> Result<Confirmed, Error>",
|
||
"identity.rs: pub fn show(self, display: impl FnOnce(&[&str])) -> Shown",
|
||
"identity.rs: pub fn with_entropy(entropy: [u8; 32]) -> Enrollment",
|
||
// The app's own backup secret, derived: by design (docs/IDENTITY.md), never the root.
|
||
"lib.rs: pub fn domi_backup_secret(&self, app_id: &str) -> Result<Zeroizing<[u8; 32]>, Error>",
|
||
"lib.rs: pub fn domi_created(&self) -> Option<i64>",
|
||
"lib.rs: pub fn domi_issued(&self) -> &[identity::Issued]",
|
||
"lib.rs: pub fn domi_mark_revoked(&mut self, subkey: &str, revoked_at: u64) -> Result<u64, Error>",
|
||
"lib.rs: pub fn domi_record(&mut self, issued: identity::Issued) -> Result<(), Error>",
|
||
"lib.rs: pub fn domi_record_revocation(&mut self, subkey: &str, statements: Vec<String>) -> Result<(), Error>",
|
||
"lib.rs: pub fn domi_root_public(&self) -> Option<[u8; 32]>",
|
||
"lib.rs: pub fn domi_sign_cert(&self, tbs: &[u8]) -> Result<[u8; 64], Error>",
|
||
"lib.rs: pub fn domi_sign_revocation(&self, tbs: &[u8]) -> Result<[u8; 64], Error>",
|
||
];
|
||
want.sort();
|
||
assert_eq!(got, want, "review the identity surface: it must never return the root secret");
|
||
}
|