Skip to content

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:

Terminal window
openssl x509 -in certificate.crt -out certificate.pem -outform PEM

openssl 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:

Terminal window
sudo apt install certbot

The 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:

Terminal window
sudo certbot certonly --webroot --webroot-path /opt/openproject/public -d openproject.mydomain.com

On 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:

Terminal window
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:

Terminal window
sudo certbot certificates

Delete a certificate

Choose a certificate to delete from an interactive list:

Terminal window
sudo certbot delete

Or delete one by name:

Terminal window
sudo certbot delete --cert-name example.com

certbot 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

Related notes

2 min read

Trivy

Install Trivy and scan project files, Git repositories, container images and Kubernetes workloads for vulnerabilities and misconfigurations.

  • Security
  • DevOps & Containers
6 min read

GnuPG (GPG)

Create and manage GPG keys, sign Git commits with GPG or SSH, sign Maven artifacts in CI and move keys to another account.

  • Security
3 min read

JSON Web Tokens (JWT)

How access and refresh tokens work in JWT authentication, the endpoints a server needs, and where a client should keep each token.

  • Security
  • APIs & Integration

Type to search the whole site.