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 ... 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 30# 27-Apr-98 snh Translation into perl, fix existing CA bug. 31# 32# 33# Steve Henson 34# shenson@bigfoot.com 35 36# default openssl.cnf file has setup as per the following 37# demoCA ... where everything is stored 38 39$DAYS="-days 365"; 40$REQ="openssl req $SSLEAY_CONFIG"; 41$CA="openssl ca $SSLEAY_CONFIG"; 42$VERIFY="openssl verify"; 43$X509="openssl x509"; 44$PKCS12="openssl pkcs12"; 45 46$CATOP="./demoCA"; 47$CAKEY="cakey.pem"; 48$CACERT="cacert.pem"; 49 50$DIRMODE = 0777; 51 52$RET = 0; 53 54foreach (@ARGV) { 55 if ( /^(-\?|-h|-help)$/ ) { 56 print STDERR "usage: CA -newcert|-newreq|-newca|-sign|-verify\n"; 57 exit 0; 58 } elsif (/^-newcert$/) { 59 # create a certificate 60 system ("$REQ -new -x509 -keyout newreq.pem -out newreq.pem $DAYS"); 61 $RET=$?; 62 print "Certificate (and private key) is in newreq.pem\n" 63 } elsif (/^-newreq$/) { 64 # create a certificate request 65 system ("$REQ -new -keyout newreq.pem -out newreq.pem $DAYS"); 66 $RET=$?; 67 print "Request (and private key) is in newreq.pem\n"; 68 } elsif (/^-newca$/) { 69 # if explicitly asked for or it doesn't exist then setup the 70 # directory structure that Eric likes to manage things 71 $NEW="1"; 72 if ( "$NEW" || ! -f "${CATOP}/serial" ) { 73 # create the directory hierarchy 74 mkdir $CATOP, $DIRMODE; 75 mkdir "${CATOP}/certs", $DIRMODE; 76 mkdir "${CATOP}/crl", $DIRMODE ; 77 mkdir "${CATOP}/newcerts", $DIRMODE; 78 mkdir "${CATOP}/private", $DIRMODE; 79 open OUT, ">${CATOP}/serial"; 80 print OUT "01\n"; 81 close OUT; 82 open OUT, ">${CATOP}/index.txt"; 83 close OUT; 84 } 85 if ( ! -f "${CATOP}/private/$CAKEY" ) { 86 print "CA certificate filename (or enter to create)\n"; 87 $FILE = <STDIN>; 88 89 chop $FILE; 90 91 # ask user for existing CA certificate 92 if ($FILE) { 93 cp_pem($FILE,"${CATOP}/private/$CAKEY", "PRIVATE"); 94 cp_pem($FILE,"${CATOP}/$CACERT", "CERTIFICATE"); 95 $RET=$?; 96 } else { 97 print "Making CA certificate ...\n"; 98 system ("$REQ -new -x509 -keyout " . 99 "${CATOP}/private/$CAKEY -out ${CATOP}/$CACERT $DAYS"); 100 $RET=$?; 101 } 102 } 103 } elsif (/^-pkcs12$/) { 104 my $cname = $ARGV[1]; 105 $cname = "My Certificate" unless defined $cname; 106 system ("$PKCS12 -in newcert.pem -inkey newreq.pem " . 107 "-certfile ${CATOP}/$CACERT -out newcert.p12 " . 108 "-export -name \"$cname\""); 109 $RET=$?; 110 exit $RET; 111 } elsif (/^-xsign$/) { 112 system ("$CA -policy policy_anything -infiles newreq.pem"); 113 $RET=$?; 114 } elsif (/^(-sign|-signreq)$/) { 115 system ("$CA -policy policy_anything -out newcert.pem " . 116 "-infiles newreq.pem"); 117 $RET=$?; 118 print "Signed certificate is in newcert.pem\n"; 119 } elsif (/^-signcert$/) { 120 system ("$X509 -x509toreq -in newreq.pem -signkey newreq.pem " . 121 "-out tmp.pem"); 122 system ("$CA -policy policy_anything -out newcert.pem " . 123 "-infiles tmp.pem"); 124 $RET = $?; 125 print "Signed certificate is in newcert.pem\n"; 126 } elsif (/^-verify$/) { 127 if (shift) { 128 foreach $j (@ARGV) { 129 system ("$VERIFY -CAfile $CATOP/$CACERT $j"); 130 $RET=$? if ($? != 0); 131 } 132 exit $RET; 133 } else { 134 system ("$VERIFY -CAfile $CATOP/$CACERT newcert.pem"); 135 $RET=$?; 136 exit 0; 137 } 138 } else { 139 print STDERR "Unknown arg $_\n"; 140 print STDERR "usage: CA -newcert|-newreq|-newca|-sign|-verify\n"; 141 exit 1; 142 } 143} 144 145exit $RET; 146 147sub cp_pem { 148my ($infile, $outfile, $bound) = @_; 149open IN, $infile; 150open OUT, ">$outfile"; 151my $flag = 0; 152while (<IN>) { 153 $flag = 1 if (/^-----BEGIN.*$bound/) ; 154 print OUT $_ if ($flag); 155 if (/^-----END.*$bound/) { 156 close IN; 157 close OUT; 158 return; 159 } 160} 161} 162 163