是否可以更新Keychain项目的kSecAttrAccessible值?

mbi*_*nna 23 security iphone keychain ipad ios

是否可以更新kSecAttrAccessibleKeychain中现有项的属性值?项目被添加到钥匙串后似乎无法更改.以下步骤支持我的假设.

将新项添加到钥匙串:

NSData *encodedIdentifier = [@"BUNDLE_IDENTIFIER" 
                             dataUsingEncoding:NSUTF8StringEncoding];
NSData *encodedPassword = [@"PASSWORD"
                           dataUsingEncoding:NSUTF8StringEncoding];

// Construct a Keychain item
NSDictionary *keychainItem = 
    [NSDictionary dictionaryWithObjectsAndKeys:
        kSecClassGenericPassword, kSecClass,
        encodedIdentifier, kSecAttrGeneric,
        encodedIdentifier, kSecAttrService,
        @"USERNAME", kSecAttrAccount,
        kSecAttrAccessibleWhenUnlocked, kSecAttrAccessible,
        encodedPassword, kSecValueData
        nil];

// Add item to Keychain
OSStatus addItemStatus = SecItemAdd((CFDictionaryRef)keychainItem, NULL);
Run Code Online (Sandbox Code Playgroud)

在稍后的时间,该属性改变kSecAttrAccessiblekSecAttrAccessibleWhenUnlockedkSecAttrAccessibleAfterFirstUnlock:

NSData *encodedIdentifier = [@"BUNDLE_IDENTIFIER" 
                             dataUsingEncoding:NSUTF8StringEncoding];

NSDictionary *query = [NSDictionary dictionaryWithObjectsAndKeys:
                       kSecClassGenericPassword, kSecClass,
                       encodedIdentifier, kSecAttrGeneric,
                       encodedIdentifier, kSecAttrService,
                       nil];

NSDictionary *updatedAttributes = 
    [NSDictionary dictionaryWithObject:kSecAttrAccessibleAfterFirstUnlock 
                                forKey:kSecAttrAccessible];

OSStatus updateItemStatus = SecItemUpdate((CFDictionaryRef)query, 
                                          (CFDictionaryRef)updatedAttributes);
Run Code Online (Sandbox Code Playgroud)

这种方法的问题在于updateItemStatus总是导致状态errSecUnimplemented.

我认为应该可以更新值,kSecAttrAccessible因为应用程序的需求会发生变化.如果一个应用程序添加10个项目来钥匙串在过去没有使用指定的防护等级kSecAttrAccessible.kSecAttrAccessibleWhenUnlocked如果开发人员未明确设置保护类,则Keychain会隐式地为新项分配值.之后,开发人员需要将保护类更改为,kSecAttrAccessibleAfterFirstUnlock因为应用程序必须在后台访问它(多任务处理).开发人员如何实现这一目标?

Apple开发者论坛中已有一个主题,但尚未得出答案:https://devforums.apple.com/thread/87646?tstart = 0

mbi*_*nna 22

在Apple开发人员技术支持(ADTS)上打开支持事件后,我收到了回复此问题的回复.SecItemUpdate()需要通过属性的Keychain项目的数据kSecValueData来执行属性的更新kSecAttrAccessible.根据ADTS,此约束目前未在参考文档中记录.

NSData *encodedIdentifier = [@"BUNDLE_IDENTIFIER" 
                             dataUsingEncoding:NSUTF8StringEncoding];
NSDictionary *query = [NSDictionary dictionaryWithObjectsAndKeys:
                       kSecClassGenericPassword, kSecClass,
                       encodedIdentifier, kSecAttrGeneric,
                       encodedIdentifier, kSecAttrService,
                       nil];

// Obtain the Keychain item's data via SecItemCopyMatching()
NSData *itemData = ...;

NSDictionary *updatedAttributes = 
    [NSDictionary dictionaryWithObjectsAndKeys:
        kSecAttrAccessibleAfterFirstUnlock, kSecAttrAccessible,
        (CFDataRef)itemData, kSecValueData,
        nil];

OSStatus updateItemStatus = SecItemUpdate((CFDictionaryRef)query, 
                                          (CFDictionaryRef)updatedAttributes);

// updateItemStatus should have the value errSecSuccess
Run Code Online (Sandbox Code Playgroud)