首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >iOS应用中的自定义证书

iOS应用中的自定义证书
EN

Stack Overflow用户
提问于 2015-01-09 00:46:07
回答 1查看 1.1K关注 0票数 0

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

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

* NEW *

因此,我尝试在这里使用清单2-1和清单2-2学习教程ref/doc/uid/TP40001358-CH208-SW13。现在,我放弃了从密钥链获得证书,而是从主包加载证书。

代码语言:javascript
复制
NSData *certData = [[NSData alloc] initWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"myfile.com" ofType:@"pfx"]];
CFDataRef myCertData = (__bridge_retained CFDataRef)(certData); 

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

接下来,我修改了这两个清单中的代码,因为方法签名实际上与目标C方法签名不匹配。谁来帮帮我,为什么苹果会这样展示它?函数中没有修改任何代码。

代码语言:javascript
复制
- (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”变量的尊重。

代码语言:javascript
复制
- (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;
}

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

代码语言:javascript
复制
[self extractIdentityAndTrust:myCertData withIdentity:identity withTrust:trust withPassword:CFSTR("supersecret")];
[self copySummaryString:identity];

当我试图运行它时,我会得到一个线程1: EXC_BAD_ACCESS(code=1,adress=0x2b)错误,XCode在下面的行上暂停:

代码语言:javascript
复制
CFStringRef certSummary = SecCertificateCopySubjectSummary

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

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2015-01-09 01:04:09

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

10

*新

我查了你的最新消息。你们两种方法都很有魅力。

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

代码语言:javascript
复制
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];
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/27852186

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档