3 # Wrapper around the ca to make it easier to use
4 # Edit CA.pl.in not CA.pl!
10 my $openssl = "openssl";
11 if(defined $ENV{'OPENSSL'}) {
12 $openssl = $ENV{'OPENSSL'};
14 $ENV{'OPENSSL'} = $openssl;
19 my $SSLEAY_CONFIG = $ENV{"SSLEAY_CONFIG"};
20 my $DAYS = "-days 365";
21 my $CADAYS = "-days 1095"; # 3 years
22 my $REQ = "$openssl req $SSLEAY_CONFIG";
23 my $CA = "$openssl ca $SSLEAY_CONFIG";
24 my $VERIFY = "$openssl verify";
25 my $X509 = "$openssl x509";
26 my $PKCS12 = "$openssl pkcs12";
28 # default openssl.cnf file has setup as per the following
29 my $CATOP = "./demoCA";
30 my $CAKEY = "cakey.pem";
31 my $CAREQ = "careq.pem";
32 my $CACERT = "cacert.pem";
33 my $CACRL = "crl.pem";
36 my $NEWKEY = "newkey.pem";
37 my $NEWREQ = "newreq.pem";
38 my $NEWCERT = "newcert.pem";
39 my $NEWP12 = "newcert.p12";
41 my $WHAT = shift @ARGV;
44 # See if reason for a CRL entry is valid; exit if not.
49 if ($r eq 'unspecified' || $r eq 'keyCompromise'
50 || $r eq 'CACompromise' || $r eq 'affiliationChanged'
51 || $r eq 'superseded' || $r eq 'cessationOfOperation'
52 || $r eq 'certificateHold' || $r eq 'removeFromCRL') {
55 print STDERR "Invalid CRL reason; must be one of:\n";
56 print STDERR " unspecified, keyCompromise, CACompromise,\n";
57 print STDERR " affiliationChanged, superseded, cessationOfOperation\n";
58 print STDERR " certificateHold, removeFromCRL";
62 # Copy a PEM-format file; return like exit status (zero means ok)
65 my ($infile, $outfile, $bound) = @_;
68 open IN, $infile || die "Cannot open $infile, $!";
69 open OUT, ">$outfile" || die "Cannot write to $outfile, $!";
71 $found = 1 if /^-----BEGIN.*$bound/;
72 print OUT $_ if $found;
73 $found = 2, last if /^-----END.*$bound/;
77 return $found == 2 ? 0 : 1;
80 # Wrapper around system; useful for debugging. Returns just the exit status
84 print "====\n$cmd\n" if $verbose;
85 my $status = system($cmd);
86 print "==> $status\n====\n" if $verbose;
91 if ( $WHAT =~ /^(-\?|-h|-help)$/ ) {
92 print STDERR "usage: CA -newcert|-newreq|-newreq-nodes|-newca|-sign|-verify\n";
93 print STDERR " CA -pkcs12 [certname]\n";
94 print STDERR " CA -crl|-revoke cert-filename [reason]\n";
97 if ($WHAT eq '-newcert' ) {
98 # create a certificate
99 $RET = run("$REQ -new -x509 -keyout $NEWKEY -out $NEWCERT $DAYS");
100 print "Cert is in $NEWCERT, private key is in $NEWKEY\n" if $RET == 0;
101 } elsif ($WHAT eq '-newreq' ) {
102 # create a certificate request
103 $RET = run("$REQ -new -keyout $NEWKEY -out $NEWREQ $DAYS");
104 print "Request is in $NEWREQ, private key is in $NEWKEY\n" if $RET == 0;
105 } elsif ($WHAT eq '-newreq-nodes' ) {
106 # create a certificate request
107 $RET = run("$REQ -new -nodes -keyout $NEWKEY -out $NEWREQ $DAYS");
108 print "Request is in $NEWREQ, private key is in $NEWKEY\n" if $RET == 0;
109 } elsif ($WHAT eq '-newca' ) {
110 # create the directory hierarchy
111 mkdir ${CATOP}, $DIRMODE;
112 mkdir "${CATOP}/certs", $DIRMODE;
113 mkdir "${CATOP}/crl", $DIRMODE ;
114 mkdir "${CATOP}/newcerts", $DIRMODE;
115 mkdir "${CATOP}/private", $DIRMODE;
116 open OUT, ">${CATOP}/index.txt";
118 open OUT, ">${CATOP}/crlnumber";
121 # ask user for existing CA certificate
122 print "CA certificate filename (or enter to create)\n";
126 copy_pemfile($FILE,"${CATOP}/private/$CAKEY", "PRIVATE");
127 copy_pemfile($FILE,"${CATOP}/$CACERT", "CERTIFICATE");
129 print "Making CA certificate ...\n";
130 $RET = run("$REQ -new -keyout"
131 . " ${CATOP}/private/$CAKEY"
132 . " -out ${CATOP}/$CAREQ");
133 $RET = run("$CA -create_serial"
134 . " -out ${CATOP}/$CACERT $CADAYS -batch"
135 . " -keyfile ${CATOP}/private/$CAKEY -selfsign"
136 . " -extensions v3_ca"
137 . " -infiles ${CATOP}/$CAREQ") if $RET == 0;
138 print "CA certificate is in ${CATOP}/$CACERT\n" if $RET == 0;
140 } elsif ($WHAT eq '-pkcs12' ) {
141 my $cname = $ARGV[1];
142 $cname = "My Certificate" unless defined $cname;
143 $RET = run("$PKCS12 -in $NEWCERT -inkey $NEWKEY"
144 . " -certfile ${CATOP}/$CACERT"
146 . " -export -name \"$cname\"");
147 print "PKCS #12 file is in $NEWP12\n" if $RET == 0;
148 } elsif ($WHAT eq '-xsign' ) {
149 $RET = run("$CA -policy policy_anything -infiles $NEWREQ");
150 } elsif ($WHAT eq '-sign' ) {
151 $RET = run("$CA -policy policy_anything -out $NEWCERT -infiles $NEWREQ");
152 print "Signed certificate is in $NEWCERT\n" if $RET == 0;
153 } elsif ($WHAT eq '-signCA' ) {
154 $RET = run("$CA -policy policy_anything -out $NEWCERT"
155 . " -extensions v3_ca -infiles $NEWREQ");
156 print "Signed CA certificate is in $NEWCERT\n" if $RET == 0;
157 } elsif ($WHAT eq '-signcert' ) {
158 $RET = run("$X509 -x509toreq -in $NEWREQ -signkey $NEWREQ"
160 $RET = run("$CA -policy policy_anything -out $NEWCERT"
161 . " -infiles tmp.pem") if $RET == 0;
162 print "Signed certificate is in $NEWCERT\n" if $RET == 0;
163 } elsif ($WHAT eq '-verify' ) {
164 my @files = @ARGV ? @ARGV : ( $NEWCERT );
166 foreach $file (@files) {
167 my $status = run("$VERIFY -CAfile ${CATOP}/$CACERT $file");
168 $RET = $status if $status != 0;
170 } elsif ($WHAT eq '-crl' ) {
171 $RET = run("$CA -gencrl -out ${CATOP}/crl/$CACRL");
172 print "Generated CRL is in ${CATOP}/crl/$CACRL\n" if $RET == 0;
173 } elsif ($WHAT eq '-revoke' ) {
174 my $cname = $ARGV[1];
175 if (!defined $cname) {
176 print "Certificate filename is required; reason optional.\n";
179 my $reason = $ARGV[2];
180 $reason = " -crl_reason $reason"
181 if defined $reason && crl_reason_ok($reason);
182 $RET = run("$CA -revoke \"$cname\"" . $reason);
184 print STDERR "Unknown arg \"$WHAT\"\n";
185 print STDERR "Use -help for help.\n";