当前位置: 首页 > 知识库问答 >
问题:

如何为WCF服务以编程方式创建自签名证书?

翟曦
2023-03-14

我有一个在本地系统帐户下作为Windows服务运行的自托管WCF服务器。我正在尝试用C#以编程方式创建一个自签名证书,用于使用消息级安全性的Net.tcpendpoint。

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

    CX509PrivateKey privateKey = new CX509PrivateKey();
    privateKey.ProviderName = "Microsoft Strong Cryptographic Provider";
    privateKey.Length = 1024;
    privateKey.KeySpec = X509KeySpec.XCN_AT_KEYEXCHANGE;
    privateKey.KeyUsage = X509PrivateKeyUsageFlags.XCN_NCRYPT_ALLOW_DECRYPT_FLAG | X509PrivateKeyUsageFlags.XCN_NCRYPT_ALLOW_KEY_AGREEMENT_FLAG;
    privateKey.MachineContext = true;
    privateKey.ExportPolicy = X509PrivateKeyExportFlags.XCN_NCRYPT_ALLOW_EXPORT_FLAG;
    privateKey.Create();

    // Use the stronger SHA512 hashing algorithm
    var hashobj = new CObjectId();
    hashobj.InitializeFromAlgorithmName(ObjectIdGroupId.XCN_CRYPT_HASH_ALG_OID_GROUP_ID,
        ObjectIdPublicKeyFlags.XCN_CRYPT_OID_INFO_PUBKEY_ANY,
        AlgorithmFlags.AlgorithmFlagsNone, "SHA1");

    // Create the self signing request
    var cert = new CX509CertificateRequestCertificate();
    cert.InitializeFromPrivateKey(X509CertificateEnrollmentContext.ContextMachine, privateKey, "");
    cert.Subject = dn;
    cert.Issuer = dn; // the issuer and the subject are the same
    cert.NotBefore = DateTime.Now.Date;
    // this cert expires immediately. Change to whatever makes sense for you
    cert.NotAfter = cert.NotBefore + expirationLength;
    //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)
        // mark private key to go into the Machine store instead of the current users store
        X509KeyStorageFlags.Exportable | X509KeyStorageFlags.MachineKeySet
    );
}
X509Store store = new X509Store(storeName, StoreLocation.LocalMachine);
store.Open(OpenFlags.ReadWrite);
store.Add(newCert);
store.Close();
Private key directory:
C:\ProgramData\Microsoft\Crypto\RSA\MachineKeys
Private key file name:
f0d47c7826b8ef5148b6d412f1c40024_4a8a026f-58e4-40f7-b779-3ae9b6aae1a7

我可以在资源管理器中看到这个1.43KB的文件。如果我查看propertiesSecurity,我会看到SYSTEM和Administrators都具有完全控制权。

在研究这个错误时,我看到了许多关于私钥丢失或权限不正确的答案。我看不出有什么问题。

真正奇怪的是,如果我使用mmc证书插件,转到证书并选择所有任务管理私钥...我看到了相同的安全设置。查看后,即使我只是打开对话框并点击Cancel按钮,证书现在也可以在WCF中正常工作。我可以简单地重新启动服务,一切都运行得很完美。

Cryptographic Parameters:
    Provider Name:  Microsoft Software Key Storage Provider
    Algorithm Name: Not Available.
    Key Name:   {A23712D0-9A7B-4377-89DB-B1B39E3DA8B5}
    Key Type:   Machine key.

Cryptographic Operation:
    Operation:  Open Key.
    Return Code:    0x80090011

我还在寻找问题的原因。

更新#2:我能够使用下面的公认答案使这个工作。有趣的是,这段代码现在似乎将证书放在机器存储中,而不需要调用X509Store代码。我仍然调用代码,因为我不确定,它没有伤害任何东西。下面是我用来创建证书的最终代码。

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

        CX509PrivateKey privateKey = new CX509PrivateKey();
        privateKey.ProviderName = "Microsoft Strong Cryptographic Provider";
        privateKey.Length = 2048;
        privateKey.KeySpec = X509KeySpec.XCN_AT_KEYEXCHANGE;
        privateKey.KeyUsage = X509PrivateKeyUsageFlags.XCN_NCRYPT_ALLOW_DECRYPT_FLAG | X509PrivateKeyUsageFlags.XCN_NCRYPT_ALLOW_KEY_AGREEMENT_FLAG;
        privateKey.MachineContext = true;
        privateKey.ExportPolicy = X509PrivateKeyExportFlags.XCN_NCRYPT_ALLOW_PLAINTEXT_EXPORT_FLAG;
        privateKey.Create();

        // Use the stronger SHA512 hashing algorithm
        var hashobj = new CObjectId();
        hashobj.InitializeFromAlgorithmName(ObjectIdGroupId.XCN_CRYPT_HASH_ALG_OID_GROUP_ID,
            ObjectIdPublicKeyFlags.XCN_CRYPT_OID_INFO_PUBKEY_ANY,
            AlgorithmFlags.AlgorithmFlagsNone, "SHA512");

        // Create the self signing request
        var cert = new CX509CertificateRequestCertificate();
        cert.InitializeFromPrivateKey(X509CertificateEnrollmentContext.ContextMachine, privateKey, "");
        cert.Subject = dn;
        cert.Issuer = dn; // the issuer and the subject are the same
        cert.NotBefore = DateTime.Now.Date;
        // this cert expires immediately. Change to whatever makes sense for you
        cert.NotAfter = cert.NotBefore + expirationLength;
        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)
            // mark private key to go into the Machine store instead of the current users store
            X509KeyStorageFlags.Exportable | X509KeyStorageFlags.MachineKeySet | X509KeyStorageFlags.PersistKeySet
        );
    }

共有1个答案

燕昊东
2023-03-14

我在PowerShell中使用相同的代码时也遇到了同样的问题。似乎有时私钥就会消失。我使用了进程监视器,您可以看到正在删除的密钥文件。

我解决这一问题的方法是将X509KeyStorageFlags.PersistKeySet添加到X509Certificate2构造函数中。

 类似资料:
  • 问题内容: 我想使用jarsigner对jar进行签名,然后使用Java应用程序对其进行验证,该Java应用程序没有将签名的jar作为其类路径的一部分(即,仅使用jar的文件系统位置) 现在我的问题是从罐子中取出签名文件,有没有简单的方法可以做到这一点? 我玩过Inflater和Jar InputStreams并没有运气。 还是可以通过更好的方式来实现? 谢谢 问题答案: 《安全提供程序实施指南》

  • 我正尝试使用以下命令以编程方式创建:

  • 我正在为嵌入式Linux设备添加HTTPS支持。我尝试通过以下步骤生成自签名证书: 这是可行的,但我得到一些错误,例如,谷歌Chrome:

  • 问题内容: 是否有免费的库可根据图像序列创建MPEG(或任何其他简单的视频格式)? 它也必须在Linux上运行,并且最好具有Python绑定。 问题答案: 我知道有一个mencoder(是mplayer项目的一部分)和ffmpeg,它们都可以做到这一点。

  • 我有一个充满自定义视图的应用程序。当我尝试以编程方式创建FAB时,它会抛出一个错误 原因:java.lang.IllegalArgumentException:您需要在设计库中使用theme.AppCompat主题(或后代)。

  • 问题内容: 我已经开始为API编写包装,该包装要求所有请求都通过HTTPS进行。我不想在开发和测试它时向实际的API发出请求,而是希望在本地运行自己的服务器来模拟响应。 我对如何生成创建HTTPS服务器并向其发送请求所需的证书感到困惑。 我的服务器看起来像这样: Pem文件是使用以下命令生成的: 一个请求看起来像这样: 通过此设置,我得到了,所以我认为我需要为该请求添加一个选项。 所以我的问题是我