【问题标题】:SSL failing with Allow Arbitrary Loads = falseSSL 因 Allow Arbitrary Loads = false 而失败
【发布时间】:2021-08-17 10:06:35
【问题描述】:

根据我的要求,我不应该Allow Arbitrary Loads = true。所以我设置为false。

我允许我的 URLsession 委托上的信任证书。

我的网址https://sample-app.10.names.io

代码:

public func urlSession(_ session: URLSession, didReceive challenge: URLAuthenticationChallenge, completionHandler: @escaping (URLSession.AuthChallengeDisposition, URLCredential?) -> Void) {
       //Trust the certificate even if not valid
       let urlCredential = URLCredential(trust: challenge.protectionSpace.serverTrust!)
       completionHandler(.useCredential, urlCredential)
    }

我的错误:

Code=-1200 "发生 SSL 错误,无法与服务器建立安全连接。" UserInfo={NSLocalizedRecoverySuggestion=您还是要连接到服务器吗?,

如果我使 Allow Arbitrary Loads = true,那么只有它可以工作。但根据我的要求,我不应该更改为 true。任何建议都会有所帮助。

谢谢

更新:

我也在下面尝试过:

<key>NSAppTransportSecurity</key>
<dict>
    <key>NSExceptionDomains</key>
    <dict>
        <key>https://sample-app.10.names.io</key>
        <dict>
            <!--Include to allow subdomains-->
            <key>NSIncludesSubdomains</key>
            <true/>
            <!--Include to allow HTTP requests-->
            <key>NSTemporaryExceptionAllowsInsecureHTTPLoads</key>
            <true/>
            <!--Include to specify minimum TLS version-->
            <key>NSTemporaryExceptionMinimumTLSVersion</key>
            <string>TLSv1.1</string>
        </dict>
    </dict>
</dict>

【问题讨论】:

    标签: ios swift iphone xcode ssl


    【解决方案1】:

    可以使用终端命令

    nscurl --ats-diagnostics --verbose https://sample-app.10.names.io

    测试您的服务器的 ATS 合规性。

    这样做表明您的服务器仅在 perfect forward secrecy 被禁用时通过。您的服务器似乎不支持 ECDHE 密码。

    您可以通过在您的 ATS 配置异常域中指定 NSExceptionRequiresForwardSecrecy 来配置 ATS 以忽略完美的前向保密要求,但实际上您应该修补您的服务器以使用更新的 TLS 代码。否则很容易受到重放 MITM 攻击。

    【讨论】:

      【解决方案2】:

      如果您在项目中积极使用Alamofire,我建议您使用内置的Session,可以轻松配置为使用您的服务器信任证书。这是一些关于如何在我的一个项目中设置它的代码。

      class SessionManagerProvider {
      
      // MARK: - State
      
      let hosts: [String]
      let disableEvaluation: Bool
      
      // MARK: - Init
      
      init(urls: [URL], disableEvaluation: Bool = false) {
          hosts = urls.compactMap { $0.host }
          self.disableEvaluation = disableEvaluation
      }
      
      // MARK: - Factory
      
      func make() -> Session {
          // Configure network client with SSL pinning.
          let configuration = URLSessionConfiguration.af.default
          configuration.timeoutIntervalForRequest = Constants.Backend.timeoutIntervalForRequest
          configuration.timeoutIntervalForResource = Constants.Backend.timeoutIntervalForResource
          // Allow more connections than API requests to avoid an issue, when URLSession starts to
          // time-out requests when there are too many connections.
          configuration.httpMaximumConnectionsPerHost = Constants.maxConcurrentApiCalls * 2
          let policies = serverTrustPolicies(disableEvaluation: disableEvaluation)
          let securityManager = ServerTrustManager(evaluators: policies)
          let sessionManager = Session(configuration: configuration, serverTrustManager: securityManager)
          return sessionManager
      }
      
      private func serverTrustPolicies(disableEvaluation: Bool) -> [String: ServerTrustEvaluating] {
          var policies: [String: ServerTrustEvaluating] = [:]
      
          for host in hosts {
              if disableEvaluation {
                  policies[host] = DisabledTrustEvaluator()
              } else {
                  policies[host] = PublicKeysTrustEvaluator(
                      performDefaultValidation: true,
                      validateHost: true
                  )
              }
          }
      
          return policies
      }
      

      }

      【讨论】:

      • 我将证书与服务器进行了比较,然后我作为受信任的证书传递。但是如果我改变我的Allow Arbitrary Loads = true 那么它就可以工作了。但根据我的要求,它必须是FALSE
      • 我明白你的意思。 TRUE 表示您允许不安全的 HTTP(没有 S)连接到您的应用,这使其不安全。
      • 是的,但我允许证书作为受信任的证书。但仍然没有成功。
      猜你喜欢
      • 2022-11-08
      • 2018-11-09
      • 2014-02-18
      • 2018-09-29
      • 1970-01-01
      • 2017-01-27
      • 2021-11-25
      • 1970-01-01
      • 2014-12-10
      相关资源
      最近更新 更多