src: enable v8 deprecation warnings and fix them
[platform/upstream/nodejs.git] / tools / mk-ca-bundle.pl
1 #!/usr/bin/perl -w
2 # ***************************************************************************
3 # *                                  _   _ ____  _
4 # *  Project                     ___| | | |  _ \| |
5 # *                             / __| | | | |_) | |
6 # *                            | (__| |_| |  _ <| |___
7 # *                             \___|\___/|_| \_\_____|
8 # *
9 # * Copyright (C) 1998 - 2014, 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 File::Basename 'dirname';
34 use Getopt::Std;
35 use MIME::Base64;
36 use strict;
37 use vars qw($opt_h $opt_i $opt_l $opt_p $opt_q $opt_s $opt_t $opt_v $opt_w);
38 use List::Util;
39 use Text::Wrap;
40
41 # If the OpenSSL commandline is not in search path you can configure it here!
42 my $openssl = 'openssl';
43
44 my $version = '1.25';
45
46 $opt_w = 72; # default base64 encoded lines length
47
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;
52
53 my @valid_mozilla_trust_purposes = (
54   "DIGITAL_SIGNATURE",
55   "NON_REPUDIATION",
56   "KEY_ENCIPHERMENT",
57   "DATA_ENCIPHERMENT",
58   "KEY_AGREEMENT",
59   "KEY_CERT_SIGN",
60   "CRL_SIGN",
61   "SERVER_AUTH",
62   "CLIENT_AUTH",
63   "CODE_SIGNING",
64   "EMAIL_PROTECTION",
65   "IPSEC_END_SYSTEM",
66   "IPSEC_TUNNEL",
67   "IPSEC_USER",
68   "TIME_STAMPING",
69   "STEP_UP_APPROVED"
70 );
71
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).
77 );
78
79 my $default_signature_algorithms = $opt_s = "MD5";
80
81 my @valid_signature_algorithms = (
82   "MD5",
83   "SHA1",
84   "SHA256",
85   "SHA384",
86   "SHA512"
87 );
88
89 $0 =~ s@.*(/|\\)@@;
90 $Getopt::Std::STANDARD_HELP_VERSION = 1;
91 getopts('bd:fhilnp:qs:tuvw:');
92
93 if ($opt_i) {
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");
101 }
102
103 sub HELP_MESSAGE() {
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";
119   exit;
120 }
121
122 sub VERSION_MESSAGE() {
123   print "${0} version ${version} running Perl ${]} on ${^O}\n";
124 }
125
126 HELP_MESSAGE() if ($opt_h);
127
128 sub report($@) {
129   my $output = shift;
130
131   print STDERR $output . "\n" unless $opt_q;
132 }
133
134 sub is_in_list($@) {
135   my $target = shift;
136
137   return defined(List::Util::first { $target eq $_ } @_);
138 }
139
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 = @_;
146
147   my @values = map {
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 );
152
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;
155
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";
159     HELP_MESSAGE();
160   }
161
162   @values = @valid_values if ( is_in_list("ALL",@values) );
163
164   return @values;
165 }
166
167 if ( $opt_p !~ m/:/ ) {
168   print "Error: Mozilla trust identifier list must include both purposes and levels\n";
169   HELP_MESSAGE();
170 }
171
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 );
175
176 my @included_signature_algorithms = parse_csv_param( "signature algorithm", $opt_s, @valid_signature_algorithms );
177
178 sub should_output_cert(%) {
179   my %trust_purposes_by_level = @_;
180
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}} ) );
184   }
185
186   return 0;
187 }
188
189 my $crt = $ARGV[0] || dirname(__FILE__) . '/../src/node_root_certs.h';
190 my $txt = dirname(__FILE__) . '/certdata.txt';
191
192 my $stdout = $crt eq '-';
193
194 if( $stdout ) {
195     open(CRT, '> -') or die "Couldn't open STDOUT: $!\n";
196 } else {
197     open(CRT,">$crt.~") or die "Couldn't open $crt.~: $!\n";
198 }
199
200 my $caname;
201 my $certnum = 0;
202 my $skipnum = 0;
203 my $start_of_cert = 0;
204
205 open(TXT,"$txt") or die "Couldn't open $txt: $!\n";
206 while (<TXT>) {
207   if (/\*\*\*\*\* BEGIN LICENSE BLOCK \*\*\*\*\*/) {
208     print CRT;
209     print if ($opt_l);
210     while (<TXT>) {
211       print CRT;
212       print if ($opt_l);
213       last if (/\*\*\*\*\* END LICENSE BLOCK \*\*\*\*\*/);
214     }
215   }
216   next if /^#|^\s*$/;
217   chomp;
218   if (/^CVS_ID\s+\"(.*)\"/) {
219     print CRT "/* $1 */\n";
220   }
221
222   # this is a match for the start of a certificate
223   if (/^CKA_CLASS CK_OBJECT_CLASS CKO_CERTIFICATE/) {
224     $start_of_cert = 1
225   }
226   if ($start_of_cert && /^CKA_LABEL UTF8 \"(.*)\"/) {
227     $caname = $1;
228   }
229   my %trust_purposes_by_level;
230   if ($start_of_cert && /^CKA_VALUE MULTILINE_OCTAL/) {
231     my $data;
232     while (<TXT>) {
233       last if (/^END/);
234       chomp;
235       my @octets = split(/\\/);
236       shift @octets;
237       for (@octets) {
238         $data .= chr(oct);
239       }
240     }
241     # scan forwards until the trust part
242     while (<TXT>) {
243       last if (/^CKA_CLASS CK_OBJECT_CLASS CKO_NSS_TRUST/);
244       chomp;
245     }
246     # now scan the trust part to determine how we should trust this cert
247     while (<TXT>) {
248       last if (/^#/);
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";
254         } else {
255           push @{$trust_purposes_by_level{$2}}, $1;
256         }
257       }
258     }
259
260     if ( !should_output_cert(%trust_purposes_by_level) ) {
261       $skipnum ++;
262     } else {
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"
266               . $encoded
267               . "\"-----END CERTIFICATE-----\\n\",\n";
268       print CRT "\n/* $caname */\n";
269
270       my $maxStringLength = length($caname);
271       if ($opt_t) {
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";
276         }
277       }
278       if (!$opt_t) {
279         print CRT $pem;
280       } else {
281         my $pipe = "";
282         foreach my $hash (@included_signature_algorithms) {
283           $pipe = "|$openssl x509 -" . $hash . " -fingerprint -noout -inform PEM";
284           if (!$stdout) {
285             $pipe .= " >> $crt.~";
286             close(CRT) or die "Couldn't close $crt.~: $!";
287           }
288           open(TMP, $pipe) or die "Couldn't open openssl pipe: $!";
289           print TMP $pem;
290           close(TMP) or die "Couldn't close openssl pipe: $!";
291           if (!$stdout) {
292             open(CRT, ">>$crt.~") or die "Couldn't open $crt.~: $!";
293           }
294         }
295         $pipe = "|$openssl x509 -text -inform PEM";
296         if (!$stdout) {
297           $pipe .= " >> $crt.~";
298           close(CRT) or die "Couldn't close $crt.~: $!";
299         }
300         open(TMP, $pipe) or die "Couldn't open openssl pipe: $!";
301         print TMP $pem;
302         close(TMP) or die "Couldn't close openssl pipe: $!";
303         if (!$stdout) {
304           open(CRT, ">>$crt.~") or die "Couldn't open $crt.~: $!";
305         }
306       }
307       report "Parsing: $caname" if ($opt_v);
308       $certnum ++;
309       $start_of_cert = 0;
310     }
311   }
312 }
313 close(TXT) or die "Couldn't close $txt: $!\n";
314 close(CRT) or die "Couldn't close $crt.~: $!\n";
315 unless( $stdout ) {
316     rename "$crt.~", $crt or die "Failed to rename $crt.~ to $crt: $!\n";
317 }
318 report "Done ($certnum CA certs processed, $skipnum skipped).";