Skip to content

Commit

Permalink
Simplify blindsigning code and Use nbgl_useCaseReviewStreamingBlindSi…
Browse files Browse the repository at this point in the history
…gningStart for stax
  • Loading branch information
ajinkyaraj-23 committed Sep 20, 2024
1 parent f510cb5 commit 1d12e8f
Show file tree
Hide file tree
Showing 27 changed files with 74 additions and 124 deletions.
30 changes: 6 additions & 24 deletions app/src/apdu_sign.c
Original file line number Diff line number Diff line change
Expand Up @@ -363,6 +363,9 @@ refill_error(void)
tz_ui_stream_push_risky_accept_reject(TZ_UI_STREAM_CB_BLINDSIGN, TZ_UI_STREAM_CB_CANCEL);

#elif HAVE_NBGL
// The following call is just to invoke switch_to_blindsigning
// with TZ_UI_STREAM_CB_CANCEL callback type in function tz_ui_nav_cb()
// The text will not be shown.
tz_ui_stream_push_all(TZ_UI_STREAM_CB_CANCEL,
"Parsing error",
tz_parser_result_name(st->errno),
Expand Down Expand Up @@ -853,26 +856,6 @@ handle_data_apdu_clear(command_t *cmd)
#ifdef HAVE_NBGL
static nbgl_layoutTagValueList_t useCaseTagValueList;

void
reject_blindsign_cb(void)
{
FUNC_ENTER(("void"));

stream_cb(TZ_UI_STREAM_CB_BLINDSIGN_REJECT);
global.step = ST_IDLE;
ui_home_init();

FUNC_LEAVE();
}

void
reject_blindsign_review_cb(void)
{
FUNC_ENTER(("void"));
nbgl_useCaseStatus("Transaction rejected", false, reject_blindsign_cb);
FUNC_LEAVE();
}

void
accept_blindsign_cb(void)
{
Expand Down Expand Up @@ -925,17 +908,16 @@ continue_blindsign_cb(void)
{
FUNC_ENTER(("void"));
nbgl_operationType_t op = TYPE_TRANSACTION;
op |= BLIND_OPERATION;

useCaseTagValueList.pairs = NULL;
useCaseTagValueList.callback = getTagValuePair;
useCaseTagValueList.startIndex = 0;
useCaseTagValueList.nbPairs = 2;
useCaseTagValueList.smallCaseForValue = false;
useCaseTagValueList.wrapping = false;
nbgl_useCaseReview(op, &useCaseTagValueList, &C_tezos,
REVIEW("Transaction"), NULL, SIGN("Transaction"),
reviewChoice);
nbgl_useCaseReviewBlindSigning(op, &useCaseTagValueList, &C_tezos,
REVIEW("Transaction"), NULL,
SIGN("Transaction"), NULL, reviewChoice);

FUNC_LEAVE();
}
Expand Down
91 changes: 29 additions & 62 deletions app/src/ui_stream_nbgl.c
Original file line number Diff line number Diff line change
Expand Up @@ -30,19 +30,7 @@ void tz_transaction_choice(bool getMorePairs);

void drop_last_screen(void);
void push_str(const char *text, size_t len, char **out);
void switch_to_blindsigning(const char *err_type, const char *err_code);

void
tz_cancel_ui(void)
{
tz_ui_stream_t *s = &global.stream;
FUNC_ENTER(("void"));

size_t bucket = s->current % TZ_UI_STREAM_HISTORY_SCREENS;
switch_to_blindsigning(s->screens[bucket].pairs[0].item,
s->screens[bucket].pairs[0].value);
FUNC_LEAVE();
}
void switch_to_blindsigning_on_error(void);

void
tz_reject(void)
Expand Down Expand Up @@ -77,67 +65,44 @@ tz_reject_ui(void)
FUNC_LEAVE();
}

static void
start_blindsign(void)
{
FUNC_ENTER(("void"));

tz_ui_stream_t *s = &global.stream;
s->cb(TZ_UI_STREAM_CB_BLINDSIGN);

FUNC_LEAVE();
}

static void
blindsign_choice(bool confirm)
{
TZ_PREAMBLE(("void"));
if (confirm) {
start_blindsign();
} else {
tz_reject_ui();
}
TZ_POSTAMBLE;
}

static void
blindsign_splash(bool confirm)
{
TZ_PREAMBLE(("void"));
if (confirm) {
tz_reject_ui();
} else {
char blindsign_msg[150]
= "Transaction could not be decoded correctly. Learn More:\n"
"bit.ly/ledger-tez\nERROR: ";
memcpy(blindsign_msg + strlen(blindsign_msg), global.error_code,
ERROR_CODE_SIZE);
nbgl_useCaseChoice(&C_Important_Circle_64px,
"The transaction cannot be trusted", blindsign_msg,
"I accept the risk", "Reject transaction",
blindsign_choice);
tz_ui_stream_t *s = &global.stream;
s->cb(TZ_UI_STREAM_CB_BLINDSIGN);
}

TZ_POSTAMBLE;
}

void
switch_to_blindsigning(__attribute__((unused)) const char *err_type,
const char *err_code)
switch_to_blindsigning_on_error(void)
{
TZ_PREAMBLE(("void"));
PRINTF("[DEBUG] refill_error: global.step = %d\n %s", global.step,
err_code);
TZ_ASSERT(EXC_UNEXPECTED_STATE, global.step == ST_CLEAR_SIGN);
global.keys.apdu.sign.step = SIGN_ST_WAIT_USER_INPUT;
global.step = ST_BLIND_SIGN;
memcpy(global.error_code, err_code, sizeof(global.error_code));

nbgl_useCaseChoice(&C_Important_Circle_64px, "Security risk detected",
"It may not be safe to sign this transaction. To "
"continue, you'll need to review the risk.",
"Back to safety", "Review risk", blindsign_splash);
// copy error code
tz_ui_stream_t *s = &global.stream;
size_t bucket = s->current % TZ_UI_STREAM_HISTORY_SCREENS;
const char *err_code = s->screens[bucket].pairs[0].value;
PRINTF("[DEBUG] refill_error: global.step = %d\n %s", global.step,
err_code);
char blindsign_msg[150]
= "Transaction could not be decoded correctly. Learn More:\n"
"bit.ly/ledger-tez\nERROR: ";
memcpy(blindsign_msg + strlen(blindsign_msg), err_code, ERROR_CODE_SIZE);

// Show error msg and ask user to proceed to blindsign
nbgl_useCaseChoice(&C_Important_Circle_64px,
"The transaction cannot be trusted", blindsign_msg,
"Reject transaction", "Proceed to Blindsign",
blindsign_choice);
TZ_POSTAMBLE;
}

Expand Down Expand Up @@ -374,28 +339,30 @@ tz_ui_nav_cb(void)

size_t bucket = s->current % TZ_UI_STREAM_HISTORY_SCREENS;

if (tz_ui_stream_get_cb_type() == TZ_UI_STREAM_CB_CANCEL) {
// We hit an error in the parsing workflow...
tz_cancel_ui();
switch (tz_ui_stream_get_cb_type()) {
case TZ_UI_STREAM_CB_CANCEL:
switch_to_blindsigning_on_error();
result = false;
} else if (tz_ui_stream_get_cb_type()
== TZ_UI_STREAM_CB_EXPERT_MODE_ENABLE) {
break;
case TZ_UI_STREAM_CB_EXPERT_MODE_ENABLE:
tz_enable_expert_mode_ui();
result = false;
} else if (tz_ui_stream_get_cb_type()
== TZ_UI_STREAM_CB_EXPERT_MODE_FIELD) {
break;
case TZ_UI_STREAM_CB_EXPERT_MODE_FIELD:
expert_mode_splash();
s->current--;
s->screens[bucket].cb_type = TZ_UI_STREAM_CB_NOCB;
result = false;
} else {
break;
default:
c->list.pairs = s->screens[bucket].pairs;
c->list.callback = NULL;
c->list.startIndex = 0;
c->list.nbPairs = s->screens[bucket].nb_pairs;
c->list.smallCaseForValue = false;
c->list.wrapping = true;
result = true;
break;
}
}

Expand Down
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file not shown.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file not shown.
3 changes: 1 addition & 2 deletions tests/integration/touch/test_blindsign_too_deep.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,7 @@
app.assert_screen("tbtd_review_0")
app.review.next()

app.process_blindsign_warnings("loading_operation")
app.send_apdu("800f82001211020000000c02000000070200000002002a")
app.process_blindsign_warnings(True, "800f82001211020000000c02000000070200000002002a")

app.assert_screen("tbtd_start_review_blindsign")
app.review.next()
Expand Down
21 changes: 12 additions & 9 deletions tests/integration/touch/test_blindsign_too_deep_rejections.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
send_initialize_msg,
send_payload,
verify_err_reject_response,
reject_flow
reject_flow, assert_home_with_code
)

if __name__ == "__main__":
Expand Down Expand Up @@ -46,17 +46,22 @@
app.assert_screen("tbtdr_review_0")
app.review.next()
app.assert_screen("unsafe_operation_warning_1")
app.review.reject()
verify_err_reject_response(app, "unsafe_operation_warning_2")
with app.fading_screen("loading_operation"):
app.review.reject()
app.send_apdu("800f82001211020000000c02000000070200000002002a")
app.assert_screen("unsafe_operation_warning_2")
with app.fading_screen("rejected"):
app.review.enable_blindsign.confirm()
assert_home_with_code(app, "9405")


# Rejecting at review blindsign operation
send_initialize_msg(app, "800f000011048000002c800006c18000000080000000")
send_payload(app, "800f0100eb0502000000f702000000f202000000ed02000000e802000000e302000000de02000000d902000000d402000000cf02000000ca02000000c502000000c002000000bb02000000b602000000b102000000ac02000000a702000000a2020000009d02000000980200000093020000008e02000000890200000084020000007f020000007a02000000750200000070020000006b02000000660200000061020000005c02000000570200000052020000004d02000000480200000043020000003e02000000390200000034020000002f020000002a02000000250200000020020000001b020000001602000000")
app.review.next()
app.assert_screen("tbtdr_review_0")
app.review.next()
app.process_blindsign_warnings("loading_operation")
app.send_apdu("800f82001211020000000c02000000070200000002002a")
app.process_blindsign_warnings(True, "800f82001211020000000c02000000070200000002002a")
verify_err_reject_response(app, "tbtdr_start_review_blindsign")

# Rejecting at blindsign review screen
Expand All @@ -65,8 +70,7 @@
app.review.next()
app.assert_screen("tbtdr_review_0")
app.review.next()
app.process_blindsign_warnings("loading_operation")
app.send_apdu("800f82001211020000000c02000000070200000002002a")
app.process_blindsign_warnings(True, "800f82001211020000000c02000000070200000002002a")
app.assert_screen("tbtdr_start_review_blindsign")
app.review.next()
verify_err_reject_response(app,"tbtd_review_1")
Expand All @@ -78,8 +82,7 @@
app.review.next()
app.assert_screen("tbtdr_review_0")
app.review.next()
app.process_blindsign_warnings("loading_operation")
app.send_apdu("800f82001211020000000c02000000070200000002002a")
app.process_blindsign_warnings(True, "800f82001211020000000c02000000070200000002002a")
app.assert_screen("tbtdr_start_review_blindsign")
app.review.next()
app.assert_screen("tbtd_review_1")
Expand Down
4 changes: 3 additions & 1 deletion tests/integration/touch/test_blindsign_too_large.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,9 @@
send_payload(app, "800f810028050092abf8e3d9e5f8cfd9ae8a9fe5f28ea1d5b5abf1af82dae8a4b68df3d1889eb6f988f5e8d31a")
app.review.next()

app.process_blindsign_warnings("tbtd_start_review_blindsign")
app.process_blindsign_warnings()
app.assert_screen("tbtd_start_review_blindsign")
app.review.next()
app.assert_screen("tbtl_review_1")
app.review.next()
app.assert_screen("operation_sign")
Expand Down
30 changes: 8 additions & 22 deletions tests/integration/touch/test_parsing_errors.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
tezos_app,
send_initialize_msg,
send_payload,
verify_err_reject_response
verify_err_reject_response, reject_flow, verify_parsing_err_reject_response
)

if __name__ == "__main__":
Expand All @@ -33,16 +33,12 @@
send_initialize_msg(app, "800f000011048000002c800006c18000000080000000")
send_payload(app, "800f81005e0100000000000000000000000000000000000000000000000000000000000000006c016e8874874d31c3fbd636e924d5a036a43ec8faa7d0860308362d80d30e01000000000000000000000000000000000000000000ff02000000020316")
app.review.next()
app.assert_screen("unsafe_operation_warning_1")
app.review.enable_blindsign.reject()
verify_err_reject_response(app, "invalid_tag")
verify_parsing_err_reject_response(app, "invalid_tag")

send_initialize_msg(app, "800f000011048000002c800006c18000000080000000")
send_payload(app, "800f81005e03000000000000000000000000000000000000000000000000000000000000000001016e8874874d31c3fbd636e924d5a036a43ec8faa7d0860308362d80d30e01000000000000000000000000000000000000000000ff02000000020316")
app.review.next()
app.assert_screen("unsafe_operation_warning_1")
app.review.enable_blindsign.reject()
verify_err_reject_response(app, "invalid_tag")
verify_parsing_err_reject_response(app, "invalid_tag")

print("Invalid input: 1 byte removed inside")
send_initialize_msg(app, "800f000011048000002c800006c18000000080000000")
Expand All @@ -52,9 +48,7 @@
app.review.next()
app.assert_screen("tpe_review_0_02")
app.review.next()
app.assert_screen("unsafe_operation_warning_1")
app.review.enable_blindsign.reject()
verify_err_reject_response(app, "invalid_tag")
verify_parsing_err_reject_response(app, "invalid_tag")

print("Invalid input: 1 byte introduce at the end")
send_initialize_msg(app, "800f000011048000002c800006c18000000080000000")
Expand All @@ -68,9 +62,7 @@
app.review.next()
app.assert_screen("tpe_review_0_03_full")
app.review.next()
app.assert_screen("unsafe_operation_warning_1")
app.review.enable_blindsign.reject()
verify_err_reject_response(app, "invalid_tag")
verify_parsing_err_reject_response(app, "invalid_tag")

print("Invalid input: 1 byte introduced inside")
send_initialize_msg(app, "800f000011048000002c800006c18000000080000000")
Expand All @@ -80,18 +72,14 @@
app.review.next()
app.assert_screen("tpe_review_0_02_dest_only")
app.review.next()
app.assert_screen("unsafe_operation_warning_1")
app.review.enable_blindsign.reject()
verify_err_reject_response(app, "invalid_tag")
verify_parsing_err_reject_response(app, "invalid_tag")

# full output: 12345678901234567890123456789012345678901234567890123456789012345678901234567890
print("Too Large input")
send_initialize_msg(app, "800f000011048000002c800006c18000000080000000")
send_payload(app, "800f810028050092abf8e3d9e5f8cfd9ae8a9fe5f28ea1d5b5abf1af82dae8a4b68df3d1889eb6f988f5e8d31a")
app.review.next()
app.assert_screen("unsafe_operation_warning_1")
app.review.enable_blindsign.reject()
verify_err_reject_response(app, "too_large")
verify_parsing_err_reject_response(app, "too_large")

# full output: {{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{42}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}
print("Too Deep expression")
Expand All @@ -100,9 +88,7 @@
app.review.next()
app.assert_screen('tpe_review_too_deep_0')
app.review.next()
app.assert_screen("unsafe_operation_warning_1")
app.review.enable_blindsign.reject()
verify_err_reject_response(app, "too_deep")
verify_parsing_err_reject_response(app, "too_deep")

print("wrong last packet")
send_initialize_msg(app, "800f000011048000002c800006c18000000080000000")
Expand Down
Loading

0 comments on commit 1d12e8f

Please sign in to comment.