我在我的应用程序的文档文件夹中有一个PKCS12文件,其中包含一个证书和一个私钥。 我可以打开此.p12文件,提取标识对象并显示一些信息,这要归功于Apple的文档(https://developer.apple.com/library/ios/#documentation/Security/Conceptual/CertKeyTrustProgGuide/iPhone_Tasks/iPhone_Tasks .html#// apple_ref/doc/uid/TP40001358-CH208-DontLinkElementID_10)如何将已添加的SecIdentityRef更新为iOS应用程序的钥匙串?
我现在要做的是将此标识存储到钥匙串中,以便稍后使用它。我已经阅读了iOS Keychain上的很多不同的东西,我很难弄清它是如何工作的。
Apple的代码似乎使用persistent_ref来检索存储在Keychain中的Identity。但我真的不明白这是什么...它是一个简单的参考,如记忆参考?如果是这种情况,设备重新启动时会发生什么?
无法找到关于此的更多信息我试图通过使用kSecAttr属性以不同方式执行此操作。 目前的代码工作正常添加身份到钥匙串:
NSMutableDictionary * dictionary = [[[NSMutableDictionary alloc] init] autorelease];
[dictionary setObject:@"LABEL" forKey:kSecAttrLabel];
[dictionary setObject:(id)newIdentity forKey:(id)kSecValueRef];
OSStatus status = SecItemAdd((CFDictionaryRef)dictionary, NULL);
但是,如果我尝试添加了第二遍我收到-25299错误是“精”,因为它已经存在。
NSMutableDictionary *searchDictionary = [[[NSMutableDictionary alloc] init] autorelease];
[searchDictionary setObject:@"LABEL" forKey:kSecAttrLabel];
[searchDictionary setObject:(id)kSecMatchLimitOne forKey:(id)kSecMatchLimit];
[searchDictionary setObject:(id)kCFBooleanTrue forKey:(id)kSecReturnRef];
NSMutableDictionary *updateDictionary = [[NSMutableDictionary alloc] init];
[updateDictionary setObject:(id)newIdentity forKey:(id)kSecValueRef];
OSStatus status = SecItemUpdate((CFDictionaryRef)searchDictionary,(CFDictionaryRef)updateDictionary);
有了这个代码,我收到了-50状态错误显然是因为我有无效的参数......哪一个:我试图通过这样的更新处理呢?为什么?我能做些什么来正确更新我的钥匙串?
编辑:建议我试图在添加它之前删除现有的元素,但我坚持使用相同的状态代码(-50)。下面是我试过的代码:
NSMutableDictionary *searchDictionary = [self setupSearchDirectoryForIdentifier:identifier];
OSStatus status = SecItemDelete((CFDictionaryRef)searchDictionary);
NSAssert(status == noErr, @"Problem deleting current keychain item.");
setupSearchDirectoryForIdentifier只需创建一个NSDictionnary与我的项目的标签:
- (NSMutableDictionary *)setupSearchDirectoryForIdentifier:(NSString *)identifier {
// Setup dictionary to access keychain.
NSMutableDictionary *searchDictionary = [[[NSMutableDictionary alloc] init] autorelease];
[searchDictionary setObject:identifier forKey:kSecAttrLabel];
return searchDictionary;
}
谢谢
PS:我上的Xcode 4.2/iPad的开发5.1.1
刚编辑我的问题。我试图删除之前添加项目,但我仍然有相同的状态代码,即使我只在searchDictionary中有一个元素。对我来说没有多大意义。 – Anth0