2 # ***************************************************************************
4 # * Project ___| | | | _ \| |
5 # * / __| | | | |_) | |
6 # * | (__| |_| | _ <| |___
7 # * \___|\___/|_| \_\_____|
9 # * Copyright (C) 1998 - 2008, Daniel Stenberg, <daniel@haxx.se>, et al.
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.
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.
19 # * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
20 # * KIND, either express or implied.
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 writtten by Roland Krikava.
30 # This Perl script works on almost any platform since its only external
31 # dependency is the OpenSSL commandline tool.
32 # Hacked by Guenter Knauf.
38 my $url = 'http://lxr.mozilla.org/seamonkey/source/security/nss/lib/ckfw/builtins/certdata.txt?raw=1';
39 my $crt = 'ca-bundle.crt';
40 my $tmp = 'mytmpfile.txt';
41 # If the OpenSSL commandline is not in search path you can configure it here!
42 my $openssl = 'openssl';
48 printf("Usage:\t%s [-i] [-l] [-n] [-u] [-v]\n", substr($0, rindex($0, '/') + 1));
49 print "\t-i\tprint version info about used modules\n";
50 print "\t-l\tprint license info about certdata.txt\n";
51 print "\t-n\tno download of certdata.txt (to use existing)\n";
52 print "\t-u\tunlink (remove) certdata.txt after processing\n";
53 print "\t-v\tbe verbose and print out processed CAs\n";
58 print "Perl Version : $]\n";
59 print "Operating System Name : $^O\n";
60 printf("MIME::Base64.pm Version : %s\n", $MIME::Base64::VERSION);
61 printf("LWP::UserAgent.pm Version : %s\n", $LWP::UserAgent::VERSION);
62 print ("=" x 78 . "\n");
65 my $txt = substr($url, rindex($url, '/') + 1);
68 print "Downloading '$txt' ...\n" if ($opt_v);
69 my $ua = new LWP::UserAgent;
70 my $req = new HTTP::Request('GET', $url);
71 my $res = $ua->request($req);
72 if ($res->is_success) {
73 open(TXT,">$txt") or die "Couldn't open $txt: $!";
74 print TXT $res->content . "\n";
75 close(TXT) or die "Couldn't close $txt: $!";
77 die $res->status_line;
81 die "File '$txt' doesnt exist - dont use -n switch to download the file!\n" if (!-e $txt);
83 my $currentdate = scalar gmtime() . " UTC";
84 open(CRT,">$crt") or die "Couldn't open $crt: $!";
87 ## $crt -- Bundle of CA Root Certificates
89 ## Converted at: $currentdate
91 ## This is a bundle of X.509 certificates of public Certificate Authorities
92 ## (CA). These were automatically extracted from Mozilla's root certificates
93 ## file (certdata.txt). This file can be found in the mozilla source tree:
94 ## '/mozilla/security/nss/lib/ckfw/builtins/certdata.txt'
96 ## It contains the certificates in both plain text and PEM format
97 ## and therefore can be directly used with curl / libcurl, or with an
98 ## Apache+mod_ssl webserver for SSL client authentication.
99 ## Just configure this file as the SSLCACertificateFile.
104 close(CRT) or die "Couldn't close $crt: $!";
107 open(TXT,"$txt") or die "Couldn't open $file: $!";
109 if (/\*\*\*\*\* BEGIN LICENSE BLOCK \*\*\*\*\*/) {
110 open(CRT, ">>$crt") or die "Couldn't open $crt: $!";
116 last if (/\*\*\*\*\* END LICENSE BLOCK \*\*\*\*\*/);
118 close(CRT) or die "Couldn't close $crt: $!";
123 if (/^CVS_ID\s+\"(.*)\"/) {
124 open(CRT, ">>$crt") or die "Couldn't open $crt: $!";
126 close(CRT) or die "Couldn't close $crt: $!";
128 if (/^CKA_LABEL\s+[A-Z0-9]+\s+\"(.*)\"/) {
131 if (/^CKA_VALUE MULTILINE_OCTAL/) {
136 my @octets = split(/\\/);
142 open(CRT, ">>$crt") or die "Couldn't open $crt: $!";
145 print CRT ("=" x length($val) . "\n");
146 close(CRT) or die "Couldn't close $file: $!";
147 open(TMP, ">$tmp") or die "Couldn't open $tmp: $!";
149 print TMP "-----BEGIN CERTIFICATE-----\n";
150 print TMP MIME::Base64::encode($data);
151 print TMP "-----END CERTIFICATE-----\n";
152 close(TMP) or die "Couldn't close $tmp: $!";
153 system("$openssl x509 -md5 -fingerprint -text -in $tmp -inform PEM >> $crt");
154 print "Parsing: $val\n" if ($opt_v);
158 close(TXT) or die "Couldn't close $txt: $!";
159 unlink $txt if ($opt_u);
161 print "Done ($certnum CA certs processed).\n";