Skip to content

Commit 04a43b1

Browse files
authored
Deprecated passing X509 objects to add_extra_chain_cert (#1336)
Added support for passing cryptography.x509.Certificate
1 parent a6af6a1 commit 04a43b1

File tree

3 files changed

+14
-5
lines changed

3 files changed

+14
-5
lines changed

CHANGELOG.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ Deprecations:
1515

1616
- Deprecated ``OpenSSL.rand`` - callers should use ``os.urandom()`` instead.
1717
- Deprecated ``OpenSSL.crypto.get_elliptic_curves`` and ``OpenSSL.crypto.get_elliptic_curve``, as well as passing the reult of them to ``OpenSSL.SSL.Context.set_tmp_ecdh``, users should instead pass curves from ``cryptography``.
18-
- Deprecated passing ``X509`` objects to ``OpenSSL.SSL.Context.use_certificate`` and ``OpenSSL.SSL.Connection.use_certificate``, users should instead pass ``cryptography.x509.Certificate`` instances. This is in preparation for deprecating pyOpenSSL's ``X509`` entirely.
18+
- Deprecated passing ``X509`` objects to ``OpenSSL.SSL.Context.use_certificate``, ``OpenSSL.SSL.Connection.use_certificate``, and ``OpenSSL.SSL.Context.add_extra_chain_cert``, users should instead pass ``cryptography.x509.Certificate`` instances. This is in preparation for deprecating pyOpenSSL's ``X509`` entirely.
1919

2020
Changes:
2121
^^^^^^^^

src/OpenSSL/SSL.py

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1151,15 +1151,24 @@ def use_certificate(self, cert: X509 | x509.Certificate) -> None:
11511151
if not use_result:
11521152
_raise_current_error()
11531153

1154-
def add_extra_chain_cert(self, certobj: X509) -> None:
1154+
def add_extra_chain_cert(self, certobj: X509 | x509.Certificate) -> None:
11551155
"""
11561156
Add certificate to chain
11571157
11581158
:param certobj: The X509 certificate object to add to the chain
11591159
:return: None
11601160
"""
11611161
if not isinstance(certobj, X509):
1162-
raise TypeError("certobj must be an X509 instance")
1162+
certobj = X509.from_cryptography(certobj)
1163+
else:
1164+
warnings.warn(
1165+
(
1166+
"Passing pyOpenSSL X509 objects is deprecated. You "
1167+
"should use a cryptography.x509.Certificate instead."
1168+
),
1169+
DeprecationWarning,
1170+
stacklevel=2,
1171+
)
11631172

11641173
copy = _lib.X509_dup(certobj._x509)
11651174
add_result = _lib.SSL_CTX_add_extra_chain_cert(self._context, copy)

tests/test_ssl.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2586,7 +2586,7 @@ def test_get_peer_cert_chain(self):
25862586
serverContext.use_privatekey(skey)
25872587
serverContext.use_certificate(scert)
25882588
serverContext.add_extra_chain_cert(icert)
2589-
serverContext.add_extra_chain_cert(cacert)
2589+
serverContext.add_extra_chain_cert(cacert.to_cryptography())
25902590
server = Connection(serverContext, None)
25912591
server.set_accept_state()
25922592

@@ -2630,7 +2630,7 @@ def test_get_verified_chain(self):
26302630
serverContext = Context(SSLv23_METHOD)
26312631
serverContext.use_privatekey(skey)
26322632
serverContext.use_certificate(scert)
2633-
serverContext.add_extra_chain_cert(icert)
2633+
serverContext.add_extra_chain_cert(icert.to_cryptography())
26342634
serverContext.add_extra_chain_cert(cacert)
26352635
server = Connection(serverContext, None)
26362636
server.set_accept_state()

0 commit comments

Comments
 (0)