]> git.ipfire.org Git - thirdparty/openssl.git/blob - apps/CA.pl.in
Reduce chances of issuer and serial number duplication by use of random
[thirdparty/openssl.git] / apps / CA.pl.in
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[-nodes] ... 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 $SSLEAY_CONFIG=$ENV{"SSLEAY_CONFIG"};
40 $DAYS="-days 365"; # 1 year
41 $CADAYS="-days 1095"; # 3 years
42 $REQ="openssl req $SSLEAY_CONFIG";
43 $CA="openssl ca $SSLEAY_CONFIG";
44 $VERIFY="openssl verify";
45 $X509="openssl x509";
46 $PKCS12="openssl pkcs12";
47
48 $CATOP="./demoCA";
49 $CAKEY="cakey.pem";
50 $CAREQ="careq.pem";
51 $CACERT="cacert.pem";
52
53 $DIRMODE = 0777;
54
55 $RET = 0;
56
57 foreach (@ARGV) {
58 if ( /^(-\?|-h|-help)$/ ) {
59 print STDERR "usage: CA -newcert|-newreq|-newreq-nodes|-newca|-sign|-verify\n";
60 exit 0;
61 } elsif (/^-newcert$/) {
62 # create a certificate
63 system ("$REQ -new -x509 -keyout newreq.pem -out newreq.pem $DAYS");
64 $RET=$?;
65 print "Certificate (and private key) is in newreq.pem\n"
66 } elsif (/^-newreq$/) {
67 # create a certificate request
68 system ("$REQ -new -keyout newreq.pem -out newreq.pem $DAYS");
69 $RET=$?;
70 print "Request (and private key) is in newreq.pem\n";
71 } elsif (/^-newreq-nodes$/) {
72 # create a certificate request
73 system ("$REQ -new -nodes -keyout newreq.pem -out newreq.pem $DAYS");
74 $RET=$?;
75 print "Request (and private key) is in newreq.pem\n";
76 } elsif (/^-newca$/) {
77 # if explicitly asked for or it doesn't exist then setup the
78 # directory structure that Eric likes to manage things
79 $NEW="1";
80 if ( "$NEW" || ! -f "${CATOP}/serial" ) {
81 # create the directory hierarchy
82 mkdir $CATOP, $DIRMODE;
83 mkdir "${CATOP}/certs", $DIRMODE;
84 mkdir "${CATOP}/crl", $DIRMODE ;
85 mkdir "${CATOP}/newcerts", $DIRMODE;
86 mkdir "${CATOP}/private", $DIRMODE;
87 open OUT, ">${CATOP}/index.txt";
88 close OUT;
89 }
90 if ( ! -f "${CATOP}/private/$CAKEY" ) {
91 print "CA certificate filename (or enter to create)\n";
92 $FILE = <STDIN>;
93
94 chop $FILE;
95
96 # ask user for existing CA certificate
97 if ($FILE) {
98 cp_pem($FILE,"${CATOP}/private/$CAKEY", "PRIVATE");
99 cp_pem($FILE,"${CATOP}/$CACERT", "CERTIFICATE");
100 $RET=$?;
101 } else {
102 print "Making CA certificate ...\n";
103 system ("$REQ -new -keyout " .
104 "${CATOP}/private/$CAKEY -out ${CATOP}/$CAREQ");
105 system ("$CA -create_serial " .
106 "-out ${CATOP}/$CACERT $CADAYS -batch " .
107 "-keyfile ${CATOP}/private/$CAKEY -selfsign " .
108 "-infiles ${CATOP}/$CAREQ ");
109 $RET=$?;
110 }
111 }
112 } elsif (/^-pkcs12$/) {
113 my $cname = $ARGV[1];
114 $cname = "My Certificate" unless defined $cname;
115 system ("$PKCS12 -in newcert.pem -inkey newreq.pem " .
116 "-certfile ${CATOP}/$CACERT -out newcert.p12 " .
117 "-export -name \"$cname\"");
118 $RET=$?;
119 exit $RET;
120 } elsif (/^-xsign$/) {
121 system ("$CA -policy policy_anything -infiles newreq.pem");
122 $RET=$?;
123 } elsif (/^(-sign|-signreq)$/) {
124 system ("$CA -policy policy_anything -out newcert.pem " .
125 "-infiles newreq.pem");
126 $RET=$?;
127 print "Signed certificate is in newcert.pem\n";
128 } elsif (/^(-signCA)$/) {
129 system ("$CA -policy policy_anything -out newcert.pem " .
130 "-extensions v3_ca -infiles newreq.pem");
131 $RET=$?;
132 print "Signed CA certificate is in newcert.pem\n";
133 } elsif (/^-signcert$/) {
134 system ("$X509 -x509toreq -in newreq.pem -signkey newreq.pem " .
135 "-out tmp.pem");
136 system ("$CA -policy policy_anything -out newcert.pem " .
137 "-infiles tmp.pem");
138 $RET = $?;
139 print "Signed certificate is in newcert.pem\n";
140 } elsif (/^-verify$/) {
141 if (shift) {
142 foreach $j (@ARGV) {
143 system ("$VERIFY -CAfile $CATOP/$CACERT $j");
144 $RET=$? if ($? != 0);
145 }
146 exit $RET;
147 } else {
148 system ("$VERIFY -CAfile $CATOP/$CACERT newcert.pem");
149 $RET=$?;
150 exit 0;
151 }
152 } else {
153 print STDERR "Unknown arg $_\n";
154 print STDERR "usage: CA -newcert|-newreq|-newreq-nodes|-newca|-sign|-verify\n";
155 exit 1;
156 }
157 }
158
159 exit $RET;
160
161 sub cp_pem {
162 my ($infile, $outfile, $bound) = @_;
163 open IN, $infile;
164 open OUT, ">$outfile";
165 my $flag = 0;
166 while (<IN>) {
167 $flag = 1 if (/^-----BEGIN.*$bound/) ;
168 print OUT $_ if ($flag);
169 if (/^-----END.*$bound/) {
170 close IN;
171 close OUT;
172 return;
173 }
174 }
175 }
176