|
| 1 | +#include "config.h" |
| 2 | +#include <bitcoin/privkey.h> |
| 3 | +#include <ccan/array_size/array_size.h> |
| 4 | +#include <ccan/crypto/hkdf_sha256/hkdf_sha256.h> |
| 5 | +#include <ccan/crypto/sha256/sha256.h> |
| 6 | +#include <ccan/tal/grab_file/grab_file.h> |
| 7 | +#include <ccan/tal/str/str.h> |
| 8 | +#include <common/bech32.h> |
| 9 | +#include <common/codex32.h> |
| 10 | +#include <common/json_param.h> |
| 11 | +#include <common/json_stream.h> |
| 12 | +#include <errno.h> |
| 13 | +#include <plugins/libplugin.h> |
| 14 | + |
| 15 | +/* Information this plugin wants to keep. */ |
| 16 | +struct exposesecret { |
| 17 | + char *exposure_passphrase; |
| 18 | + struct pubkey our_node_id; |
| 19 | + const char *our_node_alias; |
| 20 | +}; |
| 21 | + |
| 22 | +static struct exposesecret *exposesecret_data(struct plugin *plugin) |
| 23 | +{ |
| 24 | + return plugin_get_data(plugin, struct exposesecret); |
| 25 | +} |
| 26 | + |
| 27 | +/* Don't let compiler do clever things, which would allow the caller |
| 28 | + * to measure time, and figure out how much of the passphrase matched! */ |
| 29 | +static bool compare_passphrases(const char *a, const char *b) |
| 30 | +{ |
| 31 | + struct sha256 a_sha, b_sha; |
| 32 | + |
| 33 | + /* Technically, this gives information about passphrase length, but |
| 34 | + * they can also just brute force it if it is small, so this doesn't |
| 35 | + * add much! Also, hashing messes with timing quite a bit. */ |
| 36 | + sha256(&a_sha, a, strlen(a)); |
| 37 | + sha256(&b_sha, b, strlen(b)); |
| 38 | + |
| 39 | + return sha256_eq(&a_sha, &b_sha); |
| 40 | +} |
| 41 | + |
| 42 | +static struct command_result *json_exposesecret(struct command *cmd, |
| 43 | + const char *buffer, |
| 44 | + const jsmntok_t *params) |
| 45 | +{ |
| 46 | + const struct exposesecret *exposesecret = exposesecret_data(cmd->plugin); |
| 47 | + struct json_stream *js; |
| 48 | + u8 *contents; |
| 49 | + const char *id, *passphrase, *err; |
| 50 | + struct secret hsm_secret; |
| 51 | + struct privkey node_privkey; |
| 52 | + struct pubkey node_id; |
| 53 | + char *bip93; |
| 54 | + u32 salt = 0; |
| 55 | + |
| 56 | + if (!param_check(cmd, buffer, params, |
| 57 | + p_req("passphrase", param_string, &passphrase), |
| 58 | + p_opt("identifier", param_string, &id), |
| 59 | + NULL)) |
| 60 | + return command_param_failed(); |
| 61 | + |
| 62 | + if (!exposesecret->exposure_passphrase) |
| 63 | + return command_fail(cmd, LIGHTNINGD, "exposesecrets-passphrase is not set"); |
| 64 | + |
| 65 | + /* Technically, this could become a timing oracle. */ |
| 66 | + if (!compare_passphrases(exposesecret->exposure_passphrase, passphrase)) |
| 67 | + return command_fail(cmd, LIGHTNINGD, "passphrase does not match exposesecrets-passphrase"); |
| 68 | + |
| 69 | + contents = grab_file(tmpctx, "hsm_secret"); |
| 70 | + if (!contents) |
| 71 | + return command_fail(cmd, LIGHTNINGD, "Could not open hsm_secret: %s", strerror(errno)); |
| 72 | + |
| 73 | + /* grab_file adds a \0 byte at the end for convenience */ |
| 74 | + if (tal_bytelen(contents) == sizeof(hsm_secret) + 1) { |
| 75 | + memcpy(&hsm_secret, contents, sizeof(hsm_secret)); |
| 76 | + } else { |
| 77 | + return command_fail(cmd, LIGHTNINGD, "Not a valid hsm_secret file? Bad length (maybe encrypted?)"); |
| 78 | + } |
| 79 | + |
| 80 | + /* Before we expose it, check it's correct! */ |
| 81 | + hkdf_sha256(&node_privkey, sizeof(node_privkey), |
| 82 | + &salt, sizeof(salt), |
| 83 | + &hsm_secret, |
| 84 | + sizeof(hsm_secret), |
| 85 | + "nodeid", 6); |
| 86 | + |
| 87 | + /* Should not happen! */ |
| 88 | + if (!pubkey_from_privkey(&node_privkey, &node_id)) |
| 89 | + return command_fail(cmd, LIGHTNINGD, "Invalid private key?"); |
| 90 | + |
| 91 | + if (!pubkey_eq(&node_id, &exposesecret->our_node_id)) |
| 92 | + return command_fail(cmd, LIGHTNINGD, "This hsm_secret is not for the current node"); |
| 93 | + |
| 94 | + /* If they didn't give an identifier, we make an appropriate one! */ |
| 95 | + if (!id) { |
| 96 | + size_t off = 0; |
| 97 | + /* If we run out of alias, use x. */ |
| 98 | + char idstr[] = "xxxx"; |
| 99 | + |
| 100 | + for (size_t i = 0; idstr[off]; i++) { |
| 101 | + unsigned char c = exposesecret->our_node_alias[i]; |
| 102 | + if (c == 0) |
| 103 | + break; |
| 104 | + if (c >= sizeof(bech32_charset_rev)) |
| 105 | + continue; |
| 106 | + /* Convert to lower case */ |
| 107 | + c = tolower(c); |
| 108 | + /* Must be a valid bech32 char now */ |
| 109 | + if (bech32_charset_rev[c] == -1) |
| 110 | + continue; |
| 111 | + idstr[off++] = c; |
| 112 | + } |
| 113 | + |
| 114 | + id = tal_strdup(cmd, idstr); |
| 115 | + } |
| 116 | + |
| 117 | + /* This also cannot fail! */ |
| 118 | + err = codex32_secret_encode(tmpctx, "cl", id, 0, hsm_secret.data, 32, &bip93); |
| 119 | + if (err) |
| 120 | + return command_fail(cmd, LIGHTNINGD, "Unexpected failure encoding hsm_secret: %s", err); |
| 121 | + |
| 122 | + /* If we're just checking, stop */ |
| 123 | + if (command_check_only(cmd)) |
| 124 | + return command_check_done(cmd); |
| 125 | + |
| 126 | + js = jsonrpc_stream_success(cmd); |
| 127 | + json_add_string(js, "identifier", id); |
| 128 | + json_add_string(js, "codex32", bip93); |
| 129 | + return command_finished(cmd, js); |
| 130 | +} |
| 131 | + |
| 132 | +static const char *init(struct plugin *plugin, |
| 133 | + const char *buf UNUSED, const jsmntok_t *config UNUSED) |
| 134 | +{ |
| 135 | + struct exposesecret *exposesecret = exposesecret_data(plugin); |
| 136 | + rpc_scan(plugin, "getinfo", |
| 137 | + take(json_out_obj(NULL, NULL, NULL)), |
| 138 | + "{id:%,alias:%}", |
| 139 | + JSON_SCAN(json_to_pubkey, &exposesecret->our_node_id), |
| 140 | + JSON_SCAN_TAL(exposesecret, json_strdup, &exposesecret->our_node_alias)); |
| 141 | + return NULL; |
| 142 | +} |
| 143 | + |
| 144 | +static const struct plugin_command commands[] = { |
| 145 | + { |
| 146 | + "exposesecret", |
| 147 | + json_exposesecret, |
| 148 | + } |
| 149 | +}; |
| 150 | + |
| 151 | +int main(int argc, char *argv[]) |
| 152 | +{ |
| 153 | + setup_locale(); |
| 154 | + |
| 155 | + struct exposesecret *exposesecret = talz(NULL, struct exposesecret); |
| 156 | + plugin_main(argv, init, take(exposesecret), |
| 157 | + PLUGIN_RESTARTABLE, true, NULL, commands, ARRAY_SIZE(commands), |
| 158 | + NULL, 0, NULL, 0, NULL, 0, |
| 159 | + plugin_option("exposesecret-passphrase", "string", |
| 160 | + "Enable exposesecret command to allow HSM Secret backup, with this passphrase", |
| 161 | + charp_option, NULL, &exposesecret->exposure_passphrase), |
| 162 | + NULL); |
| 163 | +} |
0 commit comments