2022-05-16 10:59:20 +02:00
|
|
|
#ifdef HAVE_EIP712_FULL_SUPPORT
|
|
|
|
|
|
2022-03-04 18:15:37 +01:00
|
|
|
#include <stdlib.h>
|
|
|
|
|
#include <stdint.h>
|
|
|
|
|
#include <stdbool.h>
|
2022-07-05 17:18:22 +02:00
|
|
|
#include "commands_712.h"
|
2022-07-19 11:49:18 +02:00
|
|
|
#include "apdu_constants.h" // APDU response codes
|
2022-08-25 10:54:34 +02:00
|
|
|
#include "context_712.h"
|
2022-05-02 15:30:14 +02:00
|
|
|
#include "field_hash.h"
|
2022-04-08 18:55:56 +02:00
|
|
|
#include "path.h"
|
2022-05-12 17:29:35 +02:00
|
|
|
#include "ui_logic.h"
|
2022-07-05 11:41:46 +02:00
|
|
|
#include "typed_data.h"
|
2022-07-05 14:38:04 +02:00
|
|
|
#include "schema_hash.h"
|
|
|
|
|
#include "filtering.h"
|
|
|
|
|
#include "common_712.h"
|
2023-06-30 13:42:56 +02:00
|
|
|
#include "common_ui.h" // ui_idle
|
2024-04-12 11:09:06 +02:00
|
|
|
#include "manage_asset_info.h"
|
|
|
|
|
|
|
|
|
|
// APDUs P1
|
|
|
|
|
#define P1_COMPLETE 0x00
|
|
|
|
|
#define P1_PARTIAL 0xFF
|
|
|
|
|
|
|
|
|
|
// APDUs P2
|
|
|
|
|
#define P2_DEF_NAME 0x00
|
|
|
|
|
#define P2_DEF_FIELD 0xFF
|
|
|
|
|
#define P2_IMPL_NAME P2_DEF_NAME
|
|
|
|
|
#define P2_IMPL_ARRAY 0x0F
|
|
|
|
|
#define P2_IMPL_FIELD P2_DEF_FIELD
|
|
|
|
|
#define P2_FILT_ACTIVATE 0x00
|
|
|
|
|
#define P2_FILT_MESSAGE_INFO 0x0F
|
2024-05-13 17:40:26 +02:00
|
|
|
#define P2_FILT_DATE_TIME 0xFC
|
2024-04-12 11:09:06 +02:00
|
|
|
#define P2_FILT_AMOUNT_JOIN_TOKEN 0xFD
|
|
|
|
|
#define P2_FILT_AMOUNT_JOIN_VALUE 0xFE
|
|
|
|
|
#define P2_FILT_RAW_FIELD 0xFF
|
2022-03-04 18:15:37 +01:00
|
|
|
|
2022-07-11 17:12:58 +02:00
|
|
|
/**
|
|
|
|
|
* Send the response to the previous APDU command
|
|
|
|
|
*
|
|
|
|
|
* In case of an error it uses the global variable to retrieve the error code and resets
|
|
|
|
|
* the app context
|
|
|
|
|
*
|
|
|
|
|
* @param[in] success whether the command was successful
|
|
|
|
|
*/
|
2022-07-19 11:49:18 +02:00
|
|
|
void handle_eip712_return_code(bool success) {
|
|
|
|
|
if (success) {
|
2022-07-19 11:04:16 +02:00
|
|
|
apdu_response_code = APDU_RESPONSE_OK;
|
2022-08-23 12:01:32 +02:00
|
|
|
} else if (apdu_response_code == APDU_RESPONSE_OK) { // somehow not set
|
|
|
|
|
apdu_response_code = APDU_RESPONSE_ERROR_NO_INFO;
|
2022-07-19 11:04:16 +02:00
|
|
|
}
|
2022-10-03 13:44:50 +02:00
|
|
|
|
|
|
|
|
G_io_apdu_buffer[0] = (apdu_response_code >> 8) & 0xff;
|
|
|
|
|
G_io_apdu_buffer[1] = apdu_response_code & 0xff;
|
2022-07-19 11:04:16 +02:00
|
|
|
|
|
|
|
|
// Send back the response, do not restart the event loop
|
|
|
|
|
io_exchange(CHANNEL_APDU | IO_RETURN_AFTER_TX, 2);
|
|
|
|
|
|
2022-07-19 11:49:18 +02:00
|
|
|
if (!success) {
|
2022-07-19 11:04:16 +02:00
|
|
|
eip712_context_deinit();
|
2023-06-30 13:42:56 +02:00
|
|
|
ui_idle();
|
2022-07-19 11:04:16 +02:00
|
|
|
}
|
|
|
|
|
}
|
2022-03-04 18:15:37 +01:00
|
|
|
|
2022-07-11 17:12:58 +02:00
|
|
|
/**
|
|
|
|
|
* Process the EIP712 struct definition command
|
|
|
|
|
*
|
|
|
|
|
* @param[in] apdu_buf the APDU payload
|
|
|
|
|
* @return whether the command was successful or not
|
|
|
|
|
*/
|
2022-07-19 11:49:18 +02:00
|
|
|
bool handle_eip712_struct_def(const uint8_t *const apdu_buf) {
|
2022-05-12 17:28:40 +02:00
|
|
|
bool ret = true;
|
|
|
|
|
|
2022-07-19 11:49:18 +02:00
|
|
|
if (eip712_context == NULL) {
|
2022-07-19 11:04:16 +02:00
|
|
|
ret = eip712_context_init();
|
2022-03-04 18:15:37 +01:00
|
|
|
}
|
2022-10-18 11:09:00 +02:00
|
|
|
|
|
|
|
|
if (struct_state == DEFINED) {
|
|
|
|
|
ret = false;
|
|
|
|
|
}
|
|
|
|
|
|
2022-07-19 11:49:18 +02:00
|
|
|
if (ret) {
|
|
|
|
|
switch (apdu_buf[OFFSET_P2]) {
|
2022-08-10 11:47:05 +02:00
|
|
|
case P2_DEF_NAME:
|
2022-07-05 11:53:22 +02:00
|
|
|
ret = set_struct_name(apdu_buf[OFFSET_LC], &apdu_buf[OFFSET_CDATA]);
|
2022-07-19 11:04:16 +02:00
|
|
|
break;
|
2022-08-10 11:47:05 +02:00
|
|
|
case P2_DEF_FIELD:
|
2022-07-13 18:33:29 +02:00
|
|
|
ret = set_struct_field(apdu_buf[OFFSET_LC], &apdu_buf[OFFSET_CDATA]);
|
2022-07-19 11:04:16 +02:00
|
|
|
break;
|
|
|
|
|
default:
|
|
|
|
|
PRINTF("Unknown P2 0x%x for APDU 0x%x\n",
|
2022-07-19 11:49:18 +02:00
|
|
|
apdu_buf[OFFSET_P2],
|
|
|
|
|
apdu_buf[OFFSET_INS]);
|
2022-07-19 11:04:16 +02:00
|
|
|
apdu_response_code = APDU_RESPONSE_INVALID_P1_P2;
|
|
|
|
|
ret = false;
|
|
|
|
|
}
|
2022-05-04 11:31:44 +02:00
|
|
|
}
|
2022-07-19 11:04:16 +02:00
|
|
|
handle_eip712_return_code(ret);
|
2022-05-12 17:28:40 +02:00
|
|
|
return ret;
|
|
|
|
|
}
|
|
|
|
|
|
2022-07-11 17:12:58 +02:00
|
|
|
/**
|
|
|
|
|
* Process the EIP712 struct implementation command
|
|
|
|
|
*
|
|
|
|
|
* @param[in] apdu_buf the APDU payload
|
|
|
|
|
* @return whether the command was successful or not
|
|
|
|
|
*/
|
2022-07-19 11:49:18 +02:00
|
|
|
bool handle_eip712_struct_impl(const uint8_t *const apdu_buf) {
|
2022-07-19 11:04:16 +02:00
|
|
|
bool ret = false;
|
2022-06-20 15:15:58 +02:00
|
|
|
bool reply_apdu = true;
|
2022-05-12 17:28:40 +02:00
|
|
|
|
2022-07-19 11:49:18 +02:00
|
|
|
if (eip712_context == NULL) {
|
2022-07-19 11:04:16 +02:00
|
|
|
apdu_response_code = APDU_RESPONSE_CONDITION_NOT_SATISFIED;
|
2022-07-19 11:49:18 +02:00
|
|
|
} else {
|
|
|
|
|
switch (apdu_buf[OFFSET_P2]) {
|
2022-08-10 11:47:05 +02:00
|
|
|
case P2_IMPL_NAME:
|
2022-07-19 11:04:16 +02:00
|
|
|
// set root type
|
2022-10-26 14:15:09 +02:00
|
|
|
ret = path_set_root((char *) &apdu_buf[OFFSET_CDATA], apdu_buf[OFFSET_LC]);
|
|
|
|
|
if (ret) {
|
2022-07-19 11:49:18 +02:00
|
|
|
if (N_storage.verbose_eip712) {
|
2022-07-19 11:04:16 +02:00
|
|
|
ui_712_review_struct(path_get_root());
|
|
|
|
|
reply_apdu = false;
|
|
|
|
|
}
|
|
|
|
|
ui_712_field_flags_reset();
|
|
|
|
|
}
|
|
|
|
|
break;
|
2022-08-10 11:47:05 +02:00
|
|
|
case P2_IMPL_FIELD:
|
2022-07-19 11:04:16 +02:00
|
|
|
if ((ret = field_hash(&apdu_buf[OFFSET_CDATA],
|
|
|
|
|
apdu_buf[OFFSET_LC],
|
2022-07-19 11:49:18 +02:00
|
|
|
apdu_buf[OFFSET_P1] != P1_COMPLETE))) {
|
2022-06-23 14:39:06 +02:00
|
|
|
reply_apdu = false;
|
|
|
|
|
}
|
2022-07-19 11:04:16 +02:00
|
|
|
break;
|
2022-08-10 11:47:05 +02:00
|
|
|
case P2_IMPL_ARRAY:
|
2022-07-19 11:49:18 +02:00
|
|
|
ret = path_new_array_depth(&apdu_buf[OFFSET_CDATA], apdu_buf[OFFSET_LC]);
|
2022-07-19 11:04:16 +02:00
|
|
|
break;
|
|
|
|
|
default:
|
|
|
|
|
PRINTF("Unknown P2 0x%x for APDU 0x%x\n",
|
|
|
|
|
apdu_buf[OFFSET_P2],
|
|
|
|
|
apdu_buf[OFFSET_INS]);
|
|
|
|
|
apdu_response_code = APDU_RESPONSE_INVALID_P1_P2;
|
|
|
|
|
}
|
2022-05-12 17:28:40 +02:00
|
|
|
}
|
2022-07-19 11:49:18 +02:00
|
|
|
if (reply_apdu) {
|
2022-07-19 11:04:16 +02:00
|
|
|
handle_eip712_return_code(ret);
|
2022-05-04 11:31:44 +02:00
|
|
|
}
|
2022-05-12 17:28:40 +02:00
|
|
|
return ret;
|
2022-03-04 18:15:37 +01:00
|
|
|
}
|
2022-05-12 17:29:35 +02:00
|
|
|
|
2022-07-11 17:12:58 +02:00
|
|
|
/**
|
|
|
|
|
* Process the EIP712 filtering command
|
|
|
|
|
*
|
|
|
|
|
* @param[in] apdu_buf the APDU payload
|
|
|
|
|
* @return whether the command was successful or not
|
|
|
|
|
*/
|
2022-07-19 11:49:18 +02:00
|
|
|
bool handle_eip712_filtering(const uint8_t *const apdu_buf) {
|
2022-06-13 12:50:55 +02:00
|
|
|
bool ret = true;
|
2022-06-23 14:39:06 +02:00
|
|
|
bool reply_apdu = true;
|
2022-06-13 12:50:55 +02:00
|
|
|
|
2022-07-19 11:49:18 +02:00
|
|
|
if (eip712_context == NULL) {
|
2022-07-19 11:04:16 +02:00
|
|
|
apdu_response_code = APDU_RESPONSE_CONDITION_NOT_SATISFIED;
|
2024-04-12 11:09:06 +02:00
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
if ((apdu_buf[OFFSET_P2] != P2_FILT_ACTIVATE) &&
|
|
|
|
|
(ui_712_get_filtering_mode() != EIP712_FILTERING_FULL)) {
|
|
|
|
|
handle_eip712_return_code(true);
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
switch (apdu_buf[OFFSET_P2]) {
|
|
|
|
|
case P2_FILT_ACTIVATE:
|
|
|
|
|
if (!N_storage.verbose_eip712) {
|
|
|
|
|
ui_712_set_filtering_mode(EIP712_FILTERING_FULL);
|
|
|
|
|
ret = compute_schema_hash();
|
|
|
|
|
}
|
|
|
|
|
forget_known_assets();
|
|
|
|
|
break;
|
|
|
|
|
case P2_FILT_MESSAGE_INFO:
|
|
|
|
|
ret = filtering_message_info(&apdu_buf[OFFSET_CDATA], apdu_buf[OFFSET_LC]);
|
|
|
|
|
if (ret) {
|
|
|
|
|
reply_apdu = false;
|
|
|
|
|
}
|
|
|
|
|
break;
|
2024-05-13 17:40:26 +02:00
|
|
|
case P2_FILT_DATE_TIME:
|
|
|
|
|
ret = filtering_date_time(&apdu_buf[OFFSET_CDATA], apdu_buf[OFFSET_LC]);
|
|
|
|
|
break;
|
2024-04-12 11:09:06 +02:00
|
|
|
case P2_FILT_AMOUNT_JOIN_TOKEN:
|
|
|
|
|
ret = filtering_amount_join_token(&apdu_buf[OFFSET_CDATA], apdu_buf[OFFSET_LC]);
|
|
|
|
|
break;
|
|
|
|
|
case P2_FILT_AMOUNT_JOIN_VALUE:
|
|
|
|
|
ret = filtering_amount_join_value(&apdu_buf[OFFSET_CDATA], apdu_buf[OFFSET_LC]);
|
|
|
|
|
break;
|
|
|
|
|
case P2_FILT_RAW_FIELD:
|
|
|
|
|
ret = filtering_raw_field(&apdu_buf[OFFSET_CDATA], apdu_buf[OFFSET_LC]);
|
|
|
|
|
break;
|
|
|
|
|
default:
|
|
|
|
|
PRINTF("Unknown P2 0x%x for APDU 0x%x\n", apdu_buf[OFFSET_P2], apdu_buf[OFFSET_INS]);
|
|
|
|
|
apdu_response_code = APDU_RESPONSE_INVALID_P1_P2;
|
|
|
|
|
ret = false;
|
2022-06-13 12:50:55 +02:00
|
|
|
}
|
2024-07-18 15:48:46 +02:00
|
|
|
if ((apdu_buf[OFFSET_P2] > P2_FILT_MESSAGE_INFO) && ret) {
|
|
|
|
|
if (ui_712_push_new_filter_path()) {
|
|
|
|
|
if (!ui_712_filters_counter_incr()) {
|
|
|
|
|
ret = false;
|
|
|
|
|
apdu_response_code = APDU_RESPONSE_INVALID_DATA;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2022-07-19 11:49:18 +02:00
|
|
|
if (reply_apdu) {
|
2022-07-19 11:04:16 +02:00
|
|
|
handle_eip712_return_code(ret);
|
2022-06-13 12:50:55 +02:00
|
|
|
}
|
|
|
|
|
return ret;
|
|
|
|
|
}
|
|
|
|
|
|
2022-07-11 17:12:58 +02:00
|
|
|
/**
|
|
|
|
|
* Process the EIP712 sign command
|
|
|
|
|
*
|
|
|
|
|
* @param[in] apdu_buf the APDU payload
|
|
|
|
|
* @return whether the command was successful or not
|
|
|
|
|
*/
|
2022-07-19 11:49:18 +02:00
|
|
|
bool handle_eip712_sign(const uint8_t *const apdu_buf) {
|
2022-07-19 11:04:16 +02:00
|
|
|
bool ret = false;
|
2022-07-05 17:18:22 +02:00
|
|
|
uint8_t length = apdu_buf[OFFSET_LC];
|
2022-07-19 11:04:16 +02:00
|
|
|
|
2022-07-19 11:49:18 +02:00
|
|
|
if (eip712_context == NULL) {
|
2022-07-19 11:04:16 +02:00
|
|
|
apdu_response_code = APDU_RESPONSE_CONDITION_NOT_SATISFIED;
|
2022-10-18 11:48:03 +02:00
|
|
|
}
|
|
|
|
|
// if the final hashes are still zero or if there are some unimplemented fields
|
|
|
|
|
else if (allzeroes(tmpCtx.messageSigningContext712.domainHash,
|
|
|
|
|
sizeof(tmpCtx.messageSigningContext712.domainHash)) ||
|
|
|
|
|
allzeroes(tmpCtx.messageSigningContext712.messageHash,
|
|
|
|
|
sizeof(tmpCtx.messageSigningContext712.messageHash)) ||
|
|
|
|
|
(path_get_field() != NULL)) {
|
|
|
|
|
apdu_response_code = APDU_RESPONSE_CONDITION_NOT_SATISFIED;
|
2024-07-23 17:26:00 +02:00
|
|
|
} else if ((ui_712_get_filtering_mode() == EIP712_FILTERING_FULL) &&
|
|
|
|
|
(ui_712_remaining_filters() != 0)) {
|
|
|
|
|
PRINTF("%d EIP712 filters are missing\n", ui_712_remaining_filters());
|
|
|
|
|
apdu_response_code = APDU_RESPONSE_REF_DATA_NOT_FOUND;
|
2022-07-19 11:49:18 +02:00
|
|
|
} else if (parseBip32(&apdu_buf[OFFSET_CDATA], &length, &tmpCtx.messageSigningContext.bip32) !=
|
|
|
|
|
NULL) {
|
|
|
|
|
if (!N_storage.verbose_eip712 && (ui_712_get_filtering_mode() == EIP712_FILTERING_BASIC)) {
|
2022-07-19 11:04:16 +02:00
|
|
|
ui_712_message_hash();
|
|
|
|
|
}
|
|
|
|
|
ret = true;
|
|
|
|
|
ui_712_end_sign();
|
2022-06-20 15:15:58 +02:00
|
|
|
}
|
2022-07-19 11:49:18 +02:00
|
|
|
if (!ret) {
|
2022-07-19 11:04:16 +02:00
|
|
|
handle_eip712_return_code(ret);
|
|
|
|
|
}
|
|
|
|
|
return ret;
|
2022-05-12 17:29:35 +02:00
|
|
|
}
|
2022-05-16 10:59:20 +02:00
|
|
|
|
2022-07-19 11:49:18 +02:00
|
|
|
#endif // HAVE_EIP712_FULL_SUPPORT
|