【问题标题】:Upgrading from makecert.exe to CertEnroll - issues with certificate trust从 makecert.exe 升级到 CertEnroll - 证书信任问题
【发布时间】:2019-12-13 11:45:37
【问题描述】:

到目前为止,我有一个应用程序使用 makecert.exe 生成自我证书。但是由于 makecert 无法添加 SubjectAltName 字段,我需要将代码迁移到 certenroll.dll

这是原始的 makecert 代码:

public static X509Certificate2 MakeCert(string subjectName)
    {
        X509Certificate2 cert;
        string certFile = Path.Combine(Path.GetTempPath(), subjectName + ".cer");

        var process = new Process
        {
            StartInfo = new ProcessStartInfo
            {
                FileName = "makecert.exe",
                Arguments = " -pe -ss my -n \"CN=" + subjectName + ", O=myCert, OU=Created by me\" -sky exchange -in MyCustomRoot -is my -eku 1.3.6.1.5.5.7.3.1 -cy end -a sha1 -m 132 -b 10/08/2018 " + certFile,
                UseShellExecute = false,
                RedirectStandardOutput = true,
                CreateNoWindow = true
            }
        };

        process.Start();
        string str = "";
        while (!process.StandardOutput.EndOfStream)
        {
            var line = process.StandardOutput.ReadLine();
            str += line;
            //Console.WriteLine(line);
        }
        process.WaitForExit();

        cert = new X509Certificate2(certFile);
        // Install Cert
        try
        {

            var store = new X509Store(StoreName.My, StoreLocation.CurrentUser);
            store.Open(OpenFlags.ReadWrite);
            try
            {
                var contentType = X509Certificate2.GetCertContentType(certFile);
                var pfx = cert.Export(contentType);
                cert = new X509Certificate2(pfx, (string)null, X509KeyStorageFlags.PersistKeySet | X509KeyStorageFlags.MachineKeySet);
                store.Add(cert);
            }
            finally
            {
                store.Close();
            }
        }
        catch (Exception ex)
        {
            Console.WriteLine(String.Format("Could not create the certificate from file from {0}", certFile), ex);
        }
        return cert;
    }

这是 certenroll.dll 代码:

   public static X509Certificate2 CertOpen(string subjectName)
    {
        try
        {
            X509Store store = new X509Store("My", StoreLocation.CurrentUser);
            store.Open(OpenFlags.ReadOnly);
            try
            {
                var cer = store.Certificates.Find(
                    X509FindType.FindBySubjectName,
                    subjectName,
                    false);

                if (cer.Count > 0)
                {
                    return cer[0];
                }
                else
                {
                    return null;
                }
            }
            finally
            {
                store.Close();
            }
        }
        catch
        {
            return null;
        }
    }

    public static X509Certificate2 CertCreateNew(string subjectName)
    {
        // create DN for subject and issuer
        var dn = new CX500DistinguishedName();
        dn.Encode("CN=" + subjectName, X500NameFlags.XCN_CERT_NAME_STR_NONE);


        // create a new private key for the certificate
        CX509PrivateKey privateKey = new CX509PrivateKey();
        privateKey.ProviderName = "Microsoft Base Cryptographic Provider v1.0";
        privateKey.MachineContext = false;
        privateKey.Length = 2048;
        privateKey.KeySpec = X509KeySpec.XCN_AT_SIGNATURE; // use is not limited
        privateKey.ExportPolicy = X509PrivateKeyExportFlags.XCN_NCRYPT_ALLOW_PLAINTEXT_EXPORT_FLAG;
        privateKey.Create();


        var hashobj = new CObjectId();
        hashobj.InitializeFromAlgorithmName(ObjectIdGroupId.XCN_CRYPT_HASH_ALG_OID_GROUP_ID,
            ObjectIdPublicKeyFlags.XCN_CRYPT_OID_INFO_PUBKEY_ANY,
            AlgorithmFlags.AlgorithmFlagsNone, "SHA256");

        // add extended key usage if you want - look at MSDN for a list of possible OIDs
        var oid = new CObjectId();
        oid.InitializeFromValue("1.3.6.1.5.5.7.3.1"); // SSL server
        var oidlist = new CObjectIds();
        oidlist.Add(oid);
        var eku = new CX509ExtensionEnhancedKeyUsage();
        eku.InitializeEncode(oidlist);

        // Create the self signing request
        var cert = new CX509CertificateRequestCertificate();

        cert.InitializeFromPrivateKey(X509CertificateEnrollmentContext.ContextUser, privateKey, "");

        X509Certificate2 signer = CertOpen("MyCustomRoot");
        if (signer == null)
        {
            throw new CryptographicException("Signer not found");
        }
        String base64str = Convert.ToBase64String(signer.RawData);


        ISignerCertificate signerCertificate = new CSignerCertificate();
        signerCertificate.Initialize(false, X509PrivateKeyVerify.VerifySilent, EncodingType.XCN_CRYPT_STRING_BASE64, base64str);
        // this line MUST be called AFTER IX509CertificateRequestCertificate.InitializeFromPrivateKey call,
        // otherwise you will get OLE_E_BLANK uninitialized object error.
        cert.SignerCertificate = (CSignerCertificate)signerCertificate;


        cert.Subject = dn;
        cert.Issuer.Encode(signer.Subject, X500NameFlags.XCN_CERT_NAME_STR_NONE); ; // the issuer and the subject are the same
        cert.NotBefore = DateTime.Now;
        // this cert expires immediately. Change to whatever makes sense for you
        cert.NotAfter = DateTime.Now.AddYears(10);
        cert.X509Extensions.Add((CX509Extension)eku); // add the EKU
        cert.HashAlgorithm = hashobj; // Specify the hashing algorithm
        cert.Encode(); // encode the certificate

        // Do the final enrollment process
        var enroll = new CX509Enrollment();
        enroll.InitializeFromRequest(cert); // load the certificate
        enroll.CertificateFriendlyName = subjectName; // Optional: add a friendly name

        string csr = enroll.CreateRequest(); // Output the request in base64
                                             // and install it back as the response
        enroll.InstallResponse(InstallResponseRestrictionFlags.AllowUntrustedCertificate,
            csr, EncodingType.XCN_CRYPT_STRING_BASE64, ""); // no password
                                                            // output a base64 encoded PKCS#12 so we can import it back to the .Net security classes
        var base64encoded = enroll.CreatePFX("", // no password, this is for internal consumption
            PFXExportOptions.PFXExportChainWithRoot);

        // instantiate the target class with the PKCS#12 data (and the empty password)
        return new System.Security.Cryptography.X509Certificates.X509Certificate2(
            System.Convert.FromBase64String(base64encoded), "",
            // mark the private key as exportable (this is usually what you want to do)
            System.Security.Cryptography.X509Certificates.X509KeyStorageFlags.Exportable
        );
    }

除了 Crypt32 的帮助之外,我现在遇到了 signerCertificate.Initialize 行的问题。我似乎无法让它使用我的自我证书。根证书。我假设我正在尝试以错误的格式提供它,因为我收到以下错误:

证书没有引用私有的属性 钥匙。 0x8009200a (CRYPT_E_UNEXPECTED_MSG_TYPE)

【问题讨论】:

    标签: c# ssl-certificate x509certificate2 makecert certenroll


    【解决方案1】:

    您必须在IX509CertificateRequestCertificate 对象的SignerCertificate 属性中指定签名者证书(代码中的cert 变量)。签名者证书必须以ISignerCertificate 实例的形式提供。更多信息:ISignerCertificate interface

    更新 1 (13.12.2019)

    很抱歉,ISignerCertificate 通话中的几乎每一部分都是不正确的。

    1. 如果指定X509PrivateKeyVerify.VerifyNone,则不检查私钥是否存在。您需要使用X509PrivateKeyVerify.VerifySilent 标志。

    2. 您正在使用带有 PEM 页眉和页脚的 Base64 格式将证书格式化为字符串。您正在使用EncodingType.XCN_CRYPT_STRING_BASE64,它需要没有 PEM 信封的原始 Base64 字符串。 PEM 格式的证书使用EncodingType.XCN_CRYPT_STRING_BASE64HEADER 编码类型。在你的情况下,我会这样做:


    X509Certificate signer = CertOpen("MyCustomRoot");
    if (signer == null) {
        throw new CryptographicException("Signer not found");
    }
    String base64str = Convert.ToBase64String(signer.RawData);
    signerCertificate.Initialize(false, X509PrivateKeyVerify.VerifySilent, EncodingType.XCN_CRYPT_STRING_BASE64, base64str);
    
    <...>
    // put issuer directly from issuer cert:
    issuer.Encode(signer.Subject, X500NameFlags.XCN_CERT_NAME_STR_NONE);
    <...>
    cert.InitializeFromPrivateKey(X509CertificateEnrollmentContext.ContextUser, privateKey, "");
    // this line MUST be called AFTER IX509CertificateRequestCertificate.InitializeFromPrivateKey call,
    // otherwise you will get OLE_E_BLANK uninitialized object error.
    cert.SignerCertificate = signerCertificate;
    

    还有一些小的改进:

    1. CertOpen 方法中,您不会关闭商店。
    2. if (cer != null &amp;&amp; cer.Count &gt;0) -- IIRC,X509Certificate2Collection.Find 永远不会返回 null,所以只需检查返回的集合是否为非空。
    3. 您在初始化之前将 ISignerCertificate 对象分配给请求。请参阅上面的我的 cmets。
    4. 请记住,SHA512 在默认情况下并未在所有加密模块中启用。 SHA512 is disabled in Windows when you use TLS 1.2

    更新 2 (14.12.2019)

    我用我昨天提供的修改重新编写了代码,代码有效。 CRYPT_E_UNEXPECTED_MSG_TYPE 错误表明签名者证书在证书存储中没有私钥。

    【讨论】:

    • 谢谢,我在代码中添加了以下内容: ISignerCertificate signerCertificate = new CSignerCertificate(); signerCertificate.Initialize(false, X509PrivateKeyVerify.VerifyNone, EncodingType.XCN_CRYPT_STRING_HEX,??); cert.SignerCertificate = (CSignerCertificate)signerCertificate;但我无法计算出需要什么来代替 ??当 signerCertificate 被初始化时。它要求 strCertificate。这是根证书吗?如果是,为什么 makecert 不需要它时需要它,这是否意味着我需要从商店中提取根证书
    • ISignerCertificate 初始化程序需要证书或指纹(如果在 Windows 7 或更新的操作系统上)。
    • 再次感谢。我尝试使用 var cer = store.Certificates.Find(X509FindType.FindBySubjectName, "MyCustomRoot", false); 检索根证书并将其作为字符串提供给 Initialize,但它会将其吐出。这是错误的做法吗? P.S 您可以进行小型项目吗?我可以使用真正了解加密的人。它在很大程度上超出了我的范围。
    • 错误是:证书没有引用私钥的属性。 0x8009200a (CRYPT_E_UNEXPECTED_MSG_TYPE)
    • ISignerCertificate signerCertificate = new CSignerCertificate(); signerCertificate.Initialize(false, X509PrivateKeyVerify.VerifyNone, EncodingType.XCN_CRYPT_STRING_BASE64,ExportToPEM(CertOpen("MyCustomRoot"))); cert.SignerCertificate = (CSignerCertificate)signerCertificate; CertOpen() - 使用 X509Store.Certificates.Find 定位根证书 ExportToPem() - 返回带有“BEGIN/END CERTIFICATE”行的字符串根证书并使用 (Convert.ToBase64String(cert.Export(X509ContentType.Cert), Base64FormattingOptions. InsertLineBreaks))
    猜你喜欢
    • 1970-01-01
    • 2016-04-27
    • 1970-01-01
    • 1970-01-01
    • 2019-10-06
    • 1970-01-01
    • 2020-11-08
    • 1970-01-01
    • 2023-01-19
    相关资源
    最近更新 更多