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: 135 158 85.4 %
Date: 2016-05-26 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-2016 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             : #include "php_random.h"
      34             : 
      35             : #if PHP_WIN32
      36             : #include "win32/winutil.h"
      37             : #endif
      38             : 
      39       22587 : PHP_MINIT_FUNCTION(password) /* {{{ */
      40             : {
      41       22587 :         REGISTER_LONG_CONSTANT("PASSWORD_DEFAULT", PHP_PASSWORD_DEFAULT, CONST_CS | CONST_PERSISTENT);
      42       22587 :         REGISTER_LONG_CONSTANT("PASSWORD_BCRYPT", PHP_PASSWORD_BCRYPT, CONST_CS | CONST_PERSISTENT);
      43             : 
      44       22587 :         REGISTER_LONG_CONSTANT("PASSWORD_BCRYPT_DEFAULT_COST", PHP_PASSWORD_BCRYPT_COST, CONST_CS | CONST_PERSISTENT);
      45             : 
      46       22587 :         return SUCCESS;
      47             : }
      48             : /* }}} */
      49             : 
      50           4 : static char* php_password_get_algo_name(const php_password_algo algo)
      51             : {
      52           4 :         switch (algo) {
      53             :                 case PHP_PASSWORD_BCRYPT:
      54           2 :                         return "bcrypt";
      55             :                 case PHP_PASSWORD_UNKNOWN:
      56             :                 default:
      57           2 :                         return "unknown";
      58             :         }
      59             : }
      60             : 
      61          12 : static php_password_algo php_password_determine_algo(const char *hash, const size_t len)
      62             : {
      63          12 :         if (len > 3 && hash[0] == '$' && hash[1] == '2' && hash[2] == 'y' && len == 60) {
      64           8 :                 return PHP_PASSWORD_BCRYPT;
      65             :         }
      66             : 
      67           4 :         return PHP_PASSWORD_UNKNOWN;
      68             : }
      69             : 
      70           2 : static int php_password_salt_is_alphabet(const char *str, const size_t len) /* {{{ */
      71             : {
      72           2 :         size_t i = 0;
      73             : 
      74          48 :         for (i = 0; i < len; i++) {
      75          47 :                 if (!((str[i] >= 'A' && str[i] <= 'Z') || (str[i] >= 'a' && str[i] <= 'z') || (str[i] >= '0' && str[i] <= '9') || str[i] == '.' || str[i] == '/')) {
      76           1 :                         return FAILURE;
      77             :                 }
      78             :         }
      79           1 :         return SUCCESS;
      80             : }
      81             : /* }}} */
      82             : 
      83           3 : static int php_password_salt_to64(const char *str, const size_t str_len, const size_t out_len, char *ret) /* {{{ */
      84             : {
      85           3 :         size_t pos = 0;
      86             :         zend_string *buffer;
      87           3 :         if ((int) str_len < 0) {
      88           0 :                 return FAILURE;
      89             :         }
      90           3 :         buffer = php_base64_encode((unsigned char*) str, str_len);
      91           3 :         if (ZSTR_LEN(buffer) < out_len) {
      92             :                 /* Too short of an encoded string generated */
      93             :                 zend_string_release(buffer);
      94           0 :                 return FAILURE;
      95             :         }
      96          69 :         for (pos = 0; pos < out_len; pos++) {
      97          66 :                 if (ZSTR_VAL(buffer)[pos] == '+') {
      98           0 :                         ret[pos] = '.';
      99          66 :                 } else if (ZSTR_VAL(buffer)[pos] == '=') {
     100             :                         zend_string_free(buffer);
     101           0 :                         return FAILURE;
     102             :                 } else {
     103          66 :                         ret[pos] = ZSTR_VAL(buffer)[pos];
     104             :                 }
     105             :         }
     106             :         zend_string_free(buffer);
     107           3 :         return SUCCESS;
     108             : }
     109             : /* }}} */
     110             : 
     111           2 : static int php_password_make_salt(size_t length, char *ret) /* {{{ */
     112             : {
     113             :         size_t 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           2 :         if (FAILURE == php_random_bytes_silent(buffer, raw_length)) {
     127           0 :                 php_error_docref(NULL, E_WARNING, "Unable to generate salt");
     128           0 :                 efree(buffer);
     129           0 :                 return FAILURE;
     130             :         }
     131             : 
     132           2 :         result = safe_emalloc(length, 1, 1);
     133           2 :         if (php_password_salt_to64(buffer, raw_length, length, result) == FAILURE) {
     134           0 :                 php_error_docref(NULL, E_WARNING, "Generated salt too short");
     135           0 :                 efree(buffer);
     136           0 :                 efree(result);
     137           0 :                 return FAILURE;
     138             :         }
     139           2 :         memcpy(ret, result, length);
     140           2 :         efree(result);
     141           2 :         efree(buffer);
     142           2 :         ret[length] = 0;
     143           2 :         return SUCCESS;
     144             : }
     145             : /* }}} */
     146             : 
     147           6 : PHP_FUNCTION(password_get_info)
     148             : {
     149             :         php_password_algo algo;
     150             :         size_t hash_len;
     151             :         char *hash, *algo_name;
     152             :         zval options;
     153             : 
     154           6 :         if (zend_parse_parameters(ZEND_NUM_ARGS(), "s", &hash, &hash_len) == FAILURE) {
     155           2 :                 return;
     156             :         }
     157             : 
     158           4 :         array_init(&options);
     159             : 
     160           4 :         algo = php_password_determine_algo(hash, (size_t) hash_len);
     161           4 :         algo_name = php_password_get_algo_name(algo);
     162             : 
     163           4 :         switch (algo) {
     164             :                 case PHP_PASSWORD_BCRYPT:
     165             :                         {
     166           2 :                                 zend_long cost = PHP_PASSWORD_BCRYPT_COST;
     167           2 :                                 sscanf(hash, "$2y$" ZEND_LONG_FMT "$", &cost);
     168           2 :                                 add_assoc_long(&options, "cost", cost);
     169             :                         }
     170             :                         break;
     171             :                 case PHP_PASSWORD_UNKNOWN:
     172             :                 default:
     173             :                         break;
     174             :         }
     175             : 
     176           4 :         array_init(return_value);
     177             : 
     178           4 :         add_assoc_long(return_value, "algo", algo);
     179           4 :         add_assoc_string(return_value, "algoName", algo_name);
     180           4 :         add_assoc_zval(return_value, "options", &options);
     181             : }
     182             : 
     183          13 : PHP_FUNCTION(password_needs_rehash)
     184             : {
     185          13 :         zend_long new_algo = 0;
     186             :         php_password_algo algo;
     187             :         size_t hash_len;
     188             :         char *hash;
     189          13 :         HashTable *options = 0;
     190             :         zval *option_buffer;
     191             : 
     192          13 :         if (zend_parse_parameters(ZEND_NUM_ARGS(), "sl|H", &hash, &hash_len, &new_algo, &options) == FAILURE) {
     193           5 :                 return;
     194             :         }
     195             : 
     196           8 :         algo = php_password_determine_algo(hash, (size_t) hash_len);
     197             : 
     198           8 :         if (algo != new_algo) {
     199           1 :                 RETURN_TRUE;
     200             :         }
     201             : 
     202           7 :         switch (algo) {
     203             :                 case PHP_PASSWORD_BCRYPT:
     204             :                         {
     205           6 :                                 zend_long new_cost = PHP_PASSWORD_BCRYPT_COST, cost = 0;
     206             : 
     207           6 :                                 if (options && (option_buffer = zend_hash_str_find(options, "cost", sizeof("cost")-1)) != NULL) {
     208           5 :                                         new_cost = zval_get_long(option_buffer);
     209             :                                 }
     210             : 
     211           6 :                                 sscanf(hash, "$2y$" ZEND_LONG_FMT "$", &cost);
     212           6 :                                 if (cost != new_cost) {
     213           3 :                                         RETURN_TRUE;
     214             :                                 }
     215             :                         }
     216             :                         break;
     217             :                 case PHP_PASSWORD_UNKNOWN:
     218             :                 default:
     219             :                         break;
     220             :         }
     221           4 :         RETURN_FALSE;
     222             : }
     223             : 
     224             : /* {{{ proto boolean password_make_salt(string password, string hash)
     225             : Verify a hash created using crypt() or password_hash() */
     226           9 : PHP_FUNCTION(password_verify)
     227             : {
     228           9 :         int status = 0, i;
     229             :         size_t password_len, hash_len;
     230             :         char *password, *hash;
     231             :         zend_string *ret;
     232             : 
     233           9 :         if (zend_parse_parameters(ZEND_NUM_ARGS(), "ss", &password, &password_len, &hash, &hash_len) == FAILURE) {
     234           2 :                 RETURN_FALSE;
     235             :         }
     236           7 :         if ((ret = php_crypt(password, (int)password_len, hash, (int)hash_len, 1)) == NULL) {
     237           1 :                 RETURN_FALSE;
     238             :         }
     239             : 
     240           6 :         if (ZSTR_LEN(ret) != hash_len || hash_len < 13) {
     241             :                 zend_string_free(ret);
     242           3 :                 RETURN_FALSE;
     243             :         }
     244             : 
     245             :         /* We're using this method instead of == in order to provide
     246             :          * resistance towards timing attacks. This is a constant time
     247             :          * equality check that will always check every byte of both
     248             :          * values. */
     249         136 :         for (i = 0; i < hash_len; i++) {
     250         133 :                 status |= (ZSTR_VAL(ret)[i] ^ hash[i]);
     251             :         }
     252             : 
     253             :         zend_string_free(ret);
     254             : 
     255           3 :         RETURN_BOOL(status == 0);
     256             : 
     257             : }
     258             : /* }}} */
     259             : 
     260             : /* {{{ proto string password_hash(string password, int algo, array options = array())
     261             : Hash a password */
     262          18 : PHP_FUNCTION(password_hash)
     263             : {
     264             :         char hash_format[8], *hash, *salt, *password;
     265          18 :         zend_long algo = 0;
     266          18 :         size_t password_len = 0;
     267             :         int hash_len;
     268          18 :         size_t salt_len = 0, required_salt_len = 0, hash_format_len;
     269          18 :         HashTable *options = 0;
     270             :         zval *option_buffer;
     271             :         zend_string *result;
     272             : 
     273          18 :         if (zend_parse_parameters(ZEND_NUM_ARGS(), "sl|H", &password, &password_len, &algo, &options) == FAILURE) {
     274           5 :                 return;
     275             :         }
     276             : 
     277          13 :         switch (algo) {
     278             :                 case PHP_PASSWORD_BCRYPT:
     279             :                 {
     280          12 :                         zend_long cost = PHP_PASSWORD_BCRYPT_COST;
     281             : 
     282          12 :                         if (options && (option_buffer = zend_hash_str_find(options, "cost", sizeof("cost")-1)) != NULL) {
     283           4 :                                 cost = zval_get_long(option_buffer);
     284             :                         }
     285             : 
     286          12 :                         if (cost < 4 || cost > 31) {
     287           3 :                                 php_error_docref(NULL, E_WARNING, "Invalid bcrypt cost parameter specified: " ZEND_LONG_FMT, cost);
     288           3 :                                 RETURN_NULL();
     289             :                         }
     290             : 
     291           9 :                         required_salt_len = 22;
     292           9 :                         sprintf(hash_format, "$2y$%02ld$", (long) cost);
     293           9 :                         hash_format_len = 7;
     294             :                 }
     295           9 :                 break;
     296             :                 case PHP_PASSWORD_UNKNOWN:
     297             :                 default:
     298           1 :                         php_error_docref(NULL, E_WARNING, "Unknown password hashing algorithm: " ZEND_LONG_FMT, algo);
     299           1 :                         RETURN_NULL();
     300             :         }
     301             : 
     302          11 :         if (options && (option_buffer = zend_hash_str_find(options, "salt", sizeof("salt")-1)) != NULL) {
     303             :                 zend_string *buffer;
     304             : 
     305           7 :                 php_error_docref(NULL, E_DEPRECATED, "Use of the 'salt' option to password_hash is deprecated");
     306             : 
     307           7 :                 switch (Z_TYPE_P(option_buffer)) {
     308             :                         case IS_STRING:
     309           8 :                                 buffer = zend_string_copy(Z_STR_P(option_buffer));
     310           4 :                                 break;
     311             :                         case IS_LONG:
     312             :                         case IS_DOUBLE:
     313             :                         case IS_OBJECT:
     314           2 :                                 buffer = zval_get_string(option_buffer);
     315           2 :                                 break;
     316             :                         case IS_FALSE:
     317             :                         case IS_TRUE:
     318             :                         case IS_NULL:
     319             :                         case IS_RESOURCE:
     320             :                         case IS_ARRAY:
     321             :                         default:
     322           1 :                                 php_error_docref(NULL, E_WARNING, "Non-string salt parameter supplied");
     323           1 :                                 RETURN_NULL();
     324             :                 }
     325             : 
     326             :                 /* XXX all the crypt related APIs work with int for string length.
     327             :                         That should be revised for size_t and then we maybe don't require
     328             :                         the > INT_MAX check. */
     329           6 :                 if (ZSTR_LEN(buffer) > INT_MAX) {
     330           0 :                         php_error_docref(NULL, E_WARNING, "Supplied salt is too long");
     331           0 :                         RETURN_NULL();
     332           6 :                 } else if (ZSTR_LEN(buffer) < required_salt_len) {
     333           4 :                         php_error_docref(NULL, E_WARNING, "Provided salt is too short: %zd expecting %zd", ZSTR_LEN(buffer), required_salt_len);
     334             :                         zend_string_release(buffer);
     335           4 :                         RETURN_NULL();
     336           2 :                 } else if (php_password_salt_is_alphabet(ZSTR_VAL(buffer), ZSTR_LEN(buffer)) == FAILURE) {
     337           1 :                         salt = safe_emalloc(required_salt_len, 1, 1);
     338           1 :                         if (php_password_salt_to64(ZSTR_VAL(buffer), ZSTR_LEN(buffer), required_salt_len, salt) == FAILURE) {
     339           0 :                                 efree(salt);
     340           0 :                                 php_error_docref(NULL, E_WARNING, "Provided salt is too short: %zd", ZSTR_LEN(buffer));
     341             :                                 zend_string_release(buffer);
     342           0 :                                 RETURN_NULL();
     343             :                         }
     344           1 :                         salt_len = required_salt_len;
     345             :                 } else {
     346           1 :                         salt = safe_emalloc(required_salt_len, 1, 1);
     347           1 :                         memcpy(salt, ZSTR_VAL(buffer), required_salt_len);
     348           1 :                         salt_len = required_salt_len;
     349             :                 }
     350             :                 zend_string_release(buffer);
     351             :         } else {
     352           2 :                 salt = safe_emalloc(required_salt_len, 1, 1);
     353           2 :                 if (php_password_make_salt(required_salt_len, salt) == FAILURE) {
     354           0 :                         efree(salt);
     355           0 :                         RETURN_FALSE;
     356             :                 }
     357           2 :                 salt_len = required_salt_len;
     358             :         }
     359             : 
     360           4 :         salt[salt_len] = 0;
     361             : 
     362           4 :         hash = safe_emalloc(salt_len + hash_format_len, 1, 1);
     363           4 :         sprintf(hash, "%s%s", hash_format, salt);
     364           4 :         hash[hash_format_len + salt_len] = 0;
     365             : 
     366           4 :         efree(salt);
     367             : 
     368             :         /* This cast is safe, since both values are defined here in code and cannot overflow */
     369           4 :         hash_len = (int) (hash_format_len + salt_len);
     370             : 
     371           4 :         if ((result = php_crypt(password, (int)password_len, hash, hash_len, 1)) == NULL) {
     372           0 :                 efree(hash);
     373           0 :                 RETURN_FALSE;
     374             :         }
     375             : 
     376           4 :         efree(hash);
     377             : 
     378           4 :         if (ZSTR_LEN(result) < 13) {
     379             :                 zend_string_free(result);
     380           0 :                 RETURN_FALSE;
     381             :         }
     382             : 
     383           4 :         RETURN_STR(result);
     384             : }
     385             : /* }}} */
     386             : 
     387             : #endif /* HAVE_CRYPT */
     388             : /*
     389             :  * Local variables:
     390             :  * tab-width: 4
     391             :  * c-basic-offset: 4
     392             :  * End:
     393             :  * vim600: sw=4 ts=4 fdm=marker
     394             :  * vim<600: sw=4 ts=4
     395             :  */

Generated by: LCOV version 1.10

Generated at Thu, 26 May 2016 12:48:07 +0000 (2 days ago)

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