Legacy function - only works with binary plain format */
| 167 | |
| 168 | /* Legacy function - only works with binary plain format */ |
| 169 | static 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) |
no test coverage detected