CA.pl.in 7.4 KB

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