2
0

mk-ca-bundle.pl 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  1. #!/usr/bin/perl -w
  2. # ***************************************************************************
  3. # * _ _ ____ _
  4. # * Project ___| | | | _ \| |
  5. # * / __| | | | |_) | |
  6. # * | (__| |_| | _ <| |___
  7. # * \___|\___/|_| \_\_____|
  8. # *
  9. # * Copyright (C) 1998 - 2013, Daniel Stenberg, <daniel@haxx.se>, et al.
  10. # *
  11. # * This software is licensed as described in the file COPYING, which
  12. # * you should have received as part of this distribution. The terms
  13. # * are also available at http://curl.haxx.se/docs/copyright.html.
  14. # *
  15. # * You may opt to use, copy, modify, merge, publish, distribute and/or sell
  16. # * copies of the Software, and permit persons to whom the Software is
  17. # * furnished to do so, under the terms of the COPYING file.
  18. # *
  19. # * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
  20. # * KIND, either express or implied.
  21. # *
  22. # ***************************************************************************
  23. # This Perl script creates a fresh ca-bundle.crt file for use with libcurl.
  24. # It downloads certdata.txt from Mozilla's source tree (see URL below),
  25. # then parses certdata.txt and extracts CA Root Certificates into PEM format.
  26. # These are then processed with the OpenSSL commandline tool to produce the
  27. # final ca-bundle.crt file.
  28. # The script is based on the parse-certs script written by Roland Krikava.
  29. # This Perl script works on almost any platform since its only external
  30. # dependency is the OpenSSL commandline tool for optional text listing.
  31. # Hacked by Guenter Knauf.
  32. #
  33. use Getopt::Std;
  34. use MIME::Base64;
  35. use LWP::UserAgent;
  36. use strict;
  37. use vars qw($opt_b $opt_f $opt_h $opt_i $opt_l $opt_n $opt_q $opt_t $opt_u $opt_v $opt_w);
  38. my $url = 'http://mxr.mozilla.org/mozilla/source/security/nss/lib/ckfw/builtins/certdata.txt?raw=1';
  39. # If the OpenSSL commandline is not in search path you can configure it here!
  40. my $openssl = 'openssl';
  41. my $version = '1.19';
  42. $opt_w = 76; # default base64 encoded lines length
  43. $0 =~ s@.*(/|\\)@@;
  44. $Getopt::Std::STANDARD_HELP_VERSION = 1;
  45. getopts('bfhilnqtuvw:');
  46. if ($opt_i) {
  47. print ("=" x 78 . "\n");
  48. print "Script Version : $version\n";
  49. print "Perl Version : $]\n";
  50. print "Operating System Name : $^O\n";
  51. print "Getopt::Std.pm Version : ${Getopt::Std::VERSION}\n";
  52. print "MIME::Base64.pm Version : ${MIME::Base64::VERSION}\n";
  53. print "LWP::UserAgent.pm Version : ${LWP::UserAgent::VERSION}\n";
  54. print "LWP.pm Version : ${LWP::VERSION}\n";
  55. print ("=" x 78 . "\n");
  56. }
  57. sub HELP_MESSAGE() {
  58. print "Usage:\t${0} [-b] [-f] [-i] [-l] [-n] [-q] [-t] [-u] [-v] [-w<l>] [<outputfile>]\n";
  59. print "\t-b\tbackup an existing version of ca-bundle.crt\n";
  60. print "\t-f\tforce rebuild even if certdata.txt is current\n";
  61. print "\t-i\tprint version info about used modules\n";
  62. print "\t-l\tprint license info about certdata.txt\n";
  63. print "\t-n\tno download of certdata.txt (to use existing)\n";
  64. print "\t-q\tbe really quiet (no progress output at all)\n";
  65. print "\t-t\tinclude plain text listing of certificates\n";
  66. print "\t-u\tunlink (remove) certdata.txt after processing\n";
  67. print "\t-v\tbe verbose and print out processed CAs\n";
  68. print "\t-w <l>\twrap base64 output lines after <l> chars (default: ${opt_w})\n";
  69. exit;
  70. }
  71. sub VERSION_MESSAGE() {
  72. print "${0} version ${version} running Perl ${]} on ${^O}\n";
  73. }
  74. HELP_MESSAGE() if ($opt_h);
  75. my $crt = $ARGV[0] || 'ca-bundle.crt';
  76. (my $txt = $url) =~ s@(.*/|\?.*)@@g;
  77. my $stdout = $crt eq '-';
  78. my $resp;
  79. my $fetched;
  80. unless ($opt_n and -e $txt) {
  81. print STDERR "Downloading '$txt' ...\n" if (!$opt_q);
  82. my $ua = new LWP::UserAgent(agent => "$0/$version");
  83. $ua->env_proxy();
  84. $resp = $ua->mirror($url, $txt);
  85. if ($resp && $resp->code eq '304') {
  86. print STDERR "Not modified\n" unless $opt_q;
  87. exit 0 if -e $crt && !$opt_f;
  88. } else {
  89. $fetched = 1;
  90. }
  91. if( !$resp || $resp->code !~ /^(?:200|304)$/ ) {
  92. print STDERR "Unable to download latest data: "
  93. . ($resp? $resp->code . ' - ' . $resp->message : "LWP failed") . "\n"
  94. unless $opt_q;
  95. exit 1 if -e $crt || ! -r $txt;
  96. }
  97. }
  98. my $currentdate = scalar gmtime($fetched ? $resp->last_modified : (stat($txt))[9]);
  99. my $format = $opt_t ? "plain text and " : "";
  100. if( $stdout ) {
  101. open(CRT, '> -') or die "Couldn't open STDOUT: $!\n";
  102. } else {
  103. open(CRT,">$crt.~") or die "Couldn't open $crt.~: $!\n";
  104. }
  105. print CRT <<EOT;
  106. ##
  107. ## $crt -- Bundle of CA Root Certificates
  108. ##
  109. ## Certificate data from Mozilla as of: ${currentdate}
  110. ##
  111. ## This is a bundle of X.509 certificates of public Certificate Authorities
  112. ## (CA). These were automatically extracted from Mozilla's root certificates
  113. ## file (certdata.txt). This file can be found in the mozilla source tree:
  114. ## ${url}
  115. ##
  116. ## It contains the certificates in ${format}PEM format and therefore
  117. ## can be directly used with curl / libcurl / php_curl, or with
  118. ## an Apache+mod_ssl webserver for SSL client authentication.
  119. ## Just configure this file as the SSLCACertificateFile.
  120. ##
  121. EOT
  122. print STDERR "Processing '$txt' ...\n" if (!$opt_q);
  123. my $caname;
  124. my $certnum = 0;
  125. my $skipnum = 0;
  126. my $start_of_cert = 0;
  127. open(TXT,"$txt") or die "Couldn't open $txt: $!\n";
  128. while (<TXT>) {
  129. if (/\*\*\*\*\* BEGIN LICENSE BLOCK \*\*\*\*\*/) {
  130. print CRT;
  131. print if ($opt_l);
  132. while (<TXT>) {
  133. print CRT;
  134. print if ($opt_l);
  135. last if (/\*\*\*\*\* END LICENSE BLOCK \*\*\*\*\*/);
  136. }
  137. }
  138. next if /^#|^\s*$/;
  139. chomp;
  140. if (/^CVS_ID\s+\"(.*)\"/) {
  141. print CRT "# $1\n";
  142. }
  143. # this is a match for the start of a certificate
  144. if (/^CKA_CLASS CK_OBJECT_CLASS CKO_CERTIFICATE/) {
  145. $start_of_cert = 1
  146. }
  147. if ($start_of_cert && /^CKA_LABEL UTF8 \"(.*)\"/) {
  148. $caname = $1;
  149. }
  150. my $untrusted = 1;
  151. if ($start_of_cert && /^CKA_VALUE MULTILINE_OCTAL/) {
  152. my $data;
  153. while (<TXT>) {
  154. last if (/^END/);
  155. chomp;
  156. my @octets = split(/\\/);
  157. shift @octets;
  158. for (@octets) {
  159. $data .= chr(oct);
  160. }
  161. }
  162. # scan forwards until the trust part
  163. while (<TXT>) {
  164. last if (/^CKA_CLASS CK_OBJECT_CLASS CKO_NSS_TRUST/);
  165. chomp;
  166. }
  167. # now scan the trust part for untrusted certs
  168. while (<TXT>) {
  169. last if (/^#/);
  170. if (/^CKA_TRUST_SERVER_AUTH\s+CK_TRUST\s+CKT_NSS_TRUSTED_DELEGATOR$/) {
  171. $untrusted = 0;
  172. }
  173. }
  174. if ($untrusted) {
  175. $skipnum ++;
  176. } else {
  177. my $encoded = MIME::Base64::encode_base64($data, '');
  178. $encoded =~ s/(.{1,${opt_w}})/$1\n/g;
  179. my $pem = "-----BEGIN CERTIFICATE-----\n"
  180. . $encoded
  181. . "-----END CERTIFICATE-----\n";
  182. print CRT "\n$caname\n";
  183. print CRT ("=" x length($caname) . "\n");
  184. if (!$opt_t) {
  185. print CRT $pem;
  186. } else {
  187. my $pipe = "|$openssl x509 -md5 -fingerprint -text -inform PEM";
  188. if (!$stdout) {
  189. $pipe .= " >> $crt.~";
  190. close(CRT) or die "Couldn't close $crt.~: $!";
  191. }
  192. open(TMP, $pipe) or die "Couldn't open openssl pipe: $!";
  193. print TMP $pem;
  194. close(TMP) or die "Couldn't close openssl pipe: $!";
  195. if (!$stdout) {
  196. open(CRT, ">>$crt.~") or die "Couldn't open $crt.~: $!";
  197. }
  198. }
  199. print STDERR "Parsing: $caname\n" if ($opt_v);
  200. $certnum ++;
  201. $start_of_cert = 0;
  202. }
  203. }
  204. }
  205. close(TXT) or die "Couldn't close $txt: $!\n";
  206. close(CRT) or die "Couldn't close $crt.~: $!\n";
  207. unless( $stdout ) {
  208. if ($opt_b && -e $crt) {
  209. my $bk = 1;
  210. while (-e "$crt.~${bk}~") {
  211. $bk++;
  212. }
  213. rename $crt, "$crt.~${bk}~" or die "Failed to create backup $crt.~$bk}~: $!\n";
  214. } elsif( -e $crt ) {
  215. unlink( $crt ) or die "Failed to remove $crt: $!\n";
  216. }
  217. rename "$crt.~", $crt or die "Failed to rename $crt.~ to $crt: $!\n";
  218. }
  219. unlink $txt if ($opt_u);
  220. print STDERR "Done ($certnum CA certs processed, $skipnum untrusted skipped).\n" if (!$opt_q);
  221. exit;