]> git.bitcoin.ninja Git - rust-lightning/blob - fuzz/src/onion_message.rs
OffersMessageHandler trait for OnionMessenger
[rust-lightning] / fuzz / src / onion_message.rs
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;
7
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, OffersMessage, OffersMessageHandler, OnionMessenger};
15
16 use crate::utils::test_logger;
17
18 use std::io::{self, Cursor};
19 use std::sync::atomic::{AtomicU64, Ordering};
20
21 #[inline]
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 = [0; 32];
26                 secret_bytes[31] = 2;
27                 let secret = SecretKey::from_slice(&secret_bytes).unwrap();
28                 let keys_manager = KeyProvider {
29                         node_secret: secret,
30                         counter: AtomicU64::new(0),
31                 };
32                 let offers_msg_handler = TestOffersMessageHandler {};
33                 let custom_msg_handler = TestCustomMessageHandler {};
34                 let onion_messenger = OnionMessenger::new(
35                         &keys_manager, &keys_manager, logger, &offers_msg_handler, &custom_msg_handler
36                 );
37                 let mut pk = [2; 33]; pk[1] = 0xff;
38                 let peer_node_id_not_used = PublicKey::from_slice(&pk).unwrap();
39                 onion_messenger.handle_onion_message(&peer_node_id_not_used, &msg);
40         }
41 }
42
43 /// Method that needs to be added manually, {name}_test
44 pub fn onion_message_test<Out: test_logger::Output>(data: &[u8], out: Out) {
45         let logger = test_logger::TestLogger::new("".to_owned(), out);
46         do_test(data, &logger);
47 }
48
49 /// Method that needs to be added manually, {name}_run
50 #[no_mangle]
51 pub extern "C" fn onion_message_run(data: *const u8, datalen: usize) {
52         let logger = test_logger::TestLogger::new("".to_owned(), test_logger::DevNull {});
53         do_test(unsafe { std::slice::from_raw_parts(data, datalen) }, &logger);
54 }
55
56 struct TestOffersMessageHandler {}
57
58 impl OffersMessageHandler for TestOffersMessageHandler {
59         fn handle_message(&self, _message: OffersMessage) {}
60 }
61
62 struct TestCustomMessage {}
63
64 const CUSTOM_MESSAGE_TYPE: u64 = 4242;
65 const CUSTOM_MESSAGE_CONTENTS: [u8; 32] = [42; 32];
66
67 impl CustomOnionMessageContents for TestCustomMessage {
68         fn tlv_type(&self) -> u64 {
69                 CUSTOM_MESSAGE_TYPE
70         }
71 }
72
73 impl Writeable for TestCustomMessage {
74         fn write<W: Writer>(&self, w: &mut W) -> Result<(), io::Error> {
75                 Ok(CUSTOM_MESSAGE_CONTENTS.write(w)?)
76         }
77 }
78
79 struct TestCustomMessageHandler {}
80
81 impl CustomOnionMessageHandler for TestCustomMessageHandler {
82         type CustomMessage = TestCustomMessage;
83         fn handle_custom_message(&self, _msg: Self::CustomMessage) {}
84         fn read_custom_message<R: io::Read>(&self, _message_type: u64, buffer: &mut R) -> Result<Option<Self::CustomMessage>, msgs::DecodeError> {
85                 let mut buf = Vec::new();
86                 buffer.read_to_end(&mut buf)?;
87                 return Ok(Some(TestCustomMessage {}))
88         }
89 }
90
91 pub struct VecWriter(pub Vec<u8>);
92 impl Writer for VecWriter {
93         fn write_all(&mut self, buf: &[u8]) -> Result<(), ::std::io::Error> {
94                 self.0.extend_from_slice(buf);
95                 Ok(())
96         }
97 }
98 struct KeyProvider {
99         node_secret: SecretKey,
100         counter: AtomicU64,
101 }
102
103 impl EntropySource for KeyProvider {
104         fn get_secure_random_bytes(&self) -> [u8; 32] {
105                 let ctr = self.counter.fetch_add(1, Ordering::Relaxed);
106                 [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
107                         (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]
108         }
109 }
110
111 impl NodeSigner for KeyProvider {
112         fn get_node_id(&self, recipient: Recipient) -> Result<PublicKey, ()> {
113                 let node_secret = match recipient {
114                         Recipient::Node => Ok(&self.node_secret),
115                         Recipient::PhantomNode => Err(())
116                 }?;
117                 Ok(PublicKey::from_secret_key(&Secp256k1::signing_only(), node_secret))
118         }
119
120         fn ecdh(&self, recipient: Recipient, other_key: &PublicKey, tweak: Option<&Scalar>) -> Result<SharedSecret, ()> {
121                 let mut node_secret = match recipient {
122                         Recipient::Node => Ok(self.node_secret.clone()),
123                         Recipient::PhantomNode => Err(())
124                 }?;
125                 if let Some(tweak) = tweak {
126                         node_secret = node_secret.mul_tweak(tweak).map_err(|_| ())?;
127                 }
128                 Ok(SharedSecret::new(other_key, &node_secret))
129         }
130
131         fn get_inbound_payment_key_material(&self) -> KeyMaterial { unreachable!() }
132
133         fn sign_invoice(&self, _hrp_bytes: &[u8], _invoice_data: &[u5], _recipient: Recipient) -> Result<RecoverableSignature, ()> {
134                 unreachable!()
135         }
136
137         fn sign_gossip_message(&self, _msg: lightning::ln::msgs::UnsignedGossipMessage) -> Result<bitcoin::secp256k1::ecdsa::Signature, ()> {
138                 unreachable!()
139         }
140 }
141
142 impl SignerProvider for KeyProvider {
143         type Signer = EnforcingSigner;
144
145         fn generate_channel_keys_id(&self, _inbound: bool, _channel_value_satoshis: u64, _user_channel_id: u128) -> [u8; 32] { unreachable!() }
146
147         fn derive_channel_signer(&self, _channel_value_satoshis: u64, _channel_keys_id: [u8; 32]) -> Self::Signer {
148                 unreachable!()
149         }
150
151         fn read_chan_signer(&self, _data: &[u8]) -> Result<EnforcingSigner, DecodeError> { unreachable!() }
152
153         fn get_destination_script(&self) -> Result<Script, ()> { unreachable!() }
154
155         fn get_shutdown_scriptpubkey(&self) -> Result<ShutdownScript, ()> { unreachable!() }
156 }
157
158 #[cfg(test)]
159 mod tests {
160         use lightning::util::logger::{Logger, Record};
161         use std::collections::HashMap;
162         use std::sync::Mutex;
163
164         struct TrackingLogger {
165                 /// (module, message) -> count
166                 pub lines: Mutex<HashMap<(String, String), usize>>,
167         }
168         impl Logger for TrackingLogger {
169                 fn log(&self, record: &Record) {
170                         *self.lines.lock().unwrap().entry((record.module_path.to_string(), format!("{}", record.args))).or_insert(0) += 1;
171                         println!("{:<5} [{} : {}, {}] {}", record.level.to_string(), record.module_path, record.file, record.line, record.args);
172                 }
173         }
174
175         #[test]
176         fn test_no_onion_message_breakage() {
177                 let one_hop_om = "020000000000000000000000000000000000000000000000000000000000000e01055600020000000000000000000000000000000000000000000000000000000000000e0136041095000000000000000000000000000000fd1092202a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000e800000000000000000000000000000000000000000000000000000000000000";
178                 let logger = TrackingLogger { lines: Mutex::new(HashMap::new()) };
179                 super::do_test(&::hex::decode(one_hop_om).unwrap(), &logger);
180                 {
181                         let log_entries = logger.lines.lock().unwrap();
182                         assert_eq!(log_entries.get(&("lightning::onion_message::messenger".to_string(),
183                                                 "Received an onion message with path_id None and no reply_path".to_string())), Some(&1));
184                 }
185
186                 let two_unblinded_hops_om = "020000000000000000000000000000000000000000000000000000000000000e01055600020000000000000000000000000000000000000000000000000000000000000e0135043304210200000000000000000000000000000000000000000000000000000000000000029500000000000000000000000000000036000000000000000000000000000000000000000000000000000000000000003604104b000000000000000000000000000000fd1092202a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000b200000000000000000000000000000000000000000000000000000000000000";
187                 let logger = TrackingLogger { lines: Mutex::new(HashMap::new()) };
188                 super::do_test(&::hex::decode(two_unblinded_hops_om).unwrap(), &logger);
189                 {
190                         let log_entries = logger.lines.lock().unwrap();
191                         assert_eq!(log_entries.get(&("lightning::onion_message::messenger".to_string(), "Forwarding an onion message to peer 020000000000000000000000000000000000000000000000000000000000000002".to_string())), Some(&1));
192                 }
193
194                 let two_unblinded_two_blinded_om = "020000000000000000000000000000000000000000000000000000000000000e01055600020000000000000000000000000000000000000000000000000000000000000e01350433042102000000000000000000000000000000000000000000000000000000000000000295000000000000000000000000000000400000000000000000000000000000000000000000000000000000000000000058045604210200000000000000000000000000000000000000000000000000000000000000020821020000000000000000000000000000000000000000000000000000000000000e014b000000000000000000000000000000b20000000000000000000000000000000000000000000000000000000000000035043304210200000000000000000000000000000000000000000000000000000000000000029500000000000000000000000000000036000000000000000000000000000000000000000000000000000000000000003604104b000000000000000000000000000000fd1092202a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000b200000000000000000000000000000000000000000000000000000000000000";
195                 let logger = TrackingLogger { lines: Mutex::new(HashMap::new()) };
196                 super::do_test(&::hex::decode(two_unblinded_two_blinded_om).unwrap(), &logger);
197                 {
198                         let log_entries = logger.lines.lock().unwrap();
199                         assert_eq!(log_entries.get(&("lightning::onion_message::messenger".to_string(), "Forwarding an onion message to peer 020000000000000000000000000000000000000000000000000000000000000002".to_string())), Some(&1));
200                 }
201
202                 let three_blinded_om = "020000000000000000000000000000000000000000000000000000000000000e01055600020000000000000000000000000000000000000000000000000000000000000e013504330421020000000000000000000000000000000000000000000000000000000000000002950000000000000000000000000000006c0000000000000000000000000000000000000000000000000000000000000035043304210200000000000000000000000000000000000000000000000000000000000000024b000000000000000000000000000000ac00000000000000000000000000000000000000000000000000000000000000360410d1000000000000000000000000000000fd1092202a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000b200000000000000000000000000000000000000000000000000000000000000";
203                 let logger = TrackingLogger { lines: Mutex::new(HashMap::new()) };
204                 super::do_test(&::hex::decode(three_blinded_om).unwrap(), &logger);
205                 {
206                         let log_entries = logger.lines.lock().unwrap();
207                         assert_eq!(log_entries.get(&("lightning::onion_message::messenger".to_string(), "Forwarding an onion message to peer 020000000000000000000000000000000000000000000000000000000000000002".to_string())), Some(&1));
208                 }
209         }
210 }