From 520616e8405c6be70708fbac3f3591d6bdadf4ae Mon Sep 17 00:00:00 2001
From: watson81 <patrick@patrickwatson.org>
Date: Fri, 28 Feb 2014 11:16:48 -0500
Subject: [PATCH] Added the ability to select certificates by trust purpose and
 level; added SHA1 and SHA256 hashes when output in full text mode

---
 docs/mk-ca-bundle.1 |  20 ++++++--
 lib/mk-ca-bundle.pl | 140 +++++++++++++++++++++++++++++++++++++++++++++++-----
 2 files changed, 144 insertions(+), 16 deletions(-)

diff --git a/docs/mk-ca-bundle.1 b/docs/mk-ca-bundle.1
index 1c43c1a..bd9dfe5 100644
--- a/docs/mk-ca-bundle.1
+++ b/docs/mk-ca-bundle.1
@@ -24,13 +24,14 @@
 .SH NAME
 mk-ca-bundle \- convert mozilla's certdata.txt to PEM format
 .SH SYNOPSIS
-mk-ca-bundle [bilnqtuv]
+mk-ca-bundle [bilnqtuvyz]
 .I [outputfile]
 .SH DESCRIPTION
 The mk-ca-bundle tool downloads the certdata.txt file from Mozilla's source
-tree over HTTP, 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 file.
+tree over HTTP, then parses certdata.txt and extracts certificates
+into PEM format.  By default, only CA root certificates trusted to issue SSL
+server authentication certificates are extracted. These are then processed with
+the OpenSSL commandline tool to produce the final ca-bundle file.
 
 The default \fIoutputfile\fP name is \fBca-bundle.crt\fP. By setting it to '-'
 (a single dash) you will get the output sent to STDOUT instead of a file.
@@ -62,6 +63,17 @@ include plain text listing of certificates
 unlink (remove) certdata.txt after processing
 .IP -v
 be verbose and print out processed CAs
+.IP -y [purposes]
+comma separated list of Mozilla trust purposes to include in output. Valid
+purposes are: ALL, DIGITAL_SIGNATURE, NON_REPUDIATION, KEY_ENCIPHERMENT,
+DATA_ENCIPHERMENT, KEY_AGREEMENT, KEY_CERT_SIGN, CRL_SIGN,
+SERVER_AUTH (default), CLIENT_AUTH, CODE_SIGNING, EMAIL_PROTECTION,
+IPSEC_END_SYSTEM, IPSEC_TUNNEL, IPSEC_USER, TIME_STAMPING, STEP_UP_APPROVED
+(Added in version 1.21, Perl version only)
+.IP -z [levels]
+comma separated list of Mozilla trust levels to include in output. Valid trust
+levels are: ALL, TRUSTED_DELEGATOR (default), NOT_TRUSTED, MUST_VERIFY_TRUST, TRUSTED
+(Added in version 1.21, Perl version only)
 .SH EXIT STATUS
 Returns 0 on success. Returns 1 if it fails to download data.
 .SH CERTDATA FORMAT
diff --git a/lib/mk-ca-bundle.pl b/lib/mk-ca-bundle.pl
index 4b78ff1..ee28d92 100755
--- a/lib/mk-ca-bundle.pl
+++ b/lib/mk-ca-bundle.pl
@@ -34,7 +34,9 @@ use Getopt::Std;
 use MIME::Base64;
 use LWP::UserAgent;
 use strict;
-use 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);
+use 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 $opt_y $opt_z);
+use List::Util;
+use Text::Wrap;
 
 my %urls = (
   'nss' =>
@@ -56,13 +58,43 @@ $opt_d = 'release';
 # If the OpenSSL commandline is not in search path you can configure it here!
 my $openssl = 'openssl';
 
-my $version = '1.20';
+my $version = '1.21';
 
 $opt_w = 76; # default base64 encoded lines length
 
+# default cert types to include in the output (default is to include CAs which may issue SSL server certs)
+my $opt_y_def = $opt_y = "SERVER_AUTH";
+my $opt_z_def = $opt_z = "TRUSTED_DELEGATOR";
+
+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).
+    );
+
 $0 =~ s@.*(/|\\)@@;
 $Getopt::Std::STANDARD_HELP_VERSION = 1;
-getopts('bd:fhilnqtuvw:');
+getopts('bd:fhilnqtuvw:y:z:');
 
 if(!defined($opt_d)) {
     # to make plain "-d" use not cause warnings, and actually still work
@@ -102,7 +134,7 @@ sub WARNING_MESSAGE() {
 }
 
 sub HELP_MESSAGE() {
-  print "Usage:\t${0} [-b] [-d<certdata>] [-f] [-i] [-l] [-n] [-q] [-t] [-u] [-v] [-w<l>] [<outputfile>]\n";
+  print "Usage:\t${0} [-b] [-d<certdata>] [-f] [-i] [-l] [-n] [-q] [-t] [-u] [-v] [-w<l>] [-y<p>] [-z<l>] [<outputfile>]\n";
   print "\t-b\tbackup an existing version of ca-bundle.crt\n";
   print "\t-d\tspecify Mozilla tree to pull certdata.txt or custom URL\n";
   print "\t\t  Valid names are:\n";
@@ -116,6 +148,12 @@ sub HELP_MESSAGE() {
   print "\t-u\tunlink (remove) certdata.txt after processing\n";
   print "\t-v\tbe verbose and print out processed CAs\n";
   print "\t-w <l>\twrap base64 output lines after <l> chars (default: ${opt_w})\n";
+  print wrap("\t","\t\t", "-y <p>\tcomma separated list of Mozilla trust purposes to include in output. (default: ${opt_y_def})"), "\n";
+  print "\t\t  Valid purposes are:\n";
+  print wrap("\t\t    ","\t\t    ", join( ", ", "ALL", @valid_mozilla_trust_purposes ) ), "\n";
+  print wrap("\t","\t\t", "-z <l>\tcomma separated list of Mozilla trust levels to include in output. (default: ${opt_z_def})"), "\n";
+  print "\t\t  Valid levels are:\n";
+  print wrap("\t\t    ","\t\t    ", join( ", ", "ALL", @valid_mozilla_trust_levels ) ), "\n";
   exit;
 }
 
@@ -126,6 +164,54 @@ sub VERSION_MESSAGE() {
 WARNING_MESSAGE() unless ($opt_q || $url =~ m/^(ht|f)tps:/i );
 HELP_MESSAGE() if ($opt_h);
 
+sub IS_IN_LIST($@) {
+  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
+sub PARSE_PARAM_TRUST($$@) {
+  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"
+  my @invalid = grep { !IS_IN_LIST($_,"ALL",@valid_values) } @values;
+
+  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();
+  }
+  
+  @values = @valid_values if ( IS_IN_LIST("ALL",@values) );
+  
+  return @values;
+}
+
+my @included_mozilla_trust_purposes = PARSE_PARAM_TRUST( "trust purpose", $opt_y, @valid_mozilla_trust_purposes );
+my @included_mozilla_trust_levels = PARSE_PARAM_TRUST( "trust level", $opt_z, @valid_mozilla_trust_levels );
+
+sub SHOULD_OUTPUT_CERT(%) {
+  my %trust_purposes_by_level = @_;
+  
+  foreach my $level (@included_mozilla_trust_levels) {
+    # for each level we want to output, see if any of our desired purposes are included
+    return 1 if ( defined( List::Util::first { IS_IN_LIST( $_, @included_mozilla_trust_purposes ) } @{$trust_purposes_by_level{$level}} ) );
+  }
+  
+  return 0;
+}
+
 my $crt = $ARGV[0] || 'ca-bundle.crt';
 (my $txt = $url) =~ s@(.*/|\?.*)@@g;
 
@@ -209,7 +295,7 @@ while (<TXT>) {
   if ($start_of_cert && /^CKA_LABEL UTF8 \"(.*)\"/) {
     $caname = $1;
   }
-  my $untrusted = 1;
+  my %trust_purposes_by_level;
   if ($start_of_cert && /^CKA_VALUE MULTILINE_OCTAL/) {
     my $data;
     while (<TXT>) {
@@ -226,14 +312,21 @@ while (<TXT>) {
       last if (/^CKA_CLASS CK_OBJECT_CLASS CKO_NSS_TRUST/);
       chomp;
     }
-    # now scan the trust part for untrusted certs
+    # now scan the trust part to determine how we should trust this cert
     while (<TXT>) {
       last if (/^#/);
-      if (/^CKA_TRUST_SERVER_AUTH\s+CK_TRUST\s+CKT_NSS_TRUSTED_DELEGATOR$/) {
-          $untrusted = 0;
+      if (/^CKA_TRUST_([A-Z_]+)\s+CK_TRUST\s+CKT_NSS_([A-Z_]+)\s*$/) {
+        if ( !IS_IN_LIST($1,@valid_mozilla_trust_purposes) ) {
+          print STDERR "Warning: Unrecognized trust purpose for cert: $caname. Trust purpose: $1. Trust Level: $2\n" if (!$opt_q);
+        } elsif ( !IS_IN_LIST($2,@valid_mozilla_trust_levels) ) {
+          print STDERR "Warning: Unrecognized trust level for cert: $caname. Trust purpose: $1. Trust Level: $2\n" if (!$opt_q);
+        } else {
+            push @{$trust_purposes_by_level{$2}}, $1;
+        }
       }
     }
-    if ($untrusted) {
+
+    if ( !SHOULD_OUTPUT_CERT(%trust_purposes_by_level) ) {
       $skipnum ++;
     } else {
       my $encoded = MIME::Base64::encode_base64($data, '');
@@ -242,11 +335,34 @@ while (<TXT>) {
               . $encoded
               . "-----END CERTIFICATE-----\n";
       print CRT "\n$caname\n";
-      print CRT ("=" x length($caname) . "\n");
+
+      my $maxStringLength = length($caname);
+      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");
       if (!$opt_t) {
         print CRT $pem;
       } else {
-        my $pipe = "|$openssl x509 -md5 -fingerprint -text -inform PEM";
+        my @hashes = ('md5', 'sha1', 'sha256');
+        my $pipe = "";
+        foreach my $hash (@hashes) {
+            $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";
         if (!$stdout) {
           $pipe .= " >> $crt.~";
           close(CRT) or die "Couldn't close $crt.~: $!";
@@ -279,7 +395,7 @@ unless( $stdout ) {
     rename "$crt.~", $crt or die "Failed to rename $crt.~ to $crt: $!\n";
 }
 unlink $txt if ($opt_u);
-print STDERR "Done ($certnum CA certs processed, $skipnum untrusted skipped).\n" if (!$opt_q);
+print STDERR "Done ($certnum CA certs processed, $skipnum skipped).\n" if (!$opt_q);
 
 exit;
 
-- 
1.8.3.msysgit.0

