Skip to content

Commit b4d4385

Browse files
authored
Merge pull request #168 from blag/rsa-privkey-no-verify
Just emit a warning when verifying with a private key
2 parents a9f8554 + 6a3865b commit b4d4385

File tree

7 files changed

+40
-4
lines changed

7 files changed

+40
-4
lines changed

.travis.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ matrix:
1212
include:
1313
# Linting
1414
- python: 3.6
15-
env: TOX_ENV=flake8
15+
env: TOXENV=flake8
1616
# CPython 2.7
1717
- python: 2.7
1818
env: TOXENV=py27-base

jose/backends/pycrypto_backend.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
from base64 import b64encode
22

33
import six
4+
import warnings
45

56
import Crypto.Hash.SHA256
67
import Crypto.Hash.SHA384
@@ -147,6 +148,9 @@ def sign(self, msg):
147148
raise JWKError(e)
148149

149150
def verify(self, msg, sig):
151+
if not self.is_public():
152+
warnings.warn("Attempting to verify a message with a private key. "
153+
"This is not recommended.")
150154
try:
151155
return PKCS1_v1_5.new(self.prepared_key).verify(self.hash_alg.new(msg), sig)
152156
except Exception:

jose/backends/rsa_backend.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
import binascii
22

33
import six
4+
import warnings
5+
46
from pyasn1.error import PyAsn1Error
57

68
import rsa as pyrsa
@@ -200,6 +202,9 @@ def sign(self, msg):
200202
return pyrsa.sign(msg, self._prepared_key, self.hash_alg)
201203

202204
def verify(self, msg, sig):
205+
if not self.is_public():
206+
warnings.warn("Attempting to verify a message with a private key. "
207+
"This is not recommended.")
203208
try:
204209
pyrsa.verify(msg, sig, self._prepared_key)
205210
return True

jose/utils.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11

22
import base64
3-
import hmac
43
import six
54
import struct
65
import sys

pytest.ini

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
[pytest]
2+
markers =
3+
pycrypto: marks tests as applicable with PyCrypto backend
4+
pycryptodome: marks tests as applicable with PyCryptodome backend
5+
ecdsa: marks tests as applicable with ecdsa backend
6+
cryptography: marks tests as applicable with cryptography backend
7+
backend_compatibility: mark tests as testing compatibility between backends

setup.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
import os
44
import platform
55

6-
import jose
6+
import jose # noqa: F401
77

88
from setuptools import setup
99

tests/test_jws.py

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,18 @@
11
import json
2+
import warnings
3+
4+
import pytest
25

36
from jose import jwk
47
from jose import jws
8+
from jose.backends import RSAKey
59
from jose.constants import ALGORITHMS
610
from jose.exceptions import JWSError
711

8-
import pytest
12+
try:
13+
from jose.backends.cryptography_backend import CryptographyRSAKey
14+
except ImportError:
15+
CryptographyRSAKey = None
916

1017

1118
@pytest.fixture
@@ -291,6 +298,20 @@ def test_wrong_key(self, payload):
291298
with pytest.raises(JWSError):
292299
jws.verify(token, rsa_public_key, ALGORITHMS.HS256)
293300

301+
@pytest.mark.skipif(RSAKey is CryptographyRSAKey, reason="Cryptography backend outright fails verification")
302+
def test_private_verify_raises_warning(self, payload):
303+
token = jws.sign(payload, rsa_private_key, algorithm='RS256')
304+
305+
# verify with public
306+
jws.verify(token, rsa_public_key, algorithms='RS256')
307+
308+
with warnings.catch_warnings(record=True) as w:
309+
# verify with private raises warning
310+
jws.verify(token, rsa_private_key, algorithms='RS256')
311+
312+
assert ("Attempting to verify a message with a private key. "
313+
"This is not recommended.") == str(w[-1].message)
314+
294315

295316
ec_private_key = """-----BEGIN EC PRIVATE KEY-----
296317
MIHcAgEBBEIBzs13YUnYbLfYXTz4SG4DE4rPmsL3wBTdy34JcO+BDpI+NDZ0pqam

0 commit comments

Comments
 (0)