diff --git a/pkgs/id/crates/id-core/src/graph/delegation.rs b/pkgs/id/crates/id-core/src/graph/delegation.rs new file mode 100644 index 0000000000..346c74622e --- /dev/null +++ b/pkgs/id/crates/id-core/src/graph/delegation.rs @@ -0,0 +1,934 @@ +//! Delegation chains and two-key control transfer (slice +//! A-GRAPH-9132-01, #10239; design part A §11, statements S5.56, S5.57, +//! S5.61, S5.64). +//! +//! # Grants +//! +//! A [`Grant`] lets `grantee_key` act in a space within a [`Scope`] (exact +//! typed array-address prefixes) and a [`Permissions`] set, between +//! `not_before` and `expires` (UTC Unix seconds). It is a signed envelope +//! whose signer must be `grantor_key`. A root grant (no predecessor) must +//! come from the space's own key; any other grant names the grant that +//! gave its grantor authority. [`DelegationSet::authorize`] walks from a +//! leaf grant to the root and fails closed on the first problem, in this +//! order for each grant, leaf first: +//! +//! 1. chain longer than the profile's depth budget: `DelegationChainTooDeep`; +//! 2. a grantor key seen earlier in the chain (or the requester): `DelegationCycle`; +//! 3. a grant for another space: `WrongSpace`; +//! 4. a revoked grant: `Revoked`; +//! 5. outside its time window, widened by the clock skew allowance: +//! `NotYetValid` or `Expired`; +//! 6. at the leaf, a grantee, permission or path that does not cover the +//! request: `NotAuthorized`; above the leaf, a parent that did not +//! grant to the child's grantor (`BrokenChain`), lacks `delegate` +//! (`NotDelegable`), or grants less than the child claims +//! (`PermissionExceeded`, `ScopeExceeded`); +//! 7. a root not granted by the space's key (`NotAuthorized`), or a +//! predecessor that is not known (`MissingLink`). +//! +//! There is no implicit transitive delegation: a grantee can pass on +//! authority only with `delegate`. Before any of that, a delegated write +//! (anything but read) fails with `StaleFrontier` when the latest +//! verified authority frontier is older than the profile's revocation +//! staleness bound; a read is allowed and reported as stale. +//! +//! # Replay and revocation +//! +//! [`DelegationSet::admit_grant`] keeps `(grantor, domain, nonce)` unique: +//! the identical signed envelope again is idempotent, the same nonce with +//! a different body is `ReplayConflict`. A revocation is a signed record +//! naming a grant ID, signed by that grant's grantor or the space's key. +//! +//! # Transfer +//! +//! A transfer replaces the controlling key only when the old controller +//! signs an offer and the new controller signs an acceptance over the +//! *identical* [`TransferStatement`] bytes (space, both keys, scope, +//! nonce, challenge, purpose and expiry). It is a separate record type +//! from a grant and never changes the immutable space address. +//! +//! Cross-signing statements and recovery policy are not part of this +//! slice. Module wiring into `graph/mod.rs` belongs to slice 9110-09; +//! until then the integration test includes this file by path. + +use std::collections::{HashMap, HashSet}; +use std::fmt; + +use ed25519_dalek::SigningKey; + +use super::space_binding::SpaceAddress; +use super::term::{ObjectRef, Term}; +use crate::canonical_cbor::{Value, decode_canonical, encode_canonical}; +use crate::hash_profile::ProfileDispatch; +use crate::id_shape::HashProfile; +use crate::signed_envelope::{Body, Envelope, EnvelopeError, EnvelopeId, PayloadId, SUITE_ED25519}; + +/// Signing domain of a grant. +pub const GRANT_DOMAIN: &str = "id/delegation-grant"; +/// Signing domain of a revocation. +pub const REVOCATION_DOMAIN: &str = "id/delegation-revocation"; +/// Signing domain of a transfer offer. +pub const TRANSFER_OFFER_DOMAIN: &str = "id/space-transfer-offer"; +/// Signing domain of a transfer acceptance. +pub const TRANSFER_ACCEPT_DOMAIN: &str = "id/space-transfer-accept"; +/// Purpose bound inside every transfer statement. +pub const TRANSFER_PURPOSE: &str = "id/space-transfer"; +/// Signing context (record profile) of every record here. +pub const DELEGATION_PROFILE: &str = "delegation/v1"; +/// Payload version of every record here. +pub const PAYLOAD_VERSION: u64 = 1; +/// Longest nonce, in bytes. +pub const MAX_NONCE_BYTES: usize = 64; +/// Most prefixes in one scope. +pub const MAX_SCOPE_PREFIXES: usize = 64; + +/// Profile that names record and payload IDs. +pub const ID_PROFILE: HashProfile = HashProfile::RawBlake3_256; + +/// Operator-validated limits. Requests cannot change them. +#[derive(Debug, Clone, Copy, PartialEq, Eq)] +pub struct Profile { + /// Most grants in one chain. + pub max_depth: usize, + /// Allowed clock difference, in seconds, on both ends of a window. + pub clock_skew_secs: u64, + /// Oldest authority frontier, in seconds, a delegated write accepts. + pub max_revocation_staleness_secs: u64, +} + +impl Profile { + /// The design's initial deployment profile. + pub const INITIAL: Self = Self { + max_depth: 16, + clock_skew_secs: 300, + max_revocation_staleness_secs: 24 * 60 * 60, + }; +} + +/// One permission. +#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] +pub enum Permission { + /// Read. + Read, + /// Append new records. + Append, + /// Edit existing records. + Edit, + /// Moderate. + Moderate, + /// Pass authority on to others. + Delegate, +} + +impl Permission { + const fn bit(self) -> u8 { + match self { + Self::Read => 1, + Self::Append => 2, + Self::Edit => 4, + Self::Moderate => 8, + Self::Delegate => 16, + } + } +} + +/// A set of permissions. +#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Default)] +pub struct Permissions(u8); + +impl Permissions { + /// Every defined permission bit. + pub const ALL_BITS: u8 = 31; + + /// The set from its bits. + /// + /// # Errors + /// + /// [`DelegationError::Malformed`] for an undefined bit. + pub const fn from_bits(bits: u8) -> Result { + if bits & !Self::ALL_BITS != 0 { + return Err(DelegationError::Malformed("unknown permission bit")); + } + Ok(Self(bits)) + } + + /// The set of these permissions. + #[must_use] + pub fn of(permissions: &[Permission]) -> Self { + Self(permissions.iter().fold(0, |bits, p| bits | p.bit())) + } + + /// The bits. + #[must_use] + pub const fn bits(self) -> u8 { + self.0 + } + + /// Whether `permission` is in the set. + #[must_use] + pub const fn has(self, permission: Permission) -> bool { + self.0 & permission.bit() != 0 + } + + /// Whether every permission in `other` is in this set. + #[must_use] + pub const fn covers(self, other: Self) -> bool { + other.0 & !self.0 == 0 + } +} + +/// Exact typed array-address prefixes. An empty prefix covers every +/// path; a path is covered when it starts with one of the prefixes. +#[derive(Debug, Clone, PartialEq, Eq)] +pub struct Scope { + prefixes: Vec>, +} + +fn prefix_value(prefix: &[Term]) -> Result { + prefix + .iter() + .map(|t| { + t.to_value() + .map_err(|_error| DelegationError::Malformed("scope term")) + }) + .collect::, _>>() + .map(Value::Array) +} + +fn sort_key(prefix: &[Term]) -> Vec { + prefix_value(prefix) + .ok() + .and_then(|value| encode_canonical(&value).ok()) + .unwrap_or_default() +} + +impl Scope { + /// A scope from prefixes, stored in canonical order without repeats. + /// + /// # Errors + /// + /// [`DelegationError::Malformed`] for too many prefixes or a term + /// that cannot be encoded. + pub fn new(mut prefixes: Vec>) -> Result { + if prefixes.len() > MAX_SCOPE_PREFIXES { + return Err(DelegationError::Malformed("too many scope prefixes")); + } + for prefix in &prefixes { + prefix_value(prefix)?; + } + prefixes.sort_by_cached_key(|p| sort_key(p)); + prefixes.dedup(); + Ok(Self { prefixes }) + } + + /// The prefixes, in canonical order. + #[must_use] + pub fn prefixes(&self) -> &[Vec] { + &self.prefixes + } + + /// Whether `path` starts with one of the prefixes. + #[must_use] + pub fn covers_path(&self, path: &[Term]) -> bool { + self.prefixes.iter().any(|p| path.starts_with(p)) + } + + /// Whether every path `other` covers is covered here. + #[must_use] + pub fn contains(&self, other: &Self) -> bool { + other.prefixes.iter().all(|p| self.covers_path(p)) + } + + fn to_value(&self) -> Result { + self.prefixes + .iter() + .map(|p| prefix_value(p)) + .collect::, _>>() + .map(Value::Array) + } + + fn from_value(value: &Value) -> Result { + let Value::Array(prefixes) = value else { + return Err(DelegationError::Malformed("scope must be an array")); + }; + let prefixes = prefixes + .iter() + .map(|p| { + let Value::Array(terms) = p else { + return Err(DelegationError::Malformed("scope prefix must be an array")); + }; + terms + .iter() + .map(|t| { + Term::from_value(t) + .map_err(|_error| DelegationError::Malformed("scope term")) + }) + .collect() + }) + .collect::, _>>()?; + Self::new(prefixes) + } +} + +/// A delegation grant. +#[derive(Debug, Clone, PartialEq, Eq)] +pub struct Grant { + /// The space the grant is for. + pub space: SpaceAddress, + /// The key that grants, and signs. + pub grantor_key: [u8; 32], + /// The key that receives authority. + pub grantee_key: [u8; 32], + /// Where the authority applies. + pub scope: Scope, + /// What the authority allows. + pub permissions: Permissions, + /// Earliest valid time, UTC Unix seconds. + pub not_before: u64, + /// Latest valid time, UTC Unix seconds. + pub expires: u64, + /// Replay-protection nonce, unique per grantor. + pub nonce: Vec, + /// The grant that gave the grantor authority; `None` for a root. + pub predecessor: Option, +} + +/// Why a delegation record or request was refused. +#[derive(Debug, Clone, PartialEq, Eq)] +pub enum DelegationError { + /// A record is not well formed. + Malformed(&'static str), + /// A record's envelope does not verify. + Envelope(EnvelopeError), + /// A record was signed for another purpose or profile. + WrongDomain(String), + /// A record is not signed by the key it must be signed by. + WrongSigner, + /// The same grantor, domain and nonce name a different record. + ReplayConflict, + /// A revocation names a grant this set does not hold. + UnknownGrant, + /// The request is not covered. + NotAuthorized(&'static str), + /// A grant is for another space. + WrongSpace, + /// A grant was revoked. + Revoked, + /// A grant's window has not started. + NotYetValid, + /// A grant or transfer has expired. + Expired, + /// A parent did not grant to the child's grantor. + BrokenChain, + /// A parent lacks `delegate`. + NotDelegable, + /// A child claims a permission its parent lacks. + PermissionExceeded, + /// A child claims scope its parent lacks. + ScopeExceeded, + /// A predecessor grant is not known. + MissingLink, + /// A key grants twice in one chain. + DelegationCycle, + /// The chain is longer than the depth budget. + DelegationChainTooDeep, + /// A delegated write against a too-old authority frontier. + StaleFrontier, + /// A transfer's offer and acceptance sign different statements. + TransferMismatch, + /// A transfer hands control to the key that already holds it. + SameKey, +} + +impl DelegationError { + /// A stable failure class name. + #[must_use] + pub const fn class(&self) -> &'static str { + match self { + Self::Malformed(_) => "malformed", + Self::Envelope(_) => "bad_signature", + Self::WrongDomain(_) => "wrong_domain", + Self::WrongSigner => "wrong_signer", + Self::ReplayConflict => "replay_conflict", + Self::UnknownGrant => "unknown_grant", + Self::NotAuthorized(_) => "not_authorized", + Self::WrongSpace => "wrong_space", + Self::Revoked => "revoked", + Self::NotYetValid => "not_yet_valid", + Self::Expired => "expired", + Self::BrokenChain => "broken_chain", + Self::NotDelegable => "not_delegable", + Self::PermissionExceeded => "permission_exceeded", + Self::ScopeExceeded => "scope_exceeded", + Self::MissingLink => "missing_link", + Self::DelegationCycle => "delegation_cycle", + Self::DelegationChainTooDeep => "delegation_chain_too_deep", + Self::StaleFrontier => "stale_frontier", + Self::TransferMismatch => "transfer_mismatch", + Self::SameKey => "same_key", + } + } +} + +impl fmt::Display for DelegationError { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + match self { + Self::Malformed(why) => write!(f, "malformed delegation record: {why}"), + Self::Envelope(error) => write!(f, "delegation record: {error}"), + Self::WrongDomain(domain) => write!(f, "record domain {domain:?} is not expected here"), + Self::NotAuthorized(why) => write!(f, "not authorized: {why}"), + other => f.write_str(other.class()), + } + } +} + +impl std::error::Error for DelegationError {} + +impl From for DelegationError { + fn from(error: EnvelopeError) -> Self { + Self::Envelope(error) + } +} + +fn key_from(value: &Value, what: &'static str) -> Result<[u8; 32], DelegationError> { + let Value::Bytes(bytes) = value else { + return Err(DelegationError::Malformed(what)); + }; + <[u8; 32]>::try_from(bytes.as_slice()).map_err(|_error| DelegationError::Malformed(what)) +} + +fn space_from(value: &Value) -> Result { + let Value::Bytes(bytes) = value else { + return Err(DelegationError::Malformed("space must be bytes")); + }; + SpaceAddress::from_bytes(bytes).map_err(|_error| DelegationError::Malformed("space address")) +} + +const fn uint(value: &Value, what: &'static str) -> Result { + match value { + Value::UInt(n) => Ok(*n), + _ => Err(DelegationError::Malformed(what)), + } +} + +fn nonce_from(value: &Value) -> Result, DelegationError> { + match value { + Value::Bytes(bytes) if !bytes.is_empty() && bytes.len() <= MAX_NONCE_BYTES => { + Ok(bytes.clone()) + } + _ => Err(DelegationError::Malformed("nonce must be 1 to 64 bytes")), + } +} + +fn envelope_ref(id: EnvelopeId) -> Result { + Term::Object(ObjectRef::Envelope(id)) + .to_value() + .map_err(|_error| DelegationError::Malformed("grant reference")) +} + +fn envelope_ref_from(value: &Value) -> Result { + match Term::from_value(value) { + Ok(Term::Object(ObjectRef::Envelope(id))) => Ok(id), + _ => Err(DelegationError::Malformed("grant reference")), + } +} + +/// Decodes a payload array of the expected length. Each record decoder +/// then calls [`require_canonical`], which rebuilds the payload from the +/// decoded fields (with this module's version and, for transfers, its +/// purpose) and needs identical bytes: each record has one encoding, and +/// an unknown version or purpose is refused there. +fn payload_items(payload: &[u8], len: usize) -> Result, DelegationError> { + let Ok(Value::Array(items)) = decode_canonical(payload) else { + return Err(DelegationError::Malformed("payload must be an array")); + }; + if items.len() != len { + return Err(DelegationError::Malformed("payload has the wrong length")); + } + Ok(items) +} + +fn require_canonical(payload: &[u8], value: &Value) -> Result<(), DelegationError> { + match encode_canonical(value) { + Ok(bytes) if bytes == payload => Ok(()), + _ => Err(DelegationError::Malformed("payload is not canonical")), + } +} + +impl Grant { + fn to_value(&self) -> Result { + Ok(Value::Array(vec![ + Value::UInt(PAYLOAD_VERSION), + Value::Bytes(self.space.to_bytes()), + Value::Bytes(self.grantor_key.to_vec()), + Value::Bytes(self.grantee_key.to_vec()), + self.scope.to_value()?, + Value::UInt(u64::from(self.permissions.bits())), + Value::UInt(self.not_before), + Value::UInt(self.expires), + Value::Bytes(self.nonce.clone()), + match self.predecessor { + Some(id) => envelope_ref(id)?, + None => Value::Null, + }, + ])) + } + + /// The canonical payload bytes. + /// + /// # Errors + /// + /// [`DelegationError::Malformed`] when a field cannot be encoded. + pub fn payload_bytes(&self) -> Result, DelegationError> { + encode_canonical(&self.to_value()?) + .map_err(|_error| DelegationError::Malformed("grant encoding")) + } + + /// Decodes a grant from its canonical payload. + /// + /// # Errors + /// + /// [`DelegationError::Malformed`] for any other shape, a window that + /// ends before it starts, or a non-canonical encoding. + pub fn from_payload(payload: &[u8]) -> Result { + let items = payload_items(payload, 10)?; + let bits = u8::try_from(uint(&items[5], "permissions")?) + .map_err(|_error| DelegationError::Malformed("unknown permission bit"))?; + let grant = Self { + space: space_from(&items[1])?, + grantor_key: key_from(&items[2], "grantor key")?, + grantee_key: key_from(&items[3], "grantee key")?, + scope: Scope::from_value(&items[4])?, + permissions: Permissions::from_bits(bits)?, + not_before: uint(&items[6], "not_before")?, + expires: uint(&items[7], "expires")?, + nonce: nonce_from(&items[8])?, + predecessor: match &items[9] { + Value::Null => None, + other => Some(envelope_ref_from(other)?), + }, + }; + if grant.expires < grant.not_before { + return Err(DelegationError::Malformed("grant expires before it starts")); + } + require_canonical(payload, &grant.to_value()?)?; + Ok(grant) + } + + /// Signs the grant with the grantor's key. + /// + /// # Errors + /// + /// [`DelegationError::WrongSigner`] when `key` is not the grantor's, + /// or an encoding or signing error. + pub fn sign(&self, signer_id: &str, key: &SigningKey) -> Result { + if key.verifying_key().to_bytes() != self.grantor_key { + return Err(DelegationError::WrongSigner); + } + sign_record(GRANT_DOMAIN, self.payload_bytes()?, signer_id, key) + } + + const fn window(&self, utc: u64, skew: u64) -> Result<(), DelegationError> { + if self.not_before > utc.saturating_add(skew) { + return Err(DelegationError::NotYetValid); + } + if self.expires.saturating_add(skew) < utc { + return Err(DelegationError::Expired); + } + Ok(()) + } +} + +fn sign_record( + domain: &str, + payload: Vec, + signer_id: &str, + key: &SigningKey, +) -> Result { + let body = Body { + domain: domain.to_owned(), + context: DELEGATION_PROFILE.to_owned(), + subject: None, + graph: None, + predecessor: None, + signer_id: signer_id.to_owned(), + key: key.verifying_key().to_bytes(), + suite: SUITE_ED25519.to_owned(), + payload_id: PayloadId::of(ID_PROFILE, &payload).map_err(EnvelopeError::from)?, + payload_length: payload.len() as u64, + }; + Ok(Envelope::sign(body, payload, key)?) +} + +fn check_record( + envelope: &Envelope, + domain: &str, + dispatch: &ProfileDispatch, +) -> Result<(), DelegationError> { + if envelope.body.domain != domain { + return Err(DelegationError::WrongDomain(envelope.body.domain.clone())); + } + if envelope.body.context != DELEGATION_PROFILE { + return Err(DelegationError::WrongDomain(envelope.body.context.clone())); + } + envelope.verify(dispatch)?; + Ok(()) +} + +/// Signs a revocation of `grant` with `key`. +/// +/// # Errors +/// +/// An encoding or signing error. +pub fn sign_revocation( + grant: EnvelopeId, + signer_id: &str, + key: &SigningKey, +) -> Result { + let payload = encode_canonical(&Value::Array(vec![ + Value::UInt(PAYLOAD_VERSION), + envelope_ref(grant)?, + ])) + .map_err(|_error| DelegationError::Malformed("revocation encoding"))?; + sign_record(REVOCATION_DOMAIN, payload, signer_id, key) +} + +/// What [`DelegationSet::authorize`] established. +#[derive(Debug, Clone, Copy, PartialEq, Eq)] +pub enum Authority { + /// The requester holds the space's own key. + Owner, + /// A valid chain of this many grants covers the request. + Delegated { + /// Grants from leaf to root. + depth: usize, + /// The authority frontier is past the staleness bound; only a + /// read gets this far, and must be shown as stale. + stale: bool, + }, +} + +/// One action asked of a space. +#[derive(Debug, Clone, PartialEq, Eq)] +pub struct Request { + /// The space acted in. + pub space: SpaceAddress, + /// The key acting. + pub key: [u8; 32], + /// The typed array-address path acted on. + pub path: Vec, + /// The permission needed. + pub permission: Permission, +} + +/// The verifier's trusted view of time. +#[derive(Debug, Clone, Copy, PartialEq, Eq)] +pub struct Clock { + /// Trusted verifier time, UTC Unix seconds. + pub verifier_utc: u64, + /// Age, in seconds, of the latest verified authority frontier. + pub frontier_age_secs: u64, +} + +/// Grants and revocations verified at one authority frontier. +#[derive(Debug, Default, Clone)] +pub struct DelegationSet { + grants: HashMap, + nonces: HashMap<([u8; 32], Vec), EnvelopeId>, + revoked: HashSet, +} + +impl DelegationSet { + /// An empty set. + #[must_use] + pub fn new() -> Self { + Self::default() + } + + /// Verifies and admits a grant, returning its ID. + /// + /// # Errors + /// + /// [`DelegationError`] for a bad record, a signer other than the + /// grantor, or a reused nonce with a different record. + pub fn admit_grant( + &mut self, + envelope: &Envelope, + dispatch: &ProfileDispatch, + ) -> Result { + check_record(envelope, GRANT_DOMAIN, dispatch)?; + let grant = Grant::from_payload(&envelope.payload)?; + if envelope.body.key != grant.grantor_key { + return Err(DelegationError::WrongSigner); + } + let id = envelope.id(ID_PROFILE)?; + let slot = (grant.grantor_key, grant.nonce.clone()); + match self.nonces.get(&slot) { + Some(existing) if *existing == id => return Ok(id), + Some(_) => return Err(DelegationError::ReplayConflict), + None => {} + } + self.nonces.insert(slot, id); + self.grants.insert(id, grant); + Ok(id) + } + + /// Verifies and admits a revocation. + /// + /// # Errors + /// + /// [`DelegationError`] for a bad record, an unknown grant, or a + /// signer that is neither the grant's grantor nor the space's key. + pub fn admit_revocation( + &mut self, + envelope: &Envelope, + dispatch: &ProfileDispatch, + ) -> Result { + check_record(envelope, REVOCATION_DOMAIN, dispatch)?; + let items = payload_items(&envelope.payload, 2)?; + let target = envelope_ref_from(&items[1])?; + require_canonical( + &envelope.payload, + &Value::Array(vec![Value::UInt(PAYLOAD_VERSION), envelope_ref(target)?]), + )?; + let grant = self + .grants + .get(&target) + .ok_or(DelegationError::UnknownGrant)?; + if envelope.body.key != grant.grantor_key && envelope.body.key != *grant.space.key() { + return Err(DelegationError::WrongSigner); + } + self.revoked.insert(target); + Ok(target) + } + + /// The admitted grant with this ID. + #[must_use] + pub fn grant(&self, id: &EnvelopeId) -> Option<&Grant> { + self.grants.get(id) + } + + /// Decides `request`, following the chain that ends at `leaf`. + /// + /// # Errors + /// + /// The first [`DelegationError`] in the order the module docs give. + pub fn authorize( + &self, + request: &Request, + leaf: Option<&EnvelopeId>, + clock: Clock, + profile: &Profile, + ) -> Result { + if request.key == *request.space.key() { + return Ok(Authority::Owner); + } + let stale = clock.frontier_age_secs > profile.max_revocation_staleness_secs; + if stale && request.permission != Permission::Read { + return Err(DelegationError::StaleFrontier); + } + let Some(leaf) = leaf else { + return Err(DelegationError::NotAuthorized("no grant presented")); + }; + let mut seen: HashSet<[u8; 32]> = HashSet::from([request.key]); + let mut child: Option<&Grant> = None; + let mut at = *leaf; + let mut depth = 0; + loop { + let grant = self.grants.get(&at).ok_or(DelegationError::MissingLink)?; + depth += 1; + if depth > profile.max_depth { + return Err(DelegationError::DelegationChainTooDeep); + } + if !seen.insert(grant.grantor_key) { + return Err(DelegationError::DelegationCycle); + } + if grant.space != request.space { + return Err(DelegationError::WrongSpace); + } + if self.revoked.contains(&at) { + return Err(DelegationError::Revoked); + } + grant.window(clock.verifier_utc, profile.clock_skew_secs)?; + match child { + None => { + if grant.grantee_key != request.key { + return Err(DelegationError::NotAuthorized("grant is for another key")); + } + if !grant.permissions.has(request.permission) { + return Err(DelegationError::NotAuthorized("permission not granted")); + } + if !grant.scope.covers_path(&request.path) { + return Err(DelegationError::NotAuthorized("path outside the grant")); + } + } + Some(child) => { + if grant.grantee_key != child.grantor_key { + return Err(DelegationError::BrokenChain); + } + if !grant.permissions.has(Permission::Delegate) { + return Err(DelegationError::NotDelegable); + } + if !grant.permissions.covers(child.permissions) { + return Err(DelegationError::PermissionExceeded); + } + if !grant.scope.contains(&child.scope) { + return Err(DelegationError::ScopeExceeded); + } + } + } + match grant.predecessor { + None if grant.grantor_key == *request.space.key() => { + return Ok(Authority::Delegated { depth, stale }); + } + None => { + return Err(DelegationError::NotAuthorized( + "root not granted by the space", + )); + } + Some(parent) => { + child = Some(grant); + at = parent; + } + } + } + } +} + +/// The statement both keys sign to transfer control of a space. +#[derive(Debug, Clone, PartialEq, Eq)] +pub struct TransferStatement { + /// The space whose control moves. + pub space: SpaceAddress, + /// The current controller. + pub old_key: [u8; 32], + /// The new controller. + pub new_key: [u8; 32], + /// What control moves. + pub scope: Scope, + /// Replay-protection nonce. + pub nonce: Vec, + /// A challenge both parties saw. + pub challenge: Vec, + /// Latest valid time, UTC Unix seconds. + pub expires: u64, +} + +impl TransferStatement { + fn to_value(&self) -> Result { + Ok(Value::Array(vec![ + Value::UInt(PAYLOAD_VERSION), + Value::Text(TRANSFER_PURPOSE.to_owned()), + Value::Bytes(self.space.to_bytes()), + Value::Bytes(self.old_key.to_vec()), + Value::Bytes(self.new_key.to_vec()), + self.scope.to_value()?, + Value::Bytes(self.nonce.clone()), + Value::Bytes(self.challenge.clone()), + Value::UInt(self.expires), + ])) + } + + /// The canonical bytes both parties sign. + /// + /// # Errors + /// + /// [`DelegationError::Malformed`] when a field cannot be encoded. + pub fn payload_bytes(&self) -> Result, DelegationError> { + encode_canonical(&self.to_value()?) + .map_err(|_error| DelegationError::Malformed("transfer encoding")) + } + + fn from_payload(payload: &[u8]) -> Result { + let items = payload_items(payload, 9)?; + let Value::Bytes(challenge) = &items[7] else { + return Err(DelegationError::Malformed("challenge must be bytes")); + }; + let statement = Self { + space: space_from(&items[2])?, + old_key: key_from(&items[3], "old key")?, + new_key: key_from(&items[4], "new key")?, + scope: Scope::from_value(&items[5])?, + nonce: nonce_from(&items[6])?, + challenge: challenge.clone(), + expires: uint(&items[8], "expires")?, + }; + require_canonical(payload, &statement.to_value()?)?; + Ok(statement) + } + + /// The old controller's signed offer. + /// + /// # Errors + /// + /// [`DelegationError::WrongSigner`] when `key` is not the old key. + pub fn sign_offer( + &self, + signer_id: &str, + key: &SigningKey, + ) -> Result { + if key.verifying_key().to_bytes() != self.old_key { + return Err(DelegationError::WrongSigner); + } + sign_record(TRANSFER_OFFER_DOMAIN, self.payload_bytes()?, signer_id, key) + } + + /// The new controller's signed acceptance. + /// + /// # Errors + /// + /// [`DelegationError::WrongSigner`] when `key` is not the new key. + pub fn sign_acceptance( + &self, + signer_id: &str, + key: &SigningKey, + ) -> Result { + if key.verifying_key().to_bytes() != self.new_key { + return Err(DelegationError::WrongSigner); + } + sign_record( + TRANSFER_ACCEPT_DOMAIN, + self.payload_bytes()?, + signer_id, + key, + ) + } +} + +/// Verifies a transfer of control away from `controller`. +/// +/// # Errors +/// +/// [`DelegationError`] when either record fails to verify, the two do +/// not sign identical statement bytes, a signer is not the statement's +/// key, the old key is not `controller`, both keys are the same, or the +/// statement has expired. +pub fn verify_transfer( + offer: &Envelope, + acceptance: &Envelope, + controller: &[u8; 32], + clock: Clock, + profile: &Profile, + dispatch: &ProfileDispatch, +) -> Result { + check_record(offer, TRANSFER_OFFER_DOMAIN, dispatch)?; + check_record(acceptance, TRANSFER_ACCEPT_DOMAIN, dispatch)?; + if offer.payload != acceptance.payload { + return Err(DelegationError::TransferMismatch); + } + let statement = TransferStatement::from_payload(&offer.payload)?; + if offer.body.key != statement.old_key + || acceptance.body.key != statement.new_key + || statement.old_key != *controller + { + return Err(DelegationError::WrongSigner); + } + if statement.old_key == statement.new_key { + return Err(DelegationError::SameKey); + } + if statement.expires.saturating_add(profile.clock_skew_secs) < clock.verifier_utc { + return Err(DelegationError::Expired); + } + Ok(statement) +} diff --git a/pkgs/id/crates/id-core/tests/delegation_model.rs b/pkgs/id/crates/id-core/tests/delegation_model.rs new file mode 100644 index 0000000000..cb9223f400 --- /dev/null +++ b/pkgs/id/crates/id-core/tests/delegation_model.rs @@ -0,0 +1,1155 @@ +//! Delegation chains and two-key control transfer (slice +//! A-GRAPH-9132-01, issue #10239; design part A §11, statements S5.56, +//! S5.57, S5.61, S5.64). +//! +//! Oracle: **model**. The proptest at the end builds random grant +//! forests over a few keys (seed 0 owns the space), admits some of them, +//! revokes some, and asks random requests at random times, depths and +//! skews. An independent abstract authority-chain model, written over +//! plain integers and byte strings rather than the module's types, +//! decides each request; the module must return the same outcome and +//! failure class. Targeted tests pin down the cases the generator is +//! unlikely to hit on its own: replay, revocation signers, stale +//! frontiers, the depth budget, canonical payloads and transfers. +//! +//! `graph/mod.rs` does not export these modules yet (slice 9110-09 wires +//! them), so this test includes them, and the modules they build on, by +//! path. + +#![allow( + clippy::unwrap_used, + clippy::expect_used, + clippy::panic, + clippy::cast_possible_truncation, + clippy::too_many_lines, + clippy::type_complexity +)] + +#[path = "../src/canonical_cbor.rs"] +#[allow(dead_code)] +mod canonical_cbor; + +#[path = "../src/id_shape.rs"] +#[allow(dead_code)] +mod id_shape; + +#[path = "../src/hash_profile.rs"] +#[allow(dead_code)] +mod hash_profile; + +#[path = "../src/signed_envelope.rs"] +#[allow(dead_code)] +mod signed_envelope; + +#[path = "../src/graph/term.rs"] +#[allow(dead_code)] +mod term; + +#[path = "../src/graph/space_binding.rs"] +#[allow(dead_code)] +mod space_binding; + +#[path = "../src/graph/delegation.rs"] +mod delegation; + +use std::collections::HashSet; + +use canonical_cbor::{Value, decode_canonical, encode_canonical}; +use delegation::{ + Authority, Clock, DelegationError, DelegationSet, GRANT_DOMAIN, Grant, Permission, Permissions, + Profile, REVOCATION_DOMAIN, Request, Scope, TRANSFER_ACCEPT_DOMAIN, TRANSFER_OFFER_DOMAIN, + TransferStatement, sign_revocation, verify_transfer, +}; +use ed25519_dalek::SigningKey; +use hash_profile::ProfileDispatch; +use id_shape::HashProfile; +use proptest::prelude::*; +use signed_envelope::{Body, Envelope, EnvelopeId, PayloadId, SUITE_ED25519}; +use space_binding::SpaceAddress; +use term::Term; + +const SIGNER: &str = "tester"; + +fn key(seed: u8) -> SigningKey { + SigningKey::from_bytes(&[seed; 32]) +} + +fn public(seed: u8) -> [u8; 32] { + key(seed).verifying_key().to_bytes() +} + +fn space(seed: u8) -> SpaceAddress { + SpaceAddress::for_key(public(seed)).unwrap() +} + +fn dispatch() -> ProfileDispatch { + ProfileDispatch::default() +} + +fn step(b: u8) -> Term { + Term::typed(1, vec![b]) +} + +fn path(bytes: &[u8]) -> Vec { + bytes.iter().copied().map(step).collect() +} + +fn scope(prefixes: &[&[u8]]) -> Scope { + Scope::new(prefixes.iter().map(|p| path(p)).collect()).unwrap() +} + +const NOW: Clock = Clock { + verifier_utc: 1_000, + frontier_age_secs: 0, +}; + +/// Signs any payload under any domain, so tests can build records the +/// module would never make. +fn record(domain: &str, context: &str, payload: Vec, signer: &SigningKey) -> Envelope { + let body = Body { + domain: domain.to_owned(), + context: context.to_owned(), + subject: None, + graph: None, + predecessor: None, + signer_id: SIGNER.to_owned(), + key: signer.verifying_key().to_bytes(), + suite: SUITE_ED25519.to_owned(), + payload_id: PayloadId::of(HashProfile::RawBlake3_256, &payload).unwrap(), + payload_length: payload.len() as u64, + }; + Envelope::sign(body, payload, signer).unwrap() +} + +struct GrantSpec { + space: u8, + grantor: u8, + grantee: u8, + scope: Scope, + perms: Permissions, + window: (u64, u64), + nonce: u8, + pred: Option, +} + +impl GrantSpec { + fn new(grantor: u8, grantee: u8, pred: Option) -> Self { + Self { + space: 0, + grantor, + grantee, + scope: scope(&[&[]]), + perms: Permissions::from_bits(Permissions::ALL_BITS).unwrap(), + window: (0, 2_000), + nonce: 1, + pred, + } + } + + fn grant(&self) -> Grant { + Grant { + space: space(self.space), + grantor_key: public(self.grantor), + grantee_key: public(self.grantee), + scope: self.scope.clone(), + permissions: self.perms, + not_before: self.window.0, + expires: self.window.1, + nonce: vec![self.nonce], + predecessor: self.pred, + } + } + + fn sign(&self) -> Envelope { + self.grant().sign(SIGNER, &key(self.grantor)).unwrap() + } +} + +fn admit(set: &mut DelegationSet, spec: &GrantSpec) -> EnvelopeId { + set.admit_grant(&spec.sign(), &dispatch()).unwrap() +} + +fn request(seed: u8, at: &[u8], permission: Permission) -> Request { + Request { + space: space(0), + key: public(seed), + path: path(at), + permission, + } +} + +// --------------------------------------------------------------------- +// Targeted cases. + +#[test] +fn owner_and_a_two_link_chain_authorize() { + let mut set = DelegationSet::new(); + let root = admit(&mut set, &GrantSpec::new(0, 1, None)); + let mut leaf = GrantSpec::new(1, 2, Some(root)); + leaf.perms = Permissions::of(&[Permission::Read, Permission::Append]); + leaf.scope = scope(&[&[7]]); + let leaf = admit(&mut set, &leaf); + + let p = Profile::INITIAL; + assert_eq!( + set.authorize(&request(0, &[9], Permission::Moderate), None, NOW, &p), + Ok(Authority::Owner) + ); + assert_eq!( + set.authorize( + &request(2, &[7, 3], Permission::Append), + Some(&leaf), + NOW, + &p + ), + Ok(Authority::Delegated { + depth: 2, + stale: false + }) + ); + assert_eq!( + set.authorize(&request(2, &[8], Permission::Append), Some(&leaf), NOW, &p), + Err(DelegationError::NotAuthorized("path outside the grant")) + ); + assert_eq!( + set.authorize(&request(2, &[7], Permission::Edit), Some(&leaf), NOW, &p), + Err(DelegationError::NotAuthorized("permission not granted")) + ); + assert_eq!( + set.authorize(&request(3, &[7], Permission::Read), Some(&leaf), NOW, &p), + Err(DelegationError::NotAuthorized("grant is for another key")) + ); + assert_eq!( + set.authorize(&request(2, &[7], Permission::Read), None, NOW, &p), + Err(DelegationError::NotAuthorized("no grant presented")) + ); +} + +#[test] +fn authority_does_not_pass_on_without_delegate() { + let mut set = DelegationSet::new(); + let mut root = GrantSpec::new(0, 1, None); + root.perms = Permissions::of(&[Permission::Read, Permission::Append]); + let root = admit(&mut set, &root); + let mut leaf = GrantSpec::new(1, 2, Some(root)); + leaf.perms = Permissions::of(&[Permission::Read]); + let leaf = admit(&mut set, &leaf); + assert_eq!( + set.authorize( + &request(2, &[], Permission::Read), + Some(&leaf), + NOW, + &Profile::INITIAL + ), + Err(DelegationError::NotDelegable) + ); +} + +#[test] +fn a_child_cannot_exceed_its_parent() { + let mut set = DelegationSet::new(); + let mut root = GrantSpec::new(0, 1, None); + root.perms = Permissions::of(&[Permission::Read, Permission::Delegate]); + root.scope = scope(&[&[1]]); + let root = admit(&mut set, &root); + + let mut wider_perms = GrantSpec::new(1, 2, Some(root)); + wider_perms.perms = Permissions::of(&[Permission::Read, Permission::Edit]); + wider_perms.scope = scope(&[&[1, 2]]); + let wider_perms = admit(&mut set, &wider_perms); + + let mut wider_scope = GrantSpec::new(1, 2, Some(root)); + wider_scope.perms = Permissions::of(&[Permission::Read]); + wider_scope.scope = scope(&[&[1, 2], &[3]]); + wider_scope.nonce = 2; + let wider_scope = admit(&mut set, &wider_scope); + + let mut not_chained = GrantSpec::new(3, 2, Some(root)); + not_chained.perms = Permissions::of(&[Permission::Read]); + let not_chained = admit(&mut set, ¬_chained); + + let p = Profile::INITIAL; + let read = request(2, &[1, 2], Permission::Read); + assert_eq!( + set.authorize(&read, Some(&wider_perms), NOW, &p), + Err(DelegationError::PermissionExceeded) + ); + assert_eq!( + set.authorize(&read, Some(&wider_scope), NOW, &p), + Err(DelegationError::ScopeExceeded) + ); + assert_eq!( + set.authorize(&read, Some(¬_chained), NOW, &p), + Err(DelegationError::BrokenChain) + ); +} + +#[test] +fn a_root_must_come_from_the_space_key() { + let mut set = DelegationSet::new(); + let root = admit(&mut set, &GrantSpec::new(1, 2, None)); + assert_eq!( + set.authorize( + &request(2, &[], Permission::Read), + Some(&root), + NOW, + &Profile::INITIAL + ), + Err(DelegationError::NotAuthorized( + "root not granted by the space" + )) + ); + let mut elsewhere = GrantSpec::new(5, 2, None); + elsewhere.space = 5; + let elsewhere = admit(&mut set, &elsewhere); + assert_eq!( + set.authorize( + &request(2, &[], Permission::Read), + Some(&elsewhere), + NOW, + &Profile::INITIAL + ), + Err(DelegationError::WrongSpace) + ); +} + +#[test] +fn a_missing_parent_and_a_repeated_grantor_fail_closed() { + let mut set = DelegationSet::new(); + let root_spec = GrantSpec::new(0, 1, None); + let root_id = root_spec.sign().id(HashProfile::RawBlake3_256).unwrap(); + let orphan = admit(&mut set, &GrantSpec::new(1, 2, Some(root_id))); + let p = Profile::INITIAL; + assert_eq!( + set.authorize(&request(2, &[], Permission::Read), Some(&orphan), NOW, &p), + Err(DelegationError::MissingLink) + ); + assert_eq!( + set.authorize(&request(2, &[], Permission::Read), Some(&root_id), NOW, &p), + Err(DelegationError::MissingLink) + ); + + let root = admit(&mut set, &root_spec); + assert_eq!(root, root_id); + let back = admit(&mut set, &GrantSpec::new(1, 2, Some(root))); + let mut again = GrantSpec::new(2, 1, Some(back)); + again.nonce = 9; + let again = admit(&mut set, &again); + let mut leaf = GrantSpec::new(1, 3, Some(again)); + leaf.nonce = 10; + let leaf = admit(&mut set, &leaf); + assert_eq!( + set.authorize(&request(3, &[], Permission::Read), Some(&leaf), NOW, &p), + Err(DelegationError::DelegationCycle) + ); + // A key cannot authorize itself through a grant it issued. + let mut own = GrantSpec::new(2, 2, Some(back)); + own.nonce = 11; + let own = admit(&mut set, &own); + assert_eq!( + set.authorize(&request(2, &[], Permission::Read), Some(&own), NOW, &p), + Err(DelegationError::DelegationCycle) + ); +} + +#[test] +fn the_depth_budget_is_exact() { + let mut set = DelegationSet::new(); + let mut tip = admit(&mut set, &GrantSpec::new(0, 1, None)); + // Keys 1..=16 each grant to the next: 16 grants end at key 16. + for seed in 1..16u8 { + tip = admit(&mut set, &GrantSpec::new(seed, seed + 1, Some(tip))); + } + let p = Profile::INITIAL; + assert_eq!( + set.authorize(&request(16, &[], Permission::Read), Some(&tip), NOW, &p), + Ok(Authority::Delegated { + depth: 16, + stale: false + }) + ); + let deeper = admit(&mut set, &GrantSpec::new(16, 17, Some(tip))); + assert_eq!( + set.authorize(&request(17, &[], Permission::Read), Some(&deeper), NOW, &p), + Err(DelegationError::DelegationChainTooDeep) + ); +} + +#[test] +fn windows_allow_exactly_the_skew() { + let mut set = DelegationSet::new(); + let mut spec = GrantSpec::new(0, 1, None); + spec.window = (1_300, 1_400); + let early = admit(&mut set, &spec); + let p = Profile::INITIAL; + let at = |utc| Clock { + verifier_utc: utc, + frontier_age_secs: 0, + }; + let read = request(1, &[], Permission::Read); + assert_eq!( + set.authorize(&read, Some(&early), at(999), &p), + Err(DelegationError::NotYetValid) + ); + assert!(set.authorize(&read, Some(&early), at(1_000), &p).is_ok()); + assert!(set.authorize(&read, Some(&early), at(1_700), &p).is_ok()); + assert_eq!( + set.authorize(&read, Some(&early), at(1_701), &p), + Err(DelegationError::Expired) + ); +} + +#[test] +fn a_stale_frontier_blocks_writes_and_marks_reads() { + let mut set = DelegationSet::new(); + let root = admit(&mut set, &GrantSpec::new(0, 1, None)); + let p = Profile::INITIAL; + let old = Clock { + verifier_utc: 1_000, + frontier_age_secs: p.max_revocation_staleness_secs + 1, + }; + let edge = Clock { + verifier_utc: 1_000, + frontier_age_secs: p.max_revocation_staleness_secs, + }; + for permission in [ + Permission::Append, + Permission::Edit, + Permission::Moderate, + Permission::Delegate, + ] { + assert_eq!( + set.authorize(&request(1, &[], permission), Some(&root), old, &p), + Err(DelegationError::StaleFrontier) + ); + assert!( + set.authorize(&request(1, &[], permission), Some(&root), edge, &p) + .is_ok() + ); + } + assert_eq!( + set.authorize(&request(1, &[], Permission::Read), Some(&root), old, &p), + Ok(Authority::Delegated { + depth: 1, + stale: true + }) + ); + // The space's own key is never blocked by a stale frontier. + assert_eq!( + set.authorize(&request(0, &[], Permission::Edit), None, old, &p), + Ok(Authority::Owner) + ); +} + +#[test] +fn nonces_replay_idempotently_and_conflict_otherwise() { + let mut set = DelegationSet::new(); + let spec = GrantSpec::new(0, 1, None); + let first = admit(&mut set, &spec); + assert_eq!(set.admit_grant(&spec.sign(), &dispatch()), Ok(first)); + let mut other = GrantSpec::new(0, 2, None); + other.nonce = spec.nonce; + assert_eq!( + set.admit_grant(&other.sign(), &dispatch()), + Err(DelegationError::ReplayConflict) + ); + // The same nonce from another grantor is a separate slot. + let mut elsewhere = GrantSpec::new(1, 2, Some(first)); + elsewhere.nonce = spec.nonce; + assert!(set.admit_grant(&elsewhere.sign(), &dispatch()).is_ok()); + assert_eq!(set.grant(&first), Some(&spec.grant())); +} + +#[test] +fn grants_must_be_signed_by_the_grantor_for_this_purpose() { + let spec = GrantSpec::new(0, 1, None); + assert_eq!( + spec.grant().sign(SIGNER, &key(3)), + Err(DelegationError::WrongSigner) + ); + let payload = spec.grant().payload_bytes().unwrap(); + let mut set = DelegationSet::new(); + let forged = record(GRANT_DOMAIN, "delegation/v1", payload.clone(), &key(3)); + assert_eq!( + set.admit_grant(&forged, &dispatch()), + Err(DelegationError::WrongSigner) + ); + let wrong_domain = record("id/other", "delegation/v1", payload.clone(), &key(0)); + assert_eq!( + set.admit_grant(&wrong_domain, &dispatch()) + .map_err(|e| e.class()), + Err("wrong_domain") + ); + let wrong_profile = record(GRANT_DOMAIN, "delegation/v2", payload.clone(), &key(0)); + assert_eq!( + set.admit_grant(&wrong_profile, &dispatch()) + .map_err(|e| e.class()), + Err("wrong_domain") + ); + let mut tampered = spec.sign(); + tampered.payload[3] ^= 1; + assert_eq!( + set.admit_grant(&tampered, &dispatch()) + .map_err(|e| e.class()), + Err("bad_signature") + ); + let revocation = sign_revocation( + record(GRANT_DOMAIN, "delegation/v1", payload, &key(0)) + .id(HashProfile::RawBlake3_256) + .unwrap(), + SIGNER, + &key(0), + ) + .unwrap(); + assert_eq!( + set.admit_grant(&revocation, &dispatch()) + .map_err(|e| e.class()), + Err("wrong_domain") + ); +} + +fn grant_value(spec: &GrantSpec) -> Vec { + let Value::Array(items) = decode_canonical(&spec.grant().payload_bytes().unwrap()).unwrap() + else { + panic!("grant payload is an array"); + }; + items +} + +fn signed_payload(items: Vec) -> Envelope { + record( + GRANT_DOMAIN, + "delegation/v1", + encode_canonical(&Value::Array(items)).unwrap(), + &key(0), + ) +} + +#[test] +fn grant_payloads_are_canonical_and_bounded() { + let mut spec = GrantSpec::new(0, 1, None); + spec.scope = scope(&[&[2], &[1, 5], &[1, 5]]); + let grant = spec.grant(); + assert_eq!(grant.scope.prefixes().len(), 2); + assert_eq!( + Grant::from_payload(&grant.payload_bytes().unwrap()), + Ok(grant) + ); + + let malformed = |items: Vec| { + let mut set = DelegationSet::new(); + set.admit_grant(&signed_payload(items), &dispatch()) + .map(|_| ()) + .map_err(|e| e.class()) + }; + // Scope prefixes out of canonical order. + let mut items = grant_value(&spec); + let Value::Array(prefixes) = &mut items[4] else { + panic!("scope is an array"); + }; + prefixes.reverse(); + assert_eq!(malformed(items), Err("malformed")); + // Unknown permission bits. + let mut items = grant_value(&spec); + items[5] = Value::UInt(32); + assert_eq!(malformed(items), Err("malformed")); + let mut items = grant_value(&spec); + items[5] = Value::UInt(256 + 1); + assert_eq!(malformed(items), Err("malformed")); + // A window that ends before it starts. + let mut items = grant_value(&spec); + items[6] = Value::UInt(5); + items[7] = Value::UInt(4); + assert_eq!(malformed(items), Err("malformed")); + // A one-second window is fine. + assert_eq!(window_ok(&spec, 4, 4), Ok(())); + // Empty, oversize and non-bytes nonces. + for nonce in [ + Value::Bytes(vec![]), + Value::Bytes(vec![0; 65]), + Value::UInt(1), + ] { + let mut items = grant_value(&spec); + items[8] = nonce; + assert_eq!(malformed(items), Err("malformed")); + } + let mut items = grant_value(&spec); + items[8] = Value::Bytes(vec![0; 64]); + assert_eq!(malformed(items), Ok(())); + // Version, length, keys, space and predecessor shapes. + let mut items = grant_value(&spec); + items[0] = Value::UInt(2); + assert_eq!(malformed(items), Err("malformed")); + let mut items = grant_value(&spec); + items.push(Value::Null); + assert_eq!(malformed(items), Err("malformed")); + let mut items = grant_value(&spec); + items[3] = Value::Bytes(vec![1; 31]); + assert_eq!(malformed(items), Err("malformed")); + let mut items = grant_value(&spec); + items[1] = Value::Bytes(vec![1; 8]); + assert_eq!(malformed(items), Err("malformed")); + let mut items = grant_value(&spec); + items[9] = Value::Bytes(vec![1; 8]); + assert_eq!(malformed(items), Err("malformed")); + assert_eq!( + Scope::new(vec![vec![step(0)]; 65]).map_err(|e| e.class()), + Err("malformed") + ); + let most: Vec> = (0..64u8).map(|i| vec![step(i)]).collect(); + assert_eq!(Scope::new(most).unwrap().prefixes().len(), 64); +} + +fn window_ok(spec: &GrantSpec, nb: u64, exp: u64) -> Result<(), &'static str> { + let mut items = grant_value(spec); + items[6] = Value::UInt(nb); + items[7] = Value::UInt(exp); + let mut set = DelegationSet::new(); + set.admit_grant(&signed_payload(items), &dispatch()) + .map(|_| ()) + .map_err(|e| e.class()) +} + +#[test] +fn revocations_need_the_grantor_or_the_space_key() { + let mut set = DelegationSet::new(); + let root = admit(&mut set, &GrantSpec::new(0, 1, None)); + let leaf = admit(&mut set, &GrantSpec::new(1, 2, Some(root))); + let mut sibling = GrantSpec::new(1, 3, Some(root)); + sibling.nonce = 2; + let sibling = admit(&mut set, &sibling); + let p = Profile::INITIAL; + let read = |seed| request(seed, &[], Permission::Read); + + let by_stranger = sign_revocation(leaf, SIGNER, &key(4)).unwrap(); + assert_eq!( + set.admit_revocation(&by_stranger, &dispatch()), + Err(DelegationError::WrongSigner) + ); + let by_grantee = sign_revocation(leaf, SIGNER, &key(2)).unwrap(); + assert_eq!( + set.admit_revocation(&by_grantee, &dispatch()), + Err(DelegationError::WrongSigner) + ); + let unknown = sign_revocation( + GrantSpec::new(0, 4, None) + .sign() + .id(HashProfile::RawBlake3_256) + .unwrap(), + SIGNER, + &key(0), + ) + .unwrap(); + assert_eq!( + set.admit_revocation(&unknown, &dispatch()), + Err(DelegationError::UnknownGrant) + ); + assert!(set.authorize(&read(2), Some(&leaf), NOW, &p).is_ok()); + + let by_grantor = sign_revocation(leaf, SIGNER, &key(1)).unwrap(); + assert_eq!(set.admit_revocation(&by_grantor, &dispatch()), Ok(leaf)); + assert_eq!( + set.authorize(&read(2), Some(&leaf), NOW, &p), + Err(DelegationError::Revoked) + ); + assert!(set.authorize(&read(3), Some(&sibling), NOW, &p).is_ok()); + + // The space's key revokes a grant it did not issue directly, and + // revoking a parent ends every chain under it. + let by_owner = sign_revocation(sibling, SIGNER, &key(0)).unwrap(); + assert_eq!(set.admit_revocation(&by_owner, &dispatch()), Ok(sibling)); + let by_owner_root = sign_revocation(root, SIGNER, &key(0)).unwrap(); + assert_eq!(set.admit_revocation(&by_owner_root, &dispatch()), Ok(root)); + assert_eq!( + set.authorize(&read(1), Some(&root), NOW, &p), + Err(DelegationError::Revoked) + ); + + let grant_as_revocation = GrantSpec::new(0, 1, None).sign(); + assert_eq!( + set.admit_revocation(&grant_as_revocation, &dispatch()) + .map_err(|e| e.class()), + Err("wrong_domain") + ); + let bad_payload = record( + REVOCATION_DOMAIN, + "delegation/v1", + encode_canonical(&Value::Array(vec![ + Value::UInt(1), + Value::Bytes(vec![0; 4]), + ])) + .unwrap(), + &key(0), + ); + assert_eq!( + set.admit_revocation(&bad_payload, &dispatch()) + .map_err(|e| e.class()), + Err("malformed") + ); +} + +fn statement() -> TransferStatement { + TransferStatement { + space: space(0), + old_key: public(0), + new_key: public(1), + scope: scope(&[&[]]), + nonce: vec![7], + challenge: b"challenge".to_vec(), + expires: 1_100, + } +} + +#[test] +fn a_transfer_needs_both_keys_over_identical_bytes() { + let s = statement(); + let offer = s.sign_offer(SIGNER, &key(0)).unwrap(); + let accept = s.sign_acceptance(SIGNER, &key(1)).unwrap(); + let p = Profile::INITIAL; + let d = dispatch(); + let verified = verify_transfer(&offer, &accept, &public(0), NOW, &p, &d).unwrap(); + assert_eq!(verified, s); + // The address stays the one the old key named. + assert_eq!(verified.space, space(0)); + assert_eq!(verified.space.key(), &public(0)); + + assert_eq!( + s.sign_offer(SIGNER, &key(1)), + Err(DelegationError::WrongSigner) + ); + assert_eq!( + s.sign_acceptance(SIGNER, &key(0)), + Err(DelegationError::WrongSigner) + ); + // The new key alone, or the old key alone, is not enough. + let forged_offer = record( + TRANSFER_OFFER_DOMAIN, + "delegation/v1", + s.payload_bytes().unwrap(), + &key(1), + ); + assert_eq!( + verify_transfer(&forged_offer, &accept, &public(0), NOW, &p, &d), + Err(DelegationError::WrongSigner) + ); + let forged_accept = record( + TRANSFER_ACCEPT_DOMAIN, + "delegation/v1", + s.payload_bytes().unwrap(), + &key(0), + ); + assert_eq!( + verify_transfer(&offer, &forged_accept, &public(0), NOW, &p, &d), + Err(DelegationError::WrongSigner) + ); + // The offer and the acceptance are not interchangeable. + assert_eq!( + verify_transfer(&accept, &offer, &public(0), NOW, &p, &d).map_err(|e| e.class()), + Err("wrong_domain") + ); + // Any differing field breaks the pairing. + for change in 0..5 { + let mut other = s.clone(); + match change { + 0 => other.challenge = b"other".to_vec(), + 1 => other.nonce = vec![8], + 2 => other.expires += 1, + 3 => other.scope = scope(&[&[1]]), + _ => other.space = space(2), + } + let accept = other.sign_acceptance(SIGNER, &key(1)).unwrap(); + assert_eq!( + verify_transfer(&offer, &accept, &public(0), NOW, &p, &d), + Err(DelegationError::TransferMismatch) + ); + } + // Only the current controller can transfer. + assert_eq!( + verify_transfer(&offer, &accept, &public(2), NOW, &p, &d), + Err(DelegationError::WrongSigner) + ); + // Expiry honours the skew allowance exactly. + let late = |utc| Clock { + verifier_utc: utc, + frontier_age_secs: 0, + }; + assert!(verify_transfer(&offer, &accept, &public(0), late(1_400), &p, &d).is_ok()); + assert_eq!( + verify_transfer(&offer, &accept, &public(0), late(1_401), &p, &d), + Err(DelegationError::Expired) + ); + // A transfer to the same key is refused. + let mut same = s; + same.new_key = public(0); + let offer = same.sign_offer(SIGNER, &key(0)).unwrap(); + let accept = same.sign_acceptance(SIGNER, &key(0)).unwrap(); + assert_eq!( + verify_transfer(&offer, &accept, &public(0), NOW, &p, &d), + Err(DelegationError::SameKey) + ); +} + +#[test] +fn transfer_payloads_are_checked() { + let s = statement(); + let Value::Array(items) = decode_canonical(&s.payload_bytes().unwrap()).unwrap() else { + panic!("statement is an array"); + }; + let p = Profile::INITIAL; + let check = |items: Vec| { + let bytes = encode_canonical(&Value::Array(items)).unwrap(); + let offer = record( + TRANSFER_OFFER_DOMAIN, + "delegation/v1", + bytes.clone(), + &key(0), + ); + let accept = record(TRANSFER_ACCEPT_DOMAIN, "delegation/v1", bytes, &key(1)); + verify_transfer(&offer, &accept, &public(0), NOW, &p, &dispatch()).map_err(|e| e.class()) + }; + assert!(check(items.clone()).is_ok()); + let mut purpose = items.clone(); + purpose[1] = Value::Text("id/other".to_owned()); + assert_eq!(check(purpose), Err("malformed")); + let mut challenge = items.clone(); + challenge[7] = Value::UInt(1); + assert_eq!(check(challenge), Err("malformed")); + let mut short = items; + short.pop(); + assert_eq!(check(short), Err("malformed")); +} + +#[test] +fn error_classes_are_stable() { + let all = [ + (DelegationError::Malformed("x"), "malformed"), + (DelegationError::WrongDomain(String::new()), "wrong_domain"), + (DelegationError::WrongSigner, "wrong_signer"), + (DelegationError::ReplayConflict, "replay_conflict"), + (DelegationError::UnknownGrant, "unknown_grant"), + (DelegationError::NotAuthorized("x"), "not_authorized"), + (DelegationError::WrongSpace, "wrong_space"), + (DelegationError::Revoked, "revoked"), + (DelegationError::NotYetValid, "not_yet_valid"), + (DelegationError::Expired, "expired"), + (DelegationError::BrokenChain, "broken_chain"), + (DelegationError::NotDelegable, "not_delegable"), + (DelegationError::PermissionExceeded, "permission_exceeded"), + (DelegationError::ScopeExceeded, "scope_exceeded"), + (DelegationError::MissingLink, "missing_link"), + (DelegationError::DelegationCycle, "delegation_cycle"), + ( + DelegationError::DelegationChainTooDeep, + "delegation_chain_too_deep", + ), + (DelegationError::StaleFrontier, "stale_frontier"), + (DelegationError::TransferMismatch, "transfer_mismatch"), + (DelegationError::SameKey, "same_key"), + ]; + let mut seen = HashSet::new(); + for (error, class) in all { + assert_eq!(error.class(), class); + assert!(seen.insert(class)); + assert!(!error.to_string().is_empty()); + } +} + +// --------------------------------------------------------------------- +// Independent model: grants are plain records over key seeds, scopes are +// sets of byte-string prefixes, and permissions are bitmasks. + +const OWNER: u8 = 0; +const KEYS: u8 = 5; + +#[derive(Debug, Clone)] +struct MGrant { + other_space: bool, + grantor: u8, + grantee: u8, + prefixes: Vec>, + perms: u8, + nb: u64, + exp: u64, + pred: Option, + admitted: bool, + revoked: bool, +} + +#[derive(Debug, Clone)] +struct MRequest { + key: u8, + leaf: Option, + perm: u8, + path: Vec, + utc: u64, + age: u64, + max_depth: usize, + skew: u64, + stale_after: u64, +} + +const READ: u8 = 1; +const DELEGATE: u8 = 16; + +fn m_covers(prefixes: &[Vec], path: &[u8]) -> bool { + prefixes + .iter() + .any(|p| p.len() <= path.len() && path[..p.len()] == p[..]) +} + +/// The model's decision, as `Ok((depth, stale))` or a failure class. +fn model(grants: &[MGrant], r: &MRequest) -> Result, &'static str> { + if r.key == OWNER { + return Ok(None); + } + let stale = r.age > r.stale_after; + if stale && r.perm != READ { + return Err("stale_frontier"); + } + let Some(leaf) = r.leaf else { + return Err("not_authorized"); + }; + // The grant indices from leaf to root. + let mut chain = vec![leaf]; + while let Some(pred) = grants[*chain.last().unwrap()].pred { + chain.push(pred); + } + let mut used: Vec = vec![r.key]; + for (pos, &i) in chain.iter().enumerate() { + let g = &grants[i]; + if !g.admitted { + return Err("missing_link"); + } + if pos >= r.max_depth { + return Err("delegation_chain_too_deep"); + } + if used.contains(&g.grantor) { + return Err("delegation_cycle"); + } + used.push(g.grantor); + if g.other_space { + return Err("wrong_space"); + } + if g.revoked { + return Err("revoked"); + } + if g.nb > r.utc + r.skew { + return Err("not_yet_valid"); + } + if g.exp + r.skew < r.utc { + return Err("expired"); + } + if pos == 0 { + if g.grantee != r.key || g.perms & r.perm == 0 || !m_covers(&g.prefixes, &r.path) { + return Err("not_authorized"); + } + } else { + let c = &grants[chain[pos - 1]]; + if g.grantee != c.grantor { + return Err("broken_chain"); + } + if g.perms & DELEGATE == 0 { + return Err("not_delegable"); + } + if c.perms & !g.perms != 0 { + return Err("permission_exceeded"); + } + if !c.prefixes.iter().all(|p| m_covers(&g.prefixes, p)) { + return Err("scope_exceeded"); + } + } + } + let root = &grants[*chain.last().unwrap()]; + if root.grantor == OWNER { + Ok(Some((chain.len(), stale))) + } else { + Err("not_authorized") + } +} + +fn prefix() -> impl Strategy> { + prop::collection::vec(0..2u8, 0..3) +} + +fn m_grant(index: usize) -> impl Strategy { + ( + prop::bool::weighted(0.05), + prop_oneof![3 => Just(OWNER), 7 => 1..KEYS], + 0..KEYS, + prop::collection::vec(prefix(), 0..3), + prop_oneof![4 => Just(31u8), 6 => 0..32u8], + (0..20u64, 0..20u64), + prop_oneof![ + 1 => Just(None), + if index == 0 { 0 } else { 4 } => (0..index.max(1)).prop_map(Some), + ], + prop::bool::weighted(0.9), + prop::bool::weighted(0.08), + ) + .prop_map( + |(other_space, grantor, grantee, prefixes, perms, (a, b), pred, admitted, revoked)| { + MGrant { + other_space, + grantor, + grantee, + prefixes, + perms, + nb: a.min(b), + exp: a.max(b), + pred, + admitted, + revoked, + } + }, + ) +} + +fn forest() -> impl Strategy> { + (1..9usize).prop_flat_map(|n| (0..n).map(m_grant).collect::>()) +} + +fn m_request(n: usize) -> impl Strategy { + ( + prop_oneof![1 => Just(OWNER), 9 => 1..KEYS], + prop_oneof![1 => Just(None), 9 => (0..n).prop_map(Some)], + prop::sample::select(vec![1u8, 2, 4, 8, 16]), + prop::collection::vec(0..2u8, 0..4), + 0..25u64, + prop_oneof![9 => Just(0u64), 1 => 0..20u64], + 1..6usize, + 0..3u64, + 5..15u64, + ) + .prop_map( + |(key, leaf, perm, path, utc, age, max_depth, skew, stale_after)| MRequest { + key, + leaf, + perm, + path, + utc, + age, + max_depth, + skew, + stale_after, + }, + ) +} + +const fn permission(bit: u8) -> Permission { + match bit { + 1 => Permission::Read, + 2 => Permission::Append, + 4 => Permission::Edit, + 8 => Permission::Moderate, + _ => Permission::Delegate, + } +} + +/// Builds the module's set from the model's records. +fn realize(grants: &[MGrant]) -> (DelegationSet, Vec) { + let mut set = DelegationSet::new(); + let mut ids: Vec = Vec::new(); + for (i, g) in grants.iter().enumerate() { + let spec = GrantSpec { + space: if g.other_space { KEYS } else { OWNER }, + grantor: g.grantor, + grantee: g.grantee, + scope: Scope::new(g.prefixes.iter().map(|p| path(p)).collect()).unwrap(), + perms: Permissions::from_bits(g.perms).unwrap(), + window: (g.nb, g.exp), + nonce: i as u8, + pred: g.pred.map(|p| ids[p]), + }; + let envelope = spec.sign(); + let id = envelope.id(HashProfile::RawBlake3_256).unwrap(); + if g.admitted { + assert_eq!(set.admit_grant(&envelope, &dispatch()), Ok(id)); + } + ids.push(id); + } + for (g, id) in grants.iter().zip(&ids) { + if g.admitted && g.revoked { + let revocation = sign_revocation(*id, SIGNER, &key(g.grantor)).unwrap(); + assert_eq!(set.admit_revocation(&revocation, &dispatch()), Ok(*id)); + } + } + (set, ids) +} + +proptest! { + #![proptest_config(ProptestConfig::with_cases(512))] + + #[test] + fn authorization_agrees_with_the_model( + (grants, requests) in forest().prop_flat_map(|g| { + let n = g.len(); + (Just(g), prop::collection::vec(m_request(n), 1..6)) + }) + ) { + let mut grants = grants; + // A revocation can only name an admitted grant. + for g in &mut grants { + g.revoked &= g.admitted; + } + let (set, ids) = realize(&grants); + for r in &requests { + let request = Request { + space: space(OWNER), + key: public(r.key), + path: path(&r.path), + permission: permission(r.perm), + }; + let clock = Clock { verifier_utc: r.utc, frontier_age_secs: r.age }; + let profile = Profile { + max_depth: r.max_depth, + clock_skew_secs: r.skew, + max_revocation_staleness_secs: r.stale_after, + }; + let got = set + .authorize(&request, r.leaf.map(|i| &ids[i]), clock, &profile) + .map(|a| match a { + Authority::Owner => None, + Authority::Delegated { depth, stale } => Some((depth, stale)), + }) + .map_err(|e| e.class()); + prop_assert_eq!(got, model(&grants, r), "request {:?}", r); + } + } + + #[test] + fn scope_containment_agrees_with_the_model( + parent in prop::collection::vec(prefix(), 0..4), + child in prop::collection::vec(prefix(), 0..4), + probe in prop::collection::vec(0..2u8, 0..4), + ) { + let p = Scope::new(parent.iter().map(|x| path(x)).collect()).unwrap(); + let c = Scope::new(child.iter().map(|x| path(x)).collect()).unwrap(); + prop_assert_eq!(p.covers_path(&path(&probe)), m_covers(&parent, &probe)); + let contained = child.iter().all(|x| m_covers(&parent, x)); + prop_assert_eq!(p.contains(&c), contained); + // Containment means every path the child covers, the parent covers. + if contained && m_covers(&child, &probe) { + prop_assert!(m_covers(&parent, &probe)); + } + } + + #[test] + fn grants_round_trip_through_their_payload( + g in m_grant(0), + nonce in prop::collection::vec(any::(), 1..=64), + ) { + let grant = Grant { + space: space(OWNER), + grantor_key: public(g.grantor), + grantee_key: public(g.grantee), + scope: Scope::new(g.prefixes.iter().map(|p| path(p)).collect()).unwrap(), + permissions: Permissions::from_bits(g.perms).unwrap(), + not_before: g.nb, + expires: g.exp, + nonce, + predecessor: None, + }; + let bytes = grant.payload_bytes().unwrap(); + prop_assert_eq!(Grant::from_payload(&bytes), Ok(grant)); + } +}