HTTP 加载证书数据
2016-05-31 本文已影响50人
梦里落花Daniel
//证书解析
- (BOOL)extractPassword:(NSString*)pwdStr certChain:(SecCertificateRef*)outCertChain fromPKCS12Data:(NSData *)inPKCS12Data {
BOOL result= NO;
OSStatus securityError = errSecSuccess;
NSDictionary *optionsDictionary = [NSDictionary dictionaryWithObject:pwdStr forKey:(id)CFBridgingRelease(kSecImportExportPassphrase)];
CFArrayRef items = CFArrayCreate(NULL, 0, 0, NULL);
securityError = SecPKCS12Import((__bridge CFDataRef)inPKCS12Data,(__bridge CFDictionaryRef)optionsDictionary,&items);
if (securityError == 0) {
CFDictionaryRef myIdentityAndTrust = CFArrayGetValueAtIndex (items, 0);
const void *tempCertChain = NULL;
tempCertChain = CFArrayGetValueAtIndex((CFArrayRef)CFDictionaryGetValue (myIdentityAndTrust, kSecImportItemCertChain), 0);
*outCertChain = (SecCertificateRef)tempCertChain;
result = YES;
}
CFRelease(items);
return result;
}
//函数调用
BOOL result = [self extractPassword:password certChain:&certChain fromPKCS12Data:PKCS12Data];
if (result)
{
NSData *data = (NSData*)(CFBridgingRelease(SecCertificateCopyData(certChain)));
securityPolicy = [AFSecurityPolicy policyWithPinningMode:AFSSLPinningModePublicKey];
[securityPolicy setPinnedCertificates:[NSArray arrayWithObject:data]];
[securityPolicy setAllowInvalidCertificates:YES];
option.m_securityPolicy = securityPolicy;
}