|
24 | 24 | #include <string.h> |
25 | 25 |
|
26 | 26 | #include <libyang/libyang.h> |
| 27 | +#include <openssl/rand.h> |
27 | 28 |
|
28 | 29 | #include "compat.h" |
29 | 30 | #include "config.h" |
@@ -495,15 +496,32 @@ _nc_server_config_add_ssh_user_password(const struct ly_ctx *ctx, const char *tr |
495 | 496 | const char *password, struct lyd_node **config) |
496 | 497 | { |
497 | 498 | int ret = 0; |
| 499 | + size_t i; |
498 | 500 | char *hashed_pw = NULL; |
499 | | - const char *salt = "$6$idsizuippipk$"; |
| 501 | + char salt[3 /* "$6$" */ + 16 /* random chars */ + 1 /* trailing '$' */ + 1 /* NUL */]; |
500 | 502 | struct crypt_data *cdata = NULL; |
501 | | - |
502 | | - NC_CHECK_ARG_RET(NULL, ctx, tree_path, password, config, 1); |
| 503 | + unsigned char rnd[16]; |
| 504 | + static const char itoa64[] = "./0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"; |
503 | 505 |
|
504 | 506 | cdata = calloc(1, sizeof *cdata); |
505 | 507 | NC_CHECK_ERRMEM_GOTO(!cdata, ret = 1, cleanup); |
506 | 508 |
|
| 509 | + /* generate a random salt compatible with crypt SHA-512: "$6$<salt>$" */ |
| 510 | + if (RAND_bytes(rnd, sizeof rnd) != 1) { |
| 511 | + ERR(NULL, "Generating random salt failed."); |
| 512 | + ret = 1; |
| 513 | + goto cleanup; |
| 514 | + } |
| 515 | + |
| 516 | + salt[0] = '$'; |
| 517 | + salt[1] = '6'; |
| 518 | + salt[2] = '$'; |
| 519 | + for (i = 0; i < sizeof rnd; ++i) { |
| 520 | + salt[3 + i] = itoa64[rnd[i] % 64]; |
| 521 | + } |
| 522 | + salt[3 + sizeof rnd] = '$'; |
| 523 | + salt[3 + sizeof rnd + 1] = '\0'; |
| 524 | + |
507 | 525 | hashed_pw = crypt_r(password, salt, cdata); |
508 | 526 | if (!hashed_pw) { |
509 | 527 | ERR(NULL, "Hashing password failed (%s).", strerror(errno)); |
|
0 commit comments