Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 1bc7068

Browse files
committed
Issue #19784: poplib now supports SSLContext.check_hostname and server name
indication for TLS/SSL connections.
1 parent b8a3f58 commit 1bc7068

4 files changed

Lines changed: 26 additions & 3 deletions

File tree

Doc/library/poplib.rst

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,10 @@ The :mod:`poplib` module provides two classes:
5353
.. versionchanged:: 3.2
5454
*context* parameter added.
5555

56+
.. versionchanged:: 3.4
57+
The class now supports hostname check with
58+
:attr:`SSLContext.check_hostname` and *Server Name Indicator* (see
59+
:data:`~ssl.HAS_SNI`).
5660

5761
One exception is defined as an attribute of the :mod:`poplib` module:
5862

@@ -198,6 +202,11 @@ An :class:`POP3` instance has the following methods:
198202

199203
.. versionadded:: 3.4
200204

205+
.. versionchanged:: 3.4
206+
The method now supports hostname check with
207+
:attr:`SSLContext.check_hostname` and *Server Name Indicator* (see
208+
:data:`~ssl.HAS_SNI`).
209+
201210

202211
Instances of :class:`POP3_SSL` have no additional methods. The interface of this
203212
subclass is identical to its parent.

Lib/poplib.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -387,7 +387,9 @@ def stls(self, context=None):
387387
if context is None:
388388
context = ssl._create_stdlib_context()
389389
resp = self._shortcmd('STLS')
390-
self.sock = context.wrap_socket(self.sock)
390+
server_hostname = self.host if ssl.HAS_SNI else None
391+
self.sock = context.wrap_socket(self.sock,
392+
server_hostname=server_hostname)
391393
self.file = self.sock.makefile('rb')
392394
self._tls_established = True
393395
return resp
@@ -428,7 +430,9 @@ def __init__(self, host, port=POP3_SSL_PORT, keyfile=None, certfile=None,
428430

429431
def _create_socket(self, timeout):
430432
sock = POP3._create_socket(self, timeout)
431-
sock = self.context.wrap_socket(sock)
433+
server_hostname = self.host if ssl.HAS_SNI else None
434+
sock = self.context.wrap_socket(sock,
435+
server_hostname=server_hostname)
432436
return sock
433437

434438
def stls(self, keyfile=None, certfile=None, context=None):

Lib/test/test_poplib.py

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,8 @@
2323
import ssl
2424

2525
SUPPORTS_SSL = True
26-
CERTFILE = os.path.join(os.path.dirname(__file__) or os.curdir, "keycert.pem")
26+
CERTFILE = os.path.join(os.path.dirname(__file__) or os.curdir, "keycert3.pem")
27+
CAFILE = os.path.join(os.path.dirname(__file__) or os.curdir, "pycacert.pem")
2728
requires_ssl = skipUnless(SUPPORTS_SSL, 'SSL not supported')
2829

2930
# the dummy data returned by server when LIST and RETR commands are issued
@@ -332,6 +333,12 @@ def test_stls(self):
332333
def test_stls_context(self):
333334
expected = b'+OK Begin TLS negotiation'
334335
ctx = ssl.SSLContext(ssl.PROTOCOL_TLSv1)
336+
ctx.load_verify_locations(CAFILE)
337+
ctx.verify_mode = ssl.CERT_REQUIRED
338+
ctx.check_hostname = True
339+
with self.assertRaises(ssl.CertificateError):
340+
resp = self.client.stls(context=ctx)
341+
self.client = poplib.POP3("localhost", self.server.port, timeout=3)
335342
resp = self.client.stls(context=ctx)
336343
self.assertEqual(resp, expected)
337344

Misc/NEWS

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,9 @@ Core and Builtins
1818
Library
1919
-------
2020

21+
- Issue #19784: poplib now supports SSLContext.check_hostname and server name
22+
indication for TLS/SSL connections.
23+
2124
- Issue #19782: imaplib now supports SSLContext.check_hostname and server name
2225
indication for TLS/SSL connections.
2326

0 commit comments

Comments
 (0)