简体   繁体   English

如何从Bouncy Castle创建X509证书以与AuthenticateAsServer一起使用?

[英]How to create a X509 certificate from Bouncy Castle for use with AuthenticateAsServer?

I've used Bouncy Castle to create an X509 certificate, but I can't use that together with SslStream.AuthenticateAsServer or SslStream.AuthenticateAsClient since they (of course) use the .Net version. 我已经使用Bouncy Castle创建了X509证书,但是我不能将其与SslStream.AuthenticateAsServerSslStream.AuthenticateAsClient一起使用,因为它们(当然)使用.Net版本。 Although there is a converter , DotNetUtilities.ToX509Certificate() , in Bouncy Castle which takes aa BC X509 and returns a .Net X509. 尽管在Bouncy Castle中有一个转换器DotNetUtilities.ToX509Certificate() ,它使用BC X509并返回.Net X509。 The problem seems to be that AuthenticateAsServer/AuthenticateAsClient needs a certificate with the private key included. 问题似乎是AuthenticateAsServer / AuthenticateAsClient需要包含私钥的证书。 At least when I try to just convert and then use the new certificate I get a CryptographicException: "Key does not exist" when trying to connect using SslStream. 至少当我尝试仅转换然后使用新证书时,会出现CryptographicException: "Key does not exist"尝试使用SslStream连接时出现CryptographicException: "Key does not exist"情况。

So I thought that I need to create an X509Certificate2 from Bouncy Castle, since that can contain the private key as well. 因此,我认为我需要从Bouncy Castle创建一个X509Certificate2,因为它也可以包含私钥。 But the solution I found seems a bit...odd, and I was wondering if anyone else now a better way to use a BC X509Certificate with SslStream. 但是我找到的解决方案似乎有点奇怪,我想知道是否还有其他人现在可以将BC X509证书与SslStream一起使用。

This is how I create a X509Certificate2 from a BC Certificate: 这是我从BC证书创建X509Certificate2的方法:

private static X509Certificate CreateDotNetCertificate(Org.BouncyCastle.X509.X509Certificate certificate, AsymmetricCipherKeyPair keyPair)
{
   var store = new Pkcs12Store();
   string friendlyName = certificate.SubjectDN.ToString();
   var certificateEntry = new X509CertificateEntry(certificate);
   store.SetCertificateEntry(friendlyName, certificateEntry);
   store.SetKeyEntry(friendlyName, new AsymmetricKeyEntry(keyPair.Private), new[] { certificateEntry });

   var stream = new MemoryStream();
   var password = "a password";
   store.Save(stream, password.ToCharArray(), new SecureRandom(randomGenerator));

   return new X509Certificate2(stream.ToArray(), password, X509KeyStorageFlags.PersistKeySet | X509KeyStorageFlags.Exportable);
}

It seems a bit weird that I need to take a "detour" through a Pkcs12Store to be able to create my X509Certificate2. 我似乎需要通过Pkcs12Store进行“绕行”才能创建我的X509Certificate2。

The solution is taken from this blog: http://blog.differentpla.net/post/20 该解决方案来自以下博客: http : //blog.differentpla.net/post/20

Windows works with certificates stored in Windows Certificate Storage . Windows使用Windows证书存储中存储的证书。 I don't know if BouncyCastle provides direct access to Windows CertStorage but if it doesn't, than the only option is to import the certificate to CertStorage from file (or from other source), then use it. 我不知道BouncyCastle是否提供对Windows CertStorage的直接访问,但是如果不提供,则唯一的选择是从文件(或从其他来源)将证书导入CertStorage,然后使用它。 PKCS#12 is the right format to transfer the certificate with its private key together, so it's quite natural to use it as an intermediate medium. PKCS#12是将证书及其私钥一起传输的正确格式,因此将其用作中间介质是很自然的。

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

相关问题 充气城堡的CMS签名X509证书 - CMS Signature X509 Certificate with Bouncy Castle 如何读取 Pem 证书和私钥文件并创建 Bouncy Castle X509Certificate 和 Bouncy Castle AsymmetricKeyParameter dotnet 核心? - How to Read a Pem Cert and Private Key Files and create Bouncy Castle X509Certificate and Bouncy Castle AsymmetricKeyParameter dotnet core? 如何使用makecert创建WCF接受的X509证书 - How to use makecert to create a X509 certificate accepted by WCF c# 如何从证书和密钥或 pfx 或 p7b 创建 X509 证书 - c# How to create X509 Certificate from certificate and key or pfx or p7b C# 如何使用 Bouncy Castle 而不是 x509certificate2 在 Asp.net 5 中使用 S/Mime - C# How to S/Mime in Asp.net 5 with Bouncy Castle instead of x509certificate2 如何在C#中使用Bouncy Castle将X.509 v.3谷歌证书添加到项目中 - How to add X.509 v.3 google certificate to project with Bouncy Castle in c# 如何在没有完整 X509 证书的情况下使用 PdfPkcs7? - How to use PdfPkcs7 without a full X509 certificate? 如何从X509证书初始化ECDiffieHellmanCngPublicKey - How to initialize ECDiffieHellmanCngPublicKey from an X509 Certificate 如何从.NET中的X509证书中提取电子邮件? - How to extract the Email from a X509 Certificate in .NET? 如何完全验证 X509 证书? - How to fully validate a X509 certificate?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM