Skip to content

Renew certificates

A machine that has a certificate from the CA gets the next one by itself: the current certificate and its key are the proof, so nobody has to hand out a new token.

bxp renew --cert web.example.com.crt --key web.example.com.key \
    --exec "systemctl reload nginx"

How it behaves

  • It does nothing while more than a third of the lifetime is left, and says when it will renew. So run it every hour: it renews at the right moment by itself. Certificates last 24 hours by default and are renewed with 8 hours left, so a daily run is too seldom. --force renews now.

    Not yet:      valid until 2026-11-17 09:15, renews from 2026-11-17 01:15 on
    
  • The new certificate has the same names and the same lifetime as the old one. Renewal never adds names; that takes a token.

  • TLS: a new key is made (of the same kind and size), and both files are replaced. SSH host: the certificate file is replaced; the host key stays.
  • --exec runs after a renewal, so the program using the certificate picks up the new one.
  • An expired or revoked certificate cannot be renewed: such a machine gets a new certificate with a token.

What it renews

Certificate Renew with
TLS, from BastionXP CA's API (bxp tls, a token, a login) bxp renew
SSH host, from the API bxp renew
SSH user run bxp ssh again (they last hours)
From ACME the ACME client: bxp acme renew, certbot renew, ...
From SCEP SCEP renewal: bxp scep renew, or the device

A certificate is renewed the way it was obtained. bxp renew refuses ACME and SCEP certificates and says which to use:

error: the CA server refused the request: this certificate came from SCEP: renew it over SCEP (bxp scep renew, or the device's SCEP renewal)

bxp list certificates on the CA shows how each certificate was obtained, in the VIA column.

The program keeps running

There is no need to stop it. The files are replaced in one step each: a new file is written next to the old one and renamed over it. A program that already loaded the old certificate keeps using it from memory, and one that holds the old file open keeps reading the old content.

To start using the new certificate, the program needs a reload, not a restart. That is what --exec is for:

Program --exec Effect
nginx systemctl reload nginx New connections get the new certificate; open ones finish undisturbed.
Apache apachectl graceful The same.
HAProxy systemctl reload haproxy The same.
OpenSSH systemctl reload sshd New logins see the new host certificate; open sessions stay.
PostgreSQL psql -c "SELECT pg_reload_conf()" New connections use the new certificate.

Some programs read the files again by themselves and need no --exec; some need a full restart. Check the program's documentation for "reload certificates".

--exec is optional. Without it, the program keeps the old certificate until it is reloaded some other way. That is fine as long as the reload comes before the old certificate expires. A 24-hour certificate is renewed 8 hours before it ends, which leaves no room for that: use --exec. A 30-day certificate is renewed 10 days before it ends, so a weekly reload is enough. To keep changes inside a maintenance window, give those certificates a longer-lived profile and either run the timer only then (for example OnCalendar=Sat 03:00, with --exec), or renew without --exec and reload during the window.

File ownership

The new files keep the owner, group and permissions of the old ones, so a program that reads its key as its own user, or through its group, can still read it after the renewal. bxp renew checks before asking the CA that it may do this: it must run as root or as the owner of the files.

Run it every hour

/etc/systemd/system/bastionxp-ca-renew.service
[Unit]
Description=Renew certificates from BastionXP CA

[Service]
Type=oneshot
ExecStart=/usr/local/bin/bxp renew --cert /etc/nginx/web.crt --key /etc/nginx/web.key --exec "systemctl reload nginx"
/etc/systemd/system/bastionxp-ca-renew.timer
[Unit]
Description=Renew certificates from BastionXP CA hourly

[Timer]
OnCalendar=hourly
RandomizedDelaySec=15m
Persistent=true

[Install]
WantedBy=timers.target
sudo systemctl enable --now bastionxp-ca-renew.timer

Persistent=true catches up after the machine was off; RandomizedDelaySec keeps many machines from asking the CA at the same second. Both files are in packaging/systemd/.

17 * * * * /usr/local/bin/bxp renew --cert /etc/nginx/web.crt --key /etc/nginx/web.key --exec "systemctl reload nginx"

One service with several ExecStart= lines (allowed for Type=oneshot):

[Service]
Type=oneshot
ExecStart=/usr/local/bin/bxp renew --cert /etc/nginx/web.crt --key /etc/nginx/web.key --exec "systemctl reload nginx"
ExecStart=/usr/local/bin/bxp renew --cert /etc/ssh/ssh_host_ed25519_key-cert.pub --key /etc/ssh/ssh_host_ed25519_key --exec "systemctl reload sshd"

The command reads the CA's address and root from what bxp trust saved for the user it runs as (root, in the examples above), or from --ca-url and --root.

Windows

On Windows, a program that keeps the file open without allowing it to be replaced makes the renewal fail; it is tried again at the next run. Use Task Scheduler to run it every hour.

Behind the command

bxp renew sends a CSR for the new key to POST /v1/x509/renew (or {} to /v1/ssh/renew), with a renewal proof instead of a token: a short-lived JWT signed with the current certificate's key, carrying the certificate. See HTTP API: renewal.