ó
ñ1Mc           @   s\  d  Z  d d d d d g Z d d l j j Z d d l Z d d l m Z d d	 l m	 Z	 d d
 l
 m Z d d l Z d d l m Z m Z d d l m Z d „  Z d d e j j e j ƒ  d „ Z d d e j j e j ƒ  d „ Z d d e j j e d „ Z d d e j j d d „ Z d d d „  ƒ  YZ d d d „  ƒ  YZ d e f d „  ƒ  YZ d S(   sv   
Make Twisted use M2Crypto for SSL

Copyright (c) 2004-2007 Open Source Applications Foundation.
All rights reserved.
t
   connectSSLt
   connectTCPt	   listenSSLt	   listenTCPt   TLSProtocolWrapperiÿÿÿÿN(   t   ProtocolWrapper(   t   ITLSTransport(   t
   implements(   t   m2t   X509(   t   Checkerc         C   s   d S(   Ni   (    (   t   peerX509t   expectedHost(    (    sI   /usr/lib64/python2.7/site-packages/M2Crypto/SSL/TwistedProtocolWrapper.pyt"   _alwaysSucceedsPostConnectionCheck   s    i   c   	         s=   t  j | ƒ } ‡  ‡ f d †  | _ | j |  | | | | ƒ S(   s~   
    A convenience function to start an SSL/TLS connection using Twisted.
    
    See IReactorSSL interface in Twisted. 
    c            s%   t  |  | d d d d d ˆ  d ˆ ƒS(   Nt   startPassThroughi    t   clienti   t   contextFactoryt   postConnectionCheck(   R   (   t   factoryt   wrappedProtocol(   R   R   (    sI   /usr/lib64/python2.7/site-packages/M2Crypto/SSL/TwistedProtocolWrapper.pyt   <lambda>$   s    (   t   policiest   WrappingFactoryt   protocolR   (	   t   hostt   portR   R   t   timeoutt   bindAddresst   reactorR   t   wrappingFactory(    (   R   R   sI   /usr/lib64/python2.7/site-packages/M2Crypto/SSL/TwistedProtocolWrapper.pyR       s    	c            s:   t  j | ƒ } ‡  f d †  | _ | j |  | | | | ƒ S(   s¶   
    A convenience function to start a TCP connection using Twisted. 

    NOTE: You must call startTLS(ctx) to go into SSL/TLS mode.

    See IReactorTCP interface in Twisted. 
    c            s%   t  |  | d d d d d d  d ˆ  ƒS(   NR   i   R   R   R   (   R   t   None(   R   R   (   R   (    sI   /usr/lib64/python2.7/site-packages/M2Crypto/SSL/TwistedProtocolWrapper.pyR   9   s    (   R   R   R   R   (   R   R   R   R   R   R   R   R   (    (   R   sI   /usr/lib64/python2.7/site-packages/M2Crypto/SSL/TwistedProtocolWrapper.pyR   .   s    
i   t    c            s:   t  j | ƒ } ‡  ‡ f d †  | _ | j |  | | | ƒ S(   s~   
    A convenience function to listen for SSL/TLS connections using Twisted. 

    See IReactorSSL interface in Twisted. 
    c            s%   t  |  | d d d d d ˆ  d ˆ ƒS(   NR   i    R   R   R   (   R   (   R   R   (   R   R   (    sI   /usr/lib64/python2.7/site-packages/M2Crypto/SSL/TwistedProtocolWrapper.pyR   L   s    (   R   R   R   R   (   R   R   R   t   backlogt	   interfaceR   R   R   (    (   R   R   sI   /usr/lib64/python2.7/site-packages/M2Crypto/SSL/TwistedProtocolWrapper.pyR   C   s    c            s7   t  j | ƒ } ‡  f d †  | _ | j |  | | | ƒ S(   s¾   
    A convenience function to listen for TCP connections using Twisted. 
    
    NOTE: You must call startTLS(ctx) to go into SSL/TLS mode.

    See IReactorTCP interface in Twisted. 
    c            s%   t  |  | d d d d d d  d ˆ  ƒS(   NR   i   R   i    R   R   (   R   R   (   R   R   (   R   (    sI   /usr/lib64/python2.7/site-packages/M2Crypto/SSL/TwistedProtocolWrapper.pyR   a   s    (   R   R   R   R   (   R   R   R    R!   R   R   R   (    (   R   sI   /usr/lib64/python2.7/site-packages/M2Crypto/SSL/TwistedProtocolWrapper.pyR   V   s    
t	   _BioProxyc           B   s2   e  Z d  Z e j Z d „  Z d „  Z d „  Z RS(   s   
    The purpose of this class is to eliminate the __del__ method from
    TLSProtocolWrapper, and thus letting it be garbage collected.
    c         C   s   | |  _  d  S(   N(   t   bio(   t   selfR#   (    (    sI   /usr/lib64/python2.7/site-packages/M2Crypto/SSL/TwistedProtocolWrapper.pyt   __init__s   s    c         C   s   |  j  S(   N(   R#   (   R$   (    (    sI   /usr/lib64/python2.7/site-packages/M2Crypto/SSL/TwistedProtocolWrapper.pyt   _ptrv   s    c         C   s&   |  j  d  k	 r" |  j |  j  ƒ n  d  S(   N(   R#   R   t   m2_bio_free_all(   R$   (    (    sI   /usr/lib64/python2.7/site-packages/M2Crypto/SSL/TwistedProtocolWrapper.pyt   __del__y   s    (	   t   __name__t
   __module__t   __doc__R   t   bio_free_allR'   R%   R&   R(   (    (    (    sI   /usr/lib64/python2.7/site-packages/M2Crypto/SSL/TwistedProtocolWrapper.pyR"   k   s
   			t	   _SSLProxyc           B   s2   e  Z d  Z e j Z d „  Z d „  Z d „  Z RS(   s   
    The purpose of this class is to eliminate the __del__ method from
    TLSProtocolWrapper, and thus letting it be garbage collected.
    c         C   s   | |  _  d  S(   N(   t   ssl(   R$   R.   (    (    sI   /usr/lib64/python2.7/site-packages/M2Crypto/SSL/TwistedProtocolWrapper.pyR%   †   s    c         C   s   |  j  S(   N(   R.   (   R$   (    (    sI   /usr/lib64/python2.7/site-packages/M2Crypto/SSL/TwistedProtocolWrapper.pyR&   ‰   s    c         C   s&   |  j  d  k	 r" |  j |  j  ƒ n  d  S(   N(   R.   R   t   m2_ssl_free(   R$   (    (    sI   /usr/lib64/python2.7/site-packages/M2Crypto/SSL/TwistedProtocolWrapper.pyR(   Œ   s    (	   R)   R*   R+   R   t   ssl_freeR/   R%   R&   R(   (    (    (    sI   /usr/lib64/python2.7/site-packages/M2Crypto/SSL/TwistedProtocolWrapper.pyR-   ~   s
   			c           B   s–   e  Z d  Z e e ƒ d „  Z d „  Z d „  Z d „  Z d „  Z	 d „  Z
 d „  Z d „  Z d	 „  Z d
 „  Z d „  Z d d d „ Z d d „ Z RS(   sä   
    A SSL/TLS protocol wrapper to be used with Twisted. Typically
    you would not use this class directly. Use connectTCP, 
    connectSSL, listenTCP, listenSSL functions defined above,
    which will hook in this class.
    c         C   s‰   | |  _  | |  _ d |  _ d |  _ d |  _ d |  _ | |  _ d |  _ | d k r` t	 |  _
 n	 | |  _
 | s… |  j | j ƒ  ƒ n  d S(   sŽ  
        @param factory:
        @param wrappedProtocol:
        @param startPassThrough:    If true we won't encrypt at all. Need to
                                    call startTLS() later to switch to SSL/TLS.
        @param client:              True if this should be a client protocol.
        @param contextFactory:      Factory that creates SSL.Context objects.
                                    The called function is getContext().
        @param postConnectionCheck: The post connection check callback that
                                    will be called just after connection has
                                    been established but before any real data
                                    has been exchanged. The first argument to
                                    this function is an X509 object, the second
                                    is the expected host name string.
        R   i    N(   R   R   t   datat	   encryptedt
   tlsStartedt   checkedt   isClientt	   helloDoneR   R   R   t   startTLSt
   getContext(   R$   R   R   R   R   R   R   (    (    sI   /usr/lib64/python2.7/site-packages/M2Crypto/SSL/TwistedProtocolWrapper.pyR%   ›   s    									c         C   ss   t  |  d d ƒ r9 d |  _ d |  _ d |  _ d |  _ n  d |  _ d |  _ d |  _ d |  _	 d |  _
 d |  _ d S(   sI   
        Clear this instance, after which it is ready for reuse.
        R3   i    R   i   N(   t   getattrR   t   sslBioR.   t   internalBiot
   networkBioR1   R2   R3   R4   R5   R6   (   R$   (    (    sI   /usr/lib64/python2.7/site-packages/M2Crypto/SSL/TwistedProtocolWrapper.pyt   clearÇ   s    								c         C   s¦  |  j  r t d ‚ n  | |  _ t j t j ƒ  ƒ |  _ t j |  j d ƒ t t j t j ƒ  ƒ ƒ |  _	 t j |  j	 j
 ƒ  d ƒ t j |  j |  j	 j
 ƒ  ƒ t t j t j ƒ  ƒ ƒ |  _ t t j |  j j ƒ ƒ |  _ |  j rú t j |  j j
 ƒ  ƒ n t j |  j j
 ƒ  ƒ t j |  j j
 ƒ  |  j |  j ƒ t j |  j j
 ƒ  |  j j
 ƒ  t j ƒ t j |  j j
 ƒ  ƒ } t j |  j j
 ƒ  | t j Bt j Bƒ d |  _  d S(   sq   
        Start SSL/TLS. If this is not called, this instance just passes data
        through untouched.
        s   TLS already startedi    i   N(   R3   t	   Exceptiont   ctxR   t   bio_newt	   bio_s_bioR;   t   bio_set_write_buf_sizeR"   R<   R&   t   bio_make_bio_pairt	   bio_f_sslR:   R-   t   ssl_newR.   R5   t   ssl_set_connect_statet   ssl_set_accept_statet   ssl_set_biot   bio_set_sslt   bio_nocloset   ssl_get_modet   ssl_set_modet   SSL_MODE_ENABLE_PARTIAL_WRITEt#   SSL_MODE_ACCEPT_MOVING_WRITE_BUFFER(   R$   R?   t   mode(    (    sI   /usr/lib64/python2.7/site-packages/M2Crypto/SSL/TwistedProtocolWrapper.pyR7   Ù   s(    			"(
c         C   s—   |  j  s t j |  | ƒ d  Sy, |  j | ƒ } t j |  | ƒ d |  _ WnG t j j k
 r’ } t j	 |  j
 j ƒ  ƒ | j d f | _ | ‚ n Xd  S(   Ni   i    (   R3   R   t   writet   _encryptR6   t   M2Cryptot   BIOt   BIOErrorR   t   ssl_get_verify_resultR.   R&   t   args(   R$   R1   t   encryptedDatat   e(    (    sI   /usr/lib64/python2.7/site-packages/M2Crypto/SSL/TwistedProtocolWrapper.pyRP     s    	(c         C   s@   |  j  s& t j |  d j | ƒ ƒ d  S|  j d j | ƒ ƒ d  S(   NR   (   R3   R   t   writeSequencet   joinRP   (   R$   R1   (    (    sI   /usr/lib64/python2.7/site-packages/M2Crypto/SSL/TwistedProtocolWrapper.pyRY     s    	c         C   s   t  j |  ƒ d  S(   N(   R   t   loseConnection(   R$   (    (    sI   /usr/lib64/python2.7/site-packages/M2Crypto/SSL/TwistedProtocolWrapper.pyR[     s    c         C   s:   t  j |  ƒ |  j r6 |  j r6 |  j r6 |  j ƒ  n  d  S(   N(   R   t   connectionMadeR3   R5   R6   t   _clientHello(   R$   (    (    sI   /usr/lib64/python2.7/site-packages/M2Crypto/SSL/TwistedProtocolWrapper.pyR\     s    c         C   sâ   |  j  s t j |  | ƒ d  S|  j | 7_ yh xa |  j ƒ  } |  j ƒ  |  j ƒ  } t j |  | ƒ t j |  | ƒ | d k r2 | d k r2 Pq2 q2 WnG t j	 j
 k
 rÝ } t j |  j j ƒ  ƒ | j d f | _ | ‚ n Xd  S(   NR   i    (   R3   R   t   dataReceivedR2   t   _decryptt   _checkRQ   RP   RR   RS   RT   R   RU   R.   R&   RV   (   R$   R1   t   decryptedDataRW   RX   (    (    sI   /usr/lib64/python2.7/site-packages/M2Crypto/SSL/TwistedProtocolWrapper.pyR^      s     	
(c         C   s   |  j  ƒ  t j |  | ƒ d  S(   N(   R=   R   t   connectionLost(   R$   t   reason(    (    sI   /usr/lib64/python2.7/site-packages/M2Crypto/SSL/TwistedProtocolWrapper.pyRb   :  s    
c         C   sº   |  j  r¶ t j |  j j ƒ  ƒ r¶ t j |  j j ƒ  ƒ } | d  k	 r[ t j | d ƒ } n  |  j rw |  j	 j
 d } n |  j	 j ƒ  j } |  j | | ƒ sª t j d ‚ n  d |  _  n  d  S(   Ni   i    s   post connection check(   R4   R   t   ssl_is_init_finishedR.   R&   t   ssl_get_peer_certR   R	   R5   t	   transportt   addrt   getPeerR   R   R
   t   SSLVerificationError(   R$   t   x509R   (    (    sI   /usr/lib64/python2.7/site-packages/M2Crypto/SSL/TwistedProtocolWrapper.pyR`   >  s    "	c         C   s}   y/ |  j  d d ƒ } t j |  | ƒ d |  _ WnG t j j k
 rx } t j |  j	 j
 ƒ  ƒ | j d f | _ | ‚ n Xd  S(   Nt   clientHelloi   i    (   RQ   R   RP   R6   RR   RS   RT   R   RU   R.   R&   RV   (   R$   RW   RX   (    (    sI   /usr/lib64/python2.7/site-packages/M2Crypto/SSL/TwistedProtocolWrapper.pyR]   K  s    (R   i    c         C   s	  d } |  j  | 7_  |  j j ƒ  } |  j j ƒ  } t j } t j } t j } t j }	 t j	 }
 x¢ | | ƒ } | d k rŠ |  j  d k s | rÄ | | |  j  ƒ } | d k r± qÄ |  j  | |  _  n  |	 | ƒ } | r|
 | | ƒ } | d  k	 r| | 7} qqc Pqc | S(   NR   i    (   R1   R:   R&   R<   R   t   bio_ctrl_get_write_guaranteet	   bio_writet   bio_should_retryt   bio_ctrl_pendingt   bio_readR   (   R$   R1   Rk   RW   t	   sslBioPtrR<   t   m2bio_ctrl_get_write_guaranteet   m2bio_writet   m2bio_should_retryt   m2bio_ctrl_pendingt
   m2bio_readt   gt   rt   pendingt   d(    (    sI   /usr/lib64/python2.7/site-packages/M2Crypto/SSL/TwistedProtocolWrapper.pyRQ   X  s0    					!c         C   s  |  j  | 7_  d } |  j j ƒ  } |  j j ƒ  } t j } t j } t j } t j } t j	 }	 xœ | | ƒ }
 |
 d k r¾ |  j  d k r¾ | | |  j  ƒ } | d k r« q¾ |  j  | |  _  n  | | ƒ } | rû |	 | | ƒ } | d  k	 rü | | 7} qü qc Pqc | S(   NR   i    (   R2   R:   R&   R<   R   Rl   Rm   Rn   Ro   Rp   R   (   R$   R1   Ra   Rq   R<   Rr   Rs   Rt   Ru   Rv   Rw   Rx   Ry   Rz   (    (    sI   /usr/lib64/python2.7/site-packages/M2Crypto/SSL/TwistedProtocolWrapper.pyR_   z  s0    					(   R)   R*   R+   R   R   R%   R=   R7   RP   RY   R[   R\   R^   Rb   R`   R]   RQ   R_   (    (    (    sI   /usr/lib64/python2.7/site-packages/M2Crypto/SSL/TwistedProtocolWrapper.pyR   ‘   s   
	,		(								"(    (    (   R+   t   __all__t   twisted.protocols.policiest	   protocolsR   t   twisted.internet.reactort   twistedR   t   twisted.internet.interfacesR   t   zope.interfaceR   RR   R   R	   t   M2Crypto.SSLR
   R   R   t   internetR   R    R   R   R   R"   R-   R   (    (    (    sI   /usr/lib64/python2.7/site-packages/M2Crypto/SSL/TwistedProtocolWrapper.pyt   <module>   s6   						