1*1323ec57SEd Maste# $OpenBSD: hostkey-agent.sh,v 1.13 2021/09/30 05:20:08 dtucker Exp $ 2bc5531deSDag-Erling Smørgrav# Placed in the Public Domain. 3bc5531deSDag-Erling Smørgrav 4bc5531deSDag-Erling Smørgravtid="hostkey agent" 5bc5531deSDag-Erling Smørgrav 6*1323ec57SEd Masterm -f $OBJ/agent-key.* $OBJ/ssh_proxy.orig $OBJ/known_hosts.orig $OBJ/agent-ca* 7bc5531deSDag-Erling Smørgrav 8bc5531deSDag-Erling Smørgravtrace "start agent" 919261079SEd Masteeval `${SSHAGENT} ${EXTRA_AGENT_ARGS} -s` > /dev/null 10bc5531deSDag-Erling Smørgravr=$? 11bc5531deSDag-Erling Smørgrav[ $r -ne 0 ] && fatal "could not start ssh-agent: exit code $r" 12bc5531deSDag-Erling Smørgrav 13bc5531deSDag-Erling Smørgravgrep -vi 'hostkey' $OBJ/sshd_proxy > $OBJ/sshd_proxy.orig 14bc5531deSDag-Erling Smørgravecho "HostKeyAgent $SSH_AUTH_SOCK" >> $OBJ/sshd_proxy.orig 15bc5531deSDag-Erling Smørgrav 16*1323ec57SEd Mastetrace "make CA key" 17*1323ec57SEd Maste 18*1323ec57SEd Maste${SSHKEYGEN} -qt ed25519 -f $OBJ/agent-ca -N '' || fatal "ssh-keygen CA" 19*1323ec57SEd Maste 20bc5531deSDag-Erling Smørgravtrace "load hostkeys" 2119261079SEd Mastefor k in $SSH_KEYTYPES ; do 22bc5531deSDag-Erling Smørgrav ${SSHKEYGEN} -qt $k -f $OBJ/agent-key.$k -N '' || fatal "ssh-keygen $k" 23*1323ec57SEd Maste ${SSHKEYGEN} -s $OBJ/agent-ca -qh -n localhost-with-alias \ 24*1323ec57SEd Maste -I localhost-with-alias $OBJ/agent-key.$k.pub || \ 25*1323ec57SEd Maste fatal "sign $k" 26*1323ec57SEd Maste ${SSHADD} -k $OBJ/agent-key.$k >/dev/null 2>&1 || \ 27bc5531deSDag-Erling Smørgrav fatal "couldn't load key $OBJ/agent-key.$k" 28bc5531deSDag-Erling Smørgrav # Remove private key so the server can't use it. 29bc5531deSDag-Erling Smørgrav rm $OBJ/agent-key.$k || fatal "couldn't rm $OBJ/agent-key.$k" 30bc5531deSDag-Erling Smørgravdone 31*1323ec57SEd Masterm $OBJ/agent-ca # Don't need CA private any more either 32bc5531deSDag-Erling Smørgrav 33bc5531deSDag-Erling Smørgravunset SSH_AUTH_SOCK 34bc5531deSDag-Erling Smørgrav 3519261079SEd Mastefor k in $SSH_KEYTYPES ; do 36*1323ec57SEd Maste verbose "key type $k" 37eccfee6eSDag-Erling Smørgrav cp $OBJ/sshd_proxy.orig $OBJ/sshd_proxy 38eccfee6eSDag-Erling Smørgrav echo "HostKeyAlgorithms $k" >> $OBJ/sshd_proxy 39*1323ec57SEd Maste echo "Hostkey $OBJ/agent-key.${k}" >> $OBJ/sshd_proxy 40bc5531deSDag-Erling Smørgrav opts="-oHostKeyAlgorithms=$k -F $OBJ/ssh_proxy" 41*1323ec57SEd Maste ( printf 'localhost-with-alias,127.0.0.1,::1 ' ; 42*1323ec57SEd Maste cat $OBJ/agent-key.$k.pub) > $OBJ/known_hosts 43bc5531deSDag-Erling Smørgrav SSH_CONNECTION=`${SSH} $opts host 'echo $SSH_CONNECTION'` 44bc5531deSDag-Erling Smørgrav if [ $? -ne 0 ]; then 45*1323ec57SEd Maste fail "keytype $k failed" 46bc5531deSDag-Erling Smørgrav fi 47bc5531deSDag-Erling Smørgrav if [ "$SSH_CONNECTION" != "UNKNOWN 65535 UNKNOWN 65535" ]; then 48*1323ec57SEd Maste fail "bad SSH_CONNECTION key type $k" 49bc5531deSDag-Erling Smørgrav fi 50bc5531deSDag-Erling Smørgravdone 51*1323ec57SEd Maste 52*1323ec57SEd MasteSSH_CERTTYPES=`ssh -Q key-sig | grep 'cert-v01@openssh.com'` 53*1323ec57SEd Maste 54*1323ec57SEd Maste# Prepare sshd_proxy for certificates. 55*1323ec57SEd Mastecp $OBJ/sshd_proxy.orig $OBJ/sshd_proxy 56*1323ec57SEd MasteHOSTKEYALGS="" 57*1323ec57SEd Mastefor k in $SSH_CERTTYPES ; do 58*1323ec57SEd Maste test -z "$HOSTKEYALGS" || HOSTKEYALGS="${HOSTKEYALGS}," 59*1323ec57SEd Maste HOSTKEYALGS="${HOSTKEYALGS}${k}" 60*1323ec57SEd Mastedone 61*1323ec57SEd Mastefor k in $SSH_KEYTYPES ; do 62*1323ec57SEd Maste echo "Hostkey $OBJ/agent-key.${k}.pub" >> $OBJ/sshd_proxy 63*1323ec57SEd Maste echo "HostCertificate $OBJ/agent-key.${k}-cert.pub" >> $OBJ/sshd_proxy 64*1323ec57SEd Maste test -f $OBJ/agent-key.${k}.pub || fatal "no $k key" 65*1323ec57SEd Maste test -f $OBJ/agent-key.${k}-cert.pub || fatal "no $k cert" 66*1323ec57SEd Mastedone 67*1323ec57SEd Masteecho "HostKeyAlgorithms $HOSTKEYALGS" >> $OBJ/sshd_proxy 68*1323ec57SEd Maste 69*1323ec57SEd Maste# Add only CA trust anchor to known_hosts. 70*1323ec57SEd Maste( printf '@cert-authority localhost-with-alias ' ; 71*1323ec57SEd Maste cat $OBJ/agent-ca.pub) > $OBJ/known_hosts 72*1323ec57SEd Maste 73*1323ec57SEd Mastefor k in $SSH_CERTTYPES ; do 74*1323ec57SEd Maste verbose "cert type $k" 75*1323ec57SEd Maste opts="-oHostKeyAlgorithms=$k -F $OBJ/ssh_proxy" 76*1323ec57SEd Maste SSH_CONNECTION=`${SSH} $opts host 'echo $SSH_CONNECTION'` 77*1323ec57SEd Maste if [ $? -ne 0 ]; then 78*1323ec57SEd Maste fail "cert type $k failed" 79*1323ec57SEd Maste fi 80*1323ec57SEd Maste if [ "$SSH_CONNECTION" != "UNKNOWN 65535 UNKNOWN 65535" ]; then 81*1323ec57SEd Maste fail "bad SSH_CONNECTION key type $k" 82*1323ec57SEd Maste fi 83bc5531deSDag-Erling Smørgravdone 84bc5531deSDag-Erling Smørgrav 85bc5531deSDag-Erling Smørgravtrace "kill agent" 86bc5531deSDag-Erling Smørgrav${SSHAGENT} -k > /dev/null 87bc5531deSDag-Erling Smørgrav 88