美文网首页
通过无效的HTTPS证校验

通过无效的HTTPS证校验

作者: 阳光下的灰尘 | 来源:发表于2022-03-03 11:24 被阅读0次

1、wkwebview 通过无效的HTTPS证校验

实现协议方法 WKNavigationDelegate

- (void)webView:(WKWebView *)webView didReceiveAuthenticationChallenge:(NSURLAuthenticationChallenge *)challenge completionHandler:(void (^)(NSURLSessionAuthChallengeDisposition disposition, NSURLCredential * _Nullable credential))completionHandler {
    if ([challenge.protectionSpace.authenticationMethod isEqualToString:NSURLAuthenticationMethodServerTrust]) {
        NSURLCredential *card = [[NSURLCredential alloc]initWithTrust:challenge.protectionSpace.serverTrust];
        completionHandler(NSURLSessionAuthChallengeUseCredential,card);
    }
}

2、NSURLSession 通过无效的HTTPS证校验

NSURLSession 创建时 delegate:self 要有代理,并实现代理NSURLSessionDelegate的方法

NSURLSession *session = [NSURLSession sessionWithConfiguration:configuration delegate:self delegateQueue:[[NSOperationQueue alloc] init]];
        NSURLSessionDataTask *task = [session dataTaskWithRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:zipUrl]] completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) {
            NSLog(@"发起了证书请求");

        }];
        [task resume];

实现协议方法 NSURLSessionDelegate

- (void)URLSession:(NSURLSession *)session didReceiveChallenge:(NSURLAuthenticationChallenge *)challenge completionHandler:(void (^)(NSURLSessionAuthChallengeDisposition, NSURLCredential * _Nullable))completionHandler {

    NSURLSessionAuthChallengeDisposition disposition = NSURLSessionAuthChallengePerformDefaultHandling;
    __block NSURLCredential *credential = nil;

    if ([challenge.protectionSpace.authenticationMethod isEqualToString:NSURLAuthenticationMethodServerTrust] && ([challenge.protectionSpace.host hasSuffix:@"bctstgsit1.ftcwifi.com"] || [challenge.protectionSpace.host hasSuffix:@"bctstgusmf.ftcwifi.com"])) {
        credential = [NSURLCredential credentialForTrust:challenge.protectionSpace.serverTrust];
        if (credential) {
            disposition = NSURLSessionAuthChallengeUseCredential;
        } else {
            disposition = NSURLSessionAuthChallengePerformDefaultHandling;
        }
    } else {
        disposition = NSURLSessionAuthChallengePerformDefaultHandling;
    }

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

3、AFN(AFNetworking) 通过无效的HTTPS证校验

修改 AFURLSessionManager 文件 中的 URLSession:task:didReceiveChallenge:completionHandler: 方法

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

    if (self.authenticationChallengeHandler) {
        id result = self.authenticationChallengeHandler(session, task, challenge, completionHandler);
        if (result == nil) {
            return;
        } else if ([result isKindOfClass:NSError.class]) {
            objc_setAssociatedObject(task, AuthenticationChallengeErrorKey, result, OBJC_ASSOCIATION_RETAIN);
            disposition = NSURLSessionAuthChallengeCancelAuthenticationChallenge;
        } else if ([result isKindOfClass:NSURLCredential.class]) {
            credential = result;
            disposition = NSURLSessionAuthChallengeUseCredential;
        } else if ([result isKindOfClass:NSNumber.class]) {
            disposition = [result integerValue];
            NSAssert(disposition == NSURLSessionAuthChallengePerformDefaultHandling || disposition == NSURLSessionAuthChallengeCancelAuthenticationChallenge || disposition == NSURLSessionAuthChallengeRejectProtectionSpace, @"");
            evaluateServerTrust = disposition == NSURLSessionAuthChallengePerformDefaultHandling && [challenge.protectionSpace.authenticationMethod isEqualToString:NSURLAuthenticationMethodServerTrust];
        } else {
            @throw [NSException exceptionWithName:@"Invalid Return Value" reason:@"The return value from the authentication challenge handler must be nil, an NSError, an NSURLCredential or an NSNumber." userInfo:nil];
        }
    } else {
        evaluateServerTrust = [challenge.protectionSpace.authenticationMethod isEqualToString:NSURLAuthenticationMethodServerTrust];
    }

    /// https://www.baidu.com 不使用  证书校验
    if ([challenge.protectionSpace.authenticationMethod isEqualToString:NSURLAuthenticationMethodServerTrust] &&
        ([challenge.protectionSpace.host hasSuffix:@"www.baidu.com"] || [challenge.protectionSpace.host hasSuffix:@"www.baidu.com"])) {
           // 不管证书是否有效都使用
           [challenge.sender useCredential:[NSURLCredential credentialForTrust:challenge.protectionSpace.serverTrust] forAuthenticationChallenge:challenge];
           disposition = NSURLSessionAuthChallengeUseCredential;
           credential = [NSURLCredential credentialForTrust:challenge.protectionSpace.serverTrust];
           if (completionHandler) {
               completionHandler(disposition, credential);
           }
       }
       else {
           if (evaluateServerTrust) {
               if ([self.securityPolicy evaluateServerTrust:challenge.protectionSpace.serverTrust forDomain:challenge.protectionSpace.host]) {
                   disposition = NSURLSessionAuthChallengeUseCredential;
                   credential = [NSURLCredential credentialForTrust:challenge.protectionSpace.serverTrust];
               } else {
                   objc_setAssociatedObject(task, AuthenticationChallengeErrorKey,
                                            [self serverTrustErrorForServerTrust:challenge.protectionSpace.serverTrust url:task.currentRequest.URL],
                                            OBJC_ASSOCIATION_RETAIN);
                   disposition = NSURLSessionAuthChallengeCancelAuthenticationChallenge;
               }
           }

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

相关文章

网友评论

      本文标题:通过无效的HTTPS证校验

      本文链接:https://www.haomeiwen.com/subject/zqnakrtx.html