3 ## MAca-bundle.pl -- Regenerate ca-root-nss.crt from the Mozilla certdata.txt
5 ## Rewritten in September 2011 by Matthias Andree to heed untrust
8 ## Copyright (c) 2011, 2013 Matthias Andree <mandree@FreeBSD.org>
9 ## All rights reserved.
10 ## Copyright (c) 2018, Allan Jude <allanjude@FreeBSD.org>
12 ## Redistribution and use in source and binary forms, with or without
13 ## modification, are permitted provided that the following conditions are
16 ## * Redistributions of source code must retain the above copyright
17 ## notice, this list of conditions and the following disclaimer.
19 ## * Redistributions in binary form must reproduce the above copyright
20 ## notice, this list of conditions and the following disclaimer in the
21 ## documentation and/or other materials provided with the distribution.
23 ## THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
24 ## "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
25 ## LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
26 ## FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
27 ## COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
28 ## INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
29 ## BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
30 ## LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
31 ## CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32 ## LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
33 ## ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
34 ## POSSIBILITY OF SUCH DAMAGE.
41 my $VERSION = '$FreeBSD$';
42 my $generated = '@' . 'generated';
52 if defined $ENV{'WITH_DEBUG'}
53 and $ENV{'WITH_DEBUG'} !~ m/(?i)^(no|0|false|)$/;
57 "infile:s" => \$infile,
58 "outputdir:s" => \$outputdir)
59 or die("Error in command line arguments\n$0 [-d] [-i input-file] [-o output-dir]\n");
62 open($inputfh, "<", $infile) or die "Failed to open $infile";
71 print $dstfile <<EOFH;
75 ## This is a single X.509 certificate for a public Certificate
76 ## Authority (CA). It was automatically extracted from Mozilla's
77 ## root CA list (the file `certdata.txt' in security/nss).
88 ## ca-root-nss.crt -- Bundle of CA Root Certificates
90 ## This is a bundle of X.509 certificates of public Certificate
91 ## Authorities (CA). These were automatically extracted from Mozilla's
92 ## root CA list (the file `certdata.txt').
105 my ($fh, $label, $certdata) = @_;
106 return unless $certdata;
107 open(OUT, "|openssl x509 -text -inform DER -fingerprint")
108 or die "could not pipe to openssl x509";
110 close(OUT) or die "openssl x509 failed with exit code $?";
120 my (undef,@oct) = split /\\/;
121 my @bin = map(chr(oct), @oct);
122 $data .= join('', @bin);
140 if (/^CKA_LABEL UTF8 "([^"]+)"/) {
144 if (/^CKA_VALUE MULTILINE_OCTAL/) {
145 $certdata = graboct($ifh);
148 if (/^CKA_SERIAL_NUMBER MULTILINE_OCTAL/) {
149 $serial = graboct($ifh);
152 return ($serial, $cka_label, $certdata);
166 if (/^CKA_LABEL UTF8 "([^"]+)"/) {
170 if (/^CKA_SERIAL_NUMBER MULTILINE_OCTAL/) {
171 $serial = graboct($ifh);
174 if (/^CKA_TRUST_(SERVER_AUTH|EMAIL_PROTECTION|CODE_SIGNING) CK_TRUST (\S+)$/)
176 if ($2 eq 'CKT_NSS_NOT_TRUSTED') {
178 } elsif ($2 eq 'CKT_NSS_TRUSTED_DELEGATOR') {
180 } elsif ($2 ne 'CKT_NSS_MUST_VERIFY_TRUST') {
181 confess "Unknown trust setting on line $.:\n"
183 . "Script must be updated:";
188 if (!$maytrust && !$distrust && $debug) {
189 print STDERR "line $.: no explicit trust/distrust found for $cka_label\n";
192 my $trust = ($maytrust and not $distrust);
193 return ($serial, $cka_label, $trust);
197 print_header(*STDOUT, "");
201 if (/^CKA_CLASS CK_OBJECT_CLASS CKO_CERTIFICATE/) {
202 my ($serial, $label, $certdata) = grabcert($inputfh);
203 if (defined $certs{$label."\0".$serial}) {
204 warn "Certificate $label duplicated!\n";
206 $certs{$label."\0".$serial} = $certdata;
207 # We store the label in a separate hash because truncating the key
208 # with \0 was causing garbage data after the end of the text.
209 $labels{$label."\0".$serial} = $label;
210 } elsif (/^CKA_CLASS CK_OBJECT_CLASS CKO_NSS_TRUST/) {
211 my ($serial, $label, $trust) = grabtrust($inputfh);
212 if (defined $trusts{$label."\0".$serial}) {
213 warn "Trust for $label duplicated!\n";
215 $trusts{$label."\0".$serial} = $trust;
216 $labels{$label."\0".$serial} = $label;
217 } elsif (/^CVS_ID.*Revision: ([^ ]*).*/) {
218 print "## Source: \"certdata.txt\" CVS revision $1\n##\n\n";
222 sub label_to_filename(@) {
224 map { s/\0.*//; s/[^[:alnum:]\-]/_/g; $_ = "$_.pem"; } @res;
225 return wantarray ? @res : $res[0];
228 # weed out untrusted certificates
230 foreach my $it (keys %trusts) {
232 if (!exists($certs{$it})) {
233 warn "Found trust for nonexistent certificate $labels{$it}\n" if $debug;
236 warn "Skipping untrusted $labels{$it}\n" if $debug;
243 print "## Untrusted certificates omitted from this bundle: $untrusted\n\n";
245 print STDERR "## Untrusted certificates omitted from this bundle: $untrusted\n";
248 foreach my $it (sort {uc($a) cmp uc($b)} keys %certs) {
251 if (!exists($trusts{$it})) {
252 die "Found certificate without trust block,\naborting";
255 $filename = label_to_filename($labels{$it});
256 open($fh, ">", "$outputdir/$filename") or die "Failed to open certificate $filename";
257 print_header($fh, $labels{$it});
259 printcert($fh, $labels{$it}, $certs{$it});
261 close($fh) or die "Unable to close: $filename";
266 print STDERR "Trusting $certcount: $labels{$it}\n" if $debug;
269 if ($certcount < 25) {
270 die "Certificate count of $certcount is implausibly low.\nAbort";
274 print "## Number of certificates: $certcount\n";
275 print "## End of file.\n";
277 print STDERR "## Number of certificates: $certcount\n";