iOS在https中验证CA签名

- (void)URLSession:(NSURLSession *)session
didReceiveChallenge:(NSURLAuthenticationChallenge *)challenge
 completionHandler:(void (^)(NSURLSessionAuthChallengeDisposition disposition, NSURLCredential *credential))completionHandler
{
    NSURLSessionAuthChallengeDisposition disposition = NSURLSessionAuthChallengePerformDefaultHandling;
    __block NSURLCredential *credential = nil;

    if (self.sessionDidReceiveAuthenticationChallenge) {
        disposition = self.sessionDidReceiveAuthenticationChallenge(session, challenge, &credential);
    } else {
        if ([challenge.protectionSpace.authenticationMethod isEqualToString:NSURLAuthenticationMethodServerTrust]) {
            if ([self.securityPolicy evaluateServerTrust:challenge.protectionSpace.serverTrust forDomain:challenge.protectionSpace.host]) {
                credential = [NSURLCredential credentialForTrust:challenge.protectionSpace.serverTrust];
                SecTrustRef serverT = challenge.protectionSpace.serverTrust;
                for (CFIndex index = 0; index < SecTrustGetCertificateCount(serverT); index++) {
                    SecCertificateRef secC = SecTrustGetCertificateAtIndex(serverT, index);
                    CFStringRef name;
                    SecCertificateCopyCommonName(secC, &name);
                    NSString *nameText = (__bridge NSString *)(name);
                    NSLog(@"\\\\%@",nameText);
                }
                
                if (credential) {
                    disposition = NSURLSessionAuthChallengeUseCredential;
                } else {
                    disposition = NSURLSessionAuthChallengePerformDefaultHandling;
                }
            } else {
                disposition = NSURLSessionAuthChallengeCancelAuthenticationChallenge;
            }
        } else {
            disposition = NSURLSessionAuthChallengePerformDefaultHandling;
        }
    }

    if (completionHandler) {
        completionHandler(disposition, credential);
    }
}

上述NSLog输出为以下字符串内容,从下到上

 

iOS在https中验证CA签名

 

iOS在https中验证CA签名

上一篇:移动端适配之路的一步步了解


下一篇:adb获取当前打开的app的包名