Skip to content

Commit b42e6dc

Browse files
authored
Allow Connection.get_peer_certificate to return a cryptography certificate (#1352)
1 parent 38d8b04 commit b42e6dc

File tree

3 files changed

+37
-10
lines changed

3 files changed

+37
-10
lines changed

CHANGELOG.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ Deprecations:
2121
Changes:
2222
^^^^^^^^
2323

24-
* ``OpenSSL.SSL.Connection.get_certificate`` now takes an ``as_cryptography`` keyword-argument. When ``True`` is passed then a ``cryptography.x509.Certificate`` is returned, instead of an ``OpenSSL.crypto.X509``. In the future, passing ``False`` (the default) will be deprecated.
24+
* ``OpenSSL.SSL.Connection.get_certificate`` and ``OpenSSL.SSL.Connection.get_peer_certificate`` now take an ``as_cryptography`` keyword-argument. When ``True`` is passed then a ``cryptography.x509.Certificate`` is returned, instead of an ``OpenSSL.crypto.X509``. In the future, passing ``False`` (the default) will be deprecated.
2525

2626

2727
24.2.1 (2024-07-20)

src/OpenSSL/SSL.py

Lines changed: 28 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2708,14 +2708,10 @@ def get_certificate(
27082708
) -> X509 | None:
27092709
pass
27102710

2711-
@typing.overload
27122711
def get_certificate(
2713-
self, *, as_cryptography: bool = False
2714-
) -> X509 | x509.Certificate | None:
2715-
pass
2716-
2717-
def get_certificate(
2718-
self, *, as_cryptography: bool = False
2712+
self,
2713+
*,
2714+
as_cryptography: typing.Literal[True] | typing.Literal[False] = False,
27192715
) -> X509 | x509.Certificate | None:
27202716
"""
27212717
Retrieve the local certificate (if any)
@@ -2735,15 +2731,38 @@ def get_certificate(
27352731
return pycert
27362732
return None
27372733

2738-
def get_peer_certificate(self) -> X509 | None:
2734+
@typing.overload
2735+
def get_peer_certificate(
2736+
self, *, as_cryptography: typing.Literal[True]
2737+
) -> x509.Certificate | None:
2738+
pass
2739+
2740+
@typing.overload
2741+
def get_peer_certificate(
2742+
self, *, as_cryptography: typing.Literal[False] = False
2743+
) -> X509 | None:
2744+
pass
2745+
2746+
def get_peer_certificate(
2747+
self,
2748+
*,
2749+
as_cryptography: typing.Literal[True] | typing.Literal[False] = False,
2750+
) -> X509 | x509.Certificate | None:
27392751
"""
27402752
Retrieve the other side's certificate (if any)
27412753
2754+
:param bool as_cryptography: Controls whether a
2755+
``cryptography.x509.Certificate`` or an ``OpenSSL.crypto.X509``
2756+
object should be returned.
2757+
27422758
:return: The peer's certificate
27432759
"""
27442760
cert = _lib.SSL_get_peer_certificate(self._ssl)
27452761
if cert != _ffi.NULL:
2746-
return X509._from_raw_x509_ptr(cert)
2762+
pycert = X509._from_raw_x509_ptr(cert)
2763+
if as_cryptography:
2764+
return pycert.to_cryptography()
2765+
return pycert
27472766
return None
27482767

27492768
@staticmethod

tests/test_ssl.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1073,6 +1073,14 @@ def _load_verify_locations_test(self, *args):
10731073
cert = clientSSL.get_peer_certificate()
10741074
assert cert.get_subject().CN == "Testing Root CA"
10751075

1076+
cryptography_cert = clientSSL.get_peer_certificate(
1077+
as_cryptography=True
1078+
)
1079+
assert (
1080+
cryptography_cert.subject.rfc4514_string()
1081+
== "CN=Testing Root CA,O=Testing,L=Chicago,ST=IL,C=US"
1082+
)
1083+
10761084
def _load_verify_cafile(self, cafile):
10771085
"""
10781086
Verify that if path to a file containing a certificate is passed to

0 commit comments

Comments
 (0)