Refactor password_hash()

Pull salt generation out to a helper.
Merge options/hash into single switch.
Restore php_error->php_error_docref from last diff. (Error messages matter)
This commit is contained in:
Sara Golemon 2017-07-16 17:31:39 -04:00
parent 7165e28738
commit e93f089d6a
No known key found for this signature in database
GPG key ID: DBDB397470D12172

View file

@ -135,20 +135,20 @@ static zend_string* php_password_make_salt(size_t length) /* {{{ */
zend_string *ret, *buffer; zend_string *ret, *buffer;
if (length > (INT_MAX / 3)) { if (length > (INT_MAX / 3)) {
php_error(E_WARNING, "Length is too large to safely generate"); php_error_docref(NULL, E_WARNING, "Length is too large to safely generate");
return NULL; return NULL;
} }
buffer = zend_string_alloc(length * 3 / 4 + 1, 0); buffer = zend_string_alloc(length * 3 / 4 + 1, 0);
if (FAILURE == php_random_bytes_silent(ZSTR_VAL(buffer), ZSTR_LEN(buffer))) { if (FAILURE == php_random_bytes_silent(ZSTR_VAL(buffer), ZSTR_LEN(buffer))) {
php_error(E_WARNING, "Unable to generate salt"); php_error_docref(NULL, E_WARNING, "Unable to generate salt");
zend_string_release(buffer); zend_string_release(buffer);
return NULL; return NULL;
} }
ret = zend_string_alloc(length, 0); ret = zend_string_alloc(length, 0);
if (php_password_salt_to64(ZSTR_VAL(buffer), ZSTR_LEN(buffer), length, ZSTR_VAL(ret)) == FAILURE) { if (php_password_salt_to64(ZSTR_VAL(buffer), ZSTR_LEN(buffer), length, ZSTR_VAL(ret)) == FAILURE) {
php_error(E_WARNING, "Generated salt too short"); php_error_docref(NULL, E_WARNING, "Generated salt too short");
zend_string_release(buffer); zend_string_release(buffer);
zend_string_release(ret); zend_string_release(ret);
return NULL; return NULL;
@ -341,93 +341,18 @@ PHP_FUNCTION(password_verify)
} }
/* }}} */ /* }}} */
/* {{{ proto string password_hash(string password, int algo[, array options = array()]) static zend_string* php_password_get_salt(zval *return_value, int required_salt_len, HashTable *options) {
Hash a password */ zend_string *buffer;
PHP_FUNCTION(password_hash)
{
char hash_format[8];
zend_long algo = 0;
zend_string *password, *salt;
size_t required_salt_len = 0, hash_format_len;
HashTable *options = 0;
zval *option_buffer; zval *option_buffer;
#if HAVE_ARGON2LIB if (!options || !(option_buffer = zend_hash_str_find(options, "salt", sizeof("salt") - 1))) {
size_t time_cost = PHP_PASSWORD_ARGON2_TIME_COST; buffer = php_password_make_salt(required_salt_len);
size_t memory_cost = PHP_PASSWORD_ARGON2_MEMORY_COST; if (!buffer) {
size_t threads = PHP_PASSWORD_ARGON2_THREADS; RETVAL_FALSE;
argon2_type type = Argon2_i;
#endif
ZEND_PARSE_PARAMETERS_START(2, 3)
Z_PARAM_STR(password)
Z_PARAM_LONG(algo)
Z_PARAM_OPTIONAL
Z_PARAM_ARRAY_OR_OBJECT_HT(options)
ZEND_PARSE_PARAMETERS_END();
switch (algo) {
case PHP_PASSWORD_BCRYPT:
{
zend_long cost = PHP_PASSWORD_BCRYPT_COST;
if (options && (option_buffer = zend_hash_str_find(options, "cost", sizeof("cost")-1)) != NULL) {
cost = zval_get_long(option_buffer);
} }
return buffer;
if (cost < 4 || cost > 31) {
php_error_docref(NULL, E_WARNING, "Invalid bcrypt cost parameter specified: " ZEND_LONG_FMT, cost);
RETURN_NULL();
} }
required_salt_len = 22;
sprintf(hash_format, "$2y$%02ld$", (long) cost);
hash_format_len = 7;
}
break;
#if HAVE_ARGON2LIB
case PHP_PASSWORD_ARGON2I:
{
if (options && (option_buffer = zend_hash_str_find(options, "memory_cost", sizeof("memory_cost")-1)) != NULL) {
memory_cost = zval_get_long(option_buffer);
}
if (memory_cost > ARGON2_MAX_MEMORY || memory_cost < ARGON2_MIN_MEMORY) {
php_error_docref(NULL, E_WARNING, "Memory cost is outside of allowed memory range", memory_cost);
RETURN_NULL();
}
if (options && (option_buffer = zend_hash_str_find(options, "time_cost", sizeof("time_cost")-1)) != NULL) {
time_cost = zval_get_long(option_buffer);
}
if (time_cost > ARGON2_MAX_TIME || time_cost < ARGON2_MIN_TIME) {
php_error_docref(NULL, E_WARNING, "Time cost is outside of allowed time range", time_cost);
RETURN_NULL();
}
if (options && (option_buffer = zend_hash_str_find(options, "threads", sizeof("threads")-1)) != NULL) {
threads = zval_get_long(option_buffer);
}
if (threads > ARGON2_MAX_LANES || threads == 0) {
php_error_docref(NULL, E_WARNING, "Invalid number of threads", threads);
RETURN_NULL();
}
required_salt_len = 16;
}
break;
#endif
case PHP_PASSWORD_UNKNOWN:
default:
php_error_docref(NULL, E_WARNING, "Unknown password hashing algorithm: " ZEND_LONG_FMT, algo);
RETURN_NULL();
}
if (options && (option_buffer = zend_hash_str_find(options, "salt", sizeof("salt")-1)) != NULL) {
zend_string *buffer;
php_error_docref(NULL, E_DEPRECATED, "Use of the 'salt' option to password_hash is deprecated"); php_error_docref(NULL, E_DEPRECATED, "Use of the 'salt' option to password_hash is deprecated");
switch (Z_TYPE_P(option_buffer)) { switch (Z_TYPE_P(option_buffer)) {
@ -446,7 +371,7 @@ PHP_FUNCTION(password_hash)
case IS_ARRAY: case IS_ARRAY:
default: default:
php_error_docref(NULL, E_WARNING, "Non-string salt parameter supplied"); php_error_docref(NULL, E_WARNING, "Non-string salt parameter supplied");
RETURN_NULL(); return NULL;
} }
/* XXX all the crypt related APIs work with int for string length. /* XXX all the crypt related APIs work with int for string length.
@ -454,35 +379,74 @@ PHP_FUNCTION(password_hash)
the > INT_MAX check. */ the > INT_MAX check. */
if (ZSTR_LEN(buffer) > INT_MAX) { if (ZSTR_LEN(buffer) > INT_MAX) {
php_error_docref(NULL, E_WARNING, "Supplied salt is too long"); php_error_docref(NULL, E_WARNING, "Supplied salt is too long");
RETURN_NULL(); zend_string_release(buffer);
} else if (ZSTR_LEN(buffer) < required_salt_len) { return NULL;
}
if (ZSTR_LEN(buffer) < required_salt_len) {
php_error_docref(NULL, E_WARNING, "Provided salt is too short: %zd expecting %zd", ZSTR_LEN(buffer), required_salt_len); php_error_docref(NULL, E_WARNING, "Provided salt is too short: %zd expecting %zd", ZSTR_LEN(buffer), required_salt_len);
zend_string_release(buffer); zend_string_release(buffer);
RETURN_NULL(); return NULL;
} else if (php_password_salt_is_alphabet(ZSTR_VAL(buffer), ZSTR_LEN(buffer)) == FAILURE) { }
salt = zend_string_alloc(required_salt_len, 0);
if (php_password_salt_is_alphabet(ZSTR_VAL(buffer), ZSTR_LEN(buffer)) == FAILURE) {
zend_string *salt = zend_string_alloc(required_salt_len, 0);
if (php_password_salt_to64(ZSTR_VAL(buffer), ZSTR_LEN(buffer), required_salt_len, ZSTR_VAL(salt)) == FAILURE) { if (php_password_salt_to64(ZSTR_VAL(buffer), ZSTR_LEN(buffer), required_salt_len, ZSTR_VAL(salt)) == FAILURE) {
zend_string_release(salt);
php_error_docref(NULL, E_WARNING, "Provided salt is too short: %zd", ZSTR_LEN(buffer)); php_error_docref(NULL, E_WARNING, "Provided salt is too short: %zd", ZSTR_LEN(buffer));
zend_string_release(salt);
zend_string_release(buffer); zend_string_release(buffer);
RETURN_NULL(); return NULL;
} }
zend_string_release(buffer);
return salt;
} else { } else {
salt = zend_string_alloc(required_salt_len, 0); zend_string *salt = zend_string_alloc(required_salt_len, 0);
memcpy(ZSTR_VAL(salt), ZSTR_VAL(buffer), required_salt_len); memcpy(ZSTR_VAL(salt), ZSTR_VAL(buffer), required_salt_len);
}
zend_string_release(buffer); zend_string_release(buffer);
} else { return salt;
salt = php_password_make_salt(required_salt_len);
if (!salt) {
RETURN_FALSE;
}
} }
}
/* {{{ proto string password_hash(string password, int algo[, array options = array()])
Hash a password */
PHP_FUNCTION(password_hash)
{
zend_string *password;
zend_long algo = PHP_PASSWORD_DEFAULT;
HashTable *options = NULL;
#if HAVE_ARGON2LIB
#endif
ZEND_PARSE_PARAMETERS_START(2, 3)
Z_PARAM_STR(password)
Z_PARAM_LONG(algo)
Z_PARAM_OPTIONAL
Z_PARAM_ARRAY_OR_OBJECT_HT(options)
ZEND_PARSE_PARAMETERS_END();
switch (algo) { switch (algo) {
case PHP_PASSWORD_BCRYPT: case PHP_PASSWORD_BCRYPT:
{ {
zend_string *result, *hash; char hash_format[10];
size_t hash_format_len;
zend_string *result, *hash, *salt;
zval *option_buffer;
zend_long cost = PHP_PASSWORD_BCRYPT_COST;
if (options && (option_buffer = zend_hash_str_find(options, "cost", sizeof("cost")-1)) != NULL) {
cost = zval_get_long(option_buffer);
}
if (cost < 4 || cost > 31) {
php_error_docref(NULL, E_WARNING, "Invalid bcrypt cost parameter specified: " ZEND_LONG_FMT, cost);
RETURN_NULL();
}
hash_format_len = snprintf(hash_format, sizeof(hash_format), "$2y$%02ld$", (long) cost);
if (!(salt = php_password_get_salt(return_value, 22, options))) {
return;
}
ZSTR_VAL(salt)[ZSTR_LEN(salt)] = 0; ZSTR_VAL(salt)[ZSTR_LEN(salt)] = 0;
hash = zend_string_alloc(ZSTR_LEN(salt) + hash_format_len, 0); hash = zend_string_alloc(ZSTR_LEN(salt) + hash_format_len, 0);
@ -510,11 +474,47 @@ PHP_FUNCTION(password_hash)
#if HAVE_ARGON2LIB #if HAVE_ARGON2LIB
case PHP_PASSWORD_ARGON2I: case PHP_PASSWORD_ARGON2I:
{ {
zval *boption_buffer;
zend_string *salt, *out, *encoded;
size_t time_cost = PHP_PASSWORD_ARGON2_TIME_COST;
size_t memory_cost = PHP_PASSWORD_ARGON2_MEMORY_COST;
size_t threads = PHP_PASSWORD_ARGON2_THREADS;
argon2_type type = Argon2_i;
size_t encoded_len; size_t encoded_len;
int status = 0; int status = 0;
zend_string *out = zend_string_alloc(32, 0);
zend_string *encoded;
if (options && (option_buffer = zend_hash_str_find(options, "memory_cost", sizeof("memory_cost")-1)) != NULL) {
memory_cost = zval_get_long(option_buffer);
}
if (memory_cost > ARGON2_MAX_MEMORY || memory_cost < ARGON2_MIN_MEMORY) {
php_error_docref(NULL, E_WARNING, "Memory cost is outside of allowed memory range", memory_cost);
RETURN_NULL();
}
if (options && (option_buffer = zend_hash_str_find(options, "time_cost", sizeof("time_cost")-1)) != NULL) {
time_cost = zval_get_long(option_buffer);
}
if (time_cost > ARGON2_MAX_TIME || time_cost < ARGON2_MIN_TIME) {
php_error_docref(NULL, E_WARNING, "Time cost is outside of allowed time range", time_cost);
RETURN_NULL();
}
if (options && (option_buffer = zend_hash_str_find(options, "threads", sizeof("threads")-1)) != NULL) {
threads = zval_get_long(option_buffer);
}
if (threads > ARGON2_MAX_LANES || threads == 0) {
php_error_docref(NULL, E_WARNING, "Invalid number of threads", threads);
RETURN_NULL();
}
if (!(salt = php_password_get_salt(return_value, 16, options))) {
return;
}
out = zend_string_alloc(32, 0);
encoded_len = argon2_encodedlen( encoded_len = argon2_encodedlen(
time_cost, time_cost,
memory_cost, memory_cost,
@ -527,7 +527,6 @@ PHP_FUNCTION(password_hash)
); );
encoded = zend_string_alloc(encoded_len, 0); encoded = zend_string_alloc(encoded_len, 0);
status = argon2_hash( status = argon2_hash(
time_cost, time_cost,
memory_cost, memory_cost,
@ -549,7 +548,7 @@ PHP_FUNCTION(password_hash)
if (status != ARGON2_OK) { if (status != ARGON2_OK) {
zend_string_free(encoded); zend_string_free(encoded);
php_error(E_WARNING, "%s", argon2_error_message(status)); php_error_docref(NULL, E_WARNING, "%s", argon2_error_message(status));
RETURN_FALSE; RETURN_FALSE;
} }
@ -558,8 +557,10 @@ PHP_FUNCTION(password_hash)
} }
break; break;
#endif #endif
case PHP_PASSWORD_UNKNOWN:
default: default:
RETURN_FALSE; php_error_docref(NULL, E_WARNING, "Unknown password hashing algorithm: " ZEND_LONG_FMT, algo);
RETURN_NULL();
} }
} }
/* }}} */ /* }}} */