5

サーバーと安全に通信したいのですが、これが私がやっていることです...

NSURLProtectionSpace *protectionSpace = [challenge protectionSpace];
SecTrustRef trust = [protectionSpace serverTrust];
NSURLCredential *credential = [NSURLCredential credentialForTrust:trust];

    SecPolicyRef myPolicy = SecPolicyCreateBasicX509();

NSArray * certs = [[NSArray alloc] initWithObjects:(id)certificate,nil]; //certificate is my server's cert.
credential = [NSURLCredential credentialForTrust:trust];

    SecTrustSetAnchorCertificates(trust,
                                  (CFArrayRef) [NSArray arrayWithObject:(id) certificate ]);    

OSStatus status = SecTrustCreateWithCertificates(certs, myPolicy, &trust);

SecTrustResultType trustResult = 0;

if (status == noErr) {
    status = SecTrustEvaluate(trust, &trustResult);
}

    NSLog(@"Trust I get: %d", trustResult);
[certs release];

if (trustResult == kSecTrustResultRecoverableTrustFailure) {
    NSLog(@"Recoverable Failure");
    CFAbsoluteTime trustTime,currentTime,timeIncrement,newTime;
    CFDateRef newDate;

    trustTime = SecTrustGetVerifyTime(trust);             
    timeIncrement = 31536000;                               
    currentTime = CFAbsoluteTimeGetCurrent();              
    newTime = currentTime - timeIncrement;                  
    if (trustTime - newTime){                               
        newDate = CFDateCreate(NULL, newTime);              
        SecTrustSetVerifyDate(trust, newDate);            
        status = SecTrustEvaluate(trust, &trustResult);   
    }
    NSLog(@"Trust again:%d", trustResult);// AGAIN kSecTrustResultRecoverableTrustFailure(5) over here

}

誰もがなぜそれが起こっているのか知っています...証明書の有効期限についてではないようですが(実際にはそうではありません)、理由である可能性があります。

ありがとうございました

アル

4

1 に答える 1

5

SecTrustResultRecoverableTrustFailure次の場合に発生します

  • 証明書は md5 ハッシュされています (IOS5)
  • サーバーはルート証明書と中間証明書を提示しません
  • SecTrustSetAnchorCertificatesOnly(trust,YES) が設定されており、アンカー証明書は組み込みのアンカー証明書にのみ含まれています
  • 証明書の有効期限が切れています
  • ?

サーバー証明書だけではなく、証明書チェーン全体を送信するように Web サーバーを構成することで、問題を解決しました。

Apache mod_ssl を構成することにより: https://httpd.apache.org/docs/2.2/mod/mod_ssl.html#sslcertificatechainfile

于 2011-10-14T06:58:24.000 に答える