1 # $OpenBSD: hostkey-rotate.sh,v 1.5 2015/09/04 04:23:10 djm Exp $
2 # Placed in the Public Domain.
6 # Need full names here since they are used in HostKeyAlgorithms
7 HOSTKEY_TYPES="ecdsa-sha2-nistp256 ssh-ed25519 ssh-rsa ssh-dss"
9 rm -f $OBJ/hkr.* $OBJ/ssh_proxy.orig
11 grep -vi 'hostkey' $OBJ/sshd_proxy > $OBJ/sshd_proxy.orig
12 echo "UpdateHostkeys=yes" >> $OBJ/ssh_proxy
15 trace "prepare hostkeys"
18 for k in `${SSH} -Q key-plain` ; do
19 ${SSHKEYGEN} -qt $k -f $OBJ/hkr.$k -N '' || fatal "ssh-keygen $k"
20 echo "Hostkey $OBJ/hkr.${k}" >> $OBJ/sshd_proxy.orig
21 nkeys=`expr $nkeys + 1`
22 test "x$all_algs" = "x" || all_algs="${all_algs},"
23 all_algs="${all_algs}$k"
27 # All ssh should succeed in this test
28 ${SSH} -F $OBJ/ssh_proxy "$@" x true || fail "ssh $@ failed"
34 _n=`wc -l $OBJ/known_hosts | awk '{ print $1 }'` || fatal "wc failed"
35 [ "x$_n" = "x$_expected" ] || fail "$_message (got $_n wanted $_expected)"
41 test "x$_kfile" = "x" && _kfile="$OBJ/hkr.${_type}.pub"
42 _kpub=`awk "/$_type /"' { print $2 }' < $_kfile` || \
44 fgrep "$_kpub" $OBJ/known_hosts > /dev/null
47 cp $OBJ/sshd_proxy.orig $OBJ/sshd_proxy
49 # Connect to sshd with StrictHostkeyChecking=no
50 verbose "learn hostkey with StrictHostKeyChecking=no"
52 dossh -oHostKeyAlgorithms=ssh-ed25519 -oStrictHostKeyChecking=no
53 # Verify no additional keys learned
54 expect_nkeys 1 "unstrict connect keys"
55 check_key_present ssh-ed25519 || fail "unstrict didn't learn key"
57 # Connect to sshd as usual
58 verbose "learn additional hostkeys"
59 dossh -oStrictHostKeyChecking=yes -oHostKeyAlgorithms=$all_algs
60 # Check that other keys learned
61 expect_nkeys $nkeys "learn hostkeys"
62 check_key_present ssh-rsa || fail "didn't learn keys"
65 for k in `${SSH} -Q key-plain` ; do
66 verbose "learn additional hostkeys, type=$k"
67 dossh -oStrictHostKeyChecking=yes -oHostKeyAlgorithms=$k,$all_algs
68 expect_nkeys $nkeys "learn hostkeys $k"
69 check_key_present $k || fail "didn't learn $k"
72 # Change one hostkey (non primary) and relearn
73 verbose "learn changed non-primary hostkey"
74 mv $OBJ/hkr.ssh-rsa.pub $OBJ/hkr.ssh-rsa.pub.old
75 rm -f $OBJ/hkr.ssh-rsa
76 ${SSHKEYGEN} -qt ssh-rsa -f $OBJ/hkr.ssh-rsa -N '' || fatal "ssh-keygen $k"
77 dossh -oStrictHostKeyChecking=yes -oHostKeyAlgorithms=$all_algs
78 # Check that the key was replaced
79 expect_nkeys $nkeys "learn hostkeys"
80 check_key_present ssh-rsa $OBJ/hkr.ssh-rsa.pub.old && fail "old key present"
81 check_key_present ssh-rsa || fail "didn't learn changed key"
83 # Add new hostkey (primary type) to sshd and connect
84 verbose "learn new primary hostkey"
85 ${SSHKEYGEN} -qt ssh-rsa -f $OBJ/hkr.ssh-rsa-new -N '' || fatal "ssh-keygen $k"
86 ( cat $OBJ/sshd_proxy.orig ; echo HostKey $OBJ/hkr.ssh-rsa-new ) \
88 # Check new hostkey added
89 dossh -oStrictHostKeyChecking=yes -oHostKeyAlgorithms=ssh-rsa,$all_algs
90 expect_nkeys `expr $nkeys + 1` "learn hostkeys"
91 check_key_present ssh-rsa || fail "current key missing"
92 check_key_present ssh-rsa $OBJ/hkr.ssh-rsa-new.pub || fail "new key missing"
94 # Remove old hostkey (primary type) from sshd
95 verbose "rotate primary hostkey"
96 cp $OBJ/sshd_proxy.orig $OBJ/sshd_proxy
97 mv $OBJ/hkr.ssh-rsa.pub $OBJ/hkr.ssh-rsa.pub.old
98 mv $OBJ/hkr.ssh-rsa-new.pub $OBJ/hkr.ssh-rsa.pub
99 mv $OBJ/hkr.ssh-rsa-new $OBJ/hkr.ssh-rsa
100 # Check old hostkey removed
101 dossh -oStrictHostKeyChecking=yes -oHostKeyAlgorithms=ssh-rsa,$all_algs
102 expect_nkeys $nkeys "learn hostkeys"
103 check_key_present ssh-rsa $OBJ/hkr.ssh-rsa.pub.old && fail "old key present"
104 check_key_present ssh-rsa || fail "didn't learn changed key"
106 # Connect again, forcing rotated key
107 verbose "check rotate primary hostkey"
108 dossh -oStrictHostKeyChecking=yes -oHostKeyAlgorithms=ssh-rsa
109 expect_nkeys 1 "learn hostkeys"
110 check_key_present ssh-rsa || fail "didn't learn changed key"