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 "defines.h"
|
---|
26 | #include <stdio.h>
|
---|
27 | #include <stdlib.h>
|
---|
28 | #include <string.h>
|
---|
29 | #include <time.h>
|
---|
30 |
|
---|
31 | #include "../globals.h"
|
---|
32 |
|
---|
33 | #include "protocol_t0.h"
|
---|
34 | #include "atr.h"
|
---|
35 |
|
---|
36 | /*
|
---|
37 | * Not exported constants definition
|
---|
38 | */
|
---|
39 |
|
---|
40 | #define PROTOCOL_T0_MAX_NULLS 200
|
---|
41 | #define PROTOCOL_T0_DEFAULT_WI 10
|
---|
42 | #define PROTOCOL_T0_MAX_SHORT_COMMAND 260
|
---|
43 | #define PROTOCOL_T0_MAX_SHORT_RESPONSE 258
|
---|
44 |
|
---|
45 | #define PROTOCOL_T14_MAX_NULLS 200
|
---|
46 | #define PROTOCOL_T14_DEFAULT_WI 10
|
---|
47 | #define PROTOCOL_T14_MAX_SHORT_COMMAND 260
|
---|
48 | #define PROTOCOL_T14_MAX_SHORT_RESPONSE 258
|
---|
49 |
|
---|
50 | /*
|
---|
51 | * Not exported functions declaration
|
---|
52 | */
|
---|
53 |
|
---|
54 | static int Protocol_T0_Case1 (APDU_Cmd * cmd, APDU_Rsp ** rsp);
|
---|
55 |
|
---|
56 | static int Protocol_T0_Case2S (APDU_Cmd * cmd, APDU_Rsp ** rsp);
|
---|
57 | static int Protocol_T14_Case2S (APDU_Cmd * cmd, APDU_Rsp ** rsp);
|
---|
58 |
|
---|
59 | static int Protocol_T0_Case3S (APDU_Cmd * cmd, APDU_Rsp ** rsp);
|
---|
60 | static int Protocol_T14_Case3S (APDU_Cmd * cmd, APDU_Rsp ** rsp);
|
---|
61 |
|
---|
62 | static int Protocol_T0_Case4S (APDU_Cmd * cmd, APDU_Rsp ** rsp);
|
---|
63 |
|
---|
64 | static int Protocol_T0_Case2E (APDU_Cmd * cmd, APDU_Rsp ** rsp);
|
---|
65 |
|
---|
66 | static int Protocol_T0_Case3E (APDU_Cmd * cmd, APDU_Rsp ** rsp);
|
---|
67 |
|
---|
68 | static int Protocol_T0_Case4E (APDU_Cmd * cmd, APDU_Rsp ** rsp);
|
---|
69 |
|
---|
70 | static int Protocol_T0_ExchangeTPDU (APDU_Cmd * cmd, APDU_Rsp ** rsp);
|
---|
71 | static int Protocol_T14_ExchangeTPDU (APDU_Cmd * cmd, APDU_Rsp ** rsp);
|
---|
72 |
|
---|
73 | /*
|
---|
74 | * Exproted funtions definition
|
---|
75 | */
|
---|
76 |
|
---|
77 | int Protocol_T0_Init (PPS_ProtocolParameters * params)
|
---|
78 | {
|
---|
79 | BYTE wi;
|
---|
80 |
|
---|
81 | /* Integer value WI = TC2, by default 10 */
|
---|
82 | #ifndef PROTOCOL_T0_USE_DEFAULT_TIMINGS
|
---|
83 | if (ATR_GetInterfaceByte (atr, 2, ATR_INTERFACE_BYTE_TC, &(wi)) != ATR_OK)
|
---|
84 | #endif
|
---|
85 | wi = PROTOCOL_T0_DEFAULT_WI;
|
---|
86 |
|
---|
87 | /* WWT = 960 * WI * (Fi / f) * 1000 milliseconds */
|
---|
88 | double F = (double) atr_f_table[params->FI];
|
---|
89 | unsigned long wwt = (long unsigned int) (960 * wi * (F / ICC_Async_GetClockRate ()) * 1000);
|
---|
90 |
|
---|
91 | /* Set timings */
|
---|
92 | icc_timings.block_timeout = wwt;
|
---|
93 | icc_timings.char_timeout = wwt;
|
---|
94 | ICC_Async_SetTimings ();
|
---|
95 |
|
---|
96 | #ifdef DEBUG_PROTOCOL
|
---|
97 | printf ("Protocol: T=0: WWT=%d, Clockrate=%lu\n", (int)(wwt),ICC_Async_GetClockRate());
|
---|
98 | #endif
|
---|
99 |
|
---|
100 | return PROTOCOL_T0_OK;
|
---|
101 | }
|
---|
102 |
|
---|
103 | int Protocol_T14_Init (PPS_ProtocolParameters * params)
|
---|
104 | {
|
---|
105 | BYTE wi;
|
---|
106 |
|
---|
107 | /* Integer value WI = TC2, by default 10 */
|
---|
108 | #ifndef PROTOCOL_T14_USE_DEFAULT_TIMINGS
|
---|
109 | if (ATR_GetInterfaceByte (atr, 2, ATR_INTERFACE_BYTE_TC, &(wi)) != ATR_OK)
|
---|
110 | #endif
|
---|
111 | wi = PROTOCOL_T14_DEFAULT_WI;
|
---|
112 |
|
---|
113 | /* WWT = 960 * WI * (Fi / f) * 1000 milliseconds */
|
---|
114 | double F = (double) atr_f_table[params->FI];
|
---|
115 | unsigned long wwt = (long unsigned int) (960 * wi * (F / ICC_Async_GetClockRate ()) * 1000);
|
---|
116 | wwt >>= 1;
|
---|
117 |
|
---|
118 | /* Set timings */
|
---|
119 | icc_timings.block_timeout = wwt;
|
---|
120 | icc_timings.char_timeout = wwt;
|
---|
121 | ICC_Async_SetTimings ();
|
---|
122 |
|
---|
123 | #ifdef DEBUG_PROTOCOL
|
---|
124 | printf ("Protocol: T=14: WWT=%d\n", (int)(wwt));
|
---|
125 | #endif
|
---|
126 |
|
---|
127 | return PROTOCOL_T14_OK;
|
---|
128 | }
|
---|
129 |
|
---|
130 | int Protocol_T0_Command (APDU_Cmd * cmd, APDU_Rsp ** rsp)
|
---|
131 | {
|
---|
132 | int cmd_case, ret;
|
---|
133 |
|
---|
134 | cmd_case = APDU_Cmd_Case (cmd);
|
---|
135 |
|
---|
136 | #ifdef DEBUG_PROTOCOL
|
---|
137 | if (cmd_case != APDU_MALFORMED)
|
---|
138 | printf ("Protocol: T=0 Case %d %s\n", (cmd_case & 0x0F), APDU_CASE_IS_EXTENDED (cmd_case)? "extended": "short");
|
---|
139 | #endif
|
---|
140 |
|
---|
141 | if (cmd_case == APDU_CASE_1)
|
---|
142 | ret = Protocol_T0_Case1 (cmd, rsp);
|
---|
143 | else if (cmd_case == APDU_CASE_2S)
|
---|
144 | ret = Protocol_T0_Case2S (cmd, rsp);
|
---|
145 | else if (cmd_case == APDU_CASE_3S)
|
---|
146 | ret = Protocol_T0_Case3S (cmd, rsp);
|
---|
147 | else if (cmd_case == APDU_CASE_4S)
|
---|
148 | ret = Protocol_T0_Case4S (cmd, rsp);
|
---|
149 | else if (cmd_case == APDU_CASE_2E)
|
---|
150 | ret = Protocol_T0_Case2E (cmd, rsp);
|
---|
151 | else if (cmd_case == APDU_CASE_3E)
|
---|
152 | ret = Protocol_T0_Case3E (cmd, rsp);
|
---|
153 | else if (cmd_case == APDU_CASE_4E)
|
---|
154 | ret = Protocol_T0_Case4E (cmd, rsp);
|
---|
155 | else
|
---|
156 | {
|
---|
157 | #ifdef DEBUG_PROTOCOL
|
---|
158 | printf ("Protocol: T=0: Invalid APDU\n");
|
---|
159 | #endif
|
---|
160 | ret = PROTOCOL_T0_ERROR;
|
---|
161 | }
|
---|
162 |
|
---|
163 | return ret;
|
---|
164 | }
|
---|
165 |
|
---|
166 | int Protocol_T14_Command (APDU_Cmd * cmd, APDU_Rsp ** rsp)
|
---|
167 | {
|
---|
168 | int cmd_case, ret;
|
---|
169 |
|
---|
170 | cmd_case = APDU_Cmd_Case (cmd);
|
---|
171 |
|
---|
172 | #ifdef DEBUG_PROTOCOL
|
---|
173 | if (cmd_case != APDU_MALFORMED)
|
---|
174 | printf ("Protocol: T=14 Case %d %s\n", (cmd_case & 0x0F), APDU_CASE_IS_EXTENDED (cmd_case)? "extended": "short");
|
---|
175 | #endif
|
---|
176 |
|
---|
177 | if (cmd_case == APDU_CASE_2S)
|
---|
178 | {
|
---|
179 | ret = Protocol_T14_Case2S (cmd, rsp);
|
---|
180 | }
|
---|
181 | else if (cmd_case == APDU_CASE_3S)
|
---|
182 | {
|
---|
183 | ret = Protocol_T14_Case3S (cmd, rsp);
|
---|
184 | }
|
---|
185 | else
|
---|
186 | {
|
---|
187 | #ifdef DEBUG_PROTOCOL
|
---|
188 | printf ("Protocol: T=14: Invalid APDU\n");
|
---|
189 | #endif
|
---|
190 | ret = PROTOCOL_T14_ERROR;
|
---|
191 | }
|
---|
192 |
|
---|
193 | return ret;
|
---|
194 | }
|
---|
195 |
|
---|
196 | /*
|
---|
197 | * Not exported functions definition
|
---|
198 | */
|
---|
199 |
|
---|
200 | static int Protocol_T0_Case1 (APDU_Cmd * cmd, APDU_Rsp ** rsp)
|
---|
201 | {
|
---|
202 | int ret;
|
---|
203 | BYTE buffer[5];
|
---|
204 | APDU_Cmd *tpdu_cmd;
|
---|
205 |
|
---|
206 | /* Map command APDU onto TPDU */
|
---|
207 | memcpy (buffer, APDU_Cmd_Raw (cmd), 4);
|
---|
208 | buffer[4] = 0x00;
|
---|
209 |
|
---|
210 | tpdu_cmd = APDU_Cmd_New (buffer, 5);
|
---|
211 |
|
---|
212 | /* Send command TPDU */
|
---|
213 | ret = Protocol_T0_ExchangeTPDU(tpdu_cmd, rsp);
|
---|
214 |
|
---|
215 | /* Delete command TPDU */
|
---|
216 | APDU_Cmd_Delete (tpdu_cmd);
|
---|
217 |
|
---|
218 | return ret;
|
---|
219 | }
|
---|
220 |
|
---|
221 |
|
---|
222 | static int Protocol_T0_Case2S (APDU_Cmd * cmd, APDU_Rsp ** rsp)
|
---|
223 | {
|
---|
224 | int ret;
|
---|
225 |
|
---|
226 | /* Send command TPDU */
|
---|
227 | ret = Protocol_T0_ExchangeTPDU(cmd, rsp);
|
---|
228 |
|
---|
229 | return ret;
|
---|
230 | }
|
---|
231 |
|
---|
232 | static int Protocol_T14_Case2S (APDU_Cmd * cmd, APDU_Rsp ** rsp)
|
---|
233 | {
|
---|
234 | int ret;
|
---|
235 |
|
---|
236 | /* Send command TPDU */
|
---|
237 | ret = Protocol_T14_ExchangeTPDU(cmd, rsp);
|
---|
238 |
|
---|
239 | return ret;
|
---|
240 | }
|
---|
241 |
|
---|
242 | static int Protocol_T0_Case3S (APDU_Cmd * cmd, APDU_Rsp ** rsp)
|
---|
243 | {
|
---|
244 | int ret;
|
---|
245 | APDU_Rsp *tpdu_rsp;
|
---|
246 | #ifdef PROTOCOL_T0_ISO
|
---|
247 | BYTE buffer[5];
|
---|
248 | APDU_Cmd *tpdu_cmd;
|
---|
249 | #endif
|
---|
250 |
|
---|
251 | /* Send command TPDU */
|
---|
252 | ret = Protocol_T0_ExchangeTPDU(cmd, (&tpdu_rsp));
|
---|
253 |
|
---|
254 | if (ret == PROTOCOL_T0_OK)
|
---|
255 | {
|
---|
256 | #ifdef PROTOCOL_T0_ISO
|
---|
257 | /* Le definitely not accepted */
|
---|
258 | if (APDU_Rsp_SW1 (tpdu_rsp) == 0x67)
|
---|
259 | {
|
---|
260 | /* Map response APDU onto TPDU without change */
|
---|
261 | (*rsp) = tpdu_rsp;
|
---|
262 | }
|
---|
263 | else if (APDU_Rsp_SW1 (tpdu_rsp) == 0x6C) /* Le not accepted, La indicated */
|
---|
264 | {
|
---|
265 | /* Map command APDU onto TPDU */
|
---|
266 | memcpy (buffer, APDU_Cmd_Raw (cmd), 4);
|
---|
267 | buffer[4] = APDU_Rsp_SW2 (tpdu_rsp);
|
---|
268 |
|
---|
269 | tpdu_cmd = APDU_Cmd_New (buffer, 5);
|
---|
270 |
|
---|
271 | /* Delete response TPDU */
|
---|
272 | APDU_Rsp_Delete (tpdu_rsp);
|
---|
273 |
|
---|
274 | /* Re-issue command TPDU */
|
---|
275 | ret = Protocol_T0_ExchangeTPDU(tpdu_cmd, rsp);
|
---|
276 |
|
---|
277 | /* Delete command TPDU */
|
---|
278 | APDU_Cmd_Delete (tpdu_cmd);
|
---|
279 |
|
---|
280 | if (ret == PROTOCOL_T0_OK)
|
---|
281 | {
|
---|
282 | if (APDU_Rsp_DataLen ((*rsp)) > APDU_Cmd_Le (cmd))
|
---|
283 | {
|
---|
284 | /* Map response APDU onto TPDU */
|
---|
285 | APDU_Rsp_TruncateData ((*rsp), APDU_Cmd_Le (cmd));
|
---|
286 | }
|
---|
287 | }
|
---|
288 | }
|
---|
289 | else if (APDU_Rsp_SW1 (tpdu_rsp) == 0x61) /* Command processed, Lx indicated */
|
---|
290 | {
|
---|
291 | /* MAP response TPDU onto APDU */
|
---|
292 | (*rsp) = tpdu_rsp;
|
---|
293 |
|
---|
294 | /* Prepare Get Response TPDU */
|
---|
295 | buffer[0] = APDU_Cmd_Cla (cmd);
|
---|
296 | buffer[1] = 0xC0;
|
---|
297 | buffer[2] = 0x00;
|
---|
298 | buffer[3] = 0x00;
|
---|
299 |
|
---|
300 | do
|
---|
301 | {
|
---|
302 | /* Issue Get Response command TPDU */
|
---|
303 | buffer[4] = APDU_Rsp_SW2 (tpdu_rsp);
|
---|
304 | tpdu_cmd = APDU_Cmd_New (buffer, 5);
|
---|
305 |
|
---|
306 | ret = Protocol_T0_ExchangeTPDU(tpdu_cmd, (&tpdu_rsp));
|
---|
307 |
|
---|
308 | /* Delete command TPDU */
|
---|
309 | APDU_Cmd_Delete (tpdu_cmd);
|
---|
310 |
|
---|
311 | if (ret == PROTOCOL_T0_OK)
|
---|
312 | {
|
---|
313 | /* Append response TPDU to APDU */
|
---|
314 | if (APDU_Rsp_AppendData ((*rsp), tpdu_rsp) != APDU_OK)
|
---|
315 | ret = PROTOCOL_T0_ERROR;
|
---|
316 |
|
---|
317 | /* Delete response TPDU */
|
---|
318 | APDU_Rsp_Delete (tpdu_rsp);
|
---|
319 | }
|
---|
320 | }
|
---|
321 | while ((ret == PROTOCOL_T0_OK) && (APDU_Rsp_SW1 (*rsp) == 0x61));
|
---|
322 |
|
---|
323 | if (ret == PROTOCOL_T0_OK)
|
---|
324 | {
|
---|
325 | if (APDU_Rsp_DataLen ((*rsp)) > APDU_Cmd_Le (cmd))
|
---|
326 | {
|
---|
327 | /* Map response APDU onto TPDU */
|
---|
328 | APDU_Rsp_TruncateData ((*rsp), APDU_Cmd_Le (cmd));
|
---|
329 | }
|
---|
330 | }
|
---|
331 | }
|
---|
332 | else /* Le accepted */
|
---|
333 | {
|
---|
334 | /* Map response TPDU onto APDU without change */
|
---|
335 | (*rsp) = tpdu_rsp;
|
---|
336 | }
|
---|
337 | #else
|
---|
338 | (*rsp) = tpdu_rsp;
|
---|
339 | #endif
|
---|
340 | }
|
---|
341 |
|
---|
342 | return ret;
|
---|
343 | }
|
---|
344 |
|
---|
345 | static int Protocol_T14_Case3S (APDU_Cmd * cmd, APDU_Rsp ** rsp)
|
---|
346 | {
|
---|
347 | int ret;
|
---|
348 |
|
---|
349 | /* Send command TPDU */
|
---|
350 | ret = Protocol_T14_ExchangeTPDU(cmd, rsp);
|
---|
351 |
|
---|
352 | return ret;
|
---|
353 | }
|
---|
354 |
|
---|
355 | static int Protocol_T0_Case4S (APDU_Cmd * cmd, APDU_Rsp ** rsp)
|
---|
356 | {
|
---|
357 | int ret;
|
---|
358 | BYTE buffer[PROTOCOL_T0_MAX_SHORT_COMMAND];
|
---|
359 | APDU_Cmd *tpdu_cmd;
|
---|
360 | APDU_Rsp *tpdu_rsp;
|
---|
361 |
|
---|
362 | /* Map command APDU onto TPDU */
|
---|
363 | memcpy (buffer, APDU_Cmd_Raw (cmd), APDU_Cmd_RawLen (cmd) - 1);
|
---|
364 |
|
---|
365 | tpdu_cmd = APDU_Cmd_New (buffer, APDU_Cmd_RawLen (cmd) - 1);
|
---|
366 |
|
---|
367 | /* Send command TPDU */
|
---|
368 | ret = Protocol_T0_ExchangeTPDU(tpdu_cmd, (&tpdu_rsp));
|
---|
369 |
|
---|
370 | /* Delete command TPDU */
|
---|
371 | APDU_Cmd_Delete (tpdu_cmd);
|
---|
372 |
|
---|
373 | if (ret == PROTOCOL_T0_OK)
|
---|
374 | {
|
---|
375 | #ifdef PROTOCOL_T0_ISO
|
---|
376 | /* Command accepted with information added */
|
---|
377 | if (APDU_Rsp_SW1 (tpdu_rsp) == 0x61)
|
---|
378 | {
|
---|
379 | /* Prepare Get Reponse command TPDU */
|
---|
380 | buffer[0] = APDU_Cmd_Cla (cmd);
|
---|
381 | buffer[1] = 0xC0;
|
---|
382 | buffer[2] = 0x00;
|
---|
383 | buffer[3] = 0x00;
|
---|
384 |
|
---|
385 | if (APDU_Rsp_SW2 (tpdu_rsp) == 0x00)
|
---|
386 | buffer[4] = (BYTE) APDU_Cmd_Le (cmd);
|
---|
387 | else
|
---|
388 | buffer[4] = MIN (APDU_Cmd_Le (cmd), APDU_Rsp_SW2 (tpdu_rsp));
|
---|
389 |
|
---|
390 | tpdu_cmd = APDU_Cmd_New (buffer, 5);
|
---|
391 |
|
---|
392 | /* Delete response TPDU */
|
---|
393 | APDU_Rsp_Delete (tpdu_rsp);
|
---|
394 |
|
---|
395 | /* Issue Get Reponse command */
|
---|
396 | ret = Protocol_T0_ExchangeTPDU(tpdu_cmd, rsp);
|
---|
397 |
|
---|
398 | /* Delete command TPDU */
|
---|
399 | APDU_Cmd_Delete (tpdu_cmd);
|
---|
400 | }
|
---|
401 | else if ((APDU_Rsp_SW1 (tpdu_rsp) & 0xF0) == 0x60) /* Command not accepted */
|
---|
402 | {
|
---|
403 | /* Map response TPDU onto APDU without change */
|
---|
404 | (*rsp) = tpdu_rsp;
|
---|
405 | }
|
---|
406 | else /* Command accepted */
|
---|
407 | {
|
---|
408 | /* Delete response TPDU */
|
---|
409 | APDU_Rsp_Delete (tpdu_rsp);
|
---|
410 |
|
---|
411 | /* Prepare Get Reponse TPDU */
|
---|
412 | buffer[0] = APDU_Cmd_Cla (cmd);
|
---|
413 | buffer[1] = 0xC0;
|
---|
414 | buffer[2] = 0x00;
|
---|
415 | buffer[3] = 0x00;
|
---|
416 | buffer[4] = (BYTE) APDU_Cmd_Le (cmd);
|
---|
417 |
|
---|
418 | tpdu_cmd = APDU_Cmd_New (buffer, 5);
|
---|
419 |
|
---|
420 | /* Issue Get Reponse command TPDU */
|
---|
421 | ret = Protocol_T0_Case3S (t0, tpdu_cmd, rsp);
|
---|
422 |
|
---|
423 | /* Delete command TPDU */
|
---|
424 | APDU_Cmd_Delete (tpdu_cmd);
|
---|
425 | }
|
---|
426 | #else
|
---|
427 | (*rsp) = tpdu_rsp;
|
---|
428 | #endif
|
---|
429 | }
|
---|
430 |
|
---|
431 | return ret;
|
---|
432 | }
|
---|
433 |
|
---|
434 |
|
---|
435 | static int Protocol_T0_Case2E (APDU_Cmd * cmd, APDU_Rsp ** rsp)
|
---|
436 | {
|
---|
437 | int ret = PROTOCOL_T0_OK;
|
---|
438 | BYTE buffer[PROTOCOL_T0_MAX_SHORT_COMMAND];
|
---|
439 | APDU_Cmd *tpdu_cmd;
|
---|
440 | APDU_Rsp *tpdu_rsp;
|
---|
441 | ulong i;
|
---|
442 |
|
---|
443 | if (APDU_Cmd_Lc (cmd) < 256)
|
---|
444 | {
|
---|
445 | /* MAP APDU onto command TPDU */
|
---|
446 | buffer[0] = APDU_Cmd_Cla (cmd);
|
---|
447 | buffer[1] = APDU_Cmd_Ins (cmd);
|
---|
448 | buffer[2] = APDU_Cmd_P1 (cmd);
|
---|
449 | buffer[3] = APDU_Cmd_P2 (cmd);
|
---|
450 | buffer[4] = (BYTE) APDU_Cmd_Lc (cmd);
|
---|
451 |
|
---|
452 | memcpy (buffer + 5, APDU_Cmd_Data (cmd), buffer[4]);
|
---|
453 |
|
---|
454 | tpdu_cmd = APDU_Cmd_New (buffer, buffer[4] + 5);
|
---|
455 |
|
---|
456 | /* Send command TPDU */
|
---|
457 | ret = Protocol_T0_ExchangeTPDU(tpdu_cmd, rsp);
|
---|
458 |
|
---|
459 | /* Delete command TPDU */
|
---|
460 | APDU_Cmd_Delete (tpdu_cmd);
|
---|
461 | }
|
---|
462 | else
|
---|
463 | {
|
---|
464 | /* Prepare envelope TPDU */
|
---|
465 | buffer[0] = APDU_Cmd_Cla (cmd);
|
---|
466 | buffer[1] = 0xC2;
|
---|
467 | buffer[2] = 0x00;
|
---|
468 | buffer[3] = 0x00;
|
---|
469 |
|
---|
470 | for (i = 0; i < APDU_Cmd_RawLen (cmd); i += buffer[4])
|
---|
471 | {
|
---|
472 | /* Create envelope command TPDU */
|
---|
473 | buffer[4] = MIN (255, APDU_Cmd_RawLen (cmd) - i);
|
---|
474 | memcpy (buffer + 5, APDU_Cmd_Raw (cmd) + i, buffer[4]);
|
---|
475 |
|
---|
476 | tpdu_cmd = APDU_Cmd_New (buffer, buffer[4] + 5);
|
---|
477 |
|
---|
478 | /* Send envelope command TPDU */
|
---|
479 | ret = Protocol_T0_ExchangeTPDU(tpdu_cmd, (&tpdu_rsp));
|
---|
480 |
|
---|
481 | /* Delete command TPDU */
|
---|
482 | APDU_Cmd_Delete (tpdu_cmd);
|
---|
483 |
|
---|
484 | if (ret == PROTOCOL_T0_OK)
|
---|
485 | {
|
---|
486 | /* Card does support envelope command */
|
---|
487 | if (APDU_Rsp_SW1 (tpdu_rsp) == 0x90)
|
---|
488 | {
|
---|
489 | /* This is not the last segment */
|
---|
490 | if (buffer[4] + i < APDU_Cmd_RawLen (cmd))
|
---|
491 | {
|
---|
492 | /* Delete response TPDU */
|
---|
493 | APDU_Rsp_Delete (tpdu_rsp);
|
---|
494 | }
|
---|
495 | else
|
---|
496 | {
|
---|
497 | /* Map response TPDU onto APDU */
|
---|
498 | (*rsp) = tpdu_rsp;
|
---|
499 | }
|
---|
500 | }
|
---|
501 | else /* Card does not support envelope command or error */
|
---|
502 | {
|
---|
503 | /* Map response tpdu onto APDU without change */
|
---|
504 | (*rsp) = tpdu_rsp;
|
---|
505 | break;
|
---|
506 | }
|
---|
507 | }
|
---|
508 | else
|
---|
509 | {
|
---|
510 | break;
|
---|
511 | }
|
---|
512 | }
|
---|
513 | }
|
---|
514 |
|
---|
515 | return ret;
|
---|
516 | }
|
---|
517 |
|
---|
518 |
|
---|
519 | static int Protocol_T0_Case3E (APDU_Cmd * cmd, APDU_Rsp ** rsp)
|
---|
520 | {
|
---|
521 | int ret;
|
---|
522 | BYTE buffer[5];
|
---|
523 | APDU_Cmd *tpdu_cmd;
|
---|
524 | APDU_Rsp *tpdu_rsp;
|
---|
525 | long Lm, Lx;
|
---|
526 |
|
---|
527 | if (APDU_Cmd_Le (cmd) <= 256)
|
---|
528 | {
|
---|
529 | /* Map APDU onto command TPDU */
|
---|
530 | buffer[0] = APDU_Cmd_Cla (cmd);
|
---|
531 | buffer[1] = APDU_Cmd_Ins (cmd);
|
---|
532 | buffer[2] = APDU_Cmd_P1 (cmd);
|
---|
533 | buffer[3] = APDU_Cmd_P2 (cmd);
|
---|
534 | buffer[4] = (BYTE) APDU_Cmd_Le (cmd);
|
---|
535 |
|
---|
536 | tpdu_cmd = APDU_Cmd_New (buffer, 5);
|
---|
537 |
|
---|
538 | /* Send command TPDU */
|
---|
539 | ret = Protocol_T0_Case3S (tpdu_cmd, rsp);
|
---|
540 |
|
---|
541 | /* Delete command TPDU */
|
---|
542 | APDU_Cmd_Delete (tpdu_cmd);
|
---|
543 | }
|
---|
544 | else
|
---|
545 | {
|
---|
546 | /* Map APDU onto command TPDU */
|
---|
547 | buffer[0] = APDU_Cmd_Cla (cmd);
|
---|
548 | buffer[1] = APDU_Cmd_Ins (cmd);
|
---|
549 | buffer[2] = APDU_Cmd_P1 (cmd);
|
---|
550 | buffer[3] = APDU_Cmd_P2 (cmd);
|
---|
551 | buffer[4] = 0x00;
|
---|
552 |
|
---|
553 | tpdu_cmd = APDU_Cmd_New (buffer, 5);
|
---|
554 |
|
---|
555 | /* Send command TPDU */
|
---|
556 | ret = Protocol_T0_ExchangeTPDU(tpdu_cmd, (&tpdu_rsp));
|
---|
557 |
|
---|
558 | /* Delete command TPDU */
|
---|
559 | APDU_Cmd_Delete (tpdu_cmd);
|
---|
560 |
|
---|
561 | if (ret == PROTOCOL_T0_OK)
|
---|
562 | {
|
---|
563 | /* Le definitely not accepted */
|
---|
564 | if (APDU_Rsp_SW1 (tpdu_rsp) == 0x67)
|
---|
565 | {
|
---|
566 | /* Map response APDU onto TPDU without change */
|
---|
567 | (*rsp) = tpdu_rsp;
|
---|
568 | }
|
---|
569 | else if (APDU_Rsp_SW1 (tpdu_rsp) == 0x6C) /* Le not accepted, La indicated */
|
---|
570 | {
|
---|
571 | /* Map command APDU onto TPDU */
|
---|
572 | memcpy (buffer, APDU_Cmd_Raw (cmd), 4);
|
---|
573 | buffer[4] = APDU_Rsp_SW2 (tpdu_rsp);
|
---|
574 |
|
---|
575 | tpdu_cmd = APDU_Cmd_New (buffer, 5);
|
---|
576 |
|
---|
577 | /* Delete response TPDU */
|
---|
578 | APDU_Rsp_Delete (tpdu_rsp);
|
---|
579 |
|
---|
580 | /* Re-issue command TPDU */
|
---|
581 | ret = Protocol_T0_ExchangeTPDU(tpdu_cmd, rsp);
|
---|
582 |
|
---|
583 | /* Delete command TPDU */
|
---|
584 | APDU_Cmd_Delete (tpdu_cmd);
|
---|
585 | }
|
---|
586 | else if (APDU_Rsp_SW1 (tpdu_rsp) == 0x61) /* Command processed, Lx indicated */
|
---|
587 | {
|
---|
588 | /* Map response TPDU onto APDU */
|
---|
589 | (*rsp) = tpdu_rsp;
|
---|
590 |
|
---|
591 | Lx = (APDU_Rsp_SW2 (tpdu_rsp) == 0x00) ? 256 : APDU_Rsp_SW2 (tpdu_rsp);
|
---|
592 | Lm = APDU_Cmd_Le (cmd) - APDU_Rsp_DataLen (*rsp);
|
---|
593 |
|
---|
594 | /* Prepare Get Response TPDU */
|
---|
595 | buffer[0] = APDU_Cmd_Cla (cmd);
|
---|
596 | buffer[1] = 0xC0;
|
---|
597 | buffer[2] = 0x00;
|
---|
598 | buffer[3] = 0x00;
|
---|
599 |
|
---|
600 | while (Lm > 0)
|
---|
601 | {
|
---|
602 | buffer[4] = (BYTE) MIN (Lm, Lx);
|
---|
603 |
|
---|
604 | tpdu_cmd = APDU_Cmd_New (buffer, 5);
|
---|
605 |
|
---|
606 | /* Issue Get Response command TPDU */
|
---|
607 | ret = Protocol_T0_ExchangeTPDU(tpdu_cmd, (&tpdu_rsp));
|
---|
608 |
|
---|
609 | /* Delete command TPDU */
|
---|
610 | APDU_Cmd_Delete (tpdu_cmd);
|
---|
611 |
|
---|
612 | if (ret == PROTOCOL_T0_OK)
|
---|
613 | {
|
---|
614 | /* Append response TPDU to APDU */
|
---|
615 | if (APDU_Rsp_AppendData ((*rsp), tpdu_rsp) != APDU_OK)
|
---|
616 | {
|
---|
617 | ret = PROTOCOL_T0_ERROR;
|
---|
618 | APDU_Rsp_Delete (tpdu_rsp);
|
---|
619 | break;
|
---|
620 | }
|
---|
621 |
|
---|
622 | /* Delete response TPDU */
|
---|
623 | APDU_Rsp_Delete (tpdu_rsp);
|
---|
624 | }
|
---|
625 | else
|
---|
626 | {
|
---|
627 | break;
|
---|
628 | }
|
---|
629 |
|
---|
630 | Lm = APDU_Cmd_Le (cmd) - APDU_Rsp_DataLen (*rsp);
|
---|
631 | }/* Lm == 0 */
|
---|
632 | }
|
---|
633 | else /* Le accepted: card has no more than 265 bytes or does not support Get Response */
|
---|
634 | {
|
---|
635 | /* Map response TPDU onto APDU without change */
|
---|
636 | (*rsp) = tpdu_rsp;
|
---|
637 | }
|
---|
638 | }
|
---|
639 | }
|
---|
640 |
|
---|
641 | return ret;
|
---|
642 | }
|
---|
643 |
|
---|
644 |
|
---|
645 | static int Protocol_T0_Case4E (APDU_Cmd * cmd, APDU_Rsp ** rsp)
|
---|
646 | {
|
---|
647 | int ret;
|
---|
648 | BYTE buffer[PROTOCOL_T0_MAX_SHORT_COMMAND];
|
---|
649 | APDU_Cmd *tpdu_cmd, *gr_cmd;
|
---|
650 | APDU_Rsp *tpdu_rsp;
|
---|
651 | long Le;
|
---|
652 |
|
---|
653 | /* 4E1 */
|
---|
654 | if (APDU_Cmd_Lc (cmd) < 256)
|
---|
655 | {
|
---|
656 | /* Map APDU onto command TPDU */
|
---|
657 | buffer[0] = APDU_Cmd_Cla (cmd);
|
---|
658 | buffer[1] = APDU_Cmd_Ins (cmd);
|
---|
659 | buffer[2] = APDU_Cmd_P1 (cmd);
|
---|
660 | buffer[3] = APDU_Cmd_P2 (cmd);
|
---|
661 | buffer[4] = (BYTE) APDU_Cmd_Lc (cmd);
|
---|
662 | memcpy (buffer + 5, APDU_Cmd_Data (cmd), buffer[4]);
|
---|
663 |
|
---|
664 | tpdu_cmd = APDU_Cmd_New (buffer, buffer[4] + 5);
|
---|
665 |
|
---|
666 | /* Send command TPDU */
|
---|
667 | ret = Protocol_T0_ExchangeTPDU(tpdu_cmd, (&tpdu_rsp));
|
---|
668 |
|
---|
669 | /* Delete command TPDU */
|
---|
670 | APDU_Cmd_Delete (tpdu_cmd);
|
---|
671 | }
|
---|
672 | else /* 4E2 */
|
---|
673 | {
|
---|
674 | ret = Protocol_T0_Case2E (cmd, (&tpdu_rsp));
|
---|
675 | }
|
---|
676 |
|
---|
677 | /* 4E1 a) b) and c) */
|
---|
678 | if (ret == PROTOCOL_T0_OK)
|
---|
679 | {
|
---|
680 | if (APDU_Rsp_SW1 (tpdu_rsp) == 0x61)
|
---|
681 | {
|
---|
682 | /* Lm == (Le - APDU_Rsp_RawLen (tpdu_rsp)) == 0 */
|
---|
683 |
|
---|
684 | if (APDU_Rsp_SW2 (tpdu_rsp) != 0x00)
|
---|
685 | Le = MIN(APDU_Rsp_SW2 (tpdu_rsp), APDU_Cmd_Le(cmd));
|
---|
686 | else
|
---|
687 | Le = APDU_Cmd_Le (cmd);
|
---|
688 |
|
---|
689 | /* Delete response TPDU */
|
---|
690 | APDU_Rsp_Delete (tpdu_rsp);
|
---|
691 |
|
---|
692 | /* Prepare extended Get Response APDU command */
|
---|
693 | buffer[0] = APDU_Cmd_Cla (cmd);
|
---|
694 | buffer[1] = 0xC0;
|
---|
695 | buffer[2] = 0x00;
|
---|
696 | buffer[3] = 0x00;
|
---|
697 | buffer[4] = 0x00; /* B1 = 0x00 */
|
---|
698 | buffer[5] = (BYTE) (Le >> 8); /* B2 = BL-1 */
|
---|
699 | buffer[6] = (BYTE) (Le & 0x00FF); /* B3 = BL */
|
---|
700 |
|
---|
701 | gr_cmd = APDU_Cmd_New (buffer, 7);
|
---|
702 |
|
---|
703 | /* Issue Case 3E get response command */
|
---|
704 | ret = Protocol_T0_Case3E (gr_cmd, rsp);
|
---|
705 |
|
---|
706 | /* Delete Get Response command APDU */
|
---|
707 | APDU_Cmd_Delete (gr_cmd);
|
---|
708 | }
|
---|
709 | else if ((APDU_Rsp_SW1 (tpdu_rsp) & 0xF0) == 0x60)
|
---|
710 | {
|
---|
711 | /* Map response TPDU onto APDU without change */
|
---|
712 | (*rsp) = tpdu_rsp;
|
---|
713 | }
|
---|
714 | else
|
---|
715 | {
|
---|
716 | /* Delete response TPDU */
|
---|
717 | APDU_Rsp_Delete (tpdu_rsp);
|
---|
718 |
|
---|
719 | /* Prepare extended Get Response APDU command */
|
---|
720 | buffer[0] = APDU_Cmd_Cla (cmd);
|
---|
721 | buffer[1] = 0xC0;
|
---|
722 | buffer[2] = 0x00;
|
---|
723 | buffer[3] = 0x00;
|
---|
724 | buffer[4] = 0x00; /* B1 = 0x00 */
|
---|
725 | buffer[5] = (BYTE) (APDU_Cmd_Le (cmd) >> 8); /* B2 = BL-1 */
|
---|
726 | buffer[6] = (BYTE) (APDU_Cmd_Le (cmd) & 0x00FF); /* B3 = BL */
|
---|
727 |
|
---|
728 | gr_cmd = APDU_Cmd_New (buffer, 7);
|
---|
729 |
|
---|
730 | /* Issue Case 3E get response command */
|
---|
731 | ret = Protocol_T0_Case3E (gr_cmd, rsp);
|
---|
732 |
|
---|
733 | /* Delete Get Response command APDU */
|
---|
734 | APDU_Cmd_Delete (gr_cmd);
|
---|
735 | }
|
---|
736 | }
|
---|
737 | return ret;
|
---|
738 | }
|
---|
739 |
|
---|
740 |
|
---|
741 | static int Protocol_T0_ExchangeTPDU (APDU_Cmd * cmd, APDU_Rsp ** rsp)
|
---|
742 | {
|
---|
743 | BYTE buffer[PROTOCOL_T0_MAX_SHORT_RESPONSE];
|
---|
744 | BYTE *data;
|
---|
745 | long Lc, Le, sent, recv;
|
---|
746 | int ret = PROTOCOL_T0_OK, nulls, cmd_case;
|
---|
747 |
|
---|
748 | /* Parse APDU */
|
---|
749 | Lc = APDU_Cmd_Lc (cmd);
|
---|
750 | Le = APDU_Cmd_Le (cmd);
|
---|
751 | data = APDU_Cmd_Data (cmd);
|
---|
752 | cmd_case = APDU_Cmd_Case (cmd);
|
---|
753 |
|
---|
754 | /* Check case of command */
|
---|
755 | if ((cmd_case != APDU_CASE_2S) && (cmd_case != APDU_CASE_3S))
|
---|
756 | return PROTOCOL_T0_ERROR;
|
---|
757 |
|
---|
758 | /* Send header bytes */
|
---|
759 | if (ICC_Async_Transmit (5, APDU_Cmd_Header (cmd)) != ICC_ASYNC_OK)
|
---|
760 | {
|
---|
761 | (*rsp) = NULL;
|
---|
762 | return PROTOCOL_T0_ICC_ERROR;
|
---|
763 | }
|
---|
764 |
|
---|
765 | /* Initialise counters */
|
---|
766 | nulls = 0;
|
---|
767 | sent = 0;
|
---|
768 | recv = 0;
|
---|
769 |
|
---|
770 | /*
|
---|
771 | * Let's be a bit paranoid with buffer sizes within this loop
|
---|
772 | * so it doesn't overflow reception and transmission buffers
|
---|
773 | * if card does not strictly respect the protocol
|
---|
774 | */
|
---|
775 |
|
---|
776 | while (recv < PROTOCOL_T0_MAX_SHORT_RESPONSE)
|
---|
777 | {
|
---|
778 | /* Read one procedure byte */
|
---|
779 | if (ICC_Async_Receive (1, buffer + recv) != ICC_ASYNC_OK)
|
---|
780 | {
|
---|
781 | ret = PROTOCOL_T0_ICC_ERROR;
|
---|
782 | break;
|
---|
783 | }
|
---|
784 |
|
---|
785 | /* NULL byte received */
|
---|
786 | if (buffer[recv] == 0x60)
|
---|
787 | {
|
---|
788 | nulls++;
|
---|
789 |
|
---|
790 | /* Maximum number of nulls reached */
|
---|
791 | if (nulls >= PROTOCOL_T0_MAX_NULLS)
|
---|
792 | {
|
---|
793 | ret = PROTOCOL_T0_NULL_ERROR;
|
---|
794 | break;
|
---|
795 | }
|
---|
796 |
|
---|
797 | continue;
|
---|
798 | }
|
---|
799 | else if ((buffer[recv] & 0xF0) == 0x60 || (buffer[recv] & 0xF0) == 0x90) /* SW1 byte received */
|
---|
800 | {//printf("sw1\n");
|
---|
801 | recv++;
|
---|
802 |
|
---|
803 | if (recv >= PROTOCOL_T0_MAX_SHORT_RESPONSE)
|
---|
804 | return PROTOCOL_T0_ERROR;
|
---|
805 |
|
---|
806 | /* Read SW2 byte */
|
---|
807 | if (ICC_Async_Receive (1, buffer + recv) != ICC_ASYNC_OK)
|
---|
808 | {
|
---|
809 | ret = PROTOCOL_T0_ICC_ERROR;
|
---|
810 | break;
|
---|
811 | }
|
---|
812 |
|
---|
813 | recv++;
|
---|
814 |
|
---|
815 | ret = PROTOCOL_T0_OK;
|
---|
816 | break;
|
---|
817 | }
|
---|
818 | else if ((buffer[recv] & 0x0E) == (APDU_Cmd_Ins (cmd) & 0x0E)) /* ACK byte received */
|
---|
819 | {//printf("ack\n");
|
---|
820 | /* Reset null's counter */
|
---|
821 | nulls = 0;
|
---|
822 |
|
---|
823 | /* Case 2 command: send data */
|
---|
824 | if (cmd_case == APDU_CASE_2S)
|
---|
825 | {
|
---|
826 | if (sent >= Lc)
|
---|
827 | return PROTOCOL_T0_ERROR;
|
---|
828 |
|
---|
829 | if (ICC_Async_Transmit(MAX (Lc - sent, 0), data + sent) != ICC_ASYNC_OK) /* Send remaining data bytes */
|
---|
830 | {
|
---|
831 | ret = PROTOCOL_T0_ICC_ERROR;
|
---|
832 | break;
|
---|
833 | }
|
---|
834 |
|
---|
835 | sent = Lc;
|
---|
836 | continue;
|
---|
837 | }
|
---|
838 | else /* Case 3 command: receive data */
|
---|
839 | {
|
---|
840 | if (recv >= PROTOCOL_T0_MAX_SHORT_RESPONSE)
|
---|
841 | return PROTOCOL_T0_ERROR;
|
---|
842 |
|
---|
843 | /*
|
---|
844 | * Le <= PROTOCOL_T0_MAX_SHORT_RESPONSE - 2 for short commands
|
---|
845 | */
|
---|
846 |
|
---|
847 | /* Read remaining data bytes */
|
---|
848 | if (ICC_Async_Receive(MAX (Le - recv, 0), buffer + recv) != ICC_ASYNC_OK)
|
---|
849 | {//printf("error %d\n", (int)Le);
|
---|
850 | ret = PROTOCOL_T0_ICC_ERROR;
|
---|
851 | break;
|
---|
852 | }
|
---|
853 |
|
---|
854 | recv = Le;
|
---|
855 | continue;
|
---|
856 | }
|
---|
857 | }
|
---|
858 | else if ((buffer[recv] & 0x0E) == ((~APDU_Cmd_Ins (cmd)) & 0x0E)) /* ~ACK byte received */
|
---|
859 | {//printf("~ack\n");
|
---|
860 | /* Reset null's counter */
|
---|
861 | nulls = 0;
|
---|
862 |
|
---|
863 | /* Case 2 command: send data */
|
---|
864 | if (cmd_case == APDU_CASE_2S)
|
---|
865 | {
|
---|
866 | if (sent >= Lc)
|
---|
867 | return PROTOCOL_T0_ERROR;
|
---|
868 |
|
---|
869 | /* Send next data byte */
|
---|
870 | if (ICC_Async_Transmit (1, data + sent) !=ICC_ASYNC_OK)
|
---|
871 | {
|
---|
872 | ret = PROTOCOL_T0_ICC_ERROR;
|
---|
873 | break;
|
---|
874 | }
|
---|
875 |
|
---|
876 | sent++;
|
---|
877 | continue;
|
---|
878 | }
|
---|
879 | else /* Case 3 command: receive data */
|
---|
880 | {
|
---|
881 | if (recv >= PROTOCOL_T0_MAX_SHORT_RESPONSE)
|
---|
882 | return PROTOCOL_T0_ERROR;
|
---|
883 |
|
---|
884 | /* Read next data byte */
|
---|
885 | if (ICC_Async_Receive (1, buffer + recv) != ICC_ASYNC_OK)
|
---|
886 | {
|
---|
887 | ret = PROTOCOL_T0_ICC_ERROR;
|
---|
888 | break;
|
---|
889 | }
|
---|
890 |
|
---|
891 | recv++;
|
---|
892 | continue;
|
---|
893 | }
|
---|
894 | }
|
---|
895 | else /* Anything else received */
|
---|
896 | {//printf("hs\n");
|
---|
897 | ret = PROTOCOL_T0_ERROR;
|
---|
898 | break;
|
---|
899 | }
|
---|
900 | }
|
---|
901 |
|
---|
902 | if (ret == PROTOCOL_T0_OK)
|
---|
903 | (*rsp) = APDU_Rsp_New (buffer, recv);
|
---|
904 | else
|
---|
905 | (*rsp) = NULL;
|
---|
906 |
|
---|
907 | return (ret);
|
---|
908 | }
|
---|
909 |
|
---|
910 | static int Protocol_T14_ExchangeTPDU (APDU_Cmd * cmd, APDU_Rsp ** rsp)
|
---|
911 | {
|
---|
912 | BYTE buffer[PROTOCOL_T14_MAX_SHORT_RESPONSE];
|
---|
913 | BYTE *cmd_raw;
|
---|
914 | long sent, recv, cmd_len;
|
---|
915 | int ret = PROTOCOL_T14_OK, nulls, cmd_case;
|
---|
916 | BYTE ixor = 0x3E;
|
---|
917 | BYTE ixor1 = 0x3F;
|
---|
918 | BYTE b1 = 0x01;
|
---|
919 | int i;
|
---|
920 |
|
---|
921 | /* Parse APDU */
|
---|
922 | cmd_len = APDU_Cmd_Lc (cmd) + 5;
|
---|
923 | cmd_raw = APDU_Cmd_Raw (cmd);
|
---|
924 | cmd_case = APDU_Cmd_Case (cmd);
|
---|
925 | for(i=0; i<cmd_len; i++)
|
---|
926 | ixor^=cmd_raw[i];
|
---|
927 |
|
---|
928 | /* Check case of command */
|
---|
929 | if ((cmd_case != APDU_CASE_2S) && (cmd_case != APDU_CASE_3S))
|
---|
930 | return PROTOCOL_T14_ERROR;
|
---|
931 |
|
---|
932 | if (reader[ridx].typ != R_INTERNAL)
|
---|
933 | {
|
---|
934 | /* Send 0x01 byte */
|
---|
935 | if (ICC_Async_Transmit (1, &b1) != ICC_ASYNC_OK)
|
---|
936 | {
|
---|
937 | (*rsp) = NULL;
|
---|
938 | return PROTOCOL_T14_ICC_ERROR;
|
---|
939 | }
|
---|
940 |
|
---|
941 | /* Send apdu */
|
---|
942 | if (ICC_Async_Transmit (cmd_len, cmd_raw) != ICC_ASYNC_OK)
|
---|
943 | {
|
---|
944 | (*rsp) = NULL;
|
---|
945 | return PROTOCOL_T14_ICC_ERROR;
|
---|
946 | }
|
---|
947 |
|
---|
948 | /* Send xor byte */
|
---|
949 | if (ICC_Async_Transmit (1, &ixor) != ICC_ASYNC_OK)
|
---|
950 | {
|
---|
951 | (*rsp) = NULL;
|
---|
952 | return PROTOCOL_T14_ICC_ERROR;
|
---|
953 | }
|
---|
954 | }
|
---|
955 | else
|
---|
956 | {
|
---|
957 | buffer[0] = 0x01;
|
---|
958 | memcpy(buffer+1, cmd_raw, cmd_len);
|
---|
959 | buffer[cmd_len+1] = ixor;
|
---|
960 |
|
---|
961 | /* Send apdu */
|
---|
962 | if (ICC_Async_Transmit (cmd_len+2, buffer) != ICC_ASYNC_OK)
|
---|
963 | {
|
---|
964 | (*rsp) = NULL;
|
---|
965 | return PROTOCOL_T14_ICC_ERROR;
|
---|
966 | }
|
---|
967 | }
|
---|
968 |
|
---|
969 | /* Initialise counters */
|
---|
970 | nulls = 0;
|
---|
971 | sent = 0;
|
---|
972 | recv = 0;
|
---|
973 |
|
---|
974 |
|
---|
975 |
|
---|
976 | /*
|
---|
977 | * Let's be a bit paranoid with buffer sizes within this loop
|
---|
978 | * so it doesn't overflow reception and transmission buffers
|
---|
979 | * if card does not strictly respect the protocol
|
---|
980 | */
|
---|
981 |
|
---|
982 | while (recv < PROTOCOL_T14_MAX_SHORT_RESPONSE)
|
---|
983 | {
|
---|
984 | if(cmd_raw[0] == 0x02 && cmd_raw[1] == 0x09)
|
---|
985 | {
|
---|
986 | #ifdef HAVE_NANOSLEEP
|
---|
987 | struct timespec req_ts;
|
---|
988 |
|
---|
989 | // req_ts.tv_sec = 1;
|
---|
990 | // req_ts.tv_nsec = 500000000;
|
---|
991 | req_ts.tv_sec = 2;
|
---|
992 | req_ts.tv_nsec = 500000000;
|
---|
993 | nanosleep (&req_ts, NULL);
|
---|
994 | #else
|
---|
995 | usleep (999999L);
|
---|
996 | #endif
|
---|
997 | }
|
---|
998 | /* Read one procedure byte */
|
---|
999 | if (ICC_Async_Receive (8, buffer) != ICC_ASYNC_OK)
|
---|
1000 | {
|
---|
1001 | ret = PROTOCOL_T14_ICC_ERROR;
|
---|
1002 | break;
|
---|
1003 | }
|
---|
1004 | else
|
---|
1005 | {
|
---|
1006 | recv = (long)buffer[7];
|
---|
1007 |
|
---|
1008 | if(recv)
|
---|
1009 | {
|
---|
1010 | if (ICC_Async_Receive (recv, buffer + 8) != ICC_ASYNC_OK)
|
---|
1011 | {
|
---|
1012 | ret = PROTOCOL_T14_ICC_ERROR;
|
---|
1013 | break;
|
---|
1014 | }
|
---|
1015 | }
|
---|
1016 |
|
---|
1017 | if (ICC_Async_Receive (1, &ixor) != ICC_ASYNC_OK)
|
---|
1018 | {
|
---|
1019 | ret = PROTOCOL_T14_ICC_ERROR;
|
---|
1020 | break;
|
---|
1021 | }
|
---|
1022 |
|
---|
1023 | for(i=0; i<8+recv; i++)
|
---|
1024 | ixor1^=buffer[i];
|
---|
1025 |
|
---|
1026 | if(ixor1 != ixor)
|
---|
1027 | {
|
---|
1028 | ret = PROTOCOL_T14_ERROR;
|
---|
1029 | break;
|
---|
1030 | }
|
---|
1031 |
|
---|
1032 |
|
---|
1033 |
|
---|
1034 |
|
---|
1035 | ret = PROTOCOL_T14_OK;
|
---|
1036 | break;
|
---|
1037 | }
|
---|
1038 | }
|
---|
1039 |
|
---|
1040 | if (ret == PROTOCOL_T14_OK)
|
---|
1041 | {
|
---|
1042 | memcpy(buffer + 8 + recv, buffer + 2, 2);
|
---|
1043 | (*rsp) = APDU_Rsp_New (buffer + 8, recv + 2);
|
---|
1044 | }
|
---|
1045 | else
|
---|
1046 | {
|
---|
1047 | (*rsp) = NULL;
|
---|
1048 | }
|
---|
1049 |
|
---|
1050 | return (ret);
|
---|
1051 | }
|
---|