SSL/TLS certificates
What the usual certificate files contain, how to convert CRT to PEM, and how to issue, list, renew and delete Let's Encrypt certificates with Certbot.
3 min readUpdated
A TLS (formerly SSL) certificate binds a public key to a domain name and is signed by a certificate authority (CA). This note explains the file types you meet when handling certificates, converts between formats with OpenSSL, and covers issuing free certificates from Let’s Encrypt with Certbot: with an HTTP webroot, with a manual DNS challenge, and how to list, renew and delete them.
Certificate file types
.key: the private key. Only the key owner should be able to read it..csr: the certificate signing request, sent to a CA to have the key signed. It contains the public key, not the private key..crt: the certificate issued by the CA, which vouches for the public key. It does not contain the private key, and it is what you hand to other parties such as HTTPS clients..pem: a text container with Base64-encoded content between header and footer lines. It can hold any of the above..p12(PKCS#12): a binary container, usually used to bundle the private key with its certificate (and chain).
A PEM file looks like this, with the label naming what it contains (for example CERTIFICATE or PRIVATE KEY):
-----BEGIN CERTIFICATE-----...-----END CERTIFICATE-----There is no single extension for a CA-issued certificate: the same certificate may come as .crt or .pem.
Convert a CRT to PEM
To write a certificate out as a PEM file, use openssl x509:
openssl x509 -in certificate.crt -out certificate.pem -outform PEMopenssl reads the input as PEM by default. If the .crt file is binary (DER-encoded), add -inform DER.
Install Certbot
Certbot is the Let’s Encrypt client maintained by the EFF. On Debian and Ubuntu install it from the distribution:
sudo apt install certbotThe old certbot-auto wrapper script is deprecated and no longer distributed.
Issue a certificate with the webroot method
The webroot method places a challenge file in the web server’s document root, where Let’s Encrypt fetches it over HTTP. The server must therefore be reachable from the internet on port 80. This example issues a certificate for an OpenProject server:
sudo certbot certonly --webroot --webroot-path /opt/openproject/public -d openproject.mydomain.comOn success, the certificate chain (fullchain.pem) and private key (privkey.pem) are in /etc/letsencrypt/live/openproject.mydomain.com/. For OpenProject, run openproject reconfigure and answer yes when the wizard asks about SSL, then point it at these files.
Issue a certificate with a manual DNS challenge
When the server is not reachable on port 80, or you need the certificate somewhere else (for example for GitLab Pages), prove control of the domain with DNS TXT records instead:
certbot certonly --manual --preferred-challenges dns -d domain.tld -d www.domain.tld --config-dir . --logs-dir . --work-dir .The three --*-dir . options keep Certbot’s configuration, logs and working files in the current directory, so the command runs without root. Certbot then asks you to create one TXT record per name and waits until you confirm:
_acme-challenge.domain.tld_acme-challenge.www.domain.tld
Certificates issued with --manual are not renewed automatically: repeat the command before they expire.
Renew certificates
Let’s Encrypt certificates are valid for 90 days. certbot renew renews every certificate that expires within the next 30 days and does nothing otherwise. The Debian package installs a systemd timer that runs it twice a day. Without the timer, add a crontab entry (sudo crontab -e):
0 1 * * * certbot renew --quiet --post-hook "systemctl reload apache2"This runs certbot renew every day at 01:00. The post hook reloads Apache so it picks up the new certificate.
List certificates
Show the certificates Certbot manages, with their domains, expiry dates and file paths:
sudo certbot certificatesDelete a certificate
Choose a certificate to delete from an interactive list:
sudo certbot deleteOr delete one by name:
sudo certbot delete --cert-name example.comcertbot delete removes the certificate’s files from all three places where Certbot keeps them. Deleting by hand means removing the domain’s entries from each of:
/etc/letsencrypt/archive/etc/letsencrypt/live/etc/letsencrypt/renewal
References
- Certbot user guide
- Let’s Encrypt challenge types
- Let’s Encrypt integration for GitLab Pages (GitLab docs)
- How to secure Apache with Let’s Encrypt on Debian 9 (DigitalOcean)
- Importing Let’s Encrypt into Java and GlassFish (Let’s Encrypt community)
- RSA key sizes: 2048 or 4096 bits? (Daniel Pocock)
- RSA vs. DSA for SSH authentication keys (Security Stack Exchange)