// THE iCLOUD KEYCHAIN COPY (e2e rulings R7a, 16 Nov // 2026). A phone holding the vault asks the core for a platform key // (`recovery.makePlatform { store: "apple" }`): 32 random bytes the object // keeps only a hash of, under which it keeps a wrap of the vault key. This // file keeps those bytes as ONE SYNCHRONIZABLE Keychain item, so iCloud // Keychain (end-to-end encrypted) carries it to every iPhone and Mac on the // same Apple account; a new iPhone that signs in reads it and opens with no // step (`recovery.openPlatform`). // // * `kSecAttrSynchronizable = true`, and so `AfterFirstUnlock` — a // synchronizable item cannot be `ThisDeviceOnly`. // * Service `com.cottondb.recovery`, account `apple-`, in the // keychain group every Cotton app shares (`VaultKey.accessGroup`), so // any Cotton app keeps it and any reads it. // * A search names `kSecAttrSynchronizable` on purpose: without it a query // matches only items that do not sync, and the copy would read as gone. // // What a simulator can prove: the item is written and read back, and the // object keeps the `apple` wrap. What it cannot: the sync itself, which needs // an Apple account with iCloud Keychain on and a second device. import Foundation import Security /// Where the platform key is kept. public protocol PlatformKeyKeeping: Sendable { func read(database: String) -> String? @discardableResult func write(_ key: String, database: String) -> Bool } /// The iCloud Keychain copy. public struct ICloudPlatformKeys: PlatformKeyKeeping { public static let service = "com.cottondb.recovery" private let accessGroup: String? public init(accessGroup: String? = VaultKey.accessGroup) { self.accessGroup = accessGroup } static func account(_ database: String) -> String { "apple-\(database)" } public func read(database: String) -> String? { var query = base(database) query[kSecReturnData as String] = true query[kSecMatchLimit as String] = kSecMatchLimitOne var found: CFTypeRef? guard SecItemCopyMatching(query as CFDictionary, &found) == errSecSuccess, let data = found as? Data else { return nil } let text = String(decoding: data, as: UTF8.self) return text.isEmpty ? nil : text } @discardableResult public func write(_ key: String, database: String) -> Bool { SecItemDelete(base(database) as CFDictionary) var attributes = base(database) attributes[kSecValueData as String] = Data(key.utf8) attributes[kSecAttrAccessible as String] = kSecAttrAccessibleAfterFirstUnlock return SecItemAdd(attributes as CFDictionary, nil) == errSecSuccess } private func base(_ database: String) -> [String: Any] { var query: [String: Any] = [ kSecClass as String: kSecClassGenericPassword, kSecAttrService as String: Self.service, kSecAttrAccount as String: Self.account(database), kSecAttrSynchronizable as String: true, ] if let accessGroup { query[kSecAttrAccessGroup as String] = accessGroup } return query } } /// A keeper in memory, for tests. public final class InMemoryPlatformKeys: PlatformKeyKeeping, @unchecked Sendable { private let lock = NSLock() private var keys: [String: String] public init(_ seeded: [String: String] = [:]) { keys = seeded } public func read(database: String) -> String? { lock.withLock { keys[database] } } @discardableResult public func write(_ key: String, database: String) -> Bool { lock.withLock { keys[database] = key }; return true } }