カスタム CA によって署名された証明書を使用する HTTPS サーバーに接続したい。IOS アプリケーションにハードワイヤードされた認証機関の CA 証明書を使用したいと考えています。私はこのコードを試しました:
- (void)viewDidLoad {
[super viewDidLoad];
.....
NSString *rootCertPath = [[NSBundle mainBundle] pathForResource:@"qfixca"
ofType:@"der"];
self.rootCertData = [NSData dataWithContentsOfFile:rootCertPath];
.....
NSMutableURLRequest *theRequest=[NSMutableURLRequest requestWithURL:
[NSURL URLWithString:url] cachePolicy:
NSURLRequestUseProtocolCachePolicy timeoutInterval:10.0];
[theRequest setValue:userAgent forHTTPHeaderField:@"User-Agent"];
NSURLConnection *theConnection=[[NSURLConnection alloc]
initWithRequest:theRequest delegate:self];
if (theConnection) {
NSLog(@"Connection establisted successfully");
} else {
NSLog(@"Connection failed.");
}
}
- (void)connection:(NSURLConnection *)connection
willSendRequestForAuthenticationChallenge:(NSURLAuthenticationChallenge *)challenge {
SecTrustRef trust = challenge.protectionSpace.serverTrust;
CFDataRef certDataRef = (__bridge_retained CFDataRef)self.rootCertData;
SecCertificateRef cert = SecCertificateCreateWithData(NULL, certDataRef);
// Establish a chain of trust anchored on our bundled certificate.
CFArrayRef certArrayRef = CFArrayCreate(NULL, (void *)&cert, 1, NULL);
SecTrustSetAnchorCertificates(trust, certArrayRef);
SecTrustResultType result;
OSStatus status = SecTrustEvaluate(trust, &result);
if (status == errSecSuccess &&
(result == kSecTrustResultProceed ||
result == kSecTrustResultUnspecified))
{
NSURLCredential *cred = [NSURLCredential credentialForTrust:trust];
[challenge.sender useCredential:cred forAuthenticationChallenge:challenge];
NSLog(@"sending response");
}
else {
[challenge.sender cancelAuthenticationChallenge:challenge];
}
}
問題は、ルート CA 証明書をアンカーとして追加した後です。
SecTrustSetAnchorCertificates
電話
[challenge.sender useCredential:cred forAuthenticationChallenge:challenge];
私が期待したように動作しません。The certificate for this server is invalid エラーが発生しました。CA証明書をアンカーとして追加して直接呼び出すのをスキップすると
[challenge.sender useCredential:cred forAuthenticationChallenge:challenge];
動作しますが、サーバーの証明書を確認できません。私は何を間違っていますか?本当にありがとう!
アダム