public inbox for pve-devel@lists.proxmox.com
 help / color / mirror / Atom feed
From: Mark Atwood <mark@wolfssl.com>
To: pve-devel@lists.proxmox.com
Subject: [PATCH common v2 1/2] ticket: accept RSA tickets signed with SHA-256
Date: Fri, 13 Nov 2026 03:06:38 +0000	[thread overview]
Message-ID: <20261113030639.3514-2-mark@wolfssl.com> (raw)
In-Reply-To: <20261113030639.3514-1-mark@wolfssl.com>

Verification only accepted tickets with an RSA signature made with
SHA-1, the default digest of Crypt::OpenSSL::RSA. Providers that
refuse SHA-1 signatures, such as FIPS ones, cannot create or check
such tickets. As a first step towards signing with SHA-256, accept
tickets signed with either SHA-256 or SHA-1, trying SHA-256 first.

Accepting SHA-256 first lets nodes that verify tickets be upgraded
ahead of the ones that sign with SHA-256, so it needs to be rolled out
before any signing change. This also affects Proxmox Mail Gateway,
which uses PVE::Ticket for its tickets too.

A mismatching signature makes the library return false. A provider
that refuses SHA-1 makes it die instead, for example wolfProvider
when signing: "RSA.xs:1007: OpenSSL error: (null)". Treat exactly that
kind of message as the signature not matching for SHA-1, and warn
about all other errors, so they do not go unnoticed. wolfProvider
still verifies SHA-1 signatures, so there SHA-1 tickets from nodes
that are not upgraded yet keep working.

Verifying switches the digest of the public key object, which is state
that callers can see. Always reset it to the library default, SHA-1,
after verifying, regardless of which digest matched or whether an
error happened. This leaves the object as it was before and does not
leave a digest behind that depends on the last ticket seen. Documenting
the left-over state instead would have made every caller depend on it.

The tests use tickets signed with the openssl command line, so they do
not rely on the signing code. They also cover that verification works
without the noerr parameter, and the handling of provider errors.

Signed-off-by: Mark Atwood <mark@wolfssl.com>
---
 src/PVE/Ticket.pm   | 35 +++++++++++++++++-
 test/ticket_test.pl | 87 +++++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 121 insertions(+), 1 deletion(-)

diff --git a/src/PVE/Ticket.pm b/src/PVE/Ticket.pm
index 5bd06fc..f20ab5d 100644
--- a/src/PVE/Ticket.pm
+++ b/src/PVE/Ticket.pm
@@ -71,6 +71,39 @@ sub assemble_rsa_ticket {
     return $ticket;
 }
 
+# Checks the signature of a ticket, accepting SHA-256 and SHA-1 signatures. Returns true if one of
+# them matches.
+#
+# TODO: Remove SHA-1 fallback once SHA-256 signed tickets are the default across PMG and PVE for at
+# least one major release.
+#
+# Note: Always leaves $rsa_pub with the library default digest (SHA-1), as it was before it got
+# used here, no matter which one matched.
+my sub verify_rsa_signature {
+    my ($rsa_pub, $full, $sig) = @_;
+
+    my $valid = 0;
+    for my $hash ('sha256', 'sha1') {
+        # A mismatch returns false. A provider that refuses to use SHA-1 makes the library die with
+        # a message without any OpenSSL error text, e.g. "RSA.xs:1007: OpenSSL error: (null)".
+        # That is the expected outcome for SHA-1 in FIPS mode and just means no match.
+        $valid = eval {
+            $hash eq 'sha256' ? $rsa_pub->use_sha256_hash() : $rsa_pub->use_sha1_hash();
+            $rsa_pub->verify($full, $sig);
+        };
+        if (my $err = $@) {
+            # anything else is unexpected and should not go unnoticed
+            warn "unexpected error verifying $hash ticket signature - $err"
+                if $hash ne 'sha1' || $err !~ m/^RSA\.xs:\d+: OpenSSL error: \(null\) at /;
+        }
+        last if $valid;
+    }
+
+    $rsa_pub->use_sha1_hash();
+
+    return $valid;
+}
+
 sub verify_rsa_ticket {
     my ($rsa_pub, $prefix, $ticket, $secret_data, $min_age, $max_age, $noerr) = @_;
 
@@ -80,7 +113,7 @@ sub verify_rsa_ticket {
 
         my $full = defined($secret_data) ? "$plain:$secret_data" : $plain;
 
-        if ($rsa_pub->verify($full, decode_base64($sig))) {
+        if (verify_rsa_signature($rsa_pub, $full, decode_base64($sig))) {
             if ($plain =~ m/^\Q$prefix\E:(?:(\S+):)?([A-Z0-9]{8})$/) {
                 my $data = $1; # Note: not all tickets contains data
                 my $timestamp = $2;
diff --git a/test/ticket_test.pl b/test/ticket_test.pl
index e10ab97..ee4212a 100755
--- a/test/ticket_test.pl
+++ b/test/ticket_test.pl
@@ -10,6 +10,19 @@ use Test::More;
 
 use PVE::Ticket;
 
+# A verifier that, like a FIPS provider, dies when asked to verify with SHA-1, and that tracks the
+# digest that was set last.
+package RefusingSha1 {
+    sub new($class, $real, $error) { return bless { real => $real, error => $error }, $class }
+    sub use_sha1_hash($self) { $self->{hash} = 'sha1'; $self->{real}->use_sha1_hash() }
+    sub use_sha256_hash($self) { $self->{hash} = 'sha256'; $self->{real}->use_sha256_hash() }
+
+    sub verify($self, @args) {
+        die $self->{error} if $self->{hash} eq 'sha1';
+        return $self->{real}->verify(@args);
+    }
+}
+
 my $rsa_priv = Crypt::OpenSSL::RSA->generate_key(2048);
 my $rsa_pub = Crypt::OpenSSL::RSA->new_public_key($rsa_priv->get_public_key_string());
 
@@ -42,4 +55,78 @@ for my $data ('a%41b@pve', 'a%6fb@pve') {
     is(verify($ticket), undef, "ticket without percent escape for '$data' is rejected");
 }
 
+# Tickets signed by OpenSSL's command line for the key below ('PVE:root@pam:00000001', signed with
+# 'openssl dgst -sha1|-sha256 -sign'), so the verification does not depend on our own signing.
+my $fixed_pub_pem = <<'EOF_PEM';
+-----BEGIN PUBLIC KEY-----
+MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAoCOvd4Gtfa8X3sqIrKa3
+EbmtxvPhV9A0Ik9m3rveZ6rW8RkZdcXAB5lA21+ix/LPz40AGqHHTCIPXv6/uddv
+Os0DXXOM+vyqVfiqTQ3hpaXzzdM0Ip7bFOECuu7UON7haXvBij2Vnxs6kf1vBU7P
+BkDPsOo91OS2Lr1y8yRxEEPdT0CG2MF77cwQQHGufkXr13AvYaweFFASCTKVf+aG
+s09W5VasJYf8HOXfptmDxgn+DHAfwHOyBTu6f+pRQXFy82aQOIB3FPSR8877CM4w
+XaC4t0uZPQqwGVojJxRGbnhXru7/JgRbnZXcoF+QyaqDXpWp+t9iMzVQAnWk3XKh
+5wIDAQAB
+-----END PUBLIC KEY-----
+EOF_PEM
+
+my $fixed_sig = {
+    sha1 =>
+        'lH/tsT8EXCANTltNawgHdXMkjvsywEluoIK4rDVcDGtURSCy7XOo4IMx1tBycEafD8IDESBhlk08kO35Np2CebUHd49dTJOYg1rVm1Hl4QmT0kGmMCuPz4OttEID8UZ9UHGn+VQmmVFz2hyOoliQB80usiEo6moG3eN3ZJE8yJN9vZTJHsyg+DFgIf9YPOQFYSLnjOrIiWXsm2TQqp7MvICUewC/tQqHuLj1f4ZmKhI5rbQXOSp4/L6BGTJ5sgRP4Rdc3djyOuKlJmx48jtVVlK2RgpgnZXR2YVqACkK2R2mK3/JUgYCw2ZMRgu7hs5upx5gXigHPLZCF+FroGDMEg==',
+    sha256 =>
+        'mejkKQiURn2A9A49QwyfBqSW/oSgEq1YMD8YKXbmYp6kLY/VYwntVKogMrNZcEj9sWwkGiSXnx77defwbNfsHpWpBCalIJtH5m267Pkuk2j4p2bKG656wcBrU+Dscev+uuPDIwWke9QhrpyoFHODE7oJuWxrn3D1Cnlj+tZ2ps66a+XGyNVCznrCZFJuTcPuRvarkLYgBOOsjBOg2tsSw7Bee8xF+bDFF0TkyLRhizRPY10qKJnEyaLlEtvTZ51AcGCQPaMhLk9xJlflN8sqa3V8TUQdDBTox5HQMHR+n+H74xFR9UkXhI2sUmrk+ar0Az72Yy2Dzxe+IQZMCatLJg==',
+};
+
+# the ticket is from 1970, so use a huge maximum age
+my sub verify_fixed($hash, $pub, $noerr = 1) {
+    my $ticket = "PVE:root\@pam:00000001::$fixed_sig->{$hash}";
+    return PVE::Ticket::verify_rsa_ticket($pub, 'PVE', $ticket, undef, -20, 1e12, $noerr);
+}
+
+my $fixed_pub = Crypt::OpenSSL::RSA->new_public_key($fixed_pub_pem);
+for my $hash ('sha1', 'sha256') {
+    is(verify_fixed($hash, $fixed_pub), 'root@pam', "$hash signed ticket from openssl is accepted");
+    # without $noerr, a valid ticket must not die either
+    is(verify_fixed($hash, $fixed_pub, 0), 'root@pam',
+        "$hash signed ticket accepted without noerr");
+}
+is(eval { verify_fixed('sha256', $rsa_pub, 0) }, undef, "ticket of other key is rejected");
+like($@, qr/invalid PVE ticket/, "ticket of other key raises without noerr");
+
+my sub verify_refusing($hash, $error) {
+    my $pub = RefusingSha1->new(Crypt::OpenSSL::RSA->new_public_key($fixed_pub_pem), $error);
+    my @warnings;
+    local $SIG{__WARN__} = sub { push @warnings, @_ };
+    my $res = verify_fixed($hash, $pub);
+    return ($res, \@warnings, $pub->{hash});
+}
+
+my ($res, $warnings, $last_hash) =
+    verify_refusing('sha256', "RSA.xs:1: OpenSSL error: (null) at x.pl line 1.\n");
+is($res, 'root@pam', "sha256 ticket is accepted if SHA-1 is refused");
+is_deeply($warnings, [], "no warning if SHA-256 matched");
+
+($res, $warnings, $last_hash) =
+    verify_refusing('sha1', "RSA.xs:1: OpenSSL error: (null) at x.pl line 1.\n");
+is($res, undef, "sha1 ticket is rejected if SHA-1 is refused");
+is_deeply($warnings, [], "no warning for the expected SHA-1 refusal");
+is($last_hash, 'sha1', "verifier is left with the default digest after a refusal");
+
+($res, $warnings, $last_hash) = verify_refusing('sha1', "out of memory\n");
+is($res, undef, "sha1 ticket is rejected on unexpected error");
+is(scalar(@$warnings), 1, "unexpected error is warned about");
+like($warnings->[0] // '', qr/out of memory/, "warning has the error");
+
+($res, $warnings, $last_hash) = verify_refusing(
+    'sha1',
+    "RSA.xs:1: OpenSSL error: error:02000064:rsa routines::bad signature at x.pl line 1.\n",
+);
+is(scalar(@$warnings), 1, "other OpenSSL errors are warned about too");
+
+# the digest left on the verifier must not depend on which one matched
+for my $hash ('sha1', 'sha256') {
+    ($res, $warnings, $last_hash) =
+        verify_refusing($hash, "RSA.xs:1: OpenSSL error: (null) at x.pl line 1.\n");
+    is($last_hash, 'sha1', "verifier is left with the default digest for $hash ticket");
+}
+
 done_testing();



  reply	other threads:[~2026-11-13  3:34 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-13  3:06 [PATCH common v2 0/2] ticket: sign RSA tickets with SHA-256, opt-in Mark Atwood
2026-11-13  3:06 ` Mark Atwood [this message]
2026-11-17 16:05   ` [PATCH common v2 1/2] ticket: accept RSA tickets signed with SHA-256 Shannon Sterz
2026-11-13  3:06 ` [PATCH common v2 2/2] ticket: allow signing RSA tickets " Mark Atwood
2026-11-17 16:05   ` Shannon Sterz

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=20261113030639.3514-2-mark@wolfssl.com \
    --to=mark@wolfssl.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