home *** CD-ROM | disk | FTP | other *** search
/ Maximum CD 2011 February / maximum-cd-2011-02.iso / DiscContents / digsby_setup85.exe / lib / M2Crypto / SSL / Context.pyo (.txt) < prev    next >
Encoding:
Python Compiled Bytecode  |  2010-11-24  |  8.2 KB  |  197 lines

  1. # Source Generated with Decompyle++
  2. # File: in.pyo (Python 2.6)
  3.  
  4. from weakref import WeakValueDictionary
  5. import cb
  6. from M2Crypto import util, BIO, Err, RSA, m2, X509
  7.  
  8. class _ctxmap:
  9.     singleton = None
  10.     
  11.     def __init__(self):
  12.         self.map = WeakValueDictionary()
  13.  
  14.     
  15.     def __getitem__(self, key):
  16.         return self.map[key]
  17.  
  18.     
  19.     def __setitem__(self, key, value):
  20.         self.map[key] = value
  21.  
  22.     
  23.     def __delitem__(self, key):
  24.         del self.map[key]
  25.  
  26.  
  27.  
  28. def map():
  29.     if _ctxmap.singleton is None:
  30.         _ctxmap.singleton = _ctxmap()
  31.     
  32.     return _ctxmap.singleton
  33.  
  34.  
  35. class Context:
  36.     m2_ssl_ctx_free = m2.ssl_ctx_free
  37.     
  38.     def __init__(self, protocol = 'sslv23', weak_crypto = None):
  39.         proto = getattr(m2, protocol + '_method', None)
  40.         if proto is None:
  41.             raise ValueError, "no such protocol '%s'" % protocol
  42.         proto is None
  43.         self.ctx = m2.ssl_ctx_new(proto())
  44.         self.allow_unknown_ca = 0
  45.         map()[long(self.ctx)] = self
  46.         m2.ssl_ctx_set_cache_size(self.ctx, 0x80L)
  47.         if weak_crypto is None:
  48.             if protocol == 'sslv23':
  49.                 self.set_options(m2.SSL_OP_ALL | m2.SSL_OP_NO_SSLv2)
  50.             
  51.             self.set_cipher_list('ALL:!ADH:!LOW:!EXP:!MD5:@STRENGTH')
  52.         
  53.  
  54.     
  55.     def __del__(self):
  56.         if getattr(self, 'ctx', None):
  57.             self.m2_ssl_ctx_free(self.ctx)
  58.         
  59.  
  60.     
  61.     def close(self):
  62.         del map()[long(self.ctx)]
  63.  
  64.     
  65.     def load_cert(self, certfile, keyfile = None, callback = util.passphrase_callback):
  66.         m2.ssl_ctx_passphrase_callback(self.ctx, callback)
  67.         m2.ssl_ctx_use_cert(self.ctx, certfile)
  68.         if not keyfile:
  69.             keyfile = certfile
  70.         
  71.         m2.ssl_ctx_use_privkey(self.ctx, keyfile)
  72.         if not m2.ssl_ctx_check_privkey(self.ctx):
  73.             raise ValueError, 'public/private key mismatch'
  74.         m2.ssl_ctx_check_privkey(self.ctx)
  75.  
  76.     
  77.     def load_cert_chain(self, certchainfile, keyfile = None, callback = util.passphrase_callback):
  78.         m2.ssl_ctx_passphrase_callback(self.ctx, callback)
  79.         m2.ssl_ctx_use_cert_chain(self.ctx, certchainfile)
  80.         if not keyfile:
  81.             keyfile = certchainfile
  82.         
  83.         m2.ssl_ctx_use_privkey(self.ctx, keyfile)
  84.         if not m2.ssl_ctx_check_privkey(self.ctx):
  85.             raise ValueError, 'public/private key mismatch'
  86.         m2.ssl_ctx_check_privkey(self.ctx)
  87.  
  88.     
  89.     def set_client_CA_list_from_file(self, cafile):
  90.         m2.ssl_ctx_set_client_CA_list_from_file(self.ctx, cafile)
  91.  
  92.     load_client_CA = load_client_ca = set_client_CA_list_from_file
  93.     
  94.     def load_verify_locations(self, cafile = None, capath = None):
  95.         if cafile is None and capath is None:
  96.             raise ValueError('cafile and capath can not both be None.')
  97.         capath is None
  98.         return m2.ssl_ctx_load_verify_locations(self.ctx, cafile, capath)
  99.  
  100.     load_verify_info = load_verify_locations
  101.     
  102.     def set_session_id_ctx(self, id):
  103.         ret = m2.ssl_ctx_set_session_id_context(self.ctx, id)
  104.         if not ret:
  105.             raise Err.SSLError(Err.get_error_code(), '')
  106.         ret
  107.  
  108.     
  109.     def set_allow_unknown_ca(self, ok):
  110.         self.allow_unknown_ca = ok
  111.  
  112.     
  113.     def get_allow_unknown_ca(self):
  114.         return self.allow_unknown_ca
  115.  
  116.     
  117.     def set_verify(self, mode, depth, callback = None):
  118.         if callback is None:
  119.             m2.ssl_ctx_set_verify_default(self.ctx, mode)
  120.         else:
  121.             m2.ssl_ctx_set_verify(self.ctx, mode, callback)
  122.         m2.ssl_ctx_set_verify_depth(self.ctx, depth)
  123.  
  124.     
  125.     def get_verify_mode(self):
  126.         return m2.ssl_ctx_get_verify_mode(self.ctx)
  127.  
  128.     
  129.     def get_verify_depth(self):
  130.         return m2.ssl_ctx_get_verify_depth(self.ctx)
  131.  
  132.     
  133.     def set_tmp_dh(self, dhpfile):
  134.         f = BIO.openfile(dhpfile)
  135.         dhp = m2.dh_read_parameters(f.bio_ptr())
  136.         return m2.ssl_ctx_set_tmp_dh(self.ctx, dhp)
  137.  
  138.     
  139.     def set_tmp_dh_callback(self, callback = None):
  140.         if callback is not None:
  141.             m2.ssl_ctx_set_tmp_dh_callback(self.ctx, callback)
  142.         
  143.  
  144.     
  145.     def set_tmp_rsa(self, rsa):
  146.         if isinstance(rsa, RSA.RSA):
  147.             return m2.ssl_ctx_set_tmp_rsa(self.ctx, rsa.rsa)
  148.         raise TypeError, 'Expected an instance of RSA.RSA, got %s.' % (rsa,)
  149.  
  150.     
  151.     def set_tmp_rsa_callback(self, callback = None):
  152.         if callback is not None:
  153.             m2.ssl_ctx_set_tmp_rsa_callback(self.ctx, callback)
  154.         
  155.  
  156.     
  157.     def set_info_callback(self, callback = cb.ssl_info_callback):
  158.         m2.ssl_ctx_set_info_callback(self.ctx, callback)
  159.  
  160.     
  161.     def set_cipher_list(self, cipher_list):
  162.         return m2.ssl_ctx_set_cipher_list(self.ctx, cipher_list)
  163.  
  164.     
  165.     def add_session(self, session):
  166.         return m2.ssl_ctx_add_session(self.ctx, session._ptr())
  167.  
  168.     
  169.     def remove_session(self, session):
  170.         return m2.ssl_ctx_remove_session(self.ctx, session._ptr())
  171.  
  172.     
  173.     def get_session_timeout(self):
  174.         return m2.ssl_ctx_get_session_timeout(self.ctx)
  175.  
  176.     
  177.     def set_session_timeout(self, timeout):
  178.         return m2.ssl_ctx_set_session_timeout(self.ctx, timeout)
  179.  
  180.     
  181.     def set_session_cache_mode(self, mode):
  182.         return m2.ssl_ctx_set_session_cache_mode(self.ctx, mode)
  183.  
  184.     
  185.     def get_session_cache_mode(self):
  186.         return m2.ssl_ctx_get_session_cache_mode(self.ctx)
  187.  
  188.     
  189.     def set_options(self, op):
  190.         return m2.ssl_ctx_set_options(self.ctx, op)
  191.  
  192.     
  193.     def get_cert_store(self):
  194.         return X509.X509_Store(m2.ssl_ctx_get_cert_store(self.ctx))
  195.  
  196.  
  197.