sphinx: rename confusing functions, ensure valid payloads.

"sphinx_add_hop" takes a literal hop to include,
"sphinx_add_modern_hop" prepends the length.  Now we always prepend a
length, make it clear that the literal version is a shortcut:

* sphinx_add_hop -> sphinx_add_hop_has_length
* sphinx_add_modern_hop -> sphinx_add_hop

In addition, we check that length is actually correct!  This means
`createonion` can no longer create legacy or otherwise-invalid onions:
fix tests and update man page to remove legacy usage.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
Changelog-Changed: JSON-RPC: `createonion` no longer allows non-TLV-style payloads.
This commit is contained in:
Rusty Russell
2022-09-28 14:19:37 +09:30
committed by Christian Decker
parent 8771c86379
commit f00cc23f67
11 changed files with 59 additions and 49 deletions

View File

@@ -4,6 +4,7 @@
#include <ccan/mem/mem.h>
#include <common/onion.h>
#include <common/onionreply.h>
#include <common/overflows.h>
#include <common/sphinx.h>
@@ -103,17 +104,29 @@ size_t sphinx_path_payloads_size(const struct sphinx_path *path)
return size;
}
void sphinx_add_hop(struct sphinx_path *path, const struct pubkey *pubkey,
const u8 *payload TAKES)
bool sphinx_add_hop_has_length(struct sphinx_path *path, const struct pubkey *pubkey,
const u8 *payload TAKES)
{
struct sphinx_hop sp;
bigsize_t lenlen, prepended_len;
/* You promised size was prepended! */
if (tal_bytelen(payload) == 0)
return false;
lenlen = bigsize_get(payload, tal_bytelen(payload), &prepended_len);
if (add_overflows_u64(lenlen, prepended_len))
return false;
if (lenlen + prepended_len != tal_bytelen(payload))
return false;
sp.raw_payload = tal_dup_talarr(path, u8, payload);
sp.pubkey = *pubkey;
tal_arr_expand(&path->hops, sp);
return true;
}
void sphinx_add_modern_hop(struct sphinx_path *path, const struct pubkey *pubkey,
const u8 *payload TAKES)
void sphinx_add_hop(struct sphinx_path *path, const struct pubkey *pubkey,
const u8 *payload TAKES)
{
u8 *with_len = tal_arr(NULL, u8, 0);
size_t len = tal_bytelen(payload);
@@ -122,7 +135,8 @@ void sphinx_add_modern_hop(struct sphinx_path *path, const struct pubkey *pubkey
if (taken(payload))
tal_free(payload);
sphinx_add_hop(path, pubkey, take(with_len));
if (!sphinx_add_hop_has_length(path, pubkey, take(with_len)))
abort();
}
/* Small helper to append data to a buffer and update the position