Improper certificate validation
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:
- Another client's identity. PHP stores
peer_certificateandpeer_certificate_chainin the context only when the client presents a certificate, and never clears them. A client that presented no certificate was reported byPsl\TLS\StreamInterface::getState()with thepeerCertificateandpeerCertificateChainof the last client that presented one on the same listener. Overlapping handshakes on one listener could also report each other's certificate. - Stale certificate authority. Context options are merged and cannot be removed. A
ServerConfigurationwithoutcertificateAuthorityorcertificateAuthorityPath, used after a configuration that set one on the same listener (for example a per-host configuration chosen throughLazyAcceptor), kept verifying client certificates against the earlier authority, and accepted the certificates it issued. - Stale
allow_self_signedand other trust options. A client handshake on a stream accepted from the listener (Psl\TLS\Connector::connect()withallowSelfSigned), or user code settingsslcontext options on such a stream, left options such asallow_self_signedandverify_depthin the shared context. With a staleallow_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
tcpandtlspackages now suggest it),Psl\TCP\listen()accepts connections withsocket_accept()andsocket_export_stream(), which gives every accepted connection a private stream context. - Server handshakes reset
peer_certificateandpeer_certificate_chainbefore 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 fromPsl\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_signedorverify_depththat the currentServerConfigurationdoes not set, with aPsl\Network\Exception\RuntimeExceptionnaming 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 forpeer_certificate_chain, and do not run the handshakes of one listener concurrently. WithPsl\TLS\Listener, do this in aPsl\Network\ListenerInterfacethat wraps the inner listener. - Use one listener per
ServerConfiguration, and do not run client handshakes or setsslcontext 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.candext/openssl/xp_ssl.c(accepted streams share the listening socket's context;php_openssl_capture_peer_certswrites the peer certificate into it only when one is presented) - github.com/
php-standard-library/ php-standard-library/ releases/ tag/ 6.4.0