2021-12-04 12:23:56 +01:00
|
|
|
#include "config.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>
|
2020-04-03 05:21:22 +02:00
|
|
|
#include <common/ecdh.h>
|
2021-12-14 22:57:21 +01:00
|
|
|
#include <common/errcode.h>
|
2021-01-03 16:59:24 +01:00
|
|
|
#include <common/hsm_encryption.h>
|
2020-05-15 12:30:25 +02:00
|
|
|
#include <common/json_helpers.h>
|
2020-02-04 06:53:17 +01:00
|
|
|
#include <common/param.h>
|
2021-09-16 07:00:42 +02:00
|
|
|
#include <common/type_to_string.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>
|
2021-12-04 12:23:56 +01:00
|
|
|
#include <lightningd/hsm_control.h>
|
2020-02-04 06:53:17 +01:00
|
|
|
#include <lightningd/json.h>
|
|
|
|
#include <lightningd/jsonrpc.h>
|
2021-12-04 12:23:56 +01:00
|
|
|
#include <lightningd/lightningd.h>
|
|
|
|
#include <lightningd/subd.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>
|
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)
|
2021-12-14 22:57:21 +01:00
|
|
|
err(HSM_GENERIC_ERROR, "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)
|
2021-12-14 22:57:21 +01:00
|
|
|
err(HSM_GENERIC_ERROR, "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) {
|
2022-03-10 22:26:20 +01:00
|
|
|
if (is_hsm_secret_encrypted("hsm_secret") == 1)
|
2021-12-14 22:57:21 +01:00
|
|
|
errx(HSM_ERROR_IS_ENCRYPT, "hsm_secret is encrypted, you need to pass the "
|
2022-03-10 22:26:20 +01:00
|
|
|
"--encrypted-hsm startup option.");
|
2019-10-03 23:08:43 +02:00
|
|
|
}
|
|
|
|
|
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))))
|
2021-12-14 22:57:21 +01:00
|
|
|
err(HSM_GENERIC_ERROR, "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-12-16 04:13:37 +01:00
|
|
|
&ld->id, bip32_base,
|
2021-09-21 23:17:25 +02:00
|
|
|
&ld->bolt12_base,
|
|
|
|
&ld->onion_reply_secret)) {
|
2019-10-03 21:55:32 +02:00
|
|
|
if (ld->config.keypass)
|
2021-12-14 22:57:21 +01:00
|
|
|
errx(HSM_BAD_PASSWORD, "Wrong password for encrypted hsm_secret.");
|
|
|
|
errx(HSM_GENERIC_ERROR, "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);
|