我正在尝试向安全服务器打开输入/输出流,但一直在 CFNetwork SSLHandshake失败(-9806)
我为异常域等设置了plist值
<key>NSAppTransportSecurity</key>
<dict>
<key>NSExceptionDomains</key>
<dict>
<key>someserver.com</key>
<dict>
<key>NSIncludesSubdomains</key>
<true/>
<key>NSThirdPartyExceptionMinimumTLSVersion</key>
<string>TLSv1.0</string>
<key>NSThirdPartyExceptionRequiresForwardSecrecy</key>
<false/>
<key>NSTemporaryExceptionAllowsInsecureHTTPLoads</key>
<true/>
</dict>
</dict>
</dict>
我也在plist中试过这个:
<key>NSAppTransportSecurity</key>
<dict>
<key>NSAllowsArbitraryLoads</key>
<true/>
</dict>
这是我的代码:
-(void)startStream{
NSString *urlStr = @"https://stream.someserver.com";
NSURL *website = [NSURL URLWithString:urlStr];
CFReadStreamRef readStream;
CFWriteStreamRef writeStream;
CFStreamCreatePairWithSocketToHost(NULL, (CFStringRef)CFBridgingRetain([website host]), 443, &readStream, &writeStream);
NSInputStream *inputStream = (__bridge_transfer NSInputStream *)readStream;
NSOutputStream *outputStream = (__bridge_transfer NSOutputStream *)writeStream;
[inputStream setDelegate:self];
[outputStream setDelegate:self];
[inputStream scheduleInRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode];
[outputStream scheduleInRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode];
NSMutableDictionary *settings = [NSMutableDictionary dictionaryWithCapacity:1];
[settings setObject:(NSString *)NSStreamSocketSecurityLevelTLSv1 forKey:(NSString *)kCFStreamSSLLevel];
[settings setObject:[NSNumber numberWithBool:YES] forKey:(NSString *)kCFStreamSSLValidatesCertificateChain];
[settings setObject:@"stream.someserver.com" forKey:(NSString *)kCFStreamSSLPeerName];
CFWriteStreamSetProperty((CFWriteStreamRef)outputStream, kCFStreamPropertySSLSettings, (CFTypeRef)settings);
CFReadStreamSetProperty((CFReadStreamRef)inputStream, kCFStreamPropertySSLSettings, (CFTypeRef)settings);
[inputStream open];
[outputStream open];
}
在 CFNetwork SSLHandshake失败(-9806)错误之前,流打开后有大约5秒的延迟。
注意:安全服务器不是我的,我无法更改任何设置。它是经过测试和建立的服务器,有许多用户流式传输
答案 0 :(得分:1)
我不确定ssl连接握手中哪个阶段失败,但您可以尝试将设置字典更改为以下内容:
NSDictionary *settings = [[NSDictionary alloc] initWithObjectsAndKeys:
[NSNumber numberWithBool:YES], kCFStreamSSLAllowsExpiredCertificates,
[NSNumber numberWithBool:YES], kCFStreamSSLAllowsAnyRoot,
[NSNumber numberWithBool:YES], kCFStreamSSLAllowsExpiredRoots,
[NSNumber numberWithBool:NO], kCFStreamSSLValidatesCertificateChain,
//kCFNull,kCFStreamSSLPeerName,
kCFStreamSocketSecurityLevelSSLv3, kCFStreamSSLLevel,
[NSNumber numberWithBool:YES], kCFStreamPropertyShouldCloseNativeSocket,
nil];
如果成功,请删除每个&#34;安全漏洞&#34;定义如上。
答案 1 :(得分:1)
查看Apple documentation,您似乎使用了错误的密钥。
例如
NSThirdPartyExceptionMinimumTLSVersion
必须为NSExceptionMinimumTLSVersion
NSTemporaryExceptionAllowsInsecureHTTPLoads
必须为NSExceptionAllowsInsecureHTTPLoads
答案 2 :(得分:1)
在您的SSL设置中,将kCFStreamSSLValidatesCertificateChain
设置为@NO
以完全禁用服务器信任评估。
在您的委托实现中,当您第一次获得NSStreamEventHasSpaceAvailable
或NSStreamEventHasBytesAvailable
状态时,请从流中检索kCFStreamPropertySSLPeerTrust
属性。
您应该返回SecTrustRef
,您可以评估并获取以下信息:
SecTrustRef trust = (SecTrustRef)[stream propertyForKey:kCFStreamPropertySSLPeerTrust];
SecTrustResultType trustResult;
OSStatus status = SecTrustEvaluate(trust, &trustResult);
if (status != errSecSuccess) {
NSLog(@"failed to evaluate");
} else {
OSStatus trustResultCode = SecTrustGetTrustResult(trust, &trustResult);
}
最终trustResultCode
应该为您提供信任评估失败的具体原因。根据具体原因,您可以修改SecTrustRef
或创建一个全新的以获得成功的评估。
Apple Tech Note HTTPS Server Trust Evaluation确实提供了丰富的信息。