V!\c@@s#dZddlmZddlZddlmZddlmZ ddl m Z yddl m Z Wn'ek rdefd YZ nXdd lmZmZdd lmZydd lmZWn'ek rdZd dlmZnXddlZddlZd dlmZddlZd dlm Z ddgZ!e"Z#iej$j%ej&6ej$j'ej(6Z)e*edre*ej$drej$j+e)ej,e j?j@ZAejBeCZDdZEdZFdZGdZHdZIdeJfdYZKerd 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@sAtttj_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|dddkr7tdnddlm}|}t|dddkrwtdndS( 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.pyRs 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@sddl}yaxMddgD]?}|j|r|t|}|jd|j|SqW|j|SWn|jjk rdSXdS(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_encodes iisutf-8N(ii(R'tsyst version_infotdecode(R1R3((s</tmp/pip-install-usGedi/urllib3/urllib3/contrib/pyopenssl.pyt_dnsname_to_stdlibs   cC@st|dr|j}ntt|j}y|jjtjj }WnJtj k rcgStj t tj tfk r}tjd|gSXgtt|jtjD]}|dk rd|f^q}|jd|jtjD|S(sU Given an PyOpenSSL certificate, provides all the subject alternative names. tto_cryptographysA 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_names(   "t WrappedSocketcB@seZdZedZdZdZdZdZdZ dZ dZ d Z d Z ed Zd Zd ZRS(sAPI-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 |jjS(N(RStfileno(RW((s</tmp/pip-install-usGedi/urllib3/urllib3/contrib/pyopenssl.pyRYscC@s;|jdkr!|jd8_n|jr7|jndS(Nii(RURVtclose(RW((s</tmp/pip-install-usGedi/urllib3/urllib3/contrib/pyopenssl.pyt_decref_socketios s cO@sy|jj||}Wntjjk rb}|jrM|jdkrMdStt|ntjj k r}|jj tjj krdSnXtjj k rt j|j|jjstdq|j||SnX|SdS(NisUnexpected EOFtsThe read operation timed out(isUnexpected 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@sy|jj||SWntjjk r`}|jrK|jdkrKdStt|ntjj k r}|jj tjj krdSnTtjj k rt j|j|jjstdq|j||SnXdS(NisUnexpected EOFisThe read operation timed out(isUnexpected 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@sxtry|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|jjdS(N(RRtshutdown(RW((s</tmp/pip-install-usGedi/urllib3/urllib3/contrib/pyopenssl.pyRuKscC@sZ|jdkrGyt|_|jjSWqVtjjk 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|jjfffd6t|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|jn|jd8_dS(Ni(RURZ(RW((s</tmp/pip-install-usGedi/urllib3/urllib3/contrib/pyopenssl.pyt_dropns (RRt__doc__RRXRYR[R]RkRlRpRtRuRZR RRR(((s</tmp/pip-install-usGedi/urllib3/urllib3/contrib/pyopenssl.pyRQs          icC@s%|jd7_t|||dtS(NiRZ(RUR R(RWtmodetbufsize((s</tmp/pip-install-usGedi/urllib3/urllib3/contrib/pyopenssl.pytmakefilevsRcB@seZdZdZedZejdZedZejdZdZdZ d d d dZ 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.pyRXs  cC@s|jS(N(R(RW((s</tmp/pip-install-usGedi/urllib3/urllib3/contrib/pyopenssl.pytoptionsscC@s||_|jj|dS(N(RRt set_options(RWR?((s</tmp/pip-install-usGedi/urllib3/urllib3/contrib/pyopenssl.pyRs cC@st|jjS(N(t_openssl_to_stdlib_verifyRtget_verify_mode(RW((s</tmp/pip-install-usGedi/urllib3/urllib3/contrib/pyopenssl.pyt verify_modescC@s|jjt|tdS(N(Rt set_verifyt_stdlib_to_openssl_verifyt_verify_callback(RWR?((s</tmp/pip-install-usGedi/urllib3/urllib3/contrib/pyopenssl.pyRs cC@s|jjdS(N(Rtset_default_verify_paths(RW((s</tmp/pip-install-usGedi/urllib3/urllib3/contrib/pyopenssl.pyRscC@s8t|tjr$|jd}n|jj|dS(Nsutf-8(t isinstanceR t text_typeR.Rtset_cipher_list(RWtciphers((s</tmp/pip-install-usGedi/urllib3/urllib3/contrib/pyopenssl.pyt set_ciphersscC@sx|dk r|jd}n|dk r<|jd}n|jj|||dk rt|jjt|ndS(Nsutf-8(R'R.Rtload_verify_locationsR (RWtcafiletcapathtcadata((s</tmp/pip-install-usGedi/urllib3/urllib3/contrib/pyopenssl.pyRs   c@sR|jj|dk r8|jjfdn|jj|pJ|dS(Nc@sS(N((t max_lengtht prompt_twicetuserdata(tpassword(s</tmp/pip-install-usGedi/urllib3/urllib3/contrib/pyopenssl.pyts(Rtuse_certificate_chain_fileR't set_passwd_cbtuse_privatekey_file(RWtcertfiletkeyfileR((Rs</tmp/pip-install-usGedi/urllib3/urllib3/contrib/pyopenssl.pytload_cert_chains cC@stjj|j|}t|tjr<|jd}n|dk rX|j |n|j xt ry|j Wnltjj k rtj||jsetdqeqen,tjjk r}tjd|nXPqeWt||S(Nsutf-8sselect timed outsbad handshake: %r(R^R_t ConnectionRRR RR.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_sockets"   N(RRRRXtpropertyRtsetterRRRR'RRR RR(((s</tmp/pip-install-usGedi/urllib3/urllib3/contrib/pyopenssl.pyRs    cC@s |dkS(Ni((RRterr_not err_deptht return_code((s</tmp/pip-install-usGedi/urllib3/urllib3/contrib/pyopenssl.pyRs(ORt __future__Rt OpenSSL.SSLR^t cryptographyRt$cryptography.hazmat.backends.opensslRR<t)cryptography.hazmat.backends.openssl.x509Rtcryptography.x509RR(t ExceptionRSRRRbtioR R R'tpackages.backports.makefileR tloggingRtpackagesR R4R\R t__all__RRR_t SSLv23_METHODtPROTOCOL_SSLv23t TLSv1_METHODtPROTOCOL_TLSv1RR;RRRRtupdatet SSLv3_METHODtPROTOCOL_SSLv3tAttributeErrort VERIFY_NONEt CERT_NONEt VERIFY_PEERt CERT_OPTIONALtVERIFY_FAIL_IF_NO_PEER_CERTt CERT_REQUIREDRtdicttitemsRRqRRRRt getLoggerRRDRRRR7RPtobjectRQRRR(((s</tmp/pip-install-usGedi/urllib3/urllib3/contrib/pyopenssl.pyt+sp       !!!     & 4~ R