a Y;@s&dZddlmZddlZddlmZddlmZ ddl m Z ddl m Z mZddlmZzdd l mZWn"eydZd d lmZYn0ddlZddlZddlZddlZd d lmZd dgZdZejejj ej!ejj"iZ#e$edre$ejdrejj%e#ej&<e$edr,e$ejdr,ejj'e#ej(<ze#)ej*ejj+iWne,yXYn0ej-ejj.ej/ejj0ej1ejj0ejj2iZ3e4dde35DZ6dZ7ejZ8ej9j:Z;edd Z?ddZ@ddZAddZBddZCGdd d eDZEerd(d"d#ZFneZFeFeE_FGd$d%d%eDZGd&d'ZHdS))ab SSL with SNI_-support for Python 2. Follow these instructions if you would like to verify SSL certificates in Python 2. Note, the default libraries do *not* do certificate checking; you need to do additional work to validate certificates yourself. This needs the following packages installed: * pyOpenSSL (tested with 16.0.0) * cryptography (minimum 1.3.4, from pyopenssl) * idna (minimum 2.0, from cryptography) However, pyopenssl depends on cryptography, which depends on idna, so while we use all three directly here we end up having relatively few packages required. You can install them with the following command: pip install pyopenssl cryptography idna To activate certificate checking, call :func:`~urllib3.contrib.pyopenssl.inject_into_urllib3` from your Python code before you begin making HTTP requests. This can be done in a ``sitecustomize`` module, or at any other time before your application begins using ``urllib3``, like this:: try: import urllib3.contrib.pyopenssl urllib3.contrib.pyopenssl.inject_into_urllib3() except ImportError: pass Now you can use :mod:`urllib3` as you normally would, and it will support SNI when the required modules are installed. Activating this module also has the positive side effect of disabling SSL/TLS compression in Python 2 (see `CRIME attack`_). If you want to configure the default list of supported cipher suites, you can set the ``urllib3.contrib.pyopenssl.DEFAULT_SSL_CIPHER_LIST`` variable. .. _sni: https://en.wikipedia.org/wiki/Server_Name_Indication .. _crime attack: https://en.wikipedia.org/wiki/CRIME_(security_exploit) )absolute_importN)x509)backend) _Certificate)timeouterror)BytesIO) _fileobject)backport_makefile)utilinject_into_urllib3extract_from_urllib3TPROTOCOL_TLSv1_1TLSv1_1_METHODPROTOCOL_TLSv1_2TLSv1_2_METHODccs|]\}}||fVqdSN).0kvrrR/Users/ymaher/Downloads/lambdas_org/requests/packages/urllib3/contrib/pyopenssl.py _sri@cCs.tttj_tt_ttj_dt_dtj_dS)z7Monkey-patch urllib3 with PyOpenSSL-backed SSL-support.TN)_validate_dependencies_metPyOpenSSLContextr ssl_ SSLContextHAS_SNI IS_PYOPENSSLrrrrr ms cCs(ttj_tt_ttj_dt_dtj_dS)z4Undo monkey-patching by :func:`inject_into_urllib3`.FN)orig_util_SSLContextr rrorig_util_HAS_SNIrrrrrrrys cCsRddlm}t|dddur$tdddlm}|}t|dddurNtddS) z{ Verifies that PyOpenSSL's package-level dependencies have been met. Throws `ImportError` if they are not met. r) Extensionsget_extension_for_classNzX'cryptography' module missing required functionality. Try upgrading to v1.3.4 or newer.)X509_x509zS'pyOpenSSL' module missing required functionality. Try upgrading to v0.14 or newer.)Zcryptography.x509.extensionsr"getattr ImportErrorZOpenSSL.cryptor$)r"r$rrrrrs  rcCs(dd}||}tjdkr$|d}|S)a Converts a dNSName SubjectAlternativeName field to the form used by the standard library on the given Python version. Cryptography produces a dNSName as a unicode string that was idna-decoded from ASCII bytes. We need to idna-encode that string to get it back, and then on Python 3 we also need to convert to unicode via UTF-8 (the stdlib uses PyUnicode_FromStringAndSize on it, which decodes via UTF-8). cSsNddl}dD]6}||r |t|d}|d||Sq ||S)z Borrowed wholesale from the Python Cryptography Project. It turns out that we can't just safely call `idna.encode`: it can explode for wildcard names. This avoids that problem. rN)z*..ascii)idna startswithlenencode)namer*prefixrrr idna_encodes  z'_dnsname_to_stdlib..idna_encode)rutf-8)sys version_infodecode)r.r0rrr_dnsname_to_stdlibs   r6c Cstt|j}z|jtjj}WnXtjy8gYStj tj tj t fyx}zt d|gWYd}~Sd}~00dd|tjD}|dd|tjD|S)zU Given an PyOpenSSL certificate, provides all the subject alternative names. zA problem was encountered with the certificate that prevented urllib3 from finding the SubjectAlternativeName field. This can affect certificate validation. The error was %sNcSsg|]}dt|fqS)DNS)r6rr.rrr sz%get_subj_alt_name..css|]}dt|fVqdS)z IP AddressN)strr8rrrrsz$get_subj_alt_name..)ropenssl_backendr% extensionsr#rZSubjectAlternativeNamevalueZExtensionNotFoundZDuplicateExtensionZUnsupportedExtensionZUnsupportedGeneralNameType UnicodeErrorlogwarningZget_values_for_typeZDNSNameextendZ IPAddress)Z peer_certcertextenamesrrrget_subj_alt_names,       rFc@s|eZdZdZdddZddZddZd d Zd d Zd dZ ddZ ddZ ddZ ddZ d ddZddZddZdS)! WrappedSocketzAPI-compatibility wrapper for Python OpenSSL's Connection-class. Note: _makefile_refs, _drop() and _reuse() are needed for the garbage collector of pypy. TcCs"||_||_||_d|_d|_dSNrF) connectionsocketsuppress_ragged_eofs_makefile_refs_closed)selfrIrJrKrrr__init__s zWrappedSocket.__init__cCs |jSr)rJfilenorNrrrrPszWrappedSocket.filenocCs*|jdkr|jd8_|jr&|dS)Nr)rLrMcloserQrrr_decref_socketioss zWrappedSocket._decref_socketiosc Osz|jj|i|}Wntjjyh}z6|jrH|jdkrHWYd}~dStt|WYd}~nd}~0tjj y}z.|j tjj krWYd}~dSWYd}~nXd}~0tjj yt |j|j}|stdn|j|i|YSYn0|SdS)NzUnexpected EOFThe read operation timed out)rIrecvOpenSSLSSL SysCallErrorrKargs SocketErrorr:ZeroReturnError get_shutdownRECEIVED_SHUTDOWN WantReadErrorr wait_for_readrJ gettimeoutr)rNr]kwargsdatarDrdrrrrYs   zWrappedSocket.recvc Osz|jj|i|WStjjyf}z6|jrF|jdkrFWYd}~dStt|WYd}~nd}~0tjj y}z.|j tjj krWYd}~dSWYd}~nVd}~0tjj yt |j|j}|stdn|j|i|YSYn0dS)NrUrrX)rI recv_intorZr[r\rKr]r^r:r_r`rarbr rcrJrdr)rNr]rerDrgrrrrhs  zWrappedSocket.recv_intocCs |j|Sr)rJ settimeout)rNrrrrri'szWrappedSocket.settimeoutc Csz|j|WStjjyFt|j|j}|s>t YqYqtjj yx}zt t |WYd}~qd}~00qdSr) rIsendrZr[ZWantWriteErrorr wait_for_writerJrdrr\r^r:)rNrfwrrDrrr_send_until_done*szWrappedSocket._send_until_donecCs4d}|t|kr0||||t}||7}qdSNr)r,rmSSL_WRITE_BLOCKSIZE)rNrf total_sentsentrrrsendall6s zWrappedSocket.sendallcCs|jdSr)rIshutdownrQrrrrs<szWrappedSocket.shutdowncCsJ|jdkr8zd|_|jWStjjy4YdS0n|jd8_dS)NrRT)rLrMrIrSrZr[ErrorrQrrrrS@s   zWrappedSocket.closeFcCsD|j}|s|S|r(tjtjj|Sd|jffft|dS)N commonName)subjectsubjectAltName) rIZget_peer_certificaterZZcryptoZdump_certificateZ FILETYPE_ASN1Z get_subjectZCNrF)rN binary_formrrrr getpeercertJs zWrappedSocket.getpeercertcCs|jd7_dSNrR)rLrQrrr_reuse\szWrappedSocket._reusecCs&|jdkr|n|jd8_dSrz)rLrSrQrrr_drop_s  zWrappedSocket._dropN)T)F)__name__ __module__ __qualname____doc__rOrPrTrYrhrirmrrrsrSryr{r|rrrrrGs   rGrVcCs|jd7_t|||ddS)NrRT)rS)rLr )rNmodebufsizerrrmakefilegsrc@szeZdZdZddZeddZejddZeddZejd dZd d Z d d Z dddZ dddZ dddZ dS)rz I am a wrapper class for the PyOpenSSL ``Context`` object. I am responsible for translating the interface of the standard library ``SSLContext`` object to calls into PyOpenSSL. cCs*t||_tj|j|_d|_d|_dSrH)_openssl_versionsprotocolrZr[Context_ctx_optionscheck_hostname)rNrrrrrOvs zPyOpenSSLContext.__init__cCs|jSr)rrQrrroptions|szPyOpenSSLContext.optionscCs||_|j|dSr)rrZ set_optionsrNr=rrrrscCst|jSr)_openssl_to_stdlib_verifyrZget_verify_moderQrrr verify_modeszPyOpenSSLContext.verify_modecCs|jt|tdSr)rZ set_verify_stdlib_to_openssl_verify_verify_callbackrrrrrscCs|jdSr)rset_default_verify_pathsrQrrrrsz)PyOpenSSLContext.set_default_verify_pathscCs&t|tjr|d}|j|dSNr2) isinstancesix text_typer-rZset_cipher_list)rNciphersrrr set_cipherss  zPyOpenSSLContext.set_ciphersNcCsN|dur|d}|dur$|d}|j|||durJ|jt|dSr)r-rload_verify_locationsr)rNcafilecapathcadatarrrrs  z&PyOpenSSLContext.load_verify_locationscs<|j|dur(|jfdd|j|p4|dS)NcsSrr) max_lengthZ prompt_twiceZuserdatapasswordrrrWz2PyOpenSSLContext.load_cert_chain..)rZuse_certificate_fileZ set_passwd_cbZuse_privatekey_file)rNcertfilekeyfilerrrrload_cert_chains z PyOpenSSLContext.load_cert_chainFTc Cstj|j|}t|tjr&|d}|dur8||| z | Wqtjj yt ||}|sztdYq@Yqtjjy}ztd|WYd}~qd}~00qq@t||S)Nr2zselect timed outzbad handshake: %r)rZr[ Connectionrrrrr-Zset_tlsext_host_nameZset_connect_state do_handshakerbr rcrdrrtsslSSLErrorrG) rNsock server_sidedo_handshake_on_connectrKserver_hostnamecnxrgrDrrr wrap_sockets"    $zPyOpenSSLContext.wrap_socket)NNN)NN)FTTN)r}r~rrrOpropertyrsetterrrrrrrrrrrrps$     rcCs|dkSrnr)rrZerr_noZ err_depthZ return_coderrrrsr)rV)Ir __future__rZ OpenSSL.SSLrZZ cryptographyrZ$cryptography.hazmat.backends.opensslrr;Z)cryptography.hazmat.backends.openssl.x509rrJrrr^iorr r'Zpackages.backports.makefiler loggingrrr3r __all__rPROTOCOL_SSLv23r[Z SSLv23_METHODPROTOCOL_TLSv1Z TLSv1_METHODrhasattrrrrrupdateZPROTOCOL_SSLv3Z SSLv3_METHODAttributeError CERT_NONEZ VERIFY_NONE CERT_OPTIONALZ VERIFY_PEER CERT_REQUIREDZVERIFY_FAIL_IF_NO_PEER_CERTrdictitemsrror!rrr getLoggerr}r?r rrr6rFobjectrGrrrrrrrsn+              0 S