2

長い質問で申し訳ありません。これまでのところ、HTTPS 接続の証明書を信頼するために以下の方法を使用しましたが、正常に動作します。

- (BOOL)connection:(NSURLConnection *)connection canAuthenticateAgainstProtectionSpace:(NSURLProtectionSpace *)protectionSpace {
  return [protectionSpace.authenticationMethod isEqualToString:NSURLAuthenticationMethodServerTrust];
}


- (void)connection:(NSURLConnection *)connection didReceiveAuthenticationChallenge:(NSURLAuthenticationChallenge *)challenge {
  if ([challenge.protectionSpace.authenticationMethod isEqualToString:NSURLAuthenticationMethodServerTrust])
    if ([trustedHosts containsObject:challenge.protectionSpace.host])
      [challenge.sender useCredential:[NSURLCredential credentialForTrust:challenge.protectionSpace.serverTrust] forAuthenticationChallenge:challenge];

  [challenge.sender continueWithoutCredentialForAuthenticationChallenge:challenge];
}

ただし、サーバーへのすべてのリクエストは、これらのデリゲート メソッドを呼び出すのに少なくとも 10 ~ 15 秒かかります。顧客はそれを欠陥として提起したので、証明書.derをバンドルに保持しようとし、リクエストで証明書、信頼、ポリシーオブジェクトを送信しようとしました。以下のコードを使用して、.der から証明書を取得しました。

NSString *thePath = [[NSBundle mainBundle]
                     pathForResource:@"Mycertificate" ofType:@"der"];
NSData *certData = [[NSData alloc]
                    initWithContentsOfFile:thePath];

CFDataRef myCertData = (__bridge CFDataRef)certData;                 // 1

SecCertificateRef myCert;
myCert = SecCertificateCreateWithData(NULL, myCertData);    // 2
SecPolicyRef myPolicy = SecPolicyCreateBasicX509();         // 3

SecCertificateRef certArray[1] = { myCert };
CFArrayRef myCerts = CFArrayCreate(
                                   NULL, (void *)certArray,
                                   1, NULL);
SecTrustRef myTrust;

OSStatus status = SecTrustCreateWithCertificates(
                                                 myCerts,
                                                 myPolicy,
                                                 &myTrust);  // 4

SecTrustResultType trustResult;
if (status == noErr) {
    status = SecTrustEvaluate(myTrust, &trustResult);       // 5
    [request setClientCertificates:[NSArray arrayWithObject:(id)CFBridgingRelease(myCert)]];
}
//...
NSLog(@"myTrust  %@", myTrust);
// 6
if (trustResult == kSecTrustResultRecoverableTrustFailure) {
    // ...;
}
// ...
if (myPolicy)
    CFRelease(myPolicy);

myCerts にはすべての証明書オブジェクトが含まれ、myTrust には信頼できるオブジェクトが含まれ、myPolicy にはポリシー オブジェクトが含まれます。以下のリクエストをサーバーに送信しています

NSURL *url = [NSURL URLWithString:@"https://....."];

NSMutableURLRequest *request = [self setURLRequestWithData:requestParam andURL:url];

_theConnection = [[NSURLConnection alloc] initWithRequest:request delegate:self];

このリクエストで証明書、信頼、およびポリシー オブジェクトを設定する方法がわかりません。この問題で私を助けてください。リクエストがサーバー証明書を常に信頼するようにリクエストをサーバーに送信する方法。?? 前もって感謝します。

4

0 に答える 0