ó
^\kdc           @   sÝ  d  Z  d d l m Z d d l m Z d d l Z d d l m	 Z
 d d l m Z m Z d d l m Z d d l Z d d l Z d d l m 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 6e j j e j 6Z  i e j j! e j" 6e j j# e j$ 6e j j# e j j% e j& 6Z' d d d Z( e j Z) e j* Z+ d „  Z, d „  Z- d e f d „  ƒ  YZ d „  Z. d e f d „  ƒ  YZ/ d e0 f d „  ƒ  YZ1 d „  Z2 d d d d d d d „ Z* d S(   sQ  SSL with SNI_-support for Python 2.

This needs the following packages installed:

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

To activate it call :func:`~urllib3.contrib.pyopenssl.inject_into_urllib3`.
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
encryption 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.
    Default: ``EECDH+ECDSA+AESGCM EECDH+aRSA+AESGCM EECDH+ECDSA+SHA256
    EECDH+aRSA+SHA256 EECDH+aRSA+RC4 EDH+aRSA EECDH RC4 !aNULL !eNULL !LOW !3DES
    !MD5 !EXP !PSK !SRP !DSS'``

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

iÿÿÿÿ(   t   SUBJ_ALT_NAME_SUPPORT(   t   SubjectAltNameN(   t   decoder(   t   univt
   constraint(   t   _fileobject(   t   StringIOi   (   t
   connection(   t   utilt   inject_into_urllib3t   extract_from_urllib3s%   EECDH+ECDSA+AESGCM EECDH+aRSA+AESGCM s=   EECDH+ECDSA+SHA256 EECDH+aRSA+SHA256 EECDH+aRSA+RC4 EDH+aRSA s;   EECDH RC4 !aNULL !eNULL !LOW !3DES !MD5 !EXP !PSK !SRP !DSSc           C   s   t  t _  t t _ d S(   s7   Monkey-patch urllib3 with PyOpenSSL-backed SSL-support.N(   t   ssl_wrap_socketR   t   HAS_SNIR   (    (    (    s^   /var/www/testScraper/venv/build/pip/pip/_vendor/requests/packages/urllib3/contrib/pyopenssl.pyR	   T   s    	c           C   s   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   (    (    (    s^   /var/www/testScraper/venv/build/pip/pip/_vendor/requests/packages/urllib3/contrib/pyopenssl.pyR
   [   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^   /var/www/testScraper/venv/build/pip/pip/_vendor/requests/packages/urllib3/contrib/pyopenssl.pyR   c   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^   /var/www/testScraper/venv/build/pip/pip/_vendor/requests/packages/urllib3/contrib/pyopenssl.pyt   get_subj_alt_namen   s*    		%t
   fileobjectc           B   s    e  Z d  d „ Z d  d „ Z RS(   iÿÿÿÿc   	      C   sî  t  |  j |  j ƒ } |  j } | j d d ƒ | d k  r§ t ƒ  |  _ xT t rœ y |  j j | ƒ } Wn t	 j
 j k
 r� qI n X| sŒ Pn  | j | ƒ qI W| j ƒ  S| j ƒ  } | | k r| j d ƒ | j | ƒ } t ƒ  |  _ |  j j | j ƒ  ƒ | St ƒ  |  _ xÐ t rß| | } y |  j j | ƒ } Wn t	 j
 j k
 rRqn X| s]Pn  t | ƒ } | | k r€| r€| S| | k r | j | ƒ ~ Pn  | | k sÂt d | | f ƒ ‚ | j | ƒ | | 7} ~ qW| j ƒ  Sd  S(   Ni    i   s   recv(%d) returned %d bytes(   t   maxt	   _rbufsizet   default_bufsizet   _rbuft   seekR   t   Truet   _sockt   recvt   OpenSSLt   SSLt   WantReadErrort   writet   getvaluet   tellt   readR    t   AssertionError(	   t   selft   sizet   rbufsizet   buft   datat   buf_lent   rvt   leftt   n(    (    s^   /var/www/testScraper/venv/build/pip/pip/_vendor/requests/packages/urllib3/contrib/pyopenssl.pyRA   Ž   sT    		
	
"
c         C   sÛ  |  j  } | j d d ƒ | j ƒ  d k r” | j d ƒ | j | ƒ } | j d ƒ sh t | ƒ | k rŽ t ƒ  |  _  |  j  j | j ƒ  ƒ | S~ n  | d k  r,|  j	 d k r[| j d ƒ | j ƒ  g } t ƒ  |  _  d  } |  j j } xb t rMy: x3 | d k r*| d ƒ } | sPn  | j | ƒ qø WWn t j j k
 rHqì n XPqì Wd j | ƒ S| j d d ƒ t ƒ  |  _  x¨ t r!y |  j j |  j	 ƒ } Wn t j j k
 rµqzn X| sÀPn  | j d ƒ } | d k r| d 7} | j | |  ƒ |  j  j | | ƒ ~ Pn  | j | ƒ qzW| j ƒ  S| j d d ƒ | j ƒ  } | | k r–| j d ƒ | j | ƒ }	 t ƒ  |  _  |  j  j | j ƒ  ƒ |	 St ƒ  |  _  x(t rÌy |  j j |  j	 ƒ } Wn t j j k
 ràq¥n X| sëPn  | | }
 | j d d |
 ƒ } | d k rZ| d 7} |  j  j | | ƒ | rO| j | |  ƒ PqZ| |  Sn  t | ƒ } | | k r}| r}| S| |
 k r²| j | |
  ƒ |  j  j | |
 ƒ Pn  | j | ƒ | | 7} q¥W| j ƒ  Sd  S(   Ni    i   s   
i   t    (   R6   R7   R@   t   readlinet   endswithR    R   R>   RA   R4   t   NoneR9   R:   R8   R#   R;   R<   R=   t   joint   findR?   (   RC   RD   RF   t   blinet   buffersRG   R:   t   nlRH   RI   RJ   RK   (    (    s^   /var/www/testScraper/venv/build/pip/pip/_vendor/requests/packages/urllib3/contrib/pyopenssl.pyRM   Ð   s¢    	!		

	

(   R   R   RA   RM   (    (    (    s^   /var/www/testScraper/venv/build/pip/pip/_vendor/requests/packages/urllib3/contrib/pyopenssl.pyR2   Œ   s   Bt   WrappedSocketc           B   sS   e  Z d  Z d „  Z d „  Z d d „ Z d „  Z d „  Z d „  Z e	 d „ Z
 RS(	   s@   API-compatibility wrapper for Python OpenSSL's Connection-class.c         C   s   | |  _  | |  _ d  S(   N(   R   t   socket(   RC   R   RV   (    (    s^   /var/www/testScraper/venv/build/pip/pip/_vendor/requests/packages/urllib3/contrib/pyopenssl.pyt   __init__5  s    	c         C   s   |  j  j ƒ  S(   N(   RV   t   fileno(   RC   (    (    s^   /var/www/testScraper/venv/build/pip/pip/_vendor/requests/packages/urllib3/contrib/pyopenssl.pyRX   9  s    iÿÿÿÿc         C   s   t  |  j | | ƒ S(   N(   R2   R   (   RC   t   modet   bufsize(    (    s^   /var/www/testScraper/venv/build/pip/pip/_vendor/requests/packages/urllib3/contrib/pyopenssl.pyt   makefile<  s    c         C   s   |  j  j | ƒ S(   N(   RV   t
   settimeout(   RC   t   timeout(    (    s^   /var/www/testScraper/venv/build/pip/pip/_vendor/requests/packages/urllib3/contrib/pyopenssl.pyR\   ?  s    c         C   s   |  j  j | ƒ S(   N(   R   t   sendall(   RC   RG   (    (    s^   /var/www/testScraper/venv/build/pip/pip/_vendor/requests/packages/urllib3/contrib/pyopenssl.pyR^   B  s    c         C   s   |  j  j ƒ  S(   N(   R   t   shutdown(   RC   (    (    s^   /var/www/testScraper/venv/build/pip/pip/_vendor/requests/packages/urllib3/contrib/pyopenssl.pyt   closeE  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_certificateR;   t   cryptot   dump_certificatet   FILETYPE_ASN1t   get_subjectt   CNR1   (   RC   t   binary_formt   x509t   value(    (    s^   /var/www/testScraper/venv/build/pip/pip/_vendor/requests/packages/urllib3/contrib/pyopenssl.pyt   getpeercertH  s    		(   R   R   R   RW   RX   R[   R\   R^   R`   t   FalseRm   (    (    (    s^   /var/www/testScraper/venv/build/pip/pip/_vendor/requests/packages/urllib3/contrib/pyopenssl.pyRU   2  s   					c         C   s
   | d k S(   Ni    (    (   t   cnxRk   t   err_not	   err_deptht   return_code(    (    s^   /var/www/testScraper/venv/build/pip/pip/_vendor/requests/packages/urllib3/contrib/pyopenssl.pyt   _verify_callback^  s    c         C   s‰  t  j j t | ƒ } | r, | j | ƒ n  | rB | j | ƒ n  | t j k rh | j t	 | t
 ƒ n  | r· y | j | d  ƒ Wq· t  j j k
 r³ } t j d | | ƒ ‚ q· Xn  d }	 | j |	 ƒ | j t ƒ t  j j | |  ƒ }
 |
 j | ƒ |
 j ƒ  xv t r{y |
 j ƒ  WnZ t  j j k
 rLt j |  g g  g  ƒ qn+ t  j j k
 rv} t j d | ƒ ‚ n XPqWt |
 |  ƒ S(   Ns   bad ca_certs: %ri   s   bad handshake(   R;   R<   t   Contextt   _openssl_versionst   use_certificate_filet   use_privatekey_filet   sslt	   CERT_NONEt
   set_verifyt   _openssl_verifyRs   t   load_verify_locationsRO   t   Errort   SSLErrort   set_optionst   set_cipher_listt   DEFAULT_SSL_CIPHER_LISTt
   Connectiont   set_tlsext_host_namet   set_connect_stateR8   t   do_handshakeR=   t   selectRU   (   t   sockt   keyfilet   certfilet	   cert_reqst   ca_certst   server_hostnamet   ssl_versiont   ctxt   et   OP_NO_COMPRESSIONRo   (    (    s^   /var/www/testScraper/venv/build/pip/pip/_vendor/requests/packages/urllib3/contrib/pyopenssl.pyR   b  s8    
	(4   R   t%   ndg.httpsclient.ssl_peer_verificationR    t   ndg.httpsclient.subj_alt_nameR   t   BaseSubjectAltNamet   OpenSSL.SSLR;   t   pyasn1.codec.derR   R   t   pyasn1.typeR   R   RV   R   Rx   R†   t	   cStringIOR   RL   R   R   t   __all__R   R<   t   SSLv23_METHODt   PROTOCOL_SSLv23t   SSLv3_METHODt   PROTOCOL_SSLv3t   TLSv1_METHODt   PROTOCOL_TLSv1Ru   t   VERIFY_NONERy   t   VERIFY_PEERt   CERT_OPTIONALt   VERIFY_FAIL_IF_NO_PEER_CERTt   CERT_REQUIREDR{   R�   R   R   R   R	   R
   R1   R2   t   objectRU   Rs   RO   (    (    (    s^   /var/www/testScraper/venv/build/pip/pip/_vendor/requests/packages/urllib3/contrib/pyopenssl.pyt   <module>'   sH   						¦,		