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: 164 193 85.0 %
Date: 2014-11-22 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-2014 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       20507 : PHP_MINIT_FUNCTION(password) /* {{{ */
      39             : {
      40       20507 :         REGISTER_LONG_CONSTANT("PASSWORD_DEFAULT", PHP_PASSWORD_DEFAULT, CONST_CS | CONST_PERSISTENT);
      41       20507 :         REGISTER_LONG_CONSTANT("PASSWORD_BCRYPT", PHP_PASSWORD_BCRYPT, CONST_CS | CONST_PERSISTENT);
      42             : 
      43       20507 :         REGISTER_LONG_CONSTANT("PASSWORD_BCRYPT_DEFAULT_COST", PHP_PASSWORD_BCRYPT_COST, CONST_CS | CONST_PERSISTENT);
      44             : 
      45       20507 :         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 (buffer->len < 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 (buffer->val[pos] == '+') {
      97           0 :                         ret[pos] = '.';
      98          66 :                 } else if (buffer->val[pos] == '=') {
      99             :                         zend_string_free(buffer);
     100           0 :                         return FAILURE;
     101             :                 } else {
     102          66 :                         ret[pos] = buffer->val[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 TSRMLS_DC) /* {{{ */
     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 TSRMLS_CC, 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(TSRMLS_C) / 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 TSRMLS_CC, 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() TSRMLS_CC, "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() TSRMLS_CC, "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          11 :                                 if (options && (option_buffer = zend_symtable_str_find(options, "cost", sizeof("cost")-1)) != NULL) {
     235           5 :                                         if (Z_TYPE_P(option_buffer) != IS_LONG) {
     236             :                                                 zval cast_option_buffer;
     237           1 :                                                 ZVAL_DUP(&cast_option_buffer, option_buffer);
     238           1 :                                                 convert_to_long(&cast_option_buffer);
     239           1 :                                                 new_cost = Z_LVAL(cast_option_buffer);
     240             :                                                 zval_dtor(&cast_option_buffer);
     241             :                                         } else {
     242           4 :                                                 new_cost = Z_LVAL_P(option_buffer);
     243             :                                         }
     244             :                                 }
     245             : 
     246           6 :                                 sscanf(hash, "$2y$" ZEND_LONG_FMT "$", &cost);
     247           6 :                                 if (cost != new_cost) {
     248           3 :                                         RETURN_TRUE;
     249             :                                 }
     250             :                         }
     251             :                         break;
     252             :                 case PHP_PASSWORD_UNKNOWN:
     253             :                 default:
     254             :                         break;
     255             :         }
     256           4 :         RETURN_FALSE;
     257             : }
     258             : 
     259             : /* {{{ proto boolean password_make_salt(string password, string hash)
     260             : Verify a hash created using crypt() or password_hash() */
     261           6 : PHP_FUNCTION(password_verify)
     262             : {
     263           6 :         int status = 0, i;
     264             :         size_t password_len, hash_len;
     265             :         char *password, *hash;
     266             :         zend_string *ret;
     267             :         
     268           6 :         if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "ss", &password, &password_len, &hash, &hash_len) == FAILURE) {
     269           2 :                 RETURN_FALSE;
     270             :         }
     271           4 :         if ((ret = php_crypt(password, (int)password_len, hash, (int)hash_len)) == NULL) {
     272           0 :                 RETURN_FALSE;
     273             :         }
     274             : 
     275           4 :         if (ret->len != hash_len || hash_len < 13) {
     276             :                 zend_string_free(ret);
     277           2 :                 RETURN_FALSE;
     278             :         }
     279             :         
     280             :         /* We're using this method instead of == in order to provide
     281             :          * resistance towards timing attacks. This is a constant time
     282             :          * equality check that will always check every byte of both
     283             :          * values. */
     284         122 :         for (i = 0; i < hash_len; i++) {
     285         120 :                 status |= (ret->val[i] ^ hash[i]);
     286             :         }
     287             : 
     288             :         zend_string_free(ret);
     289             : 
     290           2 :         RETURN_BOOL(status == 0);
     291             :         
     292             : }
     293             : /* }}} */
     294             : 
     295             : /* {{{ proto string password_hash(string password, int algo, array options = array())
     296             : Hash a password */
     297          18 : PHP_FUNCTION(password_hash)
     298             : {
     299             :         char *hash_format, *hash, *salt, *password;
     300          18 :         zend_long algo = 0;
     301          18 :         size_t password_len = 0;
     302             :         int hash_len;
     303          18 :         size_t salt_len = 0, required_salt_len = 0, hash_format_len;
     304          18 :         HashTable *options = 0;
     305             :         zval *option_buffer;
     306             :         zend_string *result;
     307             : 
     308          18 :         if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "sl|H", &password, &password_len, &algo, &options) == FAILURE) {
     309           5 :                 return;
     310             :         }
     311             : 
     312          13 :         switch (algo) {
     313             :                 case PHP_PASSWORD_BCRYPT:
     314             :                 {
     315          12 :                         zend_long cost = PHP_PASSWORD_BCRYPT_COST;
     316             :         
     317          22 :                         if (options && (option_buffer = zend_symtable_str_find(options, "cost", sizeof("cost")-1)) != NULL) {
     318           4 :                                 if (Z_TYPE_P(option_buffer) != IS_LONG) {
     319             :                                         zval cast_option_buffer;
     320           1 :                                         ZVAL_DUP(&cast_option_buffer, option_buffer);
     321           1 :                                         convert_to_long(&cast_option_buffer);
     322           1 :                                         cost = Z_LVAL(cast_option_buffer);
     323             :                                         zval_dtor(&cast_option_buffer);
     324             :                                 } else {
     325           3 :                                         cost = Z_LVAL_P(option_buffer);
     326             :                                 }
     327             :                         }
     328             :         
     329          12 :                         if (cost < 4 || cost > 31) {
     330           3 :                                 php_error_docref(NULL TSRMLS_CC, E_WARNING, "Invalid bcrypt cost parameter specified: " ZEND_LONG_FMT, cost);
     331           3 :                                 RETURN_NULL();
     332             :                         }
     333             :                         
     334           9 :                         required_salt_len = 22;
     335           9 :                         hash_format = emalloc(8);
     336           9 :                         sprintf(hash_format, "$2y$%02ld$", (long) cost);
     337           9 :                         hash_format_len = 7;
     338             :                 }
     339           9 :                 break;
     340             :                 case PHP_PASSWORD_UNKNOWN:
     341             :                 default:
     342           1 :                         php_error_docref(NULL TSRMLS_CC, E_WARNING, "Unknown password hashing algorithm: " ZEND_LONG_FMT, algo);
     343           1 :                         RETURN_NULL();
     344             :         }
     345             : 
     346          18 :         if (options && (option_buffer = zend_symtable_str_find(options, "salt", sizeof("salt")-1)) != NULL) {
     347             :                 char *buffer;
     348           7 :                 size_t buffer_len = 0;
     349           7 :                 switch (Z_TYPE_P(option_buffer)) {
     350             :                         case IS_STRING:
     351           4 :                                 buffer = estrndup(Z_STRVAL_P(option_buffer), Z_STRLEN_P(option_buffer));
     352           4 :                                 buffer_len = Z_STRLEN_P(option_buffer);
     353           4 :                                 break;
     354             :                         case IS_LONG:
     355             :                         case IS_DOUBLE:
     356             :                         case IS_OBJECT: {
     357             :                                 zval cast_option_buffer;
     358             : 
     359           2 :                                 ZVAL_DUP(&cast_option_buffer, option_buffer);
     360           2 :                                 convert_to_string(&cast_option_buffer);
     361           2 :                                 if (Z_TYPE(cast_option_buffer) == IS_STRING) {
     362           2 :                                         buffer = estrndup(Z_STRVAL(cast_option_buffer), Z_STRLEN(cast_option_buffer));
     363           2 :                                         buffer_len = Z_STRLEN(cast_option_buffer);
     364             :                                         zval_dtor(&cast_option_buffer);
     365           2 :                                         break;
     366             :                                 }
     367             :                                 zval_dtor(&cast_option_buffer);
     368             :                         }
     369             :                         case IS_FALSE:
     370             :                         case IS_TRUE:
     371             :                         case IS_NULL:
     372             :                         case IS_RESOURCE:
     373             :                         case IS_ARRAY:
     374             :                         default:
     375           1 :                                 efree(hash_format);
     376           1 :                                 php_error_docref(NULL TSRMLS_CC, E_WARNING, "Non-string salt parameter supplied");
     377           1 :                                 RETURN_NULL();
     378             :                 }
     379             : 
     380             :                 /* XXX all the crypt related APIs work with int for string length.
     381             :                         That should be revised for size_t and then we maybe don't require
     382             :                         the > INT_MAX check. */
     383           6 :                 if (buffer_len > INT_MAX) {
     384           0 :                         efree(hash_format);
     385           0 :                         efree(buffer);
     386           0 :                         php_error_docref(NULL TSRMLS_CC, E_WARNING, "Supplied salt is too long");
     387           0 :                         RETURN_NULL();
     388           6 :                 } else if (buffer_len < required_salt_len) {
     389           4 :                         efree(hash_format);
     390           4 :                         efree(buffer);
     391           4 :                         php_error_docref(NULL TSRMLS_CC, E_WARNING, "Provided salt is too short: %zd expecting %zd", buffer_len, required_salt_len);
     392           4 :                         RETURN_NULL();
     393           2 :                 } else if (php_password_salt_is_alphabet(buffer, buffer_len) == FAILURE) {
     394           1 :                         salt = safe_emalloc(required_salt_len, 1, 1);
     395           1 :                         if (php_password_salt_to64(buffer, buffer_len, required_salt_len, salt) == FAILURE) {
     396           0 :                                 efree(hash_format);
     397           0 :                                 efree(buffer);
     398           0 :                                 efree(salt);
     399           0 :                                 php_error_docref(NULL TSRMLS_CC, E_WARNING, "Provided salt is too short: %zd", buffer_len);
     400           0 :                                 RETURN_NULL();
     401             :                         }
     402           1 :                         salt_len = required_salt_len;
     403             :                 } else {
     404           1 :                         salt = safe_emalloc(required_salt_len, 1, 1);
     405           1 :                         memcpy(salt, buffer, required_salt_len);
     406           1 :                         salt_len = required_salt_len;
     407             :                 }
     408           2 :                 efree(buffer);
     409             :         } else {
     410           2 :                 salt = safe_emalloc(required_salt_len, 1, 1);
     411           2 :                 if (php_password_make_salt(required_salt_len, salt TSRMLS_CC) == FAILURE) {
     412           0 :                         efree(hash_format);
     413           0 :                         efree(salt);
     414           0 :                         RETURN_FALSE;
     415             :                 }
     416           2 :                 salt_len = required_salt_len;
     417             :         }
     418             :         
     419           4 :         salt[salt_len] = 0;
     420             : 
     421           4 :         hash = safe_emalloc(salt_len + hash_format_len, 1, 1);
     422           4 :         sprintf(hash, "%s%s", hash_format, salt);
     423           4 :         hash[hash_format_len + salt_len] = 0;
     424             : 
     425           4 :         efree(hash_format);
     426           4 :         efree(salt);
     427             : 
     428             :         /* This cast is safe, since both values are defined here in code and cannot overflow */
     429           4 :         hash_len = (int) (hash_format_len + salt_len);
     430             : 
     431           4 :         if ((result = php_crypt(password, (int)password_len, hash, hash_len)) == NULL) {
     432           0 :                 efree(hash);
     433           0 :                 RETURN_FALSE;
     434             :         }
     435             : 
     436           4 :         efree(hash);
     437             : 
     438           4 :         if (result->len < 13) {
     439             :                 zend_string_free(result);
     440           0 :                 RETURN_FALSE;
     441             :         }
     442             : 
     443           4 :         RETURN_STR(result);
     444             : }
     445             : /* }}} */
     446             : 
     447             : #endif /* HAVE_CRYPT */
     448             : /*
     449             :  * Local variables:
     450             :  * tab-width: 4
     451             :  * c-basic-offset: 4
     452             :  * End:
     453             :  * vim600: sw=4 ts=4 fdm=marker
     454             :  * vim<600: sw=4 ts=4
     455             :  */

Generated by: LCOV version 1.10

Generated at Sat, 22 Nov 2014 23:01:25 +0000 (3 days ago)

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