dualopen and splicing: add minimal verification of tx_signatures
What changed, and why it matters
This commit adds missing safety checks in Core Lightning when a peer sends signatures for opening or splicing a dual-funded Lightning channel. Before the patch, the software accepted peer-provided transaction witnesses without verifying that they were non-empty, properly formatted, or using the required SIGHASH_ALL signature mode. It also ignored extra witness stacks that did not correspond to any input. The fix makes the node reject malformed or suspicious signatures, which helps prevent invalid or malicious transactions from being finalized and broadcast.
Apply the patch and run the new unit tests. Review any custom channel-opening or splicing flows that may rely on the previous lenient witness handling. Monitor for peer-disconnection warnings mentioning bad witnesses or mismatched counts, which may indicate attempted abuse of the prior behavior.
Security signals we found
Missing input validation on peer-provided witness data
BOLT-2 compliance fix for tx_signatures
Rejection of empty witness stacks
Rejection of non-SIGHASH_ALL signature flags
Rejection of non-standard witness shapes
Fix for previously ignored extra witness stacks
Addition of unit tests for security-critical validation
Evidence from the diff
The patch implements minimal verification of tx_signatures per BOLT-2 for dual-funded channel opening (dualopend) and splicing. New helper functions in common/psbt_internal.c parse and validate witness stacks: they reject empty witnesses, truncated or trailing-byte witness encodings, non-P2WPKH/P2WSH/P2TR inputs, oversized or over-counted P2WSH/Tapscript stack items, Taproot annexes, and signatures whose sighash flag is not SIGHASH_ALL (with P2TR key-path 64-byte signatures accepted as SIGHASH_DEFAULT). It also enforces that the number of witnesses equals the number of inputs contributed by the sending peer. channeld.c and openingd/dualopend.c now call these validators before finalizing inputs. Unit tests are added for both the per-witness and count checks.
Changed components
channeld/channeld.c (splice negotiation)openingd/dualopend.c (dual-funded channel open)common/psbt_internal.c/h (witness validation helpers)common/test/run-tx_signatures_count.ccommon/test/run-tx_signatures_witness.cInspect captured patch +633 / −9
### channeld/channeld.c
@@ -3907,6 +3907,8 @@ static void resume_splice_negotiation(struct peer *peer,
enum tx_role our_role = inflight->i_am_initiator
? TX_INITIATOR
: TX_ACCEPTER;
+ enum tx_role their_role =
+ our_role == TX_INITIATOR ? TX_ACCEPTER : TX_INITIATOR;
const u8 *wit_script;
struct channel_id cid;
enum peer_wire type;
@@ -3926,6 +3928,7 @@ static void resume_splice_negotiation(struct peer *peer,
const u8 *msg_received;
struct witness **inws;
struct bitcoin_signature *their_sig;
+ const char *err;
if (peer->splicing) {
inws = peer->splicing->inws;
@@ -4168,12 +4171,6 @@ static void resume_splice_negotiation(struct peer *peer,
splice_funding_index,
wit_script);
- if (tal_count(inws) > current_psbt->num_inputs)
- peer_failed_warn(peer->pps, &peer->channel_id,
- "%zu too many witness elements"
- " received",
- tal_count(inws) - current_psbt->num_inputs);
-
/* We put the PSBT + sigs all together */
for (size_t j = 0, i = 0; i < current_psbt->num_inputs; i++) {
struct wally_psbt_input *in =
@@ -4208,10 +4205,26 @@ static void resume_splice_negotiation(struct peer *peer,
tal_count(inws));
}
+ err = check_tx_signatures_witness(tmpctx, in, inws[j]);
+ if (err)
+ peer_failed_warn(
+ peer->pps, &peer->channel_id,
+ "Bad witness at position %zu: %s", j, err);
+
psbt_finalize_input(current_psbt, in,
inws[j++]);
}
+ /* Same checks as dualopen. The splice funding input is not
+ * in the witness array; its signature is the TLV. Extra
+ * witnesses used to be ignored if they fit under num_inputs. */
+ err = check_tx_signatures_witness_count(
+ tmpctx, current_psbt, their_role, tal_count(inws),
+ (int)splice_funding_index);
+ if (err)
+ peer_failed_warn(peer->pps, &peer->channel_id, "%s",
+ err);
+
final_tx = bitcoin_tx_with_psbt(tmpctx, current_psbt);
wit_stack = bitcoin_witness_2of2(final_tx, &splice_sig,
### common/psbt_internal.c
@@ -1,7 +1,9 @@
#include "config.h"
#include <assert.h>
#include <bitcoin/psbt.h>
+#include <bitcoin/pubkey.h>
#include <bitcoin/script.h>
+#include <ccan/tal/str/str.h>
#include <common/psbt_internal.h>
#include <common/psbt_open.h>
#include <common/utils.h>
@@ -32,6 +34,60 @@ static bool next_size(const u8 **cursor, size_t *max, size_t *size)
return true;
}
+/* If @elements is non-NULL it must hold at least as many slots as the
+ * stack claims. A failure may have written a prefix; ignore it. */
+static bool parse_witness_stack(const u8 *witness, size_t *num_items,
+ struct witness_element *elements)
+{
+ const u8 *cursor = witness;
+ size_t max = tal_bytelen(witness);
+ size_t n, item_len;
+
+ if (!next_size(&cursor, &max, &n))
+ return false;
+
+ for (size_t i = 0; i < n; i++) {
+ if (!next_size(&cursor, &max, &item_len))
+ return false;
+ if (max < item_len)
+ return false;
+ if (elements) {
+ elements[i].data = cursor;
+ elements[i].len = item_len;
+ }
+ cursor += item_len;
+ max -= item_len;
+ }
+
+ /* Encoding ends here; anything left is not a witness. */
+ if (max != 0)
+ return false;
+
+ *num_items = n;
+ return true;
+}
+
+bool witness_stack_items(const u8 *witness, size_t *num_items)
+{
+ return parse_witness_stack(witness, num_items, NULL);
+}
+
+const struct witness_element *witness_stack_elements(const tal_t *ctx,
+ const u8 *witness)
+{
+ size_t n;
+ struct witness_element *elements;
+
+ if (!parse_witness_stack(witness, &n, NULL))
+ return NULL;
+
+ /* Each item costs at least its length byte, so n fits the blob. */
+ elements = tal_arr(ctx, struct witness_element, n);
+ if (!parse_witness_stack(witness, &n, elements))
+ return tal_free(elements);
+ return elements;
+}
+
static u8 *next_script(const tal_t *ctx, const u8 **cursor, size_t *max)
{
const u8 *p;
@@ -169,3 +225,198 @@ psbt_to_witnesses(const tal_t *ctx,
return witnesses;
}
+
+/* Bitcoin Core src/policy/policy.h. Applied only to the type they belong to. */
+#define MAX_STANDARD_P2WSH_STACK_ITEMS 100
+#define MAX_STANDARD_P2WSH_STACK_ITEM_SIZE 80
+#define MAX_STANDARD_P2WSH_SCRIPT_SIZE 3600
+#define MAX_STANDARD_TAPSCRIPT_STACK_ITEM_SIZE 80
+
+/* BIP341 annex tag. BIP342 leaf version is the low 7 bits of 0xc0. */
+#define WITNESS_ANNEX_TAG 0x50
+#define TAPROOT_LEAF_MASK 0xfe
+#define TAPROOT_LEAF_TAPSCRIPT 0xc0
+
+/* A P2WPKH witness is always a signature and a compressed pubkey.
+ * No byte cap: the shape is fixed. */
+static const char *check_p2wpkh_witness(const tal_t *ctx,
+ const struct witness_element *elems)
+{
+ size_t n = tal_count(elems);
+
+ if (n != 2)
+ return tal_fmt(ctx,
+ "witness is non-standard: "
+ "P2WPKH witness has %zu items, "
+ "expected a signature and a public key",
+ n);
+ if (elems[1].len != PUBKEY_CMPR_LEN)
+ return tal_fmt(ctx,
+ "witness is non-standard: "
+ "P2WPKH public key is %zu bytes",
+ elems[1].len);
+ /* Sighash byte sits after the DER signature. No interpreter. */
+ if (elems[0].len == 0 || elems[0].data[elems[0].len - 1] != SIGHASH_ALL)
+ return tal_fmt(ctx,
+ "witness is non-standard: "
+ "P2WPKH signature is not SIGHASH_ALL");
+ return NULL;
+}
+
+/* Items below the witness script, not the script itself. */
+static const char *check_p2wsh_witness(const tal_t *ctx,
+ const struct witness_element *elems)
+{
+ size_t n = tal_count(elems);
+ size_t stack_items = n - 1;
+
+ if (elems[n - 1].len > MAX_STANDARD_P2WSH_SCRIPT_SIZE)
+ return tal_fmt(ctx,
+ "witness is non-standard: "
+ "witness script is %zu bytes",
+ elems[n - 1].len);
+
+ if (stack_items > MAX_STANDARD_P2WSH_STACK_ITEMS)
+ return tal_fmt(ctx,
+ "witness is non-standard: "
+ "%zu stack items",
+ stack_items);
+
+ for (size_t i = 0; i < stack_items; i++) {
+ if (elems[i].len > MAX_STANDARD_P2WSH_STACK_ITEM_SIZE)
+ return tal_fmt(ctx,
+ "witness is non-standard: "
+ "stack item %zu is %zu bytes",
+ i, elems[i].len);
+ }
+ /* FIXME: no SIGHASH_ALL check. Which stack items are signatures
+ * depends on the witness script, so this needs an interpreter. */
+ return NULL;
+}
+
+/* Key path has no size policy. Tapscript limits the items under the
+ * script; an annex is non-standard. */
+static const char *check_p2tr_witness(const tal_t *ctx,
+ const struct witness_element *elems)
+{
+ size_t n = tal_count(elems);
+ const struct witness_element *control;
+
+ if (n >= 2 && elems[n - 1].len > 0
+ && elems[n - 1].data[0] == WITNESS_ANNEX_TAG)
+ return tal_fmt(ctx, "witness is non-standard: taproot annex");
+
+ /* Key path. 64 bytes omits the sighash: that is SIGHASH_DEFAULT,
+ * which is SIGHASH_ALL. 65 bytes must end in SIGHASH_ALL. */
+ if (n == 1) {
+ const struct witness_element *sig = &elems[0];
+
+ if (sig->len == 64
+ || (sig->len == 65 && sig->data[64] == SIGHASH_ALL))
+ return NULL;
+ return tal_fmt(ctx,
+ "witness is non-standard: "
+ "P2TR key path signature is not SIGHASH_ALL");
+ }
+
+ if (n < 2)
+ return NULL;
+
+ /* FIXME: no SIGHASH_ALL check on a script-path spend. Which stack
+ * items are signatures depends on the leaf script. */
+
+ control = &elems[n - 1];
+ if (control->len == 0)
+ return tal_fmt(ctx,
+ "witness is non-standard: "
+ "empty taproot control block");
+ if ((control->data[0] & TAPROOT_LEAF_MASK) != TAPROOT_LEAF_TAPSCRIPT)
+ return NULL;
+
+ for (size_t i = 0; i + 2 < n; i++) {
+ if (elems[i].len > MAX_STANDARD_TAPSCRIPT_STACK_ITEM_SIZE)
+ return tal_fmt(ctx,
+ "witness is non-standard: "
+ "tapscript stack item %zu is %zu bytes",
+ i, elems[i].len);
+ }
+ return NULL;
+}
+
+/* Our inputs carry witness_utxo. Peer inputs carry the prevtx in utxo. */
+static const u8 *spent_scriptpubkey(const struct wally_psbt_input *in,
+ size_t *script_len)
+{
+ if (in->witness_utxo && in->witness_utxo->script) {
+ *script_len = in->witness_utxo->script_len;
+ return in->witness_utxo->script;
+ }
+ if (in->utxo && in->index < in->utxo->num_outputs
+ && in->utxo->outputs[in->index].script) {
+ *script_len = in->utxo->outputs[in->index].script_len;
+ return in->utxo->outputs[in->index].script;
+ }
+ *script_len = 0;
+ return NULL;
+}
+
+const char *check_tx_signatures_witness(const tal_t *ctx,
+ const struct wally_psbt_input *in,
+ const struct witness *witness)
+{
+ const struct witness_element *elems;
+ const u8 *script;
+ size_t script_len;
+
+ if (!witness || !witness->witness_data ||
+ tal_count(witness->witness_data) == 0)
+ return tal_fmt(ctx, "witness is empty");
+
+ elems = witness_stack_elements(ctx, witness->witness_data);
+ if (!elems)
+ return tal_fmt(ctx, "witness is truncated or has trailing bytes");
+
+ if (tal_count(elems) == 0)
+ return tal_fmt(ctx, "witness is empty");
+
+ script = spent_scriptpubkey(in, &script_len);
+ if (script && is_p2wpkh(script, script_len, NULL))
+ return check_p2wpkh_witness(ctx, elems);
+ if (script && is_p2wsh(script, script_len, NULL))
+ return check_p2wsh_witness(ctx, elems);
+ if (script && is_p2tr(script, script_len, NULL))
+ return check_p2tr_witness(ctx, elems);
+
+ return tal_fmt(ctx,
+ "witness is non-standard: "
+ "input is not P2WPKH, P2WSH or P2TR");
+}
+
+const char *check_tx_signatures_witness_count(const tal_t *ctx,
+ const struct wally_psbt *psbt,
+ enum tx_role their_role,
+ size_t num_witnesses,
+ int input_index_to_ignore)
+{
+ size_t peer_inputs = 0;
+
+ for (size_t i = 0; i < psbt->num_inputs; i++) {
+ u64 in_serial;
+
+ if (input_index_to_ignore >= 0
+ && i == (size_t)input_index_to_ignore)
+ continue;
+ if (!psbt_get_serial_id(&psbt->inputs[i].unknowns, &in_serial))
+ continue;
+ if (in_serial % 2 != their_role)
+ continue;
+ peer_inputs++;
+ }
+
+ if (peer_inputs != num_witnesses)
+ return tal_fmt(ctx,
+ "Mismatched witness stack count, expecting %zu, "
+ "got %zu",
+ peer_inputs, num_witnesses);
+ return NULL;
+}
### common/psbt_internal.h
@@ -2,13 +2,30 @@
#define LIGHTNING_COMMON_PSBT_INTERNAL_H
#include "config.h"
+#include <ccan/short_types/short_types.h>
#include <ccan/tal/tal.h>
#include <common/tx_roles.h>
struct wally_psbt;
struct wally_psbt_input;
struct witness;
+/* One element of a witness stack. @data points into the witness blob. */
+struct witness_element {
+ const u8 *data;
+ size_t len;
+};
+
+/* True iff witness is a complete witness stack: item count, that many
+ * items, no trailing bytes. @num_items is set only on success. */
+bool witness_stack_items(const u8 *witness, size_t *num_items);
+
+/* Elements of a complete witness stack, pointing into @witness.
+ * NULL if truncated or trailing bytes remain. An empty stack is a
+ * zero-length array, not NULL. */
+const struct witness_element *witness_stack_elements(const tal_t *ctx,
+ const u8 *witness);
+
/* psbt_finalize_input - Finalize an input with a given witness stack
*
* Sets the given witness elements onto the PSBT. Also finalizes
@@ -21,6 +38,22 @@ void psbt_finalize_input(const tal_t *ctx,
struct wally_psbt_input *in,
const struct witness *witness);
+/* Minimal tx_signatures witness check, shared by dual open and splice.
+ * NULL if acceptable. */
+const char *check_tx_signatures_witness(const tal_t *ctx,
+ const struct wally_psbt_input *in,
+ const struct witness *witness);
+
+/* The number of witnesses must equal the inputs added by the sending
+ * node. @input_index_to_ignore is not counted (the splice funding
+ * input, whose signature is not in the witness array). Pass -1 to
+ * count every input of @their_role. */
+const char *check_tx_signatures_witness_count(const tal_t *ctx,
+ const struct wally_psbt *psbt,
+ enum tx_role their_role,
+ size_t num_witnesses,
+ int input_index_to_ignore);
+
/* psbt_to_witnesses - Take a side's sigs from a PSBT and copy to a
* wire witness
*
### common/test/Makefile
@@ -11,6 +11,11 @@ COMMON_TEST_COMMON_OBJS := \
common/utils.o
$(COMMON_TEST_PROGRAMS): $(COMMON_TEST_COMMON_OBJS) $(BITCOIN_OBJS)
+
+# These call into psbt_internal, which is not #included.
+common/test/run-tx_signatures_witness \
+common/test/run-tx_signatures_count: \
+ libcommon.a
$(COMMON_TEST_OBJS): $(COMMON_HEADERS) $(WIRE_HEADERS) $(COMMON_SRC) common/test/Makefile
ALL_C_SOURCES += $(COMMON_TEST_SRC)
### common/test/run-tx_signatures_count.c
@@ -0,0 +1,103 @@
+#include "config.h"
+#include <bitcoin/psbt.h>
+#include <bitcoin/tx.h>
+#include <common/psbt_internal.h>
+#include <common/psbt_open.h>
+#include <common/setup.h>
+#include <common/utils.h>
+#include <stdio.h>
+#include <string.h>
+
+static struct wally_psbt *psbt_with_serials(const tal_t *ctx,
+ const u64 *serials,
+ size_t n)
+{
+ struct wally_psbt *psbt = create_psbt(ctx, 0, 0, 0);
+
+ for (size_t i = 0; i < n; i++) {
+ struct bitcoin_outpoint outpoint;
+
+ memset(&outpoint, 0, sizeof(outpoint));
+ outpoint.n = i;
+ psbt_append_input(psbt, &outpoint, BITCOIN_TX_RBF_SEQUENCE,
+ NULL, NULL, NULL);
+ psbt_input_set_serial_id(psbt, &psbt->inputs[i], serials[i]);
+ }
+ return psbt;
+}
+
+static void expect_count(const char *what,
+ const u64 *serials,
+ size_t ninputs,
+ enum tx_role role,
+ size_t num_witnesses,
+ const char *needle)
+{
+ struct wally_psbt *psbt = psbt_with_serials(tmpctx, serials, ninputs);
+ const char *err = check_tx_signatures_witness_count(tmpctx, psbt, role,
+ num_witnesses, -1);
+
+ if (needle) {
+ if (!err || !strstr(err, needle)) {
+ fprintf(stderr, "%s: expected '%s', got '%s'\n",
+ what, needle, err ? err : "(accepted)");
+ abort();
+ }
+ } else if (err) {
+ fprintf(stderr, "%s: unexpected reject: %s\n", what, err);
+ abort();
+ }
+}
+
+/* Extra witnesses used to be ignored. Peer serials are odd. */
+static void test_witness_count(void)
+{
+ const u64 one_peer[] = { 1 };
+ const u64 ours_and_peer[] = { 0, 1 };
+ const u64 two_peer[] = { 1, 3 };
+
+ expect_count("matching count", one_peer, 1, TX_ACCEPTER, 1, NULL);
+ expect_count("extra witness", one_peer, 1, TX_ACCEPTER, 2,
+ "expecting 1, got 2");
+ expect_count("too few witnesses", one_peer, 1, TX_ACCEPTER, 0,
+ "expecting 1, got 0");
+ expect_count("our input is not counted", ours_and_peer, 2,
+ TX_ACCEPTER, 1, NULL);
+ expect_count("two peer inputs, one witness", two_peer, 2,
+ TX_ACCEPTER, 1, "expecting 2, got 1");
+}
+
+/* Splice does not put the shared funding input in the witness array. */
+static void test_ignored_funding_input(void)
+{
+ const u64 serials[] = { 1, 3 };
+ struct wally_psbt *psbt = psbt_with_serials(tmpctx, serials, 2);
+ const char *err;
+
+ err = check_tx_signatures_witness_count(tmpctx, psbt, TX_ACCEPTER,
+ 1, 0);
+ if (err) {
+ fprintf(stderr, "ignored funding input: unexpected reject: %s\n",
+ err);
+ abort();
+ }
+ err = check_tx_signatures_witness_count(tmpctx, psbt, TX_ACCEPTER,
+ 2, 0);
+ if (!err || !strstr(err, "expecting 1, got 2")) {
+ fprintf(stderr, "ignored funding input: expected mismatch, got '%s'\n",
+ err ? err : "(accepted)");
+ abort();
+ }
+}
+
+int main(int argc, char *argv[])
+{
+ common_setup(argv[0]);
+ chainparams = chainparams_for_network("bitcoin");
+
+ test_witness_count();
+ test_ignored_funding_input();
+
+ common_shutdown();
+ return 0;
+}
### common/test/run-tx_signatures_witness.c
@@ -0,0 +1,190 @@
+#include "config.h"
+#include <assert.h>
+#include <bitcoin/psbt.h>
+#include <bitcoin/pubkey.h>
+#include <bitcoin/signature.h>
+#include <common/psbt_internal.h>
+#include <common/setup.h>
+#include <common/utils.h>
+#include <stdio.h>
+#include <string.h>
+#include <wally_psbt.h>
+#include <wire/peer_wire.h>
+
+static struct witness *witness_raw(const tal_t *ctx, const void *data, size_t n)
+{
+ struct witness *w = tal(ctx, struct witness);
+
+ w->witness_data = tal_dup_arr(w, u8, data, n, 0);
+ return w;
+}
+
+static struct witness *witness_items(const tal_t *ctx,
+ const u8 *const *items,
+ const size_t *lens,
+ size_t n)
+{
+ struct witness *w = tal(ctx, struct witness);
+
+ w->witness_data = tal_arr(w, u8, 0);
+ add_varint(&w->witness_data, n);
+ for (size_t i = 0; i < n; i++) {
+ add_varint(&w->witness_data, lens[i]);
+ if (lens[i])
+ tal_expand(&w->witness_data, items[i], lens[i]);
+ }
+ return w;
+}
+
+/* OP_0 PUSH20 <20 bytes>. */
+static const u8 p2wpkh_spk[] = {
+ 0x00, 0x14,
+ 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11,
+ 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11
+};
+
+static struct witness *p2wpkh_witness(const tal_t *ctx)
+{
+ static const u8 sig[] = { 0x30, 0x01 };
+ static const u8 pk[PUBKEY_CMPR_LEN] = { 0x02 };
+ const u8 *items[] = { sig, pk };
+ size_t lens[] = { sizeof(sig), sizeof(pk) };
+
+ return witness_items(ctx, items, lens, 2);
+}
+
+static struct wally_psbt_input *input_for_script(const tal_t *ctx,
+ const u8 *script,
+ size_t script_len)
+{
+ struct wally_psbt_input *in = talz(ctx, struct wally_psbt_input);
+ struct wally_tx_output *out;
+
+ tal_wally_start();
+ assert(wally_tx_output_init_alloc(1000, script, script_len, &out)
+ == WALLY_OK);
+ tal_wally_end(in);
+ in->witness_utxo = out;
+ return in;
+}
+
+static void expect_reject(const char *what,
+ const struct wally_psbt_input *in,
+ const struct witness *witness,
+ const char *needle)
+{
+ const char *err = check_tx_signatures_witness(tmpctx, in, witness);
+
+ if (!err || !strstr(err, needle)) {
+ fprintf(stderr, "%s: expected '%s', got '%s'\n",
+ what, needle, err ? err : "(accepted)");
+ abort();
+ }
+}
+
+static void expect_ok(const char *what,
+ const struct wally_psbt_input *in,
+ const struct witness *witness)
+{
+ const char *err = check_tx_signatures_witness(tmpctx, in, witness);
+
+ if (err) {
+ fprintf(stderr, "%s: unexpected reject: %s\n", what, err);
+ abort();
+ }
+}
+
+static void test_witness_cases(void)
+{
+ struct wally_psbt_input *in;
+ struct witness *empty, *zero, *trunc_count, *trunc_item, *ok;
+ u8 *spk;
+ static const u8 trunc_count_raw[] = { 0x02, 0x00 };
+ static const u8 trunc_item_raw[] = { 0x01, 0x05, 0xaa };
+ static const u8 zero_raw[] = { 0x00 };
+
+ spk = tal_dup_arr(tmpctx, u8, p2wpkh_spk, sizeof(p2wpkh_spk), 0);
+ in = input_for_script(tmpctx, spk, tal_count(spk));
+
+ expect_reject("null witness", in, NULL, "witness is empty");
+
+ empty = tal(tmpctx, struct witness);
+ empty->witness_data = NULL;
+ expect_reject("null witness_data", in, empty, "witness is empty");
+
+ empty->witness_data = tal_arr(empty, u8, 0);
+ expect_reject("empty witness", in, empty, "witness is empty");
+
+ zero = witness_raw(tmpctx, zero_raw, sizeof(zero_raw));
+ expect_reject("zero-count witness", in, zero, "witness is empty");
+
+ trunc_count = witness_raw(tmpctx, trunc_count_raw, sizeof(trunc_count_raw));
+ expect_reject("truncated count", in, trunc_count,
+ "truncated or has trailing bytes");
+
+ trunc_item = witness_raw(tmpctx, trunc_item_raw, sizeof(trunc_item_raw));
+ expect_reject("truncated item", in, trunc_item,
+ "truncated or has trailing bytes");
+
+ ok = p2wpkh_witness(tmpctx);
+ expect_ok("p2wpkh witness", in, ok);
+}
+
+/* OP_1 PUSH32 <32 bytes>. */
+static const u8 p2tr_spk[] = {
+ 0x51, 0x20,
+ 0x22, 0x22, 0x22, 0x22, 0x22, 0x22, 0x22, 0x22,
+ 0x22, 0x22, 0x22, 0x22, 0x22, 0x22, 0x22, 0x22,
+ 0x22, 0x22, 0x22, 0x22, 0x22, 0x22, 0x22, 0x22,
+ 0x22, 0x22, 0x22, 0x22, 0x22, 0x22, 0x22, 0x22
+};
+
+static struct witness *one_item(const tal_t *ctx, const u8 *item, size_t len)
+{
+ const u8 *items[] = { item };
+ size_t lens[] = { len };
+
+ return witness_items(ctx, items, lens, 1);
+}
+
+static void test_sighash(void)
+{
+ struct wally_psbt_input *p2wpkh, *p2tr;
+ u8 *spk;
+ u8 bad_sig[] = { 0x30, 0x03 };
+ u8 keypath64[64] = { 0 };
+ u8 keypath65[65] = { 0 };
+ static const u8 pk[PUBKEY_CMPR_LEN] = { 0x02 };
+ const u8 *bad_items[] = { bad_sig, pk };
+ size_t bad_lens[] = { sizeof(bad_sig), sizeof(pk) };
+
+ spk = tal_dup_arr(tmpctx, u8, p2wpkh_spk, sizeof(p2wpkh_spk), 0);
+ p2wpkh = input_for_script(tmpctx, spk, tal_count(spk));
+ expect_reject("p2wpkh sighash none", p2wpkh,
+ witness_items(tmpctx, bad_items, bad_lens, 2),
+ "not SIGHASH_ALL");
+
+ spk = tal_dup_arr(tmpctx, u8, p2tr_spk, sizeof(p2tr_spk), 0);
+ p2tr = input_for_script(tmpctx, spk, tal_count(spk));
+ expect_ok("p2tr default sighash", p2tr,
+ one_item(tmpctx, keypath64, sizeof(keypath64)));
+ keypath65[64] = SIGHASH_ALL;
+ expect_ok("p2tr explicit sighash all", p2tr,
+ one_item(tmpctx, keypath65, sizeof(keypath65)));
+ keypath65[64] = 0x03;
+ expect_reject("p2tr sighash single", p2tr,
+ one_item(tmpctx, keypath65, sizeof(keypath65)),
+ "not SIGHASH_ALL");
+}
+
+int main(int argc, char *argv[])
+{
+ common_setup(argv[0]);
+ chainparams = chainparams_for_network("bitcoin");
+
+ test_witness_cases();
+ test_sighash();
+
+ common_shutdown();
+ return 0;
+}
### openingd/dualopend.c
@@ -1367,6 +1367,8 @@ static void handle_tx_sigs(struct state *state, const u8 *msg)
struct bitcoin_txid txid;
const struct witness **witnesses;
struct tx_state *tx_state = state->tx_state;
+ size_t peer_input_count = 0;
+ const char *err;
struct tlv_tx_signatures_tlvs *txsig_tlvs = tlv_tx_signatures_tlvs_new(tmpctx);
if (!fromwire_tx_signatures(tmpctx, msg, &cid, &txid,
@@ -1423,8 +1425,18 @@ static void handle_tx_sigs(struct state *state, const u8 *msg)
"tx_signatures sent before commitment sigs %s",
tal_hex(msg, msg));
+ /* BOLT #2:
+ * The receiving node:
+ * - MUST fail the negotiation if:
+ * - the message contains an empty `witness`
+ * - the number of `witnesses` does not equal the number of inputs
+ * added by the sending node
+ * - the `txid` does not match the txid of the transaction
+ * - the `witnesses` are non-standard
+ * - a signature uses a flag that is not `SIGHASH_ALL` (0x01)
+ */
/* We put the PSBT + sigs all together */
- for (size_t i = 0, j = 0; i < tx_state->psbt->num_inputs; i++) {
+ for (size_t i = 0, j; i < tx_state->psbt->num_inputs; i++) {
struct wally_psbt_input *in =
&tx_state->psbt->inputs[i];
u64 in_serial;
@@ -1437,15 +1449,32 @@ static void handle_tx_sigs(struct state *state, const u8 *msg)
}
if (in_serial % 2 != their_role(state))
continue;
+ peer_input_count++;
- if (j == tal_count(witnesses))
+ if (peer_input_count > tal_count(witnesses))
open_err_warn(state, "Mismatched witness stack count %s",
tal_hex(msg, msg));
+ j = peer_input_count - 1;
+
+ /* do some checks before handing the witness to wally */
+ err = check_tx_signatures_witness(tmpctx, in, witnesses[j]);
+ if (err)
+ open_err_warn(state,
+ "Bad witness at position %zu: %s. "
+ "Received tx_signatures: %s",
+ j, err, tal_hex(msg, msg));
+
psbt_finalize_input(tx_state->psbt, in, witnesses[j]);
- j++;
}
+ err = check_tx_signatures_witness_count(tmpctx, tx_state->psbt,
+ their_role(state),
+ tal_count(witnesses), -1);
+ if (err)
+ open_err_warn(state, "%s. Received tx_signatures: %s",
+ err, tal_hex(msg, msg));
+
tx_state->remote_funding_sigs_rcvd = true;
/* Send to the controller, who will broadcast the funding_tx
* as soon as we've got our sigs */Why this scored 59/100
Community notes
Notes can correct, qualify, or add evidence to the AI analysis. Every note shown here has been validated by a human moderator.
The AI analysis stands alone for now. Submit a note if you can add evidence or important context.