Cleanup cert config files for tests
[openssl.git] / test / recipes / 80-test_ca.t
1 #! /usr/bin/env perl
2 # Copyright 2015-2020 The OpenSSL Project Authors. All Rights Reserved.
3 #
4 # Licensed under the Apache License 2.0 (the "License").  You may not use
5 # this file except in compliance with the License.  You can obtain a copy
6 # in the file LICENSE in the source distribution or at
7 # https://www.openssl.org/source/license.html
8
9
10 use strict;
11 use warnings;
12
13 use POSIX;
14 use File::Path 2.00 qw/rmtree/;
15 use OpenSSL::Test qw/:DEFAULT cmdstr srctop_file/;
16 use OpenSSL::Test::Utils;
17
18 setup("test_ca");
19
20 $ENV{OPENSSL} = cmdstr(app(["openssl"]), display => 1);
21
22 my $cnf = '"' . srctop_file("test","ca-and-certs.cnf") . '"';;
23 my $std_openssl_cnf = '"'
24     . srctop_file("apps", $^O eq "VMS" ? "openssl-vms.cnf" : "openssl.cnf")
25     . '"';
26
27 rmtree("demoCA", { safe => 0 });
28
29 plan tests => 6;
30  SKIP: {
31      $ENV{OPENSSL_CONFIG} = '-config ' . $cnf;
32      skip "failed creating CA structure", 4
33          if !ok(run(perlapp(["CA.pl","-newca"], stdin => undef)),
34                 'creating CA structure');
35
36      $ENV{OPENSSL_CONFIG} = '-config ' . $cnf;
37      skip "failed creating new certificate request", 3
38          if !ok(run(perlapp(["CA.pl","-newreq",
39                              '-extra-req', '-outform DER -section userreq'])),
40                 'creating certificate request');
41      $ENV{OPENSSL_CONFIG} = '-rand_serial -inform DER -config '.$std_openssl_cnf;
42      skip "failed to sign certificate request", 2
43          if !is(yes(cmdstr(perlapp(["CA.pl", "-sign"]))), 0,
44                 'signing certificate request');
45
46      ok(run(perlapp(["CA.pl", "-verify", "newcert.pem"])),
47         'verifying new certificate');
48
49      skip "CT not configured, can't use -precert", 1
50          if disabled("ct");
51
52      $ENV{OPENSSL_CONFIG} = '-config ' . $cnf;
53      ok(run(perlapp(["CA.pl", "-precert", '-extra-req', '-section userreq'], stderr => undef)),
54         'creating new pre-certificate');
55 }
56
57 SKIP: {
58     skip "SM2 is not supported by this OpenSSL build", 1
59               if disabled("sm2");
60
61     is(yes(cmdstr(app(["openssl", "ca", "-config",
62                        $cnf,
63                        "-in", srctop_file("test", "certs", "sm2-csr.pem"),
64                        "-out", "sm2-test.crt",
65                        "-sigopt", "distid:1234567812345678",
66                        "-vfyopt", "distid:1234567812345678",
67                        "-md", "sm3",
68                        "-cert", srctop_file("test", "certs", "sm2-root.crt"),
69                        "-keyfile", srctop_file("test", "certs", "sm2-root.key")]))),
70        0,
71        "Signing SM2 certificate request");
72 }
73
74 sub yes {
75     my $cntr = 10;
76     open(PIPE, "|-", join(" ",@_));
77     local $SIG{PIPE} = "IGNORE";
78     1 while $cntr-- > 0 && print PIPE "y\n";
79     close PIPE;
80     return 0;
81 }
82