blob: 03dcf60f961640537c6d322dff45a67328607b6d [file] [log] [blame]
Harald Welte9d3e3822015-11-09 00:50:54 +01001#include <stdint.h>
2#include <stdio.h>
3#include <string.h>
4#include <assert.h>
5#include <stdlib.h>
6
7#include "card_emu.h"
8#include "cardemu_prot.h"
9#include "tc_etu.h"
10#include "req_ctx.h"
11
12/* stub functions required by card_emu.c */
13
14int card_emu_uart_update_fidi(uint8_t uart_chan, unsigned int fidi)
15{
16 printf("uart_update_fidi(uart_chan=%u, fidi=%u)\n", uart_chan, fidi);
17 return 0;
18}
19
20int card_emu_uart_tx(uint8_t uart_chan, uint8_t byte)
21{
Harald Welte6bf8c122016-02-24 21:04:08 +010022 printf("UART_TX(%02x)\n", byte);
Harald Welte9d3e3822015-11-09 00:50:54 +010023 return 1;
24}
25
26void card_emu_uart_enable(uint8_t uart_chan, uint8_t rxtx)
27{
Harald Welteb4362862015-11-14 19:02:33 +010028 char *rts;
29 switch (rxtx) {
30 case 0:
31 rts = "OFF";
32 break;
33 case ENABLE_TX:
34 rts = "TX";
35 break;
36 case ENABLE_RX:
37 rts = "RX";
38 break;
39 default:
40 rts = "unknown";
41 break;
42 };
43
44 printf("uart_enable(uart_chan=%u, %s)\n", uart_chan, rts);
Harald Welte9d3e3822015-11-09 00:50:54 +010045}
46
47void tc_etu_set_wtime(uint8_t tc_chan, uint16_t wtime)
48{
49 printf("tc_etu_set_wtime(tc_chan=%u, wtime=%u)\n", tc_chan, wtime);
50}
51
52void tc_etu_set_etu(uint8_t tc_chan, uint16_t etu)
53{
54 printf("tc_etu_set_etu(tc_chan=%u, etu=%u)\n", tc_chan, etu);
55}
56
57void tc_etu_init(uint8_t chan_nr, void *handle)
58{
Harald Welte6bf8c122016-02-24 21:04:08 +010059 printf("tc_etu_init(tc_chan=%u)\n", chan_nr);
Harald Welte9d3e3822015-11-09 00:50:54 +010060}
61
Harald Welte6bf8c122016-02-24 21:04:08 +010062void tc_etu_enable(uint8_t chan_nr)
63{
64 printf("tc_etu_enable(tc_chan=%u)\n", chan_nr);
65}
Harald Welte9d3e3822015-11-09 00:50:54 +010066
Harald Welte6bf8c122016-02-24 21:04:08 +010067void tc_etu_disable(uint8_t chan_nr)
68{
69 printf("tc_etu_disable(tc_chan=%u)\n", chan_nr);
70}
Harald Welte9d3e3822015-11-09 00:50:54 +010071
72
73#if 0
74/* process a single byte received from the reader */
75void card_emu_process_rx_byte(struct card_handle *ch, uint8_t byte);
76
Harald Welte9d3e3822015-11-09 00:50:54 +010077/* hardware driver informs us that a card I/O signal has changed */
78void card_emu_io_statechg(struct card_handle *ch, enum card_io io, int active);
79
80/* User sets a new ATR to be returned during next card reset */
81int card_emu_set_atr(struct card_handle *ch, const uint8_t *atr, uint8_t len);
82#endif
83
84
85static int verify_atr(struct card_handle *ch)
86{
87 uint8_t atr[4];
88 uint8_t byte;
89 unsigned int i;
90
Harald Welte6bf8c122016-02-24 21:04:08 +010091 printf("receiving + verifying ATR:\n");
Harald Welte9d3e3822015-11-09 00:50:54 +010092 for (i = 0; i < sizeof(atr); i++) {
Harald Welte6bf8c122016-02-24 21:04:08 +010093 assert(card_emu_tx_byte(ch) == 1);
Harald Welte9d3e3822015-11-09 00:50:54 +010094 }
Harald Welte6bf8c122016-02-24 21:04:08 +010095 assert(card_emu_tx_byte(ch) == 0);
Harald Welte9d3e3822015-11-09 00:50:54 +010096
97 return 1;
98}
99
100static void io_start_card(struct card_handle *ch)
101{
Harald Welte9d3e3822015-11-09 00:50:54 +0100102 /* bring the card up from the dead */
103 card_emu_io_statechg(ch, CARD_IO_VCC, 1);
Harald Welte6bf8c122016-02-24 21:04:08 +0100104 assert(card_emu_tx_byte(ch) == 0);
Harald Welte9d3e3822015-11-09 00:50:54 +0100105 card_emu_io_statechg(ch, CARD_IO_CLK, 1);
Harald Welte6bf8c122016-02-24 21:04:08 +0100106 assert(card_emu_tx_byte(ch) == 0);
Harald Welte9d3e3822015-11-09 00:50:54 +0100107 card_emu_io_statechg(ch, CARD_IO_RST, 1);
Harald Welte6bf8c122016-02-24 21:04:08 +0100108 assert(card_emu_tx_byte(ch) == 0);
Harald Welte9d3e3822015-11-09 00:50:54 +0100109
110 /* release from reset and verify th ATR */
111 card_emu_io_statechg(ch, CARD_IO_RST, 0);
112 verify_atr(ch);
113}
114
Harald Welteeef6c2a2016-02-24 22:19:03 +0100115static void reader_send_bytes(struct card_handle *ch, const uint8_t *bytes, unsigned int len)
Harald Welte9d3e3822015-11-09 00:50:54 +0100116{
117 unsigned int i;
Harald Welte84ec2522015-11-14 23:03:50 +0100118 for (i = 0; i < len; i++) {
119 printf("UART_RX(%02x)\n", bytes[i]);
Harald Welte9d3e3822015-11-09 00:50:54 +0100120 card_emu_process_rx_byte(ch, bytes[i]);
Harald Welte84ec2522015-11-14 23:03:50 +0100121 }
Harald Welte9d3e3822015-11-09 00:50:54 +0100122}
123
124static void dump_rctx(struct req_ctx *rctx)
125{
126 struct cardemu_usb_msg_hdr *mh =
127 (struct cardemu_usb_msg_hdr *) rctx->data;
128 struct cardemu_usb_msg_rx_data *rxd;
129 int i;
130
131 printf("req_ctx(%p): state=%u, size=%u, tot_len=%u, idx=%u, data=%p\n",
132 rctx, rctx->state, rctx->size, rctx->tot_len, rctx->idx, rctx->data);
133 printf(" msg_type=%u, seq_nr=%u, data_len=%u\n",
134 mh->msg_type, mh->seq_nr, mh->data_len);
135
136 switch (mh->msg_type) {
137 case CEMU_USB_MSGT_DO_RX_DATA:
138 rxd = (struct cardemu_usb_msg_rx_data *)mh;
139 printf(" flags=%x, data=", rxd->flags);
140 for (i = 0; i < mh->data_len; i++)
141 printf(" %02x", rxd->data[i]);
142 printf("\n");
143 break;
144 }
145}
146
Harald Welte0ab6fcd2016-02-25 00:08:22 +0100147static void get_and_verify_rctx(int state, const char *data, unsigned int len)
148{
149 struct req_ctx *rctx;
150 struct cardemu_usb_msg_tx_data *td;
151 struct cardemu_usb_msg_rx_data *rd;
152
153 rctx = req_ctx_find_get(0, state, RCTX_S_USB_TX_BUSY);
154 assert(rctx);
155 dump_rctx(rctx);
156
157 /* verify the contents of the rctx */
158 switch (state) {
159 case RCTX_S_USB_TX_PENDING:
160 td = (struct cardemu_usb_msg_tx_data *) rctx->data;
161 assert(td->hdr.msg_type == CEMU_USB_MSGT_DO_RX_DATA);
162 assert(td->hdr.data_len == len);
163 assert(!memcmp(td->data, data, len));
164 break;
165#if 0
166 case RCTX_S_UART_RX_PENDING:
167 rd = (struct cardemu_usb_msg_rx_data *) rctx->data;
168 assert(rd->hdr.data_len == len);
169 assert(!memcmp(rd->data, data, len));
170 break;
171#endif
172 default:
173 assert(0);
174 }
175
176 /* free the req_ctx, indicating it has fully arrived on the host */
177 req_ctx_set_state(rctx, RCTX_S_FREE);
178}
179
Harald Welteb4362862015-11-14 19:02:33 +0100180/* emulate a TPDU header being sent by the reader/phone */
Harald Welteeef6c2a2016-02-24 22:19:03 +0100181static void rdr_send_tpdu_hdr(struct card_handle *ch, const uint8_t *tpdu_hdr)
Harald Welte9d3e3822015-11-09 00:50:54 +0100182{
Harald Welte9d3e3822015-11-09 00:50:54 +0100183 /* we don't want a receive context to become available during
184 * the first four bytes */
Harald Welteeef6c2a2016-02-24 22:19:03 +0100185 reader_send_bytes(ch, tpdu_hdr, 4);
Harald Welte84ec2522015-11-14 23:03:50 +0100186 assert(!req_ctx_find_get(0, RCTX_S_USB_TX_PENDING, RCTX_S_USB_TX_BUSY));
Harald Welte9d3e3822015-11-09 00:50:54 +0100187
Harald Welteeef6c2a2016-02-24 22:19:03 +0100188 reader_send_bytes(ch, tpdu_hdr+4, 1);
Harald Welte9d3e3822015-11-09 00:50:54 +0100189 /* but then after the final byte of the TPDU header, we want a
190 * receive context to be available for USB transmission */
Harald Welte0ab6fcd2016-02-25 00:08:22 +0100191 get_and_verify_rctx(RCTX_S_USB_TX_PENDING, tpdu_hdr, 5);
Harald Welteb4362862015-11-14 19:02:33 +0100192}
193
194/* emulate a CEMU_USB_MSGT_DT_TX_DATA received from USB */
Harald Welteeef6c2a2016-02-24 22:19:03 +0100195static void host_to_device_data(const uint8_t *data, uint16_t len, unsigned int flags)
Harald Welteb4362862015-11-14 19:02:33 +0100196{
197 struct req_ctx *rctx;
Harald Welte84ec2522015-11-14 23:03:50 +0100198 struct cardemu_usb_msg_tx_data *rd;
Harald Welteb4362862015-11-14 19:02:33 +0100199
200 /* allocate a free req_ctx */
Harald Welte84ec2522015-11-14 23:03:50 +0100201 rctx = req_ctx_find_get(0, RCTX_S_FREE, RCTX_S_USB_RX_BUSY);
Harald Welteb4362862015-11-14 19:02:33 +0100202 assert(rctx);
203
204 /* initialize the header */
205 rd = (struct cardemu_usb_msg_rx_data *) rctx->data;
Harald Welteeef6c2a2016-02-24 22:19:03 +0100206 rctx->tot_len = sizeof(*rd) + len;
Harald Welteb4362862015-11-14 19:02:33 +0100207 cardemu_hdr_set(&rd->hdr, CEMU_USB_MSGT_DT_TX_DATA);
Harald Welteeef6c2a2016-02-24 22:19:03 +0100208 rd->flags = flags;
Harald Welteb4362862015-11-14 19:02:33 +0100209 /* copy data and set length */
210 rd->hdr.data_len = len;
211 memcpy(rd->data, data, len);
212
213 /* hand the req_ctx to the UART transmit code */
214 req_ctx_set_state(rctx, RCTX_S_UART_TX_PENDING);
215}
216
Harald Welteeef6c2a2016-02-24 22:19:03 +0100217/* card-transmit any pending characters */
218static int card_tx_print_chars(struct card_handle *ch)
Harald Welteb4362862015-11-14 19:02:33 +0100219{
220 uint8_t byte;
221 int count = 0;
222
Harald Welte6bf8c122016-02-24 21:04:08 +0100223 while (card_emu_tx_byte(ch)) {
Harald Welteb4362862015-11-14 19:02:33 +0100224 count++;
225 }
226 return count;
Harald Welte9d3e3822015-11-09 00:50:54 +0100227}
228
Harald Welte84ec2522015-11-14 23:03:50 +0100229const uint8_t tpdu_hdr_sel_mf[] = { 0xA0, 0xA4, 0x00, 0x00, 0x00 };
Harald Welte61bb30e2015-11-14 23:44:14 +0100230const uint8_t tpdu_pb_sw[] = { 0x90, 0x00 };
Harald Welte9d3e3822015-11-09 00:50:54 +0100231
Harald Weltec043e642016-02-24 23:26:55 +0100232static void
233test_tpdu_reader2card(struct card_handle *ch, const uint8_t *hdr, const uint8_t *body, uint8_t body_len)
234{
Harald Weltec043e642016-02-24 23:26:55 +0100235 printf("\n==> transmitting APDU (HDR + PB + card-RX)\n");
236
237 /* emulate the reader sending a TPDU header */
238 rdr_send_tpdu_hdr(ch, hdr);
239 /* we shouldn't have any pending card-TX yet */
240 assert(!card_tx_print_chars(ch));
Harald Welte0ab6fcd2016-02-25 00:08:22 +0100241
Harald Weltec043e642016-02-24 23:26:55 +0100242 /* card emulator PC sends a singly byte PB response via USB */
243 host_to_device_data(hdr+1, 1, CEMU_DATA_F_FINAL | CEMU_DATA_F_PB_AND_RX);
244 /* card actually sends that single PB */
245 assert(card_tx_print_chars(ch) == 1);
246
247 /* emulate more characters from reader to card */
248 reader_send_bytes(ch, body, body_len);
249
250 /* check if we have received them on the USB side */
Harald Welte0ab6fcd2016-02-25 00:08:22 +0100251 get_and_verify_rctx(RCTX_S_USB_TX_PENDING, body, body_len);
Harald Weltec043e642016-02-24 23:26:55 +0100252
253 /* ensure there is no extra data received on usb */
254 assert(!req_ctx_find_get(0, RCTX_S_USB_TX_PENDING, RCTX_S_USB_TX_BUSY));
255
256 /* card emulator sends SW via USB */
257 host_to_device_data(tpdu_pb_sw, sizeof(tpdu_pb_sw),
258 CEMU_DATA_F_FINAL | CEMU_DATA_F_PB_AND_TX);
259 /* obtain any pending tx chars */
260 assert(card_tx_print_chars(ch) == sizeof(tpdu_pb_sw));
261
262 /* simulate some clock stop */
263 card_emu_io_statechg(ch, CARD_IO_CLK, 0);
264 card_emu_io_statechg(ch, CARD_IO_CLK, 1);
265}
266
267static void
268test_tpdu_card2reader(struct card_handle *ch, const uint8_t *hdr, const uint8_t *body, uint8_t body_len)
269{
270 printf("\n==> transmitting APDU (HDR + PB + card-TX)\n");
271
272 /* emulate the reader sending a TPDU header */
273 rdr_send_tpdu_hdr(ch, hdr);
274 assert(!card_tx_print_chars(ch));
275
276 /* card emulator PC sends a response PB via USB */
277 host_to_device_data(hdr+1, 1, CEMU_DATA_F_PB_AND_TX);
278
279 /* card actually sends that PB */
280 assert(card_tx_print_chars(ch) == 1);
281
282 /* emulate more characters from card to reader */
283 host_to_device_data(body, body_len, 0);
284 /* obtain those bytes as they arrvive on the card */
285 assert(card_tx_print_chars(ch) == body_len);
286
287 /* ensure there is no extra data received on usb */
288 assert(!req_ctx_find_get(0, RCTX_S_USB_TX_PENDING, RCTX_S_USB_TX_BUSY));
289
290 /* card emulator sends SW via USB */
291 host_to_device_data(tpdu_pb_sw, sizeof(tpdu_pb_sw), CEMU_DATA_F_FINAL);
292
293 /* obtain any pending tx chars */
294 assert(card_tx_print_chars(ch) == sizeof(tpdu_pb_sw));
295
296 /* simulate some clock stop */
297 card_emu_io_statechg(ch, CARD_IO_CLK, 0);
298 card_emu_io_statechg(ch, CARD_IO_CLK, 1);
299}
300
301
Harald Welteeef6c2a2016-02-24 22:19:03 +0100302/* READ RECORD (offset 0, 10 bytes) */
303const uint8_t tpdu_hdr_read_rec[] = { 0xA0, 0xB2, 0x00, 0x00, 0x0A };
304const uint8_t tpdu_body_read_rec[] = { 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09 };
305
306/* WRITE RECORD */
307const uint8_t tpdu_hdr_write_rec[] = { 0xA0, 0xD2, 0x00, 0x00, 0x07 };
308const uint8_t tpdu_body_write_rec[] = { 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06 };
Harald Welte84ec2522015-11-14 23:03:50 +0100309
Harald Welte9d3e3822015-11-09 00:50:54 +0100310int main(int argc, char **argv)
311{
312 struct card_handle *ch;
Harald Welte84ec2522015-11-14 23:03:50 +0100313 unsigned int i;
Harald Welte9d3e3822015-11-09 00:50:54 +0100314
315 req_ctx_init();
316
317 ch = card_emu_init(0, 23, 42);
318 assert(ch);
319
Harald Welteb4362862015-11-14 19:02:33 +0100320 /* start up the card (VCC/RST, ATR) */
Harald Welte9d3e3822015-11-09 00:50:54 +0100321 io_start_card(ch);
Harald Welteeef6c2a2016-02-24 22:19:03 +0100322 assert(!card_tx_print_chars(ch));
Harald Welte9d3e3822015-11-09 00:50:54 +0100323
Harald Welte84ec2522015-11-14 23:03:50 +0100324 for (i = 0; i < 2; i++) {
Harald Weltec043e642016-02-24 23:26:55 +0100325 test_tpdu_reader2card(ch, tpdu_hdr_write_rec, tpdu_body_write_rec, sizeof(tpdu_body_write_rec));
Harald Welte84ec2522015-11-14 23:03:50 +0100326
Harald Weltec043e642016-02-24 23:26:55 +0100327 test_tpdu_card2reader(ch, tpdu_hdr_read_rec, tpdu_body_read_rec, sizeof(tpdu_body_read_rec));
Harald Welte84ec2522015-11-14 23:03:50 +0100328 }
Harald Welte9d3e3822015-11-09 00:50:54 +0100329
330 exit(0);
331}