Skip to content

Commit bf7e5ac

Browse files
committed
Add an HMAC to OffersContext::OutboundPayment
When receiving an InvoiceError in response to an InvoiceRequest, the corresponding payment should be abandoned. Add an HMAC to OffersContext::OutboundPayment such that the payment ID can be authenticated prior to abandoning the payment.
1 parent 4364faa commit bf7e5ac

File tree

2 files changed

+15
-3
lines changed

2 files changed

+15
-3
lines changed

lightning/src/blinded_path/message.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@ use bitcoin::secp256k1::{self, PublicKey, Secp256k1, SecretKey};
1616
#[allow(unused_imports)]
1717
use crate::prelude::*;
1818

19+
use bitcoin::hashes::hmac::Hmac;
20+
use bitcoin::hashes::sha256::Hash as Sha256;
1921
use crate::blinded_path::{BlindedHop, BlindedPath, IntroductionNode, NextMessageHop, NodeIdLookUp};
2022
use crate::blinded_path::utils;
2123
use crate::io;
@@ -146,6 +148,12 @@ pub enum OffersContext {
146148
/// [`Refund`]: crate::offers::refund::Refund
147149
/// [`InvoiceRequest`]: crate::offers::invoice_request::InvoiceRequest
148150
nonce: Nonce,
151+
152+
/// Authentication code for the [`PaymentId`], which should be checked when the context is
153+
/// used with an [`InvoiceError`].
154+
///
155+
/// [`InvoiceError`]: crate::offers::invoice_error::InvoiceError
156+
hmac: Hmac<Sha256>,
149157
},
150158
/// Context used by a [`BlindedPath`] as a reply path for a [`Bolt12Invoice`].
151159
///
@@ -173,6 +181,7 @@ impl_writeable_tlv_based_enum!(OffersContext,
173181
(1, OutboundPayment) => {
174182
(0, payment_id, required),
175183
(1, nonce, required),
184+
(2, hmac, required),
176185
},
177186
(2, InboundPayment) => {
178187
(0, payment_hash, required),

lightning/src/ln/channelmanager.rs

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,7 @@ use crate::offers::nonce::Nonce;
6868
use crate::offers::offer::{Offer, OfferBuilder};
6969
use crate::offers::parse::Bolt12SemanticError;
7070
use crate::offers::refund::{Refund, RefundBuilder};
71+
use crate::offers::signer;
7172
use crate::onion_message::async_payments::{AsyncPaymentsMessage, HeldHtlcAvailable, ReleaseHeldHtlc, AsyncPaymentsMessageHandler};
7273
use crate::onion_message::messenger::{new_pending_onion_message, Destination, MessageRouter, PendingOnionMessage, Responder, ResponseInstruction};
7374
use crate::onion_message::offers::{OffersMessage, OffersMessageHandler};
@@ -4222,7 +4223,7 @@ where
42224223
None if invoice.is_for_refund_without_paths() => {
42234224
invoice.verify_using_metadata(expanded_key, secp_ctx)
42244225
},
4225-
Some(&OffersContext::OutboundPayment { payment_id, nonce }) => {
4226+
Some(&OffersContext::OutboundPayment { payment_id, nonce, .. }) => {
42264227
invoice.verify_using_payer_data(payment_id, nonce, expanded_key, secp_ctx)
42274228
},
42284229
_ => Err(()),
@@ -8878,7 +8879,8 @@ macro_rules! create_refund_builder { ($self: ident, $builder: ty) => {
88788879
let secp_ctx = &$self.secp_ctx;
88798880

88808881
let nonce = Nonce::from_entropy_source(entropy);
8881-
let context = OffersContext::OutboundPayment { payment_id, nonce };
8882+
let hmac = signer::hmac_for_payment_id(payment_id, nonce, expanded_key);
8883+
let context = OffersContext::OutboundPayment { payment_id, nonce, hmac };
88828884
let path = $self.create_blinded_paths_using_absolute_expiry(context, Some(absolute_expiry))
88838885
.and_then(|paths| paths.into_iter().next().ok_or(()))
88848886
.map_err(|_| Bolt12SemanticError::MissingPaths)?;
@@ -9013,7 +9015,8 @@ where
90139015
};
90149016
let invoice_request = builder.build_and_sign()?;
90159017

9016-
let context = OffersContext::OutboundPayment { payment_id, nonce };
9018+
let hmac = signer::hmac_for_payment_id(payment_id, nonce, expanded_key);
9019+
let context = OffersContext::OutboundPayment { payment_id, nonce, hmac };
90179020
let reply_paths = self.create_blinded_paths(context)
90189021
.map_err(|_| Bolt12SemanticError::MissingPaths)?;
90199022

0 commit comments

Comments
 (0)