Skip to content

Commit 59febcb

Browse files
rustyrussellcdecker
authored andcommitted
sphinx: explain why parse_onionpacket fails.
Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
1 parent 66de6b8 commit 59febcb

File tree

7 files changed

+31
-24
lines changed

7 files changed

+31
-24
lines changed

channeld/channeld.c

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -534,9 +534,12 @@ static struct secret *get_shared_secret(const tal_t *ctx,
534534
struct onionpacket *op;
535535
struct secret *secret = tal(ctx, struct secret);
536536
const u8 *msg;
537+
/* FIXME: Use this! */
538+
enum onion_type why_bad;
537539

538540
/* We unwrap the onion now. */
539-
op = parse_onionpacket(tmpctx, htlc->routing, TOTAL_PACKET_SIZE);
541+
op = parse_onionpacket(tmpctx, htlc->routing, TOTAL_PACKET_SIZE,
542+
&why_bad);
540543
if (!op)
541544
return tal_free(secret);
542545

common/sphinx.c

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -75,30 +75,31 @@ u8 *serialize_onionpacket(
7575
return dst;
7676
}
7777

78-
struct onionpacket *parse_onionpacket(
79-
const tal_t *ctx,
80-
const void *src,
81-
const size_t srclen
82-
)
78+
struct onionpacket *parse_onionpacket(const tal_t *ctx,
79+
const void *src,
80+
const size_t srclen,
81+
enum onion_type *why_bad)
8382
{
8483
struct onionpacket *m;
8584
int p = 0;
8685
u8 rawEphemeralkey[33];
8786

88-
if (srclen != TOTAL_PACKET_SIZE)
89-
return NULL;
87+
assert(srclen == TOTAL_PACKET_SIZE);
9088

9189
m = talz(ctx, struct onionpacket);
9290

9391
read_buffer(&m->version, src, 1, &p);
9492
if (m->version != 0x00) {
9593
// FIXME add logging
94+
*why_bad = WIRE_INVALID_ONION_VERSION;
9695
return tal_free(m);
9796
}
9897
read_buffer(rawEphemeralkey, src, 33, &p);
9998

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

103104
read_buffer(&m->routinginfo, src, ROUTING_INFO_SIZE, &p);
104105
read_buffer(&m->mac, src, SECURITY_PARAMETER, &p);

common/sphinx.h

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
#include <ccan/tal/tal.h>
1010
#include <secp256k1.h>
1111
#include <sodium/randombytes.h>
12+
#include <wire/gen_onion_wire.h>
1213
#include <wire/wire.h>
1314

1415
#define SECURITY_PARAMETER 32
@@ -148,13 +149,13 @@ u8 *serialize_onionpacket(
148149
*
149150
* @ctx: tal context to allocate from
150151
* @src: buffer to read the packet from
151-
* @srclen: length of the @src
152+
* @srclen: length of the @src (must be TOTAL_PACKET_SIZE)
153+
* @why_bad: if NULL return, this is what was wrong with the packet.
152154
*/
153-
struct onionpacket *parse_onionpacket(
154-
const tal_t *ctx,
155-
const void *src,
156-
const size_t srclen
157-
);
155+
struct onionpacket *parse_onionpacket(const tal_t *ctx,
156+
const void *src,
157+
const size_t srclen,
158+
enum onion_type *why_bad);
158159

159160
struct onionreply {
160161
/* Node index in the path that is replying */

devtools/Makefile

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,8 @@ DEVTOOLS_COMMON_OBJS := \
1414
common/type_to_string.o \
1515
common/utils.o \
1616
common/version.o \
17-
common/wireaddr.o
17+
common/wireaddr.o \
18+
wire/gen_onion_wire.o
1819

1920
devtools-all: $(DEVTOOLS)
2021

devtools/onion.c

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,7 @@ static void do_decode(int argc, char **argv)
6666
memset(hextemp, 0, sizeof(hextemp));
6767
u8 shared_secret[32];
6868
u8 assocdata[32];
69+
enum onion_type why_bad;
6970

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

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

85-
msg = parse_onionpacket(ctx, serialized, sizeof(serialized));
86+
msg = parse_onionpacket(ctx, serialized, sizeof(serialized), &why_bad);
8687

8788
if (!msg)
88-
errx(1, "Error parsing message.");
89+
errx(1, "Error parsing message: %s", onion_type_name(why_bad));
8990

9091
if (!onion_shared_secret(shared_secret, msg, &seckey))
9192
errx(1, "Error creating shared secret.");

lightningd/peer_htlcs.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -648,7 +648,8 @@ static bool peer_accepted_htlc(struct channel *channel,
648648

649649
/* channeld tests this, so it should pass. */
650650
op = parse_onionpacket(tmpctx, hin->onion_routing_packet,
651-
sizeof(hin->onion_routing_packet));
651+
sizeof(hin->onion_routing_packet),
652+
failcode);
652653
if (!op) {
653654
channel_internal_error(channel,
654655
"bad onion in got_revoke: %s",

wallet/test/run-wallet.c

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -367,11 +367,10 @@ struct command_result *param_tok(struct command *cmd UNNEEDED, const char *name
367367
const jsmntok_t **out UNNEEDED)
368368
{ fprintf(stderr, "param_tok called!\n"); abort(); }
369369
/* Generated stub for parse_onionpacket */
370-
struct onionpacket *parse_onionpacket(
371-
const tal_t *ctx UNNEEDED,
372-
const void *src UNNEEDED,
373-
const size_t srclen
374-
)
370+
struct onionpacket *parse_onionpacket(const tal_t *ctx UNNEEDED,
371+
const void *src UNNEEDED,
372+
const size_t srclen UNNEEDED,
373+
enum onion_type *why_bad UNNEEDED)
375374
{ fprintf(stderr, "parse_onionpacket called!\n"); abort(); }
376375
/* Generated stub for payment_failed */
377376
void payment_failed(struct lightningd *ld UNNEEDED, const struct htlc_out *hout UNNEEDED,

0 commit comments

Comments
 (0)