PFX, P12, PKCS#12 — the same thing

.pfx and .p12 are two extensions for the same format. Unlike PEM it is binary, usually holds several objects at once and is always password-protected — even when the password is empty.

Building a PFX from PEM files

You typically have three files: the certificate, the private key and the chain of intermediates (with Let's Encrypt cert.pem, privkey.pem, chain.pem):

openssl pkcs12 -export -inkey privkey.pem -in cert.pem -certfile chain.pem -name "mail.yourdomain.com" -out mail.pfx
  • -certfile includes the intermediates. Without it the server later lacks the chain — the most common reason for certificate verification failed after an import.
  • -name becomes the “friendly name” on Windows and makes it easier to pick in the certificate store later.
  • openssl prompts for the password. For scripts use -passout file:path — -passout pass:… by contrast shows up in the process list and shell history.

If you have fullchain.pem (certificate and chain in one file), -in fullchain.pem without -certfile is enough.

When Windows rejects the PFX

OpenSSL 3 encrypts PFX files with AES-256 and SHA-256. Windows Server 2016, older Windows 10 builds and some Java and appliance versions do not understand this and report, misleadingly, “The password you entered is incorrect”. The fix is the old scheme:

openssl pkcs12 -export -inkey privkey.pem -in cert.pem -certfile chain.pem -out mail-compatible.pfx -keypbe PBE-SHA1-3DES -certpbe PBE-SHA1-3DES -macalg sha1

Use it only for that case — for current systems the default is the better choice.

Taking a PFX apart

First look at what is inside (without writing anything):

openssl pkcs12 -in mail.pfx -info -noout

Then extract the parts one by one:

# private key only, unencrypted openssl pkcs12 -in mail.pfx -nocerts -noenc -out privkey.pem # server certificate only openssl pkcs12 -in mail.pfx -clcerts -nokeys -out cert.pem # intermediates only openssl pkcs12 -in mail.pfx -cacerts -nokeys -out chain.pem

-noenc is the name from OpenSSL 3 on; older versions only know -nodes, which still works in OpenSSL 3. Without the option openssl asks for a new password and writes the key encrypted — which a mail server cannot unlock at start-up, see Remove a private key passphrase.

Everything in one file, for HAProxy for instance: openssl pkcs12 -in mail.pfx -noenc -out complete.pem.

Bag attributes trip up some programs

When extracting, openssl writes lines such as Bag Attributes and friendlyName in front of each object. Most servers ignore them; a few parsers choke. Passing the files through once cleans them up:

openssl x509 -in cert.pem -out cert-clean.pem openssl pkey -in privkey.pem -out privkey-clean.pem

Opening older PFX files with OpenSSL 3

PFX files from a Windows export or an old appliance are often encrypted with RC2-40. OpenSSL 3 moved that to the legacy provider and reports:

Error outputting keys and certificates ...:unsupported:...:Global default library context, Algorithm (RC2-40-CBC : 0), Properties ()

The file is not broken. Append -legacy to the command and it works: openssl pkcs12 -in old.pfx -legacy -noenc -out old.pem. A wrong password, by contrast, reads Mac verify error: invalid password?.

Afterwards

Whether key and certificate still belong together after the conversion and the chain is complete is covered in Check certificate and key.

Verify your work

A test mail through your mail server to hello@analyzemy.email shows how it looks from outside: TLS path, SPF, DKIM, DMARC and blacklists in one report.

Analyze your email now

Last updated: · All guides