Skip to content

Commit

Permalink
sphinx: explain why parse_onionpacket fails.
Browse files Browse the repository at this point in the history
Signed-off-by: Rusty Russell <[email protected]>
  • Loading branch information
rustyrussell authored and cdecker committed Jan 8, 2019
1 parent 66de6b8 commit 59febcb
Show file tree
Hide file tree
Showing 7 changed files with 31 additions and 24 deletions.
5 changes: 4 additions & 1 deletion channeld/channeld.c
Original file line number Diff line number Diff line change
Expand Up @@ -534,9 +534,12 @@ static struct secret *get_shared_secret(const tal_t *ctx,
struct onionpacket *op;
struct secret *secret = tal(ctx, struct secret);
const u8 *msg;
/* FIXME: Use this! */
enum onion_type why_bad;

/* We unwrap the onion now. */
op = parse_onionpacket(tmpctx, htlc->routing, TOTAL_PACKET_SIZE);
op = parse_onionpacket(tmpctx, htlc->routing, TOTAL_PACKET_SIZE,
&why_bad);
if (!op)
return tal_free(secret);

Expand Down
17 changes: 9 additions & 8 deletions common/sphinx.c
Original file line number Diff line number Diff line change
Expand Up @@ -75,30 +75,31 @@ u8 *serialize_onionpacket(
return dst;
}

struct onionpacket *parse_onionpacket(
const tal_t *ctx,
const void *src,
const size_t srclen
)
struct onionpacket *parse_onionpacket(const tal_t *ctx,
const void *src,
const size_t srclen,
enum onion_type *why_bad)
{
struct onionpacket *m;
int p = 0;
u8 rawEphemeralkey[33];

if (srclen != TOTAL_PACKET_SIZE)
return NULL;
assert(srclen == TOTAL_PACKET_SIZE);

m = talz(ctx, struct onionpacket);

read_buffer(&m->version, src, 1, &p);
if (m->version != 0x00) {
// FIXME add logging
*why_bad = WIRE_INVALID_ONION_VERSION;
return tal_free(m);
}
read_buffer(rawEphemeralkey, src, 33, &p);

if (secp256k1_ec_pubkey_parse(secp256k1_ctx, &m->ephemeralkey, rawEphemeralkey, 33) != 1)
if (secp256k1_ec_pubkey_parse(secp256k1_ctx, &m->ephemeralkey, rawEphemeralkey, 33) != 1) {
*why_bad = WIRE_INVALID_ONION_KEY;
return tal_free(m);
}

read_buffer(&m->routinginfo, src, ROUTING_INFO_SIZE, &p);
read_buffer(&m->mac, src, SECURITY_PARAMETER, &p);
Expand Down
13 changes: 7 additions & 6 deletions common/sphinx.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
#include <ccan/tal/tal.h>
#include <secp256k1.h>
#include <sodium/randombytes.h>
#include <wire/gen_onion_wire.h>
#include <wire/wire.h>

#define SECURITY_PARAMETER 32
Expand Down Expand Up @@ -148,13 +149,13 @@ u8 *serialize_onionpacket(
*
* @ctx: tal context to allocate from
* @src: buffer to read the packet from
* @srclen: length of the @src
* @srclen: length of the @src (must be TOTAL_PACKET_SIZE)
* @why_bad: if NULL return, this is what was wrong with the packet.
*/
struct onionpacket *parse_onionpacket(
const tal_t *ctx,
const void *src,
const size_t srclen
);
struct onionpacket *parse_onionpacket(const tal_t *ctx,
const void *src,
const size_t srclen,
enum onion_type *why_bad);

struct onionreply {
/* Node index in the path that is replying */
Expand Down
3 changes: 2 additions & 1 deletion devtools/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@ DEVTOOLS_COMMON_OBJS := \
common/type_to_string.o \
common/utils.o \
common/version.o \
common/wireaddr.o
common/wireaddr.o \
wire/gen_onion_wire.o

devtools-all: $(DEVTOOLS)

Expand Down
5 changes: 3 additions & 2 deletions devtools/onion.c
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ static void do_decode(int argc, char **argv)
memset(hextemp, 0, sizeof(hextemp));
u8 shared_secret[32];
u8 assocdata[32];
enum onion_type why_bad;

memset(&assocdata, 'B', sizeof(assocdata));

Expand All @@ -82,10 +83,10 @@ static void do_decode(int argc, char **argv)
errx(1, "Invalid onion hex '%s'", hextemp);
}

msg = parse_onionpacket(ctx, serialized, sizeof(serialized));
msg = parse_onionpacket(ctx, serialized, sizeof(serialized), &why_bad);

if (!msg)
errx(1, "Error parsing message.");
errx(1, "Error parsing message: %s", onion_type_name(why_bad));

if (!onion_shared_secret(shared_secret, msg, &seckey))
errx(1, "Error creating shared secret.");
Expand Down
3 changes: 2 additions & 1 deletion lightningd/peer_htlcs.c
Original file line number Diff line number Diff line change
Expand Up @@ -648,7 +648,8 @@ static bool peer_accepted_htlc(struct channel *channel,

/* channeld tests this, so it should pass. */
op = parse_onionpacket(tmpctx, hin->onion_routing_packet,
sizeof(hin->onion_routing_packet));
sizeof(hin->onion_routing_packet),
failcode);
if (!op) {
channel_internal_error(channel,
"bad onion in got_revoke: %s",
Expand Down
9 changes: 4 additions & 5 deletions wallet/test/run-wallet.c
Original file line number Diff line number Diff line change
Expand Up @@ -367,11 +367,10 @@ struct command_result *param_tok(struct command *cmd UNNEEDED, const char *name
const jsmntok_t **out UNNEEDED)
{ fprintf(stderr, "param_tok called!\n"); abort(); }
/* Generated stub for parse_onionpacket */
struct onionpacket *parse_onionpacket(
const tal_t *ctx UNNEEDED,
const void *src UNNEEDED,
const size_t srclen
)
struct onionpacket *parse_onionpacket(const tal_t *ctx UNNEEDED,
const void *src UNNEEDED,
const size_t srclen UNNEEDED,
enum onion_type *why_bad UNNEEDED)
{ fprintf(stderr, "parse_onionpacket called!\n"); abort(); }
/* Generated stub for payment_failed */
void payment_failed(struct lightningd *ld UNNEEDED, const struct htlc_out *hout UNNEEDED,
Expand Down

0 comments on commit 59febcb

Please sign in to comment.