Update Keychain kSecAttrAccessible with SecItemUpdate return



我正在尝试根据mbinna问题更新我的钥匙串项目的kSecAttrAccess。

问题是以下代码为updateItemStatus变量返回 -50。我看了一个类似的问题,然后从我的查询中删除了kSecReturnRef属性(newQuery(,但它仍然不起作用并返回 -50,这意味着"传递给函数的一个或多个参数无效"。

我做错了什么?

NSString *privateKeyAttrTag = @"mykeytag";
NSDictionary *getQuery = [NSDictionary dictionaryWithObjectsAndKeys:
kSecClassKey, kSecClass, 
privateKeyAttrTag, kSecAttrApplicationTag, 
kSecAttrKeyTypeRSA, kSecAttrKeyType, 
@YES, kSecReturnRef, 
kSecAttrAccessibleWhenUnlocked, kSecAttrAccessible, nil];
CFTypeRef dataTypeRef = NULL;
OSStatus status = SecItemCopyMatching(
(__bridge CFDictionaryRef)getQuery, &dataTypeRef);
if (status==errSecSuccess && dataTypeRef != NULL) {
NSData *data = (__bridge NSData *)dataTypeRef;
NSDictionary *newQuery = [NSDictionary dictionaryWithObjectsAndKeys:
kSecClassKey, kSecClass,
privateKeyAttrTag, kSecAttrApplicationTag,
kSecAttrKeyTypeRSA, kSecAttrKeyType,
kSecAttrAccessibleWhenUnlocked, kSecAttrAccessible, nil];
NSDictionary *updateAttrs = [NSDictionary dictionaryWithObjectsAndKeys: 
kSecAttrAccessibleAfterFirstUnlock, kSecAttrAccessible,
(CFDataRef)data, kSecValueData, nil];
OSStatus updateItemStatus = SecItemUpdate(
(__bridge CFDictionaryRef)newQuery, (__bridge CFDictionaryRef)updateAttrs);
// updateItemStatus == -50, which means "One or more parameters passed to a function were not valid."
}

问题是因为dataTypeRef不是它本身NSData,它是一个包含数据的Dictionary

完整代码:

NSString *keyTag = @"mykeytag";
NSDictionary *getQuery = @{
(NSString *)kSecClass:              (NSString *)kSecClassKey,
(NSString *)kSecAttrApplicationTag: keyTag,
(NSString *)kSecAttrKeyType:        (NSString *)kSecAttrKeyTypeRSA,
(NSString *)kSecReturnRef:          @YES,
(NSString *)kSecAttrAccessible:     (NSString *)kSecAttrAccessibleWhenUnlocked,
(NSString *)kSecReturnData:         @YES
};
CFDictionaryRef item = NULL;
OSStatus status = SecItemCopyMatching((__bridge CFDictionaryRef)getQuery, (CFTypeRef *)&item);
if (status == errSecSuccess && item != NULL) {
NSDictionary *itemDictionary = (__bridge_transfer NSDictionary *)item;
NSMutableDictionary *updateItem = [NSMutableDictionary dictionaryWithDictionary:itemDictionary];
[updateItem setObject:[getQuery objectForKey:(id)kSecClass] forKey:(id)kSecClass];
NSData *data = itemDictionary[(id)kSecValueData];
NSDictionary *attributesToUpdate = [NSDictionary dictionaryWithObjectsAndKeys:
(NSString *)kSecAttrAccessibleAfterFirstUnlock,
kSecAttrAccessible,
(CFDataRef)data,
kSecValueData,
nil];
OSStatus updateItemStatus = SecItemUpdate((__bridge CFDictionaryRef)updateItem,
(__bridge CFDictionaryRef)attributesToUpdate);
}

相关内容

  • 没有找到相关文章

最新更新