Rietveld Code Review Tool
Help | Bug tracker | Discussion group | Source code | Sign in
(86231)

Side by Side Diff: Lib/imaplib.py

Issue 19689: ssl.create_default_context()
Patch Set: Created 5 years, 3 months ago
Left:
Right:
Use n/p to move between diff chunks; N/P to move between comments. Please Sign in to add in-line comments.
Jump to:
View unified diff | Download patch
« no previous file with comments | « Lib/http/client.py ('k') | Lib/nntplib.py » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 """IMAP4 client. 1 """IMAP4 client.
2 2
3 Based on RFC 2060. 3 Based on RFC 2060.
4 4
5 Public class: IMAP4 5 Public class: IMAP4
6 Public variable: Debug 6 Public variable: Debug
7 Public functions: Internaldate2tuple 7 Public functions: Internaldate2tuple
8 Int2AP 8 Int2AP
9 ParseFlags 9 ParseFlags
10 Time2Internaldate 10 Time2Internaldate
(...skipping 724 matching lines...) Expand 10 before | Expand all | Expand 10 after
735 def starttls(self, ssl_context=None): 735 def starttls(self, ssl_context=None):
736 name = 'STARTTLS' 736 name = 'STARTTLS'
737 if not HAVE_SSL: 737 if not HAVE_SSL:
738 raise self.error('SSL support missing') 738 raise self.error('SSL support missing')
739 if self._tls_established: 739 if self._tls_established:
740 raise self.abort('TLS session already established') 740 raise self.abort('TLS session already established')
741 if name not in self.capabilities: 741 if name not in self.capabilities:
742 raise self.abort('TLS not supported by server') 742 raise self.abort('TLS not supported by server')
743 # Generate a default SSL context if none was passed. 743 # Generate a default SSL context if none was passed.
744 if ssl_context is None: 744 if ssl_context is None:
745 ssl_context = ssl.SSLContext(ssl.PROTOCOL_SSLv23) 745 ssl_context = ssl.create_default_context()
746 # SSLv2 considered harmful.
747 ssl_context.options |= ssl.OP_NO_SSLv2
748 typ, dat = self._simple_command(name) 746 typ, dat = self._simple_command(name)
749 if typ == 'OK': 747 if typ == 'OK':
750 self.sock = ssl_context.wrap_socket(self.sock) 748 self.sock = ssl_context.wrap_socket(self.sock)
751 self.file = self.sock.makefile('rb') 749 self.file = self.sock.makefile('rb')
752 self._tls_established = True 750 self._tls_established = True
753 self._get_capabilities() 751 self._get_capabilities()
754 else: 752 else:
755 raise self.error("Couldn't establish TLS session") 753 raise self.error("Couldn't establish TLS session")
756 return self._untagged_response(typ, dat, name) 754 return self._untagged_response(typ, dat, name)
757 755
(...skipping 445 matching lines...) Expand 10 before | Expand all | Expand 10 after
1203 def __init__(self, host='', port=IMAP4_SSL_PORT, keyfile=None, certfile= None, ssl_context=None): 1201 def __init__(self, host='', port=IMAP4_SSL_PORT, keyfile=None, certfile= None, ssl_context=None):
1204 if ssl_context is not None and keyfile is not None: 1202 if ssl_context is not None and keyfile is not None:
1205 raise ValueError("ssl_context and keyfile arguments are mutually " 1203 raise ValueError("ssl_context and keyfile arguments are mutually "
1206 "exclusive") 1204 "exclusive")
1207 if ssl_context is not None and certfile is not None: 1205 if ssl_context is not None and certfile is not None:
1208 raise ValueError("ssl_context and certfile arguments are mutuall y " 1206 raise ValueError("ssl_context and certfile arguments are mutuall y "
1209 "exclusive") 1207 "exclusive")
1210 1208
1211 self.keyfile = keyfile 1209 self.keyfile = keyfile
1212 self.certfile = certfile 1210 self.certfile = certfile
1211 if ssl_context is None:
1212 ssl_context = ssl.create_default_context(certfile=certfile,
1213 keyfile=keyfile)
1213 self.ssl_context = ssl_context 1214 self.ssl_context = ssl_context
1214 IMAP4.__init__(self, host, port) 1215 IMAP4.__init__(self, host, port)
1215 1216
1216 def _create_socket(self): 1217 def _create_socket(self):
1217 sock = IMAP4._create_socket(self) 1218 sock = IMAP4._create_socket(self)
1218 if self.ssl_context: 1219 return self.ssl_context.wrap_socket(sock)
1219 return self.ssl_context.wrap_socket(sock)
1220 else:
1221 return ssl.wrap_socket(sock, self.keyfile, self.certfile)
1222 1220
1223 def open(self, host='', port=IMAP4_SSL_PORT): 1221 def open(self, host='', port=IMAP4_SSL_PORT):
1224 """Setup connection to remote server on "host:port". 1222 """Setup connection to remote server on "host:port".
1225 (default: localhost:standard IMAP4 SSL port). 1223 (default: localhost:standard IMAP4 SSL port).
1226 This connection will be used by the routines: 1224 This connection will be used by the routines:
1227 read, readline, send, shutdown. 1225 read, readline, send, shutdown.
1228 """ 1226 """
1229 IMAP4.open(self, host, port) 1227 IMAP4.open(self, host, port)
1230 1228
1231 __all__.append("IMAP4_SSL") 1229 __all__.append("IMAP4_SSL")
(...skipping 301 matching lines...) Expand 10 before | Expand all | Expand 10 after
1533 except: 1531 except:
1534 print('\nTests failed.') 1532 print('\nTests failed.')
1535 1533
1536 if not Debug: 1534 if not Debug:
1537 print(''' 1535 print('''
1538 If you would like to see debugging output, 1536 If you would like to see debugging output,
1539 try: %s -d5 1537 try: %s -d5
1540 ''' % sys.argv[0]) 1538 ''' % sys.argv[0])
1541 1539
1542 raise 1540 raise
OLDNEW
« no previous file with comments | « Lib/http/client.py ('k') | Lib/nntplib.py » ('j') | no next file with comments »

RSS Feeds Recent Issues | This issue
This is Rietveld 894c83f36cb7+