PHP  
 PHP: Test and Code Coverage Analysis
downloads | QA | documentation | faq | getting help | mailing lists | reporting bugs | php.net sites | links | my php.net 
 

LCOV - code coverage report
Current view: top level - ext/standard - password.c (source / functions) Hit Total Coverage
Test: PHP Code Coverage Lines: 157 184 85.3 %
Date: 2015-08-29 Functions: 10 10 100.0 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : /*
       2             :    +----------------------------------------------------------------------+
       3             :    | PHP Version 7                                                        |
       4             :    +----------------------------------------------------------------------+
       5             :    | Copyright (c) 1997-2015 The PHP Group                                |
       6             :    +----------------------------------------------------------------------+
       7             :    | This source file is subject to version 3.01 of the PHP license,      |
       8             :    | that is bundled with this package in the file LICENSE, and is        |
       9             :    | available through the world-wide-web at the following url:           |
      10             :    | http://www.php.net/license/3_01.txt                                  |
      11             :    | If you did not receive a copy of the PHP license and are unable to   |
      12             :    | obtain it through the world-wide-web, please send a note to          |
      13             :    | license@php.net so we can mail you a copy immediately.               |
      14             :    +----------------------------------------------------------------------+
      15             :    | Authors: Anthony Ferrara <ircmaxell@php.net>                         |
      16             :    +----------------------------------------------------------------------+
      17             : */
      18             : 
      19             : /* $Id$ */
      20             : 
      21             : #include <stdlib.h>
      22             : 
      23             : #include "php.h"
      24             : #if HAVE_CRYPT
      25             : 
      26             : #include "fcntl.h"
      27             : #include "php_password.h"
      28             : #include "php_rand.h"
      29             : #include "php_crypt.h"
      30             : #include "base64.h"
      31             : #include "zend_interfaces.h"
      32             : #include "info.h"
      33             : 
      34             : #if PHP_WIN32
      35             : #include "win32/winutil.h"
      36             : #endif
      37             : 
      38       21291 : PHP_MINIT_FUNCTION(password) /* {{{ */
      39             : {
      40       21291 :         REGISTER_LONG_CONSTANT("PASSWORD_DEFAULT", PHP_PASSWORD_DEFAULT, CONST_CS | CONST_PERSISTENT);
      41       21291 :         REGISTER_LONG_CONSTANT("PASSWORD_BCRYPT", PHP_PASSWORD_BCRYPT, CONST_CS | CONST_PERSISTENT);
      42             : 
      43       21291 :         REGISTER_LONG_CONSTANT("PASSWORD_BCRYPT_DEFAULT_COST", PHP_PASSWORD_BCRYPT_COST, CONST_CS | CONST_PERSISTENT);
      44             : 
      45       21291 :         return SUCCESS;
      46             : }
      47             : /* }}} */
      48             : 
      49           4 : static char* php_password_get_algo_name(const php_password_algo algo)
      50             : {
      51           4 :         switch (algo) {
      52             :                 case PHP_PASSWORD_BCRYPT:
      53           2 :                         return "bcrypt";
      54             :                 case PHP_PASSWORD_UNKNOWN:
      55             :                 default:
      56           2 :                         return "unknown";
      57             :         }
      58             : }
      59             : 
      60          12 : static php_password_algo php_password_determine_algo(const char *hash, const size_t len)
      61             : {
      62          12 :         if (len > 3 && hash[0] == '$' && hash[1] == '2' && hash[2] == 'y' && len == 60) {
      63           8 :                 return PHP_PASSWORD_BCRYPT;
      64             :         }
      65             : 
      66           4 :         return PHP_PASSWORD_UNKNOWN;
      67             : }
      68             : 
      69           2 : static int php_password_salt_is_alphabet(const char *str, const size_t len) /* {{{ */
      70             : {
      71           2 :         size_t i = 0;
      72             : 
      73          48 :         for (i = 0; i < len; i++) {
      74          47 :                 if (!((str[i] >= 'A' && str[i] <= 'Z') || (str[i] >= 'a' && str[i] <= 'z') || (str[i] >= '0' && str[i] <= '9') || str[i] == '.' || str[i] == '/')) {
      75           1 :                         return FAILURE;
      76             :                 }
      77             :         }
      78           1 :         return SUCCESS;
      79             : }
      80             : /* }}} */
      81             : 
      82           3 : static int php_password_salt_to64(const char *str, const size_t str_len, const size_t out_len, char *ret) /* {{{ */
      83             : {
      84           3 :         size_t pos = 0;
      85             :         zend_string *buffer;
      86           3 :         if ((int) str_len < 0) {
      87           0 :                 return FAILURE;
      88             :         }
      89           3 :         buffer = php_base64_encode((unsigned char*) str, str_len);
      90           3 :         if (ZSTR_LEN(buffer) < out_len) {
      91             :                 /* Too short of an encoded string generated */
      92             :                 zend_string_release(buffer);
      93           0 :                 return FAILURE;
      94             :         }
      95          69 :         for (pos = 0; pos < out_len; pos++) {
      96          66 :                 if (ZSTR_VAL(buffer)[pos] == '+') {
      97           1 :                         ret[pos] = '.';
      98          65 :                 } else if (ZSTR_VAL(buffer)[pos] == '=') {
      99             :                         zend_string_free(buffer);
     100           0 :                         return FAILURE;
     101             :                 } else {
     102          65 :                         ret[pos] = ZSTR_VAL(buffer)[pos];
     103             :                 }
     104             :         }
     105             :         zend_string_free(buffer);
     106           3 :         return SUCCESS;
     107             : }
     108             : /* }}} */
     109             : 
     110           2 : static int php_password_make_salt(size_t length, char *ret) /* {{{ */
     111             : {
     112           2 :         int buffer_valid = 0;
     113             :         size_t i, raw_length;
     114             :         char *buffer;
     115             :         char *result;
     116             : 
     117           2 :         if (length > (INT_MAX / 3)) {
     118           0 :                 php_error_docref(NULL, E_WARNING, "Length is too large to safely generate");
     119           0 :                 return FAILURE;
     120             :         }
     121             : 
     122           2 :         raw_length = length * 3 / 4 + 1;
     123             : 
     124           2 :         buffer = (char *) safe_emalloc(raw_length, 1, 1);
     125             : 
     126             : #if PHP_WIN32
     127             :         {
     128             :                 BYTE *iv_b = (BYTE *) buffer;
     129             :                 if (php_win32_get_random_bytes(iv_b, raw_length) == SUCCESS) {
     130             :                         buffer_valid = 1;
     131             :                 }
     132             :         }
     133             : #else
     134             :         {
     135             :                 int fd, n;
     136           2 :                 size_t read_bytes = 0;
     137           2 :                 fd = open("/dev/urandom", O_RDONLY);
     138           2 :                 if (fd >= 0) {
     139           6 :                         while (read_bytes < raw_length) {
     140           2 :                                 n = read(fd, buffer + read_bytes, raw_length - read_bytes);
     141           2 :                                 if (n < 0) {
     142           0 :                                         break;
     143             :                                 }
     144           2 :                                 read_bytes += (size_t) n;
     145             :                         }
     146           2 :                         close(fd);
     147             :                 }
     148           2 :                 if (read_bytes >= raw_length) {
     149           2 :                         buffer_valid = 1;
     150             :                 }
     151             :         }
     152             : #endif
     153           2 :         if (!buffer_valid) {
     154           0 :                 for (i = 0; i < raw_length; i++) {
     155           0 :                         buffer[i] ^= (char) (255.0 * php_rand() / RAND_MAX);
     156             :                 }
     157             :         }
     158             : 
     159           2 :         result = safe_emalloc(length, 1, 1);
     160           2 :         if (php_password_salt_to64(buffer, raw_length, length, result) == FAILURE) {
     161           0 :                 php_error_docref(NULL, E_WARNING, "Generated salt too short");
     162           0 :                 efree(buffer);
     163           0 :                 efree(result);
     164           0 :                 return FAILURE;
     165             :         }
     166           2 :         memcpy(ret, result, length);
     167           2 :         efree(result);
     168           2 :         efree(buffer);
     169           2 :         ret[length] = 0;
     170           2 :         return SUCCESS;
     171             : }
     172             : /* }}} */
     173             : 
     174           6 : PHP_FUNCTION(password_get_info)
     175             : {
     176             :         php_password_algo algo;
     177             :         size_t hash_len;
     178             :         char *hash, *algo_name;
     179             :         zval options;
     180             : 
     181           6 :         if (zend_parse_parameters(ZEND_NUM_ARGS(), "s", &hash, &hash_len) == FAILURE) {
     182           2 :                 return;
     183             :         }
     184             : 
     185           4 :         array_init(&options);
     186             : 
     187           4 :         algo = php_password_determine_algo(hash, (size_t) hash_len);
     188           4 :         algo_name = php_password_get_algo_name(algo);
     189             : 
     190           4 :         switch (algo) {
     191             :                 case PHP_PASSWORD_BCRYPT:
     192             :                         {
     193           2 :                                 zend_long cost = PHP_PASSWORD_BCRYPT_COST;
     194           2 :                                 sscanf(hash, "$2y$" ZEND_LONG_FMT "$", &cost);
     195           2 :                                 add_assoc_long(&options, "cost", cost);
     196             :                         }
     197             :                         break;
     198             :                 case PHP_PASSWORD_UNKNOWN:
     199             :                 default:
     200             :                         break;
     201             :         }
     202             : 
     203           4 :         array_init(return_value);
     204             : 
     205           4 :         add_assoc_long(return_value, "algo", algo);
     206           4 :         add_assoc_string(return_value, "algoName", algo_name);
     207           4 :         add_assoc_zval(return_value, "options", &options);
     208             : }
     209             : 
     210          13 : PHP_FUNCTION(password_needs_rehash)
     211             : {
     212          13 :         zend_long new_algo = 0;
     213             :         php_password_algo algo;
     214             :         size_t hash_len;
     215             :         char *hash;
     216          13 :         HashTable *options = 0;
     217             :         zval *option_buffer;
     218             : 
     219          13 :         if (zend_parse_parameters(ZEND_NUM_ARGS(), "sl|H", &hash, &hash_len, &new_algo, &options) == FAILURE) {
     220           5 :                 return;
     221             :         }
     222             : 
     223           8 :         algo = php_password_determine_algo(hash, (size_t) hash_len);
     224             : 
     225           8 :         if (algo != new_algo) {
     226           1 :                 RETURN_TRUE;
     227             :         }
     228             : 
     229           7 :         switch (algo) {
     230             :                 case PHP_PASSWORD_BCRYPT:
     231             :                         {
     232           6 :                                 zend_long new_cost = PHP_PASSWORD_BCRYPT_COST, cost = 0;
     233             : 
     234           6 :                                 if (options && (option_buffer = zend_hash_str_find(options, "cost", sizeof("cost")-1)) != NULL) {
     235           5 :                                         new_cost = zval_get_long(option_buffer);
     236             :                                 }
     237             : 
     238           6 :                                 sscanf(hash, "$2y$" ZEND_LONG_FMT "$", &cost);
     239           6 :                                 if (cost != new_cost) {
     240           3 :                                         RETURN_TRUE;
     241             :                                 }
     242             :                         }
     243             :                         break;
     244             :                 case PHP_PASSWORD_UNKNOWN:
     245             :                 default:
     246             :                         break;
     247             :         }
     248           4 :         RETURN_FALSE;
     249             : }
     250             : 
     251             : /* {{{ proto boolean password_make_salt(string password, string hash)
     252             : Verify a hash created using crypt() or password_hash() */
     253           9 : PHP_FUNCTION(password_verify)
     254             : {
     255           9 :         int status = 0, i;
     256             :         size_t password_len, hash_len;
     257             :         char *password, *hash;
     258             :         zend_string *ret;
     259             : 
     260           9 :         if (zend_parse_parameters(ZEND_NUM_ARGS(), "ss", &password, &password_len, &hash, &hash_len) == FAILURE) {
     261           2 :                 RETURN_FALSE;
     262             :         }
     263           7 :         if ((ret = php_crypt(password, (int)password_len, hash, (int)hash_len, 1)) == NULL) {
     264           1 :                 RETURN_FALSE;
     265             :         }
     266             : 
     267           6 :         if (ZSTR_LEN(ret) != hash_len || hash_len < 13) {
     268             :                 zend_string_free(ret);
     269           3 :                 RETURN_FALSE;
     270             :         }
     271             : 
     272             :         /* We're using this method instead of == in order to provide
     273             :          * resistance towards timing attacks. This is a constant time
     274             :          * equality check that will always check every byte of both
     275             :          * values. */
     276         136 :         for (i = 0; i < hash_len; i++) {
     277         133 :                 status |= (ZSTR_VAL(ret)[i] ^ hash[i]);
     278             :         }
     279             : 
     280             :         zend_string_free(ret);
     281             : 
     282           3 :         RETURN_BOOL(status == 0);
     283             : 
     284             : }
     285             : /* }}} */
     286             : 
     287             : /* {{{ proto string password_hash(string password, int algo, array options = array())
     288             : Hash a password */
     289          18 : PHP_FUNCTION(password_hash)
     290             : {
     291             :         char *hash_format, *hash, *salt, *password;
     292          18 :         zend_long algo = 0;
     293          18 :         size_t password_len = 0;
     294             :         int hash_len;
     295          18 :         size_t salt_len = 0, required_salt_len = 0, hash_format_len;
     296          18 :         HashTable *options = 0;
     297             :         zval *option_buffer;
     298             :         zend_string *result;
     299             : 
     300          18 :         if (zend_parse_parameters(ZEND_NUM_ARGS(), "sl|H", &password, &password_len, &algo, &options) == FAILURE) {
     301           5 :                 return;
     302             :         }
     303             : 
     304          13 :         switch (algo) {
     305             :                 case PHP_PASSWORD_BCRYPT:
     306             :                 {
     307          12 :                         zend_long cost = PHP_PASSWORD_BCRYPT_COST;
     308             : 
     309          12 :                         if (options && (option_buffer = zend_hash_str_find(options, "cost", sizeof("cost")-1)) != NULL) {
     310           4 :                                 cost = zval_get_long(option_buffer);
     311             :                         }
     312             : 
     313          12 :                         if (cost < 4 || cost > 31) {
     314           3 :                                 php_error_docref(NULL, E_WARNING, "Invalid bcrypt cost parameter specified: " ZEND_LONG_FMT, cost);
     315           3 :                                 RETURN_NULL();
     316             :                         }
     317             : 
     318           9 :                         required_salt_len = 22;
     319           9 :                         hash_format = emalloc(8);
     320           9 :                         sprintf(hash_format, "$2y$%02ld$", (long) cost);
     321           9 :                         hash_format_len = 7;
     322             :                 }
     323           9 :                 break;
     324             :                 case PHP_PASSWORD_UNKNOWN:
     325             :                 default:
     326           1 :                         php_error_docref(NULL, E_WARNING, "Unknown password hashing algorithm: " ZEND_LONG_FMT, algo);
     327           1 :                         RETURN_NULL();
     328             :         }
     329             : 
     330          11 :         if (options && (option_buffer = zend_hash_str_find(options, "salt", sizeof("salt")-1)) != NULL) {
     331             :                 char *buffer;
     332           7 :                 size_t buffer_len = 0;
     333             : 
     334           7 :                 php_error_docref(NULL, E_DEPRECATED, "Use of the 'salt' option to password_hash is deprecated");
     335             : 
     336           7 :                 switch (Z_TYPE_P(option_buffer)) {
     337             :                         case IS_STRING:
     338           4 :                                 buffer = estrndup(Z_STRVAL_P(option_buffer), Z_STRLEN_P(option_buffer));
     339           4 :                                 buffer_len = Z_STRLEN_P(option_buffer);
     340           4 :                                 break;
     341             :                         case IS_LONG:
     342             :                         case IS_DOUBLE:
     343             :                         case IS_OBJECT:
     344             :                         {
     345           2 :                                 zend_string *tmp = zval_get_string(option_buffer);
     346           2 :                                 buffer = estrndup(ZSTR_VAL(tmp), ZSTR_LEN(tmp));
     347           2 :                                 buffer_len = ZSTR_LEN(tmp);
     348             :                                 zend_string_release(tmp);
     349           2 :                                 break;
     350             :                         }
     351             :                         case IS_FALSE:
     352             :                         case IS_TRUE:
     353             :                         case IS_NULL:
     354             :                         case IS_RESOURCE:
     355             :                         case IS_ARRAY:
     356             :                         default:
     357           1 :                                 efree(hash_format);
     358           1 :                                 php_error_docref(NULL, E_WARNING, "Non-string salt parameter supplied");
     359           1 :                                 RETURN_NULL();
     360             :                 }
     361             : 
     362             :                 /* XXX all the crypt related APIs work with int for string length.
     363             :                         That should be revised for size_t and then we maybe don't require
     364             :                         the > INT_MAX check. */
     365           6 :                 if (buffer_len > INT_MAX) {
     366           0 :                         efree(hash_format);
     367           0 :                         efree(buffer);
     368           0 :                         php_error_docref(NULL, E_WARNING, "Supplied salt is too long");
     369           0 :                         RETURN_NULL();
     370           6 :                 } else if (buffer_len < required_salt_len) {
     371           4 :                         efree(hash_format);
     372           4 :                         efree(buffer);
     373           4 :                         php_error_docref(NULL, E_WARNING, "Provided salt is too short: %zd expecting %zd", buffer_len, required_salt_len);
     374           4 :                         RETURN_NULL();
     375           2 :                 } else if (php_password_salt_is_alphabet(buffer, buffer_len) == FAILURE) {
     376           1 :                         salt = safe_emalloc(required_salt_len, 1, 1);
     377           1 :                         if (php_password_salt_to64(buffer, buffer_len, required_salt_len, salt) == FAILURE) {
     378           0 :                                 efree(hash_format);
     379           0 :                                 efree(buffer);
     380           0 :                                 efree(salt);
     381           0 :                                 php_error_docref(NULL, E_WARNING, "Provided salt is too short: %zd", buffer_len);
     382           0 :                                 RETURN_NULL();
     383             :                         }
     384           1 :                         salt_len = required_salt_len;
     385             :                 } else {
     386           1 :                         salt = safe_emalloc(required_salt_len, 1, 1);
     387           1 :                         memcpy(salt, buffer, required_salt_len);
     388           1 :                         salt_len = required_salt_len;
     389             :                 }
     390           2 :                 efree(buffer);
     391             :         } else {
     392           2 :                 salt = safe_emalloc(required_salt_len, 1, 1);
     393           2 :                 if (php_password_make_salt(required_salt_len, salt) == FAILURE) {
     394           0 :                         efree(hash_format);
     395           0 :                         efree(salt);
     396           0 :                         RETURN_FALSE;
     397             :                 }
     398           2 :                 salt_len = required_salt_len;
     399             :         }
     400             : 
     401           4 :         salt[salt_len] = 0;
     402             : 
     403           4 :         hash = safe_emalloc(salt_len + hash_format_len, 1, 1);
     404           4 :         sprintf(hash, "%s%s", hash_format, salt);
     405           4 :         hash[hash_format_len + salt_len] = 0;
     406             : 
     407           4 :         efree(hash_format);
     408           4 :         efree(salt);
     409             : 
     410             :         /* This cast is safe, since both values are defined here in code and cannot overflow */
     411           4 :         hash_len = (int) (hash_format_len + salt_len);
     412             : 
     413           4 :         if ((result = php_crypt(password, (int)password_len, hash, hash_len, 1)) == NULL) {
     414           0 :                 efree(hash);
     415           0 :                 RETURN_FALSE;
     416             :         }
     417             : 
     418           4 :         efree(hash);
     419             : 
     420           4 :         if (ZSTR_LEN(result) < 13) {
     421             :                 zend_string_free(result);
     422           0 :                 RETURN_FALSE;
     423             :         }
     424             : 
     425           4 :         RETURN_STR(result);
     426             : }
     427             : /* }}} */
     428             : 
     429             : #endif /* HAVE_CRYPT */
     430             : /*
     431             :  * Local variables:
     432             :  * tab-width: 4
     433             :  * c-basic-offset: 4
     434             :  * End:
     435             :  * vim600: sw=4 ts=4 fdm=marker
     436             :  * vim<600: sw=4 ts=4
     437             :  */

Generated by: LCOV version 1.10

Generated at Sat, 29 Aug 2015 10:22:24 +0000 (2 days ago)

Copyright © 2005-2015 The PHP Group
All rights reserved.