Skip to content

Commit ab6f405

Browse files
committed
common/hsm_encryption: use const char * for errors.
Signed-off-by: Rusty Russell <[email protected]>
1 parent bc419b4 commit ab6f405

File tree

4 files changed

+28
-20
lines changed

4 files changed

+28
-20
lines changed

common/hsm_encryption.c

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,22 +7,25 @@
77
#include <unistd.h>
88

99
int hsm_secret_encryption_key_with_exitcode(const char *pass, struct secret *key,
10-
char **err_msg)
10+
const char **err_msg)
1111
{
1212
u8 salt[16] = "c-lightning\0\0\0\0\0";
1313

1414
/* Don't swap the encryption key ! */
1515
if (sodium_mlock(key->data, sizeof(key->data)) != 0) {
16-
*err_msg = "Could not lock hsm_secret encryption key memory.";
16+
if (err_msg)
17+
*err_msg = "Could not lock hsm_secret encryption key memory.";
1718
return EXITCODE_HSM_GENERIC_ERROR;
1819
}
1920

2021
/* Check bounds. */
2122
if (strlen(pass) < crypto_pwhash_argon2id_PASSWD_MIN) {
22-
*err_msg = "Password too short to be able to derive a key from it.";
23+
if (err_msg)
24+
*err_msg = "Password too short to be able to derive a key from it.";
2325
return EXITCODE_HSM_BAD_PASSWORD;
2426
} else if (strlen(pass) > crypto_pwhash_argon2id_PASSWD_MAX) {
25-
*err_msg = "Password too long to be able to derive a key from it.";
27+
if (err_msg)
28+
*err_msg = "Password too long to be able to derive a key from it.";
2629
return EXITCODE_HSM_BAD_PASSWORD;
2730
}
2831

@@ -33,7 +36,8 @@ int hsm_secret_encryption_key_with_exitcode(const char *pass, struct secret *key
3336
crypto_pwhash_argon2id_OPSLIMIT_MODERATE,
3437
crypto_pwhash_argon2id_MEMLIMIT_MODERATE,
3538
crypto_pwhash_ALG_ARGON2ID13) != 0) {
36-
*err_msg = "Could not derive a key from the password.";
39+
if (err_msg)
40+
*err_msg = "Could not derive a key from the password.";
3741
return EXITCODE_HSM_BAD_PASSWORD;
3842
}
3943

@@ -112,7 +116,7 @@ static bool getline_stdin_pass(char **passwd, size_t *passwd_size)
112116
return true;
113117
}
114118

115-
char *read_stdin_pass_with_exit_code(char **reason, int *exit_code)
119+
char *read_stdin_pass_with_exit_code(const char **reason, int *exit_code)
116120
{
117121
struct termios current_term, temp_term;
118122
char *passwd = NULL;

common/hsm_encryption.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ struct encrypted_hsm_secret {
2727
* On success, 0 is returned, on error a value > 0 is returned and it can be used as exit code.
2828
*/
2929
int hsm_secret_encryption_key_with_exitcode(const char *pass, struct secret *key,
30-
char **err_msg);
30+
const char **err_msg);
3131

3232
/** Encrypt the hsm_secret using a previously derived encryption key.
3333
* @encryption_key: the key derived from the passphrase.
@@ -62,7 +62,7 @@ void discard_key(struct secret *key TAKES);
6262
*
6363
* Caller must free the string as it does tal-reallocate getline's output.
6464
*/
65-
char *read_stdin_pass_with_exit_code(char **reason, int *exit_code);
65+
char *read_stdin_pass_with_exit_code(const char **reason, int *exit_code);
6666

6767
/** Returns -1 on error (and sets errno), 0 if not encrypted, 1 if it is */
6868
int is_hsm_secret_encrypted(const char *path);

lightningd/options.c

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -637,7 +637,8 @@ static void prompt(struct lightningd *ld, const char *str)
637637
*/
638638
static char *opt_set_hsm_password(struct lightningd *ld)
639639
{
640-
char *passwd, *passwd_confirmation, *err_msg;
640+
char *passwd, *passwd_confirmation;
641+
const char *err_msg;
641642
int is_encrypted;
642643

643644
is_encrypted = is_hsm_secret_encrypted("hsm_secret");
@@ -657,13 +658,13 @@ static char *opt_set_hsm_password(struct lightningd *ld)
657658

658659
passwd = read_stdin_pass_with_exit_code(&err_msg, &opt_exitcode);
659660
if (!passwd)
660-
return err_msg;
661+
return cast_const(char *, err_msg);
661662
if (!is_encrypted) {
662663
prompt(ld, "Confirm hsm_secret password:");
663664
fflush(stdout);
664665
passwd_confirmation = read_stdin_pass_with_exit_code(&err_msg, &opt_exitcode);
665666
if (!passwd_confirmation)
666-
return err_msg;
667+
return cast_const(char *, err_msg);
667668

668669
if (!streq(passwd, passwd_confirmation)) {
669670
opt_exitcode = EXITCODE_HSM_BAD_PASSWORD;
@@ -677,7 +678,7 @@ static char *opt_set_hsm_password(struct lightningd *ld)
677678

678679
opt_exitcode = hsm_secret_encryption_key_with_exitcode(passwd, ld->config.keypass, &err_msg);
679680
if (opt_exitcode > 0)
680-
return err_msg;
681+
return cast_const(char *, err_msg);
681682

682683
ld->encrypted_hsm = true;
683684
free(passwd);

tools/hsmtool.c

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@ static void get_encrypted_hsm_secret(struct secret *hsm_secret,
105105
{
106106
struct secret key;
107107
struct encrypted_hsm_secret encrypted_secret;
108-
char *err;
108+
const char *err;
109109
int exit_code;
110110

111111
grab_hsm_file(hsm_secret_path,
@@ -184,7 +184,8 @@ static void get_hsm_secret(struct secret *hsm_secret,
184184
/* This checks the file existence, too. */
185185
if (hsm_secret_is_encrypted(hsm_secret_path)) {
186186
int exit_code;
187-
char *err, *passwd;
187+
char *passwd;
188+
const char *err;
188189

189190
printf("Enter hsm_secret password:\n");
190191
fflush(stdout);
@@ -202,8 +203,8 @@ static int decrypt_hsm(const char *hsm_secret_path)
202203
{
203204
int fd;
204205
struct secret hsm_secret;
205-
char *passwd, *err;
206-
const char *dir, *backup;
206+
char *passwd;
207+
const char *dir, *backup, *err;
207208
int exit_code = 0;
208209
/* This checks the file existence, too. */
209210
if (!hsm_secret_is_encrypted(hsm_secret_path))
@@ -293,8 +294,8 @@ static int encrypt_hsm(const char *hsm_secret_path)
293294
int fd;
294295
struct secret key, hsm_secret;
295296
struct encrypted_hsm_secret encrypted_hsm_secret;
296-
char *passwd, *passwd_confirmation, *err;
297-
const char *dir, *backup;
297+
char *passwd, *passwd_confirmation;
298+
const char *err, *dir, *backup;
298299
int exit_code = 0;
299300

300301
/* This checks the file existence, too. */
@@ -519,7 +520,8 @@ static void read_mnemonic(char *mnemonic) {
519520
static int generate_hsm(const char *hsm_secret_path)
520521
{
521522
char mnemonic[BIP39_WORDLIST_LEN];
522-
char *passphrase, *err;
523+
char *passphrase;
524+
const char *err;
523525
int exit_code = 0;
524526

525527
read_mnemonic(mnemonic);
@@ -634,7 +636,8 @@ static int check_hsm(const char *hsm_secret_path)
634636
u8 bip32_seed[BIP39_SEED_LEN_512];
635637
size_t bip32_seed_len;
636638
int exit_code;
637-
char *passphrase, *err;
639+
char *passphrase;
640+
const char *err;
638641

639642
get_hsm_secret(&hsm_secret, hsm_secret_path);
640643

0 commit comments

Comments
 (0)