def smime_load_pkcs7(p7file): # type: (AnyStr) -> PKCS7 bio = m2.bio_new_file(p7file, 'r') try: p7_ptr, bio_ptr = m2.smime_read_pkcs7(bio) finally: m2.bio_free(bio) if bio_ptr is None: return PKCS7(p7_ptr, 1), None else: return PKCS7(p7_ptr, 1), BIO.BIO(bio_ptr, 1)
def load_pkcs7(p7file): bio = m2.bio_new_file(p7file, 'r') if bio is None: raise BIO.BIOError(Err.get_error()) try: p7_ptr = m2.pkcs7_read_bio(bio) finally: m2.bio_free(bio) if p7_ptr is None: raise PKCS7_Error(Err.get_error()) return PKCS7(p7_ptr, 1)
def smime_load_pkcs7(p7file): bio = m2.bio_new_file(p7file, 'r') if bio is None: raise BIO.BIOError(Err.get_error()) try: p7_ptr, bio_ptr = m2.smime_read_pkcs7(bio) finally: m2.bio_free(bio) if p7_ptr is None: raise SMIME_Error(Err.get_error()) if bio_ptr is None: return PKCS7(p7_ptr, 1), None else: return PKCS7(p7_ptr, 1), BIO.BIO(bio_ptr, 1)
def load_pkcs7_der(p7file): """ Load a PKCS7 object from a PKCS7 DER file. Return PKCS7 object. """ bio = m2.bio_new_file(p7file, 'r') if bio is None: raise PKCS7VerifyError(Err.get_error()) try: p7_ptr = m2.pkcs7_read_bio_der(bio) finally: m2.bio_free(bio) if p7_ptr is None: raise PKCS7VerifyError(Err.get_error()) return PKCS7(p7_ptr, 1)
def __init__(self, pyfile, mode='rb'): # type: (AnyStr, AnyStr) -> None super(File, self).__init__(self,) if isinstance(pyfile, six.string_types): self.fname = pyfile self.bio = m2.bio_new_file(self.fname, mode) self.pyfile = None else: # This is for downward compatibility, but I don't think, that it is # good practice to have two handles for the same file. Whats about # concurrent write access? Last write, last wins? Especially since Py3 # has its own buffer management. See: # # https://docs.python.org/3.3/c-api/file.html # pyfile.flush() self.fname = pyfile.name self.bio = m2.bio_new_fd(pyfile.fileno(), m2.bio_noclose) self.closed = False
def load_key(file, callback=util.passphrase_callback): # type: (AnyStr, Callable) -> PKey """ Load an M2Crypto.EVP.PKey from file. @param file: Name of file containing the key in PEM format. @param callback: A Python callable object that is invoked to acquire a passphrase with which to protect the key. @return: M2Crypto.EVP.PKey object. """ bio = m2.bio_new_file(file, 'r') if bio is None: raise BIO.BIOError(Err.get_error()) cptr = m2.pkey_read_pem(bio, callback) m2.bio_free(bio) if cptr is None: raise EVPError(Err.get_error()) return PKey(cptr, 1)
#!/usr/bin/env python2.0 """Demonstrates the use of m2.bio_set_mem_eof_return(). Copyright (c) 1999-2003 Ng Pheng Siong. All rights reserved.""" from M2Crypto import m2 m2.lib_init() use_mem = 1 if use_mem: bio = m2.bio_new(m2.bio_s_mem()) else: bio = m2.bio_new_file('XXX', 'wb') ciph = m2.bf_cbc() filt = m2.bio_new(m2.bio_f_cipher()) m2.bio_set_cipher(filt, ciph, 'key', 'iv', 1) m2.bio_push(filt, bio) m2.bio_write(filt, '12345678901234567890') m2.bio_flush(filt) m2.bio_pop(filt) m2.bio_free(filt) if use_mem: m2.bio_set_mem_eof_return(bio, 0) xxx = m2.bio_read(bio, 100) print `xxx`, len(xxx) m2.bio_free(bio) if use_mem: bio = m2.bio_new(m2.bio_s_mem()) m2.bio_write(bio, xxx)