Debian Codegraph @

testcases/policy/policytest.sh.in

raw
1 #!/bin/bash
2
3 # Get script directory as source directory for all other files
4 SOURCE="${BASH_SOURCE[0]}"
5 while [ -h "$SOURCE" ]; do # resolve $SOURCE until the file is no longer a symlink
6 DIR="$( cd -P "$( dirname "$SOURCE" )" >/dev/null 2>&1 && pwd )"
7 SOURCE="$(readlink "$SOURCE")"
8 [[ $SOURCE != /* ]] && SOURCE="$DIR/$SOURCE" # if $SOURCE was a relative symlink, we need to resolve it relative to the path where the symlink file was located
9 done
10 DIR="$( cd -P "$( dirname "$SOURCE" )" >/dev/null 2>&1 && pwd )"
11
12 restoreconfig() {
13 if test -e @sysconfdir@/opencryptoki/strength.conf.ptbak; then
14 rm -f @sysconfdir@/opencryptoki/strength.conf
15 mv @sysconfdir@/opencryptoki/strength.conf.ptbak @sysconfdir@/opencryptoki/strength.conf
16 chown root:@pkcs_group@ @sysconfdir@/opencryptoki/strength.conf
17 chmod 0640 @sysconfdir@/opencryptoki/strength.conf
18 fi
19 if test -e @sysconfdir@/opencryptoki/policy.conf.ptbak; then
20 rm -f @sysconfdir@/opencryptoki/policy.conf
21 mv @sysconfdir@/opencryptoki/policy.conf.ptbak @sysconfdir@/opencryptoki/policy.conf
22 chown root:@pkcs_group@ @sysconfdir@/opencryptoki/policy.conf
23 chmod 0640 @sysconfdir@/opencryptoki/policy.conf
24 fi
25 }
26
27 backupconfig() {
28 if test -e @sysconfdir@/opencryptoki/strength.conf; then
29 mv @sysconfdir@/opencryptoki/strength.conf @sysconfdir@/opencryptoki/strength.conf.ptbak
30 fi
31 if test -e @sysconfdir@/opencryptoki/policy.conf; then
32 mv @sysconfdir@/opencryptoki/policy.conf @sysconfdir@/opencryptoki/policy.conf.ptbak
33 fi
34 trap restoreconfig EXIT
35 }
36
37 initstrength() {
38 cat <<EOF > @sysconfdir@/opencryptoki/strength.conf
39 version strength-0
40
41 strength 112 {
42 MOD_EXP = 2048
43 ECC = 224
44 SYMMETRIC = 112
45 digest = 224
46 signature = 112
47 }
48 strength 128 {
49 MOD_EXP = 3072
50 ECC = 256
51 SYMMETRIC = 128
52 digest = 256
53 signature = 128
54 }
55 strength 192 {
56 MOD_EXP = 7680
57 ECC = 384
58 SYMMETRIC = 192
59 digest = 384
60 signature = 192
61 }
62 strength 256 {
63 MOD_EXP = 15360
64 ECC = 512
65 SYMMETRIC = 256
66 digest = 512
67 signature = 256
68 }
69 EOF
70 chown root:@pkcs_group@ @sysconfdir@/opencryptoki/strength.conf
71 chmod 0640 @sysconfdir@/opencryptoki/strength.conf
72 }
73
74 # Usage: genpolicy <strengthvalue>
75 genpolicy() {
76 cat <<EOF > @sysconfdir@/opencryptoki/policy.conf
77 version policy-0
78 strength = $1
79 EOF
80 chown root:@pkcs_group@ @sysconfdir@/opencryptoki/policy.conf
81 chmod 0640 @sysconfdir@/opencryptoki/policy.conf
82 }
83
84 if test $# -ne 1; then
85 echo "USAGE: policytest <slotid>"
86 exit 1;
87 fi
88
89 SLOT=$1
90 FAILED=/bin/false
91
92 # basic initialization
93 backupconfig
94 initstrength
95
96 # start with strength of 0
97 genpolicy 0
98
99 # check which tests we can run
100 TESTS=""
101 SEP=""
102
103 if pkcsconf -m -c $SLOT | grep -q "(CKM_RSA_PKCS)"; then
104 TESTS="${TESTS}${SEP}RSA"
105 SEP=","
106 fi
107 if pkcsconf -m -c $SLOT | grep -q "(CKM_ECDSA)"; then
108 TESTS="${TESTS}${SEP}EC"
109 SEP=","
110 fi
111 if pkcsconf -m -c $SLOT | grep -q "(CKM_AES_CBC_PAD)" &&\
112 pkcsconf -m -c $SLOT | grep -A 2 "(CKM_AES_CBC_PAD)" | grep -q CKF_WRAP; then
113 TESTS="${TESTS}${SEP}AESWRAP"
114 SEP=","
115 fi
116 if pkcsconf -m -c $SLOT | grep -q "(CKM_SHA512)"; then
117 TESTS="${TESTS}${SEP}DIGEST"
118 SEP=","
119 fi
120 if pkcsconf -m -c $SLOT | grep -q "(CKM_DH_PKCS_DERIVE)"; then
121 TESTS="${TESTS}${SEP}DH"
122 SEP=","
123 fi
124
125 if test "x$TESTS" == "x"; then
126 echo "Cannot run any tests on this slot"
127 exit 0
128 fi
129
130 echo "Using test mask $TESTS"
131
132 echo "Running tests with session keys..."
133
134 ${DIR}/policytest -s $SLOT -g -t -d -r $TESTS || {
135 echo "Failed with session keys for strength 0"
136 FAILED=/bin/true
137 }
138
139 genpolicy 112
140
141 ${DIR}/policytest -s $SLOT -g -t -d -r $TESTS -f DH || {
142 echo "Failed with session keys for strength 112"
143 FAILED=/bin/true
144 }
145
146 genpolicy 128
147
148 ${DIR}/policytest -s $SLOT -g -t -d -r $TESTS -f DH || {
149 echo "Failed with session keys for strength 128"
150 FAILED=/bin/true
151 }
152
153 genpolicy 192
154
155 ${DIR}/policytest -s $SLOT -g -t -d -r $TESTS -f DH,RSA || {
156 echo "Failed with session keys for strength 192"
157 FAILED=/bin/true
158 }
159
160 genpolicy 256
161
162 ${DIR}/policytest -s $SLOT -g -t -d -r $TESTS -f DH,RSA,EC || {
163 echo "Failed with session keys for strength 256"
164 FAILED=/bin/true
165 }
166
167 echo "...now generating token keys..."
168
169 genpolicy 0
170
171 ${DIR}/policytest -s $SLOT -g -k || {
172 echo "Failed to generate token keys"
173 exit 1
174 }
175
176 echo "...now running tests with token keys..."
177
178 ${DIR}/policytest -s $SLOT -t -r $TESTS || {
179 echo "Failed with token keys from strength 0"
180 FAILED=/bin/true
181 }
182
183 genpolicy 112
184
185 ${DIR}/policytest -s $SLOT -t -r $TESTS -f DH || {
186 echo "Failed with token keys from strength 112"
187 FAILED=/bin/true
188 }
189
190 genpolicy 128
191
192 ${DIR}/policytest -s $SLOT -t -r $TESTS -f DH || {
193 echo "Failed with token keys from strength 128"
194 FAILED=/bin/true
195 }
196
197 genpolicy 192
198
199 ${DIR}/policytest -s $SLOT -t -r $TESTS -f DH,RSA || {
200 echo "Failed with token keys from strength 192"
201 FAILED=/bin/true
202 }
203
204 genpolicy 256
205
206 ${DIR}/policytest -s $SLOT -t -r $TESTS -f DH,RSA,EC || {
207 echo "Failed with token keys from strength 256"
208 FAILED=/bin/true
209 }
210
211 echo "...now deleting token keys"
212
213 genpolicy 0
214 ${DIR}/policytest -s $SLOT -d || echo "Could not delete all generated tokens keys"
215
216 if $FAILED; then
217 echo "Some tests have failed"
218 exit 1
219 fi
220
221 exit 0