kCFStreamErrorDomainSSL -9802错误,但它的HTTPS网址

时间:2016-01-14 03:09:13

标签: ios cfnetwork

所以我知道ATS的内容以及如何编辑info.plist以允许HTTP。但是,网址为https://api.map.baidu.com/api?v=2。 0& ak = 1XjLLEhZhQNUzd93EjU5nOGQ& s = 1,这是一个HTTPS请求,但我仍然得到

NSURLSession/NSURLConnection HTTP load failed (kCFStreamErrorDomainSSL, -9802)

然后我在setenv("CFNETWORK_DIAGNOSTICS", "3", 1);中添加didFinishLaunchingWithOptions以启用详细日志。

在日志中,我找到了错误日志:

5510 Jan 14 10:52:01  MCompass[8549] <Notice>: CFNetwork Diagnostics [3:363] 10:52:01.458 {
5511     Response Error
5512     Request: <CFURLRequest 0x7fecf3cddcb0 [0x10aff37b0]> {url = https://api.map.baidu.com/api?v=2.0&ak=1XjLLEhZhQNUzd93EjU5nOGQ&s=1, cs = 0x0}
5513       Error: Error Domain=kCFErrorDomainCFNetwork Code=-1200 "(null)" UserInfo={_kCFStreamPropertySSLClientCertificateState=0,                                   kCFStreamPropertySSLPeerTrust=<SecTrustRef: 0x7fecf406bbf0>, _kCFNetworkCFStreamSSLErrorOriginalValue=-9802, _kCFStreamErrorDomainKey=3,                         _kCFStreamErrorCodeKey=-9802, kCFStreamPropertySSLPeerCertificates=<CFArray 0x7fecf406cda0 [0x10aff37b0]>{type = immutable, count = 3, values = (
5514                 0 : <cert(0x7fecf3fa80e0) s: baidu.com i: VeriSign Class 3 International Server CA - G3>
5515                 1 : <cert(0x7fecf3fa8920) s: VeriSign Class 3 International Server CA - G3 i: VeriSign Class 3 Public Primary Certification Authority - G5>
5516                 2 : <cert(0x7fecf4069fd0) s: VeriSign Class 3 Public Primary Certification Authority - G5 i: Class 3 Public Primary Certification Authority>
5517              )}}
5518     } [3:363]                                                                                                                                               
5519 Jan 14 10:52:01  MCompass[8549] <Notice>: CFNetwork Diagnostics [3:364] 10:52:01.459 {
5520                Did Fail
5521                  Loader: <CFMutableURLRequest 0x7fecf3cdd9f0 [0x10aff37b0]> {url = https://api.map.baidu.com/api?v=2.0&ak=1XjLLEhZhQNUzd93EjU5nOGQ&s=1, cs       = 0x0}
5522                   Error: Error Domain=kCFErrorDomainCFNetwork Code=-1200 "(null)" UserInfo={_kCFStreamPropertySSLClientCertificateState=0,                       kCFStreamPropertySSLPeerTrust=<SecTrustRef: 0x7fecf406bbf0>, _kCFNetworkCFStreamSSLErrorOriginalValue=-9802, _kCFStreamErrorDomainKey=3,                         _kCFStreamErrorCodeKey=-9802, kCFStreamPropertySSLPeerCertificates=<CFArray 0x7fecf406cda0 [0x10aff37b0]>{type = immutable, count = 3, values = (
5523                             0 : <cert(0x7fecf3fa80e0) s: baidu.com i: VeriSign Class 3 International Server CA - G3>
5524                             1 : <cert(0x7fecf3fa8920) s: VeriSign Class 3 International Server CA - G3 i: VeriSign Class 3 Public Primary Certification          Authority - G5>
5525                             2 : <cert(0x7fecf4069fd0) s: VeriSign Class 3 Public Primary Certification Authority - G5 i: Class 3 Public Primary                  Certification Authority>
5526                          )}}
5527     init to origin load: 0.00280595s
5528              total time: 0.447458s
5529             total bytes: 0
5530     } [3:364]

我很困惑,因为它的HTTPS请求,但仍有问题。我在Chrome上尝试了网址,它返回了一个有效的证书(我有像X509这样的证书)。但无法弄清楚它被阻挡的原因。

有人可以帮忙吗?预先感谢。将此域添加到ATS例外会有所帮助,但我不想添加它,因为它已经是HTTPS了!

更新

正在运行

/usr/bin/nscurl --ats-diagnostics -v "https://api.map.baidu.com/api?v=2.0&ak=1XjLLEhZhQNUzd93EjU5nOGQ&s=1"

将返回ALL PASS:

Xuans-MacBook-Pro:~ xuan$ /usr/bin/nscurl --ats-diagnostics -v "https://api.map.baidu.com/api?v=2.0&ak=1XjLLEhZhQNUzd93EjU5nOGQ&s=1"
Starting ATS Diagnostics

Configuring ATS Info.plist keys and displaying the result of HTTPS loads to https://api.map.baidu.com/api?v=2.0&ak=1XjLLEhZhQNUzd93EjU5nOGQ&s=1.
A test will "PASS" if URLSession:task:didCompleteWithError: returns a nil error.
================================================================================

Default ATS Secure Connection
---
ATS Default Connection
ATS Dictionary:
{
}
Result : PASS
---

================================================================================

Allowing Arbitrary Loads

---
Allow All Loads
ATS Dictionary:
{
    NSAllowsArbitraryLoads = true;
}
Result : PASS
---

================================================================================

Configuring TLS exceptions for api.map.baidu.com

---
TLSv1.2
ATS Dictionary:
{
    NSExceptionDomains =     {
        "api.map.baidu.com" =         {
            NSExceptionMinimumTLSVersion = "TLSv1.2";
        };
    };
}
Result : PASS
---

---
TLSv1.1
ATS Dictionary:
{
    NSExceptionDomains =     {
        "api.map.baidu.com" =         {
            NSExceptionMinimumTLSVersion = "TLSv1.1";
        };
    };
}
Result : PASS
---

---
TLSv1.0
ATS Dictionary:
{
    NSExceptionDomains =     {
        "api.map.baidu.com" =         {
            NSExceptionMinimumTLSVersion = "TLSv1.0";
        };
    };
}
Result : PASS
---

================================================================================

Configuring PFS exceptions for api.map.baidu.com

---
Disabling Perfect Forward Secrecy
ATS Dictionary:
{
    NSExceptionDomains =     {
        "api.map.baidu.com" =         {
            NSExceptionRequiresForwardSecrecy = false;
        };
    };
}
Result : PASS
---

================================================================================

Configuring PFS exceptions and allowing insecure HTTP for api.map.baidu.com

---
Disabling Perfect Forward Secrecy and Allowing Insecure HTTP
ATS Dictionary:
{
    NSExceptionDomains =     {
        "api.map.baidu.com" =         {
            NSExceptionAllowsInsecureHTTPLoads = true;
            NSExceptionRequiresForwardSecrecy = false;
        };
    };
}
Result : PASS
---

================================================================================

Configuring TLS exceptions with PFS disabled for api.map.baidu.com

---
TLSv1.2 with PFS disabled
ATS Dictionary:
{
    NSExceptionDomains =     {
        "api.map.baidu.com" =         {
            NSExceptionMinimumTLSVersion = "TLSv1.2";
            NSExceptionRequiresForwardSecrecy = false;
        };
    };
}
Result : PASS
---

---
TLSv1.1 with PFS disabled
ATS Dictionary:
{
    NSExceptionDomains =     {
        "api.map.baidu.com" =         {
            NSExceptionMinimumTLSVersion = "TLSv1.1";
            NSExceptionRequiresForwardSecrecy = false;
        };
    };
}
Result : PASS
---

---
TLSv1.0 with PFS disabled
ATS Dictionary:
{
    NSExceptionDomains =     {
        "api.map.baidu.com" =         {
            NSExceptionMinimumTLSVersion = "TLSv1.0";
            NSExceptionRequiresForwardSecrecy = false;
        };
    };
}
Result : PASS
---

================================================================================

Configuring TLS exceptions with PFS disabled and insecure HTTP allowed for api.map.baidu.com

---
TLSv1.2 with PFS disabled and insecure HTTP allowed
ATS Dictionary:
{
    NSExceptionDomains =     {
        "api.map.baidu.com" =         {
            NSExceptionAllowsInsecureHTTPLoads = true;
            NSExceptionMinimumTLSVersion = "TLSv1.2";
            NSExceptionRequiresForwardSecrecy = false;
        };
    };
}
Result : PASS
---

---
TLSv1.1 with PFS disabled and insecure HTTP allowed
ATS Dictionary:
{
    NSExceptionDomains =     {
        "api.map.baidu.com" =         {
            NSExceptionAllowsInsecureHTTPLoads = true;
            NSExceptionMinimumTLSVersion = "TLSv1.1";
            NSExceptionRequiresForwardSecrecy = false;
        };
    };
}
Result : PASS
---

---
TLSv1.0 with PFS disabled and insecure HTTP allowed
ATS Dictionary:
{
    NSExceptionDomains =     {
        "api.map.baidu.com" =         {
            NSExceptionAllowsInsecureHTTPLoads = true;
            NSExceptionMinimumTLSVersion = "TLSv1.0";
            NSExceptionRequiresForwardSecrecy = false;
        };
    };
}
Result : PASS
---

================================================================================

2 个答案:

答案 0 :(得分:1)

正如this answer所述,只需通过HTTPS访问您的API网址并不意味着它符合Apple的ATS。我也使用nscurl,但我认为该工具还不够成熟,有时可能效率很低。

SSL Labs测试更好,详细,imho。它可以帮助您找出SSL配置中缺少的内容。

请注意ATS requires至少TLS 1.2和完美前向保密密码套件。

答案 1 :(得分:0)

我不知道这是否能解决您的问题,但最近我遇到了类似的问题。在我的情况下,我还有一台服务器,每个服务器都通过了nscurl --ats-diagnostics并且通过PASS,但是在应用程序中出现了ATS -9802错误。服务器使用TLS 1.2版,具有前向保密性,使用了良好的密码套件,并拥有SHA256证书。

SSL实验室页面有一个指向答案的提示 - 它说一切都很好,但SSL链不完整。服务器稍有错误配置,因为它提供了正确的证书,但没有连接到根证书(应该已经在客户端上)所需的中间证书。有可以下载中间证书的地方的指针,因此SSL Labs页面就是这样做的,结果只是将等级降级为“B”。但是,这意味着客户端实现也需要能够自己下载中间证书 - 并非所有实现都可以。

在我的情况下,因为这是在测试/开发环境中,我们遇到了其他服务器偶尔出错配置的问题,我们将AFSecurityPolicy属性validatesDomainName设置为NO,因为它解决了这些其他问题(它在生产中当然是肯定的)。但是,这也成为了这个服务器的设置,这不是绝对必要的。这反过来意味着AFSecurityPolicy在配置SecPolicyCreateBasicX509()时使用SecPolicyCreateSSL()而不是SecTrustRef。除了SecTrustGetNetworkFetchAllowed()州的标题文档外,这一点基本上没问题:

  

默认情况下,如果启用了缺失证书的网络提取       信任评估包括SSL策略,否则被禁用。

所以,这就是问题所在。 nscurl将使用SSL策略,因此它会下载中间证书并正常工作。但是,当该标志被关闭时,ATS将在运行时失败,因为SecTrustEvaluate()将返回kSecTrustResultRecoverableTrustFailure,没有进一步干预将被视为失败。如果我将validatesDomainName设置回YES,则它开始工作(在此服务器上)。或者,如果在添加策略后对SecTrustRef有句柄,则可以调用

SecTrustSetNetworkFetchAllowed(trustRef, true);

因为即使使用X509策略,它也将允许App Transport Security获取中间证书。或者,您可以修复服务器配置以提供整个证书链,但不包括根证书,就像它应该的那样。

编辑: SecTrustSetNetworkFetchAllowed来电仅适用于iOS10。对于iOS9,我不得不使用SSL策略调用SecTrustSetPolicies(),即重置策略列表 - 这是ATS在iOS9上获取丢失证书的唯一方法。