c6a381881becfe5b188446a34ae50ff3ce81fceb
[gd/gnutls] / tests / destructive / p11-kit-load.sh
1 #!/bin/sh
2
3 # Copyright (C) 2017 Red Hat, Inc.
4 #
5 # This file is part of p11-kit.
6 #
7 # p11-kit is free software; you can redistribute it and/or modify it
8 # under the terms of the GNU General Public License as published by the
9 # Free Software Foundation; either version 3 of the License, or (at
10 # your option) any later version.
11 #
12 # p11-kit is distributed in the hope that it will be useful, but
13 # WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15 # General Public License for more details.
16 #
17 # You should have received a copy of the GNU Lesser General Public License
18 # along with this program.  If not, see <https://www.gnu.org/licenses/>
19
20 #set -e
21
22 srcdir="${srcdir:-.}"
23 builddir="${builddir:-.}"
24 P11TOOL="${P11TOOL:-../src/p11tool${EXEEXT}}"
25 CERTTOOL="${CERTTOOL:-../src/certtool${EXEEXT}}"
26 DIFF="${DIFF:-diff}"
27 PKGCONFIG="${PKG_CONFIG:-$(which pkg-config)}"
28 TMP_SOFTHSM_DIR="./softhsm-load.$$.tmp"
29 P11DIR="p11-kit-conf.$$.tmp"
30 PIN=1234
31 PUK=1234
32
33 for lib in ${libdir} ${libdir}/pkcs11 /usr/lib64/pkcs11/ /usr/lib/pkcs11/ /usr/lib/x86_64-linux-gnu/pkcs11/;do
34         if test -f "${lib}/p11-kit-trust.so"; then
35                 TRUST_MODULE="${lib}/p11-kit-trust.so"
36                 echo "located ${MODULE}"
37                 break
38         fi
39 done
40
41 for lib in ${libdir} ${libdir}/pkcs11 /usr/lib64/pkcs11/ /usr/lib/pkcs11/ /usr/lib/x86_64-linux-gnu/pkcs11/ /usr/lib/softhsm/;do
42         if test -f "${lib}/libsofthsm2.so"; then
43                 SOFTHSM_MODULE="${lib}/libsofthsm2.so"
44                 echo "located ${MODULE}"
45                 break
46         fi
47 done
48
49 ${PKGCONFIG} --version >/dev/null || exit 77
50
51 if ! test -x "${P11TOOL}"; then
52         echo "p11tool was not found"
53         exit 77
54 fi
55
56 if ! test -f "${TRUST_MODULE}"; then
57         echo "p11-kit trust module was not found"
58         exit 77
59 fi
60
61 if ! test -f "${SOFTHSM_MODULE}"; then
62         echo "softhsm module was not found"
63         exit 77
64 fi
65
66 # Create pkcs11.conf with two modules, a trusted (p11-kit-trust)
67 # and softhsm (not trusted)
68 mkdir -p ${P11DIR}
69
70 cat <<_EOF_ >${P11DIR}/p11-kit-trust.module
71 module: p11-kit-trust.so
72 trust-policy: yes
73 _EOF_
74
75 cat <<_EOF_ >${P11DIR}/softhsm.module
76 module: libsofthsm2.so
77 _EOF_
78
79 # Setup softhsm
80 rm -rf ${TMP_SOFTHSM_DIR}
81 mkdir -p ${TMP_SOFTHSM_DIR}
82 SOFTHSM2_CONF=${TMP_SOFTHSM_DIR}/conf
83 export SOFTHSM2_CONF
84 echo "objectstore.backend = file" > "${SOFTHSM2_CONF}"
85 echo "directories.tokendir = ${TMP_SOFTHSM_DIR}" >> "${SOFTHSM2_CONF}"
86
87 softhsm2-util --init-token --slot 0 --label "GnuTLS-Test" --so-pin "${PUK}" --pin "${PIN}" >/dev/null #2>&1
88 if test $? != 0; then
89         echo "failed to initialize softhsm"
90         exit 1
91 fi
92
93 FILTERTOKEN="sed s/token=.*//g"
94
95 # Check whether both are listed
96
97 nr=$(${builddir}/pkcs11/list-tokens -o ${P11DIR} -a|${FILTERTOKEN}|sort -u|wc -l)
98 #nr=$(${P11TOOL} --list-tokens|grep 'Module:'|sort -u|wc -l)
99 if test "$nr" != 2;then
100         echo "Error: did not find 2 modules ($nr)"
101         ${builddir}/pkcs11/list-tokens -o ${P11DIR}
102         exit 1
103 fi
104
105 ## Check whether p11tool with a specific provider would list only that
106 ## That is, check whether p11tool will list the trust module
107 ## if we only load softhsm (it should as trust modules
108 ## are always loaded).ould list them both
109
110
111 #nr=$(${P11TOOL} --provider "${SOFTHSM_MODULE}" --list-tokens|grep -c ^Token)
112 nr=$(${builddir}/pkcs11/list-tokens -o ${P11DIR} -m -s "${SOFTHSM_MODULE}"|${FILTERTOKEN}|sort -u|wc -l)
113 if test "$nr" != 1;then
114         echo "Error: did not find softhsm modules"
115         ${builddir}/pkcs11/list-tokens -o ${P11DIR} -m -s "${SOFTHSM_MODULE}"
116         exit 1
117 fi
118
119 # Check whether both modules are found when gnutls_pkcs11_init
120 # is not called but a pkcs11 operation is called.
121 nr=$(${builddir}/pkcs11/list-tokens -o ${P11DIR} -d|${FILTERTOKEN}|sort -u|wc -l)
122 if test "$nr" != 2;then
123         echo "Error in test 1: did not find 2 modules"
124         ${builddir}/pkcs11/list-tokens -o ${P11DIR} -d
125         exit 1
126 fi
127
128 # Check whether both modules are found when gnutls_pkcs11_init 
129 # is called with the auto flag
130 nr=$(${builddir}/pkcs11/list-tokens -o ${P11DIR} -a|${FILTERTOKEN}|sort -u|wc -l)
131 if test "$nr" != 2;then
132         echo "Error in test 2: did not find 2 modules"
133         ${builddir}/pkcs11/list-tokens -o ${P11DIR} -a
134         exit 1
135 fi
136
137 # Check whether only trusted modules are listed when the
138 # trusted flag is given to gnutls_pkcs11_init().
139 nr=$(${builddir}/pkcs11/list-tokens -o ${P11DIR} -t|${FILTERTOKEN}|sort -u|wc -l)
140 if test "$nr" != 1;then
141         echo "Error in test 3: did not find the trusted module"
142         ${builddir}/pkcs11/list-tokens -o ${P11DIR} -t
143         exit 1
144 fi
145
146 # Check whether only trusted is listed after certificate verification
147 # is performed.
148 nr=$(${builddir}/pkcs11/list-tokens -o ${P11DIR} -v|${FILTERTOKEN}|sort -u|wc -l)
149 if test "$nr" != 1;then
150         echo "Error in test 4: did not find 1 module"
151         echo xxx
152         GNUTLS_DEBUG_LEVEL=4 P11_KIT_DEBUG=all ${builddir}/pkcs11/list-tokens -o ${P11DIR} -v
153         exit 1
154 fi
155
156 # Check whether only trusted is listed when gnutls_pkcs11_init
157 # is called with manual flag and a certificate verification is performed.
158 nr=$(${builddir}/pkcs11/list-tokens -o ${P11DIR} -m -v|${FILTERTOKEN}|sort -u|wc -l)
159 if test "$nr" != 1;then
160         echo "Error in test 5: did not find 1 module"
161         ${builddir}/pkcs11/list-tokens -o ${P11DIR} -m -v
162         exit 1
163 fi
164
165 # Check whether all modules are listed after certificate verification
166 # is performed then a PKCS#11 function is called.
167 nr=$(${builddir}/pkcs11/list-tokens -o ${P11DIR} -v -d|${FILTERTOKEN}|sort -u|wc -l)
168 if test "$nr" != 2;then
169         echo "Error in test 6: did not find all modules"
170         ${builddir}/pkcs11/list-tokens -o ${P11DIR} -v -d
171         exit 1
172 fi
173
174 # Check whether all modules are listed after a private key operation.
175 nr=$(${builddir}/pkcs11/list-tokens -o ${P11DIR} -p|${FILTERTOKEN}|sort -u|wc -l)
176 if test "$nr" != 2;then
177         echo "Error in test 7: did not find all modules"
178         ${builddir}/pkcs11/list-tokens -o ${P11DIR} -p
179         exit 1
180 fi
181
182 rm -f ${P11DIR}/*
183 rm -rf ${TMP_SOFTHSM_DIR}
184
185 exit 0