2012-07-23 21 views
6

httpリクエストを行うAPIコードをいくつか書いていますが、シンプルな記述が簡単にできるように、呼び出しに[NSUrlConnection:sendAsynchronousRequest:queue:completionHandler]を使用していますハンドラを使用して、各呼び出しごとに異なる代理人と異なるクラスを持つ必要はありません。NSUrlConnection sendAsynchronousRequestと自己署名入りの証明書

私が抱えている問題は、自己署名証明書を受け入れる唯一の方法は、証明書が大丈夫であるという2つの機能を実装する代理人を置くことです。ブロックを使用する非同期メソッドでこれを行う方法はありますか?

答えて

3

いいえ、デリゲートの呼び出しはそれほど難しくありません。 )この静的ファイル

static CFArrayRef certs; 

2ください)

1あなたの初期化中に次の操作を行います:これはあなたが必要とするコードである

// I had a crt certificate, needed a der one, so found this site: 
    // http://fixunix.com/openssl/537621-re-der-crt-file-conversion.html 
    // and did this from Terminal: openssl x509 -in crt.crt -outform der -out crt.der 
    NSString *path = [[NSBundle mainBundle] pathForResource:@"<your name>" ofType:@"der"]; 
    assert(path); 
    NSData *data = [NSData dataWithContentsOfFile:path]; 
    assert(data); 

    SecCertificateRef rootcert = SecCertificateCreateWithData(NULL, (__bridge CFDataRef)data); 
    if(rootcert) { 
     const void *array[1] = { rootcert }; 
     certs = CFArrayCreate(NULL, array, 1, &kCFTypeArrayCallBacks); 
     CFRelease(rootcert); // for completeness, really does not matter 
    } else { 
     NSLog(@"BIG TROUBLE - ROOT CERTIFICATE FAILED!"); 
    } 

3)次に、このメソッドを追加:

- (void)connection:(NSURLConnection *)conn didReceiveAuthenticationChallenge:(NSURLAuthenticationChallenge *)challenge 
{ 
#pragma unused(conn) 
    // NSLog(@"didReceiveAuthenticationChallenge %@ FAILURES=%zd", [[challenge protectionSpace] authenticationMethod], (ssize_t)[challenge previousFailureCount]); 

    /* Setup */ 
    NSURLProtectionSpace *protectionSpace = [challenge protectionSpace]; 
    assert(protectionSpace); 
    SecTrustRef trust      = [protectionSpace serverTrust]; 
    assert(trust); 
    CFRetain(trust);      // Don't know when ARC might release protectionSpace 
    NSURLCredential *credential    = [NSURLCredential credentialForTrust:trust]; 

    BOOL trusted = NO; 
    OSStatus err; 
    SecTrustResultType trustResult = 0; 

    err = SecTrustSetAnchorCertificates(trust, certs); 
    if (err == noErr) { 
     err = SecTrustEvaluate(trust, &trustResult); 
     if(err == noErr) { 
     // http://developer.apple.com/library/mac/#qa/qa1360/_index.html 
      switch(trustResult) { 
      case kSecTrustResultProceed: 
      case kSecTrustResultConfirm: 
      case kSecTrustResultUnspecified: 
       trusted = YES; 
       break; 
      }  
     } 
    } 
    CFRelease(trust); 

    // Return based on whether we decided to trust or not 
    if (trusted) { 
     [[challenge sender] useCredential:credential forAuthenticationChallenge:challenge]; 
    } else { 
     NSLog(@"Trust evaluation failed"); 
     [[challenge sender] cancelAuthenticationChallenge:challenge]; 
    } 
} 
関連する問題