Export X509 certificate from PFX keystore with OpenSSL

Sometimes we find ourselves in the need of converting certificates between various formats.

Recently I received a .pfx keystore (pkcs12 format) containing a X509v3 certificate I needed to import in a java jks keystore.

Exporting the certificate from the .pfx file

Exporting the certificate is pretty easy, just use the following command:
openssl pkcs12 -in pfxkeystore.pfx -out certificate.pem -nokeys -nodes

Convert to x509 format

The exported certificate is not a x509 certificate. To convert it use the following command:
openssl x509 -outform der -in certificate.pem -out certificate.cer

Import Certificate into jks using keytool

Now you can just import the certificate normally into your jks keystore using this command:
keytool -importcert -alias certificatealias -file certificate.cer -keystore jkskeystore.jks