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