1 from ctypescrypto import libcrypto
2 from ctypes import c_char_p, c_void_p, c_int, string_at, c_long,POINTER,byref, create_string_buffer
5 Provides interface to OpenSSL memory bios
6 use str() to get contents of writable bio
7 use bio member to pass to libcrypto function
9 def __init__(self,data=None):
10 """ If data is specified, creates read-only BIO. If data is
11 None, creates writable BIO
14 method=libcrypto.BIO_s_mem()
15 self.bio=libcrypto.BIO_new(method)
17 self.bio=libcrypto.BIO_new_mem_buf(c_char_p(data),len(data))
19 libcrypto.BIO_free(self.bio)
23 l=libcrypto.BIO_ctrl(self.bio,3,0,byref(p))
25 def read(self,length=None):
26 if not length is None:
27 if type(length)!=type(0):
28 raise TypeError("length to read should be number")
29 buf=create_string_buffer(length)
30 readbytes=libcrypto.BIO_read(self.bio,buf,length)
32 raise NotImplementedError("Function is not supported by this BIO")
37 return buf.raw[:readbytes]
39 buf=create_string_buffer(1024)
43 r=libcrypto.BIO_read(self.bio,buf,1024)
45 raise NotImplementedError("Function is not supported by this BIO")
53 r=libcrypto.BIO_write(self.bio,data,len(data))
55 raise NotImplementedError("Function not supported by this BIO")
57 raise IOError("Not all data were successfully written")
59 #FIXME TODO - BIO should have stream-like interface
60 libcrypto.BIO_s_mem.restype=c_void_p
61 libcrypto.BIO_new.restype=c_void_p
62 libcrypto.BIO_new.argtypes=(c_void_p,)
63 libcrypto.BIO_ctrl.restype=c_long
64 libcrypto.BIO_ctrl.argtypes=(c_void_p,c_int,c_long,POINTER(c_char_p))
65 libcrypto.BIO_read.argtypes=(c_void_p,c_char_p,c_int)
66 libcrypto.BIO_write.argtypes=(c_void_p,c_char_p,c_int)
67 libcrypto.BIO_free.argtypes=(c_void_p,)
68 libcrypto.BIO_new_mem_buf.restype=c_void_p
69 libcrypto.BIO_new_mem_buf.argtypes=(c_char_p,c_int)