2
0

CA.pl.in 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. #!/usr/local/bin/perl
  2. #
  3. # CA - wrapper around ca to make it easier to use ... basically ca requires
  4. # some setup stuff to be done before you can use it and this makes
  5. # things easier between now and when Eric is convinced to fix it :-)
  6. #
  7. # CA -newca ... will setup the right stuff
  8. # CA -newreq[-nodes] ... will generate a certificate request
  9. # CA -sign ... will sign the generated request and output
  10. #
  11. # At the end of that grab newreq.pem and newcert.pem (one has the key
  12. # and the other the certificate) and cat them together and that is what
  13. # you want/need ... I'll make even this a little cleaner later.
  14. #
  15. #
  16. # 12-Jan-96 tjh Added more things ... including CA -signcert which
  17. # converts a certificate to a request and then signs it.
  18. # 10-Jan-96 eay Fixed a few more bugs and added the SSLEAY_CONFIG
  19. # environment variable so this can be driven from
  20. # a script.
  21. # 25-Jul-96 eay Cleaned up filenames some more.
  22. # 11-Jun-96 eay Fixed a few filename missmatches.
  23. # 03-May-96 eay Modified to use 'ssleay cmd' instead of 'cmd'.
  24. # 18-Apr-96 tjh Original hacking
  25. #
  26. # Tim Hudson
  27. # tjh@cryptsoft.com
  28. #
  29. # 27-Apr-98 snh Translation into perl, fix existing CA bug.
  30. #
  31. #
  32. # Steve Henson
  33. # shenson@bigfoot.com
  34. # default openssl.cnf file has setup as per the following
  35. # demoCA ... where everything is stored
  36. my $openssl;
  37. if(defined $ENV{OPENSSL}) {
  38. $openssl = $ENV{OPENSSL};
  39. } else {
  40. $openssl = "openssl";
  41. $ENV{OPENSSL} = $openssl;
  42. }
  43. $SSLEAY_CONFIG=$ENV{"SSLEAY_CONFIG"};
  44. $DAYS="-days 365"; # 1 year
  45. $CADAYS="-days 1095"; # 3 years
  46. $REQ="$openssl req $SSLEAY_CONFIG";
  47. $CA="$openssl ca $SSLEAY_CONFIG";
  48. $VERIFY="$openssl verify";
  49. $X509="$openssl x509";
  50. $PKCS12="$openssl pkcs12";
  51. $CATOP="./demoCA";
  52. $CAKEY="cakey.pem";
  53. $CAREQ="careq.pem";
  54. $CACERT="cacert.pem";
  55. $DIRMODE = 0777;
  56. $RET = 0;
  57. foreach (@ARGV) {
  58. if ( /^(-\?|-h|-help)$/ ) {
  59. print STDERR "usage: CA -newcert|-newreq|-newreq-nodes|-newca|-sign|-verify\n";
  60. exit 0;
  61. } elsif (/^-newcert$/) {
  62. # create a certificate
  63. system ("$REQ -new -x509 -keyout newkey.pem -out newcert.pem $DAYS");
  64. $RET=$?;
  65. print "Certificate is in newcert.pem, private key is in newkey.pem\n"
  66. } elsif (/^-newreq$/) {
  67. # create a certificate request
  68. system ("$REQ -new -keyout newkey.pem -out newreq.pem $DAYS");
  69. $RET=$?;
  70. print "Request is in newreq.pem, private key is in newkey.pem\n";
  71. } elsif (/^-newreq-nodes$/) {
  72. # create a certificate request
  73. system ("$REQ -new -nodes -keyout newkey.pem -out newreq.pem $DAYS");
  74. $RET=$?;
  75. print "Request is in newreq.pem, private key is in newkey.pem\n";
  76. } elsif (/^-newca$/) {
  77. # if explicitly asked for or it doesn't exist then setup the
  78. # directory structure that Eric likes to manage things
  79. $NEW="1";
  80. if ( "$NEW" || ! -f "${CATOP}/serial" ) {
  81. # create the directory hierarchy
  82. mkdir $CATOP, $DIRMODE;
  83. mkdir "${CATOP}/certs", $DIRMODE;
  84. mkdir "${CATOP}/crl", $DIRMODE ;
  85. mkdir "${CATOP}/newcerts", $DIRMODE;
  86. mkdir "${CATOP}/private", $DIRMODE;
  87. open OUT, ">${CATOP}/index.txt";
  88. close OUT;
  89. open OUT, ">${CATOP}/crlnumber";
  90. print OUT "01\n";
  91. close OUT;
  92. }
  93. if ( ! -f "${CATOP}/private/$CAKEY" ) {
  94. print "CA certificate filename (or enter to create)\n";
  95. $FILE = <STDIN>;
  96. chop $FILE;
  97. # ask user for existing CA certificate
  98. if ($FILE) {
  99. cp_pem($FILE,"${CATOP}/private/$CAKEY", "PRIVATE");
  100. cp_pem($FILE,"${CATOP}/$CACERT", "CERTIFICATE");
  101. $RET=$?;
  102. } else {
  103. print "Making CA certificate ...\n";
  104. system ("$REQ -new -keyout " .
  105. "${CATOP}/private/$CAKEY -out ${CATOP}/$CAREQ");
  106. system ("$CA -create_serial " .
  107. "-out ${CATOP}/$CACERT $CADAYS -batch " .
  108. "-keyfile ${CATOP}/private/$CAKEY -selfsign " .
  109. "-extensions v3_ca " .
  110. "-infiles ${CATOP}/$CAREQ ");
  111. $RET=$?;
  112. }
  113. }
  114. } elsif (/^-pkcs12$/) {
  115. my $cname = $ARGV[1];
  116. $cname = "My Certificate" unless defined $cname;
  117. system ("$PKCS12 -in newcert.pem -inkey newkey.pem " .
  118. "-certfile ${CATOP}/$CACERT -out newcert.p12 " .
  119. "-export -name \"$cname\"");
  120. $RET=$?;
  121. print "PKCS #12 file is in newcert.p12\n";
  122. exit $RET;
  123. } elsif (/^-xsign$/) {
  124. system ("$CA -policy policy_anything -infiles newreq.pem");
  125. $RET=$?;
  126. } elsif (/^(-sign|-signreq)$/) {
  127. system ("$CA -policy policy_anything -out newcert.pem " .
  128. "-infiles newreq.pem");
  129. $RET=$?;
  130. print "Signed certificate is in newcert.pem\n";
  131. } elsif (/^(-signCA)$/) {
  132. system ("$CA -policy policy_anything -out newcert.pem " .
  133. "-extensions v3_ca -infiles newreq.pem");
  134. $RET=$?;
  135. print "Signed CA certificate is in newcert.pem\n";
  136. } elsif (/^-signcert$/) {
  137. system ("$X509 -x509toreq -in newreq.pem -signkey newreq.pem " .
  138. "-out tmp.pem");
  139. system ("$CA -policy policy_anything -out newcert.pem " .
  140. "-infiles tmp.pem");
  141. $RET = $?;
  142. print "Signed certificate is in newcert.pem\n";
  143. } elsif (/^-verify$/) {
  144. if (shift) {
  145. foreach $j (@ARGV) {
  146. system ("$VERIFY -CAfile $CATOP/$CACERT $j");
  147. $RET=$? if ($? != 0);
  148. }
  149. exit $RET;
  150. } else {
  151. system ("$VERIFY -CAfile $CATOP/$CACERT newcert.pem");
  152. $RET=$?;
  153. exit 0;
  154. }
  155. } else {
  156. print STDERR "Unknown arg $_\n";
  157. print STDERR "usage: CA -newcert|-newreq|-newreq-nodes|-newca|-sign|-verify\n";
  158. exit 1;
  159. }
  160. }
  161. exit $RET;
  162. sub cp_pem {
  163. my ($infile, $outfile, $bound) = @_;
  164. open IN, $infile;
  165. open OUT, ">$outfile";
  166. my $flag = 0;
  167. while (<IN>) {
  168. $flag = 1 if (/^-----BEGIN.*$bound/) ;
  169. print OUT $_ if ($flag);
  170. if (/^-----END.*$bound/) {
  171. close IN;
  172. close OUT;
  173. return;
  174. }
  175. }
  176. }