Skip to content

Commit

Permalink
Deprecated passing X509 objects to add_extra_chain_cert (#1336)
Browse files Browse the repository at this point in the history
Added support for passing cryptography.x509.Certificate
  • Loading branch information
alex committed Aug 4, 2024
1 parent a6af6a1 commit 04a43b1
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 5 deletions.
2 changes: 1 addition & 1 deletion CHANGELOG.rst
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ Deprecations:

- Deprecated ``OpenSSL.rand`` - callers should use ``os.urandom()`` instead.
- 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``.
- 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.
- 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.

Changes:
^^^^^^^^
Expand Down
13 changes: 11 additions & 2 deletions src/OpenSSL/SSL.py
Original file line number Diff line number Diff line change
Expand Up @@ -1151,15 +1151,24 @@ def use_certificate(self, cert: X509 | x509.Certificate) -> None:
if not use_result:
_raise_current_error()

def add_extra_chain_cert(self, certobj: X509) -> None:
def add_extra_chain_cert(self, certobj: X509 | x509.Certificate) -> None:
"""
Add certificate to chain
:param certobj: The X509 certificate object to add to the chain
:return: None
"""
if not isinstance(certobj, X509):
raise TypeError("certobj must be an X509 instance")
certobj = X509.from_cryptography(certobj)
else:
warnings.warn(
(
"Passing pyOpenSSL X509 objects is deprecated. You "
"should use a cryptography.x509.Certificate instead."
),
DeprecationWarning,
stacklevel=2,
)

copy = _lib.X509_dup(certobj._x509)
add_result = _lib.SSL_CTX_add_extra_chain_cert(self._context, copy)
Expand Down
4 changes: 2 additions & 2 deletions tests/test_ssl.py
Original file line number Diff line number Diff line change
Expand Up @@ -2586,7 +2586,7 @@ def test_get_peer_cert_chain(self):
serverContext.use_privatekey(skey)
serverContext.use_certificate(scert)
serverContext.add_extra_chain_cert(icert)
serverContext.add_extra_chain_cert(cacert)
serverContext.add_extra_chain_cert(cacert.to_cryptography())
server = Connection(serverContext, None)
server.set_accept_state()

Expand Down Expand Up @@ -2630,7 +2630,7 @@ def test_get_verified_chain(self):
serverContext = Context(SSLv23_METHOD)
serverContext.use_privatekey(skey)
serverContext.use_certificate(scert)
serverContext.add_extra_chain_cert(icert)
serverContext.add_extra_chain_cert(icert.to_cryptography())
serverContext.add_extra_chain_cert(cacert)
server = Connection(serverContext, None)
server.set_accept_state()
Expand Down

0 comments on commit 04a43b1

Please sign in to comment.