Kévin Redon | 45ad62d | 2018-06-07 18:56:41 +0200 | [diff] [blame] | 1 | /* |
| 2 | * (C) 2010-2017 by Harald Welte <hwelte@sysmocom.de> |
| 3 | * (C) 2018 by Kevin Redon <kredon@sysmocom.de> |
| 4 | * All Rights Reserved |
Christina Quast | a90eefa | 2015-02-24 17:52:29 +0100 | [diff] [blame] | 5 | * |
Kévin Redon | 45ad62d | 2018-06-07 18:56:41 +0200 | [diff] [blame] | 6 | * This program is free software; you can redistribute it and/or modify |
| 7 | * it under the terms of the GNU Affero General Public License as published by |
| 8 | * the Free Software Foundation; either version 3 of the License, or |
| 9 | * (at your option) any later version. |
Christina Quast | a90eefa | 2015-02-24 17:52:29 +0100 | [diff] [blame] | 10 | * |
Kévin Redon | 45ad62d | 2018-06-07 18:56:41 +0200 | [diff] [blame] | 11 | * 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 |
| 14 | * GNU Affero General Public License for more details. |
Christina Quast | a90eefa | 2015-02-24 17:52:29 +0100 | [diff] [blame] | 15 | * |
Kévin Redon | 45ad62d | 2018-06-07 18:56:41 +0200 | [diff] [blame] | 16 | * You should have received a copy of the GNU Affero General Public License |
| 17 | * along with this program. If not, see <http://www.gnu.org/licenses/>. |
Christina Quast | a90eefa | 2015-02-24 17:52:29 +0100 | [diff] [blame] | 18 | * |
Christina Quast | a90eefa | 2015-02-24 17:52:29 +0100 | [diff] [blame] | 19 | */ |
Kévin Redon | 8fa6ff5 | 2018-06-25 16:00:33 +0200 | [diff] [blame] | 20 | /* This code implement the Sniffer mode to sniff the communication between a SIM card (or any ISO 7816 smart card) and a phone (or any ISO 7816 card reader). |
Kévin Redon | d7a6de5 | 2018-06-11 13:46:35 +0200 | [diff] [blame] | 21 | * For historical reasons (i.e. SIMtrace hardware) the USART peripheral connected to the SIM card is used. |
Kévin Redon | 709a431 | 2018-07-03 16:10:04 +0200 | [diff] [blame] | 22 | * TODO put common ISO7816-3 code is separate library (and combine clean with iso7816_4) |
Kévin Redon | d7a6de5 | 2018-06-11 13:46:35 +0200 | [diff] [blame] | 23 | */ |
Kévin Redon | 36abece | 2018-06-04 16:30:01 +0200 | [diff] [blame] | 24 | #include "board.h" |
| 25 | #include "simtrace.h" |
| 26 | |
Harald Welte | 2fb5996 | 2016-02-28 12:34:26 +0100 | [diff] [blame] | 27 | #ifdef HAVE_SNIFFER |
| 28 | |
Christina Quast | a90eefa | 2015-02-24 17:52:29 +0100 | [diff] [blame] | 29 | /*------------------------------------------------------------------------------ |
| 30 | * Headers |
| 31 | *------------------------------------------------------------------------------*/ |
| 32 | |
Christina Quast | a90eefa | 2015-02-24 17:52:29 +0100 | [diff] [blame] | 33 | #include <string.h> |
Kévin Redon | 8fa6ff5 | 2018-06-25 16:00:33 +0200 | [diff] [blame] | 34 | #include "utils.h" |
| 35 | #include "iso7816_fidi.h" |
Kévin Redon | de97fd2 | 2018-07-01 18:23:23 +0200 | [diff] [blame] | 36 | /* USB related libraries */ |
| 37 | #include "osmocom/core/linuxlist.h" |
| 38 | #include "osmocom/core/msgb.h" |
| 39 | #include "llist_irqsafe.h" |
| 40 | #include "usb_buf.h" |
| 41 | #include "simtrace_usb.h" |
| 42 | #include "simtrace_prot.h" |
Christina Quast | a90eefa | 2015-02-24 17:52:29 +0100 | [diff] [blame] | 43 | |
| 44 | /*------------------------------------------------------------------------------ |
| 45 | * Internal definitions |
| 46 | *------------------------------------------------------------------------------*/ |
| 47 | |
Kévin Redon | 8fa6ff5 | 2018-06-25 16:00:33 +0200 | [diff] [blame] | 48 | /*! Maximum Answer-To-Reset (ATR) size in bytes |
Kévin Redon | d7a6de5 | 2018-06-11 13:46:35 +0200 | [diff] [blame] | 49 | * @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 |
| 50 | * @remark technical there is no size limitation since Yi present in T0,TDi will indicate if more interface bytes are present, including TDi+i |
| 51 | */ |
| 52 | #define MAX_ATR_SIZE 33 |
Kévin Redon | 8fa6ff5 | 2018-06-25 16:00:33 +0200 | [diff] [blame] | 53 | /*! Maximum Protocol and Parameters Selection (PPS) size in bytes |
| 54 | * @note defined in ISO/IEC 7816-3:2006(E) section 9.2 |
| 55 | */ |
| 56 | #define MAX_PPS_SIZE 6 |
Kévin Redon | d7a6de5 | 2018-06-11 13:46:35 +0200 | [diff] [blame] | 57 | |
| 58 | /*! ISO 7816-3 states relevant to the sniff mode */ |
| 59 | enum iso7816_3_sniff_state { |
| 60 | ISO7816_S_RESET, /*!< in Reset */ |
| 61 | ISO7816_S_WAIT_ATR, /*!< waiting for ATR to start */ |
| 62 | ISO7816_S_IN_ATR, /*!< while we are receiving the ATR */ |
Kévin Redon | 00ec89d | 2018-06-27 16:41:52 +0200 | [diff] [blame] | 63 | ISO7816_S_WAIT_TPDU, /*!< waiting for start of new TPDU */ |
| 64 | ISO7816_S_IN_TPDU, /*!< inside a single TPDU */ |
Kévin Redon | 8fa6ff5 | 2018-06-25 16:00:33 +0200 | [diff] [blame] | 65 | ISO7816_S_IN_PPS_REQ, /*!< while we are inside the PPS request */ |
| 66 | ISO7816_S_WAIT_PPS_RSP, /*!< waiting for start of the PPS response */ |
| 67 | ISO7816_S_IN_PPS_RSP, /*!< while we are inside the PPS request */ |
Kévin Redon | d7a6de5 | 2018-06-11 13:46:35 +0200 | [diff] [blame] | 68 | }; |
| 69 | |
| 70 | /*! Answer-To-Reset (ATR) sub-states of ISO7816_S_IN_ATR |
| 71 | * @note defined in ISO/IEC 7816-3:2006(E) section 8 |
| 72 | */ |
| 73 | enum atr_sniff_state { |
| 74 | ATR_S_WAIT_TS, /*!< initial byte */ |
| 75 | ATR_S_WAIT_T0, /*!< format byte */ |
| 76 | ATR_S_WAIT_TA, /*!< first sub-group interface byte */ |
| 77 | ATR_S_WAIT_TB, /*!< second sub-group interface byte */ |
| 78 | ATR_S_WAIT_TC, /*!< third sub-group interface byte */ |
| 79 | ATR_S_WAIT_TD, /*!< fourth sub-group interface byte */ |
| 80 | ATR_S_WAIT_HIST, /*!< historical byte */ |
| 81 | ATR_S_WAIT_TCK, /*!< check byte */ |
Kévin Redon | 8fa6ff5 | 2018-06-25 16:00:33 +0200 | [diff] [blame] | 82 | }; |
| 83 | |
| 84 | /*! Protocol and Parameters Selection (PPS) sub-states of ISO7816_S_IN_PTS_REQ/ISO7816_S_IN_PTS_RSP |
| 85 | * @note defined in ISO/IEC 7816-3:2006(E) section 9 |
| 86 | */ |
| 87 | enum pps_sniff_state { |
| 88 | PPS_S_WAIT_PPSS, /*!< initial byte */ |
| 89 | PPS_S_WAIT_PPS0, /*!< format byte */ |
| 90 | PPS_S_WAIT_PPS1, /*!< first parameter byte */ |
| 91 | PPS_S_WAIT_PPS2, /*!< second parameter byte */ |
| 92 | PPS_S_WAIT_PPS3, /*!< third parameter byte */ |
| 93 | PPS_S_WAIT_PCK, /*!< check byte */ |
Kévin Redon | 35e8bdf | 2018-07-03 16:15:58 +0200 | [diff] [blame] | 94 | PPS_S_WAIT_END, /*!< all done */ |
Kévin Redon | d7a6de5 | 2018-06-11 13:46:35 +0200 | [diff] [blame] | 95 | }; |
Kévin Redon | 45ad62d | 2018-06-07 18:56:41 +0200 | [diff] [blame] | 96 | |
Kévin Redon | 00ec89d | 2018-06-27 16:41:52 +0200 | [diff] [blame] | 97 | /*! Transport Protocol Data Unit (TPDU) sub-states of ISO7816_S_IN_TPDU |
| 98 | * @note defined in ISO/IEC 7816-3:2006(E) section 10 and 12 |
| 99 | * @remark APDUs are formed by one or more command+response TPDUs |
| 100 | */ |
| 101 | enum tpdu_sniff_state { |
| 102 | TPDU_S_CLA, /*!< class byte */ |
| 103 | TPDU_S_INS, /*!< instruction byte */ |
| 104 | TPDU_S_P1, /*!< first parameter byte for the instruction */ |
| 105 | TPDU_S_P2, /*!< second parameter byte for the instruction */ |
| 106 | TPDU_S_P3, /*!< third parameter byte encoding the data length */ |
| 107 | TPDU_S_PROCEDURE, /*!< procedure byte (could also be SW1) */ |
| 108 | TPDU_S_DATA_REMAINING, /*!< remaining data bytes */ |
| 109 | TPDU_S_DATA_SINGLE, /*!< single data byte */ |
| 110 | TPDU_S_SW1, /*!< first status word */ |
| 111 | TPDU_S_SW2, /*!< second status word */ |
| 112 | }; |
| 113 | |
Christina Quast | a90eefa | 2015-02-24 17:52:29 +0100 | [diff] [blame] | 114 | /*------------------------------------------------------------------------------ |
| 115 | * Internal variables |
| 116 | *------------------------------------------------------------------------------*/ |
Kévin Redon | d7a6de5 | 2018-06-11 13:46:35 +0200 | [diff] [blame] | 117 | |
| 118 | /* note: the sniffer code is currently designed to support only one sniffing interface, but the hardware would support a second one. |
| 119 | * to support a second sniffer interface the code should be restructured to use handles. |
| 120 | */ |
| 121 | /* Pin configurations */ |
Kévin Redon | 8fa6ff5 | 2018-06-25 16:00:33 +0200 | [diff] [blame] | 122 | /*! Pin configuration to sniff communication (using USART connection card) */ |
Kévin Redon | ee62a9d | 2018-06-11 13:42:23 +0200 | [diff] [blame] | 123 | static const Pin pins_sniff[] = { PINS_SIM_SNIFF }; |
Kévin Redon | 8fa6ff5 | 2018-06-25 16:00:33 +0200 | [diff] [blame] | 124 | /*! Pin configuration to interconnect phone and card using the bus switch */ |
Harald Welte | 7dd3dfd | 2016-03-03 12:32:04 +0100 | [diff] [blame] | 125 | static const Pin pins_bus[] = { PINS_BUS_SNIFF }; |
Kévin Redon | 8fa6ff5 | 2018-06-25 16:00:33 +0200 | [diff] [blame] | 126 | /*! Pin configuration to power the card by the phone */ |
Kévin Redon | d7a6de5 | 2018-06-11 13:46:35 +0200 | [diff] [blame] | 127 | static const Pin pins_power[] = { PINS_PWR_SNIFF }; |
Kévin Redon | 8fa6ff5 | 2018-06-25 16:00:33 +0200 | [diff] [blame] | 128 | /*! Pin configuration for timer counter to measure ETU timing */ |
Kévin Redon | 45ad62d | 2018-06-07 18:56:41 +0200 | [diff] [blame] | 129 | static const Pin pins_tc[] = { PINS_TC }; |
Kévin Redon | 8fa6ff5 | 2018-06-25 16:00:33 +0200 | [diff] [blame] | 130 | /*! Pin configuration for card reset line */ |
| 131 | static const Pin pin_rst = PIN_SIM_RST_SNIFF; |
| 132 | |
Kévin Redon | d7a6de5 | 2018-06-11 13:46:35 +0200 | [diff] [blame] | 133 | /* USART related variables */ |
Kévin Redon | 8fa6ff5 | 2018-06-25 16:00:33 +0200 | [diff] [blame] | 134 | /*! USART peripheral used to sniff communication */ |
Kévin Redon | 45ad62d | 2018-06-07 18:56:41 +0200 | [diff] [blame] | 135 | static struct Usart_info sniff_usart = { |
| 136 | .base = USART_SIM, |
| 137 | .id = ID_USART_SIM, |
Harald Welte | 7dd3dfd | 2016-03-03 12:32:04 +0100 | [diff] [blame] | 138 | .state = USART_RCV, |
| 139 | }; |
Kévin Redon | 8fa6ff5 | 2018-06-25 16:00:33 +0200 | [diff] [blame] | 140 | /*! Ring buffer to store sniffer communication data */ |
Kévin Redon | 7b73462 | 2018-06-06 16:13:48 +0200 | [diff] [blame] | 141 | static struct ringbuf sniff_buffer; |
| 142 | |
Kévin Redon | de97fd2 | 2018-07-01 18:23:23 +0200 | [diff] [blame] | 143 | /* Flags to know is the card status changed (see SIMTRACE_MSGT_DT_SNIFF_CHANGE flags) */ |
| 144 | volatile uint32_t change_flags = 0; |
| 145 | |
Kévin Redon | 8fa6ff5 | 2018-06-25 16:00:33 +0200 | [diff] [blame] | 146 | /* ISO 7816 variables */ |
| 147 | /*! ISO 7816-3 state */ |
| 148 | enum iso7816_3_sniff_state iso_state = ISO7816_S_RESET; |
| 149 | /*! ATR state */ |
| 150 | enum atr_sniff_state atr_state; |
| 151 | /*! ATR data |
| 152 | * @remark can be used to check later protocol changes |
| 153 | */ |
| 154 | uint8_t atr[MAX_ATR_SIZE]; |
| 155 | /*! Current index in the ATR data */ |
| 156 | uint8_t atr_i = 0; |
| 157 | /*! If convention conversion is needed */ |
| 158 | bool convention_convert = false; |
| 159 | /*! The supported T protocols */ |
| 160 | uint16_t t_protocol_support = 0; |
| 161 | /*! PPS state |
| 162 | * @remark it is shared between request and response since they aren't simultaneous but follow the same procedure |
| 163 | */ |
| 164 | enum pps_sniff_state pps_state; |
| 165 | /*! PPS request data |
| 166 | * @remark can be used to check PPS response |
| 167 | */ |
| 168 | uint8_t pps_req[MAX_PPS_SIZE]; |
| 169 | /*! PPS response data */ |
| 170 | uint8_t pps_rsp[MAX_PPS_SIZE]; |
Kévin Redon | 00ec89d | 2018-06-27 16:41:52 +0200 | [diff] [blame] | 171 | /*! TPDU state */ |
| 172 | enum tpdu_sniff_state tpdu_state; |
| 173 | /*! Final TPDU packet |
| 174 | * @note this is the complete command+response TPDU, including header, data, and status words |
| 175 | * @remark this does not include the procedure bytes |
| 176 | */ |
| 177 | uint8_t tpdu_packet[5+256+2]; |
| 178 | /*! Current index in TPDU packet */ |
Kévin Redon | c9bd715 | 2018-07-03 16:17:00 +0200 | [diff] [blame] | 179 | uint16_t tpdu_packet_i = 0; |
Kévin Redon | 8fa6ff5 | 2018-06-25 16:00:33 +0200 | [diff] [blame] | 180 | |
King Kévin | 1200a52 | 2018-07-04 10:18:03 +0200 | [diff] [blame^] | 181 | /*! Waiting Time (WT) |
| 182 | * @note defined in ISO/IEC 7816-3:2006(E) section 8.1 and 10.2 |
| 183 | */ |
| 184 | uint32_t wt = 9600; |
| 185 | |
Kévin Redon | 7b73462 | 2018-06-06 16:13:48 +0200 | [diff] [blame] | 186 | /*------------------------------------------------------------------------------ |
Kévin Redon | 7b73462 | 2018-06-06 16:13:48 +0200 | [diff] [blame] | 187 | * Internal functions |
| 188 | *------------------------------------------------------------------------------*/ |
Kévin Redon | 36abece | 2018-06-04 16:30:01 +0200 | [diff] [blame] | 189 | |
Kévin Redon | 8fa6ff5 | 2018-06-25 16:00:33 +0200 | [diff] [blame] | 190 | /*! Convert data between direct and inverse convention |
| 191 | * @note direct convention is LSb first and HIGH=1; inverse conversion in MSb first and LOW=1 |
| 192 | * @remark use a look up table to speed up conversion |
| 193 | */ |
| 194 | static 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, }; |
| 195 | |
King Kévin | 1200a52 | 2018-07-04 10:18:03 +0200 | [diff] [blame^] | 196 | /*! Update Waiting Time (WT) |
| 197 | * @param[in] wi Waiting Integer (0 if unchanged) |
| 198 | * @param[in] d Baud Rate divider (0 if unchanged) |
| 199 | * @note set wt to be used by the receiver timeout |
| 200 | * @note defined in ISO/IEC 7816-3:2006(E) section 8.1 and 10.2 |
| 201 | */ |
| 202 | static void update_wt(uint8_t wi, uint8_t d) |
| 203 | { |
| 204 | static uint8_t wt_wi = 10; /* Waiting time Integer (WI), used to calculate the Waiting Time (WT) */ |
| 205 | static uint8_t wt_d = 1; /* baud rate adjustment integer (the actual value, not the table index) */ |
| 206 | |
| 207 | if (0!=wi) { |
| 208 | wt_wi = wi; |
| 209 | } |
| 210 | if (0!=d) { |
| 211 | wt_d = d; |
| 212 | } |
| 213 | wt = wt_wi*960UL*wt_d; |
| 214 | TRACE_INFO("WT updated to %u\n\r", wt); |
| 215 | } |
| 216 | |
Kévin Redon | de97fd2 | 2018-07-01 18:23:23 +0200 | [diff] [blame] | 217 | /*! Allocate USB buffer and push + initialize simtrace_msg_hdr |
| 218 | * @param[in] ep USB IN endpoint where the message will be sent to |
| 219 | * @param[in] msg_class SIMtrace USB message class |
| 220 | * @param[in] msg_type SIMtrace USB message type |
| 221 | * @return USB message with allocated ans initialized header, or NULL if allocation failed |
| 222 | */ |
| 223 | static struct msgb *usb_msg_alloc_hdr(uint8_t ep, uint8_t msg_class, uint8_t msg_type) |
| 224 | { |
| 225 | struct msgb *usb_msg = usb_buf_alloc(SIMTRACE_USB_EP_CARD_DATAIN); |
| 226 | if (!usb_msg) { |
| 227 | return NULL; |
| 228 | } |
| 229 | struct simtrace_msg_hdr *usb_msg_header; |
| 230 | usb_msg->l1h = msgb_put(usb_msg, sizeof(*usb_msg_header)); |
| 231 | usb_msg_header = (struct simtrace_msg_hdr *) usb_msg->l1h; |
| 232 | memset(usb_msg_header, 0, sizeof(*usb_msg_header)); |
| 233 | usb_msg_header->msg_class = SIMTRACE_MSGC_SNIFF; |
| 234 | usb_msg_header->msg_type = SIMTRACE_MSGT_SNIFF_CHANGE; |
| 235 | usb_msg->l2h = usb_msg->l1h + sizeof(*usb_msg_header); |
| 236 | |
| 237 | return usb_msg; |
| 238 | } |
| 239 | |
| 240 | /* update SIMtrace header msg_len and submit USB buffer |
| 241 | * param[in] usb_msg USB message to update and send |
| 242 | */ |
| 243 | void usb_msg_upd_len_and_submit(struct msgb *usb_msg) |
| 244 | { |
| 245 | struct simtrace_msg_hdr *usb_msg_header = (struct simtrace_msg_hdr *) usb_msg->l1h; |
| 246 | usb_msg_header->msg_len = msgb_length(usb_msg); |
| 247 | usb_buf_submit(usb_msg); |
| 248 | } |
| 249 | |
Kévin Redon | 8fa6ff5 | 2018-06-25 16:00:33 +0200 | [diff] [blame] | 250 | /*! Update the ISO 7816-3 state |
| 251 | * @param[in] iso_state_new new ISO 7816-3 state to update to |
| 252 | */ |
| 253 | static void change_state(enum iso7816_3_sniff_state iso_state_new) |
| 254 | { |
| 255 | /* sanity check */ |
| 256 | if (iso_state_new==iso_state) { |
| 257 | TRACE_WARNING("Already in ISO 7816 state %u\n\r", iso_state); |
| 258 | return; |
| 259 | } |
| 260 | |
| 261 | /* handle actions to perform when switching state */ |
| 262 | switch (iso_state_new) { |
| 263 | case ISO7816_S_RESET: |
| 264 | update_fidi(&sniff_usart, 0x11); /* reset baud rate to default Di/Fi values */ |
King Kévin | 1200a52 | 2018-07-04 10:18:03 +0200 | [diff] [blame^] | 265 | update_wt(10, 1); /* reset WT time-out */ |
Kévin Redon | 709a431 | 2018-07-03 16:10:04 +0200 | [diff] [blame] | 266 | change_flags |= SNIFF_CHANGE_FLAG_RESET_HOLD; /* set flag and let main loop send it */ |
Kévin Redon | 8fa6ff5 | 2018-06-25 16:00:33 +0200 | [diff] [blame] | 267 | break; |
| 268 | case ISO7816_S_WAIT_ATR: |
| 269 | rbuf_reset(&sniff_buffer); /* reset buffer for new communication */ |
Kévin Redon | 709a431 | 2018-07-03 16:10:04 +0200 | [diff] [blame] | 270 | change_flags |= SNIFF_CHANGE_FLAG_RESET_RELEASE; /* set flag and let main loop send it */ |
Kévin Redon | 8fa6ff5 | 2018-06-25 16:00:33 +0200 | [diff] [blame] | 271 | break; |
| 272 | case ISO7816_S_IN_ATR: |
| 273 | atr_i = 0; |
| 274 | convention_convert = false; |
| 275 | t_protocol_support = 0; |
| 276 | atr_state = ATR_S_WAIT_TS; |
| 277 | break; |
| 278 | case ISO7816_S_IN_PPS_REQ: |
| 279 | case ISO7816_S_IN_PPS_RSP: |
| 280 | pps_state = PPS_S_WAIT_PPSS; |
| 281 | break; |
Kévin Redon | 00ec89d | 2018-06-27 16:41:52 +0200 | [diff] [blame] | 282 | case ISO7816_S_WAIT_TPDU: |
| 283 | tpdu_state = TPDU_S_CLA; |
| 284 | tpdu_packet_i = 0; |
| 285 | break; |
Kévin Redon | 8fa6ff5 | 2018-06-25 16:00:33 +0200 | [diff] [blame] | 286 | default: |
| 287 | break; |
| 288 | } |
| 289 | |
| 290 | /* save new state */ |
| 291 | iso_state = iso_state_new; |
Kévin Redon | cf59919 | 2018-06-27 16:38:31 +0200 | [diff] [blame] | 292 | //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 */ |
| 293 | } |
| 294 | |
Kévin Redon | de97fd2 | 2018-07-01 18:23:23 +0200 | [diff] [blame] | 295 | /*! Send current ATR over USB |
Kévin Redon | 35e8bdf | 2018-07-03 16:15:58 +0200 | [diff] [blame] | 296 | * @param[in] complete if the ATR is complete |
| 297 | * @note Also print the ATR to debug console |
Kévin Redon | de97fd2 | 2018-07-01 18:23:23 +0200 | [diff] [blame] | 298 | */ |
Kévin Redon | 35e8bdf | 2018-07-03 16:15:58 +0200 | [diff] [blame] | 299 | static void usb_send_atr(bool complete) |
Kévin Redon | cf59919 | 2018-06-27 16:38:31 +0200 | [diff] [blame] | 300 | { |
Kévin Redon | de97fd2 | 2018-07-01 18:23:23 +0200 | [diff] [blame] | 301 | /* Check state */ |
Kévin Redon | cf59919 | 2018-06-27 16:38:31 +0200 | [diff] [blame] | 302 | if (ISO7816_S_IN_ATR!=iso_state) { |
| 303 | TRACE_WARNING("Can't print ATR in ISO 7816-3 state %u\n\r", iso_state); |
| 304 | return; |
| 305 | } |
Kévin Redon | de97fd2 | 2018-07-01 18:23:23 +0200 | [diff] [blame] | 306 | if (atr_i>=ARRAY_SIZE(atr)) { |
| 307 | TRACE_ERROR("ATR buffer overflow\n\r"); |
| 308 | return; |
| 309 | } |
Kévin Redon | cf59919 | 2018-06-27 16:38:31 +0200 | [diff] [blame] | 310 | |
Kévin Redon | de97fd2 | 2018-07-01 18:23:23 +0200 | [diff] [blame] | 311 | /* Show activity on LED */ |
Kévin Redon | cf59919 | 2018-06-27 16:38:31 +0200 | [diff] [blame] | 312 | led_blink(LED_GREEN, BLINK_2O_F); |
Kévin Redon | de97fd2 | 2018-07-01 18:23:23 +0200 | [diff] [blame] | 313 | |
| 314 | /* Print ATR */ |
Kévin Redon | 35e8bdf | 2018-07-03 16:15:58 +0200 | [diff] [blame] | 315 | printf("ATR%s: ", complete ? "" : " (incomplete)"); |
Kévin Redon | cf59919 | 2018-06-27 16:38:31 +0200 | [diff] [blame] | 316 | uint8_t i; |
Kévin Redon | de97fd2 | 2018-07-01 18:23:23 +0200 | [diff] [blame] | 317 | for (i = 0; i < atr_i; i++) { |
Kévin Redon | cf59919 | 2018-06-27 16:38:31 +0200 | [diff] [blame] | 318 | printf("%02x ", atr[i]); |
| 319 | } |
| 320 | printf("\n\r"); |
Kévin Redon | de97fd2 | 2018-07-01 18:23:23 +0200 | [diff] [blame] | 321 | |
| 322 | /* Send ATR over USB */ |
| 323 | struct msgb *usb_msg = usb_msg_alloc_hdr(SIMTRACE_USB_EP_CARD_DATAIN, SIMTRACE_MSGC_SNIFF, SIMTRACE_MSGT_SNIFF_ATR); |
| 324 | if (!usb_msg) { |
| 325 | return; |
| 326 | } |
| 327 | struct sniff_data *usb_sniff_data_atr = (struct sniff_data *) msgb_put(usb_msg, sizeof(*usb_sniff_data_atr)); |
Kévin Redon | 35e8bdf | 2018-07-03 16:15:58 +0200 | [diff] [blame] | 328 | usb_sniff_data_atr->complete = complete; |
Kévin Redon | de97fd2 | 2018-07-01 18:23:23 +0200 | [diff] [blame] | 329 | usb_sniff_data_atr->length = atr_i; |
| 330 | uint8_t *data = msgb_put(usb_msg, usb_sniff_data_atr->length); |
| 331 | memcpy(data, atr, atr_i); |
| 332 | usb_msg_upd_len_and_submit(usb_msg); |
Kévin Redon | 8fa6ff5 | 2018-06-25 16:00:33 +0200 | [diff] [blame] | 333 | } |
| 334 | |
| 335 | /*! Process ATR byte |
| 336 | * @param[in] byte ATR byte to process |
| 337 | */ |
| 338 | static void process_byte_atr(uint8_t byte) |
| 339 | { |
| 340 | static uint8_t atr_hist_len = 0; /* store the number of expected historical bytes */ |
| 341 | static uint8_t y = 0; /* last mask of the upcoming TA, TB, TC, TD interface bytes */ |
King Kévin | 1200a52 | 2018-07-04 10:18:03 +0200 | [diff] [blame^] | 342 | static uint8_t i = 0; /* interface byte subgroup number */ |
Kévin Redon | 8fa6ff5 | 2018-06-25 16:00:33 +0200 | [diff] [blame] | 343 | |
| 344 | /* sanity check */ |
| 345 | if (ISO7816_S_IN_ATR!=iso_state) { |
| 346 | TRACE_ERROR("Processing ATR data in wrong ISO 7816-3 state %u\n\r", iso_state); |
| 347 | return; |
| 348 | } |
| 349 | if (atr_i>=ARRAY_SIZE(atr)) { |
Kévin Redon | cf59919 | 2018-06-27 16:38:31 +0200 | [diff] [blame] | 350 | TRACE_ERROR("ATR data overflow\n\r"); |
Kévin Redon | 8fa6ff5 | 2018-06-25 16:00:33 +0200 | [diff] [blame] | 351 | return; |
| 352 | } |
| 353 | |
| 354 | /* save data for use by other functions */ |
| 355 | atr[atr_i++] = byte; |
| 356 | |
| 357 | /* handle ATR byte depending on current state */ |
| 358 | switch (atr_state) { |
| 359 | case ATR_S_WAIT_TS: /* see ISO/IEC 7816-3:2006 section 8.1 */ |
| 360 | switch (byte) { |
| 361 | case 0x23: /* direct convention used, but decoded using inverse convention (a parity error should also have occurred) */ |
| 362 | case 0x30: /* inverse convention used, but decoded using direct convention (a parity error should also have occurred) */ |
| 363 | convention_convert = !convention_convert; |
| 364 | case 0x3b: /* direct convention used and correctly decoded */ |
| 365 | case 0x3f: /* inverse convention used and correctly decoded */ |
| 366 | atr_state = ATR_S_WAIT_T0; /* wait for format byte */ |
| 367 | break; |
| 368 | default: |
| 369 | atr_i--; /* revert last byte */ |
| 370 | TRACE_WARNING("Invalid TS received\n\r"); |
| 371 | } |
King Kévin | 1200a52 | 2018-07-04 10:18:03 +0200 | [diff] [blame^] | 372 | i = 0; /* first interface byte sub-group is coming (T0 is kind of TD0) */ |
Kévin Redon | 8fa6ff5 | 2018-06-25 16:00:33 +0200 | [diff] [blame] | 373 | break; |
| 374 | case ATR_S_WAIT_T0: /* see ISO/IEC 7816-3:2006 section 8.2.2 */ |
| 375 | case ATR_S_WAIT_TD: /* see ISO/IEC 7816-3:2006 section 8.2.3 */ |
| 376 | if (ATR_S_WAIT_T0==atr_state) { |
| 377 | atr_hist_len = (byte&0x0f); /* save the number of historical bytes */ |
| 378 | } else if (ATR_S_WAIT_TD==atr_state) { |
| 379 | t_protocol_support |= (1<<(byte&0x0f)); /* remember supported protocol to know if TCK will be present */ |
| 380 | } |
| 381 | y = (byte&0xf0); /* remember upcoming interface bytes */ |
King Kévin | 1200a52 | 2018-07-04 10:18:03 +0200 | [diff] [blame^] | 382 | i++; /* next interface byte sub-group is coming */ |
Kévin Redon | 8fa6ff5 | 2018-06-25 16:00:33 +0200 | [diff] [blame] | 383 | if (y&0x10) { |
| 384 | atr_state = ATR_S_WAIT_TA; /* wait for interface byte TA */ |
| 385 | break; |
| 386 | } |
| 387 | case ATR_S_WAIT_TA: /* see ISO/IEC 7816-3:2006 section 8.2.3 */ |
| 388 | if (y&0x20) { |
| 389 | atr_state = ATR_S_WAIT_TB; /* wait for interface byte TB */ |
| 390 | break; |
| 391 | } |
| 392 | case ATR_S_WAIT_TB: /* see ISO/IEC 7816-3:2006 section 8.2.3 */ |
| 393 | if (y&0x40) { |
| 394 | atr_state = ATR_S_WAIT_TC; /* wait for interface byte TC */ |
| 395 | break; |
| 396 | } |
| 397 | case ATR_S_WAIT_TC: /* see ISO/IEC 7816-3:2006 section 8.2.3 */ |
King Kévin | 1200a52 | 2018-07-04 10:18:03 +0200 | [diff] [blame^] | 398 | /* retrieve WI encoded in TC2*/ |
| 399 | if (ATR_S_WAIT_TC==atr_state && 2==i) { |
| 400 | if (0==byte) { |
| 401 | update_wt(10, 0); |
| 402 | } else { |
| 403 | update_wt(byte, 0); |
| 404 | } |
| 405 | } |
Kévin Redon | 8fa6ff5 | 2018-06-25 16:00:33 +0200 | [diff] [blame] | 406 | if (y&0x80) { |
| 407 | atr_state = ATR_S_WAIT_TD; /* wait for interface byte TD */ |
| 408 | break; |
| 409 | } else if (atr_hist_len) { |
| 410 | atr_state = ATR_S_WAIT_HIST; /* wait for historical bytes */ |
| 411 | break; |
| 412 | } |
| 413 | case ATR_S_WAIT_HIST: /* see ISO/IEC 7816-3:2006 section 8.2.4 */ |
| 414 | if (atr_hist_len) { |
| 415 | atr_hist_len--; |
| 416 | } |
| 417 | if (0==atr_hist_len) { |
| 418 | if (t_protocol_support>1) { |
| 419 | atr_state = ATR_S_WAIT_TCK; /* wait for check bytes */ |
| 420 | break; |
| 421 | } |
| 422 | } else { |
| 423 | break; |
| 424 | } |
| 425 | case ATR_S_WAIT_TCK: /* see ISO/IEC 7816-3:2006 section 8.2.5 */ |
| 426 | /* we could verify the checksum, but we are just here to sniff */ |
Kévin Redon | 35e8bdf | 2018-07-03 16:15:58 +0200 | [diff] [blame] | 427 | usb_send_atr(true); /* send ATR to host software using USB */ |
Kévin Redon | 00ec89d | 2018-06-27 16:41:52 +0200 | [diff] [blame] | 428 | change_state(ISO7816_S_WAIT_TPDU); /* go to next state */ |
Kévin Redon | 8fa6ff5 | 2018-06-25 16:00:33 +0200 | [diff] [blame] | 429 | break; |
| 430 | default: |
| 431 | TRACE_INFO("Unknown ATR state %u\n\r", atr_state); |
| 432 | } |
| 433 | } |
| 434 | |
Kévin Redon | de97fd2 | 2018-07-01 18:23:23 +0200 | [diff] [blame] | 435 | /*! Send current PPS over USB |
Kévin Redon | 35e8bdf | 2018-07-03 16:15:58 +0200 | [diff] [blame] | 436 | * @param[in] complete if the PPS is complete |
| 437 | * @note Also print the PPS over the debug console |
Kévin Redon | de97fd2 | 2018-07-01 18:23:23 +0200 | [diff] [blame] | 438 | */ |
Kévin Redon | 35e8bdf | 2018-07-03 16:15:58 +0200 | [diff] [blame] | 439 | static void usb_send_pps(bool complete) |
Kévin Redon | cf59919 | 2018-06-27 16:38:31 +0200 | [diff] [blame] | 440 | { |
| 441 | uint8_t *pps_cur; /* current PPS (request or response) */ |
| 442 | |
Kévin Redon | de97fd2 | 2018-07-01 18:23:23 +0200 | [diff] [blame] | 443 | /* Sanity check */ |
Kévin Redon | cf59919 | 2018-06-27 16:38:31 +0200 | [diff] [blame] | 444 | if (ISO7816_S_IN_PPS_REQ==iso_state) { |
| 445 | pps_cur = pps_req; |
| 446 | } else if (ISO7816_S_IN_PPS_RSP==iso_state) { |
| 447 | pps_cur = pps_rsp; |
| 448 | } else { |
| 449 | TRACE_ERROR("Can't print PPS in ISO 7816-3 state %u\n\r", iso_state); |
| 450 | return; |
| 451 | } |
| 452 | |
Kévin Redon | de97fd2 | 2018-07-01 18:23:23 +0200 | [diff] [blame] | 453 | /* Get only relevant data */ |
| 454 | uint8_t pps[6]; |
| 455 | uint8_t pps_i = 0; |
Kévin Redon | 35e8bdf | 2018-07-03 16:15:58 +0200 | [diff] [blame] | 456 | if (pps_state>PPS_S_WAIT_PPSS) { |
| 457 | pps[pps_i++] = pps_cur[0]; |
| 458 | } |
| 459 | if (pps_state>PPS_S_WAIT_PPS0) { |
| 460 | pps[pps_i++] = pps_cur[1]; |
| 461 | } |
| 462 | if (pps_state>PPS_S_WAIT_PPS1 && pps_cur[1]&0x10) { |
Kévin Redon | de97fd2 | 2018-07-01 18:23:23 +0200 | [diff] [blame] | 463 | pps[pps_i++] = pps_cur[2]; |
Kévin Redon | cf59919 | 2018-06-27 16:38:31 +0200 | [diff] [blame] | 464 | } |
Kévin Redon | 35e8bdf | 2018-07-03 16:15:58 +0200 | [diff] [blame] | 465 | if (pps_state>PPS_S_WAIT_PPS2 && pps_cur[1]&0x20) { |
Kévin Redon | de97fd2 | 2018-07-01 18:23:23 +0200 | [diff] [blame] | 466 | pps[pps_i++] = pps_cur[3]; |
Kévin Redon | cf59919 | 2018-06-27 16:38:31 +0200 | [diff] [blame] | 467 | } |
Kévin Redon | 35e8bdf | 2018-07-03 16:15:58 +0200 | [diff] [blame] | 468 | if (pps_state>PPS_S_WAIT_PPS3 && pps_cur[1]&0x40) { |
Kévin Redon | de97fd2 | 2018-07-01 18:23:23 +0200 | [diff] [blame] | 469 | pps[pps_i++] = pps_cur[4]; |
Kévin Redon | cf59919 | 2018-06-27 16:38:31 +0200 | [diff] [blame] | 470 | } |
Kévin Redon | 35e8bdf | 2018-07-03 16:15:58 +0200 | [diff] [blame] | 471 | if (pps_state>PPS_S_WAIT_PCK) { |
| 472 | pps[pps_i++] = pps_cur[5]; |
| 473 | } |
Kévin Redon | de97fd2 | 2018-07-01 18:23:23 +0200 | [diff] [blame] | 474 | |
| 475 | /* Show activity on LED */ |
| 476 | led_blink(LED_GREEN, BLINK_2O_F); |
| 477 | |
| 478 | /* Print PPS */ |
Kévin Redon | 35e8bdf | 2018-07-03 16:15:58 +0200 | [diff] [blame] | 479 | printf("PPS%s: ", complete ? "" : " (incomplete)"); |
Kévin Redon | de97fd2 | 2018-07-01 18:23:23 +0200 | [diff] [blame] | 480 | uint8_t i; |
Kévin Redon | 35e8bdf | 2018-07-03 16:15:58 +0200 | [diff] [blame] | 481 | for (i = 0; i < pps_i; i++) { |
Kévin Redon | de97fd2 | 2018-07-01 18:23:23 +0200 | [diff] [blame] | 482 | printf("%02x ", pps[i]); |
| 483 | } |
Kévin Redon | cf59919 | 2018-06-27 16:38:31 +0200 | [diff] [blame] | 484 | printf("\n\r"); |
Kévin Redon | de97fd2 | 2018-07-01 18:23:23 +0200 | [diff] [blame] | 485 | |
| 486 | /* Send message over USB */ |
| 487 | struct msgb *usb_msg = usb_msg_alloc_hdr(SIMTRACE_USB_EP_CARD_DATAIN, SIMTRACE_MSGC_SNIFF, SIMTRACE_MSGT_SNIFF_PPS); |
| 488 | if (!usb_msg) { |
| 489 | return; |
| 490 | } |
| 491 | struct sniff_data *usb_sniff_data_pps = (struct sniff_data *) msgb_put(usb_msg, sizeof(*usb_sniff_data_pps)); |
Kévin Redon | 35e8bdf | 2018-07-03 16:15:58 +0200 | [diff] [blame] | 492 | usb_sniff_data_pps->complete = complete; |
Kévin Redon | de97fd2 | 2018-07-01 18:23:23 +0200 | [diff] [blame] | 493 | usb_sniff_data_pps->length = pps_i; |
| 494 | uint8_t *data = msgb_put(usb_msg, usb_sniff_data_pps->length); |
| 495 | memcpy(data, pps, pps_i); |
| 496 | usb_msg_upd_len_and_submit(usb_msg); |
| 497 | } |
| 498 | |
| 499 | /*! Send Fi/Di change over USB |
| 500 | * @param[in] fidi Fi/Di factor as encoded in TA1 |
| 501 | */ |
| 502 | static void usb_send_fidi(uint8_t fidi) |
| 503 | { |
| 504 | /* Send message over USB */ |
| 505 | struct msgb *usb_msg = usb_msg_alloc_hdr(SIMTRACE_USB_EP_CARD_DATAIN, SIMTRACE_MSGC_SNIFF, SIMTRACE_MSGT_SNIFF_FIDI); |
| 506 | if (!usb_msg) { |
| 507 | return; |
| 508 | } |
| 509 | struct sniff_fidi *usb_sniff_fidi = (struct sniff_fidi *) msgb_put(usb_msg, sizeof(*usb_sniff_fidi)); |
| 510 | usb_sniff_fidi->fidi = fidi; |
| 511 | usb_msg_upd_len_and_submit(usb_msg); |
Kévin Redon | cf59919 | 2018-06-27 16:38:31 +0200 | [diff] [blame] | 512 | } |
| 513 | |
Kévin Redon | 8fa6ff5 | 2018-06-25 16:00:33 +0200 | [diff] [blame] | 514 | static void process_byte_pps(uint8_t byte) |
| 515 | { |
| 516 | uint8_t *pps_cur; /* current PPS (request or response) */ |
| 517 | |
| 518 | /* sanity check */ |
| 519 | if (ISO7816_S_IN_PPS_REQ==iso_state) { |
| 520 | pps_cur = pps_req; |
| 521 | } else if (ISO7816_S_IN_PPS_RSP==iso_state) { |
| 522 | pps_cur = pps_rsp; |
| 523 | } else { |
| 524 | TRACE_ERROR("Processing PPS data in wrong ISO 7816-3 state %u\n\r", iso_state); |
| 525 | return; |
| 526 | } |
| 527 | |
| 528 | /* handle PPS byte depending on current state */ |
| 529 | switch (pps_state) { /* see ISO/IEC 7816-3:2006 section 9.2 */ |
| 530 | case PPS_S_WAIT_PPSS: /*!< initial byte */ |
| 531 | if (0xff) { |
| 532 | pps_cur[0] = byte; |
| 533 | pps_state = PPS_S_WAIT_PPS0; /* go to next state */ |
| 534 | } else { |
| 535 | TRACE_INFO("Invalid PPSS received\n\r"); |
Kévin Redon | 00ec89d | 2018-06-27 16:41:52 +0200 | [diff] [blame] | 536 | change_state(ISO7816_S_WAIT_TPDU); /* go back to TPDU state */ |
Kévin Redon | 8fa6ff5 | 2018-06-25 16:00:33 +0200 | [diff] [blame] | 537 | } |
| 538 | break; |
| 539 | case PPS_S_WAIT_PPS0: /*!< format byte */ |
| 540 | pps_cur[1] = byte; |
| 541 | if (pps_cur[1]&0x10) { |
| 542 | pps_state = PPS_S_WAIT_PPS1; /* go to next state */ |
| 543 | break; |
| 544 | } |
| 545 | case PPS_S_WAIT_PPS1: /*!< first parameter byte */ |
| 546 | pps_cur[2] = byte; /* not always right but doesn't affect the process */ |
| 547 | if (pps_cur[1]&0x20) { |
| 548 | pps_state = PPS_S_WAIT_PPS2; /* go to next state */ |
| 549 | break; |
| 550 | } |
| 551 | case PPS_S_WAIT_PPS2: /*!< second parameter byte */ |
| 552 | pps_cur[3] = byte; /* not always right but doesn't affect the process */ |
| 553 | if (pps_cur[1]&0x40) { |
| 554 | pps_state = PPS_S_WAIT_PPS3; /* go to next state */ |
| 555 | break; |
| 556 | } |
| 557 | case PPS_S_WAIT_PPS3: /*!< third parameter byte */ |
| 558 | pps_cur[4] = byte; /* not always right but doesn't affect the process */ |
| 559 | pps_state = PPS_S_WAIT_PCK; /* go to next state */ |
| 560 | break; |
| 561 | case PPS_S_WAIT_PCK: /*!< check byte */ |
| 562 | pps_cur[5] = byte; /* not always right but doesn't affect the process */ |
| 563 | /* verify the checksum */ |
| 564 | uint8_t check = 0; |
| 565 | check ^= pps_cur[0]; |
| 566 | check ^= pps_cur[1]; |
| 567 | if (pps_cur[1]&0x10) { |
| 568 | check ^= pps_cur[2]; |
| 569 | } |
| 570 | if (pps_cur[1]&0x20) { |
| 571 | check ^= pps_cur[3]; |
| 572 | } |
| 573 | if (pps_cur[1]&0x40) { |
| 574 | check ^= pps_cur[4]; |
| 575 | } |
| 576 | check ^= pps_cur[5]; |
Kévin Redon | 35e8bdf | 2018-07-03 16:15:58 +0200 | [diff] [blame] | 577 | pps_state = PPS_S_WAIT_END; |
| 578 | usb_send_pps(true); /* send PPS to host software using USB */ |
Kévin Redon | 8fa6ff5 | 2018-06-25 16:00:33 +0200 | [diff] [blame] | 579 | if (ISO7816_S_IN_PPS_REQ==iso_state) { |
| 580 | if (0==check) { /* checksum is valid */ |
| 581 | change_state(ISO7816_S_WAIT_PPS_RSP); /* go to next state */ |
| 582 | } else { /* checksum is invalid */ |
Kévin Redon | 00ec89d | 2018-06-27 16:41:52 +0200 | [diff] [blame] | 583 | change_state(ISO7816_S_WAIT_TPDU); /* go to next state */ |
Kévin Redon | 8fa6ff5 | 2018-06-25 16:00:33 +0200 | [diff] [blame] | 584 | } |
| 585 | } else if (ISO7816_S_IN_PPS_RSP==iso_state) { |
| 586 | if (0==check) { /* checksum is valid */ |
| 587 | uint8_t fn, dn; |
| 588 | if (pps_cur[1]&0x10) { |
| 589 | fn = (pps_cur[2]>>4); |
| 590 | dn = (pps_cur[2]&0x0f); |
| 591 | } else { |
| 592 | fn = 1; |
| 593 | dn = 1; |
| 594 | } |
Kévin Redon | 7406337 | 2018-07-03 15:57:03 +0200 | [diff] [blame] | 595 | TRACE_INFO("PPS negotiation successful: Fn=%u Dn=%u\n\r", fi_table[fn], di_table[dn]); |
Kévin Redon | 8fa6ff5 | 2018-06-25 16:00:33 +0200 | [diff] [blame] | 596 | update_fidi(&sniff_usart, pps_cur[2]); |
King Kévin | 1200a52 | 2018-07-04 10:18:03 +0200 | [diff] [blame^] | 597 | update_wt(0, di_table[dn]); |
Kévin Redon | de97fd2 | 2018-07-01 18:23:23 +0200 | [diff] [blame] | 598 | usb_send_fidi(pps_cur[2]); /* send Fi/Di change notification to host software over USB */ |
Kévin Redon | 8fa6ff5 | 2018-06-25 16:00:33 +0200 | [diff] [blame] | 599 | } else { /* checksum is invalid */ |
| 600 | TRACE_INFO("PPS negotiation failed\n\r"); |
| 601 | } |
Kévin Redon | 00ec89d | 2018-06-27 16:41:52 +0200 | [diff] [blame] | 602 | change_state(ISO7816_S_WAIT_TPDU); /* go to next state */ |
Kévin Redon | 8fa6ff5 | 2018-06-25 16:00:33 +0200 | [diff] [blame] | 603 | } |
| 604 | break; |
Kévin Redon | 35e8bdf | 2018-07-03 16:15:58 +0200 | [diff] [blame] | 605 | case PPS_S_WAIT_END: |
| 606 | TRACE_WARNING("Unexpected PPS received %u\n\r", pps_state); |
| 607 | break; |
Kévin Redon | 8fa6ff5 | 2018-06-25 16:00:33 +0200 | [diff] [blame] | 608 | default: |
Kévin Redon | 35e8bdf | 2018-07-03 16:15:58 +0200 | [diff] [blame] | 609 | TRACE_WARNING("Unknown PPS state %u\n\r", pps_state); |
| 610 | break; |
Kévin Redon | 8fa6ff5 | 2018-06-25 16:00:33 +0200 | [diff] [blame] | 611 | } |
| 612 | } |
| 613 | |
Kévin Redon | de97fd2 | 2018-07-01 18:23:23 +0200 | [diff] [blame] | 614 | /*! Send current TPDU over USB |
Kévin Redon | 35e8bdf | 2018-07-03 16:15:58 +0200 | [diff] [blame] | 615 | * @param[in] complete if the TPDU is complete |
| 616 | * @note Also print the TPDU over the debug console |
Kévin Redon | de97fd2 | 2018-07-01 18:23:23 +0200 | [diff] [blame] | 617 | */ |
Kévin Redon | 35e8bdf | 2018-07-03 16:15:58 +0200 | [diff] [blame] | 618 | static void usb_send_tpdu(bool complete) |
Kévin Redon | 8fa6ff5 | 2018-06-25 16:00:33 +0200 | [diff] [blame] | 619 | { |
Kévin Redon | de97fd2 | 2018-07-01 18:23:23 +0200 | [diff] [blame] | 620 | /* Check state */ |
Kévin Redon | 00ec89d | 2018-06-27 16:41:52 +0200 | [diff] [blame] | 621 | if (ISO7816_S_IN_TPDU!=iso_state) { |
| 622 | TRACE_WARNING("Can't print TPDU in ISO 7816-3 state %u\n\r", iso_state); |
| 623 | return; |
| 624 | } |
| 625 | |
Kévin Redon | de97fd2 | 2018-07-01 18:23:23 +0200 | [diff] [blame] | 626 | /* Show activity on LED */ |
Kévin Redon | 00ec89d | 2018-06-27 16:41:52 +0200 | [diff] [blame] | 627 | led_blink(LED_GREEN, BLINK_2O_F); |
Kévin Redon | de97fd2 | 2018-07-01 18:23:23 +0200 | [diff] [blame] | 628 | |
| 629 | /* Print TPDU */ |
Kévin Redon | 35e8bdf | 2018-07-03 16:15:58 +0200 | [diff] [blame] | 630 | printf("TPDU%s: ", complete ? "" : " (incomplete)"); |
Kévin Redon | 00ec89d | 2018-06-27 16:41:52 +0200 | [diff] [blame] | 631 | uint16_t i; |
| 632 | for (i = 0; i < tpdu_packet_i && i < ARRAY_SIZE(tpdu_packet); i++) { |
| 633 | printf("%02x ", tpdu_packet[i]); |
| 634 | } |
| 635 | printf("\n\r"); |
Kévin Redon | de97fd2 | 2018-07-01 18:23:23 +0200 | [diff] [blame] | 636 | |
| 637 | /* Send ATR over USB */ |
| 638 | struct msgb *usb_msg = usb_msg_alloc_hdr(SIMTRACE_USB_EP_CARD_DATAIN, SIMTRACE_MSGC_SNIFF, SIMTRACE_MSGT_SNIFF_TPDU); |
| 639 | if (!usb_msg) { |
| 640 | return; |
| 641 | } |
| 642 | struct sniff_data *usb_sniff_data_tpdu = (struct sniff_data *) msgb_put(usb_msg, sizeof(*usb_sniff_data_tpdu)); |
Kévin Redon | 35e8bdf | 2018-07-03 16:15:58 +0200 | [diff] [blame] | 643 | usb_sniff_data_tpdu->complete = complete; |
Kévin Redon | de97fd2 | 2018-07-01 18:23:23 +0200 | [diff] [blame] | 644 | usb_sniff_data_tpdu->length = tpdu_packet_i; |
| 645 | uint8_t *data = msgb_put(usb_msg, usb_sniff_data_tpdu->length); |
| 646 | memcpy(data, tpdu_packet, tpdu_packet_i); |
| 647 | usb_msg_upd_len_and_submit(usb_msg); |
Kévin Redon | 00ec89d | 2018-06-27 16:41:52 +0200 | [diff] [blame] | 648 | } |
| 649 | |
| 650 | static void process_byte_tpdu(uint8_t byte) |
| 651 | { |
| 652 | /* sanity check */ |
| 653 | if (ISO7816_S_IN_TPDU!=iso_state) { |
| 654 | TRACE_ERROR("Processing TPDU data in wrong ISO 7816-3 state %u\n\r", iso_state); |
| 655 | return; |
| 656 | } |
| 657 | if (tpdu_packet_i>=ARRAY_SIZE(tpdu_packet)) { |
| 658 | TRACE_ERROR("TPDU data overflow\n\r"); |
| 659 | return; |
| 660 | } |
| 661 | |
| 662 | /* handle TPDU byte depending on current state */ |
| 663 | switch (tpdu_state) { |
| 664 | case TPDU_S_CLA: |
| 665 | if (0xff==byte) { |
| 666 | TRACE_WARNING("0xff is not a valid class byte\n\r"); |
Kévin Redon | c9bd715 | 2018-07-03 16:17:00 +0200 | [diff] [blame] | 667 | change_state(ISO7816_S_WAIT_TPDU); /* go back to TPDU state */ |
| 668 | return; |
Kévin Redon | 00ec89d | 2018-06-27 16:41:52 +0200 | [diff] [blame] | 669 | } |
| 670 | tpdu_packet_i = 0; |
| 671 | tpdu_packet[tpdu_packet_i++] = byte; |
| 672 | tpdu_state = TPDU_S_INS; |
| 673 | break; |
| 674 | case TPDU_S_INS: |
Kévin Redon | c9bd715 | 2018-07-03 16:17:00 +0200 | [diff] [blame] | 675 | if ((0x60==(byte&0xf0)) || (0x90==(byte&0xf0))) { |
| 676 | TRACE_WARNING("invalid CLA 0x%02x\n\r", byte); |
| 677 | change_state(ISO7816_S_WAIT_TPDU); /* go back to TPDU state */ |
| 678 | return; |
| 679 | } |
Kévin Redon | 00ec89d | 2018-06-27 16:41:52 +0200 | [diff] [blame] | 680 | tpdu_packet_i = 1; |
| 681 | tpdu_packet[tpdu_packet_i++] = byte; |
| 682 | tpdu_state = TPDU_S_P1; |
| 683 | break; |
| 684 | case TPDU_S_P1: |
| 685 | tpdu_packet_i = 2; |
| 686 | tpdu_packet[tpdu_packet_i++] = byte; |
| 687 | tpdu_state = TPDU_S_P2; |
| 688 | break; |
| 689 | case TPDU_S_P2: |
| 690 | tpdu_packet_i = 3; |
| 691 | tpdu_packet[tpdu_packet_i++] = byte; |
| 692 | tpdu_state = TPDU_S_P3; |
| 693 | break; |
| 694 | case TPDU_S_P3: |
| 695 | tpdu_packet_i = 4; |
| 696 | tpdu_packet[tpdu_packet_i++] = byte; |
| 697 | tpdu_state = TPDU_S_PROCEDURE; |
| 698 | break; |
| 699 | case TPDU_S_PROCEDURE: |
| 700 | if (0x60==byte) { /* wait for next procedure byte */ |
| 701 | break; |
| 702 | } else if (tpdu_packet[1]==byte) { /* get all remaining data bytes */ |
| 703 | tpdu_state = TPDU_S_DATA_REMAINING; |
| 704 | break; |
| 705 | } else if ((~tpdu_packet[1])==byte) { /* get single data byte */ |
| 706 | tpdu_state = TPDU_S_DATA_SINGLE; |
| 707 | break; |
| 708 | } |
| 709 | case TPDU_S_SW1: |
| 710 | if ((0x60==(byte&0xf0)) || (0x90==(byte&0xf0))) { /* this procedure byte is SW1 */ |
| 711 | tpdu_packet[tpdu_packet_i++] = byte; |
| 712 | tpdu_state = TPDU_S_SW2; |
| 713 | } else { |
| 714 | TRACE_WARNING("invalid SW1 0x%02x\n\r", byte); |
Kévin Redon | c9bd715 | 2018-07-03 16:17:00 +0200 | [diff] [blame] | 715 | change_state(ISO7816_S_WAIT_TPDU); /* go back to TPDU state */ |
| 716 | return; |
Kévin Redon | 00ec89d | 2018-06-27 16:41:52 +0200 | [diff] [blame] | 717 | } |
| 718 | break; |
| 719 | case TPDU_S_SW2: |
| 720 | tpdu_packet[tpdu_packet_i++] = byte; |
Kévin Redon | 35e8bdf | 2018-07-03 16:15:58 +0200 | [diff] [blame] | 721 | usb_send_tpdu(true); /* send TPDU to host software using USB */ |
Kévin Redon | 00ec89d | 2018-06-27 16:41:52 +0200 | [diff] [blame] | 722 | change_state(ISO7816_S_WAIT_TPDU); /* this is the end of the TPDU */ |
| 723 | break; |
| 724 | case TPDU_S_DATA_SINGLE: |
| 725 | case TPDU_S_DATA_REMAINING: |
| 726 | tpdu_packet[tpdu_packet_i++] = byte; |
| 727 | if (0==tpdu_packet[4]) { |
| 728 | if (5+256<=tpdu_packet_i) { |
Kévin Redon | c9bd715 | 2018-07-03 16:17:00 +0200 | [diff] [blame] | 729 | tpdu_state = TPDU_S_PROCEDURE; |
Kévin Redon | 00ec89d | 2018-06-27 16:41:52 +0200 | [diff] [blame] | 730 | } |
| 731 | } else { |
| 732 | if (5+tpdu_packet[4]<=tpdu_packet_i) { |
Kévin Redon | c9bd715 | 2018-07-03 16:17:00 +0200 | [diff] [blame] | 733 | tpdu_state = TPDU_S_PROCEDURE; |
Kévin Redon | 00ec89d | 2018-06-27 16:41:52 +0200 | [diff] [blame] | 734 | } |
| 735 | } |
| 736 | if (TPDU_S_DATA_SINGLE==tpdu_state) { |
| 737 | tpdu_state = TPDU_S_PROCEDURE; |
| 738 | } |
| 739 | break; |
| 740 | default: |
| 741 | TRACE_ERROR("unhandled TPDU state %u\n\r", tpdu_state); |
| 742 | } |
Kévin Redon | 8fa6ff5 | 2018-06-25 16:00:33 +0200 | [diff] [blame] | 743 | } |
| 744 | |
Kévin Redon | d7a6de5 | 2018-06-11 13:46:35 +0200 | [diff] [blame] | 745 | /*! Interrupt Service Routine called on USART activity */ |
Kévin Redon | 8fa6ff5 | 2018-06-25 16:00:33 +0200 | [diff] [blame] | 746 | void Sniffer_usart_isr(void) |
Kévin Redon | d7a6de5 | 2018-06-11 13:46:35 +0200 | [diff] [blame] | 747 | { |
King Kévin | 1200a52 | 2018-07-04 10:18:03 +0200 | [diff] [blame^] | 748 | /* Remaining Waiting Time (WI) counter (>16 bits) */ |
| 749 | static volatile uint32_t wt_remaining = 9600; |
| 750 | |
Kévin Redon | d7a6de5 | 2018-06-11 13:46:35 +0200 | [diff] [blame] | 751 | /* Read channel status register */ |
Kévin Redon | 709a431 | 2018-07-03 16:10:04 +0200 | [diff] [blame] | 752 | uint32_t csr = sniff_usart.base->US_CSR; |
| 753 | /* Verify if there was an error */ |
| 754 | if (csr & US_CSR_OVRE) { |
| 755 | TRACE_WARNING("USART overrun error\n\r"); |
| 756 | sniff_usart.base->US_CR |= US_CR_RSTSTA; |
| 757 | } |
| 758 | if (csr & US_CSR_FRAME) { |
| 759 | TRACE_WARNING("USART framing error\n\r"); |
| 760 | sniff_usart.base->US_CR |= US_CR_RSTSTA; |
| 761 | } |
King Kévin | 1200a52 | 2018-07-04 10:18:03 +0200 | [diff] [blame^] | 762 | |
Kévin Redon | d7a6de5 | 2018-06-11 13:46:35 +0200 | [diff] [blame] | 763 | /* Verify if character has been received */ |
| 764 | if (csr & US_CSR_RXRDY) { |
| 765 | /* Read communication data byte between phone and SIM */ |
| 766 | uint8_t byte = sniff_usart.base->US_RHR; |
King Kévin | 1200a52 | 2018-07-04 10:18:03 +0200 | [diff] [blame^] | 767 | /* Reset WT timer */ |
| 768 | wt_remaining = wt; |
Kévin Redon | d7a6de5 | 2018-06-11 13:46:35 +0200 | [diff] [blame] | 769 | /* Store sniffed data into buffer (also clear interrupt */ |
Kévin Redon | 709a431 | 2018-07-03 16:10:04 +0200 | [diff] [blame] | 770 | if (rbuf_is_full(&sniff_buffer)) { |
| 771 | TRACE_ERROR("USART buffer full\n\r"); |
| 772 | } else { |
| 773 | rbuf_write(&sniff_buffer, byte); |
| 774 | } |
Kévin Redon | d7a6de5 | 2018-06-11 13:46:35 +0200 | [diff] [blame] | 775 | } |
King Kévin | 1200a52 | 2018-07-04 10:18:03 +0200 | [diff] [blame^] | 776 | |
Kévin Redon | 638cec8 | 2018-06-27 16:42:56 +0200 | [diff] [blame] | 777 | /* Verify it WT timeout occurred, to detect unresponsive card */ |
| 778 | if (csr & US_CSR_TIMEOUT) { |
King Kévin | 1200a52 | 2018-07-04 10:18:03 +0200 | [diff] [blame^] | 779 | if (wt_remaining<=(sniff_usart.base->US_RTOR&0xffff)) { |
| 780 | /* Just set the flag and let the main loop handle it */ |
| 781 | change_flags |= SNIFF_CHANGE_FLAG_TIMEOUT_WT; |
| 782 | /* Reset timeout value */ |
| 783 | wt_remaining = wt; |
| 784 | } else { |
| 785 | 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 */ |
| 786 | } |
| 787 | if (wt_remaining>0xffff) { |
| 788 | sniff_usart.base->US_RTOR = 0xffff; |
| 789 | } else { |
| 790 | sniff_usart.base->US_RTOR = wt_remaining; |
| 791 | } |
| 792 | /* Stop timeout until next character is received (and clears the timeout flag) */ |
Kévin Redon | 638cec8 | 2018-06-27 16:42:56 +0200 | [diff] [blame] | 793 | sniff_usart.base->US_CR |= US_CR_STTTO; |
King Kévin | 1200a52 | 2018-07-04 10:18:03 +0200 | [diff] [blame^] | 794 | if (!(change_flags & SNIFF_CHANGE_FLAG_TIMEOUT_WT)) { |
| 795 | /* Immediately restart the counter it the WT timeout did not occur (needs the timeout flag to be cleared) */ |
| 796 | sniff_usart.base->US_CR |= US_CR_RETTO; |
| 797 | } |
Kévin Redon | 638cec8 | 2018-06-27 16:42:56 +0200 | [diff] [blame] | 798 | } |
Kévin Redon | d7a6de5 | 2018-06-11 13:46:35 +0200 | [diff] [blame] | 799 | } |
| 800 | |
Kévin Redon | 8fa6ff5 | 2018-06-25 16:00:33 +0200 | [diff] [blame] | 801 | /** PIO interrupt service routine to checks if the card reset line has changed |
| 802 | */ |
| 803 | static void Sniffer_reset_isr(const Pin* pPin) |
| 804 | { |
| 805 | /* Ensure an edge on the reset pin cause the interrupt */ |
| 806 | if (pPin->id!=pin_rst.id || 0==(pPin->mask&pin_rst.mask)) { |
| 807 | TRACE_ERROR("Pin other than reset caused a interrupt\n\r"); |
| 808 | return; |
| 809 | } |
| 810 | /* Update the ISO state according to the reset change */ |
| 811 | if (PIO_Get(&pin_rst)) { |
| 812 | if (ISO7816_S_WAIT_ATR!=iso_state) { |
| 813 | change_state(ISO7816_S_WAIT_ATR); |
| 814 | } |
| 815 | } else { |
| 816 | if (ISO7816_S_RESET!=iso_state) { |
| 817 | change_state(ISO7816_S_RESET); |
| 818 | } |
| 819 | } |
| 820 | } |
| 821 | |
Kévin Redon | d7a6de5 | 2018-06-11 13:46:35 +0200 | [diff] [blame] | 822 | /*------------------------------------------------------------------------------ |
| 823 | * Global functions |
| 824 | *------------------------------------------------------------------------------*/ |
| 825 | |
| 826 | void Sniffer_usart1_irq(void) |
| 827 | { |
| 828 | if (ID_USART1==sniff_usart.id) { |
Kévin Redon | 8fa6ff5 | 2018-06-25 16:00:33 +0200 | [diff] [blame] | 829 | Sniffer_usart_isr(); |
Kévin Redon | d7a6de5 | 2018-06-11 13:46:35 +0200 | [diff] [blame] | 830 | } |
| 831 | } |
| 832 | |
| 833 | void Sniffer_usart0_irq(void) |
| 834 | { |
| 835 | if (ID_USART0==sniff_usart.id) { |
Kévin Redon | 8fa6ff5 | 2018-06-25 16:00:33 +0200 | [diff] [blame] | 836 | Sniffer_usart_isr(); |
Kévin Redon | d7a6de5 | 2018-06-11 13:46:35 +0200 | [diff] [blame] | 837 | } |
| 838 | } |
| 839 | |
Christina Quast | a90eefa | 2015-02-24 17:52:29 +0100 | [diff] [blame] | 840 | /*----------------------------------------------------------------------------- |
Christina Quast | d2b05f0 | 2015-02-25 18:44:24 +0100 | [diff] [blame] | 841 | * Initialization routine |
Christina Quast | a90eefa | 2015-02-24 17:52:29 +0100 | [diff] [blame] | 842 | *-----------------------------------------------------------------------------*/ |
Christina Quast | a90eefa | 2015-02-24 17:52:29 +0100 | [diff] [blame] | 843 | |
Harald Welte | ed75c62 | 2017-11-28 21:23:12 +0100 | [diff] [blame] | 844 | /* Called during USB enumeration after device is enumerated by host */ |
Harald Welte | 7dd3dfd | 2016-03-03 12:32:04 +0100 | [diff] [blame] | 845 | void Sniffer_configure(void) |
Christina Quast | a90eefa | 2015-02-24 17:52:29 +0100 | [diff] [blame] | 846 | { |
Kévin Redon | 36abece | 2018-06-04 16:30:01 +0200 | [diff] [blame] | 847 | TRACE_INFO("Sniffer config\n\r"); |
Christina Quast | a90eefa | 2015-02-24 17:52:29 +0100 | [diff] [blame] | 848 | } |
Christina Quast | fb524b9 | 2015-02-27 13:39:45 +0100 | [diff] [blame] | 849 | |
Harald Welte | ed75c62 | 2017-11-28 21:23:12 +0100 | [diff] [blame] | 850 | /* called when *different* configuration is set by host */ |
Harald Welte | 7dd3dfd | 2016-03-03 12:32:04 +0100 | [diff] [blame] | 851 | void Sniffer_exit(void) |
Christina Quast | fb524b9 | 2015-02-27 13:39:45 +0100 | [diff] [blame] | 852 | { |
Kévin Redon | 36abece | 2018-06-04 16:30:01 +0200 | [diff] [blame] | 853 | TRACE_INFO("Sniffer exit\n\r"); |
Kévin Redon | de97fd2 | 2018-07-01 18:23:23 +0200 | [diff] [blame] | 854 | /* Disable USART */ |
Kévin Redon | 45ad62d | 2018-06-07 18:56:41 +0200 | [diff] [blame] | 855 | USART_DisableIt(sniff_usart.base, US_IER_RXRDY); |
| 856 | /* NOTE: don't forget to set the IRQ according to the USART peripheral used */ |
Kévin Redon | 8fa6ff5 | 2018-06-25 16:00:33 +0200 | [diff] [blame] | 857 | NVIC_DisableIRQ(IRQ_USART_SIM); |
Kévin Redon | 45ad62d | 2018-06-07 18:56:41 +0200 | [diff] [blame] | 858 | USART_SetReceiverEnabled(sniff_usart.base, 0); |
Kévin Redon | de97fd2 | 2018-07-01 18:23:23 +0200 | [diff] [blame] | 859 | /* Disable RST IRQ */ |
| 860 | PIO_DisableIt(&pin_rst); |
| 861 | NVIC_DisableIRQ(PIOA_IRQn); /* CAUTION this needs to match to the correct port */ |
Harald Welte | 7dd3dfd | 2016-03-03 12:32:04 +0100 | [diff] [blame] | 862 | } |
| 863 | |
Kévin Redon | 36abece | 2018-06-04 16:30:01 +0200 | [diff] [blame] | 864 | /* called when *Sniffer* configuration is set by host */ |
Harald Welte | 7dd3dfd | 2016-03-03 12:32:04 +0100 | [diff] [blame] | 865 | void Sniffer_init(void) |
| 866 | { |
Kévin Redon | 36abece | 2018-06-04 16:30:01 +0200 | [diff] [blame] | 867 | TRACE_INFO("Sniffer Init\n\r"); |
Kévin Redon | 7b73462 | 2018-06-06 16:13:48 +0200 | [diff] [blame] | 868 | |
Kévin Redon | 45ad62d | 2018-06-07 18:56:41 +0200 | [diff] [blame] | 869 | /* Configure pins to sniff communication between phone and card */ |
| 870 | PIO_Configure(pins_sniff, PIO_LISTSIZE(pins_sniff)); |
| 871 | /* Configure pins to connect phone to card */ |
| 872 | PIO_Configure(pins_bus, PIO_LISTSIZE(pins_bus)); |
| 873 | /* Configure pins to forward phone power to card */ |
| 874 | PIO_Configure(pins_power, PIO_LISTSIZE(pins_power)); |
Kévin Redon | 8fa6ff5 | 2018-06-25 16:00:33 +0200 | [diff] [blame] | 875 | /* Enable interrupts on port with reset line */ |
| 876 | NVIC_EnableIRQ(PIOA_IRQn); /* CAUTION this needs to match to the correct port */ |
| 877 | /* Register ISR to handle card reset change */ |
| 878 | PIO_ConfigureIt(&pin_rst, &Sniffer_reset_isr); |
| 879 | /* Enable interrupt on card reset pin */ |
| 880 | PIO_EnableIt(&pin_rst); |
Kévin Redon | 45ad62d | 2018-06-07 18:56:41 +0200 | [diff] [blame] | 881 | |
Kévin Redon | 7b73462 | 2018-06-06 16:13:48 +0200 | [diff] [blame] | 882 | /* Clear ring buffer containing the sniffed data */ |
| 883 | rbuf_reset(&sniff_buffer); |
Kévin Redon | 45ad62d | 2018-06-07 18:56:41 +0200 | [diff] [blame] | 884 | /* Configure USART to as ISO-7816 slave communication to sniff communication */ |
| 885 | ISO7816_Init(&sniff_usart, CLK_SLAVE); |
| 886 | /* Only receive data when sniffing */ |
| 887 | USART_SetReceiverEnabled(sniff_usart.base, 1); |
King Kévin | 1200a52 | 2018-07-04 10:18:03 +0200 | [diff] [blame^] | 888 | /* Enable Receiver time-out to detect waiting time (WT) time-out (e.g. unresponsive cards) */ |
| 889 | sniff_usart.base->US_RTOR = wt; |
Kévin Redon | 638cec8 | 2018-06-27 16:42:56 +0200 | [diff] [blame] | 890 | /* Enable interrupt to indicate when data has been received or timeout occurred */ |
| 891 | USART_EnableIt(sniff_usart.base, US_IER_RXRDY | US_IER_TIMEOUT); |
Kévin Redon | fe763b7 | 2018-07-03 16:17:56 +0200 | [diff] [blame] | 892 | /* Set USB priority lower than USART to not miss sniffing data (both at 0 per default) */ |
| 893 | if (NVIC_GetPriority(IRQ_USART_SIM)>=NVIC_GetPriority(UDP_IRQn)) { |
| 894 | NVIC_SetPriority(UDP_IRQn, NVIC_GetPriority(IRQ_USART_SIM)+2); |
| 895 | } |
Kévin Redon | d7a6de5 | 2018-06-11 13:46:35 +0200 | [diff] [blame] | 896 | /* Enable interrupt requests for the USART peripheral */ |
| 897 | NVIC_EnableIRQ(IRQ_USART_SIM); |
| 898 | |
Kévin Redon | 8fa6ff5 | 2018-06-25 16:00:33 +0200 | [diff] [blame] | 899 | /* Reset state */ |
| 900 | if (ISO7816_S_RESET!=iso_state) { |
| 901 | change_state(ISO7816_S_RESET); |
| 902 | } |
Harald Welte | 7dd3dfd | 2016-03-03 12:32:04 +0100 | [diff] [blame] | 903 | } |
| 904 | |
Kévin Redon | 709a431 | 2018-07-03 16:10:04 +0200 | [diff] [blame] | 905 | /*! Send card change flags over USB |
| 906 | * @param[in] flags change flags corresponding to SIMTRACE_MSGT_SNIFF_CHANGE |
| 907 | */ |
| 908 | static void usb_send_change(uint32_t flags) |
| 909 | { |
| 910 | /* Check flags */ |
| 911 | if(0==flags) { /* no changes */ |
| 912 | return; |
| 913 | } |
| 914 | |
| 915 | if (flags&SNIFF_CHANGE_FLAG_TIMEOUT_WT) { |
| 916 | printf("waiting time (WT) timeout\n\r"); |
| 917 | } |
| 918 | |
| 919 | /* Send message over USB */ |
| 920 | struct msgb *usb_msg = usb_msg_alloc_hdr(SIMTRACE_USB_EP_CARD_DATAIN, SIMTRACE_MSGC_SNIFF, SIMTRACE_MSGT_SNIFF_CHANGE); |
| 921 | if (!usb_msg) { |
| 922 | return; |
| 923 | } |
| 924 | struct sniff_change *usb_sniff_change = (struct sniff_change *) msgb_put(usb_msg, sizeof(*usb_sniff_change)); |
| 925 | usb_sniff_change->flags = flags; |
| 926 | usb_msg_upd_len_and_submit(usb_msg); |
| 927 | } |
| 928 | |
Kévin Redon | 8fa6ff5 | 2018-06-25 16:00:33 +0200 | [diff] [blame] | 929 | /* Main (idle/busy) loop of this USB configuration */ |
Harald Welte | 7dd3dfd | 2016-03-03 12:32:04 +0100 | [diff] [blame] | 930 | void Sniffer_run(void) |
| 931 | { |
Kévin Redon | de97fd2 | 2018-07-01 18:23:23 +0200 | [diff] [blame] | 932 | /* Handle USB queue */ |
| 933 | /* first try to send any pending messages on INT */ |
| 934 | usb_refill_to_host(SIMTRACE_USB_EP_CARD_INT); |
| 935 | /* then try to send any pending messages on IN */ |
| 936 | usb_refill_to_host(SIMTRACE_USB_EP_CARD_DATAIN); |
| 937 | /* ensure we can handle incoming USB messages from the host */ |
| 938 | /* currently we don't need any incoming data |
| 939 | usb_refill_from_host(SIMTRACE_USB_EP_CARD_DATAOUT); |
| 940 | struct llist_head *queue = usb_get_queue(SIMTRACE_USB_EP_CARD_DATAOUT); |
| 941 | process_any_usb_commands(queue); |
| 942 | */ |
| 943 | |
| 944 | /* Handle sniffed data */ |
| 945 | if (!rbuf_is_empty(&sniff_buffer)) { /* use if instead of while to let the main loop restart the watchdog */ |
| 946 | uint8_t byte = rbuf_read(&sniff_buffer); |
Kévin Redon | 709a431 | 2018-07-03 16:10:04 +0200 | [diff] [blame] | 947 | /* Convert convention if required */ |
| 948 | if (convention_convert) { |
| 949 | byte = convention_convert_lut[byte]; |
| 950 | } |
| 951 | //TRACE_ERROR_WP(">%02x", byte); |
Kévin Redon | de97fd2 | 2018-07-01 18:23:23 +0200 | [diff] [blame] | 952 | switch (iso_state) { /* Handle byte depending on state */ |
| 953 | case ISO7816_S_RESET: /* During reset we shouldn't receive any data */ |
| 954 | break; |
| 955 | case ISO7816_S_WAIT_ATR: /* After a reset we expect the ATR */ |
| 956 | change_state(ISO7816_S_IN_ATR); /* go to next state */ |
| 957 | case ISO7816_S_IN_ATR: /* More ATR data incoming */ |
| 958 | process_byte_atr(byte); |
| 959 | break; |
| 960 | case ISO7816_S_WAIT_TPDU: /* After the ATR we expect TPDU or PPS data */ |
| 961 | case ISO7816_S_WAIT_PPS_RSP: |
| 962 | if (byte == 0xff) { |
| 963 | if (ISO7816_S_WAIT_PPS_RSP==iso_state) { |
| 964 | change_state(ISO7816_S_IN_PPS_RSP); /* Go to PPS state */ |
| 965 | } else { |
| 966 | change_state(ISO7816_S_IN_PPS_REQ); /* Go to PPS state */ |
| 967 | } |
| 968 | process_byte_pps(byte); |
| 969 | break; |
| 970 | } |
| 971 | case ISO7816_S_IN_TPDU: /* More TPDU data incoming */ |
| 972 | if (ISO7816_S_WAIT_TPDU==iso_state) { |
| 973 | change_state(ISO7816_S_IN_TPDU); |
| 974 | } |
| 975 | process_byte_tpdu(byte); |
| 976 | break; |
| 977 | case ISO7816_S_IN_PPS_REQ: |
| 978 | case ISO7816_S_IN_PPS_RSP: |
| 979 | process_byte_pps(byte); |
| 980 | break; |
| 981 | default: |
| 982 | TRACE_ERROR("Data received in unknown state %u\n\r", iso_state); |
| 983 | } |
| 984 | } |
| 985 | |
| 986 | /* Handle flags */ |
| 987 | if (change_flags) { /* WARNING this is not synced with the data buffer handling */ |
Kévin Redon | 709a431 | 2018-07-03 16:10:04 +0200 | [diff] [blame] | 988 | if (change_flags&SNIFF_CHANGE_FLAG_TIMEOUT_WT) { |
| 989 | /* Use timeout to detect interrupted data transmission */ |
| 990 | switch (iso_state) { |
| 991 | case ISO7816_S_IN_ATR: |
Kévin Redon | 35e8bdf | 2018-07-03 16:15:58 +0200 | [diff] [blame] | 992 | usb_send_atr(false); /* send incomplete ATR to host software using USB */ |
Kévin Redon | 709a431 | 2018-07-03 16:10:04 +0200 | [diff] [blame] | 993 | change_state(ISO7816_S_WAIT_ATR); |
| 994 | break; |
| 995 | case ISO7816_S_IN_TPDU: |
Kévin Redon | 35e8bdf | 2018-07-03 16:15:58 +0200 | [diff] [blame] | 996 | usb_send_tpdu(false); /* send incomplete PPS to host software using USB */ |
Kévin Redon | 709a431 | 2018-07-03 16:10:04 +0200 | [diff] [blame] | 997 | change_state(ISO7816_S_WAIT_TPDU); |
| 998 | break; |
| 999 | case ISO7816_S_IN_PPS_REQ: |
| 1000 | case ISO7816_S_IN_PPS_RSP: |
Kévin Redon | 35e8bdf | 2018-07-03 16:15:58 +0200 | [diff] [blame] | 1001 | usb_send_pps(false); /* send incomplete TPDU to host software using USB */ |
Kévin Redon | 709a431 | 2018-07-03 16:10:04 +0200 | [diff] [blame] | 1002 | change_state(ISO7816_S_WAIT_TPDU); |
| 1003 | break; |
| 1004 | default: |
| 1005 | change_flags &= ~SNIFF_CHANGE_FLAG_TIMEOUT_WT; /* We don't care about the timeout is all other cases */ |
| 1006 | break; |
| 1007 | } |
| 1008 | } |
| 1009 | if (change_flags) { |
| 1010 | usb_send_change(change_flags); /* send timeout to host software over USB */ |
| 1011 | change_flags = 0; /* Reset flags */ |
| 1012 | } |
Kévin Redon | de97fd2 | 2018-07-01 18:23:23 +0200 | [diff] [blame] | 1013 | } |
Christina Quast | fb524b9 | 2015-02-27 13:39:45 +0100 | [diff] [blame] | 1014 | } |
Harald Welte | 2fb5996 | 2016-02-28 12:34:26 +0100 | [diff] [blame] | 1015 | #endif /* HAVE_SNIFFER */ |