
z\Vc           @   s   d  Z  d d g Z d d l m Z d d l Z d d l m Z m Z m Z m	 Z	 m
 Z
 m Z d d d     YZ d	   Z d d d
     YZ d S(   sI   SSL Context

Copyright (c) 1999-2004 Ng Pheng Siong. All rights reserved.t   mapt   Contexti(   t   WeakValueDictionaryN(   t   utilt   BIOt   Errt   RSAt   m2t   X509t   _ctxmapc           B   s2   e  Z d Z d    Z d   Z d   Z d   Z RS(   c         C   s   t    |  _ d  S(   N(   R   R    (   t   self(    (    s:   /usr/lib64/python2.7/site-packages/M2Crypto/SSL/Context.pyt   __init__   s    c         C   s   |  j  | S(   N(   R    (   R
   t   key(    (    s:   /usr/lib64/python2.7/site-packages/M2Crypto/SSL/Context.pyt   __getitem__   s    c         C   s   | |  j  | <d  S(   N(   R    (   R
   R   t   value(    (    s:   /usr/lib64/python2.7/site-packages/M2Crypto/SSL/Context.pyt   __setitem__   s    c         C   s   |  j  | =d  S(   N(   R    (   R
   R   (    (    s:   /usr/lib64/python2.7/site-packages/M2Crypto/SSL/Context.pyt   __delitem__   s    N(   t   __name__t
   __module__t   Nonet	   singletonR   R   R   R   (    (    (    s:   /usr/lib64/python2.7/site-packages/M2Crypto/SSL/Context.pyR	      s
   			c           C   s%   t  j d  k r t    t  _ n  t  j S(   N(   R	   R   R   (    (    (    s:   /usr/lib64/python2.7/site-packages/M2Crypto/SSL/Context.pyR       s    c           B   sJ  e  Z d  Z e j Z d d d d  Z d   Z d   Z	 d e
 j d  Z d e
 j d  Z d   Z e Z Z d d d  Z e Z d	   Z d
   Z d   Z d d  Z d   Z d   Z d   Z d d  Z d   Z d d  Z e j d  Z d   Z  d   Z! d   Z" d   Z# d   Z$ d   Z% d   Z& d   Z' d   Z( RS(   s   'Context' for SSL connections.t   sslv23c         C   s   t  t | d d   } | d  k r2 t d |  n  t j |    |  _ d |  _ | |  _ |  t   t	 |  j  <t j
 |  j d  | d  k r | d k r |  j t j t j B n  |  j d  n  d  S(   Nt   _methods   no such protocol '%s'i    l    R   s!   ALL:!ADH:!LOW:!EXP:!MD5:@STRENGTH(   t   getattrR   R   t
   ValueErrort   ssl_ctx_newt   ctxt   allow_unknown_cat   post_connection_checkR    t   longt   ssl_ctx_set_cache_sizet   set_optionst
   SSL_OP_ALLt   SSL_OP_NO_SSLv2t   set_cipher_list(   R
   t   protocolt   weak_cryptoR   t   proto(    (    s:   /usr/lib64/python2.7/site-packages/M2Crypto/SSL/Context.pyR   '   s    		c         C   s)   t  |  d d   r% |  j |  j  n  d  S(   NR   (   R   R   t   m2_ssl_ctx_freeR   (   R
   (    (    s:   /usr/lib64/python2.7/site-packages/M2Crypto/SSL/Context.pyt   __del__6   s    c         C   s   t    t |  j  =d  S(   N(   R    R   R   (   R
   (    (    s:   /usr/lib64/python2.7/site-packages/M2Crypto/SSL/Context.pyt   close:   s    c         C   sj   t  j |  j |  t  j |  j |  | s5 | } n  t  j |  j |  t  j |  j  sf t d  n  d S(   sw  Load certificate and private key into the context.
        
        @param certfile: File that contains the PEM-encoded certificate.
        @type certfile:  str
        @param keyfile:  File that contains the PEM-encoded private key.
                         Default value of None indicates that the private key
                         is to be found in 'certfile'.
        @type keyfile:   str

        @param callback: Callable object to be invoked if the private key is
                         passphrase-protected. Default callback provides a
                         simple terminal-style input for the passphrase.
        s   public/private key mismatchN(   R   t   ssl_ctx_passphrase_callbackR   t   ssl_ctx_use_certt   ssl_ctx_use_privkeyt   ssl_ctx_check_privkeyR   (   R
   t   certfilet   keyfilet   callback(    (    s:   /usr/lib64/python2.7/site-packages/M2Crypto/SSL/Context.pyt	   load_cert=   s    	c         C   sj   t  j |  j |  t  j |  j |  | s5 | } n  t  j |  j |  t  j |  j  sf t d  n  d S(   s  Load certificate chain and private key into the context.
        
        @param certchainfile: File object containing the PEM-encoded
                              certificate chain.
        @type  certchainfile: str
        @param keyfile:       File object containing the PEM-encoded private
                              key. Default value of None indicates that the
                              private key is to be found in 'certchainfile'.
        @type keyfile:        str  

        @param callback:      Callable object to be invoked if the private key
                              is passphrase-protected. Default callback 
                              provides a simple terminal-style input for the
                              passphrase.
        s   public/private key mismatchN(   R   R)   R   t   ssl_ctx_use_cert_chainR+   R,   R   (   R
   t   certchainfileR.   R/   (    (    s:   /usr/lib64/python2.7/site-packages/M2Crypto/SSL/Context.pyt   load_cert_chainS   s    	c         C   s   t  j |  j |  d S(   s   Load CA certs into the context. These CA certs are sent to the
        peer during *SSLv3 certificate request*.
        
        @param cafile: File object containing one or more PEM-encoded CA
                       certificates concatenated together.
        @type cafile:  str
        N(   R   t$   ssl_ctx_set_client_CA_list_from_fileR   (   R
   t   cafile(    (    s:   /usr/lib64/python2.7/site-packages/M2Crypto/SSL/Context.pyt   set_client_CA_list_from_filek   s    c         C   s=   | d k r' | d k r' t d   n  t j |  j | |  S(   s  Load CA certs into the context. These CA certs are used during
        verification of the peer's certificate.

        @param cafile: File containing one or more PEM-encoded CA certificates
                       concatenated together.
        @type cafile:  str
        @param capath: Directory containing PEM-encoded CA certificates
                       (one certificate per file).
        @type capath:  str
        s'   cafile and capath can not both be None.N(   R   R   R   t   ssl_ctx_load_verify_locationsR   (   R
   R5   t   capath(    (    s:   /usr/lib64/python2.7/site-packages/M2Crypto/SSL/Context.pyt   load_verify_locationsx   s    c         C   s:   t  j |  j |  } | s6 t j t j   d   n  d  S(   Nt    (   R   t   ssl_ctx_set_session_id_contextR   R   t   SSLErrort   get_error_code(   R
   t   idt   ret(    (    s:   /usr/lib64/python2.7/site-packages/M2Crypto/SSL/Context.pyt   set_session_id_ctx   s    c         C   s   | |  _  d S(   s   Set the context to accept/reject a peer certificate if the 
        certificate's CA is unknown.

        @param ok:       True to accept, False to reject.
        @type ok:        boolean
        N(   R   (   R
   t   ok(    (    s:   /usr/lib64/python2.7/site-packages/M2Crypto/SSL/Context.pyt   set_allow_unknown_ca   s    c         C   s   |  j  S(   sv   Get the context's setting that accepts/rejects a peer
        certificate if the certificate's CA is unknown.
        (   R   (   R
   (    (    s:   /usr/lib64/python2.7/site-packages/M2Crypto/SSL/Context.pyt   get_allow_unknown_ca   s    c         C   sO   | d k r" t j |  j |  n t j |  j | |  t j |  j |  d S(   s  
        Set verify options. Most applications will need to call this
        method with the right options to make a secure SSL connection.
        
        @param mode:     The verification mode to use. Typically at least
                         SSL.verify_peer is used. Clients would also typically
                         add SSL.verify_fail_if_no_peer_cert.
        @type mode:      int                 
        @param depth:    The maximum allowed depth of the certificate chain
                         returned by the peer.
        @type depth:     int
        @param callback: Callable that can be used to specify custom
                         verification checks.
        N(   R   R   t   ssl_ctx_set_verify_defaultR   t   ssl_ctx_set_verifyt   ssl_ctx_set_verify_depth(   R
   t   modet   depthR/   (    (    s:   /usr/lib64/python2.7/site-packages/M2Crypto/SSL/Context.pyt
   set_verify   s    c         C   s   t  j |  j  S(   N(   R   t   ssl_ctx_get_verify_modeR   (   R
   (    (    s:   /usr/lib64/python2.7/site-packages/M2Crypto/SSL/Context.pyt   get_verify_mode   s    c         C   s   t  j |  j  S(   N(   R   t   ssl_ctx_get_verify_depthR   (   R
   (    (    s:   /usr/lib64/python2.7/site-packages/M2Crypto/SSL/Context.pyt   get_verify_depth   s    c         C   s7   t  j |  } t j | j    } t j |  j |  S(   s   Load ephemeral DH parameters into the context.

        @param dhpfile: File object containing the PEM-encoded DH 
                        parameters.
        @type dhpfile:  str
        (   R   t   openfileR   t   dh_read_parameterst   bio_ptrt   ssl_ctx_set_tmp_dhR   (   R
   t   dhpfilet   ft   dhp(    (    s:   /usr/lib64/python2.7/site-packages/M2Crypto/SSL/Context.pyt
   set_tmp_dh   s    c         C   s&   | d  k	 r" t j |  j |  n  d  S(   N(   R   R   t   ssl_ctx_set_tmp_dh_callbackR   (   R
   R/   (    (    s:   /usr/lib64/python2.7/site-packages/M2Crypto/SSL/Context.pyt   set_tmp_dh_callback   s    c         C   s<   t  | t j  r( t j |  j | j  St d | f  d S(   sa   Load ephemeral RSA key into the context.

        @param rsa: M2Crypto.RSA.RSA instance.
        s(   Expected an instance of RSA.RSA, got %s.N(   t
   isinstanceR   R   t   ssl_ctx_set_tmp_rsaR   t   rsat	   TypeError(   R
   RZ   (    (    s:   /usr/lib64/python2.7/site-packages/M2Crypto/SSL/Context.pyt   set_tmp_rsa   s    c         C   s&   | d  k	 r" t j |  j |  n  d  S(   N(   R   R   t   ssl_ctx_set_tmp_rsa_callbackR   (   R
   R/   (    (    s:   /usr/lib64/python2.7/site-packages/M2Crypto/SSL/Context.pyt   set_tmp_rsa_callback   s    c         C   s   t  j |  j |  d S(   s  
        Set a callback function that can be used to get state information
        about the SSL connections that are created from this context.
        
        @param callback: Callback function. The default prints information to
                         stderr.
        N(   R   t   ssl_ctx_set_info_callbackR   (   R
   R/   (    (    s:   /usr/lib64/python2.7/site-packages/M2Crypto/SSL/Context.pyt   set_info_callback   s    c         C   s   t  j |  j |  S(   N(   R   t   ssl_ctx_set_cipher_listR   (   R
   t   cipher_list(    (    s:   /usr/lib64/python2.7/site-packages/M2Crypto/SSL/Context.pyR"      s    c         C   s   t  j |  j | j    S(   N(   R   t   ssl_ctx_add_sessionR   t   _ptr(   R
   t   session(    (    s:   /usr/lib64/python2.7/site-packages/M2Crypto/SSL/Context.pyt   add_session   s    c         C   s   t  j |  j | j    S(   N(   R   t   ssl_ctx_remove_sessionR   Rd   (   R
   Re   (    (    s:   /usr/lib64/python2.7/site-packages/M2Crypto/SSL/Context.pyt   remove_session   s    c         C   s   t  j |  j  S(   N(   R   t   ssl_ctx_get_session_timeoutR   (   R
   (    (    s:   /usr/lib64/python2.7/site-packages/M2Crypto/SSL/Context.pyt   get_session_timeout   s    c         C   s   t  j |  j |  S(   N(   R   t   ssl_ctx_set_session_timeoutR   (   R
   t   timeout(    (    s:   /usr/lib64/python2.7/site-packages/M2Crypto/SSL/Context.pyt   set_session_timeout   s    c         C   s   t  j |  j |  S(   N(   R   t   ssl_ctx_set_session_cache_modeR   (   R
   RG   (    (    s:   /usr/lib64/python2.7/site-packages/M2Crypto/SSL/Context.pyt   set_session_cache_mode   s    c         C   s   t  j |  j  S(   N(   R   t   ssl_ctx_get_session_cache_modeR   (   R
   (    (    s:   /usr/lib64/python2.7/site-packages/M2Crypto/SSL/Context.pyt   get_session_cache_mode   s    c         C   s   t  j |  j |  S(   N(   R   t   ssl_ctx_set_optionsR   (   R
   t   op(    (    s:   /usr/lib64/python2.7/site-packages/M2Crypto/SSL/Context.pyR      s    c         C   s   t  j t j |  j   S(   s   
        Get the certificate store associated with this context.
        
        @warning: The store is NOT refcounted, and as such can not be relied
        to be valid once the context goes away or is changed.
        (   R   t
   X509_StoreR   t   ssl_ctx_get_cert_storeR   (   R
   (    (    s:   /usr/lib64/python2.7/site-packages/M2Crypto/SSL/Context.pyt   get_cert_store   s    N()   R   R   t   __doc__R   t   ssl_ctx_freeR&   R   R   R'   R(   R   t   passphrase_callbackR0   R3   R6   t   load_client_CAt   load_client_caR9   t   load_verify_infoR@   RB   RC   RI   RK   RM   RU   RW   R\   R^   t   cbt   ssl_info_callbackR`   R"   Rf   Rh   Rj   Rm   Ro   Rq   R   Rv   (    (    (    s:   /usr/lib64/python2.7/site-packages/M2Crypto/SSL/Context.pyR   !   s@   				
								

								(    (    (   Rw   t   __all__t   weakrefR   R}   t   M2CryptoR   R   R   R   R   R   R	   R    R   (    (    (    s:   /usr/lib64/python2.7/site-packages/M2Crypto/SSL/Context.pyt   <module>   s   .	