Harald Welte | 9d3e382 | 2015-11-09 00:50:54 +0100 | [diff] [blame] | 1 | #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" |
Harald Welte | 8e7fca3 | 2017-05-07 16:14:33 +0200 | [diff] [blame] | 10 | #include "usb_buf.h" |
| 11 | |
| 12 | #define PHONE_DATAIN 1 |
| 13 | #define PHONE_INT 2 |
| 14 | #define PHONE_DATAOUT 3 |
Harald Welte | 9d3e382 | 2015-11-09 00:50:54 +0100 | [diff] [blame] | 15 | |
| 16 | /* stub functions required by card_emu.c */ |
| 17 | |
Harald Welte | a1cd0f3 | 2017-02-27 14:29:07 +0100 | [diff] [blame] | 18 | void card_emu_uart_wait_tx_idle(uint8_t uart_chan) |
| 19 | { |
| 20 | } |
| 21 | |
Harald Welte | 9d3e382 | 2015-11-09 00:50:54 +0100 | [diff] [blame] | 22 | int card_emu_uart_update_fidi(uint8_t uart_chan, unsigned int fidi) |
| 23 | { |
| 24 | printf("uart_update_fidi(uart_chan=%u, fidi=%u)\n", uart_chan, fidi); |
| 25 | return 0; |
| 26 | } |
| 27 | |
Harald Welte | 86d047b | 2016-02-25 00:26:17 +0100 | [diff] [blame] | 28 | /* a buffer in which we store those bytes send by the UART towards the card |
| 29 | * reader, so we can verify in test cases what was actually written */ |
| 30 | static uint8_t tx_debug_buf[1024]; |
| 31 | static unsigned int tx_debug_buf_idx; |
| 32 | |
Harald Welte | 9d3e382 | 2015-11-09 00:50:54 +0100 | [diff] [blame] | 33 | int card_emu_uart_tx(uint8_t uart_chan, uint8_t byte) |
| 34 | { |
Harald Welte | 6bf8c12 | 2016-02-24 21:04:08 +0100 | [diff] [blame] | 35 | printf("UART_TX(%02x)\n", byte); |
Harald Welte | 86d047b | 2016-02-25 00:26:17 +0100 | [diff] [blame] | 36 | tx_debug_buf[tx_debug_buf_idx++] = byte; |
Harald Welte | 9d3e382 | 2015-11-09 00:50:54 +0100 | [diff] [blame] | 37 | return 1; |
| 38 | } |
| 39 | |
Harald Welte | 86d047b | 2016-02-25 00:26:17 +0100 | [diff] [blame] | 40 | static void reader_check_and_clear(const uint8_t *data, unsigned int len) |
| 41 | { |
| 42 | assert(len == tx_debug_buf_idx); |
| 43 | assert(!memcmp(tx_debug_buf, data, len)); |
| 44 | tx_debug_buf_idx = 0; |
| 45 | } |
| 46 | |
Harald Welte | 9d3e382 | 2015-11-09 00:50:54 +0100 | [diff] [blame] | 47 | void card_emu_uart_enable(uint8_t uart_chan, uint8_t rxtx) |
| 48 | { |
Harald Welte | b436286 | 2015-11-14 19:02:33 +0100 | [diff] [blame] | 49 | char *rts; |
| 50 | switch (rxtx) { |
| 51 | case 0: |
| 52 | rts = "OFF"; |
| 53 | break; |
| 54 | case ENABLE_TX: |
| 55 | rts = "TX"; |
| 56 | break; |
| 57 | case ENABLE_RX: |
| 58 | rts = "RX"; |
| 59 | break; |
| 60 | default: |
| 61 | rts = "unknown"; |
| 62 | break; |
| 63 | }; |
| 64 | |
| 65 | printf("uart_enable(uart_chan=%u, %s)\n", uart_chan, rts); |
Harald Welte | 9d3e382 | 2015-11-09 00:50:54 +0100 | [diff] [blame] | 66 | } |
| 67 | |
| 68 | void tc_etu_set_wtime(uint8_t tc_chan, uint16_t wtime) |
| 69 | { |
| 70 | printf("tc_etu_set_wtime(tc_chan=%u, wtime=%u)\n", tc_chan, wtime); |
| 71 | } |
| 72 | |
| 73 | void tc_etu_set_etu(uint8_t tc_chan, uint16_t etu) |
| 74 | { |
| 75 | printf("tc_etu_set_etu(tc_chan=%u, etu=%u)\n", tc_chan, etu); |
| 76 | } |
| 77 | |
| 78 | void tc_etu_init(uint8_t chan_nr, void *handle) |
| 79 | { |
Harald Welte | 6bf8c12 | 2016-02-24 21:04:08 +0100 | [diff] [blame] | 80 | printf("tc_etu_init(tc_chan=%u)\n", chan_nr); |
Harald Welte | 9d3e382 | 2015-11-09 00:50:54 +0100 | [diff] [blame] | 81 | } |
| 82 | |
Harald Welte | 6bf8c12 | 2016-02-24 21:04:08 +0100 | [diff] [blame] | 83 | void tc_etu_enable(uint8_t chan_nr) |
| 84 | { |
| 85 | printf("tc_etu_enable(tc_chan=%u)\n", chan_nr); |
| 86 | } |
Harald Welte | 9d3e382 | 2015-11-09 00:50:54 +0100 | [diff] [blame] | 87 | |
Harald Welte | 6bf8c12 | 2016-02-24 21:04:08 +0100 | [diff] [blame] | 88 | void tc_etu_disable(uint8_t chan_nr) |
| 89 | { |
| 90 | printf("tc_etu_disable(tc_chan=%u)\n", chan_nr); |
| 91 | } |
Harald Welte | 9d3e382 | 2015-11-09 00:50:54 +0100 | [diff] [blame] | 92 | |
Harald Welte | 86d047b | 2016-02-25 00:26:17 +0100 | [diff] [blame] | 93 | const uint8_t atr[] = { 0x3b, 0x02, 0x14, 0x50 }; |
Harald Welte | 9d3e382 | 2015-11-09 00:50:54 +0100 | [diff] [blame] | 94 | |
| 95 | static int verify_atr(struct card_handle *ch) |
| 96 | { |
Harald Welte | 9d3e382 | 2015-11-09 00:50:54 +0100 | [diff] [blame] | 97 | unsigned int i; |
| 98 | |
Harald Welte | 6bf8c12 | 2016-02-24 21:04:08 +0100 | [diff] [blame] | 99 | printf("receiving + verifying ATR:\n"); |
Harald Welte | 9d3e382 | 2015-11-09 00:50:54 +0100 | [diff] [blame] | 100 | for (i = 0; i < sizeof(atr); i++) { |
Harald Welte | 6bf8c12 | 2016-02-24 21:04:08 +0100 | [diff] [blame] | 101 | assert(card_emu_tx_byte(ch) == 1); |
Harald Welte | 9d3e382 | 2015-11-09 00:50:54 +0100 | [diff] [blame] | 102 | } |
Harald Welte | 6bf8c12 | 2016-02-24 21:04:08 +0100 | [diff] [blame] | 103 | assert(card_emu_tx_byte(ch) == 0); |
Harald Welte | 86d047b | 2016-02-25 00:26:17 +0100 | [diff] [blame] | 104 | reader_check_and_clear(atr, sizeof(atr)); |
Harald Welte | 9d3e382 | 2015-11-09 00:50:54 +0100 | [diff] [blame] | 105 | |
| 106 | return 1; |
| 107 | } |
| 108 | |
| 109 | static void io_start_card(struct card_handle *ch) |
| 110 | { |
Harald Welte | 86d047b | 2016-02-25 00:26:17 +0100 | [diff] [blame] | 111 | card_emu_set_atr(ch, atr, sizeof(atr)); |
| 112 | |
Harald Welte | 9d3e382 | 2015-11-09 00:50:54 +0100 | [diff] [blame] | 113 | /* bring the card up from the dead */ |
| 114 | card_emu_io_statechg(ch, CARD_IO_VCC, 1); |
Harald Welte | 6bf8c12 | 2016-02-24 21:04:08 +0100 | [diff] [blame] | 115 | assert(card_emu_tx_byte(ch) == 0); |
Harald Welte | 9d3e382 | 2015-11-09 00:50:54 +0100 | [diff] [blame] | 116 | card_emu_io_statechg(ch, CARD_IO_CLK, 1); |
Harald Welte | 6bf8c12 | 2016-02-24 21:04:08 +0100 | [diff] [blame] | 117 | assert(card_emu_tx_byte(ch) == 0); |
Harald Welte | 9d3e382 | 2015-11-09 00:50:54 +0100 | [diff] [blame] | 118 | card_emu_io_statechg(ch, CARD_IO_RST, 1); |
Harald Welte | 6bf8c12 | 2016-02-24 21:04:08 +0100 | [diff] [blame] | 119 | assert(card_emu_tx_byte(ch) == 0); |
Harald Welte | 9d3e382 | 2015-11-09 00:50:54 +0100 | [diff] [blame] | 120 | |
| 121 | /* release from reset and verify th ATR */ |
| 122 | card_emu_io_statechg(ch, CARD_IO_RST, 0); |
| 123 | verify_atr(ch); |
| 124 | } |
| 125 | |
Harald Welte | eef6c2a | 2016-02-24 22:19:03 +0100 | [diff] [blame] | 126 | static void reader_send_bytes(struct card_handle *ch, const uint8_t *bytes, unsigned int len) |
Harald Welte | 9d3e382 | 2015-11-09 00:50:54 +0100 | [diff] [blame] | 127 | { |
| 128 | unsigned int i; |
Harald Welte | 84ec252 | 2015-11-14 23:03:50 +0100 | [diff] [blame] | 129 | for (i = 0; i < len; i++) { |
| 130 | printf("UART_RX(%02x)\n", bytes[i]); |
Harald Welte | 9d3e382 | 2015-11-09 00:50:54 +0100 | [diff] [blame] | 131 | card_emu_process_rx_byte(ch, bytes[i]); |
Harald Welte | 84ec252 | 2015-11-14 23:03:50 +0100 | [diff] [blame] | 132 | } |
Harald Welte | 9d3e382 | 2015-11-09 00:50:54 +0100 | [diff] [blame] | 133 | } |
| 134 | |
Harald Welte | 8e7fca3 | 2017-05-07 16:14:33 +0200 | [diff] [blame] | 135 | static void dump_rctx(struct msgb *msg) |
Harald Welte | 9d3e382 | 2015-11-09 00:50:54 +0100 | [diff] [blame] | 136 | { |
| 137 | struct cardemu_usb_msg_hdr *mh = |
Harald Welte | 8e7fca3 | 2017-05-07 16:14:33 +0200 | [diff] [blame] | 138 | (struct cardemu_usb_msg_hdr *) msg->l1h; |
Harald Welte | 9d3e382 | 2015-11-09 00:50:54 +0100 | [diff] [blame] | 139 | struct cardemu_usb_msg_rx_data *rxd; |
| 140 | int i; |
Harald Welte | 8e7fca3 | 2017-05-07 16:14:33 +0200 | [diff] [blame] | 141 | #if 0 |
Harald Welte | 9d3e382 | 2015-11-09 00:50:54 +0100 | [diff] [blame] | 142 | |
| 143 | printf("req_ctx(%p): state=%u, size=%u, tot_len=%u, idx=%u, data=%p\n", |
| 144 | rctx, rctx->state, rctx->size, rctx->tot_len, rctx->idx, rctx->data); |
Harald Welte | a1cd0f3 | 2017-02-27 14:29:07 +0100 | [diff] [blame] | 145 | printf(" msg_type=%u, seq_nr=%u, msg_len=%u\n", |
| 146 | mh->msg_type, mh->seq_nr, mh->msg_len); |
Harald Welte | 8e7fca3 | 2017-05-07 16:14:33 +0200 | [diff] [blame] | 147 | #endif |
| 148 | printf("%s\n", msgb_hexdump(msg)); |
Harald Welte | 9d3e382 | 2015-11-09 00:50:54 +0100 | [diff] [blame] | 149 | |
| 150 | switch (mh->msg_type) { |
| 151 | case CEMU_USB_MSGT_DO_RX_DATA: |
| 152 | rxd = (struct cardemu_usb_msg_rx_data *)mh; |
| 153 | printf(" flags=%x, data=", rxd->flags); |
Harald Welte | d295b92 | 2016-03-18 21:01:36 +0100 | [diff] [blame] | 154 | for (i = 0; i < rxd->data_len; i++) |
Harald Welte | 9d3e382 | 2015-11-09 00:50:54 +0100 | [diff] [blame] | 155 | printf(" %02x", rxd->data[i]); |
| 156 | printf("\n"); |
| 157 | break; |
| 158 | } |
| 159 | } |
| 160 | |
Harald Welte | 8e7fca3 | 2017-05-07 16:14:33 +0200 | [diff] [blame] | 161 | static void get_and_verify_rctx(uint8_t ep, const uint8_t *data, unsigned int len) |
Harald Welte | 0ab6fcd | 2016-02-25 00:08:22 +0100 | [diff] [blame] | 162 | { |
Harald Welte | 8e7fca3 | 2017-05-07 16:14:33 +0200 | [diff] [blame] | 163 | struct llist_head *queue = usb_get_queue(ep); |
| 164 | struct msgb *msg; |
Harald Welte | 0ab6fcd | 2016-02-25 00:08:22 +0100 | [diff] [blame] | 165 | struct cardemu_usb_msg_tx_data *td; |
| 166 | struct cardemu_usb_msg_rx_data *rd; |
Harald Welte | 8e7fca3 | 2017-05-07 16:14:33 +0200 | [diff] [blame] | 167 | struct cardemu_usb_msg_hdr *mh; |
Harald Welte | 0ab6fcd | 2016-02-25 00:08:22 +0100 | [diff] [blame] | 168 | |
Harald Welte | 8e7fca3 | 2017-05-07 16:14:33 +0200 | [diff] [blame] | 169 | assert(queue); |
| 170 | msg = msgb_dequeue(queue); |
| 171 | assert(msg); |
| 172 | dump_rctx(msg); |
| 173 | assert(msg->l1h); |
| 174 | mh = (struct cardemu_usb_msg_hdr *) msg->l1h; |
Harald Welte | 0ab6fcd | 2016-02-25 00:08:22 +0100 | [diff] [blame] | 175 | |
| 176 | /* verify the contents of the rctx */ |
Harald Welte | 8e7fca3 | 2017-05-07 16:14:33 +0200 | [diff] [blame] | 177 | switch (mh->msg_type) { |
| 178 | case CEMU_USB_MSGT_DO_RX_DATA: |
| 179 | rd = (struct cardemu_usb_msg_rx_data *) msg->l1h; |
| 180 | assert(rd->hdr.msg_type == CEMU_USB_MSGT_DO_RX_DATA); |
| 181 | assert(rd->data_len == len); |
| 182 | assert(!memcmp(rd->data, data, len)); |
Harald Welte | 0ab6fcd | 2016-02-25 00:08:22 +0100 | [diff] [blame] | 183 | break; |
| 184 | #if 0 |
| 185 | case RCTX_S_UART_RX_PENDING: |
| 186 | rd = (struct cardemu_usb_msg_rx_data *) rctx->data; |
Harald Welte | d295b92 | 2016-03-18 21:01:36 +0100 | [diff] [blame] | 187 | assert(rd->data_len == len); |
Harald Welte | 0ab6fcd | 2016-02-25 00:08:22 +0100 | [diff] [blame] | 188 | assert(!memcmp(rd->data, data, len)); |
| 189 | break; |
| 190 | #endif |
| 191 | default: |
| 192 | assert(0); |
| 193 | } |
| 194 | |
| 195 | /* free the req_ctx, indicating it has fully arrived on the host */ |
Harald Welte | 8e7fca3 | 2017-05-07 16:14:33 +0200 | [diff] [blame] | 196 | usb_buf_free(msg); |
Harald Welte | 0ab6fcd | 2016-02-25 00:08:22 +0100 | [diff] [blame] | 197 | } |
| 198 | |
Harald Welte | 4ba66d0 | 2016-02-25 19:38:56 +0100 | [diff] [blame] | 199 | static void get_and_verify_rctx_pps(const uint8_t *data, unsigned int len) |
| 200 | { |
Harald Welte | 8e7fca3 | 2017-05-07 16:14:33 +0200 | [diff] [blame] | 201 | struct llist_head *queue = usb_get_queue(PHONE_DATAIN); |
| 202 | struct msgb *msg; |
Harald Welte | 4ba66d0 | 2016-02-25 19:38:56 +0100 | [diff] [blame] | 203 | struct cardemu_usb_msg_pts_info *ptsi; |
| 204 | |
Harald Welte | 8e7fca3 | 2017-05-07 16:14:33 +0200 | [diff] [blame] | 205 | assert(queue); |
| 206 | msg = msgb_dequeue(queue); |
| 207 | assert(msg); |
| 208 | dump_rctx(msg); |
| 209 | assert(msg->l1h); |
Harald Welte | 4ba66d0 | 2016-02-25 19:38:56 +0100 | [diff] [blame] | 210 | |
Harald Welte | 8e7fca3 | 2017-05-07 16:14:33 +0200 | [diff] [blame] | 211 | ptsi = (struct cardemu_usb_msg_pts_info *) msg->l1h; |
Harald Welte | 4ba66d0 | 2016-02-25 19:38:56 +0100 | [diff] [blame] | 212 | /* FIXME: verify */ |
| 213 | assert(ptsi->hdr.msg_type == CEMU_USB_MSGT_DO_PTS); |
| 214 | assert(!memcmp(ptsi->req, data, len)); |
| 215 | assert(!memcmp(ptsi->resp, data, len)); |
| 216 | |
| 217 | /* free the req_ctx, indicating it has fully arrived on the host */ |
Harald Welte | 8e7fca3 | 2017-05-07 16:14:33 +0200 | [diff] [blame] | 218 | usb_buf_free(msg); |
Harald Welte | 4ba66d0 | 2016-02-25 19:38:56 +0100 | [diff] [blame] | 219 | } |
| 220 | |
Harald Welte | b436286 | 2015-11-14 19:02:33 +0100 | [diff] [blame] | 221 | /* emulate a TPDU header being sent by the reader/phone */ |
Harald Welte | eef6c2a | 2016-02-24 22:19:03 +0100 | [diff] [blame] | 222 | static void rdr_send_tpdu_hdr(struct card_handle *ch, const uint8_t *tpdu_hdr) |
Harald Welte | 9d3e382 | 2015-11-09 00:50:54 +0100 | [diff] [blame] | 223 | { |
Harald Welte | 8e7fca3 | 2017-05-07 16:14:33 +0200 | [diff] [blame] | 224 | struct llist_head *queue = usb_get_queue(PHONE_DATAIN); |
| 225 | |
Harald Welte | 9d3e382 | 2015-11-09 00:50:54 +0100 | [diff] [blame] | 226 | /* we don't want a receive context to become available during |
| 227 | * the first four bytes */ |
Harald Welte | eef6c2a | 2016-02-24 22:19:03 +0100 | [diff] [blame] | 228 | reader_send_bytes(ch, tpdu_hdr, 4); |
Harald Welte | 8e7fca3 | 2017-05-07 16:14:33 +0200 | [diff] [blame] | 229 | assert(llist_empty(queue)); |
Harald Welte | 9d3e382 | 2015-11-09 00:50:54 +0100 | [diff] [blame] | 230 | |
Harald Welte | eef6c2a | 2016-02-24 22:19:03 +0100 | [diff] [blame] | 231 | reader_send_bytes(ch, tpdu_hdr+4, 1); |
Harald Welte | 9d3e382 | 2015-11-09 00:50:54 +0100 | [diff] [blame] | 232 | /* but then after the final byte of the TPDU header, we want a |
| 233 | * receive context to be available for USB transmission */ |
Harald Welte | 8e7fca3 | 2017-05-07 16:14:33 +0200 | [diff] [blame] | 234 | get_and_verify_rctx(PHONE_DATAIN, tpdu_hdr, 5); |
Harald Welte | b436286 | 2015-11-14 19:02:33 +0100 | [diff] [blame] | 235 | } |
| 236 | |
| 237 | /* emulate a CEMU_USB_MSGT_DT_TX_DATA received from USB */ |
Harald Welte | 8e7fca3 | 2017-05-07 16:14:33 +0200 | [diff] [blame] | 238 | static void host_to_device_data(struct card_handle *ch, const uint8_t *data, uint16_t len, |
| 239 | unsigned int flags) |
Harald Welte | b436286 | 2015-11-14 19:02:33 +0100 | [diff] [blame] | 240 | { |
Harald Welte | 8e7fca3 | 2017-05-07 16:14:33 +0200 | [diff] [blame] | 241 | struct msgb *msg; |
Harald Welte | 84ec252 | 2015-11-14 23:03:50 +0100 | [diff] [blame] | 242 | struct cardemu_usb_msg_tx_data *rd; |
Harald Welte | 8e7fca3 | 2017-05-07 16:14:33 +0200 | [diff] [blame] | 243 | struct llist_head *queue; |
Harald Welte | b436286 | 2015-11-14 19:02:33 +0100 | [diff] [blame] | 244 | |
| 245 | /* allocate a free req_ctx */ |
Harald Welte | 8e7fca3 | 2017-05-07 16:14:33 +0200 | [diff] [blame] | 246 | msg = usb_buf_alloc(PHONE_DATAOUT); |
| 247 | assert(msg); |
| 248 | msg->l1h = msg->head; |
Harald Welte | b436286 | 2015-11-14 19:02:33 +0100 | [diff] [blame] | 249 | |
| 250 | /* initialize the header */ |
Harald Welte | 8e7fca3 | 2017-05-07 16:14:33 +0200 | [diff] [blame] | 251 | rd = (struct cardemu_usb_msg_tx_data *) msgb_put(msg, sizeof(*rd) + len); |
Harald Welte | b436286 | 2015-11-14 19:02:33 +0100 | [diff] [blame] | 252 | cardemu_hdr_set(&rd->hdr, CEMU_USB_MSGT_DT_TX_DATA); |
Harald Welte | eef6c2a | 2016-02-24 22:19:03 +0100 | [diff] [blame] | 253 | rd->flags = flags; |
Harald Welte | b436286 | 2015-11-14 19:02:33 +0100 | [diff] [blame] | 254 | /* copy data and set length */ |
Harald Welte | d295b92 | 2016-03-18 21:01:36 +0100 | [diff] [blame] | 255 | rd->data_len = len; |
Harald Welte | b436286 | 2015-11-14 19:02:33 +0100 | [diff] [blame] | 256 | memcpy(rd->data, data, len); |
Harald Welte | d295b92 | 2016-03-18 21:01:36 +0100 | [diff] [blame] | 257 | rd->hdr.msg_len = sizeof(*rd) + len; |
Harald Welte | b436286 | 2015-11-14 19:02:33 +0100 | [diff] [blame] | 258 | |
| 259 | /* hand the req_ctx to the UART transmit code */ |
Harald Welte | 8e7fca3 | 2017-05-07 16:14:33 +0200 | [diff] [blame] | 260 | queue = card_emu_get_uart_tx_queue(ch); |
| 261 | assert(queue); |
| 262 | msgb_enqueue(queue, msg); |
Harald Welte | b436286 | 2015-11-14 19:02:33 +0100 | [diff] [blame] | 263 | } |
| 264 | |
Harald Welte | eef6c2a | 2016-02-24 22:19:03 +0100 | [diff] [blame] | 265 | /* card-transmit any pending characters */ |
Harald Welte | 86d047b | 2016-02-25 00:26:17 +0100 | [diff] [blame] | 266 | static int card_tx_verify_chars(struct card_handle *ch, const uint8_t *data, unsigned int data_len) |
Harald Welte | b436286 | 2015-11-14 19:02:33 +0100 | [diff] [blame] | 267 | { |
Harald Welte | b436286 | 2015-11-14 19:02:33 +0100 | [diff] [blame] | 268 | int count = 0; |
| 269 | |
Harald Welte | 6bf8c12 | 2016-02-24 21:04:08 +0100 | [diff] [blame] | 270 | while (card_emu_tx_byte(ch)) { |
Harald Welte | b436286 | 2015-11-14 19:02:33 +0100 | [diff] [blame] | 271 | count++; |
| 272 | } |
Harald Welte | 86d047b | 2016-02-25 00:26:17 +0100 | [diff] [blame] | 273 | |
| 274 | assert(count == data_len); |
| 275 | reader_check_and_clear(data, data_len); |
| 276 | |
Harald Welte | b436286 | 2015-11-14 19:02:33 +0100 | [diff] [blame] | 277 | return count; |
Harald Welte | 9d3e382 | 2015-11-09 00:50:54 +0100 | [diff] [blame] | 278 | } |
| 279 | |
Harald Welte | 84ec252 | 2015-11-14 23:03:50 +0100 | [diff] [blame] | 280 | const uint8_t tpdu_hdr_sel_mf[] = { 0xA0, 0xA4, 0x00, 0x00, 0x00 }; |
Harald Welte | 61bb30e | 2015-11-14 23:44:14 +0100 | [diff] [blame] | 281 | const uint8_t tpdu_pb_sw[] = { 0x90, 0x00 }; |
Harald Welte | 9d3e382 | 2015-11-09 00:50:54 +0100 | [diff] [blame] | 282 | |
Harald Welte | c043e64 | 2016-02-24 23:26:55 +0100 | [diff] [blame] | 283 | static void |
| 284 | test_tpdu_reader2card(struct card_handle *ch, const uint8_t *hdr, const uint8_t *body, uint8_t body_len) |
| 285 | { |
Harald Welte | c043e64 | 2016-02-24 23:26:55 +0100 | [diff] [blame] | 286 | printf("\n==> transmitting APDU (HDR + PB + card-RX)\n"); |
| 287 | |
| 288 | /* emulate the reader sending a TPDU header */ |
| 289 | rdr_send_tpdu_hdr(ch, hdr); |
| 290 | /* we shouldn't have any pending card-TX yet */ |
Harald Welte | 86d047b | 2016-02-25 00:26:17 +0100 | [diff] [blame] | 291 | card_tx_verify_chars(ch, NULL, 0); |
Harald Welte | 0ab6fcd | 2016-02-25 00:08:22 +0100 | [diff] [blame] | 292 | |
Harald Welte | c043e64 | 2016-02-24 23:26:55 +0100 | [diff] [blame] | 293 | /* card emulator PC sends a singly byte PB response via USB */ |
Harald Welte | 8e7fca3 | 2017-05-07 16:14:33 +0200 | [diff] [blame] | 294 | host_to_device_data(ch, hdr+1, 1, CEMU_DATA_F_FINAL | CEMU_DATA_F_PB_AND_RX); |
Harald Welte | c043e64 | 2016-02-24 23:26:55 +0100 | [diff] [blame] | 295 | /* card actually sends that single PB */ |
Harald Welte | 86d047b | 2016-02-25 00:26:17 +0100 | [diff] [blame] | 296 | card_tx_verify_chars(ch, hdr+1, 1); |
Harald Welte | c043e64 | 2016-02-24 23:26:55 +0100 | [diff] [blame] | 297 | |
| 298 | /* emulate more characters from reader to card */ |
| 299 | reader_send_bytes(ch, body, body_len); |
| 300 | |
| 301 | /* check if we have received them on the USB side */ |
Harald Welte | 8e7fca3 | 2017-05-07 16:14:33 +0200 | [diff] [blame] | 302 | get_and_verify_rctx(PHONE_DATAIN, body, body_len); |
Harald Welte | c043e64 | 2016-02-24 23:26:55 +0100 | [diff] [blame] | 303 | |
| 304 | /* ensure there is no extra data received on usb */ |
Harald Welte | 8e7fca3 | 2017-05-07 16:14:33 +0200 | [diff] [blame] | 305 | assert(llist_empty(usb_get_queue(PHONE_DATAOUT))); |
Harald Welte | c043e64 | 2016-02-24 23:26:55 +0100 | [diff] [blame] | 306 | |
| 307 | /* card emulator sends SW via USB */ |
Harald Welte | 8e7fca3 | 2017-05-07 16:14:33 +0200 | [diff] [blame] | 308 | host_to_device_data(ch, tpdu_pb_sw, sizeof(tpdu_pb_sw), |
Harald Welte | c043e64 | 2016-02-24 23:26:55 +0100 | [diff] [blame] | 309 | CEMU_DATA_F_FINAL | CEMU_DATA_F_PB_AND_TX); |
| 310 | /* obtain any pending tx chars */ |
Harald Welte | 86d047b | 2016-02-25 00:26:17 +0100 | [diff] [blame] | 311 | card_tx_verify_chars(ch, tpdu_pb_sw, sizeof(tpdu_pb_sw)); |
Harald Welte | c043e64 | 2016-02-24 23:26:55 +0100 | [diff] [blame] | 312 | |
| 313 | /* simulate some clock stop */ |
| 314 | card_emu_io_statechg(ch, CARD_IO_CLK, 0); |
| 315 | card_emu_io_statechg(ch, CARD_IO_CLK, 1); |
| 316 | } |
| 317 | |
| 318 | static void |
| 319 | test_tpdu_card2reader(struct card_handle *ch, const uint8_t *hdr, const uint8_t *body, uint8_t body_len) |
| 320 | { |
| 321 | printf("\n==> transmitting APDU (HDR + PB + card-TX)\n"); |
| 322 | |
| 323 | /* emulate the reader sending a TPDU header */ |
| 324 | rdr_send_tpdu_hdr(ch, hdr); |
Harald Welte | 86d047b | 2016-02-25 00:26:17 +0100 | [diff] [blame] | 325 | card_tx_verify_chars(ch, NULL, 0); |
Harald Welte | c043e64 | 2016-02-24 23:26:55 +0100 | [diff] [blame] | 326 | |
| 327 | /* card emulator PC sends a response PB via USB */ |
Harald Welte | 8e7fca3 | 2017-05-07 16:14:33 +0200 | [diff] [blame] | 328 | host_to_device_data(ch, hdr+1, 1, CEMU_DATA_F_PB_AND_TX); |
Harald Welte | c043e64 | 2016-02-24 23:26:55 +0100 | [diff] [blame] | 329 | |
| 330 | /* card actually sends that PB */ |
Harald Welte | 86d047b | 2016-02-25 00:26:17 +0100 | [diff] [blame] | 331 | card_tx_verify_chars(ch, hdr+1, 1); |
Harald Welte | c043e64 | 2016-02-24 23:26:55 +0100 | [diff] [blame] | 332 | |
| 333 | /* emulate more characters from card to reader */ |
Harald Welte | 8e7fca3 | 2017-05-07 16:14:33 +0200 | [diff] [blame] | 334 | host_to_device_data(ch, body, body_len, 0); |
Harald Welte | c043e64 | 2016-02-24 23:26:55 +0100 | [diff] [blame] | 335 | /* obtain those bytes as they arrvive on the card */ |
Harald Welte | 86d047b | 2016-02-25 00:26:17 +0100 | [diff] [blame] | 336 | card_tx_verify_chars(ch, body, body_len); |
Harald Welte | c043e64 | 2016-02-24 23:26:55 +0100 | [diff] [blame] | 337 | |
| 338 | /* ensure there is no extra data received on usb */ |
Harald Welte | 8e7fca3 | 2017-05-07 16:14:33 +0200 | [diff] [blame] | 339 | assert(llist_empty(usb_get_queue(PHONE_DATAOUT))); |
Harald Welte | c043e64 | 2016-02-24 23:26:55 +0100 | [diff] [blame] | 340 | |
| 341 | /* card emulator sends SW via USB */ |
Harald Welte | 8e7fca3 | 2017-05-07 16:14:33 +0200 | [diff] [blame] | 342 | host_to_device_data(ch, tpdu_pb_sw, sizeof(tpdu_pb_sw), CEMU_DATA_F_FINAL); |
Harald Welte | c043e64 | 2016-02-24 23:26:55 +0100 | [diff] [blame] | 343 | |
| 344 | /* obtain any pending tx chars */ |
Harald Welte | 86d047b | 2016-02-25 00:26:17 +0100 | [diff] [blame] | 345 | card_tx_verify_chars(ch, tpdu_pb_sw, sizeof(tpdu_pb_sw)); |
Harald Welte | c043e64 | 2016-02-24 23:26:55 +0100 | [diff] [blame] | 346 | |
| 347 | /* simulate some clock stop */ |
| 348 | card_emu_io_statechg(ch, CARD_IO_CLK, 0); |
| 349 | card_emu_io_statechg(ch, CARD_IO_CLK, 1); |
| 350 | } |
| 351 | |
Harald Welte | 4ba66d0 | 2016-02-25 19:38:56 +0100 | [diff] [blame] | 352 | const uint8_t pps[] = { |
| 353 | /* PPSS identifies the PPS request or response and is set to |
| 354 | * 'FF'. */ |
| 355 | 0xFF, // PPSS |
| 356 | /* In PPS0, each bit 5, 6 or 7 set to 1 indicates the presence |
| 357 | * of an optional byte PPS 1 , PPS 2 , PPS 3 , |
| 358 | * respectively. Bits 4 to 1 encode a type T to propose a |
| 359 | * transmission protocol. Bit 8 is reserved for future |
| 360 | * use and shall be set to 0. */ |
| 361 | 0b00010000, // PPS0: PPS1 present |
| 362 | 0x00, // PPS1 proposed Fi/Di value |
| 363 | 0xFF ^ 0b00010000// PCK |
| 364 | }; |
| 365 | |
| 366 | static void |
| 367 | test_ppss(struct card_handle *ch) |
| 368 | { |
| 369 | reader_send_bytes(ch, pps, sizeof(pps)); |
| 370 | get_and_verify_rctx_pps(pps, sizeof(pps)); |
| 371 | card_tx_verify_chars(ch, pps, sizeof(pps)); |
| 372 | } |
Harald Welte | c043e64 | 2016-02-24 23:26:55 +0100 | [diff] [blame] | 373 | |
Harald Welte | eef6c2a | 2016-02-24 22:19:03 +0100 | [diff] [blame] | 374 | /* READ RECORD (offset 0, 10 bytes) */ |
| 375 | const uint8_t tpdu_hdr_read_rec[] = { 0xA0, 0xB2, 0x00, 0x00, 0x0A }; |
| 376 | const uint8_t tpdu_body_read_rec[] = { 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09 }; |
| 377 | |
| 378 | /* WRITE RECORD */ |
| 379 | const uint8_t tpdu_hdr_write_rec[] = { 0xA0, 0xD2, 0x00, 0x00, 0x07 }; |
| 380 | const uint8_t tpdu_body_write_rec[] = { 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06 }; |
Harald Welte | 84ec252 | 2015-11-14 23:03:50 +0100 | [diff] [blame] | 381 | |
Harald Welte | 9d3e382 | 2015-11-09 00:50:54 +0100 | [diff] [blame] | 382 | int main(int argc, char **argv) |
| 383 | { |
| 384 | struct card_handle *ch; |
Harald Welte | 84ec252 | 2015-11-14 23:03:50 +0100 | [diff] [blame] | 385 | unsigned int i; |
Harald Welte | 9d3e382 | 2015-11-09 00:50:54 +0100 | [diff] [blame] | 386 | |
Harald Welte | 8e7fca3 | 2017-05-07 16:14:33 +0200 | [diff] [blame] | 387 | ch = card_emu_init(0, 23, 42, PHONE_DATAIN, PHONE_INT); |
Harald Welte | 9d3e382 | 2015-11-09 00:50:54 +0100 | [diff] [blame] | 388 | assert(ch); |
| 389 | |
Harald Welte | 8e7fca3 | 2017-05-07 16:14:33 +0200 | [diff] [blame] | 390 | usb_buf_init(); |
| 391 | |
Harald Welte | b436286 | 2015-11-14 19:02:33 +0100 | [diff] [blame] | 392 | /* start up the card (VCC/RST, ATR) */ |
Harald Welte | 9d3e382 | 2015-11-09 00:50:54 +0100 | [diff] [blame] | 393 | io_start_card(ch); |
Harald Welte | 86d047b | 2016-02-25 00:26:17 +0100 | [diff] [blame] | 394 | card_tx_verify_chars(ch, NULL, 0); |
Harald Welte | 9d3e382 | 2015-11-09 00:50:54 +0100 | [diff] [blame] | 395 | |
Harald Welte | 4ba66d0 | 2016-02-25 19:38:56 +0100 | [diff] [blame] | 396 | test_ppss(ch); |
| 397 | |
Harald Welte | 84ec252 | 2015-11-14 23:03:50 +0100 | [diff] [blame] | 398 | for (i = 0; i < 2; i++) { |
Harald Welte | c043e64 | 2016-02-24 23:26:55 +0100 | [diff] [blame] | 399 | test_tpdu_reader2card(ch, tpdu_hdr_write_rec, tpdu_body_write_rec, sizeof(tpdu_body_write_rec)); |
Harald Welte | 84ec252 | 2015-11-14 23:03:50 +0100 | [diff] [blame] | 400 | |
Harald Welte | c043e64 | 2016-02-24 23:26:55 +0100 | [diff] [blame] | 401 | test_tpdu_card2reader(ch, tpdu_hdr_read_rec, tpdu_body_read_rec, sizeof(tpdu_body_read_rec)); |
Harald Welte | 84ec252 | 2015-11-14 23:03:50 +0100 | [diff] [blame] | 402 | } |
Harald Welte | 9d3e382 | 2015-11-09 00:50:54 +0100 | [diff] [blame] | 403 | |
| 404 | exit(0); |
| 405 | } |