@@ -63,7 +63,7 @@ typedef enum {
63
63
NFT_ID = 0x72 ,
64
64
} e_tlv_tag ;
65
65
66
- typedef enum { KEY_ID_TEST = 0x00 , KEY_ID_PROD = 0x03 } e_key_id ;
66
+ typedef enum { TN_KEY_ID_DOMAIN_SVC = 0x03 , TN_KEY_ID_CAL = 0x06 } e_tn_key_id ;
67
67
68
68
typedef struct {
69
69
uint8_t * buf ;
@@ -90,7 +90,7 @@ typedef struct {
90
90
} s_trusted_name_info ;
91
91
92
92
typedef struct {
93
- e_key_id key_id ;
93
+ e_tn_key_id key_id ;
94
94
uint8_t input_sig_size ;
95
95
const uint8_t * input_sig ;
96
96
cx_sha256_t hash_ctx ;
@@ -110,10 +110,56 @@ static s_tlv_payload g_tlv_payload = {0};
110
110
static s_trusted_name_info g_trusted_name_info = {0 };
111
111
char g_trusted_name [TRUSTED_NAME_MAX_LENGTH + 1 ];
112
112
113
+ static bool matching_type (e_name_type type , uint8_t type_count , const e_name_type * types ) {
114
+ for (int i = 0 ; i < type_count ; ++ i ) {
115
+ if (type == types [i ]) return true;
116
+ }
117
+ return false;
118
+ }
119
+
120
+ static bool matching_source (e_name_source source ,
121
+ uint8_t source_count ,
122
+ const e_name_source * sources ) {
123
+ for (int i = 0 ; i < source_count ; ++ i ) {
124
+ if (source == sources [i ]) return true;
125
+ }
126
+ return false;
127
+ }
128
+
129
+ static bool matching_trusted_name (const s_trusted_name_info * trusted_name ,
130
+ uint8_t type_count ,
131
+ const e_name_type * types ,
132
+ uint8_t source_count ,
133
+ const e_name_source * sources ,
134
+ const uint64_t * chain_id ,
135
+ const uint8_t * addr ) {
136
+ switch (trusted_name -> struct_version ) {
137
+ case 1 :
138
+ if (!matching_type (TN_TYPE_ACCOUNT , type_count , types )) {
139
+ return false;
140
+ }
141
+ if (!chain_is_ethereum_compatible (chain_id )) {
142
+ return false;
143
+ }
144
+ break ;
145
+ case 2 :
146
+ if (!matching_type (trusted_name -> name_type , type_count , types )) {
147
+ return false;
148
+ }
149
+ if (!matching_source (trusted_name -> name_source , source_count , sources )) {
150
+ return false;
151
+ }
152
+ if (* chain_id != trusted_name -> chain_id ) {
153
+ return false;
154
+ }
155
+ break ;
156
+ }
157
+ return memcmp (addr , trusted_name -> addr , ADDRESS_LENGTH ) == 0 ;
158
+ }
159
+
113
160
/**
114
161
* Checks if a trusted name matches the given parameters
115
162
*
116
- * Does not care about the trusted name source for now.
117
163
* Always wipes the content of \ref g_trusted_name_info
118
164
*
119
165
* @param[in] types_count number of given trusted name types
@@ -122,36 +168,23 @@ char g_trusted_name[TRUSTED_NAME_MAX_LENGTH + 1];
122
168
* @param[in] addr given address
123
169
* @return whether there is or not
124
170
*/
125
- bool has_trusted_name (uint8_t types_count ,
126
- const e_name_type * types ,
127
- const uint64_t * chain_id ,
128
- const uint8_t * addr ) {
129
- bool ret = false;
171
+ const char * get_trusted_name (uint8_t type_count ,
172
+ const e_name_type * types ,
173
+ uint8_t source_count ,
174
+ const e_name_source * sources ,
175
+ const uint64_t * chain_id ,
176
+ const uint8_t * addr ) {
177
+ const char * ret = NULL ;
130
178
131
179
if (g_trusted_name_info .rcv_flags != 0 ) {
132
- for (int i = 0 ; i < types_count ; ++ i ) {
133
- switch (g_trusted_name_info .struct_version ) {
134
- case 1 :
135
- if (types [i ] == TYPE_ACCOUNT ) {
136
- // Check if chain ID is known to be Ethereum-compatible (same derivation
137
- // path)
138
- if ((chain_is_ethereum_compatible (chain_id )) &&
139
- (memcmp (addr , g_trusted_name_info .addr , ADDRESS_LENGTH ) == 0 )) {
140
- ret = true;
141
- }
142
- }
143
- break ;
144
- case 2 :
145
- if (types [i ] == g_trusted_name_info .name_type ) {
146
- if (* chain_id == g_trusted_name_info .chain_id ) {
147
- ret = true;
148
- }
149
- }
150
- break ;
151
- default :
152
- ret = false;
153
- }
154
- if (ret ) break ;
180
+ if (matching_trusted_name (& g_trusted_name_info ,
181
+ type_count ,
182
+ types ,
183
+ source_count ,
184
+ sources ,
185
+ chain_id ,
186
+ addr )) {
187
+ ret = g_trusted_name_info .name ;
155
188
}
156
189
explicit_bzero (& g_trusted_name_info , sizeof (g_trusted_name_info ));
157
190
}
@@ -378,7 +411,7 @@ static bool handle_trusted_name(const s_tlv_data *data,
378
411
return false;
379
412
}
380
413
if ((trusted_name_info -> struct_version == 1 ) ||
381
- (trusted_name_info -> name_type == TYPE_ACCOUNT )) {
414
+ (trusted_name_info -> name_type == TN_TYPE_ACCOUNT )) {
382
415
// TODO: Remove once other domain name providers are supported
383
416
if ((data -> length < 5 ) ||
384
417
(strncmp (".eth" , (char * ) & data -> value [data -> length - 4 ], 4 ) != 0 )) {
@@ -474,10 +507,13 @@ static bool handle_trusted_name_type(const s_tlv_data *data,
474
507
return false;
475
508
}
476
509
switch (value ) {
477
- case TYPE_ACCOUNT :
478
- case TYPE_CONTRACT :
510
+ case TN_TYPE_ACCOUNT :
511
+ case TN_TYPE_CONTRACT :
479
512
break ;
480
- case TYPE_NFT :
513
+ case TN_TYPE_NFT_COLLECTION :
514
+ case TN_TYPE_TOKEN :
515
+ case TN_TYPE_WALLET :
516
+ case TN_TYPE_CONTEXT_ADDRESS :
481
517
default :
482
518
PRINTF ("Error: unsupported trusted name type (%u)!\n" , value );
483
519
return false;
@@ -505,13 +541,14 @@ static bool handle_trusted_name_source(const s_tlv_data *data,
505
541
return false;
506
542
}
507
543
switch (value ) {
508
- case SOURCE_CAL :
509
- case SOURCE_ENS :
544
+ case TN_SOURCE_CAL :
545
+ case TN_SOURCE_ENS :
510
546
break ;
511
- case SOURCE_LAB :
512
- case SOURCE_UD :
513
- case SOURCE_FN :
514
- case SOURCE_DNS :
547
+ case TN_SOURCE_LAB :
548
+ case TN_SOURCE_UD :
549
+ case TN_SOURCE_FN :
550
+ case TN_SOURCE_DNS :
551
+ case TN_SOURCE_DYNAMIC_RESOLVER :
515
552
default :
516
553
PRINTF ("Error: unsupported trusted name source (%u)!\n" , value );
517
554
return false;
@@ -555,16 +592,22 @@ static bool handle_nft_id(const s_tlv_data *data,
555
592
static bool verify_signature (const s_sig_ctx * sig_ctx ) {
556
593
uint8_t hash [INT256_LENGTH ];
557
594
cx_err_t error = CX_INTERNAL_ERROR ;
558
- #ifdef HAVE_TRUSTED_NAME_TEST_KEY
559
- e_key_id valid_key_id = KEY_ID_TEST ;
560
- #else
561
- e_key_id valid_key_id = KEY_ID_PROD ;
562
- #endif
563
595
bool ret_code = false;
596
+ const uint8_t * pk ;
597
+ size_t pk_size ;
564
598
565
- if (sig_ctx -> key_id != valid_key_id ) {
566
- PRINTF ("Error: Unknown metadata key ID %u\n" , sig_ctx -> key_id );
567
- return false;
599
+ switch (sig_ctx -> key_id ) {
600
+ case TN_KEY_ID_DOMAIN_SVC :
601
+ pk = TRUSTED_NAME_PUB_KEY ;
602
+ pk_size = sizeof (TRUSTED_NAME_PUB_KEY );
603
+ break ;
604
+ case TN_KEY_ID_CAL :
605
+ pk = LEDGER_SIGNATURE_PUBLIC_KEY ;
606
+ pk_size = sizeof (LEDGER_SIGNATURE_PUBLIC_KEY );
607
+ break ;
608
+ default :
609
+ PRINTF ("Error: Unknown metadata key ID %u\n" , sig_ctx -> key_id );
610
+ return false;
568
611
}
569
612
570
613
CX_CHECK (
@@ -573,10 +616,10 @@ static bool verify_signature(const s_sig_ctx *sig_ctx) {
573
616
CX_CHECK (check_signature_with_pubkey ("Domain Name" ,
574
617
hash ,
575
618
sizeof (hash ),
576
- TRUSTED_NAME_PUB_KEY ,
577
- sizeof ( TRUSTED_NAME_PUB_KEY ) ,
619
+ pk ,
620
+ pk_size ,
578
621
#ifdef HAVE_LEDGER_PKI
579
- CERTIFICATE_PUBLIC_KEY_USAGE_COIN_META ,
622
+ CERTIFICATE_PUBLIC_KEY_USAGE_TRUSTED_NAME ,
580
623
#endif
581
624
(uint8_t * ) (sig_ctx -> input_sig ),
582
625
sig_ctx -> input_sig_size ));
@@ -651,8 +694,8 @@ static bool verify_struct(const s_trusted_name_info *trusted_name_info) {
651
694
return false;
652
695
}
653
696
switch (trusted_name_info -> name_type ) {
654
- case TYPE_ACCOUNT :
655
- if (trusted_name_info -> name_source == SOURCE_CAL ) {
697
+ case TN_TYPE_ACCOUNT :
698
+ if (trusted_name_info -> name_source == TN_SOURCE_CAL ) {
656
699
PRINTF ("Error: cannot accept an account name from the CAL!\n" );
657
700
return false;
658
701
}
@@ -661,8 +704,8 @@ static bool verify_struct(const s_trusted_name_info *trusted_name_info) {
661
704
return false;
662
705
}
663
706
break ;
664
- case TYPE_CONTRACT :
665
- if (trusted_name_info -> name_source != SOURCE_CAL ) {
707
+ case TN_TYPE_CONTRACT :
708
+ if (trusted_name_info -> name_source != TN_SOURCE_CAL ) {
666
709
PRINTF ("Error: cannot accept a contract name from given source (%u)!\n" ,
667
710
trusted_name_info -> name_source );
668
711
return false;
0 commit comments