1 // Imports that need to be added manually
3 use bitcoin::blockdata::script::ScriptBuf;
4 use bitcoin::secp256k1::ecdh::SharedSecret;
5 use bitcoin::secp256k1::ecdsa::RecoverableSignature;
6 use bitcoin::secp256k1::schnorr;
7 use bitcoin::secp256k1::{self, PublicKey, Scalar, Secp256k1, SecretKey};
9 use lightning::blinded_path::{BlindedPath, EmptyNodeIdLookUp};
10 use lightning::ln::features::InitFeatures;
11 use lightning::ln::msgs::{self, DecodeError, OnionMessageHandler};
12 use lightning::ln::script::ShutdownScript;
13 use lightning::offers::invoice::UnsignedBolt12Invoice;
14 use lightning::offers::invoice_request::UnsignedInvoiceRequest;
15 use lightning::onion_message::messenger::{
16 CustomOnionMessageHandler, Destination, MessageRouter, OnionMessagePath, OnionMessenger,
17 PendingOnionMessage, Responder, ResponseInstruction,
19 use lightning::onion_message::offers::{OffersMessage, OffersMessageHandler};
20 use lightning::onion_message::packet::OnionMessageContents;
21 use lightning::sign::{EntropySource, KeyMaterial, NodeSigner, Recipient, SignerProvider};
22 use lightning::util::logger::Logger;
23 use lightning::util::ser::{Readable, Writeable, Writer};
24 use lightning::util::test_channel_signer::TestChannelSigner;
26 use crate::utils::test_logger;
28 use std::io::{self, Cursor};
29 use std::sync::atomic::{AtomicU64, Ordering};
32 /// Actual fuzz test, method signature and name are fixed
33 pub fn do_test<L: Logger>(data: &[u8], logger: &L) {
34 if let Ok(msg) = <msgs::OnionMessage as Readable>::read(&mut Cursor::new(data)) {
35 let mut secret_bytes = [1; 32];
37 let secret = SecretKey::from_slice(&secret_bytes).unwrap();
38 let keys_manager = KeyProvider { node_secret: secret, counter: AtomicU64::new(0) };
39 let node_id_lookup = EmptyNodeIdLookUp {};
40 let message_router = TestMessageRouter {};
41 let offers_msg_handler = TestOffersMessageHandler {};
42 let custom_msg_handler = TestCustomMessageHandler {};
43 let onion_messenger = OnionMessenger::new(
54 let mut secret_bytes = [0; 32];
56 let secret = SecretKey::from_slice(&secret_bytes).unwrap();
57 PublicKey::from_secret_key(&Secp256k1::signing_only(), &secret)
60 let mut features = InitFeatures::empty();
61 features.set_onion_messages_optional();
62 let init = msgs::Init { features, networks: None, remote_network_address: None };
64 onion_messenger.peer_connected(&peer_node_id, &init, false).unwrap();
65 onion_messenger.handle_onion_message(&peer_node_id, &msg);
69 /// Method that needs to be added manually, {name}_test
70 pub fn onion_message_test<Out: test_logger::Output>(data: &[u8], out: Out) {
71 let logger = test_logger::TestLogger::new("".to_owned(), out);
72 do_test(data, &logger);
75 /// Method that needs to be added manually, {name}_run
77 pub extern "C" fn onion_message_run(data: *const u8, datalen: usize) {
78 let logger = test_logger::TestLogger::new("".to_owned(), test_logger::DevNull {});
79 do_test(unsafe { std::slice::from_raw_parts(data, datalen) }, &logger);
82 struct TestMessageRouter {}
84 impl MessageRouter for TestMessageRouter {
86 &self, _sender: PublicKey, _peers: Vec<PublicKey>, destination: Destination,
87 ) -> Result<OnionMessagePath, ()> {
88 Ok(OnionMessagePath { intermediate_nodes: vec![], destination, first_node_addresses: None })
91 fn create_blinded_paths<T: secp256k1::Signing + secp256k1::Verification>(
92 &self, _recipient: PublicKey, _peers: Vec<PublicKey>, _secp_ctx: &Secp256k1<T>,
93 ) -> Result<Vec<BlindedPath>, ()> {
98 struct TestOffersMessageHandler {}
100 impl OffersMessageHandler for TestOffersMessageHandler {
102 &self, _message: OffersMessage, _responder: Option<Responder>,
103 ) -> ResponseInstruction<OffersMessage> {
104 ResponseInstruction::NoResponse
109 struct TestCustomMessage {}
111 const CUSTOM_MESSAGE_TYPE: u64 = 4242;
112 const CUSTOM_MESSAGE_CONTENTS: [u8; 32] = [42; 32];
114 impl OnionMessageContents for TestCustomMessage {
115 fn tlv_type(&self) -> u64 {
118 fn msg_type(&self) -> &'static str {
123 impl Writeable for TestCustomMessage {
124 fn write<W: Writer>(&self, w: &mut W) -> Result<(), io::Error> {
125 Ok(CUSTOM_MESSAGE_CONTENTS.write(w)?)
129 struct TestCustomMessageHandler {}
131 impl CustomOnionMessageHandler for TestCustomMessageHandler {
132 type CustomMessage = TestCustomMessage;
133 fn handle_custom_message(
134 &self, message: Self::CustomMessage, responder: Option<Responder>,
135 ) -> ResponseInstruction<Self::CustomMessage> {
137 Some(responder) => responder.respond(message),
138 None => ResponseInstruction::NoResponse,
141 fn read_custom_message<R: io::Read>(
142 &self, _message_type: u64, buffer: &mut R,
143 ) -> Result<Option<Self::CustomMessage>, msgs::DecodeError> {
144 let mut buf = Vec::new();
145 buffer.read_to_end(&mut buf)?;
146 return Ok(Some(TestCustomMessage {}));
148 fn release_pending_custom_messages(&self) -> Vec<PendingOnionMessage<Self::CustomMessage>> {
153 pub struct VecWriter(pub Vec<u8>);
154 impl Writer for VecWriter {
155 fn write_all(&mut self, buf: &[u8]) -> Result<(), ::std::io::Error> {
156 self.0.extend_from_slice(buf);
161 node_secret: SecretKey,
165 impl EntropySource for KeyProvider {
166 fn get_secure_random_bytes(&self) -> [u8; 32] {
167 let ctr = self.counter.fetch_add(1, Ordering::Relaxed);
169 let random_bytes = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
170 (ctr >> 8*7) as u8, (ctr >> 8*6) as u8, (ctr >> 8*5) as u8, (ctr >> 8*4) as u8,
171 (ctr >> 8*3) as u8, (ctr >> 8*2) as u8, (ctr >> 8*1) as u8, 14, (ctr >> 8*0) as u8];
176 impl NodeSigner for KeyProvider {
177 fn get_node_id(&self, recipient: Recipient) -> Result<PublicKey, ()> {
178 let node_secret = match recipient {
179 Recipient::Node => Ok(&self.node_secret),
180 Recipient::PhantomNode => Err(()),
182 Ok(PublicKey::from_secret_key(&Secp256k1::signing_only(), node_secret))
186 &self, recipient: Recipient, other_key: &PublicKey, tweak: Option<&Scalar>,
187 ) -> Result<SharedSecret, ()> {
188 let mut node_secret = match recipient {
189 Recipient::Node => Ok(self.node_secret.clone()),
190 Recipient::PhantomNode => Err(()),
192 if let Some(tweak) = tweak {
193 node_secret = node_secret.mul_tweak(tweak).map_err(|_| ())?;
195 Ok(SharedSecret::new(other_key, &node_secret))
198 fn get_inbound_payment_key_material(&self) -> KeyMaterial {
203 &self, _hrp_bytes: &[u8], _invoice_data: &[u5], _recipient: Recipient,
204 ) -> Result<RecoverableSignature, ()> {
208 fn sign_bolt12_invoice_request(
209 &self, _invoice_request: &UnsignedInvoiceRequest,
210 ) -> Result<schnorr::Signature, ()> {
214 fn sign_bolt12_invoice(
215 &self, _invoice: &UnsignedBolt12Invoice,
216 ) -> Result<schnorr::Signature, ()> {
220 fn sign_gossip_message(
221 &self, _msg: lightning::ln::msgs::UnsignedGossipMessage,
222 ) -> Result<bitcoin::secp256k1::ecdsa::Signature, ()> {
227 impl SignerProvider for KeyProvider {
228 type EcdsaSigner = TestChannelSigner;
230 type TaprootSigner = TestChannelSigner;
232 fn generate_channel_keys_id(
233 &self, _inbound: bool, _channel_value_satoshis: u64, _user_channel_id: u128,
238 fn derive_channel_signer(
239 &self, _channel_value_satoshis: u64, _channel_keys_id: [u8; 32],
240 ) -> Self::EcdsaSigner {
244 fn read_chan_signer(&self, _data: &[u8]) -> Result<TestChannelSigner, DecodeError> {
248 fn get_destination_script(&self, _channel_keys_id: [u8; 32]) -> Result<ScriptBuf, ()> {
252 fn get_shutdown_scriptpubkey(&self) -> Result<ShutdownScript, ()> {
259 use bitcoin::hashes::hex::FromHex;
260 use lightning::util::logger::{Logger, Record};
261 use std::collections::HashMap;
262 use std::sync::Mutex;
264 struct TrackingLogger {
265 /// (module, message) -> count
266 pub lines: Mutex<HashMap<(String, String), usize>>,
268 impl Logger for TrackingLogger {
269 fn log(&self, record: Record) {
270 let mut lines_lock = self.lines.lock().unwrap();
271 let key = (record.module_path.to_string(), format!("{}", record.args));
272 *lines_lock.entry(key).or_insert(0) += 1;
274 "{:<5} [{} : {}, {}] {}",
275 record.level.to_string(),
285 fn test_no_onion_message_breakage() {
287 020000000000000000000000000000000000000000000000000000000000000e01055600020000000000000\
288 000000000000000000000000000000000000000000000000e01ae0276020000000000000000000000000000\
289 000000000000000000000000000000000002020000000000000000000000000000000000000000000000000\
290 000000000000e0101022a0000000000000000000000000000014551231950b75fc4402da1732fc9bebf0010\
291 9500000000000000000000000000000004106d000000000000000000000000000000fd1092202a2a2a2a2a2\
292 a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a0000000000000000000000000000000000\
293 000000000000000000000000000000000000000000000000000000000000000000000000000000000000000\
294 000000000000000000000000000000000000000000000000000000000000000000000000000000000000000\
295 000000000000000000000000000000000000000000000000000000000000000000000000000000000000000\
296 000000000000000000000000000000000000000000000000000000000000000000000000000000000000000\
297 000000000000000000000000000000000000000000000000000000000000000000000000000000000000000\
298 000000000000000000000000000000000000000000000000000000000000000000000000000000000000000\
299 000000000000000000000000000000000000000000000000000000000000000000000000000000000000000\
300 000000000000000000000000000000000000000000000000000000000000000000000000000000000000000\
301 000000000000000000000000000000000000000000000000000000000000000000000000000000000000000\
302 000000000000000000000000000000000000000000000000000000000000000000000000000000000000000\
303 000000000000000000000000000000000000000000000000000000000000000000000000000000000000000\
304 000000000000000000000000000000000000000000000000000000000000000000000000000000000000000\
305 000000000000000000000000000000000000000000000000000000000000000000000000000000000000000\
306 000000000000000000000000000000000000000000000000000000000000000000000000000000000000000\
307 000000000000000000000000000000000000000000000000000000000000000000000000000000000000000\
308 000000000000000000000000000000000000000000000000000000000000000000000000000000000000000\
309 000000000000000000000000000000000000000000000000000000000000000000000000000000000000000\
310 000000000000000000000000000000000000000000000000000000000000000000000000000000000000000\
311 000000000000000000000000000000000000000000000000000000000000000000000000000000000000000\
312 000000000000000000000000000000000000000000000000000000000000000000000000000000000000000\
313 000000000000000000000000000000000000000000000000000000000000000000000000000000000000000\
314 000000000000000000000000000000000000000000000000000000000000000000000000000000000000000\
315 000000000000000000000000000000000000000000000000000000000000000000000000000000000000000\
316 000000000000000000000000000000000000000000000000000000000000000000000000000000000000000\
317 000000000000000000000000000000000000000000000000000000000000000000000000000000000000000\
318 000000000000000000000000000000000000000005600000000000000000000000000000000000000000000\
320 let logger = TrackingLogger { lines: Mutex::new(HashMap::new()) };
321 super::do_test(&<Vec<u8>>::from_hex(one_hop_om).unwrap(), &logger);
323 let log_entries = logger.lines.lock().unwrap();
324 assert_eq!(log_entries.get(&("lightning::onion_message::messenger".to_string(),
325 "Received an onion message with path_id None and a reply_path: Custom(TestCustomMessage)"
326 .to_string())), Some(&1));
327 assert_eq!(log_entries.get(&("lightning::onion_message::messenger".to_string(),
328 "Constructing onion message when responding with Custom Message to an onion message with path_id None: TestCustomMessage".to_string())), Some(&1));
329 assert_eq!(log_entries.get(&("lightning::onion_message::messenger".to_string(),
330 "Buffered onion message when responding with Custom Message to an onion message with path_id None".to_string())), Some(&1));
333 let two_unblinded_hops_om = "\
334 020000000000000000000000000000000000000000000000000000000000000e01055600020000000000000\
335 000000000000000000000000000000000000000000000000e01350433042102020202020202020202020202\
336 02020202020202020202020202020202020202026d000000000000000000000000000000eb0000000000000\
337 000000000000000000000000000000000000000000000000036041096000000000000000000000000000000\
338 fd1092202a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a000000000000000\
339 000000000000000000000000000000000000000000000000000000000000000000000000000000000000000\
340 000000000000000000000000000000000000000000000000000000000000000000000000000000000000000\
341 000000000000000000000000000000000000000000000000000000000000000000000000000000000000000\
342 000000000000000000000000000000000000000000000000000000000000000000000000000000000000000\
343 000000000000000000000000000000000000000000000000000000000000000000000000000000000000000\
344 000000000000000000000000000000000000000000000000000000000000000000000000000000000000000\
345 000000000000000000000000000000000000000000000000000000000000000000000000000000000000000\
346 000000000000000000000000000000000000000000000000000000000000000000000000000000000000000\
347 000000000000000000000000000000000000000000000000000000000000000000000000000000000000000\
348 000000000000000000000000000000000000000000000000000000000000000000000000000000000000000\
349 000000000000000000000000000000000000000000000000000000000000000000000000000000000000000\
350 000000000000000000000000000000000000000000000000000000000000000000000000000000000000000\
351 000000000000000000000000000000000000000000000000000000000000000000000000000000000000000\
352 000000000000000000000000000000000000000000000000000000000000000000000000000000000000000\
353 000000000000000000000000000000000000000000000000000000000000000000000000000000000000000\
354 000000000000000000000000000000000000000000000000000000000000000000000000000000000000000\
355 000000000000000000000000000000000000000000000000000000000000000000000000000000000000000\
356 000000000000000000000000000000000000000000000000000000000000000000000000000000000000000\
357 000000000000000000000000000000000000000000000000000000000000000000000000000000000000000\
358 000000000000000000000000000000000000000000000000000000000000000000000000000000000000000\
359 000000000000000000000000000000000000000000000000000000000000000000000000000000000000000\
360 000000000000000000000000000000000000000000000000000000000000000000000000000000000000000\
361 000000000000000000000000000000000000000000000000000000000000000000000000000000000000000\
362 000000000000000000000000000000000000000000000000000000000000000000000000000000000000000\
363 000000000000000000000000000000000000000000000000000000000000000000000000000000000000000\
364 000000000000000000000000000000000000000000000000000000000000000000000000000000000000000\
365 000000000000000000000000000000000000000004800000000000000000000000000000000000000000000\
367 let logger = TrackingLogger { lines: Mutex::new(HashMap::new()) };
368 super::do_test(&<Vec<u8>>::from_hex(two_unblinded_hops_om).unwrap(), &logger);
370 let log_entries = logger.lines.lock().unwrap();
371 assert_eq!(log_entries.get(&("lightning::onion_message::messenger".to_string(), "Forwarding an onion message to peer 020202020202020202020202020202020202020202020202020202020202020202".to_string())), Some(&1));
374 let two_unblinded_two_blinded_om = "\
375 020000000000000000000000000000000000000000000000000000000000000e01055600020000000000000\
376 000000000000000000000000000000000000000000000000e01350433042102020202020202020202020202\
377 02020202020202020202020202020202020202026d0000000000000000000000000000009e0000000000000\
378 000000000000000000000000000000000000000000000000058045604210203030303030303030303030303\
379 030303030303030303030303030303030303020821020000000000000000000000000000000000000000000\
380 000000000000000000e0196000000000000000000000000000000e900000000000000000000000000000000\
381 000000000000000000000000000000350433042102040404040404040404040404040404040404040404040\
382 4040404040404040402ca000000000000000000000000000000420000000000000000000000000000000000\
383 00000000000000000000000000003604103f000000000000000000000000000000fd1092202a2a2a2a2a2a2\
384 a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a000000000000000000000000000000000000\
385 000000000000000000000000000000000000000000000000000000000000000000000000000000000000000\
386 000000000000000000000000000000000000000000000000000000000000000000000000000000000000000\
387 000000000000000000000000000000000000000000000000000000000000000000000000000000000000000\
388 000000000000000000000000000000000000000000000000000000000000000000000000000000000000000\
389 000000000000000000000000000000000000000000000000000000000000000000000000000000000000000\
390 000000000000000000000000000000000000000000000000000000000000000000000000000000000000000\
391 000000000000000000000000000000000000000000000000000000000000000000000000000000000000000\
392 000000000000000000000000000000000000000000000000000000000000000000000000000000000000000\
393 000000000000000000000000000000000000000000000000000000000000000000000000000000000000000\
394 000000000000000000000000000000000000000000000000000000000000000000000000000000000000000\
395 000000000000000000000000000000000000000000000000000000000000000000000000000000000000000\
396 000000000000000000000000000000000000000000000000000000000000000000000000000000000000000\
397 000000000000000000000000000000000000000000000000000000000000000000000000000000000000000\
398 000000000000000000000000000000000000000000000000000000000000000000000000000000000000000\
399 000000000000000000000000000000000000000000000000000000000000000000000000000000000000000\
400 000000000000000000000000000000000000000000000000000000000000000000000000000000000000000\
401 000000000000000000000000000000000000000000000000000000000000000000000000000000000000000\
402 000000000000000000000000000000000000000000000000000000000000000000000000000000000000000\
403 000000000000000000000000000000000000000000000000000000000000000000000000000000000000000\
404 000000000000000000000000000000000000000000000000000000000000000000000000000000000000000\
405 000000000000000000000000000000000000000000000000000000000000000000000000000000000000000\
406 000000000000000000000000000000000000000004800000000000000000000000000000000000000000000\
408 let logger = TrackingLogger { lines: Mutex::new(HashMap::new()) };
409 super::do_test(&<Vec<u8>>::from_hex(two_unblinded_two_blinded_om).unwrap(), &logger);
411 let log_entries = logger.lines.lock().unwrap();
412 assert_eq!(log_entries.get(&("lightning::onion_message::messenger".to_string(), "Forwarding an onion message to peer 020202020202020202020202020202020202020202020202020202020202020202".to_string())), Some(&1));
415 let three_blinded_om = "\
416 020000000000000000000000000000000000000000000000000000000000000e01055600020000000000000\
417 000000000000000000000000000000000000000000000000e01350433042102020202020202020202020202\
418 02020202020202020202020202020202020202026d000000000000000000000000000000b20000000000000\
419 000000000000000000000000000000000000000000000000035043304210203030303030303030303030303\
420 030303030303030303030303030303030303029600000000000000000000000000000033000000000000000\
421 000000000000000000000000000000000000000000000003604104e000000000000000000000000000000fd\
422 1092202a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a00000000000000000\
423 000000000000000000000000000000000000000000000000000000000000000000000000000000000000000\
424 000000000000000000000000000000000000000000000000000000000000000000000000000000000000000\
425 000000000000000000000000000000000000000000000000000000000000000000000000000000000000000\
426 000000000000000000000000000000000000000000000000000000000000000000000000000000000000000\
427 000000000000000000000000000000000000000000000000000000000000000000000000000000000000000\
428 000000000000000000000000000000000000000000000000000000000000000000000000000000000000000\
429 000000000000000000000000000000000000000000000000000000000000000000000000000000000000000\
430 000000000000000000000000000000000000000000000000000000000000000000000000000000000000000\
431 000000000000000000000000000000000000000000000000000000000000000000000000000000000000000\
432 000000000000000000000000000000000000000000000000000000000000000000000000000000000000000\
433 000000000000000000000000000000000000000000000000000000000000000000000000000000000000000\
434 000000000000000000000000000000000000000000000000000000000000000000000000000000000000000\
435 000000000000000000000000000000000000000000000000000000000000000000000000000000000000000\
436 000000000000000000000000000000000000000000000000000000000000000000000000000000000000000\
437 000000000000000000000000000000000000000000000000000000000000000000000000000000000000000\
438 000000000000000000000000000000000000000000000000000000000000000000000000000000000000000\
439 000000000000000000000000000000000000000000000000000000000000000000000000000000000000000\
440 000000000000000000000000000000000000000000000000000000000000000000000000000000000000000\
441 000000000000000000000000000000000000000000000000000000000000000000000000000000000000000\
442 000000000000000000000000000000000000000000000000000000000000000000000000000000000000000\
443 000000000000000000000000000000000000000000000000000000000000000000000000000000000000000\
444 000000000000000000000000000000000000000000000000000000000000000000000000000000000000000\
445 000000000000000000000000000000000000000000000000000000000000000000000000000000000000000\
446 000000000000000000000000000000000000000000000000000000000000000000000000000000000000000\
447 000000000000000000000000000000000000000004800000000000000000000000000000000000000000000\
449 let logger = TrackingLogger { lines: Mutex::new(HashMap::new()) };
450 super::do_test(&<Vec<u8>>::from_hex(three_blinded_om).unwrap(), &logger);
452 let log_entries = logger.lines.lock().unwrap();
453 assert_eq!(log_entries.get(&("lightning::onion_message::messenger".to_string(), "Forwarding an onion message to peer 020202020202020202020202020202020202020202020202020202020202020202".to_string())), Some(&1));