summaryrefslogtreecommitdiffstats
path: root/Lib
diff options
context:
space:
mode:
authorMateusz Nowak <nowak.mateusz@hotmail.com>2023-09-20 01:20:54 (GMT)
committerGitHub <noreply@github.com>2023-09-20 01:20:54 (GMT)
commit5a740cd06ec1191767edcc6d3a7d5eca7873cb7b (patch)
tree26f85d51e466e5ce837874798cffe888d31f7c4c /Lib
parentddf2e953c27d529b7e321c972ede2afce5dfb0b0 (diff)
downloadcpython-5a740cd06ec1191767edcc6d3a7d5eca7873cb7b.zip
cpython-5a740cd06ec1191767edcc6d3a7d5eca7873cb7b.tar.gz
cpython-5a740cd06ec1191767edcc6d3a7d5eca7873cb7b.tar.bz2
gh-109109: Expose retrieving certificate chains in SSL module (#109113)
Adds APIs to get the TLS certificate chains, verified or full unverified, from SSLSocket and SSLObject. Co-authored-by: Gregory P. Smith [Google LLC] <greg@krypto.org>
Diffstat (limited to 'Lib')
-rw-r--r--Lib/ssl.py33
1 files changed, 33 insertions, 0 deletions
diff --git a/Lib/ssl.py b/Lib/ssl.py
index c4c5a4c..62e5585 100644
--- a/Lib/ssl.py
+++ b/Lib/ssl.py
@@ -876,6 +876,31 @@ class SSLObject:
"""
return self._sslobj.getpeercert(binary_form)
+ def get_verified_chain(self):
+ """Returns verified certificate chain provided by the other
+ end of the SSL channel as a list of DER-encoded bytes.
+
+ If certificate verification was disabled method acts the same as
+ ``SSLSocket.get_unverified_chain``.
+ """
+ chain = self._sslobj.get_verified_chain()
+
+ if chain is None:
+ return []
+
+ return [cert.public_bytes(_ssl.ENCODING_DER) for cert in chain]
+
+ def get_unverified_chain(self):
+ """Returns raw certificate chain provided by the other
+ end of the SSL channel as a list of DER-encoded bytes.
+ """
+ chain = self._sslobj.get_unverified_chain()
+
+ if chain is None:
+ return []
+
+ return [cert.public_bytes(_ssl.ENCODING_DER) for cert in chain]
+
def selected_npn_protocol(self):
"""Return the currently selected NPN protocol as a string, or ``None``
if a next protocol was not negotiated or if NPN is not supported by one
@@ -1130,6 +1155,14 @@ class SSLSocket(socket):
return self._sslobj.getpeercert(binary_form)
@_sslcopydoc
+ def get_verified_chain(self):
+ return self._sslobj.get_verified_chain()
+
+ @_sslcopydoc
+ def get_unverified_chain(self):
+ return self._sslobj.get_unverified_chain()
+
+ @_sslcopydoc
def selected_npn_protocol(self):
self._checkClosed()
warnings.warn(