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