ios - 使用 SecItemUpdate 更新钥匙串(keychain) kSecAttrAccessible 返回 -50

标签 ios objective-c keychain security-framework

我正在尝试根据 mbinna 更新我的钥匙串(keychain)项的 kSecAttrAccessible问题。

问题是以下代码为 updateItemStatus 变量返回 -50。我看了一个 similar question about it然后从我的查询 (newQuery) 中删除了 kSecReturnRef 属性,但它仍然不起作用并返回 -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);
}

关于ios - 使用 SecItemUpdate 更新钥匙串(keychain) kSecAttrAccessible 返回 -50,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57617499/

相关文章:

ios - 在后台立即将屏幕截图发送到服务器

objective-c - 多个@protocols 的奇怪或错误警告

ios - UIlabel 问题 : Keep the string value after app is closed?

ios - SpriteKit,如何将实时摄像机放置在后台

iphone - 发布在 friend 墙上,graph api IOS

ios - 快速浏览 UITableView 中的文本字段

ios - iOS 中的钥匙串(keychain)访问 : "Keychain failed to store the value with code: -50"

ios - 如何使用任何月份和周数 swift 3 获取周开始日期和结束日期?

ios - b2clogin.com域出现问题。使用Mac和IOS,它会显示其他站点的用户名和密码

android - 如何让两个或多个React Native应用程序彼此共享数据?