Harald Welte | 9d3e382 | 2015-11-09 00:50:54 +0100 | [diff] [blame] | 1 | /* ISO7816-3 state machine for the card side */ |
| 2 | /* (C) 2010-2015 by Harald Welte <hwelte@hmw-consulting.de> |
| 3 | * |
| 4 | * This program is free software; you can redistribute it and/or modify |
| 5 | * it under the terms of the GNU General Public License as published by |
| 6 | * the Free Software Foundation; either version 2 of the License, or |
| 7 | * (at your option) any later version. |
| 8 | * |
| 9 | * This program is distributed in the hope that it will be useful, |
| 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 | * GNU General Public License for more details. |
| 13 | * |
| 14 | * You should have received a copy of the GNU General Public License |
| 15 | * along with this program; if not, write to the Free Software |
| 16 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
| 17 | * |
| 18 | */ |
| 19 | |
Harald Welte | 4c473da | 2015-11-14 13:31:11 +0100 | [diff] [blame] | 20 | #include <assert.h> |
Harald Welte | 9d3e382 | 2015-11-09 00:50:54 +0100 | [diff] [blame] | 21 | #include <errno.h> |
| 22 | #include <string.h> |
| 23 | #include <stdint.h> |
| 24 | #include <sys/types.h> |
| 25 | |
| 26 | #include "utils.h" |
| 27 | #include "trace.h" |
| 28 | #include "iso7816_fidi.h" |
| 29 | #include "tc_etu.h" |
| 30 | #include "card_emu.h" |
| 31 | #include "req_ctx.h" |
| 32 | #include "cardemu_prot.h" |
Harald Welte | 54cb3d0 | 2016-02-29 14:12:40 +0100 | [diff] [blame] | 33 | #include "linuxlist.h" |
Harald Welte | 9d3e382 | 2015-11-09 00:50:54 +0100 | [diff] [blame] | 34 | |
| 35 | |
| 36 | #define NUM_SLOTS 2 |
| 37 | |
| 38 | #define ISO7816_3_INIT_WTIME 9600 |
| 39 | #define ISO7816_3_DEFAULT_WI 10 |
| 40 | #define ISO7816_3_ATR_LEN_MAX (1+32) /* TS plus 32 chars */ |
| 41 | |
| 42 | #define ISO7816_3_PB_NULL 0x60 |
| 43 | |
| 44 | enum iso7816_3_card_state { |
| 45 | ISO_S_WAIT_POWER, /* waiting for power being applied */ |
| 46 | ISO_S_WAIT_CLK, /* waiting for clock being applied */ |
| 47 | ISO_S_WAIT_RST, /* waiting for reset being released */ |
| 48 | ISO_S_WAIT_ATR, /* waiting for start of ATR */ |
| 49 | ISO_S_IN_ATR, /* transmitting ATR to reader */ |
| 50 | ISO_S_IN_PTS, /* transmitting ATR to reader */ |
| 51 | ISO_S_WAIT_TPDU, /* waiting for data from reader */ |
| 52 | ISO_S_IN_TPDU, /* inside a TPDU */ |
| 53 | }; |
| 54 | |
| 55 | /* detailed sub-states of ISO_S_IN_PTS */ |
| 56 | enum pts_state { |
| 57 | PTS_S_WAIT_REQ_PTSS, |
| 58 | PTS_S_WAIT_REQ_PTS0, |
| 59 | PTS_S_WAIT_REQ_PTS1, |
| 60 | PTS_S_WAIT_REQ_PTS2, |
| 61 | PTS_S_WAIT_REQ_PTS3, |
| 62 | PTS_S_WAIT_REQ_PCK, |
| 63 | PTS_S_WAIT_RESP_PTSS = PTS_S_WAIT_REQ_PTSS | 0x10, |
| 64 | PTS_S_WAIT_RESP_PTS0 = PTS_S_WAIT_REQ_PTS0 | 0x10, |
| 65 | PTS_S_WAIT_RESP_PTS1 = PTS_S_WAIT_REQ_PTS1 | 0x10, |
| 66 | PTS_S_WAIT_RESP_PTS2 = PTS_S_WAIT_REQ_PTS2 | 0x10, |
| 67 | PTS_S_WAIT_RESP_PTS3 = PTS_S_WAIT_REQ_PTS3 | 0x10, |
| 68 | PTS_S_WAIT_RESP_PCK = PTS_S_WAIT_REQ_PCK | 0x10, |
| 69 | }; |
| 70 | |
| 71 | #define _PTSS 0 |
| 72 | #define _PTS0 1 |
| 73 | #define _PTS1 2 |
| 74 | #define _PTS2 3 |
| 75 | #define _PTS3 4 |
| 76 | #define _PCK 5 |
| 77 | |
Harald Welte | 16cf408 | 2015-11-11 19:02:48 +0100 | [diff] [blame] | 78 | /* T-PDU state machine states */ |
Harald Welte | 9d3e382 | 2015-11-09 00:50:54 +0100 | [diff] [blame] | 79 | enum tpdu_state { |
Harald Welte | 16cf408 | 2015-11-11 19:02:48 +0100 | [diff] [blame] | 80 | TPDU_S_WAIT_CLA, /* waiting for CLA byte from reader */ |
| 81 | TPDU_S_WAIT_INS, /* waiting for INS byte from reader */ |
| 82 | TPDU_S_WAIT_P1, /* waiting for P1 byte from reader */ |
| 83 | TPDU_S_WAIT_P2, /* waiting for P2 byte from reader */ |
| 84 | TPDU_S_WAIT_P3, /* waiting for P3 byte from reader */ |
Harald Welte | 9d3e382 | 2015-11-09 00:50:54 +0100 | [diff] [blame] | 85 | TPDU_S_WAIT_PB, /* waiting for Tx of procedure byte */ |
| 86 | TPDU_S_WAIT_RX, /* waiitng for more data from reader */ |
| 87 | TPDU_S_WAIT_TX, /* waiting for more data to reader */ |
| 88 | }; |
| 89 | |
| 90 | #define _CLA 0 |
| 91 | #define _INS 1 |
| 92 | #define _P1 2 |
| 93 | #define _P2 3 |
| 94 | #define _P3 4 |
| 95 | |
| 96 | struct card_handle { |
| 97 | enum iso7816_3_card_state state; |
| 98 | |
| 99 | /* signal levels */ |
| 100 | uint8_t vcc_active; /* 1 = on, 0 = off */ |
| 101 | uint8_t in_reset; /* 1 = RST low, 0 = RST high */ |
| 102 | uint8_t clocked; /* 1 = active, 0 = inactive */ |
| 103 | |
Harald Welte | 16cf408 | 2015-11-11 19:02:48 +0100 | [diff] [blame] | 104 | /* timing parameters, from PTS */ |
Harald Welte | 9d3e382 | 2015-11-09 00:50:54 +0100 | [diff] [blame] | 105 | uint8_t fi; |
| 106 | uint8_t di; |
| 107 | uint8_t wi; |
| 108 | |
| 109 | uint8_t tc_chan; /* TC channel number */ |
| 110 | uint8_t uart_chan; /* UART channel */ |
| 111 | |
| 112 | uint32_t waiting_time; /* in clocks */ |
| 113 | |
| 114 | /* ATR state machine */ |
| 115 | struct { |
| 116 | uint8_t idx; |
| 117 | uint8_t len; |
| 118 | //uint8_t hist_len; |
| 119 | //uint8_t last_td; |
| 120 | uint8_t atr[ISO7816_3_ATR_LEN_MAX]; |
| 121 | } atr; |
| 122 | |
| 123 | /* PPS / PTS support */ |
| 124 | struct { |
| 125 | enum pts_state state; |
Harald Welte | 16cf408 | 2015-11-11 19:02:48 +0100 | [diff] [blame] | 126 | uint8_t req[6]; /* request bytes */ |
| 127 | uint8_t resp[6]; /* response bytes */ |
Harald Welte | 9d3e382 | 2015-11-09 00:50:54 +0100 | [diff] [blame] | 128 | } pts; |
| 129 | |
| 130 | /* TPDU */ |
| 131 | struct { |
| 132 | enum tpdu_state state; |
Harald Welte | 16cf408 | 2015-11-11 19:02:48 +0100 | [diff] [blame] | 133 | uint8_t hdr[5]; /* CLA INS P1 P2 P3 */ |
Harald Welte | 9d3e382 | 2015-11-09 00:50:54 +0100 | [diff] [blame] | 134 | } tpdu; |
| 135 | |
Harald Welte | 16cf408 | 2015-11-11 19:02:48 +0100 | [diff] [blame] | 136 | struct req_ctx *uart_rx_ctx; /* UART RX -> USB TX */ |
| 137 | struct req_ctx *uart_tx_ctx; /* USB RX -> UART TX */ |
Harald Welte | 9d3e382 | 2015-11-09 00:50:54 +0100 | [diff] [blame] | 138 | |
Harald Welte | 54cb3d0 | 2016-02-29 14:12:40 +0100 | [diff] [blame] | 139 | struct llist_head usb_tx_queue; |
| 140 | struct llist_head uart_tx_queue; |
| 141 | |
Harald Welte | 9d3e382 | 2015-11-09 00:50:54 +0100 | [diff] [blame] | 142 | struct { |
| 143 | uint32_t tx_bytes; |
| 144 | uint32_t rx_bytes; |
| 145 | uint32_t pps; |
| 146 | } stats; |
| 147 | }; |
| 148 | |
Harald Welte | 54cb3d0 | 2016-02-29 14:12:40 +0100 | [diff] [blame] | 149 | struct llist_head *card_emu_get_usb_tx_queue(struct card_handle *ch) |
| 150 | { |
| 151 | return &ch->usb_tx_queue; |
| 152 | } |
| 153 | |
| 154 | struct llist_head *card_emu_get_uart_tx_queue(struct card_handle *ch) |
| 155 | { |
| 156 | return &ch->uart_tx_queue; |
| 157 | } |
| 158 | |
Harald Welte | 2935b3c | 2015-11-14 20:00:14 +0100 | [diff] [blame] | 159 | static void set_tpdu_state(struct card_handle *ch, enum tpdu_state new_ts); |
Harald Welte | e7194ab | 2015-11-14 21:03:25 +0100 | [diff] [blame] | 160 | static void set_pts_state(struct card_handle *ch, enum pts_state new_ptss); |
Harald Welte | 2935b3c | 2015-11-14 20:00:14 +0100 | [diff] [blame] | 161 | |
Harald Welte | b5288e8 | 2015-11-14 21:15:52 +0100 | [diff] [blame] | 162 | static void flush_rx_buffer(struct card_handle *ch) |
| 163 | { |
| 164 | struct req_ctx *rctx; |
| 165 | struct cardemu_usb_msg_rx_data *rd; |
| 166 | |
| 167 | rctx = ch->uart_rx_ctx; |
| 168 | if (!rctx) |
| 169 | return; |
| 170 | |
Harald Welte | 54cb3d0 | 2016-02-29 14:12:40 +0100 | [diff] [blame] | 171 | ch->uart_rx_ctx = NULL; |
Harald Welte | b5288e8 | 2015-11-14 21:15:52 +0100 | [diff] [blame] | 172 | |
| 173 | /* store length of data payload fild in header */ |
Harald Welte | 54cb3d0 | 2016-02-29 14:12:40 +0100 | [diff] [blame] | 174 | rd = (struct cardemu_usb_msg_rx_data *) ch->uart_rx_ctx->data; |
Harald Welte | b5288e8 | 2015-11-14 21:15:52 +0100 | [diff] [blame] | 175 | rd->hdr.data_len = rctx->idx; |
Harald Welte | 54cb3d0 | 2016-02-29 14:12:40 +0100 | [diff] [blame] | 176 | |
| 177 | llist_add_tail(&rctx->list, &ch->usb_tx_queue); |
Harald Welte | b5288e8 | 2015-11-14 21:15:52 +0100 | [diff] [blame] | 178 | req_ctx_set_state(rctx, RCTX_S_USB_TX_PENDING); |
Harald Welte | b5288e8 | 2015-11-14 21:15:52 +0100 | [diff] [blame] | 179 | |
| 180 | /* FIXME: call into USB code to see if this buffer can |
| 181 | * be transmitted now */ |
| 182 | } |
| 183 | |
Harald Welte | 4ba66d0 | 2016-02-25 19:38:56 +0100 | [diff] [blame] | 184 | /* convert a non-contiguous PTS request/responsei into a contiguous |
| 185 | * buffer, returning the number of bytes used in the buffer */ |
| 186 | static int serialize_pts(uint8_t *out, const uint8_t *in) |
| 187 | { |
| 188 | int i = 0; |
| 189 | |
| 190 | out[i++] = in[_PTSS]; |
| 191 | out[i++] = in[_PTS0]; |
| 192 | if (in[_PTS0] & (1 << 4)) |
| 193 | out[i++] = in[_PTS1]; |
| 194 | if (in[_PTS0] & (1 << 5)) |
| 195 | out[i++] = in[_PTS2]; |
| 196 | if (in[_PTS0] & (1 << 6)) |
| 197 | out[i++] = in[_PTS3]; |
| 198 | out[i++] = in[_PCK]; |
| 199 | |
| 200 | return i; |
| 201 | } |
| 202 | |
Harald Welte | 17db2f1 | 2016-02-26 09:48:57 +0100 | [diff] [blame] | 203 | static uint8_t csum_pts(const uint8_t *in) |
| 204 | { |
| 205 | uint8_t out[6]; |
| 206 | int len = serialize_pts(out, in); |
| 207 | uint8_t csum = 0; |
| 208 | int i; |
| 209 | |
| 210 | /* we don't include the PCK byte in the checksumming process */ |
| 211 | len -= 1; |
| 212 | |
| 213 | for (i = 0; i < len; i++) |
| 214 | csum = csum ^ out[i]; |
| 215 | |
| 216 | return csum; |
| 217 | } |
| 218 | |
Harald Welte | 4ba66d0 | 2016-02-25 19:38:56 +0100 | [diff] [blame] | 219 | static void flush_pts(struct card_handle *ch) |
| 220 | { |
| 221 | struct req_ctx *rctx; |
| 222 | struct cardemu_usb_msg_pts_info *ptsi; |
| 223 | |
| 224 | rctx = req_ctx_find_get(0, RCTX_S_FREE, RCTX_S_UART_RX_BUSY); |
| 225 | if (!rctx) |
| 226 | return; |
| 227 | |
| 228 | ptsi = (struct cardemu_usb_msg_pts_info *) rctx->data; |
| 229 | ptsi->hdr.msg_type = CEMU_USB_MSGT_DO_PTS; |
| 230 | ptsi->hdr.data_len = serialize_pts(ptsi->req, ch->pts.req); |
| 231 | serialize_pts(ptsi->resp, ch->pts.resp); |
| 232 | |
Harald Welte | 54cb3d0 | 2016-02-29 14:12:40 +0100 | [diff] [blame] | 233 | llist_add_tail(&rctx->list, &ch->usb_tx_queue); |
Harald Welte | 4ba66d0 | 2016-02-25 19:38:56 +0100 | [diff] [blame] | 234 | req_ctx_set_state(rctx, RCTX_S_USB_TX_PENDING); |
| 235 | |
| 236 | /* FIXME: call into USB code to see if this buffer can |
| 237 | * be transmitted now */ |
| 238 | } |
| 239 | |
Harald Welte | 8c49636 | 2016-02-27 16:24:09 +0100 | [diff] [blame] | 240 | static void emu_update_fidi(struct card_handle *ch) |
Harald Welte | 9d3e382 | 2015-11-09 00:50:54 +0100 | [diff] [blame] | 241 | { |
| 242 | int rc; |
| 243 | |
| 244 | rc = compute_fidi_ratio(ch->fi, ch->di); |
| 245 | if (rc > 0 && rc < 0x400) { |
Harald Welte | 22bf67f | 2016-02-29 10:18:48 +0100 | [diff] [blame] | 246 | TRACE_DEBUG("computed Fi(%u) Di(%u) ratio: %d\r\n", |
Harald Welte | 9d3e382 | 2015-11-09 00:50:54 +0100 | [diff] [blame] | 247 | ch->fi, ch->di, rc); |
| 248 | /* make sure UART uses new F/D ratio */ |
| 249 | card_emu_uart_update_fidi(ch->uart_chan, rc); |
| 250 | /* notify ETU timer about this */ |
| 251 | tc_etu_set_etu(ch->tc_chan, rc); |
| 252 | } else |
Harald Welte | 22bf67f | 2016-02-29 10:18:48 +0100 | [diff] [blame] | 253 | TRACE_DEBUG("computed FiDi ration %d unsupported\r\n", rc); |
Harald Welte | 9d3e382 | 2015-11-09 00:50:54 +0100 | [diff] [blame] | 254 | } |
| 255 | |
| 256 | /* Update the ISO 7816-3 TPDU receiver state */ |
| 257 | static void card_set_state(struct card_handle *ch, |
| 258 | enum iso7816_3_card_state new_state) |
| 259 | { |
| 260 | switch (new_state) { |
| 261 | case ISO_S_WAIT_POWER: |
| 262 | case ISO_S_WAIT_CLK: |
| 263 | case ISO_S_WAIT_RST: |
| 264 | /* disable Rx and Tx of UART */ |
| 265 | card_emu_uart_enable(ch->uart_chan, 0); |
| 266 | break; |
| 267 | case ISO_S_WAIT_ATR: |
Harald Welte | e7194ab | 2015-11-14 21:03:25 +0100 | [diff] [blame] | 268 | set_pts_state(ch, PTS_S_WAIT_REQ_PTSS); |
Harald Welte | 9d3e382 | 2015-11-09 00:50:54 +0100 | [diff] [blame] | 269 | /* Reset to initial Fi / Di ratio */ |
| 270 | ch->fi = 1; |
| 271 | ch->di = 1; |
Harald Welte | 8c49636 | 2016-02-27 16:24:09 +0100 | [diff] [blame] | 272 | emu_update_fidi(ch); |
Harald Welte | 9d3e382 | 2015-11-09 00:50:54 +0100 | [diff] [blame] | 273 | /* initialize todefault WI, this will be overwritten if we |
| 274 | * receive TC2, and it will be programmed into hardware after |
| 275 | * ATR is finished */ |
| 276 | ch->wi = ISO7816_3_DEFAULT_WI; |
| 277 | /* update waiting time to initial waiting time */ |
| 278 | ch->waiting_time = ISO7816_3_INIT_WTIME; |
| 279 | tc_etu_set_wtime(ch->tc_chan, ch->waiting_time); |
| 280 | /* Set ATR sub-state to initial state */ |
| 281 | ch->atr.idx = 0; |
| 282 | //set_atr_state(ch, ATR_S_WAIT_TS); |
| 283 | /* Notice that we are just coming out of reset */ |
| 284 | //ch->sh.flags |= SIMTRACE_FLAG_ATR; |
| 285 | card_emu_uart_enable(ch->uart_chan, ENABLE_TX); |
| 286 | break; |
| 287 | break; |
| 288 | case ISO_S_WAIT_TPDU: |
| 289 | /* enable the receiver, disable transmitter */ |
Harald Welte | 2935b3c | 2015-11-14 20:00:14 +0100 | [diff] [blame] | 290 | set_tpdu_state(ch, TPDU_S_WAIT_CLA); |
Harald Welte | 9d3e382 | 2015-11-09 00:50:54 +0100 | [diff] [blame] | 291 | card_emu_uart_enable(ch->uart_chan, ENABLE_RX); |
| 292 | break; |
| 293 | case ISO_S_IN_ATR: |
| 294 | case ISO_S_IN_PTS: |
| 295 | case ISO_S_IN_TPDU: |
| 296 | /* do nothing */ |
| 297 | break; |
| 298 | } |
| 299 | |
| 300 | if (ch->state == new_state) |
| 301 | return; |
| 302 | |
Harald Welte | 22bf67f | 2016-02-29 10:18:48 +0100 | [diff] [blame] | 303 | TRACE_DEBUG("7816 card state %u -> %u\r\n", ch->state, new_state); |
Harald Welte | 9d3e382 | 2015-11-09 00:50:54 +0100 | [diff] [blame] | 304 | ch->state = new_state; |
| 305 | } |
| 306 | |
| 307 | |
| 308 | /********************************************************************** |
| 309 | * PTS / PPS handling |
| 310 | **********************************************************************/ |
| 311 | |
| 312 | /* Update the ATR sub-state */ |
| 313 | static void set_pts_state(struct card_handle *ch, enum pts_state new_ptss) |
| 314 | { |
Harald Welte | 22bf67f | 2016-02-29 10:18:48 +0100 | [diff] [blame] | 315 | TRACE_DEBUG("7816 PTS state %u -> %u\r\n", ch->pts.state, new_ptss); |
Harald Welte | 9d3e382 | 2015-11-09 00:50:54 +0100 | [diff] [blame] | 316 | ch->pts.state = new_ptss; |
| 317 | } |
| 318 | |
| 319 | /* Determine the next PTS state */ |
| 320 | static enum pts_state next_pts_state(struct card_handle *ch) |
| 321 | { |
| 322 | uint8_t is_resp = ch->pts.state & 0x10; |
| 323 | uint8_t sstate = ch->pts.state & 0x0f; |
| 324 | uint8_t *pts_ptr; |
| 325 | |
| 326 | if (!is_resp) |
| 327 | pts_ptr = ch->pts.req; |
| 328 | else |
| 329 | pts_ptr = ch->pts.resp; |
| 330 | |
| 331 | switch (sstate) { |
| 332 | case PTS_S_WAIT_REQ_PTSS: |
| 333 | goto from_ptss; |
| 334 | case PTS_S_WAIT_REQ_PTS0: |
| 335 | goto from_pts0; |
| 336 | case PTS_S_WAIT_REQ_PTS1: |
| 337 | goto from_pts1; |
| 338 | case PTS_S_WAIT_REQ_PTS2: |
| 339 | goto from_pts2; |
| 340 | case PTS_S_WAIT_REQ_PTS3: |
| 341 | goto from_pts3; |
| 342 | } |
| 343 | |
| 344 | if (ch->pts.state == PTS_S_WAIT_REQ_PCK) |
| 345 | return PTS_S_WAIT_RESP_PTSS; |
| 346 | |
| 347 | from_ptss: |
| 348 | return PTS_S_WAIT_REQ_PTS0 | is_resp; |
| 349 | from_pts0: |
| 350 | if (pts_ptr[_PTS0] & (1 << 4)) |
| 351 | return PTS_S_WAIT_REQ_PTS1 | is_resp; |
| 352 | from_pts1: |
| 353 | if (pts_ptr[_PTS0] & (1 << 5)) |
| 354 | return PTS_S_WAIT_REQ_PTS2 | is_resp; |
| 355 | from_pts2: |
| 356 | if (pts_ptr[_PTS0] & (1 << 6)) |
| 357 | return PTS_S_WAIT_REQ_PTS3 | is_resp; |
| 358 | from_pts3: |
| 359 | return PTS_S_WAIT_REQ_PCK | is_resp; |
| 360 | } |
| 361 | |
| 362 | |
| 363 | static enum iso7816_3_card_state |
| 364 | process_byte_pts(struct card_handle *ch, uint8_t byte) |
| 365 | { |
| 366 | switch (ch->pts.state) { |
| 367 | case PTS_S_WAIT_REQ_PTSS: |
| 368 | ch->pts.req[_PTSS] = byte; |
| 369 | break; |
| 370 | case PTS_S_WAIT_REQ_PTS0: |
| 371 | ch->pts.req[_PTS0] = byte; |
| 372 | break; |
| 373 | case PTS_S_WAIT_REQ_PTS1: |
| 374 | ch->pts.req[_PTS1] = byte; |
| 375 | break; |
| 376 | case PTS_S_WAIT_REQ_PTS2: |
| 377 | ch->pts.req[_PTS2] = byte; |
| 378 | break; |
| 379 | case PTS_S_WAIT_REQ_PTS3: |
| 380 | ch->pts.req[_PTS3] = byte; |
| 381 | break; |
| 382 | case PTS_S_WAIT_REQ_PCK: |
| 383 | ch->pts.req[_PCK] = byte; |
Harald Welte | 17db2f1 | 2016-02-26 09:48:57 +0100 | [diff] [blame] | 384 | if (ch->pts.req[_PCK] != csum_pts(ch->pts.req)) { |
Harald Welte | 22bf67f | 2016-02-29 10:18:48 +0100 | [diff] [blame] | 385 | TRACE_DEBUG("Error in PTS Checksum!\r\n"); |
Harald Welte | 17db2f1 | 2016-02-26 09:48:57 +0100 | [diff] [blame] | 386 | /* Wait for the next TPDU */ |
| 387 | set_pts_state(ch, PTS_S_WAIT_REQ_PTSS); |
| 388 | return ISO_S_WAIT_TPDU; |
| 389 | } |
Harald Welte | 4ba66d0 | 2016-02-25 19:38:56 +0100 | [diff] [blame] | 390 | /* FIXME: check if proposal matches capabilities in ATR */ |
Harald Welte | 9d3e382 | 2015-11-09 00:50:54 +0100 | [diff] [blame] | 391 | memcpy(ch->pts.resp, ch->pts.req, sizeof(ch->pts.resp)); |
| 392 | break; |
Harald Welte | 4c473da | 2015-11-14 13:31:11 +0100 | [diff] [blame] | 393 | default: |
Harald Welte | 22bf67f | 2016-02-29 10:18:48 +0100 | [diff] [blame] | 394 | TRACE_DEBUG("process_byte_pts() in invalid state %u\r\n", |
Harald Welte | 4c473da | 2015-11-14 13:31:11 +0100 | [diff] [blame] | 395 | ch->pts.state); |
| 396 | break; |
Harald Welte | 9d3e382 | 2015-11-09 00:50:54 +0100 | [diff] [blame] | 397 | } |
| 398 | /* calculate the next state and set it */ |
| 399 | set_pts_state(ch, next_pts_state(ch)); |
| 400 | |
Harald Welte | 4ba66d0 | 2016-02-25 19:38:56 +0100 | [diff] [blame] | 401 | if (ch->pts.state == PTS_S_WAIT_RESP_PTSS) { |
| 402 | flush_pts(ch); |
| 403 | /* activate UART TX to transmit PTS response */ |
| 404 | card_emu_uart_enable(ch->uart_chan, ENABLE_TX); |
| 405 | } |
| 406 | |
Harald Welte | 9d3e382 | 2015-11-09 00:50:54 +0100 | [diff] [blame] | 407 | return ISO_S_IN_PTS; |
| 408 | } |
| 409 | |
| 410 | /* return a single byte to be transmitted to the reader */ |
Harald Welte | 855ba9e | 2016-02-24 21:00:46 +0100 | [diff] [blame] | 411 | static int tx_byte_pts(struct card_handle *ch) |
Harald Welte | 9d3e382 | 2015-11-09 00:50:54 +0100 | [diff] [blame] | 412 | { |
Harald Welte | 855ba9e | 2016-02-24 21:00:46 +0100 | [diff] [blame] | 413 | uint8_t byte; |
| 414 | |
| 415 | /* 1: Determine the next transmit byte */ |
Harald Welte | 9d3e382 | 2015-11-09 00:50:54 +0100 | [diff] [blame] | 416 | switch (ch->pts.state) { |
| 417 | case PTS_S_WAIT_RESP_PTSS: |
Harald Welte | 855ba9e | 2016-02-24 21:00:46 +0100 | [diff] [blame] | 418 | byte = ch->pts.resp[_PTSS]; |
Harald Welte | 9d3e382 | 2015-11-09 00:50:54 +0100 | [diff] [blame] | 419 | break; |
| 420 | case PTS_S_WAIT_RESP_PTS0: |
Harald Welte | 855ba9e | 2016-02-24 21:00:46 +0100 | [diff] [blame] | 421 | byte = ch->pts.resp[_PTS0]; |
Harald Welte | 9d3e382 | 2015-11-09 00:50:54 +0100 | [diff] [blame] | 422 | break; |
| 423 | case PTS_S_WAIT_RESP_PTS1: |
Harald Welte | 855ba9e | 2016-02-24 21:00:46 +0100 | [diff] [blame] | 424 | byte = ch->pts.resp[_PTS1]; |
Harald Welte | 9d3e382 | 2015-11-09 00:50:54 +0100 | [diff] [blame] | 425 | /* This must be TA1 */ |
Harald Welte | 855ba9e | 2016-02-24 21:00:46 +0100 | [diff] [blame] | 426 | ch->fi = byte >> 4; |
| 427 | ch->di = byte & 0xf; |
Harald Welte | 22bf67f | 2016-02-29 10:18:48 +0100 | [diff] [blame] | 428 | TRACE_DEBUG("found Fi=%u Di=%u\r\n", ch->fi, ch->di); |
Harald Welte | 9d3e382 | 2015-11-09 00:50:54 +0100 | [diff] [blame] | 429 | break; |
| 430 | case PTS_S_WAIT_RESP_PTS2: |
Harald Welte | 855ba9e | 2016-02-24 21:00:46 +0100 | [diff] [blame] | 431 | byte = ch->pts.resp[_PTS2]; |
Harald Welte | 9d3e382 | 2015-11-09 00:50:54 +0100 | [diff] [blame] | 432 | break; |
| 433 | case PTS_S_WAIT_RESP_PTS3: |
Harald Welte | 855ba9e | 2016-02-24 21:00:46 +0100 | [diff] [blame] | 434 | byte = ch->pts.resp[_PTS3]; |
Harald Welte | 9d3e382 | 2015-11-09 00:50:54 +0100 | [diff] [blame] | 435 | break; |
| 436 | case PTS_S_WAIT_RESP_PCK: |
Harald Welte | 855ba9e | 2016-02-24 21:00:46 +0100 | [diff] [blame] | 437 | byte = ch->pts.resp[_PCK]; |
Harald Welte | 9d3e382 | 2015-11-09 00:50:54 +0100 | [diff] [blame] | 438 | /* update baud rate generator with Fi/Di */ |
Harald Welte | 8c49636 | 2016-02-27 16:24:09 +0100 | [diff] [blame] | 439 | emu_update_fidi(ch); |
Harald Welte | 855ba9e | 2016-02-24 21:00:46 +0100 | [diff] [blame] | 440 | break; |
Harald Welte | d79dc4f | 2015-11-14 13:32:21 +0100 | [diff] [blame] | 441 | default: |
Harald Welte | 22bf67f | 2016-02-29 10:18:48 +0100 | [diff] [blame] | 442 | TRACE_DEBUG("get_byte_pts() in invalid state %u\r\n", |
Harald Welte | d79dc4f | 2015-11-14 13:32:21 +0100 | [diff] [blame] | 443 | ch->pts.state); |
Harald Welte | 855ba9e | 2016-02-24 21:00:46 +0100 | [diff] [blame] | 444 | return 0; |
| 445 | } |
| 446 | |
| 447 | /* 2: Transmit the byte */ |
| 448 | card_emu_uart_tx(ch->uart_chan, byte); |
| 449 | |
| 450 | /* 3: Update the state */ |
| 451 | |
| 452 | switch (ch->pts.state) { |
| 453 | case PTS_S_WAIT_RESP_PCK: |
| 454 | /* Wait for the next TPDU */ |
| 455 | card_set_state(ch, ISO_S_WAIT_TPDU); |
| 456 | set_pts_state(ch, PTS_S_WAIT_REQ_PTSS); |
| 457 | break; |
| 458 | default: |
| 459 | /* calculate the next state and set it */ |
| 460 | set_pts_state(ch, next_pts_state(ch)); |
Harald Welte | d79dc4f | 2015-11-14 13:32:21 +0100 | [diff] [blame] | 461 | break; |
Harald Welte | 9d3e382 | 2015-11-09 00:50:54 +0100 | [diff] [blame] | 462 | } |
Harald Welte | d79dc4f | 2015-11-14 13:32:21 +0100 | [diff] [blame] | 463 | |
Harald Welte | 855ba9e | 2016-02-24 21:00:46 +0100 | [diff] [blame] | 464 | /* return number of bytes transmitted */ |
| 465 | return 1; |
Harald Welte | 9d3e382 | 2015-11-09 00:50:54 +0100 | [diff] [blame] | 466 | } |
| 467 | |
| 468 | |
| 469 | /********************************************************************** |
| 470 | * TPDU handling |
| 471 | **********************************************************************/ |
| 472 | |
Harald Welte | 4d80467 | 2015-11-14 23:02:38 +0100 | [diff] [blame] | 473 | |
| 474 | /* compute number of data bytes according to Chapter 10.3.2 of 7816-3 */ |
| 475 | static unsigned int t0_num_data_bytes(uint8_t p3, int reader_to_card) |
| 476 | { |
| 477 | if (reader_to_card) { |
| 478 | return p3; |
| 479 | } else { |
| 480 | if (p3 == 0) |
| 481 | return 256; |
| 482 | else |
| 483 | return p3; |
| 484 | } |
| 485 | } |
| 486 | |
Harald Welte | 9d3e382 | 2015-11-09 00:50:54 +0100 | [diff] [blame] | 487 | /* add a just-received TPDU byte (from reader) to USB buffer */ |
Harald Welte | 61bb30e | 2015-11-14 23:44:14 +0100 | [diff] [blame] | 488 | static void add_tpdu_byte(struct card_handle *ch, uint8_t byte) |
Harald Welte | 9d3e382 | 2015-11-09 00:50:54 +0100 | [diff] [blame] | 489 | { |
| 490 | struct req_ctx *rctx; |
| 491 | struct cardemu_usb_msg_rx_data *rd; |
Harald Welte | 4d80467 | 2015-11-14 23:02:38 +0100 | [diff] [blame] | 492 | unsigned int num_data_bytes = t0_num_data_bytes(ch->tpdu.hdr[_P3], 0); |
Harald Welte | 9d3e382 | 2015-11-09 00:50:54 +0100 | [diff] [blame] | 493 | |
| 494 | /* ensure we have a buffer */ |
| 495 | if (!ch->uart_rx_ctx) { |
Harald Welte | 4d80467 | 2015-11-14 23:02:38 +0100 | [diff] [blame] | 496 | rctx = ch->uart_rx_ctx = req_ctx_find_get(0, RCTX_S_FREE, RCTX_S_UART_RX_BUSY); |
| 497 | if (!ch->uart_rx_ctx) { |
Harald Welte | 22bf67f | 2016-02-29 10:18:48 +0100 | [diff] [blame] | 498 | TRACE_DEBUG("Received UART byte but unable to allocate Rx Buf\r\n"); |
Harald Welte | 9d3e382 | 2015-11-09 00:50:54 +0100 | [diff] [blame] | 499 | return; |
Harald Welte | 4d80467 | 2015-11-14 23:02:38 +0100 | [diff] [blame] | 500 | } |
Harald Welte | 9d3e382 | 2015-11-09 00:50:54 +0100 | [diff] [blame] | 501 | rd = (struct cardemu_usb_msg_rx_data *) ch->uart_rx_ctx->data; |
| 502 | cardemu_hdr_set(&rd->hdr, CEMU_USB_MSGT_DO_RX_DATA); |
| 503 | rctx->tot_len = sizeof(*rd); |
| 504 | rctx->idx = 0; |
| 505 | } else |
| 506 | rctx = ch->uart_rx_ctx; |
| 507 | |
| 508 | rd = (struct cardemu_usb_msg_rx_data *) rctx->data; |
| 509 | |
| 510 | rd->data[rctx->idx++] = byte; |
| 511 | rctx->tot_len++; |
| 512 | |
| 513 | /* check if the buffer is full. If so, send it */ |
Harald Welte | 4d80467 | 2015-11-14 23:02:38 +0100 | [diff] [blame] | 514 | if (rctx->tot_len >= sizeof(*rd) + num_data_bytes) { |
| 515 | rd->flags |= CEMU_DATA_F_FINAL; |
Harald Welte | b5288e8 | 2015-11-14 21:15:52 +0100 | [diff] [blame] | 516 | flush_rx_buffer(ch); |
Harald Welte | 61bb30e | 2015-11-14 23:44:14 +0100 | [diff] [blame] | 517 | /* We need to transmit the SW now, */ |
| 518 | set_tpdu_state(ch, TPDU_S_WAIT_TX); |
Harald Welte | 4d80467 | 2015-11-14 23:02:38 +0100 | [diff] [blame] | 519 | } else if (rctx->tot_len >= rctx->size) |
| 520 | flush_rx_buffer(ch); |
Harald Welte | 9d3e382 | 2015-11-09 00:50:54 +0100 | [diff] [blame] | 521 | } |
| 522 | |
| 523 | static void set_tpdu_state(struct card_handle *ch, enum tpdu_state new_ts) |
| 524 | { |
Harald Welte | 05b41c6 | 2015-11-14 20:58:48 +0100 | [diff] [blame] | 525 | if (ch->tpdu.state == new_ts) |
| 526 | return; |
| 527 | |
Harald Welte | 22bf67f | 2016-02-29 10:18:48 +0100 | [diff] [blame] | 528 | TRACE_DEBUG("7816 TPDU state %u -> %u\r\n", ch->tpdu.state, new_ts); |
Harald Welte | 05b41c6 | 2015-11-14 20:58:48 +0100 | [diff] [blame] | 529 | |
Harald Welte | 9d3e382 | 2015-11-09 00:50:54 +0100 | [diff] [blame] | 530 | switch (new_ts) { |
| 531 | case TPDU_S_WAIT_CLA: |
Harald Welte | 05b41c6 | 2015-11-14 20:58:48 +0100 | [diff] [blame] | 532 | case TPDU_S_WAIT_RX: |
Harald Welte | 9d3e382 | 2015-11-09 00:50:54 +0100 | [diff] [blame] | 533 | card_emu_uart_enable(ch->uart_chan, ENABLE_RX); |
| 534 | break; |
| 535 | case TPDU_S_WAIT_PB: |
| 536 | /* we just completed the TPDU header from reader to card |
| 537 | * and now need to disable the receiver, enable the |
| 538 | * transmitter and transmit the procedure byte */ |
| 539 | card_emu_uart_enable(ch->uart_chan, ENABLE_TX); |
| 540 | break; |
| 541 | } |
Harald Welte | 05b41c6 | 2015-11-14 20:58:48 +0100 | [diff] [blame] | 542 | |
Harald Welte | 9d3e382 | 2015-11-09 00:50:54 +0100 | [diff] [blame] | 543 | ch->tpdu.state = new_ts; |
| 544 | } |
| 545 | |
| 546 | static enum tpdu_state next_tpdu_state(struct card_handle *ch) |
| 547 | { |
| 548 | switch (ch->tpdu.state) { |
| 549 | case TPDU_S_WAIT_CLA: |
| 550 | return TPDU_S_WAIT_INS; |
| 551 | case TPDU_S_WAIT_INS: |
| 552 | return TPDU_S_WAIT_P1; |
| 553 | case TPDU_S_WAIT_P1: |
| 554 | return TPDU_S_WAIT_P2; |
| 555 | case TPDU_S_WAIT_P2: |
| 556 | return TPDU_S_WAIT_P3; |
| 557 | case TPDU_S_WAIT_P3: |
| 558 | return TPDU_S_WAIT_PB; |
| 559 | /* simply stay in Rx or Tx by default */ |
| 560 | case TPDU_S_WAIT_PB: |
| 561 | return TPDU_S_WAIT_PB; |
| 562 | case TPDU_S_WAIT_RX: |
| 563 | return TPDU_S_WAIT_RX; |
| 564 | case TPDU_S_WAIT_TX: |
| 565 | return TPDU_S_WAIT_TX; |
| 566 | } |
Harald Welte | 4c473da | 2015-11-14 13:31:11 +0100 | [diff] [blame] | 567 | /* we should never reach here */ |
| 568 | assert(0); |
| 569 | return -1; |
Harald Welte | 9d3e382 | 2015-11-09 00:50:54 +0100 | [diff] [blame] | 570 | } |
| 571 | |
| 572 | static void send_tpdu_header(struct card_handle *ch) |
| 573 | { |
| 574 | struct req_ctx *rctx; |
| 575 | struct cardemu_usb_msg_rx_data *rd; |
| 576 | |
Harald Welte | 43f7949 | 2016-02-29 10:06:54 +0100 | [diff] [blame] | 577 | TRACE_DEBUG("%s: %02x %02x %02x %02x %02x\r\n", __func__, |
| 578 | ch->tpdu.hdr[0], ch->tpdu.hdr[1], |
| 579 | ch->tpdu.hdr[2], ch->tpdu.hdr[3], |
| 580 | ch->tpdu.hdr[4]); |
| 581 | |
Harald Welte | 9d3e382 | 2015-11-09 00:50:54 +0100 | [diff] [blame] | 582 | /* if we already/still have a context, send it off */ |
Harald Welte | f1697e2 | 2016-03-02 10:28:54 +0100 | [diff] [blame^] | 583 | if (ch->uart_rx_ctx) { |
| 584 | TRACE_DEBUG("have old buffer\r\n"); |
| 585 | if (ch->uart_rx_ctx->idx) { |
| 586 | TRACE_DEBUG("flushing old buffer\r\n"); |
| 587 | flush_rx_buffer(ch); |
| 588 | } |
| 589 | } else { |
| 590 | TRACE_DEBUG("allocating new buffer\r\n"); |
| 591 | /* ensure we have a new buffer */ |
| 592 | ch->uart_rx_ctx = req_ctx_find_get(0, RCTX_S_FREE, RCTX_S_UART_RX_BUSY); |
| 593 | if (!ch->uart_rx_ctx) { |
| 594 | TRACE_ERROR("%s: ENOMEM\r\n", __func__); |
| 595 | return; |
| 596 | } |
Harald Welte | 9d3e382 | 2015-11-09 00:50:54 +0100 | [diff] [blame] | 597 | } |
Harald Welte | 9d3e382 | 2015-11-09 00:50:54 +0100 | [diff] [blame] | 598 | rctx = ch->uart_rx_ctx; |
| 599 | rd = (struct cardemu_usb_msg_rx_data *) rctx->data; |
| 600 | |
| 601 | /* initializ header */ |
| 602 | cardemu_hdr_set(&rd->hdr, CEMU_USB_MSGT_DO_RX_DATA); |
| 603 | rd->flags = CEMU_DATA_F_TPDU_HDR; |
| 604 | rctx->tot_len = sizeof(*rd) + sizeof(ch->tpdu.hdr); |
Harald Welte | 0ef96d5 | 2016-02-25 00:09:17 +0100 | [diff] [blame] | 605 | rctx->idx = sizeof(ch->tpdu.hdr); |
Harald Welte | 9d3e382 | 2015-11-09 00:50:54 +0100 | [diff] [blame] | 606 | |
| 607 | /* copy TPDU header to data field */ |
| 608 | memcpy(rd->data, ch->tpdu.hdr, sizeof(ch->tpdu.hdr)); |
Harald Welte | b5288e8 | 2015-11-14 21:15:52 +0100 | [diff] [blame] | 609 | /* rd->data_len is set in flush_rx_buffer() */ |
Harald Welte | 9d3e382 | 2015-11-09 00:50:54 +0100 | [diff] [blame] | 610 | |
Harald Welte | b5288e8 | 2015-11-14 21:15:52 +0100 | [diff] [blame] | 611 | flush_rx_buffer(ch); |
Harald Welte | 9d3e382 | 2015-11-09 00:50:54 +0100 | [diff] [blame] | 612 | } |
| 613 | |
| 614 | static enum iso7816_3_card_state |
| 615 | process_byte_tpdu(struct card_handle *ch, uint8_t byte) |
| 616 | { |
| 617 | switch (ch->tpdu.state) { |
| 618 | case TPDU_S_WAIT_CLA: |
| 619 | ch->tpdu.hdr[_CLA] = byte; |
Harald Welte | 4d80467 | 2015-11-14 23:02:38 +0100 | [diff] [blame] | 620 | set_tpdu_state(ch, next_tpdu_state(ch)); |
Harald Welte | 9d3e382 | 2015-11-09 00:50:54 +0100 | [diff] [blame] | 621 | break; |
| 622 | case TPDU_S_WAIT_INS: |
| 623 | ch->tpdu.hdr[_INS] = byte; |
Harald Welte | 4d80467 | 2015-11-14 23:02:38 +0100 | [diff] [blame] | 624 | set_tpdu_state(ch, next_tpdu_state(ch)); |
Harald Welte | 9d3e382 | 2015-11-09 00:50:54 +0100 | [diff] [blame] | 625 | break; |
| 626 | case TPDU_S_WAIT_P1: |
| 627 | ch->tpdu.hdr[_P1] = byte; |
Harald Welte | 4d80467 | 2015-11-14 23:02:38 +0100 | [diff] [blame] | 628 | set_tpdu_state(ch, next_tpdu_state(ch)); |
Harald Welte | 9d3e382 | 2015-11-09 00:50:54 +0100 | [diff] [blame] | 629 | break; |
| 630 | case TPDU_S_WAIT_P2: |
| 631 | ch->tpdu.hdr[_P2] = byte; |
Harald Welte | 4d80467 | 2015-11-14 23:02:38 +0100 | [diff] [blame] | 632 | set_tpdu_state(ch, next_tpdu_state(ch)); |
Harald Welte | 9d3e382 | 2015-11-09 00:50:54 +0100 | [diff] [blame] | 633 | break; |
| 634 | case TPDU_S_WAIT_P3: |
| 635 | ch->tpdu.hdr[_P3] = byte; |
Harald Welte | 4d80467 | 2015-11-14 23:02:38 +0100 | [diff] [blame] | 636 | set_tpdu_state(ch, next_tpdu_state(ch)); |
Harald Welte | 9d3e382 | 2015-11-09 00:50:54 +0100 | [diff] [blame] | 637 | /* FIXME: start timer to transmit further 0x60 */ |
| 638 | /* send the TPDU header as part of a procedure byte |
| 639 | * request to the USB host */ |
| 640 | send_tpdu_header(ch); |
| 641 | break; |
| 642 | case TPDU_S_WAIT_RX: |
Harald Welte | 61bb30e | 2015-11-14 23:44:14 +0100 | [diff] [blame] | 643 | add_tpdu_byte(ch, byte); |
| 644 | break; |
Harald Welte | 9d3e382 | 2015-11-09 00:50:54 +0100 | [diff] [blame] | 645 | default: |
Harald Welte | 22bf67f | 2016-02-29 10:18:48 +0100 | [diff] [blame] | 646 | TRACE_DEBUG("process_byte_tpdu() in invalid state %u\r\n", |
Harald Welte | 9d3e382 | 2015-11-09 00:50:54 +0100 | [diff] [blame] | 647 | ch->tpdu.state); |
| 648 | } |
Harald Welte | 9d3e382 | 2015-11-09 00:50:54 +0100 | [diff] [blame] | 649 | |
| 650 | /* ensure we stay in TPDU ISO state */ |
| 651 | return ISO_S_IN_TPDU; |
| 652 | } |
| 653 | |
Harald Welte | 855ba9e | 2016-02-24 21:00:46 +0100 | [diff] [blame] | 654 | /* tx a single byte to be transmitted to the reader */ |
| 655 | static int tx_byte_tpdu(struct card_handle *ch) |
Harald Welte | 9d3e382 | 2015-11-09 00:50:54 +0100 | [diff] [blame] | 656 | { |
| 657 | struct req_ctx *rctx; |
| 658 | struct cardemu_usb_msg_tx_data *td; |
Harald Welte | 855ba9e | 2016-02-24 21:00:46 +0100 | [diff] [blame] | 659 | uint8_t byte; |
Harald Welte | 9d3e382 | 2015-11-09 00:50:54 +0100 | [diff] [blame] | 660 | |
| 661 | /* ensure we are aware of any data that might be pending for |
| 662 | * transmit */ |
| 663 | if (!ch->uart_tx_ctx) { |
Harald Welte | 54cb3d0 | 2016-02-29 14:12:40 +0100 | [diff] [blame] | 664 | if (llist_empty(&ch->uart_tx_queue)) |
Harald Welte | 9d3e382 | 2015-11-09 00:50:54 +0100 | [diff] [blame] | 665 | return 0; |
| 666 | |
Harald Welte | 54cb3d0 | 2016-02-29 14:12:40 +0100 | [diff] [blame] | 667 | /* dequeue first at head */ |
| 668 | ch->uart_tx_ctx = llist_entry(ch->uart_tx_queue.next, |
| 669 | struct req_ctx, list); |
| 670 | llist_del(&ch->uart_tx_ctx->list); |
| 671 | req_ctx_set_state(ch->uart_tx_ctx, RCTX_S_UART_TX_BUSY); |
| 672 | |
Harald Welte | 9d3e382 | 2015-11-09 00:50:54 +0100 | [diff] [blame] | 673 | /* start with index zero */ |
| 674 | ch->uart_tx_ctx->idx = 0; |
| 675 | |
| 676 | } |
| 677 | rctx = ch->uart_tx_ctx; |
| 678 | td = (struct cardemu_usb_msg_tx_data *) rctx->data; |
| 679 | |
Harald Welte | 9d3e382 | 2015-11-09 00:50:54 +0100 | [diff] [blame] | 680 | /* take the next pending byte out of the rctx */ |
Harald Welte | 855ba9e | 2016-02-24 21:00:46 +0100 | [diff] [blame] | 681 | byte = td->data[rctx->idx++]; |
| 682 | |
| 683 | card_emu_uart_tx(ch->uart_chan, byte); |
Harald Welte | 9d3e382 | 2015-11-09 00:50:54 +0100 | [diff] [blame] | 684 | |
Harald Welte | f16b618 | 2016-02-24 22:18:46 +0100 | [diff] [blame] | 685 | /* this must happen _after_ the byte has been transmittd */ |
| 686 | switch (ch->tpdu.state) { |
| 687 | case TPDU_S_WAIT_PB: |
| 688 | /* if we just transmitted the procedure byte, we need to decide |
| 689 | * if we want to continue to receive or transmit */ |
| 690 | if (td->flags & CEMU_DATA_F_PB_AND_TX) |
| 691 | set_tpdu_state(ch, TPDU_S_WAIT_TX); |
| 692 | else if (td->flags & CEMU_DATA_F_PB_AND_RX) |
| 693 | set_tpdu_state(ch, TPDU_S_WAIT_RX); |
| 694 | break; |
| 695 | } |
| 696 | |
Harald Welte | 9d3e382 | 2015-11-09 00:50:54 +0100 | [diff] [blame] | 697 | /* check if the buffer has now been fully transmitted */ |
| 698 | if ((rctx->idx >= td->hdr.data_len) || |
Harald Welte | f16b618 | 2016-02-24 22:18:46 +0100 | [diff] [blame] | 699 | (td->data + rctx->idx >= rctx->data + rctx->tot_len)) { |
Harald Welte | 52922ff | 2015-11-14 20:59:56 +0100 | [diff] [blame] | 700 | if (td->flags & CEMU_DATA_F_PB_AND_RX) { |
| 701 | /* we have just sent the procedure byte and now |
| 702 | * need to continue receiving */ |
| 703 | set_tpdu_state(ch, TPDU_S_WAIT_RX); |
Harald Welte | 2935b3c | 2015-11-14 20:00:14 +0100 | [diff] [blame] | 704 | } else { |
Harald Welte | 52922ff | 2015-11-14 20:59:56 +0100 | [diff] [blame] | 705 | /* we have transmitted all bytes */ |
| 706 | if (td->flags & CEMU_DATA_F_FINAL) { |
| 707 | /* this was the final part of the APDU, go |
| 708 | * back to state one*/ |
| 709 | card_set_state(ch, ISO_S_WAIT_TPDU); |
| 710 | } else { |
| 711 | /* FIXME: call into USB code to chec if we need |
| 712 | * to submit a free buffer to accept |
| 713 | * further data on bulk out endpoint */ |
| 714 | } |
Harald Welte | 2935b3c | 2015-11-14 20:00:14 +0100 | [diff] [blame] | 715 | } |
Harald Welte | 9d3e382 | 2015-11-09 00:50:54 +0100 | [diff] [blame] | 716 | req_ctx_set_state(rctx, RCTX_S_FREE); |
| 717 | ch->uart_tx_ctx = NULL; |
Harald Welte | 9d3e382 | 2015-11-09 00:50:54 +0100 | [diff] [blame] | 718 | } |
| 719 | |
| 720 | return 1; |
| 721 | } |
| 722 | |
| 723 | /********************************************************************** |
| 724 | * Public API |
| 725 | **********************************************************************/ |
| 726 | |
| 727 | /* process a single byte received from the reader */ |
| 728 | void card_emu_process_rx_byte(struct card_handle *ch, uint8_t byte) |
| 729 | { |
| 730 | int new_state = -1; |
| 731 | |
| 732 | ch->stats.rx_bytes++; |
| 733 | |
| 734 | switch (ch->state) { |
| 735 | case ISO_S_WAIT_POWER: |
| 736 | case ISO_S_WAIT_CLK: |
| 737 | case ISO_S_WAIT_RST: |
| 738 | case ISO_S_WAIT_ATR: |
Harald Welte | 22bf67f | 2016-02-29 10:18:48 +0100 | [diff] [blame] | 739 | TRACE_DEBUG("Received UART char in 7816 state %u\r\n", |
Harald Welte | 4d80467 | 2015-11-14 23:02:38 +0100 | [diff] [blame] | 740 | ch->state); |
Harald Welte | 9d3e382 | 2015-11-09 00:50:54 +0100 | [diff] [blame] | 741 | /* we shouldn't receive any data from the reader yet! */ |
| 742 | break; |
| 743 | case ISO_S_WAIT_TPDU: |
| 744 | if (byte == 0xff) { |
| 745 | new_state = process_byte_pts(ch, byte); |
| 746 | ch->stats.pps++; |
| 747 | goto out_silent; |
| 748 | } |
| 749 | /* fall-through */ |
| 750 | case ISO_S_IN_TPDU: |
| 751 | new_state = process_byte_tpdu(ch, byte); |
| 752 | break; |
| 753 | case ISO_S_IN_PTS: |
| 754 | new_state = process_byte_pts(ch, byte); |
| 755 | goto out_silent; |
| 756 | } |
| 757 | |
| 758 | out_silent: |
| 759 | if (new_state != -1) |
| 760 | card_set_state(ch, new_state); |
| 761 | } |
| 762 | |
Harald Welte | 855ba9e | 2016-02-24 21:00:46 +0100 | [diff] [blame] | 763 | /* transmit a single byte to the reader */ |
| 764 | int card_emu_tx_byte(struct card_handle *ch) |
Harald Welte | 9d3e382 | 2015-11-09 00:50:54 +0100 | [diff] [blame] | 765 | { |
| 766 | int rc = 0; |
| 767 | |
| 768 | switch (ch->state) { |
| 769 | case ISO_S_IN_ATR: |
| 770 | if (ch->atr.idx < ch->atr.len) { |
Harald Welte | 855ba9e | 2016-02-24 21:00:46 +0100 | [diff] [blame] | 771 | uint8_t byte; |
| 772 | byte = ch->atr.atr[ch->atr.idx++]; |
Harald Welte | 9d3e382 | 2015-11-09 00:50:54 +0100 | [diff] [blame] | 773 | rc = 1; |
Harald Welte | 855ba9e | 2016-02-24 21:00:46 +0100 | [diff] [blame] | 774 | |
| 775 | card_emu_uart_tx(ch->uart_chan, byte); |
| 776 | |
Harald Welte | 9d3e382 | 2015-11-09 00:50:54 +0100 | [diff] [blame] | 777 | /* detect end of ATR */ |
| 778 | if (ch->atr.idx >= ch->atr.len) |
| 779 | card_set_state(ch, ISO_S_WAIT_TPDU); |
| 780 | } |
| 781 | break; |
| 782 | case ISO_S_IN_PTS: |
Harald Welte | 855ba9e | 2016-02-24 21:00:46 +0100 | [diff] [blame] | 783 | rc = tx_byte_pts(ch); |
Harald Welte | 9d3e382 | 2015-11-09 00:50:54 +0100 | [diff] [blame] | 784 | break; |
| 785 | case ISO_S_IN_TPDU: |
Harald Welte | 855ba9e | 2016-02-24 21:00:46 +0100 | [diff] [blame] | 786 | rc = tx_byte_tpdu(ch); |
Harald Welte | 9d3e382 | 2015-11-09 00:50:54 +0100 | [diff] [blame] | 787 | break; |
| 788 | } |
| 789 | |
| 790 | if (rc) |
| 791 | ch->stats.tx_bytes++; |
| 792 | |
Harald Welte | 4d80467 | 2015-11-14 23:02:38 +0100 | [diff] [blame] | 793 | /* if we return 0 here, the UART needs to disable transmit-ready |
| 794 | * interrupts */ |
Harald Welte | 9d3e382 | 2015-11-09 00:50:54 +0100 | [diff] [blame] | 795 | return rc; |
| 796 | } |
| 797 | |
Harald Welte | acae412 | 2016-03-02 10:27:58 +0100 | [diff] [blame] | 798 | void card_emu_have_new_uart_tx(struct card_handle *ch) |
| 799 | { |
| 800 | switch (ch->state) { |
| 801 | case ISO_S_IN_TPDU: |
| 802 | switch (ch->tpdu.state) { |
| 803 | case TPDU_S_WAIT_TX: |
| 804 | case TPDU_S_WAIT_PB: |
| 805 | card_emu_uart_enable(ch->uart_chan, ENABLE_TX); |
| 806 | break; |
| 807 | default: |
| 808 | break; |
| 809 | } |
| 810 | default: |
| 811 | break; |
| 812 | } |
| 813 | } |
| 814 | |
Harald Welte | 9d3e382 | 2015-11-09 00:50:54 +0100 | [diff] [blame] | 815 | /* hardware driver informs us that a card I/O signal has changed */ |
| 816 | void card_emu_io_statechg(struct card_handle *ch, enum card_io io, int active) |
| 817 | { |
| 818 | switch (io) { |
| 819 | case CARD_IO_VCC: |
Harald Welte | 47ee283 | 2016-02-29 10:09:46 +0100 | [diff] [blame] | 820 | if (active == 0 && ch->vcc_active == 1) { |
Harald Welte | 22bf67f | 2016-02-29 10:18:48 +0100 | [diff] [blame] | 821 | TRACE_DEBUG("VCC deactivated\r\n"); |
Harald Welte | 22cdf2a | 2016-02-24 22:18:11 +0100 | [diff] [blame] | 822 | tc_etu_disable(ch->tc_chan); |
Harald Welte | 9d3e382 | 2015-11-09 00:50:54 +0100 | [diff] [blame] | 823 | card_set_state(ch, ISO_S_WAIT_POWER); |
Harald Welte | 47ee283 | 2016-02-29 10:09:46 +0100 | [diff] [blame] | 824 | } else if (active == 1 && ch->vcc_active == 0) { |
Harald Welte | 22bf67f | 2016-02-29 10:18:48 +0100 | [diff] [blame] | 825 | TRACE_DEBUG("VCC activated\r\n"); |
Harald Welte | 9d3e382 | 2015-11-09 00:50:54 +0100 | [diff] [blame] | 826 | card_set_state(ch, ISO_S_WAIT_CLK); |
Harald Welte | 47ee283 | 2016-02-29 10:09:46 +0100 | [diff] [blame] | 827 | } |
Harald Welte | 9d3e382 | 2015-11-09 00:50:54 +0100 | [diff] [blame] | 828 | ch->vcc_active = active; |
| 829 | break; |
| 830 | case CARD_IO_CLK: |
Harald Welte | 47ee283 | 2016-02-29 10:09:46 +0100 | [diff] [blame] | 831 | if (active == 1 && ch->clocked == 0) { |
Harald Welte | 22bf67f | 2016-02-29 10:18:48 +0100 | [diff] [blame] | 832 | TRACE_DEBUG("CLK activated\r\n"); |
Harald Welte | 47ee283 | 2016-02-29 10:09:46 +0100 | [diff] [blame] | 833 | if (ch->state == ISO_S_WAIT_CLK) |
| 834 | card_set_state(ch, ISO_S_WAIT_RST); |
| 835 | } else if (active == 0 && ch->clocked == 1) { |
Harald Welte | 22bf67f | 2016-02-29 10:18:48 +0100 | [diff] [blame] | 836 | TRACE_DEBUG("CLK deactivated\r\n"); |
Harald Welte | 47ee283 | 2016-02-29 10:09:46 +0100 | [diff] [blame] | 837 | } |
Harald Welte | 9d3e382 | 2015-11-09 00:50:54 +0100 | [diff] [blame] | 838 | ch->clocked = active; |
| 839 | break; |
| 840 | case CARD_IO_RST: |
Harald Welte | 47ee283 | 2016-02-29 10:09:46 +0100 | [diff] [blame] | 841 | if (active == 0 && ch->in_reset) { |
Harald Welte | 22bf67f | 2016-02-29 10:18:48 +0100 | [diff] [blame] | 842 | TRACE_DEBUG("RST released\r\n"); |
Harald Welte | 47ee283 | 2016-02-29 10:09:46 +0100 | [diff] [blame] | 843 | if (ch->vcc_active && ch->clocked) { |
| 844 | /* enable the TC/ETU counter once reset has been released */ |
| 845 | tc_etu_enable(ch->tc_chan); |
| 846 | card_set_state(ch, ISO_S_WAIT_ATR); |
| 847 | /* FIXME: wait 400 to 40k clock cycles before sending ATR */ |
| 848 | card_set_state(ch, ISO_S_IN_ATR); |
| 849 | } |
| 850 | } else if (active && !ch->in_reset) { |
Harald Welte | 22bf67f | 2016-02-29 10:18:48 +0100 | [diff] [blame] | 851 | TRACE_DEBUG("RST asserted\r\n"); |
Harald Welte | 22cdf2a | 2016-02-24 22:18:11 +0100 | [diff] [blame] | 852 | tc_etu_disable(ch->tc_chan); |
Harald Welte | 9d3e382 | 2015-11-09 00:50:54 +0100 | [diff] [blame] | 853 | } |
| 854 | ch->in_reset = active; |
| 855 | break; |
| 856 | } |
| 857 | } |
| 858 | |
| 859 | /* User sets a new ATR to be returned during next card reset */ |
| 860 | int card_emu_set_atr(struct card_handle *ch, const uint8_t *atr, uint8_t len) |
| 861 | { |
| 862 | if (len > sizeof(ch->atr.atr)) |
| 863 | return -1; |
| 864 | |
| 865 | memcpy(ch->atr.atr, atr, len); |
| 866 | ch->atr.len = len; |
| 867 | ch->atr.idx = 0; |
| 868 | |
| 869 | /* FIXME: race condition with trasmitting ATR to reader? */ |
| 870 | |
| 871 | return 0; |
| 872 | } |
| 873 | |
| 874 | /* hardware driver informs us that one (more) ETU has expired */ |
| 875 | void tc_etu_wtime_half_expired(void *handle) |
| 876 | { |
| 877 | struct card_handle *ch = handle; |
| 878 | /* transmit NULL procedure byte well before waiting time expires */ |
| 879 | card_emu_uart_tx(ch->uart_chan, ISO7816_3_PB_NULL); |
| 880 | } |
| 881 | |
| 882 | /* hardware driver informs us that one (more) ETU has expired */ |
| 883 | void tc_etu_wtime_expired(void *handle) |
| 884 | { |
| 885 | } |
| 886 | |
| 887 | /* shortest ATR found in smartcard_list.txt */ |
| 888 | static const uint8_t default_atr[] = { 0x3B, 0x02, 0x14, 0x50 }; |
| 889 | |
| 890 | static struct card_handle card_handles[NUM_SLOTS]; |
| 891 | |
| 892 | struct card_handle *card_emu_init(uint8_t slot_num, uint8_t tc_chan, uint8_t uart_chan) |
| 893 | { |
| 894 | struct card_handle *ch; |
| 895 | |
| 896 | if (slot_num >= ARRAY_SIZE(card_handles)) |
| 897 | return NULL; |
| 898 | |
| 899 | ch = &card_handles[slot_num]; |
| 900 | |
| 901 | memset(ch, 0, sizeof(*ch)); |
| 902 | |
Harald Welte | 54cb3d0 | 2016-02-29 14:12:40 +0100 | [diff] [blame] | 903 | INIT_LLIST_HEAD(&ch->usb_tx_queue); |
| 904 | INIT_LLIST_HEAD(&ch->uart_tx_queue); |
| 905 | |
Harald Welte | 9d3e382 | 2015-11-09 00:50:54 +0100 | [diff] [blame] | 906 | /* initialize the card_handle with reasonabe defaults */ |
| 907 | ch->state = ISO_S_WAIT_POWER; |
| 908 | ch->vcc_active = 0; |
| 909 | ch->in_reset = 1; |
| 910 | ch->clocked = 0; |
| 911 | |
| 912 | ch->fi = 0; |
| 913 | ch->di = 1; |
| 914 | ch->wi = ISO7816_3_DEFAULT_WI; |
| 915 | |
| 916 | ch->tc_chan = tc_chan; |
| 917 | ch->uart_chan = uart_chan; |
| 918 | ch->waiting_time = ISO7816_3_INIT_WTIME; |
| 919 | |
| 920 | ch->atr.idx = 0; |
| 921 | ch->atr.len = sizeof(default_atr); |
| 922 | memcpy(ch->atr.atr, default_atr, ch->atr.len); |
| 923 | |
| 924 | ch->pts.state = PTS_S_WAIT_REQ_PTSS; |
| 925 | ch->tpdu.state = TPDU_S_WAIT_CLA; |
| 926 | |
| 927 | tc_etu_init(ch->tc_chan, ch); |
| 928 | |
| 929 | return ch; |
| 930 | } |