2013-03-10 10 views
2

iOS 응용 프로그램을 개발 중입니다. 자체 서명 된 CA 인증서가있는 사용자 지정 인증 기관이 있습니다. 인증 기관은 사용자와 https 서버 모두에 대해 인증서를 발급합니다. 나는 CA 인증서를 사용하여 https 서버를 인증 할 수 있고 클라이언트 인증서를 사용하여 https 서버와 통신 할 수있는 iOS 응용 프로그램을 만들고 싶습니다. 이미 클라이언트 인증서를 사용하여 https 서버와 통신하는 코드가 있지만 시스템 키 링에 ca 인증서를 가져와야합니다. 나는 응용 프로그램에 하드 코딩 된 CA 인증서를 갖고 싶습니다. 내 코드는 다음과 같습니다.클라이언트 인증서가있는 SecTrustSetAnchorCertificates

- (BOOL)connection:(NSURLConnection *)connection canAuthenticateAgainstProtectionSpace:(NSURLProtectionSpace *)protectionSpace { 

    bool result=NO; 
    if ([protectionSpace authenticationMethod] == NSURLAuthenticationMethodServerTrust) { 
     result= YES; 
    } else if([protectionSpace authenticationMethod] ==  NSURLAuthenticationMethodClientCertificate) { 
     result = YES; 
    } 
    return result; 
} 

- (BOOL)shouldTrustProtectionSpace:(NSURLProtectionSpace *)protectionSpace { 
    CFDataRef certDataRef = (__bridge_retained CFDataRef)self.rootCertData; 
    SecCertificateRef cert = SecCertificateCreateWithData(NULL, certDataRef); 

    SecTrustRef serverTrust = protectionSpace.serverTrust; 

    CFArrayRef certArrayRef = CFArrayCreate(NULL, (void *)&cert, 1, NULL); 
    SecTrustSetAnchorCertificates(serverTrust, certArrayRef); 

    SecTrustResultType trustResult; 
    SecTrustEvaluate(serverTrust, &trustResult); 

    return trustResult == kSecTrustResultUnspecified; 
} 

- (void)connection:(NSURLConnection *)connection didReceiveAuthenticationChallenge:(NSURLAuthenticationChallenge *)challenge { 

    NSLog(@"Did receive auth challange %@",[challenge debugDescription]); 

    NSURLProtectionSpace *protectionSpace = [challenge protectionSpace]; 

    NSString *authMethod = [protectionSpace authenticationMethod]; 
    if(authMethod == NSURLAuthenticationMethodServerTrust) { 
     NSLog(@"Verifying The Trust"); 

     NSURLCredential* cred=[NSURLCredential credentialForTrust:[protectionSpace serverTrust]]; 
     if ([self shouldTrustProtectionSpace:challenge.protectionSpace]) { 
      [[challenge sender] useCredential:cred forAuthenticationChallenge:challenge]; 
      NSLog(@"OK"); 
     } else { 
      NSLog(@"FAILED"); 
      [challenge.sender performDefaultHandlingForAuthenticationChallenge:challenge]; 
     } 

    } 
    if(authMethod == NSURLAuthenticationMethodClientCertificate) { 
     NSLog(@"Trying Certificate"); 
     ..... 

서버에 클라이언트 인증서가 필요하지 않을 때까지 모든 것이 작동합니다. 이 순간에 나는이 서버 인증서가 유효하고 심지어 클라이언트 인증서가 서버로 전송되고, 모든 작품, .der과 CA-CERT 시스템의 열쇠 고리에로드가있을 때 실행이 점

NSLog(@"Trying Certificate"); 

에 도달하지 않습니다 오류가 발생합니다 , 서버는 사용자를 인식 할 수 있습니다. 오류없이

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

하지만이 경우 인증서를 확인할 수 아니에요 :이 전화를 건너 뛸 때

SecTrustSetAnchorCertificates(serverTrust, certArrayRef); 

어떻게 든 신뢰에 영향을 미치는 것을 나는 것은, 원인, 나는 간단하게 할 수 있습니다.

내가 뭘 잘못하고 있니?

큰 감사, 아담

답변