Re@@s1dZddlmZddlZddlmZddlmZ ddl m Z yddl m Z Wn(ek rGdd d eZ YnXdd lmZmZdd lmZydd lmZWn(ek rdZd dlmZYnXddlZddlZd dlmZddlZd dlmZddgZ dZ!ej"ej#j$ej%ej#j&iZ'e(edre(ej#drej#j)e'ej*e?Z@ddZAddZBdd ZCd!d"ZDd#d$ZEGd%d&d&eFZGerd.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@seZdZdS)rN)__name__ __module__ __qualname__r r /builddir/build/BUILDROOT/alt-python35-pip-20.2.4-5.el8.x86_64/opt/alt/python35/lib/python3.5/site-packages/pip/_vendor/urllib3/contrib/pyopenssl.pyr7s r)timeouterror)BytesIO) _fileobject)backport_makefile)six)utilinject_into_urllib3extract_from_urllib3TPROTOCOL_SSLv3 SSLv3_METHODPROTOCOL_TLSv1_1TLSv1_1_METHODPROTOCOL_TLSv1_2TLSv1_2_METHODccs!|]\}}||fVqdS)Nr ).0kvr r r gsri@cCsJttt_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 rss     cCsCtt_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      cCsuddlm}t|dddkr4tdddlm}|}t|dddkrqtddS) 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 cCsZdd}d|kr|S||}|dkr8dStjdkrV|jd}|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. c Ssddlm}ygxSddgD]E}|j|r |t|d}|jd|j|Sq W|j|SWn|jjk rdSYnXdS)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)idnaz*..Nascii) pip._vendorr. startswithlenencodecore IDNAError)namer.prefixr r r idna_encodesz'_dnsname_to_stdlib..idna_encode:Nrzutf-8)r;r)sys version_infodecode)r7r9r r r _dnsname_to_stdlibs    r?cCst|dr|j}ntt|j}y|jjtjj }Wnctj k rggSYnKtj t tj tfk r}ztjd|gSWYdd}~XnXddtt|jtjD}|jdd|jtjD|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 %sNcSs(g|]}|dk rd|fqS)NDNSr )rr7r r r s z%get_subj_alt_name..css!|]}dt|fVqdS)z IP AddressN)str)rr7r r r rsz$get_subj_alt_name..)hasattrr@ropenssl_backendr+ extensionsr)rZSubjectAlternativeNamevalueZExtensionNotFoundZDuplicateExtensionrZUnsupportedGeneralNameType UnicodeErrorlogwarningmapr?Zget_values_for_typeZDNSNameextendZ IPAddress)Z peer_certcertextenamesr r r get_subj_alt_names*  rQc@seZdZdZdddZddZddZd d Zd d Zd dZ ddZ ddZ ddZ ddZ dddZddZddZddZd S)! WrappedSocketzAPI-compatibility wrapper for Python OpenSSL's Connection-class. Note: _makefile_refs, _drop() and _reuse() are needed for the garbage collector of pypy. TcCs1||_||_||_d|_d|_dS)NrF) connectionsocketsuppress_ragged_eofs_makefile_refs_closed)selfrSrTrUr r r __init__ s     zWrappedSocket.__init__cCs |jjS)N)rTfileno)rXr r r rZszWrappedSocket.filenocCs5|jdkr|jd8_|jr1|jdS)Nr)rVrWclose)rXr r r _decref_socketioss zWrappedSocket._decref_socketioscOsGy|jj||}Wn#tjjk rt}z3|jrP|jdkrPdStt|WYdd}~Xntjj k r|jj tjj krdSYntjj k rt j|j|jjstdn|j||SYnBtjjk r>}ztjd|WYdd}~XnX|SdS)Nr[Unexpected EOFzThe read operation timed outzread error: %r)r`r^)rSrecvOpenSSLSSL SysCallErrorrUargs SocketErrorrCZeroReturnError get_shutdownRECEIVED_SHUTDOWN WantReadErrorr wait_for_readrT gettimeoutr ErrorsslSSLError)rXrekwargsdatarOr r r ras"$&zWrappedSocket.recvcOsAy|jj||SWn#tjjk rr}z3|jrN|jdkrNdStt|WYdd}~Xntjj k r|jj tjj krdSYntjj k rt j|j|jjstdn|j||SYn>tjjk r<}ztjd|WYdd}~XnXdS)Nr[Unexpected EOFrzThe read operation timed outzread error: %rr`)r`rr)rS recv_intorbrcrdrUrerfrCrgrhrirjrrkrTrlr rmrnro)rXrerprOr r r rs7s $zWrappedSocket.recv_intocCs|jj|S)N)rT settimeout)rXr r r r rtNszWrappedSocket.settimeoutcCsxy|jj|SWqtjjk r[tj|j|jjsTt wYqtjj k r}zt t |WYdd}~XqXqWdS)N) rSsendrbrcZWantWriteErrorrwait_for_writerTrlr rdrfrC)rXrqrOr r r _send_until_doneQs zWrappedSocket._send_until_donecCsJd}x=|t|krE|j|||t}||7}q WdS)Nr)r3rwSSL_WRITE_BLOCKSIZE)rXrq total_sentsentr r r sendall\s zWrappedSocket.sendallcCs|jjdS)N)rSshutdown)rXr r r r|dszWrappedSocket.shutdownc Cs^|jdkrKyd|_|jjSWqZtjjk rGdSYqZXn|jd8_dS)Nr[T)rVrWrSr\rbrcrm)rXr r r r\hs  zWrappedSocket.closeFcCsc|jj}|s|S|r8tjjtjj|Sdd|jjfffdt|iS)Nsubject commonNamesubjectAltName) rSZget_peer_certificaterbZcryptoZdump_certificateZ FILETYPE_ASN1Z get_subjectZCNrQ)rX binary_formrr r r getpeercertrszWrappedSocket.getpeercertcCs |jjS)N)rSZget_protocol_version_name)rXr r r versionszWrappedSocket.versioncCs|jd7_dS)Nr[)rV)rXr r r _reuseszWrappedSocket._reusecCs/|jdkr|jn|jd8_dS)Nr[)rVr\)rXr r r _drops zWrappedSocket._dropN)rrr __doc__rYrZr]rarsrtrwr{r|r\rrrrr r r r rRs           rRr[cCs%|jd7_t|||ddS)Nr[r\T)rVr)rXmodebufsizer r r makefilesrc@seZdZdZddZeddZejddZeddZejd dZd d Z d d Z dddddZ ddddZ ddddddZ dS)r!z 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||_tjj|j|_d|_d|_dS)NrF)_openssl_versionsprotocolrbrcContext_ctx_optionscheck_hostname)rXrr r r rYs  zPyOpenSSLContext.__init__cCs|jS)N)r)rXr r r optionsszPyOpenSSLContext.optionscCs||_|jj|dS)N)rrZ set_options)rXrGr r r rs cCst|jjS)N)_openssl_to_stdlib_verifyrZget_verify_mode)rXr r r verify_modeszPyOpenSSLContext.verify_modecCs|jjt|tdS)N)rZ set_verify_stdlib_to_openssl_verify_verify_callback)rXrGr r r rscCs|jjdS)N)rset_default_verify_paths)rXr r r rsz)PyOpenSSLContext.set_default_verify_pathscCs5t|tjr!|jd}|jj|dS)Nzutf-8) isinstancer text_typer4rZset_cipher_list)rXciphersr r r set_ciphersszPyOpenSSLContext.set_ciphersNcCs|dk r|jd}|dk r6|jd}y9|jj|||dk rn|jjt|Wn>tjjk r}ztjd|WYdd}~XnXdS)Nzutf-8z'unable to load trusted certificates: %r) r4rload_verify_locationsrrbrcrmrnro)rXcafilecapathcadatarOr r r rs   z&PyOpenSSLContext.load_verify_locationscss|jj|dk rYttjs=jd|jjfdd|jj|pk|dS)Nzutf-8csS)Nr )_)passwordr r sz2PyOpenSSLContext.load_cert_chain..)rZuse_certificate_chain_filerr binary_typer4Z set_passwd_cbZuse_privatekey_file)rXcertfilekeyfilerr )rr load_cert_chains  z PyOpenSSLContext.load_cert_chainFTcCstjj|j|}t|tjr9|jd}|dk rR|j||j xy|j Wn|tjj k rt j ||jstdw_Yn>tjjk r}ztjd|WYdd}~XnXPq_Wt||S)Nzutf-8zselect timed outzbad handshake: %r)rbrc Connectionrrrrr4Zset_tlsext_host_nameZset_connect_state do_handshakerjrrkrlr rmrnrorR)rXsock server_sidedo_handshake_on_connectrUserver_hostnamecnxrOr r r wrap_sockets"    &zPyOpenSSLContext.wrap_socket)rrr rrYpropertyrsetterrrrrrrr r r r r!s      r!cCs |dkS)Nrr )rrZerr_noZ err_depthZ return_coder r r rsrr`)Kr __future__rZ OpenSSL.SSLrbZ cryptographyrZ$cryptography.hazmat.backends.opensslrrEZ)cryptography.hazmat.backends.openssl.x509rZcryptography.x509rr- ExceptionrTr r rfiorrZpackages.backports.makefilerloggingrnpackagesrr<r__all__r$ PROTOCOL_TLSrcZ SSLv23_METHODPROTOCOL_TLSv1Z TLSv1_METHODrrDrrrrrr CERT_NONEZ VERIFY_NONE CERT_OPTIONALZ VERIFY_PEER CERT_REQUIREDZVERIFY_FAIL_IF_NO_PEER_CERTrdictitemsrrxr'r#r"r& getLoggerrrIrrr r?rQobjectrRrr!rr r r r +sh       !!!    + 6 Z