aboutsummaryrefslogtreecommitdiff
path: root/tests/pkcs11/setup-softhsm-tokens.sh
blob: 532c86a706c14d3a3d0ee8bcc3e772bb22ba33ef (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
#!/bin/bash

# The client keys are stored in a SoftHSM device.

TESTDIR=$1
PRIVKEY=$2
OBJNAME=$3
LOADPUBLIC=$4
shift 4

PUBKEY="$PRIVKEY.pub"

echo "TESTDIR: $TESTDIR"
echo "PRIVKEY: $PRIVKEY"
echo "PUBKEY: $PUBKEY"
echo "OBJNAME: $OBJNAME"
echo "LOADPUBLIC: $LOADPUBLIC"

# Create temporary directory for tokens
install -d -m 0755 $TESTDIR/db

# Create SoftHSM configuration file
cat >$TESTDIR/softhsm.conf <<EOF
directories.tokendir = $TESTDIR/db
objectstore.backend = file
log.level = DEBUG
EOF

export SOFTHSM2_CONF=$TESTDIR/softhsm.conf

cat $TESTDIR/softhsm.conf

#init
cmd='softhsm2-util --init-token --label "$OBJNAME" --free --pin 1234 --so-pin 1234'
eval echo "$cmd"
out=$(eval $cmd)
ret=$?
if [ $ret -ne 0 ]; then
    echo "Init token failed"
    echo "$out"
    exit 1
fi

#load private key
cmd='p11tool --provider /usr/lib64/pkcs11/libsofthsm2.so --write --load-privkey "$PRIVKEY" --label "$OBJNAME" --login --set-pin=1234 "pkcs11:token="$OBJNAME""'
eval echo "$cmd"
out=$(eval $cmd)
ret=$?
if [ $ret -ne 0 ]; then
   echo "Loading privkey failed"
   echo "$out"
   exit 1
fi

cat $PUBKEY

ls -l $TESTDIR

if [ $LOADPUBLIC -ne 0 ]; then
#load public key
    cmd='p11tool --provider /usr/lib64/pkcs11/libsofthsm2.so --write --load-pubkey "$PUBKEY" --label "$OBJNAME" --login --set-pin=1234 "pkcs11:token="$OBJNAME""'
    eval echo "$cmd"
    out=$(eval $cmd)
    ret=$?
    if [ $ret -ne 0 ]; then
        echo "Loading pubkey failed"
        echo "$out"
        exit 1
    fi
fi

cmd='p11tool --list-all --login "pkcs11:token="$OBJNAME"" --set-pin=1234'
eval echo "$cmd"
out=$(eval $cmd)
ret=$?
if [ $ret -ne 0 ]; then
    echo "Loging failed"
    echo "$out"
    exit 1
fi
echo "$out"

exit 0