Skip to content

Commit eee117e

Browse files
Implement receiving and forwarding onion messages
This required adapting `onion_utils::decode_next_hop` to work for both payments and onion messages. Currently we just print out the path_id of any onion messages we receive. In the future, these received onion messages will be redirected to their respective handlers: i.e. an invoice_request will go to an InvoiceHandler, custom onion messages will go to a custom handler, etc.
1 parent e4748ab commit eee117e

File tree

3 files changed

+230
-21
lines changed

3 files changed

+230
-21
lines changed

lightning/src/ln/channelmanager.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -2150,7 +2150,7 @@ impl<Signer: Sign, M: Deref, T: Deref, K: Deref, F: Deref, L: Deref> ChannelMana
21502150
}
21512151
}
21522152

2153-
let next_hop = match onion_utils::decode_next_hop(shared_secret, &msg.onion_routing_packet.hop_data[..], msg.onion_routing_packet.hmac, msg.payment_hash) {
2153+
let next_hop = match onion_utils::decode_next_payment_hop(shared_secret, &msg.onion_routing_packet.hop_data[..], msg.onion_routing_packet.hmac, msg.payment_hash) {
21542154
Ok(res) => res,
21552155
Err(onion_utils::OnionDecodeErr::Malformed { err_msg, err_code }) => {
21562156
return_malformed_err!(err_msg, err_code);
@@ -3049,7 +3049,7 @@ impl<Signer: Sign, M: Deref, T: Deref, K: Deref, F: Deref, L: Deref> ChannelMana
30493049
let phantom_secret_res = self.keys_manager.get_node_secret(Recipient::PhantomNode);
30503050
if phantom_secret_res.is_ok() && fake_scid::is_valid_phantom(&self.fake_scid_rand_bytes, short_chan_id) {
30513051
let phantom_shared_secret = SharedSecret::new(&onion_packet.public_key.unwrap(), &phantom_secret_res.unwrap()).secret_bytes();
3052-
let next_hop = match onion_utils::decode_next_hop(phantom_shared_secret, &onion_packet.hop_data, onion_packet.hmac, payment_hash) {
3052+
let next_hop = match onion_utils::decode_next_payment_hop(phantom_shared_secret, &onion_packet.hop_data, onion_packet.hmac, payment_hash) {
30533053
Ok(res) => res,
30543054
Err(onion_utils::OnionDecodeErr::Malformed { err_msg, err_code }) => {
30553055
let sha256_of_onion = Sha256::hash(&onion_packet.hop_data).into_inner();

lightning/src/ln/onion_utils.rs

+98-15
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ use routing::gossip::NetworkUpdate;
1616
use routing::router::RouteHop;
1717
use util::chacha20::{ChaCha20, ChaChaReader};
1818
use util::errors::{self, APIError};
19-
use util::ser::{Readable, Writeable, LengthCalculatingWriter};
19+
use util::ser::{Readable, ReadableArgs, Writeable, LengthCalculatingWriter};
2020
use util::logger::Logger;
2121

2222
use bitcoin::hashes::{Hash, HashEngine};
@@ -75,7 +75,7 @@ pub(super) fn gen_ammag_from_shared_secret(shared_secret: &[u8]) -> [u8; 32] {
7575
Hmac::from_engine(hmac).into_inner()
7676
}
7777

78-
pub(super) fn next_hop_packet_pubkey<T: secp256k1::Signing + secp256k1::Verification>(secp_ctx: &Secp256k1<T>, mut packet_pubkey: PublicKey, packet_shared_secret: &[u8; 32]) -> Result<PublicKey, secp256k1::Error> {
78+
pub(crate) fn next_hop_packet_pubkey<T: secp256k1::Signing + secp256k1::Verification>(secp_ctx: &Secp256k1<T>, mut packet_pubkey: PublicKey, packet_shared_secret: &[u8; 32]) -> Result<PublicKey, secp256k1::Error> {
7979
let blinding_factor = {
8080
let mut sha = Sha256::engine();
8181
sha.input(&packet_pubkey.serialize()[..]);
@@ -632,7 +632,37 @@ pub(super) fn process_onion_failure<T: secp256k1::Signing, L: Deref>(secp_ctx: &
632632
} else { unreachable!(); }
633633
}
634634

635-
/// Data decrypted from the onion payload.
635+
/// Used in the decoding of inbound payments' and onion messages' routing packets. This enum allows
636+
/// us to use `decode_next_hop` to return the payloads and next hop packet bytes of both payments
637+
/// and onion messages.
638+
enum Payload {
639+
/// This payload was for an incoming payment.
640+
Payment(msgs::OnionHopData),
641+
/// This payload was for an incoming onion message.
642+
Message(onion_message::Payload),
643+
}
644+
645+
enum NextPacketBytes {
646+
Payment([u8; 20*65]),
647+
Message(Vec<u8>),
648+
}
649+
650+
/// Data decrypted from an onion message's onion payload.
651+
pub(crate) enum MessageHop {
652+
/// This onion payload was for us, not for forwarding to a next-hop.
653+
Receive(onion_message::Payload),
654+
/// This onion payload needs to be forwarded to a next-hop.
655+
Forward {
656+
/// Onion payload data used in forwarding the onion message.
657+
next_hop_data: onion_message::Payload,
658+
/// HMAC of the next hop's onion packet.
659+
next_hop_hmac: [u8; 32],
660+
/// Bytes of the onion packet we're forwarding.
661+
new_packet_bytes: Vec<u8>,
662+
},
663+
}
664+
665+
/// Data decrypted from a payment's onion payload.
636666
pub(crate) enum Hop {
637667
/// This onion payload was for us, not for forwarding to a next-hop. Contains information for
638668
/// verifying the incoming payment.
@@ -649,6 +679,7 @@ pub(crate) enum Hop {
649679
}
650680

651681
/// Error returned when we fail to decode the onion packet.
682+
#[derive(Debug)]
652683
pub(crate) enum OnionDecodeErr {
653684
/// The HMAC of the onion packet did not match the hop data.
654685
Malformed {
@@ -662,11 +693,44 @@ pub(crate) enum OnionDecodeErr {
662693
},
663694
}
664695

665-
pub(crate) fn decode_next_hop(shared_secret: [u8; 32], hop_data: &[u8], hmac_bytes: [u8; 32], payment_hash: PaymentHash) -> Result<Hop, OnionDecodeErr> {
696+
pub(crate) fn decode_next_message_hop(shared_secret: [u8; 32], hop_data: &[u8], hmac_bytes: [u8; 32], encrypted_tlvs_ss: SharedSecret) -> Result<MessageHop, OnionDecodeErr> {
697+
match decode_next_hop(shared_secret, hop_data, hmac_bytes, None, Some(encrypted_tlvs_ss)) {
698+
Ok((Payload::Message(next_hop_data), None)) => Ok(MessageHop::Receive(next_hop_data)),
699+
Ok((Payload::Message(next_hop_data), Some((next_hop_hmac, NextPacketBytes::Message(new_packet_bytes))))) => {
700+
Ok(MessageHop::Forward {
701+
next_hop_data,
702+
next_hop_hmac,
703+
new_packet_bytes
704+
})
705+
},
706+
Err(e) => Err(e),
707+
_ => unreachable!()
708+
}
709+
}
710+
711+
pub(crate) fn decode_next_payment_hop(shared_secret: [u8; 32], hop_data: &[u8], hmac_bytes: [u8; 32], payment_hash: PaymentHash) -> Result<Hop, OnionDecodeErr> {
712+
match decode_next_hop(shared_secret, hop_data, hmac_bytes, Some(payment_hash), None) {
713+
Ok((Payload::Payment(next_hop_data), None)) => Ok(Hop::Receive(next_hop_data)),
714+
Ok((Payload::Payment(next_hop_data), Some((next_hop_hmac, NextPacketBytes::Payment(new_packet_bytes))))) => {
715+
Ok(Hop::Forward {
716+
next_hop_data,
717+
next_hop_hmac,
718+
new_packet_bytes
719+
})
720+
},
721+
Err(e) => Err(e),
722+
_ => unreachable!()
723+
}
724+
}
725+
726+
fn decode_next_hop(shared_secret: [u8; 32], hop_data: &[u8], hmac_bytes: [u8; 32], payment_hash: Option<PaymentHash>, encrypted_tlv_ss: Option<SharedSecret>) -> Result<(Payload, Option<([u8; 32], NextPacketBytes)>), OnionDecodeErr> {
727+
assert!(payment_hash.is_some() && encrypted_tlv_ss.is_none() || payment_hash.is_none() && encrypted_tlv_ss.is_some());
666728
let (rho, mu) = gen_rho_mu_from_shared_secret(&shared_secret);
667729
let mut hmac = HmacEngine::<Sha256>::new(&mu);
668730
hmac.input(hop_data);
669-
hmac.input(&payment_hash.0[..]);
731+
if let Some(payment_hash) = payment_hash {
732+
hmac.input(&payment_hash.0[..]);
733+
}
670734
if !fixed_time_eq(&Hmac::from_engine(hmac).into_inner(), &hmac_bytes) {
671735
return Err(OnionDecodeErr::Malformed {
672736
err_msg: "HMAC Check failed",
@@ -676,7 +740,20 @@ pub(crate) fn decode_next_hop(shared_secret: [u8; 32], hop_data: &[u8], hmac_byt
676740

677741
let mut chacha = ChaCha20::new(&rho, &[0u8; 8]);
678742
let mut chacha_stream = ChaChaReader { chacha: &mut chacha, read: Cursor::new(&hop_data[..]) };
679-
match <msgs::OnionHopData as Readable>::read(&mut chacha_stream) {
743+
let payload_read_res = if payment_hash.is_some() {
744+
match <msgs::OnionHopData as Readable>::read(&mut chacha_stream) {
745+
Ok(payload) => Ok(Payload::Payment(payload)),
746+
Err(e) => Err(e)
747+
}
748+
} else if encrypted_tlv_ss.is_some() {
749+
match <onion_message::Payload as ReadableArgs<SharedSecret>>::read(&mut chacha_stream, encrypted_tlv_ss.unwrap()) {
750+
Ok(payload) => Ok(Payload::Message(payload)),
751+
Err(e) => Err(e)
752+
}
753+
} else {
754+
unreachable!(); // We already asserted that one of these is `Some`
755+
};
756+
match payload_read_res {
680757
Err(err) => {
681758
let error_code = match err {
682759
msgs::DecodeError::UnknownVersion => 0x4000 | 1, // unknown realm byte
@@ -714,10 +791,17 @@ pub(crate) fn decode_next_hop(shared_secret: [u8; 32], hop_data: &[u8], hmac_byt
714791
chacha_stream.read_exact(&mut next_bytes).unwrap();
715792
assert_ne!(next_bytes[..], [0; 32][..]);
716793
}
717-
return Ok(Hop::Receive(msg));
794+
return Ok((msg, None));
718795
} else {
719-
let mut new_packet_bytes = [0; 20*65];
720-
let read_pos = chacha_stream.read(&mut new_packet_bytes).unwrap();
796+
let (mut new_packet_bytes, read_pos) = if payment_hash.is_some() {
797+
let mut new_packet_bytes = [0 as u8; 20*65];
798+
let read_pos = chacha_stream.read(&mut new_packet_bytes).unwrap();
799+
(NextPacketBytes::Payment(new_packet_bytes), read_pos)
800+
} else {
801+
let mut new_packet_bytes = vec![0 as u8; hop_data.len()];
802+
let read_pos = chacha_stream.read(&mut new_packet_bytes).unwrap();
803+
(NextPacketBytes::Message(new_packet_bytes), read_pos)
804+
};
721805
#[cfg(debug_assertions)]
722806
{
723807
// Check two things:
@@ -729,12 +813,11 @@ pub(crate) fn decode_next_hop(shared_secret: [u8; 32], hop_data: &[u8], hmac_byt
729813
}
730814
// Once we've emptied the set of bytes our peer gave us, encrypt 0 bytes until we
731815
// fill the onion hop data we'll forward to our next-hop peer.
732-
chacha_stream.chacha.process_in_place(&mut new_packet_bytes[read_pos..]);
733-
return Ok(Hop::Forward {
734-
next_hop_data: msg,
735-
next_hop_hmac: hmac,
736-
new_packet_bytes,
737-
})
816+
match new_packet_bytes {
817+
NextPacketBytes::Payment(ref mut bytes) => chacha_stream.chacha.process_in_place(&mut bytes[read_pos..]),
818+
NextPacketBytes::Message(ref mut bytes) => chacha_stream.chacha.process_in_place(&mut bytes[read_pos..]),
819+
}
820+
return Ok((msg, Some((hmac, new_packet_bytes))))
738821
}
739822
},
740823
}

lightning/src/onion_message.rs

+130-4
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,13 @@ use bitcoin::hashes::sha256::Hash as Sha256;
1414
use bitcoin::secp256k1::{self, PublicKey, Secp256k1, SecretKey};
1515
use bitcoin::secp256k1::ecdh::SharedSecret;
1616

17-
use chain::keysinterface::{InMemorySigner, KeysInterface, KeysManager, Sign};
17+
use chain::keysinterface::{InMemorySigner, KeysInterface, KeysManager, Recipient, Sign};
1818
use ln::msgs::{self, DecodeError, OnionMessageHandler};
1919
use ln::onion_utils;
20-
use util::chacha20poly1305rfc::ChaChaPolyWriteAdapter;
20+
use util::chacha20poly1305rfc::{ChaChaPolyReadAdapter, ChaChaPolyWriteAdapter};
2121
use util::events::{MessageSendEvent, MessageSendEventsProvider};
2222
use util::logger::Logger;
23-
use util::ser::{IgnoringLengthReadable, LengthRead, LengthReadable, Readable, VecWriter, Writeable, Writer};
23+
use util::ser::{FixedLengthReader, IgnoringLengthReadable, LengthRead, LengthReadable, LengthReadableArgs, Readable, ReadableArgs, VecWriter, Writeable, Writer};
2424

2525
use core::mem;
2626
use core::ops::Deref;
@@ -125,6 +125,41 @@ impl Writeable for (Payload, [u8; 32]) {
125125
}
126126
}
127127

128+
/// Reads of `Payload`s are parameterized by the `rho` of a `SharedSecret`, which is used to decrypt
129+
/// the onion message payload's `encrypted_data` field.
130+
impl ReadableArgs<SharedSecret> for Payload {
131+
fn read<R: Read>(mut r: &mut R, encrypted_tlvs_ss: SharedSecret) -> Result<Self, DecodeError> {
132+
use bitcoin::consensus::encode::{Decodable, Error, VarInt};
133+
let v: VarInt = Decodable::consensus_decode(&mut r)
134+
.map_err(|e| match e {
135+
Error::Io(ioe) => DecodeError::from(ioe),
136+
_ => DecodeError::InvalidValue
137+
})?;
138+
if v.0 == 0 { // 0-length payload
139+
return Err(DecodeError::InvalidValue)
140+
}
141+
142+
let mut rd = FixedLengthReader::new(r, v.0);
143+
// TODO: support reply paths
144+
let mut _reply_path_bytes: Option<Vec<u8>> = Some(Vec::new());
145+
let mut read_adapter: Option<ChaChaPolyReadAdapter<ControlTlvs>> = None;
146+
let (rho, _) = onion_utils::gen_rho_mu_from_shared_secret(&encrypted_tlvs_ss.secret_bytes());
147+
decode_tlv_stream!(&mut rd, {
148+
(2, _reply_path_bytes, vec_type),
149+
(4, read_adapter, (option: LengthReadableArgs, rho))
150+
});
151+
rd.eat_remaining().map_err(|_| DecodeError::ShortRead)?;
152+
153+
if read_adapter.is_none() {
154+
return Err(DecodeError::InvalidValue)
155+
}
156+
157+
Ok(Payload {
158+
encrypted_tlvs: EncryptedTlvs::Unblinded(read_adapter.unwrap().readable),
159+
})
160+
}
161+
}
162+
128163
/// Onion messages contain an encrypted TLV stream. This can be supplied by someone else, in the
129164
/// case that we're sending to a blinded route, or created by us if we're constructing payloads for
130165
/// unblinded hops in the onion message's path.
@@ -382,7 +417,98 @@ impl<Signer: Sign, K: Deref, L: Deref> OnionMessageHandler for OnionMessenger<Si
382417
where K::Target: KeysInterface<Signer = Signer>,
383418
L::Target: Logger,
384419
{
385-
fn handle_onion_message(&self, _peer_node_id: &PublicKey, msg: &msgs::OnionMessage) {}
420+
/// Handle an incoming onion message. Currently, if a message was destined for us we will log, but
421+
/// soon we'll delegate the onion message to a handler that can generate invoices or send
422+
/// payments.
423+
fn handle_onion_message(&self, _peer_node_id: &PublicKey, msg: &msgs::OnionMessage) {
424+
let node_secret = match self.keys_manager.get_node_secret(Recipient::Node) {
425+
Ok(secret) => secret,
426+
Err(e) => {
427+
log_trace!(self.logger, "Failed to retrieve node secret: {:?}", e);
428+
return
429+
}
430+
};
431+
let encrypted_data_ss = SharedSecret::new(&msg.blinding_point, &node_secret);
432+
let onion_decode_shared_secret = {
433+
let blinding_factor = {
434+
let mut hmac = HmacEngine::<Sha256>::new(b"blinded_node_id");
435+
hmac.input(encrypted_data_ss.as_ref());
436+
Hmac::from_engine(hmac).into_inner()
437+
};
438+
let mut blinded_priv = node_secret.clone();
439+
if let Err(e) = blinded_priv.mul_assign(&blinding_factor) {
440+
log_trace!(self.logger, "Failed to compute blinded public key: {}", e);
441+
return
442+
}
443+
SharedSecret::new(&msg.onion_routing_packet.public_key, &blinded_priv).secret_bytes()
444+
};
445+
match onion_utils::decode_next_message_hop(onion_decode_shared_secret, &msg.onion_routing_packet.hop_data[..], msg.onion_routing_packet.hmac, encrypted_data_ss) {
446+
Ok(onion_utils::MessageHop::Receive(Payload {
447+
encrypted_tlvs: EncryptedTlvs::Unblinded(ControlTlvs::Receive { path_id })
448+
})) => {
449+
log_info!(self.logger, "Received an onion message with path_id: {:02x?}", path_id);
450+
},
451+
Ok(onion_utils::MessageHop::Forward {
452+
next_hop_data: Payload {
453+
encrypted_tlvs: EncryptedTlvs::Unblinded(ControlTlvs::Receive { path_id }),
454+
}, .. }) => {
455+
// We received an onion message that had fake extra hops at the end of its blinded route.
456+
// TODO support adding extra hops to blinded routes and test this case
457+
log_info!(self.logger, "Received an onion message with path_id: {:02x?}", path_id);
458+
},
459+
Ok(onion_utils::MessageHop::Forward {
460+
next_hop_data: Payload {
461+
encrypted_tlvs: EncryptedTlvs::Unblinded(ControlTlvs::Forward { next_node_id, next_blinding_override }),
462+
},
463+
next_hop_hmac, new_packet_bytes
464+
}) => {
465+
let new_pubkey = match onion_utils::next_hop_packet_pubkey(&self.secp_ctx, msg.onion_routing_packet.public_key, &onion_decode_shared_secret) {
466+
Ok(pk) => pk,
467+
Err(e) => {
468+
log_trace!(self.logger, "Failed to compute next hop packet pubkey: {}", e);
469+
return
470+
}
471+
};
472+
let outgoing_packet = Packet {
473+
version: 0,
474+
public_key: new_pubkey,
475+
hop_data: new_packet_bytes.to_vec(),
476+
hmac: next_hop_hmac.clone(),
477+
};
478+
479+
let mut pending_msg_events = self.pending_msg_events.lock().unwrap();
480+
pending_msg_events.push(MessageSendEvent::SendOnionMessage {
481+
node_id: next_node_id,
482+
msg: msgs::OnionMessage {
483+
blinding_point: match next_blinding_override {
484+
Some(blinding_point) => blinding_point,
485+
None => {
486+
let blinding_factor = {
487+
let mut sha = Sha256::engine();
488+
sha.input(&msg.blinding_point.serialize()[..]);
489+
sha.input(encrypted_data_ss.as_ref());
490+
Sha256::from_engine(sha).into_inner()
491+
};
492+
let mut next_blinding_point = msg.blinding_point.clone();
493+
if let Err(e) = next_blinding_point.mul_assign(&self.secp_ctx, &blinding_factor[..]) {
494+
log_trace!(self.logger, "Failed to compute next blinding point: {}", e);
495+
return
496+
}
497+
next_blinding_point
498+
},
499+
},
500+
len: outgoing_packet.len(),
501+
onion_routing_packet: outgoing_packet,
502+
},
503+
});
504+
},
505+
Err(e) => {
506+
log_trace!(self.logger, "Errored decoding onion message packet: {:?}", e);
507+
},
508+
_ => {} // Unreachable unless someone encodes a `Forward` payload as the final payload, which
509+
// is bogus and should be fine to drop
510+
};
511+
}
386512
}
387513

388514
impl<Signer: Sign, K: Deref, L: Deref> MessageSendEventsProvider for OnionMessenger<Signer, K, L>

0 commit comments

Comments
 (0)