Initial commit
[platform/upstream/ccid.git] / src / parse.c
1 /*
2     parse.c: parse CCID structure
3     Copyright (C) 2003-2010   Ludovic Rousseau
4
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.
9
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.
14
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.
18 */
19
20 #include <stdio.h>
21 #include <string.h>
22 # ifdef S_SPLINT_S
23 # include <sys/types.h>
24 # endif
25 #include <errno.h>
26 #include <unistd.h>
27
28 #include "defs.h"
29 #include "ccid.h"
30
31 /* define DISPLAY_EXTRA_VALUES to display the extra (invalid) values
32  * returned by bNumClockSupported and bNumDataRatesSupported */
33 #undef DISPLAY_EXTRA_VALUES
34
35 #ifndef TRUE
36 #define TRUE 1
37 #define FALSE 0
38 #endif
39
40 #define BLUE "\33[34m"
41 #define RED "\33[31m"
42 #define BRIGHT_RED "\33[01;31m"
43 #define GREEN "\33[32m"
44 #define MAGENTA "\33[35m"
45 #define NORMAL "\33[0m"
46
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;
50
51 static int ccid_parse_interface_descriptor(libusb_device_handle *handle,
52         struct libusb_device_descriptor desc,
53         struct libusb_config_descriptor *config_desc,
54         int num,
55         const struct libusb_interface *usb_interface);
56
57
58 /*****************************************************************************
59  *
60  *                                      main
61  *
62  ****************************************************************************/
63 int main(int argc, char *argv[])
64 {
65         libusb_device **devs, *dev;
66         int nb = 0, r, i;
67         unsigned char buffer[256];
68         char class_ff = FALSE;
69         ssize_t cnt;
70
71         if ((argc > 1) && (0 == strcmp(argv[1], "-p")))
72                 class_ff = TRUE;
73
74         r = libusb_init(NULL);
75         if (r < 0)
76         {
77                 (void)printf("libusb_init() failed\n");
78         return r;
79         }
80
81         cnt = libusb_get_device_list(NULL, &devs);
82     if (cnt < 0)
83         {
84                 (void)printf("libusb_get_device_list() failed\n");
85         return (int)cnt;
86         }
87
88         /* for every device */
89         i = 0;
90         while ((dev = devs[i++]) != NULL)
91         {
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;
96 #ifndef __APPLE__
97                 int interface;
98 #endif
99                 int num = 0;
100
101                 r = libusb_open(dev, &handle);
102                 if (r < 0)
103                 {
104                         (void)fprintf(stderr, "Can't libusb_open(): %s\n",
105                                 libusb_error_name(r));
106                         if (getuid())
107                         {
108                                 (void)fprintf(stderr,
109                                         BRIGHT_RED "Please, restart the command as root\n" NORMAL);
110                                 return 1;
111                         }
112                         continue;
113                 }
114
115                 r = libusb_get_device_descriptor(dev, &desc);
116         if (r < 0)
117                 {
118             (void)fprintf(stderr,
119                                 BRIGHT_RED "failed to get device descriptor" NORMAL);
120             return 1;
121         }
122
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));
127
128                 (void)fprintf(stderr, " idVendor:  0x%04X", desc.idVendor);
129                 r = libusb_get_string_descriptor_ascii(handle, desc.iManufacturer,
130                         buffer, sizeof(buffer));
131                 if (r < 0)
132                 {
133                         (void)fprintf(stderr, "  Can't get iManufacturer string\n");
134                         if (getuid())
135                         {
136                                 (void)fprintf(stderr,
137                                         BRIGHT_RED "Please, restart the command as root\n" NORMAL);
138                                 return 1;
139                         }
140                 }
141                 else
142                         (void)fprintf(stderr,
143                                 "  iManufacturer: " BLUE "%s\n" NORMAL, buffer);
144
145                 (void)fprintf(stderr, " idProduct: 0x%04X", desc.idProduct);
146                 r = libusb_get_string_descriptor_ascii(handle, desc.iProduct,
147                         buffer, sizeof(buffer));
148                 if (r < 0)
149                         (void)fprintf(stderr, "  Can't get iProduct string\n");
150                 else
151                         (void)fprintf(stderr, "  iProduct: " BLUE "%s\n" NORMAL, buffer);
152
153 again:
154                 /* check if the device has bInterfaceClass == 11 */
155                 r = libusb_get_active_config_descriptor(dev, &config_desc);
156                 if (r < 0)
157                 {
158                         (void)fprintf(stderr, "  Can't get config descriptor: %d\n", r);
159                         (void)libusb_close(handle);
160                         continue;
161                 }
162
163                 usb_interface = get_ccid_usb_interface(config_desc, &num);
164                 if (NULL == usb_interface)
165                 {
166                         (void)libusb_close(handle);
167                         /* only if we found no CCID interface */
168                         if (0 == num)
169                                 (void)fprintf(stderr, RED "  NOT a CCID/ICCD device\n" NORMAL);
170                         continue;
171                 }
172                 if (!class_ff && (0xFF == usb_interface->altsetting->bInterfaceClass))
173                 {
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]);
176                         continue;
177                 }
178                 (void)fprintf(stderr,
179                         GREEN "  Found a CCID/ICCD device at interface %d\n" NORMAL, num);
180
181                 /* now we found a free reader and we try to use it */
182 #if 0
183                 if (NULL == dev->config)
184                 {
185                         (void)libusb_close(handle);
186                         (void)fprintf(stderr, "No dev->config found for %s/%s\n",
187                                         bus->dirname, dev->filename);
188                         continue;
189                 }
190 #endif
191
192 #ifndef __APPLE__
193                 interface = usb_interface->altsetting->bInterfaceNumber;
194                 r = libusb_claim_interface(handle, interface);
195                 if (r < 0)
196                 {
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);
202
203                         if (EBUSY == errno)
204                         {
205                                 (void)fprintf(stderr,
206                                         BRIGHT_RED " Please, stop pcscd and retry\n\n" NORMAL);
207
208                                 if (class_ff)
209                                         /* maybe the device with Class = 0xFF is NOT a CCID
210                                          * reader */
211                                         continue;
212                                 else
213                                         return TRUE;
214                         }
215                         continue;
216                 }
217 #endif
218
219                 (void)ccid_parse_interface_descriptor(handle, desc, config_desc, num,
220                         usb_interface);
221                 nb++;
222
223 #ifndef __APPLE__
224                 (void)libusb_release_interface(handle, interface);
225 #endif
226                 /* check for another CCID interface on the same device */
227                 num++;
228                 goto again;
229         }
230
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");
234
235         libusb_exit(NULL);
236
237         return 0;
238 } /* main */
239
240
241 /*****************************************************************************
242  *
243  *                                      Parse a CCID USB Descriptor
244  *
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,
249         int num,
250         const struct libusb_interface *usb_interface)
251 {
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 */
255         int r;
256
257         /*
258          * Vendor/model name
259          */
260         (void)printf(" idVendor: 0x%04X\n", desc.idVendor);
261         r = libusb_get_string_descriptor_ascii(handle, desc.iManufacturer,
262                 buffer, sizeof(buffer));
263         if (r < 0)
264         {
265                 (void)printf("  Can't get iManufacturer string\n");
266                 if (getuid())
267                 {
268                         (void)fprintf(stderr,
269                                 BRIGHT_RED "Please, restart the command as root\n\n" NORMAL);
270                         return TRUE;
271                 }
272         }
273         else
274                 (void)printf("  iManufacturer: %s\n", buffer);
275
276         (void)printf(" idProduct: 0x%04X\n", desc.idProduct);
277         r =  libusb_get_string_descriptor_ascii(handle, desc.iProduct,
278                 buffer, sizeof(buffer));
279         if (r < 0)
280                 (void)printf("  Can't get iProduct string\n");
281         else
282                 (void)printf("  iProduct: %s\n", buffer);
283
284         (void)printf(" bcdDevice: %X.%02X (firmware release?)\n",
285                 desc.bcdDevice >> 8, desc.bcdDevice & 0xFF);
286
287         usb_interface_descriptor = get_ccid_usb_interface(config_desc, &num)->altsetting;
288
289         (void)printf(" bLength: %d\n", usb_interface_descriptor->bLength);
290
291         (void)printf(" bDescriptorType: %d\n", usb_interface_descriptor->bDescriptorType);
292
293         (void)printf(" bInterfaceNumber: %d\n", usb_interface_descriptor->bInterfaceNumber);
294
295         (void)printf(" bAlternateSetting: %d\n", usb_interface_descriptor->bAlternateSetting);
296
297         (void)printf(" bNumEndpoints: %d\n", usb_interface_descriptor->bNumEndpoints);
298         switch (usb_interface_descriptor->bNumEndpoints)
299         {
300                 case 0:
301                         (void)printf("  Control only\n");
302                         break;
303                 case 1:
304                         (void)printf("  Interrupt-IN\n");
305                         break;
306                 case 2:
307                         (void)printf("  bulk-IN and bulk-OUT\n");
308                         break;
309                 case 3:
310                         (void)printf("  bulk-IN, bulk-OUT and Interrupt-IN\n");
311                         break;
312                 default:
313                         (void)printf("  UNKNOWN value\n");
314         }
315
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");
319         else
320         {
321                 (void)printf("\n  NOT A CCID DEVICE\n");
322                 if (usb_interface_descriptor->bInterfaceClass != 0xFF)
323                         return TRUE;
324                 else
325                         (void)printf("  Class is 0xFF (proprietary)\n");
326         }
327
328         (void)printf(" bInterfaceSubClass: %d\n",
329                 usb_interface_descriptor->bInterfaceSubClass);
330         if (usb_interface_descriptor->bInterfaceSubClass)
331                 (void)printf("  UNSUPPORTED SubClass\n");
332
333         (void)printf(" bInterfaceProtocol: %d\n",
334                 usb_interface_descriptor->bInterfaceProtocol);
335         switch (usb_interface_descriptor->bInterfaceProtocol)
336         {
337                 case 0:
338                         (void)printf("  bulk transfer, optional interrupt-IN (CCID)\n");
339                         break;
340                 case 1:
341                         (void)printf("  ICCD Version A, Control transfers, (no interrupt-IN)\n");
342                         break;
343                 case 2:
344                         (void)printf("  ICCD Version B, Control transfers, (optional interrupt-IN)\n");
345                         break;
346                 default:
347                         (void)printf("  UNSUPPORTED InterfaceProtocol\n");
348         }
349
350         r = libusb_get_string_descriptor_ascii(handle, usb_interface_descriptor->iInterface,
351                 buffer, sizeof(buffer));
352         if (r < 0)
353                 (void)printf(" Can't get iInterface string\n");
354         else
355                 (void)printf(" iInterface: %s\n", buffer);
356
357         device_descriptor = get_ccid_device_descriptor(usb_interface);
358         if (NULL == device_descriptor)
359         {
360                 (void)printf("\n  NOT A CCID DEVICE\n");
361                 return TRUE;
362         }
363
364         /*
365          * CCID Class Descriptor
366          */
367         (void)printf(" CCID Class Descriptor\n");
368
369         (void)printf("  bLength: 0x%02X\n", device_descriptor[0]);
370         if (device_descriptor[0] != 0x36)
371         {
372                 (void)printf("   UNSUPPORTED bLength\n");
373                 return TRUE;
374         }
375
376         (void)printf("  bDescriptorType: 0x%02X\n", device_descriptor[1]);
377         if (device_descriptor[1] != 0x21)
378         {
379                 if (0xFF == device_descriptor[1])
380                         (void)printf("   PROPRIETARY bDescriptorType\n");
381                 else
382                 {
383                         (void)printf("   UNSUPPORTED bDescriptorType\n");
384                         return TRUE;
385                 }
386         }
387
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");
397
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");
404
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)");
409         {
410                 int n;
411
412                 /* See CCID 3.7.2 page 25 */
413                 n = libusb_control_transfer(handle,
414                         0xA1, /* request type */
415                         0x02, /* GET CLOCK FREQUENCIES */
416                         0x00, /* value */
417                         usb_interface_descriptor->bInterfaceNumber, /* interface */
418                         buffer,
419                         sizeof(buffer),
420                         2 * 1000);
421
422                 /* we got an error? */
423                 if (n <= 0)
424                 {
425                         (void)(void)printf("   IFD does not support GET CLOCK FREQUENCIES request: %s\n", libusb_error_name(n));
426                         if (EBUSY == errno)
427                         {
428                                 (void)fprintf(stderr,
429                                         BRIGHT_RED "   Please, stop pcscd and retry\n\n" NORMAL);
430                                 return TRUE;
431                         }
432                 }
433                 else
434                         if (n % 4)      /* not a multiple of 4 */
435                                 (void)printf("   wrong size for GET CLOCK FREQUENCIES: %d\n", n);
436                         else
437                         {
438                                 int i;
439
440                                 /* we do not get the expected number of data rates */
441                                 if ((n != device_descriptor[18]*4) && device_descriptor[18])
442                                 {
443                                         (void)printf("   Got %d clock frequencies but was expecting %d\n",
444                                                 n/4, device_descriptor[18]);
445
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;
450 #endif
451                                 }
452
453                                 for (i=0; i<n; i+=4)
454                                         (void)printf("   Support %d kHz\n", dw2i(buffer, i));
455                         }
456         }
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)");
461         {
462                 int n;
463
464                 /* See CCID 3.7.3 page 25 */
465                 n = libusb_control_transfer(handle,
466                         0xA1, /* request type */
467                         0x03, /* GET DATA RATES */
468                         0x00, /* value */
469                         usb_interface_descriptor->bInterfaceNumber, /* interface */
470                         buffer,
471                         sizeof(buffer),
472                         2 * 1000);
473
474                 /* we got an error? */
475                 if (n <= 0)
476                         (void)printf("   IFD does not support GET_DATA_RATES request: %s\n",
477                                 libusb_error_name(n));
478                 else
479                         if (n % 4)      /* not a multiple of 4 */
480                                 (void)printf("   wrong size for GET_DATA_RATES: %d\n", n);
481                         else
482                         {
483                                 int i;
484
485                                 /* we do not get the expected number of data rates */
486                                 if ((n != device_descriptor[27]*4) && device_descriptor[27])
487                                 {
488                                         (void)printf("   Got %d data rates but was expecting %d\n", n/4,
489                                                 device_descriptor[27]);
490
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;
495 #endif
496                                 }
497
498                                 for (i=0; i<n; i+=4)
499                                         (void)printf("   Support %d bps\n", dw2i(buffer, i));
500                         }
501         }
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");
510
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");
522
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)
549         {
550                 case 0x00:
551                         (void)printf("   00.... Character level exchange\n");
552                         break;
553
554                 case 0x01:
555                         (void)printf("   01.... TPDU level exchange\n");
556                         break;
557
558                 case 0x02:
559                         (void)printf("   02.... Short APDU level exchange\n");
560                         break;
561
562                 case 0x04:
563                         (void)printf("   04.... Short and Extended APDU level exchange\n");
564                         break;
565         }
566         if (device_descriptor[42] & 0x10)
567                 (void)printf("   10.... USB Wake up signaling supported on card insertion and removal\n");
568
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]);
587
588         return FALSE;
589 } /* ccid_parse_interface_descriptor */
590