从证书和密钥创建 X509Certificate2,而不创建 PFX 文件



过去,我一直通过导出带有密码的PFX证书来制作安全的TcpListener,但想知道是否可以跳过此步骤。

我没有使用商业 SSL 证书,并且有一个用于颁发服务器证书的根 CA。在 C# 中托管 TcpListener 时,这些服务器证书需要额外的步骤(我猜是因为没有使用 CSR)......但是,如果我确实有私钥和OpenSSL生成/使用的证书怎么办。

sslCertificate = new X509Certificate2("myExportedCert.pfx", "1234");

所以这很棒,但是我必须发出一个openssl命令来从证书和私钥制作pfx文件,然后编造一些密码。然后将此密码包含在我的代码中。

我想知道这一步是否非常必要。有没有办法从证书中组成 X509Certificate2,然后应用私钥。构造函数参数仅允许 Cert 部分,但加密失败,因为没有私钥。

另外,我不想依赖OpenSSL或IIS来导出pfx。似乎很笨拙。

理想情况下,我想要:

sslCertificate = new X509Certificate2("myCert.crt");
sslCertificate.ApplyPrivateKey(keyBytes) // <= or "private.key" or whatever
sslStream.AuthenticateAsServer(sslCertificate, false, SslProtocols.Default, false);

您要求几种不同的东西,具有不同程度的轻松程度。

将私钥附加到证书

从 .NET Framework 4.7.2 或 .NET Core 2.0 开始,可以组合使用证书和密钥。 它不会修改证书对象,而是生成一个知道密钥的新证书对象。

using (X509Certificate2 pubOnly = new X509Certificate2("myCert.crt"))
using (X509Certificate2 pubPrivEphemeral = pubOnly.CopyWithPrivateKey(privateKey))
{
// Export as PFX and re-import if you want "normal PFX private key lifetime"
// (this step is currently required for SslStream, but not for most other things
// using certificates)
return new X509Certificate2(pubPrivEphemeral.Export(X509ContentType.Pfx));
}

在.NET Framework(但不是.NET Core)上,如果您的私钥是RSACryptoServiceProviderDSACryptoServiceProvider您可以使用cert.PrivateKey = key,但这具有复杂的副作用,不鼓励使用。

加载私钥

这个更难,除非你已经解决了它。

在大多数情况下,答案是在不使用BouncyCastle的c#中的数字签名中,但是如果您可以迁移到.NET Core 3.0,事情就会变得容易得多。

PKCS#8 私钥信息

从 .NET Core 3.0 开始,您可以相对简单地执行此操作:

using (RSA rsa = RSA.Create())
{
rsa.ImportPkcs8PrivateKey(binaryEncoding, out _);
// do stuff with the key now
}

(当然,如果您有 PEM,则需要通过提取 BEGIN 和 END 分隔符之间的内容并通过Convert.FromBase64String运行它来"取消 PEM"它以获得binaryEncoding)。

PKCS#8 加密私钥信息

从 .NET Core 3.0 开始,您可以相对简单地执行此操作:

using (RSA rsa = RSA.Create())
{
rsa.ImportEncryptedPkcs8PrivateKey(password, binaryEncoding, out _);
// do stuff with the key now
}

(如上所述,如果是 PEM,则需要先"去 PEM"它)。

PKCS#1 RSAPrivateKey

从 .NET Core 3.0 开始,您可以相对简单地执行此操作:

using (RSA rsa = RSA.Create())
{
rsa.ImportRSAPrivateKey(binaryEncoding, out _);
// do stuff with the key now
}

(如果 PEM,则为"去 PEM")。

最后我这样做了,它工作正常:

...
if (!File.Exists(pfx)) {
// Generate PFX
string arguments = "openssl pkcs12 -export -in " + certPath + "" + certFile + ".crt -inkey " + certPath + "" + certFile + ".key -out " + certPath + "" + certFile + ".pfx -passout pass:" + pfxPassword;
ProcessStartInfo opensslPsi = new ProcessStartInfo("sudo", arguments);
opensslPsi.UseShellExecute = false;
opensslPsi.RedirectStandardOutput = true;
using (Process p = Process.Start(opensslPsi)) {
p.WaitForExit();
}
// Set Permission
ProcessStartInfo chmodPsi = new ProcessStartInfo("sudo", "chmod 644 " + certPath + "" + certFile + ".pfx");
chmodPsi.UseShellExecute = false;
chmodPsi.RedirectStandardOutput = true;
using (Process p = Process.Start(chmodPsi)) {
p.WaitForExit();
}
}
sslCertificate = new X509Certificate2(pfx, pfxPassword);
...

最新更新