Generate and Sign Certificate Request using pure .net Framework

前端 未结 1 744

I am trying to use pure .net code to create a certificate request and create a certificate from the certificate request against an existing CA certificate I have available (

相关标签:
1条回答
  • 2020-11-27 04:36

    Short answer: You can starting in .NET Framework 4.7.2.

    This functionality was originally added to .NET Core 2.0 in the form of the CertificateRequest class, which can build a PKCS#10 certification signing request or an X.509 (self-signed or chained) public key certificate.

    The classes for that feature were made available in .NET Framework 4.7.2.

    using (RSA parent = RSA.Create(4096))
    using (RSA rsa = RSA.Create(2048))
    {
        CertificateRequest parentReq = new CertificateRequest(
            "CN=Experimental Issuing Authority",
            parent,
            HashAlgorithmName.SHA256,
            RSASignaturePadding.Pkcs1);
    
        parentReq.CertificateExtensions.Add(
            new X509BasicConstraintsExtension(true, false, 0, true));
    
        parentReq.CertificateExtensions.Add(
            new X509SubjectKeyIdentifierExtension(parentReq.PublicKey, false));
    
        using (X509Certificate2 parentCert = parentReq.CreateSelfSigned(
            DateTimeOffset.UtcNow.AddDays(-45),
            DateTimeOffset.UtcNow.AddDays(365)))
        {
            CertificateRequest req = new CertificateRequest(
                "CN=Valid-Looking Timestamp Authority",
                rsa,
                HashAlgorithmName.SHA256,
                RSASignaturePadding.Pkcs1);
    
            req.CertificateExtensions.Add(
                new X509BasicConstraintsExtension(false, false, 0, false));
    
            req.CertificateExtensions.Add(
                new X509KeyUsageExtension(
                    X509KeyUsageFlags.DigitalSignature | X509KeyUsageFlags.NonRepudiation,
                    false));
    
            req.CertificateExtensions.Add(
                new X509EnhancedKeyUsageExtension(
                    new OidCollection
                    {
                        new Oid("1.3.6.1.5.5.7.3.8")
                    },
                    true));
    
            req.CertificateExtensions.Add(
                new X509SubjectKeyIdentifierExtension(req.PublicKey, false));
    
            using (X509Certificate2 cert = req.Create(
                parentCert,
                DateTimeOffset.UtcNow.AddDays(-1),
                DateTimeOffset.UtcNow.AddDays(90),
                new byte[] { 1, 2, 3, 4 }))
            {
                // Do something with these certs, like export them to PFX,
                // or add them to an X509Store, or whatever.
            }
        }
    }
    

    Longer answer if you're stuck on older versions: To accomplish your goal without adding any new P/Invokes, you would need to read and understand the following documents:

    • ITU-T X.680-201508, the ASN.1 language
    • IETF RFC 5280 or ITU-T X.509, the documents that explain the fields in X.509 certificates.
    • IETF RFC 2986, explains the PKCS#10 certification signing request
    • ITU-T X.690, explains the BER encoding family for ASN.1 (including DER) which tells you how to read and write bytes to achieve the semantic meaning from X.509 / PKCS#10.

    And then you could write a DER writer/reader, and just emit the bytes for what you want.

    0 讨论(0)
提交回复
热议问题