简体   繁体   English

从Keychain检索数据时,有一小部分用户获得errSecItemNotFound

[英]Small percentage of users getting errSecItemNotFound when retrieving data from the Keychain

I have an iOS app that stores an access token in the Keychain. 我有一个iOS应用程序,在Keychain中存储访问令牌。 In the last few months, I've noticed that around 2% of the users get an errSecItemNotFound when trying to retrieve the token. 在过去的几个月里,我注意到大约2%的用户在尝试检索令牌时获得了errSecItemNotFound

All the relevant StackOverflow threads point to background tasks being the culprit ( iOS KeyChain not retrieving values from background ) or including invalid params in the query string ( Keychain: Item reported as errSecItemNotFound, but receive errSecDuplicateItem on addition ). 所有相关的StackOverflow线程都指向后台任务是罪魁祸首( iOS KeyChain不从后台检索值 )或在查询字符串中包含无效参数( Keychain:Item报告为errSecItemNotFound,但在添加时接收errSecDuplicateItem )。

I'm using kSecAttrAccessibleAfterFirstUnlock so background tasks should be able to access the Keychain just fine. 我正在使用kSecAttrAccessibleAfterFirstUnlock,因此后台任务应该能够正常访问Keychain。

Moreover, the search query looks like this: 此外,搜索查询如下所示:

NSMutableDictionary *query = [[NSMutableDictionary alloc] init];
[query setObject:(__bridge id)kSecClassGenericPassword forKey:(__bridge id)kSecClass];
[query setObject:(__bridge id)kCFBooleanTrue forKey:(__bridge id)kSecReturnData];
[query setObject:(__bridge id)kSecMatchLimitOne forKey:(__bridge id)kSecMatchLimit];
[query setObject:service forKey:(__bridge id)kSecAttrService];
[query setObject:key forKey:(__bridge id)kSecAttrGeneric];
[query setObject:key forKey:(__bridge id)kSecAttrAccount];

(Setting kSecAttrGeneric is probably redundant, but it does not affect the outcome of the query anyway) (设置kSecAttrGeneric可能是多余的,但它不会影响查询的结果)

For the record, I've experienced this bug with both SSKeyChain and UICKeychainStore . 为了记录,我遇到了SSKeyChainUICKeychainStore的这个错误。

Any hints would be highly appreciated :] 任何提示都将受到高度赞赏:]

I was using KDJKeychainItemWrapper and had similar issues. 我正在使用KDJKeychainItemWrapper并遇到类似的问题。 In the end I replaced its use of kSecAttrGeneric to use kSecAttrService. 最后,我将其替换为使用kSecAttrGeneric来使用kSecAttrService。 This fixed all my issues of not finding entries and duplicates due to service not being defined. 这修复了由于未定义服务而未查找条目和重复的所有问题。

I believe that the primary keys for kSecClassGenericPassword are just kSecAttrAccount and kSecAttrService. 我相信kSecClassGenericPassword的主键只是kSecAttrAccount和kSecAttrService。

If you move to not using kSecAttrGeneric it should sort itself out with perhaps the user having to put their password in again. 如果你不使用kSecAttrGeneric,它应该排除自己,也许用户必须再次输入密码。

Also pick a service name which will not clash with anything else. 还要选择一个不会与其他任何东西冲突的服务名称。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM