<s> from binascii import hexlify , unhexlify <EOL> from hashlib import md5 , sha1 , sha256 <EOL> from . . exceptions import SSLError , InsecurePlatformWarning <EOL> SSLContext = None <EOL> HAS_SNI = False <EOL> create_default_context = None <EOL> HASHFUNC_MAP = { <EOL> <NUM_LIT:32> : md5 , <EOL> <NUM_LIT> : sha1 , <EOL> <NUM_LIT:64> : sha256 , <EOL> } <EOL> import errno <EOL> import warnings <EOL> try : <EOL> import ssl <EOL> from ssl import wrap_socket , CERT_NONE , PROTOCOL_SSLv23 <EOL> from ssl import HAS_SNI <EOL> except ImportError : <EOL> pass <EOL> try : <EOL> from ssl import OP_NO_SSLv2 , OP_NO_SSLv3 , OP_NO_COMPRESSION <EOL> except ImportError : <EOL> OP_NO_SSLv2 , OP_NO_SSLv3 = <NUM_LIT> , <NUM_LIT> <EOL> OP_NO_COMPRESSION = <NUM_LIT> <EOL> DEFAULT_CIPHERS = ( <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> ) <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 , keyfile ) : <EOL> self . certfile = certfile <EOL> self . keyfile = keyfile <EOL> def load_verify_locations ( self , cafile = None , capath = None ) : <EOL> self . ca_certs = cafile <EOL> if capath is not None : <EOL> raise SSLError ( "<STR_LIT>" ) <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 ) : <EOL> warnings . warn ( <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <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> } <EOL> if self . supports_set_ciphers : <EOL> return wrap_socket ( socket , ciphers = self . ciphers , ** kwargs ) <EOL> else : <EOL> return wrap_socket ( socket , ** kwargs ) <EOL> def assert_fingerprint ( cert , fingerprint ) : <EOL> """<STR_LIT>""" <EOL> fingerprint = fingerprint . replace ( '<STR_LIT::>' , '<STR_LIT>' ) . lower ( ) <EOL> digest_length = len ( fingerprint ) <EOL> hashfunc = HASHFUNC_MAP . get ( digest_length ) <EOL> if not hashfunc : <EOL> raise SSLError ( <EOL> '<STR_LIT>' . format ( fingerprint ) ) <EOL> fingerprint_bytes = unhexlify ( fingerprint . encode ( ) ) <EOL> cert_digest = hashfunc ( cert ) . digest ( ) <EOL> if cert_digest != fingerprint_bytes : <EOL> raise SSLError ( '<STR_LIT>' <EOL> . format ( fingerprint , hexlify ( cert_digest ) ) ) <EOL> def resolve_cert_reqs ( candidate ) : <EOL> """<STR_LIT>""" <EOL> if candidate is None : <EOL> return CERT_NONE <EOL> if isinstance ( candidate , str ) : <EOL> res = getattr ( ssl , candidate , None ) <EOL> if res is None : <EOL> res = getattr ( ssl , '<STR_LIT>' + candidate ) <EOL> return res <EOL> return candidate <EOL> def resolve_ssl_version ( candidate ) : <EOL> """<STR_LIT>""" <EOL> if candidate is None : <EOL> return PROTOCOL_SSLv23 <EOL> if isinstance ( candidate , str ) : <EOL> res = getattr ( ssl , candidate , None ) <EOL> if res is None : <EOL> res = getattr ( ssl , '<STR_LIT>' + candidate ) <EOL> return res <EOL> return candidate <EOL> def create_urllib3_context ( ssl_version = None , cert_reqs = None , <EOL> options = None , ciphers = None ) : <EOL> """<STR_LIT>""" <EOL> context = SSLContext ( ssl_version or ssl . PROTOCOL_SSLv23 ) <EOL> cert_reqs = ssl . CERT_REQUIRED if cert_reqs is None else cert_reqs <EOL> if options is None : <EOL> options = <NUM_LIT:0> <EOL> options |= OP_NO_SSLv2 <EOL> options |= OP_NO_SSLv3 <EOL> options |= OP_NO_COMPRESSION <EOL> context . options |= options <EOL> if getattr ( context , '<STR_LIT>' , True ) : <EOL> context . set_ciphers ( ciphers or DEFAULT_CIPHERS ) <EOL> context . verify_mode = cert_reqs <EOL> if getattr ( context , '<STR_LIT>' , None ) is not None : <EOL> context . check_hostname = False <EOL> return context <EOL> def ssl_wrap_socket ( sock , keyfile = None , certfile = None , cert_reqs = None , <EOL> ca_certs = None , server_hostname = None , <EOL> ssl_version = None , ciphers = None , ssl_context = None , <EOL> ca_cert_dir = None ) : <EOL> """<STR_LIT>""" <EOL> context = ssl_context <EOL> if context is None : <EOL> context = create_urllib3_context ( ssl_version , cert_reqs , <EOL> ciphers = ciphers ) <EOL> if ca_certs or ca_cert_dir : <EOL> try : <EOL> context . load_verify_locations ( ca_certs , ca_cert_dir ) <EOL> except IOError as e : <EOL> raise SSLError ( e ) <EOL> except OSError as e : <EOL> if e . errno == errno . ENOENT : <EOL> raise SSLError ( e ) <EOL> raise <EOL> if certfile : <EOL> context . load_cert_chain ( certfile , keyfile ) <EOL> if HAS_SNI : <EOL> return context . wrap_socket ( sock , server_hostname = server_hostname ) <EOL> return context . wrap_socket ( sock ) </s>
