Skip to content

stream_socket_enable_crypto ignores most ssl.* context options, and a cancelled TLS handshake leaks the SSL object #328

Open

Description

@blacktrs

Summary

stream_socket_enable_crypto() (client side, src/Runtime/Stdlib/Net.php) reads only ssl.verify_peer and ssl.verify_peer_name from the stream context (see __mc_ctx_unpack / stream_context_create in Io.php). php honours many more, so a program that STARTTLS-upgrades a connection (PDO pgsql/mysql, SMTP, IMAP, ...) cannot trust a private CA or present a client certificate through the standard API. Ignored today:

  • ssl.cafile, ssl.capath (only the host default store is ever used)
  • ssl.local_cert / ssl.local_pk on the CLIENT side (they only feed server-side accept)
  • ssl.ciphers
  • ssl.peer_name (the name checked and sent as SNI is always the connect host)
  • ssl.verify_depth
  • minimum / maximum protocol version (STREAM_CRYPTO_METHOD_TLSv1_2_CLIENT etc. map to the same highest-version method)
  • failures give no reason (no ssl warning text), only false

The PDO pgsql/mysql drivers work around this by driving OpenSSL themselves (__McPdoTls in prelude/pdo_sql.php, extra FFI thunks). Fixing the stdlib would let them drop that code.

Second bug: handshake cancel leaks the SSL object

__mc_tls_handshake (Net.php, ~lines 739-745) frees the SSL* only on the failure return paths. A cancellation raised while __mc_tls_drive_connect is parked in the handshake propagates as an exception past the sslFree, leaking the SSL* (and its session state). Same hole in __mc_tls_server_handshake. Fix: free on every non-success exit (try/catch Throwable + rethrow, or a success flag in a finally).

Repro

Not hermetic enough to minimise as a tests/aot/repro file in the time box: the first part needs a TLS fixture with a private CA (stream_socket_client('ssl://127.0.0.1:PORT', ..., ctx with ssl.cafile + ssl.peer_name) fails here and succeeds under php); the second needs a cancelled fiber parked in a handshake against a silent peer. tests/pdo_net/cases/pgsql_async_tls_cancel.php shows the cancel scenario end to end for the PDO path.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    bug

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions