fixed regex to fetch certdata.txt version since it was replaced by CVS (argh!)
[platform/upstream/curl.git] / lib / mk-ca-bundle.pl
1 #!/usr/bin/perl
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 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.
33 #
34 use Getopt::Std;
35 use MIME::Base64;
36 use LWP::UserAgent;
37
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';
43
44 getopts('hilnuv');
45
46 if ($opt_h) {
47   $0 =~ s/\\/\//g;
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";
54   exit;
55 }
56
57 if ($opt_i) {
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");
63 }
64
65 my $txt = substr($url, rindex($url, '/') + 1);
66 $txt =~ s/\?.*//;
67 if (!$opt_n) {
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: $!";
76   } else {
77     die $res->status_line;
78   }
79 }
80
81 die "File '$txt' doesnt exist - dont use -n switch to download the file!\n" if (!-e $txt);
82
83 my $currentdate = scalar gmtime() . " UTC";
84 open(CRT,">$crt") or die "Couldn't open $crt: $!";
85 print CRT <<EOT;
86 ##
87 ## $crt -- Bundle of CA Root Certificates
88 ##
89 ## Converted at: $currentdate
90 ##
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'
95 ##
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.
100 ##
101
102 EOT
103
104 close(CRT) or die "Couldn't close $crt: $!";
105
106 my $certnum;
107 open(TXT,"$txt") or die "Couldn't open $file: $!";
108 while (<TXT>) {
109   if (/\*\*\*\*\* BEGIN LICENSE BLOCK \*\*\*\*\*/) {
110     open(CRT, ">>$crt") or die "Couldn't open $crt: $!";
111     print CRT;
112     print if ($opt_l);
113     while (<TXT>) {
114       print CRT;
115       print if ($opt_l);
116       last if (/\*\*\*\*\* END LICENSE BLOCK \*\*\*\*\*/);
117     }
118     close(CRT) or die "Couldn't close $crt: $!";
119   }
120   next if /^#/;
121   next if /^\s*$/;
122   chomp;
123   if (/^CVS_ID\s+\"(.*)\"/) {
124     open(CRT, ">>$crt") or die "Couldn't open $crt: $!";
125     print CRT "# $1\n";
126     close(CRT) or die "Couldn't close $crt: $!";
127   }
128   if (/^CKA_LABEL\s+[A-Z0-9]+\s+\"(.*)\"/) {
129     $val = $1;
130   }
131   if (/^CKA_VALUE MULTILINE_OCTAL/) {
132     my $data;
133     while (<TXT>) {
134       last if (/^END/);
135       chomp;
136       my @octets = split(/\\/);
137       shift @octets;
138       for (@octets) {
139         $data .= chr(oct);
140       }
141     }
142     open(CRT, ">>$crt") or die "Couldn't open $crt: $!";
143     print CRT "\n";
144     print CRT "$val\n";
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: $!";
148     print TMP "$val\n";
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);
155     $certnum ++;
156   }
157 }
158 close(TXT) or die "Couldn't close $txt: $!";
159 unlink $txt if ($opt_u);
160 unlink $tmp;
161 print "Done ($certnum CA certs processed).\n";
162
163 exit;
164
165