def close(self): # type: () -> None # Original version: what's wrong with it? # self.closed = 1 # if self.close_pyfile: # self.pyfile.close() log.info('closing %s file', self.fname) m2.bio_free(self.bio) self.closed = True
def m2_MemoryBuffer_from_ct_ptr(ct_bio): # generate and immeidately free a BIO object so that we can get a # reference to the underlying SwigPyObject m2_buf = BIO.MemoryBuffer() m2.bio_free(m2_buf._ptr()) # even though we've free'd we still have the ref # replace the pointer change_swig_ptr(m2_buf._ptr(), ct_bio) # return the MemoryBuffer object (that references our ctypes BIO obj) return m2_buf
def m2_MemoryBuffer_from_ct_ptr(ct_bio): # generate and immeidately free a BIO object so that we can get a # reference to the underlying SwigPyObject m2_buf = BIO.MemoryBuffer() m2.bio_free( m2_buf._ptr()) # even though we've free'd we still have the ref # replace the pointer change_swig_ptr(m2_buf._ptr(), ct_bio) # return the MemoryBuffer object (that references our ctypes BIO obj) return m2_buf
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): # 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 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 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)
def c_style(HOST, PORT, req): # Set up SSL context. ctx = m2.ssl_ctx_new(m2.sslv3_method()) m2.ssl_ctx_use_cert(ctx, 'client.pem') m2.ssl_ctx_use_privkey(ctx, 'client.pem') # Make the socket connection. s = socket(AF_INET, SOCK_STREAM) s.connect((HOST, PORT)) # Set up the SSL connection. sbio = m2.bio_new_socket(s.fileno(), 0) ssl = m2.ssl_new(ctx) m2.ssl_set_bio(ssl, sbio, sbio) m2.ssl_connect(ssl) sslbio = m2.bio_new(m2.bio_f_ssl()) m2.bio_set_ssl(sslbio, ssl, 0) # Push a buffering BIO over the SSL BIO. iobuf = m2.bio_new(m2.bio_f_buffer()) topbio = m2.bio_push(iobuf, sslbio) # Send the request. m2.bio_write(sslbio, req) # Receive the response. while 1: data = m2.bio_gets(topbio, 4096) if not data: break sys.stdout.write(data) # Cleanup. May be missing some necessary steps. ;-| m2.bio_pop(topbio) m2.bio_free(iobuf) m2.ssl_shutdown(ssl) m2.ssl_free(ssl) m2.ssl_ctx_free(ctx) s.close()
def __del__(self): if not self.closed: m2.bio_free(self.bio)
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) m2.bio_set_mem_eof_return(bio, 0) else: bio = m2.bio_new_file('XXX', 'rb') ciph = m2.bf_cbc() filt = m2.bio_new(m2.bio_f_cipher()) m2.bio_set_cipher(filt, ciph, 'key', 'iv', 0)