【问题标题】:SSL and NSStreamSSL 和 NSStream
【发布时间】:2013-05-17 22:15:51
【问题描述】:

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

【问题讨论】:

  • 如果你使用 NSURLConnection 你可以获得异步回调,这有点像一个流,特别是因为你不能真正寻找那个流
  • 你还有这个问题吗?

标签: objective-c macos nsstream cfsocket


【解决方案1】:

SSL/TLS 有不同的版本。可能您正在连接的服务器无法使用 TLS 版本 1 进行通信。尝试将值 NSStreamSocketSecurityLevelNegotiatedSSL 设置为 forKey:NSStreamSocketSecurityLevelKey 以获得可能的最高连接版本。

进一步尝试以这种方式设置 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 的服务器。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-12-09
    • 1970-01-01
    • 1970-01-01
    • 2015-04-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-03-25
    相关资源
    最近更新 更多