使用CFDataRef将证书保存到钥匙串
问题描述:
我试图获得CFDataRef
持久性参考,用于我的SecIdentityRef
身份。但是,使用Apple提供的标准代码时,函数返回0x0 CFDataRef
。输入参数不是零,但它不能再工作了。代码使用完美。使用CFDataRef将证书保存到钥匙串
CFTypeRef persistent_ref;
CFDataRef persistentRefForIdentity(SecIdentityRef identity)
{
const void *keys[] = { kSecReturnPersistentRef, kSecValueRef };
const void *values[] = { kCFBooleanTrue, identity };
CFDictionaryRef dict = CFDictionaryCreate(NULL, keys, values, 2, NULL, NULL);
OSStatus status = SecItemAdd(dict, &persistent_ref); // `SecItemAdd` returns 0
if (dict)
CFRelease(dict);
return (CFDataRef)persistent_ref;
}
status
是0,但仍然persistent_ref
没有价值。
有谁知道发生了什么问题?
答
使CFTypeRef persistent_ref成为局部变量。或者在调用SecItemAdd函数之前确保将它设置为NULL。请参阅下面的代码。
CFDataRef persistentRefForIdentity (SecIdentityRef identity)
{
OSStatus status = errSecSuccess;
CFTypeRef persistent_ref = NULL;
const void *keys[] = { kSecReturnPersistentRef, kSecValueRef };
const void *values[] = { kCFBooleanTrue, identity };
CFDictionaryRef dict = CFDictionaryCreate(NULL, keys, values, 2, NULL, NULL);
// Delete anything already added
SecItemDelete(dict);
//Add the new one
status = SecItemAdd (dict, &persistent_ref);
if (status != errSecSuccess)
return nil;
if (dict)
CFRelease(dict);
return (CFDataRef)persistent_ref;
}
看的像清单2-3从https://developer.apple.com/library/ios/DOCUMENTATION/Security/Conceptual/CertKeyTrustProgGuide/iPhone_Tasks/iPhone_Tasks.html#//apple_ref/doc/uid/TP40001358 -CH208-SW13。你有没有像清单2-1那样获得身份? – 2014-09-22 10:54:35
'presistent_ref'中的问题可能是全局变量。可能会改变其他方法吗?使其成为本地。 – 2014-09-22 10:57:27
是的,我正在从PKCS-12数据中获取身份。当试图使变量'persistent_ref'本地时,它仍然是0x0。 – Hidde 2014-09-22 20:54:31