<s> """<STR_LIT>""" <EOL> import ssl <EOL> import warnings <EOL> from . . _compat import MODERN_SSL <EOL> try : <EOL> from ssl import ( <EOL> OP_NO_SSLv2 , OP_NO_SSLv3 , OP_NO_COMPRESSION , <EOL> OP_CIPHER_SERVER_PREFERENCE , OP_SINGLE_DH_USE , OP_SINGLE_ECDH_USE <EOL> ) <EOL> except ImportError : <EOL> OP_NO_SSLv2 = <NUM_LIT> <EOL> OP_NO_SSLv3 = <NUM_LIT> <EOL> OP_NO_COMPRESSION = <NUM_LIT> <EOL> OP_CIPHER_SERVER_PREFERENCE = <NUM_LIT> <EOL> OP_SINGLE_DH_USE = <NUM_LIT> <EOL> OP_SINGLE_ECDH_USE = <NUM_LIT> <EOL> DEFAULT_CIPHERS = ( <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> ) <EOL> RESTRICTED_SERVER_CIPHERS = ( <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> ) <EOL> class InsecurePlatformWarning ( Warning ) : <EOL> """<STR_LIT>""" <EOL> pass <EOL> try : <EOL> from ssl import SSLContext <EOL> except ImportError : <EOL> import sys <EOL> class SSLContext ( object ) : <EOL> supports_set_ciphers = ( ( <NUM_LIT:2> , <NUM_LIT:7> ) <= sys . version_info < ( <NUM_LIT:3> , ) or <EOL> ( <NUM_LIT:3> , <NUM_LIT:2> ) <= sys . version_info ) <EOL> def __init__ ( self , protocol_version ) : <EOL> self . protocol = protocol_version <EOL> self . check_hostname = False <EOL> self . verify_mode = ssl . CERT_NONE <EOL> self . ca_certs = None <EOL> self . options = <NUM_LIT:0> <EOL> self . certfile = None <EOL> self . keyfile = None <EOL> self . ciphers = None <EOL> def load_cert_chain ( self , certfile = None , keyfile = None ) : <EOL> self . certfile = certfile <EOL> self . keyfile = keyfile <EOL> def load_verify_locations ( self , cafile = None , capath = None ) : <EOL> if capath is not None : <EOL> raise OSError ( "<STR_LIT>" ) <EOL> self . ca_certs = cafile <EOL> def set_ciphers ( self , cipher_suite ) : <EOL> if not self . supports_set_ciphers : <EOL> raise TypeError ( <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> ) <EOL> self . ciphers = cipher_suite <EOL> def wrap_socket ( self , socket , server_hostname = None , server_side = False ) : <EOL> warnings . warn ( <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" , <EOL> InsecurePlatformWarning <EOL> ) <EOL> kwargs = { <EOL> "<STR_LIT>" : self . keyfile , <EOL> "<STR_LIT>" : self . certfile , <EOL> "<STR_LIT>" : self . ca_certs , <EOL> "<STR_LIT>" : self . verify_mode , <EOL> "<STR_LIT>" : self . protocol , <EOL> "<STR_LIT>" : server_side , <EOL> } <EOL> if self . supports_set_ciphers : <EOL> return ssl . wrap_socket ( socket , ciphers = self . ciphers , ** kwargs ) <EOL> else : <EOL> return ssl . wrap_socket ( socket , ** kwargs ) <EOL> def create_thriftpy_context ( server_side = False , ciphers = None ) : <EOL> """<STR_LIT>""" <EOL> if MODERN_SSL : <EOL> if server_side : <EOL> context = ssl . create_default_context ( ssl . Purpose . CLIENT_AUTH ) <EOL> else : <EOL> context = ssl . create_default_context ( ssl . Purpose . SERVER_AUTH ) <EOL> if ciphers : <EOL> context . set_ciphers ( ciphers ) <EOL> else : <EOL> context = SSLContext ( ssl . PROTOCOL_SSLv23 ) <EOL> context . options |= OP_NO_SSLv2 <EOL> context . options |= OP_NO_SSLv3 <EOL> context . options |= OP_NO_COMPRESSION <EOL> if server_side : <EOL> context . options |= OP_CIPHER_SERVER_PREFERENCE <EOL> context . options |= OP_SINGLE_DH_USE <EOL> context . options |= OP_SINGLE_ECDH_USE <EOL> else : <EOL> context . verify_mode = ssl . CERT_REQUIRED <EOL> warnings . warn ( <EOL> "<STR_LIT>" , <EOL> InsecurePlatformWarning ) <EOL> if getattr ( context , '<STR_LIT>' , True ) : <EOL> if ciphers : <EOL> context . set_ciphers ( ciphers ) <EOL> else : <EOL> warnings . warn ( "<STR_LIT>" , <EOL> InsecurePlatformWarning ) <EOL> return context </s>
