Ñò
mÈKc           @   sƒ  d  Z  d d k Z d d k Z d d k l Z d d k l Z l Z l Z d d k l Z l Z l	 Z	 l
 Z
 d d k l Z l Z l Z d d k l Z l Z l Z l Z l Z l Z l Z l Z l Z d d k l Z l Z d d	 k l Z d d k Z d
 e f d „  ƒ  YZ e e e e e	 e e e d „ Z  d „  Z! d Z" d Z# d „  Z$ d „  Z% e e d „ Z& d „  Z' e e d „ Z( 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   SSLError(   t	   CERT_NONEt   CERT_OPTIONALt   CERT_REQUIRED(   t   PROTOCOL_SSLv2t   PROTOCOL_SSLv3t   PROTOCOL_SSLv23t   PROTOCOL_TLSv1(   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   sockett   _fileobject(   t   getnameinfot	   SSLSocketc        	   B   s  e  Z d  Z d d e e e d e e 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 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
            s“  t  i ˆ  d | i ƒd ‡  f d † ˆ  _ d ‡  f d † ˆ  _ d d ‡  f d † ˆ  _ d d ‡  f d † ˆ  _ d  d ‡  f d † ˆ  _ d  d ‡  f d	 † ˆ  _	 | o | o
 | } n y t  i
 ˆ  ƒ Wn d  ˆ  _ nk Xt i ˆ  i | | | | | | ƒ ˆ  _ | o< ˆ  i ƒ  }
 z ˆ  i d  ƒ ˆ  i ƒ  Wd  ˆ  i |
 ƒ Xn | ˆ  _ | ˆ  _ | ˆ  _ | ˆ  _ | ˆ  _ | ˆ  _ |	 ˆ  _ d ˆ  _ d  S(
   Nt   _socki    c            s   t  i ˆ  |  | ƒ S(    (   R   t   send(   t   datat   flags(   t   self(    s   /usr/lib/python2.6/ssl.pyt   <lambda>^   s    c            s   t  i ˆ  |  | | ƒ S(    (   R   t   sendto(   R   t   addrR   (   R   (    s   /usr/lib/python2.6/ssl.pyR   _   s    i   c            s   t  i ˆ  |  | ƒ S(    (   R   t   recv(   t   buflenR   (   R   (    s   /usr/lib/python2.6/ssl.pyR   `   s    c            s   t  i ˆ  |  | | ƒ S(    (   R   t   recvfrom(   R   R!   R   (   R   (    s   /usr/lib/python2.6/ssl.pyR   a   s    c            s   t  i ˆ  |  | | ƒ S(    (   R   t	   recv_into(   t   buffert   nbytesR   (   R   (    s   /usr/lib/python2.6/ssl.pyR   b   s    c            s   t  i ˆ  |  | | ƒ S(    (   R   t   recvfrom_into(   R$   R%   R   (   R   (    s   /usr/lib/python2.6/ssl.pyR   c   s    (   R   t   __init__R   R   R   R    R"   t   NoneR#   R&   t   getpeernamet   _sslobjt   _sslt   sslwrapt
   gettimeoutt
   settimeoutt   do_handshaket   keyfilet   certfilet	   cert_reqst   ssl_versiont   ca_certst   do_handshake_on_connectt   suppress_ragged_eofst   _makefile_refs(   R   t   sockR0   R1   t   server_sideR2   R3   R4   R5   R6   t   timeout(    (   R   s   /usr/lib/python2.6/ssl.pyR'   W   s<    
							i   c         C   sV   y |  i  i | ƒ SWn; t j
 o/ } | i d t j o |  i o d S‚  n Xd S(   sO   Read up to LEN bytes and return them.
        Return zero-length string on EOF.i    t    N(   R*   t   readR    t   argsR   R6   (   R   t   lent   x(    (    s   /usr/lib/python2.6/ssl.pyR<   ‚   s    c         C   s   |  i  i | ƒ S(   sh   Write DATA to the underlying SSL channel.  Returns
        number of bytes of DATA actually transmitted.(   R*   t   write(   R   R   (    (    s   /usr/lib/python2.6/ssl.pyR@      s    c         C   s   |  i  i | ƒ 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.(   R*   t   peer_certificate(   R   t   binary_form(    (    s   /usr/lib/python2.6/ssl.pyt   getpeercert–   s    c         C   s    |  i  p d  S|  i  i ƒ  Sd  S(   N(   R*   R(   t   cipher(   R   (    (    s   /usr/lib/python2.6/ssl.pyRD   Ÿ   s    
i    c         C   s¿   |  i  ož | d j o t d |  i ƒ ‚ n xŠ t ok y |  i  i | ƒ } WnJ t j
 o> } | i d t j o d S| i d t j o d S‚  q1 X| Sq1 Wn t	 i
 |  | | ƒ Sd  S(   Ni    s3   non-zero flags not allowed in calls to send() on %s(   R*   t
   ValueErrort	   __class__t   TrueR@   R    R=   R   R   R   R   (   R   R   R   t   vR?   (    (    s   /usr/lib/python2.6/ssl.pyR   ¦   s"    
 c         C   s;   |  i  o t d |  i ƒ ‚ n t i |  | | | ƒ Sd  S(   Ns%   sendto not allowed on instances of %s(   R*   RE   RF   R   R   (   R   R   R   R   (    (    s   /usr/lib/python2.6/ssl.pyR   »   s    
c         C   sŽ   |  i  om | d j o t d |  i ƒ ‚ n t | ƒ } d } x/ | | j  o! |  i | | ƒ } | | 7} qC W| St i |  | | ƒ Sd  S(   Ni    s6   non-zero flags not allowed in calls to sendall() on %s(   R*   RE   RF   R>   R   R   t   sendall(   R   R   R   t   amountt   countRH   (    (    s   /usr/lib/python2.6/ssl.pyRI   Â   s    
 c         C   s¢   |  i  o | d j o t d |  i ƒ ‚ n xm t oN y |  i | ƒ SWq1 t j
 o* } | i d t j o q1 q‚ | ‚ q1 Xq1 Wn t i	 |  | | ƒ Sd  S(   Ni    s6   non-zero flags not allowed in calls to sendall() on %s(
   R*   RE   RF   RG   R<   R    R=   R   R   R    (   R   R!   R   R?   (    (    s   /usr/lib/python2.6/ssl.pyR    Ñ   s    
 c         C   sü   | o | d  j o t | ƒ } n | d  j o
 d } n |  i o | d j o t d |  i ƒ ‚ n xŒ t oj y- |  i | ƒ } t | ƒ } | | | *| SWql t j
 o* } | i d t	 j o ql qÙ | ‚ ql Xql Wn t
 i |  | | | ƒ Sd  S(   Ni   i    s8   non-zero flags not allowed in calls to recv_into() on %s(   R(   R>   R*   RE   RF   RG   R<   R    R=   R   R   R#   (   R   R$   R%   R   t
   tmp_bufferRH   R?   (    (    s   /usr/lib/python2.6/ssl.pyR#   â   s*    

 
c         C   s;   |  i  o t d |  i ƒ ‚ n t i |  | | | ƒ Sd  S(   Ns'   recvfrom not allowed on instances of %s(   R*   RE   RF   R   R"   (   R   R   R!   R   (    (    s   /usr/lib/python2.6/ssl.pyR"   ú   s    
c         C   s;   |  i  o t d |  i ƒ ‚ n t i |  | | | ƒ Sd  S(   Ns,   recvfrom_into not allowed on instances of %s(   R*   RE   RF   R   R&   (   R   R$   R%   R   (    (    s   /usr/lib/python2.6/ssl.pyR&     s    
c         C   s    |  i  o |  i  i ƒ  Sd Sd  S(   Ni    (   R*   t   pending(   R   (    (    s   /usr/lib/python2.6/ssl.pyRM     s    
c         C   sA   |  i  o |  i  i ƒ  } d  |  _  | St d t |  ƒ ƒ ‚ d  S(   Ns   No SSL wrapper around (   R*   t   shutdownR(   RE   t   str(   R   t   s(    (    s   /usr/lib/python2.6/ssl.pyt   unwrap  s
    
	c         C   s   d  |  _ t i |  | ƒ d  S(   N(   R(   R*   R   RN   (   R   t   how(    (    s   /usr/lib/python2.6/ssl.pyRN     s    	c         C   s=   |  i  d j  o d  |  _ t i |  ƒ n |  i  d 8_  d  S(   Ni   (   R7   R(   R*   R   t   close(   R   (    (    s   /usr/lib/python2.6/ssl.pyRS     s    	c         C   s   |  i  i ƒ  d S(   s   Perform a TLS/SSL handshake.N(   R*   R/   (   R   (    (    s   /usr/lib/python2.6/ssl.pyR/   !  s    c         C   s|   |  i  o t d ƒ ‚ n t i |  | ƒ t i |  i t |  i |  i	 |  i
 |  i |  i ƒ |  _  |  i o |  i ƒ  n d S(   sQ   Connects to remote ADDR, and then wraps the connection in
        an SSL channel.s/   attempt to connect already-connected SSLSocket!N(   R*   RE   R   t   connectR+   R,   R   t   FalseR0   R1   R2   R3   R4   R5   R/   (   R   R   (    (    s   /usr/lib/python2.6/ssl.pyRT   '  s    

c         C   sj   t  i |  ƒ \ } } t | d |  i d |  i d t d |  i d |  i d |  i d |  i	 d |  i
 ƒ| f 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.R0   R1   R9   R2   R3   R4   R5   R6   (   R   t   acceptR   R0   R1   RG   R2   R3   R4   R5   R6   (   R   t   newsockR   (    (    s   /usr/lib/python2.6/ssl.pyRV   7  s    								t   riÿÿÿÿc         C   s   |  i  d 7_  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   (   R7   R   (   R   t   modet   bufsize(    (    s   /usr/lib/python2.6/ssl.pyt   makefileI  s    N(   t   __name__t
   __module__t   __doc__R(   RU   R   R   RG   R'   R<   R@   RC   RD   R   R   RI   R    R#   R"   R&   RM   RQ   RN   RS   R/   RT   RV   R[   (    (    (    s   /usr/lib/python2.6/ssl.pyR   Q   s2   '										c	   	      C   s:   t  |  d | d | d | d | d | d | d | d | ƒS(	   NR0   R1   R9   R2   R3   R4   R5   R6   (   R   (	   R8   R0   R1   R9   R2   R3   R4   R5   R6   (    (    s   /usr/lib/python2.6/ssl.pyt   wrap_socketT  s
    c         C   s%   d d k  } | i | i |  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_timeR`   (    (    s   /usr/lib/python2.6/ssl.pyt   cert_time_to_secondsc  s    s   -----BEGIN CERTIFICATE-----s   -----END CERTIFICATE-----c         C   sa   t  t d ƒ o0 t i |  ƒ } t d t i | d ƒ t d St d t i |  ƒ 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   base64Re   t
   PEM_HEADERt   textwrapt   fillt
   PEM_FOOTERt   encodestring(   t   der_cert_bytest   f(    (    s   /usr/lib/python2.6/ssl.pyt   DER_cert_to_PEM_certo  s    !c         C   s{   |  i  t ƒ p t d t ƒ ‚ n |  i ƒ  i t ƒ p t d t ƒ ‚ n |  i ƒ  t t ƒ t t ƒ !} t i | ƒ 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
   startswithRh   RE   t   stript   endswithRk   R>   Rg   t   decodestring(   t   pem_cert_stringt   d(    (    s   /usr/lib/python2.6/ssl.pyt   PEM_cert_to_DER_cert  s     c         C   sz   |  \ } } | d j	 o
 t } n t } t t ƒ  d | d | d | ƒ} | i |  ƒ | i t ƒ } | i ƒ  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.R3   R2   R4   N(
   R(   R   R   R_   R   RT   RC   RG   RS   Ro   (   R   R3   R4   t   hostt   portR2   RP   t   dercert(    (    s   /usr/lib/python2.6/ssl.pyt   get_server_certificate  s    

c         C   sP   |  t  j o d S|  t j o d S|  t j o d S|  t j o d Sd Sd  S(   Nt   TLSv1t   SSLv23t   SSLv2t   SSLv3s	   <unknown>(   R   R   R   R   (   t   protocol_code(    (    s   /usr/lib/python2.6/ssl.pyt   get_protocol_name   s    c         C   sd   t  |  d ƒ o |  i }  n t i |  d | | t t d ƒ } y |  i ƒ  Wn n X| i ƒ  | 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(	   Rf   R   R+   R,   R   R   R(   R)   R/   (   R8   R0   R1   t   ssl_sock(    (    s   /usr/lib/python2.6/ssl.pyt   sslwrap_simple¯  s    
()   R^   Ri   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   _getnameinfoRg   R   R(   RU   RG   R_   Rd   Rh   Rk   Ro   Rv   Rz   R€   R‚   (    (    (    s   /usr/lib/python2.6/ssl.pyt   <module>8   s2   "@ÿ 					