2012-06-25 10:44:43 +08:00
|
|
|
/*
|
|
|
|
+----------------------------------------------------------------------+
|
|
|
|
| PHP Version 5 |
|
|
|
|
+----------------------------------------------------------------------+
|
|
|
|
| Copyright (c) 1997-2012 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"
|
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);
|
|
|
|
REGISTER_LONG_CONSTANT("PASSWORD_BCRYPT", PHP_PASSWORD_BCRYPT, CONST_CS | CONST_PERSISTENT);
|
2012-06-25 10:44:43 +08:00
|
|
|
return SUCCESS;
|
|
|
|
}
|
|
|
|
/* }}} */
|
|
|
|
|
2012-06-27 23:04:41 +08:00
|
|
|
static int php_password_salt_is_alphabet(const char *str, const int len) /* {{{ */
|
2012-06-25 10:44:43 +08:00
|
|
|
{
|
2012-06-27 23:04:41 +08:00
|
|
|
int 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 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return 1;
|
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-06-27 23:04:41 +08:00
|
|
|
static int php_password_salt_to64(const char *str, const int str_len, const int out_len, char *ret) /* {{{ */
|
2012-06-25 10:44:43 +08:00
|
|
|
{
|
2012-06-27 23:04:41 +08:00
|
|
|
int pos = 0;
|
2012-06-25 10:44:43 +08:00
|
|
|
unsigned char *buffer;
|
2012-06-27 23:04:41 +08:00
|
|
|
buffer = php_base64_encode((unsigned char*) str, str_len, NULL);
|
|
|
|
for (pos = 0; pos < out_len; pos++) {
|
|
|
|
if (buffer[pos] == '+') {
|
|
|
|
ret[pos] = '.';
|
2012-06-25 10:44:43 +08:00
|
|
|
} else if (buffer[pos] == '=') {
|
|
|
|
efree(buffer);
|
|
|
|
return FAILURE;
|
2012-06-27 23:04:41 +08:00
|
|
|
} else {
|
2012-06-25 10:44:43 +08:00
|
|
|
ret[pos] = buffer[pos];
|
|
|
|
}
|
2012-06-27 23:04:41 +08:00
|
|
|
}
|
2012-06-25 10:44:43 +08:00
|
|
|
efree(buffer);
|
|
|
|
return SUCCESS;
|
|
|
|
}
|
2012-06-27 23:04:41 +08:00
|
|
|
/* }}} */
|
2012-06-25 10:44:43 +08:00
|
|
|
|
2012-06-25 20:15:17 +08:00
|
|
|
#define PHP_PASSWORD_FUNCTION_EXISTS(func, func_len) (zend_hash_find(EG(function_table), (func), (func_len) + 1, (void **) &func_ptr) == SUCCESS && func_ptr->type == ZEND_INTERNAL_FUNCTION && func_ptr->internal_function.handler != zif_display_disabled_function)
|
|
|
|
|
2012-06-27 23:04:41 +08:00
|
|
|
static int php_password_make_salt(long length, int raw, char *ret TSRMLS_DC) /* {{{ */
|
2012-06-25 10:44:43 +08:00
|
|
|
{
|
2012-06-27 11:09:08 +08:00
|
|
|
int buffer_valid = 0;
|
|
|
|
long i, raw_length;
|
2012-06-25 10:44:43 +08:00
|
|
|
char *buffer;
|
2012-06-25 20:15:17 +08:00
|
|
|
|
2012-06-25 10:44:43 +08:00
|
|
|
if (raw) {
|
|
|
|
raw_length = length;
|
|
|
|
} else {
|
2012-06-27 11:09:08 +08:00
|
|
|
if (length > (LONG_MAX / 3)) {
|
|
|
|
php_error_docref(NULL TSRMLS_CC, E_WARNING, "Length is too large to safely generate");
|
|
|
|
return FAILURE;
|
|
|
|
}
|
2012-06-25 10:44:43 +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;
|
|
|
|
if (php_win32_get_random_bytes(iv_b, (size_t) 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;
|
|
|
|
}
|
|
|
|
read_bytes += n;
|
|
|
|
}
|
|
|
|
close(fd);
|
|
|
|
}
|
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
if (raw) {
|
|
|
|
memcpy(ret, buffer, length);
|
|
|
|
} else {
|
|
|
|
char *result;
|
2012-06-29 23:37:39 +08:00
|
|
|
result = safe_emalloc(length, 1, 1);
|
2012-06-25 10:44:43 +08:00
|
|
|
if (php_password_salt_to64(buffer, raw_length, length, result) == FAILURE) {
|
2012-06-25 20:50:39 +08:00
|
|
|
php_error_docref(NULL TSRMLS_CC, E_WARNING, "Generated salt too short");
|
2012-06-25 10:44:43 +08:00
|
|
|
efree(buffer);
|
|
|
|
efree(result);
|
|
|
|
return FAILURE;
|
|
|
|
} else {
|
|
|
|
memcpy(ret, result, length);
|
|
|
|
efree(result);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
efree(buffer);
|
|
|
|
ret[length] = 0;
|
|
|
|
return SUCCESS;
|
2012-06-27 23:04:41 +08:00
|
|
|
}
|
|
|
|
/* }}} */
|
2012-06-25 10:44:43 +08:00
|
|
|
|
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-06-29 03:29:40 +08:00
|
|
|
if (strlen(ret) != hash_len) {
|
|
|
|
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-06-27 23:04:41 +08:00
|
|
|
/* {{{ proto string password_make_salt(int length, boolean raw_output = false)
|
|
|
|
Make a new random salt */
|
2012-06-25 10:44:43 +08:00
|
|
|
PHP_FUNCTION(password_make_salt)
|
|
|
|
{
|
|
|
|
char *salt;
|
2012-06-27 11:09:08 +08:00
|
|
|
long length = 0;
|
2012-06-25 10:44:43 +08:00
|
|
|
zend_bool raw_output = 0;
|
|
|
|
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "l|b", &length, &raw_output) == FAILURE) {
|
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
|
|
|
if (length <= 0) {
|
2012-06-27 11:09:08 +08:00
|
|
|
php_error_docref(NULL TSRMLS_CC, E_WARNING, "Length cannot be less than or equal zero: %ld", length);
|
2012-06-29 03:29:40 +08:00
|
|
|
RETURN_NULL();
|
2012-06-27 11:09:08 +08:00
|
|
|
} else if (length > (LONG_MAX / 3)) {
|
|
|
|
php_error_docref(NULL TSRMLS_CC, E_WARNING, "Length is too large to safely generate");
|
2012-06-29 03:29:40 +08:00
|
|
|
RETURN_NULL();
|
2012-06-25 10:44:43 +08:00
|
|
|
}
|
2012-06-27 11:09:08 +08:00
|
|
|
|
2012-06-29 23:37:39 +08:00
|
|
|
salt = safe_emalloc(length, 1, 1);
|
2012-06-25 20:15:17 +08:00
|
|
|
if (php_password_make_salt(length, (int) raw_output, salt TSRMLS_CC) == FAILURE) {
|
2012-06-25 10:44:43 +08:00
|
|
|
efree(salt);
|
|
|
|
RETURN_FALSE;
|
|
|
|
}
|
|
|
|
RETURN_STRINGL(salt, length, 0);
|
|
|
|
}
|
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;
|
|
|
|
int algo = 0, salt_len = 0, required_salt_len = 0, hash_format_len, password_len;
|
2012-06-27 23:04:41 +08:00
|
|
|
HashTable *options = 0;
|
2012-06-29 03:29:40 +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-06-29 03:29:40 +08:00
|
|
|
RETURN_NULL();
|
2012-06-25 11:25:18 +08:00
|
|
|
}
|
|
|
|
|
2012-07-03 20:24:31 +08:00
|
|
|
switch (algo) {
|
|
|
|
case PHP_PASSWORD_BCRYPT:
|
|
|
|
{
|
|
|
|
int cost = PHP_PASSWORD_BCRYPT_COST;
|
|
|
|
|
|
|
|
if (options && zend_symtable_find(options, "cost", 5, (void **) &option_buffer) == SUCCESS) {
|
|
|
|
convert_to_long_ex(option_buffer);
|
|
|
|
cost = Z_LVAL_PP(option_buffer);
|
|
|
|
zval_ptr_dtor(option_buffer);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (cost < 4 || cost > 31) {
|
|
|
|
php_error_docref(NULL TSRMLS_CC, E_WARNING, "Invalid bcrypt cost parameter specified: %d", cost);
|
|
|
|
RETURN_NULL();
|
|
|
|
}
|
|
|
|
|
|
|
|
required_salt_len = 22;
|
|
|
|
hash_format = emalloc(8);
|
|
|
|
sprintf(hash_format, "$2y$%02d$", cost);
|
|
|
|
hash_format_len = 7;
|
2012-06-25 10:44:43 +08:00
|
|
|
}
|
2012-07-03 20:24:31 +08:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
php_error_docref(NULL TSRMLS_CC, E_WARNING, "Unknown password hashing algorithm: %d", 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
|
|
|
|
2012-06-27 23:04:41 +08:00
|
|
|
if (options && zend_symtable_find(options, "salt", 5, (void**) &option_buffer) == SUCCESS) {
|
2012-06-25 10:44:43 +08:00
|
|
|
char *buffer;
|
|
|
|
int buffer_len;
|
2012-06-29 03:29:40 +08:00
|
|
|
switch (Z_TYPE_PP(option_buffer)) {
|
|
|
|
case IS_NULL:
|
|
|
|
case IS_STRING:
|
|
|
|
case IS_LONG:
|
|
|
|
case IS_DOUBLE:
|
|
|
|
case IS_BOOL:
|
|
|
|
case IS_OBJECT:
|
|
|
|
convert_to_string_ex(option_buffer);
|
|
|
|
if (Z_TYPE_PP(option_buffer) == IS_STRING) {
|
|
|
|
buffer = Z_STRVAL_PP(option_buffer);
|
|
|
|
buffer_len = Z_STRLEN_PP(option_buffer);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case IS_RESOURCE:
|
|
|
|
case IS_ARRAY:
|
|
|
|
default:
|
|
|
|
zval_ptr_dtor(option_buffer);
|
|
|
|
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
|
|
|
}
|
|
|
|
if (buffer_len < required_salt_len) {
|
2012-06-25 10:44:43 +08:00
|
|
|
efree(hash_format);
|
2012-06-27 23:04:41 +08:00
|
|
|
zval_ptr_dtor(option_buffer);
|
|
|
|
php_error_docref(NULL TSRMLS_CC, E_WARNING, "Provided salt is too short: %d expecting %d", buffer_len, required_salt_len);
|
2012-06-29 03:29:40 +08:00
|
|
|
RETURN_NULL();
|
2012-06-27 23:04:41 +08:00
|
|
|
} else if (0 == php_password_salt_is_alphabet(buffer, buffer_len)) {
|
2012-06-25 10:44:43 +08:00
|
|
|
salt = emalloc(required_salt_len + 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-06-25 11:36:09 +08:00
|
|
|
efree(salt);
|
2012-06-27 23:04:41 +08:00
|
|
|
zval_ptr_dtor(option_buffer);
|
|
|
|
php_error_docref(NULL TSRMLS_CC, E_WARNING, "Provided salt is too short: %d", salt_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-06-25 10:44:43 +08:00
|
|
|
salt = emalloc(required_salt_len + 1);
|
|
|
|
memcpy(salt, buffer, required_salt_len);
|
2012-06-27 23:04:41 +08:00
|
|
|
salt_len = required_salt_len;
|
2012-06-25 10:44:43 +08:00
|
|
|
}
|
|
|
|
zval_ptr_dtor(option_buffer);
|
2012-06-27 23:04:41 +08:00
|
|
|
} else {
|
2012-06-25 10:44:43 +08:00
|
|
|
salt = emalloc(required_salt_len + 1);
|
2012-06-27 11:09:08 +08:00
|
|
|
if (php_password_make_salt((long) required_salt_len, 0, 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-06-29 23:32:25 +08:00
|
|
|
if (php_crypt(password, password_len, hash, hash_format_len + salt_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;
|
|
|
|
}
|
|
|
|
|
2012-06-29 23:32:25 +08:00
|
|
|
RETURN_STRING(result, 0);
|
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
|
|
|
|
*/
|