Skip to content

Commit 8df18d6

Browse files
authored
Merge pull request #158 from IdentityPython/cryptography_update
Cryptography fixes
2 parents 727bb9b + 0ee8d4a commit 8df18d6

17 files changed

+42
-58
lines changed

pyproject.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ exclude_lines = [
2222

2323
[tool.poetry]
2424
name = "cryptojwt"
25-
version = "1.9.0"
25+
version = "1.9.1"
2626
description = "Python implementation of JWT, JWE, JWS and JWK"
2727
authors = ["Roland Hedberg <[email protected]>"]
2828
license = "Apache-2.0"

src/cryptojwt/__init__.py

+1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
"""JSON Web Token"""
2+
23
import logging
34

45
import pkg_resources

src/cryptojwt/jwe/aes.py

+3-4
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
import os
22
from struct import pack
33

4-
from cryptography.hazmat.backends import default_backend
54
from cryptography.hazmat.primitives import hmac
65
from cryptography.hazmat.primitives.ciphers import Cipher
76
from cryptography.hazmat.primitives.ciphers import algorithms
@@ -37,7 +36,7 @@ def __init__(self, key_len=32, key=None, msg_padding="PKCS7"):
3736

3837
def _mac(self, hash_key, hash_func, auth_data, iv, enc_msg, key_len):
3938
al = pack("!Q", 8 * len(auth_data))
40-
h = hmac.HMAC(hash_key, hash_func(), backend=default_backend())
39+
h = hmac.HMAC(hash_key, hash_func())
4140
h.update(auth_data)
4241
h.update(iv)
4342
h.update(enc_msg)
@@ -54,7 +53,7 @@ def encrypt(self, msg, iv="", auth_data=b""):
5453

5554
hash_key, enc_key, key_len, hash_func = get_keys_seclen_dgst(self.key, iv)
5655

57-
cipher = Cipher(algorithms.AES(enc_key), modes.CBC(iv), backend=default_backend())
56+
cipher = Cipher(algorithms.AES(enc_key), modes.CBC(iv))
5857
encryptor = cipher.encryptor()
5958

6059
pmsg = self.padder.update(msg)
@@ -77,7 +76,7 @@ def decrypt(self, msg, iv="", auth_data=b"", tag=b"", key=None):
7776
if comp_tag != tag:
7877
raise VerificationError("AES-CBC HMAC")
7978

80-
cipher = Cipher(algorithms.AES(enc_key), modes.CBC(iv), backend=default_backend())
79+
cipher = Cipher(algorithms.AES(enc_key), modes.CBC(iv))
8180
decryptor = cipher.decryptor()
8281

8382
ctext = decryptor.update(msg)

src/cryptojwt/jwe/jwe_ec.py

+3-4
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
import struct
22

3-
from cryptography.hazmat.backends import default_backend
43
from cryptography.hazmat.primitives.asymmetric import ec
54
from cryptography.hazmat.primitives.keywrap import aes_key_unwrap
65
from cryptography.hazmat.primitives.keywrap import aes_key_wrap
@@ -87,7 +86,7 @@ def enc_setup(self, msg, key=None, auth_data=b"", **kwargs):
8786
try:
8887
_epk = kwargs["epk"]
8988
except KeyError:
90-
_epk = ec.generate_private_key(NIST2SEC[as_unicode(key.crv)], default_backend())
89+
_epk = ec.generate_private_key(curve=NIST2SEC[as_unicode(key.crv)]())
9190
epk = ECKey().load_key(_epk.public_key())
9291
else:
9392
if isinstance(_epk, ec.EllipticCurvePrivateKey):
@@ -120,7 +119,7 @@ def enc_setup(self, msg, key=None, auth_data=b"", **kwargs):
120119
klen = int(_post[1:4])
121120
kek = ecdh_derive_key(_epk, key.pub_key, apu, apv, str(_post).encode(), klen)
122121
cek = self._generate_key(self.enc, cek=cek)
123-
encrypted_key = aes_key_wrap(kek, cek, default_backend())
122+
encrypted_key = aes_key_wrap(kek, cek)
124123
else:
125124
raise Exception("Unsupported algorithm %s" % self.alg)
126125

@@ -172,7 +171,7 @@ def dec_setup(self, token, key=None, **kwargs):
172171
_pre, _post = self.headers["alg"].split("+")
173172
klen = int(_post[1:4])
174173
kek = ecdh_derive_key(key, epubkey.pub_key, apu, apv, str(_post).encode(), klen)
175-
self.cek = aes_key_unwrap(kek, token.encrypted_key(), default_backend())
174+
self.cek = aes_key_unwrap(kek, token.encrypted_key())
176175
else:
177176
raise Exception("Unsupported algorithm %s" % self.headers["alg"])
178177

src/cryptojwt/jwe/jwe_hmac.py

+2-3
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
import logging
22
import zlib
33

4-
from cryptography.hazmat.backends import default_backend
54
from cryptography.hazmat.primitives.keywrap import aes_key_unwrap
65
from cryptography.hazmat.primitives.keywrap import aes_key_wrap
76

@@ -57,7 +56,7 @@ def encrypt(self, key, iv="", cek="", **kwargs):
5756

5857
# The iv for this function must be 64 bit
5958
# Which is certainly different from the one above
60-
jek = aes_key_wrap(kek, cek, default_backend())
59+
jek = aes_key_wrap(kek, cek)
6160

6261
_enc = self["enc"]
6362
_auth_data = jwe.b64_encode_header()
@@ -85,7 +84,7 @@ def decrypt(self, token, key=None, cek=None):
8584
except AttributeError:
8685
key = key.key
8786
# The iv for this function must be 64 bit
88-
cek = aes_key_unwrap(key, jek, default_backend())
87+
cek = aes_key_unwrap(key, jek)
8988

9089
auth_data = jwe.b64_protected_header()
9190
msg = self._decrypt(

src/cryptojwt/jwe/utils.py

+1-2
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
import struct
33
from math import ceil
44

5-
from cryptography.hazmat.backends import default_backend
65
from cryptography.hazmat.primitives import hashes
76
from cryptography.hazmat.primitives.hashes import SHA256
87
from cryptography.hazmat.primitives.hashes import SHA384
@@ -107,7 +106,7 @@ def concat_sha256(secret, dk_len, other_info):
107106
while len(dkm) < dk_bytes:
108107
counter += 1
109108
counter_bytes = struct.pack("!I", counter)
110-
digest = hashes.Hash(hashes.SHA256(), backend=default_backend())
109+
digest = hashes.Hash(hashes.SHA256())
111110
digest.update(counter_bytes)
112111
digest.update(secret)
113112
digest.update(other_info)

src/cryptojwt/jwk/ec.py

+3-4
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
from cryptography.hazmat.backends import default_backend
21
from cryptography.hazmat.primitives.asymmetric import ec
32

43
from cryptojwt.exception import KeyNotFound
@@ -50,7 +49,7 @@ def ec_construct_public(num):
5049
raise UnsupportedECurve("Unsupported elliptic curve: {}".format(num["crv"]))
5150

5251
ecpn = ec.EllipticCurvePublicNumbers(num["x"], num["y"], _sec_crv())
53-
return ecpn.public_key(default_backend())
52+
return ecpn.public_key()
5453

5554

5655
def ec_construct_private(num):
@@ -64,7 +63,7 @@ def ec_construct_private(num):
6463
"""
6564
pub_ecpn = ec.EllipticCurvePublicNumbers(num["x"], num["y"], NIST2SEC[as_unicode(num["crv"])]())
6665
priv_ecpn = ec.EllipticCurvePrivateNumbers(num["d"], pub_ecpn)
67-
return priv_ecpn.private_key(default_backend())
66+
return priv_ecpn.private_key()
6867

6968

7069
class ECKey(AsymmetricKey):
@@ -285,7 +284,7 @@ def cmp_keys(a, b, key_type):
285284

286285

287286
def new_ec_key(crv, kid="", **kwargs):
288-
_key = ec.generate_private_key(curve=NIST2SEC[crv], backend=default_backend())
287+
_key = ec.generate_private_key(curve=NIST2SEC[crv]())
289288

290289
_rk = ECKey(priv_key=_key, kid=kid, **kwargs)
291290
if not kid:

src/cryptojwt/jwk/jwk.py

+4-7
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
import json
33
import os
44

5-
from cryptography.hazmat import backends
65
from cryptography.hazmat.primitives.asymmetric import ec
76
from cryptography.hazmat.primitives.asymmetric import ed448
87
from cryptography.hazmat.primitives.asymmetric import ed25519
@@ -105,9 +104,7 @@ def key_from_jwk_dict(jwk_dict, private=None):
105104

106105
if _jwk_dict.get("d", None) is not None:
107106
# Ecdsa private key.
108-
_jwk_dict["priv_key"] = ec.derive_private_key(
109-
base64url_to_long(_jwk_dict["d"]), curve, backends.default_backend()
110-
)
107+
_jwk_dict["priv_key"] = ec.derive_private_key(base64url_to_long(_jwk_dict["d"]), curve)
111108
_jwk_dict["pub_key"] = _jwk_dict["priv_key"].public_key()
112109
else:
113110
# Ecdsa public key.
@@ -116,7 +113,7 @@ def key_from_jwk_dict(jwk_dict, private=None):
116113
base64url_to_long(_jwk_dict["y"]),
117114
curve,
118115
)
119-
_jwk_dict["pub_key"] = ec_pub_numbers.public_key(backends.default_backend())
116+
_jwk_dict["pub_key"] = ec_pub_numbers.public_key()
120117
return ECKey(**_jwk_dict)
121118
elif _jwk_dict["kty"] == "RSA":
122119
ensure_rsa_params(_jwk_dict, private)
@@ -151,10 +148,10 @@ def key_from_jwk_dict(jwk_dict, private=None):
151148
rsa_priv_numbers = rsa.RSAPrivateNumbers(
152149
p_long, q_long, d_long, dp_long, dq_long, qi_long, rsa_pub_numbers
153150
)
154-
_jwk_dict["priv_key"] = rsa_priv_numbers.private_key(backends.default_backend())
151+
_jwk_dict["priv_key"] = rsa_priv_numbers.private_key()
155152
_jwk_dict["pub_key"] = _jwk_dict["priv_key"].public_key()
156153
else:
157-
_jwk_dict["pub_key"] = rsa_pub_numbers.public_key(backends.default_backend())
154+
_jwk_dict["pub_key"] = rsa_pub_numbers.public_key()
158155

159156
if _jwk_dict["kty"] != "RSA":
160157
raise WrongKeyType('"{}" should have been "RSA"'.format(_jwk_dict["kty"]))

src/cryptojwt/jwk/rsa.py

+4-9
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
import base64
22
import logging
33

4-
from cryptography.hazmat.backends import default_backend
54
from cryptography.hazmat.primitives import serialization
65
from cryptography.hazmat.primitives.asymmetric import rsa
76

@@ -40,9 +39,7 @@ def generate_and_store_rsa_key(key_size=2048, filename="rsa.key", passphrase="")
4039
:return: A
4140
cryptography.hazmat.primitives.asymmetric.rsa.RSAPrivateKey instance
4241
"""
43-
private_key = rsa.generate_private_key(
44-
public_exponent=65537, key_size=key_size, backend=default_backend()
45-
)
42+
private_key = rsa.generate_private_key(public_exponent=65537, key_size=key_size)
4643

4744
with open(filename, "wb") as keyfile:
4845
if passphrase:
@@ -141,7 +138,7 @@ def x509_rsa_load(txt):
141138

142139
def rsa_construct_public(numbers):
143140
rpn = rsa.RSAPublicNumbers(**numbers)
144-
return rpn.public_key(default_backend())
141+
return rpn.public_key()
145142

146143

147144
def rsa_construct_private(numbers):
@@ -181,7 +178,7 @@ def rsa_construct_private(numbers):
181178

182179
rpubn = rsa.RSAPublicNumbers(e=numbers["e"], n=numbers["n"])
183180
rprivn = rsa.RSAPrivateNumbers(public_numbers=rpubn, **cnum)
184-
return rprivn.private_key(default_backend())
181+
return rprivn.private_key()
185182

186183

187184
def cmp_public_numbers(pn1, pn2):
@@ -492,9 +489,7 @@ def new_rsa_key(key_size=2048, kid="", public_exponent=65537, **kwargs):
492489
:return: A :py:class:`cryptojwt.jwk.rsa.RSAKey` instance
493490
"""
494491

495-
_key = rsa.generate_private_key(
496-
public_exponent=public_exponent, key_size=key_size, backend=default_backend()
497-
)
492+
_key = rsa.generate_private_key(public_exponent=public_exponent, key_size=key_size)
498493

499494
_rk = RSAKey(priv_key=_key, kid=kid, **kwargs)
500495
if not _rk.kid:

src/cryptojwt/jwk/x509.py

+5-8
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
import logging
44

55
from cryptography import x509
6-
from cryptography.hazmat.backends import default_backend
76
from cryptography.hazmat.primitives import serialization
87
from cryptography.hazmat.primitives.asymmetric import ec
98
from cryptography.hazmat.primitives.asymmetric import rsa
@@ -22,7 +21,7 @@ def import_public_key_from_pem_file(filename):
2221
:return: A public key instance
2322
"""
2423
with open(filename, "rb") as key_file:
25-
public_key = serialization.load_pem_public_key(key_file.read(), backend=default_backend())
24+
public_key = serialization.load_pem_public_key(key_file.read())
2625
return public_key
2726

2827

@@ -35,9 +34,7 @@ def import_private_key_from_pem_file(filename, passphrase=None):
3534
:return: A private key instance
3635
"""
3736
with open(filename, "rb") as key_file:
38-
private_key = serialization.load_pem_private_key(
39-
key_file.read(), password=passphrase, backend=default_backend()
40-
)
37+
private_key = serialization.load_pem_private_key(key_file.read(), password=passphrase)
4138
return private_key
4239

4340

@@ -56,7 +53,7 @@ def import_public_key_from_pem_data(pem_data):
5653
pem_data = bytes("{}\n{}\n{}".format(PREFIX, pem_data, POSTFIX), "utf-8")
5754
else:
5855
pem_data = bytes(pem_data, "utf-8")
59-
cert = x509.load_pem_x509_certificate(pem_data, default_backend())
56+
cert = x509.load_pem_x509_certificate(pem_data)
6057
return cert.public_key()
6158

6259

@@ -68,7 +65,7 @@ def import_public_key_from_cert_file(filename):
6865
:return: A public key instance
6966
"""
7067
with open(filename, "rb") as key_file:
71-
cert = x509.load_pem_x509_certificate(key_file.read(), backend=default_backend())
68+
cert = x509.load_pem_x509_certificate(key_file.read())
7269
return cert.public_key()
7370

7471

@@ -81,7 +78,7 @@ def der_cert(der_data):
8178
"""
8279
if isinstance(der_data, str):
8380
der_data = bytes(der_data, "utf-8")
84-
return x509.load_der_x509_certificate(der_data, default_backend())
81+
return x509.load_der_x509_certificate(der_data)
8582

8683

8784
def load_x509_cert(url, httpc, spec2key, **get_args):

src/cryptojwt/jws/hmac.py

+2-3
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
from cryptography.hazmat.backends import default_backend
21
from cryptography.hazmat.primitives import hashes
32
from cryptography.hazmat.primitives import hmac
43

@@ -26,7 +25,7 @@ def sign(self, msg, key):
2625
:param key: The key
2726
:return: A signature
2827
"""
29-
h = hmac.HMAC(key, self.algorithm(), default_backend())
28+
h = hmac.HMAC(key, self.algorithm())
3029
h.update(msg)
3130
return h.finalize()
3231

@@ -41,7 +40,7 @@ def verify(self, msg, sig, key):
4140
Exception.
4241
"""
4342
try:
44-
h = hmac.HMAC(key, self.algorithm(), default_backend())
43+
h = hmac.HMAC(key, self.algorithm())
4544
h.update(msg)
4645
h.verify(sig)
4746
return True

src/cryptojwt/jws/jws.py

+1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
"""JSON Web Token"""
2+
23
import json
34
import logging
45

src/cryptojwt/jws/pss.py

+1-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
import logging
22

33
from cryptography.exceptions import InvalidSignature
4-
from cryptography.hazmat.backends import default_backend
54
from cryptography.hazmat.primitives import hashes
65
from cryptography.hazmat.primitives.asymmetric import padding
76
from cryptography.hazmat.primitives.asymmetric import utils
@@ -32,7 +31,7 @@ def sign(self, msg, key):
3231
:param key: The key
3332
:return: A signature
3433
"""
35-
hasher = hashes.Hash(self.hash_algorithm(), backend=default_backend())
34+
hasher = hashes.Hash(self.hash_algorithm())
3635
hasher.update(msg)
3736
digest = hasher.finalize()
3837
sig = key.sign(

src/cryptojwt/jwt.py

+1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
"""Basic JSON Web Token implementation."""
2+
23
import json
34
import logging
45
import time

src/cryptojwt/jwx.py

+1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
"""A basic class on which to build the JWS and JWE classes."""
2+
23
import json
34
import logging
45
import warnings

0 commit comments

Comments
 (0)