1 // Imports that need to be added manually
2 use bitcoin::bech32::u5;
3 use bitcoin::blockdata::script::Script;
4 use bitcoin::secp256k1::{PublicKey, Scalar, Secp256k1, SecretKey};
5 use bitcoin::secp256k1::ecdh::SharedSecret;
6 use bitcoin::secp256k1::ecdsa::RecoverableSignature;
8 use lightning::sign::{Recipient, KeyMaterial, EntropySource, NodeSigner, SignerProvider};
9 use lightning::ln::msgs::{self, DecodeError, OnionMessageHandler};
10 use lightning::ln::script::ShutdownScript;
11 use lightning::util::enforcing_trait_impls::EnforcingSigner;
12 use lightning::util::logger::Logger;
13 use lightning::util::ser::{Readable, Writeable, Writer};
14 use lightning::onion_message::{CustomOnionMessageContents, CustomOnionMessageHandler, Destination, MessageRouter, OffersMessage, OffersMessageHandler, OnionMessagePath, OnionMessenger};
16 use crate::utils::test_logger;
18 use std::io::{self, Cursor};
19 use std::sync::atomic::{AtomicU64, Ordering};
22 /// Actual fuzz test, method signature and name are fixed
23 pub fn do_test<L: Logger>(data: &[u8], logger: &L) {
24 if let Ok(msg) = <msgs::OnionMessage as Readable>::read(&mut Cursor::new(data)) {
25 let mut secret_bytes = [1; 32];
27 let secret = SecretKey::from_slice(&secret_bytes).unwrap();
28 let keys_manager = KeyProvider {
30 counter: AtomicU64::new(0),
32 let message_router = TestMessageRouter {};
33 let offers_msg_handler = TestOffersMessageHandler {};
34 let custom_msg_handler = TestCustomMessageHandler {};
35 let onion_messenger = OnionMessenger::new(
36 &keys_manager, &keys_manager, logger, &message_router, &offers_msg_handler,
39 let mut pk = [2; 33]; pk[1] = 0xff;
40 let peer_node_id_not_used = PublicKey::from_slice(&pk).unwrap();
41 onion_messenger.handle_onion_message(&peer_node_id_not_used, &msg);
45 /// Method that needs to be added manually, {name}_test
46 pub fn onion_message_test<Out: test_logger::Output>(data: &[u8], out: Out) {
47 let logger = test_logger::TestLogger::new("".to_owned(), out);
48 do_test(data, &logger);
51 /// Method that needs to be added manually, {name}_run
53 pub extern "C" fn onion_message_run(data: *const u8, datalen: usize) {
54 let logger = test_logger::TestLogger::new("".to_owned(), test_logger::DevNull {});
55 do_test(unsafe { std::slice::from_raw_parts(data, datalen) }, &logger);
58 struct TestMessageRouter {}
60 impl MessageRouter for TestMessageRouter {
62 &self, _sender: PublicKey, _peers: Vec<PublicKey>, destination: Destination
63 ) -> Result<OnionMessagePath, ()> {
65 intermediate_nodes: vec![],
71 struct TestOffersMessageHandler {}
73 impl OffersMessageHandler for TestOffersMessageHandler {
74 fn handle_message(&self, _message: OffersMessage) -> Option<OffersMessage> {
79 struct TestCustomMessage {}
81 const CUSTOM_MESSAGE_TYPE: u64 = 4242;
82 const CUSTOM_MESSAGE_CONTENTS: [u8; 32] = [42; 32];
84 impl CustomOnionMessageContents for TestCustomMessage {
85 fn tlv_type(&self) -> u64 {
90 impl Writeable for TestCustomMessage {
91 fn write<W: Writer>(&self, w: &mut W) -> Result<(), io::Error> {
92 Ok(CUSTOM_MESSAGE_CONTENTS.write(w)?)
96 struct TestCustomMessageHandler {}
98 impl CustomOnionMessageHandler for TestCustomMessageHandler {
99 type CustomMessage = TestCustomMessage;
100 fn handle_custom_message(&self, _msg: Self::CustomMessage) -> Option<Self::CustomMessage> {
101 Some(TestCustomMessage {})
103 fn read_custom_message<R: io::Read>(&self, _message_type: u64, buffer: &mut R) -> Result<Option<Self::CustomMessage>, msgs::DecodeError> {
104 let mut buf = Vec::new();
105 buffer.read_to_end(&mut buf)?;
106 return Ok(Some(TestCustomMessage {}))
110 pub struct VecWriter(pub Vec<u8>);
111 impl Writer for VecWriter {
112 fn write_all(&mut self, buf: &[u8]) -> Result<(), ::std::io::Error> {
113 self.0.extend_from_slice(buf);
118 node_secret: SecretKey,
122 impl EntropySource for KeyProvider {
123 fn get_secure_random_bytes(&self) -> [u8; 32] {
124 let ctr = self.counter.fetch_add(1, Ordering::Relaxed);
125 [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
126 (ctr >> 8*7) as u8, (ctr >> 8*6) as u8, (ctr >> 8*5) as u8, (ctr >> 8*4) as u8, (ctr >> 8*3) as u8, (ctr >> 8*2) as u8, (ctr >> 8*1) as u8, 14, (ctr >> 8*0) as u8]
130 impl NodeSigner for KeyProvider {
131 fn get_node_id(&self, recipient: Recipient) -> Result<PublicKey, ()> {
132 let node_secret = match recipient {
133 Recipient::Node => Ok(&self.node_secret),
134 Recipient::PhantomNode => Err(())
136 Ok(PublicKey::from_secret_key(&Secp256k1::signing_only(), node_secret))
139 fn ecdh(&self, recipient: Recipient, other_key: &PublicKey, tweak: Option<&Scalar>) -> Result<SharedSecret, ()> {
140 let mut node_secret = match recipient {
141 Recipient::Node => Ok(self.node_secret.clone()),
142 Recipient::PhantomNode => Err(())
144 if let Some(tweak) = tweak {
145 node_secret = node_secret.mul_tweak(tweak).map_err(|_| ())?;
147 Ok(SharedSecret::new(other_key, &node_secret))
150 fn get_inbound_payment_key_material(&self) -> KeyMaterial { unreachable!() }
152 fn sign_invoice(&self, _hrp_bytes: &[u8], _invoice_data: &[u5], _recipient: Recipient) -> Result<RecoverableSignature, ()> {
156 fn sign_gossip_message(&self, _msg: lightning::ln::msgs::UnsignedGossipMessage) -> Result<bitcoin::secp256k1::ecdsa::Signature, ()> {
161 impl SignerProvider for KeyProvider {
162 type Signer = EnforcingSigner;
164 fn generate_channel_keys_id(&self, _inbound: bool, _channel_value_satoshis: u64, _user_channel_id: u128) -> [u8; 32] { unreachable!() }
166 fn derive_channel_signer(&self, _channel_value_satoshis: u64, _channel_keys_id: [u8; 32]) -> Self::Signer {
170 fn read_chan_signer(&self, _data: &[u8]) -> Result<EnforcingSigner, DecodeError> { unreachable!() }
172 fn get_destination_script(&self) -> Result<Script, ()> { unreachable!() }
174 fn get_shutdown_scriptpubkey(&self) -> Result<ShutdownScript, ()> { unreachable!() }
179 use lightning::util::logger::{Logger, Record};
180 use std::collections::HashMap;
181 use std::sync::Mutex;
183 struct TrackingLogger {
184 /// (module, message) -> count
185 pub lines: Mutex<HashMap<(String, String), usize>>,
187 impl Logger for TrackingLogger {
188 fn log(&self, record: &Record) {
189 *self.lines.lock().unwrap().entry((record.module_path.to_string(), format!("{}", record.args))).or_insert(0) += 1;
190 println!("{:<5} [{} : {}, {}] {}", record.level.to_string(), record.module_path, record.file, record.line, record.args);
195 fn test_no_onion_message_breakage() {
196 let one_hop_om = "020000000000000000000000000000000000000000000000000000000000000e01055600020000000000000000000000000000000000000000000000000000000000000e01ae0276020000000000000000000000000000000000000000000000000000000000000002020000000000000000000000000000000000000000000000000000000000000e0101022a0000000000000000000000000000014551231950b75fc4402da1732fc9bebf00109500000000000000000000000000000004106d000000000000000000000000000000fd1092202a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000005600000000000000000000000000000000000000000000000000000000000000";
197 let logger = TrackingLogger { lines: Mutex::new(HashMap::new()) };
198 super::do_test(&::hex::decode(one_hop_om).unwrap(), &logger);
200 let log_entries = logger.lines.lock().unwrap();
201 assert_eq!(log_entries.get(&("lightning::onion_message::messenger".to_string(),
202 "Received an onion message with path_id None and a reply_path".to_string())), Some(&1));
203 assert_eq!(log_entries.get(&("lightning::onion_message::messenger".to_string(),
204 "Responding to onion message with path_id None".to_string())), Some(&1));
205 assert_eq!(log_entries.get(&("lightning::onion_message::messenger".to_string(),
206 "Failed responding to onion message with path_id None: TooFewBlindedHops".to_string())), Some(&1));
209 let two_unblinded_hops_om = "020000000000000000000000000000000000000000000000000000000000000e01055600020000000000000000000000000000000000000000000000000000000000000e0135043304210202020202020202020202020202020202020202020202020202020202020202026d000000000000000000000000000000eb0000000000000000000000000000000000000000000000000000000000000036041096000000000000000000000000000000fd1092202a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000004800000000000000000000000000000000000000000000000000000000000000";
210 let logger = TrackingLogger { lines: Mutex::new(HashMap::new()) };
211 super::do_test(&::hex::decode(two_unblinded_hops_om).unwrap(), &logger);
213 let log_entries = logger.lines.lock().unwrap();
214 assert_eq!(log_entries.get(&("lightning::onion_message::messenger".to_string(), "Forwarding an onion message to peer 020202020202020202020202020202020202020202020202020202020202020202".to_string())), Some(&1));
217 let two_unblinded_two_blinded_om = "020000000000000000000000000000000000000000000000000000000000000e01055600020000000000000000000000000000000000000000000000000000000000000e0135043304210202020202020202020202020202020202020202020202020202020202020202026d0000000000000000000000000000009e0000000000000000000000000000000000000000000000000000000000000058045604210203030303030303030303030303030303030303030303030303030303030303020821020000000000000000000000000000000000000000000000000000000000000e0196000000000000000000000000000000e9000000000000000000000000000000000000000000000000000000000000003504330421020404040404040404040404040404040404040404040404040404040404040402ca00000000000000000000000000000042000000000000000000000000000000000000000000000000000000000000003604103f000000000000000000000000000000fd1092202a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000004800000000000000000000000000000000000000000000000000000000000000";
218 let logger = TrackingLogger { lines: Mutex::new(HashMap::new()) };
219 super::do_test(&::hex::decode(two_unblinded_two_blinded_om).unwrap(), &logger);
221 let log_entries = logger.lines.lock().unwrap();
222 assert_eq!(log_entries.get(&("lightning::onion_message::messenger".to_string(), "Forwarding an onion message to peer 020202020202020202020202020202020202020202020202020202020202020202".to_string())), Some(&1));
225 let three_blinded_om = "020000000000000000000000000000000000000000000000000000000000000e01055600020000000000000000000000000000000000000000000000000000000000000e0135043304210202020202020202020202020202020202020202020202020202020202020202026d000000000000000000000000000000b20000000000000000000000000000000000000000000000000000000000000035043304210203030303030303030303030303030303030303030303030303030303030303029600000000000000000000000000000033000000000000000000000000000000000000000000000000000000000000003604104e000000000000000000000000000000fd1092202a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000004800000000000000000000000000000000000000000000000000000000000000";
226 let logger = TrackingLogger { lines: Mutex::new(HashMap::new()) };
227 super::do_test(&::hex::decode(three_blinded_om).unwrap(), &logger);
229 let log_entries = logger.lines.lock().unwrap();
230 assert_eq!(log_entries.get(&("lightning::onion_message::messenger".to_string(), "Forwarding an onion message to peer 020202020202020202020202020202020202020202020202020202020202020202".to_string())), Some(&1));