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