
^Wc           @@  s  d  Z  d d l m Z y$ d d l m Z d d l m Z Wn e k
 r[ Z	 e
 e	   n Xd d l Z d d l m Z d d l m Z m Z d d l m Z m Z y d d	 l m Z Wn' e
 k
 r d Z d d
 l m Z n Xd d l Z d d l Z d d l Z d d l m Z d d l m  Z  d d g Z! e Z" i e j# j$ e j% 6e j# j& e j' 6Z( e) e d  re) e j# d  re j# j* e( e j+ <n  e) e d  re) e j# d  re j# j, e( e j- <n  y! e( j. i e j# j/ e j0 6 Wn e1 k
 r n Xi e j# j2 e j3 6e j# j4 e j5 6e j# j4 e j# j6 e j7 6Z8 e  j9 j: j; d  Z< d Z= e  j" Z> e j? Z@ d   ZA d   ZB d e f d     YZ d   ZC d eD f d     YZE e rd d  ZF n e ZF eF eE _F d   ZG d d d d d d d d   Z? d S(!   s!  SSL with SNI_-support for Python 2. Follow these instructions if you would
like to verify SSL certificates in Python 2. Note, the default libraries do
*not* do certificate checking; you need to do additional work to validate
certificates yourself.

This needs the following packages installed:

* pyOpenSSL (tested with 0.13)
* ndg-httpsclient (tested with 0.3.2)
* pyasn1 (tested with 0.1.6)

You can install them with the following command:

    pip install pyopenssl ndg-httpsclient pyasn1

To activate certificate checking, call
:func:`~urllib3.contrib.pyopenssl.inject_into_urllib3` from your Python code
before you begin making HTTP requests. This can be done in a ``sitecustomize``
module, or at any other time before your application begins using ``urllib3``,
like this::

    try:
        import urllib3.contrib.pyopenssl
        urllib3.contrib.pyopenssl.inject_into_urllib3()
    except ImportError:
        pass

Now you can use :mod:`urllib3` as you normally would, and it will support SNI
when the required modules are installed.

Activating this module also has the positive side effect of disabling SSL/TLS
compression in Python 2 (see `CRIME attack`_).

If you want to configure the default list of supported cipher suites, you can
set the ``urllib3.contrib.pyopenssl.DEFAULT_SSL_CIPHER_LIST`` variable.

Module Variables
----------------

:var DEFAULT_SSL_CIPHER_LIST: The list of supported SSL/TLS cipher suites.

.. _sni: https://en.wikipedia.org/wiki/Server_Name_Indication
.. _crime attack: https://en.wikipedia.org/wiki/CRIME_(security_exploit)

i    (   t   absolute_import(   t   SUBJ_ALT_NAME_SUPPORT(   t   SubjectAltNameN(   t   decoder(   t   univt
   constraint(   t   timeoutt   error(   t   _fileobject(   t   backport_makefilei   (   t
   connection(   t   utilt   inject_into_urllib3t   extract_from_urllib3t   PROTOCOL_TLSv1_1t   TLSv1_1_METHODt   PROTOCOL_TLSv1_2t   TLSv1_2_METHODt   asciii @  c           C@  s   t  t _  t t _ t t _ d S(   s7   Monkey-patch urllib3 with PyOpenSSL-backed SSL-support.N(   t   ssl_wrap_socketR
   t   HAS_SNIR   t   Truet   IS_PYOPENSSL(    (    (    s   /private/var/folders/3n/6h2rwf155rn1m71wwyxn79n80000gn/T/pip-build-IcAT_k/pip/pip/_vendor/requests/packages/urllib3/contrib/pyopenssl.pyR   n   s    		c           C@  s   t  t _ t t _ t t _ d S(   s4   Undo monkey-patching by :func:`inject_into_urllib3`.N(   t   orig_connection_ssl_wrap_socketR
   R   t   orig_util_HAS_SNIR   R   t   FalseR   (    (    (    s   /private/var/folders/3n/6h2rwf155rn1m71wwyxn79n80000gn/T/pip-build-IcAT_k/pip/pip/_vendor/requests/packages/urllib3/contrib/pyopenssl.pyR   v   s    		R   c           B@  s*   e  Z d  Z e j j e j d d  Z RS(   s0   ASN.1 implementation for subjectAltNames supporti   i   (   t   __name__t
   __module__t   __doc__R   t
   SequenceOft   sizeSpecR   t   ValueSizeConstraint(    (    (    s   /private/var/folders/3n/6h2rwf155rn1m71wwyxn79n80000gn/T/pip-build-IcAT_k/pip/pip/_vendor/requests/packages/urllib3/contrib/pyopenssl.pyR      s   	c         C@  s  g  } t  s | St   } x t |  j    D] } |  j |  } | j   } | d k r_ q, n  | j   } t j | d | } x | D]x } t	 | t  s q n  xZ t t
 |   D]F }	 | j |	  }
 |
 j   d k r q n  | j t |
 j     q Wq Wq, W| S(   Nt   subjectAltNamet   asn1Spect   dNSName(   R   R   t   ranget   get_extension_countt   get_extensiont   get_short_namet   get_datat   der_decodert   decodet
   isinstancet   lent   getComponentByPositiont   getNamet   appendt   strt   getComponent(   t	   peer_certt   dns_namet   general_namest   it   extt   ext_namet   ext_datt   decoded_datt   namet   entryt	   component(    (    s   /private/var/folders/3n/6h2rwf155rn1m71wwyxn79n80000gn/T/pip-build-IcAT_k/pip/pip/_vendor/requests/packages/urllib3/contrib/pyopenssl.pyt   get_subj_alt_name   s*    		%t   WrappedSocketc           B@  s   e  Z d  Z e d  Z d   Z d   Z d   Z d   Z d   Z	 d   Z
 d   Z d	   Z d
   Z e d  Z d   Z d   Z RS(   s   API-compatibility wrapper for Python OpenSSL's Connection-class.

    Note: _makefile_refs, _drop() and _reuse() are needed for the garbage
    collector of pypy.
    c         C@  s1   | |  _  | |  _ | |  _ d |  _ t |  _ d  S(   Ni    (   R
   t   sockett   suppress_ragged_eofst   _makefile_refsR   t   _closed(   t   selfR
   R>   R?   (    (    s   /private/var/folders/3n/6h2rwf155rn1m71wwyxn79n80000gn/T/pip-build-IcAT_k/pip/pip/_vendor/requests/packages/urllib3/contrib/pyopenssl.pyt   __init__   s
    				c         C@  s   |  j  j   S(   N(   R>   t   fileno(   RB   (    (    s   /private/var/folders/3n/6h2rwf155rn1m71wwyxn79n80000gn/T/pip-build-IcAT_k/pip/pip/_vendor/requests/packages/urllib3/contrib/pyopenssl.pyRD      s    c         C@  s;   |  j  d k r! |  j  d 8_  n  |  j r7 |  j   n  d  S(   Ni    i   (   R@   RA   t   close(   RB   (    (    s   /private/var/folders/3n/6h2rwf155rn1m71wwyxn79n80000gn/T/pip-build-IcAT_k/pip/pip/_vendor/requests/packages/urllib3/contrib/pyopenssl.pyt   _decref_socketios   s    	c         O@  s  y |  j  j | |   } Wn t j j k
 rb } |  j rM | j d k rM d St t |    n t j j	 k
 r } |  j  j
   t j j k r d S  np t j j k
 rt j |  j g g  g  |  j j    \ } } } | s t d   q|  j | |   Sn X| Sd  S(   Nis   Unexpected EOFt    s   The read operation timed out(   is   Unexpected EOF(   R
   t   recvt   OpenSSLt   SSLt   SysCallErrorR?   t   argst   SocketErrorR/   t   ZeroReturnErrort   get_shutdownt   RECEIVED_SHUTDOWNt   WantReadErrort   selectR>   t
   gettimeoutR   (   RB   RL   t   kwargst   datat   et   rdt   wdt   ed(    (    s   /private/var/folders/3n/6h2rwf155rn1m71wwyxn79n80000gn/T/pip-build-IcAT_k/pip/pip/_vendor/requests/packages/urllib3/contrib/pyopenssl.pyRH      s"    *c         O@  s
  y |  j  j | |   SWn t j j k
 r` } |  j rK | j d k rK d St t |    n t j j	 k
 r } |  j  j
   t j j k r d S  nl t j j k
 rt j |  j g g  g  |  j j    \ } } } | s t d   q|  j | |   Sn Xd  S(   Nis   Unexpected EOFi    s   The read operation timed out(   is   Unexpected EOF(   R
   t	   recv_intoRI   RJ   RK   R?   RL   RM   R/   RN   RO   RP   RQ   RR   R>   RS   R   (   RB   RL   RT   RV   RW   RX   RY   (    (    s   /private/var/folders/3n/6h2rwf155rn1m71wwyxn79n80000gn/T/pip-build-IcAT_k/pip/pip/_vendor/requests/packages/urllib3/contrib/pyopenssl.pyRZ      s     *c         C@  s   |  j  j |  S(   N(   R>   t
   settimeout(   RB   R   (    (    s   /private/var/folders/3n/6h2rwf155rn1m71wwyxn79n80000gn/T/pip-build-IcAT_k/pip/pip/_vendor/requests/packages/urllib3/contrib/pyopenssl.pyR[      s    c         C@  s   x} t  r y |  j j |  SWq t j j k
 r{ t j g  |  j g g  |  j j    \ } } } | s t	    q q q Xq Wd  S(   N(
   R   R
   t   sendRI   RJ   t   WantWriteErrorRR   R>   RS   R   (   RB   RU   t   _t   wlist(    (    s   /private/var/folders/3n/6h2rwf155rn1m71wwyxn79n80000gn/T/pip-build-IcAT_k/pip/pip/_vendor/requests/packages/urllib3/contrib/pyopenssl.pyt   _send_until_done   s    	c         C@  sG   d } x: | t  |  k  rB |  j | | | t ! } | | 7} q	 Wd  S(   Ni    (   R+   R`   t   SSL_WRITE_BLOCKSIZE(   RB   RU   t
   total_sentt   sent(    (    s   /private/var/folders/3n/6h2rwf155rn1m71wwyxn79n80000gn/T/pip-build-IcAT_k/pip/pip/_vendor/requests/packages/urllib3/contrib/pyopenssl.pyt   sendall   s    c         C@  s   |  j  j   d  S(   N(   R
   t   shutdown(   RB   (    (    s   /private/var/folders/3n/6h2rwf155rn1m71wwyxn79n80000gn/T/pip-build-IcAT_k/pip/pip/_vendor/requests/packages/urllib3/contrib/pyopenssl.pyRe      s    c         C@  sZ   |  j  d k  rG y t |  _ |  j j   SWqV t j j k
 rC d  SXn |  j  d 8_  d  S(   Ni   (   R@   R   RA   R
   RE   RI   RJ   t   Error(   RB   (    (    s   /private/var/folders/3n/6h2rwf155rn1m71wwyxn79n80000gn/T/pip-build-IcAT_k/pip/pip/_vendor/requests/packages/urllib3/contrib/pyopenssl.pyRE     s    	c         C@  s~   |  j  j   } | s | S| r8 t j j t j j |  Si d | j   j f f f d 6g  t |  D] } d | f ^ qd d 6S(   Nt
   commonNamet   subjectt   DNSR    (	   R
   t   get_peer_certificateRI   t   cryptot   dump_certificatet   FILETYPE_ASN1t   get_subjectt   CNR<   (   RB   t   binary_formt   x509t   value(    (    s   /private/var/folders/3n/6h2rwf155rn1m71wwyxn79n80000gn/T/pip-build-IcAT_k/pip/pip/_vendor/requests/packages/urllib3/contrib/pyopenssl.pyt   getpeercert  s    		c         C@  s   |  j  d 7_  d  S(   Ni   (   R@   (   RB   (    (    s   /private/var/folders/3n/6h2rwf155rn1m71wwyxn79n80000gn/T/pip-build-IcAT_k/pip/pip/_vendor/requests/packages/urllib3/contrib/pyopenssl.pyt   _reuse#  s    c         C@  s/   |  j  d k  r |  j   n |  j  d 8_  d  S(   Ni   (   R@   RE   (   RB   (    (    s   /private/var/folders/3n/6h2rwf155rn1m71wwyxn79n80000gn/T/pip-build-IcAT_k/pip/pip/_vendor/requests/packages/urllib3/contrib/pyopenssl.pyt   _drop&  s    (   R   R   R   R   RC   RD   RF   RH   RZ   R[   R`   Rd   Re   RE   R   Rs   Rt   Ru   (    (    (    s   /private/var/folders/3n/6h2rwf155rn1m71wwyxn79n80000gn/T/pip-build-IcAT_k/pip/pip/_vendor/requests/packages/urllib3/contrib/pyopenssl.pyR=      s   									
	ic         C@  s%   |  j  d 7_  t |  | | d t S(   Ni   RE   (   R@   R   R   (   RB   t   modet   bufsize(    (    s   /private/var/folders/3n/6h2rwf155rn1m71wwyxn79n80000gn/T/pip-build-IcAT_k/pip/pip/_vendor/requests/packages/urllib3/contrib/pyopenssl.pyt   makefile.  s    c         C@  s
   | d k S(   Ni    (    (   t   cnxRq   t   err_not	   err_deptht   return_code(    (    s   /private/var/folders/3n/6h2rwf155rn1m71wwyxn79n80000gn/T/pip-build-IcAT_k/pip/pip/_vendor/requests/packages/urllib3/contrib/pyopenssl.pyt   _verify_callback7  s    c         C@  s  t  j j t |  } | r8 | p% | } | j |  n  | rN | j |  n  | t j k rt | j t	 | t
  n  | s | r y | j | |  Wq t  j j k
 r }	 t j d | |	   q Xn
 | j   d }
 | j |
  | j t  t  j j | |   } t | t j  r,| j d  } n  | j |  | j   x t ry | j   Wn t  j j k
 rt j |  g g  g  |  j    \ } } } | sFt d   qFqFn, t  j j k
 r}	 t j d |	   n XPqFWt | |   S(   Ns   bad ca_certs: %ri   s   utf-8s   select timed outs   bad handshake: %r(    RI   RJ   t   Contextt   _openssl_versionst   use_certificate_filet   use_privatekey_filet   sslt	   CERT_NONEt
   set_verifyt   _openssl_verifyR}   t   load_verify_locationsRf   t   SSLErrort   set_default_verify_pathst   set_optionst   set_cipher_listt   DEFAULT_SSL_CIPHER_LISTt
   ConnectionR*   t   sixt	   text_typet   encodet   set_tlsext_host_namet   set_connect_stateR   t   do_handshakeRQ   RR   RS   R   R=   (   t   sockt   keyfilet   certfilet	   cert_reqst   ca_certst   server_hostnamet   ssl_versiont   ca_cert_dirt   ctxRV   t   OP_NO_COMPRESSIONRy   RW   R^   (    (    s   /private/var/folders/3n/6h2rwf155rn1m71wwyxn79n80000gn/T/pip-build-IcAT_k/pip/pip/_vendor/requests/packages/urllib3/contrib/pyopenssl.pyR   ;  sD    

	*(H   R   t
   __future__R    t%   ndg.httpsclient.ssl_peer_verificationR   t   ndg.httpsclient.subj_alt_nameR   t   BaseSubjectAltNamet   SyntaxErrorRV   t   ImportErrort   OpenSSL.SSLRI   t   pyasn1.codec.derR   R(   t   pyasn1.typeR   R   R>   R   R   RM   R   t   Nonet#   urllib3.packages.backports.makefileR	   R   RR   R   RG   R
   R   t   __all__R   RJ   t   SSLv23_METHODt   PROTOCOL_SSLv23t   TLSv1_METHODt   PROTOCOL_TLSv1R   t   hasattrR   R   R   R   t   updatet   SSLv3_METHODt   PROTOCOL_SSLv3t   AttributeErrort   VERIFY_NONER   t   VERIFY_PEERt   CERT_OPTIONALt   VERIFY_FAIL_IF_NO_PEER_CERTt   CERT_REQUIREDR   t   ssl_t   DEFAULT_CIPHERSR   R   Ra   R   R   R   R   R   R<   t   objectR=   Rx   R}   (    (    (    s   /private/var/folders/3n/6h2rwf155rn1m71wwyxn79n80000gn/T/pip-build-IcAT_k/pip/pip/_vendor/requests/packages/urllib3/contrib/pyopenssl.pyt   <module>-   sl   !!!									