1e1051a39Sopenharmony_ci#!{- $config{HASHBANGPERL} -}
2e1051a39Sopenharmony_ci# Copyright 2000-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_ci# Wrapper around the ca to make it easier to use
11e1051a39Sopenharmony_ci#
12e1051a39Sopenharmony_ci# {- join("\n# ", @autowarntext) -}
13e1051a39Sopenharmony_ci
14e1051a39Sopenharmony_ciuse strict;
15e1051a39Sopenharmony_ciuse warnings;
16e1051a39Sopenharmony_ci
17e1051a39Sopenharmony_cimy $verbose = 1;
18e1051a39Sopenharmony_cimy @OPENSSL_CMDS = ("req", "ca", "pkcs12", "x509", "verify");
19e1051a39Sopenharmony_ci
20e1051a39Sopenharmony_cimy $openssl = $ENV{'OPENSSL'} // "openssl";
21e1051a39Sopenharmony_ci$ENV{'OPENSSL'} = $openssl;
22e1051a39Sopenharmony_cimy $OPENSSL_CONFIG = $ENV{"OPENSSL_CONFIG"} // "";
23e1051a39Sopenharmony_ci
24e1051a39Sopenharmony_ci# Command invocations.
25e1051a39Sopenharmony_cimy $REQ = "$openssl req $OPENSSL_CONFIG";
26e1051a39Sopenharmony_cimy $CA = "$openssl ca $OPENSSL_CONFIG";
27e1051a39Sopenharmony_cimy $VERIFY = "$openssl verify";
28e1051a39Sopenharmony_cimy $X509 = "$openssl x509";
29e1051a39Sopenharmony_cimy $PKCS12 = "$openssl pkcs12";
30e1051a39Sopenharmony_ci
31e1051a39Sopenharmony_ci# Default values for various configuration settings.
32e1051a39Sopenharmony_cimy $CATOP = "./demoCA";
33e1051a39Sopenharmony_cimy $CAKEY = "cakey.pem";
34e1051a39Sopenharmony_cimy $CAREQ = "careq.pem";
35e1051a39Sopenharmony_cimy $CACERT = "cacert.pem";
36e1051a39Sopenharmony_cimy $CACRL = "crl.pem";
37e1051a39Sopenharmony_cimy $DAYS = "-days 365";
38e1051a39Sopenharmony_cimy $CADAYS = "-days 1095";	# 3 years
39e1051a39Sopenharmony_cimy $NEWKEY = "newkey.pem";
40e1051a39Sopenharmony_cimy $NEWREQ = "newreq.pem";
41e1051a39Sopenharmony_cimy $NEWCERT = "newcert.pem";
42e1051a39Sopenharmony_cimy $NEWP12 = "newcert.p12";
43e1051a39Sopenharmony_ci
44e1051a39Sopenharmony_ci# Commandline parsing
45e1051a39Sopenharmony_cimy %EXTRA;
46e1051a39Sopenharmony_cimy $WHAT = shift @ARGV || "";
47e1051a39Sopenharmony_ci@ARGV = parse_extra(@ARGV);
48e1051a39Sopenharmony_cimy $RET = 0;
49e1051a39Sopenharmony_ci
50e1051a39Sopenharmony_ci# Split out "-extra-CMD value", and return new |@ARGV|. Fill in
51e1051a39Sopenharmony_ci# |EXTRA{CMD}| with list of values.
52e1051a39Sopenharmony_cisub parse_extra
53e1051a39Sopenharmony_ci{
54e1051a39Sopenharmony_ci    foreach ( @OPENSSL_CMDS ) {
55e1051a39Sopenharmony_ci        $EXTRA{$_} = '';
56e1051a39Sopenharmony_ci    }
57e1051a39Sopenharmony_ci
58e1051a39Sopenharmony_ci    my @result;
59e1051a39Sopenharmony_ci    while ( scalar(@_) > 0 ) {
60e1051a39Sopenharmony_ci        my $arg = shift;
61e1051a39Sopenharmony_ci        if ( $arg !~ m/-extra-([a-z0-9]+)/ ) {
62e1051a39Sopenharmony_ci            push @result, $arg;
63e1051a39Sopenharmony_ci            next;
64e1051a39Sopenharmony_ci        }
65e1051a39Sopenharmony_ci        $arg =~ s/-extra-//;
66e1051a39Sopenharmony_ci        die("Unknown \"-${arg}-extra\" option, exiting")
67e1051a39Sopenharmony_ci            unless scalar grep { $arg eq $_ } @OPENSSL_CMDS;
68e1051a39Sopenharmony_ci        $EXTRA{$arg} .= " " . shift;
69e1051a39Sopenharmony_ci    }
70e1051a39Sopenharmony_ci    return @result;
71e1051a39Sopenharmony_ci}
72e1051a39Sopenharmony_ci
73e1051a39Sopenharmony_ci
74e1051a39Sopenharmony_ci# See if reason for a CRL entry is valid; exit if not.
75e1051a39Sopenharmony_cisub crl_reason_ok
76e1051a39Sopenharmony_ci{
77e1051a39Sopenharmony_ci    my $r = shift;
78e1051a39Sopenharmony_ci
79e1051a39Sopenharmony_ci    if ($r eq 'unspecified' || $r eq 'keyCompromise'
80e1051a39Sopenharmony_ci        || $r eq 'CACompromise' || $r eq 'affiliationChanged'
81e1051a39Sopenharmony_ci        || $r eq 'superseded' || $r eq 'cessationOfOperation'
82e1051a39Sopenharmony_ci        || $r eq 'certificateHold' || $r eq 'removeFromCRL') {
83e1051a39Sopenharmony_ci        return 1;
84e1051a39Sopenharmony_ci    }
85e1051a39Sopenharmony_ci    print STDERR "Invalid CRL reason; must be one of:\n";
86e1051a39Sopenharmony_ci    print STDERR "    unspecified, keyCompromise, CACompromise,\n";
87e1051a39Sopenharmony_ci    print STDERR "    affiliationChanged, superseded, cessationOfOperation\n";
88e1051a39Sopenharmony_ci    print STDERR "    certificateHold, removeFromCRL";
89e1051a39Sopenharmony_ci    exit 1;
90e1051a39Sopenharmony_ci}
91e1051a39Sopenharmony_ci
92e1051a39Sopenharmony_ci# Copy a PEM-format file; return like exit status (zero means ok)
93e1051a39Sopenharmony_cisub copy_pemfile
94e1051a39Sopenharmony_ci{
95e1051a39Sopenharmony_ci    my ($infile, $outfile, $bound) = @_;
96e1051a39Sopenharmony_ci    my $found = 0;
97e1051a39Sopenharmony_ci
98e1051a39Sopenharmony_ci    open IN, $infile || die "Cannot open $infile, $!";
99e1051a39Sopenharmony_ci    open OUT, ">$outfile" || die "Cannot write to $outfile, $!";
100e1051a39Sopenharmony_ci    while (<IN>) {
101e1051a39Sopenharmony_ci        $found = 1 if /^-----BEGIN.*$bound/;
102e1051a39Sopenharmony_ci        print OUT $_ if $found;
103e1051a39Sopenharmony_ci        $found = 2, last if /^-----END.*$bound/;
104e1051a39Sopenharmony_ci    }
105e1051a39Sopenharmony_ci    close IN;
106e1051a39Sopenharmony_ci    close OUT;
107e1051a39Sopenharmony_ci    return $found == 2 ? 0 : 1;
108e1051a39Sopenharmony_ci}
109e1051a39Sopenharmony_ci
110e1051a39Sopenharmony_ci# Wrapper around system; useful for debugging.  Returns just the exit status
111e1051a39Sopenharmony_cisub run
112e1051a39Sopenharmony_ci{
113e1051a39Sopenharmony_ci    my $cmd = shift;
114e1051a39Sopenharmony_ci    print "====\n$cmd\n" if $verbose;
115e1051a39Sopenharmony_ci    my $status = system($cmd);
116e1051a39Sopenharmony_ci    print "==> $status\n====\n" if $verbose;
117e1051a39Sopenharmony_ci    return $status >> 8;
118e1051a39Sopenharmony_ci}
119e1051a39Sopenharmony_ci
120e1051a39Sopenharmony_ci
121e1051a39Sopenharmony_ciif ( $WHAT =~ /^(-\?|-h|-help)$/ ) {
122e1051a39Sopenharmony_ci    print STDERR <<EOF;
123e1051a39Sopenharmony_ciUsage:
124e1051a39Sopenharmony_ci    CA.pl -newcert | -newreq | -newreq-nodes | -xsign | -sign | -signCA | -signcert | -crl | -newca [-extra-cmd parameter]
125e1051a39Sopenharmony_ci    CA.pl -pkcs12 [certname]
126e1051a39Sopenharmony_ci    CA.pl -verify certfile ...
127e1051a39Sopenharmony_ci    CA.pl -revoke certfile [reason]
128e1051a39Sopenharmony_ciEOF
129e1051a39Sopenharmony_ci    exit 0;
130e1051a39Sopenharmony_ci}
131e1051a39Sopenharmony_ci
132e1051a39Sopenharmony_ciif ($WHAT eq '-newcert' ) {
133e1051a39Sopenharmony_ci    # create a certificate
134e1051a39Sopenharmony_ci    $RET = run("$REQ -new -x509 -keyout $NEWKEY -out $NEWCERT $DAYS"
135e1051a39Sopenharmony_ci            . " $EXTRA{req}");
136e1051a39Sopenharmony_ci    print "Cert is in $NEWCERT, private key is in $NEWKEY\n" if $RET == 0;
137e1051a39Sopenharmony_ci} elsif ($WHAT eq '-precert' ) {
138e1051a39Sopenharmony_ci    # create a pre-certificate
139e1051a39Sopenharmony_ci    $RET = run("$REQ -x509 -precert -keyout $NEWKEY -out $NEWCERT $DAYS"
140e1051a39Sopenharmony_ci            . " $EXTRA{req}");
141e1051a39Sopenharmony_ci    print "Pre-cert is in $NEWCERT, private key is in $NEWKEY\n" if $RET == 0;
142e1051a39Sopenharmony_ci} elsif ($WHAT =~ /^\-newreq(\-nodes)?$/ ) {
143e1051a39Sopenharmony_ci    # create a certificate request
144e1051a39Sopenharmony_ci    $RET = run("$REQ -new $1 -keyout $NEWKEY -out $NEWREQ $DAYS $EXTRA{req}");
145e1051a39Sopenharmony_ci    print "Request is in $NEWREQ, private key is in $NEWKEY\n" if $RET == 0;
146e1051a39Sopenharmony_ci} elsif ($WHAT eq '-newca' ) {
147e1051a39Sopenharmony_ci    # create the directory hierarchy
148e1051a39Sopenharmony_ci    my @dirs = ( "${CATOP}", "${CATOP}/certs", "${CATOP}/crl",
149e1051a39Sopenharmony_ci                "${CATOP}/newcerts", "${CATOP}/private" );
150e1051a39Sopenharmony_ci    die "${CATOP}/index.txt exists.\nRemove old sub-tree to proceed,"
151e1051a39Sopenharmony_ci        if -f "${CATOP}/index.txt";
152e1051a39Sopenharmony_ci    die "${CATOP}/serial exists.\nRemove old sub-tree to proceed,"
153e1051a39Sopenharmony_ci        if -f "${CATOP}/serial";
154e1051a39Sopenharmony_ci    foreach my $d ( @dirs ) {
155e1051a39Sopenharmony_ci        if ( -d $d ) {
156e1051a39Sopenharmony_ci            warn "Directory $d exists" if -d $d;
157e1051a39Sopenharmony_ci        } else {
158e1051a39Sopenharmony_ci            mkdir $d or die "Can't mkdir $d, $!";
159e1051a39Sopenharmony_ci        }
160e1051a39Sopenharmony_ci    }
161e1051a39Sopenharmony_ci
162e1051a39Sopenharmony_ci    open OUT, ">${CATOP}/index.txt";
163e1051a39Sopenharmony_ci    close OUT;
164e1051a39Sopenharmony_ci    open OUT, ">${CATOP}/crlnumber";
165e1051a39Sopenharmony_ci    print OUT "01\n";
166e1051a39Sopenharmony_ci    close OUT;
167e1051a39Sopenharmony_ci    # ask user for existing CA certificate
168e1051a39Sopenharmony_ci    print "CA certificate filename (or enter to create)\n";
169e1051a39Sopenharmony_ci    my $FILE;
170e1051a39Sopenharmony_ci    $FILE = "" unless defined($FILE = <STDIN>);
171e1051a39Sopenharmony_ci    $FILE =~ s{\R$}{};
172e1051a39Sopenharmony_ci    if ($FILE ne "") {
173e1051a39Sopenharmony_ci        copy_pemfile($FILE,"${CATOP}/private/$CAKEY", "PRIVATE");
174e1051a39Sopenharmony_ci        copy_pemfile($FILE,"${CATOP}/$CACERT", "CERTIFICATE");
175e1051a39Sopenharmony_ci    } else {
176e1051a39Sopenharmony_ci        print "Making CA certificate ...\n";
177e1051a39Sopenharmony_ci        $RET = run("$REQ -new -keyout ${CATOP}/private/$CAKEY"
178e1051a39Sopenharmony_ci                . " -out ${CATOP}/$CAREQ $EXTRA{req}");
179e1051a39Sopenharmony_ci        $RET = run("$CA -create_serial"
180e1051a39Sopenharmony_ci                . " -out ${CATOP}/$CACERT $CADAYS -batch"
181e1051a39Sopenharmony_ci                . " -keyfile ${CATOP}/private/$CAKEY -selfsign"
182e1051a39Sopenharmony_ci                . " -extensions v3_ca"
183e1051a39Sopenharmony_ci                . " -infiles ${CATOP}/$CAREQ $EXTRA{ca}") if $RET == 0;
184e1051a39Sopenharmony_ci        print "CA certificate is in ${CATOP}/$CACERT\n" if $RET == 0;
185e1051a39Sopenharmony_ci    }
186e1051a39Sopenharmony_ci} elsif ($WHAT eq '-pkcs12' ) {
187e1051a39Sopenharmony_ci    my $cname = $ARGV[0];
188e1051a39Sopenharmony_ci    $cname = "My Certificate" unless defined $cname;
189e1051a39Sopenharmony_ci    $RET = run("$PKCS12 -in $NEWCERT -inkey $NEWKEY"
190e1051a39Sopenharmony_ci            . " -certfile ${CATOP}/$CACERT -out $NEWP12"
191e1051a39Sopenharmony_ci            . " -export -name \"$cname\" $EXTRA{pkcs12}");
192e1051a39Sopenharmony_ci    print "PKCS #12 file is in $NEWP12\n" if $RET == 0;
193e1051a39Sopenharmony_ci} elsif ($WHAT eq '-xsign' ) {
194e1051a39Sopenharmony_ci    $RET = run("$CA -policy policy_anything -infiles $NEWREQ $EXTRA{ca}");
195e1051a39Sopenharmony_ci} elsif ($WHAT eq '-sign' ) {
196e1051a39Sopenharmony_ci    $RET = run("$CA -policy policy_anything -out $NEWCERT"
197e1051a39Sopenharmony_ci            . " -infiles $NEWREQ $EXTRA{ca}");
198e1051a39Sopenharmony_ci    print "Signed certificate is in $NEWCERT\n" if $RET == 0;
199e1051a39Sopenharmony_ci} elsif ($WHAT eq '-signCA' ) {
200e1051a39Sopenharmony_ci    $RET = run("$CA -policy policy_anything -out $NEWCERT"
201e1051a39Sopenharmony_ci            . " -extensions v3_ca -infiles $NEWREQ $EXTRA{ca}");
202e1051a39Sopenharmony_ci    print "Signed CA certificate is in $NEWCERT\n" if $RET == 0;
203e1051a39Sopenharmony_ci} elsif ($WHAT eq '-signcert' ) {
204e1051a39Sopenharmony_ci    $RET = run("$X509 -x509toreq -in $NEWREQ -signkey $NEWREQ"
205e1051a39Sopenharmony_ci            . " -out tmp.pem $EXTRA{x509}");
206e1051a39Sopenharmony_ci    $RET = run("$CA -policy policy_anything -out $NEWCERT"
207e1051a39Sopenharmony_ci            .  "-infiles tmp.pem $EXTRA{ca}") if $RET == 0;
208e1051a39Sopenharmony_ci    print "Signed certificate is in $NEWCERT\n" if $RET == 0;
209e1051a39Sopenharmony_ci} elsif ($WHAT eq '-verify' ) {
210e1051a39Sopenharmony_ci    my @files = @ARGV ? @ARGV : ( $NEWCERT );
211e1051a39Sopenharmony_ci    foreach my $file (@files) {
212e1051a39Sopenharmony_ci        # -CAfile quoted for VMS, since the C RTL downcases all unquoted
213e1051a39Sopenharmony_ci        # arguments to C programs
214e1051a39Sopenharmony_ci        my $status = run("$VERIFY \"-CAfile\" ${CATOP}/$CACERT $file $EXTRA{verify}");
215e1051a39Sopenharmony_ci        $RET = $status if $status != 0;
216e1051a39Sopenharmony_ci    }
217e1051a39Sopenharmony_ci} elsif ($WHAT eq '-crl' ) {
218e1051a39Sopenharmony_ci    $RET = run("$CA -gencrl -out ${CATOP}/crl/$CACRL $EXTRA{ca}");
219e1051a39Sopenharmony_ci    print "Generated CRL is in ${CATOP}/crl/$CACRL\n" if $RET == 0;
220e1051a39Sopenharmony_ci} elsif ($WHAT eq '-revoke' ) {
221e1051a39Sopenharmony_ci    my $cname = $ARGV[0];
222e1051a39Sopenharmony_ci    if (!defined $cname) {
223e1051a39Sopenharmony_ci        print "Certificate filename is required; reason optional.\n";
224e1051a39Sopenharmony_ci        exit 1;
225e1051a39Sopenharmony_ci    }
226e1051a39Sopenharmony_ci    my $reason = $ARGV[1];
227e1051a39Sopenharmony_ci    $reason = " -crl_reason $reason"
228e1051a39Sopenharmony_ci        if defined $reason && crl_reason_ok($reason);
229e1051a39Sopenharmony_ci    $RET = run("$CA -revoke \"$cname\"" . $reason . $EXTRA{ca});
230e1051a39Sopenharmony_ci} else {
231e1051a39Sopenharmony_ci    print STDERR "Unknown arg \"$WHAT\"\n";
232e1051a39Sopenharmony_ci    print STDERR "Use -help for help.\n";
233e1051a39Sopenharmony_ci    exit 1;
234e1051a39Sopenharmony_ci}
235e1051a39Sopenharmony_ci
236e1051a39Sopenharmony_ciexit $RET;
237