CA.pl 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  1. #!/usr/bin/env perl
  2. # Copyright 2000-2016 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 ..\..\openssl-1.1.0f\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 $FILE;
  44. # See if reason for a CRL entry is valid; exit if not.
  45. sub crl_reason_ok
  46. {
  47. my $r = shift;
  48. if ($r eq 'unspecified' || $r eq 'keyCompromise'
  49. || $r eq 'CACompromise' || $r eq 'affiliationChanged'
  50. || $r eq 'superseded' || $r eq 'cessationOfOperation'
  51. || $r eq 'certificateHold' || $r eq 'removeFromCRL') {
  52. return 1;
  53. }
  54. print STDERR "Invalid CRL reason; must be one of:\n";
  55. print STDERR " unspecified, keyCompromise, CACompromise,\n";
  56. print STDERR " affiliationChanged, superseded, cessationOfOperation\n";
  57. print STDERR " certificateHold, removeFromCRL";
  58. exit 1;
  59. }
  60. # Copy a PEM-format file; return like exit status (zero means ok)
  61. sub copy_pemfile
  62. {
  63. my ($infile, $outfile, $bound) = @_;
  64. my $found = 0;
  65. open IN, $infile || die "Cannot open $infile, $!";
  66. open OUT, ">$outfile" || die "Cannot write to $outfile, $!";
  67. while (<IN>) {
  68. $found = 1 if /^-----BEGIN.*$bound/;
  69. print OUT $_ if $found;
  70. $found = 2, last if /^-----END.*$bound/;
  71. }
  72. close IN;
  73. close OUT;
  74. return $found == 2 ? 0 : 1;
  75. }
  76. # Wrapper around system; useful for debugging. Returns just the exit status
  77. sub run
  78. {
  79. my $cmd = shift;
  80. print "====\n$cmd\n" if $verbose;
  81. my $status = system($cmd);
  82. print "==> $status\n====\n" if $verbose;
  83. return $status >> 8;
  84. }
  85. if ( $WHAT =~ /^(-\?|-h|-help)$/ ) {
  86. print STDERR "usage: CA -newcert|-newreq|-newreq-nodes|-newca|-sign|-signcert|-verify\n";
  87. print STDERR " CA -pkcs12 [certname]\n";
  88. print STDERR " CA -crl|-revoke cert-filename [reason]\n";
  89. exit 0;
  90. }
  91. if ($WHAT eq '-newcert' ) {
  92. # create a certificate
  93. $RET = run("$REQ -new -x509 -keyout $NEWKEY -out $NEWCERT $DAYS");
  94. print "Cert is in $NEWCERT, private key is in $NEWKEY\n" if $RET == 0;
  95. } elsif ($WHAT eq '-newreq' ) {
  96. # create a certificate request
  97. $RET = run("$REQ -new -keyout $NEWKEY -out $NEWREQ $DAYS");
  98. print "Request is in $NEWREQ, private key is in $NEWKEY\n" if $RET == 0;
  99. } elsif ($WHAT eq '-newreq-nodes' ) {
  100. # create a certificate request
  101. $RET = run("$REQ -new -nodes -keyout $NEWKEY -out $NEWREQ $DAYS");
  102. print "Request is in $NEWREQ, private key is in $NEWKEY\n" if $RET == 0;
  103. } elsif ($WHAT eq '-newca' ) {
  104. # create the directory hierarchy
  105. mkdir ${CATOP}, $DIRMODE;
  106. mkdir "${CATOP}/certs", $DIRMODE;
  107. mkdir "${CATOP}/crl", $DIRMODE ;
  108. mkdir "${CATOP}/newcerts", $DIRMODE;
  109. mkdir "${CATOP}/private", $DIRMODE;
  110. open OUT, ">${CATOP}/index.txt";
  111. close OUT;
  112. open OUT, ">${CATOP}/crlnumber";
  113. print OUT "01\n";
  114. close OUT;
  115. # ask user for existing CA certificate
  116. print "CA certificate filename (or enter to create)\n";
  117. $FILE = "" unless defined($FILE = <STDIN>);
  118. $FILE =~ s{\R$}{};
  119. if ($FILE ne "") {
  120. copy_pemfile($FILE,"${CATOP}/private/$CAKEY", "PRIVATE");
  121. copy_pemfile($FILE,"${CATOP}/$CACERT", "CERTIFICATE");
  122. } else {
  123. print "Making CA certificate ...\n";
  124. $RET = run("$REQ -new -keyout"
  125. . " ${CATOP}/private/$CAKEY"
  126. . " -out ${CATOP}/$CAREQ");
  127. $RET = run("$CA -create_serial"
  128. . " -out ${CATOP}/$CACERT $CADAYS -batch"
  129. . " -keyfile ${CATOP}/private/$CAKEY -selfsign"
  130. . " -extensions v3_ca"
  131. . " -infiles ${CATOP}/$CAREQ") if $RET == 0;
  132. print "CA certificate is in ${CATOP}/$CACERT\n" if $RET == 0;
  133. }
  134. } elsif ($WHAT eq '-pkcs12' ) {
  135. my $cname = $ARGV[1];
  136. $cname = "My Certificate" unless defined $cname;
  137. $RET = run("$PKCS12 -in $NEWCERT -inkey $NEWKEY"
  138. . " -certfile ${CATOP}/$CACERT"
  139. . " -out $NEWP12"
  140. . " -export -name \"$cname\"");
  141. print "PKCS #12 file is in $NEWP12\n" if $RET == 0;
  142. } elsif ($WHAT eq '-xsign' ) {
  143. $RET = run("$CA -policy policy_anything -infiles $NEWREQ");
  144. } elsif ($WHAT eq '-sign' ) {
  145. $RET = run("$CA -policy policy_anything -out $NEWCERT -infiles $NEWREQ");
  146. print "Signed certificate is in $NEWCERT\n" if $RET == 0;
  147. } elsif ($WHAT eq '-signCA' ) {
  148. $RET = run("$CA -policy policy_anything -out $NEWCERT"
  149. . " -extensions v3_ca -infiles $NEWREQ");
  150. print "Signed CA certificate is in $NEWCERT\n" if $RET == 0;
  151. } elsif ($WHAT eq '-signcert' ) {
  152. $RET = run("$X509 -x509toreq -in $NEWREQ -signkey $NEWREQ"
  153. . " -out tmp.pem");
  154. $RET = run("$CA -policy policy_anything -out $NEWCERT"
  155. . " -infiles tmp.pem") if $RET == 0;
  156. print "Signed certificate is in $NEWCERT\n" if $RET == 0;
  157. } elsif ($WHAT eq '-verify' ) {
  158. my @files = @ARGV ? @ARGV : ( $NEWCERT );
  159. my $file;
  160. foreach $file (@files) {
  161. my $status = run("$VERIFY \"-CAfile\" ${CATOP}/$CACERT $file");
  162. $RET = $status if $status != 0;
  163. }
  164. } elsif ($WHAT eq '-crl' ) {
  165. $RET = run("$CA -gencrl -out ${CATOP}/crl/$CACRL");
  166. print "Generated CRL is in ${CATOP}/crl/$CACRL\n" if $RET == 0;
  167. } elsif ($WHAT eq '-revoke' ) {
  168. my $cname = $ARGV[1];
  169. if (!defined $cname) {
  170. print "Certificate filename is required; reason optional.\n";
  171. exit 1;
  172. }
  173. my $reason = $ARGV[2];
  174. $reason = " -crl_reason $reason"
  175. if defined $reason && crl_reason_ok($reason);
  176. $RET = run("$CA -revoke \"$cname\"" . $reason);
  177. } else {
  178. print STDERR "Unknown arg \"$WHAT\"\n";
  179. print STDERR "Use -help for help.\n";
  180. exit 1;
  181. }
  182. exit $RET;