CA.pl.in 6.2 KB

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