ssl 将Python异步连接升级到TLS时的排序问题

dbf7pr2w  于 2023-01-13  发布在  Python
关注(0)|答案(1)|浏览(178)

我有一个Python服务器,需要将现有连接升级到TLS。该连接是由asyncio.start_server创建的。
它需要在连接过程中升级到TLS,因为它正在模拟MySQL's wire protocol
这个项目是mysql-mimic。下面是我目前所做的尝试:https://github.com/kelsin/mysql-mimic/pull/17
似乎有某种顺序问题:如果客户端在服务器之前启动TLS,则服务器永远不会完成握手。
下面是重现此问题的脚本:

import asyncio
import ssl

# Self-signed certs for testing
CERT_FILE = "cert.pem"
CERT = """
-----BEGIN CERTIFICATE-----
MIIEIzCCAwugAwIBAgIUUMROPFb3ZMssWfl9/tJVMB0Zl0owDQYJKoZIhvcNAQEL
BQAwgZ8xCzAJBgNVBAYTAlVTMRMwEQYDVQQIDApDYWxpZm9ybmlhMRYwFAYDVQQH
DA1TYW4gRnJhbmNpc2NvMRQwEgYDVQQKDAtteXNxbC1taW1pYzEUMBIGA1UECwwL
bXlzcWwtbWltaWMxFDASBgNVBAMMC215c3FsLW1pbWljMSEwHwYJKoZIhvcNAQkB
FhJleGFtcGxlQGRvbWFpbi5jb20wIBcNMjIxMDI0MTgyNzQ0WhgPMjEyMjA5MzAx
ODI3NDRaMIGfMQswCQYDVQQGEwJVUzETMBEGA1UECAwKQ2FsaWZvcm5pYTEWMBQG
A1UEBwwNU2FuIEZyYW5jaXNjbzEUMBIGA1UECgwLbXlzcWwtbWltaWMxFDASBgNV
BAsMC215c3FsLW1pbWljMRQwEgYDVQQDDAtteXNxbC1taW1pYzEhMB8GCSqGSIb3
DQEJARYSZXhhbXBsZUBkb21haW4uY29tMIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8A
MIIBCgKCAQEAsnmS0k1Q0f+mv7izbT5K8zqYP54KwyW000qMhYLW/Ir5fGAw2QHg
Q1vMBY+1oaHcO1DxjriG4l8P8ho9AShAD63Q3PVUiy3Prxi3PaZ/jPsI5rN/8s7s
TXai6Po6gD56uYtZACl4cjF2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vLtJ3Yr767YWSvPh9SmNSBEeQGibs9JHmCp9niayve673/H8Y2
XkCBZ/iDPwpCyaZglAbqLRViSltbYtOPtaZbNAxxhQKBgH8Gj0yg3A5DwWEyeUKR
KJBS2rPiKgrhJs8Kd8GZZUb3H5WGqzfgrRK1p9j5Ug8UXSWbB9e5jg4ymVtcAAhc
tRz4rCvYNY8fHlA2TfzrOeEuuxtoMFyxd26eFjZvS/w2VdFrIQZbSvDkV4hMqhpS
CCslSFIIZOMCzqgQtM+NTQJ6
-----END PRIVATE KEY-----
"""

async def start_tls(reader, writer, sslcontext, server_side=False):
    # Borrowed from https://stackoverflow.com/questions/62851407/how-do-i-enable-tls-on-an-already-connected-python-asyncio-stream
    transport = writer.transport
    protocol = transport.get_protocol()
    loop = asyncio.get_event_loop()
    new_transport = await loop.start_tls(
        transport=transport,
        protocol=protocol,
        sslcontext=sslcontext,
        server_side=server_side,
        ssl_handshake_timeout=5,  # arbitrarily small timeout so the test fails faster
    )
    reader._transport = new_transport
    writer._transport = new_transport

async def server_cb(reader, writer):
    # Sleeping 1 second seems to reliably reproduce the error
    await asyncio.sleep(1)

    sslcontext = ssl.SSLContext(ssl.PROTOCOL_TLS_SERVER)
    sslcontext.load_cert_chain(CERT_FILE, KEY_FILE)
    await start_tls(reader, writer, sslcontext, server_side=True)

    # We never get here
    print(await reader.read(4))
    writer.write(b"pong")
    await writer.drain()

async def client(reader, writer):
    sslcontext = ssl.SSLContext(ssl.PROTOCOL_TLS_CLIENT)
    sslcontext.check_hostname = False
    sslcontext.verify_mode = ssl.VerifyMode.CERT_NONE
    await start_tls(reader, writer, sslcontext, server_side=False)

    # We never get here
    writer.write(b"ping")
    await writer.drain()
    print(await reader.read(4))

async def main():
    with open(CERT_FILE, "w") as fp:
        fp.write(CERT)
    with open(KEY_FILE, "w") as fp:
        fp.write(KEY)

    server = await asyncio.start_server(
        client_connected_cb=server_cb,
        host="localhost",
        port=0,  # Let the OS pick an available port
    )
    task = asyncio.create_task(server.serve_forever())
    try:
        port = server.sockets[0].getsockname()[1]
        reader, writer = await asyncio.open_connection(host="localhost", port=port)
        await client(reader, writer)
        writer.close()
        await writer.wait_closed()
    finally:
        task.cancel()

asyncio.run(main())

输出:

Task exception was never retrieved
future: <Task finished name='Task-5' coro=<server_cb() done, defined at /Users/barak_alon/Library/Application Support/JetBrains/PyCharmCE2022.2/scratches/tls_race_condition.py:82> exception=ConnectionResetError()>
Traceback (most recent call last):
  File "/Users/barak_alon/Library/Application Support/JetBrains/PyCharmCE2022.2/scratches/tls_race_condition.py", line 88, in server_cb
    await start_tls(reader, writer, sslcontext, server_side=True)
  File "/Users/barak_alon/Library/Application Support/JetBrains/PyCharmCE2022.2/scratches/tls_race_condition.py", line 71, in start_tls
    new_transport = await loop.start_tls(
  File "/Users/barak_alon/.pyenv/versions/3.9.7/lib/python3.9/asyncio/base_events.py", line 1231, in start_tls
    await waiter
ConnectionResetError
Traceback (most recent call last):
  File "/Users/barak_alon/Library/Application Support/JetBrains/PyCharmCE2022.2/scratches/tls_race_condition.py", line 129, in <module>
    asyncio.run(main())
  File "/Users/barak_alon/.pyenv/versions/3.9.7/lib/python3.9/asyncio/runners.py", line 44, in run
    return loop.run_until_complete(main)
  File "/Users/barak_alon/.pyenv/versions/3.9.7/lib/python3.9/asyncio/base_events.py", line 642, in run_until_complete
    return future.result()
  File "/Users/barak_alon/Library/Application Support/JetBrains/PyCharmCE2022.2/scratches/tls_race_condition.py", line 123, in main
    await client(reader, writer)
  File "/Users/barak_alon/Library/Application Support/JetBrains/PyCharmCE2022.2/scratches/tls_race_condition.py", line 100, in client
    await start_tls(reader, writer, sslcontext, server_side=False)
  File "/Users/barak_alon/Library/Application Support/JetBrains/PyCharmCE2022.2/scratches/tls_race_condition.py", line 71, in start_tls
    new_transport = await loop.start_tls(
  File "/Users/barak_alon/.pyenv/versions/3.9.7/lib/python3.9/asyncio/base_events.py", line 1231, in start_tls
    await waiter
ConnectionAbortedError: SSL handshake is taking longer than 5 seconds: aborting the connection

有人知道这里发生了什么吗?有没有不需要定制asyncio Protocol的修复程序?

xggvc2p6

xggvc2p61#

在尝试将传输提升到TLS之前调用.pause_reading()可能会有所帮助。

相关问题