【发布时间】:2010-11-30 10:40:00
【问题描述】:
我正在与客户端证书身份验证作斗争。当服务器需要一个凭证(在这种情况下是一个证书)时,这个方法会从 NSURLConnection 委托中调用:
- (void)connection:(NSURLConnection *)connection didReceiveAuthenticationChallenge:(NSURLAuthenticationChallenge *)challenge
我想从文件中加载证书,填写凭据并运行此方法:
[[challenge sender] useCredential:[self credential] forAuthenticationChallenge:challenge];
但我不知道如何初始化(或填充)SecIdentityRef 参数。这是我创建凭据的代码:
NSString *certPath = [[NSBundle mainBundle] pathForResource:@"certificate" ofType:@"cer"];
NSData *certData = [[NSData alloc] initWithContentsOfFile:certPath];
SecIdentityRef myIdentity; // ???
SecCertificateRef myCert = SecCertificateCreateWithData(NULL, (CFDataRef)certData);
[certData release];
SecCertificateRef certArray[1] = { myCert };
CFArrayRef myCerts = CFArrayCreate(NULL, (void *)certArray, 1, NULL);
CFRelease(myCert);
NSURLCredential *credential = [NSURLCredential credentialWithIdentity:myIdentity
certificates:(NSArray *)myCerts
persistence:NSURLCredentialPersistencePermanent];
CFRelease(myCerts);
有人知道怎么解决吗?谢谢。
我终于找到了解决办法,但是又出现了一个新问题:
我的客户端没有将证书发送到服务器。服务器请求证书后,应用程序运行此方法:
- (void)connection:(NSURLConnection *)connection didReceiveAuthenticationChallenge:(NSURLAuthenticationChallenge *)challenge
我填写了凭据(就像我上面提到的那样),但连接以错误结束:NSURLErrorDomain -1206。根据服务器日志,客户端证书不是由应用程序发送的。
有人遇到过这种行为吗?我是否需要以某种方式验证应用程序中的证书?或者还有什么可以让它工作的?如果有帮助,我可以提供我当前的代码。感谢您的任何想法...
【问题讨论】:
标签: iphone authentication ssl certificate