我可以使用 openssl 成功连接到公司服务器:
openssl s_client -connect {ip_address}:{tcp_port} -cert client.pem -key client.key -CAfile ca.cer
输出显示协商的协议是TLS 1.2。
我想用C#实现这个连接。这是我目前的尝试。请注意,我提供了客户端的证书和私钥,但我没有检查服务器证书:
var clientPrivateKey = RSA.Create();
clientPrivateKey.ImportFromPem(File.ReadAllText("client.key"));
var clientCertificate = new X509Certificate2("client.pem").CopyWithPrivateKey(clientPrivateKey);
var clientCertificates = new X509Certificate2Collection(clientCertificate);
using (var tcpClient = new TcpClient(ipAddress, tcpPort))
using (var sslStream = new SslStream(tcpClient.GetStream()))
{
sslStream.AuthenticateAsClient(new SslClientAuthenticationOptions
{
AllowRenegotiation = true,
EnabledSslProtocols = SslProtocols.Tls12 | SslProtocols.Tls13,
CertificateRevocationCheckMode = X509RevocationMode.NoCheck,
ClientCertificates = clientCertificates,
TargetHost = ipAddress, // ???
RemoteCertificateValidationCallback = (_, _, _, _) => true
});
}
不幸的是
sslStream.AuthenticateAsClient
抛出这个错误:
Received an unexpected EOF or 0 bytes from the transport stream. (System.IO.IOException)
我已经在 MSDN、StackOverflow 等上阅读了有关此问题的所有内容。一些观察:
虽然 openssl 命令行说协议是 TLS 1.2,但我尝试了其他值,也尝试不设置
EnabledSslProtocols
值,但没有任何帮助。我也尝试更改或省略 AllowRenegotiation
和 CertificateRevocationCheckMode
,但这也没有帮助。
TargetHost
值不能为空,但我不知道它应该是什么。我尝试了空字符串、服务器证书中的 CN,甚至服务器的 IP 地址,但没有任何帮助。有趣的是,上面的 openssl 命令行显示 Can't use SSL_get_servername
,这很可疑,但我不知道它到底是什么意思。
-- 提前感谢您的帮助!