205 lines
8.8 KiB
Rust
205 lines
8.8 KiB
Rust
//! v1.1a: revocation (`TRV1`, docs/V1_1.md §1) and the backup secret (§3).
|
|
//!
|
|
//! - A revocation is root-signed in its own domain; the root's signer refuses to sign it as a cert and the
|
|
//! other way round.
|
|
//! - It keeps exactly the listed events of the revoked subkey; every other event by that subkey is
|
|
//! rejected, whatever time it claims. Unrelated authors are untouched.
|
|
//! - Split statements count only when complete; the result does not depend on the order they arrive in;
|
|
//! two statements for one subkey keep only what both keep.
|
|
//! - The backup secret is the same from the 24 words every time, differs per app, and seals so that only
|
|
//! the same secret opens.
|
|
|
|
use proptest::prelude::*;
|
|
use tessera::{
|
|
BackupSecret, CertRequest, Id, PublicKey, Revocation, RevocationError, RevocationSet, RevokeSigner, RootKey,
|
|
RootSigner, Subkey, hash, revoke,
|
|
};
|
|
|
|
fn root(n: u8) -> RootKey {
|
|
let m = bip39::Mnemonic::from_entropy(&[n; 32]).unwrap();
|
|
RootKey::from_mnemonic(&m.to_string()).unwrap()
|
|
}
|
|
|
|
fn h(n: u32) -> [u8; 32] {
|
|
hash(&n.to_be_bytes())
|
|
}
|
|
|
|
const T: u64 = 1_793_700_000;
|
|
|
|
#[test]
|
|
fn a_revocation_round_trips_and_verifies_under_its_root() {
|
|
let r = root(1);
|
|
let sub = Subkey::from_seed(&[5; 32]).public();
|
|
let parts = revoke(&r, sub, T, vec![h(3), h(1), h(2), h(1)]).unwrap();
|
|
assert_eq!(parts.len(), 1);
|
|
let p = &parts[0];
|
|
assert_eq!((p.root(), p.subkey(), p.revoked_at()), (r.public(), sub, T));
|
|
assert_eq!(
|
|
p.accepted(),
|
|
&[h(1), h(2), h(3)].iter().copied().collect::<std::collections::BTreeSet<_>>().into_iter().collect::<Vec<_>>()
|
|
);
|
|
let back = Revocation::from_bytes(&p.to_bytes()).unwrap();
|
|
assert_eq!(&back, p);
|
|
assert!(back.verify(&r.public()).is_ok());
|
|
assert_eq!(back.verify(&root(2).public()), Err(RevocationError::WrongRoot));
|
|
}
|
|
|
|
#[test]
|
|
fn a_tampered_or_malformed_revocation_is_refused() {
|
|
let r = root(1);
|
|
let bytes = revoke(&r, Subkey::from_seed(&[5; 32]).public(), T, vec![h(1)]).unwrap()[0].to_bytes();
|
|
for i in [5, 40, 70, 80, bytes.len() - 1] {
|
|
let mut b = bytes.clone();
|
|
b[i] ^= 1;
|
|
let bad = Revocation::from_bytes(&b).map(|x| x.verify(&r.public()));
|
|
assert!(!matches!(bad, Ok(Ok(()))), "flip at {i} still verifies");
|
|
}
|
|
for cut in 0..bytes.len() {
|
|
assert!(Revocation::from_bytes(&bytes[..cut]).is_err(), "a truncation at {cut} decoded");
|
|
}
|
|
let mut extra = bytes.clone();
|
|
extra.push(0);
|
|
assert!(Revocation::from_bytes(&extra).is_err(), "trailing bytes");
|
|
let mut as_cert = bytes;
|
|
as_cert[..4].copy_from_slice(b"TSC1");
|
|
assert!(Revocation::from_bytes(&as_cert).is_err(), "a cert is not a revocation");
|
|
}
|
|
|
|
#[test]
|
|
fn a_root_signs_a_revocation_and_a_cert_only_in_their_own_domains() {
|
|
let r = root(1);
|
|
let sub = Subkey::from_seed(&[5; 32]).public();
|
|
let req = CertRequest::standard(sub, Id::new("fiscus").unwrap(), Id::new("sync").unwrap(), T);
|
|
let cert_tbs = req.tbs(&r.public());
|
|
assert!(r.sign_revocation_tbs(&cert_tbs).is_err(), "a cert's tbs is not signed as a revocation");
|
|
let rev_tbs = revoke(&r, sub, T, vec![]).unwrap()[0].tbs();
|
|
assert!(r.sign_cert_tbs(&rev_tbs).is_err(), "a revocation's tbs is not signed as a cert");
|
|
let other = revoke(&root(2), sub, T, vec![]).unwrap()[0].tbs();
|
|
assert!(r.sign_revocation_tbs(&other).is_err(), "a root signs only revocations that name it");
|
|
}
|
|
|
|
#[test]
|
|
fn only_the_listed_events_of_a_revoked_subkey_stay_valid() {
|
|
let r = root(1);
|
|
let lost = Subkey::from_seed(&[5; 32]).public();
|
|
let other = Subkey::from_seed(&[6; 32]).public();
|
|
let mut set = RevocationSet::default();
|
|
for p in revoke(&r, lost, T, vec![h(1), h(2)]).unwrap() {
|
|
set.add(p, &[r.public()]).unwrap();
|
|
}
|
|
assert!(set.check(&lost, &h(1)).is_ok());
|
|
assert!(set.check(&lost, &h(2)).is_ok());
|
|
assert!(set.check(&lost, &h(3)).is_err(), "written after, or backdated: rejected either way");
|
|
assert!(set.check(&other, &h(3)).is_ok(), "other authors are untouched");
|
|
assert!(set.is_revoked(&lost) && !set.is_revoked(&other));
|
|
}
|
|
|
|
#[test]
|
|
fn a_revocation_from_a_root_outside_the_group_is_refused() {
|
|
let lost = Subkey::from_seed(&[5; 32]).public();
|
|
let mut set = RevocationSet::default();
|
|
let p = revoke(&root(9), lost, T, vec![]).unwrap().remove(0);
|
|
assert_eq!(set.add(p, &[root(1).public()]), Err(RevocationError::UnknownRoot));
|
|
assert!(set.check(&lost, &h(1)).is_ok());
|
|
}
|
|
|
|
#[test]
|
|
fn a_long_list_is_split_and_counts_only_when_complete() {
|
|
let r = root(1);
|
|
let lost = Subkey::from_seed(&[5; 32]).public();
|
|
let kept: Vec<[u8; 32]> = (0..tessera::REVOCATION_PART_MAX as u32 * 2 + 10).map(h).collect();
|
|
let parts = revoke(&r, lost, T, kept.clone()).unwrap();
|
|
assert_eq!(parts.len(), 3);
|
|
for p in &parts {
|
|
assert!(p.to_bytes().len() < 240 * 1024, "a part fits in one event");
|
|
}
|
|
let mut set = RevocationSet::default();
|
|
set.add(parts[0].clone(), &[r.public()]).unwrap();
|
|
set.add(parts[2].clone(), &[r.public()]).unwrap();
|
|
assert!(set.check(&lost, &h(u32::MAX)).is_ok(), "incomplete: not in force yet");
|
|
set.add(parts[1].clone(), &[r.public()]).unwrap();
|
|
assert!(set.check(&lost, &h(u32::MAX)).is_err(), "complete: in force");
|
|
assert!(kept.iter().all(|k| set.check(&lost, k).is_ok()));
|
|
}
|
|
|
|
#[test]
|
|
fn two_revocations_of_one_subkey_keep_only_what_both_keep() {
|
|
let r = root(1);
|
|
let lost = Subkey::from_seed(&[5; 32]).public();
|
|
let mut set = RevocationSet::default();
|
|
for p in revoke(&r, lost, T, vec![h(1), h(2), h(3)]).unwrap() {
|
|
set.add(p, &[r.public()]).unwrap();
|
|
}
|
|
for p in revoke(&r, lost, T + 60, vec![h(2), h(3), h(4)]).unwrap() {
|
|
set.add(p, &[r.public()]).unwrap();
|
|
}
|
|
let kept: Vec<u32> = (1..=4).filter(|n| set.check(&lost, &h(*n)).is_ok()).collect();
|
|
assert_eq!(kept, vec![2, 3], "a later statement never widens what is kept");
|
|
}
|
|
|
|
proptest! {
|
|
#[test]
|
|
fn the_result_does_not_depend_on_arrival_order(seed in any::<u64>(), keep in proptest::collection::vec(0u32..40, 0..30)) {
|
|
let r = root(1);
|
|
let lost = Subkey::from_seed(&[5; 32]).public();
|
|
let mut parts = revoke(&r, lost, T, keep.iter().map(|n| h(*n)).collect()).unwrap();
|
|
parts.extend(revoke(&r, lost, T + 1, (0..40).filter(|n| n % 3 != 0).map(h).collect()).unwrap());
|
|
let build = |order: &[Revocation]| {
|
|
let mut set = RevocationSet::default();
|
|
for p in order {
|
|
set.add(p.clone(), &[r.public()]).unwrap();
|
|
}
|
|
(0..40).map(|n| set.check(&lost, &h(n)).is_ok()).collect::<Vec<_>>()
|
|
};
|
|
let a = build(&parts);
|
|
let mut shuffled = parts.clone();
|
|
let mut s = seed;
|
|
for i in (1..shuffled.len()).rev() {
|
|
s = s.wrapping_mul(6364136223846793005).wrapping_add(1442695040888963407);
|
|
shuffled.swap(i, (s >> 33) as usize % (i + 1));
|
|
}
|
|
prop_assert_eq!(a, build(&shuffled));
|
|
}
|
|
}
|
|
|
|
#[test]
|
|
fn the_backup_secret_comes_back_from_the_words_and_differs_per_app() {
|
|
let fiscus = Id::new("fiscus").unwrap();
|
|
let a = root(1).backup_secret(&fiscus);
|
|
let b = root(1).backup_secret(&fiscus);
|
|
let other_app = root(1).backup_secret(&Id::new("calenda").unwrap());
|
|
let other_person = root(2).backup_secret(&fiscus);
|
|
let nonce = [7u8; 24];
|
|
let sealed = a.seal(&nonce, b"aad", b"the self group's log");
|
|
assert_eq!(b.open(b"aad", &sealed).as_deref(), Some(b"the self group's log".as_slice()));
|
|
assert_eq!(b.open(b"other aad", &sealed), None);
|
|
assert_eq!(other_app.open(b"aad", &sealed), None);
|
|
assert_eq!(other_person.open(b"aad", &sealed), None);
|
|
assert!(!sealed.windows(4).any(|w| w == b"self"), "sealed, not readable");
|
|
assert_ne!(*a.mailbox_seed(), *other_app.mailbox_seed());
|
|
assert_eq!(*a.mailbox_seed(), *b.mailbox_seed());
|
|
// An app gets the 32 bytes from Custos; nothing reads them back out.
|
|
let from_custos = BackupSecret::from_bytes([1u8; 32]);
|
|
assert_eq!(from_custos.open(b"", &from_custos.seal(&nonce, b"", b"x")).as_deref(), Some(b"x".as_slice()));
|
|
assert_eq!(format!("{from_custos:?}"), "BackupSecret([redacted])");
|
|
}
|
|
|
|
#[test]
|
|
fn the_backup_secret_test_vector() {
|
|
// docs/TESSERA.md §12: the zero-entropy mnemonic, app "fiscus". Custos derives the same.
|
|
let s = root(0).backup_secret(&Id::new("fiscus").unwrap());
|
|
let mailbox = data_encoding::BASE64URL_NOPAD.encode(s.mailbox_seed().as_ref());
|
|
let sealed = data_encoding::BASE64URL_NOPAD.encode(&s.seal(&[0u8; 24], b"", b""));
|
|
assert_eq!((mailbox.as_str(), sealed.as_str()), (VECTOR_MAILBOX, VECTOR_SEALED_EMPTY));
|
|
}
|
|
|
|
const VECTOR_MAILBOX: &str = "IygCOrh-ibO6-KY5AXQ9Prl2SyPWQIyZZ-anffcbMCU";
|
|
const VECTOR_SEALED_EMPTY: &str = "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAci_JRbtsx5prR83KT9Y7LA";
|
|
|
|
#[allow(dead_code)]
|
|
fn _signer_bound(_: &impl RevokeSigner) {}
|
|
#[allow(dead_code)]
|
|
fn _root_signer(_: &impl RootSigner) {}
|
|
#[allow(dead_code)]
|
|
fn _key(_: PublicKey) {}
|