source: trunk/csctapi/pps.c@ 1279

Last change on this file since 1279 was 1279, checked in by dingo35, 11 years ago

Remove ICC_Async dynamic data structure, cleanup some parameters in t0 and t1

File size: 16.0 KB
Line 
1/*
2 pps.c
3 Protocol Parameters Selection
4
5 This file is part of the Unix driver for Towitoko smartcard readers
6 Copyright (C) 2000 2001 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 "pps.h"
26#include "atr.h"
27#include "protocol_t0.h"
28#include "protocol_t1.h"
29#include <stdlib.h>
30#include <stdio.h>
31#include <string.h>
32#include "ifd.h"
33#include "../globals.h"
34
35/*
36 * Not exported constants definition
37 */
38
39#define PPS_DEFAULT_PROTOCOL 0x00
40
41/*
42 * Not exported macros definition
43 */
44
45#define PPS_HAS_PPS1(block) ((block[1] & 0x10) == 0x10)
46#define PPS_HAS_PPS2(block) ((block[1] & 0x20) == 0x20)
47#define PPS_HAS_PPS3(block) ((block[1] & 0x40) == 0x40)
48
49/*
50 * Not exported funtions declaration
51 */
52
53static int PPS_Exchange (BYTE * params, unsigned *length);
54
55static bool PPS_Match (BYTE * request, unsigned len_request, BYTE * reply, unsigned len_reply);
56
57static unsigned PPS_GetLength (BYTE * block);
58
59static int PPS_InitICC (PPS * pps);
60
61static int PPS_InitProtocol (PPS * pps, int protocol_selected);
62
63static BYTE PPS_GetPCK (BYTE * block, unsigned length);
64
65/*
66 * Exported functions definition
67 */
68
69PPS * PPS_New ()
70{
71 PPS *pps;
72
73 pps = (PPS *) malloc (sizeof (PPS));
74
75 if (pps != NULL)
76 {
77 pps->protocol = NULL;
78 pps->parameters.t = PPS_DEFAULT_PROTOCOL;
79 pps->parameters.FI = ATR_DEFAULT_FI;
80 pps->parameters.d = ATR_DEFAULT_D;
81 pps->parameters.n = ATR_DEFAULT_N;
82 }
83
84 return pps;
85}
86
87int PPS_Perform (PPS * pps, BYTE * params, unsigned *length)
88{
89 //Performs PPS Exchange on command when *length >0
90 //If unsuccesfull, or when length <= 0
91 //Gets parametes from ATR
92 //If necessary perform PPS session
93 //
94 //Output is pps->params.FI,n,d,t set with correct values
95 //and switched SC-device conform these values to correct baudrate
96 //
97 //We need to store FI instread of F, because SCI_DEV works with FI
98 //and it is easier to overclock then
99 //also from FI -> F is easy, other way around not
100
101 int ret;
102 bool PPS_success;
103
104 /* Perform PPS Exchange if requested by command */
105 if ((*length) > 0)
106 {
107 ret = PPS_Exchange (params, length);
108
109 /* Get parameters from PPS handsake */
110 if (ret == PPS_OK)
111 {
112 pps->parameters.t = params[1] & 0x0F;
113
114 if (PPS_HAS_PPS1 (params))
115 {
116 pps->parameters.FI = (params[2] >> 4);
117 pps->parameters.d = atr_d_table[(params[2] & 0x0F)];
118 }
119
120/*
121 ret = PPS_InitICC(pps);
122
123 if (ret != PPS_OK)
124 return ret;
125*/
126 }
127/*
128 else
129 {
130 return ret;
131 }
132*/
133 }
134 PPS_success = PPS_OK;
135 int protocol_selected = 0; //stores which TAi,TBi etc. bytes must be used 0 means not set
136 if ((*length) <= 0 || !PPS_success) // If not by command, or PPS Exchange by command failed: Try PPS Exchange by ATR or Get parameters from ATR
137 {
138 int numprot = atr->pn;
139 //if there is a trailing TD, this number is one too high
140 BYTE tx;
141 if (ATR_GetInterfaceByte (atr, numprot-1, ATR_INTERFACE_BYTE_TD, &tx) == ATR_OK)
142 if ((tx & 0xF0) == 0)
143 numprot--;
144 cs_debug("ATR reports %i protocol lines:",numprot);
145 int i,point;
146 char txt[50];
147 bool OffersT[3]; //T14 stored as T2
148 for (i = 0; i <= 2; i++)
149 OffersT[i] = FALSE;
150 for (i=1; i<= numprot; i++) {
151 point = 0;
152 if (ATR_GetInterfaceByte (atr, i, ATR_INTERFACE_BYTE_TA, &tx) == ATR_OK) {
153 sprintf((char *)txt+point,"TA%i=%02X ",i,tx);
154 point +=7;
155 }
156 if (ATR_GetInterfaceByte (atr, i, ATR_INTERFACE_BYTE_TB, &tx) == ATR_OK) {
157 sprintf((char *)txt+point,"TB%i=%02X ",i,tx);
158 point +=7;
159 }
160 if (ATR_GetInterfaceByte (atr, i, ATR_INTERFACE_BYTE_TC, &tx) == ATR_OK) {
161 sprintf((char *)txt+point,"TC%i=%02X ",i,tx);
162 point +=7;
163 }
164 if (ATR_GetInterfaceByte (atr, i, ATR_INTERFACE_BYTE_TD, &tx) == ATR_OK) {
165 sprintf((char *)txt+point,"TD%i=%02X ",i,tx);
166 point +=7;
167 tx &= 0X0F;
168 sprintf((char *)txt+point,"(T%i)",tx);
169 if (tx == 14)
170 OffersT[2] = TRUE;
171 else
172 OffersT[tx] = TRUE;
173 }
174 else {
175 sprintf((char *)txt+point,"no TD%i means T0",i);
176 OffersT[0] = TRUE;
177 }
178 cs_debug("%s",txt);
179 }
180
181 int numprottype = 0;
182 for (i = 0; i <= 2; i++)
183 if (OffersT[i])
184 numprottype ++;
185 cs_debug("%i protocol types detected. Historical bytes: %s",numprottype, cs_hexdump(1,atr->hb,atr->hbn));
186
187//If more than one protocol type and/or TA1 parameter values other than the default values and/or N equeal to 255 is/are indicated in the answer to reset, the card shall know unambiguously, after having sent the answer to reset, which protocol type or/and transmission parameter values (FI, D, N) will be used. Consequently a selection of the protocol type and/or the transmission parameters values shall be specified.
188 ATR_GetParameter (atr, ATR_PARAMETER_N, &(pps->parameters.n));
189 ATR_GetProtocolType(atr,1,&(pps->parameters.t)); //get protocol from TD1
190 BYTE TA2;
191 bool SpecificMode = (ATR_GetInterfaceByte (atr, 2, ATR_INTERFACE_BYTE_TA, &TA2) == ATR_OK); //if TA2 present, specific mode, else negotiable mode
192 if (SpecificMode) {
193 pps->parameters.t = TA2 & 0x0F;
194 if ((TA2 & 0x10) != 0x10) { //bit 5 set to 0 means F and D explicitly defined in interface characters
195 BYTE TA1;
196 if (ATR_GetInterfaceByte (atr, 1 , ATR_INTERFACE_BYTE_TA, &TA1) == ATR_OK) {
197 pps->parameters.FI = TA1 >> 4;
198 ATR_GetParameter (atr, ATR_PARAMETER_D, &(pps->parameters.d));
199 }
200 else {
201 pps->parameters.FI = ATR_DEFAULT_FI;
202 pps->parameters.d = ATR_DEFAULT_D;
203 }
204 }
205 else {
206 cs_log("Specific mode: speed 'implicitly defined', not sure how to proceed, assuming default values");
207 pps->parameters.FI = ATR_DEFAULT_FI;
208 pps->parameters.d = ATR_DEFAULT_D;
209 }
210 cs_debug("Specific mode: T%i, F=%.0f, D=%.6f, N=%.0f\n", pps->parameters.t, (double) atr_f_table[pps->parameters.FI], pps->parameters.d, pps->parameters.n);
211 }
212 else { //negotiable mode
213
214 bool NeedsPTS = ((pps->parameters.t != 14) && (numprottype > 1 || (atr->ib[0][ATR_INTERFACE_BYTE_TA].present == TRUE && atr->ib[0][ATR_INTERFACE_BYTE_TA].value != 0x11) || pps->parameters.n == 255)); //needs PTS according to ISO 7816 , SCI gets stuck on our PTS
215 if (NeedsPTS) {
216 // PTSS PTS0 PTS1 PTS2 PTS3 PCK
217 // PTSS PTS0 PTS1 PCK
218 BYTE req[] = { 0xFF, 0x10, 0x00, 0x00 }; //we currently do not support PTS2, standard guardtimes
219 req[1]=0x10 | pps->parameters.t; //PTS0 always flags PTS1 to be sent always
220 if (ATR_GetInterfaceByte (atr, 1, ATR_INTERFACE_BYTE_TA, &req[2]) != ATR_OK) //PTS1
221 req[2] = 0x11; //defaults FI and DI to 1
222 //req[3]=PPS_GetPCK(req,sizeof(req)-1); will be set by PPS_Exchange
223 unsigned int len = sizeof(req);
224 ret = PPS_Exchange (req, &len);
225 if (ret == PPS_OK) {
226 pps->parameters.FI = req[2] >> 4;
227 BYTE DI = req[2] & 0x0F;
228 pps->parameters.d = (double) (atr_d_table[DI]);
229 PPS_success = TRUE;
230 cs_debug("PTS Succesfull, selected protocol: T%i, F=%.0f, D=%.6f, N=%.0f\n", pps->parameters.t, (double) atr_f_table[pps->parameters.FI], pps->parameters.d, pps->parameters.n);
231 }
232 else
233 cs_ddump(req,4,"PTS Failure, response:");
234 }
235
236 //FIXME Currently InitICC sets baudrate to 9600 for all T14 cards (=no switching);
237 //When for SCI, T14 protocol, TA1 is obeyed, this goes OK for mosts devices, but somehow on DM7025 Sky S02 card goes wrong when setting ETU (ok on DM800/DM8000)
238 if (!PPS_success) {//last PPS not succesfull
239 BYTE TA1;
240 if (ATR_GetInterfaceByte (atr, 1 , ATR_INTERFACE_BYTE_TA, &TA1) == ATR_OK) {
241 pps->parameters.FI = TA1 >> 4;
242 ATR_GetParameter (atr, ATR_PARAMETER_D, &(pps->parameters.d));
243 }
244 else { //do not obey TA1
245 pps->parameters.FI = ATR_DEFAULT_FI;
246 pps->parameters.d = ATR_DEFAULT_D;
247 }
248 ATR_GetProtocolType (atr, 1, &(pps->parameters.t));
249 protocol_selected = 1;
250
251 if (NeedsPTS) {
252 if ((pps->parameters.d == 32) || (pps->parameters.d == 12) || (pps->parameters.d == 20))
253 pps->parameters.d = 0; //behave conform "old" atr_d_table; viaccess cards that fail PTS need this
254 }
255 /////Here all non-ISO behaviour
256 /////End all non-ISO behaviour
257
258 cs_debug("No PTS %s, selected protocol T%i, F=%.0f, D=%.6f, N=%.0f\n", NeedsPTS?"happened":"needed", pps->parameters.t, (double) atr_f_table[pps->parameters.FI], pps->parameters.d, pps->parameters.n);
259 }
260 }//end negotiable mode
261 }//end length<0
262
263 //make sure no zero values
264 double F = (double) atr_f_table[pps->parameters.FI];
265 if (!F) {
266 pps->parameters.FI = ATR_DEFAULT_FI;
267 cs_log("Warning: F=0 is invalid, forcing FI=%d", pps->parameters.FI);
268 }
269 if (!pps->parameters.d) {
270 pps->parameters.d = ATR_DEFAULT_D;
271 cs_log("Warning: D=0 is invalid, forcing D=%.0f",pps->parameters.d);
272 }
273
274 protocol_type = pps->parameters.t;
275
276#ifdef DEBUG_PROTOCOL
277 printf("PPS: T=%i, F=%.0f, D=%.6f, N=%.0f\n",
278 pps->parameters.t,
279 F,
280 pps->parameters.d,
281 pps->parameters.n);
282#endif
283
284 ret = PPS_InitICC(pps);
285
286 if (ret != PPS_OK)
287 return ret;
288
289 /* Initialize selected protocol with selected parameters */
290 if (pps->parameters.t == 1)
291 ret = PPS_InitProtocol (pps, 3); //FIXME in practice most T1 cards carry timing parameters in TA3, TB3 and TC3
292 else
293 ret = PPS_InitProtocol (pps, 2); //FIXME T0 cards carry timing parameters in TC2
294
295 return ret;
296}
297
298void * PPS_GetProtocol (PPS * pps)
299{
300 return pps->protocol;
301}
302
303PPS_ProtocolParameters *PPS_GetProtocolParameters (PPS * pps)
304{
305 /* User must Remember not to reference this struct after removing PPS */
306 return &(pps->parameters);
307}
308
309void PPS_Delete (PPS * pps)
310{
311 free (pps);
312}
313
314/*
315 * Not exported funtions definition
316 */
317
318static int PPS_Exchange (BYTE * params, unsigned *length)
319{
320 BYTE confirm[PPS_MAX_LENGTH];
321 unsigned len_request, len_confirm;
322 int ret;
323#ifdef DEBUG_PROTOCOL
324 int i;
325#endif
326
327 len_request = PPS_GetLength (params);
328 params[len_request - 1] = PPS_GetPCK(params, len_request - 1);
329
330#ifdef DEBUG_PROTOCOL
331 printf ("PPS: Sending request: ");
332 for (i = 0; i < len_request; i++)
333 printf ("%X ", params[i]);
334 printf ("\n");
335#endif
336
337 cs_debug("PTS: Sending request: %s", cs_hexdump(1, params, len_request));
338
339 /* Send PPS request */
340#ifdef COOL
341 //unsigned char ptsAck[10];
342 //u_int8 ptsLen = len_request;
343 unsigned short int ptsLen = len_request;
344 int Status = cnxt_smc_start_pps(handle, params, confirm, &ptsLen, TRUE);
345 printf ("cnxt_smc_start_pps Status=%i\n", Status);
346 len_confirm = ptsLen;
347#ifdef DEBUG_PROTOCOL
348 printf("COOL: confirm: \n");
349 for (i = 0; i < ptsLen; i++)
350 printf ("%02X", confirm[i]);
351 printf ("\n");
352 fflush(stdout);
353 printf("COOL: req: \n");
354 for (i = 0; i < len_request; i++)
355 printf ("%02X", params[i]);
356 printf ("\n");
357 fflush(stdout);
358#endif
359 if (Status)
360 return PPS_HANDSAKE_ERROR;
361#else
362 if (ICC_Async_Transmit (len_request, params) != ICC_ASYNC_OK)
363 return PPS_ICC_ERROR;
364
365 /* Get PPS confirm */
366 if (ICC_Async_Receive (2, confirm) != ICC_ASYNC_OK)
367 return PPS_ICC_ERROR;
368
369 len_confirm = PPS_GetLength (confirm);
370
371 if (ICC_Async_Receive (len_confirm - 2, confirm + 2) != ICC_ASYNC_OK)
372 return PPS_ICC_ERROR;
373
374#ifdef DEBUG_PROTOCOL
375 printf ("PPS: Receivig confirm: ");
376 for (i = 0; i < len_confirm; i++)
377 printf ("%X ", confirm[i]);
378 printf ("\n");
379#endif
380
381 cs_debug("PTS: Receiving confirm: %s", cs_hexdump(1, confirm, len_confirm));
382
383 if (!PPS_Match (params, len_request, confirm, len_confirm))
384 ret = PPS_HANDSAKE_ERROR;
385 else
386 ret = PPS_OK;
387#endif
388
389 /* Copy PPS handsake */
390 memcpy (params, confirm, len_confirm);
391 (*length) = len_confirm;
392
393 return ret;
394}
395
396static bool PPS_Match (BYTE * request, unsigned len_request, BYTE * confirm, unsigned len_confirm)
397{
398 /* See if the reply differs from request */
399 if ((len_request != len_confirm) || (memcmp (request, confirm, len_request)))
400 {
401 /* See if the card specifies other than default FI and D */
402 //if ((PPS_HAS_PPS1 (confirm)) && (confirm[2] != request[2]))
403 return FALSE;
404 }
405
406 return TRUE;
407}
408
409static unsigned PPS_GetLength (BYTE * block)
410{
411 unsigned length = 3;
412
413 if (PPS_HAS_PPS1 (block))
414 length++;
415
416 if (PPS_HAS_PPS2 (block))
417 length++;
418
419 if (PPS_HAS_PPS3 (block))
420 length++;
421
422 return length;
423}
424
425static int PPS_InitICC (PPS * pps)
426{
427#ifdef SCI_DEV
428#include <sys/ioctl.h>
429#include "sci_global.h"
430#include "sci_ioctl.h"
431 if(reader[ridx].typ == R_INTERNAL)
432 {
433 int n;
434 SCI_PARAMETERS params;
435 //memset(&params,0,sizeof(SCI_PARAMETERS));
436 if (ioctl(reader[ridx].handle, IOCTL_GET_PARAMETERS, &params) < 0 )
437 return PPS_ICC_ERROR;
438
439 params.T = pps->parameters.t;
440 params.fs = atr_fs_table[pps->parameters.FI] / 1000000;
441 double F = (double) atr_f_table[pps->parameters.FI];
442 //for Irdeto T14 cards, do not set ETU
443 if (!(atr->hbn >= 6 && !memcmp(atr->hb, "IRDETO", 6) && params.T == 14))
444 params.ETU = F / pps->parameters.d;
445 if (pps->parameters.n == 255) //only for T0 or also for T1?
446 params.EGT = 0;
447 else
448 params.EGT = pps->parameters.n;
449
450 double a;
451 ATR_GetParameter(atr, ATR_PARAMETER_P, &a);
452 params.P=(unsigned char)a;
453 ATR_GetParameter(atr, ATR_PARAMETER_I, &a);
454 params.I=(unsigned char)a;
455
456 cs_debug("Setting T=%d fs=%lu mhz ETU=%d WWT=%d CWT=%d BWT=%d EGT=%d clock=%d check=%d P=%d I=%d U=%d", (int)params.T, params.fs, (int)params.ETU, (int)params.WWT, (int)params.CWT, (int)params.BWT, (int)params.EGT, (int)params.clock_stop_polarity, (int)params.check, (int)params.P, (int)params.I, (int)params.U);
457
458 if (ioctl(reader[ridx].handle, IOCTL_SET_PARAMETERS, &params)!=0)
459 return PPS_ICC_ERROR;
460
461 }
462#elif COOL
463 if(reader[ridx].typ == R_INTERNAL) {
464 int mhz = atr_fs_table[pps->parameters.FI] / 10000;
465 if (!Cool_SetBaudrate(mhz))
466 return PPS_ICC_ERROR;
467#ifdef DEBUG_PROTOCOL
468 printf("Coolstream: set clock to %i * 10kHz\n", mhz);
469#endif
470 return PPS_OK;
471 }
472#endif
473 {
474 unsigned long baudrate;
475 double F = (double) atr_f_table[pps->parameters.FI];
476 if (pps->parameters.t == 14)
477 baudrate = 9600;
478 else
479 baudrate = pps->parameters.d * ICC_Async_GetClockRate () / F;
480
481#ifdef DEBUG_PROTOCOL
482 printf ("PPS: Baudrate = %d\n", (int)baudrate);
483#endif
484
485
486 if (ICC_Async_SetBaudrate (baudrate) != ICC_ASYNC_OK)
487 return PPS_ICC_ERROR;
488
489 return PPS_OK;
490 }
491}
492
493static int PPS_InitProtocol (PPS * pps, int selected_protocol)
494{
495 int ret;
496
497 if (pps->parameters.t == ATR_PROTOCOL_TYPE_T0)
498 {
499 pps->protocol = Protocol_T0_New ();
500
501 if ((pps->protocol) != NULL)
502 {
503 ret = Protocol_T0_Init ((Protocol_T0 *) pps->protocol, &(pps->parameters), selected_protocol);
504
505 if (ret != PROTOCOL_T0_OK)
506 {
507 Protocol_T0_Delete ((Protocol_T0 *) pps->protocol);
508 pps->protocol = NULL;
509 return PPS_PROTOCOL_ERROR;
510 }
511
512 return PPS_OK;
513 }
514 }
515 else if (pps->parameters.t == ATR_PROTOCOL_TYPE_T1)
516 {
517 pps->protocol = Protocol_T1_New ();
518
519 if (pps->protocol != NULL)
520 {
521 ret = Protocol_T1_Init ((Protocol_T1 *) pps->protocol, selected_protocol);
522
523 if (ret != PROTOCOL_T1_OK)
524 {
525 Protocol_T1_Delete ((Protocol_T1 *) pps->protocol);
526 pps->protocol = NULL;
527 return PPS_PROTOCOL_ERROR;
528 }
529
530 return PPS_OK;
531 }
532 }
533 else if (pps->parameters.t == ATR_PROTOCOL_TYPE_T14)
534 {
535 pps->protocol = Protocol_T14_New ();
536
537 if ((pps->protocol) != NULL)
538 {
539 ret = Protocol_T14_Init ((Protocol_T14 *) pps->protocol, &(pps->parameters), selected_protocol);
540
541 if (ret != PROTOCOL_T14_OK)
542 {
543 Protocol_T14_Delete ((Protocol_T14 *) pps->protocol);
544 pps->protocol = NULL;
545 return PPS_PROTOCOL_ERROR;
546 }
547
548 return PPS_OK;
549 }
550 }
551 else
552 {
553 pps->protocol = NULL;
554 }
555
556 return PPS_PROTOCOL_ERROR;
557}
558
559static BYTE PPS_GetPCK (BYTE * block, unsigned length)
560{
561 BYTE pck;
562 unsigned i;
563
564 pck = block[0];
565 for (i = 1; i < length; i++)
566 pck ^= block[i];
567
568 return pck;
569}
Note: See TracBrowser for help on using the repository browser.