Skip to content

Commit eeefdaf

Browse files
committed
Always return an Error Message in invalid sig/key errors in Channel
1 parent 2a93f98 commit eeefdaf

File tree

1 file changed

+14
-15
lines changed

1 file changed

+14
-15
lines changed

src/ln/channel.rs

Lines changed: 14 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -338,18 +338,17 @@ const B_OUTPUT_PLUS_SPENDING_INPUT_WEIGHT: u64 = 104; // prevout: 40, nSequence:
338338
pub const MAX_FUNDING_SATOSHIS: u64 = (1 << 24);
339339

340340
macro_rules! secp_call {
341-
( $res: expr, $err: expr ) => {
341+
( $res: expr, $err: expr, $chan_id: expr ) => {
342342
match $res {
343343
Ok(key) => key,
344-
//TODO: make the error a parameter
345-
Err(_) => return Err(HandleError{err: $err, action: Some(msgs::ErrorAction::DisconnectPeer{ msg: None })})
344+
Err(_) => return Err(HandleError {err: $err, action: Some(msgs::ErrorAction::SendErrorMessage {msg: msgs::ErrorMessage {channel_id: $chan_id, data: $err.to_string()}})})
346345
}
347346
};
348347
}
349348

350349
macro_rules! secp_derived_key {
351-
( $res: expr ) => {
352-
secp_call!($res, "Derived invalid key, peer is maliciously selecting parameters")
350+
( $res: expr, $chan_id: expr ) => {
351+
secp_call!($res, "Derived invalid key, peer is maliciously selecting parameters", $chan_id)
353352
}
354353
}
355354
impl Channel {
@@ -883,7 +882,7 @@ impl Channel {
883882
let delayed_payment_base = PublicKey::from_secret_key(&self.secp_ctx, &self.local_keys.delayed_payment_base_key);
884883
let htlc_basepoint = PublicKey::from_secret_key(&self.secp_ctx, &self.local_keys.htlc_base_key);
885884

886-
Ok(secp_derived_key!(TxCreationKeys::new(&self.secp_ctx, &per_commitment_point, &delayed_payment_base, &htlc_basepoint, &self.their_revocation_basepoint.unwrap(), &self.their_payment_basepoint.unwrap(), &self.their_htlc_basepoint.unwrap())))
885+
Ok(secp_derived_key!(TxCreationKeys::new(&self.secp_ctx, &per_commitment_point, &delayed_payment_base, &htlc_basepoint, &self.their_revocation_basepoint.unwrap(), &self.their_payment_basepoint.unwrap(), &self.their_htlc_basepoint.unwrap()), self.channel_id()))
887886
}
888887

889888
#[inline]
@@ -896,7 +895,7 @@ impl Channel {
896895
let revocation_basepoint = PublicKey::from_secret_key(&self.secp_ctx, &self.local_keys.revocation_base_key);
897896
let htlc_basepoint = PublicKey::from_secret_key(&self.secp_ctx, &self.local_keys.htlc_base_key);
898897

899-
Ok(secp_derived_key!(TxCreationKeys::new(&self.secp_ctx, &self.their_cur_commitment_point.unwrap(), &self.their_delayed_payment_basepoint.unwrap(), &self.their_htlc_basepoint.unwrap(), &revocation_basepoint, &payment_basepoint, &htlc_basepoint)))
898+
Ok(secp_derived_key!(TxCreationKeys::new(&self.secp_ctx, &self.their_cur_commitment_point.unwrap(), &self.their_delayed_payment_basepoint.unwrap(), &self.their_htlc_basepoint.unwrap(), &revocation_basepoint, &payment_basepoint, &htlc_basepoint), self.channel_id()))
900899
}
901900

902901
/// Gets the redeemscript for the funding transaction output (ie the funding transaction output
@@ -960,7 +959,7 @@ impl Channel {
960959

961960
let htlc_redeemscript = chan_utils::get_htlc_redeemscript(&htlc, &keys);
962961

963-
let our_htlc_key = secp_derived_key!(chan_utils::derive_private_key(&self.secp_ctx, &keys.per_commitment_point, &self.local_keys.htlc_base_key));
962+
let our_htlc_key = secp_derived_key!(chan_utils::derive_private_key(&self.secp_ctx, &keys.per_commitment_point, &self.local_keys.htlc_base_key), self.channel_id());
964963
let sighash = Message::from_slice(&bip143::SighashComponents::new(&tx).sighash_all(&tx.input[0], &htlc_redeemscript, htlc.amount_msat / 1000)[..]).unwrap();
965964
let is_local_tx = PublicKey::from_secret_key(&self.secp_ctx, &our_htlc_key) == keys.a_htlc_key;
966965
Ok((htlc_redeemscript, self.secp_ctx.sign(&sighash, &our_htlc_key), is_local_tx))
@@ -1268,7 +1267,7 @@ impl Channel {
12681267
let local_sighash = Message::from_slice(&bip143::SighashComponents::new(&local_initial_commitment_tx).sighash_all(&local_initial_commitment_tx.input[0], &funding_script, self.channel_value_satoshis)[..]).unwrap();
12691268

12701269
// They sign the "local" commitment transaction, allowing us to broadcast the tx if we wish.
1271-
secp_call!(self.secp_ctx.verify(&local_sighash, &sig, &self.their_funding_pubkey.unwrap()), "Invalid funding_created signature from peer");
1270+
secp_call!(self.secp_ctx.verify(&local_sighash, &sig, &self.their_funding_pubkey.unwrap()), "Invalid funding_created signature from peer", self.channel_id());
12721271

12731272
// We sign the "remote" commitment transaction, allowing them to broadcast the tx if they wish.
12741273
Ok((remote_initial_commitment_tx, self.secp_ctx.sign(&remote_sighash, &self.local_keys.funding_key)))
@@ -1331,7 +1330,7 @@ impl Channel {
13311330
let local_sighash = Message::from_slice(&bip143::SighashComponents::new(&local_initial_commitment_tx).sighash_all(&local_initial_commitment_tx.input[0], &funding_script, self.channel_value_satoshis)[..]).unwrap();
13321331

13331332
// They sign the "local" commitment transaction, allowing us to broadcast the tx if we wish.
1334-
secp_call!(self.secp_ctx.verify(&local_sighash, &msg.signature, &self.their_funding_pubkey.unwrap()), "Invalid funding_signed signature from peer");
1333+
secp_call!(self.secp_ctx.verify(&local_sighash, &msg.signature, &self.their_funding_pubkey.unwrap()), "Invalid funding_signed signature from peer", self.channel_id());
13351334

13361335
self.sign_commitment_transaction(&mut local_initial_commitment_tx, &msg.signature);
13371336
self.channel_monitor.provide_latest_local_commitment_tx_info(local_initial_commitment_tx.clone(), local_keys, self.feerate_per_kw, Vec::new());
@@ -1515,7 +1514,7 @@ impl Channel {
15151514
let mut local_commitment_tx = self.build_commitment_transaction(self.cur_local_commitment_transaction_number, &local_keys, true, false);
15161515
let local_commitment_txid = local_commitment_tx.0.txid();
15171516
let local_sighash = Message::from_slice(&bip143::SighashComponents::new(&local_commitment_tx.0).sighash_all(&local_commitment_tx.0.input[0], &funding_script, self.channel_value_satoshis)[..]).unwrap();
1518-
secp_call!(self.secp_ctx.verify(&local_sighash, &msg.signature, &self.their_funding_pubkey.unwrap()), "Invalid commitment tx signature from peer");
1517+
secp_call!(self.secp_ctx.verify(&local_sighash, &msg.signature, &self.their_funding_pubkey.unwrap()), "Invalid commitment tx signature from peer", self.channel_id());
15191518

15201519
if msg.htlc_signatures.len() != local_commitment_tx.1.len() {
15211520
return Err(HandleError{err: "Got wrong number of HTLC signatures from remote", action: None});
@@ -1530,7 +1529,7 @@ impl Channel {
15301529
let mut htlc_tx = self.build_htlc_transaction(&local_commitment_txid, htlc, true, &local_keys);
15311530
let htlc_redeemscript = chan_utils::get_htlc_redeemscript(&htlc, &local_keys);
15321531
let htlc_sighash = Message::from_slice(&bip143::SighashComponents::new(&htlc_tx).sighash_all(&htlc_tx.input[0], &htlc_redeemscript, htlc.amount_msat / 1000)[..]).unwrap();
1533-
secp_call!(self.secp_ctx.verify(&htlc_sighash, &msg.htlc_signatures[idx], &local_keys.b_htlc_key), "Invalid HTLC tx siganture from peer");
1532+
secp_call!(self.secp_ctx.verify(&htlc_sighash, &msg.htlc_signatures[idx], &local_keys.b_htlc_key), "Invalid HTLC tx siganture from peer", self.channel_id());
15341533
let htlc_sig = if htlc.offered {
15351534
let htlc_sig = self.sign_htlc_transaction(&mut htlc_tx, &msg.htlc_signatures[idx], &None, htlc, &local_keys)?;
15361535
new_local_commitment_txn.push(htlc_tx);
@@ -1666,7 +1665,7 @@ impl Channel {
16661665
return Err(HandleError{err: "Got revoke/ACK message when channel was not in an operational state", action: None});
16671666
}
16681667
if let Some(their_prev_commitment_point) = self.their_prev_commitment_point {
1669-
if PublicKey::from_secret_key(&self.secp_ctx, &secp_call!(SecretKey::from_slice(&self.secp_ctx, &msg.per_commitment_secret), "Peer provided an invalid per_commitment_secret")) != their_prev_commitment_point {
1668+
if PublicKey::from_secret_key(&self.secp_ctx, &secp_call!(SecretKey::from_slice(&self.secp_ctx, &msg.per_commitment_secret), "Peer provided an invalid per_commitment_secret", self.channel_id())) != their_prev_commitment_point {
16701669
return Err(HandleError{err: "Got a revoke commitment secret which didn't correspond to their current pubkey", action: None});
16711670
}
16721671
}
@@ -1893,7 +1892,7 @@ impl Channel {
18931892
// limits, so check for that case by re-checking the signature here.
18941893
closing_tx = self.build_closing_transaction(msg.fee_satoshis, true).0;
18951894
sighash = Message::from_slice(&bip143::SighashComponents::new(&closing_tx).sighash_all(&closing_tx.input[0], &funding_redeemscript, self.channel_value_satoshis)[..]).unwrap();
1896-
secp_call!(self.secp_ctx.verify(&sighash, &msg.signature, &self.their_funding_pubkey.unwrap()), "Invalid closing tx signature from peer");
1895+
secp_call!(self.secp_ctx.verify(&sighash, &msg.signature, &self.their_funding_pubkey.unwrap()), "Invalid closing tx signature from peer", self.channel_id());
18971896
},
18981897
};
18991898

@@ -2448,7 +2447,7 @@ impl Channel {
24482447
let htlc_tx = self.build_htlc_transaction(&remote_commitment_txid, htlc, false, &remote_keys);
24492448
let htlc_redeemscript = chan_utils::get_htlc_redeemscript(&htlc, &remote_keys);
24502449
let htlc_sighash = Message::from_slice(&bip143::SighashComponents::new(&htlc_tx).sighash_all(&htlc_tx.input[0], &htlc_redeemscript, htlc.amount_msat / 1000)[..]).unwrap();
2451-
let our_htlc_key = secp_derived_key!(chan_utils::derive_private_key(&self.secp_ctx, &remote_keys.per_commitment_point, &self.local_keys.htlc_base_key));
2450+
let our_htlc_key = secp_derived_key!(chan_utils::derive_private_key(&self.secp_ctx, &remote_keys.per_commitment_point, &self.local_keys.htlc_base_key), self.channel_id());
24522451
htlc_sigs.push(self.secp_ctx.sign(&htlc_sighash, &our_htlc_key));
24532452
}
24542453

0 commit comments

Comments
 (0)