/* +----------------------------------------------------------------------+ | PHP Version 5 | +----------------------------------------------------------------------+ | 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" #include "zend_interfaces.h" #include "info.h" #if PHP_WIN32 #include "win32/winutil.h" #endif PHP_MINIT_FUNCTION(password) /* {{{ */ { REGISTER_LONG_CONSTANT("PASSWORD_DEFAULT", PHP_PASSWORD_DEFAULT, CONST_CS | CONST_PERSISTENT); REGISTER_LONG_CONSTANT("PASSWORD_BCRYPT", PHP_PASSWORD_BCRYPT, CONST_CS | CONST_PERSISTENT); 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"; } } 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; 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; } } return SUCCESS; } /* }}} */ 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; size_t ret_len = 0; unsigned char *buffer; if ((int) str_len < 0) { return FAILURE; } buffer = php_base64_encode((unsigned char*) str, (int) str_len, (int*) &ret_len); if (ret_len < out_len) { /* Too short of an encoded string generated */ efree(buffer); return FAILURE; } for (pos = 0; pos < out_len; pos++) { if (buffer[pos] == '+') { ret[pos] = '.'; } else if (buffer[pos] == '=') { efree(buffer); return FAILURE; } else { ret[pos] = buffer[pos]; } } efree(buffer); return SUCCESS; } /* }}} */ static int php_password_make_salt(size_t length, char *ret TSRMLS_DC) /* {{{ */ { int buffer_valid = 0; size_t i, raw_length; char *buffer; char *result; if (length > (INT_MAX / 3)) { php_error_docref(NULL TSRMLS_CC, 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 PHP_WIN32 { BYTE *iv_b = (BYTE *) buffer; if (php_win32_get_random_bytes(iv_b, raw_length) == SUCCESS) { buffer_valid = 1; } } #else { int fd, n; size_t read_bytes = 0; fd = open("/dev/urandom", O_RDONLY); if (fd >= 0) { while (read_bytes < raw_length) { n = read(fd, buffer + read_bytes, raw_length - read_bytes); if (n < 0) { break; } read_bytes += (size_t) n; } close(fd); } if (read_bytes >= raw_length) { buffer_valid = 1; } } #endif if (!buffer_valid) { for (i = 0; i < raw_length; i++) { buffer[i] ^= (char) (255.0 * php_rand(TSRMLS_C) / RAND_MAX); } } result = safe_emalloc(length, 1, 1); if (php_password_salt_to64(buffer, raw_length, length, result) == FAILURE) { php_error_docref(NULL TSRMLS_CC, E_WARNING, "Generated salt too short"); efree(buffer); efree(result); return FAILURE; } memcpy(ret, result, (int) length); efree(result); efree(buffer); ret[length] = 0; return SUCCESS; } /* }}} */ PHP_FUNCTION(password_get_info) { php_password_algo algo; int hash_len; char *hash, *algo_name; zval *options; if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s", &hash, &hash_len) == FAILURE) { return; } if (hash_len < 0) { php_error_docref(NULL TSRMLS_CC, E_WARNING, "Supplied password hash too long to safely identify"); RETURN_FALSE; } ALLOC_INIT_ZVAL(options); array_init(options); algo = php_password_determine_algo(hash, (size_t) hash_len); algo_name = php_password_get_algo_name(algo); switch (algo) { case PHP_PASSWORD_BCRYPT: { long cost = PHP_PASSWORD_BCRYPT_COST; sscanf(hash, "$2y$%ld$", &cost); add_assoc_long(options, "cost", cost); } break; case PHP_PASSWORD_UNKNOWN: default: break; } array_init(return_value); add_assoc_long(return_value, "algo", algo); add_assoc_string(return_value, "algoName", algo_name, 1); add_assoc_zval(return_value, "options", options); } PHP_FUNCTION(password_needs_rehash) { long new_algo = 0; php_password_algo algo; int hash_len; char *hash; HashTable *options = 0; zval **option_buffer; if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "sl|H", &hash, &hash_len, &new_algo, &options) == FAILURE) { return; } if (hash_len < 0) { php_error_docref(NULL TSRMLS_CC, E_WARNING, "Supplied password hash too long to safely identify"); RETURN_FALSE; } algo = php_password_determine_algo(hash, (size_t) hash_len); if (algo != new_algo) { RETURN_TRUE; } switch (algo) { case PHP_PASSWORD_BCRYPT: { long new_cost = PHP_PASSWORD_BCRYPT_COST, cost = 0; if (options && zend_symtable_find(options, "cost", sizeof("cost"), (void **) &option_buffer) == SUCCESS) { if (Z_TYPE_PP(option_buffer) != IS_LONG) { zval cast_option_buffer; MAKE_COPY_ZVAL(option_buffer, &cast_option_buffer); convert_to_long(&cast_option_buffer); new_cost = Z_LVAL(cast_option_buffer); zval_dtor(&cast_option_buffer); } else { new_cost = Z_LVAL_PP(option_buffer); } } sscanf(hash, "$2y$%ld$", &cost); if (cost != new_cost) { RETURN_TRUE; } } break; case PHP_PASSWORD_UNKNOWN: default: break; } RETURN_FALSE; } /* {{{ proto boolean password_make_salt(string password, string hash) Verify a hash created using crypt() or password_hash() */ PHP_FUNCTION(password_verify) { int status = 0, i; int password_len, hash_len; char *ret, *password, *hash; if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "ss", &password, &password_len, &hash, &hash_len) == FAILURE) { RETURN_FALSE; } if (php_crypt(password, password_len, hash, hash_len, &ret) == FAILURE) { RETURN_FALSE; } if (strlen(ret) != hash_len || hash_len < 13) { efree(ret); RETURN_FALSE; } /* We're using this method instead of == in order to provide * resistence 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 |= (ret[i] ^ hash[i]); } efree(ret); RETURN_BOOL(status == 0); } /* }}} */ /* {{{ proto string password_hash(string password, int algo, array options = array()) Hash a password */ PHP_FUNCTION(password_hash) { char hash_format[8], *hash, *salt, *password, *result; long algo = 0; int password_len = 0, hash_len; size_t salt_len = 0, required_salt_len = 0, hash_format_len; HashTable *options = 0; zval **option_buffer; if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "sl|H", &password, &password_len, &algo, &options) == FAILURE) { return; } switch (algo) { case PHP_PASSWORD_BCRYPT: { long cost = PHP_PASSWORD_BCRYPT_COST; if (options && zend_symtable_find(options, "cost", 5, (void **) &option_buffer) == SUCCESS) { if (Z_TYPE_PP(option_buffer) != IS_LONG) { zval cast_option_buffer; MAKE_COPY_ZVAL(option_buffer, &cast_option_buffer); convert_to_long(&cast_option_buffer); cost = Z_LVAL(cast_option_buffer); zval_dtor(&cast_option_buffer); } else { cost = Z_LVAL_PP(option_buffer); } } if (cost < 4 || cost > 31) { php_error_docref(NULL TSRMLS_CC, E_WARNING, "Invalid bcrypt cost parameter specified: %ld", cost); RETURN_NULL(); } required_salt_len = 22; sprintf(hash_format, "$2y$%02ld$", cost); hash_format_len = 7; } break; case PHP_PASSWORD_UNKNOWN: default: php_error_docref(NULL TSRMLS_CC, E_WARNING, "Unknown password hashing algorithm: %ld", algo); RETURN_NULL(); } if (options && zend_symtable_find(options, "salt", 5, (void**) &option_buffer) == SUCCESS) { char *buffer; int buffer_len_int = 0; size_t buffer_len; zval cast_option_buffer = zval_used_for_init; switch (Z_TYPE_PP(option_buffer)) { case IS_STRING: buffer = Z_STRVAL_PP(option_buffer); buffer_len_int = Z_STRLEN_PP(option_buffer); break; case IS_LONG: case IS_DOUBLE: case IS_OBJECT: MAKE_COPY_ZVAL(option_buffer, &cast_option_buffer); convert_to_string(&cast_option_buffer); if (Z_TYPE(cast_option_buffer) == IS_STRING) { buffer = Z_STRVAL(cast_option_buffer); buffer_len_int = Z_STRLEN(cast_option_buffer); break; } case IS_BOOL: case IS_NULL: case IS_RESOURCE: case IS_ARRAY: default: if (Z_TYPE(cast_option_buffer) != IS_NULL) { zval_dtor(&cast_option_buffer); } php_error_docref(NULL TSRMLS_CC, E_WARNING, "Non-string salt parameter supplied"); RETURN_NULL(); } if (buffer_len_int < 0) { php_error_docref(NULL TSRMLS_CC, E_WARNING, "Supplied salt is too long"); if (Z_TYPE(cast_option_buffer) != IS_NULL) { zval_dtor(&cast_option_buffer); } RETURN_NULL(); } buffer_len = (size_t) buffer_len_int; if (buffer_len < required_salt_len) { php_error_docref(NULL TSRMLS_CC, E_WARNING, "Provided salt is too short: %lu expecting %lu", (unsigned long) buffer_len, (unsigned long) required_salt_len); if (Z_TYPE(cast_option_buffer) != IS_NULL) { zval_dtor(&cast_option_buffer); } RETURN_NULL(); } else if (php_password_salt_is_alphabet(buffer, buffer_len) == FAILURE) { salt = safe_emalloc(required_salt_len, 1, 1); if (php_password_salt_to64(buffer, buffer_len, required_salt_len, salt) == FAILURE) { efree(salt); php_error_docref(NULL TSRMLS_CC, E_WARNING, "Provided salt is too short: %lu", (unsigned long) buffer_len); if (Z_TYPE(cast_option_buffer) != IS_NULL) { zval_dtor(&cast_option_buffer); } RETURN_NULL(); } salt_len = required_salt_len; } else { salt = safe_emalloc(required_salt_len, 1, 1); memcpy(salt, buffer, (int) required_salt_len); salt_len = required_salt_len; } if (Z_TYPE(cast_option_buffer) != IS_NULL) { zval_dtor(&cast_option_buffer); } } else { salt = safe_emalloc(required_salt_len, 1, 1); if (php_password_make_salt(required_salt_len, salt TSRMLS_CC) == FAILURE) { efree(salt); RETURN_FALSE; } salt_len = required_salt_len; } 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; 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 (php_crypt(password, password_len, hash, hash_len, &result) == FAILURE) { efree(hash); RETURN_FALSE; } efree(hash); if (strlen(result) < 13) { efree(result); RETURN_FALSE; } RETURN_STRING(result, 0); } /* }}} */ #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 */
Name | Type | Size | Permission | Actions |
---|---|---|---|---|
array.c | File | 130.19 KB | 0644 |
|
assert.c | File | 9.31 KB | 0644 |
|
base64.c | File | 7.73 KB | 0644 |
|
base64.h | File | 1.57 KB | 0644 |
|
basic_functions.c | File | 169.08 KB | 0644 |
|
basic_functions.h | File | 7.42 KB | 0644 |
|
browscap.c | File | 16.82 KB | 0644 |
|
crc32.c | File | 1.77 KB | 0644 |
|
crc32.h | File | 4.78 KB | 0644 |
|
credits.c | File | 5.91 KB | 0644 |
|
credits.h | File | 1.7 KB | 0644 |
|
credits_ext.h | File | 5.51 KB | 0644 |
|
credits_sapi.h | File | 1.63 KB | 0644 |
|
crypt.c | File | 8.43 KB | 0644 |
|
crypt_blowfish.c | File | 31.68 KB | 0644 |
|
crypt_blowfish.h | File | 1.05 KB | 0644 |
|
crypt_freesec.c | File | 21.64 KB | 0644 |
|
crypt_freesec.h | File | 662 B | 0644 |
|
crypt_sha256.c | File | 21.77 KB | 0644 |
|
crypt_sha512.c | File | 26.45 KB | 0644 |
|
css.c | File | 2.43 KB | 0644 |
|
css.h | File | 1.21 KB | 0644 |
|
cyr_convert.c | File | 11.56 KB | 0644 |
|
datetime.c | File | 3.85 KB | 0644 |
|
dir.c | File | 15.08 KB | 0644 |
|
dl.c | File | 9.18 KB | 0644 |
|
dl.h | File | 1.57 KB | 0644 |
|
dns.c | File | 27.68 KB | 0644 |
|
exec.c | File | 13.13 KB | 0644 |
|
exec.h | File | 1.69 KB | 0644 |
|
file.c | File | 68.46 KB | 0644 |
|
file.h | File | 4.63 KB | 0644 |
|
filestat.c | File | 34.39 KB | 0644 |
|
filters.c | File | 56.51 KB | 0644 |
|
flock_compat.c | File | 6.9 KB | 0644 |
|
formatted_print.c | File | 20.19 KB | 0644 |
|
fsock.c | File | 3.89 KB | 0644 |
|
ftok.c | File | 2.22 KB | 0644 |
|
ftp_fopen_wrapper.c | File | 32.1 KB | 0644 |
|
head.c | File | 9.18 KB | 0644 |
|
head.h | File | 1.62 KB | 0644 |
|
html.c | File | 48.15 KB | 0644 |
|
html.h | File | 2.71 KB | 0644 |
|
html_tables.h | File | 471.57 KB | 0644 |
|
http.c | File | 7.7 KB | 0644 |
|
http_fopen_wrapper.c | File | 33.65 KB | 0644 |
|
image.c | File | 40.83 KB | 0644 |
|
incomplete_class.c | File | 5.61 KB | 0644 |
|
info.c | File | 44.03 KB | 0644 |
|
info.h | File | 20.2 KB | 0644 |
|
iptc.c | File | 9.85 KB | 0644 |
|
lcg.c | File | 3.11 KB | 0644 |
|
levenshtein.c | File | 4.05 KB | 0644 |
|
link.c | File | 5.83 KB | 0644 |
|
mail.c | File | 13.74 KB | 0644 |
|
math.c | File | 29.12 KB | 0644 |
|
md5.c | File | 10.65 KB | 0644 |
|
md5.h | File | 2.12 KB | 0644 |
|
metaphone.c | File | 11.84 KB | 0644 |
|
microtime.c | File | 4.36 KB | 0644 |
|
pack.c | File | 27.05 KB | 0644 |
|
pack.h | File | 1.25 KB | 0644 |
|
pageinfo.c | File | 3.92 KB | 0644 |
|
password.c | File | 12.06 KB | 0644 |
|
php_array.h | File | 4.62 KB | 0644 |
|
php_assert.h | File | 1.4 KB | 0644 |
|
php_browscap.h | File | 1.3 KB | 0644 |
|
php_crypt.h | File | 1.63 KB | 0644 |
|
php_crypt_r.c | File | 10.78 KB | 0644 |
|
php_crypt_r.h | File | 2 KB | 0644 |
|
php_dir.h | File | 1.67 KB | 0644 |
|
php_dns.h | File | 2.82 KB | 0644 |
|
php_ext_syslog.h | File | 1.47 KB | 0644 |
|
php_filestat.h | File | 3.28 KB | 0644 |
|
php_fopen_wrapper.c | File | 11.49 KB | 0644 |
|
php_fopen_wrappers.h | File | 1.92 KB | 0644 |
|
php_image.h | File | 2.37 KB | 0644 |
|
php_incomplete_class.h | File | 2.47 KB | 0644 |
|
php_lcg.h | File | 1.5 KB | 0644 |
|
php_mail.h | File | 1.37 KB | 0644 |
|
php_password.h | File | 1.58 KB | 0644 |
|
php_rand.h | File | 2.56 KB | 0644 |
|
php_smart_str.h | File | 6.57 KB | 0644 |
|
php_smart_str_public.h | File | 1.29 KB | 0644 |
|
php_standard.h | File | 2.21 KB | 0644 |
|
php_string.h | File | 6.23 KB | 0644 |
|
php_var.h | File | 7.33 KB | 0644 |
|
php_versioning.h | File | 1.37 KB | 0644 |
|
proc_open.c | File | 26 KB | 0644 |
|
proc_open.h | File | 1.81 KB | 0644 |
|
quot_print.c | File | 7.51 KB | 0644 |
|
quot_print.h | File | 1.51 KB | 0644 |
|
rand.c | File | 11.01 KB | 0644 |
|
scanf.c | File | 29.45 KB | 0644 |
|
scanf.h | File | 2.27 KB | 0644 |
|
sha1.c | File | 11.58 KB | 0644 |
|
sha1.h | File | 1.71 KB | 0644 |
|
soundex.c | File | 3.29 KB | 0644 |
|
streamsfuncs.c | File | 45.17 KB | 0644 |
|
string.c | File | 135.19 KB | 0644 |
|
strnatcmp.c | File | 4.57 KB | 0644 |
|
syslog.c | File | 6.35 KB | 0644 |
|
type.c | File | 9.06 KB | 0644 |
|
uniqid.c | File | 2.62 KB | 0644 |
|
url.c | File | 17.9 KB | 0644 |
|
url.h | File | 2.28 KB | 0644 |
|
url_scanner_ex.c | File | 27.89 KB | 0644 |
|
url_scanner_ex.h | File | 2.09 KB | 0644 |
|
user_filters.c | File | 18.47 KB | 0644 |
|
uuencode.c | File | 6.63 KB | 0644 |
|
var.c | File | 29.15 KB | 0644 |
|
var_unserializer.c | File | 29.18 KB | 0644 |
|
versioning.c | File | 5.87 KB | 0644 |
|