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