common: make sphinx code ignorant of payload format.

Now "raw_payload" is always the complete string (including realm or length
bytes at the front).

This has several effects:
1. We can receive an decrypt an onion which is grossly malformed.
2. We can still hand this to the htlc_accepted hook.
3. We then fail it unless the htlc_accepted accepts it manually.
4. The createonion API now takes the raw payload, and does not know
   anything about "style".

The only caveat is that the sphinx code needs to know the payload
length: we have a call for that, which simply tells it to copy the
entire onion (and treat us as the final node) if it's invalid.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
This commit is contained in:
Rusty Russell
2019-12-05 20:36:28 +10:30
committed by Christian Decker
parent bb538a1862
commit f7ebbb2ec5
17 changed files with 544 additions and 513 deletions

View File

@@ -68,39 +68,20 @@ struct hop_data_legacy {
u32 outgoing_cltv;
};
enum sphinx_payload_type {
SPHINX_V0_PAYLOAD = 0,
SPHINX_TLV_PAYLOAD = 1,
SPHINX_RAW_PAYLOAD = 255,
};
/*
* All the necessary information to generate a valid onion for this hop on a
* sphinx path. The payload is preserialized in order since the onion
* generation is payload agnostic. */
struct sphinx_hop {
struct pubkey pubkey;
enum sphinx_payload_type type;
const u8 *payload;
const u8 *raw_payload;
u8 hmac[HMAC_SIZE];
};
struct route_step {
enum route_next_case nextcase;
struct onionpacket *next;
enum sphinx_payload_type type;
union {
struct hop_data_legacy v0;
struct tlv_tlv_payload *tlv;
} payload;
u8 *raw_payload;
/* Quick access for internal use. */
struct amount_msat *amt_to_forward;
u32 *outgoing_cltv;
struct short_channel_id *forward_channel;
struct secret *payment_secret;
struct amount_msat *total_msat;
};
/**
@@ -237,30 +218,9 @@ struct sphinx_path *sphinx_path_new_with_key(const tal_t *ctx,
const struct secret *session_key);
/**
* Add a raw payload hop to the path.
* Add a payload hop to the path.
*/
void sphinx_add_raw_hop(struct sphinx_path *path, const struct pubkey *pubkey,
enum sphinx_payload_type type, const u8 *payload);
/**
* Add a non-final hop to the path.
*/
void sphinx_add_nonfinal_hop(struct sphinx_path *path,
const struct pubkey *pubkey,
bool use_tlv,
const struct short_channel_id *scid,
struct amount_msat forward,
u32 outgoing_cltv);
/**
* Add a final hop to the path.
*/
bool sphinx_add_final_hop(struct sphinx_path *path,
const struct pubkey *pubkey,
bool use_tlv,
struct amount_msat forward,
u32 outgoing_cltv,
struct amount_msat total_msat,
const struct secret *payment_secret);
void sphinx_add_hop(struct sphinx_path *path, const struct pubkey *pubkey,
const u8 *payload TAKES);
#endif /* LIGHTNING_COMMON_SPHINX_H */