iOS keychain: SecItemUpdate returns -50 (paramErr) when updating kSecAttrAccessible

穿精又带淫゛_ 提交于 2019-12-03 17:03:02

The fact that a query can successfully find you the keychain item via SecItemCopyMatching doesn't mean the same query can be used to update the keychain item.

I'm using the following query for item lookup:

[queryPrivateKey setObject:(__bridge id)kSecClassKey forKey:(__bridge id)kSecClass];
[queryPrivateKey setObject:(__bridge id)kSecAttrKeyTypeRSA forKey:(__bridge id)kSecAttrKeyType];
[queryPrivateKey setObject:[NSNumber numberWithBool:YES] forKey:(__bridge id)kSecReturnRef];
[queryPrivateKey setObject:[EncryptionHelper privateKeyTag:JWT_KEYPAIR_TAG] forKey:(__bridge id<NSCopying>)(kSecAttrApplicationTag)];

sanityCheck = SecItemCopyMatching((__bridge CFDictionaryRef)(queryPrivateKey), (void *)&privateKeyef);

However, in order to use this query for item update, I first had to do:

[queryPrivateKey removeObjectForKey:(__bridge id)kSecReturnRef];

Then I can update:

OSStatus updateItemStatus = SecItemUpdate((__bridge CFDictionaryRef)queryPrivateKey,(__bridge CFDictionaryRef)updatedAttributes);

Obviously, kSecReturnRef is not an acceptable key in the query dictionary of SecItemUpdate. I was not able to find the list of acceptable keys of the query of SecItemUpdate from the apple documentation. From the documentation of SecItemUpdate, it seems that only these keys are acceptable, but that doesn't seem to be the correct list as I am expecting keys like kSecClass etc to be in the list. If anyone has an updated doc link please share it, for now it just takes me some trial and error to find out which keys are acceptable for SecItemUpdate.

After the item has been found, there is also another complexity with updating kSecAttrAccessible: you can't update from a higher security setting like kSecAttrAccessibleWhenUnlocked to a lower setting like kSecAttrAccessibleAlways when the phone is locked for security reasons, so the migration has to happen when the phone is unlocked. A good place for the migration is when the app is resumed in foreground, because the device must be in an unlocked state when app is in foreground.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!