使用密码加载X509 pem证书。目标C.

时间:2016-10-19 08:31:01

标签: ios objective-c x509

我是Objective-c和iOS编程的新手。

我需要使用REST API,其中请求需要使用x509 PEM证书文件和证书密码进行相互SSL身份验证。

我的问题是我真的不知道如何在目标C中做到这一点,在C#中它会像以下一样简单:

X509Certificate2 myCertificate = new x509Certificate2(certificatePath, certificatePassword);
myRequest.ClientCertificate.Add(myCertificate);

其中"myRequest"只表示http请求的变量。

我一直在很多论坛和帖子中寻找一段时间,包括一些旧的stackoverflow问题。 人们给出的主要提示是“使用OpenSSL”,但我已经在很少的地方读过(我认为即使在stackoverflow问题中),Apple已经弃用了OpenSSL。

问题:如何使用密码加载.pem x509证书,以使用Objective-C语言将POST请求发送到外部REST API服务?

谢谢。

1 个答案:

答案 0 :(得分:0)

我自己的解决方案:

使用OpenSSL命令转换.pem文件介绍pkcs12证书:

openssl pkcs12 -export -in "certificateFile" -inkey "KeyFile" -out "certificate.p12"

然后设置本教程中的一些代码:https://vanjakom.wordpress.com/tag/nsurlconnection/

基本上:

- (void)connection:(NSURLConnection *)connection didReceiveAuthenticationChallenge:(NSURLAuthenticationChallenge *)challenge
{
    NSLog(@"Authentication challenge");

    // load cert
    NSString *path = [[NSBundle mainBundle] pathForResource:@"userA" ofType:@"p12"];
    NSData *p12data = [NSData dataWithContentsOfFile:path];
    CFDataRef inP12data = (__bridge CFDataRef)p12data;

    SecIdentityRef myIdentity;
    SecTrustRef myTrust;
    OSStatus status = extractIdentityAndTrust(inP12data, &myIdentity, &myTrust);

    SecCertificateRef myCertificate;
    SecIdentityCopyCertificate(myIdentity, &myCertificate);
    const void *certs[] = { myCertificate };
    CFArrayRef certsArray = CFArrayCreate(NULL, certs, 1, NULL);

    NSURLCredential *credential = [NSURLCredential credentialWithIdentity:myIdentity certificates:(__bridge NSArray*)certsArray persistence:NSURLCredentialPersistencePermanent];

    [[challenge sender] useCredential:credential forAuthenticationChallenge:challenge];
}

OSStatus extractIdentityAndTrust(CFDataRef inP12data, SecIdentityRef *identity, SecTrustRef *trust)
{
    OSStatus securityError = errSecSuccess;

    CFStringRef password = CFSTR("userA");
    const void *keys[] = { kSecImportExportPassphrase };
    const void *values[] = { password };

    CFDictionaryRef options = CFDictionaryCreate(NULL, keys, values, 1, NULL, NULL);

    CFArrayRef items = CFArrayCreate(NULL, 0, 0, NULL);
    securityError = SecPKCS12Import(inP12data, options, &items);

    if (securityError == 0) {
        CFDictionaryRef myIdentityAndTrust = CFArrayGetValueAtIndex(items, 0);
        const void *tempIdentity = NULL;
        tempIdentity = CFDictionaryGetValue(myIdentityAndTrust, kSecImportItemIdentity);
        *identity = (SecIdentityRef)tempIdentity;
        const void *tempTrust = NULL;
        tempTrust = CFDictionaryGetValue(myIdentityAndTrust, kSecImportItemTrust);
        *trust = (SecTrustRef)tempTrust;
    }

    if (options) {
        CFRelease(options);
    }

    return securityError;
}

"从教程中复制的代码,没有根据自己的需要复制我自己的修改"。

它已经解决了