Skip to content

Commit

Permalink
Deprecated passing X509 objects to use_certificate
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 8c42c52 commit 9f2f225
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 4 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.rst
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ Deprecations:
^^^^^^^^^^^^^

- Deprecated ``OpenSSL.rand`` - callers should use ``os.urandom()`` instead.
- 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 preperation for deprecating pyOpenSSL's ``X509`` entirely.

Changes:
^^^^^^^^
Expand Down
27 changes: 23 additions & 4 deletions src/OpenSSL/SSL.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
from typing import Any, Callable, List, Optional, Sequence, TypeVar
from weakref import WeakValueDictionary

from cryptography import x509
from cryptography.hazmat.primitives.asymmetric import ec

from OpenSSL._util import (
Expand Down Expand Up @@ -1126,7 +1127,7 @@ def use_certificate_file(
if not use_result:
_raise_current_error()

def use_certificate(self, cert: X509) -> None:
def use_certificate(self, cert: X509 | x509.Certificate) -> None:
"""
Load a certificate from a X509 object
Expand All @@ -1135,7 +1136,16 @@ def use_certificate(self, cert: X509) -> None:
"""
# Mirrored at Connection.use_certificate
if not isinstance(cert, X509):
raise TypeError("cert must be an X509 instance")
cert = X509.from_cryptography(cert)
else:
warnings.warn(
(
"Passing pyOpensSSL X509 objects is deprecated. You "
"should use a cryptography.x509.Certificate instead."
),
DeprecationWarning,
stacklevel=2,
)

use_result = _lib.SSL_CTX_use_certificate(self._context, cert._x509)
if not use_result:
Expand Down Expand Up @@ -2017,7 +2027,7 @@ def get_verify_mode(self) -> int:
"""
return _lib.SSL_get_verify_mode(self._ssl)

def use_certificate(self, cert: X509) -> None:
def use_certificate(self, cert: X509 | x509.Certificate) -> None:
"""
Load a certificate from a X509 object
Expand All @@ -2026,7 +2036,16 @@ def use_certificate(self, cert: X509) -> None:
"""
# Mirrored from Context.use_certificate
if not isinstance(cert, X509):
raise TypeError("cert must be an X509 instance")
cert = X509.from_cryptography(cert)
else:
warnings.warn(
(
"Passing pyOpensSSL X509 objects is deprecated. You "
"should use a cryptography.x509.Certificate instead."
),
DeprecationWarning,
stacklevel=2,
)

use_result = _lib.SSL_use_certificate(self._ssl, cert._x509)
if not use_result:
Expand Down
3 changes: 3 additions & 0 deletions tests/test_ssl.py
Original file line number Diff line number Diff line change
Expand Up @@ -2208,6 +2208,9 @@ def test_use_certificate(self, ctx_or_conn):
ctx_or_conn.use_certificate(
load_certificate(FILETYPE_PEM, root_cert_pem)
)
ctx_or_conn.use_certificate(
load_certificate(FILETYPE_PEM, root_cert_pem).to_cryptography()
)

def test_use_certificate_wrong_args(self, ctx_or_conn):
"""
Expand Down

0 comments on commit 9f2f225

Please sign in to comment.