最近在做新的項目椿息,現(xiàn)在改用Https做驗證了,開始以為沒什么覺得不會遇到坑枷餐,后來才發(fā)現(xiàn)自己是真的遇到坑了靶瘸,現(xiàn)在把自己遇到的坑都說一遍,增加別人的效率。
首先怨咪,大家百度來的afnetworking的https驗證屋剑,一般的講解都是單向驗證的。而我們這次是雙向驗證的诗眨,就是客服端和服務(wù)端都驗證唉匾。一開始自己收到了兩個證書,一個是pem格式的(服務(wù)端證書)和另外一個是p12格式的(客戶端證書)匠楚。所以我們這兩個證書是都要驗證的肄鸽,但是afnetworking好像是不支持pem格式證書直接驗證的,所以首先要把pem的證書轉(zhuǎn)換成cer格式的證書油啤。使用的是openssl的方式去轉(zhuǎn)換的,代碼如下:
openssl x509 -in /usr/local/ssl/test.pem -out /usr/local/ssl/test.cer
前面那是pem格式證書的地址蟀苛,后面那個是你要轉(zhuǎn)成的cer格式的證書想要放在的地址益咬。
這樣我們就把pem格式的證書轉(zhuǎn)換成了cer格式的證書了,下面的工作就是我們需要怎么去調(diào)用afnetworking的方法去實現(xiàn)雙向驗證了帜平。
首先將cer和p12的兩個證書直接拖入工程里來幽告,
然后設(shè)置項目的info.plist,添加幾個設(shè)置,如圖:
這樣外部的環(huán)境我們就設(shè)置好了裆甩,然后就是調(diào)用afnetworking的方法了
NSString *certFilePath = [[NSBundle mainBundle] pathForResource:@"chain" ofType:@"cer"];
NSData *certData = [NSData dataWithContentsOfFile:certFilePath];
NSSet *certSet = [NSSet setWithObject:certData];
AFSecurityPolicy *policy = [AFSecurityPolicy policyWithPinningMode:AFSSLPinningModeCertificate withPinnedCertificates:certSet];
policy.allowInvalidCertificates = YES;
policy.validatesDomainName = NO;
_manager = [AFHTTPSessionManager manager];
_manager.securityPolicy = policy;
_manager.requestSerializer = [AFHTTPRequestSerializer serializer];
_manager.responseSerializer = [AFHTTPResponseSerializer serializer];
_manager.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"application/json", @"text/json", @"text/javascript",@"text/plain", nil];
_manager.requestSerializer.cachePolicy = NSURLRequestReloadIgnoringLocalCacheData;
[_manager setSessionDidBecomeInvalidBlock:^(NSURLSession * _Nonnull session, NSError * _Nonnull error) {
NSLog(@"setSessionDidBecomeInvalidBlock");
}];
//客戶端請求驗證 重寫 setSessionDidReceiveAuthenticationChallengeBlock 方法
__weak typeof(self)weakSelf = self;
[_manager setSessionDidReceiveAuthenticationChallengeBlock:^NSURLSessionAuthChallengeDisposition(NSURLSession*session, NSURLAuthenticationChallenge *challenge, NSURLCredential *__autoreleasing*_credential) {
NSURLSessionAuthChallengeDisposition disposition = NSURLSessionAuthChallengePerformDefaultHandling;
__autoreleasing NSURLCredential *credential =nil;
if([challenge.protectionSpace.authenticationMethod isEqualToString:NSURLAuthenticationMethodServerTrust]) {
if([weakSelf.manager.securityPolicy evaluateServerTrust:challenge.protectionSpace.serverTrust forDomain:challenge.protectionSpace.host]) {
credential = [NSURLCredential credentialForTrust:challenge.protectionSpace.serverTrust];
if(credential) {
disposition =NSURLSessionAuthChallengeUseCredential;
} else {
disposition =NSURLSessionAuthChallengePerformDefaultHandling;
}
} else {
disposition = NSURLSessionAuthChallengeCancelAuthenticationChallenge;
}
} else {
// client authentication
SecIdentityRef identity = NULL;
SecTrustRef trust = NULL;
NSString *p12 = [[NSBundle mainBundle] pathForResource:@"client"ofType:@"p12"];
NSFileManager *fileManager =[NSFileManager defaultManager];
if(![fileManager fileExistsAtPath:p12])
{
NSLog(@"client.p12:not exist");
}
else
{
NSData *PKCS12Data = [NSData dataWithContentsOfFile:p12];
if ([[weakSelf class]extractIdentity:&identity andTrust:&trust fromPKCS12Data:PKCS12Data])
{
SecCertificateRef certificate = NULL;
SecIdentityCopyCertificate(identity, &certificate);
const void*certs[] = {certificate};
CFArrayRef certArray =CFArrayCreate(kCFAllocatorDefault, certs,1,NULL);
credential =[NSURLCredential credentialWithIdentity:identity certificates:(__bridge NSArray*)certArray persistence:NSURLCredentialPersistencePermanent];
disposition =NSURLSessionAuthChallengeUseCredential;
}
}
}
*_credential = credential;
return disposition;
}];
如果你是在afnetworking上面封裝了一層冗锁,就想我一樣,那就在初始化AFHTTPSessionManager的時候把這些代碼調(diào)整一下嗤栓,寫入里面冻河。
然后添加一個類方法就可以了:
+(BOOL)extractIdentity:(SecIdentityRef*)outIdentity andTrust:(SecTrustRef *)outTrust fromPKCS12Data:(NSData *)inPKCS12Data {
OSStatus securityError = errSecSuccess;
//client certificate password
NSDictionary*optionsDictionary = [NSDictionary dictionaryWithObject:@"證書密碼"
forKey:(__bridge id)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*tempIdentity =NULL;
tempIdentity= CFDictionaryGetValue (myIdentityAndTrust,kSecImportItemIdentity);
*outIdentity = (SecIdentityRef)tempIdentity;
const void*tempTrust =NULL;
tempTrust = CFDictionaryGetValue(myIdentityAndTrust,kSecImportItemTrust);
*outTrust = (SecTrustRef)tempTrust;
} else {
NSLog(@"Failedwith error code %d",(int)securityError);
return NO;
}
return YES;
}
里面那個證書密碼,就是你的p12的證書密碼茉帅。
這樣我們就完成了Afnetworking3.0以后的https雙向驗證的功能了叨叙,但是我完成之后,發(fā)現(xiàn)并沒有驗證成功堪澎。所有就設(shè)置了一個全局?jǐn)帱c擂错,斷點停在了如圖:
然后我就打印了證書的data,發(fā)現(xiàn)data里面是有數(shù)據(jù)的樱蛤。那到底是什么原因呢钮呀,后來摸索一會兒發(fā)現(xiàn)是證書模式不對,就是如圖:
這里的模式不對昨凡,我們點擊進(jìn)去之后爽醋,發(fā)現(xiàn)他是一個枚舉一共三個:
AFSSLPinningModeNone: 代表客戶端無條件地信任服務(wù)器端返回的證書。
AFSSLPinningModePublicKey: 代表客戶端會將服務(wù)器端返回的證書與本地保存的證書中土匀,PublicKey的部分進(jìn)行校驗子房;如果正確,才繼續(xù)進(jìn)行。
AFSSLPinningModeCertificate: 代表客戶端會將服務(wù)器端返回的證書和本地保存的證書中的所有內(nèi)容证杭,包括PublicKey和證書部分田度,全部進(jìn)行校驗;如果正確解愤,才繼續(xù)進(jìn)行镇饺。
所以遇到這種情況,你又不知道是什么格式送讲,就一個個試試吧奸笤。
好了,就說道這里哼鬓,希望對你有幫助监右。