B
    ÐH/\bk  ã            	   @   s°  d Z ddlmZ ddlZejZddlZddlmZ ddl	m
Z
 ddl	mZ ddl	mZ ddl	mZ dd	lmZ dd
lmZ ddddddddgZeeeƒ eddg ddZye W n ek
rÈ   dZY nX ee Zdekräe d¡ ejZG dd„ deƒZejdddfdd„Ze ddejdddddf	dd„Z!eZ"e!Z#G dd„ deƒZ$esVe%e$dƒsje&dd „ d!d „ ƒe$_e'd"ƒZ(e'd#ƒZ)e'd$ƒZ*ddde+e dd%d%df	d&d„Z,e dfd'd„Z-dS )(z®SSL wrapper for socket objects on Python 2.7.9 and above.

For the documentation, refer to :mod:`ssl` module manual.

This module implements cooperative SSL socket wrappers.
é    )Úabsolute_importN)Úsocket)Útimeout_default)Úcreate_connection)Úerror)Útimeout)ÚPYPY)Úcopy_globalsÚ
SSLContextÚ	SSLSocketÚwrap_socketÚget_server_certificateÚcreate_default_contextÚ_create_unverified_contextÚ_create_default_https_contextÚ_create_stdlib_contextr   r   © )Znames_to_ignoreZdunder_names_to_keep)ÚrecvÚrecvfromÚ	recv_intoÚrecvfrom_intoÚsendÚsendtoÚ
namedtuplec               @   s   e Zd Zddd„ZdS )r
   FTNc             C   s   t |||||| dS )N)ÚsockÚserver_sideÚdo_handshake_on_connectÚsuppress_ragged_eofsÚserver_hostnameÚ_context)r   )Úselfr   r   r   r   r   r   r   ú0lib/python3.7/site-packages/gevent/_sslgte279.pyr   <   s
    zSSLContext.wrap_socket)FTTN)Ú__name__Ú
__module__Ú__qualname__r   r   r   r   r!   r
   ;   s      c             C   sî   t | tƒst| ƒ‚ttƒ}| jtO  _| jtO  _| jtt	ddƒO  _| t
jkrdt|_d|_nV| t
jkrº| jtt	ddƒO  _| jtt	ddƒO  _| jtt	ddƒO  _| t¡ |sÆ|sÆ|rÖ| |||¡ n|jtkrê| | ¡ |S )zíCreate a SSLContext object with default settings.

    NOTE: The protocol and settings may change anytime without prior
          deprecation. The values represent a fair balance between maximum
          compatibility and security.
    ZOP_NO_COMPRESSIONr   TZOP_CIPHER_SERVER_PREFERENCEZOP_SINGLE_DH_USEZOP_SINGLE_ECDH_USE)Ú
isinstanceÚ_ASN1ObjectÚ	TypeErrorr
   ÚPROTOCOL_SSLv23ÚoptionsÚOP_NO_SSLv2ÚOP_NO_SSLv3ÚgetattrÚ_sslÚPurposeÚSERVER_AUTHÚCERT_REQUIREDÚverify_modeÚcheck_hostnameZCLIENT_AUTHÚset_ciphersZ_RESTRICTED_SERVER_CIPHERSÚload_verify_locationsÚ	CERT_NONEÚload_default_certs)ÚpurposeÚcafileÚcapathÚcadataÚcontextr   r   r!   r   G   s&    





Fc	       
      C   s¢   t |tƒst|ƒ‚t| ƒ}	|	 jtO  _|	 jtO  _|dk	rD||	_||	_|rZ|sZt	dƒ‚|sb|rn|	 
||¡ |sz|sz|rŠ|	 |||¡ n|	jtkrž|	 |¡ |	S )a/  Create a SSLContext object for Python stdlib modules

    All Python stdlib modules shall use this function to create SSLContext
    objects in order to keep common settings in one place. The configuration
    is less restrict than create_default_context()'s to increase backward
    compatibility.
    Nzcertfile must be specified)r%   r&   r'   r
   r)   r*   r+   r1   r2   Ú
ValueErrorÚload_cert_chainr4   r5   r6   )
ZprotocolÚ	cert_reqsr2   r7   ÚcertfileÚkeyfiler8   r9   r:   r;   r   r   r!   r   w   s"    


c               @   sª  e Zd ZdZddddeeddeedddddddfdd„Ze	dd	„ ƒZ
e
jd
d	„ ƒZ
dd„ ZdZdd„Zdd„ Zd[dd„Zdd„ Zd\dd„Zdd„ Zeedƒr¢dd„ Zdd„ Zdd „ Zd!d"„ Zdefd#d$„Zd]d%d&„Zd'd(„ Zd^d)d*„Zd_d+d,„Zd`d-d.„Zdad/d0„Z dbd1d2„Z!d3d4„ Z"d5d6„ Z#d7d8„ Z$d9d:„ Z%d;d<„ Z&e'rJd=d>„ Z(d?d@„ Z)dAdB„ Z*dCdD„ Z+dEdF„ Z,dGdH„ Z-dIdJ„ Z.dKdL„ Z/dMdN„ Z0dOdP„ Z1dcdSdT„Z2dddVdW„Z3dXdY„ Z4dS )er   z„
    gevent `ssl.SSLSocket <https://docs.python.org/2/library/ssl.html#ssl-sockets>`_
    for Pythons >= 2.7.9 but less than 3.
    NFTr   c          
   C   s\  |r|| _ n¤|r|stdƒ‚|r,|s,tdƒ‚|r8|s8|}t|ƒ| _ || j _|rZ| j  |¡ |rl| j  ||¡ |r|| j  |¡ |rŒ| j  |¡ || _|| _	|| _
|| _|| _|| _| tt¡tkrÈtdƒ‚trätj| |d | ¡  ntj| |jd x2tD ]*}yt| |ƒ W qú tk
r"   Y qúX qúW |r<|r<tdƒ‚| j jrT|sTtdƒ‚|| _|| _|| _|| _|   | !¡ ¡ y|  "¡  W n8 t#k
r¾ } z|j$t$j%krª‚ d}W d d }~X Y nX d}d	| _&d| _'d | _(|| _)|rXyD| j j*| j||| d
| _(|r$|  !¡ }|dkrtdƒ‚|  +¡  W n0 t#k
rV } z|  ,¡  |‚W d d }~X Y nX d S )Nz5certfile must be specified for server-side operationszcertfile must be specifiedz!only stream sockets are supported)Ú_sockz4server_hostname can only be specified in client modez'check_hostname requires server_hostnameFTr   )Ússl_sockg        zHdo_handshake_on_connect should not be specified for non-blocking sockets)-r   r<   r
   r1   r4   r=   Zset_npn_protocolsr3   r@   r?   r>   Ússl_versionÚca_certsÚciphersZ
getsockoptZ
SOL_SOCKETZSO_TYPEÚSOCK_STREAMÚNotImplementedErrorr   r   Ú__init__Ú_droprA   Ú_delegate_methodsÚdelattrÚAttributeErrorr2   r   r   r   r   Ú
settimeoutÚ
gettimeoutÚgetpeernameÚsocket_errorÚerrnoZENOTCONNÚ_makefile_refsZ_closedÚ_sslobjÚ
_connectedÚ_wrap_socketÚdo_handshakeÚclose)r    r   r@   r?   r   r>   rC   rD   r   ZfamilyÚtypeÚprotoÚfilenor   Znpn_protocolsrE   r   r   ÚattrÚeZ	connectedr   Úxr   r   r!   rH   ­   s†    





zSSLSocket.__init__c             C   s   | j S )N)r   )r    r   r   r!   r;     s    zSSLSocket.contextc             C   s   || _ || j_d S )N)r   rS   r;   )r    Zctxr   r   r!   r;     s    c             C   s   t d| jj ƒ‚d S )NzCan't dup() %s instances)rG   Ú	__class__r"   )r    r   r   r!   Údup  s    zSSLSocket.dupc             C   s   d S )Nr   )r    Úmsgr   r   r!   Ú_checkClosed  s    zSSLSocket._checkClosedc             C   s   | j s|  ¡  d S )N)rT   rO   )r    r   r   r!   Ú_check_connected"  s    zSSLSocket._check_connectedé   c          
   C   s&  |   ¡  x| jstdƒ‚|dkr2|dkr.dS dS |dk rJ|dkrJtdƒ‚y&|dk	rb| j ||¡S | j |pnd¡S  tk
r    | jdkrŒ‚ | j| jtd Y q t	k
rÎ   | jdkrº‚ | j| j
td Y q tk
r } z0|jd tkr
| jr
|dk	rdS dS ‚ W dd}~X Y qX qW dS )	zORead up to LEN bytes and return them.
        Return zero-length string on EOF.z'Read on closed or unwrapped SSL socket.r   Nó    zNegative read lengthi   g        )Útimeout_exc)ra   rS   r<   ÚreadÚSSLWantReadErrorr   Ú_waitÚ_read_eventÚ_SSLErrorReadTimeoutÚSSLWantWriteErrorÚ_write_eventÚSSLErrorÚargsÚSSL_ERROR_EOFr   )r    ÚlenÚbufferÚexr   r   r!   rf   *  s4    


zSSLSocket.readc          
   C   sª   |   ¡  xœ| jstdƒ‚y| j |¡S  tk
r  } z^|jd tkrb| jdkrP‚ | j| j	t
d n.|jd tkrŽ| jdkr|‚ | j| jt
d n‚ W dd}~X Y q
X q
W dS )zhWrite DATA to the underlying SSL channel.  Returns
        number of bytes of DATA actually transmitted.z(Write on closed or unwrapped SSL socket.r   g        )re   N)ra   rS   r<   Úwriterm   rn   ÚSSL_ERROR_WANT_READr   rh   ri   Ú_SSLErrorWriteTimeoutÚSSL_ERROR_WANT_WRITErl   )r    Údatarr   r   r   r!   rs   K  s     

zSSLSocket.writec             C   s   |   ¡  |  ¡  | j |¡S )záReturns a formatted version of the data in the
        certificate provided by the other end of the SSL channel.
        Return None if no certificate was provided, {} if a
        certificate was provided, but not validated.)ra   rb   rS   Zpeer_certificate)r    Zbinary_formr   r   r!   Úgetpeercertb  s    zSSLSocket.getpeercertc             C   s"   |   ¡  | jrtjsd S | j ¡ S )N)ra   rS   r-   ZHAS_NPNÚselected_npn_protocol)r    r   r   r!   ry   l  s    zSSLSocket.selected_npn_protocolÚHAS_ALPNc             C   s"   |   ¡  | jrtjsd S | j ¡ S )N)ra   rS   r-   rz   Úselected_alpn_protocol)r    r   r   r!   r{   t  s    z SSLSocket.selected_alpn_protocolc             C   s   |   ¡  | jsd S | j ¡ S )N)ra   rS   Úcipher)r    r   r   r!   r|   z  s    zSSLSocket.cipherc             C   s   |   ¡  | jsd S | j ¡ S )N)ra   rS   Úcompression)r    r   r   r!   r}   €  s    zSSLSocket.compressionc             C   s   |dkrt d|| jf ƒ‚d S )Nr   z/non-zero flags not allowed in calls to %s on %s)r<   r^   )r    ÚmethÚflagsr   r   r!   Z__check_flags†  s    zSSLSocket.__check_flagsc             C   sª   |   ¡  |  d|¡ |tkr"| j}| js8t | |||¡S xly| j |¡S  tk
rt   | jdkrddS |  	| j
¡ Y q: tk
r    | jdkrdS |  	| j¡ Y q:X q:W d S )Nr   g        r   )ra   Ú_SSLSocket__check_flagsr   r   rS   r   r   rs   rg   rh   ri   rk   rl   )r    rw   r   r   r   r   r!   r   Œ  s"    

zSSLSocket.sendc             C   sH   |   ¡  | jrtd| j ƒ‚n&|d kr4t | ||¡S t | |||¡S d S )Nz%sendto not allowed on instances of %s)ra   rS   r<   r^   r   r   )r    rw   Zflags_or_addrÚaddrr   r   r!   r   ¢  s    zSSLSocket.sendtoc             O   s   t d| j ƒ‚d S )Nz&sendmsg not allowed on instances of %s)rG   r^   )r    rn   Úkwargsr   r   r!   Úsendmsg¬  s    zSSLSocket.sendmsgc          
   C   sh   |   ¡  |  d|¡ yt | |¡ W n> tk
rb } z | jdkrHtdƒ‚t|jŽ ‚W d d }~X Y nX d S )NÚsendallg        z&The operation did not complete (write))	ra   r€   r   r„   Ú_socket_timeoutr   rk   rm   rn   )r    rw   r   rr   r   r   r!   r„   ²  s    
zSSLSocket.sendallc             C   sH   |   ¡  | jr:|dkr$td| j ƒ‚|dkr0dS |  |¡S t | ||¡S )Nr   z3non-zero flags not allowed in calls to recv() on %srd   )ra   rS   r<   r^   rf   r   r   )r    Úbuflenr   r   r   r!   r   À  s    

zSSLSocket.recvc             C   sf   |   ¡  |d k	r"|d kr"t|ƒ}n|d kr.d}| jrV|dkrJtd| j ƒ‚|  ||¡S t | |||¡S )Ni   r   z8non-zero flags not allowed in calls to recv_into() on %s)ra   rp   rS   r<   r^   rf   r   r   )r    rq   Únbytesr   r   r   r!   r   Ì  s    

zSSLSocket.recv_intoc             C   s*   |   ¡  | jrtd| j ƒ‚t | ||¡S )Nz'recvfrom not allowed on instances of %s)ra   rS   r<   r^   r   r   )r    r†   r   r   r   r!   r   Ü  s
    
zSSLSocket.recvfromc             C   s2   |   ¡  | jrtd| j ƒ‚nt | |||¡S d S )Nz,recvfrom_into not allowed on instances of %s)ra   rS   r<   r^   r   r   )r    rq   r‡   r   r   r   r!   r   ã  s
    zSSLSocket.recvfrom_intoc             O   s   t d| j ƒ‚d S )Nz&recvmsg not allowed on instances of %s)rG   r^   )r    rn   r‚   r   r   r!   Úrecvmsgë  s    zSSLSocket.recvmsgc             O   s   t d| j ƒ‚d S )Nz+recvmsg_into not allowed on instances of %s)rG   r^   )r    rn   r‚   r   r   r!   Úrecvmsg_intoï  s    zSSLSocket.recvmsg_intoc             C   s   |   ¡  | jr| j ¡ S dS )Nr   )ra   rS   Úpending)r    r   r   r!   rŠ   ó  s    
zSSLSocket.pendingc             C   s   |   ¡  d | _t | |¡ d S )N)ra   rS   r   Úshutdown)r    Zhowr   r   r!   r‹   ù  s    zSSLSocket.shutdownc             C   s.   | j dk rd | _t | ¡ n|  j d8  _ d S )Né   )rR   rS   r   rW   )r    r   r   r!   rW   þ  s    
zSSLSocket.closec             C   s   |  j d7  _ d S )NrŒ   )rR   )r    r   r   r!   Ú_reuse  s    zSSLSocket._reusec             C   s&   | j dk r|  ¡  n|  j d8  _ d S )NrŒ   )rR   rW   )r    r   r   r!   rI   
  s    

zSSLSocket._dropc          
   C   sº   x´y
| j  ¡ S  tk
r° } z†|jd tkr6| jr6dS |jd tkrj| jdkrP‚ t 	¡  | j
| jtd n6|jd tkrž| jdkr„‚ t 	¡  | j
| jtd n‚ W d d }~X Y qX qW d S )Nr   Ú g        )re   )rS   r‹   rm   rn   ro   r   rt   r   ÚsysZ	exc_clearrh   ri   rj   rv   rl   ru   )r    rr   r   r   r!   Ú_sslobj_shutdown  s"    


zSSLSocket._sslobj_shutdownc             C   s.   | j stdt| ƒ ƒ‚|  ¡ }d | _ t|dS )NzNo SSL wrapper around )rA   )rS   r<   Ústrr   r   )r    Úsr   r   r!   Úunwrap$  s
    zSSLSocket.unwrapc             C   s   d | _ t | ¡ d S )N)rS   r   Ú_real_close)r    r   r   r!   r”   /  s    zSSLSocket._real_closec             C   s¨   |   ¡  xty| j ¡  P W q
 tk
rJ   | jdkr6‚ | j| jtd Y q
 tk
rx   | jdkrd‚ | j| j	td Y q
X q
W | j
jr¤| js”tdƒ‚t|  ¡ | jƒ dS )zPerform a TLS/SSL handshake.g        )re   z-check_hostname needs server_hostname argumentN)rb   rS   rV   rg   r   rh   ri   Ú_SSLErrorHandshakeTimeoutrk   rl   r   r2   r   r<   Zmatch_hostnamerx   )r    r   r   r!   rV   3  s"    


zSSLSocket.do_handshakec             C   s–   | j rtdƒ‚| jrtdƒ‚| jj| jd| j| d| _y>|rJt 	| |¡}nd }t 
| |¡ |srd| _| jrr|  ¡  |S  tk
r   d | _‚ Y nX d S )Nz!can't connect in server-side modez/attempt to connect already-connected SSLSocket!F)rB   T)r   r<   rT   r   rU   rA   r   rS   r   Ú
connect_exÚconnectr   rV   rP   )r    r   r–   Zrcr   r   r!   Ú_real_connectI  s$    zSSLSocket._real_connectc             C   s   |   |d¡ dS )zQConnects to remote ADDR, and then wraps the connection in
        an SSL channel.FN)r˜   )r    r   r   r   r!   r—   `  s    zSSLSocket.connectc             C   s   |   |d¡S )zQConnects to remote ADDR, and then wraps the connection in
        an SSL channel.T)r˜   )r    r   r   r   r!   r–   e  s    zSSLSocket.connect_exc             C   s6   t  | ¡\}}| ¡  | jj|| j| jdd}||fS )z¿Accepts a new connection from a remote client, and returns
        a tuple containing that new connection wrapped with a server-side
        SSL channel, and the address of the remote client.T)r   r   r   )r   ÚacceptZ_drop_eventsr   r   r   r   )r    Znewsockr   r   r   r!   r™   j  s    zSSLSocket.acceptÚréÿÿÿÿc             C   s"   t s|  jd7  _t| ||ddS )zMake and return a file-like object that
        works with the SSL connection.  Just use the code
        from the socket module.rŒ   T)rW   )r   rR   Z_fileobject)r    ÚmodeÚbufsizer   r   r!   Úmakefilew  s    zSSLSocket.makefileú
tls-uniquec             C   s>   |t krtdƒ‚|dkr&td |¡ƒ‚| jdkr4dS | j ¡ S )zäGet channel binding data for current connection.  Raise ValueError
        if the requested `cb_type` is not supported.  Return bytes of the data
        or None if the data is not available (e.g. before the handshake).
        z Unsupported channel binding typez
tls-uniquez({0} channel binding type not implementedN)ZCHANNEL_BINDING_TYPESr<   rG   ÚformatrS   Ztls_unique_cb)r    Zcb_typer   r   r!   Úget_channel_binding‚  s    
zSSLSocket.get_channel_bindingc             C   s   | j dkrdS | j  ¡ S )z—
        Return a string identifying the protocol version used by the
        current SSL channel, or None if there is no established channel.
        N)rS   Úversion)r    r   r   r!   r¢   ‘  s    
zSSLSocket.version)N)rc   N)F)N)r   )rc   r   )Nr   )rc   r   )Nr   )rš   r›   )rŸ   )5r"   r#   r$   Ú__doc__r5   r(   ZAF_INETrF   rH   Úpropertyr;   Úsetterr_   ra   rb   rf   rs   rx   ry   Úhasattrr-   r{   r|   r}   r€   r   r   r   rƒ   r„   r   r   r   r   rˆ   r‰   rŠ   r‹   rW   r   r   rI   r   r“   r”   rV   r˜   r—   r–   r™   rž   r¡   r¢   r   r   r   r!   r   §   sd   ]

!











r   c             C   s   |   ¡ S )N)rN   )r    r   r   r!   Ú<lambda>¢  s    r§   c             C   s
   |   |¡S )N)rM   )r    Úvaluer   r   r!   r§   £  s    zThe read operation timed outzThe write operation timed outz!The handshake operation timed outTc
       
      C   s   t | |||||||||	d
S )N)
r   r@   r?   r   r>   rC   rD   r   r   rE   )r   )
r   r@   r?   r   r>   rC   rD   r   r   rE   r   r   r!   r   «  s    c       	   
   C   sl   | \}}|dk	rt }nt}t|||d}tt| ƒƒ*}t| |¡ƒ}| d¡}W dQ R X W dQ R X t|ƒS )z÷Retrieve the certificate from the server at the specified address,
    and return it as a PEM-encoded string.
    If 'ca_certs' is specified, validate the server cert against it.
    If 'ssl_version' is specified, use it in the connection attempt.N)r>   r8   T)r0   r5   r   Úclosingr   r   rx   ZDER_cert_to_PEM_cert)	r   rC   rD   Ú_r>   r;   r   ZsslsockZdercertr   r   r!   r   ¹  s    ).r£   Z
__future__r   ZsslZ__ssl__r-   rQ   Zgevent._socket2r   Zgevent.socketr   r   r   rP   r   r…   Zgevent._compatr   Zgevent._utilr	   Z__implements__ÚglobalsZ__imports__rJ   Ú	NameErrorÚ__all__Úremover
   Zorig_SSLContextr.   r/   r   r(   r   r   r   r   r¦   r¤   rm   rj   ru   r•   r5   r   r   r   r   r   r!   Ú<module>   sp   


/'   v	