From 8b479ac5873b1d2e11514ac7d7141892d6099946 Mon Sep 17 00:00:00 2001 From: shaavan Date: Fri, 6 Sep 2024 17:31:13 +0530 Subject: [PATCH] Add HMAC, and nonce to OffersContext::InboundPayment Introduce HMAC and nonce calculation when sending Invoice with reply path, so that if we receive InvoiceError back for the corresponding Invoice we can verify the payment hash before logging it. --- lightning/src/blinded_path/message.rs | 15 +++++++++++++++ lightning/src/ln/channelmanager.rs | 11 +++++++++-- 2 files changed, 24 insertions(+), 2 deletions(-) diff --git a/lightning/src/blinded_path/message.rs b/lightning/src/blinded_path/message.rs index e3899b50e..256483fec 100644 --- a/lightning/src/blinded_path/message.rs +++ b/lightning/src/blinded_path/message.rs @@ -347,6 +347,19 @@ pub enum OffersContext { /// /// [`Bolt12Invoice::payment_hash`]: crate::offers::invoice::Bolt12Invoice::payment_hash payment_hash: PaymentHash, + + /// A nonce used for authenticating that a received [`InvoiceError`] is for a valid + /// sent [`Bolt12Invoice`]. + /// + /// [`InvoiceError`]: crate::offers::invoice_error::InvoiceError + /// [`Bolt12Invoice`]: crate::offers::invoice::Bolt12Invoice + nonce: Nonce, + + /// Authentication code for the [`PaymentHash`], which should be checked when the context is + /// used to log the received [`InvoiceError`]. + /// + /// [`InvoiceError`]: crate::offers::invoice_error::InvoiceError + hmac: Hmac, }, } @@ -366,6 +379,8 @@ impl_writeable_tlv_based_enum!(OffersContext, }, (2, InboundPayment) => { (0, payment_hash, required), + (1, nonce, required), + (2, hmac, required) }, ); diff --git a/lightning/src/ln/channelmanager.rs b/lightning/src/ln/channelmanager.rs index 56457e79c..c8597f8f0 100644 --- a/lightning/src/ln/channelmanager.rs +++ b/lightning/src/ln/channelmanager.rs @@ -9226,8 +9226,10 @@ where let builder: InvoiceBuilder = builder.into(); let invoice = builder.allow_mpp().build_and_sign(secp_ctx)?; + let nonce = Nonce::from_entropy_source(entropy); + let hmac = payment_hash.hmac_for_offer_payment(nonce, expanded_key); let context = OffersContext::InboundPayment { - payment_hash: invoice.payment_hash(), + payment_hash: invoice.payment_hash(), nonce, hmac }; let reply_paths = self.create_blinded_paths(context) .map_err(|_| Bolt12SemanticError::MissingPaths)?; @@ -10987,7 +10989,12 @@ where }, OffersMessage::InvoiceError(invoice_error) => { let payment_hash = match context { - Some(OffersContext::InboundPayment { payment_hash }) => Some(payment_hash), + Some(OffersContext::InboundPayment { payment_hash, nonce, hmac }) => { + match payment_hash.verify(hmac, nonce, expanded_key) { + Ok(_) => Some(payment_hash), + Err(_) => None, + } + }, _ => None, }; -- 2.39.5