iPhone: HTTPS client cert authentication

前端 未结 3 1179
北海茫月
北海茫月 2020-12-22 23:48

I\'m fighting with a client certificate authentication. When a server needs a credential (a certificate in this case), this method is invoked from NSURLConnection d

相关标签:
3条回答
  • 2020-12-23 00:34

    I use these steps:

    1. extract SecIdentityRef from the pkcs12 certificate file using SecPKCS12Import function
    2. use SecIdentityCopyCertificate function to get SecCertificateRef

    and the rest (a credential initialization) is the same as in my question... I can put here more code if you want. Note that there is a bug (http://openradar.appspot.com/7090030) in the iphone simulator, so it is not possible to work with a lot of certifcates in the simulator.

    0 讨论(0)
  • 2020-12-23 00:42

    Of course the problem was with the iPhone simulator in xcode :) After updating to version 3.1 it started to work...

    0 讨论(0)
  • 2020-12-23 00:50

    You can also search for identity in keychain if you store this information there:

    + (SecIdentityRef)dumpSecIdentityRef
    {
    OSStatus    err;
    CFArrayRef  result;
    CFIndex     resultCount;
    CFIndex     resultIndex;
    
    result = NULL;
    err = SecItemCopyMatching((__bridge CFDictionaryRef) [NSDictionary dictionaryWithObjectsAndKeys:
                                                          (__bridge id)kSecClassIdentity,
                                                          kSecClass, kSecMatchLimitAll,
                                                          kSecMatchLimit, kCFBooleanTrue,
                                                          kSecReturnRef, kCFBooleanTrue,
                                                          kSecReturnAttributes, nil],
                              (CFTypeRef *) &result);
    
    if ((result != NULL) && (err == noErr)) {
    
        NSMutableArray *identitiesArray = [NSMutableArray new];
    
        resultCount = CFArrayGetCount(result);
        for (resultIndex = 0; resultIndex < resultCount; resultIndex++) {
            NSDictionary *  thisResult;
            thisResult = (__bridge NSDictionary *) CFArrayGetValueAtIndex(result, resultIndex);
            NSLog(@"%@", (__bridge id)(result));
            [identitiesArray addObject:thisResult];
        }
    
        CFRelease(result);
        //TO DO - choose correct identity object from array.
        SecIdentityRef myIdentity = (__bridge SecIdentityRef)([[identitiesArray objectAtIndex:0] valueForKey:@"v_Ref"]);
    
        return myIdentity;
    }
    return nil;
    }
    
    0 讨论(0)
提交回复
热议问题