-
Notifications
You must be signed in to change notification settings - Fork 407
WIP: eliminate leak of node secret key from KeysInterface #1362
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
devrandom
wants to merge
1
commit into
lightningdevkit:main
from
lightning-signer:2022-03-protocol-encryptor
Closed
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -25,8 +25,9 @@ use bitcoin::hashes::sha256::Hash as Sha256; | |
use bitcoin::hashes::sha256d::Hash as Sha256dHash; | ||
use bitcoin::hash_types::WPubkeyHash; | ||
|
||
use bitcoin::secp256k1::ecdh::SharedSecret; | ||
use bitcoin::secp256k1::key::{SecretKey, PublicKey}; | ||
use bitcoin::secp256k1::{Secp256k1, Signature, Signing}; | ||
use bitcoin::secp256k1::{Secp256k1, Signature, Signing, All, SignOnly}; | ||
use bitcoin::secp256k1::recovery::RecoverableSignature; | ||
use bitcoin::secp256k1; | ||
|
||
|
@@ -37,7 +38,7 @@ use util::ser::{Writeable, Writer, Readable, ReadableArgs}; | |
use chain::transaction::OutPoint; | ||
use ln::{chan_utils, PaymentPreimage}; | ||
use ln::chan_utils::{HTLCOutputInCommitment, make_funding_redeemscript, ChannelPublicKeys, HolderCommitmentTransaction, ChannelTransactionParameters, CommitmentTransaction, ClosingTransaction}; | ||
use ln::msgs::UnsignedChannelAnnouncement; | ||
use ln::msgs::{UnsignedChannelAnnouncement, UnsignedChannelUpdate, UnsignedNodeAnnouncement}; | ||
use ln::script::ShutdownScript; | ||
|
||
use prelude::*; | ||
|
@@ -392,16 +393,60 @@ pub enum Recipient { | |
PhantomNode, | ||
} | ||
|
||
/// | ||
pub trait SharedSecretProduce: Send + Sync { | ||
/// | ||
fn public_key(&self, secp_ctx: &secp256k1::Secp256k1<SignOnly>) -> PublicKey; | ||
/// | ||
fn shared_secret(&self, other: &PublicKey) -> SharedSecret; | ||
/// | ||
fn do_clone(&self) -> Box<dyn SharedSecretProduce>; | ||
} | ||
|
||
/// | ||
pub struct EphemeralSharedSecretProducer { | ||
/// | ||
pub secret_key: SecretKey | ||
} | ||
|
||
impl SharedSecretProduce for EphemeralSharedSecretProducer { | ||
fn public_key(&self, secp_ctx: &Secp256k1<SignOnly>) -> PublicKey { | ||
PublicKey::from_secret_key(&secp_ctx, &self.secret_key) | ||
} | ||
|
||
fn shared_secret(&self, other: &PublicKey) -> SharedSecret { | ||
SharedSecret::new(other, &self.secret_key) | ||
} | ||
|
||
fn do_clone(&self) -> Box<dyn SharedSecretProduce> { | ||
Box::new(Self { secret_key: self.secret_key }) | ||
} | ||
} | ||
|
||
impl EphemeralSharedSecretProducer { | ||
/// | ||
pub fn new(secret_key: SecretKey) -> Box<dyn SharedSecretProduce> { | ||
Box::new( Self { secret_key }) | ||
} | ||
} | ||
|
||
/// A trait to describe an object which can get user secrets and key material. | ||
pub trait KeysInterface { | ||
/// A type which implements Sign which will be returned by get_channel_signer. | ||
type Signer : Sign; | ||
|
||
/// Get node secret key (aka node_id or network_key) based on the provided [`Recipient`]. | ||
/// A shared secret producer using the node key | ||
fn get_shared_secret_producer(&self) -> Box<dyn SharedSecretProduce>; | ||
|
||
/// ECDH | ||
fn shared_secret(&self, recipient: Recipient, other: &PublicKey) -> Result<SharedSecret, ()>; | ||
|
||
/// Get node public key (AKA node ID) | ||
/// | ||
/// This method must return the same value each time it is called with a given `Recipient` | ||
/// parameter. | ||
fn get_node_secret(&self, recipient: Recipient) -> Result<SecretKey, ()>; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The goal is to eliminate this function. |
||
fn get_node_key(&self, recipient: Recipient, secp_ctx: &Secp256k1<secp256k1::All>) -> Result<PublicKey, ()>; | ||
|
||
/// Get a script pubkey which we send funds to when claiming on-chain contestable outputs. | ||
/// | ||
/// This method should return a different value each time it is called, to avoid linking | ||
|
@@ -441,6 +486,12 @@ pub trait KeysInterface { | |
/// The secret key used to sign the invoice is dependent on the [`Recipient`]. | ||
fn sign_invoice(&self, hrp_bytes: &[u8], invoice_data: &[u5], receipient: Recipient) -> Result<RecoverableSignature, ()>; | ||
|
||
/// Sign a node announcement | ||
fn sign_node_announcement(&self, msg: &UnsignedNodeAnnouncement, secp_ctx: &Secp256k1<secp256k1::All>) -> Result<Signature, ()>; | ||
|
||
/// Sign a channel update | ||
fn sign_channel_update(&self, msg: &UnsignedChannelUpdate, secp_ctx: &Secp256k1<secp256k1::All>) -> Result<Signature, ()>; | ||
|
||
/// Get secret key material as bytes for use in encrypting and decrypting inbound payment data. | ||
/// | ||
/// If the implementor of this trait supports [phantom node payments], then every node that is | ||
|
@@ -1118,17 +1169,21 @@ impl KeysManager { | |
|
||
Ok(spend_tx) | ||
} | ||
} | ||
|
||
impl KeysInterface for KeysManager { | ||
type Signer = InMemorySigner; | ||
|
||
fn get_node_secret(&self, recipient: Recipient) -> Result<SecretKey, ()> { | ||
match recipient { | ||
Recipient::Node => Ok(self.node_secret.clone()), | ||
Recipient::PhantomNode => Err(()) | ||
} | ||
} | ||
} | ||
|
||
impl KeysInterface for KeysManager { | ||
type Signer = InMemorySigner; | ||
|
||
fn get_shared_secret_producer(&self) -> Box<dyn SharedSecretProduce> { | ||
EphemeralSharedSecretProducer::new(self.node_secret) | ||
} | ||
|
||
fn get_inbound_payment_key_material(&self) -> KeyMaterial { | ||
self.inbound_payment_key.clone() | ||
|
@@ -1169,12 +1224,30 @@ impl KeysInterface for KeysManager { | |
|
||
fn sign_invoice(&self, hrp_bytes: &[u8], invoice_data: &[u5], recipient: Recipient) -> Result<RecoverableSignature, ()> { | ||
let preimage = construct_invoice_preimage(&hrp_bytes, &invoice_data); | ||
let secret = match recipient { | ||
Recipient::Node => self.get_node_secret(Recipient::Node)?, | ||
Recipient::PhantomNode => return Err(()), | ||
}; | ||
let secret = self.get_node_secret(recipient)?; | ||
Ok(self.secp_ctx.sign_recoverable(&hash_to_message!(&Sha256::hash(&preimage)), &secret)) | ||
} | ||
|
||
fn sign_node_announcement(&self, msg: &UnsignedNodeAnnouncement, secp_ctx: &Secp256k1<All>) -> Result<Signature, ()> { | ||
let msghash = hash_to_message!(&Sha256dHash::hash(&msg.encode()[..])[..]); | ||
Ok(secp_ctx.sign(&msghash, &self.node_secret)) | ||
|
||
} | ||
|
||
fn sign_channel_update(&self, msg: &UnsignedChannelUpdate, secp_ctx: &Secp256k1<All>) -> Result<Signature, ()> { | ||
let msghash = hash_to_message!(&Sha256dHash::hash(&msg.encode()[..])[..]); | ||
Ok(secp_ctx.sign(&msghash, &self.node_secret)) | ||
} | ||
|
||
fn shared_secret(&self, recipient: Recipient, other: &PublicKey) -> Result<SharedSecret, ()> { | ||
let secret = self.get_node_secret(recipient)?; | ||
Ok(SharedSecret::new(other, &secret)) | ||
} | ||
|
||
fn get_node_key(&self, recipient: Recipient, secp_ctx: &Secp256k1<All>) -> Result<PublicKey, ()> { | ||
let secret = self.get_node_secret(recipient)?; | ||
Ok(PublicKey::from_secret_key(&secp_ctx, &secret)) | ||
} | ||
} | ||
|
||
/// Similar to [`KeysManager`], but allows the node using this struct to receive phantom node | ||
|
@@ -1207,11 +1280,8 @@ pub struct PhantomKeysManager { | |
impl KeysInterface for PhantomKeysManager { | ||
type Signer = InMemorySigner; | ||
|
||
fn get_node_secret(&self, recipient: Recipient) -> Result<SecretKey, ()> { | ||
match recipient { | ||
Recipient::Node => self.inner.get_node_secret(Recipient::Node), | ||
Recipient::PhantomNode => Ok(self.phantom_secret.clone()), | ||
} | ||
fn get_shared_secret_producer(&self) -> Box<dyn SharedSecretProduce> { | ||
EphemeralSharedSecretProducer::new(self.inner.node_secret) | ||
} | ||
|
||
fn get_inbound_payment_key_material(&self) -> KeyMaterial { | ||
|
@@ -1243,6 +1313,23 @@ impl KeysInterface for PhantomKeysManager { | |
let secret = self.get_node_secret(recipient)?; | ||
Ok(self.inner.secp_ctx.sign_recoverable(&hash_to_message!(&Sha256::hash(&preimage)), &secret)) | ||
} | ||
|
||
fn sign_node_announcement(&self, msg: &UnsignedNodeAnnouncement, secp_ctx: &Secp256k1<All>) -> Result<Signature, ()> { | ||
self.inner.sign_node_announcement(msg, secp_ctx) | ||
} | ||
|
||
fn sign_channel_update(&self, msg: &UnsignedChannelUpdate, secp_ctx: &Secp256k1<All>) -> Result<Signature, ()> { | ||
self.inner.sign_channel_update(msg, secp_ctx) | ||
} | ||
|
||
fn shared_secret(&self, recipient: Recipient, other: &PublicKey) -> Result<SharedSecret, ()> { | ||
let secret = self.get_node_secret(recipient)?; | ||
Ok(SharedSecret::new(other, &secret)) | ||
} | ||
|
||
fn get_node_key(&self, recipient: Recipient, secp_ctx: &Secp256k1<All>) -> Result<PublicKey, ()> { | ||
Ok(PublicKey::from_secret_key(&secp_ctx, &self.get_node_secret(recipient)?)) | ||
} | ||
} | ||
|
||
impl PhantomKeysManager { | ||
|
@@ -1275,6 +1362,13 @@ impl PhantomKeysManager { | |
pub fn derive_channel_keys(&self, channel_value_satoshis: u64, params: &[u8; 32]) -> InMemorySigner { | ||
self.inner.derive_channel_keys(channel_value_satoshis, params) | ||
} | ||
|
||
pub(crate) fn get_node_secret(&self, recipient: Recipient) -> Result<SecretKey, ()> { | ||
match recipient { | ||
Recipient::Node => Ok(self.inner.node_secret.clone()), | ||
Recipient::PhantomNode => Ok(self.phantom_secret.clone()) | ||
} | ||
} | ||
} | ||
|
||
// Ensure that BaseSign can have a vtable | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not quite sure I understand the need for this trait here, and super not a fan of box-dyn in our interface.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The trait replaces
SecretKey
in several places. It seems particularly necessary inDirectionalNoiseState
, so that low-level protocol enum doesn't get exposed to the fullKeysInterface
.The
Box<dyn>
was expedient for the draft - a generic would propagate into a lot of peer protocol structs.