]> www.wagner.pp.ru Git - oss/ctypescrypto.git/blob - ctypescrypto/bio.py
Initial commit of modules
[oss/ctypescrypto.git] / ctypescrypto / bio.py
1 from ctypescrypto import libcrypto
2 from ctypes import c_char_p, c_int, string_at
3 class Membio:
4         """ 
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
8         """
9         def __init__(self,data=None):
10                 """ If data is specified, creates read-only BIO. If data is
11                         None, creates writable BIO
12                 """
13                 if data is None:
14                         method=libcrypto.BIO_s_mem()
15                         self.bio=libcrypto.BIO_new(method)
16                 else:
17                         self.bio=libcrypto.BIO_new_mem_buf(c_char_p(data),len(data))q
18         def __del__(self):
19                 libcrypto.BIO_free(self.bio)
20                 del(self.bio)
21         def __str__(self):
22                 p=c_char_p(None)
23                 l=BIO_get_mem_data(self.bio,byref(p))
24                 return string_at(p,l)
25 #FIXME TODO - BIO should have stream-like interface
26 libcrypto.BIO_s_mem.restype=c_void_p
27 libcrypto.BIO_new.restype=c_void_p
28 libcrypto.BIO_new.argtypes=(c_void_p,)
29 libcrypto.BIO_get_mem_data.restype=c_long
30 libcrypto.BIO_get_mem_data.argtypes=(c_void_p,POINTER(c_char_p))