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