diff --git a/common/sphinx.c b/common/sphinx.c index 5a1b1cf89..c254e6317 100644 --- a/common/sphinx.c +++ b/common/sphinx.c @@ -58,10 +58,18 @@ struct sphinx_path { struct pubkey *rendezvous_id; }; -struct sphinx_path *sphinx_path_new(const tal_t *ctx, const u8 *associated_data) +struct sphinx_path *sphinx_path_new(const tal_t *ctx, + const u8 *associated_data, + size_t associated_data_len) { struct sphinx_path *sp = tal(ctx, struct sphinx_path); - sp->associated_data = tal_dup_talarr(sp, u8, associated_data); + if (associated_data) { + sp->associated_data + = tal_dup_arr(sp, u8, associated_data, associated_data_len, 0); + } else { + assert(associated_data_len == 0); + sp->associated_data = NULL; + } sp->session_key = NULL; sp->rendezvous_id = NULL; sp->hops = tal_arr(sp, struct sphinx_hop, 0); @@ -70,9 +78,10 @@ struct sphinx_path *sphinx_path_new(const tal_t *ctx, const u8 *associated_data) struct sphinx_path *sphinx_path_new_with_key(const tal_t *ctx, const u8 *associated_data, + size_t associated_data_len, const struct secret *session_key) { - struct sphinx_path *sp = sphinx_path_new(ctx, associated_data); + struct sphinx_path *sp = sphinx_path_new(ctx, associated_data, associated_data_len); sp->session_key = tal_dup(sp, struct secret, session_key); return sp; } diff --git a/common/sphinx.h b/common/sphinx.h index d1426bb47..933e82627 100644 --- a/common/sphinx.h +++ b/common/sphinx.h @@ -182,7 +182,8 @@ u8 *unwrap_onionreply(const tal_t *ctx, * passed to `create_onionpacket` to generate the packet. */ struct sphinx_path *sphinx_path_new(const tal_t *ctx, - const u8 *associated_data); + const u8 *associated_data, + size_t associated_data_len); /** * Create a new empty sphinx_path with a given `session_key`. @@ -192,6 +193,7 @@ struct sphinx_path *sphinx_path_new(const tal_t *ctx, */ struct sphinx_path *sphinx_path_new_with_key(const tal_t *ctx, const u8 *associated_data, + size_t associated_data_len, const struct secret *session_key); /** diff --git a/common/test/run-blindedpath_onion.c b/common/test/run-blindedpath_onion.c index 7fa7abe59..a58ccae9f 100644 --- a/common/test/run-blindedpath_onion.c +++ b/common/test/run-blindedpath_onion.c @@ -235,7 +235,7 @@ int main(int argc, char *argv[]) pubkey_from_privkey(&path_key[DAVE], &path_key_pub[DAVE]); /* Create an onion which encodes this. */ - sphinx_path = sphinx_path_new(tmpctx, NULL); + sphinx_path = sphinx_path_new(tmpctx, NULL, 0); for (size_t i = 0; i < 4; i++) { struct tlv_onionmsg_tlv *payload = tlv_onionmsg_tlv_new(tmpctx); diff --git a/common/test/run-onion-message-test.c b/common/test/run-onion-message-test.c index ae1d55de7..7dba2c5cc 100644 --- a/common/test/run-onion-message-test.c +++ b/common/test/run-onion-message-test.c @@ -306,7 +306,7 @@ int main(int argc, char *argv[]) json_strfield("unknown_tag_1", "68656c6c6f"); /* Create the onionmessage */ - sphinx_path = sphinx_path_new(tmpctx, NULL); + sphinx_path = sphinx_path_new(tmpctx, NULL, 0); for (size_t i = 0; i < ARRAY_SIZE(erd); i++) { struct tlv_onionmsg_tlv *tlv = tlv_onionmsg_tlv_new(tmpctx); u8 *onionmsg_tlv; diff --git a/common/test/run-onion-test-vector.c b/common/test/run-onion-test-vector.c index e7246dc0f..d29637fec 100644 --- a/common/test/run-onion-test-vector.c +++ b/common/test/run-onion-test-vector.c @@ -161,7 +161,8 @@ int main(int argc, char *argv[]) generate_tok = json_get_member(json, toks, "generate"); json_to_secret(json, json_get_member(json, generate_tok, "session_key"), &session_key); assoc_data = json_tok_bin_from_hex(tmpctx, json, json_get_member(json, generate_tok, "associated_data")); - sp = sphinx_path_new_with_key(tmpctx, assoc_data, &session_key); + sp = sphinx_path_new_with_key(tmpctx, assoc_data, tal_bytelen(assoc_data), + &session_key); json_for_each_arr(i, t, json_get_member(json, generate_tok, "hops")) { struct pubkey k; const u8 *cursor; diff --git a/common/test/run-route_blinding_onion_test.c b/common/test/run-route_blinding_onion_test.c index 4435ee576..9c554ebd1 100644 --- a/common/test/run-route_blinding_onion_test.c +++ b/common/test/run-route_blinding_onion_test.c @@ -171,7 +171,8 @@ int main(int argc, char *argv[]) } /* Now, create onion! */ - sp = sphinx_path_new_with_key(tmpctx, associated_data, &session_key); + sp = sphinx_path_new_with_key(tmpctx, associated_data, tal_bytelen(associated_data), + &session_key); for (i = 0; i < tal_count(ids); i++) sphinx_add_hop_has_length(sp, &ids[i], onionhops[i]); diff --git a/devtools/onion.c b/devtools/onion.c index 54ef3d6e1..0d517c15e 100644 --- a/devtools/onion.c +++ b/devtools/onion.c @@ -36,7 +36,8 @@ static void do_generate(int argc, char **argv, memset(&session_key, 'A', sizeof(struct secret)); - sp = sphinx_path_new_with_key(ctx, assocdata, &session_key); + sp = sphinx_path_new_with_key(ctx, assocdata, tal_bytelen(assocdata), + &session_key); sphinx_path_set_rendezvous(sp, rvnode_id); for (int i = 0; i < num_hops; i++) { @@ -218,7 +219,8 @@ static void runtest(const char *filename) associated_data = json_tok_bin_from_hex(ctx, buffer, associated_data_tok); session_key_raw = json_tok_bin_from_hex(ctx, buffer, session_key_tok); memcpy(&session_key, session_key_raw, sizeof(session_key)); - path = sphinx_path_new_with_key(ctx, associated_data, &session_key); + path = sphinx_path_new_with_key(ctx, associated_data, tal_bytelen(associated_data), + &session_key); /* Unpack the hops and build up the path */ hopstok = json_get_member(buffer, gentok, "hops"); diff --git a/lightningd/onion_message.c b/lightningd/onion_message.c index c30699e33..4e52a95b2 100644 --- a/lightningd/onion_message.c +++ b/lightningd/onion_message.c @@ -236,7 +236,7 @@ static struct command_result *json_injectonionmessage(struct command *cmd, "experimental-onion-messages not enabled"); /* Create an onion which encodes this. */ - sphinx_path = sphinx_path_new(cmd, NULL); + sphinx_path = sphinx_path_new(cmd, NULL, 0); for (size_t i = 0; i < tal_count(hops); i++) sphinx_add_hop(sphinx_path, &hops[i].node, hops[i].tlv); diff --git a/lightningd/pay.c b/lightningd/pay.c index b1f961e7a..07b4abe3d 100644 --- a/lightningd/pay.c +++ b/lightningd/pay.c @@ -1196,7 +1196,7 @@ send_payment(struct lightningd *ld, and use bitcoind's block height, even if we're behind in processing */ base_expiry = get_network_blockheight(ld->topology) + 1; - path = sphinx_path_new(tmpctx, rhash->u.u8); + path = sphinx_path_new(tmpctx, rhash->u.u8, sizeof(rhash->u.u8)); /* Extract IDs for each hop: create_onionpacket wants array. */ for (i = 0; i < n_hops; i++) ids[i] = route[i].node_id; @@ -2340,9 +2340,10 @@ static struct command_result *json_createonion(struct command *cmd, } if (session_key == NULL) - sp = sphinx_path_new(cmd, assocdata); + sp = sphinx_path_new(cmd, assocdata, tal_bytelen(assocdata)); else - sp = sphinx_path_new_with_key(cmd, assocdata, session_key); + sp = sphinx_path_new_with_key(cmd, assocdata, tal_bytelen(assocdata), + session_key); for (size_t i=0; i