Skip to content

Commit

Permalink
Merge pull request #3264 from jkczyz/2024-08-remove-user-provided-pay…
Browse files Browse the repository at this point in the history
…er-id

Disallow user-provided `payer_signing_pubkey`
  • Loading branch information
TheBlueMatt authored Nov 12, 2024
2 parents f152689 + c331b67 commit 70add14
Show file tree
Hide file tree
Showing 13 changed files with 658 additions and 780 deletions.
7 changes: 0 additions & 7 deletions fuzz/src/chanmon_consistency.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,6 @@ use lightning::ln::msgs::{
use lightning::ln::script::ShutdownScript;
use lightning::ln::types::ChannelId;
use lightning::offers::invoice::UnsignedBolt12Invoice;
use lightning::offers::invoice_request::UnsignedInvoiceRequest;
use lightning::onion_message::messenger::{Destination, MessageRouter, OnionMessagePath};
use lightning::routing::router::{InFlightHtlcs, Path, Route, RouteHop, RouteParameters, Router};
use lightning::sign::{
Expand Down Expand Up @@ -340,12 +339,6 @@ impl NodeSigner for KeyProvider {
unreachable!()
}

fn sign_bolt12_invoice_request(
&self, _invoice_request: &UnsignedInvoiceRequest,
) -> Result<schnorr::Signature, ()> {
unreachable!()
}

fn sign_bolt12_invoice(
&self, _invoice: &UnsignedBolt12Invoice,
) -> Result<schnorr::Signature, ()> {
Expand Down
7 changes: 0 additions & 7 deletions fuzz/src/full_stack.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,6 @@ use lightning::ln::peer_handler::{
use lightning::ln::script::ShutdownScript;
use lightning::ln::types::ChannelId;
use lightning::offers::invoice::UnsignedBolt12Invoice;
use lightning::offers::invoice_request::UnsignedInvoiceRequest;
use lightning::onion_message::messenger::{Destination, MessageRouter, OnionMessagePath};
use lightning::routing::gossip::{NetworkGraph, P2PGossipSync};
use lightning::routing::router::{
Expand Down Expand Up @@ -413,12 +412,6 @@ impl NodeSigner for KeyProvider {
unreachable!()
}

fn sign_bolt12_invoice_request(
&self, _invoice_request: &UnsignedInvoiceRequest,
) -> Result<schnorr::Signature, ()> {
unreachable!()
}

fn sign_bolt12_invoice(
&self, _invoice: &UnsignedBolt12Invoice,
) -> Result<schnorr::Signature, ()> {
Expand Down
43 changes: 25 additions & 18 deletions fuzz/src/offer_deser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,15 @@
// licenses.

use crate::utils::test_logger;
use bitcoin::secp256k1::{Keypair, PublicKey, Secp256k1, SecretKey};
use bitcoin::secp256k1::Secp256k1;
use core::convert::TryFrom;
use lightning::offers::invoice_request::UnsignedInvoiceRequest;
use lightning::ln::channelmanager::PaymentId;
use lightning::ln::inbound_payment::ExpandedKey;
use lightning::offers::invoice_request::InvoiceRequest;
use lightning::offers::nonce::Nonce;
use lightning::offers::offer::{Amount, Offer, Quantity};
use lightning::offers::parse::Bolt12SemanticError;
use lightning::sign::{EntropySource, KeyMaterial};
use lightning::util::ser::Writeable;

#[inline]
Expand All @@ -22,27 +26,30 @@ pub fn do_test<Out: test_logger::Output>(data: &[u8], _out: Out) {
offer.write(&mut bytes).unwrap();
assert_eq!(data, bytes);

let secp_ctx = Secp256k1::new();
let keys = Keypair::from_secret_key(&secp_ctx, &SecretKey::from_slice(&[42; 32]).unwrap());
let pubkey = PublicKey::from(keys);
let mut buffer = Vec::new();

if let Ok(invoice_request) = build_response(&offer, pubkey) {
invoice_request
.sign(|message: &UnsignedInvoiceRequest| {
Ok(secp_ctx.sign_schnorr_no_aux_rand(message.as_ref().as_digest(), &keys))
})
.unwrap()
.write(&mut buffer)
.unwrap();
if let Ok(invoice_request) = build_request(&offer) {
invoice_request.write(&mut buffer).unwrap();
}
}
}

fn build_response(
offer: &Offer, pubkey: PublicKey,
) -> Result<UnsignedInvoiceRequest, Bolt12SemanticError> {
let mut builder = offer.request_invoice(vec![42; 64], pubkey)?;
struct FixedEntropy;

impl EntropySource for FixedEntropy {
fn get_secure_random_bytes(&self) -> [u8; 32] {
[42; 32]
}
}

fn build_request(offer: &Offer) -> Result<InvoiceRequest, Bolt12SemanticError> {
let expanded_key = ExpandedKey::new(&KeyMaterial([42; 32]));
let entropy = FixedEntropy {};
let nonce = Nonce::from_entropy_source(&entropy);
let secp_ctx = Secp256k1::new();
let payment_id = PaymentId([1; 32]);

let mut builder = offer.request_invoice(&expanded_key, nonce, &secp_ctx, payment_id)?;

builder = match offer.amount() {
None => builder.amount_msats(1000).unwrap(),
Expand All @@ -56,7 +63,7 @@ fn build_response(
Quantity::One => builder,
};

builder.build()
builder.build_and_sign()
}

pub fn offer_deser_test<Out: test_logger::Output>(data: &[u8], out: Out) {
Expand Down
7 changes: 0 additions & 7 deletions fuzz/src/onion_message.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ use lightning::ln::msgs::{self, DecodeError, OnionMessageHandler};
use lightning::ln::peer_handler::IgnoringMessageHandler;
use lightning::ln::script::ShutdownScript;
use lightning::offers::invoice::UnsignedBolt12Invoice;
use lightning::offers::invoice_request::UnsignedInvoiceRequest;
use lightning::onion_message::async_payments::{
AsyncPaymentsMessageHandler, HeldHtlcAvailable, ReleaseHeldHtlc,
};
Expand Down Expand Up @@ -234,12 +233,6 @@ impl NodeSigner for KeyProvider {
unreachable!()
}

fn sign_bolt12_invoice_request(
&self, _invoice_request: &UnsignedInvoiceRequest,
) -> Result<schnorr::Signature, ()> {
unreachable!()
}

fn sign_bolt12_invoice(
&self, _invoice: &UnsignedBolt12Invoice,
) -> Result<schnorr::Signature, ()> {
Expand Down
4 changes: 0 additions & 4 deletions lightning/src/ln/blinded_payment_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ use crate::ln::onion_utils;
use crate::ln::onion_utils::INVALID_ONION_BLINDING;
use crate::ln::outbound_payment::{Retry, IDEMPOTENCY_TIMEOUT_TICKS};
use crate::offers::invoice::UnsignedBolt12Invoice;
use crate::offers::invoice_request::UnsignedInvoiceRequest;
use crate::prelude::*;
use crate::routing::router::{BlindedTail, Path, Payee, PaymentParameters, RouteHop, RouteParameters};
use crate::sign::{KeyMaterial, NodeSigner, Recipient};
Expand Down Expand Up @@ -1540,9 +1539,6 @@ fn route_blinding_spec_test_vector() {
fn sign_invoice(
&self, _invoice: &RawBolt11Invoice, _recipient: Recipient,
) -> Result<RecoverableSignature, ()> { unreachable!() }
fn sign_bolt12_invoice_request(
&self, _invoice_request: &UnsignedInvoiceRequest,
) -> Result<schnorr::Signature, ()> { unreachable!() }
fn sign_bolt12_invoice(
&self, _invoice: &UnsignedBolt12Invoice,
) -> Result<schnorr::Signature, ()> { unreachable!() }
Expand Down
6 changes: 3 additions & 3 deletions lightning/src/ln/channelmanager.rs
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ use crate::ln::outbound_payment::{OutboundPayments, PendingOutboundPayment, Retr
use crate::ln::wire::Encode;
use crate::offers::invoice::{Bolt12Invoice, DEFAULT_RELATIVE_EXPIRY, DerivedSigningPubkey, ExplicitSigningPubkey, InvoiceBuilder, UnsignedBolt12Invoice};
use crate::offers::invoice_error::InvoiceError;
use crate::offers::invoice_request::{DerivedPayerSigningPubkey, InvoiceRequest, InvoiceRequestBuilder};
use crate::offers::invoice_request::{InvoiceRequest, InvoiceRequestBuilder};
use crate::offers::nonce::Nonce;
use crate::offers::offer::{Offer, OfferBuilder};
use crate::offers::parse::Bolt12SemanticError;
Expand Down Expand Up @@ -9632,8 +9632,8 @@ where
let secp_ctx = &self.secp_ctx;

let nonce = Nonce::from_entropy_source(entropy);
let builder: InvoiceRequestBuilder<DerivedPayerSigningPubkey, secp256k1::All> = offer
.request_invoice_deriving_signing_pubkey(expanded_key, nonce, secp_ctx, payment_id)?
let builder: InvoiceRequestBuilder<secp256k1::All> = offer
.request_invoice(expanded_key, nonce, secp_ctx, payment_id)?
.into();
let builder = builder.chain_hash(self.chain_hash)?;

Expand Down
23 changes: 13 additions & 10 deletions lightning/src/ln/outbound_payment.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2745,6 +2745,8 @@ mod tests {
let router = test_utils::TestRouter::new(network_graph, &logger, &scorer);
let secp_ctx = Secp256k1::new();
let keys_manager = test_utils::TestKeysInterface::new(&[0; 32], Network::Testnet);
let expanded_key = ExpandedKey::new(&KeyMaterial([42; 32]));
let nonce = Nonce([0; 16]);

let pending_events = Mutex::new(VecDeque::new());
let outbound_payments = OutboundPayments::new(new_hash_map());
Expand All @@ -2762,9 +2764,8 @@ mod tests {
let invoice = OfferBuilder::new(recipient_pubkey())
.amount_msats(1000)
.build().unwrap()
.request_invoice(vec![1; 32], payer_pubkey()).unwrap()
.build().unwrap()
.sign(payer_sign).unwrap()
.request_invoice(&expanded_key, nonce, &secp_ctx, payment_id).unwrap()
.build_and_sign().unwrap()
.respond_with_no_std(payment_paths(), payment_hash(), created_at).unwrap()
.build().unwrap()
.sign(recipient_sign).unwrap();
Expand Down Expand Up @@ -2801,15 +2802,16 @@ mod tests {

let pending_events = Mutex::new(VecDeque::new());
let outbound_payments = OutboundPayments::new(new_hash_map());
let expanded_key = ExpandedKey::new(&KeyMaterial([42; 32]));
let nonce = Nonce([0; 16]);
let payment_id = PaymentId([0; 32]);
let expiration = StaleExpiration::AbsoluteTimeout(Duration::from_secs(100));

let invoice = OfferBuilder::new(recipient_pubkey())
.amount_msats(1000)
.build().unwrap()
.request_invoice(vec![1; 32], payer_pubkey()).unwrap()
.build().unwrap()
.sign(payer_sign).unwrap()
.request_invoice(&expanded_key, nonce, &secp_ctx, payment_id).unwrap()
.build_and_sign().unwrap()
.respond_with_no_std(payment_paths(), payment_hash(), now()).unwrap()
.build().unwrap()
.sign(recipient_sign).unwrap();
Expand Down Expand Up @@ -2862,15 +2864,16 @@ mod tests {

let pending_events = Mutex::new(VecDeque::new());
let outbound_payments = OutboundPayments::new(new_hash_map());
let expanded_key = ExpandedKey::new(&KeyMaterial([42; 32]));
let nonce = Nonce([0; 16]);
let payment_id = PaymentId([0; 32]);
let expiration = StaleExpiration::AbsoluteTimeout(Duration::from_secs(100));

let invoice = OfferBuilder::new(recipient_pubkey())
.amount_msats(1000)
.build().unwrap()
.request_invoice(vec![1; 32], payer_pubkey()).unwrap()
.build().unwrap()
.sign(payer_sign).unwrap()
.request_invoice(&expanded_key, nonce, &secp_ctx, payment_id).unwrap()
.build_and_sign().unwrap()
.respond_with_no_std(payment_paths(), payment_hash(), now()).unwrap()
.build().unwrap()
.sign(recipient_sign).unwrap();
Expand Down Expand Up @@ -2955,7 +2958,7 @@ mod tests {
OfferBuilder::new(recipient_pubkey())
.amount_msats(1000)
.build().unwrap()
.request_invoice_deriving_signing_pubkey(&expanded_key, nonce, &secp_ctx, payment_id)
.request_invoice(&expanded_key, nonce, &secp_ctx, payment_id)
.unwrap()
.build_and_sign()
.unwrap()
Expand Down
Loading

0 comments on commit 70add14

Please sign in to comment.