//! A passkey as a `passkey` row, its private half sealed under the vault.//!//! ```text//! seal (passkey, vmk, row_id, vmk_version) → PasskeyRow private_key_enc = seal(vmk, row_id, vmk_version, scalar)//! open (row, row_id, vmk) → Passkey the scalar opened, checked against the row's public_key//! ```//!//! The rule is [`super::sealed_row`]'s, the one a password's payload is//! sealed by too: the vault's `v2`, the row id and the key version as the//! associated data, the version the row's own `vmk_version` column beside//! `private_key_enc`.//!//! Only a device holding the VMK seals or opens one; the Worker stores the//! sealed text and never a key that opens it.use serde_json::{Map, Value};usesuper::passkey::{from_row, Passkey, PasskeyError};usesuper::sealed_row::{self, SealedError};usecrate::vault::seal::Vmk;implFrom<SealedError> forPasskeyError {fn from(error: SealedError) -> Self {match error {SealedError::NotSealed => PasskeyError::NotSealed,SealedError::DoesNotOpen => PasskeyError::DoesNotOpen,SealedError::MissingColumn(column) => PasskeyError::MissingColumn(column), } }}/// The columns a passkey writes, its private half sealed. `rp_name`,/// `user_name`, `transports` and `note` are the caller's, beside these.#[derive(Debug, Clone, PartialEq, Eq)]pubstructPasskeyRow {pub rp_id: String,pub user_handle: String,pub credential_id: String,pub algorithm: String,pub public_key: String,pub sign_count: u32,/// `v2:`… — never the scalar.pub private_key_enc: String,pub vmk_version: u32,}implPasskeyRow {/// The row's columns by key, as a create writes them.pubfn columns(&self) -> Map<String, Value> {letmut out = Map::new(); out.insert("rp_id".into(), Value::from(self.rp_id.clone())); out.insert("user_handle".into(), Value::from(self.user_handle.clone())); out.insert("credential_id".into(), Value::from(self.credential_id.clone())); out.insert("algorithm".into(), Value::from(self.algorithm.clone())); out.insert("public_key".into(), Value::from(self.public_key.clone())); out.insert("sign_count".into(), Value::from(self.sign_count)); out.insert("private_key_enc".into(), Value::from(self.private_key_enc.clone())); out.insert("vmk_version".into(), Value::from(self.vmk_version)); out }}/// Seal a passkey into the row `row_id`, under the VMK at `vmk_version`.pubfn seal( passkey: &Passkey, vmk: &Vmk, row_id: &str, vmk_version: u32,) -> Result<PasskeyRow, PasskeyError> {let scalar = passkey.private_scalar();let private_key_enc = sealed_row::seal(vmk, row_id, vmk_version, &scalar)?;Ok(PasskeyRow { rp_id: passkey.rp_id.clone(), user_handle: passkey.user_handle.clone(), credential_id: passkey.credential_id.clone(), algorithm: passkey.algorithm.as_str().to_string(), public_key: passkey.public_key.clone(), sign_count: passkey.sign_count, private_key_enc, vmk_version, })}/// Reopen the passkey a row holds: its sealed scalar opened under the VMK at/// the row's own `vmk_version`, then held to the row's `public_key`.pubfn open(row: &Map<String, Value>, row_id: &str, vmk: &Vmk) -> Result<Passkey, PasskeyError> {let text = |column: &'static str| -> Result<&str, PasskeyError> { row.get(column) .and_then(Value::as_str) .ok_or(PasskeyError::MissingColumn(column)) };let count = |column: &'static str| -> Result<u32, PasskeyError> {match row.get(column) {None | Some(Value::Null) => Ok(0),Some(value) => value .as_u64() .or_else(|| value.as_f64().filter(|n| n.fract() == 0.0 && *n >= 0.0).map(|n| n as u64)) .and_then(|n| u32::try_from(n).ok()) .ok_or(PasskeyError::MissingColumn(column)), } };let opened = sealed_row::open(row, row_id, vmk, "private_key_enc")?; from_row( text("rp_id")?, text("user_handle")?, text("credential_id")?, text("algorithm")?, text("public_key")?, count("sign_count")?, &opened, )}#[cfg(test)]#[path = "passkey_row_tests.rs"]mod tests;