ios - 使用自签名根证书的 SSLHandshake 失败 (-9808)

标签 ios ssl ssl-certificate afnetworking afnetworking-2

我有带有 REST API 的自定义硬件设备。设备具有自签名根证书。我正在尝试进行简单的 GET 调用,但我的所有请求都失败了:

CFNetwork SSLHandshake failed (-9808)
NSURLSession/NSURLConnection HTTP load failed (kCFStreamErrorDomainSSL, -9808)

更多说明:

CFNetwork SSLHandshake failed (-9808)
NSURLSession/NSURLConnection HTTP load failed (kCFStreamErrorDomainSSL, -9808)
Error Error Domain=NSURLErrorDomain Code=-1202 "The certificate for this server is invalid. You might be connecting to a server that is pretending to be “192.168.68.97” which could put your confidential information at risk." UserInfo={NSURLErrorFailingURLPeerTrustErrorKey=<SecTrustRef: 0x7fa2fb219530>, NSLocalizedRecoverySuggestion=Would you like to connect to the server anyway?, _kCFStreamErrorDomainKey=3, _kCFStreamErrorCodeKey=-9808, NSErrorPeerCertificateChainKey=<CFArray 0x7fa2fb20ffd0 [0x1016d37b0]>{type = immutable, count = 1, values = (
    0 : <cert(0x7fa2f954a2e0) s: selfSignedRootCertificate i: selfSignedRootCertificate>
)}, NSUnderlyingError=0x7fa2fb3018e0 {Error Domain=kCFErrorDomainCFNetwork Code=-1202 "(null)" UserInfo={_kCFStreamPropertySSLClientCertificateState=0, kCFStreamPropertySSLPeerTrust=<SecTrustRef: 0x7fa2fb219530>, _kCFNetworkCFStreamSSLErrorOriginalValue=-9808, _kCFStreamErrorDomainKey=3, _kCFStreamErrorCodeKey=-9808, kCFStreamPropertySSLPeerCertificates=<CFArray 0x7fa2fb20ffd0 [0x1016d37b0]>{type = immutable, count = 1, values = (
    0 : <cert(0x7fa2f954a2e0) s: selfSignedRootCertificate i: selfSignedRootCertificate>
)}}}, NSLocalizedDescription=The certificate for this server is invalid. You might be connecting to a server that is pretending to be “192.168.68.97” which could put your confidential information at risk., NSErrorFailingURLKey=https://192.168.68.97/api/switch/ctrl?switch=1&action=on, NSErrorFailingURLStringKey=https://192.168.68.97/api/switch/ctrl?switch=1&action=on, NSErrorClientCertificateStateKey=0}

我在 AFHTTPSessionManger 子类中的代码是

  NSData *myCertificate = [NSData dataWithContentsOfFile: [[NSBundle mainBundle] pathForResource:@"selfSignedRootCertificate" ofType:@"cer"]];
    AFSecurityPolicy *securityPolicy = [AFSecurityPolicy policyWithPinningMode:AFSSLPinningModePublicKey];
    securityPolicy.validatesDomainName = NO;
    securityPolicy.allowInvalidCertificates = YES;
    securityPolicy.pinnedCertificates = @[myCertificate];

并且我尝试根据 iOS 9.0 的变化向 plist 添加异常

<key>NSAppTransportSecurity</key>
<dict>
    <key>NSAllowsArbitraryLoads</key>
    <true/>
</dict>

我不知道我做错了什么。我的观点是将我的自签名根证书固定到应用程序以便能够使用 https 连接。你能帮我用正确的方法去做吗?

我有 AFNetworking (2.6.1)。

感谢您的帮助!

最佳答案

所以我终于找到了问题的根源。问题出在自定义硬件设备上的 ssl-library 中。在硬件上重新实现后,一切都很顺利。

关于ios - 使用自签名根证书的 SSLHandshake 失败 (-9808),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33340907/

相关文章:

ios - 当 NSUrlSendAssynchronousrequest 操作运行时,选项卡栏应用程序按钮被阻止

iOS 11 ARKit : Drag Object in 3D View

ios - Swift println 不打印数组值

php - 不从 HTTP 重定向到 HTTPS

ssl - 如何管理自托管 HTTPS 的 SSL key

ios - 为 iPhone 和 iPad(普通版和 Retina 版)制作通用游戏

ssl - 配置 Erlang 以使用 SSL

ssl - 使用从 openssl api 获取的 key 解密 ssl 网络数据包

ssl - 证书中包括哪些证书签名请求信息?

php - 缺少 SSL 证书?