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";
54 if ( /^(-\?|-h|-help)$/ ) {
55 print STDERR "usage: CA -newcert|-newreq|-newca|-sign|-verify\n";
57 } elsif (/^-newcert$/) {
58 # create a certificate
59 system ("$REQ -new -x509 -keyout newreq.pem -out newreq.pem $DAYS");
61 print "Certificate (and private key) is in newreq.pem\n"
62 } elsif (/^-newreq$/) {
63 # create a certificate request
64 system ("$REQ -new -keyout newreq.pem -out newreq.pem $DAYS");
66 print "Request (and private key) is in newreq.pem\n";
67 } elsif (/^-newca$/) {
68 # if explictly asked for or it doesn't exist then setup the
69 # directory structure that Eric likes to manage things
71 if ( "$NEW" || ! -f ${CATOP}/serial ) {
72 # create the directory hierarchy
73 mkdir $CATOP, $DIRMODE;
74 mkdir "${CATOP}/certs", $DIRMODE;
75 mkdir "${CATOP}/crl", $DIRMODE ;
76 mkdir "${CATOP}/newcerts", $DIRMODE;
77 mkdir "${CATOP}/private", $DIRMODE;
78 open OUT, ">${CATOP}/serial";
81 open OUT, ">${CATOP}/index.txt";
84 if ( ! -f "${CATOP}/private/$CAKEY" ) {
85 print "CA certificate filename (or enter to create)\n";
90 # ask user for existing CA certificate
92 cp_pem($FILE,"${CATOP}/private/$CAKEY", "PRIVATE");
93 cp_pem($FILE,"${CATOP}/$CACERT", "CERTIFICATE");
96 print "Making CA certificate ...\n";
97 system ("$REQ -new -x509 -keyout " .
98 "${CATOP}/private/$CAKEY -out ${CATOP}/$CACERT $DAYS");
102 } elsif (/^-xsign$/) {
103 system ("$CA -policy policy_anything -infiles newreq.pem");
105 } elsif (/^(-sign|-signreq)$/) {
106 system ("$CA -policy policy_anything -out newcert.pem " .
107 "-infiles newreq.pem");
109 print "Signed certificate is in newcert.pem\n";
110 } elsif (/^-signcert$/) {
111 system ("$X509 -x509toreq -in newreq.pem -signkey newreq.pem " .
113 system ("$CA -policy policy_anything -out newcert.pem " .
116 print "Signed certificate is in newcert.pem\n";
117 } elsif (/^-verify$/) {
120 system ("$VERIFY -CAfile $CATOP/$CACERT $j");
121 $RET=$? if ($? != 0);
125 system ("$VERIFY -CAfile $CATOP/$CACERT newcert.pem");
130 print STDERR "Unknown arg $_\n";
131 print STDERR "usage: CA -newcert|-newreq|-newca|-sign|-verify\n";
139 my ($infile, $outfile, $bound) = @_;
141 open OUT, ">$outfile";
144 $flag = 1 if (/^-----BEGIN.*$bound/) ;
145 print OUT $_ if ($flag);
146 if (/^-----END.*$bound/) {