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: Anthony Ferrara <ircmaxell@php.net> |
14 | Charles R. Portwood II <charlesportwoodii@erianna.com> |
15 +----------------------------------------------------------------------+
16 */
17
18 #include <stdlib.h>
19
20 #include "php.h"
21
22 #include "fcntl.h"
23 #include "php_password.h"
24 #include "php_crypt.h"
25 #include "base64.h"
26 #include "zend_interfaces.h"
27 #include "info.h"
28 #include "ext/random/php_random_csprng.h"
29 #include "password_arginfo.h"
30 #ifdef HAVE_ARGON2LIB
31 #include "argon2.h"
32 #endif
33
34 #ifdef PHP_WIN32
35 #include "win32/winutil.h"
36 #endif
37
38 static zend_array php_password_algos;
39
php_password_algo_register(const char * ident,const php_password_algo * algo)40 int php_password_algo_register(const char *ident, const php_password_algo *algo) {
41 zend_string *key = zend_string_init_interned(ident, strlen(ident), 1);
42 return zend_hash_add_ptr(&php_password_algos, key, (void *) algo) ? SUCCESS : FAILURE;
43 }
44
php_password_algo_unregister(const char * ident)45 void php_password_algo_unregister(const char *ident) {
46 zend_hash_str_del(&php_password_algos, ident, strlen(ident));
47 }
48
php_password_salt_to64(const char * str,const size_t str_len,const size_t out_len,char * ret)49 static int php_password_salt_to64(const char *str, const size_t str_len, const size_t out_len, char *ret) /* {{{ */
50 {
51 size_t pos = 0;
52 zend_string *buffer;
53 if ((int) str_len < 0) {
54 return FAILURE;
55 }
56 buffer = php_base64_encode((unsigned char*) str, str_len);
57 if (ZSTR_LEN(buffer) < out_len) {
58 /* Too short of an encoded string generated */
59 zend_string_release_ex(buffer, 0);
60 return FAILURE;
61 }
62 for (pos = 0; pos < out_len; pos++) {
63 if (ZSTR_VAL(buffer)[pos] == '+') {
64 ret[pos] = '.';
65 } else if (ZSTR_VAL(buffer)[pos] == '=') {
66 zend_string_free(buffer);
67 return FAILURE;
68 } else {
69 ret[pos] = ZSTR_VAL(buffer)[pos];
70 }
71 }
72 zend_string_free(buffer);
73 return SUCCESS;
74 }
75 /* }}} */
76
php_password_make_salt(size_t length)77 static zend_string* php_password_make_salt(size_t length) /* {{{ */
78 {
79 zend_string *ret, *buffer;
80
81 if (length > (INT_MAX / 3)) {
82 zend_value_error("Length is too large to safely generate");
83 return NULL;
84 }
85
86 buffer = zend_string_alloc(length * 3 / 4 + 1, 0);
87 if (FAILURE == php_random_bytes_throw(ZSTR_VAL(buffer), ZSTR_LEN(buffer))) {
88 zend_value_error("Unable to generate salt");
89 zend_string_release_ex(buffer, 0);
90 return NULL;
91 }
92
93 ret = zend_string_alloc(length, 0);
94 if (php_password_salt_to64(ZSTR_VAL(buffer), ZSTR_LEN(buffer), length, ZSTR_VAL(ret)) == FAILURE) {
95 zend_value_error("Generated salt too short");
96 zend_string_release_ex(buffer, 0);
97 zend_string_release_ex(ret, 0);
98 return NULL;
99 }
100 zend_string_release_ex(buffer, 0);
101 ZSTR_VAL(ret)[length] = 0;
102 return ret;
103 }
104 /* }}} */
105
php_password_get_salt(zval * unused_,size_t required_salt_len,HashTable * options)106 static zend_string* php_password_get_salt(zval *unused_, size_t required_salt_len, HashTable *options) {
107 if (options && zend_hash_str_exists(options, "salt", sizeof("salt") - 1)) {
108 php_error_docref(NULL, E_WARNING, "The \"salt\" option has been ignored, since providing a custom salt is no longer supported");
109 }
110
111 return php_password_make_salt(required_salt_len);
112 }
113
114 /* bcrypt implementation */
115
php_password_bcrypt_valid(const zend_string * hash)116 static bool php_password_bcrypt_valid(const zend_string *hash) {
117 const char *h = ZSTR_VAL(hash);
118 return (ZSTR_LEN(hash) == 60) &&
119 (h[0] == '$') && (h[1] == '2') && (h[2] == 'y');
120 }
121
php_password_bcrypt_get_info(zval * return_value,const zend_string * hash)122 static int php_password_bcrypt_get_info(zval *return_value, const zend_string *hash) {
123 zend_long cost = PHP_PASSWORD_BCRYPT_COST;
124
125 if (!php_password_bcrypt_valid(hash)) {
126 /* Should never get called this way. */
127 return FAILURE;
128 }
129
130 sscanf(ZSTR_VAL(hash), "$2y$" ZEND_LONG_FMT "$", &cost);
131 add_assoc_long(return_value, "cost", cost);
132
133 return SUCCESS;
134 }
135
php_password_bcrypt_needs_rehash(const zend_string * hash,zend_array * options)136 static bool php_password_bcrypt_needs_rehash(const zend_string *hash, zend_array *options) {
137 zval *znew_cost;
138 zend_long old_cost = PHP_PASSWORD_BCRYPT_COST;
139 zend_long new_cost = PHP_PASSWORD_BCRYPT_COST;
140
141 if (!php_password_bcrypt_valid(hash)) {
142 /* Should never get called this way. */
143 return 1;
144 }
145
146 sscanf(ZSTR_VAL(hash), "$2y$" ZEND_LONG_FMT "$", &old_cost);
147 if (options && (znew_cost = zend_hash_str_find(options, "cost", sizeof("cost")-1)) != NULL) {
148 new_cost = zval_get_long(znew_cost);
149 }
150
151 return old_cost != new_cost;
152 }
153
php_password_bcrypt_verify(const zend_string * password,const zend_string * hash)154 static bool php_password_bcrypt_verify(const zend_string *password, const zend_string *hash) {
155 int status = 0;
156 zend_string *ret = php_crypt(ZSTR_VAL(password), (int)ZSTR_LEN(password), ZSTR_VAL(hash), (int)ZSTR_LEN(hash), 1);
157
158 if (!ret) {
159 return 0;
160 }
161
162 if (ZSTR_LEN(hash) < 13) {
163 zend_string_free(ret);
164 return 0;
165 }
166
167 /* We're using this method instead of == in order to provide
168 * resistance towards timing attacks. This is a constant time
169 * equality check that will always check every byte of both
170 * values. */
171 status = php_safe_bcmp(ret, hash);
172
173 zend_string_free(ret);
174 return status == 0;
175 }
176
php_password_bcrypt_hash(const zend_string * password,zend_array * options)177 static zend_string* php_password_bcrypt_hash(const zend_string *password, zend_array *options) {
178 char hash_format[10];
179 size_t hash_format_len;
180 zend_string *result, *hash, *salt;
181 zval *zcost;
182 zend_long cost = PHP_PASSWORD_BCRYPT_COST;
183
184 if (options && (zcost = zend_hash_str_find(options, "cost", sizeof("cost")-1)) != NULL) {
185 cost = zval_get_long(zcost);
186 }
187
188 if (cost < 4 || cost > 31) {
189 zend_value_error("Invalid bcrypt cost parameter specified: " ZEND_LONG_FMT, cost);
190 return NULL;
191 }
192
193 hash_format_len = snprintf(hash_format, sizeof(hash_format), "$2y$%02" ZEND_LONG_FMT_SPEC "$", cost);
194 if (!(salt = php_password_get_salt(NULL, Z_UL(22), options))) {
195 return NULL;
196 }
197 ZSTR_VAL(salt)[ZSTR_LEN(salt)] = 0;
198
199 hash = zend_string_alloc(ZSTR_LEN(salt) + hash_format_len, 0);
200 sprintf(ZSTR_VAL(hash), "%s%s", hash_format, ZSTR_VAL(salt));
201 ZSTR_VAL(hash)[hash_format_len + ZSTR_LEN(salt)] = 0;
202
203 zend_string_release_ex(salt, 0);
204
205 /* This cast is safe, since both values are defined here in code and cannot overflow */
206 result = php_crypt(ZSTR_VAL(password), (int)ZSTR_LEN(password), ZSTR_VAL(hash), (int)ZSTR_LEN(hash), 1);
207 zend_string_release_ex(hash, 0);
208
209 if (!result) {
210 return NULL;
211 }
212
213 if (ZSTR_LEN(result) < 13) {
214 zend_string_free(result);
215 return NULL;
216 }
217
218 return result;
219 }
220
221 const php_password_algo php_password_algo_bcrypt = {
222 "bcrypt",
223 php_password_bcrypt_hash,
224 php_password_bcrypt_verify,
225 php_password_bcrypt_needs_rehash,
226 php_password_bcrypt_get_info,
227 php_password_bcrypt_valid,
228 };
229
230
231 #ifdef HAVE_ARGON2LIB
232 /* argon2i/argon2id shared implementation */
233
extract_argon2_parameters(const zend_string * hash,zend_long * v,zend_long * memory_cost,zend_long * time_cost,zend_long * threads)234 static int extract_argon2_parameters(const zend_string *hash,
235 zend_long *v, zend_long *memory_cost,
236 zend_long *time_cost, zend_long *threads) /* {{{ */
237 {
238 const char *p = ZSTR_VAL(hash);
239 if (!hash || (ZSTR_LEN(hash) < sizeof("$argon2id$"))) {
240 return FAILURE;
241 }
242 if (!memcmp(p, "$argon2i$", sizeof("$argon2i$") - 1)) {
243 p += sizeof("$argon2i$") - 1;
244 } else if (!memcmp(p, "$argon2id$", sizeof("$argon2id$") - 1)) {
245 p += sizeof("$argon2id$") - 1;
246 } else {
247 return FAILURE;
248 }
249
250 sscanf(p, "v=" ZEND_LONG_FMT "$m=" ZEND_LONG_FMT ",t=" ZEND_LONG_FMT ",p=" ZEND_LONG_FMT,
251 v, memory_cost, time_cost, threads);
252
253 return SUCCESS;
254 }
255 /* }}} */
256
php_password_argon2_get_info(zval * return_value,const zend_string * hash)257 static int php_password_argon2_get_info(zval *return_value, const zend_string *hash) {
258 zend_long v = 0;
259 zend_long memory_cost = PHP_PASSWORD_ARGON2_MEMORY_COST;
260 zend_long time_cost = PHP_PASSWORD_ARGON2_TIME_COST;
261 zend_long threads = PHP_PASSWORD_ARGON2_THREADS;
262
263 extract_argon2_parameters(hash, &v, &memory_cost, &time_cost, &threads);
264
265 add_assoc_long(return_value, "memory_cost", memory_cost);
266 add_assoc_long(return_value, "time_cost", time_cost);
267 add_assoc_long(return_value, "threads", threads);
268
269 return SUCCESS;
270 }
271
php_password_argon2_needs_rehash(const zend_string * hash,zend_array * options)272 static bool php_password_argon2_needs_rehash(const zend_string *hash, zend_array *options) {
273 zend_long v = 0;
274 zend_long new_memory_cost = PHP_PASSWORD_ARGON2_MEMORY_COST, memory_cost = 0;
275 zend_long new_time_cost = PHP_PASSWORD_ARGON2_TIME_COST, time_cost = 0;
276 zend_long new_threads = PHP_PASSWORD_ARGON2_THREADS, threads = 0;
277 zval *option_buffer;
278
279 if (options && (option_buffer = zend_hash_str_find(options, "memory_cost", sizeof("memory_cost")-1)) != NULL) {
280 new_memory_cost = zval_get_long(option_buffer);
281 }
282
283 if (options && (option_buffer = zend_hash_str_find(options, "time_cost", sizeof("time_cost")-1)) != NULL) {
284 new_time_cost = zval_get_long(option_buffer);
285 }
286
287 if (options && (option_buffer = zend_hash_str_find(options, "threads", sizeof("threads")-1)) != NULL) {
288 new_threads = zval_get_long(option_buffer);
289 }
290
291 extract_argon2_parameters(hash, &v, &memory_cost, &time_cost, &threads);
292
293 return (new_time_cost != time_cost) ||
294 (new_memory_cost != memory_cost) ||
295 (new_threads != threads);
296 }
297
php_password_argon2_hash(const zend_string * password,zend_array * options,argon2_type type)298 static zend_string *php_password_argon2_hash(const zend_string *password, zend_array *options, argon2_type type) {
299 zval *option_buffer;
300 zend_string *salt, *out, *encoded;
301 size_t time_cost = PHP_PASSWORD_ARGON2_TIME_COST;
302 size_t memory_cost = PHP_PASSWORD_ARGON2_MEMORY_COST;
303 size_t threads = PHP_PASSWORD_ARGON2_THREADS;
304 size_t encoded_len;
305 int status = 0;
306
307 if (options && (option_buffer = zend_hash_str_find(options, "memory_cost", sizeof("memory_cost")-1)) != NULL) {
308 memory_cost = zval_get_long(option_buffer);
309 }
310
311 if (memory_cost > ARGON2_MAX_MEMORY || memory_cost < ARGON2_MIN_MEMORY) {
312 zend_value_error("Memory cost is outside of allowed memory range");
313 return NULL;
314 }
315
316 if (options && (option_buffer = zend_hash_str_find(options, "time_cost", sizeof("time_cost")-1)) != NULL) {
317 time_cost = zval_get_long(option_buffer);
318 }
319
320 if (time_cost > ARGON2_MAX_TIME || time_cost < ARGON2_MIN_TIME) {
321 zend_value_error("Time cost is outside of allowed time range");
322 return NULL;
323 }
324
325 if (options && (option_buffer = zend_hash_str_find(options, "threads", sizeof("threads")-1)) != NULL) {
326 threads = zval_get_long(option_buffer);
327 }
328
329 if (threads > ARGON2_MAX_LANES || threads == 0) {
330 zend_value_error("Invalid number of threads");
331 return NULL;
332 }
333
334 if (!(salt = php_password_get_salt(NULL, Z_UL(16), options))) {
335 return NULL;
336 }
337
338 out = zend_string_alloc(32, 0);
339 encoded_len = argon2_encodedlen(
340 time_cost,
341 memory_cost,
342 threads,
343 (uint32_t)ZSTR_LEN(salt),
344 ZSTR_LEN(out),
345 type
346 );
347
348 encoded = zend_string_alloc(encoded_len - 1, 0);
349 status = argon2_hash(
350 time_cost,
351 memory_cost,
352 threads,
353 ZSTR_VAL(password),
354 ZSTR_LEN(password),
355 ZSTR_VAL(salt),
356 ZSTR_LEN(salt),
357 ZSTR_VAL(out),
358 ZSTR_LEN(out),
359 ZSTR_VAL(encoded),
360 encoded_len,
361 type,
362 ARGON2_VERSION_NUMBER
363 );
364
365 zend_string_release_ex(out, 0);
366 zend_string_release_ex(salt, 0);
367
368 if (status != ARGON2_OK) {
369 zend_string_efree(encoded);
370 zend_value_error("%s", argon2_error_message(status));
371 return NULL;
372 }
373
374 ZSTR_VAL(encoded)[ZSTR_LEN(encoded)] = 0;
375 return encoded;
376 }
377
378 /* argon2i specific methods */
379
php_password_argon2i_verify(const zend_string * password,const zend_string * hash)380 static bool php_password_argon2i_verify(const zend_string *password, const zend_string *hash) {
381 return ARGON2_OK == argon2_verify(ZSTR_VAL(hash), ZSTR_VAL(password), ZSTR_LEN(password), Argon2_i);
382 }
383
php_password_argon2i_hash(const zend_string * password,zend_array * options)384 static zend_string *php_password_argon2i_hash(const zend_string *password, zend_array *options) {
385 return php_password_argon2_hash(password, options, Argon2_i);
386 }
387
388 const php_password_algo php_password_algo_argon2i = {
389 "argon2i",
390 php_password_argon2i_hash,
391 php_password_argon2i_verify,
392 php_password_argon2_needs_rehash,
393 php_password_argon2_get_info,
394 NULL,
395 };
396
397 /* argon2id specific methods */
398
php_password_argon2id_verify(const zend_string * password,const zend_string * hash)399 static bool php_password_argon2id_verify(const zend_string *password, const zend_string *hash) {
400 return ARGON2_OK == argon2_verify(ZSTR_VAL(hash), ZSTR_VAL(password), ZSTR_LEN(password), Argon2_id);
401 }
402
php_password_argon2id_hash(const zend_string * password,zend_array * options)403 static zend_string *php_password_argon2id_hash(const zend_string *password, zend_array *options) {
404 return php_password_argon2_hash(password, options, Argon2_id);
405 }
406
407 const php_password_algo php_password_algo_argon2id = {
408 "argon2id",
409 php_password_argon2id_hash,
410 php_password_argon2id_verify,
411 php_password_argon2_needs_rehash,
412 php_password_argon2_get_info,
413 NULL,
414 };
415 #endif
416
PHP_MINIT_FUNCTION(password)417 PHP_MINIT_FUNCTION(password) /* {{{ */
418 {
419 zend_hash_init(&php_password_algos, 4, NULL, ZVAL_PTR_DTOR, 1);
420
421 register_password_symbols(module_number);
422
423 if (FAILURE == php_password_algo_register("2y", &php_password_algo_bcrypt)) {
424 return FAILURE;
425 }
426
427 #ifdef HAVE_ARGON2LIB
428 if (FAILURE == php_password_algo_register("argon2i", &php_password_algo_argon2i)) {
429 return FAILURE;
430 }
431 if (FAILURE == php_password_algo_register("argon2id", &php_password_algo_argon2id)) {
432 return FAILURE;
433 }
434 #endif
435
436 return SUCCESS;
437 }
438 /* }}} */
439
PHP_MSHUTDOWN_FUNCTION(password)440 PHP_MSHUTDOWN_FUNCTION(password) /* {{{ */
441 {
442 #ifdef ZTS
443 if (!tsrm_is_main_thread()) {
444 return SUCCESS;
445 }
446 #endif
447 zend_hash_destroy(&php_password_algos);
448 return SUCCESS;
449 }
450 /* }}} */
451
php_password_algo_default(void)452 const php_password_algo* php_password_algo_default(void) {
453 return &php_password_algo_bcrypt;
454 }
455
php_password_algo_find(const zend_string * ident)456 const php_password_algo* php_password_algo_find(const zend_string *ident) {
457 zval *tmp;
458
459 if (!ident) {
460 return NULL;
461 }
462
463 tmp = zend_hash_find(&php_password_algos, (zend_string*)ident);
464 if (!tmp || (Z_TYPE_P(tmp) != IS_PTR)) {
465 return NULL;
466 }
467
468 return Z_PTR_P(tmp);
469 }
470
php_password_algo_find_zval(zend_string * arg_str,zend_long arg_long,bool arg_is_null)471 static const php_password_algo* php_password_algo_find_zval(zend_string *arg_str, zend_long arg_long, bool arg_is_null) {
472 if (arg_is_null) {
473 return php_password_algo_default();
474 }
475
476 if (arg_str) {
477 return php_password_algo_find(arg_str);
478 }
479
480 switch (arg_long) {
481 case 0: return php_password_algo_default();
482 case 1: return &php_password_algo_bcrypt;
483 #ifdef HAVE_ARGON2LIB
484 case 2: return &php_password_algo_argon2i;
485 case 3: return &php_password_algo_argon2id;
486 #else
487 case 2:
488 {
489 zend_string *n = ZSTR_INIT_LITERAL("argon2i", 0);
490 const php_password_algo* ret = php_password_algo_find(n);
491 zend_string_release(n);
492 return ret;
493 }
494 case 3:
495 {
496 zend_string *n = ZSTR_INIT_LITERAL("argon2id", 0);
497 const php_password_algo* ret = php_password_algo_find(n);
498 zend_string_release(n);
499 return ret;
500 }
501 #endif
502 }
503
504 return NULL;
505 }
506
php_password_algo_extract_ident(const zend_string * hash)507 zend_string *php_password_algo_extract_ident(const zend_string* hash) {
508 const char *ident, *ident_end;
509
510 if (!hash || ZSTR_LEN(hash) < 3) {
511 /* Minimum prefix: "$x$" */
512 return NULL;
513 }
514
515 ident = ZSTR_VAL(hash) + 1;
516 ident_end = strchr(ident, '$');
517 if (!ident_end) {
518 /* No terminating '$' */
519 return NULL;
520 }
521
522 return zend_string_init(ident, ident_end - ident, 0);
523 }
524
php_password_algo_identify_ex(const zend_string * hash,const php_password_algo * default_algo)525 const php_password_algo* php_password_algo_identify_ex(const zend_string* hash, const php_password_algo *default_algo) {
526 const php_password_algo *algo;
527 zend_string *ident = php_password_algo_extract_ident(hash);
528
529 if (!ident) {
530 return default_algo;
531 }
532
533 algo = php_password_algo_find(ident);
534 zend_string_release(ident);
535 return (!algo || (algo->valid && !algo->valid(hash))) ? default_algo : algo;
536 }
537
538 /* {{{ Retrieves information about a given hash */
PHP_FUNCTION(password_get_info)539 PHP_FUNCTION(password_get_info)
540 {
541 const php_password_algo *algo;
542 zend_string *hash, *ident;
543 zval options;
544
545 ZEND_PARSE_PARAMETERS_START(1, 1)
546 Z_PARAM_STR(hash)
547 ZEND_PARSE_PARAMETERS_END();
548
549 array_init(return_value);
550 array_init(&options);
551
552 ident = php_password_algo_extract_ident(hash);
553 algo = php_password_algo_find(ident);
554 if (!algo || (algo->valid && !algo->valid(hash))) {
555 if (ident) {
556 zend_string_release(ident);
557 }
558 add_assoc_null(return_value, "algo");
559 add_assoc_string(return_value, "algoName", "unknown");
560 add_assoc_zval(return_value, "options", &options);
561 return;
562 }
563
564 add_assoc_str(return_value, "algo", php_password_algo_extract_ident(hash));
565 zend_string_release(ident);
566
567 add_assoc_string(return_value, "algoName", algo->name);
568 if (algo->get_info) {
569 algo->get_info(&options, hash);
570 }
571 add_assoc_zval(return_value, "options", &options);
572 }
573 /** }}} */
574
575 /* {{{ Determines if a given hash requires re-hashing based upon parameters */
PHP_FUNCTION(password_needs_rehash)576 PHP_FUNCTION(password_needs_rehash)
577 {
578 const php_password_algo *old_algo, *new_algo;
579 zend_string *hash;
580 zend_string *new_algo_str;
581 zend_long new_algo_long = 0;
582 bool new_algo_is_null;
583 zend_array *options = NULL;
584
585 ZEND_PARSE_PARAMETERS_START(2, 3)
586 Z_PARAM_STR(hash)
587 Z_PARAM_STR_OR_LONG_OR_NULL(new_algo_str, new_algo_long, new_algo_is_null)
588 Z_PARAM_OPTIONAL
589 Z_PARAM_ARRAY_HT(options)
590 ZEND_PARSE_PARAMETERS_END();
591
592 new_algo = php_password_algo_find_zval(new_algo_str, new_algo_long, new_algo_is_null);
593 if (!new_algo) {
594 /* Unknown new algorithm, never prompt to rehash. */
595 RETURN_FALSE;
596 }
597
598 old_algo = php_password_algo_identify_ex(hash, NULL);
599 if (old_algo != new_algo) {
600 /* Different algorithm preferred, always rehash. */
601 RETURN_TRUE;
602 }
603
604 RETURN_BOOL(old_algo->needs_rehash(hash, options));
605 }
606 /* }}} */
607
608 /* {{{ Verify a hash created using crypt() or password_hash() */
PHP_FUNCTION(password_verify)609 PHP_FUNCTION(password_verify)
610 {
611 zend_string *password, *hash;
612 const php_password_algo *algo;
613
614 ZEND_PARSE_PARAMETERS_START(2, 2)
615 Z_PARAM_STR(password)
616 Z_PARAM_STR(hash)
617 ZEND_PARSE_PARAMETERS_END();
618
619 algo = php_password_algo_identify(hash);
620 RETURN_BOOL(algo && (!algo->verify || algo->verify(password, hash)));
621 }
622 /* }}} */
623
624 /* {{{ Hash a password */
PHP_FUNCTION(password_hash)625 PHP_FUNCTION(password_hash)
626 {
627 zend_string *password, *digest = NULL;
628 zend_string *algo_str;
629 zend_long algo_long = 0;
630 bool algo_is_null;
631 const php_password_algo *algo;
632 zend_array *options = NULL;
633
634 ZEND_PARSE_PARAMETERS_START(2, 3)
635 Z_PARAM_STR(password)
636 Z_PARAM_STR_OR_LONG_OR_NULL(algo_str, algo_long, algo_is_null)
637 Z_PARAM_OPTIONAL
638 Z_PARAM_ARRAY_HT(options)
639 ZEND_PARSE_PARAMETERS_END();
640
641 algo = php_password_algo_find_zval(algo_str, algo_long, algo_is_null);
642 if (!algo) {
643 zend_argument_value_error(2, "must be a valid password hashing algorithm");
644 RETURN_THROWS();
645 }
646
647 digest = algo->hash(password, options);
648 if (!digest) {
649 if (!EG(exception)) {
650 zend_throw_error(NULL, "Password hashing failed for unknown reason");
651 }
652 RETURN_THROWS();
653 }
654
655 RETURN_NEW_STR(digest);
656 }
657 /* }}} */
658
659 /* {{{ */
PHP_FUNCTION(password_algos)660 PHP_FUNCTION(password_algos) {
661 zend_string *algo;
662
663 ZEND_PARSE_PARAMETERS_NONE();
664
665 array_init(return_value);
666 ZEND_HASH_MAP_FOREACH_STR_KEY(&php_password_algos, algo) {
667 add_next_index_str(return_value, zend_string_copy(algo));
668 } ZEND_HASH_FOREACH_END();
669 }
670 /* }}} */
671