//! A password as a `secret` row, its payload sealed on the owner's devices. //! //! ```text //! seal (payload, vmk, row_id, vmk_version) → PasswordRow value_enc = seal(vmk, row_id, vmk_version, payload JSON) //! open (row, row_id, vmk) → SecretPayload opened under the row's own vmk_version //! ``` //! //! The rule is [`super::sealed_row`]'s, the one a passkey's private scalar is //! sealed by: the vault's `v2` under the vault master key, the row id and the //! key version as the associated data, the version the row's own //! `vmk_version` column beside `value_enc`. The payload is the vault's typed //! [`SecretPayload`] (`{"kind":"login","password":…}`), the one shape Rust, //! the file and Swift already share. //! //! **Not the object's `sealed` column.** A `secret` row the Worker wrote //! keeps its value in `sealed`, `k1:` under the Worker's own `VAULT_KEY`, a //! key the Worker holds and opens with. A row Cotton Keys writes keeps //! `value_enc` alone, a key the Worker never holds, and writes no `hint`: four //! characters of a password are four characters the Worker would read. Rows //! the Worker sealed are left as they are; moving them is the coordinator's. use serde_json::{Map, Value}; use zeroize::Zeroizing; use super::sealed_row::{self, SealedError, VMK_VERSION}; use crate::vault::payload::SecretPayload; use crate::vault::seal::Vmk; /// The column a password row keeps its sealed payload in. pub const VALUE_ENC: &str = "value_enc"; /// Why a password did not seal or open, in words. Never the value. #[derive(Debug, Clone, PartialEq, Eq, thiserror::Error)] pub enum PasswordError { #[error("the password could not be sealed")] NotSealed, #[error("this password does not open with this vault key")] DoesNotOpen, #[error("this password row has no {0}")] MissingColumn(&'static str), #[error("the sealed payload is not a password this build reads")] NotAPayload, } impl From for PasswordError { fn from(error: SealedError) -> Self { match error { SealedError::NotSealed => PasswordError::NotSealed, SealedError::DoesNotOpen => PasswordError::DoesNotOpen, SealedError::MissingColumn(column) => PasswordError::MissingColumn(column), } } } /// The sealed columns a password writes. `kind`, `name`, `site` and /// `username` are the caller's, beside these. #[derive(Debug, Clone, PartialEq, Eq)] pub struct PasswordRow { /// `v2:`… — never the value. pub value_enc: String, pub vmk_version: u32, } impl PasswordRow { /// The row's sealed columns by key, as a write patches them. pub fn columns(&self) -> Map { let mut out = Map::new(); out.insert(VALUE_ENC.into(), Value::from(self.value_enc.clone())); out.insert(VMK_VERSION.into(), Value::from(self.vmk_version)); out } } /// Seal a payload into the row `row_id`, under the VMK at `vmk_version`. pub fn seal(payload: &SecretPayload, vmk: &Vmk, row_id: &str, vmk_version: u32) -> Result { let json = Zeroizing::new(serde_json::to_vec(payload).map_err(|_| PasswordError::NotSealed)?); let value_enc = sealed_row::seal(vmk, row_id, vmk_version, &json)?; Ok(PasswordRow { value_enc, vmk_version }) } /// Reopen the payload a row holds, under the VMK at the row's own /// `vmk_version`. A row the Worker sealed (`sealed`, no `value_enc`) is /// refused as missing its `value_enc`: this device never holds `VAULT_KEY`. pub fn open(row: &Map, row_id: &str, vmk: &Vmk) -> Result { let opened = sealed_row::open(row, row_id, vmk, VALUE_ENC)?; serde_json::from_slice(&opened).map_err(|_| PasswordError::NotAPayload) } #[cfg(test)] #[path = "password_row_tests.rs"] mod tests;