2008-02-11 13:52:45 -05:00
|
|
|
#!/usr/bin/perl -w
|
2008-02-07 20:08:25 -05:00
|
|
|
# ***************************************************************************
|
|
|
|
# * _ _ ____ _
|
|
|
|
# * Project ___| | | | _ \| |
|
|
|
|
# * / __| | | | |_) | |
|
|
|
|
# * | (__| |_| | _ <| |___
|
|
|
|
# * \___|\___/|_| \_\_____|
|
|
|
|
# *
|
2013-01-05 17:29:04 -05:00
|
|
|
# * Copyright (C) 1998 - 2013, 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;
|
2012-03-31 13:48:15 -04:00
|
|
|
use LWP::UserAgent;
|
2008-02-11 10:00:00 -05:00
|
|
|
use strict;
|
2013-01-05 17:29:04 -05:00
|
|
|
use vars qw($opt_b $opt_f $opt_h $opt_i $opt_l $opt_n $opt_q $opt_t $opt_u $opt_v);
|
2008-02-07 20:08:25 -05:00
|
|
|
|
2012-04-04 10:40:41 -04: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';
|
|
|
|
|
2013-01-05 17:29:04 -05:00
|
|
|
my $version = '1.17';
|
2008-02-14 19:26:26 -05:00
|
|
|
|
2013-01-05 17:29:04 -05:00
|
|
|
getopts('bfhilnqtuv');
|
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) {
|
2013-01-05 17:29:04 -05:00
|
|
|
printf("Usage:\t%s [-b] [-f] [-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";
|
2013-01-05 17:29:04 -05:00
|
|
|
print "\t-f\tforce rebuild even if certdata.txt is current\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
|
|
|
|
2013-01-05 17:29:04 -05:00
|
|
|
my $stdout = $crt eq '-';
|
2011-03-14 01:52:33 -04:00
|
|
|
my $resp;
|
2013-01-05 17:29:04 -05:00
|
|
|
my $fetched;
|
2011-03-14 01:52:33 -04:00
|
|
|
|
|
|
|
unless ($opt_n and -e $txt) {
|
2013-01-05 17:29:04 -05:00
|
|
|
print STDERR "Downloading '$txt' ...\n" if (!$opt_q);
|
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);
|
2011-09-04 11:05:49 -04:00
|
|
|
if ($resp && $resp->code eq '304') {
|
2013-01-05 17:29:04 -05:00
|
|
|
print STDERR "Not modified\n" unless $opt_q;
|
|
|
|
exit 0 if -e $crt && !$opt_f;
|
|
|
|
} else {
|
|
|
|
$fetched = 1;
|
2011-09-04 11:05:49 -04:00
|
|
|
}
|
2013-01-05 17:29:04 -05:00
|
|
|
if( !$resp || $resp->code !~ /^(?:200|304)$/ ) {
|
|
|
|
print STDERR "Unable to download latest data: "
|
|
|
|
. ($resp? $resp->code . ' - ' . $resp->message : "LWP failed") . "\n"
|
|
|
|
unless $opt_q;
|
|
|
|
exit 1 if -e $crt || ! -r $txt;
|
2008-02-09 20:29:24 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-01-05 17:29:04 -05:00
|
|
|
my $currentdate = scalar gmtime($fetched ? $resp->last_modified : (stat($txt))[9]);
|
|
|
|
|
2008-02-11 10:00:00 -05:00
|
|
|
my $format = $opt_t ? "plain text and " : "";
|
2013-01-05 17:29:04 -05:00
|
|
|
if( $stdout ) {
|
|
|
|
open(CRT, '> -') or die "Couldn't open STDOUT: $!\n";
|
|
|
|
} else {
|
|
|
|
open(CRT,">$crt.~") or die "Couldn't open $crt.~: $!\n";
|
|
|
|
}
|
2008-02-07 20:08:25 -05:00
|
|
|
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
|
|
|
|
|
2013-01-05 17:29:04 -05:00
|
|
|
print STDERR "Processing '$txt' ...\n" if (!$opt_q);
|
2008-02-11 10:00:00 -05:00
|
|
|
my $caname;
|
|
|
|
my $certnum = 0;
|
2011-09-04 11:05:49 -04:00
|
|
|
my $skipnum = 0;
|
2012-09-04 17:21:15 -04:00
|
|
|
my $start_of_cert = 0;
|
|
|
|
|
2013-01-05 17:29:04 -05:00
|
|
|
open(TXT,"$txt") or die "Couldn't open $txt: $!\n";
|
2008-02-07 20:08:25 -05:00
|
|
|
while (<TXT>) {
|
|
|
|
if (/\*\*\*\*\* BEGIN LICENSE BLOCK \*\*\*\*\*/) {
|
|
|
|
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 \*\*\*\*\*/);
|
|
|
|
}
|
|
|
|
}
|
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
|
|
|
print CRT "# $1\n";
|
|
|
|
}
|
2012-09-04 17:21:15 -04:00
|
|
|
|
|
|
|
# this is a match for the start of a certificate
|
|
|
|
if (/^CKA_CLASS CK_OBJECT_CLASS CKO_CERTIFICATE/) {
|
|
|
|
$start_of_cert = 1
|
|
|
|
}
|
|
|
|
if ($start_of_cert && /^CKA_LABEL UTF8 \"(.*)\"/) {
|
2008-02-11 10:00:00 -05:00
|
|
|
$caname = $1;
|
2008-02-07 20:08:25 -05:00
|
|
|
}
|
2011-09-04 11:05:49 -04:00
|
|
|
my $untrusted = 0;
|
2012-09-04 17:21:15 -04:00
|
|
|
if ($start_of_cert && /^CKA_VALUE MULTILINE_OCTAL/) {
|
2008-02-07 20:08:25 -05:00
|
|
|
my $data;
|
|
|
|
while (<TXT>) {
|
|
|
|
last if (/^END/);
|
|
|
|
chomp;
|
|
|
|
my @octets = split(/\\/);
|
|
|
|
shift @octets;
|
|
|
|
for (@octets) {
|
|
|
|
$data .= chr(oct);
|
|
|
|
}
|
|
|
|
}
|
2012-09-04 17:21:15 -04:00
|
|
|
# scan forwards until the trust part
|
2011-09-04 11:05:49 -04:00
|
|
|
while (<TXT>) {
|
2012-09-04 17:21:15 -04:00
|
|
|
last if (/^CKA_CLASS CK_OBJECT_CLASS CKO_NSS_TRUST/);
|
|
|
|
chomp;
|
|
|
|
}
|
|
|
|
# now scan the trust part for untrusted certs
|
|
|
|
while (<TXT>) {
|
|
|
|
last if (/^#/);
|
|
|
|
if (/^CKA_TRUST_SERVER_AUTH\s+CK_TRUST\s+CKT_NSS_NOT_TRUSTED$/
|
|
|
|
or /^CKA_TRUST_SERVER_AUTH\s+CK_TRUST\s+CKT_NSS_TRUST_UNKNOWN$/) {
|
|
|
|
$untrusted = 1;
|
|
|
|
}
|
2008-02-11 10:00:00 -05:00
|
|
|
}
|
2011-09-04 11:05:49 -04:00
|
|
|
if ($untrusted) {
|
|
|
|
$skipnum ++;
|
|
|
|
} else {
|
|
|
|
my $pem = "-----BEGIN CERTIFICATE-----\n"
|
|
|
|
. MIME::Base64::encode($data)
|
|
|
|
. "-----END CERTIFICATE-----\n";
|
|
|
|
print CRT "\n$caname\n";
|
|
|
|
print CRT ("=" x length($caname) . "\n");
|
|
|
|
if (!$opt_t) {
|
|
|
|
print CRT $pem;
|
|
|
|
}
|
|
|
|
if ($opt_t) {
|
2013-01-05 17:29:04 -05:00
|
|
|
open(TMP, "|$openssl x509 -md5 -fingerprint -text -inform PEM >> $crt") or die "Couldn't open openssl pipe: $!\n";
|
2011-09-04 11:05:49 -04:00
|
|
|
print TMP $pem;
|
2013-01-05 17:29:04 -05:00
|
|
|
close(TMP) or die "Couldn't close openssl pipe: $!\n";
|
2011-09-04 11:05:49 -04:00
|
|
|
}
|
2013-01-05 17:29:04 -05:00
|
|
|
print STDERR "Parsing: $caname\n" if ($opt_v);
|
2011-09-04 11:05:49 -04:00
|
|
|
$certnum ++;
|
2012-09-04 17:21:15 -04:00
|
|
|
$start_of_cert = 0;
|
2008-02-11 10:00:00 -05:00
|
|
|
}
|
2008-02-07 20:08:25 -05:00
|
|
|
}
|
|
|
|
}
|
2013-01-05 17:29:04 -05:00
|
|
|
close(TXT) or die "Couldn't close $txt: $!\n";
|
|
|
|
close(CRT) or die "Couldn't close $crt.~: $!\n";
|
|
|
|
unless( $stdout ) {
|
|
|
|
if ($opt_b && -e $crt) {
|
|
|
|
my $bk = 1;
|
|
|
|
while (-e "$crt.~${bk}~") {
|
|
|
|
$bk++;
|
|
|
|
}
|
|
|
|
rename $crt, "$crt.~${bk}~" or die "Failed to create backup $crt.~$bk}~: $!\n";
|
|
|
|
} elsif( -e $crt ) {
|
|
|
|
unlink( $crt ) or die "Failed to remove $crt: $!\n";
|
|
|
|
}
|
|
|
|
rename "$crt.~", $crt or die "Failed to rename $crt.~ to $crt: $!\n";
|
|
|
|
}
|
2008-02-07 20:08:25 -05:00
|
|
|
unlink $txt if ($opt_u);
|
2013-01-05 17:29:04 -05:00
|
|
|
print STDERR "Done ($certnum CA certs processed, $skipnum untrusted skipped).\n" if (!$opt_q);
|
2008-02-07 20:08:25 -05:00
|
|
|
|
|
|
|
exit;
|
|
|
|
|
|
|
|
|