6

nhóm,Không thể thực hiện xác thực bằng chứng chỉ ứng dụng khách trên iPhone

Tôi có dịch vụ REST dựa trên mạng được định cấu hình với SSL 2 chiều. Ở phía iphone của tôi, tôi đã cài đặt chứng chỉ máy chủ trong các cấu hình thiết bị và chứng chỉ ứng dụng khách được đóng gói như tài nguyên ứng dụng. Xác thực chứng chỉ máy chủ đang hoạt động tốt, nhưng xác thực chứng chỉ ứng dụng khách không thành công. Bên dưới là đoạn mã của tôi

- (void)connection:(NSURLConnection *) connection didReceiveAuthenticationChallenge:(NSURLAuthenticationChallenge *)challenge { 
    NSLog(@"Authentication challenge with host: %@", challenge.protectionSpace.host); 

    if([challenge previousFailureCount] == 0) { 
     NSURLProtectionSpace *protectionSpace = [challenge protectionSpace]; 
     NSString *authMethod = [protectionSpace authenticationMethod]; 
     if(authMethod == NSURLAuthenticationMethodServerTrust) { 
      NSLog(@"Verifying The Trust"); 
      [[challenge sender] useCredential:[NSURLCredential credentialForTrust:[protectionSpace serverTrust]] forAuthenticationChallenge:challenge]; 
     } 
     else if(authMethod == NSURLAuthenticationMethodClientCertificate) { 
      NSLog(@"Trying Certificate"); 
      // load cert 

      NSString *thePath = [[NSBundle mainBundle] 
           pathForResource:@"Myclientcertificate" ofType:@"pfx"]; 
      NSData *PKCS12Data = [[NSData alloc] initWithContentsOfFile:thePath]; 
      CFDataRef inPKCS12Data = (CFDataRef)PKCS12Data;    

      OSStatus status = noErr; 
      SecIdentityRef myIdentity; 
      SecTrustRef myTrust; 

      status = extractIdentityAndTrust(
              inPKCS12Data, 
              &myIdentity, 
              &myTrust); 

      SecTrustResultType trustResult; 

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

      SecCertificateRef myCertificate; 
      SecIdentityCopyCertificate(myIdentity, &myCertificate); 
      const void *certs[] = { myCertificate }; 
      CFArrayRef certsArray = CFArrayCreate(NULL, certs, 1, NULL); 


      NSURLCredential *credential = [NSURLCredential credentialWithIdentity:myIdentity certificates:(NSArray*)certsArray persistence:NSURLCredentialPersistencePermanent]; 

      [[challenge sender] useCredential:credential forAuthenticationChallenge:challenge]; 


     } 
    } 

} 

- (BOOL)connection:(NSURLConnection *)connection canAuthenticateAgainstProtectionSpace:(NSURLProtectionSpace *)protectionSpace 
{ 
    BOOL result; 
    NSLog(@"canAuthenticateAgainstProtectionSpace: %@", protectionSpace.authenticationMethod); 
    if ([protectionSpace authenticationMethod] == NSURLAuthenticationMethodServerTrust) { 
     result= YES; 
    } else if([protectionSpace authenticationMethod] == NSURLAuthenticationMethodClientCertificate) { 
     result = YES; 
    } 
    return result; 
} 

OSStatus extractIdentityAndTrust(CFDataRef inPKCS12Data, SecIdentityRef *identity, SecTrustRef *trust){ 
    OSStatus securityError = errSecSuccess; 


    CFStringRef password = CFSTR("1234"); 
    const void *keys[] = { kSecImportExportPassphrase }; 
    const void *values[] = { password }; 
    CFDictionaryRef optionsDictionary = CFDictionaryCreate(
                  NULL, keys, 
                  values, 1, 
                  NULL, NULL); 
    CFArrayRef items = CFArrayCreate(NULL, 0, 0, NULL); 
    securityError = SecPKCS12Import(inPKCS12Data, 
            optionsDictionary, 
            &items); 

     if (securityError == 0) {         
     CFDictionaryRef myIdentityAndTrust = CFArrayGetValueAtIndex (items, 0); 
     const void *tempIdentity = NULL; 
     tempIdentity = CFDictionaryGetValue (myIdentityAndTrust, 
              kSecImportItemIdentity); 
     *identity = (SecIdentityRef)tempIdentity; 
     const void *tempTrust = NULL; 
     tempTrust = CFDictionaryGetValue (myIdentityAndTrust, kSecImportItemTrust); 
     *trust = (SecTrustRef)tempTrust; 
    } 

    if (optionsDictionary) { 
     CFRelease(optionsDictionary); 
    } 

    return securityError; 
} 

Kết nối của tôi không thành công với lỗi được đề cập bên dưới.

{ 
    NSErrorFailingURLKey = "https://myIpdaddress/Service1.svc/test/random"; 
    NSErrorFailingURLStringKey = "https://myIpdaddress/Service1.svc/test/random"; 
    NSLocalizedDescription = "The server \U201cmyIpdaddress\U201d requires a client certificate."; 
    NSUnderlyingError = "Error Domain=kCFErrorDomainCFNetwork Code=-1206 \"The server \U201cmyIpdaddress\U201d requires a client certificate.\" UserInfo=0x4b240b0 {NSErrorFailingURLKey=https://myIpdaddress/Service1.svc/test/random, NSErrorFailingURLStringKey=https://myIpdaddress/Service1.svc/test/random, NSLocalizedDescription=The server \U201cmyIpdaddress\U201d requires a client certificate.}"; 
} 

Vui lòng giúp tôi cách reslove điều này.

Trả lời

1

Tôi đã gặp sự cố tương tự chính xác này.

Khắc phục cho tôi là sửa Tiêu đề HTTP 'Máy chủ'.

Thứ tôi đang sử dụng bao gồm cổng và một phần của đường dẫn. Khi tôi đã sửa tiêu đề này để chỉ bao gồm phần máy chủ của url, mọi thứ bắt đầu hoạt động.

Tôi nghĩ rằng máy chủ đã từ chối nhận dạng của tôi khi tôi có tiêu đề máy chủ sai và tôi cho rằng thông báo "yêu cầu chứng chỉ ứng dụng khách" là phản hồi chung cũng có nghĩa là máy chủ không chấp nhận chứng chỉ được trình bày.

Các vấn đề liên quan