Skip to content

Commit

Permalink
STAX: clearsigning flow
Browse files Browse the repository at this point in the history
Clearsigning for STAX is slightly more complex, due to having to
support the 'skip' flow, that allows a user to go directly to the
signing page.
  • Loading branch information
emturner committed Sep 22, 2023
1 parent 36e7ffe commit de38840
Show file tree
Hide file tree
Showing 40 changed files with 446 additions and 35 deletions.
8 changes: 8 additions & 0 deletions app/src/apdu_sign.c
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,7 @@ refill()
tz_parser_state *st = &global.apdu.sign.u.clear.parser_state;
TZ_PREAMBLE(("void"));

skip:
while (!TZ_IS_BLOCKED(tz_operation_parser_step(st)))
;
PRINTF("[DEBUG] refill(errno: %s) \n", tz_parser_result_name(st->errno));
Expand All @@ -165,6 +166,13 @@ refill()

tz_parser_flush_up_to(st, global.line_buf, TZ_UI_STREAM_CONTENTS_SIZE,
wrote);

// Do as much parsing as we can in one go when skipping.
// TODO: perhaps when skipping we could only skip the current
// operation, rather than the whole rest-of-batch
if (global.apdu.sign.u.clear.skip_to_sign)
goto skip;

break;
case TZ_BLO_FEED_ME:
TZ_CHECK(send_continue());
Expand Down
1 change: 1 addition & 0 deletions app/src/apdu_sign.h
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ typedef struct {
struct {
size_t total_length;
tz_parser_state parser_state;
bool skip_to_sign;
} clear;
struct {
uint8_t tag;
Expand Down
16 changes: 10 additions & 6 deletions app/src/ui_stream.c
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ const bagl_icon_details_t C_icon_rien = {0, 0, 1, NULL, NULL};

// Model

#ifdef HAVE_BAGL
void
tz_ui_stream_init(void (*cb)(uint8_t))
{
Expand All @@ -46,18 +47,22 @@ tz_ui_stream_init(void (*cb)(uint8_t))
s->total = -1;
FUNC_LEAVE();
}
#endif

void
tz_ui_stream_push_accept_reject(void)
{
FUNC_ENTER(("void"));
#ifdef HAVE_BAGL
tz_ui_stream_push(TZ_UI_STREAM_CB_ACCEPT, "Accept?",
"Press both buttons to accept.", TZ_UI_ICON_TICK);
tz_ui_stream_push(TZ_UI_STREAM_CB_REJECT, "Reject?",
"Press both buttons to reject.", TZ_UI_ICON_CROSS);
#endif
FUNC_LEAVE();
}

#ifdef HAVE_BAGL
void
tz_ui_stream_close()
{
Expand All @@ -71,8 +76,8 @@ tz_ui_stream_close()
s->full = true;
FUNC_LEAVE();
}
#endif // HAVE_BAGL

#ifdef HAVE_BAGL
uint8_t
tz_ui_max_line_chars(const char *value, int length)
{
Expand All @@ -88,7 +93,7 @@ tz_ui_max_line_chars(const char *value, int length)
#ifdef TARGET_NANOS
will_fit = se_get_cropped_length(value, will_fit, BAGL_WIDTH,
BAGL_ENCODING_LATIN1);
#else
#elif defined(HAVE_BAGL)
uint8_t width;
will_fit++;
do {
Expand Down Expand Up @@ -195,7 +200,6 @@ tz_ui_stream_pushl(tz_ui_cb_type_t type, const char *title, const char *value,

return offset;
}
#endif // HAVE_BAGL

tz_ui_cb_type_t
tz_ui_stream_get_type(void)
Expand Down Expand Up @@ -232,7 +236,7 @@ succ()
}
FUNC_LEAVE();
}
#endif // HAVE_BAGL
#endif // HAVE_BAGL

// View

Expand Down Expand Up @@ -403,17 +407,17 @@ tz_ui_stream_start(void)
FUNC_LEAVE();
}

#ifdef HAVE_BAGL
void
tz_ui_stream()
{
FUNC_ENTER(("void"));

#ifdef HAVE_BAGL
tz_ui_stream_t *s = &global.stream;
if (s->pressed_right)
succ();

redisplay();
#endif // HAVE_BAGL
FUNC_LEAVE();
}
#endif // HAVE_BAGL
6 changes: 6 additions & 0 deletions app/src/ui_stream.h
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,14 @@

#define TZ_UI_STREAM_HISTORY_SCREENS 8
#define TZ_UI_STREAM_TITLE_WIDTH TZ_SCREEN_WITDH_BETWEEN_ICONS_BOLD_11PX

#ifdef HAVE_BAGL
#define TZ_UI_STREAM_CONTENTS_WIDTH TZ_SCREEN_WITDH_FULL_REGULAR_11PX
#define TZ_UI_STREAM_CONTENTS_LINES (TZ_SCREEN_LINES_11PX - 1)
#elif HAVE_NBGL
#define TZ_UI_STREAM_CONTENTS_WIDTH 40
#define TZ_UI_STREAM_CONTENTS_LINES 1
#endif

#define TZ_UI_STREAM_CONTENTS_SIZE \
(TZ_UI_STREAM_CONTENTS_WIDTH * TZ_UI_STREAM_CONTENTS_LINES)
Expand Down
209 changes: 188 additions & 21 deletions app/src/ui_stream_nbgl.c
Original file line number Diff line number Diff line change
Expand Up @@ -17,36 +17,203 @@

#ifdef HAVE_NBGL

#include <ux.h>

#include "globals.h"
#include "nbgl_use_case.h"

size_t
tz_ui_stream_push(tz_ui_cb_type_t type, const char *title, const char *value,
tz_ui_icon_t icon)
{
return tz_ui_stream_pushl(type, title, value, -1, icon);
void tz_reject(void) {
tz_ui_stream_t *s = &global.stream;

FUNC_ENTER(("void"));

s->cb(TZ_UI_STREAM_CB_REJECT);
ui_home_init();

FUNC_LEAVE();
}

size_t
tz_ui_stream_push_all(__attribute__((unused)) tz_ui_cb_type_t type,
__attribute__((unused)) const char *title,
const char *value,
__attribute__((unused)) tz_ui_icon_t icon)
{
return strlen(value);
void tz_reject_ui(void) {

// Stax can move into user input at any point in the flow
global.apdu.sign.step = SIGN_ST_WAIT_USER_INPUT;

nbgl_useCaseStatus("Rejected", false, tz_reject);
}


void tz_accept_blindsign_cb(void) {
tz_ui_stream_t *s = &global.stream;

FUNC_ENTER(("void"));

global.apdu.sign.step = SIGN_ST_WAIT_USER_INPUT;
s->cb(TZ_UI_STREAM_CB_ACCEPT);
ui_home_init();

FUNC_LEAVE();
}

void tz_choice_ui(bool accept) {
FUNC_ENTER(("accept=%d", accept));

if (accept) {
nbgl_useCaseStatus("SIGNING\nSUCCESSFUL", true, tz_accept_blindsign_cb);
} else {
tz_reject_ui();
}

FUNC_LEAVE();
}

size_t
tz_ui_stream_pushl(__attribute__((unused)) tz_ui_cb_type_t type,
__attribute__((unused)) const char *title,
const char *value, ssize_t max,
__attribute__((unused)) tz_ui_icon_t icon)
bool tz_ui_nav_cb(uint8_t, nbgl_pageContent_t*);

void tz_ui_continue(void) {
FUNC_ENTER(("void"));

tz_ui_stream_t *s = &global.stream;

if (!s->full)
s->cb(TZ_UI_STREAM_CB_REFILL);

FUNC_LEAVE();
return;
}

void tz_ui_start(void) {
FUNC_ENTER(("void"));

nbgl_useCaseForwardOnlyReview("Reject",
NULL,
tz_ui_nav_cb,
tz_choice_ui);

FUNC_LEAVE();
return;
}


void tz_ui_stream_init (void (*cb)(uint8_t)) {
tz_ui_stream_t *s = &global.stream;

FUNC_ENTER(("cb=%p", cb));
memset(s, 0x0, sizeof(*s));
s->cb = cb;
s->full = false;
s->current = 0;
s->total = -1;

nbgl_useCaseReviewStart(&C_tezos,
"Review request to sign operation",
NULL,
"Reject request",
tz_ui_start,
tz_reject_ui
);

FUNC_LEAVE();
}

static char tz_ui_pair_title[20];
static char tz_ui_pair_content[80];
static nbgl_layoutTagValue_t tz_ui_pair;
static nbgl_layoutTagValueList_t tz_ui_tag_value_list;

static nbgl_layoutTagValue_t* tz_ui_getTagValuePair(__attribute__((unused))uint8_t pairIndex) {
FUNC_ENTER(("pairIndex=%d", pairIndex));

tz_ui_stream_t *s = &global.stream;

PRINTF("[DEBUG] pressed_right=%d\n", s->pressed_right);

if (s->current < s->total && s->pressed_right) {
s->current++;
s->pressed_right = false;
}

tz_ui_pair_title[0] = 0;
tz_ui_pair_content[0] = 0;

size_t bucket = s->current % TZ_UI_STREAM_HISTORY_SCREENS;
STRLCPY(tz_ui_pair_title, s->screens[bucket].title);
STRLCPY(tz_ui_pair_content, s->screens[bucket].body[0]);

tz_ui_pair.item = tz_ui_pair_title;
tz_ui_pair.value = tz_ui_pair_content;

PRINTF("show title=%s, content=%s from bucket=%d\n", tz_ui_pair_title, tz_ui_pair_content, bucket);
FUNC_LEAVE();
return &tz_ui_pair;
}


void
tz_ui_stream_close()
{
size_t length = strlen(value);
tz_ui_stream_t *s = &global.stream;

if (max != -1)
length = MIN(length, (size_t)max);
FUNC_ENTER(("void"));
if (s->full) {
PRINTF("trying to close already closed stream display");
THROW(EXC_UNKNOWN);
}
s->full = true;

return length;
if (global.apdu.sign.u.clear.skip_to_sign) {
tz_ui_start();
}

FUNC_LEAVE();
}

bool tz_ui_nav_cb(uint8_t page, nbgl_pageContent_t* content) {
FUNC_ENTER(("page=%d, content=%p", page, content));

tz_ui_stream_t *s = &global.stream;

if (page > 0 && !s->pressed_right) {
s->pressed_right = true;
}

PRINTF("pressed_right=%d, current=%d, total=%d, full=%d\n", s->pressed_right,
s->current, s->total, s->full);

if (page == LAST_PAGE_FOR_REVIEW) {
// skipped
PRINTF("Skip requested");
global.apdu.sign.u.clear.skip_to_sign = true;
tz_ui_continue();
} else if ((s->current == s->total) && !s->full) {
tz_ui_continue();
}

if (!s->full && !global.apdu.sign.u.clear.skip_to_sign) {
tz_ui_tag_value_list.pairs = NULL;
tz_ui_tag_value_list.callback = tz_ui_getTagValuePair;
tz_ui_tag_value_list.startIndex = 0;
tz_ui_tag_value_list.nbPairs = 1;
tz_ui_tag_value_list.smallCaseForValue = false;
tz_ui_tag_value_list.wrapping = false;

content->type = TAG_VALUE_LIST;
content->tagValueList = tz_ui_tag_value_list;
} else {
content->type = INFO_LONG_PRESS;
content->infoLongPress.icon = &C_tezos;
content->infoLongPress.text = "Sign";
content->infoLongPress.longPressText = "Sign";
}

FUNC_LEAVE();

return (!global.apdu.sign.u.clear.skip_to_sign || s->full);
}

__attribute__((noreturn)) void tz_ui_stream() {
FUNC_ENTER(("void"));

//tz_ui_stream_t *s = &global.stream;

Check notice

Code scanning / CodeQL

Commented-out code Note

This comment appears to contain commented-out code.

//THROW(ASYNC_EXCEPTION);

Check notice

Code scanning / CodeQL

Commented-out code Note

This comment appears to contain commented-out code.
}
#endif
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@
start_speculos "$seed"
expect_full_text 'Tezos Wallet' 'ready for' 'safe signing'
send_async_apdus \
800f000011048000002c800006c18000000080000000 "expect_apdu_return 9000" \
800f0100eb030000000000000000000000000000000000000000000000000000000000000000ce00ffdd6102321bc251e4a5190ad5b12b251069d9b4904e02030400000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000c63966303966323935326433343532386337333366393436313563666333396263353535363139666335353064643461363762613232303863653865383637616133643133613665663939646662653332633639373461613961323135306432316563613239633333343965353963313362393038316631 "expect_apdu_return 9000" \
800f82004f63313162343430616334643334353564656462653465653064653135613861663632306434633836323437643964313332646531626236646132336435666639643864666664613232626139613834 "expect_apdu_return c08f5e1a02d15b05c4066b43fc31aa1ccad30f6c7a18f44723e5af0b6584292236e919219e90793ef502e8883f5317206277607438695933fcb954f4ef451db19628a114880836193c755ddda4bf188b9764231975b2c5ecb64bc4bdc9c459039000"
800f000011048000002c800006c18000000080000000 "expect_apdu_return 9000" \
800f0100eb030000000000000000000000000000000000000000000000000000000000000000ce00ffdd6102321bc251e4a5190ad5b12b251069d9b4904e02030400000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000c63966303966323935326433343532386337333366393436313563666333396263353535363139666335353064643461363762613232303863653865383637616133643133613665663939646662653332633639373461613961323135306432316563613239633333343965353963313362393038316631 "expect_apdu_return 9000" \
800f82004f63313162343430616334643334353564656462653465653064653135613861663632306434633836323437643964313332646531626236646132336435666639643864666664613232626139613834 "expect_apdu_return c08f5e1a02d15b05c4066b43fc31aa1ccad30f6c7a18f44723e5af0b6584292236e919219e90793ef502e8883f5317206277607438695933fcb954f4ef451db19628a114880836193c755ddda4bf188b9764231975b2c5ecb64bc4bdc9c459039000"
expect_section_content nanosp 'Operation (0)' 'SR: execute outbox message'
press_button right
expect_section_content nanosp 'Fee' '0.01 tz'
Expand Down
4 changes: 2 additions & 2 deletions tests/integration/nanosp/test_sign_transaction.sh
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@
start_speculos "$seed"
expect_full_text 'Tezos Wallet' 'ready for' 'safe signing'
send_async_apdus \
800f000011048000002c800006c18000000080000000 "expect_apdu_return 9000
800f000011048000002c800006c18000000080000000 "expect_apdu_return 9000
"\
800f81005e0300000000000000000000000000000000000000000000000000000000000000006c016e8874874d31c3fbd636e924d5a036a43ec8faa7d0860308362d80d30e01000000000000000000000000000000000000000000ff02000000020316 "expect_apdu_return f6d5fa0e79cac216e25104938ac873ca17ee9d7f06763719293b413cf2ed475cf63d045a1cc9f73eee5775c5d496fa9d3aa9ae57fb97217f746a8728639795b7b2220e84ce5759ed111399ea3263d810c230d6a4fffcb6e82797c5ca673a17089000
800f81005e0300000000000000000000000000000000000000000000000000000000000000006c016e8874874d31c3fbd636e924d5a036a43ec8faa7d0860308362d80d30e01000000000000000000000000000000000000000000ff02000000020316 "expect_apdu_return f6d5fa0e79cac216e25104938ac873ca17ee9d7f06763719293b413cf2ed475cf63d045a1cc9f73eee5775c5d496fa9d3aa9ae57fb97217f746a8728639795b7b2220e84ce5759ed111399ea3263d810c230d6a4fffcb6e82797c5ca673a17089000
"
expect_full_text 'Operation (0)' 'Transaction'
press_button right
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@
start_speculos "$seed"
expect_full_text 'Tezos Wallet' 'ready for' 'safe signing'
send_async_apdus \
800f000011048000002c800006c18000000080000000 "expect_apdu_return 9000" \
800f0100eb030000000000000000000000000000000000000000000000000000000000000000ce00ffdd6102321bc251e4a5190ad5b12b251069d9b4904e02030400000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000c63966303966323935326433343532386337333366393436313563666333396263353535363139666335353064643461363762613232303863653865383637616133643133613665663939646662653332633639373461613961323135306432316563613239633333343965353963313362393038316631 "expect_apdu_return 9000" \
800f82004f63313162343430616334643334353564656462653465653064653135613861663632306434633836323437643964313332646531626236646132336435666639643864666664613232626139613834 "expect_apdu_return c08f5e1a02d15b05c4066b43fc31aa1ccad30f6c7a18f44723e5af0b6584292236e919219e90793ef502e8883f5317206277607438695933fcb954f4ef451db19628a114880836193c755ddda4bf188b9764231975b2c5ecb64bc4bdc9c459039000"
800f000011048000002c800006c18000000080000000 "expect_apdu_return 9000" \
800f0100eb030000000000000000000000000000000000000000000000000000000000000000ce00ffdd6102321bc251e4a5190ad5b12b251069d9b4904e02030400000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000c63966303966323935326433343532386337333366393436313563666333396263353535363139666335353064643461363762613232303863653865383637616133643133613665663939646662653332633639373461613961323135306432316563613239633333343965353963313362393038316631 "expect_apdu_return 9000" \
800f82004f63313162343430616334643334353564656462653465653064653135613861663632306434633836323437643964313332646531626236646132336435666639643864666664613232626139613834 "expect_apdu_return c08f5e1a02d15b05c4066b43fc31aa1ccad30f6c7a18f44723e5af0b6584292236e919219e90793ef502e8883f5317206277607438695933fcb954f4ef451db19628a114880836193c755ddda4bf188b9764231975b2c5ecb64bc4bdc9c459039000"
expect_section_content nanox 'Operation (0)' 'SR: execute outbox message'
press_button right
expect_section_content nanox 'Fee' '0.01 tz'
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.
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.
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

0 comments on commit de38840

Please sign in to comment.