3 nf\ev)@sdZddlmZddlZddlZddlZddlZddlZddl Z ddl Z ddl Z ddl Z ddl mZddlmZmZmZddlmZmZmZmZydd l mZWn$ek rdZdd lmZYnXd d gZd ZejZejj Z!e j"Z#e j$Z%dZ&ej'ej(ej)ej*ej+ej,ej-ej.ej/ej0ej1ej2ej3ej4ej5ej6ej7ej8ej9ej:ej;ejej?ej@ejAejBejCejDejEejFejGg!ZHe jIejJejKfiZLeMe drejNejNfeLe jO<eMe drejPejPfeLe jQ<eMe drejJejJfeLe jR<eMe drejSejSfeLe jT<eMe dr$ejKejKfeLe jU<eMe dr@eLe jIeLe jV<dd ZWdd ZXddZYddZZej[eYZ\ej]eZZ^Gddde_Z`erd$ddZan d%d dZaeae`_aGd!d"d"e_ZbdS)&aU SecureTranport support for urllib3 via ctypes. This makes platform-native TLS available to urllib3 users on macOS without the use of a compiler. This is an important feature because the Python Package Index is moving to become a TLSv1.2-or-higher server, and the default OpenSSL that ships with macOS is not capable of doing TLSv1.2. The only way to resolve this is to give macOS users an alternative solution to the problem, and that solution is to use SecureTransport. We use ctypes here because this solution must not require a compiler. That's because pip is not allowed to require a compiler either. This is not intended to be a seriously long-term solution to this problem. The hope is that PEP 543 will eventually solve this issue for us, at which point we can retire this contrib module. But in the short term, we need to solve the impending tire fire that is Python on Mac without this kind of contrib module. So...here we are. To use this module, simply import and inject it:: import urllib3.contrib.securetransport urllib3.contrib.securetransport.inject_into_urllib3() Happy TLSing! )absolute_importN)util)Security SecurityConstCoreFoundation)_assert_no_error_cert_array_from_pem_temporary_keychain_load_client_cert_chain) _fileobject)backport_makefileinject_into_urllib3extract_from_urllib3Ti@PROTOCOL_SSLv2PROTOCOL_SSLv3PROTOCOL_TLSv1PROTOCOL_TLSv1_1PROTOCOL_TLSv1_2 PROTOCOL_TLScCs(ttj_tt_ttj_dt_dtj_dS)zG Monkey-patch urllib3 with SecureTransport-backed SSL-support. TN)SecureTransportContextrssl_ SSLContextHAS_SNIIS_SECURETRANSPORTrr%/usr/lib/python3.6/securetransport.pyrs cCs(ttj_tt_ttj_dt_dtj_dS)z> Undo monkey-patching by :func:`inject_into_urllib3`. FN)orig_util_SSLContextrrrorig_util_HAS_SNIrrrrrrrs c Csxd}y8tj|}|dkr tjS|j}|d}|j}d}d}y|xv||kr|dksZ|dkrttj||sttjt j d||} t j | j ||} |j| | } || 7}| sB|stjSPqBWWnhtjk r"} zH| j }|dk o|t j kr||d<|t jks |t jkrtjSWYdd} ~ XnX||d<||krrrrsendallCszWrappedSocket.sendallc Cs$|jtj|jWdQRXdS)N)rLrZSSLCloserA)rIrrrshutdownIs zWrappedSocket.shutdowncCs|jdkrd|_|jr(tj|jd|_|jr@tj|jd|_|jrvtj|jtj|jt j |j d|_|_ |j j S|jd8_dS)NrT)rBrCrArr\rFrDrZSecKeychainDeleteshutilZrmtreerEr#rK)rIrrrrKMs        zWrappedSocket.closeFc Cs|s tdtj}d}d}ztj|jtj|}t||sBdStj|}|sTdStj |d}|sht tj |}|szt t j |}t j|} tj| |}Wd|rt j||rt j|X|S)Nz2SecureTransport only supports dumping binary certsr) ValueErrorrrXrYrAr(rZr ZSecTrustGetCertificateCountZSecTrustGetCertificateAtIndexAssertionErrorZSecCertificateCopyDatarZCFDataGetLengthZCFDataGetBytePtrr;r\) rIZ binary_formraZcertdataZ der_bytesrPZ cert_countZleafZ data_lengthr3rrr getpeercert`s6       zWrappedSocket.getpeercertcCs|jd7_dS)Nr)rB)rIrrr_reuseszWrappedSocket._reusecCs&|jdkr|jn|jd8_dS)Nr)rBrK)rIrrr_drops  zWrappedSocket._drop)N)F)__name__ __module__ __qualname____doc__rJ contextlibcontextmanagerrLrQrbrkrlrmrnr*rHr$r<rsrtrKrxryrzrrrrr@!s& >Z ( >r@cCs|jd7_t|||ddS)NrT)rK)rBr )rImodebufsizerrrmakefilesrrcOsd}t|||f||S)Nr)r)rIr bufferingargskwargsrrrrsc@seZdZdZddZeddZejddZeddZejd dZed d Z e jd d Z d dZ ddZ ddZ dddZ dddZdddZdS)rz I am a wrapper class for the SecureTransport library, to translate the interface of the standard library ``SSLContext`` object to calls into SecureTransport. cCs8t|\|_|_d|_d|_d|_d|_d|_d|_dS)NrF) _protocol_to_min_max _min_version _max_version_options_verify _trust_bundle _client_cert _client_key_client_key_passphrase)rIZprotocolrrrrJszSecureTransportContext.__init__cCsdS)z SecureTransport cannot have its hostname checking disabled. For more, see the comment on getpeercert() in this file. Tr)rIrrrcheck_hostnamesz%SecureTransportContext.check_hostnamecCsdS)z SecureTransport cannot have its hostname checking disabled. For more, see the comment on getpeercert() in this file. Nr)rIr]rrrrscCs|jS)N)r)rIrrroptionsszSecureTransportContext.optionscCs ||_dS)N)r)rIr]rrrrscCs|jr tjStjS)N)rr[ CERT_REQUIREDZ CERT_NONE)rIrrr verify_modesz"SecureTransportContext.verify_modecCs|tjkrdnd|_dS)NTF)r[rr)rIr]rrrrscCsdS)Nr)rIrrrset_default_verify_pathss z/SecureTransportContext.set_default_verify_pathscCs|jS)N)r)rIrrrload_default_certssz)SecureTransportContext.load_default_certscCs|tjjkrtddS)Nz5SecureTransport doesn't support custom cipher strings)rrZDEFAULT_CIPHERSrv)rIrOrrr set_cipherss z"SecureTransportContext.set_ciphersNcCs|dk rtd|p||_dS)Nz1SecureTransport does not support cert directories)rvr)rIZcafileZcapathZcadatarrrload_verify_locationssz,SecureTransportContext.load_verify_locationscCs||_||_||_dS)N)rrZ_client_cert_passphrase)rIZcertfileZkeyfileZpasswordrrrload_cert_chain sz&SecureTransportContext.load_cert_chainFTc CsL| s t|st|stt|}|j||j|j|j|j|j|j|j |S)N) rwr@rkrrrrrrr)rIZsockZ server_sideZdo_handshake_on_connectZsuppress_ragged_eofsrjr5rrr wrap_sockets    z"SecureTransportContext.wrap_socket)NNN)NN)FTTN)r{r|r}r~rJpropertyrsetterrrrrrrrrrrrrrs      r)r)rN)cr~Z __future__rrr(r&Zos.pathrSrur#r[Z threadingweakrefrZ_securetransport.bindingsrrrZ_securetransport.low_levelr r r r r ImportErrorZpackages.backports.makefiler__all__rrrrrWeakValueDictionaryr ZLockrerrZTLS_AES_256_GCM_SHA384ZTLS_CHACHA20_POLY1305_SHA256ZTLS_AES_128_GCM_SHA256Z'TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384Z%TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384Z'TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256Z%TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256Z#TLS_DHE_DSS_WITH_AES_256_GCM_SHA384Z#TLS_DHE_RSA_WITH_AES_256_GCM_SHA384Z#TLS_DHE_DSS_WITH_AES_128_GCM_SHA256Z#TLS_DHE_RSA_WITH_AES_128_GCM_SHA256Z'TLS_ECDHE_ECDSA_WITH_AES_256_CBC_SHA384Z%TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA384Z$TLS_ECDHE_ECDSA_WITH_AES_256_CBC_SHAZ"TLS_ECDHE_RSA_WITH_AES_256_CBC_SHAZ#TLS_DHE_RSA_WITH_AES_256_CBC_SHA256Z#TLS_DHE_DSS_WITH_AES_256_CBC_SHA256Z TLS_DHE_RSA_WITH_AES_256_CBC_SHAZ TLS_DHE_DSS_WITH_AES_256_CBC_SHAZ'TLS_ECDHE_ECDSA_WITH_AES_128_CBC_SHA256Z%TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA256Z$TLS_ECDHE_ECDSA_WITH_AES_128_CBC_SHAZ"TLS_ECDHE_RSA_WITH_AES_128_CBC_SHAZ#TLS_DHE_RSA_WITH_AES_128_CBC_SHA256Z#TLS_DHE_DSS_WITH_AES_128_CBC_SHA256Z TLS_DHE_RSA_WITH_AES_128_CBC_SHAZ TLS_DHE_DSS_WITH_AES_128_CBC_SHAZTLS_RSA_WITH_AES_256_GCM_SHA384ZTLS_RSA_WITH_AES_128_GCM_SHA256ZTLS_RSA_WITH_AES_256_CBC_SHA256ZTLS_RSA_WITH_AES_128_CBC_SHA256ZTLS_RSA_WITH_AES_256_CBC_SHAZTLS_RSA_WITH_AES_128_CBC_SHArNZPROTOCOL_SSLv23Z kTLSProtocol1ZkTLSProtocol12rhasattrZ kSSLProtocol2rZ kSSLProtocol3rrZkTLSProtocol11rrrrrr:r?Z SSLReadFuncrcZ SSLWriteFuncrdobjectr@rrrrrrs          76