source: trunk/csctapi/atr.c@ 2934

Last change on this file since 2934 was 2934, checked in by rorothetroll, 10 years ago

more test for ATR checking. Check that TB1 is valid (<0x80)

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