MCPcopy Create free account
hub / github.com/ElementsProject/lightning / load_hsm

Function load_hsm

hsmd/hsmd.c:427–466  ·  view source on GitHub ↗

~ We always load the HSM file, even if we just created it above. This * both unifies the code paths, and provides a nice sanity check that the * file contents are as they will be for future invocations. */

Source from the content-addressed store, hash-verified

425 * both unifies the code paths, and provides a nice sanity check that the
426 * file contents are as they will be for future invocations. */
427static void load_hsm(const char *passphrase)
428{
429 u8 *hsm_secret_contents;
430 struct hsm_secret *hsms;
431 enum hsm_secret_error err;
432
433 /* Read the hsm_secret file */
434 hsm_secret_contents = grab_file_raw(tmpctx, "hsm_secret");
435 if (!hsm_secret_contents) {
436 hsmd_send_init_reply_failure(HSM_SECRET_ERR_INVALID_FORMAT, STATUS_FAIL_INTERNAL_ERROR,
437 "Could not read hsm_secret: %s", strerror(errno));
438 }
439
440 /* Extract the secret using the new hsm_secret module */
441 hsms = extract_hsm_secret(tmpctx, hsm_secret_contents,
442 tal_bytelen(hsm_secret_contents),
443 passphrase, &err);
444 if (!hsms) {
445 /* Wrong passphrase is user error, not internal error - exit cleanly */
446 if (err == HSM_SECRET_ERR_WRONG_PASSPHRASE) {
447 u8 *msg = towire_hsmd_init_reply_failure(NULL, err,
448 tal_fmt(tmpctx,
449 "Failed to load hsm_secret: %s",
450 hsm_secret_error_str(err)));
451 wire_sync_write(REQ_FD, msg);
452 exit(0);
453 }
454 hsmd_send_init_reply_failure(err, STATUS_FAIL_INTERNAL_ERROR,
455 "Failed to load hsm_secret: %s", hsm_secret_error_str(err));
456 }
457
458 /* Allocate and populate our global hsm_secret */
459 hsm_secret = tal(NULL, struct hsm_secret);
460 hsm_secret->secret_data = tal_steal(hsm_secret, hsms->secret_data);
461 hsm_secret->type = hsms->type;
462 hsm_secret->mnemonic = tal_steal(hsm_secret, hsms->mnemonic);
463
464 /*~ Don't swap this secret data to disk for security. */
465 mlock_tal_memory(hsm_secret->secret_data);
466}
467
468/*~ We have a pre-init call in developer mode, to set dev flags */
469static struct io_plan *preinit_hsm(struct io_conn *conn,

Callers 1

init_hsmFunction · 0.85

Calls 7

grab_file_rawFunction · 0.85
extract_hsm_secretFunction · 0.85
tal_bytelenFunction · 0.85
hsm_secret_error_strFunction · 0.85
mlock_tal_memoryFunction · 0.85
wire_sync_writeFunction · 0.50

Tested by

no test coverage detected