]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - crypto/openssh/regress/hostkey-rotate.sh
Import OpenCSD v.1.4.0.
[FreeBSD/FreeBSD.git] / crypto / openssh / regress / hostkey-rotate.sh
1 #       $OpenBSD: hostkey-rotate.sh,v 1.10 2022/01/05 08:25:05 djm Exp $
2 #       Placed in the Public Domain.
3
4 tid="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 #
12 case `grep --version 2>&1 | awk '/GNU grep/{print $4}'` in
13 2.19)                   fgrep=good ;;
14 1.*|2.?|2.?.?|2.1?)     fgrep=bad ;;    # stock GNU grep
15 2.5.1*)                 fgrep=bad ;;    # FreeBSD and NetBSD
16 *)                      fgrep=good ;;
17 esac
18 if 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 }
23 fi
24
25 rm -f $OBJ/hkr.* $OBJ/ssh_proxy.orig $OBJ/ssh_proxy.orig
26
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
32 rm $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.
36 primary="ssh-ed25519"
37 secondary="$primary"
38
39 trace "prepare hostkeys"
40 nkeys=0
41 all_algs=""
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         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
56 done
57
58 dossh() {
59         # All ssh should succeed in this test
60         ${SSH} -F $OBJ/ssh_proxy "$@" x true || fail "ssh $@ failed"
61 }
62
63 expect_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
70 check_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
79 cp $OBJ/sshd_proxy.orig $OBJ/sshd_proxy
80
81 # Connect to sshd with StrictHostkeyChecking=no
82 verbose "learn hostkey with StrictHostKeyChecking=no"
83 >$OBJ/known_hosts
84 dossh -oHostKeyAlgorithms=$primary -oStrictHostKeyChecking=no
85 # Verify no additional keys learned
86 expect_nkeys 1 "unstrict connect keys"
87 check_key_present $primary || fail "unstrict didn't learn key"
88
89 # Connect to sshd as usual
90 verbose "learn additional hostkeys"
91 dossh -oStrictHostKeyChecking=yes -oHostKeyAlgorithms=$all_algs
92 # Check that other keys learned
93 expect_nkeys $nkeys "learn hostkeys"
94 for k in $SSH_HOSTKEY_TYPES; do
95         check_key_present $k || fail "didn't learn keytype $k"
96 done
97
98 # Check each key type
99 for 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"
108 done
109
110 # Change one hostkey (non primary) and relearn
111 if [ "$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"
123 fi
124
125 # Add new hostkey (primary type) to sshd and connect
126 verbose "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
131 dossh -oStrictHostKeyChecking=yes -oHostKeyAlgorithms=${primary},$all_algs
132 expect_nkeys `expr $nkeys + 1` "learn hostkeys"
133 check_key_present ${primary} || fail "current key missing"
134 check_key_present ${primary} $OBJ/hkr.${primary}-new.pub || fail "new key missing"
135
136 # Remove old hostkey (primary type) from sshd
137 verbose "rotate primary hostkey"
138 cp $OBJ/sshd_proxy.orig $OBJ/sshd_proxy
139 mv $OBJ/hkr.${primary}.pub $OBJ/hkr.${primary}.pub.old
140 mv $OBJ/hkr.${primary}-new.pub $OBJ/hkr.${primary}.pub
141 mv $OBJ/hkr.${primary}-new $OBJ/hkr.${primary}
142 # Check old hostkey removed
143 dossh -oStrictHostKeyChecking=yes -oHostKeyAlgorithms=${primary},$all_algs
144 expect_nkeys $nkeys "learn hostkeys"
145 check_key_present ${primary} $OBJ/hkr.${primary}.pub.old && fail "old key present"
146 check_key_present ${primary} || fail "didn't learn changed key"
147
148 # Connect again, forcing rotated key
149 verbose "check rotate primary hostkey"
150 dossh -oStrictHostKeyChecking=yes -oHostKeyAlgorithms=${primary}
151 expect_nkeys 1 "learn hostkeys"
152 check_key_present ${primary} || fail "didn't learn changed key"