objective-c - SSL 和 NSStream

标签 objective-c macos nsstream cfsocket

我尝试通过 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/

相关文章:

cocoa-touch - 将 SSL 添加到 iOS 应用程序同步

ios - 滚动时 UITableView 不刷新

objective-c - 对象泄露 : Object allocated and stored is not referenced later in this execution

swift - 如何在 mac os x 应用程序中通过 shell 命令访问文件?

ruby - 在 Mac OS X 上调试 Ruby 中的 malloc 错误

ios - 从 iOS 发送 C++ Protocol Buffer 消息

ios - 流套接字连接中的 SSL

ios - 如何在 Objective-C 中为蒙版图像添加不带蒙版的边框

iPhone Facebook 视频上传

macos - 如何安装特定版本的 vim (+clipboard)