Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow to pass through pem loading unsafe option #354

Merged
merged 1 commit into from
Apr 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 7 additions & 2 deletions jwcrypto/jwk.py
Original file line number Diff line number Diff line change
Expand Up @@ -339,6 +339,7 @@ def __init__(self, **kwargs):
super(JWK, self).__init__()
self._cache_pub_k = None
self._cache_pri_k = None
self.unsafe_skip_rsa_key_validation = False

if 'generate' in kwargs:
self.generate_key(**kwargs)
Expand Down Expand Up @@ -838,7 +839,9 @@ def _rsa_pub(self):
def _rsa_pri(self):
k = self._cache_pri_k
if k is None:
k = self._rsa_pri_n().private_key(default_backend())
u = self.unsafe_skip_rsa_key_validation
k = self._rsa_pri_n().private_key(default_backend(),
unsafe_skip_rsa_key_validation=u)
self._cache_pri_k = k
return k

Expand Down Expand Up @@ -993,8 +996,10 @@ def import_from_pem(self, data, password=None, kid=None):
"""

try:
u = self.unsafe_skip_rsa_key_validation
key = serialization.load_pem_private_key(
data, password=password, backend=default_backend())
data, password=password, backend=default_backend(),
unsafe_skip_rsa_key_validation=u)
except ValueError as e:
if password is not None:
raise e
Expand Down
10 changes: 10 additions & 0 deletions jwcrypto/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -757,6 +757,16 @@ def test_thumbprint_uri(self):
"urn:ietf:params:oauth:jwk-thumbprint:sha-256:{}".format(
PublicKeys['thumbprints'][1]))

def test_unsafe_rsa(self):
key = jwk.JWK()
key.unsafe_skip_rsa_key_validation = True
key.import_from_pem(RSAPrivatePEM, password=RSAPrivatePassword)
self.assertTrue(key.has_private)
# finally check private works
s = jws.JWS(payload='plaintext')
s.add_signature(key, None, {"alg": "PS256"})
s.serialize()


# RFC 7515 - A.1
A1_protected = \
Expand Down