1#	$OpenBSD: hostkey-rotate.sh,v 1.9 2020/10/07 06:38:16 djm Exp $
2#	Placed in the Public Domain.
3
4tid="hostkey rotate"
5
6#
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
11#
12case `grep --version 2>&1 | awk '/GNU grep/{print $4}'` in
132.19)			fgrep=good ;;
141.*|2.?|2.?.?|2.1?)	fgrep=bad ;;	# stock GNU grep
152.5.1*)			fgrep=bad ;;	# FreeBSD and NetBSD
16*)			fgrep=good ;;
17esac
18if test "x$fgrep" = "xbad"; then
19	fgrep()
20{
21	awk 'BEGIN{e=1} {if (index($0,"'$1'")>0){e=0;print}} END{exit e}' $2
22}
23fi
24
25rm -f $OBJ/hkr.* $OBJ/ssh_proxy.orig $OBJ/ssh_proxy.orig
26
27grep -vi 'hostkey' $OBJ/sshd_proxy > $OBJ/sshd_proxy.orig
28mv $OBJ/ssh_proxy $OBJ/ssh_proxy.orig
29grep -vi 'globalknownhostsfile' $OBJ/ssh_proxy.orig > $OBJ/ssh_proxy
30echo "UpdateHostkeys=yes" >> $OBJ/ssh_proxy
31echo "GlobalKnownHostsFile=none" >> $OBJ/ssh_proxy
32rm $OBJ/known_hosts
33
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.
36primary="ssh-ed25519"
37secondary="$primary"
38
39trace "prepare hostkeys"
40nkeys=0
41all_algs=""
42for 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"
48	case "$k" in
49		ssh-rsa)	secondary="ssh-rsa" ;;
50	esac
51done
52
53dossh() {
54	# All ssh should succeed in this test
55	${SSH} -F $OBJ/ssh_proxy "$@" x true || fail "ssh $@ failed"
56}
57
58expect_nkeys() {
59	_expected=$1
60	_message=$2
61	_n=`wc -l $OBJ/known_hosts | awk '{ print $1 }'` || fatal "wc failed"
62	[ "x$_n" = "x$_expected" ] || fail "$_message (got $_n wanted $_expected)"
63}
64
65check_key_present() {
66	_type=$1
67	_kfile=$2
68	test "x$_kfile" = "x" && _kfile="$OBJ/hkr.${_type}.pub"
69	_kpub=`awk "/$_type /"' { print $2 }' < $_kfile` || \
70		fatal "awk failed"
71	fgrep "$_kpub" $OBJ/known_hosts > /dev/null
72}
73
74cp $OBJ/sshd_proxy.orig $OBJ/sshd_proxy
75
76# Connect to sshd with StrictHostkeyChecking=no
77verbose "learn hostkey with StrictHostKeyChecking=no"
78>$OBJ/known_hosts
79dossh -oHostKeyAlgorithms=$primary -oStrictHostKeyChecking=no
80# Verify no additional keys learned
81expect_nkeys 1 "unstrict connect keys"
82check_key_present $primary || fail "unstrict didn't learn key"
83
84# Connect to sshd as usual
85verbose "learn additional hostkeys"
86dossh -oStrictHostKeyChecking=yes -oHostKeyAlgorithms=$all_algs
87# Check that other keys learned
88expect_nkeys $nkeys "learn hostkeys"
89for k in $SSH_HOSTKEY_TYPES; do
90	check_key_present $k || fail "didn't learn keytype $k"
91done
92
93# Check each key type
94for 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"
99done
100
101# Change one hostkey (non primary) and relearn
102if [ "$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"
114fi
115
116# Add new hostkey (primary type) to sshd and connect
117verbose "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 ) \
120    > $OBJ/sshd_proxy
121# Check new hostkey added
122dossh -oStrictHostKeyChecking=yes -oHostKeyAlgorithms=${primary},$all_algs
123expect_nkeys `expr $nkeys + 1` "learn hostkeys"
124check_key_present ${primary} || fail "current key missing"
125check_key_present ${primary} $OBJ/hkr.${primary}-new.pub || fail "new key missing"
126
127# Remove old hostkey (primary type) from sshd
128verbose "rotate primary hostkey"
129cp $OBJ/sshd_proxy.orig $OBJ/sshd_proxy
130mv $OBJ/hkr.${primary}.pub $OBJ/hkr.${primary}.pub.old
131mv $OBJ/hkr.${primary}-new.pub $OBJ/hkr.${primary}.pub
132mv $OBJ/hkr.${primary}-new $OBJ/hkr.${primary}
133# Check old hostkey removed
134dossh -oStrictHostKeyChecking=yes -oHostKeyAlgorithms=${primary},$all_algs
135expect_nkeys $nkeys "learn hostkeys"
136check_key_present ${primary} $OBJ/hkr.${primary}.pub.old && fail "old key present"
137check_key_present ${primary} || fail "didn't learn changed key"
138
139# Connect again, forcing rotated key
140verbose "check rotate primary hostkey"
141dossh -oStrictHostKeyChecking=yes -oHostKeyAlgorithms=${primary}
142expect_nkeys 1 "learn hostkeys"
143check_key_present ${primary} || fail "didn't learn changed key"
144