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
39 $SSLEAY_CONFIG=$ENV{"SSLEAY_CONFIG"};
41 $REQ="openssl req $SSLEAY_CONFIG";
42 $CA="openssl ca $SSLEAY_CONFIG";
43 $VERIFY="openssl verify";
45 $PKCS12="openssl pkcs12";
56 if ( /^(-\?|-h|-help)$/ ) {
57 print STDERR "usage: CA -newcert|-newreq|-newca|-sign|-verify\n";
59 } elsif (/^-newcert$/) {
60 # create a certificate
61 system ("$REQ -new -x509 -keyout newreq.pem -out newreq.pem $DAYS");
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");
68 print "Request (and private key) is in newreq.pem\n";
69 } elsif (/^-newca$/) {
70 # if explicitly asked for or it doesn't exist then setup the
71 # directory structure that Eric likes to manage things
73 if ( "$NEW" || ! -f "${CATOP}/serial" ) {
74 # create the directory hierarchy
75 mkdir $CATOP, $DIRMODE;
76 mkdir "${CATOP}/certs", $DIRMODE;
77 mkdir "${CATOP}/crl", $DIRMODE ;
78 mkdir "${CATOP}/newcerts", $DIRMODE;
79 mkdir "${CATOP}/private", $DIRMODE;
80 open OUT, ">${CATOP}/serial";
83 open OUT, ">${CATOP}/index.txt";
86 if ( ! -f "${CATOP}/private/$CAKEY" ) {
87 print "CA certificate filename (or enter to create)\n";
92 # ask user for existing CA certificate
94 cp_pem($FILE,"${CATOP}/private/$CAKEY", "PRIVATE");
95 cp_pem($FILE,"${CATOP}/$CACERT", "CERTIFICATE");
98 print "Making CA certificate ...\n";
99 system ("$REQ -new -x509 -keyout " .
100 "${CATOP}/private/$CAKEY -out ${CATOP}/$CACERT $DAYS");
104 } elsif (/^-pkcs12$/) {
105 my $cname = $ARGV[1];
106 $cname = "My Certificate" unless defined $cname;
107 system ("$PKCS12 -in newcert.pem -inkey newreq.pem " .
108 "-certfile ${CATOP}/$CACERT -out newcert.p12 " .
109 "-export -name \"$cname\"");
112 } elsif (/^-xsign$/) {
113 system ("$CA -policy policy_anything -infiles newreq.pem");
115 } elsif (/^(-sign|-signreq)$/) {
116 system ("$CA -policy policy_anything -out newcert.pem " .
117 "-infiles newreq.pem");
119 print "Signed certificate is in newcert.pem\n";
120 } elsif (/^(-signCA)$/) {
121 system ("$CA -policy policy_anything -out newcert.pem " .
122 "-extensions v3_ca -infiles newreq.pem");
124 print "Signed CA certificate is in newcert.pem\n";
125 } elsif (/^-signcert$/) {
126 system ("$X509 -x509toreq -in newreq.pem -signkey newreq.pem " .
128 system ("$CA -policy policy_anything -out newcert.pem " .
131 print "Signed certificate is in newcert.pem\n";
132 } elsif (/^-verify$/) {
135 system ("$VERIFY -CAfile $CATOP/$CACERT $j");
136 $RET=$? if ($? != 0);
140 system ("$VERIFY -CAfile $CATOP/$CACERT newcert.pem");
145 print STDERR "Unknown arg $_\n";
146 print STDERR "usage: CA -newcert|-newreq|-newca|-sign|-verify\n";
154 my ($infile, $outfile, $bound) = @_;
156 open OUT, ">$outfile";
159 $flag = 1 if (/^-----BEGIN.*$bound/) ;
160 print OUT $_ if ($flag);
161 if (/^-----END.*$bound/) {