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