6

團隊,無法使用iPhone中的客戶端證書進行身份驗證

我使用基於.net的REST服務配置了雙向SSL。在我的iphone端,我已經在設備配置文件中安裝了服務器證書,並且客戶端證書被捆綁爲應用程序資源。服務器證書驗證工作正常,但客戶端證書驗證失敗。以下是我的代碼片段

- (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; 
} 

我的連接失敗,出現下面提到的錯誤。

{ 
    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.}"; 
} 

請幫助我如何reslove這一點。

回答

1

我遇到了這個完全相同的問題。

解決方法是糾正'主機'HTTP標頭。

我正在使用的包括端口和路徑的一部分。一旦我糾正這個頭只包括網址的主機部分,事情開始工作。

我認爲服務器拒絕我的身份時,我有錯誤的主機頭,我認爲「需要客戶端證書」消息是一般性的迴應,也可能意味着服務器不接受證書提交。

相關問題