From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from gate001.proxmox.com (gate001.proxmox.com [45.144.208.40]) by lore.proxmox.com (Postfix) with ESMTPS id F10041FF0C0 for ; Wed, 21 Oct 2026 11:52:58 +0200 (CEST) Received: from gate001.proxmox.com (localhost.localdomain [127.0.0.1]) by gate001.proxmox.com (Proxmox) with ESMTP id AB04F21683; Wed, 21 Oct 2026 11:52:51 +0200 (CEST) From: David Riley To: pve-devel@lists.proxmox.com Subject: [PATCH pve-common v3 1/4] certificate: add helper to extract validity period Date: Wed, 21 Oct 2026 11:52:26 +0200 Message-ID: <20261021095229.50842-2-d.riley@proxmox.com> X-Mailer: git-send-email 2.47.3 In-Reply-To: <20261021095229.50842-1-d.riley@proxmox.com> References: <20261021095229.50842-1-d.riley@proxmox.com> MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit X-Bm-Milter-Handled: 55990f41-d878-4baa-be0a-ee34c49e34d2 X-Bm-Transport-Timestamp: 1792576367783 X-SPAM-LEVEL: Spam detection results: 0 AWL 0.675 Adjusted score from AWL reputation of From: address DMARC_MISSING 0.1 Missing DMARC policy KAM_DMARC_STATUS 0.01 Test Rule for DKIM or SPF Failure with Strict Alignment (newer systems) RCVD_IN_DNSWL_MED -2.3 Sender listed at https://www.dnswl.org/, medium trust SPF_HELO_NONE 0.001 SPF: HELO does not publish an SPF Record SPF_PASS -0.001 SPF: sender matches SPF record Message-ID-Hash: AHCYWWUN7IFJKVCDILZWNYOUU34OIUNA X-Message-ID-Hash: AHCYWWUN7IFJKVCDILZWNYOUU34OIUNA X-MailFrom: d.riley@proxmox.com X-Mailman-Rule-Misses: dmarc-mitigation; no-senders; approved; loop; banned-address; emergency; member-moderation; nonmember-moderation; administrivia; implicit-dest; max-recipients; max-size; news-moderation; no-subject; digests; suspicious-header CC: David Riley X-Mailman-Version: 3.3.10 Precedence: list List-Id: Proxmox VE development discussion List-Help: List-Owner: List-Post: List-Subscribe: List-Unsubscribe: Previously, only the expiration timestamp was needed, so only notAfter was parsed from the certificate. To prepare for lifetime calculations, introduce a helper that extracts both the notBefore and notAfter timestamps [0] in a single certificate read. Adapt the existing expiration helper to consume it without any change in behavior for existing callers. [0] https://www.rfc-editor.org/info/rfc5280/#section-4.1 Signed-off-by: David Riley Reviewed-by: Michael Köppl Tested-by: Michael Köppl --- src/PVE/Certificate.pm | 16 ++++++++++++++-- 1 file changed, 14 insertions(+), 2 deletions(-) diff --git a/src/PVE/Certificate.pm b/src/PVE/Certificate.pm index b8415e2..8406949 100644 --- a/src/PVE/Certificate.pm +++ b/src/PVE/Certificate.pm @@ -331,16 +331,28 @@ sub get_certificate_info { # Obtain the expiration timestamp of a X.509 certificate as a UNIX epoch. sub get_expiration_as_epoch { my ($cert_path) = @_; + my (undef, $not_after) = get_validity_as_epoch($cert_path); + return $not_after; +} + +# Obtain the notBefore and notAfter timestamps of a X.509 certificate as UNIX epochs. +sub get_validity_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)) }; + my ($not_before, $not_after) = eval { + ( + convert_asn1_to_epoch(Net::SSLeay::X509_get_notBefore($cert)), + convert_asn1_to_epoch(Net::SSLeay::X509_get_notAfter($cert)), + ); + }; my $err = $@; Net::SSLeay::X509_free($cert); die $err if $err; - return $not_after; + return ($not_before, $not_after); } # Checks whether certificate expires before $timestamp (UNIX epoch) -- 2.47.3