Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to create .pfx file from certificate and private key?

I need .pfx file to install https on website on IIS.

I have two separate files: certificate (.cer or pem) and private key (.crt) but IIS accepts only .pfx files.

I obviously installed certificate and it is available in certificate manager (mmc) but when I select Certificate Export Wizard I cannot select PFX format (it's greyed out)

Are there any tools to do that or C# examples of doing that programtically?

like image 971
jlp Avatar asked Jun 10 '11 14:06

jlp


People also ask

Does pfx include private key?

A PFX file is a certificate in PKCS#12 format. It contains the SSL certificate (public keys) and the corresponding private keys.


4 Answers

You will need to use openssl.

openssl pkcs12 -export -out domain.name.pfx -inkey domain.name.key -in domain.name.crt

The key file is just a text file with your private key in it.

If you have a root CA and intermediate certs, then include them as well using multiple -in params

openssl pkcs12 -export -out domain.name.pfx -inkey domain.name.key -in domain.name.crt -in intermediate.crt -in rootca.crt

If you have a bundled crt file that you use, for example, with nginx, you can pass that in along with the cert all in one:

cat domain.name.crt | tee -a domain.name.bundled.crt
cat intermediate.crt | tee -a domain.name.bundled.crt
cat rootca.crt | tee -a domain.name.bundled.crt
openssl pkcs12 -export -out domain.name.pfx \
  -inkey domain.name.key \
  -in domain.name.bundled.crt 

You can install openssl from here: openssl

like image 187
jdehlin Avatar answered Oct 21 '22 20:10

jdehlin


If you're looking for a Windows GUI, check out DigiCert. I just used this and it was fairly simple.

Under the SSL tab, I first Imported the Certificate. Then once I selected the Certificate I was able to export as a PFX, both with and without a keyfile.

https://www.digicert.com/util

like image 35
joelnet Avatar answered Oct 21 '22 19:10

joelnet


The Microsoft Pvk2Pfx command line utility seems to have the functionality you need:

Pvk2Pfx (Pvk2Pfx.exe) is a command-line tool copies public key and private key information contained in .spc, .cer, and .pvk files to a Personal Information Exchange (.pfx) file.
http://msdn.microsoft.com/en-us/library/windows/hardware/ff550672(v=vs.85).aspx

Note: if you need/want/prefer a C# solution, then you may want to consider using the http://www.bouncycastle.org/ api.

like image 41
Seymour Avatar answered Oct 21 '22 19:10

Seymour


You do NOT need openssl or makecert or any of that. You also don't need the personal key given to you by your CA. I can almost guarantee that the problem is that you expect to be able to use the key and cer files provided by your CA but they aren't based on "the IIS way".

SSL Certs for IIS with PFX once and for all - SSL and IIS Explained - http://rainabba.blogspot.com/2014/03/ssl-certs-for-iis-with-pfx-once-and-for.html

Use IIS "Server Certificates" UI to "Generate Certificate Request" (the details of this request are out of the scope of this article but those details are critical). This will give you a CSR prepped for IIS. You then give that CSR to your CA and ask for a certificate. Then you take the CER/CRT file they give you, go back to IIS, "Complete Certificate Request" in the same place you generated the request. It may ask for a .CER and you might have a .CRT. They are the same thing. Just change the extension or use the . extension drop-down to select your .CRT. Now provide a proper "friendly name" (*.yourdomain.example, yourdomain.example, foo.yourdomain.example, etc..) THIS IS IMPORTANT! This MUST match what you setup the CSR for and what your CA provided you. If you asked for a wildcard, your CA must have approved and generated a wildcard and you must use the same. If your CSR was generated for foo.yourdomain.example, you MUST provide the same at this step.

like image 33
rainabba Avatar answered Oct 21 '22 19:10

rainabba