ó V!¶\c@@s#dZddlmZddlZddlmZddlmZ ddl m Z yddl m Z Wn'ek r�defd „ƒYZ nXdd lmZmZdd lmZydd lmZWn'ek ródZd dlmZnXddlZddlZd dlmZddlZd dlm Z ddgZ!e"Z#iej$j%ej&6ej$j'ej(6Z)e*edƒr§e*ej$dƒr§ej$j+e)ej,e j?j@ZAejBeCƒZDd„ZEd„ZFd„ZGd„ZHd„ZIdeJfd„ƒYZKerñd d!„ZLneZLeLeK_Ld"eJfd#„ƒYZMd$„ZNdS(%sb 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) i(tabsolute_importN(tx509(tbackend(t _Certificate(tUnsupportedExtensionRcB@seZRS((t__name__t __module__(((s</tmp/pip-install-usGedi/urllib3/urllib3/contrib/pyopenssl.pyR6s(ttimeoutterror(tBytesIO(t _fileobjecti(tbackport_makefile(tsix(tutiltinject_into_urllib3textract_from_urllib3tPROTOCOL_TLSv1_1tTLSv1_1_METHODtPROTOCOL_TLSv1_2tTLSv1_2_METHODcc@s!|]\}}||fVqdS(N((t.0tktv((s</tmp/pip-install-usGedi/urllib3/urllib3/contrib/pyopenssl.pys fsi@cC@sAtƒttj_tt_ttj_tt_ttj_dS(s7Monkey-patch urllib3 with PyOpenSSL-backed SSL-support.N(t_validate_dependencies_mettPyOpenSSLContextR tssl_t SSLContexttHAS_SNItTruet IS_PYOPENSSL(((s</tmp/pip-install-usGedi/urllib3/urllib3/contrib/pyopenssl.pyRss     cC@s:ttj_tt_ttj_tt_ttj_dS(s4Undo monkey-patching by :func:`inject_into_urllib3`.N(torig_util_SSLContextR RRtorig_util_HAS_SNIRtFalseR(((s</tmp/pip-install-usGedi/urllib3/urllib3/contrib/pyopenssl.pyRs     cC@s{ddlm}t|ddƒdkr7tdƒ‚nddlm}|ƒ}t|ddƒdkrwtdƒ‚ndS( s{ Verifies that PyOpenSSL's package-level dependencies have been met. Throws `ImportError` if they are not met. i(t Extensionstget_extension_for_classsX'cryptography' module missing required functionality. Try upgrading to v1.3.4 or newer.(tX509t_x509sS'pyOpenSSL' module missing required functionality. Try upgrading to v0.14 or newer.N(tcryptography.x509.extensionsR!tgetattrtNonet ImportErrortOpenSSL.cryptoR#(R!R#R((s</tmp/pip-install-usGedi/urllib3/urllib3/contrib/pyopenssl.pyR‰s cC@sJd„}||ƒ}|dkr%dStjdkrF|jdƒ}n|S(s% 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). If the name cannot be idna-encoded then we return None signalling that the name given should be skipped. cS@sŒddl}yaxMddgD]?}|j|ƒr|t|ƒ}|jdƒ|j|ƒSqW|j|ƒSWn|jjk r‡dSXdS(sÒ 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. iNu*.u.tascii(tidnat startswithtlentencodetcoret IDNAErrorR'(tnameR+tprefix((s</tmp/pip-install-usGedi/urllib3/urllib3/contrib/pyopenssl.pyt idna_encodeªs iisutf-8N(ii(R'tsyst version_infotdecode(R1R3((s</tmp/pip-install-usGedi/urllib3/urllib3/contrib/pyopenssl.pyt_dnsname_to_stdlib�s   cC@st|dƒr|jƒ}ntt|jƒ}y|jjtjƒj }WnJtj k rcgStj t tj tfk r˜}tjd|ƒgSXgtt|jtjƒƒD]}|dk rµd|f^qµ}|jd„|jtjƒDƒƒ|S(sU Given an PyOpenSSL certificate, provides all the subject alternative names. tto_cryptographys­A problem was encountered with the certificate that prevented urllib3 from finding the SubjectAlternativeName field. This can affect certificate validation. The error was %stDNScs@s!|]}dt|ƒfVqdS(s IP AddressN(tstr(RR1((s</tmp/pip-install-usGedi/urllib3/urllib3/contrib/pyopenssl.pys ðsN(thasattrR8Rtopenssl_backendR$t extensionsR"RtSubjectAlternativeNametvaluetExtensionNotFoundtDuplicateExtensionRtUnsupportedGeneralNameTypet UnicodeErrortlogtwarningtmapR7tget_values_for_typetDNSNameR'textendt IPAddress(t peer_certtcerttextteR1tnames((s</tmp/pip-install-usGedi/urllib3/urllib3/contrib/pyopenssl.pytget_subj_alt_nameÃs(   "t WrappedSocketcB@s‰eZdZed„Zd„Zd„Zd„Zd„Zd„Z d„Z d„Z d „Z d „Z ed „Zd „Zd „ZRS(s§API-compatibility wrapper for Python OpenSSL's Connection-class. Note: _makefile_refs, _drop() and _reuse() are needed for the garbage collector of pypy. cC@s1||_||_||_d|_t|_dS(Ni(t connectiontsockettsuppress_ragged_eofst_makefile_refsR t_closed(tselfRRRSRT((s</tmp/pip-install-usGedi/urllib3/urllib3/contrib/pyopenssl.pyt__init__þs     cC@s |jjƒS(N(RStfileno(RW((s</tmp/pip-install-usGedi/urllib3/urllib3/contrib/pyopenssl.pyRYscC@s;|jdkr!|jd8_n|jr7|jƒndS(Nii(RURVtclose(RW((s</tmp/pip-install-usGedi/urllib3/urllib3/contrib/pyopenssl.pyt_decref_socketios s cO@søy|jj||Ž}WnÔtjjk rb}|jrM|jdkrMdStt|ƒƒ‚n’tjj k rœ}|jj ƒtjj kr–dS‚nXtjj k rït j|j|jjƒƒsÜtdƒ‚qô|j||ŽSnX|SdS(NiÿÿÿÿsUnexpected EOFtsThe read operation timed out(iÿÿÿÿsUnexpected EOF(RRtrecvtOpenSSLtSSLt SysCallErrorRTtargst SocketErrorR:tZeroReturnErrort get_shutdowntRECEIVED_SHUTDOWNt WantReadErrorR t wait_for_readRSt gettimeoutR(RWRatkwargstdataRN((s</tmp/pip-install-usGedi/urllib3/urllib3/contrib/pyopenssl.pyR]scO@sòy|jj||ŽSWnÔtjjk r`}|jrK|jdkrKdStt|ƒƒ‚nŽtjj k rš}|jj ƒtjj kr”dS‚nTtjj k rít j|j|jjƒƒsÚtdƒ‚qî|j||ŽSnXdS(NiÿÿÿÿsUnexpected EOFisThe read operation timed out(iÿÿÿÿsUnexpected EOF(RRt recv_intoR^R_R`RTRaRbR:RcRdReRfR RgRSRhR(RWRaRiRN((s</tmp/pip-install-usGedi/urllib3/urllib3/contrib/pyopenssl.pyRk$scC@s|jj|ƒS(N(RSt settimeout(RWR((s</tmp/pip-install-usGedi/urllib3/urllib3/contrib/pyopenssl.pyRl7scC@s–x�tr‘y|jj|ƒSWqtjjk rctj|j|jj ƒƒst ƒ‚qqqtjj k r�}t t |ƒƒ‚qXqWdS(N(RRRtsendR^R_tWantWriteErrorR twait_for_writeRSRhRR`RbR:(RWRjRN((s</tmp/pip-install-usGedi/urllib3/urllib3/contrib/pyopenssl.pyt_send_until_done:s  cC@sGd}x:|t|ƒkrB|j|||t!ƒ}||7}q WdS(Ni(R-RptSSL_WRITE_BLOCKSIZE(RWRjt total_senttsent((s</tmp/pip-install-usGedi/urllib3/urllib3/contrib/pyopenssl.pytsendallEscC@s|jjƒdS(N(RRtshutdown(RW((s</tmp/pip-install-usGedi/urllib3/urllib3/contrib/pyopenssl.pyRuKscC@sZ|jdkrGyt|_|jjƒSWqVtjjk rCdSXn|jd8_dS(Ni(RURRVRRRZR^R_tError(RW((s</tmp/pip-install-usGedi/urllib3/urllib3/contrib/pyopenssl.pyRZOs cC@se|jjƒ}|s|S|r8tjjtjj|ƒSid|jƒjfffd6t|ƒd6S(Nt commonNametsubjecttsubjectAltName( RRtget_peer_certificateR^tcryptotdump_certificatet FILETYPE_ASN1t get_subjecttCNRP(RWt binary_formR((s</tmp/pip-install-usGedi/urllib3/urllib3/contrib/pyopenssl.pyt getpeercertYs  cC@s|jd7_dS(Ni(RU(RW((s</tmp/pip-install-usGedi/urllib3/urllib3/contrib/pyopenssl.pyt_reusekscC@s/|jdkr|jƒn|jd8_dS(Ni(RURZ(RW((s</tmp/pip-install-usGedi/urllib3/urllib3/contrib/pyopenssl.pyt_dropns (RRt__doc__RRXRYR[R]RkRlRpRtRuRZR R�R‚Rƒ(((s</tmp/pip-install-usGedi/urllib3/urllib3/contrib/pyopenssl.pyRQ÷s          iÿÿÿÿcC@s%|jd7_t|||dtƒS(NiRZ(RUR R(RWtmodetbufsize((s</tmp/pip-install-usGedi/urllib3/urllib3/contrib/pyopenssl.pytmakefilevsRcB@s¡eZdZd„Zed„ƒZejd„ƒZed„ƒZejd„ƒZd„Zd„Z d d d d„Z d d d „Z e eed d „ZRS( s 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. cC@s;t||_tjj|jƒ|_d|_t|_dS(Ni( t_openssl_versionstprotocolR^R_tContextt_ctxt_optionsR tcheck_hostname(RWR‰((s</tmp/pip-install-usGedi/urllib3/urllib3/contrib/pyopenssl.pyRX…s  cC@s|jS(N(RŒ(RW((s</tmp/pip-install-usGedi/urllib3/urllib3/contrib/pyopenssl.pytoptions‹scC@s||_|jj|ƒdS(N(RŒR‹t set_options(RWR?((s</tmp/pip-install-usGedi/urllib3/urllib3/contrib/pyopenssl.pyRŽ�s cC@st|jjƒS(N(t_openssl_to_stdlib_verifyR‹tget_verify_mode(RW((s</tmp/pip-install-usGedi/urllib3/urllib3/contrib/pyopenssl.pyt verify_mode”scC@s|jjt|tƒdS(N(R‹t set_verifyt_stdlib_to_openssl_verifyt_verify_callback(RWR?((s</tmp/pip-install-usGedi/urllib3/urllib3/contrib/pyopenssl.pyR’˜s cC@s|jjƒdS(N(R‹tset_default_verify_paths(RW((s</tmp/pip-install-usGedi/urllib3/urllib3/contrib/pyopenssl.pyR–ŸscC@s8t|tjƒr$|jdƒ}n|jj|ƒdS(Nsutf-8(t isinstanceR t text_typeR.R‹tset_cipher_list(RWtciphers((s</tmp/pip-install-usGedi/urllib3/urllib3/contrib/pyopenssl.pyt set_ciphers¢scC@sx|dk r|jdƒ}n|dk r<|jdƒ}n|jj||ƒ|dk rt|jjt|ƒƒndS(Nsutf-8(R'R.R‹tload_verify_locationsR (RWtcafiletcapathtcadata((s</tmp/pip-install-usGedi/urllib3/urllib3/contrib/pyopenssl.pyRœ§s   c@sR|jj|ƒˆdk r8|jj‡fd†ƒn|jj|pJ|ƒdS(Nc@sˆS(N((t max_lengtht prompt_twicetuserdata(tpassword(s</tmp/pip-install-usGedi/urllib3/urllib3/contrib/pyopenssl.pyt³s(R‹tuse_certificate_chain_fileR't set_passwd_cbtuse_privatekey_file(RWtcertfiletkeyfileR£((R£s</tmp/pip-install-usGedi/urllib3/urllib3/contrib/pyopenssl.pytload_cert_chain°s cC@sútjj|j|ƒ}t|tjƒr<|jdƒ}n|dk rX|j |ƒn|j ƒxˆt rìy|j ƒWnltjj k r¼tj||jƒƒsetdƒ‚qeqen,tjjk rç}tjd|ƒ‚nXPqeWt||ƒS(Nsutf-8sselect timed outsbad handshake: %r(R^R_t ConnectionR‹R—R R˜R.R'tset_tlsext_host_nametset_connect_stateRt do_handshakeRfR RgRhRRvtssltSSLErrorRQ(RWtsockt server_sidetdo_handshake_on_connectRTtserver_hostnametcnxRN((s</tmp/pip-install-usGedi/urllib3/urllib3/contrib/pyopenssl.pyt wrap_socket¶s"   N(RRR„RXtpropertyRŽtsetterR’R–R›R'RœRªR RR¶(((s</tmp/pip-install-usGedi/urllib3/urllib3/contrib/pyopenssl.pyRs    cC@s |dkS(Ni((RµRterr_not err_deptht return_code((s</tmp/pip-install-usGedi/urllib3/urllib3/contrib/pyopenssl.pyR•Ñs(OR„t __future__Rt OpenSSL.SSLR^t cryptographyRt$cryptography.hazmat.backends.opensslRR<t)cryptography.hazmat.backends.openssl.x509Rtcryptography.x509RR(t ExceptionRSRRRbtioR R R'tpackages.backports.makefileR tloggingR¯tpackagesR R4R\R t__all__RRR_t SSLv23_METHODtPROTOCOL_SSLv23t TLSv1_METHODtPROTOCOL_TLSv1RˆR;RRRRtupdatet SSLv3_METHODtPROTOCOL_SSLv3tAttributeErrort VERIFY_NONEt CERT_NONEt VERIFY_PEERt CERT_OPTIONALtVERIFY_FAIL_IF_NO_PEER_CERTt CERT_REQUIREDR”tdicttitemsR�RqRRRRt getLoggerRRDRRRR7RPtobjectRQR‡RR•(((s</tmp/pip-install-usGedi/urllib3/urllib3/contrib/pyopenssl.pyt+sp       !!!     & 4~ R