2012-09-20 14 views
12

मैं आईओएस 5 में आपसी प्रमाणीकरण लागू करने के लिए कोशिश कर रहा हूँ, लेकिन मैं समस्याएं हो रही हैं:आईओएस म्युचुअल प्रमाणीकरण

{NSUnderlyingError = "Error Domain=kCFErrorDomainCFNetwork Code=-1200 \"An SSL error has occurred and a secure connection to the server cannot be made.\" UserInfo=0x18d830 {NSLocalizedDescription=An SSL error has occurred and a secure connection to the server cannot be made., _kCFNetworkCFStreamSSLErrorOriginalValue=-9800, _kCFStreamPropertySSLClientCertificateState=0, NSLocalizedRecoverySuggestion=Would you like to connect to the server anyway?, NSErrorFailingURLStringKey=https://192.168.24.110:8081/t01.json, kCFStreamPropertySSLPeerTrust=<SecTrustRef: 0xceaa2d0>, NSErrorFailingURLKey=https://192.168.24.110:8081/t01.json} 

मैं सर्वर के लिए कुंजी, प्रमाण पत्र और PKCS12 उत्पन्न (या तो स्वयं पर हस्ताक्षर किए या एक नकली सीए मैं के साथ

openssl genrsa -out client.key 1024 
openssl req -new -key client.key -out client.csr 

self-signed 
openssl req -new -key ca.key -x509 -days 1095 -out ca.crt 

CA signed 
openssl x509 -req -days 365 -in client.csr -CA server.crt -CAkey server.key -CAcreateserial -out client.crt 

CRT to PEM 
openssl x509 -in client.crt -out client.der -outform DER 
openssl x509 -in client.der -inform DER -out client.pem -outform PEM 

PEM TO PKCS 12 
openssl pkcs12 -export -in client.pem -inkey client.key -out client.p12 

जिसके परिणामस्वरूप client.p12 फ़ाइल पूरी तरह से काम करता है जब मैं ब्राउज़र (FF15) में आयात: हमेशा इस तरह है कि समस्या) और ग्राहक मिल गया। तो समस्या पिछले चरणों में नहीं पता है।

आईओएस ओर मैं इस उदाहरण की कोशिश की: http://vanjakom.wordpress.com/tag/nsurlconnection/

और इस मैं क्या लिखा है जब मुझे लगता है कि उदाहरण काम नहीं कर पाया है:

// Returns an array containing the certificate 
- (CFArrayRef)getCertificate:(SecIdentityRef) identity { 
    SecCertificateRef certificate = nil; 

    SecIdentityCopyCertificate(identity, &certificate); 
    SecCertificateRef certs[1] = { certificate }; 

    CFArrayRef array = CFArrayCreate(NULL, (const void **) certs, 1, NULL); 

    SecPolicyRef myPolicy = SecPolicyCreateBasicX509(); 
    SecTrustRef myTrust; 

    OSStatus status = SecTrustCreateWithCertificates(array, myPolicy, &myTrust); 
    if (status == noErr) { 
     NSLog(@"No Err creating certificate"); 
    } else { 
     NSLog(@"Possible Err Creating certificate"); 
    } 
    return array; 
} 

// Returns the identity 
- (SecIdentityRef)getClientCertificate { 
    SecIdentityRef identityApp = nil; 
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); 
    NSString *documentsDirectoryPath = [paths objectAtIndex:0]; 
    NSString *myFilePath = [documentsDirectoryPath stringByAppendingPathComponent:@"file12.p12"]; 
    NSData *PKCS12Data = [NSData dataWithContentsOfFile:myFilePath]; 

    CFDataRef inPKCS12Data = (__bridge CFDataRef)PKCS12Data; 
    CFStringRef password = CFSTR("password"); 
    const void *keys[] = { kSecImportExportPassphrase };//kSecImportExportPassphrase }; 
    const void *values[] = { password }; 
    CFDictionaryRef options = CFDictionaryCreate(NULL, keys, values, 1, NULL, NULL); 
    CFArrayRef items = CFArrayCreate(NULL, 0, 0, NULL); 
    OSStatus securityError = SecPKCS12Import(inPKCS12Data, options, &items); 
    CFRelease(options); 
    CFRelease(password); 
    if (securityError == errSecSuccess) { 
     NSLog(@"Success opening p12 certificate. Items: %ld", CFArrayGetCount(items)); 
     CFDictionaryRef identityDict = CFArrayGetValueAtIndex(items, 0); 
     identityApp = (SecIdentityRef)CFDictionaryGetValue(identityDict, kSecImportItemIdentity); 
    } else { 
     NSLog(@"Error opening Certificate."); 
    } 

    return identityApp; 
} 

- (void)connection:(NSURLConnection *)connection didReceiveAuthenticationChallenge:(NSURLAuthenticationChallenge *)challenge { 
    if ([challenge previousFailureCount] == 0) { 
     SecIdentityRef identity = [self getClientCertificate]; // Go get a SecIdentityRef 
     CFArrayRef certs = [self getCertificate:identity]; // Get an array of certificates 
     // Convert the CFArrayRef to a NSArray 
     NSArray *myArray = (__bridge NSArray *)certs; 

     // Create the NSURLCredential 
     NSURLCredential *newCredential = [NSURLCredential credentialWithIdentity:identity certificates:myArray persistence:NSURLCredentialPersistencePermanent]; 

     // Send 
     [challenge.sender useCredential:newCredential forAuthenticationChallenge:challenge];  
    } else { 
     // Failed 
     [[challenge sender] cancelAuthenticationChallenge:challenge]; 
    } 
} 

- (BOOL)connection:(NSURLConnection *)connection canAuthenticateAgainstProtectionSpace:(NSURLProtectionSpace *)protectionSpace 
{ 
    return YES; 
} 

दोनों ही मामलों मैं ग्राहक autheticate नहीं सकते हैं। इसके अलावा मैंने डिवाइस (आईफोन/आईपैड) पर server.crt प्रमाण पत्र भी स्थापित किया है, लेकिन मैं "त्रुटि डोमेन = NSURLErrorDomain कोड = -1200" प्राप्त करता रहता हूं।

कोई विचार? धन्यवाद।

+0

मैंने जो कोड लिखा है वह पूरी तरह से काम करता है, समस्या सर्वर की तरफ थी। – lontra

+1

आपको अपना समाधान एक उत्तर के रूप में पोस्ट करना चाहिए और इसके आगे के बड़े चेकमार्क पर क्लिक करके इसे स्वीकार करना चाहिए, इस तरह आप यहां हल किए गए प्रश्नों को चिह्नित करते हैं। –

उत्तर

3

मैंने जो कोड लिखा है वह पूरी तरह से काम करता है, समस्या सर्वर की तरफ थी।

+1

क्या आप कृपया सर्वर की समस्या को जोड़ सकते हैं? मुझे भी वही समस्या हो रही है –

संबंधित मुद्दे