Is it possible to update a Keychain item's kSecAttrAccessible value?

前端 未结 2 930
独厮守ぢ
独厮守ぢ 2020-12-29 04:25

Is it possible to update the value of the attribute kSecAttrAccessible of existing items in the Keychain? It seems that it cannot be changed after the item was

相关标签:
2条回答
  • 2020-12-29 04:36

    After opening a support incident at Apple Developer Technical Support (ADTS), I received a reply that answers this question. SecItemUpdate() requires the Keychain item's data via the attribute kSecValueData to perform the update of the attribute kSecAttrAccessible. According to ADTS, this constraint is currently not documented in the reference documentation.

    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
    
    0 讨论(0)
  • 2020-12-29 04:37

    I was unable to get the other answer to work. I ended up testing kSecAttrAccessibile and if it wasn't what I wanted I recorded the value and attributes in the keychain in local variables, reset the keychain, set kSecAttrAccessible as desired and then set the value and attributes in the keychain to their original settings.

    0 讨论(0)
提交回复
热议问题