2018-01-07 14:34:46 +08:00
#!/usr/bin/env perl
2008-02-08 09:08:25 +08:00
# ***************************************************************************
# * _ _ ____ _
# * Project ___| | | | _ \| |
# * / __| | | | |_) | |
# * | (__| |_| | _ <| |___
# * \___|\___/|_| \_\_____|
# *
2020-01-21 05:23:44 +08:00
# * Copyright (C) 1998 - 2020, Daniel Stenberg, <daniel@haxx.se>, et al.
2008-02-08 09:08:25 +08:00
# *
# * This software is licensed as described in the file COPYING, which
# * you should have received as part of this distribution. The terms
2016-02-03 07:19:02 +08:00
# * are also available at https://curl.haxx.se/docs/copyright.html.
2008-02-08 09:08:25 +08:00
# *
# * 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-15 03:40:18 +08:00
# This Perl script creates a fresh ca-bundle.crt file for use with libcurl.
2008-02-08 09:08:25 +08: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 23:00:00 +08:00
# The script is based on the parse-certs script written by Roland Krikava.
2008-02-08 09:08:25 +08:00
# This Perl script works on almost any platform since its only external
2008-02-11 23:00:00 +08:00
# dependency is the OpenSSL commandline tool for optional text listing.
2008-02-08 09:08:25 +08:00
# Hacked by Guenter Knauf.
#
2016-10-30 13:01:29 +08:00
use Encode ;
2008-02-08 09:08:25 +08:00
use Getopt::Std ;
use MIME::Base64 ;
2008-02-11 23:00:00 +08:00
use strict ;
2018-01-07 14:34:46 +08:00
use warnings ;
2016-09-15 14:26:56 +08:00
use vars qw( $opt_b $opt_d $opt_f $opt_h $opt_i $opt_k $opt_l $opt_m $opt_n $opt_p $opt_q $opt_s $opt_t $opt_u $opt_v $opt_w ) ;
2014-05-08 17:37:45 +08:00
use List::Util ;
use Text::Wrap ;
2020-01-21 05:23:44 +08:00
use Time::Local ;
2014-10-23 21:18:05 +08:00
my $ MOD_SHA = "Digest::SHA" ;
eval "require $MOD_SHA" ;
if ( $@ ) {
$ MOD_SHA = "Digest::SHA::PurePerl" ;
eval "require $MOD_SHA" ;
}
2016-09-15 14:26:56 +08:00
eval "require LWP::UserAgent" ;
2014-01-06 06:37:26 +08:00
my % urls = (
'nss' = >
2017-10-19 20:40:54 +08:00
'https://hg.mozilla.org/projects/nss/raw-file/default/lib/ckfw/builtins/certdata.txt' ,
2014-01-06 06:37:26 +08:00
'central' = >
2016-09-15 14:26:56 +08:00
'https://hg.mozilla.org/mozilla-central/raw-file/default/security/nss/lib/ckfw/builtins/certdata.txt' ,
2014-01-06 06:37:26 +08:00
'beta' = >
2016-09-15 14:26:56 +08:00
'https://hg.mozilla.org/releases/mozilla-beta/raw-file/default/security/nss/lib/ckfw/builtins/certdata.txt' ,
2014-01-06 06:37:26 +08:00
'release' = >
2016-09-15 14:26:56 +08:00
'https://hg.mozilla.org/releases/mozilla-release/raw-file/default/security/nss/lib/ckfw/builtins/certdata.txt' ,
2014-01-06 06:37:26 +08:00
) ;
$ opt_d = 'release' ;
2008-02-08 09:08:25 +08:00
# If the OpenSSL commandline is not in search path you can configure it here!
my $ openssl = 'openssl' ;
2020-04-22 14:13:23 +08:00
my $ version = '1.28' ;
2008-02-15 08:26:26 +08:00
2013-04-04 06:02:49 +08:00
$ opt_w = 76 ; # default base64 encoded lines length
2014-05-08 17:37:45 +08:00
# default cert types to include in the output (default is to include CAs which may issue SSL server certs)
my $ default_mozilla_trust_purposes = "SERVER_AUTH" ;
my $ default_mozilla_trust_levels = "TRUSTED_DELEGATOR" ;
$ opt_p = $ default_mozilla_trust_purposes . ":" . $ default_mozilla_trust_levels ;
my @ valid_mozilla_trust_purposes = (
"DIGITAL_SIGNATURE" ,
"NON_REPUDIATION" ,
"KEY_ENCIPHERMENT" ,
"DATA_ENCIPHERMENT" ,
"KEY_AGREEMENT" ,
"KEY_CERT_SIGN" ,
"CRL_SIGN" ,
"SERVER_AUTH" ,
"CLIENT_AUTH" ,
"CODE_SIGNING" ,
"EMAIL_PROTECTION" ,
"IPSEC_END_SYSTEM" ,
"IPSEC_TUNNEL" ,
"IPSEC_USER" ,
"TIME_STAMPING" ,
"STEP_UP_APPROVED"
) ;
my @ valid_mozilla_trust_levels = (
"TRUSTED_DELEGATOR" , # CAs
"NOT_TRUSTED" , # Don't trust these certs.
"MUST_VERIFY_TRUST" , # This explicitly tells us that it ISN'T a CA but is otherwise ok. In other words, this should tell the app to ignore any other sources that claim this is a CA.
"TRUSTED" # This cert is trusted, but only for itself and not for delegates (i.e. it is not a CA).
) ;
my $ default_signature_algorithms = $ opt_s = "MD5" ;
my @ valid_signature_algorithms = (
"MD5" ,
"SHA1" ,
"SHA256" ,
2014-10-15 18:48:27 +08:00
"SHA384" ,
2014-09-11 22:26:26 +08:00
"SHA512"
2014-05-08 17:37:45 +08:00
) ;
2013-04-04 10:01:01 +08:00
$ 0 =~ s@.*(/|\\)@@ ;
2013-04-04 06:21:10 +08:00
$ Getopt:: Std:: STANDARD_HELP_VERSION = 1 ;
2016-09-15 14:26:56 +08:00
getopts ( 'bd:fhiklmnp:qs:tuvw:' ) ;
2014-01-06 06:37:26 +08:00
2014-01-06 06:53:26 +08:00
if ( ! defined ( $ opt_d ) ) {
# to make plain "-d" use not cause warnings, and actually still work
$ opt_d = 'release' ;
}
2014-01-06 06:37:26 +08:00
# Use predefined URL or else custom URL specified on command line.
2016-09-15 14:26:56 +08:00
my $ url ;
if ( defined ( $ urls { $ opt_d } ) ) {
$ url = $ urls { $ opt_d } ;
if ( ! $ opt_k && $ url !~ /^https:\/\//i ) {
die "The URL for '$opt_d' is not HTTPS. Use -k to override (insecure).\n" ;
}
}
else {
$ url = $ opt_d ;
}
2008-02-08 09:08:25 +08:00
2014-10-27 02:07:35 +08:00
my $ curl = `curl -V` ;
2014-09-10 18:14:13 +08:00
2008-02-15 08:26:26 +08:00
if ( $ opt_i ) {
print ( "=" x 78 . "\n" ) ;
2014-10-23 21:18:05 +08:00
print "Script Version : $version\n" ;
print "Perl Version : $]\n" ;
print "Operating System Name : $^O\n" ;
print "Getopt::Std.pm Version : ${Getopt::Std::VERSION}\n" ;
2018-06-07 16:44:30 +08:00
print "Encode::Encoding.pm Version : ${Encode::Encoding::VERSION}\n" ;
2014-10-23 21:18:05 +08:00
print "MIME::Base64.pm Version : ${MIME::Base64::VERSION}\n" ;
2016-09-15 14:26:56 +08:00
print "LWP::UserAgent.pm Version : ${LWP::UserAgent::VERSION}\n" if ( $ LWP:: UserAgent:: VERSION ) ;
print "LWP.pm Version : ${LWP::VERSION}\n" if ( $ LWP:: VERSION ) ;
2014-10-23 21:18:05 +08:00
print "Digest::SHA.pm Version : ${Digest::SHA::VERSION}\n" if ( $ Digest:: SHA:: VERSION ) ;
print "Digest::SHA::PurePerl.pm Version : ${Digest::SHA::PurePerl::VERSION}\n" if ( $ Digest:: SHA:: PurePerl:: VERSION ) ;
2008-02-15 08:26:26 +08:00
print ( "=" x 78 . "\n" ) ;
}
2014-10-27 02:07:35 +08:00
sub warning_message () {
2014-01-06 06:37:26 +08:00
if ( $ opt_d =~ m/^risk$/i ) { # Long Form Warning and Exit
print "Warning: Use of this script may pose some risk:\n" ;
2014-09-11 22:26:26 +08:00
print "\n" ;
2016-09-15 14:26:56 +08:00
print " 1) If you use HTTP URLs they are subject to a man in the middle attack\n" ;
2014-09-11 22:26:26 +08:00
print " 2) Default to 'release', but more recent updates may be found in other trees\n" ;
print " 3) certdata.txt file format may change, lag time to update this script\n" ;
print " 4) Generally unwise to blindly trust CAs without manual review & verification\n" ;
print " 5) Mozilla apps use additional security checks aren't represented in certdata\n" ;
print " 6) Use of this script will make a security engineer grind his teeth and\n" ;
print " swear at you. ;)\n" ;
2014-01-06 06:37:26 +08:00
exit ;
} else { # Short Form Warning
print "Warning: Use of this script may pose some risk, -d risk for more details.\n" ;
}
}
2013-04-04 06:21:10 +08:00
sub HELP_MESSAGE () {
2016-09-15 14:26:56 +08:00
print "Usage:\t${0} [-b] [-d<certdata>] [-f] [-i] [-k] [-l] [-n] [-p<purposes:levels>] [-q] [-s<algorithms>] [-t] [-u] [-v] [-w<l>] [<outputfile>]\n" ;
2008-02-10 09:29:24 +08:00
print "\t-b\tbackup an existing version of ca-bundle.crt\n" ;
2014-01-06 06:37:26 +08:00
print "\t-d\tspecify Mozilla tree to pull certdata.txt or custom URL\n" ;
print "\t\t Valid names are:\n" ;
print "\t\t " , join ( ", " , map { ( $ _ =~ m/$opt_d/ ) ? "$_ (default)" : "$_" } sort keys % urls ) , "\n" ;
2013-01-06 06:29:04 +08:00
print "\t-f\tforce rebuild even if certdata.txt is current\n" ;
2008-02-08 09:08:25 +08:00
print "\t-i\tprint version info about used modules\n" ;
2016-09-15 14:26:56 +08:00
print "\t-k\tallow URLs other than HTTPS, enable HTTP fallback (insecure)\n" ;
2008-02-08 09:58:11 +08:00
print "\t-l\tprint license info about certdata.txt\n" ;
2016-08-02 18:17:10 +08:00
print "\t-m\tinclude meta data in output\n" ;
2008-02-08 09:08:25 +08:00
print "\t-n\tno download of certdata.txt (to use existing)\n" ;
2014-05-08 17:37:45 +08:00
print wrap ( "\t" , "\t\t" , "-p\tlist of Mozilla trust purposes and levels for certificates to include in output. Takes the form of a comma separated list of purposes, a colon, and a comma separated list of levels. (default: $default_mozilla_trust_purposes:$default_mozilla_trust_levels)" ) , "\n" ;
print "\t\t Valid purposes are:\n" ;
print wrap ( "\t\t " , "\t\t " , join ( ", " , "ALL" , @ valid_mozilla_trust_purposes ) ) , "\n" ;
print "\t\t Valid levels are:\n" ;
print wrap ( "\t\t " , "\t\t " , join ( ", " , "ALL" , @ valid_mozilla_trust_levels ) ) , "\n" ;
2008-02-11 23:00:00 +08:00
print "\t-q\tbe really quiet (no progress output at all)\n" ;
2014-05-08 17:37:45 +08:00
print wrap ( "\t" , "\t\t" , "-s\tcomma separated list of certificate signatures/hashes to output in plain text mode. (default: $default_signature_algorithms)\n" ) ;
print "\t\t Valid signature algorithms are:\n" ;
print wrap ( "\t\t " , "\t\t " , join ( ", " , "ALL" , @ valid_signature_algorithms ) ) , "\n" ;
2008-02-11 23:00:00 +08:00
print "\t-t\tinclude plain text listing of certificates\n" ;
2008-02-08 09:08:25 +08:00
print "\t-u\tunlink (remove) certdata.txt after processing\n" ;
print "\t-v\tbe verbose and print out processed CAs\n" ;
2013-04-04 06:02:49 +08:00
print "\t-w <l>\twrap base64 output lines after <l> chars (default: ${opt_w})\n" ;
2008-02-08 09:08:25 +08:00
exit ;
}
2013-04-04 06:21:10 +08:00
sub VERSION_MESSAGE () {
2013-04-04 06:23:58 +08:00
print "${0} version ${version} running Perl ${]} on ${^O}\n" ;
2013-04-04 06:21:10 +08:00
}
2014-10-27 02:07:35 +08:00
warning_message ( ) unless ( $ opt_q || $ url =~ m/^(ht|f)tps:/i ) ;
2013-04-04 06:21:10 +08:00
HELP_MESSAGE ( ) if ( $ opt_h ) ;
2015-03-11 21:34:59 +08:00
sub report ($@) {
my $ output = shift ;
print STDERR $ output . "\n" unless $ opt_q ;
}
2014-10-27 02:07:35 +08:00
sub is_in_list ($@) {
2014-05-08 17:37:45 +08:00
my $ target = shift ;
return defined ( List::Util:: first { $ target eq $ _ } @ _ ) ;
}
# Parses $param_string as a case insensitive comma separated list with optional whitespace
# validates that only allowed parameters are supplied
2014-10-27 02:07:35 +08:00
sub parse_csv_param ($$@) {
2014-05-08 17:37:45 +08:00
my $ description = shift ;
my $ param_string = shift ;
my @ valid_values = @ _ ;
my @ values = map {
s/^\s+// ; # strip leading spaces
s/\s+$// ; # strip trailing spaces
uc $ _ # return the modified string as upper case
} split ( ',' , $ param_string ) ;
# Find all values which are not in the list of valid values or "ALL"
2014-10-27 02:07:35 +08:00
my @ invalid = grep { ! is_in_list ( $ _ , "ALL" , @ valid_values ) } @ values ;
2014-05-08 17:37:45 +08:00
if ( scalar ( @ invalid ) > 0 ) {
# Tell the user which parameters were invalid and print the standard help message which will exit
print "Error: Invalid " , $ description , scalar ( @ invalid ) == 1 ? ": " : "s: " , join ( ", " , map { "\"$_\"" } @ invalid ) , "\n" ;
HELP_MESSAGE ( ) ;
}
2014-09-11 22:26:26 +08:00
2014-10-27 02:07:35 +08:00
@ values = @ valid_values if ( is_in_list ( "ALL" , @ values ) ) ;
2014-09-11 22:26:26 +08:00
2014-05-08 17:37:45 +08:00
return @ values ;
}
2016-09-07 16:41:11 +08:00
sub sha256 {
2014-10-23 21:18:05 +08:00
my $ result ;
if ( $ Digest:: SHA:: VERSION || $ Digest:: SHA:: PurePerl:: VERSION ) {
open ( FILE , $ _ [ 0 ] ) or die "Can't open '$_[0]': $!" ;
binmode ( FILE ) ;
2016-09-07 16:41:11 +08:00
$ result = $ MOD_SHA - > new ( 256 ) - > addfile ( * FILE ) - > hexdigest ;
2014-10-23 21:18:05 +08:00
close ( FILE ) ;
} else {
# Use OpenSSL command if Perl Digest::SHA modules not available
2016-10-25 15:17:26 +08:00
$ result = `"$openssl" dgst -r -sha256 "$_[0]"` ;
$ result =~ s/^([0-9a-f]{64}) .+/$1/is ;
2014-10-23 21:18:05 +08:00
}
return $ result ;
2014-08-14 05:42:53 +08:00
}
2014-10-23 21:18:05 +08:00
2016-09-07 16:41:11 +08:00
sub oldhash {
my $ hash = "" ;
2014-10-23 22:27:40 +08:00
open ( C , "<$_[0]" ) || return 0 ;
while ( <C> ) {
chomp ;
2016-09-07 16:41:11 +08:00
if ( $ _ =~ /^\#\# SHA256: (.*)/ ) {
$ hash = $ 1 ;
2014-10-23 22:27:40 +08:00
last ;
2014-08-14 05:42:53 +08:00
}
2014-10-23 22:27:40 +08:00
}
close ( C ) ;
2016-09-07 16:41:11 +08:00
return $ hash ;
2014-08-14 05:42:53 +08:00
}
2014-05-08 17:37:45 +08:00
if ( $ opt_p !~ m/:/ ) {
print "Error: Mozilla trust identifier list must include both purposes and levels\n" ;
HELP_MESSAGE ( ) ;
}
( my $ included_mozilla_trust_purposes_string , my $ included_mozilla_trust_levels_string ) = split ( ':' , $ opt_p ) ;
2014-10-27 02:07:35 +08:00
my @ included_mozilla_trust_purposes = parse_csv_param ( "trust purpose" , $ included_mozilla_trust_purposes_string , @ valid_mozilla_trust_purposes ) ;
my @ included_mozilla_trust_levels = parse_csv_param ( "trust level" , $ included_mozilla_trust_levels_string , @ valid_mozilla_trust_levels ) ;
2014-05-08 17:37:45 +08:00
2014-10-27 02:07:35 +08:00
my @ included_signature_algorithms = parse_csv_param ( "signature algorithm" , $ opt_s , @ valid_signature_algorithms ) ;
2014-05-08 17:37:45 +08:00
2014-10-27 02:07:35 +08:00
sub should_output_cert (%) {
2014-05-08 17:37:45 +08:00
my % trust_purposes_by_level = @ _ ;
2014-09-11 22:26:26 +08:00
2014-05-08 17:37:45 +08:00
foreach my $ level ( @ included_mozilla_trust_levels ) {
# for each level we want to output, see if any of our desired purposes are included
2014-10-27 02:07:35 +08:00
return 1 if ( defined ( List::Util:: first { is_in_list ( $ _ , @ included_mozilla_trust_purposes ) } @ { $ trust_purposes_by_level { $ level } } ) ) ;
2014-05-08 17:37:45 +08:00
}
2014-09-11 22:26:26 +08:00
2014-05-08 17:37:45 +08:00
return 0 ;
}
2008-02-08 10:38:12 +08:00
my $ crt = $ ARGV [ 0 ] || 'ca-bundle.crt' ;
2011-04-14 07:27:39 +08:00
( my $ txt = $ url ) =~ s@(.*/|\?.*)@@g ;
2008-02-11 23:00:00 +08:00
2013-01-06 06:29:04 +08:00
my $ stdout = $ crt eq '-' ;
2011-03-14 13:52:33 +08:00
my $ resp ;
2013-01-06 06:29:04 +08:00
my $ fetched ;
2011-03-14 13:52:33 +08:00
2016-09-07 16:41:11 +08:00
my $ oldhash = oldhash ( $ crt ) ;
2014-08-14 05:42:53 +08:00
2016-09-07 16:41:11 +08:00
report "SHA256 of old file: $oldhash" ;
2014-09-10 18:14:13 +08:00
2016-09-15 14:26:56 +08:00
if ( ! $ opt_n ) {
report "Downloading $txt ..." ;
# If we have an HTTPS URL then use curl
if ( $ url =~ /^https:\/\//i ) {
if ( $ curl ) {
if ( $ curl =~ /^Protocols:.* https( |$)/m ) {
report "Get certdata with curl!" ;
my $ proto = ! $ opt_k ? "--proto =https" : "" ;
my $ quiet = $ opt_q ? "-s" : "" ;
my @ out = `curl -w %{response_code} $proto $quiet -o "$txt" "$url"` ;
2017-06-16 13:58:23 +08:00
if ( ! $? && @ out && $ out [ 0 ] == 200 ) {
2016-09-15 14:26:56 +08:00
$ fetched = 1 ;
report "Downloaded $txt" ;
}
else {
report "Failed downloading via HTTPS with curl" ;
if ( - e $ txt && ! unlink ( $ txt ) ) {
report "Failed to remove '$txt': $!" ;
}
}
}
else {
report "curl lacks https support" ;
}
}
else {
report "curl not found" ;
}
2014-10-23 22:27:40 +08:00
}
2014-08-14 05:42:53 +08:00
2016-09-15 14:26:56 +08:00
# If nothing was fetched then use LWP
if ( ! $ fetched ) {
if ( $ url =~ /^https:\/\//i ) {
report "Falling back to HTTP" ;
$ url =~ s/^https:\/\//http:\/\//i ;
}
if ( ! $ opt_k ) {
report "URLs other than HTTPS are disabled by default, to enable use -k" ;
exit 1 ;
}
report "Get certdata with LWP!" ;
if ( ! defined ( $ { LWP::UserAgent:: VERSION } ) ) {
report "LWP is not available (LWP::UserAgent not found)" ;
exit 1 ;
}
my $ ua = new LWP:: UserAgent ( agent = > "$0/$version" ) ;
$ ua - > env_proxy ( ) ;
$ resp = $ ua - > mirror ( $ url , $ txt ) ;
if ( $ resp && $ resp - > code eq '304' ) {
report "Not modified" ;
exit 0 if - e $ crt && ! $ opt_f ;
}
else {
2013-01-06 06:29:04 +08:00
$ fetched = 1 ;
2016-09-15 14:26:56 +08:00
report "Downloaded $txt" ;
}
if ( ! $ resp || $ resp - > code !~ /^(?:200|304)$/ ) {
2015-03-11 21:34:59 +08:00
report "Unable to download latest data: "
. ( $ resp ? $ resp - > code . ' - ' . $ resp - > message : "LWP failed" ) ;
2013-01-06 06:29:04 +08:00
exit 1 if - e $ crt || ! - r $ txt ;
2016-09-15 14:26:56 +08:00
}
2008-02-10 09:29:24 +08:00
}
}
2014-09-10 18:14:13 +08:00
my $ filedate = $ resp ? $ resp - > last_modified : ( stat ( $ txt ) ) [ 9 ] ;
2014-08-14 05:42:53 +08:00
my $ datesrc = "as of" ;
if ( ! $ filedate ) {
# mxr.mozilla.org gave us a time, hg.mozilla.org does not!
$ filedate = time ( ) ;
$ datesrc = "downloaded on" ;
}
# get the hash from the download file
2016-09-07 16:41:11 +08:00
my $ newhash = sha256 ( $ txt ) ;
2014-08-14 05:42:53 +08:00
2016-09-07 16:41:11 +08:00
if ( ! $ opt_f && $ oldhash eq $ newhash ) {
2015-03-11 21:34:59 +08:00
report "Downloaded file identical to previous run\'s source file. Exiting" ;
2018-06-14 04:45:23 +08:00
if ( $ opt_u && - e $ txt && ! unlink ( $ txt ) ) {
report "Failed to remove $txt: $!\n" ;
}
2014-08-14 05:42:53 +08:00
exit ;
}
2016-09-07 16:41:11 +08:00
report "SHA256 of new file: $newhash" ;
2014-08-14 05:42:53 +08:00
my $ currentdate = scalar gmtime ( $ filedate ) ;
2013-01-06 06:29:04 +08:00
2008-02-11 23:00:00 +08:00
my $ format = $ opt_t ? "plain text and " : "" ;
2013-01-06 06:29:04 +08: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-08 09:08:25 +08:00
print CRT << EOT ;
##
2014-08-14 05:42:53 +08:00
## Bundle of CA Root Certificates
2008-02-08 09:08:25 +08:00
##
2016-10-25 15:17:26 +08:00
## Certificate data from Mozilla ${datesrc}: ${currentdate} GMT
2008-02-08 09:08:25 +08: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:
2013-04-10 06:20:37 +08:00
## ${url}
2008-02-08 09:08:25 +08:00
##
2008-02-11 23:00:00 +08:00
## It contains the certificates in ${format}PEM format and therefore
2008-02-15 08:26:26 +08:00
## can be directly used with curl / libcurl / php_curl, or with
## an Apache+mod_ssl webserver for SSL client authentication.
2008-02-08 09:08:25 +08:00
## Just configure this file as the SSLCACertificateFile.
##
2014-10-28 22:20:42 +08:00
## Conversion done with mk-ca-bundle.pl version $version.
2016-09-07 16:41:11 +08:00
## SHA256: $newhash
2014-08-14 05:42:53 +08:00
##
2008-02-08 09:08:25 +08:00
EOT
2015-03-11 21:34:59 +08:00
report "Processing '$txt' ..." ;
2008-02-11 23:00:00 +08:00
my $ caname ;
my $ certnum = 0 ;
2011-09-04 23:05:49 +08:00
my $ skipnum = 0 ;
2012-09-05 05:21:15 +08:00
my $ start_of_cert = 0 ;
2016-08-02 18:17:10 +08:00
my @ precert ;
2020-01-21 05:23:44 +08:00
my $ cka_value ;
my $ valid = 1 ;
2012-09-05 05:21:15 +08:00
2013-01-06 06:29:04 +08:00
open ( TXT , "$txt" ) or die "Couldn't open $txt: $!\n" ;
2008-02-08 09:08:25 +08:00
while ( <TXT> ) {
if ( /\*\*\*\*\* BEGIN LICENSE BLOCK \*\*\*\*\*/ ) {
print CRT ;
2008-02-08 09:58:11 +08:00
print if ( $ opt_l ) ;
2008-02-08 09:08:25 +08:00
while ( <TXT> ) {
print CRT ;
2008-02-08 09:58:11 +08:00
print if ( $ opt_l ) ;
2008-02-08 09:08:25 +08:00
last if ( /\*\*\*\*\* END LICENSE BLOCK \*\*\*\*\*/ ) ;
}
}
2016-08-02 18:17:10 +08:00
elsif ( /^# (Issuer|Serial Number|Subject|Not Valid Before|Not Valid After |Fingerprint \(MD5\)|Fingerprint \(SHA1\)):/ ) {
push @ precert , $ _ ;
2020-01-21 05:23:44 +08:00
$ valid = 1 ;
2016-08-02 18:17:10 +08:00
next ;
}
elsif ( /^#|^\s*$/ ) {
undef @ precert ;
next ;
2008-02-08 09:08:25 +08:00
}
2016-08-02 18:17:10 +08:00
chomp ;
2012-09-05 05:21:15 +08:00
2020-01-21 05:23:44 +08:00
# Example:
# CKA_NSS_SERVER_DISTRUST_AFTER MULTILINE_OCTAL
# \062\060\060\066\061\067\060\060\060\060\060\060\132
# END
if ( /^CKA_NSS_SERVER_DISTRUST_AFTER (CK_BBOOL CK_FALSE|MULTILINE_OCTAL)/ ) {
if ( $ 1 eq "MULTILINE_OCTAL" ) {
my @ timestamp ;
while ( <TXT> ) {
last if ( /^END/ ) ;
chomp ;
my @ octets = split ( /\\/ ) ;
shift @ octets ;
for ( @ octets ) {
push @ timestamp , chr ( oct ) ;
}
}
# A trailing Z in the timestamp signifies UTC
if ( $ timestamp [ 12 ] ne "Z" ) {
report "distrust date stamp is not using UTC" ;
}
# Example date: 200617000000Z
# Means 2020-06-17 00:00:00 UTC
my $ distrustat =
timegm ( $ timestamp [ 10 ] . $ timestamp [ 11 ] , # second
$ timestamp [ 8 ] . $ timestamp [ 9 ] , # minute
$ timestamp [ 6 ] . $ timestamp [ 7 ] , # hour
$ timestamp [ 4 ] . $ timestamp [ 5 ] , # day
( $ timestamp [ 2 ] . $ timestamp [ 3 ] ) - 1 , # month
"20" . $ timestamp [ 0 ] . $ timestamp [ 1 ] ) ; # year
if ( time >= $ distrustat ) {
# not trusted anymore
$ skipnum + + ;
report "Skipping: $caname is not trusted anymore" if ( $ opt_v ) ;
$ valid = 0 ;
}
else {
# still trusted
}
}
next ;
}
2012-09-05 05:21:15 +08: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 23:00:00 +08:00
$ caname = $ 1 ;
2008-02-08 09:08:25 +08:00
}
2014-05-08 17:37:45 +08:00
my % trust_purposes_by_level ;
2012-09-05 05:21:15 +08:00
if ( $ start_of_cert && /^CKA_VALUE MULTILINE_OCTAL/ ) {
2020-01-21 05:23:44 +08:00
$ cka_value = "" ;
2008-02-08 09:08:25 +08:00
while ( <TXT> ) {
last if ( /^END/ ) ;
chomp ;
my @ octets = split ( /\\/ ) ;
shift @ octets ;
for ( @ octets ) {
2020-01-21 05:23:44 +08:00
$ cka_value . = chr ( oct ) ;
2008-02-08 09:08:25 +08:00
}
}
2020-01-21 05:23:44 +08:00
}
if ( /^CKA_CLASS CK_OBJECT_CLASS CKO_NSS_TRUST/ && $ valid ) {
2014-05-08 17:37:45 +08:00
# now scan the trust part to determine how we should trust this cert
2012-09-05 05:21:15 +08:00
while ( <TXT> ) {
last if ( /^#/ ) ;
2014-05-08 17:37:45 +08:00
if ( /^CKA_TRUST_([A-Z_]+)\s+CK_TRUST\s+CKT_NSS_([A-Z_]+)\s*$/ ) {
2014-10-27 02:07:35 +08:00
if ( ! is_in_list ( $ 1 , @ valid_mozilla_trust_purposes ) ) {
2015-03-11 21:34:59 +08:00
report "Warning: Unrecognized trust purpose for cert: $caname. Trust purpose: $1. Trust Level: $2" ;
2014-10-27 02:07:35 +08:00
} elsif ( ! is_in_list ( $ 2 , @ valid_mozilla_trust_levels ) ) {
2015-03-11 21:34:59 +08:00
report "Warning: Unrecognized trust level for cert: $caname. Trust purpose: $1. Trust Level: $2" ;
2014-05-08 17:37:45 +08:00
} else {
push @ { $ trust_purposes_by_level { $ 2 } } , $ 1 ;
}
2012-09-05 05:21:15 +08:00
}
2008-02-11 23:00:00 +08:00
}
2014-05-08 17:37:45 +08:00
2014-10-27 02:07:35 +08:00
if ( ! should_output_cert ( % trust_purposes_by_level ) ) {
2011-09-04 23:05:49 +08:00
$ skipnum + + ;
2018-06-07 16:44:30 +08:00
report "Skipping: $caname" if ( $ opt_v ) ;
2011-09-04 23:05:49 +08:00
} else {
2020-01-21 05:23:44 +08:00
my $ data = $ cka_value ;
$ cka_value = "" ;
2020-04-22 14:13:23 +08:00
if ( ! length ( $ data ) ) {
# if empty, skip
next ;
}
2013-04-04 06:02:49 +08:00
my $ encoded = MIME::Base64:: encode_base64 ( $ data , '' ) ;
$ encoded =~ s/(.{1,${opt_w}})/$1\n/g ;
2011-09-04 23:05:49 +08:00
my $ pem = "-----BEGIN CERTIFICATE-----\n"
2013-04-04 06:02:49 +08:00
. $ encoded
2011-09-04 23:05:49 +08:00
. "-----END CERTIFICATE-----\n" ;
print CRT "\n$caname\n" ;
2016-08-02 18:17:10 +08:00
print CRT @ precert if ( $ opt_m ) ;
2018-06-07 16:44:30 +08:00
my $ maxStringLength = length ( decode ( 'UTF-8' , $ caname , Encode:: FB_CROAK | Encode:: LEAVE_SRC ) ) ;
2014-05-08 17:37:45 +08:00
if ( $ opt_t ) {
foreach my $ key ( keys % trust_purposes_by_level ) {
my $ string = $ key . ": " . join ( ", " , @ { $ trust_purposes_by_level { $ key } } ) ;
$ maxStringLength = List::Util:: max ( length ( $ string ) , $ maxStringLength ) ;
print CRT $ string . "\n" ;
}
}
print CRT ( "=" x $ maxStringLength . "\n" ) ;
2011-09-04 23:05:49 +08:00
if ( ! $ opt_t ) {
print CRT $ pem ;
2013-04-10 06:20:37 +08:00
} else {
2014-05-08 17:37:45 +08:00
my $ pipe = "" ;
foreach my $ hash ( @ included_signature_algorithms ) {
$ pipe = "|$openssl x509 -" . $ hash . " -fingerprint -noout -inform PEM" ;
if ( ! $ stdout ) {
$ pipe . = " >> $crt.~" ;
close ( CRT ) or die "Couldn't close $crt.~: $!" ;
}
open ( TMP , $ pipe ) or die "Couldn't open openssl pipe: $!" ;
print TMP $ pem ;
close ( TMP ) or die "Couldn't close openssl pipe: $!" ;
if ( ! $ stdout ) {
open ( CRT , ">>$crt.~" ) or die "Couldn't open $crt.~: $!" ;
}
}
$ pipe = "|$openssl x509 -text -inform PEM" ;
2013-04-10 06:20:37 +08:00
if ( ! $ stdout ) {
$ pipe . = " >> $crt.~" ;
close ( CRT ) or die "Couldn't close $crt.~: $!" ;
}
open ( TMP , $ pipe ) or die "Couldn't open openssl pipe: $!" ;
print TMP $ pem ;
close ( TMP ) or die "Couldn't close openssl pipe: $!" ;
if ( ! $ stdout ) {
open ( CRT , ">>$crt.~" ) or die "Couldn't open $crt.~: $!" ;
}
2011-09-04 23:05:49 +08:00
}
2015-03-11 21:34:59 +08:00
report "Parsing: $caname" if ( $ opt_v ) ;
2011-09-04 23:05:49 +08:00
$ certnum + + ;
2012-09-05 05:21:15 +08:00
$ start_of_cert = 0 ;
2008-02-11 23:00:00 +08:00
}
2016-08-02 18:17:10 +08:00
undef @ precert ;
2008-02-08 09:08:25 +08:00
}
2016-08-02 18:17:10 +08:00
2008-02-08 09:08:25 +08:00
}
2013-01-06 06:29:04 +08: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" ;
}
2016-09-15 14:26:56 +08:00
if ( $ opt_u && - e $ txt && ! unlink ( $ txt ) ) {
report "Failed to remove $txt: $!\n" ;
}
2015-03-11 21:34:59 +08:00
report "Done ($certnum CA certs processed, $skipnum skipped)." ;