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