// Where Cotton Keys' credential provider fills a password from: one item per// login in the keychain group the app shares with its provider, as a// passkey's private half lives there (`PasskeyKeychain`).//// The provider holds no core and cannot open a row, so the APP keeps this// copy: after each pass it asks the core (`password.mirror`) for every login// sealed on the owner's devices that this keychain lacks or holds at another// stamp, writes those, and removes the ones the core names gone// (`PasswordArrivals`). The item is DEVICE-LOCAL PLAINTEXT under the// device's own protection (`kSecAttrAccessibleAfterFirstUnlockThisDeviceOnly`,// never synced, never backed up off the phone); the row's `value_enc` is the// synced copy, sealed under the vault. A login the Worker sealed (k1) never// reaches it.//// A second service keeps what the provider was handed to SAVE (iOS 26.2's// save request) until the app seals it into a row: the provider writes the// password here under a fresh pending id and a mailbox entry names that id,// never the password (`PasswordMailbox`).//// The item's `generic` attribute carries what the list and the identity store// need (id, site, account, name, stamp), never the password. Nothing here// logs.importFoundationimportSecurity/// What a kept login says about itself, without its password.publicstructPasswordKeychainItem: Codable, Equatable, Sendable {/// The `secret` row's id (a pending save's own id in the pending store).publicvar id: String/// The login's site, a host (`password.list`'s `site`).publicvar site: Stringpublicvar username: String?publicvar name: String?/// The core's stamp for what this copy was made from (`password.mirror`).publicvar stamp: Stringpublicinit(id: String, site: String, username: String?, name: String?, stamp: String) {self.id = idself.site = siteself.username = usernameself.name = nameself.stamp = stamp }}/// Where password items are kept: the shared keychain group on a phone, a/// dictionary in a test.publicprotocolPasswordKeys: Sendable {func save(_ item: PasswordKeychainItem, password: Data) throwsfunc password(id: String) -> Data?func items() -> [PasswordKeychainItem]func remove(id: String)}/// The shared keychain group's password items, under one service.publicstructPasswordKeychain: PasswordKeys {/// The logins the provider fills from.publicstaticlet service = "cotton.keys.password"/// What the provider was handed to save, waiting for the app.publicstaticlet pendingService = "cotton.keys.password.pending"privatelet service: Stringprivatelet accessGroup: String?/// `accessGroup` nil files the item where the system does (a test, an/// unsigned build); a shipping build names the shared group, as/// ``PasskeyKeychain/sharedGroup(bundle:)`` reads it.publicinit(service: String = PasswordKeychain.service, accessGroup: String? = PasskeyKeychain.sharedGroup()) {self.service = serviceself.accessGroup = accessGroup }/// The store of saves waiting for the app.publicstaticfunc pending(accessGroup: String? = PasskeyKeychain.sharedGroup()) -> PasswordKeychain {PasswordKeychain(service: pendingService, accessGroup: accessGroup) }privatefunc base() -> [String: Any] {var query: [String: Any] = [ kSecClass asString: kSecClassGenericPassword, kSecAttrService asString: service, ]iflet accessGroup { query[kSecAttrAccessGroup asString] = accessGroup }return query }/// Keep `password` for `item`, replacing an item with the same id.publicfunc save(_ item: PasswordKeychainItem, password: Data) throws { remove(id: item.id)var query = base() query[kSecAttrAccount asString] = item.id query[kSecAttrLabel asString] = item.site query[kSecAttrGeneric asString] = try JSONEncoder().encode(item) query[kSecValueData asString] = password query[kSecAttrAccessible asString] = kSecAttrAccessibleAfterFirstUnlockThisDeviceOnly query[kSecAttrSynchronizable asString] = falselet status = SecItemAdd(query as CFDictionary, nil)guard status == errSecSuccess else { throwPasskeyKeychainError(status: status) } }/// The password kept under this id, or nil.publicfunc password(id: String) -> Data? {var query = base() query[kSecAttrAccount asString] = id query[kSecReturnData asString] = true query[kSecMatchLimit asString] = kSecMatchLimitOnevar found: CFTypeRef?guardSecItemCopyMatching(query as CFDictionary, &found) == errSecSuccess else { returnnil }return found as? Data }/// Every kept login, without its password, by site then account.publicfunc items() -> [PasswordKeychainItem] {var query = base() query[kSecReturnAttributes asString] = true query[kSecMatchLimit asString] = kSecMatchLimitAllvar found: CFTypeRef?guardSecItemCopyMatching(query as CFDictionary, &found) == errSecSuccess,let rows = found as? [[String: Any]]else { return [] }return rows.compactMap { row inguardlet generic = row[kSecAttrGeneric asString] as? Dataelse { returnnil }returntry? JSONDecoder().decode(PasswordKeychainItem.self, from: generic) } .sorted { ($0.site, $0.username ?? "") < ($1.site, $1.username ?? "") } }/// The logins kept for one service identifier: the site itself, or a/// site under it (`accounts.invented.test` for `invented.test`), or the/// host of an address.publicfunc items(for service: String) -> [PasswordKeychainItem] {let host = (URL(string: service)?.host ?? service).lowercased()return items().filter { $0.site == host || host.hasSuffix("." + $0.site) || $0.site.hasSuffix("." + host) } }/// Forget one item.publicfunc remove(id: String) {var query = base() query[kSecAttrAccount asString] = idSecItemDelete(query as CFDictionary) }}