crypt.c 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  1. /*
  2. +----------------------------------------------------------------------+
  3. | Copyright (c) The PHP Group |
  4. +----------------------------------------------------------------------+
  5. | This source file is subject to version 3.01 of the PHP license, |
  6. | that is bundled with this package in the file LICENSE, and is |
  7. | available through the world-wide-web at the following url: |
  8. | https://www.php.net/license/3_01.txt |
  9. | If you did not receive a copy of the PHP license and are unable to |
  10. | obtain it through the world-wide-web, please send a note to |
  11. | license@php.net so we can mail you a copy immediately. |
  12. +----------------------------------------------------------------------+
  13. | Authors: Stig Bakken <ssb@php.net> |
  14. | Zeev Suraski <zeev@php.net> |
  15. | Rasmus Lerdorf <rasmus@php.net> |
  16. | Pierre Joye <pierre@php.net> |
  17. +----------------------------------------------------------------------+
  18. */
  19. #include <stdlib.h>
  20. #include "php.h"
  21. #if HAVE_UNISTD_H
  22. #include <unistd.h>
  23. #endif
  24. #if PHP_USE_PHP_CRYPT_R
  25. # include "php_crypt_r.h"
  26. # include "crypt_freesec.h"
  27. #else
  28. # if HAVE_CRYPT_H
  29. # if defined(CRYPT_R_GNU_SOURCE) && !defined(_GNU_SOURCE)
  30. # define _GNU_SOURCE
  31. # endif
  32. # include <crypt.h>
  33. # endif
  34. #endif
  35. #include <time.h>
  36. #include <string.h>
  37. #ifdef PHP_WIN32
  38. #include <process.h>
  39. #endif
  40. #include "php_crypt.h"
  41. #include "php_random.h"
  42. /* sha512 crypt has the maximal salt length of 123 characters */
  43. #define PHP_MAX_SALT_LEN 123
  44. /* Used to check DES salts to ensure that they contain only valid characters */
  45. #define IS_VALID_SALT_CHARACTER(c) (((c) >= '.' && (c) <= '9') || ((c) >= 'A' && (c) <= 'Z') || ((c) >= 'a' && (c) <= 'z'))
  46. PHP_MINIT_FUNCTION(crypt) /* {{{ */
  47. {
  48. REGISTER_LONG_CONSTANT("CRYPT_SALT_LENGTH", PHP_MAX_SALT_LEN, CONST_CS | CONST_PERSISTENT);
  49. REGISTER_LONG_CONSTANT("CRYPT_STD_DES", 1, CONST_CS | CONST_PERSISTENT);
  50. REGISTER_LONG_CONSTANT("CRYPT_EXT_DES", 1, CONST_CS | CONST_PERSISTENT);
  51. REGISTER_LONG_CONSTANT("CRYPT_MD5", 1, CONST_CS | CONST_PERSISTENT);
  52. REGISTER_LONG_CONSTANT("CRYPT_BLOWFISH", 1, CONST_CS | CONST_PERSISTENT);
  53. REGISTER_LONG_CONSTANT("CRYPT_SHA256", 1, CONST_CS | CONST_PERSISTENT);
  54. REGISTER_LONG_CONSTANT("CRYPT_SHA512", 1, CONST_CS | CONST_PERSISTENT);
  55. #if PHP_USE_PHP_CRYPT_R
  56. php_init_crypt_r();
  57. #endif
  58. return SUCCESS;
  59. }
  60. /* }}} */
  61. PHP_MSHUTDOWN_FUNCTION(crypt) /* {{{ */
  62. {
  63. #if PHP_USE_PHP_CRYPT_R
  64. php_shutdown_crypt_r();
  65. #endif
  66. return SUCCESS;
  67. }
  68. /* }}} */
  69. PHPAPI zend_string *php_crypt(const char *password, const int pass_len, const char *salt, int salt_len, bool quiet)
  70. {
  71. char *crypt_res;
  72. zend_string *result;
  73. if (salt[0] == '*' && (salt[1] == '0' || salt[1] == '1')) {
  74. return NULL;
  75. }
  76. /* Windows (win32/crypt) has a stripped down version of libxcrypt and
  77. a CryptoApi md5_crypt implementation */
  78. #if PHP_USE_PHP_CRYPT_R
  79. {
  80. struct php_crypt_extended_data buffer;
  81. if (salt[0]=='$' && salt[1]=='1' && salt[2]=='$') {
  82. char output[MD5_HASH_MAX_LEN], *out;
  83. out = php_md5_crypt_r(password, salt, output);
  84. if (out) {
  85. return zend_string_init(out, strlen(out), 0);
  86. }
  87. return NULL;
  88. } else if (salt[0]=='$' && salt[1]=='6' && salt[2]=='$') {
  89. char *output;
  90. output = emalloc(PHP_MAX_SALT_LEN);
  91. crypt_res = php_sha512_crypt_r(password, salt, output, PHP_MAX_SALT_LEN);
  92. if (!crypt_res) {
  93. ZEND_SECURE_ZERO(output, PHP_MAX_SALT_LEN);
  94. efree(output);
  95. return NULL;
  96. } else {
  97. result = zend_string_init(output, strlen(output), 0);
  98. ZEND_SECURE_ZERO(output, PHP_MAX_SALT_LEN);
  99. efree(output);
  100. return result;
  101. }
  102. } else if (salt[0]=='$' && salt[1]=='5' && salt[2]=='$') {
  103. char *output;
  104. output = emalloc(PHP_MAX_SALT_LEN);
  105. crypt_res = php_sha256_crypt_r(password, salt, output, PHP_MAX_SALT_LEN);
  106. if (!crypt_res) {
  107. ZEND_SECURE_ZERO(output, PHP_MAX_SALT_LEN);
  108. efree(output);
  109. return NULL;
  110. } else {
  111. result = zend_string_init(output, strlen(output), 0);
  112. ZEND_SECURE_ZERO(output, PHP_MAX_SALT_LEN);
  113. efree(output);
  114. return result;
  115. }
  116. } else if (
  117. salt[0] == '$' &&
  118. salt[1] == '2' &&
  119. salt[3] == '$') {
  120. char output[PHP_MAX_SALT_LEN + 1];
  121. memset(output, 0, PHP_MAX_SALT_LEN + 1);
  122. crypt_res = php_crypt_blowfish_rn(password, salt, output, sizeof(output));
  123. if (!crypt_res) {
  124. ZEND_SECURE_ZERO(output, PHP_MAX_SALT_LEN + 1);
  125. return NULL;
  126. } else {
  127. result = zend_string_init(output, strlen(output), 0);
  128. ZEND_SECURE_ZERO(output, PHP_MAX_SALT_LEN + 1);
  129. return result;
  130. }
  131. } else if (salt[0] == '_'
  132. || (IS_VALID_SALT_CHARACTER(salt[0]) && IS_VALID_SALT_CHARACTER(salt[1]))) {
  133. /* DES Fallback */
  134. memset(&buffer, 0, sizeof(buffer));
  135. _crypt_extended_init_r();
  136. crypt_res = _crypt_extended_r((const unsigned char *) password, salt, &buffer);
  137. if (!crypt_res || (salt[0] == '*' && salt[1] == '0')) {
  138. return NULL;
  139. } else {
  140. result = zend_string_init(crypt_res, strlen(crypt_res), 0);
  141. return result;
  142. }
  143. } else {
  144. /* Unknown hash type */
  145. return NULL;
  146. }
  147. }
  148. #else
  149. # if defined(HAVE_CRYPT_R) && (defined(_REENTRANT) || defined(_THREAD_SAFE))
  150. # if defined(CRYPT_R_STRUCT_CRYPT_DATA)
  151. struct crypt_data buffer;
  152. memset(&buffer, 0, sizeof(buffer));
  153. # elif defined(CRYPT_R_CRYPTD)
  154. CRYPTD buffer;
  155. # else
  156. # error Data struct used by crypt_r() is unknown. Please report.
  157. # endif
  158. crypt_res = crypt_r(password, salt, &buffer);
  159. # elif defined(HAVE_CRYPT)
  160. crypt_res = crypt(password, salt);
  161. # else
  162. # error No crypt() implementation
  163. # endif
  164. #endif
  165. if (!crypt_res || (salt[0] == '*' && salt[1] == '0')) {
  166. return NULL;
  167. } else {
  168. result = zend_string_init(crypt_res, strlen(crypt_res), 0);
  169. return result;
  170. }
  171. }
  172. /* }}} */
  173. /* {{{ Hash a string */
  174. PHP_FUNCTION(crypt)
  175. {
  176. char salt[PHP_MAX_SALT_LEN + 1];
  177. char *str, *salt_in = NULL;
  178. size_t str_len, salt_in_len = 0;
  179. zend_string *result;
  180. ZEND_PARSE_PARAMETERS_START(2, 2)
  181. Z_PARAM_STRING(str, str_len)
  182. Z_PARAM_STRING(salt_in, salt_in_len)
  183. ZEND_PARSE_PARAMETERS_END();
  184. salt[0] = salt[PHP_MAX_SALT_LEN] = '\0';
  185. /* This will produce suitable results if people depend on DES-encryption
  186. * available (passing always 2-character salt). At least for glibc6.1 */
  187. memset(&salt[1], '$', PHP_MAX_SALT_LEN - 1);
  188. memcpy(salt, salt_in, MIN(PHP_MAX_SALT_LEN, salt_in_len));
  189. salt_in_len = MIN(PHP_MAX_SALT_LEN, salt_in_len);
  190. salt[salt_in_len] = '\0';
  191. if ((result = php_crypt(str, (int)str_len, salt, (int)salt_in_len, 0)) == NULL) {
  192. if (salt[0] == '*' && salt[1] == '0') {
  193. RETURN_STRING("*1");
  194. } else {
  195. RETURN_STRING("*0");
  196. }
  197. }
  198. RETURN_STR(result);
  199. }
  200. /* }}} */