diff -r c8f283cd3e6e Lib/imaplib.py --- a/Lib/imaplib.py Wed Apr 27 19:30:16 2011 +0200 +++ b/Lib/imaplib.py Wed Apr 27 16:40:55 2011 -0400 @@ -1177,25 +1177,30 @@ """IMAP4 client class over SSL connection - Instantiate with: IMAP4_SSL([host[, port[, keyfile[, certfile]]]]) + Instantiate with: IMAP4_SSL([host[, port[, keyfile[, certfile[, ssl_context]]]]]) host - host's name (default: localhost); - port - port number (default: standard IMAP4 SSL port). + port - port number (default: standard IMAP4 SSL port); keyfile - PEM formatted file that contains your private key (default: None); certfile - PEM formatted certificate chain file (default: None); - + ssl_context - a SSLContext object that contains your certificate chain and private key, + if ssl_context is provided, the parameters keyfile and certfile are ignored (default: None); for more documentation see the docstring of the parent class IMAP4. """ - def __init__(self, host = '', port = IMAP4_SSL_PORT, keyfile = None, certfile = None): + def __init__(self, host = '', port = IMAP4_SSL_PORT, keyfile = None, certfile = None, ssl_context = None): self.keyfile = keyfile self.certfile = certfile + self.ssl_context = ssl_context IMAP4.__init__(self, host, port) def _create_socket(self): sock = IMAP4._create_socket(self) - return ssl.wrap_socket(sock, self.keyfile, self.certfile) + if self.ssl_context: + return self.ssl_context.wrap_socket(sock) + else: + return ssl.wrap_socket(sock, self.keyfile, self.certfile) def open(self, host='', port=IMAP4_SSL_PORT): """Setup connection to remote server on "host:port". diff -r c8f283cd3e6e Lib/test/test_imaplib.py --- a/Lib/test/test_imaplib.py Wed Apr 27 19:30:16 2011 +0200 +++ b/Lib/test/test_imaplib.py Wed Apr 27 16:40:55 2011 -0400 @@ -258,11 +258,26 @@ port = 993 imap_class = IMAP4_SSL + def check_logincapa(self, server): + for cap in server.capabilities: + self.assertIsInstance(cap, str) + self.assertFalse('LOGINDISABLED' in server.capabilities) + self.assertTrue('AUTH=PLAIN' in server.capabilities) + def test_logincapa(self): - for cap in self.server.capabilities: - self.assertIsInstance(cap, str) - self.assertFalse('LOGINDISABLED' in self.server.capabilities) - self.assertTrue('AUTH=PLAIN' in self.server.capabilities) + self.check_logincapa(self.server) + + def test_logincapa_with_client_private_key_file(self): + with transient_internet(self.host): + _server = self.imap_class(self.host, self.port, certfile = CERTFILE) + self.check_logincapa(_server) + + def test_logincapa_with_client_ssl_context(self): + with transient_internet(self.host): + ssl_context = ssl.SSLContext(ssl.PROTOCOL_SSLv23) + ssl_context.load_cert_chain(CERTFILE) + _server = self.imap_class(self.host, self.port, ssl_context = ssl_context) + self.check_logincapa(_server) def test_main():