1 # $OpenBSD: hostkey-rotate.sh,v 1.9 2020/10/07 06:38:16 djm Exp $
2 # Placed in the Public Domain.
7 # GNU (f)grep <=2.18, as shipped by FreeBSD<=12 and NetBSD<=9 will occasionally
8 # fail to find ssh host keys in the hostkey-rotate test. If we have those
9 # versions, use awk instead.
10 # See # https://bugs.freebsd.org/bugzilla/show_bug.cgi?id=258616
12 case `grep --version 2>&1 | awk '/GNU grep/{print $4}'` in
14 1.*|2.?|2.?.?|2.1?) fgrep=bad ;; # stock GNU grep
15 2.5.1*) fgrep=bad ;; # FreeBSD and NetBSD
18 if test "x$fgrep" = "xbad"; then
21 awk 'BEGIN{e=1} {if (index($0,"'$1'")>0){e=0;print}} END{exit e}' $2
25 rm -f $OBJ/hkr.* $OBJ/ssh_proxy.orig $OBJ/ssh_proxy.orig
27 grep -vi 'hostkey' $OBJ/sshd_proxy > $OBJ/sshd_proxy.orig
28 mv $OBJ/ssh_proxy $OBJ/ssh_proxy.orig
29 grep -vi 'globalknownhostsfile' $OBJ/ssh_proxy.orig > $OBJ/ssh_proxy
30 echo "UpdateHostkeys=yes" >> $OBJ/ssh_proxy
31 echo "GlobalKnownHostsFile=none" >> $OBJ/ssh_proxy
34 # The "primary" key type is ed25519 since it's supported even when built
35 # without OpenSSL. The secondary is RSA if it's supported.
39 trace "prepare hostkeys"
42 for k in $SSH_HOSTKEY_TYPES; do
43 ${SSHKEYGEN} -qt $k -f $OBJ/hkr.$k -N '' || fatal "ssh-keygen $k"
44 echo "Hostkey $OBJ/hkr.${k}" >> $OBJ/sshd_proxy.orig
45 nkeys=`expr $nkeys + 1`
46 test "x$all_algs" = "x" || all_algs="${all_algs},"
47 all_algs="${all_algs}$k"
49 ssh-rsa) secondary="ssh-rsa" ;;
54 # All ssh should succeed in this test
55 ${SSH} -F $OBJ/ssh_proxy "$@" x true || fail "ssh $@ failed"
61 _n=`wc -l $OBJ/known_hosts | awk '{ print $1 }'` || fatal "wc failed"
62 [ "x$_n" = "x$_expected" ] || fail "$_message (got $_n wanted $_expected)"
68 test "x$_kfile" = "x" && _kfile="$OBJ/hkr.${_type}.pub"
69 _kpub=`awk "/$_type /"' { print $2 }' < $_kfile` || \
71 fgrep "$_kpub" $OBJ/known_hosts > /dev/null
74 cp $OBJ/sshd_proxy.orig $OBJ/sshd_proxy
76 # Connect to sshd with StrictHostkeyChecking=no
77 verbose "learn hostkey with StrictHostKeyChecking=no"
79 dossh -oHostKeyAlgorithms=$primary -oStrictHostKeyChecking=no
80 # Verify no additional keys learned
81 expect_nkeys 1 "unstrict connect keys"
82 check_key_present $primary || fail "unstrict didn't learn key"
84 # Connect to sshd as usual
85 verbose "learn additional hostkeys"
86 dossh -oStrictHostKeyChecking=yes -oHostKeyAlgorithms=$all_algs
87 # Check that other keys learned
88 expect_nkeys $nkeys "learn hostkeys"
89 for k in $SSH_HOSTKEY_TYPES; do
90 check_key_present $k || fail "didn't learn keytype $k"
94 for k in $SSH_HOSTKEY_TYPES; do
95 verbose "learn additional hostkeys, type=$k"
96 dossh -oStrictHostKeyChecking=yes -oHostKeyAlgorithms=$k,$all_algs
97 expect_nkeys $nkeys "learn hostkeys $k"
98 check_key_present $k || fail "didn't learn $k correctly"
101 # Change one hostkey (non primary) and relearn
102 if [ "$primary" != "$secondary" ]; then
103 verbose "learn changed non-primary hostkey type=${secondary}"
104 mv $OBJ/hkr.${secondary}.pub $OBJ/hkr.${secondary}.pub.old
105 rm -f $OBJ/hkr.${secondary}
106 ${SSHKEYGEN} -qt ${secondary} -f $OBJ/hkr.${secondary} -N '' || \
107 fatal "ssh-keygen $secondary"
108 dossh -oStrictHostKeyChecking=yes -oHostKeyAlgorithms=$all_algs
109 # Check that the key was replaced
110 expect_nkeys $nkeys "learn hostkeys"
111 check_key_present ${secondary} $OBJ/hkr.${secondary}.pub.old && \
112 fail "old key present"
113 check_key_present ${secondary} || fail "didn't learn changed key"
116 # Add new hostkey (primary type) to sshd and connect
117 verbose "learn new primary hostkey"
118 ${SSHKEYGEN} -qt ${primary} -f $OBJ/hkr.${primary}-new -N '' || fatal "ssh-keygen ed25519"
119 ( cat $OBJ/sshd_proxy.orig ; echo HostKey $OBJ/hkr.${primary}-new ) \
121 # Check new hostkey added
122 dossh -oStrictHostKeyChecking=yes -oHostKeyAlgorithms=${primary},$all_algs
123 expect_nkeys `expr $nkeys + 1` "learn hostkeys"
124 check_key_present ${primary} || fail "current key missing"
125 check_key_present ${primary} $OBJ/hkr.${primary}-new.pub || fail "new key missing"
127 # Remove old hostkey (primary type) from sshd
128 verbose "rotate primary hostkey"
129 cp $OBJ/sshd_proxy.orig $OBJ/sshd_proxy
130 mv $OBJ/hkr.${primary}.pub $OBJ/hkr.${primary}.pub.old
131 mv $OBJ/hkr.${primary}-new.pub $OBJ/hkr.${primary}.pub
132 mv $OBJ/hkr.${primary}-new $OBJ/hkr.${primary}
133 # Check old hostkey removed
134 dossh -oStrictHostKeyChecking=yes -oHostKeyAlgorithms=${primary},$all_algs
135 expect_nkeys $nkeys "learn hostkeys"
136 check_key_present ${primary} $OBJ/hkr.${primary}.pub.old && fail "old key present"
137 check_key_present ${primary} || fail "didn't learn changed key"
139 # Connect again, forcing rotated key
140 verbose "check rotate primary hostkey"
141 dossh -oStrictHostKeyChecking=yes -oHostKeyAlgorithms=${primary}
142 expect_nkeys 1 "learn hostkeys"
143 check_key_present ${primary} || fail "didn't learn changed key"