iOS 应用程序中的自定义证书

Custom Certificate in iOS App

我创建了一个证书(p12 类型),并使用 Apple Configurator 安装到 iPad 上。我现在想在我编写的应用程序中访问该证书,但我似乎找不到它的源代码示例。有很多关于如何使用证书的示例,但我一直无法找到任何关于如何将其导入应用程序的信息。

有人能指出我正确的方向吗?提前致谢。

* 新 *

所以我尝试按照此处的教程 https://developer.apple.com/library/ios/documentation/Security/Conceptual/CertKeyTrustProgGuide/iPhone_Tasks/iPhone_Tasks.html#//apple_ref/doc/uid/TP40001358-CH208-SW13 使用清单 2-1 和清单 2-2。现在我放弃了从钥匙串获取证书,而是从主 Bundle 加载它。

NSData *certData = [[NSData alloc] initWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"myfile.com" ofType:@"pfx"]];
CFDataRef myCertData = (__bridge_retained CFDataRef)(certData); 

我不确定我的加载是否正确,但是当我调试时,certData 不是 NULL 并且包含字节。

接下来我修改了两个清单中的代码,因为方法签名与 Objective C 方法签名并不真正匹配。有人帮我看看为什么苹果会这样显示?函数内的代码没有被修改。

- (OSStatus) extractIdentityAndTrust: (CFDataRef) inPKCS12Data withIdentity:(SecIdentityRef *) outIdentity withTrust:(SecTrustRef *) outTrust withPassword:(CFStringRef) keyPassword
{
    OSStatus securityError = errSecSuccess;
    const void *keys[] =   { kSecImportExportPassphrase };
    const void *values[] = { keyPassword };
    CFDictionaryRef optionsDictionary = NULL;

    optionsDictionary = CFDictionaryCreate(NULL, keys, values, (keyPassword ? 1 : 0), NULL, NULL);

    CFArrayRef items = NULL;
    securityError = SecPKCS12Import(inPKCS12Data, optionsDictionary, &items);

    if (securityError == 0) {
        CFDictionaryRef myIdentityAndTrust = CFArrayGetValueAtIndex (items, 0);
        const void *tempIdentity = NULL;
        tempIdentity = CFDictionaryGetValue (myIdentityAndTrust,
                                             kSecImportItemIdentity);
        CFRetain(tempIdentity);
        *outIdentity = (SecIdentityRef)tempIdentity;
        const void *tempTrust = NULL;
        tempTrust = CFDictionaryGetValue (myIdentityAndTrust, kSecImportItemTrust);

        CFRetain(tempTrust);
        *outTrust = (SecTrustRef)tempTrust;
    }

    if (optionsDictionary)
        CFRelease(optionsDictionary);

    if (items)
        CFRelease(items);

    return securityError;
}

接下来我修改了copySummaryString 的方法签名。我还必须在函数内为 'identity' 变量添加一个引用。

- (NSString *)copySummaryString:(SecIdentityRef *) identity
{
    // Get the certificate from the identity.
    SecCertificateRef myReturnedCertificate = NULL;
    OSStatus status = SecIdentityCopyCertificate (*identity, &myReturnedCertificate);

    if (status) {
        NSLog(@"SecIdentityCopyCertificate failed.\n");
        return NULL;
    }

    CFStringRef certSummary = SecCertificateCopySubjectSummary
    (myReturnedCertificate);

    NSString* summaryString = [[NSString alloc] initWithString:(__bridge NSString *)certSummary];

    CFRelease(certSummary);

    return summaryString;
}

最后,在我的调用函数中,我创建了一个传递的身份和信任变量:

[self extractIdentityAndTrust:myCertData withIdentity:identity withTrust:trust withPassword:CFSTR("supersecret")];
[self copySummaryString:identity];

当我尝试运行它时,我得到一个 Thread 1: EXC_BAD_ACCESS(code=1, address=0x2b) 错误并且 XCode 在以下行暂停:

CFStringRef certSummary = SecCertificateCopySubjectSummary

这段代码没有引用我创建的任何变量,所以我真的很惊讶它会在这里崩溃。加载此证书的最终目标是将其用于 HTTPS。我希望我至少走在正确的道路上。

这里是关于通过您的移动应用查找和使用预装证书和身份的文档和示例。

https://developer.apple.com/library/ios/documentation/Security/Conceptual/CertKeyTrustProgGuide/iPhone_Tasks/iPhone_Tasks.html#//apple_ref/doc/uid/TP40001358-CH208-DontLinkElementID_10

*新

我检查了你的更新。 你们这两种方法都很有魅力。

我使用下面的源代码调用了您的方法,并且能够正确提取 SummaryString。

SecIdentityRef identity = nil;
SecTrustRef trust = nil;

NSData *certData = [[NSData alloc] initWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"[Dev] InHouse_Certificates" ofType:@"p12"]];
CFDataRef myCertData = (__bridge_retained CFDataRef)(certData);

[self extractIdentityAndTrust:myCertData withIdentity:&identity withTrust:&trust withPassword:CFSTR("1234")];
NSString* summaryString = [self copySummaryString:&identity];