且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

iOS 9.3:发生SSL错误,无法与服务器建立安全连接

更新时间:2022-03-13 16:58:53

我认为您尝试连接的服务器具有无效证书或与ECC,密码等的iOS 9标准不匹配。

I presume the server you are trying to connect has invalid certificates or doesn't match up with the iOS 9 standards for ECC, Ciphers etc.


  • 如果您使用的是高级网络API-NSURLSession,NSURLConnection或其他任何分层 - 您无法直接控制所提供的密码套件由客户。这些API使用自己的内部逻辑选择一组cypher套件。

  • If you’re using high-level networking APIs—NSURLSession, NSURLConnection, or anything layered on top of those—you don’t have direct control over the cypher suites offered by the client. Those APIs choose a set of cypher suites using their own internal logic.

如果您正在使用较低级别的网络API-CFSocketStream,通过其NSStream和CFStream API以及任何低于此值的内容 - 您可以明确选择该集合您想要使用的密码套件。如何执行此操作取决于具体的API。

If you’re using lower-level networking APIs—CFSocketStream, via its NSStream and CFStream APIs, and anything lower than that—you can explicitly choose the set of cypher suites you want to use. How you do this depends on the specific API.

标准做法是:


  1. 创建流对

  1. create the stream pair

为TLS配置

使用kCFStreamPropertySSLContext属性获取安全传输上下文

get the Secure Transport context using the kCFStreamPropertySSLContext property

在该上下文中配置特定属性

configure specific properties in that context

打开流

你可以看到一个这样的例子TLSTool示例代码。具体来说,请查看 TLSToolServer 类,您可以在其中查看此序列。

You can see an example of this in the TLSTool sample code. Specifically, look at the TLSToolServer class, where you can see exactly this sequence.

在非常短的上下文中,您希望以这种方式配置流然而,在Alamofire的情况下,你可以通过以下方式直接执行此操作:

In a very short context, you want to configure the stream in such a way that it bypasses the security, however, in the case of Alamofire you can do this directly by:

func bypassAuthentication() {
        let manager = Alamofire.Manager.sharedInstance
        manager.delegate.sessionDidReceiveChallenge = { session, challenge in
            var disposition: NSURLSessionAuthChallengeDisposition = .PerformDefaultHandling
            var credential: NSURLCredential?
            if challenge.protectionSpace.authenticationMethod == NSURLAuthenticationMethodServerTrust {
                disposition = NSURLSessionAuthChallengeDisposition.UseCredential
                credential = NSURLCredential(forTrust: challenge.protectionSpace.serverTrust!)
            } else {
                if challenge.previousFailureCount > 0 {
                    disposition = .CancelAuthenticationChallenge
                } else {
                    credential = manager.session.configuration.URLCredentialStorage?.defaultCredentialForProtectionSpace(challenge.protectionSpace)
                    if credential != nil {
                        disposition = .UseCredential
                    }
                }
            }
            return (disposition, credential)
        }
    }

如果有帮助请告诉我。
谢谢!

let me know if that helps. Thank you!