【问题标题】:How do I disable encryption with the SslStream class?如何使用 SslStream 类禁用加密?
【发布时间】:2017-06-09 11:06:11
【问题描述】:

我正在通过TLS/SSL using the SslStream class 连接到 FTPS 服务器。一切都很顺利。但是我需要支持一个名为“CCC”的命令,它基本上会禁用加密。如果我只是这样做:

SendCommand("CCC");
sslStream.Close();
netStream.Write("...<futher FTP commands>...")

然后 FTP 服务器响应看似垃圾的数据(大约 30 个字节),然后再不响应任何命令(超时)。

FTP日志如下:

# Connect()
Status:   Connecting to ***:21
Response: 220-IPv6 connections are also welcome on this server.
Command:  AUTH TLS
Response: 234 AUTH TLS OK.
Status:   FTPS Authentication Successful
Command:  USER ***
Response: 331 User *** OK. Password required
Command:  PASS ***
Response: 230 OK. Current restricted directory is /
Command:  PBSZ 0
Response: 200 PBSZ=0
Command:  PROT P
Response: 200 Data protection level set to "private"
Status:   Text encoding: System.Text.UTF8Encoding
Command:  OPTS UTF8 ON
Response: 200 OK, UTF-8 enabled
Command:  SYST
Response: 215 UNIX Type: L8
Command:  CCC
Response: 200 Control connection unencrypted
Status:   The stale data was: ************

如您所见,FTP 服务器发回“200 Control connection unencrypted”,表示命令成功。同样重要的是要注意响应是以加密格式发送的。

所以我可能需要在禁用加密的同时继续使用 SslStream。在禁用加密算法时,可能仍需要“块模式”通信。有谁知道我该怎么做?

【问题讨论】:

  • 我认为服务器可能希望您真正关闭安全连接,而不仅仅是丢弃它。据我所知,SslStream 不支持此功能,因此您可能需要研究如何手动执行此操作
  • “关闭安全连接”和丢弃它有什么区别?

标签: c# .net encryption ftp sslstream


【解决方案1】:

“关闭安全连接”和“关闭安全连接”有什么区别? 放弃了吗?

“关闭安全连接”是指在不关闭底层 TCP 连接的情况下发送 TLS close_notify 警报。 SslStreamthrough use of the constructors that take a bool innerStreamOpen argument 支持此功能。通常,当您调用SslStream.Close() 时,对等方会通过交换 TLS close_notify 消息安全地关闭 TLS 连接,然后立即关闭底层 TCP 连接。但是,如果您使用带有 leaveInnerStreamOpen 参数设置为 true 的 SslStream 构造函数,则 TCP 连接不会关闭,并且可能会在其上发送更多不安全数据。例如,

var tcpClient = new TcpClient("127.0.0.1", 9876);
var tcpStream = tcpClient.GetStream();
var sslStream = new SslStream(tcpStream, true);
sslStream.AuthenticateAsClient("127.0.0.1");
sslStream.Write(....);  // use the secure connection.
sslStream.Close();   // close the TLS connection: the tcp stream is still viable
tcpStream.Write(...) // use the unsecured TCP connection
tcpStream.Close();  // now the TCP connection is terminated.

现在手动实现基于 SSL 客户端的 FTP 似乎相当棘手。就我个人而言,在尝试编写自己的实现之前,我会寻找现有的成熟实现。

【讨论】:

  • 谢谢詹姆斯,但我已经意识到这一点,正如我在 Q 中的代码 sn-p 中所示。但是我不明白“手动通过 SSL 客户端进行 FTP”的含义.为什么我不能在关闭 sslStream 后使用 tcpStream.Write() 发送纯文本数据?是否需要不同的格式?
【解决方案2】:

FixedSslStream 课程对我有用。当您在其上调用 Close() 时,它会发送 SSL close_notify 警报!然后就可以继续向基流发送明文数据了。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-10-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-10-12
    • 1970-01-01
    • 2021-02-19
    • 1970-01-01
    相关资源
    最近更新 更多