Skip to content

Commit 40920a5

Browse files
author
Gasper Zejn
committed
Disallow verifying signatures with private RSA keys.
Some backends are smart and know how to verify with private keys too. Disallow that on those backends.
1 parent deea760 commit 40920a5

File tree

3 files changed

+14
-0
lines changed

3 files changed

+14
-0
lines changed

jose/backends/pycrypto_backend.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,8 @@ def sign(self, msg):
147147
raise JWKError(e)
148148

149149
def verify(self, msg, sig):
150+
if not self.is_public():
151+
return False
150152
try:
151153
return PKCS1_v1_5.new(self.prepared_key).verify(self.hash_alg.new(msg), sig)
152154
except Exception:

jose/backends/rsa_backend.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -200,6 +200,8 @@ def sign(self, msg):
200200
return pyrsa.sign(msg, self._prepared_key, self.hash_alg)
201201

202202
def verify(self, msg, sig):
203+
if not self.is_public():
204+
return False
203205
try:
204206
pyrsa.verify(msg, sig, self._prepared_key)
205207
return True

tests/test_jws.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -291,6 +291,16 @@ def test_wrong_key(self, payload):
291291
with pytest.raises(JWSError):
292292
jws.verify(token, rsa_public_key, ALGORITHMS.HS256)
293293

294+
def test_private_verify(self, payload):
295+
token = jws.sign(payload, rsa_private_key, algorithm='RS256')
296+
297+
# verify with public
298+
dec = jws.verify(token, rsa_public_key, algorithms='RS256')
299+
300+
with pytest.raises(JWSError):
301+
# verify with private does not work
302+
dec = jws.verify(token, rsa_private_key, algorithms='RS256')
303+
294304

295305
ec_private_key = """-----BEGIN EC PRIVATE KEY-----
296306
MIHcAgEBBEIBzs13YUnYbLfYXTz4SG4DE4rPmsL3wBTdy34JcO+BDpI+NDZ0pqam

0 commit comments

Comments
 (0)