1 |
|
---|
2 | /*
|
---|
3 | pps.c
|
---|
4 | Protocol Parameters Selection
|
---|
5 |
|
---|
6 | This file is part of the Unix driver for Towitoko smartcard readers
|
---|
7 | Copyright (C) 2000 2001 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 "pps.h"
|
---|
27 | #include "atr.h"
|
---|
28 | #include "protocol_t0.h"
|
---|
29 | #include "protocol_t1.h"
|
---|
30 | #include <stdlib.h>
|
---|
31 | #include <stdio.h>
|
---|
32 | #include <string.h>
|
---|
33 |
|
---|
34 | /*
|
---|
35 | * Not exported constants definition
|
---|
36 | */
|
---|
37 |
|
---|
38 | #define PPS_DEFAULT_PROTOCOL 0x00
|
---|
39 |
|
---|
40 | /*
|
---|
41 | * Not exported macros definition
|
---|
42 | */
|
---|
43 |
|
---|
44 | #define PPS_HAS_PPS1(block) ((block[1] & 0x10) == 0x10)
|
---|
45 | #define PPS_HAS_PPS2(block) ((block[1] & 0x20) == 0x20)
|
---|
46 | #define PPS_HAS_PPS3(block) ((block[1] & 0x40) == 0x40)
|
---|
47 |
|
---|
48 | /*
|
---|
49 | * Not exported funtions declaration
|
---|
50 | */
|
---|
51 |
|
---|
52 | static int PPS_Exchange(PPS * pps, BYTE * params, unsigned *length);
|
---|
53 |
|
---|
54 | static bool PPS_Match(BYTE * request, unsigned len_request, BYTE * reply, unsigned len_reply);
|
---|
55 |
|
---|
56 | static unsigned PPS_GetLength(BYTE * block);
|
---|
57 |
|
---|
58 | static int PPS_InitICC(PPS * pps);
|
---|
59 |
|
---|
60 | static int PPS_InitProtocol(PPS * pps);
|
---|
61 |
|
---|
62 | static void PPS_SelectFirstProtocol(PPS * pps);
|
---|
63 |
|
---|
64 | static BYTE PPS_GetPCK(BYTE * block, unsigned length);
|
---|
65 |
|
---|
66 | /*
|
---|
67 | * Exported functions definition
|
---|
68 | */
|
---|
69 |
|
---|
70 | PPS *PPS_New(ICC_Async * icc)
|
---|
71 | {
|
---|
72 | PPS *pps;
|
---|
73 |
|
---|
74 | pps = (PPS *) malloc(sizeof (PPS));
|
---|
75 |
|
---|
76 | if (pps != NULL) {
|
---|
77 | pps->icc = icc;
|
---|
78 | pps->protocol = NULL;
|
---|
79 | pps->parameters.t = PPS_DEFAULT_PROTOCOL;
|
---|
80 | pps->parameters.f = ATR_DEFAULT_F;
|
---|
81 | pps->parameters.d = ATR_DEFAULT_D;
|
---|
82 | pps->parameters.n = ATR_DEFAULT_N;
|
---|
83 | }
|
---|
84 |
|
---|
85 | return pps;
|
---|
86 | }
|
---|
87 |
|
---|
88 | int PPS_PerformPTS(PPS *pps)
|
---|
89 | {
|
---|
90 | int ret;
|
---|
91 | unsigned long baudrate;
|
---|
92 | BYTE req[4];
|
---|
93 | BYTE confirm[PPS_MAX_LENGTH];
|
---|
94 | ATR *atr;
|
---|
95 | unsigned len_confirm;
|
---|
96 | unsigned len_request=4;
|
---|
97 |
|
---|
98 | atr = ICC_Async_GetAtr(pps->icc);
|
---|
99 | ATR_GetProtocolType(atr,0,&(pps->parameters.t));
|
---|
100 |
|
---|
101 | req[0]=0xFF;
|
---|
102 | req[1]=0x10 | pps->parameters.t;
|
---|
103 | req[2]=atr->ib[0][ATR_INTERFACE_BYTE_TA].value;
|
---|
104 | req[3]=PPS_GetPCK(req,len_request-1);
|
---|
105 |
|
---|
106 | #ifdef DEBUG_PROTOCOL
|
---|
107 | printf("PTS: Sending request: ");
|
---|
108 | for (i = 0; i < len_request; i++)
|
---|
109 | printf("%X ", req[i]);
|
---|
110 | printf("\n");
|
---|
111 | #endif
|
---|
112 |
|
---|
113 | /* Send PTS request */
|
---|
114 | if (ICC_Async_Transmit(pps->icc, len_request, req) != ICC_ASYNC_OK)
|
---|
115 | return PPS_ICC_ERROR;
|
---|
116 |
|
---|
117 | if (ICC_Async_Receive(pps->icc, len_request, confirm) != ICC_ASYNC_OK)
|
---|
118 | {
|
---|
119 | #ifdef DEBUG_PROTOCOL
|
---|
120 | printf("PTS: error receiving PTS answer !!\n");
|
---|
121 | #endif
|
---|
122 | return PPS_ICC_ERROR;
|
---|
123 | }
|
---|
124 | len_confirm = len_request;
|
---|
125 |
|
---|
126 | #ifdef DEBUG_PROTOCOL
|
---|
127 | printf("PTS: Receiving confirm: ");
|
---|
128 | for (i = 0; i < len_confirm; i++)
|
---|
129 | printf("%X ", confirm[i]);
|
---|
130 | printf("\n");
|
---|
131 | #endif
|
---|
132 |
|
---|
133 | if (!PPS_Match(req, len_request, confirm, len_confirm))
|
---|
134 | ret = PPS_HANDSAKE_ERROR;
|
---|
135 | else
|
---|
136 | ret = PPS_OK;
|
---|
137 |
|
---|
138 | /* Copy PPS handsake */
|
---|
139 | memcpy(req, confirm, len_confirm);
|
---|
140 |
|
---|
141 | // compute baudrate to be use if PTS handshake was successfull.
|
---|
142 | baudrate=(long unsigned int)((float)ICC_Async_GetClockRate(pps->icc)*pps->parameters.d /pps->parameters.f);
|
---|
143 |
|
---|
144 | if (ICC_Async_SetBaudrate(pps->icc, baudrate) != ICC_ASYNC_OK)
|
---|
145 | return PPS_ICC_ERROR;
|
---|
146 |
|
---|
147 | return ret;
|
---|
148 | }
|
---|
149 |
|
---|
150 | int PPS_Perform(PPS * pps, BYTE * params, unsigned *length)
|
---|
151 | {
|
---|
152 | ATR *atr;
|
---|
153 | int ret;
|
---|
154 |
|
---|
155 | /* Perform PPS Exchange if requested */
|
---|
156 | if ((*length) > 0) {
|
---|
157 | ret = PPS_Exchange(pps, params, length);
|
---|
158 |
|
---|
159 | /* Get parameters from PPS handsake */
|
---|
160 | if (ret == PPS_OK) {
|
---|
161 | pps->parameters.t = params[1] & 0x0F;
|
---|
162 |
|
---|
163 | if (PPS_HAS_PPS1(params)) {
|
---|
164 | pps->parameters.f = atr_f_table[(params[2] >> 4)];
|
---|
165 | pps->parameters.d = atr_d_table[(params[2] & 0x0F)];
|
---|
166 | #ifdef DEBUG_PROTOCOL
|
---|
167 | printf("PPS: pps->parameters.n %f\n",pps->parameters.n);
|
---|
168 | printf("PPS: pps->parameters.d %f\n",pps->parameters.d);
|
---|
169 | printf("PPS: pps->parameters.f %f\n",pps->parameters.f);
|
---|
170 | printf("PPS: Calling PPP_InitICC to set PPS params\n");
|
---|
171 | #endif
|
---|
172 | }
|
---|
173 |
|
---|
174 | ret = PPS_InitICC(pps);
|
---|
175 |
|
---|
176 | if (ret != PPS_OK)
|
---|
177 | return ret;
|
---|
178 | } else {
|
---|
179 | return ret;
|
---|
180 | }
|
---|
181 | } else { /* Get parameters from ATR */
|
---|
182 |
|
---|
183 | PPS_SelectFirstProtocol(pps);
|
---|
184 |
|
---|
185 | #ifndef PPS_USE_DEFAULT_TIMINGS
|
---|
186 | atr = ICC_Async_GetAtr(pps->icc);
|
---|
187 | #ifdef DEBUG_PROTOCOL
|
---|
188 | printf("PPS: atr.lenght = %u\n",atr->length);
|
---|
189 | printf("PPS: atr.TS = %u\n",atr->TS);
|
---|
190 | printf("PPS: atr.T0 = %u\n",atr->T0);
|
---|
191 | printf("PPS: atr.TA = %u\n",atr->ib[0][ATR_INTERFACE_BYTE_TA].value);
|
---|
192 | printf("PPS: atr.FI = %u\n",(atr->ib[0][ATR_INTERFACE_BYTE_TA].value & 0xF0) >> 4);
|
---|
193 | printf("PPS: atr.DI = %u\n",(atr->ib[0][ATR_INTERFACE_BYTE_TA].value & 0x0F));
|
---|
194 | #endif
|
---|
195 | ATR_GetParameter(atr, ATR_PARAMETER_N, &(pps->parameters.n));
|
---|
196 | ATR_GetParameter(atr, ATR_PARAMETER_D, &(pps->parameters.d));
|
---|
197 | ATR_GetParameter(atr, ATR_PARAMETER_F, &(pps->parameters.f));
|
---|
198 |
|
---|
199 | #ifdef DEBUG_PROTOCOL
|
---|
200 | printf("PPS: pps->parameters.n %f\n",pps->parameters.n);
|
---|
201 | printf("PPS: pps->parameters.d %f\n",pps->parameters.d);
|
---|
202 | printf("PPS: pps->parameters.f %f\n",pps->parameters.f);
|
---|
203 | printf("PPS: Calling PPP_InitICC to set PPS params\n");
|
---|
204 | #endif
|
---|
205 | ret = PPS_InitICC(pps);
|
---|
206 |
|
---|
207 | if (ret != PPS_OK)
|
---|
208 | return ret;
|
---|
209 | #endif
|
---|
210 | }
|
---|
211 |
|
---|
212 | #ifdef DEBUG_PROTOCOL
|
---|
213 | printf("PPS: T=%X, F=%.0f, D=%.6f, N=%.0f\n", pps->parameters.t, pps->parameters.f, pps->parameters.d, pps->parameters.n);
|
---|
214 | #endif
|
---|
215 |
|
---|
216 | /* Initialize selected protocol with selected parameters */
|
---|
217 | ret = PPS_InitProtocol(pps);
|
---|
218 | /*
|
---|
219 | #ifdef DEBUG_PROTOCOL
|
---|
220 | printf("PPS: Attempting PTS\n");
|
---|
221 | #endif
|
---|
222 | if(PPS_PreformPTS(pps)==PPS_OK)
|
---|
223 | {
|
---|
224 | printf("PTS handcheck succeded.\n");
|
---|
225 | }
|
---|
226 | else
|
---|
227 | {
|
---|
228 | printf("PTS handcheck failed.\n");
|
---|
229 | }
|
---|
230 |
|
---|
231 | #ifdef DEBUG_PROTOCOL
|
---|
232 | printf("PPS: pps->parameters.n %f\n",pps->parameters.n);
|
---|
233 | printf("PPS: pps->parameters.d %f\n",pps->parameters.d);
|
---|
234 | printf("PPS: pps->parameters.f %f\n",pps->parameters.f);
|
---|
235 | #endif
|
---|
236 | */
|
---|
237 | return ret;
|
---|
238 | }
|
---|
239 |
|
---|
240 | void *PPS_GetProtocol(PPS * pps)
|
---|
241 | {
|
---|
242 | return pps->protocol;
|
---|
243 | }
|
---|
244 |
|
---|
245 | PPS_ProtocolParameters *PPS_GetProtocolParameters(PPS * pps)
|
---|
246 | {
|
---|
247 | /* User must Remember not to reference this struct after removing PPS */
|
---|
248 | return &(pps->parameters);
|
---|
249 | }
|
---|
250 |
|
---|
251 | void PPS_Delete(PPS * pps)
|
---|
252 | {
|
---|
253 | free(pps);
|
---|
254 | }
|
---|
255 |
|
---|
256 | /*
|
---|
257 | * Not exported funtions definition
|
---|
258 | */
|
---|
259 |
|
---|
260 | static int PPS_Exchange(PPS * pps, BYTE * params, unsigned *length)
|
---|
261 | {
|
---|
262 | BYTE confirm[PPS_MAX_LENGTH];
|
---|
263 | unsigned len_request, len_confirm;
|
---|
264 | int ret;
|
---|
265 |
|
---|
266 | #ifdef DEBUG_PROTOCOL
|
---|
267 | int i;
|
---|
268 | #endif
|
---|
269 |
|
---|
270 | len_request = PPS_GetLength(params);
|
---|
271 | params[len_request - 1] = PPS_GetPCK(params, len_request - 1);
|
---|
272 |
|
---|
273 | #ifdef DEBUG_PROTOCOL
|
---|
274 | printf("PPS: Sending request: ");
|
---|
275 | for (i = 0; i < len_request; i++)
|
---|
276 | printf("%X ", params[i]);
|
---|
277 | printf("\n");
|
---|
278 | #endif
|
---|
279 |
|
---|
280 | /* Send PPS request */
|
---|
281 | if (ICC_Async_Transmit(pps->icc, len_request, params) != ICC_ASYNC_OK)
|
---|
282 | return PPS_ICC_ERROR;
|
---|
283 |
|
---|
284 | /* Get PPS confirm */
|
---|
285 | if (ICC_Async_Receive(pps->icc, 2, confirm) != ICC_ASYNC_OK)
|
---|
286 | {
|
---|
287 | #ifdef DEBUG_PROTOCOL
|
---|
288 | printf("PPS: error receiving confirm !!\n");
|
---|
289 | #endif
|
---|
290 | return PPS_ICC_ERROR;
|
---|
291 | }
|
---|
292 | len_confirm = PPS_GetLength(confirm);
|
---|
293 |
|
---|
294 | if (ICC_Async_Receive(pps->icc, len_confirm - 2, confirm + 2) != ICC_ASYNC_OK)
|
---|
295 | {
|
---|
296 | #ifdef DEBUG_PROTOCOL
|
---|
297 | printf("PPS: error receiving answer !!\n");
|
---|
298 | #endif
|
---|
299 | return PPS_ICC_ERROR;
|
---|
300 | }
|
---|
301 |
|
---|
302 | #ifdef DEBUG_PROTOCOL
|
---|
303 | printf("PPS: Receiving confirm: ");
|
---|
304 | for (i = 0; i < len_confirm; i++)
|
---|
305 | printf("%X ", confirm[i]);
|
---|
306 | printf("\n");
|
---|
307 | #endif
|
---|
308 |
|
---|
309 | if (!PPS_Match(params, len_request, confirm, len_confirm))
|
---|
310 | ret = PPS_HANDSAKE_ERROR;
|
---|
311 | else
|
---|
312 | ret = PPS_OK;
|
---|
313 |
|
---|
314 | /* Copy PPS handsake */
|
---|
315 | memcpy(params, confirm, len_confirm);
|
---|
316 | (*length) = len_confirm;
|
---|
317 |
|
---|
318 | return ret;
|
---|
319 | }
|
---|
320 |
|
---|
321 | static bool PPS_Match(BYTE * request, unsigned len_request, BYTE * confirm, unsigned len_confirm)
|
---|
322 | {
|
---|
323 | /* See if the reply differs from request */
|
---|
324 | if ((len_request != len_confirm) || (!memcmp(request, confirm, len_request))) {
|
---|
325 | /* See if the card specifies other than default FI and D */
|
---|
326 | if ((PPS_HAS_PPS1(confirm)) && (confirm[2] != request[2]))
|
---|
327 | return FALSE;
|
---|
328 | }
|
---|
329 |
|
---|
330 | return TRUE;
|
---|
331 | }
|
---|
332 |
|
---|
333 | static unsigned PPS_GetLength(BYTE * block)
|
---|
334 | {
|
---|
335 | unsigned length = 3;
|
---|
336 |
|
---|
337 | if (PPS_HAS_PPS1(block))
|
---|
338 | length++;
|
---|
339 |
|
---|
340 | if (PPS_HAS_PPS2(block))
|
---|
341 | length++;
|
---|
342 |
|
---|
343 | if (PPS_HAS_PPS3(block))
|
---|
344 | length++;
|
---|
345 |
|
---|
346 | return length;
|
---|
347 | }
|
---|
348 |
|
---|
349 | static int PPS_InitICC(PPS * pps)
|
---|
350 | {
|
---|
351 | unsigned long baudrate;
|
---|
352 | long double work_etu;
|
---|
353 |
|
---|
354 | /* Work etu = (1/D) * (F/fs) * 1000 milliseconds */
|
---|
355 | work_etu = (1000 * pps->parameters.f) / (pps->parameters.d * ICC_Async_GetClockRate(pps->icc));
|
---|
356 |
|
---|
357 | // FIXME :
|
---|
358 | // initializing the baudrate here is plain wrong.
|
---|
359 | // the card inits MUST be done at 9600 bps
|
---|
360 | // and if , and only if, there is a PPS (or PTS) handshake
|
---|
361 | // then we use this value for the new baudrate
|
---|
362 | // The F and D parameter returned by the ATR are to be used ONLY
|
---|
363 | // if a PPS or PTS exchange is done, otherwize everything MUST
|
---|
364 | // be done at 9600 bps
|
---|
365 | // this mean we need to fix the logic around the card reset
|
---|
366 | // as for now it uses this new rate in the midle of the reset just after getting the ATR
|
---|
367 | // which doesn't work for example with a card returning 3F 77 18 .. as in this case
|
---|
368 | // F=372 and D=12 (see the commented atr_d_table in atr.c which has the right value
|
---|
369 | // as the curent one as 0 for the index 8 which is of course wrong as D=0 is an invalid value).
|
---|
370 | // I'm leaving it like this until we discuss what to do as a rework of the code is needed.
|
---|
371 | //
|
---|
372 |
|
---|
373 | /* Baudrate = 1000 / etu bps */
|
---|
374 | baudrate = (long unsigned int) (1000 / work_etu);
|
---|
375 |
|
---|
376 | #ifdef DEBUG_PROTOCOL
|
---|
377 | printf("PPS: Baudrate = %d\n", (int) baudrate);
|
---|
378 | #endif
|
---|
379 |
|
---|
380 | if (ICC_Async_SetBaudrate(pps->icc, baudrate) != ICC_ASYNC_OK)
|
---|
381 | return PPS_ICC_ERROR;
|
---|
382 |
|
---|
383 | return PPS_OK;
|
---|
384 | }
|
---|
385 |
|
---|
386 | static int PPS_InitProtocol(PPS * pps)
|
---|
387 | {
|
---|
388 | int ret;
|
---|
389 |
|
---|
390 | if (pps->parameters.t == ATR_PROTOCOL_TYPE_T0) {
|
---|
391 | pps->protocol = Protocol_T0_New();
|
---|
392 |
|
---|
393 | if ((pps->protocol) != NULL) {
|
---|
394 | ret = Protocol_T0_Init((Protocol_T0 *) pps->protocol, (ICC_Async *) pps->icc, &(pps->parameters));
|
---|
395 |
|
---|
396 | if (ret != PROTOCOL_T0_OK) {
|
---|
397 | Protocol_T0_Delete((Protocol_T0 *) pps->protocol);
|
---|
398 | pps->protocol = NULL;
|
---|
399 | return PPS_PROTOCOL_ERROR;
|
---|
400 | }
|
---|
401 |
|
---|
402 | return PPS_OK;
|
---|
403 | }
|
---|
404 | } else if (pps->parameters.t == ATR_PROTOCOL_TYPE_T1) {
|
---|
405 | pps->protocol = Protocol_T1_New();
|
---|
406 |
|
---|
407 | if (pps->protocol != NULL) {
|
---|
408 | ret = Protocol_T1_Init((Protocol_T1 *) pps->protocol, (ICC_Async *) pps->icc, &(pps->parameters));
|
---|
409 |
|
---|
410 | if (ret != PROTOCOL_T1_OK) {
|
---|
411 | Protocol_T1_Delete((Protocol_T1 *) pps->protocol);
|
---|
412 | pps->protocol = NULL;
|
---|
413 | return PPS_PROTOCOL_ERROR;
|
---|
414 | }
|
---|
415 |
|
---|
416 | return PPS_OK;
|
---|
417 | }
|
---|
418 | } else if (pps->parameters.t == ATR_PROTOCOL_TYPE_T14) {
|
---|
419 | pps->protocol = Protocol_T14_New();
|
---|
420 |
|
---|
421 | if ((pps->protocol) != NULL) {
|
---|
422 | ret = Protocol_T14_Init((Protocol_T14 *) pps->protocol, (ICC_Async *) pps->icc, &(pps->parameters));
|
---|
423 |
|
---|
424 | if (ret != PROTOCOL_T14_OK) {
|
---|
425 | Protocol_T14_Delete((Protocol_T14 *) pps->protocol);
|
---|
426 | pps->protocol = NULL;
|
---|
427 | return PPS_PROTOCOL_ERROR;
|
---|
428 | }
|
---|
429 |
|
---|
430 | return PPS_OK;
|
---|
431 | }
|
---|
432 | } else {
|
---|
433 | pps->protocol = NULL;
|
---|
434 | }
|
---|
435 |
|
---|
436 | return PPS_PROTOCOL_ERROR;
|
---|
437 | }
|
---|
438 |
|
---|
439 | static void PPS_SelectFirstProtocol(PPS * pps)
|
---|
440 | {
|
---|
441 | ATR *atr = ICC_Async_GetAtr(pps->icc);
|
---|
442 | unsigned np;
|
---|
443 |
|
---|
444 | pps->parameters.t = ATR_PROTOCOL_TYPE_T0;
|
---|
445 |
|
---|
446 | ATR_GetNumberOfProtocols(atr, &np);
|
---|
447 |
|
---|
448 | /*
|
---|
449 | * Get protocol offered by interface bytes T*2 if available,
|
---|
450 | * (that is, if TD1 is available), * otherwise use default T=0
|
---|
451 | */
|
---|
452 | if (np > 1)
|
---|
453 | ATR_GetProtocolType(atr, 2, &(pps->parameters.t));
|
---|
454 |
|
---|
455 | #ifdef DEBUG_PROTOCOL
|
---|
456 | printf("PPS: Protocol T=%d selected\n", pps->parameters.t);
|
---|
457 | #endif
|
---|
458 | }
|
---|
459 |
|
---|
460 | static BYTE PPS_GetPCK(BYTE * block, unsigned length)
|
---|
461 | {
|
---|
462 | BYTE pck;
|
---|
463 | unsigned i;
|
---|
464 |
|
---|
465 | pck = block[0];
|
---|
466 | for (i = 1; i < length; i++)
|
---|
467 | pck ^= block[i];
|
---|
468 |
|
---|
469 | return pck;
|
---|
470 | }
|
---|