
_XTc        	   @   s  d  d l  m Z m Z d  d l m Z m Z m Z d d l m Z d Z
 e Z d Z d  d l Z d  d l Z y0 d  d l m Z m Z m Z d  d l m Z Wn e k
 r n Xy  d  d l m Z m Z m Z Wn# e k
 r d \ Z Z d Z n Xy d  d l m Z Wn e k
 r#d Z n Xy d  d l m
 Z
 Wn3 e k
 rmd  d l Z d e f d     YZ
 n Xd   Z d   Z d   Z d e j d d d  Z d d d d d d d d d  Z d S(   i(   t   hexlifyt	   unhexlify(   t   md5t   sha1t   sha256i   (   t   SSLErrorN(   t   wrap_sockett	   CERT_NONEt   PROTOCOL_SSLv23(   t   HAS_SNI(   t   OP_NO_SSLv2t   OP_NO_SSLv3t   OP_NO_COMPRESSIONi   i   i   (   t   _DEFAULT_CIPHERSs   ECDH+AESGCM:DH+AESGCM:ECDH+AES256:DH+AES256:ECDH+AES128:DH+AES:ECDH+HIGH:DH+HIGH:ECDH+3DES:DH+3DES:RSA+AESGCM:RSA+AES:RSA+HIGH:RSA+3DES:!aNULL:!eNULL:!MD5(   t
   SSLContextR   c           B   sG   e  Z e j d k Z d   Z d   Z d   Z d   Z d d  Z
 RS(	   i   i   c         C   sO   | |  _  t |  _ t j |  _ d  |  _ d |  _ d  |  _	 d  |  _
 d  |  _ d  S(   Ni    (   t   protocolt   Falset   check_hostnamet   sslR   t   verify_modet   Nonet   ca_certst   optionst   certfilet   keyfilet   ciphers(   t   selft   protocol_version(    (    s5   /usr/lib/python2.7/site-packages/urllib3/util/ssl_.pyt   __init__,   s    						c         C   s   | |  _  | |  _ d  S(   N(   R   R   (   R   R   R   (    (    s5   /usr/lib/python2.7/site-packages/urllib3/util/ssl_.pyt   load_cert_chain7   s    	c         C   s   | |  _  d  S(   N(   R   (   R   t   location(    (    s5   /usr/lib/python2.7/site-packages/urllib3/util/ssl_.pyt   load_verify_locations;   s    c         C   s%   |  j  s t d   n  | |  _ d  S(   Ns   Your version of Python does not support setting a custom cipher suite. Please upgrade to Python 2.7, 3.2, or later if you need this functionality.(   t   supports_set_cipherst	   TypeErrorR   (   R   t   cipher_suite(    (    s5   /usr/lib/python2.7/site-packages/urllib3/util/ssl_.pyt   set_ciphers>   s    	c         C   sh   i |  j  d 6|  j d 6|  j d 6|  j d 6|  j d 6} |  j rW t | d |  j | St | |  Sd  S(   NR   R   R   t	   cert_reqst   ssl_versionR   (   R   R   R   R   R   R    R   R   (   R   t   sockett   server_hostnamet   kwargs(    (    s5   /usr/lib/python2.7/site-packages/urllib3/util/ssl_.pyR   G   s    



	(   i   i   N(   t   __name__t
   __module__t   syst   version_infoR    R   R   R   R#   R   R   (    (    (    s5   /usr/lib/python2.7/site-packages/urllib3/util/ssl_.pyR   )   s   					c         C   s   i t  d 6t d 6t d 6} | j d d  j   } t t |  d  \ } } | s` | | k ro t d   n  t | j	    } | | } | |   j
   } | | k s t d j t |  t |     n  d	 S(
   s   
    Checks if given fingerprint matches the supplied certificate.

    :param cert:
        Certificate as bytes object.
    :param fingerprint:
        Fingerprint as string of hexdigits, can be interspersed by colons.
    i   i   i    t   :t    i   s!   Fingerprint is of invalid length.s6   Fingerprints did not match. Expected "{0}", got "{1}".N(   R   R   R   t   replacet   lowert   divmodt   lenR   R   t   encodet   digestt   formatR    (   t   certt   fingerprintt   hashfunc_mapt   digest_lengtht   oddt   fingerprint_bytest   hashfunct   cert_digest(    (    s5   /usr/lib/python2.7/site-packages/urllib3/util/ssl_.pyt   assert_fingerprintU   s    

		c         C   s[   |  d k r t St |  t  rW t t |  d  } | d k rS t t d |   } n  | S|  S(   s  
    Resolves the argument to a numeric constant, which can be passed to
    the wrap_socket function/method from the ssl module.
    Defaults to :data:`ssl.CERT_NONE`.
    If given a string it is assumed to be the name of the constant in the
    :mod:`ssl` module or its abbrevation.
    (So you can specify `REQUIRED` instead of `CERT_REQUIRED`.
    If it's neither `None` nor a string we assume it is already the numeric
    constant which can directly be passed to wrap_socket.
    t   CERT_N(   R   R   t
   isinstancet   strt   getattrR   (   t	   candidatet   res(    (    s5   /usr/lib/python2.7/site-packages/urllib3/util/ssl_.pyt   resolve_cert_reqsz   s    c         C   s[   |  d k r t St |  t  rW t t |  d  } | d k rS t t d |   } n  | S|  S(   s    
    like resolve_cert_reqs
    t	   PROTOCOL_N(   R   R   R@   RA   RB   R   (   RC   RD   (    (    s5   /usr/lib/python2.7/site-packages/urllib3/util/ssl_.pyt   resolve_ssl_version   s    c         C   s   t  |  p t j  } | d k rH d } | t O} | t O} | t O} n  | j | O_ t | d t	  r | j
 | px t  n  | | _ t | d d  d k	 r t | _ n  | S(   s  All arguments have the same meaning as ``ssl_wrap_socket``.

    By default, this function does a lot of the same work that
    ``ssl.create_default_context`` does on Python 3.4+. It:

    - Disables SSLv2, SSLv3, and compression
    - Sets a restricted set of server ciphers

    If you wish to enable SSLv3, you can do::

        from urllib3.util import ssl_
        context = ssl_.create_urllib3_context()
        context.options &= ~ssl_.OP_NO_SSLv3

    You can do the same to enable compression (substituting ``COMPRESSION``
    for ``SSLv3`` in the last line above).

    :param ssl_version:
        The desired protocol version to use. This will default to
        PROTOCOL_SSLv23 which will negotiate the highest protocol that both
        the server and your installation of OpenSSL support.
    :param cert_reqs:
        Whether to require the certificate verification. This defaults to
        ``ssl.CERT_REQUIRED``.
    :param options:
        Specific OpenSSL options. These default to ``ssl.OP_NO_SSLv2``,
        ``ssl.OP_NO_SSLv3``, ``ssl.OP_NO_COMPRESSION``.
    :param ciphers:
        Which cipher suites to allow the server to select.
    :returns:
        Constructed SSLContext object with specified options
    :rtype: SSLContext
    i    R    R   N(   R   R   R   R   R
   R   R   R   RB   t   TrueR#   R   R   R   R   (   R%   R$   R   R   t   context(    (    s5   /usr/lib/python2.7/site-packages/urllib3/util/ssl_.pyt   create_urllib3_context   s    #

	c	         C   s   | }	 |	 d k r* t | | d | }	 n  | r y |	 j |  Wq t k
 rb }
 t |
   q t k
 r }
 |
 j t j k r t |
   n    q Xn  | r |	 j | |  n  t	 r |	 j
 |  d | S|	 j
 |   S(   s0  
    All arguments except for server_hostname and ssl_context have the same
    meaning as they do when using :func:`ssl.wrap_socket`.

    :param server_hostname:
        When SNI is supported, the expected hostname of the certificate
    :param ssl_context:
        A pre-made :class:`SSLContext` object. If none is provided, one will
        be created using :func:`create_urllib3_context`.
    :param ciphers:
        A string of ciphers we wish the client to support. This is not
        supported on Python 2.6 as the ssl module does not support it.
    R   R'   N(   R   RJ   R   t   IOErrorR   t   OSErrort   errnot   ENOENTR   R	   R   (   t   sockR   R   R$   R   R'   R%   R   t   ssl_contextRI   t   e(    (    s5   /usr/lib/python2.7/site-packages/urllib3/util/ssl_.pyt   ssl_wrap_socket   s$    
(   i   i   (    t   binasciiR    R   t   hashlibR   R   R   t
   exceptionsR   R   R   R   R	   t   create_default_contextRM   R   R   R   R   t   ImportErrorR
   R   R   R   R+   t   objectR>   RE   RG   t   CERT_REQUIREDRJ   RR   (    (    (    s5   /usr/lib/python2.7/site-packages/urllib3/util/ssl_.pyt   <module>   sD    

,	%			;	