為您解碼網(wǎng)站建設(shè)的點(diǎn)點(diǎn)滴滴
發(fā)表日期:2018-03 文章編輯:小燈 瀏覽次數(shù):2579
AFNetworking 單向校驗(yàn)證書(shū)
+ (AFSecurityPolicy*)customPrivateSecurityPolicy?{
? ? // 先導(dǎo)入證書(shū)
? ? NSString *cerPath = [[NSBundle mainBundle] pathForResource:@"private" ofType:@"cer"];//證書(shū)的路徑
? ? NSData *certData = [NSData dataWithContentsOfFile:cerPath];
?? // AFSSLPinningModeCertificate 使用證書(shū)驗(yàn)證模式
? ? AFSecurityPolicy *securityPolicy = [AFSecurityPolicy policyWithPinningMode:AFSSLPinningModeCertificate];
? ? // allowInvalidCertificates 是否允許無(wú)效證書(shū)(也就是自建的證書(shū)),默認(rèn)為NO
? ? // 如果是需要驗(yàn)證自建證書(shū),需要設(shè)置為YES
?? ?securityPolicy.allowInvalidCertificates = YES;
? ? //validatesDomainName 是否需要驗(yàn)證域名,默認(rèn)為YES;
? ? //假如證書(shū)的域名與你請(qǐng)求的域名不一致,需把該項(xiàng)設(shè)置為NO;如設(shè)成NO的話(huà),即服務(wù)器使用其他可信任機(jī)構(gòu)頒發(fā)的證書(shū),也可以建立連接,這個(gè)非常危險(xiǎn),建議打開(kāi)。
? ? //置為NO,主要用于這種情況:客戶(hù)端請(qǐng)求的是子域名,而證書(shū)上的是另外一個(gè)域名。因?yàn)镾SL證書(shū)上的域名是獨(dú)立的,假如證書(shū)上注冊(cè)的域名是www.google.com,那么mail.google.com是無(wú)法驗(yàn)證通過(guò)的;當(dāng)然,有錢(qián)可以注冊(cè)通配符的域名*.google.com,但這個(gè)還是比較貴的。
? ? //如置為NO,建議自己添加對(duì)應(yīng)域名的校驗(yàn)邏輯。
? ? securityPolicy.validatesDomainName = NO;
?? ?securityPolicy.pinnedCertificates = @[certData];
? ? return securityPolicy;
}
測(cè)試:
?1.獲得請(qǐng)求管理者
AFHTTPRequestOperationManager *manage = [AFHTTPRequestOperationManager manager];
2.返回結(jié)果類(lèi)型:
manage.responseSerializer = [AFHTTPResponseSerializer serializer];
?3. 加上這行代碼,https ssl 驗(yàn)證。
?[manage setSecurityPolicy:[self customPrivateSecurityPolicy]];
重點(diǎn)在于處理NSURLConnection的didReceiveAuthenticationChallenge代理方法,對(duì)CA文件進(jìn)行驗(yàn)證,并建立信任連接。//https 證書(shū)驗(yàn)證
- (BOOL)connection:(NSURLConnection *)connection canAuthenticateAgainstProtectionSpace:(NSURLProtectionSpace *)prote {
? ? ///NO 系統(tǒng)進(jìn)行管理? YES 調(diào)用connection: didReceiveAuthenticationChallenge 進(jìn)行驗(yàn)證
? ? return YES;
}
- (void)connection:(NSURLConnection *)connection didReceiveAuthenticationChallenge:(NSURLAuthenticationChallenge *)challenge {
? ? // 獲取der格式CA證書(shū)路徑
? ? NSString *certPath = [[NSBundle mainBundle] ?pathForResource:@"CustomCA" ofType:@"der"];
? ? // 提取二進(jìn)制內(nèi)容
?? ?NSData *derCA = [NSData dataWithContentsOfFile:certPath];
? ? // 根據(jù)二進(jìn)制內(nèi)容提取證書(shū)信息
? ? SecCertificateRef caRef = SecCertificateCreateWithData(NULL, (__bridge CFDataRef)derCA);
? ? // 形成鑰匙鏈
? ? NSArray * chain = [NSArray arrayWithObject:(__bridge id)(caRef)];
? ? CFArrayRef caChainArrayRef = CFBridgingRetain(chain);
? ? // 取出服務(wù)器證書(shū)
? ? SecTrustRef trust = [[challenge protectionSpace] serverTrust];
? ? SecTrustResultType trustResult = kSecTrustResultInvalid;
? ? // 設(shè)置為我們自有的CA證書(shū)鑰匙連
? ? int err = SecTrustSetAnchorCertificates(trust, caChainArrayRef);
? ? if (err == noErr) {
? ? ? ? // 用CA證書(shū)驗(yàn)證服務(wù)器證書(shū)
? ? ? ? err = SecTrustEvaluate(trust, &trustResult);
? ? }
? ? // 檢查結(jié)果 kSecTrustResultConfirm 當(dāng)值是這個(gè)時(shí)應(yīng)詢(xún)問(wèn)用戶(hù)許可,但這個(gè)值在iOS7.0后廢棄了,系統(tǒng)支持到7.0 可以不管理這個(gè)值
? ? BOOL trusted = (err == noErr) && ((trustResult == kSecTrustResultProceed) || (trustResult == kSecTrustResultUnspecified));
#warning 使用根證書(shū)驗(yàn)證存在服務(wù)器證書(shū)如果不是我們使用的GlobalSignRootCA簽發(fā)的這個(gè)子證書(shū)簽發(fā)的(另一個(gè)子證書(shū)簽發(fā))也能校驗(yàn)過(guò)
? ? if (trusted) {
? ? ? ? [challenge.sender useCredential:[NSURLCredential credentialForTrust:challenge.protectionSpace.serverTrust] forAuthenticationChallenge:challenge];
? ? } else {
? ? ? ? //驗(yàn)證證書(shū)不通過(guò)
? ? ? ? //當(dāng)執(zhí)行到這的時(shí)候應(yīng)該去排查一下具體為什么沒(méi)過(guò),可以看一下是否換證書(shū)了,是否是用GlobalSignRootCA或其直接簽發(fā)的子證書(shū)簽發(fā)的
? ? ? ? //現(xiàn)在的驗(yàn)證的全部是這個(gè)機(jī)構(gòu)簽發(fā)的證書(shū)。這個(gè)機(jī)構(gòu)換證書(shū)的幾率很小,因?yàn)轵?yàn)證書(shū)是用根證書(shū)驗(yàn)的,簽發(fā)機(jī)構(gòu)要換 就要把所有證書(shū)換掉 一般不會(huì)這么做,
? ? ? ? [challenge.sender cancelAuthenticationChallenge:challenge];
? ? }
}