xref: /freebsd/crypto/openssl/test/recipes/80-test_tsa.t (revision e0c4386e7e71d93b0edc0c8fa156263fc4a8b0b6)
1*e0c4386eSCy Schubert#! /usr/bin/env perl
2*e0c4386eSCy Schubert# Copyright 2015-2021 The OpenSSL Project Authors. All Rights Reserved.
3*e0c4386eSCy Schubert#
4*e0c4386eSCy Schubert# Licensed under the Apache License 2.0 (the "License").  You may not use
5*e0c4386eSCy Schubert# this file except in compliance with the License.  You can obtain a copy
6*e0c4386eSCy Schubert# in the file LICENSE in the source distribution or at
7*e0c4386eSCy Schubert# https://www.openssl.org/source/license.html
8*e0c4386eSCy Schubert
9*e0c4386eSCy Schubert
10*e0c4386eSCy Schubertuse strict;
11*e0c4386eSCy Schubertuse warnings;
12*e0c4386eSCy Schubert
13*e0c4386eSCy Schubertuse POSIX;
14*e0c4386eSCy Schubertuse File::Spec::Functions qw/splitdir curdir catfile/;
15*e0c4386eSCy Schubertuse File::Compare;
16*e0c4386eSCy Schubertuse OpenSSL::Test qw/:DEFAULT cmdstr srctop_file data_file/;
17*e0c4386eSCy Schubertuse OpenSSL::Test::Utils;
18*e0c4386eSCy Schubert
19*e0c4386eSCy Schubertsetup("test_tsa");
20*e0c4386eSCy Schubert
21*e0c4386eSCy Schubertplan skip_all => "TS is not supported by this OpenSSL build"
22*e0c4386eSCy Schubert    if disabled("ts");
23*e0c4386eSCy Schubert
24*e0c4386eSCy Schubert# All these are modified inside indir further down. They need to exist
25*e0c4386eSCy Schubert# here, however, to be available in all subroutines.
26*e0c4386eSCy Schubertmy $openssl_conf;
27*e0c4386eSCy Schubertmy $testtsa;
28*e0c4386eSCy Schubertmy $tsacakey;
29*e0c4386eSCy Schubertmy $CAtsa;
30*e0c4386eSCy Schubertmy @QUERY = ("openssl", "ts", "-query");
31*e0c4386eSCy Schubertmy @REPLY;
32*e0c4386eSCy Schubertmy @VERIFY = ("openssl", "ts", "-verify");
33*e0c4386eSCy Schubert
34*e0c4386eSCy Schubertsub create_tsa_cert {
35*e0c4386eSCy Schubert    my $INDEX = shift;
36*e0c4386eSCy Schubert    my $EXT = shift;
37*e0c4386eSCy Schubert    my $r = 1;
38*e0c4386eSCy Schubert    $ENV{TSDNSECT} = "ts_cert_dn";
39*e0c4386eSCy Schubert
40*e0c4386eSCy Schubert    ok(run(app(["openssl", "req", "-config", $openssl_conf, "-new",
41*e0c4386eSCy Schubert                "-out", "tsa_req${INDEX}.pem",
42*e0c4386eSCy Schubert                "-key", srctop_file("test", "certs", "alt${INDEX}-key.pem"),
43*e0c4386eSCy Schubert                "-keyout", "tsa_key${INDEX}.pem"])));
44*e0c4386eSCy Schubert    note "using extension $EXT";
45*e0c4386eSCy Schubert    ok(run(app(["openssl", "x509", "-req",
46*e0c4386eSCy Schubert                "-in", "tsa_req${INDEX}.pem",
47*e0c4386eSCy Schubert                "-out", "tsa_cert${INDEX}.pem",
48*e0c4386eSCy Schubert                "-CA", "tsaca.pem", "-CAkey", $tsacakey,
49*e0c4386eSCy Schubert                "-CAcreateserial",
50*e0c4386eSCy Schubert                "-extfile", $openssl_conf, "-extensions", $EXT])));
51*e0c4386eSCy Schubert}
52*e0c4386eSCy Schubert
53*e0c4386eSCy Schubertsub create_resp {
54*e0c4386eSCy Schubert    my $config = shift;
55*e0c4386eSCy Schubert    my $chain = shift;
56*e0c4386eSCy Schubert    my $queryfile = shift;
57*e0c4386eSCy Schubert    my $outputfile = shift;
58*e0c4386eSCy Schubert
59*e0c4386eSCy Schubert    ok(run(app([@REPLY, "-section", $config, "-queryfile", $queryfile,
60*e0c4386eSCy Schubert                "-chain", $chain, # this overrides "certs" entry in config
61*e0c4386eSCy Schubert                "-out", $outputfile])));
62*e0c4386eSCy Schubert}
63*e0c4386eSCy Schubert
64*e0c4386eSCy Schubertsub verify_ok {
65*e0c4386eSCy Schubert    my $datafile = shift;
66*e0c4386eSCy Schubert    my $queryfile = shift;
67*e0c4386eSCy Schubert    my $inputfile = shift;
68*e0c4386eSCy Schubert    my $untrustedfile = shift;
69*e0c4386eSCy Schubert
70*e0c4386eSCy Schubert    ok(run(app([@VERIFY, "-queryfile", $queryfile, "-in", $inputfile,
71*e0c4386eSCy Schubert                "-CAfile", "tsaca.pem", "-untrusted", $untrustedfile])));
72*e0c4386eSCy Schubert    ok(run(app([@VERIFY, "-data", $datafile, "-in", $inputfile,
73*e0c4386eSCy Schubert                "-CAfile", "tsaca.pem", "-untrusted", $untrustedfile])));
74*e0c4386eSCy Schubert}
75*e0c4386eSCy Schubert
76*e0c4386eSCy Schubertsub verify_fail {
77*e0c4386eSCy Schubert    my $queryfile = shift;
78*e0c4386eSCy Schubert    my $inputfile = shift;
79*e0c4386eSCy Schubert    my $untrustedfile = shift; # is needed for resp2, but not for resp1
80*e0c4386eSCy Schubert    my $cafile = shift;
81*e0c4386eSCy Schubert
82*e0c4386eSCy Schubert    ok(!run(app([@VERIFY, "-queryfile", $queryfile, "-in", $inputfile,
83*e0c4386eSCy Schubert                 "-untrusted", $untrustedfile, "-CAfile", $cafile])));
84*e0c4386eSCy Schubert}
85*e0c4386eSCy Schubert
86*e0c4386eSCy Schubert# main functions
87*e0c4386eSCy Schubert
88*e0c4386eSCy Schubertplan tests => 27;
89*e0c4386eSCy Schubert
90*e0c4386eSCy Schubertnote "setting up TSA test directory";
91*e0c4386eSCy Schubertindir "tsa" => sub
92*e0c4386eSCy Schubert{
93*e0c4386eSCy Schubert    $openssl_conf = srctop_file("test", "CAtsa.cnf");
94*e0c4386eSCy Schubert    $testtsa = srctop_file("test", "recipes", "80-test_tsa.t");
95*e0c4386eSCy Schubert    $tsacakey = srctop_file("test", "certs", "ca-key.pem");
96*e0c4386eSCy Schubert    $CAtsa = srctop_file("test", "CAtsa.cnf");
97*e0c4386eSCy Schubert    @REPLY = ("openssl", "ts", "-config", $openssl_conf, "-reply");
98*e0c4386eSCy Schubert
99*e0c4386eSCy Schubert    # ../apps/CA.pl needs these
100*e0c4386eSCy Schubert    $ENV{OPENSSL_CONFIG} = "-config $openssl_conf";
101*e0c4386eSCy Schubert    $ENV{OPENSSL} = cmdstr(app(["openssl"]), display => 1);
102*e0c4386eSCy Schubert
103*e0c4386eSCy Schubert SKIP: {
104*e0c4386eSCy Schubert     $ENV{TSDNSECT} = "ts_ca_dn";
105*e0c4386eSCy Schubert     skip "failed", 19
106*e0c4386eSCy Schubert         unless ok(run(app(["openssl", "req", "-config", $openssl_conf,
107*e0c4386eSCy Schubert                            "-new", "-x509", "-noenc",
108*e0c4386eSCy Schubert                            "-out", "tsaca.pem", "-key", $tsacakey])),
109*e0c4386eSCy Schubert                   'creating a new CA for the TSA tests');
110*e0c4386eSCy Schubert
111*e0c4386eSCy Schubert     skip "failed", 18
112*e0c4386eSCy Schubert         unless subtest 'creating tsa_cert1.pem TSA server cert' => sub {
113*e0c4386eSCy Schubert             create_tsa_cert("1", "tsa_cert")
114*e0c4386eSCy Schubert     };
115*e0c4386eSCy Schubert
116*e0c4386eSCy Schubert     skip "failed", 17
117*e0c4386eSCy Schubert         unless subtest 'creating tsa_cert2.pem non-TSA server cert' => sub {
118*e0c4386eSCy Schubert             create_tsa_cert("2", "non_tsa_cert")
119*e0c4386eSCy Schubert     };
120*e0c4386eSCy Schubert
121*e0c4386eSCy Schubert     skip "failed", 16
122*e0c4386eSCy Schubert         unless ok(run(app([@QUERY, "-data", $testtsa,
123*e0c4386eSCy Schubert                            "-tspolicy", "tsa_policy1", "-cert",
124*e0c4386eSCy Schubert                            "-out", "req1.tsq"])),
125*e0c4386eSCy Schubert                   'creating req1.req time stamp request for file testtsa');
126*e0c4386eSCy Schubert
127*e0c4386eSCy Schubert     ok(run(app([@QUERY, "-in", "req1.tsq", "-text"])),
128*e0c4386eSCy Schubert        'printing req1.req');
129*e0c4386eSCy Schubert
130*e0c4386eSCy Schubert     subtest 'generating valid response for req1.req' => sub {
131*e0c4386eSCy Schubert         create_resp("tsa_config1", "tsaca.pem", "req1.tsq", "resp1.tsr")
132*e0c4386eSCy Schubert     };
133*e0c4386eSCy Schubert
134*e0c4386eSCy Schubert     subtest 'generating response with wrong 2nd certid for req1.req' => sub {
135*e0c4386eSCy Schubert         create_resp("tsa_config1", "tsa_cert1.pem", "req1.tsq",
136*e0c4386eSCy Schubert                     "resp1_invalid.tsr")
137*e0c4386eSCy Schubert     };
138*e0c4386eSCy Schubert
139*e0c4386eSCy Schubert     ok(run(app([@REPLY, "-in", "resp1.tsr", "-text"])),
140*e0c4386eSCy Schubert        'printing response');
141*e0c4386eSCy Schubert
142*e0c4386eSCy Schubert     subtest 'verifying valid response' => sub {
143*e0c4386eSCy Schubert         verify_ok($testtsa, "req1.tsq", "resp1.tsr", "tsa_cert1.pem")
144*e0c4386eSCy Schubert     };
145*e0c4386eSCy Schubert
146*e0c4386eSCy Schubert     skip "failed", 11
147*e0c4386eSCy Schubert         unless subtest 'verifying valid token' => sub {
148*e0c4386eSCy Schubert             ok(run(app([@REPLY, "-in", "resp1.tsr",
149*e0c4386eSCy Schubert                         "-out", "resp1.tsr.token", "-token_out"])));
150*e0c4386eSCy Schubert             ok(run(app([@VERIFY, "-queryfile", "req1.tsq",
151*e0c4386eSCy Schubert                         "-in", "resp1.tsr.token", "-token_in",
152*e0c4386eSCy Schubert                         "-CAfile", "tsaca.pem"])));
153*e0c4386eSCy Schubert             ok(run(app([@VERIFY, "-data", $testtsa,
154*e0c4386eSCy Schubert                         "-in", "resp1.tsr.token", "-token_in",
155*e0c4386eSCy Schubert                         "-CAfile", "tsaca.pem"])));
156*e0c4386eSCy Schubert     };
157*e0c4386eSCy Schubert
158*e0c4386eSCy Schubert     skip "failed", 10
159*e0c4386eSCy Schubert         unless ok(run(app([@QUERY, "-data", $testtsa,
160*e0c4386eSCy Schubert                            "-tspolicy", "tsa_policy2", "-no_nonce",
161*e0c4386eSCy Schubert                            "-out", "req2.tsq"])),
162*e0c4386eSCy Schubert                   'creating req2.req time stamp request for file testtsa');
163*e0c4386eSCy Schubert
164*e0c4386eSCy Schubert     ok(run(app([@QUERY, "-in", "req2.tsq", "-text"])),
165*e0c4386eSCy Schubert        'printing req2.req');
166*e0c4386eSCy Schubert
167*e0c4386eSCy Schubert     skip "failed", 8
168*e0c4386eSCy Schubert         unless subtest 'generating valid response for req2.req' => sub {
169*e0c4386eSCy Schubert             create_resp("tsa_config1", "tsaca.pem", "req2.tsq", "resp2.tsr")
170*e0c4386eSCy Schubert     };
171*e0c4386eSCy Schubert
172*e0c4386eSCy Schubert     skip "failed", 7
173*e0c4386eSCy Schubert         unless subtest 'checking -token_in and -token_out options with -reply' => sub {
174*e0c4386eSCy Schubert             my $RESPONSE2="resp2.tsr.copy.tsr";
175*e0c4386eSCy Schubert             my $TOKEN_DER="resp2.tsr.token.der";
176*e0c4386eSCy Schubert
177*e0c4386eSCy Schubert             ok(run(app([@REPLY, "-in", "resp2.tsr",
178*e0c4386eSCy Schubert                         "-out", "$TOKEN_DER", "-token_out"])));
179*e0c4386eSCy Schubert             ok(run(app([@REPLY, "-in", "$TOKEN_DER",
180*e0c4386eSCy Schubert                         "-token_in", "-out", "$RESPONSE2"])));
181*e0c4386eSCy Schubert             is(compare($RESPONSE2, "resp2.tsr"), 0);
182*e0c4386eSCy Schubert             ok(run(app([@REPLY, "-in", "resp2.tsr",
183*e0c4386eSCy Schubert                         "-text", "-token_out"])));
184*e0c4386eSCy Schubert             ok(run(app([@REPLY, "-in", "$TOKEN_DER",
185*e0c4386eSCy Schubert                         "-token_in", "-text", "-token_out"])));
186*e0c4386eSCy Schubert             ok(run(app([@REPLY, "-queryfile", "req2.tsq",
187*e0c4386eSCy Schubert                         "-text", "-token_out"])));
188*e0c4386eSCy Schubert     };
189*e0c4386eSCy Schubert
190*e0c4386eSCy Schubert     ok(run(app([@REPLY, "-in", "resp2.tsr", "-text"])),
191*e0c4386eSCy Schubert        'printing response');
192*e0c4386eSCy Schubert
193*e0c4386eSCy Schubert     subtest 'verifying valid resp1, wrong untrusted is not used' => sub {
194*e0c4386eSCy Schubert         verify_ok($testtsa, "req1.tsq", "resp1.tsr", "tsa_cert2.pem")
195*e0c4386eSCy Schubert     };
196*e0c4386eSCy Schubert
197*e0c4386eSCy Schubert     subtest 'verifying invalid resp1 with wrong 2nd certid' => sub {
198*e0c4386eSCy Schubert         verify_fail($testtsa, "req1.tsq", "resp1_invalid.tsr", "tsa_cert2.pem")
199*e0c4386eSCy Schubert     };
200*e0c4386eSCy Schubert
201*e0c4386eSCy Schubert     subtest 'verifying valid resp2, correct untrusted being used' => sub {
202*e0c4386eSCy Schubert         verify_ok($testtsa, "req2.tsq", "resp2.tsr", "tsa_cert1.pem")
203*e0c4386eSCy Schubert     };
204*e0c4386eSCy Schubert
205*e0c4386eSCy Schubert     subtest 'verifying resp2 against wrong req1 should fail' => sub {
206*e0c4386eSCy Schubert         verify_fail("req1.tsq", "resp2.tsr", "tsa_cert1.pem", "tsaca.pem")
207*e0c4386eSCy Schubert     };
208*e0c4386eSCy Schubert
209*e0c4386eSCy Schubert     subtest 'verifying resp1 against wrong req2 should fail' => sub {
210*e0c4386eSCy Schubert         verify_fail("req2.tsq", "resp1.tsr", "tsa_cert1.pem", "tsaca.pem")
211*e0c4386eSCy Schubert     };
212*e0c4386eSCy Schubert
213*e0c4386eSCy Schubert     subtest 'verifying resp1 using wrong untrusted should fail' => sub {
214*e0c4386eSCy Schubert         verify_fail("req2.tsq", "resp2.tsr", "tsa_cert2.pem", "tsaca.pem")
215*e0c4386eSCy Schubert     };
216*e0c4386eSCy Schubert
217*e0c4386eSCy Schubert     subtest 'verifying resp1 using wrong root should fail' => sub {
218*e0c4386eSCy Schubert         verify_fail("req1.tsq", "resp1.tsr", "tsa_cert1.pem", "tsa_cert1.pem")
219*e0c4386eSCy Schubert     };
220*e0c4386eSCy Schubert
221*e0c4386eSCy Schubert     skip "failure", 2
222*e0c4386eSCy Schubert         unless ok(run(app([@QUERY, "-data", $CAtsa,
223*e0c4386eSCy Schubert                            "-no_nonce", "-out", "req3.tsq"])),
224*e0c4386eSCy Schubert                   "creating req3.req time stamp request for file CAtsa.cnf");
225*e0c4386eSCy Schubert
226*e0c4386eSCy Schubert     ok(run(app([@QUERY, "-in", "req3.tsq", "-text"])),
227*e0c4386eSCy Schubert        'printing req3.req');
228*e0c4386eSCy Schubert
229*e0c4386eSCy Schubert     subtest 'verifying resp1 against wrong req3 should fail' => sub {
230*e0c4386eSCy Schubert         verify_fail("req3.tsq", "resp1.tsr", "tsa_cert1.pem", "tsaca.pem")
231*e0c4386eSCy Schubert     };
232*e0c4386eSCy Schubert    }
233*e0c4386eSCy Schubert
234*e0c4386eSCy Schubert    # verifying response with two ESSCertIDs, referring to leaf cert
235*e0c4386eSCy Schubert    # "sectigo-signer.pem" and intermediate cert "sectigo-time-stamping-ca.pem"
236*e0c4386eSCy Schubert    # 1. validation chain contains these certs and root "user-trust-ca.pem"
237*e0c4386eSCy Schubert    ok(run(app([@VERIFY, "-no_check_time",
238*e0c4386eSCy Schubert                "-queryfile", data_file("all-zero.tsq"),
239*e0c4386eSCy Schubert                "-in", data_file("sectigo-all-zero.tsr"),
240*e0c4386eSCy Schubert                "-CAfile", data_file("user-trust-ca.pem")])),
241*e0c4386eSCy Schubert     "validation with two ESSCertIDs and 3-element chain");
242*e0c4386eSCy Schubert    # 2. validation chain contains these certs, a cross-cert, and different root
243*e0c4386eSCy Schubert    ok(run(app([@VERIFY, "-no_check_time",
244*e0c4386eSCy Schubert                "-queryfile", data_file("all-zero.tsq"),
245*e0c4386eSCy Schubert                "-in", data_file("sectigo-all-zero.tsr"),
246*e0c4386eSCy Schubert                "-untrusted", data_file("user-trust-ca-aaa.pem"),
247*e0c4386eSCy Schubert                "-CAfile", data_file("comodo-aaa.pem")])),
248*e0c4386eSCy Schubert     "validation with two ESSCertIDs and 4-element chain");
249*e0c4386eSCy Schubert
250*e0c4386eSCy Schubert}, create => 1, cleanup => 1
251