New pkey functions for keygen callbacks and retrieving operation type.
[openssl.git] / test / testtsa
1 #!/bin/sh
2
3 #
4 # A few very basic tests for the 'ts' time stamping authority command.
5 #
6
7 SH="/bin/sh"
8 PATH=../../apps:$PATH
9 export SH PATH
10
11 OPENSSL_CONF="../CAtsa.cnf"
12 export OPENSSL_CONF
13 # Because that's what ../apps/CA.sh really looks at
14 SSLEAY_CONFIG="-config $OPENSSL_CONF"
15 export SSLEAY_CONFIG
16
17 OPENSSL="`pwd`/../util/opensslwrap.sh"
18 export OPENSSL
19
20 error () {
21
22     echo "TSA test failed!" >&2
23     exit 1
24 }
25
26 setup_dir () {
27
28     rm -rf tsa 2>/dev/null
29     mkdir tsa
30     cd ./tsa
31 }
32
33 clean_up_dir () {
34
35     cd ..
36     rm -rf tsa
37 }
38
39 create_ca () {
40
41     echo "Creating a new CA for the TSA tests..."
42     /bin/rm -fr demoCA
43     $SH ../../apps/CA.sh -newca <<EOF
44
45 HU
46 Budapest
47 Budapest
48 Gov-CA Ltd.
49 ca1
50 EOF
51     test $? != 0 && error
52 }
53
54 create_tsa_cert () {
55
56     INDEX=$1
57     EXT=$2
58     ../../util/shlib_wrap.sh ../../apps/openssl req -new -out tsa_req${INDEX}.pem -keyout tsa_key${INDEX}.pem <<EOF
59 HU
60 Budapest
61 Buda
62 Hun-TSA Ltd.
63 tsa${INDEX}
64 EOF
65     test $? != 0 && error
66
67     ../../util/shlib_wrap.sh ../../apps/openssl ca -in tsa_req${INDEX}.pem -out tsa_cert${INDEX}.pem \
68         -extensions $EXT <<EOF
69 y
70 y
71 EOF
72     test $? != 0 && error
73 }
74
75 print_request () {
76
77     ../../util/shlib_wrap.sh ../../apps/openssl ts -query -in $1 -text
78 }
79
80 create_time_stamp_request1 () {
81
82     ../../util/shlib_wrap.sh ../../apps/openssl ts -query -data ../testtsa -policy tsa_policy1 -cert -out req1.tsq
83     test $? != 0 && error
84 }
85
86 create_time_stamp_request2 () {
87
88     ../../util/shlib_wrap.sh ../../apps/openssl ts -query -data ../testtsa -policy tsa_policy2 -no_nonce \
89         -out req2.tsq
90     test $? != 0 && error
91 }
92
93 create_time_stamp_request3 () {
94
95     ../../util/shlib_wrap.sh ../../apps/openssl ts -query -data ../CAtsa.cnf -no_nonce -out req3.tsq
96     test $? != 0 && error
97 }
98
99 print_response () {
100
101     ../../util/shlib_wrap.sh ../../apps/openssl ts -reply -in $1 -text
102     test $? != 0 && error
103 }
104
105 create_time_stamp_response () {
106
107     ../../util/shlib_wrap.sh ../../apps/openssl ts -reply -section $3 -queryfile $1 -out $2
108     test $? != 0 && error
109 }
110
111 time_stamp_response_token_test () {
112
113     RESPONSE2=$2.copy.tsr
114     TOKEN_DER=$2.token.der
115     ../../util/shlib_wrap.sh ../../apps/openssl ts -reply -in $2 -out $TOKEN_DER -token_out
116     test $? != 0 && error
117     ../../util/shlib_wrap.sh ../../apps/openssl ts -reply -in $TOKEN_DER -token_in -out $RESPONSE2
118     test $? != 0 && error
119     cmp $RESPONSE2 $2
120     test $? != 0 && error
121     ../../util/shlib_wrap.sh ../../apps/openssl ts -reply -in $2 -text -token_out
122     test $? != 0 && error
123     ../../util/shlib_wrap.sh ../../apps/openssl ts -reply -in $TOKEN_DER -token_in -text -token_out
124     test $? != 0 && error
125     ../../util/shlib_wrap.sh ../../apps/openssl ts -reply -queryfile $1 -text -token_out
126     test $? != 0 && error
127 }
128
129 verify_time_stamp_response () {
130
131     ../../util/shlib_wrap.sh ../../apps/openssl ts -verify -queryfile $1 -in $2 -CAfile demoCA/cacert.pem \
132         -untrusted tsa_cert1.pem
133     test $? != 0 && error
134     ../../util/shlib_wrap.sh ../../apps/openssl ts -verify -data $3 -in $2 -CAfile demoCA/cacert.pem \
135         -untrusted tsa_cert1.pem
136     test $? != 0 && error
137 }
138
139 verify_time_stamp_token () {
140
141     # create the token from the response first
142     ../../util/shlib_wrap.sh ../../apps/openssl ts -reply -in $2 -out $2.token -token_out
143     test $? != 0 && error
144     ../../util/shlib_wrap.sh ../../apps/openssl ts -verify -queryfile $1 -in $2.token -token_in \
145         -CAfile demoCA/cacert.pem -untrusted tsa_cert1.pem
146     test $? != 0 && error
147     ../../util/shlib_wrap.sh ../../apps/openssl ts -verify -data $3 -in $2.token -token_in \
148         -CAfile demoCA/cacert.pem -untrusted tsa_cert1.pem
149     test $? != 0 && error
150 }
151
152 verify_time_stamp_response_fail () {
153
154     ../../util/shlib_wrap.sh ../../apps/openssl ts -verify -queryfile $1 -in $2 -CAfile demoCA/cacert.pem \
155         -untrusted tsa_cert1.pem
156     # Checks if the verification failed, as it should have.
157     test $? == 0 && error
158     echo Ok
159 }
160
161 # main functions
162
163 echo "Setting up TSA test directory..."
164 setup_dir
165
166 echo "Creating CA for TSA tests..."
167 create_ca
168
169 echo "Creating tsa_cert1.pem TSA server cert..."
170 create_tsa_cert 1 tsa_cert
171
172 echo "Creating tsa_cert2.pem non-TSA server cert..."
173 create_tsa_cert 2 non_tsa_cert
174
175 echo "Creating req1.req time stamp request for file testtsa..."
176 create_time_stamp_request1
177
178 echo "Printing req1.req..."
179 print_request req1.tsq
180
181 echo "Generating valid response for req1.req..."
182 create_time_stamp_response req1.tsq resp1.tsr tsa_config1
183
184 echo "Printing response..."
185 print_response resp1.tsr
186
187 echo "Verifying valid response..."
188 verify_time_stamp_response req1.tsq resp1.tsr ../testtsa
189
190 echo "Verifying valid token..."
191 verify_time_stamp_token req1.tsq resp1.tsr ../testtsa
192
193 # The tests below are commented out, because invalid signer certificates
194 # can no longer be specified in the config file.
195
196 # echo "Generating _invalid_ response for req1.req..."
197 # create_time_stamp_response req1.tsq resp1_bad.tsr tsa_config2
198
199 # echo "Printing response..."
200 # print_response resp1_bad.tsr
201
202 # echo "Verifying invalid response, it should fail..."
203 # verify_time_stamp_response_fail req1.tsq resp1_bad.tsr
204
205 echo "Creating req2.req time stamp request for file testtsa..."
206 create_time_stamp_request2
207
208 echo "Printing req2.req..."
209 print_request req2.tsq
210
211 echo "Generating valid response for req2.req..."
212 create_time_stamp_response req2.tsq resp2.tsr tsa_config1
213
214 echo "Checking '-token_in' and '-token_out' options with '-reply'..."
215 time_stamp_response_token_test req2.tsq resp2.tsr
216
217 echo "Printing response..."
218 print_response resp2.tsr
219
220 echo "Verifying valid response..."
221 verify_time_stamp_response req2.tsq resp2.tsr ../testtsa
222
223 echo "Verifying response against wrong request, it should fail..."
224 verify_time_stamp_response_fail req1.tsq resp2.tsr
225
226 echo "Verifying response against wrong request, it should fail..."
227 verify_time_stamp_response_fail req2.tsq resp1.tsr
228
229 echo "Creating req3.req time stamp request for file CAtsa.cnf..."
230 create_time_stamp_request3
231
232 echo "Printing req3.req..."
233 print_request req3.tsq
234
235 echo "Verifying response against wrong request, it should fail..."
236 verify_time_stamp_response_fail req3.tsq resp1.tsr
237
238 echo "Cleaning up..."
239 clean_up_dir
240
241 exit 0