6

我正在尝试使用 SslStream 和 TLS 1.2 协议与远程服务器建立 TCP 连接。代码如下:

_tcpClient.Connect(endPoint);

var certificate = new X509Certificate2(_settings.CertificateFilePath, _settings.CertificatePassword, X509KeyStorageFlags.MachineKeySet);
var certificates = new X509CertificateCollection { certificate };

_nStream = _tcpClient.GetStream();
_sslStream = new SslStream(_nStream, false,
    (o, x509Certificate, chain, errors) => true,
    (o, s, collection, x509Certificate, issuers) =>
    { return collection[0]; }
);

_sslStream.AuthenticateAsClient(_settings.HostIpAddress, certificates, SslProtocols.Tls12, true);
_sslStream.Write(someData, 0, someData.Length);

但是,我遇到了一个例外:

System.Security.Authentication.AuthenticationException:对 SSPI 的调用失败,请参阅内部异常。---> System.ComponentModel.Win32Exception:处理证书时发生未知错误

--- 内部异常堆栈跟踪结束

在 System.Net.Security.SslState.CheckThrow(Boolean authSucessCheck) 在 System.Net.Security.SslStream.Write(Byte[] 缓冲区,Int32 偏移量,Int32 计数)

我启用了 SChannel 日志记录并在 Windows 事件日志中发现了这一点:

远程服务器已请求 SSL 客户端身份验证,但找不到合适的客户端证书。将尝试匿名连接。

然后我按照这里的描述启用了 System.Net日志记录并得到了这个日志(我从中删除了一些证书数据)。看起来客户端证书没问题,但由于某种原因,日志显示Remote certificate: null虽然很明显有一些从远程服务器发回的数据看起来非常像证书。日志最后说returned code=CertUnknown。我不知道问题可能出在哪里(远程服务器证书?我的代码?远程/本地服务器设置?),希望有任何帮助。

SslProtocols.Ssl3注意:如果我通过指定而不是SslProtocols.Tls12一切正常工作来更改我的代码以使用 SSL 3 。但我真的需要使用 TLS,因为这是远程服务器所有者要求做的。

4

1 回答 1

0

我们让我们的 websockets 服务器使用 TLS 1.2。

手动添加“ws.SslConfiguration.EnabledSslProtocols = System.Security.Authentication.SslProtocols.Tls12;” 解决了这个问题。

于 2021-05-11T15:02:48.137 回答