a †oeŸBã@sˆdZddlmZddlZddlZddlmZddlm Z zddl m Z Wn"e yjGdd„deƒZ Yn0dd lmZdd lmZdd lmZzdd lmZWn"e yÂdZd dlmZYn0ddlZddlZddlZddlZd dlmZd dlmZd dl m!Z!ej"de#d d�ddgZ$dZ%ej&ej'j(e!ej'j(ej)ej'j*iZ+e,edƒ�rle,ej'dƒ�rlej'j-e+ej.<e,edƒ�r”e,ej'dƒ�r”ej'j/e+ej0<e,edƒ�r¼e,ej'dƒ�r¼ej'j1e+ej2<ej3ej'j4ej5ej'j6ej7ej'j6ej'j8iZ9e:dd„e9 ;¡DƒƒZej?j@ZAe BeC¡ZDd d„ZEd!d„ZFd"d#„ZGd$d%„ZHd&d'„ZIGd(d)„d)eJƒZKe�rbd1d+d,„ZLneZLeLeK_LGd-d.„d.eJƒZMd/d0„ZNdS)2a† TLS with SNI_-support for Python 2. Follow these instructions if you would like to verify TLS 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: .. code-block:: bash $ python -m 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: .. code-block:: python 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`_). .. _sni: https://en.wikipedia.org/wiki/Server_Name_Indication .. _crime attack: https://en.wikipedia.org/wiki/CRIME_(security_exploit) .. _pyopenssl: https://www.pyopenssl.org .. _cryptography: https://cryptography.io .. _idna: https://github.com/kjd/idna é)Úabsolute_importN)Úx509)Úbackend)ÚUnsupportedExtensionc@s eZdZdS)rN)Ú__name__Ú __module__Ú __qualname__©r r ú:C:\Program Files\Certbot\pkgs\urllib3\contrib\pyopenssl.pyr;sr)ÚBytesIO)Úerror)Útimeout)Ú _fileobjecté)Úbackport_makefile)Úutil)Úsix)ÚPROTOCOL_TLS_CLIENTz°'urllib3.contrib.pyopenssl' module is deprecated and will be removed in a future release of urllib3 2.x. Read more in this issue: https://github.com/urllib3/urllib3/issues/2680)ÚcategoryÚ stacklevelÚinject_into_urllib3Úextract_from_urllib3TÚPROTOCOL_SSLv3Ú SSLv3_METHODÚPROTOCOL_TLSv1_1ÚTLSv1_1_METHODÚPROTOCOL_TLSv1_2ÚTLSv1_2_METHODccs|]\}}||fVqdS©Nr )Ú.0ÚkÚvr r r Ú vór"i@cCs4tƒtt_ttj_tt_ttj_dt_dtj_dS)z7Monkey-patch urllib3 with PyOpenSSL-backed SSL-support.TN)Ú_validate_dependencies_metÚPyOpenSSLContextrÚ SSLContextÚssl_ÚHAS_SNIÚ IS_PYOPENSSLr r r r r‚scCs.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 r�s cCsRddlm}t|ddƒdur$tdƒ‚ddlm}|ƒ}t|ddƒdurNtdƒ‚dS) z{ Verifies that PyOpenSSL's package-level dependencies have been met. Throws `ImportError` if they are not met. r)Ú ExtensionsÚget_extension_for_classNzX'cryptography' module missing required functionality. Try upgrading to v1.3.4 or newer.)ÚX509Z_x509zS'pyOpenSSL' module missing required functionality. Try upgrading to v0.14 or newer.)Zcryptography.x509.extensionsr,ÚgetattrÚ ImportErrorÚOpenSSL.cryptor.)r,r.rr r r r$šs ÿ ÿr$cCs@dd„}d|vr|S||ƒ}|dur(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. cSspddl}zJdD]8}| |¡r|t|ƒd…}| d¡| |¡WSq| |¡WS|jjyjYdS0dS)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Ú startswithÚlenÚencodeZcoreZ IDNAError)Únamer4Úprefixr r r Ú idna_encodeÂs  z'_dnsname_to_stdlib..idna_encodeú:N)érúutf-8)ÚsysÚ version_infoÚdecode)r8r:r r r Ú_dnsname_to_stdlib´s  rAc CsÚt|dƒr| ¡}ntj tjj|¡}t |t¡}z|j   tj ¡j }WnVtj y^gYStjttjtfyœ}zt d|¡gWYd}~Sd}~00dd„tt| tj¡ƒDƒ}| dd„| tj¡Dƒ¡|S)zU Given an PyOpenSSL certificate, provides all the subject alternative names. Úto_cryptographyz­A problem was encountered with the certificate that prevented urllib3 from finding the SubjectAlternativeName field. This can affect certificate validation. The error was %sNcSsg|]}|durd|f‘qS)NZDNSr ©rr8r r r Ú sþz%get_subj_alt_name..css|]}dt|ƒfVqdS)z IP AddressN)ÚstrrCr r r r" sz$get_subj_alt_name..)ÚhasattrrBÚOpenSSLÚcryptoÚdump_certificateÚ FILETYPE_ASN1rZload_der_x509_certificateÚopenssl_backendÚ extensionsr-ZSubjectAlternativeNameÚvalueZExtensionNotFoundZDuplicateExtensionrZUnsupportedGeneralNameTypeÚ UnicodeErrorÚlogZwarningÚmaprAZget_values_for_typeZDNSNameÚextendZ IPAddress)Z peer_certZcertZderÚextÚeÚnamesr r r Úget_subj_alt_nameßs4   ü ü þ  ÿrUc@s„eZdZdZd!dd„Zdd„Zdd„Zd d „Zd d „Zd d„Z dd„Z dd„Z dd„Z dd„Z d"dd„Zdd„Zdd„Zdd„Zd S)#Ú WrappedSocketz§API-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|_dS©NrF)Ú connectionÚsocketÚsuppress_ragged_eofsÚ_makefile_refsÚ_closed)ÚselfrXrYrZr r r Ú__init__s zWrappedSocket.__init__cCs |j ¡Sr)rYÚfileno©r]r r r r_"szWrappedSocket.filenocCs*|jdkr|jd8_|jr&| ¡dS)Nré)r[r\Úcloser`r r r Ú_decref_socketios&s zWrappedSocket._decref_socketiosc Osz|jj|i|¤Ž}W�nütjjyj}z6|jrJ|jdkrJWYd}~dStt|ƒƒ‚WYd}~n¸d}~0tjj y˜|j  ¡tjj kr’YdS‚Yn‚tjj yÞt  |j|j ¡¡sÆtdƒ‚n|j|i|¤ŽYSYn<tjj�y}zt d|¡‚WYd}~nd}~00|SdS)N©éÿÿÿÿzUnexpected EOFr#úThe read operation timed outúread error: %r)rXÚrecvrGÚSSLÚ SysCallErrorrZÚargsÚ SocketErrorrEÚZeroReturnErrorÚ get_shutdownÚRECEIVED_SHUTDOWNÚ WantReadErrorrÚ wait_for_readrYÚ gettimeoutr ÚErrorÚsslÚSSLError)r]rkÚkwargsÚdatarSr r r rh,s"  $zWrappedSocket.recvc Osz|jj|i|¤ŽWStjjyf}z6|jrF|jdkrFWYd}~dStt|ƒƒ‚WYd}~n´d}~0tjj y”|j  ¡tjj krŽYdS‚Yn~tjj yÚt  |j|j ¡¡sÂtdƒ‚n|j|i|¤ŽYSYn8tjj�y}zt d|¡‚WYd}~n d}~00dS)Nrdrrfrg)rXÚ recv_intorGrirjrZrkrlrErmrnrorprrqrYrrr rsrtru)r]rkrvrSr r r rxEs   zWrappedSocket.recv_intocCs |j |¡Sr)rYÚ settimeout)r]r r r r ry\szWrappedSocket.settimeoutc Cs|z|j |¡WStjjyBt |j|j ¡¡s:t ƒ‚YqYqtjj yt}zt t |ƒƒ‚WYd}~qd}~00qdSr) rXÚsendrGriZWantWriteErrorrZwait_for_writerYrrr rjrlrE)r]rwrSr r r Ú_send_until_done_szWrappedSocket._send_until_donecCs4d}|t|ƒkr0| |||t…¡}||7}qdS©Nr)r6r{ÚSSL_WRITE_BLOCKSIZE)r]rwZ total_sentÚsentr r r Úsendalljs  ÿzWrappedSocket.sendallcCs|j ¡dSr)rXÚshutdownr`r r r r€rszWrappedSocket.shutdowncCsJ|jdkr8zd|_|j ¡WStjjy4YdS0n|jd8_dS)NraT)r[r\rXrbrGrirsr`r r r rbvs   zWrappedSocket.closeFcCsD|j ¡}|s|S|r(tj tjj|¡Sd| ¡jffft|ƒdœS)NZ commonName)ZsubjectZsubjectAltName) rXZget_peer_certificaterGrHrIrJZ get_subjectZCNrU)r]Z binary_formrr r r Ú getpeercert€s þzWrappedSocket.getpeercertcCs |j ¡Sr)rXZget_protocol_version_namer`r r r ÚversionŽszWrappedSocket.versioncCs|jd7_dS©Nra)r[r`r r r Ú_reuse‘szWrappedSocket._reusecCs&|jdkr| ¡n|jd8_dSrƒ)r[rbr`r r r Ú_drop”s  zWrappedSocket._dropN)T)F)rrrÚ__doc__r^r_rcrhrxryr{rr€rbr�r‚r„r…r r r r rVs   rVrecCs|jd7_t|||dd�S)NraT)rb)r[r)r]ÚmodeÚbufsizer r r Úmakefile�sr‰c@s‚eZdZdZdd„Zedd„ƒZejdd„ƒZedd„ƒZejd d„ƒZd d „Z d d „Z ddd„Z ddd„Z dd„Z ddd„ZdS)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||_tj |j¡|_d|_d|_dSrW)Ú_openssl_versionsÚprotocolrGriZContextÚ_ctxÚ_optionsZcheck_hostname)r]r‹r r r r^®s zPyOpenSSLContext.__init__cCs|jSr)r�r`r r r Úoptions´szPyOpenSSLContext.optionscCs||_|j |¡dSr)r�rŒZ set_options©r]rMr r r rޏscCst|j ¡Sr)Ú_openssl_to_stdlib_verifyrŒZget_verify_moder`r r r Ú verify_mode½szPyOpenSSLContext.verify_modecCs|j t|t¡dSr)rŒZ set_verifyÚ_stdlib_to_openssl_verifyÚ_verify_callbackr�r r r r‘ÁscCs|j ¡dSr)rŒÚset_default_verify_pathsr`r r r r”Åsz)PyOpenSSLContext.set_default_verify_pathscCs&t|tjƒr| d¡}|j |¡dS)Nr=)Ú isinstancerÚ text_typer7rŒZset_cipher_list)r]Zciphersr r r Ú set_ciphersÈs  zPyOpenSSLContext.set_ciphersNc CsŠ|dur| d¡}|dur$| d¡}z*|j ||¡|durL|j t|ƒ¡Wn6tjjy„}zt d|¡‚WYd}~n d}~00dS)Nr=z'unable to load trusted certificates: %r) r7rŒÚload_verify_locationsr rGrirsrtru)r]ZcafileZcapathZcadatarSr r r r˜Ís  z&PyOpenSSLContext.load_verify_locationscsR|j |¡ˆdur>tˆtjƒs*ˆ d¡‰|j ‡fdd„¡|j |pJ|¡dS)Nr=csˆSrr )Ú_©Úpasswordr r ÚÞr#z2PyOpenSSLContext.load_cert_chain..)rŒZuse_certificate_chain_filer•rZ binary_typer7Z set_passwd_cbZuse_privatekey_file)r]ZcertfileZkeyfiler›r ršr Úload_cert_chainÙs    z PyOpenSSLContext.load_cert_chaincCsdd„|Dƒ}|j |¡S)NcSsg|]}t |¡‘qSr )rZ ensure_binary)rÚpr r r rDâr#z7PyOpenSSLContext.set_alpn_protocols..)rŒZset_alpn_protos)r]Z protocolsr r r Úset_alpn_protocolsász#PyOpenSSLContext.set_alpn_protocolsFTc CsÂtj |j|¡}t|tjƒr&| d¡}|dur8| |¡|  ¡z |  ¡Wq¸tjj y~t   || ¡¡svtdƒ‚Yq@Yq¸tjjy²}zt d|¡‚WYd}~q¸d}~00q¸q@t||ƒS)Nr=zselect timed outzbad handshake: %r)rGriZ ConnectionrŒr•rr–r7Zset_tlsext_host_nameZset_connect_stateZ do_handshakerprrqrrr rsrtrurV)r]ZsockZ server_sideZdo_handshake_on_connectrZZserver_hostnameÚcnxrSr r r Ú wrap_socketås     $zPyOpenSSLContext.wrap_socket)NNN)NN)FTTN)rrrr†r^ÚpropertyrŽÚsetterr‘r”r—r˜r�rŸr¡r r r r r%§s(     úr%cCs|dkSr|r )r rZerr_noZ err_depthZ return_coder r r r“sr“)re)Or†Z __future__rr1rGZ OpenSSL.SSLZ cryptographyrZ$cryptography.hazmat.backends.opensslrrKZcryptography.x509rr0Ú ExceptionÚior rYr rlr rZpackages.backports.makefilerZloggingrtr>ÚwarningsÚrZpackagesrZ util.ssl_rÚwarnÚDeprecationWarningÚ__all__r(Z PROTOCOL_TLSriZ SSLv23_METHODZPROTOCOL_TLSv1Z TLSv1_METHODrŠrFrrrrrrZ CERT_NONEZ VERIFY_NONEZ CERT_OPTIONALZ VERIFY_PEERZ CERT_REQUIREDZVERIFY_FAIL_IF_NO_PEER_CERTr’ÚdictÚitemsr�r}r+r'r&r*Z getLoggerrrOrrr$rArUÚobjectrVr‰r%r“r r r r Ús‚/           û  ý   ÿý   +5 ^