password.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462
  1. /*
  2. +----------------------------------------------------------------------+
  3. | PHP Version 5 |
  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: 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) {
  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[8], *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. sprintf(hash_format, "$2y$%02ld$", cost);
  296. hash_format_len = 7;
  297. }
  298. break;
  299. case PHP_PASSWORD_UNKNOWN:
  300. default:
  301. php_error_docref(NULL TSRMLS_CC, E_WARNING, "Unknown password hashing algorithm: %ld", algo);
  302. RETURN_NULL();
  303. }
  304. if (options && zend_symtable_find(options, "salt", 5, (void**) &option_buffer) == SUCCESS) {
  305. char *buffer;
  306. int buffer_len_int = 0;
  307. size_t buffer_len;
  308. zval cast_option_buffer = zval_used_for_init;
  309. switch (Z_TYPE_PP(option_buffer)) {
  310. case IS_STRING:
  311. buffer = Z_STRVAL_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. MAKE_COPY_ZVAL(option_buffer, &cast_option_buffer);
  318. convert_to_string(&cast_option_buffer);
  319. if (Z_TYPE(cast_option_buffer) == IS_STRING) {
  320. buffer = Z_STRVAL(cast_option_buffer);
  321. buffer_len_int = Z_STRLEN(cast_option_buffer);
  322. break;
  323. }
  324. case IS_BOOL:
  325. case IS_NULL:
  326. case IS_RESOURCE:
  327. case IS_ARRAY:
  328. default:
  329. if (Z_TYPE(cast_option_buffer) != IS_NULL) {
  330. zval_dtor(&cast_option_buffer);
  331. }
  332. php_error_docref(NULL TSRMLS_CC, E_WARNING, "Non-string salt parameter supplied");
  333. RETURN_NULL();
  334. }
  335. if (buffer_len_int < 0) {
  336. php_error_docref(NULL TSRMLS_CC, E_WARNING, "Supplied salt is too long");
  337. if (Z_TYPE(cast_option_buffer) != IS_NULL) {
  338. zval_dtor(&cast_option_buffer);
  339. }
  340. RETURN_NULL();
  341. }
  342. buffer_len = (size_t) buffer_len_int;
  343. if (buffer_len < required_salt_len) {
  344. 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);
  345. if (Z_TYPE(cast_option_buffer) != IS_NULL) {
  346. zval_dtor(&cast_option_buffer);
  347. }
  348. RETURN_NULL();
  349. } else if (php_password_salt_is_alphabet(buffer, buffer_len) == FAILURE) {
  350. salt = safe_emalloc(required_salt_len, 1, 1);
  351. if (php_password_salt_to64(buffer, buffer_len, required_salt_len, salt) == FAILURE) {
  352. efree(salt);
  353. php_error_docref(NULL TSRMLS_CC, E_WARNING, "Provided salt is too short: %lu", (unsigned long) buffer_len);
  354. if (Z_TYPE(cast_option_buffer) != IS_NULL) {
  355. zval_dtor(&cast_option_buffer);
  356. }
  357. RETURN_NULL();
  358. }
  359. salt_len = required_salt_len;
  360. } else {
  361. salt = safe_emalloc(required_salt_len, 1, 1);
  362. memcpy(salt, buffer, (int) required_salt_len);
  363. salt_len = required_salt_len;
  364. }
  365. if (Z_TYPE(cast_option_buffer) != IS_NULL) {
  366. zval_dtor(&cast_option_buffer);
  367. }
  368. } else {
  369. salt = safe_emalloc(required_salt_len, 1, 1);
  370. if (php_password_make_salt(required_salt_len, salt TSRMLS_CC) == FAILURE) {
  371. efree(salt);
  372. RETURN_FALSE;
  373. }
  374. salt_len = required_salt_len;
  375. }
  376. salt[salt_len] = 0;
  377. hash = safe_emalloc(salt_len + hash_format_len, 1, 1);
  378. sprintf(hash, "%s%s", hash_format, salt);
  379. hash[hash_format_len + salt_len] = 0;
  380. efree(salt);
  381. /* This cast is safe, since both values are defined here in code and cannot overflow */
  382. hash_len = (int) (hash_format_len + salt_len);
  383. if (php_crypt(password, password_len, hash, hash_len, &result) == FAILURE) {
  384. efree(hash);
  385. RETURN_FALSE;
  386. }
  387. efree(hash);
  388. if (strlen(result) < 13) {
  389. efree(result);
  390. RETURN_FALSE;
  391. }
  392. RETURN_STRING(result, 0);
  393. }
  394. /* }}} */
  395. #endif /* HAVE_CRYPT */
  396. /*
  397. * Local variables:
  398. * tab-width: 4
  399. * c-basic-offset: 4
  400. * End:
  401. * vim600: sw=4 ts=4 fdm=marker
  402. * vim<600: sw=4 ts=4
  403. */