Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

devtools/onion: fixed argument passing for generate and decode methods #3250

Merged
merged 4 commits into from
Nov 14, 2019
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 29 additions & 21 deletions devtools/onion.c
Original file line number Diff line number Diff line change
Expand Up @@ -31,38 +31,39 @@ static void do_generate(int argc, char **argv,
struct sphinx_path *sp;
struct hop_data hops_data[num_hops];

assocdata = tal_arr(ctx, u8, ASSOC_DATA_SIZE);
const u8* tmp_assocdata =tal_dup_arr(ctx, u8, assocdata,
ASSOC_DATA_SIZE, 0);
memset(&session_key, 'A', sizeof(struct secret));

sp = sphinx_path_new_with_key(ctx, assocdata, &session_key);
sp = sphinx_path_new_with_key(ctx, tmp_assocdata, &session_key);

for (int i = 0; i < num_hops; i++) {
size_t klen = strcspn(argv[1 + i], "/");
size_t klen = strcspn(argv[2 + i], "/");
if (hex_data_size(klen) == PRIVKEY_LEN) {
if (!hex_decode(argv[1 + i], klen, rawprivkey, PRIVKEY_LEN))
if (!hex_decode(argv[2 + i], klen, rawprivkey, PRIVKEY_LEN))
errx(1, "Invalid private key hex '%s'",
argv[1 + i]);
argv[2 + i]);

if (secp256k1_ec_pubkey_create(secp256k1_ctx,
&path[i].pubkey,
rawprivkey) != 1)
errx(1, "Could not decode pubkey");
} else if (hex_data_size(klen) == PUBKEY_CMPR_LEN) {
if (!pubkey_from_hexstr(argv[i + 1], klen, &path[i]))
if (!pubkey_from_hexstr(argv[2 + i], klen, &path[i]))
errx(1, "Invalid public key hex '%s'",
argv[1 + i]);
argv[2 + i]);
} else {
errx(1,
"Provided key is neither a pubkey nor a privkey: "
"%s\n",
argv[1 + i]);
argv[2 + i]);
}

memset(&hops_data[i], 0, sizeof(hops_data[i]));
if (argv[1 + i][klen] != '\0') {
if (argv[2 + i][klen] != '\0') {
/* FIXME: Generic realm support, not this hack! */
/* FIXME: Multi hop! */
const char *hopstr = argv[1 + i] + klen + 1;
const char *hopstr = argv[2 + i] + klen + 1;
size_t dsize = hex_data_size(strlen(hopstr));
be64 scid, msat;
be32 cltv;
Expand Down Expand Up @@ -100,7 +101,7 @@ static void do_generate(int argc, char **argv,
hops_data[i].outgoing_cltv = i;
}
fprintf(stderr, "Hopdata %d: %s\n", i, tal_hexstr(NULL, &hops_data[i], sizeof(hops_data[i])));
sphinx_add_v0_hop(sp, &path[i], &hops_data[i].channel_id, hops_data[i].amt_forward, i);
sphinx_add_v0_hop(sp, &path[i], &hops_data[i].channel_id, hops_data[i].amt_forward, hops_data[i].outgoing_cltv);
}

struct onionpacket *res = create_onionpacket(ctx, sp, &shared_secrets);
Expand Down Expand Up @@ -141,20 +142,24 @@ static void do_decode(int argc, char **argv, const u8 assocdata[ASSOC_DATA_SIZE]
const tal_t *ctx = talz(NULL, tal_t);
u8 serialized[TOTAL_PACKET_SIZE];
struct route_step *step;
char hextemp[2 * sizeof(serialized)];
memset(hextemp, 0, sizeof(hextemp));

if (argc != 3)
opt_usage_exit_fail("Expect a privkey with --decode");
if (argc != 4)
opt_usage_exit_fail("Expect an filename and privkey with 'decode' method");

if (!read_all(STDIN_FILENO, hextemp, sizeof(hextemp)))
errx(1, "Reading in onion");
char *hextemp = grab_file(ctx, argv[2]);
size_t hexlen = strlen(hextemp);

if (!hex_decode(hextemp, sizeof(hextemp), serialized, sizeof(serialized))) {
// trim trailing whitespace
while (isspace(hextemp[hexlen-1]))
hexlen--;

if (!hex_decode(hextemp, hexlen, serialized, sizeof(serialized))) {
errx(1, "Invalid onion hex '%s'", hextemp);
}

step = decode_with_privkey(ctx, serialized, tal_strdup(ctx, argv[2]), assocdata);
const u8* tmp_assocdata =tal_dup_arr(ctx, u8, assocdata,
ASSOC_DATA_SIZE, 0);
step = decode_with_privkey(ctx, serialized, tal_strdup(ctx, argv[3]), tmp_assocdata);

if (!step || !step->next)
errx(1, "Error processing message.");
Expand Down Expand Up @@ -304,11 +309,14 @@ int main(int argc, char **argv)
assocdata,
"Associated data (usu. payment_hash of payment)");
opt_register_noarg("--help|-h", opt_usage_and_exit,
"\n\n\tdecode <onion>\n"
"\n\n\tdecode <onion_file> <privkey>\n"
"\tgenerate <pubkey1> <pubkey2> ...\n"
"\tgenerate <pubkey1>[/hopdata] <pubkey2>[/hopdata]\n"
"\tgenerate <privkey1>[/hopdata] <privkey2>[/hopdata]\n"
"\truntest <test-filename>\n\n", "Show this message");
"\truntest <test-filename>\n\n", "Show this message\n\n"
"\texample:\n"
"\t> onion generate 02c18e7ff9a319983e85094b8c957da5c1230ecb328c1f1c7e88029f1fec2046f8/00000000000000000000000000000f424000000138000000000000000000000000 --assoc-data 44ee26f01e54665937b892f6afbfdfb88df74bcca52d563f088668cf4490aacd > onion.dat\n"
"\t> onion decode onion.dat 78302c8edb1b94e662464e99af721054b6ab9d577d3189f933abde57709c5cb8 --assoc-data 44ee26f01e54665937b892f6afbfdfb88df74bcca52d563f088668cf4490aacd\n");
opt_register_version();

opt_early_parse(argc, argv, opt_log_stderr_exit);
Expand Down