Skip to content

Commit 7dbafff

Browse files
authored
Stop passing backend to cryptography (#1094)
1 parent bcb2011 commit 7dbafff

File tree

3 files changed

+15
-47
lines changed

3 files changed

+15
-47
lines changed

src/OpenSSL/crypto.py

Lines changed: 5 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -79,19 +79,6 @@ class Error(Exception):
7979
_openssl_assert = _make_assert(Error)
8080

8181

82-
def _get_backend():
83-
"""
84-
Importing the backend from cryptography has the side effect of activating
85-
the osrandom engine. This mutates the global state of OpenSSL in the
86-
process and causes issues for various programs that use subinterpreters or
87-
embed Python. By putting the import in this function we can avoid
88-
triggering this side effect unless _get_backend is called.
89-
"""
90-
from cryptography.hazmat.backends.openssl.backend import backend
91-
92-
return backend
93-
94-
9582
def _untested_error(where):
9683
"""
9784
An OpenSSL API failed somehow. Additionally, the failure which was
@@ -241,13 +228,12 @@ def to_cryptography_key(self):
241228
load_der_public_key,
242229
)
243230

244-
backend = _get_backend()
245231
if self._only_public:
246232
der = dump_publickey(FILETYPE_ASN1, self)
247-
return load_der_public_key(der, backend)
233+
return load_der_public_key(der)
248234
else:
249235
der = dump_privatekey(FILETYPE_ASN1, self)
250-
return load_der_private_key(der, None, backend)
236+
return load_der_private_key(der, None)
251237

252238
@classmethod
253239
def from_cryptography_key(cls, crypto_key):
@@ -897,8 +883,7 @@ def to_cryptography(self):
897883

898884
der = dump_certificate_request(FILETYPE_ASN1, self)
899885

900-
backend = _get_backend()
901-
return load_der_x509_csr(der, backend)
886+
return load_der_x509_csr(der)
902887

903888
@classmethod
904889
def from_cryptography(cls, crypto_req):
@@ -1118,8 +1103,7 @@ def to_cryptography(self):
11181103
from cryptography.x509 import load_der_x509_certificate
11191104

11201105
der = dump_certificate(FILETYPE_ASN1, self)
1121-
backend = _get_backend()
1122-
return load_der_x509_certificate(der, backend)
1106+
return load_der_x509_certificate(der)
11231107

11241108
@classmethod
11251109
def from_cryptography(cls, crypto_cert):
@@ -2267,9 +2251,7 @@ def to_cryptography(self):
22672251
from cryptography.x509 import load_der_x509_crl
22682252

22692253
der = dump_crl(FILETYPE_ASN1, self)
2270-
2271-
backend = _get_backend()
2272-
return load_der_x509_crl(der, backend)
2254+
return load_der_x509_crl(der)
22732255

22742256
@classmethod
22752257
def from_cryptography(cls, crypto_crl):

tests/test_crypto.py

Lines changed: 8 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@
1515
import pytest
1616

1717
from cryptography import x509
18-
from cryptography.hazmat.backends.openssl.backend import backend
1918
from cryptography.hazmat.primitives import serialization
2019
from cryptography.hazmat.primitives.asymmetric import rsa
2120

@@ -1017,9 +1016,7 @@ def test_convert_from_cryptography_private_key(self):
10171016
"""
10181017
PKey.from_cryptography_key creates a proper private PKey.
10191018
"""
1020-
key = serialization.load_pem_private_key(
1021-
intermediate_key_pem, None, backend
1022-
)
1019+
key = serialization.load_pem_private_key(intermediate_key_pem, None)
10231020
pkey = PKey.from_cryptography_key(key)
10241021

10251022
assert isinstance(pkey, PKey)
@@ -1031,7 +1028,7 @@ def test_convert_from_cryptography_public_key(self):
10311028
"""
10321029
PKey.from_cryptography_key creates a proper public PKey.
10331030
"""
1034-
key = serialization.load_pem_public_key(cleartextPublicKeyPEM, backend)
1031+
key = serialization.load_pem_public_key(cleartextPublicKeyPEM)
10351032
pkey = PKey.from_cryptography_key(key)
10361033

10371034
assert isinstance(pkey, PKey)
@@ -1043,9 +1040,7 @@ def test_convert_from_cryptography_unsupported_type(self):
10431040
"""
10441041
PKey.from_cryptography_key raises TypeError with an unsupported type.
10451042
"""
1046-
key = serialization.load_pem_private_key(
1047-
ec_private_key_pem, None, backend
1048-
)
1043+
key = serialization.load_pem_private_key(ec_private_key_pem, None)
10491044
with pytest.raises(TypeError):
10501045
PKey.from_cryptography_key(key)
10511046

@@ -1699,9 +1694,7 @@ def test_verify_success(self):
16991694
assert request.verify(pkey)
17001695

17011696
def test_convert_from_cryptography(self):
1702-
crypto_req = x509.load_pem_x509_csr(
1703-
cleartextCertificateRequestPEM, backend
1704-
)
1697+
crypto_req = x509.load_pem_x509_csr(cleartextCertificateRequestPEM)
17051698
req = X509Req.from_cryptography(crypto_req)
17061699
assert isinstance(req, X509Req)
17071700

@@ -2217,9 +2210,7 @@ def test_sign_bad_pubkey_type(self):
22172210
cert.sign(object(), b"sha256")
22182211

22192212
def test_convert_from_cryptography(self):
2220-
crypto_cert = x509.load_pem_x509_certificate(
2221-
intermediate_cert_pem, backend
2222-
)
2213+
crypto_cert = x509.load_pem_x509_certificate(intermediate_cert_pem)
22232214
cert = X509.from_cryptography(crypto_cert)
22242215

22252216
assert isinstance(cert, X509)
@@ -3561,7 +3552,7 @@ def test_export_pem(self):
35613552
dumped_crl = self._get_crl().export(
35623553
self.cert, self.pkey, days=20, digest=b"sha256"
35633554
)
3564-
crl = x509.load_pem_x509_crl(dumped_crl, backend)
3555+
crl = x509.load_pem_x509_crl(dumped_crl)
35653556
revoked = crl.get_revoked_certificate_by_serial_number(0x03AB)
35663557
assert revoked is not None
35673558
assert crl.issuer == x509.Name(
@@ -3588,7 +3579,7 @@ def test_export_der(self):
35883579
dumped_crl = self._get_crl().export(
35893580
self.cert, self.pkey, FILETYPE_ASN1, digest=b"sha256"
35903581
)
3591-
crl = x509.load_der_x509_crl(dumped_crl, backend)
3582+
crl = x509.load_der_x509_crl(dumped_crl)
35923583
revoked = crl.get_revoked_certificate_by_serial_number(0x03AB)
35933584
assert revoked is not None
35943585
assert crl.issuer == x509.Name(
@@ -3857,7 +3848,7 @@ def test_verify_with_missing_crl(self):
38573848
assert err.value.certificate.get_subject().CN == "intermediate-service"
38583849

38593850
def test_convert_from_cryptography(self):
3860-
crypto_crl = x509.load_pem_x509_crl(crlData, backend)
3851+
crypto_crl = x509.load_pem_x509_crl(crlData)
38613852
crl = CRL.from_cryptography(crypto_crl)
38623853
assert isinstance(crl, CRL)
38633854

tests/test_ssl.py

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,6 @@
4040
from pretend import raiser
4141

4242
from cryptography import x509
43-
from cryptography.hazmat.backends import default_backend
4443
from cryptography.hazmat.primitives import hashes
4544
from cryptography.hazmat.primitives import serialization
4645
from cryptography.hazmat.primitives.asymmetric import rsa
@@ -447,9 +446,7 @@ def ca_file(tmpdir):
447446
"""
448447
Create a valid PEM file with CA certificates and return the path.
449448
"""
450-
key = rsa.generate_private_key(
451-
public_exponent=65537, key_size=2048, backend=default_backend()
452-
)
449+
key = rsa.generate_private_key(public_exponent=65537, key_size=2048)
453450
public_key = key.public_key()
454451

455452
builder = x509.CertificateBuilder()
@@ -469,9 +466,7 @@ def ca_file(tmpdir):
469466
critical=True,
470467
)
471468

472-
certificate = builder.sign(
473-
private_key=key, algorithm=hashes.SHA256(), backend=default_backend()
474-
)
469+
certificate = builder.sign(private_key=key, algorithm=hashes.SHA256())
475470

476471
ca_file = tmpdir.join("test.pem")
477472
ca_file.write_binary(

0 commit comments

Comments
 (0)