Another stack.
[openssl.git] / apps / CA.pl
1 #!/usr/local/bin/perl
2 #
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 :-)
6 #
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 
10 #
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.
14 #
15 #
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
20 #                  a script.
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
25 #
26 # Tim Hudson
27 # tjh@cryptsoft.com
28 #
29
30 # 27-Apr-98 snh    Translation into perl, fix existing CA bug.
31 #
32 #
33 # Steve Henson
34 # shenson@bigfoot.com
35
36 # default openssl.cnf file has setup as per the following
37 # demoCA ... where everything is stored
38
39 $DAYS="-days 365";
40 $REQ="openssl req $SSLEAY_CONFIG";
41 $CA="openssl ca $SSLEAY_CONFIG";
42 $VERIFY="openssl verify";
43 $X509="openssl x509";
44
45 $CATOP="./demoCA";
46 $CAKEY="cakey.pem";
47 $CACERT="cacert.pem";
48
49 $DIRMODE = 0777;
50
51 $RET = 0;
52
53 foreach (@ARGV) {
54         if ( /^(-\?|-h|-help)$/ ) {
55             print STDERR "usage: CA -newcert|-newreq|-newca|-sign|-verify\n";
56             exit 0;
57         } elsif (/^-newcert$/) {
58             # create a certificate
59             system ("$REQ -new -x509 -keyout newreq.pem -out newreq.pem $DAYS");
60             $RET=$?;
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");
65             $RET=$?;
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 
70             $NEW="1";
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";
79                 print OUT "01\n";
80                 close OUT;
81                 open OUT, ">${CATOP}/index.txt";
82                 close OUT;
83             }
84             if ( ! -f "${CATOP}/private/$CAKEY" ) {
85                 print "CA certificate filename (or enter to create)\n";
86                 $FILE = <STDIN>;
87
88                 chop $FILE;
89
90                 # ask user for existing CA certificate
91                 if ($FILE) {
92                     cp_pem($FILE,"${CATOP}/private/$CAKEY", "PRIVATE");
93                     cp_pem($FILE,"${CATOP}/$CACERT", "CERTIFICATE");
94                     $RET=$?;
95                 } else {
96                     print "Making CA certificate ...\n";
97                     system ("$REQ -new -x509 -keyout " .
98                         "${CATOP}/private/$CAKEY -out ${CATOP}/$CACERT $DAYS");
99                     $RET=$?;
100                 }
101             }
102         } elsif (/^-xsign$/) {
103             system ("$CA -policy policy_anything -infiles newreq.pem");
104             $RET=$?;
105         } elsif (/^(-sign|-signreq)$/) {
106             system ("$CA -policy policy_anything -out newcert.pem " .
107                                                         "-infiles newreq.pem");
108             $RET=$?;
109             print "Signed certificate is in newcert.pem\n";
110         } elsif (/^-signcert$/) {
111             system ("$X509 -x509toreq -in newreq.pem -signkey newreq.pem " .
112                                                                 "-out tmp.pem");
113             system ("$CA -policy policy_anything -out newcert.pem " .
114                                                         "-infiles tmp.pem");
115             $RET = $?;
116             print "Signed certificate is in newcert.pem\n";
117         } elsif (/^-verify$/) {
118             if (shift) {
119                 foreach $j (@ARGV) {
120                     system ("$VERIFY -CAfile $CATOP/$CACERT $j");
121                     $RET=$? if ($? != 0);
122                 }
123                 exit $RET;
124             } else {
125                     system ("$VERIFY -CAfile $CATOP/$CACERT newcert.pem");
126                     $RET=$?;
127                     exit 0;
128             }
129         } else {
130             print STDERR "Unknown arg $_\n";
131             print STDERR "usage: CA -newcert|-newreq|-newca|-sign|-verify\n";
132             exit 1;
133         }
134 }
135
136 exit $RET;
137
138 sub cp_pem {
139 my ($infile, $outfile, $bound) = @_;
140 open IN, $infile;
141 open OUT, ">$outfile";
142 my $flag = 0;
143 while (<IN>) {
144         $flag = 1 if (/^-----BEGIN.*$bound/) ;
145         print OUT $_ if ($flag);
146         if (/^-----END.*$bound/) {
147                 close IN;
148                 close OUT;
149                 return;
150         }
151 }
152 }
153