Skip to content

Commit 2045eb3

Browse files
committed
config util UPDATE use random salt for generating passwords
Using constant public salt is equivalent to using none, so generate one each time
1 parent 8d3e2ba commit 2045eb3

File tree

1 file changed

+21
-3
lines changed

1 file changed

+21
-3
lines changed

src/server_config_util_ssh.c

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
#include <string.h>
2525

2626
#include <libyang/libyang.h>
27+
#include <openssl/rand.h>
2728

2829
#include "compat.h"
2930
#include "config.h"
@@ -495,15 +496,32 @@ _nc_server_config_add_ssh_user_password(const struct ly_ctx *ctx, const char *tr
495496
const char *password, struct lyd_node **config)
496497
{
497498
int ret = 0;
499+
size_t i;
498500
char *hashed_pw = NULL;
499-
const char *salt = "$6$idsizuippipk$";
501+
char salt[3 /* "$6$" */ + 16 /* random chars */ + 1 /* trailing '$' */ + 1 /* NUL */];
500502
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";
503505

504506
cdata = calloc(1, sizeof *cdata);
505507
NC_CHECK_ERRMEM_GOTO(!cdata, ret = 1, cleanup);
506508

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+
507525
hashed_pw = crypt_r(password, salt, cdata);
508526
if (!hashed_pw) {
509527
ERR(NULL, "Hashing password failed (%s).", strerror(errno));

0 commit comments

Comments
 (0)