blob: 837694374265faf88a271a778a750ec2dcc2d816 [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 Welte7abdb512016-03-03 17:48:32 +010020#define TRACE_LEVEL 6
21
Harald Welte4c473da2015-11-14 13:31:11 +010022#include <assert.h>
Harald Welte9d3e3822015-11-09 00:50:54 +010023#include <errno.h>
24#include <string.h>
25#include <stdint.h>
26#include <sys/types.h>
27
28#include "utils.h"
29#include "trace.h"
30#include "iso7816_fidi.h"
31#include "tc_etu.h"
32#include "card_emu.h"
33#include "req_ctx.h"
34#include "cardemu_prot.h"
Harald Welte54cb3d02016-02-29 14:12:40 +010035#include "linuxlist.h"
Harald Welte9d3e3822015-11-09 00:50:54 +010036
37
38#define NUM_SLOTS 2
39
40#define ISO7816_3_INIT_WTIME 9600
41#define ISO7816_3_DEFAULT_WI 10
42#define ISO7816_3_ATR_LEN_MAX (1+32) /* TS plus 32 chars */
43
44#define ISO7816_3_PB_NULL 0x60
45
46enum iso7816_3_card_state {
47 ISO_S_WAIT_POWER, /* waiting for power being applied */
48 ISO_S_WAIT_CLK, /* waiting for clock being applied */
49 ISO_S_WAIT_RST, /* waiting for reset being released */
50 ISO_S_WAIT_ATR, /* waiting for start of ATR */
51 ISO_S_IN_ATR, /* transmitting ATR to reader */
52 ISO_S_IN_PTS, /* transmitting ATR to reader */
53 ISO_S_WAIT_TPDU, /* waiting for data from reader */
54 ISO_S_IN_TPDU, /* inside a TPDU */
55};
56
57/* detailed sub-states of ISO_S_IN_PTS */
58enum pts_state {
59 PTS_S_WAIT_REQ_PTSS,
60 PTS_S_WAIT_REQ_PTS0,
61 PTS_S_WAIT_REQ_PTS1,
62 PTS_S_WAIT_REQ_PTS2,
63 PTS_S_WAIT_REQ_PTS3,
64 PTS_S_WAIT_REQ_PCK,
65 PTS_S_WAIT_RESP_PTSS = PTS_S_WAIT_REQ_PTSS | 0x10,
66 PTS_S_WAIT_RESP_PTS0 = PTS_S_WAIT_REQ_PTS0 | 0x10,
67 PTS_S_WAIT_RESP_PTS1 = PTS_S_WAIT_REQ_PTS1 | 0x10,
68 PTS_S_WAIT_RESP_PTS2 = PTS_S_WAIT_REQ_PTS2 | 0x10,
69 PTS_S_WAIT_RESP_PTS3 = PTS_S_WAIT_REQ_PTS3 | 0x10,
70 PTS_S_WAIT_RESP_PCK = PTS_S_WAIT_REQ_PCK | 0x10,
71};
72
73#define _PTSS 0
74#define _PTS0 1
75#define _PTS1 2
76#define _PTS2 3
77#define _PTS3 4
78#define _PCK 5
79
Harald Welte16cf4082015-11-11 19:02:48 +010080/* T-PDU state machine states */
Harald Welte9d3e3822015-11-09 00:50:54 +010081enum tpdu_state {
Harald Welte16cf4082015-11-11 19:02:48 +010082 TPDU_S_WAIT_CLA, /* waiting for CLA byte from reader */
83 TPDU_S_WAIT_INS, /* waiting for INS byte from reader */
84 TPDU_S_WAIT_P1, /* waiting for P1 byte from reader */
85 TPDU_S_WAIT_P2, /* waiting for P2 byte from reader */
86 TPDU_S_WAIT_P3, /* waiting for P3 byte from reader */
Harald Welte9d3e3822015-11-09 00:50:54 +010087 TPDU_S_WAIT_PB, /* waiting for Tx of procedure byte */
88 TPDU_S_WAIT_RX, /* waiitng for more data from reader */
89 TPDU_S_WAIT_TX, /* waiting for more data to reader */
90};
91
92#define _CLA 0
93#define _INS 1
94#define _P1 2
95#define _P2 3
96#define _P3 4
97
98struct card_handle {
99 enum iso7816_3_card_state state;
100
101 /* signal levels */
102 uint8_t vcc_active; /* 1 = on, 0 = off */
103 uint8_t in_reset; /* 1 = RST low, 0 = RST high */
104 uint8_t clocked; /* 1 = active, 0 = inactive */
105
Harald Welte16cf4082015-11-11 19:02:48 +0100106 /* timing parameters, from PTS */
Harald Welte9d3e3822015-11-09 00:50:54 +0100107 uint8_t fi;
108 uint8_t di;
109 uint8_t wi;
110
111 uint8_t tc_chan; /* TC channel number */
112 uint8_t uart_chan; /* UART channel */
113
114 uint32_t waiting_time; /* in clocks */
115
116 /* ATR state machine */
117 struct {
118 uint8_t idx;
119 uint8_t len;
120 //uint8_t hist_len;
121 //uint8_t last_td;
122 uint8_t atr[ISO7816_3_ATR_LEN_MAX];
123 } atr;
124
125 /* PPS / PTS support */
126 struct {
127 enum pts_state state;
Harald Welte16cf4082015-11-11 19:02:48 +0100128 uint8_t req[6]; /* request bytes */
129 uint8_t resp[6]; /* response bytes */
Harald Welte9d3e3822015-11-09 00:50:54 +0100130 } pts;
131
132 /* TPDU */
133 struct {
134 enum tpdu_state state;
Harald Welte16cf4082015-11-11 19:02:48 +0100135 uint8_t hdr[5]; /* CLA INS P1 P2 P3 */
Harald Welte9d3e3822015-11-09 00:50:54 +0100136 } tpdu;
137
Harald Welte16cf4082015-11-11 19:02:48 +0100138 struct req_ctx *uart_rx_ctx; /* UART RX -> USB TX */
139 struct req_ctx *uart_tx_ctx; /* USB RX -> UART TX */
Harald Welte9d3e3822015-11-09 00:50:54 +0100140
Harald Welte54cb3d02016-02-29 14:12:40 +0100141 struct llist_head usb_tx_queue;
142 struct llist_head uart_tx_queue;
143
Harald Welte9d3e3822015-11-09 00:50:54 +0100144 struct {
145 uint32_t tx_bytes;
146 uint32_t rx_bytes;
147 uint32_t pps;
148 } stats;
149};
150
Harald Welte54cb3d02016-02-29 14:12:40 +0100151struct llist_head *card_emu_get_usb_tx_queue(struct card_handle *ch)
152{
153 return &ch->usb_tx_queue;
154}
155
156struct llist_head *card_emu_get_uart_tx_queue(struct card_handle *ch)
157{
158 return &ch->uart_tx_queue;
159}
160
Harald Welte2935b3c2015-11-14 20:00:14 +0100161static void set_tpdu_state(struct card_handle *ch, enum tpdu_state new_ts);
Harald Weltee7194ab2015-11-14 21:03:25 +0100162static void set_pts_state(struct card_handle *ch, enum pts_state new_ptss);
Harald Welte2935b3c2015-11-14 20:00:14 +0100163
Harald Welteb5288e82015-11-14 21:15:52 +0100164static void flush_rx_buffer(struct card_handle *ch)
165{
166 struct req_ctx *rctx;
167 struct cardemu_usb_msg_rx_data *rd;
168
169 rctx = ch->uart_rx_ctx;
170 if (!rctx)
171 return;
172
Harald Welte54cb3d02016-02-29 14:12:40 +0100173 ch->uart_rx_ctx = NULL;
Harald Welteb5288e82015-11-14 21:15:52 +0100174
175 /* store length of data payload fild in header */
Harald Weltefcdd6602016-03-02 10:33:58 +0100176 rd = (struct cardemu_usb_msg_rx_data *) rctx->data;
Harald Welteb5288e82015-11-14 21:15:52 +0100177 rd->hdr.data_len = rctx->idx;
Harald Welte54cb3d02016-02-29 14:12:40 +0100178
179 llist_add_tail(&rctx->list, &ch->usb_tx_queue);
Harald Welteb5288e82015-11-14 21:15:52 +0100180 req_ctx_set_state(rctx, RCTX_S_USB_TX_PENDING);
Harald Welteb5288e82015-11-14 21:15:52 +0100181
182 /* FIXME: call into USB code to see if this buffer can
183 * be transmitted now */
184}
185
Harald Welte4ba66d02016-02-25 19:38:56 +0100186/* convert a non-contiguous PTS request/responsei into a contiguous
187 * buffer, returning the number of bytes used in the buffer */
188static int serialize_pts(uint8_t *out, const uint8_t *in)
189{
190 int i = 0;
191
192 out[i++] = in[_PTSS];
193 out[i++] = in[_PTS0];
194 if (in[_PTS0] & (1 << 4))
195 out[i++] = in[_PTS1];
196 if (in[_PTS0] & (1 << 5))
197 out[i++] = in[_PTS2];
198 if (in[_PTS0] & (1 << 6))
199 out[i++] = in[_PTS3];
200 out[i++] = in[_PCK];
201
202 return i;
203}
204
Harald Welte17db2f12016-02-26 09:48:57 +0100205static uint8_t csum_pts(const uint8_t *in)
206{
207 uint8_t out[6];
208 int len = serialize_pts(out, in);
209 uint8_t csum = 0;
210 int i;
211
212 /* we don't include the PCK byte in the checksumming process */
213 len -= 1;
214
215 for (i = 0; i < len; i++)
216 csum = csum ^ out[i];
217
218 return csum;
219}
220
Harald Welte4ba66d02016-02-25 19:38:56 +0100221static void flush_pts(struct card_handle *ch)
222{
223 struct req_ctx *rctx;
224 struct cardemu_usb_msg_pts_info *ptsi;
225
226 rctx = req_ctx_find_get(0, RCTX_S_FREE, RCTX_S_UART_RX_BUSY);
227 if (!rctx)
228 return;
229
230 ptsi = (struct cardemu_usb_msg_pts_info *) rctx->data;
231 ptsi->hdr.msg_type = CEMU_USB_MSGT_DO_PTS;
232 ptsi->hdr.data_len = serialize_pts(ptsi->req, ch->pts.req);
233 serialize_pts(ptsi->resp, ch->pts.resp);
234
Harald Welte54cb3d02016-02-29 14:12:40 +0100235 llist_add_tail(&rctx->list, &ch->usb_tx_queue);
Harald Welte4ba66d02016-02-25 19:38:56 +0100236 req_ctx_set_state(rctx, RCTX_S_USB_TX_PENDING);
237
238 /* FIXME: call into USB code to see if this buffer can
239 * be transmitted now */
240}
241
Harald Welte8c496362016-02-27 16:24:09 +0100242static void emu_update_fidi(struct card_handle *ch)
Harald Welte9d3e3822015-11-09 00:50:54 +0100243{
244 int rc;
245
246 rc = compute_fidi_ratio(ch->fi, ch->di);
247 if (rc > 0 && rc < 0x400) {
Harald Welte22bf67f2016-02-29 10:18:48 +0100248 TRACE_DEBUG("computed Fi(%u) Di(%u) ratio: %d\r\n",
Harald Welte9d3e3822015-11-09 00:50:54 +0100249 ch->fi, ch->di, rc);
250 /* make sure UART uses new F/D ratio */
251 card_emu_uart_update_fidi(ch->uart_chan, rc);
252 /* notify ETU timer about this */
253 tc_etu_set_etu(ch->tc_chan, rc);
254 } else
Harald Welte22bf67f2016-02-29 10:18:48 +0100255 TRACE_DEBUG("computed FiDi ration %d unsupported\r\n", rc);
Harald Welte9d3e3822015-11-09 00:50:54 +0100256}
257
258/* Update the ISO 7816-3 TPDU receiver state */
259static void card_set_state(struct card_handle *ch,
260 enum iso7816_3_card_state new_state)
261{
262 switch (new_state) {
263 case ISO_S_WAIT_POWER:
264 case ISO_S_WAIT_CLK:
265 case ISO_S_WAIT_RST:
266 /* disable Rx and Tx of UART */
267 card_emu_uart_enable(ch->uart_chan, 0);
268 break;
269 case ISO_S_WAIT_ATR:
Harald Weltee7194ab2015-11-14 21:03:25 +0100270 set_pts_state(ch, PTS_S_WAIT_REQ_PTSS);
Harald Welte9d3e3822015-11-09 00:50:54 +0100271 /* Reset to initial Fi / Di ratio */
272 ch->fi = 1;
273 ch->di = 1;
Harald Welte8c496362016-02-27 16:24:09 +0100274 emu_update_fidi(ch);
Harald Welte9d3e3822015-11-09 00:50:54 +0100275 /* initialize todefault WI, this will be overwritten if we
276 * receive TC2, and it will be programmed into hardware after
277 * ATR is finished */
278 ch->wi = ISO7816_3_DEFAULT_WI;
279 /* update waiting time to initial waiting time */
280 ch->waiting_time = ISO7816_3_INIT_WTIME;
281 tc_etu_set_wtime(ch->tc_chan, ch->waiting_time);
282 /* Set ATR sub-state to initial state */
283 ch->atr.idx = 0;
284 //set_atr_state(ch, ATR_S_WAIT_TS);
285 /* Notice that we are just coming out of reset */
286 //ch->sh.flags |= SIMTRACE_FLAG_ATR;
287 card_emu_uart_enable(ch->uart_chan, ENABLE_TX);
288 break;
289 break;
290 case ISO_S_WAIT_TPDU:
291 /* enable the receiver, disable transmitter */
Harald Welte2935b3c2015-11-14 20:00:14 +0100292 set_tpdu_state(ch, TPDU_S_WAIT_CLA);
Harald Welte9d3e3822015-11-09 00:50:54 +0100293 card_emu_uart_enable(ch->uart_chan, ENABLE_RX);
294 break;
295 case ISO_S_IN_ATR:
296 case ISO_S_IN_PTS:
297 case ISO_S_IN_TPDU:
298 /* do nothing */
299 break;
300 }
301
302 if (ch->state == new_state)
303 return;
304
Harald Welte22bf67f2016-02-29 10:18:48 +0100305 TRACE_DEBUG("7816 card state %u -> %u\r\n", ch->state, new_state);
Harald Welte9d3e3822015-11-09 00:50:54 +0100306 ch->state = new_state;
307}
308
309
310/**********************************************************************
311 * PTS / PPS handling
312 **********************************************************************/
313
314/* Update the ATR sub-state */
315static void set_pts_state(struct card_handle *ch, enum pts_state new_ptss)
316{
Harald Welte22bf67f2016-02-29 10:18:48 +0100317 TRACE_DEBUG("7816 PTS state %u -> %u\r\n", ch->pts.state, new_ptss);
Harald Welte9d3e3822015-11-09 00:50:54 +0100318 ch->pts.state = new_ptss;
319}
320
321/* Determine the next PTS state */
322static enum pts_state next_pts_state(struct card_handle *ch)
323{
324 uint8_t is_resp = ch->pts.state & 0x10;
325 uint8_t sstate = ch->pts.state & 0x0f;
326 uint8_t *pts_ptr;
327
328 if (!is_resp)
329 pts_ptr = ch->pts.req;
330 else
331 pts_ptr = ch->pts.resp;
332
333 switch (sstate) {
334 case PTS_S_WAIT_REQ_PTSS:
335 goto from_ptss;
336 case PTS_S_WAIT_REQ_PTS0:
337 goto from_pts0;
338 case PTS_S_WAIT_REQ_PTS1:
339 goto from_pts1;
340 case PTS_S_WAIT_REQ_PTS2:
341 goto from_pts2;
342 case PTS_S_WAIT_REQ_PTS3:
343 goto from_pts3;
344 }
345
346 if (ch->pts.state == PTS_S_WAIT_REQ_PCK)
347 return PTS_S_WAIT_RESP_PTSS;
348
349from_ptss:
350 return PTS_S_WAIT_REQ_PTS0 | is_resp;
351from_pts0:
352 if (pts_ptr[_PTS0] & (1 << 4))
353 return PTS_S_WAIT_REQ_PTS1 | is_resp;
354from_pts1:
355 if (pts_ptr[_PTS0] & (1 << 5))
356 return PTS_S_WAIT_REQ_PTS2 | is_resp;
357from_pts2:
358 if (pts_ptr[_PTS0] & (1 << 6))
359 return PTS_S_WAIT_REQ_PTS3 | is_resp;
360from_pts3:
361 return PTS_S_WAIT_REQ_PCK | is_resp;
362}
363
364
365static enum iso7816_3_card_state
366process_byte_pts(struct card_handle *ch, uint8_t byte)
367{
368 switch (ch->pts.state) {
369 case PTS_S_WAIT_REQ_PTSS:
370 ch->pts.req[_PTSS] = byte;
371 break;
372 case PTS_S_WAIT_REQ_PTS0:
373 ch->pts.req[_PTS0] = byte;
374 break;
375 case PTS_S_WAIT_REQ_PTS1:
376 ch->pts.req[_PTS1] = byte;
377 break;
378 case PTS_S_WAIT_REQ_PTS2:
379 ch->pts.req[_PTS2] = byte;
380 break;
381 case PTS_S_WAIT_REQ_PTS3:
382 ch->pts.req[_PTS3] = byte;
383 break;
384 case PTS_S_WAIT_REQ_PCK:
385 ch->pts.req[_PCK] = byte;
Harald Welte17db2f12016-02-26 09:48:57 +0100386 if (ch->pts.req[_PCK] != csum_pts(ch->pts.req)) {
Harald Welte22bf67f2016-02-29 10:18:48 +0100387 TRACE_DEBUG("Error in PTS Checksum!\r\n");
Harald Welte17db2f12016-02-26 09:48:57 +0100388 /* Wait for the next TPDU */
389 set_pts_state(ch, PTS_S_WAIT_REQ_PTSS);
390 return ISO_S_WAIT_TPDU;
391 }
Harald Welte4ba66d02016-02-25 19:38:56 +0100392 /* FIXME: check if proposal matches capabilities in ATR */
Harald Welte9d3e3822015-11-09 00:50:54 +0100393 memcpy(ch->pts.resp, ch->pts.req, sizeof(ch->pts.resp));
394 break;
Harald Welte4c473da2015-11-14 13:31:11 +0100395 default:
Harald Welte22bf67f2016-02-29 10:18:48 +0100396 TRACE_DEBUG("process_byte_pts() in invalid state %u\r\n",
Harald Welte4c473da2015-11-14 13:31:11 +0100397 ch->pts.state);
398 break;
Harald Welte9d3e3822015-11-09 00:50:54 +0100399 }
400 /* calculate the next state and set it */
401 set_pts_state(ch, next_pts_state(ch));
402
Harald Welte4ba66d02016-02-25 19:38:56 +0100403 if (ch->pts.state == PTS_S_WAIT_RESP_PTSS) {
404 flush_pts(ch);
405 /* activate UART TX to transmit PTS response */
406 card_emu_uart_enable(ch->uart_chan, ENABLE_TX);
407 }
408
Harald Welte9d3e3822015-11-09 00:50:54 +0100409 return ISO_S_IN_PTS;
410}
411
412/* return a single byte to be transmitted to the reader */
Harald Welte855ba9e2016-02-24 21:00:46 +0100413static int tx_byte_pts(struct card_handle *ch)
Harald Welte9d3e3822015-11-09 00:50:54 +0100414{
Harald Welte855ba9e2016-02-24 21:00:46 +0100415 uint8_t byte;
416
417 /* 1: Determine the next transmit byte */
Harald Welte9d3e3822015-11-09 00:50:54 +0100418 switch (ch->pts.state) {
419 case PTS_S_WAIT_RESP_PTSS:
Harald Welte855ba9e2016-02-24 21:00:46 +0100420 byte = ch->pts.resp[_PTSS];
Harald Welte9d3e3822015-11-09 00:50:54 +0100421 break;
422 case PTS_S_WAIT_RESP_PTS0:
Harald Welte855ba9e2016-02-24 21:00:46 +0100423 byte = ch->pts.resp[_PTS0];
Harald Welte9d3e3822015-11-09 00:50:54 +0100424 break;
425 case PTS_S_WAIT_RESP_PTS1:
Harald Welte855ba9e2016-02-24 21:00:46 +0100426 byte = ch->pts.resp[_PTS1];
Harald Welte9d3e3822015-11-09 00:50:54 +0100427 /* This must be TA1 */
Harald Welte855ba9e2016-02-24 21:00:46 +0100428 ch->fi = byte >> 4;
429 ch->di = byte & 0xf;
Harald Welte22bf67f2016-02-29 10:18:48 +0100430 TRACE_DEBUG("found Fi=%u Di=%u\r\n", ch->fi, ch->di);
Harald Welte9d3e3822015-11-09 00:50:54 +0100431 break;
432 case PTS_S_WAIT_RESP_PTS2:
Harald Welte855ba9e2016-02-24 21:00:46 +0100433 byte = ch->pts.resp[_PTS2];
Harald Welte9d3e3822015-11-09 00:50:54 +0100434 break;
435 case PTS_S_WAIT_RESP_PTS3:
Harald Welte855ba9e2016-02-24 21:00:46 +0100436 byte = ch->pts.resp[_PTS3];
Harald Welte9d3e3822015-11-09 00:50:54 +0100437 break;
438 case PTS_S_WAIT_RESP_PCK:
Harald Welte855ba9e2016-02-24 21:00:46 +0100439 byte = ch->pts.resp[_PCK];
Harald Welte9d3e3822015-11-09 00:50:54 +0100440 /* update baud rate generator with Fi/Di */
Harald Welte8c496362016-02-27 16:24:09 +0100441 emu_update_fidi(ch);
Harald Welte855ba9e2016-02-24 21:00:46 +0100442 break;
Harald Welted79dc4f2015-11-14 13:32:21 +0100443 default:
Harald Welte22bf67f2016-02-29 10:18:48 +0100444 TRACE_DEBUG("get_byte_pts() in invalid state %u\r\n",
Harald Welted79dc4f2015-11-14 13:32:21 +0100445 ch->pts.state);
Harald Welte855ba9e2016-02-24 21:00:46 +0100446 return 0;
447 }
448
449 /* 2: Transmit the byte */
450 card_emu_uart_tx(ch->uart_chan, byte);
451
452 /* 3: Update the state */
453
454 switch (ch->pts.state) {
455 case PTS_S_WAIT_RESP_PCK:
456 /* Wait for the next TPDU */
457 card_set_state(ch, ISO_S_WAIT_TPDU);
458 set_pts_state(ch, PTS_S_WAIT_REQ_PTSS);
459 break;
460 default:
461 /* calculate the next state and set it */
462 set_pts_state(ch, next_pts_state(ch));
Harald Welted79dc4f2015-11-14 13:32:21 +0100463 break;
Harald Welte9d3e3822015-11-09 00:50:54 +0100464 }
Harald Welted79dc4f2015-11-14 13:32:21 +0100465
Harald Welte855ba9e2016-02-24 21:00:46 +0100466 /* return number of bytes transmitted */
467 return 1;
Harald Welte9d3e3822015-11-09 00:50:54 +0100468}
469
470
471/**********************************************************************
472 * TPDU handling
473 **********************************************************************/
474
Harald Welte4d804672015-11-14 23:02:38 +0100475
476/* compute number of data bytes according to Chapter 10.3.2 of 7816-3 */
477static unsigned int t0_num_data_bytes(uint8_t p3, int reader_to_card)
478{
479 if (reader_to_card) {
480 return p3;
481 } else {
482 if (p3 == 0)
483 return 256;
484 else
485 return p3;
486 }
487}
488
Harald Welte9d3e3822015-11-09 00:50:54 +0100489/* add a just-received TPDU byte (from reader) to USB buffer */
Harald Welte61bb30e2015-11-14 23:44:14 +0100490static void add_tpdu_byte(struct card_handle *ch, uint8_t byte)
Harald Welte9d3e3822015-11-09 00:50:54 +0100491{
492 struct req_ctx *rctx;
493 struct cardemu_usb_msg_rx_data *rd;
Harald Welte4d804672015-11-14 23:02:38 +0100494 unsigned int num_data_bytes = t0_num_data_bytes(ch->tpdu.hdr[_P3], 0);
Harald Welte9d3e3822015-11-09 00:50:54 +0100495
496 /* ensure we have a buffer */
497 if (!ch->uart_rx_ctx) {
Harald Welte4d804672015-11-14 23:02:38 +0100498 rctx = ch->uart_rx_ctx = req_ctx_find_get(0, RCTX_S_FREE, RCTX_S_UART_RX_BUSY);
499 if (!ch->uart_rx_ctx) {
Harald Welte40901a02016-03-03 10:42:45 +0100500 TRACE_ERROR("Received UART byte but ENOMEM\r\n");
Harald Welte9d3e3822015-11-09 00:50:54 +0100501 return;
Harald Welte4d804672015-11-14 23:02:38 +0100502 }
Harald Welte9d3e3822015-11-09 00:50:54 +0100503 rd = (struct cardemu_usb_msg_rx_data *) ch->uart_rx_ctx->data;
504 cardemu_hdr_set(&rd->hdr, CEMU_USB_MSGT_DO_RX_DATA);
505 rctx->tot_len = sizeof(*rd);
506 rctx->idx = 0;
507 } else
508 rctx = ch->uart_rx_ctx;
509
510 rd = (struct cardemu_usb_msg_rx_data *) rctx->data;
511
512 rd->data[rctx->idx++] = byte;
513 rctx->tot_len++;
514
515 /* check if the buffer is full. If so, send it */
Harald Welte4d804672015-11-14 23:02:38 +0100516 if (rctx->tot_len >= sizeof(*rd) + num_data_bytes) {
517 rd->flags |= CEMU_DATA_F_FINAL;
Harald Welteb5288e82015-11-14 21:15:52 +0100518 flush_rx_buffer(ch);
Harald Welte61bb30e2015-11-14 23:44:14 +0100519 /* We need to transmit the SW now, */
520 set_tpdu_state(ch, TPDU_S_WAIT_TX);
Harald Welte4d804672015-11-14 23:02:38 +0100521 } else if (rctx->tot_len >= rctx->size)
522 flush_rx_buffer(ch);
Harald Welte9d3e3822015-11-09 00:50:54 +0100523}
524
525static void set_tpdu_state(struct card_handle *ch, enum tpdu_state new_ts)
526{
Harald Welte05b41c62015-11-14 20:58:48 +0100527 if (ch->tpdu.state == new_ts)
528 return;
529
Harald Welte22bf67f2016-02-29 10:18:48 +0100530 TRACE_DEBUG("7816 TPDU state %u -> %u\r\n", ch->tpdu.state, new_ts);
Harald Welte05b41c62015-11-14 20:58:48 +0100531
Harald Welte9d3e3822015-11-09 00:50:54 +0100532 switch (new_ts) {
533 case TPDU_S_WAIT_CLA:
Harald Welte05b41c62015-11-14 20:58:48 +0100534 case TPDU_S_WAIT_RX:
Harald Welte9d3e3822015-11-09 00:50:54 +0100535 card_emu_uart_enable(ch->uart_chan, ENABLE_RX);
536 break;
537 case TPDU_S_WAIT_PB:
538 /* we just completed the TPDU header from reader to card
539 * and now need to disable the receiver, enable the
540 * transmitter and transmit the procedure byte */
541 card_emu_uart_enable(ch->uart_chan, ENABLE_TX);
542 break;
Harald Weltead434402016-03-02 11:18:29 +0100543 default:
544 break;
Harald Welte9d3e3822015-11-09 00:50:54 +0100545 }
Harald Welte05b41c62015-11-14 20:58:48 +0100546
Harald Welte9d3e3822015-11-09 00:50:54 +0100547 ch->tpdu.state = new_ts;
548}
549
550static enum tpdu_state next_tpdu_state(struct card_handle *ch)
551{
552 switch (ch->tpdu.state) {
553 case TPDU_S_WAIT_CLA:
554 return TPDU_S_WAIT_INS;
555 case TPDU_S_WAIT_INS:
556 return TPDU_S_WAIT_P1;
557 case TPDU_S_WAIT_P1:
558 return TPDU_S_WAIT_P2;
559 case TPDU_S_WAIT_P2:
560 return TPDU_S_WAIT_P3;
561 case TPDU_S_WAIT_P3:
562 return TPDU_S_WAIT_PB;
563 /* simply stay in Rx or Tx by default */
564 case TPDU_S_WAIT_PB:
565 return TPDU_S_WAIT_PB;
566 case TPDU_S_WAIT_RX:
567 return TPDU_S_WAIT_RX;
568 case TPDU_S_WAIT_TX:
569 return TPDU_S_WAIT_TX;
570 }
Harald Welte4c473da2015-11-14 13:31:11 +0100571 /* we should never reach here */
572 assert(0);
573 return -1;
Harald Welte9d3e3822015-11-09 00:50:54 +0100574}
575
576static void send_tpdu_header(struct card_handle *ch)
577{
578 struct req_ctx *rctx;
579 struct cardemu_usb_msg_rx_data *rd;
580
Harald Welte43f79492016-02-29 10:06:54 +0100581 TRACE_DEBUG("%s: %02x %02x %02x %02x %02x\r\n", __func__,
582 ch->tpdu.hdr[0], ch->tpdu.hdr[1],
583 ch->tpdu.hdr[2], ch->tpdu.hdr[3],
584 ch->tpdu.hdr[4]);
585
Harald Welte9d3e3822015-11-09 00:50:54 +0100586 /* if we already/still have a context, send it off */
Harald Weltef1697e22016-03-02 10:28:54 +0100587 if (ch->uart_rx_ctx) {
588 TRACE_DEBUG("have old buffer\r\n");
589 if (ch->uart_rx_ctx->idx) {
590 TRACE_DEBUG("flushing old buffer\r\n");
591 flush_rx_buffer(ch);
592 }
593 } else {
594 TRACE_DEBUG("allocating new buffer\r\n");
595 /* ensure we have a new buffer */
596 ch->uart_rx_ctx = req_ctx_find_get(0, RCTX_S_FREE, RCTX_S_UART_RX_BUSY);
597 if (!ch->uart_rx_ctx) {
598 TRACE_ERROR("%s: ENOMEM\r\n", __func__);
599 return;
600 }
Harald Welte9d3e3822015-11-09 00:50:54 +0100601 }
Harald Welte9d3e3822015-11-09 00:50:54 +0100602 rctx = ch->uart_rx_ctx;
603 rd = (struct cardemu_usb_msg_rx_data *) rctx->data;
604
605 /* initializ header */
606 cardemu_hdr_set(&rd->hdr, CEMU_USB_MSGT_DO_RX_DATA);
607 rd->flags = CEMU_DATA_F_TPDU_HDR;
608 rctx->tot_len = sizeof(*rd) + sizeof(ch->tpdu.hdr);
Harald Welte0ef96d52016-02-25 00:09:17 +0100609 rctx->idx = sizeof(ch->tpdu.hdr);
Harald Welte9d3e3822015-11-09 00:50:54 +0100610
611 /* copy TPDU header to data field */
612 memcpy(rd->data, ch->tpdu.hdr, sizeof(ch->tpdu.hdr));
Harald Welteb5288e82015-11-14 21:15:52 +0100613 /* rd->data_len is set in flush_rx_buffer() */
Harald Welte9d3e3822015-11-09 00:50:54 +0100614
Harald Welteb5288e82015-11-14 21:15:52 +0100615 flush_rx_buffer(ch);
Harald Welte9d3e3822015-11-09 00:50:54 +0100616}
617
618static enum iso7816_3_card_state
619process_byte_tpdu(struct card_handle *ch, uint8_t byte)
620{
621 switch (ch->tpdu.state) {
622 case TPDU_S_WAIT_CLA:
623 ch->tpdu.hdr[_CLA] = byte;
Harald Welte4d804672015-11-14 23:02:38 +0100624 set_tpdu_state(ch, next_tpdu_state(ch));
Harald Welte9d3e3822015-11-09 00:50:54 +0100625 break;
626 case TPDU_S_WAIT_INS:
627 ch->tpdu.hdr[_INS] = byte;
Harald Welte4d804672015-11-14 23:02:38 +0100628 set_tpdu_state(ch, next_tpdu_state(ch));
Harald Welte9d3e3822015-11-09 00:50:54 +0100629 break;
630 case TPDU_S_WAIT_P1:
631 ch->tpdu.hdr[_P1] = byte;
Harald Welte4d804672015-11-14 23:02:38 +0100632 set_tpdu_state(ch, next_tpdu_state(ch));
Harald Welte9d3e3822015-11-09 00:50:54 +0100633 break;
634 case TPDU_S_WAIT_P2:
635 ch->tpdu.hdr[_P2] = byte;
Harald Welte4d804672015-11-14 23:02:38 +0100636 set_tpdu_state(ch, next_tpdu_state(ch));
Harald Welte9d3e3822015-11-09 00:50:54 +0100637 break;
638 case TPDU_S_WAIT_P3:
639 ch->tpdu.hdr[_P3] = byte;
Harald Welte4d804672015-11-14 23:02:38 +0100640 set_tpdu_state(ch, next_tpdu_state(ch));
Harald Welte9d3e3822015-11-09 00:50:54 +0100641 /* FIXME: start timer to transmit further 0x60 */
642 /* send the TPDU header as part of a procedure byte
643 * request to the USB host */
644 send_tpdu_header(ch);
645 break;
646 case TPDU_S_WAIT_RX:
Harald Welte61bb30e2015-11-14 23:44:14 +0100647 add_tpdu_byte(ch, byte);
648 break;
Harald Welte9d3e3822015-11-09 00:50:54 +0100649 default:
Harald Welte22bf67f2016-02-29 10:18:48 +0100650 TRACE_DEBUG("process_byte_tpdu() in invalid state %u\r\n",
Harald Welte9d3e3822015-11-09 00:50:54 +0100651 ch->tpdu.state);
652 }
Harald Welte9d3e3822015-11-09 00:50:54 +0100653
654 /* ensure we stay in TPDU ISO state */
655 return ISO_S_IN_TPDU;
656}
657
Harald Welte855ba9e2016-02-24 21:00:46 +0100658/* tx a single byte to be transmitted to the reader */
659static int tx_byte_tpdu(struct card_handle *ch)
Harald Welte9d3e3822015-11-09 00:50:54 +0100660{
661 struct req_ctx *rctx;
662 struct cardemu_usb_msg_tx_data *td;
Harald Welte855ba9e2016-02-24 21:00:46 +0100663 uint8_t byte;
Harald Welte9d3e3822015-11-09 00:50:54 +0100664
665 /* ensure we are aware of any data that might be pending for
666 * transmit */
667 if (!ch->uart_tx_ctx) {
Harald Welte54cb3d02016-02-29 14:12:40 +0100668 if (llist_empty(&ch->uart_tx_queue))
Harald Welte9d3e3822015-11-09 00:50:54 +0100669 return 0;
670
Harald Welte54cb3d02016-02-29 14:12:40 +0100671 /* dequeue first at head */
672 ch->uart_tx_ctx = llist_entry(ch->uart_tx_queue.next,
673 struct req_ctx, list);
674 llist_del(&ch->uart_tx_ctx->list);
675 req_ctx_set_state(ch->uart_tx_ctx, RCTX_S_UART_TX_BUSY);
676
Harald Welte9d3e3822015-11-09 00:50:54 +0100677 /* start with index zero */
678 ch->uart_tx_ctx->idx = 0;
679
680 }
681 rctx = ch->uart_tx_ctx;
682 td = (struct cardemu_usb_msg_tx_data *) rctx->data;
683
Harald Welte9d3e3822015-11-09 00:50:54 +0100684 /* take the next pending byte out of the rctx */
Harald Welte855ba9e2016-02-24 21:00:46 +0100685 byte = td->data[rctx->idx++];
686
687 card_emu_uart_tx(ch->uart_chan, byte);
Harald Welte9d3e3822015-11-09 00:50:54 +0100688
Harald Weltef16b6182016-02-24 22:18:46 +0100689 /* this must happen _after_ the byte has been transmittd */
690 switch (ch->tpdu.state) {
691 case TPDU_S_WAIT_PB:
692 /* if we just transmitted the procedure byte, we need to decide
693 * if we want to continue to receive or transmit */
694 if (td->flags & CEMU_DATA_F_PB_AND_TX)
695 set_tpdu_state(ch, TPDU_S_WAIT_TX);
696 else if (td->flags & CEMU_DATA_F_PB_AND_RX)
697 set_tpdu_state(ch, TPDU_S_WAIT_RX);
698 break;
Harald Weltead434402016-03-02 11:18:29 +0100699 default:
700 break;
Harald Weltef16b6182016-02-24 22:18:46 +0100701 }
702
Harald Welte9d3e3822015-11-09 00:50:54 +0100703 /* check if the buffer has now been fully transmitted */
704 if ((rctx->idx >= td->hdr.data_len) ||
Harald Weltef16b6182016-02-24 22:18:46 +0100705 (td->data + rctx->idx >= rctx->data + rctx->tot_len)) {
Harald Welte52922ff2015-11-14 20:59:56 +0100706 if (td->flags & CEMU_DATA_F_PB_AND_RX) {
707 /* we have just sent the procedure byte and now
708 * need to continue receiving */
709 set_tpdu_state(ch, TPDU_S_WAIT_RX);
Harald Welte2935b3c2015-11-14 20:00:14 +0100710 } else {
Harald Welte52922ff2015-11-14 20:59:56 +0100711 /* we have transmitted all bytes */
712 if (td->flags & CEMU_DATA_F_FINAL) {
713 /* this was the final part of the APDU, go
714 * back to state one*/
715 card_set_state(ch, ISO_S_WAIT_TPDU);
716 } else {
717 /* FIXME: call into USB code to chec if we need
718 * to submit a free buffer to accept
719 * further data on bulk out endpoint */
720 }
Harald Welte2935b3c2015-11-14 20:00:14 +0100721 }
Harald Welte9d3e3822015-11-09 00:50:54 +0100722 req_ctx_set_state(rctx, RCTX_S_FREE);
723 ch->uart_tx_ctx = NULL;
Harald Welte9d3e3822015-11-09 00:50:54 +0100724 }
725
726 return 1;
727}
728
729/**********************************************************************
730 * Public API
731 **********************************************************************/
732
733/* process a single byte received from the reader */
734void card_emu_process_rx_byte(struct card_handle *ch, uint8_t byte)
735{
736 int new_state = -1;
737
738 ch->stats.rx_bytes++;
739
740 switch (ch->state) {
741 case ISO_S_WAIT_POWER:
742 case ISO_S_WAIT_CLK:
743 case ISO_S_WAIT_RST:
744 case ISO_S_WAIT_ATR:
Harald Welte22bf67f2016-02-29 10:18:48 +0100745 TRACE_DEBUG("Received UART char in 7816 state %u\r\n",
Harald Welte4d804672015-11-14 23:02:38 +0100746 ch->state);
Harald Welte9d3e3822015-11-09 00:50:54 +0100747 /* we shouldn't receive any data from the reader yet! */
748 break;
749 case ISO_S_WAIT_TPDU:
750 if (byte == 0xff) {
751 new_state = process_byte_pts(ch, byte);
752 ch->stats.pps++;
753 goto out_silent;
754 }
755 /* fall-through */
756 case ISO_S_IN_TPDU:
757 new_state = process_byte_tpdu(ch, byte);
758 break;
759 case ISO_S_IN_PTS:
760 new_state = process_byte_pts(ch, byte);
761 goto out_silent;
762 }
763
764out_silent:
765 if (new_state != -1)
766 card_set_state(ch, new_state);
767}
768
Harald Welte855ba9e2016-02-24 21:00:46 +0100769/* transmit a single byte to the reader */
770int card_emu_tx_byte(struct card_handle *ch)
Harald Welte9d3e3822015-11-09 00:50:54 +0100771{
772 int rc = 0;
773
774 switch (ch->state) {
775 case ISO_S_IN_ATR:
776 if (ch->atr.idx < ch->atr.len) {
Harald Welte855ba9e2016-02-24 21:00:46 +0100777 uint8_t byte;
778 byte = ch->atr.atr[ch->atr.idx++];
Harald Welte9d3e3822015-11-09 00:50:54 +0100779 rc = 1;
Harald Welte855ba9e2016-02-24 21:00:46 +0100780
781 card_emu_uart_tx(ch->uart_chan, byte);
782
Harald Welte9d3e3822015-11-09 00:50:54 +0100783 /* detect end of ATR */
784 if (ch->atr.idx >= ch->atr.len)
785 card_set_state(ch, ISO_S_WAIT_TPDU);
786 }
787 break;
788 case ISO_S_IN_PTS:
Harald Welte855ba9e2016-02-24 21:00:46 +0100789 rc = tx_byte_pts(ch);
Harald Welte9d3e3822015-11-09 00:50:54 +0100790 break;
791 case ISO_S_IN_TPDU:
Harald Welte855ba9e2016-02-24 21:00:46 +0100792 rc = tx_byte_tpdu(ch);
Harald Welte9d3e3822015-11-09 00:50:54 +0100793 break;
Harald Weltead434402016-03-02 11:18:29 +0100794 default:
795 break;
Harald Welte9d3e3822015-11-09 00:50:54 +0100796 }
797
798 if (rc)
799 ch->stats.tx_bytes++;
800
Harald Welte4d804672015-11-14 23:02:38 +0100801 /* if we return 0 here, the UART needs to disable transmit-ready
802 * interrupts */
Harald Welte9d3e3822015-11-09 00:50:54 +0100803 return rc;
804}
805
Harald Welteacae4122016-03-02 10:27:58 +0100806void card_emu_have_new_uart_tx(struct card_handle *ch)
807{
808 switch (ch->state) {
809 case ISO_S_IN_TPDU:
810 switch (ch->tpdu.state) {
811 case TPDU_S_WAIT_TX:
812 case TPDU_S_WAIT_PB:
813 card_emu_uart_enable(ch->uart_chan, ENABLE_TX);
814 break;
815 default:
816 break;
817 }
818 default:
819 break;
820 }
821}
822
Harald Welte9d3e3822015-11-09 00:50:54 +0100823/* hardware driver informs us that a card I/O signal has changed */
824void card_emu_io_statechg(struct card_handle *ch, enum card_io io, int active)
825{
826 switch (io) {
827 case CARD_IO_VCC:
Harald Welte47ee2832016-02-29 10:09:46 +0100828 if (active == 0 && ch->vcc_active == 1) {
Harald Welte22bf67f2016-02-29 10:18:48 +0100829 TRACE_DEBUG("VCC deactivated\r\n");
Harald Welte22cdf2a2016-02-24 22:18:11 +0100830 tc_etu_disable(ch->tc_chan);
Harald Welte9d3e3822015-11-09 00:50:54 +0100831 card_set_state(ch, ISO_S_WAIT_POWER);
Harald Welte47ee2832016-02-29 10:09:46 +0100832 } else if (active == 1 && ch->vcc_active == 0) {
Harald Welte22bf67f2016-02-29 10:18:48 +0100833 TRACE_DEBUG("VCC activated\r\n");
Harald Welte9d3e3822015-11-09 00:50:54 +0100834 card_set_state(ch, ISO_S_WAIT_CLK);
Harald Welte47ee2832016-02-29 10:09:46 +0100835 }
Harald Welte9d3e3822015-11-09 00:50:54 +0100836 ch->vcc_active = active;
837 break;
838 case CARD_IO_CLK:
Harald Welte47ee2832016-02-29 10:09:46 +0100839 if (active == 1 && ch->clocked == 0) {
Harald Welte22bf67f2016-02-29 10:18:48 +0100840 TRACE_DEBUG("CLK activated\r\n");
Harald Welte47ee2832016-02-29 10:09:46 +0100841 if (ch->state == ISO_S_WAIT_CLK)
842 card_set_state(ch, ISO_S_WAIT_RST);
843 } else if (active == 0 && ch->clocked == 1) {
Harald Welte22bf67f2016-02-29 10:18:48 +0100844 TRACE_DEBUG("CLK deactivated\r\n");
Harald Welte47ee2832016-02-29 10:09:46 +0100845 }
Harald Welte9d3e3822015-11-09 00:50:54 +0100846 ch->clocked = active;
847 break;
848 case CARD_IO_RST:
Harald Welte47ee2832016-02-29 10:09:46 +0100849 if (active == 0 && ch->in_reset) {
Harald Welte40901a02016-03-03 10:42:45 +0100850 TRACE_INFO("RST released\r\n");
Harald Welte47ee2832016-02-29 10:09:46 +0100851 if (ch->vcc_active && ch->clocked) {
852 /* enable the TC/ETU counter once reset has been released */
853 tc_etu_enable(ch->tc_chan);
854 card_set_state(ch, ISO_S_WAIT_ATR);
855 /* FIXME: wait 400 to 40k clock cycles before sending ATR */
856 card_set_state(ch, ISO_S_IN_ATR);
857 }
858 } else if (active && !ch->in_reset) {
Harald Welte40901a02016-03-03 10:42:45 +0100859 TRACE_INFO("RST asserted\r\n");
Harald Welte22cdf2a2016-02-24 22:18:11 +0100860 tc_etu_disable(ch->tc_chan);
Harald Welte9d3e3822015-11-09 00:50:54 +0100861 }
862 ch->in_reset = active;
863 break;
864 }
865}
866
867/* User sets a new ATR to be returned during next card reset */
868int card_emu_set_atr(struct card_handle *ch, const uint8_t *atr, uint8_t len)
869{
870 if (len > sizeof(ch->atr.atr))
871 return -1;
872
873 memcpy(ch->atr.atr, atr, len);
874 ch->atr.len = len;
875 ch->atr.idx = 0;
876
877 /* FIXME: race condition with trasmitting ATR to reader? */
878
879 return 0;
880}
881
882/* hardware driver informs us that one (more) ETU has expired */
883void tc_etu_wtime_half_expired(void *handle)
884{
885 struct card_handle *ch = handle;
886 /* transmit NULL procedure byte well before waiting time expires */
Harald Weltedda73552016-03-02 10:29:55 +0100887 switch (ch->state) {
888 case ISO_S_IN_TPDU:
889 switch (ch->tpdu.state) {
890 case TPDU_S_WAIT_PB:
891 case TPDU_S_WAIT_TX:
892 putchar('N');
893 card_emu_uart_tx(ch->uart_chan, ISO7816_3_PB_NULL);
894 break;
895 default:
896 break;
897 }
898 break;
899 default:
900 break;
901 }
Harald Welte9d3e3822015-11-09 00:50:54 +0100902}
903
904/* hardware driver informs us that one (more) ETU has expired */
905void tc_etu_wtime_expired(void *handle)
906{
Harald Welte40901a02016-03-03 10:42:45 +0100907 TRACE_ERROR("wtime_exp\r\n");
Harald Welte9d3e3822015-11-09 00:50:54 +0100908}
909
910/* shortest ATR found in smartcard_list.txt */
911static const uint8_t default_atr[] = { 0x3B, 0x02, 0x14, 0x50 };
912
913static struct card_handle card_handles[NUM_SLOTS];
914
915struct card_handle *card_emu_init(uint8_t slot_num, uint8_t tc_chan, uint8_t uart_chan)
916{
917 struct card_handle *ch;
918
919 if (slot_num >= ARRAY_SIZE(card_handles))
920 return NULL;
921
922 ch = &card_handles[slot_num];
923
924 memset(ch, 0, sizeof(*ch));
925
Harald Welte54cb3d02016-02-29 14:12:40 +0100926 INIT_LLIST_HEAD(&ch->usb_tx_queue);
927 INIT_LLIST_HEAD(&ch->uart_tx_queue);
928
Harald Welte9d3e3822015-11-09 00:50:54 +0100929 /* initialize the card_handle with reasonabe defaults */
930 ch->state = ISO_S_WAIT_POWER;
931 ch->vcc_active = 0;
932 ch->in_reset = 1;
933 ch->clocked = 0;
934
935 ch->fi = 0;
936 ch->di = 1;
937 ch->wi = ISO7816_3_DEFAULT_WI;
938
939 ch->tc_chan = tc_chan;
940 ch->uart_chan = uart_chan;
941 ch->waiting_time = ISO7816_3_INIT_WTIME;
942
943 ch->atr.idx = 0;
944 ch->atr.len = sizeof(default_atr);
945 memcpy(ch->atr.atr, default_atr, ch->atr.len);
946
947 ch->pts.state = PTS_S_WAIT_REQ_PTSS;
948 ch->tpdu.state = TPDU_S_WAIT_CLA;
949
950 tc_etu_init(ch->tc_chan, ch);
951
952 return ch;
953}