
*?gc        
   @   sf  d  Z  d d l 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
 r8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- d d  Z. d   Z/ d e f d     YZ0 e e e1 e e e e2 e2 e d 	 Z3 d   Z4 d Z5 d Z6 d   Z7 d   Z8 e e d  Z9 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
iN(   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   CertificateErrorc           B   s   e  Z RS(    (   t   __name__t
   __module__(    (    (    s   /usr/lib/python2.7/ssl.pyR#   e   s   i   c   
      C   sR  g  } |  s t  S|  j d  } | d } | d } | j d  } | | k rg t d t |     n  | s |  j   | j   k S| d k r | j d  nY | j d  s | j d  r | j t j	 |   n" | j t j	 |  j
 d d	   x$ | D] } | j t j	 |   q Wt j d
 d j |  d t j  }	 |	 j |  S(   sh   Matching according to RFC 6125, section 6.4.3

    http://tools.ietf.org/html/rfc6125#section-6.4.3
    t   .i    i   t   *s,   too many wildcards in certificate DNS name: s   [^.]+s   xn--s   \*s   [^.]*s   \As   \.s   \Z(   t   Falset   splitt   countR#   t   reprt   lowert   appendt
   startswitht   ret   escapet   replacet   compilet   joint
   IGNORECASEt   match(
   t   dnt   hostnamet   max_wildcardst   patst   dn_splitt   leftmostt	   remaindert	   wildcardst   fragt   pat(    (    s   /usr/lib/python2.7/ssl.pyt   _dnsname_matchi   s*    

"&c         C   s[  |  s t  d   n  g  } |  j d d  } xC | D]; \ } } | d k r4 t | |  r_ d S| j |  q4 q4 W| s xc |  j d d  D]L } xC | D]; \ } } | d k r t | |  r d S| j |  q q Wq Wn  t |  d k rt d | d	 j t t |   f   n; t |  d k rKt d
 | | d f   n t d   d S(   s)  Verify that *cert* (in decoded format as returned by
    SSLSocket.getpeercert()) matches the *hostname*.  RFC 2818 and RFC 6125
    rules are followed, but IP addresses are not accepted for *hostname*.

    CertificateError is raised on failure. On success, the function
    returns nothing.
    s   empty or no certificatet   subjectAltNamet   DNSNt   subjectt
   commonNamei   s&   hostname %r doesn't match either of %ss   , s   hostname %r doesn't match %ri    s=   no appropriate commonName or subjectAltName fields were found(    (    (	   t
   ValueErrort   getR@   R-   t   lenR#   R3   t   mapR+   (   t   certR7   t   dnsnamest   sant   keyt   valuet   sub(    (    s   /usr/lib/python2.7/ssl.pyt   match_hostname   s.    %t	   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   sx  | 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  |  _ |  j   } |  j d  y |  j j d  } Wn: t k
 rb} | j t j t j f k rY  n  d } n X|  j |  | r#d } t | j |  } | | _ d  | _ y |  j   Wn t k
 rn Xz
 |  Wd  d  } Xq#nG Xt |  _ t j |  j | | | | | | |
  |  _ | r#|  j    n  | |  _! | |  _" | |  _# | |  _$ | |  _% |
 |  _& | |  _' |	 |  _( d |  _) d  S(   Ns!   only stream sockets are supportedt   _socki    i   t    s5   Closed before TLS handshake with data in recv buffer.(*   t
   getsockoptR    R!   R"   t   NotImplementedErrorR   t   __init__RQ   R   t   delattrt   AttributeErrort   Nonet   _SSLv2_IF_EXISTSt   _DEFAULT_CIPHERSt   getpeernamet   socket_errort   errnot   ENOTCONNR(   t
   _connectedt   _sslobjt
   gettimeoutt
   settimeoutt   recvt   EINVALR   t   reasont   libraryt   closet   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   sockRl   Rm   t   server_sideRn   Ro   Rp   Rr   Rs   Rq   t   attrt   et   sock_timeoutt   notconn_pre_handshake_dataRe   t    notconn_pre_handshake_data_error(    (    s   /usr/lib/python2.7/ssl.pyRU      sp    				
		
										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    RR   N(   R`   t   readR   t   argsR   Rs   (   Ru   RG   t   x(    (    s   /usr/lib/python2.7/ssl.pyR}     s    c         C   s   |  j  j |  S(   sh   Write DATA to the underlying SSL channel.  Returns
        number of bytes of DATA actually transmitted.(   R`   t   write(   Ru   t   data(    (    s   /usr/lib/python2.7/ssl.pyR   +  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.(   R`   t   peer_certificate(   Ru   t   binary_form(    (    s   /usr/lib/python2.7/ssl.pyt   getpeercert2  s    c         C   s   |  j  s d  S|  j  j   Sd  S(   N(   R`   RX   t   cipher(   Ru   (    (    s   /usr/lib/python2.7/ssl.pyR   ;  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(   R`   RE   t	   __class__Rh   R   R   R~   R   R   RQ   t   send(   Ru   R   t   flagst   vR   (    (    s   /usr/lib/python2.7/ssl.pyR   B  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(   R`   RE   R   RX   RQ   t   sendto(   Ru   R   t   flags_or_addrt   addr(    (    s   /usr/lib/python2.7/ssl.pyR   W  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(   R`   RE   R   RG   R   R   t   sendall(   Ru   R   R   t   amountR*   R   (    (    s   /usr/lib/python2.7/ssl.pyR   `  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(   R`   RE   R   R}   RQ   Rc   (   Ru   t   buflenR   (    (    s   /usr/lib/python2.7/ssl.pyRc   o  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(   RX   RG   R`   RE   R   R}   RQ   t	   recv_into(   Ru   t   buffert   nbytesR   t
   tmp_bufferR   (    (    s   /usr/lib/python2.7/ssl.pyR   y  s    		
c         C   s6   |  j  r t d |  j   n |  j j | |  Sd  S(   Ns'   recvfrom not allowed on instances of %s(   R`   RE   R   RQ   t   recvfrom(   Ru   R   R   (    (    s   /usr/lib/python2.7/ssl.pyR     s    	c         C   s9   |  j  r t d |  j   n |  j j | | |  Sd  S(   Ns,   recvfrom_into not allowed on instances of %s(   R`   RE   R   RQ   t   recvfrom_into(   Ru   R   R   R   (    (    s   /usr/lib/python2.7/ssl.pyR     s    	c         C   s   |  j  r |  j  j   Sd Sd  S(   Ni    (   R`   t   pending(   Ru   (    (    s   /usr/lib/python2.7/ssl.pyR     s    	c         C   s?   |  j  r% |  j  j   } d  |  _  | St d t |     d  S(   Ns   No SSL wrapper around (   R`   t   shutdownRX   RE   t   str(   Ru   t   s(    (    s   /usr/lib/python2.7/ssl.pyt   unwrap  s
    		c         C   s   d  |  _ t j |  |  d  S(   N(   RX   R`   R   R   (   Ru   t   how(    (    s   /usr/lib/python2.7/ssl.pyR     s    	c         C   s;   |  j  d k  r( d  |  _ t j |   n |  j  d 8_  d  S(   Ni   (   Rt   RX   R`   R   Rg   (   Ru   (    (    s   /usr/lib/python2.7/ssl.pyRg     s    	c         C   s   |  j  j   d S(   s   Perform a TLS/SSL handshake.N(   R`   Rk   (   Ru   (    (    s   /usr/lib/python2.7/ssl.pyRk     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!(   R_   RE   Ri   Rj   RQ   R(   Rl   Rm   Rn   Ro   Rp   Rq   R`   R   t
   connect_exRX   t   connectRr   Rk   Rh   R\   (   Ru   R   t   return_errnot   rc(    (    s   /usr/lib/python2.7/ssl.pyt   _real_connect  s$    			c         C   s   |  j  | t  d S(   sQ   Connects to remote ADDR, and then wraps the connection in
        an SSL channel.N(   R   R(   (   Ru   R   (    (    s   /usr/lib/python2.7/ssl.pyR     s    c         C   s   |  j  | t  S(   sQ   Connects to remote ADDR, and then wraps the connection in
        an SSL channel.(   R   Rh   (   Ru   R   (    (    s   /usr/lib/python2.7/ssl.pyR     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.Rl   Rm   Rw   Rn   Ro   Rp   Rq   Rr   Rs   N(   R   t   acceptRP   Rl   Rm   Rh   Rn   Ro   Rp   Rq   Rr   Rs   R\   Rg   (   Ru   t   newsockR   Ry   (    (    s   /usr/lib/python2.7/ssl.pyR     s     									
t   ric         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   Rg   (   Rt   R   Rh   (   Ru   t   modet   bufsize(    (    s   /usr/lib/python2.7/ssl.pyt   makefile  s    N(   R$   R%   t   __doc__RX   R(   R   R   Rh   RU   R}   R   R   R   R   R   R   Rc   R   R   R   R   R   R   Rg   Rk   R   R   R   R   R   (    (    (    s   /usr/lib/python2.7/ssl.pyRP      s6   P				
									c
   
      C   s@   t  |  d | d | d | d | d | d | d | d | d	 |	 	S(
   NRl   Rm   Rw   Rn   Ro   Rp   Rr   Rs   Rq   (   RP   (
   Rv   Rl   Rm   Rw   Rn   Ro   Rp   Rr   Rs   Rq   (    (    s   /usr/lib/python2.7/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.iNs   %b %d %H:%M:%S %Y GMT(   t   timet   mktimet   strptime(   t	   cert_timeR   (    (    s   /usr/lib/python2.7/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   /usr/lib/python2.7/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(	   R.   R   RE   t   stript   endswithR   RG   R   t   decodestring(   t   pem_cert_stringt   d(    (    s   /usr/lib/python2.7/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.Ro   Rn   Rp   N(
   RX   R   R   R   R   R   R   Rh   Rg   R   (   R   Ro   Rp   t   hostt   portRn   R   t   dercert(    (    s   /usr/lib/python2.7/ssl.pyt   get_server_certificate6  s    	
c         C   s   t  j |  d  S(   Ns	   <unknown>(   t   _PROTOCOL_NAMESRF   (   t   protocol_code(    (    s   /usr/lib/python2.7/ssl.pyt   get_protocol_nameI  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.RQ   i    N(
   R   RQ   Ri   Rj   R   R   RX   R[   R\   Rk   (   Rv   Rl   Rm   t   ssl_sock(    (    s   /usr/lib/python2.7/ssl.pyt   sslwrap_simpleO  s    
(<   R   R   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   RY   t   ImportErrorRX   R   R   R   R   R\   R   t   _getnameinfoR    R!   R"   R   R]   RZ   RE   R#   R@   RO   RP   R(   Rh   R   R   R   R   R   R   R   R   R   (    (    (    s   /usr/lib/python2.7/ssl.pyt   <module>8   sV   @




"3	( 9					