Skip to content

Commit 0eaef1c

Browse files
authored
Deprecated passing X509 objects to add_client_ca (#1347)
Added support for passing cryptography.x509.Certificate
1 parent 4c89c97 commit 0eaef1c

File tree

3 files changed

+18
-5
lines changed

3 files changed

+18
-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``, ``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.
18+
- Deprecated passing ``X509`` objects to ``OpenSSL.SSL.Context.use_certificate``, ``OpenSSL.SSL.Connection.use_certificate``, ``OpenSSL.SSL.Context.add_extra_chain_cert``, and ``OpenSSL.SSL.Context.add_client_ca``, 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: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1492,7 +1492,9 @@ def set_client_ca_list(
14921492

14931493
_lib.SSL_CTX_set_client_CA_list(self._context, name_stack)
14941494

1495-
def add_client_ca(self, certificate_authority: X509) -> None:
1495+
def add_client_ca(
1496+
self, certificate_authority: X509 | x509.Certificate
1497+
) -> None:
14961498
"""
14971499
Add the CA certificate to the list of preferred signers for this
14981500
context.
@@ -1506,7 +1508,18 @@ def add_client_ca(self, certificate_authority: X509) -> None:
15061508
.. versionadded:: 0.10
15071509
"""
15081510
if not isinstance(certificate_authority, X509):
1509-
raise TypeError("certificate_authority must be an X509 instance")
1511+
certificate_authority = X509.from_cryptography(
1512+
certificate_authority
1513+
)
1514+
else:
1515+
warnings.warn(
1516+
(
1517+
"Passing pyOpenSSL X509 objects is deprecated. You "
1518+
"should use a cryptography.x509.Certificate instead."
1519+
),
1520+
DeprecationWarning,
1521+
stacklevel=2,
1522+
)
15101523

15111524
add_result = _lib.SSL_CTX_add_client_CA(
15121525
self._context, certificate_authority._x509

tests/test_ssl.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3922,7 +3922,7 @@ def test_multiple_add_client_ca(self):
39223922

39233923
def multiple_ca(ctx):
39243924
ctx.add_client_ca(cacert)
3925-
ctx.add_client_ca(secert)
3925+
ctx.add_client_ca(secert.to_cryptography())
39263926
return [cadesc, sedesc]
39273927

39283928
self._check_client_ca_list(multiple_ca)
@@ -3962,7 +3962,7 @@ def test_set_after_add_client_ca(self):
39623962
sedesc = secert.get_subject()
39633963

39643964
def set_replaces_add_ca(ctx):
3965-
ctx.add_client_ca(clcert)
3965+
ctx.add_client_ca(clcert.to_cryptography())
39663966
ctx.set_client_ca_list([cadesc])
39673967
ctx.add_client_ca(secert)
39683968
return [cadesc, sedesc]

0 commit comments

Comments
 (0)