2 # Copyright 2000-2016 The OpenSSL Project Authors. All Rights Reserved.
4 # Licensed under the OpenSSL license (the "License"). You may not use
5 # this file except in compliance with the License. You can obtain a copy
6 # in the file LICENSE in the source distribution or at
7 # https://www.openssl.org/source/license.html
10 # Wrapper around the ca to make it easier to use
12 # WARNING: do not edit!
13 # Generated by Makefile from apps/CA.pl.in
18 my $openssl = "openssl";
19 if(defined $ENV{'OPENSSL'}) {
20 $openssl = $ENV{'OPENSSL'};
22 $ENV{'OPENSSL'} = $openssl;
27 my $OPENSSL_CONFIG = $ENV{"OPENSSL_CONFIG"} || "";
28 my $DAYS = "-days 365";
29 my $CADAYS = "-days 1095"; # 3 years
30 my $REQ = "$openssl req $OPENSSL_CONFIG";
31 my $CA = "$openssl ca $OPENSSL_CONFIG";
32 my $VERIFY = "$openssl verify";
33 my $X509 = "$openssl x509";
34 my $PKCS12 = "$openssl pkcs12";
36 # default openssl.cnf file has setup as per the following
37 my $CATOP = "./demoCA";
38 my $CAKEY = "cakey.pem";
39 my $CAREQ = "careq.pem";
40 my $CACERT = "cacert.pem";
41 my $CACRL = "crl.pem";
44 my $NEWKEY = "newkey.pem";
45 my $NEWREQ = "newreq.pem";
46 my $NEWCERT = "newcert.pem";
47 my $NEWP12 = "newcert.p12";
49 my $WHAT = shift @ARGV || "";
52 # See if reason for a CRL entry is valid; exit if not.
57 if ($r eq 'unspecified' || $r eq 'keyCompromise'
58 || $r eq 'CACompromise' || $r eq 'affiliationChanged'
59 || $r eq 'superseded' || $r eq 'cessationOfOperation'
60 || $r eq 'certificateHold' || $r eq 'removeFromCRL') {
63 print STDERR "Invalid CRL reason; must be one of:\n";
64 print STDERR " unspecified, keyCompromise, CACompromise,\n";
65 print STDERR " affiliationChanged, superseded, cessationOfOperation\n";
66 print STDERR " certificateHold, removeFromCRL";
70 # Copy a PEM-format file; return like exit status (zero means ok)
73 my ($infile, $outfile, $bound) = @_;
76 open IN, $infile || die "Cannot open $infile, $!";
77 open OUT, ">$outfile" || die "Cannot write to $outfile, $!";
79 $found = 1 if /^-----BEGIN.*$bound/;
80 print OUT $_ if $found;
81 $found = 2, last if /^-----END.*$bound/;
85 return $found == 2 ? 0 : 1;
88 # Wrapper around system; useful for debugging. Returns just the exit status
92 print "====\n$cmd\n" if $verbose;
93 my $status = system($cmd);
94 print "==> $status\n====\n" if $verbose;
99 if ( $WHAT =~ /^(-\?|-h|-help)$/ ) {
100 print STDERR "usage: CA -newcert|-newreq|-newreq-nodes|-newca|-sign|-signcert|-verify\n";
101 print STDERR " CA -pkcs12 [certname]\n";
102 print STDERR " CA -crl|-revoke cert-filename [reason]\n";
105 if ($WHAT eq '-newcert' ) {
106 # create a certificate
107 $RET = run("$REQ -new -x509 -keyout $NEWKEY -out $NEWCERT $DAYS");
108 print "Cert is in $NEWCERT, private key is in $NEWKEY\n" if $RET == 0;
109 } elsif ($WHAT eq '-newreq' ) {
110 # create a certificate request
111 $RET = run("$REQ -new -keyout $NEWKEY -out $NEWREQ $DAYS");
112 print "Request is in $NEWREQ, private key is in $NEWKEY\n" if $RET == 0;
113 } elsif ($WHAT eq '-newreq-nodes' ) {
114 # create a certificate request
115 $RET = run("$REQ -new -nodes -keyout $NEWKEY -out $NEWREQ $DAYS");
116 print "Request is in $NEWREQ, private key is in $NEWKEY\n" if $RET == 0;
117 } elsif ($WHAT eq '-newca' ) {
118 # create the directory hierarchy
119 mkdir ${CATOP}, $DIRMODE;
120 mkdir "${CATOP}/certs", $DIRMODE;
121 mkdir "${CATOP}/crl", $DIRMODE ;
122 mkdir "${CATOP}/newcerts", $DIRMODE;
123 mkdir "${CATOP}/private", $DIRMODE;
124 open OUT, ">${CATOP}/index.txt";
126 open OUT, ">${CATOP}/crlnumber";
129 # ask user for existing CA certificate
130 print "CA certificate filename (or enter to create)\n";
131 $FILE = "" unless defined($FILE = <STDIN>);
134 copy_pemfile($FILE,"${CATOP}/private/$CAKEY", "PRIVATE");
135 copy_pemfile($FILE,"${CATOP}/$CACERT", "CERTIFICATE");
137 print "Making CA certificate ...\n";
138 $RET = run("$REQ -new -keyout"
139 . " ${CATOP}/private/$CAKEY"
140 . " -out ${CATOP}/$CAREQ");
141 $RET = run("$CA -create_serial"
142 . " -out ${CATOP}/$CACERT $CADAYS -batch"
143 . " -keyfile ${CATOP}/private/$CAKEY -selfsign"
144 . " -extensions v3_ca"
145 . " -infiles ${CATOP}/$CAREQ") if $RET == 0;
146 print "CA certificate is in ${CATOP}/$CACERT\n" if $RET == 0;
148 } elsif ($WHAT eq '-pkcs12' ) {
149 my $cname = $ARGV[1];
150 $cname = "My Certificate" unless defined $cname;
151 $RET = run("$PKCS12 -in $NEWCERT -inkey $NEWKEY"
152 . " -certfile ${CATOP}/$CACERT"
154 . " -export -name \"$cname\"");
155 print "PKCS #12 file is in $NEWP12\n" if $RET == 0;
156 } elsif ($WHAT eq '-xsign' ) {
157 $RET = run("$CA -policy policy_anything -infiles $NEWREQ");
158 } elsif ($WHAT eq '-sign' ) {
159 $RET = run("$CA -policy policy_anything -out $NEWCERT -infiles $NEWREQ");
160 print "Signed certificate is in $NEWCERT\n" if $RET == 0;
161 } elsif ($WHAT eq '-signCA' ) {
162 $RET = run("$CA -policy policy_anything -out $NEWCERT"
163 . " -extensions v3_ca -infiles $NEWREQ");
164 print "Signed CA certificate is in $NEWCERT\n" if $RET == 0;
165 } elsif ($WHAT eq '-signcert' ) {
166 $RET = run("$X509 -x509toreq -in $NEWREQ -signkey $NEWREQ"
168 $RET = run("$CA -policy policy_anything -out $NEWCERT"
169 . " -infiles tmp.pem") if $RET == 0;
170 print "Signed certificate is in $NEWCERT\n" if $RET == 0;
171 } elsif ($WHAT eq '-verify' ) {
172 my @files = @ARGV ? @ARGV : ( $NEWCERT );
174 foreach $file (@files) {
175 my $status = run("$VERIFY \"-CAfile\" ${CATOP}/$CACERT $file");
176 $RET = $status if $status != 0;
178 } elsif ($WHAT eq '-crl' ) {
179 $RET = run("$CA -gencrl -out ${CATOP}/crl/$CACRL");
180 print "Generated CRL is in ${CATOP}/crl/$CACRL\n" if $RET == 0;
181 } elsif ($WHAT eq '-revoke' ) {
182 my $cname = $ARGV[1];
183 if (!defined $cname) {
184 print "Certificate filename is required; reason optional.\n";
187 my $reason = $ARGV[2];
188 $reason = " -crl_reason $reason"
189 if defined $reason && crl_reason_ok($reason);
190 $RET = run("$CA -revoke \"$cname\"" . $reason);
192 print STDERR "Unknown arg \"$WHAT\"\n";
193 print STDERR "Use -help for help.\n";