Skip to content

Commit 0c98489

Browse files
committed
revocation enforcement in signer
1 parent ebcddd9 commit 0c98489

File tree

3 files changed

+32
-14
lines changed

3 files changed

+32
-14
lines changed

lightning/src/chain/keysinterface.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -202,7 +202,8 @@ pub trait ChannelKeys : Send+Clone {
202202
/// Gets the commitment seed for a specific commitment number, thereby revoking the commitment
203203
///
204204
/// After this is called, it is an error to try to sign the relevant local commitment transaction.
205-
/// May be called more than once (e.g. if re-establishing the channel).
205+
/// Must be called in monotonic order, without gaps. May be called more than once (e.g. if
206+
/// re-establishing the channel).
206207
/// Note that the commitment number starts at (1 << 48) - 1 and counts backwards.
207208
fn get_revoke_commitment_secret(&self, idx: u64) -> [u8; 32];
208209
/// Gets the local channel public keys and basepoints

lightning/src/ln/functional_tests.rs

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1603,27 +1603,26 @@ fn test_fee_spike_violation_fails_htlc() {
16031603

16041604
// Get the EnforcingChannelKeys for each channel, which will be used to (1) get the keys
16051605
// needed to sign the new commitment tx and (2) sign the new commitment tx.
1606-
let (local_revocation_basepoint, local_htlc_basepoint, local_payment_point, local_secret, local_secret2) = {
1606+
let (local_revocation_basepoint, local_htlc_basepoint, local_payment_point, local_secret, next_local_point) = {
16071607
let chan_lock = nodes[0].node.channel_state.lock().unwrap();
16081608
let local_chan = chan_lock.by_id.get(&chan.2).unwrap();
16091609
let chan_keys = local_chan.get_local_keys();
16101610
let pubkeys = chan_keys.pubkeys();
16111611
(pubkeys.revocation_basepoint, pubkeys.htlc_basepoint, pubkeys.payment_point,
1612-
chan_keys.get_revoke_commitment_secret(INITIAL_COMMITMENT_NUMBER), chan_keys.get_revoke_commitment_secret(INITIAL_COMMITMENT_NUMBER - 2))
1612+
chan_keys.get_revoke_commitment_secret(INITIAL_COMMITMENT_NUMBER),
1613+
chan_keys.get_per_commitment_point(INITIAL_COMMITMENT_NUMBER - 2, &secp_ctx))
16131614
};
1614-
let (remote_delayed_payment_basepoint, remote_htlc_basepoint, remote_payment_point, remote_secret1) = {
1615+
let (remote_delayed_payment_basepoint, remote_htlc_basepoint, remote_payment_point, remote_point) = {
16151616
let chan_lock = nodes[1].node.channel_state.lock().unwrap();
16161617
let remote_chan = chan_lock.by_id.get(&chan.2).unwrap();
16171618
let chan_keys = remote_chan.get_local_keys();
16181619
let pubkeys = chan_keys.pubkeys();
16191620
(pubkeys.delayed_payment_basepoint, pubkeys.htlc_basepoint, pubkeys.payment_point,
1620-
chan_keys.get_revoke_commitment_secret(INITIAL_COMMITMENT_NUMBER - 1))
1621+
chan_keys.get_per_commitment_point(INITIAL_COMMITMENT_NUMBER - 1, &secp_ctx))
16211622
};
16221623

16231624
// Assemble the set of keys we can use for signatures for our commitment_signed message.
1624-
let commitment_secret = SecretKey::from_slice(&remote_secret1).unwrap();
1625-
let per_commitment_point = PublicKey::from_secret_key(&secp_ctx, &commitment_secret);
1626-
let commit_tx_keys = chan_utils::TxCreationKeys::new(&secp_ctx, &per_commitment_point, &remote_delayed_payment_basepoint,
1625+
let commit_tx_keys = chan_utils::TxCreationKeys::new(&secp_ctx, &remote_point, &remote_delayed_payment_basepoint,
16271626
&remote_htlc_basepoint, &local_revocation_basepoint, &local_htlc_basepoint).unwrap();
16281627

16291628
// Build the remote commitment transaction so we can sign it, and then later use the
@@ -1706,10 +1705,11 @@ fn test_fee_spike_violation_fails_htlc() {
17061705
let _ = nodes[1].node.get_and_clear_pending_msg_events();
17071706

17081707
// Send the RAA to nodes[1].
1709-
let per_commitment_secret = local_secret;
1710-
let next_secret = SecretKey::from_slice(&local_secret2).unwrap();
1711-
let next_per_commitment_point = PublicKey::from_secret_key(&secp_ctx, &next_secret);
1712-
let raa_msg = msgs::RevokeAndACK{ channel_id: chan.2, per_commitment_secret, next_per_commitment_point};
1708+
let raa_msg = msgs::RevokeAndACK {
1709+
channel_id: chan.2,
1710+
per_commitment_secret: local_secret,
1711+
next_per_commitment_point: next_local_point
1712+
};
17131713
nodes[1].node.handle_revoke_and_ack(&nodes[0].node.get_our_node_id(), &raa_msg);
17141714

17151715
let events = nodes[1].node.get_and_clear_pending_msg_events();
@@ -8128,9 +8128,11 @@ fn test_counterparty_raa_skip_no_crash() {
81288128
let mut guard = nodes[0].node.channel_state.lock().unwrap();
81298129
let local_keys = &guard.by_id.get_mut(&channel_id).unwrap().local_keys;
81308130
const INITIAL_COMMITMENT_NUMBER: u64 = (1 << 48) - 1;
8131+
let per_commitment_secret = local_keys.get_revoke_commitment_secret(INITIAL_COMMITMENT_NUMBER);
8132+
// Must revoke without gaps
8133+
local_keys.get_revoke_commitment_secret(INITIAL_COMMITMENT_NUMBER - 1);
81318134
let next_per_commitment_point = PublicKey::from_secret_key(&Secp256k1::new(),
81328135
&SecretKey::from_slice(&local_keys.get_revoke_commitment_secret(INITIAL_COMMITMENT_NUMBER - 2)).unwrap());
8133-
let per_commitment_secret = local_keys.get_revoke_commitment_secret(INITIAL_COMMITMENT_NUMBER);
81348136

81358137
nodes[1].node.handle_revoke_and_ack(&nodes[0].node.get_our_node_id(),
81368138
&msgs::RevokeAndACK { channel_id, per_commitment_secret, next_per_commitment_point });

lightning/src/util/enforcing_trait_impls.rs

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,14 @@ use util::ser::{Writeable, Writer, Readable};
1515
use std::io::Error;
1616
use ln::msgs::DecodeError;
1717

18+
const INITIAL_COMMITMENT_NUMBER: u64 = (1 << 48) - 1;
19+
1820
/// Enforces some rules on ChannelKeys calls. Eventually we will probably want to expose a variant
1921
/// of this which would essentially be what you'd want to run on a hardware wallet.
2022
#[derive(Clone)]
2123
pub struct EnforcingChannelKeys {
2224
pub inner: InMemoryChannelKeys,
25+
revoked_commitment: Arc<Mutex<u64>>,
2326
commitment_number_obscure_and_last: Arc<Mutex<(Option<u64>, u64)>>,
2427
}
2528

@@ -28,6 +31,7 @@ impl EnforcingChannelKeys {
2831
Self {
2932
inner,
3033
commitment_number_obscure_and_last: Arc::new(Mutex::new((None, 0))),
34+
revoked_commitment: Arc::new(Mutex::new(INITIAL_COMMITMENT_NUMBER + 1)),
3135
}
3236
}
3337
}
@@ -52,7 +56,14 @@ impl ChannelKeys for EnforcingChannelKeys {
5256
self.inner.get_per_commitment_point(idx, secp_ctx)
5357
}
5458

55-
fn get_revoke_commitment_secret(&self, idx: u64) -> [u8; 32] { self.inner.get_revoke_commitment_secret(idx) }
59+
fn get_revoke_commitment_secret(&self, idx: u64) -> [u8; 32] {
60+
let mut revoked = self.revoked_commitment.lock().unwrap();
61+
if idx != *revoked && idx != *revoked - 1 {
62+
panic!("can only revoke the current or next unrevoked commitment - trying {}, revoked {}", idx, *revoked)
63+
}
64+
*revoked = idx;
65+
self.inner.get_revoke_commitment_secret(idx)
66+
}
5667
fn pubkeys(&self) -> &ChannelPublicKeys { self.inner.pubkeys() }
5768
fn key_derivation_params(&self) -> (u64, u64) { self.inner.key_derivation_params() }
5869

@@ -124,6 +135,8 @@ impl ChannelKeys for EnforcingChannelKeys {
124135
impl Writeable for EnforcingChannelKeys {
125136
fn write<W: Writer>(&self, writer: &mut W) -> Result<(), Error> {
126137
self.inner.write(writer)?;
138+
let revoked = *self.revoked_commitment.lock().unwrap();
139+
revoked.write(writer)?;
127140
let (obscure, last) = *self.commitment_number_obscure_and_last.lock().unwrap();
128141
obscure.write(writer)?;
129142
last.write(writer)?;
@@ -134,9 +147,11 @@ impl Writeable for EnforcingChannelKeys {
134147
impl Readable for EnforcingChannelKeys {
135148
fn read<R: ::std::io::Read>(reader: &mut R) -> Result<Self, DecodeError> {
136149
let inner = Readable::read(reader)?;
150+
let revoked = Readable::read(reader)?;
137151
let obscure_and_last = Readable::read(reader)?;
138152
Ok(EnforcingChannelKeys {
139153
inner: inner,
154+
revoked_commitment: Arc::new(Mutex::new(revoked)),
140155
commitment_number_obscure_and_last: Arc::new(Mutex::new(obscure_and_last))
141156
})
142157
}

0 commit comments

Comments
 (0)