在报告无法与 beta 一起使用后,我必须将应用程序更新到 iOS5。问题的根源在于我们的自定义 SSL 证书验证不再有效。
在 didReceiveAuthenticationChallenge 部分,我们获取根证书并调用 SecTrustEvaluate。这在 iOS4 上运行良好。
protectionSpace = [challenge protectionSpace];
trust = [protectionSpace serverTrust];
err = SecTrustEvaluate(trust, &trustResult);
trusted = (err == noErr) && ((trustResult == kSecTrustResultProceed) || (trustResult == kSecTrustResultUnspecified));
if (!trusted) {
err = SecTrustSetAnchorCertificates(trust, (CFArrayRef)[EagleAccessAppDelegate getDelegate].rootCertificates);
if (err == noErr) {
err = SecTrustEvaluate(trust, &trustResult);
}
trusted = (err == noErr) && ((trustResult == kSecTrustResultProceed) || (trustResult == kSecTrustResultUnspecified));
}
if (trusted) {
NSURLCredential *cred = [NSURLCredential credentialForTrust:trust];
[[challenge sender] useCredential:cred forAuthenticationChallenge:challenge];
} else {
[[challenge sender] cancelAuthenticationChallenge:challenge];
}
证书以 DER 格式存储为应用程序中包含的资源。
// Load Certificates.
NSString *devFilePath = [[NSBundle mainBundle] pathForResource:@"ipms-dev-ca.der" ofType:@"crt"];
NSData *devRootCertificate = [[[NSData alloc] initWithContentsOfFile:devFilePath] autorelease];
SecCertificateRef devRoot = SecCertificateCreateWithData(NULL, (CFDataRef) devRootCertificate);
NSString *prodFilePath = [[NSBundle mainBundle] pathForResource:@"ipms-prod-ca.der" ofType:@"crt"];
NSData *prodRootCertificate = [[[NSData alloc] initWithContentsOfFile:prodFilePath] autorelease];
SecCertificateRef prodRoot = SecCertificateCreateWithData(NULL, (CFDataRef) prodRootCertificate);
self.rootCertificates = [[NSArray alloc] initWithObjects:(id)devRoot, (id)prodRoot, nil];
我们基本上有自己的 CA 证书,我们用它来为我们的应用程序连接到的服务器颁发证书。
我可以使用 AdvancedURLConnections 示例应用程序重新创建它。