// 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-<database id>`, 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.importFoundationimportSecurity/// Where the platform key is kept.publicprotocolPlatformKeyKeeping: Sendable {func read(database: String) -> String? @discardableResult func write(_ key: String, database: String) -> Bool}/// The iCloud Keychain copy.publicstruct ICloudPlatformKeys: PlatformKeyKeeping {publicstaticlet service = "com.cottondb.recovery"privatelet accessGroup: String?publicinit(accessGroup: String? = VaultKey.accessGroup) {self.accessGroup = accessGroup }staticfunc account(_ database: String) -> String { "apple-\(database)" }publicfunc read(database: String) -> String? {var query = base(database) query[kSecReturnData asString] = true query[kSecMatchLimit asString] = kSecMatchLimitOnevar found: CFTypeRef?guardSecItemCopyMatching(query as CFDictionary, &found) == errSecSuccess, let data = found as? Dataelse { returnnil }let text = String(decoding: data, as: UTF8.self)return text.isEmpty ? nil : text } @discardableResult publicfunc write(_ key: String, database: String) -> Bool {SecItemDelete(base(database) as CFDictionary)var attributes = base(database) attributes[kSecValueData asString] = Data(key.utf8) attributes[kSecAttrAccessible asString] = kSecAttrAccessibleAfterFirstUnlockreturnSecItemAdd(attributes as CFDictionary, nil) == errSecSuccess }privatefunc base(_ database: String) -> [String: Any] {var query: [String: Any] = [ kSecClass asString: kSecClassGenericPassword, kSecAttrService asString: Self.service, kSecAttrAccount asString: Self.account(database), kSecAttrSynchronizable asString: true, ]iflet accessGroup { query[kSecAttrAccessGroup asString] = accessGroup }return query }}/// A keeper in memory, for tests.publicfinalclassInMemoryPlatformKeys: PlatformKeyKeeping, @unchecked Sendable {privatelet lock = NSLock()privatevar keys: [String: String]publicinit(_ seeded: [String: String] = [:]) { keys = seeded }publicfunc read(database: String) -> String? { lock.withLock { keys[database] } } @discardableResult publicfunc write(_ key: String, database: String) -> Bool { lock.withLock { keys[database] = key }; returntrue }}