card_emu: Correctly handle SW after reader -> card data phase

This commit is contained in:
Harald Welte
2015-11-14 23:44:14 +01:00
parent 84ec252ff4
commit 61bb30e4ea
2 changed files with 12 additions and 7 deletions

View File

@@ -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;
@@ -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)
@@ -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);

View File

@@ -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 };
@@ -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));
@@ -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);