deps: update gyp to 828ce09
[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 - 2013, 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_q $opt_t $opt_v $opt_w);
38
39 my $url = 'http://mxr.mozilla.org/mozilla/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
43 my $version = '1.19';
44
45 $opt_w = 72; # default base64 encoded lines length
46
47 $0 =~ s@.*(/|\\)@@;
48 $Getopt::Std::STANDARD_HELP_VERSION = 1;
49 getopts('bfhilnqtuvw:');
50
51 if ($opt_i) {
52   print ("=" x 78 . "\n");
53   print "Script Version            : $version\n";
54   print "Perl Version              : $]\n";
55   print "Operating System Name     : $^O\n";
56   print "Getopt::Std.pm Version    : ${Getopt::Std::VERSION}\n";
57   print "MIME::Base64.pm Version   : ${MIME::Base64::VERSION}\n";
58   print ("=" x 78 . "\n");
59 }
60
61 sub HELP_MESSAGE() {
62   print "Usage:\t${0} [-i] [-l] [-q] [-t] [-v] [-w<l>] [<outputfile>]\n";
63   print "\t-i\tprint version info about used modules\n";
64   print "\t-l\tprint license info about certdata.txt\n";
65   print "\t-q\tbe really quiet (no progress output at all)\n";
66   print "\t-t\tinclude plain text listing of certificates\n";
67   print "\t-v\tbe verbose and print out processed CAs\n";
68   print "\t-w <l>\twrap base64 output lines after <l> chars (default: ${opt_w})\n";
69   exit;
70 }
71
72 sub VERSION_MESSAGE() {
73   print "${0} version ${version} running Perl ${]} on ${^O}\n";
74 }
75
76 HELP_MESSAGE() if ($opt_h);
77
78 my $crt = $ARGV[0] || dirname(__FILE__) . '/../src/node_root_certs.h';
79 my $txt = dirname(__FILE__) . '/certdata.txt';
80
81 my $stdout = $crt eq '-';
82 my $resp;
83 my $fetched;
84
85 my $currentdate = scalar gmtime($fetched ? $resp->last_modified : (stat($txt))[9]);
86
87 my $format = $opt_t ? "plain text and " : "";
88 if( $stdout ) {
89     open(CRT, '> -') or die "Couldn't open STDOUT: $!\n";
90 } else {
91     open(CRT,">$crt") or die "Couldn't open $crt: $!\n";
92 }
93 print CRT <<EOT;
94 /* $crt -- Bundle of CA Root Certificates
95  *
96  * Certificate data from Mozilla as of: ${currentdate}
97  *
98  * This is a bundle of X.509 certificates of public Certificate Authorities
99  * (CA). These were automatically extracted from Mozilla's root certificates
100  * file (certdata.txt).  This file can be found in the mozilla source tree:
101  * ${url}
102  *
103  * It contains the certificates in ${format}PEM format and therefore
104  * can be directly used with curl / libcurl / php_curl, or with
105  * an Apache+mod_ssl webserver for SSL client authentication.
106  * Just configure this file as the SSLCACertificateFile.
107  */
108
109 EOT
110
111 print STDERR "Processing  '$txt' ...\n" if (!$opt_q);
112 my $caname;
113 my $certnum = 0;
114 my $skipnum = 0;
115 my $start_of_cert = 0;
116
117 open(TXT,"$txt") or die "Couldn't open $txt: $!\n";
118 while (<TXT>) {
119   if (/\*\*\*\*\* BEGIN LICENSE BLOCK \*\*\*\*\*/) {
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   }
128   next if /^#|^\s*$/;
129   chomp;
130   if (/^CVS_ID\s+\"(.*)\"/) {
131     print CRT "/* $1 */\n";
132   }
133
134   # this is a match for the start of a certificate
135   if (/^CKA_CLASS CK_OBJECT_CLASS CKO_CERTIFICATE/) {
136     $start_of_cert = 1
137   }
138   if ($start_of_cert && /^CKA_LABEL UTF8 \"(.*)\"/) {
139     $caname = $1;
140   }
141   my $untrusted = 1;
142   if ($start_of_cert && /^CKA_VALUE MULTILINE_OCTAL/) {
143     my $data;
144     while (<TXT>) {
145       last if (/^END/);
146       chomp;
147       my @octets = split(/\\/);
148       shift @octets;
149       for (@octets) {
150         $data .= chr(oct);
151       }
152     }
153     # scan forwards until the trust part
154     while (<TXT>) {
155       last if (/^CKA_CLASS CK_OBJECT_CLASS CKO_NSS_TRUST/);
156       chomp;
157     }
158     # now scan the trust part for untrusted certs
159     while (<TXT>) {
160       last if (/^#/);
161       if (/^CKA_TRUST_SERVER_AUTH\s+CK_TRUST\s+CKT_NSS_TRUSTED_DELEGATOR$/) {
162           $untrusted = 0;
163       }
164     }
165     if ($untrusted) {
166       $skipnum ++;
167     } else {
168       my $encoded = MIME::Base64::encode_base64($data, '');
169       $encoded =~ s/(.{1,${opt_w}})/"$1\\n"\n/g;
170       my $pem = "\"-----BEGIN CERTIFICATE-----\\n\"\n"
171               . $encoded
172               . "\"-----END CERTIFICATE-----\\n\",\n";
173       print CRT "\n/* $caname */\n";
174       if (!$opt_t) {
175         print CRT $pem;
176       } else {
177         my $pipe = "|$openssl x509 -md5 -fingerprint -text -inform PEM";
178         if (!$stdout) {
179           $pipe .= " >> $crt";
180           close(CRT) or die "Couldn't close $crt: $!";
181         }
182         open(TMP, $pipe) or die "Couldn't open openssl pipe: $!";
183         print TMP $pem;
184         close(TMP) or die "Couldn't close openssl pipe: $!";
185         if (!$stdout) {
186           open(CRT, ">>$crt") or die "Couldn't open $crt: $!";
187         }
188       }
189       print STDERR "Parsing: $caname\n" if ($opt_v);
190       $certnum ++;
191       $start_of_cert = 0;
192     }
193   }
194 }
195 close(TXT) or die "Couldn't close $txt: $!\n";
196 close(CRT) or die "Couldn't close $crt: $!\n";
197 print STDERR "Done ($certnum CA certs processed, $skipnum untrusted skipped).\n" if (!$opt_q);
198
199 exit;
200
201