我正在为 iPhone 开发一个应用程序,需要证书来调用某些服务,因此我将证书添加到我的钥匙串(keychain)中,执行以下操作:
SecCertificateRef cert = SecCertificateCreateWithData(NULL, (__bridge CFDataRef) certificadoData);
NSMutableDictionary *dictionary = [[NSMutableDictionary alloc] init];
[dictionary setObject:(__bridge id)kSecClassCertificate forKey:(__bridge id)kSecClass];
[dictionary setObject:(__bridge id)(cert) forKey:(__bridge id<NSCopying>)(kSecValueRef)];
OSStatus status = SecItemAdd((__bridge CFDictionaryRef)dictionary, NULL);
当我列出此代码之前的所有 kSecClassIdentity 时,结果为无,而在此代码之后,返回的是两个身份和一个证书。 当我尝试使用这些身份时,其中一个可以正常工作,但另一个则不能。为什么 SecItemAdd 为一个 kSecClassCertificate 创建两个 kSecClassIdentity?我怎样才能识别正确的呢?
最佳答案
我只需解决这个问题,根据我的研究,问题是其中一个身份包含私钥,另一个身份包含公钥。
因此,当您尝试检索身份时,必须添加
value: kSecAttrKeyClassPrivate / kSecAttrKeyClassPublic
key: kSecAttrKeyClass
到在 SecItemCopyMatching
中用作过滤器的字典,例如:
NSMutableDictionary *filterDictionary = [NSMutableDictionary dictionaryWithObjectsAndKeys:
(__bridge id)kSecClassIdentity, kSecClass,
kSecMatchLimitAll, kSecMatchLimit,
kCFBooleanTrue, kSecReturnRef,
kSecAttrKeyClassPrivate, kSecAttrKeyClass,
nil];
关于iphone - SecItemAdd 创建两个身份,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19713368/