From: Thomas Ellmenreich <t.ellmenreich@proxmox.com>
To: pve-devel@lists.proxmox.com
Cc: Thomas Ellmenreich <t.ellmenreich@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 [thread overview]
Message-ID: <20261109103733.128704-2-t.ellmenreich@proxmox.com> (raw)
In-Reply-To: <20261109103733.128704-1-t.ellmenreich@proxmox.com>
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 <t.ellmenreich@proxmox.com>
---
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
next prev parent reply other threads:[~2026-11-09 10:37 UTC|newest]
Thread overview: 4+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-11-09 10:37 [PATCH common 0/3] pem parser: handle missing newline edge case Thomas Ellmenreich
2026-11-09 10:37 ` Thomas Ellmenreich [this message]
2026-11-09 10:37 ` [PATCH common 2/3] tests: introduce tests for pem_to_der Thomas Ellmenreich
2026-11-09 10:37 ` [PATCH common 3/3] check pem: force at least one newline after header and footer Thomas Ellmenreich
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20261109103733.128704-2-t.ellmenreich@proxmox.com \
--to=t.ellmenreich@proxmox.com \
--cc=pve-devel@lists.proxmox.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox