2 # -*- coding: cp1251 -*-
3 lappend auto_path [file dirname [info script]]
4 package require ossltest
7 if [file exists tsa.serial] {
10 set testname [file rootname [file tail $::argv0]]
13 # Formats 64 hex digits into format
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 "-"]
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]
35 # Chop newline from last line
36 return [string trim $out "\n"]
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
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]]
54 start_tests "Тесты на timestamping protocol"
56 test "Creating CA 2001" {
57 makeCA ${testname}CA gost2001:A
60 test "Creating CA 2012" {
66 if {[info exists env(ALG_LIST)]} {
67 set alg_list $env(ALG_LIST)
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}}
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]
80 gost2012* {set ::test::ca ${testname}CA-2012}
81 * {set ::test::ca ${testname}CA}
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"
90 test "creating TSA configuration file" {
91 makeFile tsa.cnf "\[ tsa \]
92 default_tsa = 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
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"
108 test "Printing request 1" {
109 cleanup_print [openssl "ts -query -text -in req1.tsq"]
111 Hash Algorithm: [hash_long_name $hash_alg]
114 Policy OID: unspecified
115 Nonce: 0xXXXXXXXXXXXXXXXX
116 Certificate required: no
120 test "Creating reply for request1" {
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
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:
130 Status description: unspecified
131 Failure info: unspecified
136 Hash Algorithm: [hash_long_name $hash_alg]
139 Serial number: 0x[format "%02X" $serial_num]
140 Time stamp: extracted
141 Accuracy: unspecified
143 Nonce: 0xXXXXXXXXXXXXXXXX
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"
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"
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"
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"]
166 } 0 "Verification: OK\n"
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"
173 test "Printing request 2" {
174 cleanup_print [openssl "ts -query -text -in req2.tsq"]
176 Hash Algorithm: [hash_long_name $hash_alg]
179 Policy OID: unspecified
180 Nonce: 0xXXXXXXXXXXXXXXXX
181 Certificate required: yes
185 test "Replying to request 2" {
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
190 set creation_time2 $::test::timestamp
192 test -time 1 "Printing reply 2" {
193 cleanup_print [openssl "ts -reply -text -in resp2.tsr"]
194 } 0 [list "Status info:
196 Status description: unspecified
197 Failure info: unspecified
202 Hash Algorithm: [hash_long_name $hash_alg]
205 Serial number: 0x[format "%02X" $serial_num]
206 Time stamp: extracted
207 Accuracy: unspecified
209 Nonce: 0xXXXXXXXXXXXXXXXX
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"
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"
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"
226 test "Verifying reply2 against request 1" {
227 grep "Verification" [openssl "ts -verify -in resp2.tsr -queryfile req1.tsq -CAfile $::test::ca/cacert.pem"]
230 test "Creating request 3 with sha1 digest" {
231 openssl "ts -query -sha1 -data testdata -cert -out req3.tsq"
236 test "Printing request 3" {
237 cleanup_print [openssl "ts -query -text -in req3.tsq"]
241 [format_hash $sha1hash1]
242 Policy OID: unspecified
243 Nonce: 0xXXXXXXXXXXXXXXXX
244 Certificate required: yes
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
254 test "Printing reply 3" {
255 cleanup_print [openssl "ts -reply -text -in resp3.tsr"]
258 Status description: Message digest algorithm is not supported.
259 Failure info: unrecognized or unsupported algorithm identifier
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"
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"
276 test "Replying to request 4" {
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
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"
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"
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
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"