Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion Doc/library/asyncio-stream.rst
Original file line number Diff line number Diff line change
Expand Up @@ -399,7 +399,7 @@ StreamWriter
to the event loop explicitly with ``await asyncio.sleep(0)``
(see :func:`asyncio.sleep`).

.. method:: start_tls(sslcontext, *, server_hostname=None, \
.. method:: start_tls(sslcontext, *, server_side=None, server_hostname=None, \
ssl_handshake_timeout=None, ssl_shutdown_timeout=None)
:async:

Expand All @@ -409,6 +409,10 @@ StreamWriter

* *sslcontext*: a configured instance of :class:`~ssl.SSLContext`.

* *server_side*: set to ``True`` to perform the server side of the TLS
handshake, or ``False`` to perform the client side. By default
(``None``), the role is inferred from how the stream was created.

* *server_hostname*: sets or overrides the host name that the target
server's certificate will be matched against.

Expand All @@ -426,6 +430,8 @@ StreamWriter
Added the *ssl_shutdown_timeout* parameter.

.. versionchanged:: 3.16
Added the *server_side* parameter.

Raises a ``ValueError`` if ``sslcontext.check_hostname`` is ``True``
and ``server_hostname`` is not supplied.

Expand Down
4 changes: 3 additions & 1 deletion Lib/asyncio/streams.py
Original file line number Diff line number Diff line change
Expand Up @@ -395,11 +395,13 @@ async def drain(self):
await self._protocol._drain_helper()

async def start_tls(self, sslcontext, *,
server_side=None,
server_hostname=None,
ssl_handshake_timeout=None,
ssl_shutdown_timeout=None):
"""Upgrade an existing stream-based connection to TLS."""
server_side = self._protocol._client_connected_cb is not None
if server_side is None:
server_side = self._protocol._client_connected_cb is not None
protocol = self._protocol
await self.drain()
new_transport = await self._loop.start_tls( # type: ignore
Expand Down
43 changes: 43 additions & 0 deletions Lib/test/test_asyncio/test_streams.py
Original file line number Diff line number Diff line change
Expand Up @@ -920,6 +920,49 @@ async def run_test():
self.loop.run_until_complete(run_test())
self.assertEqual(messages, [])

@unittest.skipIf(ssl is None, 'No ssl module')
def test_start_tls_reversed_roles(self):
async def run_test():
accepted = self.loop.create_future()
async with await asyncio.start_server(
lambda r, w: accepted.set_result((r, w)),
socket_helper.HOSTv4, 0,
) as server:
address = server.sockets[0].getsockname()
client_reader, client_writer = await asyncio.open_connection(
*address
)
server_reader, server_writer = await accepted
try:
await asyncio.gather(
client_writer.start_tls(
test_utils.simple_server_sslcontext(),
server_side=True,
),
server_writer.start_tls(
test_utils.simple_client_sslcontext(),
server_side=False,
),
)
client_writer.write(b"hello")
self.assertEqual(
await server_reader.readexactly(5), b"hello"
)
server_writer.write(b"world")
self.assertEqual(
await client_reader.readexactly(5), b"world"
)
finally:
client_writer.close()
server_writer.close()
await asyncio.gather(
client_writer.wait_closed(), server_writer.wait_closed()
)

self.loop.run_until_complete(
asyncio.wait_for(run_test(), support.SHORT_TIMEOUT)
)

def test_streamwriter_start_tls_updates_reader_transport(self):
reader = asyncio.StreamReader(loop=self.loop)
protocol = asyncio.StreamReaderProtocol(reader, loop=self.loop)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Allow overriding the TLS client or server role in
:meth:`asyncio.StreamWriter.start_tls` with the *server_side* keyword argument.
Loading