2021-09-30 07:20:08 +02:00
|
|
|
# $OpenBSD: hostkey-agent.sh,v 1.13 2021/09/30 05:20:08 dtucker Exp $
|
2015-01-17 19:54:30 +01:00
|
|
|
# Placed in the Public Domain.
|
|
|
|
|
|
|
|
tid="hostkey agent"
|
|
|
|
|
2021-09-29 03:32:21 +02:00
|
|
|
rm -f $OBJ/agent-key.* $OBJ/ssh_proxy.orig $OBJ/known_hosts.orig $OBJ/agent-ca*
|
2015-01-17 19:54:30 +01:00
|
|
|
|
|
|
|
trace "start agent"
|
2019-11-27 00:43:10 +01:00
|
|
|
eval `${SSHAGENT} ${EXTRA_AGENT_ARGS} -s` > /dev/null
|
2015-01-17 19:54:30 +01:00
|
|
|
r=$?
|
|
|
|
[ $r -ne 0 ] && fatal "could not start ssh-agent: exit code $r"
|
|
|
|
|
|
|
|
grep -vi 'hostkey' $OBJ/sshd_proxy > $OBJ/sshd_proxy.orig
|
|
|
|
echo "HostKeyAgent $SSH_AUTH_SOCK" >> $OBJ/sshd_proxy.orig
|
|
|
|
|
2021-09-29 03:32:21 +02:00
|
|
|
trace "make CA key"
|
|
|
|
|
|
|
|
${SSHKEYGEN} -qt ed25519 -f $OBJ/agent-ca -N '' || fatal "ssh-keygen CA"
|
|
|
|
|
2015-01-17 19:54:30 +01:00
|
|
|
trace "load hostkeys"
|
2019-12-16 03:39:05 +01:00
|
|
|
for k in $SSH_KEYTYPES ; do
|
2015-01-20 09:02:33 +01:00
|
|
|
${SSHKEYGEN} -qt $k -f $OBJ/agent-key.$k -N '' || fatal "ssh-keygen $k"
|
2021-09-29 03:32:21 +02:00
|
|
|
${SSHKEYGEN} -s $OBJ/agent-ca -qh -n localhost-with-alias \
|
|
|
|
-I localhost-with-alias $OBJ/agent-key.$k.pub || \
|
|
|
|
fatal "sign $k"
|
|
|
|
${SSHADD} -k $OBJ/agent-key.$k >/dev/null 2>&1 || \
|
2015-01-20 09:02:33 +01:00
|
|
|
fatal "couldn't load key $OBJ/agent-key.$k"
|
2015-01-17 19:54:30 +01:00
|
|
|
# Remove private key so the server can't use it.
|
2015-01-20 09:02:33 +01:00
|
|
|
rm $OBJ/agent-key.$k || fatal "couldn't rm $OBJ/agent-key.$k"
|
2015-01-17 19:54:30 +01:00
|
|
|
done
|
2021-09-29 03:32:21 +02:00
|
|
|
rm $OBJ/agent-ca # Don't need CA private any more either
|
2015-01-17 19:54:30 +01:00
|
|
|
|
|
|
|
unset SSH_AUTH_SOCK
|
|
|
|
|
2021-09-29 03:32:21 +02:00
|
|
|
for k in $SSH_KEYTYPES ; do
|
|
|
|
verbose "key type $k"
|
|
|
|
cp $OBJ/sshd_proxy.orig $OBJ/sshd_proxy
|
|
|
|
echo "HostKeyAlgorithms $k" >> $OBJ/sshd_proxy
|
|
|
|
echo "Hostkey $OBJ/agent-key.${k}" >> $OBJ/sshd_proxy
|
|
|
|
opts="-oHostKeyAlgorithms=$k -F $OBJ/ssh_proxy"
|
|
|
|
( printf 'localhost-with-alias,127.0.0.1,::1 ' ;
|
|
|
|
cat $OBJ/agent-key.$k.pub) > $OBJ/known_hosts
|
|
|
|
SSH_CONNECTION=`${SSH} $opts host 'echo $SSH_CONNECTION'`
|
|
|
|
if [ $? -ne 0 ]; then
|
|
|
|
fail "keytype $k failed"
|
|
|
|
fi
|
|
|
|
if [ "$SSH_CONNECTION" != "UNKNOWN 65535 UNKNOWN 65535" ]; then
|
2021-09-30 07:20:08 +02:00
|
|
|
fail "bad SSH_CONNECTION key type $k"
|
2021-09-29 03:32:21 +02:00
|
|
|
fi
|
|
|
|
done
|
|
|
|
|
|
|
|
SSH_CERTTYPES=`ssh -Q key-sig | grep 'cert-v01@openssh.com'`
|
|
|
|
|
|
|
|
# Prepare sshd_proxy for certificates.
|
|
|
|
cp $OBJ/sshd_proxy.orig $OBJ/sshd_proxy
|
|
|
|
HOSTKEYALGS=""
|
|
|
|
for k in $SSH_CERTTYPES ; do
|
|
|
|
test -z "$HOSTKEYALGS" || HOSTKEYALGS="${HOSTKEYALGS},"
|
|
|
|
HOSTKEYALGS="${HOSTKEYALGS}${k}"
|
|
|
|
done
|
|
|
|
for k in $SSH_KEYTYPES ; do
|
|
|
|
echo "Hostkey $OBJ/agent-key.${k}.pub" >> $OBJ/sshd_proxy
|
|
|
|
echo "HostCertificate $OBJ/agent-key.${k}-cert.pub" >> $OBJ/sshd_proxy
|
|
|
|
test -f $OBJ/agent-key.${k}.pub || fatal "no $k key"
|
|
|
|
test -f $OBJ/agent-key.${k}-cert.pub || fatal "no $k cert"
|
|
|
|
done
|
|
|
|
echo "HostKeyAlgorithms $HOSTKEYALGS" >> $OBJ/sshd_proxy
|
|
|
|
|
|
|
|
# Add only CA trust anchor to known_hosts.
|
|
|
|
( printf '@cert-authority localhost-with-alias ' ;
|
|
|
|
cat $OBJ/agent-ca.pub) > $OBJ/known_hosts
|
|
|
|
|
|
|
|
for k in $SSH_CERTTYPES ; do
|
|
|
|
verbose "cert type $k"
|
|
|
|
opts="-oHostKeyAlgorithms=$k -F $OBJ/ssh_proxy"
|
|
|
|
SSH_CONNECTION=`${SSH} $opts host 'echo $SSH_CONNECTION'`
|
|
|
|
if [ $? -ne 0 ]; then
|
|
|
|
fail "cert type $k failed"
|
|
|
|
fi
|
|
|
|
if [ "$SSH_CONNECTION" != "UNKNOWN 65535 UNKNOWN 65535" ]; then
|
2021-09-30 07:20:08 +02:00
|
|
|
fail "bad SSH_CONNECTION key type $k"
|
2021-09-29 03:32:21 +02:00
|
|
|
fi
|
2015-01-17 19:54:30 +01:00
|
|
|
done
|
|
|
|
|
|
|
|
trace "kill agent"
|
|
|
|
${SSHAGENT} -k > /dev/null
|
|
|
|
|