@@ -1424,6 +1424,94 @@ fn custom_tlvs_to_blinded_path() {
14241424 ) ;
14251425}
14261426
1427+ #[ test]
1428+ fn fails_receive_tlvs_authentication ( ) {
1429+ let chanmon_cfgs = create_chanmon_cfgs ( 2 ) ;
1430+ let node_cfgs = create_node_cfgs ( 2 , & chanmon_cfgs) ;
1431+ let node_chanmgrs = create_node_chanmgrs ( 2 , & node_cfgs, & [ None , None ] ) ;
1432+ let nodes = create_network ( 2 , & node_cfgs, & node_chanmgrs) ;
1433+ let chan_upd = create_announced_chan_between_nodes_with_value ( & nodes, 0 , 1 , 1_000_000 , 0 ) . 0 . contents ;
1434+
1435+ let amt_msat = 5000 ;
1436+ let ( payment_preimage, payment_hash, payment_secret) = get_payment_preimage_hash ( & nodes[ 1 ] , Some ( amt_msat) , None ) ;
1437+ let payee_tlvs = UnauthenticatedReceiveTlvs {
1438+ payment_secret,
1439+ payment_constraints : PaymentConstraints {
1440+ max_cltv_expiry : u32:: max_value ( ) ,
1441+ htlc_minimum_msat : chan_upd. htlc_minimum_msat ,
1442+ } ,
1443+ payment_context : PaymentContext :: Bolt12Refund ( Bolt12RefundContext { } ) ,
1444+ } ;
1445+ let nonce = Nonce ( [ 42u8 ; 16 ] ) ;
1446+ let expanded_key = chanmon_cfgs[ 1 ] . keys_manager . get_inbound_payment_key ( ) ;
1447+ let payee_tlvs = payee_tlvs. authenticate ( nonce, & expanded_key) ;
1448+
1449+ let mut secp_ctx = Secp256k1 :: new ( ) ;
1450+ let blinded_path = BlindedPaymentPath :: new (
1451+ & [ ] , nodes[ 1 ] . node . get_our_node_id ( ) , payee_tlvs, u64:: MAX , TEST_FINAL_CLTV as u16 ,
1452+ & chanmon_cfgs[ 1 ] . keys_manager , & secp_ctx
1453+ ) . unwrap ( ) ;
1454+
1455+ let route_params = RouteParameters :: from_payment_params_and_value (
1456+ PaymentParameters :: blinded ( vec ! [ blinded_path] ) ,
1457+ amt_msat,
1458+ ) ;
1459+
1460+ // Test authentication works normally.
1461+ nodes[ 0 ] . node . send_payment ( payment_hash, RecipientOnionFields :: spontaneous_empty ( ) , PaymentId ( payment_hash. 0 ) , route_params, Retry :: Attempts ( 0 ) ) . unwrap ( ) ;
1462+ check_added_monitors ( & nodes[ 0 ] , 1 ) ;
1463+ pass_along_route ( & nodes[ 0 ] , & [ & [ & nodes[ 1 ] ] ] , amt_msat, payment_hash, payment_secret) ;
1464+ claim_payment ( & nodes[ 0 ] , & [ & nodes[ 1 ] ] , payment_preimage) ;
1465+
1466+ // Swap in a different nonce to force authentication to fail.
1467+ let ( _, payment_hash, payment_secret) = get_payment_preimage_hash ( & nodes[ 1 ] , Some ( amt_msat) , None ) ;
1468+ let payee_tlvs = UnauthenticatedReceiveTlvs {
1469+ payment_secret,
1470+ payment_constraints : PaymentConstraints {
1471+ max_cltv_expiry : u32:: max_value ( ) ,
1472+ htlc_minimum_msat : chan_upd. htlc_minimum_msat ,
1473+ } ,
1474+ payment_context : PaymentContext :: Bolt12Refund ( Bolt12RefundContext { } ) ,
1475+ } ;
1476+ let nonce = Nonce ( [ 43u8 ; 16 ] ) ;
1477+ let mut payee_tlvs = payee_tlvs. authenticate ( nonce, & expanded_key) ;
1478+ payee_tlvs. authentication . 1 = Nonce ( [ 0u8 ; 16 ] ) ;
1479+
1480+ let mut secp_ctx = Secp256k1 :: new ( ) ;
1481+ let blinded_path = BlindedPaymentPath :: new (
1482+ & [ ] , nodes[ 1 ] . node . get_our_node_id ( ) , payee_tlvs, u64:: MAX , TEST_FINAL_CLTV as u16 ,
1483+ & chanmon_cfgs[ 1 ] . keys_manager , & secp_ctx
1484+ ) . unwrap ( ) ;
1485+
1486+ let route_params = RouteParameters :: from_payment_params_and_value (
1487+ PaymentParameters :: blinded ( vec ! [ blinded_path] ) ,
1488+ amt_msat,
1489+ ) ;
1490+
1491+ nodes[ 0 ] . node . send_payment ( payment_hash, RecipientOnionFields :: spontaneous_empty ( ) , PaymentId ( payment_hash. 0 ) , route_params, Retry :: Attempts ( 0 ) ) . unwrap ( ) ;
1492+ check_added_monitors ( & nodes[ 0 ] , 1 ) ;
1493+
1494+ let mut events = nodes[ 0 ] . node . get_and_clear_pending_msg_events ( ) ;
1495+ assert_eq ! ( events. len( ) , 1 ) ;
1496+ let ev = remove_first_msg_event_to_node ( & nodes[ 1 ] . node . get_our_node_id ( ) , & mut events) ;
1497+ let mut payment_event = SendEvent :: from_event ( ev) ;
1498+
1499+ nodes[ 1 ] . node . handle_update_add_htlc ( nodes[ 0 ] . node . get_our_node_id ( ) , & payment_event. msgs [ 0 ] ) ;
1500+ check_added_monitors ! ( nodes[ 1 ] , 0 ) ;
1501+ do_commitment_signed_dance ( & nodes[ 1 ] , & nodes[ 0 ] , & payment_event. commitment_msg , true , true ) ;
1502+ nodes[ 1 ] . node . process_pending_htlc_forwards ( ) ;
1503+
1504+ let mut update_fail = get_htlc_update_msgs ! ( nodes[ 1 ] , nodes[ 0 ] . node. get_our_node_id( ) ) ;
1505+ assert ! ( update_fail. update_fail_htlcs. len( ) == 1 ) ;
1506+ let fail_msg = & update_fail. update_fail_htlcs [ 0 ] ;
1507+ nodes[ 0 ] . node . handle_update_fail_htlc ( nodes[ 1 ] . node . get_our_node_id ( ) , fail_msg) ;
1508+ commitment_signed_dance ! ( nodes[ 0 ] , nodes[ 1 ] , update_fail. commitment_signed, false ) ;
1509+ expect_payment_failed_conditions (
1510+ & nodes[ 0 ] , payment_hash, true ,
1511+ PaymentFailedConditions :: new ( ) . expected_htlc_error_data ( 0x4000 | 22 , & [ ] ) ,
1512+ ) ;
1513+ }
1514+
14271515fn secret_from_hex ( hex : & str ) -> SecretKey {
14281516 SecretKey :: from_slice ( & <Vec < u8 > >:: from_hex ( hex) . unwrap ( ) ) . unwrap ( )
14291517}
0 commit comments