U .e8@@s\dZddlmZddlZddlmZddlmZ ddl m Z zddl m Z Wn$ek rpGdd d eZ YnXdd lmZmZdd lmZzdd lmZWn$ek rdZd dlmZYnXddlZddlZd dlmZddlZd dlmZddgZ dZ!ej"ej#j$ej%ej#j&iZ'e(edr@e(ej#dr@ej#j)e'ej*<e(edrhe(ej#drhej#j+e'ej,<e(edre(ej#drej#j-e'ej.<ej/ej#j0ej1ej#j2ej3ej#j2ej#j4iZ5e6dde57DZ8dZ9ej!Z:ej;je?Z@ddZAddZBdd ZCd!d"ZDd#d$ZEGd%d&d&eFZGer6d.d(d)ZHneZHeHeG_HGd*d+d+eFZId,d-ZJdS)/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)UnsupportedExtensionc@s eZdZdS)rN)__name__ __module__ __qualname__r r I/usr/lib/python3.8/site-packages/pip/_vendor/urllib3/contrib/pyopenssl.pyr7sr)timeouterror)BytesIO) _fileobject)backport_makefile)six)utilinject_into_urllib3extract_from_urllib3TPROTOCOL_SSLv3 SSLv3_METHODPROTOCOL_TLSv1_1TLSv1_1_METHODPROTOCOL_TLSv1_2TLSv1_2_METHODccs|]\}}||fVqdSNr ).0kvr r r gsr i@cCs4ttt_ttj_tt_ttj_dt_dtj_dS)z7Monkey-patch urllib3 with PyOpenSSL-backed SSL-support.TN)_validate_dependencies_metPyOpenSSLContextr SSLContextssl_HAS_SNI IS_PYOPENSSLr r r r rsscCs.tt_ttj_tt_ttj_dt_dtj_dS)z4Undo monkey-patching by :func:`inject_into_urllib3`.FN)orig_util_SSLContextrr#r$orig_util_HAS_SNIr%r&r r r r rs cCsRddlm}t|dddkr$tdddlm}|}t|dddkrNtddS) 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+rr r r r!s  r!cCs@dd}d|kr|S||}|dkr(dStjdkr<|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). If the name cannot be idna-encoded then we return None signalling that the name given should be skipped. cSsvddlm}zJdD]8}||r|t|d}|d||WSq||WS|jjk rpYdSXdS)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. r)idna)z*..Nascii)Z pip._vendorr/ startswithlenencodeZcoreZ IDNAError)namer/prefixr r r idna_encodes   z'_dnsname_to_stdlib..idna_encode:N)rutf-8)sys version_infodecode)r5r7r r r _dnsname_to_stdlibs  r>c Cst|dr|}n tt|j}z|jtjj }WnZtj k rNgYStj t tj tfk r}ztd|gWYSd}~XYnXddtt|tjD}|dd|tjD|S)zU Given an PyOpenSSL certificate, provides all the subject alternative names. to_cryptographyzA problem was encountered with the certificate that prevented urllib3 from finding the SubjectAlternativeName field. This can affect certificate validation. The error was %sNcSsg|]}|dk rd|fqS)NZDNSr rr5r r r sz%get_subj_alt_name..css|]}dt|fVqdS)z IP AddressN)strr@r r r r sz$get_subj_alt_name..)hasattrr?ropenssl_backendr, extensionsr*rZSubjectAlternativeNamevalueZExtensionNotFoundZDuplicateExtensionrZUnsupportedGeneralNameType UnicodeErrorlogZwarningmapr>Zget_values_for_typeZDNSNameextendZ IPAddress)Z peer_certZcertZextenamesr r r get_subj_alt_names2      rMc@seZdZdZd!ddZddZddZd d Zd d Zd dZ ddZ ddZ ddZ ddZ d"ddZddZddZddZd S)# 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)selfrPrQrRr r r __init__ s zWrappedSocket.__init__cCs |jSr)rQfilenorUr r r rWszWrappedSocket.filenocCs*|jdkr|jd8_|jr&|dS)Nr)rSrTcloserXr r r _decref_socketioss zWrappedSocket._decref_socketiosc Osz|jj||}Wntjjk r^}z*|jrB|jdkrBWYdStt|W5d}~XYntjj k r|j tjj krYdSYn~tjj k rt |j|jstdn|j||YSYn:tjjk r}ztd|W5d}~XYnX|SdS)NzUnexpected EOFThe read operation timed outread error: %r)rPrecvOpenSSLSSL SysCallErrorrRargs SocketErrorrBZeroReturnError get_shutdownRECEIVED_SHUTDOWN WantReadErrorr wait_for_readrQ gettimeoutr ErrorsslSSLError)rUrekwargsdatarKr r r ras"   zWrappedSocket.recvc Os z|jj||WStjjk r\}z*|jr@|jdkr@WYdStt|W5d}~XYntjj k r|j tjj krYdSYnztjj k rt |j|jstdn|j||YSYn6tjjk r}ztd|W5d}~XYnXdS)Nr\rr_r`)rP recv_intorbrcrdrRrerfrBrgrhrirjrrkrQrlr rmrnro)rUrerprKr r r rr7s   zWrappedSocket.recv_intocCs |j|Sr)rQ settimeout)rUr r r r rsNszWrappedSocket.settimeoutc Cs|z|j|WStjjk rDt|j|jsttjs*d|jfdd|j|pJ|dS)Nr:csSrr )_passwordr r r^z2PyOpenSSLContext.load_cert_chain..)rZuse_certificate_chain_filerrZ binary_typer4Z set_passwd_cbZuse_privatekey_file)rUZcertfileZkeyfilerr rr load_cert_chains    z PyOpenSSLContext.load_cert_chainFTc Cstj|j|}t|tjr&|d}|dk r8||| z | Wqtjj k rt ||sxtdYq@Yqtjjk r}ztd|W5d}~XYqXqq@t||S)Nr:zselect timed outzbad handshake: %r)rbrcZ Connectionrrrrr4Zset_tlsext_host_nameZset_connect_stateZ do_handshakerjrrkrlr rmrnrorN)rUZsockZ server_sideZdo_handshake_on_connectrRZserver_hostnamecnxrKr r r wrap_sockets      zPyOpenSSLContext.wrap_socket)NNN)NN)FTTN)rrr rrVpropertyrsetterrrrrrrr r r r r"s&     r"cCs|dkSrvr )rrZerr_noZ err_depthZ return_coder r r rsr)r])KrZ __future__rZ OpenSSL.SSLrbZ cryptographyrZ$cryptography.hazmat.backends.opensslrrDZ)cryptography.hazmat.backends.openssl.x509rZcryptography.x509rr. ExceptionrQr r rfiorrZpackages.backports.makefilerZloggingrnZpackagesrr;r__all__r%Z PROTOCOL_TLSrcZ SSLv23_METHODZPROTOCOL_TLSv1Z TLSv1_METHODrrCrrrrrrZ CERT_NONEZ VERIFY_NONEZ CERT_OPTIONALZ VERIFY_PEERZ CERT_REQUIREDZVERIFY_FAIL_IF_NO_PEER_CERTrdictitemsrrwr(r$r#r'Z getLoggerrrHrrr!r>rMobjectrNrr"rr r r r sz+          +6 W