|
| 1 | +// Imports that need to be added manually |
| 2 | +use bitcoin::bech32::u5; |
| 3 | +use bitcoin::blockdata::script::Script; |
| 4 | +use bitcoin::secp256k1::{PublicKey, SecretKey}; |
| 5 | +use bitcoin::secp256k1::ecdh::SharedSecret; |
| 6 | +use bitcoin::secp256k1::ecdsa::RecoverableSignature; |
| 7 | + |
| 8 | +use lightning::chain::keysinterface::{Recipient, KeyMaterial, KeysInterface}; |
| 9 | +use lightning::ln::msgs::{self, DecodeError}; |
| 10 | +use lightning::ln::script::ShutdownScript; |
| 11 | +use lightning::util::enforcing_trait_impls::EnforcingSigner; |
| 12 | +use lightning::util::ser::{Readable, Writeable, Writer}; |
| 13 | +use lightning::onion_message::OnionMessenger; |
| 14 | + |
| 15 | +use utils::test_logger; |
| 16 | + |
| 17 | +use std::io::Cursor; |
| 18 | +use std::sync::atomic::{AtomicU64, Ordering}; |
| 19 | + |
| 20 | +/// Actual fuzz test, method signature and name are fixed |
| 21 | +fn do_test<Out: test_logger::Output>(data: &[u8], out: Out) { |
| 22 | + if let Ok(msg) = <msgs::OnionMessage as Readable>::read(&mut Cursor::new(data)) { |
| 23 | + // Serialization checking adapted from `msg_targets::utils::test_msg_simple` |
| 24 | + let mut w = VecWriter(Vec::new()); |
| 25 | + msg.write(&mut w).unwrap(); |
| 26 | + assert_eq!(msg.serialized_length(), w.0.len()); |
| 27 | + |
| 28 | + let onion_message = <msgs::OnionMessage as Readable>::read(&mut Cursor::new(&w.0)).unwrap(); |
| 29 | + let mut w_two = VecWriter(Vec::new()); |
| 30 | + msg.write(&mut w_two).unwrap(); |
| 31 | + assert_eq!(&w.0[..], &w_two.0[..]); |
| 32 | + |
| 33 | + // Finally, make sure we can handle the onion message in OnionMessenger |
| 34 | + let secret = SecretKey::from_slice(&[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1]).unwrap(); |
| 35 | + let logger = test_logger::TestLogger::new("".to_owned(), out); |
| 36 | + let keys_manager = KeyProvider { |
| 37 | + node_secret: secret, |
| 38 | + counter: AtomicU64::new(0), |
| 39 | + }; |
| 40 | + let onion_messenger = OnionMessenger::new(&keys_manager, &logger); |
| 41 | + let peer_node_id_not_used = PublicKey::from_slice(&[0; 33]).unwrap(); |
| 42 | + onion_messenger.handle_onion_message(&peer_node_id_not_used, &onion_message); |
| 43 | + } |
| 44 | +} |
| 45 | + |
| 46 | +/// Method that needs to be added manually, {name}_test |
| 47 | +pub fn onion_message_test<Out: test_logger::Output>(data: &[u8], out: Out) { |
| 48 | + do_test(data, out); |
| 49 | +} |
| 50 | + |
| 51 | +/// Method that needs to be added manually, {name}_run |
| 52 | +#[no_mangle] |
| 53 | +pub extern "C" fn onion_message_run(data: *const u8, datalen: usize) { |
| 54 | + do_test(unsafe { std::slice::from_raw_parts(data, datalen) }, test_logger::DevNull {}); |
| 55 | +} |
| 56 | + |
| 57 | +pub struct VecWriter(pub Vec<u8>); |
| 58 | +impl Writer for VecWriter { |
| 59 | + fn write_all(&mut self, buf: &[u8]) -> Result<(), ::std::io::Error> { |
| 60 | + self.0.extend_from_slice(buf); |
| 61 | + Ok(()) |
| 62 | + } |
| 63 | +} |
| 64 | +struct KeyProvider { |
| 65 | + node_secret: SecretKey, |
| 66 | + counter: AtomicU64, |
| 67 | +} |
| 68 | +impl KeysInterface for KeyProvider { |
| 69 | + type Signer = EnforcingSigner; |
| 70 | + |
| 71 | + fn get_node_secret(&self, _recipient: Recipient) -> Result<SecretKey, ()> { |
| 72 | + Ok(self.node_secret.clone()) |
| 73 | + } |
| 74 | + |
| 75 | + fn ecdh(&self, recipient: Recipient, other_key: &PublicKey, tweak: Option<&[u8; 32]>) -> Result<SharedSecret, ()> { |
| 76 | + let mut node_secret = self.get_node_secret(recipient)?; |
| 77 | + if let Some(tweak) = tweak { |
| 78 | + node_secret.mul_assign(tweak).map_err(|_| ())?; |
| 79 | + } |
| 80 | + Ok(SharedSecret::new(other_key, &node_secret)) |
| 81 | + } |
| 82 | + |
| 83 | + fn get_inbound_payment_key_material(&self) -> KeyMaterial { unreachable!() } |
| 84 | + |
| 85 | + fn get_destination_script(&self) -> Script { unreachable!() } |
| 86 | + |
| 87 | + fn get_shutdown_scriptpubkey(&self) -> ShutdownScript { unreachable!() } |
| 88 | + |
| 89 | + fn get_channel_signer(&self, _inbound: bool, _channel_value_satoshis: u64) -> EnforcingSigner { |
| 90 | + unreachable!() |
| 91 | + } |
| 92 | + |
| 93 | + fn get_secure_random_bytes(&self) -> [u8; 32] { |
| 94 | + let ctr = self.counter.fetch_add(1, Ordering::Relaxed); |
| 95 | + [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
| 96 | + (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] |
| 97 | + } |
| 98 | + |
| 99 | + fn read_chan_signer(&self, _data: &[u8]) -> Result<EnforcingSigner, DecodeError> { unreachable!() } |
| 100 | + |
| 101 | + fn sign_invoice(&self, _hrp_bytes: &[u8], _invoice_data: &[u5], _recipient: Recipient) -> Result<RecoverableSignature, ()> { |
| 102 | + unreachable!() |
| 103 | + } |
| 104 | +} |
0 commit comments