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 C42FC1FF09B for ; Mon, 09 Nov 2026 11:37:59 +0100 (CET) Received: from gate001.proxmox.com (localhost.localdomain [127.0.0.1]) by gate001.proxmox.com (Proxmox) with ESMTP id 7BB802163C; Mon, 09 Nov 2026 11:37:49 +0100 (CET) From: Thomas Ellmenreich To: pve-devel@lists.proxmox.com Subject: [PATCH common 1/3] pem to der: make header and footer parsing stricter Date: Mon, 9 Nov 2026 11:37:31 +0100 Message-ID: <20261109103733.128704-2-t.ellmenreich@proxmox.com> X-Mailer: git-send-email 2.47.3 In-Reply-To: <20261109103733.128704-1-t.ellmenreich@proxmox.com> References: <20261109103733.128704-1-t.ellmenreich@proxmox.com> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Bm-Milter-Handled: 55990f41-d878-4baa-be0a-ee34c49e34d2 X-Bm-Transport-Timestamp: 1794220663364 X-SPAM-LEVEL: Spam detection results: 0 AWL 0.354 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: Y7I465FMGX2A2G3AKCIYFIIQGNTZ7QQS X-Message-ID-Hash: Y7I465FMGX2A2G3AKCIYFIIQGNTZ7QQS X-MailFrom: t.ellmenreich@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: Thomas Ellmenreich X-Mailman-Version: 3.3.10 Precedence: list List-Id: Proxmox VE development discussion List-Help: List-Owner: List-Post: List-Subscribe: List-Unsubscribe: When parsing a PEM to then decode it to a DER, the header and footer regex could previously lead to the header being included in the decoded content as is the case with the following example: -----BEGIN CERTIFICATE-----TG9yZW0gaXBzdW0= -----END CERTIFICATE----- Since the subroutine's focus is not to validate the structure but just to decode the content, create a clearer error boundary by die'ing quickly if no header or footer is found. Additionally, there was an edgecase where the regex for the label would glob up the whole PEM. A stricter label regex avoids globbing up the whole PEM and can then be used to produce a more meaningful error. Signed-off-by: Thomas Ellmenreich --- src/PVE/Certificate.pm | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/src/PVE/Certificate.pm b/src/PVE/Certificate.pm index 589bce8..6985d75 100644 --- a/src/PVE/Certificate.pm +++ b/src/PVE/Certificate.pm @@ -183,14 +183,17 @@ sub check_pem { sub pem_to_der { my ($content) = @_; - my $header = $header_re->(qr/.*?/); - my $footer = $footer_re->(qr/.*?/); + # RFC 7468 allows a single dash, so only dissallow 2 consecutive dashes + my $header = $header_re->(qr/(?:(?!--).)+?/); + my $footer = $footer_re->(qr/(?:(?!--).)+?/); $content = strip_leading_text($content); # only take first PEM entry - $content =~ s/^$header$//mg; - $content =~ s/$footer.*//sg; + $content =~ s/^$header$//mg + or die "Could not convert PEM to DER, no header found.\n"; + $content =~ s/^$footer.*//ms + or die "Could not convert PEM to DER, no footer found.\n"; $content = decode_base64($content); -- 2.47.3