~ This is the response to lightningd's HSM_INIT request, which is the first * thing it sends. */
| 434 | /*~ This is the response to lightningd's HSM_INIT request, which is the first |
| 435 | * thing it sends. */ |
| 436 | static struct io_plan *init_hsm(struct io_conn *conn, |
| 437 | struct client *c, |
| 438 | const u8 *msg_in) |
| 439 | { |
| 440 | struct privkey *privkey; |
| 441 | struct secret *seed; |
| 442 | struct secrets *secrets; |
| 443 | struct sha256 *shaseed; |
| 444 | struct secret *hsm_encryption_key; |
| 445 | struct bip32_key_version bip32_key_version; |
| 446 | |
| 447 | /* This must be lightningd. */ |
| 448 | assert(is_lightningd(c)); |
| 449 | |
| 450 | /*~ The fromwire_* routines are autogenerated, based on the message |
| 451 | * definitions in hsm_client_wire.csv. The format of those files is |
| 452 | * an extension of the simple comma-separated format output by the |
| 453 | * BOLT tools/extract-formats.py tool. */ |
| 454 | if (!fromwire_hsmd_init(NULL, msg_in, &bip32_key_version, &chainparams, |
| 455 | &hsm_encryption_key, &privkey, &seed, &secrets, &shaseed)) |
| 456 | return bad_req(conn, c, msg_in); |
| 457 | |
| 458 | /*~ The memory is actually copied in towire(), so lock the `hsm_secret` |
| 459 | * encryption key (new) memory again here. */ |
| 460 | if (hsm_encryption_key && sodium_mlock(hsm_encryption_key, |
| 461 | sizeof(hsm_encryption_key)) != 0) |
| 462 | status_failed(STATUS_FAIL_INTERNAL_ERROR, |
| 463 | "Could not lock memory for hsm_secret encryption key."); |
| 464 | /*~ Don't swap this. */ |
| 465 | sodium_mlock(hsm_secret.data, sizeof(hsm_secret.data)); |
| 466 | |
| 467 | #if DEVELOPER |
| 468 | dev_force_privkey = privkey; |
| 469 | dev_force_bip32_seed = seed; |
| 470 | dev_force_channel_secrets = secrets; |
| 471 | dev_force_channel_secrets_shaseed = shaseed; |
| 472 | #endif |
| 473 | |
| 474 | /* Once we have read the init message we know which params the master |
| 475 | * will use */ |
| 476 | c->chainparams = chainparams; |
| 477 | maybe_create_new_hsm(hsm_encryption_key, true); |
| 478 | load_hsm(hsm_encryption_key); |
| 479 | |
| 480 | /*~ We don't need the hsm_secret encryption key anymore. */ |
| 481 | if (hsm_encryption_key) |
| 482 | discard_key(take(hsm_encryption_key)); |
| 483 | |
| 484 | return req_reply(conn, c, hsmd_init(hsm_secret, bip32_key_version)); |
| 485 | } |
| 486 | |
| 487 | /*~ Since we process requests then service them in strict order, and because |
| 488 | * only lightningd can request a new client fd, we can get away with a global |
no test coverage detected