@@ -189,11 +189,10 @@ pub struct ChannelManager {
189
189
const CLTV_EXPIRY_DELTA : u16 = 6 * 24 * 2 ; //TODO?
190
190
191
191
macro_rules! secp_call {
192
- ( $res : expr ) => {
192
+ ( $res: expr , $err_msg : expr , $action : expr ) => {
193
193
match $res {
194
194
Ok ( key) => key,
195
- //TODO: Make the err a parameter!
196
- Err ( _) => return Err ( HandleError { err: "Key error" , action: None } )
195
+ Err ( _) => return Err ( HandleError { err: $err_msg, action: Some ( $action) } )
197
196
}
198
197
} ;
199
198
}
@@ -475,7 +474,7 @@ impl ChannelManager {
475
474
476
475
// can only fail if an intermediary hop has an invalid public key or session_priv is invalid
477
476
#[ inline]
478
- fn construct_onion_keys_callback < T : secp256k1:: Signing , FType : FnMut ( SharedSecret , [ u8 ; 32 ] , PublicKey , & RouteHop ) > ( secp_ctx : & Secp256k1 < T > , route : & Route , session_priv : & SecretKey , mut callback : FType ) -> Result < ( ) , HandleError > {
477
+ fn construct_onion_keys_callback < T : secp256k1:: Signing , FType : FnMut ( SharedSecret , [ u8 ; 32 ] , PublicKey , & RouteHop ) > ( secp_ctx : & Secp256k1 < T > , route : & Route , session_priv : & SecretKey , mut callback : FType ) -> Result < ( ) , secp256k1 :: Error > {
479
478
let mut blinded_priv = session_priv. clone ( ) ;
480
479
let mut blinded_pub = PublicKey :: from_secret_key ( secp_ctx, & blinded_priv) ;
481
480
@@ -490,7 +489,7 @@ impl ChannelManager {
490
489
491
490
let ephemeral_pubkey = blinded_pub;
492
491
493
- secp_call ! ( blinded_priv. mul_assign( secp_ctx, & secp_call! ( SecretKey :: from_slice( secp_ctx, & blinding_factor) ) ) ) ;
492
+ blinded_priv. mul_assign ( secp_ctx, & SecretKey :: from_slice ( secp_ctx, & blinding_factor) ? ) ? ;
494
493
blinded_pub = PublicKey :: from_secret_key ( secp_ctx, & blinded_priv) ;
495
494
496
495
callback ( shared_secret, blinding_factor, ephemeral_pubkey, hop) ;
@@ -500,7 +499,7 @@ impl ChannelManager {
500
499
}
501
500
502
501
// can only fail if an intermediary hop has an invalid public key or session_priv is invalid
503
- fn construct_onion_keys < T : secp256k1:: Signing > ( secp_ctx : & Secp256k1 < T > , route : & Route , session_priv : & SecretKey ) -> Result < Vec < OnionKeys > , HandleError > {
502
+ fn construct_onion_keys < T : secp256k1:: Signing > ( secp_ctx : & Secp256k1 < T > , route : & Route , session_priv : & SecretKey ) -> Result < Vec < OnionKeys > , secp256k1 :: Error > {
504
503
let mut res = Vec :: with_capacity ( route. hops . len ( ) ) ;
505
504
506
505
Self :: construct_onion_keys_callback ( secp_ctx, route, session_priv, |shared_secret, _blinding_factor, ephemeral_pubkey, _| {
@@ -905,15 +904,17 @@ impl ChannelManager {
905
904
}
906
905
}
907
906
908
- let session_priv = secp_call ! ( SecretKey :: from_slice( & self . secp_ctx, & {
907
+ let session_priv = SecretKey :: from_slice ( & self . secp_ctx , & {
909
908
let mut session_key = [ 0 ; 32 ] ;
910
909
rng:: fill_bytes ( & mut session_key) ;
911
910
session_key
912
- } ) ) ;
911
+ } ) . expect ( "RNG is bad!" ) ;
913
912
914
913
let cur_height = self . latest_block_height . load ( Ordering :: Acquire ) as u32 + 1 ;
915
914
916
- let onion_keys = ChannelManager :: construct_onion_keys ( & self . secp_ctx , & route, & session_priv) ?;
915
+ //TODO: This should return something other than HandleError, that's really intended for
916
+ //p2p-returns only.
917
+ let onion_keys = secp_call ! ( ChannelManager :: construct_onion_keys( & self . secp_ctx, & route, & session_priv) , "Pubkey along hop was maliciously selected" , msgs:: ErrorAction :: IgnoreError ) ;
917
918
let ( onion_payloads, htlc_msat, htlc_cltv) = ChannelManager :: build_onion_payloads ( & route, cur_height) ?;
918
919
let onion_packet = ChannelManager :: construct_onion_packet ( onion_payloads, onion_keys, & payment_hash) ?;
919
920
@@ -1982,19 +1983,20 @@ impl ChannelMessageHandler for ChannelManager {
1982
1983
match channel_state. by_id . get_mut ( & msg. channel_id ) {
1983
1984
Some ( chan) => {
1984
1985
if chan. get_their_node_id ( ) != * their_node_id {
1985
- return Err ( HandleError { err : "Got a message for a channel from the wrong node!" , action : None } )
1986
+ return Err ( HandleError { err : "Got a message for a channel from the wrong node!" , action : Some ( msgs :: ErrorAction :: IgnoreError ) } )
1986
1987
}
1987
1988
if !chan. is_usable ( ) {
1988
- return Err ( HandleError { err : "Got an announcement_signatures before we were ready for it" , action : None } ) ;
1989
+ return Err ( HandleError { err : "Got an announcement_signatures before we were ready for it" , action : Some ( msgs :: ErrorAction :: IgnoreError ) } ) ;
1989
1990
}
1990
1991
1991
1992
let our_node_id = self . get_our_node_id ( ) ;
1992
1993
let ( announcement, our_bitcoin_sig) = chan. get_channel_announcement ( our_node_id. clone ( ) , self . genesis_hash . clone ( ) ) ?;
1993
1994
1994
1995
let were_node_one = announcement. node_id_1 == our_node_id;
1995
1996
let msghash = Message :: from_slice ( & Sha256dHash :: from_data ( & announcement. encode ( ) [ ..] ) [ ..] ) . unwrap ( ) ;
1996
- secp_call ! ( self . secp_ctx. verify( & msghash, & msg. node_signature, if were_node_one { & announcement. node_id_2 } else { & announcement. node_id_1 } ) ) ;
1997
- secp_call ! ( self . secp_ctx. verify( & msghash, & msg. bitcoin_signature, if were_node_one { & announcement. bitcoin_key_2 } else { & announcement. bitcoin_key_1 } ) ) ;
1997
+ let bad_sig_action = msgs:: ErrorAction :: SendErrorMessage { msg : msgs:: ErrorMessage { channel_id : msg. channel_id . clone ( ) , data : "Invalid signature in announcement_signatures" . to_string ( ) } } ;
1998
+ secp_call ! ( self . secp_ctx. verify( & msghash, & msg. node_signature, if were_node_one { & announcement. node_id_2 } else { & announcement. node_id_1 } ) , "Bad announcement_signatures node_signature" , bad_sig_action) ;
1999
+ secp_call ! ( self . secp_ctx. verify( & msghash, & msg. bitcoin_signature, if were_node_one { & announcement. bitcoin_key_2 } else { & announcement. bitcoin_key_1 } ) , "Bad announcement_signatures bitcoin_signature" , bad_sig_action) ;
1998
2000
1999
2001
let our_node_sig = self . secp_ctx . sign ( & msghash, & self . our_network_key ) ;
2000
2002
@@ -2006,7 +2008,7 @@ impl ChannelMessageHandler for ChannelManager {
2006
2008
contents : announcement,
2007
2009
} , self . get_channel_update ( chan) . unwrap ( ) ) // can only fail if we're not in a ready state
2008
2010
} ,
2009
- None => return Err ( HandleError { err : "Failed to find corresponding channel" , action : None } )
2011
+ None => return Err ( HandleError { err : "Failed to find corresponding channel" , action : Some ( msgs :: ErrorAction :: IgnoreError ) } )
2010
2012
}
2011
2013
} ;
2012
2014
let mut pending_events = self . pending_events . lock ( ) . unwrap ( ) ;
0 commit comments