blob: 1c691afa767d347ade5cec01a7a402af297bd50f [file] [log] [blame]
Kévin Redon9a12d682018-07-08 13:21:16 +02001/* SIMtrace 2 sniffer mode
2 *
3 * (C) 2016-2017 by Harald Welte <hwelte@hmw-consulting.de>
4 * (C) 2018 by sysmocom -s.f.m.c. GmbH, Author: Kevin Redon <kredon@sysmocom.de>
Christina Quasta90eefa2015-02-24 17:52:29 +01005 *
Kévin Redon45ad62d2018-06-07 18:56:41 +02006 * This program is free software; you can redistribute it and/or modify
Kévin Redon9a12d682018-07-08 13:21:16 +02007 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
Kévin Redon45ad62d2018-06-07 18:56:41 +02009 * (at your option) any later version.
Christina Quasta90eefa2015-02-24 17:52:29 +010010 *
Kévin Redon45ad62d2018-06-07 18:56:41 +020011 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
Kévin Redon9a12d682018-07-08 13:21:16 +020014 * GNU General Public License for more details.
Christina Quasta90eefa2015-02-24 17:52:29 +010015 *
Kévin Redon9a12d682018-07-08 13:21:16 +020016 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA
Christina Quasta90eefa2015-02-24 17:52:29 +010019 */
Kévin Redon9a12d682018-07-08 13:21:16 +020020/* This code implement the Sniffer mode to sniff the communication between a
21 * SIM card (or any ISO 7816 smart card) and a phone (or any ISO 7816 card
22 * reader).
23 * For historical reasons (i.e. SIMtrace hardware) the USART peripheral
24 * connected to the SIM card is used.
25 * TODO put common ISO7816-3 code is separate library (and combine clean with
26 * iso7816_4)
Kévin Redond7a6de52018-06-11 13:46:35 +020027 */
Kévin Redon36abece2018-06-04 16:30:01 +020028#include "board.h"
29#include "simtrace.h"
30
Harald Welte2fb59962016-02-28 12:34:26 +010031#ifdef HAVE_SNIFFER
32
Christina Quasta90eefa2015-02-24 17:52:29 +010033/*------------------------------------------------------------------------------
34 * Headers
35 *------------------------------------------------------------------------------*/
36
Christina Quasta90eefa2015-02-24 17:52:29 +010037#include <string.h>
Kévin Redon8fa6ff52018-06-25 16:00:33 +020038#include "utils.h"
39#include "iso7816_fidi.h"
Kévin Redonde97fd22018-07-01 18:23:23 +020040/* USB related libraries */
41#include "osmocom/core/linuxlist.h"
42#include "osmocom/core/msgb.h"
43#include "llist_irqsafe.h"
44#include "usb_buf.h"
45#include "simtrace_usb.h"
46#include "simtrace_prot.h"
Christina Quasta90eefa2015-02-24 17:52:29 +010047
48/*------------------------------------------------------------------------------
49 * Internal definitions
50 *------------------------------------------------------------------------------*/
51
Kévin Redon8fa6ff52018-06-25 16:00:33 +020052/*! Maximum Answer-To-Reset (ATR) size in bytes
Kévin Redond7a6de52018-06-11 13:46:35 +020053 * @note defined in ISO/IEC 7816-3:2006(E) section 8.2.1 as 32, on top the initial character TS of section 8.1
54 * @remark technical there is no size limitation since Yi present in T0,TDi will indicate if more interface bytes are present, including TDi+i
55 */
56#define MAX_ATR_SIZE 33
Kévin Redon8fa6ff52018-06-25 16:00:33 +020057/*! Maximum Protocol and Parameters Selection (PPS) size in bytes
58 * @note defined in ISO/IEC 7816-3:2006(E) section 9.2
59 */
60#define MAX_PPS_SIZE 6
Kévin Redond7a6de52018-06-11 13:46:35 +020061
62/*! ISO 7816-3 states relevant to the sniff mode */
63enum iso7816_3_sniff_state {
64 ISO7816_S_RESET, /*!< in Reset */
65 ISO7816_S_WAIT_ATR, /*!< waiting for ATR to start */
66 ISO7816_S_IN_ATR, /*!< while we are receiving the ATR */
Kévin Redon00ec89d2018-06-27 16:41:52 +020067 ISO7816_S_WAIT_TPDU, /*!< waiting for start of new TPDU */
68 ISO7816_S_IN_TPDU, /*!< inside a single TPDU */
Kévin Redon8fa6ff52018-06-25 16:00:33 +020069 ISO7816_S_IN_PPS_REQ, /*!< while we are inside the PPS request */
70 ISO7816_S_WAIT_PPS_RSP, /*!< waiting for start of the PPS response */
71 ISO7816_S_IN_PPS_RSP, /*!< while we are inside the PPS request */
Kévin Redond7a6de52018-06-11 13:46:35 +020072};
73
74/*! Answer-To-Reset (ATR) sub-states of ISO7816_S_IN_ATR
75 * @note defined in ISO/IEC 7816-3:2006(E) section 8
76 */
77enum atr_sniff_state {
78 ATR_S_WAIT_TS, /*!< initial byte */
79 ATR_S_WAIT_T0, /*!< format byte */
80 ATR_S_WAIT_TA, /*!< first sub-group interface byte */
81 ATR_S_WAIT_TB, /*!< second sub-group interface byte */
82 ATR_S_WAIT_TC, /*!< third sub-group interface byte */
83 ATR_S_WAIT_TD, /*!< fourth sub-group interface byte */
84 ATR_S_WAIT_HIST, /*!< historical byte */
85 ATR_S_WAIT_TCK, /*!< check byte */
Kévin Redon8fa6ff52018-06-25 16:00:33 +020086};
87
88/*! Protocol and Parameters Selection (PPS) sub-states of ISO7816_S_IN_PTS_REQ/ISO7816_S_IN_PTS_RSP
89 * @note defined in ISO/IEC 7816-3:2006(E) section 9
90 */
91enum pps_sniff_state {
92 PPS_S_WAIT_PPSS, /*!< initial byte */
93 PPS_S_WAIT_PPS0, /*!< format byte */
94 PPS_S_WAIT_PPS1, /*!< first parameter byte */
95 PPS_S_WAIT_PPS2, /*!< second parameter byte */
96 PPS_S_WAIT_PPS3, /*!< third parameter byte */
97 PPS_S_WAIT_PCK, /*!< check byte */
Kévin Redon35e8bdf2018-07-03 16:15:58 +020098 PPS_S_WAIT_END, /*!< all done */
Kévin Redond7a6de52018-06-11 13:46:35 +020099};
Kévin Redon45ad62d2018-06-07 18:56:41 +0200100
Kévin Redon00ec89d2018-06-27 16:41:52 +0200101/*! Transport Protocol Data Unit (TPDU) sub-states of ISO7816_S_IN_TPDU
102 * @note defined in ISO/IEC 7816-3:2006(E) section 10 and 12
103 * @remark APDUs are formed by one or more command+response TPDUs
104 */
105enum tpdu_sniff_state {
106 TPDU_S_CLA, /*!< class byte */
107 TPDU_S_INS, /*!< instruction byte */
108 TPDU_S_P1, /*!< first parameter byte for the instruction */
109 TPDU_S_P2, /*!< second parameter byte for the instruction */
110 TPDU_S_P3, /*!< third parameter byte encoding the data length */
111 TPDU_S_PROCEDURE, /*!< procedure byte (could also be SW1) */
112 TPDU_S_DATA_REMAINING, /*!< remaining data bytes */
113 TPDU_S_DATA_SINGLE, /*!< single data byte */
114 TPDU_S_SW1, /*!< first status word */
115 TPDU_S_SW2, /*!< second status word */
116};
117
Christina Quasta90eefa2015-02-24 17:52:29 +0100118/*------------------------------------------------------------------------------
119 * Internal variables
120 *------------------------------------------------------------------------------*/
Kévin Redond7a6de52018-06-11 13:46:35 +0200121
122/* note: the sniffer code is currently designed to support only one sniffing interface, but the hardware would support a second one.
123 * to support a second sniffer interface the code should be restructured to use handles.
124 */
125/* Pin configurations */
Kévin Redon8fa6ff52018-06-25 16:00:33 +0200126/*! Pin configuration to sniff communication (using USART connection card) */
Kévin Redonee62a9d2018-06-11 13:42:23 +0200127static const Pin pins_sniff[] = { PINS_SIM_SNIFF };
Kévin Redon8fa6ff52018-06-25 16:00:33 +0200128/*! Pin configuration to interconnect phone and card using the bus switch */
Harald Welte7dd3dfd2016-03-03 12:32:04 +0100129static const Pin pins_bus[] = { PINS_BUS_SNIFF };
Kévin Redon8fa6ff52018-06-25 16:00:33 +0200130/*! Pin configuration to power the card by the phone */
Kévin Redond7a6de52018-06-11 13:46:35 +0200131static const Pin pins_power[] = { PINS_PWR_SNIFF };
Kévin Redon8fa6ff52018-06-25 16:00:33 +0200132/*! Pin configuration for timer counter to measure ETU timing */
Kévin Redon45ad62d2018-06-07 18:56:41 +0200133static const Pin pins_tc[] = { PINS_TC };
Kévin Redon8fa6ff52018-06-25 16:00:33 +0200134/*! Pin configuration for card reset line */
135static const Pin pin_rst = PIN_SIM_RST_SNIFF;
136
Kévin Redond7a6de52018-06-11 13:46:35 +0200137/* USART related variables */
Kévin Redon8fa6ff52018-06-25 16:00:33 +0200138/*! USART peripheral used to sniff communication */
Kévin Redon45ad62d2018-06-07 18:56:41 +0200139static struct Usart_info sniff_usart = {
140 .base = USART_SIM,
141 .id = ID_USART_SIM,
Harald Welte7dd3dfd2016-03-03 12:32:04 +0100142 .state = USART_RCV,
143};
Kévin Redon8fa6ff52018-06-25 16:00:33 +0200144/*! Ring buffer to store sniffer communication data */
Kévin Redon7b734622018-06-06 16:13:48 +0200145static struct ringbuf sniff_buffer;
146
Kévin Redonde97fd22018-07-01 18:23:23 +0200147/* Flags to know is the card status changed (see SIMTRACE_MSGT_DT_SNIFF_CHANGE flags) */
148volatile uint32_t change_flags = 0;
149
Kévin Redon8fa6ff52018-06-25 16:00:33 +0200150/* ISO 7816 variables */
151/*! ISO 7816-3 state */
152enum iso7816_3_sniff_state iso_state = ISO7816_S_RESET;
153/*! ATR state */
154enum atr_sniff_state atr_state;
155/*! ATR data
156 * @remark can be used to check later protocol changes
157 */
158uint8_t atr[MAX_ATR_SIZE];
159/*! Current index in the ATR data */
160uint8_t atr_i = 0;
161/*! If convention conversion is needed */
162bool convention_convert = false;
163/*! The supported T protocols */
164uint16_t t_protocol_support = 0;
165/*! PPS state
166 * @remark it is shared between request and response since they aren't simultaneous but follow the same procedure
167 */
168enum pps_sniff_state pps_state;
169/*! PPS request data
170 * @remark can be used to check PPS response
171 */
172uint8_t pps_req[MAX_PPS_SIZE];
173/*! PPS response data */
174uint8_t pps_rsp[MAX_PPS_SIZE];
Kévin Redon00ec89d2018-06-27 16:41:52 +0200175/*! TPDU state */
176enum tpdu_sniff_state tpdu_state;
177/*! Final TPDU packet
178 * @note this is the complete command+response TPDU, including header, data, and status words
179 * @remark this does not include the procedure bytes
180 */
181uint8_t tpdu_packet[5+256+2];
182/*! Current index in TPDU packet */
Kévin Redonc9bd7152018-07-03 16:17:00 +0200183uint16_t tpdu_packet_i = 0;
Kévin Redon8fa6ff52018-06-25 16:00:33 +0200184
King Kévin1200a522018-07-04 10:18:03 +0200185/*! Waiting Time (WT)
186 * @note defined in ISO/IEC 7816-3:2006(E) section 8.1 and 10.2
187 */
188uint32_t wt = 9600;
189
Kévin Redon7b734622018-06-06 16:13:48 +0200190/*------------------------------------------------------------------------------
Kévin Redon7b734622018-06-06 16:13:48 +0200191 * Internal functions
192 *------------------------------------------------------------------------------*/
Kévin Redon36abece2018-06-04 16:30:01 +0200193
Kévin Redon8fa6ff52018-06-25 16:00:33 +0200194/*! Convert data between direct and inverse convention
195 * @note direct convention is LSb first and HIGH=1; inverse conversion in MSb first and LOW=1
196 * @remark use a look up table to speed up conversion
197 */
198static const uint8_t convention_convert_lut[256] = { 0xff, 0x7f, 0xbf, 0x3f, 0xdf, 0x5f, 0x9f, 0x1f, 0xef, 0x6f, 0xaf, 0x2f, 0xcf, 0x4f, 0x8f, 0x0f, 0xf7, 0x77, 0xb7, 0x37, 0xd7, 0x57, 0x97, 0x17, 0xe7, 0x67, 0xa7, 0x27, 0xc7, 0x47, 0x87, 0x07, 0xfb, 0x7b, 0xbb, 0x3b, 0xdb, 0x5b, 0x9b, 0x1b, 0xeb, 0x6b, 0xab, 0x2b, 0xcb, 0x4b, 0x8b, 0x0b, 0xf3, 0x73, 0xb3, 0x33, 0xd3, 0x53, 0x93, 0x13, 0xe3, 0x63, 0xa3, 0x23, 0xc3, 0x43, 0x83, 0x03, 0xfd, 0x7d, 0xbd, 0x3d, 0xdd, 0x5d, 0x9d, 0x1d, 0xed, 0x6d, 0xad, 0x2d, 0xcd, 0x4d, 0x8d, 0x0d, 0xf5, 0x75, 0xb5, 0x35, 0xd5, 0x55, 0x95, 0x15, 0xe5, 0x65, 0xa5, 0x25, 0xc5, 0x45, 0x85, 0x05, 0xf9, 0x79, 0xb9, 0x39, 0xd9, 0x59, 0x99, 0x19, 0xe9, 0x69, 0xa9, 0x29, 0xc9, 0x49, 0x89, 0x09, 0xf1, 0x71, 0xb1, 0x31, 0xd1, 0x51, 0x91, 0x11, 0xe1, 0x61, 0xa1, 0x21, 0xc1, 0x41, 0x81, 0x01, 0xfe, 0x7e, 0xbe, 0x3e, 0xde, 0x5e, 0x9e, 0x1e, 0xee, 0x6e, 0xae, 0x2e, 0xce, 0x4e, 0x8e, 0x0e, 0xf6, 0x76, 0xb6, 0x36, 0xd6, 0x56, 0x96, 0x16, 0xe6, 0x66, 0xa6, 0x26, 0xc6, 0x46, 0x86, 0x06, 0xfa, 0x7a, 0xba, 0x3a, 0xda, 0x5a, 0x9a, 0x1a, 0xea, 0x6a, 0xaa, 0x2a, 0xca, 0x4a, 0x8a, 0x0a, 0xf2, 0x72, 0xb2, 0x32, 0xd2, 0x52, 0x92, 0x12, 0xe2, 0x62, 0xa2, 0x22, 0xc2, 0x42, 0x82, 0x02, 0xfc, 0x7c, 0xbc, 0x3c, 0xdc, 0x5c, 0x9c, 0x1c, 0xec, 0x6c, 0xac, 0x2c, 0xcc, 0x4c, 0x8c, 0x0c, 0xf4, 0x74, 0xb4, 0x34, 0xd4, 0x54, 0x94, 0x14, 0xe4, 0x64, 0xa4, 0x24, 0xc4, 0x44, 0x84, 0x04, 0xf8, 0x78, 0xb8, 0x38, 0xd8, 0x58, 0x98, 0x18, 0xe8, 0x68, 0xa8, 0x28, 0xc8, 0x48, 0x88, 0x08, 0xf0, 0x70, 0xb0, 0x30, 0xd0, 0x50, 0x90, 0x10, 0xe0, 0x60, 0xa0, 0x20, 0xc0, 0x40, 0x80, 0x00, };
199
King Kévin1200a522018-07-04 10:18:03 +0200200/*! Update Waiting Time (WT)
201 * @param[in] wi Waiting Integer (0 if unchanged)
202 * @param[in] d Baud Rate divider (0 if unchanged)
203 * @note set wt to be used by the receiver timeout
204 * @note defined in ISO/IEC 7816-3:2006(E) section 8.1 and 10.2
205 */
206static void update_wt(uint8_t wi, uint8_t d)
207{
208 static uint8_t wt_wi = 10; /* Waiting time Integer (WI), used to calculate the Waiting Time (WT) */
209 static uint8_t wt_d = 1; /* baud rate adjustment integer (the actual value, not the table index) */
210
Kévin Redon55f06122018-07-08 14:13:29 +0200211 if (0 != wi) {
King Kévin1200a522018-07-04 10:18:03 +0200212 wt_wi = wi;
213 }
Kévin Redon55f06122018-07-08 14:13:29 +0200214 if (0 != d) {
King Kévin1200a522018-07-04 10:18:03 +0200215 wt_d = d;
216 }
217 wt = wt_wi*960UL*wt_d;
218 TRACE_INFO("WT updated to %u\n\r", wt);
219}
220
Kévin Redonde97fd22018-07-01 18:23:23 +0200221/*! Allocate USB buffer and push + initialize simtrace_msg_hdr
222 * @param[in] ep USB IN endpoint where the message will be sent to
223 * @param[in] msg_class SIMtrace USB message class
224 * @param[in] msg_type SIMtrace USB message type
225 * @return USB message with allocated ans initialized header, or NULL if allocation failed
226 */
227static struct msgb *usb_msg_alloc_hdr(uint8_t ep, uint8_t msg_class, uint8_t msg_type)
228{
229 struct msgb *usb_msg = usb_buf_alloc(SIMTRACE_USB_EP_CARD_DATAIN);
230 if (!usb_msg) {
231 return NULL;
232 }
233 struct simtrace_msg_hdr *usb_msg_header;
234 usb_msg->l1h = msgb_put(usb_msg, sizeof(*usb_msg_header));
235 usb_msg_header = (struct simtrace_msg_hdr *) usb_msg->l1h;
236 memset(usb_msg_header, 0, sizeof(*usb_msg_header));
Kévin Redonf82f0f62018-07-08 15:10:23 +0200237 usb_msg_header->msg_class = msg_class;
238 usb_msg_header->msg_type = msg_type;
Kévin Redonde97fd22018-07-01 18:23:23 +0200239 usb_msg->l2h = usb_msg->l1h + sizeof(*usb_msg_header);
240
241 return usb_msg;
242}
243
244/* update SIMtrace header msg_len and submit USB buffer
245 * param[in] usb_msg USB message to update and send
246 */
247void usb_msg_upd_len_and_submit(struct msgb *usb_msg)
248{
249 struct simtrace_msg_hdr *usb_msg_header = (struct simtrace_msg_hdr *) usb_msg->l1h;
250 usb_msg_header->msg_len = msgb_length(usb_msg);
251 usb_buf_submit(usb_msg);
252}
253
Kévin Redon8fa6ff52018-06-25 16:00:33 +0200254/*! Update the ISO 7816-3 state
255 * @param[in] iso_state_new new ISO 7816-3 state to update to
256 */
257static void change_state(enum iso7816_3_sniff_state iso_state_new)
258{
259 /* sanity check */
Kévin Redon55f06122018-07-08 14:13:29 +0200260 if (iso_state_new == iso_state) {
Kévin Redon8fa6ff52018-06-25 16:00:33 +0200261 TRACE_WARNING("Already in ISO 7816 state %u\n\r", iso_state);
262 return;
263 }
264
265 /* handle actions to perform when switching state */
266 switch (iso_state_new) {
267 case ISO7816_S_RESET:
268 update_fidi(&sniff_usart, 0x11); /* reset baud rate to default Di/Fi values */
King Kévin1200a522018-07-04 10:18:03 +0200269 update_wt(10, 1); /* reset WT time-out */
Kévin Redon8fa6ff52018-06-25 16:00:33 +0200270 break;
271 case ISO7816_S_WAIT_ATR:
272 rbuf_reset(&sniff_buffer); /* reset buffer for new communication */
Kévin Redon8fa6ff52018-06-25 16:00:33 +0200273 break;
274 case ISO7816_S_IN_ATR:
275 atr_i = 0;
276 convention_convert = false;
277 t_protocol_support = 0;
278 atr_state = ATR_S_WAIT_TS;
279 break;
280 case ISO7816_S_IN_PPS_REQ:
281 case ISO7816_S_IN_PPS_RSP:
282 pps_state = PPS_S_WAIT_PPSS;
283 break;
Kévin Redon00ec89d2018-06-27 16:41:52 +0200284 case ISO7816_S_WAIT_TPDU:
285 tpdu_state = TPDU_S_CLA;
286 tpdu_packet_i = 0;
287 break;
Kévin Redon8fa6ff52018-06-25 16:00:33 +0200288 default:
289 break;
290 }
291
292 /* save new state */
293 iso_state = iso_state_new;
Kévin Redoncf599192018-06-27 16:38:31 +0200294 //TRACE_INFO("Changed to ISO 7816-3 state %u\n\r", iso_state); /* don't print since this is function is also called by ISRs */
295}
296
Kévin Redonf82f0f62018-07-08 15:10:23 +0200297static void usb_send_data(enum simtrace_msg_type_sniff type, const uint8_t* data, uint16_t length, uint32_t flags)
298{
299 /* Sanity check */
300 if (type != SIMTRACE_MSGT_SNIFF_ATR && type != SIMTRACE_MSGT_SNIFF_PPS && type != SIMTRACE_MSGT_SNIFF_TPDU) {
301 return;
302 }
303
304 /* Show activity on LED */
Kévin Redonda5578b2018-07-08 16:32:20 +0200305 led_blink(LED_GREEN, BLINK_2F_O);
Kévin Redonf82f0f62018-07-08 15:10:23 +0200306
Kévin Redonf82f0f62018-07-08 15:10:23 +0200307 /* Print message */
308 switch (type) {
309 case SIMTRACE_MSGT_SNIFF_ATR:
310 printf("ATR");
311 break;
312 case SIMTRACE_MSGT_SNIFF_PPS:
313 printf("PPS");
314 break;
315 case SIMTRACE_MSGT_SNIFF_TPDU:
316 printf("TPDU");
317 break;
318 default:
319 printf("???");
320 break;
321 }
322 if (flags) {
323 printf(" (");
324 if (flags & SNIFF_DATA_FLAG_ERROR_INCOMPLETE) {
325 printf("incomplete");
326 flags &= ~SNIFF_DATA_FLAG_ERROR_INCOMPLETE;
327 if (flags) {
328 printf(", ");
329 }
330 }
331 if (flags & SNIFF_DATA_FLAG_ERROR_MALFORMED) {
332 printf("malformed");
333 flags &= ~SNIFF_DATA_FLAG_ERROR_MALFORMED;
334 if (flags) {
335 printf(", ");
336 }
337 }
Kévin Redona95bb1e2018-07-08 17:17:49 +0200338 putchar(')');
Kévin Redonf82f0f62018-07-08 15:10:23 +0200339 }
340 printf(": ");
341 uint16_t i;
342 for (i = 0; i < length; i++) {
343 printf("%02x ", data[i]);
344 }
345 printf("\n\r");
Kévin Redon411428e2018-07-08 15:47:38 +0200346
347 /* Send data over USB */
348 struct msgb *usb_msg = usb_msg_alloc_hdr(SIMTRACE_USB_EP_CARD_DATAIN, SIMTRACE_MSGC_SNIFF, type);
349 if (!usb_msg) {
350 return;
351 }
352 struct sniff_data *usb_sniff_data = (struct sniff_data *) msgb_put(usb_msg, sizeof(*usb_sniff_data));
353 usb_sniff_data->flags = flags;
354 usb_sniff_data->length = length;
355 uint8_t *sniff_data = msgb_put(usb_msg, usb_sniff_data->length);
356 memcpy(sniff_data, data, length);
357 usb_msg_upd_len_and_submit(usb_msg);
358
Kévin Redonf82f0f62018-07-08 15:10:23 +0200359}
360
Kévin Redonde97fd22018-07-01 18:23:23 +0200361/*! Send current ATR over USB
Kévin Redonf82f0f62018-07-08 15:10:23 +0200362 * @param[in] flags SNIFF_DATA_FLAG_ data flags
Kévin Redon35e8bdf2018-07-03 16:15:58 +0200363 * @note Also print the ATR to debug console
Kévin Redonde97fd22018-07-01 18:23:23 +0200364 */
Kévin Redonf82f0f62018-07-08 15:10:23 +0200365static void usb_send_atr(uint32_t flags)
Kévin Redoncf599192018-06-27 16:38:31 +0200366{
Kévin Redonde97fd22018-07-01 18:23:23 +0200367 /* Check state */
Kévin Redon55f06122018-07-08 14:13:29 +0200368 if (ISO7816_S_IN_ATR != iso_state) {
Kévin Redoncf599192018-06-27 16:38:31 +0200369 TRACE_WARNING("Can't print ATR in ISO 7816-3 state %u\n\r", iso_state);
370 return;
371 }
Kévin Redon55f06122018-07-08 14:13:29 +0200372 if (atr_i >= ARRAY_SIZE(atr)) {
Kévin Redonde97fd22018-07-01 18:23:23 +0200373 TRACE_ERROR("ATR buffer overflow\n\r");
374 return;
375 }
Kévin Redoncf599192018-06-27 16:38:31 +0200376
Kévin Redonde97fd22018-07-01 18:23:23 +0200377 /* Send ATR over USB */
Kévin Redonf82f0f62018-07-08 15:10:23 +0200378 usb_send_data(SIMTRACE_MSGT_SNIFF_ATR, atr, atr_i, flags);
Kévin Redon8fa6ff52018-06-25 16:00:33 +0200379}
380
381/*! Process ATR byte
382 * @param[in] byte ATR byte to process
383 */
384static void process_byte_atr(uint8_t byte)
385{
386 static uint8_t atr_hist_len = 0; /* store the number of expected historical bytes */
387 static uint8_t y = 0; /* last mask of the upcoming TA, TB, TC, TD interface bytes */
King Kévin1200a522018-07-04 10:18:03 +0200388 static uint8_t i = 0; /* interface byte subgroup number */
Kévin Redon8fa6ff52018-06-25 16:00:33 +0200389
390 /* sanity check */
Kévin Redon55f06122018-07-08 14:13:29 +0200391 if (ISO7816_S_IN_ATR != iso_state) {
Kévin Redon8fa6ff52018-06-25 16:00:33 +0200392 TRACE_ERROR("Processing ATR data in wrong ISO 7816-3 state %u\n\r", iso_state);
393 return;
394 }
Kévin Redon55f06122018-07-08 14:13:29 +0200395 if (atr_i >= ARRAY_SIZE(atr)) {
Kévin Redoncf599192018-06-27 16:38:31 +0200396 TRACE_ERROR("ATR data overflow\n\r");
Kévin Redon8fa6ff52018-06-25 16:00:33 +0200397 return;
398 }
399
400 /* save data for use by other functions */
401 atr[atr_i++] = byte;
402
403 /* handle ATR byte depending on current state */
404 switch (atr_state) {
405 case ATR_S_WAIT_TS: /* see ISO/IEC 7816-3:2006 section 8.1 */
406 switch (byte) {
407 case 0x23: /* direct convention used, but decoded using inverse convention (a parity error should also have occurred) */
408 case 0x30: /* inverse convention used, but decoded using direct convention (a parity error should also have occurred) */
409 convention_convert = !convention_convert;
410 case 0x3b: /* direct convention used and correctly decoded */
411 case 0x3f: /* inverse convention used and correctly decoded */
412 atr_state = ATR_S_WAIT_T0; /* wait for format byte */
413 break;
414 default:
Kévin Redon8fa6ff52018-06-25 16:00:33 +0200415 TRACE_WARNING("Invalid TS received\n\r");
Kévin Redonc6b96802018-07-08 17:18:49 +0200416 led_blink(LED_RED, BLINK_2F_O); /* indicate error to user */
417 usb_send_atr(SNIFF_DATA_FLAG_ERROR_MALFORMED); /* send ATR to host software using USB */
418 change_state(ISO7816_S_WAIT_ATR); /* reset state */
419 break;
Kévin Redon8fa6ff52018-06-25 16:00:33 +0200420 }
King Kévin1200a522018-07-04 10:18:03 +0200421 i = 0; /* first interface byte sub-group is coming (T0 is kind of TD0) */
Kévin Redon8fa6ff52018-06-25 16:00:33 +0200422 break;
423 case ATR_S_WAIT_T0: /* see ISO/IEC 7816-3:2006 section 8.2.2 */
424 case ATR_S_WAIT_TD: /* see ISO/IEC 7816-3:2006 section 8.2.3 */
Kévin Redon55f06122018-07-08 14:13:29 +0200425 if (ATR_S_WAIT_T0 == atr_state) {
426 atr_hist_len = (byte & 0x0f); /* save the number of historical bytes */
427 } else if (ATR_S_WAIT_TD == atr_state) {
428 t_protocol_support |= (1<<(byte & 0x0f)); /* remember supported protocol to know if TCK will be present */
Kévin Redon8fa6ff52018-06-25 16:00:33 +0200429 }
Kévin Redon55f06122018-07-08 14:13:29 +0200430 y = (byte & 0xf0); /* remember upcoming interface bytes */
King Kévin1200a522018-07-04 10:18:03 +0200431 i++; /* next interface byte sub-group is coming */
Kévin Redon55f06122018-07-08 14:13:29 +0200432 if (y & 0x10) {
Kévin Redon8fa6ff52018-06-25 16:00:33 +0200433 atr_state = ATR_S_WAIT_TA; /* wait for interface byte TA */
434 break;
435 }
436 case ATR_S_WAIT_TA: /* see ISO/IEC 7816-3:2006 section 8.2.3 */
Kévin Redon55f06122018-07-08 14:13:29 +0200437 if (y & 0x20) {
Kévin Redon8fa6ff52018-06-25 16:00:33 +0200438 atr_state = ATR_S_WAIT_TB; /* wait for interface byte TB */
439 break;
440 }
441 case ATR_S_WAIT_TB: /* see ISO/IEC 7816-3:2006 section 8.2.3 */
Kévin Redon55f06122018-07-08 14:13:29 +0200442 if (y & 0x40) {
Kévin Redon8fa6ff52018-06-25 16:00:33 +0200443 atr_state = ATR_S_WAIT_TC; /* wait for interface byte TC */
444 break;
445 }
446 case ATR_S_WAIT_TC: /* see ISO/IEC 7816-3:2006 section 8.2.3 */
King Kévin1200a522018-07-04 10:18:03 +0200447 /* retrieve WI encoded in TC2*/
448 if (ATR_S_WAIT_TC==atr_state && 2==i) {
Kévin Redon55f06122018-07-08 14:13:29 +0200449 if (0 == byte) {
King Kévin1200a522018-07-04 10:18:03 +0200450 update_wt(10, 0);
451 } else {
452 update_wt(byte, 0);
453 }
454 }
Kévin Redon55f06122018-07-08 14:13:29 +0200455 if (y & 0x80) {
Kévin Redon8fa6ff52018-06-25 16:00:33 +0200456 atr_state = ATR_S_WAIT_TD; /* wait for interface byte TD */
457 break;
458 } else if (atr_hist_len) {
459 atr_state = ATR_S_WAIT_HIST; /* wait for historical bytes */
460 break;
461 }
462 case ATR_S_WAIT_HIST: /* see ISO/IEC 7816-3:2006 section 8.2.4 */
463 if (atr_hist_len) {
464 atr_hist_len--;
465 }
Kévin Redon55f06122018-07-08 14:13:29 +0200466 if (0 == atr_hist_len) {
467 if (t_protocol_support > 1) {
Kévin Redon8fa6ff52018-06-25 16:00:33 +0200468 atr_state = ATR_S_WAIT_TCK; /* wait for check bytes */
469 break;
470 }
471 } else {
472 break;
473 }
474 case ATR_S_WAIT_TCK: /* see ISO/IEC 7816-3:2006 section 8.2.5 */
475 /* we could verify the checksum, but we are just here to sniff */
Kévin Redonf82f0f62018-07-08 15:10:23 +0200476 usb_send_atr(0); /* send ATR to host software using USB */
Kévin Redon00ec89d2018-06-27 16:41:52 +0200477 change_state(ISO7816_S_WAIT_TPDU); /* go to next state */
Kévin Redon8fa6ff52018-06-25 16:00:33 +0200478 break;
479 default:
480 TRACE_INFO("Unknown ATR state %u\n\r", atr_state);
481 }
482}
483
Kévin Redonde97fd22018-07-01 18:23:23 +0200484/*! Send current PPS over USB
Kévin Redonf82f0f62018-07-08 15:10:23 +0200485 * @param[in] flags SNIFF_DATA_FLAG_ data flags
Kévin Redon35e8bdf2018-07-03 16:15:58 +0200486 * @note Also print the PPS over the debug console
Kévin Redonde97fd22018-07-01 18:23:23 +0200487 */
Kévin Redonf82f0f62018-07-08 15:10:23 +0200488static void usb_send_pps(uint32_t flags)
Kévin Redoncf599192018-06-27 16:38:31 +0200489{
490 uint8_t *pps_cur; /* current PPS (request or response) */
491
Kévin Redonde97fd22018-07-01 18:23:23 +0200492 /* Sanity check */
Kévin Redon55f06122018-07-08 14:13:29 +0200493 if (ISO7816_S_IN_PPS_REQ == iso_state) {
Kévin Redoncf599192018-06-27 16:38:31 +0200494 pps_cur = pps_req;
Kévin Redon55f06122018-07-08 14:13:29 +0200495 } else if (ISO7816_S_IN_PPS_RSP == iso_state) {
Kévin Redoncf599192018-06-27 16:38:31 +0200496 pps_cur = pps_rsp;
497 } else {
498 TRACE_ERROR("Can't print PPS in ISO 7816-3 state %u\n\r", iso_state);
499 return;
500 }
501
Kévin Redonde97fd22018-07-01 18:23:23 +0200502 /* Get only relevant data */
503 uint8_t pps[6];
504 uint8_t pps_i = 0;
Kévin Redon55f06122018-07-08 14:13:29 +0200505 if (pps_state > PPS_S_WAIT_PPSS) {
Kévin Redon35e8bdf2018-07-03 16:15:58 +0200506 pps[pps_i++] = pps_cur[0];
507 }
Kévin Redon55f06122018-07-08 14:13:29 +0200508 if (pps_state > PPS_S_WAIT_PPS0) {
Kévin Redon35e8bdf2018-07-03 16:15:58 +0200509 pps[pps_i++] = pps_cur[1];
510 }
Kévin Redon55f06122018-07-08 14:13:29 +0200511 if (pps_state > PPS_S_WAIT_PPS1 && pps_cur[1] & 0x10) {
Kévin Redonde97fd22018-07-01 18:23:23 +0200512 pps[pps_i++] = pps_cur[2];
Kévin Redoncf599192018-06-27 16:38:31 +0200513 }
Kévin Redon55f06122018-07-08 14:13:29 +0200514 if (pps_state > PPS_S_WAIT_PPS2 && pps_cur[1] & 0x20) {
Kévin Redonde97fd22018-07-01 18:23:23 +0200515 pps[pps_i++] = pps_cur[3];
Kévin Redoncf599192018-06-27 16:38:31 +0200516 }
Kévin Redon55f06122018-07-08 14:13:29 +0200517 if (pps_state > PPS_S_WAIT_PPS3 && pps_cur[1] & 0x40) {
Kévin Redonde97fd22018-07-01 18:23:23 +0200518 pps[pps_i++] = pps_cur[4];
Kévin Redoncf599192018-06-27 16:38:31 +0200519 }
Kévin Redon55f06122018-07-08 14:13:29 +0200520 if (pps_state > PPS_S_WAIT_PCK) {
Kévin Redon35e8bdf2018-07-03 16:15:58 +0200521 pps[pps_i++] = pps_cur[5];
522 }
Kévin Redonde97fd22018-07-01 18:23:23 +0200523
524 /* Send message over USB */
Kévin Redonf82f0f62018-07-08 15:10:23 +0200525 usb_send_data(SIMTRACE_MSGT_SNIFF_PPS, pps, pps_i, flags);
Kévin Redonde97fd22018-07-01 18:23:23 +0200526}
527
528/*! Send Fi/Di change over USB
529 * @param[in] fidi Fi/Di factor as encoded in TA1
530 */
531static void usb_send_fidi(uint8_t fidi)
532{
533 /* Send message over USB */
534 struct msgb *usb_msg = usb_msg_alloc_hdr(SIMTRACE_USB_EP_CARD_DATAIN, SIMTRACE_MSGC_SNIFF, SIMTRACE_MSGT_SNIFF_FIDI);
535 if (!usb_msg) {
536 return;
537 }
538 struct sniff_fidi *usb_sniff_fidi = (struct sniff_fidi *) msgb_put(usb_msg, sizeof(*usb_sniff_fidi));
539 usb_sniff_fidi->fidi = fidi;
540 usb_msg_upd_len_and_submit(usb_msg);
Kévin Redoncf599192018-06-27 16:38:31 +0200541}
542
Kévin Redon8fa6ff52018-06-25 16:00:33 +0200543static void process_byte_pps(uint8_t byte)
544{
545 uint8_t *pps_cur; /* current PPS (request or response) */
546
547 /* sanity check */
Kévin Redon55f06122018-07-08 14:13:29 +0200548 if (ISO7816_S_IN_PPS_REQ == iso_state) {
Kévin Redon8fa6ff52018-06-25 16:00:33 +0200549 pps_cur = pps_req;
Kévin Redon55f06122018-07-08 14:13:29 +0200550 } else if (ISO7816_S_IN_PPS_RSP == iso_state) {
Kévin Redon8fa6ff52018-06-25 16:00:33 +0200551 pps_cur = pps_rsp;
552 } else {
553 TRACE_ERROR("Processing PPS data in wrong ISO 7816-3 state %u\n\r", iso_state);
554 return;
555 }
556
557 /* handle PPS byte depending on current state */
558 switch (pps_state) { /* see ISO/IEC 7816-3:2006 section 9.2 */
559 case PPS_S_WAIT_PPSS: /*!< initial byte */
560 if (0xff) {
561 pps_cur[0] = byte;
562 pps_state = PPS_S_WAIT_PPS0; /* go to next state */
563 } else {
564 TRACE_INFO("Invalid PPSS received\n\r");
Kévin Redonc6b96802018-07-08 17:18:49 +0200565 led_blink(LED_RED, BLINK_2F_O); /* indicate error to user */
566 usb_send_pps(SNIFF_DATA_FLAG_ERROR_MALFORMED); /* send ATR to host software using USB */
Kévin Redon00ec89d2018-06-27 16:41:52 +0200567 change_state(ISO7816_S_WAIT_TPDU); /* go back to TPDU state */
Kévin Redon8fa6ff52018-06-25 16:00:33 +0200568 }
569 break;
570 case PPS_S_WAIT_PPS0: /*!< format byte */
571 pps_cur[1] = byte;
Kévin Redon55f06122018-07-08 14:13:29 +0200572 if (pps_cur[1] & 0x10) {
Kévin Redon8fa6ff52018-06-25 16:00:33 +0200573 pps_state = PPS_S_WAIT_PPS1; /* go to next state */
574 break;
575 }
576 case PPS_S_WAIT_PPS1: /*!< first parameter byte */
577 pps_cur[2] = byte; /* not always right but doesn't affect the process */
Kévin Redon55f06122018-07-08 14:13:29 +0200578 if (pps_cur[1] & 0x20) {
Kévin Redon8fa6ff52018-06-25 16:00:33 +0200579 pps_state = PPS_S_WAIT_PPS2; /* go to next state */
580 break;
581 }
582 case PPS_S_WAIT_PPS2: /*!< second parameter byte */
583 pps_cur[3] = byte; /* not always right but doesn't affect the process */
Kévin Redon55f06122018-07-08 14:13:29 +0200584 if (pps_cur[1] & 0x40) {
Kévin Redon8fa6ff52018-06-25 16:00:33 +0200585 pps_state = PPS_S_WAIT_PPS3; /* go to next state */
586 break;
587 }
588 case PPS_S_WAIT_PPS3: /*!< third parameter byte */
589 pps_cur[4] = byte; /* not always right but doesn't affect the process */
590 pps_state = PPS_S_WAIT_PCK; /* go to next state */
591 break;
592 case PPS_S_WAIT_PCK: /*!< check byte */
593 pps_cur[5] = byte; /* not always right but doesn't affect the process */
594 /* verify the checksum */
595 uint8_t check = 0;
596 check ^= pps_cur[0];
597 check ^= pps_cur[1];
Kévin Redon55f06122018-07-08 14:13:29 +0200598 if (pps_cur[1] & 0x10) {
Kévin Redon8fa6ff52018-06-25 16:00:33 +0200599 check ^= pps_cur[2];
600 }
Kévin Redon55f06122018-07-08 14:13:29 +0200601 if (pps_cur[1] & 0x20) {
Kévin Redon8fa6ff52018-06-25 16:00:33 +0200602 check ^= pps_cur[3];
603 }
Kévin Redon55f06122018-07-08 14:13:29 +0200604 if (pps_cur[1] & 0x40) {
Kévin Redon8fa6ff52018-06-25 16:00:33 +0200605 check ^= pps_cur[4];
606 }
607 check ^= pps_cur[5];
Kévin Redon35e8bdf2018-07-03 16:15:58 +0200608 pps_state = PPS_S_WAIT_END;
Kévin Redonf82f0f62018-07-08 15:10:23 +0200609 usb_send_pps(0); /* send PPS to host software using USB */
Kévin Redon55f06122018-07-08 14:13:29 +0200610 if (ISO7816_S_IN_PPS_REQ == iso_state) {
611 if (0 == check) { /* checksum is valid */
Kévin Redon8fa6ff52018-06-25 16:00:33 +0200612 change_state(ISO7816_S_WAIT_PPS_RSP); /* go to next state */
613 } else { /* checksum is invalid */
Kévin Redon00ec89d2018-06-27 16:41:52 +0200614 change_state(ISO7816_S_WAIT_TPDU); /* go to next state */
Kévin Redon8fa6ff52018-06-25 16:00:33 +0200615 }
Kévin Redon55f06122018-07-08 14:13:29 +0200616 } else if (ISO7816_S_IN_PPS_RSP == iso_state) {
617 if (0 == check) { /* checksum is valid */
Kévin Redon8fa6ff52018-06-25 16:00:33 +0200618 uint8_t fn, dn;
Kévin Redon55f06122018-07-08 14:13:29 +0200619 if (pps_cur[1] & 0x10) {
620 fn = (pps_cur[2] >> 4);
621 dn = (pps_cur[2] & 0x0f);
Kévin Redon8fa6ff52018-06-25 16:00:33 +0200622 } else {
623 fn = 1;
624 dn = 1;
625 }
Kévin Redon74063372018-07-03 15:57:03 +0200626 TRACE_INFO("PPS negotiation successful: Fn=%u Dn=%u\n\r", fi_table[fn], di_table[dn]);
Kévin Redon8fa6ff52018-06-25 16:00:33 +0200627 update_fidi(&sniff_usart, pps_cur[2]);
King Kévin1200a522018-07-04 10:18:03 +0200628 update_wt(0, di_table[dn]);
Kévin Redonde97fd22018-07-01 18:23:23 +0200629 usb_send_fidi(pps_cur[2]); /* send Fi/Di change notification to host software over USB */
Kévin Redon8fa6ff52018-06-25 16:00:33 +0200630 } else { /* checksum is invalid */
631 TRACE_INFO("PPS negotiation failed\n\r");
632 }
Kévin Redon00ec89d2018-06-27 16:41:52 +0200633 change_state(ISO7816_S_WAIT_TPDU); /* go to next state */
Kévin Redon8fa6ff52018-06-25 16:00:33 +0200634 }
635 break;
Kévin Redon35e8bdf2018-07-03 16:15:58 +0200636 case PPS_S_WAIT_END:
637 TRACE_WARNING("Unexpected PPS received %u\n\r", pps_state);
638 break;
Kévin Redon8fa6ff52018-06-25 16:00:33 +0200639 default:
Kévin Redon35e8bdf2018-07-03 16:15:58 +0200640 TRACE_WARNING("Unknown PPS state %u\n\r", pps_state);
641 break;
Kévin Redon8fa6ff52018-06-25 16:00:33 +0200642 }
643}
644
Kévin Redonde97fd22018-07-01 18:23:23 +0200645/*! Send current TPDU over USB
Kévin Redonf82f0f62018-07-08 15:10:23 +0200646 * @param[in] flags SNIFF_DATA_FLAG_ data flags
Kévin Redon35e8bdf2018-07-03 16:15:58 +0200647 * @note Also print the TPDU over the debug console
Kévin Redonde97fd22018-07-01 18:23:23 +0200648 */
Kévin Redonf82f0f62018-07-08 15:10:23 +0200649static void usb_send_tpdu(uint32_t flags)
Kévin Redon8fa6ff52018-06-25 16:00:33 +0200650{
Kévin Redonde97fd22018-07-01 18:23:23 +0200651 /* Check state */
Kévin Redon55f06122018-07-08 14:13:29 +0200652 if (ISO7816_S_IN_TPDU != iso_state) {
Kévin Redon00ec89d2018-06-27 16:41:52 +0200653 TRACE_WARNING("Can't print TPDU in ISO 7816-3 state %u\n\r", iso_state);
654 return;
655 }
656
Kévin Redonde97fd22018-07-01 18:23:23 +0200657 /* Send ATR over USB */
Kévin Redonf82f0f62018-07-08 15:10:23 +0200658 usb_send_data(SIMTRACE_MSGT_SNIFF_TPDU, tpdu_packet, tpdu_packet_i, flags);
Kévin Redon00ec89d2018-06-27 16:41:52 +0200659}
660
661static void process_byte_tpdu(uint8_t byte)
662{
663 /* sanity check */
Kévin Redon55f06122018-07-08 14:13:29 +0200664 if (ISO7816_S_IN_TPDU != iso_state) {
Kévin Redon00ec89d2018-06-27 16:41:52 +0200665 TRACE_ERROR("Processing TPDU data in wrong ISO 7816-3 state %u\n\r", iso_state);
666 return;
667 }
Kévin Redon55f06122018-07-08 14:13:29 +0200668 if (tpdu_packet_i >= ARRAY_SIZE(tpdu_packet)) {
Kévin Redon00ec89d2018-06-27 16:41:52 +0200669 TRACE_ERROR("TPDU data overflow\n\r");
670 return;
671 }
672
673 /* handle TPDU byte depending on current state */
674 switch (tpdu_state) {
675 case TPDU_S_CLA:
Kévin Redon55f06122018-07-08 14:13:29 +0200676 if (0xff == byte) {
Kévin Redon00ec89d2018-06-27 16:41:52 +0200677 TRACE_WARNING("0xff is not a valid class byte\n\r");
Kévin Redonc6b96802018-07-08 17:18:49 +0200678 led_blink(LED_RED, BLINK_2F_O); /* indicate error to user */
679 usb_send_tpdu(SNIFF_DATA_FLAG_ERROR_MALFORMED); /* send ATR to host software using USB */
Kévin Redonc9bd7152018-07-03 16:17:00 +0200680 change_state(ISO7816_S_WAIT_TPDU); /* go back to TPDU state */
681 return;
Kévin Redon00ec89d2018-06-27 16:41:52 +0200682 }
683 tpdu_packet_i = 0;
684 tpdu_packet[tpdu_packet_i++] = byte;
685 tpdu_state = TPDU_S_INS;
686 break;
687 case TPDU_S_INS:
Kévin Redon55f06122018-07-08 14:13:29 +0200688 if ((0x60 == (byte & 0xf0)) || (0x90 == (byte & 0xf0))) {
Kévin Redonc9bd7152018-07-03 16:17:00 +0200689 TRACE_WARNING("invalid CLA 0x%02x\n\r", byte);
Kévin Redonc6b96802018-07-08 17:18:49 +0200690 led_blink(LED_RED, BLINK_2F_O); /* indicate error to user */
691 usb_send_tpdu(SNIFF_DATA_FLAG_ERROR_MALFORMED); /* send ATR to host software using USB */
Kévin Redonc9bd7152018-07-03 16:17:00 +0200692 change_state(ISO7816_S_WAIT_TPDU); /* go back to TPDU state */
693 return;
694 }
Kévin Redon00ec89d2018-06-27 16:41:52 +0200695 tpdu_packet_i = 1;
696 tpdu_packet[tpdu_packet_i++] = byte;
697 tpdu_state = TPDU_S_P1;
698 break;
699 case TPDU_S_P1:
700 tpdu_packet_i = 2;
701 tpdu_packet[tpdu_packet_i++] = byte;
702 tpdu_state = TPDU_S_P2;
703 break;
704 case TPDU_S_P2:
705 tpdu_packet_i = 3;
706 tpdu_packet[tpdu_packet_i++] = byte;
707 tpdu_state = TPDU_S_P3;
708 break;
709 case TPDU_S_P3:
710 tpdu_packet_i = 4;
711 tpdu_packet[tpdu_packet_i++] = byte;
712 tpdu_state = TPDU_S_PROCEDURE;
713 break;
714 case TPDU_S_PROCEDURE:
Kévin Redon55f06122018-07-08 14:13:29 +0200715 if (0x60 == byte) { /* wait for next procedure byte */
Kévin Redon00ec89d2018-06-27 16:41:52 +0200716 break;
Kévin Redon55f06122018-07-08 14:13:29 +0200717 } else if (tpdu_packet[1] == byte) { /* get all remaining data bytes */
Kévin Redon00ec89d2018-06-27 16:41:52 +0200718 tpdu_state = TPDU_S_DATA_REMAINING;
719 break;
Kévin Redon55f06122018-07-08 14:13:29 +0200720 } else if ((~tpdu_packet[1]) == byte) { /* get single data byte */
Kévin Redon00ec89d2018-06-27 16:41:52 +0200721 tpdu_state = TPDU_S_DATA_SINGLE;
722 break;
723 }
724 case TPDU_S_SW1:
Kévin Redon55f06122018-07-08 14:13:29 +0200725 if ((0x60 == (byte & 0xf0)) || (0x90 == (byte & 0xf0))) { /* this procedure byte is SW1 */
Kévin Redon00ec89d2018-06-27 16:41:52 +0200726 tpdu_packet[tpdu_packet_i++] = byte;
727 tpdu_state = TPDU_S_SW2;
728 } else {
729 TRACE_WARNING("invalid SW1 0x%02x\n\r", byte);
Kévin Redonc6b96802018-07-08 17:18:49 +0200730 led_blink(LED_RED, BLINK_2F_O); /* indicate error to user */
731 usb_send_tpdu(SNIFF_DATA_FLAG_ERROR_MALFORMED); /* send ATR to host software using USB */
Kévin Redonc9bd7152018-07-03 16:17:00 +0200732 change_state(ISO7816_S_WAIT_TPDU); /* go back to TPDU state */
733 return;
Kévin Redon00ec89d2018-06-27 16:41:52 +0200734 }
735 break;
736 case TPDU_S_SW2:
737 tpdu_packet[tpdu_packet_i++] = byte;
Kévin Redonf82f0f62018-07-08 15:10:23 +0200738 usb_send_tpdu(0); /* send TPDU to host software using USB */
Kévin Redon00ec89d2018-06-27 16:41:52 +0200739 change_state(ISO7816_S_WAIT_TPDU); /* this is the end of the TPDU */
740 break;
741 case TPDU_S_DATA_SINGLE:
742 case TPDU_S_DATA_REMAINING:
743 tpdu_packet[tpdu_packet_i++] = byte;
Kévin Redon55f06122018-07-08 14:13:29 +0200744 if (0 == tpdu_packet[4]) {
745 if (5+256 <= tpdu_packet_i) {
Kévin Redonc9bd7152018-07-03 16:17:00 +0200746 tpdu_state = TPDU_S_PROCEDURE;
Kévin Redon00ec89d2018-06-27 16:41:52 +0200747 }
748 } else {
Kévin Redon55f06122018-07-08 14:13:29 +0200749 if (5+tpdu_packet[4] <= tpdu_packet_i) {
Kévin Redonc9bd7152018-07-03 16:17:00 +0200750 tpdu_state = TPDU_S_PROCEDURE;
Kévin Redon00ec89d2018-06-27 16:41:52 +0200751 }
752 }
Kévin Redon55f06122018-07-08 14:13:29 +0200753 if (TPDU_S_DATA_SINGLE == tpdu_state) {
Kévin Redon00ec89d2018-06-27 16:41:52 +0200754 tpdu_state = TPDU_S_PROCEDURE;
755 }
756 break;
757 default:
758 TRACE_ERROR("unhandled TPDU state %u\n\r", tpdu_state);
759 }
Kévin Redon8fa6ff52018-06-25 16:00:33 +0200760}
761
Kévin Redond7a6de52018-06-11 13:46:35 +0200762/*! Interrupt Service Routine called on USART activity */
Kévin Redon8fa6ff52018-06-25 16:00:33 +0200763void Sniffer_usart_isr(void)
Kévin Redond7a6de52018-06-11 13:46:35 +0200764{
King Kévin1200a522018-07-04 10:18:03 +0200765 /* Remaining Waiting Time (WI) counter (>16 bits) */
766 static volatile uint32_t wt_remaining = 9600;
767
Kévin Redond7a6de52018-06-11 13:46:35 +0200768 /* Read channel status register */
Kévin Redon709a4312018-07-03 16:10:04 +0200769 uint32_t csr = sniff_usart.base->US_CSR;
770 /* Verify if there was an error */
771 if (csr & US_CSR_OVRE) {
772 TRACE_WARNING("USART overrun error\n\r");
773 sniff_usart.base->US_CR |= US_CR_RSTSTA;
774 }
775 if (csr & US_CSR_FRAME) {
776 TRACE_WARNING("USART framing error\n\r");
777 sniff_usart.base->US_CR |= US_CR_RSTSTA;
778 }
King Kévin1200a522018-07-04 10:18:03 +0200779
Kévin Redond7a6de52018-06-11 13:46:35 +0200780 /* Verify if character has been received */
781 if (csr & US_CSR_RXRDY) {
782 /* Read communication data byte between phone and SIM */
783 uint8_t byte = sniff_usart.base->US_RHR;
King Kévin1200a522018-07-04 10:18:03 +0200784 /* Reset WT timer */
785 wt_remaining = wt;
Kévin Redond7a6de52018-06-11 13:46:35 +0200786 /* Store sniffed data into buffer (also clear interrupt */
Kévin Redon709a4312018-07-03 16:10:04 +0200787 if (rbuf_is_full(&sniff_buffer)) {
788 TRACE_ERROR("USART buffer full\n\r");
789 } else {
790 rbuf_write(&sniff_buffer, byte);
791 }
Kévin Redond7a6de52018-06-11 13:46:35 +0200792 }
King Kévin1200a522018-07-04 10:18:03 +0200793
Kévin Redon638cec82018-06-27 16:42:56 +0200794 /* Verify it WT timeout occurred, to detect unresponsive card */
795 if (csr & US_CSR_TIMEOUT) {
Kévin Redon55f06122018-07-08 14:13:29 +0200796 if (wt_remaining <= (sniff_usart.base->US_RTOR & 0xffff)) {
King Kévin1200a522018-07-04 10:18:03 +0200797 /* Just set the flag and let the main loop handle it */
798 change_flags |= SNIFF_CHANGE_FLAG_TIMEOUT_WT;
799 /* Reset timeout value */
800 wt_remaining = wt;
801 } else {
Kévin Redon55f06122018-07-08 14:13:29 +0200802 wt_remaining -= (sniff_usart.base->US_RTOR & 0xffff); /* be sure to subtract the actual timeout since the new might not have been set and reloaded yet */
King Kévin1200a522018-07-04 10:18:03 +0200803 }
Kévin Redon55f06122018-07-08 14:13:29 +0200804 if (wt_remaining > 0xffff) {
King Kévin1200a522018-07-04 10:18:03 +0200805 sniff_usart.base->US_RTOR = 0xffff;
806 } else {
807 sniff_usart.base->US_RTOR = wt_remaining;
808 }
809 /* Stop timeout until next character is received (and clears the timeout flag) */
Kévin Redon638cec82018-06-27 16:42:56 +0200810 sniff_usart.base->US_CR |= US_CR_STTTO;
King Kévin1200a522018-07-04 10:18:03 +0200811 if (!(change_flags & SNIFF_CHANGE_FLAG_TIMEOUT_WT)) {
812 /* Immediately restart the counter it the WT timeout did not occur (needs the timeout flag to be cleared) */
813 sniff_usart.base->US_CR |= US_CR_RETTO;
814 }
Kévin Redon638cec82018-06-27 16:42:56 +0200815 }
Kévin Redond7a6de52018-06-11 13:46:35 +0200816}
817
Kévin Redon8fa6ff52018-06-25 16:00:33 +0200818/** PIO interrupt service routine to checks if the card reset line has changed
819 */
820static void Sniffer_reset_isr(const Pin* pPin)
821{
822 /* Ensure an edge on the reset pin cause the interrupt */
Kévin Redon55f06122018-07-08 14:13:29 +0200823 if (pPin->id != pin_rst.id || 0 == (pPin->mask & pin_rst.mask)) {
Kévin Redon8fa6ff52018-06-25 16:00:33 +0200824 TRACE_ERROR("Pin other than reset caused a interrupt\n\r");
825 return;
826 }
827 /* Update the ISO state according to the reset change */
828 if (PIO_Get(&pin_rst)) {
Kévin Redona2fccba2018-07-08 15:49:46 +0200829 change_flags |= SNIFF_CHANGE_FLAG_RESET_HOLD; /* set flag and let main loop send it */
Kévin Redon8fa6ff52018-06-25 16:00:33 +0200830 } else {
Kévin Redona2fccba2018-07-08 15:49:46 +0200831 change_flags |= SNIFF_CHANGE_FLAG_RESET_RELEASE; /* set flag and let main loop send it */
Kévin Redon8fa6ff52018-06-25 16:00:33 +0200832 }
833}
834
Kévin Redond7a6de52018-06-11 13:46:35 +0200835/*------------------------------------------------------------------------------
836 * Global functions
837 *------------------------------------------------------------------------------*/
838
839void Sniffer_usart1_irq(void)
840{
Kévin Redon55f06122018-07-08 14:13:29 +0200841 if (ID_USART1 == sniff_usart.id) {
Kévin Redon8fa6ff52018-06-25 16:00:33 +0200842 Sniffer_usart_isr();
Kévin Redond7a6de52018-06-11 13:46:35 +0200843 }
844}
845
846void Sniffer_usart0_irq(void)
847{
Kévin Redon55f06122018-07-08 14:13:29 +0200848 if (ID_USART0 == sniff_usart.id) {
Kévin Redon8fa6ff52018-06-25 16:00:33 +0200849 Sniffer_usart_isr();
Kévin Redond7a6de52018-06-11 13:46:35 +0200850 }
851}
852
Christina Quasta90eefa2015-02-24 17:52:29 +0100853/*-----------------------------------------------------------------------------
Christina Quastd2b05f02015-02-25 18:44:24 +0100854 * Initialization routine
Christina Quasta90eefa2015-02-24 17:52:29 +0100855 *-----------------------------------------------------------------------------*/
Christina Quasta90eefa2015-02-24 17:52:29 +0100856
Harald Welteed75c622017-11-28 21:23:12 +0100857/* Called during USB enumeration after device is enumerated by host */
Harald Welte7dd3dfd2016-03-03 12:32:04 +0100858void Sniffer_configure(void)
Christina Quasta90eefa2015-02-24 17:52:29 +0100859{
Kévin Redon36abece2018-06-04 16:30:01 +0200860 TRACE_INFO("Sniffer config\n\r");
Christina Quasta90eefa2015-02-24 17:52:29 +0100861}
Christina Quastfb524b92015-02-27 13:39:45 +0100862
Harald Welteed75c622017-11-28 21:23:12 +0100863/* called when *different* configuration is set by host */
Harald Welte7dd3dfd2016-03-03 12:32:04 +0100864void Sniffer_exit(void)
Christina Quastfb524b92015-02-27 13:39:45 +0100865{
Kévin Redon36abece2018-06-04 16:30:01 +0200866 TRACE_INFO("Sniffer exit\n\r");
Kévin Redonde97fd22018-07-01 18:23:23 +0200867 /* Disable USART */
Kévin Redon45ad62d2018-06-07 18:56:41 +0200868 USART_DisableIt(sniff_usart.base, US_IER_RXRDY);
869 /* NOTE: don't forget to set the IRQ according to the USART peripheral used */
Kévin Redon8fa6ff52018-06-25 16:00:33 +0200870 NVIC_DisableIRQ(IRQ_USART_SIM);
Kévin Redon45ad62d2018-06-07 18:56:41 +0200871 USART_SetReceiverEnabled(sniff_usart.base, 0);
Kévin Redonde97fd22018-07-01 18:23:23 +0200872 /* Disable RST IRQ */
873 PIO_DisableIt(&pin_rst);
874 NVIC_DisableIRQ(PIOA_IRQn); /* CAUTION this needs to match to the correct port */
Harald Welte7dd3dfd2016-03-03 12:32:04 +0100875}
876
Kévin Redon36abece2018-06-04 16:30:01 +0200877/* called when *Sniffer* configuration is set by host */
Harald Welte7dd3dfd2016-03-03 12:32:04 +0100878void Sniffer_init(void)
879{
Kévin Redon36abece2018-06-04 16:30:01 +0200880 TRACE_INFO("Sniffer Init\n\r");
Kévin Redon7b734622018-06-06 16:13:48 +0200881
Kévin Redon45ad62d2018-06-07 18:56:41 +0200882 /* Configure pins to sniff communication between phone and card */
883 PIO_Configure(pins_sniff, PIO_LISTSIZE(pins_sniff));
884 /* Configure pins to connect phone to card */
885 PIO_Configure(pins_bus, PIO_LISTSIZE(pins_bus));
886 /* Configure pins to forward phone power to card */
887 PIO_Configure(pins_power, PIO_LISTSIZE(pins_power));
Kévin Redon8fa6ff52018-06-25 16:00:33 +0200888 /* Enable interrupts on port with reset line */
889 NVIC_EnableIRQ(PIOA_IRQn); /* CAUTION this needs to match to the correct port */
890 /* Register ISR to handle card reset change */
891 PIO_ConfigureIt(&pin_rst, &Sniffer_reset_isr);
892 /* Enable interrupt on card reset pin */
893 PIO_EnableIt(&pin_rst);
Kévin Redon45ad62d2018-06-07 18:56:41 +0200894
Kévin Redon7b734622018-06-06 16:13:48 +0200895 /* Clear ring buffer containing the sniffed data */
896 rbuf_reset(&sniff_buffer);
Kévin Redon45ad62d2018-06-07 18:56:41 +0200897 /* Configure USART to as ISO-7816 slave communication to sniff communication */
898 ISO7816_Init(&sniff_usart, CLK_SLAVE);
899 /* Only receive data when sniffing */
900 USART_SetReceiverEnabled(sniff_usart.base, 1);
King Kévin1200a522018-07-04 10:18:03 +0200901 /* Enable Receiver time-out to detect waiting time (WT) time-out (e.g. unresponsive cards) */
902 sniff_usart.base->US_RTOR = wt;
Kévin Redon638cec82018-06-27 16:42:56 +0200903 /* Enable interrupt to indicate when data has been received or timeout occurred */
904 USART_EnableIt(sniff_usart.base, US_IER_RXRDY | US_IER_TIMEOUT);
Kévin Redonfe763b72018-07-03 16:17:56 +0200905 /* Set USB priority lower than USART to not miss sniffing data (both at 0 per default) */
Kévin Redon55f06122018-07-08 14:13:29 +0200906 if (NVIC_GetPriority(IRQ_USART_SIM) >= NVIC_GetPriority(UDP_IRQn)) {
907 NVIC_SetPriority(UDP_IRQn, NVIC_GetPriority(IRQ_USART_SIM) + 2);
Kévin Redonfe763b72018-07-03 16:17:56 +0200908 }
Kévin Redond7a6de52018-06-11 13:46:35 +0200909 /* Enable interrupt requests for the USART peripheral */
910 NVIC_EnableIRQ(IRQ_USART_SIM);
911
Kévin Redon8fa6ff52018-06-25 16:00:33 +0200912 /* Reset state */
Kévin Redon55f06122018-07-08 14:13:29 +0200913 if (ISO7816_S_RESET != iso_state) {
Kévin Redon8fa6ff52018-06-25 16:00:33 +0200914 change_state(ISO7816_S_RESET);
915 }
Harald Welte7dd3dfd2016-03-03 12:32:04 +0100916}
917
Kévin Redon709a4312018-07-03 16:10:04 +0200918/*! Send card change flags over USB
919 * @param[in] flags change flags corresponding to SIMTRACE_MSGT_SNIFF_CHANGE
920 */
921static void usb_send_change(uint32_t flags)
922{
923 /* Check flags */
Kévin Redon55f06122018-07-08 14:13:29 +0200924 if(0 == flags) { /* no changes */
Kévin Redon709a4312018-07-03 16:10:04 +0200925 return;
926 }
927
Kévin Redon55f06122018-07-08 14:13:29 +0200928 if (flags & SNIFF_CHANGE_FLAG_TIMEOUT_WT) {
Kévin Redon709a4312018-07-03 16:10:04 +0200929 printf("waiting time (WT) timeout\n\r");
930 }
931
932 /* Send message over USB */
933 struct msgb *usb_msg = usb_msg_alloc_hdr(SIMTRACE_USB_EP_CARD_DATAIN, SIMTRACE_MSGC_SNIFF, SIMTRACE_MSGT_SNIFF_CHANGE);
934 if (!usb_msg) {
935 return;
936 }
937 struct sniff_change *usb_sniff_change = (struct sniff_change *) msgb_put(usb_msg, sizeof(*usb_sniff_change));
938 usb_sniff_change->flags = flags;
939 usb_msg_upd_len_and_submit(usb_msg);
940}
941
Kévin Redon8fa6ff52018-06-25 16:00:33 +0200942/* Main (idle/busy) loop of this USB configuration */
Harald Welte7dd3dfd2016-03-03 12:32:04 +0100943void Sniffer_run(void)
944{
Kévin Redonde97fd22018-07-01 18:23:23 +0200945 /* Handle USB queue */
946 /* first try to send any pending messages on INT */
947 usb_refill_to_host(SIMTRACE_USB_EP_CARD_INT);
948 /* then try to send any pending messages on IN */
949 usb_refill_to_host(SIMTRACE_USB_EP_CARD_DATAIN);
950 /* ensure we can handle incoming USB messages from the host */
951 /* currently we don't need any incoming data
952 usb_refill_from_host(SIMTRACE_USB_EP_CARD_DATAOUT);
953 struct llist_head *queue = usb_get_queue(SIMTRACE_USB_EP_CARD_DATAOUT);
954 process_any_usb_commands(queue);
955 */
956
Kévin Redona2fccba2018-07-08 15:49:46 +0200957 /* WARNING: the signal data and flags are not synchronized. We have to hope
958 * the processing is fast enough to not land in the wrong state while data
959 * is remaining
960 */
Kévin Redonde97fd22018-07-01 18:23:23 +0200961 /* Handle sniffed data */
962 if (!rbuf_is_empty(&sniff_buffer)) { /* use if instead of while to let the main loop restart the watchdog */
963 uint8_t byte = rbuf_read(&sniff_buffer);
Kévin Redon709a4312018-07-03 16:10:04 +0200964 /* Convert convention if required */
965 if (convention_convert) {
966 byte = convention_convert_lut[byte];
967 }
968 //TRACE_ERROR_WP(">%02x", byte);
Kévin Redonde97fd22018-07-01 18:23:23 +0200969 switch (iso_state) { /* Handle byte depending on state */
970 case ISO7816_S_RESET: /* During reset we shouldn't receive any data */
971 break;
972 case ISO7816_S_WAIT_ATR: /* After a reset we expect the ATR */
973 change_state(ISO7816_S_IN_ATR); /* go to next state */
974 case ISO7816_S_IN_ATR: /* More ATR data incoming */
975 process_byte_atr(byte);
976 break;
977 case ISO7816_S_WAIT_TPDU: /* After the ATR we expect TPDU or PPS data */
978 case ISO7816_S_WAIT_PPS_RSP:
Kévin Redon55f06122018-07-08 14:13:29 +0200979 if (0xff == byte) {
980 if (ISO7816_S_WAIT_PPS_RSP == iso_state) {
Kévin Redonde97fd22018-07-01 18:23:23 +0200981 change_state(ISO7816_S_IN_PPS_RSP); /* Go to PPS state */
982 } else {
983 change_state(ISO7816_S_IN_PPS_REQ); /* Go to PPS state */
984 }
985 process_byte_pps(byte);
986 break;
987 }
988 case ISO7816_S_IN_TPDU: /* More TPDU data incoming */
Kévin Redon55f06122018-07-08 14:13:29 +0200989 if (ISO7816_S_WAIT_TPDU == iso_state) {
Kévin Redonde97fd22018-07-01 18:23:23 +0200990 change_state(ISO7816_S_IN_TPDU);
991 }
992 process_byte_tpdu(byte);
993 break;
994 case ISO7816_S_IN_PPS_REQ:
995 case ISO7816_S_IN_PPS_RSP:
996 process_byte_pps(byte);
997 break;
998 default:
999 TRACE_ERROR("Data received in unknown state %u\n\r", iso_state);
1000 }
1001 }
1002
1003 /* Handle flags */
1004 if (change_flags) { /* WARNING this is not synced with the data buffer handling */
Kévin Redona2fccba2018-07-08 15:49:46 +02001005 if (change_flags & SNIFF_CHANGE_FLAG_RESET_HOLD) {
1006 if (ISO7816_S_WAIT_ATR != iso_state) {
1007 change_state(ISO7816_S_WAIT_ATR);
1008 }
1009 }
1010 if (change_flags & SNIFF_CHANGE_FLAG_RESET_RELEASE) {
1011 if (ISO7816_S_RESET != iso_state) {
1012 change_state(ISO7816_S_RESET);
1013 }
1014 }
Kévin Redon55f06122018-07-08 14:13:29 +02001015 if (change_flags & SNIFF_CHANGE_FLAG_TIMEOUT_WT) {
Kévin Redon709a4312018-07-03 16:10:04 +02001016 /* Use timeout to detect interrupted data transmission */
1017 switch (iso_state) {
1018 case ISO7816_S_IN_ATR:
Kévin Redonc6b96802018-07-08 17:18:49 +02001019 led_blink(LED_RED, BLINK_2F_O); /* indicate error to user */
Kévin Redonf82f0f62018-07-08 15:10:23 +02001020 usb_send_atr(SNIFF_DATA_FLAG_ERROR_INCOMPLETE); /* send incomplete ATR to host software using USB */
Kévin Redon709a4312018-07-03 16:10:04 +02001021 change_state(ISO7816_S_WAIT_ATR);
1022 break;
1023 case ISO7816_S_IN_TPDU:
Kévin Redonc6b96802018-07-08 17:18:49 +02001024 led_blink(LED_RED, BLINK_2F_O); /* indicate error to user */
Kévin Redonf82f0f62018-07-08 15:10:23 +02001025 usb_send_tpdu(SNIFF_DATA_FLAG_ERROR_INCOMPLETE); /* send incomplete PPS to host software using USB */
Kévin Redon709a4312018-07-03 16:10:04 +02001026 change_state(ISO7816_S_WAIT_TPDU);
1027 break;
1028 case ISO7816_S_IN_PPS_REQ:
1029 case ISO7816_S_IN_PPS_RSP:
Kévin Redonc6b96802018-07-08 17:18:49 +02001030 led_blink(LED_RED, BLINK_2F_O); /* indicate error to user */
Kévin Redonf82f0f62018-07-08 15:10:23 +02001031 usb_send_pps(SNIFF_DATA_FLAG_ERROR_INCOMPLETE); /* send incomplete TPDU to host software using USB */
Kévin Redon709a4312018-07-03 16:10:04 +02001032 change_state(ISO7816_S_WAIT_TPDU);
1033 break;
1034 default:
1035 change_flags &= ~SNIFF_CHANGE_FLAG_TIMEOUT_WT; /* We don't care about the timeout is all other cases */
1036 break;
1037 }
1038 }
1039 if (change_flags) {
1040 usb_send_change(change_flags); /* send timeout to host software over USB */
1041 change_flags = 0; /* Reset flags */
1042 }
Kévin Redonde97fd22018-07-01 18:23:23 +02001043 }
Christina Quastfb524b92015-02-27 13:39:45 +01001044}
Harald Welte2fb59962016-02-28 12:34:26 +01001045#endif /* HAVE_SNIFFER */