~ubuntu-branches/ubuntu/karmic/gnupg2/karmic-updates

« back to all changes in this revision

Viewing changes to scd/apdu.c

  • Committer: Bazaar Package Importer
  • Author(s): Matthias Urlichs
  • Date: 2005-12-08 22:13:21 UTC
  • mto: (5.1.1 edgy)
  • mto: This revision was merged to the branch mainline in revision 3.
  • Revision ID: james.westby@ubuntu.com-20051208221321-d54343ca8hlwzkac
Tags: upstream-1.9.19
ImportĀ upstreamĀ versionĀ 1.9.19

Show diffs side-by-side

added added

removed removed

Lines of Context:
15
15
 *
16
16
 * You should have received a copy of the GNU General Public License
17
17
 * along with this program; if not, write to the Free Software
18
 
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
 
18
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
 
19
 * USA.
19
20
 *
20
 
 * $Id: apdu.c,v 1.4.2.24 2004/10/20 08:54:45 wk Exp $
 
21
 * $Id: apdu.c 3882 2005-09-05 14:36:36Z wk $
21
22
 */
22
23
 
 
24
/* NOTE: This module is also used by other software, thus the use of
 
25
   the macro USE_GNU_PTH is mandatory.  For GnuPG this macro is
 
26
   guaranteed to be defined true. */
 
27
 
23
28
#include <config.h>
24
29
#include <errno.h>
25
30
#include <stdio.h>
32
37
# include <unistd.h>
33
38
# include <fcntl.h>
34
39
#endif
35
 
#ifdef HAVE_OPENSC
36
 
# include <opensc/opensc.h>
37
 
# ifdef USE_GNU_PTH
38
 
# undef USE_GNU_PTH
39
 
# endif
40
 
#endif
 
40
 
41
41
 
42
42
/* If requested include the definitions for the remote APDU protocol
43
43
   code. */
74
74
#endif
75
75
#endif
76
76
 
77
 
 
 
77
 
78
78
#define MAX_READER 4 /* Number of readers we support concurrently. */
79
79
 
80
80
 
119
119
    pid_t pid;
120
120
#endif /*NEED_PCSC_WRAPPER*/
121
121
  } pcsc;
122
 
#ifdef HAVE_OPENSC
123
 
  struct {
124
 
    struct sc_context *ctx;
125
 
    struct sc_card *scard;
126
 
  } osc;
127
 
#endif /*HAVE_OPENSC*/
128
122
#ifdef USE_G10CODE_RAPDU
129
123
  struct {
130
124
    rapdu_t handle;
133
127
  char *rdrname;     /* Name of the connected reader or NULL if unknown. */
134
128
  int last_status;
135
129
  int status;
 
130
  int is_t0;         /* True if we know that we are running T=0. */
136
131
  unsigned char atr[33];
137
132
  size_t atrlen;           /* A zero length indicates that the ATR has
138
133
                              not yet been read; i.e. the card is not
158
153
static char (* DLSTDCALL CT_close) (unsigned short ctn);
159
154
 
160
155
/* PC/SC constants and function pointer. */
161
 
#define PCSC_SCOPE_USER      0 
162
 
#define PCSC_SCOPE_TERMINAL  1 
163
 
#define PCSC_SCOPE_SYSTEM    2 
164
 
#define PCSC_SCOPE_GLOBAL    3 
 
156
#define PCSC_SCOPE_USER      0
 
157
#define PCSC_SCOPE_TERMINAL  1
 
158
#define PCSC_SCOPE_SYSTEM    2
 
159
#define PCSC_SCOPE_GLOBAL    3
165
160
 
166
 
#define PCSC_PROTOCOL_T0     1 
167
 
#define PCSC_PROTOCOL_T1     2 
168
 
#define PCSC_PROTOCOL_RAW    4 
 
161
#define PCSC_PROTOCOL_T0     1
 
162
#define PCSC_PROTOCOL_T1     2
 
163
#define PCSC_PROTOCOL_RAW    4
169
164
 
170
165
#define PCSC_SHARE_EXCLUSIVE 1
171
166
#define PCSC_SHARE_SHARED    2
176
171
#define PCSC_UNPOWER_CARD    2
177
172
#define PCSC_EJECT_CARD      3
178
173
 
179
 
#define PCSC_UNKNOWN    0x0001  
 
174
#define PCSC_UNKNOWN    0x0001
180
175
#define PCSC_ABSENT     0x0002  /* Card is absent.  */
181
176
#define PCSC_PRESENT    0x0004  /* Card is present.  */
182
177
#define PCSC_SWALLOWED  0x0008  /* Card is present and electrical connected. */
196
191
#define PCSC_STATE_INUSE       0x0100  /* Shared mode.  */
197
192
#define PCSC_STATE_MUTE        0x0200  /* Unresponsive card.  */
198
193
 
199
 
 
200
 
struct pcsc_io_request_s 
 
194
/* Some PC/SC error codes.  */
 
195
#define PCSC_E_CANCELLED               0x80100002
 
196
#define PCSC_E_CANT_DISPOSE            0x8010000E
 
197
#define PCSC_E_INSUFFICIENT_BUFFER     0x80100008
 
198
#define PCSC_E_INVALID_ATR             0x80100015
 
199
#define PCSC_E_INVALID_HANDLE          0x80100003
 
200
#define PCSC_E_INVALID_PARAMETER       0x80100004
 
201
#define PCSC_E_INVALID_TARGET          0x80100005
 
202
#define PCSC_E_INVALID_VALUE           0x80100011
 
203
#define PCSC_E_NO_MEMORY               0x80100006
 
204
#define PCSC_E_UNKNOWN_READER          0x80100009
 
205
#define PCSC_E_TIMEOUT                 0x8010000A
 
206
#define PCSC_E_SHARING_VIOLATION       0x8010000B
 
207
#define PCSC_E_NO_SMARTCARD            0x8010000C
 
208
#define PCSC_E_UNKNOWN_CARD            0x8010000D
 
209
#define PCSC_E_PROTO_MISMATCH          0x8010000F
 
210
#define PCSC_E_NOT_READY               0x80100010
 
211
#define PCSC_E_SYSTEM_CANCELLED        0x80100012
 
212
#define PCSC_E_NOT_TRANSACTED          0x80100016
 
213
#define PCSC_E_READER_UNAVAILABLE      0x80100017
 
214
#define PCSC_W_REMOVED_CARD            0x80100069
 
215
 
 
216
 
 
217
struct pcsc_io_request_s
201
218
{
202
 
  unsigned long protocol; 
 
219
  unsigned long protocol;
203
220
  unsigned long pci_len;
204
221
};
205
222
 
260
277
                                     unsigned long timeout);
261
278
 
262
279
 
 
280
/*  Prototypes.  */
 
281
static int pcsc_get_status (int slot, unsigned int *status);
 
282
 
263
283
 
264
284
 
265
 
/* 
 
285
/*
266
286
      Helper
267
287
 */
268
 
 
 
288
 
269
289
 
270
290
/* Find an unused reader slot for PORTSTR and put it into the reader
271
291
   table.  Return -1 on error or the index into the reader table. */
272
 
static int 
273
 
new_reader_slot (void)    
 
292
static int
 
293
new_reader_slot (void)
274
294
{
275
295
  int i, reader = -1;
276
296
 
304
324
 
305
325
  reader_table[reader].used = 1;
306
326
  reader_table[reader].last_status = 0;
 
327
  reader_table[reader].is_t0 = 1;
307
328
#ifdef NEED_PCSC_WRAPPER
308
329
  reader_table[reader].pcsc.req_fd = -1;
309
330
  reader_table[reader].pcsc.rsp_fd = -1;
387
408
 
388
409
 
389
410
 
390
 
/* 
391
 
       ct API Interface 
 
411
/*
 
412
       ct API Interface
392
413
 */
393
414
 
394
415
static const char *
425
446
  int rc;
426
447
  unsigned char dad[1], sad[1], cmd[11], buf[256];
427
448
  unsigned short buflen;
428
 
  
 
449
 
429
450
  /* Check whether card has been inserted. */
430
 
  dad[0] = 1;     /* Destination address: CT. */    
 
451
  dad[0] = 1;     /* Destination address: CT. */
431
452
  sad[0] = 2;     /* Source address: Host. */
432
453
 
433
454
  cmd[0] = 0x20;  /* Class byte. */
446
467
      return SW_HOST_CARD_IO_ERROR;
447
468
    }
448
469
 
449
 
  /* Connected, now activate the card. */           
450
 
  dad[0] = 1;    /* Destination address: CT. */    
 
470
  /* Connected, now activate the card. */
 
471
  dad[0] = 1;    /* Destination address: CT. */
451
472
  sad[0] = 2;    /* Source address: Host. */
452
473
 
453
474
  cmd[0] = 0x20;  /* Class byte. */
517
538
  int rc;
518
539
  unsigned char dad[1], sad[1];
519
540
  unsigned short ctbuflen;
520
 
  
 
541
 
521
542
  /* If we don't have an ATR, we need to reset the reader first. */
522
543
  if (!reader_table[slot].atrlen
523
544
      && (rc = reset_ct_reader (slot)))
524
545
    return rc;
525
546
 
526
 
  dad[0] = 0;     /* Destination address: Card. */    
 
547
  dad[0] = 0;     /* Destination address: Card. */
527
548
  sad[0] = 2;     /* Source address: Host. */
528
549
  ctbuflen = *buflen;
529
550
  if (DBG_CARD_IO)
582
603
}
583
604
 
584
605
 
 
606
/*
 
607
       PC/SC Interface
 
608
 */
 
609
 
585
610
#ifdef NEED_PCSC_WRAPPER
586
611
static int
587
612
writen (int fd, const void *buf, size_t nbytes)
624
649
#else
625
650
      n = read (fd, buf, nleft);
626
651
#endif
627
 
      if (n < 0 && errno == EINTR) 
 
652
      if (n < 0 && errno == EINTR)
628
653
        continue;
629
654
      if (n < 0)
630
655
        return -1; /* read error. */
637
662
    *nread = buflen - nleft;
638
663
 
639
664
/*   log_printhex ("  readn:", orig_buf, *nread); */
640
 
    
 
665
 
641
666
  return 0;
642
667
}
643
668
#endif /*NEED_PCSC_WRAPPER*/
656
681
    {
657
682
    case 0x0002: s = "cancelled"; break;
658
683
    case 0x000e: s = "can't dispose"; break;
659
 
    case 0x0008: s = "insufficient buffer"; break;   
 
684
    case 0x0008: s = "insufficient buffer"; break;
660
685
    case 0x0015: s = "invalid ATR"; break;
661
686
    case 0x0003: s = "invalid handle"; break;
662
 
    case 0x0004: s = "invalid parameter"; break; 
 
687
    case 0x0004: s = "invalid parameter"; break;
663
688
    case 0x0005: s = "invalid target"; break;
664
 
    case 0x0011: s = "invalid value"; break; 
665
 
    case 0x0006: s = "no memory"; break;  
666
 
    case 0x0013: s = "comm error"; break;      
667
 
    case 0x0001: s = "internal error"; break;     
668
 
    case 0x0014: s = "unknown error"; break; 
669
 
    case 0x0007: s = "waited too long"; break;  
 
689
    case 0x0011: s = "invalid value"; break;
 
690
    case 0x0006: s = "no memory"; break;
 
691
    case 0x0013: s = "comm error"; break;
 
692
    case 0x0001: s = "internal error"; break;
 
693
    case 0x0014: s = "unknown error"; break;
 
694
    case 0x0007: s = "waited too long"; break;
670
695
    case 0x0009: s = "unknown reader"; break;
671
 
    case 0x000a: s = "timeout"; break; 
672
 
    case 0x000b: s = "sharing violation"; break;       
 
696
    case 0x000a: s = "timeout"; break;
 
697
    case 0x000b: s = "sharing violation"; break;
673
698
    case 0x000c: s = "no smartcard"; break;
674
 
    case 0x000d: s = "unknown card"; break;   
675
 
    case 0x000f: s = "proto mismatch"; break;          
676
 
    case 0x0010: s = "not ready"; break;               
677
 
    case 0x0012: s = "system cancelled"; break;        
 
699
    case 0x000d: s = "unknown card"; break;
 
700
    case 0x000f: s = "proto mismatch"; break;
 
701
    case 0x0010: s = "not ready"; break;
 
702
    case 0x0012: s = "system cancelled"; break;
678
703
    case 0x0016: s = "not transacted"; break;
679
 
    case 0x0017: s = "reader unavailable"; break; 
680
 
    case 0x0065: s = "unsupported card"; break;        
681
 
    case 0x0066: s = "unresponsive card"; break;       
682
 
    case 0x0067: s = "unpowered card"; break;          
683
 
    case 0x0068: s = "reset card"; break;              
684
 
    case 0x0069: s = "removed card"; break;            
685
 
    case 0x006a: s = "inserted card"; break;           
686
 
    case 0x001f: s = "unsupported feature"; break;     
687
 
    case 0x0019: s = "PCI too small"; break;           
688
 
    case 0x001a: s = "reader unsupported"; break;      
689
 
    case 0x001b: s = "duplicate reader"; break;        
690
 
    case 0x001c: s = "card unsupported"; break;        
691
 
    case 0x001d: s = "no service"; break;              
692
 
    case 0x001e: s = "service stopped"; break;      
 
704
    case 0x0017: s = "reader unavailable"; break;
 
705
    case 0x0065: s = "unsupported card"; break;
 
706
    case 0x0066: s = "unresponsive card"; break;
 
707
    case 0x0067: s = "unpowered card"; break;
 
708
    case 0x0068: s = "reset card"; break;
 
709
    case 0x0069: s = "removed card"; break;
 
710
    case 0x006a: s = "inserted card"; break;
 
711
    case 0x001f: s = "unsupported feature"; break;
 
712
    case 0x0019: s = "PCI too small"; break;
 
713
    case 0x001a: s = "reader unsupported"; break;
 
714
    case 0x001b: s = "duplicate reader"; break;
 
715
    case 0x001c: s = "card unsupported"; break;
 
716
    case 0x001d: s = "no service"; break;
 
717
    case 0x001e: s = "service stopped"; break;
693
718
    default:     s = "unknown PC/SC error code"; break;
694
719
    }
695
720
  return s;
696
721
}
697
722
 
698
 
/* 
699
 
       PC/SC Interface
700
 
 */
 
723
/* Map PC/SC error codes to our special host status words.  */
 
724
static int
 
725
pcsc_error_to_sw (long ec)
 
726
{
 
727
  int rc;
 
728
 
 
729
  switch (ec)
 
730
    {
 
731
    case 0:  rc = 0; break;
 
732
 
 
733
    case PCSC_E_CANCELLED:           rc = SW_HOST_ABORTED; break;
 
734
    case PCSC_E_NO_MEMORY:           rc = SW_HOST_OUT_OF_CORE; break;
 
735
    case PCSC_E_TIMEOUT:             rc = SW_HOST_CARD_IO_ERROR; break;
 
736
    case PCSC_E_SHARING_VIOLATION:   rc = SW_HOST_LOCKING_FAILED; break;
 
737
    case PCSC_E_NO_SMARTCARD:        rc = SW_HOST_NO_CARD; break;
 
738
    case PCSC_W_REMOVED_CARD:        rc = SW_HOST_NO_CARD; break;
 
739
 
 
740
    case PCSC_E_INVALID_TARGET:
 
741
    case PCSC_E_INVALID_VALUE:
 
742
    case PCSC_E_INVALID_HANDLE: 
 
743
    case PCSC_E_INVALID_PARAMETER:
 
744
    case PCSC_E_INSUFFICIENT_BUFFER: rc = SW_HOST_INV_VALUE; break;
 
745
 
 
746
    default:  rc = SW_HOST_GENERAL_ERROR; break;
 
747
    }
 
748
 
 
749
  return rc;
 
750
}
701
751
 
702
752
static void
703
753
dump_pcsc_reader_status (int slot)
713
763
}
714
764
 
715
765
 
 
766
/* Send an PC/SC reset command and return a status word on error or 0
 
767
   on success. */
716
768
static int
717
769
reset_pcsc_reader (int slot)
718
770
{
722
774
  size_t len;
723
775
  int i, n;
724
776
  unsigned char msgbuf[9];
 
777
  unsigned int dummy_status;
 
778
  int sw = SW_HOST_CARD_IO_ERROR;
725
779
 
726
780
  slotp = reader_table + slot;
727
781
 
728
 
  if (slotp->pcsc.req_fd == -1 
729
 
      || slotp->pcsc.rsp_fd == -1 
 
782
  if (slotp->pcsc.req_fd == -1
 
783
      || slotp->pcsc.rsp_fd == -1
730
784
      || slotp->pcsc.pid == (pid_t)(-1) )
731
785
    {
732
786
      log_error ("pcsc_get_status: pcsc-wrapper not running\n");
733
 
      return SW_HOST_CARD_IO_ERROR;
 
787
      return sw;
734
788
    }
735
789
 
736
790
  msgbuf[0] = 0x05; /* RESET command. */
762
816
  len -= 4; /* Already read the error code. */
763
817
  if (len > DIM (slotp->atr))
764
818
    {
765
 
      log_error ("PC/SC returned a too large ATR (len=%x)\n", len);
 
819
      log_error ("PC/SC returned a too large ATR (len=%lx)\n",
 
820
                 (unsigned long)len);
 
821
      sw = SW_HOST_GENERAL_ERROR;
766
822
      goto command_failed;
767
823
    }
768
824
  err = (msgbuf[5] << 24) | (msgbuf[6] << 16) | (msgbuf[7] << 8 ) | msgbuf[8];
769
825
  if (err)
770
826
    {
771
 
      log_error ("PC/SC RESET failed: %s\n", pcsc_error_string (err));
 
827
      log_error ("PC/SC RESET failed: %s (0x%lx)\n",
 
828
                 pcsc_error_string (err), err);
 
829
      /* If the error code is no smart card, we should not considere
 
830
         this a major error and close the wrapper.  */
 
831
      sw = pcsc_error_to_sw (err);
 
832
      if (err == PCSC_E_NO_SMARTCARD)
 
833
        return sw;
772
834
      goto command_failed;
773
835
    }
774
836
 
775
 
  /* The open fucntion may return a zero for the ATR length to
 
837
  /* The open function may return a zero for the ATR length to
776
838
     indicate that no card is present.  */
777
839
  n = len;
778
840
  if (n)
786
848
    }
787
849
  slotp->atrlen = len;
788
850
 
 
851
  /* Read the status so that IS_T0 will be set. */
 
852
  pcsc_get_status (slot, &dummy_status);
 
853
 
789
854
  return 0;
790
855
 
791
856
 command_failed:
796
861
  kill (slotp->pcsc.pid, SIGTERM);
797
862
  slotp->pcsc.pid = (pid_t)(-1);
798
863
  slotp->used = 0;
799
 
  return -1;
 
864
  return sw;
800
865
 
801
866
#else /* !NEED_PCSC_WRAPPER */
802
867
  long err;
827
892
      log_error ("pcsc_connect failed: %s (0x%lx)\n",
828
893
                  pcsc_error_string (err), err);
829
894
      reader_table[slot].pcsc.card = 0;
830
 
      return SW_HOST_CARD_IO_ERROR;
831
 
    }      
832
 
 
833
 
  
 
895
      return pcsc_error_to_sw (err);
 
896
    }
 
897
 
 
898
 
834
899
  atrlen = 33;
835
900
  nreader = sizeof reader - 1;
836
901
  err = pcsc_status (reader_table[slot].pcsc.card,
842
907
      log_error ("pcsc_status failed: %s (0x%lx)\n",
843
908
                  pcsc_error_string (err), err);
844
909
      reader_table[slot].atrlen = 0;
845
 
      return SW_HOST_CARD_IO_ERROR;
 
910
      return pcsc_error_to_sw (err);
846
911
    }
847
912
  if (atrlen >= DIM (reader_table[0].atr))
848
913
    log_bug ("ATR returned by pcsc_status is too large\n");
849
914
  reader_table[slot].atrlen = atrlen;
 
915
  reader_table[slot].is_t0 = !!(card_protocol & PCSC_PROTOCOL_T0);
850
916
 
851
917
  return 0;
852
918
#endif /* !NEED_PCSC_WRAPPER */
862
928
  size_t len, full_len;
863
929
  int i, n;
864
930
  unsigned char msgbuf[9];
865
 
  unsigned char buffer[12];
 
931
  unsigned char buffer[16];
 
932
  int sw = SW_HOST_CARD_IO_ERROR;
866
933
 
867
934
  slotp = reader_table + slot;
868
935
 
869
 
  if (slotp->pcsc.req_fd == -1 
870
 
      || slotp->pcsc.rsp_fd == -1 
 
936
  if (slotp->pcsc.req_fd == -1
 
937
      || slotp->pcsc.rsp_fd == -1
871
938
      || slotp->pcsc.pid == (pid_t)(-1) )
872
939
    {
873
940
      log_error ("pcsc_get_status: pcsc-wrapper not running\n");
874
 
      return SW_HOST_CARD_IO_ERROR;
 
941
      return sw;
875
942
    }
876
943
 
877
944
  msgbuf[0] = 0x04; /* STATUS command. */
906
973
    {
907
974
      log_error ("pcsc_status failed: %s (0x%lx)\n",
908
975
                 pcsc_error_string (err), err);
909
 
      return SW_HOST_CARD_IO_ERROR;
 
976
      /* This is a proper error code, so return immediately.  */
 
977
      return pcsc_error_to_sw (err);
910
978
    }
911
979
 
912
980
  full_len = len;
913
 
  
914
 
  n = 8 < len ? 8 : len;
915
 
  if ((i=readn (slotp->pcsc.rsp_fd, buffer, n, &len)) || len != 8)
 
981
 
 
982
  /* The current version returns 3 words but we allow also for old
 
983
     versions returning only 2 words. */
 
984
  n = 12 < len ? 12 : len;
 
985
  if ((i=readn (slotp->pcsc.rsp_fd, buffer, n, &len))
 
986
      || (len != 8 && len != 12))
916
987
    {
917
988
      log_error ("error receiving PC/SC STATUS response: %s\n",
918
989
                 i? strerror (errno) : "premature EOF");
919
990
      goto command_failed;
920
991
    }
921
992
 
 
993
  slotp->is_t0 = (len == 12 && !!(buffer[11] & PCSC_PROTOCOL_T0));
 
994
 
 
995
 
922
996
  full_len -= len;
923
997
  /* Newer versions of the wrapper might send more status bytes.
924
998
     Read them. */
935
1009
        }
936
1010
      full_len -= n;
937
1011
    }
938
 
   
 
1012
 
939
1013
  /* We are lucky: The wrapper already returns the data in the
940
1014
     required format. */
941
1015
  *status = buffer[3];
950
1024
  kill (slotp->pcsc.pid, SIGTERM);
951
1025
  slotp->pcsc.pid = (pid_t)(-1);
952
1026
  slotp->used = 0;
953
 
  return -1;
 
1027
  return sw;
954
1028
 
955
1029
#else /*!NEED_PCSC_WRAPPER*/
956
1030
 
957
1031
  long err;
958
1032
  struct pcsc_readerstate_s rdrstates[1];
959
 
  
 
1033
 
960
1034
  memset (rdrstates, 0, sizeof *rdrstates);
961
1035
  rdrstates[0].reader = reader_table[slot].rdrname;
962
1036
  rdrstates[0].current_state = PCSC_STATE_UNAWARE;
963
1037
  err = pcsc_get_status_change (reader_table[slot].pcsc.context,
964
1038
                                0,
965
1039
                                rdrstates, 1);
966
 
  if (err == 0x8010000a) /* Timeout.  */
967
 
    err = 0;
 
1040
  if (err == PCSC_E_TIMEOUT)
 
1041
    err = 0; /* Timeout is no error error here. */
968
1042
  if (err)
969
1043
    {
970
1044
      log_error ("pcsc_get_status_change failed: %s (0x%lx)\n",
971
1045
                 pcsc_error_string (err), err);
972
 
      return SW_HOST_CARD_IO_ERROR;
 
1046
      return pcsc_error_to_sw (err);
973
1047
    }
974
1048
 
975
1049
 
997
1071
  if ( (*status & 6) == 6
998
1072
       && !(rdrstates[0].event_state & PCSC_STATE_INUSE) )
999
1073
    *status |= 1;
1000
 
  
1001
 
  return 0; 
 
1074
 
 
1075
  return 0;
1002
1076
#endif /*!NEED_PCSC_WRAPPER*/
1003
1077
}
1004
1078
 
1016
1090
  size_t len, full_len;
1017
1091
  int i, n;
1018
1092
  unsigned char msgbuf[9];
 
1093
  int sw = SW_HOST_CARD_IO_ERROR;
1019
1094
 
1020
1095
  if (!reader_table[slot].atrlen
1021
1096
      && (err = reset_pcsc_reader (slot)))
1026
1101
 
1027
1102
  slotp = reader_table + slot;
1028
1103
 
1029
 
  if (slotp->pcsc.req_fd == -1 
1030
 
      || slotp->pcsc.rsp_fd == -1 
 
1104
  if (slotp->pcsc.req_fd == -1
 
1105
      || slotp->pcsc.rsp_fd == -1
1031
1106
      || slotp->pcsc.pid == (pid_t)(-1) )
1032
1107
    {
1033
1108
      log_error ("pcsc_send_apdu: pcsc-wrapper not running\n");
1034
 
      return SW_HOST_CARD_IO_ERROR;
 
1109
      return sw;
1035
1110
    }
1036
1111
 
1037
1112
  msgbuf[0] = 0x03; /* TRANSMIT command. */
1067
1142
    {
1068
1143
      log_error ("pcsc_transmit failed: %s (0x%lx)\n",
1069
1144
                 pcsc_error_string (err), err);
1070
 
      return SW_HOST_CARD_IO_ERROR;
 
1145
      return pcsc_error_to_sw (err);
1071
1146
    }
1072
1147
 
1073
1148
   full_len = len;
1074
 
   
 
1149
 
1075
1150
   n = *buflen < len ? *buflen : len;
1076
1151
   if ((i=readn (slotp->pcsc.rsp_fd, buffer, n, &len)) || len != n)
1077
1152
     {
1113
1188
  kill (slotp->pcsc.pid, SIGTERM);
1114
1189
  slotp->pcsc.pid = (pid_t)(-1);
1115
1190
  slotp->used = 0;
1116
 
  return -1;
 
1191
  return sw;
1117
1192
 
1118
1193
#else /*!NEED_PCSC_WRAPPER*/
1119
1194
 
1120
1195
  long err;
1121
1196
  struct pcsc_io_request_s send_pci;
1122
1197
  unsigned long recv_len;
1123
 
  
 
1198
 
1124
1199
  if (!reader_table[slot].atrlen
1125
1200
      && (err = reset_pcsc_reader (slot)))
1126
1201
    return err;
1141
1216
  if (err)
1142
1217
    log_error ("pcsc_transmit failed: %s (0x%lx)\n",
1143
1218
               pcsc_error_string (err), err);
1144
 
  
1145
 
  return err? SW_HOST_CARD_IO_ERROR:0; 
 
1219
 
 
1220
  return pcsc_error_to_sw (err);
1146
1221
#endif /*!NEED_PCSC_WRAPPER*/
1147
1222
}
1148
1223
 
1159
1234
 
1160
1235
  slotp = reader_table + slot;
1161
1236
 
1162
 
  if (slotp->pcsc.req_fd == -1 
1163
 
      || slotp->pcsc.rsp_fd == -1 
 
1237
  if (slotp->pcsc.req_fd == -1
 
1238
      || slotp->pcsc.rsp_fd == -1
1164
1239
      || slotp->pcsc.pid == (pid_t)(-1) )
1165
1240
    {
1166
1241
      log_error ("close_pcsc_reader: pcsc-wrapper not running\n");
1198
1273
  if (err)
1199
1274
    log_error ("pcsc_close failed: %s (0x%lx)\n",
1200
1275
               pcsc_error_string (err), err);
1201
 
  
1202
 
  /* We will the wrapper in any case - errors are merely
 
1276
 
 
1277
  /* We will close the wrapper in any case - errors are merely
1203
1278
     informational. */
1204
 
  
 
1279
 
1205
1280
 command_failed:
1206
1281
  close (slotp->pcsc.req_fd);
1207
1282
  close (slotp->pcsc.rsp_fd);
1222
1297
#endif /*!NEED_PCSC_WRAPPER*/
1223
1298
}
1224
1299
 
 
1300
/* Note:  It is a pitty that we can't return proper error codes.  */
1225
1301
static int
1226
1302
open_pcsc_reader (const char *portstr)
1227
1303
{
1237
1313
  size_t len;
1238
1314
  unsigned char msgbuf[9];
1239
1315
  int err;
 
1316
  unsigned int dummy_status;
 
1317
  int sw = SW_HOST_CARD_IO_ERROR;
1240
1318
 
1241
1319
  slot = new_reader_slot ();
1242
1320
  if (slot == -1)
1246
1324
  /* Fire up the pcsc wrapper.  We don't use any fork/exec code from
1247
1325
     the common directy but implement it direclty so that this file
1248
1326
     may still be source copied. */
1249
 
  
 
1327
 
1250
1328
  if (pipe (rp) == -1)
1251
1329
    {
1252
1330
      log_error ("error creating a pipe: %s\n", strerror (errno));
1261
1339
      slotp->used = 0;
1262
1340
      return -1;
1263
1341
    }
1264
 
      
 
1342
 
1265
1343
  pid = fork ();
1266
1344
  if (pid == -1)
1267
1345
    {
1283
1361
      /* Double fork. */
1284
1362
      pid = fork ();
1285
1363
      if (pid == -1)
1286
 
        _exit (31); 
 
1364
        _exit (31);
1287
1365
      if (pid)
1288
1366
        _exit (0); /* Immediate exit this parent, so that the child
1289
1367
                      gets cleaned up by the init process. */
1293
1371
        log_fatal ("dup2 stdin failed: %s\n", strerror (errno));
1294
1372
      if (rp[1] != 1 && dup2 (rp[1], 1) == -1)
1295
1373
        log_fatal ("dup2 stdout failed: %s\n", strerror (errno));
1296
 
      
 
1374
 
1297
1375
      /* Send stderr to the bit bucket. */
1298
1376
      fd = open ("/dev/null", O_WRONLY);
1299
1377
      if (fd == -1)
1318
1396
      _exit (31);
1319
1397
    }
1320
1398
 
1321
 
  /* 
 
1399
  /*
1322
1400
     === Parent ===
1323
1401
   */
1324
1402
  close (wp[0]);
1328
1406
 
1329
1407
  /* Wait for the intermediate child to terminate. */
1330
1408
#ifdef USE_GNU_PTH
1331
 
#define WAIT pth_waitpid 
 
1409
#define WAIT pth_waitpid
1332
1410
#else
1333
 
#define WAIT waitpid 
 
1411
#define WAIT waitpid
1334
1412
#endif
1335
1413
  while ( (i=WAIT (pid, NULL, 0)) == -1 && errno == EINTR)
1336
1414
    ;
1366
1444
  len -= 4; /* Already read the error code. */
1367
1445
  if (len > DIM (slotp->atr))
1368
1446
    {
1369
 
      log_error ("PC/SC returned a too large ATR (len=%x)\n", len);
 
1447
      log_error ("PC/SC returned a too large ATR (len=%lx)\n",
 
1448
                 (unsigned long)len);
1370
1449
      goto command_failed;
1371
1450
    }
1372
1451
  err = (msgbuf[5] << 24) | (msgbuf[6] << 16) | (msgbuf[7] << 8 ) | msgbuf[8];
1373
1452
  if (err)
1374
1453
    {
1375
1454
      log_error ("PC/SC OPEN failed: %s\n", pcsc_error_string (err));
 
1455
      sw = pcsc_error_to_sw (err);
1376
1456
      goto command_failed;
1377
1457
    }
1378
1458
 
1379
1459
  slotp->last_status = 0;
1380
1460
 
1381
 
  /* The open fucntion may return a zero for the ATR length to
 
1461
  /* The open request may return a zero for the ATR length to
1382
1462
     indicate that no card is present.  */
1383
1463
  n = len;
1384
1464
  if (n)
1401
1481
  reader_table[slot].send_apdu_reader = pcsc_send_apdu;
1402
1482
  reader_table[slot].dump_status_reader = dump_pcsc_reader_status;
1403
1483
 
1404
 
  dump_reader_status (slot); 
 
1484
  /* Read the status so that IS_T0 will be set. */
 
1485
  pcsc_get_status (slot, &dummy_status);
 
1486
 
 
1487
  dump_reader_status (slot);
1405
1488
  return slot;
1406
1489
 
1407
1490
 command_failed:
1412
1495
  kill (slotp->pcsc.pid, SIGTERM);
1413
1496
  slotp->pcsc.pid = (pid_t)(-1);
1414
1497
  slotp->used = 0;
 
1498
  /* There is no way to return SW. */
1415
1499
  return -1;
 
1500
 
1416
1501
#else /*!NEED_PCSC_WRAPPER */
1417
1502
  long err;
1418
1503
  int slot;
1434
1519
      reader_table[slot].used = 0;
1435
1520
      return -1;
1436
1521
    }
1437
 
  
 
1522
 
1438
1523
  err = pcsc_list_readers (reader_table[slot].pcsc.context,
1439
1524
                           NULL, NULL, &nreader);
1440
1525
  if (!err)
1445
1530
          log_error ("error allocating memory for reader list\n");
1446
1531
          pcsc_release_context (reader_table[slot].pcsc.context);
1447
1532
          reader_table[slot].used = 0;
1448
 
          return -1;
 
1533
          return -1 /*SW_HOST_OUT_OF_CORE*/;
1449
1534
        }
1450
1535
      err = pcsc_list_readers (reader_table[slot].pcsc.context,
1451
1536
                               NULL, list, &nreader);
1457
1542
      pcsc_release_context (reader_table[slot].pcsc.context);
1458
1543
      reader_table[slot].used = 0;
1459
1544
      xfree (list);
1460
 
      return -1;
 
1545
      return -1 /*pcsc_error_to_sw (err)*/;
1461
1546
    }
1462
1547
 
1463
1548
  listlen = nreader;
1483
1568
      log_error ("error allocating memory for reader name\n");
1484
1569
      pcsc_release_context (reader_table[slot].pcsc.context);
1485
1570
      reader_table[slot].used = 0;
1486
 
      return -1;
 
1571
      return -1 /*SW_HOST_OUT_OF_CORE*/;
1487
1572
    }
1488
1573
  strcpy (reader_table[slot].rdrname, portstr? portstr : list);
1489
1574
  xfree (list);
1494
1579
                      PCSC_PROTOCOL_T0|PCSC_PROTOCOL_T1,
1495
1580
                      &reader_table[slot].pcsc.card,
1496
1581
                      &reader_table[slot].pcsc.protocol);
1497
 
  if (err == 0x8010000c) /* No smartcard.  */
 
1582
  if (err == PCSC_E_NO_SMARTCARD) 
1498
1583
    reader_table[slot].pcsc.card = 0;
1499
1584
  else if (err)
1500
1585
    {
1505
1590
      reader_table[slot].rdrname = NULL;
1506
1591
      reader_table[slot].used = 0;
1507
1592
      xfree (list);
1508
 
      return -1;
1509
 
    }      
 
1593
      return -1 /*pcsc_error_to_sw (err)*/;
 
1594
    }
1510
1595
 
1511
1596
  reader_table[slot].atrlen = 0;
1512
1597
  reader_table[slot].last_status = 0;
1532
1617
          /* If we got to here we know that a card is present
1533
1618
             and usable.  Thus remember this.  */
1534
1619
          reader_table[slot].last_status = (1|2|4| 0x8000);
 
1620
          reader_table[slot].is_t0 = !!(card_protocol & PCSC_PROTOCOL_T0);
1535
1621
        }
1536
1622
    }
1537
1623
 
1544
1630
/*   log_debug ("state    from pcsc_status: 0x%lx\n", card_state); */
1545
1631
/*   log_debug ("protocol from pcsc_status: 0x%lx\n", card_protocol); */
1546
1632
 
1547
 
  dump_reader_status (slot); 
 
1633
  dump_reader_status (slot);
1548
1634
  return slot;
1549
1635
#endif /*!NEED_PCSC_WRAPPER */
1550
1636
}
1553
1639
 
1554
1640
 
1555
1641
#ifdef HAVE_LIBUSB
1556
 
/* 
 
1642
/*
1557
1643
     Internal CCID driver interface.
1558
1644
 */
1559
1645
 
1570
1656
  ccid_close_reader (reader_table[slot].ccid.handle);
1571
1657
  reader_table[slot].used = 0;
1572
1658
  return 0;
1573
 
}                       
1574
 
  
 
1659
}
 
1660
 
1575
1661
 
1576
1662
static int
1577
1663
shutdown_ccid_reader (int slot)
1578
1664
{
1579
1665
  ccid_shutdown_reader (reader_table[slot].ccid.handle);
1580
1666
  return 0;
1581
 
}                       
1582
 
  
 
1667
}
 
1668
 
1583
1669
 
1584
1670
static int
1585
1671
reset_ccid_reader (int slot)
1596
1682
  assert (sizeof slotp->atr >= sizeof atr);
1597
1683
  slotp->atrlen = atrlen;
1598
1684
  memcpy (slotp->atr, atr, atrlen);
1599
 
  dump_reader_status (slot); 
 
1685
  dump_reader_status (slot);
1600
1686
  return 0;
1601
 
}                       
1602
 
  
 
1687
}
 
1688
 
1603
1689
 
1604
1690
static int
1605
1691
get_status_ccid (int slot, unsigned int *status)
1615
1701
    *status = 1|2|4;
1616
1702
  else if (bits == 1)
1617
1703
    *status = 2;
1618
 
  else 
 
1704
  else
1619
1705
    *status = 0;
1620
1706
 
1621
1707
  return 0;
1647
1733
  if (err)
1648
1734
    log_error ("ccid_transceive failed: (0x%lx)\n",
1649
1735
               err);
1650
 
  
1651
 
  return err; 
 
1736
 
 
1737
  return err;
1652
1738
}
1653
1739
 
1654
1740
/* Open the reader and try to read an ATR.  */
1692
1778
  reader_table[slot].send_apdu_reader = send_apdu_ccid;
1693
1779
  reader_table[slot].dump_status_reader = dump_ccid_reader_status;
1694
1780
 
1695
 
  dump_reader_status (slot); 
 
1781
  dump_reader_status (slot);
1696
1782
  return slot;
1697
1783
}
1698
1784
 
1702
1788
 
1703
1789
 
1704
1790
 
1705
 
#ifdef HAVE_OPENSC
1706
 
/* 
1707
 
     OpenSC Interface.
1708
 
 
1709
 
     This uses the OpenSC primitives to send APDUs.  We need this
1710
 
     because we can't mix OpenSC and native (i.e. ctAPI or PC/SC)
1711
 
     access to a card for resource conflict reasons.
1712
 
 */
1713
 
 
1714
 
 
1715
 
static int
1716
 
close_osc_reader (int slot)
1717
 
{
1718
 
  /* FIXME: Implement. */
1719
 
  reader_table[slot].used = 0;
1720
 
  return 0;
1721
 
}
1722
 
 
1723
 
static int
1724
 
reset_osc_reader (int slot)
1725
 
{
1726
 
  return SW_HOST_NOT_SUPPORTED;
1727
 
}
1728
 
 
1729
 
 
1730
 
static int
1731
 
osc_get_status (int slot, unsigned int *status)
1732
 
{
1733
 
  return SW_HOST_NOT_SUPPORTED;
1734
 
}
1735
 
 
1736
 
 
1737
 
/* Actually send the APDU of length APDULEN to SLOT and return a
1738
 
   maximum of *BUFLEN data in BUFFER, the actual returned size will be
1739
 
   set to BUFLEN.  Returns: OpenSC error code. */
1740
 
static int
1741
 
osc_send_apdu (int slot, unsigned char *apdu, size_t apdulen,
1742
 
                unsigned char *buffer, size_t *buflen)
1743
 
{
1744
 
  long err;
1745
 
  struct sc_apdu a;
1746
 
  unsigned char data[SC_MAX_APDU_BUFFER_SIZE];
1747
 
  unsigned char result[SC_MAX_APDU_BUFFER_SIZE];
1748
 
 
1749
 
  if (DBG_CARD_IO)
1750
 
    log_printhex ("  APDU_data:", apdu, apdulen);
1751
 
 
1752
 
  if (apdulen < 4)
1753
 
    {
1754
 
      log_error ("osc_send_apdu: APDU is too short\n");
1755
 
      return SW_HOST_INV_VALUE;
1756
 
    }
1757
 
 
1758
 
  memset(&a, 0, sizeof a);
1759
 
  a.cla = *apdu++;
1760
 
  a.ins = *apdu++;
1761
 
  a.p1 = *apdu++;
1762
 
  a.p2 = *apdu++;
1763
 
  apdulen -= 4;
1764
 
 
1765
 
  if (!apdulen)
1766
 
    a.cse = SC_APDU_CASE_1;
1767
 
  else if (apdulen == 1) 
1768
 
    {
1769
 
      a.le = *apdu? *apdu : 256;
1770
 
      apdu++; apdulen--;
1771
 
      a.cse = SC_APDU_CASE_2_SHORT;
1772
 
    }
1773
 
  else
1774
 
    {
1775
 
      a.lc = *apdu++; apdulen--;
1776
 
      if (apdulen < a.lc)
1777
 
        {
1778
 
          log_error ("osc_send_apdu: APDU shorter than specified in Lc\n");
1779
 
          return SW_HOST_INV_VALUE;
1780
 
 
1781
 
        }
1782
 
      memcpy(data, apdu, a.lc);
1783
 
      apdu += a.lc; apdulen -= a.lc;
1784
 
 
1785
 
      a.data = data;
1786
 
      a.datalen = a.lc;
1787
 
      
1788
 
      if (!apdulen)
1789
 
        a.cse = SC_APDU_CASE_3_SHORT;
1790
 
      else
1791
 
        {
1792
 
          a.le = *apdu? *apdu : 256;
1793
 
          apdu++; apdulen--;
1794
 
          if (apdulen)
1795
 
            {
1796
 
              log_error ("osc_send_apdu: APDU larger than specified\n");
1797
 
              return SW_HOST_INV_VALUE;
1798
 
            }
1799
 
          a.cse = SC_APDU_CASE_4_SHORT;
1800
 
        }
1801
 
    }
1802
 
 
1803
 
  a.resp = result;
1804
 
  a.resplen = DIM(result);
1805
 
 
1806
 
  err = sc_transmit_apdu (reader_table[slot].osc.scard, &a);
1807
 
  if (err)
1808
 
    {
1809
 
      log_error ("sc_apdu_transmit failed: %s\n", sc_strerror (err));
1810
 
      return SW_HOST_CARD_IO_ERROR;
1811
 
    }
1812
 
 
1813
 
  if (*buflen < 2 || a.resplen > *buflen - 2)
1814
 
    {
1815
 
      log_error ("osc_send_apdu: provided buffer too short to store result\n");
1816
 
      return SW_HOST_INV_VALUE;
1817
 
    }
1818
 
  memcpy (buffer, a.resp, a.resplen);
1819
 
  buffer[a.resplen] = a.sw1;
1820
 
  buffer[a.resplen+1] = a.sw2;
1821
 
  *buflen = a.resplen + 2;
1822
 
  return 0;
1823
 
}
1824
 
 
1825
 
static int
1826
 
open_osc_reader (int portno)
1827
 
{
1828
 
  int err;
1829
 
  int slot;
1830
 
  reader_table_t slotp;
1831
 
 
1832
 
  slot = new_reader_slot ();
1833
 
  if (slot == -1)
1834
 
    return -1;
1835
 
  slotp = reader_table + slot;
1836
 
 
1837
 
  err = sc_establish_context (&slotp->osc.ctx, "scdaemon");
1838
 
  if (err)
1839
 
    {
1840
 
      log_error ("failed to establish SC context: %s\n", sc_strerror (err));
1841
 
      slotp->used = 0;
1842
 
      return -1;
1843
 
    }
1844
 
  if (portno < 0 || portno >= slotp->osc.ctx->reader_count)
1845
 
    {
1846
 
      log_error ("no card reader available\n");
1847
 
      sc_release_context (slotp->osc.ctx);
1848
 
      slotp->used = 0;
1849
 
      return -1;
1850
 
    }
1851
 
 
1852
 
  /* Redirect to our logging facility. */
1853
 
  slotp->osc.ctx->error_file = log_get_stream ();
1854
 
  slotp->osc.ctx->debug = opt.debug_sc;
1855
 
  slotp->osc.ctx->debug_file = log_get_stream ();
1856
 
 
1857
 
  if (sc_detect_card_presence (slotp->osc.ctx->reader[portno], 0) != 1)
1858
 
    {
1859
 
      log_error ("no card present\n");
1860
 
      sc_release_context (slotp->osc.ctx);
1861
 
      slotp->used = 0;
1862
 
      return -1;
1863
 
    }
1864
 
  
1865
 
  /* We want the standard ISO driver. */
1866
 
  /*FIXME: OpenSC does not like "iso7816", so we use EMV for now. */
1867
 
  err = sc_set_card_driver(slotp->osc.ctx, "emv");
1868
 
  if (err)
1869
 
    {
1870
 
      log_error ("failed to select the iso7816 driver: %s\n",
1871
 
                 sc_strerror (err));
1872
 
      sc_release_context (slotp->osc.ctx);
1873
 
      slotp->used = 0;
1874
 
      return -1;
1875
 
    }
1876
 
 
1877
 
  /* Now connect the card and hope that OpenSC won't try to be too
1878
 
     smart. */
1879
 
  err = sc_connect_card (slotp->osc.ctx->reader[portno], 0,
1880
 
                         &slotp->osc.scard);
1881
 
  if (err)
1882
 
    {
1883
 
      log_error ("failed to connect card in reader %d: %s\n",
1884
 
                 portno, sc_strerror (err));
1885
 
      sc_release_context (slotp->osc.ctx);
1886
 
      slotp->used = 0;
1887
 
      return -1;
1888
 
    }
1889
 
  if (opt.verbose)
1890
 
    log_info ("connected to card in opensc reader %d using driver `%s'\n",
1891
 
              portno, slotp->osc.scard->driver->name);
1892
 
 
1893
 
  err = sc_lock (slotp->osc.scard);
1894
 
  if (err)
1895
 
    {
1896
 
      log_error ("can't lock card in reader %d: %s\n",
1897
 
                 portno, sc_strerror (err));
1898
 
      sc_disconnect_card (slotp->osc.scard, 0);
1899
 
      sc_release_context (slotp->osc.ctx);
1900
 
      slotp->used = 0;
1901
 
      return -1;
1902
 
    }
1903
 
 
1904
 
  if (slotp->osc.scard->atr_len >= DIM (slotp->atr))
1905
 
    log_bug ("ATR returned by opensc is too large\n");
1906
 
  slotp->atrlen = slotp->osc.scard->atr_len;
1907
 
  memcpy (slotp->atr, slotp->osc.scard->atr, slotp->atrlen);
1908
 
 
1909
 
  reader_table[slot].close_reader = close_osc_reader;
1910
 
  reader_table[slot].reset_reader = reset_osc_reader;
1911
 
  reader_table[slot].get_status_reader = osc_get_status;
1912
 
  reader_table[slot].send_apdu_reader = osc_send_apdu;
1913
 
  reader_table[slot].dump_status_reader = NULL;
1914
 
 
1915
 
  dump_reader_status (slot); 
1916
 
  return slot;
1917
 
}
1918
 
 
1919
 
#endif /* HAVE_OPENSC */
1920
 
 
1921
 
 
1922
 
 
1923
1791
#ifdef USE_G10CODE_RAPDU
1924
 
/* 
 
1792
/*
1925
1793
     The Remote APDU Interface.
1926
1794
 
1927
1795
     This uses the Remote APDU protocol to contact a reader.
1940
1808
    {
1941
1809
    case RAPDU_STATUS_SUCCESS:  rc = 0; break;
1942
1810
 
1943
 
    case RAPDU_STATUS_INVCMD:  
1944
 
    case RAPDU_STATUS_INVPROT:  
1945
 
    case RAPDU_STATUS_INVSEQ:  
 
1811
    case RAPDU_STATUS_INVCMD:
 
1812
    case RAPDU_STATUS_INVPROT:
 
1813
    case RAPDU_STATUS_INVSEQ:
1946
1814
    case RAPDU_STATUS_INVCOOKIE:
1947
1815
    case RAPDU_STATUS_INVREADER:  rc = SW_HOST_INV_VALUE;  break;
1948
1816
 
2007
1875
    {
2008
1876
      log_error ("ATR returned by the RAPDU layer is too large\n");
2009
1877
      rapdu_msg_release (msg);
2010
 
      return SW_HOST_INV_VALUE; 
 
1878
      return SW_HOST_INV_VALUE;
2011
1879
    }
2012
1880
  slotp->atrlen = msg->datalen;
2013
1881
  memcpy (slotp->atr, msg->data, msg->datalen);
2061
1929
 
2062
1930
/* Actually send the APDU of length APDULEN to SLOT and return a
2063
1931
   maximum of *BUFLEN data in BUFFER, the actual returned size will be
2064
 
   set to BUFLEN.  Returns: OpenSC error code. */
 
1932
   set to BUFLEN.  Returns: APDU error code. */
2065
1933
static int
2066
1934
my_rapdu_send_apdu (int slot, unsigned char *apdu, size_t apdulen,
2067
1935
                    unsigned char *buffer, size_t *buflen)
2107
1975
      rapdu_msg_release (msg);
2108
1976
      return sw;
2109
1977
    }
2110
 
  
 
1978
 
2111
1979
  if (msg->datalen > maxlen)
2112
1980
    {
2113
1981
      log_error ("rapdu response apdu too large\n");
2114
1982
      rapdu_msg_release (msg);
2115
 
      return SW_HOST_INV_VALUE; 
 
1983
      return SW_HOST_INV_VALUE;
2116
1984
    }
2117
1985
 
2118
1986
  *buflen = msg->datalen;
2151
2019
      return -1;
2152
2020
    }
2153
2021
 
 
2022
  rapdu_set_reader (slotp->rapdu.handle, portno);
2154
2023
 
2155
2024
  rapdu_set_iofunc (slotp->rapdu.handle,
2156
2025
                    readfnc, readfnc_value,
2196
2065
  reader_table[slot].send_apdu_reader = my_rapdu_send_apdu;
2197
2066
  reader_table[slot].dump_status_reader = NULL;
2198
2067
 
2199
 
  dump_reader_status (slot); 
 
2068
  dump_reader_status (slot);
2200
2069
  rapdu_msg_release (msg);
2201
2070
  return slot;
2202
2071
 
2203
 
 failure:      
 
2072
 failure:
2204
2073
  rapdu_msg_release (msg);
2205
2074
  rapdu_release (slotp->rapdu.handle);
2206
2075
  slotp->used = 0;
2211
2080
 
2212
2081
 
2213
2082
 
2214
 
/* 
 
2083
/*
2215
2084
       Driver Access
2216
2085
 */
2217
2086
 
2256
2125
 
2257
2126
/* Open the reader and return an internal slot number or -1 on
2258
2127
   error. If PORTSTR is NULL we default to a suitable port (for ctAPI:
2259
 
   the first USB reader.  For PC/SC the first listed reader).  If
2260
 
   OpenSC support is compiled in, we first try to use OpenSC. */
 
2128
   the first USB reader.  For PC/SC the first listed reader). */
2261
2129
int
2262
2130
apdu_open_reader (const char *portstr)
2263
2131
{
2283
2151
 
2284
2152
#endif /* HAVE_LIBUSB */
2285
2153
 
2286
 
#ifdef HAVE_OPENSC
2287
 
  if (!opt.disable_opensc)
2288
 
    {
2289
 
      int port = portstr? atoi (portstr) : 0;
2290
 
 
2291
 
      return open_osc_reader (port);
2292
 
    }
2293
 
#endif /* HAVE_OPENSC */  
2294
 
 
2295
 
 
2296
2154
  if (opt.ctapi_driver && *opt.ctapi_driver)
2297
2155
    {
2298
2156
      int port = portstr? atoi (portstr) : 32768;
2300
2158
      if (!ct_api_loaded)
2301
2159
        {
2302
2160
          void *handle;
2303
 
          
 
2161
 
2304
2162
          handle = dlopen (opt.ctapi_driver, RTLD_LAZY);
2305
2163
          if (!handle)
2306
2164
            {
2322
2180
      return open_ct_reader (port);
2323
2181
    }
2324
2182
 
2325
 
  
 
2183
 
2326
2184
  /* No ctAPI configured, so lets try the PC/SC API */
2327
2185
  if (!pcsc_api_loaded)
2328
2186
    {
2371
2229
      pcsc_set_timeout       = dlsym (handle, "SCardSetTimeout");
2372
2230
 
2373
2231
      if (!pcsc_establish_context
2374
 
          || !pcsc_release_context  
2375
 
          || !pcsc_list_readers     
 
2232
          || !pcsc_release_context
 
2233
          || !pcsc_list_readers
2376
2234
          || !pcsc_get_status_change
2377
 
          || !pcsc_connect          
 
2235
          || !pcsc_connect
2378
2236
          || !pcsc_reconnect
2379
2237
          || !pcsc_disconnect
2380
2238
          || !pcsc_status
2381
2239
          || !pcsc_begin_transaction
2382
2240
          || !pcsc_end_transaction
2383
 
          || !pcsc_transmit         
 
2241
          || !pcsc_transmit
2384
2242
          /* || !pcsc_set_timeout */)
2385
2243
        {
2386
2244
          /* Note that set_timeout is currently not used and also not
2388
2246
          log_error ("apdu_open_reader: invalid PC/SC driver "
2389
2247
                     "(%d%d%d%d%d%d%d%d%d%d%d%d)\n",
2390
2248
                     !!pcsc_establish_context,
2391
 
                     !!pcsc_release_context,  
2392
 
                     !!pcsc_list_readers,     
2393
 
                     !!pcsc_get_status_change,     
2394
 
                     !!pcsc_connect,          
2395
 
                     !!pcsc_reconnect,          
 
2249
                     !!pcsc_release_context,
 
2250
                     !!pcsc_list_readers,
 
2251
                     !!pcsc_get_status_change,
 
2252
                     !!pcsc_connect,
 
2253
                     !!pcsc_reconnect,
2396
2254
                     !!pcsc_disconnect,
2397
2255
                     !!pcsc_status,
2398
2256
                     !!pcsc_begin_transaction,
2399
2257
                     !!pcsc_end_transaction,
2400
 
                     !!pcsc_transmit,         
 
2258
                     !!pcsc_transmit,
2401
2259
                     !!pcsc_set_timeout );
2402
2260
          dlclose (handle);
2403
2261
          return -1;
2404
2262
        }
2405
 
#endif /*!NEED_PCSC_WRAPPER*/  
 
2263
#endif /*!NEED_PCSC_WRAPPER*/
2406
2264
      pcsc_api_loaded = 1;
2407
2265
    }
2408
2266
 
2416
2274
   only be called once and the slot will not be valid afther this.
2417
2275
 
2418
2276
   If PORTSTR is NULL we default to the first availabe port.
2419
 
*/  
 
2277
*/
2420
2278
int
2421
2279
apdu_open_remote_reader (const char *portstr,
2422
2280
                         const unsigned char *cookie, size_t length,
2436
2294
                            writefnc, writefnc_value,
2437
2295
                            closefnc, closefnc_value);
2438
2296
#else
2439
 
#ifdef _WIN32 
 
2297
#ifdef _WIN32
2440
2298
  errno = ENOENT;
2441
2299
#else
2442
2300
  errno = ENOSYS;
2489
2347
 
2490
2348
  if (slot < 0 || slot >= MAX_READER || !reader_table[slot].used )
2491
2349
    return SW_HOST_NO_DRIVER;
2492
 
  
 
2350
 
2493
2351
  if ((sw = lock_slot (slot)))
2494
2352
    return sw;
2495
2353
 
2521
2379
 
2522
2380
  if (slot < 0 || slot >= MAX_READER || !reader_table[slot].used )
2523
2381
    return SW_HOST_NO_DRIVER;
2524
 
  
 
2382
 
2525
2383
  if ((sw = trylock_slot (slot)))
2526
2384
    return sw;
2527
2385
 
2550
2408
            }
2551
2409
        }
2552
2410
    }
2553
 
  
 
2411
 
2554
2412
  unlock_slot (slot);
2555
2413
  return sw;
2556
2414
}
2557
2415
 
2558
 
  
 
2416
 
2559
2417
 
2560
2418
unsigned char *
2561
2419
apdu_get_atr (int slot, size_t *atrlen)
2562
2420
{
2563
 
  char *buf;
 
2421
  unsigned char *buf;
2564
2422
 
2565
2423
  if (slot < 0 || slot >= MAX_READER || !reader_table[slot].used )
2566
2424
    return NULL;
2567
 
  
 
2425
 
2568
2426
  buf = xtrymalloc (reader_table[slot].atrlen);
2569
2427
  if (!buf)
2570
2428
    return NULL;
2574
2432
}
2575
2433
 
2576
2434
 
2577
 
    
 
2435
 
2578
2436
/* Retrieve the status for SLOT. The function does only wait for the
2579
2437
   card to become available if HANG is set to true. On success the
2580
2438
   bits in STATUS will be set to
2660
2518
   returned data.  The length of that data will be put into
2661
2519
   *RETBUFLEN.  The caller is reponsible for releasing the buffer even
2662
2520
   in case of errors.  */
2663
 
int 
 
2521
int
2664
2522
apdu_send_le(int slot, int class, int ins, int p0, int p1,
2665
2523
             int lc, const char *data, int le,
2666
2524
             unsigned char **retbuf, size_t *retbuflen)
2682
2540
               class, ins, p0, p1, lc, le);
2683
2541
 
2684
2542
  if (lc != -1 && (lc > 255 || lc < 0))
2685
 
    return SW_WRONG_LENGTH; 
2686
 
  if (le != -1 && (le > 256 || le < 1))
2687
 
    return SW_WRONG_LENGTH; 
 
2543
    return SW_WRONG_LENGTH;
 
2544
  if (le != -1 && (le > 256 || le < 0))
 
2545
    return SW_WRONG_LENGTH;
2688
2546
  if ((!data && lc != -1) || (data && lc == -1))
2689
2547
    return SW_HOST_INV_VALUE;
2690
2548
 
2701
2559
      apdu[apdulen++] = lc;
2702
2560
      memcpy (apdu+apdulen, data, lc);
2703
2561
      apdulen += lc;
 
2562
      /* T=0 does not allow the use of Lc together with Le; thus
 
2563
         disable Le in this case. */
 
2564
      if (reader_table[slot].is_t0)
 
2565
        le = -1;
2704
2566
    }
2705
2567
  if (le != -1)
2706
 
    apdu[apdulen++] = le; /* Truncation is okay becuase 0 means 256. */
 
2568
    apdu[apdulen++] = le; /* Truncation is okay because 0 means 256. */
2707
2569
  assert (sizeof (apdu) >= apdulen);
2708
2570
  /* As safeguard don't pass any garbage from the stack to the driver. */
2709
2571
  memset (apdu+apdulen, 0, sizeof (apdu) - apdulen);
2721
2583
  resultlen -= 2;
2722
2584
  if (DBG_CARD_IO)
2723
2585
    {
2724
 
      log_debug (" response: sw=%04X  datalen=%d\n", sw, resultlen);
 
2586
      log_debug (" response: sw=%04X  datalen=%d\n",
 
2587
                 sw, (unsigned int)resultlen);
2725
2588
      if ( !retbuf && (sw == SW_SUCCESS || (sw & 0xff00) == SW_MORE_DATA))
2726
2589
        log_printhex ("     dump: ", result, resultlen);
2727
2590
    }
2763
2626
      do
2764
2627
        {
2765
2628
          int len = (sw & 0x00ff);
2766
 
          
 
2629
 
2767
2630
          if (DBG_CARD_IO)
2768
2631
            log_debug ("apdu_send_simple(%d): %d more bytes available\n",
2769
2632
                       slot, len);
2772
2635
          apdu[apdulen++] = 0xC0;
2773
2636
          apdu[apdulen++] = 0;
2774
2637
          apdu[apdulen++] = 0;
2775
 
          apdu[apdulen++] = len; 
 
2638
          apdu[apdulen++] = len;
2776
2639
          memset (apdu+apdulen, 0, sizeof (apdu) - apdulen);
2777
2640
          resultlen = RESULTLEN;
2778
2641
          rc = send_apdu (slot, apdu, apdulen, result, &resultlen);
2787
2650
          resultlen -= 2;
2788
2651
          if (DBG_CARD_IO)
2789
2652
            {
2790
 
              log_debug ("     more: sw=%04X  datalen=%d\n", sw, resultlen);
 
2653
              log_debug ("     more: sw=%04X  datalen=%d\n",
 
2654
                         sw, (unsigned int)resultlen);
2791
2655
              if (!retbuf && (sw==SW_SUCCESS || (sw&0xff00)==SW_MORE_DATA))
2792
2656
                log_printhex ("     dump: ", result, resultlen);
2793
2657
            }
2820
2684
                      slot, sw);
2821
2685
        }
2822
2686
      while ((sw & 0xff00) == SW_MORE_DATA);
2823
 
      
 
2687
 
2824
2688
      if (retbuf)
2825
2689
        {
2826
2690
          *retbuflen = p - *retbuf;
2834
2698
 
2835
2699
  if (DBG_CARD_IO && retbuf && sw == SW_SUCCESS)
2836
2700
    log_printhex ("      dump: ", *retbuf, *retbuflen);
2837
 
 
 
2701
 
2838
2702
  return sw;
2839
2703
#undef RESULTLEN
2840
2704
}
2848
2712
   data.  The length of that data will be put into *RETBUFLEN.  The
2849
2713
   caller is reponsible for releasing the buffer even in case of
2850
2714
   errors.  */
2851
 
int 
 
2715
int
2852
2716
apdu_send (int slot, int class, int ins, int p0, int p1,
2853
2717
           int lc, const char *data, unsigned char **retbuf, size_t *retbuflen)
2854
2718
{
2855
 
  return apdu_send_le (slot, class, ins, p0, p1, lc, data, 256, 
 
2719
  return apdu_send_le (slot, class, ins, p0, p1, lc, data, 256,
2856
2720
                       retbuf, retbuflen);
2857
2721
}
2858
2722
 
2862
2726
   also be passed as NULL. The return value is the status word or -1
2863
2727
   for an invalid SLOT or other non card related error.  No data will be
2864
2728
   returned. */
2865
 
int 
 
2729
int
2866
2730
apdu_send_simple (int slot, int class, int ins, int p0, int p1,
2867
2731
                  int lc, const char *data)
2868
2732
{
2878
2742
   the end.  The function does not return a regular status word but 0
2879
2743
   on success.  If the slot is locked, the fucntion returns
2880
2744
   immediately.*/
2881
 
int 
 
2745
int
2882
2746
apdu_send_direct (int slot, const unsigned char *apdudata, size_t apdudatalen,
2883
2747
                  int handle_more,
2884
2748
                  unsigned char **retbuf, size_t *retbuflen)
2899
2763
  if ((sw = trylock_slot (slot)))
2900
2764
    return sw;
2901
2765
 
2902
 
  /* We simply trucntate a too long APDU.  */
 
2766
  /* We simply trunctate a too long APDU.  */
2903
2767
  if (apdudatalen > sizeof apdu)
2904
2768
    apdudatalen = sizeof apdu;
2905
2769
  apdulen = apdudatalen;
2906
2770
  memcpy (apdu, apdudata, apdudatalen);
2907
2771
  class = apdulen? *apdu : 0;
2908
2772
 
2909
 
 
 
2773
  resultlen = RESULTLEN;
2910
2774
  rc = send_apdu (slot, apdu, apdulen, result, &resultlen);
2911
2775
  if (rc || resultlen < 2)
2912
2776
    {
2920
2784
  resultlen -= 2;
2921
2785
  if (DBG_CARD_IO)
2922
2786
    {
2923
 
      log_debug (" response: sw=%04X  datalen=%d\n", sw, resultlen);
 
2787
      log_debug (" response: sw=%04X  datalen=%d\n",
 
2788
                 sw, (unsigned int)resultlen);
2924
2789
      if ( !retbuf && (sw == SW_SUCCESS || (sw & 0xff00) == SW_MORE_DATA))
2925
2790
        log_printhex ("     dump: ", result, resultlen);
2926
2791
    }
2948
2813
      do
2949
2814
        {
2950
2815
          int len = (sw & 0x00ff);
2951
 
          
 
2816
 
2952
2817
          if (DBG_CARD_IO)
2953
2818
            log_debug ("apdu_send_direct(%d): %d more bytes available\n",
2954
2819
                       slot, len);
2957
2822
          apdu[apdulen++] = 0xC0;
2958
2823
          apdu[apdulen++] = 0;
2959
2824
          apdu[apdulen++] = 0;
2960
 
          apdu[apdulen++] = len; 
 
2825
          apdu[apdulen++] = len;
2961
2826
          memset (apdu+apdulen, 0, sizeof (apdu) - apdulen);
2962
2827
          resultlen = RESULTLEN;
2963
2828
          rc = send_apdu (slot, apdu, apdulen, result, &resultlen);
2972
2837
          resultlen -= 2;
2973
2838
          if (DBG_CARD_IO)
2974
2839
            {
2975
 
              log_debug ("     more: sw=%04X  datalen=%d\n", sw, resultlen);
 
2840
              log_debug ("     more: sw=%04X  datalen=%d\n",
 
2841
                         sw, (unsigned int)resultlen);
2976
2842
              if (!retbuf && (sw==SW_SUCCESS || (sw&0xff00)==SW_MORE_DATA))
2977
2843
                log_printhex ("     dump: ", result, resultlen);
2978
2844
            }
3005
2871
                      slot, sw);
3006
2872
        }
3007
2873
      while ((sw & 0xff00) == SW_MORE_DATA);
3008
 
      
 
2874
 
3009
2875
      if (retbuf)
3010
2876
        {
3011
2877
          *retbuflen = p - *retbuf;
3041
2907
 
3042
2908
  if (DBG_CARD_IO && retbuf)
3043
2909
    log_printhex ("      dump: ", *retbuf, *retbuflen);
3044
 
 
 
2910
 
3045
2911
  return 0;
3046
2912
#undef RESULTLEN
3047
2913
}
3048
 
 
3049