[vault] Identity: restore from the 24 words, revocation signing for revoked keys only, the backup secret (docs/IDENTITY.md; v0.10.0)
This commit is contained in:
@@ -1,5 +1,6 @@
|
||||
//! The Domi identity's root key (docs/IDENTITY.md). It lives in the vault body and never leaves it: nothing
|
||||
//! here or on [`Vault`] returns it, and the vault signs nothing with it but subkey certificates.
|
||||
//! here or on [`Vault`] returns it, and the vault signs nothing with it but subkey certificates and
|
||||
//! revocations of keys the person revoked. It derives each app's backup secret from it.
|
||||
//!
|
||||
//! Creating it is a typestate, so the mnemonic is always shown before the root exists:
|
||||
//!
|
||||
@@ -46,6 +47,13 @@ use zeroize::Zeroizing;
|
||||
/// The root signs `CERT_DOMAIN ‖ tbs`, and only a `tbs` that starts with `CERT_MAGIC ‖ its public key`.
|
||||
const CERT_DOMAIN: &[u8] = b"tessera cert v1\0";
|
||||
const CERT_MAGIC: &[u8] = b"TSC1";
|
||||
/// And `REVOKE_DOMAIN ‖ tbs`, only for a `tbs` that starts with `REVOKE_MAGIC ‖ its public key ‖ a key the
|
||||
/// person revoked ‖ that key's revoked_at` (Fundamentum docs/TESSERA.md §12).
|
||||
const REVOKE_DOMAIN: &[u8] = b"tessera revoke v1\0";
|
||||
const REVOKE_MAGIC: &[u8] = b"TRV1";
|
||||
/// magic 4, root 32, subkey 32, revoked_at 8, part 2, parts 2, count 4.
|
||||
const REVOKE_HEADER: usize = 84;
|
||||
const BACKUP_SALT: &[u8] = b"tessera backup v1";
|
||||
const WORDS: usize = 24;
|
||||
|
||||
/// Step 1: fresh entropy. Nothing is derived from it until the words have been shown.
|
||||
@@ -111,6 +119,26 @@ impl Enrollment {
|
||||
display(&words);
|
||||
Shown { entropy: self.entropy, challenge: random_challenge() }
|
||||
}
|
||||
|
||||
/// Restores an identity from its whole phrase, typed back (docs/IDENTITY.md, "Restoring it"): all 24
|
||||
/// words prove more than three. Any case, any spacing. Anything that is not 24 English BIP39 words with a
|
||||
/// valid checksum is [`Error::NotAMnemonic`], which names no word.
|
||||
pub fn restore(phrase: &str) -> Result<Confirmed, Error> {
|
||||
// Room for lowercasing without a reallocation that would leave a copy behind.
|
||||
let mut joined = Zeroizing::new(String::with_capacity(phrase.len() * 3 + 1));
|
||||
for (i, w) in phrase.split_whitespace().enumerate() {
|
||||
if i > 0 {
|
||||
joined.push(' ');
|
||||
}
|
||||
joined.extend(w.chars().flat_map(char::to_lowercase));
|
||||
}
|
||||
if joined.split(' ').count() != WORDS {
|
||||
return Err(Error::NotAMnemonic);
|
||||
}
|
||||
let m =
|
||||
bip39::Mnemonic::parse_in_normalized(bip39::Language::English, &joined).map_err(|_| Error::NotAMnemonic)?;
|
||||
Ok(Confirmed { secret: crypto::domi_root_secret(&m) })
|
||||
}
|
||||
}
|
||||
|
||||
impl Shown {
|
||||
@@ -165,7 +193,7 @@ impl fmt::Debug for Confirmed {
|
||||
}
|
||||
|
||||
/// A cert the vault signed, for display (docs/IDENTITY.md). A record, not a trust list.
|
||||
#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
|
||||
#[derive(Clone, Debug, Default, PartialEq, Eq, Serialize, Deserialize)]
|
||||
pub struct Issued {
|
||||
/// `app` or `device`.
|
||||
pub kind: String,
|
||||
@@ -180,6 +208,12 @@ pub struct Issued {
|
||||
pub not_after: u64,
|
||||
/// Unix milliseconds.
|
||||
pub issued: i64,
|
||||
/// Unix seconds: when the person revoked the key (core 0.10.0).
|
||||
#[serde(default, skip_serializing_if = "Option::is_none")]
|
||||
pub revoked_at: Option<u64>,
|
||||
/// The signed `TRV1` statements, base64url, once an app sent the events to keep.
|
||||
#[serde(default, skip_serializing_if = "Vec::is_empty")]
|
||||
pub revocation: Vec<String>,
|
||||
}
|
||||
|
||||
/// The identity in an unlocked vault.
|
||||
@@ -212,6 +246,45 @@ impl Root {
|
||||
Ok(crypto::ed25519_sign(&self.secret, &msg))
|
||||
}
|
||||
|
||||
pub(crate) fn sign_revocation(&self, tbs: &[u8]) -> Result<[u8; 64], Error> {
|
||||
let names_me = tbs.len() >= REVOKE_HEADER && &tbs[..4] == REVOKE_MAGIC && tbs[4..36] == self.public;
|
||||
if !names_me {
|
||||
return Err(Error::NotARevocation);
|
||||
}
|
||||
let subkey = data_encoding::BASE64URL_NOPAD.encode(&tbs[36..68]);
|
||||
let at = u64::from_be_bytes(tbs[68..76].try_into().expect("8 bytes"));
|
||||
if !self.issued.iter().any(|i| i.subkey == subkey && i.revoked_at == Some(at)) {
|
||||
return Err(Error::NotARevocation);
|
||||
}
|
||||
let msg = Zeroizing::new([REVOKE_DOMAIN, tbs].concat());
|
||||
Ok(crypto::ed25519_sign(&self.secret, &msg))
|
||||
}
|
||||
|
||||
/// Marks every cert of `subkey` revoked; keeps the earliest time if one was marked before.
|
||||
pub(crate) fn mark_revoked(&mut self, subkey: &str, at: u64) -> Result<u64, Error> {
|
||||
let mut certs = self.issued.iter().filter(|i| i.subkey == subkey).peekable();
|
||||
if certs.peek().is_none() {
|
||||
return Err(Error::UnknownKey);
|
||||
}
|
||||
let at = certs.filter_map(|i| i.revoked_at).min().unwrap_or(at);
|
||||
self.issued.iter_mut().filter(|i| i.subkey == subkey).for_each(|i| i.revoked_at = Some(at));
|
||||
Ok(at)
|
||||
}
|
||||
|
||||
pub(crate) fn record_revocation(&mut self, subkey: &str, statements: Vec<String>) -> Result<(), Error> {
|
||||
let mut certs = self.issued.iter_mut().filter(|i| i.subkey == subkey).peekable();
|
||||
if certs.peek().is_none() {
|
||||
return Err(Error::UnknownKey);
|
||||
}
|
||||
certs.for_each(|i| i.revocation = statements.clone());
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// `HKDF-SHA256(salt "tessera backup v1", ikm = root secret, info = app id)` (docs/IDENTITY.md).
|
||||
pub(crate) fn backup_secret(&self, app_id: &str) -> Zeroizing<[u8; KEY_LEN]> {
|
||||
crypto::hkdf(self.secret.as_ref(), BACKUP_SALT, app_id.as_bytes())
|
||||
}
|
||||
|
||||
pub(crate) fn json(&self) -> RootJson {
|
||||
RootJson {
|
||||
root: SecretString::new(hex(self.secret.as_ref())),
|
||||
|
||||
@@ -71,6 +71,12 @@ pub enum Error {
|
||||
NotACert,
|
||||
#[error("those words are not the ones in the recovery phrase")]
|
||||
WrongWords,
|
||||
#[error("these are not the 24 words of a recovery phrase: check each word and their order")]
|
||||
NotAMnemonic,
|
||||
#[error("the Domi identity signs a revocation only for a key revoked in Custos, at the time it was revoked")]
|
||||
NotARevocation,
|
||||
#[error("the Domi identity did not certify this key")]
|
||||
UnknownKey,
|
||||
#[error("this vault already has {0} signing keys")]
|
||||
SigningExists(&'static str),
|
||||
#[error("signing keys: {0}")]
|
||||
@@ -828,4 +834,27 @@ impl Vault {
|
||||
self.identity.as_mut().ok_or(Error::NoIdentity)?.issued.push(issued);
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// The person revoked the key `subkey` (base64url): marks every cert of it. Returns the revocation time in
|
||||
/// effect, the earlier one if it was marked before. The caller saves the vault.
|
||||
pub fn domi_mark_revoked(&mut self, subkey: &str, revoked_at: u64) -> Result<u64, Error> {
|
||||
self.identity.as_mut().ok_or(Error::NoIdentity)?.mark_revoked(subkey, revoked_at)
|
||||
}
|
||||
|
||||
/// Ed25519 with the root over `"tessera revoke v1\0" ‖ tbs`, for a `tbs` that starts with `TRV1`, this
|
||||
/// root's public key, a key marked revoked and its `revoked_at` ([`Error::NotARevocation`] otherwise).
|
||||
pub fn domi_sign_revocation(&self, tbs: &[u8]) -> Result<[u8; 64], Error> {
|
||||
self.identity.as_ref().ok_or(Error::NoIdentity)?.sign_revocation(tbs)
|
||||
}
|
||||
|
||||
/// Keeps the signed statements (base64url `TRV1`) with every cert of `subkey`. The caller saves the vault.
|
||||
pub fn domi_record_revocation(&mut self, subkey: &str, statements: Vec<String>) -> Result<(), Error> {
|
||||
self.identity.as_mut().ok_or(Error::NoIdentity)?.record_revocation(subkey, statements)
|
||||
}
|
||||
|
||||
/// The backup secret of the app `app_id` (docs/IDENTITY.md): derived from the root, handed to that app
|
||||
/// only. The caller checks `app_id` (a-z, 0-9, `-`) and the app's right to it.
|
||||
pub fn domi_backup_secret(&self, app_id: &str) -> Result<Zeroizing<[u8; 32]>, Error> {
|
||||
Ok(self.identity.as_ref().ok_or(Error::NoIdentity)?.backup_secret(app_id))
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user