Skip to content

Commit 6579dd1

Browse files
committed
cleanup
1 parent 4a67fc4 commit 6579dd1

File tree

3 files changed

+36
-20
lines changed

3 files changed

+36
-20
lines changed

ext/openssl/openssl.c

+5
Original file line numberDiff line numberDiff line change
@@ -1412,6 +1412,11 @@ PHP_MSHUTDOWN_FUNCTION(openssl)
14121412
php_stream_xport_unregister("tlsv1.3");
14131413
#endif
14141414

1415+
#if PHP_OPENSSL_API_VERSION >= 0x30200
1416+
if (FAILURE == PHP_MSHUTDOWN(openssl_pwhash)(SHUTDOWN_FUNC_ARGS_PASSTHRU)) {
1417+
return FAILURE;
1418+
}
1419+
#endif
14151420
/* reinstate the default tcp handler */
14161421
php_stream_xport_register("tcp", php_stream_generic_socket_factory);
14171422

ext/openssl/openssl_pwhash.c

+27-19
Original file line numberDiff line numberDiff line change
@@ -32,13 +32,18 @@
3232
#include <openssl/kdf.h>
3333
#include <openssl/thread.h>
3434

35-
#define MEMLIMIT_MIN 8u
36-
#define MEMLIMIT_MAX 0xFFFFFFFFu
37-
#define OPSLIMIT_MIN 1u
38-
#define OPSLIMIT_MAX 0xFFFFFFFFu
39-
#define THREADS_MIN 1u
40-
#define THREADS_MAX 0xFFFFFFFFu
35+
#define MEMLIMIT_MIN 8u
36+
#define MEMLIMIT_MAX UINT32_MAX
37+
#define OPSLIMIT_MIN 1u
38+
#define OPSLIMIT_MAX UINT32_MAX
39+
#define THREADS_MIN 1u
40+
#define THREADS_MAX UINT32_MAX
4141

42+
#define ARGON_VERSION 0x13
43+
44+
#define SALT_SIZE 16
45+
#define HASH_SIZE 32
46+
#define DIGEST_SIZE 128
4247

4348
static inline int get_options(zend_array *options, uint32_t *memlimit, uint32_t *opslimit, uint32_t *threads)
4449
{
@@ -79,10 +84,6 @@ static inline int get_options(zend_array *options, uint32_t *memlimit, uint32_t
7984
return SUCCESS;
8085
}
8186

82-
#define SALT_SIZE 16
83-
#define HASH_SIZE 32
84-
#define DIGEST_SIZE 128
85-
8687
static bool php_openssl_argon2_compute_hash(
8788
const char *algo,
8889
uint32_t version, uint32_t memlimit, uint32_t opslimit, uint32_t threads,
@@ -141,11 +142,11 @@ static bool php_openssl_argon2_compute_hash(
141142

142143
static zend_string *php_openssl_argon2_hash(const zend_string *password, zend_array *options, const char *algo)
143144
{
144-
uint32_t opslimit, memlimit, threads, version=0x13;
145+
uint32_t opslimit, memlimit, threads, version = ARGON_VERSION;
145146
zend_string *digest = NULL, *salt64 = NULL, *hash64 = NULL;
146147
unsigned char hash[HASH_SIZE+1], salt[SALT_SIZE+1];
147148

148-
if ((ZSTR_LEN(password) >= 0xffffffff)) {
149+
if ((ZSTR_LEN(password) >= UINT32_MAX)) {
149150
zend_value_error("Password is too long");
150151
return NULL;
151152
}
@@ -161,7 +162,7 @@ static zend_string *php_openssl_argon2_hash(const zend_string *password, zend_ar
161162
return NULL;
162163
}
163164

164-
hash64 = php_base64_encode(hash, sizeof(hash)-1);
165+
hash64 = php_base64_encode(hash, HASH_SIZE);
165166
/* No padding utsing 32 *4 / 3 = 42.6 (43 + 1 padding char) */
166167
ZEND_ASSERT(ZSTR_LEN(hash64)==44 && ZSTR_VAL(hash64)[43]=='=');
167168
ZSTR_VAL(hash64)[43] = 0;
@@ -237,7 +238,7 @@ static bool php_openssl_argon2_verify(const zend_string *password, const zend_st
237238
zend_string *salt, *hash, *new;
238239
bool ret = false;
239240

240-
if ((ZSTR_LEN(password) >= 0xffffffff) || (ZSTR_LEN(digest) >= 0xffffffff)) {
241+
if ((ZSTR_LEN(password) >= UINT32_MAX) || (ZSTR_LEN(digest) >= UINT32_MAX)) {
241242
return false;
242243
}
243244
if (FAILURE == php_openssl_argon2_extract(digest, &version, &memlimit, &opslimit, &threads, &salt, &hash)) {
@@ -271,7 +272,7 @@ static bool php_openssl_argon2id_verify(const zend_string *password, const zend_
271272
static bool php_openssl_argon2_needs_rehash(const zend_string *hash, zend_array *options)
272273
{
273274
uint32_t version, opslimit, memlimit, threads;
274-
uint32_t new_version = 0x13, new_opslimit, new_memlimit, new_threads;
275+
uint32_t new_version = ARGON_VERSION, new_opslimit, new_memlimit, new_threads;
275276

276277
if (FAILURE == get_options(options, &new_memlimit, &new_opslimit, &new_threads)) {
277278
return true;
@@ -280,7 +281,7 @@ static bool php_openssl_argon2_needs_rehash(const zend_string *hash, zend_array
280281
return true;
281282
}
282283

283-
// Algo checked before
284+
// Algo already checked in pasword_needs_rehash implementation
284285
return (version != new_version) ||
285286
(opslimit != new_opslimit) ||
286287
(memlimit != new_memlimit) ||
@@ -289,7 +290,7 @@ static bool php_openssl_argon2_needs_rehash(const zend_string *hash, zend_array
289290

290291
static int php_openssl_argon2_get_info(zval *return_value, const zend_string *hash)
291292
{
292-
uint32_t v = 0, threads = 1;
293+
uint32_t v, threads;
293294
uint32_t memory_cost;
294295
uint32_t time_cost;
295296

@@ -299,6 +300,7 @@ static int php_openssl_argon2_get_info(zval *return_value, const zend_string *ha
299300
add_assoc_long(return_value, "memory_cost", memory_cost);
300301
add_assoc_long(return_value, "time_cost", time_cost);
301302
add_assoc_long(return_value, "threads", threads);
303+
302304
return SUCCESS;
303305
}
304306

@@ -333,7 +335,7 @@ static const php_password_algo openssl_algo_argon2id = {
333335

334336
PHP_FUNCTION(openssl_password_hash)
335337
{
336-
zend_string *password, *algo, *digest = NULL;
338+
zend_string *password, *algo, *digest;
337339
zend_array *options = NULL;
338340

339341
ZEND_PARSE_PARAMETERS_START(2, 3)
@@ -384,7 +386,7 @@ PHP_MINIT_FUNCTION(openssl_pwhash)
384386
zend_register_functions(NULL, ext_functions, NULL, type);
385387

386388
if (php_password_algo_find(argon2i)) {
387-
/* Nothing to do. Core has registered these algorithms for us. */
389+
/* Nothing to do. Core or sodium has registered these algorithms for us. */
388390
zend_string_release(argon2i);
389391
return SUCCESS;
390392
}
@@ -402,4 +404,10 @@ PHP_MINIT_FUNCTION(openssl_pwhash)
402404
return SUCCESS;
403405
}
404406

407+
PHP_MSHUTDOWN_FUNCTION(openssl_pwhash)
408+
{
409+
zend_unregister_functions(ext_functions, -1, NULL);
410+
411+
return SUCCESS;
412+
}
405413
#endif /* PHP_OPENSSL_API_VERSION >= 0x30200 */

ext/openssl/php_openssl.h

+4-1
Original file line numberDiff line numberDiff line change
@@ -187,11 +187,14 @@ static inline php_openssl_certificate_object *php_openssl_certificate_from_obj(z
187187
#endif
188188

189189
PHP_MINIT_FUNCTION(openssl);
190-
PHP_MINIT_FUNCTION(openssl_pwhash);
191190
PHP_MSHUTDOWN_FUNCTION(openssl);
192191
PHP_MINFO_FUNCTION(openssl);
193192
PHP_GINIT_FUNCTION(openssl);
194193
PHP_GSHUTDOWN_FUNCTION(openssl);
194+
#if PHP_OPENSSL_API_VERSION >= 0x30200
195+
PHP_MINIT_FUNCTION(openssl_pwhash);
196+
PHP_MSHUTDOWN_FUNCTION(openssl_pwhash);
197+
#endif
195198

196199
#ifdef PHP_WIN32
197200
#define PHP_OPENSSL_BIO_MODE_R(flags) (((flags) & PKCS7_BINARY) ? "rb" : "r")

0 commit comments

Comments
 (0)