certificate: factor out obtaining the expiration timestamp

This can be useful to have, e.g., when requiring different behaviors
the nearer an expiry gets.

Signed-off-by: Thomas Lamprecht <t.lamprecht@proxmox.com>
This commit is contained in:
Thomas Lamprecht 2024-11-11 09:51:57 +01:00
parent 8915b9669c
commit 61073db2bc

View File

@ -316,11 +316,9 @@ sub get_certificate_info {
return $info;
};
# Checks whether certificate expires before $timestamp (UNIX epoch)
sub check_expiry {
my ($cert_path, $timestamp) = @_;
$timestamp //= time();
# Obtain the expiration timestamp of a X.509 certificate as a UNIX epoch.
sub get_expiration_as_epoch {
my ($cert_path) = @_;
my $cert = $read_certificate->($cert_path);
my $not_after = eval { convert_asn1_to_epoch(Net::SSLeay::X509_get_notAfter($cert)) };
@ -330,6 +328,17 @@ sub check_expiry {
die $err if $err;
return $not_after;
};
# Checks whether certificate expires before $timestamp (UNIX epoch)
sub check_expiry {
my ($cert_path, $timestamp) = @_;
$timestamp //= time();
my $not_after = get_expiration_as_epoch($cert_path);
return ($not_after < $timestamp) ? 1 : 0;
};