2019-11-26 23:43:10 +00:00
|
|
|
# $OpenBSD: hostkey-rotate.sh,v 1.8 2019/11/26 23:43:10 djm Exp $
|
2015-01-26 06:12:18 +00:00
|
|
|
# Placed in the Public Domain.
|
|
|
|
|
|
|
|
tid="hostkey rotate"
|
|
|
|
|
|
|
|
rm -f $OBJ/hkr.* $OBJ/ssh_proxy.orig
|
|
|
|
|
|
|
|
grep -vi 'hostkey' $OBJ/sshd_proxy > $OBJ/sshd_proxy.orig
|
|
|
|
echo "UpdateHostkeys=yes" >> $OBJ/ssh_proxy
|
|
|
|
rm $OBJ/known_hosts
|
|
|
|
|
2019-08-30 05:08:28 +00:00
|
|
|
# The "primary" key type is ed25519 since it's supported even when built
|
|
|
|
# without OpenSSL. The secondary is RSA if it's supported.
|
|
|
|
primary="ssh-ed25519"
|
|
|
|
secondary="$primary"
|
|
|
|
|
2015-01-26 06:12:18 +00:00
|
|
|
trace "prepare hostkeys"
|
|
|
|
nkeys=0
|
|
|
|
all_algs=""
|
2019-11-26 23:43:10 +00:00
|
|
|
for k in $SSH_HOSTKEY_TYPES; do
|
2015-01-26 06:12:18 +00:00
|
|
|
${SSHKEYGEN} -qt $k -f $OBJ/hkr.$k -N '' || fatal "ssh-keygen $k"
|
|
|
|
echo "Hostkey $OBJ/hkr.${k}" >> $OBJ/sshd_proxy.orig
|
|
|
|
nkeys=`expr $nkeys + 1`
|
|
|
|
test "x$all_algs" = "x" || all_algs="${all_algs},"
|
|
|
|
all_algs="${all_algs}$k"
|
2019-08-30 05:08:28 +00:00
|
|
|
case "$k" in
|
|
|
|
ssh-rsa) secondary="ssh-rsa" ;;
|
|
|
|
esac
|
2015-01-26 06:12:18 +00:00
|
|
|
done
|
|
|
|
|
|
|
|
dossh() {
|
|
|
|
# All ssh should succeed in this test
|
|
|
|
${SSH} -F $OBJ/ssh_proxy "$@" x true || fail "ssh $@ failed"
|
|
|
|
}
|
|
|
|
|
|
|
|
expect_nkeys() {
|
|
|
|
_expected=$1
|
|
|
|
_message=$2
|
|
|
|
_n=`wc -l $OBJ/known_hosts | awk '{ print $1 }'` || fatal "wc failed"
|
|
|
|
[ "x$_n" = "x$_expected" ] || fail "$_message (got $_n wanted $_expected)"
|
|
|
|
}
|
|
|
|
|
|
|
|
check_key_present() {
|
|
|
|
_type=$1
|
|
|
|
_kfile=$2
|
|
|
|
test "x$_kfile" = "x" && _kfile="$OBJ/hkr.${_type}.pub"
|
2015-03-03 17:53:40 +00:00
|
|
|
_kpub=`awk "/$_type /"' { print $2 }' < $_kfile` || \
|
2015-01-26 06:12:18 +00:00
|
|
|
fatal "awk failed"
|
2015-03-03 17:53:40 +00:00
|
|
|
fgrep "$_kpub" $OBJ/known_hosts > /dev/null
|
2015-01-26 06:12:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
cp $OBJ/sshd_proxy.orig $OBJ/sshd_proxy
|
|
|
|
|
|
|
|
# Connect to sshd with StrictHostkeyChecking=no
|
|
|
|
verbose "learn hostkey with StrictHostKeyChecking=no"
|
|
|
|
>$OBJ/known_hosts
|
2019-08-30 05:08:28 +00:00
|
|
|
dossh -oHostKeyAlgorithms=$primary -oStrictHostKeyChecking=no
|
2015-01-26 06:12:18 +00:00
|
|
|
# Verify no additional keys learned
|
|
|
|
expect_nkeys 1 "unstrict connect keys"
|
2019-08-30 05:08:28 +00:00
|
|
|
check_key_present $primary || fail "unstrict didn't learn key"
|
2015-01-26 06:12:18 +00:00
|
|
|
|
|
|
|
# Connect to sshd as usual
|
|
|
|
verbose "learn additional hostkeys"
|
2015-07-10 06:23:25 +00:00
|
|
|
dossh -oStrictHostKeyChecking=yes -oHostKeyAlgorithms=$all_algs
|
2015-01-26 06:12:18 +00:00
|
|
|
# Check that other keys learned
|
|
|
|
expect_nkeys $nkeys "learn hostkeys"
|
2019-11-26 23:43:10 +00:00
|
|
|
for k in $SSH_HOSTKEY_TYPES; do
|
2019-08-30 05:08:28 +00:00
|
|
|
check_key_present $k || fail "didn't learn keytype $k"
|
|
|
|
done
|
2015-01-26 06:12:18 +00:00
|
|
|
|
|
|
|
# Check each key type
|
2019-11-26 23:43:10 +00:00
|
|
|
for k in $SSH_HOSTKEY_TYPES; do
|
2015-01-26 06:12:18 +00:00
|
|
|
verbose "learn additional hostkeys, type=$k"
|
|
|
|
dossh -oStrictHostKeyChecking=yes -oHostKeyAlgorithms=$k,$all_algs
|
|
|
|
expect_nkeys $nkeys "learn hostkeys $k"
|
2019-08-30 05:08:28 +00:00
|
|
|
check_key_present $k || fail "didn't learn $k correctly"
|
2015-01-26 06:12:18 +00:00
|
|
|
done
|
|
|
|
|
|
|
|
# Change one hostkey (non primary) and relearn
|
2019-08-30 05:08:28 +00:00
|
|
|
if [ "$primary" != "$secondary" ]; then
|
|
|
|
verbose "learn changed non-primary hostkey type=${secondary}"
|
|
|
|
mv $OBJ/hkr.${secondary}.pub $OBJ/hkr.${secondary}.pub.old
|
|
|
|
rm -f $OBJ/hkr.${secondary}
|
|
|
|
${SSHKEYGEN} -qt ${secondary} -f $OBJ/hkr.${secondary} -N '' || \
|
|
|
|
fatal "ssh-keygen $secondary"
|
|
|
|
dossh -oStrictHostKeyChecking=yes -oHostKeyAlgorithms=$all_algs
|
|
|
|
# Check that the key was replaced
|
|
|
|
expect_nkeys $nkeys "learn hostkeys"
|
|
|
|
check_key_present ${secondary} $OBJ/hkr.${secondary}.pub.old && \
|
|
|
|
fail "old key present"
|
|
|
|
check_key_present ${secondary} || fail "didn't learn changed key"
|
|
|
|
fi
|
2015-01-26 06:12:18 +00:00
|
|
|
|
|
|
|
# Add new hostkey (primary type) to sshd and connect
|
|
|
|
verbose "learn new primary hostkey"
|
2019-08-30 05:08:28 +00:00
|
|
|
${SSHKEYGEN} -qt ${primary} -f $OBJ/hkr.${primary}-new -N '' || fatal "ssh-keygen ed25519"
|
|
|
|
( cat $OBJ/sshd_proxy.orig ; echo HostKey $OBJ/hkr.${primary}-new ) \
|
2015-01-26 06:12:18 +00:00
|
|
|
> $OBJ/sshd_proxy
|
|
|
|
# Check new hostkey added
|
2019-08-30 05:08:28 +00:00
|
|
|
dossh -oStrictHostKeyChecking=yes -oHostKeyAlgorithms=${primary},$all_algs
|
2015-01-26 06:12:18 +00:00
|
|
|
expect_nkeys `expr $nkeys + 1` "learn hostkeys"
|
2019-08-30 05:08:28 +00:00
|
|
|
check_key_present ${primary} || fail "current key missing"
|
|
|
|
check_key_present ${primary} $OBJ/hkr.${primary}-new.pub || fail "new key missing"
|
2015-01-26 06:12:18 +00:00
|
|
|
|
|
|
|
# Remove old hostkey (primary type) from sshd
|
|
|
|
verbose "rotate primary hostkey"
|
|
|
|
cp $OBJ/sshd_proxy.orig $OBJ/sshd_proxy
|
2019-08-30 05:08:28 +00:00
|
|
|
mv $OBJ/hkr.${primary}.pub $OBJ/hkr.${primary}.pub.old
|
|
|
|
mv $OBJ/hkr.${primary}-new.pub $OBJ/hkr.${primary}.pub
|
|
|
|
mv $OBJ/hkr.${primary}-new $OBJ/hkr.${primary}
|
2015-01-26 06:12:18 +00:00
|
|
|
# Check old hostkey removed
|
2019-08-30 05:08:28 +00:00
|
|
|
dossh -oStrictHostKeyChecking=yes -oHostKeyAlgorithms=${primary},$all_algs
|
2015-01-26 06:12:18 +00:00
|
|
|
expect_nkeys $nkeys "learn hostkeys"
|
2019-08-30 05:08:28 +00:00
|
|
|
check_key_present ${primary} $OBJ/hkr.${primary}.pub.old && fail "old key present"
|
|
|
|
check_key_present ${primary} || fail "didn't learn changed key"
|
2015-01-26 06:12:18 +00:00
|
|
|
|
|
|
|
# Connect again, forcing rotated key
|
|
|
|
verbose "check rotate primary hostkey"
|
2019-08-30 05:08:28 +00:00
|
|
|
dossh -oStrictHostKeyChecking=yes -oHostKeyAlgorithms=${primary}
|
2015-01-26 06:12:18 +00:00
|
|
|
expect_nkeys 1 "learn hostkeys"
|
2019-08-30 05:08:28 +00:00
|
|
|
check_key_present ${primary} || fail "didn't learn changed key"
|