1 | /*
|
---|
2 | protocol_t0.c
|
---|
3 | Handling of ISO 7816 T=0 protocol
|
---|
4 |
|
---|
5 | This file is part of the Unix driver for Towitoko smartcard readers
|
---|
6 | Copyright (C) 2000 Carlos Prados <cprados@yahoo.com>
|
---|
7 |
|
---|
8 | This version is modified by doz21 to work in a special manner ;)
|
---|
9 |
|
---|
10 | This library is free software; you can redistribute it and/or
|
---|
11 | modify it under the terms of the GNU Lesser General Public
|
---|
12 | License as published by the Free Software Foundation; either
|
---|
13 | version 2 of the License, or (at your option) any later version.
|
---|
14 |
|
---|
15 | This library is distributed in the hope that it will be useful,
|
---|
16 | but WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
17 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
---|
18 | Lesser General Public License for more details.
|
---|
19 |
|
---|
20 | You should have received a copy of the GNU Lesser General Public
|
---|
21 | License along with this library; if not, write to the Free Software
|
---|
22 | Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
---|
23 | */
|
---|
24 |
|
---|
25 | #include "../globals.h"
|
---|
26 |
|
---|
27 | #ifdef WITH_CARDREADER
|
---|
28 | #include "../oscam-time.h"
|
---|
29 | #include "icc_async.h"
|
---|
30 | #include "protocol_t0.h"
|
---|
31 | /*
|
---|
32 | * Not exported constants definition
|
---|
33 | */
|
---|
34 |
|
---|
35 | #define OK 0
|
---|
36 | #define ERROR 1
|
---|
37 |
|
---|
38 | #define PROTOCOL_T0_MAX_NULLS 200
|
---|
39 | #define PROTOCOL_T0_DEFAULT_WI 10
|
---|
40 | #define PROTOCOL_T0_MAX_SHORT_COMMAND 260
|
---|
41 | #define PROTOCOL_T0_MAX_SHORT_RESPONSE 258
|
---|
42 |
|
---|
43 | #define PROTOCOL_T14_MAX_NULLS 200
|
---|
44 | #define PROTOCOL_T14_DEFAULT_WI 10
|
---|
45 | #define PROTOCOL_T14_MAX_SHORT_COMMAND 260
|
---|
46 | #define PROTOCOL_T14_MAX_SHORT_RESPONSE 258
|
---|
47 |
|
---|
48 | /* Types of APDU's */
|
---|
49 | #define APDU_CASE_1 0x0001 /* Nor send neither receive data */
|
---|
50 | #define APDU_CASE_2S 0x0002 /* Receive data (1..256) */
|
---|
51 | #define APDU_CASE_3S 0x0003 /* Send data (1..255) */
|
---|
52 | #define APDU_CASE_4S 0x0004 /* Send data (1..255) and receive data (1..256) */
|
---|
53 | #define APDU_CASE_2E 0x0102 /* Receive data (1..65536) */
|
---|
54 | #define APDU_CASE_3E 0x0103 /* Send data (1..65535) */
|
---|
55 | #define APDU_CASE_4E 0x0104 /* Send data (1..65535) and receive data (1..65536) */
|
---|
56 | #define APDU_MALFORMED 5 /* Malformed APDU */
|
---|
57 |
|
---|
58 | /* Timings in ATR are not used in T=0 cards */
|
---|
59 | /* #undef PROTOCOL_T0_USE_DEFAULT_TIMINGS */
|
---|
60 |
|
---|
61 | /*
|
---|
62 | * Not exported functions declaration
|
---|
63 | */
|
---|
64 |
|
---|
65 | static int32_t Protocol_T0_Case2E (struct s_reader * reader, unsigned char * command, uint16_t command_len, unsigned char * rsp, uint16_t * lr);
|
---|
66 |
|
---|
67 | static int32_t Protocol_T0_Case3E (struct s_reader * reader, unsigned char * command, unsigned char * rsp, uint16_t * lr);
|
---|
68 |
|
---|
69 | static int32_t Protocol_T0_Case4E (struct s_reader * reader, unsigned char * command, uint16_t command_len, unsigned char * rsp, uint16_t * lr);
|
---|
70 |
|
---|
71 | static int32_t Protocol_T0_ExchangeTPDU (struct s_reader *reader, unsigned char * command, uint16_t command_len, unsigned char * rsp, uint16_t * lr);
|
---|
72 |
|
---|
73 | static int32_t APDU_Cmd_Case (unsigned char * command, uint16_t command_len)
|
---|
74 | {
|
---|
75 | unsigned char B1;
|
---|
76 | uint16_t B2B3;
|
---|
77 | uint32_t L;
|
---|
78 | int32_t res;
|
---|
79 |
|
---|
80 | /* Calculate length of body */
|
---|
81 | L = MAX(command_len - 4, 0);
|
---|
82 |
|
---|
83 | /* Case 1 */
|
---|
84 | if (L == 0)
|
---|
85 | res = APDU_CASE_1;
|
---|
86 | else {
|
---|
87 | /* Get first byte of body */
|
---|
88 | B1 = command[4];
|
---|
89 |
|
---|
90 | if ((B1 != 0) && (L == (uint32_t)B1 + 1))
|
---|
91 | res = APDU_CASE_2S;
|
---|
92 | else if (L == 1)
|
---|
93 | res = APDU_CASE_3S;
|
---|
94 | else if ((B1 != 0) && (L == (uint32_t)B1 + 2))
|
---|
95 | res = APDU_CASE_4S;
|
---|
96 | else if ((B1 == 0) && (L>2)) {
|
---|
97 | /* Get second and third byte of body */
|
---|
98 | B2B3 = (((uint16_t)(command[5]) << 8) | command[6]);
|
---|
99 |
|
---|
100 | if ((B2B3 != 0) && (L == (uint32_t)B2B3 + 3))
|
---|
101 | res = APDU_CASE_2E;
|
---|
102 | else if (L == 3)
|
---|
103 | res = APDU_CASE_3E;
|
---|
104 | else if ((B2B3 != 0) && (L == (uint32_t)B2B3 + 5))
|
---|
105 | res = APDU_CASE_4E;
|
---|
106 | else
|
---|
107 | res = APDU_MALFORMED;
|
---|
108 | }
|
---|
109 | else
|
---|
110 | res = APDU_MALFORMED;
|
---|
111 | }
|
---|
112 | return res;
|
---|
113 | }
|
---|
114 |
|
---|
115 | /*
|
---|
116 | * Exported funtions definition
|
---|
117 | */
|
---|
118 |
|
---|
119 | int32_t Protocol_T0_Command (struct s_reader * reader, unsigned char * command, uint16_t command_len, unsigned char * rsp, uint16_t * lr)
|
---|
120 | {
|
---|
121 | *lr = 0; //will be returned in case of error
|
---|
122 | if (command_len < 5) //APDU_CASE_1 or malformed
|
---|
123 | return ERROR;
|
---|
124 | int32_t cmd_case = APDU_Cmd_Case (command, command_len);
|
---|
125 | switch (cmd_case) {
|
---|
126 | case APDU_CASE_2E:
|
---|
127 | return Protocol_T0_Case2E (reader, command, command_len, rsp, lr);
|
---|
128 | case APDU_CASE_3E:
|
---|
129 | return Protocol_T0_Case3E (reader, command, rsp, lr);
|
---|
130 | case APDU_CASE_4E:
|
---|
131 | return Protocol_T0_Case4E (reader, command, command_len, rsp, lr);
|
---|
132 | case APDU_CASE_4S:
|
---|
133 | command_len--; //FIXME this should change 4S to 2S/3S command
|
---|
134 | case APDU_CASE_2S:
|
---|
135 | case APDU_CASE_3S:
|
---|
136 | return Protocol_T0_ExchangeTPDU(reader, command, command_len, rsp, lr);
|
---|
137 | default:
|
---|
138 | rdr_debug_mask(reader, D_IFD, "Protocol: T=0: Invalid APDU");
|
---|
139 | return ERROR;
|
---|
140 | }
|
---|
141 | }
|
---|
142 |
|
---|
143 | /*
|
---|
144 | * Not exported functions definition
|
---|
145 | */
|
---|
146 |
|
---|
147 |
|
---|
148 | static int32_t Protocol_T0_Case2E (struct s_reader * reader, unsigned char * command, uint16_t command_len, unsigned char * rsp, uint16_t * lr)
|
---|
149 | {
|
---|
150 | unsigned char buffer[PROTOCOL_T0_MAX_SHORT_COMMAND];
|
---|
151 | unsigned char tpdu_rsp[CTA_RES_LEN];
|
---|
152 | uint16_t tpdu_lr = 0;
|
---|
153 | uint32_t i;
|
---|
154 |
|
---|
155 | uint32_t Lc = (((uint32_t)(command[5]) << 8) | command[6]);
|
---|
156 | if (Lc < 256)
|
---|
157 | {
|
---|
158 | /* MAP APDU onto command TPDU */
|
---|
159 | memcpy(buffer, command, 4);
|
---|
160 | buffer[4] = (unsigned char) Lc;
|
---|
161 | memcpy (buffer + 5, command + 7, buffer[4]);
|
---|
162 | return Protocol_T0_ExchangeTPDU(reader, buffer, buffer[4] + 5, rsp, lr);
|
---|
163 | }
|
---|
164 |
|
---|
165 | /* Prepare envelope TPDU */
|
---|
166 | buffer[0] = command[0];
|
---|
167 | buffer[1] = 0xC2;
|
---|
168 | buffer[2] = 0x00;
|
---|
169 | buffer[3] = 0x00;
|
---|
170 |
|
---|
171 | for (i = 0; i < command_len; i += buffer[4])
|
---|
172 | {
|
---|
173 | /* Create envelope command TPDU */
|
---|
174 | buffer[4] = MIN (255, command_len - i);
|
---|
175 | memcpy (buffer + 5, command + i, buffer[4]);
|
---|
176 | call (Protocol_T0_ExchangeTPDU(reader, buffer, buffer[4] + 5, tpdu_rsp, &tpdu_lr));
|
---|
177 | /* Card does support envelope command */
|
---|
178 | if (tpdu_rsp[tpdu_lr - 2] == 0x90)
|
---|
179 | {
|
---|
180 | /* This is not the last segment */
|
---|
181 | if (buffer[4] + i < command_len)
|
---|
182 | tpdu_lr = 0;
|
---|
183 | else {
|
---|
184 | memcpy(rsp, tpdu_rsp, tpdu_lr); // Map response TPDU onto APDU
|
---|
185 | *lr = tpdu_lr;
|
---|
186 | }
|
---|
187 | }
|
---|
188 | else /* Card does not support envelope command or error */
|
---|
189 | {
|
---|
190 | memcpy(rsp, tpdu_rsp, tpdu_lr); // Map response TPDU onto APDU
|
---|
191 | *lr = tpdu_lr;
|
---|
192 | break;
|
---|
193 | }
|
---|
194 | }
|
---|
195 | return OK;
|
---|
196 | }
|
---|
197 |
|
---|
198 |
|
---|
199 | static int32_t Protocol_T0_Case3E (struct s_reader * reader, unsigned char * command, unsigned char * rsp, uint16_t * lr)
|
---|
200 | {
|
---|
201 | int32_t ret;
|
---|
202 | unsigned char buffer[5];
|
---|
203 | unsigned char tpdu_rsp[CTA_RES_LEN];
|
---|
204 | uint16_t tpdu_lr = 0;
|
---|
205 | int32_t Lm, Lx;
|
---|
206 |
|
---|
207 | uint32_t Le = ((((uint32_t)(command[5]) << 8) | command[6]) == 0 ? 65536 : (((uint32_t)(command[5]) << 8) | command[6]));
|
---|
208 | memcpy(buffer, command, 4);//Map APDU command onto TPDU
|
---|
209 |
|
---|
210 | if (Le <= 256)
|
---|
211 | {
|
---|
212 | buffer[4] = (unsigned char)Le;
|
---|
213 | return Protocol_T0_ExchangeTPDU(reader, buffer, 5, rsp, lr); //this was Case3S !!!
|
---|
214 | }
|
---|
215 |
|
---|
216 | /* Map APDU onto command TPDU */
|
---|
217 | buffer[4] = 0x00;
|
---|
218 | call (Protocol_T0_ExchangeTPDU(reader, buffer, 5 , tpdu_rsp, &tpdu_lr));
|
---|
219 |
|
---|
220 | if (tpdu_rsp[tpdu_lr - 2] == 0x6C) {/* Le not accepted, La indicated */
|
---|
221 | /* Map command APDU onto TPDU */
|
---|
222 | memcpy (buffer, command, 4);
|
---|
223 | buffer[4] = tpdu_rsp[tpdu_lr - 1];
|
---|
224 |
|
---|
225 | /* Delete response TPDU */
|
---|
226 | tpdu_lr = 0;
|
---|
227 |
|
---|
228 | return Protocol_T0_ExchangeTPDU(reader, buffer, 5, rsp, lr); //Reissue command
|
---|
229 | }
|
---|
230 |
|
---|
231 | memcpy(rsp, tpdu_rsp, tpdu_lr);//Map response TPDU onto APDU without change , also for SW1 = 0x67
|
---|
232 | *lr = tpdu_lr;
|
---|
233 | ret = OK;
|
---|
234 | if (tpdu_rsp[tpdu_lr - 2] == 0x61) {/* Command processed, Lx indicated */
|
---|
235 | Lx = (tpdu_rsp[tpdu_lr - 1] == 0x00) ? 256 : tpdu_rsp[tpdu_lr - 1];
|
---|
236 | Lm = Le - (*lr - 2);
|
---|
237 |
|
---|
238 | /* Prepare Get Response TPDU */
|
---|
239 | buffer[0] = command[0];
|
---|
240 | buffer[1] = 0xC0;
|
---|
241 | buffer[2] = 0x00;
|
---|
242 | buffer[3] = 0x00;
|
---|
243 |
|
---|
244 | while (Lm > 0)
|
---|
245 | {
|
---|
246 | buffer[4] = (unsigned char) MIN (Lm, Lx);
|
---|
247 | call (Protocol_T0_ExchangeTPDU(reader, buffer, 5, tpdu_rsp, &tpdu_lr));
|
---|
248 |
|
---|
249 | /* Append response TPDU to APDU */
|
---|
250 | if ((*lr + tpdu_lr) > CTA_RES_LEN) {
|
---|
251 | rdr_log(reader, "TPDU Append error, new length %i exceeds max length %i", *lr + tpdu_lr, CTA_RES_LEN);
|
---|
252 | return ERROR;
|
---|
253 | }
|
---|
254 | memcpy (rsp + (*lr - 2), tpdu_rsp, tpdu_lr);
|
---|
255 | *lr += tpdu_lr;
|
---|
256 |
|
---|
257 | /* Delete response TPDU */
|
---|
258 | tpdu_lr = 0;
|
---|
259 |
|
---|
260 | Lm = Le - (*lr - 2);
|
---|
261 | }/* Lm == 0 */
|
---|
262 | }
|
---|
263 | return ret;
|
---|
264 | }
|
---|
265 |
|
---|
266 |
|
---|
267 | static int32_t Protocol_T0_Case4E (struct s_reader * reader, unsigned char * command, uint16_t command_len, unsigned char * rsp, uint16_t * lr)
|
---|
268 | {
|
---|
269 | int32_t ret;
|
---|
270 | unsigned char buffer[PROTOCOL_T0_MAX_SHORT_COMMAND];
|
---|
271 | unsigned char tpdu_rsp[CTA_RES_LEN];
|
---|
272 | uint16_t tpdu_lr = 0;
|
---|
273 | int32_t Le;
|
---|
274 |
|
---|
275 | uint32_t Lc = (((uint32_t)(command[5]) << 8) | command[6]);
|
---|
276 | /* 4E1 */
|
---|
277 | if (Lc < 256) {
|
---|
278 | /* Map APDU onto command TPDU */
|
---|
279 | memcpy(buffer,command,4);
|
---|
280 | buffer[4] = (unsigned char) Lc;
|
---|
281 | memcpy (buffer + 5, command, buffer[4]);
|
---|
282 | ret = Protocol_T0_ExchangeTPDU(reader, buffer, buffer[4] + 5, tpdu_rsp, &tpdu_lr);
|
---|
283 | }
|
---|
284 | else /* 4E2 */
|
---|
285 | ret = Protocol_T0_Case2E (reader, command, command_len, tpdu_rsp, &tpdu_lr);
|
---|
286 |
|
---|
287 | /* 4E1 a) b) and c) */
|
---|
288 | if (ret == OK)
|
---|
289 | {
|
---|
290 | Le = ((((uint32_t)(command[command_len - 2]) << 8) | command[command_len - 1]) == 0 ? 65536 : (((uint32_t)(command[command_len - 2]) << 8) | command[command_len - 1]));
|
---|
291 | if (tpdu_rsp[tpdu_lr - 2] == 0x61)
|
---|
292 | {
|
---|
293 | /* Lm == (Le - APDU_Rsp_RawLen (tpdu_rsp)) == 0 */
|
---|
294 | if (tpdu_rsp[tpdu_lr - 1] != 0x00)
|
---|
295 | Le = MIN(tpdu_rsp[tpdu_lr - 1], Le);
|
---|
296 |
|
---|
297 | /* Delete response TPDU */
|
---|
298 | tpdu_lr = 0;
|
---|
299 |
|
---|
300 | /* Prepare extended Get Response APDU command */
|
---|
301 | buffer[0] = command[0];
|
---|
302 | buffer[1] = 0xC0;
|
---|
303 | buffer[2] = 0x00;
|
---|
304 | buffer[3] = 0x00;
|
---|
305 | buffer[4] = 0x00; /* B1 = 0x00 */
|
---|
306 | buffer[5] = (unsigned char) (Le >> 8); /* B2 = BL-1 */
|
---|
307 | buffer[6] = (unsigned char) (Le & 0x00FF); /* B3 = BL */
|
---|
308 | ret = Protocol_T0_Case3E (reader, buffer, rsp, lr);
|
---|
309 | }
|
---|
310 | else if ((tpdu_rsp[tpdu_lr - 2] & 0xF0) == 0x60)
|
---|
311 | {
|
---|
312 | /* Map response TPDU onto APDU without change */
|
---|
313 | memcpy(rsp, tpdu_rsp, tpdu_lr);
|
---|
314 | *lr = tpdu_lr;
|
---|
315 | }
|
---|
316 | else
|
---|
317 | {
|
---|
318 | /* Delete response TPDU */
|
---|
319 | tpdu_lr = 0;
|
---|
320 |
|
---|
321 | /* Prepare extended Get Response APDU command */
|
---|
322 | buffer[0] = command[0];
|
---|
323 | buffer[1] = 0xC0;
|
---|
324 | buffer[2] = 0x00;
|
---|
325 | buffer[3] = 0x00;
|
---|
326 | buffer[4] = 0x00; /* B1 = 0x00 */
|
---|
327 | buffer[5] = (unsigned char) Le >> 8; /* B2 = BL-1 */
|
---|
328 | buffer[6] = (unsigned char) Le & 0x00FF; /* B3 = BL */
|
---|
329 | ret = Protocol_T0_Case3E (reader, buffer, rsp, lr);
|
---|
330 | }
|
---|
331 | }
|
---|
332 | return ret;
|
---|
333 | }
|
---|
334 |
|
---|
335 |
|
---|
336 | static int32_t Protocol_T0_ExchangeTPDU (struct s_reader *reader, unsigned char * command, uint16_t command_len, unsigned char * rsp, uint16_t * lr)
|
---|
337 | {
|
---|
338 | unsigned char buffer[PROTOCOL_T0_MAX_SHORT_RESPONSE];
|
---|
339 | unsigned char *data;
|
---|
340 | int32_t Lc, Le, sent, recved;
|
---|
341 | int32_t nulls, cmd_case;
|
---|
342 | int32_t timeout;
|
---|
343 | *lr = 0; //in case of error this will be returned
|
---|
344 |
|
---|
345 | cmd_case = APDU_Cmd_Case (command, command_len);
|
---|
346 | switch (cmd_case) {
|
---|
347 | case APDU_CASE_2S:
|
---|
348 | Lc = command[4];
|
---|
349 | Le = 0;
|
---|
350 | data = command + 5;
|
---|
351 | break;
|
---|
352 | case APDU_CASE_3S:
|
---|
353 | Lc = 0;
|
---|
354 | Le = command[4];
|
---|
355 | data = NULL;
|
---|
356 | break;
|
---|
357 | default:
|
---|
358 | rdr_debug_mask(reader, D_TRACE, "ERROR: invalid cmd_case = %i in Protocol_T0_ExchangeTPDU",cmd_case);
|
---|
359 | return ERROR;
|
---|
360 | }
|
---|
361 | timeout = ICC_Async_GetTimings (reader, reader->char_delay); // we are going to send: char delay timeout
|
---|
362 | if (ICC_Async_Transmit (reader, 5, command, 0, timeout)!=OK) return ERROR; //Send header bytes
|
---|
363 |
|
---|
364 | /* Initialise counters */
|
---|
365 | nulls = 0;
|
---|
366 | sent = 0;
|
---|
367 | recved = 0;
|
---|
368 |
|
---|
369 | /*
|
---|
370 | * Let's be a bit paranoid with buffer sizes within this loop
|
---|
371 | * so it doesn't overflow reception and transmission buffers
|
---|
372 | * if card does not strictly respect the protocol
|
---|
373 | */
|
---|
374 |
|
---|
375 | while (recved < PROTOCOL_T0_MAX_SHORT_RESPONSE)
|
---|
376 | {
|
---|
377 | timeout = ICC_Async_GetTimings (reader, reader->read_timeout); // we are going to receive: WWT timeout
|
---|
378 | if (ICC_Async_Receive (reader, 1, buffer + recved, 0, timeout) != OK) return ERROR;//Read one procedure byte
|
---|
379 |
|
---|
380 | /* NULL byte received */
|
---|
381 | if (buffer[recved] == 0x60) {
|
---|
382 | nulls++;
|
---|
383 | if (nulls >= PROTOCOL_T0_MAX_NULLS) { //Maximum number of nulls reached
|
---|
384 | rdr_debug_mask(reader, D_TRACE, "ERROR: %s: Maximum number of nulls reached: %d", __func__, nulls);
|
---|
385 | return ERROR;
|
---|
386 | }
|
---|
387 | }
|
---|
388 | else if ((buffer[recved] & 0xF0) == 0x60 || (buffer[recved] & 0xF0) == 0x90) /* SW1 byte received */
|
---|
389 | {
|
---|
390 | rdr_debug_mask(reader, D_TRACE, "SW1: %02X", buffer[recved]&0xf0);
|
---|
391 | recved++;
|
---|
392 | if (recved >= PROTOCOL_T0_MAX_SHORT_RESPONSE) {
|
---|
393 | rdr_debug_mask(reader, D_TRACE, "ERROR: %s: Maximum short response exceeded: %d", __func__, recved);
|
---|
394 | return ERROR;
|
---|
395 | }
|
---|
396 | timeout = ICC_Async_GetTimings (reader, reader->read_timeout); // we are going to receive: WWT timeout
|
---|
397 | if (ICC_Async_Receive (reader, 1, buffer + recved, 0, timeout) !=OK) return ERROR; //Read SW2 byte
|
---|
398 | rdr_debug_mask(reader, D_TRACE, "SW2: %02X", buffer[recved]&0xf0);
|
---|
399 | recved++;
|
---|
400 | break;
|
---|
401 | }
|
---|
402 | else if ((buffer[recved] & 0x0E) == (command[1] & 0x0E)) /* ACK byte received */
|
---|
403 | {//printf("ack\n");
|
---|
404 | /* Reset null's counter */
|
---|
405 | nulls = 0;
|
---|
406 |
|
---|
407 | /* Case 2 command: send data */
|
---|
408 | if (cmd_case == APDU_CASE_2S) {
|
---|
409 | if (sent >= Lc) {
|
---|
410 | rdr_debug_mask(reader, D_TRACE, "ERROR: %s: ACK byte: sent=%d exceeds Lc=%d", __func__, sent, Lc);
|
---|
411 | return ERROR;
|
---|
412 | }
|
---|
413 | timeout = ICC_Async_GetTimings (reader, reader->char_delay); // we are going to send: char delay timeout
|
---|
414 | if(ICC_Async_Transmit(reader, MAX (Lc - sent, 0), data + sent, 0, timeout)!=OK) return ERROR; /* Send remaining data bytes */
|
---|
415 | sent = Lc;
|
---|
416 | continue;
|
---|
417 | }
|
---|
418 | else /* Case 3 command: receive data */
|
---|
419 | {
|
---|
420 | if (recved >= PROTOCOL_T0_MAX_SHORT_RESPONSE) {
|
---|
421 | rdr_debug_mask(reader, D_TRACE, "ERROR: %s: Case 3 ACK - maximum short response exceeded: %d", __func__, recved);
|
---|
422 | return ERROR;
|
---|
423 | }
|
---|
424 |
|
---|
425 | /*
|
---|
426 | * Le <= PROTOCOL_T0_MAX_SHORT_RESPONSE - 2 for short commands
|
---|
427 | */
|
---|
428 |
|
---|
429 | /* Read remaining data bytes */
|
---|
430 | timeout = ICC_Async_GetTimings (reader, reader->read_timeout); // we are going to receive: WWT timeout
|
---|
431 | if (ICC_Async_Receive(reader, MAX (Le - recved, 0), buffer + recved, 0, timeout) != OK) return ERROR;
|
---|
432 | recved = Le;
|
---|
433 | continue;
|
---|
434 | }
|
---|
435 | }
|
---|
436 | else if ((buffer[recved] & 0x0E) == ((~command[1]) & 0x0E)) /* ~ACK byte received */
|
---|
437 | {//printf("~ack\n");
|
---|
438 | nulls = 0; //Reset null's counter
|
---|
439 |
|
---|
440 | /* Case 2 command: send data */
|
---|
441 | if (cmd_case == APDU_CASE_2S) {
|
---|
442 | if (sent >= Lc) {
|
---|
443 | rdr_debug_mask(reader, D_TRACE, "ERROR: %s: ~ACK byte: sent=%d exceeds Lc=%d", __func__, sent, Lc);
|
---|
444 | return ERROR;
|
---|
445 | }
|
---|
446 | timeout = ICC_Async_GetTimings (reader, reader->char_delay); // we are going to send: char delay timeout
|
---|
447 | if(ICC_Async_Transmit (reader, 1, data + sent, 0, timeout)!=OK) return ERROR; //Send next data byte
|
---|
448 | sent++;
|
---|
449 | continue;
|
---|
450 | }
|
---|
451 | else {/* Case 3 command: receive data */
|
---|
452 | if (recved >= PROTOCOL_T0_MAX_SHORT_RESPONSE) {
|
---|
453 | rdr_debug_mask(reader, D_TRACE, "ERROR: %s: Case 3 ~ACK - maximum short response exceeded: %d", __func__, recved);
|
---|
454 | return ERROR;
|
---|
455 | }
|
---|
456 | timeout = ICC_Async_GetTimings (reader, reader->read_timeout); // we are going to receive: WWT timeout
|
---|
457 | if(ICC_Async_Receive (reader, 1, buffer + recved, 0, timeout)!=OK) return ERROR;//Read next data byte
|
---|
458 | recved++;
|
---|
459 | continue;
|
---|
460 | }
|
---|
461 | }
|
---|
462 | else { /* Anything else received */
|
---|
463 | rdr_debug_mask(reader, D_TRACE, "ERROR: %s: Received unexpected character: %02X", __func__, buffer[recved]);
|
---|
464 | return ERROR;
|
---|
465 | }
|
---|
466 | }//while
|
---|
467 |
|
---|
468 | memcpy(rsp, buffer, recved);
|
---|
469 | *lr = recved;
|
---|
470 | return OK;
|
---|
471 | }
|
---|
472 |
|
---|
473 | int32_t Protocol_T14_ExchangeTPDU (struct s_reader *reader, unsigned char * cmd_raw, uint16_t command_len, unsigned char * rsp, uint16_t * lr)
|
---|
474 | {
|
---|
475 | unsigned char buffer[PROTOCOL_T14_MAX_SHORT_RESPONSE];
|
---|
476 | int32_t recved;
|
---|
477 | int32_t cmd_case;
|
---|
478 | int32_t timeout;
|
---|
479 | unsigned char ixor = 0x3E;
|
---|
480 | unsigned char ixor1 = 0x3F;
|
---|
481 | int32_t i;
|
---|
482 | int32_t cmd_len = (int32_t) command_len;
|
---|
483 | *lr = 0; //in case of error this is returned
|
---|
484 |
|
---|
485 | /* Parse APDU */
|
---|
486 | cmd_case = APDU_Cmd_Case (cmd_raw, cmd_len);
|
---|
487 | for(i=0; i<cmd_len; i++)
|
---|
488 | ixor^=cmd_raw[i];
|
---|
489 |
|
---|
490 | /* Check case of command */
|
---|
491 | if ((cmd_case != APDU_CASE_2S) && (cmd_case != APDU_CASE_3S)) {
|
---|
492 | rdr_debug_mask(reader, D_TRACE, "ERROR: invalid cmd_case = %i in Protocol_T14_ExchangeTPDU", cmd_case);
|
---|
493 | return ERROR;
|
---|
494 | }
|
---|
495 |
|
---|
496 | buffer[0] = 0x01; //send 0x01 byte
|
---|
497 | memcpy(buffer+1, cmd_raw, cmd_len); // apdu
|
---|
498 | buffer[cmd_len+1] = ixor; // xor byte
|
---|
499 |
|
---|
500 | /* Send apdu */
|
---|
501 | timeout = ICC_Async_GetTimings (reader, reader->char_delay); // we are going to send: char delay timeout
|
---|
502 | if(ICC_Async_Transmit (reader, cmd_len+2, buffer, 0, timeout)!=OK) return ERROR;//send apdu
|
---|
503 | if(cmd_raw[0] == 0x02 && cmd_raw[1] == 0x09) cs_sleepms(2500); //FIXME why wait? -> needed for init on overclocked T14 cards
|
---|
504 |
|
---|
505 | timeout = ICC_Async_GetTimings (reader, reader->read_timeout); // we are going to receive: WWT timeout
|
---|
506 | if(ICC_Async_Receive (reader, 8, buffer, 0, timeout)!=OK) return ERROR; //Read one procedure byte
|
---|
507 | recved = (int32_t)buffer[7];
|
---|
508 | if(recved){
|
---|
509 | timeout = ICC_Async_GetTimings (reader, reader->read_timeout); // we are going to receive: WWT timeout
|
---|
510 | if (ICC_Async_Receive (reader, recved, buffer + 8, 0 , timeout)!=OK) return ERROR;
|
---|
511 | }
|
---|
512 | timeout = ICC_Async_GetTimings (reader, reader->read_timeout); // we are going to receive: WWT timeout
|
---|
513 | if(ICC_Async_Receive (reader, 1, &ixor, 0, timeout)!=OK) return ERROR;
|
---|
514 | for(i=0; i<8+recved; i++)
|
---|
515 | ixor1^=buffer[i];
|
---|
516 | if(ixor1 != ixor) {
|
---|
517 | rdr_debug_mask(reader, D_TRACE, "ERROR: invalid checksum = %02X expected %02X", ixor1, ixor);
|
---|
518 | return ERROR;
|
---|
519 | }
|
---|
520 | memcpy(buffer + 8 + recved, buffer + 2, 2);
|
---|
521 | *lr = recved + 2;
|
---|
522 | memcpy(rsp, buffer + 8, *lr);
|
---|
523 | return OK;
|
---|
524 | }
|
---|
525 | #endif
|
---|