2 # ***************************************************************************
4 # * Project ___| | | | _ \| |
5 # * / __| | | | |_) | |
6 # * | (__| |_| | _ <| |___
7 # * \___|\___/|_| \_\_____|
9 # * Copyright (C) 1998 - 2014, 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.
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.
33 use File::Basename 'dirname';
37 use vars qw($opt_h $opt_i $opt_l $opt_p $opt_q $opt_s $opt_t $opt_v $opt_w);
41 # If the OpenSSL commandline is not in search path you can configure it here!
42 my $openssl = 'openssl';
46 $opt_w = 72; # default base64 encoded lines length
48 # default cert types to include in the output (default is to include CAs which may issue SSL server certs)
49 my $default_mozilla_trust_purposes = "SERVER_AUTH";
50 my $default_mozilla_trust_levels = "TRUSTED_DELEGATOR";
51 $opt_p = $default_mozilla_trust_purposes . ":" . $default_mozilla_trust_levels;
53 my @valid_mozilla_trust_purposes = (
72 my @valid_mozilla_trust_levels = (
73 "TRUSTED_DELEGATOR", # CAs
74 "NOT_TRUSTED", # Don't trust these certs.
75 "MUST_VERIFY_TRUST", # This explicitly tells us that it ISN'T a CA but is otherwise ok. In other words, this should tell the app to ignore any other sources that claim this is a CA.
76 "TRUSTED" # This cert is trusted, but only for itself and not for delegates (i.e. it is not a CA).
79 my $default_signature_algorithms = $opt_s = "MD5";
81 my @valid_signature_algorithms = (
90 $Getopt::Std::STANDARD_HELP_VERSION = 1;
91 getopts('bd:fhilnp:qs:tuvw:');
94 print ("=" x 78 . "\n");
95 print "Script Version : $version\n";
96 print "Perl Version : $]\n";
97 print "Operating System Name : $^O\n";
98 print "Getopt::Std.pm Version : ${Getopt::Std::VERSION}\n";
99 print "MIME::Base64.pm Version : ${MIME::Base64::VERSION}\n";
100 print ("=" x 78 . "\n");
104 print "Usage:\t${0} [-i] [-l] [-p<purposes:levels>] [-q] [-s<algorithms>] [-t] [-v] [-w<l>] [<outputfile>]\n";
105 print "\t-i\tprint version info about used modules\n";
106 print "\t-l\tprint license info about certdata.txt\n";
107 print wrap("\t","\t\t", "-p\tlist of Mozilla trust purposes and levels for certificates to include in output. Takes the form of a comma separated list of purposes, a colon, and a comma separated list of levels. (default: $default_mozilla_trust_purposes:$default_mozilla_trust_levels)"), "\n";
108 print "\t\t Valid purposes are:\n";
109 print wrap("\t\t ","\t\t ", join( ", ", "ALL", @valid_mozilla_trust_purposes ) ), "\n";
110 print "\t\t Valid levels are:\n";
111 print wrap("\t\t ","\t\t ", join( ", ", "ALL", @valid_mozilla_trust_levels ) ), "\n";
112 print "\t-q\tbe really quiet (no progress output at all)\n";
113 print wrap("\t","\t\t", "-s\tcomma separated list of certificate signatures/hashes to output in plain text mode. (default: $default_signature_algorithms)\n");
114 print "\t\t Valid signature algorithms are:\n";
115 print wrap("\t\t ","\t\t ", join( ", ", "ALL", @valid_signature_algorithms ) ), "\n";
116 print "\t-t\tinclude plain text listing of certificates\n";
117 print "\t-v\tbe verbose and print out processed CAs\n";
118 print "\t-w <l>\twrap base64 output lines after <l> chars (default: ${opt_w})\n";
122 sub VERSION_MESSAGE() {
123 print "${0} version ${version} running Perl ${]} on ${^O}\n";
126 HELP_MESSAGE() if ($opt_h);
131 print STDERR $output . "\n" unless $opt_q;
137 return defined(List::Util::first { $target eq $_ } @_);
140 # Parses $param_string as a case insensitive comma separated list with optional whitespace
141 # validates that only allowed parameters are supplied
142 sub parse_csv_param($$@) {
143 my $description = shift;
144 my $param_string = shift;
145 my @valid_values = @_;
148 s/^\s+//; # strip leading spaces
149 s/\s+$//; # strip trailing spaces
150 uc $_ # return the modified string as upper case
151 } split( ',', $param_string );
153 # Find all values which are not in the list of valid values or "ALL"
154 my @invalid = grep { !is_in_list($_,"ALL",@valid_values) } @values;
156 if ( scalar(@invalid) > 0 ) {
157 # Tell the user which parameters were invalid and print the standard help message which will exit
158 print "Error: Invalid ", $description, scalar(@invalid) == 1 ? ": " : "s: ", join( ", ", map { "\"$_\"" } @invalid ), "\n";
162 @values = @valid_values if ( is_in_list("ALL",@values) );
167 if ( $opt_p !~ m/:/ ) {
168 print "Error: Mozilla trust identifier list must include both purposes and levels\n";
172 (my $included_mozilla_trust_purposes_string, my $included_mozilla_trust_levels_string) = split( ':', $opt_p );
173 my @included_mozilla_trust_purposes = parse_csv_param( "trust purpose", $included_mozilla_trust_purposes_string, @valid_mozilla_trust_purposes );
174 my @included_mozilla_trust_levels = parse_csv_param( "trust level", $included_mozilla_trust_levels_string, @valid_mozilla_trust_levels );
176 my @included_signature_algorithms = parse_csv_param( "signature algorithm", $opt_s, @valid_signature_algorithms );
178 sub should_output_cert(%) {
179 my %trust_purposes_by_level = @_;
181 foreach my $level (@included_mozilla_trust_levels) {
182 # for each level we want to output, see if any of our desired purposes are included
183 return 1 if ( defined( List::Util::first { is_in_list( $_, @included_mozilla_trust_purposes ) } @{$trust_purposes_by_level{$level}} ) );
189 my $crt = $ARGV[0] || dirname(__FILE__) . '/../src/node_root_certs.h';
190 my $txt = dirname(__FILE__) . '/certdata.txt';
192 my $stdout = $crt eq '-';
195 open(CRT, '> -') or die "Couldn't open STDOUT: $!\n";
197 open(CRT,">$crt.~") or die "Couldn't open $crt.~: $!\n";
203 my $start_of_cert = 0;
205 open(TXT,"$txt") or die "Couldn't open $txt: $!\n";
207 if (/\*\*\*\*\* BEGIN LICENSE BLOCK \*\*\*\*\*/) {
213 last if (/\*\*\*\*\* END LICENSE BLOCK \*\*\*\*\*/);
218 if (/^CVS_ID\s+\"(.*)\"/) {
219 print CRT "/* $1 */\n";
222 # this is a match for the start of a certificate
223 if (/^CKA_CLASS CK_OBJECT_CLASS CKO_CERTIFICATE/) {
226 if ($start_of_cert && /^CKA_LABEL UTF8 \"(.*)\"/) {
229 my %trust_purposes_by_level;
230 if ($start_of_cert && /^CKA_VALUE MULTILINE_OCTAL/) {
235 my @octets = split(/\\/);
241 # scan forwards until the trust part
243 last if (/^CKA_CLASS CK_OBJECT_CLASS CKO_NSS_TRUST/);
246 # now scan the trust part to determine how we should trust this cert
249 if (/^CKA_TRUST_([A-Z_]+)\s+CK_TRUST\s+CKT_NSS_([A-Z_]+)\s*$/) {
250 if ( !is_in_list($1,@valid_mozilla_trust_purposes) ) {
251 report "Warning: Unrecognized trust purpose for cert: $caname. Trust purpose: $1. Trust Level: $2";
252 } elsif ( !is_in_list($2,@valid_mozilla_trust_levels) ) {
253 report "Warning: Unrecognized trust level for cert: $caname. Trust purpose: $1. Trust Level: $2";
255 push @{$trust_purposes_by_level{$2}}, $1;
260 if ( !should_output_cert(%trust_purposes_by_level) ) {
263 my $encoded = MIME::Base64::encode_base64($data, '');
264 $encoded =~ s/(.{1,${opt_w}})/"$1\\n"\n/g;
265 my $pem = "\"-----BEGIN CERTIFICATE-----\\n\"\n"
267 . "\"-----END CERTIFICATE-----\\n\",\n";
268 print CRT "\n/* $caname */\n";
270 my $maxStringLength = length($caname);
272 foreach my $key (keys %trust_purposes_by_level) {
273 my $string = $key . ": " . join(", ", @{$trust_purposes_by_level{$key}});
274 $maxStringLength = List::Util::max( length($string), $maxStringLength );
275 print CRT $string . "\n";
282 foreach my $hash (@included_signature_algorithms) {
283 $pipe = "|$openssl x509 -" . $hash . " -fingerprint -noout -inform PEM";
285 $pipe .= " >> $crt.~";
286 close(CRT) or die "Couldn't close $crt.~: $!";
288 open(TMP, $pipe) or die "Couldn't open openssl pipe: $!";
290 close(TMP) or die "Couldn't close openssl pipe: $!";
292 open(CRT, ">>$crt.~") or die "Couldn't open $crt.~: $!";
295 $pipe = "|$openssl x509 -text -inform PEM";
297 $pipe .= " >> $crt.~";
298 close(CRT) or die "Couldn't close $crt.~: $!";
300 open(TMP, $pipe) or die "Couldn't open openssl pipe: $!";
302 close(TMP) or die "Couldn't close openssl pipe: $!";
304 open(CRT, ">>$crt.~") or die "Couldn't open $crt.~: $!";
307 report "Parsing: $caname" if ($opt_v);
313 close(TXT) or die "Couldn't close $txt: $!\n";
314 close(CRT) or die "Couldn't close $crt.~: $!\n";
316 rename "$crt.~", $crt or die "Failed to rename $crt.~ to $crt: $!\n";
318 report "Done ($certnum CA certs processed, $skipnum skipped).";