Use SSLContext if available so we send SNI

Fixes https://github.com/imaplib2/imaplib2/issues/5

Signed-off-by: Nicolas Sebrecht <nicolas.s-dev@laposte.net>
This commit is contained in:
Julien Cristau 2018-09-13 11:57:58 +02:00 committed by Nicolas Sebrecht
parent 741b33b455
commit 0a831b3cab

View File

@ -545,7 +545,16 @@ class IMAP4(object):
ssl_version = TLS_MAP[self.tls_level][self.ssl_version] ssl_version = TLS_MAP[self.tls_level][self.ssl_version]
self.sock = ssl.wrap_socket(self.sock, self.keyfile, self.certfile, ca_certs=self.ca_certs, cert_reqs=cert_reqs, ssl_version=ssl_version) if getattr(ssl, 'HAS_SNI', False):
ctx = ssl.SSLContext(ssl_version)
ctx.verify_mode = cert_reqs
if self.ca_certs is not None:
ctx.load_verify_locations(self.ca_certs)
if self.certfile or self.keyfile:
ctx.load_cert_chain(self.certfile, self.keyfile)
self.sock = ctx.wrap_socket(self.sock, server_hostname=self.host)
else:
self.sock = ssl.wrap_socket(self.sock, self.keyfile, self.certfile, ca_certs=self.ca_certs, cert_reqs=cert_reqs, ssl_version=ssl_version)
ssl_exc = ssl.SSLError ssl_exc = ssl.SSLError
self.read_fd = self.sock.fileno() self.read_fd = self.sock.fileno()
except ImportError: except ImportError: