As far as I know, NSURLCredentialStorage is just a wrapper for the keychain services with a more convenient API. Which is why I’d like to use it. But I also want to take advantage of the Data Protection feature kSecAttrAccessibleWhenUnlockedThisDeviceOnly that keychain offers.
Is there a way to set this attribute when using NSURLCredentialStorage to store credentials?
Turns out the answer is Yes, kind of. I looked into the keychain item that
NSURLCredentialStoragecreated. It is of the classkSecClassInternetPasswordand has the access keykSecAttrAccessibleset to “ak”, which iskSecAttrAccessibleWhenUnlocked. So the password is not decrypted while the device is locked.The only downside is that
NSURLCredentialStoragedoesn’t offer a way to change that tokSecAttrAccessibleWhenUnlockedThisDeviceOnlyto get an additional level of security for your backed-up data. You could only change that attribute manually on the keychain item using the lower level keychain APIs (i.e.SecItemUpdate).