@@ -52,6 +52,7 @@ use crate::ln::types::ChannelId;
5252use crate::types::payment::{PaymentHash, PaymentPreimage, PaymentSecret};
5353use crate::ln::channel::{self, Channel, ChannelError, ChannelUpdateStatus, FundedChannel, ShutdownResult, UpdateFulfillCommitFetch, OutboundV1Channel, ReconnectionMsg, InboundV1Channel, WithChannelContext};
5454use crate::ln::channel::PendingV2Channel;
55+ use crate::ln::our_peer_storage::OurPeerStorage;
5556use crate::ln::channel_state::ChannelDetails;
5657use crate::types::features::{Bolt12InvoiceFeatures, ChannelFeatures, ChannelTypeFeatures, InitFeatures, NodeFeatures};
5758#[cfg(any(feature = "_test_utils", test))]
@@ -8322,15 +8323,40 @@ This indicates a bug inside LDK. Please report this error at https://github.com/
83228323 }
83238324 }
83248325
8325- fn internal_peer_storage_retrieval(&self, counterparty_node_id: PublicKey, _msg : msgs::PeerStorageRetrieval) -> Result<(), MsgHandleErrInternal> {
8326- // TODO: Decrypt and check if have any stale or missing ChannelMonitor.
8326+ fn internal_peer_storage_retrieval(&self, counterparty_node_id: PublicKey, msg : msgs::PeerStorageRetrieval) -> Result<(), MsgHandleErrInternal> {
8327+ // TODO: Check if have any stale or missing ChannelMonitor.
83278328 let logger = WithContext::from(&self.logger, Some(counterparty_node_id), None, None);
83288329
8329- log_debug!(logger, "Received unexpected peer_storage_retrieval from {}. This is unusual since we do not yet distribute peer storage. Sending a warning.", log_pubkey!(counterparty_node_id));
8330+ // `MIN_CYPHERTEXT_LEN` is 16 bytes because the mandatory authentication tag length is 16 bytes.
8331+ const MIN_CYPHERTEXT_LEN: usize = 16;
83308332
8331- Err(MsgHandleErrInternal::from_chan_no_close(ChannelError::Warn(
8332- "Invalid peer_storage_retrieval message received.".into(),
8333- ), ChannelId([0; 32])))
8333+ if msg.data.len() < MIN_CYPHERTEXT_LEN {
8334+ log_debug!(logger, "Invalid YourPeerStorage received from {}", log_pubkey!(counterparty_node_id));
8335+ return Err(MsgHandleErrInternal::from_chan_no_close(ChannelError::Warn(
8336+ "Invalid peer_storage_retrieval message received.".into(),
8337+ ), ChannelId([0; 32])));
8338+ }
8339+
8340+ let our_peerstorage_encryption_key = self.node_signer.get_peer_storage_key();
8341+ let our_peer_storage = OurPeerStorage::new(msg.data);
8342+
8343+ match our_peer_storage.decrypt_our_peer_storage(our_peerstorage_encryption_key) {
8344+ Ok(decrypted_data) => {
8345+ // Decryption successful.
8346+ if decrypted_data.len() == 0 {
8347+ log_trace!(logger, "Received a peer storage from peer {} with 0 channels.", log_pubkey!(counterparty_node_id));
8348+ }
8349+ }
8350+ Err(_) => {
8351+ log_debug!(logger, "Invalid YourPeerStorage received from {}", log_pubkey!(counterparty_node_id));
8352+
8353+ return Err(MsgHandleErrInternal::from_chan_no_close(ChannelError::Ignore(
8354+ "Invalid peer_storage_retrieval message received.".into(),
8355+ ), ChannelId([0; 32])));
8356+ }
8357+ }
8358+
8359+ Ok(())
83348360 }
83358361
83368362 fn internal_peer_storage(&self, counterparty_node_id: PublicKey, msg: msgs::PeerStorage) -> Result<(), MsgHandleErrInternal> {
@@ -16299,7 +16325,7 @@ mod tests {
1629916325pub mod bench {
1630016326 use crate::chain::Listen;
1630116327 use crate::chain::chainmonitor::{ChainMonitor, Persist};
16302- use crate::sign::{KeysManager, InMemorySigner};
16328+ use crate::sign::{KeysManager, InMemorySigner, NodeSigner };
1630316329 use crate::events::Event;
1630416330 use crate::ln::channelmanager::{BestBlock, ChainParameters, ChannelManager, PaymentHash, PaymentPreimage, PaymentId, RecipientOnionFields, Retry};
1630516331 use crate::ln::functional_test_utils::*;
0 commit comments