繁体   English   中英

将私钥添加到 X509Certificate

[英]Add Private Key to X509Certificate

我正在处理一些当前使用 OpenSSL.net 为证书签名请求创建公钥/私钥对的代码。 该请求配备了公钥并发送到 CA,后者返回签名证书。 然后将之前创建的私钥添加到证书中:

myCert.PrivateKey = CryptoKey.FromPrivateKey(rsa.PrivateKeyAsPEM, null);

问题是我需要一个 .net X509Certificate,因为软件的其余部分使用 SslStream 和其他 .net 类来实现 TLS。

我能够从 CA 的响应中创建证书,但我没有找到将私钥添加到其中的方法。 我还尝试从 CA 的响应中创建 OpenSSL 证书,将其导出为 DER 或 PEM 并从中创建 .net 证书,但它始终忽略私钥。

关于如何解决这个问题的任何想法?

我创建了一个小的帮助程序 NuGet 包来创建基于public keyprivate (rsa) keyX509 证书

// Generate with: openssl req -x509 -sha256 -nodes -days 365 -newkey rsa:2048 -keyout private.key -out certificate_pub.crt
string certificateText = File.ReadAllText("certificate_pub.crt");
string privateKeyText = File.ReadAllText("private.key");

ICertificateProvider provider = new CertificateFromFileProvider(certificateText, privateKeyText);
X509Certificate2 certificate = provider.Certificate;

// Example: use the PrivateKey from the certificate above for signing a JWT token using Jose.Jwt:
string token = Jose.JWT.Encode(payload, certificate.PrivateKey, JwsAlgorithm.RS256);

有关基于opensslkey 的功能和代码示例,请参阅NuGetGithub-project

我想也许你在这里错过了一些概念性的想法?

证书不应包含私钥。 私钥始终是私有的,证书是将您的公钥绑定到您的专有名称的东西。 换句话说,证书是一份由权威机构签署的文件,用于确认您与世界共享的特定公钥属于您而非其他任何人。 因此它永远不能包含私钥,因为您与世界共享您的证书!

暂无
暂无

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM