source: trunk/csctapi/atr.c@ 3181

Last change on this file since 3181 was 3181, checked in by dingo35, 10 years ago

Adding threadsafety FIXMEs, feel free to join checking..

File size: 10.8 KB
Line 
1//FIXME Not checked on threadsafety yet; after checking please remove this line
2/*
3 atr.c
4 ISO 7816 ICC's answer to reset abstract data type implementation
5
6 This file is part of the Unix driver for Towitoko smartcard readers
7 Copyright (C) 2000 Carlos Prados <cprados@yahoo.com>
8
9 This version is modified by doz21 to work in a special manner ;)
10
11 This library is free software; you can redistribute it and/or
12 modify it under the terms of the GNU Lesser General Public
13 License as published by the Free Software Foundation; either
14 version 2 of the License, or (at your option) any later version.
15
16 This library is distributed in the hope that it will be useful,
17 but WITHOUT ANY WARRANTY; without even the implied warranty of
18 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19 Lesser General Public License for more details.
20
21 You should have received a copy of the GNU Lesser General Public
22 License along with this library; if not, write to the Free Software
23 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
24*/
25
26#include <stdlib.h>
27#include <string.h>
28#include "../globals.h"
29#include "defines.h"
30#include "atr.h"
31
32/*
33 * Not exported variables definition
34 */
35
36unsigned long atr_fs_table[16] = {4000000L, 5000000L, 6000000L, 8000000L, 12000000L, 16000000L, 20000000L, 0, 0, 5000000L, 7500000L, 10000000L, 15000000L, 20000000L, 0, 0};
37
38static unsigned atr_num_ib_table[16] = {0, 1, 1, 2, 1, 2, 2, 3, 1, 2, 2, 3, 2, 3, 3, 4};
39
40/*
41 * Exported variables definition
42 */
43
44unsigned atr_f_table[16] = {372, 372, 558, 744, 1116, 1488, 1860, 0, 0, 512, 768, 1024, 1536, 2048, 0, 0};
45
46double atr_d_table[16] = {0, 1, 2, 4, 8, 16, 32, 64, 12, 20, 0.5, 0.25, 0.125, 0.0625, 0.03125, 0.015625};
47//old table has 0 for RFU:
48//double 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};
49
50unsigned atr_i_table[4] = {25, 50, 100, 0};
51
52/*
53 * Exported funcions definition
54 */
55
56int ATR_InitFromArray (ATR * atr, BYTE atr_buffer[ATR_MAX_SIZE], unsigned length)
57{
58 BYTE TDi;
59 BYTE buffer[ATR_MAX_SIZE];
60 unsigned pointer = 0, pn = 0;
61
62 /* Check size of buffer */
63 if (length < 2)
64 return (ATR_MALFORMED);
65
66 /* Check if ATR is from a inverse convention card */
67 if (atr_buffer[0] == 0x03)
68 {
69 for (pointer = 0; pointer < length; pointer++)
70 buffer[pointer] = ~(INVERT_BYTE (atr_buffer[pointer]));
71 }
72 else
73 {
74 memcpy (buffer, atr_buffer, length);
75 }
76
77 /* Store T0 and TS */
78 atr->TS = buffer[0];
79
80 atr->T0 = TDi = buffer[1];
81 pointer = 1;
82
83 /* Store number of historical bytes */
84 atr->hbn = TDi & 0x0F;
85
86 /* TCK is not present by default */
87 (atr->TCK).present = FALSE;
88
89 /* Extract interface bytes */
90 while (pointer < length)
91 {
92 /* Check buffer is long enought */
93 if (pointer + atr_num_ib_table[(0xF0 & TDi) >> 4] >= length)
94 {
95 return (ATR_MALFORMED);
96 }
97
98 /* Check TAi is present */
99 if ((TDi | 0xEF) == 0xFF)
100 {
101 pointer++;
102 atr->ib[pn][ATR_INTERFACE_BYTE_TA].value = buffer[pointer];
103 atr->ib[pn][ATR_INTERFACE_BYTE_TA].present = TRUE;
104 }
105 else
106 {
107 atr->ib[pn][ATR_INTERFACE_BYTE_TA].present = FALSE;
108 }
109
110 /* Check TBi is present */
111 if ((TDi | 0xDF) == 0xFF)
112 {
113 pointer++;
114 atr->ib[pn][ATR_INTERFACE_BYTE_TB].value = buffer[pointer];
115 atr->ib[pn][ATR_INTERFACE_BYTE_TB].present = TRUE;
116 }
117 else
118 {
119 atr->ib[pn][ATR_INTERFACE_BYTE_TB].present = FALSE;
120 }
121
122 /* Check TCi is present */
123 if ((TDi | 0xBF) == 0xFF)
124 {
125 pointer++;
126 atr->ib[pn][ATR_INTERFACE_BYTE_TC].value = buffer[pointer];
127 atr->ib[pn][ATR_INTERFACE_BYTE_TC].present = TRUE;
128 }
129 else
130 {
131 atr->ib[pn][ATR_INTERFACE_BYTE_TC].present = FALSE;
132 }
133
134 /* Read TDi if present */
135 if ((TDi | 0x7F) == 0xFF)
136 {
137 pointer++;
138 TDi = atr->ib[pn][ATR_INTERFACE_BYTE_TD].value = buffer[pointer];
139 atr->ib[pn][ATR_INTERFACE_BYTE_TD].present = TRUE;
140 (atr->TCK).present = ((TDi & 0x0F) != ATR_PROTOCOL_TYPE_T0);
141 if (pn >= ATR_MAX_PROTOCOLS)
142 return (ATR_MALFORMED);
143 pn++;
144 }
145 else
146 {
147 atr->ib[pn][ATR_INTERFACE_BYTE_TD].present = FALSE;
148 break;
149 }
150 }
151
152 /* Store number of protocols */
153 atr->pn = pn + 1;
154
155 /* Store historical bytes */
156 if (pointer + atr->hbn >= length) {
157 cs_log("ATR is malformed, it reports %i historical bytes but there are only %i",atr->hbn, length-pointer-2);
158 if (length-pointer >= 2)
159 atr->hbn = length-pointer-2;
160 else {
161 atr->hbn = 0;
162 atr->length = pointer + 1;
163 return (ATR_MALFORMED);
164 }
165
166 }
167
168 memcpy (atr->hb, buffer + pointer + 1, atr->hbn);
169 pointer += (atr->hbn);
170
171 /* Store TCK */
172 if ((atr->TCK).present)
173 {
174 if (pointer + 1 >= length)
175 return (ATR_MALFORMED);
176
177 pointer++;
178
179 (atr->TCK).value = buffer[pointer];
180 }
181
182 atr->length = pointer + 1;
183
184 // check that TA1, if pn==1 , has a valid value for FI
185 if ( (atr->pn==1) && (atr->ib[pn][ATR_INTERFACE_BYTE_TA].present == TRUE)) {
186 uchar FI;
187 cs_debug("TA1 = %02x",atr->ib[pn][ATR_INTERFACE_BYTE_TA].value);
188 FI=(atr->ib[pn][ATR_INTERFACE_BYTE_TA].value & 0xF0)>>4;
189 cs_debug("FI = %02x",FI);
190 if(atr_fs_table[FI]==0) {
191 cs_debug("Invalid ATR as FI is not returning a valid frequency value");
192 return (ATR_MALFORMED);
193 }
194 }
195
196 // check that TB1 < 0x80
197 if ( (atr->pn==1) && (atr->ib[pn][ATR_INTERFACE_BYTE_TB].present == TRUE)) {
198 if(atr->ib[pn][ATR_INTERFACE_BYTE_TB].value > 0x80) {
199 cs_debug("Invalid ATR as TB1 has an invalid value");
200 return (ATR_MALFORMED);
201 }
202 }
203 return (ATR_OK);
204}
205
206int ATR_GetConvention (ATR * atr, int *convention)
207{
208 if (atr->TS == 0x3B)
209 (*convention) = ATR_CONVENTION_DIRECT;
210 else if (atr->TS == 0x3F)
211 (*convention) = ATR_CONVENTION_INVERSE;
212 else
213 return (ATR_MALFORMED);
214
215 return (ATR_OK);
216}
217
218int ATR_GetSize (ATR * atr, unsigned *size)
219{
220 (*size) = atr->length;
221 return (ATR_OK);
222}
223
224int ATR_GetNumberOfProtocols (ATR * atr, unsigned *number_protocols)
225{
226 (*number_protocols) = atr->pn;
227 return (ATR_OK);
228}
229
230int ATR_GetProtocolType (ATR * atr, unsigned number_protocol, BYTE *protocol_type)
231{
232 if ((number_protocol > atr->pn) || number_protocol < 1)
233 return ATR_NOT_FOUND;
234
235 if (atr->ib[number_protocol - 1][ATR_INTERFACE_BYTE_TD].present)
236 (*protocol_type) = (atr->ib[number_protocol - 1][ATR_INTERFACE_BYTE_TD].value & 0x0F);
237 else
238 (*protocol_type) = ATR_PROTOCOL_TYPE_T0;
239
240 return (ATR_OK);
241}
242
243int ATR_GetInterfaceByte (ATR * atr, unsigned number, int character, BYTE * value)
244{
245 if (number > atr->pn || number < 1)
246 return (ATR_NOT_FOUND);
247
248 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))
249 (*value) = atr->ib[number - 1][character].value;
250 else
251 return (ATR_NOT_FOUND);
252
253 return (ATR_OK);
254}
255
256int ATR_GetIntegerValue (ATR * atr, int name, BYTE * value)
257{
258 int ret;
259
260 if (name == ATR_INTEGER_VALUE_FI)
261 {
262 if (atr->ib[0][ATR_INTERFACE_BYTE_TA].present)
263 {
264 (*value) = (atr->ib[0][ATR_INTERFACE_BYTE_TA].value & 0xF0) >> 4;
265 ret = ATR_OK;
266 }
267 else
268 {
269 ret = ATR_NOT_FOUND;
270 }
271 }
272 else if (name == ATR_INTEGER_VALUE_DI)
273 {
274 if (atr->ib[0][ATR_INTERFACE_BYTE_TA].present)
275 {
276 (*value) = (atr->ib[0][ATR_INTERFACE_BYTE_TA].value & 0x0F);
277 ret = ATR_OK;
278 }
279 else
280 {
281 ret = ATR_NOT_FOUND;
282 }
283 }
284 else if (name == ATR_INTEGER_VALUE_II)
285 {
286 if (atr->ib[0][ATR_INTERFACE_BYTE_TB].present)
287 {
288 (*value) = (atr->ib[0][ATR_INTERFACE_BYTE_TB].value & 0x60) >> 5;
289 ret = ATR_OK;
290 }
291 else
292 {
293 ret = ATR_NOT_FOUND;
294 }
295 }
296 else if (name == ATR_INTEGER_VALUE_PI1)
297 {
298 if (atr->ib[0][ATR_INTERFACE_BYTE_TB].present)
299 {
300 (*value) = (atr->ib[0][ATR_INTERFACE_BYTE_TB].value & 0x1F);
301 ret = ATR_OK;
302 }
303 else
304 {
305 ret = ATR_NOT_FOUND;
306 }
307 }
308 else if (name == ATR_INTEGER_VALUE_PI2)
309 {
310 if (atr->ib[1][ATR_INTERFACE_BYTE_TB].present)
311 {
312 (*value) = atr->ib[1][ATR_INTERFACE_BYTE_TB].value;
313 ret = ATR_OK;
314 }
315 else
316 {
317 ret = ATR_NOT_FOUND;
318 }
319 }
320 else if (name == ATR_INTEGER_VALUE_N)
321 {
322 if (atr->ib[0][ATR_INTERFACE_BYTE_TC].present)
323 {
324 (*value) = atr->ib[0][ATR_INTERFACE_BYTE_TC].value;
325 ret = ATR_OK;
326 }
327 else
328 {
329 ret = ATR_NOT_FOUND;
330 }
331 }
332 else
333 {
334 ret = ATR_NOT_FOUND;
335 }
336
337 return ret;
338}
339
340int ATR_GetParameter (ATR * atr, int name, double *parameter)
341{
342 BYTE FI, DI, II, PI1, PI2, N;
343
344 if (name == ATR_PARAMETER_F)
345 {
346 if (ATR_GetIntegerValue (atr, ATR_INTEGER_VALUE_FI, &FI) != ATR_OK)
347 FI = ATR_DEFAULT_FI;
348 (*parameter) = (double) (atr_f_table[FI]);
349 return (ATR_OK);
350 }
351 else if (name == ATR_PARAMETER_D)
352 {
353 if (ATR_GetIntegerValue (atr, ATR_INTEGER_VALUE_DI, &DI) == ATR_OK)
354 (*parameter) = (double) (atr_d_table[DI]);
355 else
356 (*parameter) = (double) ATR_DEFAULT_D;
357 return (ATR_OK);
358 }
359 else if (name == ATR_PARAMETER_I)
360 {
361 if (ATR_GetIntegerValue (atr, ATR_INTEGER_VALUE_II, &II) == ATR_OK)
362 (*parameter) = (double) (atr_i_table[II]);
363 else
364 (*parameter) = ATR_DEFAULT_I;
365 return (ATR_OK);
366 }
367 else if (name == ATR_PARAMETER_P)
368 {
369 if (ATR_GetIntegerValue (atr, ATR_INTEGER_VALUE_PI2, &PI2) == ATR_OK)
370 (*parameter) = (double) PI2;
371 else if (ATR_GetIntegerValue (atr, ATR_INTEGER_VALUE_PI1, &PI1) == ATR_OK)
372 (*parameter) = (double) PI1;
373 else
374 (*parameter) = (double) ATR_DEFAULT_P;
375 return (ATR_OK);
376 }
377 else if (name == ATR_PARAMETER_N)
378 {
379 if (ATR_GetIntegerValue (atr, ATR_INTEGER_VALUE_N, &N) == ATR_OK)
380 (*parameter) = (double) N;
381 else
382 (*parameter) = (double) ATR_DEFAULT_N;
383 return (ATR_OK);
384 }
385
386 return (ATR_NOT_FOUND);
387}
388
389int ATR_GetHistoricalBytes (ATR * atr, BYTE hist[ATR_MAX_HISTORICAL], unsigned *length)
390{
391 if (atr->hbn == 0)
392 return (ATR_NOT_FOUND);
393
394 (*length) = atr->hbn;
395 memcpy (hist, atr->hb, atr->hbn);
396 return (ATR_OK);
397}
398
399int ATR_GetRaw (ATR * atr, BYTE buffer[ATR_MAX_SIZE], unsigned *length)
400{
401 unsigned i, j;
402
403 buffer[0] = atr->TS;
404 buffer[1] = atr->T0;
405
406 j = 2;
407
408 for (i = 0; i < atr->pn; i++)
409 {
410 if (atr->ib[i][ATR_INTERFACE_BYTE_TA].present)
411 buffer[j++] = atr->ib[i][ATR_INTERFACE_BYTE_TA].value;
412
413 if (atr->ib[i][ATR_INTERFACE_BYTE_TB].present)
414 buffer[j++] = atr->ib[i][ATR_INTERFACE_BYTE_TB].value;
415
416 if (atr->ib[i][ATR_INTERFACE_BYTE_TC].present)
417 buffer[j++] = atr->ib[i][ATR_INTERFACE_BYTE_TC].value;
418
419 if (atr->ib[i][ATR_INTERFACE_BYTE_TD].present)
420 buffer[j++] = atr->ib[i][ATR_INTERFACE_BYTE_TD].value;
421 }
422
423 if (atr->hbn > 0)
424 {
425 memcpy (&(buffer[j]), atr->hb, atr->hbn);
426 j += atr->hbn;
427 }
428
429 if ((atr->TCK).present)
430 buffer[j++] = (atr->TCK).value;
431
432 (*length) = j;
433
434 return ATR_OK;
435}
436
437int ATR_GetCheckByte (ATR * atr, BYTE * check_byte)
438{
439 if (!((atr->TCK).present))
440 return (ATR_NOT_FOUND);
441
442 (*check_byte) = (atr->TCK).value;
443 return (ATR_OK);
444}
445
446int ATR_GetFsMax (ATR * atr, unsigned long *fsmax)
447{
448 BYTE FI;
449
450 if (ATR_GetIntegerValue (atr, ATR_INTEGER_VALUE_FI, &FI) == ATR_OK)
451 (*fsmax) = atr_fs_table[FI];
452 else
453 (*fsmax) = atr_fs_table[1];
454
455 return (ATR_OK);
456}
Note: See TracBrowser for help on using the repository browser.