2
0

mk-ca-bundle.pl 6.2 KB

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