1# $OpenBSD: hostkey-rotate.sh,v 1.8 2019/11/26 23:43:10 djm Exp $ 2# Placed in the Public Domain. 3 4tid="hostkey rotate" 5 6rm -f $OBJ/hkr.* $OBJ/ssh_proxy.orig 7 8grep -vi 'hostkey' $OBJ/sshd_proxy > $OBJ/sshd_proxy.orig 9echo "UpdateHostkeys=yes" >> $OBJ/ssh_proxy 10rm $OBJ/known_hosts 11 12# The "primary" key type is ed25519 since it's supported even when built 13# without OpenSSL. The secondary is RSA if it's supported. 14primary="ssh-ed25519" 15secondary="$primary" 16 17trace "prepare hostkeys" 18nkeys=0 19all_algs="" 20for k in $SSH_HOSTKEY_TYPES; do 21 ${SSHKEYGEN} -qt $k -f $OBJ/hkr.$k -N '' || fatal "ssh-keygen $k" 22 echo "Hostkey $OBJ/hkr.${k}" >> $OBJ/sshd_proxy.orig 23 nkeys=`expr $nkeys + 1` 24 test "x$all_algs" = "x" || all_algs="${all_algs}," 25 all_algs="${all_algs}$k" 26 case "$k" in 27 ssh-rsa) secondary="ssh-rsa" ;; 28 esac 29done 30 31dossh() { 32 # All ssh should succeed in this test 33 ${SSH} -F $OBJ/ssh_proxy "$@" x true || fail "ssh $@ failed" 34} 35 36expect_nkeys() { 37 _expected=$1 38 _message=$2 39 _n=`wc -l $OBJ/known_hosts | awk '{ print $1 }'` || fatal "wc failed" 40 [ "x$_n" = "x$_expected" ] || fail "$_message (got $_n wanted $_expected)" 41} 42 43check_key_present() { 44 _type=$1 45 _kfile=$2 46 test "x$_kfile" = "x" && _kfile="$OBJ/hkr.${_type}.pub" 47 _kpub=`awk "/$_type /"' { print $2 }' < $_kfile` || \ 48 fatal "awk failed" 49 fgrep "$_kpub" $OBJ/known_hosts > /dev/null 50} 51 52cp $OBJ/sshd_proxy.orig $OBJ/sshd_proxy 53 54# Connect to sshd with StrictHostkeyChecking=no 55verbose "learn hostkey with StrictHostKeyChecking=no" 56>$OBJ/known_hosts 57dossh -oHostKeyAlgorithms=$primary -oStrictHostKeyChecking=no 58# Verify no additional keys learned 59expect_nkeys 1 "unstrict connect keys" 60check_key_present $primary || fail "unstrict didn't learn key" 61 62# Connect to sshd as usual 63verbose "learn additional hostkeys" 64dossh -oStrictHostKeyChecking=yes -oHostKeyAlgorithms=$all_algs 65# Check that other keys learned 66expect_nkeys $nkeys "learn hostkeys" 67for k in $SSH_HOSTKEY_TYPES; do 68 check_key_present $k || fail "didn't learn keytype $k" 69done 70 71# Check each key type 72for k in $SSH_HOSTKEY_TYPES; do 73 verbose "learn additional hostkeys, type=$k" 74 dossh -oStrictHostKeyChecking=yes -oHostKeyAlgorithms=$k,$all_algs 75 expect_nkeys $nkeys "learn hostkeys $k" 76 check_key_present $k || fail "didn't learn $k correctly" 77done 78 79# Change one hostkey (non primary) and relearn 80if [ "$primary" != "$secondary" ]; then 81 verbose "learn changed non-primary hostkey type=${secondary}" 82 mv $OBJ/hkr.${secondary}.pub $OBJ/hkr.${secondary}.pub.old 83 rm -f $OBJ/hkr.${secondary} 84 ${SSHKEYGEN} -qt ${secondary} -f $OBJ/hkr.${secondary} -N '' || \ 85 fatal "ssh-keygen $secondary" 86 dossh -oStrictHostKeyChecking=yes -oHostKeyAlgorithms=$all_algs 87 # Check that the key was replaced 88 expect_nkeys $nkeys "learn hostkeys" 89 check_key_present ${secondary} $OBJ/hkr.${secondary}.pub.old && \ 90 fail "old key present" 91 check_key_present ${secondary} || fail "didn't learn changed key" 92fi 93 94# Add new hostkey (primary type) to sshd and connect 95verbose "learn new primary hostkey" 96${SSHKEYGEN} -qt ${primary} -f $OBJ/hkr.${primary}-new -N '' || fatal "ssh-keygen ed25519" 97( cat $OBJ/sshd_proxy.orig ; echo HostKey $OBJ/hkr.${primary}-new ) \ 98 > $OBJ/sshd_proxy 99# Check new hostkey added 100dossh -oStrictHostKeyChecking=yes -oHostKeyAlgorithms=${primary},$all_algs 101expect_nkeys `expr $nkeys + 1` "learn hostkeys" 102check_key_present ${primary} || fail "current key missing" 103check_key_present ${primary} $OBJ/hkr.${primary}-new.pub || fail "new key missing" 104 105# Remove old hostkey (primary type) from sshd 106verbose "rotate primary hostkey" 107cp $OBJ/sshd_proxy.orig $OBJ/sshd_proxy 108mv $OBJ/hkr.${primary}.pub $OBJ/hkr.${primary}.pub.old 109mv $OBJ/hkr.${primary}-new.pub $OBJ/hkr.${primary}.pub 110mv $OBJ/hkr.${primary}-new $OBJ/hkr.${primary} 111# Check old hostkey removed 112dossh -oStrictHostKeyChecking=yes -oHostKeyAlgorithms=${primary},$all_algs 113expect_nkeys $nkeys "learn hostkeys" 114check_key_present ${primary} $OBJ/hkr.${primary}.pub.old && fail "old key present" 115check_key_present ${primary} || fail "didn't learn changed key" 116 117# Connect again, forcing rotated key 118verbose "check rotate primary hostkey" 119dossh -oStrictHostKeyChecking=yes -oHostKeyAlgorithms=${primary} 120expect_nkeys 1 "learn hostkeys" 121check_key_present ${primary} || fail "didn't learn changed key" 122