public inbox for pve-devel@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 09/11] pem to der: create clearer error boundary
Date: Wed, 18 Nov 2026 11:31:23 +0100	[thread overview]
Message-ID: <20261118103127.165100-10-t.ellmenreich@proxmox.com> (raw)
In-Reply-To: <20261118103127.165100-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-----

Here the header regex would not match, but the footer would, meaning that
the header is included in the decoded content. Instead of hiding this
error, fail quickly and show a meaningful error which makes for a
clearer error boundary.

Tests have also been added to pin down 'pem_to_der' functionality.

Signed-off-by: Thomas Ellmenreich <t.ellmenreich@proxmox.com>
---
 src/PVE/Certificate.pm  |   8 +-
 test/Makefile           |   1 +
 test/pem_to_der_test.pl | 161 ++++++++++++++++++++++++++++++++++++++++
 3 files changed, 167 insertions(+), 3 deletions(-)
 create mode 100755 test/pem_to_der_test.pl

diff --git a/src/PVE/Certificate.pm b/src/PVE/Certificate.pm
index 620ba6b..2f65568 100644
--- a/src/PVE/Certificate.pm
+++ b/src/PVE/Certificate.pm
@@ -214,11 +214,13 @@ sub pem_to_der {
     my $header = $header_re->($ANY_LABEL);
     my $footer = $footer_re->($ANY_LABEL);
 
-    $content = strip_leading_text($content);
+    $content = strip_surrounding_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);
 
diff --git a/test/Makefile b/test/Makefile
index 9caba10..f111511 100644
--- a/test/Makefile
+++ b/test/Makefile
@@ -19,6 +19,7 @@ TESTS = lock_file.test			\
 	ticket_test.test \
 	check_pem_test.test \
 	split_pem_test.test \
+	pem_to_der_test.test \
 
 all:
 
diff --git a/test/pem_to_der_test.pl b/test/pem_to_der_test.pl
new file mode 100755
index 0000000..c35e1f4
--- /dev/null
+++ b/test/pem_to_der_test.pl
@@ -0,0 +1,161 @@
+#!/usr/bin/perl
+# Tests the PVE::Certificate::pem_to_der function for
+# correctness and coverage of edgecases.
+use strict;
+use warnings;
+
+use lib '../src';
+
+use Test::More;
+
+use PVE::Certificate;
+
+# Arrange
+my $setup = [
+    {
+        name => "single pem",
+        pem => q{-----BEGIN CERTIFICATE-----
+TG9yZW0gaXBzdW0gZG9sb3Igc2l0IGFtZXQsIGNvbnNlY3RldHVyIGFkaXBpc2Np
+bmcgZWxpdCwgc2VkIGRvIGVpdXNtb2QgdGVtcG9yIGluY2lkaWR1bnQgdXQgbGFi
+b3JlIGV0IGRvbG9yZSBtYWduYSBhbGlxdWEuIFV0IGVuaW0gYWQgbWluaW0gdmVu
+aWFtLCBxdWlzIG5vc3RydWQgZXhlcmNpdGF0aW9uIHVsbGFtY28gbGFib3JpcyBu
+aXNpIHV0IGFsaXF1aXAgZXggZWEgY29tbW9kbyBjb25zZXF1YXQuIER1aXMgYXV0
+ZSBpcnVyZSBkb2xvciBpbiByZXByZWhlbmRlcml0IGluIHZvbHVwdGF0ZSB2ZWxp
+dCBlc3NlIGNpbGx1bSBkb2xvcmUgZXUgZnVnaWF0IG51bGxhIHBhcmlhdHVyLiBF
+eGNlcHRldXIgc2ludCBvY2NhZWNhdCBjdXBpZGF0YXQgbm9uIHByb2lkZW50LCBz
+dW50IGluIGN1bHBhIHF1aSBvZmZpY2lhIGRlc2VydW50IG1vbGxpdCBhbmltIGlk
+IGVzdCBsYWJvcnVtLg==
+-----END CERTIFICATE-----
+},
+        expected_decoded_result =>
+            "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.",
+    },
+    {
+        name => "single pem no terminating newline",
+        pem => q{-----BEGIN CERTIFICATE-----
+TG9yZW0gaXBzdW0=
+-----END CERTIFICATE-----},
+        expected_decoded_result => "Lorem ipsum",
+    },
+    {
+        name => "multiple pem",
+        pem => q{-----BEGIN CERTIFICATE-----
+TG9yZW0gaXBzdW0gMQ==
+-----END CERTIFICATE-----
+-----BEGIN CERTIFICATE-----
+TG9yZW0gaXBzdW0gMg==
+-----END CERTIFICATE-----
+-----BEGIN CERTIFICATE-----
+TG9yZW0gaXBzdW0gMw==
+-----END CERTIFICATE-----
+},
+        expected_decoded_result => "Lorem ipsum 1",
+    },
+    {
+        name => "multiple pem leading newline",
+        pem => q{
+-----BEGIN CERTIFICATE-----
+TG9yZW0gaXBzdW0gMQ==
+-----END CERTIFICATE-----
+-----BEGIN CERTIFICATE-----
+TG9yZW0gaXBzdW0gMg==
+-----END CERTIFICATE-----
+-----BEGIN CERTIFICATE-----
+TG9yZW0gaXBzdW0gMw==
+-----END CERTIFICATE-----
+},
+        expected_decoded_result => "Lorem ipsum 1",
+    },
+    {
+        name => "multiple pem leading text",
+        pem => q{
+some leading
+text that is expected
+to be stripped
+-----BEGIN CERTIFICATE-----
+TG9yZW0gaXBzdW0gMQ==
+-----END CERTIFICATE-----
+-----BEGIN CERTIFICATE-----
+TG9yZW0gaXBzdW0gMg==
+-----END CERTIFICATE-----
+-----BEGIN CERTIFICATE-----
+TG9yZW0gaXBzdW0gMw==
+-----END CERTIFICATE-----
+},
+        expected_decoded_result => "Lorem ipsum 1",
+    },
+    {
+        name => "multiple pem interleaved text",
+        pem => q{
+some leading text
+-----BEGIN CERTIFICATE-----
+TG9yZW0gaXBzdW0gMQ==
+-----END CERTIFICATE-----
+some leading text
+-----BEGIN CERTIFICATE-----
+TG9yZW0gaXBzdW0gMg==
+-----END CERTIFICATE-----
+some leading text
+-----BEGIN CERTIFICATE-----
+TG9yZW0gaXBzdW0gMw==
+-----END CERTIFICATE-----
+some end text
+},
+        expected_decoded_result => "Lorem ipsum 1",
+    },
+    {
+        name => "multiple pem no newline after footer",
+        pem => q{
+-----BEGIN CERTIFICATE-----
+TG9yZW0gaXBzdW0gMQ==
+-----END CERTIFICATE----------BEGIN CERTIFICATE-----
+TG9yZW0gaXBzdW0gMg==
+-----END CERTIFICATE-----
+-----BEGIN CERTIFICATE-----
+TG9yZW0gaXBzdW0gMw==
+-----END CERTIFICATE-----
+},
+        expected_decoded_result => "Lorem ipsum 1",
+    },
+    {
+        name => "all in one line",
+        pem => "-----BEGIN CERTIFICATE-----TG9yZW0gaXBzdW0=-----END CERTIFICATE-----",
+        expected_error => "header",
+    },
+    {
+        name => "only newline after begin",
+        pem => q{-----BEGIN CERTIFICATE-----
+TG9yZW0gaXBzdW0=-----END CERTIFICATE-----},
+        expected_error => "footer",
+    },
+    {
+        name => "only newline after content",
+        pem => q{-----BEGIN CERTIFICATE-----TG9yZW0gaXBzdW0=
+-----END CERTIFICATE-----},
+        expected_error => "header",
+    },
+    {
+        name => "only newline after end",
+        pem => q{-----BEGIN CERTIFICATE-----TG9yZW0gaXBzdW0=-----END CERTIFICATE-----
+},
+        expected_error => "header",
+    },
+];
+
+for my $data ($setup->@*) {
+    subtest $data->{name} => sub {
+        my $to_der_result = eval { PVE::Certificate::pem_to_der($data->{pem}) };
+        my $to_der_error = $@;
+        if (defined($data->{expected_decoded_result})) {
+            is($to_der_result, $data->{expected_decoded_result}, "has correct decoded result");
+        } else {
+            like(
+                $to_der_error,
+                qr/Could not convert PEM to DER, no $data->{expected_error} found./,
+                "has died with expected error",
+            );
+        }
+    }
+}
+
+done_testing();
-- 
2.47.3





  parent reply	other threads:[~2026-11-18 10:32 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 ` [PATCH common v2 06/11] pem parsing: strip all surrounding text from chain entries Thomas Ellmenreich
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 ` Thomas Ellmenreich [this message]
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-10-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
Service provided by Proxmox Server Solutions GmbH | Privacy | Legal