CA.pl 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  1. #!/croot/openssl_1687380633776/_build_env/bin/perl
  2. # Copyright 2000-2021 The OpenSSL Project Authors. All Rights Reserved.
  3. #
  4. # Licensed under the Apache License 2.0 (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. # Wrapper around the ca to make it easier to use
  10. #
  11. # WARNING: do not edit!
  12. # Generated by Makefile from apps/CA.pl.in
  13. use strict;
  14. use warnings;
  15. my $verbose = 1;
  16. my @OPENSSL_CMDS = ("req", "ca", "pkcs12", "x509", "verify");
  17. my $openssl = $ENV{'OPENSSL'} // "openssl";
  18. $ENV{'OPENSSL'} = $openssl;
  19. my $OPENSSL_CONFIG = $ENV{"OPENSSL_CONFIG"} // "";
  20. # Command invocations.
  21. my $REQ = "$openssl req $OPENSSL_CONFIG";
  22. my $CA = "$openssl ca $OPENSSL_CONFIG";
  23. my $VERIFY = "$openssl verify";
  24. my $X509 = "$openssl x509";
  25. my $PKCS12 = "$openssl pkcs12";
  26. # Default values for various configuration settings.
  27. my $CATOP = "./demoCA";
  28. my $CAKEY = "cakey.pem";
  29. my $CAREQ = "careq.pem";
  30. my $CACERT = "cacert.pem";
  31. my $CACRL = "crl.pem";
  32. my $DAYS = "-days 365";
  33. my $CADAYS = "-days 1095"; # 3 years
  34. my $NEWKEY = "newkey.pem";
  35. my $NEWREQ = "newreq.pem";
  36. my $NEWCERT = "newcert.pem";
  37. my $NEWP12 = "newcert.p12";
  38. # Commandline parsing
  39. my %EXTRA;
  40. my $WHAT = shift @ARGV || "";
  41. @ARGV = parse_extra(@ARGV);
  42. my $RET = 0;
  43. # Split out "-extra-CMD value", and return new |@ARGV|. Fill in
  44. # |EXTRA{CMD}| with list of values.
  45. sub parse_extra
  46. {
  47. foreach ( @OPENSSL_CMDS ) {
  48. $EXTRA{$_} = '';
  49. }
  50. my @result;
  51. while ( scalar(@_) > 0 ) {
  52. my $arg = shift;
  53. if ( $arg !~ m/-extra-([a-z0-9]+)/ ) {
  54. push @result, $arg;
  55. next;
  56. }
  57. $arg =~ s/-extra-//;
  58. die("Unknown \"-${arg}-extra\" option, exiting")
  59. unless scalar grep { $arg eq $_ } @OPENSSL_CMDS;
  60. $EXTRA{$arg} .= " " . shift;
  61. }
  62. return @result;
  63. }
  64. # See if reason for a CRL entry is valid; exit if not.
  65. sub crl_reason_ok
  66. {
  67. my $r = shift;
  68. if ($r eq 'unspecified' || $r eq 'keyCompromise'
  69. || $r eq 'CACompromise' || $r eq 'affiliationChanged'
  70. || $r eq 'superseded' || $r eq 'cessationOfOperation'
  71. || $r eq 'certificateHold' || $r eq 'removeFromCRL') {
  72. return 1;
  73. }
  74. print STDERR "Invalid CRL reason; must be one of:\n";
  75. print STDERR " unspecified, keyCompromise, CACompromise,\n";
  76. print STDERR " affiliationChanged, superseded, cessationOfOperation\n";
  77. print STDERR " certificateHold, removeFromCRL";
  78. exit 1;
  79. }
  80. # Copy a PEM-format file; return like exit status (zero means ok)
  81. sub copy_pemfile
  82. {
  83. my ($infile, $outfile, $bound) = @_;
  84. my $found = 0;
  85. open IN, $infile || die "Cannot open $infile, $!";
  86. open OUT, ">$outfile" || die "Cannot write to $outfile, $!";
  87. while (<IN>) {
  88. $found = 1 if /^-----BEGIN.*$bound/;
  89. print OUT $_ if $found;
  90. $found = 2, last if /^-----END.*$bound/;
  91. }
  92. close IN;
  93. close OUT;
  94. return $found == 2 ? 0 : 1;
  95. }
  96. # Wrapper around system; useful for debugging. Returns just the exit status
  97. sub run
  98. {
  99. my $cmd = shift;
  100. print "====\n$cmd\n" if $verbose;
  101. my $status = system($cmd);
  102. print "==> $status\n====\n" if $verbose;
  103. return $status >> 8;
  104. }
  105. if ( $WHAT =~ /^(-\?|-h|-help)$/ ) {
  106. print STDERR <<EOF;
  107. Usage:
  108. CA.pl -newcert | -newreq | -newreq-nodes | -xsign | -sign | -signCA | -signcert | -crl | -newca [-extra-cmd parameter]
  109. CA.pl -pkcs12 [certname]
  110. CA.pl -verify certfile ...
  111. CA.pl -revoke certfile [reason]
  112. EOF
  113. exit 0;
  114. }
  115. if ($WHAT eq '-newcert' ) {
  116. # create a certificate
  117. $RET = run("$REQ -new -x509 -keyout $NEWKEY -out $NEWCERT $DAYS"
  118. . " $EXTRA{req}");
  119. print "Cert is in $NEWCERT, private key is in $NEWKEY\n" if $RET == 0;
  120. } elsif ($WHAT eq '-precert' ) {
  121. # create a pre-certificate
  122. $RET = run("$REQ -x509 -precert -keyout $NEWKEY -out $NEWCERT $DAYS"
  123. . " $EXTRA{req}");
  124. print "Pre-cert is in $NEWCERT, private key is in $NEWKEY\n" if $RET == 0;
  125. } elsif ($WHAT =~ /^\-newreq(\-nodes)?$/ ) {
  126. # create a certificate request
  127. $RET = run("$REQ -new $1 -keyout $NEWKEY -out $NEWREQ $DAYS $EXTRA{req}");
  128. print "Request is in $NEWREQ, private key is in $NEWKEY\n" if $RET == 0;
  129. } elsif ($WHAT eq '-newca' ) {
  130. # create the directory hierarchy
  131. my @dirs = ( "${CATOP}", "${CATOP}/certs", "${CATOP}/crl",
  132. "${CATOP}/newcerts", "${CATOP}/private" );
  133. die "${CATOP}/index.txt exists.\nRemove old sub-tree to proceed,"
  134. if -f "${CATOP}/index.txt";
  135. die "${CATOP}/serial exists.\nRemove old sub-tree to proceed,"
  136. if -f "${CATOP}/serial";
  137. foreach my $d ( @dirs ) {
  138. if ( -d $d ) {
  139. warn "Directory $d exists" if -d $d;
  140. } else {
  141. mkdir $d or die "Can't mkdir $d, $!";
  142. }
  143. }
  144. open OUT, ">${CATOP}/index.txt";
  145. close OUT;
  146. open OUT, ">${CATOP}/crlnumber";
  147. print OUT "01\n";
  148. close OUT;
  149. # ask user for existing CA certificate
  150. print "CA certificate filename (or enter to create)\n";
  151. my $FILE;
  152. $FILE = "" unless defined($FILE = <STDIN>);
  153. $FILE =~ s{\R$}{};
  154. if ($FILE ne "") {
  155. copy_pemfile($FILE,"${CATOP}/private/$CAKEY", "PRIVATE");
  156. copy_pemfile($FILE,"${CATOP}/$CACERT", "CERTIFICATE");
  157. } else {
  158. print "Making CA certificate ...\n";
  159. $RET = run("$REQ -new -keyout ${CATOP}/private/$CAKEY"
  160. . " -out ${CATOP}/$CAREQ $EXTRA{req}");
  161. $RET = run("$CA -create_serial"
  162. . " -out ${CATOP}/$CACERT $CADAYS -batch"
  163. . " -keyfile ${CATOP}/private/$CAKEY -selfsign"
  164. . " -extensions v3_ca"
  165. . " -infiles ${CATOP}/$CAREQ $EXTRA{ca}") if $RET == 0;
  166. print "CA certificate is in ${CATOP}/$CACERT\n" if $RET == 0;
  167. }
  168. } elsif ($WHAT eq '-pkcs12' ) {
  169. my $cname = $ARGV[0];
  170. $cname = "My Certificate" unless defined $cname;
  171. $RET = run("$PKCS12 -in $NEWCERT -inkey $NEWKEY"
  172. . " -certfile ${CATOP}/$CACERT -out $NEWP12"
  173. . " -export -name \"$cname\" $EXTRA{pkcs12}");
  174. print "PKCS #12 file is in $NEWP12\n" if $RET == 0;
  175. } elsif ($WHAT eq '-xsign' ) {
  176. $RET = run("$CA -policy policy_anything -infiles $NEWREQ $EXTRA{ca}");
  177. } elsif ($WHAT eq '-sign' ) {
  178. $RET = run("$CA -policy policy_anything -out $NEWCERT"
  179. . " -infiles $NEWREQ $EXTRA{ca}");
  180. print "Signed certificate is in $NEWCERT\n" if $RET == 0;
  181. } elsif ($WHAT eq '-signCA' ) {
  182. $RET = run("$CA -policy policy_anything -out $NEWCERT"
  183. . " -extensions v3_ca -infiles $NEWREQ $EXTRA{ca}");
  184. print "Signed CA certificate is in $NEWCERT\n" if $RET == 0;
  185. } elsif ($WHAT eq '-signcert' ) {
  186. $RET = run("$X509 -x509toreq -in $NEWREQ -signkey $NEWREQ"
  187. . " -out tmp.pem $EXTRA{x509}");
  188. $RET = run("$CA -policy policy_anything -out $NEWCERT"
  189. . "-infiles tmp.pem $EXTRA{ca}") if $RET == 0;
  190. print "Signed certificate is in $NEWCERT\n" if $RET == 0;
  191. } elsif ($WHAT eq '-verify' ) {
  192. my @files = @ARGV ? @ARGV : ( $NEWCERT );
  193. foreach my $file (@files) {
  194. # -CAfile quoted for VMS, since the C RTL downcases all unquoted
  195. # arguments to C programs
  196. my $status = run("$VERIFY \"-CAfile\" ${CATOP}/$CACERT $file $EXTRA{verify}");
  197. $RET = $status if $status != 0;
  198. }
  199. } elsif ($WHAT eq '-crl' ) {
  200. $RET = run("$CA -gencrl -out ${CATOP}/crl/$CACRL $EXTRA{ca}");
  201. print "Generated CRL is in ${CATOP}/crl/$CACRL\n" if $RET == 0;
  202. } elsif ($WHAT eq '-revoke' ) {
  203. my $cname = $ARGV[0];
  204. if (!defined $cname) {
  205. print "Certificate filename is required; reason optional.\n";
  206. exit 1;
  207. }
  208. my $reason = $ARGV[1];
  209. $reason = " -crl_reason $reason"
  210. if defined $reason && crl_reason_ok($reason);
  211. $RET = run("$CA -revoke \"$cname\"" . $reason . $EXTRA{ca});
  212. } else {
  213. print STDERR "Unknown arg \"$WHAT\"\n";
  214. print STDERR "Use -help for help.\n";
  215. exit 1;
  216. }
  217. exit $RET;