Crypto IV: SSH keys that expire

· PM-mirror


The first three parts of this series were about secrets: age locks files, SOPS locks the values inside them, and Magic Wormhole moves them between computers. This one goes back to SSH, which I wrote about at length here, to fix one thing plain keys can't do: expire.

A public key in authorized_keys is valid until someone deletes that line. A lost laptop, a friend who helped with a server once, a key made for a test three years ago: all of them still open the door, on every server they were copied to, and nothing says which line belongs to whom or when it should stop working.

OpenSSH has had an answer since version 5.4, from 2010, and it needs no extra software. A small certificate authority, which is just another SSH key pair, signs a user's public key together with a name, the accounts it may use and a validity window. Servers stop trusting a list of keys and trust the CA instead. Anything the CA signed works until it expires, and nothing else gets in.

The same trick works in the other direction. The CA can sign the server's host key, so the client knows it reached the right machine without the "are you sure you want to continue connecting?" question on the first visit.

digraph {
rankdir=LR
fontname="Helvetica"
margin="0.4,0.12"
node [shape=box, style=rounded, fontname="Helvetica", fontsize=11]
edge [fontname="Helvetica", fontsize=9, arrowsize=0.6]
uca [label="user CA", color="#d93a2b", fontcolor="#d93a2b"]
hca [label="host CA", color="#d93a2b", fontcolor="#d93a2b"]
ucert [label="your key\n+ certificate\nvalid 8 hours"]
hcert [label="server host key\n+ certificate"]
srv [label="sshd\nTrustedUserCAKeys"]
cli [label="ssh client\n@cert-authority"]
uca -> ucert [label="signs"]
hca -> hcert [label="signs"]
ucert -> srv [label="accepted until\nit expires"]
hcert -> cli [label="no first-visit\nprompt"]
}

A user certificate #

Everything below is a real run, on a test machine with example names. First the CA, then a normal key, then the signature:

The signing command carries the whole policy:

The result is a new file, id_ed25519-cert.pub, next to the key. ssh picks it up on its own when it sits beside the private key with that name, and ssh-keygen -L shows what's inside.

One detail from the output: the command ran at 19:51, and the certificate is valid from 19:50. ssh-keygen starts the window a little in the past, which gives a server whose clock runs slightly behind some room before it rejects a certificate that was just made. Clocks matter here. A server with the wrong time will reject good certificates or accept expired ones.

The CA in this test has no passphrase, to keep the screenshots short. A real one gets a strong passphrase and lives somewhere that isn't a server, because whoever holds it can sign their way into everything that trusts it.

The server side #

The server needs the CA's public key and one line pointing at it. There's no authorized_keys for this user at all:

# /etc/ssh/sshd_config.d/ca.conf
TrustedUserCAKeys /etc/ssh/user_ca.pub
HostCertificate /etc/ssh/ssh_host_ed25519_key-cert.pub
PasswordAuthentication no

The HostCertificate line is for the other direction, which comes next. After a reload of sshd, any key the user CA signed for you logs in as you.

A host certificate #

The host CA is a second key pair. Keeping it separate from the user CA means a leak of one doesn't hand over the other. It signs the server's public host key with -h, and -n lists the names the server answers to:

The signed host certificate goes back to the server, where the HostCertificate line above points at it. On the client, one line in known_hosts says "trust anything under *.example.lan that this CA signed". The first ssh to the server logs in straight away, with no fingerprint to compare and no prompt. A new server signed by the same CA gets the same treatment, so known_hosts stops growing one line per machine.

When it expires #

The bottom of that screenshot is the point of all this. The same key, signed again with a window that ended in January, gets Permission denied (publickey). The client doesn't say why, which is normal for SSH. The server does:

$ grep -oE 'ID .*|Certificate invalid: .*' /var/log/sshd.log
ID you@laptop (serial 1) CA ED25519 SHA256:nQsXu1UG1ZvB0qao789Oa/4WrYMw5ziJ8oXg9CPzz4k
Certificate invalid: expired

The first line is the successful login from before, with the key ID and serial of the certificate that was used. The second is the expired one being turned away. The test server wrote its log to a file. On Debian and Ubuntu the same lines show up in journalctl -u ssh.

With short certificates, nobody has to clean anything up. A key signed for eight hours stops working eight hours later, whether anyone remembers it or not.

Revoking before it expires #

Sometimes eight hours is too long, like when a laptop goes missing at lunch. OpenSSH keeps revocations in a KRL, a key revocation list,1 and the server checks it on every login:

$ ssh-keygen -s ca/user_ca -I you@laptop -n you -z 3 -V +8h ~/.ssh/id_ed25519.pub
Signed user key /home/you/.ssh/id_ed25519-cert.pub: id "you@laptop" serial 3 for you valid from 2026-11-26T19:50:00 to 2026-11-27T03:51:05
$ ssh you@server.example.lan whoami
you
$ ssh-keygen -k -f revoked.krl -s ca/user_ca.pub ~/.ssh/id_ed25519-cert.pub
Revoking from /home/you/.ssh/id_ed25519-cert.pub
$ ssh-keygen -Q -f revoked.krl ~/.ssh/id_ed25519-cert.pub
/home/you/.ssh/id_ed25519-cert.pub (you@laptop): REVOKED

The KRL goes to the server, and one more line in the same config file tells sshd to read it:

RevokedKeys /etc/ssh/revoked.krl

After a reload, the certificate that worked a minute ago doesn't:

$ ssh you@server.example.lan whoami
you@server.example.lan: Permission denied (publickey).

$ grep -o 'revoked.*' /var/log/sshd.log
revoked by file /etc/ssh/revoked.krl

ssh-keygen -k -u adds to an existing KRL instead of starting a new one.

Certificates that can do less #

A certificate can also carry limits, set with -O when signing. A key for a backup job, for example, only needs to run one command from one network:

ssh-keygen -s ca/user_ca -I backup@nas -n backup -V +30d \
-O source-address=192.0.2.0/24 \
-O force-command=/usr/local/bin/receive-backup \
-O no-port-forwarding -O no-agent-forwarding \
backup_key.pub

source-address refuses logins from anywhere else, force-command runs that one program no matter what the client asks for, and the no- options switch off the extensions the certificate would otherwise get. -O clear drops all of them at once, and each one can be added back by name.

What changes day to day #

A short-lived certificate has to be signed again when it runs out. For one person, that's a single command in the morning. Bigger setups put the CA behind a small service that checks who's asking and signs automatically, so nobody touches the CA key by hand. Either way, the servers only ever need the CA's public key, and that file almost never changes.

Sources #

  1. A KRL is a compact binary file that can revoke certificates by serial number, by key ID or by the key itself. OpenSSH can also read a plain list of public keys in the same RevokedKeys setting.