CA.pl.in 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  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. }
  90. if ( ! -f "${CATOP}/private/$CAKEY" ) {
  91. print "CA certificate filename (or enter to create)\n";
  92. $FILE = <STDIN>;
  93. chop $FILE;
  94. # ask user for existing CA certificate
  95. if ($FILE) {
  96. cp_pem($FILE,"${CATOP}/private/$CAKEY", "PRIVATE");
  97. cp_pem($FILE,"${CATOP}/$CACERT", "CERTIFICATE");
  98. $RET=$?;
  99. } else {
  100. print "Making CA certificate ...\n";
  101. system ("$REQ -new -keyout " .
  102. "${CATOP}/private/$CAKEY -out ${CATOP}/$CAREQ");
  103. system ("$CA -create_serial " .
  104. "-out ${CATOP}/$CACERT $CADAYS -batch " .
  105. "-keyfile ${CATOP}/private/$CAKEY -selfsign " .
  106. "-infiles ${CATOP}/$CAREQ ");
  107. $RET=$?;
  108. }
  109. }
  110. } elsif (/^-pkcs12$/) {
  111. my $cname = $ARGV[1];
  112. $cname = "My Certificate" unless defined $cname;
  113. system ("$PKCS12 -in newcert.pem -inkey newkey.pem " .
  114. "-certfile ${CATOP}/$CACERT -out newcert.p12 " .
  115. "-export -name \"$cname\"");
  116. $RET=$?;
  117. print "PKCS #12 file is in newcert.p12\n";
  118. exit $RET;
  119. } elsif (/^-xsign$/) {
  120. system ("$CA -policy policy_anything -infiles newreq.pem");
  121. $RET=$?;
  122. } elsif (/^(-sign|-signreq)$/) {
  123. system ("$CA -policy policy_anything -out newcert.pem " .
  124. "-infiles newreq.pem");
  125. $RET=$?;
  126. print "Signed certificate is in newcert.pem\n";
  127. } elsif (/^(-signCA)$/) {
  128. system ("$CA -policy policy_anything -out newcert.pem " .
  129. "-extensions v3_ca -infiles newreq.pem");
  130. $RET=$?;
  131. print "Signed CA certificate is in newcert.pem\n";
  132. } elsif (/^-signcert$/) {
  133. system ("$X509 -x509toreq -in newreq.pem -signkey newreq.pem " .
  134. "-out tmp.pem");
  135. system ("$CA -policy policy_anything -out newcert.pem " .
  136. "-infiles tmp.pem");
  137. $RET = $?;
  138. print "Signed certificate is in newcert.pem\n";
  139. } elsif (/^-verify$/) {
  140. if (shift) {
  141. foreach $j (@ARGV) {
  142. system ("$VERIFY -CAfile $CATOP/$CACERT $j");
  143. $RET=$? if ($? != 0);
  144. }
  145. exit $RET;
  146. } else {
  147. system ("$VERIFY -CAfile $CATOP/$CACERT newcert.pem");
  148. $RET=$?;
  149. exit 0;
  150. }
  151. } else {
  152. print STDERR "Unknown arg $_\n";
  153. print STDERR "usage: CA -newcert|-newreq|-newreq-nodes|-newca|-sign|-verify\n";
  154. exit 1;
  155. }
  156. }
  157. exit $RET;
  158. sub cp_pem {
  159. my ($infile, $outfile, $bound) = @_;
  160. open IN, $infile;
  161. open OUT, ">$outfile";
  162. my $flag = 0;
  163. while (<IN>) {
  164. $flag = 1 if (/^-----BEGIN.*$bound/) ;
  165. print OUT $_ if ($flag);
  166. if (/^-----END.*$bound/) {
  167. close IN;
  168. close OUT;
  169. return;
  170. }
  171. }
  172. }