【问题标题】:Importing a client certificate into the iPhone's keychain将客户端证书导入 iPhone 的钥匙串
【发布时间】:2011-12-01 10:05:27
【问题描述】:

我正在编写一个与服务器通信的应用程序,该服务器要求客户端使用客户端证书对自己进行身份验证。我需要从应用程序包中的 .p12 文件中提取证书并将其添加到应用程序钥匙串中。

我一直在尝试从 Apple 的 "Certificate, Key, and Trust Services Tasks for iOS" 中弄清楚如何让它工作,但对我来说它似乎不完整,并且没有指定我如何向钥匙串添加任何东西(?)。

我很迷茫,如有任何帮助,请提前致谢!

【问题讨论】:

    标签: iphone ios xcode client-certificates pkcs#12


    【解决方案1】:

    Certificate, Key, and Trust Services Tasks for iOS”确实包含足够的信息来从 .p12 文件中提取证书。

    • 清单 2-1 演示了如何提取 SecIdentityRef

    • 清单 2-2 第二行 (// 1) 显示了如何复制 SecCertificateRef 来自 SecIdentityRef。

    示例加载 p12 文件,提取证书,安装到钥匙串。 (不包括错误处理和内存管理)

      NSString * password = @"Your-P12-File-Password";
      NSString * path = [[NSBundle mainBundle]
                         pathForResource:@"Your-P12-File" ofType:@"p12"];
    
      // prepare password
      CFStringRef cfPassword = CFStringCreateWithCString(NULL,
                                                         password.UTF8String,
                                                         kCFStringEncodingUTF8);
      const void *keys[]   = { kSecImportExportPassphrase };
      const void *values[] = { cfPassword };
      CFDictionaryRef optionsDictionary
      = CFDictionaryCreate(kCFAllocatorDefault, keys, values, 1,
                                                      NULL, NULL);
    
      // prepare p12 file content
      NSData * fileContent = [[NSData alloc] initWithContentsOfFile:path];
      CFDataRef cfDataOfFileContent = (__bridge CFDataRef)fileContent;
    
      // extract p12 file content into items (array)
      CFArrayRef items = CFArrayCreate(NULL, 0, 0, NULL);
      OSStatus status = errSecSuccess;
      status = SecPKCS12Import(cfDataOfFileContent,
                               optionsDictionary,
                               &items);
      // TODO: error handling on status
    
      // extract identity
      CFDictionaryRef yourIdentityAndTrust = CFArrayGetValueAtIndex(items, 0);
      const void *tempIdentity = NULL;
      tempIdentity = CFDictionaryGetValue(yourIdentityAndTrust,
                                          kSecImportItemIdentity);
    
      SecIdentityRef yourIdentity = (SecIdentityRef)tempIdentity;
    
    
      // get certificate from identity
      SecCertificateRef yourCertificate = NULL;
      status = SecIdentityCopyCertificate(yourIdentity, &yourCertificate);
    
    
      // at last, install certificate into keychain
      const void *keys2[]   = {    kSecValueRef,             kSecClass };
      const void *values2[] = { yourCertificate,  kSecClassCertificate };
      CFDictionaryRef dict
      = CFDictionaryCreate(kCFAllocatorDefault, keys2, values2,
                                                2, NULL, NULL);
      status = SecItemAdd(dict, NULL);
    
      // TODO: error handling on status
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-10-21
      • 1970-01-01
      • 2017-10-07
      • 2022-11-20
      • 2011-02-15
      相关资源
      最近更新 更多