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