mirror of
https://github.com/python/cpython.git
synced 2025-12-09 10:37:17 +00:00
Issue #19784: poplib now supports SSLContext.check_hostname and server name
indication for TLS/SSL connections.
This commit is contained in:
parent
b8a3f58158
commit
1bc7068d7f
4 changed files with 26 additions and 3 deletions
|
|
@ -387,7 +387,9 @@ class POP3:
|
|||
if context is None:
|
||||
context = ssl._create_stdlib_context()
|
||||
resp = self._shortcmd('STLS')
|
||||
self.sock = context.wrap_socket(self.sock)
|
||||
server_hostname = self.host if ssl.HAS_SNI else None
|
||||
self.sock = context.wrap_socket(self.sock,
|
||||
server_hostname=server_hostname)
|
||||
self.file = self.sock.makefile('rb')
|
||||
self._tls_established = True
|
||||
return resp
|
||||
|
|
@ -428,7 +430,9 @@ if HAVE_SSL:
|
|||
|
||||
def _create_socket(self, timeout):
|
||||
sock = POP3._create_socket(self, timeout)
|
||||
sock = self.context.wrap_socket(sock)
|
||||
server_hostname = self.host if ssl.HAS_SNI else None
|
||||
sock = self.context.wrap_socket(sock,
|
||||
server_hostname=server_hostname)
|
||||
return sock
|
||||
|
||||
def stls(self, keyfile=None, certfile=None, context=None):
|
||||
|
|
|
|||
|
|
@ -23,7 +23,8 @@ if hasattr(poplib, 'POP3_SSL'):
|
|||
import ssl
|
||||
|
||||
SUPPORTS_SSL = True
|
||||
CERTFILE = os.path.join(os.path.dirname(__file__) or os.curdir, "keycert.pem")
|
||||
CERTFILE = os.path.join(os.path.dirname(__file__) or os.curdir, "keycert3.pem")
|
||||
CAFILE = os.path.join(os.path.dirname(__file__) or os.curdir, "pycacert.pem")
|
||||
requires_ssl = skipUnless(SUPPORTS_SSL, 'SSL not supported')
|
||||
|
||||
# the dummy data returned by server when LIST and RETR commands are issued
|
||||
|
|
@ -332,6 +333,12 @@ class TestPOP3Class(TestCase):
|
|||
def test_stls_context(self):
|
||||
expected = b'+OK Begin TLS negotiation'
|
||||
ctx = ssl.SSLContext(ssl.PROTOCOL_TLSv1)
|
||||
ctx.load_verify_locations(CAFILE)
|
||||
ctx.verify_mode = ssl.CERT_REQUIRED
|
||||
ctx.check_hostname = True
|
||||
with self.assertRaises(ssl.CertificateError):
|
||||
resp = self.client.stls(context=ctx)
|
||||
self.client = poplib.POP3("localhost", self.server.port, timeout=3)
|
||||
resp = self.client.stls(context=ctx)
|
||||
self.assertEqual(resp, expected)
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue