// The two halves of this device's vault key, and who may ask for which. // // `VaultKey` is the Keychain item. This is the RULE about when it is touched, // and it exists because the two halves have completely different costs: // // * the PUBLIC half is not a secret. The server needs it to wrap the // passwords' master key for this device, it travels in the join and in // `vault.publishKey`, and asking for it must cost nothing. // * the PRIVATE half is guarded by presence. ``privateKey()`` asks Face ID, // Touch ID or the passcode first (`VaultPresence`; the item itself has // carried no access control since 16 Nov 2026, see `VaultKey.swift`): a // person tapping Reveal, Approve, Allow, or saving a secret they are about // to seal. ``quietPrivateKey()`` asks nothing, and is called only where a // person must not be asked (R7, R8): the first sign-in's founding, the // re-open once another device approved this one, a recovery code's or a // trusted person's open, the iCloud Keychain copy. // // **A launch must never prompt.** So the public half is CACHED beside the // private one, in an ordinary Keychain slot with no access control, written at // the instant the key is minted. A launch then publishes from the cache, and // the private item is not touched at all. The cache holds a public key: losing // it leaks nothing, and finding it tampered with costs nothing either, because // the server wraps for whatever public key the row carries and a wrap this // device cannot open is simply a `vault_key_mismatch` the next unlock names. // // **Minting does not prompt either.** So a phone mints, caches, publishes and // founds and says nothing — and the first prompt the person ever sees is the // one in front of their first reveal, Approve or Allow. import Foundation /// What a device's vault key looks like from outside the Keychain. public struct VaultKeyring: Sendable { /// The presence-guarded private half. private let keys: any VaultKeyStore /// The ordinary slot the public half is cached in. private let cache: any SecretStore private let slot: String /// The account name the public half is cached under, beside `vault-key`. public static let publicSlot = "vault-public-key" /// **The cache is filed beside the PRIVATE half, not beside the phone's /// device key.** `VaultKey.service` is the bundle id on the phone and the /// Mac app's own service on the Mac (Cotton note "The vault — the honest sentence"), and the /// two halves of one key belong under one service: filed apart, the Mac /// asks the system for an item in the phone's service, which on a re-signed /// build is a login-password dialog in front of the Passwords screens and /// on any build is a second place to keep in step. /// Asked before every ``privateKey()``: the phone's own lock. private let presence: @Sendable () throws -> Void public init( keys: any VaultKeyStore = KeychainVaultKeyStore(), cache: any SecretStore = KeychainSecretStore(service: VaultKey.service), slot: String = VaultKeyring.publicSlot, presence: @escaping @Sendable () throws -> Void = { try VaultPresence.ask() } ) { self.keys = keys self.cache = cache self.slot = slot self.presence = presence } /// This device's PUBLIC vault key, minting the pair the first time. /// /// **Never prompts.** A key that exists is reported from the cache; one /// that does not is minted, and the public half is derived from the key in /// hand rather than by reading it back. /// /// A private item that exists with NO cached public half is the one state /// this cannot answer without a prompt — it can only happen if the cache /// was cleared under a key that was not — so it answers `nil` rather than /// prompting, and the reveal path publishes on its way through. `nil` is a /// state the caller draws, never a mint: minting over the item would /// orphan a wrap the server has already written. public func publicKey() throws -> String? { if let minted = try VaultKey.mintIfAbsent(store: keys) { guard let published = VaultKey.publicHalf(of: minted) else { throw VaultKeyError.slotUnreadable } cache.write(published, to: slot) return published } return cache.read(slot) } /// The PRIVATE half — **this prompts.** Call it from a reveal or an explicit act that signs membership, /// and never on the main thread. /// /// It refreshes the public cache on the way past, which is what repairs the /// one state `publicKey()` cannot: by here the key is in hand anyway. public func privateKey() throws -> String { try presence() return try quietPrivateKey() } /// The PRIVATE half with nobody asked — **only** where a person must not /// be (see the header): founding at sign-in, the re-open after an /// approval, a code's or a helper's open, the iCloud Keychain copy. public func quietPrivateKey() throws -> String { let key = try VaultKey.key(store: keys) if let published = VaultKey.publicHalf(of: key) { cache.write(published, to: slot) } return key } /// Start this phone's vault over — both halves, so the next launch mints /// and publishes rather than reporting a public key with no private one. public func forget() { VaultKey.forget(store: keys) cache.forget(slot) } }