Skip to content

Micro optimizations for xp_ssl.c #7447

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Sep 3, 2021
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
1 change: 0 additions & 1 deletion ext/openssl/tests/sni_server.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ if (!function_exists("proc_open")) die("skip no proc_open");
$serverCode = <<<'CODE'
$flags = STREAM_SERVER_BIND|STREAM_SERVER_LISTEN;
$ctx = stream_context_create(['ssl' => [
'local_cert' => __DIR__ . '/domain1.pem',
'SNI_server_certs' => [
"cs.php.net" => __DIR__ . "/sni_server_cs.pem",
"uk.php.net" => __DIR__ . "/sni_server_uk.pem",
Expand Down
1 change: 0 additions & 1 deletion ext/openssl/tests/sni_server_key_cert.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ if (!function_exists("proc_open")) die("skip no proc_open");
$serverCode = <<<'CODE'
$flags = STREAM_SERVER_BIND|STREAM_SERVER_LISTEN;
$ctx = stream_context_create(['ssl' => [
'local_cert' => __DIR__ . '/domain1.pem',
'SNI_server_certs' => [
"cs.php.net" => [
'local_cert' => __DIR__ . "/sni_server_cs_cert.pem",
Expand Down
67 changes: 30 additions & 37 deletions ext/openssl/xp_ssl.c
Original file line number Diff line number Diff line change
Expand Up @@ -897,35 +897,30 @@ static int php_openssl_set_local_cert(SSL_CTX *ctx, php_stream *stream) /* {{{ *
char resolved_path_buff[MAXPATHLEN];
const char *private_key = NULL;

if (VCWD_REALPATH(certfile, resolved_path_buff)) {
/* a certificate to use for authentication */
if (SSL_CTX_use_certificate_chain_file(ctx, resolved_path_buff) != 1) {
php_error_docref(NULL, E_WARNING,
"Unable to set local cert chain file `%s'; Check that your cafile/capath "
"settings include details of your certificate and its issuer",
certfile);
return FAILURE;
}
GET_VER_OPT_STRING("local_pk", private_key);

if (private_key) {
char resolved_path_buff_pk[MAXPATHLEN];
if (VCWD_REALPATH(private_key, resolved_path_buff_pk)) {
if (SSL_CTX_use_PrivateKey_file(ctx, resolved_path_buff_pk, SSL_FILETYPE_PEM) != 1) {
php_error_docref(NULL, E_WARNING, "Unable to set private key file `%s'", resolved_path_buff_pk);
return FAILURE;
}
}
} else {
if (SSL_CTX_use_PrivateKey_file(ctx, resolved_path_buff, SSL_FILETYPE_PEM) != 1) {
php_error_docref(NULL, E_WARNING, "Unable to set private key file `%s'", resolved_path_buff);
return FAILURE;
}
}
if (!VCWD_REALPATH(certfile, resolved_path_buff)) {
php_error_docref(NULL, E_WARNING, "Unable to get real path of certificate file `%s'", certfile);
return FAILURE;
}
/* a certificate to use for authentication */
if (SSL_CTX_use_certificate_chain_file(ctx, resolved_path_buff) != 1) {
php_error_docref(NULL, E_WARNING,
"Unable to set local cert chain file `%s'; Check that your cafile/capath "
"settings include details of your certificate and its issuer",
certfile);
return FAILURE;
}

if (!SSL_CTX_check_private_key(ctx)) {
php_error_docref(NULL, E_WARNING, "Private key does not match certificate!");
}
GET_VER_OPT_STRING("local_pk", private_key);
if (private_key && !VCWD_REALPATH(private_key, resolved_path_buff)) {
php_error_docref(NULL, E_WARNING, "Unable to get real path of private key file `%s'", private_key);
return FAILURE;
}
if (SSL_CTX_use_PrivateKey_file(ctx, resolved_path_buff, SSL_FILETYPE_PEM) != 1) {
php_error_docref(NULL, E_WARNING, "Unable to set private key file `%s'", resolved_path_buff);
return FAILURE;
}
if (!SSL_CTX_check_private_key(ctx)) {
php_error_docref(NULL, E_WARNING, "Private key does not match certificate!");
}
}

Expand Down Expand Up @@ -1614,11 +1609,16 @@ int php_openssl_setup_crypto(php_stream *stream,
/* We need to do slightly different things based on client/server method
* so lets remember which method was selected */
sslsock->is_client = cparam->inputs.method & STREAM_CRYPTO_IS_CLIENT;
method_flags = ((cparam->inputs.method >> 1) << 1);
method_flags = cparam->inputs.method & ~STREAM_CRYPTO_IS_CLIENT;

method = sslsock->is_client ? SSLv23_client_method() : SSLv23_server_method();
sslsock->ctx = SSL_CTX_new(method);

if (sslsock->ctx == NULL) {
php_error_docref(NULL, E_WARNING, "SSL context creation failure");
return FAILURE;
}

GET_VER_OPT_LONG("min_proto_version", min_version);
GET_VER_OPT_LONG("max_proto_version", max_version);
method_flags = php_openssl_get_proto_version_flags(method_flags, min_version, max_version);
Expand All @@ -1628,11 +1628,6 @@ int php_openssl_setup_crypto(php_stream *stream,
ssl_ctx_options = SSL_OP_ALL;
#endif

if (sslsock->ctx == NULL) {
php_error_docref(NULL, E_WARNING, "SSL context creation failure");
return FAILURE;
}

if (GET_VER_OPT("no_ticket") && zend_is_true(val)) {
ssl_ctx_options |= SSL_OP_NO_TICKET;
}
Expand Down Expand Up @@ -2293,9 +2288,7 @@ static inline int php_openssl_tcp_sockop_accept(php_stream *stream, php_openssl_

if (xparam->outputs.client && sock->enable_on_connect) {
/* remove the client bit */
if (sock->method & STREAM_CRYPTO_IS_CLIENT) {
sock->method = ((sock->method >> 1) << 1);
}
sock->method &= ~STREAM_CRYPTO_IS_CLIENT;

clisockdata->method = sock->method;

Expand Down