Skip to content

Commit 6a78b0d

Browse files
alexreaperhulk
andauthored
Deprecated passing X509 objects to use_certificate (#1330)
* Deprecated passing X509 objects to use_certificate Added support for passing cryptography.x509.Certificate * Update CHANGELOG.rst Co-authored-by: Paul Kehrer <[email protected]> --------- Co-authored-by: Paul Kehrer <[email protected]>
1 parent 2df8071 commit 6a78b0d

File tree

3 files changed

+27
-4
lines changed

3 files changed

+27
-4
lines changed

CHANGELOG.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +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.
1819

1920
Changes:
2021
^^^^^^^^

src/OpenSSL/SSL.py

Lines changed: 23 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
from typing import Any, Callable, List, Optional, Sequence, TypeVar
1212
from weakref import WeakValueDictionary
1313

14+
from cryptography import x509
1415
from cryptography.hazmat.primitives.asymmetric import ec
1516

1617
from OpenSSL._util import (
@@ -1126,7 +1127,7 @@ def use_certificate_file(
11261127
if not use_result:
11271128
_raise_current_error()
11281129

1129-
def use_certificate(self, cert: X509) -> None:
1130+
def use_certificate(self, cert: X509 | x509.Certificate) -> None:
11301131
"""
11311132
Load a certificate from a X509 object
11321133
@@ -1135,7 +1136,16 @@ def use_certificate(self, cert: X509) -> None:
11351136
"""
11361137
# Mirrored at Connection.use_certificate
11371138
if not isinstance(cert, X509):
1138-
raise TypeError("cert must be an X509 instance")
1139+
cert = X509.from_cryptography(cert)
1140+
else:
1141+
warnings.warn(
1142+
(
1143+
"Passing pyOpenSSL X509 objects is deprecated. You "
1144+
"should use a cryptography.x509.Certificate instead."
1145+
),
1146+
DeprecationWarning,
1147+
stacklevel=2,
1148+
)
11391149

11401150
use_result = _lib.SSL_CTX_use_certificate(self._context, cert._x509)
11411151
if not use_result:
@@ -2017,7 +2027,7 @@ def get_verify_mode(self) -> int:
20172027
"""
20182028
return _lib.SSL_get_verify_mode(self._ssl)
20192029

2020-
def use_certificate(self, cert: X509) -> None:
2030+
def use_certificate(self, cert: X509 | x509.Certificate) -> None:
20212031
"""
20222032
Load a certificate from a X509 object
20232033
@@ -2026,7 +2036,16 @@ def use_certificate(self, cert: X509) -> None:
20262036
"""
20272037
# Mirrored from Context.use_certificate
20282038
if not isinstance(cert, X509):
2029-
raise TypeError("cert must be an X509 instance")
2039+
cert = X509.from_cryptography(cert)
2040+
else:
2041+
warnings.warn(
2042+
(
2043+
"Passing pyOpenSSL X509 objects is deprecated. You "
2044+
"should use a cryptography.x509.Certificate instead."
2045+
),
2046+
DeprecationWarning,
2047+
stacklevel=2,
2048+
)
20302049

20312050
use_result = _lib.SSL_use_certificate(self._ssl, cert._x509)
20322051
if not use_result:

tests/test_ssl.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2208,6 +2208,9 @@ def test_use_certificate(self, ctx_or_conn):
22082208
ctx_or_conn.use_certificate(
22092209
load_certificate(FILETYPE_PEM, root_cert_pem)
22102210
)
2211+
ctx_or_conn.use_certificate(
2212+
load_certificate(FILETYPE_PEM, root_cert_pem).to_cryptography()
2213+
)
22112214

22122215
def test_use_certificate_wrong_args(self, ctx_or_conn):
22132216
"""

0 commit comments

Comments
 (0)