Maaz Zindani

Loading the index…

↑↓ move↵ openesc close4072 pages
Security and hardening

Extracting a certificate and private key from a .p12 file fails

Erroropenssl pkcs12 -in file.p12 stops without writing the certificate or key

Short answer

On OpenSSL 3 the legacy ciphers that older .p12 files use (such as RC2-40) are not loaded by default, so reading them fails. Add -legacy. Also check the password source, and ask for the certificate and the key in two separate commands, with the key output unencrypted only if you really need that.

  • Applies to OpenSSL 3.0, Ubuntu 24.04
  • Updated
  • By Maaz Zindani, Senior Software Engineer

Fix it fast

  1. 1Find out which cause you have

    Ask openssl to describe the file. The info output lists the algorithms it uses, and a failure here tells you the file itself cannot be read with the current options.

    openssl pkcs12 -in file.p12 -info -noout
  2. 2Match what you see, then jump to its fix

    1. 1The same file opens on an old OpenSSL but fails on OpenSSL 3, and the file was made by an older tool or an old Windows or Java export.The file uses legacy encryption that OpenSSL 3 does not load by defaultFix
    2. 2openssl asks for a password you do not have typed, or a script gives it the wrong text.The password is missing or taken from the wrong sourceFix
    3. 3A tool that reads cert.pem finds a key block in it, or a server refuses a key file that is encrypted.The certificate and the key are asked for in one output, or the key stays encryptedFix

What does the error look like?

  • openssl pkcs12 -in file.p12 stops without writing the certificate or key
  • A .p12 that an older tool made cannot be read by the OpenSSL 3 command line
  • The extracted key is an encrypted PEM block that another tool cannot use

Cause 1 of 3

The file uses legacy encryption that OpenSSL 3 does not load by default

1Check

Run the same read with the legacy option. If it now succeeds, the file uses algorithms from the legacy provider (the provider holds ciphers such as RC2 and DES).

openssl pkcs12 -in file.p12 -info -noout -legacy

2Fix

Read the file with -legacy and write the pieces out. Then export a new .p12 without -legacy, which uses AES-256 by default.

openssl pkcs12 -legacy -in file.p12 -out all.pem -noenc
openssl pkcs12 -in all.pem -export -out new.p12

3Verify

The new file opens with the default options.

openssl pkcs12 -in new.p12 -info -noout

Confirmed by docs.openssl.org

Did this fix it?

Cause 2 of 3

The password is missing or taken from the wrong source

1Check

-passin sets where the password for the input file comes from. Read it from a file, which takes only the first line, so the password does not show in the process list.

openssl pkcs12 -in file.p12 -passin file:p12pass.txt -info -noout

2Fix

Use the password the file was exported with. The pass: form is visible to other users through ps, and env: needs care, so prefer file: in scripts.

openssl pkcs12 -in file.p12 -passin file:p12pass.txt -nokeys -out cert.pem

3Verify

The command writes the output file and returns without a password error.

openssl x509 -in cert.pem -noout -subject -enddate

Confirmed by docs.openssl.org

Did this fix it?

Cause 3 of 3

The certificate and the key are asked for in one output, or the key stays encrypted

1Check

Without -noenc the private key is written encrypted and a passphrase is requested. -nokeys writes no private key, and -nocerts writes no certificates.

head -n 2 key.pem

2Fix

Extract the two parts into two files. Use -noenc only when the target service needs an unencrypted key, and protect the file.

openssl pkcs12 -in file.p12 -nokeys -out cert.pem
openssl pkcs12 -in file.p12 -nocerts -noenc -out key.pem
chmod 600 key.pem

3Verify

Each file holds only its own part, and the certificate prints.

openssl x509 -in cert.pem -noout -subject
grep -c "PRIVATE KEY" cert.pem

Confirmed by docs.openssl.org

Did this fix it?

How do I stop it coming back?

  • Export new .p12 files with current OpenSSL defaults, not with legacy options.
  • Keep the password in a protected file or secret store and pass it with -passin file:.
  • Delete unencrypted key files after you have loaded them where they are needed.

Sources

How was this page made?

I drafted this page with AI assistance from the official documents listed under Sources. Every cause above names the document that confirms it. I have not reproduced this fix on a machine of my own yet, so read it as documented guidance, not a test report. Found a mistake? Tell me.

Which stack is behind this page?

Related fixes

Next step

Want this handled for good?

I look after servers, apps and releases end to end. Tell me what keeps breaking.