2

我尝试通过 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 时,我仍然有同样的错误。

如何配置流以使用 ssl?

4

1 回答 1

2

SSL/TLS 有不同的版本。也许您正在连接的服务器无法使用 TLS 版本 1 进行通信。尝试使用 的值NSStreamSocketSecurityLevelNegotiatedSSLforKey: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 的服务器。

于 2014-03-26T10:41:27.703 回答