Richard Levitte | abe256e | 2018-03-06 20:35:30 +0100 | [diff] [blame] | 1 | #!{- $config{HASHBANGPERL} -} |
Matt Caswell | 38fc02a | 2021-06-17 13:24:59 +0100 | [diff] [blame] | 2 | # Copyright 2000-2021 The OpenSSL Project Authors. All Rights Reserved. |
Rich Salz | e0a6519 | 2016-04-19 22:10:43 -0400 | [diff] [blame] | 3 | # |
Richard Levitte | dffa752 | 2018-12-06 13:00:26 +0100 | [diff] [blame] | 4 | # Licensed under the Apache License 2.0 (the "License"). You may not use |
Rich Salz | e0a6519 | 2016-04-19 22:10:43 -0400 | [diff] [blame] | 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 | |
stephen | 8f3e97b | 1999-01-01 00:54:48 +0000 | [diff] [blame] | 9 | # |
Rich Salz | 5a3aa85 | 2015-04-30 21:44:40 -0400 | [diff] [blame] | 10 | # Wrapper around the ca to make it easier to use |
Richard Levitte | 9ab6fc5 | 2016-01-25 21:19:59 +0100 | [diff] [blame] | 11 | # |
| 12 | # {- join("\n# ", @autowarntext) -} |
stephen | 8f3e97b | 1999-01-01 00:54:48 +0000 | [diff] [blame] | 13 | |
Rich Salz | 5a3aa85 | 2015-04-30 21:44:40 -0400 | [diff] [blame] | 14 | use strict; |
| 15 | use warnings; |
| 16 | |
Rich Salz | 5a3aa85 | 2015-04-30 21:44:40 -0400 | [diff] [blame] | 17 | my $verbose = 1; |
Rich Salz | cab33af | 2020-03-16 15:53:00 -0400 | [diff] [blame] | 18 | my @OPENSSL_CMDS = ("req", "ca", "pkcs12", "x509", "verify"); |
stephen | 8f3e97b | 1999-01-01 00:54:48 +0000 | [diff] [blame] | 19 | |
Rich Salz | cab33af | 2020-03-16 15:53:00 -0400 | [diff] [blame] | 20 | my $openssl = $ENV{'OPENSSL'} // "openssl"; |
| 21 | $ENV{'OPENSSL'} = $openssl; |
| 22 | my $OPENSSL_CONFIG = $ENV{"OPENSSL_CONFIG"} // ""; |
| 23 | |
| 24 | # Command invocations. |
Rich Salz | b0700d2 | 2015-10-27 15:11:48 -0400 | [diff] [blame] | 25 | my $REQ = "$openssl req $OPENSSL_CONFIG"; |
| 26 | my $CA = "$openssl ca $OPENSSL_CONFIG"; |
Rich Salz | 5a3aa85 | 2015-04-30 21:44:40 -0400 | [diff] [blame] | 27 | my $VERIFY = "$openssl verify"; |
| 28 | my $X509 = "$openssl x509"; |
| 29 | my $PKCS12 = "$openssl pkcs12"; |
stephen | 8f3e97b | 1999-01-01 00:54:48 +0000 | [diff] [blame] | 30 | |
Rich Salz | cab33af | 2020-03-16 15:53:00 -0400 | [diff] [blame] | 31 | # Default values for various configuration settings. |
Rich Salz | 5a3aa85 | 2015-04-30 21:44:40 -0400 | [diff] [blame] | 32 | my $CATOP = "./demoCA"; |
| 33 | my $CAKEY = "cakey.pem"; |
| 34 | my $CAREQ = "careq.pem"; |
| 35 | my $CACERT = "cacert.pem"; |
| 36 | my $CACRL = "crl.pem"; |
Rich Salz | cab33af | 2020-03-16 15:53:00 -0400 | [diff] [blame] | 37 | my $DAYS = "-days 365"; |
| 38 | my $CADAYS = "-days 1095"; # 3 years |
Andrew Galante | 3066cf2 | 2021-01-08 13:27:49 -0800 | [diff] [blame] | 39 | my $EXTENSIONS = "-extensions v3_ca"; |
| 40 | my $POLICY = "-policy policy_anything"; |
Rich Salz | 5a3aa85 | 2015-04-30 21:44:40 -0400 | [diff] [blame] | 41 | my $NEWKEY = "newkey.pem"; |
| 42 | my $NEWREQ = "newreq.pem"; |
| 43 | my $NEWCERT = "newcert.pem"; |
| 44 | my $NEWP12 = "newcert.p12"; |
stephen | 8f3e97b | 1999-01-01 00:54:48 +0000 | [diff] [blame] | 45 | |
Rich Salz | cab33af | 2020-03-16 15:53:00 -0400 | [diff] [blame] | 46 | # Commandline parsing |
| 47 | my %EXTRA; |
| 48 | my $WHAT = shift @ARGV || ""; |
| 49 | @ARGV = parse_extra(@ARGV); |
| 50 | my $RET = 0; |
| 51 | |
| 52 | # Split out "-extra-CMD value", and return new |@ARGV|. Fill in |
| 53 | # |EXTRA{CMD}| with list of values. |
| 54 | sub 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 asplund | 022696c | 2016-10-28 10:01:02 +0300 | [diff] [blame] | 73 | } |
| 74 | |
Rich Salz | cab33af | 2020-03-16 15:53:00 -0400 | [diff] [blame] | 75 | |
Rich Salz | 5a3aa85 | 2015-04-30 21:44:40 -0400 | [diff] [blame] | 76 | # See if reason for a CRL entry is valid; exit if not. |
| 77 | sub crl_reason_ok |
| 78 | { |
| 79 | my $r = shift; |
stephen | 8f3e97b | 1999-01-01 00:54:48 +0000 | [diff] [blame] | 80 | |
Rich Salz | 5a3aa85 | 2015-04-30 21:44:40 -0400 | [diff] [blame] | 81 | 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 | } |
stephen | 8f3e97b | 1999-01-01 00:54:48 +0000 | [diff] [blame] | 93 | |
Rich Salz | 5a3aa85 | 2015-04-30 21:44:40 -0400 | [diff] [blame] | 94 | # Copy a PEM-format file; return like exit status (zero means ok) |
| 95 | sub 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 |
| 113 | sub 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 | |
| 123 | if ( $WHAT =~ /^(-\?|-h|-help)$/ ) { |
Rich Salz | cab33af | 2020-03-16 15:53:00 -0400 | [diff] [blame] | 124 | print STDERR <<EOF; |
| 125 | Usage: |
| 126 | CA.pl -newcert | -newreq | -newreq-nodes | -xsign | -sign | -signCA | -signcert | -crl | -newca [-extra-cmd parameter] |
Dr. David von Oheimb | 359efea | 2021-05-17 11:04:40 +0200 | [diff] [blame] | 127 | CA.pl -pkcs12 [certname] |
| 128 | CA.pl -verify certfile ... |
| 129 | CA.pl -revoke certfile [reason] |
Rich Salz | cab33af | 2020-03-16 15:53:00 -0400 | [diff] [blame] | 130 | EOF |
Rich Salz | 5a3aa85 | 2015-04-30 21:44:40 -0400 | [diff] [blame] | 131 | exit 0; |
| 132 | } |
Rich Salz | cab33af | 2020-03-16 15:53:00 -0400 | [diff] [blame] | 133 | |
Rich Salz | 5a3aa85 | 2015-04-30 21:44:40 -0400 | [diff] [blame] | 134 | if ($WHAT eq '-newcert' ) { |
| 135 | # create a certificate |
Rich Salz | cab33af | 2020-03-16 15:53:00 -0400 | [diff] [blame] | 136 | $RET = run("$REQ -new -x509 -keyout $NEWKEY -out $NEWCERT $DAYS" |
| 137 | . " $EXTRA{req}"); |
Rich Salz | 5a3aa85 | 2015-04-30 21:44:40 -0400 | [diff] [blame] | 138 | print "Cert is in $NEWCERT, private key is in $NEWKEY\n" if $RET == 0; |
Rob Percival | 505fb99 | 2017-01-13 19:06:03 +0000 | [diff] [blame] | 139 | } elsif ($WHAT eq '-precert' ) { |
Rob Percival | b6486bf | 2016-03-10 19:15:13 +0000 | [diff] [blame] | 140 | # create a pre-certificate |
Rich Salz | cab33af | 2020-03-16 15:53:00 -0400 | [diff] [blame] | 141 | $RET = run("$REQ -x509 -precert -keyout $NEWKEY -out $NEWCERT $DAYS" |
| 142 | . " $EXTRA{req}"); |
Rob Percival | b6486bf | 2016-03-10 19:15:13 +0000 | [diff] [blame] | 143 | print "Pre-cert is in $NEWCERT, private key is in $NEWKEY\n" if $RET == 0; |
Rich Salz | 32cd473 | 2017-10-07 14:59:18 -0400 | [diff] [blame] | 144 | } elsif ($WHAT =~ /^\-newreq(\-nodes)?$/ ) { |
Rich Salz | 5a3aa85 | 2015-04-30 21:44:40 -0400 | [diff] [blame] | 145 | # create a certificate request |
Rich Salz | fa4dd54 | 2017-10-06 11:06:12 -0400 | [diff] [blame] | 146 | $RET = run("$REQ -new $1 -keyout $NEWKEY -out $NEWREQ $DAYS $EXTRA{req}"); |
Rich Salz | 5a3aa85 | 2015-04-30 21:44:40 -0400 | [diff] [blame] | 147 | 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 Salz | cab33af | 2020-03-16 15:53:00 -0400 | [diff] [blame] | 150 | 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 Salz | 5a3aa85 | 2015-04-30 21:44:40 -0400 | [diff] [blame] | 164 | 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 Salz | cab33af | 2020-03-16 15:53:00 -0400 | [diff] [blame] | 171 | my $FILE; |
Viktor Dukhovni | ce3d25d | 2016-02-13 02:53:13 -0500 | [diff] [blame] | 172 | $FILE = "" unless defined($FILE = <STDIN>); |
| 173 | $FILE =~ s{\R$}{}; |
| 174 | if ($FILE ne "") { |
Rich Salz | 5a3aa85 | 2015-04-30 21:44:40 -0400 | [diff] [blame] | 175 | copy_pemfile($FILE,"${CATOP}/private/$CAKEY", "PRIVATE"); |
| 176 | copy_pemfile($FILE,"${CATOP}/$CACERT", "CERTIFICATE"); |
| 177 | } else { |
| 178 | print "Making CA certificate ...\n"; |
Rich Salz | cab33af | 2020-03-16 15:53:00 -0400 | [diff] [blame] | 179 | $RET = run("$REQ -new -keyout ${CATOP}/private/$CAKEY" |
marko asplund | 022696c | 2016-10-28 10:01:02 +0300 | [diff] [blame] | 180 | . " -out ${CATOP}/$CAREQ $EXTRA{req}"); |
Rich Salz | 5a3aa85 | 2015-04-30 21:44:40 -0400 | [diff] [blame] | 181 | $RET = run("$CA -create_serial" |
| 182 | . " -out ${CATOP}/$CACERT $CADAYS -batch" |
| 183 | . " -keyfile ${CATOP}/private/$CAKEY -selfsign" |
Andrew Galante | 3066cf2 | 2021-01-08 13:27:49 -0800 | [diff] [blame] | 184 | . " $EXTENSIONS" |
Rich Salz | cab33af | 2020-03-16 15:53:00 -0400 | [diff] [blame] | 185 | . " -infiles ${CATOP}/$CAREQ $EXTRA{ca}") if $RET == 0; |
Rich Salz | 5a3aa85 | 2015-04-30 21:44:40 -0400 | [diff] [blame] | 186 | print "CA certificate is in ${CATOP}/$CACERT\n" if $RET == 0; |
| 187 | } |
| 188 | } elsif ($WHAT eq '-pkcs12' ) { |
Markus Sauermann | 1e2804f | 2017-12-03 13:23:21 +0100 | [diff] [blame] | 189 | my $cname = $ARGV[0]; |
Rich Salz | 5a3aa85 | 2015-04-30 21:44:40 -0400 | [diff] [blame] | 190 | $cname = "My Certificate" unless defined $cname; |
| 191 | $RET = run("$PKCS12 -in $NEWCERT -inkey $NEWKEY" |
Rich Salz | cab33af | 2020-03-16 15:53:00 -0400 | [diff] [blame] | 192 | . " -certfile ${CATOP}/$CACERT -out $NEWP12" |
marko asplund | 022696c | 2016-10-28 10:01:02 +0300 | [diff] [blame] | 193 | . " -export -name \"$cname\" $EXTRA{pkcs12}"); |
Rich Salz | 5a3aa85 | 2015-04-30 21:44:40 -0400 | [diff] [blame] | 194 | print "PKCS #12 file is in $NEWP12\n" if $RET == 0; |
| 195 | } elsif ($WHAT eq '-xsign' ) { |
Andrew Galante | 3066cf2 | 2021-01-08 13:27:49 -0800 | [diff] [blame] | 196 | $RET = run("$CA $POLICY -infiles $NEWREQ $EXTRA{ca}"); |
Rich Salz | 5a3aa85 | 2015-04-30 21:44:40 -0400 | [diff] [blame] | 197 | } elsif ($WHAT eq '-sign' ) { |
Andrew Galante | 3066cf2 | 2021-01-08 13:27:49 -0800 | [diff] [blame] | 198 | $RET = run("$CA $POLICY -out $NEWCERT" |
Rich Salz | cab33af | 2020-03-16 15:53:00 -0400 | [diff] [blame] | 199 | . " -infiles $NEWREQ $EXTRA{ca}"); |
Rich Salz | 5a3aa85 | 2015-04-30 21:44:40 -0400 | [diff] [blame] | 200 | print "Signed certificate is in $NEWCERT\n" if $RET == 0; |
| 201 | } elsif ($WHAT eq '-signCA' ) { |
Andrew Galante | 3066cf2 | 2021-01-08 13:27:49 -0800 | [diff] [blame] | 202 | $RET = run("$CA $POLICY -out $NEWCERT" |
| 203 | . " $EXTENSIONS -infiles $NEWREQ $EXTRA{ca}"); |
Rich Salz | 5a3aa85 | 2015-04-30 21:44:40 -0400 | [diff] [blame] | 204 | 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 asplund | 022696c | 2016-10-28 10:01:02 +0300 | [diff] [blame] | 207 | . " -out tmp.pem $EXTRA{x509}"); |
Andrew Galante | 3066cf2 | 2021-01-08 13:27:49 -0800 | [diff] [blame] | 208 | $RET = run("$CA $POLICY -out $NEWCERT" |
Rich Salz | cab33af | 2020-03-16 15:53:00 -0400 | [diff] [blame] | 209 | . "-infiles tmp.pem $EXTRA{ca}") if $RET == 0; |
Rich Salz | 5a3aa85 | 2015-04-30 21:44:40 -0400 | [diff] [blame] | 210 | print "Signed certificate is in $NEWCERT\n" if $RET == 0; |
| 211 | } elsif ($WHAT eq '-verify' ) { |
Rich Salz | 33fbca8 | 2015-05-01 07:11:17 -0400 | [diff] [blame] | 212 | my @files = @ARGV ? @ARGV : ( $NEWCERT ); |
Rich Salz | cab33af | 2020-03-16 15:53:00 -0400 | [diff] [blame] | 213 | foreach my $file (@files) { |
Richard Levitte | f49b42e | 2021-06-22 10:52:09 +0200 | [diff] [blame] | 214 | # -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 Salz | 5a3aa85 | 2015-04-30 21:44:40 -0400 | [diff] [blame] | 217 | $RET = $status if $status != 0; |
| 218 | } |
| 219 | } elsif ($WHAT eq '-crl' ) { |
marko asplund | 022696c | 2016-10-28 10:01:02 +0300 | [diff] [blame] | 220 | $RET = run("$CA -gencrl -out ${CATOP}/crl/$CACRL $EXTRA{ca}"); |
Rich Salz | 5a3aa85 | 2015-04-30 21:44:40 -0400 | [diff] [blame] | 221 | print "Generated CRL is in ${CATOP}/crl/$CACRL\n" if $RET == 0; |
| 222 | } elsif ($WHAT eq '-revoke' ) { |
Markus Sauermann | 1e2804f | 2017-12-03 13:23:21 +0100 | [diff] [blame] | 223 | my $cname = $ARGV[0]; |
Rich Salz | 5a3aa85 | 2015-04-30 21:44:40 -0400 | [diff] [blame] | 224 | if (!defined $cname) { |
| 225 | print "Certificate filename is required; reason optional.\n"; |
| 226 | exit 1; |
| 227 | } |
Markus Sauermann | 1e2804f | 2017-12-03 13:23:21 +0100 | [diff] [blame] | 228 | my $reason = $ARGV[1]; |
Rich Salz | 5a3aa85 | 2015-04-30 21:44:40 -0400 | [diff] [blame] | 229 | $reason = " -crl_reason $reason" |
| 230 | if defined $reason && crl_reason_ok($reason); |
marko asplund | 022696c | 2016-10-28 10:01:02 +0300 | [diff] [blame] | 231 | $RET = run("$CA -revoke \"$cname\"" . $reason . $EXTRA{ca}); |
Rich Salz | 5a3aa85 | 2015-04-30 21:44:40 -0400 | [diff] [blame] | 232 | } else { |
| 233 | print STDERR "Unknown arg \"$WHAT\"\n"; |
| 234 | print STDERR "Use -help for help.\n"; |
| 235 | exit 1; |
stephen | 8f3e97b | 1999-01-01 00:54:48 +0000 | [diff] [blame] | 236 | } |
| 237 | |
| 238 | exit $RET; |