Skip to content

Commit 6c15f1e

Browse files
committed
common/hsm_secret: remove grab_file_contents now it has inspired grab_file_raw!
Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
1 parent 42f9361 commit 6c15f1e

File tree

4 files changed

+12
-37
lines changed

4 files changed

+12
-37
lines changed

common/hsm_secret.c

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -497,15 +497,6 @@ const char *format_type_name(enum hsm_secret_type type)
497497
return "unknown";
498498
}
499499

500-
u8 *grab_file_contents(const tal_t *ctx, const char *filename, size_t *len)
501-
{
502-
u8 *contents = grab_file_raw(ctx, filename);
503-
if (len)
504-
*len = tal_bytelen(contents);
505-
506-
return contents;
507-
}
508-
509500
bool is_mnemonic_secret(size_t secret_len)
510501
{
511502
return secret_len == HSM_SECRET_MNEMONIC_SIZE;

common/hsm_secret.h

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -166,17 +166,6 @@ int is_legacy_hsm_secret_encrypted(const char *path);
166166
*/
167167
const char *format_type_name(enum hsm_secret_type type);
168168

169-
/**
170-
* Wrapper around grab_file that removes the NUL terminator.
171-
* @ctx - tal context for allocation
172-
* @filename - path to the file to read
173-
* @len - output parameter for the file length (excluding NUL terminator)
174-
*
175-
* Returns file contents with NUL terminator removed, or NULL on error.
176-
* Unlike grab_file, the returned data does not include the NUL terminator.
177-
*/
178-
u8 *grab_file_contents(const tal_t *ctx, const char *filename, size_t *len);
179-
180169
/**
181170
* Derive encryption key from passphrase using Argon2.
182171
* @ctx - tal context for allocation

hsmd/hsmd.c

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -432,17 +432,16 @@ static void load_hsm(const char *passphrase)
432432
enum hsm_secret_error err;
433433

434434
/* Read the hsm_secret file */
435-
size_t hsm_secret_len;
436-
hsm_secret_contents = grab_file_contents(tmpctx, "hsm_secret", &hsm_secret_len);
435+
hsm_secret_contents = grab_file_raw(tmpctx, "hsm_secret");
437436
if (!hsm_secret_contents) {
438437
hsmd_send_init_reply_failure(HSM_SECRET_ERR_INVALID_FORMAT, STATUS_FAIL_INTERNAL_ERROR,
439438
"Could not read hsm_secret: %s", strerror(errno));
440439
}
441440

442441
/* Extract the secret using the new hsm_secret module */
443442
hsms = extract_hsm_secret(tmpctx, hsm_secret_contents,
444-
hsm_secret_len,
445-
passphrase, &err);
443+
tal_bytelen(hsm_secret_contents),
444+
passphrase, &err);
446445
if (!hsms) {
447446
hsmd_send_init_reply_failure(err, STATUS_FAIL_INTERNAL_ERROR,
448447
"Failed to load hsm_secret: %s", hsm_secret_error_str(err));

tools/hsmtool.c

Lines changed: 9 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -87,8 +87,7 @@ static bool ensure_hsm_secret_exists(int fd, const char *path)
8787
/* Load hsm_secret using the unified interface */
8888
static struct hsm_secret *load_hsm_secret(const tal_t *ctx, const char *hsm_secret_path)
8989
{
90-
size_t contents_len;
91-
u8 *contents = grab_file_contents(tmpctx, hsm_secret_path, &contents_len);
90+
u8 *contents = grab_file_raw(tmpctx, hsm_secret_path);
9291
const char *passphrase = NULL;
9392
struct hsm_secret *hsms;
9493
enum hsm_secret_error error;
@@ -97,15 +96,15 @@ static struct hsm_secret *load_hsm_secret(const tal_t *ctx, const char *hsm_secr
9796
err(EXITCODE_ERROR_HSM_FILE, "Reading hsm_secret");
9897

9998
/* Get passphrase if needed */
100-
if (hsm_secret_needs_passphrase(contents, contents_len)) {
99+
if (hsm_secret_needs_passphrase(contents, tal_bytelen(contents))) {
101100
printf("Enter hsm_secret password:\n");
102101
fflush(stdout);
103102
passphrase = read_stdin_pass(tmpctx, &error);
104103
if (!passphrase)
105104
errx(EXITCODE_ERROR_HSM_FILE, "Could not read password: %s", hsm_secret_error_str(error));
106105
}
107106

108-
hsms = extract_hsm_secret(ctx, contents, contents_len, passphrase, &error);
107+
hsms = extract_hsm_secret(ctx, contents, tal_bytelen(contents), passphrase, &error);
109108
if (!hsms) {
110109
err(EXITCODE_ERROR_HSM_FILE, "%s", hsm_secret_error_str(error));
111110
}
@@ -120,12 +119,11 @@ static void decrypt_hsm(const char *hsm_secret_path)
120119
const char *dir, *backup;
121120

122121
/* Check if it's a format we can decrypt */
123-
size_t contents_len;
124-
u8 *contents = grab_file_contents(tmpctx, hsm_secret_path, &contents_len);
122+
u8 *contents = grab_file_raw(tmpctx, hsm_secret_path);
125123
if (!contents)
126124
err(EXITCODE_ERROR_HSM_FILE, "Reading hsm_secret");
127125

128-
enum hsm_secret_type type = detect_hsm_secret_type(contents, contents_len);
126+
enum hsm_secret_type type = detect_hsm_secret_type(contents, tal_bytelen(contents));
129127

130128
if (type != HSM_SECRET_ENCRYPTED) {
131129
errx(ERROR_USAGE, "decrypt command only works on legacy encrypted binary format (73 bytes).\n"
@@ -178,12 +176,11 @@ static void encrypt_hsm(const char *hsm_secret_path)
178176
enum hsm_secret_error pass_err;
179177

180178
/* Check if it's a format we can encrypt */
181-
size_t contents_len;
182-
u8 *contents = grab_file_contents(tmpctx, hsm_secret_path, &contents_len);
179+
u8 *contents = grab_file_raw(tmpctx, hsm_secret_path);
183180
if (!contents)
184181
err(EXITCODE_ERROR_HSM_FILE, "Reading hsm_secret");
185182

186-
enum hsm_secret_type type = detect_hsm_secret_type(contents, contents_len);
183+
enum hsm_secret_type type = detect_hsm_secret_type(contents, tal_bytelen(contents));
187184

188185
if (type != HSM_SECRET_PLAIN) {
189186
errx(ERROR_USAGE, "encrypt command only works on legacy plain binary format (32 bytes).\n"
@@ -290,15 +287,14 @@ static void print_codexsecret(const char *hsm_secret_path, const char *id)
290287

291288
static void print_emergencyrecover(const char *emer_rec_path)
292289
{
293-
size_t scb_len;
294-
u8 *scb = grab_file_contents(tmpctx, emer_rec_path, &scb_len);
290+
u8 *scb = grab_file_raw(tmpctx, emer_rec_path);
295291
char *output, *hrp = "clnemerg";
296292
if (!scb) {
297293
err(EXITCODE_ERROR_HSM_FILE, "Reading emergency.recover");
298294
}
299295
u5 *data = tal_arr(tmpctx, u5, 0);
300296

301-
bech32_push_bits(&data, scb, scb_len * 8);
297+
bech32_push_bits(&data, scb, tal_bytelen(scb) * 8);
302298
output = tal_arr(tmpctx, char, strlen(hrp) + tal_count(data) + 8);
303299

304300
bech32_encode(output, hrp, data, tal_count(data), (size_t)-1,

0 commit comments

Comments
 (0)