Skip to content

Commit 3d050da

Browse files
committed
Stop using default_backend, no longer required
1 parent 81553e5 commit 3d050da

File tree

4 files changed

+16
-29
lines changed

4 files changed

+16
-29
lines changed

setup.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
# Dependencies are restricted by major version range according to semver.
1717
# By default, version minimums are set to be compatible with the oldest supported Ubuntu LTS (currently 18.04).
1818
"lxml >= 4.2.1, < 5",
19-
"cryptography >= 2.1.4",
19+
"cryptography >= 3.4.8", # Set to the version in Ubuntu 22.04 due to features we need from cryptography 3.1
2020
"pyOpenSSL >= 17.5.0",
2121
"certifi >= 2018.1.18",
2222
],

signxml/__init__.py

Lines changed: 9 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
from dataclasses import dataclass
33
from typing import List, Optional, Tuple, Union
44

5-
from cryptography.hazmat.backends import default_backend
65
from cryptography.hazmat.primitives.asymmetric import dsa, ec, rsa, utils
76
from cryptography.hazmat.primitives.asymmetric.padding import PKCS1v15
87
from cryptography.hazmat.primitives.hashes import Hash
@@ -92,7 +91,7 @@ class XMLSignatureProcessor(XMLProcessor):
9291

9392
def _get_digest(self, data, algorithm: digest_algorithms):
9493
algorithm_implementation = digest_algorithm_implementations[algorithm]()
95-
hasher = Hash(algorithm=algorithm_implementation, backend=default_backend())
94+
hasher = Hash(algorithm=algorithm_implementation)
9695
hasher.update(data)
9796
return hasher.finalize()
9897

@@ -324,9 +323,7 @@ def sign(
324323
raise InvalidInput('Parameter "key" is required')
325324
elif not self.sign_alg.name.startswith("HMAC_"):
326325
if isinstance(key, (str, bytes)):
327-
signing_settings.key = load_pem_private_key(
328-
ensure_bytes(key), password=passphrase, backend=default_backend()
329-
)
326+
signing_settings.key = load_pem_private_key(ensure_bytes(key), password=passphrase)
330327
else:
331328
signing_settings.key = key
332329

@@ -354,9 +351,7 @@ def sign(
354351
signed_info_node, algorithm=self.c14n_alg, inclusive_ns_prefixes=signature_inclusive_ns_prefixes
355352
)
356353
if self.sign_alg.name.startswith("HMAC_"):
357-
signer = HMAC(
358-
key=key, algorithm=digest_algorithm_implementations[self.sign_alg](), backend=default_backend()
359-
)
354+
signer = HMAC(key=key, algorithm=digest_algorithm_implementations[self.sign_alg]())
360355
signer.update(signed_info_c14n)
361356
signature_value_node.text = b64encode(signer.finalize()).decode()
362357
sig_root.append(signature_value_node)
@@ -565,7 +560,7 @@ def _verify_signature_with_pubkey(
565560
self, signed_info_c14n, raw_signature, key_value, der_encoded_key_value, signature_alg
566561
):
567562
if der_encoded_key_value is not None:
568-
key = load_der_public_key(b64decode(der_encoded_key_value.text), backend=default_backend())
563+
key = load_der_public_key(b64decode(der_encoded_key_value.text))
569564

570565
digest_algorithm_implementation = digest_algorithm_implementations[signature_alg]()
571566
if signature_alg.name.startswith("ECDSA_"):
@@ -578,7 +573,7 @@ def _verify_signature_with_pubkey(
578573
y = bytes_to_long(key_data[len(key_data) // 2 :])
579574
curve_class = self.known_ecdsa_curves[named_curve.get("URI")]
580575
ecpn = ec.EllipticCurvePublicNumbers(x=x, y=y, curve=curve_class()) # type: ignore
581-
key = ecpn.public_key(backend=default_backend())
576+
key = ecpn.public_key()
582577
elif not isinstance(key, ec.EllipticCurvePublicKey):
583578
raise InvalidInput("DER encoded key value does not match specified signature algorithm")
584579
dss_signature = self._encode_dss_signature(raw_signature, key.key_size)
@@ -593,7 +588,7 @@ def _verify_signature_with_pubkey(
593588
g = self._get_long(dsa_key_value, "G", require=False)
594589
y = self._get_long(dsa_key_value, "Y")
595590
dsapn = dsa.DSAPublicNumbers(y=y, parameter_numbers=dsa.DSAParameterNumbers(p=p, q=q, g=g))
596-
key = dsapn.public_key(backend=default_backend()) # type: ignore
591+
key = dsapn.public_key() # type: ignore
597592
elif not isinstance(key, dsa.DSAPublicKey):
598593
raise InvalidInput("DER encoded key value does not match specified signature algorithm")
599594
# TODO: supply meaningful key_size_bits for signature length assertion
@@ -604,7 +599,7 @@ def _verify_signature_with_pubkey(
604599
rsa_key_value = self._find(key_value, "RSAKeyValue")
605600
modulus = self._get_long(rsa_key_value, "Modulus")
606601
exponent = self._get_long(rsa_key_value, "Exponent")
607-
key = rsa.RSAPublicNumbers(e=exponent, n=modulus).public_key(backend=default_backend())
602+
key = rsa.RSAPublicNumbers(e=exponent, n=modulus).public_key()
608603
elif not isinstance(key, rsa.RSAPublicKey):
609604
raise InvalidInput("DER encoded key value does not match specified signature algorithm")
610605
key.verify(
@@ -884,11 +879,7 @@ def verify(
884879
if self.hmac_key is None:
885880
raise InvalidInput('Parameter "hmac_key" is required when verifying a HMAC signature')
886881

887-
signer = HMAC(
888-
key=ensure_bytes(self.hmac_key),
889-
algorithm=digest_algorithm_implementations[signature_alg](),
890-
backend=default_backend(),
891-
)
882+
signer = HMAC(key=ensure_bytes(self.hmac_key), algorithm=digest_algorithm_implementations[signature_alg]())
892883
signer.update(signed_info_c14n)
893884
if raw_signature != signer.finalize():
894885
raise InvalidSignature("Signature mismatch (HMAC)")
@@ -983,7 +974,7 @@ def check_key_value_matches_cert_public_key(self, key_value, public_key, signatu
983974

984975
def check_der_key_value_matches_cert_public_key(self, der_encoded_key_value, public_key, signature_alg):
985976
# TODO: Add a test case for this functionality
986-
der_public_key = load_der_public_key(b64decode(der_encoded_key_value.text), backend=default_backend())
977+
der_public_key = load_der_public_key(b64decode(der_encoded_key_value.text))
987978

988979
if (
989980
signature_alg.name.startswith("ECDSA_")

signxml/util/__init__.py

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
from typing import Any, List, Optional
1616
from xml.etree import ElementTree as stdlibElementTree
1717

18-
from cryptography.hazmat.primitives import hashes
18+
from cryptography.hazmat.primitives import hashes, hmac
1919
from lxml import etree
2020

2121
from ..exceptions import InvalidCertificate, InvalidInput, RedundantCert, SignXMLException
@@ -318,10 +318,7 @@ def get_root(self, data):
318318

319319

320320
def hmac_sha1(key, message):
321-
from cryptography.hazmat.backends import default_backend
322-
from cryptography.hazmat.primitives import hashes, hmac
323-
324-
hasher = hmac.HMAC(key, hashes.SHA1(), backend=default_backend())
321+
hasher = hmac.HMAC(key, hashes.SHA1())
325322
hasher.update(message)
326323
return hasher.finalize()
327324

test/test.py

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@
1010
from xml.etree import ElementTree as stdlibElementTree
1111

1212
import cryptography.exceptions
13-
from cryptography.hazmat.backends import default_backend
1413
from cryptography.hazmat.primitives.asymmetric import dsa, ec, rsa
1514
from lxml import etree
1615

@@ -81,9 +80,9 @@ def setUp(self):
8180
)
8281
self.keys = dict(
8382
hmac=b"secret",
84-
rsa=rsa.generate_private_key(public_exponent=65537, key_size=2048, backend=default_backend()),
85-
dsa=dsa.generate_private_key(key_size=1024, backend=default_backend()),
86-
ecdsa=ec.generate_private_key(curve=ec.SECP384R1(), backend=default_backend()),
83+
rsa=rsa.generate_private_key(public_exponent=65537, key_size=2048),
84+
dsa=dsa.generate_private_key(key_size=1024),
85+
ecdsa=ec.generate_private_key(curve=ec.SECP384R1()),
8786
)
8887

8988
def test_basic_signxml_statements(self):
@@ -230,7 +229,7 @@ def get_x509_cert(**kwargs):
230229
from OpenSSL.crypto import X509
231230

232231
with open(os.path.join(interop_dir, "TR2012", "rsa-cert.der"), "rb") as fh:
233-
return [X509.from_cryptography(load_der_x509_certificate(fh.read(), backend=default_backend()))]
232+
return [X509.from_cryptography(load_der_x509_certificate(fh.read()))]
234233

235234
signature_files = glob(os.path.join(interop_dir, "TR2012", "signature*.xml"))
236235
for signature_file in signature_files:

0 commit comments

Comments
 (0)