113498266Sopenharmony_ci#!/usr/bin/env perl
213498266Sopenharmony_ci# ***************************************************************************
313498266Sopenharmony_ci# *                                  _   _ ____  _
413498266Sopenharmony_ci# *  Project                     ___| | | |  _ \| |
513498266Sopenharmony_ci# *                             / __| | | | |_) | |
613498266Sopenharmony_ci# *                            | (__| |_| |  _ <| |___
713498266Sopenharmony_ci# *                             \___|\___/|_| \_\_____|
813498266Sopenharmony_ci# *
913498266Sopenharmony_ci# * Copyright (C) Daniel Stenberg, <daniel@haxx.se>, et al.
1013498266Sopenharmony_ci# *
1113498266Sopenharmony_ci# * This software is licensed as described in the file COPYING, which
1213498266Sopenharmony_ci# * you should have received as part of this distribution. The terms
1313498266Sopenharmony_ci# * are also available at https://curl.se/docs/copyright.html.
1413498266Sopenharmony_ci# *
1513498266Sopenharmony_ci# * You may opt to use, copy, modify, merge, publish, distribute and/or sell
1613498266Sopenharmony_ci# * copies of the Software, and permit persons to whom the Software is
1713498266Sopenharmony_ci# * furnished to do so, under the terms of the COPYING file.
1813498266Sopenharmony_ci# *
1913498266Sopenharmony_ci# * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
2013498266Sopenharmony_ci# * KIND, either express or implied.
2113498266Sopenharmony_ci# *
2213498266Sopenharmony_ci# * SPDX-License-Identifier: curl
2313498266Sopenharmony_ci# *
2413498266Sopenharmony_ci# ***************************************************************************
2513498266Sopenharmony_ci# This Perl script creates a fresh ca-bundle.crt file for use with libcurl.
2613498266Sopenharmony_ci# It downloads certdata.txt from Mozilla's source tree (see URL below),
2713498266Sopenharmony_ci# then parses certdata.txt and extracts CA Root Certificates into PEM format.
2813498266Sopenharmony_ci# These are then processed with the OpenSSL commandline tool to produce the
2913498266Sopenharmony_ci# final ca-bundle.crt file.
3013498266Sopenharmony_ci# The script is based on the parse-certs script written by Roland Krikava.
3113498266Sopenharmony_ci# This Perl script works on almost any platform since its only external
3213498266Sopenharmony_ci# dependency is the OpenSSL commandline tool for optional text listing.
3313498266Sopenharmony_ci# Hacked by Guenter Knauf.
3413498266Sopenharmony_ci#
3513498266Sopenharmony_ciuse Encode;
3613498266Sopenharmony_ciuse Getopt::Std;
3713498266Sopenharmony_ciuse MIME::Base64;
3813498266Sopenharmony_ciuse strict;
3913498266Sopenharmony_ciuse warnings;
4013498266Sopenharmony_ciuse 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);
4113498266Sopenharmony_ciuse List::Util;
4213498266Sopenharmony_ciuse Text::Wrap;
4313498266Sopenharmony_ciuse Time::Local;
4413498266Sopenharmony_cimy $MOD_SHA = "Digest::SHA";
4513498266Sopenharmony_cieval "require $MOD_SHA";
4613498266Sopenharmony_ciif ($@) {
4713498266Sopenharmony_ci  $MOD_SHA = "Digest::SHA::PurePerl";
4813498266Sopenharmony_ci  eval "require $MOD_SHA";
4913498266Sopenharmony_ci}
5013498266Sopenharmony_cieval "require LWP::UserAgent";
5113498266Sopenharmony_ci
5213498266Sopenharmony_cimy %urls = (
5313498266Sopenharmony_ci  'nss' =>
5413498266Sopenharmony_ci    'https://hg.mozilla.org/projects/nss/raw-file/default/lib/ckfw/builtins/certdata.txt',
5513498266Sopenharmony_ci  'central' =>
5613498266Sopenharmony_ci    'https://hg.mozilla.org/mozilla-central/raw-file/default/security/nss/lib/ckfw/builtins/certdata.txt',
5713498266Sopenharmony_ci  'beta' =>
5813498266Sopenharmony_ci    'https://hg.mozilla.org/releases/mozilla-beta/raw-file/default/security/nss/lib/ckfw/builtins/certdata.txt',
5913498266Sopenharmony_ci  'release' =>
6013498266Sopenharmony_ci    'https://hg.mozilla.org/releases/mozilla-release/raw-file/default/security/nss/lib/ckfw/builtins/certdata.txt',
6113498266Sopenharmony_ci);
6213498266Sopenharmony_ci
6313498266Sopenharmony_ci$opt_d = 'release';
6413498266Sopenharmony_ci
6513498266Sopenharmony_ci# If the OpenSSL commandline is not in search path you can configure it here!
6613498266Sopenharmony_cimy $openssl = 'openssl';
6713498266Sopenharmony_ci
6813498266Sopenharmony_cimy $version = '1.29';
6913498266Sopenharmony_ci
7013498266Sopenharmony_ci$opt_w = 76; # default base64 encoded lines length
7113498266Sopenharmony_ci
7213498266Sopenharmony_ci# default cert types to include in the output (default is to include CAs which
7313498266Sopenharmony_ci# may issue SSL server certs)
7413498266Sopenharmony_cimy $default_mozilla_trust_purposes = "SERVER_AUTH";
7513498266Sopenharmony_cimy $default_mozilla_trust_levels = "TRUSTED_DELEGATOR";
7613498266Sopenharmony_ci$opt_p = $default_mozilla_trust_purposes . ":" . $default_mozilla_trust_levels;
7713498266Sopenharmony_ci
7813498266Sopenharmony_cimy @valid_mozilla_trust_purposes = (
7913498266Sopenharmony_ci  "DIGITAL_SIGNATURE",
8013498266Sopenharmony_ci  "NON_REPUDIATION",
8113498266Sopenharmony_ci  "KEY_ENCIPHERMENT",
8213498266Sopenharmony_ci  "DATA_ENCIPHERMENT",
8313498266Sopenharmony_ci  "KEY_AGREEMENT",
8413498266Sopenharmony_ci  "KEY_CERT_SIGN",
8513498266Sopenharmony_ci  "CRL_SIGN",
8613498266Sopenharmony_ci  "SERVER_AUTH",
8713498266Sopenharmony_ci  "CLIENT_AUTH",
8813498266Sopenharmony_ci  "CODE_SIGNING",
8913498266Sopenharmony_ci  "EMAIL_PROTECTION",
9013498266Sopenharmony_ci  "IPSEC_END_SYSTEM",
9113498266Sopenharmony_ci  "IPSEC_TUNNEL",
9213498266Sopenharmony_ci  "IPSEC_USER",
9313498266Sopenharmony_ci  "TIME_STAMPING",
9413498266Sopenharmony_ci  "STEP_UP_APPROVED"
9513498266Sopenharmony_ci);
9613498266Sopenharmony_ci
9713498266Sopenharmony_cimy @valid_mozilla_trust_levels = (
9813498266Sopenharmony_ci  "TRUSTED_DELEGATOR",    # CAs
9913498266Sopenharmony_ci  "NOT_TRUSTED",          # Don't trust these certs.
10013498266Sopenharmony_ci  "MUST_VERIFY_TRUST",    # This explicitly tells us that it ISN'T a CA but is
10113498266Sopenharmony_ci                          # otherwise ok. In other words, this should tell the
10213498266Sopenharmony_ci                          # app to ignore any other sources that claim this is
10313498266Sopenharmony_ci                          # a CA.
10413498266Sopenharmony_ci  "TRUSTED"               # This cert is trusted, but only for itself and not
10513498266Sopenharmony_ci                          # for delegates (i.e. it is not a CA).
10613498266Sopenharmony_ci);
10713498266Sopenharmony_ci
10813498266Sopenharmony_cimy $default_signature_algorithms = $opt_s = "MD5";
10913498266Sopenharmony_ci
11013498266Sopenharmony_cimy @valid_signature_algorithms = (
11113498266Sopenharmony_ci  "MD5",
11213498266Sopenharmony_ci  "SHA1",
11313498266Sopenharmony_ci  "SHA256",
11413498266Sopenharmony_ci  "SHA384",
11513498266Sopenharmony_ci  "SHA512"
11613498266Sopenharmony_ci);
11713498266Sopenharmony_ci
11813498266Sopenharmony_ci$0 =~ s@.*(/|\\)@@;
11913498266Sopenharmony_ci$Getopt::Std::STANDARD_HELP_VERSION = 1;
12013498266Sopenharmony_cigetopts('bd:fhiklmnp:qs:tuvw:');
12113498266Sopenharmony_ci
12213498266Sopenharmony_ciif(!defined($opt_d)) {
12313498266Sopenharmony_ci    # to make plain "-d" use not cause warnings, and actually still work
12413498266Sopenharmony_ci    $opt_d = 'release';
12513498266Sopenharmony_ci}
12613498266Sopenharmony_ci
12713498266Sopenharmony_ci# Use predefined URL or else custom URL specified on command line.
12813498266Sopenharmony_cimy $url;
12913498266Sopenharmony_ciif(defined($urls{$opt_d})) {
13013498266Sopenharmony_ci  $url = $urls{$opt_d};
13113498266Sopenharmony_ci  if(!$opt_k && $url !~ /^https:\/\//i) {
13213498266Sopenharmony_ci    die "The URL for '$opt_d' is not HTTPS. Use -k to override (insecure).\n";
13313498266Sopenharmony_ci  }
13413498266Sopenharmony_ci}
13513498266Sopenharmony_cielse {
13613498266Sopenharmony_ci  $url = $opt_d;
13713498266Sopenharmony_ci}
13813498266Sopenharmony_ci
13913498266Sopenharmony_cimy $curl = `curl -V`;
14013498266Sopenharmony_ci
14113498266Sopenharmony_ciif ($opt_i) {
14213498266Sopenharmony_ci  print ("=" x 78 . "\n");
14313498266Sopenharmony_ci  print "Script Version                   : $version\n";
14413498266Sopenharmony_ci  print "Perl Version                     : $]\n";
14513498266Sopenharmony_ci  print "Operating System Name            : $^O\n";
14613498266Sopenharmony_ci  print "Getopt::Std.pm Version           : ${Getopt::Std::VERSION}\n";
14713498266Sopenharmony_ci  print "Encode::Encoding.pm Version      : ${Encode::Encoding::VERSION}\n";
14813498266Sopenharmony_ci  print "MIME::Base64.pm Version          : ${MIME::Base64::VERSION}\n";
14913498266Sopenharmony_ci  print "LWP::UserAgent.pm Version        : ${LWP::UserAgent::VERSION}\n" if($LWP::UserAgent::VERSION);
15013498266Sopenharmony_ci  print "LWP.pm Version                   : ${LWP::VERSION}\n" if($LWP::VERSION);
15113498266Sopenharmony_ci  print "Digest::SHA.pm Version           : ${Digest::SHA::VERSION}\n" if ($Digest::SHA::VERSION);
15213498266Sopenharmony_ci  print "Digest::SHA::PurePerl.pm Version : ${Digest::SHA::PurePerl::VERSION}\n" if ($Digest::SHA::PurePerl::VERSION);
15313498266Sopenharmony_ci  print ("=" x 78 . "\n");
15413498266Sopenharmony_ci}
15513498266Sopenharmony_ci
15613498266Sopenharmony_cisub warning_message() {
15713498266Sopenharmony_ci  if ( $opt_d =~ m/^risk$/i ) { # Long Form Warning and Exit
15813498266Sopenharmony_ci    print "Warning: Use of this script may pose some risk:\n";
15913498266Sopenharmony_ci    print "\n";
16013498266Sopenharmony_ci    print "  1) If you use HTTP URLs they are subject to a man in the middle attack\n";
16113498266Sopenharmony_ci    print "  2) Default to 'release', but more recent updates may be found in other trees\n";
16213498266Sopenharmony_ci    print "  3) certdata.txt file format may change, lag time to update this script\n";
16313498266Sopenharmony_ci    print "  4) Generally unwise to blindly trust CAs without manual review & verification\n";
16413498266Sopenharmony_ci    print "  5) Mozilla apps use additional security checks aren't represented in certdata\n";
16513498266Sopenharmony_ci    print "  6) Use of this script will make a security engineer grind his teeth and\n";
16613498266Sopenharmony_ci    print "     swear at you.  ;)\n";
16713498266Sopenharmony_ci    exit;
16813498266Sopenharmony_ci  } else { # Short Form Warning
16913498266Sopenharmony_ci    print "Warning: Use of this script may pose some risk, -d risk for more details.\n";
17013498266Sopenharmony_ci  }
17113498266Sopenharmony_ci}
17213498266Sopenharmony_ci
17313498266Sopenharmony_cisub HELP_MESSAGE() {
17413498266Sopenharmony_ci  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";
17513498266Sopenharmony_ci  print "\t-b\tbackup an existing version of ca-bundle.crt\n";
17613498266Sopenharmony_ci  print "\t-d\tspecify Mozilla tree to pull certdata.txt or custom URL\n";
17713498266Sopenharmony_ci  print "\t\t  Valid names are:\n";
17813498266Sopenharmony_ci  print "\t\t    ", join( ", ", map { ( $_ =~ m/$opt_d/ ) ? "$_ (default)" : "$_" } sort keys %urls ), "\n";
17913498266Sopenharmony_ci  print "\t-f\tforce rebuild even if certdata.txt is current\n";
18013498266Sopenharmony_ci  print "\t-i\tprint version info about used modules\n";
18113498266Sopenharmony_ci  print "\t-k\tallow URLs other than HTTPS, enable HTTP fallback (insecure)\n";
18213498266Sopenharmony_ci  print "\t-l\tprint license info about certdata.txt\n";
18313498266Sopenharmony_ci  print "\t-m\tinclude meta data in output\n";
18413498266Sopenharmony_ci  print "\t-n\tno download of certdata.txt (to use existing)\n";
18513498266Sopenharmony_ci  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";
18613498266Sopenharmony_ci  print "\t\t  Valid purposes are:\n";
18713498266Sopenharmony_ci  print wrap("\t\t    ","\t\t    ", join( ", ", "ALL", @valid_mozilla_trust_purposes ) ), "\n";
18813498266Sopenharmony_ci  print "\t\t  Valid levels are:\n";
18913498266Sopenharmony_ci  print wrap("\t\t    ","\t\t    ", join( ", ", "ALL", @valid_mozilla_trust_levels ) ), "\n";
19013498266Sopenharmony_ci  print "\t-q\tbe really quiet (no progress output at all)\n";
19113498266Sopenharmony_ci  print wrap("\t","\t\t", "-s\tcomma separated list of certificate signatures/hashes to output in plain text mode. (default: $default_signature_algorithms)\n");
19213498266Sopenharmony_ci  print "\t\t  Valid signature algorithms are:\n";
19313498266Sopenharmony_ci  print wrap("\t\t    ","\t\t    ", join( ", ", "ALL", @valid_signature_algorithms ) ), "\n";
19413498266Sopenharmony_ci  print "\t-t\tinclude plain text listing of certificates\n";
19513498266Sopenharmony_ci  print "\t-u\tunlink (remove) certdata.txt after processing\n";
19613498266Sopenharmony_ci  print "\t-v\tbe verbose and print out processed CAs\n";
19713498266Sopenharmony_ci  print "\t-w <l>\twrap base64 output lines after <l> chars (default: ${opt_w})\n";
19813498266Sopenharmony_ci  exit;
19913498266Sopenharmony_ci}
20013498266Sopenharmony_ci
20113498266Sopenharmony_cisub VERSION_MESSAGE() {
20213498266Sopenharmony_ci  print "${0} version ${version} running Perl ${]} on ${^O}\n";
20313498266Sopenharmony_ci}
20413498266Sopenharmony_ci
20513498266Sopenharmony_ciwarning_message() unless ($opt_q || $url =~ m/^(ht|f)tps:/i );
20613498266Sopenharmony_ciHELP_MESSAGE() if ($opt_h);
20713498266Sopenharmony_ci
20813498266Sopenharmony_cisub report($@) {
20913498266Sopenharmony_ci  my $output = shift;
21013498266Sopenharmony_ci
21113498266Sopenharmony_ci  print STDERR $output . "\n" unless $opt_q;
21213498266Sopenharmony_ci}
21313498266Sopenharmony_ci
21413498266Sopenharmony_cisub is_in_list($@) {
21513498266Sopenharmony_ci  my $target = shift;
21613498266Sopenharmony_ci
21713498266Sopenharmony_ci  return defined(List::Util::first { $target eq $_ } @_);
21813498266Sopenharmony_ci}
21913498266Sopenharmony_ci
22013498266Sopenharmony_ci# Parses $param_string as a case insensitive comma separated list with optional
22113498266Sopenharmony_ci# whitespace validates that only allowed parameters are supplied
22213498266Sopenharmony_cisub parse_csv_param($$@) {
22313498266Sopenharmony_ci  my $description = shift;
22413498266Sopenharmony_ci  my $param_string = shift;
22513498266Sopenharmony_ci  my @valid_values = @_;
22613498266Sopenharmony_ci
22713498266Sopenharmony_ci  my @values = map {
22813498266Sopenharmony_ci    s/^\s+//;  # strip leading spaces
22913498266Sopenharmony_ci    s/\s+$//;  # strip trailing spaces
23013498266Sopenharmony_ci    uc $_      # return the modified string as upper case
23113498266Sopenharmony_ci  } split( ',', $param_string );
23213498266Sopenharmony_ci
23313498266Sopenharmony_ci  # Find all values which are not in the list of valid values or "ALL"
23413498266Sopenharmony_ci  my @invalid = grep { !is_in_list($_,"ALL",@valid_values) } @values;
23513498266Sopenharmony_ci
23613498266Sopenharmony_ci  if ( scalar(@invalid) > 0 ) {
23713498266Sopenharmony_ci    # Tell the user which parameters were invalid and print the standard help
23813498266Sopenharmony_ci    # message which will exit
23913498266Sopenharmony_ci    print "Error: Invalid ", $description, scalar(@invalid) == 1 ? ": " : "s: ", join( ", ", map { "\"$_\"" } @invalid ), "\n";
24013498266Sopenharmony_ci    HELP_MESSAGE();
24113498266Sopenharmony_ci  }
24213498266Sopenharmony_ci
24313498266Sopenharmony_ci  @values = @valid_values if ( is_in_list("ALL",@values) );
24413498266Sopenharmony_ci
24513498266Sopenharmony_ci  return @values;
24613498266Sopenharmony_ci}
24713498266Sopenharmony_ci
24813498266Sopenharmony_cisub sha256 {
24913498266Sopenharmony_ci  my $result;
25013498266Sopenharmony_ci  if ($Digest::SHA::VERSION || $Digest::SHA::PurePerl::VERSION) {
25113498266Sopenharmony_ci    open(FILE, $_[0]) or die "Can't open '$_[0]': $!";
25213498266Sopenharmony_ci    binmode(FILE);
25313498266Sopenharmony_ci    $result = $MOD_SHA->new(256)->addfile(*FILE)->hexdigest;
25413498266Sopenharmony_ci    close(FILE);
25513498266Sopenharmony_ci  } else {
25613498266Sopenharmony_ci    # Use OpenSSL command if Perl Digest::SHA modules not available
25713498266Sopenharmony_ci    $result = `"$openssl" dgst -r -sha256 "$_[0]"`;
25813498266Sopenharmony_ci    $result =~ s/^([0-9a-f]{64}) .+/$1/is;
25913498266Sopenharmony_ci  }
26013498266Sopenharmony_ci  return $result;
26113498266Sopenharmony_ci}
26213498266Sopenharmony_ci
26313498266Sopenharmony_ci
26413498266Sopenharmony_cisub oldhash {
26513498266Sopenharmony_ci  my $hash = "";
26613498266Sopenharmony_ci  open(C, "<$_[0]") || return 0;
26713498266Sopenharmony_ci  while(<C>) {
26813498266Sopenharmony_ci    chomp;
26913498266Sopenharmony_ci    if($_ =~ /^\#\# SHA256: (.*)/) {
27013498266Sopenharmony_ci      $hash = $1;
27113498266Sopenharmony_ci      last;
27213498266Sopenharmony_ci    }
27313498266Sopenharmony_ci  }
27413498266Sopenharmony_ci  close(C);
27513498266Sopenharmony_ci  return $hash;
27613498266Sopenharmony_ci}
27713498266Sopenharmony_ci
27813498266Sopenharmony_ciif ( $opt_p !~ m/:/ ) {
27913498266Sopenharmony_ci  print "Error: Mozilla trust identifier list must include both purposes and levels\n";
28013498266Sopenharmony_ci  HELP_MESSAGE();
28113498266Sopenharmony_ci}
28213498266Sopenharmony_ci
28313498266Sopenharmony_ci(my $included_mozilla_trust_purposes_string, my $included_mozilla_trust_levels_string) = split( ':', $opt_p );
28413498266Sopenharmony_cimy @included_mozilla_trust_purposes = parse_csv_param( "trust purpose", $included_mozilla_trust_purposes_string, @valid_mozilla_trust_purposes );
28513498266Sopenharmony_cimy @included_mozilla_trust_levels = parse_csv_param( "trust level", $included_mozilla_trust_levels_string, @valid_mozilla_trust_levels );
28613498266Sopenharmony_ci
28713498266Sopenharmony_cimy @included_signature_algorithms = parse_csv_param( "signature algorithm", $opt_s, @valid_signature_algorithms );
28813498266Sopenharmony_ci
28913498266Sopenharmony_cisub should_output_cert(%) {
29013498266Sopenharmony_ci  my %trust_purposes_by_level = @_;
29113498266Sopenharmony_ci
29213498266Sopenharmony_ci  foreach my $level (@included_mozilla_trust_levels) {
29313498266Sopenharmony_ci    # for each level we want to output, see if any of our desired purposes are
29413498266Sopenharmony_ci    # included
29513498266Sopenharmony_ci    return 1 if ( defined( List::Util::first { is_in_list( $_, @included_mozilla_trust_purposes ) } @{$trust_purposes_by_level{$level}} ) );
29613498266Sopenharmony_ci  }
29713498266Sopenharmony_ci
29813498266Sopenharmony_ci  return 0;
29913498266Sopenharmony_ci}
30013498266Sopenharmony_ci
30113498266Sopenharmony_cimy $crt = $ARGV[0] || 'ca-bundle.crt';
30213498266Sopenharmony_ci(my $txt = $url) =~ s@(.*/|\?.*)@@g;
30313498266Sopenharmony_ci
30413498266Sopenharmony_cimy $stdout = $crt eq '-';
30513498266Sopenharmony_cimy $resp;
30613498266Sopenharmony_cimy $fetched;
30713498266Sopenharmony_ci
30813498266Sopenharmony_cimy $oldhash = oldhash($crt);
30913498266Sopenharmony_ci
31013498266Sopenharmony_cireport "SHA256 of old file: $oldhash";
31113498266Sopenharmony_ci
31213498266Sopenharmony_ciif(!$opt_n) {
31313498266Sopenharmony_ci  report "Downloading $txt ...";
31413498266Sopenharmony_ci
31513498266Sopenharmony_ci  # If we have an HTTPS URL then use curl
31613498266Sopenharmony_ci  if($url =~ /^https:\/\//i) {
31713498266Sopenharmony_ci    if($curl) {
31813498266Sopenharmony_ci      if($curl =~ /^Protocols:.* https( |$)/m) {
31913498266Sopenharmony_ci        report "Get certdata with curl!";
32013498266Sopenharmony_ci        my $proto = !$opt_k ? "--proto =https" : "";
32113498266Sopenharmony_ci        my $quiet = $opt_q ? "-s" : "";
32213498266Sopenharmony_ci        my @out = `curl -w %{response_code} $proto $quiet -o "$txt" "$url"`;
32313498266Sopenharmony_ci        if(!$? && @out && $out[0] == 200) {
32413498266Sopenharmony_ci          $fetched = 1;
32513498266Sopenharmony_ci          report "Downloaded $txt";
32613498266Sopenharmony_ci        }
32713498266Sopenharmony_ci        else {
32813498266Sopenharmony_ci          report "Failed downloading via HTTPS with curl";
32913498266Sopenharmony_ci          if(-e $txt && !unlink($txt)) {
33013498266Sopenharmony_ci            report "Failed to remove '$txt': $!";
33113498266Sopenharmony_ci          }
33213498266Sopenharmony_ci        }
33313498266Sopenharmony_ci      }
33413498266Sopenharmony_ci      else {
33513498266Sopenharmony_ci        report "curl lacks https support";
33613498266Sopenharmony_ci      }
33713498266Sopenharmony_ci    }
33813498266Sopenharmony_ci    else {
33913498266Sopenharmony_ci      report "curl not found";
34013498266Sopenharmony_ci    }
34113498266Sopenharmony_ci  }
34213498266Sopenharmony_ci
34313498266Sopenharmony_ci  # If nothing was fetched then use LWP
34413498266Sopenharmony_ci  if(!$fetched) {
34513498266Sopenharmony_ci    if($url =~ /^https:\/\//i) {
34613498266Sopenharmony_ci      report "Falling back to HTTP";
34713498266Sopenharmony_ci      $url =~ s/^https:\/\//http:\/\//i;
34813498266Sopenharmony_ci    }
34913498266Sopenharmony_ci    if(!$opt_k) {
35013498266Sopenharmony_ci      report "URLs other than HTTPS are disabled by default, to enable use -k";
35113498266Sopenharmony_ci      exit 1;
35213498266Sopenharmony_ci    }
35313498266Sopenharmony_ci    report "Get certdata with LWP!";
35413498266Sopenharmony_ci    if(!defined(${LWP::UserAgent::VERSION})) {
35513498266Sopenharmony_ci      report "LWP is not available (LWP::UserAgent not found)";
35613498266Sopenharmony_ci      exit 1;
35713498266Sopenharmony_ci    }
35813498266Sopenharmony_ci    my $ua  = new LWP::UserAgent(agent => "$0/$version");
35913498266Sopenharmony_ci    $ua->env_proxy();
36013498266Sopenharmony_ci    $resp = $ua->mirror($url, $txt);
36113498266Sopenharmony_ci    if($resp && $resp->code eq '304') {
36213498266Sopenharmony_ci      report "Not modified";
36313498266Sopenharmony_ci      exit 0 if -e $crt && !$opt_f;
36413498266Sopenharmony_ci    }
36513498266Sopenharmony_ci    else {
36613498266Sopenharmony_ci      $fetched = 1;
36713498266Sopenharmony_ci      report "Downloaded $txt";
36813498266Sopenharmony_ci    }
36913498266Sopenharmony_ci    if(!$resp || $resp->code !~ /^(?:200|304)$/) {
37013498266Sopenharmony_ci      report "Unable to download latest data: "
37113498266Sopenharmony_ci        . ($resp? $resp->code . ' - ' . $resp->message : "LWP failed");
37213498266Sopenharmony_ci      exit 1 if -e $crt || ! -r $txt;
37313498266Sopenharmony_ci    }
37413498266Sopenharmony_ci  }
37513498266Sopenharmony_ci}
37613498266Sopenharmony_ci
37713498266Sopenharmony_cimy $filedate = $resp ? $resp->last_modified : (stat($txt))[9];
37813498266Sopenharmony_cimy $datesrc = "as of";
37913498266Sopenharmony_ciif(!$filedate) {
38013498266Sopenharmony_ci    # mxr.mozilla.org gave us a time, hg.mozilla.org does not!
38113498266Sopenharmony_ci    $filedate = time();
38213498266Sopenharmony_ci    $datesrc="downloaded on";
38313498266Sopenharmony_ci}
38413498266Sopenharmony_ci
38513498266Sopenharmony_ci# get the hash from the download file
38613498266Sopenharmony_cimy $newhash= sha256($txt);
38713498266Sopenharmony_ci
38813498266Sopenharmony_ciif(!$opt_f && $oldhash eq $newhash) {
38913498266Sopenharmony_ci    report "Downloaded file identical to previous run\'s source file. Exiting";
39013498266Sopenharmony_ci    if($opt_u && -e $txt && !unlink($txt)) {
39113498266Sopenharmony_ci        report "Failed to remove $txt: $!\n";
39213498266Sopenharmony_ci    }
39313498266Sopenharmony_ci    exit;
39413498266Sopenharmony_ci}
39513498266Sopenharmony_ci
39613498266Sopenharmony_cireport "SHA256 of new file: $newhash";
39713498266Sopenharmony_ci
39813498266Sopenharmony_cimy $currentdate = scalar gmtime($filedate);
39913498266Sopenharmony_ci
40013498266Sopenharmony_cimy $format = $opt_t ? "plain text and " : "";
40113498266Sopenharmony_ciif( $stdout ) {
40213498266Sopenharmony_ci    open(CRT, '> -') or die "Couldn't open STDOUT: $!\n";
40313498266Sopenharmony_ci} else {
40413498266Sopenharmony_ci    open(CRT,">$crt.~") or die "Couldn't open $crt.~: $!\n";
40513498266Sopenharmony_ci}
40613498266Sopenharmony_ciprint CRT <<EOT;
40713498266Sopenharmony_ci##
40813498266Sopenharmony_ci## Bundle of CA Root Certificates
40913498266Sopenharmony_ci##
41013498266Sopenharmony_ci## Certificate data from Mozilla ${datesrc}: ${currentdate} GMT
41113498266Sopenharmony_ci##
41213498266Sopenharmony_ci## This is a bundle of X.509 certificates of public Certificate Authorities
41313498266Sopenharmony_ci## (CA). These were automatically extracted from Mozilla's root certificates
41413498266Sopenharmony_ci## file (certdata.txt).  This file can be found in the mozilla source tree:
41513498266Sopenharmony_ci## ${url}
41613498266Sopenharmony_ci##
41713498266Sopenharmony_ci## It contains the certificates in ${format}PEM format and therefore
41813498266Sopenharmony_ci## can be directly used with curl / libcurl / php_curl, or with
41913498266Sopenharmony_ci## an Apache+mod_ssl webserver for SSL client authentication.
42013498266Sopenharmony_ci## Just configure this file as the SSLCACertificateFile.
42113498266Sopenharmony_ci##
42213498266Sopenharmony_ci## Conversion done with mk-ca-bundle.pl version $version.
42313498266Sopenharmony_ci## SHA256: $newhash
42413498266Sopenharmony_ci##
42513498266Sopenharmony_ci
42613498266Sopenharmony_ciEOT
42713498266Sopenharmony_ci
42813498266Sopenharmony_cireport "Processing  '$txt' ...";
42913498266Sopenharmony_cimy $caname;
43013498266Sopenharmony_cimy $certnum = 0;
43113498266Sopenharmony_cimy $skipnum = 0;
43213498266Sopenharmony_cimy $start_of_cert = 0;
43313498266Sopenharmony_cimy $main_block = 0;
43413498266Sopenharmony_cimy $main_block_name;
43513498266Sopenharmony_cimy $trust_block = 0;
43613498266Sopenharmony_cimy $trust_block_name;
43713498266Sopenharmony_cimy @precert;
43813498266Sopenharmony_cimy $cka_value;
43913498266Sopenharmony_cimy $valid = 0;
44013498266Sopenharmony_ci
44113498266Sopenharmony_ciopen(TXT,"$txt") or die "Couldn't open $txt: $!\n";
44213498266Sopenharmony_ciwhile (<TXT>) {
44313498266Sopenharmony_ci  if (/\*\*\*\*\* BEGIN LICENSE BLOCK \*\*\*\*\*/) {
44413498266Sopenharmony_ci    print CRT;
44513498266Sopenharmony_ci    print if ($opt_l);
44613498266Sopenharmony_ci    while (<TXT>) {
44713498266Sopenharmony_ci      print CRT;
44813498266Sopenharmony_ci      print if ($opt_l);
44913498266Sopenharmony_ci      last if (/\*\*\*\*\* END LICENSE BLOCK \*\*\*\*\*/);
45013498266Sopenharmony_ci    }
45113498266Sopenharmony_ci    next;
45213498266Sopenharmony_ci  }
45313498266Sopenharmony_ci  # The input file format consists of blocks of Mozilla objects.
45413498266Sopenharmony_ci  # The blocks are separated by blank lines but may be related.
45513498266Sopenharmony_ci  elsif(/^\s*$/) {
45613498266Sopenharmony_ci    $main_block = 0;
45713498266Sopenharmony_ci    $trust_block = 0;
45813498266Sopenharmony_ci    next;
45913498266Sopenharmony_ci  }
46013498266Sopenharmony_ci  # Each certificate has a main block.
46113498266Sopenharmony_ci  elsif(/^# Certificate "(.*)"/) {
46213498266Sopenharmony_ci    (!$main_block && !$trust_block) or die "Unexpected certificate block";
46313498266Sopenharmony_ci    $main_block = 1;
46413498266Sopenharmony_ci    $main_block_name = $1;
46513498266Sopenharmony_ci    # Reset all other certificate variables.
46613498266Sopenharmony_ci    $trust_block = 0;
46713498266Sopenharmony_ci    $trust_block_name = "";
46813498266Sopenharmony_ci    $valid = 0;
46913498266Sopenharmony_ci    $start_of_cert = 0;
47013498266Sopenharmony_ci    $caname = "";
47113498266Sopenharmony_ci    $cka_value = "";
47213498266Sopenharmony_ci    undef @precert;
47313498266Sopenharmony_ci    next;
47413498266Sopenharmony_ci  }
47513498266Sopenharmony_ci  # Each certificate's main block is followed by a trust block.
47613498266Sopenharmony_ci  elsif(/^# Trust for (?:Certificate )?"(.*)"/) {
47713498266Sopenharmony_ci    (!$main_block && !$trust_block) or die "Unexpected trust block";
47813498266Sopenharmony_ci    $trust_block = 1;
47913498266Sopenharmony_ci    $trust_block_name = $1;
48013498266Sopenharmony_ci    if($main_block_name ne $trust_block_name) {
48113498266Sopenharmony_ci      die "cert name \"$main_block_name\" != trust name \"$trust_block_name\"";
48213498266Sopenharmony_ci    }
48313498266Sopenharmony_ci    next;
48413498266Sopenharmony_ci  }
48513498266Sopenharmony_ci  # Ignore other blocks.
48613498266Sopenharmony_ci  #
48713498266Sopenharmony_ci  # There is a documentation comment block, a BEGINDATA block, and a bunch of
48813498266Sopenharmony_ci  # blocks starting with "# Explicitly Distrust <certname>".
48913498266Sopenharmony_ci  #
49013498266Sopenharmony_ci  # The latter is for certificates that have already been removed and are not
49113498266Sopenharmony_ci  # included. Not all explicitly distrusted certificates are ignored at this
49213498266Sopenharmony_ci  # point, just those without an actual certificate.
49313498266Sopenharmony_ci  elsif(!$main_block && !$trust_block) {
49413498266Sopenharmony_ci    next;
49513498266Sopenharmony_ci  }
49613498266Sopenharmony_ci  elsif(/^#/) {
49713498266Sopenharmony_ci    # The commented lines in a main block are plaintext metadata that describes
49813498266Sopenharmony_ci    # the certificate. Issuer, Subject, Fingerprint, etc.
49913498266Sopenharmony_ci    if($main_block) {
50013498266Sopenharmony_ci      push @precert, $_ if not /^#$/;
50113498266Sopenharmony_ci      if(/^# Not Valid After : (.*)/) {
50213498266Sopenharmony_ci        my $stamp = $1;
50313498266Sopenharmony_ci        use Time::Piece;
50413498266Sopenharmony_ci        # Not Valid After : Thu Sep 30 14:01:15 2021
50513498266Sopenharmony_ci        my $t = Time::Piece->strptime($stamp, "%a %b %d %H:%M:%S %Y");
50613498266Sopenharmony_ci        my $delta = ($t->epoch - time()); # negative means no longer valid
50713498266Sopenharmony_ci        if($delta < 0) {
50813498266Sopenharmony_ci          $skipnum++;
50913498266Sopenharmony_ci          report "Skipping: $main_block_name is not valid anymore" if ($opt_v);
51013498266Sopenharmony_ci          $valid = 0;
51113498266Sopenharmony_ci        }
51213498266Sopenharmony_ci        else {
51313498266Sopenharmony_ci          $valid = 1;
51413498266Sopenharmony_ci        }
51513498266Sopenharmony_ci      }
51613498266Sopenharmony_ci    }
51713498266Sopenharmony_ci    next;
51813498266Sopenharmony_ci  }
51913498266Sopenharmony_ci  elsif(!$valid) {
52013498266Sopenharmony_ci    next;
52113498266Sopenharmony_ci  }
52213498266Sopenharmony_ci
52313498266Sopenharmony_ci  chomp;
52413498266Sopenharmony_ci
52513498266Sopenharmony_ci  if($main_block) {
52613498266Sopenharmony_ci    if(/^CKA_CLASS CK_OBJECT_CLASS CKO_CERTIFICATE/) {
52713498266Sopenharmony_ci      !$start_of_cert or die "Duplicate CKO_CERTIFICATE object";
52813498266Sopenharmony_ci      $start_of_cert = 1;
52913498266Sopenharmony_ci      next;
53013498266Sopenharmony_ci    }
53113498266Sopenharmony_ci    elsif(!$start_of_cert) {
53213498266Sopenharmony_ci      next;
53313498266Sopenharmony_ci    }
53413498266Sopenharmony_ci    elsif(/^CKA_LABEL UTF8 \"(.*)\"/) {
53513498266Sopenharmony_ci      ($caname eq "") or die "Duplicate CKA_LABEL attribute";
53613498266Sopenharmony_ci      $caname = $1;
53713498266Sopenharmony_ci      if($caname ne $main_block_name) {
53813498266Sopenharmony_ci        die "caname \"$caname\" != cert name \"$main_block_name\"";
53913498266Sopenharmony_ci      }
54013498266Sopenharmony_ci      next;
54113498266Sopenharmony_ci    }
54213498266Sopenharmony_ci    elsif(/^CKA_VALUE MULTILINE_OCTAL/) {
54313498266Sopenharmony_ci      ($cka_value eq "") or die "Duplicate CKA_VALUE attribute";
54413498266Sopenharmony_ci      while (<TXT>) {
54513498266Sopenharmony_ci        last if (/^END/);
54613498266Sopenharmony_ci        chomp;
54713498266Sopenharmony_ci        my @octets = split(/\\/);
54813498266Sopenharmony_ci        shift @octets;
54913498266Sopenharmony_ci        for (@octets) {
55013498266Sopenharmony_ci          $cka_value .= chr(oct);
55113498266Sopenharmony_ci        }
55213498266Sopenharmony_ci      }
55313498266Sopenharmony_ci      next;
55413498266Sopenharmony_ci    }
55513498266Sopenharmony_ci    elsif (/^CKA_NSS_SERVER_DISTRUST_AFTER (CK_BBOOL CK_FALSE|MULTILINE_OCTAL)/) {
55613498266Sopenharmony_ci      # Example:
55713498266Sopenharmony_ci      # CKA_NSS_SERVER_DISTRUST_AFTER MULTILINE_OCTAL
55813498266Sopenharmony_ci      # \062\060\060\066\061\067\060\060\060\060\060\060\132
55913498266Sopenharmony_ci      # END
56013498266Sopenharmony_ci      if($1 eq "MULTILINE_OCTAL") {
56113498266Sopenharmony_ci        my @timestamp;
56213498266Sopenharmony_ci        while (<TXT>) {
56313498266Sopenharmony_ci          last if (/^END/);
56413498266Sopenharmony_ci          chomp;
56513498266Sopenharmony_ci          my @octets = split(/\\/);
56613498266Sopenharmony_ci          shift @octets;
56713498266Sopenharmony_ci          for (@octets) {
56813498266Sopenharmony_ci            push @timestamp, chr(oct);
56913498266Sopenharmony_ci          }
57013498266Sopenharmony_ci        }
57113498266Sopenharmony_ci        scalar(@timestamp) == 13 or die "Failed parsing timestamp";
57213498266Sopenharmony_ci        # A trailing Z in the timestamp signifies UTC
57313498266Sopenharmony_ci        if($timestamp[12] ne "Z") {
57413498266Sopenharmony_ci          report "distrust date stamp is not using UTC";
57513498266Sopenharmony_ci        }
57613498266Sopenharmony_ci        # Example date: 200617000000Z
57713498266Sopenharmony_ci        # Means 2020-06-17 00:00:00 UTC
57813498266Sopenharmony_ci        my $distrustat =
57913498266Sopenharmony_ci          timegm($timestamp[10] . $timestamp[11], # second
58013498266Sopenharmony_ci                 $timestamp[8] . $timestamp[9],   # minute
58113498266Sopenharmony_ci                 $timestamp[6] . $timestamp[7],   # hour
58213498266Sopenharmony_ci                 $timestamp[4] . $timestamp[5],   # day
58313498266Sopenharmony_ci                 ($timestamp[2] . $timestamp[3]) - 1, # month
58413498266Sopenharmony_ci                 "20" . $timestamp[0] . $timestamp[1]); # year
58513498266Sopenharmony_ci        if(time >= $distrustat) {
58613498266Sopenharmony_ci          # not trusted anymore
58713498266Sopenharmony_ci          $skipnum++;
58813498266Sopenharmony_ci          report "Skipping: $main_block_name is not trusted anymore" if ($opt_v);
58913498266Sopenharmony_ci          $valid = 0;
59013498266Sopenharmony_ci        }
59113498266Sopenharmony_ci        else {
59213498266Sopenharmony_ci          # still trusted
59313498266Sopenharmony_ci        }
59413498266Sopenharmony_ci      }
59513498266Sopenharmony_ci      next;
59613498266Sopenharmony_ci    }
59713498266Sopenharmony_ci    else {
59813498266Sopenharmony_ci      next;
59913498266Sopenharmony_ci    }
60013498266Sopenharmony_ci  }
60113498266Sopenharmony_ci
60213498266Sopenharmony_ci  if(!$trust_block || !$start_of_cert || $caname eq "" || $cka_value eq "") {
60313498266Sopenharmony_ci    die "Certificate extraction failed";
60413498266Sopenharmony_ci  }
60513498266Sopenharmony_ci
60613498266Sopenharmony_ci  my %trust_purposes_by_level;
60713498266Sopenharmony_ci
60813498266Sopenharmony_ci  if(/^CKA_CLASS CK_OBJECT_CLASS CKO_NSS_TRUST/) {
60913498266Sopenharmony_ci    # now scan the trust part to determine how we should trust this cert
61013498266Sopenharmony_ci    while (<TXT>) {
61113498266Sopenharmony_ci      if(/^\s*$/) {
61213498266Sopenharmony_ci        $trust_block = 0;
61313498266Sopenharmony_ci        last;
61413498266Sopenharmony_ci      }
61513498266Sopenharmony_ci      if (/^CKA_TRUST_([A-Z_]+)\s+CK_TRUST\s+CKT_NSS_([A-Z_]+)\s*$/) {
61613498266Sopenharmony_ci        if ( !is_in_list($1,@valid_mozilla_trust_purposes) ) {
61713498266Sopenharmony_ci          report "Warning: Unrecognized trust purpose for cert: $caname. Trust purpose: $1. Trust Level: $2";
61813498266Sopenharmony_ci        } elsif ( !is_in_list($2,@valid_mozilla_trust_levels) ) {
61913498266Sopenharmony_ci          report "Warning: Unrecognized trust level for cert: $caname. Trust purpose: $1. Trust Level: $2";
62013498266Sopenharmony_ci        } else {
62113498266Sopenharmony_ci          push @{$trust_purposes_by_level{$2}}, $1;
62213498266Sopenharmony_ci        }
62313498266Sopenharmony_ci      }
62413498266Sopenharmony_ci    }
62513498266Sopenharmony_ci
62613498266Sopenharmony_ci    # Sanity check that an explicitly distrusted certificate only has trust
62713498266Sopenharmony_ci    # purposes with a trust level of NOT_TRUSTED.
62813498266Sopenharmony_ci    #
62913498266Sopenharmony_ci    # Certificate objects that are explicitly distrusted are in a certificate
63013498266Sopenharmony_ci    # block that starts # Certificate "Explicitly Distrust(ed) <certname>",
63113498266Sopenharmony_ci    # where "Explicitly Distrust(ed) " was prepended to the original cert name.
63213498266Sopenharmony_ci    if($caname =~ /distrust/i ||
63313498266Sopenharmony_ci       $main_block_name =~ /distrust/i ||
63413498266Sopenharmony_ci       $trust_block_name =~ /distrust/i) {
63513498266Sopenharmony_ci      my @levels = keys %trust_purposes_by_level;
63613498266Sopenharmony_ci      if(scalar(@levels) != 1 || $levels[0] ne "NOT_TRUSTED") {
63713498266Sopenharmony_ci        die "\"$caname\" must have all trust purposes at level NOT_TRUSTED.";
63813498266Sopenharmony_ci      }
63913498266Sopenharmony_ci    }
64013498266Sopenharmony_ci
64113498266Sopenharmony_ci    if ( !should_output_cert(%trust_purposes_by_level) ) {
64213498266Sopenharmony_ci      $skipnum ++;
64313498266Sopenharmony_ci      report "Skipping: $caname lacks acceptable trust level" if ($opt_v);
64413498266Sopenharmony_ci    } else {
64513498266Sopenharmony_ci      my $encoded = MIME::Base64::encode_base64($cka_value, '');
64613498266Sopenharmony_ci      $encoded =~ s/(.{1,${opt_w}})/$1\n/g;
64713498266Sopenharmony_ci      my $pem = "-----BEGIN CERTIFICATE-----\n"
64813498266Sopenharmony_ci              . $encoded
64913498266Sopenharmony_ci              . "-----END CERTIFICATE-----\n";
65013498266Sopenharmony_ci      print CRT "\n$caname\n";
65113498266Sopenharmony_ci      my $maxStringLength = length(decode('UTF-8', $caname, Encode::FB_CROAK | Encode::LEAVE_SRC));
65213498266Sopenharmony_ci      print CRT ("=" x $maxStringLength . "\n");
65313498266Sopenharmony_ci      if ($opt_t) {
65413498266Sopenharmony_ci        foreach my $key (sort keys %trust_purposes_by_level) {
65513498266Sopenharmony_ci           my $string = $key . ": " . join(", ", @{$trust_purposes_by_level{$key}});
65613498266Sopenharmony_ci           print CRT $string . "\n";
65713498266Sopenharmony_ci        }
65813498266Sopenharmony_ci      }
65913498266Sopenharmony_ci      if($opt_m) {
66013498266Sopenharmony_ci        print CRT for @precert;
66113498266Sopenharmony_ci      }
66213498266Sopenharmony_ci      if (!$opt_t) {
66313498266Sopenharmony_ci        print CRT $pem;
66413498266Sopenharmony_ci      } else {
66513498266Sopenharmony_ci        my $pipe = "";
66613498266Sopenharmony_ci        foreach my $hash (@included_signature_algorithms) {
66713498266Sopenharmony_ci          $pipe = "|$openssl x509 -" . $hash . " -fingerprint -noout -inform PEM";
66813498266Sopenharmony_ci          if (!$stdout) {
66913498266Sopenharmony_ci            $pipe .= " >> $crt.~";
67013498266Sopenharmony_ci            close(CRT) or die "Couldn't close $crt.~: $!";
67113498266Sopenharmony_ci          }
67213498266Sopenharmony_ci          open(TMP, $pipe) or die "Couldn't open openssl pipe: $!";
67313498266Sopenharmony_ci          print TMP $pem;
67413498266Sopenharmony_ci          close(TMP) or die "Couldn't close openssl pipe: $!";
67513498266Sopenharmony_ci          if (!$stdout) {
67613498266Sopenharmony_ci            open(CRT, ">>$crt.~") or die "Couldn't open $crt.~: $!";
67713498266Sopenharmony_ci          }
67813498266Sopenharmony_ci        }
67913498266Sopenharmony_ci        $pipe = "|$openssl x509 -text -inform PEM";
68013498266Sopenharmony_ci        if (!$stdout) {
68113498266Sopenharmony_ci          $pipe .= " >> $crt.~";
68213498266Sopenharmony_ci          close(CRT) or die "Couldn't close $crt.~: $!";
68313498266Sopenharmony_ci        }
68413498266Sopenharmony_ci        open(TMP, $pipe) or die "Couldn't open openssl pipe: $!";
68513498266Sopenharmony_ci        print TMP $pem;
68613498266Sopenharmony_ci        close(TMP) or die "Couldn't close openssl pipe: $!";
68713498266Sopenharmony_ci        if (!$stdout) {
68813498266Sopenharmony_ci          open(CRT, ">>$crt.~") or die "Couldn't open $crt.~: $!";
68913498266Sopenharmony_ci        }
69013498266Sopenharmony_ci      }
69113498266Sopenharmony_ci      report "Processed: $caname" if ($opt_v);
69213498266Sopenharmony_ci      $certnum ++;
69313498266Sopenharmony_ci    }
69413498266Sopenharmony_ci  }
69513498266Sopenharmony_ci}
69613498266Sopenharmony_ciclose(TXT) or die "Couldn't close $txt: $!\n";
69713498266Sopenharmony_ciclose(CRT) or die "Couldn't close $crt.~: $!\n";
69813498266Sopenharmony_ciunless( $stdout ) {
69913498266Sopenharmony_ci    if ($opt_b && -e $crt) {
70013498266Sopenharmony_ci        my $bk = 1;
70113498266Sopenharmony_ci        while (-e "$crt.~${bk}~") {
70213498266Sopenharmony_ci            $bk++;
70313498266Sopenharmony_ci        }
70413498266Sopenharmony_ci        rename $crt, "$crt.~${bk}~" or die "Failed to create backup $crt.~$bk}~: $!\n";
70513498266Sopenharmony_ci    } elsif( -e $crt ) {
70613498266Sopenharmony_ci        unlink( $crt ) or die "Failed to remove $crt: $!\n";
70713498266Sopenharmony_ci    }
70813498266Sopenharmony_ci    rename "$crt.~", $crt or die "Failed to rename $crt.~ to $crt: $!\n";
70913498266Sopenharmony_ci}
71013498266Sopenharmony_ciif($opt_u && -e $txt && !unlink($txt)) {
71113498266Sopenharmony_ci  report "Failed to remove $txt: $!\n";
71213498266Sopenharmony_ci}
71313498266Sopenharmony_cireport "Done ($certnum CA certs processed, $skipnum skipped).";
714