Skip to main content
Menu
Advisories

Improper certificate validation

GHSA-3VRH-X3C6-5H36Published Updated GitHub

Description

Impact

PHP gives every stream returned by stream_socket_accept() the stream context of the listening socket, and Psl\TCP\listen() accepted connections that way, so all connections accepted from one listener shared one stream context. Psl\TLS\Acceptor::accept(), Psl\TLS\Listener::accept() and Psl\TLS\ClientHello::complete() (the second step of Psl\TLS\LazyAcceptor) write the server's TLS options into that context before each handshake, and PHP captures the client certificate into the same context. This caused three flaws:

  1. Another client's identity. PHP stores peer_certificate and peer_certificate_chain in the context only when the client presents a certificate, and never clears them. A client that presented no certificate was reported by Psl\TLS\StreamInterface::getState() with the peerCertificate and peerCertificateChain of the last client that presented one on the same listener. Overlapping handshakes on one listener could also report each other's certificate.
  2. Stale certificate authority. Context options are merged and cannot be removed. A ServerConfiguration without certificateAuthority or certificateAuthorityPath, used after a configuration that set one on the same listener (for example a per-host configuration chosen through LazyAcceptor), kept verifying client certificates against the earlier authority, and accepted the certificates it issued.
  3. Stale allow_self_signed and other trust options. A client handshake on a stream accepted from the listener (Psl\TLS\Connector::connect() with allowSelfSigned), or user code setting ssl context options on such a stream, left options such as allow_self_signed and verify_depth in the shared context. With a stale allow_self_signed, a server configured to trust one authority accepted any self-signed client certificate, with any subject, as the peer.

PHP's server-side verification asks for a client certificate but does not require one, so a PSL server that requires mutual TLS does it by checking getState()->peerCertificate, and identifies the client from it. A server that authenticates or authorizes clients this way is affected: through the first flaw, a remote client with no certificate and no credentials is treated as the last client that authenticated with a certificate on the same listener. The attacker cannot choose which identity it receives, and needs at least one earlier handshake with a client certificate on that listener. The second and third flaws need an application that uses more than one server configuration on one listener, or that runs client handshakes or sets ssl context options on streams accepted from it. Servers that do not use client certificates are not affected.

The shared context comes from PHP's accept path, so every supported PHP version is affected, in every release since TLS servers on Psl\TCP listeners shipped in 5.0.0 (then published as azjezz/psl).

Patches

Fixed in 6.4.0.

  • When ext-sockets is available (the tcp and tls packages now suggest it), Psl\TCP\listen() accepts connections with socket_accept() and socket_export_stream(), which gives every accepted connection a private stream context.
  • Server handshakes reset peer_certificate and peer_certificate_chain before each handshake, read the connection state in the same step that completes the handshake, and remove the captured certificates from the context afterwards, so a client without a certificate is reported with none, and overlapping handshakes each report their own peer, with or without ext-sockets. Read the peer certificate from Psl\TLS\StreamInterface::getState(), not from the stream context.
  • Without ext-sockets, a server upgrade refuses a stream whose context carries cafile, capath, verify_peer, verify_peer_name, peer_fingerprint, allow_self_signed or verify_depth that the current ServerConfiguration does not set, with a Psl\Network\Exception\RuntimeException naming the option, instead of applying another handshake's trust settings.

Upgrade php-standard-library/tcp together with php-standard-library/tls, since the private context comes from the tcp package. Without ext-sockets, the options of an earlier configuration that do not decide trust (ciphers, alpnProtocols, sniCertificates, a key passphrase, disabled session tickets) still apply to a later configuration on the same listener; install ext-sockets, or use one listener per ServerConfiguration.

Workarounds

Upgrading is recommended. On affected versions:

  • Before each server handshake, clear the captured certificate on the accepted stream with stream_context_set_option($stream->getStream(), 'ssl', 'peer_certificate', null) and the same call for peer_certificate_chain, and do not run the handshakes of one listener concurrently. With Psl\TLS\Listener, do this in a Psl\Network\ListenerInterface that wraps the inner listener.
  • Use one listener per ServerConfiguration, and do not run client handshakes or set ssl context options on streams accepted from a listener that serves TLS.

References

  • RFC 8446 section 4.4.2 (TLS 1.3 Certificate message: a client may answer a certificate request with an empty certificate list)
  • RFC 5246 section 7.4.6 (TLS 1.2 client Certificate message)
  • PHP source: main/streams/xp_socket.c and ext/openssl/xp_ssl.c (accepted streams share the listening socket's context; php_openssl_capture_peer_certs writes the peer certificate into it only when one is presented)
  • github.com/php-standard-library/php-standard-library/releases/tag/6.4.0

Affected versions

References

GitHub · php-standard-library/php-standard-library