Resync
[CMakeLuaTailorHgBridge.git] / CMakeLua / Utilities / cmcurl-7.19.0 / lib / mk-ca-bundle.pl
blob2ee7a043a0fa0c5553e02d543111c4fe8d7d9b8a
1 #!/usr/bin/perl -w
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: mk-ca-bundle.pl,v 1.1.1.1 2008-09-23 16:32:05 hoffman Exp $
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 written by Roland Krikava.
30 # This Perl script works on almost any platform since its only external
31 # dependency is the OpenSSL commandline tool for optional text listing.
32 # Hacked by Guenter Knauf.
34 use Getopt::Std;
35 use MIME::Base64;
36 use LWP::UserAgent;
37 use strict;
38 use vars qw($opt_b $opt_h $opt_i $opt_l $opt_n $opt_q $opt_t $opt_u $opt_v);
40 my $url = 'http://mxr.mozilla.org/seamonkey/source/security/nss/lib/ckfw/builtins/certdata.txt?raw=1';
41 # If the OpenSSL commandline is not in search path you can configure it here!
42 my $openssl = 'openssl';
44 my $version = $1 if ('$Revision: 1.1.1.1 $' =~ /\s(\d+\.\d+)\s/);
46 getopts('bhilnqtuv');
48 if ($opt_i) {
49 print ("=" x 78 . "\n");
50 print "Script Version : $version\n";
51 print "Perl Version : $]\n";
52 print "Operating System Name : $^O\n";
53 print "Getopt::Std.pm Version : ${Getopt::Std::VERSION}\n";
54 print "MIME::Base64.pm Version : ${MIME::Base64::VERSION}\n";
55 print "LWP::UserAgent.pm Version : ${LWP::UserAgent::VERSION}\n";
56 print "LWP.pm Version : ${LWP::VERSION}\n";
57 print ("=" x 78 . "\n");
60 $0 =~ s/\\/\//g;
61 $0 = substr($0, rindex($0, '/') + 1);
62 if ($opt_h) {
63 printf("Usage:\t%s [-b] [-i] [-l] [-n] [-q] [-t] [-u] [-v] [<outputfile>]\n", $0);
64 print "\t-b\tbackup an existing version of ca-bundle.crt\n";
65 print "\t-i\tprint version info about used modules\n";
66 print "\t-l\tprint license info about certdata.txt\n";
67 print "\t-n\tno download of certdata.txt (to use existing)\n";
68 print "\t-q\tbe really quiet (no progress output at all)\n";
69 print "\t-t\tinclude plain text listing of certificates\n";
70 print "\t-u\tunlink (remove) certdata.txt after processing\n";
71 print "\t-v\tbe verbose and print out processed CAs\n";
72 exit;
75 my $crt = $ARGV[0] || 'ca-bundle.crt';
76 my $txt = substr($url, rindex($url, '/') + 1);
77 $txt =~ s/\?.*//;
79 if (!$opt_n || !-e $txt) {
80 print "Downloading '$txt' ...\n" if (!$opt_q);
81 my $ua = new LWP::UserAgent(agent => "$0/$version");
82 my $req = new HTTP::Request('GET', $url);
83 my $res = $ua->request($req);
84 if ($res->is_success) {
85 open(TXT,">$txt") or die "Couldn't open $txt: $!";
86 print TXT $res->content . "\n";
87 close(TXT) or die "Couldn't close $txt: $!";
88 } else {
89 die $res->status_line;
93 if ($opt_b && -e $crt) {
94 my $bk = 1;
95 while (-e "$crt.~${bk}~") {
96 $bk++;
98 rename $crt, "$crt.~${bk}~";
101 my $format = $opt_t ? "plain text and " : "";
102 my $currentdate = scalar gmtime() . " UTC";
103 open(CRT,">$crt") or die "Couldn't open $crt: $!";
104 print CRT <<EOT;
106 ## $crt -- Bundle of CA Root Certificates
108 ## Converted at: ${currentdate}
110 ## This is a bundle of X.509 certificates of public Certificate Authorities
111 ## (CA). These were automatically extracted from Mozilla's root certificates
112 ## file (certdata.txt). This file can be found in the mozilla source tree:
113 ## '/mozilla/security/nss/lib/ckfw/builtins/certdata.txt'
115 ## It contains the certificates in ${format}PEM format and therefore
116 ## can be directly used with curl / libcurl / php_curl, or with
117 ## an Apache+mod_ssl webserver for SSL client authentication.
118 ## Just configure this file as the SSLCACertificateFile.
123 close(CRT) or die "Couldn't close $crt: $!";
125 print "Processing '$txt' ...\n" if (!$opt_q);
126 my $caname;
127 my $certnum = 0;
128 open(TXT,"$txt") or die "Couldn't open $txt: $!";
129 while (<TXT>) {
130 if (/\*\*\*\*\* BEGIN LICENSE BLOCK \*\*\*\*\*/) {
131 open(CRT, ">>$crt") or die "Couldn't open $crt: $!";
132 print CRT;
133 print if ($opt_l);
134 while (<TXT>) {
135 print CRT;
136 print if ($opt_l);
137 last if (/\*\*\*\*\* END LICENSE BLOCK \*\*\*\*\*/);
139 close(CRT) or die "Couldn't close $crt: $!";
141 next if /^#|^\s*$/;
142 chomp;
143 if (/^CVS_ID\s+\"(.*)\"/) {
144 open(CRT, ">>$crt") or die "Couldn't open $crt: $!";
145 print CRT "# $1\n";
146 close(CRT) or die "Couldn't close $crt: $!";
148 if (/^CKA_LABEL\s+[A-Z0-9]+\s+\"(.*)\"/) {
149 $caname = $1;
151 if (/^CKA_VALUE MULTILINE_OCTAL/) {
152 my $data;
153 while (<TXT>) {
154 last if (/^END/);
155 chomp;
156 my @octets = split(/\\/);
157 shift @octets;
158 for (@octets) {
159 $data .= chr(oct);
162 my $pem = "-----BEGIN CERTIFICATE-----\n"
163 . MIME::Base64::encode($data)
164 . "-----END CERTIFICATE-----\n";
165 open(CRT, ">>$crt") or die "Couldn't open $crt: $!";
166 print CRT "\n$caname\n";
167 print CRT ("=" x length($caname) . "\n");
168 if (!$opt_t) {
169 print CRT $pem;
171 close(CRT) or die "Couldn't close $crt: $!";
172 if ($opt_t) {
173 open(TMP, "|$openssl x509 -md5 -fingerprint -text -inform PEM >> $crt") or die "Couldn't open openssl pipe: $!";
174 print TMP $pem;
175 close(TMP) or die "Couldn't close openssl pipe: $!";
177 print "Parsing: $caname\n" if ($opt_v);
178 $certnum ++;
181 close(TXT) or die "Couldn't close $txt: $!";
182 unlink $txt if ($opt_u);
183 print "Done ($certnum CA certs processed).\n" if (!$opt_q);
185 exit;