source: trunk/csctapi/atr.c@ 67

Last change on this file since 67 was 67, checked in by rorothetroll, 12 years ago

csctapi/io_serial.[c,h] : added read/write serial routine for OS X
csctapi/ifd_towitoko.c : added calls to the new OS X routines inside #ifdefs
csctapi/atr.c : added calls to the new OS X routines inside #ifdefs

File size: 12.8 KB
Line 
1/*
2 atr.c
3 ISO 7816 ICC's answer to reset abstract data type implementation
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 "atr.h"
27#include <stdlib.h>
28#include <string.h>
29
30/*
31 * Not exported variables definition
32 */
33
34static unsigned long atr_fs_table[16] = {0, 5000000L, 6000000L, 8000000L, 12000000L, 16000000L, 20000000L, 0, 0, 5000000L, 7500000L, 10000000L, 15000000L, 20000000L, 0, 0};
35
36static unsigned atr_num_ib_table[16] = {0, 1, 1, 2, 1, 2, 2, 3, 1, 2, 2, 3, 2, 3, 3, 4};
37
38/*
39 * Exported variables definition
40 */
41
42unsigned atr_f_table[16] = {0, 372, 558, 744, 1116, 1488, 1860, 0, 0, 512, 768, 1024, 1536, 2048, 0, 0};
43
44double atr_d_table[16] = {0, 1, 2, 4, 8, 16, 0, 0, 0, 0, 0.5, 0.25, 125, 0.0625, 0.03125, 0.015625};
45
46unsigned atr_i_table[4] = {25, 50, 100, 0};
47
48/*
49 * Not exported functions declaration
50 */
51
52static bool ATR_GetNextByte (IO_Serial * io, unsigned timeout, BYTE * b, bool invert);
53
54/*
55 * Exported funcions definition
56 */
57
58ATR * ATR_New (void)
59{
60 ATR *atr;
61
62 /* Allocate memory */
63 atr = (ATR *) malloc (sizeof (ATR));
64
65 return atr;
66}
67
68int ATR_InitFromArray (ATR * atr, BYTE atr_buffer[ATR_MAX_SIZE], unsigned length)
69{
70 BYTE TDi;
71 BYTE buffer[ATR_MAX_SIZE];
72 unsigned pointer = 0, pn = 0;
73
74 /* Check size of buffer */
75 if (length < 2)
76 return (ATR_MALFORMED);
77
78 /* Check if ATR is from a inverse convention card */
79 if (atr_buffer[0] == 0x03)
80 {
81 for (pointer = 0; pointer < length; pointer++)
82 buffer[pointer] = ~(INVERT_BYTE (atr_buffer[pointer]));
83 }
84 else
85 {
86 memcpy (buffer, atr_buffer, length);
87 }
88
89 /* Store T0 and TS */
90 atr->TS = buffer[0];
91
92 atr->T0 = TDi = buffer[1];
93 pointer = 1;
94
95 /* Store number of historical bytes */
96 atr->hbn = TDi & 0x0F;
97
98 /* TCK is not present by default */
99 (atr->TCK).present = FALSE;
100
101 /* Extract interface bytes */
102 while (pointer < length)
103 {
104 /* Check buffer is long enought */
105 if (pointer + atr_num_ib_table[(0xF0 & TDi) >> 4] >= length)
106 {
107 return (ATR_MALFORMED);
108 }
109
110 /* Check TAi is present */
111 if ((TDi | 0xEF) == 0xFF)
112 {
113 pointer++;
114 atr->ib[pn][ATR_INTERFACE_BYTE_TA].value = buffer[pointer];
115 atr->ib[pn][ATR_INTERFACE_BYTE_TA].present = TRUE;
116 }
117 else
118 {
119 atr->ib[pn][ATR_INTERFACE_BYTE_TA].present = FALSE;
120 }
121
122 /* Check TBi is present */
123 if ((TDi | 0xDF) == 0xFF)
124 {
125 pointer++;
126 atr->ib[pn][ATR_INTERFACE_BYTE_TB].value = buffer[pointer];
127 atr->ib[pn][ATR_INTERFACE_BYTE_TB].present = TRUE;
128 }
129 else
130 {
131 atr->ib[pn][ATR_INTERFACE_BYTE_TB].present = FALSE;
132 }
133
134 /* Check TCi is present */
135 if ((TDi | 0xBF) == 0xFF)
136 {
137 pointer++;
138 atr->ib[pn][ATR_INTERFACE_BYTE_TC].value = buffer[pointer];
139 atr->ib[pn][ATR_INTERFACE_BYTE_TC].present = TRUE;
140 }
141 else
142 {
143 atr->ib[pn][ATR_INTERFACE_BYTE_TC].present = FALSE;
144 }
145
146 /* Read TDi if present */
147 if ((TDi | 0x7F) == 0xFF)
148 {
149 pointer++;
150 TDi = atr->ib[pn][ATR_INTERFACE_BYTE_TD].value = buffer[pointer];
151 atr->ib[pn][ATR_INTERFACE_BYTE_TD].present = TRUE;
152 (atr->TCK).present = ((TDi & 0x0F) != ATR_PROTOCOL_TYPE_T0);
153 if (pn >= ATR_MAX_PROTOCOLS)
154 return (ATR_MALFORMED);
155 pn++;
156 }
157 else
158 {
159 atr->ib[pn][ATR_INTERFACE_BYTE_TD].present = FALSE;
160 break;
161 }
162 }
163
164 /* Store number of protocols */
165 atr->pn = pn + 1;
166
167 /* Store historical bytes */
168 if (pointer + atr->hbn >= length)
169 return (ATR_MALFORMED);
170
171 memcpy (atr->hb, buffer + pointer + 1, atr->hbn);
172 pointer += (atr->hbn);
173
174 /* Store TCK */
175 if ((atr->TCK).present)
176 {
177 if (pointer + 1 >= length)
178 return (ATR_MALFORMED);
179
180 pointer++;
181
182 (atr->TCK).value = buffer[pointer];
183 }
184
185 atr->length = pointer + 1;
186 return (ATR_OK);
187}
188
189int ATR_InitFromStream (ATR * atr, IO_Serial * io, unsigned timeout)
190{
191 BYTE TDi;
192 unsigned pointer = 0, pn = 0, i;
193 bool invert;
194
195 invert = FALSE;
196
197 /* Store T0 and TS */
198 if (!ATR_GetNextByte (io, timeout, &(atr->TS), invert))
199 return ATR_IO_ERROR;
200
201 if (atr->TS == 0x03)
202 {
203 atr->TS = 0x3F;
204 invert = TRUE;
205 }
206
207 if ((atr->TS != 0x3B) && (atr->TS != 0x3F))
208 return ATR_MALFORMED;
209
210 if (!ATR_GetNextByte (io, timeout, &(atr->T0), invert))
211 return ATR_MALFORMED;
212
213 TDi = atr->T0;
214 pointer = 1;
215
216 /* Store number of historical bytes */
217 atr->hbn = TDi & 0x0F;
218
219 /* TCK is not present by default */
220 (atr->TCK).present = FALSE;
221
222 /* Extract interface bytes */
223 while (TRUE)
224 {
225 /* Check TAi is present */
226 if ((TDi | 0xEF) == 0xFF)
227 {
228 pointer++;
229 if (!ATR_GetNextByte(io, timeout, &(atr->ib[pn][ATR_INTERFACE_BYTE_TA].value),invert))
230 return ATR_MALFORMED;
231 atr->ib[pn][ATR_INTERFACE_BYTE_TA].present = TRUE;
232 }
233 else
234 {
235 atr->ib[pn][ATR_INTERFACE_BYTE_TA].present = FALSE;
236 }
237
238 /* Check TBi is present */
239 if ((TDi | 0xDF) == 0xFF)
240 {
241 pointer++;
242 if (!ATR_GetNextByte(io, timeout, &(atr->ib[pn][ATR_INTERFACE_BYTE_TB].value),invert))
243 return ATR_MALFORMED;
244 atr->ib[pn][ATR_INTERFACE_BYTE_TB].present = TRUE;
245 }
246 else
247 {
248 atr->ib[pn][ATR_INTERFACE_BYTE_TB].present = FALSE;
249 }
250
251 /* Check TCi is present */
252 if ((TDi | 0xBF) == 0xFF)
253 {
254 pointer++;
255 if (!ATR_GetNextByte(io, timeout, &(atr->ib[pn][ATR_INTERFACE_BYTE_TC].value), invert))
256 return ATR_MALFORMED;
257 atr->ib[pn][ATR_INTERFACE_BYTE_TC].present = TRUE;
258 }
259 else
260 {
261 atr->ib[pn][ATR_INTERFACE_BYTE_TC].present = FALSE;
262 }
263
264 /* Read TDi if present */
265 if ((TDi | 0x7F) == 0xFF)
266 {
267 pointer++;
268 if (!ATR_GetNextByte(io, timeout, &(atr->ib[pn][ATR_INTERFACE_BYTE_TD].value), invert))
269 return ATR_MALFORMED;
270 TDi = atr->ib[pn][ATR_INTERFACE_BYTE_TD].value;
271 atr->ib[pn][ATR_INTERFACE_BYTE_TD].present = TRUE;
272 (atr->TCK).present = ((TDi & 0x0F) != ATR_PROTOCOL_TYPE_T0);
273 if (pn >= ATR_MAX_PROTOCOLS)
274 return (ATR_MALFORMED);
275 pn++;
276 }
277 else
278 {
279 atr->ib[pn][ATR_INTERFACE_BYTE_TD].present = FALSE;
280 break;
281 }
282 }
283
284 /* Store number of protocols */
285 atr->pn = pn + 1;
286
287 /* Store historical bytes */
288 for (i = 0; i < (atr->hbn); i++)
289 {
290 if (!ATR_GetNextByte (io, timeout, &(atr->hb[i]), invert))
291 return ATR_MALFORMED;
292 }
293
294 pointer += (atr->hbn);
295
296 /* Store TCK */
297 if ((atr->TCK).present)
298 {
299 pointer++;
300
301 if (!ATR_GetNextByte (io, timeout, (&((atr->TCK).value)), invert))
302 return ATR_MALFORMED;
303 }
304
305 atr->length = pointer + 1;
306 return (ATR_OK);
307
308}
309
310void ATR_Delete (ATR * atr)
311{
312 free (atr);
313}
314
315int ATR_GetConvention (ATR * atr, int *convention)
316{
317 if (atr->TS == 0x3B)
318 (*convention) = ATR_CONVENTION_DIRECT;
319 else if (atr->TS == 0x3F)
320 (*convention) = ATR_CONVENTION_INVERSE;
321 else
322 return (ATR_MALFORMED);
323
324 return (ATR_OK);
325}
326
327int ATR_GetSize (ATR * atr, unsigned *size)
328{
329 (*size) = atr->length;
330 return (ATR_OK);
331}
332
333int ATR_GetNumberOfProtocols (ATR * atr, unsigned *number_protocols)
334{
335 (*number_protocols) = atr->pn;
336 return (ATR_OK);
337}
338
339int ATR_GetProtocolType (ATR * atr, unsigned number_protocol, BYTE *protocol_type)
340{
341 if ((number_protocol > atr->pn) || number_protocol < 2)
342 return ATR_NOT_FOUND;
343
344 if (atr->ib[number_protocol - 2][ATR_INTERFACE_BYTE_TD].present)
345 (*protocol_type) = (atr->ib[number_protocol - 2][ATR_INTERFACE_BYTE_TD].value & 0x0F);
346 else
347 (*protocol_type) = ATR_PROTOCOL_TYPE_T0;
348
349 return (ATR_OK);
350}
351
352int ATR_GetInterfaceByte (ATR * atr, unsigned number, int character, BYTE * value)
353{
354 if (number > atr->pn || number < 1)
355 return (ATR_NOT_FOUND);
356
357 if (atr->ib[number - 1][character].present && (character == ATR_INTERFACE_BYTE_TA || character == ATR_INTERFACE_BYTE_TB || character == ATR_INTERFACE_BYTE_TC || character == ATR_INTERFACE_BYTE_TD))
358 (*value) = atr->ib[number - 1][character].value;
359 else
360 return (ATR_NOT_FOUND);
361
362 return (ATR_OK);
363}
364
365int ATR_GetIntegerValue (ATR * atr, int name, BYTE * value)
366{
367 int ret;
368
369 if (name == ATR_INTEGER_VALUE_FI)
370 {
371 if (atr->ib[0][ATR_INTERFACE_BYTE_TA].present)
372 {
373 (*value) = (atr->ib[0][ATR_INTERFACE_BYTE_TA].value & 0xF0) >> 4;
374 ret = ATR_OK;
375 }
376 else
377 {
378 ret = ATR_NOT_FOUND;
379 }
380 }
381 else if (name == ATR_INTEGER_VALUE_DI)
382 {
383 if (atr->ib[0][ATR_INTERFACE_BYTE_TA].present)
384 {
385 (*value) = (atr->ib[0][ATR_INTERFACE_BYTE_TA].value & 0x0F);
386 ret = ATR_OK;
387 }
388 else
389 {
390 ret = ATR_NOT_FOUND;
391 }
392 }
393 else if (name == ATR_INTEGER_VALUE_II)
394 {
395 if (atr->ib[0][ATR_INTERFACE_BYTE_TB].present)
396 {
397 (*value) = (atr->ib[0][ATR_INTERFACE_BYTE_TB].value & 0x60) >> 5;
398 ret = ATR_OK;
399 }
400 else
401 {
402 ret = ATR_NOT_FOUND;
403 }
404 }
405 else if (name == ATR_INTEGER_VALUE_PI1)
406 {
407 if (atr->ib[0][ATR_INTERFACE_BYTE_TB].present)
408 {
409 (*value) = (atr->ib[0][ATR_INTERFACE_BYTE_TB].value & 0x1F);
410 ret = ATR_OK;
411 }
412 else
413 {
414 ret = ATR_NOT_FOUND;
415 }
416 }
417 else if (name == ATR_INTEGER_VALUE_PI2)
418 {
419 if (atr->ib[1][ATR_INTERFACE_BYTE_TB].present)
420 {
421 (*value) = atr->ib[1][ATR_INTERFACE_BYTE_TB].value;
422 ret = ATR_OK;
423 }
424 else
425 {
426 ret = ATR_NOT_FOUND;
427 }
428 }
429 else if (name == ATR_INTEGER_VALUE_N)
430 {
431 if (atr->ib[0][ATR_INTERFACE_BYTE_TC].present)
432 {
433 (*value) = atr->ib[0][ATR_INTERFACE_BYTE_TC].value;
434 ret = ATR_OK;
435 }
436 else
437 {
438 ret = ATR_NOT_FOUND;
439 }
440 }
441 else
442 {
443 ret = ATR_NOT_FOUND;
444 }
445
446 return ret;
447}
448
449int ATR_GetParameter (ATR * atr, int name, double *parameter)
450{
451 BYTE FI, DI, II, PI1, PI2, N;
452
453 if (name == ATR_PARAMETER_F)
454 {
455 if (ATR_GetIntegerValue (atr, ATR_INTEGER_VALUE_FI, &FI) == ATR_OK)
456 (*parameter) = (double) (atr_f_table[FI]);
457 else
458 (*parameter) = (double) ATR_DEFAULT_F;
459 return (ATR_OK);
460 }
461 else if (name == ATR_PARAMETER_D)
462 {
463 if (ATR_GetIntegerValue (atr, ATR_INTEGER_VALUE_DI, &DI) == ATR_OK)
464 (*parameter) = (double) (atr_d_table[DI]);
465 else
466 (*parameter) = (double) ATR_DEFAULT_D;
467 return (ATR_OK);
468 }
469 else if (name == ATR_PARAMETER_I)
470 {
471 if (ATR_GetIntegerValue (atr, ATR_INTEGER_VALUE_II, &II) == ATR_OK)
472 (*parameter) = (double) (atr_i_table[II]);
473 else
474 (*parameter) = ATR_DEFAULT_I;
475 return (ATR_OK);
476 }
477 else if (name == ATR_PARAMETER_P)
478 {
479 if (ATR_GetIntegerValue (atr, ATR_INTEGER_VALUE_PI2, &PI2) == ATR_OK)
480 (*parameter) = (double) PI2;
481 else if (ATR_GetIntegerValue (atr, ATR_INTEGER_VALUE_PI1, &PI1) == ATR_OK)
482 (*parameter) = (double) PI1;
483 else
484 (*parameter) = (double) ATR_DEFAULT_P;
485 return (ATR_OK);
486 }
487 else if (name == ATR_PARAMETER_N)
488 {
489 if (ATR_GetIntegerValue (atr, ATR_INTEGER_VALUE_N, &N) == ATR_OK)
490 (*parameter) = (double) N;
491 else
492 (*parameter) = (double) ATR_DEFAULT_N;
493 return (ATR_OK);
494 }
495
496 return (ATR_NOT_FOUND);
497}
498
499int ATR_GetHistoricalBytes (ATR * atr, BYTE hist[ATR_MAX_HISTORICAL], unsigned *length)
500{
501 if (atr->hbn == 0)
502 return (ATR_NOT_FOUND);
503
504 (*length) = atr->hbn;
505 memcpy (hist, atr->hb, atr->hbn);
506 return (ATR_OK);
507}
508
509int ATR_GetRaw (ATR * atr, BYTE buffer[ATR_MAX_SIZE], unsigned *length)
510{
511 unsigned i, j;
512
513 buffer[0] = atr->TS;
514 buffer[1] = atr->T0;
515
516 j = 2;
517
518 for (i = 0; i < atr->pn; i++)
519 {
520 if (atr->ib[i][ATR_INTERFACE_BYTE_TA].present)
521 buffer[j++] = atr->ib[i][ATR_INTERFACE_BYTE_TA].value;
522
523 if (atr->ib[i][ATR_INTERFACE_BYTE_TB].present)
524 buffer[j++] = atr->ib[i][ATR_INTERFACE_BYTE_TB].value;
525
526 if (atr->ib[i][ATR_INTERFACE_BYTE_TC].present)
527 buffer[j++] = atr->ib[i][ATR_INTERFACE_BYTE_TC].value;
528
529 if (atr->ib[i][ATR_INTERFACE_BYTE_TD].present)
530 buffer[j++] = atr->ib[i][ATR_INTERFACE_BYTE_TD].value;
531 }
532
533 if (atr->hbn > 0)
534 {
535 memcpy (&(buffer[j]), atr->hb, atr->hbn);
536 j += atr->hbn;
537 }
538
539 if ((atr->TCK).present)
540 buffer[j++] = (atr->TCK).value;
541
542 (*length) = j;
543
544 return ATR_OK;
545}
546
547int ATR_GetCheckByte (ATR * atr, BYTE * check_byte)
548{
549 if (!((atr->TCK).present))
550 return (ATR_NOT_FOUND);
551
552 (*check_byte) = (atr->TCK).value;
553 return (ATR_OK);
554}
555
556int ATR_GetFsMax (ATR * atr, unsigned long *fsmax)
557{
558 BYTE FI;
559
560 if (ATR_GetIntegerValue (atr, ATR_INTEGER_VALUE_FI, &FI) == ATR_OK)
561 (*fsmax) = atr_fs_table[FI];
562 else
563 (*fsmax) = atr_fs_table[1];
564
565 return (ATR_OK);
566}
567
568/*
569 * Not exported functions definition
570 */
571
572static bool ATR_GetNextByte (IO_Serial * io, unsigned timeout, BYTE * byte, bool invert)
573{
574 bool ret;
575#ifdef OS_MACOSX
576 ret = IO_Serial_Read_MacOSX (io, timeout, 1, byte);
577#else
578 ret = IO_Serial_Read (io, timeout, 1, byte);
579#endif
580 /* Para tarjetas inversas quiza */
581 if (invert)
582 (*byte) = ~(INVERT_BYTE (*byte));
583
584 return ret;
585}
Note: See TracBrowser for help on using the repository browser.