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: Anthony Ferrara <ircmaxell@php.net> |
16 | Charles R. Portwood II <charlesportwoodii@erianna.com> |
17 +----------------------------------------------------------------------+
18 */
19
20 /* $Id$ */
21
22 #include <stdlib.h>
23
24 #include "php.h"
25
26 #include "fcntl.h"
27 #include "php_password.h"
28 #include "php_rand.h"
29 #include "php_crypt.h"
30 #include "base64.h"
31 #include "zend_interfaces.h"
32 #include "info.h"
33 #include "php_random.h"
34 #if HAVE_ARGON2LIB
35 #include "argon2.h"
36 #endif
37
38 #ifdef PHP_WIN32
39 #include "win32/winutil.h"
40 #endif
41
PHP_MINIT_FUNCTION(password)42 PHP_MINIT_FUNCTION(password) /* {{{ */
43 {
44 REGISTER_LONG_CONSTANT("PASSWORD_DEFAULT", PHP_PASSWORD_DEFAULT, CONST_CS | CONST_PERSISTENT);
45 REGISTER_LONG_CONSTANT("PASSWORD_BCRYPT", PHP_PASSWORD_BCRYPT, CONST_CS | CONST_PERSISTENT);
46 #if HAVE_ARGON2LIB
47 REGISTER_LONG_CONSTANT("PASSWORD_ARGON2I", PHP_PASSWORD_ARGON2I, CONST_CS | CONST_PERSISTENT);
48 #endif
49
50 REGISTER_LONG_CONSTANT("PASSWORD_BCRYPT_DEFAULT_COST", PHP_PASSWORD_BCRYPT_COST, CONST_CS | CONST_PERSISTENT);
51 #if HAVE_ARGON2LIB
52 REGISTER_LONG_CONSTANT("PASSWORD_ARGON2_DEFAULT_MEMORY_COST", PHP_PASSWORD_ARGON2_MEMORY_COST, CONST_CS | CONST_PERSISTENT);
53 REGISTER_LONG_CONSTANT("PASSWORD_ARGON2_DEFAULT_TIME_COST", PHP_PASSWORD_ARGON2_TIME_COST, CONST_CS | CONST_PERSISTENT);
54 REGISTER_LONG_CONSTANT("PASSWORD_ARGON2_DEFAULT_THREADS", PHP_PASSWORD_ARGON2_THREADS, CONST_CS | CONST_PERSISTENT);
55 #endif
56
57 return SUCCESS;
58 }
59 /* }}} */
60
php_password_get_algo_name(const php_password_algo algo)61 static zend_string* php_password_get_algo_name(const php_password_algo algo)
62 {
63 switch (algo) {
64 case PHP_PASSWORD_BCRYPT:
65 return zend_string_init("bcrypt", sizeof("bcrypt") - 1, 0);
66 #if HAVE_ARGON2LIB
67 case PHP_PASSWORD_ARGON2I:
68 return zend_string_init("argon2i", sizeof("argon2i") - 1, 0);
69 #endif
70 case PHP_PASSWORD_UNKNOWN:
71 default:
72 return zend_string_init("unknown", sizeof("unknown") - 1, 0);
73 }
74 }
75
php_password_determine_algo(const zend_string * hash)76 static php_password_algo php_password_determine_algo(const zend_string *hash)
77 {
78 const char *h = ZSTR_VAL(hash);
79 const size_t len = ZSTR_LEN(hash);
80 if (len == 60 && h[0] == '$' && h[1] == '2' && h[2] == 'y') {
81 return PHP_PASSWORD_BCRYPT;
82 }
83 #if HAVE_ARGON2LIB
84 if (len >= sizeof("$argon2i$")-1 && !memcmp(h, "$argon2i$", sizeof("$argon2i$")-1)) {
85 return PHP_PASSWORD_ARGON2I;
86 }
87 #endif
88
89 return PHP_PASSWORD_UNKNOWN;
90 }
91
php_password_salt_is_alphabet(const char * str,const size_t len)92 static int php_password_salt_is_alphabet(const char *str, const size_t len) /* {{{ */
93 {
94 size_t i = 0;
95
96 for (i = 0; i < len; i++) {
97 if (!((str[i] >= 'A' && str[i] <= 'Z') || (str[i] >= 'a' && str[i] <= 'z') || (str[i] >= '0' && str[i] <= '9') || str[i] == '.' || str[i] == '/')) {
98 return FAILURE;
99 }
100 }
101 return SUCCESS;
102 }
103 /* }}} */
104
php_password_salt_to64(const char * str,const size_t str_len,const size_t out_len,char * ret)105 static int php_password_salt_to64(const char *str, const size_t str_len, const size_t out_len, char *ret) /* {{{ */
106 {
107 size_t pos = 0;
108 zend_string *buffer;
109 if ((int) str_len < 0) {
110 return FAILURE;
111 }
112 buffer = php_base64_encode((unsigned char*) str, str_len);
113 if (ZSTR_LEN(buffer) < out_len) {
114 /* Too short of an encoded string generated */
115 zend_string_release(buffer);
116 return FAILURE;
117 }
118 for (pos = 0; pos < out_len; pos++) {
119 if (ZSTR_VAL(buffer)[pos] == '+') {
120 ret[pos] = '.';
121 } else if (ZSTR_VAL(buffer)[pos] == '=') {
122 zend_string_free(buffer);
123 return FAILURE;
124 } else {
125 ret[pos] = ZSTR_VAL(buffer)[pos];
126 }
127 }
128 zend_string_free(buffer);
129 return SUCCESS;
130 }
131 /* }}} */
132
php_password_make_salt(size_t length)133 static zend_string* php_password_make_salt(size_t length) /* {{{ */
134 {
135 zend_string *ret, *buffer;
136
137 if (length > (INT_MAX / 3)) {
138 php_error_docref(NULL, E_WARNING, "Length is too large to safely generate");
139 return NULL;
140 }
141
142 buffer = zend_string_alloc(length * 3 / 4 + 1, 0);
143 if (FAILURE == php_random_bytes_silent(ZSTR_VAL(buffer), ZSTR_LEN(buffer))) {
144 php_error_docref(NULL, E_WARNING, "Unable to generate salt");
145 zend_string_release(buffer);
146 return NULL;
147 }
148
149 ret = zend_string_alloc(length, 0);
150 if (php_password_salt_to64(ZSTR_VAL(buffer), ZSTR_LEN(buffer), length, ZSTR_VAL(ret)) == FAILURE) {
151 php_error_docref(NULL, E_WARNING, "Generated salt too short");
152 zend_string_release(buffer);
153 zend_string_release(ret);
154 return NULL;
155 }
156 zend_string_release(buffer);
157 ZSTR_VAL(ret)[length] = 0;
158 return ret;
159 }
160 /* }}} */
161
162 /* {{{ proto array password_get_info(string $hash)
163 Retrieves information about a given hash */
PHP_FUNCTION(password_get_info)164 PHP_FUNCTION(password_get_info)
165 {
166 php_password_algo algo;
167 zend_string *hash, *algo_name;
168 zval options;
169
170 ZEND_PARSE_PARAMETERS_START(1, 1)
171 Z_PARAM_STR(hash)
172 ZEND_PARSE_PARAMETERS_END();
173
174 array_init(&options);
175
176 algo = php_password_determine_algo(hash);
177 algo_name = php_password_get_algo_name(algo);
178
179 switch (algo) {
180 case PHP_PASSWORD_BCRYPT:
181 {
182 zend_long cost = PHP_PASSWORD_BCRYPT_COST;
183 sscanf(ZSTR_VAL(hash), "$2y$" ZEND_LONG_FMT "$", &cost);
184 add_assoc_long(&options, "cost", cost);
185 }
186 break;
187 #if HAVE_ARGON2LIB
188 case PHP_PASSWORD_ARGON2I:
189 {
190 zend_long v = 0;
191 zend_long memory_cost = PHP_PASSWORD_ARGON2_MEMORY_COST;
192 zend_long time_cost = PHP_PASSWORD_ARGON2_TIME_COST;
193 zend_long threads = PHP_PASSWORD_ARGON2_THREADS;
194
195 sscanf(ZSTR_VAL(hash), "$%*[argon2i]$v=" ZEND_LONG_FMT "$m=" ZEND_LONG_FMT ",t=" ZEND_LONG_FMT ",p=" ZEND_LONG_FMT, &v, &memory_cost, &time_cost, &threads);
196 add_assoc_long(&options, "memory_cost", memory_cost);
197 add_assoc_long(&options, "time_cost", time_cost);
198 add_assoc_long(&options, "threads", threads);
199 }
200 break;
201 #endif
202 case PHP_PASSWORD_UNKNOWN:
203 default:
204 break;
205 }
206
207 array_init(return_value);
208
209 add_assoc_long(return_value, "algo", algo);
210 add_assoc_str(return_value, "algoName", algo_name);
211 add_assoc_zval(return_value, "options", &options);
212 }
213 /** }}} */
214
215 /* {{{ proto boolean password_needs_rehash(string $hash, integer $algo[, array $options])
216 Determines if a given hash requires re-hashing based upon parameters */
PHP_FUNCTION(password_needs_rehash)217 PHP_FUNCTION(password_needs_rehash)
218 {
219 zend_long new_algo = 0;
220 php_password_algo algo;
221 zend_string *hash;
222 HashTable *options = 0;
223 zval *option_buffer;
224
225 ZEND_PARSE_PARAMETERS_START(2, 3)
226 Z_PARAM_STR(hash)
227 Z_PARAM_LONG(new_algo)
228 Z_PARAM_OPTIONAL
229 Z_PARAM_ARRAY_OR_OBJECT_HT(options)
230 ZEND_PARSE_PARAMETERS_END();
231
232 algo = php_password_determine_algo(hash);
233
234 if ((zend_long)algo != new_algo) {
235 RETURN_TRUE;
236 }
237
238 switch (algo) {
239 case PHP_PASSWORD_BCRYPT:
240 {
241 zend_long new_cost = PHP_PASSWORD_BCRYPT_COST, cost = 0;
242
243 if (options && (option_buffer = zend_hash_str_find(options, "cost", sizeof("cost")-1)) != NULL) {
244 new_cost = zval_get_long(option_buffer);
245 }
246
247 sscanf(ZSTR_VAL(hash), "$2y$" ZEND_LONG_FMT "$", &cost);
248 if (cost != new_cost) {
249 RETURN_TRUE;
250 }
251 }
252 break;
253 #if HAVE_ARGON2LIB
254 case PHP_PASSWORD_ARGON2I:
255 {
256 zend_long v = 0;
257 zend_long new_memory_cost = PHP_PASSWORD_ARGON2_MEMORY_COST, memory_cost = 0;
258 zend_long new_time_cost = PHP_PASSWORD_ARGON2_TIME_COST, time_cost = 0;
259 zend_long new_threads = PHP_PASSWORD_ARGON2_THREADS, threads = 0;
260
261 if (options && (option_buffer = zend_hash_str_find(options, "memory_cost", sizeof("memory_cost")-1)) != NULL) {
262 new_memory_cost = zval_get_long(option_buffer);
263 }
264
265 if (options && (option_buffer = zend_hash_str_find(options, "time_cost", sizeof("time_cost")-1)) != NULL) {
266 new_time_cost = zval_get_long(option_buffer);
267 }
268
269 if (options && (option_buffer = zend_hash_str_find(options, "threads", sizeof("threads")-1)) != NULL) {
270 new_threads = zval_get_long(option_buffer);
271 }
272
273 sscanf(ZSTR_VAL(hash), "$%*[argon2i]$v=" ZEND_LONG_FMT "$m=" ZEND_LONG_FMT ",t=" ZEND_LONG_FMT ",p=" ZEND_LONG_FMT, &v, &memory_cost, &time_cost, &threads);
274
275 if (new_time_cost != time_cost || new_memory_cost != memory_cost || new_threads != threads) {
276 RETURN_TRUE;
277 }
278 }
279 break;
280 #endif
281 case PHP_PASSWORD_UNKNOWN:
282 default:
283 break;
284 }
285 RETURN_FALSE;
286 }
287 /* }}} */
288
289 /* {{{ proto boolean password_verify(string password, string hash)
290 Verify a hash created using crypt() or password_hash() */
PHP_FUNCTION(password_verify)291 PHP_FUNCTION(password_verify)
292 {
293 zend_string *password, *hash;
294 php_password_algo algo;
295
296 ZEND_PARSE_PARAMETERS_START(2, 2)
297 Z_PARAM_STR(password)
298 Z_PARAM_STR(hash)
299 ZEND_PARSE_PARAMETERS_END_EX(RETURN_FALSE);
300
301 algo = php_password_determine_algo(hash);
302
303 switch(algo) {
304 #if HAVE_ARGON2LIB
305 case PHP_PASSWORD_ARGON2I:
306 RETURN_BOOL(ARGON2_OK == argon2_verify(ZSTR_VAL(hash), ZSTR_VAL(password), ZSTR_LEN(password), Argon2_i));
307 break;
308 #endif
309 case PHP_PASSWORD_BCRYPT:
310 case PHP_PASSWORD_UNKNOWN:
311 default:
312 {
313 size_t i;
314 int status = 0;
315 zend_string *ret = php_crypt(ZSTR_VAL(password), (int)ZSTR_LEN(password), ZSTR_VAL(hash), (int)ZSTR_LEN(hash), 1);
316
317 if (!ret) {
318 RETURN_FALSE;
319 }
320
321 if (ZSTR_LEN(ret) != ZSTR_LEN(hash) || ZSTR_LEN(hash) < 13) {
322 zend_string_free(ret);
323 RETURN_FALSE;
324 }
325
326 /* We're using this method instead of == in order to provide
327 * resistance towards timing attacks. This is a constant time
328 * equality check that will always check every byte of both
329 * values. */
330 for (i = 0; i < ZSTR_LEN(hash); i++) {
331 status |= (ZSTR_VAL(ret)[i] ^ ZSTR_VAL(hash)[i]);
332 }
333
334 zend_string_free(ret);
335
336 RETURN_BOOL(status == 0);
337 }
338 }
339
340 RETURN_FALSE;
341 }
342 /* }}} */
343
php_password_get_salt(zval * return_value,size_t required_salt_len,HashTable * options)344 static zend_string* php_password_get_salt(zval *return_value, size_t required_salt_len, HashTable *options) {
345 zend_string *buffer;
346 zval *option_buffer;
347
348 if (!options || !(option_buffer = zend_hash_str_find(options, "salt", sizeof("salt") - 1))) {
349 buffer = php_password_make_salt(required_salt_len);
350 if (!buffer) {
351 RETVAL_FALSE;
352 }
353 return buffer;
354 }
355
356 php_error_docref(NULL, E_DEPRECATED, "Use of the 'salt' option to password_hash is deprecated");
357
358 switch (Z_TYPE_P(option_buffer)) {
359 case IS_STRING:
360 buffer = zend_string_copy(Z_STR_P(option_buffer));
361 break;
362 case IS_LONG:
363 case IS_DOUBLE:
364 case IS_OBJECT:
365 buffer = zval_get_string(option_buffer);
366 break;
367 case IS_FALSE:
368 case IS_TRUE:
369 case IS_NULL:
370 case IS_RESOURCE:
371 case IS_ARRAY:
372 default:
373 php_error_docref(NULL, E_WARNING, "Non-string salt parameter supplied");
374 return NULL;
375 }
376
377 /* XXX all the crypt related APIs work with int for string length.
378 That should be revised for size_t and then we maybe don't require
379 the > INT_MAX check. */
380 if (ZEND_SIZE_T_INT_OVFL(ZSTR_LEN(buffer))) {
381 php_error_docref(NULL, E_WARNING, "Supplied salt is too long");
382 zend_string_release(buffer);
383 return NULL;
384 }
385
386 if (ZSTR_LEN(buffer) < required_salt_len) {
387 php_error_docref(NULL, E_WARNING, "Provided salt is too short: %zd expecting %zd", ZSTR_LEN(buffer), required_salt_len);
388 zend_string_release(buffer);
389 return NULL;
390 }
391
392 if (php_password_salt_is_alphabet(ZSTR_VAL(buffer), ZSTR_LEN(buffer)) == FAILURE) {
393 zend_string *salt = zend_string_alloc(required_salt_len, 0);
394 if (php_password_salt_to64(ZSTR_VAL(buffer), ZSTR_LEN(buffer), required_salt_len, ZSTR_VAL(salt)) == FAILURE) {
395 php_error_docref(NULL, E_WARNING, "Provided salt is too short: %zd", ZSTR_LEN(buffer));
396 zend_string_release(salt);
397 zend_string_release(buffer);
398 return NULL;
399 }
400 zend_string_release(buffer);
401 return salt;
402 } else {
403 zend_string *salt = zend_string_alloc(required_salt_len, 0);
404 memcpy(ZSTR_VAL(salt), ZSTR_VAL(buffer), required_salt_len);
405 zend_string_release(buffer);
406 return salt;
407 }
408 }
409
410 /* {{{ proto string password_hash(string password, int algo[, array options = array()])
411 Hash a password */
PHP_FUNCTION(password_hash)412 PHP_FUNCTION(password_hash)
413 {
414 zend_string *password;
415 zend_long algo = PHP_PASSWORD_DEFAULT;
416 HashTable *options = NULL;
417
418 #if HAVE_ARGON2LIB
419 #endif
420
421 ZEND_PARSE_PARAMETERS_START(2, 3)
422 Z_PARAM_STR(password)
423 Z_PARAM_LONG(algo)
424 Z_PARAM_OPTIONAL
425 Z_PARAM_ARRAY_OR_OBJECT_HT(options)
426 ZEND_PARSE_PARAMETERS_END();
427
428 switch (algo) {
429 case PHP_PASSWORD_BCRYPT:
430 {
431 char hash_format[10];
432 size_t hash_format_len;
433 zend_string *result, *hash, *salt;
434 zval *option_buffer;
435 zend_long cost = PHP_PASSWORD_BCRYPT_COST;
436
437 if (options && (option_buffer = zend_hash_str_find(options, "cost", sizeof("cost")-1)) != NULL) {
438 cost = zval_get_long(option_buffer);
439 }
440
441 if (cost < 4 || cost > 31) {
442 php_error_docref(NULL, E_WARNING, "Invalid bcrypt cost parameter specified: " ZEND_LONG_FMT, cost);
443 RETURN_NULL();
444 }
445
446 hash_format_len = snprintf(hash_format, sizeof(hash_format), "$2y$%02" ZEND_LONG_FMT_SPEC "$", cost);
447 if (!(salt = php_password_get_salt(return_value, Z_UL(22), options))) {
448 return;
449 }
450 ZSTR_VAL(salt)[ZSTR_LEN(salt)] = 0;
451
452 hash = zend_string_alloc(ZSTR_LEN(salt) + hash_format_len, 0);
453 sprintf(ZSTR_VAL(hash), "%s%s", hash_format, ZSTR_VAL(salt));
454 ZSTR_VAL(hash)[hash_format_len + ZSTR_LEN(salt)] = 0;
455
456 zend_string_release(salt);
457
458 /* This cast is safe, since both values are defined here in code and cannot overflow */
459 result = php_crypt(ZSTR_VAL(password), (int)ZSTR_LEN(password), ZSTR_VAL(hash), (int)ZSTR_LEN(hash), 1);
460 zend_string_release(hash);
461
462 if (!result) {
463 RETURN_FALSE;
464 }
465
466 if (ZSTR_LEN(result) < 13) {
467 zend_string_free(result);
468 RETURN_FALSE;
469 }
470
471 RETURN_STR(result);
472 }
473 break;
474 #if HAVE_ARGON2LIB
475 case PHP_PASSWORD_ARGON2I:
476 {
477 zval *option_buffer;
478 zend_string *salt, *out, *encoded;
479 size_t time_cost = PHP_PASSWORD_ARGON2_TIME_COST;
480 size_t memory_cost = PHP_PASSWORD_ARGON2_MEMORY_COST;
481 size_t threads = PHP_PASSWORD_ARGON2_THREADS;
482 argon2_type type = Argon2_i;
483 size_t encoded_len;
484 int status = 0;
485
486 if (options && (option_buffer = zend_hash_str_find(options, "memory_cost", sizeof("memory_cost")-1)) != NULL) {
487 memory_cost = zval_get_long(option_buffer);
488 }
489
490 if (memory_cost > ARGON2_MAX_MEMORY || memory_cost < ARGON2_MIN_MEMORY) {
491 php_error_docref(NULL, E_WARNING, "Memory cost is outside of allowed memory range", memory_cost);
492 RETURN_NULL();
493 }
494
495 if (options && (option_buffer = zend_hash_str_find(options, "time_cost", sizeof("time_cost")-1)) != NULL) {
496 time_cost = zval_get_long(option_buffer);
497 }
498
499 if (time_cost > ARGON2_MAX_TIME || time_cost < ARGON2_MIN_TIME) {
500 php_error_docref(NULL, E_WARNING, "Time cost is outside of allowed time range", time_cost);
501 RETURN_NULL();
502 }
503
504 if (options && (option_buffer = zend_hash_str_find(options, "threads", sizeof("threads")-1)) != NULL) {
505 threads = zval_get_long(option_buffer);
506 }
507
508 if (threads > ARGON2_MAX_LANES || threads == 0) {
509 php_error_docref(NULL, E_WARNING, "Invalid number of threads", threads);
510 RETURN_NULL();
511 }
512
513 if (!(salt = php_password_get_salt(return_value, Z_UL(16), options))) {
514 return;
515 }
516
517 out = zend_string_alloc(32, 0);
518 encoded_len = argon2_encodedlen(
519 time_cost,
520 memory_cost,
521 threads,
522 (uint32_t)ZSTR_LEN(salt),
523 ZSTR_LEN(out)
524 #if HAVE_ARGON2ID
525 , type
526 #endif
527 );
528
529 encoded = zend_string_alloc(encoded_len - 1, 0);
530 status = argon2_hash(
531 time_cost,
532 memory_cost,
533 threads,
534 ZSTR_VAL(password),
535 ZSTR_LEN(password),
536 ZSTR_VAL(salt),
537 ZSTR_LEN(salt),
538 ZSTR_VAL(out),
539 ZSTR_LEN(out),
540 ZSTR_VAL(encoded),
541 encoded_len,
542 type,
543 ARGON2_VERSION_NUMBER
544 );
545
546 zend_string_release(out);
547 zend_string_release(salt);
548
549 if (status != ARGON2_OK) {
550 zend_string_free(encoded);
551 php_error_docref(NULL, E_WARNING, "%s", argon2_error_message(status));
552 RETURN_FALSE;
553 }
554
555 ZSTR_VAL(encoded)[ZSTR_LEN(encoded)] = 0;
556 RETURN_STR(encoded);
557 }
558 break;
559 #endif
560 case PHP_PASSWORD_UNKNOWN:
561 default:
562 php_error_docref(NULL, E_WARNING, "Unknown password hashing algorithm: " ZEND_LONG_FMT, algo);
563 RETURN_NULL();
564 }
565 }
566 /* }}} */
567
568 /*
569 * Local variables:
570 * tab-width: 4
571 * c-basic-offset: 4
572 * End:
573 * vim600: sw=4 ts=4 fdm=marker
574 * vim<600: sw=4 ts=4
575 */
576