2016-05-06 8 views
0

有効なSSL証明書ではなくなったカスタムSSLを使用してサーバーに接続しています。 NSURLConnectionデリゲートのチャレンジをバイパスするために任意のコードを追加できるようにinfo.plistを更新しました。NSURLConnectionのiOSで断続的なSSLエラーが発生しました

-(void)connection:(NSURLConnection *)connection willSendRequestForAuthenticationChallenge:(NSURLAuthenticationChallenge *)challenge { 
NSLog(@"willSendRequestForAuthenticationChallenge"); 
BOOL trusted = NO; 
if ([challenge.protectionSpace.authenticationMethod isEqualToString:NSURLAuthenticationMethodServerTrust]) { 
    NSString *thePath = [[NSBundle mainBundle] pathForResource:@"cert" ofType:@"der"]; 
    NSData *certData = [[NSData alloc] initWithContentsOfFile:thePath]; 
    CFDataRef certDataRef = (__bridge_retained CFDataRef)certData; 
    SecCertificateRef cert = SecCertificateCreateWithData(NULL, certDataRef); 
    SecPolicyRef policyRef = SecPolicyCreateBasicX509(); 
    SecCertificateRef certArray[1] = { cert }; 
    CFArrayRef certArrayRef = CFArrayCreate(NULL, (void *)certArray, 1, NULL); 
    SecTrustRef serverTrust = challenge.protectionSpace.serverTrust; 
    SecTrustSetAnchorCertificates(serverTrust, certArrayRef); 
    SecTrustResultType trustResult; 
    SecTrustEvaluate(serverTrust, &trustResult); 
    trusted = (trustResult == kSecTrustResultUnspecified); 
    CFRelease(certArrayRef); 
    CFRelease(policyRef); 
    CFRelease(cert); 
    CFRelease(certDataRef); 
} 
if (trusted) { 
    [challenge.sender useCredential:[NSURLCredential credentialForTrust:challenge.protectionSpace.serverTrust] forAuthenticationChallenge:challenge]; 
} else { 
    [challenge.sender performDefaultHandlingForAuthenticationChallenge:challenge]; 
} 
} 

ただし、断続的に起こっている、私はSSLエラーを取得しています。デリゲートwillSendRequestForAuthenticationChallengeを代わりに直接didFailWithErrorデリゲートに呼び出すのではありません。

-(void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error

答えて

0

あなたのコードは微妙に間違っています。

  • kSecTrustResultProceed

お電話の場合を除きまた、期限切れの証明書のために、上記のコードはkSecTrustResultRecoverableTrustFailureエラーが表示されるはずですkSecTrustResultUnspecified

  • は結果状態がいずれかの場合に証明書が信頼されています
    SecTrustSetOptions(serverTrust,kSecTrustOptionAllowExpired); 
    

    ただし、期限切れの証明書を許可することは強くお勧めします。

    「有効ではない」とは、iOS 9の最小要件に違反している場合で、iOS 9でコンパイルした場合は、iOS 9で実行したときの動作とまったく同じですSDK。詳細については、App Transport SecurityのGoogle検索を行ってください。

  • 関連する問題