Skip to content

Commit 9cc6969

Browse files
Verify blinded keysend payment secrets
If we're receiving a keysend to a blinded path, then we created the payment secret within. Using our inbound_payment_key, we can decrypt the payment secret bytes to get the payment's min_cltv_expiry_delta and min amount, to verify the payment is valid. However, if we're receiving an MPP keysend *not* to a blinded path, then we did not create the payment secret and shouldn't verify it since it's only used to correlate MPP parts. Therefore, store whether the payment secret is recipient-generated in our pending inbound payment data so we know whether to verify it or not.
1 parent 34c2f25 commit 9cc6969

File tree

4 files changed

+114
-13
lines changed

4 files changed

+114
-13
lines changed

lightning/src/ln/blinded_payment_tests.rs

+75-2
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,10 @@ use crate::util::config::UserConfig;
3535
use crate::util::ser::WithoutLength;
3636
use crate::util::test_utils;
3737
use lightning_invoice::RawBolt11Invoice;
38+
#[cfg(async_payments)] use {
39+
crate::ln::inbound_payment,
40+
crate::types::payment::PaymentPreimage,
41+
};
3842

3943
fn blinded_payment_path(
4044
payment_secret: PaymentSecret, intro_node_min_htlc: u64, intro_node_max_htlc: u64,
@@ -1209,6 +1213,7 @@ fn conditionally_round_fwd_amt() {
12091213
}
12101214

12111215
#[test]
1216+
#[cfg(async_payments)]
12121217
fn blinded_keysend() {
12131218
let mut mpp_keysend_config = test_default_channel_config();
12141219
mpp_keysend_config.accept_mpp_keysend = true;
@@ -1219,8 +1224,15 @@ fn blinded_keysend() {
12191224
create_announced_chan_between_nodes_with_value(&nodes, 0, 1, 1_000_000, 0);
12201225
let chan_upd_1_2 = create_announced_chan_between_nodes_with_value(&nodes, 1, 2, 1_000_000, 0).0.contents;
12211226

1227+
let inbound_payment_key = inbound_payment::ExpandedKey::new(
1228+
&nodes[2].keys_manager.get_inbound_payment_key_material()
1229+
);
1230+
let payment_secret = inbound_payment::create_for_spontaneous_payment(
1231+
&inbound_payment_key, None, u32::MAX, nodes[2].node.duration_since_epoch().as_secs(), None
1232+
).unwrap();
1233+
12221234
let amt_msat = 5000;
1223-
let (keysend_preimage, _, payment_secret) = get_payment_preimage_hash(&nodes[2], None, None);
1235+
let keysend_preimage = PaymentPreimage([42; 32]);
12241236
let route_params = get_blinded_route_parameters(amt_msat, payment_secret, 1,
12251237
1_0000_0000,
12261238
nodes.iter().skip(1).map(|n| n.node.get_our_node_id()).collect(),
@@ -1241,6 +1253,7 @@ fn blinded_keysend() {
12411253
}
12421254

12431255
#[test]
1256+
#[cfg(async_payments)]
12441257
fn blinded_mpp_keysend() {
12451258
let mut mpp_keysend_config = test_default_channel_config();
12461259
mpp_keysend_config.accept_mpp_keysend = true;
@@ -1254,8 +1267,15 @@ fn blinded_mpp_keysend() {
12541267
let chan_1_3 = create_announced_chan_between_nodes(&nodes, 1, 3);
12551268
let chan_2_3 = create_announced_chan_between_nodes(&nodes, 2, 3);
12561269

1270+
let inbound_payment_key = inbound_payment::ExpandedKey::new(
1271+
&nodes[3].keys_manager.get_inbound_payment_key_material()
1272+
);
1273+
let payment_secret = inbound_payment::create_for_spontaneous_payment(
1274+
&inbound_payment_key, None, u32::MAX, nodes[3].node.duration_since_epoch().as_secs(), None
1275+
).unwrap();
1276+
12571277
let amt_msat = 15_000_000;
1258-
let (keysend_preimage, _, payment_secret) = get_payment_preimage_hash(&nodes[3], None, None);
1278+
let keysend_preimage = PaymentPreimage([42; 32]);
12591279
let route_params = {
12601280
let pay_params = PaymentParameters::blinded(
12611281
vec![
@@ -1293,6 +1313,59 @@ fn blinded_mpp_keysend() {
12931313
);
12941314
}
12951315

1316+
#[test]
1317+
#[cfg(async_payments)]
1318+
fn invalid_keysend_payment_secret() {
1319+
let mut mpp_keysend_config = test_default_channel_config();
1320+
mpp_keysend_config.accept_mpp_keysend = true;
1321+
let chanmon_cfgs = create_chanmon_cfgs(3);
1322+
let node_cfgs = create_node_cfgs(3, &chanmon_cfgs);
1323+
let node_chanmgrs = create_node_chanmgrs(3, &node_cfgs, &[None, None, Some(mpp_keysend_config)]);
1324+
let mut nodes = create_network(3, &node_cfgs, &node_chanmgrs);
1325+
create_announced_chan_between_nodes_with_value(&nodes, 0, 1, 1_000_000, 0);
1326+
let chan_upd_1_2 = create_announced_chan_between_nodes_with_value(&nodes, 1, 2, 1_000_000, 0).0.contents;
1327+
1328+
let invalid_payment_secret = PaymentSecret([42; 32]);
1329+
let amt_msat = 5000;
1330+
let keysend_preimage = PaymentPreimage([42; 32]);
1331+
let route_params = get_blinded_route_parameters(
1332+
amt_msat, invalid_payment_secret, 1, 1_0000_0000,
1333+
nodes.iter().skip(1).map(|n| n.node.get_our_node_id()).collect(), &[&chan_upd_1_2],
1334+
&chanmon_cfgs[2].keys_manager
1335+
);
1336+
1337+
let payment_hash = nodes[0].node.send_spontaneous_payment_with_retry(Some(keysend_preimage), RecipientOnionFields::spontaneous_empty(), PaymentId(keysend_preimage.0), route_params, Retry::Attempts(0)).unwrap();
1338+
check_added_monitors(&nodes[0], 1);
1339+
1340+
let expected_route: &[&[&Node]] = &[&[&nodes[1], &nodes[2]]];
1341+
let mut events = nodes[0].node.get_and_clear_pending_msg_events();
1342+
assert_eq!(events.len(), 1);
1343+
1344+
let ev = remove_first_msg_event_to_node(&nodes[1].node.get_our_node_id(), &mut events);
1345+
let args = PassAlongPathArgs::new(
1346+
&nodes[0], &expected_route[0], amt_msat, payment_hash, ev.clone()
1347+
)
1348+
.with_payment_secret(invalid_payment_secret)
1349+
.with_payment_preimage(keysend_preimage)
1350+
.expect_failure(HTLCDestination::FailedPayment { payment_hash });
1351+
do_pass_along_path(args);
1352+
1353+
let updates_2_1 = get_htlc_update_msgs!(nodes[2], nodes[1].node.get_our_node_id());
1354+
assert_eq!(updates_2_1.update_fail_malformed_htlcs.len(), 1);
1355+
let update_malformed = &updates_2_1.update_fail_malformed_htlcs[0];
1356+
assert_eq!(update_malformed.sha256_of_onion, [0; 32]);
1357+
assert_eq!(update_malformed.failure_code, INVALID_ONION_BLINDING);
1358+
nodes[1].node.handle_update_fail_malformed_htlc(nodes[2].node.get_our_node_id(), update_malformed);
1359+
do_commitment_signed_dance(&nodes[1], &nodes[2], &updates_2_1.commitment_signed, true, false);
1360+
1361+
let updates_1_0 = get_htlc_update_msgs!(nodes[1], nodes[0].node.get_our_node_id());
1362+
assert_eq!(updates_1_0.update_fail_htlcs.len(), 1);
1363+
nodes[0].node.handle_update_fail_htlc(nodes[1].node.get_our_node_id(), &updates_1_0.update_fail_htlcs[0]);
1364+
do_commitment_signed_dance(&nodes[0], &nodes[1], &updates_1_0.commitment_signed, false, false);
1365+
expect_payment_failed_conditions(&nodes[0], payment_hash, false,
1366+
PaymentFailedConditions::new().expected_htlc_error_data(INVALID_ONION_BLINDING, &[0; 32]));
1367+
}
1368+
12961369
#[test]
12971370
fn custom_tlvs_to_blinded_path() {
12981371
let chanmon_cfgs = create_chanmon_cfgs(2);

lightning/src/ln/channelmanager.rs

+22-7
Original file line numberDiff line numberDiff line change
@@ -220,6 +220,10 @@ pub enum PendingHTLCRouting {
220220
custom_tlvs: Vec<(u64, Vec<u8>)>,
221221
/// Set if this HTLC is the final hop in a multi-hop blinded path.
222222
requires_blinded_error: bool,
223+
/// Set if we are receiving a keysend to a blinded path, meaning we created the
224+
/// [`PaymentSecret`] and should verify it using our
225+
/// [`NodeSigner::get_inbound_payment_key_material`].
226+
has_recipient_created_payment_secret: bool,
223227
},
224228
}
225229

@@ -5699,7 +5703,10 @@ where
56995703
}
57005704
}) => {
57015705
let blinded_failure = routing.blinded_failure();
5702-
let (cltv_expiry, onion_payload, payment_data, payment_context, phantom_shared_secret, mut onion_fields) = match routing {
5706+
let (
5707+
cltv_expiry, onion_payload, payment_data, payment_context, phantom_shared_secret,
5708+
mut onion_fields, has_recipient_created_payment_secret
5709+
) = match routing {
57035710
PendingHTLCRouting::Receive {
57045711
payment_data, payment_metadata, payment_context,
57055712
incoming_cltv_expiry, phantom_shared_secret, custom_tlvs,
@@ -5709,19 +5716,21 @@ where
57095716
let onion_fields = RecipientOnionFields { payment_secret: Some(payment_data.payment_secret),
57105717
payment_metadata, custom_tlvs };
57115718
(incoming_cltv_expiry, OnionPayload::Invoice { _legacy_hop_data },
5712-
Some(payment_data), payment_context, phantom_shared_secret, onion_fields)
5719+
Some(payment_data), payment_context, phantom_shared_secret, onion_fields,
5720+
true)
57135721
},
57145722
PendingHTLCRouting::ReceiveKeysend {
57155723
payment_data, payment_preimage, payment_metadata,
5716-
incoming_cltv_expiry, custom_tlvs, requires_blinded_error: _
5724+
incoming_cltv_expiry, custom_tlvs, requires_blinded_error: _,
5725+
has_recipient_created_payment_secret,
57175726
} => {
57185727
let onion_fields = RecipientOnionFields {
57195728
payment_secret: payment_data.as_ref().map(|data| data.payment_secret),
57205729
payment_metadata,
57215730
custom_tlvs,
57225731
};
57235732
(incoming_cltv_expiry, OnionPayload::Spontaneous(payment_preimage),
5724-
payment_data, None, None, onion_fields)
5733+
payment_data, None, None, onion_fields, has_recipient_created_payment_secret)
57255734
},
57265735
_ => {
57275736
panic!("short_channel_id == 0 should imply any pending_forward entries are of type Receive");
@@ -5886,9 +5895,8 @@ where
58865895
// that we are the ultimate recipient of the given payment hash.
58875896
// Further, we must not expose whether we have any other HTLCs
58885897
// associated with the same payment_hash pending or not.
5889-
match claimable_htlc.onion_payload {
5890-
OnionPayload::Invoice { .. } => {
5891-
let payment_data = payment_data.unwrap();
5898+
let payment_preimage = if has_recipient_created_payment_secret {
5899+
if let Some(ref payment_data) = payment_data {
58925900
let (payment_preimage, min_final_cltv_expiry_delta) = match inbound_payment::verify(payment_hash, &payment_data, self.highest_seen_timestamp.load(Ordering::Acquire) as u64, &self.inbound_payment_key, &self.logger) {
58935901
Ok(result) => result,
58945902
Err(()) => {
@@ -5904,6 +5912,12 @@ where
59045912
fail_htlc!(claimable_htlc, payment_hash);
59055913
}
59065914
}
5915+
payment_preimage
5916+
} else { fail_htlc!(claimable_htlc, payment_hash); }
5917+
} else { None };
5918+
match claimable_htlc.onion_payload {
5919+
OnionPayload::Invoice { .. } => {
5920+
let payment_data = payment_data.unwrap();
59075921
let purpose = events::PaymentPurpose::from_parts(
59085922
payment_preimage,
59095923
payment_data.payment_secret,
@@ -11422,6 +11436,7 @@ impl_writeable_tlv_based_enum!(PendingHTLCRouting,
1142211436
(3, payment_metadata, option),
1142311437
(4, payment_data, option), // Added in 0.0.116
1142411438
(5, custom_tlvs, optional_vec),
11439+
(7, has_recipient_created_payment_secret, (default_value, false)),
1142511440
},
1142611441
);
1142711442

lightning/src/ln/functional_test_utils.rs

+13-1
Original file line numberDiff line numberDiff line change
@@ -2587,6 +2587,7 @@ pub struct PassAlongPathArgs<'a, 'b, 'c, 'd> {
25872587
pub is_probe: bool,
25882588
pub custom_tlvs: Vec<(u64, Vec<u8>)>,
25892589
pub payment_metadata: Option<Vec<u8>>,
2590+
pub expected_failure: Option<HTLCDestination>,
25902591
}
25912592

25922593
impl<'a, 'b, 'c, 'd> PassAlongPathArgs<'a, 'b, 'c, 'd> {
@@ -2597,7 +2598,7 @@ impl<'a, 'b, 'c, 'd> PassAlongPathArgs<'a, 'b, 'c, 'd> {
25972598
Self {
25982599
origin_node, expected_path, recv_value, payment_hash, payment_secret: None, event,
25992600
payment_claimable_expected: true, clear_recipient_events: true, expected_preimage: None,
2600-
is_probe: false, custom_tlvs: Vec::new(), payment_metadata: None,
2601+
is_probe: false, custom_tlvs: Vec::new(), payment_metadata: None, expected_failure: None,
26012602
}
26022603
}
26032604
pub fn without_clearing_recipient_events(mut self) -> Self {
@@ -2629,13 +2630,19 @@ impl<'a, 'b, 'c, 'd> PassAlongPathArgs<'a, 'b, 'c, 'd> {
26292630
self.payment_metadata = Some(payment_metadata);
26302631
self
26312632
}
2633+
pub fn expect_failure(mut self, failure: HTLCDestination) -> Self {
2634+
self.payment_claimable_expected = false;
2635+
self.expected_failure = Some(failure);
2636+
self
2637+
}
26322638
}
26332639

26342640
pub fn do_pass_along_path<'a, 'b, 'c>(args: PassAlongPathArgs) -> Option<Event> {
26352641
let PassAlongPathArgs {
26362642
origin_node, expected_path, recv_value, payment_hash: our_payment_hash,
26372643
payment_secret: our_payment_secret, event: ev, payment_claimable_expected,
26382644
clear_recipient_events, expected_preimage, is_probe, custom_tlvs, payment_metadata,
2645+
expected_failure
26392646
} = args;
26402647

26412648
let mut payment_event = SendEvent::from_event(ev);
@@ -2699,6 +2706,11 @@ pub fn do_pass_along_path<'a, 'b, 'c>(args: PassAlongPathArgs) -> Option<Event>
26992706
_ => panic!("Unexpected event"),
27002707
}
27012708
event = Some(events_2[0].clone());
2709+
} else if let Some(ref failure) = expected_failure {
2710+
assert_eq!(events_2.len(), 2);
2711+
expect_htlc_handling_failed_destinations!(events_2, &[failure]);
2712+
node.node.process_pending_htlc_forwards();
2713+
check_added_monitors!(node, 1);
27022714
} else {
27032715
assert!(events_2.is_empty());
27042716
}

lightning/src/ln/onion_payment.rs

+4-3
Original file line numberDiff line numberDiff line change
@@ -135,14 +135,14 @@ pub(super) fn create_recv_pending_htlc_info(
135135
) -> Result<PendingHTLCInfo, InboundHTLCErr> {
136136
let (
137137
payment_data, keysend_preimage, custom_tlvs, onion_amt_msat, onion_cltv_expiry,
138-
payment_metadata, payment_context, requires_blinded_error
138+
payment_metadata, payment_context, requires_blinded_error, has_recipient_created_payment_secret
139139
) = match hop_data {
140140
msgs::InboundOnionPayload::Receive {
141141
payment_data, keysend_preimage, custom_tlvs, sender_intended_htlc_amt_msat,
142142
cltv_expiry_height, payment_metadata, ..
143143
} =>
144144
(payment_data, keysend_preimage, custom_tlvs, sender_intended_htlc_amt_msat,
145-
cltv_expiry_height, payment_metadata, None, false),
145+
cltv_expiry_height, payment_metadata, None, false, keysend_preimage.is_none()),
146146
msgs::InboundOnionPayload::BlindedReceive {
147147
sender_intended_htlc_amt_msat, total_msat, cltv_expiry_height, payment_secret,
148148
intro_node_blinding_point, payment_constraints, payment_context, keysend_preimage,
@@ -161,7 +161,7 @@ pub(super) fn create_recv_pending_htlc_info(
161161
let payment_data = msgs::FinalOnionHopData { payment_secret, total_msat };
162162
(Some(payment_data), keysend_preimage, custom_tlvs,
163163
sender_intended_htlc_amt_msat, cltv_expiry_height, None, Some(payment_context),
164-
intro_node_blinding_point.is_none())
164+
intro_node_blinding_point.is_none(), true)
165165
}
166166
msgs::InboundOnionPayload::Forward { .. } => {
167167
return Err(InboundHTLCErr {
@@ -241,6 +241,7 @@ pub(super) fn create_recv_pending_htlc_info(
241241
incoming_cltv_expiry: onion_cltv_expiry,
242242
custom_tlvs,
243243
requires_blinded_error,
244+
has_recipient_created_payment_secret,
244245
}
245246
} else if let Some(data) = payment_data {
246247
PendingHTLCRouting::Receive {

0 commit comments

Comments
 (0)