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