Convert PFX to PEM and extract the private key
Web servers such as nginx and Apache want the certificate and the private key as separate PEM files, but a certificate exported from Windows arrives as one .pfx. This guide takes a PFX apart with CertConvert, without sending the key anywhere.
What you need
- The
.pfxor.p12file. - Its password.
A PFX holds two things, so there are two short steps: the certificates, then the key.
Step 1: extract the certificates
- Open the Convert view and drag in the PFX, or choose Add Files….
- Type the PFX password in Input Password (If Needed).
- Set Target format to PEM Certificate (.pem).
- Choose Convert And Save….
The PEM file contains every certificate in the PFX and no private key.
Step 2: extract the private key
- Open the Keys view and choose Open Key…, then pick the same PFX.
- Type the PFX password in Key Password (If Needed) and choose Unlock.
- Under Convert format, choose PKCS #8 PEM (Unencrypted), or PKCS #8 PEM (Encrypted, AES-256) and set a New Password.
- Choose Save Converted Key….
From the command line
# certificates only
certconvert convert device.pfx --password secret -o device.pem
# the private key, unencrypted PKCS #8
certconvert key convert device.pfx --password secret -o device.key
# the private key, encrypted with a new passphrase
certconvert key convert device.pfx --password secret -o device-encrypted.key \
--to pkcs8-enc --out-password newsecret
# confirm the certificate and the unencrypted key are a pair
certconvert key match --cert device.pem --key device.key
For a server that wants a traditional RSA key, use --to pkcs1. For an EC key in SEC 1 form, use --to sec1.
The OpenSSL equivalent
For comparison, the same job in OpenSSL takes two commands:
openssl pkcs12 -in device.pfx -nokeys -out device.pem
openssl pkcs12 -in device.pfx -nocerts -nodes -out device.key
OpenSSL 3 spells -nodes as -noenc; the older flag still works there.
OpenSSL is named here for comparison only. See trademarks.
Common snags
“…is password-protected”
The PFX needs its password. Type it in the password field in the app, or pass --password on the command line.
The PEM holds more than one certificate
The PFX carried a chain. That is normal. To check or correct the order, see fix certificate chain order.
Which key format does my server need?
PKCS #8, which starts -----BEGIN PRIVATE KEY-----, works almost everywhere. Older software may want PKCS #1, which starts -----BEGIN RSA PRIVATE KEY-----. The Keys view converts between them.