@@ -625,7 +625,7 @@ impl ChannelManager {
625
625
626
626
Ok ( msgs:: OnionPacket {
627
627
version : 0 ,
628
- public_key : onion_keys. first ( ) . unwrap ( ) . ephemeral_pubkey ,
628
+ public_key : Ok ( onion_keys. first ( ) . unwrap ( ) . ephemeral_pubkey ) ,
629
629
hop_data : packet_data,
630
630
hmac : hmac_res,
631
631
} )
@@ -681,10 +681,7 @@ impl ChannelManager {
681
681
ChannelManager :: encrypt_failure_packet ( shared_secret, & failure_packet. encode ( ) [ ..] )
682
682
}
683
683
684
- fn decode_update_add_htlc_onion ( & self , msg : & msgs:: UpdateAddHTLC ) -> ( PendingHTLCStatus , SharedSecret , MutexGuard < ChannelHolder > ) {
685
- let shared_secret = SharedSecret :: new ( & self . secp_ctx , & msg. onion_routing_packet . public_key , & self . our_network_key ) ;
686
- let ( rho, mu) = ChannelManager :: gen_rho_mu_from_shared_secret ( & shared_secret) ;
687
-
684
+ fn decode_update_add_htlc_onion ( & self , msg : & msgs:: UpdateAddHTLC ) -> ( PendingHTLCStatus , Option < SharedSecret > , MutexGuard < ChannelHolder > ) {
688
685
macro_rules! get_onion_hash {
689
686
( ) => {
690
687
{
@@ -697,6 +694,19 @@ impl ChannelManager {
697
694
}
698
695
}
699
696
697
+ if let Err ( _) = msg. onion_routing_packet . public_key {
698
+ log_info ! ( self , "Failed to accept/forward incoming HTLC with invalid ephemeral pubkey" ) ;
699
+ return ( PendingHTLCStatus :: Fail ( HTLCFailureMsg :: Malformed ( msgs:: UpdateFailMalformedHTLC {
700
+ channel_id : msg. channel_id ,
701
+ htlc_id : msg. htlc_id ,
702
+ sha256_of_onion : get_onion_hash ! ( ) ,
703
+ failure_code : 0x8000 | 0x4000 | 6 ,
704
+ } ) ) , None , self . channel_state . lock ( ) . unwrap ( ) ) ;
705
+ }
706
+
707
+ let shared_secret = SharedSecret :: new ( & self . secp_ctx , & msg. onion_routing_packet . public_key . unwrap ( ) , & self . our_network_key ) ;
708
+ let ( rho, mu) = ChannelManager :: gen_rho_mu_from_shared_secret ( & shared_secret) ;
709
+
700
710
let mut channel_state = None ;
701
711
macro_rules! return_err {
702
712
( $msg: expr, $err_code: expr, $data: expr) => {
@@ -709,7 +719,7 @@ impl ChannelManager {
709
719
channel_id: msg. channel_id,
710
720
htlc_id: msg. htlc_id,
711
721
reason: ChannelManager :: build_first_hop_failure_packet( & shared_secret, $err_code, $data) ,
712
- } ) ) , shared_secret, channel_state. unwrap( ) ) ;
722
+ } ) ) , Some ( shared_secret) , channel_state. unwrap( ) ) ;
713
723
}
714
724
}
715
725
}
@@ -776,7 +786,7 @@ impl ChannelManager {
776
786
chacha. process ( & msg. onion_routing_packet . hop_data [ 65 ..] , & mut new_packet_data[ 0 ..19 * 65 ] ) ;
777
787
chacha. process ( & ChannelManager :: ZERO [ 0 ..65 ] , & mut new_packet_data[ 19 * 65 ..] ) ;
778
788
779
- let mut new_pubkey = msg. onion_routing_packet . public_key . clone ( ) ;
789
+ let mut new_pubkey = msg. onion_routing_packet . public_key . unwrap ( ) ;
780
790
781
791
let blinding_factor = {
782
792
let mut sha = Sha256 :: new ( ) ;
@@ -786,26 +796,19 @@ impl ChannelManager {
786
796
sha. result ( & mut res) ;
787
797
match SecretKey :: from_slice ( & self . secp_ctx , & res) {
788
798
Err ( _) => {
789
- // Return temporary node failure as its technically our issue, not the
790
- // channel's issue.
791
- return_err ! ( "Blinding factor is an invalid private key" , 0x2000 | 2 , & [ 0 ; 0 ] ) ;
799
+ return_err ! ( "Blinding factor is an invalid private key" , 0x8000 | 0x4000 | 6 , & get_onion_hash!( ) ) ;
792
800
} ,
793
801
Ok ( key) => key
794
802
}
795
803
} ;
796
804
797
- match new_pubkey. mul_assign ( & self . secp_ctx , & blinding_factor) {
798
- Err ( _) => {
799
- // Return temporary node failure as its technically our issue, not the
800
- // channel's issue.
801
- return_err ! ( "New blinding factor is an invalid private key" , 0x2000 | 2 , & [ 0 ; 0 ] ) ;
802
- } ,
803
- Ok ( _) => { }
804
- } ;
805
+ if let Err ( _) = new_pubkey. mul_assign ( & self . secp_ctx , & blinding_factor) {
806
+ return_err ! ( "New blinding factor is an invalid private key" , 0x8000 | 0x4000 | 6 , & get_onion_hash!( ) ) ;
807
+ }
805
808
806
809
let outgoing_packet = msgs:: OnionPacket {
807
810
version : 0 ,
808
- public_key : new_pubkey,
811
+ public_key : Ok ( new_pubkey) ,
809
812
hop_data : new_packet_data,
810
813
hmac : next_hop_data. hmac . clone ( ) ,
811
814
} ;
@@ -852,7 +855,7 @@ impl ChannelManager {
852
855
}
853
856
}
854
857
855
- ( pending_forward_info, shared_secret, channel_state. unwrap ( ) )
858
+ ( pending_forward_info, Some ( shared_secret) , channel_state. unwrap ( ) )
856
859
}
857
860
858
861
/// only fails if the channel does not yet have an assigned short_id
@@ -1735,7 +1738,7 @@ impl ChannelMessageHandler for ChannelManager {
1735
1738
pending_forward_info = PendingHTLCStatus :: Fail ( HTLCFailureMsg :: Relay ( msgs:: UpdateFailHTLC {
1736
1739
channel_id : msg. channel_id ,
1737
1740
htlc_id : msg. htlc_id ,
1738
- reason : ChannelManager :: build_first_hop_failure_packet ( & shared_secret, 0x4000 | 0x2000 | 2 , & [ 0 ; 0 ] ) ,
1741
+ reason : ChannelManager :: build_first_hop_failure_packet ( & shared_secret. unwrap ( ) , 0x4000 | 0x2000 | 2 , & [ 0 ; 0 ] ) ,
1739
1742
} ) ) ;
1740
1743
} else {
1741
1744
will_forward = true ;
@@ -1774,15 +1777,15 @@ impl ChannelMessageHandler for ChannelManager {
1774
1777
} ;
1775
1778
* outbound_route = PendingOutboundHTLC :: CycledRoute {
1776
1779
source_short_channel_id,
1777
- incoming_packet_shared_secret : shared_secret,
1780
+ incoming_packet_shared_secret : shared_secret. unwrap ( ) ,
1778
1781
route,
1779
1782
session_priv,
1780
1783
} ;
1781
1784
} ,
1782
1785
hash_map:: Entry :: Vacant ( e) => {
1783
1786
e. insert ( PendingOutboundHTLC :: IntermediaryHopData {
1784
1787
source_short_channel_id,
1785
- incoming_packet_shared_secret : shared_secret,
1788
+ incoming_packet_shared_secret : shared_secret. unwrap ( ) ,
1786
1789
} ) ;
1787
1790
}
1788
1791
}
0 commit comments