xref: /freebsd/crypto/openssh/regress/hostkey-rotate.sh (revision d0b2dbfa0ecf2bbc9709efc5e20baf8e4b44bbbf)
1#	$OpenBSD: hostkey-rotate.sh,v 1.10 2022/01/05 08:25:05 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	case "$k" in
48	ssh-rsa)
49		secondary="ssh-rsa"
50		all_algs="${all_algs}rsa-sha2-256,rsa-sha2-512,$k"
51		;;
52	*)
53		all_algs="${all_algs}$k"
54		;;
55	esac
56done
57
58dossh() {
59	# All ssh should succeed in this test
60	${SSH} -F $OBJ/ssh_proxy "$@" x true || fail "ssh $@ failed"
61}
62
63expect_nkeys() {
64	_expected=$1
65	_message=$2
66	_n=`wc -l $OBJ/known_hosts | awk '{ print $1 }'` || fatal "wc failed"
67	[ "x$_n" = "x$_expected" ] || fail "$_message (got $_n wanted $_expected)"
68}
69
70check_key_present() {
71	_type=$1
72	_kfile=$2
73	test "x$_kfile" = "x" && _kfile="$OBJ/hkr.${_type}.pub"
74	_kpub=`awk "/$_type /"' { print $2 }' < $_kfile` || \
75		fatal "awk failed"
76	fgrep "$_kpub" $OBJ/known_hosts > /dev/null
77}
78
79cp $OBJ/sshd_proxy.orig $OBJ/sshd_proxy
80
81# Connect to sshd with StrictHostkeyChecking=no
82verbose "learn hostkey with StrictHostKeyChecking=no"
83>$OBJ/known_hosts
84dossh -oHostKeyAlgorithms=$primary -oStrictHostKeyChecking=no
85# Verify no additional keys learned
86expect_nkeys 1 "unstrict connect keys"
87check_key_present $primary || fail "unstrict didn't learn key"
88
89# Connect to sshd as usual
90verbose "learn additional hostkeys"
91dossh -oStrictHostKeyChecking=yes -oHostKeyAlgorithms=$all_algs
92# Check that other keys learned
93expect_nkeys $nkeys "learn hostkeys"
94for k in $SSH_HOSTKEY_TYPES; do
95	check_key_present $k || fail "didn't learn keytype $k"
96done
97
98# Check each key type
99for k in $SSH_HOSTKEY_TYPES; do
100	case "$k" in
101	ssh-rsa) alg="rsa-sha2-256,rsa-sha2-512,ssh-rsa" ;;
102	*) alg="$k" ;;
103	esac
104	verbose "learn additional hostkeys, type=$k"
105	dossh -oStrictHostKeyChecking=yes -oHostKeyAlgorithms=$alg,$all_algs
106	expect_nkeys $nkeys "learn hostkeys $k"
107	check_key_present $k || fail "didn't learn $k correctly"
108done
109
110# Change one hostkey (non primary) and relearn
111if [ "$primary" != "$secondary" ]; then
112	verbose "learn changed non-primary hostkey type=${secondary}"
113	mv $OBJ/hkr.${secondary}.pub $OBJ/hkr.${secondary}.pub.old
114	rm -f $OBJ/hkr.${secondary}
115	${SSHKEYGEN} -qt ${secondary} -f $OBJ/hkr.${secondary} -N '' || \
116	    fatal "ssh-keygen $secondary"
117	dossh -oStrictHostKeyChecking=yes -oHostKeyAlgorithms=$all_algs
118	# Check that the key was replaced
119	expect_nkeys $nkeys "learn hostkeys"
120	check_key_present ${secondary} $OBJ/hkr.${secondary}.pub.old && \
121	    fail "old key present"
122	check_key_present ${secondary} || fail "didn't learn changed key"
123fi
124
125# Add new hostkey (primary type) to sshd and connect
126verbose "learn new primary hostkey"
127${SSHKEYGEN} -qt ${primary} -f $OBJ/hkr.${primary}-new -N '' || fatal "ssh-keygen ed25519"
128( cat $OBJ/sshd_proxy.orig ; echo HostKey $OBJ/hkr.${primary}-new ) \
129    > $OBJ/sshd_proxy
130# Check new hostkey added
131dossh -oStrictHostKeyChecking=yes -oHostKeyAlgorithms=${primary},$all_algs
132expect_nkeys `expr $nkeys + 1` "learn hostkeys"
133check_key_present ${primary} || fail "current key missing"
134check_key_present ${primary} $OBJ/hkr.${primary}-new.pub || fail "new key missing"
135
136# Remove old hostkey (primary type) from sshd
137verbose "rotate primary hostkey"
138cp $OBJ/sshd_proxy.orig $OBJ/sshd_proxy
139mv $OBJ/hkr.${primary}.pub $OBJ/hkr.${primary}.pub.old
140mv $OBJ/hkr.${primary}-new.pub $OBJ/hkr.${primary}.pub
141mv $OBJ/hkr.${primary}-new $OBJ/hkr.${primary}
142# Check old hostkey removed
143dossh -oStrictHostKeyChecking=yes -oHostKeyAlgorithms=${primary},$all_algs
144expect_nkeys $nkeys "learn hostkeys"
145check_key_present ${primary} $OBJ/hkr.${primary}.pub.old && fail "old key present"
146check_key_present ${primary} || fail "didn't learn changed key"
147
148# Connect again, forcing rotated key
149verbose "check rotate primary hostkey"
150dossh -oStrictHostKeyChecking=yes -oHostKeyAlgorithms=${primary}
151expect_nkeys 1 "learn hostkeys"
152check_key_present ${primary} || fail "didn't learn changed key"
153