[vault] The recovery key: one per two-factor vault, returned once, taken only by Vault::recover, which replaces every factor and the key (slot kind 5, three audit actions); v0.17.0
This commit is contained in:
@@ -22,6 +22,7 @@ mod format;
|
||||
pub mod identity;
|
||||
mod item;
|
||||
pub mod provision;
|
||||
mod recovery;
|
||||
mod save;
|
||||
mod secret;
|
||||
pub mod security;
|
||||
@@ -29,6 +30,7 @@ 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 secret::{SecretBytes, SecretString};
|
||||
|
||||
use crypto::KEY_LEN;
|
||||
@@ -63,6 +65,12 @@ pub enum Error {
|
||||
FactorRejected,
|
||||
#[error("these two factors are kept on the same device; use one of them with another factor")]
|
||||
SameDevice,
|
||||
#[error("this is not a recovery key as Custos prints it: check each group of characters")]
|
||||
RecoveryKeyMistyped,
|
||||
#[error("this recovery key does not open this vault: it is another vault's, or an older one that was replaced")]
|
||||
WrongRecoveryKey,
|
||||
#[error("this vault has no recovery key")]
|
||||
NoRecoveryKey,
|
||||
#[error("a vault has at most 8 factors and one master password")]
|
||||
TooManyFactors,
|
||||
#[error("this vault has no Domi identity yet")]
|
||||
@@ -271,6 +279,7 @@ const BODY_FORMAT_2: u32 = 2;
|
||||
const INFO_FACTOR: &[u8] = b"custos factor v2";
|
||||
const INFO_PAIR: &[u8] = b"custos pair v2";
|
||||
const INFO_SINGLE: &[u8] = b"custos single v2";
|
||||
const INFO_RECOVERY: &[u8] = b"custos recovery v2";
|
||||
|
||||
#[derive(Deserialize)]
|
||||
struct Body {
|
||||
@@ -721,6 +730,106 @@ impl Vault {
|
||||
}
|
||||
}
|
||||
|
||||
// ---- the recovery key (docs/FACTORS.md, "The recovery key") ----
|
||||
|
||||
/// Which recovery key a vault file has (its id, 8 hex characters), from the header alone.
|
||||
pub fn recovery_id_of(bytes: &[u8]) -> Result<Option<String>, Error> {
|
||||
Ok(Header::parse(bytes)?.0.recovery_slot().map(|s| recovery::id_hex(&s.id)))
|
||||
}
|
||||
|
||||
/// Which recovery key this vault has.
|
||||
pub fn recovery_id(&self) -> Option<String> {
|
||||
self.slots
|
||||
.iter()
|
||||
.find(|s| s.kind == format::SLOT_RECOVERY)
|
||||
.and_then(|s| format::RecoverySlot::parse(&s.payload).ok())
|
||||
.map(|s| recovery::id_hex(&s.id))
|
||||
}
|
||||
|
||||
fn recovery_put(&mut self) -> RecoveryKey {
|
||||
let mut id = [0u8; recovery::ID_LEN];
|
||||
crypto::fill_random(&mut id);
|
||||
let material = crypto::random_key();
|
||||
let mut salt = [0u8; SALT_LEN];
|
||||
crypto::fill_random(&mut salt);
|
||||
let mut slot = format::RecoverySlot { id, salt, nonce: crypto::random_nonce(), wrapped: Vec::new() };
|
||||
let kek = crypto::hkdf(material.as_ref(), &slot.salt, INFO_RECOVERY);
|
||||
slot.wrapped = crypto::seal(&kek, &slot.nonce, self.key.as_ref(), &slot.aad());
|
||||
self.slots.retain(|s| s.kind != format::SLOT_RECOVERY);
|
||||
self.slots.push(slot.raw());
|
||||
recovery::encode(&id, &material)
|
||||
}
|
||||
|
||||
/// Makes the vault's recovery key and returns it, once: it is in no file and no log, so what the caller does
|
||||
/// not show or print is lost. A key the vault already had stops working. Only a two-factor vault has one
|
||||
/// (not version 1, not a machine-key vault). The caller saves.
|
||||
pub fn recovery_create(&mut self, now: i64) -> Result<RecoveryKey, Error> {
|
||||
if self.version != VERSION_2 {
|
||||
return Err(Error::NeedTwoFactors);
|
||||
}
|
||||
if self.is_store() {
|
||||
return Err(Error::FactorRejected);
|
||||
}
|
||||
let key = self.recovery_put();
|
||||
self.security.recovery_note(security::Action::RecoveryCreated, key.id(), now);
|
||||
Ok(key)
|
||||
}
|
||||
|
||||
/// Removes the recovery key: from the next save on, only the factors open the vault. The caller saves.
|
||||
pub fn recovery_remove(&mut self, now: i64) -> Result<(), Error> {
|
||||
let id = self.recovery_id().ok_or(Error::NoRecoveryKey)?;
|
||||
self.slots.retain(|s| s.kind != format::SLOT_RECOVERY);
|
||||
self.security.recovery_note(security::Action::RecoveryRemoved, &id, now);
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// Opens a vault with its recovery key and, before handing it back, replaces what opened it until now: every
|
||||
/// factor is dropped, `new_password` and `second` (anything but a password) become its two factors, and the
|
||||
/// used recovery key is replaced by the new one returned here. The items and everything else stay. Nothing is
|
||||
/// written: the caller saves, and until it does the file is as it was.
|
||||
///
|
||||
/// This is the only call that takes a recovery key, so the key is never an everyday unlock.
|
||||
pub fn recover(
|
||||
bytes: &[u8],
|
||||
key: &SecretString,
|
||||
new_password: &SecretString,
|
||||
kdf: KdfParams,
|
||||
second: Unlock,
|
||||
label: &str,
|
||||
now: i64,
|
||||
) -> Result<(Vault, RecoveryKey), Error> {
|
||||
// What was typed is checked first: a typing mistake is not a wrong key.
|
||||
let (id, material) = recovery::decode(key)?;
|
||||
if second.kind() == FactorKind::Password {
|
||||
return Err(Error::NeedTwoFactors);
|
||||
}
|
||||
second.check()?;
|
||||
if new_password.expose().is_empty() {
|
||||
return Err(Error::WrongPassword);
|
||||
}
|
||||
kdf.check()?;
|
||||
let (header, body_at) = Header::parse(bytes)?;
|
||||
if header.version != VERSION_2 {
|
||||
return Err(Error::NoRecoveryKey);
|
||||
}
|
||||
let slot = header.recovery_slot().ok_or(Error::NoRecoveryKey)?;
|
||||
if slot.id != id {
|
||||
return Err(Error::WrongRecoveryKey);
|
||||
}
|
||||
let kek = crypto::hkdf(material.as_ref(), &slot.salt, INFO_RECOVERY);
|
||||
let k = crypto::open(&kek, &slot.nonce, &slot.wrapped, &slot.aad()).ok_or(Error::WrongRecoveryKey)?;
|
||||
let mut v = Vault::open_body(bytes, header, body_at, key32(k)?)?;
|
||||
// Whoever held the old factors holds nothing of this vault from the next save on.
|
||||
v.slots.clear();
|
||||
v.factors.clear();
|
||||
v.kdf_new = kdf;
|
||||
v.add_factor(Unlock::Password(new_password), "Master password")?;
|
||||
v.add_factor(second, label)?;
|
||||
let new_key = v.recovery_put();
|
||||
v.security.recovery_note(security::Action::Recovered, &recovery::id_hex(&id), now);
|
||||
Ok((v, new_key))
|
||||
}
|
||||
|
||||
fn is_store(&self) -> bool {
|
||||
self.slots.iter().any(|s| s.kind == format::SLOT_SINGLE)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user