Search code examples
visual-studioc++-clicertificateexex509certificate2

Attach .pfx certificate to exe file


I have to deploy a software to n clients that will install a certificate and use it. I don't want to deploy two files (.pfx and exe) just one (.exe that will contain the .pfx).

What i'm doing now is import the certificate from a location.

X509Certificate2^ x509 = gcnew X509Certificate2;
x509->Import( "C:\\Tmp\\certficate.pfx" );

Is it possible ?


Solution

  • You could always embed the certificate data as a resource.

    One warning though: if someone gets the executable, they can pull out the PFX file pretty easily.

    Are you able to securely distribute the executable?

    Here are some rough steps, distilled from: http://www.spikezilla-software.com/blog/?p=24

    • Add the PFX to your project. Then click once on the file, and in the Properties window, set the Build Action to Embedded Resource
    • Read the embedded PFX file and import the certificate

    This is C# but you should be able to translate to C++/CLI pretty easily:

    var stream = Assembly.GetExecutingAssembly().GetManifestResourceStream("MyFile.pfx");
    var bytes = new byte[stream.Length];
    stream.Read(bytes, 0, bytes.Length);
    var cert = new X509Certificate2(bytes, "certPassword");