all lists on lists.proxmox.com
 help / color / mirror / Atom feed
From: Thomas Ellmenreich <t.ellmenreich@proxmox.com>
To: pve-devel@lists.proxmox.com
Cc: Thomas Ellmenreich <t.ellmenreich@proxmox.com>
Subject: [PATCH common v2 06/11] pem parsing: strip all surrounding text from chain entries
Date: Wed, 18 Nov 2026 11:31:20 +0100	[thread overview]
Message-ID: <20261118103127.165100-7-t.ellmenreich@proxmox.com> (raw)
In-Reply-To: <20261118103127.165100-1-t.ellmenreich@proxmox.com>

Passing a chain that contains entries with different labels, but then
specificing one of those labels leads to 'split_pem' only splitting on
that specific label. This means that a single entry returned by
'split_pem' can contain multiple pem entries.

That said it always contains a single entry of the specified label, so
strip away any content before the BEGIN and after the END of the
specified label.

Signed-off-by: Thomas Ellmenreich <t.ellmenreich@proxmox.com>
---
 src/PVE/Certificate.pm | 19 +++++++++++++++----
 test/check_pem_test.pl |  8 +-------
 2 files changed, 16 insertions(+), 11 deletions(-)

diff --git a/src/PVE/Certificate.pm b/src/PVE/Certificate.pm
index 0380250..84b97e8 100644
--- a/src/PVE/Certificate.pm
+++ b/src/PVE/Certificate.pm
@@ -139,6 +139,18 @@ my sub trim_trailing_space {
     return $content;
 }
 
+my sub strip_surrounding_text {
+    my ($content, %opts) = @_;
+
+    my $label = $opts{label} // $ANY_LABEL;
+
+    my $header = $header_re->($label);
+    my $footer = $footer_re->($label);
+    $content =~ s/\A.*?(?=$header)//s;
+    $content =~ s/$footer\K.*\z//s;
+    return trim_trailing_space($content);
+}
+
 sub strip_leading_text {
     my ($content) = @_;
 
@@ -177,15 +189,14 @@ sub check_pem {
 
     my @result_entries = ();
     for my $entry (@split) {
-        my $stripped = strip_leading_text($entry);
-        my $trimmed = trim_trailing_space($stripped);
+        my $stripped = strip_surrounding_text($entry, label => $label);
 
-        if ($trimmed !~ /$re/) {
+        if ($stripped !~ /$re/) {
             @result_entries = ();
             last;
         }
 
-        push @result_entries, $trimmed;
+        push @result_entries, $stripped;
     }
     my $result_pem = join "\n", @result_entries;
 
diff --git a/test/check_pem_test.pl b/test/check_pem_test.pl
index d806f09..b1bb6f0 100755
--- a/test/check_pem_test.pl
+++ b/test/check_pem_test.pl
@@ -103,8 +103,7 @@ test
 some extended content},
         expected_result => q{-----BEGIN CERTIFICATE-----
 test
------END CERTIFICATE-----
-some extended content}
+-----END CERTIFICATE-----}
     },
     {
         expected_success => 0,
@@ -154,11 +153,6 @@ TG9yZW0gaXBzdW0gMg==
 -----END CERTIFICATE-----},
         expected_result => q{-----BEGIN CERTIFICATE-----
 TG9yZW0gaXBzdW0gMQ==
------END CERTIFICATE-----
-Subject: CN=Org Issuing CA 3
-Issuer: CN=Org Root CA
------BEGIN CERTIFICATE-----
-TG9yZW0gaXBzdW0gMg==
 -----END CERTIFICATE-----}
     },
     {
-- 
2.47.3





  parent reply	other threads:[~2026-11-18 10:31 UTC|newest]

Thread overview: 12+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-18 10:31 [PATCH common v2 00/11] pem parser: refactor parsing to cover more edge cases Thomas Ellmenreich
2026-11-18 10:31 ` [PATCH common v2 01/11] certificate tests: use q{} for multiline strings Thomas Ellmenreich
2026-11-18 10:31 ` [PATCH common v2 02/11] pem parser: add constant to match any label Thomas Ellmenreich
2026-11-18 10:31 ` [PATCH common v2 03/11] pem parser: refactor check_pem to use list instead of string Thomas Ellmenreich
2026-11-18 10:31 ` [PATCH common v2 04/11] pem parser: stricter handling of newlines Thomas Ellmenreich
2026-11-18 10:31 ` [PATCH common v2 05/11] certificate formats: enable multiple parsing for pem-string format Thomas Ellmenreich
2026-11-18 10:31 ` Thomas Ellmenreich [this message]
2026-11-18 10:31 ` [PATCH common v2 07/11] pem parsing: make validation of pem entry stricter Thomas Ellmenreich
2026-11-18 10:31 ` [PATCH common v2 08/11] pem parsing: default splitting to any label Thomas Ellmenreich
2026-11-18 10:31 ` [PATCH common v2 09/11] pem to der: create clearer error boundary Thomas Ellmenreich
2026-11-18 10:31 ` [PATCH common v2 10/11] certificate tests: refactor and exapand testsuite Thomas Ellmenreich
2026-11-18 10:31 ` [PATCH common v2 11/11] pom parsing: allow for CRLF line ending 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=20261118103127.165100-7-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 an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.
Service provided by Proxmox Server Solutions GmbH | Privacy | Legal