]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - crypto/openssh/regress/hostkey-rotate.sh
contrib/terminus: update to terminus-font-4.49.1
[FreeBSD/FreeBSD.git] / crypto / openssh / regress / hostkey-rotate.sh
1 #       $OpenBSD: hostkey-rotate.sh,v 1.9 2020/10/07 06:38:16 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         all_algs="${all_algs}$k"
48         case "$k" in
49                 ssh-rsa)        secondary="ssh-rsa" ;;
50         esac
51 done
52
53 dossh() {
54         # All ssh should succeed in this test
55         ${SSH} -F $OBJ/ssh_proxy "$@" x true || fail "ssh $@ failed"
56 }
57
58 expect_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
65 check_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
74 cp $OBJ/sshd_proxy.orig $OBJ/sshd_proxy
75
76 # Connect to sshd with StrictHostkeyChecking=no
77 verbose "learn hostkey with StrictHostKeyChecking=no"
78 >$OBJ/known_hosts
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"
83
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"
91 done
92
93 # Check each key type
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"
99 done
100
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"
114 fi
115
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 ) \
120     > $OBJ/sshd_proxy
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"
126
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"
138
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"