Using a CA with SSH

Using a CA with ssh means you can sign a key for a user, and everywhere that the user trusts the CA you can login, without having to copy your SSH key everywhere again. This allows for things like fast rollover of keys (eg: daily), or trusting the fingerprint of a machine that you're logging into, which can be very useful when you're managing large numbers of machines, or machines that get new host keys (eg by reinstalling) regularly.

You'll probably want at least openssh 5.6, although some of the functionality is available in 5.3.

Creating the CA key

Signing your host keys

The -V flag specifies the validity interval. These will work from 5 minutes ago (to allow for clock drift), for another 10 years. Tweak as you see necessary.

the -h flag specifies what hostnames that this machine is allowed to assert. You might want to add other useful hostnames there.

ssh-keygen -s /etc/ssh/ca \
     -I "$(hostname --fqdn) host key" \
     -n "$(hostname),$(hostname --fqdn),$(hostname -I|tr ' ' ',')" \
     -V -5m:+3650d \
     -h \
     /etc/ssh/ \
     /etc/ssh/ \

Using the CA for host verification

The * below specifies that this is certificate authority is allowed to attest for any hostname, if you have one, or more common domain name suffixes, you might want to use (for example) *,* instead. Note that if you do replace the * in the known_hosts files with a domain name, they annoyingly won't match if you're using a DNS search path.

Signing your user keys

You might also want to change the validity, for example if you're rotating keys daily, you might want to set the validity to 24h instead of 10 years. The -n parameter specifies the name of the principals to assert, multiple can be listed comma seperated. The -O parameter (not shown) can be used to further restrict what the key can be used for. In particular you might want to set -O source-address=$(hostname -I | tr ' ' ',') so that the key can only be used from the set of IPs that the host has.

If you're using pkcs11 tokens to hold your ssh key, you may need to run ssh-keygen -D $PKCS11_MODULE_PATH >~/.ssh/ so that you have a public key to sign. If your CA private key is being held in a pkcs11 token, you can use the -D parameter, in this case the -s parameter has to point to the public key of the CA.

ssh-keygen -s /etc/ssh/ca \
    -I "$(whoami)@$(hostname --fqdn) user key" \
    -n "$(whoami)" \
    -V -5m:+3650d \
    ~/.ssh/ \
    ~/.ssh/ \

ssh-add ~/.ssh/*

Using the CA for user verification

If you omit the AuthorizedPrincipalsFile option, and/or the principals= argument below, then the principal name must match the user they're trying to login as.

To revoke keys

Interestingly, the ssh CRL appears to not be signed, so anyone can create a new CRL, so make sure your CRLs are protected.

To use

If you specify the ssh key, then ssh will load the associated certificate. You can't add a certificate without the associated key.

For example, you can use ssh -i ~/.ssh/id_dsa $hostname (if the key is one of the default files, then you don't need to specify it.)

You can also load both the key and the certificate into an agent with the command ssh-add -i ~/.ssh/id_dsa.

Or, you can specify the command with .ssh/config file:

Host *
   IdentifyFile ~/.ssh/id_dsa



This is a generic ssh gotcha: Host clauses in ssh config match what you say on the command line. So Host * will only match if you type, but not foo. You possibly want to use: Match canonical *