blob: 2c31ee6c8de13afb838b33dab6e8f8e6244bd231 [file] [log] [blame]
Richard Levitteabe256e2018-03-06 20:35:30 +01001#!{- $config{HASHBANGPERL} -}
Matt Caswell38fc02a2021-06-17 13:24:59 +01002# Copyright 2000-2021 The OpenSSL Project Authors. All Rights Reserved.
Rich Salze0a65192016-04-19 22:10:43 -04003#
Richard Levittedffa7522018-12-06 13:00:26 +01004# Licensed under the Apache License 2.0 (the "License"). You may not use
Rich Salze0a65192016-04-19 22:10:43 -04005# 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
stephen8f3e97b1999-01-01 00:54:48 +00009#
Rich Salz5a3aa852015-04-30 21:44:40 -040010# Wrapper around the ca to make it easier to use
Richard Levitte9ab6fc52016-01-25 21:19:59 +010011#
12# {- join("\n# ", @autowarntext) -}
stephen8f3e97b1999-01-01 00:54:48 +000013
Rich Salz5a3aa852015-04-30 21:44:40 -040014use strict;
15use warnings;
16
Rich Salz5a3aa852015-04-30 21:44:40 -040017my $verbose = 1;
Rich Salzcab33af2020-03-16 15:53:00 -040018my @OPENSSL_CMDS = ("req", "ca", "pkcs12", "x509", "verify");
stephen8f3e97b1999-01-01 00:54:48 +000019
Rich Salzcab33af2020-03-16 15:53:00 -040020my $openssl = $ENV{'OPENSSL'} // "openssl";
21$ENV{'OPENSSL'} = $openssl;
22my $OPENSSL_CONFIG = $ENV{"OPENSSL_CONFIG"} // "";
23
24# Command invocations.
Rich Salzb0700d22015-10-27 15:11:48 -040025my $REQ = "$openssl req $OPENSSL_CONFIG";
26my $CA = "$openssl ca $OPENSSL_CONFIG";
Rich Salz5a3aa852015-04-30 21:44:40 -040027my $VERIFY = "$openssl verify";
28my $X509 = "$openssl x509";
29my $PKCS12 = "$openssl pkcs12";
stephen8f3e97b1999-01-01 00:54:48 +000030
Rich Salzcab33af2020-03-16 15:53:00 -040031# Default values for various configuration settings.
Rich Salz5a3aa852015-04-30 21:44:40 -040032my $CATOP = "./demoCA";
33my $CAKEY = "cakey.pem";
34my $CAREQ = "careq.pem";
35my $CACERT = "cacert.pem";
36my $CACRL = "crl.pem";
Rich Salzcab33af2020-03-16 15:53:00 -040037my $DAYS = "-days 365";
38my $CADAYS = "-days 1095"; # 3 years
Andrew Galante3066cf22021-01-08 13:27:49 -080039my $EXTENSIONS = "-extensions v3_ca";
40my $POLICY = "-policy policy_anything";
Rich Salz5a3aa852015-04-30 21:44:40 -040041my $NEWKEY = "newkey.pem";
42my $NEWREQ = "newreq.pem";
43my $NEWCERT = "newcert.pem";
44my $NEWP12 = "newcert.p12";
stephen8f3e97b1999-01-01 00:54:48 +000045
Rich Salzcab33af2020-03-16 15:53:00 -040046# Commandline parsing
47my %EXTRA;
48my $WHAT = shift @ARGV || "";
49@ARGV = parse_extra(@ARGV);
50my $RET = 0;
51
52# Split out "-extra-CMD value", and return new |@ARGV|. Fill in
53# |EXTRA{CMD}| with list of values.
54sub parse_extra
55{
56 foreach ( @OPENSSL_CMDS ) {
57 $EXTRA{$_} = '';
58 }
59
60 my @result;
61 while ( scalar(@_) > 0 ) {
62 my $arg = shift;
63 if ( $arg !~ m/-extra-([a-z0-9]+)/ ) {
64 push @result, $arg;
65 next;
66 }
67 $arg =~ s/-extra-//;
68 die("Unknown \"-${arg}-extra\" option, exiting")
69 unless scalar grep { $arg eq $_ } @OPENSSL_CMDS;
70 $EXTRA{$arg} .= " " . shift;
71 }
72 return @result;
marko asplund022696c2016-10-28 10:01:02 +030073}
74
Rich Salzcab33af2020-03-16 15:53:00 -040075
Rich Salz5a3aa852015-04-30 21:44:40 -040076# See if reason for a CRL entry is valid; exit if not.
77sub crl_reason_ok
78{
79 my $r = shift;
stephen8f3e97b1999-01-01 00:54:48 +000080
Rich Salz5a3aa852015-04-30 21:44:40 -040081 if ($r eq 'unspecified' || $r eq 'keyCompromise'
82 || $r eq 'CACompromise' || $r eq 'affiliationChanged'
83 || $r eq 'superseded' || $r eq 'cessationOfOperation'
84 || $r eq 'certificateHold' || $r eq 'removeFromCRL') {
85 return 1;
86 }
87 print STDERR "Invalid CRL reason; must be one of:\n";
88 print STDERR " unspecified, keyCompromise, CACompromise,\n";
89 print STDERR " affiliationChanged, superseded, cessationOfOperation\n";
90 print STDERR " certificateHold, removeFromCRL";
91 exit 1;
92}
stephen8f3e97b1999-01-01 00:54:48 +000093
Rich Salz5a3aa852015-04-30 21:44:40 -040094# Copy a PEM-format file; return like exit status (zero means ok)
95sub copy_pemfile
96{
97 my ($infile, $outfile, $bound) = @_;
98 my $found = 0;
99
100 open IN, $infile || die "Cannot open $infile, $!";
101 open OUT, ">$outfile" || die "Cannot write to $outfile, $!";
102 while (<IN>) {
103 $found = 1 if /^-----BEGIN.*$bound/;
104 print OUT $_ if $found;
105 $found = 2, last if /^-----END.*$bound/;
106 }
107 close IN;
108 close OUT;
109 return $found == 2 ? 0 : 1;
110}
111
112# Wrapper around system; useful for debugging. Returns just the exit status
113sub run
114{
115 my $cmd = shift;
116 print "====\n$cmd\n" if $verbose;
117 my $status = system($cmd);
118 print "==> $status\n====\n" if $verbose;
119 return $status >> 8;
120}
121
122
123if ( $WHAT =~ /^(-\?|-h|-help)$/ ) {
Rich Salzcab33af2020-03-16 15:53:00 -0400124 print STDERR <<EOF;
125Usage:
126 CA.pl -newcert | -newreq | -newreq-nodes | -xsign | -sign | -signCA | -signcert | -crl | -newca [-extra-cmd parameter]
Dr. David von Oheimb359efea2021-05-17 11:04:40 +0200127 CA.pl -pkcs12 [certname]
128 CA.pl -verify certfile ...
129 CA.pl -revoke certfile [reason]
Rich Salzcab33af2020-03-16 15:53:00 -0400130EOF
Rich Salz5a3aa852015-04-30 21:44:40 -0400131 exit 0;
132}
Rich Salzcab33af2020-03-16 15:53:00 -0400133
Rich Salz5a3aa852015-04-30 21:44:40 -0400134if ($WHAT eq '-newcert' ) {
135 # create a certificate
Rich Salzcab33af2020-03-16 15:53:00 -0400136 $RET = run("$REQ -new -x509 -keyout $NEWKEY -out $NEWCERT $DAYS"
137 . " $EXTRA{req}");
Rich Salz5a3aa852015-04-30 21:44:40 -0400138 print "Cert is in $NEWCERT, private key is in $NEWKEY\n" if $RET == 0;
Rob Percival505fb992017-01-13 19:06:03 +0000139} elsif ($WHAT eq '-precert' ) {
Rob Percivalb6486bf2016-03-10 19:15:13 +0000140 # create a pre-certificate
Rich Salzcab33af2020-03-16 15:53:00 -0400141 $RET = run("$REQ -x509 -precert -keyout $NEWKEY -out $NEWCERT $DAYS"
142 . " $EXTRA{req}");
Rob Percivalb6486bf2016-03-10 19:15:13 +0000143 print "Pre-cert is in $NEWCERT, private key is in $NEWKEY\n" if $RET == 0;
Rich Salz32cd4732017-10-07 14:59:18 -0400144} elsif ($WHAT =~ /^\-newreq(\-nodes)?$/ ) {
Rich Salz5a3aa852015-04-30 21:44:40 -0400145 # create a certificate request
Rich Salzfa4dd542017-10-06 11:06:12 -0400146 $RET = run("$REQ -new $1 -keyout $NEWKEY -out $NEWREQ $DAYS $EXTRA{req}");
Rich Salz5a3aa852015-04-30 21:44:40 -0400147 print "Request is in $NEWREQ, private key is in $NEWKEY\n" if $RET == 0;
148} elsif ($WHAT eq '-newca' ) {
149 # create the directory hierarchy
Rich Salzcab33af2020-03-16 15:53:00 -0400150 my @dirs = ( "${CATOP}", "${CATOP}/certs", "${CATOP}/crl",
151 "${CATOP}/newcerts", "${CATOP}/private" );
152 die "${CATOP}/index.txt exists.\nRemove old sub-tree to proceed,"
153 if -f "${CATOP}/index.txt";
154 die "${CATOP}/serial exists.\nRemove old sub-tree to proceed,"
155 if -f "${CATOP}/serial";
156 foreach my $d ( @dirs ) {
157 if ( -d $d ) {
158 warn "Directory $d exists" if -d $d;
159 } else {
160 mkdir $d or die "Can't mkdir $d, $!";
161 }
162 }
163
Rich Salz5a3aa852015-04-30 21:44:40 -0400164 open OUT, ">${CATOP}/index.txt";
165 close OUT;
166 open OUT, ">${CATOP}/crlnumber";
167 print OUT "01\n";
168 close OUT;
169 # ask user for existing CA certificate
170 print "CA certificate filename (or enter to create)\n";
Rich Salzcab33af2020-03-16 15:53:00 -0400171 my $FILE;
Viktor Dukhovnice3d25d2016-02-13 02:53:13 -0500172 $FILE = "" unless defined($FILE = <STDIN>);
173 $FILE =~ s{\R$}{};
174 if ($FILE ne "") {
Rich Salz5a3aa852015-04-30 21:44:40 -0400175 copy_pemfile($FILE,"${CATOP}/private/$CAKEY", "PRIVATE");
176 copy_pemfile($FILE,"${CATOP}/$CACERT", "CERTIFICATE");
177 } else {
178 print "Making CA certificate ...\n";
Rich Salzcab33af2020-03-16 15:53:00 -0400179 $RET = run("$REQ -new -keyout ${CATOP}/private/$CAKEY"
marko asplund022696c2016-10-28 10:01:02 +0300180 . " -out ${CATOP}/$CAREQ $EXTRA{req}");
Rich Salz5a3aa852015-04-30 21:44:40 -0400181 $RET = run("$CA -create_serial"
182 . " -out ${CATOP}/$CACERT $CADAYS -batch"
183 . " -keyfile ${CATOP}/private/$CAKEY -selfsign"
Andrew Galante3066cf22021-01-08 13:27:49 -0800184 . " $EXTENSIONS"
Rich Salzcab33af2020-03-16 15:53:00 -0400185 . " -infiles ${CATOP}/$CAREQ $EXTRA{ca}") if $RET == 0;
Rich Salz5a3aa852015-04-30 21:44:40 -0400186 print "CA certificate is in ${CATOP}/$CACERT\n" if $RET == 0;
187 }
188} elsif ($WHAT eq '-pkcs12' ) {
Markus Sauermann1e2804f2017-12-03 13:23:21 +0100189 my $cname = $ARGV[0];
Rich Salz5a3aa852015-04-30 21:44:40 -0400190 $cname = "My Certificate" unless defined $cname;
191 $RET = run("$PKCS12 -in $NEWCERT -inkey $NEWKEY"
Rich Salzcab33af2020-03-16 15:53:00 -0400192 . " -certfile ${CATOP}/$CACERT -out $NEWP12"
marko asplund022696c2016-10-28 10:01:02 +0300193 . " -export -name \"$cname\" $EXTRA{pkcs12}");
Rich Salz5a3aa852015-04-30 21:44:40 -0400194 print "PKCS #12 file is in $NEWP12\n" if $RET == 0;
195} elsif ($WHAT eq '-xsign' ) {
Andrew Galante3066cf22021-01-08 13:27:49 -0800196 $RET = run("$CA $POLICY -infiles $NEWREQ $EXTRA{ca}");
Rich Salz5a3aa852015-04-30 21:44:40 -0400197} elsif ($WHAT eq '-sign' ) {
Andrew Galante3066cf22021-01-08 13:27:49 -0800198 $RET = run("$CA $POLICY -out $NEWCERT"
Rich Salzcab33af2020-03-16 15:53:00 -0400199 . " -infiles $NEWREQ $EXTRA{ca}");
Rich Salz5a3aa852015-04-30 21:44:40 -0400200 print "Signed certificate is in $NEWCERT\n" if $RET == 0;
201} elsif ($WHAT eq '-signCA' ) {
Andrew Galante3066cf22021-01-08 13:27:49 -0800202 $RET = run("$CA $POLICY -out $NEWCERT"
203 . " $EXTENSIONS -infiles $NEWREQ $EXTRA{ca}");
Rich Salz5a3aa852015-04-30 21:44:40 -0400204 print "Signed CA certificate is in $NEWCERT\n" if $RET == 0;
205} elsif ($WHAT eq '-signcert' ) {
206 $RET = run("$X509 -x509toreq -in $NEWREQ -signkey $NEWREQ"
marko asplund022696c2016-10-28 10:01:02 +0300207 . " -out tmp.pem $EXTRA{x509}");
Andrew Galante3066cf22021-01-08 13:27:49 -0800208 $RET = run("$CA $POLICY -out $NEWCERT"
Rich Salzcab33af2020-03-16 15:53:00 -0400209 . "-infiles tmp.pem $EXTRA{ca}") if $RET == 0;
Rich Salz5a3aa852015-04-30 21:44:40 -0400210 print "Signed certificate is in $NEWCERT\n" if $RET == 0;
211} elsif ($WHAT eq '-verify' ) {
Rich Salz33fbca82015-05-01 07:11:17 -0400212 my @files = @ARGV ? @ARGV : ( $NEWCERT );
Rich Salzcab33af2020-03-16 15:53:00 -0400213 foreach my $file (@files) {
Richard Levittef49b42e2021-06-22 10:52:09 +0200214 # -CAfile quoted for VMS, since the C RTL downcases all unquoted
215 # arguments to C programs
216 my $status = run("$VERIFY \"-CAfile\" ${CATOP}/$CACERT $file $EXTRA{verify}");
Rich Salz5a3aa852015-04-30 21:44:40 -0400217 $RET = $status if $status != 0;
218 }
219} elsif ($WHAT eq '-crl' ) {
marko asplund022696c2016-10-28 10:01:02 +0300220 $RET = run("$CA -gencrl -out ${CATOP}/crl/$CACRL $EXTRA{ca}");
Rich Salz5a3aa852015-04-30 21:44:40 -0400221 print "Generated CRL is in ${CATOP}/crl/$CACRL\n" if $RET == 0;
222} elsif ($WHAT eq '-revoke' ) {
Markus Sauermann1e2804f2017-12-03 13:23:21 +0100223 my $cname = $ARGV[0];
Rich Salz5a3aa852015-04-30 21:44:40 -0400224 if (!defined $cname) {
225 print "Certificate filename is required; reason optional.\n";
226 exit 1;
227 }
Markus Sauermann1e2804f2017-12-03 13:23:21 +0100228 my $reason = $ARGV[1];
Rich Salz5a3aa852015-04-30 21:44:40 -0400229 $reason = " -crl_reason $reason"
230 if defined $reason && crl_reason_ok($reason);
marko asplund022696c2016-10-28 10:01:02 +0300231 $RET = run("$CA -revoke \"$cname\"" . $reason . $EXTRA{ca});
Rich Salz5a3aa852015-04-30 21:44:40 -0400232} else {
233 print STDERR "Unknown arg \"$WHAT\"\n";
234 print STDERR "Use -help for help.\n";
235 exit 1;
stephen8f3e97b1999-01-01 00:54:48 +0000236}
237
238exit $RET;