home *** CD-ROM | disk | FTP | other *** search
/ Maximum CD 2012 January / maximum-cd-2012-01.iso / DiscContents / digsby_setup.exe / lib / M2Crypto / SMIME.pyo (.txt) < prev    next >
Encoding:
Python Compiled Bytecode  |  2011-10-05  |  8.9 KB  |  276 lines

  1. # Source Generated with Decompyle++
  2. # File: in.pyo (Python 2.6)
  3.  
  4. import BIO
  5. import EVP
  6. import X509
  7. import Err
  8. import util
  9. import m2
  10. PKCS7_TEXT = m2.PKCS7_TEXT
  11. PKCS7_NOCERTS = m2.PKCS7_NOCERTS
  12. PKCS7_NOSIGS = m2.PKCS7_NOSIGS
  13. PKCS7_NOCHAIN = m2.PKCS7_NOCHAIN
  14. PKCS7_NOINTERN = m2.PKCS7_NOINTERN
  15. PKCS7_NOVERIFY = m2.PKCS7_NOVERIFY
  16. PKCS7_DETACHED = m2.PKCS7_DETACHED
  17. PKCS7_BINARY = m2.PKCS7_BINARY
  18. PKCS7_NOATTR = m2.PKCS7_NOATTR
  19. PKCS7_SIGNED = m2.PKCS7_SIGNED
  20. PKCS7_ENVELOPED = m2.PKCS7_ENVELOPED
  21. PKCS7_SIGNED_ENVELOPED = m2.PKCS7_SIGNED_ENVELOPED
  22. PKCS7_DATA = m2.PKCS7_DATA
  23.  
  24. class PKCS7_Error(Exception):
  25.     pass
  26.  
  27. m2.pkcs7_init(PKCS7_Error)
  28.  
  29. class PKCS7:
  30.     m2_pkcs7_free = m2.pkcs7_free
  31.     
  32.     def __init__(self, pkcs7 = None, _pyfree = 0):
  33.         if pkcs7 is not None:
  34.             self.pkcs7 = pkcs7
  35.             self._pyfree = _pyfree
  36.         else:
  37.             self.pkcs7 = m2.pkcs7_new()
  38.             self._pyfree = 1
  39.  
  40.     
  41.     def __del__(self):
  42.         if getattr(self, '_pyfree', 0):
  43.             self.m2_pkcs7_free(self.pkcs7)
  44.         
  45.  
  46.     
  47.     def _ptr(self):
  48.         return self.pkcs7
  49.  
  50.     
  51.     def type(self, text_name = 0):
  52.         if text_name:
  53.             return m2.pkcs7_type_sn(self.pkcs7)
  54.         return m2.pkcs7_type_nid(self.pkcs7)
  55.  
  56.     
  57.     def write(self, bio):
  58.         return m2.pkcs7_write_bio(self.pkcs7, bio._ptr())
  59.  
  60.     
  61.     def write_der(self, bio):
  62.         return m2.pkcs7_write_bio_der(self.pkcs7, bio._ptr())
  63.  
  64.     
  65.     def get0_signers(self, certs, flags = 0):
  66.         return X509.X509_Stack(m2.pkcs7_get0_signers(self.pkcs7, certs.stack, flags), 1)
  67.  
  68.  
  69.  
  70. def load_pkcs7(p7file):
  71.     bio = m2.bio_new_file(p7file, 'r')
  72.     if bio is None:
  73.         raise Err.get_error()
  74.     bio is None
  75.     
  76.     try:
  77.         p7_ptr = m2.pkcs7_read_bio(bio)
  78.     finally:
  79.         m2.bio_free(bio)
  80.  
  81.     if p7_ptr is None:
  82.         raise Err.get_error()
  83.     p7_ptr is None
  84.     return PKCS7(p7_ptr, 1)
  85.  
  86.  
  87. def load_pkcs7_bio(p7_bio):
  88.     p7_ptr = m2.pkcs7_read_bio(p7_bio._ptr())
  89.     if p7_ptr is None:
  90.         raise Err.get_error()
  91.     p7_ptr is None
  92.     return PKCS7(p7_ptr, 1)
  93.  
  94.  
  95. def smime_load_pkcs7(p7file):
  96.     bio = m2.bio_new_file(p7file, 'r')
  97.     if bio is None:
  98.         raise Err.get_error()
  99.     bio is None
  100.     
  101.     try:
  102.         (p7_ptr, bio_ptr) = m2.smime_read_pkcs7(bio)
  103.     finally:
  104.         m2.bio_free(bio)
  105.  
  106.     if p7_ptr is None:
  107.         raise Err.get_error()
  108.     p7_ptr is None
  109.     if bio_ptr is None:
  110.         return (PKCS7(p7_ptr, 1), None)
  111.     return (PKCS7(p7_ptr, 1), BIO.BIO(bio_ptr, 1))
  112.  
  113.  
  114. def smime_load_pkcs7_bio(p7_bio):
  115.     (p7_ptr, bio_ptr) = m2.smime_read_pkcs7(p7_bio._ptr())
  116.     if p7_ptr is None:
  117.         raise Err.get_error()
  118.     p7_ptr is None
  119.     if bio_ptr is None:
  120.         return (PKCS7(p7_ptr, 1), None)
  121.     return (PKCS7(p7_ptr, 1), BIO.BIO(bio_ptr, 1))
  122.  
  123.  
  124. class Cipher:
  125.     
  126.     def __init__(self, algo):
  127.         cipher = getattr(m2, algo, None)
  128.         if cipher is None:
  129.             raise ValueError, ('unknown cipher', algo)
  130.         cipher is None
  131.         self.cipher = cipher()
  132.  
  133.     
  134.     def _ptr(self):
  135.         return self.cipher
  136.  
  137.  
  138.  
  139. class SMIME_Error(Exception):
  140.     pass
  141.  
  142. m2.smime_init(SMIME_Error)
  143.  
  144. class SMIME:
  145.     
  146.     def load_key(self, keyfile, certfile = None, callback = util.passphrase_callback):
  147.         if certfile is None:
  148.             certfile = keyfile
  149.         
  150.         self.pkey = EVP.load_key(keyfile, callback)
  151.         self.x509 = X509.load_cert(certfile)
  152.  
  153.     
  154.     def load_key_bio(self, keybio, certbio = None, callback = util.passphrase_callback):
  155.         if certbio is None:
  156.             certbio = keybio
  157.         
  158.         self.pkey = EVP.load_key_bio(keybio, callback)
  159.         self.x509 = X509.load_cert_bio(certbio)
  160.  
  161.     
  162.     def set_x509_stack(self, stack):
  163.         self.x509_stack = stack
  164.  
  165.     
  166.     def set_x509_store(self, store):
  167.         self.x509_store = store
  168.  
  169.     
  170.     def set_cipher(self, cipher):
  171.         self.cipher = cipher
  172.  
  173.     
  174.     def unset_key(self):
  175.         del self.pkey
  176.         del self.x509
  177.  
  178.     
  179.     def unset_x509_stack(self):
  180.         del self.x509_stack
  181.  
  182.     
  183.     def unset_x509_store(self):
  184.         del self.x509_store
  185.  
  186.     
  187.     def unset_cipher(self):
  188.         del self.cipher
  189.  
  190.     
  191.     def encrypt(self, data_bio, flags = 0):
  192.         if not hasattr(self, 'cipher'):
  193.             raise SMIME_Error, 'no cipher: use set_cipher()'
  194.         hasattr(self, 'cipher')
  195.         if not hasattr(self, 'x509_stack'):
  196.             raise SMIME_Error, 'no recipient certs: use set_x509_stack()'
  197.         hasattr(self, 'x509_stack')
  198.         pkcs7 = m2.pkcs7_encrypt(self.x509_stack._ptr(), data_bio._ptr(), self.cipher._ptr(), flags)
  199.         if pkcs7 is None:
  200.             raise SMIME_Error, Err.get_error()
  201.         pkcs7 is None
  202.         return PKCS7(pkcs7, 1)
  203.  
  204.     
  205.     def decrypt(self, pkcs7, flags = 0):
  206.         if not hasattr(self, 'pkey'):
  207.             raise SMIME_Error, 'no private key: use load_key()'
  208.         hasattr(self, 'pkey')
  209.         if not hasattr(self, 'x509'):
  210.             raise SMIME_Error, 'no certificate: load_key() used incorrectly?'
  211.         hasattr(self, 'x509')
  212.         blob = m2.pkcs7_decrypt(pkcs7._ptr(), self.pkey._ptr(), self.x509._ptr(), flags)
  213.         if blob is None:
  214.             raise SMIME_Error, Err.get_error()
  215.         blob is None
  216.         return blob
  217.  
  218.     
  219.     def sign(self, data_bio, flags = 0):
  220.         if not hasattr(self, 'pkey'):
  221.             raise SMIME_Error, 'no private key: use load_key()'
  222.         hasattr(self, 'pkey')
  223.         if hasattr(self, 'x509_stack'):
  224.             pkcs7 = m2.pkcs7_sign1(self.x509._ptr(), self.pkey._ptr(), self.x509_stack._ptr(), data_bio._ptr(), flags)
  225.             if pkcs7 is None:
  226.                 raise SMIME_Error, Err.get_error()
  227.             pkcs7 is None
  228.             return PKCS7(pkcs7, 1)
  229.         pkcs7 = m2.pkcs7_sign0(self.x509._ptr(), self.pkey._ptr(), data_bio._ptr(), flags)
  230.         if pkcs7 is None:
  231.             raise SMIME_Error, Err.get_error()
  232.         pkcs7 is None
  233.         return PKCS7(pkcs7, 1)
  234.  
  235.     
  236.     def verify(self, pkcs7, data_bio = None, flags = 0):
  237.         if not hasattr(self, 'x509_stack'):
  238.             raise SMIME_Error, 'no signer certs: use set_x509_stack()'
  239.         hasattr(self, 'x509_stack')
  240.         if not hasattr(self, 'x509_store'):
  241.             raise SMIME_Error, 'no x509 cert store: use set_x509_store()'
  242.         hasattr(self, 'x509_store')
  243.         p7 = pkcs7._ptr()
  244.         if data_bio is None:
  245.             blob = m2.pkcs7_verify0(p7, self.x509_stack._ptr(), self.x509_store._ptr(), flags)
  246.         else:
  247.             blob = m2.pkcs7_verify1(p7, self.x509_stack._ptr(), self.x509_store._ptr(), data_bio._ptr(), flags)
  248.         if blob is None:
  249.             raise SMIME_Error, Err.get_error()
  250.         blob is None
  251.         return blob
  252.  
  253.     
  254.     def write(self, out_bio, pkcs7, data_bio = None, flags = 0):
  255.         if data_bio is None:
  256.             return m2.smime_write_pkcs7(out_bio._ptr(), pkcs7._ptr(), flags)
  257.         return m2.smime_write_pkcs7_multi(out_bio._ptr(), pkcs7._ptr(), data_bio._ptr(), flags)
  258.  
  259.  
  260.  
  261. def text_crlf(text):
  262.     bio_in = BIO.MemoryBuffer(text)
  263.     bio_out = BIO.MemoryBuffer()
  264.     if m2.smime_crlf_copy(bio_in, bio_out):
  265.         return bio_out.read()
  266.     raise Err.get_error()
  267.  
  268.  
  269. def text_crlf_bio(bio_in):
  270.     bio_out = BIO.MemoryBuffer()
  271.     m2.smime_crlf_copy(bio_in, bio_out)
  272.     if m2.smime_crlf_copy(bio_in, bio_out):
  273.         return bio_out
  274.     raise Err.get_error()
  275.  
  276.