CA.pl.in 6.2 KB

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