从我在 MPMoviewPlayerController 的文档中看到的,NSURLCredentialStorage 可以设置为 NSURLConnection 身份验证挑战的替代方案(这对于从 URL 加载资源但抽象 NSURLConnection 并且不提供处理身份验证的委托的高级类很有用)挑战)。这看起来类似于在浏览器中安装证书并在某个地址需要时自动选择所需的证书。
为了测试这一点,我设置了自己的证书颁发机构、服务器证书和客户端证书(.p12 文件)。我已经设置了一个 https 服务器,并使用浏览器从一台单独的计算机成功测试了客户端证书。
现在我需要将证书集成到我的 iPhone 应用程序中。
我已经捆绑了 p12 文件,并在应用程序启动时执行以下操作:
NSString *thePath = [[NSBundle mainBundle] pathForResource:@"clientside" ofType:@"p12"];
NSData *PKCS12Data = [[NSData alloc] initWithContentsOfFile:thePath];
CFDataRef inPKCS12Data = (CFDataRef)PKCS12Data;
SecIdentityRef identity;
SecTrustRef trust;
extractIdentityAndTrust(inPKCS12Data, &identity, &trust);
SecCertificateRef certificate = NULL;
SecIdentityCopyCertificate (identity, &certificate);
const void *certs[] = {certificate};
CFArrayRef certArray = CFArrayCreate(kCFAllocatorDefault, certs, 1, NULL);
NSURLCredential *credential = [NSURLCredential credentialWithIdentity:identity certificates:(NSArray*)certArray persistence:NSURLCredentialPersistencePermanent];
NSURLProtectionSpace *protectionSpace = [[NSURLProtectionSpace alloc]
initWithHost: @"myhostname"
port: 443
protocol: @"https"
realm: nil
authenticationMethod: NSURLAuthenticationMethodClientCertificate];
[[NSURLCredentialStorage sharedCredentialStorage]
setDefaultCredential: credential
forProtectionSpace: protectionSpace];
extractIdentityAndTrust 函数只是从“iOS 的证书、密钥和信任服务任务”文档页面复制而来(我只是用自己的密码替换了证书密码)。
据我所知,身份和证书加载得很好。如果我在控制台中打印 NSURLCredential 对象,我会得到如下内容:<NSURLCredential: 0x140ea0>: (null)
(我不知道这是否是一个好兆头)。
但是,当我执行 setDefaultCredential 时,应用程序崩溃而没有提供太多信息。
堆栈跟踪看起来像这样:
#0 0x350a41c8 in CFHash
#1 0x3515180c in __CFBasicHashStandardHashKey
#2 0x351534e4 in ___CFBasicHashFindBucket_Linear
#3 0x350a4000 in CFBasicHashFindBucket
#4 0x350a3ecc in CFDictionaryGetValue
#5 0x344a0df2 in URLCredentialStorage::setDefaultCredentialForProtectionSpace
#6 0x3446a5aa in CFURLCredentialStorageSetDefaultCredentialForProtectionSpace
#7 0x339ec79e in -[NSURLCredentialStorage setDefaultCredential:forProtectionSpace:]
#8 0x00002612 in -[AVPlayerExampleViewController awakeFromNib] at AVPlayerExampleViewController.m:84
在我看来,凭证中似乎没有设置某些内容。
关于如何解决这个问题有什么想法吗?
EDIT:
为了进行测试,我通过从 Mail 打开文件在 iPhone 上安装了证书。我可以通过 Safari 通过 https 访问页面,但不能通过我的应用程序访问。
唯一有效的是,如果我打开一个 NSURLConnection 到受保护域上的任何资源,响应 didReceiveAuthenticationChallenge (我加载我的凭据与上面的代码完全相同,只是我将其直接发送到 NSURLAuthenticationChallenge 的发件人),然后访问这些内容我真的需要使用更高级别的课程。