首页 > 移动平台 > 详细

iOS在https中验证CA签名

时间:2020-02-13 20:32:23      阅读:76      评论:0      收藏:0      [点我收藏+]
- (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签名

原文:https://www.cnblogs.com/yuxiaoyiyou/p/12304664.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!