1/* 2 ifdhandler.c: IFDH API 3 Copyright (C) 2003-2009 Ludovic Rousseau 4 5 This library is free software; you can redistribute it and/or 6 modify it under the terms of the GNU Lesser General Public 7 License as published by the Free Software Foundation; either 8 version 2.1 of the License, or (at your option) any later version. 9 10 This library is distributed in the hope that it will be useful, 11 but WITHOUT ANY WARRANTY; without even the implied warranty of 12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 13 Lesser General Public License for more details. 14 15 You should have received a copy of the GNU Lesser General Public License 16 along with this library; if not, write to the Free Software Foundation, 17 Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. 18*/ 19 20/* $Id: ifdhandler.c 4346 2009-07-28 13:39:37Z rousseau $ */ 21 22#include <stdio.h> 23#include <string.h> 24#include <stdlib.h> 25#include <arpa/inet.h> 26#include "misc.h" 27#include "config.h" 28#include <pcsclite.h> 29#include <ifdhandler.h> 30#include <reader.h> 31 32#include "ccid.h" 33#include "defs.h" 34#include "ccid_ifdhandler.h" 35#include "debug.h" 36#include "utils.h" 37#include "commands.h" 38#include "towitoko/atr.h" 39#include "towitoko/pps.h" 40#include "parser.h" 41 42#ifdef HAVE_PTHREAD 43#include <pthread.h> 44#endif 45 46/* Array of structures to hold the ATR and other state value of each slot */ 47static CcidDesc CcidSlots[CCID_DRIVER_MAX_READERS]; 48 49/* global mutex */ 50#ifdef HAVE_PTHREAD 51static pthread_mutex_t ifdh_context_mutex = PTHREAD_MUTEX_INITIALIZER; 52#endif 53 54int LogLevel = DEBUG_LEVEL_CRITICAL | DEBUG_LEVEL_INFO; 55int DriverOptions = 0; 56int PowerOnVoltage = VOLTAGE_5V; 57static int DebugInitialized = FALSE; 58 59/* local functions */ 60#if HAVE_DECL_TAG_IFD_POLLING_THREAD && !defined(TWIN_SERIAL) && defined(USE_USB_INTERRUPT) 61static RESPONSECODE IFDHPolling(DWORD Lun); 62static RESPONSECODE IFDHSleep(DWORD Lun); 63#endif 64static void init_driver(void); 65static void extra_egt(ATR_t *atr, _ccid_descriptor *ccid_desc, DWORD Protocol); 66static char find_baud_rate(unsigned int baudrate, unsigned int *list); 67static unsigned int T0_card_timeout(double f, double d, int TC1, int TC2, 68 int clock_frequency); 69static unsigned int T1_card_timeout(double f, double d, int TC1, int BWI, 70 int CWI, int clock_frequency); 71 72 73EXTERNAL RESPONSECODE IFDHCreateChannelByName(DWORD Lun, LPSTR lpcDevice) 74{ 75 RESPONSECODE return_value = IFD_SUCCESS; 76 int reader_index; 77 status_t ret; 78 79 if (! DebugInitialized) 80 init_driver(); 81 82 DEBUG_INFO3("lun: %X, device: %s", Lun, lpcDevice); 83 84 if (-1 == (reader_index = GetNewReaderIndex(Lun))) 85 return IFD_COMMUNICATION_ERROR; 86 87 /* Reset ATR buffer */ 88 CcidSlots[reader_index].nATRLength = 0; 89 *CcidSlots[reader_index].pcATRBuffer = '\0'; 90 91 /* Reset PowerFlags */ 92 CcidSlots[reader_index].bPowerFlags = POWERFLAGS_RAZ; 93 94 /* reader name */ 95 CcidSlots[reader_index].readerName = strdup(lpcDevice); 96 97#ifdef HAVE_PTHREAD 98 (void)pthread_mutex_lock(&ifdh_context_mutex); 99#endif 100 101 ret = OpenPortByName(reader_index, lpcDevice); 102 if (ret != STATUS_SUCCESS) 103 { 104 DEBUG_CRITICAL("failed"); 105 if (STATUS_NO_SUCH_DEVICE == ret) 106 return_value = IFD_NO_SUCH_DEVICE; 107 else 108 return_value = IFD_COMMUNICATION_ERROR; 109 110 /* release the allocated reader_index */ 111 ReleaseReaderIndex(reader_index); 112 } 113 else 114 { 115 /* Maybe we have a special treatment for this reader */ 116 (void)ccid_open_hack_pre(reader_index); 117 118 /* Try to access the reader */ 119 /* This "warm up" sequence is sometimes needed when pcscd is 120 * restarted with the reader already connected. We get some 121 * "usb_bulk_read: Resource temporarily unavailable" on the first 122 * few tries. It is an empirical hack */ 123 if ((IFD_COMMUNICATION_ERROR == IFDHICCPresence(Lun)) 124 && (IFD_COMMUNICATION_ERROR == IFDHICCPresence(Lun)) 125 && (IFD_COMMUNICATION_ERROR == IFDHICCPresence(Lun))) 126 { 127 DEBUG_CRITICAL("failed"); 128 return_value = IFD_COMMUNICATION_ERROR; 129 130 /* release the allocated resources */ 131 (void)ClosePort(reader_index); 132 ReleaseReaderIndex(reader_index); 133 } 134 else 135 /* Maybe we have a special treatment for this reader */ 136 (void)ccid_open_hack_post(reader_index); 137 } 138 139#ifdef HAVE_PTHREAD 140 (void)pthread_mutex_unlock(&ifdh_context_mutex); 141#endif 142 143 return return_value; 144} /* IFDHCreateChannelByName */ 145 146 147EXTERNAL RESPONSECODE IFDHCreateChannel(DWORD Lun, DWORD Channel) 148{ 149 /* 150 * Lun - Logical Unit Number, use this for multiple card slots or 151 * multiple readers. 0xXXXXYYYY - XXXX multiple readers, YYYY multiple 152 * slots. The resource manager will set these automatically. By 153 * default the resource manager loads a new instance of the driver so 154 * if your reader does not have more than one smartcard slot then 155 * ignore the Lun in all the functions. Future versions of PC/SC might 156 * support loading multiple readers through one instance of the driver 157 * in which XXXX would be important to implement if you want this. 158 */ 159 160 /* 161 * Channel - Channel ID. This is denoted by the following: 0x000001 - 162 * /dev/pcsc/1 0x000002 - /dev/pcsc/2 0x000003 - /dev/pcsc/3 163 * 164 * USB readers may choose to ignore this parameter and query the bus 165 * for the particular reader. 166 */ 167 168 /* 169 * This function is required to open a communications channel to the 170 * port listed by Channel. For example, the first serial reader on 171 * COM1 would link to /dev/pcsc/1 which would be a sym link to 172 * /dev/ttyS0 on some machines This is used to help with intermachine 173 * independance. 174 * 175 * Once the channel is opened the reader must be in a state in which 176 * it is possible to query IFDHICCPresence() for card status. 177 * 178 * returns: 179 * 180 * IFD_SUCCESS IFD_COMMUNICATION_ERROR 181 */ 182 RESPONSECODE return_value = IFD_SUCCESS; 183 int reader_index; 184 185 if (! DebugInitialized) 186 init_driver(); 187 188 DEBUG_INFO2("lun: %X", Lun); 189 190 if (-1 == (reader_index = GetNewReaderIndex(Lun))) 191 return IFD_COMMUNICATION_ERROR; 192 193 /* Reset ATR buffer */ 194 CcidSlots[reader_index].nATRLength = 0; 195 *CcidSlots[reader_index].pcATRBuffer = '\0'; 196 197 /* Reset PowerFlags */ 198 CcidSlots[reader_index].bPowerFlags = POWERFLAGS_RAZ; 199 200 /* reader name */ 201 CcidSlots[reader_index].readerName = strdup("no name"); 202 203#ifdef HAVE_PTHREAD 204 (void)pthread_mutex_lock(&ifdh_context_mutex); 205#endif 206 207 if (OpenPort(reader_index, Channel) != STATUS_SUCCESS) 208 { 209 DEBUG_CRITICAL("failed"); 210 return_value = IFD_COMMUNICATION_ERROR; 211 212 /* release the allocated reader_index */ 213 ReleaseReaderIndex(reader_index); 214 } 215 else 216 { 217 /* Maybe we have a special treatment for this reader */ 218 (void)ccid_open_hack_pre(reader_index); 219 220 /* Try to access the reader */ 221 /* This "warm up" sequence is sometimes needed when pcscd is 222 * restarted with the reader already connected. We get some 223 * "usb_bulk_read: Resource temporarily unavailable" on the first 224 * few tries. It is an empirical hack */ 225 if ((IFD_COMMUNICATION_ERROR == IFDHICCPresence(Lun)) 226 && (IFD_COMMUNICATION_ERROR == IFDHICCPresence(Lun)) 227 && (IFD_COMMUNICATION_ERROR == IFDHICCPresence(Lun))) 228 { 229 DEBUG_CRITICAL("failed"); 230 return_value = IFD_COMMUNICATION_ERROR; 231 232 /* release the allocated resources */ 233 (void)ClosePort(reader_index); 234 ReleaseReaderIndex(reader_index); 235 } 236 else 237 /* Maybe we have a special treatment for this reader */ 238 (void)ccid_open_hack_post(reader_index); 239 } 240 241#ifdef HAVE_PTHREAD 242 (void)pthread_mutex_unlock(&ifdh_context_mutex); 243#endif 244 245 return return_value; 246} /* IFDHCreateChannel */ 247 248 249EXTERNAL RESPONSECODE IFDHCloseChannel(DWORD Lun) 250{ 251 /* 252 * This function should close the reader communication channel for the 253 * particular reader. Prior to closing the communication channel the 254 * reader should make sure the card is powered down and the terminal 255 * is also powered down. 256 * 257 * returns: 258 * 259 * IFD_SUCCESS IFD_COMMUNICATION_ERROR 260 */ 261 int reader_index; 262 263 if (-1 == (reader_index = LunToReaderIndex(Lun))) 264 return IFD_COMMUNICATION_ERROR; 265 266 DEBUG_INFO3("%s (lun: %X)", CcidSlots[reader_index].readerName, Lun); 267 268 /* Restore the default timeout 269 * No need to wait too long if the reader disapeared */ 270 get_ccid_descriptor(reader_index)->readTimeout = DEFAULT_COM_READ_TIMEOUT; 271 272 (void)CmdPowerOff(reader_index); 273 /* No reader status check, if it failed, what can you do ? :) */ 274 275#ifdef HAVE_PTHREAD 276 (void)pthread_mutex_lock(&ifdh_context_mutex); 277#endif 278 279 (void)ClosePort(reader_index); 280 ReleaseReaderIndex(reader_index); 281 282 free(CcidSlots[reader_index].readerName); 283 memset(&CcidSlots[reader_index], 0, sizeof(CcidSlots[reader_index])); 284 285#ifdef HAVE_PTHREAD 286 (void)pthread_mutex_unlock(&ifdh_context_mutex); 287#endif 288 289 return IFD_SUCCESS; 290} /* IFDHCloseChannel */ 291 292 293#if HAVE_DECL_TAG_IFD_POLLING_THREAD && !defined(TWIN_SERIAL) && defined(USE_USB_INTERRUPT) 294static RESPONSECODE IFDHPolling(DWORD Lun) 295{ 296 int reader_index; 297 int ret; 298 299 if (-1 == (reader_index = LunToReaderIndex(Lun))) 300 return IFD_COMMUNICATION_ERROR; 301 302 /* log only if DEBUG_LEVEL_PERIODIC is set */ 303 if (LogLevel & DEBUG_LEVEL_PERIODIC) 304 DEBUG_INFO3("%s (lun: %X)", CcidSlots[reader_index].readerName, Lun); 305 306 ret = InterruptRead(reader_index, 2*1000); /* 2 seconds */ 307 if (ret > 0) 308 return IFD_SUCCESS; 309 if (0 == ret) 310 return IFD_NO_SUCH_DEVICE; 311 return IFD_COMMUNICATION_ERROR; 312} 313 314/* on an ICCD device the card is always inserted 315 * so no card movement will ever happen: just do nothing */ 316static RESPONSECODE IFDHSleep(DWORD Lun) 317{ 318 int reader_index; 319 320 if (-1 == (reader_index = LunToReaderIndex(Lun))) 321 return IFD_COMMUNICATION_ERROR; 322 323 DEBUG_INFO3("%s (lun: %X)", CcidSlots[reader_index].readerName, Lun); 324 325 /* just sleep for 5 seconds since the polling thread is NOT killable 326 * so pcscd event thread must loop to exit cleanly 327 * 328 * Once the driver (libusb in fact) will support 329 * TAG_IFD_POLLING_THREAD_KILLABLE then we could use a much longer delay 330 * and be killed before pcscd exits 331 */ 332 (void)sleep(600); /* 10 minutes */ 333 return IFD_SUCCESS; 334} 335#endif 336 337 338EXTERNAL RESPONSECODE IFDHGetCapabilities(DWORD Lun, DWORD Tag, 339 PDWORD Length, PUCHAR Value) 340{ 341 /* 342 * This function should get the slot/card capabilities for a 343 * particular slot/card specified by Lun. Again, if you have only 1 344 * card slot and don't mind loading a new driver for each reader then 345 * ignore Lun. 346 * 347 * Tag - the tag for the information requested example: TAG_IFD_ATR - 348 * return the Atr and it's size (required). these tags are defined in 349 * ifdhandler.h 350 * 351 * Length - the length of the returned data Value - the value of the 352 * data 353 * 354 * returns: 355 * 356 * IFD_SUCCESS IFD_ERROR_TAG 357 */ 358 int reader_index; 359 360 if (-1 == (reader_index = LunToReaderIndex(Lun))) 361 return IFD_COMMUNICATION_ERROR; 362 363 DEBUG_INFO4("tag: 0x%X, %s (lun: %X)", Tag, 364 CcidSlots[reader_index].readerName, Lun); 365 366 switch (Tag) 367 { 368 case TAG_IFD_ATR: 369 case SCARD_ATTR_ATR_STRING: 370 /* If Length is not zero, powerICC has been performed. 371 * Otherwise, return NULL pointer 372 * Buffer size is stored in *Length */ 373 *Length = (*Length < CcidSlots[reader_index].nATRLength) ? 374 *Length : CcidSlots[reader_index].nATRLength; 375 376 if (*Length) 377 memcpy(Value, CcidSlots[reader_index].pcATRBuffer, *Length); 378 break; 379 380#ifdef HAVE_PTHREAD 381 case TAG_IFD_SIMULTANEOUS_ACCESS: 382 if (*Length >= 1) 383 { 384 *Length = 1; 385 *Value = CCID_DRIVER_MAX_READERS; 386 } 387 break; 388 389 case TAG_IFD_THREAD_SAFE: 390 if (*Length >= 1) 391 { 392 *Length = 1; 393#ifdef __APPLE__ 394 *Value = 0; /* Apple pcscd is bogus (rdar://problem/5697388) */ 395#else 396 *Value = 1; /* Can talk to multiple readers at the same time */ 397#endif 398 } 399 break; 400#endif 401 402 case TAG_IFD_SLOTS_NUMBER: 403 if (*Length >= 1) 404 { 405 *Length = 1; 406 *Value = 1 + get_ccid_descriptor(reader_index) -> bMaxSlotIndex; 407#ifdef USE_COMPOSITE_AS_MULTISLOT 408 { 409 /* On MacOS X or Linux+libusb we can simulate a 410 * composite device with 2 CCID interfaces by a 411 * multi-slot reader */ 412 int readerID = get_ccid_descriptor(reader_index) -> readerID; 413 414 if ((GEMALTOPROXDU == readerID) || (GEMALTOPROXSU == readerID)) 415 *Value = 2; 416 } 417#endif 418 DEBUG_INFO2("Reader supports %d slot(s)", *Value); 419 } 420 break; 421 422 case TAG_IFD_SLOT_THREAD_SAFE: 423 if (*Length >= 1) 424 { 425 *Length = 1; 426 *Value = 0; /* Can NOT talk to multiple slots at the same time */ 427 } 428 break; 429 430 case SCARD_ATTR_VENDOR_IFD_VERSION: 431 /* Vendor-supplied interface device version (DWORD in the form 432 * 0xMMmmbbbb where MM = major version, mm = minor version, and 433 * bbbb = build number). */ 434 *Length = sizeof(DWORD); 435 if (Value) 436 *(DWORD *)Value = CCID_VERSION; 437 break; 438 439 case SCARD_ATTR_VENDOR_NAME: 440#define VENDOR_NAME "Ludovic Rousseau" 441 *Length = sizeof(VENDOR_NAME); 442 if (Value) 443 memcpy(Value, VENDOR_NAME, sizeof(VENDOR_NAME)); 444 break; 445 446 case SCARD_ATTR_MAXINPUT: 447 *Length = sizeof(uint32_t); 448 if (Value) 449 *(uint32_t *)Value = get_ccid_descriptor(reader_index) -> dwMaxCCIDMessageLength -10; 450 break; 451 452#if HAVE_DECL_TAG_IFD_POLLING_THREAD && !defined(TWIN_SERIAL) && defined(USE_USB_INTERRUPT) 453 case TAG_IFD_POLLING_THREAD: 454 { 455 _ccid_descriptor *ccid_desc; 456 457 /* default value: not supported */ 458 *Length = 0; 459 460 ccid_desc = get_ccid_descriptor(reader_index); 461 /* CCID and not ICCD */ 462 if ((0 == ccid_desc -> bInterfaceProtocol) 463 /* 3 end points */ 464 && (3 == ccid_desc -> bNumEndpoints)) 465 { 466 *Length = sizeof(void *); 467 if (Value) 468 *(void **)Value = IFDHPolling; 469 } 470 471 if ((ICCD_A == ccid_desc->bInterfaceProtocol) 472 || (ICCD_B == ccid_desc->bInterfaceProtocol)) 473 { 474 *Length = sizeof(void *); 475 if (Value) 476 *(void **)Value = IFDHSleep; 477 } 478 } 479 break; 480 481 case TAG_IFD_POLLING_THREAD_KILLABLE: 482 { 483 _ccid_descriptor *ccid_desc; 484 485 /* default value: not supported */ 486 *Length = 0; 487 488 ccid_desc = get_ccid_descriptor(reader_index); 489 if ((ICCD_A == ccid_desc->bInterfaceProtocol) 490 || (ICCD_B == ccid_desc->bInterfaceProtocol)) 491 { 492 *Length = 1; /* 1 char */ 493 if (Value) 494 *Value = 1; /* TRUE */ 495 } 496 } 497 break; 498#endif 499 500 default: 501 return IFD_ERROR_TAG; 502 } 503 504 return IFD_SUCCESS; 505} /* IFDHGetCapabilities */ 506 507 508EXTERNAL RESPONSECODE IFDHSetCapabilities(DWORD Lun, DWORD Tag, 509 /*@unused@*/ DWORD Length, /*@unused@*/ PUCHAR Value) 510{ 511 /* 512 * This function should set the slot/card capabilities for a 513 * particular slot/card specified by Lun. Again, if you have only 1 514 * card slot and don't mind loading a new driver for each reader then 515 * ignore Lun. 516 * 517 * Tag - the tag for the information needing set 518 * 519 * Length - the length of the returned data Value - the value of the 520 * data 521 * 522 * returns: 523 * 524 * IFD_SUCCESS IFD_ERROR_TAG IFD_ERROR_SET_FAILURE 525 * IFD_ERROR_VALUE_READ_ONLY 526 */ 527 528 /* By default, say it worked */ 529 530 int reader_index; 531 532 if (-1 == (reader_index = LunToReaderIndex(Lun))) 533 return IFD_COMMUNICATION_ERROR; 534 535 DEBUG_INFO4("tag: 0x%X, %s (lun: %X)", Tag, 536 CcidSlots[reader_index].readerName, Lun); 537 538 /* if (CheckLun(Lun)) 539 return IFD_COMMUNICATION_ERROR; */ 540 541 return IFD_NOT_SUPPORTED; 542} /* IFDHSetCapabilities */ 543 544 545EXTERNAL RESPONSECODE IFDHSetProtocolParameters(DWORD Lun, DWORD Protocol, 546 UCHAR Flags, UCHAR PTS1, UCHAR PTS2, UCHAR PTS3) 547{ 548 /* 549 * This function should set the PTS of a particular card/slot using 550 * the three PTS parameters sent 551 * 552 * Protocol - SCARD_PROTOCOL_T0 or SCARD_PROTOCOL_T1 553 * Flags - Logical OR of possible values: 554 * IFD_NEGOTIATE_PTS1 555 * IFD_NEGOTIATE_PTS2 556 * IFD_NEGOTIATE_PTS3 557 * to determine which PTS values to negotiate. 558 * PTS1,PTS2,PTS3 - PTS Values. 559 * 560 * returns: 561 * IFD_SUCCESS 562 * IFD_ERROR_PTS_FAILURE 563 * IFD_COMMUNICATION_ERROR 564 * IFD_PROTOCOL_NOT_SUPPORTED 565 */ 566 567 BYTE pps[PPS_MAX_LENGTH]; 568 ATR_t atr; 569 unsigned int len; 570 int convention; 571 int reader_index; 572 573 /* Set ccid desc params */ 574 CcidDesc *ccid_slot; 575 _ccid_descriptor *ccid_desc; 576 577 if (-1 == (reader_index = LunToReaderIndex(Lun))) 578 return IFD_COMMUNICATION_ERROR; 579 580 DEBUG_INFO4("protocol T=%d, %s (lun: %X)", Protocol-SCARD_PROTOCOL_T0, 581 CcidSlots[reader_index].readerName, Lun); 582 583 /* Set to zero buffer */ 584 memset(pps, 0, sizeof(pps)); 585 memset(&atr, 0, sizeof(atr)); 586 587 /* Get ccid params */ 588 ccid_slot = get_ccid_slot(reader_index); 589 ccid_desc = get_ccid_descriptor(reader_index); 590 591 /* Do not send CCID command SetParameters or PPS to the CCID 592 * The CCID will do this himself */ 593 if (ccid_desc->dwFeatures & CCID_CLASS_AUTO_PPS_PROP) 594 goto end; 595 596 /* Get ATR of the card */ 597 (void)ATR_InitFromArray(&atr, ccid_slot->pcATRBuffer, 598 ccid_slot->nATRLength); 599 600 /* Apply Extra EGT patch for bogus cards */ 601 extra_egt(&atr, ccid_desc, Protocol); 602 603 if (SCARD_PROTOCOL_T0 == Protocol) 604 pps[1] |= ATR_PROTOCOL_TYPE_T0; 605 else 606 if (SCARD_PROTOCOL_T1 == Protocol) 607 pps[1] |= ATR_PROTOCOL_TYPE_T1; 608 else 609 return IFD_PROTOCOL_NOT_SUPPORTED; 610 611 /* TA2 present -> specific mode */ 612 if (atr.ib[1][ATR_INTERFACE_BYTE_TA].present) 613 { 614 if (pps[1] != (atr.ib[1][ATR_INTERFACE_BYTE_TA].value & 0x0F)) 615 { 616 /* wrong protocol */ 617 DEBUG_COMM3("Specific mode in T=%d and T=%d requested", 618 atr.ib[1][ATR_INTERFACE_BYTE_TA].value & 0x0F, pps[1]); 619 620 return IFD_PROTOCOL_NOT_SUPPORTED; 621 } 622 } 623 624 /* TCi (i>2) indicates CRC instead of LRC */ 625 if (SCARD_PROTOCOL_T1 == Protocol) 626 { 627 t1_state_t *t1 = &(ccid_slot -> t1); 628 int i; 629 630 /* TCi (i>2) present? */ 631 for (i=2; i<ATR_MAX_PROTOCOLS; i++) 632 if (atr.ib[i][ATR_INTERFACE_BYTE_TC].present) 633 { 634 if (0 == atr.ib[i][ATR_INTERFACE_BYTE_TC].value) 635 { 636 DEBUG_COMM("Use LRC"); 637 (void)t1_set_param(t1, IFD_PROTOCOL_T1_CHECKSUM_LRC, 0); 638 } 639 else 640 if (1 == atr.ib[i][ATR_INTERFACE_BYTE_TC].value) 641 { 642 DEBUG_COMM("Use CRC"); 643 (void)t1_set_param(t1, IFD_PROTOCOL_T1_CHECKSUM_CRC, 0); 644 } 645 else 646 DEBUG_COMM2("Wrong value for TCi: %d", 647 atr.ib[i][ATR_INTERFACE_BYTE_TC].value); 648 649 /* only the first TCi (i>2) must be used */ 650 break; 651 } 652 } 653 654 /* PTS1? */ 655 if (Flags & IFD_NEGOTIATE_PTS1) 656 { 657 /* just use the value passed in argument */ 658 pps[1] |= 0x10; /* PTS1 presence */ 659 pps[2] = PTS1; 660 } 661 else 662 { 663 /* TA1 present */ 664 if (atr.ib[0][ATR_INTERFACE_BYTE_TA].present) 665 { 666 unsigned int card_baudrate; 667 unsigned int default_baudrate; 668 double f, d; 669 670 (void)ATR_GetParameter(&atr, ATR_PARAMETER_D, &d); 671 (void)ATR_GetParameter(&atr, ATR_PARAMETER_F, &f); 672 673 /* may happen with non ISO cards */ 674 if ((0 == f) || (0 == d)) 675 { 676 /* values for TA1=11 */ 677 f = 372; 678 d = 1; 679 } 680 681 /* Baudrate = f x D/F */ 682 card_baudrate = (unsigned int) (1000 * ccid_desc->dwDefaultClock 683 * d / f); 684 685 default_baudrate = (unsigned int) (1000 * ccid_desc->dwDefaultClock 686 * ATR_DEFAULT_D / ATR_DEFAULT_F); 687 688 /* if the card does not try to lower the default speed */ 689 if ((card_baudrate > default_baudrate) 690 /* and the reader is fast enough */ 691 && (card_baudrate <= ccid_desc->dwMaxDataRate)) 692 { 693 /* the reader has no baud rates table */ 694 if ((NULL == ccid_desc->arrayOfSupportedDataRates) 695 /* or explicitely support it */ 696 || find_baud_rate(card_baudrate, 697 ccid_desc->arrayOfSupportedDataRates)) 698 { 699 pps[1] |= 0x10; /* PTS1 presence */ 700 pps[2] = atr.ib[0][ATR_INTERFACE_BYTE_TA].value; 701 702 DEBUG_COMM2("Set speed to %d bauds", card_baudrate); 703 } 704 else 705 { 706 DEBUG_COMM2("Reader does not support %d bauds", 707 card_baudrate); 708 709 /* TA2 present -> specific mode: the card is supporting 710 * only the baud rate specified in TA1 but reader does not 711 * support this value. Reject the card. */ 712 if (atr.ib[1][ATR_INTERFACE_BYTE_TA].present) 713 return IFD_COMMUNICATION_ERROR; 714 } 715 } 716 else 717 { 718 /* the card is too fast for the reader */ 719 if ((card_baudrate > ccid_desc->dwMaxDataRate +2) 720 /* but TA1 <= 97 */ 721 && (atr.ib[0][ATR_INTERFACE_BYTE_TA].value <= 0x97) 722 /* and the reader has a baud rate table */ 723 && ccid_desc->arrayOfSupportedDataRates) 724 { 725 unsigned char old_TA1; 726 727 old_TA1 = atr.ib[0][ATR_INTERFACE_BYTE_TA].value; 728 while (atr.ib[0][ATR_INTERFACE_BYTE_TA].value > 0x94) 729 { 730 /* use a lower TA1 */ 731 atr.ib[0][ATR_INTERFACE_BYTE_TA].value--; 732 733 (void)ATR_GetParameter(&atr, ATR_PARAMETER_D, &d); 734 (void)ATR_GetParameter(&atr, ATR_PARAMETER_F, &f); 735 736 /* Baudrate = f x D/F */ 737 card_baudrate = (unsigned int) (1000 * 738 ccid_desc->dwDefaultClock * d / f); 739 740 if (find_baud_rate(card_baudrate, 741 ccid_desc->arrayOfSupportedDataRates)) 742 { 743 pps[1] |= 0x10; /* PTS1 presence */ 744 pps[2] = atr.ib[0][ATR_INTERFACE_BYTE_TA].value; 745 746 DEBUG_COMM2("Set adapted speed to %d bauds", 747 card_baudrate); 748 749 break; 750 } 751 } 752 753 /* restore original TA1 value */ 754 atr.ib[0][ATR_INTERFACE_BYTE_TA].value = old_TA1; 755 } 756 } 757 } 758 } 759 760 /* PTS2? */ 761 if (Flags & IFD_NEGOTIATE_PTS2) 762 { 763 pps[1] |= 0x20; /* PTS2 presence */ 764 pps[3] = PTS2; 765 } 766 767 /* PTS3? */ 768 if (Flags & IFD_NEGOTIATE_PTS3) 769 { 770 pps[1] |= 0x40; /* PTS3 presence */ 771 pps[4] = PTS3; 772 } 773 774 /* Generate PPS */ 775 pps[0] = 0xFF; 776 777 /* Automatic PPS made by the ICC? */ 778 if ((! (ccid_desc->dwFeatures & CCID_CLASS_AUTO_PPS_CUR)) 779 /* TA2 absent: negociable mode */ 780 && (! atr.ib[1][ATR_INTERFACE_BYTE_TA].present)) 781 { 782 int default_protocol; 783 784 if (ATR_MALFORMED == ATR_GetDefaultProtocol(&atr, &default_protocol)) 785 return IFD_PROTOCOL_NOT_SUPPORTED; 786 787 /* if the requested protocol is not the default one 788 * or a TA1/PPS1 is present */ 789 if (((pps[1] & 0x0F) != default_protocol) || (PPS_HAS_PPS1(pps))) 790 { 791#ifdef O2MICRO_OZ776_PATCH 792 if ((OZ776 == ccid_desc->readerID) 793 || (OZ776_7772 == ccid_desc->readerID)) 794 { 795 /* convert from ATR_PROTOCOL_TYPE_T? to SCARD_PROTOCOL_T? */ 796 Protocol = default_protocol + 797 (SCARD_PROTOCOL_T0 - ATR_PROTOCOL_TYPE_T0); 798 DEBUG_INFO2("PPS not supported on O2Micro readers. Using T=%d", 799 Protocol - SCARD_PROTOCOL_T0); 800 } 801 else 802#endif 803 if (PPS_Exchange(reader_index, pps, &len, &pps[2]) != PPS_OK) 804 { 805 DEBUG_INFO("PPS_Exchange Failed"); 806 807 return IFD_ERROR_PTS_FAILURE; 808 } 809 } 810 } 811 812 /* Now we must set the reader parameters */ 813 (void)ATR_GetConvention(&atr, &convention); 814 815 /* specific mode and implicit parameters? (b5 of TA2) */ 816 if (atr.ib[1][ATR_INTERFACE_BYTE_TA].present 817 && (atr.ib[1][ATR_INTERFACE_BYTE_TA].value & 0x10)) 818 return IFD_COMMUNICATION_ERROR; 819 820 /* T=1 */ 821 if (SCARD_PROTOCOL_T1 == Protocol) 822 { 823 BYTE param[] = { 824 0x11, /* Fi/Di */ 825 0x10, /* TCCKS */ 826 0x00, /* GuardTime */ 827 0x4D, /* BWI/CWI */ 828 0x00, /* ClockStop */ 829 0x20, /* IFSC */ 830 0x00 /* NADValue */ 831 }; 832 int i; 833 t1_state_t *t1 = &(ccid_slot -> t1); 834 RESPONSECODE ret; 835 double f, d; 836 837 /* TA1 is not default */ 838 if (PPS_HAS_PPS1(pps)) 839 param[0] = pps[2]; 840 841 /* CRC checksum? */ 842 if (2 == t1->rc_bytes) 843 param[1] |= 0x01; 844 845 /* the CCID should ignore this bit */ 846 if (ATR_CONVENTION_INVERSE == convention) 847 param[1] |= 0x02; 848 849 /* get TC1 Extra guard time */ 850 if (atr.ib[0][ATR_INTERFACE_BYTE_TC].present) 851 param[2] = atr.ib[0][ATR_INTERFACE_BYTE_TC].value; 852 853 /* TBi (i>2) present? BWI/CWI */ 854 for (i=2; i<ATR_MAX_PROTOCOLS; i++) 855 if (atr.ib[i][ATR_INTERFACE_BYTE_TB].present) 856 { 857 DEBUG_COMM3("BWI/CWI (TB%d) present: 0x%02X", i+1, 858 atr.ib[i][ATR_INTERFACE_BYTE_TB].value); 859 param[3] = atr.ib[i][ATR_INTERFACE_BYTE_TB].value; 860 861 { 862 /* Hack for OpenPGP card */ 863 char openpgp_atr[] = { 0x3B, 0xFA, 0x13, 0x00, 0xFF, 0x81, 864 0x31, 0x80, 0x45, 0x00, 0x31, 0xC1, 0x73, 0xC0, 865 0x01, 0x00, 0x00, 0x90, 0x00, 0xB1 }; 866 867 if (0 == memcmp(ccid_slot->pcATRBuffer, openpgp_atr, 868 ccid_slot->nATRLength)) 869 /* change BWI from 4 to 7 to increase BWT from 870 * 1.4s to 11s and avoid a timeout during on 871 * board key generation (bogus card) */ 872 { 873 param[3] = 0x75; 874 DEBUG_COMM2("OpenPGP hack, using 0x%02X", param[3]); 875 } 876 } 877 878 /* only the first TBi (i>2) must be used */ 879 break; 880 } 881 882 /* compute communication timeout */ 883 (void)ATR_GetParameter(&atr, ATR_PARAMETER_F, &f); 884 (void)ATR_GetParameter(&atr, ATR_PARAMETER_D, &d); 885 ccid_desc->readTimeout = T1_card_timeout(f, d, param[2], 886 (param[3] & 0xF0) >> 4 /* BWI */, param[3] & 0x0F /* CWI */, 887 ccid_desc->dwDefaultClock); 888 889 /* TAi (i>2) present? IFSC */ 890 for (i=2; i<ATR_MAX_PROTOCOLS; i++) 891 if (atr.ib[i][ATR_INTERFACE_BYTE_TA].present) 892 { 893 DEBUG_COMM3("IFSC (TA%d) present: %d", i+1, 894 atr.ib[i][ATR_INTERFACE_BYTE_TA].value); 895 param[5] = atr.ib[i][ATR_INTERFACE_BYTE_TA].value; 896 897 /* only the first TAi (i>2) must be used */ 898 break; 899 } 900 901 DEBUG_COMM2("Timeout: %d seconds", ccid_desc->readTimeout); 902 903 ret = SetParameters(reader_index, 1, sizeof(param), param); 904 if (IFD_SUCCESS != ret) 905 return ret; 906 } 907 else 908 /* T=0 */ 909 { 910 BYTE param[] = { 911 0x11, /* Fi/Di */ 912 0x00, /* TCCKS */ 913 0x00, /* GuardTime */ 914 0x0A, /* WaitingInteger */ 915 0x00 /* ClockStop */ 916 }; 917 RESPONSECODE ret; 918 double f, d; 919 920 /* TA1 is not default */ 921 if (PPS_HAS_PPS1(pps)) 922 param[0] = pps[2]; 923 924 if (ATR_CONVENTION_INVERSE == convention) 925 param[1] |= 0x02; 926 927 /* get TC1 Extra guard time */ 928 if (atr.ib[0][ATR_INTERFACE_BYTE_TC].present) 929 param[2] = atr.ib[0][ATR_INTERFACE_BYTE_TC].value; 930 931 /* TC2 WWT */ 932 if (atr.ib[1][ATR_INTERFACE_BYTE_TC].present) 933 param[3] = atr.ib[1][ATR_INTERFACE_BYTE_TC].value; 934 935 /* compute communication timeout */ 936 (void)ATR_GetParameter(&atr, ATR_PARAMETER_F, &f); 937 (void)ATR_GetParameter(&atr, ATR_PARAMETER_D, &d); 938 939 ccid_desc->readTimeout = T0_card_timeout(f, d, param[2] /* TC1 */, 940 param[3] /* TC2 */, ccid_desc->dwDefaultClock); 941 942 DEBUG_COMM2("Communication timeout: %d seconds", 943 ccid_desc->readTimeout); 944 945 ret = SetParameters(reader_index, 0, sizeof(param), param); 946 if (IFD_SUCCESS != ret) 947 return ret; 948 } 949 950 /* set IFSC & IFSD in T=1 */ 951 if (SCARD_PROTOCOL_T1 == Protocol) 952 { 953 t1_state_t *t1 = &(ccid_slot -> t1); 954 int i; 955 956 /* TAi (i>2) present? */ 957 for (i=2; i<ATR_MAX_PROTOCOLS; i++) 958 if (atr.ib[i][ATR_INTERFACE_BYTE_TA].present) 959 { 960 DEBUG_COMM3("IFSC (TA%d) present: %d", i+1, 961 atr.ib[i][ATR_INTERFACE_BYTE_TA].value); 962 (void)t1_set_param(t1, IFD_PROTOCOL_T1_IFSC, 963 atr.ib[i][ATR_INTERFACE_BYTE_TA].value); 964 965 /* only the first TAi (i>2) must be used */ 966 break; 967 } 968 969 /* IFSD not negociated by the reader? */ 970 if (! (ccid_desc->dwFeatures & CCID_CLASS_AUTO_IFSD)) 971 { 972 DEBUG_COMM2("Negociate IFSD at %d", ccid_desc -> dwMaxIFSD); 973 if (t1_negotiate_ifsd(t1, 0, ccid_desc -> dwMaxIFSD) < 0) 974 return IFD_COMMUNICATION_ERROR; 975 } 976 (void)t1_set_param(t1, IFD_PROTOCOL_T1_IFSD, ccid_desc -> dwMaxIFSD); 977 978 DEBUG_COMM3("T=1: IFSC=%d, IFSD=%d", t1->ifsc, t1->ifsd); 979 } 980 981end: 982 /* store used protocol for use by the secure commands (verify/change PIN) */ 983 ccid_desc->cardProtocol = Protocol; 984 985 return IFD_SUCCESS; 986} /* IFDHSetProtocolParameters */ 987 988 989EXTERNAL RESPONSECODE IFDHPowerICC(DWORD Lun, DWORD Action, 990 PUCHAR Atr, PDWORD AtrLength) 991{ 992 /* 993 * This function controls the power and reset signals of the smartcard 994 * reader at the particular reader/slot specified by Lun. 995 * 996 * Action - Action to be taken on the card. 997 * 998 * IFD_POWER_UP - Power and reset the card if not done so (store the 999 * ATR and return it and it's length). 1000 * 1001 * IFD_POWER_DOWN - Power down the card if not done already 1002 * (Atr/AtrLength should be zero'd) 1003 * 1004 * IFD_RESET - Perform a quick reset on the card. If the card is not 1005 * powered power up the card. (Store and return the Atr/Length) 1006 * 1007 * Atr - Answer to Reset of the card. The driver is responsible for 1008 * caching this value in case IFDHGetCapabilities is called requesting 1009 * the ATR and it's length. This should not exceed MAX_ATR_SIZE. 1010 * 1011 * AtrLength - Length of the Atr. This should not exceed 1012 * MAX_ATR_SIZE. 1013 * 1014 * Notes: 1015 * 1016 * Memory cards without an ATR should return IFD_SUCCESS on reset but 1017 * the Atr should be zero'd and the length should be zero 1018 * 1019 * Reset errors should return zero for the AtrLength and return 1020 * IFD_ERROR_POWER_ACTION. 1021 * 1022 * returns: 1023 * 1024 * IFD_SUCCESS IFD_ERROR_POWER_ACTION IFD_COMMUNICATION_ERROR 1025 * IFD_NOT_SUPPORTED 1026 */ 1027 1028 unsigned int nlength; 1029 RESPONSECODE return_value = IFD_SUCCESS; 1030 unsigned char pcbuffer[RESP_BUF_SIZE]; 1031 int reader_index; 1032 const char *actions[] = { "PowerUp", "PowerDown", "Reset" }; 1033 unsigned int oldReadTimeout; 1034 _ccid_descriptor *ccid_descriptor; 1035 1036 /* By default, assume it won't work :) */ 1037 *AtrLength = 0; 1038 1039 if (-1 == (reader_index = LunToReaderIndex(Lun))) 1040 return IFD_COMMUNICATION_ERROR; 1041 1042 DEBUG_INFO4("action: %s, %s (lun: %X)", actions[Action-IFD_POWER_UP], 1043 CcidSlots[reader_index].readerName, Lun); 1044 1045 switch (Action) 1046 { 1047 case IFD_POWER_DOWN: 1048 /* Clear ATR buffer */ 1049 CcidSlots[reader_index].nATRLength = 0; 1050 *CcidSlots[reader_index].pcATRBuffer = '\0'; 1051 1052 /* Memorise the request */ 1053 CcidSlots[reader_index].bPowerFlags |= MASK_POWERFLAGS_PDWN; 1054 1055 /* send the command */ 1056 if (IFD_SUCCESS != CmdPowerOff(reader_index)) 1057 { 1058 DEBUG_CRITICAL("PowerDown failed"); 1059 return_value = IFD_ERROR_POWER_ACTION; 1060 goto end; 1061 } 1062 1063 /* clear T=1 context */ 1064 t1_release(&(get_ccid_slot(reader_index) -> t1)); 1065 break; 1066 1067 case IFD_POWER_UP: 1068 case IFD_RESET: 1069 /* save the current read timeout computed from card capabilities */ 1070 ccid_descriptor = get_ccid_descriptor(reader_index); 1071 oldReadTimeout = ccid_descriptor->readTimeout; 1072 1073 /* use a very long timeout since the card can use up to 1074 * (9600+12)*33 ETU in total 1075 * 12 ETU per byte 1076 * 9600 ETU max between each byte 1077 * 33 bytes max for ATR 1078 * 1 ETU = 372 cycles during ATR 1079 * with a 4 MHz clock => 29 seconds 1080 */ 1081 ccid_descriptor->readTimeout = 60; 1082 1083 nlength = sizeof(pcbuffer); 1084 return_value = CmdPowerOn(reader_index, &nlength, pcbuffer, 1085 PowerOnVoltage); 1086 1087 /* set back the old timeout */ 1088 ccid_descriptor->readTimeout = oldReadTimeout; 1089 1090 if (return_value != IFD_SUCCESS) 1091 { 1092 /* used by GemCore SIM PRO: no card is present */ 1093 get_ccid_descriptor(reader_index)->dwSlotStatus 1094 = IFD_ICC_NOT_PRESENT;; 1095 1096 DEBUG_CRITICAL("PowerUp failed"); 1097 return_value = IFD_ERROR_POWER_ACTION; 1098 goto end; 1099 } 1100 1101 /* Power up successful, set state variable to memorise it */ 1102 CcidSlots[reader_index].bPowerFlags |= MASK_POWERFLAGS_PUP; 1103 CcidSlots[reader_index].bPowerFlags &= ~MASK_POWERFLAGS_PDWN; 1104 1105 /* Reset is returned, even if TCK is wrong */ 1106 CcidSlots[reader_index].nATRLength = *AtrLength = 1107 (nlength < MAX_ATR_SIZE) ? nlength : MAX_ATR_SIZE; 1108 memcpy(Atr, pcbuffer, *AtrLength); 1109 memcpy(CcidSlots[reader_index].pcATRBuffer, pcbuffer, *AtrLength); 1110 1111 /* initialise T=1 context */ 1112 (void)t1_init(&(get_ccid_slot(reader_index) -> t1), reader_index); 1113 break; 1114 1115 default: 1116 DEBUG_CRITICAL("Action not supported"); 1117 return_value = IFD_NOT_SUPPORTED; 1118 } 1119end: 1120 1121 return return_value; 1122} /* IFDHPowerICC */ 1123 1124 1125EXTERNAL RESPONSECODE IFDHTransmitToICC(DWORD Lun, SCARD_IO_HEADER SendPci, 1126 PUCHAR TxBuffer, DWORD TxLength, 1127 PUCHAR RxBuffer, PDWORD RxLength, /*@unused@*/ PSCARD_IO_HEADER RecvPci) 1128{ 1129 /* 1130 * This function performs an APDU exchange with the card/slot 1131 * specified by Lun. The driver is responsible for performing any 1132 * protocol specific exchanges such as T=0/1 ... differences. Calling 1133 * this function will abstract all protocol differences. 1134 * 1135 * SendPci Protocol - 0, 1, .... 14 Length - Not used. 1136 * 1137 * TxBuffer - Transmit APDU example (0x00 0xA4 0x00 0x00 0x02 0x3F 1138 * 0x00) TxLength - Length of this buffer. RxBuffer - Receive APDU 1139 * example (0x61 0x14) RxLength - Length of the received APDU. This 1140 * function will be passed the size of the buffer of RxBuffer and this 1141 * function is responsible for setting this to the length of the 1142 * received APDU. This should be ZERO on all errors. The resource 1143 * manager will take responsibility of zeroing out any temporary APDU 1144 * buffers for security reasons. 1145 * 1146 * RecvPci Protocol - 0, 1, .... 14 Length - Not used. 1147 * 1148 * Notes: The driver is responsible for knowing what type of card it 1149 * has. If the current slot/card contains a memory card then this 1150 * command should ignore the Protocol and use the MCT style commands 1151 * for support for these style cards and transmit them appropriately. 1152 * If your reader does not support memory cards or you don't want to 1153 * then ignore this. 1154 * 1155 * RxLength should be set to zero on error. 1156 * 1157 * returns: 1158 * 1159 * IFD_SUCCESS IFD_COMMUNICATION_ERROR IFD_RESPONSE_TIMEOUT 1160 * IFD_ICC_NOT_PRESENT IFD_PROTOCOL_NOT_SUPPORTED 1161 */ 1162 1163 RESPONSECODE return_value; 1164 unsigned int rx_length; 1165 int reader_index; 1166 1167 if (-1 == (reader_index = LunToReaderIndex(Lun))) 1168 return IFD_COMMUNICATION_ERROR; 1169 1170 DEBUG_INFO3("%s (lun: %X)", CcidSlots[reader_index].readerName, Lun); 1171 1172 rx_length = *RxLength; 1173 return_value = CmdXfrBlock(reader_index, TxLength, TxBuffer, &rx_length, 1174 RxBuffer, SendPci.Protocol); 1175 if (IFD_SUCCESS == return_value) 1176 *RxLength = rx_length; 1177 else 1178 *RxLength = 0; 1179 1180 return return_value; 1181} /* IFDHTransmitToICC */ 1182 1183 1184EXTERNAL RESPONSECODE IFDHControl(DWORD Lun, DWORD dwControlCode, 1185 PUCHAR TxBuffer, DWORD TxLength, PUCHAR RxBuffer, DWORD RxLength, 1186 PDWORD pdwBytesReturned) 1187{ 1188 /* 1189 * This function performs a data exchange with the reader (not the 1190 * card) specified by Lun. Here XXXX will only be used. It is 1191 * responsible for abstracting functionality such as PIN pads, 1192 * biometrics, LCD panels, etc. You should follow the MCT, CTBCS 1193 * specifications for a list of accepted commands to implement. 1194 * 1195 * TxBuffer - Transmit data TxLength - Length of this buffer. RxBuffer 1196 * - Receive data RxLength - Length of the received data. This 1197 * function will be passed the length of the buffer RxBuffer and it 1198 * must set this to the length of the received data. 1199 * 1200 * Notes: RxLength should be zero on error. 1201 */ 1202 RESPONSECODE return_value = IFD_COMMUNICATION_ERROR; 1203 int reader_index; 1204 1205 reader_index = LunToReaderIndex(Lun); 1206 if ((-1 == reader_index) || (NULL == pdwBytesReturned)) 1207 return return_value; 1208 1209 DEBUG_INFO4("ControlCode: 0x%X, %s (lun: %X)", dwControlCode, 1210 CcidSlots[reader_index].readerName, Lun); 1211 DEBUG_INFO_XXD("Control TxBuffer: ", TxBuffer, TxLength); 1212 1213 /* Set the return length to 0 to avoid problems */ 1214 *pdwBytesReturned = 0; 1215 1216 if (IOCTL_SMARTCARD_VENDOR_IFD_EXCHANGE == dwControlCode) 1217 { 1218 if (FALSE == (DriverOptions & DRIVER_OPTION_CCID_EXCHANGE_AUTHORIZED)) 1219 { 1220 DEBUG_INFO("ifd exchange (Escape command) not allowed"); 1221 return_value = IFD_COMMUNICATION_ERROR; 1222 } 1223 else 1224 { 1225 unsigned int iBytesReturned; 1226 1227 iBytesReturned = RxLength; 1228 return_value = CmdEscape(reader_index, TxBuffer, TxLength, RxBuffer, 1229 &iBytesReturned); 1230 *pdwBytesReturned = iBytesReturned; 1231 } 1232 } 1233 1234 /* Implement the PC/SC v2.02.05 Part 10 IOCTL mechanism */ 1235 1236 /* Query for features */ 1237 if (CM_IOCTL_GET_FEATURE_REQUEST == dwControlCode) 1238 { 1239 unsigned int iBytesReturned = 0; 1240 PCSC_TLV_STRUCTURE *pcsc_tlv = (PCSC_TLV_STRUCTURE *)RxBuffer; 1241 1242 /* we need room for up to for records */ 1243 if (RxLength < 4 * sizeof(PCSC_TLV_STRUCTURE)) 1244 return IFD_COMMUNICATION_ERROR; 1245 1246 /* We can only support direct verify and/or modify currently */ 1247 if (get_ccid_descriptor(reader_index) -> bPINSupport 1248 & CCID_CLASS_PIN_VERIFY) 1249 { 1250 pcsc_tlv -> tag = FEATURE_VERIFY_PIN_DIRECT; 1251 pcsc_tlv -> length = 0x04; /* always 0x04 */ 1252 pcsc_tlv -> value = htonl(IOCTL_FEATURE_VERIFY_PIN_DIRECT); 1253 1254 pcsc_tlv++; 1255 iBytesReturned += sizeof(PCSC_TLV_STRUCTURE); 1256 } 1257 1258 if (get_ccid_descriptor(reader_index) -> bPINSupport 1259 & CCID_CLASS_PIN_MODIFY) 1260 { 1261 pcsc_tlv -> tag = FEATURE_MODIFY_PIN_DIRECT; 1262 pcsc_tlv -> length = 0x04; /* always 0x04 */ 1263 pcsc_tlv -> value = htonl(IOCTL_FEATURE_MODIFY_PIN_DIRECT); 1264 1265 pcsc_tlv++; 1266 iBytesReturned += sizeof(PCSC_TLV_STRUCTURE); 1267 } 1268 1269#ifdef FEATURE_IFD_PIN_PROPERTIES 1270 /* We can always forward wLcdLayout */ 1271 pcsc_tlv -> tag = FEATURE_IFD_PIN_PROPERTIES; 1272 pcsc_tlv -> length = 0x04; /* always 0x04 */ 1273 pcsc_tlv -> value = htonl(IOCTL_FEATURE_IFD_PIN_PROPERTIES); 1274 1275 pcsc_tlv++; 1276 iBytesReturned += sizeof(PCSC_TLV_STRUCTURE); 1277#endif 1278 1279 if (KOBIL_TRIBANK == get_ccid_descriptor(reader_index) -> readerID) 1280 { 1281 pcsc_tlv -> tag = FEATURE_MCT_READERDIRECT; 1282 pcsc_tlv -> length = 0x04; /* always 0x04 */ 1283 pcsc_tlv -> value = htonl(IOCTL_FEATURE_MCT_READERDIRECT); 1284 1285 pcsc_tlv++; 1286 iBytesReturned += sizeof(PCSC_TLV_STRUCTURE); 1287 } 1288 1289 *pdwBytesReturned = iBytesReturned; 1290 return_value = IFD_SUCCESS; 1291 } 1292 1293#ifdef FEATURE_IFD_PIN_PROPERTIES 1294 /* Get PIN handling capabilities */ 1295 if (IOCTL_FEATURE_IFD_PIN_PROPERTIES == dwControlCode) 1296 { 1297 PIN_PROPERTIES_STRUCTURE *caps = (PIN_PROPERTIES_STRUCTURE *)RxBuffer; 1298 1299 if (RxLength < sizeof(PIN_PROPERTIES_STRUCTURE)) 1300 return IFD_COMMUNICATION_ERROR; 1301 1302 /* Only give the LCD size for now */ 1303 caps -> wLcdLayout = get_ccid_descriptor(reader_index) -> wLcdLayout; 1304 caps -> wLcdMaxCharacters = 0x0000; 1305 caps -> wLcdMaxLines = 0x0000; 1306 caps -> bEntryValidationCondition = 0x07; /* Default */ 1307 caps -> bTimeOut2 = 0x00; /* We do not distinguish bTimeOut from TimeOut2 */ 1308 1309 *pdwBytesReturned = sizeof(*caps); 1310 return_value = IFD_SUCCESS; 1311 } 1312#endif 1313 1314 /* Verify a PIN, plain CCID */ 1315 if (IOCTL_FEATURE_VERIFY_PIN_DIRECT == dwControlCode) 1316 { 1317 unsigned int iBytesReturned; 1318 1319 iBytesReturned = RxLength; 1320 return_value = SecurePINVerify(reader_index, TxBuffer, TxLength, 1321 RxBuffer, &iBytesReturned); 1322 *pdwBytesReturned = iBytesReturned; 1323 } 1324 1325 /* Modify a PIN, plain CCID */ 1326 if (IOCTL_FEATURE_MODIFY_PIN_DIRECT == dwControlCode) 1327 { 1328 unsigned int iBytesReturned; 1329 1330 iBytesReturned = RxLength; 1331 return_value = SecurePINModify(reader_index, TxBuffer, TxLength, 1332 RxBuffer, &iBytesReturned); 1333 *pdwBytesReturned = iBytesReturned; 1334 } 1335 1336 /* MCT: Multifunctional Card Terminal */ 1337 if (IOCTL_FEATURE_MCT_READERDIRECT == dwControlCode) 1338 { 1339 if ( (TxBuffer[0] != 0x20) /* CLA */ 1340 || ((TxBuffer[1] & 0xF0) != 0x70) /* INS */ 1341 /* valid INS are 1342 * 0x70: SECODER INFO 1343 * 0x71: SECODER SELECT APPLICATION 1344 * 0x72: SECODER APPLICATION ACTIVE 1345 * 0x73: SECODER DATA CONFIRMATION 1346 * 0x74: SECODER PROCESS AUTHENTICATION TOKEN */ 1347 || ((TxBuffer[1] & 0x0F) > 4) 1348 || (TxBuffer[2] != 0x00) /* P1 */ 1349 || (TxBuffer[3] != 0x00) /* P2 */ 1350 || (TxBuffer[4] != 0x00) /* Lind */ 1351 ) 1352 { 1353 DEBUG_INFO("MCT Command refused by driver"); 1354 return_value = IFD_COMMUNICATION_ERROR; 1355 } 1356 else 1357 { 1358 unsigned int iBytesReturned; 1359 1360 /* we just transmit the buffer as a CCID Escape command */ 1361 iBytesReturned = RxLength; 1362 return_value = CmdEscape(reader_index, TxBuffer, TxLength, RxBuffer, 1363 &iBytesReturned); 1364 *pdwBytesReturned = iBytesReturned; 1365 } 1366 } 1367 1368 if (IFD_SUCCESS != return_value) 1369 *pdwBytesReturned = 0; 1370 1371 DEBUG_INFO_XXD("Control RxBuffer: ", RxBuffer, *pdwBytesReturned); 1372 return return_value; 1373} /* IFDHControl */ 1374 1375 1376EXTERNAL RESPONSECODE IFDHICCPresence(DWORD Lun) 1377{ 1378 /* 1379 * This function returns the status of the card inserted in the 1380 * reader/slot specified by Lun. It will return either: 1381 * 1382 * returns: IFD_ICC_PRESENT IFD_ICC_NOT_PRESENT 1383 * IFD_COMMUNICATION_ERROR 1384 */ 1385 1386 unsigned char pcbuffer[SIZE_GET_SLOT_STATUS]; 1387 RESPONSECODE return_value = IFD_COMMUNICATION_ERROR; 1388 int oldLogLevel; 1389 int reader_index; 1390 _ccid_descriptor *ccid_descriptor; 1391 unsigned int oldReadTimeout; 1392 1393 if (-1 == (reader_index = LunToReaderIndex(Lun))) 1394 return IFD_COMMUNICATION_ERROR; 1395 1396 DEBUG_PERIODIC3("%s (lun: %X)", CcidSlots[reader_index].readerName, Lun); 1397 1398 ccid_descriptor = get_ccid_descriptor(reader_index); 1399 1400 if (GEMCORESIMPRO == ccid_descriptor->readerID) 1401 { 1402 return_value = ccid_descriptor->dwSlotStatus; 1403 goto end; 1404 } 1405 1406 /* save the current read timeout computed from card capabilities */ 1407 oldReadTimeout = ccid_descriptor->readTimeout; 1408 1409 /* use default timeout since the reader may not be present anymore */ 1410 ccid_descriptor->readTimeout = DEFAULT_COM_READ_TIMEOUT; 1411 1412 /* if DEBUG_LEVEL_PERIODIC is not set we remove DEBUG_LEVEL_COMM */ 1413 oldLogLevel = LogLevel; 1414 if (! (LogLevel & DEBUG_LEVEL_PERIODIC)) 1415 LogLevel &= ~DEBUG_LEVEL_COMM; 1416 1417 return_value = CmdGetSlotStatus(reader_index, pcbuffer); 1418 1419 /* set back the old timeout */ 1420 ccid_descriptor->readTimeout = oldReadTimeout; 1421 1422 /* set back the old LogLevel */ 1423 LogLevel = oldLogLevel; 1424 1425 if (return_value != IFD_SUCCESS) 1426 return return_value; 1427 1428 return_value = IFD_COMMUNICATION_ERROR; 1429 switch (pcbuffer[7] & CCID_ICC_STATUS_MASK) /* bStatus */ 1430 { 1431 case CCID_ICC_PRESENT_ACTIVE: 1432 return_value = IFD_ICC_PRESENT; 1433 /* use default slot */ 1434 break; 1435 1436 case CCID_ICC_PRESENT_INACTIVE: 1437 if ((CcidSlots[reader_index].bPowerFlags == POWERFLAGS_RAZ) 1438 || (CcidSlots[reader_index].bPowerFlags & MASK_POWERFLAGS_PDWN)) 1439 /* the card was previously absent */ 1440 return_value = IFD_ICC_PRESENT; 1441 else 1442 { 1443 /* the card was previously present but has been 1444 * removed and inserted between two consecutive 1445 * IFDHICCPresence() calls */ 1446 CcidSlots[reader_index].bPowerFlags = POWERFLAGS_RAZ; 1447 return_value = IFD_ICC_NOT_PRESENT; 1448 } 1449 break; 1450 1451 case CCID_ICC_ABSENT: 1452 /* Reset ATR buffer */ 1453 CcidSlots[reader_index].nATRLength = 0; 1454 *CcidSlots[reader_index].pcATRBuffer = '\0'; 1455 1456 /* Reset PowerFlags */ 1457 CcidSlots[reader_index].bPowerFlags = POWERFLAGS_RAZ; 1458 1459 return_value = IFD_ICC_NOT_PRESENT; 1460 break; 1461 } 1462 1463#if 0 1464 /* SCR331-DI contactless reader */ 1465 if (((SCR331DI == ccid_descriptor->readerID) 1466 || (SDI010 == ccid_descriptor->readerID) 1467 || (SCR331DINTTCOM == ccid_descriptor->readerID)) 1468 && (ccid_descriptor->bCurrentSlotIndex > 0)) 1469 { 1470 unsigned char cmd[] = { 0x11 }; 1471 /* command: 11 ?? 1472 * response: 00 11 01 ?? no card 1473 * 01 04 00 ?? card present */ 1474 1475 unsigned char res[10]; 1476 unsigned int length_res = sizeof(res); 1477 RESPONSECODE ret; 1478 1479 /* if DEBUG_LEVEL_PERIODIC is not set we remove DEBUG_LEVEL_COMM */ 1480 oldLogLevel = LogLevel; 1481 if (! (LogLevel & DEBUG_LEVEL_PERIODIC)) 1482 LogLevel &= ~DEBUG_LEVEL_COMM; 1483 1484 ret = CmdEscape(reader_index, cmd, sizeof(cmd), res, &length_res); 1485 1486 /* set back the old LogLevel */ 1487 LogLevel = oldLogLevel; 1488 1489 if (ret != IFD_SUCCESS) 1490 { 1491 DEBUG_INFO("CmdEscape failed"); 1492 /* simulate a card absent */ 1493 res[0] = 0; 1494 } 1495 1496 if (0x01 == res[0]) 1497 return_value = IFD_ICC_PRESENT; 1498 else 1499 { 1500 /* Reset ATR buffer */ 1501 CcidSlots[reader_index].nATRLength = 0; 1502 *CcidSlots[reader_index].pcATRBuffer = '\0'; 1503 1504 /* Reset PowerFlags */ 1505 CcidSlots[reader_index].bPowerFlags = POWERFLAGS_RAZ; 1506 1507 return_value = IFD_ICC_NOT_PRESENT; 1508 } 1509 } 1510#endif 1511 1512end: 1513 DEBUG_PERIODIC2("Card %s", 1514 IFD_ICC_PRESENT == return_value ? "present" : "absent"); 1515 1516 return return_value; 1517} /* IFDHICCPresence */ 1518 1519 1520CcidDesc *get_ccid_slot(unsigned int reader_index) 1521{ 1522 return &CcidSlots[reader_index]; 1523} /* get_ccid_slot */ 1524 1525 1526void init_driver(void) 1527{ 1528 char keyValue[TOKEN_MAX_VALUE_SIZE]; 1529 char infofile[FILENAME_MAX]; 1530 char *e; 1531 1532 DEBUG_INFO("Driver version: " VERSION); 1533 1534 /* Info.plist full patch filename */ 1535 (void)snprintf(infofile, sizeof(infofile), "%s/%s/Contents/Info.plist", 1536 PCSCLITE_HP_DROPDIR, BUNDLE); 1537 1538 /* Log level */ 1539 if (0 == LTPBundleFindValueWithKey(infofile, "ifdLogLevel", keyValue, 0)) 1540 { 1541 /* convert from hex or dec or octal */ 1542 LogLevel = strtoul(keyValue, NULL, 0); 1543 1544 /* print the log level used */ 1545 DEBUG_INFO2("LogLevel: 0x%.4X", LogLevel); 1546 } 1547 1548 e = getenv("LIBCCID_ifdLogLevel"); 1549 if (e) 1550 { 1551 /* convert from hex or dec or octal */ 1552 LogLevel = strtoul(e, NULL, 0); 1553 1554 /* print the log level used */ 1555 DEBUG_INFO2("LogLevel from LIBCCID_ifdLogLevel: 0x%.4X", LogLevel); 1556 } 1557 1558 /* Driver options */ 1559 if (0 == LTPBundleFindValueWithKey(infofile, "ifdDriverOptions", keyValue, 0)) 1560 { 1561 /* convert from hex or dec or octal */ 1562 DriverOptions = strtoul(keyValue, NULL, 0); 1563 1564 /* print the log level used */ 1565 DEBUG_INFO2("DriverOptions: 0x%.4X", DriverOptions); 1566 } 1567 1568 /* get the voltage parameter */ 1569 switch ((DriverOptions >> 4) & 0x03) 1570 { 1571 case 0: 1572 PowerOnVoltage = VOLTAGE_5V; 1573 break; 1574 1575 case 1: 1576 PowerOnVoltage = VOLTAGE_3V; 1577 break; 1578 1579 case 2: 1580 PowerOnVoltage = VOLTAGE_1_8V; 1581 break; 1582 1583 case 3: 1584 PowerOnVoltage = VOLTAGE_AUTO; 1585 break; 1586 } 1587 1588 /* initialise the Lun to reader_index mapping */ 1589 InitReaderIndex(); 1590 1591 DebugInitialized = TRUE; 1592} /* init_driver */ 1593 1594 1595void extra_egt(ATR_t *atr, _ccid_descriptor *ccid_desc, DWORD Protocol) 1596{ 1597 /* This function use an EGT value for cards who comply with followings 1598 * criterias: 1599 * - TA1 > 11 1600 * - current EGT = 0x00 or 0xFF 1601 * - T=0 or (T=1 and CWI >= 2) 1602 * 1603 * Without this larger EGT some non ISO 7816-3 smart cards may not 1604 * communicate with the reader. 1605 * 1606 * This modification is harmless, the reader will just be less restrictive 1607 */ 1608 1609 unsigned int card_baudrate; 1610 unsigned int default_baudrate; 1611 double f, d; 1612 int i; 1613 1614 /* if TA1 not present */ 1615 if (! atr->ib[0][ATR_INTERFACE_BYTE_TA].present) 1616 return; 1617 1618 (void)ATR_GetParameter(atr, ATR_PARAMETER_D, &d); 1619 (void)ATR_GetParameter(atr, ATR_PARAMETER_F, &f); 1620 1621 /* may happen with non ISO cards */ 1622 if ((0 == f) || (0 == d)) 1623 return; 1624 1625 /* Baudrate = f x D/F */ 1626 card_baudrate = (unsigned int) (1000 * ccid_desc->dwDefaultClock * d / f); 1627 1628 default_baudrate = (unsigned int) (1000 * ccid_desc->dwDefaultClock 1629 * ATR_DEFAULT_D / ATR_DEFAULT_F); 1630 1631 /* TA1 > 11? */ 1632 if (card_baudrate <= default_baudrate) 1633 return; 1634 1635 /* Current EGT = 0 or FF? */ 1636 if (atr->ib[0][ATR_INTERFACE_BYTE_TC].present && 1637 ((0x00 == atr->ib[0][ATR_INTERFACE_BYTE_TC].value) || 1638 (0xFF == atr->ib[0][ATR_INTERFACE_BYTE_TC].value))) 1639 { 1640 if (SCARD_PROTOCOL_T0 == Protocol) 1641 { 1642 /* Init TC1 */ 1643 atr->ib[0][ATR_INTERFACE_BYTE_TC].present = TRUE; 1644 atr->ib[0][ATR_INTERFACE_BYTE_TC].value = 2; 1645 DEBUG_INFO("Extra EGT patch applied"); 1646 } 1647 1648 if (SCARD_PROTOCOL_T1 == Protocol) 1649 { 1650 /* TBi (i>2) present? BWI/CWI */ 1651 for (i=2; i<ATR_MAX_PROTOCOLS; i++) 1652 { 1653 /* CWI >= 2 ? */ 1654 if (atr->ib[i][ATR_INTERFACE_BYTE_TB].present && 1655 ((atr->ib[i][ATR_INTERFACE_BYTE_TB].value & 0x0F) >= 2)) 1656 { 1657 /* Init TC1 */ 1658 atr->ib[0][ATR_INTERFACE_BYTE_TC].present = TRUE; 1659 atr->ib[0][ATR_INTERFACE_BYTE_TC].value = 2; 1660 DEBUG_INFO("Extra EGT patch applied"); 1661 1662 /* only the first TBi (i>2) must be used */ 1663 break; 1664 } 1665 } 1666 } 1667 } 1668} /* extra_egt */ 1669 1670 1671static char find_baud_rate(unsigned int baudrate, unsigned int *list) 1672{ 1673 int i; 1674 1675 DEBUG_COMM2("Card baud rate: %d", baudrate); 1676 1677 /* Does the reader support the announced smart card data speed? */ 1678 for (i=0;; i++) 1679 { 1680 /* end of array marker */ 1681 if (0 == list[i]) 1682 break; 1683 1684 DEBUG_COMM2("Reader can do: %d", list[i]); 1685 1686 /* We must take into account that the card_baudrate integral value 1687 * is an approximative result, computed from the d/f float result. 1688 */ 1689 if ((baudrate < list[i] + 2) && (baudrate > list[i] - 2)) 1690 return TRUE; 1691 } 1692 1693 return FALSE; 1694} /* find_baud_rate */ 1695 1696 1697static unsigned int T0_card_timeout(double f, double d, int TC1, int TC2, 1698 int clock_frequency) 1699{ 1700 unsigned int timeout = DEFAULT_COM_READ_TIMEOUT; 1701 double EGT, WWT; 1702 unsigned int t; 1703 1704 /* Timeout applied on ISO_IN or ISO_OUT card exchange 1705 * we choose the maximum computed value. 1706 * 1707 * ISO_IN timeout is the sum of: 1708 * Terminal: Smart card: 1709 * 5 bytes header cmd -> 1710 * <- Procedure byte 1711 * 256 data bytes -> 1712 * <- SW1-SW2 1713 * = 261 EGT + 3 WWT + 3 WWT 1714 * 1715 * ISO_OUT Timeout is the sum of: 1716 * Terminal: Smart card: 1717 * 5 bytes header cmd -> 1718 * <- Procedure byte + 256 data bytes + SW1-SW2 1719 * = 5 EGT + 1 WWT + 259 WWT 1720 */ 1721 1722 /* clock_frequency is in kHz so the times are in milliseconds and not 1723 * in seconds */ 1724 1725 /* may happen with non ISO cards */ 1726 if ((0 == f) || (0 == d) || (0 == clock_frequency)) 1727 return 60; /* 60 seconds */ 1728 1729 /* EGT */ 1730 /* see ch. 6.5.3 Extra Guard Time, page 12 of ISO 7816-3 */ 1731 EGT = 12 * f / d / clock_frequency + (f / d) * TC1 / clock_frequency; 1732 1733 /* card WWT */ 1734 /* see ch. 8.2 Character level, page 15 of ISO 7816-3 */ 1735 WWT = 960 * TC2 * f / clock_frequency; 1736 1737 /* ISO in */ 1738 t = 261 * EGT + (3 + 3) * WWT; 1739 /* Convert from milliseonds to seconds rouned to the upper value 1740 * use +1 instead of ceil() to round up to the nearest integer 1741 * so we can avoid a dependency on the math library */ 1742 t = t/1000 +1; 1743 if (timeout < t) 1744 timeout = t; 1745 1746 /* ISO out */ 1747 t = 5 * EGT + (1 + 259) * WWT; 1748 t = t/1000 +1; 1749 if (timeout < t) 1750 timeout = t; 1751 1752 return timeout; 1753} /* T0_card_timeout */ 1754 1755 1756static unsigned int T1_card_timeout(double f, double d, int TC1, 1757 int BWI, int CWI, int clock_frequency) 1758{ 1759 double EGT, BWT, CWT, etu; 1760 unsigned int timeout; 1761 1762 /* Timeout applied on ISO in + ISO out card exchange 1763 * 1764 * Timeout is the sum of: 1765 * - ISO in delay between leading edge of the first character sent by the 1766 * interface device and the last one (NAD PCB LN APDU CKS) = 260 EGT, 1767 * - delay between ISO in and ISO out = BWT, 1768 * - ISO out delay between leading edge of the first character sent by the 1769 * card and the last one (NAD PCB LN DATAS CKS) = 260 CWT. 1770 */ 1771 1772 /* clock_frequency is in kHz so the times are in milliseconds and not 1773 * in seconds */ 1774 1775 /* may happen with non ISO cards */ 1776 if ((0 == f) || (0 == d) || (0 == clock_frequency)) 1777 return 60; /* 60 seconds */ 1778 1779 /* see ch. 6.5.2 Transmission factors F and D, page 12 of ISO 7816-3 */ 1780 etu = f / d / clock_frequency; 1781 1782 /* EGT */ 1783 /* see ch. 6.5.3 Extra Guard Time, page 12 of ISO 7816-3 */ 1784 EGT = 12 * etu + (f / d) * TC1 / clock_frequency; 1785 1786 /* card BWT */ 1787 /* see ch. 9.5.3.2 Block Waiting Time, page 20 of ISO 7816-3 */ 1788 BWT = 11 * etu + (1<<BWI) * 960 * 372 / clock_frequency; 1789 1790 /* card CWT */ 1791 /* see ch. 9.5.3.1 Caracter Waiting Time, page 20 of ISO 7816-3 */ 1792 CWT = (11 + (1<<CWI)) * etu; 1793 1794 timeout = 260*EGT + BWT + 260*CWT; 1795 1796 /* Convert from milliseonds to seconds rounded to the upper value 1797 * we use +1 instead of ceil() to round up to the nearest greater integer 1798 * so we can avoid a dependency on the math library */ 1799 timeout = timeout/1000 +1; 1800 1801 return timeout; 1802} /* T1_card_timeout */ 1803 1804