-
Notifications
You must be signed in to change notification settings - Fork 409
Authenticate use of offer blinded paths #3139
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
5278d31
0a5918e
d7aeaa0
219691f
e156415
c0cae08
c58a1bb
7904e3c
f546aad
1ff8c8d
a5145e4
6a54618
35b75fd
bf42847
9d46340
f537abd
c2a120e
559daeb
bdf3330
fd596c3
114954c
868fee7
14634c6
2c2f3fe
e6ee194
4ed37d8
df5d7ea
718bc47
825bda0
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -22,6 +22,7 @@ use crate::io; | |
use crate::io::Cursor; | ||
use crate::ln::channelmanager::PaymentId; | ||
use crate::ln::onion_utils; | ||
use crate::offers::nonce::Nonce; | ||
use crate::onion_message::packet::ControlTlvs; | ||
use crate::sign::{NodeSigner, Recipient}; | ||
use crate::crypto::streams::ChaChaPolyReadAdapter; | ||
|
@@ -85,37 +86,71 @@ impl Writeable for ReceiveTlvs { | |
} | ||
} | ||
|
||
/// Represents additional data included by the recipient in a [`BlindedPath`]. | ||
/// Additional data included by the recipient in a [`BlindedPath`]. | ||
/// | ||
/// This data is encrypted by the recipient and remains invisible to anyone else. | ||
/// It is included in the [`BlindedPath`], making it accessible again to the recipient | ||
/// whenever the [`BlindedPath`] is used. | ||
/// The recipient can authenticate the message and utilize it for further processing | ||
/// if needed. | ||
/// This data is encrypted by the recipient and will be given to the corresponding message handler | ||
/// when handling a message sent over the [`BlindedPath`]. The recipient can use this data to | ||
/// authenticate the message or for further processing if needed. | ||
#[derive(Clone, Debug)] | ||
pub enum MessageContext { | ||
/// Represents the data specific to [`OffersMessage`] | ||
/// Context specific to an [`OffersMessage`]. | ||
/// | ||
/// [`OffersMessage`]: crate::onion_message::offers::OffersMessage | ||
Offers(OffersContext), | ||
/// Represents custom data received in a Custom Onion Message. | ||
/// Context specific to a [`CustomOnionMessageHandler::CustomMessage`]. | ||
/// | ||
/// [`CustomOnionMessageHandler::CustomMessage`]: crate::onion_message::messenger::CustomOnionMessageHandler::CustomMessage | ||
Custom(Vec<u8>), | ||
} | ||
|
||
/// Contains the data specific to [`OffersMessage`] | ||
/// Contains data specific to an [`OffersMessage`]. | ||
/// | ||
/// [`OffersMessage`]: crate::onion_message::offers::OffersMessage | ||
#[derive(Clone, Debug)] | ||
#[derive(Clone, Debug, Eq, PartialEq)] | ||
pub enum OffersContext { | ||
/// Represents an unknown BOLT12 payment context. | ||
/// This variant is used when a message is sent without | ||
/// using a [`BlindedPath`] or over one created prior to | ||
/// LDK version 0.0.124. | ||
/// Represents an unknown BOLT12 message context. | ||
/// | ||
/// This variant is used when a message is sent without using a [`BlindedPath`] or over one | ||
/// created prior to LDK version 0.0.124. | ||
Unknown {}, | ||
/// Represents an outbound BOLT12 payment context. | ||
/// Context used by a [`BlindedPath`] within an [`Offer`]. | ||
/// | ||
/// This variant is intended to be received when handling an [`InvoiceRequest`]. | ||
/// | ||
/// [`Offer`]: crate::offers::offer::Offer | ||
/// [`InvoiceRequest`]: crate::offers::invoice_request::InvoiceRequest | ||
InvoiceRequest { | ||
/// A nonce used for authenticating that an [`InvoiceRequest`] is for a valid [`Offer`] and | ||
/// for deriving the offer's signing keys. | ||
/// | ||
/// [`InvoiceRequest`]: crate::offers::invoice_request::InvoiceRequest | ||
/// [`Offer`]: crate::offers::offer::Offer | ||
nonce: Nonce, | ||
}, | ||
/// Context used by a [`BlindedPath`] within a [`Refund`] or as a reply path for an | ||
/// [`InvoiceRequest`]. | ||
/// | ||
/// This variant is intended to be received when handling a [`Bolt12Invoice`] or an | ||
/// [`InvoiceError`]. | ||
/// | ||
/// [`Refund`]: crate::offers::refund::Refund | ||
/// [`InvoiceRequest`]: crate::offers::invoice_request::InvoiceRequest | ||
/// [`Bolt12Invoice`]: crate::offers::invoice::Bolt12Invoice | ||
/// [`InvoiceError`]: crate::offers::invoice_error::InvoiceError | ||
OutboundPayment { | ||
/// Payment ID of the outbound BOLT12 payment. | ||
payment_id: PaymentId | ||
/// Payment ID used when creating a [`Refund`] or [`InvoiceRequest`]. | ||
/// | ||
/// [`Refund`]: crate::offers::refund::Refund | ||
/// [`InvoiceRequest`]: crate::offers::invoice_request::InvoiceRequest | ||
payment_id: PaymentId, | ||
|
||
/// A nonce used for authenticating that a [`Bolt12Invoice`] is for a valid [`Refund`] or | ||
/// [`InvoiceRequest`] and for deriving their signing keys. | ||
/// | ||
/// [`Bolt12Invoice`]: crate::offers::invoice::Bolt12Invoice | ||
/// [`Refund`]: crate::offers::refund::Refund | ||
/// [`InvoiceRequest`]: crate::offers::invoice_request::InvoiceRequest | ||
nonce: Nonce, | ||
}, | ||
} | ||
|
||
|
@@ -126,8 +161,12 @@ impl_writeable_tlv_based_enum!(MessageContext, | |
|
||
impl_writeable_tlv_based_enum!(OffersContext, | ||
(0, Unknown) => {}, | ||
(1, OutboundPayment) => { | ||
(1, InvoiceRequest) => { | ||
(0, nonce, required), | ||
}, | ||
(2, OutboundPayment) => { | ||
(0, payment_id, required), | ||
(1, nonce, required), | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This should be There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Oh, you mean don't worry about using odd numbers until we release? Wasn't sure if we had a convention around that. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We don't, it doesn't matter much so we often just do odds. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Reverted |
||
}, | ||
); | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -64,6 +64,7 @@ use crate::ln::wire::Encode; | |
use crate::offers::invoice::{BlindedPayInfo, Bolt12Invoice, DEFAULT_RELATIVE_EXPIRY, DerivedSigningPubkey, ExplicitSigningPubkey, InvoiceBuilder, UnsignedBolt12Invoice}; | ||
use crate::offers::invoice_error::InvoiceError; | ||
use crate::offers::invoice_request::{DerivedPayerId, InvoiceRequestBuilder}; | ||
use crate::offers::nonce::Nonce; | ||
use crate::offers::offer::{Offer, OfferBuilder}; | ||
use crate::offers::parse::Bolt12SemanticError; | ||
use crate::offers::refund::{Refund, RefundBuilder}; | ||
|
@@ -2254,7 +2255,10 @@ where | |
event_persist_notifier: Notifier, | ||
needs_persist_flag: AtomicBool, | ||
|
||
#[cfg(not(any(test, feature = "_test_utils")))] | ||
pending_offers_messages: Mutex<Vec<PendingOnionMessage<OffersMessage>>>, | ||
#[cfg(any(test, feature = "_test_utils"))] | ||
pub(crate) pending_offers_messages: Mutex<Vec<PendingOnionMessage<OffersMessage>>>, | ||
|
||
/// Tracks the message events that are to be broadcasted when we are connected to some peer. | ||
pending_broadcast_messages: Mutex<Vec<MessageSendEvent>>, | ||
|
@@ -4199,15 +4203,35 @@ where | |
/// whether or not the payment was successful. | ||
/// | ||
/// [timer tick]: Self::timer_tick_occurred | ||
pub fn send_payment_for_bolt12_invoice(&self, invoice: &Bolt12Invoice) -> Result<(), Bolt12PaymentError> { | ||
let secp_ctx = &self.secp_ctx; | ||
let expanded_key = &self.inbound_payment_key; | ||
match invoice.verify(expanded_key, secp_ctx) { | ||
pub fn send_payment_for_bolt12_invoice( | ||
&self, invoice: &Bolt12Invoice, context: &OffersContext, | ||
) -> Result<(), Bolt12PaymentError> { | ||
match self.verify_bolt12_invoice(invoice, context) { | ||
Ok(payment_id) => self.send_payment_for_verified_bolt12_invoice(invoice, payment_id), | ||
Err(()) => Err(Bolt12PaymentError::UnexpectedInvoice), | ||
} | ||
} | ||
|
||
fn verify_bolt12_invoice( | ||
&self, invoice: &Bolt12Invoice, context: &OffersContext, | ||
) -> Result<PaymentId, ()> { | ||
let secp_ctx = &self.secp_ctx; | ||
let expanded_key = &self.inbound_payment_key; | ||
|
||
match context { | ||
OffersContext::Unknown {} if invoice.is_for_refund_without_paths() => { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Are we planning to also stop using There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Yeah, looks like I need to update that to use
This code here will stay the same since the context is from the refund's blinded path -- or There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ah okay. To me it reads like using an There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yeah, I think we wanted to avoid having an There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. But if we're avoiding an There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Right, I'm in agreement that once we remove the last use of There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ah okay, thanks for that clarification. |
||
invoice.verify_using_metadata(expanded_key, secp_ctx) | ||
}, | ||
OffersContext::OutboundPayment { payment_id, nonce } => { | ||
invoice | ||
.verify_using_payer_data(*payment_id, *nonce, expanded_key, secp_ctx) | ||
.then(|| *payment_id) | ||
.ok_or(()) | ||
}, | ||
_ => Err(()), | ||
} | ||
} | ||
|
||
fn send_payment_for_verified_bolt12_invoice(&self, invoice: &Bolt12Invoice, payment_id: PaymentId) -> Result<(), Bolt12PaymentError> { | ||
let best_block_height = self.best_block.read().unwrap().height; | ||
let _persistence_guard = PersistenceNotifierGuard::notify_on_drop(self); | ||
|
@@ -8784,13 +8808,12 @@ macro_rules! create_offer_builder { ($self: ident, $builder: ty) => { | |
let entropy = &*$self.entropy_source; | ||
let secp_ctx = &$self.secp_ctx; | ||
|
||
let path = $self.create_blinded_paths_using_absolute_expiry(OffersContext::Unknown {}, absolute_expiry) | ||
let nonce = Nonce::from_entropy_source(entropy); | ||
let context = OffersContext::InvoiceRequest { nonce }; | ||
let path = $self.create_blinded_paths_using_absolute_expiry(context, absolute_expiry) | ||
.and_then(|paths| paths.into_iter().next().ok_or(())) | ||
.map_err(|_| Bolt12SemanticError::MissingPaths)?; | ||
|
||
let builder = OfferBuilder::deriving_signing_pubkey( | ||
node_id, expanded_key, entropy, secp_ctx | ||
) | ||
let builder = OfferBuilder::deriving_signing_pubkey(node_id, expanded_key, nonce, secp_ctx) | ||
.chain_hash($self.chain_hash) | ||
.path(path); | ||
|
||
|
@@ -8858,13 +8881,14 @@ macro_rules! create_refund_builder { ($self: ident, $builder: ty) => { | |
let entropy = &*$self.entropy_source; | ||
let secp_ctx = &$self.secp_ctx; | ||
|
||
let context = OffersContext::OutboundPayment { payment_id }; | ||
let nonce = Nonce::from_entropy_source(entropy); | ||
let context = OffersContext::OutboundPayment { payment_id, nonce }; | ||
let path = $self.create_blinded_paths_using_absolute_expiry(context, Some(absolute_expiry)) | ||
.and_then(|paths| paths.into_iter().next().ok_or(())) | ||
.map_err(|_| Bolt12SemanticError::MissingPaths)?; | ||
|
||
let builder = RefundBuilder::deriving_payer_id( | ||
node_id, expanded_key, entropy, secp_ctx, amount_msats, payment_id | ||
node_id, expanded_key, nonce, secp_ctx, amount_msats, payment_id | ||
)? | ||
.chain_hash($self.chain_hash) | ||
.absolute_expiry(absolute_expiry) | ||
|
@@ -8973,8 +8997,9 @@ where | |
let entropy = &*self.entropy_source; | ||
let secp_ctx = &self.secp_ctx; | ||
|
||
let nonce = Nonce::from_entropy_source(entropy); | ||
let builder: InvoiceRequestBuilder<DerivedPayerId, secp256k1::All> = offer | ||
.request_invoice_deriving_payer_id(expanded_key, entropy, secp_ctx, payment_id)? | ||
.request_invoice_deriving_payer_id(expanded_key, nonce, secp_ctx, payment_id)? | ||
.into(); | ||
let builder = builder.chain_hash(self.chain_hash)?; | ||
|
||
|
@@ -8992,8 +9017,9 @@ where | |
}; | ||
let invoice_request = builder.build_and_sign()?; | ||
|
||
let context = OffersContext::OutboundPayment { payment_id }; | ||
let reply_paths = self.create_blinded_paths(context).map_err(|_| Bolt12SemanticError::MissingPaths)?; | ||
let context = OffersContext::OutboundPayment { payment_id, nonce }; | ||
let reply_paths = self.create_blinded_paths(context) | ||
.map_err(|_| Bolt12SemanticError::MissingPaths)?; | ||
|
||
let _persistence_guard = PersistenceNotifierGuard::notify_on_drop(self); | ||
|
||
|
@@ -10692,7 +10718,7 @@ where | |
|
||
let abandon_if_payment = |context| { | ||
match context { | ||
OffersContext::OutboundPayment { payment_id } => self.abandon_payment(payment_id), | ||
OffersContext::OutboundPayment { payment_id, .. } => self.abandon_payment(payment_id), | ||
_ => {}, | ||
} | ||
}; | ||
|
@@ -10703,19 +10729,32 @@ where | |
Some(responder) => responder, | ||
None => return ResponseInstruction::NoResponse, | ||
}; | ||
|
||
let nonce = match context { | ||
OffersContext::Unknown {} if invoice_request.metadata().is_some() => None, | ||
OffersContext::InvoiceRequest { nonce } => Some(nonce), | ||
_ => return ResponseInstruction::NoResponse, | ||
}; | ||
|
||
let invoice_request = match nonce { | ||
valentinewallace marked this conversation as resolved.
Show resolved
Hide resolved
|
||
Some(nonce) => match invoice_request.verify_using_recipient_data( | ||
nonce, expanded_key, secp_ctx, | ||
) { | ||
Ok(invoice_request) => invoice_request, | ||
Err(()) => return ResponseInstruction::NoResponse, | ||
}, | ||
None => match invoice_request.verify_using_metadata(expanded_key, secp_ctx) { | ||
Ok(invoice_request) => invoice_request, | ||
Err(()) => return ResponseInstruction::NoResponse, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. FWIW I don't think there's anything wrong with ignoring the BOLTs here - as long as we handle "unsolicited invoice_request that is for an offer we didnt built" and "invoice_request that we received over the wrong path" the same we're fine. Not sure it matters much if we think this case isn't going to be a common one that we need to worry about making sure we get good errors back, but if we think it'd be helpful we should just do it. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Currently, this will only be triggered for older offers. When we add support for producing offers without blinded paths (i.e. with a public node id), it will also be hit. So at that point someone could de-anonymize the older offer by sending an invoice request for it to the node id given in the newer offer. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Hmm? Not sure I understand the scenario you're describing - are you saying if we issue two offers, one with a blinded path and one that publishes our node, someone can use the one with the blinded path and the one that publishes our node somehow to get different behavior? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ah, no, I was saying they could determine that they came from the same issuer. Though, that is actually a different problem than what you are asking about and one we probably can't solve. Sorry, think I was confused about your comment. Were you saying we should continue to send an There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think so? Like, if we think it doesn't matter if we pass back an error then no need, but if we think we may hit those cases and we should pass back an error then we should ignore the BOLTs here. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yeah, probably not useful to send an |
||
}, | ||
}; | ||
|
||
let amount_msats = match InvoiceBuilder::<DerivedSigningPubkey>::amount_msats( | ||
&invoice_request | ||
&invoice_request.inner | ||
) { | ||
Ok(amount_msats) => amount_msats, | ||
Err(error) => return responder.respond(OffersMessage::InvoiceError(error.into())), | ||
}; | ||
let invoice_request = match invoice_request.verify(expanded_key, secp_ctx) { | ||
Ok(invoice_request) => invoice_request, | ||
Err(()) => { | ||
let error = Bolt12SemanticError::InvalidMetadata; | ||
return responder.respond(OffersMessage::InvoiceError(error.into())); | ||
}, | ||
}; | ||
|
||
let relative_expiry = DEFAULT_RELATIVE_EXPIRY.as_secs() as u32; | ||
let (payment_hash, payment_secret) = match self.create_inbound_payment( | ||
|
@@ -10788,24 +10827,28 @@ where | |
} | ||
}, | ||
OffersMessage::Invoice(invoice) => { | ||
let result = match invoice.verify(expanded_key, secp_ctx) { | ||
Ok(payment_id) => { | ||
let features = self.bolt12_invoice_features(); | ||
if invoice.invoice_features().requires_unknown_bits_from(&features) { | ||
Err(InvoiceError::from(Bolt12SemanticError::UnknownRequiredFeatures)) | ||
} else if self.default_configuration.manually_handle_bolt12_invoices { | ||
let event = Event::InvoiceReceived { payment_id, invoice, responder }; | ||
self.pending_events.lock().unwrap().push_back((event, None)); | ||
return ResponseInstruction::NoResponse; | ||
} else { | ||
self.send_payment_for_verified_bolt12_invoice(&invoice, payment_id) | ||
.map_err(|e| { | ||
log_trace!(self.logger, "Failed paying invoice: {:?}", e); | ||
InvoiceError::from_string(format!("{:?}", e)) | ||
}) | ||
} | ||
}, | ||
Err(()) => Err(InvoiceError::from_string("Unrecognized invoice".to_owned())), | ||
let payment_id = match self.verify_bolt12_invoice(&invoice, &context) { | ||
Ok(payment_id) => payment_id, | ||
Err(()) => return ResponseInstruction::NoResponse, | ||
}; | ||
|
||
let result = { | ||
let features = self.bolt12_invoice_features(); | ||
if invoice.invoice_features().requires_unknown_bits_from(&features) { | ||
Err(InvoiceError::from(Bolt12SemanticError::UnknownRequiredFeatures)) | ||
} else if self.default_configuration.manually_handle_bolt12_invoices { | ||
let event = Event::InvoiceReceived { | ||
payment_id, invoice, context, responder, | ||
}; | ||
self.pending_events.lock().unwrap().push_back((event, None)); | ||
return ResponseInstruction::NoResponse; | ||
} else { | ||
self.send_payment_for_verified_bolt12_invoice(&invoice, payment_id) | ||
.map_err(|e| { | ||
log_trace!(self.logger, "Failed paying invoice: {:?}", e); | ||
InvoiceError::from_string(format!("{:?}", e)) | ||
}) | ||
} | ||
}; | ||
|
||
match result { | ||
|
Uh oh!
There was an error while loading. Please reload this page.