[vault] The recovery key is printed as 8 groups of 8
This commit is contained in:
@@ -29,8 +29,8 @@ pub mod security;
|
||||
pub mod signing;
|
||||
|
||||
pub use item::{Attachment, Field, HISTORY_LIMIT, HistoryEntry, Item, ItemType, new_id, now_ms};
|
||||
pub use save::{CrashPoint, save_atomic, save_atomic_crashing};
|
||||
pub use recovery::RecoveryKey;
|
||||
pub use save::{CrashPoint, save_atomic, save_atomic_crashing};
|
||||
pub use secret::{SecretBytes, SecretString};
|
||||
|
||||
use crypto::KEY_LEN;
|
||||
|
||||
@@ -13,8 +13,9 @@ pub(crate) const ID_LEN: usize = 4;
|
||||
pub(crate) const MATERIAL_LEN: usize = 32;
|
||||
const CHECK_LEN: usize = 4;
|
||||
const CHECK_CONTEXT: &[u8] = b"custos recovery key v1";
|
||||
/// 40 bytes as base32: 64 characters, written as 16 groups of 4.
|
||||
/// 40 bytes as base32: 64 characters, written as 8 groups of 8.
|
||||
const TEXT_LEN: usize = 64;
|
||||
const GROUP: usize = 8;
|
||||
|
||||
/// A recovery key as it is printed. `Debug` never shows it.
|
||||
pub struct RecoveryKey {
|
||||
@@ -28,7 +29,7 @@ impl RecoveryKey {
|
||||
&self.id
|
||||
}
|
||||
|
||||
/// The key: 16 groups of 4 characters, `A-Z` and `2-7`.
|
||||
/// The key: 8 groups of 8 characters, `A-Z` and `2-7`.
|
||||
pub fn expose(&self) -> &str {
|
||||
&self.text
|
||||
}
|
||||
@@ -59,9 +60,9 @@ pub(crate) fn encode(id: &[u8; ID_LEN], material: &[u8; MATERIAL_LEN]) -> Recove
|
||||
raw[ID_LEN..ID_LEN + MATERIAL_LEN].copy_from_slice(material);
|
||||
raw[ID_LEN + MATERIAL_LEN..].copy_from_slice(&check(id, material));
|
||||
let flat = Zeroizing::new(BASE32_NOPAD.encode(raw.as_ref()));
|
||||
let mut text = Zeroizing::new(String::with_capacity(TEXT_LEN + TEXT_LEN / 4));
|
||||
let mut text = Zeroizing::new(String::with_capacity(TEXT_LEN + TEXT_LEN / GROUP));
|
||||
for (i, c) in flat.chars().enumerate() {
|
||||
if i > 0 && i % 4 == 0 {
|
||||
if i > 0 && i % GROUP == 0 {
|
||||
text.push('-');
|
||||
}
|
||||
text.push(c);
|
||||
@@ -105,9 +106,14 @@ mod tests {
|
||||
let key = encode(&[0xa1, 0xb2, 0xc3, 0xd4], &[9; 32]);
|
||||
assert_eq!(key.id(), "a1b2c3d4");
|
||||
let text = key.expose().to_owned();
|
||||
assert_eq!(text.len(), 64 + 15);
|
||||
assert!(text.split('-').all(|g| g.len() == 4 && g.bytes().all(|b| b.is_ascii_uppercase() || (b'2'..=b'7').contains(&b))));
|
||||
for typed in [text.clone(), text.to_lowercase(), text.replace('-', " "), text.replace('-', ""), format!(" {text}\r\n")] {
|
||||
assert_eq!(text.len(), 64 + 7);
|
||||
assert!(
|
||||
text.split('-')
|
||||
.all(|g| g.len() == 8 && g.bytes().all(|b| b.is_ascii_uppercase() || (b'2'..=b'7').contains(&b)))
|
||||
);
|
||||
for typed in
|
||||
[text.clone(), text.to_lowercase(), text.replace('-', " "), text.replace('-', ""), format!(" {text}\r\n")]
|
||||
{
|
||||
let (id, material) = decode(&SecretString::from(typed.as_str())).unwrap();
|
||||
assert_eq!((id, *material), ([0xa1, 0xb2, 0xc3, 0xd4], [9; 32]));
|
||||
}
|
||||
@@ -119,7 +125,7 @@ mod tests {
|
||||
let text = encode(&[1, 2, 3, 4], &[9; 32]).expose().to_owned();
|
||||
let mistyped = |s: String| matches!(decode(&SecretString::from(s.as_str())), Err(Error::RecoveryKeyMistyped));
|
||||
// One character changed, anywhere: the check catches it.
|
||||
for at in [0usize, 7, 40, 78] {
|
||||
for at in [0usize, 7, 40, 70] {
|
||||
let mut chars: Vec<char> = text.chars().collect();
|
||||
if chars[at] == '-' {
|
||||
continue;
|
||||
@@ -130,12 +136,12 @@ mod tests {
|
||||
// Two groups swapped, a group missing, one too many, a character that is never in a key.
|
||||
let groups: Vec<&str> = text.split('-').collect();
|
||||
let mut swapped = groups.clone();
|
||||
swapped.swap(2, 9);
|
||||
swapped.swap(2, 6);
|
||||
if swapped != groups {
|
||||
assert!(mistyped(swapped.join("-")));
|
||||
}
|
||||
assert!(mistyped(groups[1..].join("-")));
|
||||
assert!(mistyped(format!("{text}-AAAA")));
|
||||
assert!(mistyped(format!("{text}-AAAAAAAA")));
|
||||
assert!(mistyped(text.replacen(|c: char| c != '-', "1", 1)));
|
||||
assert!(mistyped(text.replacen('-', "_", 1)));
|
||||
assert!(mistyped(String::new()));
|
||||
|
||||
@@ -86,7 +86,9 @@ fn a_factor_is_added_and_removed_without_the_others() {
|
||||
// Only the new factor's material is needed: its pairs come from the factor keys in the body.
|
||||
let phone = v.add_factor(Unlock::Material(FactorKind::Approval, &PHONE), "Phone").unwrap();
|
||||
let bytes = v.to_bytes().unwrap();
|
||||
let mut v = open(&bytes, Unlock::Material(FactorKind::Approval, &PHONE), Unlock::Material(FactorKind::Device, &DEVICE)).unwrap();
|
||||
let mut v =
|
||||
open(&bytes, Unlock::Material(FactorKind::Approval, &PHONE), Unlock::Material(FactorKind::Device, &DEVICE))
|
||||
.unwrap();
|
||||
let kinds: Vec<_> = v.factors().iter().map(|f| f.kind).collect();
|
||||
assert_eq!(kinds, [FactorKind::Password, FactorKind::Device, FactorKind::Approval]);
|
||||
// A lost device: drop its factor; the other two still open, the device no longer helps.
|
||||
@@ -108,7 +110,8 @@ fn a_forgotten_password_is_replaced_with_two_other_factors() {
|
||||
let mut v = two_factor_vault();
|
||||
v.add_factor(Unlock::Material(FactorKind::Totp, &TOTP), "TOTP backup").unwrap();
|
||||
let bytes = v.to_bytes().unwrap();
|
||||
let mut v = open(&bytes, Unlock::Material(FactorKind::Device, &DEVICE), Unlock::Material(FactorKind::Totp, &TOTP)).unwrap();
|
||||
let mut v =
|
||||
open(&bytes, Unlock::Material(FactorKind::Device, &DEVICE), Unlock::Material(FactorKind::Totp, &TOTP)).unwrap();
|
||||
v.change_password(&pw("New9-Password4"), FAST).unwrap();
|
||||
let bytes2 = v.to_bytes().unwrap();
|
||||
let dev = || Unlock::Material(FactorKind::Device, &DEVICE);
|
||||
@@ -168,7 +171,14 @@ fn a_machine_store_opens_with_its_device_key_alone() {
|
||||
let user = two_factor_vault().to_bytes().unwrap();
|
||||
assert!(!Vault::is_machine_store(&user).unwrap());
|
||||
assert!(matches!(Vault::open_machine_store(&user, &DEVICE), Err(Error::NeedTwoFactors)));
|
||||
assert!(Vault::open_with(&bytes, Unlock::Material(FactorKind::Device, &STORE), Unlock::Material(FactorKind::Device, &STORE)).is_err());
|
||||
assert!(
|
||||
Vault::open_with(
|
||||
&bytes,
|
||||
Unlock::Material(FactorKind::Device, &STORE),
|
||||
Unlock::Material(FactorKind::Device, &STORE)
|
||||
)
|
||||
.is_err()
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
@@ -178,10 +188,20 @@ fn limits_are_enforced() {
|
||||
v.add_factor(Unlock::Material(FactorKind::Device, &[100 + i; 32]), "extra").unwrap();
|
||||
}
|
||||
assert_eq!(v.factors().len(), 8);
|
||||
assert!(matches!(v.add_factor(Unlock::Material(FactorKind::Device, &[1; 32]), "ninth"), Err(Error::TooManyFactors)));
|
||||
assert!(matches!(
|
||||
v.add_factor(Unlock::Material(FactorKind::Device, &[1; 32]), "ninth"),
|
||||
Err(Error::TooManyFactors)
|
||||
));
|
||||
// Every pair still opens with 8 factors (8 factor slots + 28 pair slots).
|
||||
let bytes = v.to_bytes().unwrap();
|
||||
assert!(open(&bytes, Unlock::Material(FactorKind::Device, &[100; 32]), Unlock::Material(FactorKind::Device, &[105; 32])).is_ok());
|
||||
assert!(
|
||||
open(
|
||||
&bytes,
|
||||
Unlock::Material(FactorKind::Device, &[100; 32]),
|
||||
Unlock::Material(FactorKind::Device, &[105; 32])
|
||||
)
|
||||
.is_ok()
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
@@ -190,7 +210,10 @@ fn no_factor_material_in_debug_output() {
|
||||
let u = Unlock::Material(FactorKind::Device, &secret);
|
||||
let p = pw("Orbit7-Fable3");
|
||||
let printed = format!("{u:?} {:?}", Unlock::Password(&p));
|
||||
assert!(!printed.contains("171") && !printed.to_lowercase().contains("ab, ab") && !printed.contains("Orbit7"), "{printed}");
|
||||
assert!(
|
||||
!printed.contains("171") && !printed.to_lowercase().contains("ab, ab") && !printed.contains("Orbit7"),
|
||||
"{printed}"
|
||||
);
|
||||
let v = two_factor_vault();
|
||||
let printed = format!("{v:?} {:?}", v.factors());
|
||||
for f in v.factors() {
|
||||
@@ -207,7 +230,8 @@ fn the_key_pair_item_type_round_trips() {
|
||||
k.set("private-key", "-----BEGIN PRIVATE KEY-----\n…", 5);
|
||||
v.add(k);
|
||||
let bytes = v.to_bytes().unwrap();
|
||||
let back = open(&bytes, Unlock::Password(&pw("Orbit7-Fable3")), Unlock::Material(FactorKind::Device, &DEVICE)).unwrap();
|
||||
let back =
|
||||
open(&bytes, Unlock::Password(&pw("Orbit7-Fable3")), Unlock::Material(FactorKind::Device, &DEVICE)).unwrap();
|
||||
let item = back.items().iter().find(|i| i.kind == ItemType::KeyPair).unwrap();
|
||||
assert!(item.fields.iter().any(|f| f.name == "private-key" && f.secret));
|
||||
}
|
||||
|
||||
@@ -113,7 +113,8 @@ fn a_recovery_key_is_never_an_everyday_unlock() {
|
||||
// It does not count as a factor: two are still needed and it is not listed.
|
||||
assert_eq!(Vault::factor_ids(&bytes).unwrap().len(), 3);
|
||||
// Recovering needs a real second factor and a password worth the name.
|
||||
let two_passwords = Vault::recover(&bytes, &text, &pw("a new master password"), FAST, Unlock::Password(&old), "x", NOW);
|
||||
let two_passwords =
|
||||
Vault::recover(&bytes, &text, &pw("a new master password"), FAST, Unlock::Password(&old), "x", NOW);
|
||||
assert!(matches!(two_passwords, Err(Error::NeedTwoFactors)));
|
||||
let short = Vault::recover(&bytes, &text, &pw(""), FAST, device(&NEW_PC), "x", NOW);
|
||||
assert!(matches!(short, Err(Error::WrongPassword) | Err(Error::FactorRejected)));
|
||||
@@ -134,7 +135,7 @@ fn a_wrong_an_old_or_another_vaults_key_opens_nothing() {
|
||||
let mut other = vault();
|
||||
let others = other.recovery_create(NOW).unwrap();
|
||||
assert!(matches!(recover(&bytes, others.expose(), "a new master password"), Err(Error::WrongRecoveryKey)));
|
||||
// A key with this vault's id but other bytes (a forged sheet): the wrap decides, not the id.
|
||||
// The right key with its last character changed: the check says so before the vault is tried.
|
||||
let forged: String = {
|
||||
let mut flat: Vec<char> = second.expose().chars().collect();
|
||||
let last = flat.len() - 1;
|
||||
|
||||
+2
-2
@@ -117,8 +117,8 @@ deliberate exception to "never one secret", and it is hedged accordingly:
|
||||
- The vault key stays the same across a recovery: a **copy of the file from before** still opens with the old
|
||||
factors and the old recovery key. A recovery takes back the vault, not its old copies.
|
||||
|
||||
**As printed:** `id (4 bytes) ‖ key (32 bytes) ‖ check (4 bytes)` in base32 without padding, 64 characters in 16
|
||||
groups of 4 (`A–Z`, `2–7`). The check is the first 4 bytes of `SHA-256("custos recovery key v1" ‖ id ‖ key)`.
|
||||
**As printed:** `id (4 bytes) ‖ key (32 bytes) ‖ check (4 bytes)` in base32 without padding, 64 characters in 8
|
||||
groups of 8 (`A–Z`, `2–7`). The check is the first 4 bytes of `SHA-256("custos recovery key v1" ‖ id ‖ key)`.
|
||||
A reader ignores case, spaces and hyphens; any other character, another length or a failed check is
|
||||
`RecoveryKeyMistyped`, told apart from `WrongRecoveryKey` (a well-formed key that is not this vault's current one)
|
||||
before the vault is touched. The **id** (8 hex characters) is not secret: it is in the header, shown in Settings
|
||||
|
||||
Reference in New Issue
Block a user