summaryrefslogtreecommitdiffstats
path: root/test/recipes/80-test_tsa.t
blob: 132209713f330ffc759bb5d507db1c34f058b64c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
#! /usr/bin/perl

use strict;
use warnings;

use POSIX;
use File::Spec::Functions qw/splitdir curdir catfile/;
use File::Compare;
use Test::More;
use OpenSSL::Test qw/:DEFAULT cmdstr top_file/;

setup("test_tsa");

# All these are modified inside indir further down. They need to exist
# here, however, to be available in all subroutines.
my $testtsa;
my $CAtsa;

sub create_ca {
    $ENV{TSDNSECT} = "ts_ca_dn";
    return
	ok(run(app(["openssl", "req", "-new", "-x509", "-nodes",
		    "-out", "tsaca.pem", "-keyout", "tsacakey.pem"])),
	   'creating a new CA for the TSA tests');
}

sub create_tsa_cert {
    my $INDEX = shift;
    my $EXT = shift;
    my $r = 1;
    $ENV{TSDNSECT} = "ts_ca_dn";

    $r *= ok(run(app(["openssl", "req", "-new",
		      "-out", "tsa_req${INDEX}.pem",
		      "-keyout", "tsa_key${INDEX}.pem"])));
    note "using extension $EXT";
    $r *= ok(run(app(["openssl", "x509", "-req",
		      "-in", "tsa_req${INDEX}.pem",
		      "-out", "tsa_cert${INDEX}.pem",
		      "-CA", "tsaca.pem", "-CAkey", "tsacakey.pem",
		      "-CAcreateserial",
		      "-extfile", $ENV{OPENSSL_CONF}, "-extensions", $EXT])));
    return $r;
}

sub print_request {
    my $input = shift;
    return ok(run(app(["openssl", "ts", "-query", "-in", $input, "-text"])));
}

sub create_time_stamp_request1 {
    return
	ok(run(app(["openssl", "ts", "-query", "-data", $testtsa, "-policy", "tsa_policy1", "-cert", "-out", "req1.tsq"])));
}

sub create_time_stamp_request2 {

    return
	ok(run(app(["openssl", "ts", "-query", "-data", $testtsa, "-policy", "tsa_policy2", "-no_nonce", "-out", "req2.tsq"])));
}

sub create_time_stamp_request3 {

    return
	ok(run(app(["openssl", "ts", "-query", "-data", $CAtsa, "-no_nonce", "-out", "req3.tsq"])))
}

sub print_response {
    my $inputfile = shift;

    return
	ok(run(app(["openssl", "ts", "-reply", "-in", "$inputfile", "-text"])));
}

sub create_time_stamp_response {
    my $queryfile = shift;
    my $outputfile = shift;
    my $datafile = shift;

    return
	ok(run(app(["openssl", "ts", "-reply", "-section", "$datafile", "-queryfile", "$queryfile", "-out", "$outputfile"])));
}

sub time_stamp_response_token_test {
    my $queryfile = shift;
    my $inputfile = shift;
    my $RESPONSE2="$inputfile.copy.tsr";
    my $TOKEN_DER="$inputfile.token.der";

    ok(run(app(["openssl", "ts", "-reply", "-in", "$inputfile", "-out", "$TOKEN_DER", "-token_out"])));
    ok(run(app(["openssl", "ts", "-reply", "-in", "$TOKEN_DER", "-token_in", "-out", "$RESPONSE2"])));
    is(compare($RESPONSE2, $inputfile), 0);
    ok(run(app(["openssl", "ts", "-reply", "-in", "$inputfile", "-text", "-token_out"])));
    ok(run(app(["openssl", "ts", "-reply", "-in", "$TOKEN_DER", "-token_in", "-text", "-token_out"])));
    ok(run(app(["openssl", "ts", "-reply", "-queryfile", "$queryfile", "-text", "-token_out"])));
}

sub verify_time_stamp_response {
    my $queryfile = shift;
    my $inputfile = shift;
    my $datafile = shift;

    ok(run(app(["openssl", "ts", "-verify", "-queryfile", "$queryfile", "-in", "$inputfile", "-CAfile", "tsaca.pem", "-untrusted", "tsa_cert1.pem"])));
    ok(run(app(["openssl", "ts", "-verify", "-data", "$datafile", "-in", "$inputfile", "-CAfile", "tsaca.pem", "-untrusted", "tsa_cert1.pem"])));
}

sub verify_time_stamp_token {
    my $queryfile = shift;
    my $inputfile = shift;
    my $datafile = shift;

    # create the token from the response first
    ok(run(app(["openssl", "ts", "-reply", "-in", "$inputfile", "-out", "$inputfile.token", "-token_out"])));
    ok(run(app(["openssl", "ts", "-verify", "-queryfile", "$queryfile", "-in", "$inputfile.token", "-token_in", "-CAfile", "tsaca.pem", "-untrusted", "tsa_cert1.pem"])));
    ok(run(app(["openssl", "ts", "-verify", "-data", "$datafile", "-in", "$inputfile.token", "-token_in", "-CAfile", "tsaca.pem", "-untrusted", "tsa_cert1.pem"])));
}

sub verify_time_stamp_response_fail {
    my $queryfile = shift;
    my $inputfile = shift;

    ok(!run(app(["openssl", "ts", "-verify", "-queryfile", "$queryfile", "-in", "$inputfile", "-CAfile", "tsaca.pem", "-untrusted", "tsa_cert1.pem"])));
}

# main functions

indir "tsa" => sub {

    $ENV{OPENSSL_CONF} = top_file("test", "CAtsa.cnf");
    # Because that's what ../apps/CA.pl really looks at
    $ENV{SSLEAY_CONFIG} = "-config ".$ENV{OPENSSL_CONF};
    $ENV{OPENSSL} = cmdstr(app(["openssl"]));
    $testtsa = top_file("test", "recipes", "80-test_tsa.t");
    $CAtsa = top_file("test", "CAtsa.cnf");

    plan tests => 20;

  SKIP: {
      skip "failed", 19
	  if !subtest 'creating CA for TSA tests' => sub { create_ca };

      skip "failed", 18
	  if !subtest 'creating tsa_cert1.pem TSA server cert' => sub {
	      create_tsa_cert("1", "tsa_cert")
      };

      skip "failed", 17
	  if !subtest 'creating tsa_cert2.pem non-TSA server cert' => sub {
	      create_tsa_cert("2", "non_tsa_cert")
      };

      skip "failed", 16
	  if !subtest 'creating req1.req time stamp request for file testtsa' => sub {
	      create_time_stamp_request1()
      };

      subtest 'printing req1.req' => sub {
	  print_request("req1.tsq")
      };

      subtest 'generating valid response for req1.req' => sub {
	  create_time_stamp_response("req1.tsq", "resp1.tsr", "tsa_config1")
      };

      subtest 'printing response' => sub {
	  print_response("resp1.tsr")
      };

      subtest 'verifying valid response' => sub {
	  verify_time_stamp_response("req1.tsq", "resp1.tsr", $testtsa)
      };

      subtest 'verifying valid token' => sub {
	  verify_time_stamp_token("req1.tsq", "resp1.tsr", $testtsa)
      };

      subtest 'creating req2.req time stamp request for file testtsa' => sub {
	  create_time_stamp_request2()
      };

      subtest 'printing req2.req' => sub {
	  print_request("req2.tsq")
      };

      subtest 'generating valid response for req2.req' => sub {
	  create_time_stamp_response("req2.tsq", "resp2.tsr", "tsa_config1")
      };

      subtest 'checking -token_in and -token_out options with -reply' => sub {
	  time_stamp_response_token_test("req2.tsq", "resp2.tsr")
      };

      subtest 'printing response' => sub {
	  print_response("resp2.tsr")
      };

      subtest 'verifying valid response' => sub {
	  verify_time_stamp_response("req2.tsq", "resp2.tsr", $testtsa)
      };

      subtest 'verifying response against wrong request, it should fail' => sub {
	  verify_time_stamp_response_fail("req1.tsq", "resp2.tsr")
      };

      subtest 'verifying response against wrong request, it should fail' => sub {
	  verify_time_stamp_response_fail("req2.tsq", "resp1.tsr")
      };

      subtest 'creating req3.req time stamp request for file CAtsa.cnf' => sub {
	  create_time_stamp_request3()
      };

      subtest 'printing req3.req' => sub {
	  print_request("req3.tsq")
      };

      subtest 'verifying response against wrong request, it should fail' => sub {
	  verify_time_stamp_response_fail("req3.tsq", "resp1.tsr")
      };
    }
}, cleanup => 1, create => 1;