gpt4 book ai didi

.net - 无需外部库即可生成自签名证书

转载 作者:行者123 更新时间:2023-12-03 17:43:54 24 4
gpt4 key购买 nike

我很想知道是否有一种简单的方法可以创建类似于以下 New-SelfSignedCertificate 的自签名证书命令(例如,其他提供程序也可以)。我只想使用没有 P/Invoke 的 .NET 库或外部库(如 Bouncy CaSTLe)或不从应用程序调用 PowerShell。

New-SelfSignedCertificate -DnsName $certificateName -CertStoreLocation $certificateStore -KeyExportPolicy Exportable -Provider "Microsoft Enhanced RSA and AES Cryptographic Provider" -NotAfter $certificateNotAfter

我想最简单的选择是调用 PowerShell 或使用诸如 Bouncy CaSTLe 之类的 Nuget 库,如果这在没有外部设施的情况下不可行?虽然感觉如果我足够了解如何构造证书,那么可以创建一个字节数组模板或类似的并在 X509Certificate2 中使用它。构造函数。

看来需要
public X509Certificate2 GenerateCertificate(string fileName, string password, string subjectName, StoreName storeName, DateTime endDate, DateTime notAfter, string provider = "Microsoft Enhanced RSA and AES Cryptographic Provider")
{
//Could provider be taken from https://stackoverflow.com/questions/43474902/generate-self-signed-rsa-2048-sha-256-certificate-pfx-file-using-openssl?
var newCertificate = new X509Certificate2(fileName, password, X509KeyStorageFlags.Exportable);

/*
# The following creates a self-signed certificate with one year of running time.
$currentDate = Get-Date
$certificateEndDate = $currentDate.AddYears(1)
$certificateNotAfter = $certificateEndDate.AddYears(1)

$certificateName = "https://www.test.com/test"
$certificateStore = "Cert:\LocalMachine\My"
New-SelfSignedCertificate -DnsName $certificateName -CertStoreLocation $certificateStore -KeyExportPolicy Exportable -Provider "Microsoft Enhanced RSA and AES Cryptographic Provider" -NotAfter $certificateNotAfter
*/
}


我发现了更多选择:

一篇博文 Creating Authority-Signed and Self-Signed Certificates in .NET作者:Steve Syfuhs 和另一篇使用 Mono 扩展的 SO 帖子, Mono.Security won't set multiple KeyUsages .除了已经讨论过的选择之外,它是“类似这样的”。

最佳答案

在任何已发布的 .NET 版本中都无需创建证书。
.NET Core 2.0(尚未发布,但应该很快发布)已经和 .NET Framework 4.7.2 通过 CertificateRequest 添加了此功能(可以执行自签名证书、链签名证书或 PKCS#10 证书/证书签名请求的 API)。
PowerShell 命令结合了三件事:

  • key 存储参数 (CSP)
  • 证书创建
  • 证书存储(添加证书的存储)

  • 要在 localhost 上创建适用于 TLS 服务器身份验证的“简单”自签名证书:
    X509Certificate2 certificate = null;
    var sanBuilder = new SubjectAlternativeNameBuilder();
    sanBuilder.AddDnsName("localhost");

    // New .NET Core Create(int) method. Or use
    // rsa = RSA.Create(), rsa.KeySize = newRsaKeySize,
    // or (on .NET Framework) new RSACng(newRsaKeySize)
    using (RSA rsa = RSA.Create(newRsaKeySize))
    {
    var certRequest = new CertificateRequest(
    $"CN=localhost",
    rsa,
    HashAlgorithmName.SHA256,
    RSASignaturePadding.Pkcs1);

    // Explicitly not a CA.
    certRequest.CertificateExtensions.Add(
    new X509BasicConstraintsExtension(false, false, 0, false));

    certRequest.CertificateExtensions.Add(
    new X509KeyUsageExtension(
    X509KeyUsageFlags.DigitalSignature | X509KeyUsageFlags.KeyEncipherment,
    true));

    // TLS Server EKU
    certRequest.CertificateExtensions.Add(
    new X509EnhancedKeyUsageExtension(
    new OidCollection
    {
    new Oid("1.3.6.1.5.5.7.3.1"),
    },
    false));

    // Add the SubjectAlternativeName extension
    certRequest.CertificateExtensions.Add(sanBuilder.Build());

    DateTimeOffset now = DateTimeOffset.UtcNow;
    certificate = certRequest.CreateSelfSigned(now, now.AddDays(365.25));
    }
    这个创建的证书有一个临时私钥——它当前没有写入磁盘。如果您不关心它使用什么 key 存储提供程序,那么此时最好的办法是将证书(和私钥)导出为 PFX/PKCS#12,然后使用 PersistKeySet 重新导入它。 (和 Exportable ,因为你想要),并将导入的副本添加到您选择的 X509Store。
    如果您关心 key 存储提供程序(在您的情况下是 CAPI CSP),或者您想避免导出/导入,您可以使用预持久 key 创建它。所以你会替换 RSA.Create(newRsaKeySize)
    资本API:
    var cspParameters = new CspParameters(
    /* PROV_RSA_AES */ 24,
    "Microsoft Enhanced RSA and AES Cryptographic Provider",
    Guid.NewGuid().ToString());

    using (RSA rsa = new RSACryptoServiceProvider(newRsaKeySize, cspParameters))
    天然气:
    var keyParams = new CngKeyCreationParameters();
    keyParams.Parameters.Add(
    new CngProperty(
    "Length",
    BitConverter.GetBytes(newRsaKeySize),
    CngPropertyOptions.Persist));

    using (CngKey rsaKey = CngKey.Create(CngAlgorithm.RSA, Guid.NewGuid().ToString(), keyParams)
    using (RSA rsa = new RSACng(rsaKey))
    然后以通常的方式将其添加到打开的 X509Store 实例中。

    it feels like that if I knew enough how to construct certificates, it'd be possible to create a byte array template or such and use that in the X509Certificate2 constructor.


    真的。但这有点棘手。您需要学习 ASN.1 ( ITU-T X.680 )、DER ( ITU-T X.690 ) 和 X.509 ( RFC 5280ITU-T X.509 )。如果您想创建与私钥匹配的证书,则需要学习 PFX/PKCS#12 ( RFC 7292 ,但仅限于一些较旧的选项,除非您是 Win10 专用),并且有很多前提知识,也是。
    我认为这些都是值得学习的有趣的东西,也是值得了解的好东西......但是当我开始在白板上涂鸦 DER 时,我的同事们给了我奇怪的表情,所以我可能并不代表开发人员对“有趣”的一般看法.

    关于.net - 无需外部库即可生成自签名证书,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45232980/

    24 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com