ó
è?F[c           @   s   d  Z  d d l Z d d l Z d d l Z d d l Z d d l m Z m Z d e j f d     YZ	 d   Z
 d   Z d e j f d	     YZ d S(
   sC   Extensions to allow HTTPS requests with SSL certificate validation.i˙˙˙˙N(   t   sixt   http_clientt   InvalidCertificateExceptionc           B   s    e  Z d  Z d   Z d   Z RS(   s?   Raised when a certificate is provided with an invalid hostname.c         C   s/   t  j j |   | |  _ | |  _ | |  _ d S(   sĤ   Constructor.

        Args:
          host: The hostname the connection was made to.
          cert: The SSL certificate (as a dictionary) the host returned.
        N(   R   t   HTTPExceptiont   __init__t   hostt   certt   reason(   t   selfR   R   R   (    (    s4   lib/python2.7/site-packages/boto/https_connection.pyR   "   s    		c         C   s   d |  j  |  j |  j f S(   Ns0   Host %s returned an invalid certificate (%s): %s(   R   R   R   (   R   (    (    s4   lib/python2.7/site-packages/boto/https_connection.pyt   __str__.   s    (   t   __name__t
   __module__t   __doc__R   R	   (    (    (    s4   lib/python2.7/site-packages/boto/https_connection.pyR      s   	c         C   s   d |  k rA g  |  d D]& } | d j    d k r | d ^ q Sg  |  d D]. } | d d j    d k rL | d d ^ qL Sd S(   sı   Returns a list of valid host globs for an SSL certificate.

    Args:
      cert: A dictionary representing an SSL certificate.
    Returns:
      list: A list of valid host globs.
    t   subjectAltNamei    t   dnsi   t   subjectt
   commonnameN(   t   lower(   R   t   x(    (    s4   lib/python2.7/site-packages/boto/https_connection.pyt   GetValidHostsForCert3   s    5c         C   sx   t  |   } t j j d | |  xO | D]G } | j d d  j d d  } t j d | f | t j  r) t Sq) Wt	 S(   s  Validates that a given hostname is valid for an SSL certificate.

    Args:
      cert: A dictionary representing an SSL certificate.
      hostname: The hostname to test.
    Returns:
      bool: Whether or not the hostname is valid for this certificate.
    s@   validating server certificate: hostname=%s, certificate hosts=%st   .s   \.t   *s   [^.]*s   ^%s$(
   R   t   botot   logt   debugt   replacet   ret   searcht   It   Truet   False(   R   t   hostnamet   hostsR   t   host_re(    (    s4   lib/python2.7/site-packages/boto/https_connection.pyt   ValidateCertificateHostnameB   s    		
t   CertValidatingHTTPSConnectionc           B   s8   e  Z d  Z e j Z e d d d d d  Z d   Z RS(   sD   An HTTPConnection that connects over SSL and validates certificates.c         K   sT   t  j r | | d <n  t j j |  d | d | | | |  _ | |  _ | |  _ d S(   s*  Constructor.

        Args:
          host: The hostname. Can be in 'host:port' form.
          port: The port. Defaults to 443.
          key_file: A file containing the client's private key
          cert_file: A file containing the client's certificates
          ca_certs: A file contianing a set of concatenated certificate authority
              certs for validating the server against.
          strict: When true, causes BadStatusLine to be raised if the status line
              can't be parsed as a valid HTTP/1.0 or 1.1 status line.
        t   strictR   t   portN(   R    t   PY2R   t   HTTPConnectionR   t   key_filet	   cert_filet   ca_certs(   R   R   R%   R(   R)   R*   R$   t   kwargs(    (    s4   lib/python2.7/site-packages/boto/https_connection.pyR   [   s    			c      
   C   s  t  |  d  r3 t j |  j |  j f |  j  } n t j |  j |  j f  } d } |  j rq | d |  j 7} n
 | d 7} t j j	 |  t
 j | d |  j d |  j d t
 j d |  j |  _ |  j j   } |  j j d	 d
  d
 } t | |  st | | d |   n  d S(   s(   Connect to a host on a given (SSL) port.t   timeouts   wrapping ssl socket; s   CA certificate file=%ss   using system provided SSL certst   keyfilet   certfilet	   cert_reqsR*   t   :i    s/   remote hostname "%s" does not match certificateN(   t   hasattrt   sockett   create_connectionR   R%   R,   R*   R   R   R   t   sslt   wrap_socketR(   R)   t   CERT_REQUIREDt   sockt   getpeercertt   splitR"   R   (   R   R7   t   msgR   R   (    (    s4   lib/python2.7/site-packages/boto/https_connection.pyt   connectt   s&    $	
		N(	   R
   R   R   R   t
   HTTPS_PORTt   default_portt   NoneR   R;   (    (    (    s4   lib/python2.7/site-packages/boto/https_connection.pyR#   V   s
   		(   R   R   R2   R4   R   t   boto.compatR    R   R   R   R   R"   R'   R#   (    (    (    s4   lib/python2.7/site-packages/boto/https_connection.pyt   <module>   s   		