1296853Sdes# $OpenBSD: hostkey-rotate.sh,v 1.5 2015/09/04 04:23:10 djm Exp $ 2285031Sdes# Placed in the Public Domain. 3285031Sdes 4285031Sdestid="hostkey rotate" 5285031Sdes 6285031Sdes# Need full names here since they are used in HostKeyAlgorithms 7285031SdesHOSTKEY_TYPES="ecdsa-sha2-nistp256 ssh-ed25519 ssh-rsa ssh-dss" 8285031Sdes 9285031Sdesrm -f $OBJ/hkr.* $OBJ/ssh_proxy.orig 10285031Sdes 11285031Sdesgrep -vi 'hostkey' $OBJ/sshd_proxy > $OBJ/sshd_proxy.orig 12285031Sdesecho "UpdateHostkeys=yes" >> $OBJ/ssh_proxy 13285031Sdesrm $OBJ/known_hosts 14285031Sdes 15285031Sdestrace "prepare hostkeys" 16285031Sdesnkeys=0 17285031Sdesall_algs="" 18295367Sdesfor k in `${SSH} -Q key-plain` ; do 19285031Sdes ${SSHKEYGEN} -qt $k -f $OBJ/hkr.$k -N '' || fatal "ssh-keygen $k" 20285031Sdes echo "Hostkey $OBJ/hkr.${k}" >> $OBJ/sshd_proxy.orig 21285031Sdes nkeys=`expr $nkeys + 1` 22285031Sdes test "x$all_algs" = "x" || all_algs="${all_algs}," 23285031Sdes all_algs="${all_algs}$k" 24285031Sdesdone 25285031Sdes 26285031Sdesdossh() { 27285031Sdes # All ssh should succeed in this test 28285031Sdes ${SSH} -F $OBJ/ssh_proxy "$@" x true || fail "ssh $@ failed" 29285031Sdes} 30285031Sdes 31285031Sdesexpect_nkeys() { 32285031Sdes _expected=$1 33285031Sdes _message=$2 34285031Sdes _n=`wc -l $OBJ/known_hosts | awk '{ print $1 }'` || fatal "wc failed" 35285031Sdes [ "x$_n" = "x$_expected" ] || fail "$_message (got $_n wanted $_expected)" 36285031Sdes} 37285031Sdes 38285031Sdescheck_key_present() { 39285031Sdes _type=$1 40285031Sdes _kfile=$2 41285031Sdes test "x$_kfile" = "x" && _kfile="$OBJ/hkr.${_type}.pub" 42285031Sdes _kpub=`awk "/$_type /"' { print $2 }' < $_kfile` || \ 43285031Sdes fatal "awk failed" 44285031Sdes fgrep "$_kpub" $OBJ/known_hosts > /dev/null 45285031Sdes} 46285031Sdes 47285031Sdescp $OBJ/sshd_proxy.orig $OBJ/sshd_proxy 48285031Sdes 49285031Sdes# Connect to sshd with StrictHostkeyChecking=no 50285031Sdesverbose "learn hostkey with StrictHostKeyChecking=no" 51285031Sdes>$OBJ/known_hosts 52285031Sdesdossh -oHostKeyAlgorithms=ssh-ed25519 -oStrictHostKeyChecking=no 53285031Sdes# Verify no additional keys learned 54285031Sdesexpect_nkeys 1 "unstrict connect keys" 55285031Sdescheck_key_present ssh-ed25519 || fail "unstrict didn't learn key" 56285031Sdes 57285031Sdes# Connect to sshd as usual 58285031Sdesverbose "learn additional hostkeys" 59295367Sdesdossh -oStrictHostKeyChecking=yes -oHostKeyAlgorithms=$all_algs 60285031Sdes# Check that other keys learned 61285031Sdesexpect_nkeys $nkeys "learn hostkeys" 62285031Sdescheck_key_present ssh-rsa || fail "didn't learn keys" 63285031Sdes 64285031Sdes# Check each key type 65295367Sdesfor k in `${SSH} -Q key-plain` ; do 66285031Sdes verbose "learn additional hostkeys, type=$k" 67285031Sdes dossh -oStrictHostKeyChecking=yes -oHostKeyAlgorithms=$k,$all_algs 68285031Sdes expect_nkeys $nkeys "learn hostkeys $k" 69285031Sdes check_key_present $k || fail "didn't learn $k" 70285031Sdesdone 71285031Sdes 72285031Sdes# Change one hostkey (non primary) and relearn 73285031Sdesverbose "learn changed non-primary hostkey" 74285031Sdesmv $OBJ/hkr.ssh-rsa.pub $OBJ/hkr.ssh-rsa.pub.old 75285031Sdesrm -f $OBJ/hkr.ssh-rsa 76285031Sdes${SSHKEYGEN} -qt ssh-rsa -f $OBJ/hkr.ssh-rsa -N '' || fatal "ssh-keygen $k" 77295367Sdesdossh -oStrictHostKeyChecking=yes -oHostKeyAlgorithms=$all_algs 78285031Sdes# Check that the key was replaced 79285031Sdesexpect_nkeys $nkeys "learn hostkeys" 80285031Sdescheck_key_present ssh-rsa $OBJ/hkr.ssh-rsa.pub.old && fail "old key present" 81285031Sdescheck_key_present ssh-rsa || fail "didn't learn changed key" 82285031Sdes 83285031Sdes# Add new hostkey (primary type) to sshd and connect 84285031Sdesverbose "learn new primary hostkey" 85285031Sdes${SSHKEYGEN} -qt ssh-rsa -f $OBJ/hkr.ssh-rsa-new -N '' || fatal "ssh-keygen $k" 86285031Sdes( cat $OBJ/sshd_proxy.orig ; echo HostKey $OBJ/hkr.ssh-rsa-new ) \ 87285031Sdes > $OBJ/sshd_proxy 88285031Sdes# Check new hostkey added 89285031Sdesdossh -oStrictHostKeyChecking=yes -oHostKeyAlgorithms=ssh-rsa,$all_algs 90285031Sdesexpect_nkeys `expr $nkeys + 1` "learn hostkeys" 91285031Sdescheck_key_present ssh-rsa || fail "current key missing" 92285031Sdescheck_key_present ssh-rsa $OBJ/hkr.ssh-rsa-new.pub || fail "new key missing" 93285031Sdes 94285031Sdes# Remove old hostkey (primary type) from sshd 95285031Sdesverbose "rotate primary hostkey" 96285031Sdescp $OBJ/sshd_proxy.orig $OBJ/sshd_proxy 97285031Sdesmv $OBJ/hkr.ssh-rsa.pub $OBJ/hkr.ssh-rsa.pub.old 98285031Sdesmv $OBJ/hkr.ssh-rsa-new.pub $OBJ/hkr.ssh-rsa.pub 99285031Sdesmv $OBJ/hkr.ssh-rsa-new $OBJ/hkr.ssh-rsa 100285031Sdes# Check old hostkey removed 101285031Sdesdossh -oStrictHostKeyChecking=yes -oHostKeyAlgorithms=ssh-rsa,$all_algs 102285031Sdesexpect_nkeys $nkeys "learn hostkeys" 103285031Sdescheck_key_present ssh-rsa $OBJ/hkr.ssh-rsa.pub.old && fail "old key present" 104285031Sdescheck_key_present ssh-rsa || fail "didn't learn changed key" 105285031Sdes 106285031Sdes# Connect again, forcing rotated key 107285031Sdesverbose "check rotate primary hostkey" 108285031Sdesdossh -oStrictHostKeyChecking=yes -oHostKeyAlgorithms=ssh-rsa 109285031Sdesexpect_nkeys 1 "learn hostkeys" 110285031Sdescheck_key_present ssh-rsa || fail "didn't learn changed key" 111