crypt.c 8.4 KB

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