mk-ca-bundle.pl 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  1. #!/usr/bin/perl -w
  2. # ***************************************************************************
  3. # * _ _ ____ _
  4. # * Project ___| | | | _ \| |
  5. # * / __| | | | |_) | |
  6. # * | (__| |_| | _ <| |___
  7. # * \___|\___/|_| \_\_____|
  8. # *
  9. # * Copyright (C) 1998 - 2011, 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_h $opt_i $opt_l $opt_n $opt_q $opt_t $opt_u $opt_v);
  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.16';
  42. getopts('bhilnqtuv');
  43. if ($opt_i) {
  44. print ("=" x 78 . "\n");
  45. print "Script Version : $version\n";
  46. print "Perl Version : $]\n";
  47. print "Operating System Name : $^O\n";
  48. print "Getopt::Std.pm Version : ${Getopt::Std::VERSION}\n";
  49. print "MIME::Base64.pm Version : ${MIME::Base64::VERSION}\n";
  50. print "LWP::UserAgent.pm Version : ${LWP::UserAgent::VERSION}\n";
  51. print "LWP.pm Version : ${LWP::VERSION}\n";
  52. print ("=" x 78 . "\n");
  53. }
  54. $0 =~ s@.*(/|\\)@@;
  55. if ($opt_h) {
  56. printf("Usage:\t%s [-b] [-i] [-l] [-n] [-q] [-t] [-u] [-v] [<outputfile>]\n", $0);
  57. print "\t-b\tbackup an existing version of ca-bundle.crt\n";
  58. print "\t-i\tprint version info about used modules\n";
  59. print "\t-l\tprint license info about certdata.txt\n";
  60. print "\t-n\tno download of certdata.txt (to use existing)\n";
  61. print "\t-q\tbe really quiet (no progress output at all)\n";
  62. print "\t-t\tinclude plain text listing of certificates\n";
  63. print "\t-u\tunlink (remove) certdata.txt after processing\n";
  64. print "\t-v\tbe verbose and print out processed CAs\n";
  65. exit;
  66. }
  67. my $crt = $ARGV[0] || 'ca-bundle.crt';
  68. (my $txt = $url) =~ s@(.*/|\?.*)@@g;
  69. my $resp;
  70. unless ($opt_n and -e $txt) {
  71. print "Downloading '$txt' ...\n" if (!$opt_q);
  72. my $ua = new LWP::UserAgent(agent => "$0/$version");
  73. $ua->env_proxy();
  74. $resp = $ua->mirror($url, $txt);
  75. if ($resp && $resp->code eq '304') {
  76. print "Not modified\n" unless $opt_q;
  77. exit 0;
  78. }
  79. }
  80. my $currentdate = scalar gmtime($resp ? $resp->last_modified : (stat($txt))[9]);
  81. if ($opt_b && -e $crt) {
  82. my $bk = 1;
  83. while (-e "$crt.~${bk}~") {
  84. $bk++;
  85. }
  86. rename $crt, "$crt.~${bk}~";
  87. }
  88. my $format = $opt_t ? "plain text and " : "";
  89. open(CRT,">$crt") or die "Couldn't open $crt: $!";
  90. print CRT <<EOT;
  91. ##
  92. ## $crt -- Bundle of CA Root Certificates
  93. ##
  94. ## Certificate data from Mozilla as of: ${currentdate}
  95. ##
  96. ## This is a bundle of X.509 certificates of public Certificate Authorities
  97. ## (CA). These were automatically extracted from Mozilla's root certificates
  98. ## file (certdata.txt). This file can be found in the mozilla source tree:
  99. ## $url
  100. ##
  101. ## It contains the certificates in ${format}PEM format and therefore
  102. ## can be directly used with curl / libcurl / php_curl, or with
  103. ## an Apache+mod_ssl webserver for SSL client authentication.
  104. ## Just configure this file as the SSLCACertificateFile.
  105. ##
  106. EOT
  107. close(CRT) or die "Couldn't close $crt: $!";
  108. print "Processing '$txt' ...\n" if (!$opt_q);
  109. my $caname;
  110. my $certnum = 0;
  111. my $skipnum = 0;
  112. open(TXT,"$txt") or die "Couldn't open $txt: $!";
  113. while (<TXT>) {
  114. if (/\*\*\*\*\* BEGIN LICENSE BLOCK \*\*\*\*\*/) {
  115. open(CRT, ">>$crt") or die "Couldn't open $crt: $!";
  116. print CRT;
  117. print if ($opt_l);
  118. while (<TXT>) {
  119. print CRT;
  120. print if ($opt_l);
  121. last if (/\*\*\*\*\* END LICENSE BLOCK \*\*\*\*\*/);
  122. }
  123. close(CRT) or die "Couldn't close $crt: $!";
  124. }
  125. next if /^#|^\s*$/;
  126. chomp;
  127. if (/^CVS_ID\s+\"(.*)\"/) {
  128. open(CRT, ">>$crt") or die "Couldn't open $crt: $!";
  129. print CRT "# $1\n";
  130. close(CRT) or die "Couldn't close $crt: $!";
  131. }
  132. if (/^CKA_LABEL\s+[A-Z0-9]+\s+\"(.*)\"/) {
  133. $caname = $1;
  134. }
  135. my $untrusted = 0;
  136. if (/^CKA_VALUE MULTILINE_OCTAL/) {
  137. my $data;
  138. while (<TXT>) {
  139. last if (/^END/);
  140. chomp;
  141. my @octets = split(/\\/);
  142. shift @octets;
  143. for (@octets) {
  144. $data .= chr(oct);
  145. }
  146. }
  147. while (<TXT>) {
  148. last if (/^#$/);
  149. $untrusted = 1 if (/^CKA_TRUST_SERVER_AUTH\s+CK_TRUST\s+CKT_NSS_NOT_TRUSTED$/
  150. or /^CKA_TRUST_SERVER_AUTH\s+CK_TRUST\s+CKT_NSS_TRUST_UNKNOWN$/);
  151. }
  152. if ($untrusted) {
  153. $skipnum ++;
  154. } else {
  155. my $pem = "-----BEGIN CERTIFICATE-----\n"
  156. . MIME::Base64::encode($data)
  157. . "-----END CERTIFICATE-----\n";
  158. open(CRT, ">>$crt") or die "Couldn't open $crt: $!";
  159. print CRT "\n$caname\n";
  160. print CRT ("=" x length($caname) . "\n");
  161. if (!$opt_t) {
  162. print CRT $pem;
  163. }
  164. close(CRT) or die "Couldn't close $crt: $!";
  165. if ($opt_t) {
  166. open(TMP, "|$openssl x509 -md5 -fingerprint -text -inform PEM >> $crt") or die "Couldn't open openssl pipe: $!";
  167. print TMP $pem;
  168. close(TMP) or die "Couldn't close openssl pipe: $!";
  169. }
  170. print "Parsing: $caname\n" if ($opt_v);
  171. $certnum ++;
  172. }
  173. }
  174. }
  175. close(TXT) or die "Couldn't close $txt: $!";
  176. unlink $txt if ($opt_u);
  177. print "Done ($certnum CA certs processed, $skipnum untrusted skipped).\n" if (!$opt_q);
  178. exit;