1
我有支持NSURLConnection的UIWebView。我想添加證書。當我想從證書中提取身份和信任OSStatus返回錯誤-26275。你有什麼想法如何使它正確嗎?下面的代碼:無法提取證書(錯誤-26275)
- (void)connection:(NSURLConnection *)connection didReceiveAuthenticationChallenge:(NSURLAuthenticationChallenge *)challenge {
NSError *error = nil;
NSString *path = [[NSBundle mainBundle] pathForResource:[@"xxxx.pem" stringByDeletingPathExtension] ofType:[@"xxxx.pem" pathExtension]];
NSData *certData = [[NSData alloc] initWithContentsOfFile:path options:0 error:&error];
CFDataRef inP12data = (__bridge CFDataRef)certData;
SecIdentityRef identity;
SecTrustRef trust;
OSStatus status = extractIdentityAndTrust(inP12data, &identity, &trust);
NSLog(@"status %d", (int)status);
if(status == errSecSuccess) {
SecCertificateRef certificate;
SecIdentityCopyCertificate(identity, &certificate);
const void *certs[] = { certificate };
CFArrayRef certsArray = CFArrayCreate(NULL, certs, 1, NULL);
NSArray *certificatesForCredential = (__bridge NSArray *)certsArray;
NSURLCredential *credential = [NSURLCredential credentialWithIdentity:identity
certificates:certificatesForCredential
persistence:NSURLCredentialPersistencePermanent];
[challenge.sender useCredential:credential forAuthenticationChallenge:challenge];
CFRelease(identity);
CFRelease(certificate);
CFRelease(certsArray);
}
else {
[challenge.sender cancelAuthenticationChallenge:challenge];
}
}
和extractIdentityAndTrust功能:
OSStatus extractIdentityAndTrust(CFDataRef inPKCS12Data, SecIdentityRef *identity, SecTrustRef *trust){
OSStatus securityError = errSecSuccess;
CFStringRef password = CFSTR("XXXXX");
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;
}