B
    H/\7B              	   @   sJ  d Z ddlmZ ddlZejZddlZddlZddlm	Z	 ddl
mZmZ ddl
mZmZ ddl
mZ ddlmZ dd	lmZ d
dddgZeee edg ddZeeeeeB Zdekred G dd
 d
e	Zeseedse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ddZ(dS ) a<  
SSL wrapper for socket objects on Python 2.7.8 and below.

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

This module implements cooperative SSL socket wrappers.

.. deprecated:: 1.3
   This module is not secure. Support for Python versions
   with only this level of SSL will be dropped in gevent 1.4.
    )absolute_importN)socket)_fileobjecttimeout_default)errorEWOULDBLOCK)timeout)PYPY)copy_globals	SSLSocketwrap_socketget_server_certificatesslwrap_simpler    )Znames_to_ignoreZdunder_names_to_keep
namedtuplec            	   @   s   e Zd ZdZdddeeddddf	ddZd7dd	Zd
d Zd8ddZ	dd Z
defddZd9ddZdd Zd:ddZd;ddZdd Zdd Zdd  Zd!d" Zd#d$ Zd%d& Zd'd( Zerd)d* Zd+d, Zd-d. Zd/d0 Zd1d2 Zd<d5d6ZdS )=r   z{
    gevent `ssl.SSLSocket <https://docs.python.org/2.6/library/ssl.html#sslsocket-objects>`_
    for Pythons < 2.7.9.
    NFTc          
   C   s   t j| |d tr|  |r&|s&|}yt |  W n: tk
rn } z|jd tjkrX d | _	W d d }~X Y nNX |
d krt
| j||||||| _	nt
| j|||||||
| _	|r|   || _|| _|| _|| _|| _|
| _|| _|	| _d| _d S )N)_sockr   )r   __init__r	   _dropZgetpeernamesocket_errorargserrnoZENOTCONN_sslobj_sslsslwrapr   do_handshakekeyfilecertfile	cert_reqsssl_versionca_certsciphersdo_handshake_on_connectsuppress_ragged_eofs_makefile_refs)selfsockr   r   server_sider   r   r   r!   r"   r    er   r   +lib/python3.7/site-packages/gevent/_ssl2.pyr   ;   s<    

zSSLSocket.__init__   c          
   C   s   xy| j |S  tk
r } z|jd tkr8| jr8dS |jd tkrl| jdkrR 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 )zORead up to LEN bytes and return them.
        Return zero-length string on EOF.r    g        )timeout_excN)r   readSSLErrorr   SSL_ERROR_EOFr"   SSL_ERROR_WANT_READr   sys	exc_clear_wait_read_event_SSLErrorReadTimeoutSSL_ERROR_WANT_WRITE_write_event)r$   lenexr   r   r(   r,   g   s"    

zSSLSocket.readc          
   C   s   xy| j |S  tk
r } zn|jd tkrT| jdkr: t  | j| j	t
d n6|jd tkr| jdkrn t  | 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.r   g        )r+   N)r   writer-   r   r/   r   r0   r1   r2   r3   _SSLErrorWriteTimeoutr5   r6   )r$   datar8   r   r   r(   r9   ~   s    

zSSLSocket.writec             C   s   | j |S )zReturns 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.)r   Zpeer_certificate)r$   Zbinary_formr   r   r(   getpeercert   s    zSSLSocket.getpeercertc             C   s   | j s
d S | j  S )N)r   cipher)r$   r   r   r(   r=      s    zSSLSocket.cipherr   c          
   C   s   |t kr| j}| jr|dkr*td| j xy| j|}W n tk
r } zj|jd tkr| jdkrjdS t	
  | | j n4|jd tkr| jdkrdS t	
  | | j n W d d }~X Y q,X |S q,W nt| |||S d S )Nr   z3non-zero flags not allowed in calls to send() on %sg        )r   r   r   
ValueError	__class__r9   r-   r   r/   r0   r1   r2   r3   r5   r6   r   send)r$   r;   flagsr   vxr   r   r(   r@      s0    



zSSLSocket.sendc          
   C   sT   yt | | W n> tk
rN } z | jdkr4ttt|j W d d }~X Y nX d S )Ng        )r   sendall_socket_timeoutr   r-   r5   r   )r$   r;   rA   r8   r   r   r(   rD      s    
zSSLSocket.sendallc             G   s*   | j rtd| j ntj| f| S d S )Nz%sendto not allowed on instances of %s)r   r>   r?   r   sendto)r$   r   r   r   r(   rF      s    zSSLSocket.sendtoc             C   s4   | j r&|dkrtd| j | |S t| ||S )Nr   z3non-zero flags not allowed in calls to recv() on %s)r   r>   r?   r,   r   recv)r$   ZbuflenrA   r   r   r(   rG      s    

zSSLSocket.recvc          
   C   s   |r|d krt |}n|d kr"d}| jr|dkr>td| j xy"| |}t |}||d |< |S  tk
r } z8|jd tkr| jdkr t	
  | | j w@n W d d }~X Y q@X q@W nt| |||S d S )Ni   r   z8non-zero flags not allowed in calls to recv_into() on %sg        )r7   r   r>   r?   r,   r-   r   r/   r   r0   r1   r2   r3   r   	recv_into)r$   buffernbytesrA   Z
tmp_bufferrB   rC   r   r   r(   rH      s0    



zSSLSocket.recv_intoc             G   s*   | j rtd| j ntj| f| S d S )Nz'recvfrom not allowed on instances of %s)r   r>   r?   r   recvfrom)r$   r   r   r   r(   rK      s    zSSLSocket.recvfromc             G   s*   | j rtd| j ntj| f| S d S )Nz,recvfrom_into not allowed on instances of %s)r   r>   r?   r   recvfrom_into)r$   r   r   r   r(   rL      s    zSSLSocket.recvfrom_intoc             C   s   | j r| j  S dS )Nr   )r   pending)r$   r   r   r(   rM     s    
zSSLSocket.pendingc          
   C   s   xy
| 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   r*   g        )r+   )r   shutdownr-   r   r.   r"   r/   r   r0   r1   r2   r3   r4   r5   r6   r:   )r$   r8   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 )r   )r   r>   strrO   r   )r$   sr   r   r(   unwrap  s
    zSSLSocket.unwrapc             C   s   d | _ t| | d S )N)r   r   rN   )r$   Zhowr   r   r(   rN   "  s    zSSLSocket.shutdownc             C   s.   | j dk rd | _t|  n|  j d8  _ d S )N   )r#   r   r   close)r$   r   r   r(   rT   &  s    
zSSLSocket.closec             C   s   |  j d7  _ d S )NrS   )r#   )r$   r   r   r(   _reuse/  s    zSSLSocket._reusec             C   s&   | j dk r|   n|  j d8  _ d S )NrS   )r#   rT   )r$   r   r   r(   r   2  s    

zSSLSocket._dropc          
   C   s   xy
| j  S  tk
r } zn|jd tkrR| jdkr8 t  | j| j	t
d n6|jd tkr| jdkrl t  | j| jt
d n W dd}~X Y qX qW dS )zPerform a TLS/SSL handshake.r   g        )r+   N)r   r   r-   r   r/   r   r0   r1   r2   r3   _SSLErrorHandshakeTimeoutr5   r6   )r$   r8   r   r   r(   r   8  s    


zSSLSocket.do_handshakec          
   C   s   | j rtdt| | | jdkrJt| jd| j| j	| j
| j| j| _ n(t| jd| j| j	| j
| j| j| j| _ | jr|   dS )zQConnects to remote ADDR, and then wraps the connection in
        an SSL channel.z/attempt to connect already-connected SSLSocket!NF)r   r>   r   connectr    r   r   r   r   r   r   r   r   r!   r   )r$   addrr   r   r(   rW   K  s    
zSSLSocket.connectc             C   s   | j }xhy| \}}P W nD tk
r^ } z&|jd tksD| jdkrF t  W dd}~X Y nX | | j	 qW t
|| j| jd| j| j| j| j| j| jd
}||fS )zAccepts 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.r   g        NT)	r   r   r&   r   r   r   r!   r"   r    )r   acceptr   r   r   r   r0   r1   r2   r3   r   r   r   r   r   r   r!   r"   r    )r$   r%   Zclient_socketZaddressr8   Zsslobjr   r   r(   rY   ^  s*    
zSSLSocket.acceptrc             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.rS   T)rT   )r	   r#   r   )r$   modebufsizer   r   r(   makefilez  s    zSSLSocket.makefile)r)   )F)r   )r)   r   )Nr   )rZ   r[   )__name__
__module____qualname____doc__	CERT_NONEPROTOCOL_SSLv23r   r,   r9   r<   r=   r   r@   rD   rF   rG   rH   rK   rL   rM   rO   rR   rN   rT   r	   rU   r   r   rW   rY   r^   r   r   r   r(   r   5   s<   '





r   c             C   s   |   S )N)Z
gettimeout)r$   r   r   r(   <lambda>  s    re   c             C   s
   |  |S )N)Z
settimeout)r$   valuer   r   r(   re     s    zThe read operation timed outzThe write operation timed outz!The handshake operation timed outFTc
       
      C   s   t | |||||||||	d
S )z)Create a new :class:`SSLSocket` instance.)	r   r   r&   r   r   r   r!   r"   r    )r   )
r%   r   r   r&   r   r   r   r!   r"   r    r   r   r(   r     s    c             C   sH   |dk	rt }nt}tt |||d}||  |d}|  t|S )zRetrieve 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   r   r   T)ZCERT_REQUIREDrc   r   r   rW   r<   rT   ZDER_cert_to_PEM_cert)rX   r   r   r   rQ   Zdercertr   r   r(   r     s    


c             C   s   t | ||S )zA replacement for the old socket.ssl function.  Designed
    for compatibility with Python 2.5 and earlier.  Will disappear in
    Python 3.0.)r   )r%   r   r   r   r   r(   r     s    )NN))rb   Z
__future__r   ZsslZ__ssl__r   r0   r   Zgevent._socket2r   Zgevent.socketr   r   r   r   r   r   rE   Zgevent._compatr	   Zgevent._utilr
   Z__implements__globalsZ__imports__listset__all__remover   hasattrpropertyr-   r4   r:   rV   rc   rd   r   r   r   r   r   r   r(   <module>   sJ   
  Q
