Skip to content

Commit 5d6f286

Browse files
committed
removing some of the encrypted-hsm specific logic
1 parent 1ae8dc0 commit 5d6f286

File tree

6 files changed

+156
-268
lines changed

6 files changed

+156
-268
lines changed

hsmd/hsmd.c

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -422,26 +422,17 @@ static void load_hsm(const char *passphrase)
422422
struct hsm_secret *hsms;
423423
enum hsm_secret_error err;
424424

425-
status_debug("HSM: Starting load_hsm with passphrase=%s", passphrase ? "provided" : "none");
426-
427-
/* Initialize wally tal context for libwally operations */
428-
429-
status_debug("HSM: Initialized wally tal context for load_hsm");
430-
431425
/* Read the hsm_secret file */
432426
hsm_secret_contents = grab_file(tmpctx, "hsm_secret");
433427
if (!hsm_secret_contents) {
434428
status_failed(STATUS_FAIL_INTERNAL_ERROR,
435429
"Could not read hsm_secret: %s", strerror(errno));
436430
}
437-
status_debug("HSM: Successfully read hsm_secret file, size=%zu", tal_bytelen(hsm_secret_contents));
438431

439432
/* Remove the NUL terminator that grab_file adds */
440433
tal_resize(&hsm_secret_contents, tal_bytelen(hsm_secret_contents) - 1);
441-
status_debug("HSM: Removed NUL terminator, new size=%zu", tal_bytelen(hsm_secret_contents));
442434

443435
/* Extract the secret using the new hsm_secret module */
444-
status_debug("HSM: Calling extract_hsm_secret");
445436
tal_wally_start();
446437
hsms = extract_hsm_secret(tmpctx, hsm_secret_contents,
447438
tal_bytelen(hsm_secret_contents),
@@ -451,11 +442,9 @@ static void load_hsm(const char *passphrase)
451442
status_failed(STATUS_FAIL_INTERNAL_ERROR,
452443
"Failed to load hsm_secret: %s", hsm_secret_error_str(err));
453444
}
454-
status_debug("HSM: Successfully extracted hsm_secret");
455445

456446
/* Copy the extracted secret to our global hsm_secret */
457447
memcpy(&hsm_secret, &hsms->secret, sizeof(hsm_secret));
458-
status_debug("HSM: Copied secret to global hsm_secret");
459448
}
460449

461450
/*~ We have a pre-init call in developer mode, to set dev flags */

lightningd/hsm_control.c

Lines changed: 5 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -82,38 +82,13 @@ bool hsm_capable(struct lightningd *ld, u32 msgtype)
8282
return hsm_is_capable(ld->hsm_capabilities, msgtype);
8383
}
8484

85-
/* Read hsm passphrase if needed for mnemonic-based hsm_secret */
86-
static const char *read_hsm_passphrase_if_needed(struct lightningd *ld)
87-
{
88-
if (!ld->hsm_passphrase_required)
89-
return NULL;
90-
91-
log_info(ld->log, "The hsm_secret uses a mnemonic with a passphrase. In order to "
92-
"derive the seed and start the node you must provide the passphrase.");
93-
log_info(ld->log, "Enter hsm_secret passphrase: ");
94-
95-
enum hsm_secret_error err;
96-
const char *passphrase = read_stdin_pass(tmpctx, &err);
97-
if (err != HSM_SECRET_OK) {
98-
fatal("Failed to read passphrase: %s", hsm_secret_error_str(err));
99-
}
100-
101-
return passphrase;
102-
}
103-
10485
struct ext_key *hsm_init(struct lightningd *ld)
10586
{
10687
u8 *msg;
10788
int fds[2];
10889
struct ext_key *bip32_base;
10990
u32 hsm_version;
11091
struct pubkey unused;
111-
const char *hsm_passphrase = NULL;
112-
113-
/* Read passphrase if needed for mnemonic-based hsm_secret */
114-
if (ld->hsm_passphrase_required) {
115-
hsm_passphrase = read_hsm_passphrase_if_needed(ld);
116-
}
11792

11893
/* We actually send requests synchronously: only status is async. */
11994
if (socketpair(AF_LOCAL, SOCK_STREAM, 0, fds) != 0)
@@ -126,15 +101,6 @@ struct ext_key *hsm_init(struct lightningd *ld)
126101
if (!ld->hsm)
127102
err(EXITCODE_HSM_GENERIC_ERROR, "Could not subd hsm");
128103

129-
/* If hsm_secret is encrypted and the --encrypted-hsm startup option is
130-
* not passed, don't let hsmd use the first 32 bytes of the cypher as the
131-
* actual secret. */
132-
if (!ld->config.keypass) {
133-
if (is_legacy_hsm_secret_encrypted("hsm_secret") == 1)
134-
errx(EXITCODE_HSM_ERROR_IS_ENCRYPT, "hsm_secret is encrypted, you need to pass the "
135-
"--encrypted-hsm startup option.");
136-
}
137-
138104
ld->hsm_fd = fds[0];
139105

140106
if (ld->developer) {
@@ -155,15 +121,15 @@ struct ext_key *hsm_init(struct lightningd *ld)
155121

156122
/* Create TLV for passphrase if needed */
157123
struct tlv_hsmd_init_tlvs *tlv = NULL;
158-
if (hsm_passphrase) {
124+
if (ld->hsm_passphrase) {
159125
tlv = tlv_hsmd_init_tlvs_new(tmpctx);
160-
tlv->hsm_passphrase = tal_strdup(tlv, hsm_passphrase);
126+
tlv->hsm_passphrase = tal_strdup(tlv, ld->hsm_passphrase);
161127
}
162128

163129
if (!wire_sync_write(ld->hsm_fd, towire_hsmd_init(tmpctx,
164130
&chainparams->bip32_key_version,
165131
chainparams,
166-
ld->config.keypass,
132+
NULL,
167133
ld->dev_force_privkey,
168134
ld->dev_force_bip32_seed,
169135
ld->dev_force_channel_secrets,
@@ -182,8 +148,8 @@ struct ext_key *hsm_init(struct lightningd *ld)
182148
&unused)) {
183149
/* nothing to do. */
184150
} else {
185-
if (ld->config.keypass)
186-
errx(EXITCODE_HSM_BAD_PASSWORD, "Wrong password for encrypted hsm_secret.");
151+
if (ld->hsm_passphrase)
152+
errx(EXITCODE_HSM_BAD_PASSWORD, "Wrong passphrase for hsm_secret.");
187153
errx(EXITCODE_HSM_GENERIC_ERROR, "HSM did not give init reply");
188154
}
189155

lightningd/lightningd.c

Lines changed: 5 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -236,7 +236,7 @@ static struct lightningd *new_lightningd(const tal_t *ctx)
236236
ld->alias = NULL;
237237
ld->rgb = NULL;
238238
ld->recover = NULL;
239-
ld->hsm_passphrase_required = false;
239+
ld->hsm_passphrase = NULL;
240240
list_head_init(&ld->connects);
241241
list_head_init(&ld->waitsendpay_commands);
242242
list_head_init(&ld->close_commands);
@@ -316,10 +316,11 @@ static struct lightningd *new_lightningd(const tal_t *ctx)
316316
/*~ This is set when a JSON RPC command comes in to shut us down. */
317317
ld->stop_conn = NULL;
318318

319-
/*~ This is used to signal that `hsm_secret` is encrypted, and will
320-
* be set to `true` if the `--encrypted-hsm` option is passed at startup.
319+
/*~ This is used to store the passphrase for hsm_secret if needed.
320+
* It will be set if the `--hsm-passphrase` option is passed at startup.
321321
*/
322-
ld->encrypted_hsm = false;
322+
323+
ld->hsm_passphrase = NULL;
323324

324325
/* This is used to override subdaemons */
325326
strmap_init(&ld->alt_subdaemons);
@@ -1312,14 +1313,6 @@ int main(int argc, char *argv[])
13121313
/*~ This is the ccan/io central poll override from above. */
13131314
io_poll_override(io_poll_lightningd);
13141315

1315-
/*~ If hsm_secret is encrypted, we don't need its encryption key
1316-
* anymore. Note that sodium_munlock() also zeroes the memory.*/
1317-
if (ld->config.keypass) {
1318-
destroy_secret(ld->config.keypass);
1319-
tal_free(ld->config.keypass);
1320-
ld->config.keypass = NULL;
1321-
}
1322-
13231316
/*~ Our default color and alias are derived from our node id, so we
13241317
* can only set those now (if not set by config options). */
13251318
setup_color_and_alias(ld);

lightningd/lightningd.h

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -69,8 +69,7 @@ struct config {
6969
/* Minimal amount of effective funding_satoshis for accepting channels */
7070
u64 min_capacity_sat;
7171

72-
/* This is the key we use to encrypt `hsm_secret`. */
73-
struct secret *keypass;
72+
/* Encryption key derivation is now handled by hsmd internally */
7473

7574
/* How long before we give up waiting for INIT msg */
7675
u32 connection_timeout_secs;
@@ -379,12 +378,10 @@ struct lightningd {
379378
char *wallet_dsn;
380379

381380

382-
/* Whether hsm_secret requires a passphrase */
383-
bool hsm_passphrase_required;
381+
/* HSM passphrase for any format that needs it */
382+
char *hsm_passphrase;
383+
384384

385-
/* Legacy encrypted hsm_secret support */
386-
bool encrypted_hsm;
387-
struct secret *keypass;
388385
/* What (additional) messages the HSM accepts */
389386
u32 *hsm_capabilities;
390387

lightningd/options.c

Lines changed: 49 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -559,6 +559,46 @@ static void prompt(struct lightningd *ld, const char *str)
559559
fflush(stdout);
560560
}
561561

562+
/* Read HSM passphrase from user input */
563+
static char *read_hsm_passphrase(struct lightningd *ld)
564+
{
565+
const char *passphrase, *passphrase_confirmation;
566+
enum hsm_secret_error err;
567+
568+
prompt(ld, "The hsm_secret requires a passphrase. In order to "
569+
"access it and start the node you must provide the passphrase.");
570+
prompt(ld, "Enter hsm_secret passphrase:");
571+
572+
passphrase = read_stdin_pass(tmpctx, &err);
573+
if (err != HSM_SECRET_OK) {
574+
opt_exitcode = EXITCODE_HSM_PASSWORD_INPUT_ERR;
575+
return tal_strdup(tmpctx, hsm_secret_error_str(err));
576+
}
577+
578+
/* We need confirmation if the hsm_secret file doesn't exist yet */
579+
if (!path_is_file("hsm_secret")) {
580+
prompt(ld, "Confirm hsm_secret passphrase:");
581+
fflush(stdout);
582+
passphrase_confirmation = read_stdin_pass(tmpctx, &err);
583+
if (err != HSM_SECRET_OK) {
584+
opt_exitcode = EXITCODE_HSM_PASSWORD_INPUT_ERR;
585+
return tal_strdup(tmpctx, hsm_secret_error_str(err));
586+
}
587+
588+
if (!streq(passphrase, passphrase_confirmation)) {
589+
opt_exitcode = EXITCODE_HSM_BAD_PASSWORD;
590+
return "Passphrase confirmation mismatch.";
591+
}
592+
}
593+
594+
/* Store passphrase in lightningd struct */
595+
ld->hsm_passphrase = tal_strdup(ld, passphrase);
596+
597+
/* Encryption key derivation is handled by hsmd internally */
598+
599+
return NULL;
600+
}
601+
562602
/* Prompt the user to enter a password, from which will be derived the key used
563603
* for `hsm_secret` encryption.
564604
* The algorithm used to derive the key is Argon2(id), to which libsodium
@@ -569,8 +609,6 @@ static void prompt(struct lightningd *ld, const char *str)
569609
*/
570610
static char *opt_set_hsm_password(struct lightningd *ld)
571611
{
572-
const char *passwd, *passwd_confirmation;
573-
enum hsm_secret_error err;
574612
int is_encrypted;
575613

576614
/* Show deprecation warning */
@@ -590,43 +628,10 @@ static char *opt_set_hsm_password(struct lightningd *ld)
590628
log_info(ld->log, "'hsm_secret' does not exist (%s)",
591629
strerror(errno));
592630

593-
prompt(ld, "The hsm_secret is encrypted with a password. In order to "
594-
"decrypt it and start the node you must provide the password.");
595-
prompt(ld, "Enter hsm_secret password:");
596-
597-
passwd = read_stdin_pass(tmpctx, &err);
598-
if (err != HSM_SECRET_OK) {
599-
opt_exitcode = EXITCODE_HSM_PASSWORD_INPUT_ERR;
600-
return tal_strdup(tmpctx, hsm_secret_error_str(err));
601-
}
602-
603-
if (!is_encrypted) {
604-
prompt(ld, "Confirm hsm_secret password:");
605-
fflush(stdout);
606-
passwd_confirmation = read_stdin_pass(tmpctx, &err);
607-
if (err != HSM_SECRET_OK) {
608-
opt_exitcode = EXITCODE_HSM_PASSWORD_INPUT_ERR;
609-
return tal_strdup(tmpctx, hsm_secret_error_str(err));
610-
}
611-
612-
if (!streq(passwd, passwd_confirmation)) {
613-
opt_exitcode = EXITCODE_HSM_BAD_PASSWORD;
614-
return "Passwords confirmation mismatch.";
615-
}
616-
}
617-
prompt(ld, "");
618-
619-
ld->config.keypass = tal(NULL, struct secret);
620-
621-
/* Derive encryption key from passphrase using the same function as hsm_secret.c */
622-
ld->config.keypass = get_encryption_key(tmpctx, passwd);
623-
if (!ld->config.keypass) {
624-
opt_exitcode = EXITCODE_HSM_BAD_PASSWORD;
625-
return "Could not derive encryption key from password.";
626-
}
627-
628-
ld->encrypted_hsm = true;
629-
631+
/* Read passphrase from user */
632+
char *err = read_hsm_passphrase(ld);
633+
if (err)
634+
return err;
630635
return NULL;
631636
}
632637

@@ -635,7 +640,11 @@ static char *opt_set_hsm_password(struct lightningd *ld)
635640
*/
636641
static char *opt_set_hsm_passphrase(struct lightningd *ld)
637642
{
638-
ld->hsm_passphrase_required = true;
643+
/* Read passphrase from user */
644+
char *err = read_hsm_passphrase(ld);
645+
if (err)
646+
return err;
647+
639648
return NULL;
640649
}
641650

0 commit comments

Comments
 (0)