Skip to content

Commit

Permalink
Fix lint issues
Browse files Browse the repository at this point in the history
  • Loading branch information
yogh333 committed Nov 19, 2024
1 parent cf3867d commit c4df991
Show file tree
Hide file tree
Showing 7 changed files with 179 additions and 425 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/lint-workflow.yml
Original file line number Diff line number Diff line change
Expand Up @@ -45,4 +45,4 @@ jobs:
# Use Config file when the github action supports it
builtin: clear,rare
check_filenames: true
skip: ./libsol/printer_test.c,./tests/Cargo.lock
skip: ./libsol/printer_test.c,./tests/Cargo.lock,./tools/apdu_generator/Cargo.lock
2 changes: 1 addition & 1 deletion libsol/spl_token_instruction.c
Original file line number Diff line number Diff line change
Expand Up @@ -507,7 +507,7 @@ int print_spl_token_transfer_info(const SplTokenTransferInfo* info,

if (g_trusted_token_account_owner_pubkey_set) {
item = transaction_summary_general_item();
summary_item_set_string(item, "To", (char *)g_trusted_token_account_owner_pubkey);
summary_item_set_string(item, "To", (char*) g_trusted_token_account_owner_pubkey);
}

item = transaction_summary_general_item();
Expand Down
21 changes: 10 additions & 11 deletions src/apdu.c
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ int apdu_handle_message(const uint8_t* apdu_message,
const bool first_data_chunk = !(header.p2 & P2_EXTEND);

if (header.instruction == InsDeprecatedGetAppConfiguration ||
header.instruction == InsGetAppConfiguration ||
header.instruction == InsGetAppConfiguration ||
header.instruction == InsTrustedInfoGetChallenge) {
// return early if no data is expected for the command
explicit_bzero(apdu_command, sizeof(ApduCommand));
Expand All @@ -126,13 +126,12 @@ int apdu_handle_message(const uint8_t* apdu_message,
} else {
explicit_bzero(apdu_command, sizeof(ApduCommand));
}
}
else {
} else {
explicit_bzero(apdu_command, sizeof(ApduCommand));
}

if ((first_data_chunk) && (header.instruction != InsTrustedInfoProvideInfo)) {
// read derivation path
// read derivation path
if (!header.deprecated_host && header.instruction != InsGetPubkey) {
if (!header.data_length) {
return ApduReplySolanaInvalidMessageSize;
Expand All @@ -148,9 +147,9 @@ int apdu_handle_message(const uint8_t* apdu_message,
apdu_command->num_derivation_paths = 1;
}
const int ret = read_derivation_path(header.data,
header.data_length,
apdu_command->derivation_path,
&apdu_command->derivation_path_length);
header.data_length,
apdu_command->derivation_path,
&apdu_command->derivation_path_length);
if (ret) {
return ret;
}
Expand All @@ -175,15 +174,15 @@ int apdu_handle_message(const uint8_t* apdu_message,
if (header.data_length != data_len) {
return ApduReplySolanaInvalidMessageSize;
}
}
}
if (header.data) {
if (apdu_command->message_length + header.data_length > MAX_MESSAGE_LENGTH) {
return ApduReplySolanaInvalidMessageSize;
}

memcpy(apdu_command->message + apdu_command->message_length,
header.data,
header.data_length);
header.data,
header.data_length);
apdu_command->message_length += header.data_length;
} else if (header.instruction != InsDeprecatedGetPubkey && header.instruction != InsGetPubkey) {
return ApduReplySolanaInvalidMessageSize;
Expand Down
87 changes: 45 additions & 42 deletions src/handle_provide_trusted_info.c
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@

#include "ledger_pki.h"

#define TYPE_ADDRESS 0x06
#define TYPE_ADDRESS 0x06
#define TYPE_DYN_RESOLVER 0x06

#define STRUCT_TYPE_TRUSTED_NAME 0x03
Expand Down Expand Up @@ -47,31 +47,31 @@ typedef enum {

/*
trusted_name_descriptor = tlv(TAG_STRUCTURE_TYPE, u8(TYPE_TRUSTED_NAME)) 3 bytes +
& tlv(TAG_VERSION, u8(0x02)) 3 bytes +
& tlv(TAG_VERSION, u8(0x02)) 3 bytes +
& tlv(TAG_TRUSTED_NAME_TYPE, 0x06) 3 bytes +
& tlv(TAG_TRUSTED_NAME_SOURCE, 0x06) 3 bytes +
& tlv(TAG_TRUSTED_NAME, trusted_name) 2 + 44 bytes +
& tlv(TAG_TRUSTED_NAME, trusted_name) 2 + 44 bytes +
& tlv(TAG_CHAIN_ID, chain_id) 2 + 8 bytes +
& tlv(TAG_ADDRESS, address) 2 + 44 bytes +
& tlv(TAG_SOURCE_CONTRACT, source_contract)* 2 + 44 bytes +
& tlv(TAG_CHALLENGE, challenge) 2 + 4 bytes +
& tlv(TAG_SIGNER_KEY_ID, key_id) 2 + 2 bytes +
& tlv(TAG_SIGNER_ALGORITHM, signature_algorithm) 2 + 1 byte +
& tlv(TAG_SIGNATURE, signature(~,~)) 2 + 72 bytes => Total = 247 bytes
& tlv(TAG_SIGNATURE, signature(~,~)) 2 + 72 bytes => 247
T L V
01 01 03 TAG_STRUCTURE_TYPE
02 01 02 TAG_VERSION
70 01 06 TAG_TRUSTED_NAME_TYPE
71 01 06 TAG_TRUSTED_NAME_SOURCE
20 20 276497ba0bb8659172b72edd8c66e18f561764d9c86a610a3a7e0f79c0baf9db TAG_TRUSTED_NAME
23 01 65 TAG_CHAIN_ID
23 01 65 TAG_CHAIN_ID
22 20 606501b302e1801892f80a2979f585f8855d0f2034790a2455f744fac503d7b5 TAG_ADDRESS
73 20 c6fa7af3bedbad3a3d65f36aabc97431b1bbe4c2d2f6e0e47ca60203452f5d61 TAG_SOURCE_CONTRACT
12 04 deadbeef TAG_CHALLENGE
13 01 03 TAG_SIGNER_KEY_ID
14 01 01 TAG_SIGNER_ALGORITHM
15 47 3045022100ac06a740744dac01b4b848ca2730c507e2a7a93f92952a0c9757bb12d8e6de490220262a529c8d809b9b2538bce34ecaa03b5c54f602656fd210f01c304a3da0b52e
14 01 01 TAG_SIGNER_ALGORITHM
15 47 30..2e
01 01 03
Expand All @@ -85,7 +85,7 @@ T L V
12 04 DEADBEEF
13 01 00
14 01 01
15 47 3045022100B559CD96CCCDA78CA393CE5D4A300E52177ABCC18F5E674FE9D4088322628ADD02200EAA84D21EE275DEC82F5D747ADAA408816AC03CC133BD722DC26016F7FA92CF
15 47 30..CF
*/

Expand Down Expand Up @@ -126,7 +126,7 @@ typedef struct {
bool valid;
uint8_t struct_version;
uint8_t token_account[MAX_ADDRESS_LENGTH + 1];
uint8_t* owner;
uint8_t *owner;
uint8_t spl_token[MAX_ADDRESS_LENGTH + 1];
uint64_t chain_id;
uint8_t name_type;
Expand Down Expand Up @@ -311,16 +311,16 @@ static bool handle_signature(const s_tlv_data *data,
* @return whether it was successful
*/
static bool handle_source_contract(const s_tlv_data *data,
s_trusted_name_info *trusted_name_info,
s_sig_ctx *sig_ctx) {
s_trusted_name_info *trusted_name_info,
s_sig_ctx *sig_ctx) {
(void) sig_ctx;
if (data->length > MAX_ADDRESS_LENGTH) {
PRINTF("SPL Token address too long! (%u)\n", data->length);
return false;
}

memcpy(trusted_name_info->spl_token, data->value, data->length);

trusted_name_info->spl_token[data->length] = '\0';
return true;
}
Expand Down Expand Up @@ -365,9 +365,9 @@ static bool handle_trusted_name(const s_tlv_data *data,
PRINTF("Token Account address too long! (%u)\n", data->length);
return false;
}

memcpy(trusted_name_info->token_account, data->value, data->length);

trusted_name_info->token_account[data->length] = '\0';
return true;
}
Expand Down Expand Up @@ -408,18 +408,16 @@ static bool handle_chain_id(const s_tlv_data *data,
bool res = false;

switch (data->length) {
case 1:
{
trusted_name_info->chain_id = data->value[0];
res = true;
break;
}
case 2:
{
trusted_name_info->chain_id = (data->value[0] << 8) | data->value[1];
res = true;
break;
}
case 1: {
trusted_name_info->chain_id = data->value[0];
res = true;
break;
}
case 2: {
trusted_name_info->chain_id = (data->value[0] << 8) | data->value[1];
res = true;
break;
}
default:
PRINTF("Error while parsing chain ID: length = %d\n", data->length);
}
Expand Down Expand Up @@ -563,7 +561,6 @@ static bool handle_tlv_data(s_tlv_handler *handlers,
static bool verify_struct(const s_trusted_name_info *trusted_name_info) {
uint32_t required_flags;


if (!(RCV_FLAG(STRUCT_VERSION_RCV_BIT) & trusted_name_info->rcv_flags)) {
PRINTF("Error: no struct version specified!\n");
return false;
Expand All @@ -584,12 +581,14 @@ static bool verify_struct(const s_trusted_name_info *trusted_name_info) {
switch (trusted_name_info->name_type) {
case TYPE_ADDRESS:
if (trusted_name_info->name_source != TYPE_DYN_RESOLVER) {
PRINTF("Error: unsupported trusted name source (%u)!\n", trusted_name_info->name_source);
PRINTF("Error: unsupported trusted name source (%u)!\n",
trusted_name_info->name_source);
return false;
}
break;
default:
PRINTF("Error: unsupported trusted name type (%u)!\n", trusted_name_info->name_type);
PRINTF("Error: unsupported trusted name type (%u)!\n",
trusted_name_info->name_type);
return false;
}
break;
Expand Down Expand Up @@ -729,19 +728,20 @@ static bool parse_tlv(const s_tlv_payload *payload,
}
data.value = &payload->buf[offset];
if (!handle_tlv_data(handlers,
(sizeof(handlers)/sizeof(handlers[0])),
(sizeof(handlers) / sizeof(handlers[0])),
&data,
trusted_name_info,
sig_ctx)) {
return false;
}
offset += data.length;
if (data.tag != SIGNATURE) { // the signature wasn't computed on itself
CX_ASSERT(cx_hash_no_throw(
(cx_hash_t *) &sig_ctx->hash_ctx,
0,
&payload->buf[tag_start_off],
(offset - tag_start_off), NULL, 0));
CX_ASSERT(cx_hash_no_throw((cx_hash_t *) &sig_ctx->hash_ctx,
0,
&payload->buf[tag_start_off],
(offset - tag_start_off),
NULL,
0));
}
step = TLV_TAG;
break;
Expand All @@ -768,7 +768,6 @@ static void free_payload(s_tlv_payload *payload) {
}

static bool init_tlv_payload(uint8_t length, s_tlv_payload *payload) {

// check if no payload is already in memory
if (payload->buf != NULL) {
free_payload(payload);
Expand Down Expand Up @@ -810,7 +809,7 @@ void handle_provide_trusted_info(void) {
}
// feed into tlv payload
memcpy(g_tlv_payload.buf + g_tlv_payload.size, data, data_length);
g_tlv_payload.size += data_length;
g_tlv_payload.size += data_length;

PRINTF("Received %d bytes of trusted info\n", g_tlv_payload.size);

Expand All @@ -823,13 +822,17 @@ void handle_provide_trusted_info(void) {
free_payload(&g_tlv_payload);
roll_challenge(); // prevent brute-force guesses
g_trusted_name_info.rcv_flags = 0;
memset(g_trusted_token_account_owner_pubkey, 0, sizeof(g_trusted_token_account_owner_pubkey));
memset(g_trusted_token_account_owner_pubkey,
0,
sizeof(g_trusted_token_account_owner_pubkey));
g_trusted_token_account_owner_pubkey_set = false;
THROW(ApduReplySolanaInvalidTrustedInfo);
}

PRINTF("Token account : %s owned by %s\n", g_trusted_name_info.token_account, g_trusted_token_account_owner_pubkey);


PRINTF("Token account : %s owned by %s\n",
g_trusted_name_info.token_account,
g_trusted_token_account_owner_pubkey);

free_payload(&g_tlv_payload);
roll_challenge(); // prevent replays
THROW(ApduReplySuccess);
Expand Down
7 changes: 5 additions & 2 deletions src/ledger_pki.c
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#include "apdu.h"
#include "ledger_pki.h"
#include "cx.h"
#include "cx.h"
#include "os_pki.h"

#define KEY_USAGE_STR(x) \
Expand Down Expand Up @@ -48,7 +48,10 @@ int check_signature_with_pubkey(const char *tag,
goto end;
}
} else {
PRINTF("[%s] ********** Issue when loading PKI certificate, cannot check signature **********\n", tag);
PRINTF(
"[%s] ********** Issue when loading PKI certificate, cannot check signature "
"**********\n",
tag);
error = ApduReplySolanaInvalidTrustedInfo;
goto end;
}
Expand Down
6 changes: 3 additions & 3 deletions src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -92,10 +92,10 @@ void handleApdu(volatile unsigned int *flags, volatile unsigned int *tx, int rx)
case InsTrustedInfoGetChallenge:
handle_get_challenge(tx);
break;

case InsTrustedInfoProvideInfo:
handle_provide_trusted_info();
THROW(ApduReplySuccess);
handle_provide_trusted_info();
THROW(ApduReplySuccess);

default:
THROW(ApduReplyUnimplementedInstruction);
Expand Down
Loading

0 comments on commit c4df991

Please sign in to comment.