[php-src] PHP-8.5.11: Fix GHSA-vvx9-73fr-5jjx: do not fall back to CN if the cert has a service ID

From: Date: Tue, 22 Sep 2026 13:25:47 +0000
Subject: [php-src] PHP-8.5.11: Fix GHSA-vvx9-73fr-5jjx: do not fall back to CN if the cert has a service ID
Groups: php.cvs 
Request: Send a blank email to php-cvs+get-141413@lists.php.net to get a copy of this message
Author: Jakub Zelenka (bukka) Pusher: edorian Date: 2026-09-20T14:40:35+02:00 Commit: https://github.com/php/php-src/commit/2638f6515915b1468b499ad2eb96f49a0772bb80 Raw diff: https://github.com/php/php-src/commit/2638f6515915b1468b499ad2eb96f49a0772bb80.diff Fix GHSA-vvx9-73fr-5jjx: do not fall back to CN if the cert has a service ID RFC 6125 6.4.4 forbids matching CN-ID if the certificate presents a DNS-ID, SRV-ID or URI-ID. Falling back allowed a CA that validated only one of those identities to leave an unvalidated CN to be matched as a host name. IP-ID is left out as it is out of scope of RFC 6125 and suppressing CN there would break IP-only SAN certificates. Changed paths: A ext/openssl/tests/san_absent_cn_fallback.phpt A ext/openssl/tests/san_no_cn_fallback.phpt A ext/openssl/tests/san_srv_no_cn_fallback.phpt A ext/openssl/tests/san_uri_no_cn_fallback.phpt M ext/openssl/tests/san_peer_matching.phpt M ext/openssl/xp_ssl.c Diff: diff --git a/ext/openssl/tests/san_absent_cn_fallback.phpt b/ext/openssl/tests/san_absent_cn_fallback.phpt new file mode 100644 index 000000000000..30f4821b5fe5 --- /dev/null +++ b/ext/openssl/tests/san_absent_cn_fallback.phpt @@ -0,0 +1,61 @@ +--TEST-- +Peer verification falls back to CN when the certificate has no SAN +--EXTENSIONS-- +openssl +--SKIPIF-- +<?php +if (!function_exists("proc_open")) die("skip no proc_open"); +?> +--FILE-- +<?php +$certFile = __DIR__ . DIRECTORY_SEPARATOR . 'san_absent_cn_fallback.pem.tmp'; + +$serverCode = <<<'CODE' + $serverUri = "ssl://127.0.0.1:0"; + $serverFlags = STREAM_SERVER_BIND | STREAM_SERVER_LISTEN; + $serverCtx = stream_context_create(['ssl' => [ + 'local_cert' => '%s', + ]]); + + $server = stream_socket_server($serverUri, $errno, $errstr, $serverFlags, $serverCtx); + phpt_notify_server_start($server); + + @stream_socket_accept($server, 1); + @stream_socket_accept($server, 1); +CODE; +$serverCode = sprintf($serverCode, $certFile); + +$clientCode = <<<'CODE' + $serverUri = "ssl://{{ ADDR }}"; + $clientFlags = STREAM_CLIENT_CONNECT; + $clientCtx = stream_context_create(['ssl' => [ + 'verify_peer' => false, + ]]); + + stream_context_set_option($clientCtx, 'ssl', 'peer_name', 'cn.example.org'); + var_dump(stream_socket_client($serverUri, $errno, $errstr, 1, $clientFlags, $clientCtx)); + + stream_context_set_option($clientCtx, 'ssl', 'peer_name', 'other.example.org'); + var_dump(stream_socket_client($serverUri, $errno, $errstr, 1, $clientFlags, $clientCtx)); +CODE; + +include 'CertificateGenerator.inc'; +$certificateGenerator = new CertificateGenerator(); +$certificateGenerator->saveNewCertAsFileWithKey('cn.example.org', $certFile); + +include 'ServerClientTestCase.inc'; +ServerClientTestCase::getInstance()->run($clientCode, $serverCode); +?> +--CLEAN-- +<?php +@unlink(__DIR__ . DIRECTORY_SEPARATOR . 'san_absent_cn_fallback.pem.tmp'); +?> +--EXPECTF-- +resource(%d) of type (stream) + +Warning: stream_socket_client(): Peer certificate CN=cn.example.org' did not match expected CN=other.example.org' in %s on line %d + +Warning: stream_socket_client(): Failed to enable crypto in %s on line %d + +Warning: stream_socket_client(): Unable to connect to ssl://127.0.0.1:%d (Unknown error) in %s on line %d +bool(false) diff --git a/ext/openssl/tests/san_no_cn_fallback.phpt b/ext/openssl/tests/san_no_cn_fallback.phpt new file mode 100644 index 000000000000..ae19be414ee7 --- /dev/null +++ b/ext/openssl/tests/san_no_cn_fallback.phpt @@ -0,0 +1,64 @@ +--TEST-- +Peer verification does not fall back to CN when the certificate has a SAN +--EXTENSIONS-- +openssl +--SKIPIF-- +<?php +if (!function_exists("proc_open")) die("skip no proc_open"); +?> +--FILE-- +<?php +$certFile = __DIR__ . DIRECTORY_SEPARATOR . 'san_no_cn_fallback.pem.tmp'; + +$serverCode = <<<'CODE' + $serverUri = "ssl://127.0.0.1:0"; + $serverFlags = STREAM_SERVER_BIND | STREAM_SERVER_LISTEN; + $serverCtx = stream_context_create(['ssl' => [ + 'local_cert' => '%s', + ]]); + + $server = stream_socket_server($serverUri, $errno, $errstr, $serverFlags, $serverCtx); + phpt_notify_server_start($server); + + @stream_socket_accept($server, 1); + @stream_socket_accept($server, 1); +CODE; +$serverCode = sprintf($serverCode, $certFile); + +$clientCode = <<<'CODE' + $serverUri = "ssl://{{ ADDR }}"; + $clientFlags = STREAM_CLIENT_CONNECT; + $clientCtx = stream_context_create(['ssl' => [ + 'verify_peer' => false, + ]]); + + // The SAN entry is matched as usual. + stream_context_set_option($clientCtx, 'ssl', 'peer_name', 'san.example.org'); + var_dump(stream_socket_client($serverUri, $errno, $errstr, 1, $clientFlags, $clientCtx)); + + // The CN must not be considered because the certificate presents a SAN. + stream_context_set_option($clientCtx, 'ssl', 'peer_name', 'cn.example.org'); + var_dump(stream_socket_client($serverUri, $errno, $errstr, 1, $clientFlags, $clientCtx)); +CODE; + +include 'CertificateGenerator.inc'; +$certificateGenerator = new CertificateGenerator(); +$certificateGenerator->saveNewCertAsFileWithKey( + 'cn.example.org', $certFile, null, 'DNS:san.example.org'); + +include 'ServerClientTestCase.inc'; +ServerClientTestCase::getInstance()->run($clientCode, $serverCode); +?> +--CLEAN-- +<?php +@unlink(__DIR__ . DIRECTORY_SEPARATOR . 'san_no_cn_fallback.pem.tmp'); +?> +--EXPECTF-- +resource(%d) of type (stream) + +Warning: stream_socket_client(): Peer certificate subjectAltName did not match expected name `cn.example.org' in %s on line %d + +Warning: stream_socket_client(): Failed to enable crypto in %s on line %d + +Warning: stream_socket_client(): Unable to connect to ssl://127.0.0.1:%d (Unknown error) in %s on line %d +bool(false) diff --git a/ext/openssl/tests/san_peer_matching.phpt b/ext/openssl/tests/san_peer_matching.phpt index 0df18f4a9250..cca4d360d8bd 100644 --- a/ext/openssl/tests/san_peer_matching.phpt +++ b/ext/openssl/tests/san_peer_matching.phpt @@ -54,7 +54,7 @@ ServerClientTestCase::getInstance()->run($clientCode, $serverCode); --EXPECTF-- resource(%d) of type (stream) -Warning: stream_socket_client(): Unable to locate peer certificate CN in %s on line %d +Warning: stream_socket_client(): Peer certificate subjectAltName did not match expected name `moar.example.org' in %s on line %d Warning: stream_socket_client(): Failed to enable crypto in %s on line %d diff --git a/ext/openssl/tests/san_srv_no_cn_fallback.phpt b/ext/openssl/tests/san_srv_no_cn_fallback.phpt new file mode 100644 index 000000000000..9c130e757b78 --- /dev/null +++ b/ext/openssl/tests/san_srv_no_cn_fallback.phpt @@ -0,0 +1,57 @@ +--TEST-- +Peer verification does not fall back to CN when the certificate has an SRV-ID +--EXTENSIONS-- +openssl +--SKIPIF-- +<?php +if (!function_exists("proc_open")) die("skip no proc_open"); +?> +--FILE-- +<?php +$certFile = __DIR__ . DIRECTORY_SEPARATOR . 'san_srv_no_cn_fallback.pem.tmp'; + +$serverCode = <<<'CODE' + $serverUri = "ssl://127.0.0.1:0"; + $serverFlags = STREAM_SERVER_BIND | STREAM_SERVER_LISTEN; + $serverCtx = stream_context_create(['ssl' => [ + 'local_cert' => '%s', + ]]); + + $server = stream_socket_server($serverUri, $errno, $errstr, $serverFlags, $serverCtx); + phpt_notify_server_start($server); + + @stream_socket_accept($server, 1); +CODE; +$serverCode = sprintf($serverCode, $certFile); + +$clientCode = <<<'CODE' + $serverUri = "ssl://{{ ADDR }}"; + $clientFlags = STREAM_CLIENT_CONNECT; + $clientCtx = stream_context_create(['ssl' => [ + 'verify_peer' => false, + ]]); + + // The CN must not be considered because the certificate presents an SRV-ID. + stream_context_set_option($clientCtx, 'ssl', 'peer_name', 'cn.example.org'); + var_dump(stream_socket_client($serverUri, $errno, $errstr, 1, $clientFlags, $clientCtx)); +CODE; + +include 'CertificateGenerator.inc'; +$certificateGenerator = new CertificateGenerator(); +$certificateGenerator->saveNewCertAsFileWithKey( + 'cn.example.org', $certFile, null, 'otherName:1.3.6.1.5.5.7.8.7;IA5STRING:_https.san.example.org'); + +include 'ServerClientTestCase.inc'; +ServerClientTestCase::getInstance()->run($clientCode, $serverCode); +?> +--CLEAN-- +<?php +@unlink(__DIR__ . DIRECTORY_SEPARATOR . 'san_srv_no_cn_fallback.pem.tmp'); +?> +--EXPECTF-- +Warning: stream_socket_client(): Peer certificate subjectAltName did not match expected name `cn.example.org' in %s on line %d + +Warning: stream_socket_client(): Failed to enable crypto in %s on line %d + +Warning: stream_socket_client(): Unable to connect to ssl://127.0.0.1:%d (Unknown error) in %s on line %d +bool(false) diff --git a/ext/openssl/tests/san_uri_no_cn_fallback.phpt b/ext/openssl/tests/san_uri_no_cn_fallback.phpt new file mode 100644 index 000000000000..b621d52312bc --- /dev/null +++ b/ext/openssl/tests/san_uri_no_cn_fallback.phpt @@ -0,0 +1,57 @@ +--TEST-- +Peer verification does not fall back to CN when the certificate has a URI-ID +--EXTENSIONS-- +openssl +--SKIPIF-- +<?php +if (!function_exists("proc_open")) die("skip no proc_open"); +?> +--FILE-- +<?php +$certFile = __DIR__ . DIRECTORY_SEPARATOR . 'san_uri_no_cn_fallback.pem.tmp'; + +$serverCode = <<<'CODE' + $serverUri = "ssl://127.0.0.1:0"; + $serverFlags = STREAM_SERVER_BIND | STREAM_SERVER_LISTEN; + $serverCtx = stream_context_create(['ssl' => [ + 'local_cert' => '%s', + ]]); + + $server = stream_socket_server($serverUri, $errno, $errstr, $serverFlags, $serverCtx); + phpt_notify_server_start($server); + + @stream_socket_accept($server, 1); +CODE; +$serverCode = sprintf($serverCode, $certFile); + +$clientCode = <<<'CODE' + $serverUri = "ssl://{{ ADDR }}"; + $clientFlags = STREAM_CLIENT_CONNECT; + $clientCtx = stream_context_create(['ssl' => [ + 'verify_peer' => false, + ]]); + + // The CN must not be considered because the certificate presents a URI-ID. + stream_context_set_option($clientCtx, 'ssl', 'peer_name', 'cn.example.org'); + var_dump(stream_socket_client($serverUri, $errno, $errstr, 1, $clientFlags, $clientCtx)); +CODE; + +include 'CertificateGenerator.inc'; +$certificateGenerator = new CertificateGenerator(); +$certificateGenerator->saveNewCertAsFileWithKey( + 'cn.example.org', $certFile, null, 'URI:https://san.example.org/x'); + +include 'ServerClientTestCase.inc'; +ServerClientTestCase::getInstance()->run($clientCode, $serverCode); +?> +--CLEAN-- +<?php +@unlink(__DIR__ . DIRECTORY_SEPARATOR . 'san_uri_no_cn_fallback.pem.tmp'); +?> +--EXPECTF-- +Warning: stream_socket_client(): Peer certificate subjectAltName did not match expected name `cn.example.org' in %s on line %d + +Warning: stream_socket_client(): Failed to enable crypto in %s on line %d + +Warning: stream_socket_client(): Unable to connect to ssl://127.0.0.1:%d (Unknown error) in %s on line %d +bool(false) diff --git a/ext/openssl/xp_ssl.c b/ext/openssl/xp_ssl.c index 1398c2e23fe8..a274d875502d 100644 --- a/ext/openssl/xp_ssl.c +++ b/ext/openssl/xp_ssl.c @@ -444,7 +444,8 @@ static bool php_openssl_matches_wildcard_name(const char *subjectname, const cha } /* }}} */ -static bool php_openssl_matches_san_list(X509 *peer, const char *subject_name) /* {{{ */ +static bool php_openssl_matches_san_list( + X509 *peer, const char *subject_name, bool *has_service_id) { int i, len; unsigned char *cert_name = NULL; @@ -453,6 +454,8 @@ static bool php_openssl_matches_san_list(X509 *peer, const char *subject_name) / GENERAL_NAMES *alt_names = X509_get_ext_d2i(peer, NID_subject_alt_name, 0, 0); int alt_name_count = sk_GENERAL_NAME_num(alt_names); + *has_service_id = false; + #ifdef HAVE_IPV6_SAN /* detect if subject name is an IPv6 address and expand once if required */ char subject_name_ipv6_expanded[40]; @@ -470,6 +473,8 @@ static bool php_openssl_matches_san_list(X509 *peer, const char *subject_name) / GENERAL_NAME *san = sk_GENERAL_NAME_value(alt_names, i); if (san->type == GEN_DNS) { + *has_service_id = true; + if (ASN1_STRING_to_UTF8(&cert_name, san->d.dNSName) < 0) { /* TODO: warn ? */ continue; @@ -518,6 +523,16 @@ static bool php_openssl_matches_san_list(X509 *peer, const char *subject_name) / } } #endif + } else if (san->type == GEN_URI) { + *has_service_id = true; + } else if (san->type == GEN_OTHERNAME) { + char oid[32]; + + /* SRV-ID, matched by OID because NID_SRVName needs OpenSSL 3.0 */ + if (OBJ_obj2txt(oid, sizeof(oid), san->d.otherName->type_id, 1) > 0 + && strcmp(oid, "1.3.6.1.5.5.7.8.7") == 0) { + *has_service_id = true; + } } } @@ -525,7 +540,6 @@ static bool php_openssl_matches_san_list(X509 *peer, const char *subject_name) / return 0; } -/* }}} */ static bool php_openssl_matches_common_name(X509 *peer, const char *subject_name) /* {{{ */ { @@ -632,8 +646,16 @@ static zend_result php_openssl_apply_peer_verification_policy(SSL *ssl, X509 *pe } if (peer_name) { - if (php_openssl_matches_san_list(peer, peer_name)) { + bool has_service_id = false; + + if (php_openssl_matches_san_list(peer, peer_name, &has_service_id)) { return SUCCESS; + } else if (has_service_id) { + /* CN must be ignored if the certificate presents a service identity. */ + php_error_docref(NULL, E_WARNING, + "Peer certificate subjectAltName did not match expected name `%s'", + peer_name); + return FAILURE; } else if (php_openssl_matches_common_name(peer, peer_name)) { return SUCCESS; } else {

« previous php.cvs (#141413) next »