1e1051a39Sopenharmony_ci#! /usr/bin/env perl
2e1051a39Sopenharmony_ci# Copyright 2015-2021 The OpenSSL Project Authors. All Rights Reserved.
3e1051a39Sopenharmony_ci#
4e1051a39Sopenharmony_ci# Licensed under the Apache License 2.0 (the "License").  You may not use
5e1051a39Sopenharmony_ci# this file except in compliance with the License.  You can obtain a copy
6e1051a39Sopenharmony_ci# in the file LICENSE in the source distribution or at
7e1051a39Sopenharmony_ci# https://www.openssl.org/source/license.html
8e1051a39Sopenharmony_ci
9e1051a39Sopenharmony_ci
10e1051a39Sopenharmony_ciuse strict;
11e1051a39Sopenharmony_ciuse warnings;
12e1051a39Sopenharmony_ci
13e1051a39Sopenharmony_ciuse POSIX;
14e1051a39Sopenharmony_ciuse File::Path 2.00 qw/rmtree/;
15e1051a39Sopenharmony_ciuse OpenSSL::Test qw/:DEFAULT cmdstr data_file srctop_file/;
16e1051a39Sopenharmony_ciuse OpenSSL::Test::Utils;
17e1051a39Sopenharmony_ciuse Time::Local qw/timegm/;
18e1051a39Sopenharmony_ci
19e1051a39Sopenharmony_cisetup("test_ca");
20e1051a39Sopenharmony_ci
21e1051a39Sopenharmony_ci$ENV{OPENSSL} = cmdstr(app(["openssl"]), display => 1);
22e1051a39Sopenharmony_ci
23e1051a39Sopenharmony_cimy $cnf = srctop_file("test","ca-and-certs.cnf");
24e1051a39Sopenharmony_cimy $std_openssl_cnf = '"'
25e1051a39Sopenharmony_ci    . srctop_file("apps", $^O eq "VMS" ? "openssl-vms.cnf" : "openssl.cnf")
26e1051a39Sopenharmony_ci    . '"';
27e1051a39Sopenharmony_ci
28e1051a39Sopenharmony_cirmtree("demoCA", { safe => 0 });
29e1051a39Sopenharmony_ci
30e1051a39Sopenharmony_ciplan tests => 15;
31e1051a39Sopenharmony_ci SKIP: {
32e1051a39Sopenharmony_ci     my $cakey = srctop_file("test", "certs", "ca-key.pem");
33e1051a39Sopenharmony_ci     $ENV{OPENSSL_CONFIG} = qq(-config "$cnf");
34e1051a39Sopenharmony_ci     skip "failed creating CA structure", 4
35e1051a39Sopenharmony_ci         if !ok(run(perlapp(["CA.pl","-newca",
36e1051a39Sopenharmony_ci                             "-extra-req", "-key $cakey"], stdin => undef)),
37e1051a39Sopenharmony_ci                'creating CA structure');
38e1051a39Sopenharmony_ci
39e1051a39Sopenharmony_ci     my $eekey = srctop_file("test", "certs", "ee-key.pem");
40e1051a39Sopenharmony_ci     $ENV{OPENSSL_CONFIG} = qq(-config "$cnf");
41e1051a39Sopenharmony_ci     skip "failed creating new certificate request", 3
42e1051a39Sopenharmony_ci         if !ok(run(perlapp(["CA.pl","-newreq",
43e1051a39Sopenharmony_ci                             '-extra-req', "-outform DER -section userreq -key $eekey"])),
44e1051a39Sopenharmony_ci                'creating certificate request');
45e1051a39Sopenharmony_ci     $ENV{OPENSSL_CONFIG} = qq(-rand_serial -inform DER -config "$std_openssl_cnf");
46e1051a39Sopenharmony_ci     skip "failed to sign certificate request", 2
47e1051a39Sopenharmony_ci         if !is(yes(cmdstr(perlapp(["CA.pl", "-sign"]))), 0,
48e1051a39Sopenharmony_ci                'signing certificate request');
49e1051a39Sopenharmony_ci
50e1051a39Sopenharmony_ci     ok(run(perlapp(["CA.pl", "-verify", "newcert.pem"])),
51e1051a39Sopenharmony_ci        'verifying new certificate');
52e1051a39Sopenharmony_ci
53e1051a39Sopenharmony_ci     skip "CT not configured, can't use -precert", 1
54e1051a39Sopenharmony_ci         if disabled("ct");
55e1051a39Sopenharmony_ci
56e1051a39Sopenharmony_ci     my $eekey2 = srctop_file("test", "certs", "ee-key-3072.pem");
57e1051a39Sopenharmony_ci     $ENV{OPENSSL_CONFIG} = qq(-config "$cnf");
58e1051a39Sopenharmony_ci     ok(run(perlapp(["CA.pl", "-precert", '-extra-req', "-section userreq -key $eekey2"], stderr => undef)),
59e1051a39Sopenharmony_ci        'creating new pre-certificate');
60e1051a39Sopenharmony_ci}
61e1051a39Sopenharmony_ci
62e1051a39Sopenharmony_ciSKIP: {
63e1051a39Sopenharmony_ci    skip "SM2 is not supported by this OpenSSL build", 1
64e1051a39Sopenharmony_ci        if disabled("sm2");
65e1051a39Sopenharmony_ci
66e1051a39Sopenharmony_ci    is(yes(cmdstr(app(["openssl", "ca", "-config",
67e1051a39Sopenharmony_ci                       $cnf,
68e1051a39Sopenharmony_ci                       "-in", srctop_file("test", "certs", "sm2-csr.pem"),
69e1051a39Sopenharmony_ci                       "-out", "sm2-test.crt",
70e1051a39Sopenharmony_ci                       "-sigopt", "distid:1234567812345678",
71e1051a39Sopenharmony_ci                       "-vfyopt", "distid:1234567812345678",
72e1051a39Sopenharmony_ci                       "-md", "sm3",
73e1051a39Sopenharmony_ci                       "-cert", srctop_file("test", "certs", "sm2-root.crt"),
74e1051a39Sopenharmony_ci                       "-keyfile", srctop_file("test", "certs", "sm2-root.key")]))),
75e1051a39Sopenharmony_ci       0,
76e1051a39Sopenharmony_ci       "Signing SM2 certificate request");
77e1051a39Sopenharmony_ci}
78e1051a39Sopenharmony_ci
79e1051a39Sopenharmony_citest_revoke('notimes', {
80e1051a39Sopenharmony_ci    should_succeed => 1,
81e1051a39Sopenharmony_ci});
82e1051a39Sopenharmony_citest_revoke('lastupdate_invalid', {
83e1051a39Sopenharmony_ci    lastupdate     => '1234567890',
84e1051a39Sopenharmony_ci    should_succeed => 0,
85e1051a39Sopenharmony_ci});
86e1051a39Sopenharmony_citest_revoke('lastupdate_utctime', {
87e1051a39Sopenharmony_ci    lastupdate     => '200901123456Z',
88e1051a39Sopenharmony_ci    should_succeed => 1,
89e1051a39Sopenharmony_ci});
90e1051a39Sopenharmony_citest_revoke('lastupdate_generalizedtime', {
91e1051a39Sopenharmony_ci    lastupdate     => '20990901123456Z',
92e1051a39Sopenharmony_ci    should_succeed => 1,
93e1051a39Sopenharmony_ci});
94e1051a39Sopenharmony_citest_revoke('nextupdate_invalid', {
95e1051a39Sopenharmony_ci    nextupdate     => '1234567890',
96e1051a39Sopenharmony_ci    should_succeed => 0,
97e1051a39Sopenharmony_ci});
98e1051a39Sopenharmony_citest_revoke('nextupdate_utctime', {
99e1051a39Sopenharmony_ci    nextupdate     => '200901123456Z',
100e1051a39Sopenharmony_ci    should_succeed => 1,
101e1051a39Sopenharmony_ci});
102e1051a39Sopenharmony_citest_revoke('nextupdate_generalizedtime', {
103e1051a39Sopenharmony_ci    nextupdate     => '20990901123456Z',
104e1051a39Sopenharmony_ci    should_succeed => 1,
105e1051a39Sopenharmony_ci});
106e1051a39Sopenharmony_citest_revoke('both_utctime', {
107e1051a39Sopenharmony_ci    lastupdate     => '200901123456Z',
108e1051a39Sopenharmony_ci    nextupdate     => '200908123456Z',
109e1051a39Sopenharmony_ci    should_succeed => 1,
110e1051a39Sopenharmony_ci});
111e1051a39Sopenharmony_citest_revoke('both_generalizedtime', {
112e1051a39Sopenharmony_ci    lastupdate     => '20990901123456Z',
113e1051a39Sopenharmony_ci    nextupdate     => '20990908123456Z',
114e1051a39Sopenharmony_ci    should_succeed => 1,
115e1051a39Sopenharmony_ci});
116e1051a39Sopenharmony_ci
117e1051a39Sopenharmony_cisub test_revoke {
118e1051a39Sopenharmony_ci    my ($filename, $opts) = @_;
119e1051a39Sopenharmony_ci
120e1051a39Sopenharmony_ci    subtest "Revoke certificate and generate CRL: $filename" => sub {
121e1051a39Sopenharmony_ci        # Before Perl 5.12.0, the range of times Perl could represent was
122e1051a39Sopenharmony_ci        # limited by the size of time_t, so Time::Local was hamstrung by the
123e1051a39Sopenharmony_ci        # Y2038 problem
124e1051a39Sopenharmony_ci        # Perl 5.12.0 onwards use an internal time implementation with a
125e1051a39Sopenharmony_ci        # guaranteed >32-bit time range on all architectures, so the tests
126e1051a39Sopenharmony_ci        # involving post-2038 times won't fail provided we're running under
127e1051a39Sopenharmony_ci        # that version or newer
128e1051a39Sopenharmony_ci        plan skip_all =>
129e1051a39Sopenharmony_ci            'Perl >= 5.12.0 required to run certificate revocation tests'
130e1051a39Sopenharmony_ci            if $] < 5.012000;
131e1051a39Sopenharmony_ci
132e1051a39Sopenharmony_ci        $ENV{CN2} = $filename;
133e1051a39Sopenharmony_ci        ok(
134e1051a39Sopenharmony_ci            run(app(['openssl',
135e1051a39Sopenharmony_ci                     'req',
136e1051a39Sopenharmony_ci                     '-config',  $cnf,
137e1051a39Sopenharmony_ci                     '-new',
138e1051a39Sopenharmony_ci                     '-key',     data_file('revoked.key'),
139e1051a39Sopenharmony_ci                     '-out',     "$filename-req.pem",
140e1051a39Sopenharmony_ci                     '-section', 'userreq',
141e1051a39Sopenharmony_ci            ])),
142e1051a39Sopenharmony_ci            'Generate CSR'
143e1051a39Sopenharmony_ci        );
144e1051a39Sopenharmony_ci        delete $ENV{CN2};
145e1051a39Sopenharmony_ci
146e1051a39Sopenharmony_ci        ok(
147e1051a39Sopenharmony_ci            run(app(['openssl',
148e1051a39Sopenharmony_ci                     'ca',
149e1051a39Sopenharmony_ci                     '-batch',
150e1051a39Sopenharmony_ci                     '-config',  $cnf,
151e1051a39Sopenharmony_ci                     '-in',      "$filename-req.pem",
152e1051a39Sopenharmony_ci                     '-out',     "$filename-cert.pem",
153e1051a39Sopenharmony_ci            ])),
154e1051a39Sopenharmony_ci            'Sign CSR'
155e1051a39Sopenharmony_ci        );
156e1051a39Sopenharmony_ci
157e1051a39Sopenharmony_ci        ok(
158e1051a39Sopenharmony_ci            run(app(['openssl',
159e1051a39Sopenharmony_ci                     'ca',
160e1051a39Sopenharmony_ci                     '-config', $cnf,
161e1051a39Sopenharmony_ci                     '-revoke', "$filename-cert.pem",
162e1051a39Sopenharmony_ci            ])),
163e1051a39Sopenharmony_ci            'Revoke certificate'
164e1051a39Sopenharmony_ci        );
165e1051a39Sopenharmony_ci
166e1051a39Sopenharmony_ci        my @gencrl_opts;
167e1051a39Sopenharmony_ci
168e1051a39Sopenharmony_ci        if (exists $opts->{lastupdate}) {
169e1051a39Sopenharmony_ci            push @gencrl_opts, '-crl_lastupdate', $opts->{lastupdate};
170e1051a39Sopenharmony_ci        }
171e1051a39Sopenharmony_ci
172e1051a39Sopenharmony_ci        if (exists $opts->{nextupdate}) {
173e1051a39Sopenharmony_ci            push @gencrl_opts, '-crl_nextupdate', $opts->{nextupdate};
174e1051a39Sopenharmony_ci        }
175e1051a39Sopenharmony_ci
176e1051a39Sopenharmony_ci        is(
177e1051a39Sopenharmony_ci            run(app(['openssl',
178e1051a39Sopenharmony_ci                     'ca',
179e1051a39Sopenharmony_ci                     '-config', $cnf,
180e1051a39Sopenharmony_ci                     '-gencrl',
181e1051a39Sopenharmony_ci                     '-out',    "$filename-crl.pem",
182e1051a39Sopenharmony_ci                     '-crlsec', '60',
183e1051a39Sopenharmony_ci                     @gencrl_opts,
184e1051a39Sopenharmony_ci            ])),
185e1051a39Sopenharmony_ci            $opts->{should_succeed},
186e1051a39Sopenharmony_ci            'Generate CRL'
187e1051a39Sopenharmony_ci        );
188e1051a39Sopenharmony_ci        my $crl_gentime = time;
189e1051a39Sopenharmony_ci
190e1051a39Sopenharmony_ci        # The following tests only need to run if the CRL was supposed to be
191e1051a39Sopenharmony_ci        # generated:
192e1051a39Sopenharmony_ci        return unless $opts->{should_succeed};
193e1051a39Sopenharmony_ci
194e1051a39Sopenharmony_ci        my $crl_lastupdate = crl_field("$filename-crl.pem", 'lastUpdate');
195e1051a39Sopenharmony_ci        if (exists $opts->{lastupdate}) {
196e1051a39Sopenharmony_ci            is(
197e1051a39Sopenharmony_ci                $crl_lastupdate,
198e1051a39Sopenharmony_ci                rfc5280_time($opts->{lastupdate}),
199e1051a39Sopenharmony_ci                'CRL lastUpdate field has expected value'
200e1051a39Sopenharmony_ci            );
201e1051a39Sopenharmony_ci        } else {
202e1051a39Sopenharmony_ci            diag("CRL lastUpdate:   $crl_lastupdate");
203e1051a39Sopenharmony_ci            diag("openssl run time: $crl_gentime");
204e1051a39Sopenharmony_ci            ok(
205e1051a39Sopenharmony_ci                # Is the CRL's lastUpdate time within a second of the time that
206e1051a39Sopenharmony_ci                # `openssl ca -gencrl` was executed?
207e1051a39Sopenharmony_ci                $crl_gentime - 1 <= $crl_lastupdate && $crl_lastupdate <= $crl_gentime + 1,
208e1051a39Sopenharmony_ci                'CRL lastUpdate field has (roughly) expected value'
209e1051a39Sopenharmony_ci            );
210e1051a39Sopenharmony_ci        }
211e1051a39Sopenharmony_ci
212e1051a39Sopenharmony_ci        my $crl_nextupdate = crl_field("$filename-crl.pem", 'nextUpdate');
213e1051a39Sopenharmony_ci        if (exists $opts->{nextupdate}) {
214e1051a39Sopenharmony_ci            is(
215e1051a39Sopenharmony_ci                $crl_nextupdate,
216e1051a39Sopenharmony_ci                rfc5280_time($opts->{nextupdate}),
217e1051a39Sopenharmony_ci                'CRL nextUpdate field has expected value'
218e1051a39Sopenharmony_ci            );
219e1051a39Sopenharmony_ci        } else {
220e1051a39Sopenharmony_ci            diag("CRL nextUpdate:   $crl_nextupdate");
221e1051a39Sopenharmony_ci            diag("openssl run time: $crl_gentime");
222e1051a39Sopenharmony_ci            ok(
223e1051a39Sopenharmony_ci                # Is the CRL's lastUpdate time within a second of the time that
224e1051a39Sopenharmony_ci                # `openssl ca -gencrl` was executed, taking into account the use
225e1051a39Sopenharmony_ci                # of '-crlsec 60'?
226e1051a39Sopenharmony_ci                $crl_gentime + 59 <= $crl_nextupdate && $crl_nextupdate <= $crl_gentime + 61,
227e1051a39Sopenharmony_ci                'CRL nextUpdate field has (roughly) expected value'
228e1051a39Sopenharmony_ci            );
229e1051a39Sopenharmony_ci        }
230e1051a39Sopenharmony_ci    };
231e1051a39Sopenharmony_ci}
232e1051a39Sopenharmony_ci
233e1051a39Sopenharmony_cisub yes {
234e1051a39Sopenharmony_ci    my $cntr = 10;
235e1051a39Sopenharmony_ci    open(PIPE, "|-", join(" ",@_));
236e1051a39Sopenharmony_ci    local $SIG{PIPE} = "IGNORE";
237e1051a39Sopenharmony_ci    1 while $cntr-- > 0 && print PIPE "y\n";
238e1051a39Sopenharmony_ci    close PIPE;
239e1051a39Sopenharmony_ci    return 0;
240e1051a39Sopenharmony_ci}
241e1051a39Sopenharmony_ci
242e1051a39Sopenharmony_ci# Get the value of the lastUpdate or nextUpdate field from a CRL
243e1051a39Sopenharmony_cisub crl_field {
244e1051a39Sopenharmony_ci    my ($crl_path, $field_name) = @_;
245e1051a39Sopenharmony_ci
246e1051a39Sopenharmony_ci    my @out = run(
247e1051a39Sopenharmony_ci        app(['openssl',
248e1051a39Sopenharmony_ci             'crl',
249e1051a39Sopenharmony_ci             '-in', $crl_path,
250e1051a39Sopenharmony_ci             '-noout',
251e1051a39Sopenharmony_ci             '-' . lc($field_name),
252e1051a39Sopenharmony_ci        ]),
253e1051a39Sopenharmony_ci        capture => 1,
254e1051a39Sopenharmony_ci        statusvar => \my $exit,
255e1051a39Sopenharmony_ci    );
256e1051a39Sopenharmony_ci    ok($exit, "CRL $field_name field retrieved");
257e1051a39Sopenharmony_ci    diag("CRL $field_name: $out[0]");
258e1051a39Sopenharmony_ci
259e1051a39Sopenharmony_ci    $out[0] =~ s/^\Q$field_name\E=//;
260e1051a39Sopenharmony_ci    $out[0] =~ s/\n?//;
261e1051a39Sopenharmony_ci    my $time = human_time($out[0]);
262e1051a39Sopenharmony_ci
263e1051a39Sopenharmony_ci    return $time;
264e1051a39Sopenharmony_ci}
265e1051a39Sopenharmony_ci
266e1051a39Sopenharmony_ci# Converts human-readable ASN1_TIME_print() output to Unix time
267e1051a39Sopenharmony_cisub human_time {
268e1051a39Sopenharmony_ci    my ($human) = @_;
269e1051a39Sopenharmony_ci
270e1051a39Sopenharmony_ci    my ($mo, $d, $h, $m, $s, $y) = $human =~ /^([A-Za-z]{3})\s+(\d+) (\d{2}):(\d{2}):(\d{2}) (\d{4})/;
271e1051a39Sopenharmony_ci
272e1051a39Sopenharmony_ci    my %months = (
273e1051a39Sopenharmony_ci        Jan => 0, Feb => 1, Mar => 2, Apr => 3, May => 4,  Jun => 5,
274e1051a39Sopenharmony_ci        Jul => 6, Aug => 7, Sep => 8, Oct => 9, Nov => 10, Dec => 11,
275e1051a39Sopenharmony_ci    );
276e1051a39Sopenharmony_ci
277e1051a39Sopenharmony_ci    return timegm($s, $m, $h, $d, $months{$mo}, $y);
278e1051a39Sopenharmony_ci}
279e1051a39Sopenharmony_ci
280e1051a39Sopenharmony_ci# Converts an RFC 5280 timestamp to Unix time
281e1051a39Sopenharmony_cisub rfc5280_time {
282e1051a39Sopenharmony_ci    my ($asn1) = @_;
283e1051a39Sopenharmony_ci
284e1051a39Sopenharmony_ci    my ($y, $mo, $d, $h, $m, $s) = $asn1 =~ /^(\d{2,4})(\d{2})(\d{2})(\d{2})(\d{2})(\d{2})Z$/;
285e1051a39Sopenharmony_ci
286e1051a39Sopenharmony_ci    return timegm($s, $m, $h, $d, $mo - 1, $y);
287e1051a39Sopenharmony_ci}
288