]> www.wagner.pp.ru Git - openssl-gost/engine.git/blob - tcl_tests/ts.try
tcl_tests: ca.try: Ignore openssl crl exit status for 'corrupted CRL' test
[openssl-gost/engine.git] / tcl_tests / ts.try
1 #!/usr/bin/tclsh
2 lappend auto_path [file dirname [info script]]
3 package require ossltest
4 cd $::test::dir
5 set env(LC_TIME) C
6 if [file exists tsa.serial] {
7         file delete tsa.serial
8 }
9 set testname [file rootname [file tail $::argv0]]
10
11 #
12 # Formats 64 hex digits into format 
13 #
14 proc format_hash {hash} {
15         # Split hash into list of two-digit hexadecimal numbers
16         set list [regexp -all -inline {[[:xdigit:]]{2}} $hash]
17         for {set i 0} {$i<[llength $list]} {incr i 16} {
18                 append out [format "    %04x - " $i] 
19                 set hex [join [lrange $list $i [expr $i+15]] " "]
20                 if {[string length $hex]>24} {
21                         set hex [string replace  $hex 23 23 "-"] 
22                 }       
23                 append out $hex [string repeat " " [expr 50-[string length $hex]]]
24                 foreach char [lrange $list $i [expr $i+15]] {
25                     set n [scan $char "%x"]
26                         if {$n>=32 && $n <=126 } {
27                                 append out [format %c $n]
28                         } else {
29                                 append out "."
30                         }
31                 }       
32                 append out "\n"
33         }       
34         # Chop newline from last line
35         return [string trim $out "\n"]
36 }
37 #
38 # Replace Nonce with equvalent number of X-es because we cannot 
39 # predict nonce for comparing output, and chop out STDERR
40 # If output contains timestamp, extract it, replace with word
41 # "extracted" and return two-element list where element 0 is modified
42 # output and element 1 is timestamp in unix time
43 #
44 proc cleanup_print {output} {
45         set data  [regsub {STDERR CONTENTS:.*$} [regsub {Nonce: 0x[[:xdigit:]]+} $output  {Nonce: 0xXXXXXXXXXXXXXXXX}] {}]
46         if {[regexp "\nTime stamp: (\[^\n\]+)\n" $data -> time]} {
47                 return [list [regsub "\nTime stamp: (\[^\n\]+)\n" $data "\nTime stamp: extracted\n"] [clock scan $time]]
48         } else {
49                 return $data
50         }       
51 }
52
53 start_tests "Тесты на timestamping protocol" 
54
55 test "Creating CA 2001" {
56         makeCA ${testname}CA gost2001:A
57 } 0 1
58
59 test "Creating CA 2012" {
60         makeCA
61 } 0 1
62
63 set serial_num 0
64
65 if {[info exists env(ALG_LIST)]} {
66         set alg_list $env(ALG_LIST)
67 } else {
68         switch -exact [engine_name] {
69                 "ccore" {set alg_list {gost2001:A gost2012_256:A gost2012_512:A}}
70                 "open" {set alg_list {gost2001:A gost2012_256:A gost2012_512:A}}
71         }
72 }
73
74 foreach alg $alg_list {
75         set alg_fn [string map {":" "_"} $alg]
76         set username U_tsa_$alg_fn
77         set hash_alg [alg_hash $alg]
78         switch -glob $alg {
79                 gost2012* {set ::test::ca ${testname}CA-2012}
80                 * {set ::test::ca ${testname}CA}
81         }
82
83 test "Creating Timestamping authority certificate" {
84         makeRegisteredUser $username $alg CN "Timestamping authority $alg" \
85         extensions "keyUsage=digitalSignature,nonRepudiation\nextendedKeyUsage=critical,timeStamping"
86         openssl "verify -CAfile $::test::ca/cacert.pem -purpose timestampsign $username/cert.pem"
87 } 0 "$username/cert.pem: OK\n"
88
89 test "creating TSA configuration file" {
90         makeFile tsa.cnf "\[ tsa \]
91 default_tsa = tsa_section
92 \[ tsa_section \]
93 serial = [pwd]/tsa.serial
94 digests=[hash_short_name $hash_alg]
95 default_policy = 1.2.3.4
96 other_policies = 1.2.3.4.5,1.2.3.4.6
97 signer_digest = md_gost94
98 "
99 } 0 ""
100 test "Creating request with [hash_short_name $hash_alg], no cert request, no policy" {
101         makeFile testdata [string repeat "Test data for timestamp\n" 100]
102         set hash1 [regsub {^[^=]+= } [openssl "dgst -[hash_short_name $hash_alg] testdata"] {}]
103         set sha1hash1 [regsub {^[^=]+= } [openssl "dgst -sha1 testdata"] {}]
104         openssl "ts -query -[hash_short_name $hash_alg] -data testdata -out req1.tsq"
105         file exists req1.tsq
106 } 0 1
107 test "Printing request 1" {
108         cleanup_print [openssl "ts -query  -text -in req1.tsq"]
109 } 0 "Version: 1
110 Hash Algorithm: [hash_long_name $hash_alg]
111 Message data:
112 [format_hash $hash1]
113 Policy OID: unspecified
114 Nonce: 0xXXXXXXXXXXXXXXXX
115 Certificate required: no
116 Extensions:
117 "
118
119 test "Creating reply for request1" {
120         incr serial_num
121         openssl "ts -reply -config tsa.cnf -queryfile req1.tsq -signer $username/cert.pem -inkey $username/seckey.pem -out resp1.tsr -[hash_short_name $hash_alg]"
122         file exists resp1.tsr
123 } 0 1
124         set creation_time1 $::test::timestamp 
125 test -time 1  "printing reply 1" {
126         cleanup_print [openssl "ts -reply -text -in resp1.tsr"]
127 } 0 [list  "Status info:
128 Status: Granted.
129 Status description: unspecified
130 Failure info: unspecified
131
132 TST info:
133 Version: 1
134 Policy OID: 1.2.3.4
135 Hash Algorithm: [hash_long_name $hash_alg]
136 Message data:
137 [format_hash $hash1]
138 Serial number: 0x[format "%02X" $serial_num]
139 Time stamp: extracted
140 Accuracy: unspecified
141 Ordering: no
142 Nonce: 0xXXXXXXXXXXXXXXXX
143 TSA: unspecified
144 Extensions:
145 " $creation_time1]
146
147 test "Verifying reply against query file" {
148         grep "Verification" [openssl "ts -verify -in resp1.tsr -queryfile req1.tsq -untrusted $username/cert.pem -CAfile $::test::ca/cacert.pem"]
149 } 0 "Verification: OK\n"
150
151 test "Verifying reply against data file" {
152         grep "Verification" [openssl "ts -verify -in resp1.tsr -data testdata -untrusted $username/cert.pem -CAfile $::test::ca/cacert.pem"]
153 } 0 "Verification: OK\n"
154
155
156 test "Verifying reply against other data file" {
157         makeFile testdata2 [string repeat "Test data for timestamp 2\n" 100]
158         set hash2 [regsub {^[^=]+= } [openssl "dgst -[hash_short_name $hash_alg] testdata2"] {}]
159         openssl "ts -verify -in resp1.tsr -data testdata2 -untrusted $username/cert.pem -CAfile $::test::ca/cacert.pem"
160 } 1 "message imprint mismatch"
161
162 test "Verifying reply against explicit hash value" {
163         grep "Verification" [openssl "ts -verify -in resp1.tsr -digest $hash1 -untrusted $username/cert.pem -CAfile $::test::ca/cacert.pem"]
164
165 } 0 "Verification: OK\n"
166
167 test "Creating request 2 with [hash_short_name $hash_alg], cert requested, no policy" {
168         openssl "ts -query -[hash_short_name $hash_alg] -data testdata -cert -out req2.tsq"
169         file exists req2.tsq
170 } 0 1
171
172 test "Printing request 2" {
173         cleanup_print [openssl "ts -query  -text -in req2.tsq"]
174 } 0 "Version: 1
175 Hash Algorithm: [hash_long_name $hash_alg]
176 Message data:
177 [format_hash $hash1]
178 Policy OID: unspecified
179 Nonce: 0xXXXXXXXXXXXXXXXX
180 Certificate required: yes
181 Extensions:
182 "
183
184 test "Replying to request 2" {
185         incr serial_num
186         openssl "ts -reply -config tsa.cnf -queryfile req2.tsq -signer $username/cert.pem -inkey $username/seckey.pem -out resp2.tsr -[hash_short_name $hash_alg]"
187         file exists resp2.tsr
188 } 0 1
189 set creation_time2 $::test::timestamp
190
191 test -time 1 "Printing reply 2" {
192         cleanup_print [openssl "ts -reply -text -in resp2.tsr"]
193 } 0 [list "Status info:
194 Status: Granted.
195 Status description: unspecified
196 Failure info: unspecified
197
198 TST info:
199 Version: 1
200 Policy OID: 1.2.3.4
201 Hash Algorithm: [hash_long_name $hash_alg]
202 Message data:
203 [format_hash $hash1]
204 Serial number: 0x[format "%02X" $serial_num]
205 Time stamp: extracted
206 Accuracy: unspecified
207 Ordering: no
208 Nonce: 0xXXXXXXXXXXXXXXXX
209 TSA: unspecified
210 Extensions:
211 " $creation_time2]
212
213 test "Verifying reply2 against request file" {
214         grep "Verification" [openssl "ts -verify -in resp2.tsr -queryfile req2.tsq -CAfile $::test::ca/cacert.pem"]
215 } 0 "Verification: OK\n"
216
217 test "Verifying reply2 against data file" {
218         grep "Verification" [openssl "ts -verify -in resp2.tsr -data testdata -CAfile $::test::ca/cacert.pem"]
219 } 0 "Verification: OK\n"
220
221 test "Verifying reply2 against explicit digest" {
222         grep "Verification" [openssl "ts -verify -in resp2.tsr -digest $hash1 -CAfile $::test::ca/cacert.pem"]
223 } 0 "Verification: OK\n"
224
225 test "Verifying reply2 against request 1" {
226         grep "Verification" [openssl "ts -verify -in resp2.tsr -queryfile req1.tsq -CAfile $::test::ca/cacert.pem"]
227 } 1 "nonce mismatch"
228
229 test "Creating request 3 with sha1 digest" {
230         openssl "ts -query -sha1 -data testdata -cert -out req3.tsq"
231         file exists req3.tsq
232 } 0 1
233
234
235 test "Printing request 3" {
236         cleanup_print [openssl "ts -query  -text -in req3.tsq"]
237 } 0 "Version: 1
238 Hash Algorithm: sha1
239 Message data:
240 [format_hash $sha1hash1]
241 Policy OID: unspecified
242 Nonce: 0xXXXXXXXXXXXXXXXX
243 Certificate required: yes
244 Extensions:
245 "
246
247 test "Replying to request 3 (with badAlg status)" {
248         set creation_time3 [clock format [clock seconds] -gmt y -format "%B %d %H:%M:%S %Y GMT"]
249         openssl "ts -reply -config tsa.cnf -queryfile req3.tsq -signer $username/cert.pem -inkey $username/seckey.pem -out resp3.tsr -[hash_short_name $hash_alg]"
250         file exists resp3.tsr
251 } 0 1
252
253 test  "Printing reply 3" {
254         cleanup_print [openssl "ts -reply -text -in resp3.tsr"]
255 } 0 "Status info:
256 Status: Rejected.
257 Status description: Message digest algorithm is not supported.
258 Failure info: unrecognized or unsupported algorithm identifier
259
260 TST info:
261 Not included.
262 "
263
264 test "Verifying response 3 against request file" {
265         openssl "ts -verify -in resp3.tsr -queryfile req3.tsq -CAfile $::test::ca/cacert.pem"
266 } 1 "status code: rejection, status text: Message digest algorithm is not supported., failure codes: badAlg"
267
268
269
270 test "Creating request 4 with specific (valid) policy" {
271         openssl "ts -query -[hash_short_name $hash_alg] -data testdata -tspolicy 1.2.3.4.5 -cert -out req4.tsq"
272         file exists req4.tsq
273 } 0 1
274
275 test "Replying to request 4" {
276         incr serial_num
277         openssl "ts -reply -config tsa.cnf -queryfile req4.tsq -signer $username/cert.pem -inkey $username/seckey.pem -out resp4.tsr -[hash_short_name $hash_alg]"
278         file exists resp4.tsr
279 } 0 1
280
281 test "Verifying reply 4 against request file" {
282         grep "Verification" [openssl "ts -verify -in resp4.tsr -queryfile req4.tsq -CAfile $::test::ca/cacert.pem"]
283 } 0 "Verification: OK\n"
284
285
286 test "Creating request 5 with specific (invalid) policy" {
287         openssl "ts -query -[hash_short_name $hash_alg] -data testdata -tspolicy 1.2.3.4.9 -cert -out req5.tsq"
288         file exists req5.tsq
289 } 0 1
290
291 test "Replying to request 5" {
292         openssl "ts -reply -config tsa.cnf -queryfile req5.tsq -signer $username/cert.pem -inkey $username/seckey.pem -out resp5.tsr -[hash_short_name $hash_alg]"
293         file exists resp5.tsr
294 } 0 1
295
296
297 test "Verifying reply 5" {
298         openssl "ts -verify -in resp5.tsr -queryfile req5.tsq -CAfile $::test::ca/cacert.pem"
299 } 1 "status code: rejection, status text: Requested policy is not supported., failure codes: unacceptedPolicy"
300
301
302 }
303
304 end_tests