forked from ElementsProject/lightning
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhsmtool.c
842 lines (720 loc) · 25.5 KB
/
hsmtool.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
#include "config.h"
#include <bitcoin/tx.h>
#include <ccan/array_size/array_size.h>
#include <ccan/crypto/hkdf_sha256/hkdf_sha256.h>
#include <ccan/err/err.h>
#include <ccan/noerr/noerr.h>
#include <ccan/read_write_all/read_write_all.h>
#include <ccan/rune/rune.h>
#include <ccan/tal/grab_file/grab_file.h>
#include <ccan/tal/path/path.h>
#include <ccan/tal/str/str.h>
#include <common/bech32.h>
#include <common/bech32_util.h>
#include <common/codex32.h>
#include <common/configdir.h>
#include <common/derive_basepoints.h>
#include <common/descriptor_checksum.h>
#include <common/errcode.h>
#include <common/hsm_encryption.h>
#include <common/node_id.h>
#include <common/utils.h>
#include <errno.h>
#include <fcntl.h>
#include <inttypes.h>
#include <unistd.h>
#include <wally_bip32.h>
#include <wally_bip39.h>
#define ERROR_USAGE 2
#define ERROR_LIBSODIUM 3
#define ERROR_LIBWALLY 4
#define ERROR_KEYDERIV 5
#define ERROR_LANG_NOT_SUPPORTED 6
#define ERROR_TERM 7
static void show_usage(const char *progname)
{
printf("%s <method> [arguments]\n", progname);
printf("methods:\n");
printf(" - decrypt <path/to/hsm_secret>\n");
printf(" - encrypt <path/to/hsm_secret>\n");
printf(" - dumpcommitments <node id> <channel dbid> <depth> "
"<path/to/hsm_secret>\n");
printf(" - guesstoremote <P2WPKH address> <node id> <tries> "
"<path/to/hsm_secret>\n");
printf(" - generatehsm <path/to/new/hsm_secret>\n");
printf(" - checkhsm <path/to/new/hsm_secret>\n");
printf(" - dumponchaindescriptors [--show-secrets] <path/to/hsm_secret> [network]\n");
printf(" - makerune <path/to/hsm_secret>\n");
printf(" - getcodexsecret <path/to/hsm_secret> <id>\n");
printf(" - getemergencyrecover <path/to/emergency.recover>\n");
exit(0);
}
static bool ensure_hsm_secret_exists(int fd, const char *path)
{
const char *config_dir = path_dirname(NULL, path);
if (fsync(fd) != 0) {
close(fd);
return false;
}
if (close(fd) != 0)
return false;
fd = open(config_dir, O_RDONLY);
if (fd < 0)
return false;
if (fsync(fd) != 0) {
close(fd);
return false;
}
close(fd);
tal_free(config_dir);
return true;
}
static void grab_hsm_file(const char *hsm_secret_path,
void *dst, size_t dstlen)
{
u8 *contents = grab_file(tmpctx, hsm_secret_path);
if (!contents)
errx(EXITCODE_ERROR_HSM_FILE, "Reading hsm_secret");
/* grab_file always appends a NUL char for convenience */
if (tal_bytelen(contents) != dstlen + 1)
errx(EXITCODE_ERROR_HSM_FILE,
"hsm_secret invalid length %zu (expected %zu)",
tal_bytelen(contents)-1, dstlen);
memcpy(dst, contents, dstlen);
}
static void get_unencrypted_hsm_secret(struct secret *hsm_secret,
const char *hsm_secret_path)
{
grab_hsm_file(hsm_secret_path, hsm_secret, sizeof(*hsm_secret));
}
/* Derive the encryption key from the password provided, and try to decrypt
* the cipher. */
static void get_encrypted_hsm_secret(struct secret *hsm_secret,
const char *hsm_secret_path,
const char *passwd)
{
struct secret key;
struct encrypted_hsm_secret encrypted_secret;
char *err;
int exit_code;
grab_hsm_file(hsm_secret_path,
&encrypted_secret, sizeof(encrypted_secret));
exit_code = hsm_secret_encryption_key_with_exitcode(passwd, &key, &err);
if (exit_code > 0)
errx(exit_code, "%s", err);
if (!decrypt_hsm_secret(&key, &encrypted_secret, hsm_secret))
errx(ERROR_LIBSODIUM, "Could not retrieve the seed. Wrong password ?");
}
/* Taken from hsmd. */
static void get_channel_seed(struct secret *channel_seed, struct node_id *peer_id,
u64 dbid, struct secret *hsm_secret)
{
struct secret channel_base;
u8 input[sizeof(peer_id->k) + sizeof(dbid)];
/*~ Again, "per-peer" should be "per-channel", but Hysterical Raisins */
const char *info = "per-peer seed";
/*~ We use the DER encoding of the pubkey, because it's platform
* independent. Since the dbid is unique, however, it's completely
* unnecessary, but again, existing users can't be broken. */
/* FIXME: lnd has a nicer BIP32 method for deriving secrets which we
* should migrate to. */
hkdf_sha256(&channel_base, sizeof(struct secret), NULL, 0,
hsm_secret, sizeof(*hsm_secret),
/*~ Initially, we didn't support multiple channels per
* peer at all: a channel had to be completely forgotten
* before another could exist. That was slightly relaxed,
* but the phrase "peer seed" is wired into the seed
* generation here, so we need to keep it that way for
* existing clients, rather than using "channel seed". */
"peer seed", strlen("peer seed"));
memcpy(input, peer_id->k, sizeof(peer_id->k));
BUILD_ASSERT(sizeof(peer_id->k) == PUBKEY_CMPR_LEN);
/*~ For all that talk about platform-independence, note that this
* field is endian-dependent! But let's face it, little-endian won.
* In related news, we don't support EBCDIC or middle-endian. */
memcpy(input + PUBKEY_CMPR_LEN, &dbid, sizeof(dbid));
hkdf_sha256(channel_seed, sizeof(*channel_seed),
input, sizeof(input),
&channel_base, sizeof(channel_base),
info, strlen(info));
}
/* We detect an encrypted hsm_secret as a hsm_secret which is 73-bytes long. */
static bool hsm_secret_is_encrypted(const char *hsm_secret_path)
{
switch (is_hsm_secret_encrypted(hsm_secret_path)) {
case -1:
err(EXITCODE_ERROR_HSM_FILE, "Cannot open '%s'", hsm_secret_path);
case 1:
return true;
case 0: {
/* Extra sanity check on HSM file! */
struct stat st;
stat(hsm_secret_path, &st);
if (st.st_size != 32)
errx(EXITCODE_ERROR_HSM_FILE,
"Invalid hsm_secret '%s' (neither plaintext "
"nor encrypted).", hsm_secret_path);
return false;
}
}
abort();
}
/* If encrypted, ask for a passphrase */
static void get_hsm_secret(struct secret *hsm_secret,
const char *hsm_secret_path)
{
/* This checks the file existence, too. */
if (hsm_secret_is_encrypted(hsm_secret_path)) {
int exit_code;
char *err, *passwd;
printf("Enter hsm_secret password:\n");
fflush(stdout);
passwd = read_stdin_pass_with_exit_code(&err, &exit_code);
if (!passwd)
errx(exit_code, "%s", err);
get_encrypted_hsm_secret(hsm_secret, hsm_secret_path, passwd);
free(passwd);
} else {
get_unencrypted_hsm_secret(hsm_secret, hsm_secret_path);
}
}
static int decrypt_hsm(const char *hsm_secret_path)
{
int fd;
struct secret hsm_secret;
char *passwd, *err;
const char *dir, *backup;
int exit_code = 0;
/* This checks the file existence, too. */
if (!hsm_secret_is_encrypted(hsm_secret_path))
errx(ERROR_USAGE, "hsm_secret is not encrypted");
printf("Enter hsm_secret password:\n");
fflush(stdout);
passwd = read_stdin_pass_with_exit_code(&err, &exit_code);
if (!passwd)
errx(exit_code, "%s", err);
dir = path_dirname(NULL, hsm_secret_path);
backup = path_join(dir, dir, "hsm_secret.backup");
get_encrypted_hsm_secret(&hsm_secret, hsm_secret_path, passwd);
/* Once the encryption key derived, we don't need it anymore. */
if (passwd)
free(passwd);
/* Create a backup file, "just in case". */
rename(hsm_secret_path, backup);
fd = open(hsm_secret_path, O_CREAT|O_EXCL|O_WRONLY, 0400);
if (fd < 0)
errx(EXITCODE_ERROR_HSM_FILE, "Could not open new hsm_secret");
if (!write_all(fd, &hsm_secret, sizeof(hsm_secret))) {
unlink_noerr(hsm_secret_path);
close(fd);
rename("hsm_secret.backup", hsm_secret_path);
errx(EXITCODE_ERROR_HSM_FILE,
"Failure writing plaintext seed to hsm_secret.");
}
/* Be as paranoïd as in hsmd with the file state on disk. */
if (!ensure_hsm_secret_exists(fd, hsm_secret_path)) {
unlink_noerr(hsm_secret_path);
rename(backup, hsm_secret_path);
errx(EXITCODE_ERROR_HSM_FILE,
"Could not ensure hsm_secret existence.");
}
unlink_noerr(backup);
tal_free(dir);
printf("Successfully decrypted hsm_secret, be careful now :-).\n");
return 0;
}
static int make_codexsecret(const char *hsm_secret_path,
const char *id)
{
struct secret hsm_secret;
char *bip93;
const char *err;
get_hsm_secret(&hsm_secret, hsm_secret_path);
err = codex32_secret_encode(tmpctx, "cl", id, 0, hsm_secret.data, 32, &bip93);
if (err)
errx(ERROR_USAGE, "%s", err);
printf("%s\n", bip93);
return 0;
}
static int getemergencyrecover(const char *emer_rec_path)
{
u8 *scb = grab_file(tmpctx, emer_rec_path);
char *output, *hrp = "clnemerg";
if (!scb) {
errx(EXITCODE_ERROR_HSM_FILE, "Reading emergency.recover");
} else {
/* grab_file adds nul term */
tal_resize(&scb, tal_bytelen(scb) - 1);
}
u5 *data = tal_arr(tmpctx, u5, 0);
bech32_push_bits(&data, scb, tal_bytelen(scb) * 8);
output = tal_arr(tmpctx, char, strlen(hrp) + tal_count(data) + 8);
bech32_encode(output, hrp, data, tal_count(data), (size_t)-1,
BECH32_ENCODING_BECH32);
printf("%s\n", output);
return 0;
}
static int encrypt_hsm(const char *hsm_secret_path)
{
int fd;
struct secret key, hsm_secret;
struct encrypted_hsm_secret encrypted_hsm_secret;
char *passwd, *passwd_confirmation, *err;
const char *dir, *backup;
int exit_code = 0;
/* This checks the file existence, too. */
if (hsm_secret_is_encrypted(hsm_secret_path))
errx(ERROR_USAGE, "hsm_secret is already encrypted");
printf("Enter hsm_secret password:\n");
fflush(stdout);
passwd = read_stdin_pass_with_exit_code(&err, &exit_code);
if (!passwd)
errx(exit_code, "%s", err);
printf("Confirm hsm_secret password:\n");
fflush(stdout);
passwd_confirmation = read_stdin_pass_with_exit_code(&err, &exit_code);
if (!passwd_confirmation)
errx(exit_code, "%s", err);
if (!streq(passwd, passwd_confirmation))
errx(ERROR_USAGE, "Passwords confirmation mismatch.");
get_unencrypted_hsm_secret(&hsm_secret, hsm_secret_path);
dir = path_dirname(NULL, hsm_secret_path);
backup = path_join(dir, dir, "hsm_secret.backup");
/* Derive the encryption key from the password provided, and try to encrypt
* the seed. */
exit_code = hsm_secret_encryption_key_with_exitcode(passwd, &key, &err);
if (exit_code > 0)
errx(exit_code, "%s", err);
if (!encrypt_hsm_secret(&key, &hsm_secret, &encrypted_hsm_secret))
errx(ERROR_LIBSODIUM, "Could not encrypt the hsm_secret seed.");
/* Once the encryption key derived, we don't need it anymore. */
free(passwd);
free(passwd_confirmation);
/* Create a backup file, "just in case". */
rename(hsm_secret_path, backup);
fd = open(hsm_secret_path, O_CREAT|O_EXCL|O_WRONLY, 0400);
if (fd < 0)
errx(EXITCODE_ERROR_HSM_FILE, "Could not open new hsm_secret");
/* Write the encrypted hsm_secret. */
if (!write_all(fd, encrypted_hsm_secret.data,
sizeof(encrypted_hsm_secret.data))) {
unlink_noerr(hsm_secret_path);
close(fd);
rename(backup, hsm_secret_path);
errx(EXITCODE_ERROR_HSM_FILE, "Failure writing cipher to hsm_secret.");
}
/* Be as paranoïd as in hsmd with the file state on disk. */
if (!ensure_hsm_secret_exists(fd, hsm_secret_path)) {
unlink_noerr(hsm_secret_path);
rename(backup, hsm_secret_path);
errx(EXITCODE_ERROR_HSM_FILE, "Could not ensure hsm_secret existence.");
}
unlink_noerr(backup);
tal_free(dir);
printf("Successfully encrypted hsm_secret. You'll now have to pass the "
"--encrypted-hsm startup option.\n");
return 0;
}
static int dump_commitments_infos(struct node_id *node_id, u64 channel_id,
u64 depth, char *hsm_secret_path)
{
struct sha256 shaseed;
struct secret hsm_secret, channel_seed, per_commitment_secret;
struct pubkey per_commitment_point;
secp256k1_ctx = secp256k1_context_create(SECP256K1_CONTEXT_VERIFY
| SECP256K1_CONTEXT_SIGN);
get_hsm_secret(&hsm_secret, hsm_secret_path);
get_channel_seed(&channel_seed, node_id, channel_id, &hsm_secret);
derive_shaseed(&channel_seed, &shaseed);
printf("shaseed: %s\n", fmt_sha256(tmpctx, &shaseed));
for (u64 i = 0; i < depth; i++) {
if (!per_commit_secret(&shaseed, &per_commitment_secret, i))
errx(ERROR_KEYDERIV, "Could not derive secret #%"PRIu64, i);
printf("commit secret #%"PRIu64": %s\n",
i, tal_hexstr(tmpctx, per_commitment_secret.data,
sizeof(per_commitment_secret.data)));
if (!per_commit_point(&shaseed, &per_commitment_point, i))
errx(ERROR_KEYDERIV, "Could not derive point #%"PRIu64, i);
printf("commit point #%"PRIu64": %s\n",
i, fmt_pubkey(tmpctx, &per_commitment_point));
}
return 0;
}
/* In case of an unilateral close from the remote side while we suffered a
* loss of data, this tries to recover the private key from the `to_remote`
* output.
* This basically iterates over every `dbid` to derive the channel_seed and
* then derives the payment basepoint to compare to the pubkey hash specified
* in the witness programm.
* Note that since a node generates the key for the to_remote output from its
* *local* per_commitment_point, there is nothing we can do if
* `option_static_remotekey` was not negotiated.
*
* :param address: The bech32 address of the v0 P2WPKH witness programm
* :param node_id: The id of the node with which the channel was established
* :param tries: How many dbids to try.
* :param hsm_secret_path: The path to the hsm_secret
* :param passwd: The *optional* hsm_secret password
*/
static int guess_to_remote(const char *address, struct node_id *node_id,
u64 tries, char *hsm_secret_path)
{
struct secret hsm_secret, channel_seed, basepoint_secret;
struct pubkey basepoint;
struct ripemd160 pubkeyhash;
/* We only support P2WPKH, hence 20. */
u8 goal_pubkeyhash[20];
/* See common/bech32.h for buffer size. */
char hrp[strlen(address) - 6];
int witver;
size_t witlen;
/* Get the hrp to accept addresses from any network. */
if (bech32_decode(hrp, goal_pubkeyhash, &witlen, address, 90) != BECH32_ENCODING_BECH32)
errx(ERROR_USAGE, "Could not get address' network");
if (segwit_addr_decode(&witver, goal_pubkeyhash, &witlen, hrp, address) != 1)
errx(ERROR_USAGE, "Wrong bech32 address");
secp256k1_ctx = secp256k1_context_create(SECP256K1_CONTEXT_VERIFY
| SECP256K1_CONTEXT_SIGN);
get_hsm_secret(&hsm_secret, hsm_secret_path);
for (u64 dbid = 1; dbid < tries ; dbid++) {
get_channel_seed(&channel_seed, node_id, dbid, &hsm_secret);
if (!derive_payment_basepoint(&channel_seed,
&basepoint, &basepoint_secret))
errx(ERROR_KEYDERIV, "Could not derive basepoints for dbid %"PRIu64
" and channel seed %s.", dbid,
fmt_secret(tmpctx, &channel_seed));
pubkey_to_hash160(&basepoint, &pubkeyhash);
if (memcmp(pubkeyhash.u.u8, goal_pubkeyhash, 20) == 0) {
printf("bech32 : %s\n", address);
printf("pubkey hash : %s\n",
tal_hexstr(tmpctx, pubkeyhash.u.u8, 20));
printf("pubkey : %s \n",
fmt_pubkey(tmpctx, &basepoint));
printf("privkey : %s \n",
fmt_secret(tmpctx, &basepoint_secret));
return 0;
}
}
printf("Could not find any basepoint matching the provided witness programm.\n"
"Are you sure that the channel used `option_static_remotekey` ?\n");
return 1;
}
static void get_words(struct words **words) {
struct wordlist_lang {
char *abbr;
char *name;
};
struct wordlist_lang languages[] = {
{"en", "English"},
{"es", "Spanish"},
{"fr", "French"},
{"it", "Italian"},
{"jp", "Japanese"},
{"zhs", "Chinese Simplified"},
{"zht", "Chinese Traditional"},
};
printf("Select your language:\n");
for (size_t i = 0; i < ARRAY_SIZE(languages); i++) {
printf(" %zu) %s (%s)\n", i, languages[i].name, languages[i].abbr);
}
printf("Select [0-%zu]: ", ARRAY_SIZE(languages)-1);
fflush(stdout);
char *selected = NULL;
size_t size = 0;
size_t characters = getline(&selected, &size, stdin);
if (characters < 0)
errx(ERROR_USAGE, "Could not read line from stdin.");
/* To distinguish success/failure after call */
errno = 0;
char *endptr;
long val = strtol(selected, &endptr, 10);
if (errno == ERANGE || (errno != 0 && val == 0) || endptr == selected || val < 0 || val >= ARRAY_SIZE(languages))
errx(ERROR_USAGE, "Invalid language selection, select one from the list [0-6].");
free(selected);
bip39_get_wordlist(languages[val].abbr, words);
}
static void get_mnemonic(char *mnemonic) {
char *line = NULL;
size_t line_size = 0;
printf("Introduce your BIP39 word list separated by space (at least 12 words):\n");
fflush(stdout);
size_t characters = getline(&line, &line_size, stdin);
if (characters < 0)
errx(ERROR_USAGE, "Could not read line from stdin.");
line[characters-1] = '\0';
strcpy(mnemonic, line);
free(line);
}
static void read_mnemonic(char *mnemonic) {
/* Get words for the mnemonic language */
struct words *words;
get_words(&words);
/* Get mnemonic */
get_mnemonic(mnemonic);
if (bip39_mnemonic_validate(words, mnemonic) != 0) {
errx(ERROR_USAGE, "Invalid mnemonic: \"%s\"", mnemonic);
}
}
static int generate_hsm(const char *hsm_secret_path)
{
char mnemonic[BIP39_WORDLIST_LEN];
char *passphrase, *err;
int exit_code = 0;
read_mnemonic(mnemonic);
printf("Warning: remember that different passphrases yield different "
"bitcoin wallets.\n");
printf("If left empty, no password is used (echo is disabled).\n");
printf("Enter your passphrase: \n");
fflush(stdout);
passphrase = read_stdin_pass_with_exit_code(&err, &exit_code);
if (!passphrase)
errx(exit_code, "%s", err);
if (strlen(passphrase) == 0) {
free(passphrase);
passphrase = NULL;
}
u8 bip32_seed[BIP39_SEED_LEN_512];
size_t bip32_seed_len;
if (bip39_mnemonic_to_seed(mnemonic, passphrase, bip32_seed, sizeof(bip32_seed), &bip32_seed_len) != WALLY_OK)
errx(ERROR_LIBWALLY, "Unable to derive BIP32 seed from BIP39 mnemonic");
int fd = open(hsm_secret_path, O_CREAT|O_EXCL|O_WRONLY, 0400);
if (fd < 0) {
errx(ERROR_USAGE, "Unable to create hsm_secret file");
}
/* Write only the first 32 bytes, length of the (plaintext) seed in the
* hsm_secret. */
if (!write_all(fd, bip32_seed, 32))
errx(ERROR_USAGE, "Error writing secret to hsm_secret file");
if (fsync(fd) != 0)
errx(ERROR_USAGE, "Error fsyncing hsm_secret file");
/* This should never fail if fsync succeeded. But paranoia is good, and bugs exist */
if (close(fd) != 0)
errx(ERROR_USAGE, "Error closing hsm_secret file");
printf("New hsm_secret file created at %s\n", hsm_secret_path);
printf("Use the `encrypt` command to encrypt the BIP32 seed if needed\n");
free(passphrase);
return 0;
}
static int dumponchaindescriptors(const char *hsm_secret_path,
const char *old_passwd UNUSED,
const u32 version, bool show_secrets)
{
struct secret hsm_secret;
u8 bip32_seed[BIP32_ENTROPY_LEN_256];
u32 salt = 0;
struct ext_key master_extkey;
char *enc_xkey, *descriptor;
struct descriptor_checksum checksum;
get_hsm_secret(&hsm_secret, hsm_secret_path);
/* We use m/0/0/k as the derivation tree for onchain funds. */
/* The root seed is derived from hsm_secret using hkdf.. */
do {
hkdf_sha256(bip32_seed, sizeof(bip32_seed),
&salt, sizeof(salt),
&hsm_secret, sizeof(hsm_secret),
"bip32 seed", strlen("bip32 seed"));
salt++;
/* ..Which is used to derive m/ */
} while (bip32_key_from_seed(bip32_seed, sizeof(bip32_seed),
version, 0, &master_extkey) != WALLY_OK);
if (show_secrets) {
if (bip32_key_to_base58(&master_extkey, BIP32_FLAG_KEY_PRIVATE,
&enc_xkey) != WALLY_OK)
errx(ERROR_LIBWALLY, "Can't encode xpriv");
} else {
if (bip32_key_to_base58(&master_extkey, BIP32_FLAG_KEY_PUBLIC,
&enc_xkey) != WALLY_OK)
errx(ERROR_LIBWALLY, "Can't encode xpub");
}
/* Now we format the descriptor strings (we only ever create P2TR, P2WPKH, and
* P2SH-P2WPKH outputs). */
descriptor = tal_fmt(NULL, "wpkh(%s/0/0/*)", enc_xkey);
if (!descriptor_checksum(descriptor, strlen(descriptor), &checksum))
errx(ERROR_LIBWALLY, "Can't derive descriptor checksum for wpkh");
printf("%s#%s\n", descriptor, checksum.csum);
tal_free(descriptor);
descriptor = tal_fmt(NULL, "sh(wpkh(%s/0/0/*))", enc_xkey);
if (!descriptor_checksum(descriptor, strlen(descriptor), &checksum))
errx(ERROR_LIBWALLY, "Can't derive descriptor checksum for sh(wpkh)");
printf("%s#%s\n", descriptor, checksum.csum);
tal_free(descriptor);
descriptor = tal_fmt(NULL, "tr(%s/0/0/*)", enc_xkey);
if (!descriptor_checksum(descriptor, strlen(descriptor), &checksum))
errx(ERROR_LIBWALLY, "Can't derive descriptor checksum for tr");
printf("%s#%s\n", descriptor, checksum.csum);
tal_free(descriptor);
wally_free_string(enc_xkey);
return 0;
}
static int check_hsm(const char *hsm_secret_path)
{
char mnemonic[BIP39_WORDLIST_LEN];
struct secret hsm_secret;
u8 bip32_seed[BIP39_SEED_LEN_512];
size_t bip32_seed_len;
int exit_code;
char *passphrase, *err;
get_hsm_secret(&hsm_secret, hsm_secret_path);
printf("Warning: remember that different passphrases yield different "
"bitcoin wallets.\n");
printf("If left empty, no password is used (echo is disabled).\n");
printf("Enter your passphrase: \n");
fflush(stdout);
passphrase = read_stdin_pass_with_exit_code(&err, &exit_code);
if (!passphrase)
errx(exit_code, "%s", err);
if (strlen(passphrase) == 0) {
free(passphrase);
passphrase = NULL;
}
read_mnemonic(mnemonic);
if (bip39_mnemonic_to_seed(mnemonic, passphrase, bip32_seed, sizeof(bip32_seed), &bip32_seed_len) != WALLY_OK)
errx(ERROR_LIBWALLY, "Unable to derive BIP32 seed from BIP39 mnemonic");
/* We only use first 32 bytes */
if (memcmp(bip32_seed, hsm_secret.data, sizeof(hsm_secret.data)) != 0)
errx(ERROR_KEYDERIV, "resulting hsm_secret did not match");
printf("OK\n");
free(passphrase);
return 0;
}
static int make_rune(const char *hsm_secret_path)
{
struct secret hsm_secret, derived_secret, rune_secret;
struct rune *master_rune, *rune;
/* Get hsm_secret */
get_hsm_secret(&hsm_secret, hsm_secret_path);
/* HSM derives a root secret for `makesecret` */
hkdf_sha256(&derived_secret, sizeof(struct secret), NULL, 0,
&hsm_secret, sizeof(hsm_secret),
"derived secrets", strlen("derived secrets"));
/* Commando derives secret using makesecret "commando" */
hkdf_sha256(&rune_secret, sizeof(struct secret), NULL, 0,
&derived_secret, sizeof(derived_secret),
"commando", strlen("commando"));
master_rune = rune_new(tmpctx,
rune_secret.data,
ARRAY_SIZE(rune_secret.data),
NULL);
rune = rune_derive_start(tmpctx, master_rune, "0");
printf("%s\n", rune_to_base64(tmpctx, rune));
return 0;
}
int main(int argc, char *argv[])
{
const char *method;
setup_locale();
err_set_progname(argv[0]);
method = argc > 1 ? argv[1] : NULL;
if (!method)
show_usage(argv[0]);
if (sodium_init() == -1)
errx(ERROR_LIBSODIUM,
"Could not initialize libsodium. Not enough entropy ?");
if (streq(method, "decrypt")) {
if (argc < 3)
show_usage(argv[0]);
return decrypt_hsm(argv[2]);
}
if (streq(method, "encrypt")) {
if (argc < 3)
show_usage(argv[0]);
return encrypt_hsm(argv[2]);
}
if (streq(method, "dumpcommitments")) {
/* node_id channel_id depth hsm_secret */
if (argc < 6)
show_usage(argv[0]);
struct node_id node_id;
if (!node_id_from_hexstr(argv[2], strlen(argv[2]), &node_id))
errx(ERROR_USAGE, "Bad node id");
return dump_commitments_infos(&node_id, atol(argv[3]), atol(argv[4]),
argv[5]);
}
if (streq(method, "guesstoremote")) {
/* address node_id depth hsm_secret */
if (argc < 6)
show_usage(argv[0]);
struct node_id node_id;
if (!node_id_from_hexstr(argv[3], strlen(argv[3]), &node_id))
errx(ERROR_USAGE, "Bad node id");
return guess_to_remote(argv[2], &node_id, atol(argv[4]),
argv[5]);
}
if (streq(method, "generatehsm")) {
if (argc != 3)
show_usage(argv[0]);
char *hsm_secret_path = argv[2];
/* if hsm_secret already exists we abort the process
* we do not want to lose someone else's funds */
struct stat st;
if (stat(hsm_secret_path, &st) == 0)
errx(ERROR_USAGE, "hsm_secret file at %s already exists", hsm_secret_path);
return generate_hsm(hsm_secret_path);
}
if (streq(method, "dumponchaindescriptors")) {
char *fname = NULL;
char *net = NULL;
bool show_secrets = false;
bool only_arguments = false;
u32 version;
if (argc < 3)
show_usage(argv[0]);
for (int i = 2; i < argc; ++i) {
char *next = argv[i];
if (only_arguments || next[0] != '-') {
// this is an argument
if (!fname) {
fname = next;
continue;
}
if (!net) {
net = next;
continue;
}
errx(ERROR_USAGE,
"Argument '%s' was not expected.", next);
}
if (streq(next, "--")) {
only_arguments = true;
continue;
}
// we are processing an option here
if (streq(next, "--show-secrets")) {
show_secrets = true;
continue;
}
errx(ERROR_USAGE, "Option '%s' is not recognized.",
next);
}
if (net && (streq(net, "testnet") || streq(net, "signet")))
version = BIP32_VER_TEST_PRIVATE;
else if (net && !streq(net, "bitcoin"))
errx(ERROR_USAGE, "Network '%s' not supported."
" Supported networks: bitcoin (default),"
" testnet and signet", net);
else
version = BIP32_VER_MAIN_PRIVATE;
return dumponchaindescriptors(fname, NULL, version, show_secrets);
}
if (streq(method, "checkhsm")) {
if (argc < 3)
show_usage(argv[0]);
return check_hsm(argv[2]);
}
if (streq(method, "makerune")) {
if (argc < 3)
show_usage(argv[0]);
return make_rune(argv[2]);
}
if(streq(method, "getcodexsecret")) {
if (argc < 4)
show_usage(argv[0]);
return make_codexsecret(argv[2], argv[3]);
}
if(streq(method, "getemergencyrecover")) {
if (argc < 3)
show_usage(argv[0]);
return getemergencyrecover(argv[2]);
}
show_usage(argv[0]);
}