card_emu: Correctly handle SW after reader -> card data phase
diff --git a/firmware/src_simtrace/card_emu.c b/firmware/src_simtrace/card_emu.c
index 093e9a1..78f747c 100644
--- a/firmware/src_simtrace/card_emu.c
+++ b/firmware/src_simtrace/card_emu.c
@@ -386,7 +386,7 @@
}
/* 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;
@@ -415,11 +415,10 @@
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)
@@ -530,7 +529,8 @@
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);
diff --git a/firmware/test/card_emu_tests.c b/firmware/test/card_emu_tests.c
index d175945..e2fa9ee 100644
--- a/firmware/test/card_emu_tests.c
+++ b/firmware/test/card_emu_tests.c
@@ -203,7 +203,7 @@
}
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 };
@@ -224,7 +224,7 @@
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));
@@ -260,6 +260,11 @@
/* 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);