Skip to content

Commit 68cb21c

Browse files
committed
Change the init reply from hsmd to lightningd to get a better response
1 parent e93ec5d commit 68cb21c

File tree

6 files changed

+141
-111
lines changed

6 files changed

+141
-111
lines changed

hsmd/hsmd.c

Lines changed: 34 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -272,6 +272,23 @@ static struct io_plan *req_reply(struct io_conn *conn,
272272
return io_write_wire(conn, msg_out, client_read_next, c);
273273
}
274274

275+
/* Send an init reply failure message to lightningd and then call status_failed */
276+
static void hsmd_send_init_reply_failure(enum hsm_secret_error error_code, enum status_failreason reason, const char *error_msg, ...)
277+
{
278+
u8 *msg;
279+
280+
/* Send the init reply failure first */
281+
msg = towire_hsmd_init_reply_failure(NULL, error_code, error_msg);
282+
if (msg) {
283+
/* Send directly to lightningd via REQ_FD */
284+
write_all(REQ_FD, msg, tal_bytelen(msg));
285+
tal_free(msg);
286+
}
287+
288+
/* Then call status_failed with the error message */
289+
status_failed(reason, "%s", error_msg);
290+
}
291+
275292
static void create_hsm(int fd, const char *passphrase)
276293
{
277294
u8 *hsm_secret_data;
@@ -300,23 +317,22 @@ static void create_hsm(int fd, const char *passphrase)
300317

301318
if (ret != WALLY_OK) {
302319
unlink_noerr("hsm_secret");
303-
status_failed(STATUS_FAIL_INTERNAL_ERROR,
304-
"Failed to generate mnemonic from entropy");
320+
hsmd_send_init_reply_failure(HSM_SECRET_ERR_SEED_DERIVATION_FAILED, STATUS_FAIL_INTERNAL_ERROR,
321+
"Failed to generate mnemonic from entropy");
305322
}
306323
status_debug("HSM: Generated mnemonic from entropy");
307324

308325
if (!mnemonic) {
309326
unlink_noerr("hsm_secret");
310-
//TODO: Add passphrase error message, add new codes
311-
status_failed(STATUS_FAIL_INTERNAL_ERROR,
312-
"Failed to get generated mnemonic");
327+
hsmd_send_init_reply_failure(HSM_SECRET_ERR_SEED_DERIVATION_FAILED, STATUS_FAIL_INTERNAL_ERROR,
328+
"Failed to get generated mnemonic");
313329
}
314330

315331
/* Derive seed hash from mnemonic + passphrase (or zero if no passphrase) */
316332
if (!derive_seed_hash(mnemonic, passphrase, &seed_hash)) {
317333
unlink_noerr("hsm_secret");
318-
status_failed(STATUS_FAIL_INTERNAL_ERROR,
319-
"Failed to derive seed hash from mnemonic");
334+
hsmd_send_init_reply_failure(HSM_SECRET_ERR_SEED_DERIVATION_FAILED, STATUS_FAIL_INTERNAL_ERROR,
335+
"Failed to derive seed hash from mnemonic");
320336
}
321337
status_debug("HSM: Derived seed hash from mnemonic");
322338

@@ -334,10 +350,13 @@ static void create_hsm(int fd, const char *passphrase)
334350
u8 bip32_seed[BIP39_SEED_LEN_512];
335351
size_t bip32_seed_len;
336352

337-
if (bip39_mnemonic_to_seed(mnemonic, passphrase, bip32_seed, sizeof(bip32_seed), &bip32_seed_len) != WALLY_OK) {
353+
tal_wally_start();
354+
ret = bip39_mnemonic_to_seed(mnemonic, passphrase, bip32_seed, sizeof(bip32_seed), &bip32_seed_len);
355+
tal_wally_end(tmpctx);
356+
if (ret != WALLY_OK) {
338357
unlink_noerr("hsm_secret");
339-
status_failed(STATUS_FAIL_INTERNAL_ERROR,
340-
"Failed to derive seed from mnemonic");
358+
hsmd_send_init_reply_failure(HSM_SECRET_ERR_SEED_DERIVATION_FAILED, STATUS_FAIL_INTERNAL_ERROR,
359+
"Failed to derive seed from mnemonic");
341360
}
342361
status_debug("HSM: Derived BIP32 seed from mnemonic");
343362

@@ -425,8 +444,8 @@ static void load_hsm(const char *passphrase)
425444
/* Read the hsm_secret file */
426445
hsm_secret_contents = grab_file(tmpctx, "hsm_secret");
427446
if (!hsm_secret_contents) {
428-
status_failed(STATUS_FAIL_INTERNAL_ERROR,
429-
"Could not read hsm_secret: %s", strerror(errno));
447+
hsmd_send_init_reply_failure(HSM_SECRET_ERR_INVALID_FORMAT, STATUS_FAIL_INTERNAL_ERROR,
448+
"Could not read hsm_secret: %s", strerror(errno));
430449
}
431450

432451
/* Remove the NUL terminator that grab_file adds */
@@ -439,8 +458,8 @@ static void load_hsm(const char *passphrase)
439458
passphrase, &err);
440459
tal_wally_end(tmpctx);
441460
if (!hsms) {
442-
status_failed(STATUS_FAIL_INTERNAL_ERROR,
443-
"Failed to load hsm_secret: %s", hsm_secret_error_str(err));
461+
hsmd_send_init_reply_failure(err, STATUS_FAIL_INTERNAL_ERROR,
462+
"Failed to load hsm_secret: %s", hsm_secret_error_str(err));
444463
}
445464

446465
/* Copy the extracted secret to our global hsm_secret */
@@ -764,6 +783,7 @@ static struct io_plan *handle_client(struct io_conn *conn, struct client *c)
764783
case WIRE_HSMD_SIGN_WITHDRAWAL_REPLY:
765784
case WIRE_HSMD_SIGN_INVOICE_REPLY:
766785
case WIRE_HSMD_INIT_REPLY_V4:
786+
case WIRE_HSMD_INIT_REPLY_FAILURE:
767787
case WIRE_HSMD_DERIVE_SECRET_REPLY:
768788
case WIRE_HSMSTATUS_CLIENT_BAD_REQUEST:
769789
case WIRE_HSMD_SIGN_COMMITMENT_TX_REPLY:

hsmd/hsmd_wire.csv

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,11 @@ msgdata,hsmd_init_reply_v4,node_id,node_id,
4747
msgdata,hsmd_init_reply_v4,bip32,ext_key,
4848
msgdata,hsmd_init_reply_v4,bolt12,pubkey,
4949

50+
# HSM initialization failure response
51+
msgtype,hsmd_init_reply_failure,115
52+
msgdata,hsmd_init_reply_failure,error_code,u32,
53+
msgdata,hsmd_init_reply_failure,error_message,wirestring,
54+
5055
# Declare a new channel.
5156
msgtype,hsmd_new_channel,30
5257
msgdata,hsmd_new_channel,id,node_id,

hsmd/libhsmd.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -171,6 +171,7 @@ bool hsmd_check_client_capabilities(struct hsmd_client *client,
171171
case WIRE_HSMD_SIGN_WITHDRAWAL_REPLY:
172172
case WIRE_HSMD_SIGN_INVOICE_REPLY:
173173
case WIRE_HSMD_INIT_REPLY_V4:
174+
case WIRE_HSMD_INIT_REPLY_FAILURE:
174175
case WIRE_HSMSTATUS_CLIENT_BAD_REQUEST:
175176
case WIRE_HSMD_SIGN_COMMITMENT_TX_REPLY:
176177
case WIRE_HSMD_VALIDATE_COMMITMENT_TX_REPLY:
@@ -2300,6 +2301,7 @@ u8 *hsmd_handle_client_message(const tal_t *ctx, struct hsmd_client *client,
23002301
case WIRE_HSMD_SIGN_WITHDRAWAL_REPLY:
23012302
case WIRE_HSMD_SIGN_INVOICE_REPLY:
23022303
case WIRE_HSMD_INIT_REPLY_V4:
2304+
case WIRE_HSMD_INIT_REPLY_FAILURE:
23032305
case WIRE_HSMSTATUS_CLIENT_BAD_REQUEST:
23042306
case WIRE_HSMD_SIGN_COMMITMENT_TX_REPLY:
23052307
case WIRE_HSMD_VALIDATE_COMMITMENT_TX_REPLY:

lightningd/hsm_control.c

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -141,16 +141,25 @@ struct ext_key *hsm_init(struct lightningd *ld)
141141

142142
bip32_base = tal(ld, struct ext_key);
143143
msg = wire_sync_read(tmpctx, ld->hsm_fd);
144+
145+
/* Check for init reply failure first */
146+
u32 error_code;
147+
char *error_message;
148+
if (fromwire_hsmd_init_reply_failure(tmpctx, msg, &error_code, &error_message)) {
149+
/* HSM initialization failed - exit with the specific error code */
150+
errx(error_code, "HSM initialization failed: %s", error_message);
151+
}
152+
153+
/* Check for successful init reply */
144154
if (fromwire_hsmd_init_reply_v4(ld, msg,
145155
&hsm_version,
146156
&ld->hsm_capabilities,
147157
&ld->our_nodeid, bip32_base,
148158
&unused)) {
149159
/* nothing to do. */
150160
} else {
151-
if (ld->hsm_passphrase)
152-
errx(EXITCODE_HSM_BAD_PASSWORD, "Wrong passphrase for hsm_secret.");
153-
errx(EXITCODE_HSM_GENERIC_ERROR, "HSM did not give init reply");
161+
/* Unknown message type */
162+
errx(EXITCODE_HSM_GENERIC_ERROR, "HSM sent unknown message type");
154163
}
155164

156165
if (!pubkey_from_node_id(&ld->our_pubkey, &ld->our_nodeid))

0 commit comments

Comments
 (0)