gpt4 book ai didi

iphone - 是否可以更新钥匙串(keychain)项的 kSecAttrAccessible 值?

转载 作者:IT王子 更新时间:2023-10-29 07:59:30 31 4
gpt4 key购买 nike

是否可以更新钥匙串(keychain)中现有项目的属性 kSecAttrAccessible 的值?似乎在将项目添加到钥匙串(keychain)后无法更改。以下步骤支持我的假设。

将新项目添加到钥匙串(keychain):

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);

稍后,将属性 kSecAttrAccessiblekSecAttrAccessibleWhenUnlocked 更改为 kSecAttrAccessibleAfterFirstUnlock:

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);

此方法的问题是 updateItemStatus 总是导致状态 errSecUnimplemented

我认为应该可以更新 kSecAttrAccessible 的值,因为应用程序的要求会发生变化。如果应用程序在过去未使用 kSecAttrAccessible 指定保护类的情况下向钥匙串(keychain)添加了十个项目会怎样。如果开发人员未明确设置保护等级,钥匙串(keychain)会隐式为新项目分配值 kSecAttrAccessibleWhenUnlocked。后来,开发人员需要将保护类更改为 kSecAttrAccessibleAfterFirstUnlock,因为应用程序必须在后台访问它(多任务处理)。开发人员如何才能做到这一点?

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

最佳答案

在 Apple Developer Technical Support (ADTS) 打开支持事件后,我收到了回答这个问题的回复。 SecItemUpdate() 需要通过属性 kSecValueData 的钥匙串(keychain)项数据来执行属性 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

关于iphone - 是否可以更新钥匙串(keychain)项的 kSecAttrAccessible 值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5369238/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com