2017-01-10 06:08:33 +01:00
|
|
|
#include "hsm_control.h"
|
|
|
|
#include "lightningd.h"
|
2017-03-10 11:49:43 +01:00
|
|
|
#include "subd.h"
|
2017-01-10 06:08:33 +01:00
|
|
|
#include <ccan/err/err.h>
|
2018-07-09 13:17:59 +02:00
|
|
|
#include <ccan/fdpass/fdpass.h>
|
2017-01-10 06:08:33 +01:00
|
|
|
#include <ccan/io/io.h>
|
|
|
|
#include <ccan/take/take.h>
|
2017-08-28 18:05:01 +02:00
|
|
|
#include <common/status.h>
|
2017-08-28 18:02:01 +02:00
|
|
|
#include <common/utils.h>
|
2017-06-24 08:50:23 +02:00
|
|
|
#include <errno.h>
|
2018-09-20 05:06:42 +02:00
|
|
|
#include <hsmd/gen_hsm_wire.h>
|
2017-01-10 06:08:33 +01:00
|
|
|
#include <inttypes.h>
|
2019-03-11 20:03:14 +01:00
|
|
|
#include <lightningd/bitcoind.h>
|
2017-11-22 01:25:39 +01:00
|
|
|
#include <lightningd/hsm_control.h>
|
2017-08-28 18:04:01 +02:00
|
|
|
#include <lightningd/log.h>
|
2018-02-05 05:09:27 +01:00
|
|
|
#include <lightningd/log_status.h>
|
2017-06-24 08:50:23 +02:00
|
|
|
#include <string.h>
|
2018-07-09 13:17:59 +02:00
|
|
|
#include <sys/socket.h>
|
|
|
|
#include <sys/types.h>
|
2017-02-21 05:45:29 +01:00
|
|
|
#include <wally_bip32.h>
|
2017-06-24 08:50:23 +02:00
|
|
|
#include <wire/wire_sync.h>
|
2017-01-10 06:08:33 +01:00
|
|
|
|
2018-09-20 05:07:41 +02:00
|
|
|
static int hsm_get_fd(struct lightningd *ld,
|
2019-04-08 11:58:32 +02:00
|
|
|
const struct node_id *id,
|
2018-07-09 13:17:59 +02:00
|
|
|
u64 dbid,
|
|
|
|
int capabilities)
|
|
|
|
{
|
|
|
|
int hsm_fd;
|
|
|
|
u8 *msg;
|
|
|
|
|
|
|
|
msg = towire_hsm_client_hsmfd(NULL, id, dbid, capabilities);
|
|
|
|
if (!wire_sync_write(ld->hsm_fd, take(msg)))
|
|
|
|
fatal("Could not write to HSM: %s", strerror(errno));
|
|
|
|
|
2018-07-09 13:17:59 +02:00
|
|
|
msg = wire_sync_read(tmpctx, ld->hsm_fd);
|
2018-07-09 13:17:59 +02:00
|
|
|
if (!fromwire_hsm_client_hsmfd_reply(msg))
|
|
|
|
fatal("Bad reply from HSM: %s", tal_hex(tmpctx, msg));
|
|
|
|
|
|
|
|
hsm_fd = fdpass_recv(ld->hsm_fd);
|
|
|
|
if (hsm_fd < 0)
|
|
|
|
fatal("Could not read fd from HSM: %s", strerror(errno));
|
|
|
|
return hsm_fd;
|
|
|
|
}
|
|
|
|
|
2018-09-20 05:07:41 +02:00
|
|
|
int hsm_get_client_fd(struct lightningd *ld,
|
2019-04-08 11:58:32 +02:00
|
|
|
const struct node_id *id,
|
2018-09-20 05:07:41 +02:00
|
|
|
u64 dbid,
|
|
|
|
int capabilities)
|
|
|
|
{
|
|
|
|
assert(dbid);
|
|
|
|
|
|
|
|
return hsm_get_fd(ld, id, dbid, capabilities);
|
|
|
|
}
|
|
|
|
|
|
|
|
int hsm_get_global_fd(struct lightningd *ld, int capabilities)
|
|
|
|
{
|
|
|
|
return hsm_get_fd(ld, &ld->id, 0, capabilities);
|
|
|
|
}
|
|
|
|
|
2018-09-20 05:06:42 +02:00
|
|
|
static unsigned int hsm_msg(struct subd *hsmd,
|
|
|
|
const u8 *msg, const int *fds UNUSED)
|
|
|
|
{
|
|
|
|
/* We only expect one thing from the HSM that's not a STATUS message */
|
2019-04-08 11:58:32 +02:00
|
|
|
struct node_id client_id;
|
2018-09-20 05:06:42 +02:00
|
|
|
u8 *bad_msg;
|
2018-09-20 05:06:42 +02:00
|
|
|
char *desc;
|
2018-09-20 05:06:42 +02:00
|
|
|
|
|
|
|
if (!fromwire_hsmstatus_client_bad_request(tmpctx, msg, &client_id,
|
2018-09-20 05:06:42 +02:00
|
|
|
&desc, &bad_msg))
|
2018-09-20 05:06:42 +02:00
|
|
|
fatal("Bad status message from hsmd: %s", tal_hex(tmpctx, msg));
|
|
|
|
|
|
|
|
/* This should, of course, never happen. */
|
2018-09-20 05:06:42 +02:00
|
|
|
log_broken(hsmd->log, "client %s %s (request %s)",
|
2019-04-08 11:58:32 +02:00
|
|
|
type_to_string(tmpctx, struct node_id, &client_id),
|
2018-09-20 05:06:42 +02:00
|
|
|
desc, tal_hex(tmpctx, bad_msg));
|
2018-09-20 05:06:42 +02:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2018-05-03 14:20:28 +02:00
|
|
|
void hsm_init(struct lightningd *ld)
|
2017-01-10 06:08:33 +01:00
|
|
|
{
|
2017-08-18 06:43:52 +02:00
|
|
|
u8 *msg;
|
2018-07-09 13:17:59 +02:00
|
|
|
int fds[2];
|
2019-07-31 13:10:20 +02:00
|
|
|
struct bitcoin_blkid chainhash;
|
|
|
|
chainhash = get_chainparams(ld)->genesis_blockhash;
|
2018-07-09 13:17:59 +02:00
|
|
|
|
|
|
|
/* We actually send requests synchronously: only status is async. */
|
|
|
|
if (socketpair(AF_LOCAL, SOCK_STREAM, 0, fds) != 0)
|
|
|
|
err(1, "Could not create hsm socketpair");
|
2017-01-10 06:08:33 +01:00
|
|
|
|
2018-09-20 05:06:42 +02:00
|
|
|
ld->hsm = new_global_subd(ld, "lightning_hsmd",
|
2018-09-20 05:06:42 +02:00
|
|
|
hsm_wire_type_name,
|
2018-09-20 05:06:42 +02:00
|
|
|
hsm_msg,
|
2018-07-09 13:17:59 +02:00
|
|
|
take(&fds[1]), NULL);
|
|
|
|
if (!ld->hsm)
|
2017-03-10 11:49:43 +01:00
|
|
|
err(1, "Could not subd hsm");
|
2017-01-10 06:08:33 +01:00
|
|
|
|
2018-07-09 13:17:59 +02:00
|
|
|
ld->hsm_fd = fds[0];
|
2019-03-11 20:03:14 +01:00
|
|
|
if (!wire_sync_write(ld->hsm_fd, towire_hsm_init(tmpctx,
|
2019-07-31 13:10:20 +02:00
|
|
|
&ld->topology->bitcoind->chainparams->bip32_key_version,
|
|
|
|
&chainhash,
|
2019-07-16 23:40:14 +02:00
|
|
|
#if DEVELOPER
|
2019-07-16 23:41:14 +02:00
|
|
|
ld->dev_force_privkey,
|
2019-07-16 23:45:14 +02:00
|
|
|
ld->dev_force_bip32_seed,
|
|
|
|
ld->dev_force_channel_secrets,
|
|
|
|
ld->dev_force_channel_secrets_shaseed
|
2019-07-16 23:40:14 +02:00
|
|
|
#else
|
2019-07-16 23:45:14 +02:00
|
|
|
NULL, NULL, NULL, NULL
|
2019-07-16 23:40:14 +02:00
|
|
|
#endif
|
|
|
|
)))
|
2017-06-24 08:50:23 +02:00
|
|
|
err(1, "Writing init msg to hsm");
|
2017-01-10 06:08:33 +01:00
|
|
|
|
2017-08-28 18:09:01 +02:00
|
|
|
ld->wallet->bip32_base = tal(ld->wallet, struct ext_key);
|
2018-07-09 13:17:59 +02:00
|
|
|
msg = wire_sync_read(tmpctx, ld->hsm_fd);
|
2018-02-20 21:59:09 +01:00
|
|
|
if (!fromwire_hsm_init_reply(msg,
|
2018-07-23 04:23:03 +02:00
|
|
|
&ld->id, ld->wallet->bip32_base))
|
2017-06-24 08:50:23 +02:00
|
|
|
errx(1, "HSM did not give init reply");
|
|
|
|
}
|