164 lines
6.8 KiB
Rust
164 lines
6.8 KiB
Rust
//! The mandatory share tests (Day 2, tests 5 and 6) and the format checks from docs/SHARE.md.
|
|
|
|
use custos_share::{Field, Payload, ShareError, ShareKdf, open, seal, seal_with_kdf};
|
|
use custos_vault::{Item, ItemType, SecretString};
|
|
|
|
/// Fast parameters for tests; the format and the code path are the same as the default's.
|
|
const TEST_KDF: ShareKdf = ShareKdf { m_cost_kib: 8 * 1024, t_cost: 1, p_cost: 1 };
|
|
|
|
fn payload() -> Payload {
|
|
Payload {
|
|
title: "Router".into(),
|
|
kind: Some("wifi".into()),
|
|
fields: vec![Field::text("ssid", "Home"), Field::secret("password", "correct-horse-ž!")],
|
|
notes: None,
|
|
}
|
|
}
|
|
|
|
fn pw(s: &str) -> SecretString {
|
|
SecretString::from(s)
|
|
}
|
|
|
|
#[test]
|
|
fn round_trip_without_password() {
|
|
let s = seal(&payload(), None).unwrap();
|
|
assert!(s.fragment.starts_with("k=") && s.fragment.len() == 45, "{}", s.fragment);
|
|
assert!(!s.needs_password());
|
|
let got = open(&s.blob, &s.fragment, None).unwrap();
|
|
assert_eq!(got, payload());
|
|
}
|
|
|
|
#[test]
|
|
fn tamper_any_byte_or_wrong_key_fails_with_a_clear_error() {
|
|
let s = seal(&payload(), None).unwrap();
|
|
for i in 0..s.blob.len() {
|
|
let mut bad = s.blob.clone();
|
|
bad[i] ^= 0x01;
|
|
let e = open(&bad, &s.fragment, None).unwrap_err();
|
|
// Flipping the password flag on asks for a password; everything else fails to decrypt or parse.
|
|
assert!(
|
|
matches!(e, ShareError::DecryptFailed | ShareError::Malformed | ShareError::PasswordRequired),
|
|
"byte {i}: {e:?}"
|
|
);
|
|
}
|
|
// A different, well-formed key.
|
|
let other = seal(&payload(), None).unwrap();
|
|
assert_eq!(open(&s.blob, &other.fragment, None).unwrap_err(), ShareError::DecryptFailed);
|
|
// Truncated or extended.
|
|
assert!(open(&s.blob[..s.blob.len() - 1], &s.fragment, None).is_err());
|
|
let mut longer = s.blob.clone();
|
|
longer.push(0);
|
|
assert_eq!(open(&longer, &s.fragment, None).unwrap_err(), ShareError::DecryptFailed);
|
|
// The message is plain and never contains the secret.
|
|
let msg = ShareError::DecryptFailed.to_string();
|
|
assert!(msg.contains("damaged") && !msg.contains("correct-horse"), "{msg}");
|
|
}
|
|
|
|
#[test]
|
|
fn password_share_wrong_password_fails_right_one_works() {
|
|
let s = seal_with_kdf(&payload(), Some(&pw("tell-them-by-phone")), TEST_KDF).unwrap();
|
|
assert!(s.fragment.ends_with("&p=1") && s.needs_password());
|
|
assert_eq!(open(&s.blob, &s.fragment, None).unwrap_err(), ShareError::PasswordRequired);
|
|
assert_eq!(open(&s.blob, &s.fragment, Some(&pw("tell-them-by-phone!"))).unwrap_err(), ShareError::DecryptFailed);
|
|
assert_eq!(open(&s.blob, &s.fragment, Some(&pw(""))).unwrap_err(), ShareError::DecryptFailed);
|
|
assert_eq!(open(&s.blob, &s.fragment, Some(&pw("tell-them-by-phone"))).unwrap(), payload());
|
|
// The link alone is not enough even with the hint removed: the blob decides.
|
|
let bare = s.fragment.trim_end_matches("&p=1");
|
|
assert_eq!(open(&s.blob, bare, None).unwrap_err(), ShareError::PasswordRequired);
|
|
// The password alone is not enough: a different link key with the right password fails.
|
|
let other = seal(&payload(), None).unwrap();
|
|
let wrong_key = format!("{}&p=1", other.fragment);
|
|
assert_eq!(open(&s.blob, &wrong_key, Some(&pw("tell-them-by-phone"))).unwrap_err(), ShareError::DecryptFailed);
|
|
}
|
|
|
|
#[test]
|
|
fn default_kdf_is_the_documented_one_and_works() {
|
|
assert_eq!(ShareKdf::DEFAULT, ShareKdf { m_cost_kib: 19_456, t_cost: 2, p_cost: 1 });
|
|
let s = seal(&payload(), Some(&pw("pw"))).unwrap();
|
|
assert_eq!(&s.blob[5..17], [0x00, 0x4c, 0, 0, 2, 0, 0, 0, 1, 0, 0, 0]);
|
|
assert_eq!(open(&s.blob, &s.fragment, Some(&pw("pw"))).unwrap(), payload());
|
|
}
|
|
|
|
#[test]
|
|
fn layout_matches_docs() {
|
|
let s = seal(&payload(), None).unwrap();
|
|
assert_eq!(&s.blob[..4], b"CSH1");
|
|
assert_eq!(s.blob[4], 0);
|
|
let p = seal_with_kdf(&payload(), Some(&pw("x")), TEST_KDF).unwrap();
|
|
assert_eq!(p.blob[4], 1);
|
|
// magic + flags + kdf (12) + salt (16) + nonce (24) + tag (16) + plaintext
|
|
assert_eq!(p.blob.len(), s.blob.len() + 28);
|
|
// Unknown flags are rejected before any crypto.
|
|
let mut bad = s.blob.clone();
|
|
bad[4] = 0x02;
|
|
assert_eq!(open(&bad, &s.fragment, None).unwrap_err(), ShareError::Malformed);
|
|
}
|
|
|
|
#[test]
|
|
fn kdf_limits_are_checked_before_argon2() {
|
|
let s = seal_with_kdf(&payload(), Some(&pw("x")), TEST_KDF).unwrap();
|
|
for (offset, value) in [(5usize, 300 * 1024u32), (5, 1024), (9, 17), (9, 0), (13, 5), (13, 0)] {
|
|
let mut bad = s.blob.clone();
|
|
bad[offset..offset + 4].copy_from_slice(&value.to_le_bytes());
|
|
assert_eq!(
|
|
open(&bad, &s.fragment, Some(&pw("x"))).unwrap_err(),
|
|
ShareError::UnsupportedKdf,
|
|
"{offset}={value}"
|
|
);
|
|
}
|
|
}
|
|
|
|
#[test]
|
|
fn bad_links_are_rejected() {
|
|
let s = seal(&payload(), None).unwrap();
|
|
for frag in
|
|
["", "k=", "k=abc", "x=1", &s.fragment[..44], &format!("{}A", s.fragment), &s.fragment.replace('k', "K")]
|
|
{
|
|
assert_eq!(open(&s.blob, frag, None).unwrap_err(), ShareError::BadLink, "{frag:?}");
|
|
}
|
|
// A leading '#' (location.hash) is accepted.
|
|
assert!(open(&s.blob, &format!("#{}", s.fragment), None).is_ok());
|
|
}
|
|
|
|
#[test]
|
|
fn size_limit() {
|
|
let big = Payload { notes: Some(SecretString::new("x".repeat(70_000))), ..payload() };
|
|
assert_eq!(seal(&big, None).unwrap_err(), ShareError::TooLarge);
|
|
let mut blob = seal(&payload(), None).unwrap().blob.clone();
|
|
blob.resize(64 * 1024 + 1, 0);
|
|
assert_eq!(open(&blob, "k=AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA", None).unwrap_err(), ShareError::Malformed);
|
|
}
|
|
|
|
#[test]
|
|
fn every_link_key_and_nonce_is_fresh() {
|
|
let a = seal(&payload(), None).unwrap();
|
|
let b = seal(&payload(), None).unwrap();
|
|
assert_ne!(a.fragment, b.fragment);
|
|
assert_ne!(a.blob[5..29], b.blob[5..29]);
|
|
}
|
|
|
|
#[test]
|
|
fn payload_from_item_leaves_out_empty_fields_totp_and_autotype() {
|
|
let mut item = Item::new(ItemType::WebLogin, "GitHub", 0);
|
|
item.set("username", "mark", 0);
|
|
item.set("password", "hunter2", 0);
|
|
item.set("totp", "JBSWY3DPEHPK3PXP", 0);
|
|
item.set("autotype-match", "exe:firefox.exe", 0);
|
|
item.notes = "recovery codes in the safe".into();
|
|
let p = Payload::from_item(&item);
|
|
assert_eq!(p.title, "GitHub");
|
|
assert_eq!(p.kind.as_deref(), Some("web-login"));
|
|
let names: Vec<_> = p.fields.iter().map(|f| f.name.as_str()).collect();
|
|
assert_eq!(names, ["username", "password"]);
|
|
assert!(p.fields[1].secret && !p.fields[0].secret);
|
|
assert_eq!(p.notes.as_ref().map(|n| n.expose()), Some("recovery codes in the safe"));
|
|
}
|
|
|
|
#[test]
|
|
fn secrets_are_redacted_in_debug() {
|
|
let s = seal(&payload(), Some(&pw("pw"))).unwrap();
|
|
let out = format!("{:?} {:?}", s, payload());
|
|
assert!(!out.contains("correct-horse") && !out.contains(&s.fragment[2..]), "{out}");
|
|
assert!(out.contains("[redacted]"), "{out}");
|
|
}
|