summaryrefslogtreecommitdiff
path: root/regress/agent-pkcs11.sh
diff options
context:
space:
mode:
Diffstat (limited to 'regress/agent-pkcs11.sh')
-rw-r--r--regress/agent-pkcs11.sh97
1 files changed, 75 insertions, 22 deletions
diff --git a/regress/agent-pkcs11.sh b/regress/agent-pkcs11.sh
index db3018b88..5205d9067 100644
--- a/regress/agent-pkcs11.sh
+++ b/regress/agent-pkcs11.sh
@@ -1,16 +1,53 @@
1# $OpenBSD: agent-pkcs11.sh,v 1.3 2017/04/30 23:34:55 djm Exp $ 1# $OpenBSD: agent-pkcs11.sh,v 1.6 2019/01/21 09:13:41 djm Exp $
2# Placed in the Public Domain. 2# Placed in the Public Domain.
3 3
4tid="pkcs11 agent test" 4tid="pkcs11 agent test"
5 5
6TEST_SSH_PIN="" 6try_token_libs() {
7TEST_SSH_PKCS11=/usr/local/lib/soft-pkcs11.so.0.0 7 for _lib in "$@" ; do
8 if test -f "$_lib" ; then
9 verbose "Using token library $_lib"
10 TEST_SSH_PKCS11="$_lib"
11 return
12 fi
13 done
14 echo "skipped: Unable to find PKCS#11 token library"
15 exit 0
16}
17
18try_token_libs \
19 /usr/local/lib/softhsm/libsofthsm2.so \
20 /usr/lib64/pkcs11/libsofthsm2.so \
21 /usr/lib/x86_64-linux-gnu/softhsm/libsofthsm2.so
22
23TEST_SSH_PIN=1234
24TEST_SSH_SOPIN=12345678
25if [ "x$TEST_SSH_SSHPKCS11HELPER" != "x" ]; then
26 SSH_PKCS11_HELPER="${TEST_SSH_SSHPKCS11HELPER}"
27 export SSH_PKCS11_HELPER
28fi
8 29
9test -f "$TEST_SSH_PKCS11" || fatal "$TEST_SSH_PKCS11 does not exist" 30test -f "$TEST_SSH_PKCS11" || fatal "$TEST_SSH_PKCS11 does not exist"
10 31
11# setup environment for soft-pkcs11 token 32# setup environment for softhsm2 token
12SOFTPKCS11RC=$OBJ/pkcs11.info 33DIR=$OBJ/SOFTHSM
13export SOFTPKCS11RC 34rm -rf $DIR
35TOKEN=$DIR/tokendir
36mkdir -p $TOKEN
37SOFTHSM2_CONF=$DIR/softhsm2.conf
38export SOFTHSM2_CONF
39cat > $SOFTHSM2_CONF << EOF
40# SoftHSM v2 configuration file
41directories.tokendir = ${TOKEN}
42objectstore.backend = file
43# ERROR, WARNING, INFO, DEBUG
44log.level = DEBUG
45# If CKF_REMOVABLE_DEVICE flag should be set
46slots.removable = false
47EOF
48out=$(softhsm2-util --init-token --free --label token-slot-0 --pin "$TEST_SSH_PIN" --so-pin "$TEST_SSH_SOPIN")
49slot=$(echo -- $out | sed 's/.* //')
50
14# prevent ssh-agent from calling ssh-askpass 51# prevent ssh-agent from calling ssh-askpass
15SSH_ASKPASS=/usr/bin/true 52SSH_ASKPASS=/usr/bin/true
16export SSH_ASKPASS 53export SSH_ASKPASS
@@ -22,22 +59,27 @@ notty() {
22 if (fork) { wait; exit($? >> 8); } else { exec(@ARGV) }' "$@" 59 if (fork) { wait; exit($? >> 8); } else { exec(@ARGV) }' "$@"
23} 60}
24 61
62trace "generating keys"
63RSA=${DIR}/RSA
64EC=${DIR}/EC
65openssl genpkey -algorithm rsa > $RSA
66openssl pkcs8 -nocrypt -in $RSA |\
67 softhsm2-util --slot "$slot" --label 01 --id 01 --pin "$TEST_SSH_PIN" --import /dev/stdin
68openssl genpkey \
69 -genparam \
70 -algorithm ec \
71 -pkeyopt ec_paramgen_curve:prime256v1 |\
72 openssl genpkey \
73 -paramfile /dev/stdin > $EC
74openssl pkcs8 -nocrypt -in $EC |\
75 softhsm2-util --slot "$slot" --label 02 --id 02 --pin "$TEST_SSH_PIN" --import /dev/stdin
76
25trace "start agent" 77trace "start agent"
26eval `${SSHAGENT} -s` > /dev/null 78eval `${SSHAGENT} -s` > /dev/null
27r=$? 79r=$?
28if [ $r -ne 0 ]; then 80if [ $r -ne 0 ]; then
29 fail "could not start ssh-agent: exit code $r" 81 fail "could not start ssh-agent: exit code $r"
30else 82else
31 trace "generating key/cert"
32 rm -f $OBJ/pkcs11.key $OBJ/pkcs11.crt
33 openssl genrsa -out $OBJ/pkcs11.key 2048 > /dev/null 2>&1
34 chmod 600 $OBJ/pkcs11.key
35 openssl req -key $OBJ/pkcs11.key -new -x509 \
36 -out $OBJ/pkcs11.crt -text -subj '/CN=pkcs11 test' > /dev/null
37 printf "a\ta\t$OBJ/pkcs11.crt\t$OBJ/pkcs11.key" > $SOFTPKCS11RC
38 # add to authorized keys
39 ${SSHKEYGEN} -y -f $OBJ/pkcs11.key > $OBJ/authorized_keys_$USER
40
41 trace "add pkcs11 key to agent" 83 trace "add pkcs11 key to agent"
42 echo ${TEST_SSH_PIN} | notty ${SSHADD} -s ${TEST_SSH_PKCS11} > /dev/null 2>&1 84 echo ${TEST_SSH_PIN} | notty ${SSHADD} -s ${TEST_SSH_PKCS11} > /dev/null 2>&1
43 r=$? 85 r=$?
@@ -52,12 +94,23 @@ else
52 fail "ssh-add -l failed: exit code $r" 94 fail "ssh-add -l failed: exit code $r"
53 fi 95 fi
54 96
55 trace "pkcs11 connect via agent" 97 for k in $RSA $EC; do
56 ${SSH} -F $OBJ/ssh_proxy somehost exit 5 98 trace "testing $k"
57 r=$? 99 chmod 600 $k
58 if [ $r -ne 5 ]; then 100 ssh-keygen -y -f $k > $k.pub
59 fail "ssh connect failed (exit code $r)" 101 pub=$(cat $k.pub)
60 fi 102 ${SSHADD} -L | grep -q "$pub" || fail "key $k missing in ssh-add -L"
103 ${SSHADD} -T $k.pub || fail "ssh-add -T with $k failed"
104
105 # add to authorized keys
106 cat $k.pub > $OBJ/authorized_keys_$USER
107 trace "pkcs11 connect via agent ($k)"
108 ${SSH} -F $OBJ/ssh_proxy somehost exit 5
109 r=$?
110 if [ $r -ne 5 ]; then
111 fail "ssh connect failed (exit code $r)"
112 fi
113 done
61 114
62 trace "remove pkcs11 keys" 115 trace "remove pkcs11 keys"
63 echo ${TEST_SSH_PIN} | notty ${SSHADD} -e ${TEST_SSH_PKCS11} > /dev/null 2>&1 116 echo ${TEST_SSH_PIN} | notty ${SSHADD} -e ${TEST_SSH_PKCS11} > /dev/null 2>&1