Skip to content

Commit 86eb627

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 86eb627

4 files changed

Lines changed: 63 additions & 3 deletions

File tree

src/server_config_util_ssh.c

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -495,15 +495,31 @@ _nc_server_config_add_ssh_user_password(const struct ly_ctx *ctx, const char *tr
495495
const char *password, struct lyd_node **config)
496496
{
497497
int ret = 0;
498+
size_t i;
498499
char *hashed_pw = NULL;
499-
const char *salt = "$6$idsizuippipk$";
500+
char salt[3 /* "$6$" */ + 16 /* random chars */ + 1 /* trailing '$' */ + 1 /* NUL */];
500501
struct crypt_data *cdata = NULL;
501-
502-
NC_CHECK_ARG_RET(NULL, ctx, tree_path, password, config, 1);
502+
unsigned char rnd[16];
503+
static const char itoa64[] = "./0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
503504

504505
cdata = calloc(1, sizeof *cdata);
505506
NC_CHECK_ERRMEM_GOTO(!cdata, ret = 1, cleanup);
506507

508+
/* generate a random salt compatible with crypt SHA-512: "$6$<salt>$" */
509+
if (nc_tls_generate_random_bytes_wrap(rnd, sizeof rnd)) {
510+
ret = 1;
511+
goto cleanup;
512+
}
513+
514+
salt[0] = '$';
515+
salt[1] = '6';
516+
salt[2] = '$';
517+
for (i = 0; i < sizeof rnd; ++i) {
518+
salt[3 + i] = itoa64[rnd[i] % 64];
519+
}
520+
salt[3 + sizeof rnd] = '$';
521+
salt[3 + sizeof rnd + 1] = '\0';
522+
507523
hashed_pw = crypt_r(password, salt, cdata);
508524
if (!hashed_pw) {
509525
ERR(NULL, "Hashing password failed (%s).", strerror(errno));

src/session_mbedtls.c

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2478,3 +2478,26 @@ nc_tls_keylog_session_wrap(void *session)
24782478
{
24792479
mbedtls_ssl_set_export_keys_cb(session, nc_tls_keylog_write_line, NULL);
24802480
}
2481+
2482+
int
2483+
nc_tls_generate_random_bytes_wrap(void *buf, size_t num)
2484+
{
2485+
int rc = 0;
2486+
mbedtls_ctr_drbg_context *ctr_drbg = NULL;
2487+
mbedtls_entropy_context *entropy = NULL;
2488+
2489+
rc = nc_tls_rng_new(&ctr_drbg, &entropy);
2490+
if (rc) {
2491+
goto cleanup;
2492+
}
2493+
2494+
rc = mbedtls_ctr_drbg_random(ctr_drbg, buf, num);
2495+
if (rc) {
2496+
nc_mbedtls_strerr(NULL, rc, "Creating random bytes failed");
2497+
goto cleanup;
2498+
}
2499+
2500+
cleanup:
2501+
nc_tls_rng_destroy(ctr_drbg, entropy);
2502+
return rc;
2503+
}

src/session_openssl.c

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@
4040
#include <openssl/bio.h>
4141
#include <openssl/err.h>
4242
#include <openssl/evp.h>
43+
#include <openssl/rand.h>
4344
#include <openssl/ssl.h>
4445
#include <openssl/x509.h>
4546
#include <openssl/x509v3.h>
@@ -1975,3 +1976,14 @@ nc_tls_keylog_session_wrap(void *session)
19751976

19761977
SSL_CTX_set_keylog_callback(ctx, nc_tls_keylog_write_line);
19771978
}
1979+
1980+
int
1981+
nc_tls_generate_random_bytes_wrap(void *buf, size_t num)
1982+
{
1983+
if (RAND_bytes(buf, (int)num) != 1) {
1984+
ERR(NULL, "Generating random bytes failed (%s).", ERR_reason_error_string(ERR_get_error()));
1985+
return 1;
1986+
}
1987+
1988+
return 0;
1989+
}

src/session_wrapper.h

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -757,4 +757,13 @@ time_t nc_tls_get_cert_exp_time_wrap(void *cert);
757757
*/
758758
void nc_tls_keylog_session_wrap(void *session);
759759

760+
/**
761+
* @brief Generate random bytes.
762+
*
763+
* @param[in] buf Buffer to fill with random bytes.
764+
* @param[in] num Number of random bytes to generate. Caller is responsible for ensuring the buffer is large enough.
765+
* @return 0 on success, 1 on error.
766+
*/
767+
int nc_tls_generate_random_bytes_wrap(void *buf, size_t num);
768+
760769
#endif

0 commit comments

Comments
 (0)