~ubuntu-branches/ubuntu/oneiric/gnupg2/oneiric-updates

« back to all changes in this revision

Viewing changes to scd/ccid-driver.c

  • Committer: Bazaar Package Importer
  • Author(s): Thomas Viehmann
  • Date: 2008-10-04 10:25:53 UTC
  • mfrom: (5.1.15 intrepid)
  • Revision ID: james.westby@ubuntu.com-20081004102553-fv62pp8dsitxli47
Tags: 2.0.9-3.1
* Non-maintainer upload.
* agent/gpg-agent.c: Deinit the threading library before exec'ing
  the command to run in --daemon mode. And because that still doesn't
  restore the sigprocmask, do that manually. Closes: #499569

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
1
/* ccid-driver.c - USB ChipCardInterfaceDevices driver
2
 
 *      Copyright (C) 2003, 2004, 2005 Free Software Foundation, Inc.
3
 
 *      Written by Werner Koch.
 
2
 * Copyright (C) 2003, 2004, 2005, 2006, 2007 Free Software Foundation, Inc.
 
3
 * Written by Werner Koch.
4
4
 *
5
5
 * This file is part of GnuPG.
6
6
 *
7
7
 * GnuPG is free software; you can redistribute it and/or modify
8
8
 * it under the terms of the GNU General Public License as published by
9
 
 * the Free Software Foundation; either version 2 of the License, or
 
9
 * the Free Software Foundation; either version 3 of the License, or
10
10
 * (at your option) any later version.
11
11
 *
12
12
 * GnuPG is distributed in the hope that it will be useful,
15
15
 * GNU General Public License for more details.
16
16
 *
17
17
 * You should have received a copy of the GNU General Public License
18
 
 * along with this program; if not, write to the Free Software
19
 
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
20
 
 * USA.
 
18
 * along with this program; if not, see <http://www.gnu.org/licenses/>.
21
19
 *
22
20
 * ALTERNATIVELY, this file may be distributed under the terms of the
23
21
 * following license, in which case the provisions of this license are
53
51
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
54
52
 * OF THE POSSIBILITY OF SUCH DAMAGE.
55
53
 *
56
 
 * $Date: 2005/06/16 08:11:59 $
 
54
 * $Date: 2007-07-04 21:49:40 +0200 (Wed, 04 Jul 2007) $
57
55
 */
58
56
 
59
57
 
67
65
   portable access to USB.
68
66
 
69
67
   This driver has been tested with the SCM SCR335 and SPR532
70
 
   smartcard readers and requires that a reader implements the TPDU
71
 
   level exchange and does fully automatic initialization.
 
68
   smartcard readers and requires that a reader implements APDU or
 
69
   TPDU level exchange and does fully automatic initialization.
72
70
*/
73
71
 
74
72
#ifdef HAVE_CONFIG_H
82
80
#include <stdlib.h>
83
81
#include <string.h>
84
82
#include <assert.h>
 
83
#include <sys/types.h>
 
84
#include <sys/stat.h>
 
85
#include <fcntl.h>
85
86
 
86
87
#include <usb.h>
87
88
 
194
195
 
195
196
/* We need to know the vendor to do some hacks. */
196
197
enum {
 
198
  VENDOR_CHERRY = 0x046a,
197
199
  VENDOR_SCM    = 0x04e6,
198
 
  VENDOR_CHERRY = 0x046a,
199
 
  VENDOR_GEMPC  = 0x08e6
 
200
  VENDOR_OMNIKEY= 0x076b,
 
201
  VENDOR_GEMPC  = 0x08e6,
 
202
  VENDOR_KAAN   = 0x0d46
 
203
};
 
204
 
 
205
/* A list and a table with special transport descriptions. */
 
206
enum {
 
207
  TRANSPORT_USB    = 0, /* Standard USB transport. */
 
208
  TRANSPORT_CM4040 = 1  /* As used by the Cardman 4040. */
 
209
};
 
210
 
 
211
static struct
 
212
 
213
  char *name;  /* Device name. */
 
214
  int  type;
 
215
 
 
216
} transports[] = {
 
217
  { "/dev/cmx0", TRANSPORT_CM4040 },  
 
218
  { "/dev/cmx1", TRANSPORT_CM4040 },  
 
219
  { NULL },
200
220
};
201
221
 
202
222
 
206
226
{
207
227
  usb_dev_handle *idev;
208
228
  char *rid;
 
229
  int dev_fd;  /* -1 for USB transport or file descriptor of the
 
230
                   transport device. */
209
231
  unsigned short id_vendor;
210
232
  unsigned short id_product;
211
233
  unsigned short bcd_device;
306
328
}
307
329
  
308
330
 
309
 
 
 
331
/* Given a handle used for special transport prepare it for use.  In
 
332
   particular setup all information in way that resembles what
 
333
   parse_cccid_descriptor does. */
 
334
static void
 
335
prepare_special_transport (ccid_driver_t handle)
 
336
{
 
337
  assert (!handle->id_vendor);
 
338
 
 
339
  handle->nonnull_nad = 0;
 
340
  handle->auto_ifsd = 0;
 
341
  handle->max_ifsd = 32;
 
342
  handle->ifsd = 0;
 
343
  handle->has_pinpad = 0;
 
344
  handle->apdu_level = 0;
 
345
  switch (handle->id_product)
 
346
    {
 
347
    case TRANSPORT_CM4040:
 
348
      DEBUGOUT ("setting up transport for CardMan 4040\n");
 
349
      handle->apdu_level = 1;
 
350
      break;
 
351
 
 
352
    default: assert (!"transport not defined");
 
353
    }
 
354
}
310
355
 
311
356
/* Parse a CCID descriptor, optionally print all available features
312
357
   and test whether this reader is usable by this driver.  Returns 0
615
660
  char *rid;
616
661
  char prefix[20];
617
662
 
618
 
  sprintf (prefix, "%04X:%04X:", (vendor & 0xfff), (product & 0xffff));
 
663
  sprintf (prefix, "%04X:%04X:", (vendor & 0xffff), (product & 0xffff));
619
664
  rid = get_escaped_usb_string (idev, serialno_index, prefix, ":0");
620
665
  if (!rid)
621
666
    {
658
703
}
659
704
 
660
705
 
 
706
/* Helper for scan_or_find_devices. This function returns true if a
 
707
   requested device has been found or the caller should stop scanning
 
708
   for other reasons. */
 
709
static int
 
710
scan_or_find_usb_device (int scan_mode,
 
711
                         int *readerno, int *count, char **rid_list,
 
712
                         const char *readerid,
 
713
                         struct usb_device *dev,
 
714
                         char **r_rid,
 
715
                         struct usb_device **r_dev,
 
716
                         usb_dev_handle **r_idev,
 
717
                         unsigned char **ifcdesc_extra,
 
718
                         size_t *ifcdesc_extra_len,
 
719
                         int *interface_number,
 
720
                         int *ep_bulk_out, int *ep_bulk_in, int *ep_intr)
 
721
{
 
722
  int cfg_no;
 
723
  int ifc_no;
 
724
  int set_no;
 
725
  struct usb_config_descriptor *config;
 
726
  struct usb_interface *interface;          
 
727
  struct usb_interface_descriptor *ifcdesc;
 
728
  char *rid;
 
729
  usb_dev_handle *idev;
 
730
 
 
731
  *r_idev = NULL;
 
732
 
 
733
  for (cfg_no=0; cfg_no < dev->descriptor.bNumConfigurations; cfg_no++)
 
734
    {
 
735
      config = dev->config + cfg_no;
 
736
      if(!config)
 
737
        continue;
 
738
 
 
739
      for (ifc_no=0; ifc_no < config->bNumInterfaces; ifc_no++)
 
740
        {
 
741
          interface = config->interface + ifc_no;
 
742
          if (!interface)
 
743
            continue;
 
744
                  
 
745
          for (set_no=0; set_no < interface->num_altsetting; set_no++)
 
746
            {
 
747
              ifcdesc = (interface->altsetting + set_no);
 
748
              /* The second condition is for older SCM SPR 532 who did
 
749
                 not know about the assigned CCID class.  Instead of
 
750
                 trying to interpret the strings we simply check the
 
751
                 product ID. */
 
752
              if (ifcdesc && ifcdesc->extra
 
753
                  && ((ifcdesc->bInterfaceClass == 11
 
754
                       && ifcdesc->bInterfaceSubClass == 0
 
755
                       && ifcdesc->bInterfaceProtocol == 0)
 
756
                      || (ifcdesc->bInterfaceClass == 255
 
757
                          && dev->descriptor.idVendor == VENDOR_SCM
 
758
                          && dev->descriptor.idProduct == 0xe003)))
 
759
                {
 
760
                  idev = usb_open (dev);
 
761
                  if (!idev)
 
762
                    {
 
763
                      DEBUGOUT_1 ("usb_open failed: %s\n",
 
764
                                  strerror (errno));
 
765
                      continue; /* with next setting. */
 
766
                    }
 
767
                  
 
768
                  rid = make_reader_id (idev,
 
769
                                        dev->descriptor.idVendor,
 
770
                                        dev->descriptor.idProduct,
 
771
                                        dev->descriptor.iSerialNumber);
 
772
                  if (rid)
 
773
                    {
 
774
                      if (scan_mode)
 
775
                        {
 
776
                          char *p;
 
777
                          
 
778
                          /* We are collecting infos about all
 
779
                             available CCID readers.  Store them and
 
780
                             continue. */
 
781
                          DEBUGOUT_2 ("found CCID reader %d (ID=%s)\n",
 
782
                                      *count, rid );
 
783
                          p = malloc ((*rid_list? strlen (*rid_list):0) + 1
 
784
                                      + strlen (rid) + 1);
 
785
                          if (p)
 
786
                            {
 
787
                              *p = 0;
 
788
                              if (*rid_list)
 
789
                                {
 
790
                                  strcat (p, *rid_list);
 
791
                                  free (*rid_list);
 
792
                                }
 
793
                              strcat (p, rid);
 
794
                              strcat (p, "\n");
 
795
                              *rid_list = p;
 
796
                            }
 
797
                          else /* Out of memory. */
 
798
                            free (rid);
 
799
                          
 
800
                          rid = NULL;
 
801
                          ++*count;
 
802
                        }
 
803
                      else if (!*readerno
 
804
                               || (*readerno < 0
 
805
                                   && readerid
 
806
                                   && !strcmp (readerid, rid)))
 
807
                        {
 
808
                          /* We found the requested reader. */
 
809
                          if (ifcdesc_extra && ifcdesc_extra_len)
 
810
                            {
 
811
                              *ifcdesc_extra = malloc (ifcdesc
 
812
                                                       ->extralen);
 
813
                              if (!*ifcdesc_extra)
 
814
                                {
 
815
                                  usb_close (idev);
 
816
                                  free (rid);
 
817
                                  return 1; /* Out of core. */
 
818
                                }
 
819
                              memcpy (*ifcdesc_extra, ifcdesc->extra,
 
820
                                      ifcdesc->extralen);
 
821
                              *ifcdesc_extra_len = ifcdesc->extralen;
 
822
                            }
 
823
 
 
824
                          if (interface_number)
 
825
                            *interface_number = (ifcdesc->bInterfaceNumber);
 
826
 
 
827
                          if (ep_bulk_out)
 
828
                            *ep_bulk_out = find_endpoint (ifcdesc, 0);
 
829
                          if (ep_bulk_in)
 
830
                            *ep_bulk_in = find_endpoint (ifcdesc, 1);
 
831
                          if (ep_intr)
 
832
                            *ep_intr = find_endpoint (ifcdesc, 2);
 
833
 
 
834
                          if (r_dev)
 
835
                            *r_dev = dev;
 
836
                          if (r_rid)
 
837
                            {
 
838
                              *r_rid = rid;
 
839
                              rid = NULL;
 
840
                            }
 
841
                          else
 
842
                            free (rid);
 
843
                          
 
844
                          *r_idev = idev;
 
845
                          return 1; /* Found requested device. */
 
846
                        }
 
847
                      else
 
848
                        {
 
849
                          /* This is not yet the reader we want.
 
850
                             fixme: We should avoid the extra usb_open
 
851
                             in this case. */
 
852
                          if (*readerno >= 0)
 
853
                            --*readerno;
 
854
                        }
 
855
                      free (rid);
 
856
                    }
 
857
                  
 
858
                  usb_close (idev);
 
859
                  idev = NULL;
 
860
                  return 0;
 
861
                }
 
862
            }
 
863
        }
 
864
    }
 
865
 
 
866
  return 0;
 
867
}
661
868
 
662
869
/* Combination function to either scan all CCID devices or to find and
663
870
   open one specific device. 
664
871
 
 
872
   The function returns 0 if a reader has been found or when a scan
 
873
   returned without error.
 
874
 
665
875
   With READERNO = -1 and READERID is NULL, scan mode is used and
666
876
   R_RID should be the address where to store the list of reader_ids
667
877
   we found.  If on return this list is empty, no CCID device has been
671
881
   With READERNO >= 0 or READERID is not NULL find mode is used.  This
672
882
   uses the same algorithm as the scan mode but stops and returns at
673
883
   the entry number READERNO and return the handle for the the opened
674
 
   USB device. If R_ID is not NULL it will receive the reader ID of
 
884
   USB device. If R_RID is not NULL it will receive the reader ID of
675
885
   that device.  If R_DEV is not NULL it will the device pointer of
676
886
   that device.  If IFCDESC_EXTRA is NOT NULL it will receive a
677
887
   malloced copy of the interfaces "extra: data filed;
678
 
   IFCDESC_EXTRA_LEN receive the lengtyh of this field.  If there is
 
888
   IFCDESC_EXTRA_LEN receive the length of this field.  If there is
679
889
   no reader with number READERNO or that reader is not usable by our
680
890
   implementation NULL will be returned.  The caller must close a
681
891
   returned USB device handle and free (if not passed as NULL) the
684
894
   IFCDESC_EXTRA_LEN.  With READERID being -1 the function stops if
685
895
   the READERID was found.
686
896
 
 
897
   If R_FD is not -1 on return the device is not using USB for
 
898
   transport but the device associated with that file descriptor.  In
 
899
   this case INTERFACE will receive the transport type and the other
 
900
   USB specific return values are not used; the return value is
 
901
   (void*)(1).
 
902
 
687
903
   Note that the first entry of the returned reader ID list in scan mode
688
904
   corresponds with a READERNO of 0 in find mode.
689
905
*/
690
 
static usb_dev_handle *
 
906
static int
691
907
scan_or_find_devices (int readerno, const char *readerid,
692
908
                      char **r_rid,
693
909
                      struct usb_device **r_dev,
694
910
                      unsigned char **ifcdesc_extra,
695
911
                      size_t *ifcdesc_extra_len,
696
912
                      int *interface_number,
697
 
                      int *ep_bulk_out, int *ep_bulk_in, int *ep_intr)
 
913
                      int *ep_bulk_out, int *ep_bulk_in, int *ep_intr,
 
914
                      usb_dev_handle **r_idev,
 
915
                      int *r_fd)
698
916
{
699
917
  char *rid_list = NULL;
700
918
  int count = 0;
702
920
  struct usb_device *dev = NULL;
703
921
  usb_dev_handle *idev = NULL;
704
922
  int scan_mode = (readerno == -1 && !readerid);
 
923
  int i;
705
924
 
706
 
   /* Set return values to a default. */
 
925
  /* Set return values to a default. */
707
926
  if (r_rid)
708
927
    *r_rid = NULL;
709
928
  if (r_dev)
714
933
    *ifcdesc_extra_len = 0;
715
934
  if (interface_number)
716
935
    *interface_number = 0;
 
936
  if (r_idev)
 
937
    *r_idev = NULL;
 
938
  if (r_fd)
 
939
    *r_fd = -1;
717
940
 
718
941
  /* See whether we want scan or find mode. */
719
942
  if (scan_mode) 
734
957
    {
735
958
      for (dev = bus->devices; dev; dev = dev->next)
736
959
        {
737
 
          int cfg_no;
738
 
          
739
 
          for (cfg_no=0; cfg_no < dev->descriptor.bNumConfigurations; cfg_no++)
 
960
          if (scan_or_find_usb_device (scan_mode, &readerno, &count, &rid_list,
 
961
                                       readerid,
 
962
                                       dev,
 
963
                                       r_rid,
 
964
                                       r_dev,
 
965
                                       &idev,
 
966
                                       ifcdesc_extra,
 
967
                                       ifcdesc_extra_len,
 
968
                                       interface_number,
 
969
                                       ep_bulk_out, ep_bulk_in, ep_intr))
740
970
            {
741
 
              struct usb_config_descriptor *config = dev->config + cfg_no;
742
 
              int ifc_no;
743
 
 
744
 
              if(!config)
745
 
                continue;
746
 
 
747
 
              for (ifc_no=0; ifc_no < config->bNumInterfaces; ifc_no++)
 
971
              /* Found requested device or out of core. */
 
972
              if (!idev)
748
973
                {
749
 
                  struct usb_interface *interface
750
 
                    = config->interface + ifc_no;
751
 
                  int set_no;
752
 
                  
753
 
                  if (!interface)
754
 
                    continue;
755
 
                  
756
 
                  for (set_no=0; set_no < interface->num_altsetting; set_no++)
757
 
                    {
758
 
                      struct usb_interface_descriptor *ifcdesc
759
 
                        = interface->altsetting + set_no;
760
 
                      char *rid;
761
 
                      
762
 
                      /* The second condition is for some SCM Micro
763
 
                         SPR 532 which does not know about the
764
 
                         assigned CCID class. Instead of trying to
765
 
                         interpret the strings we simply look at the
766
 
                         product ID. */
767
 
                      if (ifcdesc && ifcdesc->extra
768
 
                          && (   (ifcdesc->bInterfaceClass == 11
769
 
                                  && ifcdesc->bInterfaceSubClass == 0
770
 
                                  && ifcdesc->bInterfaceProtocol == 0)
771
 
                              || (ifcdesc->bInterfaceClass == 255
772
 
                                  && dev->descriptor.idVendor == 0x04e6
773
 
                                  && dev->descriptor.idProduct == 0xe003)))
774
 
                        {
775
 
                          idev = usb_open (dev);
776
 
                          if (!idev)
777
 
                            {
778
 
                              DEBUGOUT_1 ("usb_open failed: %s\n",
779
 
                                          strerror (errno));
780
 
                              continue;
781
 
                            }
782
 
                              
783
 
                          rid = make_reader_id (idev,
784
 
                                                dev->descriptor.idVendor,
785
 
                                                dev->descriptor.idProduct,
786
 
                                                dev->descriptor.iSerialNumber);
787
 
                          if (rid)
788
 
                            {
789
 
                              if (scan_mode)
790
 
                                {
791
 
                                  char *p;
792
 
 
793
 
                                  /* We are collecting infos about all
794
 
                                     available CCID readers.  Store
795
 
                                     them and continue. */
796
 
                                  DEBUGOUT_2 ("found CCID reader %d "
797
 
                                              "(ID=%s)\n",
798
 
                                              count, rid );
799
 
                                  if ((p = malloc ((rid_list?
800
 
                                                    strlen (rid_list):0)
801
 
                                                   + 1 + strlen (rid)
802
 
                                                   + 1)))
803
 
                                    {
804
 
                                      *p = 0;
805
 
                                      if (rid_list)
806
 
                                        {
807
 
                                          strcat (p, rid_list);
808
 
                                          free (rid_list);
809
 
                                        }
810
 
                                      strcat (p, rid);
811
 
                                      strcat (p, "\n");
812
 
                                      rid_list = p;
813
 
                                    }
814
 
                                  else /* Out of memory. */
815
 
                                    free (rid);
816
 
                                  rid = NULL;
817
 
                                  count++;
818
 
                                }
819
 
                              else if (!readerno
820
 
                                       || (readerno < 0
821
 
                                           && readerid
822
 
                                           && !strcmp (readerid, rid)))
823
 
                                {
824
 
                                  /* We found the requested reader. */
825
 
                                  if (ifcdesc_extra && ifcdesc_extra_len)
826
 
                                    {
827
 
                                      *ifcdesc_extra = malloc (ifcdesc
828
 
                                                               ->extralen);
829
 
                                      if (!*ifcdesc_extra)
830
 
                                        {
831
 
                                          usb_close (idev);
832
 
                                          free (rid);
833
 
                                          return NULL; /* Out of core. */
834
 
                                        }
835
 
                                      memcpy (*ifcdesc_extra, ifcdesc->extra,
836
 
                                              ifcdesc->extralen);
837
 
                                      *ifcdesc_extra_len = ifcdesc->extralen;
838
 
                                    }
839
 
                                  if (interface_number)
840
 
                                    *interface_number = (ifcdesc->
841
 
                                                         bInterfaceNumber);
842
 
                                  if (ep_bulk_out)
843
 
                                    *ep_bulk_out = find_endpoint (ifcdesc, 0);
844
 
                                  if (ep_bulk_in)
845
 
                                    *ep_bulk_in = find_endpoint (ifcdesc, 1);
846
 
                                  if (ep_intr)
847
 
                                    *ep_intr = find_endpoint (ifcdesc, 2);
848
 
 
849
 
 
850
 
                                  if (r_dev)
851
 
                                    *r_dev = dev;
852
 
                                  if (r_rid)
853
 
                                    {
854
 
                                      *r_rid = rid;
855
 
                                      rid = NULL;
856
 
                                    }
857
 
                                  else
858
 
                                    free (rid);
859
 
                                  return idev; /* READY. */
860
 
                                }
861
 
                              else
862
 
                                {
863
 
                                  /* This is not yet the reader we
864
 
                                     want.  fixme: We could avoid the
865
 
                                     extra usb_open in this case. */
866
 
                                  if (readerno >= 0)
867
 
                                    readerno--;
868
 
                                }
869
 
                              free (rid);
870
 
                            }
871
 
                          
872
 
                          usb_close (idev);
873
 
                          idev = NULL;
874
 
                          goto next_device;
875
 
                        }
876
 
                    }
 
974
                  free (rid_list);
 
975
                  return -1; /* error */
877
976
                }
878
 
            }
879
 
        next_device:
880
 
          ;
881
 
        }
 
977
              *r_idev = idev;
 
978
              return 0; 
 
979
            }
 
980
        }
 
981
    }
 
982
 
 
983
  /* Now check whether there are any devices with special transport types. */
 
984
  for (i=0; transports[i].name; i++)
 
985
    {
 
986
      int fd;
 
987
      char *rid, *p;
 
988
 
 
989
      fd = open (transports[i].name, O_RDWR);
 
990
      if (fd == -1 && scan_mode && errno == EBUSY)
 
991
        {
 
992
          /* Ignore this error in scan mode because it indicates that
 
993
             the device exists but is already open (most likely by us)
 
994
             and thus in general suitable as a reader.  */
 
995
        }
 
996
      else if (fd == -1)
 
997
        {
 
998
          DEBUGOUT_2 ("failed to open `%s': %s\n",
 
999
                     transports[i].name, strerror (errno));
 
1000
          continue;
 
1001
        }
 
1002
 
 
1003
      rid = malloc (strlen (transports[i].name) + 30 + 10);
 
1004
      if (!rid)
 
1005
        {
 
1006
          if (fd != -1)
 
1007
            close (fd);
 
1008
          free (rid_list);
 
1009
          return -1; /* Error. */
 
1010
        }
 
1011
      sprintf (rid, "0000:%04X:%s:0", transports[i].type, transports[i].name);
 
1012
      if (scan_mode)
 
1013
        {
 
1014
          DEBUGOUT_2 ("found CCID reader %d (ID=%s)\n", count, rid);
 
1015
          p = malloc ((rid_list? strlen (rid_list):0) + 1 + strlen (rid) + 1);
 
1016
          if (!p)
 
1017
            {
 
1018
              if (fd != -1)
 
1019
                close (fd);
 
1020
              free (rid_list);
 
1021
              free (rid);
 
1022
              return -1; /* Error. */
 
1023
            }
 
1024
          *p = 0;
 
1025
          if (rid_list)
 
1026
            {
 
1027
              strcat (p, rid_list);
 
1028
              free (rid_list);
 
1029
            }
 
1030
          strcat (p, rid);
 
1031
          strcat (p, "\n");
 
1032
          rid_list = p;
 
1033
          ++count;
 
1034
        }
 
1035
      else if (!readerno ||
 
1036
               (readerno < 0 && readerid && !strcmp (readerid, rid)))
 
1037
        {
 
1038
          /* Found requested device. */
 
1039
          if (interface_number)
 
1040
            *interface_number = transports[i].type;
 
1041
          if (r_rid)
 
1042
            *r_rid = rid;
 
1043
          else
 
1044
            free (rid);
 
1045
          if (r_fd)
 
1046
            *r_fd = fd;
 
1047
          return 0; /* Okay, found device */
 
1048
        }
 
1049
      else /* This is not yet the reader we want. */
 
1050
        {
 
1051
          if (readerno >= 0)
 
1052
            --readerno;
 
1053
        }
 
1054
      free (rid);
 
1055
      if (fd != -1)
 
1056
        close (fd);
882
1057
    }
883
1058
 
884
1059
  if (scan_mode)
885
 
    *r_rid = rid_list;
886
 
 
887
 
  return NULL;
 
1060
    {
 
1061
      *r_rid = rid_list;
 
1062
      return 0;
 
1063
    }
 
1064
  else
 
1065
    return -1;
888
1066
}
889
1067
 
890
1068
 
891
 
/* Set the level of debugging to to usea dn return the old level.  -1
 
1069
/* Set the level of debugging to LEVEL and return the old level.  -1
892
1070
   just returns the old level.  A level of 0 disables debugging, 1
893
1071
   enables debugging, 2 enables additional tracing of the T=1
894
1072
   protocol, other values are not yet defined. */
913
1091
      initialized_usb = 1;
914
1092
    }
915
1093
 
916
 
  scan_or_find_devices (-1, NULL, &reader_list, NULL, NULL, NULL, NULL,
917
 
                        NULL, NULL, NULL);
 
1094
  if (scan_or_find_devices (-1, NULL, &reader_list, NULL, NULL, NULL, NULL,
 
1095
                            NULL, NULL, NULL, NULL, NULL))
 
1096
    return NULL; /* Error. */
918
1097
  return reader_list;
919
1098
}
920
1099
 
927
1106
  int rc = 0;
928
1107
  struct usb_device *dev = NULL;
929
1108
  usb_dev_handle *idev = NULL;
 
1109
  int dev_fd = -1;
930
1110
  char *rid = NULL;
931
1111
  unsigned char *ifcdesc_extra = NULL;
932
1112
  size_t ifcdesc_extra_len;
959
1139
  else
960
1140
    readerno = 0;  /* Default. */
961
1141
 
962
 
  idev = scan_or_find_devices (readerno, readerid, &rid, &dev,
963
 
                               &ifcdesc_extra, &ifcdesc_extra_len,
964
 
                               &ifc_no, &ep_bulk_out, &ep_bulk_in, &ep_intr);
965
 
  if (!idev)
 
1142
  if (scan_or_find_devices (readerno, readerid, &rid, &dev,
 
1143
                            &ifcdesc_extra, &ifcdesc_extra_len,
 
1144
                            &ifc_no, &ep_bulk_out, &ep_bulk_in, &ep_intr,
 
1145
                            &idev, &dev_fd) )
966
1146
    {
967
1147
      if (readerno == -1)
968
1148
        DEBUGOUT_1 ("no CCID reader with ID %s\n", readerid );
980
1160
      rc = CCID_DRIVER_ERR_OUT_OF_CORE;
981
1161
      goto leave;
982
1162
    }
983
 
  (*handle)->idev = idev;
984
1163
  (*handle)->rid = rid;
985
 
  (*handle)->id_vendor = dev->descriptor.idVendor;
986
 
  (*handle)->id_product = dev->descriptor.idProduct;
987
 
  (*handle)->bcd_device = dev->descriptor.bcdDevice;
988
 
  (*handle)->ifc_no = ifc_no;
989
 
  (*handle)->ep_bulk_out = ep_bulk_out;
990
 
  (*handle)->ep_bulk_in = ep_bulk_in;
991
 
  (*handle)->ep_intr = ep_intr;
 
1164
  if (idev) /* Regular USB transport. */
 
1165
    {
 
1166
      (*handle)->idev = idev;
 
1167
      (*handle)->dev_fd = -1;
 
1168
      (*handle)->id_vendor = dev->descriptor.idVendor;
 
1169
      (*handle)->id_product = dev->descriptor.idProduct;
 
1170
      (*handle)->bcd_device = dev->descriptor.bcdDevice;
 
1171
      (*handle)->ifc_no = ifc_no;
 
1172
      (*handle)->ep_bulk_out = ep_bulk_out;
 
1173
      (*handle)->ep_bulk_in = ep_bulk_in;
 
1174
      (*handle)->ep_intr = ep_intr;
 
1175
    }
 
1176
  else if (dev_fd != -1) /* Device transport. */
 
1177
    {
 
1178
      (*handle)->idev = NULL;
 
1179
      (*handle)->dev_fd = dev_fd;
 
1180
      (*handle)->id_vendor = 0;  /* Magic vendor for special transport. */
 
1181
      (*handle)->id_product = ifc_no; /* Transport type */
 
1182
      prepare_special_transport (*handle);
 
1183
    }
 
1184
  else
 
1185
    {
 
1186
      assert (!"no transport"); /* Bug. */
 
1187
    }
992
1188
 
993
1189
  DEBUGOUT_2 ("using CCID reader %d (ID=%s)\n",  readerno, rid );
994
1190
 
 
1191
  if (idev)
 
1192
    {
 
1193
      if (parse_ccid_descriptor (*handle, ifcdesc_extra, ifcdesc_extra_len))
 
1194
        {
 
1195
          DEBUGOUT ("device not supported\n");
 
1196
          rc = CCID_DRIVER_ERR_NO_READER;
 
1197
          goto leave;
 
1198
        }
 
1199
      
 
1200
      rc = usb_claim_interface (idev, ifc_no);
 
1201
      if (rc)
 
1202
        {
 
1203
          DEBUGOUT_1 ("usb_claim_interface failed: %d\n", rc);
 
1204
          rc = CCID_DRIVER_ERR_CARD_IO_ERROR;
 
1205
          goto leave;
 
1206
        }
 
1207
    }
995
1208
 
996
 
  if (parse_ccid_descriptor (*handle, ifcdesc_extra, ifcdesc_extra_len))
997
 
    {
998
 
      DEBUGOUT ("device not supported\n");
999
 
      rc = CCID_DRIVER_ERR_NO_READER;
1000
 
      goto leave;
1001
 
    }
1002
 
  
1003
 
  rc = usb_claim_interface (idev, ifc_no);
1004
 
  if (rc)
1005
 
    {
1006
 
      DEBUGOUT_1 ("usb_claim_interface failed: %d\n", rc);
1007
 
      rc = CCID_DRIVER_ERR_CARD_IO_ERROR;
1008
 
      goto leave;
1009
 
    }
1010
 
  
1011
1209
 leave:
1012
1210
  free (ifcdesc_extra);
1013
1211
  if (rc)
1015
1213
      free (rid);
1016
1214
      if (idev)
1017
1215
        usb_close (idev);
 
1216
      if (dev_fd != -1)
 
1217
        close (dev_fd);
1018
1218
      free (*handle);
1019
1219
      *handle = NULL;
1020
1220
    }
1054
1254
      usb_close (handle->idev);
1055
1255
      handle->idev = NULL;
1056
1256
    }
 
1257
  if (handle->dev_fd != -1)
 
1258
    {
 
1259
      close (handle->dev_fd);
 
1260
      handle->dev_fd = -1;
 
1261
    }
1057
1262
}
1058
1263
 
1059
1264
 
1080
1285
 
1081
1286
  do_close_reader (handle);
1082
1287
 
1083
 
  idev = scan_or_find_devices (-1, handle->rid, NULL, &dev,
1084
 
                               &ifcdesc_extra, &ifcdesc_extra_len,
1085
 
                               &ifc_no, &ep_bulk_out, &ep_bulk_in, &ep_intr);
1086
 
  if (!idev)
 
1288
  if (scan_or_find_devices (-1, handle->rid, NULL, &dev,
 
1289
                            &ifcdesc_extra, &ifcdesc_extra_len,
 
1290
                            &ifc_no, &ep_bulk_out, &ep_bulk_in, &ep_intr,
 
1291
                            &idev, NULL) || !idev)
1087
1292
    {
1088
1293
      DEBUGOUT_1 ("no CCID reader with ID %s\n", handle->rid);
1089
1294
      return CCID_DRIVER_ERR_NO_READER;
1090
1295
    }
1091
1296
 
1092
 
 
1093
 
  handle->idev = idev;
1094
 
  handle->ifc_no = ifc_no;
1095
 
  handle->ep_bulk_out = ep_bulk_out;
1096
 
  handle->ep_bulk_in = ep_bulk_in;
1097
 
  handle->ep_intr = ep_intr;
1098
 
 
1099
 
  if (parse_ccid_descriptor (handle, ifcdesc_extra, ifcdesc_extra_len))
1100
 
    {
1101
 
      DEBUGOUT ("device not supported\n");
1102
 
      rc = CCID_DRIVER_ERR_NO_READER;
1103
 
      goto leave;
1104
 
    }
1105
 
  
1106
 
  rc = usb_claim_interface (idev, ifc_no);
1107
 
  if (rc)
1108
 
    {
1109
 
      DEBUGOUT_1 ("usb_claim_interface failed: %d\n", rc);
1110
 
      rc = CCID_DRIVER_ERR_CARD_IO_ERROR;
1111
 
      goto leave;
1112
 
    }
1113
 
  
 
1297
  if (idev)
 
1298
    {
 
1299
      handle->idev = idev;
 
1300
      handle->ifc_no = ifc_no;
 
1301
      handle->ep_bulk_out = ep_bulk_out;
 
1302
      handle->ep_bulk_in = ep_bulk_in;
 
1303
      handle->ep_intr = ep_intr;
 
1304
 
 
1305
      if (parse_ccid_descriptor (handle, ifcdesc_extra, ifcdesc_extra_len))
 
1306
        {
 
1307
          DEBUGOUT ("device not supported\n");
 
1308
          rc = CCID_DRIVER_ERR_NO_READER;
 
1309
          goto leave;
 
1310
        }
 
1311
      
 
1312
      rc = usb_claim_interface (idev, ifc_no);
 
1313
      if (rc)
 
1314
        {
 
1315
          DEBUGOUT_1 ("usb_claim_interface failed: %d\n", rc);
 
1316
          rc = CCID_DRIVER_ERR_CARD_IO_ERROR;
 
1317
          goto leave;
 
1318
        }
 
1319
    }
 
1320
 
1114
1321
 leave:
1115
1322
  free (ifcdesc_extra);
1116
1323
  if (rc)
1117
1324
    {
1118
 
      usb_close (handle->idev);
 
1325
      if (handle->idev)
 
1326
        usb_close (handle->idev);
1119
1327
      handle->idev = NULL;
 
1328
      if (handle->dev_fd != -1)
 
1329
        close (handle->dev_fd);
 
1330
      handle->dev_fd = -1;
1120
1331
    }
1121
1332
 
1122
1333
  return rc;
1128
1339
int 
1129
1340
ccid_close_reader (ccid_driver_t handle)
1130
1341
{
1131
 
  if (!handle || !handle->idev)
 
1342
  if (!handle || (!handle->idev && handle->dev_fd == -1))
1132
1343
    return 0;
1133
1344
 
1134
1345
  do_close_reader (handle);
1147
1358
}
1148
1359
 
1149
1360
 
 
1361
/* Write NBYTES of BUF to file descriptor FD. */
 
1362
static int
 
1363
writen (int fd, const void *buf, size_t nbytes)
 
1364
{
 
1365
  size_t nleft = nbytes;
 
1366
  int nwritten;
 
1367
  
 
1368
  while (nleft > 0)
 
1369
    {
 
1370
      nwritten = write (fd, buf, nleft);
 
1371
      if (nwritten < 0)
 
1372
        {
 
1373
          if (errno == EINTR)
 
1374
            nwritten = 0;
 
1375
          else
 
1376
            return -1;
 
1377
        }
 
1378
      nleft -= nwritten;
 
1379
      buf = (const char*)buf + nwritten;
 
1380
    }
 
1381
    
 
1382
  return 0;
 
1383
}
 
1384
 
 
1385
 
1150
1386
/* Write a MSG of length MSGLEN to the designated bulk out endpoint.
1151
1387
   Returns 0 on success. */
1152
1388
static int
1154
1390
{
1155
1391
  int rc;
1156
1392
 
1157
 
  rc = usb_bulk_write (handle->idev, 
1158
 
                       handle->ep_bulk_out,
1159
 
                       (char*)msg, msglen,
1160
 
                       1000 /* ms timeout */);
1161
 
  if (rc == msglen)
1162
 
    return 0;
1163
 
 
1164
 
  if (rc == -1)
1165
 
    DEBUGOUT_1 ("usb_bulk_write error: %s\n", strerror (errno));
 
1393
  if (handle->idev)
 
1394
    {
 
1395
      rc = usb_bulk_write (handle->idev, 
 
1396
                           handle->ep_bulk_out,
 
1397
                           (char*)msg, msglen,
 
1398
                           1000 /* ms timeout */);
 
1399
      if (rc == msglen)
 
1400
        return 0;
 
1401
      if (rc == -1)
 
1402
        DEBUGOUT_1 ("usb_bulk_write error: %s\n", strerror (errno));
 
1403
      else
 
1404
        DEBUGOUT_1 ("usb_bulk_write failed: %d\n", rc);
 
1405
    }
1166
1406
  else
1167
 
    DEBUGOUT_1 ("usb_bulk_write failed: %d\n", rc);
 
1407
    {
 
1408
      rc = writen (handle->dev_fd, msg, msglen);
 
1409
      if (!rc)
 
1410
        return 0;
 
1411
      DEBUGOUT_2 ("writen to %d failed: %s\n",
 
1412
                  handle->dev_fd, strerror (errno));
 
1413
      
 
1414
    }
1168
1415
  return CCID_DRIVER_ERR_CARD_IO_ERROR;
1169
1416
}
1170
1417
 
1187
1434
     for USB IOCTLs. */
1188
1435
  memset (buffer, 0, length);
1189
1436
 retry:
1190
 
  rc = usb_bulk_read (handle->idev, 
1191
 
                      handle->ep_bulk_in,
1192
 
                      (char*)buffer, length,
1193
 
                      timeout);
1194
 
  if (rc < 0)
1195
 
    {
1196
 
      DEBUGOUT_1 ("usb_bulk_read error: %s\n", strerror (errno));
1197
 
      return CCID_DRIVER_ERR_CARD_IO_ERROR;
 
1437
  if (handle->idev)
 
1438
    {
 
1439
      rc = usb_bulk_read (handle->idev, 
 
1440
                          handle->ep_bulk_in,
 
1441
                          (char*)buffer, length,
 
1442
                          timeout);
 
1443
      if (rc < 0)
 
1444
        {
 
1445
          DEBUGOUT_1 ("usb_bulk_read error: %s\n", strerror (errno));
 
1446
          return CCID_DRIVER_ERR_CARD_IO_ERROR;
 
1447
        }
 
1448
      *nread = msglen = rc;
 
1449
    }
 
1450
  else
 
1451
    {
 
1452
      rc = read (handle->dev_fd, buffer, length);
 
1453
      if (rc < 0)
 
1454
        {
 
1455
          DEBUGOUT_2 ("read from %d failed: %s\n",
 
1456
                      handle->dev_fd, strerror (errno));
 
1457
          return CCID_DRIVER_ERR_CARD_IO_ERROR;
 
1458
        }
 
1459
      *nread = msglen = rc;
1198
1460
    }
1199
1461
 
1200
 
  *nread = msglen = rc;
1201
1462
 
1202
1463
  if (msglen < 10)
1203
1464
    {
1204
1465
      DEBUGOUT_1 ("bulk-in msg too short (%u)\n", (unsigned int)msglen);
1205
1466
      return CCID_DRIVER_ERR_INV_VALUE;
1206
1467
    }
1207
 
  if (buffer[0] != expected_type)
1208
 
    {
1209
 
      DEBUGOUT_1 ("unexpected bulk-in msg type (%02x)\n", buffer[0]);
1210
 
      return CCID_DRIVER_ERR_INV_VALUE;
1211
 
    }
1212
1468
  if (buffer[5] != 0)    
1213
1469
    {
1214
1470
      DEBUGOUT_1 ("unexpected bulk-in slot (%d)\n", buffer[5]);
1221
1477
      return CCID_DRIVER_ERR_INV_VALUE;
1222
1478
    }
1223
1479
 
 
1480
  /* We need to handle the time extension request before we check that
 
1481
     we go the expected message type.  This is in particular required
 
1482
     for the Cherry keyboard which sends a time extension request for
 
1483
     each key hit.  */
1224
1484
  if ( !(buffer[7] & 0x03) && (buffer[7] & 0xC0) == 0x80)
1225
1485
    { 
1226
1486
      /* Card present and active, time extension requested. */
1229
1489
      goto retry;
1230
1490
    }
1231
1491
 
 
1492
  if (buffer[0] != expected_type)
 
1493
    {
 
1494
      DEBUGOUT_1 ("unexpected bulk-in msg type (%02x)\n", buffer[0]);
 
1495
      return CCID_DRIVER_ERR_INV_VALUE;
 
1496
    }
 
1497
 
 
1498
 
1232
1499
  if (!no_debug)
1233
1500
    {
1234
1501
      DEBUGOUT_3 ("status: %02X  error: %02X  octet[9]: %02X\n"
1339
1606
  size_t msglen;
1340
1607
  int i, j;
1341
1608
 
1342
 
  rc = usb_bulk_read (handle->idev, 
1343
 
                      handle->ep_intr,
1344
 
                      (char*)msg, sizeof msg,
1345
 
                      0 /* ms timeout */ );
1346
 
  if (rc < 0 && errno == ETIMEDOUT)
 
1609
  if (handle->idev)
 
1610
    {
 
1611
      rc = usb_bulk_read (handle->idev, 
 
1612
                          handle->ep_intr,
 
1613
                          (char*)msg, sizeof msg,
 
1614
                          0 /* ms timeout */ );
 
1615
      if (rc < 0 && errno == ETIMEDOUT)
 
1616
        return 0;
 
1617
    }
 
1618
  else 
1347
1619
    return 0;
1348
1620
 
1349
1621
  if (rc < 0)
1449
1721
  unsigned int edc;
1450
1722
  int i;
1451
1723
  int tried_iso = 0;
 
1724
  int got_param;
1452
1725
 
1453
1726
  /* First check whether a card is available.  */
1454
1727
  rc = ccid_slot_status (handle, &statusbits);
1457
1730
  if (statusbits == 2)
1458
1731
    return CCID_DRIVER_ERR_NO_CARD;
1459
1732
 
 
1733
    
1460
1734
  /* For an inactive and also for an active card, issue the PowerOn
1461
1735
     command to get the ATR.  */
1462
1736
 again:
1505
1779
      *atrlen = n;
1506
1780
    }
1507
1781
 
 
1782
  got_param = 0;
 
1783
  msg[0] = PC_to_RDR_GetParameters;
 
1784
  msg[5] = 0; /* slot */
 
1785
  msg[6] = seqno = handle->seqno++;
 
1786
  msg[7] = 0; /* RFU */
 
1787
  msg[8] = 0; /* RFU */
 
1788
  msg[9] = 0; /* RFU */
 
1789
  set_msg_len (msg, 0);
 
1790
  msglen = 10;
 
1791
  rc = bulk_out (handle, msg, msglen);
 
1792
  if (!rc)
 
1793
    rc = bulk_in (handle, msg, sizeof msg, &msglen, RDR_to_PC_Parameters,
 
1794
                  seqno, 2000, 0);
 
1795
  if (rc)
 
1796
    DEBUGOUT ("GetParameters failed\n");
 
1797
  else
 
1798
    {
 
1799
      DEBUGOUT ("GetParametes returned");
 
1800
      for (i=0; i < msglen; i++)
 
1801
        DEBUGOUT_CONT_1 (" %02X", msg[i]);
 
1802
      DEBUGOUT_LF ();
 
1803
      if (msglen >= 10)
 
1804
        {
 
1805
          DEBUGOUT_1 ("  protocol ..........: T=%d\n", msg[9]);
 
1806
          if (msglen == 17 && msg[9] == 1)
 
1807
            {
 
1808
              DEBUGOUT_1 ("  bmFindexDindex ....: %02X\n", msg[10]);
 
1809
              DEBUGOUT_1 ("  bmTCCKST1 .........: %02X\n", msg[11]);
 
1810
              DEBUGOUT_1 ("  bGuardTimeT1 ......: %02X\n", msg[12]);
 
1811
              DEBUGOUT_1 ("  bmWaitingIntegersT1: %02X\n", msg[13]);
 
1812
              DEBUGOUT_1 ("  bClockStop ........: %02X\n", msg[14]);
 
1813
              DEBUGOUT_1 ("  bIFSC .............: %d\n", msg[15]);
 
1814
              DEBUGOUT_1 ("  bNadValue .........: %d\n", msg[16]);
 
1815
              got_param = 1;
 
1816
            }
 
1817
        }
 
1818
    }
 
1819
 
1508
1820
  /* Setup parameters to select T=1. */
1509
1821
  msg[0] = PC_to_RDR_SetParameters;
1510
1822
  msg[5] = 0; /* slot */
1513
1825
  msg[8] = 0; /* RFU */
1514
1826
  msg[9] = 0; /* RFU */
1515
1827
 
1516
 
  /* FIXME: Get those values from the ATR. */
1517
 
  msg[10]= 0x01; /* Fi/Di */
1518
 
  msg[11]= 0x10; /* LRC, direct convention. */
1519
 
  msg[12]= 0;    /* Extra guardtime. */
1520
 
  msg[13]= 0x41; /* BWI/CWI */
1521
 
  msg[14]= 0;    /* No clock stoppping. */
1522
 
  msg[15]= 254;  /* IFSC */
1523
 
  msg[16]= 0;    /* Does not support non default NAD values. */
 
1828
  if (!got_param)
 
1829
    {
 
1830
      /* FIXME: Get those values from the ATR. */
 
1831
      msg[10]= 0x01; /* Fi/Di */
 
1832
      msg[11]= 0x10; /* LRC, direct convention. */
 
1833
      msg[12]= 0;    /* Extra guardtime. */
 
1834
      msg[13]= 0x41; /* BWI/CWI */
 
1835
      msg[14]= 0;    /* No clock stoppping. */
 
1836
      msg[15]= 254;  /* IFSC */
 
1837
      msg[16]= 0;    /* Does not support non default NAD values. */
 
1838
    }
1524
1839
  set_msg_len (msg, 7);
1525
1840
  msglen = 10 + 7;
1526
1841
 
1532
1847
  rc = bulk_out (handle, msg, msglen);
1533
1848
  if (rc)
1534
1849
    return rc;
1535
 
  /* Note that we ignore the error code on purpose. */
1536
 
  bulk_in (handle, msg, sizeof msg, &msglen, RDR_to_PC_Parameters,
1537
 
           seqno, 5000, 0);
 
1850
  rc = bulk_in (handle, msg, sizeof msg, &msglen, RDR_to_PC_Parameters,
 
1851
                seqno, 5000, 0);
 
1852
  if (rc)
 
1853
    DEBUGOUT ("SetParameters failed (ignored)\n");
1538
1854
 
1539
1855
  handle->t1_ns = 0;
1540
1856
  handle->t1_nr = 0;
1541
1857
 
1542
 
  /* Send an S-Block with our maximun IFSD to the CCID.  */
1543
 
  if (!handle->auto_ifsd)
 
1858
  /* Send an S-Block with our maximum IFSD to the CCID.  */
 
1859
  if (!handle->apdu_level && !handle->auto_ifsd)
1544
1860
    {
1545
1861
      tpdu = msg+10;
1546
1862
      /* NAD: DAD=1, SAD=0 */
2027
2343
  int i;
2028
2344
  size_t dummy_nresp;
2029
2345
  int testmode;
 
2346
  int cherry_mode = 0;
2030
2347
 
2031
2348
  testmode = !resp && !nresp;
2032
2349
 
2058
2375
      || pinlen_min > pinlen_max)
2059
2376
    return CCID_DRIVER_ERR_INV_VALUE;
2060
2377
 
2061
 
  /* We have only tested this with an SCM reader so better don't risk
2062
 
     anything and do not allow the use with other readers. */
2063
 
  if (handle->id_vendor != VENDOR_SCM)
2064
 
    return CCID_DRIVER_ERR_NOT_SUPPORTED;
 
2378
  /* We have only tested a few readers so better don't risk anything
 
2379
     and do not allow the use with other readers. */
 
2380
  switch (handle->id_vendor)
 
2381
    {
 
2382
    case VENDOR_SCM:  /* Tested with SPR 532. */
 
2383
    case VENDOR_KAAN: /* Tested with KAAN Advanced (1.02). */
 
2384
      break;
 
2385
    case VENDOR_CHERRY:
 
2386
      /* The CHERRY XX44 keyboard echos an asterisk for each entered
 
2387
         character on the keyboard channel.  We use a special variant
 
2388
         of PC_to_RDR_Secure which directs these characters to the
 
2389
         smart card's bulk-in channel.  We also need to append a zero
 
2390
         Lc byte to the APDU.  It seems that it will be replaced with
 
2391
         the actual length instead of being appended before the APDU
 
2392
         is send to the card. */
 
2393
      cherry_mode = 1;
 
2394
      break;
 
2395
    default:
 
2396
     return CCID_DRIVER_ERR_NOT_SUPPORTED;
 
2397
    }
2065
2398
 
2066
2399
  if (testmode)
2067
2400
    return 0; /* Success */
2076
2409
        return rc;
2077
2410
    }
2078
2411
 
2079
 
  msg[0] = PC_to_RDR_Secure;
 
2412
  msg[0] = cherry_mode? 0x89 : PC_to_RDR_Secure;
2080
2413
  msg[5] = 0; /* slot */
2081
2414
  msg[6] = seqno = handle->seqno++;
2082
 
  msg[7] = 4; /* bBWI */
 
2415
  msg[7] = 0; /* bBWI */
2083
2416
  msg[8] = 0; /* RFU */
2084
2417
  msg[9] = 0; /* RFU */
2085
2418
  msg[10] = 0; /* Perform PIN verification. */
2088
2421
  if (handle->id_vendor == VENDOR_SCM)
2089
2422
    {
2090
2423
      /* For the SPR532 the next 2 bytes need to be zero.  We do this
2091
 
         for all SCM product. Kudos to Martin Paljak for this
 
2424
         for all SCM products.  Kudos to Martin Paljak for this
2092
2425
         hint.  */
2093
2426
      msg[13] = msg[14] = 0;
2094
2427
    }
2100
2433
      msg[14] = 0x00; /* bmPINLengthFormat:
2101
2434
                         Units are bytes, position is 0. */
2102
2435
    }
2103
 
  msg[15] = pinlen_min;   /* wPINMaxExtraDigit-Minimum.  */
2104
 
  msg[16] = pinlen_max;   /* wPINMaxExtraDigit-Maximum.  */
 
2436
 
 
2437
  /* The following is a little endian word. */
 
2438
  msg[15] = pinlen_max;   /* wPINMaxExtraDigit-Maximum.  */
 
2439
  msg[16] = pinlen_min;   /* wPINMaxExtraDigit-Minimum.  */
 
2440
 
2105
2441
  msg[17] = 0x02; /* bEntryValidationCondition:
2106
2442
                     Validation key pressed */
2107
2443
  if (pinlen_min && pinlen_max && pinlen_min == pinlen_max)
2113
2449
  /* bTeoProlog follows: */
2114
2450
  msg[22] = handle->nonnull_nad? ((1 << 4) | 0): 0;
2115
2451
  msg[23] = ((handle->t1_ns & 1) << 6); /* I-block */
2116
 
  msg[24] = 4; /* apdulen.  */
 
2452
  msg[24] = 0; /* The apdulen will be filled in by the reader.  */
2117
2453
  /* APDU follows:  */
2118
2454
  msg[25] = apdu_buf[0]; /* CLA */
2119
2455
  msg[26] = apdu_buf[1]; /* INS */
2120
2456
  msg[27] = apdu_buf[2]; /* P1 */
2121
2457
  msg[28] = apdu_buf[3]; /* P2 */
2122
2458
  msglen = 29;
 
2459
  if (cherry_mode)
 
2460
    msg[msglen++] = 0;
 
2461
  /* An EDC is not required. */
2123
2462
  set_msg_len (msg, msglen - 10);
2124
2463
 
2125
2464
  DEBUGOUT ("sending");
2133
2472
  
2134
2473
  msg = recv_buffer;
2135
2474
  rc = bulk_in (handle, msg, sizeof recv_buffer, &msglen,
2136
 
                RDR_to_PC_DataBlock, seqno, 5000, 0);
 
2475
                RDR_to_PC_DataBlock, seqno, 30000, 0);
2137
2476
  if (rc)
2138
2477
    return rc;
2139
2478
  
2140
2479
  tpdu = msg + 10;
2141
2480
  tpdulen = msglen - 10;
 
2481
 
 
2482
  if (handle->apdu_level)
 
2483
    {
 
2484
      if (resp)
 
2485
        {
 
2486
          if (tpdulen > maxresplen)
 
2487
            {
 
2488
              DEBUGOUT_2 ("provided buffer too short for received data "
 
2489
                          "(%u/%u)\n",
 
2490
                          (unsigned int)tpdulen, (unsigned int)maxresplen);
 
2491
              return CCID_DRIVER_ERR_INV_VALUE;
 
2492
            }
 
2493
          
 
2494
          memcpy (resp, tpdu, tpdulen); 
 
2495
          *nresp = tpdulen;
 
2496
        }
 
2497
      return 0;
 
2498
    }
2142
2499
  
2143
2500
  if (tpdulen < 4) 
2144
2501
    {
2284
2641
{
2285
2642
  int rc;
2286
2643
  ccid_driver_t ccid;
2287
 
  unsigned int slotstat;
 
2644
  int slotstat;
2288
2645
  unsigned char result[512];
2289
2646
  size_t resultlen;
2290
2647
  int no_pinpad = 0;
2312
2669
        }
2313
2670
      else if ( !strcmp (*argv, "--debug"))
2314
2671
        {
2315
 
          ccid_set_debug_level (1);
 
2672
          ccid_set_debug_level (ccid_set_debug_level (-1)+1);
2316
2673
          argc--; argv++;
2317
2674
        }
2318
2675
      else if ( !strcmp (*argv, "--no-poll"))