Legacy function - only works with binary encrypted format */
| 113 | |
| 114 | /* Legacy function - only works with binary encrypted format */ |
| 115 | static 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 */ |
| 169 | static void encrypt_hsm(const char *hsm_secret_path) |
no test coverage detected