CA.pl 7.9 KB

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