Skip to content

Commit 1e6eabf

Browse files
rustyrussellniftynei
authored andcommitted
developer: add --dev-force-channel-secrets.
We don't have this on a per-channel basis (yet), but it's sufficient for testing now. Signed-off-by: Rusty Russell <[email protected]>
1 parent 5879f6b commit 1e6eabf

9 files changed

+153
-44
lines changed

common/derive_basepoints.c

+75-41
Original file line numberDiff line numberDiff line change
@@ -4,19 +4,73 @@
44
#include <common/utils.h>
55
#include <wire/wire.h>
66

7+
#if DEVELOPER
8+
/* If they specify --dev-force-channel-secrets it ends up in here. */
9+
struct secrets *dev_force_channel_secrets;
10+
struct sha256 *dev_force_channel_secrets_shaseed;
11+
12+
void towire_secrets(u8 **pptr, const struct secrets *s)
13+
{
14+
towire_privkey(pptr, &s->funding_privkey);
15+
towire_secret(pptr, &s->revocation_basepoint_secret);
16+
towire_secret(pptr, &s->payment_basepoint_secret);
17+
towire_secret(pptr, &s->delayed_payment_basepoint_secret);
18+
towire_secret(pptr, &s->htlc_basepoint_secret);
19+
}
20+
21+
void fromwire_secrets(const u8 **ptr, size_t *max, struct secrets *s)
22+
{
23+
fromwire_privkey(ptr, max, &s->funding_privkey);
24+
fromwire_secret(ptr, max, &s->revocation_basepoint_secret);
25+
fromwire_secret(ptr, max, &s->payment_basepoint_secret);
26+
fromwire_secret(ptr, max, &s->delayed_payment_basepoint_secret);
27+
fromwire_secret(ptr, max, &s->htlc_basepoint_secret);
28+
}
29+
#else /* !DEVELOPER */
30+
/* Generate code refers to this, but should never be called! */
31+
void towire_secrets(u8 **pptr, const struct secrets *s)
32+
{
33+
abort();
34+
}
35+
36+
void fromwire_secrets(const u8 **ptr, size_t *max, struct secrets *s)
37+
{
38+
abort();
39+
}
40+
#endif
41+
42+
struct keys {
43+
struct privkey f, r, h, p, d;
44+
struct sha256 shaseed;
45+
};
46+
47+
static void derive_keys(const struct secret *seed, struct keys *keys)
48+
{
49+
hkdf_sha256(keys, sizeof(*keys), NULL, 0, seed, sizeof(*seed),
50+
"c-lightning", strlen("c-lightning"));
51+
52+
#if DEVELOPER
53+
if (dev_force_channel_secrets) {
54+
keys->f = dev_force_channel_secrets->funding_privkey;
55+
keys->r.secret = dev_force_channel_secrets->revocation_basepoint_secret;
56+
keys->p.secret = dev_force_channel_secrets->payment_basepoint_secret;
57+
keys->h.secret = dev_force_channel_secrets->htlc_basepoint_secret;
58+
keys->d.secret = dev_force_channel_secrets->delayed_payment_basepoint_secret;
59+
}
60+
if (dev_force_channel_secrets_shaseed)
61+
keys->shaseed = *dev_force_channel_secrets_shaseed;
62+
#endif
63+
}
64+
765
bool derive_basepoints(const struct secret *seed,
866
struct pubkey *funding_pubkey,
967
struct basepoints *basepoints,
1068
struct secrets *secrets,
1169
struct sha256 *shaseed)
1270
{
13-
struct keys {
14-
struct privkey f, r, h, p, d;
15-
struct sha256 shaseed;
16-
} keys;
71+
struct keys keys;
1772

18-
hkdf_sha256(&keys, sizeof(keys), NULL, 0, seed, sizeof(*seed),
19-
"c-lightning", strlen("c-lightning"));
73+
derive_keys(seed, &keys);
2074

2175
if (secrets) {
2276
secrets->funding_privkey = keys.f;
@@ -95,13 +149,9 @@ bool derive_payment_basepoint(const struct secret *seed,
95149
struct pubkey *payment_basepoint,
96150
struct secret *payment_secret)
97151
{
98-
struct keys {
99-
struct privkey f, r, h, p, d;
100-
struct sha256 shaseed;
101-
} keys;
152+
struct keys keys;
102153

103-
hkdf_sha256(&keys, sizeof(keys), NULL, 0, seed, sizeof(*seed),
104-
"c-lightning", strlen("c-lightning"));
154+
derive_keys(seed, &keys);
105155

106156
if (payment_basepoint) {
107157
if (!pubkey_from_privkey(&keys.p, payment_basepoint))
@@ -118,13 +168,9 @@ bool derive_delayed_payment_basepoint(const struct secret *seed,
118168
struct pubkey *delayed_payment_basepoint,
119169
struct secret *delayed_payment_secret)
120170
{
121-
struct keys {
122-
struct privkey f, r, h, p, d;
123-
struct sha256 shaseed;
124-
} keys;
171+
struct keys keys;
125172

126-
hkdf_sha256(&keys, sizeof(keys), NULL, 0, seed, sizeof(*seed),
127-
"c-lightning", strlen("c-lightning"));
173+
derive_keys(seed, &keys);
128174

129175
if (delayed_payment_basepoint) {
130176
if (!pubkey_from_privkey(&keys.d, delayed_payment_basepoint))
@@ -139,13 +185,10 @@ bool derive_delayed_payment_basepoint(const struct secret *seed,
139185

140186
bool derive_shaseed(const struct secret *seed, struct sha256 *shaseed)
141187
{
142-
struct keys {
143-
struct privkey f, r, h, p, d;
144-
struct sha256 shaseed;
145-
} keys;
188+
struct keys keys;
189+
190+
derive_keys(seed, &keys);
146191

147-
hkdf_sha256(&keys, sizeof(keys), NULL, 0, seed, sizeof(*seed),
148-
"c-lightning", strlen("c-lightning"));
149192
*shaseed = keys.shaseed;
150193
return true;
151194
}
@@ -154,18 +197,17 @@ bool derive_funding_key(const struct secret *seed,
154197
struct pubkey *funding_pubkey,
155198
struct privkey *funding_privkey)
156199
{
157-
struct privkey f;
200+
struct keys keys;
158201

159-
hkdf_sha256(&f, sizeof(f), NULL, 0, seed, sizeof(*seed),
160-
"c-lightning", strlen("c-lightning"));
202+
derive_keys(seed, &keys);
161203

162204
if (funding_pubkey) {
163-
if (!pubkey_from_privkey(&f, funding_pubkey))
205+
if (!pubkey_from_privkey(&keys.f, funding_pubkey))
164206
return false;
165207
}
166208

167209
if (funding_privkey)
168-
*funding_privkey = f;
210+
*funding_privkey = keys.f;
169211

170212
return true;
171213
}
@@ -174,13 +216,9 @@ bool derive_revocation_basepoint(const struct secret *seed,
174216
struct pubkey *revocation_basepoint,
175217
struct secret *revocation_secret)
176218
{
177-
struct keys {
178-
struct privkey f, r, h, p, d;
179-
struct sha256 shaseed;
180-
} keys;
219+
struct keys keys;
181220

182-
hkdf_sha256(&keys, sizeof(keys), NULL, 0, seed, sizeof(*seed),
183-
"c-lightning", strlen("c-lightning"));
221+
derive_keys(seed, &keys);
184222

185223
if (revocation_basepoint) {
186224
if (!pubkey_from_privkey(&keys.r, revocation_basepoint))
@@ -197,13 +235,9 @@ bool derive_htlc_basepoint(const struct secret *seed,
197235
struct pubkey *htlc_basepoint,
198236
struct secret *htlc_secret)
199237
{
200-
struct keys {
201-
struct privkey f, r, h, p, d;
202-
struct sha256 shaseed;
203-
} keys;
238+
struct keys keys;
204239

205-
hkdf_sha256(&keys, sizeof(keys), NULL, 0, seed, sizeof(*seed),
206-
"c-lightning", strlen("c-lightning"));
240+
derive_keys(seed, &keys);
207241

208242
if (htlc_basepoint) {
209243
if (!pubkey_from_privkey(&keys.h, htlc_basepoint))

common/derive_basepoints.h

+10
Original file line numberDiff line numberDiff line change
@@ -155,4 +155,14 @@ void towire_basepoints(u8 **pptr, const struct basepoints *b);
155155
void fromwire_basepoints(const u8 **ptr, size_t *max,
156156
struct basepoints *b);
157157

158+
/* For --dev-force-channel-secrets. */
159+
#if DEVELOPER
160+
extern struct secrets *dev_force_channel_secrets;
161+
extern struct sha256 *dev_force_channel_secrets_shaseed;
162+
#endif
163+
164+
/* Note: these abort if !DEVELOPER */
165+
void towire_secrets(u8 **pptr, const struct secrets *s);
166+
void fromwire_secrets(const u8 **ptr, size_t *max, struct secrets *s);
167+
158168
#endif /* LIGHTNING_COMMON_DERIVE_BASEPOINTS_H */

common/test/run-derive_basepoints.c

+12
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,24 @@
1111
/* Generated stub for fromwire_fail */
1212
const void *fromwire_fail(const u8 **cursor UNNEEDED, size_t *max UNNEEDED)
1313
{ fprintf(stderr, "fromwire_fail called!\n"); abort(); }
14+
/* Generated stub for fromwire_privkey */
15+
void fromwire_privkey(const u8 **cursor UNNEEDED, size_t *max UNNEEDED, struct privkey *privkey UNNEEDED)
16+
{ fprintf(stderr, "fromwire_privkey called!\n"); abort(); }
1417
/* Generated stub for fromwire_pubkey */
1518
void fromwire_pubkey(const u8 **cursor UNNEEDED, size_t *max UNNEEDED, struct pubkey *pubkey UNNEEDED)
1619
{ fprintf(stderr, "fromwire_pubkey called!\n"); abort(); }
20+
/* Generated stub for fromwire_secret */
21+
void fromwire_secret(const u8 **cursor UNNEEDED, size_t *max UNNEEDED, struct secret *secret UNNEEDED)
22+
{ fprintf(stderr, "fromwire_secret called!\n"); abort(); }
23+
/* Generated stub for towire_privkey */
24+
void towire_privkey(u8 **pptr UNNEEDED, const struct privkey *privkey UNNEEDED)
25+
{ fprintf(stderr, "towire_privkey called!\n"); abort(); }
1726
/* Generated stub for towire_pubkey */
1827
void towire_pubkey(u8 **pptr UNNEEDED, const struct pubkey *pubkey UNNEEDED)
1928
{ fprintf(stderr, "towire_pubkey called!\n"); abort(); }
29+
/* Generated stub for towire_secret */
30+
void towire_secret(u8 **pptr UNNEEDED, const struct secret *secret UNNEEDED)
31+
{ fprintf(stderr, "towire_secret called!\n"); abort(); }
2032
/* AUTOGENERATED MOCKS END */
2133

2234
STRUCTEQ_DEF(basepoints, 0,

hsmd/hsm_wire.csv

+2
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ hsm_init,11
1111
hsm_init,,bip32_key_version,struct bip32_key_version
1212
hsm_init,,dev_force_privkey,?struct privkey
1313
hsm_init,,dev_force_bip32_seed,?struct secret
14+
hsm_init,,dev_force_channel_secrets,?struct secrets
15+
hsm_init,,dev_force_channel_secrets_shaseed,?struct sha256
1416

1517
#include <common/bip32.h>
1618
hsm_init_reply,111

hsmd/hsmd.c

+5-1
Original file line numberDiff line numberDiff line change
@@ -572,6 +572,8 @@ static struct io_plan *init_hsm(struct io_conn *conn,
572572
struct pubkey key;
573573
struct privkey *privkey;
574574
struct secret *seed;
575+
struct secrets *secrets;
576+
struct sha256 *shaseed;
575577

576578
/* This must be lightningd. */
577579
assert(is_lightningd(c));
@@ -581,12 +583,14 @@ static struct io_plan *init_hsm(struct io_conn *conn,
581583
* an extension of the simple comma-separated format output by the
582584
* BOLT tools/extract-formats.py tool. */
583585
if (!fromwire_hsm_init(NULL, msg_in, &bip32_key_version,
584-
&privkey, &seed))
586+
&privkey, &seed, &secrets, &shaseed))
585587
return bad_req(conn, c, msg_in);
586588

587589
#if DEVELOPER
588590
dev_force_privkey = privkey;
589591
dev_force_bip32_seed = seed;
592+
dev_force_channel_secrets = secrets;
593+
dev_force_channel_secrets_shaseed = shaseed;
590594
#endif
591595
maybe_create_new_hsm();
592596
load_hsm();

lightningd/hsm_control.c

+4-2
Original file line numberDiff line numberDiff line change
@@ -97,9 +97,11 @@ void hsm_init(struct lightningd *ld)
9797
&ld->topology->bitcoind->chainparams->bip32_key_version,
9898
#if DEVELOPER
9999
ld->dev_force_privkey,
100-
ld->dev_force_bip32_seed
100+
ld->dev_force_bip32_seed,
101+
ld->dev_force_channel_secrets,
102+
ld->dev_force_channel_secrets_shaseed
101103
#else
102-
NULL, NULL
104+
NULL, NULL, NULL, NULL
103105
#endif
104106
)))
105107
err(1, "Writing init msg to hsm");

lightningd/lightningd.c

+2
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,8 @@ static struct lightningd *new_lightningd(const tal_t *ctx)
120120
ld->dev_gossip_time = 0;
121121
ld->dev_force_privkey = NULL;
122122
ld->dev_force_bip32_seed = NULL;
123+
ld->dev_force_channel_secrets = NULL;
124+
ld->dev_force_channel_secrets_shaseed = NULL;
123125
#endif
124126

125127
/*~ These are CCAN lists: an embedded double-linked list. It's not

lightningd/lightningd.h

+4
Original file line numberDiff line numberDiff line change
@@ -211,6 +211,10 @@ struct lightningd {
211211

212212
/* This is the forced bip32 seed for the node. */
213213
struct secret *dev_force_bip32_seed;
214+
215+
/* These are the forced channel secrets for the node. */
216+
struct secrets *dev_force_channel_secrets;
217+
struct sha256 *dev_force_channel_secrets_shaseed;
214218
#endif /* DEVELOPER */
215219

216220
/* tor support */

lightningd/options.c

+39
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
#include <ccan/tal/path/path.h>
1313
#include <ccan/tal/str/str.h>
1414
#include <common/configdir.h>
15+
#include <common/derive_basepoints.h>
1516
#include <common/json_command.h>
1617
#include <common/jsonrpc_errors.h>
1718
#include <common/memleak.h>
@@ -460,6 +461,42 @@ static char *opt_force_bip32_seed(const char *optarg, struct lightningd *ld)
460461
return NULL;
461462
}
462463

464+
static char *opt_force_channel_secrets(const char *optarg,
465+
struct lightningd *ld)
466+
{
467+
char **strs;
468+
tal_free(ld->dev_force_channel_secrets);
469+
tal_free(ld->dev_force_channel_secrets_shaseed);
470+
ld->dev_force_channel_secrets = tal(ld, struct secrets);
471+
ld->dev_force_channel_secrets_shaseed = tal(ld, struct sha256);
472+
473+
strs = tal_strsplit(tmpctx, optarg, "/", STR_EMPTY_OK);
474+
if (tal_count(strs) != 7) /* Last is NULL */
475+
return "Expected 6 hex secrets separated by /";
476+
477+
if (!hex_decode(strs[0], strlen(strs[0]),
478+
&ld->dev_force_channel_secrets->funding_privkey,
479+
sizeof(ld->dev_force_channel_secrets->funding_privkey))
480+
|| !hex_decode(strs[1], strlen(strs[1]),
481+
&ld->dev_force_channel_secrets->revocation_basepoint_secret,
482+
sizeof(ld->dev_force_channel_secrets->revocation_basepoint_secret))
483+
|| !hex_decode(strs[2], strlen(strs[2]),
484+
&ld->dev_force_channel_secrets->payment_basepoint_secret,
485+
sizeof(ld->dev_force_channel_secrets->payment_basepoint_secret))
486+
|| !hex_decode(strs[3], strlen(strs[3]),
487+
&ld->dev_force_channel_secrets->delayed_payment_basepoint_secret,
488+
sizeof(ld->dev_force_channel_secrets->delayed_payment_basepoint_secret))
489+
|| !hex_decode(strs[4], strlen(strs[4]),
490+
&ld->dev_force_channel_secrets->htlc_basepoint_secret,
491+
sizeof(ld->dev_force_channel_secrets->htlc_basepoint_secret))
492+
|| !hex_decode(strs[5], strlen(strs[5]),
493+
ld->dev_force_channel_secrets_shaseed,
494+
sizeof(*ld->dev_force_channel_secrets_shaseed)))
495+
return "Expected 6 hex secrets separated by /";
496+
497+
return NULL;
498+
}
499+
463500
static void dev_register_opts(struct lightningd *ld)
464501
{
465502
opt_register_noarg("--dev-no-reconnect", opt_set_invbool,
@@ -500,6 +537,8 @@ static void dev_register_opts(struct lightningd *ld)
500537
"Force HSM to use this as node private key");
501538
opt_register_arg("--dev-force-bip32-seed", opt_force_bip32_seed, NULL, ld,
502539
"Force HSM to use this as bip32 seed");
540+
opt_register_arg("--dev-force-channel-secrets", opt_force_channel_secrets, NULL, ld,
541+
"Force HSM to use these for all per-channel secrets");
503542
}
504543
#endif
505544

0 commit comments

Comments
 (0)