]> www.wagner.pp.ru Git - oss/ctypescrypto.git/blob - ctypescrypto/bio.py
5040d0c1180487b55f3423492efc3be21fa39c5b
[oss/ctypescrypto.git] / ctypescrypto / bio.py
1 """
2 Interface to OpenSSL BIO library
3 """
4 from ctypescrypto import libcrypto
5 from ctypes import c_char_p, c_void_p, c_int, string_at, c_long
6 from ctypes import POINTER, byref, create_string_buffer
7 class Membio(object):
8     """
9     Provides interface to OpenSSL memory bios
10     use str() or unicode() to get contents of writable bio
11     use bio member to pass to libcrypto function
12     """
13     def __init__(self, data=None):
14         """
15         If data is specified, creates read-only BIO. If data is
16         None, creates writable BIO, contents of which can be retrieved
17         by str() or unicode()
18         """
19         if data is None:
20             method = libcrypto.BIO_s_mem()
21             self.bio = libcrypto.BIO_new(method)
22         else:
23             self.bio = libcrypto.BIO_new_mem_buf(c_char_p(data), len(data))
24
25     def __del__(self):
26         """
27         Cleans up memory used by bio
28         """
29         libcrypto.BIO_free(self.bio)
30         del self.bio
31
32     def __str__(self):
33         """
34         Returns current contents of buffer as byte string
35         """
36         string_ptr = c_char_p(None)
37         string_len = libcrypto.BIO_ctrl(self.bio, 3, 0, byref(string_ptr))
38         return string_at(string_ptr, string_len)
39
40     def __unicode__(self):
41         """
42         Attempts to interpret current contents of buffer as UTF-8 string
43         and convert it to unicode
44         """
45         return str(self).decode("utf-8")
46
47     def read(self, length=None):
48         """
49         Reads data from readble BIO. For test purposes.
50         @param length - if specifed, limits amount of data read.
51         If not BIO is read until end of buffer
52         """
53         if not length is None:
54             if not isinstance(length, (int, long)):
55                 raise TypeError("length to read should be number")
56             buf = create_string_buffer(length)
57             readbytes = libcrypto.BIO_read(self.bio, buf, length)
58             if readbytes == -2:
59                 raise NotImplementedError("Function is not supported by" +
60                                           "this BIO")
61             if readbytes == -1:
62                 raise IOError
63             if readbytes == 0:
64                 return ""
65             return buf.raw[:readbytes]
66         else:
67             buf = create_string_buffer(1024)
68             out = ""
69             readbytes = 1
70             while readbytes > 0:
71                 readbytes = libcrypto.BIO_read(self.bio, buf, 1024)
72                 if readbytes == -2:
73                     raise NotImplementedError("Function is not supported by " +
74                                               "this BIO")
75                 if readbytes == -1:
76                     raise IOError
77                 if readbytes > 0:
78                     out += buf.raw[:readbytes]
79             return out
80
81     def write(self, data):
82         """
83         Writes data to writable bio. For test purposes
84         """
85         if isinstance(data, unicode):
86             data = data.encode("utf-8")
87         else:
88             data = str(data)
89
90         written = libcrypto.BIO_write(self.bio, data, len(data))
91         if written == -2:
92             raise NotImplementedError("Function not supported by this BIO")
93         if written < len(data):
94             raise IOError("Not all data were successfully written")
95
96     def reset(self):
97         """
98         Resets the read-only bio to start and discards all data from
99         writable bio
100         """
101         libcrypto.BIO_ctrl(self.bio, 1, 0, None)
102
103 __all__ = ['Membio']
104 libcrypto.BIO_s_mem.restype = c_void_p
105 libcrypto.BIO_new.restype = c_void_p
106 libcrypto.BIO_new.argtypes = (c_void_p, )
107 libcrypto.BIO_ctrl.restype = c_long
108 libcrypto.BIO_ctrl.argtypes = (c_void_p, c_int, c_long, POINTER(c_char_p))
109 libcrypto.BIO_read.argtypes = (c_void_p, c_char_p, c_int)
110 libcrypto.BIO_write.argtypes = (c_void_p, c_char_p, c_int)
111 libcrypto.BIO_free.argtypes = (c_void_p, )
112 libcrypto.BIO_new_mem_buf.restype = c_void_p
113 libcrypto.BIO_new_mem_buf.argtypes = (c_char_p, c_int)