..

SSH Certificate Authorities

Making things easier for yourself through security.

The problem

When you work from several machines, and to several machines, the issue of SSH key explosion tends to rear its head. Each source-destination combination requires an authorized_keys entry. Additionally, there’s no kind of built-in revocation mechanism for compromised keys. If not for security, the solution presented herein is at least a much more convenient way to manage keys.

SSH keys

Most people (hopefully) come across SSH keys when they work remotely. Often they serve to remove the need to continually type your password for every operation. I’d also argue that they’re more secure! With password-based authentication, an encrypted channel is established between the client and the server. However, your password is then passed directly through that channel to the server. This is why the host key verification is important: you probably want to establish you’re talking to the right entity.

In most cases, the TOFU (trust on first use) model works here. The threat model is of some kind of man-in-the-middle or network redirection attack. You make a connection that seems mundane, but ends up at an attacker-controlled destination. Here, the SSH host key verification will loudly complain and you at least get the opportunity to review things before deciding to connect for real.

On the other hand, TOFU doesn’t protect against connecting to an already-compromised host. And host keys don’t protect against a trusted host becoming compromised. In both these scenarios, password-based authentication will happily send your credentials to the bad guys.

Clearly password-based authentication isn’t ideal. So what do SSH keys gain us? Instead of a “what you know”, keys are a “what you have”: authentication is predicated on possession of the private key. Ideally there’s also the factor of “what you know” protecting the key itself. That way, sniffing the password alone doesn’t expose the private key, and taking the encrypted private key at rest still requires the password.

SSH keys provide session authentication with a little bit of asymmetric cryptography. The client sends an authentication request containing the public half of the keypair. As the name suggests, this is public data that can be exposed to any party. Along with the public key is a signataure, signed by the private half of the key. The plaintext that was signed contains session-specific data, so it can’t be captured and replayed.

The crux of the asymmetric cryptography here is that the private key holder can sign data that anybody in possession of the public key can verify. So, if the public key is acceptable, i.e. in the authorized_keys list, and validates the signature of the session-specific payload, we can be sure that the client is in possession of the private key.

SSH certificates

We’ve solved one issue, which is client authentication. By putting a client’s public key in authorized_keys, we allow it to authentication if it proves possession of the private key. However, things get slightly tricky with several client machines, such as a laptop and a desktop. Good security practice dictates that we don’t simply copy the private key across all client machines.

A simple argument against this is that losing control of the private key (misplacing the laptop, compromise on one machine) anywhere means losing it everywhere. This comes with all the associated hassle: regenerating keys, revoking the compromised key, etc.

Having to maintain a separate keypair on every client device moves us to a problem of distribution: every server you access needs to trust the public key from each of your clients. It’s easy to end up with many entries here. Adding another client to the fleet means a strange limbo period where you incrementally add its key to each server.

A particularly neat solution is to involve a bit of public key infrastructure (PKI) and introduce the concept of a SSH certificate. The usual client authentication described above still happens, but instead of trusting the client public key directly, we trust a certificate authority public key. The authority signs a certificate, which contains the client public key, as well as principals and a validity range.

Within the certificate, principals are essentially user identities associated with the key. I only authorise myself as a principal on servers, otherwise any certificate signed by the CA would be accepted! The validity range allows much more flexibility in credential rotation and gives an inherent expiry date. No more leaving the keys to the castle languishing on some ancient machine that subsequently gets pried into.

Managing certificates with a YubiKey

I am a big fan of hardware security tokens, the YubiKey being almost the canonical example. Apart from providing GPG and FIDO2, there’s a PIV (personal identity verification) application that can provide sign/decrypt operations to an on-card private key through PKCS#11. In concrete terms, the SSH certificates are powerful: any certificate signed by the trusted CA can be used for authentication (modulo principles and validity lifetime). To mitigate some of that risk, the signing capability is only provided when the YubiKey is physically in my computer.

To start, we can generate a new private key using the ED25519 algorithm, that always requires both PIN and touch confirmation. The PIV application has a concept of “slots”; here we use 82 which is one of the “retired key management” slots. It’s freely available, and that way we don’t step on one of the more specific usages. Using ykman (available in Guix in the python-yubikey-manager package):

ykman piv keys generate \
  --algorithm ED25519 \
  --pin-policy ALWAYS \
  --touch-policy ALWAYS \
  82 ssh-ca.pem

This gives us a PEM-encoded public key. We can then create a self-signed X.509 certificate on the YubiKey. While strictly not necessary for the SSH CA use case, it is a compatibility step for PIV and PKCS#11.

ykman piv certificates generate \
  --subject "CN=SSH User CA" \
  --valid-days 3650 \
  82 ssh-ca.pem

Next, we convert the PEM-encoded public key into the format that can be consumed by OpenSSH. A small Python wrapper (using python-cryptography) does the trick here:

from cryptography.hazmat.primitives import serialization

with open("ssh-ca.pem", "rb") as f:
    key = serialization.load_pem_public_key(f.read())

with open("ssh-ca.pub", "wb") as f:
    f.write(key.public_bytes(
        encoding=serialization.Encoding.OpenSSH,
        format=serialization.PublicFormat.OpenSSH,
    ) + b"\n")

The resulting public key here looks remarkably like a usual SSH public key: ssh-ed25519 AAAA.... In fact, this is what we add to authorized_keys:

cert-authority,principals="username" ssh-ed25519 AAAA... yubikey-ca

Issuing client certificates

The final piece of the puzzle is to issue and sign a client certificate to actually connect to a server. As described above, the certificate only contains the public key of the client, so we can do this centrally from the machine managing the CA. I have laptop.pub, desktop.pub, etc. Then to issue a certificate:

$ guix shell libp11 yubico-piv-tool
$ ssh-keygen \
  -s ssh-ca.pub \
  -D $GUIX_ENVIRONMENT/lib/libykcs11.so \
  -I <machine>-$(date +%Y%m%d) \
  -n username \
  -V +90d \
  <machine>.pub

This creates a <machine>-cert.pub valid for 90 days which can be installed on the client machine as id_ed25519-cert.pub and will automatically be discovered alongside the private key when connecting. The -I option gives an id to the certificate that will show up in logs and can be used for certificate revocation through the KRL.

Because the public keys are already in place on the CA machine, issuing new certificates when expiry is nigh is one-way. The whole process is surprisingly straightforward, elegant and flexible.