1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
|
from __future__ import absolute_import
"""M2Crypto wrapper for OpenSSL BIO API.
Copyright (c) 1999-2004 Ng Pheng Siong. All rights reserved."""
import io
import logging
from typing import Any, AnyStr, Callable, Iterable, Optional, Union # noqa
from M2Crypto import m2, six
log = logging.getLogger('BIO')
class BIOError(ValueError):
pass
m2.bio_init(BIOError)
class BIO(object):
"""Abstract object interface to the BIO API."""
m2_bio_free = m2.bio_free
def __init__(self, bio=None, _pyfree=0, _close_cb=None):
# type: (Optional[BIO], int, Optional[Callable]) -> None
self.bio = bio
self._pyfree = _pyfree
self._close_cb = _close_cb
self.closed = 0
self.write_closed = 0
def __del__(self):
if self._pyfree:
self.m2_bio_free(self.bio)
def _ptr(self):
return self.bio
# Deprecated.
bio_ptr = _ptr
def fileno(self):
# type: () -> int
return m2.bio_get_fd(self.bio)
def readable(self):
# type: () -> bool
return not self.closed
def read(self, size=None):
# type: (int) -> Union[bytes, bytearray]
if not self.readable():
raise IOError('cannot read')
if size is None:
buf = bytearray()
while 1:
data = m2.bio_read(self.bio, 4096)
if not data:
break
buf += data
return buf
elif size == 0:
return b''
elif size < 0:
raise ValueError('read count is negative')
else:
return bytes(m2.bio_read(self.bio, size))
def readline(self, size=4096):
# type: (int) -> bytes
if not self.readable():
raise IOError('cannot read')
buf = m2.bio_gets(self.bio, size)
buf = '' if buf is None else buf
return six.ensure_binary(buf)
def readlines(self, sizehint='ignored'):
# type: (Union[AnyStr, int]) -> Iterable[bytes]
if not self.readable():
raise IOError('cannot read')
lines = []
while 1:
buf = m2.bio_gets(self.bio, 4096)
if buf is None:
break
lines.append(six.ensure_binary(buf))
return lines
def writeable(self):
# type: () -> bool
return (not self.closed) and (not self.write_closed)
def write(self, data):
# type: (AnyStr) -> int
"""Write data to BIO.
:return: either data written, or [0, -1] for nothing written,
-2 not implemented
"""
if not self.writeable():
raise IOError('cannot write')
if isinstance(data, six.text_type):
data = data.encode('utf8')
return m2.bio_write(self.bio, data)
def write_close(self):
# type: () -> None
self.write_closed = 1
def flush(self):
# type: () -> None
"""Flush the buffers.
:return: 1 for success, and 0 or -1 for failure
"""
m2.bio_flush(self.bio)
def reset(self):
# type: () -> int
"""Set the bio to its initial state.
:return: 1 for success, and 0 or -1 for failure
"""
return m2.bio_reset(self.bio)
def close(self):
# type: () -> None
self.closed = 1
if self._close_cb:
self._close_cb()
def should_retry(self):
# type: () -> int
"""
Can the call be attempted again, or was there an error
ie do_handshake
"""
return m2.bio_should_retry(self.bio)
def should_read(self):
# type: () -> int
"""Should we read more data?"""
return m2.bio_should_read(self.bio)
def should_write(self):
# type: () -> int
"""Should we write more data?"""
return m2.bio_should_write(self.bio)
def tell(self):
"""Return the current offset."""
return m2.bio_tell(self.bio)
def seek(self, off):
"""Seek to the specified absolute offset."""
return m2.bio_seek(self.bio, off)
def __enter__(self):
return self
def __exit__(self, *args):
# type: (*Any) -> int
self.close()
class MemoryBuffer(BIO):
"""Object interface to BIO_s_mem.
Empirical testing suggests that this class performs less well than
cStringIO, because cStringIO is implemented in C, whereas this class
is implemented in Python. Thus, the recommended practice is to use
cStringIO for regular work and convert said cStringIO object to
a MemoryBuffer object only when necessary.
"""
def __init__(self, data=None):
# type: (Optional[bytes]) -> None
super(MemoryBuffer, self).__init__(self)
if data is not None and not isinstance(data, bytes):
raise TypeError(
"data must be bytes or None, not %s" % (type(data).__name__, ))
self.bio = m2.bio_new(m2.bio_s_mem())
self._pyfree = 1
if data is not None:
m2.bio_write(self.bio, data)
def __len__(self):
# type: () -> int
return m2.bio_ctrl_pending(self.bio)
def read(self, size=0):
# type: (int) -> bytes
if not self.readable():
raise IOError('cannot read')
if size:
return m2.bio_read(self.bio, size)
else:
return m2.bio_read(self.bio, m2.bio_ctrl_pending(self.bio))
# Backwards-compatibility.
getvalue = read_all = read
def write_close(self):
# type: () -> None
super(MemoryBuffer, self).write_close()
m2.bio_set_mem_eof_return(self.bio, 0)
close = write_close
class File(BIO):
"""Object interface to BIO_s_pyfd.
This class interfaces Python to OpenSSL functions that expect BIO. For
general file manipulation in Python, use Python's builtin file object.
"""
def __init__(self, pyfile, close_pyfile=1, mode='rb'):
# type: (Union[io.BytesIO, AnyStr], int, AnyStr) -> None
super(File, self).__init__(self, _pyfree=1)
if isinstance(pyfile, six.string_types):
pyfile = open(pyfile, mode)
# 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.pyfile = pyfile
# Be wary of https://github.com/openssl/openssl/pull/1925
# BIO_new_fd is NEVER to be used before OpenSSL 1.1.1
if hasattr(m2, "bio_new_pyfd"):
self.bio = m2.bio_new_pyfd(pyfile.fileno(), m2.bio_noclose)
else:
self.bio = m2.bio_new_pyfile(pyfile, m2.bio_noclose)
self.close_pyfile = close_pyfile
self.closed = False
def flush(self):
# type: () -> None
super(File, self).flush()
self.pyfile.flush()
def close(self):
# type: () -> None
self.flush()
super(File, self).close()
if self.close_pyfile:
self.pyfile.close()
def reset(self):
# type: () -> int
"""Set the bio to its initial state.
:return: 0 for success, and -1 for failure
"""
return super(File, self).reset()
def __del__(self):
if not self.closed:
m2.bio_free(self.bio)
def openfile(filename, mode='rb'):
# type: (AnyStr, AnyStr) -> File
try:
f = open(filename, mode)
except IOError as ex:
raise BIOError(ex.args)
return File(f)
class IOBuffer(BIO):
"""Object interface to BIO_f_buffer.
Its principal function is to be BIO_push()'ed on top of a BIO_f_ssl, so
that makefile() of said underlying SSL socket works.
"""
m2_bio_pop = m2.bio_pop
m2_bio_free = m2.bio_free
def __init__(self, under_bio, mode='rwb', _pyfree=1):
# type: (BIO, str, int) -> None
super(IOBuffer, self).__init__(self, _pyfree=_pyfree)
self.io = m2.bio_new(m2.bio_f_buffer())
self.bio = m2.bio_push(self.io, under_bio._ptr())
# This reference keeps the underlying BIO alive while we're not closed.
self._under_bio = under_bio
if 'w' in mode:
self.write_closed = 0
else:
self.write_closed = 1
def __del__(self):
# type: () -> None
if getattr(self, '_pyfree', 0):
self.m2_bio_pop(self.bio)
self.m2_bio_free(self.io)
def close(self):
# type: () -> None
BIO.close(self)
class CipherStream(BIO):
"""Object interface to BIO_f_cipher."""
SALT_LEN = m2.PKCS5_SALT_LEN
m2_bio_pop = m2.bio_pop
m2_bio_free = m2.bio_free
def __init__(self, obio):
# type: (BIO) -> None
super(CipherStream, self).__init__(self, _pyfree=1)
self.obio = obio
self.bio = m2.bio_new(m2.bio_f_cipher())
self.closed = 0
def __del__(self):
# type: () -> None
if not getattr(self, 'closed', 1):
self.close()
def close(self):
# type: () -> None
self.m2_bio_pop(self.bio)
self.m2_bio_free(self.bio)
self.closed = 1
def write_close(self):
# type: () -> None
self.obio.write_close()
def set_cipher(self, algo, key, iv, op):
# type: (str, AnyStr, AnyStr, int) -> None
cipher = getattr(m2, algo, None)
if cipher is None:
raise ValueError('unknown cipher', algo)
else:
if not isinstance(key, bytes):
key = key.encode('utf8')
if not isinstance(iv, bytes):
iv = iv.encode('utf8')
m2.bio_set_cipher(self.bio, cipher(), key, iv, int(op))
m2.bio_push(self.bio, self.obio._ptr())
class SSLBio(BIO):
"""Object interface to BIO_f_ssl."""
def __init__(self, _pyfree=1):
# type: (int) -> None
super(SSLBio, self).__init__(self, _pyfree=_pyfree)
self.bio = m2.bio_new(m2.bio_f_ssl())
self.closed = 0
def set_ssl(self, conn, close_flag=m2.bio_noclose):
## type: (Connection, int) -> None
"""
Sets the bio to the SSL pointer which is
contained in the connection object.
"""
self._pyfree = 0
m2.bio_set_ssl(self.bio, conn.ssl, close_flag)
if close_flag == m2.bio_noclose:
conn.set_ssl_close_flag(m2.bio_close)
def do_handshake(self):
# type: () -> int
"""Do the handshake.
Return 1 if the handshake completes
Return 0 or a negative number if there is a problem
"""
return m2.bio_do_handshake(self.bio)
|