2 lappend auto_path [file dirname [info script]]
3 package require ossltest
6 if [file exists tsa.serial] {
9 set testname [file rootname [file tail $::argv0]]
12 # Formats 64 hex digits into format
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 "-"]
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]
34 # Chop newline from last line
35 return [string trim $out "\n"]
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
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]]
53 start_tests "Тесты на timestamping protocol"
55 test "Creating CA 2001" {
56 makeCA ${testname}CA gost2001:A
59 test "Creating CA 2012" {
65 if {[info exists env(ALG_LIST)]} {
66 set alg_list $env(ALG_LIST)
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}}
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]
79 gost2012* {set ::test::ca ${testname}CA-2012}
80 * {set ::test::ca ${testname}CA}
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"
89 test "creating TSA configuration file" {
90 makeFile tsa.cnf "\[ tsa \]
91 default_tsa = 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
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"
107 test "Printing request 1" {
108 cleanup_print [openssl "ts -query -text -in req1.tsq"]
110 Hash Algorithm: [hash_long_name $hash_alg]
113 Policy OID: unspecified
114 Nonce: 0xXXXXXXXXXXXXXXXX
115 Certificate required: no
119 test "Creating reply for request1" {
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
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:
129 Status description: unspecified
130 Failure info: unspecified
135 Hash Algorithm: [hash_long_name $hash_alg]
138 Serial number: 0x[format "%02X" $serial_num]
139 Time stamp: extracted
140 Accuracy: unspecified
142 Nonce: 0xXXXXXXXXXXXXXXXX
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"
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"
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"
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"]
165 } 0 "Verification: OK\n"
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"
172 test "Printing request 2" {
173 cleanup_print [openssl "ts -query -text -in req2.tsq"]
175 Hash Algorithm: [hash_long_name $hash_alg]
178 Policy OID: unspecified
179 Nonce: 0xXXXXXXXXXXXXXXXX
180 Certificate required: yes
184 test "Replying to request 2" {
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
189 set creation_time2 $::test::timestamp
191 test -time 1 "Printing reply 2" {
192 cleanup_print [openssl "ts -reply -text -in resp2.tsr"]
193 } 0 [list "Status info:
195 Status description: unspecified
196 Failure info: unspecified
201 Hash Algorithm: [hash_long_name $hash_alg]
204 Serial number: 0x[format "%02X" $serial_num]
205 Time stamp: extracted
206 Accuracy: unspecified
208 Nonce: 0xXXXXXXXXXXXXXXXX
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"
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"
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"
225 test "Verifying reply2 against request 1" {
226 grep "Verification" [openssl "ts -verify -in resp2.tsr -queryfile req1.tsq -CAfile $::test::ca/cacert.pem"]
229 test "Creating request 3 with sha1 digest" {
230 openssl "ts -query -sha1 -data testdata -cert -out req3.tsq"
235 test "Printing request 3" {
236 cleanup_print [openssl "ts -query -text -in req3.tsq"]
240 [format_hash $sha1hash1]
241 Policy OID: unspecified
242 Nonce: 0xXXXXXXXXXXXXXXXX
243 Certificate required: yes
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
253 test "Printing reply 3" {
254 cleanup_print [openssl "ts -reply -text -in resp3.tsr"]
257 Status description: Message digest algorithm is not supported.
258 Failure info: unrecognized or unsupported algorithm identifier
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"
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"
275 test "Replying to request 4" {
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
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"
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"
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
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"