//! The standing grant to the person's own Claude (owner's //! decision 20 Nov 2026: connecting Claude is the grant; no timer, no card per //! token). The keys are wrapped to a MEMBER key, an X25519 pair whose private //! half the object keeps only inside the connection's tokens (a carrier per //! token, sealed under a key derived from the token string, which the object //! never stores); so at rest the object holds nothing that opens a granted //! table. //! //! ```text //! bundle = the unlock bundle (grant.rs) with request_id = token_id = member_id //! wrapped = w1(member_pub, bundle) //! statement = "cotton-agent-grant-v1\n" database_id "\n" member_id "\n" b64url(member_pub) "\n" //! sorted(withheld) joined "," "\n" hex(sha256(wrapped)) //! sig = b64url Ed25519(person key, statement) //! ``` //! //! The statement signs the POLICY ("every sealed table except `withheld`") and //! the member key it is granted to, never a table list: a table the kit adds //! later, or a table's next key version, is inside the policy, and the phone //! adds its keys on its own as an unsigned [`addition`] (a second wrap to the //! same member key). Widening the policy (a withheld table turned back on) is //! signed again. [`NEVER`] is never inside it: saved secrets are not served to //! assistants. use rand::RngCore; use zeroize::Zeroizing; use super::grant::{bundle, bundle_json, parse_bundle, GrantBundle}; use super::keys::DataKey; use super::{b64, plain_field, sha256_hex, unb64, SealError, NONCE_LEN}; use crate::keys::person::{self, PersonKey}; use crate::vault::wrap::{unwrap, wrap_with}; pub const AGENT_CONTEXT: &str = "cotton-agent-grant-v1"; /// The tables no standing grant covers: saved passwords and passkeys and the /// code runner's credentials. A read of one still files today's timed card. pub const NEVER: [&str; 3] = ["code_credential", "passkey", "secret"]; /// The tables no assistant is ever given, not in a standing grant and not in a timed window, whatever the person /// picks: Cotton Sites' list (`site`: it says where he banks; assets/design/sites/DECISIONS.md question 14, /// "Assistants never see Sites"). Unlike [`NEVER`], no card offers them. The phone and the Mac hand no key for one, /// so the object never holds a key an assistant could open one with. pub const CLOSED: [&str; 1] = ["site"]; /// Whether `table` is one no assistant is ever given (`CLOSED`). pub fn closed(table: &str) -> bool { CLOSED.contains(&table) } /// What the phone posts for a member: the policy, the wrap and the signature. #[derive(Debug, Clone, PartialEq, Eq)] pub struct AgentGrant { /// Sorted, de-duplicated; every [`NEVER`] table among them. pub withheld: Vec, pub wrapped: String, pub signer: String, pub sig: String, } /// `withheld` with [`NEVER`] added, sorted, de-duplicated, each checked. pub fn policy(withheld: impl IntoIterator>) -> Result, SealError> { let mut out: Vec = NEVER.iter().map(|t| t.to_string()).collect(); for table in withheld { plain_field("table", table.as_ref())?; out.push(table.as_ref().to_string()); } out.sort(); out.dedup(); Ok(out) } /// The bytes the person key signs. `withheld` must already be a [`policy`]. pub fn agent_statement(database_id: &str, member_id: &str, member_pub: &[u8; 32], withheld: &[String], wrapped: &str) -> Result, SealError> { plain_field("database", database_id)?; plain_field("member", member_id)?; let said = policy(withheld)?; if said != withheld { return Err(SealError::BadInput("withheld is not sorted, once each, with every never-granted table".into())); } Ok(format!("{AGENT_CONTEXT}\n{database_id}\n{member_id}\n{}\n{}\n{}", b64(member_pub), said.join(","), sha256_hex(wrapped.as_bytes())).into_bytes()) } fn check_inside(withheld: &[String], tables: &[(String, u32)]) -> Result<(), SealError> { if tables.is_empty() { return Err(SealError::BadInput("a grant names at least one table".into())); } for (table, _) in tables { plain_field("table", table)?; if withheld.contains(table) { return Err(SealError::BadInput(format!("{table} is withheld from the grant"))); } } Ok(()) } fn randomness() -> (Zeroizing<[u8; 32]>, [u8; NONCE_LEN]) { let mut ephemeral = Zeroizing::new([0u8; 32]); let mut nonce = [0u8; NONCE_LEN]; rand::rngs::OsRng.fill_bytes(ephemeral.as_mut_slice()); rand::rngs::OsRng.fill_bytes(&mut nonce); (ephemeral, nonce) } /// The grant on the phone: the tables' keys (every version up to each current /// one) wrapped to the member key, and the policy signed. pub fn build_agent_grant(dk: &DataKey, database_id: &str, member_id: &str, member_pub: &[u8; 32], tables: &[(String, u32)], withheld: &[String], person: &PersonKey) -> Result { let (ephemeral, nonce) = randomness(); build_agent_grant_with(&ephemeral, &nonce, dk, database_id, member_id, member_pub, tables, withheld, person) } /// [`build_agent_grant`] with the randomness supplied. #[allow(clippy::too_many_arguments)] pub fn build_agent_grant_with(ephemeral: &[u8; 32], nonce: &[u8; NONCE_LEN], dk: &DataKey, database_id: &str, member_id: &str, member_pub: &[u8; 32], tables: &[(String, u32)], withheld: &[String], person: &PersonKey) -> Result { let withheld = policy(withheld)?; check_inside(&withheld, tables)?; let keys = bundle(dk, database_id, member_id, member_id, tables)?; let wrapped = wrap_with(ephemeral, nonce, member_pub, bundle_json(&keys).as_bytes()).ok_or(SealError::BadInput("member_pub is not a usable X25519 key".into()))?; let statement = agent_statement(database_id, member_id, member_pub, &withheld, &wrapped)?; Ok(AgentGrant { withheld, wrapped, signer: person.fingerprint(), sig: b64(&person.sign(&statement)) }) } /// An unsigned addition inside a signed policy: more tables, or more versions, /// wrapped to the same member key. pub fn addition(dk: &DataKey, database_id: &str, member_id: &str, member_pub: &[u8; 32], tables: &[(String, u32)], withheld: &[String]) -> Result { let withheld = policy(withheld)?; check_inside(&withheld, tables)?; let keys = bundle(dk, database_id, member_id, member_id, tables)?; let (ephemeral, nonce) = randomness(); wrap_with(&ephemeral, &nonce, member_pub, bundle_json(&keys).as_bytes()).ok_or(SealError::BadInput("member_pub is not a usable X25519 key".into())) } /// Does the person key sign this policy and wrap for this member key? pub fn verify_agent_grant(person_public_key: &str, database_id: &str, member_id: &str, member_pub: &[u8; 32], grant: &AgentGrant) -> bool { if person::fingerprint(person_public_key).as_deref() != Some(grant.signer.as_str()) { return false; } let Ok(statement) = agent_statement(database_id, member_id, member_pub, &grant.withheld, &grant.wrapped) else { return false; }; let Some(sig) = unb64(&grant.sig) else { return false; }; person::verify(person_public_key, &statement, &sig) } /// Open one wrap with the member's private half (on the object, while it /// answers a call that presented a token carrying it). Refused unless it names /// this member and holds no withheld table. pub fn open_agent_wrap(member_secret: &[u8; 32], member_id: &str, withheld: &[String], wrapped: &str) -> Result { let text = Zeroizing::new(unwrap(member_secret, wrapped).ok_or(SealError::DoesNotOpen)?); let opened = parse_bundle(&text)?; if opened.request_id != member_id || opened.token_id != member_id { return Err(SealError::DoesNotOpen); } if opened.keys.keys().any(|t| withheld.contains(t)) || opened.keys.keys().ne(opened.blind.keys()) { return Err(SealError::DoesNotOpen); } Ok(opened) } #[cfg(test)] #[path = "agent_tests.rs"] mod tests;