Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions Lib/ssl.py
Original file line number Diff line number Diff line change
Expand Up @@ -1183,6 +1183,9 @@ def getpeercert(self, binary_form=False):

@_sslcopydoc
def get_verified_chain(self):
self._checkClosed()
if self._sslobj is None:
return []
chain = self._sslobj.get_verified_chain()

if chain is None:
Expand All @@ -1192,6 +1195,9 @@ def get_verified_chain(self):

@_sslcopydoc
def get_unverified_chain(self):
self._checkClosed()
if self._sslobj is None:
return []
chain = self._sslobj.get_unverified_chain()

if chain is None:
Expand Down
18 changes: 18 additions & 0 deletions Lib/test/test_ssl.py
Original file line number Diff line number Diff line change
Expand Up @@ -5435,6 +5435,24 @@ def test_certificate_chain(self):
self.assertEqual(ee, uvc[0])
self.assertNotEqual(ee, ca)

def test_chain_methods_after_unwrap(self):
# The chain methods must not crash once the TLS layer is torn down;
# like the other query methods they return an empty list when
# _sslobj is gone.
client_context, server_context, hostname = testing_context()
server = ThreadedEchoServer(context=server_context, chatty=False)
with server:
with client_context.wrap_socket(
socket.socket(),
server_hostname=hostname
) as s:
s.connect((HOST, server.port))
self.assertGreater(len(s.get_verified_chain()), 0)
s.unwrap()
self.assertIsNone(s._sslobj)
self.assertEqual(s.get_verified_chain(), [])
self.assertEqual(s.get_unverified_chain(), [])

def test_internal_chain_server(self):
client_context, server_context, hostname = testing_context()
client_context.load_cert_chain(SIGNED_CERTFILE)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Fix ``ssl.SSLSocket.get_verified_chain()`` and
``ssl.SSLSocket.get_unverified_chain()`` to return an empty list instead of
raising :exc:`AttributeError` when called after the socket's TLS layer has been
shut down. Patch by tonghuaroot.
Loading