PageRenderTime 28ms CodeModel.GetById 14ms RepoModel.GetById 0ms app.codeStats 0ms

/release/src/router/php/ext/standard/password.c

https://gitlab.com/envieidoc/advancedtomato2
C | 460 lines | 359 code | 59 blank | 42 comment | 100 complexity | 21b0ff71787f71e734811e9c5807f283 MD5 | raw file
  1. /*
  2. +----------------------------------------------------------------------+
  3. | PHP Version 5 |
  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. /* $Id$ */
  19. #include <stdlib.h>
  20. #include "php.h"
  21. #if HAVE_CRYPT
  22. #include "fcntl.h"
  23. #include "php_password.h"
  24. #include "php_rand.h"
  25. #include "php_crypt.h"
  26. #include "base64.h"
  27. #include "zend_interfaces.h"
  28. #include "info.h"
  29. #if PHP_WIN32
  30. #include "win32/winutil.h"
  31. #endif
  32. PHP_MINIT_FUNCTION(password) /* {{{ */
  33. {
  34. REGISTER_LONG_CONSTANT("PASSWORD_DEFAULT", PHP_PASSWORD_DEFAULT, CONST_CS | CONST_PERSISTENT);
  35. REGISTER_LONG_CONSTANT("PASSWORD_BCRYPT", PHP_PASSWORD_BCRYPT, CONST_CS | CONST_PERSISTENT);
  36. REGISTER_LONG_CONSTANT("PASSWORD_BCRYPT_DEFAULT_COST", PHP_PASSWORD_BCRYPT_COST, CONST_CS | CONST_PERSISTENT);
  37. return SUCCESS;
  38. }
  39. /* }}} */
  40. static char* php_password_get_algo_name(const php_password_algo algo)
  41. {
  42. switch (algo) {
  43. case PHP_PASSWORD_BCRYPT:
  44. return "bcrypt";
  45. case PHP_PASSWORD_UNKNOWN:
  46. default:
  47. return "unknown";
  48. }
  49. }
  50. static php_password_algo php_password_determine_algo(const char *hash, const size_t len)
  51. {
  52. if (len > 3 && hash[0] == '$' && hash[1] == '2' && hash[2] == 'y' && len == 60) {
  53. return PHP_PASSWORD_BCRYPT;
  54. }
  55. return PHP_PASSWORD_UNKNOWN;
  56. }
  57. static int php_password_salt_is_alphabet(const char *str, const size_t len) /* {{{ */
  58. {
  59. size_t i = 0;
  60. for (i = 0; i < len; i++) {
  61. if (!((str[i] >= 'A' && str[i] <= 'Z') || (str[i] >= 'a' && str[i] <= 'z') || (str[i] >= '0' && str[i] <= '9') || str[i] == '.' || str[i] == '/')) {
  62. return FAILURE;
  63. }
  64. }
  65. return SUCCESS;
  66. }
  67. /* }}} */
  68. static int php_password_salt_to64(const char *str, const size_t str_len, const size_t out_len, char *ret) /* {{{ */
  69. {
  70. size_t pos = 0;
  71. size_t ret_len = 0;
  72. unsigned char *buffer;
  73. if ((int) str_len < 0) {
  74. return FAILURE;
  75. }
  76. buffer = php_base64_encode((unsigned char*) str, (int) str_len, (int*) &ret_len);
  77. if (ret_len < out_len) {
  78. /* Too short of an encoded string generated */
  79. efree(buffer);
  80. return FAILURE;
  81. }
  82. for (pos = 0; pos < out_len; pos++) {
  83. if (buffer[pos] == '+') {
  84. ret[pos] = '.';
  85. } else if (buffer[pos] == '=') {
  86. efree(buffer);
  87. return FAILURE;
  88. } else {
  89. ret[pos] = buffer[pos];
  90. }
  91. }
  92. efree(buffer);
  93. return SUCCESS;
  94. }
  95. /* }}} */
  96. static int php_password_make_salt(size_t length, char *ret TSRMLS_DC) /* {{{ */
  97. {
  98. int buffer_valid = 0;
  99. size_t i, raw_length;
  100. char *buffer;
  101. char *result;
  102. if (length > (INT_MAX / 3)) {
  103. php_error_docref(NULL TSRMLS_CC, E_WARNING, "Length is too large to safely generate");
  104. return FAILURE;
  105. }
  106. raw_length = length * 3 / 4 + 1;
  107. buffer = (char *) safe_emalloc(raw_length, 1, 1);
  108. #if PHP_WIN32
  109. {
  110. BYTE *iv_b = (BYTE *) buffer;
  111. if (php_win32_get_random_bytes(iv_b, raw_length) == SUCCESS) {
  112. buffer_valid = 1;
  113. }
  114. }
  115. #else
  116. {
  117. int fd, n;
  118. size_t read_bytes = 0;
  119. fd = open("/dev/urandom", O_RDONLY);
  120. if (fd >= 0) {
  121. while (read_bytes < raw_length) {
  122. n = read(fd, buffer + read_bytes, raw_length - read_bytes);
  123. if (n < 0) {
  124. break;
  125. }
  126. read_bytes += (size_t) n;
  127. }
  128. close(fd);
  129. }
  130. if (read_bytes >= raw_length) {
  131. buffer_valid = 1;
  132. }
  133. }
  134. #endif
  135. if (!buffer_valid) {
  136. for (i = 0; i < raw_length; i++) {
  137. buffer[i] ^= (char) (255.0 * php_rand(TSRMLS_C) / RAND_MAX);
  138. }
  139. }
  140. result = safe_emalloc(length, 1, 1);
  141. if (php_password_salt_to64(buffer, raw_length, length, result) == FAILURE) {
  142. php_error_docref(NULL TSRMLS_CC, E_WARNING, "Generated salt too short");
  143. efree(buffer);
  144. efree(result);
  145. return FAILURE;
  146. }
  147. memcpy(ret, result, (int) length);
  148. efree(result);
  149. efree(buffer);
  150. ret[length] = 0;
  151. return SUCCESS;
  152. }
  153. /* }}} */
  154. PHP_FUNCTION(password_get_info)
  155. {
  156. php_password_algo algo;
  157. int hash_len;
  158. char *hash, *algo_name;
  159. zval *options;
  160. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s", &hash, &hash_len) == FAILURE) {
  161. return;
  162. }
  163. if (hash_len < 0 || (size_t) hash_len < 0) {
  164. php_error_docref(NULL TSRMLS_CC, E_WARNING, "Supplied password hash too long to safely identify");
  165. RETURN_FALSE;
  166. }
  167. ALLOC_INIT_ZVAL(options);
  168. array_init(options);
  169. algo = php_password_determine_algo(hash, (size_t) hash_len);
  170. algo_name = php_password_get_algo_name(algo);
  171. switch (algo) {
  172. case PHP_PASSWORD_BCRYPT:
  173. {
  174. long cost = PHP_PASSWORD_BCRYPT_COST;
  175. sscanf(hash, "$2y$%ld$", &cost);
  176. add_assoc_long(options, "cost", cost);
  177. }
  178. break;
  179. case PHP_PASSWORD_UNKNOWN:
  180. default:
  181. break;
  182. }
  183. array_init(return_value);
  184. add_assoc_long(return_value, "algo", algo);
  185. add_assoc_string(return_value, "algoName", algo_name, 1);
  186. add_assoc_zval(return_value, "options", options);
  187. }
  188. PHP_FUNCTION(password_needs_rehash)
  189. {
  190. long new_algo = 0;
  191. php_password_algo algo;
  192. int hash_len;
  193. char *hash;
  194. HashTable *options = 0;
  195. zval **option_buffer;
  196. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "sl|H", &hash, &hash_len, &new_algo, &options) == FAILURE) {
  197. return;
  198. }
  199. if (hash_len < 0) {
  200. php_error_docref(NULL TSRMLS_CC, E_WARNING, "Supplied password hash too long to safely identify");
  201. RETURN_FALSE;
  202. }
  203. algo = php_password_determine_algo(hash, (size_t) hash_len);
  204. if (algo != new_algo) {
  205. RETURN_TRUE;
  206. }
  207. switch (algo) {
  208. case PHP_PASSWORD_BCRYPT:
  209. {
  210. long new_cost = PHP_PASSWORD_BCRYPT_COST, cost = 0;
  211. if (options && zend_symtable_find(options, "cost", sizeof("cost"), (void **) &option_buffer) == SUCCESS) {
  212. if (Z_TYPE_PP(option_buffer) != IS_LONG) {
  213. zval cast_option_buffer;
  214. MAKE_COPY_ZVAL(option_buffer, &cast_option_buffer);
  215. convert_to_long(&cast_option_buffer);
  216. new_cost = Z_LVAL(cast_option_buffer);
  217. zval_dtor(&cast_option_buffer);
  218. } else {
  219. new_cost = Z_LVAL_PP(option_buffer);
  220. }
  221. }
  222. sscanf(hash, "$2y$%ld$", &cost);
  223. if (cost != new_cost) {
  224. RETURN_TRUE;
  225. }
  226. }
  227. break;
  228. case PHP_PASSWORD_UNKNOWN:
  229. default:
  230. break;
  231. }
  232. RETURN_FALSE;
  233. }
  234. /* {{{ proto boolean password_make_salt(string password, string hash)
  235. Verify a hash created using crypt() or password_hash() */
  236. PHP_FUNCTION(password_verify)
  237. {
  238. int status = 0, i;
  239. int password_len, hash_len;
  240. char *ret, *password, *hash;
  241. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "ss", &password, &password_len, &hash, &hash_len) == FAILURE) {
  242. RETURN_FALSE;
  243. }
  244. if (php_crypt(password, password_len, hash, hash_len, &ret) == FAILURE) {
  245. RETURN_FALSE;
  246. }
  247. if (strlen(ret) != hash_len || hash_len < 13) {
  248. efree(ret);
  249. RETURN_FALSE;
  250. }
  251. /* We're using this method instead of == in order to provide
  252. * resistence towards timing attacks. This is a constant time
  253. * equality check that will always check every byte of both
  254. * values. */
  255. for (i = 0; i < hash_len; i++) {
  256. status |= (ret[i] ^ hash[i]);
  257. }
  258. efree(ret);
  259. RETURN_BOOL(status == 0);
  260. }
  261. /* }}} */
  262. /* {{{ proto string password_hash(string password, int algo, array options = array())
  263. Hash a password */
  264. PHP_FUNCTION(password_hash)
  265. {
  266. char *hash_format, *hash, *salt, *password, *result;
  267. long algo = 0;
  268. int password_len = 0, hash_len;
  269. size_t salt_len = 0, required_salt_len = 0, hash_format_len;
  270. HashTable *options = 0;
  271. zval **option_buffer;
  272. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "sl|H", &password, &password_len, &algo, &options) == FAILURE) {
  273. return;
  274. }
  275. switch (algo) {
  276. case PHP_PASSWORD_BCRYPT:
  277. {
  278. long cost = PHP_PASSWORD_BCRYPT_COST;
  279. if (options && zend_symtable_find(options, "cost", 5, (void **) &option_buffer) == SUCCESS) {
  280. if (Z_TYPE_PP(option_buffer) != IS_LONG) {
  281. zval cast_option_buffer;
  282. MAKE_COPY_ZVAL(option_buffer, &cast_option_buffer);
  283. convert_to_long(&cast_option_buffer);
  284. cost = Z_LVAL(cast_option_buffer);
  285. zval_dtor(&cast_option_buffer);
  286. } else {
  287. cost = Z_LVAL_PP(option_buffer);
  288. }
  289. }
  290. if (cost < 4 || cost > 31) {
  291. php_error_docref(NULL TSRMLS_CC, E_WARNING, "Invalid bcrypt cost parameter specified: %ld", cost);
  292. RETURN_NULL();
  293. }
  294. required_salt_len = 22;
  295. hash_format = emalloc(8);
  296. sprintf(hash_format, "$2y$%02ld$", cost);
  297. hash_format_len = 7;
  298. }
  299. break;
  300. case PHP_PASSWORD_UNKNOWN:
  301. default:
  302. php_error_docref(NULL TSRMLS_CC, E_WARNING, "Unknown password hashing algorithm: %ld", algo);
  303. RETURN_NULL();
  304. }
  305. if (options && zend_symtable_find(options, "salt", 5, (void**) &option_buffer) == SUCCESS) {
  306. char *buffer;
  307. int buffer_len_int = 0;
  308. size_t buffer_len;
  309. switch (Z_TYPE_PP(option_buffer)) {
  310. case IS_STRING:
  311. buffer = estrndup(Z_STRVAL_PP(option_buffer), Z_STRLEN_PP(option_buffer));
  312. buffer_len_int = Z_STRLEN_PP(option_buffer);
  313. break;
  314. case IS_LONG:
  315. case IS_DOUBLE:
  316. case IS_OBJECT: {
  317. zval cast_option_buffer;
  318. MAKE_COPY_ZVAL(option_buffer, &cast_option_buffer);
  319. convert_to_string(&cast_option_buffer);
  320. if (Z_TYPE(cast_option_buffer) == IS_STRING) {
  321. buffer = estrndup(Z_STRVAL(cast_option_buffer), Z_STRLEN(cast_option_buffer));
  322. buffer_len_int = Z_STRLEN(cast_option_buffer);
  323. zval_dtor(&cast_option_buffer);
  324. break;
  325. }
  326. zval_dtor(&cast_option_buffer);
  327. }
  328. case IS_BOOL:
  329. case IS_NULL:
  330. case IS_RESOURCE:
  331. case IS_ARRAY:
  332. default:
  333. efree(hash_format);
  334. php_error_docref(NULL TSRMLS_CC, E_WARNING, "Non-string salt parameter supplied");
  335. RETURN_NULL();
  336. }
  337. if (buffer_len_int < 0) {
  338. efree(hash_format);
  339. efree(buffer);
  340. php_error_docref(NULL TSRMLS_CC, E_WARNING, "Supplied salt is too long");
  341. }
  342. buffer_len = (size_t) buffer_len_int;
  343. if (buffer_len < required_salt_len) {
  344. efree(hash_format);
  345. efree(buffer);
  346. php_error_docref(NULL TSRMLS_CC, E_WARNING, "Provided salt is too short: %lu expecting %lu", (unsigned long) buffer_len, (unsigned long) required_salt_len);
  347. RETURN_NULL();
  348. } else if (php_password_salt_is_alphabet(buffer, buffer_len) == FAILURE) {
  349. salt = safe_emalloc(required_salt_len, 1, 1);
  350. if (php_password_salt_to64(buffer, buffer_len, required_salt_len, salt) == FAILURE) {
  351. efree(hash_format);
  352. efree(buffer);
  353. efree(salt);
  354. php_error_docref(NULL TSRMLS_CC, E_WARNING, "Provided salt is too short: %lu", (unsigned long) buffer_len);
  355. RETURN_NULL();
  356. }
  357. salt_len = required_salt_len;
  358. } else {
  359. salt = safe_emalloc(required_salt_len, 1, 1);
  360. memcpy(salt, buffer, (int) required_salt_len);
  361. salt_len = required_salt_len;
  362. }
  363. efree(buffer);
  364. } else {
  365. salt = safe_emalloc(required_salt_len, 1, 1);
  366. if (php_password_make_salt(required_salt_len, salt TSRMLS_CC) == FAILURE) {
  367. efree(hash_format);
  368. efree(salt);
  369. RETURN_FALSE;
  370. }
  371. salt_len = required_salt_len;
  372. }
  373. salt[salt_len] = 0;
  374. hash = safe_emalloc(salt_len + hash_format_len, 1, 1);
  375. sprintf(hash, "%s%s", hash_format, salt);
  376. hash[hash_format_len + salt_len] = 0;
  377. efree(hash_format);
  378. efree(salt);
  379. /* This cast is safe, since both values are defined here in code and cannot overflow */
  380. hash_len = (int) (hash_format_len + salt_len);
  381. if (php_crypt(password, password_len, hash, hash_len, &result) == FAILURE) {
  382. efree(hash);
  383. RETURN_FALSE;
  384. }
  385. efree(hash);
  386. if (strlen(result) < 13) {
  387. efree(result);
  388. RETURN_FALSE;
  389. }
  390. RETURN_STRING(result, 0);
  391. }
  392. /* }}} */
  393. #endif /* HAVE_CRYPT */
  394. /*
  395. * Local variables:
  396. * tab-width: 4
  397. * c-basic-offset: 4
  398. * End:
  399. * vim600: sw=4 ts=4 fdm=marker
  400. * vim<600: sw=4 ts=4
  401. */