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>
|
2020-04-03 05:21:22 +02:00
|
|
|
#include <common/ecdh.h>
|
2020-02-04 06:53:17 +01:00
|
|
|
#include <common/json.h>
|
2020-05-15 12:30:25 +02:00
|
|
|
#include <common/json_helpers.h>
|
2020-02-04 06:53:17 +01:00
|
|
|
#include <common/jsonrpc_errors.h>
|
|
|
|
#include <common/param.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>
|
2020-08-25 03:55:38 +02:00
|
|
|
#include <hsmd/hsmd_wiregen.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>
|
2020-02-04 06:53:17 +01:00
|
|
|
#include <lightningd/json.h>
|
|
|
|
#include <lightningd/jsonrpc.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>
|
2019-10-03 23:08:43 +02:00
|
|
|
#include <sys/stat.h>
|
2018-07-09 13:17:59 +02:00
|
|
|
#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;
|
|
|
|
|
2020-08-25 03:55:38 +02:00
|
|
|
msg = towire_hsmd_client_hsmfd(NULL, id, dbid, capabilities);
|
2018-07-09 13:17:59 +02:00
|
|
|
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);
|
2020-08-25 03:55:38 +02:00
|
|
|
if (!fromwire_hsmd_client_hsmfd_reply(msg))
|
2018-07-09 13:17:59 +02:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2020-05-26 23:06:13 +02:00
|
|
|
struct ext_key *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];
|
2020-05-26 23:06:13 +02:00
|
|
|
struct ext_key *bip32_base;
|
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",
|
2020-08-25 03:55:38 +02:00
|
|
|
hsmd_wire_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
|
|
|
|
2019-10-03 23:08:43 +02:00
|
|
|
/* If hsm_secret is encrypted and the --encrypted-hsm startup option is
|
|
|
|
* not passed, don't let hsmd use the first 32 bytes of the cypher as the
|
|
|
|
* actual secret. */
|
|
|
|
if (!ld->config.keypass) {
|
|
|
|
struct stat st;
|
|
|
|
if (stat("hsm_secret", &st) == 0 && st.st_size > 32)
|
|
|
|
errx(1, "hsm_secret is encrypted, you need to pass the "
|
|
|
|
"--encrypted-hsm startup option.");
|
|
|
|
}
|
|
|
|
|
2018-07-09 13:17:59 +02:00
|
|
|
ld->hsm_fd = fds[0];
|
2020-08-25 03:55:38 +02:00
|
|
|
if (!wire_sync_write(ld->hsm_fd, towire_hsmd_init(tmpctx,
|
2019-10-15 12:58:30 +02:00
|
|
|
&chainparams->bip32_key_version,
|
2019-09-25 22:38:45 +02:00
|
|
|
chainparams,
|
2019-10-03 21:38:50 +02:00
|
|
|
ld->config.keypass,
|
2019-09-12 02:24:00 +02:00
|
|
|
IFDEV(ld->dev_force_privkey, NULL),
|
|
|
|
IFDEV(ld->dev_force_bip32_seed, NULL),
|
|
|
|
IFDEV(ld->dev_force_channel_secrets, NULL),
|
|
|
|
IFDEV(ld->dev_force_channel_secrets_shaseed, NULL))))
|
2017-06-24 08:50:23 +02:00
|
|
|
err(1, "Writing init msg to hsm");
|
2017-01-10 06:08:33 +01:00
|
|
|
|
2020-05-26 23:06:13 +02:00
|
|
|
bip32_base = tal(ld, struct ext_key);
|
2018-07-09 13:17:59 +02:00
|
|
|
msg = wire_sync_read(tmpctx, ld->hsm_fd);
|
2020-08-25 03:55:38 +02:00
|
|
|
if (!fromwire_hsmd_init_reply(msg,
|
2020-05-26 23:06:13 +02:00
|
|
|
&ld->id, bip32_base)) {
|
2019-10-03 21:55:32 +02:00
|
|
|
if (ld->config.keypass)
|
|
|
|
errx(1, "Wrong password for encrypted hsm_secret.");
|
2017-06-24 08:50:23 +02:00
|
|
|
errx(1, "HSM did not give init reply");
|
2019-10-03 21:55:32 +02:00
|
|
|
}
|
2020-05-26 23:06:13 +02:00
|
|
|
|
|
|
|
return bip32_base;
|
2017-06-24 08:50:23 +02:00
|
|
|
}
|
2020-02-04 06:53:17 +01:00
|
|
|
|
|
|
|
static struct command_result *json_getsharedsecret(struct command *cmd,
|
|
|
|
const char *buffer,
|
|
|
|
const jsmntok_t *obj UNNEEDED,
|
|
|
|
const jsmntok_t *params)
|
|
|
|
{
|
|
|
|
struct pubkey *point;
|
|
|
|
struct secret ss;
|
|
|
|
struct json_stream *response;
|
|
|
|
|
|
|
|
if (!param(cmd, buffer, params,
|
|
|
|
p_req("point", ¶m_pubkey, &point),
|
|
|
|
NULL))
|
|
|
|
return command_param_failed();
|
|
|
|
|
2020-04-03 05:21:22 +02:00
|
|
|
ecdh(point, &ss);
|
2020-02-04 06:53:17 +01:00
|
|
|
response = json_stream_success(cmd);
|
|
|
|
json_add_secret(response, "shared_secret", &ss);
|
|
|
|
return command_success(cmd, response);
|
|
|
|
}
|
|
|
|
|
|
|
|
static const struct json_command getsharedsecret_command = {
|
|
|
|
"getsharedsecret",
|
|
|
|
"utility", /* FIXME: Or "crypto"? */
|
|
|
|
&json_getsharedsecret,
|
|
|
|
"Compute the hash of the Elliptic Curve Diffie Hellman shared secret point from "
|
|
|
|
"this node private key and an input {point}."
|
|
|
|
};
|
|
|
|
AUTODATA(json_command, &getsharedsecret_command);
|