php-src/ext/standard/password.c

396 lines
10 KiB
C
Raw Normal View History

/*
+----------------------------------------------------------------------+
2014-09-20 00:33:14 +08:00
| PHP Version 7 |
+----------------------------------------------------------------------+
| Copyright (c) 1997-2016 The PHP Group |
+----------------------------------------------------------------------+
| This source file is subject to version 3.01 of the PHP license, |
| that is bundled with this package in the file LICENSE, and is |
| available through the world-wide-web at the following url: |
| http://www.php.net/license/3_01.txt |
| If you did not receive a copy of the PHP license and are unable to |
| obtain it through the world-wide-web, please send a note to |
| license@php.net so we can mail you a copy immediately. |
+----------------------------------------------------------------------+
| Authors: Anthony Ferrara <ircmaxell@php.net> |
+----------------------------------------------------------------------+
*/
/* $Id$ */
#include <stdlib.h>
#include "php.h"
#if HAVE_CRYPT
#include "fcntl.h"
#include "php_password.h"
#include "php_rand.h"
#include "php_crypt.h"
#include "base64.h"
2012-06-25 11:25:18 +08:00
#include "zend_interfaces.h"
#include "info.h"
#include "php_random.h"
#if PHP_WIN32
#include "win32/winutil.h"
#endif
PHP_MINIT_FUNCTION(password) /* {{{ */
{
2014-08-26 01:24:55 +08:00
REGISTER_LONG_CONSTANT("PASSWORD_DEFAULT", PHP_PASSWORD_DEFAULT, CONST_CS | CONST_PERSISTENT);
REGISTER_LONG_CONSTANT("PASSWORD_BCRYPT", PHP_PASSWORD_BCRYPT, CONST_CS | CONST_PERSISTENT);
2014-08-26 01:24:55 +08:00
REGISTER_LONG_CONSTANT("PASSWORD_BCRYPT_DEFAULT_COST", PHP_PASSWORD_BCRYPT_COST, CONST_CS | CONST_PERSISTENT);
return SUCCESS;
}
/* }}} */
static char* php_password_get_algo_name(const php_password_algo algo)
{
switch (algo) {
case PHP_PASSWORD_BCRYPT:
return "bcrypt";
case PHP_PASSWORD_UNKNOWN:
default:
return "unknown";
}
}
2015-01-03 17:22:58 +08:00
static php_password_algo php_password_determine_algo(const char *hash, const size_t len)
{
if (len > 3 && hash[0] == '$' && hash[1] == '2' && hash[2] == 'y' && len == 60) {
return PHP_PASSWORD_BCRYPT;
}
return PHP_PASSWORD_UNKNOWN;
}
static int php_password_salt_is_alphabet(const char *str, const size_t len) /* {{{ */
{
size_t i = 0;
2012-06-27 23:04:41 +08:00
for (i = 0; i < len; i++) {
if (!((str[i] >= 'A' && str[i] <= 'Z') || (str[i] >= 'a' && str[i] <= 'z') || (str[i] >= '0' && str[i] <= '9') || str[i] == '.' || str[i] == '/')) {
return FAILURE;
2012-06-27 23:04:41 +08:00
}
}
return SUCCESS;
}
2012-06-27 23:04:41 +08:00
/* }}} */
static int php_password_salt_to64(const char *str, const size_t str_len, const size_t out_len, char *ret) /* {{{ */
{
size_t pos = 0;
zend_string *buffer;
if ((int) str_len < 0) {
return FAILURE;
}
2014-09-14 17:00:22 +08:00
buffer = php_base64_encode((unsigned char*) str, str_len);
if (ZSTR_LEN(buffer) < out_len) {
/* Too short of an encoded string generated */
2014-08-26 01:24:55 +08:00
zend_string_release(buffer);
return FAILURE;
}
2012-06-27 23:04:41 +08:00
for (pos = 0; pos < out_len; pos++) {
if (ZSTR_VAL(buffer)[pos] == '+') {
2012-06-27 23:04:41 +08:00
ret[pos] = '.';
} else if (ZSTR_VAL(buffer)[pos] == '=') {
2014-08-26 01:24:55 +08:00
zend_string_free(buffer);
return FAILURE;
2012-06-27 23:04:41 +08:00
} else {
ret[pos] = ZSTR_VAL(buffer)[pos];
}
2012-06-27 23:04:41 +08:00
}
2014-08-26 01:24:55 +08:00
zend_string_free(buffer);
return SUCCESS;
}
2012-06-27 23:04:41 +08:00
/* }}} */
2014-12-14 06:06:14 +08:00
static int php_password_make_salt(size_t length, char *ret) /* {{{ */
{
2015-12-15 01:23:01 +08:00
size_t raw_length;
char *buffer;
char *result;
if (length > (INT_MAX / 3)) {
2014-12-14 06:06:14 +08:00
php_error_docref(NULL, E_WARNING, "Length is too large to safely generate");
return FAILURE;
}
raw_length = length * 3 / 4 + 1;
buffer = (char *) safe_emalloc(raw_length, 1, 1);
if (FAILURE == php_random_bytes_silent(buffer, raw_length)) {
php_error_docref(NULL, E_WARNING, "Unable to generate salt");
efree(buffer);
return FAILURE;
}
2015-01-03 17:22:58 +08:00
result = safe_emalloc(length, 1, 1);
if (php_password_salt_to64(buffer, raw_length, length, result) == FAILURE) {
2014-12-14 06:06:14 +08:00
php_error_docref(NULL, E_WARNING, "Generated salt too short");
efree(buffer);
efree(result);
return FAILURE;
}
2014-09-14 17:00:22 +08:00
memcpy(ret, result, length);
efree(result);
efree(buffer);
ret[length] = 0;
return SUCCESS;
2012-06-27 23:04:41 +08:00
}
/* }}} */
2012-07-06 05:46:33 +08:00
PHP_FUNCTION(password_get_info)
{
php_password_algo algo;
size_t hash_len;
char *hash, *algo_name;
zval options;
2012-07-06 05:46:33 +08:00
2014-12-14 06:06:14 +08:00
if (zend_parse_parameters(ZEND_NUM_ARGS(), "s", &hash, &hash_len) == FAILURE) {
return;
2012-07-06 05:58:19 +08:00
}
2012-07-06 05:46:33 +08:00
array_init(&options);
2012-07-06 05:46:33 +08:00
algo = php_password_determine_algo(hash, (size_t) hash_len);
algo_name = php_password_get_algo_name(algo);
2015-01-03 17:22:58 +08:00
2012-07-06 05:46:33 +08:00
switch (algo) {
case PHP_PASSWORD_BCRYPT:
2012-07-06 05:46:33 +08:00
{
2014-08-26 01:24:55 +08:00
zend_long cost = PHP_PASSWORD_BCRYPT_COST;
2014-08-26 02:22:49 +08:00
sscanf(hash, "$2y$" ZEND_LONG_FMT "$", &cost);
2014-08-26 01:24:55 +08:00
add_assoc_long(&options, "cost", cost);
2012-07-06 05:46:33 +08:00
}
break;
case PHP_PASSWORD_UNKNOWN:
default:
break;
2012-07-06 05:46:33 +08:00
}
array_init(return_value);
2015-01-03 17:22:58 +08:00
2014-08-26 01:24:55 +08:00
add_assoc_long(return_value, "algo", algo);
2014-04-15 19:40:40 +08:00
add_assoc_string(return_value, "algoName", algo_name);
2015-01-03 17:22:58 +08:00
add_assoc_zval(return_value, "options", &options);
2012-07-06 05:46:33 +08:00
}
PHP_FUNCTION(password_needs_rehash)
{
2014-08-26 01:24:55 +08:00
zend_long new_algo = 0;
php_password_algo algo;
size_t hash_len;
char *hash;
HashTable *options = 0;
zval *option_buffer;
2015-01-03 17:22:58 +08:00
2014-12-14 06:06:14 +08:00
if (zend_parse_parameters(ZEND_NUM_ARGS(), "sl|H", &hash, &hash_len, &new_algo, &options) == FAILURE) {
return;
2012-07-06 05:58:19 +08:00
}
algo = php_password_determine_algo(hash, (size_t) hash_len);
2015-01-03 17:22:58 +08:00
if (algo != new_algo) {
RETURN_TRUE;
}
switch (algo) {
case PHP_PASSWORD_BCRYPT:
{
2014-08-26 01:24:55 +08:00
zend_long new_cost = PHP_PASSWORD_BCRYPT_COST, cost = 0;
2015-01-03 17:22:58 +08:00
if (options && (option_buffer = zend_hash_str_find(options, "cost", sizeof("cost")-1)) != NULL) {
new_cost = zval_get_long(option_buffer);
}
2014-08-26 02:22:49 +08:00
sscanf(hash, "$2y$" ZEND_LONG_FMT "$", &cost);
if (cost != new_cost) {
RETURN_TRUE;
}
}
break;
case PHP_PASSWORD_UNKNOWN:
default:
break;
}
RETURN_FALSE;
}
2012-06-27 23:04:41 +08:00
/* {{{ proto boolean password_make_salt(string password, string hash)
Verify a hash created using crypt() or password_hash() */
PHP_FUNCTION(password_verify)
{
2012-06-25 11:35:26 +08:00
int status = 0, i;
size_t password_len, hash_len;
char *password, *hash;
zend_string *ret;
2015-01-03 17:22:58 +08:00
2014-12-14 06:06:14 +08:00
if (zend_parse_parameters(ZEND_NUM_ARGS(), "ss", &password, &password_len, &hash, &hash_len) == FAILURE) {
2012-06-27 23:04:41 +08:00
RETURN_FALSE;
}
if ((ret = php_crypt(password, (int)password_len, hash, (int)hash_len, 1)) == NULL) {
2012-06-25 11:35:26 +08:00
RETURN_FALSE;
}
if (ZSTR_LEN(ret) != hash_len || hash_len < 13) {
2014-08-26 01:24:55 +08:00
zend_string_free(ret);
2012-06-25 11:35:26 +08:00
RETURN_FALSE;
}
2015-01-03 17:22:58 +08:00
/* We're using this method instead of == in order to provide
* resistance towards timing attacks. This is a constant time
* equality check that will always check every byte of both
* values. */
for (i = 0; i < hash_len; i++) {
status |= (ZSTR_VAL(ret)[i] ^ hash[i]);
2012-06-25 11:35:26 +08:00
}
2014-08-26 01:24:55 +08:00
zend_string_free(ret);
2012-06-25 11:35:26 +08:00
RETURN_BOOL(status == 0);
2015-01-03 17:22:58 +08:00
}
2012-06-27 23:04:41 +08:00
/* }}} */
/* {{{ proto string password_hash(string password, int algo, array options = array())
Hash a password */
PHP_FUNCTION(password_hash)
{
2016-01-04 21:46:57 +08:00
char hash_format[8], *hash, *salt, *password;
2014-08-26 01:24:55 +08:00
zend_long algo = 0;
2014-10-23 22:38:25 +08:00
size_t password_len = 0;
int hash_len;
size_t salt_len = 0, required_salt_len = 0, hash_format_len;
2012-06-27 23:04:41 +08:00
HashTable *options = 0;
zval *option_buffer;
zend_string *result;
2014-12-14 06:06:14 +08:00
if (zend_parse_parameters(ZEND_NUM_ARGS(), "sl|H", &password, &password_len, &algo, &options) == FAILURE) {
return;
2012-06-25 11:25:18 +08:00
}
switch (algo) {
case PHP_PASSWORD_BCRYPT:
{
2014-08-26 01:24:55 +08:00
zend_long cost = PHP_PASSWORD_BCRYPT_COST;
2015-01-03 17:22:58 +08:00
if (options && (option_buffer = zend_hash_str_find(options, "cost", sizeof("cost")-1)) != NULL) {
cost = zval_get_long(option_buffer);
}
2015-01-03 17:22:58 +08:00
if (cost < 4 || cost > 31) {
2014-12-14 06:06:14 +08:00
php_error_docref(NULL, E_WARNING, "Invalid bcrypt cost parameter specified: " ZEND_LONG_FMT, cost);
RETURN_NULL();
}
2015-01-03 17:22:58 +08:00
required_salt_len = 22;
2014-09-03 21:57:28 +08:00
sprintf(hash_format, "$2y$%02ld$", (long) cost);
hash_format_len = 7;
}
break;
case PHP_PASSWORD_UNKNOWN:
default:
2014-12-14 06:06:14 +08:00
php_error_docref(NULL, E_WARNING, "Unknown password hashing algorithm: " ZEND_LONG_FMT, algo);
RETURN_NULL();
2012-06-27 23:04:41 +08:00
}
if (options && (option_buffer = zend_hash_str_find(options, "salt", sizeof("salt")-1)) != NULL) {
2016-01-04 21:46:57 +08:00
zend_string *buffer;
2015-04-10 03:51:04 +08:00
php_error_docref(NULL, E_DEPRECATED, "Use of the 'salt' option to password_hash is deprecated");
switch (Z_TYPE_P(option_buffer)) {
case IS_STRING:
2016-01-04 21:46:57 +08:00
buffer = zend_string_copy(Z_STR_P(option_buffer));
break;
2014-08-26 01:24:55 +08:00
case IS_LONG:
case IS_DOUBLE:
case IS_OBJECT:
2016-01-04 21:46:57 +08:00
buffer = zval_get_string(option_buffer);
break;
case IS_FALSE:
case IS_TRUE:
case IS_NULL:
case IS_RESOURCE:
case IS_ARRAY:
default:
2014-12-14 06:06:14 +08:00
php_error_docref(NULL, E_WARNING, "Non-string salt parameter supplied");
RETURN_NULL();
2012-06-27 23:04:41 +08:00
}
/* XXX all the crypt related APIs work with int for string length.
That should be revised for size_t and then we maybe don't require
the > INT_MAX check. */
2016-01-04 21:46:57 +08:00
if (ZSTR_LEN(buffer) > INT_MAX) {
2014-12-14 06:06:14 +08:00
php_error_docref(NULL, E_WARNING, "Supplied salt is too long");
RETURN_NULL();
2016-01-04 21:46:57 +08:00
} else 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);
zend_string_release(buffer);
RETURN_NULL();
2016-01-04 21:46:57 +08:00
} else if (php_password_salt_is_alphabet(ZSTR_VAL(buffer), ZSTR_LEN(buffer)) == FAILURE) {
salt = safe_emalloc(required_salt_len, 1, 1);
2016-01-04 21:46:57 +08:00
if (php_password_salt_to64(ZSTR_VAL(buffer), ZSTR_LEN(buffer), required_salt_len, salt) == FAILURE) {
2012-06-25 11:36:09 +08:00
efree(salt);
2016-01-04 21:46:57 +08:00
php_error_docref(NULL, E_WARNING, "Provided salt is too short: %zd", ZSTR_LEN(buffer));
zend_string_release(buffer);
RETURN_NULL();
}
2012-06-27 23:04:41 +08:00
salt_len = required_salt_len;
} else {
salt = safe_emalloc(required_salt_len, 1, 1);
2016-01-04 21:46:57 +08:00
memcpy(salt, ZSTR_VAL(buffer), required_salt_len);
2012-06-27 23:04:41 +08:00
salt_len = required_salt_len;
}
2016-01-04 21:46:57 +08:00
zend_string_release(buffer);
2012-06-27 23:04:41 +08:00
} else {
salt = safe_emalloc(required_salt_len, 1, 1);
2014-12-14 06:06:14 +08:00
if (php_password_make_salt(required_salt_len, salt) == FAILURE) {
efree(salt);
RETURN_FALSE;
}
2012-06-27 23:04:41 +08:00
salt_len = required_salt_len;
}
2015-01-03 17:22:58 +08:00
salt[salt_len] = 0;
hash = safe_emalloc(salt_len + hash_format_len, 1, 1);
sprintf(hash, "%s%s", hash_format, salt);
hash[hash_format_len + salt_len] = 0;
2012-06-25 11:25:18 +08:00
efree(salt);
/* This cast is safe, since both values are defined here in code and cannot overflow */
hash_len = (int) (hash_format_len + salt_len);
if ((result = php_crypt(password, (int)password_len, hash, hash_len, 1)) == NULL) {
efree(hash);
RETURN_FALSE;
}
2012-06-25 11:25:18 +08:00
efree(hash);
2012-06-25 11:25:18 +08:00
if (ZSTR_LEN(result) < 13) {
2014-08-26 01:24:55 +08:00
zend_string_free(result);
2012-06-25 11:25:18 +08:00
RETURN_FALSE;
}
RETURN_STR(result);
}
/* }}} */
#endif /* HAVE_CRYPT */
/*
* Local variables:
* tab-width: 4
* c-basic-offset: 4
* End:
* vim600: sw=4 ts=4 fdm=marker
* vim<600: sw=4 ts=4
*/