2012-07-23 11 views
6

मैं कुछ एपीआई कोड लिख रहा हूं जो http अनुरोध करता है, और मैं कॉल के लिए [NSUrlConnection: sendAsynchronousRequest: queue: completHandler] का उपयोग कर रहा हूं, क्योंकि यह सरल लिखना बहुत आसान बनाता है हैंडलर, और यह भी कि प्रत्येक कॉल के लिए अलग-अलग प्रतिनिधियों के साथ अलग-अलग कक्षाएं नहीं हैं।NSUrlConnection sendAsynchronousRequest और स्वयं हस्ताक्षरित प्रमाणपत्र

मेरी समस्या यह है कि ऐसा लगता है कि स्वयं हस्ताक्षरित प्रमाणपत्र स्वीकार करने का एकमात्र तरीका एक प्रतिनिधि होना है जो कुछ कार्यों को लागू करता है जो कहता है कि प्रमाणपत्र ठीक है। क्या एसिंक्रोनस विधि के साथ ऐसा करने का कोई तरीका है जो ब्लॉक का उपयोग करता है?

उत्तर

3

नहीं, लेकिन प्रतिनिधि कॉल सभी कठिन नहीं हैं। इस कोड को आप की जरूरत है:

1) यह एक फ़ाइल स्थिर

static CFArrayRef certs; 

2 बनाओ) अपने इनिशियलाइज़ में ऐसा करते हैं:

// 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]; 
    } 
} 
संबंधित मुद्दे