Certificate formats: PEM, DER and PKCS#12 and private key handling
Translation of the original in Spanish. Read it in: CatalàCastellano
Part 5 of 5Certificates, PKI and MQTT over TLS
- Certificate management (PKI) - easy-rsa
- MQTT Broker (Mosquitto) with server certificate (self-signed)
- MQTT Broker (Mosquitto) with server certificate (self-signed) and client certificate
- MQTT Broker (Mosquitto) with server certificate (Let's Encrypt)
- Certificate formats: PEM, DER and PKCS#12 and private key handling
Introduction
In this article we explain in a practical way the most common formats of digital certificates and how to handle the associated private keys. Knowing these formats and commands will let you manage your certificates correctly for use in environments that require security and automation.
Certificate formats
PEM format (Privacy Enhanced Mail)
The PEM format is human-readable, since it uses Base64 encoding in ASCII. A certificate in this format begins with the line ----BEGIN CERTIFICATE----. This is the format required to install certificates in the trust store (set of certificates of trusted entities).
DER format (Distinguished Encoding Rules)
The DER format is a binary format, more compact and not directly readable. If when inspecting your certificate you do not see the typical initial PEM line, it is probably a certificate in DER format.
Converting from DER to PEM
To install a certificate in the trust store, it must be in PEM format. If you have a certificate in DER format, for example local-ca.der, you can convert it to PEM using the following command:
sudo openssl x509 -inform der -outform pem -in local-ca.der -out local-ca.crt
PKCS#12 format
The PKCS#12 format allows packaging both the certificate and the private key and, optionally, the certificate chain (sequence of intermediate certificates that verify the validity of the certificate). It is very useful for exporting and importing complete credentials securely, since the resulting file (usually with the .p12 extension) can be protected by a password.
It is important to keep in mind that, if a certificate's private key is not inside the PKI, it cannot be exported in PKCS#12 format.
In the easy-rsa environment, you can export a certificate and its private key in PKCS#12 format with the following command:
# sintaxis:
./easyrsa export-p12 <nombre-del-certificado|FQDN>
# ejemplo:
./easyrsa export-p12 mqtt.ymbihq.local
Handling private keys
Private keys are usually protected by symmetric encryption, which means that a passphrase (password) is needed to decrypt them and use them. This protection is essential to prevent unauthorised use of the key in case of loss or theft. Sometimes it is necessary to remove or change the passphrase to make it easier to use in certain environments or automations.
Remove the passphrase
To remove the passphrase from a private key, you can use the following command:
openssl rsa -in [original.key] -out [new.key]
If you prefer to do it non-interactively, specifying the password directly in the command:
openssl rsa -in original.key -out new.key -passin pass:your_original_passphrase
Change the passphrase
If you want to change the passphrase and protect the key with a new encryption (for example, AES-256), you can do it as follows:
openssl rsa -aes256 -in original.key -out new.key
Closing
With the information explained in this article, you will be able to manage and convert certificates between PEM and DER formats, package your credentials in PKCS#12 format and handle private keys according to your needs. This knowledge will let you implement secure and automated solutions in your development and production environments.