Prevent registering policies with too many internal key expressions
What changed, and why it matters
This commit adds a safety check in Ledger's Bitcoin app so that users cannot register wallet policies that the device would later be unable to sign for. Specifically, it rejects policies containing more than the supported number of internal key expressions. Previously, such policies could be registered but signing would fail later; now registration itself is blocked with a clear error code.
No immediate action required for users; the change is defensive. Developers should ensure MAX_INTERNAL_KEY_EXPRESSIONS is documented and consistent with signing limits, and consider whether similar pre-registration checks are needed for other unsupported policy features.
Security signals we found
New error code for unsupported policy registration
Validation moved earlier in workflow to prevent registering unusable wallets
Counting of internal key expressions across normal and MuSig key expressions
Test cases added for descriptor templates exceeding the internal key expression limit
Evidence from the diff
The patch extends the wallet policy registration handler to count all key expressions that contain at least one internal key (including normal key expressions and MuSig aggregates). If the count exceeds MAX_INTERNAL_KEY_EXPRESSIONS, registration is rejected with a new error code EC_REGISTER_WALLET_TOO_MANY_INTERNAL_KEY_EXPRESSIONS (0x0004). The existing MuSig multi-internal-key check is folded into the same validation routine. A Python test verifies rejection for several descriptor templates exceeding the limit.
Changed components
src/handler/register_wallet.csrc/error_codes.htests/test_register_wallet.pyInspect captured patch +90 / −16
### src/error_codes.h
@@ -21,6 +21,9 @@
// A musig() key expression in the wallet policy has more than one internal key.
#define EC_REGISTER_WALLET_MUSIG_WITH_MULTIPLE_INTERNAL_KEYS 0x0003
+// The wallet policy has more key expressions with internal keys than supported for signing.
+#define EC_REGISTER_WALLET_TOO_MANY_INTERNAL_KEY_EXPRESSIONS 0x0004
+
/**
* SIGN_PSBT
*/
### src/handler/register_wallet.c
@@ -46,7 +46,8 @@
static bool is_policy_acceptable(const policy_node_t *policy);
static bool is_policy_name_acceptable(const char *name, size_t name_len);
-static bool has_musig_with_multiple_internal_keys(
+static bool check_policy_is_signable(
+ dispatcher_context_t *dc,
const policy_node_t *policy,
const key_type_e keys_type[static MAX_N_KEYS_IN_WALLET_POLICY]);
@@ -173,10 +174,8 @@ __attribute__((noinline)) static void confirm_and_register_wallet(
return;
}
- // MuSig2 signing produces at most one partial signature per musig() key expression, so a
- // musig() would be tricky to spend: we reject it instead. Not useful anyway.
- if (has_musig_with_multiple_internal_keys(policy, keys_type)) {
- SEND_SW_EC(dc, SW_NOT_SUPPORTED, EC_REGISTER_WALLET_MUSIG_WITH_MULTIPLE_INTERNAL_KEYS);
+ // Make sure not to register policies that we would later be unable to sign for
+ if (!check_policy_is_signable(dc, policy, keys_type)) {
return;
}
@@ -347,32 +346,63 @@ static bool is_policy_name_acceptable(const char *name, size_t name_len) {
}
/**
- * Returns true if any musig() key expression of the policy has more than one internal key.
+ * Performs the checks needed to make sure that the device will later be able to sign for the
+ * policy, beyond its validity. Rejects the policy if:
+ * - a musig() key expression has more than one internal key: MuSig2 signing produces at most one
+ * partial signature per key expression, so such a musig() would be tricky to spend (and it is
+ * not useful anyway);
+ * - there are more than MAX_INTERNAL_KEY_EXPRESSIONS key expressions with internal keys; signing
+ * only supports up to that number,
+ *
+ * Returns true on success, false if at least one condition is violated.
+ * It will send the error status before returning.
*/
-static bool has_musig_with_multiple_internal_keys(
+static bool check_policy_is_signable(
+ dispatcher_context_t *dc,
const policy_node_t *policy,
const key_type_e keys_type[static MAX_N_KEYS_IN_WALLET_POLICY]) {
int n_key_expressions = get_keyexpr_by_index(policy, 0, NULL, NULL);
LEDGER_ASSERT(n_key_expressions >= 0, "Unexpected error retrieving key expression");
+ size_t n_internal_key_expressions = 0;
for (int i = 0; i < n_key_expressions; i++) {
policy_node_keyexpr_t *key_expr;
int ret = get_keyexpr_by_index(policy, i, NULL, &key_expr);
LEDGER_ASSERT(ret >= 0, "Unexpected error retrieving key expression");
- if (key_expr->type != KEY_EXPRESSION_MUSIG) {
- continue;
- }
- const musig_aggr_key_info_t *musig_info = key_expr->m.musig_info;
int n_internal_keys = 0;
- for (int j = 0; j < musig_info->n; j++) {
- if (keys_type[musig_info->key_indexes[j]] == PUBKEY_TYPE_INTERNAL) {
+ if (key_expr->type == KEY_EXPRESSION_NORMAL) {
+ if (keys_type[key_expr->k.key_index] == PUBKEY_TYPE_INTERNAL) {
++n_internal_keys;
}
+ } else if (key_expr->type == KEY_EXPRESSION_MUSIG) {
+ const musig_aggr_key_info_t *musig_info = key_expr->m.musig_info;
+ for (int j = 0; j < musig_info->n; j++) {
+ if (keys_type[musig_info->key_indexes[j]] == PUBKEY_TYPE_INTERNAL) {
+ ++n_internal_keys;
+ }
+ }
+ if (n_internal_keys > 1) {
+ PRINTF("A musig() key expression has more than one internal key\n");
+ SEND_SW_EC(dc,
+ SW_NOT_SUPPORTED,
+ EC_REGISTER_WALLET_MUSIG_WITH_MULTIPLE_INTERNAL_KEYS);
+ return false;
+ }
+ } else {
+ LEDGER_ASSERT(false, "Unexpected key expression type");
}
- if (n_internal_keys > 1) {
- return true;
+
+ if (n_internal_keys > 0) {
+ ++n_internal_key_expressions;
}
}
- return false;
+
+ if (n_internal_key_expressions > MAX_INTERNAL_KEY_EXPRESSIONS) {
+ PRINTF("Too many internal key expressions. The maximum supported is %d\n",
+ MAX_INTERNAL_KEY_EXPRESSIONS);
+ SEND_SW_EC(dc, SW_NOT_SUPPORTED, EC_REGISTER_WALLET_TOO_MANY_INTERNAL_KEY_EXPRESSIONS);
+ return false;
+ }
+ return true;
}
### tests/test_register_wallet.py
@@ -260,6 +260,47 @@ def test_register_wallet_musig_multiple_internal_keys(client: RaggerClient):
assert error_code == EC_REGISTER_WALLET_MUSIG_WITH_MULTIPLE_INTERNAL_KEYS
+def test_register_wallet_too_many_internal_key_expressions(client: RaggerClient):
+ # The same internal key can appear in many key expressions (with disjoint derivations), but
+ # signing supports at most MAX_INTERNAL_KEY_EXPRESSIONS (8) key expressions with internal keys,
+ # counting musig() key expressions that contain an internal key. Registration must reject
+ # policies with more of them.
+
+ # defined in error_codes.h
+ EC_REGISTER_WALLET_TOO_MANY_INTERNAL_KEY_EXPRESSIONS = 0x0004
+
+ internal_key = "[f5acc2fd/48'/1'/0'/2']tpubDFAqEGNyad35aBCKUAXbQGDjdVhNueno5ZZVEn3sQbW5ci457gLR7HyTmHBg93oourBssgUxuWz1jX5uhc1qaqFo9VsybY1J5FuedLfm4dK"
+ external_key = "tpubDE7NQymr4AFtewpAsWtnreyq9ghkzQBXpCZjWLFVRAvnbf7vya2eMTvT2fPapNqL8SuVvLQdbUbMfWLVDCZKnsEBqp6UK93QEzL8Ck23AwF"
+
+ # @0/<2;3>/*, @0/<4;5>/*, ..., @0/<16;17>/*
+ keys = [f"@0/<{2*i};{2*i+1}>/*" for i in range(1, 9)]
+ leaves = [f"pk({k})" for k in keys]
+ tree = (f"{{{{{{{leaves[0]},{leaves[1]}}},{{{leaves[2]},{leaves[3]}}}}},"
+ f"{{{{{leaves[4]},{leaves[5]}}},{{{leaves[6]},{leaves[7]}}}}}}}")
+
+ for descriptor_template, keys_info in [
+ # key path + 8 leaves
+ (f"tr(@0/<0;1>/*,{tree})", [internal_key]),
+ # 9 key expressions in a single leaf
+ (f"tr(@1/**,multi_a(1,@0/<0;1>/*,{','.join(keys)}))", [internal_key, external_key]),
+ # a musig() with an internal key counts as an internal key expression
+ (f"tr(musig(@0,@1)/<0;1>/*,multi_a(1,{','.join(keys)}))", [internal_key, external_key]),
+ # same in segwit v0
+ (f"wsh(multi(1,@0/<0;1>/*,{','.join(keys)}))", [internal_key]),
+ ]:
+ with pytest.raises(ExceptionRAPDU) as e:
+ client.register_wallet(WalletPolicy(
+ name="Too many internal keys",
+ descriptor_template=descriptor_template,
+ keys_info=keys_info,
+ ))
+
+ assert DeviceException.exc.get(e.value.status) == NotSupportedError
+ assert len(e.value.data) == 2
+ error_code = int.from_bytes(e.value.data, 'big')
+ assert error_code == EC_REGISTER_WALLET_TOO_MANY_INTERNAL_KEY_EXPRESSIONS
+
+
def test_register_miniscript_long_policy(navigator: Navigator, firmware: Firmware, client:
RaggerClient, test_name: str, speculos_globals):
# This test makes sure that policies longer than 256 bytes work as expected on all devicesWhy this scored 45/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.