How to create self-signed certificate programmatically for WCF service?

13

I have a self-hosted WCF server running as a Windows service under the Local System account. I am trying to create a self-signed certificate programmatically in c# for use with a net.tcp endpoint using Message level security.

I am using the following code which is very closely based on the accepted answer in How to create a self-signed certificate using C#? with some small changes trying to solve my problem.

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
    );
}

And I store it with this code:

X509Store store = new X509Store(storeName, StoreLocation.LocalMachine);
store.Open(OpenFlags.ReadWrite);
store.Add(newCert);
store.Close();

This creates the certificate and puts it in the LocalMachine certificate store. The problem is that when I try to start the WCF service I get the following exception:

It is likely that certificate 'CN=myCertificate' may not have a private key that is capable of key exchange or the process may not have access rights for the private key. Please see inner exception for detail. Inner exception: Keyset does not exist

The output of the FindPrivateKey sample (http://msdn.microsoft.com/en-us/library/aa717039%28v=vs.100%29.aspx) for my certificate is:

Private key directory:
C:\ProgramData\Microsoft\Crypto\RSA\MachineKeys
Private key file name:
f0d47c7826b8ef5148b6d412f1c40024_4a8a026f-58e4-40f7-b779-3ae9b6aae1a7

I can see this 1.43KB file in explorer. If I look at the properties|Security I see SYSTEM and Administrators both with Full control.

In researching this error I have seen many answers about the private key missing or incorrect permissions. I can't see what the problem is.

The really strange thing is that if I use the mmc Certificate plugin, go to the certificate and choose All Tasks|Manage Private Keys... I see the same security settings. After viewing this even if I just bring up the dialog and hit the Cancel button the certificate now works correctly in WCF. I can simply restart the service and everything runs perfectly.

If I create a certificate using MakeCert it works just fine from the start. I don't know what it does differently.

One other piece of information that may not be relevant is that the certificate not only gets put in the My store where I told it to get put, but it also gets put in the "Intermediate Certification Authorities" store. I don't know why or if it matters.

So...any ideas what I am doing wrong?

UPDATE: Well, this is not just a WCF issue. I essentially get the same problem when I try to use the certificate to bind to an endpoint with http.sys using HttpSetServiceConfiguration. The method returns 1312 - "A specified logon session does not exist. It may already have been terminated". This is actually not the real error. I saw in the Security Event log an Audit Failure that say this:

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

0x80090011 is Object was not found. So this appears to be the same problem. Again, after I open the Manage Private Keys dialog for the certificate this works perfectly also.

I am still looking for the cause of the problem.

UPDATE #2: I was able to get this working using the accepted answer below. Interestingly, this code now seems to put the certificate in the Machine store without calling the X509Store code. I still call the code because I am not sure and it does not hurt anything. Here is the final code that I am using to create the certificate.

    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
        );
    }
c#
.net
certificate
wcf-security
self-signed
asked on Stack Overflow Aug 20, 2013 by MarkR • edited May 23, 2017 by Community

3 Answers

5

I could not make this work, but I found an alternate solution. (Update December 2014: I have now gotten it to work using the accepted answer.)

I was able to use the PluralSight.Crypto library to achieve what I need. I had to modify the source code slightly to get the private key to store in the LocalMachine store. The changes I made were to the file CryptContext.cs. I changed the CreateSelfSignedCertificate method. Following is a snippet of code including the change that I made. In essence, I set the Flags member of the CryptKeyProviderInformation structure to set it to 0x20 (CRYPT_MACHINE_KEYSET) if the CryptContext object contains this value in its Flags.

        byte[] asnName = properties.Name.RawData;
        GCHandle asnNameHandle = GCHandle.Alloc(asnName, GCHandleType.Pinned);

        int flags = 0;                    // New code
        if ((this.Flags & 0x20) == 0x20)  // New code
            flags = 0x20;                 // New code

        var kpi = new Win32Native.CryptKeyProviderInformation
        {
            ContainerName = this.ContainerName,
            KeySpec = (int)KeyType.Exchange,
            ProviderType = 1, // default RSA Full provider
            Flags = flags                 // New code
        };

Then I use the function in my own code like this:

        using (Pluralsight.Crypto.CryptContext ctx = new Pluralsight.Crypto.CryptContext()) {

            ctx.Flags = 0x8 | 0x20;
            ctx.Open();

            X509Certificate2 cert = ctx.CreateSelfSignedCertificate(
                new Pluralsight.Crypto.SelfSignedCertProperties
                {
                    IsPrivateKeyExportable = true,
                    KeyBitLength = 4096,
                    Name = new X500DistinguishedName("CN=" + subjectName),
                    ValidFrom = DateTime.Today,
                    ValidTo = DateTime.Today + expirationLength,
                });

            return cert;
        }

Notice that I set the Flags for the CryptContext object to be 0x8 | 0x20 (CRYPT_NEWKEYSET | CRYPT_MACHINE_KEYSET).

I wish I could figure out what was wrong with my original solution. But I need something to work and in my testing this solution does what I need. I hope it helps someone else along the way.

answered on Stack Overflow Sep 6, 2013 by MarkR • edited Dec 10, 2014 by MarkR
5

I had the same issue using the equivalent code in PowerShell. It appears that sometime the private key just disappears. I used Process Monitor and you can see the key file being deleted.

The way I solved this was to add X509KeyStorageFlags.PersistKeySet to the X509Certificate2 constructor.

answered on Stack Overflow Dec 20, 2013 by Paul Durbin • edited Nov 13, 2015 by sebagomez
3

You can also use the CLR Security library on CodePlex (https://clrsecurity.codeplex.com/). Here is sample code which creates a self signed certificate, and tests it with SSLStream.

        var machineName = Environment.MachineName;
        var keyCreationParameters = new CngKeyCreationParameters();
        keyCreationParameters.KeyUsage = CngKeyUsages.AllUsages;
        keyCreationParameters.KeyCreationOptions = CngKeyCreationOptions.OverwriteExistingKey;
        keyCreationParameters.Parameters.Add(new CngProperty("Length", BitConverter.GetBytes(4096), CngPropertyOptions.None));
        var cngKey = CngKey.Create(CngAlgorithm2.Rsa, "Test", keyCreationParameters);

        var x500DistinguishedName = new X500DistinguishedName("CN=" + machineName);
        x500DistinguishedName.Oid.Value = "1.3.6.1.5.5.7.3.1";
        var certificateCreationParameters = new X509CertificateCreationParameters(x500DistinguishedName);
        certificateCreationParameters.SignatureAlgorithm = X509CertificateSignatureAlgorithm.RsaSha512;
        certificateCreationParameters.TakeOwnershipOfKey = true;
        certificateCreationParameters.CertificateCreationOptions = X509CertificateCreationOptions.None;
        certificateCreationParameters.EndTime = new DateTime(9999, 12,31, 23, 59, 59, 999, DateTimeKind.Utc);
        var certificate = cngKey.CreateSelfSignedCertificate(certificateCreationParameters);

        var certificateStore = new X509Store(StoreName.Root, StoreLocation.CurrentUser);
        certificateStore.Open(OpenFlags.ReadWrite);
        certificateStore.Add(certificate);
        certificateStore.Close();


        var tcpListener = TcpListener.Create(6666);
        tcpListener.Start();
        var client = new TcpClient("localhost", 6666);
        var acceptedClient = tcpListener.AcceptTcpClient();
        var acceptedClinetSslStream = new SslStream(
            acceptedClient.GetStream(), false);
        var serverAuthTask = acceptedClinetSslStream.AuthenticateAsServerAsync(certificate,
                            false, SslProtocols.Tls, true);

        SslStream clientSslStream = new SslStream(
            client.GetStream(),
            false,
            delegate(object o, X509Certificate x509Certificate, X509Chain chain, SslPolicyErrors errors)
                {
                    if (errors == SslPolicyErrors.None)
                        return true;

                    Console.WriteLine("Certificate error: {0}", errors);

                    // Do not allow this client to communicate with unauthenticated servers. 
                    return false;
                },
            null);
        var clientAuthTask = clientSslStream.AuthenticateAsClientAsync(machineName);

        Task.WaitAll(serverAuthTask, clientAuthTask);
answered on Stack Overflow Mar 2, 2015 by Paul

User contributions licensed under CC BY-SA 3.0