Skip to content

Commit

Permalink
card_emu: Correctly handle SW after reader -> card data phase
Browse files Browse the repository at this point in the history
  • Loading branch information
laf0rge committed Nov 14, 2015
1 parent 84ec252 commit 61bb30e
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 7 deletions.
10 changes: 5 additions & 5 deletions firmware/src_simtrace/card_emu.c
Original file line number Diff line number Diff line change
Expand Up @@ -386,7 +386,7 @@ static unsigned int t0_num_data_bytes(uint8_t p3, int reader_to_card)
}

/* add a just-received TPDU byte (from reader) to USB buffer */
static enum iso7816_3_card_state add_tpdu_byte(struct card_handle *ch, uint8_t byte)
static void add_tpdu_byte(struct card_handle *ch, uint8_t byte)
{
struct req_ctx *rctx;
struct cardemu_usb_msg_rx_data *rd;
Expand Down Expand Up @@ -415,11 +415,10 @@ static enum iso7816_3_card_state add_tpdu_byte(struct card_handle *ch, uint8_t b
if (rctx->tot_len >= sizeof(*rd) + num_data_bytes) {
rd->flags |= CEMU_DATA_F_FINAL;
flush_rx_buffer(ch);
return ISO_S_WAIT_TPDU;
/* We need to transmit the SW now, */
set_tpdu_state(ch, TPDU_S_WAIT_TX);
} else if (rctx->tot_len >= rctx->size)
flush_rx_buffer(ch);

return ISO_S_IN_TPDU;
}

static void set_tpdu_state(struct card_handle *ch, enum tpdu_state new_ts)
Expand Down Expand Up @@ -530,7 +529,8 @@ process_byte_tpdu(struct card_handle *ch, uint8_t byte)
send_tpdu_header(ch);
break;
case TPDU_S_WAIT_RX:
return add_tpdu_byte(ch, byte);
add_tpdu_byte(ch, byte);
break;
default:
TRACE_DEBUG("process_byte_tpdu() in invalid state %u\n",
ch->tpdu.state);
Expand Down
9 changes: 7 additions & 2 deletions firmware/test/card_emu_tests.c
Original file line number Diff line number Diff line change
Expand Up @@ -203,7 +203,7 @@ static int print_tx_chars(struct card_handle *ch)
}

const uint8_t tpdu_hdr_sel_mf[] = { 0xA0, 0xA4, 0x00, 0x00, 0x00 };
const uint8_t tpdu_pb_sw[] = { 0xA4, 0x90, 0x00 };
const uint8_t tpdu_pb_sw[] = { 0x90, 0x00 };

const uint8_t tpdu_hdr_upd_bin[] = { 0xA0, 0xB2, 0x00, 0x00, 0x0A };
const uint8_t tpdu_body_upd_bin[] = { 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09 };
Expand All @@ -224,7 +224,7 @@ int main(int argc, char **argv)
assert(!print_tx_chars(ch));

for (i = 0; i < 2; i++) {
printf("\n==> transmitting APDU (PB + SW $%u)\n", i);
printf("\n==> transmitting APDU (SW $%u)\n", i);
/* emulate the reader sending a TPDU header */
send_tpdu_hdr(ch, tpdu_hdr_sel_mf);
assert(!print_tx_chars(ch));
Expand Down Expand Up @@ -260,6 +260,11 @@ int main(int argc, char **argv)
/* ensure there is no extra data received on usb */
assert(!req_ctx_find_get(0, RCTX_S_USB_TX_PENDING, RCTX_S_USB_TX_BUSY));

/* card emulator sends SW via USB */
host_to_device_data(tpdu_pb_sw, sizeof(tpdu_pb_sw), 0);
/* obtain any pending tx chars */
assert(print_tx_chars(ch) == sizeof(tpdu_pb_sw));

/* simulate some clock stop */
card_emu_io_statechg(ch, CARD_IO_CLK, 0);
card_emu_io_statechg(ch, CARD_IO_CLK, 1);
Expand Down

0 comments on commit 61bb30e

Please sign in to comment.