Decoupling ssl.SSLSocket from concrete socket.socket via a Transport Protocol / Interface

Hi everyone,

I would like to start an architectural discussion regarding the tight coupling between ssl.SSLSocket and the concrete socket.socket class, and explore whether introducing a formal transport abstraction (or typing.Protocol) could be beneficial for Python’s networking ecosystem.

Background & Motivation

Python’s networking and I/O ecosystems strongly favor duck typing, structural subtyping (typing.Protocol), and interface-based design. However, ssl.SSLSocket currently relies heavily on inheriting from the concrete socket.socket class.

While this works seamlessly for standard OS-backed BSD sockets, it creates friction for third-party libraries and advanced networking architectures that implement socket-compatible interfaces over alternative transport mechanisms without inheriting from CPython’s native socket.socket.

Abstract Use Cases

In modern systems and security engineering, there are several scenarios where TLS is needed over non-standard transports:

  1. Userspace Networking & Custom Overlays: Virtual network interfaces, custom secure tunnels, or userspace TCP/IP stacks where packet routing happens in user space rather than via standard kernel socket descriptors.
  2. In-Memory / Custom IPC Transports: High-security inter-process communication or isolated sandboxes that expose a stream/socket-like API without backing OS file descriptors.
  3. Deterministic Network Testing & Fuzzing: Advanced testing frameworks that simulate packet fragmentation, latency, or fault injection using pure-Python socket-like objects, where wrapping them directly with SSLContext.wrap_socket() is currently blocked by inheritance constraints.

(Note: While ssl.SSLObject with MemoryBIO exists for non-blocking/decoupled I/O, it requires developers to manually manage the TLS state machine, BIO buffers, and handshake loops—re-implementing logic that SSLSocket already provides out of the box for synchronous or socket-like workflows).

Design Concern

Because SSLSocket inherits directly from socket.socket, certain internal operations and C-level assumptions resolve directly against native socket structures rather than delegating through a defined interface. Consequently, simply matching the public API of socket.socket is often insufficient for third-party transports to interoperate cleanly with SSLContext.wrap_socket().

Potential Direction & Constraints

Rather than breaking backward compatibility or sacrificing C-level performance, could CPython consider:

  1. Defining a Formal Transport Contract: Documenting and defining a minimal Protocol / ABC representing the exact methods and lifecycle guarantees SSLSocket requires from an underlying transport (e.g., send, recv, getsockopt, timeout handling, etc.).
  2. Preserving the C Fast-Path: Keeping the existing C-level optimization intact when a concrete socket.socket is passed (ensuring zero performance regression for standard workloads), while allowing a delegation fallback path for custom objects implementing the transport contract.
  3. Clarifying fileno() Dependencies: Explicitly separating operations that strictly require an OS file descriptor (like select/poll integration) from core TLS stream operations.

Questions for the Maintainers

  • Would the maintainers be open to exploring a transport abstraction (or formal Protocol) to decouple SSLSocket from concrete socket.socket inheritance?
  • Are there known internal constraints in _ssl.c (beyond fileno() and buffer management) that make delegation to non-socket.socket objects impractical?

Thank you for your time and for maintaining Python’s core networking stack!

Could you please provide a detailed proposal?
What would you like to change? And how is it different from already supported MemoryBIO?
A code snippet would heal a lot.

1 Like

Thank you for asking for a more detailed proposal. After reviewing the implementation again, I realized that my initial report was not accurately scoped.

I believe the issue is not related to “_ssl.c” or the underlying TLS implementation. It is specifically about the Python-level “SSLSocket” wrapper in “Lib/ssl.py”.

What I would like to change

Currently, “SSLSocket” inherits from “socket.socket”, which means certain socket operations, such as “fileno()”, resolve against the native “socket.socket” implementation.

However, when a socket object is supplied through “SSLContext.wrap_socket(sock)”, I believe the wrapper should preserve and consistently use that supplied socket object as its transport, rather than implicitly relying on the concrete “socket.socket” implementation.

The goal is to make “SSLSocket” less dependent on the assumption that every supplied socket object is an instance of CPython’s native “socket.socket” class.

How is this different from MemoryBIO?

I understand that “SSLObject” with “MemoryBIO” already supports custom transport implementations.

However, that API requires the application to manage encrypted input and output buffers and explicitly transfer data between the transport and TLS layer.

My proposal has a different goal: preserve the existing socket-oriented programming model of “SSLSocket”, while allowing compatible third-party socket implementations to provide the underlying transport.

Expected behavior

Conceptually, I would like the socket operations used by “SSLSocket” to delegate to the socket object supplied to “wrap_socket()”, wherever appropriate.

This would allow third-party socket implementations to interoperate with the SSL wrapper without necessarily inheriting from “socket.socket”.

I apologize for initially pointing to the wrong implementation layer. I would appreciate feedback on whether this direction is feasible and how the existing “SSLSocket” architecture could accommodate it.

Can you not get an AI to write your posts please?

2 Likes

@Rosuav I’m not very fluent in writing long English sentences, so I use AI to translate.

Use a translation tool to directly translate your own words. Whatever tool you’re currently using makes it sound like you’ve used it to generate a response rather than only translate.

2 Likes

@davidism Okay, thank you for your input, friend.