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 :-)
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
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.
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
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
30 # 27-Apr-98 snh Translation into perl, fix existing CA bug.
36 # default openssl.cnf file has setup as per the following
37 # demoCA ... where everything is stored
40 $REQ="openssl req $SSLEAY_CONFIG";
41 $CA="openssl ca $SSLEAY_CONFIG";
42 $VERIFY="openssl verify";
44 $PKCS12="openssl pkcs12";
55 if ( /^(-\?|-h|-help)$/ ) {
56 print STDERR "usage: CA -newcert|-newreq|-newca|-sign|-verify\n";
58 } elsif (/^-newcert$/) {
59 # create a certificate
60 system ("$REQ -new -x509 -keyout newreq.pem -out newreq.pem $DAYS");
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");
67 print "Request (and private key) is in newreq.pem\n";
68 } elsif (/^-newca$/) {
69 # if explictly asked for or it doesn't exist then setup the
70 # directory structure that Eric likes to manage things
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";
82 open OUT, ">${CATOP}/index.txt";
85 if ( ! -f "${CATOP}/private/$CAKEY" ) {
86 print "CA certificate filename (or enter to create)\n";
91 # ask user for existing CA certificate
93 cp_pem($FILE,"${CATOP}/private/$CAKEY", "PRIVATE");
94 cp_pem($FILE,"${CATOP}/$CACERT", "CERTIFICATE");
97 print "Making CA certificate ...\n";
98 system ("$REQ -new -x509 -keyout " .
99 "${CATOP}/private/$CAKEY -out ${CATOP}/$CACERT $DAYS");
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\"");
111 } elsif (/^-xsign$/) {
112 system ("$CA -policy policy_anything -infiles newreq.pem");
114 } elsif (/^(-sign|-signreq)$/) {
115 system ("$CA -policy policy_anything -out newcert.pem " .
116 "-infiles newreq.pem");
118 print "Signed certificate is in newcert.pem\n";
119 } elsif (/^-signcert$/) {
120 system ("$X509 -x509toreq -in newreq.pem -signkey newreq.pem " .
122 system ("$CA -policy policy_anything -out newcert.pem " .
125 print "Signed certificate is in newcert.pem\n";
126 } elsif (/^-verify$/) {
129 system ("$VERIFY -CAfile $CATOP/$CACERT $j");
130 $RET=$? if ($? != 0);
134 system ("$VERIFY -CAfile $CATOP/$CACERT newcert.pem");
139 print STDERR "Unknown arg $_\n";
140 print STDERR "usage: CA -newcert|-newreq|-newca|-sign|-verify\n";
148 my ($infile, $outfile, $bound) = @_;
150 open OUT, ">$outfile";
153 $flag = 1 if (/^-----BEGIN.*$bound/) ;
154 print OUT $_ if ($flag);
155 if (/^-----END.*$bound/) {