2 parse.c: parse CCID structure
3 Copyright (C) 2003-2010 Ludovic Rousseau
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 2 of the License, or
8 (at your option) any later version.
10 This program 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
13 GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License along
16 with this program; if not, write to the Free Software Foundation, Inc., 51
17 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
23 # include <sys/types.h>
31 /* define DISPLAY_EXTRA_VALUES to display the extra (invalid) values
32 * returned by bNumClockSupported and bNumDataRatesSupported */
33 #undef DISPLAY_EXTRA_VALUES
40 #define BLUE "\33[34m"
42 #define BRIGHT_RED "\33[01;31m"
43 #define GREEN "\33[32m"
44 #define MAGENTA "\33[35m"
45 #define NORMAL "\33[0m"
47 /* global variables used in ccid_usb.c but defined in ifdhandler.c */
48 int LogLevel = 1+2+4+8; /* full debug */
49 int DriverOptions = 0;
51 static int ccid_parse_interface_descriptor(libusb_device_handle *handle,
52 struct libusb_device_descriptor desc,
53 struct libusb_config_descriptor *config_desc,
55 const struct libusb_interface *usb_interface);
58 /*****************************************************************************
62 ****************************************************************************/
63 int main(int argc, char *argv[])
65 libusb_device **devs, *dev;
67 unsigned char buffer[256];
68 char class_ff = FALSE;
71 if ((argc > 1) && (0 == strcmp(argv[1], "-p")))
74 r = libusb_init(NULL);
77 (void)printf("libusb_init() failed\n");
81 cnt = libusb_get_device_list(NULL, &devs);
84 (void)printf("libusb_get_device_list() failed\n");
88 /* for every device */
90 while ((dev = devs[i++]) != NULL)
92 struct libusb_device_descriptor desc;
93 struct libusb_config_descriptor *config_desc;
94 struct libusb_device_handle *handle;
95 const struct libusb_interface *usb_interface = NULL;
101 r = libusb_open(dev, &handle);
104 (void)fprintf(stderr, "Can't libusb_open(): %s\n",
105 libusb_error_name(r));
108 (void)fprintf(stderr,
109 BRIGHT_RED "Please, restart the command as root\n" NORMAL);
115 r = libusb_get_device_descriptor(dev, &desc);
118 (void)fprintf(stderr,
119 BRIGHT_RED "failed to get device descriptor" NORMAL);
123 (void)fprintf(stderr,
124 "Parsing USB bus/device: %04X:%04X (bus %d, device %d)\n",
125 desc.idVendor, desc.idProduct,
126 libusb_get_bus_number(dev), libusb_get_device_address(dev));
128 (void)fprintf(stderr, " idVendor: 0x%04X", desc.idVendor);
129 r = libusb_get_string_descriptor_ascii(handle, desc.iManufacturer,
130 buffer, sizeof(buffer));
133 (void)fprintf(stderr, " Can't get iManufacturer string\n");
136 (void)fprintf(stderr,
137 BRIGHT_RED "Please, restart the command as root\n" NORMAL);
142 (void)fprintf(stderr,
143 " iManufacturer: " BLUE "%s\n" NORMAL, buffer);
145 (void)fprintf(stderr, " idProduct: 0x%04X", desc.idProduct);
146 r = libusb_get_string_descriptor_ascii(handle, desc.iProduct,
147 buffer, sizeof(buffer));
149 (void)fprintf(stderr, " Can't get iProduct string\n");
151 (void)fprintf(stderr, " iProduct: " BLUE "%s\n" NORMAL, buffer);
154 /* check if the device has bInterfaceClass == 11 */
155 r = libusb_get_active_config_descriptor(dev, &config_desc);
158 (void)fprintf(stderr, " Can't get config descriptor: %d\n", r);
159 (void)libusb_close(handle);
163 usb_interface = get_ccid_usb_interface(config_desc, &num);
164 if (NULL == usb_interface)
166 (void)libusb_close(handle);
167 /* only if we found no CCID interface */
169 (void)fprintf(stderr, RED " NOT a CCID/ICCD device\n" NORMAL);
172 if (!class_ff && (0xFF == usb_interface->altsetting->bInterfaceClass))
174 (void)libusb_close(handle);
175 (void)fprintf(stderr, MAGENTA " Found a possibly CCID/ICCD device (bInterfaceClass = 0xFF). Use %s -p\n" NORMAL, argv[0]);
178 (void)fprintf(stderr,
179 GREEN " Found a CCID/ICCD device at interface %d\n" NORMAL, num);
181 /* now we found a free reader and we try to use it */
183 if (NULL == dev->config)
185 (void)libusb_close(handle);
186 (void)fprintf(stderr, "No dev->config found for %s/%s\n",
187 bus->dirname, dev->filename);
193 interface = usb_interface->altsetting->bInterfaceNumber;
194 r = libusb_claim_interface(handle, interface);
197 (void)fprintf(stderr,
198 "Can't claim interface (bus %d, device %d): %s\n",
199 libusb_get_bus_number(dev), libusb_get_device_address(dev),
200 libusb_error_name(r));
201 (void)libusb_close(handle);
205 (void)fprintf(stderr,
206 BRIGHT_RED " Please, stop pcscd and retry\n\n" NORMAL);
209 /* maybe the device with Class = 0xFF is NOT a CCID
219 (void)ccid_parse_interface_descriptor(handle, desc, config_desc, num,
224 (void)libusb_release_interface(handle, interface);
226 /* check for another CCID interface on the same device */
231 if ((0 == nb) && (0 != geteuid()))
232 (void)fprintf(stderr,
233 "Can't find any CCID device.\nMaybe you must run parse as root?\n");
241 /*****************************************************************************
243 * Parse a CCID USB Descriptor
245 ****************************************************************************/
246 static int ccid_parse_interface_descriptor(libusb_device_handle *handle,
247 struct libusb_device_descriptor desc,
248 struct libusb_config_descriptor *config_desc,
250 const struct libusb_interface *usb_interface)
252 const struct libusb_interface_descriptor *usb_interface_descriptor;
253 const unsigned char *device_descriptor;
254 unsigned char buffer[256*sizeof(int)]; /* maximum is 256 records */
260 (void)printf(" idVendor: 0x%04X\n", desc.idVendor);
261 r = libusb_get_string_descriptor_ascii(handle, desc.iManufacturer,
262 buffer, sizeof(buffer));
265 (void)printf(" Can't get iManufacturer string\n");
268 (void)fprintf(stderr,
269 BRIGHT_RED "Please, restart the command as root\n\n" NORMAL);
274 (void)printf(" iManufacturer: %s\n", buffer);
276 (void)printf(" idProduct: 0x%04X\n", desc.idProduct);
277 r = libusb_get_string_descriptor_ascii(handle, desc.iProduct,
278 buffer, sizeof(buffer));
280 (void)printf(" Can't get iProduct string\n");
282 (void)printf(" iProduct: %s\n", buffer);
284 (void)printf(" bcdDevice: %X.%02X (firmware release?)\n",
285 desc.bcdDevice >> 8, desc.bcdDevice & 0xFF);
287 usb_interface_descriptor = get_ccid_usb_interface(config_desc, &num)->altsetting;
289 (void)printf(" bLength: %d\n", usb_interface_descriptor->bLength);
291 (void)printf(" bDescriptorType: %d\n", usb_interface_descriptor->bDescriptorType);
293 (void)printf(" bInterfaceNumber: %d\n", usb_interface_descriptor->bInterfaceNumber);
295 (void)printf(" bAlternateSetting: %d\n", usb_interface_descriptor->bAlternateSetting);
297 (void)printf(" bNumEndpoints: %d\n", usb_interface_descriptor->bNumEndpoints);
298 switch (usb_interface_descriptor->bNumEndpoints)
301 (void)printf(" Control only\n");
304 (void)printf(" Interrupt-IN\n");
307 (void)printf(" bulk-IN and bulk-OUT\n");
310 (void)printf(" bulk-IN, bulk-OUT and Interrupt-IN\n");
313 (void)printf(" UNKNOWN value\n");
316 (void)printf(" bInterfaceClass: 0x%02X", usb_interface_descriptor->bInterfaceClass);
317 if (usb_interface_descriptor->bInterfaceClass == 0x0b)
318 (void)printf(" [Chip Card Interface Device Class (CCID)]\n");
321 (void)printf("\n NOT A CCID DEVICE\n");
322 if (usb_interface_descriptor->bInterfaceClass != 0xFF)
325 (void)printf(" Class is 0xFF (proprietary)\n");
328 (void)printf(" bInterfaceSubClass: %d\n",
329 usb_interface_descriptor->bInterfaceSubClass);
330 if (usb_interface_descriptor->bInterfaceSubClass)
331 (void)printf(" UNSUPPORTED SubClass\n");
333 (void)printf(" bInterfaceProtocol: %d\n",
334 usb_interface_descriptor->bInterfaceProtocol);
335 switch (usb_interface_descriptor->bInterfaceProtocol)
338 (void)printf(" bulk transfer, optional interrupt-IN (CCID)\n");
341 (void)printf(" ICCD Version A, Control transfers, (no interrupt-IN)\n");
344 (void)printf(" ICCD Version B, Control transfers, (optional interrupt-IN)\n");
347 (void)printf(" UNSUPPORTED InterfaceProtocol\n");
350 r = libusb_get_string_descriptor_ascii(handle, usb_interface_descriptor->iInterface,
351 buffer, sizeof(buffer));
353 (void)printf(" Can't get iInterface string\n");
355 (void)printf(" iInterface: %s\n", buffer);
357 device_descriptor = get_ccid_device_descriptor(usb_interface);
358 if (NULL == device_descriptor)
360 (void)printf("\n NOT A CCID DEVICE\n");
365 * CCID Class Descriptor
367 (void)printf(" CCID Class Descriptor\n");
369 (void)printf(" bLength: 0x%02X\n", device_descriptor[0]);
370 if (device_descriptor[0] != 0x36)
372 (void)printf(" UNSUPPORTED bLength\n");
376 (void)printf(" bDescriptorType: 0x%02X\n", device_descriptor[1]);
377 if (device_descriptor[1] != 0x21)
379 if (0xFF == device_descriptor[1])
380 (void)printf(" PROPRIETARY bDescriptorType\n");
383 (void)printf(" UNSUPPORTED bDescriptorType\n");
388 (void)printf(" bcdCCID: %X.%02X\n", device_descriptor[3], device_descriptor[2]);
389 (void)printf(" bMaxSlotIndex: 0x%02X\n", device_descriptor[4]);
390 (void)printf(" bVoltageSupport: 0x%02X\n", device_descriptor[5]);
391 if (device_descriptor[5] & 0x01)
392 (void)printf(" 5.0V\n");
393 if (device_descriptor[5] & 0x02)
394 (void)printf(" 3.0V\n");
395 if (device_descriptor[5] & 0x04)
396 (void)printf(" 1.8V\n");
398 (void)printf(" dwProtocols: 0x%02X%02X 0x%02X%02X\n", device_descriptor[9], device_descriptor[8],
399 device_descriptor[7], device_descriptor[6]);
400 if (device_descriptor[6] & 0x01)
401 (void)printf(" T=0\n");
402 if (device_descriptor[6] & 0x02)
403 (void)printf(" T=1\n");
405 (void)printf(" dwDefaultClock: %.3f MHz\n", dw2i(device_descriptor, 10)/1000.0);
406 (void)printf(" dwMaximumClock: %.3f MHz\n", dw2i(device_descriptor, 14)/1000.0);
407 (void)printf(" bNumClockSupported: %d%s\n", device_descriptor[18],
408 device_descriptor[18] ? "" : " (will use whatever is returned)");
412 /* See CCID 3.7.2 page 25 */
413 n = libusb_control_transfer(handle,
414 0xA1, /* request type */
415 0x02, /* GET CLOCK FREQUENCIES */
417 usb_interface_descriptor->bInterfaceNumber, /* interface */
422 /* we got an error? */
425 (void)(void)printf(" IFD does not support GET CLOCK FREQUENCIES request: %s\n", libusb_error_name(n));
428 (void)fprintf(stderr,
429 BRIGHT_RED " Please, stop pcscd and retry\n\n" NORMAL);
434 if (n % 4) /* not a multiple of 4 */
435 (void)printf(" wrong size for GET CLOCK FREQUENCIES: %d\n", n);
440 /* we do not get the expected number of data rates */
441 if ((n != device_descriptor[18]*4) && device_descriptor[18])
443 (void)printf(" Got %d clock frequencies but was expecting %d\n",
444 n/4, device_descriptor[18]);
446 /* we got more data than expected */
447 #ifndef DISPLAY_EXTRA_VALUES
448 if (n > device_descriptor[18]*4)
449 n = device_descriptor[18]*4;
454 (void)printf(" Support %d kHz\n", dw2i(buffer, i));
457 (void)printf(" dwDataRate: %d bps\n", dw2i(device_descriptor, 19));
458 (void)printf(" dwMaxDataRate: %d bps\n", dw2i(device_descriptor, 23));
459 (void)printf(" bNumDataRatesSupported: %d%s\n", device_descriptor[27],
460 device_descriptor[27] ? "" : " (will use whatever is returned)");
464 /* See CCID 3.7.3 page 25 */
465 n = libusb_control_transfer(handle,
466 0xA1, /* request type */
467 0x03, /* GET DATA RATES */
469 usb_interface_descriptor->bInterfaceNumber, /* interface */
474 /* we got an error? */
476 (void)printf(" IFD does not support GET_DATA_RATES request: %s\n",
477 libusb_error_name(n));
479 if (n % 4) /* not a multiple of 4 */
480 (void)printf(" wrong size for GET_DATA_RATES: %d\n", n);
485 /* we do not get the expected number of data rates */
486 if ((n != device_descriptor[27]*4) && device_descriptor[27])
488 (void)printf(" Got %d data rates but was expecting %d\n", n/4,
489 device_descriptor[27]);
491 /* we got more data than expected */
492 #ifndef DISPLAY_EXTRA_VALUES
493 if (n > device_descriptor[27]*4)
494 n = device_descriptor[27]*4;
499 (void)printf(" Support %d bps\n", dw2i(buffer, i));
502 (void)printf(" dwMaxIFSD: %d\n", dw2i(device_descriptor, 28));
503 (void)printf(" dwSynchProtocols: 0x%08X\n", dw2i(device_descriptor, 32));
504 if (device_descriptor[32] & 0x01)
505 (void)printf(" 2-wire protocol\n");
506 if (device_descriptor[32] & 0x02)
507 (void)printf(" 3-wire protocol\n");
508 if (device_descriptor[32] & 0x04)
509 (void)printf(" I2C protocol\n");
511 (void)printf(" dwMechanical: 0x%08X\n", dw2i(device_descriptor, 36));
512 if (device_descriptor[36] == 0)
513 (void)printf(" No special characteristics\n");
514 if (device_descriptor[36] & 0x01)
515 (void)printf(" Card accept mechanism\n");
516 if (device_descriptor[36] & 0x02)
517 (void)printf(" Card ejection mechanism\n");
518 if (device_descriptor[36] & 0x04)
519 (void)printf(" Card capture mechanism\n");
520 if (device_descriptor[36] & 0x08)
521 (void)printf(" Card lock/unlock mechanism\n");
523 (void)printf(" dwFeatures: 0x%08X\n", dw2i(device_descriptor, 40));
524 if (dw2i(device_descriptor, 40) == 0)
525 (void)printf(" No special characteristics\n");
526 if (device_descriptor[40] & 0x02)
527 (void)printf(" ....02 Automatic parameter configuration based on ATR data\n");
528 if (device_descriptor[40] & 0x04)
529 (void)printf(" ....04 Automatic activation of ICC on inserting\n");
530 if (device_descriptor[40] & 0x08)
531 (void)printf(" ....08 Automatic ICC voltage selection\n");
532 if (device_descriptor[40] & 0x10)
533 (void)printf(" ....10 Automatic ICC clock frequency change according to parameters\n");
534 if (device_descriptor[40] & 0x20)
535 (void)printf(" ....20 Automatic baud rate change according to frequency and Fi, Di params\n");
536 if (device_descriptor[40] & 0x40)
537 (void)printf(" ....40 Automatic parameters negotiation made by the CCID\n");
538 if (device_descriptor[40] & 0x80)
539 (void)printf(" ....80 Automatic PPS made by the CCID\n");
540 if (device_descriptor[41] & 0x01)
541 (void)printf(" ..01.. CCID can set ICC in clock stop mode\n");
542 if (device_descriptor[41] & 0x02)
543 (void)printf(" ..02.. NAD value other than 00 accepted (T=1)\n");
544 if (device_descriptor[41] & 0x04)
545 (void)printf(" ..04.. Automatic IFSD exchange as first exchange (T=1)\n");
546 if (device_descriptor[41] & 0x08)
547 (void)printf(" ..08.. Unknown (ICCD?)\n");
548 switch (device_descriptor[42] & 0x07)
551 (void)printf(" 00.... Character level exchange\n");
555 (void)printf(" 01.... TPDU level exchange\n");
559 (void)printf(" 02.... Short APDU level exchange\n");
563 (void)printf(" 04.... Short and Extended APDU level exchange\n");
566 if (device_descriptor[42] & 0x10)
567 (void)printf(" 10.... USB Wake up signaling supported on card insertion and removal\n");
569 (void)printf(" dwMaxCCIDMessageLength: %d bytes\n", dw2i(device_descriptor, 44));
570 (void)printf(" bClassGetResponse: 0x%02X\n", device_descriptor[48]);
571 if (0xFF == device_descriptor[48])
572 (void)printf(" echoes the APDU class\n");
573 (void)printf(" bClassEnvelope: 0x%02X\n", device_descriptor[49]);
574 if (0xFF == device_descriptor[49])
575 (void)printf(" echoes the APDU class\n");
576 (void)printf(" wLcdLayout: 0x%04X\n", (device_descriptor[51] << 8)+device_descriptor[50]);
577 if (device_descriptor[51])
578 (void)printf(" %2d lines\n", device_descriptor[51]);
579 if (device_descriptor[50])
580 (void)printf(" %2d characters per line\n", device_descriptor[50]);
581 (void)printf(" bPINSupport: 0x%02X\n", device_descriptor[52]);
582 if (device_descriptor[52] & 0x01)
583 (void)printf(" PIN Verification supported\n");
584 if (device_descriptor[52] & 0x02)
585 (void)printf(" PIN Modification supported\n");
586 (void)printf(" bMaxCCIDBusySlots: %d\n", device_descriptor[53]);
589 } /* ccid_parse_interface_descriptor */