com php-src: Add deprecated notice to invalid DES salts.: ext/standard/crypt.c ext/standard/tests/crypt/bcrypt_invalid _algorithm.phpt
ext/standard/tests/crypt/bcrypt_invalid _cost.phpt ext/standard/tests/crypt/des_fallback_inva lid_salt.phpt
| From: | Stanislav Malyshev | Date: | Fri, 09 Jan 2015 16:18:33 +0000 |
| Subject: | com php-src: Add deprecated notice to invalid DES salts.: ext/standard/crypt.c ext/standard/tests/crypt/bcrypt_invalid _algorithm.phpt ext/standard/tests/crypt/bcrypt_invalid _cost.phpt ext/standard/tests/crypt/des_fallback_inva lid_salt.phpt |
||
| Groups: | php.cvs | ||
| Request: | Send a blank email to php-cvs+get-84508@lists.php.net to get a copy of this message | ||
Commit: 462fef794fb564c8e8726ed70be0d980ad7b1cfa
Author: Anthony Ferrara <ircmaxell@gmail.com> Fri, 9 Jan 2015 11:18:33 -0500
Parents: 32f761d7f856d13ca17814899f7317614a6c4d9d
Branches: master
Link: http://git.php.net/?p=php-src.git;a=commitdiff;h=462fef794fb564c8e8726ed70be0d980ad7b1cfa
Log:
Add deprecated notice to invalid DES salts.
This will cause an error in the case where invalid salts are provided for other algorithms.
Currently, these invalid salts will silently fall back to STD_DES which is extremely weak. By
detecting invalid DES salts, we can alert the user that there is a bug in their code.
The error is currently E_DEPRECATED as this has potential to break currently working (yet insecure)
code. In the future it should be changed to an E_WARNING and return *0
Changed paths:
M ext/standard/crypt.c
A ext/standard/tests/crypt/bcrypt_invalid_algorithm.phpt
A ext/standard/tests/crypt/bcrypt_invalid_cost.phpt
A ext/standard/tests/crypt/des_fallback_invalid_salt.phpt
Diff:
diff --git a/ext/standard/crypt.c b/ext/standard/crypt.c
index 439d3e0..ac1c2ab 100644
--- a/ext/standard/crypt.c
+++ b/ext/standard/crypt.c
@@ -100,6 +100,12 @@
#define PHP_CRYPT_RAND php_rand()
+/* Used to check DES salts to ensure that they contain only valid characters */
+#define IS_VALID_SALT_CHARACTER(c) (((c) >= '.' && (c) <= '9') ||
((c) >= 'A' && (c) <= 'Z') || ((c) >= 'a' &&
(c) <= 'z'))
+
+#define DES_INVALID_SALT_ERROR "Supplied salt is not valid for DES. Possible bug in provided
salt format."
+
+
PHP_MINIT_FUNCTION(crypt) /* {{{ */
{
REGISTER_LONG_CONSTANT("CRYPT_SALT_LENGTH", PHP_MAX_SALT_LEN, CONST_CS |
CONST_PERSISTENT);
@@ -196,10 +202,7 @@ PHPAPI zend_string *php_crypt(const char *password, const int pass_len, const
ch
} else if (
salt[0] == '$' &&
salt[1] == '2' &&
- salt[3] == '$' &&
- salt[4] >= '0' && salt[4] <= '3' &&
- salt[5] >= '0' && salt[5] <= '9' &&
- salt[6] == '$') {
+ salt[3] == '$') {
char output[PHP_MAX_SALT_LEN + 1];
memset(output, 0, PHP_MAX_SALT_LEN + 1);
@@ -214,6 +217,16 @@ PHPAPI zend_string *php_crypt(const char *password, const int pass_len, const
ch
return result;
}
} else {
+ /* DES Fallback */
+
+ /* Only check the salt if it's not EXT_DES */
+ if (salt[0] != '_') {
+ /* DES style hashes */
+ if (!IS_VALID_SALT_CHARACTER(salt[0]) || !IS_VALID_SALT_CHARACTER(salt[1])) {
+ php_error_docref(NULL, E_DEPRECATED, DES_INVALID_SALT_ERROR);
+ }
+ }
+
memset(&buffer, 0, sizeof(buffer));
_crypt_extended_init_r();
@@ -238,6 +251,10 @@ PHPAPI zend_string *php_crypt(const char *password, const int pass_len, const
ch
# else
# error Data struct used by crypt_r() is unknown. Please report.
# endif
+ if (salt[0] != '$' && salt[0] != '_' &&
(!IS_VALID_SALT_CHARACTER(salt[0]) || !IS_VALID_SALT_CHARACTER(salt[1]))) {
+ /* error consistently about invalid DES fallbacks */
+ php_error_docref(NULL, E_DEPRECATED, DES_INVALID_SALT_ERROR);
+ }
crypt_res = crypt_r(password, salt, &buffer);
if (!crypt_res || (salt[0] == '*' && salt[1] == '0')) {
return NULL;
diff --git a/ext/standard/tests/crypt/bcrypt_invalid_algorithm.phpt
b/ext/standard/tests/crypt/bcrypt_invalid_algorithm.phpt
new file mode 100644
index 0000000..58504e4
--- /dev/null
+++ b/ext/standard/tests/crypt/bcrypt_invalid_algorithm.phpt
@@ -0,0 +1,12 @@
+--TEST--
+Test BCRYPT with invalid algorithm
+--FILE--
+<?php
+var_dump(crypt("test", "$23$04$1234567890123456789012345"));
+var_dump(crypt("test", "$20$04$1234567890123456789012345"));
+var_dump(crypt("test", "$2g$04$1234567890123456789012345"));
+?>
+--EXPECTF--
+string(2) "*0"
+string(2) "*0"
+string(2) "*0"
diff --git a/ext/standard/tests/crypt/bcrypt_invalid_cost.phpt
b/ext/standard/tests/crypt/bcrypt_invalid_cost.phpt
new file mode 100644
index 0000000..f5ae7c8
--- /dev/null
+++ b/ext/standard/tests/crypt/bcrypt_invalid_cost.phpt
@@ -0,0 +1,20 @@
+--TEST--
+Test BCRYPT with invalid cost
+--FILE--
+<?php
+var_dump(crypt("test", "$2a$4$1234567891234567891234567"));
+var_dump(crypt("test", "$2a$00$1234567891234567891234567"));
+var_dump(crypt("test", "$2a$01$1234567891234567891234567"));
+var_dump(crypt("test", "$2a$02$1234567891234567891234567"));
+var_dump(crypt("test", "$2a$03$1234567891234567891234567"));
+var_dump(crypt("test", "$2a$32$1234567891234567891234567"));
+var_dump(crypt("test", "$2a$40$1234567891234567891234567"));
+?>
+--EXPECTF--
+string(2) "*0"
+string(2) "*0"
+string(2) "*0"
+string(2) "*0"
+string(2) "*0"
+string(2) "*0"
+string(2) "*0"
\ No newline at end of file
diff --git a/ext/standard/tests/crypt/des_fallback_invalid_salt.phpt
b/ext/standard/tests/crypt/des_fallback_invalid_salt.phpt
new file mode 100644
index 0000000..7ef5ec6
--- /dev/null
+++ b/ext/standard/tests/crypt/des_fallback_invalid_salt.phpt
@@ -0,0 +1,15 @@
+--TEST--
+Test DES with invalid fallback
+--FILE--
+<?php
+
+var_dump(crypt("test", "$#"));
+var_dump(crypt("test", "$5zd$01"));
+
+?>
+--EXPECTF--
+Deprecated: crypt(): Supplied salt is not valid for DES. Possible bug in provided salt format. in
%s on line %d
+string(13) "$#8MWASl5pGIk"
+
+Deprecated: crypt(): Supplied salt is not valid for DES. Possible bug in provided salt format. in
%s on line %d
+string(13) "$54mkQyGCLvHs"
\ No newline at end of file