Skip to content

Commit 7e01efb

Browse files
rustyrussellcdecker
authored andcommitted
lightningd: clean up htlc_in->shared_secret to be optional.
We currently use 'all-zeroes' as 'unknown', but NULL is more natural even if we have to send it as all-zeroes over the wire due to expressiveness limitations in our generation code. Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
1 parent 90cdc47 commit 7e01efb

File tree

4 files changed

+44
-28
lines changed

4 files changed

+44
-28
lines changed

lightningd/htlc_end.c

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,7 @@ struct htlc_in *new_htlc_in(const tal_t *ctx,
111111
struct channel *channel, u64 id,
112112
u64 msatoshi, u32 cltv_expiry,
113113
const struct sha256 *payment_hash,
114-
const struct secret *shared_secret,
114+
const struct secret *shared_secret TAKES,
115115
const u8 *onion_routing_packet)
116116
{
117117
struct htlc_in *hin = tal(ctx, struct htlc_in);
@@ -122,7 +122,10 @@ struct htlc_in *new_htlc_in(const tal_t *ctx,
122122
hin->msatoshi = msatoshi;
123123
hin->cltv_expiry = cltv_expiry;
124124
hin->payment_hash = *payment_hash;
125-
hin->shared_secret = *shared_secret;
125+
if (shared_secret)
126+
hin->shared_secret = tal_dup(hin, struct secret, shared_secret);
127+
else
128+
hin->shared_secret = NULL;
126129
memcpy(hin->onion_routing_packet, onion_routing_packet,
127130
sizeof(hin->onion_routing_packet));
128131

lightningd/htlc_end.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,8 @@ struct htlc_in {
3131
/* Onion information */
3232
u8 onion_routing_packet[TOTAL_PACKET_SIZE];
3333

34-
/* Shared secret for us to send any failure message. */
35-
struct secret shared_secret;
34+
/* Shared secret for us to send any failure message (NULL if malformed) */
35+
struct secret *shared_secret;
3636

3737
/* If a local error, this is non-zero. */
3838
enum onion_type failcode;
@@ -124,7 +124,7 @@ struct htlc_in *new_htlc_in(const tal_t *ctx,
124124
struct channel *channel, u64 id,
125125
u64 msatoshi, u32 cltv_expiry,
126126
const struct sha256 *payment_hash,
127-
const struct secret *shared_secret,
127+
const struct secret *shared_secret TAKES,
128128
const u8 *onion_routing_packet);
129129

130130
/* You need to set the ID, then connect_htlc_out this! */

lightningd/peer_htlcs.c

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -640,30 +640,25 @@ static bool peer_accepted_htlc(struct channel *channel,
640640
* a subset of the cltv check done in handle_localpay and
641641
* forward_htlc. */
642642

643-
/* channeld tests this, so it should have set ss to zeroes. */
644-
op = parse_onionpacket(tmpctx, hin->onion_routing_packet,
645-
sizeof(hin->onion_routing_packet));
646-
if (!op) {
647-
if (!memeqzero(&hin->shared_secret, sizeof(hin->shared_secret))){
648-
channel_internal_error(channel,
649-
"bad onion in got_revoke: %s",
650-
tal_hexstr(channel, hin->onion_routing_packet,
651-
sizeof(hin->onion_routing_packet)));
652-
return false;
653-
}
654-
/* FIXME: could be bad version, bad key. */
655-
*failcode = WIRE_INVALID_ONION_VERSION;
643+
/* Channeld sets this to NULL if couldn't parse onion */
644+
if (!hin->shared_secret) {
645+
*failcode = WIRE_INVALID_ONION_KEY;
656646
goto out;
657647
}
658648

659-
/* Channeld sets this to zero if HSM won't ecdh it */
660-
if (memeqzero(&hin->shared_secret, sizeof(hin->shared_secret))) {
661-
*failcode = WIRE_INVALID_ONION_KEY;
662-
goto out;
649+
/* channeld tests this, so it should pass. */
650+
op = parse_onionpacket(tmpctx, hin->onion_routing_packet,
651+
sizeof(hin->onion_routing_packet));
652+
if (!op) {
653+
channel_internal_error(channel,
654+
"bad onion in got_revoke: %s",
655+
tal_hexstr(channel, hin->onion_routing_packet,
656+
sizeof(hin->onion_routing_packet)));
657+
return false;
663658
}
664659

665660
/* If it's crap, not channeld's fault, just fail it */
666-
rs = process_onionpacket(tmpctx, op, hin->shared_secret.data,
661+
rs = process_onionpacket(tmpctx, op, hin->shared_secret->data,
667662
hin->payment_hash.u.u8,
668663
sizeof(hin->payment_hash));
669664
if (!rs) {
@@ -1115,6 +1110,11 @@ static bool channel_added_their_htlc(struct channel *channel,
11151110
return false;
11161111
}
11171112

1113+
/* FIXME: Our wire generator can't handle optional elems in arrays,
1114+
* so we translate all-zero-shared-secret to NULL. */
1115+
if (memeqzero(shared_secret, sizeof(&shared_secret)))
1116+
shared_secret = NULL;
1117+
11181118
/* This stays around even if we fail it immediately: it *is*
11191119
* part of the current commitment. */
11201120
hin = new_htlc_in(channel, channel, added->id, added->amount_msat,

wallet/wallet.c

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
#include "wallet.h"
33

44
#include <bitcoin/script.h>
5+
#include <ccan/mem/mem.h>
56
#include <ccan/tal/str/str.h>
67
#include <common/key_derive.h>
78
#include <common/wireaddr.h>
@@ -1186,8 +1187,11 @@ void wallet_htlc_save_in(struct wallet *wallet,
11861187
sqlite3_bind_null(stmt, 7);
11871188
sqlite3_bind_int(stmt, 8, in->hstate);
11881189

1189-
sqlite3_bind_blob(stmt, 9, &in->shared_secret,
1190-
sizeof(in->shared_secret), SQLITE_TRANSIENT);
1190+
if (!in->shared_secret)
1191+
sqlite3_bind_null(stmt, 9);
1192+
else
1193+
sqlite3_bind_blob(stmt, 9, in->shared_secret,
1194+
sizeof(*in->shared_secret), SQLITE_TRANSIENT);
11911195

11921196
sqlite3_bind_blob(stmt, 10, &in->onion_routing_packet,
11931197
sizeof(in->onion_routing_packet), SQLITE_TRANSIENT);
@@ -1314,9 +1318,18 @@ static bool wallet_stmt2htlc_in(struct channel *channel,
13141318
in->failuremsg = sqlite3_column_arr(in, stmt, 8, u8);
13151319
in->failcode = sqlite3_column_int(stmt, 9);
13161320

1317-
assert(sqlite3_column_bytes(stmt, 11) == sizeof(struct secret));
1318-
memcpy(&in->shared_secret, sqlite3_column_blob(stmt, 11),
1319-
sizeof(struct secret));
1321+
if (sqlite3_column_type(stmt, 11) == SQLITE_NULL) {
1322+
in->shared_secret = NULL;
1323+
} else {
1324+
assert(sqlite3_column_bytes(stmt, 11) == sizeof(struct secret));
1325+
in->shared_secret = tal(in, struct secret);
1326+
memcpy(in->shared_secret, sqlite3_column_blob(stmt, 11),
1327+
sizeof(struct secret));
1328+
#ifdef COMPAT_V062
1329+
if (memeqzero(in->shared_secret, sizeof(*in->shared_secret)))
1330+
in->shared_secret = tal_free(in->shared_secret);
1331+
#endif
1332+
}
13201333

13211334
return ok;
13221335
}

0 commit comments

Comments
 (0)