agent-pkcs11.sh 3.1 KB

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