CA.pl.in 7.7 KB

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