agent-pkcs11.sh revision 1.10
1# $OpenBSD: agent-pkcs11.sh,v 1.10 2023/10/06 03:25:14 djm Exp $ 2# Placed in the Public Domain. 3 4tid="pkcs11 agent test" 5 6TEST_SSH_PIN=1234 7TEST_SSH_SOPIN=12345678 8TEST_SSH_PKCS11=/usr/local/lib/softhsm/libsofthsm2.so 9if [ "x$TEST_SSH_SSHPKCS11HELPER" != "x" ]; then 10 SSH_PKCS11_HELPER="${TEST_SSH_SSHPKCS11HELPER}" 11 export SSH_PKCS11_HELPER 12fi 13 14test -f "$TEST_SSH_PKCS11" || fatal "$TEST_SSH_PKCS11 does not exist" 15 16# setup environment for softhsm2 token 17DIR=$OBJ/SOFTHSM 18rm -rf $DIR 19TOKEN=$DIR/tokendir 20mkdir -p $TOKEN 21SOFTHSM2_CONF=$DIR/softhsm2.conf 22export SOFTHSM2_CONF 23cat > $SOFTHSM2_CONF << EOF 24# SoftHSM v2 configuration file 25directories.tokendir = ${TOKEN} 26objectstore.backend = file 27# ERROR, WARNING, INFO, DEBUG 28log.level = DEBUG 29# If CKF_REMOVABLE_DEVICE flag should be set 30slots.removable = false 31EOF 32out=$(softhsm2-util --init-token --free --label token-slot-0 --pin "$TEST_SSH_PIN" --so-pin "$TEST_SSH_SOPIN") 33slot=$(echo -- $out | sed 's/.* //') 34 35# prevent ssh-agent from calling ssh-askpass 36SSH_ASKPASS=/usr/bin/true 37export SSH_ASKPASS 38unset DISPLAY 39 40# start command w/o tty, so ssh-add accepts pin from stdin 41# XXX could force askpass instead 42notty() { 43 perl -e 'use POSIX; POSIX::setsid(); 44 if (fork) { wait; exit($? >> 8); } else { exec(@ARGV) }' "$@" 45} 46 47trace "generating keys" 48RSA=${DIR}/RSA 49RSAP8=${DIR}/RSAP8 50ECPARAM=${DIR}/ECPARAM 51EC=${DIR}/EC 52ECP8=${DIR}/ECP8 53$OPENSSL_BIN genpkey -algorithm rsa > $RSA || fatal "genpkey RSA fail" 54$OPENSSL_BIN pkcs8 -nocrypt -in $RSA > $RSAP8 || fatal "pkcs8 RSA fail" 55softhsm2-util --slot "$slot" --label 01 --id 01 \ 56 --pin "$TEST_SSH_PIN" --import $RSAP8 || fatal "softhsm import RSA fail" 57 58$OPENSSL_BIN genpkey \ 59 -genparam \ 60 -algorithm ec \ 61 -pkeyopt ec_paramgen_curve:prime256v1 > $ECPARAM || fatal "param EC fail" 62$OPENSSL_BIN genpkey -paramfile $ECPARAM > $EC || fatal "genpkey EC fail" 63$OPENSSL_BIN pkcs8 -nocrypt -in $EC > $ECP8 || fatal "pkcs8 EC fail" 64softhsm2-util --slot "$slot" --label 02 --id 02 \ 65 --pin "$TEST_SSH_PIN" --import $ECP8 || fatal "softhasm import EC fail" 66 67trace "start agent" 68eval `${SSHAGENT} ${EXTRA_AGENT_ARGS} -s` > /dev/null 69r=$? 70if [ $r -ne 0 ]; then 71 fail "could not start ssh-agent: exit code $r" 72else 73 trace "add pkcs11 key to agent" 74 echo ${TEST_SSH_PIN} | notty ${SSHADD} -s ${TEST_SSH_PKCS11} > /dev/null 2>&1 75 r=$? 76 if [ $r -ne 0 ]; then 77 fail "ssh-add -s failed: exit code $r" 78 fi 79 80 trace "pkcs11 list via agent" 81 ${SSHADD} -l > /dev/null 2>&1 82 r=$? 83 if [ $r -ne 0 ]; then 84 fail "ssh-add -l failed: exit code $r" 85 fi 86 87 for k in $RSA $EC; do 88 trace "testing $k" 89 chmod 600 $k 90 ssh-keygen -y -f $k > $k.pub 91 pub=$(cat $k.pub) 92 ${SSHADD} -L | grep -q "$pub" || \ 93 fail "key $k missing in ssh-add -L" 94 ${SSHADD} -T $k.pub || fail "ssh-add -T with $k failed" 95 96 # add to authorized keys 97 cat $k.pub > $OBJ/authorized_keys_$USER 98 trace "pkcs11 connect via agent ($k)" 99 ${SSH} -F $OBJ/ssh_proxy somehost exit 5 100 r=$? 101 if [ $r -ne 5 ]; then 102 fail "ssh connect failed (exit code $r)" 103 fi 104 done 105 106 trace "remove pkcs11 keys" 107 echo ${TEST_SSH_PIN} | notty ${SSHADD} -e ${TEST_SSH_PKCS11} > /dev/null 2>&1 108 r=$? 109 if [ $r -ne 0 ]; then 110 fail "ssh-add -e failed: exit code $r" 111 fi 112 113 trace "kill agent" 114 ${SSHAGENT} -k > /dev/null 115fi 116