1*4f52dfbbSDag-Erling Smørgrav# $OpenBSD: agent-pkcs11.sh,v 1.3 2017/04/30 23:34:55 djm Exp $ 2ce3adf43SDag-Erling Smørgrav# Placed in the Public Domain. 3ce3adf43SDag-Erling Smørgrav 4ce3adf43SDag-Erling Smørgravtid="pkcs11 agent test" 5ce3adf43SDag-Erling Smørgrav 6ce3adf43SDag-Erling SmørgravTEST_SSH_PIN="" 7ce3adf43SDag-Erling SmørgravTEST_SSH_PKCS11=/usr/local/lib/soft-pkcs11.so.0.0 8ce3adf43SDag-Erling Smørgrav 9bc5531deSDag-Erling Smørgravtest -f "$TEST_SSH_PKCS11" || fatal "$TEST_SSH_PKCS11 does not exist" 10bc5531deSDag-Erling Smørgrav 11ce3adf43SDag-Erling Smørgrav# setup environment for soft-pkcs11 token 12ce3adf43SDag-Erling SmørgravSOFTPKCS11RC=$OBJ/pkcs11.info 13ce3adf43SDag-Erling Smørgravexport SOFTPKCS11RC 14ce3adf43SDag-Erling Smørgrav# prevent ssh-agent from calling ssh-askpass 15ce3adf43SDag-Erling SmørgravSSH_ASKPASS=/usr/bin/true 16ce3adf43SDag-Erling Smørgravexport SSH_ASKPASS 17ce3adf43SDag-Erling Smørgravunset DISPLAY 18ce3adf43SDag-Erling Smørgrav 19ce3adf43SDag-Erling Smørgrav# start command w/o tty, so ssh-add accepts pin from stdin 20ce3adf43SDag-Erling Smørgravnotty() { 21ce3adf43SDag-Erling Smørgrav perl -e 'use POSIX; POSIX::setsid(); 22ce3adf43SDag-Erling Smørgrav if (fork) { wait; exit($? >> 8); } else { exec(@ARGV) }' "$@" 23ce3adf43SDag-Erling Smørgrav} 24ce3adf43SDag-Erling Smørgrav 25ce3adf43SDag-Erling Smørgravtrace "start agent" 26ce3adf43SDag-Erling Smørgraveval `${SSHAGENT} -s` > /dev/null 27ce3adf43SDag-Erling Smørgravr=$? 28ce3adf43SDag-Erling Smørgravif [ $r -ne 0 ]; then 29ce3adf43SDag-Erling Smørgrav fail "could not start ssh-agent: exit code $r" 30ce3adf43SDag-Erling Smørgravelse 31ce3adf43SDag-Erling Smørgrav trace "generating key/cert" 32ce3adf43SDag-Erling Smørgrav rm -f $OBJ/pkcs11.key $OBJ/pkcs11.crt 33ce3adf43SDag-Erling Smørgrav openssl genrsa -out $OBJ/pkcs11.key 2048 > /dev/null 2>&1 34ce3adf43SDag-Erling Smørgrav chmod 600 $OBJ/pkcs11.key 35ce3adf43SDag-Erling Smørgrav openssl req -key $OBJ/pkcs11.key -new -x509 \ 36ce3adf43SDag-Erling Smørgrav -out $OBJ/pkcs11.crt -text -subj '/CN=pkcs11 test' > /dev/null 37ce3adf43SDag-Erling Smørgrav printf "a\ta\t$OBJ/pkcs11.crt\t$OBJ/pkcs11.key" > $SOFTPKCS11RC 38ce3adf43SDag-Erling Smørgrav # add to authorized keys 39ce3adf43SDag-Erling Smørgrav ${SSHKEYGEN} -y -f $OBJ/pkcs11.key > $OBJ/authorized_keys_$USER 40ce3adf43SDag-Erling Smørgrav 41ce3adf43SDag-Erling Smørgrav trace "add pkcs11 key to agent" 42ce3adf43SDag-Erling Smørgrav echo ${TEST_SSH_PIN} | notty ${SSHADD} -s ${TEST_SSH_PKCS11} > /dev/null 2>&1 43ce3adf43SDag-Erling Smørgrav r=$? 44ce3adf43SDag-Erling Smørgrav if [ $r -ne 0 ]; then 45ce3adf43SDag-Erling Smørgrav fail "ssh-add -s failed: exit code $r" 46ce3adf43SDag-Erling Smørgrav fi 47ce3adf43SDag-Erling Smørgrav 48ce3adf43SDag-Erling Smørgrav trace "pkcs11 list via agent" 49ce3adf43SDag-Erling Smørgrav ${SSHADD} -l > /dev/null 2>&1 50ce3adf43SDag-Erling Smørgrav r=$? 51ce3adf43SDag-Erling Smørgrav if [ $r -ne 0 ]; then 52ce3adf43SDag-Erling Smørgrav fail "ssh-add -l failed: exit code $r" 53ce3adf43SDag-Erling Smørgrav fi 54ce3adf43SDag-Erling Smørgrav 55ce3adf43SDag-Erling Smørgrav trace "pkcs11 connect via agent" 56*4f52dfbbSDag-Erling Smørgrav ${SSH} -F $OBJ/ssh_proxy somehost exit 5 57ce3adf43SDag-Erling Smørgrav r=$? 58ce3adf43SDag-Erling Smørgrav if [ $r -ne 5 ]; then 59ce3adf43SDag-Erling Smørgrav fail "ssh connect failed (exit code $r)" 60ce3adf43SDag-Erling Smørgrav fi 61ce3adf43SDag-Erling Smørgrav 62ce3adf43SDag-Erling Smørgrav trace "remove pkcs11 keys" 63ce3adf43SDag-Erling Smørgrav echo ${TEST_SSH_PIN} | notty ${SSHADD} -e ${TEST_SSH_PKCS11} > /dev/null 2>&1 64ce3adf43SDag-Erling Smørgrav r=$? 65ce3adf43SDag-Erling Smørgrav if [ $r -ne 0 ]; then 66ce3adf43SDag-Erling Smørgrav fail "ssh-add -e failed: exit code $r" 67ce3adf43SDag-Erling Smørgrav fi 68ce3adf43SDag-Erling Smørgrav 69ce3adf43SDag-Erling Smørgrav trace "kill agent" 70ce3adf43SDag-Erling Smørgrav ${SSHAGENT} -k > /dev/null 71ce3adf43SDag-Erling Smørgravfi 72