Skip to content

Commit c36cda0

Browse files
committed
tls: Use TLSWrappedBuffer for handshake
Before, we were using pass-through IO (with the `net` module provided upstream) for the handshake and our buffers for the rest of the communication. This patch consistently uses the buffers.
1 parent 73908bb commit c36cda0

File tree

2 files changed

+18
-29
lines changed

2 files changed

+18
-29
lines changed

src/mbedtls/tls.pxd

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -475,12 +475,10 @@ cdef class TLSWrappedBuffer:
475475
cdef _rb.RingBuffer _input_buffer
476476
cdef _C_Buffers _c_buffers
477477
cdef _BaseContext _context
478-
cdef void _as_bio(self)
479478

480479

481480
cdef class TLSWrappedSocket:
482481
cdef _net.mbedtls_net_context _ctx
483482
cdef TLSWrappedBuffer _buffer
484483
cdef _socket
485484
cdef bint _closed
486-
cdef void _as_bio(self)

src/mbedtls/tls.pyx

Lines changed: 18 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1301,11 +1301,6 @@ cdef class _BaseContext:
13011301
def _state(self):
13021302
return HandshakeStep(self._ctx.state)
13031303

1304-
def _do_handshake(self):
1305-
"""Start the SSL/TLS handshake."""
1306-
while self._state is not HandshakeStep.HANDSHAKE_OVER:
1307-
self._do_handshake_step()
1308-
13091304
def _do_handshake_step(self):
13101305
if self._state is HandshakeStep.HANDSHAKE_OVER:
13111306
raise ValueError("handshake already over")
@@ -1438,16 +1433,14 @@ cdef class TLSWrappedBuffer:
14381433
self._context = context
14391434
self.context._reset()
14401435

1441-
def __cinit__(self):
1436+
def __cinit__(self, _BaseContext context):
14421437
self._output_buffer = _rb.RingBuffer(_tls.TLS_BUFFER_CAPACITY)
14431438
self._input_buffer = _rb.RingBuffer(_tls.TLS_BUFFER_CAPACITY)
14441439
self._c_buffers = _tls._C_Buffers(
14451440
&self._output_buffer._ctx, &self._input_buffer._ctx
14461441
)
1447-
1448-
cdef void _as_bio(self):
14491442
_tls.mbedtls_ssl_set_bio(
1450-
&(<_tls._BaseContext>self.context)._ctx,
1443+
&(<_tls._BaseContext>context)._ctx,
14511444
&self._c_buffers,
14521445
buffer_write,
14531446
buffer_read,
@@ -1483,7 +1476,20 @@ cdef class TLSWrappedBuffer:
14831476

14841477
def do_handshake(self):
14851478
# PEP 543
1486-
self.context._do_handshake()
1479+
self.context._do_handshake_step()
1480+
1481+
def _do_handshake_blocking(self, sock):
1482+
while self._context._state is not HandshakeStep.HANDSHAKE_OVER:
1483+
try:
1484+
self.context._do_handshake_step()
1485+
amt = sock.send(self.peek_outgoing(1024))
1486+
self.consume_outgoing(amt)
1487+
except WantReadError:
1488+
amt = sock.send(self.peek_outgoing(1024))
1489+
self.consume_outgoing(amt)
1490+
except WantWriteError:
1491+
data = sock.recv(1024)
1492+
self.receive_from_network(data)
14871493

14881494
def _setcookieparam(self, param):
14891495
self.context._setcookieparam(param)
@@ -1538,9 +1544,6 @@ cdef class TLSWrappedSocket:
15381544
super().__init__()
15391545
self._socket = socket
15401546
self._buffer = buffer
1541-
# Default to pass-through BIO.
1542-
self._ctx.fd = <int>socket.fileno()
1543-
self._as_bio()
15441547
self._closed = False
15451548

15461549
def __cinit__(self):
@@ -1559,14 +1562,6 @@ cdef class TLSWrappedSocket:
15591562
if not self._closed:
15601563
self.close()
15611564

1562-
cdef void _as_bio(self):
1563-
_tls.mbedtls_ssl_set_bio(
1564-
&(<_tls._BaseContext>self.context)._ctx,
1565-
&self._ctx,
1566-
_net.mbedtls_net_send,
1567-
_net.mbedtls_net_recv,
1568-
_net.mbedtls_net_recv_timeout)
1569-
15701565
def __str__(self):
15711566
return str(self._socket)
15721567

@@ -1588,9 +1583,7 @@ cdef class TLSWrappedSocket:
15881583
if self.type == _socket.SOCK_STREAM:
15891584
conn, address = self._socket.accept()
15901585
else:
1591-
data, address = self._socket.recvfrom(1024, _socket.MSG_PEEK)
1592-
assert data, "no data"
1593-
1586+
_, address = self._socket.recvfrom(1024, _socket.MSG_PEEK)
15941587
# Use this socket to communicate with the client and bind
15951588
# another one for the next connection. This procedure is
15961589
# adapted from `mbedtls_net_accept()`.
@@ -1724,9 +1717,7 @@ cdef class TLSWrappedSocket:
17241717
# PEP 543 adds the following methods.
17251718

17261719
def do_handshake(self):
1727-
self._as_bio()
1728-
self._buffer.do_handshake()
1729-
self._buffer._as_bio()
1720+
self._buffer._do_handshake_blocking(self._socket)
17301721

17311722
def setcookieparam(self, param):
17321723
self._buffer._setcookieparam(param)

0 commit comments

Comments
 (0)