From 152d1e664b8f5cb24f3c2b968dda0c1ec0f61a26 Mon Sep 17 00:00:00 2001 From: neeraj netrakar Date: Fri, 23 Oct 2026 15:02:10 +0530 Subject: [PATCH] Check the signature hash in the python-rsa RSAKey.verify rsa.verify() reads the hash algorithm from the DigestInfo in the signature and returns its name, so an RS256 key accepted PKCS#1 v1.5 signatures over MD5, SHA-1 or SHA-512 made by the same key. Compare the returned name with the key's hash_alg. --- jose/backends/rsa_backend.py | 3 +-- tests/algorithms/test_RSA.py | 8 ++++++++ 2 files changed, 9 insertions(+), 2 deletions(-) diff --git a/jose/backends/rsa_backend.py b/jose/backends/rsa_backend.py index 8139d698..4da5b034 100644 --- a/jose/backends/rsa_backend.py +++ b/jose/backends/rsa_backend.py @@ -207,8 +207,7 @@ def verify(self, msg, sig): if not self.is_public(): warnings.warn("Attempting to verify a message with a private key. " "This is not recommended.") try: - pyrsa.verify(msg, sig, self._prepared_key) - return True + return pyrsa.verify(msg, sig, self._prepared_key) == self.hash_alg except pyrsa.pkcs1.VerificationError: return False diff --git a/tests/algorithms/test_RSA.py b/tests/algorithms/test_RSA.py index b181993c..15c16b26 100644 --- a/tests/algorithms/test_RSA.py +++ b/tests/algorithms/test_RSA.py @@ -217,6 +217,14 @@ def test_python_rsa_legacy_private_key_pkcs8_to_pkcs1_invalid(self): excinfo.match("Invalid private key encoding") + @pytest.mark.parametrize("hash_method", ["MD5", "SHA-1", "SHA-512"]) + def test_python_rsa_verify_rejects_other_hash(self, hash_method): + key = PurePythonRSAKey(private_key_4096_pkcs1, ALGORITHMS.RS256) + msg = b"test" + sig = rsa_backend.pyrsa.sign(msg, key._prepared_key, hash_method) + + assert not key.public_key().verify(msg, sig) + @pytest.mark.cryptography @pytest.mark.skipif(