Skip to content

Commit 1661d3c

Browse files
committed
Add ErrorAction in secp Error -> HandleError conv in ChannelManager
1 parent c91c4be commit 1661d3c

File tree

1 file changed

+13
-11
lines changed

1 file changed

+13
-11
lines changed

src/ln/channelmanager.rs

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -189,11 +189,10 @@ pub struct ChannelManager {
189189
const CLTV_EXPIRY_DELTA: u16 = 6 * 24 * 2; //TODO?
190190

191191
macro_rules! secp_call {
192-
( $res : expr ) => {
192+
( $res: expr, $err_msg: expr, $action: expr ) => {
193193
match $res {
194194
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)})
197196
}
198197
};
199198
}
@@ -475,7 +474,7 @@ impl ChannelManager {
475474

476475
// can only fail if an intermediary hop has an invalid public key or session_priv is invalid
477476
#[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> {
479478
let mut blinded_priv = session_priv.clone();
480479
let mut blinded_pub = PublicKey::from_secret_key(secp_ctx, &blinded_priv);
481480
let mut first_iteration = true;
@@ -495,7 +494,7 @@ impl ChannelManager {
495494
}
496495
let ephemeral_pubkey = blinded_pub;
497496

498-
secp_call!(blinded_priv.mul_assign(secp_ctx, &secp_call!(SecretKey::from_slice(secp_ctx, &blinding_factor))));
497+
blinded_priv.mul_assign(secp_ctx, &SecretKey::from_slice(secp_ctx, &blinding_factor)?)?;
499498
blinded_pub = PublicKey::from_secret_key(secp_ctx, &blinded_priv);
500499

501500
callback(shared_secret, blinding_factor, ephemeral_pubkey, hop);
@@ -505,7 +504,7 @@ impl ChannelManager {
505504
}
506505

507506
// can only fail if an intermediary hop has an invalid public key or session_priv is invalid
508-
fn construct_onion_keys<T: secp256k1::Signing>(secp_ctx: &Secp256k1<T>, route: &Route, session_priv: &SecretKey) -> Result<Vec<OnionKeys>, HandleError> {
507+
fn construct_onion_keys<T: secp256k1::Signing>(secp_ctx: &Secp256k1<T>, route: &Route, session_priv: &SecretKey) -> Result<Vec<OnionKeys>, secp256k1::Error> {
509508
let mut res = Vec::with_capacity(route.hops.len());
510509

511510
Self::construct_onion_keys_callback(secp_ctx, route, session_priv, |shared_secret, _blinding_factor, ephemeral_pubkey, _| {
@@ -910,15 +909,17 @@ impl ChannelManager {
910909
}
911910
}
912911

913-
let session_priv = secp_call!(SecretKey::from_slice(&self.secp_ctx, &{
912+
let session_priv = SecretKey::from_slice(&self.secp_ctx, &{
914913
let mut session_key = [0; 32];
915914
rng::fill_bytes(&mut session_key);
916915
session_key
917-
}));
916+
}).expect("RNG is bad!");
918917

919918
let cur_height = self.latest_block_height.load(Ordering::Acquire) as u32 + 1;
920919

921-
let onion_keys = ChannelManager::construct_onion_keys(&self.secp_ctx, &route, &session_priv)?;
920+
//TODO: This should return something other than HandleError, that's really intended for
921+
//p2p-returns only.
922+
let onion_keys = secp_call!(ChannelManager::construct_onion_keys(&self.secp_ctx, &route, &session_priv), "Pubkey along hop was maliciously selected", msgs::ErrorAction::IgnoreError);
922923
let (onion_payloads, htlc_msat, htlc_cltv) = ChannelManager::build_onion_payloads(&route, cur_height)?;
923924
let onion_packet = ChannelManager::construct_onion_packet(onion_payloads, onion_keys, &payment_hash)?;
924925

@@ -1998,8 +1999,9 @@ impl ChannelMessageHandler for ChannelManager {
19981999

19992000
let were_node_one = announcement.node_id_1 == our_node_id;
20002001
let msghash = Message::from_slice(&Sha256dHash::from_data(&announcement.encode()[..])[..]).unwrap();
2001-
secp_call!(self.secp_ctx.verify(&msghash, &msg.node_signature, if were_node_one { &announcement.node_id_2 } else { &announcement.node_id_1 }));
2002-
secp_call!(self.secp_ctx.verify(&msghash, &msg.bitcoin_signature, if were_node_one { &announcement.bitcoin_key_2 } else { &announcement.bitcoin_key_1 }));
2002+
let bad_sig_action = msgs::ErrorAction::SendErrorMessage { msg: msgs::ErrorMessage { channel_id: msg.channel_id.clone(), data: "Invalid signature in announcement_signatures".to_string() } };
2003+
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);
2004+
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);
20032005

20042006
let our_node_sig = self.secp_ctx.sign(&msghash, &self.our_network_key);
20052007

0 commit comments

Comments
 (0)