Set the friendly name on a certificate during import

Posted: September 15, 2009 in C#, CA, Certificate Authority, Microsoft

So here is a commmand line example of how to do this. You need CAPICOM from microsoft which wraps the CryptoAPI.

The friendly name is a property of the cert store rather than the certificate so this code imports a certificate to the cert store and sets the friendly name as it does so.

The code takes two parameters the path to the cert file and the friendly name you wish to set.


using System;

using System.Collections.Generic;

using System.Text;

using CAPICOM;

using System.Collections;

using System.Runtime.InteropServices;

namespace CertTool


    class Program
        const uint CERT_SYSTEM_STORE_LOCAL_MACHINE = 0x20000;
        const int CAPICOM_PROPID_FRIENDLY_NAME = 11;
        const int CAPICOM_ENCODE_BINARY = 1;

        static private String _currStoreName = "My";
        static private String _FriendlyName = "Not Set";
        static private String _CertPath = "C:\\test.cer";
        static StoreClass _oCurrStore;
        static ExtendedPropertyClass _friendlyProp;
        static CertificateClass _certificate;
        static ExtendedProperties _extendedProp;

        static void Main(string[] args)
                //Friendly name Argument
                if (args.Length > 0)
                    _FriendlyName = args[0];
                //Certpath argument
                if (args.Length > 1) 
                    _CertPath = args[1];
                //Set and open the Store
                _oCurrStore = new StoreClass();
                //Call the import certificate function
            catch(Exception ex){
        //Function import the certificate to the machine store and sets the friendly name
        static bool importCert()
                //Create Certificate Object
                _certificate = new CertificateClass();
                //Load the certificate into the obejct from file
                //Create extended property Class for friendly name
                _friendlyProp = new ExtendedPropertyClass();
                _friendlyProp.PropID =  CAPICOM_PROPID.CAPICOM_PROPID_FRIENDLY_NAME;
                _friendlyProp.set_Value(CAPICOM_ENCODING_TYPE.CAPICOM_ENCODE_BINARY, _FriendlyName);

                //Add extendedProp on cert object
                _extendedProp = _certificate.ExtendedProperties();
                //Set extendded prop to friendly name object
                return true;
            catch (Exception e)
                return true;

Blacklog is a product of Blacktip Ltd

Feel free to link me in Mark Sutton CISSP
View Mark Sutton CISSP's profile on LinkedIn
I can also be found at twitter msutton


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s