1 | /*
|
---|
2 | cardterminal.c
|
---|
3 | Card Terminal handling and CT-BCS functions
|
---|
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 "cardterminal.h"
|
---|
27 | #include "atr.h"
|
---|
28 | #include <stdlib.h>
|
---|
29 | #include <string.h>
|
---|
30 |
|
---|
31 | /*
|
---|
32 | * Not exported constants definition
|
---|
33 | */
|
---|
34 |
|
---|
35 | #define CARDTERMINAL_RESETCT_BUFFER_SIZE 35
|
---|
36 | #define CARDTERMINAL_REQUESTICC_BUFFER_SIZE 35
|
---|
37 | #define CARDTERMINAL_GETSTATUS_BUFFER_SIZE 19
|
---|
38 | #define CARDTERMINAL_EJECTICC_BUFFER_SIZE 2
|
---|
39 | #define CARDTERMINAL_MANUFACTURER "DETWK"
|
---|
40 |
|
---|
41 | /*
|
---|
42 | * Not exported functions declaration
|
---|
43 | */
|
---|
44 |
|
---|
45 | static char CardTerminal_ResetCT (CardTerminal * ct, APDU_Cmd * cmd, APDU_Rsp ** rsp);
|
---|
46 |
|
---|
47 | static char CardTerminal_RequestICC (CardTerminal * ct, APDU_Cmd * cmd, APDU_Rsp ** rsp);
|
---|
48 |
|
---|
49 | static char CardTerminal_GetStatus (CardTerminal * ct, APDU_Cmd * cmd, APDU_Rsp ** rsp);
|
---|
50 |
|
---|
51 | static char CardTerminal_SetParity (CardTerminal * ct, APDU_Cmd * cmd, APDU_Rsp ** rsp);
|
---|
52 |
|
---|
53 | static char CardTerminal_EjectICC (CardTerminal * ct, APDU_Cmd * cmd, APDU_Rsp ** rsp);
|
---|
54 |
|
---|
55 | static void CardTerminal_Clear (CardTerminal * ct);
|
---|
56 |
|
---|
57 | /*
|
---|
58 | * Exported functions definition
|
---|
59 | */
|
---|
60 |
|
---|
61 | CardTerminal * CardTerminal_New (void)
|
---|
62 | {
|
---|
63 | CardTerminal *ct;
|
---|
64 |
|
---|
65 | ct = (CardTerminal *) malloc (sizeof (CardTerminal));
|
---|
66 |
|
---|
67 | if (ct != NULL)
|
---|
68 | CardTerminal_Clear (ct);
|
---|
69 |
|
---|
70 | return ct;
|
---|
71 | }
|
---|
72 |
|
---|
73 | char CardTerminal_Init (CardTerminal * ct, int reader_type, int mhz, int cardmhz)
|
---|
74 | {
|
---|
75 | char ret;
|
---|
76 | int i;
|
---|
77 |
|
---|
78 | /* Create a new IO_Serial */
|
---|
79 | ct->io = IO_Serial_New (mhz, cardmhz);
|
---|
80 |
|
---|
81 | /* Memory error */
|
---|
82 | if (ct->io == NULL)
|
---|
83 | return ERR_MEMORY;
|
---|
84 |
|
---|
85 | /* Initialise serial port */
|
---|
86 | if (!IO_Serial_Init (ct->io, reader_type))
|
---|
87 | {
|
---|
88 | free (ct->io);
|
---|
89 | ct->io = NULL;
|
---|
90 | return ERR_TRANS;
|
---|
91 | }
|
---|
92 |
|
---|
93 | /* Cearte all reader slots */
|
---|
94 | ct->num_slots = 0;
|
---|
95 | do
|
---|
96 | {
|
---|
97 | i = ct->num_slots++;
|
---|
98 |
|
---|
99 | /* Create one slot */
|
---|
100 | ct->slots[i] = CT_Slot_New ();
|
---|
101 |
|
---|
102 | if (ct->slots[i] == NULL)
|
---|
103 | {
|
---|
104 | ret = ERR_MEMORY;
|
---|
105 | break;
|
---|
106 | }
|
---|
107 |
|
---|
108 | /* Initialise slot */
|
---|
109 | ret = CT_Slot_Init (ct->slots[i], ct->io, i);
|
---|
110 |
|
---|
111 | if (ret != OK)
|
---|
112 | break;
|
---|
113 | }
|
---|
114 | while (!CT_Slot_IsLast(ct->slots[i]));
|
---|
115 |
|
---|
116 | /* On error restore initial state */
|
---|
117 | if (ret != OK)
|
---|
118 | {
|
---|
119 | while (ct->num_slots > 0)
|
---|
120 | {
|
---|
121 | if (ct->slots[i] != NULL)
|
---|
122 | {
|
---|
123 | CT_Slot_Delete (ct->slots[i]);
|
---|
124 | ct->slots[i] = NULL;
|
---|
125 | }
|
---|
126 |
|
---|
127 | ct->num_slots --;
|
---|
128 | i--;
|
---|
129 | }
|
---|
130 |
|
---|
131 | IO_Serial_Close (ct->io);
|
---|
132 | free (ct->io);
|
---|
133 | ct->io = NULL;
|
---|
134 | }
|
---|
135 | #ifdef HAVE_PTHREAD_H
|
---|
136 | else
|
---|
137 | {
|
---|
138 | pthread_mutex_init(&(ct->mutex), NULL);
|
---|
139 | }
|
---|
140 | #endif
|
---|
141 | return ret;
|
---|
142 | }
|
---|
143 |
|
---|
144 | char CardTerminal_Command (CardTerminal * ct, APDU_Cmd * cmd, APDU_Rsp ** rsp)
|
---|
145 | {
|
---|
146 | BYTE buffer[CTBCS_MIN_RESPONSE_SIZE], cla, ins;
|
---|
147 | long length;
|
---|
148 | char ret;
|
---|
149 |
|
---|
150 | /* Get class of command */
|
---|
151 | cla = APDU_Cmd_Cla (cmd);
|
---|
152 |
|
---|
153 | if (cla != CTBCS_CLA)
|
---|
154 | {
|
---|
155 | length = CTBCS_MIN_RESPONSE_SIZE;
|
---|
156 | buffer[0] = CTBCS_SW1_WRONG_CLA;
|
---|
157 | buffer[1] = CTBCS_SW2_WRONG_CLA;
|
---|
158 |
|
---|
159 | (*rsp) = APDU_Rsp_New (buffer, length);
|
---|
160 | ret = OK;
|
---|
161 | }
|
---|
162 | else
|
---|
163 | {
|
---|
164 | /* Get instruction */
|
---|
165 | ins = APDU_Cmd_Ins (cmd);
|
---|
166 |
|
---|
167 | /* Reset CT */
|
---|
168 | if (ins == CTBCS_INS_RESET)
|
---|
169 | ret = CardTerminal_ResetCT (ct, cmd, rsp);
|
---|
170 | else if (ins == CTBCS_INS_REQUEST) /* Request ICC */
|
---|
171 | ret = CardTerminal_RequestICC (ct, cmd, rsp);
|
---|
172 | else if (ins == CTBCS_INS_STATUS) /* Get Status */
|
---|
173 | ret = CardTerminal_GetStatus (ct, cmd, rsp);
|
---|
174 | else if (ins == CTBCS_INS_PARITY) /* Get Status */
|
---|
175 | ret = CardTerminal_SetParity (ct, cmd, rsp);
|
---|
176 | else if (ins == CTBCS_INS_EJECT) /* Eject ICC */
|
---|
177 | ret = CardTerminal_EjectICC (ct, cmd, rsp);
|
---|
178 | else /* Wrong instruction */
|
---|
179 | {
|
---|
180 | length = CTBCS_MIN_RESPONSE_SIZE;
|
---|
181 | buffer[0] = CTBCS_SW1_WRONG_INS;
|
---|
182 | buffer[1] = CTBCS_SW2_WRONG_INS;
|
---|
183 |
|
---|
184 | (*rsp) = APDU_Rsp_New (buffer, length);
|
---|
185 | ret = OK;
|
---|
186 | }
|
---|
187 | }
|
---|
188 |
|
---|
189 | return ret;
|
---|
190 | }
|
---|
191 |
|
---|
192 | char CardTerminal_Close (CardTerminal * ct)
|
---|
193 | {
|
---|
194 | char ret, aux;
|
---|
195 | unsigned int i;
|
---|
196 |
|
---|
197 | ret = OK;
|
---|
198 |
|
---|
199 | for (i = 0; i < ct->num_slots; i++)
|
---|
200 | {
|
---|
201 | if (ct->slots[i] != NULL)
|
---|
202 | {
|
---|
203 | aux = CT_Slot_Close (ct->slots[i]);
|
---|
204 | if (aux != OK)
|
---|
205 | ret = aux;
|
---|
206 |
|
---|
207 | CT_Slot_Delete (ct->slots[i]);
|
---|
208 | }
|
---|
209 | }
|
---|
210 |
|
---|
211 | if (ct->io != NULL)
|
---|
212 | {
|
---|
213 | if (!IO_Serial_Close (ct->io))
|
---|
214 | ret = ERR_TRANS;
|
---|
215 |
|
---|
216 | free (ct->io);
|
---|
217 | }
|
---|
218 |
|
---|
219 | CardTerminal_Clear (ct);
|
---|
220 |
|
---|
221 | #ifdef HAVE_PTHREAD_H
|
---|
222 | pthread_mutex_destroy(&(ct->mutex));
|
---|
223 | #endif
|
---|
224 | return ret;
|
---|
225 | }
|
---|
226 |
|
---|
227 | void CardTerminal_Delete (CardTerminal * ct)
|
---|
228 | {
|
---|
229 | free (ct);
|
---|
230 | }
|
---|
231 |
|
---|
232 | CT_Slot * CardTerminal_GetSlot (CardTerminal * ct, unsigned int number)
|
---|
233 | {
|
---|
234 | if (number < (ct->num_slots))
|
---|
235 | return ct->slots[number];
|
---|
236 |
|
---|
237 | return NULL;
|
---|
238 | }
|
---|
239 |
|
---|
240 | #ifdef HAVE_PTHREAD_H
|
---|
241 | pthread_mutex_t * CardTerminal_GetMutex (CardTerminal * ct)
|
---|
242 | {
|
---|
243 | return &(ct->mutex);
|
---|
244 | }
|
---|
245 | #endif
|
---|
246 |
|
---|
247 | /*
|
---|
248 | * Not exported functions definition
|
---|
249 | */
|
---|
250 |
|
---|
251 | static char CardTerminal_ResetCT (CardTerminal * ct, APDU_Cmd * cmd, APDU_Rsp ** rsp)
|
---|
252 | {
|
---|
253 | BYTE buffer[CARDTERMINAL_RESETCT_BUFFER_SIZE], p1, p2;
|
---|
254 | bool card, change;
|
---|
255 | unsigned sn, length;
|
---|
256 | void *atr;
|
---|
257 | char ret = OK;
|
---|
258 |
|
---|
259 | /* Get functional unit */
|
---|
260 | p1 = APDU_Cmd_P1 (cmd);
|
---|
261 |
|
---|
262 | /* Funcional unit is the card-terminal */
|
---|
263 | if (p1 == CTBCS_P1_CT_KERNEL)
|
---|
264 | {
|
---|
265 | /* Get command cualifier */
|
---|
266 | p2 = APDU_Cmd_P2 (cmd);
|
---|
267 |
|
---|
268 | /* Wrong command cualifier */
|
---|
269 | if (p2 != CTBCS_P2_RESET_NO_RESP && p2 != CTBCS_P2_RESET_GET_ATR && p2 != CTBCS_P2_RESET_GET_HIST)
|
---|
270 | {
|
---|
271 | buffer[0] = CTBCS_SW1_WRONG_PARAM;
|
---|
272 | buffer[1] = CTBCS_SW2_WRONG_PARAM;
|
---|
273 |
|
---|
274 | (*rsp) = APDU_Rsp_New (buffer, 2);
|
---|
275 | return OK;
|
---|
276 | }
|
---|
277 |
|
---|
278 | /* Close slots */
|
---|
279 | for (sn = 0; sn < ct->num_slots; sn++)
|
---|
280 | {
|
---|
281 | /* Close this slot */
|
---|
282 | ret = CT_Slot_Close (ct->slots[sn]);
|
---|
283 |
|
---|
284 | if (ret != OK)
|
---|
285 | {
|
---|
286 | buffer[0] = CTBCS_SW1_RESET_ERROR;
|
---|
287 | buffer[1] = CTBCS_SW2_RESET_ERROR;
|
---|
288 |
|
---|
289 | (*rsp) = APDU_Rsp_New (buffer, 2);
|
---|
290 | return ret;
|
---|
291 | }
|
---|
292 |
|
---|
293 | /* Initialise this slot */
|
---|
294 | ret = CT_Slot_Init (ct->slots[sn],ct->io,sn);
|
---|
295 |
|
---|
296 | if (ret != OK)
|
---|
297 | {
|
---|
298 | buffer[0] = CTBCS_SW1_RESET_ERROR;
|
---|
299 | buffer[1] = CTBCS_SW2_RESET_ERROR;
|
---|
300 |
|
---|
301 | (*rsp) = APDU_Rsp_New (buffer, 2);
|
---|
302 | return ret;
|
---|
303 | }
|
---|
304 | }
|
---|
305 |
|
---|
306 | length = 2;
|
---|
307 | buffer[0] = CTBCS_SW1_RESET_CT_OK;
|
---|
308 | buffer[1] = CTBCS_SW2_RESET_CT_OK;
|
---|
309 | }
|
---|
310 | else if ((p1 == CTBCS_P1_INTERFACE1) || (p1 == CTBCS_P1_INTERFACE2)) /* Funtional unit is an ICC */
|
---|
311 | {
|
---|
312 | /* Get slot number */
|
---|
313 | sn = (p1 == CTBCS_P1_INTERFACE1) ? 0 : 1;
|
---|
314 |
|
---|
315 | if (!(sn < ct->num_slots))
|
---|
316 | {
|
---|
317 | buffer[0] = CTBCS_SW1_WRONG_PARAM;
|
---|
318 | buffer[1] = CTBCS_SW2_WRONG_PARAM;
|
---|
319 |
|
---|
320 | (*rsp) = APDU_Rsp_New (buffer, 2);
|
---|
321 | return ERR_INVALID;
|
---|
322 | }
|
---|
323 |
|
---|
324 | /* Release the slot */
|
---|
325 | ret = CT_Slot_Release (ct->slots[sn]);
|
---|
326 |
|
---|
327 | if (ret != OK)
|
---|
328 | {
|
---|
329 | buffer[0] = CTBCS_SW1_RESET_ERROR;
|
---|
330 | buffer[1] = CTBCS_SW2_RESET_ERROR;
|
---|
331 |
|
---|
332 | (*rsp) = APDU_Rsp_New (buffer, 2);
|
---|
333 | return ret;
|
---|
334 | }
|
---|
335 |
|
---|
336 | /* Check for card */
|
---|
337 | ret = CT_Slot_Check (ct->slots[sn], 0, &card, &change);
|
---|
338 |
|
---|
339 | if (ret != OK)
|
---|
340 | {
|
---|
341 | buffer[0] = CTBCS_SW1_RESET_ERROR;
|
---|
342 | buffer[1] = CTBCS_SW2_RESET_ERROR;
|
---|
343 |
|
---|
344 | (*rsp) = APDU_Rsp_New (buffer, 2);
|
---|
345 | return ret;
|
---|
346 | }
|
---|
347 |
|
---|
348 | /* No card present */
|
---|
349 | if (!card)
|
---|
350 | {
|
---|
351 | buffer[0] = CTBCS_SW1_RESET_ERROR;
|
---|
352 | buffer[1] = CTBCS_SW2_RESET_ERROR;;
|
---|
353 |
|
---|
354 | (*rsp) = APDU_Rsp_New (buffer, 2);
|
---|
355 | return OK;
|
---|
356 | }
|
---|
357 |
|
---|
358 | /* Probe card type */
|
---|
359 | if (APDU_Cmd_Lc (cmd) > 1)
|
---|
360 | ret = CT_Slot_Probe (ct->slots[sn], APDU_Cmd_Data(cmd), APDU_Cmd_Lc(cmd));
|
---|
361 | else
|
---|
362 | ret = CT_Slot_Probe (ct->slots[sn], NULL, 0);
|
---|
363 |
|
---|
364 | if (ret != OK || (CT_Slot_GetICCType (ct->slots[sn]) == CT_SLOT_NULL))
|
---|
365 | {
|
---|
366 | buffer[0] = CTBCS_SW1_RESET_ERROR;
|
---|
367 | buffer[1] = CTBCS_SW2_RESET_ERROR;
|
---|
368 |
|
---|
369 | (*rsp) = APDU_Rsp_New (buffer, 2);
|
---|
370 | return ret;
|
---|
371 | }
|
---|
372 |
|
---|
373 | /* Get command cualifier */
|
---|
374 | p2 = APDU_Cmd_P2 (cmd);
|
---|
375 |
|
---|
376 | /* Do not return data */
|
---|
377 | if (p2 == CTBCS_P2_RESET_NO_RESP)
|
---|
378 | {
|
---|
379 | if (CT_Slot_GetICCType (ct->slots[sn]) == CT_SLOT_ICC_ASYNC)
|
---|
380 | {
|
---|
381 | buffer[0] = CTBCS_SW1_RESET_ASYNC_OK;
|
---|
382 | buffer[1] = CTBCS_SW2_RESET_ASYNC_OK;
|
---|
383 | length = 2;
|
---|
384 | }
|
---|
385 | else
|
---|
386 | {
|
---|
387 | buffer[0] = CTBCS_SW1_RESET_SYNC_OK;
|
---|
388 | buffer[1] = CTBCS_SW2_RESET_SYNC_OK;
|
---|
389 | length = 2;
|
---|
390 | }
|
---|
391 | }
|
---|
392 | else if (p2 == CTBCS_P2_RESET_GET_ATR) /* Return complete ATR of ICC */
|
---|
393 | {
|
---|
394 | atr = CT_Slot_GetAtr (ct->slots[sn]);
|
---|
395 |
|
---|
396 | if (CT_Slot_GetICCType (ct->slots[sn]) == CT_SLOT_ICC_ASYNC)
|
---|
397 | {
|
---|
398 | if (atr != NULL)
|
---|
399 | ATR_GetRaw ((ATR *) atr, buffer, &length);
|
---|
400 | else
|
---|
401 | length = 0;
|
---|
402 |
|
---|
403 | buffer[length] = CTBCS_SW1_RESET_ASYNC_OK;
|
---|
404 | buffer[length + 1] = CTBCS_SW2_RESET_ASYNC_OK;
|
---|
405 | // buffer[length + 1] = ct->slots[sn]->protocol_type;
|
---|
406 | length += 2;
|
---|
407 | }
|
---|
408 | }
|
---|
409 | else if (p2 == CTBCS_P2_RESET_GET_HIST) /* Return historical bytes of ATR */
|
---|
410 | {
|
---|
411 | atr = CT_Slot_GetAtr (ct->slots[sn]);
|
---|
412 |
|
---|
413 | if (CT_Slot_GetICCType (ct->slots[sn]) == CT_SLOT_ICC_ASYNC)
|
---|
414 | {
|
---|
415 | if (atr != NULL)
|
---|
416 | ATR_GetHistoricalBytes ((ATR *) atr, buffer, &length);
|
---|
417 | else
|
---|
418 | length = 0;
|
---|
419 |
|
---|
420 | buffer[length] = CTBCS_SW1_RESET_ASYNC_OK;
|
---|
421 | buffer[length + 1] = CTBCS_SW2_RESET_ASYNC_OK;
|
---|
422 | length += 2;
|
---|
423 | }
|
---|
424 | }
|
---|
425 | else /* Wrong command cualifier */
|
---|
426 | {
|
---|
427 | length = 2;
|
---|
428 | buffer[0] = CTBCS_SW1_WRONG_PARAM;
|
---|
429 | buffer[1] = CTBCS_SW2_WRONG_PARAM;
|
---|
430 | ret = OK;
|
---|
431 | }
|
---|
432 | }
|
---|
433 | else /* Wrong functional unit */
|
---|
434 | {
|
---|
435 | length = 2;
|
---|
436 | buffer[0] = CTBCS_SW1_WRONG_PARAM;
|
---|
437 | buffer[1] = CTBCS_SW2_WRONG_PARAM;
|
---|
438 | ret = OK;
|
---|
439 | }
|
---|
440 |
|
---|
441 | (*rsp) = APDU_Rsp_New (buffer, length);
|
---|
442 | return ret;
|
---|
443 | }
|
---|
444 |
|
---|
445 | static char CardTerminal_RequestICC (CardTerminal * ct, APDU_Cmd * cmd, APDU_Rsp ** rsp)
|
---|
446 | {
|
---|
447 | BYTE buffer[CARDTERMINAL_REQUESTICC_BUFFER_SIZE], p1, p2;
|
---|
448 | unsigned timeout, sn, length;
|
---|
449 | bool card, change;
|
---|
450 | void * atr;
|
---|
451 | char ret;
|
---|
452 |
|
---|
453 | /* Get functional unit */
|
---|
454 | p1 = APDU_Cmd_P1 (cmd);
|
---|
455 |
|
---|
456 | if ((p1 == CTBCS_P1_INTERFACE1) || (p1 == CTBCS_P1_INTERFACE2))
|
---|
457 | {
|
---|
458 | /* Get the slot number */
|
---|
459 | sn = (p1 == CTBCS_P1_INTERFACE1) ? 0 : 1;
|
---|
460 |
|
---|
461 | if (CT_Slot_GetICCType (ct->slots[sn]) != CT_SLOT_NULL)
|
---|
462 | {
|
---|
463 | buffer[0] = CTBCS_SW1_REQUEST_CARD_PRESENT;
|
---|
464 | buffer[1] = CTBCS_SW2_REQUEST_CARD_PRESENT;
|
---|
465 |
|
---|
466 | (*rsp) = APDU_Rsp_New (buffer, 2);
|
---|
467 | return OK;
|
---|
468 | }
|
---|
469 |
|
---|
470 | /* Get the card insertion timeout */
|
---|
471 | if (APDU_Cmd_Lc (cmd) == 1)
|
---|
472 | timeout = (APDU_Cmd_Data (cmd)[0]);
|
---|
473 | else
|
---|
474 | timeout = 0;
|
---|
475 |
|
---|
476 | /* Check for card */
|
---|
477 | ret = CT_Slot_Check (ct->slots[sn], timeout, &card, &change);
|
---|
478 |
|
---|
479 | if (ret != OK)
|
---|
480 | {
|
---|
481 | buffer[0] = CTBCS_SW1_REQUEST_ERROR;
|
---|
482 | buffer[1] = CTBCS_SW2_REQUEST_ERROR;
|
---|
483 |
|
---|
484 | (*rsp) = APDU_Rsp_New (buffer, 2);
|
---|
485 | return ret;
|
---|
486 | }
|
---|
487 |
|
---|
488 | /* No card present */
|
---|
489 | if (!card)
|
---|
490 | {
|
---|
491 | buffer[0] = CTBCS_SW1_REQUEST_NO_CARD;
|
---|
492 | buffer[1] = CTBCS_SW2_REQUEST_NO_CARD;
|
---|
493 |
|
---|
494 | (*rsp) = APDU_Rsp_New (buffer, 2);
|
---|
495 | return OK;
|
---|
496 | }
|
---|
497 |
|
---|
498 | /* Probe card type */
|
---|
499 | if (APDU_Cmd_Lc (cmd) > 1)
|
---|
500 | ret = CT_Slot_Probe (ct->slots[sn], APDU_Cmd_Data(cmd), APDU_Cmd_Lc(cmd));
|
---|
501 | else
|
---|
502 | ret = CT_Slot_Probe (ct->slots[sn], NULL, 0);
|
---|
503 |
|
---|
504 | if (ret != OK || (CT_Slot_GetICCType (ct->slots[sn]) == CT_SLOT_NULL))
|
---|
505 | {
|
---|
506 | buffer[0] = CTBCS_SW1_REQUEST_ERROR;
|
---|
507 | buffer[1] = CTBCS_SW2_REQUEST_ERROR;
|
---|
508 |
|
---|
509 | (*rsp) = APDU_Rsp_New (buffer, 2);
|
---|
510 | return ret;
|
---|
511 | }
|
---|
512 |
|
---|
513 | /* Get command cualifier */
|
---|
514 | p2 = APDU_Cmd_P2 (cmd);
|
---|
515 |
|
---|
516 | /* Do not return data */
|
---|
517 | if (p2 == CTBCS_P2_REQUEST_NO_RESP)
|
---|
518 | {
|
---|
519 | if (CT_Slot_GetICCType (ct->slots[sn]) == CT_SLOT_ICC_ASYNC)
|
---|
520 | {
|
---|
521 | buffer[0] = CTBCS_SW1_REQUEST_ASYNC_OK;
|
---|
522 | buffer[1] = CTBCS_SW2_REQUEST_ASYNC_OK;
|
---|
523 | length = 2;
|
---|
524 | }
|
---|
525 | else
|
---|
526 | {
|
---|
527 | buffer[0] = CTBCS_SW1_REQUEST_SYNC_OK;
|
---|
528 | buffer[1] = CTBCS_SW2_REQUEST_SYNC_OK;
|
---|
529 | length = 2;
|
---|
530 | }
|
---|
531 | }
|
---|
532 | else if (p2 == CTBCS_P2_REQUEST_GET_ATR) /* Return whole atr */
|
---|
533 | {
|
---|
534 | atr = CT_Slot_GetAtr (ct->slots[sn]);
|
---|
535 |
|
---|
536 | if (CT_Slot_GetICCType (ct->slots[sn]) == CT_SLOT_ICC_ASYNC)
|
---|
537 | {
|
---|
538 | if (atr != NULL)
|
---|
539 | ATR_GetRaw ((ATR *) atr, buffer, &length);
|
---|
540 | else
|
---|
541 | length = 0;
|
---|
542 |
|
---|
543 | buffer[length] = CTBCS_SW1_REQUEST_ASYNC_OK;
|
---|
544 | buffer[length + 1] = CTBCS_SW2_REQUEST_ASYNC_OK;
|
---|
545 | length += 2;
|
---|
546 | }
|
---|
547 | }
|
---|
548 | else if (p2 == CTBCS_P2_REQUEST_GET_HIST) /* Return historical bytes */
|
---|
549 | {
|
---|
550 | atr = CT_Slot_GetAtr (ct->slots[sn]);
|
---|
551 |
|
---|
552 | if (CT_Slot_GetICCType (ct->slots[sn]) == CT_SLOT_ICC_ASYNC)
|
---|
553 | {
|
---|
554 | if (atr != NULL)
|
---|
555 | ATR_GetHistoricalBytes ((ATR *) atr, buffer, &length);
|
---|
556 | else
|
---|
557 | length = 0;
|
---|
558 |
|
---|
559 | buffer[length] = CTBCS_SW1_REQUEST_ASYNC_OK;
|
---|
560 | buffer[length + 1] = CTBCS_SW2_REQUEST_ASYNC_OK;
|
---|
561 | length += 2;
|
---|
562 | }
|
---|
563 | }
|
---|
564 | else /* Wrong command cualifier */
|
---|
565 | {
|
---|
566 | length = 2;
|
---|
567 | buffer[0] = CTBCS_SW1_WRONG_PARAM;
|
---|
568 | buffer[1] = CTBCS_SW2_WRONG_PARAM;
|
---|
569 | ret = OK;
|
---|
570 | }
|
---|
571 | }
|
---|
572 | else /* Wrong functional unit */
|
---|
573 | {
|
---|
574 | length = 2;
|
---|
575 | buffer[0] = CTBCS_SW1_WRONG_PARAM;
|
---|
576 | buffer[1] = CTBCS_SW2_WRONG_PARAM;
|
---|
577 | ret = OK;
|
---|
578 | }
|
---|
579 |
|
---|
580 | (*rsp) = APDU_Rsp_New (buffer, (long)length);
|
---|
581 | return ret;
|
---|
582 | }
|
---|
583 |
|
---|
584 | static char CardTerminal_GetStatus (CardTerminal * ct, APDU_Cmd * cmd, APDU_Rsp ** rsp)
|
---|
585 | {
|
---|
586 | BYTE buffer[CARDTERMINAL_GETSTATUS_BUFFER_SIZE], p1, p2;
|
---|
587 | bool card, change;
|
---|
588 | unsigned int i;
|
---|
589 | unsigned length;
|
---|
590 | char ret = OK;
|
---|
591 |
|
---|
592 | /* Get functional unit */
|
---|
593 | p1 = APDU_Cmd_P1 (cmd);
|
---|
594 |
|
---|
595 | /* Wrong functional unit */
|
---|
596 | if (p1 != CTBCS_P1_CT_KERNEL)
|
---|
597 | {
|
---|
598 | length = 2;
|
---|
599 | buffer[0] = CTBCS_SW1_WRONG_PARAM;
|
---|
600 | buffer[1] = CTBCS_SW2_WRONG_PARAM;
|
---|
601 | ret = OK;
|
---|
602 | }
|
---|
603 |
|
---|
604 | /* Get command cualifier */
|
---|
605 | p2 = APDU_Cmd_P2 (cmd);
|
---|
606 |
|
---|
607 | if (p2 == CTBCS_P2_STATUS_MANUFACTURER)
|
---|
608 | {
|
---|
609 | length = 17;
|
---|
610 |
|
---|
611 | /* CT Manufacturer */
|
---|
612 | memcpy(buffer,CARDTERMINAL_MANUFACTURER,5);
|
---|
613 |
|
---|
614 | /* CT type */
|
---|
615 | if (ct->slots[0] != NULL)
|
---|
616 | CT_Slot_GetType(ct->slots[0], buffer + 5,5);
|
---|
617 |
|
---|
618 | /* CT version */
|
---|
619 | memcpy(buffer+10,VERSION,5);
|
---|
620 |
|
---|
621 | buffer[15] = CTBCS_SW1_OK;
|
---|
622 | buffer[16] = CTBCS_SW2_OK;
|
---|
623 | ret = OK;
|
---|
624 | }
|
---|
625 | else if (p2 == CTBCS_P2_STATUS_ICC)
|
---|
626 | {
|
---|
627 | for (i = 0; i < ct->num_slots; i++)
|
---|
628 | {
|
---|
629 | ret = CT_Slot_Check (ct->slots[i], 0, &card, &change);
|
---|
630 |
|
---|
631 | if (ret != OK)
|
---|
632 | {
|
---|
633 | /* There are no status bytes defined to be returned on error */
|
---|
634 | (*rsp) = NULL;
|
---|
635 | return ret;
|
---|
636 | }
|
---|
637 |
|
---|
638 | /* Resynchronise the driver status with the actual status of slot */
|
---|
639 | if ((CT_Slot_GetICCType (ct->slots[i]) != CT_SLOT_NULL) && (!card || change))
|
---|
640 | {
|
---|
641 | ret = CT_Slot_Release (ct->slots[i]);
|
---|
642 |
|
---|
643 | if (ret != OK)
|
---|
644 | {
|
---|
645 | (*rsp) = NULL;
|
---|
646 | return ret;
|
---|
647 | }
|
---|
648 | }
|
---|
649 |
|
---|
650 | buffer[i] = card? CTBCS_DATA_STATUS_CARD_CONNECT: CTBCS_DATA_STATUS_NOCARD;
|
---|
651 | }
|
---|
652 |
|
---|
653 | length = i+2;
|
---|
654 | buffer[i] = CTBCS_SW1_OK;
|
---|
655 | buffer[i+1] = CTBCS_SW2_OK;
|
---|
656 | }
|
---|
657 | else if (p2 == CTBCS_P2_STATUS_PROTOCOL)
|
---|
658 | {
|
---|
659 | for (i = 0; i < ct->num_slots; i++)
|
---|
660 | {
|
---|
661 | if(ct->slots[i]->protocol_type == CT_SLOT_PROTOCOL_T0)
|
---|
662 | buffer[i] = 0x00;
|
---|
663 | else if(ct->slots[i]->protocol_type == CT_SLOT_PROTOCOL_T1)
|
---|
664 | buffer[i] = 0x01;
|
---|
665 | else if(ct->slots[i]->protocol_type == CT_SLOT_PROTOCOL_T14)
|
---|
666 | buffer[i] = 0x0E;
|
---|
667 | else
|
---|
668 | buffer[i] = 0xFF;
|
---|
669 | }
|
---|
670 |
|
---|
671 | length = i+2;
|
---|
672 | buffer[i] = CTBCS_SW1_OK;
|
---|
673 | buffer[i+1] = CTBCS_SW2_OK;
|
---|
674 | ret = OK;
|
---|
675 | }
|
---|
676 | else /* Wrong command cualifier */
|
---|
677 | {
|
---|
678 | length = 2;
|
---|
679 | buffer[0] = CTBCS_SW1_WRONG_PARAM;
|
---|
680 | buffer[1] = CTBCS_SW2_WRONG_PARAM;
|
---|
681 | ret = OK;
|
---|
682 | }
|
---|
683 |
|
---|
684 | (*rsp) = APDU_Rsp_New (buffer, length);
|
---|
685 | return ret;
|
---|
686 | }
|
---|
687 |
|
---|
688 | static char CardTerminal_SetParity (CardTerminal * ct, APDU_Cmd * cmd, APDU_Rsp ** rsp)
|
---|
689 | {
|
---|
690 | BYTE buffer[2], p1, p2;
|
---|
691 | unsigned length;
|
---|
692 | char ret = OK;
|
---|
693 |
|
---|
694 | /* Get functional unit */
|
---|
695 | p1 = APDU_Cmd_P1 (cmd);
|
---|
696 |
|
---|
697 | /* Wrong functional unit */
|
---|
698 | if (p1 != CTBCS_P1_CT_KERNEL)
|
---|
699 | {
|
---|
700 | length = 2;
|
---|
701 | buffer[0] = CTBCS_SW1_WRONG_PARAM;
|
---|
702 | buffer[1] = CTBCS_SW2_WRONG_PARAM;
|
---|
703 | ret = OK;
|
---|
704 | }
|
---|
705 |
|
---|
706 | /* Get command cualifier */
|
---|
707 | p2 = APDU_Cmd_P2 (cmd);
|
---|
708 |
|
---|
709 | if (p2 == CTBCS_P2_PARITY_ODD)
|
---|
710 | {
|
---|
711 | length = 2;
|
---|
712 | IFD_Towitoko_SetParity(ct->slots[0]->ifd, IFD_TOWITOKO_PARITY_ODD);
|
---|
713 | buffer[0] = CTBCS_SW1_OK;
|
---|
714 | buffer[1] = CTBCS_SW2_OK;
|
---|
715 | ret = OK;
|
---|
716 | }
|
---|
717 | else if(p2 == CTBCS_P2_PARITY_EVEN)
|
---|
718 | {
|
---|
719 | length = 2;
|
---|
720 | IFD_Towitoko_SetParity(ct->slots[0]->ifd, IFD_TOWITOKO_PARITY_EVEN);
|
---|
721 | buffer[0] = CTBCS_SW1_OK;
|
---|
722 | buffer[1] = CTBCS_SW2_OK;
|
---|
723 | ret = OK;
|
---|
724 | }
|
---|
725 | else if (p2 == CTBCS_P2_PARITY_NONE)
|
---|
726 | {
|
---|
727 | length = 2;
|
---|
728 | IFD_Towitoko_SetParity(ct->slots[0]->ifd, IFD_TOWITOKO_PARITY_NONE);
|
---|
729 | buffer[0] = CTBCS_SW1_OK;
|
---|
730 | buffer[1] = CTBCS_SW2_OK;
|
---|
731 | ret = OK;
|
---|
732 | }
|
---|
733 | else /* Wrong command cualifier */
|
---|
734 | {
|
---|
735 | length = 2;
|
---|
736 | buffer[0] = CTBCS_SW1_WRONG_PARAM;
|
---|
737 | buffer[1] = CTBCS_SW2_WRONG_PARAM;
|
---|
738 | ret = OK;
|
---|
739 | }
|
---|
740 |
|
---|
741 | (*rsp) = APDU_Rsp_New (buffer, length);
|
---|
742 | return ret;
|
---|
743 | }
|
---|
744 |
|
---|
745 | static char CardTerminal_EjectICC (CardTerminal * ct, APDU_Cmd * cmd, APDU_Rsp ** rsp)
|
---|
746 | {
|
---|
747 | BYTE buffer[CARDTERMINAL_EJECTICC_BUFFER_SIZE], p1, p2;
|
---|
748 | unsigned int sn, timeout;
|
---|
749 | unsigned length;
|
---|
750 | bool card, change;
|
---|
751 | char ret;
|
---|
752 |
|
---|
753 | /* Get functional unit */
|
---|
754 | p1 = APDU_Cmd_P1 (cmd);
|
---|
755 |
|
---|
756 | /* Wrong functional unit */
|
---|
757 | if ((p1 != CTBCS_P1_INTERFACE1) && (p1 != CTBCS_P1_INTERFACE2))
|
---|
758 | {
|
---|
759 | buffer[0] = CTBCS_SW1_WRONG_PARAM;
|
---|
760 | buffer[1] = CTBCS_SW2_WRONG_PARAM;
|
---|
761 |
|
---|
762 | (*rsp) = APDU_Rsp_New (buffer, 2);
|
---|
763 | return OK;
|
---|
764 | }
|
---|
765 |
|
---|
766 | /* Get the slot number */
|
---|
767 | sn = (p1 == CTBCS_P1_INTERFACE1) ? 0 : 1;
|
---|
768 |
|
---|
769 | if (!(sn < ct->num_slots))
|
---|
770 | {
|
---|
771 | buffer[0] = CTBCS_SW1_WRONG_PARAM;
|
---|
772 | buffer[1] = CTBCS_SW2_WRONG_PARAM;
|
---|
773 |
|
---|
774 | (*rsp) = APDU_Rsp_New (buffer, 2);
|
---|
775 | return ERR_INVALID;
|
---|
776 | }
|
---|
777 |
|
---|
778 | /* Get command cualifier */
|
---|
779 | p2 = APDU_Cmd_P2 (cmd);
|
---|
780 |
|
---|
781 | #if 0
|
---|
782 | /* Wrong command cualifier */
|
---|
783 | if (p2 != 0)
|
---|
784 | {
|
---|
785 | buffer[0] = CTBCS_SW1_WRONG_PARAM;
|
---|
786 | buffer[1] = CTBCS_SW2_WRONG_PARAM;
|
---|
787 |
|
---|
788 | (*rsp) = APDU_Rsp_New (buffer, 2);
|
---|
789 | return OK;
|
---|
790 | }
|
---|
791 | #endif
|
---|
792 |
|
---|
793 | if (CT_Slot_GetICCType (ct->slots[sn]) == CT_SLOT_NULL)
|
---|
794 | {
|
---|
795 | buffer[0] = CTBCS_SW1_EJECT_OK;
|
---|
796 | buffer[1] = CTBCS_SW2_EJECT_OK;
|
---|
797 |
|
---|
798 | (*rsp) = APDU_Rsp_New (buffer, 2);
|
---|
799 | return OK;
|
---|
800 | }
|
---|
801 |
|
---|
802 | /* Get the card removal timeout */
|
---|
803 | if (APDU_Cmd_Lc (cmd) == 1)
|
---|
804 | timeout = (unsigned int) (*APDU_Cmd_Data (cmd));
|
---|
805 | else
|
---|
806 | timeout = 0;
|
---|
807 |
|
---|
808 | /* Check for card removal */
|
---|
809 | ret = CT_Slot_Check (ct->slots[sn], timeout, &card, &change);
|
---|
810 |
|
---|
811 | if (ret != OK)
|
---|
812 | {
|
---|
813 | (*rsp) = NULL;
|
---|
814 | return ret;
|
---|
815 | }
|
---|
816 |
|
---|
817 | /* Release ICC (always or only when card is removed?) */
|
---|
818 | ret = CT_Slot_Release (ct->slots[sn]);
|
---|
819 |
|
---|
820 | if (ret != OK)
|
---|
821 | {
|
---|
822 | (*rsp) = NULL;
|
---|
823 | return ret;
|
---|
824 | }
|
---|
825 |
|
---|
826 | if (timeout != 0)
|
---|
827 | {
|
---|
828 | if (card)
|
---|
829 | {
|
---|
830 | buffer[0] = CTBCS_SW1_EJECT_NOT_REMOVED;
|
---|
831 | buffer[1] = CTBCS_SW2_EJECT_NOT_REMOVED;
|
---|
832 | length = 2;
|
---|
833 | }
|
---|
834 | else
|
---|
835 | {
|
---|
836 | buffer[0] = CTBCS_SW1_EJECT_REMOVED;
|
---|
837 | buffer[1] = CTBCS_SW2_EJECT_REMOVED;
|
---|
838 | length = 2;
|
---|
839 | }
|
---|
840 | }
|
---|
841 | else
|
---|
842 | {
|
---|
843 | buffer[0] = CTBCS_SW1_EJECT_OK;;
|
---|
844 | buffer[1] = CTBCS_SW2_EJECT_OK;;
|
---|
845 | length = 2;
|
---|
846 | }
|
---|
847 |
|
---|
848 | (*rsp) = APDU_Rsp_New (buffer, length);
|
---|
849 | return ret;
|
---|
850 | }
|
---|
851 |
|
---|
852 | static void CardTerminal_Clear (CardTerminal * ct)
|
---|
853 | {
|
---|
854 | int i;
|
---|
855 |
|
---|
856 | ct->io = NULL;
|
---|
857 | ct->num_slots = 0;
|
---|
858 |
|
---|
859 | for (i = 0; i < CARDTERMINAL_MAX_SLOTS; i++)
|
---|
860 | ct->slots[i] = NULL;
|
---|
861 | }
|
---|