根据我在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在我看来好像有什么东西没有在凭证里设置。
对如何解决这个问题有什么想法吗?
编辑:
只是为了测试,我通过打开邮件中的文件在iPhone上安装了证书。我可以通过Safari通过https访问这些页面,但不能通过我的应用程序访问。
唯一起作用的是,如果我打开一个指向受保护域上任何资源的NSURLConnection,响应didReceiveAuthenticationChallenge (我加载我的凭据与上面的代码完全一样,只是我直接将它发送给NSURLAuthenticationChallenge的发送者),然后使用更高级别的类访问我真正需要的东西。
发布于 2010-11-15 20:05:16
这似乎是一个已知的问题,NSURLCredentialStorage仅适用于用户名和密码NSURLCredentials (不支持证书)。不幸的是,文档对此只字不提。
https://stackoverflow.com/questions/4164846
复制相似问题