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