NsurlCredentialStorage和客户端证书身份验证
-
09-10-2019 - |
题
从我在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时,应用程序崩溃而无需提供太多信息。
StackTrace看起来像这样:
#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的页面,但不能通过我的应用程序访问。
唯一有效的是,如果我对受保护域上的任何资源打开NSURLCONCENT,请响应DidreceiveAthenticationChallenge(在上面的代码中,我完全加载了凭据我真的需要使用更高级别的类。
解决方案
似乎已知的问题是,NsurlCredentialStorage仅适用于用户名和密码Nsurlcredentials(不支持证书)。不幸的是,该文件对此一无所知。