~ubuntu-branches/ubuntu/natty/gnupg2/natty-security

« back to all changes in this revision

Viewing changes to scd/ccid-driver.c

  • Committer: Bazaar Package Importer
  • Author(s): Matthias Urlichs
  • Date: 2005-12-08 22:13:21 UTC
  • mfrom: (1.1.1 upstream)
  • Revision ID: james.westby@ubuntu.com-20051208221321-4rvs2vu835iam5wv
Tags: 1.9.19-2
* Convert debian/changelog to UTF-8.
* Put gnupg-agent and gpgsm lintian overrides in the respectively
  right package.  Closes: #335066
* Added debhelper tokens to maintainer scripts.
* xsession fixes:
  o Added host name to gpg-agent PID file name.  Closes: #312717
  o Fixed xsession script to be able to run under zsh.  Closes: #308516
  o Don't run gpg-agent if one is already running.  Closes: #336480
* debian/control:
  o Fixed package description of gpgsm package.  Closes: #299842
  o Added mention of gpg-agent to description of gnupg-agent package.
    Closes: #304355
* Thanks to Peter Eisentraut <petere@debian.org> for all of the above.

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 Free Software Foundation, Inc.
 
2
 *      Copyright (C) 2003, 2004, 2005 Free Software Foundation, Inc.
3
3
 *      Written by Werner Koch.
4
4
 *
5
5
 * This file is part of GnuPG.
16
16
 *
17
17
 * You should have received a copy of the GNU General Public License
18
18
 * along with this program; if not, write to the Free Software
19
 
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
 
19
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
 
20
 * USA.
20
21
 *
21
22
 * ALTERNATIVELY, this file may be distributed under the terms of the
22
23
 * following license, in which case the provisions of this license are
52
53
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
53
54
 * OF THE POSSIBILITY OF SUCH DAMAGE.
54
55
 *
55
 
 * $Id: ccid-driver.c,v 1.20 2004/12/28 07:30:57 wk Exp $
 
56
 * $Date: 2005/06/16 08:11:59 $
56
57
 */
57
58
 
58
59
 
108
109
#  include "scdaemon.h"
109
110
#endif
110
111
 
111
 
/* Define to print information pertaining the T=1 protocol. */
112
 
#undef DEBUG_T1 
113
 
 
114
112
 
115
113
# define DEBUGOUT(t)         do { if (debug_level) \
116
114
                                  log_debug (DRVNAME t); } while (0)
120
118
                                  log_debug (DRVNAME t,(a),(b)); } while (0)
121
119
# define DEBUGOUT_3(t,a,b,c) do { if (debug_level) \
122
120
                                  log_debug (DRVNAME t,(a),(b),(c));} while (0)
 
121
# define DEBUGOUT_4(t,a,b,c,d) do { if (debug_level) \
 
122
                              log_debug (DRVNAME t,(a),(b),(c),(d));} while (0)
123
123
# define DEBUGOUT_CONT(t)    do { if (debug_level) \
124
124
                                  log_printf (t); } while (0)
125
125
# define DEBUGOUT_CONT_1(t,a)  do { if (debug_level) \
141
141
                     fprintf (stderr, DRVNAME t, (a), (b)); } while (0)
142
142
# define DEBUGOUT_3(t,a,b,c)  do { if (debug_level) \
143
143
                     fprintf (stderr, DRVNAME t, (a), (b), (c)); } while (0)
 
144
# define DEBUGOUT_4(t,a,b,c,d)  do { if (debug_level) \
 
145
                     fprintf (stderr, DRVNAME t, (a), (b), (c), (d));} while(0)
144
146
# define DEBUGOUT_CONT(t)     do { if (debug_level) \
145
147
                     fprintf (stderr, t); } while (0)
146
148
# define DEBUGOUT_CONT_1(t,a) do { if (debug_level) \
183
185
};
184
186
 
185
187
 
 
188
/* Two macro to detect whether a CCID command has failed and to get
 
189
   the error code.  These macros assume that we can access the
 
190
   mandatory first 10 bytes of a CCID message in BUF. */
 
191
#define CCID_COMMAND_FAILED(buf) ((buf)[7] & 0x40)
 
192
#define CCID_ERROR_CODE(buf)     (((unsigned char *)(buf))[8])
 
193
 
 
194
 
186
195
/* We need to know the vendor to do some hacks. */
187
196
enum {
188
 
  VENDOR_SCM = 0x04e6
 
197
  VENDOR_SCM    = 0x04e6,
 
198
  VENDOR_CHERRY = 0x046a,
 
199
  VENDOR_GEMPC  = 0x08e6
189
200
};
190
201
 
191
202
 
216
227
 
217
228
 
218
229
static int initialized_usb; /* Tracks whether USB has been initialized. */
219
 
static int debug_level;     /* Flag to control the debug output.  */
 
230
static int debug_level;     /* Flag to control the debug output. 
 
231
                               0 = No debugging
 
232
                               1 = USB I/O info
 
233
                               2 = T=1 protocol tracing
 
234
                              */
220
235
 
221
236
 
222
237
static unsigned int compute_edc (const unsigned char *data, size_t datalen,
223
238
                                 int use_crc);
224
239
static int bulk_out (ccid_driver_t handle, unsigned char *msg, size_t msglen);
225
240
static int bulk_in (ccid_driver_t handle, unsigned char *buffer, size_t length,
226
 
                    size_t *nread, int expected_type, int seqno);
 
241
                    size_t *nread, int expected_type, int seqno, int timeout,
 
242
                    int no_debug);
227
243
 
228
244
/* Convert a little endian stored 4 byte value into an unsigned
229
245
   integer. */
243
259
}
244
260
 
245
261
 
 
262
/* Pint an error message for a failed CCID command including a textual
 
263
   error code.  MSG is shall be the CCID message of at least 10 bytes. */
 
264
static void
 
265
print_command_failed (const unsigned char *msg)
 
266
{
 
267
  const char *t;
 
268
  char buffer[100];
 
269
  int ec;
 
270
 
 
271
  if (!debug_level)
 
272
    return;
 
273
 
 
274
  ec = CCID_ERROR_CODE (msg);
 
275
  switch (ec)
 
276
    {
 
277
    case 0x00: t = "Command not supported"; break;
 
278
    
 
279
    case 0xE0: t = "Slot busy"; break;
 
280
    case 0xEF: t = "PIN cancelled"; break;
 
281
    case 0xF0: t = "PIN timeout"; break;
 
282
 
 
283
    case 0xF2: t = "Automatic sequence ongoing"; break;
 
284
    case 0xF3: t = "Deactivated Protocol"; break;
 
285
    case 0xF4: t = "Procedure byte conflict"; break;
 
286
    case 0xF5: t = "ICC class not supported"; break;
 
287
    case 0xF6: t = "ICC protocol not supported"; break;
 
288
    case 0xF7: t = "Bad checksum in ATR"; break;
 
289
    case 0xF8: t = "Bad TS in ATR"; break;
 
290
 
 
291
    case 0xFB: t = "An all inclusive hardware error occurred"; break;
 
292
    case 0xFC: t = "Overrun error while talking to the ICC"; break;
 
293
    case 0xFD: t = "Parity error while talking to the ICC"; break;
 
294
    case 0xFE: t = "CCID timed out while talking to the ICC"; break;
 
295
    case 0xFF: t = "Host aborted the current activity"; break;
 
296
 
 
297
    default:
 
298
      if (ec > 0 && ec < 128)
 
299
        sprintf (buffer, "Parameter error at offset %d", ec);
 
300
      else
 
301
        sprintf (buffer, "Error code %02X", ec);
 
302
      t = buffer;
 
303
      break;
 
304
    }
 
305
  DEBUGOUT_1 ("CCID command failed: %s\n", t);
 
306
}
 
307
  
 
308
 
246
309
 
247
310
 
248
311
/* Parse a CCID descriptor, optionally print all available features
403
466
  if (buf[49] == 0xff)
404
467
    DEBUGOUT_CONT ("echo\n");
405
468
  else
406
 
    DEBUGOUT_1 ("  %02X\n", buf[48]);
 
469
    DEBUGOUT_CONT_1 ("  %02X\n", buf[48]);
407
470
 
408
471
  DEBUGOUT (  "  wlcdLayout           ");
409
472
  if (!buf[50] && !buf[51])
446
509
     send a frame of n*wMaxPacketSize back to us.  Given that
447
510
     wMaxPacketSize is 64 for these readers we set the IFSD to a value
448
511
     lower than that:
449
 
        64 - 10 CCID header -  4 T1frame - 2 reserved = 48 */
 
512
        64 - 10 CCID header -  4 T1frame - 2 reserved = 48
 
513
     Product Ids:
 
514
         0xe001 - SCR 331 
 
515
         0x5111 - SCR 331-DI 
 
516
         0x5115 - SCR 335 
 
517
         0xe003 - SPR 532 
 
518
  */
450
519
  if (handle->id_vendor == VENDOR_SCM
451
 
      /* FIXME: check whether it is the same
452
 
                firmware version for all drivers.  */
453
 
      && handle->bcd_device < 0x0513
454
 
      && handle->max_ifsd > 48)
 
520
      && handle->max_ifsd > 48      
 
521
      && (  (handle->id_product == 0xe001 && handle->bcd_device < 0x0516)
 
522
          ||(handle->id_product == 0x5111 && handle->bcd_device < 0x0620)
 
523
          ||(handle->id_product == 0x5115 && handle->bcd_device < 0x0514)
 
524
          ||(handle->id_product == 0xe003 && handle->bcd_device < 0x0504)
 
525
          ))
455
526
    {
456
527
      DEBUGOUT ("enabling workaround for buggy SCM readers\n");
457
528
      handle->max_ifsd = 48;
485
556
     all in a 2 bute Unicode encoding using little endian. */
486
557
  rc = usb_control_msg (idev, USB_ENDPOINT_IN, USB_REQ_GET_DESCRIPTOR,
487
558
                        (USB_DT_STRING << 8), 0, 
488
 
                        buf, sizeof buf, 1000 /* ms timeout */);
 
559
                        (char*)buf, sizeof buf, 1000 /* ms timeout */);
489
560
  if (rc < 4)
490
561
    langid = 0x0409; /* English.  */
491
562
  else
493
564
 
494
565
  rc = usb_control_msg (idev, USB_ENDPOINT_IN, USB_REQ_GET_DESCRIPTOR,
495
566
                        (USB_DT_STRING << 8) + idx, langid,
496
 
                        buf, sizeof buf, 1000 /* ms timeout */);
 
567
                        (char*)buf, sizeof buf, 1000 /* ms timeout */);
497
568
  if (rc < 2 || buf[1] != USB_DT_STRING)
498
569
    return NULL; /* Error or not a string. */
499
570
  len = buf[0];
699
770
                                  && ifcdesc->bInterfaceProtocol == 0)
700
771
                              || (ifcdesc->bInterfaceClass == 255
701
772
                                  && dev->descriptor.idVendor == 0x04e6
702
 
                                  && dev->descriptor.idProduct == 0xe003
703
 
                                  && ifcdesc->bInterfaceSubClass == 1
704
 
                                  && ifcdesc->bInterfaceProtocol == 1)))
 
773
                                  && dev->descriptor.idProduct == 0xe003)))
705
774
                        {
706
775
                          idev = usb_open (dev);
707
776
                          if (!idev)
821
890
 
822
891
/* Set the level of debugging to to usea dn return the old level.  -1
823
892
   just returns the old level.  A level of 0 disables debugging, 1
824
 
   enables debugging, other values are not yet defined. */
 
893
   enables debugging, 2 enables additional tracing of the T=1
 
894
   protocol, other values are not yet defined. */
825
895
int
826
896
ccid_set_debug_level (int level)
827
897
{
974
1044
      
975
1045
      rc = bulk_out (handle, msg, msglen);
976
1046
      if (!rc)
977
 
        bulk_in (handle, msg, sizeof msg, &msglen, RDR_to_PC_SlotStatus,seqno);
 
1047
        bulk_in (handle, msg, sizeof msg, &msglen, RDR_to_PC_SlotStatus,
 
1048
                 seqno, 2000, 0);
978
1049
      handle->powered_off = 1;
979
1050
    }
980
1051
  if (handle->idev)
1085
1156
 
1086
1157
  rc = usb_bulk_write (handle->idev, 
1087
1158
                       handle->ep_bulk_out,
1088
 
                       msg, msglen,
 
1159
                       (char*)msg, msglen,
1089
1160
                       1000 /* ms timeout */);
1090
1161
  if (rc == msglen)
1091
1162
    return 0;
1102
1173
   BUFFER and return the actual read number if bytes in NREAD. SEQNO
1103
1174
   is the sequence number used to send the request and EXPECTED_TYPE
1104
1175
   the type of message we expect. Does checks on the ccid
1105
 
   header. Returns 0 on success. */
 
1176
   header. TIMEOUT is the timeout value in ms. NO_DEBUG may be set to
 
1177
   avoid debug messages in case of no error. Returns 0 on success. */
1106
1178
static int
1107
1179
bulk_in (ccid_driver_t handle, unsigned char *buffer, size_t length,
1108
 
         size_t *nread, int expected_type, int seqno)
 
1180
         size_t *nread, int expected_type, int seqno, int timeout,
 
1181
         int no_debug)
1109
1182
{
1110
1183
  int i, rc;
1111
1184
  size_t msglen;
1116
1189
 retry:
1117
1190
  rc = usb_bulk_read (handle->idev, 
1118
1191
                      handle->ep_bulk_in,
1119
 
                      buffer, length,
1120
 
                      10000 /* ms timeout */ );
1121
 
  /* Fixme: instead of using a 10 second timeout we should better
1122
 
     handle the timeout here and retry if appropriate.  */
 
1192
                      (char*)buffer, length,
 
1193
                      timeout);
1123
1194
  if (rc < 0)
1124
1195
    {
1125
1196
      DEBUGOUT_1 ("usb_bulk_read error: %s\n", strerror (errno));
1157
1228
                  buffer[7], buffer[8]);
1158
1229
      goto retry;
1159
1230
    }
1160
 
  
1161
 
  DEBUGOUT_3 ("status: %02X  error: %02X  octet[9]: %02X\n"
1162
 
              "               data:",  buffer[7], buffer[8], buffer[9] );
1163
 
  for (i=10; i < msglen; i++)
1164
 
    DEBUGOUT_CONT_1 (" %02X", buffer[i]);
1165
 
  DEBUGOUT_LF ();
1166
 
 
 
1231
 
 
1232
  if (!no_debug)
 
1233
    {
 
1234
      DEBUGOUT_3 ("status: %02X  error: %02X  octet[9]: %02X\n"
 
1235
                  "               data:",  buffer[7], buffer[8], buffer[9] );
 
1236
      for (i=10; i < msglen; i++)
 
1237
        DEBUGOUT_CONT_1 (" %02X", buffer[i]);
 
1238
      DEBUGOUT_LF ();
 
1239
    }
 
1240
  if (CCID_COMMAND_FAILED (buffer))
 
1241
    print_command_failed (buffer);
 
1242
 
 
1243
  /* Check whether a card is at all available. */
1167
1244
  switch ((buffer[7] & 0x03))
1168
1245
    {
1169
1246
    case 0: /* no error */ break;
1175
1252
}
1176
1253
 
1177
1254
 
1178
 
/* Note that this fucntion won't return the error codes NO_CARD or
 
1255
/* Note that this function won't return the error codes NO_CARD or
1179
1256
   CARD_INACTIVE */
1180
1257
static int 
1181
1258
send_escape_cmd (ccid_driver_t handle,
1206
1283
  rc = bulk_out (handle, msg, msglen);
1207
1284
  if (rc)
1208
1285
    return rc;
1209
 
  rc = bulk_in (handle, msg, sizeof msg, &msglen, RDR_to_PC_Escape, seqno);
 
1286
  rc = bulk_in (handle, msg, sizeof msg, &msglen, RDR_to_PC_Escape,
 
1287
                seqno, 5000, 0);
1210
1288
 
1211
1289
  return rc;
1212
1290
}
1223
1301
 
1224
1302
  rc = usb_bulk_read (handle->idev, 
1225
1303
                      handle->ep_intr,
1226
 
                      msg, sizeof msg,
 
1304
                      (char*)msg, sizeof msg,
1227
1305
                      0 /* ms timeout */ );
1228
1306
  if (rc < 0 && errno == ETIMEDOUT)
1229
1307
    return 0;
1276
1354
  unsigned char msg[100];
1277
1355
  size_t msglen;
1278
1356
  unsigned char seqno;
 
1357
  int retries = 0;
1279
1358
 
 
1359
 retry:
1280
1360
  msg[0] = PC_to_RDR_GetSlotStatus;
1281
1361
  msg[5] = 0; /* slot */
1282
1362
  msg[6] = seqno = handle->seqno++;
1288
1368
  rc = bulk_out (handle, msg, 10);
1289
1369
  if (rc)
1290
1370
    return rc;
1291
 
  rc = bulk_in (handle, msg, sizeof msg, &msglen, RDR_to_PC_SlotStatus, seqno);
 
1371
  /* Note that we set the NO_DEBUG flag here, so that the logs won't
 
1372
     get cluttered up by a ticker function checking for the slot
 
1373
     status and debugging enabled. */
 
1374
  rc = bulk_in (handle, msg, sizeof msg, &msglen, RDR_to_PC_SlotStatus,
 
1375
                seqno, retries? 1000 : 200, 1);
 
1376
  if (rc == CCID_DRIVER_ERR_CARD_IO_ERROR && retries < 3)
 
1377
    {
 
1378
      if (!retries)
 
1379
        {
 
1380
          DEBUGOUT ("USB: CALLING USB_CLEAR_HALT\n");
 
1381
          usb_clear_halt (handle->idev, handle->ep_bulk_in);
 
1382
          usb_clear_halt (handle->idev, handle->ep_bulk_out);
 
1383
        }
 
1384
      else
 
1385
          DEBUGOUT ("USB: RETRYING bulk_in AGAIN\n");
 
1386
      retries++;
 
1387
      goto retry;
 
1388
    }
1292
1389
  if (rc && rc != CCID_DRIVER_ERR_NO_CARD
1293
1390
      && rc != CCID_DRIVER_ERR_CARD_INACTIVE)
1294
1391
    return rc;
1303
1400
              unsigned char *atr, size_t maxatrlen, size_t *atrlen)
1304
1401
{
1305
1402
  int rc;
 
1403
  int statusbits;
1306
1404
  unsigned char msg[100];
1307
1405
  unsigned char *tpdu;
1308
1406
  size_t msglen, tpdulen;
1310
1408
  int use_crc = 0;
1311
1409
  unsigned int edc;
1312
1410
  int i;
1313
 
 
 
1411
  int tried_iso = 0;
 
1412
 
 
1413
  /* First check whether a card is available.  */
 
1414
  rc = ccid_slot_status (handle, &statusbits);
 
1415
  if (rc)
 
1416
    return rc;
 
1417
  if (statusbits == 2)
 
1418
    return CCID_DRIVER_ERR_NO_CARD;
 
1419
 
 
1420
  /* For an inactive and also for an active card, issue the PowerOn
 
1421
     command to get the ATR.  */
 
1422
 again:
1314
1423
  msg[0] = PC_to_RDR_IccPowerOn;
1315
1424
  msg[5] = 0; /* slot */
1316
1425
  msg[6] = seqno = handle->seqno++;
1323
1432
  rc = bulk_out (handle, msg, msglen);
1324
1433
  if (rc)
1325
1434
    return rc;
1326
 
  rc = bulk_in (handle, msg, sizeof msg, &msglen, RDR_to_PC_DataBlock, seqno);
 
1435
  rc = bulk_in (handle, msg, sizeof msg, &msglen, RDR_to_PC_DataBlock,
 
1436
                seqno, 5000, 0);
1327
1437
  if (rc)
1328
1438
    return rc;
 
1439
  if (!tried_iso && CCID_COMMAND_FAILED (msg) && CCID_ERROR_CODE (msg) == 0xbb
 
1440
      && ((handle->id_vendor == VENDOR_CHERRY
 
1441
           && handle->id_product == 0x0005)
 
1442
          || (handle->id_vendor == VENDOR_GEMPC
 
1443
              && handle->id_product == 0x4433)
 
1444
          ))
 
1445
    {
 
1446
      tried_iso = 1;
 
1447
      /* Try switching to ISO mode. */
 
1448
      if (!send_escape_cmd (handle, (const unsigned char*)"\xF1\x01", 2))
 
1449
        goto again;
 
1450
    }
 
1451
  else if (CCID_COMMAND_FAILED (msg))
 
1452
    return CCID_DRIVER_ERR_CARD_IO_ERROR;
 
1453
 
1329
1454
 
1330
1455
  handle->powered_off = 0;
1331
1456
  
1367
1492
  if (rc)
1368
1493
    return rc;
1369
1494
  /* Note that we ignore the error code on purpose. */
1370
 
  bulk_in (handle, msg, sizeof msg, &msglen, RDR_to_PC_Parameters, seqno);
 
1495
  bulk_in (handle, msg, sizeof msg, &msglen, RDR_to_PC_Parameters,
 
1496
           seqno, 5000, 0);
1371
1497
 
1372
1498
  handle->t1_ns = 0;
1373
1499
  handle->t1_nr = 0;
1401
1527
        DEBUGOUT_CONT_1 (" %02X", msg[i]);
1402
1528
      DEBUGOUT_LF ();
1403
1529
 
1404
 
#ifdef DEBUG_T1      
1405
 
      fprintf (stderr, "T1: put %c-block seq=%d\n",
1406
 
               ((msg[11] & 0xc0) == 0x80)? 'R' :
1407
 
               (msg[11] & 0x80)? 'S' : 'I',
1408
 
               ((msg[11] & 0x80)? !!(msg[11]& 0x10) : !!(msg[11] & 0x40)));
1409
 
#endif  
 
1530
      if (debug_level > 1)
 
1531
        DEBUGOUT_3 ("T=1: put %c-block seq=%d%s\n",
 
1532
                      ((msg[11] & 0xc0) == 0x80)? 'R' :
 
1533
                                (msg[11] & 0x80)? 'S' : 'I',
 
1534
                      ((msg[11] & 0x80)? !!(msg[11]& 0x10)
 
1535
                                       : !!(msg[11] & 0x40)),
 
1536
                    (!(msg[11] & 0x80) && (msg[11] & 0x20)? " [more]":""));
1410
1537
 
1411
1538
      rc = bulk_out (handle, msg, msglen);
1412
1539
      if (rc)
1414
1541
 
1415
1542
 
1416
1543
      rc = bulk_in (handle, msg, sizeof msg, &msglen,
1417
 
                    RDR_to_PC_DataBlock, seqno);
 
1544
                    RDR_to_PC_DataBlock, seqno, 5000, 0);
1418
1545
      if (rc)
1419
1546
        return rc;
1420
1547
      
1424
1551
      if (tpdulen < 4) 
1425
1552
        return CCID_DRIVER_ERR_ABORTED; 
1426
1553
 
1427
 
#ifdef DEBUG_T1
1428
 
      fprintf (stderr, "T1: got %c-block seq=%d err=%d\n",
1429
 
               ((msg[11] & 0xc0) == 0x80)? 'R' :
1430
 
               (msg[11] & 0x80)? 'S' : 'I',
1431
 
               ((msg[11] & 0x80)? !!(msg[11]& 0x10) : !!(msg[11] & 0x40)),
1432
 
               ((msg[11] & 0xc0) == 0x80)? (msg[11] & 0x0f) : 0
1433
 
               );
1434
 
#endif
 
1554
      if (debug_level > 1)
 
1555
        DEBUGOUT_4 ("T=1: got %c-block seq=%d err=%d%s\n",
 
1556
                    ((msg[11] & 0xc0) == 0x80)? 'R' :
 
1557
                              (msg[11] & 0x80)? 'S' : 'I',
 
1558
                    ((msg[11] & 0x80)? !!(msg[11]& 0x10)
 
1559
                                     : !!(msg[11] & 0x40)),
 
1560
                    ((msg[11] & 0xc0) == 0x80)? (msg[11] & 0x0f) : 0,
 
1561
                    (!(msg[11] & 0x80) && (msg[11] & 0x20)? " [more]":""));
 
1562
 
1435
1563
      if ((tpdu[1] & 0xe0) != 0xe0 || tpdu[2] != 1)
1436
1564
        {
1437
1565
          DEBUGOUT ("invalid response for S-block (Change-IFSD)\n");
1510
1638
 
1511
1639
  msg = recv_buffer;
1512
1640
  rc = bulk_in (handle, msg, sizeof recv_buffer, &msglen,
1513
 
                RDR_to_PC_DataBlock, seqno);
 
1641
                RDR_to_PC_DataBlock, seqno, 5000, 0);
1514
1642
  if (rc)
1515
1643
    return rc;
1516
1644
      
1670
1798
        DEBUGOUT_CONT_1 (" %02X", msg[i]);
1671
1799
      DEBUGOUT_LF ();
1672
1800
 
1673
 
#ifdef DEBUG_T1      
1674
 
      fprintf (stderr, "T1: put %c-block seq=%d\n",
1675
 
               ((msg[11] & 0xc0) == 0x80)? 'R' :
1676
 
               (msg[11] & 0x80)? 'S' : 'I',
1677
 
        ((msg[11] & 0x80)? !!(msg[11]& 0x10) : !!(msg[11] & 0x40)));
1678
 
#endif  
 
1801
      if (debug_level > 1)
 
1802
          DEBUGOUT_3 ("T=1: put %c-block seq=%d%s\n",
 
1803
                      ((msg[11] & 0xc0) == 0x80)? 'R' :
 
1804
                                (msg[11] & 0x80)? 'S' : 'I',
 
1805
                      ((msg[11] & 0x80)? !!(msg[11]& 0x10)
 
1806
                                       : !!(msg[11] & 0x40)),
 
1807
                      (!(msg[11] & 0x80) && (msg[11] & 0x20)? " [more]":""));
1679
1808
 
1680
1809
      rc = bulk_out (handle, msg, msglen);
1681
1810
      if (rc)
1683
1812
 
1684
1813
      msg = recv_buffer;
1685
1814
      rc = bulk_in (handle, msg, sizeof recv_buffer, &msglen,
1686
 
                    RDR_to_PC_DataBlock, seqno);
 
1815
                    RDR_to_PC_DataBlock, seqno, 5000, 0);
1687
1816
      if (rc)
1688
1817
        return rc;
1689
1818
      
1692
1821
      
1693
1822
      if (tpdulen < 4) 
1694
1823
        {
1695
 
          usb_clear_halt (handle->idev, 0x82);
 
1824
          usb_clear_halt (handle->idev, handle->ep_bulk_in);
1696
1825
          return CCID_DRIVER_ERR_ABORTED; 
1697
1826
        }
1698
 
#ifdef DEBUG_T1
1699
 
      fprintf (stderr, "T1: got %c-block seq=%d err=%d\n",
1700
 
               ((msg[11] & 0xc0) == 0x80)? 'R' :
1701
 
               (msg[11] & 0x80)? 'S' : 'I',
1702
 
        ((msg[11] & 0x80)? !!(msg[11]& 0x10) : !!(msg[11] & 0x40)),
1703
 
               ((msg[11] & 0xc0) == 0x80)? (msg[11] & 0x0f) : 0
1704
 
               );
1705
 
#endif
 
1827
 
 
1828
      if (debug_level > 1)
 
1829
        DEBUGOUT_4 ("T=1: got %c-block seq=%d err=%d%s\n",
 
1830
                    ((msg[11] & 0xc0) == 0x80)? 'R' :
 
1831
                              (msg[11] & 0x80)? 'S' : 'I',
 
1832
                    ((msg[11] & 0x80)? !!(msg[11]& 0x10) : !!(msg[11] & 0x40)),
 
1833
                    ((msg[11] & 0xc0) == 0x80)? (msg[11] & 0x0f) : 0,
 
1834
                    (!(msg[11] & 0x80) && (msg[11] & 0x20)? " [more]":""));
1706
1835
 
1707
1836
      if (!(tpdu[1] & 0x80))
1708
1837
        { /* This is an I-block. */
1778
1907
              msg = send_buffer;
1779
1908
              tpdulen = last_tpdulen;
1780
1909
            }
1781
 
          else if (sending && !!(tpdu[1] & 0x40) == handle->t1_ns)
1782
 
            { /* Reponse does not match our sequence number. */
 
1910
          else if (sending && !!(tpdu[1] & 0x10) == handle->t1_ns)
 
1911
            { /* Response does not match our sequence number. */
1783
1912
              DEBUGOUT ("R-block with wrong seqno received on more bit\n");
1784
1913
              return CCID_DRIVER_ERR_CARD_IO_ERROR;
1785
1914
            }
1799
1928
      else 
1800
1929
        { /* This is a S-block. */
1801
1930
          retries = 0;
1802
 
          DEBUGOUT_2 ("T1 S-block %s received cmd=%d\n",
 
1931
          DEBUGOUT_2 ("T=1 S-block %s received cmd=%d\n",
1803
1932
                      (tpdu[1] & 0x20)? "response": "request",
1804
1933
                      (tpdu[1] & 0x1f));
1805
1934
          if ( !(tpdu[1] & 0x20) && (tpdu[1] & 0x1f) == 3 && tpdu[2])
1817
1946
              if (use_crc)
1818
1947
                tpdu[tpdulen++] = (edc >> 8);
1819
1948
              tpdu[tpdulen++] = edc;
1820
 
              DEBUGOUT_1 ("T1 waittime extension of bwi=%d\n", bwi);
 
1949
              DEBUGOUT_1 ("T=1 waittime extension of bwi=%d\n", bwi);
1821
1950
            }
1822
1951
          else
1823
1952
            return CCID_DRIVER_ERR_CARD_IO_ERROR;
1898
2027
  if (handle->id_vendor == VENDOR_SCM)
1899
2028
    {
1900
2029
      DEBUGOUT ("sending escape sequence to switch to a case 1 APDU\n");
1901
 
      rc = send_escape_cmd (handle, "\x80\x02\x00", 3);
 
2030
      rc = send_escape_cmd (handle, (const unsigned char*)"\x80\x02\x00", 3);
1902
2031
      if (rc)
1903
2032
        return rc;
1904
2033
    }
1960
2089
  
1961
2090
  msg = recv_buffer;
1962
2091
  rc = bulk_in (handle, msg, sizeof recv_buffer, &msglen,
1963
 
                RDR_to_PC_DataBlock, seqno);
 
2092
                RDR_to_PC_DataBlock, seqno, 5000, 0);
1964
2093
  if (rc)
1965
2094
    return rc;
1966
2095
  
1972
2101
      usb_clear_halt (handle->idev, handle->ep_bulk_in);
1973
2102
      return CCID_DRIVER_ERR_ABORTED; 
1974
2103
    }
1975
 
#ifdef DEBUG_T1
1976
 
  fprintf (stderr, "T1: got %c-block seq=%d err=%d\n",
1977
 
           ((msg[11] & 0xc0) == 0x80)? 'R' :
1978
 
           (msg[11] & 0x80)? 'S' : 'I',
1979
 
           ((msg[11] & 0x80)? !!(msg[11]& 0x10) : !!(msg[11] & 0x40)),
1980
 
           ((msg[11] & 0xc0) == 0x80)? (msg[11] & 0x0f) : 0
1981
 
           );
1982
 
#endif
 
2104
  if (debug_level > 1)
 
2105
    DEBUGOUT_4 ("T=1: got %c-block seq=%d err=%d%s\n",
 
2106
                ((msg[11] & 0xc0) == 0x80)? 'R' :
 
2107
                          (msg[11] & 0x80)? 'S' : 'I',
 
2108
                ((msg[11] & 0x80)? !!(msg[11]& 0x10) : !!(msg[11] & 0x40)),
 
2109
                ((msg[11] & 0xc0) == 0x80)? (msg[11] & 0x0f) : 0,
 
2110
                (!(msg[11] & 0x80) && (msg[11] & 0x20)? " [more]":""));
1983
2111
 
1984
2112
  if (!(tpdu[1] & 0x80))
1985
2113
    { /* This is an I-block. */
2026
2154
          DEBUGOUT ("No retries supported for Secure operation\n");
2027
2155
          return CCID_DRIVER_ERR_CARD_IO_ERROR;
2028
2156
        }
2029
 
      else if (!!(tpdu[1] & 0x40) == handle->t1_ns)
 
2157
      else if (!!(tpdu[1] & 0x10) == handle->t1_ns)
2030
2158
        { /* Reponse does not match our sequence number. */
2031
2159
          DEBUGOUT ("R-block with wrong seqno received on more bit\n");
2032
2160
          return CCID_DRIVER_ERR_CARD_IO_ERROR;
2039
2167
    }
2040
2168
  else 
2041
2169
    { /* This is a S-block. */
2042
 
      DEBUGOUT_2 ("T1 S-block %s received cmd=%d for Secure operation\n",
 
2170
      DEBUGOUT_2 ("T=1 S-block %s received cmd=%d for Secure operation\n",
2043
2171
                  (tpdu[1] & 0x20)? "response": "request",
2044
2172
                  (tpdu[1] & 0x1f));
2045
2173
      return CCID_DRIVER_ERR_CARD_IO_ERROR;