2012-06-25 10:44:43 +08:00
|
|
|
/*
|
|
|
|
+----------------------------------------------------------------------+
|
|
|
|
| PHP Version 5 |
|
|
|
|
+----------------------------------------------------------------------+
|
2014-01-03 11:08:10 +08:00
|
|
|
| Copyright (c) 1997-2014 The PHP Group |
|
2012-06-25 10:44:43 +08:00
|
|
|
+----------------------------------------------------------------------+
|
|
|
|
| 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"
|
2012-06-29 03:29:40 +08:00
|
|
|
#if HAVE_CRYPT
|
2012-06-26 09:22:16 +08:00
|
|
|
|
|
|
|
#include "fcntl.h"
|
2012-06-25 10:44:43 +08:00
|
|
|
#include "php_password.h"
|
|
|
|
#include "php_rand.h"
|
2012-06-29 03:29:40 +08:00
|
|
|
#include "php_crypt.h"
|
2012-06-25 10:44:43 +08:00
|
|
|
#include "base64.h"
|
2012-06-25 11:25:18 +08:00
|
|
|
#include "zend_interfaces.h"
|
2012-06-26 09:22:16 +08:00
|
|
|
#include "info.h"
|
|
|
|
|
|
|
|
#if PHP_WIN32
|
|
|
|
#include "win32/winutil.h"
|
|
|
|
#endif
|
|
|
|
|
2012-06-25 10:44:43 +08:00
|
|
|
PHP_MINIT_FUNCTION(password) /* {{{ */
|
|
|
|
{
|
2012-07-03 20:24:31 +08:00
|
|
|
REGISTER_LONG_CONSTANT("PASSWORD_DEFAULT", PHP_PASSWORD_DEFAULT, CONST_CS | CONST_PERSISTENT);
|
2012-09-17 22:52:07 +08:00
|
|
|
REGISTER_LONG_CONSTANT("PASSWORD_BCRYPT", PHP_PASSWORD_BCRYPT, CONST_CS | CONST_PERSISTENT);
|
2012-07-12 10:15:56 +08:00
|
|
|
|
2012-09-12 23:47:50 +08:00
|
|
|
REGISTER_LONG_CONSTANT("PASSWORD_BCRYPT_DEFAULT_COST", PHP_PASSWORD_BCRYPT_COST, CONST_CS | CONST_PERSISTENT);
|
|
|
|
|
2012-06-25 10:44:43 +08:00
|
|
|
return SUCCESS;
|
|
|
|
}
|
|
|
|
/* }}} */
|
|
|
|
|
2012-09-17 22:52:07 +08:00
|
|
|
static char* php_password_get_algo_name(const php_password_algo algo)
|
2012-09-04 23:34:00 +08:00
|
|
|
{
|
|
|
|
switch (algo) {
|
2012-09-17 22:52:07 +08:00
|
|
|
case PHP_PASSWORD_BCRYPT:
|
2012-09-04 23:34:00 +08:00
|
|
|
return "bcrypt";
|
2012-09-17 22:52:07 +08:00
|
|
|
case PHP_PASSWORD_UNKNOWN:
|
2012-09-04 23:34:00 +08:00
|
|
|
default:
|
|
|
|
return "unknown";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-09-17 22:52:07 +08:00
|
|
|
static php_password_algo php_password_determine_algo(const char *hash, const size_t len)
|
2012-07-06 04:22:49 +08:00
|
|
|
{
|
2012-09-13 22:32:54 +08:00
|
|
|
if (len > 3 && hash[0] == '$' && hash[1] == '2' && hash[2] == 'y' && len == 60) {
|
2012-09-17 22:52:07 +08:00
|
|
|
return PHP_PASSWORD_BCRYPT;
|
2012-07-06 04:22:49 +08:00
|
|
|
}
|
|
|
|
|
2012-09-17 22:52:07 +08:00
|
|
|
return PHP_PASSWORD_UNKNOWN;
|
2012-07-06 04:22:49 +08:00
|
|
|
}
|
|
|
|
|
2013-08-22 00:10:40 +08:00
|
|
|
static int php_password_salt_is_alphabet(const char *str, const size_t len) /* {{{ */
|
2012-06-25 10:44:43 +08:00
|
|
|
{
|
2012-09-04 23:34:00 +08:00
|
|
|
size_t i = 0;
|
2012-06-27 23:04:41 +08:00
|
|
|
|
2012-09-04 23:34:00 +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] == '/')) {
|
2013-08-22 00:10:40 +08:00
|
|
|
return FAILURE;
|
2012-06-27 23:04:41 +08:00
|
|
|
}
|
|
|
|
}
|
2013-08-22 00:10:40 +08:00
|
|
|
return SUCCESS;
|
2012-06-25 10:44:43 +08:00
|
|
|
}
|
2012-06-27 23:04:41 +08:00
|
|
|
/* }}} */
|
2012-06-25 10:44:43 +08:00
|
|
|
|
2013-08-22 00:10:40 +08:00
|
|
|
static int php_password_salt_to64(const char *str, const size_t str_len, const size_t out_len, char *ret) /* {{{ */
|
2012-06-25 10:44:43 +08:00
|
|
|
{
|
2012-09-04 23:34:00 +08:00
|
|
|
size_t pos = 0;
|
2014-02-24 18:48:22 +08:00
|
|
|
zend_string *buffer;
|
2012-09-04 23:34:00 +08:00
|
|
|
if ((int) str_len < 0) {
|
|
|
|
return FAILURE;
|
|
|
|
}
|
2014-02-24 18:48:22 +08:00
|
|
|
buffer = php_base64_encode((unsigned char*) str, (int) str_len);
|
|
|
|
if (buffer->len < out_len) {
|
2012-09-04 23:34:00 +08:00
|
|
|
/* Too short of an encoded string generated */
|
2014-02-24 18:48:22 +08:00
|
|
|
STR_RELEASE(buffer);
|
2012-09-04 23:34:00 +08:00
|
|
|
return FAILURE;
|
|
|
|
}
|
2012-06-27 23:04:41 +08:00
|
|
|
for (pos = 0; pos < out_len; pos++) {
|
2014-02-24 18:48:22 +08:00
|
|
|
if (buffer->val[pos] == '+') {
|
2012-06-27 23:04:41 +08:00
|
|
|
ret[pos] = '.';
|
2014-02-24 18:48:22 +08:00
|
|
|
} else if (buffer->val[pos] == '=') {
|
|
|
|
STR_FREE(buffer);
|
2012-06-25 10:44:43 +08:00
|
|
|
return FAILURE;
|
2012-06-27 23:04:41 +08:00
|
|
|
} else {
|
2014-02-24 18:48:22 +08:00
|
|
|
ret[pos] = buffer->val[pos];
|
2012-06-25 10:44:43 +08:00
|
|
|
}
|
2012-06-27 23:04:41 +08:00
|
|
|
}
|
2014-02-24 18:48:22 +08:00
|
|
|
STR_FREE(buffer);
|
2012-06-25 10:44:43 +08:00
|
|
|
return SUCCESS;
|
|
|
|
}
|
2012-06-27 23:04:41 +08:00
|
|
|
/* }}} */
|
2012-06-25 10:44:43 +08:00
|
|
|
|
2013-08-22 00:10:40 +08:00
|
|
|
static int php_password_make_salt(size_t length, char *ret TSRMLS_DC) /* {{{ */
|
2012-06-25 10:44:43 +08:00
|
|
|
{
|
2012-06-27 11:09:08 +08:00
|
|
|
int buffer_valid = 0;
|
2012-09-04 23:34:00 +08:00
|
|
|
size_t i, raw_length;
|
2012-06-25 10:44:43 +08:00
|
|
|
char *buffer;
|
2012-09-04 23:34:00 +08:00
|
|
|
char *result;
|
2012-06-25 20:15:17 +08:00
|
|
|
|
2012-09-04 23:34:00 +08:00
|
|
|
if (length > (INT_MAX / 3)) {
|
|
|
|
php_error_docref(NULL TSRMLS_CC, E_WARNING, "Length is too large to safely generate");
|
2012-07-12 10:15:56 +08:00
|
|
|
return FAILURE;
|
2012-06-25 10:44:43 +08:00
|
|
|
}
|
2012-09-04 23:34:00 +08:00
|
|
|
|
|
|
|
raw_length = length * 3 / 4 + 1;
|
|
|
|
|
2012-06-29 23:37:39 +08:00
|
|
|
buffer = (char *) safe_emalloc(raw_length, 1, 1);
|
2012-06-26 09:22:16 +08:00
|
|
|
|
|
|
|
#if PHP_WIN32
|
|
|
|
{
|
|
|
|
BYTE *iv_b = (BYTE *) buffer;
|
2012-09-04 23:34:00 +08:00
|
|
|
if (php_win32_get_random_bytes(iv_b, raw_length) == SUCCESS) {
|
2012-06-25 20:15:17 +08:00
|
|
|
buffer_valid = 1;
|
|
|
|
}
|
|
|
|
}
|
2012-06-26 09:22:16 +08:00
|
|
|
#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;
|
|
|
|
}
|
2012-09-04 23:34:00 +08:00
|
|
|
read_bytes += (size_t) n;
|
2012-06-26 09:22:16 +08:00
|
|
|
}
|
|
|
|
close(fd);
|
|
|
|
}
|
2012-09-04 23:34:00 +08:00
|
|
|
if (read_bytes >= raw_length) {
|
2012-06-25 23:37:48 +08:00
|
|
|
buffer_valid = 1;
|
|
|
|
}
|
|
|
|
}
|
2012-06-26 09:22:16 +08:00
|
|
|
#endif
|
2012-06-25 20:15:17 +08:00
|
|
|
if (!buffer_valid) {
|
|
|
|
for (i = 0; i < raw_length; i++) {
|
2012-06-26 09:22:16 +08:00
|
|
|
buffer[i] ^= (char) (255.0 * php_rand(TSRMLS_C) / RAND_MAX);
|
2012-06-25 20:15:17 +08:00
|
|
|
}
|
2012-06-25 10:44:43 +08:00
|
|
|
}
|
|
|
|
|
2012-09-04 23:34:00 +08:00
|
|
|
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;
|
2012-06-25 10:44:43 +08:00
|
|
|
}
|
2012-10-06 03:31:58 +08:00
|
|
|
memcpy(ret, result, (int) length);
|
2012-09-04 23:34:00 +08:00
|
|
|
efree(result);
|
2012-06-25 10:44:43 +08:00
|
|
|
efree(buffer);
|
|
|
|
ret[length] = 0;
|
|
|
|
return SUCCESS;
|
2012-06-27 23:04:41 +08:00
|
|
|
}
|
|
|
|
/* }}} */
|
2012-06-25 10:44:43 +08:00
|
|
|
|
2012-07-06 05:46:33 +08:00
|
|
|
PHP_FUNCTION(password_get_info)
|
|
|
|
{
|
2012-09-17 22:52:07 +08:00
|
|
|
php_password_algo algo;
|
2012-07-06 05:46:33 +08:00
|
|
|
int hash_len;
|
2012-09-17 22:52:07 +08:00
|
|
|
char *hash, *algo_name;
|
2014-02-14 17:40:11 +08:00
|
|
|
zval options;
|
2012-07-06 05:46:33 +08:00
|
|
|
|
2012-07-06 05:58:19 +08:00
|
|
|
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s", &hash, &hash_len) == FAILURE) {
|
2012-09-17 22:52:07 +08:00
|
|
|
return;
|
2012-07-06 05:58:19 +08:00
|
|
|
}
|
2012-07-06 05:46:33 +08:00
|
|
|
|
2013-10-20 05:46:09 +08:00
|
|
|
if (hash_len < 0) {
|
2012-09-17 22:59:51 +08:00
|
|
|
php_error_docref(NULL TSRMLS_CC, E_WARNING, "Supplied password hash too long to safely identify");
|
2012-09-04 23:34:00 +08:00
|
|
|
RETURN_FALSE;
|
|
|
|
}
|
|
|
|
|
2014-02-14 17:40:11 +08:00
|
|
|
array_init(&options);
|
2012-07-06 05:46:33 +08:00
|
|
|
|
2012-09-04 23:34:00 +08:00
|
|
|
algo = php_password_determine_algo(hash, (size_t) hash_len);
|
2012-09-17 22:52:07 +08:00
|
|
|
algo_name = php_password_get_algo_name(algo);
|
2012-07-06 05:46:33 +08:00
|
|
|
|
|
|
|
switch (algo) {
|
2012-09-17 22:52:07 +08:00
|
|
|
case PHP_PASSWORD_BCRYPT:
|
2012-07-06 05:46:33 +08:00
|
|
|
{
|
|
|
|
long cost = PHP_PASSWORD_BCRYPT_COST;
|
|
|
|
sscanf(hash, "$2y$%ld$", &cost);
|
2014-02-14 17:40:11 +08:00
|
|
|
add_assoc_long(&options, "cost", cost);
|
2012-07-06 05:46:33 +08:00
|
|
|
}
|
2012-09-13 22:32:54 +08:00
|
|
|
break;
|
2012-09-17 22:52:07 +08:00
|
|
|
case PHP_PASSWORD_UNKNOWN:
|
2012-09-13 22:32:54 +08:00
|
|
|
default:
|
|
|
|
break;
|
2012-07-06 05:46:33 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
array_init(return_value);
|
|
|
|
|
|
|
|
add_assoc_long(return_value, "algo", algo);
|
2012-09-17 22:52:07 +08:00
|
|
|
add_assoc_string(return_value, "algoName", algo_name, 1);
|
2014-02-14 17:40:11 +08:00
|
|
|
add_assoc_zval(return_value, "options", &options);
|
2012-07-06 05:46:33 +08:00
|
|
|
}
|
|
|
|
|
2012-07-06 04:22:49 +08:00
|
|
|
PHP_FUNCTION(password_needs_rehash)
|
|
|
|
{
|
2012-09-13 22:32:54 +08:00
|
|
|
long new_algo = 0;
|
2012-09-17 22:52:07 +08:00
|
|
|
php_password_algo algo;
|
2012-07-06 04:22:49 +08:00
|
|
|
int hash_len;
|
|
|
|
char *hash;
|
|
|
|
HashTable *options = 0;
|
2014-02-14 17:40:11 +08:00
|
|
|
zval *option_buffer;
|
2012-07-06 04:22:49 +08:00
|
|
|
|
|
|
|
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "sl|H", &hash, &hash_len, &new_algo, &options) == FAILURE) {
|
2012-09-17 22:52:07 +08:00
|
|
|
return;
|
2012-07-06 05:58:19 +08:00
|
|
|
}
|
2012-09-04 23:34:00 +08:00
|
|
|
|
|
|
|
if (hash_len < 0) {
|
2012-09-17 22:59:51 +08:00
|
|
|
php_error_docref(NULL TSRMLS_CC, E_WARNING, "Supplied password hash too long to safely identify");
|
2012-09-04 23:34:00 +08:00
|
|
|
RETURN_FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
algo = php_password_determine_algo(hash, (size_t) hash_len);
|
2012-07-06 04:22:49 +08:00
|
|
|
|
|
|
|
if (algo != new_algo) {
|
|
|
|
RETURN_TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
switch (algo) {
|
2012-09-17 22:52:07 +08:00
|
|
|
case PHP_PASSWORD_BCRYPT:
|
2012-07-06 04:22:49 +08:00
|
|
|
{
|
2012-09-17 23:43:47 +08:00
|
|
|
long new_cost = PHP_PASSWORD_BCRYPT_COST, cost = 0;
|
2012-07-06 04:22:49 +08:00
|
|
|
|
2014-02-14 17:40:11 +08:00
|
|
|
if (options && (option_buffer = zend_symtable_str_find(options, "cost", sizeof("cost")-1)) != NULL) {
|
|
|
|
if (Z_TYPE_P(option_buffer) != IS_LONG) {
|
2012-10-07 17:42:08 +08:00
|
|
|
zval cast_option_buffer;
|
2014-02-14 17:40:11 +08:00
|
|
|
ZVAL_DUP(&cast_option_buffer, option_buffer);
|
2012-10-07 17:42:08 +08:00
|
|
|
convert_to_long(&cast_option_buffer);
|
|
|
|
new_cost = Z_LVAL(cast_option_buffer);
|
|
|
|
zval_dtor(&cast_option_buffer);
|
2012-10-06 03:31:58 +08:00
|
|
|
} else {
|
2014-02-14 17:40:11 +08:00
|
|
|
new_cost = Z_LVAL_P(option_buffer);
|
2012-10-06 03:31:58 +08:00
|
|
|
}
|
2012-07-06 04:22:49 +08:00
|
|
|
}
|
|
|
|
|
2012-09-17 23:43:47 +08:00
|
|
|
sscanf(hash, "$2y$%ld$", &cost);
|
|
|
|
if (cost != new_cost) {
|
2012-07-06 04:22:49 +08:00
|
|
|
RETURN_TRUE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
2012-09-17 22:52:07 +08:00
|
|
|
case PHP_PASSWORD_UNKNOWN:
|
2012-09-13 22:32:54 +08:00
|
|
|
default:
|
|
|
|
break;
|
2012-07-06 04:22:49 +08:00
|
|
|
}
|
|
|
|
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() */
|
2012-06-25 10:44:43 +08:00
|
|
|
PHP_FUNCTION(password_verify)
|
|
|
|
{
|
2012-06-25 11:35:26 +08:00
|
|
|
int status = 0, i;
|
2012-06-29 03:29:40 +08:00
|
|
|
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) {
|
2012-06-27 23:04:41 +08:00
|
|
|
RETURN_FALSE;
|
|
|
|
}
|
2012-06-29 23:32:25 +08:00
|
|
|
if (php_crypt(password, password_len, hash, hash_len, &ret) == FAILURE) {
|
2012-06-25 11:35:26 +08:00
|
|
|
RETURN_FALSE;
|
|
|
|
}
|
|
|
|
|
2012-09-04 23:34:00 +08:00
|
|
|
if (strlen(ret) != hash_len || hash_len < 13) {
|
2012-06-29 03:29:40 +08:00
|
|
|
efree(ret);
|
2012-06-25 11:35:26 +08:00
|
|
|
RETURN_FALSE;
|
|
|
|
}
|
2012-06-27 09:15:56 +08:00
|
|
|
|
|
|
|
/* 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. */
|
2012-06-29 03:29:40 +08:00
|
|
|
for (i = 0; i < hash_len; i++) {
|
|
|
|
status |= (ret[i] ^ hash[i]);
|
2012-06-25 11:35:26 +08:00
|
|
|
}
|
|
|
|
|
2012-06-29 03:29:40 +08:00
|
|
|
efree(ret);
|
2012-06-25 11:35:26 +08:00
|
|
|
|
|
|
|
RETURN_BOOL(status == 0);
|
|
|
|
|
2012-06-25 10:44:43 +08:00
|
|
|
}
|
2012-06-27 23:04:41 +08:00
|
|
|
/* }}} */
|
2012-06-25 10:44:43 +08:00
|
|
|
|
2012-07-03 20:26:50 +08:00
|
|
|
/* {{{ proto string password_hash(string password, int algo, array options = array())
|
2012-06-25 10:44:43 +08:00
|
|
|
Hash a password */
|
2012-06-26 09:22:16 +08:00
|
|
|
PHP_FUNCTION(password_hash)
|
2012-06-25 10:44:43 +08:00
|
|
|
{
|
2012-07-03 20:24:31 +08:00
|
|
|
char *hash_format, *hash, *salt, *password, *result;
|
2012-07-06 05:31:40 +08:00
|
|
|
long algo = 0;
|
2012-09-04 23:34:00 +08:00
|
|
|
int password_len = 0, 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;
|
2014-02-14 17:40:11 +08:00
|
|
|
zval *option_buffer;
|
2012-06-25 10:44:43 +08:00
|
|
|
|
2012-07-03 20:24:31 +08:00
|
|
|
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "sl|H", &password, &password_len, &algo, &options) == FAILURE) {
|
2012-09-17 22:52:07 +08:00
|
|
|
return;
|
2012-06-25 11:25:18 +08:00
|
|
|
}
|
|
|
|
|
2012-07-03 20:24:31 +08:00
|
|
|
switch (algo) {
|
2012-09-17 22:52:07 +08:00
|
|
|
case PHP_PASSWORD_BCRYPT:
|
2012-07-03 20:24:31 +08:00
|
|
|
{
|
2012-09-04 23:34:00 +08:00
|
|
|
long cost = PHP_PASSWORD_BCRYPT_COST;
|
2012-07-03 20:24:31 +08:00
|
|
|
|
2014-02-14 17:40:11 +08:00
|
|
|
if (options && (option_buffer = zend_symtable_str_find(options, "cost", sizeof("cost")-1)) != NULL) {
|
|
|
|
if (Z_TYPE_P(option_buffer) != IS_LONG) {
|
2012-10-07 17:42:08 +08:00
|
|
|
zval cast_option_buffer;
|
2014-02-14 17:40:11 +08:00
|
|
|
ZVAL_DUP(&cast_option_buffer, option_buffer);
|
2012-10-07 17:42:08 +08:00
|
|
|
convert_to_long(&cast_option_buffer);
|
|
|
|
cost = Z_LVAL(cast_option_buffer);
|
|
|
|
zval_dtor(&cast_option_buffer);
|
2012-10-06 03:31:58 +08:00
|
|
|
} else {
|
2014-02-14 17:40:11 +08:00
|
|
|
cost = Z_LVAL_P(option_buffer);
|
2012-10-06 03:31:58 +08:00
|
|
|
}
|
2012-07-03 20:24:31 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
if (cost < 4 || cost > 31) {
|
2012-09-04 23:34:00 +08:00
|
|
|
php_error_docref(NULL TSRMLS_CC, E_WARNING, "Invalid bcrypt cost parameter specified: %ld", cost);
|
2012-07-03 20:24:31 +08:00
|
|
|
RETURN_NULL();
|
|
|
|
}
|
|
|
|
|
|
|
|
required_salt_len = 22;
|
|
|
|
hash_format = emalloc(8);
|
2012-09-04 23:34:00 +08:00
|
|
|
sprintf(hash_format, "$2y$%02ld$", cost);
|
2012-07-03 20:24:31 +08:00
|
|
|
hash_format_len = 7;
|
2012-06-25 10:44:43 +08:00
|
|
|
}
|
2012-07-03 20:24:31 +08:00
|
|
|
break;
|
2012-09-17 22:52:07 +08:00
|
|
|
case PHP_PASSWORD_UNKNOWN:
|
2012-07-03 20:24:31 +08:00
|
|
|
default:
|
2012-07-06 05:31:40 +08:00
|
|
|
php_error_docref(NULL TSRMLS_CC, E_WARNING, "Unknown password hashing algorithm: %ld", algo);
|
2012-06-29 03:29:40 +08:00
|
|
|
RETURN_NULL();
|
2012-06-27 23:04:41 +08:00
|
|
|
}
|
2012-06-25 10:44:43 +08:00
|
|
|
|
2014-02-14 17:40:11 +08:00
|
|
|
if (options && (option_buffer = zend_symtable_str_find(options, "salt", sizeof("salt")-1)) != NULL) {
|
2012-06-25 10:44:43 +08:00
|
|
|
char *buffer;
|
2012-10-06 03:53:40 +08:00
|
|
|
int buffer_len_int = 0;
|
2012-09-04 23:34:00 +08:00
|
|
|
size_t buffer_len;
|
2014-02-14 17:40:11 +08:00
|
|
|
switch (Z_TYPE_P(option_buffer)) {
|
2012-06-29 03:29:40 +08:00
|
|
|
case IS_STRING:
|
2014-02-14 17:40:11 +08:00
|
|
|
buffer = estrndup(Z_STRVAL_P(option_buffer), Z_STRLEN_P(option_buffer));
|
|
|
|
buffer_len_int = Z_STRLEN_P(option_buffer);
|
2012-10-06 22:38:41 +08:00
|
|
|
break;
|
2012-06-29 03:29:40 +08:00
|
|
|
case IS_LONG:
|
|
|
|
case IS_DOUBLE:
|
2012-10-06 22:38:41 +08:00
|
|
|
case IS_OBJECT: {
|
2012-10-07 17:42:08 +08:00
|
|
|
zval cast_option_buffer;
|
2014-02-14 17:40:11 +08:00
|
|
|
|
|
|
|
ZVAL_DUP(&cast_option_buffer, option_buffer);
|
2012-10-07 17:42:08 +08:00
|
|
|
convert_to_string(&cast_option_buffer);
|
|
|
|
if (Z_TYPE(cast_option_buffer) == IS_STRING) {
|
|
|
|
buffer = estrndup(Z_STRVAL(cast_option_buffer), Z_STRLEN(cast_option_buffer));
|
|
|
|
buffer_len_int = Z_STRLEN(cast_option_buffer);
|
|
|
|
zval_dtor(&cast_option_buffer);
|
2012-10-06 03:53:40 +08:00
|
|
|
break;
|
2012-06-29 03:29:40 +08:00
|
|
|
}
|
2012-10-07 17:42:08 +08:00
|
|
|
zval_dtor(&cast_option_buffer);
|
2012-10-06 22:38:41 +08:00
|
|
|
}
|
|
|
|
case IS_BOOL:
|
|
|
|
case IS_NULL:
|
2012-06-29 03:29:40 +08:00
|
|
|
case IS_RESOURCE:
|
|
|
|
case IS_ARRAY:
|
|
|
|
default:
|
|
|
|
efree(hash_format);
|
|
|
|
php_error_docref(NULL TSRMLS_CC, E_WARNING, "Non-string salt parameter supplied");
|
|
|
|
RETURN_NULL();
|
2012-06-27 23:04:41 +08:00
|
|
|
}
|
2012-10-06 03:53:40 +08:00
|
|
|
if (buffer_len_int < 0) {
|
|
|
|
efree(hash_format);
|
2012-10-06 22:38:41 +08:00
|
|
|
efree(buffer);
|
2012-10-06 03:53:40 +08:00
|
|
|
php_error_docref(NULL TSRMLS_CC, E_WARNING, "Supplied salt is too long");
|
|
|
|
}
|
|
|
|
buffer_len = (size_t) buffer_len_int;
|
2012-06-27 23:04:41 +08:00
|
|
|
if (buffer_len < required_salt_len) {
|
2012-06-25 10:44:43 +08:00
|
|
|
efree(hash_format);
|
2012-10-06 22:38:41 +08:00
|
|
|
efree(buffer);
|
2012-09-04 23:34:00 +08:00
|
|
|
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);
|
2012-06-29 03:29:40 +08:00
|
|
|
RETURN_NULL();
|
2013-08-22 00:10:40 +08:00
|
|
|
} else if (php_password_salt_is_alphabet(buffer, buffer_len) == FAILURE) {
|
2012-09-04 23:34:00 +08:00
|
|
|
salt = safe_emalloc(required_salt_len, 1, 1);
|
2012-06-27 23:04:41 +08:00
|
|
|
if (php_password_salt_to64(buffer, buffer_len, required_salt_len, salt) == FAILURE) {
|
2012-06-25 10:44:43 +08:00
|
|
|
efree(hash_format);
|
2012-10-06 22:38:41 +08:00
|
|
|
efree(buffer);
|
2012-06-25 11:36:09 +08:00
|
|
|
efree(salt);
|
2012-09-04 23:34:00 +08:00
|
|
|
php_error_docref(NULL TSRMLS_CC, E_WARNING, "Provided salt is too short: %lu", (unsigned long) buffer_len);
|
2012-06-29 03:29:40 +08:00
|
|
|
RETURN_NULL();
|
2012-06-25 10:44:43 +08:00
|
|
|
}
|
2012-06-27 23:04:41 +08:00
|
|
|
salt_len = required_salt_len;
|
|
|
|
} else {
|
2012-09-04 23:34:00 +08:00
|
|
|
salt = safe_emalloc(required_salt_len, 1, 1);
|
|
|
|
memcpy(salt, buffer, (int) required_salt_len);
|
2012-06-27 23:04:41 +08:00
|
|
|
salt_len = required_salt_len;
|
2012-06-25 10:44:43 +08:00
|
|
|
}
|
2012-10-06 22:38:41 +08:00
|
|
|
efree(buffer);
|
2012-06-27 23:04:41 +08:00
|
|
|
} else {
|
2012-09-04 23:34:00 +08:00
|
|
|
salt = safe_emalloc(required_salt_len, 1, 1);
|
|
|
|
if (php_password_make_salt(required_salt_len, salt TSRMLS_CC) == FAILURE) {
|
2012-06-25 10:44:43 +08:00
|
|
|
efree(hash_format);
|
|
|
|
efree(salt);
|
|
|
|
RETURN_FALSE;
|
|
|
|
}
|
2012-06-27 23:04:41 +08:00
|
|
|
salt_len = required_salt_len;
|
|
|
|
}
|
2012-06-25 10:44:43 +08:00
|
|
|
|
|
|
|
salt[salt_len] = 0;
|
|
|
|
|
2012-06-29 23:37:39 +08:00
|
|
|
hash = safe_emalloc(salt_len + hash_format_len, 1, 1);
|
2012-06-25 10:44:43 +08:00
|
|
|
sprintf(hash, "%s%s", hash_format, salt);
|
|
|
|
hash[hash_format_len + salt_len] = 0;
|
2012-06-25 11:25:18 +08:00
|
|
|
|
2012-06-25 10:44:43 +08:00
|
|
|
efree(hash_format);
|
|
|
|
efree(salt);
|
|
|
|
|
2012-09-04 23:34:00 +08:00
|
|
|
/* 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) {
|
2012-06-29 03:29:40 +08:00
|
|
|
efree(hash);
|
|
|
|
RETURN_FALSE;
|
|
|
|
}
|
2012-06-25 11:25:18 +08:00
|
|
|
|
2012-06-29 03:29:40 +08:00
|
|
|
efree(hash);
|
2012-06-25 11:25:18 +08:00
|
|
|
|
2012-06-29 03:29:40 +08:00
|
|
|
if (strlen(result) < 13) {
|
|
|
|
efree(result);
|
2012-06-25 11:25:18 +08:00
|
|
|
RETURN_FALSE;
|
|
|
|
}
|
|
|
|
|
2014-02-14 17:40:11 +08:00
|
|
|
//??? RETURN_STRING(result, 0);
|
|
|
|
RETURN_STRING(result);
|
2012-06-25 10:44:43 +08:00
|
|
|
}
|
|
|
|
/* }}} */
|
|
|
|
|
2012-06-29 03:29:40 +08:00
|
|
|
#endif /* HAVE_CRYPT */
|
2012-06-25 10:44:43 +08:00
|
|
|
/*
|
|
|
|
* Local variables:
|
|
|
|
* tab-width: 4
|
|
|
|
* c-basic-offset: 4
|
|
|
|
* End:
|
|
|
|
* vim600: sw=4 ts=4 fdm=marker
|
|
|
|
* vim<600: sw=4 ts=4
|
|
|
|
*/
|