blob: 55d227ff5d5e1a7f5661185d35051b290ed56d6c [file] [log] [blame]
Harald Welte9d3e3822015-11-09 00:50:54 +01001/* 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 Welte4c473da2015-11-14 13:31:11 +010020#include <assert.h>
Harald Welte9d3e3822015-11-09 00:50:54 +010021#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
43enum 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 */
55enum 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 Welte16cf4082015-11-11 19:02:48 +010077/* T-PDU state machine states */
Harald Welte9d3e3822015-11-09 00:50:54 +010078enum tpdu_state {
Harald Welte16cf4082015-11-11 19:02:48 +010079 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 Welte9d3e3822015-11-09 00:50:54 +010084 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
95struct 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 Welte16cf4082015-11-11 19:02:48 +0100103 /* timing parameters, from PTS */
Harald Welte9d3e3822015-11-09 00:50:54 +0100104 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 Welte16cf4082015-11-11 19:02:48 +0100125 uint8_t req[6]; /* request bytes */
126 uint8_t resp[6]; /* response bytes */
Harald Welte9d3e3822015-11-09 00:50:54 +0100127 } pts;
128
129 /* TPDU */
130 struct {
131 enum tpdu_state state;
Harald Welte16cf4082015-11-11 19:02:48 +0100132 uint8_t hdr[5]; /* CLA INS P1 P2 P3 */
Harald Welte9d3e3822015-11-09 00:50:54 +0100133 } tpdu;
134
Harald Welte16cf4082015-11-11 19:02:48 +0100135 struct req_ctx *uart_rx_ctx; /* UART RX -> USB TX */
136 struct req_ctx *uart_tx_ctx; /* USB RX -> UART TX */
Harald Welte9d3e3822015-11-09 00:50:54 +0100137
138 struct {
139 uint32_t tx_bytes;
140 uint32_t rx_bytes;
141 uint32_t pps;
142 } stats;
143};
144
Harald Welte2935b3c2015-11-14 20:00:14 +0100145static void set_tpdu_state(struct card_handle *ch, enum tpdu_state new_ts);
Harald Weltee7194ab2015-11-14 21:03:25 +0100146static void set_pts_state(struct card_handle *ch, enum pts_state new_ptss);
Harald Welte2935b3c2015-11-14 20:00:14 +0100147
Harald Welteb5288e82015-11-14 21:15:52 +0100148static 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 Welte4ba66d02016-02-25 19:38:56 +0100168/* convert a non-contiguous PTS request/responsei into a contiguous
169 * buffer, returning the number of bytes used in the buffer */
170static 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 Welte17db2f12016-02-26 09:48:57 +0100187static 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 Welte4ba66d02016-02-25 19:38:56 +0100203static 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 Welte8c496362016-02-27 16:24:09 +0100223static void emu_update_fidi(struct card_handle *ch)
Harald Welte9d3e3822015-11-09 00:50:54 +0100224{
225 int rc;
226
227 rc = compute_fidi_ratio(ch->fi, ch->di);
228 if (rc > 0 && rc < 0x400) {
229 TRACE_DEBUG("computed Fi(%u) Di(%u) ratio: %d\n",
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
236 TRACE_DEBUG("computed FiDi ration %d unsupported\n", rc);
237}
238
239/* Update the ISO 7816-3 TPDU receiver state */
240static 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 Weltee7194ab2015-11-14 21:03:25 +0100251 set_pts_state(ch, PTS_S_WAIT_REQ_PTSS);
Harald Welte9d3e3822015-11-09 00:50:54 +0100252 /* Reset to initial Fi / Di ratio */
253 ch->fi = 1;
254 ch->di = 1;
Harald Welte8c496362016-02-27 16:24:09 +0100255 emu_update_fidi(ch);
Harald Welte9d3e3822015-11-09 00:50:54 +0100256 /* 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 Welte2935b3c2015-11-14 20:00:14 +0100273 set_tpdu_state(ch, TPDU_S_WAIT_CLA);
Harald Welte9d3e3822015-11-09 00:50:54 +0100274 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
286 TRACE_DEBUG("7816 card state %u -> %u\n", ch->state, new_state);
287 ch->state = new_state;
288}
289
290
291/**********************************************************************
292 * PTS / PPS handling
293 **********************************************************************/
294
295/* Update the ATR sub-state */
296static void set_pts_state(struct card_handle *ch, enum pts_state new_ptss)
297{
298 TRACE_DEBUG("7816 PTS state %u -> %u\n", ch->pts.state, new_ptss);
299 ch->pts.state = new_ptss;
300}
301
302/* Determine the next PTS state */
303static 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
330from_ptss:
331 return PTS_S_WAIT_REQ_PTS0 | is_resp;
332from_pts0:
333 if (pts_ptr[_PTS0] & (1 << 4))
334 return PTS_S_WAIT_REQ_PTS1 | is_resp;
335from_pts1:
336 if (pts_ptr[_PTS0] & (1 << 5))
337 return PTS_S_WAIT_REQ_PTS2 | is_resp;
338from_pts2:
339 if (pts_ptr[_PTS0] & (1 << 6))
340 return PTS_S_WAIT_REQ_PTS3 | is_resp;
341from_pts3:
342 return PTS_S_WAIT_REQ_PCK | is_resp;
343}
344
345
346static enum iso7816_3_card_state
347process_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 Welte17db2f12016-02-26 09:48:57 +0100367 if (ch->pts.req[_PCK] != csum_pts(ch->pts.req)) {
368 TRACE_DEBUG("Error in PTS Checksum!\n");
369 /* Wait for the next TPDU */
370 set_pts_state(ch, PTS_S_WAIT_REQ_PTSS);
371 return ISO_S_WAIT_TPDU;
372 }
Harald Welte4ba66d02016-02-25 19:38:56 +0100373 /* FIXME: check if proposal matches capabilities in ATR */
Harald Welte9d3e3822015-11-09 00:50:54 +0100374 memcpy(ch->pts.resp, ch->pts.req, sizeof(ch->pts.resp));
375 break;
Harald Welte4c473da2015-11-14 13:31:11 +0100376 default:
377 TRACE_DEBUG("process_byte_pts() in invalid state %u\n",
378 ch->pts.state);
379 break;
Harald Welte9d3e3822015-11-09 00:50:54 +0100380 }
381 /* calculate the next state and set it */
382 set_pts_state(ch, next_pts_state(ch));
383
Harald Welte4ba66d02016-02-25 19:38:56 +0100384 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 Welte9d3e3822015-11-09 00:50:54 +0100390 return ISO_S_IN_PTS;
391}
392
393/* return a single byte to be transmitted to the reader */
Harald Welte855ba9e2016-02-24 21:00:46 +0100394static int tx_byte_pts(struct card_handle *ch)
Harald Welte9d3e3822015-11-09 00:50:54 +0100395{
Harald Welte855ba9e2016-02-24 21:00:46 +0100396 uint8_t byte;
397
398 /* 1: Determine the next transmit byte */
Harald Welte9d3e3822015-11-09 00:50:54 +0100399 switch (ch->pts.state) {
400 case PTS_S_WAIT_RESP_PTSS:
Harald Welte855ba9e2016-02-24 21:00:46 +0100401 byte = ch->pts.resp[_PTSS];
Harald Welte9d3e3822015-11-09 00:50:54 +0100402 break;
403 case PTS_S_WAIT_RESP_PTS0:
Harald Welte855ba9e2016-02-24 21:00:46 +0100404 byte = ch->pts.resp[_PTS0];
Harald Welte9d3e3822015-11-09 00:50:54 +0100405 break;
406 case PTS_S_WAIT_RESP_PTS1:
Harald Welte855ba9e2016-02-24 21:00:46 +0100407 byte = ch->pts.resp[_PTS1];
Harald Welte9d3e3822015-11-09 00:50:54 +0100408 /* This must be TA1 */
Harald Welte855ba9e2016-02-24 21:00:46 +0100409 ch->fi = byte >> 4;
410 ch->di = byte & 0xf;
Harald Welte9d3e3822015-11-09 00:50:54 +0100411 TRACE_DEBUG("found Fi=%u Di=%u\n", ch->fi, ch->di);
Harald Welte9d3e3822015-11-09 00:50:54 +0100412 break;
413 case PTS_S_WAIT_RESP_PTS2:
Harald Welte855ba9e2016-02-24 21:00:46 +0100414 byte = ch->pts.resp[_PTS2];
Harald Welte9d3e3822015-11-09 00:50:54 +0100415 break;
416 case PTS_S_WAIT_RESP_PTS3:
Harald Welte855ba9e2016-02-24 21:00:46 +0100417 byte = ch->pts.resp[_PTS3];
Harald Welte9d3e3822015-11-09 00:50:54 +0100418 break;
419 case PTS_S_WAIT_RESP_PCK:
Harald Welte855ba9e2016-02-24 21:00:46 +0100420 byte = ch->pts.resp[_PCK];
Harald Welte9d3e3822015-11-09 00:50:54 +0100421 /* update baud rate generator with Fi/Di */
Harald Welte8c496362016-02-27 16:24:09 +0100422 emu_update_fidi(ch);
Harald Welte855ba9e2016-02-24 21:00:46 +0100423 break;
Harald Welted79dc4f2015-11-14 13:32:21 +0100424 default:
425 TRACE_DEBUG("get_byte_pts() in invalid state %u\n",
426 ch->pts.state);
Harald Welte855ba9e2016-02-24 21:00:46 +0100427 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 Welted79dc4f2015-11-14 13:32:21 +0100444 break;
Harald Welte9d3e3822015-11-09 00:50:54 +0100445 }
Harald Welted79dc4f2015-11-14 13:32:21 +0100446
Harald Welte855ba9e2016-02-24 21:00:46 +0100447 /* return number of bytes transmitted */
448 return 1;
Harald Welte9d3e3822015-11-09 00:50:54 +0100449}
450
451
452/**********************************************************************
453 * TPDU handling
454 **********************************************************************/
455
Harald Welte4d804672015-11-14 23:02:38 +0100456
457/* compute number of data bytes according to Chapter 10.3.2 of 7816-3 */
458static 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 Welte9d3e3822015-11-09 00:50:54 +0100470/* add a just-received TPDU byte (from reader) to USB buffer */
Harald Welte61bb30e2015-11-14 23:44:14 +0100471static void add_tpdu_byte(struct card_handle *ch, uint8_t byte)
Harald Welte9d3e3822015-11-09 00:50:54 +0100472{
473 struct req_ctx *rctx;
474 struct cardemu_usb_msg_rx_data *rd;
Harald Welte4d804672015-11-14 23:02:38 +0100475 unsigned int num_data_bytes = t0_num_data_bytes(ch->tpdu.hdr[_P3], 0);
Harald Welte9d3e3822015-11-09 00:50:54 +0100476
477 /* ensure we have a buffer */
478 if (!ch->uart_rx_ctx) {
Harald Welte4d804672015-11-14 23:02:38 +0100479 rctx = ch->uart_rx_ctx = req_ctx_find_get(0, RCTX_S_FREE, RCTX_S_UART_RX_BUSY);
480 if (!ch->uart_rx_ctx) {
481 TRACE_DEBUG("Received UART byte but unable to allocate Rx Buf\n");
Harald Welte9d3e3822015-11-09 00:50:54 +0100482 return;
Harald Welte4d804672015-11-14 23:02:38 +0100483 }
Harald Welte9d3e3822015-11-09 00:50:54 +0100484 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 Welte4d804672015-11-14 23:02:38 +0100497 if (rctx->tot_len >= sizeof(*rd) + num_data_bytes) {
498 rd->flags |= CEMU_DATA_F_FINAL;
Harald Welteb5288e82015-11-14 21:15:52 +0100499 flush_rx_buffer(ch);
Harald Welte61bb30e2015-11-14 23:44:14 +0100500 /* We need to transmit the SW now, */
501 set_tpdu_state(ch, TPDU_S_WAIT_TX);
Harald Welte4d804672015-11-14 23:02:38 +0100502 } else if (rctx->tot_len >= rctx->size)
503 flush_rx_buffer(ch);
Harald Welte9d3e3822015-11-09 00:50:54 +0100504}
505
506static void set_tpdu_state(struct card_handle *ch, enum tpdu_state new_ts)
507{
Harald Welte05b41c62015-11-14 20:58:48 +0100508 if (ch->tpdu.state == new_ts)
509 return;
510
Harald Welte9d3e3822015-11-09 00:50:54 +0100511 TRACE_DEBUG("7816 TPDU state %u -> %u\n", ch->tpdu.state, new_ts);
Harald Welte05b41c62015-11-14 20:58:48 +0100512
Harald Welte9d3e3822015-11-09 00:50:54 +0100513 switch (new_ts) {
514 case TPDU_S_WAIT_CLA:
Harald Welte05b41c62015-11-14 20:58:48 +0100515 case TPDU_S_WAIT_RX:
Harald Welte9d3e3822015-11-09 00:50:54 +0100516 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 Welte05b41c62015-11-14 20:58:48 +0100525
Harald Welte9d3e3822015-11-09 00:50:54 +0100526 ch->tpdu.state = new_ts;
527}
528
529static 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 Welte4c473da2015-11-14 13:31:11 +0100550 /* we should never reach here */
551 assert(0);
552 return -1;
Harald Welte9d3e3822015-11-09 00:50:54 +0100553}
554
555static void send_tpdu_header(struct card_handle *ch)
556{
557 struct req_ctx *rctx;
558 struct cardemu_usb_msg_rx_data *rd;
559
Harald Welte43f79492016-02-29 10:06:54 +0100560 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 Welte9d3e3822015-11-09 00:50:54 +0100565 /* if we already/still have a context, send it off */
Harald Welte2935b3c2015-11-14 20:00:14 +0100566 if (ch->uart_rx_ctx && ch->uart_rx_ctx->idx) {
Harald Welteb5288e82015-11-14 21:15:52 +0100567 flush_rx_buffer(ch);
Harald Welte9d3e3822015-11-09 00:50:54 +0100568 }
569
570 /* ensure we have a new buffer */
Harald Welte4d804672015-11-14 23:02:38 +0100571 ch->uart_rx_ctx = req_ctx_find_get(0, RCTX_S_FREE, RCTX_S_UART_RX_BUSY);
Harald Welte9d3e3822015-11-09 00:50:54 +0100572 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 Welte0ef96d52016-02-25 00:09:17 +0100581 rctx->idx = sizeof(ch->tpdu.hdr);
Harald Welte9d3e3822015-11-09 00:50:54 +0100582
583 /* copy TPDU header to data field */
584 memcpy(rd->data, ch->tpdu.hdr, sizeof(ch->tpdu.hdr));
Harald Welteb5288e82015-11-14 21:15:52 +0100585 /* rd->data_len is set in flush_rx_buffer() */
Harald Welte9d3e3822015-11-09 00:50:54 +0100586
Harald Welteb5288e82015-11-14 21:15:52 +0100587 flush_rx_buffer(ch);
Harald Welte9d3e3822015-11-09 00:50:54 +0100588}
589
590static enum iso7816_3_card_state
591process_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 Welte4d804672015-11-14 23:02:38 +0100596 set_tpdu_state(ch, next_tpdu_state(ch));
Harald Welte9d3e3822015-11-09 00:50:54 +0100597 break;
598 case TPDU_S_WAIT_INS:
599 ch->tpdu.hdr[_INS] = byte;
Harald Welte4d804672015-11-14 23:02:38 +0100600 set_tpdu_state(ch, next_tpdu_state(ch));
Harald Welte9d3e3822015-11-09 00:50:54 +0100601 break;
602 case TPDU_S_WAIT_P1:
603 ch->tpdu.hdr[_P1] = byte;
Harald Welte4d804672015-11-14 23:02:38 +0100604 set_tpdu_state(ch, next_tpdu_state(ch));
Harald Welte9d3e3822015-11-09 00:50:54 +0100605 break;
606 case TPDU_S_WAIT_P2:
607 ch->tpdu.hdr[_P2] = byte;
Harald Welte4d804672015-11-14 23:02:38 +0100608 set_tpdu_state(ch, next_tpdu_state(ch));
Harald Welte9d3e3822015-11-09 00:50:54 +0100609 break;
610 case TPDU_S_WAIT_P3:
611 ch->tpdu.hdr[_P3] = byte;
Harald Welte4d804672015-11-14 23:02:38 +0100612 set_tpdu_state(ch, next_tpdu_state(ch));
Harald Welte9d3e3822015-11-09 00:50:54 +0100613 /* 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 Welte61bb30e2015-11-14 23:44:14 +0100619 add_tpdu_byte(ch, byte);
620 break;
Harald Welte9d3e3822015-11-09 00:50:54 +0100621 default:
622 TRACE_DEBUG("process_byte_tpdu() in invalid state %u\n",
623 ch->tpdu.state);
624 }
Harald Welte9d3e3822015-11-09 00:50:54 +0100625
626 /* ensure we stay in TPDU ISO state */
627 return ISO_S_IN_TPDU;
628}
629
Harald Welte855ba9e2016-02-24 21:00:46 +0100630/* tx a single byte to be transmitted to the reader */
631static int tx_byte_tpdu(struct card_handle *ch)
Harald Welte9d3e3822015-11-09 00:50:54 +0100632{
633 struct req_ctx *rctx;
634 struct cardemu_usb_msg_tx_data *td;
Harald Welte855ba9e2016-02-24 21:00:46 +0100635 uint8_t byte;
Harald Welte9d3e3822015-11-09 00:50:54 +0100636
637 /* ensure we are aware of any data that might be pending for
638 * transmit */
639 if (!ch->uart_tx_ctx) {
Harald Welte4d804672015-11-14 23:02:38 +0100640 ch->uart_tx_ctx = req_ctx_find_get(0, RCTX_S_UART_TX_PENDING,
Harald Welte9d3e3822015-11-09 00:50:54 +0100641 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 Welte9d3e3822015-11-09 00:50:54 +0100652 /* take the next pending byte out of the rctx */
Harald Welte855ba9e2016-02-24 21:00:46 +0100653 byte = td->data[rctx->idx++];
654
655 card_emu_uart_tx(ch->uart_chan, byte);
Harald Welte9d3e3822015-11-09 00:50:54 +0100656
Harald Weltef16b6182016-02-24 22:18:46 +0100657 /* 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 Welte9d3e3822015-11-09 00:50:54 +0100669 /* check if the buffer has now been fully transmitted */
670 if ((rctx->idx >= td->hdr.data_len) ||
Harald Weltef16b6182016-02-24 22:18:46 +0100671 (td->data + rctx->idx >= rctx->data + rctx->tot_len)) {
Harald Welte52922ff2015-11-14 20:59:56 +0100672 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 Welte2935b3c2015-11-14 20:00:14 +0100676 } else {
Harald Welte52922ff2015-11-14 20:59:56 +0100677 /* 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 Welte2935b3c2015-11-14 20:00:14 +0100687 }
Harald Welte9d3e3822015-11-09 00:50:54 +0100688 req_ctx_set_state(rctx, RCTX_S_FREE);
689 ch->uart_tx_ctx = NULL;
Harald Welte9d3e3822015-11-09 00:50:54 +0100690 }
691
692 return 1;
693}
694
695/**********************************************************************
696 * Public API
697 **********************************************************************/
698
699/* process a single byte received from the reader */
700void 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 Welte4d804672015-11-14 23:02:38 +0100711 TRACE_DEBUG("Received UART char in 7816 state %u\n",
712 ch->state);
Harald Welte9d3e3822015-11-09 00:50:54 +0100713 /* 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
730out_silent:
731 if (new_state != -1)
732 card_set_state(ch, new_state);
733}
734
Harald Welte855ba9e2016-02-24 21:00:46 +0100735/* transmit a single byte to the reader */
736int card_emu_tx_byte(struct card_handle *ch)
Harald Welte9d3e3822015-11-09 00:50:54 +0100737{
738 int rc = 0;
739
740 switch (ch->state) {
741 case ISO_S_IN_ATR:
742 if (ch->atr.idx < ch->atr.len) {
Harald Welte855ba9e2016-02-24 21:00:46 +0100743 uint8_t byte;
744 byte = ch->atr.atr[ch->atr.idx++];
Harald Welte9d3e3822015-11-09 00:50:54 +0100745 rc = 1;
Harald Welte855ba9e2016-02-24 21:00:46 +0100746
747 card_emu_uart_tx(ch->uart_chan, byte);
748
Harald Welte9d3e3822015-11-09 00:50:54 +0100749 /* 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 Welte855ba9e2016-02-24 21:00:46 +0100755 rc = tx_byte_pts(ch);
Harald Welte9d3e3822015-11-09 00:50:54 +0100756 break;
757 case ISO_S_IN_TPDU:
Harald Welte855ba9e2016-02-24 21:00:46 +0100758 rc = tx_byte_tpdu(ch);
Harald Welte9d3e3822015-11-09 00:50:54 +0100759 break;
760 }
761
762 if (rc)
763 ch->stats.tx_bytes++;
764
Harald Welte4d804672015-11-14 23:02:38 +0100765 /* if we return 0 here, the UART needs to disable transmit-ready
766 * interrupts */
Harald Welte9d3e3822015-11-09 00:50:54 +0100767 return rc;
768}
769
770/* hardware driver informs us that a card I/O signal has changed */
771void card_emu_io_statechg(struct card_handle *ch, enum card_io io, int active)
772{
773 switch (io) {
774 case CARD_IO_VCC:
Harald Welte855ba9e2016-02-24 21:00:46 +0100775 if (active == 0) {
Harald Welte22cdf2a2016-02-24 22:18:11 +0100776 tc_etu_disable(ch->tc_chan);
Harald Welte9d3e3822015-11-09 00:50:54 +0100777 card_set_state(ch, ISO_S_WAIT_POWER);
Harald Welte855ba9e2016-02-24 21:00:46 +0100778 } else if (active == 1 && ch->vcc_active == 0)
Harald Welte9d3e3822015-11-09 00:50:54 +0100779 card_set_state(ch, ISO_S_WAIT_CLK);
780 ch->vcc_active = active;
781 break;
782 case CARD_IO_CLK:
783 if (active == 1 && ch->state == ISO_S_WAIT_CLK)
784 card_set_state(ch, ISO_S_WAIT_RST);
785 ch->clocked = active;
786 break;
787 case CARD_IO_RST:
788 if (active == 0 && ch->in_reset &&
789 ch->vcc_active && ch->clocked) {
Harald Welte855ba9e2016-02-24 21:00:46 +0100790 /* enable the TC/ETU counter once reset has been released */
Harald Welte22cdf2a2016-02-24 22:18:11 +0100791 tc_etu_enable(ch->tc_chan);
Harald Weltee7194ab2015-11-14 21:03:25 +0100792 card_set_state(ch, ISO_S_WAIT_ATR);
Harald Welte855ba9e2016-02-24 21:00:46 +0100793 /* FIXME: wait 400 to 40k clock cycles before sending ATR */
Harald Welte9d3e3822015-11-09 00:50:54 +0100794 card_set_state(ch, ISO_S_IN_ATR);
Harald Welte855ba9e2016-02-24 21:00:46 +0100795 } else if (active) {
Harald Welte22cdf2a2016-02-24 22:18:11 +0100796 tc_etu_disable(ch->tc_chan);
Harald Welte9d3e3822015-11-09 00:50:54 +0100797 }
798 ch->in_reset = active;
799 break;
800 }
801}
802
803/* User sets a new ATR to be returned during next card reset */
804int card_emu_set_atr(struct card_handle *ch, const uint8_t *atr, uint8_t len)
805{
806 if (len > sizeof(ch->atr.atr))
807 return -1;
808
809 memcpy(ch->atr.atr, atr, len);
810 ch->atr.len = len;
811 ch->atr.idx = 0;
812
813 /* FIXME: race condition with trasmitting ATR to reader? */
814
815 return 0;
816}
817
818/* hardware driver informs us that one (more) ETU has expired */
819void tc_etu_wtime_half_expired(void *handle)
820{
821 struct card_handle *ch = handle;
822 /* transmit NULL procedure byte well before waiting time expires */
823 card_emu_uart_tx(ch->uart_chan, ISO7816_3_PB_NULL);
824}
825
826/* hardware driver informs us that one (more) ETU has expired */
827void tc_etu_wtime_expired(void *handle)
828{
829}
830
831/* shortest ATR found in smartcard_list.txt */
832static const uint8_t default_atr[] = { 0x3B, 0x02, 0x14, 0x50 };
833
834static struct card_handle card_handles[NUM_SLOTS];
835
836struct card_handle *card_emu_init(uint8_t slot_num, uint8_t tc_chan, uint8_t uart_chan)
837{
838 struct card_handle *ch;
839
840 if (slot_num >= ARRAY_SIZE(card_handles))
841 return NULL;
842
843 ch = &card_handles[slot_num];
844
845 memset(ch, 0, sizeof(*ch));
846
847 /* initialize the card_handle with reasonabe defaults */
848 ch->state = ISO_S_WAIT_POWER;
849 ch->vcc_active = 0;
850 ch->in_reset = 1;
851 ch->clocked = 0;
852
853 ch->fi = 0;
854 ch->di = 1;
855 ch->wi = ISO7816_3_DEFAULT_WI;
856
857 ch->tc_chan = tc_chan;
858 ch->uart_chan = uart_chan;
859 ch->waiting_time = ISO7816_3_INIT_WTIME;
860
861 ch->atr.idx = 0;
862 ch->atr.len = sizeof(default_atr);
863 memcpy(ch->atr.atr, default_atr, ch->atr.len);
864
865 ch->pts.state = PTS_S_WAIT_REQ_PTSS;
866 ch->tpdu.state = TPDU_S_WAIT_CLA;
867
868 tc_etu_init(ch->tc_chan, ch);
869
870 return ch;
871}