MCPcopy Create free account
hub / github.com/ElementsProject/lightning / encrypt_hsm

Function encrypt_hsm

tools/lightning-hsmtool.c:169–249  ·  view source on GitHub ↗

Legacy function - only works with binary plain format */

Source from the content-addressed store, hash-verified

167
168/* Legacy function - only works with binary plain format */
169static void encrypt_hsm(const char *hsm_secret_path)
170{
171 int fd;
172 struct hsm_secret *hsms;
173 u8 encrypted_hsm_secret[ENCRYPTED_HSM_SECRET_LEN];
174 const char *passwd, *passwd_confirmation;
175 const char *dir, *backup;
176 enum hsm_secret_error pass_err;
177
178 /* Check if it's a format we can encrypt */
179 u8 *contents = grab_file_raw(tmpctx, hsm_secret_path);
180 if (!contents)
181 err(EXITCODE_ERROR_HSM_FILE, "Reading hsm_secret");
182
183 enum hsm_secret_type type = detect_hsm_secret_type(contents, tal_bytelen(contents));
184
185 if (type != HSM_SECRET_PLAIN) {
186 errx(ERROR_USAGE, "encrypt command only works on legacy plain binary format (32 bytes).\n"
187 "Current file is: %s\n"
188 "For mnemonic formats, the passphrase is already integrated into the format.",
189 format_type_name(type));
190 }
191
192 /* Load the hsm_secret */
193 hsms = load_hsm_secret(tmpctx, hsm_secret_path);
194
195 printf("Enter hsm_secret password:\n");
196 fflush(stdout);
197 passwd = read_stdin_pass(tmpctx, &pass_err);
198 if (!passwd)
199 errx(EXITCODE_ERROR_HSM_FILE, "Could not read password: %s", hsm_secret_error_str(pass_err));
200
201 printf("Confirm hsm_secret password:\n");
202 fflush(stdout);
203 passwd_confirmation = read_stdin_pass(tmpctx, &pass_err);
204 if (!passwd_confirmation)
205 errx(EXITCODE_ERROR_HSM_FILE, "Could not read password: %s", hsm_secret_error_str(pass_err));
206
207 if (!streq(passwd, passwd_confirmation))
208 errx(ERROR_USAGE, "Passwords confirmation mismatch.");
209
210 dir = path_dirname(NULL, hsm_secret_path);
211 backup = path_join(dir, dir, "hsm_secret.backup");
212
213 /* Create encryption key and encrypt */
214 struct secret *encryption_key = get_encryption_key(tmpctx, passwd);
215 if (!encryption_key)
216 errx(ERROR_LIBSODIUM, "Could not derive encryption key");
217
218 struct secret legacy_secret;
219 memcpy(legacy_secret.data, hsms->secret_data, 32);
220 if (!encrypt_legacy_hsm_secret(encryption_key, &legacy_secret, encrypted_hsm_secret))
221 errx(ERROR_LIBSODIUM, "Could not encrypt the hsm_secret seed.");
222
223 /* Create a backup file, "just in case". */
224 rename(hsm_secret_path, backup);
225 fd = open(hsm_secret_path, O_CREAT|O_EXCL|O_WRONLY, 0400);
226 if (fd < 0)

Callers 1

mainFunction · 0.85

Calls 15

grab_file_rawFunction · 0.85
errFunction · 0.85
detect_hsm_secret_typeFunction · 0.85
tal_bytelenFunction · 0.85
errxFunction · 0.85
format_type_nameFunction · 0.85
load_hsm_secretFunction · 0.85
read_stdin_passFunction · 0.85
hsm_secret_error_strFunction · 0.85
path_dirnameFunction · 0.85
path_joinFunction · 0.85
get_encryption_keyFunction · 0.85

Tested by

no test coverage detected