Skip to content

Commit 96b33ab

Browse files
committed
Integrate OpenSSL libctx to pwhash
1 parent b1fce8a commit 96b33ab

File tree

5 files changed

+56
-8
lines changed

5 files changed

+56
-8
lines changed

ext/openssl/config0.m4

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -49,9 +49,6 @@ if test "$PHP_OPENSSL" != "no"; then
4949
the default provider.])])
5050

5151
AS_VAR_IF([PHP_OPENSSL_ARGON2], [no],, [
52-
AS_VAR_IF([PHP_THREAD_SAFETY], [yes],
53-
[AC_MSG_ERROR([Not supported in ZTS mode for now])])
54-
5552
PHP_CHECK_LIBRARY([crypto], [OSSL_set_max_threads],
5653
[AC_DEFINE([HAVE_OPENSSL_ARGON2], [1],
5754
[Define to 1 to enable OpenSSL argon2 password hashing.])],

ext/openssl/openssl.c

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -355,7 +355,15 @@ static PHP_INI_MH(OnUpdateLibCtx)
355355
{
356356
#if PHP_OPENSSL_API_VERSION >= 0x30000
357357
if (zend_string_equals_literal(new_value, "default")) {
358+
#if defined(ZTS) && defined(HAVE_OPENSSL_ARGON2)
359+
if (stage != ZEND_INI_STAGE_DEACTIVATE) {
360+
int err_type = stage == ZEND_INI_STAGE_RUNTIME ? E_WARNING : E_ERROR;
361+
php_error_docref(NULL, err_type, "OpenSSL libctx \"default\" cannot be used in this configuration");
362+
}
363+
return FAILURE;
364+
#else
358365
OPENSSL_G(ctx).libctx = OPENSSL_G(ctx).default_libctx;
366+
#endif
359367
} else if (zend_string_equals_literal(new_value, "custom")) {
360368
OPENSSL_G(ctx).libctx = OPENSSL_G(ctx).custom_libctx;
361369
} else {

ext/openssl/openssl_pwhash.c

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
#include "ext/standard/php_password.h"
2323
#include "php_openssl.h"
2424

25-
#if defined(HAVE_OPENSSL_ARGON2)
25+
#ifdef HAVE_OPENSSL_ARGON2
2626
#include "Zend/zend_attributes.h"
2727
#include "openssl_pwhash_arginfo.h"
2828
#include <ext/standard/base64.h>
@@ -46,6 +46,8 @@
4646
#define PHP_OPENSSL_HASH_SIZE 32
4747
#define PHP_OPENSSL_DIGEST_SIZE 128
4848

49+
ZEND_EXTERN_MODULE_GLOBALS(openssl)
50+
4951
static inline zend_result get_options(zend_array *options, uint32_t *memlimit, uint32_t *iterlimit, uint32_t *threads)
5052
{
5153
zval *opt;
@@ -98,8 +100,8 @@ static bool php_openssl_argon2_compute_hash(
98100
uint32_t oldthreads;
99101
bool ret = false;
100102

101-
oldthreads = OSSL_get_max_threads(NULL);
102-
if (OSSL_set_max_threads(NULL, threads) != 1) {
103+
oldthreads = OSSL_get_max_threads(PHP_OPENSSL_LIBCTX);
104+
if (OSSL_set_max_threads(PHP_OPENSSL_LIBCTX, threads) != 1) {
103105
goto fail;
104106
}
105107
p = params;
@@ -111,7 +113,7 @@ static bool php_openssl_argon2_compute_hash(
111113
*p++ = OSSL_PARAM_construct_octet_string(OSSL_KDF_PARAM_PASSWORD, (void *)pass, pass_len);
112114
*p++ = OSSL_PARAM_construct_end();
113115

114-
if ((kdf = EVP_KDF_fetch(NULL, algo, NULL)) == NULL) {
116+
if ((kdf = EVP_KDF_fetch(PHP_OPENSSL_LIBCTX, algo, PHP_OPENSSL_PROPQ)) == NULL) {
115117
goto fail;
116118
}
117119
if ((kctx = EVP_KDF_CTX_new(kdf)) == NULL) {
@@ -127,7 +129,7 @@ static bool php_openssl_argon2_compute_hash(
127129
fail:
128130
EVP_KDF_free(kdf);
129131
EVP_KDF_CTX_free(kctx);
130-
OSSL_set_max_threads(NULL, oldthreads);
132+
OSSL_set_max_threads(PHP_OPENSSL_LIBCTX, oldthreads);
131133

132134
return ret;
133135
}
@@ -385,4 +387,5 @@ PHP_MINIT_FUNCTION(openssl_pwhash)
385387

386388
return SUCCESS;
387389
}
390+
388391
#endif /* HAVE_OPENSSL_ARGON2 */
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
--TEST--
2+
openssl.libctx INI setting when Argon2 enabled and ZTS used
3+
--EXTENSIONS--
4+
openssl
5+
--INI--
6+
openssl.libctx = default
7+
--SKIPIF--
8+
<?php
9+
if (!ZEND_THREAD_SAFE) {
10+
die("skip - Non ZTS test");
11+
}
12+
if (!function_exists('openssl_password_hash')) {
13+
die("skip - OpenSSL Argon2 not enabled");
14+
}
15+
?>
16+
--FILE--
17+
<?php
18+
var_dump(ini_get('openssl.libctx'));
19+
?>
20+
--EXPECT--
21+
Fatal error: PHP Startup: OpenSSL libctx "default" cannot be used in this configuration in Unknown on line 0
22+
string(6) "custom"
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
--TEST--
2+
openssl.libctx INI setting when Argon2 disable or ZTS not used
3+
--EXTENSIONS--
4+
openssl
5+
--INI--
6+
openssl.libctx = default
7+
--SKIPIF--
8+
<?php
9+
if (ZEND_THREAD_SAFE && function_exists('openssl_password_hash')) {
10+
die("skip - ZTS test with Argon2 enabled");
11+
}
12+
?>
13+
--FILE--
14+
<?php
15+
var_dump(ini_get('openssl.libctx'));
16+
?>
17+
--EXPECT--
18+
string(6) "default"

0 commit comments

Comments
 (0)