ó
ú—7Tc           @   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 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 „  ƒ  YZ0 d e1 f d „  ƒ  YZ2 d „  Z3 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
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: ``ECDH+AESGCM:DH+AESGCM:ECDH+AES256:DH+AES256:ECDH+AES128:DH+AES:
    ECDH+3DES:DH+3DES:RSA+AESGCM:RSA+AES:RSA+3DES:!aNULL:!MD5:!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   _fileobjectt   timeout(   t   StringIOi   (   t
   connection(   t   utilt   inject_into_urllib3t   extract_from_urllib3s,   ECDH+AESGCM:DH+AESGCM:ECDH+AES256:DH+AES256:sA   ECDH+AES128:DH+AES:ECDH+3DES:DH+3DES:RSA+AESGCM:RSA+AES:RSA+3DES:s   !aNULL:!MD5:!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	   (    (    (    sf   /var/www/otrsdash.zarafa.com/venv/build/pip/pip/_vendor/requests/packages/urllib3/contrib/pyopenssl.pyR
   f   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   (    (    (    sf   /var/www/otrsdash.zarafa.com/venv/build/pip/pip/_vendor/requests/packages/urllib3/contrib/pyopenssl.pyR   m   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(    (    (    sf   /var/www/otrsdash.zarafa.com/venv/build/pip/pip/_vendor/requests/packages/urllib3/contrib/pyopenssl.pyR   u   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(    (    sf   /var/www/otrsdash.zarafa.com/venv/build/pip/pip/_vendor/requests/packages/urllib3/contrib/pyopenssl.pyt   get_subj_alt_name€   s*    		%t
   fileobjectc           B   s)   e  Z d  „  Z d d „ Z d d „ Z RS(   c         C   sF   t  j  |  j g g  g  |  j j ƒ  ƒ \ } } } | sB t ƒ  ‚ n  d  S(   N(   t   selectt   _sockt
   gettimeoutR   (   t   selft   rdt   wdt   ed(    (    sf   /var/www/otrsdash.zarafa.com/venv/build/pip/pip/_vendor/requests/packages/urllib3/contrib/pyopenssl.pyt   _wait_for_sock    s    iÿÿÿÿc   	      C   s  t  |  j |  j ƒ } |  j } | j d d ƒ | d k  r± t ƒ  |  _ x^ t r¦ y |  j j | ƒ } Wn$ t	 j
 j k
 r‹ |  j ƒ  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
 rf|  j ƒ  qn X| sq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   TrueR5   t   recvt   OpenSSLt   SSLt   WantReadErrorR;   t   writet   getvaluet   tellt   readR!   t   AssertionError(	   R7   t   sizet   rbufsizet   buft   datat   buf_lent   rvt   leftt   n(    (    sf   /var/www/otrsdash.zarafa.com/venv/build/pip/pip/_vendor/requests/packages/urllib3/contrib/pyopenssl.pyRI   §   sX    		

	

"
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 re| j d ƒ | j ƒ  g } t ƒ  |  _  d  } |  j j } xl t rWy: x3 | d k r*| d ƒ } | sPn  | j | ƒ qø WWn$ t j j k
 rR|  j ƒ  qì n XPqì Wd j | ƒ S| j d d ƒ t ƒ  |  _  x² t r5y |  j j |  j	 ƒ } Wn$ t j j k
 rÉ|  j ƒ  q„n X| sÔPn  | j d ƒ } | d k r%| d 7} | j | |  ƒ |  j  j | | ƒ ~ Pn  | j | ƒ q„W| j ƒ  S| j d d ƒ | j ƒ  } | | k rª| j d ƒ | j | ƒ }	 t ƒ  |  _  |  j  j | j ƒ  ƒ |	 St ƒ  |  _  x2t rêy |  j j |  j	 ƒ } Wn$ t j j k
 rþ|  j ƒ  q¹n X| s	Pn  | | }
 | j d d |
 ƒ } | d k rx| d 7} |  j  j | | ƒ | rm| j | |  ƒ Pqx| |  Sn  t | ƒ } | | k r›| r›| S| |
 k rÐ| j | |
  ƒ |  j  j | |
 ƒ Pn  | j | ƒ | | 7} q¹W| j ƒ  Sd  S(   Ni    i   s   
i   t    (   R?   R@   RH   t   readlinet   endswithR!   R   RF   RI   R=   t   NoneR5   RB   RA   R$   RC   RD   RE   R;   t   joint   findRG   (   R7   RK   RM   t   blinet   buffersRN   RB   t   nlRO   RP   RQ   RR   (    (    sf   /var/www/otrsdash.zarafa.com/venv/build/pip/pip/_vendor/requests/packages/urllib3/contrib/pyopenssl.pyRT   ë   s¨    	!	
	


	


(   R   R   R;   RI   RT   (    (    (    sf   /var/www/otrsdash.zarafa.com/venv/build/pip/pip/_vendor/requests/packages/urllib3/contrib/pyopenssl.pyR3   ž   s   	Dt   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(   R7   R   R]   (    (    sf   /var/www/otrsdash.zarafa.com/venv/build/pip/pip/_vendor/requests/packages/urllib3/contrib/pyopenssl.pyt   __init__S  s    	c         C   s   |  j  j ƒ  S(   N(   R]   t   fileno(   R7   (    (    sf   /var/www/otrsdash.zarafa.com/venv/build/pip/pip/_vendor/requests/packages/urllib3/contrib/pyopenssl.pyR_   W  s    iÿÿÿÿc         C   s   t  |  j | | ƒ S(   N(   R3   R   (   R7   t   modet   bufsize(    (    sf   /var/www/otrsdash.zarafa.com/venv/build/pip/pip/_vendor/requests/packages/urllib3/contrib/pyopenssl.pyt   makefileZ  s    c         C   s   |  j  j | ƒ S(   N(   R]   t
   settimeout(   R7   R   (    (    sf   /var/www/otrsdash.zarafa.com/venv/build/pip/pip/_vendor/requests/packages/urllib3/contrib/pyopenssl.pyRc   ]  s    c         C   s   |  j  j | ƒ S(   N(   R   t   sendall(   R7   RN   (    (    sf   /var/www/otrsdash.zarafa.com/venv/build/pip/pip/_vendor/requests/packages/urllib3/contrib/pyopenssl.pyRd   `  s    c         C   s   |  j  j ƒ  S(   N(   R   t   shutdown(   R7   (    (    sf   /var/www/otrsdash.zarafa.com/venv/build/pip/pip/_vendor/requests/packages/urllib3/contrib/pyopenssl.pyt   closec  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_certificateRC   t   cryptot   dump_certificatet   FILETYPE_ASN1t   get_subjectt   CNR2   (   R7   t   binary_formt   x509t   value(    (    sf   /var/www/otrsdash.zarafa.com/venv/build/pip/pip/_vendor/requests/packages/urllib3/contrib/pyopenssl.pyt   getpeercertf  s    		(   R   R   R   R^   R_   Rb   Rc   Rd   Rf   t   FalseRs   (    (    (    sf   /var/www/otrsdash.zarafa.com/venv/build/pip/pip/_vendor/requests/packages/urllib3/contrib/pyopenssl.pyR\   P  s   					c         C   s
   | d k S(   Ni    (    (   t   cnxRq   t   err_not	   err_deptht   return_code(    (    sf   /var/www/otrsdash.zarafa.com/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
 | j ƒ  d }	 | j |	 ƒ | j t ƒ t  j j | |  ƒ }
 |
 j | ƒ |
 j ƒ  xv t r…y |
 j ƒ  WnZ t  j j k
 rVt j |  g g  g  ƒ qn+ t  j j k
 r€} t j d | ƒ ‚ n XPqWt |
 |  ƒ S(   Ns   bad ca_certs: %ri   s   bad handshake(   RC   RD   t   Contextt   _openssl_versionst   use_certificate_filet   use_privatekey_filet   sslt	   CERT_NONEt
   set_verifyt   _openssl_verifyRy   t   load_verify_locationsRV   t   Errort   SSLErrort   set_default_verify_pathst   set_optionst   set_cipher_listt   DEFAULT_SSL_CIPHER_LISTt
   Connectiont   set_tlsext_host_namet   set_connect_stateRA   t   do_handshakeRE   R4   R\   (   t   sockt   keyfilet   certfilet	   cert_reqst   ca_certst   server_hostnamet   ssl_versiont   ctxt   et   OP_NO_COMPRESSIONRu   (    (    sf   /var/www/otrsdash.zarafa.com/venv/build/pip/pip/_vendor/requests/packages/urllib3/contrib/pyopenssl.pyR   €  s:    

	(5   R   t%   ndg.httpsclient.ssl_peer_verificationR    t   ndg.httpsclient.subj_alt_nameR   t   BaseSubjectAltNamet   OpenSSL.SSLRC   t   pyasn1.codec.derR   R   t   pyasn1.typeR   R   R]   R   R   R~   R4   t	   cStringIOR   RS   R   R	   t   __all__R   RD   t   SSLv23_METHODt   PROTOCOL_SSLv23t   SSLv3_METHODt   PROTOCOL_SSLv3t   TLSv1_METHODt   PROTOCOL_TLSv1R{   t   VERIFY_NONER   t   VERIFY_PEERt   CERT_OPTIONALt   VERIFY_FAIL_IF_NO_PEER_CERTt   CERT_REQUIREDR   Rˆ   R   R   R   R
   R   R2   R3   t   objectR\   Ry   RV   (    (    (    sf   /var/www/otrsdash.zarafa.com/venv/build/pip/pip/_vendor/requests/packages/urllib3/contrib/pyopenssl.pyt   <module>/   sH   						²,		