Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
55 changes: 55 additions & 0 deletions ext/openssl/tests/alpn_protocols_invalid.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
--TEST--
Setting an invalid TLS ALPN protocol list on a client stream fails
--EXTENSIONS--
openssl
--SKIPIF--
<?php
if (OPENSSL_VERSION_NUMBER < 0x30000000) die('skip For OpenSSL >= 3.0');
?>
--FILE--
<?php
$server = stream_socket_server('tcp://127.0.0.1:0', $errno, $errstr);
$address = stream_socket_get_name($server, false);

function try_alpn($protocols, $streamOptions): void {
global $address;

$context = stream_context_create([
'ssl' => ['alpn_protocols' => $protocols, 'verify_peer' => false],
'stream' => $streamOptions,
]);
$client = stream_socket_client("tcp://$address", $errno, $errstr, 1, STREAM_CLIENT_CONNECT, $context);
var_dump(stream_socket_enable_crypto($client, true, STREAM_CRYPTO_METHOD_TLS_CLIENT));
fclose($client);
}

foreach (['', ',', 'h2,', ',h2', 'h2,,http/1.1'] as $protocols) {
try_alpn($protocols, []);
}

try_alpn('', [
'error_mode' => StreamErrorMode::Silent,
'error_store' => StreamErrorStore::All,
]);
foreach (stream_last_errors() as $error) {
var_dump($error->code, $error->message);
}
?>
--EXPECTF--
Warning: stream_socket_enable_crypto(): Failed setting TLS ALPN protocols, protocol names must not be empty in %s on line %d
bool(false)

Warning: stream_socket_enable_crypto(): Failed setting TLS ALPN protocols, protocol names must not be empty in %s on line %d
bool(false)

Warning: stream_socket_enable_crypto(): Failed setting TLS ALPN protocols, protocol names must not be empty in %s on line %d
bool(false)

Warning: stream_socket_enable_crypto(): Failed setting TLS ALPN protocols, protocol names must not be empty in %s on line %d
bool(false)

Warning: stream_socket_enable_crypto(): Failed setting TLS ALPN protocols, protocol names must not be empty in %s on line %d
bool(false)
bool(false)
enum(StreamErrorCode::DecodingFailed)
string(67) "Failed setting TLS ALPN protocols, protocol names must not be empty"
8 changes: 7 additions & 1 deletion ext/openssl/xp_ssl.c
Original file line number Diff line number Diff line change
Expand Up @@ -2654,7 +2654,13 @@ static zend_result php_openssl_create_server_ctx(php_stream *stream,
return FAILURE;
}
if (sslsock->is_client) {
SSL_CTX_set_alpn_protos(sslsock->ctx, alpn, alpn_len);
if (SSL_CTX_set_alpn_protos(sslsock->ctx, alpn, alpn_len)) {
php_stream_warn(stream, DecodingFailed, "Failed setting TLS ALPN protocols, protocol names must not be empty");
efree(alpn);
SSL_CTX_free(sslsock->ctx);
sslsock->ctx = NULL;
return FAILURE;
}
} else {
sslsock->alpn_ctx.data = (unsigned char *) pestrndup((const char*)alpn, alpn_len, php_stream_is_persistent(stream));
sslsock->alpn_ctx.len = alpn_len;
Expand Down
Loading