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 - crypt.c (source / functions) Hit Total Coverage
Test: PHP Code Coverage Lines: 86 93 92.5 %
Date: 2016-07-19 Functions: 5 5 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: Stig Bakken <ssb@php.net>                                   |
      16             :    |          Zeev Suraski <zeev@zend.com>                                |
      17             :    |          Rasmus Lerdorf <rasmus@php.net>                             |
      18             :    |          Pierre Joye <pierre@php.net>                                |
      19             :    +----------------------------------------------------------------------+
      20             : */
      21             : 
      22             : /* $Id$ */
      23             : 
      24             : #include <stdlib.h>
      25             : 
      26             : #include "php.h"
      27             : 
      28             : #if HAVE_UNISTD_H
      29             : #include <unistd.h>
      30             : #endif
      31             : #if PHP_USE_PHP_CRYPT_R
      32             : # include "php_crypt_r.h"
      33             : # include "crypt_freesec.h"
      34             : #else
      35             : # if HAVE_CRYPT_H
      36             : #  if defined(CRYPT_R_GNU_SOURCE) && !defined(_GNU_SOURCE)
      37             : #   define _GNU_SOURCE
      38             : #  endif
      39             : #  include <crypt.h>
      40             : # endif
      41             : #endif
      42             : #if TM_IN_SYS_TIME
      43             : #include <sys/time.h>
      44             : #else
      45             : #include <time.h>
      46             : #endif
      47             : #if HAVE_STRING_H
      48             : #include <string.h>
      49             : #else
      50             : #include <strings.h>
      51             : #endif
      52             : 
      53             : #ifdef PHP_WIN32
      54             : #include <process.h>
      55             : #endif
      56             : 
      57             : #include "php_crypt.h"
      58             : #include "php_random.h"
      59             : 
      60             : /* sha512 crypt has the maximal salt length of 123 characters */
      61             : #define PHP_MAX_SALT_LEN 123
      62             : 
      63             : /* Used to check DES salts to ensure that they contain only valid characters */
      64             : #define IS_VALID_SALT_CHARACTER(c) (((c) >= '.' && (c) <= '9') || ((c) >= 'A' && (c) <= 'Z') || ((c) >= 'a' && (c) <= 'z'))
      65             : 
      66             : #define DES_INVALID_SALT_ERROR "Supplied salt is not valid for DES. Possible bug in provided salt format."
      67             : 
      68             : 
      69       23409 : PHP_MINIT_FUNCTION(crypt) /* {{{ */
      70             : {
      71       23409 :         REGISTER_LONG_CONSTANT("CRYPT_SALT_LENGTH", PHP_MAX_SALT_LEN, CONST_CS | CONST_PERSISTENT);
      72       23409 :         REGISTER_LONG_CONSTANT("CRYPT_STD_DES", 1, CONST_CS | CONST_PERSISTENT);
      73       23409 :         REGISTER_LONG_CONSTANT("CRYPT_EXT_DES", 1, CONST_CS | CONST_PERSISTENT);
      74       23409 :         REGISTER_LONG_CONSTANT("CRYPT_MD5", 1, CONST_CS | CONST_PERSISTENT);
      75       23409 :         REGISTER_LONG_CONSTANT("CRYPT_BLOWFISH", 1, CONST_CS | CONST_PERSISTENT);
      76       23409 :         REGISTER_LONG_CONSTANT("CRYPT_SHA256", 1, CONST_CS | CONST_PERSISTENT);
      77       23409 :         REGISTER_LONG_CONSTANT("CRYPT_SHA512", 1, CONST_CS | CONST_PERSISTENT);
      78             : 
      79             : #if PHP_USE_PHP_CRYPT_R
      80       23409 :         php_init_crypt_r();
      81             : #endif
      82             : 
      83       23409 :         return SUCCESS;
      84             : }
      85             : /* }}} */
      86             : 
      87       23445 : PHP_MSHUTDOWN_FUNCTION(crypt) /* {{{ */
      88             : {
      89             : #if PHP_USE_PHP_CRYPT_R
      90       23445 :         php_shutdown_crypt_r();
      91             : #endif
      92             : 
      93       23445 :         return SUCCESS;
      94             : }
      95             : /* }}} */
      96             : 
      97             : static unsigned char itoa64[] = "./0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
      98             : 
      99           1 : static void php_to64(char *s, int n) /* {{{ */
     100             : {
     101          10 :         while (--n >= 0) {
     102           8 :                 *s++ = itoa64[*s&0x3f];
     103             :         }
     104           1 : }
     105             : /* }}} */
     106             : 
     107         107 : PHPAPI zend_string *php_crypt(const char *password, const int pass_len, const char *salt, int salt_len, zend_bool quiet)
     108             : {
     109             :         char *crypt_res;
     110             :         zend_string *result;
     111             : /* Windows (win32/crypt) has a stripped down version of libxcrypt and
     112             :         a CryptoApi md5_crypt implementation */
     113             : #if PHP_USE_PHP_CRYPT_R
     114             :         {
     115             :                 struct php_crypt_extended_data buffer;
     116             : 
     117         107 :                 if (salt[0]=='$' && salt[1]=='1' && salt[2]=='$') {
     118             :                         char output[MD5_HASH_MAX_LEN], *out;
     119             : 
     120          10 :                         out = php_md5_crypt_r(password, salt, output);
     121          10 :                         if (out) {
     122          20 :                                 return zend_string_init(out, strlen(out), 0);
     123             :                         }
     124           0 :                         return NULL;
     125          97 :                 } else if (salt[0]=='$' && salt[1]=='6' && salt[2]=='$') {
     126             :                         char *output;
     127           9 :                         output = emalloc(PHP_MAX_SALT_LEN);
     128             : 
     129           9 :                         crypt_res = php_sha512_crypt_r(password, salt, output, PHP_MAX_SALT_LEN);
     130           9 :                         if (!crypt_res) {
     131           0 :                                 memset(output, 0, PHP_MAX_SALT_LEN);
     132           0 :                                 efree(output);
     133           0 :                                 return NULL;
     134             :                         } else {
     135          18 :                                 result = zend_string_init(output, strlen(output), 0);
     136           9 :                                 memset(output, 0, PHP_MAX_SALT_LEN);
     137           9 :                                 efree(output);
     138           9 :                                 return result;
     139             :                         }
     140          88 :                 } else if (salt[0]=='$' && salt[1]=='5' && salt[2]=='$') {
     141             :                         char *output;
     142          12 :                         output = emalloc(PHP_MAX_SALT_LEN);
     143             : 
     144          12 :                         crypt_res = php_sha256_crypt_r(password, salt, output, PHP_MAX_SALT_LEN);
     145          12 :                         if (!crypt_res) {
     146           0 :                                 memset(output, 0, PHP_MAX_SALT_LEN);
     147           0 :                                 efree(output);
     148           0 :                                 return NULL;
     149             :                         } else {
     150          24 :                                 result = zend_string_init(output, strlen(output), 0);
     151          12 :                                 memset(output, 0, PHP_MAX_SALT_LEN);
     152          12 :                                 efree(output);
     153          12 :                                 return result;
     154             :                         }
     155         201 :                 } else if (
     156          76 :                                 salt[0] == '$' &&
     157          64 :                                 salt[1] == '2' &&
     158          61 :                                 salt[3] == '$') {
     159             :                         char output[PHP_MAX_SALT_LEN + 1];
     160             : 
     161          61 :                         memset(output, 0, PHP_MAX_SALT_LEN + 1);
     162             : 
     163          61 :                         crypt_res = php_crypt_blowfish_rn(password, salt, output, sizeof(output));
     164          61 :                         if (!crypt_res) {
     165          24 :                                 ZEND_SECURE_ZERO(output, PHP_MAX_SALT_LEN + 1);
     166          24 :                                 return NULL;
     167             :                         } else {
     168          74 :                                 result = zend_string_init(output, strlen(output), 0);
     169          37 :                                 ZEND_SECURE_ZERO(output, PHP_MAX_SALT_LEN + 1);
     170          37 :                                 return result;
     171             :                         }
     172          15 :         } else if (salt[0] == '*' && (salt[1] == '0' || salt[1] == '1')) {
     173           2 :             return NULL;
     174             :                 } else {
     175             :                         /* DES Fallback */
     176             : 
     177             :                         /* Only check the salt if it's not EXT_DES */
     178          13 :                         if (salt[0] != '_') {
     179             :                                 /* DES style hashes */
     180           9 :                                 if (!IS_VALID_SALT_CHARACTER(salt[0]) || !IS_VALID_SALT_CHARACTER(salt[1])) {
     181           4 :                                         if (!quiet) {
     182             :                                                 /* error consistently about invalid DES fallbacks */
     183           2 :                                                 php_error_docref(NULL, E_DEPRECATED, DES_INVALID_SALT_ERROR);
     184             :                                         }
     185             :                                 }
     186             :                         }
     187             : 
     188          13 :                         memset(&buffer, 0, sizeof(buffer));
     189          13 :                         _crypt_extended_init_r();
     190             : 
     191          13 :                         crypt_res = _crypt_extended_r(password, salt, &buffer);
     192          13 :                         if (!crypt_res || (salt[0] == '*' && salt[1] == '0')) {
     193           2 :                                 return NULL;
     194             :                         } else {
     195          22 :                                 result = zend_string_init(crypt_res, strlen(crypt_res), 0);
     196          11 :                                 return result;
     197             :                         }
     198             :                 }
     199             :         }
     200             : #else
     201             : 
     202             :         if (salt[0] != '$' && salt[0] != '_' && (!IS_VALID_SALT_CHARACTER(salt[0]) || !IS_VALID_SALT_CHARACTER(salt[1]))) {
     203             :                 if (!quiet) {
     204             :                         /* error consistently about invalid DES fallbacks */
     205             :                         php_error_docref(NULL, E_DEPRECATED, DES_INVALID_SALT_ERROR);
     206             :                 }
     207             :         }
     208             : 
     209             : # if defined(HAVE_CRYPT_R) && (defined(_REENTRANT) || defined(_THREAD_SAFE))
     210             :         {
     211             : #  if defined(CRYPT_R_STRUCT_CRYPT_DATA)
     212             :                 struct crypt_data buffer;
     213             :                 memset(&buffer, 0, sizeof(buffer));
     214             : #  elif defined(CRYPT_R_CRYPTD)
     215             :                 CRYPTD buffer;
     216             : #  else
     217             : #   error Data struct used by crypt_r() is unknown. Please report.
     218             : #  endif
     219             :                 crypt_res = crypt_r(password, salt, &buffer);
     220             :         }
     221             : # elif defined(HAVE_CRYPT)
     222             :         crypt_res = crypt(password, salt);
     223             : # else
     224             : #  error No crypt() implementation
     225             : # endif
     226             : #endif
     227             : 
     228             :         if (!crypt_res || (salt[0] == '*' && salt[1] == '0')) {
     229             :                 return NULL;
     230             :         } else {
     231             :                 result = zend_string_init(crypt_res, strlen(crypt_res), 0);
     232             :                 return result;
     233             :         }
     234             : }
     235             : /* }}} */
     236             : 
     237             : 
     238             : /* {{{ proto string crypt(string str [, string salt])
     239             :    Hash a string */
     240          98 : PHP_FUNCTION(crypt)
     241             : {
     242             :         char salt[PHP_MAX_SALT_LEN + 1];
     243          98 :         char *str, *salt_in = NULL;
     244          98 :         size_t str_len, salt_in_len = 0;
     245             :         zend_string *result;
     246             : 
     247          98 :         salt[0] = salt[PHP_MAX_SALT_LEN] = '\0';
     248             : 
     249             :         /* This will produce suitable results if people depend on DES-encryption
     250             :          * available (passing always 2-character salt). At least for glibc6.1 */
     251          98 :         memset(&salt[1], '$', PHP_MAX_SALT_LEN - 1);
     252             : 
     253          98 :         if (zend_parse_parameters(ZEND_NUM_ARGS(), "s|s", &str, &str_len, &salt_in, &salt_in_len) == FAILURE) {
     254           2 :                 return;
     255             :         }
     256             : 
     257          96 :         if (salt_in) {
     258          95 :                 memcpy(salt, salt_in, MIN(PHP_MAX_SALT_LEN, salt_in_len));
     259             :         } else {
     260           1 :                 php_error_docref(NULL, E_NOTICE, "No salt parameter was specified. You must use a randomly generated salt and a strong hash function to produce a secure hash.");
     261             :         }
     262             : 
     263             :         /* The automatic salt generation covers standard DES, md5-crypt and Blowfish (simple) */
     264          96 :         if (!*salt) {
     265           1 :                 strncpy(salt, "$1$", 3);
     266           1 :                 php_random_bytes_throw(&salt[3], 8);
     267           1 :                 php_to64(&salt[3], 8);
     268           1 :                 strncpy(&salt[11], "$", PHP_MAX_SALT_LEN - 11);
     269           1 :                 salt_in_len = strlen(salt);
     270             :         } else {
     271          95 :                 salt_in_len = MIN(PHP_MAX_SALT_LEN, salt_in_len);
     272             :         }
     273          96 :         salt[salt_in_len] = '\0';
     274             : 
     275          96 :         if ((result = php_crypt(str, (int)str_len, salt, (int)salt_in_len, 0)) == NULL) {
     276          27 :                 if (salt[0] == '*' && salt[1] == '0') {
     277           4 :                         RETURN_STRING("*1");
     278             :                 } else {
     279          50 :                         RETURN_STRING("*0");
     280             :                 }
     281             :         }
     282          69 :         RETURN_STR(result);
     283             : }
     284             : /* }}} */
     285             : 
     286             : /*
     287             :  * Local variables:
     288             :  * tab-width: 4
     289             :  * c-basic-offset: 4
     290             :  * End:
     291             :  * vim600: sw=4 ts=4 fdm=marker
     292             :  * vim<600: sw=4 ts=4
     293             :  */

Generated by: LCOV version 1.10

Generated at Wed, 20 Jul 2016 02:56:28 +0000 (6 days ago)

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