]> WPIA git - cassiopeia.git/blob - lib/openssl/apps/CA.pl
3c54905b7ef4c0b1ee0308569af1fb80b8cd6f32
[cassiopeia.git] / lib / openssl / apps / CA.pl
1 #!/usr/bin/env perl
2 # Copyright 2000-2016 The OpenSSL Project Authors. All Rights Reserved.
3 #
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
8
9 #
10 # Wrapper around the ca to make it easier to use
11 #
12 # WARNING: do not edit!
13 # Generated by Makefile from apps/CA.pl.in
14
15 use strict;
16 use warnings;
17
18 my $openssl = "openssl";
19 if(defined $ENV{'OPENSSL'}) {
20     $openssl = $ENV{'OPENSSL'};
21 } else {
22     $ENV{'OPENSSL'} = $openssl;
23 }
24
25 my $verbose = 1;
26
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";
35
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";
42 my $DIRMODE = 0777;
43
44 my $NEWKEY = "newkey.pem";
45 my $NEWREQ = "newreq.pem";
46 my $NEWCERT = "newcert.pem";
47 my $NEWP12 = "newcert.p12";
48 my $RET = 0;
49 my $WHAT = shift @ARGV || "";
50 my $FILE;
51
52 # See if reason for a CRL entry is valid; exit if not.
53 sub crl_reason_ok
54 {
55     my $r = shift;
56
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') {
61         return 1;
62     }
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";
67     exit 1;
68 }
69
70 # Copy a PEM-format file; return like exit status (zero means ok)
71 sub copy_pemfile
72 {
73     my ($infile, $outfile, $bound) = @_;
74     my $found = 0;
75
76     open IN, $infile || die "Cannot open $infile, $!";
77     open OUT, ">$outfile" || die "Cannot write to $outfile, $!";
78     while (<IN>) {
79         $found = 1 if /^-----BEGIN.*$bound/;
80         print OUT $_ if $found;
81         $found = 2, last if /^-----END.*$bound/;
82     }
83     close IN;
84     close OUT;
85     return $found == 2 ? 0 : 1;
86 }
87
88 # Wrapper around system; useful for debugging.  Returns just the exit status
89 sub run
90 {
91     my $cmd = shift;
92     print "====\n$cmd\n" if $verbose;
93     my $status = system($cmd);
94     print "==> $status\n====\n" if $verbose;
95     return $status >> 8;
96 }
97
98
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";
103     exit 0;
104 }
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";
125     close OUT;
126     open OUT, ">${CATOP}/crlnumber";
127     print OUT "01\n";
128     close OUT;
129     # ask user for existing CA certificate
130     print "CA certificate filename (or enter to create)\n";
131     $FILE = "" unless defined($FILE = <STDIN>);
132     $FILE =~ s{\R$}{};
133     if ($FILE ne "") {
134         copy_pemfile($FILE,"${CATOP}/private/$CAKEY", "PRIVATE");
135         copy_pemfile($FILE,"${CATOP}/$CACERT", "CERTIFICATE");
136     } else {
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;
147     }
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"
153             . " -out $NEWP12"
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"
167             . " -out tmp.pem");
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 );
173     my $file;
174     foreach $file (@files) {
175         my $status = run("$VERIFY \"-CAfile\" ${CATOP}/$CACERT $file");
176         $RET = $status if $status != 0;
177     }
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";
185         exit 1;
186     }
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);
191 } else {
192     print STDERR "Unknown arg \"$WHAT\"\n";
193     print STDERR "Use -help for help.\n";
194     exit 1;
195 }
196
197 exit $RET;