Не удается выполнить проверку подлинности с помощью сертификата клиента в iPhone - PullRequest
6 голосов
/ 30 декабря 2011

команда

У меня есть служба REST на основе .net с двухсторонним 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.}";
}

Пожалуйста, помогите мне, как восстановить это.

1 Ответ

1 голос
/ 09 февраля 2012

Я столкнулся с точно такой же проблемой.

Исправление для меня состояло в том, чтобы исправить заголовок HTTP 'Host'.

Тот, который я использовал, включал порт и часть пути. Как только я исправил этот заголовок, включив в него только часть URL-адреса узла, все заработало.

Я думаю, что сервер отклонял мою идентификационную информацию, когда у меня был неправильный заголовок хоста, и я думаю, что сообщение «Требуется сертификат клиента» является общим ответом, который также может означать, что сервер не принял представленный сертификат.

...