OGeek|极客世界-中国程序员成长平台

标题: ios - 使用 CFDataRef 将证书保存到钥匙串(keychain) [打印本页]

作者: 菜鸟教程小白    时间: 2022-12-13 02:06
标题: ios - 使用 CFDataRef 将证书保存到钥匙串(keychain)

我正在尝试为我的 SecIdentityRef 身份获取 CFDataRef 持久引用。但是,当使用苹果提供的标准代码时,该函数为 CFDataRef 返回 0x0。输入参数不是 nil,但不知何故它不再起作用。代码使用完美。

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 仍然没有值。

有人知道怎么回事吗?



Best Answer-推荐答案


使 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;
}

关于ios - 使用 CFDataRef 将证书保存到钥匙串(keychain),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25971840/






欢迎光临 OGeek|极客世界-中国程序员成长平台 (http://sqlite.in/) Powered by Discuz! X3.4