服务器模式 SSL 必须使用带有关联私钥的证书 - 在 TLS 握手期间

And*_*rei 7 c# ssl ssl-certificate x509certificate2

我有证书someCert.cer。我使用 MMC 实用程序将其导入到本地证书存储中。我的 C# 应用程序可以使用以下代码访问它:

var store = new X509Store(StoreName.My, StoreLocation.CurrentUser);
X509Certificate2 certificate = null;
store.Open(OpenFlags.ReadOnly);
try
{
    var certificateCollection = store.Certificates.Find(X509FindType.FindByThumbprint, "THUMBPRINT", false);
    certificate = certificateCollection[0];
}
finally
{
    store.Close();
}
Run Code Online (Sandbox Code Playgroud)

该应用程序公开 TCP 套接字,当我尝试使用客户端应用程序连接到它时,出现异常: The server mode SSL must use a certificate with the associated private key

事实上,我的证书的 PrivateKey 属性是空的。我导入的证书是否不正确,或者我应该在将证书导入商店之前对证书进行一些处理吗?

我的服务器身份验证代码如下所示:

stream.AuthenticateAsServerAsync(certificate, false, SslProtocols.Tls | SslProtocols.Tls11 | SslProtocols.Tls12, true);
Run Code Online (Sandbox Code Playgroud)

Dav*_*e G 2

为了进行正确的 tls 握手,服务器需要拥有与证书中嵌入的公钥配对的私钥。当客户端连接时,会向其提供证书。这允许基于颁发机构的信任来验证服务器是否是它所说的那个人。握手期间,客户端将生成一个对称密钥,该密钥使用证书中的公钥为服务器加密。服务器将使用私有部分解密对称密钥。

我在这里过于简化了握手过程。因此,对上述内容持保留态度。(无意加密双关语)