ó
ëÅ°Sc        
   @   s/  d  Z  d d l Z d d l Z d d l m Z m Z m Z d d l m Z d d l m Z m Z m	 Z	 d d l m
 Z
 m Z m Z d d l m Z m Z m Z m Z m Z m Z m Z m Z m Z d d l m Z m Z m Z i d	 e 6d
 e 6d e 6Z y d d l m Z e Z Wn e k
 r,e Z n Xd e e <d d l m Z m Z m  Z  m! Z" d d l m# Z$ d d l m% Z% m& Z& m' Z' d d l( Z( d d l) Z) d Z* d e f d „  ƒ  YZ+ e e e, e e e e- e- e d „	 Z. d „  Z/ d Z0 d Z1 d „  Z2 d „  Z3 e e d „ Z4 d „  Z5 e e d „ Z6 d S(   s  This module provides some more Pythonic support for SSL.

Object types:

  SSLSocket -- subtype of socket.socket which does SSL over the socket

Exceptions:

  SSLError -- exception raised for I/O errors

Functions:

  cert_time_to_seconds -- convert time string used for certificate
                          notBefore and notAfter functions to integer
                          seconds past the Epoch (the time values
                          returned from time.time())

  fetch_server_certificate (HOST, PORT) -- fetch the certificate provided
                          by the server running on HOST at port PORT.  No
                          validation of the certificate is performed.

Integer constants:

SSL_ERROR_ZERO_RETURN
SSL_ERROR_WANT_READ
SSL_ERROR_WANT_WRITE
SSL_ERROR_WANT_X509_LOOKUP
SSL_ERROR_SYSCALL
SSL_ERROR_SSL
SSL_ERROR_WANT_CONNECT

SSL_ERROR_EOF
SSL_ERROR_INVALID_ERROR_CODE

The following group define certificate requirements that one side is
allowing/requiring from the other side:

CERT_NONE - no certificates from the other side are required (or will
            be looked at if provided)
CERT_OPTIONAL - certificates are not required, but if provided will be
                validated, and if validation fails, the connection will
                also fail
CERT_REQUIRED - certificates are required, and will be validated, and
                if validation fails, the connection will also fail

The following constants identify various SSL protocol variants:

PROTOCOL_SSLv2
PROTOCOL_SSLv3
PROTOCOL_SSLv23
PROTOCOL_TLSv1
iÿÿÿÿN(   t   OPENSSL_VERSION_NUMBERt   OPENSSL_VERSION_INFOt   OPENSSL_VERSION(   t   SSLError(   t	   CERT_NONEt   CERT_OPTIONALt   CERT_REQUIRED(   t   RAND_statust   RAND_egdt   RAND_add(	   t   SSL_ERROR_ZERO_RETURNt   SSL_ERROR_WANT_READt   SSL_ERROR_WANT_WRITEt   SSL_ERROR_WANT_X509_LOOKUPt   SSL_ERROR_SYSCALLt   SSL_ERROR_SSLt   SSL_ERROR_WANT_CONNECTt   SSL_ERROR_EOFt   SSL_ERROR_INVALID_ERROR_CODE(   t   PROTOCOL_SSLv3t   PROTOCOL_SSLv23t   PROTOCOL_TLSv1t   TLSv1t   SSLv23t   SSLv3(   t   PROTOCOL_SSLv2t   SSLv2(   t   sockett   _fileobjectt   _delegate_methodst   error(   t   getnameinfo(   t
   SOL_SOCKETt   SO_TYPEt   SOCK_STREAMs)   DEFAULT:!aNULL:!eNULL:!LOW:!EXPORT:!SSLv2t	   SSLSocketc        
   B   s  e  Z d  Z d d e e e d e e d d „	 Z d d „ Z	 d „  Z
 e d „ Z d „  Z d d „ Z d d	 „ Z d d
 „ Z d d d „ Z d d d „ Z d d d „ Z d d d „ Z d „  Z d „  Z d „  Z d „  Z d „  Z d „  Z d „  Z d „  Z d „  Z d d d „ Z RS(   sµ   This class implements a subtype of socket.socket that wraps
    the underlying OS socket in an SSL context when necessary, and
    provides read and write methods over that channel.c      	   C   s–  | j  t t ƒ t k r' t d ƒ ‚ n  t j |  d | j ƒx3 t D]+ } y t	 |  | ƒ WqD t
 k
 rn qD XqD W|
 d  k r” | t k r” t }
 n  | rª | rª | } n  y t j |  ƒ Wn= t k
 rú } | j t j k rå ‚  n  t |  _ d  |  _ nG Xt |  _ t j |  j | | | | | | |
 ƒ |  _ | rA|  j ƒ  n  | |  _ | |  _ | |  _ | |  _ | |  _ |
 |  _ | |  _ |	 |  _  d |  _! d  S(   Ns!   only stream sockets are supportedt   _socki    ("   t
   getsockoptR    R!   R"   t   NotImplementedErrorR   t   __init__R$   R   t   delattrt   AttributeErrort   Nonet   _SSLv2_IF_EXISTSt   _DEFAULT_CIPHERSt   getpeernamet   socket_errort   errnot   ENOTCONNt   Falset
   _connectedt   _sslobjt   Truet   _sslt   sslwrapt   do_handshaket   keyfilet   certfilet	   cert_reqst   ssl_versiont   ca_certst   cipherst   do_handshake_on_connectt   suppress_ragged_eofst   _makefile_refs(   t   selft   sockR8   R9   t   server_sideR:   R;   R<   R>   R?   R=   t   attrt   e(    (    s$   /home/thedrx/Python-2.7.8/Lib/ssl.pyR'   k   sF    													i   c         C   sQ   y |  j  j | ƒ SWn6 t k
 rL } | j d t k rF |  j rF d S‚  n Xd S(   sO   Read up to LEN bytes and return them.
        Return zero-length string on EOF.i    t    N(   R3   t   readR   t   argsR   R?   (   RA   t   lent   x(    (    s$   /home/thedrx/Python-2.7.8/Lib/ssl.pyRG   Ÿ   s    c         C   s   |  j  j | ƒ S(   sh   Write DATA to the underlying SSL channel.  Returns
        number of bytes of DATA actually transmitted.(   R3   t   write(   RA   t   data(    (    s$   /home/thedrx/Python-2.7.8/Lib/ssl.pyRK   ¬   s    c         C   s   |  j  j | ƒ S(   sá   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.(   R3   t   peer_certificate(   RA   t   binary_form(    (    s$   /home/thedrx/Python-2.7.8/Lib/ssl.pyt   getpeercert³   s    c         C   s   |  j  s d  S|  j  j ƒ  Sd  S(   N(   R3   R*   t   cipher(   RA   (    (    s$   /home/thedrx/Python-2.7.8/Lib/ssl.pyRP   ¼   s    	i    c         C   s³   |  j  rœ | d k r+ t d |  j ƒ ‚ n  x t r˜ y |  j  j | ƒ } WnD t k
 r } | j d t k rs d S| j d t k rŠ d S‚  q. X| Sq. Wn |  j	 j
 | | ƒ Sd  S(   Ni    s3   non-zero flags not allowed in calls to send() on %s(   R3   t
   ValueErrort	   __class__R4   RK   R   RH   R   R   R$   t   send(   RA   RL   t   flagst   vRJ   (    (    s$   /home/thedrx/Python-2.7.8/Lib/ssl.pyRS   Ã   s     		c         C   sX   |  j  r t d |  j ƒ ‚ n5 | d  k r> |  j j | | ƒ S|  j j | | | ƒ Sd  S(   Ns%   sendto not allowed on instances of %s(   R3   RQ   RR   R*   R$   t   sendto(   RA   RL   t   flags_or_addrt   addr(    (    s$   /home/thedrx/Python-2.7.8/Lib/ssl.pyRV   Ø   s    	c         C   sˆ   |  j  rq | d k r+ t d |  j ƒ ‚ n  t | ƒ } d } x- | | k  rl |  j | | ƒ } | | 7} q@ W| St j |  | | ƒ Sd  S(   Ni    s6   non-zero flags not allowed in calls to sendall() on %s(   R3   RQ   RR   RI   RS   R   t   sendall(   RA   RL   RT   t   amountt   countRU   (    (    s$   /home/thedrx/Python-2.7.8/Lib/ssl.pyRY   á   s    	c         C   sO   |  j  r8 | d k r+ t d |  j ƒ ‚ n  |  j | ƒ S|  j j | | ƒ Sd  S(   Ni    s3   non-zero flags not allowed in calls to recv() on %s(   R3   RQ   RR   RG   R$   t   recv(   RA   t   buflenRT   (    (    s$   /home/thedrx/Python-2.7.8/Lib/ssl.pyR\   ð   s    	c         C   s¤   | r! | d  k r! t | ƒ } n | d  k r6 d } n  |  j rŠ | d k ra t d |  j ƒ ‚ n  |  j | ƒ } t | ƒ } | | | *| S|  j j | | | ƒ Sd  S(   Ni   i    s8   non-zero flags not allowed in calls to recv_into() on %s(   R*   RI   R3   RQ   RR   RG   R$   t	   recv_into(   RA   t   buffert   nbytesRT   t
   tmp_bufferRU   (    (    s$   /home/thedrx/Python-2.7.8/Lib/ssl.pyR^   ú   s    		
c         C   s6   |  j  r t d |  j ƒ ‚ n |  j j | | ƒ Sd  S(   Ns'   recvfrom not allowed on instances of %s(   R3   RQ   RR   R$   t   recvfrom(   RA   R]   RT   (    (    s$   /home/thedrx/Python-2.7.8/Lib/ssl.pyRb     s    	c         C   s9   |  j  r t d |  j ƒ ‚ n |  j j | | | ƒ Sd  S(   Ns,   recvfrom_into not allowed on instances of %s(   R3   RQ   RR   R$   t   recvfrom_into(   RA   R_   R`   RT   (    (    s$   /home/thedrx/Python-2.7.8/Lib/ssl.pyRc     s    	c         C   s   |  j  r |  j  j ƒ  Sd Sd  S(   Ni    (   R3   t   pending(   RA   (    (    s$   /home/thedrx/Python-2.7.8/Lib/ssl.pyRd     s    	c         C   s?   |  j  r% |  j  j ƒ  } d  |  _  | St d t |  ƒ ƒ ‚ d  S(   Ns   No SSL wrapper around (   R3   t   shutdownR*   RQ   t   str(   RA   t   s(    (    s$   /home/thedrx/Python-2.7.8/Lib/ssl.pyt   unwrap  s
    		c         C   s   d  |  _ t j |  | ƒ d  S(   N(   R*   R3   R   Re   (   RA   t   how(    (    s$   /home/thedrx/Python-2.7.8/Lib/ssl.pyRe   '  s    	c         C   s;   |  j  d k  r( d  |  _ t j |  ƒ n |  j  d 8_  d  S(   Ni   (   R@   R*   R3   R   t   close(   RA   (    (    s$   /home/thedrx/Python-2.7.8/Lib/ssl.pyRj   +  s    	c         C   s   |  j  j ƒ  d S(   s   Perform a TLS/SSL handshake.N(   R3   R7   (   RA   (    (    s$   /home/thedrx/Python-2.7.8/Lib/ssl.pyR7   2  s    c      	   C   sÙ   |  j  r t d ƒ ‚ n  t j |  j t |  j |  j |  j |  j	 |  j
 |  j ƒ |  _ ya | rr t j |  | ƒ } n d  } t j |  | ƒ | s° |  j r¤ |  j ƒ  n  t |  _  n  | SWn t k
 rÔ d  |  _ ‚  n Xd  S(   Ns/   attempt to connect already-connected SSLSocket!(   R2   RQ   R5   R6   R$   R1   R8   R9   R:   R;   R<   R=   R3   R   t
   connect_exR*   t   connectR>   R7   R4   R.   (   RA   RX   t   return_errnot   rc(    (    s$   /home/thedrx/Python-2.7.8/Lib/ssl.pyt   _real_connect8  s$    			c         C   s   |  j  | t ƒ d S(   sQ   Connects to remote ADDR, and then wraps the connection in
        an SSL channel.N(   Ro   R1   (   RA   RX   (    (    s$   /home/thedrx/Python-2.7.8/Lib/ssl.pyRl   O  s    c         C   s   |  j  | t ƒ S(   sQ   Connects to remote ADDR, and then wraps the connection in
        an SSL channel.(   Ro   R4   (   RA   RX   (    (    s$   /home/thedrx/Python-2.7.8/Lib/ssl.pyRk   T  s    c         C   s¡   t  j |  ƒ \ } } yb t | d |  j d |  j d t d |  j d |  j d |  j d |  j	 d |  j
 d	 |  j ƒ	| f SWn# t k
 rœ } | j ƒ  | ‚ n Xd
 S(   s¿   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.R8   R9   RC   R:   R;   R<   R=   R>   R?   N(   R   t   acceptR#   R8   R9   R4   R:   R;   R<   R=   R>   R?   R.   Rj   (   RA   t   newsockRX   RE   (    (    s$   /home/thedrx/Python-2.7.8/Lib/ssl.pyRp   Y  s     									
t   riÿÿÿÿc         C   s%   |  j  d 7_  t |  | | d t ƒS(   s   Make and return a file-like object that
        works with the SSL connection.  Just use the code
        from the socket module.i   Rj   (   R@   R   R4   (   RA   t   modet   bufsize(    (    s$   /home/thedrx/Python-2.7.8/Lib/ssl.pyt   makefilep  s    N(   t   __name__t
   __module__t   __doc__R*   R1   R   R   R4   R'   RG   RK   RO   RP   RS   RV   RY   R\   R^   Rb   Rc   Rd   Rh   Re   Rj   R7   Ro   Rl   Rk   Rp   Ru   (    (    (    s$   /home/thedrx/Python-2.7.8/Lib/ssl.pyR#   e   s6   0				
									c
   
      C   s@   t  |  d | d | d | d | d | d | d | d | d	 |	 ƒ	S(
   NR8   R9   RC   R:   R;   R<   R>   R?   R=   (   R#   (
   RB   R8   R9   RC   R:   R;   R<   R>   R?   R=   (    (    s$   /home/thedrx/Python-2.7.8/Lib/ssl.pyt   wrap_socket}  s    c         C   s%   d d l  } | j | j |  d ƒ ƒ S(   s¢   Takes a date-time string in standard ASN1_print form
    ("MON DAY 24HOUR:MINUTE:SEC YEAR TIMEZONE") and return
    a Python time value in seconds past the epoch.iÿÿÿÿNs   %b %d %H:%M:%S %Y GMT(   t   timet   mktimet   strptime(   t	   cert_timeRz   (    (    s$   /home/thedrx/Python-2.7.8/Lib/ssl.pyt   cert_time_to_seconds  s    s   -----BEGIN CERTIFICATE-----s   -----END CERTIFICATE-----c         C   sc   t  t d ƒ rB t j |  ƒ } t d t j | d ƒ d t d St d t j |  ƒ t d Sd S(   s[   Takes a certificate in binary DER format and returns the
    PEM version of it as a string.t   standard_b64encodes   
i@   N(   t   hasattrt   base64R   t
   PEM_HEADERt   textwrapt   fillt
   PEM_FOOTERt   encodestring(   t   der_cert_bytest   f(    (    s$   /home/thedrx/Python-2.7.8/Lib/ssl.pyt   DER_cert_to_PEM_cert™  s    $c         C   sw   |  j  t ƒ s" t d t ƒ ‚ n  |  j ƒ  j t ƒ sJ t d t ƒ ‚ n  |  j ƒ  t t ƒ t t ƒ !} t j | ƒ S(   sh   Takes a certificate in ASCII PEM format and returns the
    DER-encoded version of it as a byte sequences(   Invalid PEM encoding; must start with %ss&   Invalid PEM encoding; must end with %s(	   t
   startswithR‚   RQ   t   stript   endswithR…   RI   R   t   decodestring(   t   pem_cert_stringt   d(    (    s$   /home/thedrx/Python-2.7.8/Lib/ssl.pyt   PEM_cert_to_DER_cert©  s     c         C   sx   |  \ } } | d k	 r! t } n t } t t ƒ  d | d | d | ƒ} | j |  ƒ | j t ƒ } | j ƒ  t	 | ƒ S(   s÷   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.R;   R:   R<   N(
   R*   R   R   Ry   R   Rl   RO   R4   Rj   R‰   (   RX   R;   R<   t   hostt   portR:   Rg   t   dercert(    (    s$   /home/thedrx/Python-2.7.8/Lib/ssl.pyt   get_server_certificate·  s    	
c         C   s   t  j |  d ƒ S(   Ns	   <unknown>(   t   _PROTOCOL_NAMESt   get(   t   protocol_code(    (    s$   /home/thedrx/Python-2.7.8/Lib/ssl.pyt   get_protocol_nameÊ  s    c         C   sl   t  |  d ƒ r |  j }  n  t j |  d | | t t d ƒ } y |  j ƒ  Wn t k
 r] n X| j	 ƒ  | S(   sŒ   A replacement for the old socket.ssl function.  Designed
    for compability with Python 2.5 and earlier.  Will disappear in
    Python 3.0.R$   i    N(
   R€   R$   R5   R6   R   R   R*   R-   R.   R7   (   RB   R8   R9   t   ssl_sock(    (    s$   /home/thedrx/Python-2.7.8/Lib/ssl.pyt   sslwrap_simpleÐ  s    
(7   Rx   Rƒ   R5   R    R   R   R   R   R   R   R   R   R	   R
   R   R   R   R   R   R   R   R   R   R   R   R•   R   R+   t   ImportErrorR*   R   R   R   R   R.   R   t   _getnameinfoR    R!   R"   R   R/   R,   R#   R1   R4   Ry   R~   R‚   R…   R‰   R   R”   R˜   Rš   (    (    (    s$   /home/thedrx/Python-2.7.8/Lib/ssl.pyt   <module>8   sN   @




"ÿ 					