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

Function decrypt_hsm

tools/lightning-hsmtool.c:115–166  ·  view source on GitHub ↗

Legacy function - only works with binary encrypted format */

Source from the content-addressed store, hash-verified

113
114/* Legacy function - only works with binary encrypted format */
115static void decrypt_hsm(const char *hsm_secret_path)
116{
117 int fd;
118 struct hsm_secret *hsms;
119 const char *dir, *backup;
120
121 /* Check if it's a format we can decrypt */
122 u8 *contents = grab_file_raw(tmpctx, hsm_secret_path);
123 if (!contents)
124 err(EXITCODE_ERROR_HSM_FILE, "Reading hsm_secret");
125
126 enum hsm_secret_type type = detect_hsm_secret_type(contents, tal_bytelen(contents));
127
128 if (type != HSM_SECRET_ENCRYPTED) {
129 errx(ERROR_USAGE, "decrypt command only works on legacy encrypted binary format (73 bytes).\n"
130 "Current file is: %s\n"
131 "For mnemonic formats, use the generatehsm command to create a new hsm_secret instead.",
132 format_type_name(type));
133 }
134
135 /* Load the hsm_secret */
136 hsms = load_hsm_secret(tmpctx, hsm_secret_path);
137
138 dir = path_dirname(NULL, hsm_secret_path);
139 backup = path_join(dir, dir, "hsm_secret.backup");
140
141 /* Create a backup file, "just in case". */
142 rename(hsm_secret_path, backup);
143 fd = open(hsm_secret_path, O_CREAT|O_EXCL|O_WRONLY, 0400);
144 if (fd < 0)
145 err(EXITCODE_ERROR_HSM_FILE, "Could not open new hsm_secret");
146
147 if (!write_all(fd, hsms->secret_data, tal_bytelen(hsms->secret_data))) {
148 unlink_noerr(hsm_secret_path);
149 close(fd);
150 rename("hsm_secret.backup", hsm_secret_path);
151 err(EXITCODE_ERROR_HSM_FILE,
152 "Failure writing plaintext seed to hsm_secret.");
153 }
154
155 /* Be as paranoïd as in hsmd with the file state on disk. */
156 if (!ensure_hsm_secret_exists(fd, hsm_secret_path)) {
157 unlink_noerr(hsm_secret_path);
158 rename(backup, hsm_secret_path);
159 errx(EXITCODE_ERROR_HSM_FILE,
160 "Could not ensure hsm_secret existence.");
161 }
162 unlink_noerr(backup);
163 tal_free(dir);
164
165 printf("Successfully decrypted hsm_secret, be careful now :-).\n");
166}
167
168/* Legacy function - only works with binary plain format */
169static void encrypt_hsm(const char *hsm_secret_path)

Callers 1

mainFunction · 0.85

Calls 13

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
path_dirnameFunction · 0.85
path_joinFunction · 0.85
unlink_noerrFunction · 0.85
ensure_hsm_secret_existsFunction · 0.85
tal_freeFunction · 0.85

Tested by

no test coverage detected