Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save alogoc/e3fc76194794363a65d25c7ca140e31f to your computer and use it in GitHub Desktop.
Save alogoc/e3fc76194794363a65d25c7ca140e31f to your computer and use it in GitHub Desktop.
How to convert a .pfx SSL certificate to .crt/key (pem) formats. Useful for NGINX
source: http://www.markbrilman.nl/2011/08/howto-convert-a-pfx-to-a-seperate-key-crt-file/
`openssl pkcs12 -in [yourfile.pfx] -nocerts -out [keyfile-encrypted.key]`
What this command does is extract the private key from the .pfx file. Once entered you need to type in the importpassword of the .pfx file. This is the password that you used to protect your keypair when you created your .pfx file. If you cannot remember it anymore you can just throw your .pfx file away, cause you won’t be able to import it again, anywhere!. Once you entered the import password OpenSSL requests you to type in another password, twice!. This new password will protect your .key file.
Now let’s extract the certificate:
`openssl pkcs12 -in [yourfile.pfx] -clcerts -nokeys -out [certificate.crt]`
Just press enter and your certificate appears.
Now as I mentioned in the intro of this article you sometimes need to have an unencrypted .key file to import on some devices. I probably don’t need to mention that you should be carefully. If you store your unencrypted keypair somewhere on an unsafe location anyone can have a go with it and impersonate for instance a website or a person of your company. So always be extra careful when it comes to private keys! Just throw the unencrypted keyfile away when you’re done with it, saving just the encrypted one.
The command:
`openssl rsa -in [keyfile-encrypted.key] -out [keyfile-decrypted.key]`
Notes:
- When you first extract the key, apply a new password (probably the same as you used to extract it) and then create an unencrypted key with the rsa command above
- Use an encrypted key file for NGINX otherwise it'll ask for the password every time it is restarted.
- Check the top of the extract .crt file for extra bits above the ----BEING... line and remove if necessary
- This certificated needs to be concatenated with the full chain of certificate authorities `cat domain.crt CA_bundle.crt > final.crt`
- test the cert with `openssl s_client -showcerts -connect www.domain.com:443`
@alogoc
Copy link
Author

alogoc commented Jul 25, 2018

 #!/bin/bash
   # Usage:
   # ./createcertfilesfrompfx.sh /path/to/domain.pfx
   #
   # Creates domain.pem and domain.key in the current directory
   #
   pfxpath="$1"
   
   if [ ! -f "$pfxpath" ];
   then
     echo "Cannot find PFX using path '$pfxpath'"
     exit 1
   fi
   
   crtname=`basename ${pfxpath%.*}`
   domaincacrtpath=`mktemp`
   domaincrtpath=`mktemp`
   fullcrtpath=`mktemp`
   keypath=`mktemp`
   passfilepath=`mktemp`
   read -s -p "PFX password: " pfxpass
   echo -n $pfxpass > $passfilepath
   
   echo "Creating .CRT file"
   openssl pkcs12 -in $pfxpath -out $domaincacrtpath -nodes -nokeys -cacerts -passin file:$passfilepath
   openssl pkcs12 -in $pfxpath -out $domaincrtpath -nokeys -clcerts -passin file:$passfilepath
   cat $domaincrtpath $domaincacrtpath > $fullcrtpath
   rm $domaincrtpath $domaincacrtpath
   
   echo "Creating .KEY file"
   openssl pkcs12 -in $pfxpath -nocerts -passin file:$passfilepath -passout pass:Password123 \
   | openssl rsa -out $keypath -passin pass:Password123
   
   rm $passfilepath
   
   mv $fullcrtpath ./${crtname}.pem
   mv $keypath ./${crtname}.key
   
   ls -l ${crtname}.pem ${crtname}.key

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment