5252PROTOCOL_SSLv3
5353PROTOCOL_SSLv23
5454PROTOCOL_TLSv1
55+
56+ The following constants identify various SSL alert message descriptions as per
57+ http://www.iana.org/assignments/tls-parameters/tls-parameters.xml#tls-parameters-6
58+
59+ ALERT_DESCRIPTION_CLOSE_NOTIFY
60+ ALERT_DESCRIPTION_UNEXPECTED_MESSAGE
61+ ALERT_DESCRIPTION_BAD_RECORD_MAC
62+ ALERT_DESCRIPTION_RECORD_OVERFLOW
63+ ALERT_DESCRIPTION_DECOMPRESSION_FAILURE
64+ ALERT_DESCRIPTION_HANDSHAKE_FAILURE
65+ ALERT_DESCRIPTION_BAD_CERTIFICATE
66+ ALERT_DESCRIPTION_UNSUPPORTED_CERTIFICATE
67+ ALERT_DESCRIPTION_CERTIFICATE_REVOKED
68+ ALERT_DESCRIPTION_CERTIFICATE_EXPIRED
69+ ALERT_DESCRIPTION_CERTIFICATE_UNKNOWN
70+ ALERT_DESCRIPTION_ILLEGAL_PARAMETER
71+ ALERT_DESCRIPTION_UNKNOWN_CA
72+ ALERT_DESCRIPTION_ACCESS_DENIED
73+ ALERT_DESCRIPTION_DECODE_ERROR
74+ ALERT_DESCRIPTION_DECRYPT_ERROR
75+ ALERT_DESCRIPTION_PROTOCOL_VERSION
76+ ALERT_DESCRIPTION_INSUFFICIENT_SECURITY
77+ ALERT_DESCRIPTION_INTERNAL_ERROR
78+ ALERT_DESCRIPTION_USER_CANCELLED
79+ ALERT_DESCRIPTION_NO_RENEGOTIATION
80+ ALERT_DESCRIPTION_UNSUPPORTED_EXTENSION
81+ ALERT_DESCRIPTION_CERTIFICATE_UNOBTAINABLE
82+ ALERT_DESCRIPTION_UNRECOGNIZED_NAME
83+ ALERT_DESCRIPTION_BAD_CERTIFICATE_STATUS_RESPONSE
84+ ALERT_DESCRIPTION_BAD_CERTIFICATE_HASH_VALUE
85+ ALERT_DESCRIPTION_UNKNOWN_PSK_IDENTITY
5586"""
5687
5788import textwrap
6697 SSLSyscallError , SSLEOFError ,
6798 )
6899from _ssl import CERT_NONE , CERT_OPTIONAL , CERT_REQUIRED
69- from _ssl import (
70- OP_ALL , OP_NO_SSLv2 , OP_NO_SSLv3 , OP_NO_TLSv1 ,
71- OP_CIPHER_SERVER_PREFERENCE , OP_SINGLE_DH_USE
72- )
73- try :
74- from _ssl import OP_NO_COMPRESSION
75- except ImportError :
76- pass
77- try :
78- from _ssl import OP_SINGLE_ECDH_USE
79- except ImportError :
80- pass
81100from _ssl import RAND_status , RAND_egd , RAND_add , RAND_bytes , RAND_pseudo_bytes
82- from _ssl import (
83- SSL_ERROR_ZERO_RETURN ,
84- SSL_ERROR_WANT_READ ,
85- SSL_ERROR_WANT_WRITE ,
86- SSL_ERROR_WANT_X509_LOOKUP ,
87- SSL_ERROR_SYSCALL ,
88- SSL_ERROR_SSL ,
89- SSL_ERROR_WANT_CONNECT ,
90- SSL_ERROR_EOF ,
91- SSL_ERROR_INVALID_ERROR_CODE ,
92- )
101+
102+ def _import_symbols (prefix ):
103+ for n in dir (_ssl ):
104+ if n .startswith (prefix ):
105+ globals ()[n ] = getattr (_ssl , n )
106+
107+ _import_symbols ('OP_' )
108+ _import_symbols ('ALERT_DESCRIPTION_' )
109+ _import_symbols ('SSL_ERROR_' )
110+
93111from _ssl import HAS_SNI , HAS_ECDH , HAS_NPN
112+
94113from _ssl import (PROTOCOL_SSLv3 , PROTOCOL_SSLv23 ,
95114 PROTOCOL_TLSv1 )
96115from _ssl import _OPENSSL_API_VERSION
97116
117+
98118_PROTOCOL_NAMES = {
99119 PROTOCOL_TLSv1 : "TLSv1" ,
100120 PROTOCOL_SSLv23 : "SSLv23" ,
@@ -190,7 +210,7 @@ class SSLContext(_SSLContext):
190210 """An SSLContext holds various SSL-related configuration options and
191211 data, such as certificates and possibly a private key."""
192212
193- __slots__ = ('protocol' ,)
213+ __slots__ = ('protocol' , '__weakref__' )
194214
195215 def __new__ (cls , protocol , * args , ** kwargs ):
196216 self = _SSLContext .__new__ (cls , protocol )
@@ -238,7 +258,7 @@ def __init__(self, sock=None, keyfile=None, certfile=None,
238258 _context = None ):
239259
240260 if _context :
241- self .context = _context
261+ self ._context = _context
242262 else :
243263 if server_side and not certfile :
244264 raise ValueError ("certfile must be specified for server-side "
@@ -247,16 +267,16 @@ def __init__(self, sock=None, keyfile=None, certfile=None,
247267 raise ValueError ("certfile must be specified" )
248268 if certfile and not keyfile :
249269 keyfile = certfile
250- self .context = SSLContext (ssl_version )
251- self .context .verify_mode = cert_reqs
270+ self ._context = SSLContext (ssl_version )
271+ self ._context .verify_mode = cert_reqs
252272 if ca_certs :
253- self .context .load_verify_locations (ca_certs )
273+ self ._context .load_verify_locations (ca_certs )
254274 if certfile :
255- self .context .load_cert_chain (certfile , keyfile )
275+ self ._context .load_cert_chain (certfile , keyfile )
256276 if npn_protocols :
257- self .context .set_npn_protocols (npn_protocols )
277+ self ._context .set_npn_protocols (npn_protocols )
258278 if ciphers :
259- self .context .set_ciphers (ciphers )
279+ self ._context .set_ciphers (ciphers )
260280 self .keyfile = keyfile
261281 self .certfile = certfile
262282 self .cert_reqs = cert_reqs
@@ -298,7 +318,7 @@ def __init__(self, sock=None, keyfile=None, certfile=None,
298318 if connected :
299319 # create the SSL object
300320 try :
301- self ._sslobj = self .context ._wrap_socket (self , server_side ,
321+ self ._sslobj = self ._context ._wrap_socket (self , server_side ,
302322 server_hostname )
303323 if do_handshake_on_connect :
304324 timeout = self .gettimeout ()
@@ -310,6 +330,14 @@ def __init__(self, sock=None, keyfile=None, certfile=None,
310330 except OSError as x :
311331 self .close ()
312332 raise x
333+ @property
334+ def context (self ):
335+ return self ._context
336+
337+ @context .setter
338+ def context (self , ctx ):
339+ self ._context = ctx
340+ self ._sslobj .context = ctx
313341
314342 def dup (self ):
315343 raise NotImplemented ("Can't dup() %s instances" %
0 commit comments