Skip to content

Commit e10e173

Browse files
committed
fix: remove backend parameter, it is deprecated
1 parent 734121d commit e10e173

File tree

6 files changed

+18
-32
lines changed

6 files changed

+18
-32
lines changed

src/joserfc/_rfc7517/pem.py

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -16,38 +16,37 @@
1616
BestAvailableEncryption,
1717
NoEncryption,
1818
)
19-
from cryptography.hazmat.backends import default_backend
2019
from .models import NativeKeyBinding, GenericKey
2120
from .types import DictKey
2221
from ..errors import InvalidKeyTypeError
2322
from ..util import to_bytes
2423

2524

2625
def import_from_ssh_key(raw: bytes) -> Any:
27-
return load_ssh_public_key(raw, backend=default_backend())
26+
return load_ssh_public_key(raw)
2827

2928

3029
def import_from_pem_key(raw: bytes, password: bytes | None = None) -> Any:
3130
key: Any
3231

3332
if b"OPENSSH PRIVATE" in raw:
34-
key = load_ssh_private_key(raw, password=password, backend=default_backend())
33+
key = load_ssh_private_key(raw, password=password)
3534

3635
elif b"PUBLIC" in raw:
37-
key = load_pem_public_key(raw, backend=default_backend())
36+
key = load_pem_public_key(raw)
3837

3938
elif b"PRIVATE" in raw:
40-
key = load_pem_private_key(raw, password=password, backend=default_backend())
39+
key = load_pem_private_key(raw, password=password)
4140

4241
elif b"CERTIFICATE" in raw:
43-
cert = load_pem_x509_certificate(raw, backend=default_backend())
42+
cert = load_pem_x509_certificate(raw)
4443
return cert.public_key()
4544

4645
else:
4746
try:
48-
key = load_der_private_key(raw, password=password, backend=default_backend())
47+
key = load_der_private_key(raw, password=password)
4948
except ValueError:
50-
key = load_der_public_key(raw, backend=default_backend())
49+
key = load_der_public_key(raw)
5150
return key
5251

5352

src/joserfc/_rfc7518/derive_key.py

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
from __future__ import annotations
22
import struct
33
from cryptography.hazmat.primitives import hashes
4-
from cryptography.hazmat.backends import default_backend
54
from cryptography.hazmat.primitives.kdf.concatkdf import ConcatKDFHash
65
from ..registry import Header
76
from ..util import to_bytes, urlsafe_b64decode
@@ -40,7 +39,6 @@ def derive_key_for_concat_kdf(
4039
algorithm=hashes.SHA256(),
4140
length=bit_size // 8,
4241
otherinfo=fixed_info,
43-
backend=default_backend(),
4442
)
4543
return ckdf.derive(shared_key)
4644

src/joserfc/_rfc7518/jwe_algs.py

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
import secrets
33
from cryptography.hazmat.primitives.asymmetric import padding
44
from cryptography.hazmat.primitives import hashes
5-
from cryptography.hazmat.backends import default_backend
65
from cryptography.hazmat.primitives.keywrap import (
76
aes_key_wrap,
87
aes_key_unwrap,
@@ -92,12 +91,12 @@ def __init__(self, key_size: int, recommended: bool = False):
9291

9392
def wrap_cek(self, cek: bytes, key: bytes) -> bytes:
9493
self.check_op_key(key)
95-
return aes_key_wrap(key, cek, default_backend())
94+
return aes_key_wrap(key, cek)
9695

9796
def unwrap_cek(self, ek: bytes, key: bytes) -> bytes:
9897
self.check_op_key(key)
9998
try:
100-
cek = aes_key_unwrap(key, ek, default_backend())
99+
cek = aes_key_unwrap(key, ek)
101100
except InvalidUnwrap:
102101
raise DecodeError("Unwrap AES key failed")
103102
return cek
@@ -148,7 +147,7 @@ def encrypt_cek(self, cek: bytes, recipient: Recipient[OctKey]) -> bytes:
148147
iv_size = 96
149148
iv = secrets.token_bytes(iv_size // 8)
150149

151-
cipher = Cipher(AES(op_key), GCM(iv), backend=default_backend())
150+
cipher = Cipher(AES(op_key), GCM(iv))
152151
enc = cipher.encryptor()
153152

154153
encrypted_key = enc.update(cek) + enc.finalize()
@@ -169,7 +168,7 @@ def decrypt_cek(self, recipient: Recipient[OctKey]) -> bytes:
169168
iv = urlsafe_b64decode(to_bytes(headers["iv"]))
170169
tag = urlsafe_b64decode(to_bytes(headers["tag"]))
171170

172-
cipher = Cipher(AES(op_key), GCM(iv, tag), backend=default_backend())
171+
cipher = Cipher(AES(op_key), GCM(iv, tag))
173172
d = cipher.decryptor()
174173
try:
175174
assert recipient.encrypted_key is not None
@@ -252,7 +251,6 @@ def compute_derived_key(self, key: bytes, p2s: bytes, p2c: int) -> bytes:
252251
length=self.key_size // 8,
253252
salt=salt,
254253
iterations=p2c,
255-
backend=default_backend(),
256254
)
257255
return kdf.derive(key)
258256

src/joserfc/_rfc7518/jwe_encs.py

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@
1111
from __future__ import annotations
1212
import hmac
1313
import hashlib
14-
from cryptography.hazmat.backends import default_backend
1514
from cryptography.hazmat.primitives.ciphers import Cipher
1615
from cryptography.hazmat.primitives.ciphers.algorithms import AES
1716
from cryptography.hazmat.primitives.ciphers.modes import GCM, CBC
@@ -54,7 +53,7 @@ def encrypt(self, plaintext: bytes, cek: bytes, iv: bytes, aad: bytes) -> tuple[
5453
pad = PKCS7(AES.block_size).padder()
5554
padded_data = pad.update(plaintext) + pad.finalize()
5655

57-
cipher = Cipher(AES(ekey), CBC(iv), backend=default_backend())
56+
cipher = Cipher(AES(ekey), CBC(iv))
5857
enc = cipher.encryptor()
5958
ciphertext = enc.update(padded_data) + enc.finalize()
6059
tag = self._hmac(ciphertext, aad, iv, hkey)
@@ -69,7 +68,7 @@ def decrypt(self, ciphertext: bytes, tag: bytes, cek: bytes, iv: bytes, aad: byt
6968
if not hmac.compare_digest(ctag, tag):
7069
raise DecodeError("tag does not match")
7170

72-
cipher = Cipher(AES(dkey), CBC(iv), backend=default_backend())
71+
cipher = Cipher(AES(dkey), CBC(iv))
7372
d = cipher.decryptor()
7473
data = d.update(ciphertext) + d.finalize()
7574
unpad = PKCS7(AES.block_size).unpadder()
@@ -90,15 +89,15 @@ def __init__(self, key_size: int):
9089

9190
def encrypt(self, plaintext: bytes, cek: bytes, iv: bytes, aad: bytes) -> tuple[bytes, bytes]:
9291
"""Key Encryption with AES GCM"""
93-
cipher = Cipher(AES(cek), GCM(iv), backend=default_backend())
92+
cipher = Cipher(AES(cek), GCM(iv))
9493
enc = cipher.encryptor()
9594
enc.authenticate_additional_data(aad)
9695
ciphertext = enc.update(plaintext) + enc.finalize()
9796
return ciphertext, enc.tag
9897

9998
def decrypt(self, ciphertext: bytes, tag: bytes, cek: bytes, iv: bytes, aad: bytes) -> bytes:
10099
"""Key Decryption with AES GCM"""
101-
cipher = Cipher(AES(cek), GCM(iv, tag), backend=default_backend())
100+
cipher = Cipher(AES(cek), GCM(iv, tag))
102101
d = cipher.decryptor()
103102
d.authenticate_additional_data(aad)
104103
try:

src/joserfc/_rfc7518/rsa_key.py

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@
1313
rsa_crt_dmq1,
1414
rsa_crt_iqmp,
1515
)
16-
from cryptography.hazmat.backends import default_backend
1716
from ..registry import KeyParameter
1817
from ..errors import SecurityWarning, KeyParameterError
1918
from .._rfc7517.models import AsymmetricKey
@@ -45,11 +44,7 @@ class RSABinding(CryptographyBinding):
4544

4645
@staticmethod
4746
def generate_private_key(size: int) -> RSAPrivateKey:
48-
return generate_private_key(
49-
public_exponent=65537,
50-
key_size=size,
51-
backend=default_backend(),
52-
)
47+
return generate_private_key(public_exponent=65537, key_size=size)
5348

5449
@staticmethod
5550
def import_private_key(obj: RSADictKey) -> RSAPrivateKey:
@@ -82,7 +77,7 @@ def import_private_key(obj: RSADictKey) -> RSAPrivateKey:
8277
public_numbers=public_numbers,
8378
)
8479

85-
return numbers.private_key(default_backend())
80+
return numbers.private_key()
8681

8782
@staticmethod
8883
def export_private_key(key: RSAPrivateKey) -> RSADictKey:
@@ -101,7 +96,7 @@ def export_private_key(key: RSAPrivateKey) -> RSADictKey:
10196
@staticmethod
10297
def import_public_key(obj: RSADictKey) -> RSAPublicKey:
10398
numbers = RSAPublicNumbers(base64_to_int(obj["e"]), base64_to_int(obj["n"]))
104-
return numbers.public_key(default_backend())
99+
return numbers.public_key()
105100

106101
@staticmethod
107102
def export_public_key(key: RSAPublicKey) -> dict[str, str]:

src/joserfc/_rfc8037/okp_key.py

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
import typing as t
44
from functools import cached_property
55
from cryptography.hazmat.primitives import hashes
6-
from cryptography.hazmat.backends import default_backend
76
from cryptography.hazmat.primitives.asymmetric.ed25519 import Ed25519PublicKey, Ed25519PrivateKey
87
from cryptography.hazmat.primitives.asymmetric.ed448 import Ed448PublicKey, Ed448PrivateKey
98
from cryptography.hazmat.primitives.asymmetric.x25519 import X25519PublicKey, X25519PrivateKey
@@ -282,7 +281,6 @@ def derive_key(
282281
hkdf = HKDF(
283282
algorithm=algorithm,
284283
length=OKP_SEED_SIZES[crv],
285-
backend=default_backend(),
286284
**kdf_options,
287285
)
288286
seed = hkdf.derive(to_bytes(secret))
@@ -291,7 +289,6 @@ def derive_key(
291289
pbkdf2 = PBKDF2HMAC(
292290
algorithm=algorithm,
293291
length=OKP_SEED_SIZES[crv],
294-
backend=default_backend(),
295292
**kdf_options,
296293
)
297294
seed = pbkdf2.derive(to_bytes(secret))

0 commit comments

Comments
 (0)