2008-02-11 13:52:45 -05:00
|
|
|
#!/usr/bin/perl -w
|
2008-02-07 20:08:25 -05:00
|
|
|
# ***************************************************************************
|
|
|
|
# * _ _ ____ _
|
|
|
|
# * Project ___| | | | _ \| |
|
|
|
|
# * / __| | | | |_) | |
|
|
|
|
# * | (__| |_| | _ <| |___
|
|
|
|
# * \___|\___/|_| \_\_____|
|
|
|
|
# *
|
2011-02-02 16:22:15 -05:00
|
|
|
# * Copyright (C) 1998 - 2011, Daniel Stenberg, <daniel@haxx.se>, et al.
|
2008-02-07 20:08:25 -05:00
|
|
|
# *
|
|
|
|
# * This software is licensed as described in the file COPYING, which
|
|
|
|
# * you should have received as part of this distribution. The terms
|
|
|
|
# * are also available at http://curl.haxx.se/docs/copyright.html.
|
|
|
|
# *
|
|
|
|
# * You may opt to use, copy, modify, merge, publish, distribute and/or sell
|
|
|
|
# * copies of the Software, and permit persons to whom the Software is
|
|
|
|
# * furnished to do so, under the terms of the COPYING file.
|
|
|
|
# *
|
|
|
|
# * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
|
|
|
|
# * KIND, either express or implied.
|
|
|
|
# *
|
|
|
|
# ***************************************************************************
|
2010-02-14 14:40:18 -05:00
|
|
|
# This Perl script creates a fresh ca-bundle.crt file for use with libcurl.
|
2008-02-07 20:08:25 -05:00
|
|
|
# It downloads certdata.txt from Mozilla's source tree (see URL below),
|
|
|
|
# then parses certdata.txt and extracts CA Root Certificates into PEM format.
|
|
|
|
# These are then processed with the OpenSSL commandline tool to produce the
|
|
|
|
# final ca-bundle.crt file.
|
2008-02-11 10:00:00 -05:00
|
|
|
# The script is based on the parse-certs script written by Roland Krikava.
|
2008-02-07 20:08:25 -05:00
|
|
|
# This Perl script works on almost any platform since its only external
|
2008-02-11 10:00:00 -05:00
|
|
|
# dependency is the OpenSSL commandline tool for optional text listing.
|
2008-02-07 20:08:25 -05:00
|
|
|
# Hacked by Guenter Knauf.
|
|
|
|
#
|
|
|
|
use Getopt::Std;
|
|
|
|
use MIME::Base64;
|
|
|
|
use LWP::UserAgent;
|
2008-02-11 10:00:00 -05:00
|
|
|
use strict;
|
2010-02-14 14:40:18 -05:00
|
|
|
use vars qw($opt_b $opt_h $opt_i $opt_l $opt_n $opt_q $opt_t $opt_u $opt_v);
|
2008-02-07 20:08:25 -05:00
|
|
|
|
2011-02-02 16:22:15 -05:00
|
|
|
my $url = 'http://mxr.mozilla.org/mozilla/source/security/nss/lib/ckfw/builtins/certdata.txt?raw=1';
|
2008-02-07 20:08:25 -05:00
|
|
|
# If the OpenSSL commandline is not in search path you can configure it here!
|
|
|
|
my $openssl = 'openssl';
|
|
|
|
|
2011-04-01 09:04:53 -04:00
|
|
|
my $version = '1.15';
|
2008-02-14 19:26:26 -05:00
|
|
|
|
2008-02-11 10:00:00 -05:00
|
|
|
getopts('bhilnqtuv');
|
2008-02-07 20:08:25 -05:00
|
|
|
|
2008-02-14 19:26:26 -05:00
|
|
|
if ($opt_i) {
|
|
|
|
print ("=" x 78 . "\n");
|
|
|
|
print "Script Version : $version\n";
|
|
|
|
print "Perl Version : $]\n";
|
|
|
|
print "Operating System Name : $^O\n";
|
|
|
|
print "Getopt::Std.pm Version : ${Getopt::Std::VERSION}\n";
|
|
|
|
print "MIME::Base64.pm Version : ${MIME::Base64::VERSION}\n";
|
|
|
|
print "LWP::UserAgent.pm Version : ${LWP::UserAgent::VERSION}\n";
|
|
|
|
print "LWP.pm Version : ${LWP::VERSION}\n";
|
|
|
|
print ("=" x 78 . "\n");
|
|
|
|
}
|
|
|
|
|
2011-04-13 19:27:39 -04:00
|
|
|
$0 =~ s@.*(/|\\)@@;
|
2008-02-07 20:08:25 -05:00
|
|
|
if ($opt_h) {
|
2008-02-14 19:26:26 -05:00
|
|
|
printf("Usage:\t%s [-b] [-i] [-l] [-n] [-q] [-t] [-u] [-v] [<outputfile>]\n", $0);
|
2008-02-09 20:29:24 -05:00
|
|
|
print "\t-b\tbackup an existing version of ca-bundle.crt\n";
|
2008-02-07 20:08:25 -05:00
|
|
|
print "\t-i\tprint version info about used modules\n";
|
2008-02-07 20:58:11 -05:00
|
|
|
print "\t-l\tprint license info about certdata.txt\n";
|
2008-02-07 20:08:25 -05:00
|
|
|
print "\t-n\tno download of certdata.txt (to use existing)\n";
|
2008-02-11 10:00:00 -05:00
|
|
|
print "\t-q\tbe really quiet (no progress output at all)\n";
|
|
|
|
print "\t-t\tinclude plain text listing of certificates\n";
|
2008-02-07 20:08:25 -05:00
|
|
|
print "\t-u\tunlink (remove) certdata.txt after processing\n";
|
|
|
|
print "\t-v\tbe verbose and print out processed CAs\n";
|
|
|
|
exit;
|
|
|
|
}
|
|
|
|
|
2008-02-07 21:38:12 -05:00
|
|
|
my $crt = $ARGV[0] || 'ca-bundle.crt';
|
2011-04-13 19:27:39 -04:00
|
|
|
(my $txt = $url) =~ s@(.*/|\?.*)@@g;
|
2008-02-11 10:00:00 -05:00
|
|
|
|
2011-03-14 01:52:33 -04:00
|
|
|
my $resp;
|
|
|
|
|
|
|
|
unless ($opt_n and -e $txt) {
|
2008-02-11 10:00:00 -05:00
|
|
|
print "Downloading '$txt' ...\n" if (!$opt_q);
|
2011-03-14 01:52:33 -04:00
|
|
|
|
2008-02-14 19:26:26 -05:00
|
|
|
my $ua = new LWP::UserAgent(agent => "$0/$version");
|
2011-04-01 08:58:36 -04:00
|
|
|
$ua->env_proxy();
|
2011-04-01 08:38:01 -04:00
|
|
|
$resp = $ua->mirror($url, $txt);
|
2008-02-07 20:08:25 -05:00
|
|
|
}
|
|
|
|
|
2011-03-14 01:52:33 -04:00
|
|
|
if ($resp && $resp->code eq '304') {
|
|
|
|
print "Not modified\n" unless $opt_q;
|
|
|
|
exit 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
my $currentdate = scalar gmtime($resp ? $resp->last_modified : (stat($txt))[9]);
|
|
|
|
|
2008-02-09 20:29:24 -05:00
|
|
|
if ($opt_b && -e $crt) {
|
|
|
|
my $bk = 1;
|
2008-02-11 10:00:00 -05:00
|
|
|
while (-e "$crt.~${bk}~") {
|
2008-02-09 20:29:24 -05:00
|
|
|
$bk++;
|
|
|
|
}
|
2008-02-11 10:00:00 -05:00
|
|
|
rename $crt, "$crt.~${bk}~";
|
2008-02-09 20:29:24 -05:00
|
|
|
}
|
|
|
|
|
2008-02-11 10:00:00 -05:00
|
|
|
my $format = $opt_t ? "plain text and " : "";
|
2008-02-07 20:08:25 -05:00
|
|
|
open(CRT,">$crt") or die "Couldn't open $crt: $!";
|
|
|
|
print CRT <<EOT;
|
|
|
|
##
|
|
|
|
## $crt -- Bundle of CA Root Certificates
|
|
|
|
##
|
2011-03-14 01:52:33 -04:00
|
|
|
## Certificate data from Mozilla as of: ${currentdate}
|
2008-02-07 20:08:25 -05:00
|
|
|
##
|
|
|
|
## This is a bundle of X.509 certificates of public Certificate Authorities
|
|
|
|
## (CA). These were automatically extracted from Mozilla's root certificates
|
|
|
|
## file (certdata.txt). This file can be found in the mozilla source tree:
|
2011-04-07 16:42:22 -04:00
|
|
|
## $url
|
2008-02-07 20:08:25 -05:00
|
|
|
##
|
2008-02-11 10:00:00 -05:00
|
|
|
## It contains the certificates in ${format}PEM format and therefore
|
2008-02-14 19:26:26 -05:00
|
|
|
## can be directly used with curl / libcurl / php_curl, or with
|
|
|
|
## an Apache+mod_ssl webserver for SSL client authentication.
|
2008-02-07 20:08:25 -05:00
|
|
|
## Just configure this file as the SSLCACertificateFile.
|
|
|
|
##
|
|
|
|
|
|
|
|
EOT
|
|
|
|
|
|
|
|
close(CRT) or die "Couldn't close $crt: $!";
|
|
|
|
|
2008-02-11 10:00:00 -05:00
|
|
|
print "Processing '$txt' ...\n" if (!$opt_q);
|
|
|
|
my $caname;
|
|
|
|
my $certnum = 0;
|
2008-02-09 10:32:54 -05:00
|
|
|
open(TXT,"$txt") or die "Couldn't open $txt: $!";
|
2008-02-07 20:08:25 -05:00
|
|
|
while (<TXT>) {
|
|
|
|
if (/\*\*\*\*\* BEGIN LICENSE BLOCK \*\*\*\*\*/) {
|
|
|
|
open(CRT, ">>$crt") or die "Couldn't open $crt: $!";
|
|
|
|
print CRT;
|
2008-02-07 20:58:11 -05:00
|
|
|
print if ($opt_l);
|
2008-02-07 20:08:25 -05:00
|
|
|
while (<TXT>) {
|
|
|
|
print CRT;
|
2008-02-07 20:58:11 -05:00
|
|
|
print if ($opt_l);
|
2008-02-07 20:08:25 -05:00
|
|
|
last if (/\*\*\*\*\* END LICENSE BLOCK \*\*\*\*\*/);
|
|
|
|
}
|
|
|
|
close(CRT) or die "Couldn't close $crt: $!";
|
|
|
|
}
|
2008-02-11 10:00:00 -05:00
|
|
|
next if /^#|^\s*$/;
|
2008-02-07 20:08:25 -05:00
|
|
|
chomp;
|
2008-02-07 20:58:11 -05:00
|
|
|
if (/^CVS_ID\s+\"(.*)\"/) {
|
2008-02-07 20:08:25 -05:00
|
|
|
open(CRT, ">>$crt") or die "Couldn't open $crt: $!";
|
|
|
|
print CRT "# $1\n";
|
|
|
|
close(CRT) or die "Couldn't close $crt: $!";
|
|
|
|
}
|
|
|
|
if (/^CKA_LABEL\s+[A-Z0-9]+\s+\"(.*)\"/) {
|
2008-02-11 10:00:00 -05:00
|
|
|
$caname = $1;
|
2008-02-07 20:08:25 -05:00
|
|
|
}
|
|
|
|
if (/^CKA_VALUE MULTILINE_OCTAL/) {
|
|
|
|
my $data;
|
|
|
|
while (<TXT>) {
|
|
|
|
last if (/^END/);
|
|
|
|
chomp;
|
|
|
|
my @octets = split(/\\/);
|
|
|
|
shift @octets;
|
|
|
|
for (@octets) {
|
|
|
|
$data .= chr(oct);
|
|
|
|
}
|
|
|
|
}
|
2008-02-11 10:00:00 -05:00
|
|
|
my $pem = "-----BEGIN CERTIFICATE-----\n"
|
|
|
|
. MIME::Base64::encode($data)
|
|
|
|
. "-----END CERTIFICATE-----\n";
|
2008-02-07 20:08:25 -05:00
|
|
|
open(CRT, ">>$crt") or die "Couldn't open $crt: $!";
|
2008-08-23 08:14:05 -04:00
|
|
|
print CRT "\n$caname\n";
|
2008-02-11 10:00:00 -05:00
|
|
|
print CRT ("=" x length($caname) . "\n");
|
|
|
|
if (!$opt_t) {
|
|
|
|
print CRT $pem;
|
|
|
|
}
|
2008-02-09 10:00:07 -05:00
|
|
|
close(CRT) or die "Couldn't close $crt: $!";
|
2008-02-11 10:00:00 -05:00
|
|
|
if ($opt_t) {
|
2008-02-11 13:52:45 -05:00
|
|
|
open(TMP, "|$openssl x509 -md5 -fingerprint -text -inform PEM >> $crt") or die "Couldn't open openssl pipe: $!";
|
2008-02-11 10:00:00 -05:00
|
|
|
print TMP $pem;
|
2008-02-11 13:52:45 -05:00
|
|
|
close(TMP) or die "Couldn't close openssl pipe: $!";
|
2008-02-11 10:00:00 -05:00
|
|
|
}
|
|
|
|
print "Parsing: $caname\n" if ($opt_v);
|
2008-02-07 20:08:25 -05:00
|
|
|
$certnum ++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
close(TXT) or die "Couldn't close $txt: $!";
|
|
|
|
unlink $txt if ($opt_u);
|
2008-02-11 10:00:00 -05:00
|
|
|
print "Done ($certnum CA certs processed).\n" if (!$opt_q);
|
2008-02-07 20:08:25 -05:00
|
|
|
|
|
|
|
exit;
|
|
|
|
|
|
|
|
|