<s> '''<STR_LIT>''' <EOL> from __future__ import absolute_import <EOL> try : <EOL> from ndg . httpsclient . ssl_peer_verification import SUBJ_ALT_NAME_SUPPORT <EOL> from ndg . httpsclient . subj_alt_name import SubjectAltName as BaseSubjectAltName <EOL> except SyntaxError as e : <EOL> raise ImportError ( e ) <EOL> import OpenSSL . SSL <EOL> from pyasn1 . codec . der import decoder as der_decoder <EOL> from pyasn1 . type import univ , constraint <EOL> from socket import timeout , error as SocketError <EOL> try : <EOL> from socket import _fileobject <EOL> except ImportError : <EOL> _fileobject = None <EOL> from urllib3 . packages . backports . makefile import backport_makefile <EOL> import ssl <EOL> import select <EOL> import six <EOL> from . . import connection <EOL> from . . import util <EOL> __all__ = [ '<STR_LIT>' , '<STR_LIT>' ] <EOL> HAS_SNI = SUBJ_ALT_NAME_SUPPORT <EOL> _openssl_versions = { <EOL> ssl . PROTOCOL_SSLv23 : OpenSSL . SSL . SSLv23_METHOD , <EOL> ssl . PROTOCOL_TLSv1 : OpenSSL . SSL . TLSv1_METHOD , <EOL> } <EOL> if hasattr ( ssl , '<STR_LIT>' ) and hasattr ( OpenSSL . SSL , '<STR_LIT>' ) : <EOL> _openssl_versions [ ssl . PROTOCOL_TLSv1_1 ] = OpenSSL . SSL . TLSv1_1_METHOD <EOL> if hasattr ( ssl , '<STR_LIT>' ) and hasattr ( OpenSSL . SSL , '<STR_LIT>' ) : <EOL> _openssl_versions [ ssl . PROTOCOL_TLSv1_2 ] = OpenSSL . SSL . TLSv1_2_METHOD <EOL> try : <EOL> _openssl_versions . update ( { ssl . PROTOCOL_SSLv3 : OpenSSL . SSL . SSLv3_METHOD } ) <EOL> except AttributeError : <EOL> pass <EOL> _openssl_verify = { <EOL> ssl . CERT_NONE : OpenSSL . SSL . VERIFY_NONE , <EOL> ssl . CERT_OPTIONAL : OpenSSL . SSL . VERIFY_PEER , <EOL> ssl . CERT_REQUIRED : <EOL> OpenSSL . SSL . VERIFY_PEER + OpenSSL . SSL . VERIFY_FAIL_IF_NO_PEER_CERT , <EOL> } <EOL> DEFAULT_SSL_CIPHER_LIST = util . ssl_ . DEFAULT_CIPHERS . encode ( '<STR_LIT:ascii>' ) <EOL> SSL_WRITE_BLOCKSIZE = <NUM_LIT> <EOL> orig_util_HAS_SNI = util . HAS_SNI <EOL> orig_connection_ssl_wrap_socket = connection . ssl_wrap_socket <EOL> def inject_into_urllib3 ( ) : <EOL> '<STR_LIT>' <EOL> connection . ssl_wrap_socket = ssl_wrap_socket <EOL> util . HAS_SNI = HAS_SNI <EOL> util . IS_PYOPENSSL = True <EOL> def extract_from_urllib3 ( ) : <EOL> '<STR_LIT>' <EOL> connection . ssl_wrap_socket = orig_connection_ssl_wrap_socket <EOL> util . HAS_SNI = orig_util_HAS_SNI <EOL> util . IS_PYOPENSSL = False <EOL> class SubjectAltName ( BaseSubjectAltName ) : <EOL> '''<STR_LIT>''' <EOL> sizeSpec = univ . SequenceOf . sizeSpec + constraint . ValueSizeConstraint ( <NUM_LIT:1> , <NUM_LIT> ) <EOL> def get_subj_alt_name ( peer_cert ) : <EOL> dns_name = [ ] <EOL> if not SUBJ_ALT_NAME_SUPPORT : <EOL> return dns_name <EOL> general_names = SubjectAltName ( ) <EOL> for i in range ( peer_cert . get_extension_count ( ) ) : <EOL> ext = peer_cert . get_extension ( i ) <EOL> ext_name = ext . get_short_name ( ) <EOL> if ext_name != b'<STR_LIT>' : <EOL> continue <EOL> ext_dat = ext . get_data ( ) <EOL> decoded_dat = der_decoder . decode ( ext_dat , <EOL> asn1Spec = general_names ) <EOL> for name in decoded_dat : <EOL> if not isinstance ( name , SubjectAltName ) : <EOL> continue <EOL> for entry in range ( len ( name ) ) : <EOL> component = name . getComponentByPosition ( entry ) <EOL> if component . getName ( ) != '<STR_LIT>' : <EOL> continue <EOL> dns_name . append ( str ( component . getComponent ( ) ) ) <EOL> return dns_name <EOL> class WrappedSocket ( object ) : <EOL> '''<STR_LIT>''' <EOL> def __init__ ( self , connection , socket , suppress_ragged_eofs = True ) : <EOL> self . connection = connection <EOL> self . socket = socket <EOL> self . suppress_ragged_eofs = suppress_ragged_eofs <EOL> self . _makefile_refs = <NUM_LIT:0> <EOL> self . _closed = False <EOL> def fileno ( self ) : <EOL> return self . socket . fileno ( ) <EOL> def _decref_socketios ( self ) : <EOL> if self . _makefile_refs > <NUM_LIT:0> : <EOL> self . _makefile_refs -= <NUM_LIT:1> <EOL> if self . _closed : <EOL> self . close ( ) <EOL> def recv ( self , * args , ** kwargs ) : <EOL> try : <EOL> data = self . connection . recv ( * args , ** kwargs ) <EOL> except OpenSSL . SSL . SysCallError as e : <EOL> if self . suppress_ragged_eofs and e . args == ( - <NUM_LIT:1> , '<STR_LIT>' ) : <EOL> return b'<STR_LIT>' <EOL> else : <EOL> raise SocketError ( str ( e ) ) <EOL> except OpenSSL . SSL . ZeroReturnError as e : <EOL> if self . connection . get_shutdown ( ) == OpenSSL . SSL . RECEIVED_SHUTDOWN : <EOL> return b'<STR_LIT>' <EOL> else : <EOL> raise <EOL> except OpenSSL . SSL . WantReadError : <EOL> rd , wd , ed = select . select ( <EOL> [ self . socket ] , [ ] , [ ] , self . socket . gettimeout ( ) ) <EOL> if not rd : <EOL> raise timeout ( '<STR_LIT>' ) <EOL> else : <EOL> return self . recv ( * args , ** kwargs ) <EOL> else : <EOL> return data <EOL> def recv_into ( self , * args , ** kwargs ) : <EOL> try : <EOL> return self . connection . recv_into ( * args , ** kwargs ) <EOL> except OpenSSL . SSL . SysCallError as e : <EOL> if self . suppress_ragged_eofs and e . args == ( - <NUM_LIT:1> , '<STR_LIT>' ) : <EOL> return <NUM_LIT:0> <EOL> else : <EOL> raise SocketError ( str ( e ) ) <EOL> except OpenSSL . SSL . ZeroReturnError as e : <EOL> if self . connection . get_shutdown ( ) == OpenSSL . SSL . RECEIVED_SHUTDOWN : <EOL> return <NUM_LIT:0> <EOL> else : <EOL> raise <EOL> except OpenSSL . SSL . WantReadError : <EOL> rd , wd , ed = select . select ( <EOL> [ self . socket ] , [ ] , [ ] , self . socket . gettimeout ( ) ) <EOL> if not rd : <EOL> raise timeout ( '<STR_LIT>' ) <EOL> else : <EOL> return self . recv_into ( * args , ** kwargs ) <EOL> def settimeout ( self , timeout ) : <EOL> return self . socket . settimeout ( timeout ) <EOL> def _send_until_done ( self , data ) : <EOL> while True : <EOL> try : <EOL> return self . connection . send ( data ) <EOL> except OpenSSL . SSL . WantWriteError : <EOL> _ , wlist , _ = select . select ( [ ] , [ self . socket ] , [ ] , <EOL> self . socket . gettimeout ( ) ) <EOL> if not wlist : <EOL> raise timeout ( ) <EOL> continue <EOL> def sendall ( self , data ) : <EOL> total_sent = <NUM_LIT:0> <EOL> while total_sent < len ( data ) : <EOL> sent = self . _send_until_done ( data [ total_sent : total_sent + SSL_WRITE_BLOCKSIZE ] ) <EOL> total_sent += sent <EOL> def shutdown ( self ) : <EOL> self . connection . shutdown ( ) <EOL> def close ( self ) : <EOL> if self . _makefile_refs < <NUM_LIT:1> : <EOL> try : <EOL> self . _closed = True <EOL> return self . connection . close ( ) <EOL> except OpenSSL . SSL . Error : <EOL> return <EOL> else : <EOL> self . _makefile_refs -= <NUM_LIT:1> <EOL> def getpeercert ( self , binary_form = False ) : <EOL> x509 = self . connection . get_peer_certificate ( ) <EOL> if not x509 : <EOL> return x509 <EOL> if binary_form : <EOL> return OpenSSL . crypto . dump_certificate ( <EOL> OpenSSL . crypto . FILETYPE_ASN1 , <EOL> x509 ) <EOL> return { <EOL> '<STR_LIT>' : ( <EOL> ( ( '<STR_LIT>' , x509 . get_subject ( ) . CN ) , ) , <EOL> ) , <EOL> '<STR_LIT>' : [ <EOL> ( '<STR_LIT>' , value ) <EOL> for value in get_subj_alt_name ( x509 ) <EOL> ] <EOL> } <EOL> def _reuse ( self ) : <EOL> self . _makefile_refs += <NUM_LIT:1> <EOL> def _drop ( self ) : <EOL> if self . _makefile_refs < <NUM_LIT:1> : <EOL> self . close ( ) <EOL> else : <EOL> self . _makefile_refs -= <NUM_LIT:1> <EOL> if _fileobject : <EOL> def makefile ( self , mode , bufsize = - <NUM_LIT:1> ) : <EOL> self . _makefile_refs += <NUM_LIT:1> <EOL> return _fileobject ( self , mode , bufsize , close = True ) <EOL> else : <EOL> makefile = backport_makefile <EOL> WrappedSocket . makefile = makefile <EOL> def _verify_callback ( cnx , x509 , err_no , err_depth , return_code ) : <EOL> return err_no == <NUM_LIT:0> <EOL> def ssl_wrap_socket ( sock , keyfile = None , certfile = None , cert_reqs = None , <EOL> ca_certs = None , server_hostname = None , <EOL> ssl_version = None , ca_cert_dir = None ) : <EOL> ctx = OpenSSL . SSL . Context ( _openssl_versions [ ssl_version ] ) <EOL> if certfile : <EOL> keyfile = keyfile or certfile <EOL> ctx . use_certificate_file ( certfile ) <EOL> if keyfile : <EOL> ctx . use_privatekey_file ( keyfile ) <EOL> if cert_reqs != ssl . CERT_NONE : <EOL> ctx . set_verify ( _openssl_verify [ cert_reqs ] , _verify_callback ) <EOL> if ca_certs or ca_cert_dir : <EOL> try : <EOL> ctx . load_verify_locations ( ca_certs , ca_cert_dir ) <EOL> except OpenSSL . SSL . Error as e : <EOL> raise ssl . SSLError ( '<STR_LIT>' % ca_certs , e ) <EOL> else : <EOL> ctx . set_default_verify_paths ( ) <EOL> OP_NO_COMPRESSION = <NUM_LIT> <EOL> ctx . set_options ( OP_NO_COMPRESSION ) <EOL> ctx . set_cipher_list ( DEFAULT_SSL_CIPHER_LIST ) <EOL> cnx = OpenSSL . SSL . Connection ( ctx , sock ) <EOL> if isinstance ( server_hostname , six . text_type ) : <EOL> server_hostname = server_hostname . encode ( '<STR_LIT:utf-8>' ) <EOL> cnx . set_tlsext_host_name ( server_hostname ) <EOL> cnx . set_connect_state ( ) <EOL> while True : <EOL> try : <EOL> cnx . do_handshake ( ) <EOL> except OpenSSL . SSL . WantReadError : <EOL> rd , _ , _ = select . select ( [ sock ] , [ ] , [ ] , sock . gettimeout ( ) ) <EOL> if not rd : <EOL> raise timeout ( '<STR_LIT>' ) <EOL> continue <EOL> except OpenSSL . SSL . Error as e : <EOL> raise ssl . SSLError ( '<STR_LIT>' % e ) <EOL> break <EOL> return WrappedSocket ( cnx , sock ) </s>
