Decrypt my World

Cryptography, Security, Debugging and more!

How to create a certificate request with CertEnroll and .NET (C#)

How to create a certificate request with CertEnroll and .NET (C#)

Rate This

Hi all,

The following C# sample shows how to use CertEnroll COM component to create a certificate request, send the request to the CA, get the response from the CA, and install the new certificate in the machine:

(Note that this sample is a WinForms app with 3 buttons -createRequestButton, sendRequestButton, acceptPKCS7Button- and 2 textboxes -requestText & responseText-)

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

//  Add the CertEnroll namespace

namespace CATest
    public partial class Form1 : Form
        private const int CC_DEFAULTCONFIG = 0;
        private const int CC_UIPICKCONFIG = 0x1;
        private const int CR_IN_BASE64 = 0x1;
        private const int CR_IN_FORMATANY = 0;
        private const int CR_IN_PKCS10 = 0x100;
        private const int CR_DISP_ISSUED = 0x3;
        private const int CR_DISP_UNDER_SUBMISSION = 0x5;
        private const int CR_OUT_BASE64 = 0x1;
        private const int CR_OUT_CHAIN = 0x100;

        public Form1()

        // Create request
        private void createRequestButton_Click(object sender, EventArgs e)
            //  Create all the objects that will be required
            CX509CertificateRequestPkcs10 objPkcs10 = new CX509CertificateRequestPkcs10Class();
            CX509PrivateKey objPrivateKey = new CX509PrivateKeyClass();
            CCspInformation objCSP = new CCspInformationClass();
            CCspInformations objCSPs = new CCspInformationsClass();
            CX500DistinguishedName objDN = new CX500DistinguishedNameClass();
            CX509Enrollment objEnroll = new CX509EnrollmentClass();
            CObjectIds objObjectIds = new CObjectIdsClass();
            CObjectId objObjectId = new CObjectIdClass();
            CX509ExtensionKeyUsage objExtensionKeyUsage = new CX509ExtensionKeyUsageClass(); 
            CX509ExtensionEnhancedKeyUsage objX509ExtensionEnhancedKeyUsage = new CX509ExtensionEnhancedKeyUsageClass();
            string strRequest;

                requestText.Text = "";

                //  Initialize the csp object using the desired Cryptograhic Service Provider (CSP)
                    "Microsoft Enhanced Cryptographic Provider v1.0"

                //  Add this CSP object to the CSP collection object

                //  Provide key container name, key length and key spec to the private key object
                //objPrivateKey.ContainerName = "AlejaCMa";
                objPrivateKey.Length = 1024;
                objPrivateKey.KeySpec = X509KeySpec.XCN_AT_SIGNATURE;
                objPrivateKey.KeyUsage = X509PrivateKeyUsageFlags.XCN_NCRYPT_ALLOW_ALL_USAGES; 
                objPrivateKey.MachineContext = false;

                //  Provide the CSP collection object (in this case containing only 1 CSP object)
                //  to the private key object
                objPrivateKey.CspInformations = objCSPs;

                //  Create the actual key pair

                //  Initialize the PKCS#10 certificate request object based on the private key.
                //  Using the context, indicate that this is a user certificate request and don't
                //  provide a template name

                // Key Usage Extension 
                    X509KeyUsageFlags.XCN_CERT_DIGITAL_SIGNATURE_KEY_USAGE | 
                    X509KeyUsageFlags.XCN_CERT_NON_REPUDIATION_KEY_USAGE | 
                    X509KeyUsageFlags.XCN_CERT_KEY_ENCIPHERMENT_KEY_USAGE | 

                // Enhanced Key Usage Extension
                objObjectId.InitializeFromValue(""); // OID for Client Authentication usage

                //  Encode the name in using the Distinguished Name object

                //  Assing the subject name by using the Distinguished Name object initialized above
                objPkcs10.Subject = objDN;

                // Create enrollment request
                strRequest = objEnroll.CreateRequest(

                requestText.Text = strRequest;

            } catch (Exception ex)

        // Submit request to CA and get response 
        private void sendRequestButton_Click(object sender, EventArgs e)
            //  Create all the objects that will be required
            CCertConfig objCertConfig = new CCertConfigClass();
            CCertRequest objCertRequest = new CCertRequestClass();
            string strCAConfig;
            string strRequest;
            int iDisposition;
            string strDisposition;
            string strCert;

                strRequest = requestText.Text;

                // Get CA config from UI
                //strCAConfig = objCertConfig.GetConfig(CC_DEFAULTCONFIG);
                strCAConfig = objCertConfig.GetConfig(CC_UIPICKCONFIG);                

                // Submit the request
                iDisposition = objCertRequest.Submit(
                    CR_IN_BASE64 | CR_IN_FORMATANY,

                // Check the submission status
                if (CR_DISP_ISSUED != iDisposition) // Not enrolled
                    strDisposition = objCertRequest.GetDispositionMessage();

                    if (CR_DISP_UNDER_SUBMISSION == iDisposition) // Pending
                        MessageBox.Show("The submission is pending: " + strDisposition);
                    else // Failed
                        MessageBox.Show("The submission failed: " + strDisposition);
                        MessageBox.Show("Last status: " + objCertRequest.GetLastStatus().ToString());

                // Get the certificate
                strCert = objCertRequest.GetCertificate(
                    CR_OUT_BASE64 | CR_OUT_CHAIN

                responseText.Text = strCert;
            catch (Exception ex)

        // Install response from CA
        private void acceptPKCS7Button_Click(object sender, EventArgs e)
            //  Create all the objects that will be required
            CX509Enrollment objEnroll = new CX509EnrollmentClass();
            string strCert;
                strCert = responseText.Text;

                // Install the certificate

                MessageBox.Show("Certificate installed!");
            catch (Exception ex)

I hope this helps.



Alex (Alejandro Campos Magencio)

  • Very good article, it helps me a lot :-)

  • I got the error:

    "The submission failed: Denied by Policy Module 0x80094801, The request does not contain a certificate template extension or the CertificateTemplate request attribute."

    How can I specify a certificate template extension?


  • Hi,

    can you explain a little how to do the same with the "on behalf" feature ?

    I imagine, you need to sign the request before submiting ? how ?



  • Hello

    Thank you for your sample code

    But I got an Error

    using CERTENROLLLib;

    using CERTCLIENTLib;

    the above using **** has error..

    Dose it must add some reference in this project?

    thank you very much

  • Yes, you have to add references to the CertEnroll & CertCli COM objects (Project > Add Reference > COM).

  • how does one compile this code?

    csc /t:library /r:cercli.dll does not work. Thanks.

  • Henok, take into account this is just a sample, and it's just a file of a complete project. This is part of a form part of a very basic WinForms project created with VS2008.

  • While trying to run the given example I am getting the error : CCertRequest::Submit: The RPC server is unavailable. 0x800706ba (WIN32: 1722).

    I am using Vista Ultimate as the client to run this application and submitting request to Standalone CA hosted on W2003 Enterprise edition server with Service Pack 2 and Hotfix for CA services for supporting Vista and 2008 Clients . This server is not part of any domain server. Just connceted to my companies intranet.I am able to get the certificate from this Vista machine through web enrollment module.

    I have checked that CA server and RPC server are running.

    Please give any pointer to what could be the problem.

    Thanks in advance.


  • Thanks for the code, it is rare to find a similar one.

    I have 2 questions:

    1. If I have already a public key, how do I add it to the request.

    2. How to specify the CA to which I wil submit the request.

    Thanks in advance

  • Thanks for the sample code.

    But i now i am facing 2 problems when i want to generate key from hardware token and download cert from CA to token:

    1) When i call the CreateRequest(1) during create csr, it generate private and public key pair and immediately store a certificate into my token. If I just want to store the key pair only, what should i set

    2) When i call InstallResponse(4,strCert,0,"") to install the cert to token is shows error "Data is invalid. 0x8007000d"

  • I don't the answers on the top of my head, and I would need time to reproduce and investigate. If you need help with this I suggest you open a case with us, Microsoft Technical Support.



  • How I can specify CA?

    when I put

    strCAConfig = "" (Windows 2003 CA Authority) getting

    {"CCertRequest::Submit: The RPC server is unavailable. 0x800706ba (WIN32: 1722)"}

  • Check the following MSDN article for details on how to specify a CA in the strCAConfig variable:

    ICertRequest2::Submit Method


    strConfig [in]

    Represents a valid configuration string for the Certificate Services server in the form COMPUTERNAME\CANAME, where COMPUTERNAME is the server's network name, and CANAME is the common name of the certification authority, as entered during Certificate Services setup. For information about the configuration string name, see ICertConfig.


    I hope this helps.



  • finally get the certificate!!! How would I create pfx file out of it.

    Here are my steps (according to your post):

    1. objEnroll.Initialize

    2. objEnroll.InstallResponse

    3. objEnroll.CreatePFX which return me a string

    How I can create pfx file?

Page 1 of 7 (102 items) 12345»
Leave a Comment
  • Please add 8 and 3 and type the answer here:
  • Post
Translate This Page