1
1
#ifdef HAVE_WEB3_CHECKS
2
2
3
- #include <ctype.h>
4
3
#include "cmd_get_tx_simulation.h"
5
4
#include "apdu_constants.h"
6
5
#include "hash_bytes.h"
@@ -108,23 +107,6 @@ tx_simulation_t TX_SIMULATION = {0};
108
107
memmove((void *) field, data->value, data->length); \
109
108
} while (0)
110
109
111
- /**
112
- * @brief Check the name is printable.
113
- *
114
- * @param[in] data buffer received
115
- * @param[in] name Name to check
116
- * @param[in] len Length of the name
117
- * @return True/False
118
- */
119
- static bool check_name (const uint8_t * name , uint16_t len ) {
120
- for (uint16_t i = 0 ; i < len ; i ++ ) {
121
- if (!isprint (name [i ])) {
122
- return false;
123
- }
124
- }
125
- return true;
126
- }
127
-
128
110
/**
129
111
* @brief Parse the STRUCTURE_TYPE value.
130
112
*
@@ -379,18 +361,25 @@ static bool verify_signature(s_tx_simu_ctx *context) {
379
361
*
380
362
* Check the mandatory fields are present
381
363
*
382
- * @param[in] rcv_bit indicates received fields
364
+ * @param[in] context TX Simu context
383
365
* @return whether it was successful
384
366
*/
385
- static bool verify_fields (uint32_t rcv_bit ) {
367
+ static bool verify_fields (s_tx_simu_ctx * context ) {
386
368
uint32_t expected_fields ;
387
369
388
370
expected_fields = (1 << BIT_STRUCTURE_TYPE ) | (1 << BIT_STRUCTURE_VERSION ) |
389
371
(1 << BIT_TX_HASH ) | (1 << BIT_ADDRESS ) | (1 << BIT_W3C_NORMALIZED_RISK ) |
390
372
(1 << BIT_W3C_NORMALIZED_CATEGORY ) | (1 << BIT_W3C_TINY_URL ) |
391
373
(1 << BIT_W3C_SIMU_TYPE ) | (1 << BIT_DER_SIGNATURE );
392
374
393
- return ((rcv_bit & expected_fields ) == expected_fields );
375
+ if (context -> simu -> type == SIMU_TYPE_TRANSACTION ) {
376
+ expected_fields |= (1 << BIT_CHAIN_ID );
377
+ }
378
+ if (context -> simu -> type == SIMU_TYPE_TYPED_DATA ) {
379
+ expected_fields |= (1 << BIT_DOMAIN_HASH );
380
+ }
381
+
382
+ return ((context -> rcv_flags & expected_fields ) == expected_fields );
394
383
}
395
384
396
385
/**
@@ -411,8 +400,10 @@ static void print_simulation_info(s_tx_simu_ctx *context) {
411
400
u64_to_string (context -> simu -> chain_id , chain_str , sizeof (chain_str ));
412
401
PRINTF ("[TX SIMU] - ChainID: %s\n" , chain_str );
413
402
}
414
- PRINTF ("[TX SIMU] - Risk: %d -> %s\n" , context -> simu -> risk , getTxSimuRiskStr ());
415
- PRINTF ("[TX SIMU] - Category: %d -> %s\n" , context -> simu -> category , getTxSimuCategoryStr ());
403
+ PRINTF ("[TX SIMU] - Risk: %d -> %s\n" , context -> simu -> risk , get_tx_simulation_risk_str ());
404
+ PRINTF ("[TX SIMU] - Category: %d -> %s\n" ,
405
+ context -> simu -> category ,
406
+ get_tx_simulation_category_str ());
416
407
PRINTF ("[TX SIMU] - Provider Msg: %s\n" , context -> simu -> provider_msg );
417
408
PRINTF ("[TX SIMU] - Tiny URL: %s\n" , context -> simu -> tiny_url );
418
409
}
@@ -489,14 +480,14 @@ static bool handle_tlv_payload(const uint8_t *payload, uint16_t size, bool to_fr
489
480
490
481
ctx .simu = & TX_SIMULATION ;
491
482
// Reset the structures
492
- explicit_bzero (& TX_SIMULATION , sizeof (tx_simulation_t ));
483
+ explicit_bzero (& TX_SIMULATION , sizeof (TX_SIMULATION ));
493
484
// Initialize the hash context
494
485
cx_sha256_init (& ctx .hash_ctx );
495
486
496
487
parsing_ret = tlv_parse (payload , size , (f_tlv_data_handler ) & handle_tx_simu_tlv , & ctx );
497
488
if (to_free ) mem_dealloc (size );
498
- if (!parsing_ret || !verify_fields (ctx . rcv_flags ) || !verify_signature (& ctx )) {
499
- explicit_bzero (& TX_SIMULATION , sizeof (tx_simulation_t ));
489
+ if (!parsing_ret || !verify_fields (& ctx ) || !verify_signature (& ctx )) {
490
+ explicit_bzero (& TX_SIMULATION , sizeof (TX_SIMULATION ));
500
491
explicit_bzero (& ctx , sizeof (s_tx_simu_ctx ));
501
492
return false;
502
493
}
@@ -513,7 +504,7 @@ static bool handle_tlv_payload(const uint8_t *payload, uint16_t size, bool to_fr
513
504
*
514
505
* @param[in] response_expected indicates if a response is expected
515
506
*/
516
- void handleTxSimulationOptIn (bool response_expected ) {
507
+ void handle_tx_simulation_opt_in (bool response_expected ) {
517
508
if (N_storage .w3c_opt_in ) {
518
509
// Web3 Checks already Opt-In
519
510
PRINTF ("Web3 Checks already Opt-in!\n" );
@@ -530,16 +521,17 @@ void handleTxSimulationOptIn(bool response_expected) {
530
521
/**
531
522
* @brief Handle Tx Simulation APDU.
532
523
*
533
- * @param[in] p1 APDU parameter 1
524
+ * @param[in] p1 APDU parameter 1 (indicates Data payload or Opt-In request)
525
+ * @param[in] p2 APDU parameter 2 (indicates if the payload is the first chunk)
534
526
* @param[in] data buffer received
535
527
* @param[in] length of the buffer
536
528
* @return APDU Response code
537
529
*/
538
- uint16_t handleTxSimulation (uint8_t p1 ,
539
- uint8_t p2 ,
540
- const uint8_t * data ,
541
- uint8_t length ,
542
- unsigned int * flags ) {
530
+ uint16_t handle_tx_simulation (uint8_t p1 ,
531
+ uint8_t p2 ,
532
+ const uint8_t * data ,
533
+ uint8_t length ,
534
+ unsigned int * flags ) {
543
535
uint16_t sw = APDU_RESPONSE_INTERNAL_ERROR ;
544
536
545
537
switch (p1 ) {
@@ -558,7 +550,7 @@ uint16_t handleTxSimulation(uint8_t p1,
558
550
break ;
559
551
case 0x01 :
560
552
// TX Simulation Opt-In
561
- handleTxSimulationOptIn (true);
553
+ handle_tx_simulation_opt_in (true);
562
554
* flags |= IO_ASYNCH_REPLY ;
563
555
sw = APDU_NO_RESPONSE ;
564
556
break ;
@@ -574,8 +566,8 @@ uint16_t handleTxSimulation(uint8_t p1,
574
566
* @brief Clear the TX Simulation parameters.
575
567
*
576
568
*/
577
- void clearTxSimulation (void ) {
578
- explicit_bzero (& TX_SIMULATION , sizeof (tx_simulation_t ));
569
+ void clear_tx_simulation (void ) {
570
+ explicit_bzero (& TX_SIMULATION , sizeof (TX_SIMULATION ));
579
571
}
580
572
581
573
/**
@@ -585,7 +577,7 @@ void clearTxSimulation(void) {
585
577
* @param[in] checkFromAddr flag to check the FROM address
586
578
* @return whether it was successful
587
579
*/
588
- bool checkTxSimulationParams (bool checkTxHash , bool checkFromAddr ) {
580
+ bool check_tx_simulation_params (bool checkTxHash , bool checkFromAddr ) {
589
581
uint8_t msg_sender [ADDRESS_LENGTH ] = {0 };
590
582
uint64_t chain_id = get_tx_chain_id ();
591
583
uint8_t * hash = NULL ;
@@ -703,13 +695,13 @@ bool checkTxSimulationParams(bool checkTxHash, bool checkFromAddr) {
703
695
* @param[in] checkTxHash flag to check the TX_HASH
704
696
* @param[in] checkFromAddr flag to check the FROM address
705
697
*/
706
- void setTxSimuWarning (nbgl_warning_t * p_warning , bool checkTxHash , bool checkFromAddr ) {
698
+ void set_tx_simulation_warning (nbgl_warning_t * p_warning , bool checkTxHash , bool checkFromAddr ) {
707
699
if (!N_storage .w3c_enable ) {
708
700
// W3Checks disabled
709
701
return ;
710
702
}
711
703
// W3Checks enabled => Verify parameters of the Transaction
712
- checkTxSimulationParams (checkTxHash , checkFromAddr );
704
+ check_tx_simulation_params (checkTxHash , checkFromAddr );
713
705
switch (TX_SIMULATION .risk ) {
714
706
case RISK_UNKNOWN :
715
707
p_warning -> predefinedSet |= SET_BIT (W3C_ISSUE_WARN );
@@ -727,7 +719,7 @@ void setTxSimuWarning(nbgl_warning_t *p_warning, bool checkTxHash, bool checkFro
727
719
break ;
728
720
}
729
721
p_warning -> reportProvider = PIC (TX_SIMULATION .partner );
730
- p_warning -> providerMessage = getTxSimuCategoryStr ();
722
+ p_warning -> providerMessage = get_tx_simulation_category_str ();
731
723
p_warning -> reportUrl = PIC (TX_SIMULATION .tiny_url );
732
724
}
733
725
@@ -736,7 +728,7 @@ void setTxSimuWarning(nbgl_warning_t *p_warning, bool checkTxHash, bool checkFro
736
728
*
737
729
* @return risk as a string
738
730
*/
739
- const char * getTxSimuRiskStr (void ) {
731
+ const char * get_tx_simulation_risk_str (void ) {
740
732
switch (TX_SIMULATION .risk ) {
741
733
case RISK_UNKNOWN :
742
734
return "UNKNOWN (W3C Issue)" ;
@@ -757,7 +749,7 @@ const char *getTxSimuRiskStr(void) {
757
749
*
758
750
* @return category string
759
751
*/
760
- const char * getTxSimuCategoryStr (void ) {
752
+ const char * get_tx_simulation_category_str (void ) {
761
753
// Unknown category string
762
754
switch (TX_SIMULATION .risk ) {
763
755
case RISK_UNKNOWN :
0 commit comments