简体   繁体   English

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

[英]The server mode SSL must use a certificate with the associated private key - during TLS handshake

I have a certificate someCert.cer .我有证书someCert.cer I imported it into my local certificates store using MMC utility.我使用 MMC 实用程序将它导入到我的本地证书存储中。 My C# application is able to access it using following code:我的 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();
}

The application exposes TCP socket and when I try to connect to it with my client app I get exception: The server mode SSL must use a certificate with the associated private key该应用程序公开 TCP 套接字,当我尝试使用客户端应用程序连接到它时,出现异常: The server mode SSL must use a certificate with the associated private key

Indeed my certificate's PrivateKey property is empty.事实上,我的证书的 PrivateKey 属性是空的。 Did I import my certificate incorrect or should I do something with the certificate before importing it into the store?我导入的证书不正确还是应该在将证书导入商店之前对其进行处理?

My server authentication code looks like that:我的服务器身份验证代码如下所示:

stream.AuthenticateAsServerAsync(certificate, false, SslProtocols.Tls | SslProtocols.Tls11 | SslProtocols.Tls12, true);

For a proper tls handshake, the server is required to have a private key paired with the public key embedded in the certificate.对于正确的 tls 握手,服务器需要具有与嵌入在证书中的公钥配对的私钥。 When a client connects, it is presented with the certificate.当客户端连接时,它会显示证书。 This allows for validation that the server is who it says it is based on trust of an issuance authority.这允许基于颁发机构的信任来验证服务器就是它所说的那个人。 The client, during the handshake will generate a symmetric cipher key that is encrypted for the server using the public key from the certificate.客户端在握手期间将生成一个对称密码密钥,该密钥使用证书中的公钥为服务器加密。 The server will decrypt the symmetric key using the private half.服务器将使用私钥解密对称密钥。

I'm grossly over simplifying the handshake process here.我在这里过分简化了握手过程。 So take the above with a grain of salt.因此,对上述内容持保留态度。 (No crypto pun intended) (没有加密双关语意图)

Between Jesse's comment and Dave's answer, I could work out that I'd somehow created and installed some kind of partial certificate, sort half-jobbed it.在 Jesse 的评论和 Dave 的回答之间,我可以确定我以某种方式创建并安装了某种部分证书,对它进行了排序。

I had made a cert using makecert.exe and somehow managed to install it and load it into Kestrel, but it failed when a request from Chrome came in.我已经使用makecert.exe制作了一个证书,并以某种方式设法安装它并将其加载到 Kestrel 中,但是当来自 Chrome 的请求进来时它失败了。

I couldn't find a copy of pvk2pfx.exe in order to make a PFX file from the two files that makecert.exe had kicked out, so I just tried to carry on without it.我找不到pvk2pfx.exe的副本,以便从makecert.exe踢出的两个文件中制作 PFX 文件,所以我只是尝试在没有它的情况下继续。

After that failure and reading the above, I figured that the PFX step is mandatory.在失败并阅读上述内容之后,我认为 PFX 步骤是强制性的。 So I used classic PowerShell 4 (PS6 Core wouldn't load the module):所以我使用了经典的 PowerShell 4(PS6 Core 不会加载模块):

> $cert = New-SelfSignedCertificate -certstorelocation cert:\currentuser\my -dnsname windows
> $pwd = ConvertTo-SecureString -String 'password1234' -Force -AsPlainText
> $path = 'cert:\currentuser\my\' + $cert.thumbprint
> Export-PfxCertificate -cert $path -FilePath c:\DATA\~Scrap\windows.pfx -Password $pwd

Credit to https://medium.com/the-new-control-plane/generating-self-signed-certificates-on-windows-7812a600c2d8归功于https://medium.com/the-new-control-plane/generating-self-signed-certificates-on-windows-7812a600c2d8

Tbh, I originally half-jobbed this. Tbh,我最初做了一半。 I stopped after the first command which produced a certificate in my store, but gave me a different strange error from Kestrel's stack when hit from Chrome.我在第一个在我的商店中生成证书的命令后停止了,但是当我从 Chrome 命中时,给了我一个与 Kestrel 堆栈不同的奇怪错误。 Again, the PFX step is needed for some reason.同样,出于某种原因需要 PFX 步骤。

At present I can't find a way to trust the signer, but Chrome let's me proceed so I'll sort that when I'm not so tired.目前我找不到信任签名者的方法,但 Chrome 让我继续,所以当我不太累的时候我会解决这个问题。

How did you create the certificate?你是如何创建证书的? When creating a certificate a private key is also used to sign it which is what you need.创建证书时,还使用私钥对其进行签名,这正是您所需要的。 If you have exported the certificate, you probably had the option to provide a password at some point.如果您已导出证书,您可能可以选择在某个时候提供密码。 If you have not provided a password when exporting, the certificate is exported without the private key.如果导出时未提供密码,则导出证书时不带私钥。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 SslStream.AuthenticateAsServer 抛出服务器模式 SSL 必须使用具有关联私钥的证书 - SslStream.AuthenticateAsServer throws The server mode SSL must use a certificate with the associated private key SslStream.AuthenticateAsServer异常-服务器模式SSL必须使用带有关联私钥的证书 - SslStream.AuthenticateAsServer exception - The server mode SSL must use a certificate with the associated private key 获取“服务器模式 SSL 必须使用具有关联私钥的证书。” X509Certificate2 中的错误由 use.cer 而不是.pfx - Get "The server mode SSL must use a certificate with the associated private key." error in X509Certificate2 by use .cer instead of .pfx C# SSL 服务器模式必须使用带有相应私钥的证书 - C# SSL server mode must use a certificate with the corresponding private key 验证证书TLS / SSL服务器 - Validate certificate TLS/SSL Server SSL证书和私钥(RSA) - SSL certificate and private key (RSA) SQL 服务器登录前握手(错误:31 - 加密(ssl/tls)握手失败) - SQL Server Pre-Login Handshake (error: 31 - Encryption(ssl/tls) handshake failed) 不在管理员模式下时,无法在TLS 1.2连接期间创建SSL / TLS安全通道错误 - Could not create SSL/TLS secure channel error during TLS 1.2 connection when not in admin mode 为什么在使用仅公开公钥的证书时TLS握手失败? - Why TLS Handshake fails when using a certificate that exposes only public key? 使用特定的私钥生成证书 - Use Specific Private Key to Generate Certificate
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM