common/onion_message_parse: return string, not bool.

Allows for caller to log, but more importantly, when we add a command to
inject onion messages, allows for us to capture the error.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
This commit is contained in:
Rusty Russell 2024-07-10 12:22:19 +09:30 committed by Vincenzo Palazzo
parent a3c15e8a4a
commit ba82592196
4 changed files with 47 additions and 57 deletions

View File

@ -1,6 +1,7 @@
/* Caller does fromwire_onion_message(), this does the rest. */
#include "config.h"
#include <assert.h>
#include <ccan/tal/str/str.h>
#include <common/blindedpath.h>
#include <common/ecdh.h>
#include <common/onion_message_parse.h>
@ -70,10 +71,9 @@ static bool decrypt_forwarding_onionmsg(const struct pubkey *blinding,
}
/* Returns false on failure */
bool onion_message_parse(const tal_t *ctx,
const char *onion_message_parse(const tal_t *ctx,
const u8 *onion_message_packet,
const struct pubkey *blinding,
const struct node_id *peer,
const struct pubkey *me,
u8 **next_onion_msg,
struct pubkey *next_node_id,
@ -96,25 +96,21 @@ bool onion_message_parse(const tal_t *ctx,
tal_bytelen(onion_message_packet),
&badreason);
if (!op) {
status_peer_debug(peer, "onion_message_parse: can't parse onionpacket: %s",
return tal_fmt(ctx, "onion_message_parse: can't parse onionpacket: %s",
onion_wire_name(badreason));
return false;
}
ephemeral = op->ephemeralkey;
if (!unblind_onion(blinding, ecdh, &ephemeral, &ss)) {
status_peer_debug(peer, "onion_message_parse: can't unblind onionpacket");
return false;
return tal_fmt(ctx, "onion_message_parse: can't unblind onionpacket");
}
/* Now get onion shared secret and parse it. */
ecdh(&ephemeral, &onion_ss);
rs = process_onionpacket(tmpctx, op, &onion_ss, NULL, 0);
if (!rs) {
status_peer_debug(peer,
"onion_message_parse: can't process onionpacket ss=%s",
return tal_fmt(ctx, "onion_message_parse: can't process onionpacket ss=%s",
fmt_secret(tmpctx, &onion_ss));
return false;
}
/* The raw payload is prepended with length in the modern onion. */
@ -122,21 +118,18 @@ bool onion_message_parse(const tal_t *ctx,
max = tal_bytelen(rs->raw_payload);
maxlen = fromwire_bigsize(&cursor, &max);
if (!cursor) {
status_peer_debug(peer, "onion_message_parse: Invalid hop payload %s",
return tal_fmt(ctx, "onion_message_parse: Invalid hop payload %s",
tal_hex(tmpctx, rs->raw_payload));
return false;
}
if (maxlen > max) {
status_peer_debug(peer, "onion_message_parse: overlong hop payload %s",
return tal_fmt(ctx, "onion_message_parse: overlong hop payload %s",
tal_hex(tmpctx, rs->raw_payload));
return false;
}
om = fromwire_tlv_onionmsg_tlv(tmpctx, &cursor, &maxlen);
if (!om) {
status_peer_debug(peer, "onion_message_parse: invalid onionmsg_tlv %s",
return tal_fmt(ctx, "onion_message_parse: invalid onionmsg_tlv %s",
tal_hex(tmpctx, rs->raw_payload));
return false;
}
if (rs->nextcase == ONION_END) {
*next_onion_msg = NULL;
@ -149,10 +142,9 @@ bool onion_message_parse(const tal_t *ctx,
om->encrypted_recipient_data, me,
final_alias,
final_path_id)) {
status_peer_debug(peer,
return tal_fmt(ctx,
"onion_message_parse: failed to decrypt encrypted_recipient_data"
" %s", tal_hex(tmpctx, om->encrypted_recipient_data));
return false;
}
} else {
struct pubkey next_blinding;
@ -165,19 +157,15 @@ bool onion_message_parse(const tal_t *ctx,
* - MUST ignore the message.
*/
if (tal_count(om->fields) != 1) {
status_peer_debug(peer,
"onion_message_parse: "
"disallowed tlv field");
return false;
return tal_fmt(ctx, "onion_message_parse: disallowed tlv field");
}
/* This fails as expected if no enctlv. */
if (!decrypt_forwarding_onionmsg(blinding, &ss, om->encrypted_recipient_data, next_node_id,
&next_blinding)) {
status_peer_debug(peer,
return tal_fmt(ctx,
"onion_message_parse: invalid encrypted_recipient_data %s",
tal_hex(tmpctx, om->encrypted_recipient_data));
return false;
}
*next_onion_msg = towire_onion_message(ctx,
&next_blinding,
@ -186,5 +174,5 @@ bool onion_message_parse(const tal_t *ctx,
/* Exactly one is set */
assert(!*next_onion_msg + !*final_om == 1);
return true;
return NULL;
}

View File

@ -13,7 +13,6 @@ struct pubkey;
* @ctx: context to allocate @next_onion_msg or @final_om/@path_id off
* @onion_message_packet: Sphinx-encrypted onion
* @blinding: Blinding we were given for @onion_message_packet
* @peer: node_id of peer (for status_peer_debug msgs)
* @me: my pubkey
* @next_onion_msg (out): set if we should forward, otherwise NULL.
* @next_node_id (out): set to node id to fwd to, iff *@next_onion_msg.
@ -21,12 +20,11 @@ struct pubkey;
* @final_alias (out): our alias (if *@final_om), or our own ID
* @final_path_id (out): secret enclosed, if any (iff *@final_om).
*
* Returns false if it wasn't valid.
* Returns NULL if it was valid, otherwise an error string.
*/
bool onion_message_parse(const tal_t *ctx,
const char *onion_message_parse(const tal_t *ctx,
const u8 *onion_message_packet,
const struct pubkey *blinding,
const struct node_id *peer,
const struct pubkey *me,
u8 **next_onion_msg,
struct pubkey *next_node_id,

View File

@ -371,12 +371,12 @@ int main(int argc, char *argv[])
/* For test_ecdh */
mykey = &privkey[i];
assert(onion_message_parse(tmpctx, onion_message_packet, &blinding_pub, NULL,
assert(onion_message_parse(tmpctx, onion_message_packet, &blinding_pub,
&id[i],
&onion_message, &next_node_id,
&final_om,
&final_alias,
&final_path_id));
&final_path_id) == NULL);
if (onion_message) {
json_pubkey("next_node_id", &next_node_id);
} else {

View File

@ -46,6 +46,7 @@ void handle_onion_message(struct daemon *daemon,
struct tlv_onionmsg_tlv *final_om;
struct pubkey final_alias;
struct secret *final_path_id;
const char *err;
/* Ignore unless explicitly turned on. */
if (!feature_offered(daemon->our_features->bits[NODE_ANNOUNCE_FEATURE],
@ -60,11 +61,14 @@ void handle_onion_message(struct daemon *daemon,
return;
}
if (!onion_message_parse(tmpctx, onion, &blinding, &peer->id,
err = onion_message_parse(tmpctx, onion, &blinding,
&daemon->mykey,
&next_onion_msg, &next_node,
&final_om, &final_alias, &final_path_id))
&final_om, &final_alias, &final_path_id);
if (err) {
status_peer_debug(&peer->id, "%s", err);
return;
}
if (final_om) {
u8 *omsg;