Skip to content
Open
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
35 changes: 35 additions & 0 deletions ext/openssl/tests/stream_socket_sendto_tls.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
--TEST--
stream_socket_sendto() on a stream with TLS enabled sends the data through the TLS session
--EXTENSIONS--
openssl
--FILE--
<?php
$pem = __DIR__ . DIRECTORY_SEPARATOR . 'stream_socket_sendto_tls.pem.tmp';

include 'CertificateGenerator.inc';
(new CertificateGenerator())->saveNewCertAsFileWithKey('stream_socket_sendto_tls', $pem);

$listener = stream_socket_server('tcp://127.0.0.1:0', context: stream_context_create(['ssl' => ['local_cert' => $pem]]));
$client = stream_socket_client('tcp://' . stream_socket_get_name($listener, false), context: stream_context_create(['ssl' => ['verify_peer' => false, 'verify_peer_name' => false]]));
$server = stream_socket_accept($listener);
stream_set_blocking($client, false);
stream_set_blocking($server, false);
do {
$clientDone = stream_socket_enable_crypto($client, true, STREAM_CRYPTO_METHOD_TLS_CLIENT);
$serverDone = stream_socket_enable_crypto($server, true, STREAM_CRYPTO_METHOD_TLS_SERVER);
} while ($clientDone === 0 || $serverDone === 0);

var_dump(fwrite($client, 'ENCRYPTED'));
var_dump(fread($server, 8192));
var_dump(stream_socket_sendto($client, 'SENT WITH SENDTO'));
var_dump(@fread($server, 8192));
?>
--CLEAN--
<?php
@unlink(__DIR__ . DIRECTORY_SEPARATOR . 'stream_socket_sendto_tls.pem.tmp');
?>
--EXPECT--
int(9)
string(9) "ENCRYPTED"
int(16)
string(16) "SENT WITH SENDTO"
38 changes: 38 additions & 0 deletions ext/openssl/xp_ssl.c
Original file line number Diff line number Diff line change
Expand Up @@ -2732,6 +2732,44 @@ static int php_openssl_sockop_set_option(php_stream *stream, int option, int val

return PHP_STREAM_OPTION_RETURN_OK;

Comment thread
lacatoire marked this conversation as resolved.
Resolved
case STREAM_XPORT_OP_SEND:
if (sslsock->ssl_active) {
if (xparam->inputs.flags & STREAM_OOB) {
php_error_docref(NULL, E_WARNING, "Cannot write OOB data on an encrypted stream");
xparam->outputs.returncode = -1;
} else if (xparam->inputs.buflen == 0) {
xparam->outputs.returncode = 0;
} else {
xparam->outputs.returncode = (int) php_openssl_sockop_write(stream,
xparam->inputs.buf, MIN(xparam->inputs.buflen, INT_MAX));
}
return PHP_STREAM_OPTION_RETURN_OK;
}
break;

case STREAM_XPORT_OP_RECV:
if (sslsock->ssl_active) {
if (xparam->inputs.flags & STREAM_OOB) {
php_error_docref(NULL, E_WARNING, "Cannot read OOB data on an encrypted stream");
xparam->outputs.returncode = -1;
} else if (xparam->inputs.flags & STREAM_PEEK) {
int n;

ERR_clear_error();
n = SSL_peek(sslsock->ssl_handle, xparam->inputs.buf,
(int) MIN(xparam->inputs.buflen, INT_MAX));
if (n <= 0) {
n = SSL_get_error(sslsock->ssl_handle, n) == SSL_ERROR_ZERO_RETURN ? 0 : -1;
}
xparam->outputs.returncode = n;
} else {
xparam->outputs.returncode = (int) php_openssl_sockop_read(stream,
xparam->inputs.buf, MIN(xparam->inputs.buflen, INT_MAX));
}
return PHP_STREAM_OPTION_RETURN_OK;
}
break;

default:
/* fall through */
break;
Expand Down
Loading