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

« back to all changes in this revision

Viewing changes to scd/apdu.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:
5
5
 *
6
6
 * GnuPG is free software; you can redistribute it and/or modify
7
7
 * it under the terms of the GNU General Public License as published by
8
 
 * the Free Software Foundation; either version 2 of the License, or
 
8
 * the Free Software Foundation; either version 3 of the License, or
9
9
 * (at your option) any later version.
10
10
 *
11
11
 * GnuPG is distributed in the hope that it will be useful,
14
14
 * GNU General Public License for more details.
15
15
 *
16
16
 * You should have received a copy of the GNU General Public License
17
 
 * along with this program; if not, write to the Free Software
18
 
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
19
 
 * USA.
 
17
 * along with this program; if not, see <http://www.gnu.org/licenses/>.
20
18
 *
21
 
 * $Id: apdu.c,v 1.4.2.30 2005/06/16 08:11:59 wk Exp $
 
19
 * $Id: apdu.c 4555 2007-08-01 18:25:32Z wk $
22
20
 */
23
21
 
24
22
/* NOTE: This module is also used by other software, thus the use of
33
31
#include <assert.h>
34
32
#include <signal.h>
35
33
#ifdef USE_GNU_PTH
36
 
# include <pth.h>
37
34
# include <unistd.h>
38
35
# include <fcntl.h>
 
36
# include <pth.h>
39
37
#endif
40
38
 
41
39
 
62
60
#endif /* GNUPG_MAJOR_VERSION != 1 */
63
61
 
64
62
#include "apdu.h"
65
 
#include "dynload.h"
66
63
#include "ccid-driver.h"
67
64
 
68
65
 
90
87
#define MAX_OPEN_FDS 20
91
88
#endif
92
89
 
93
 
/* Helper to pass patrameters related to keypad based operations. */
 
90
/* Helper to pass parameters related to keypad based operations. */
94
91
struct pininfo_s
95
92
{
96
93
  int mode;
222
219
#define PCSC_E_READER_UNAVAILABLE      0x80100017
223
220
#define PCSC_W_REMOVED_CARD            0x80100069
224
221
 
 
222
/* The PC/SC error is defined as a long as per specs.  Due to left
 
223
   shifts bit 31 will get sign extended.  We use this mask to fix
 
224
   it. */
 
225
#define PCSC_ERR_MASK(a)  ((a) & 0xffffffff)
 
226
 
225
227
 
226
228
struct pcsc_io_request_s
227
229
{
274
276
                                unsigned long *r_protocol,
275
277
                                unsigned char *atr, unsigned long *atrlen);
276
278
long (* DLSTDCALL pcsc_begin_transaction) (unsigned long card);
277
 
long (* DLSTDCALL pcsc_end_transaction) (unsigned long card);
 
279
long (* DLSTDCALL pcsc_end_transaction) (unsigned long card,
 
280
                                         unsigned long disposition);
278
281
long (* DLSTDCALL pcsc_transmit) (unsigned long card,
279
282
                                  const pcsc_io_request_t send_pci,
280
283
                                  const unsigned char *send_buffer,
738
741
{
739
742
  int rc;
740
743
 
741
 
  switch (ec)
 
744
  switch ( PCSC_ERR_MASK (ec) )
742
745
    {
743
746
    case 0:  rc = 0; break;
744
747
 
833
836
      sw = SW_HOST_GENERAL_ERROR;
834
837
      goto command_failed;
835
838
    }
836
 
  err = (msgbuf[5] << 24) | (msgbuf[6] << 16) | (msgbuf[7] << 8 ) | msgbuf[8];
 
839
  err = PCSC_ERR_MASK ((msgbuf[5] << 24) | (msgbuf[6] << 16)
 
840
                       | (msgbuf[7] << 8 ) | msgbuf[8]);
837
841
  if (err)
838
842
    {
839
843
      log_error ("PC/SC RESET failed: %s (0x%lx)\n",
980
984
      goto command_failed;
981
985
    }
982
986
  len -= 4; /* Already read the error code. */
983
 
  err = (msgbuf[5] << 24) | (msgbuf[6] << 16) | (msgbuf[7] << 8 ) | msgbuf[8];
 
987
  err = PCSC_ERR_MASK ((msgbuf[5] << 24) | (msgbuf[6] << 16)
 
988
                       | (msgbuf[7] << 8 ) | msgbuf[8]);
984
989
  if (err)
985
990
    {
986
991
      log_error ("pcsc_status failed: %s (0x%lx)\n",
1150
1155
      goto command_failed;
1151
1156
    }
1152
1157
  len -= 4; /* Already read the error code. */
1153
 
  err = (msgbuf[5] << 24) | (msgbuf[6] << 16) | (msgbuf[7] << 8 ) | msgbuf[8];
 
1158
  err = PCSC_ERR_MASK ((msgbuf[5] << 24) | (msgbuf[6] << 16)
 
1159
                       | (msgbuf[7] << 8 ) | msgbuf[8]);
1154
1160
  if (err)
1155
1161
    {
1156
1162
      log_error ("pcsc_transmit failed: %s (0x%lx)\n",
1282
1288
      goto command_failed;
1283
1289
    }
1284
1290
  len -= 4; /* Already read the error code. */
1285
 
  err = (msgbuf[5] << 24) | (msgbuf[6] << 16) | (msgbuf[7] << 8 ) | msgbuf[8];
 
1291
  err = PCSC_ERR_MASK ((msgbuf[5] << 24) | (msgbuf[6] << 16)
 
1292
                       | (msgbuf[7] << 8 ) | msgbuf[8]);
1286
1293
  if (err)
1287
1294
    log_error ("pcsc_close failed: %s (0x%lx)\n",
1288
1295
               pcsc_error_string (err), err);
1328
1335
  int err;
1329
1336
  unsigned int dummy_status;
1330
1337
  int sw = SW_HOST_CARD_IO_ERROR;
1331
 
  const char *wrapperpgm = GNUPG_LIBDIR "/pcsc-wrapper";
 
1338
  /* Note that we use the constant and not the fucntion because this
 
1339
     code won't be be used under Windows.  */
 
1340
  const char *wrapperpgm = GNUPG_LIBEXECDIR "/gnupg-pcsc-wrapper";
1332
1341
 
1333
1342
  if (access (wrapperpgm, X_OK))
1334
1343
    {
1469
1478
                 (unsigned long)len);
1470
1479
      goto command_failed;
1471
1480
    }
1472
 
  err = (msgbuf[5] << 24) | (msgbuf[6] << 16) | (msgbuf[7] << 8 ) | msgbuf[8];
 
1481
  err = PCSC_ERR_MASK ((msgbuf[5] << 24) | (msgbuf[6] << 16)
 
1482
                       | (msgbuf[7] << 8 ) | msgbuf[8]);
1473
1483
  if (err)
1474
1484
    {
1475
1485
      log_error ("PC/SC OPEN failed: %s\n", pcsc_error_string (err));
1594
1604
    }
1595
1605
  strcpy (reader_table[slot].rdrname, portstr? portstr : list);
1596
1606
  xfree (list);
 
1607
  list = NULL;
1597
1608
 
1598
1609
  err = pcsc_connect (reader_table[slot].pcsc.context,
1599
1610
                      reader_table[slot].rdrname,
1611
1622
      xfree (reader_table[slot].rdrname);
1612
1623
      reader_table[slot].rdrname = NULL;
1613
1624
      reader_table[slot].used = 0;
1614
 
      xfree (list);
1615
1625
      return -1 /*pcsc_error_to_sw (err)*/;
1616
1626
    }
1617
1627
 
2369
2379
}
2370
2380
 
2371
2381
/* Shutdown a reader; that is basically the same as a close but keeps
2372
 
   the handle ready for later use. A apdu_reset_header should be used
 
2382
   the handle ready for later use. A apdu_reset_reader should be used
2373
2383
   to get it active again. */
2374
2384
int
2375
2385
apdu_shutdown_reader (int slot)
2592
2602
         unsigned char **retbuf, size_t *retbuflen,
2593
2603
         struct pininfo_s *pininfo)
2594
2604
{
2595
 
#define RESULTLEN 256
 
2605
#define RESULTLEN 258
2596
2606
  unsigned char result[RESULTLEN+10]; /* 10 extra in case of bugs in
2597
2607
                                         the driver. */
2598
2608
  size_t resultlen;
2599
2609
  unsigned char apdu[5+256+1];
2600
2610
  size_t apdulen;
2601
2611
  int sw;
2602
 
  long rc; /* we need a long here due to PC/SC. */
 
2612
  long rc; /* We need a long here due to PC/SC. */
 
2613
  int did_exact_length_hack = 0;
2603
2614
 
2604
2615
  if (slot < 0 || slot >= MAX_READER || !reader_table[slot].used )
2605
2616
    return SW_HOST_NO_DRIVER;
2638
2649
  assert (sizeof (apdu) >= apdulen);
2639
2650
  /* As safeguard don't pass any garbage from the stack to the driver. */
2640
2651
  memset (apdu+apdulen, 0, sizeof (apdu) - apdulen);
 
2652
 exact_length_hack:
2641
2653
  resultlen = RESULTLEN;
2642
2654
  rc = send_apdu (slot, apdu, apdulen, result, &resultlen, pininfo);
2643
2655
  if (rc || resultlen < 2)
2648
2660
      return rc? rc : SW_HOST_INCOMPLETE_CARD_RESPONSE;
2649
2661
    }
2650
2662
  sw = (result[resultlen-2] << 8) | result[resultlen-1];
2651
 
  /* store away the returned data but strip the statusword. */
 
2663
  if (!did_exact_length_hack && SW_EXACT_LENGTH_P (sw))
 
2664
    {
 
2665
      apdu[apdulen-1] = (sw & 0x00ff);
 
2666
      did_exact_length_hack = 1;
 
2667
      goto exact_length_hack;
 
2668
    }
 
2669
 
 
2670
  /* Store away the returned data but strip the statusword. */
2652
2671
  resultlen -= 2;
2653
2672
  if (DBG_CARD_IO)
2654
2673
    {
2848
2867
   HANDLE_MORE set to true this function will handle the MORE DATA
2849
2868
   status and return all APDUs concatenated with one status word at
2850
2869
   the end.  The function does not return a regular status word but 0
2851
 
   on success.  If the slot is locked, the fucntion returns
2852
 
   immediately.*/
 
2870
   on success.  If the slot is locked, the function returns
 
2871
   immediately with an error.  */
2853
2872
int
2854
2873
apdu_send_direct (int slot, const unsigned char *apdudata, size_t apdudatalen,
2855
2874
                  int handle_more,
2856
2875
                  unsigned char **retbuf, size_t *retbuflen)
2857
2876
{
2858
 
#define RESULTLEN 256
 
2877
#define RESULTLEN 258
2859
2878
  unsigned char apdu[5+256+1];
2860
2879
  size_t apdulen;
2861
2880
  unsigned char result[RESULTLEN+10]; /* 10 extra in case of bugs in