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[-nodes] ... 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|-newreq-nodes|-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 (/^-newreq-nodes$/) {
70 # create a certificate request
71 system ("$REQ -new -nodes -keyout newreq.pem -out newreq.pem $DAYS");
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
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}/index.txt";
88 if ( ! -f
"${CATOP}/private/$CAKEY" ) {
89 print "CA certificate filename (or enter to create)\n";
94 # ask user for existing CA certificate
96 cp_pem
($FILE,"${CATOP}/private/$CAKEY", "PRIVATE");
97 cp_pem
($FILE,"${CATOP}/$CACERT", "CERTIFICATE");
100 print "Making CA certificate ...\n";
101 system ("$REQ -new -x509 -keyout " .
102 "${CATOP}/private/$CAKEY -out ${CATOP}/$CACERT $DAYS");
106 if (! -f
"${CATOP}/serial" ) {
107 system ("$X509 -in ${CATOP}/$CACERT -noout "
108 . "-next_serial -out ${CATOP}/serial");
110 } elsif (/^-pkcs12$/) {
111 my $cname = $ARGV[1];
112 $cname = "My Certificate" unless defined $cname;
113 system ("$PKCS12 -in newcert.pem -inkey newreq.pem " .
114 "-certfile ${CATOP}/$CACERT -out newcert.p12 " .
115 "-export -name \"$cname\"");
118 } elsif (/^-xsign$/) {
119 system ("$CA -policy policy_anything -infiles newreq.pem");
121 } elsif (/^(-sign|-signreq)$/) {
122 system ("$CA -policy policy_anything -out newcert.pem " .
123 "-infiles newreq.pem");
125 print "Signed certificate is in newcert.pem\n";
126 } elsif (/^(-signCA)$/) {
127 system ("$CA -policy policy_anything -out newcert.pem " .
128 "-extensions v3_ca -infiles newreq.pem");
130 print "Signed CA certificate is in newcert.pem\n";
131 } elsif (/^-signcert$/) {
132 system ("$X509 -x509toreq -in newreq.pem -signkey newreq.pem " .
134 system ("$CA -policy policy_anything -out newcert.pem " .
137 print "Signed certificate is in newcert.pem\n";
138 } elsif (/^-verify$/) {
141 system ("$VERIFY -CAfile $CATOP/$CACERT $j");
142 $RET=$?
if ($?
!= 0);
146 system ("$VERIFY -CAfile $CATOP/$CACERT newcert.pem");
151 print STDERR
"Unknown arg $_\n";
152 print STDERR
"usage: CA -newcert|-newreq|-newreq-nodes|-newca|-sign|-verify\n";
160 my ($infile, $outfile, $bound) = @_;
162 open OUT
, ">$outfile";
165 $flag = 1 if (/^-----BEGIN.*$bound/) ;
166 print OUT
$_ if ($flag);
167 if (/^-----END.*$bound/) {