Skip to content

Commit 930dd5f

Browse files
authored
Merge pull request #61 from IdentityPython/rsa_import
Work around bad RSAKey import
2 parents 0569939 + dbbe46d commit 930dd5f

22 files changed

+131
-43
lines changed

src/cryptojwt/jwe/__init__.py

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,14 @@
2222
"ECDH-ES+A192KW",
2323
"ECDH-ES+A256KW",
2424
],
25-
"enc": ["A128CBC-HS256", "A192CBC-HS384", "A256CBC-HS512", "A128GCM", "A192GCM", "A256GCM",],
25+
"enc": [
26+
"A128CBC-HS256",
27+
"A192CBC-HS384",
28+
"A256CBC-HS512",
29+
"A128GCM",
30+
"A192GCM",
31+
"A256GCM",
32+
],
2633
}
2734

2835

src/cryptojwt/jwe/aes.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,7 @@
1818

1919

2020
class AES_CBCEncrypter(Encrypter):
21-
"""
22-
"""
21+
""""""
2322

2423
def __init__(self, key_len=32, key=None, msg_padding="PKCS7"):
2524
Encrypter.__init__(self)

src/cryptojwt/jwe/jwe_ec.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -157,7 +157,12 @@ def dec_setup(self, token, key=None, **kwargs):
157157
raise Exception("Unknown key length for algorithm")
158158

159159
self.cek = ecdh_derive_key(
160-
key, epubkey.pub_key, apu, apv, str(self.headers["enc"]).encode(), dk_len,
160+
key,
161+
epubkey.pub_key,
162+
apu,
163+
apv,
164+
str(self.headers["enc"]).encode(),
165+
dk_len,
161166
)
162167
elif self.headers["alg"] in [
163168
"ECDH-ES+A128KW",

src/cryptojwt/jwe/jwe_rsa.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ def encrypt(self, key, iv="", cek="", **kwargs):
8585
return jwe.pack(parts=[jwe_enc_key, iv, ctxt, tag])
8686

8787
def decrypt(self, token, key, cek=None):
88-
""" Decrypts a JWT
88+
"""Decrypts a JWT
8989
9090
:param token: The JWT
9191
:param key: A key to use for decrypting

src/cryptojwt/jwe/jwekey.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ def alg2keytype(self, alg):
3838
return alg2keytype(alg)
3939

4040
def enc_setup(self, enc_alg, msg, auth_data=b"", key=None, iv=""):
41-
""" Encrypt JWE content.
41+
"""Encrypt JWE content.
4242
4343
:param enc_alg: The JWE "enc" value specifying the encryption algorithm
4444
:param msg: The plain text message
@@ -62,7 +62,7 @@ def enc_setup(self, enc_alg, msg, auth_data=b"", key=None, iv=""):
6262

6363
@staticmethod
6464
def _decrypt(enc, key, ctxt, iv, tag, auth_data=b""):
65-
""" Decrypt JWE content.
65+
"""Decrypt JWE content.
6666
6767
:param enc: The JWE "enc" value specifying the encryption algorithm
6868
:param key: Key (CEK)

src/cryptojwt/jwe/rsa.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,9 @@ def encrypt(self, msg, key, sign_padding="pkcs1_padding"):
2020
return key.encrypt(
2121
msg,
2222
_padding(
23-
mgf=padding.MGF1(algorithm=_chosen_hash()), algorithm=_chosen_hash(), label=None,
23+
mgf=padding.MGF1(algorithm=_chosen_hash()),
24+
algorithm=_chosen_hash(),
25+
label=None,
2426
),
2527
)
2628

src/cryptojwt/jwk/jwk.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,9 @@ def key_from_jwk_dict(jwk_dict, private=None):
9393
else:
9494
# Ecdsa public key.
9595
ec_pub_numbers = ec.EllipticCurvePublicNumbers(
96-
base64url_to_long(_jwk_dict["x"]), base64url_to_long(_jwk_dict["y"]), curve,
96+
base64url_to_long(_jwk_dict["x"]),
97+
base64url_to_long(_jwk_dict["y"]),
98+
curve,
9799
)
98100
_jwk_dict["pub_key"] = ec_pub_numbers.public_key(backends.default_backend())
99101
return ECKey(**_jwk_dict)

src/cryptojwt/jwk/rsa.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,7 @@ def rsa_eq(key1, key2):
128128

129129

130130
def x509_rsa_load(txt):
131-
""" So I get the same output format as loads produces
131+
"""So I get the same output format as loads produces
132132
:param txt:
133133
:return:
134134
"""
@@ -172,10 +172,10 @@ def rsa_construct_private(numbers):
172172
try:
173173
cnum["iqmp"] = numbers["di"]
174174
except KeyError:
175-
cnum["iqmp"] = rsa.rsa_crt_iqmp(cnum["p"], cnum["p"])
175+
cnum["iqmp"] = rsa.rsa_crt_iqmp(cnum["p"], cnum["q"])
176176
else:
177177
if not numbers["di"]:
178-
cnum["iqmp"] = rsa.rsa_crt_iqmp(cnum["p"], cnum["p"])
178+
cnum["iqmp"] = rsa.rsa_crt_iqmp(cnum["p"], cnum["q"])
179179

180180
rpubn = rsa.RSAPublicNumbers(e=numbers["e"], n=numbers["n"])
181181
rprivn = rsa.RSAPrivateNumbers(public_numbers=rpubn, **cnum)

src/cryptojwt/jws/jws.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -321,7 +321,11 @@ def verify_json(self, jws, keys=None, allow_none=False, at_least_one=False):
321321
for _sign in _signs:
322322
protected_headers = _sign.get("protected", "")
323323
token = b".".join(
324-
[protected_headers.encode(), _payload.encode(), _sign["signature"].encode(),]
324+
[
325+
protected_headers.encode(),
326+
_payload.encode(),
327+
_sign["signature"].encode(),
328+
]
325329
)
326330

327331
unprotected_headers = _sign.get("header", {})

src/cryptojwt/jws/pss.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,8 @@ def sign(self, msg, key):
3838
sig = key.sign(
3939
digest,
4040
padding.PSS(
41-
mgf=padding.MGF1(self.hash_algorithm()), salt_length=padding.PSS.MAX_LENGTH,
41+
mgf=padding.MGF1(self.hash_algorithm()),
42+
salt_length=padding.PSS.MAX_LENGTH,
4243
),
4344
utils.Prehashed(self.hash_algorithm()),
4445
)
@@ -59,7 +60,8 @@ def verify(self, msg, signature, key):
5960
signature,
6061
msg,
6162
padding.PSS(
62-
mgf=padding.MGF1(self.hash_algorithm()), salt_length=padding.PSS.MAX_LENGTH,
63+
mgf=padding.MGF1(self.hash_algorithm()),
64+
salt_length=padding.PSS.MAX_LENGTH,
6365
),
6466
self.hash_algorithm(),
6567
)

0 commit comments

Comments
 (0)