【发布时间】:2014-11-12 21:17:53
【问题描述】:
我的应用只是连接到家用设备上的网络服务器并读取一些 html。 使用 http 时它可以正常工作,但现在在 iOS 8 上使用 https 时它不起作用。
我使用AFNetworking v1 并使用这些覆盖方法覆盖 SSL 检查(是的,我知道在正常情况下不应该这样做,而且不安全等等,但那是另一个话题)。
[self setAuthenticationChallengeBlock:^(NSURLConnection *connection, NSURLAuthenticationChallenge *challenge) {
if ([challenge.protectionSpace.authenticationMethod isEqualToString:NSURLAuthenticationMethodServerTrust]) {
[challenge.sender useCredential:[NSURLCredential credentialForTrust:challenge.protectionSpace.serverTrust] forAuthenticationChallenge:challenge];
}
}];
[self setAuthenticationAgainstProtectionSpaceBlock:^BOOL(NSURLConnection *connection, NSURLProtectionSpace *protectionSpace) {
if([[protectionSpace authenticationMethod] isEqualToString:NSURLAuthenticationMethodServerTrust]) {
return YES; // Self-signed cert will be accepted
}
// If no other authentication is required, return NO for everything else
// Otherwise maybe YES for NSURLAuthenticationMethodDefault and etc.
return NO;
}];
现在在 iOS 8 中出现如下错误:
CFNetwork SSLHandshake 失败 (-9806)
CFNetwork SSLHandshake 失败 (-9806)
CFNetwork SSLHandshake 失败 (-9806)
NSURLConnection/CFURLConnection HTTP 加载失败 (kCFStreamErrorDomainSSL, -9806)
连接期间出错:Error Domain=NSURLErrorDomain Code=-1200 “发生 SSL 错误,无法与服务器建立安全连接。” UserInfo=0x7b66c110 {NSLocalizedDescription=发生 SSL 错误,无法建立与服务器的安全连接。, NSLocalizedRecoverySuggestion=您是否仍要连接到服务器?, _kCFStreamErrorCodeKey=-9806, NSErrorFailingURLStringKey=https://xxx.xxx.xxx.xxx:443/live.htm, _kCFStreamErrorDomainKey=3 , NSUnderlyingError=0x7c0d8a20 "发生 SSL 错误,无法与服务器建立安全连接。", NSErrorFailingURLKey=https://xxx.xxx.xxx.xxx:443/Info.live.htm}
我已经尝试切换到 FSNetworking,并且 sometimes 在设置时有效:
- (void)connection:(NSURLConnection *)connection
willSendRequestForAuthenticationChallenge:(NSURLAuthenticationChallenge *)challenge {
if([challenge.protectionSpace.authenticationMethod isEqualToString:NSURLAuthenticationMethodServerTrust])
{
NSLog(@"Ignoring SSL");
SecTrustRef trust = challenge.protectionSpace.serverTrust;
NSURLCredential *cred;
cred = [NSURLCredential credentialForTrust:trust];
[challenge.sender useCredential:cred forAuthenticationChallenge:challenge];
return;
}
}
但是同时进行 2 个请求,只有 1 个会成功,一个会失败,并出现与上述类似的错误。似乎只有 1 个请求到达 SSL 覆盖点。
谁能解释一下这个问题以及 iOS 8 中发生了什么改变来打破这个问题,以及我该如何解决它?
谢谢
【问题讨论】:
-
以下是包含您的错误代码的列表:opensource.apple.com/source/libsecurity_ssl/…
标签: ssl https ios8 afnetworking self-signed