我尝试通过 CFSocket 连接在 NSStream 中使用 SSL。所以我写了这段代码:
[self.input setProperty:NSStreamSocketSecurityLevelTLSv1 forKey:NSStreamSocketSecurityLevelKey];
[self.output setProperty:NSStreamSocketSecurityLevelTLSv1 forKey:NSStreamSocketSecurityLevelKey];
[self.input scheduleInRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode];
[self.output scheduleInRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode];
[self.input open];
[self.output open];
但是如果我从curl 或浏览器向我的服务器发送请求,则会出现此错误:
error:140770FC:SSL routines:SSL23_GET_SERVER_HELLO:unknown protocol
当我使用此解决方案时 NSStream SSL on used socket我仍然有同样的错误。
如何配置流以使用 ssl?
最佳答案
SSL/TLS 有不同的版本。也许您要连接的服务器无法使用 TLS 版本 1 进行通信。请尝试使用 forKey:NSStreamSocketSecurityLevelKey
的值 NSStreamSocketSecurityLevelNegotiatedSSL
来为您的连接获取可能的最高版本。
进一步尝试以这种方式设置 SSL/TLS 连接的属性
// Setting properties. Atm every certificate is accepted and no hostname will be checked
NSDictionary *settings = [[NSDictionary alloc] initWithObjectsAndKeys:
[NSNumber numberWithBool:YES], kCFStreamSSLAllowsExpiredCertificates,
[NSNumber numberWithBool:YES], kCFStreamSSLAllowsAnyRoot,
[NSNumber numberWithBool:NO], kCFStreamSSLValidatesCertificateChain,
kCFNull,kCFStreamSSLPeerName,
nil];
CFReadStreamSetProperty((CFReadStreamRef)_inputStream, kCFStreamPropertySSLSettings, (CFTypeRef)settings);
CFWriteStreamSetProperty((CFWriteStreamRef)_outputStream, kCFStreamPropertySSLSettings, (CFTypeRef)settings);
但请记住,当您以这种方式使用它时,没有太多的身份验证和针对中间人的保护。尝试使用这些设置来处理其他问题。
进一步发布更多代码并尝试连接到另一个使用 https 的服务器。
关于objective-c - SSL 和 NSStream,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16618690/