NSURLSession 的 IOS9 SSL 错误

标签 ios authentication ssl x509certificate nsurlsession

我开发了一个 IOS 应用程序,它使用 HTTPs 与服务器通信(我也是服务器的开发人员,它嵌入了一个 Tomcat 服务器)。

此应用程序在 IOS8 上没有任何问题,但在 IOS9 上却没有。

发送 HTTPs 请求时出现以下错误:

Session download has failed : Error Domain=NSURLErrorDomain Code=-1200 "An SSL error has occurred and a secure connection to the server cannot be made." UserInfo=0x7fa9c44b2e40 {NSErrorFailingURLStringKey=https://127.0.0.1:8443/MyServer/MyApp, NSLocalizedRecoverySuggestion=Would you like to connect to the server anyway?, _kCFStreamErrorDomainKey=3, NSErrorFailingURLKey=https://127.0.0.1:8443/MyServer/MyApp, _kCFStreamErrorCodeKey=-9802, NSLocalizedDescription=An SSL error has occurred and a secure connection to the server cannot be made.}

根据 Apple 文档,当错误 Code=-1200 发生时,这是因为 TLSv1.1 而不是 TLSv1.2。我已使用 openSSL 命令进行验证,并且我的服务器使用的是 TLSv1.2。

当在 info.plist 中设置属性 NSAllowsArbitraryLoads = true 时,错误消失,但我不想保持这样。

我的服务器使用的是自签名证书,这可能是问题的原因? 下面是我用于 didReceiveChallenge 的代码

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

[RequestURLUtilities dumpChallenge:challenge];


if (challenge.protectionSpace != Nil) {
    NSURLProtectionSpace *protection = challenge.protectionSpace;

    SecTrustRef sslState = protection.serverTrust;
    if (sslState == Nil) {
        NSLog(@"%s Warning: empty serverTrust",__PRETTY_FUNCTION__);
    }


    if ([protection.authenticationMethod isEqualToString:NSURLAuthenticationMethodServerTrust]) {

        NSLog(@"%s => NSURLAuthenticationMethodServerTrust", __PRETTY_FUNCTION__);
        // warning normaly I should check the validity of the SecTrustRef before to trust
        NSURLCredential* credential = [NSURLCredential credentialForTrust:sslState];

        // Put in command for test

       completionHandler(NSURLSessionAuthChallengeUseCredential, credential);

    } else {
        NSLog(@"%s => Called for another challenge", __PRETTY_FUNCTION__);
       completionHandler(NSURLSessionAuthChallengePerformDefaultHandling, NULL);
    }

}

我应该先创建自己的 CA,然后再创建自己的证书吗?如何进行?什么不见​​了?任何帮助将不胜感激。

塞巴斯蒂安。

最佳答案

在与 Apple 支持讨论后,问题是由自签名证书引起的。

ATS 只信任由知名 CA 签名的证书,拒绝所有其他证书。因此,使用自签名证书的唯一解决方案是使用 NSExceptionDomains 设置异常。

关于NSURLSession 的 IOS9 SSL 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31505808/

相关文章:

ios - 是否可以在默认设置中获取 USE TOUCHID FOR iPhone 解锁的值

authentication - OpenID Connect 和 IDP 发起的 SSO

使用多个模块/文件的登录页面和主视图(带有嵌套 View )的 Angular.io 路由

用于仅 TLS 连接协商的 Node.js TLS 选项

python - Wireshark 不显示带有 Python SSL 套接字的 SSL 数据包

ios - 隐藏/删除 UIAlertView 时调用的通知或委托(delegate)

ios - 暂停/播放 AVPlayer 时出现问题

java - 如何使用自签名证书运行 Java HTTP 服务器并信任浏览器?

ios - UIScrollView 的内容 View 忽略了 Xcode 6 上主视图的等宽约束

iphone - 编译到设备时没有任何反应