Concatenated PEM File – Split Private Key and Certificate Chain

opensslssl-certificate

I have a .pem file with SSL private key and certificate chain for my web-server concatenated into a single file. The file is in following structure:

-----BEGIN PRIVATE KEY-----
...data...
-----END PRIVATE KEY-----

-----BEGIN CERTIFICATE-----
...data...
-----END CERTIFICATE-----
-----BEGIN CERTIFICATE-----
...data...
-----END CERTIFICATE-----
-----BEGIN CERTIFICATE-----
...data...
-----END CERTIFICATE-----

What commands I use to split it to have separate files private.key and fullchain.pem?

Best Answer

Assuming the input file your-file.pem contains only 1 private key and corresponding chain of certificates.

Extract private key:

openssl storeutl -keys your-file.pem > private.key

Extract fullchain certificates:

openssl storeutl -certs your-file.pem > fullchain.pem

If the certificate data comes from standard input, use /dev/stdin :

cat your-file.pem | openssl storeutl -keys /dev/stdin
cat your-file.pem | openssl storeutl -certs /dev/stdin