mk-ca-bundle.pl 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. #!/usr/bin/perl -w
  2. # ***************************************************************************
  3. # * _ _ ____ _
  4. # * Project ___| | | | _ \| |
  5. # * / __| | | | |_) | |
  6. # * | (__| |_| | _ <| |___
  7. # * \___|\___/|_| \_\_____|
  8. # *
  9. # * Copyright (C) 1998 - 2008, 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. # * $Id$
  23. # ***************************************************************************
  24. # This Perl script creates a fresh ca-bundle.crt file for use with libcurl.
  25. # It downloads certdata.txt from Mozilla's source tree (see URL below),
  26. # then parses certdata.txt and extracts CA Root Certificates into PEM format.
  27. # These are then processed with the OpenSSL commandline tool to produce the
  28. # final ca-bundle.crt file.
  29. # The script is based on the parse-certs script written by Roland Krikava.
  30. # This Perl script works on almost any platform since its only external
  31. # dependency is the OpenSSL commandline tool for optional text listing.
  32. # Hacked by Guenter Knauf.
  33. #
  34. use Getopt::Std;
  35. use MIME::Base64;
  36. use LWP::UserAgent;
  37. use strict;
  38. use vars qw($opt_b $opt_h $opt_i $opt_l $opt_n $opt_q $opt_t $opt_u $opt_v);
  39. my $url = 'http://mxr.mozilla.org/seamonkey/source/security/nss/lib/ckfw/builtins/certdata.txt?raw=1';
  40. # If the OpenSSL commandline is not in search path you can configure it here!
  41. my $openssl = 'openssl';
  42. my $version = $1 if ('$Revision$' =~ /\s(\d+\.\d+)\s/);
  43. getopts('bhilnqtuv');
  44. if ($opt_i) {
  45. print ("=" x 78 . "\n");
  46. print "Script Version : $version\n";
  47. print "Perl Version : $]\n";
  48. print "Operating System Name : $^O\n";
  49. print "Getopt::Std.pm Version : ${Getopt::Std::VERSION}\n";
  50. print "MIME::Base64.pm Version : ${MIME::Base64::VERSION}\n";
  51. print "LWP::UserAgent.pm Version : ${LWP::UserAgent::VERSION}\n";
  52. print "LWP.pm Version : ${LWP::VERSION}\n";
  53. print ("=" x 78 . "\n");
  54. }
  55. $0 =~ s/\\/\//g;
  56. $0 = substr($0, rindex($0, '/') + 1);
  57. if ($opt_h) {
  58. printf("Usage:\t%s [-b] [-i] [-l] [-n] [-q] [-t] [-u] [-v] [<outputfile>]\n", $0);
  59. print "\t-b\tbackup an existing version of ca-bundle.crt\n";
  60. print "\t-i\tprint version info about used modules\n";
  61. print "\t-l\tprint license info about certdata.txt\n";
  62. print "\t-n\tno download of certdata.txt (to use existing)\n";
  63. print "\t-q\tbe really quiet (no progress output at all)\n";
  64. print "\t-t\tinclude plain text listing of certificates\n";
  65. print "\t-u\tunlink (remove) certdata.txt after processing\n";
  66. print "\t-v\tbe verbose and print out processed CAs\n";
  67. exit;
  68. }
  69. my $crt = $ARGV[0] || 'ca-bundle.crt';
  70. my $txt = substr($url, rindex($url, '/') + 1);
  71. $txt =~ s/\?.*//;
  72. if (!$opt_n || !-e $txt) {
  73. print "Downloading '$txt' ...\n" if (!$opt_q);
  74. my $ua = new LWP::UserAgent(agent => "$0/$version");
  75. my $req = new HTTP::Request('GET', $url);
  76. my $res = $ua->request($req);
  77. if ($res->is_success) {
  78. open(TXT,">$txt") or die "Couldn't open $txt: $!";
  79. print TXT $res->content . "\n";
  80. close(TXT) or die "Couldn't close $txt: $!";
  81. } else {
  82. die $res->status_line;
  83. }
  84. }
  85. if ($opt_b && -e $crt) {
  86. my $bk = 1;
  87. while (-e "$crt.~${bk}~") {
  88. $bk++;
  89. }
  90. rename $crt, "$crt.~${bk}~";
  91. }
  92. my $format = $opt_t ? "plain text and " : "";
  93. my $currentdate = scalar gmtime() . " UTC";
  94. open(CRT,">$crt") or die "Couldn't open $crt: $!";
  95. print CRT <<EOT;
  96. ##
  97. ## $crt -- Bundle of CA Root Certificates
  98. ##
  99. ## Converted at: ${currentdate}
  100. ##
  101. ## This is a bundle of X.509 certificates of public Certificate Authorities
  102. ## (CA). These were automatically extracted from Mozilla's root certificates
  103. ## file (certdata.txt). This file can be found in the mozilla source tree:
  104. ## '/mozilla/security/nss/lib/ckfw/builtins/certdata.txt'
  105. ##
  106. ## It contains the certificates in ${format}PEM format and therefore
  107. ## can be directly used with curl / libcurl / php_curl, or with
  108. ## an Apache+mod_ssl webserver for SSL client authentication.
  109. ## Just configure this file as the SSLCACertificateFile.
  110. ##
  111. EOT
  112. close(CRT) or die "Couldn't close $crt: $!";
  113. print "Processing '$txt' ...\n" if (!$opt_q);
  114. my $caname;
  115. my $certnum = 0;
  116. open(TXT,"$txt") or die "Couldn't open $txt: $!";
  117. while (<TXT>) {
  118. if (/\*\*\*\*\* BEGIN LICENSE BLOCK \*\*\*\*\*/) {
  119. open(CRT, ">>$crt") or die "Couldn't open $crt: $!";
  120. print CRT;
  121. print if ($opt_l);
  122. while (<TXT>) {
  123. print CRT;
  124. print if ($opt_l);
  125. last if (/\*\*\*\*\* END LICENSE BLOCK \*\*\*\*\*/);
  126. }
  127. close(CRT) or die "Couldn't close $crt: $!";
  128. }
  129. next if /^#|^\s*$/;
  130. chomp;
  131. if (/^CVS_ID\s+\"(.*)\"/) {
  132. open(CRT, ">>$crt") or die "Couldn't open $crt: $!";
  133. print CRT "# $1\n";
  134. close(CRT) or die "Couldn't close $crt: $!";
  135. }
  136. if (/^CKA_LABEL\s+[A-Z0-9]+\s+\"(.*)\"/) {
  137. $caname = $1;
  138. }
  139. if (/^CKA_VALUE MULTILINE_OCTAL/) {
  140. my $data;
  141. while (<TXT>) {
  142. last if (/^END/);
  143. chomp;
  144. my @octets = split(/\\/);
  145. shift @octets;
  146. for (@octets) {
  147. $data .= chr(oct);
  148. }
  149. }
  150. my $pem = "-----BEGIN CERTIFICATE-----\n"
  151. . MIME::Base64::encode($data)
  152. . "-----END CERTIFICATE-----\n";
  153. open(CRT, ">>$crt") or die "Couldn't open $crt: $!";
  154. print CRT "\n$caname\n";
  155. print CRT ("=" x length($caname) . "\n");
  156. if (!$opt_t) {
  157. print CRT $pem;
  158. }
  159. close(CRT) or die "Couldn't close $crt: $!";
  160. if ($opt_t) {
  161. open(TMP, "|$openssl x509 -md5 -fingerprint -text -inform PEM >> $crt") or die "Couldn't open openssl pipe: $!";
  162. print TMP $pem;
  163. close(TMP) or die "Couldn't close openssl pipe: $!";
  164. }
  165. print "Parsing: $caname\n" if ($opt_v);
  166. $certnum ++;
  167. }
  168. }
  169. close(TXT) or die "Couldn't close $txt: $!";
  170. unlink $txt if ($opt_u);
  171. print "Done ($certnum CA certs processed).\n" if (!$opt_q);
  172. exit;