Extracting a certificate and private key from a .p12 file fails
openssl pkcs12 -in file.p12 stops without writing the certificate or keyShort 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.
Fix it fast
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 -noout2Match what you see, then jump to its fix
- 1
The 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
openssl 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
A 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
- 1
What does the error look like?
openssl pkcs12 -in file.p12 stops without writing the certificate or keyA .p12 that an older tool made cannot be read by the OpenSSL 3 command lineThe 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 -legacy2Fix
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.p123Verify
The new file opens with the default options.
openssl pkcs12 -in new.p12 -info -nooutConfirmed by docs.openssl.org
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 -noout2Fix
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.pem3Verify
The command writes the output file and returns without a password error.
openssl x509 -in cert.pem -noout -subject -enddateConfirmed by docs.openssl.org
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.pem2Fix
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.pem3Verify
Each file holds only its own part, and the certificate prints.
openssl x509 -in cert.pem -noout -subject
grep -c "PRIVATE KEY" cert.pemConfirmed by docs.openssl.org
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
Security
AuthenticationException: your certificate validation callback returned false
System.Security.Authentication.AuthenticationException
This message means a validation callback was installed, yours or one a library added, and it returned false.
3 causes, each with a fixSecurity
SSH to an EC2 instance fails with Permission denied (publickey,gssapi-keyex,gssapi-with-mic)
ec2-user@ec2-203-0-113-25.compute-1.amazonaws.com: Permission denied (publickey).
On EC2 this error almost always means the instance did not accept your key for that user.
3 causes, each with a fixSecurity
gcloud init cannot finish the sign-in step
Enter authorization code
gcloud init signs in through a browser.
3 causes, each with a fixSecurity
A GitHub Actions SSH deploy step cannot log in to the server
ssh: handshake failed: ssh: unable to authenticate
The runner reached your server but the server refused the login.
4 causes, each with a fixSecurity
SSH to GitHub asks to confirm the host, or hangs on that prompt
ED25519 key fingerprint is SHA256:
SSH has never seen github.com from this machine, so it asks you to trust the server key.
2 causes, each with a fixSecurity
A GUI program over SSH says it cannot open the display
cannot open display:
ssh must forward X11 and the remote sshd must allow it.
3 causes, each with a fix