我正在处理客户端证书认证问题。当服务器需要凭据时(在这种情况下是证书),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 结束。根据服务器日志,应用程序未发送客户端证书。
有人对此有经验吗?我需要在应用程序中验证证书吗?还是需要其他方面的操作才能使其正常工作?如果需要,我可以提供我的当前代码。感谢任何想法...