Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Fetching contributors…

Octocat-spinner-32-eaf2f5

Cannot retrieve contributors at this time

file 461 lines (401 sloc) 12.27 kb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460
/*
+----------------------------------------------------------------------+
| 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"
#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 zend_bool 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 0;
}
}
return 1;
}
/* }}} */

static zend_bool 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 zend_bool 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 || (size_t) 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, *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;
hash_format = emalloc(8);
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;
switch (Z_TYPE_PP(option_buffer)) {
case IS_STRING:
buffer = estrndup(Z_STRVAL_PP(option_buffer), Z_STRLEN_PP(option_buffer));
buffer_len_int = Z_STRLEN_PP(option_buffer);
break;
case IS_LONG:
case IS_DOUBLE:
case IS_OBJECT: {
zval cast_option_buffer;
MAKE_COPY_ZVAL(option_buffer, &cast_option_buffer);
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);
break;
}
zval_dtor(&cast_option_buffer);
}
case IS_BOOL:
case IS_NULL:
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();
}
if (buffer_len_int < 0) {
efree(hash_format);
efree(buffer);
php_error_docref(NULL TSRMLS_CC, E_WARNING, "Supplied salt is too long");
}
buffer_len = (size_t) buffer_len_int;
if (buffer_len < required_salt_len) {
efree(hash_format);
efree(buffer);
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);
RETURN_NULL();
} else if (0 == php_password_salt_is_alphabet(buffer, buffer_len)) {
salt = safe_emalloc(required_salt_len, 1, 1);
if (php_password_salt_to64(buffer, buffer_len, required_salt_len, salt) == FAILURE) {
efree(hash_format);
efree(buffer);
efree(salt);
php_error_docref(NULL TSRMLS_CC, E_WARNING, "Provided salt is too short: %lu", (unsigned long) buffer_len);
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;
}
efree(buffer);
} else {
salt = safe_emalloc(required_salt_len, 1, 1);
if (php_password_make_salt(required_salt_len, salt TSRMLS_CC) == FAILURE) {
efree(hash_format);
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(hash_format);
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
*/
Something went wrong with that request. Please try again.