【问题标题】:SSLStream throwing exception when moving to .NET 4.51迁移到 .NET 4.51 时 SSLStream 抛出异常
【发布时间】:2015-11-05 15:12:32
【问题描述】:

以前在 .NET 3.5 中此代码运行良好:

protected bool OpenSsl(string hostSslServer)
    {
        if ((this._enableSSL == false) || (this._isSSLOpen == true))
            return (true);

        this._isSSLOpen = false;

        this._sslConnectionStream = new SslStream(this._tcpSocket.GetStream(), false, new RemoteCertificateValidationCallback(this.ValidateServerCertificate), null);
        this._sslConnectionStream.AuthenticateAsClient(hostSslServer);
        this._sslProtocol = _sslConnectionStream.SslProtocol.ToString();        

        this._isSSLOpen = true;
        return (true);
    }

行 this._sslProtocol = _sslConnectionStream.SslProtocol.toString();旨在确定正在使用的协议。但是从那以后我发现 TLS1.1 和 TLS1.2 不是 3.5 中 SslProtocols 枚举的一部分,所以我将项目切换到 4.5。但是现在我抛出了异常,特别是 AuthenticateAsClient 行。异常是“对 SSPI 的调用失败,请参阅内部异常”。内部异常是“收到的消息意外或格式错误”。

从那以后我做了一些研究,发现我可以使用此代码进行连接:

System.Security.Cryptography.X509Certificates.X509Certificate2Collection xc = new System.Security.Cryptography.X509Certificates.X509Certificate2Collection();
        this._sslConnectionStream.AuthenticateAsClient(hostSslServer, xc, System.Security.Authentication.SslProtocols.Tls12, false);

这用对 AuthenticateAsClient 的调用替换了前一行。但是这里的问题是,使用这段代码(我不得不承认我不明白)我将 ssl 协议设置为参数之一。我不想那样做。我想知道我正在连接的主机使用的是什么 ssl 协议。

有没有办法解决这个问题?

从那时起,我将 sslprotocols 的参数更改为:

SslProtocols.Ssl2 | SslProtocols.Ssl3 | SslProtocols.Tls | SslProtocols.Tls11 | SslProtocols.Tls12

我又抛出了一个异常:“客户端和服务器无法通信,因为它们没有共同的算法”

【问题讨论】:

  • SslProtocols 是一个flag,可以设置为SslProtocols.Tls11|SslProtocols.Tls12。如果使用 Default 选项的原始重载不起作用,那是因为服务器甚至不允许 TLS 1.0。这不是 .NET 中的错误,很可能是服务器发生了变化
  • 啊!但是现在我收到此错误:“客户端和服务器无法通信,因为它们不具备通用算法”

标签: c# .net ssl


【解决方案1】:

我从协议列表中删除了 SSL2,它可以正常工作。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-10-22
    • 1970-01-01
    • 2021-02-02
    • 1970-01-01
    • 2017-02-26
    • 2020-08-30
    • 2013-05-24
    • 1970-01-01
    相关资源
    最近更新 更多