Skip to content

Commit

Permalink
Now handle another key id for trusted names coming from CAL
Browse files Browse the repository at this point in the history
Adapted Python client
  • Loading branch information
apaillier-ledger committed Dec 13, 2024
1 parent 623b550 commit 496b51f
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 17 deletions.
16 changes: 11 additions & 5 deletions client/src/ledger_app_clients/ethereum/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -255,7 +255,7 @@ def perform_privacy_operation(self,
bip32_path,
pubkey))

def _provide_trusted_name_common(self, payload: bytes) -> RAPDU:
def _provide_trusted_name_common(self, payload: bytes, name_source: TrustedNameSource) -> RAPDU:
if self._pki_client is None:
print(f"Ledger-PKI Not supported on '{self._firmware.name}'")
else:
Expand All @@ -272,10 +272,16 @@ def _provide_trusted_name_common(self, payload: bytes) -> RAPDU:

self._pki_client.send_certificate(PKIPubKeyUsage.PUBKEY_USAGE_COIN_META, bytes.fromhex(cert_apdu))
payload += format_tlv(FieldTag.STRUCT_TYPE, 3) # TrustedName
payload += format_tlv(FieldTag.SIGNER_KEY_ID, 0) # test key
if name_source == TrustedNameSource.CAL:
key_id = 6
key = Key.CAL
else:
key_id = 3
key = Key.TRUSTED_NAME
payload += format_tlv(FieldTag.SIGNER_KEY_ID, key_id) # test key
payload += format_tlv(FieldTag.SIGNER_ALGO, 1) # secp256k1
payload += format_tlv(FieldTag.DER_SIGNATURE,
sign_data(Key.TRUSTED_NAME, payload))
sign_data(key, payload))
chunks = self._cmd_builder.provide_trusted_name(payload)
for chunk in chunks[:-1]:
self._exchange(chunk)
Expand All @@ -287,7 +293,7 @@ def provide_trusted_name_v1(self, addr: bytes, name: str, challenge: int) -> RAP
payload += format_tlv(FieldTag.COIN_TYPE, 0x3c) # ETH in slip-44
payload += format_tlv(FieldTag.TRUSTED_NAME, name)
payload += format_tlv(FieldTag.ADDRESS, addr)
return self._provide_trusted_name_common(payload)
return self._provide_trusted_name_common(payload, TrustedNameSource.ENS)

def provide_trusted_name_v2(self,
addr: bytes,
Expand All @@ -311,7 +317,7 @@ def provide_trusted_name_v2(self,
if not_valid_after is not None:
assert len(not_valid_after) == 3
payload += format_tlv(FieldTag.NOT_VALID_AFTER, struct.pack("BBB", *not_valid_after))
return self._provide_trusted_name_common(payload)
return self._provide_trusted_name_common(payload, name_source)

def set_plugin(self,
plugin_name: str,
Expand Down
30 changes: 18 additions & 12 deletions src_features/provideTrustedName/cmd_provide_trusted_name.c
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ typedef enum {
NFT_ID = 0x72,
} e_tlv_tag;

typedef enum { KEY_ID_TEST = 0x00, KEY_ID_PROD = 0x03 } e_key_id;
typedef enum { TN_KEY_ID_DOMAIN_SVC = 0x03, TN_KEY_ID_CAL = 0x06 } e_tn_key_id;

typedef struct {
uint8_t *buf;
Expand All @@ -90,7 +90,7 @@ typedef struct {
} s_trusted_name_info;

typedef struct {
e_key_id key_id;
e_tn_key_id key_id;
uint8_t input_sig_size;
const uint8_t *input_sig;
cx_sha256_t hash_ctx;
Expand Down Expand Up @@ -592,16 +592,22 @@ static bool handle_nft_id(const s_tlv_data *data,
static bool verify_signature(const s_sig_ctx *sig_ctx) {
uint8_t hash[INT256_LENGTH];
cx_err_t error = CX_INTERNAL_ERROR;
#ifdef HAVE_TRUSTED_NAME_TEST_KEY
e_key_id valid_key_id = KEY_ID_TEST;
#else
e_key_id valid_key_id = KEY_ID_PROD;
#endif
bool ret_code = false;
const uint8_t *pk;
size_t pk_size;

if (sig_ctx->key_id != valid_key_id) {
PRINTF("Error: Unknown metadata key ID %u\n", sig_ctx->key_id);
return false;
switch (sig_ctx->key_id) {
case TN_KEY_ID_DOMAIN_SVC:
pk = TRUSTED_NAME_PUB_KEY;
pk_size = sizeof(TRUSTED_NAME_PUB_KEY);
break;
case TN_KEY_ID_CAL:
pk = LEDGER_SIGNATURE_PUBLIC_KEY;
pk_size = sizeof(LEDGER_SIGNATURE_PUBLIC_KEY);
break;
default:
PRINTF("Error: Unknown metadata key ID %u\n", sig_ctx->key_id);
return false;
}

CX_CHECK(
Expand All @@ -610,8 +616,8 @@ static bool verify_signature(const s_sig_ctx *sig_ctx) {
CX_CHECK(check_signature_with_pubkey("Domain Name",
hash,
sizeof(hash),
TRUSTED_NAME_PUB_KEY,
sizeof(TRUSTED_NAME_PUB_KEY),
pk,
pk_size,
#ifdef HAVE_LEDGER_PKI
CERTIFICATE_PUBLIC_KEY_USAGE_TRUSTED_NAME,
#endif
Expand Down

0 comments on commit 496b51f

Please sign in to comment.