How to use PEM certificate in Kestrel directly?

情到浓时终转凉″ 提交于 2019-12-19 19:12:20

问题


I want to use HTTPS in my ASP.Net Core 2.0 (with Kestrel web server) application.

The official documentation uses pfx format, but I want to use PEM format (generated from Let's encrypt) directly without any conversion (at least nothing outside my C# code). Is is possible?


回答1:


The short answer is that you can't. At least, you can't without a whole lot of work or using something like Bouncy Castle.

When the cert and the key are put together into a PFX the X509Certificate2 object will have cert.HasPrivateKey == true, and is capable of using the private key via the Get[Algorithm]PrivateKey extension method family. When you load a PEM certificate only the public certificate portion is loaded (and if it's a PEM certificate with a PEM key glued onto it? That's still just a PEM certificate).

The easy way to get a private key associated with a certificate is with the new (in .NET Core 2.0) certWithKey = cert.CopyWithPrivateKey(key) extension method family. So now you "just" need to load the private key. .NET does not currently have the ability to load (or save) ".key" files (no matter what their extension). If you want to take a crack at loading one you might want to check some prior art:

  • How to parse(Convert to RSAParameters) X.509 private key in C#?
  • Create RSACryptoServiceProvider object using RSA private key file in C#
  • How is a private key encrypted in a pem certificate?
  • Export private/public keys from X509 certificate to PEM

The good news is that .NET is planning to support loading keys in the future (https://github.com/dotnet/corefx/issues/20414), but since it isn't done yet (much less released) that doesn't help you right now.



来源:https://stackoverflow.com/questions/48077748/how-to-use-pem-certificate-in-kestrel-directly

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!