// Where Cotton chat's secret lives on this phone: one item in the keychain// group the app shares with its notification extension.//// - `key`: this phone's CHAT private key (32 bytes, base64url as the core// writes it), minted by `chat.key`. Never the vault key: the vault's is read// behind Face ID, and the extension opening a message on a locked phone// could never ask for that (`crates/cotton-ffi/src/phone/cotton_chat.rs`).//// The extension reaches no server: the push carries the box it opens. The// device's credential stays in the core.//// It is `AfterFirstUnlockThisDeviceOnly`: readable by the extension while// the phone is locked once it has been unlocked since boot, never synced,// never backed up off the phone. Sign-out forgets it. Nothing here logs.importFoundationimportSecurity/// Where the chat key is kept: the shared keychain group on a/// phone, memory in a test.publicprotocolChatSecrets: Sendable {func key() -> String?func saveKey(_ key: String) throwsfunc forget()}/// Why the keychain refused, by its OSStatus.structChatKeychainError: Error, Equatable, Sendable {publiclet status: OSStatus}/// The shared keychain group's chat items.publicstructChatKeychain: ChatSecrets {publicstaticlet service = "cotton.chat"privatestaticlet keyAccount = "key"privatelet accessGroup: String?/// `accessGroup` nil files the items where the system does (a test, an/// unsigned build); a shipping build names the shared group.publicinit(accessGroup: String? = PasskeyKeychain.sharedGroup()) {self.accessGroup = accessGroup }publicfunc key() -> String? { read(Self.keyAccount).map { String(decoding: $0, as: UTF8.self) } }publicfunc saveKey(_ key: String) throws {try write(Self.keyAccount, Data(key.utf8)) }publicfunc forget() {var query = base() query[kSecAttrAccount asString] = Self.keyAccountSecItemDelete(query as CFDictionary) }privatefunc base() -> [String: Any] {var query: [String: Any] = [ kSecClass asString: kSecClassGenericPassword, kSecAttrService asString: Self.service, ]iflet accessGroup { query[kSecAttrAccessGroup asString] = accessGroup }return query }privatefunc read(_ account: String) -> Data? {var query = base() query[kSecAttrAccount asString] = account query[kSecReturnData asString] = true query[kSecMatchLimit asString] = kSecMatchLimitOnevar found: CFTypeRef?guardSecItemCopyMatching(query as CFDictionary, &found) == errSecSuccess else { returnnil }return found as? Data }privatefunc write(_ account: String, _ data: Data) throws {var query = base() query[kSecAttrAccount asString] = accountSecItemDelete(query as CFDictionary) query[kSecValueData asString] = data query[kSecAttrAccessible asString] = kSecAttrAccessibleAfterFirstUnlockThisDeviceOnly query[kSecAttrSynchronizable asString] = falselet status = SecItemAdd(query as CFDictionary, nil)guard status == errSecSuccess else { throwChatKeychainError(status: status) } }}/// Chat secrets in memory: a test's, never a phone's.publicfinalclassMemoryChatSecrets: ChatSecrets, @unchecked Sendable {privatelet lock = NSLock()privatevar heldKey: String?publicinit(key: String? = nil) { heldKey = key }publicfunc key() -> String? { lock.withLock { heldKey } }publicfunc saveKey(_ key: String) throws { lock.withLock { heldKey = key } }publicfunc forget() { lock.withLock { heldKey = nil } }}