iOS https请求出现Default TLS Trust evaluation failed问题

公司服务端更新了https,而使用的是自签名的证书,导致iOS开发过程中调用HTTPS接口时,证书不被信任,出现了Default TLS Trust evaluation failed。

解决办法如下:

NSURLSession初始化时添加代理,然后在代理方法中做证书相关处理

NSURL *url = [NSURL URLWithString:[mutableStringUrl stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLQueryAllowedCharacterSet]]];
NSURLRequest *request = [NSURLRequest requestWithURL:url cachePolicy:NSURLRequestReloadIgnoringLocalCacheData timeoutInterval:10];
NSURLSession *session = [NSURLSession sessionWithConfiguration:[NSURLSessionConfiguration defaultSessionConfiguration] delegate:self delegateQueue:[[NSOperationQueue alloc]init]];
NSURLSessionDataTask * dataTask = [session dataTaskWithRequest:request completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) {
    if (error) {
        failureBlock(error);
     } else {
        NSString * result = [[NSString alloc] initWithData:data  encoding:NSUTF8StringEncoding];
        successBlock(result);
     }
}];
[dataTask resume];

解决方式有两种:

1.跳过TLS认证,信任所有证书
2.设置白名单,把签名证书加到工程中,做校验

- (void)URLSession:(NSURLSession *)session didReceiveChallenge:(NSURLAuthenticationChallenge *)challenge completionHandler:(void (^)(NSURLSessionAuthChallengeDisposition, NSURLCredential * _Nullable))completionHandler {
    1.方法一
    if([challenge.protectionSpace.authenticationMethod isEqualToString:NSURLAuthenticationMethodServerTrust]){
        NSURLCredential *credential = [NSURLCredential credentialForTrust:challenge.protectionSpace.serverTrust];
        if(completionHandler)
            completionHandler(NSURLSessionAuthChallengeUseCredential,credential);
    }
    
    2.方法二
//    SecTrustRef servertrust = challenge.protectionSpace.serverTrust;
//    SecCertificateRef certi= SecTrustGetCertificateAtIndex(servertrust, 0);
//    NSData *certidata = CFBridgingRelease(CFBridgingRetain(CFBridgingRelease(SecCertificateCopyData(certi))));
//    NSString *path = [[NSBundle mainBundle] pathForResource:@"zwp" ofType:@"cer"];
//    NSLog(@"证书 : %@",path);
//    NSData *localCertiData = [NSData dataWithContentsOfFile:path];
//    if ([certidata isEqualToData:localCertiData]) {
//        NSURLCredential *credential = [[NSURLCredential alloc] initWithTrust:servertrust];
//        [challenge.sender useCredential:credential forAuthenticationChallenge:challenge];
//        completionHandler(NSURLSessionAuthChallengeUseCredential, credential);
//        NSLog(@"服务端证书认证通过");
//    }else {
//        completionHandler(NSURLSessionAuthChallengeCancelAuthenticationChallenge, nil);
//        NSLog(@"服务端认证失败");
//    }
    
}
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • iOS安全系列之一:HTTPS 2014-10-21 如何打造一个安全的App?这是每一个移动开发者必须面对的问题...
    不作不会死阅读 763评论 0 4
  • 原文地址:iOS安全系列之一:HTTPS 如何打造一个安全的App?这是每一个移动开发者必须面对的问题。在移动Ap...
    violafa阅读 876评论 0 2
  • 作者:Jaminzzhang 如何打造一个安全的App?这是每一个移动开发者必须面对的问题。在移动App开发领域,...
    __Lex阅读 813评论 0 5
  • 如何打造一个安全的App?这是每一个移动开发者必须面对的问题。在移动App开发领域,开发工程师对于安全方面的考虑普...
    番薯大佬阅读 300评论 0 0
  • 如何打造一个安全的App?这是每一个移动开发者必须面对的问题。在移动App开发领域,开发工程师对于安全方面的考虑普...
    半岛夏天阅读 1,060评论 0 1