1#!/usr/bin/perl -w 2# *************************************************************************** 3# * _ _ ____ _ 4# * Project ___| | | | _ \| | 5# * / __| | | | |_) | | 6# * | (__| |_| | _ <| |___ 7# * \___|\___/|_| \_\_____| 8# * 9# * Copyright (C) 1998 - 2014, 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# 33use Getopt::Std; 34use MIME::Base64; 35use LWP::UserAgent; 36use strict; 37use vars qw($opt_b $opt_d $opt_f $opt_h $opt_i $opt_l $opt_n $opt_q $opt_t $opt_u $opt_v $opt_w); 38 39my %urls = ( 40 'nss' => 41 'http://mxr.mozilla.org/nss/source/lib/ckfw/builtins/certdata.txt?raw=1', 42 'central' => 43 'http://mxr.mozilla.org/mozilla-central/source/security/nss/lib/ckfw/builtins/certdata.txt?raw=1', 44 'aurora' => 45 'http://mxr.mozilla.org/mozilla-aurora/source/security/nss/lib/ckfw/builtins/certdata.txt?raw=1', 46 'beta' => 47 'http://mxr.mozilla.org/mozilla-beta/source/security/nss/lib/ckfw/builtins/certdata.txt?raw=1', 48 'release' => 49 'http://mxr.mozilla.org/mozilla-release/source/security/nss/lib/ckfw/builtins/certdata.txt?raw=1', 50 'mozilla' => 51 'http://mxr.mozilla.org/mozilla/source/security/nss/lib/ckfw/builtins/certdata.txt?raw=1' 52); 53 54$opt_d = 'release'; 55 56# If the OpenSSL commandline is not in search path you can configure it here! 57my $openssl = 'openssl'; 58 59my $version = '1.20'; 60 61$opt_w = 76; # default base64 encoded lines length 62 63$0 =~ s@.*(/|\\)@@; 64$Getopt::Std::STANDARD_HELP_VERSION = 1; 65getopts('bd:fhilnqtuvw:'); 66 67if(!defined($opt_d)) { 68 # to make plain "-d" use not cause warnings, and actually still work 69 $opt_d = 'release'; 70} 71 72# Use predefined URL or else custom URL specified on command line. 73my $url = ( defined( $urls{$opt_d} ) ) ? $urls{$opt_d} : $opt_d; 74 75if ($opt_i) { 76 print ("=" x 78 . "\n"); 77 print "Script Version : $version\n"; 78 print "Perl Version : $]\n"; 79 print "Operating System Name : $^O\n"; 80 print "Getopt::Std.pm Version : ${Getopt::Std::VERSION}\n"; 81 print "MIME::Base64.pm Version : ${MIME::Base64::VERSION}\n"; 82 print "LWP::UserAgent.pm Version : ${LWP::UserAgent::VERSION}\n"; 83 print "LWP.pm Version : ${LWP::VERSION}\n"; 84 print ("=" x 78 . "\n"); 85} 86 87sub WARNING_MESSAGE() { 88 if ( $opt_d =~ m/^risk$/i ) { # Long Form Warning and Exit 89 print "Warning: Use of this script may pose some risk:\n"; 90 print "\n"; 91 print " 1) Using http is subject to man in the middle attack of certdata content\n"; 92 print " 2) Default to 'release', but more recent updates may be found in other trees\n"; 93 print " 3) certdata.txt file format may change, lag time to update this script\n"; 94 print " 4) Generally unwise to blindly trust CAs without manual review & verification\n"; 95 print " 5) Mozilla apps use additional security checks aren't represented in certdata\n"; 96 print " 6) Use of this script will make a security engineer grind his teeth and\n"; 97 print " swear at you. ;)\n"; 98 exit; 99 } else { # Short Form Warning 100 print "Warning: Use of this script may pose some risk, -d risk for more details.\n"; 101 } 102} 103 104sub HELP_MESSAGE() { 105 print "Usage:\t${0} [-b] [-d<certdata>] [-f] [-i] [-l] [-n] [-q] [-t] [-u] [-v] [-w<l>] [<outputfile>]\n"; 106 print "\t-b\tbackup an existing version of ca-bundle.crt\n"; 107 print "\t-d\tspecify Mozilla tree to pull certdata.txt or custom URL\n"; 108 print "\t\t Valid names are:\n"; 109 print "\t\t ", join( ", ", map { ( $_ =~ m/$opt_d/ ) ? "$_ (default)" : "$_" } sort keys %urls ), "\n"; 110 print "\t-f\tforce rebuild even if certdata.txt is current\n"; 111 print "\t-i\tprint version info about used modules\n"; 112 print "\t-l\tprint license info about certdata.txt\n"; 113 print "\t-n\tno download of certdata.txt (to use existing)\n"; 114 print "\t-q\tbe really quiet (no progress output at all)\n"; 115 print "\t-t\tinclude plain text listing of certificates\n"; 116 print "\t-u\tunlink (remove) certdata.txt after processing\n"; 117 print "\t-v\tbe verbose and print out processed CAs\n"; 118 print "\t-w <l>\twrap base64 output lines after <l> chars (default: ${opt_w})\n"; 119 exit; 120} 121 122sub VERSION_MESSAGE() { 123 print "${0} version ${version} running Perl ${]} on ${^O}\n"; 124} 125 126WARNING_MESSAGE() unless ($opt_q || $url =~ m/^(ht|f)tps:/i ); 127HELP_MESSAGE() if ($opt_h); 128 129my $crt = $ARGV[0] || 'ca-bundle.crt'; 130(my $txt = $url) =~ s@(.*/|\?.*)@@g; 131 132my $stdout = $crt eq '-'; 133my $resp; 134my $fetched; 135 136unless ($opt_n and -e $txt) { 137 print STDERR "Downloading '$txt' ...\n" if (!$opt_q); 138 my $ua = new LWP::UserAgent(agent => "$0/$version"); 139 $ua->env_proxy(); 140 $resp = $ua->mirror($url, $txt); 141 if ($resp && $resp->code eq '304') { 142 print STDERR "Not modified\n" unless $opt_q; 143 exit 0 if -e $crt && !$opt_f; 144 } else { 145 $fetched = 1; 146 } 147 if( !$resp || $resp->code !~ /^(?:200|304)$/ ) { 148 print STDERR "Unable to download latest data: " 149 . ($resp? $resp->code . ' - ' . $resp->message : "LWP failed") . "\n" 150 unless $opt_q; 151 exit 1 if -e $crt || ! -r $txt; 152 } 153} 154 155my $currentdate = scalar gmtime($fetched ? $resp->last_modified : (stat($txt))[9]); 156 157my $format = $opt_t ? "plain text and " : ""; 158if( $stdout ) { 159 open(CRT, '> -') or die "Couldn't open STDOUT: $!\n"; 160} else { 161 open(CRT,">$crt.~") or die "Couldn't open $crt.~: $!\n"; 162} 163print CRT <<EOT; 164## 165## $crt -- Bundle of CA Root Certificates 166## 167## Certificate data from Mozilla as of: ${currentdate} 168## 169## This is a bundle of X.509 certificates of public Certificate Authorities 170## (CA). These were automatically extracted from Mozilla's root certificates 171## file (certdata.txt). This file can be found in the mozilla source tree: 172## ${url} 173## 174## It contains the certificates in ${format}PEM format and therefore 175## can be directly used with curl / libcurl / php_curl, or with 176## an Apache+mod_ssl webserver for SSL client authentication. 177## Just configure this file as the SSLCACertificateFile. 178## 179 180EOT 181 182print STDERR "Processing '$txt' ...\n" if (!$opt_q); 183my $caname; 184my $certnum = 0; 185my $skipnum = 0; 186my $start_of_cert = 0; 187 188open(TXT,"$txt") or die "Couldn't open $txt: $!\n"; 189while (<TXT>) { 190 if (/\*\*\*\*\* BEGIN LICENSE BLOCK \*\*\*\*\*/) { 191 print CRT; 192 print if ($opt_l); 193 while (<TXT>) { 194 print CRT; 195 print if ($opt_l); 196 last if (/\*\*\*\*\* END LICENSE BLOCK \*\*\*\*\*/); 197 } 198 } 199 next if /^#|^\s*$/; 200 chomp; 201 if (/^CVS_ID\s+\"(.*)\"/) { 202 print CRT "# $1\n"; 203 } 204 205 # this is a match for the start of a certificate 206 if (/^CKA_CLASS CK_OBJECT_CLASS CKO_CERTIFICATE/) { 207 $start_of_cert = 1 208 } 209 if ($start_of_cert && /^CKA_LABEL UTF8 \"(.*)\"/) { 210 $caname = $1; 211 } 212 my $untrusted = 1; 213 if ($start_of_cert && /^CKA_VALUE MULTILINE_OCTAL/) { 214 my $data; 215 while (<TXT>) { 216 last if (/^END/); 217 chomp; 218 my @octets = split(/\\/); 219 shift @octets; 220 for (@octets) { 221 $data .= chr(oct); 222 } 223 } 224 # scan forwards until the trust part 225 while (<TXT>) { 226 last if (/^CKA_CLASS CK_OBJECT_CLASS CKO_NSS_TRUST/); 227 chomp; 228 } 229 # now scan the trust part for untrusted certs 230 while (<TXT>) { 231 last if (/^#/); 232 if (/^CKA_TRUST_SERVER_AUTH\s+CK_TRUST\s+CKT_NSS_TRUSTED_DELEGATOR$/) { 233 $untrusted = 0; 234 } 235 } 236 if ($untrusted) { 237 $skipnum ++; 238 } else { 239 my $encoded = MIME::Base64::encode_base64($data, ''); 240 $encoded =~ s/(.{1,${opt_w}})/$1\n/g; 241 my $pem = "-----BEGIN CERTIFICATE-----\n" 242 . $encoded 243 . "-----END CERTIFICATE-----\n"; 244 print CRT "\n$caname\n"; 245 print CRT ("=" x length($caname) . "\n"); 246 if (!$opt_t) { 247 print CRT $pem; 248 } else { 249 my $pipe = "|$openssl x509 -md5 -fingerprint -text -inform PEM"; 250 if (!$stdout) { 251 $pipe .= " >> $crt.~"; 252 close(CRT) or die "Couldn't close $crt.~: $!"; 253 } 254 open(TMP, $pipe) or die "Couldn't open openssl pipe: $!"; 255 print TMP $pem; 256 close(TMP) or die "Couldn't close openssl pipe: $!"; 257 if (!$stdout) { 258 open(CRT, ">>$crt.~") or die "Couldn't open $crt.~: $!"; 259 } 260 } 261 print STDERR "Parsing: $caname\n" if ($opt_v); 262 $certnum ++; 263 $start_of_cert = 0; 264 } 265 } 266} 267close(TXT) or die "Couldn't close $txt: $!\n"; 268close(CRT) or die "Couldn't close $crt.~: $!\n"; 269unless( $stdout ) { 270 if ($opt_b && -e $crt) { 271 my $bk = 1; 272 while (-e "$crt.~${bk}~") { 273 $bk++; 274 } 275 rename $crt, "$crt.~${bk}~" or die "Failed to create backup $crt.~$bk}~: $!\n"; 276 } elsif( -e $crt ) { 277 unlink( $crt ) or die "Failed to remove $crt: $!\n"; 278 } 279 rename "$crt.~", $crt or die "Failed to rename $crt.~ to $crt: $!\n"; 280} 281unlink $txt if ($opt_u); 282print STDERR "Done ($certnum CA certs processed, $skipnum untrusted skipped).\n" if (!$opt_q); 283 284exit; 285 286 287