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