CA.pl 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  1. #!/usr/bin/perl
  2. # Copyright 2000-2018 The OpenSSL Project Authors. All Rights Reserved.
  3. #
  4. # Licensed under the OpenSSL license (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 $openssl = "openssl";
  16. if(defined $ENV{'OPENSSL'}) {
  17. $openssl = $ENV{'OPENSSL'};
  18. } else {
  19. $ENV{'OPENSSL'} = $openssl;
  20. }
  21. my $verbose = 1;
  22. my $OPENSSL_CONFIG = $ENV{"OPENSSL_CONFIG"} || "";
  23. my $DAYS = "-days 365";
  24. my $CADAYS = "-days 1095"; # 3 years
  25. my $REQ = "$openssl req $OPENSSL_CONFIG";
  26. my $CA = "$openssl ca $OPENSSL_CONFIG";
  27. my $VERIFY = "$openssl verify";
  28. my $X509 = "$openssl x509";
  29. my $PKCS12 = "$openssl pkcs12";
  30. # default openssl.cnf file has setup as per the following
  31. my $CATOP = "./demoCA";
  32. my $CAKEY = "cakey.pem";
  33. my $CAREQ = "careq.pem";
  34. my $CACERT = "cacert.pem";
  35. my $CACRL = "crl.pem";
  36. my $DIRMODE = 0777;
  37. my $NEWKEY = "newkey.pem";
  38. my $NEWREQ = "newreq.pem";
  39. my $NEWCERT = "newcert.pem";
  40. my $NEWP12 = "newcert.p12";
  41. my $RET = 0;
  42. my $WHAT = shift @ARGV || "";
  43. my @OPENSSL_CMDS = ("req", "ca", "pkcs12", "x509", "verify");
  44. my %EXTRA = extra_args(\@ARGV, "-extra-");
  45. my $FILE;
  46. sub extra_args {
  47. my ($args_ref, $arg_prefix) = @_;
  48. my %eargs = map {
  49. if ($_ < $#$args_ref) {
  50. my ($arg, $value) = splice(@$args_ref, $_, 2);
  51. $arg =~ s/$arg_prefix//;
  52. ($arg, $value);
  53. } else {
  54. ();
  55. }
  56. } reverse grep($$args_ref[$_] =~ /$arg_prefix/, 0..$#$args_ref);
  57. my %empty = map { ($_, "") } @OPENSSL_CMDS;
  58. return (%empty, %eargs);
  59. }
  60. # See if reason for a CRL entry is valid; exit if not.
  61. sub crl_reason_ok
  62. {
  63. my $r = shift;
  64. if ($r eq 'unspecified' || $r eq 'keyCompromise'
  65. || $r eq 'CACompromise' || $r eq 'affiliationChanged'
  66. || $r eq 'superseded' || $r eq 'cessationOfOperation'
  67. || $r eq 'certificateHold' || $r eq 'removeFromCRL') {
  68. return 1;
  69. }
  70. print STDERR "Invalid CRL reason; must be one of:\n";
  71. print STDERR " unspecified, keyCompromise, CACompromise,\n";
  72. print STDERR " affiliationChanged, superseded, cessationOfOperation\n";
  73. print STDERR " certificateHold, removeFromCRL";
  74. exit 1;
  75. }
  76. # Copy a PEM-format file; return like exit status (zero means ok)
  77. sub copy_pemfile
  78. {
  79. my ($infile, $outfile, $bound) = @_;
  80. my $found = 0;
  81. open IN, $infile || die "Cannot open $infile, $!";
  82. open OUT, ">$outfile" || die "Cannot write to $outfile, $!";
  83. while (<IN>) {
  84. $found = 1 if /^-----BEGIN.*$bound/;
  85. print OUT $_ if $found;
  86. $found = 2, last if /^-----END.*$bound/;
  87. }
  88. close IN;
  89. close OUT;
  90. return $found == 2 ? 0 : 1;
  91. }
  92. # Wrapper around system; useful for debugging. Returns just the exit status
  93. sub run
  94. {
  95. my $cmd = shift;
  96. print "====\n$cmd\n" if $verbose;
  97. my $status = system($cmd);
  98. print "==> $status\n====\n" if $verbose;
  99. return $status >> 8;
  100. }
  101. if ( $WHAT =~ /^(-\?|-h|-help)$/ ) {
  102. print STDERR "usage: CA.pl -newcert | -newreq | -newreq-nodes | -xsign | -sign | -signCA | -signcert | -crl | -newca [-extra-cmd extra-params]\n";
  103. print STDERR " CA.pl -pkcs12 [-extra-pkcs12 extra-params] [certname]\n";
  104. print STDERR " CA.pl -verify [-extra-verify extra-params] certfile ...\n";
  105. print STDERR " CA.pl -revoke [-extra-ca extra-params] certfile [reason]\n";
  106. exit 0;
  107. }
  108. if ($WHAT eq '-newcert' ) {
  109. # create a certificate
  110. $RET = run("$REQ -new -x509 -keyout $NEWKEY -out $NEWCERT $DAYS $EXTRA{req}");
  111. print "Cert is in $NEWCERT, private key is in $NEWKEY\n" if $RET == 0;
  112. } elsif ($WHAT eq '-precert' ) {
  113. # create a pre-certificate
  114. $RET = run("$REQ -x509 -precert -keyout $NEWKEY -out $NEWCERT $DAYS");
  115. print "Pre-cert is in $NEWCERT, private key is in $NEWKEY\n" if $RET == 0;
  116. } elsif ($WHAT =~ /^\-newreq(\-nodes)?$/ ) {
  117. # create a certificate request
  118. $RET = run("$REQ -new $1 -keyout $NEWKEY -out $NEWREQ $DAYS $EXTRA{req}");
  119. print "Request is in $NEWREQ, private key is in $NEWKEY\n" if $RET == 0;
  120. } elsif ($WHAT eq '-newca' ) {
  121. # create the directory hierarchy
  122. mkdir ${CATOP}, $DIRMODE;
  123. mkdir "${CATOP}/certs", $DIRMODE;
  124. mkdir "${CATOP}/crl", $DIRMODE ;
  125. mkdir "${CATOP}/newcerts", $DIRMODE;
  126. mkdir "${CATOP}/private", $DIRMODE;
  127. open OUT, ">${CATOP}/index.txt";
  128. close OUT;
  129. open OUT, ">${CATOP}/crlnumber";
  130. print OUT "01\n";
  131. close OUT;
  132. # ask user for existing CA certificate
  133. print "CA certificate filename (or enter to create)\n";
  134. $FILE = "" unless defined($FILE = <STDIN>);
  135. $FILE =~ s{\R$}{};
  136. if ($FILE ne "") {
  137. copy_pemfile($FILE,"${CATOP}/private/$CAKEY", "PRIVATE");
  138. copy_pemfile($FILE,"${CATOP}/$CACERT", "CERTIFICATE");
  139. } else {
  140. print "Making CA certificate ...\n";
  141. $RET = run("$REQ -new -keyout"
  142. . " ${CATOP}/private/$CAKEY"
  143. . " -out ${CATOP}/$CAREQ $EXTRA{req}");
  144. $RET = run("$CA -create_serial"
  145. . " -out ${CATOP}/$CACERT $CADAYS -batch"
  146. . " -keyfile ${CATOP}/private/$CAKEY -selfsign"
  147. . " -extensions v3_ca $EXTRA{ca}"
  148. . " -infiles ${CATOP}/$CAREQ") if $RET == 0;
  149. print "CA certificate is in ${CATOP}/$CACERT\n" if $RET == 0;
  150. }
  151. } elsif ($WHAT eq '-pkcs12' ) {
  152. my $cname = $ARGV[0];
  153. $cname = "My Certificate" unless defined $cname;
  154. $RET = run("$PKCS12 -in $NEWCERT -inkey $NEWKEY"
  155. . " -certfile ${CATOP}/$CACERT"
  156. . " -out $NEWP12"
  157. . " -export -name \"$cname\" $EXTRA{pkcs12}");
  158. print "PKCS #12 file is in $NEWP12\n" if $RET == 0;
  159. } elsif ($WHAT eq '-xsign' ) {
  160. $RET = run("$CA -policy policy_anything $EXTRA{ca} -infiles $NEWREQ");
  161. } elsif ($WHAT eq '-sign' ) {
  162. $RET = run("$CA -policy policy_anything -out $NEWCERT $EXTRA{ca} -infiles $NEWREQ");
  163. print "Signed certificate is in $NEWCERT\n" if $RET == 0;
  164. } elsif ($WHAT eq '-signCA' ) {
  165. $RET = run("$CA -policy policy_anything -out $NEWCERT"
  166. . " -extensions v3_ca $EXTRA{ca} -infiles $NEWREQ");
  167. print "Signed CA certificate is in $NEWCERT\n" if $RET == 0;
  168. } elsif ($WHAT eq '-signcert' ) {
  169. $RET = run("$X509 -x509toreq -in $NEWREQ -signkey $NEWREQ"
  170. . " -out tmp.pem $EXTRA{x509}");
  171. $RET = run("$CA -policy policy_anything -out $NEWCERT"
  172. . "$EXTRA{ca} -infiles tmp.pem") if $RET == 0;
  173. print "Signed certificate is in $NEWCERT\n" if $RET == 0;
  174. } elsif ($WHAT eq '-verify' ) {
  175. my @files = @ARGV ? @ARGV : ( $NEWCERT );
  176. my $file;
  177. foreach $file (@files) {
  178. my $status = run("$VERIFY \"-CAfile\" ${CATOP}/$CACERT $file $EXTRA{verify}");
  179. $RET = $status if $status != 0;
  180. }
  181. } elsif ($WHAT eq '-crl' ) {
  182. $RET = run("$CA -gencrl -out ${CATOP}/crl/$CACRL $EXTRA{ca}");
  183. print "Generated CRL is in ${CATOP}/crl/$CACRL\n" if $RET == 0;
  184. } elsif ($WHAT eq '-revoke' ) {
  185. my $cname = $ARGV[0];
  186. if (!defined $cname) {
  187. print "Certificate filename is required; reason optional.\n";
  188. exit 1;
  189. }
  190. my $reason = $ARGV[1];
  191. $reason = " -crl_reason $reason"
  192. if defined $reason && crl_reason_ok($reason);
  193. $RET = run("$CA -revoke \"$cname\"" . $reason . $EXTRA{ca});
  194. } else {
  195. print STDERR "Unknown arg \"$WHAT\"\n";
  196. print STDERR "Use -help for help.\n";
  197. exit 1;
  198. }
  199. exit $RET;