Last active
July 27, 2024 16:10
-
-
Save holly/39e6aa1ab717ce7fa146b2f2f8c4eb0f to your computer and use it in GitHub Desktop.
download ca-bundle(cacert.pem from curl website) and extract and make hash symlink
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env perl | |
use strict; | |
use warnings; | |
use autodie qw(open chdir symlink); | |
use feature qw(say); | |
use HTTP::Request; | |
use LWP::UserAgent; | |
our $CACERT = "ca-bundle.crt"; | |
our $CACERT_URL = "https://curl.se/ca/cacert.pem"; | |
our $OPENSSL_CMD = "/usr/bin/openssl"; | |
our $CERT_EXT = ".pem"; | |
my $name; | |
my $start = 0; | |
my @lines; | |
sub download_cacert { | |
my $req = HTTP::Request->new(GET => $CACERT_URL); | |
my $ua = LWP::UserAgent->new; | |
my $res = $ua->request($req); | |
if ($res->is_success) { | |
open my $fh, ">", $CACERT; | |
say $fh $res->content; | |
close $fh; | |
} else { | |
die $res->status_line; | |
} | |
} | |
sub make_cert { | |
my ($name, $ref) = @_; | |
$name =~ s/ /_/g; | |
my $fname = "${name}${CERT_EXT}"; | |
open my $fh, ">", $fname; | |
say $fh join("\n", @$ref); | |
close $fh; | |
return $fname; | |
} | |
sub make_hash_name { | |
my $name = shift; | |
my $num = 0; | |
my $hash_name; | |
my @command = ($OPENSSL_CMD, "x509", "-hash", "-noout", "-in", $name); | |
open my $child, "-|", @command; | |
chomp(my $hash = <$child>); | |
close $child; | |
while (1) { | |
$hash_name = "${hash}.${num}"; | |
if (-l $hash_name) { | |
$num++; | |
} else { | |
last; | |
} | |
} | |
return $hash_name; | |
} | |
my $dir = @ARGV[0]; | |
chdir $dir if $dir; | |
download_cacert(); | |
open my $fh, "<", $CACERT; | |
while (my $line = <$fh>) { | |
$line =~ s/[\r\n]+//g; | |
if (!$name && $line =~ /^[A-za-z0-9]+/) { | |
$name = $line; | |
next; | |
} | |
if ($line =~ /^===+/) { | |
next; | |
} | |
if ($line eq "-----BEGIN CERTIFICATE-----") { | |
push @lines, $line; | |
$start = 1; | |
next; | |
} | |
if ($start == 1 && $line ne "-----END CERTIFICATE-----") { | |
push @lines, $line; | |
next; | |
} | |
if ($line eq "-----END CERTIFICATE-----") { | |
push @lines, $line; | |
my $fname = make_cert($name, \@lines); | |
my $hash_name = make_hash_name($fname); | |
symlink $fname, $hash_name; | |
say "symlink created: $hash_name -> $fname"; | |
$start = 0; | |
$name = undef; | |
@lines = (); | |
} | |
} | |
close $fh; | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment