Skip to content

Commit ed4052f

Browse files
committed
Fixed bug #69686 password_verify reports back error on PHP7 will null string.
The deprecation of DES salts created a warning when trying to verify them with password_hash. This bug fix adds a quiet mode to php_crypt() which is used by password_verify.
1 parent 3dba00b commit ed4052f

File tree

5 files changed

+26
-9
lines changed

5 files changed

+26
-9
lines changed

NEWS

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -221,7 +221,9 @@
221221
. Fixed bug #65272 (flock() out parameter not set correctly in windows).
222222
(Daniel Lowrey)
223223
. Added preg_replace_callback_array function. (Wei Dai)
224-
. Deprecated salt option to password_hash. (Anthony)
224+
. Deprecated salt option to password_hash. (Anthony)
225+
. Fixed bug #69686 (password_verify reports back error on PHP7 will null
226+
string). (Anthony)
225227
. Added Windows support for getrusage(). (Kalle)
226228

227229
- Streams:

ext/standard/crypt.c

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -151,7 +151,7 @@ static void php_to64(char *s, zend_long v, int n) /* {{{ */
151151
}
152152
/* }}} */
153153

154-
PHPAPI zend_string *php_crypt(const char *password, const int pass_len, const char *salt, int salt_len)
154+
PHPAPI zend_string *php_crypt(const char *password, const int pass_len, const char *salt, int salt_len, zend_bool quiet)
155155
{
156156
char *crypt_res;
157157
zend_string *result;
@@ -225,7 +225,10 @@ PHPAPI zend_string *php_crypt(const char *password, const int pass_len, const ch
225225
if (salt[0] != '_') {
226226
/* DES style hashes */
227227
if (!IS_VALID_SALT_CHARACTER(salt[0]) || !IS_VALID_SALT_CHARACTER(salt[1])) {
228-
php_error_docref(NULL, E_DEPRECATED, DES_INVALID_SALT_ERROR);
228+
if (!quiet) {
229+
/* error consistently about invalid DES fallbacks */
230+
php_error_docref(NULL, E_DEPRECATED, DES_INVALID_SALT_ERROR);
231+
}
229232
}
230233
}
231234

@@ -254,8 +257,10 @@ PHPAPI zend_string *php_crypt(const char *password, const int pass_len, const ch
254257
# error Data struct used by crypt_r() is unknown. Please report.
255258
# endif
256259
if (salt[0] != '$' && salt[0] != '_' && (!IS_VALID_SALT_CHARACTER(salt[0]) || !IS_VALID_SALT_CHARACTER(salt[1]))) {
257-
/* error consistently about invalid DES fallbacks */
258-
php_error_docref(NULL, E_DEPRECATED, DES_INVALID_SALT_ERROR);
260+
if (!quiet) {
261+
/* error consistently about invalid DES fallbacks */
262+
php_error_docref(NULL, E_DEPRECATED, DES_INVALID_SALT_ERROR);
263+
}
259264
}
260265
crypt_res = crypt_r(password, salt, &buffer);
261266
if (!crypt_res || (salt[0] == '*' && salt[1] == '0')) {
@@ -313,7 +318,7 @@ PHP_FUNCTION(crypt)
313318
}
314319
salt[salt_in_len] = '\0';
315320

316-
if ((result = php_crypt(str, (int)str_len, salt, (int)salt_in_len)) == NULL) {
321+
if ((result = php_crypt(str, (int)str_len, salt, (int)salt_in_len, 0)) == NULL) {
317322
if (salt[0] == '*' && salt[1] == '0') {
318323
RETURN_STRING("*1");
319324
} else {

ext/standard/password.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -260,7 +260,7 @@ PHP_FUNCTION(password_verify)
260260
if (zend_parse_parameters(ZEND_NUM_ARGS(), "ss", &password, &password_len, &hash, &hash_len) == FAILURE) {
261261
RETURN_FALSE;
262262
}
263-
if ((ret = php_crypt(password, (int)password_len, hash, (int)hash_len)) == NULL) {
263+
if ((ret = php_crypt(password, (int)password_len, hash, (int)hash_len, 1)) == NULL) {
264264
RETURN_FALSE;
265265
}
266266

@@ -415,7 +415,7 @@ PHP_FUNCTION(password_hash)
415415
/* This cast is safe, since both values are defined here in code and cannot overflow */
416416
hash_len = (int) (hash_format_len + salt_len);
417417

418-
if ((result = php_crypt(password, (int)password_len, hash, hash_len)) == NULL) {
418+
if ((result = php_crypt(password, (int)password_len, hash, hash_len, 1)) == NULL) {
419419
efree(hash);
420420
RETURN_FALSE;
421421
}

ext/standard/php_crypt.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
#ifndef PHP_CRYPT_H
2424
#define PHP_CRYPT_H
2525

26-
PHPAPI zend_string *php_crypt(const char *password, const int pass_len, const char *salt, int salt_len);
26+
PHPAPI zend_string *php_crypt(const char *password, const int pass_len, const char *salt, int salt_len, zend_bool quiet);
2727
PHP_FUNCTION(crypt);
2828
#if HAVE_CRYPT
2929
PHP_MINIT_FUNCTION(crypt);

ext/standard/tests/password/password_verify.phpt

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,21 @@ var_dump(password_verify("foo", '$2a$07$usesomesillystringforsalt$'));
1111
var_dump(password_verify('rasmusler', '$2a$07$usesomesillystringfore2uDLvp1Ii2e./U9C8sBjqp8I90dH6hi'));
1212

1313
var_dump(password_verify('rasmuslerdorf', '$2a$07$usesomesillystringfore2uDLvp1Ii2e./U9C8sBjqp8I90dH6hi'));
14+
15+
var_dump(password_verify("foo", null));
16+
17+
var_dump(password_verify("rasmuslerdorf", "rl.3StKT.4T8M"));
18+
19+
var_dump(password_verify("foo", "$1"));
20+
1421
echo "OK!";
1522
?>
1623
--EXPECT--
1724
bool(false)
1825
bool(false)
1926
bool(false)
2027
bool(true)
28+
bool(false)
29+
bool(true)
30+
bool(false)
2131
OK!

0 commit comments

Comments
 (0)