How iCloud Keychain stores and syncs an app's secrets
Most writing about iCloud Keychain is about the passwords Safari and the Passwords app save for you. This post is about the other case: a third-party app storing its own items in the keychain and syncing them through iCloud. It’s written as a reference. The facts come from Apple’s documentation, linked in each section and listed at the end, and where I’m drawing a conclusion of my own, I say so.
A Mac has two keychains
iOS has one keychain implementation. macOS has two, and which one an app ends up in decides almost everything else (TN3137).
- The file-based keychain is the traditional Mac keychain: the
loginandSystemkeychain files, protected by a keychain password and by per-item access control lists. - The data protection keychain came to the Mac with iCloud Keychain in macOS 10.9. It’s the iOS implementation: one per user, available only in a logged-in user context, with access controlled by keychain access groups.
The SecItem API can target either, and on macOS it defaults to the
file-based keychain. An app reaches the data protection keychain by
setting kSecUseDataProtectionKeychain or kSecAttrSynchronizable to true.
Apple recommends the data protection keychain, and
recommends setting kSecUseDataProtectionKeychain
on all keychain operations. iCloud Keychain requires it, and Mac Catalyst
apps and iOS apps on the Mac can only use it.
Two practical consequences:
- The
securitycommand-line tool is focused on the file-based keychain, so don’t expectsecurity find-generic-passwordto find an app’s data protection items. - Keychain Access shows both, with a limit. It lists the data protection keychain as iCloud Keychain or Local Items, depending on whether iCloud Keychain is turned on, and shows only its password items, not keys or certificates. TN3137 also notes that Keychain Access sometimes doesn’t reflect changes other apps make; quit and relaunch it if what you see looks stale.
Syncing is opt-in, per item
Nothing an app stores syncs by default. Each item that should sync must be
written with kSecAttrSynchronizable set to true. Apple sets it for Safari
passwords, Wi-Fi passwords and similar items; for a third-party app it’s the
developer’s choice
(Secure keychain syncing).
The attribute comes with rules
(kSecAttrSynchronizable):
- Reads have to ask for it too. A query without
kSecAttrSynchronizablereturns no synchronizable items. UsekSecAttrSynchronizableAnyto get both kinds. - Updates and deletes apply everywhere. Changing or deleting a synchronizable item affects every copy, not just the one on this device.
- Some accessibility classes can’t sync. The classes whose names end in
ThisDeviceOnlyare incompatible with synchronizable items. - No references. Retrieve items with
kSecReturnAttributesandkSecReturnData, not by reference, and don’t keep persistent references: they can’t move between devices and may not resolve after another device edits the item. - Limited queries. In a query that uses the attribute,
kSecMatchLimitis the onlykSecMatch…key you can use. - No file-based access lists. Synchronizable items can’t use
kSecAttrAccess(SecAccess) access control; share them through access groups instead. - Not tvOS. tvOS accepts the attribute but never syncs: items stored there never leave the device.
- What syncs depends on the OS. Since iOS 14, macOS 11 and watchOS 7, iCloud Keychain syncs passwords, certificates and cryptographic keys; earlier versions synced passwords only.
Accessibility classes
An item’s accessibility class says when the item can be read, and whether it
may leave the device. Whether it actually syncs is still up to
kSecAttrSynchronizable
(Keychain data protection).
| Class | Readable | Sync allowed |
|---|---|---|
kSecAttrAccessibleWhenUnlocked (the default) | While the device is unlocked | Yes |
kSecAttrAccessibleAfterFirstUnlock | After the first unlock since restart (suits background work) | Yes |
…ThisDeviceOnly variants | Same as above | No; also unusable if a backup is restored to another device |
kSecAttrAccessibleWhenPasscodeSetThisDeviceOnly | While unlocked, and only while a passcode is set | No; not backed up or escrowed, and unusable if the passcode is removed or reset |
Syncing isn’t the only way an item leaves a device. Items in the classes
without ThisDeviceOnly
migrate to a new device when using encrypted backups,
whether or not they sync. On every platform, the ThisDeviceOnly suffix is
what keeps an item on the device, through sync or through a backup restored
elsewhere.
One Mac-specific caveat: Apple says that on macOS, Data Protection “isn’t used directly” to enforce these classes, and doesn’t document what enforces them instead. My conclusion: don’t rely on a class for lock-state guarantees on a Mac.
Who can read an item
Every request to the data protection keychain goes through the securityd
daemon, which checks the calling app’s code-signing entitlements: its
keychain access groups, application identifier and application groups. Each
access group carries a prefix Apple allocates to the developer, such as the
Team ID or an app group identifier, so keychain items can be shared only
between apps from the same developer, and only when each app’s
provisioning profile authorizes the group
(Keychain data protection).
That’s a strong boundary between apps, and it’s worth being precise about what it doesn’t cover:
- The user, on a Mac. Keychain Access lists the app’s password items (TN3137), and anyone who can authenticate as that user can reveal their values; Secret Store’s help walks through it. With sync on, that’s true on every Mac signed in to the account.
- The app itself. Once the app has read a value and shown it on screen, copied it to the clipboard or written it to a file, the keychain’s protection no longer applies to that copy.
Choosing an access group
An item belongs to exactly one access group. If a write doesn’t name one,
the item goes into the app’s first access group: the first
keychain-access-groups entry, or the app ID if there are none
(kSecAttrAccessGroup).
- Set
kSecAttrAccessGroupexplicitly on every write. Otherwise adding akeychain-access-groupsentitlement later quietly sends new items to a different group from the old ones. - Treat the group as permanent. If an app drops a group from its entitlements, it can no longer read the items stored in it.
What end-to-end encryption covers
Apple lists Passwords and Keychain as end-to-end encrypted under Standard Data Protection, the default. Turning on Advanced Data Protection doesn’t change how the keychain is encrypted, because it already was. In Apple’s words, end-to-end encrypted data “can be decrypted only on your trusted devices”, and Apple doesn’t have the keys (iCloud data security overview).
How that works for syncing (Secure keychain syncing):
- When iCloud Keychain is first turned on, each device creates a syncing identity, a P-384 key pair kept in its own keychain.
- Each device keeps a signed list of the other devices’ syncing identities, and the devices use these lists to agree on who’s trusted. Apple still calls that set the circle of trust. A new device joins by being sponsored by a trusted device, or through iCloud Keychain recovery.
- Each synced item is encrypted so that only trusted devices can decrypt it. Apple stores it but can’t read it.
Recovery, for when you lose every device, goes through escrow. The keys that encrypt the keychain are held in records protected by clusters of hardware security modules. Getting them back takes signing in to the Apple Account, an SMS check, and the escrow secret: on accounts with two-factor authentication, which is nearly all of them, that’s the passcode of one of your devices. It’s verified with the Secure Remote Password protocol, so it’s never sent to Apple. The service allows 10 attempts; after the tenth failure the record is destroyed (Escrow security for iCloud Keychain).
What end-to-end encryption doesn’t promise:
- It protects data in transit and on Apple’s servers, not on a trusted device that someone else can unlock.
- Apple can’t decrypt your keychain for you. Recovery always needs something only you have: a device passcode through escrow, a recovery contact or a recovery key. If none of them works, the data is gone.
- It says nothing about copies an app makes outside the keychain, such as exports, backups and logs.
When two devices change the same item
iCloud Keychain doesn’t merge. Apple’s description is that when two devices update the same item, “one or the other is chosen”, and the devices converge on that version eventually. The documentation doesn’t say how the winner is picked, or anything about ordering across items.
The rest is my reading of what that means for an app:
- Keep each record in one item where you can. If a record spans two items, sync may deliver one before the other, or pick different winners for each, and the app has to repair the mismatch.
- Expect items to appear and change underneath you. An add can fail with
errSecDuplicateItembecause sync delivered the same item first; an update can geterrSecItemNotFoundbecause another device deleted it. - If you need “newest wins”, store your own version or timestamp inside the value and resolve on read. The keychain won’t do it for you.
A checklist for apps that sync secrets
- Set
kSecUseDataProtectionKeychainon every keychain call. - Set
kSecAttrSynchronizableto true on writes, and query withkSecAttrSynchronizableAny. Remember it does nothing on tvOS. - Pick an accessibility class without
ThisDeviceOnly, usuallykSecAttrAccessibleWhenUnlocked. - Set
kSecAttrAccessGroupexplicitly, choose it once and never change it. - Read with
kSecReturnDataandkSecReturnAttributes, never by reference. - Treat duplicates and missing items during writes as normal, not errors.
- Put a version or timestamp inside each value if conflicts matter.
- On a Mac, assume the user can see password items in Keychain Access, and design your export and clipboard behavior assuming values will leave the keychain.
I built Secret Store on exactly these rules. The storage layer post shows the specific choices it makes.
Sources
- Apple Platform Security: Secure keychain syncing
- Apple Platform Security: Keychain data protection
- Apple Platform Security: Escrow security for iCloud Keychain
- Apple Platform Security: Secure iCloud Keychain recovery
- Apple Support: iCloud data security overview
- Apple Developer: TN3137: On Mac keychain APIs and implementations
- Apple Developer: kSecAttrSynchronizable
- Apple Developer: kSecUseDataProtectionKeychain
- Apple Developer: kSecAttrAccessGroup
- Apple Developer: kSecAttrAccessibleWhenUnlocked
If iCloud Keychain picked the other device’s copy of one of your items tomorrow, would your app notice?