aboutsummaryrefslogtreecommitdiffstats
path: root/scd/apdu.c
diff options
context:
space:
mode:
Diffstat (limited to 'scd/apdu.c')
-rw-r--r--scd/apdu.c419
1 files changed, 243 insertions, 176 deletions
diff --git a/scd/apdu.c b/scd/apdu.c
index 040de1461..33b0802c1 100644
--- a/scd/apdu.c
+++ b/scd/apdu.c
@@ -74,7 +74,7 @@
#endif
#endif
-
+
#define MAX_READER 4 /* Number of readers we support concurrently. */
@@ -158,14 +158,14 @@ static char (* DLSTDCALL CT_data) (unsigned short ctn, unsigned char *dad,
static char (* DLSTDCALL CT_close) (unsigned short ctn);
/* PC/SC constants and function pointer. */
-#define PCSC_SCOPE_USER 0
-#define PCSC_SCOPE_TERMINAL 1
-#define PCSC_SCOPE_SYSTEM 2
-#define PCSC_SCOPE_GLOBAL 3
+#define PCSC_SCOPE_USER 0
+#define PCSC_SCOPE_TERMINAL 1
+#define PCSC_SCOPE_SYSTEM 2
+#define PCSC_SCOPE_GLOBAL 3
-#define PCSC_PROTOCOL_T0 1
-#define PCSC_PROTOCOL_T1 2
-#define PCSC_PROTOCOL_RAW 4
+#define PCSC_PROTOCOL_T0 1
+#define PCSC_PROTOCOL_T1 2
+#define PCSC_PROTOCOL_RAW 4
#define PCSC_SHARE_EXCLUSIVE 1
#define PCSC_SHARE_SHARED 2
@@ -176,7 +176,7 @@ static char (* DLSTDCALL CT_close) (unsigned short ctn);
#define PCSC_UNPOWER_CARD 2
#define PCSC_EJECT_CARD 3
-#define PCSC_UNKNOWN 0x0001
+#define PCSC_UNKNOWN 0x0001
#define PCSC_ABSENT 0x0002 /* Card is absent. */
#define PCSC_PRESENT 0x0004 /* Card is present. */
#define PCSC_SWALLOWED 0x0008 /* Card is present and electrical connected. */
@@ -196,10 +196,32 @@ static char (* DLSTDCALL CT_close) (unsigned short ctn);
#define PCSC_STATE_INUSE 0x0100 /* Shared mode. */
#define PCSC_STATE_MUTE 0x0200 /* Unresponsive card. */
-
-struct pcsc_io_request_s
+/* Some PC/SC error codes. */
+#define PCSC_E_CANCELLED 0x80100002
+#define PCSC_E_CANT_DISPOSE 0x8010000E
+#define PCSC_E_INSUFFICIENT_BUFFER 0x80100008
+#define PCSC_E_INVALID_ATR 0x80100015
+#define PCSC_E_INVALID_HANDLE 0x80100003
+#define PCSC_E_INVALID_PARAMETER 0x80100004
+#define PCSC_E_INVALID_TARGET 0x80100005
+#define PCSC_E_INVALID_VALUE 0x80100011
+#define PCSC_E_NO_MEMORY 0x80100006
+#define PCSC_E_UNKNOWN_READER 0x80100009
+#define PCSC_E_TIMEOUT 0x8010000A
+#define PCSC_E_SHARING_VIOLATION 0x8010000B
+#define PCSC_E_NO_SMARTCARD 0x8010000C
+#define PCSC_E_UNKNOWN_CARD 0x8010000D
+#define PCSC_E_PROTO_MISMATCH 0x8010000F
+#define PCSC_E_NOT_READY 0x80100010
+#define PCSC_E_SYSTEM_CANCELLED 0x80100012
+#define PCSC_E_NOT_TRANSACTED 0x80100016
+#define PCSC_E_READER_UNAVAILABLE 0x80100017
+#define PCSC_W_REMOVED_CARD 0x80100069
+
+
+struct pcsc_io_request_s
{
- unsigned long protocol;
+ unsigned long protocol;
unsigned long pci_len;
};
@@ -262,15 +284,15 @@ long (* DLSTDCALL pcsc_set_timeout) (unsigned long context,
-/*
+/*
Helper
*/
-
+
/* Find an unused reader slot for PORTSTR and put it into the reader
table. Return -1 on error or the index into the reader table. */
-static int
-new_reader_slot (void)
+static int
+new_reader_slot (void)
{
int i, reader = -1;
@@ -387,8 +409,8 @@ apdu_strerror (int rc)
-/*
- ct API Interface
+/*
+ ct API Interface
*/
static const char *
@@ -425,9 +447,9 @@ ct_activate_card (int slot)
int rc;
unsigned char dad[1], sad[1], cmd[11], buf[256];
unsigned short buflen;
-
+
/* Check whether card has been inserted. */
- dad[0] = 1; /* Destination address: CT. */
+ dad[0] = 1; /* Destination address: CT. */
sad[0] = 2; /* Source address: Host. */
cmd[0] = 0x20; /* Class byte. */
@@ -446,8 +468,8 @@ ct_activate_card (int slot)
return SW_HOST_CARD_IO_ERROR;
}
- /* Connected, now activate the card. */
- dad[0] = 1; /* Destination address: CT. */
+ /* Connected, now activate the card. */
+ dad[0] = 1; /* Destination address: CT. */
sad[0] = 2; /* Source address: Host. */
cmd[0] = 0x20; /* Class byte. */
@@ -517,13 +539,13 @@ ct_send_apdu (int slot, unsigned char *apdu, size_t apdulen,
int rc;
unsigned char dad[1], sad[1];
unsigned short ctbuflen;
-
+
/* If we don't have an ATR, we need to reset the reader first. */
if (!reader_table[slot].atrlen
&& (rc = reset_ct_reader (slot)))
return rc;
- dad[0] = 0; /* Destination address: Card. */
+ dad[0] = 0; /* Destination address: Card. */
sad[0] = 2; /* Source address: Host. */
ctbuflen = *buflen;
if (DBG_CARD_IO)
@@ -582,6 +604,10 @@ open_ct_reader (int port)
}
+/*
+ PC/SC Interface
+ */
+
#ifdef NEED_PCSC_WRAPPER
static int
writen (int fd, const void *buf, size_t nbytes)
@@ -624,7 +650,7 @@ readn (int fd, void *buf, size_t buflen, size_t *nread)
#else
n = read (fd, buf, nleft);
#endif
- if (n < 0 && errno == EINTR)
+ if (n < 0 && errno == EINTR)
continue;
if (n < 0)
return -1; /* read error. */
@@ -637,7 +663,7 @@ readn (int fd, void *buf, size_t buflen, size_t *nread)
*nread = buflen - nleft;
/* log_printhex (" readn:", orig_buf, *nread); */
-
+
return 0;
}
#endif /*NEED_PCSC_WRAPPER*/
@@ -656,48 +682,73 @@ pcsc_error_string (long err)
{
case 0x0002: s = "cancelled"; break;
case 0x000e: s = "can't dispose"; break;
- case 0x0008: s = "insufficient buffer"; break;
+ case 0x0008: s = "insufficient buffer"; break;
case 0x0015: s = "invalid ATR"; break;
case 0x0003: s = "invalid handle"; break;
- case 0x0004: s = "invalid parameter"; break;
+ case 0x0004: s = "invalid parameter"; break;
case 0x0005: s = "invalid target"; break;
- case 0x0011: s = "invalid value"; break;
- case 0x0006: s = "no memory"; break;
- case 0x0013: s = "comm error"; break;
- case 0x0001: s = "internal error"; break;
- case 0x0014: s = "unknown error"; break;
- case 0x0007: s = "waited too long"; break;
+ case 0x0011: s = "invalid value"; break;
+ case 0x0006: s = "no memory"; break;
+ case 0x0013: s = "comm error"; break;
+ case 0x0001: s = "internal error"; break;
+ case 0x0014: s = "unknown error"; break;
+ case 0x0007: s = "waited too long"; break;
case 0x0009: s = "unknown reader"; break;
- case 0x000a: s = "timeout"; break;
- case 0x000b: s = "sharing violation"; break;
+ case 0x000a: s = "timeout"; break;
+ case 0x000b: s = "sharing violation"; break;
case 0x000c: s = "no smartcard"; break;
- case 0x000d: s = "unknown card"; break;
- case 0x000f: s = "proto mismatch"; break;
- case 0x0010: s = "not ready"; break;
- case 0x0012: s = "system cancelled"; break;
+ case 0x000d: s = "unknown card"; break;
+ case 0x000f: s = "proto mismatch"; break;
+ case 0x0010: s = "not ready"; break;
+ case 0x0012: s = "system cancelled"; break;
case 0x0016: s = "not transacted"; break;
- case 0x0017: s = "reader unavailable"; break;
- case 0x0065: s = "unsupported card"; break;
- case 0x0066: s = "unresponsive card"; break;
- case 0x0067: s = "unpowered card"; break;
- case 0x0068: s = "reset card"; break;
- case 0x0069: s = "removed card"; break;
- case 0x006a: s = "inserted card"; break;
- case 0x001f: s = "unsupported feature"; break;
- case 0x0019: s = "PCI too small"; break;
- case 0x001a: s = "reader unsupported"; break;
- case 0x001b: s = "duplicate reader"; break;
- case 0x001c: s = "card unsupported"; break;
- case 0x001d: s = "no service"; break;
- case 0x001e: s = "service stopped"; break;
+ case 0x0017: s = "reader unavailable"; break;
+ case 0x0065: s = "unsupported card"; break;
+ case 0x0066: s = "unresponsive card"; break;
+ case 0x0067: s = "unpowered card"; break;
+ case 0x0068: s = "reset card"; break;
+ case 0x0069: s = "removed card"; break;
+ case 0x006a: s = "inserted card"; break;
+ case 0x001f: s = "unsupported feature"; break;
+ case 0x0019: s = "PCI too small"; break;
+ case 0x001a: s = "reader unsupported"; break;
+ case 0x001b: s = "duplicate reader"; break;
+ case 0x001c: s = "card unsupported"; break;
+ case 0x001d: s = "no service"; break;
+ case 0x001e: s = "service stopped"; break;
default: s = "unknown PC/SC error code"; break;
}
return s;
}
-/*
- PC/SC Interface
- */
+/* Map PC/SC error codes to our special host status words. */
+static int
+pcsc_error_to_sw (long ec)
+{
+ int rc;
+
+ switch (ec)
+ {
+ case 0: rc = 0; break;
+
+ case PCSC_E_CANCELLED: rc = SW_HOST_ABORTED; break;
+ case PCSC_E_NO_MEMORY: rc = SW_HOST_OUT_OF_CORE; break;
+ case PCSC_E_TIMEOUT: rc = SW_HOST_CARD_IO_ERROR; break;
+ case PCSC_E_SHARING_VIOLATION: rc = SW_HOST_LOCKING_FAILED; break;
+ case PCSC_E_NO_SMARTCARD: rc = SW_HOST_NO_CARD; break;
+ case PCSC_W_REMOVED_CARD: rc = SW_HOST_NO_CARD; break;
+
+ case PCSC_E_INVALID_TARGET:
+ case PCSC_E_INVALID_VALUE:
+ case PCSC_E_INVALID_HANDLE:
+ case PCSC_E_INVALID_PARAMETER:
+ case PCSC_E_INSUFFICIENT_BUFFER: rc = SW_HOST_INV_VALUE; break;
+
+ default: rc = SW_HOST_GENERAL_ERROR; break;
+ }
+
+ return rc;
+}
static void
dump_pcsc_reader_status (int slot)
@@ -713,6 +764,8 @@ dump_pcsc_reader_status (int slot)
}
+/* Send an PC/SC reset command and return a status word on error or 0
+ on success. */
static int
reset_pcsc_reader (int slot)
{
@@ -722,15 +775,16 @@ reset_pcsc_reader (int slot)
size_t len;
int i, n;
unsigned char msgbuf[9];
+ int sw = SW_HOST_CARD_IO_ERROR;
slotp = reader_table + slot;
- if (slotp->pcsc.req_fd == -1
- || slotp->pcsc.rsp_fd == -1
+ if (slotp->pcsc.req_fd == -1
+ || slotp->pcsc.rsp_fd == -1
|| slotp->pcsc.pid == (pid_t)(-1) )
{
log_error ("pcsc_get_status: pcsc-wrapper not running\n");
- return SW_HOST_CARD_IO_ERROR;
+ return sw;
}
msgbuf[0] = 0x05; /* RESET command. */
@@ -763,16 +817,23 @@ reset_pcsc_reader (int slot)
if (len > DIM (slotp->atr))
{
log_error ("PC/SC returned a too large ATR (len=%x)\n", len);
+ sw = SW_HOST_GENERAL_ERROR;
goto command_failed;
}
err = (msgbuf[5] << 24) | (msgbuf[6] << 16) | (msgbuf[7] << 8 ) | msgbuf[8];
if (err)
{
- log_error ("PC/SC RESET failed: %s\n", pcsc_error_string (err));
+ log_error ("PC/SC RESET failed: %s (0x%lx)\n",
+ pcsc_error_string (err), err);
+ /* If the error code is no smart card, we should not considere
+ this a major error and close the wrapper. */
+ sw = pcsc_error_to_sw (err);
+ if (err == PCSC_E_NO_SMARTCARD)
+ return sw;
goto command_failed;
}
- /* The open fucntion may return a zero for the ATR length to
+ /* The open function may return a zero for the ATR length to
indicate that no card is present. */
n = len;
if (n)
@@ -796,7 +857,7 @@ reset_pcsc_reader (int slot)
kill (slotp->pcsc.pid, SIGTERM);
slotp->pcsc.pid = (pid_t)(-1);
slotp->used = 0;
- return -1;
+ return sw;
#else /* !NEED_PCSC_WRAPPER */
long err;
@@ -827,10 +888,10 @@ reset_pcsc_reader (int slot)
log_error ("pcsc_connect failed: %s (0x%lx)\n",
pcsc_error_string (err), err);
reader_table[slot].pcsc.card = 0;
- return SW_HOST_CARD_IO_ERROR;
- }
+ return pcsc_error_to_sw (err);
+ }
+
-
atrlen = 33;
nreader = sizeof reader - 1;
err = pcsc_status (reader_table[slot].pcsc.card,
@@ -842,7 +903,7 @@ reset_pcsc_reader (int slot)
log_error ("pcsc_status failed: %s (0x%lx)\n",
pcsc_error_string (err), err);
reader_table[slot].atrlen = 0;
- return SW_HOST_CARD_IO_ERROR;
+ return pcsc_error_to_sw (err);
}
if (atrlen >= DIM (reader_table[0].atr))
log_bug ("ATR returned by pcsc_status is too large\n");
@@ -863,15 +924,16 @@ pcsc_get_status (int slot, unsigned int *status)
int i, n;
unsigned char msgbuf[9];
unsigned char buffer[12];
+ int sw = SW_HOST_CARD_IO_ERROR;
slotp = reader_table + slot;
- if (slotp->pcsc.req_fd == -1
- || slotp->pcsc.rsp_fd == -1
+ if (slotp->pcsc.req_fd == -1
+ || slotp->pcsc.rsp_fd == -1
|| slotp->pcsc.pid == (pid_t)(-1) )
{
log_error ("pcsc_get_status: pcsc-wrapper not running\n");
- return SW_HOST_CARD_IO_ERROR;
+ return sw;
}
msgbuf[0] = 0x04; /* STATUS command. */
@@ -906,11 +968,12 @@ pcsc_get_status (int slot, unsigned int *status)
{
log_error ("pcsc_status failed: %s (0x%lx)\n",
pcsc_error_string (err), err);
- return SW_HOST_CARD_IO_ERROR;
+ /* This is a proper error code, so return immediately. */
+ return pcsc_error_to_sw (err);
}
full_len = len;
-
+
n = 8 < len ? 8 : len;
if ((i=readn (slotp->pcsc.rsp_fd, buffer, n, &len)) || len != 8)
{
@@ -935,7 +998,7 @@ pcsc_get_status (int slot, unsigned int *status)
}
full_len -= n;
}
-
+
/* We are lucky: The wrapper already returns the data in the
required format. */
*status = buffer[3];
@@ -950,26 +1013,26 @@ pcsc_get_status (int slot, unsigned int *status)
kill (slotp->pcsc.pid, SIGTERM);
slotp->pcsc.pid = (pid_t)(-1);
slotp->used = 0;
- return -1;
+ return sw;
#else /*!NEED_PCSC_WRAPPER*/
long err;
struct pcsc_readerstate_s rdrstates[1];
-
+
memset (rdrstates, 0, sizeof *rdrstates);
rdrstates[0].reader = reader_table[slot].rdrname;
rdrstates[0].current_state = PCSC_STATE_UNAWARE;
err = pcsc_get_status_change (reader_table[slot].pcsc.context,
0,
rdrstates, 1);
- if (err == 0x8010000a) /* Timeout. */
- err = 0;
+ if (err == PCSC_E_TIMEOUT)
+ err = 0; /* Timeout is no error error here. */
if (err)
{
log_error ("pcsc_get_status_change failed: %s (0x%lx)\n",
pcsc_error_string (err), err);
- return SW_HOST_CARD_IO_ERROR;
+ return pcsc_error_to_sw (err);
}
@@ -997,8 +1060,8 @@ pcsc_get_status (int slot, unsigned int *status)
if ( (*status & 6) == 6
&& !(rdrstates[0].event_state & PCSC_STATE_INUSE) )
*status |= 1;
-
- return 0;
+
+ return 0;
#endif /*!NEED_PCSC_WRAPPER*/
}
@@ -1016,6 +1079,7 @@ pcsc_send_apdu (int slot, unsigned char *apdu, size_t apdulen,
size_t len, full_len;
int i, n;
unsigned char msgbuf[9];
+ int sw = SW_HOST_CARD_IO_ERROR;
if (!reader_table[slot].atrlen
&& (err = reset_pcsc_reader (slot)))
@@ -1026,12 +1090,12 @@ pcsc_send_apdu (int slot, unsigned char *apdu, size_t apdulen,
slotp = reader_table + slot;
- if (slotp->pcsc.req_fd == -1
- || slotp->pcsc.rsp_fd == -1
+ if (slotp->pcsc.req_fd == -1
+ || slotp->pcsc.rsp_fd == -1
|| slotp->pcsc.pid == (pid_t)(-1) )
{
log_error ("pcsc_send_apdu: pcsc-wrapper not running\n");
- return SW_HOST_CARD_IO_ERROR;
+ return sw;
}
msgbuf[0] = 0x03; /* TRANSMIT command. */
@@ -1067,11 +1131,11 @@ pcsc_send_apdu (int slot, unsigned char *apdu, size_t apdulen,
{
log_error ("pcsc_transmit failed: %s (0x%lx)\n",
pcsc_error_string (err), err);
- return SW_HOST_CARD_IO_ERROR;
+ return pcsc_error_to_sw (err);
}
full_len = len;
-
+
n = *buflen < len ? *buflen : len;
if ((i=readn (slotp->pcsc.rsp_fd, buffer, n, &len)) || len != n)
{
@@ -1113,14 +1177,14 @@ pcsc_send_apdu (int slot, unsigned char *apdu, size_t apdulen,
kill (slotp->pcsc.pid, SIGTERM);
slotp->pcsc.pid = (pid_t)(-1);
slotp->used = 0;
- return -1;
+ return sw;
#else /*!NEED_PCSC_WRAPPER*/
long err;
struct pcsc_io_request_s send_pci;
unsigned long recv_len;
-
+
if (!reader_table[slot].atrlen
&& (err = reset_pcsc_reader (slot)))
return err;
@@ -1141,8 +1205,8 @@ pcsc_send_apdu (int slot, unsigned char *apdu, size_t apdulen,
if (err)
log_error ("pcsc_transmit failed: %s (0x%lx)\n",
pcsc_error_string (err), err);
-
- return err? SW_HOST_CARD_IO_ERROR:0;
+
+ return pcsc_error_to_sw (err);
#endif /*!NEED_PCSC_WRAPPER*/
}
@@ -1159,8 +1223,8 @@ close_pcsc_reader (int slot)
slotp = reader_table + slot;
- if (slotp->pcsc.req_fd == -1
- || slotp->pcsc.rsp_fd == -1
+ if (slotp->pcsc.req_fd == -1
+ || slotp->pcsc.rsp_fd == -1
|| slotp->pcsc.pid == (pid_t)(-1) )
{
log_error ("close_pcsc_reader: pcsc-wrapper not running\n");
@@ -1198,10 +1262,10 @@ close_pcsc_reader (int slot)
if (err)
log_error ("pcsc_close failed: %s (0x%lx)\n",
pcsc_error_string (err), err);
-
- /* We will the wrapper in any case - errors are merely
+
+ /* We will close the wrapper in any case - errors are merely
informational. */
-
+
command_failed:
close (slotp->pcsc.req_fd);
close (slotp->pcsc.rsp_fd);
@@ -1222,6 +1286,7 @@ close_pcsc_reader (int slot)
#endif /*!NEED_PCSC_WRAPPER*/
}
+/* Note: It is a pitty that we can't return proper error codes. */
static int
open_pcsc_reader (const char *portstr)
{
@@ -1237,6 +1302,7 @@ open_pcsc_reader (const char *portstr)
size_t len;
unsigned char msgbuf[9];
int err;
+ int sw = SW_HOST_CARD_IO_ERROR;
slot = new_reader_slot ();
if (slot == -1)
@@ -1246,7 +1312,7 @@ open_pcsc_reader (const char *portstr)
/* Fire up the pcsc wrapper. We don't use any fork/exec code from
the common directy but implement it direclty so that this file
may still be source copied. */
-
+
if (pipe (rp) == -1)
{
log_error ("error creating a pipe: %s\n", strerror (errno));
@@ -1261,7 +1327,7 @@ open_pcsc_reader (const char *portstr)
slotp->used = 0;
return -1;
}
-
+
pid = fork ();
if (pid == -1)
{
@@ -1283,7 +1349,7 @@ open_pcsc_reader (const char *portstr)
/* Double fork. */
pid = fork ();
if (pid == -1)
- _exit (31);
+ _exit (31);
if (pid)
_exit (0); /* Immediate exit this parent, so that the child
gets cleaned up by the init process. */
@@ -1293,7 +1359,7 @@ open_pcsc_reader (const char *portstr)
log_fatal ("dup2 stdin failed: %s\n", strerror (errno));
if (rp[1] != 1 && dup2 (rp[1], 1) == -1)
log_fatal ("dup2 stdout failed: %s\n", strerror (errno));
-
+
/* Send stderr to the bit bucket. */
fd = open ("/dev/null", O_WRONLY);
if (fd == -1)
@@ -1318,7 +1384,7 @@ open_pcsc_reader (const char *portstr)
_exit (31);
}
- /*
+ /*
=== Parent ===
*/
close (wp[0]);
@@ -1328,9 +1394,9 @@ open_pcsc_reader (const char *portstr)
/* Wait for the intermediate child to terminate. */
#ifdef USE_GNU_PTH
-#define WAIT pth_waitpid
+#define WAIT pth_waitpid
#else
-#define WAIT waitpid
+#define WAIT waitpid
#endif
while ( (i=WAIT (pid, NULL, 0)) == -1 && errno == EINTR)
;
@@ -1373,6 +1439,7 @@ open_pcsc_reader (const char *portstr)
if (err)
{
log_error ("PC/SC OPEN failed: %s\n", pcsc_error_string (err));
+ sw = pcsc_error_to_sw (err);
goto command_failed;
}
@@ -1401,7 +1468,7 @@ open_pcsc_reader (const char *portstr)
reader_table[slot].send_apdu_reader = pcsc_send_apdu;
reader_table[slot].dump_status_reader = dump_pcsc_reader_status;
- dump_reader_status (slot);
+ dump_reader_status (slot);
return slot;
command_failed:
@@ -1412,7 +1479,9 @@ open_pcsc_reader (const char *portstr)
kill (slotp->pcsc.pid, SIGTERM);
slotp->pcsc.pid = (pid_t)(-1);
slotp->used = 0;
+ /* There is no way to return SW. */
return -1;
+
#else /*!NEED_PCSC_WRAPPER */
long err;
int slot;
@@ -1434,7 +1503,7 @@ open_pcsc_reader (const char *portstr)
reader_table[slot].used = 0;
return -1;
}
-
+
err = pcsc_list_readers (reader_table[slot].pcsc.context,
NULL, NULL, &nreader);
if (!err)
@@ -1445,7 +1514,7 @@ open_pcsc_reader (const char *portstr)
log_error ("error allocating memory for reader list\n");
pcsc_release_context (reader_table[slot].pcsc.context);
reader_table[slot].used = 0;
- return -1;
+ return -1 /*SW_HOST_OUT_OF_CORE*/;
}
err = pcsc_list_readers (reader_table[slot].pcsc.context,
NULL, list, &nreader);
@@ -1457,7 +1526,7 @@ open_pcsc_reader (const char *portstr)
pcsc_release_context (reader_table[slot].pcsc.context);
reader_table[slot].used = 0;
xfree (list);
- return -1;
+ return -1 /*pcsc_error_to_sw (err)*/;
}
listlen = nreader;
@@ -1483,7 +1552,7 @@ open_pcsc_reader (const char *portstr)
log_error ("error allocating memory for reader name\n");
pcsc_release_context (reader_table[slot].pcsc.context);
reader_table[slot].used = 0;
- return -1;
+ return -1 /*SW_HOST_OUT_OF_CORE*/;
}
strcpy (reader_table[slot].rdrname, portstr? portstr : list);
xfree (list);
@@ -1494,7 +1563,7 @@ open_pcsc_reader (const char *portstr)
PCSC_PROTOCOL_T0|PCSC_PROTOCOL_T1,
&reader_table[slot].pcsc.card,
&reader_table[slot].pcsc.protocol);
- if (err == 0x8010000c) /* No smartcard. */
+ if (err == PCSC_E_NO_SMARTCARD)
reader_table[slot].pcsc.card = 0;
else if (err)
{
@@ -1505,8 +1574,8 @@ open_pcsc_reader (const char *portstr)
reader_table[slot].rdrname = NULL;
reader_table[slot].used = 0;
xfree (list);
- return -1;
- }
+ return -1 /*pcsc_error_to_sw (err)*/;
+ }
reader_table[slot].atrlen = 0;
reader_table[slot].last_status = 0;
@@ -1544,7 +1613,7 @@ open_pcsc_reader (const char *portstr)
/* log_debug ("state from pcsc_status: 0x%lx\n", card_state); */
/* log_debug ("protocol from pcsc_status: 0x%lx\n", card_protocol); */
- dump_reader_status (slot);
+ dump_reader_status (slot);
return slot;
#endif /*!NEED_PCSC_WRAPPER */
}
@@ -1553,7 +1622,7 @@ open_pcsc_reader (const char *portstr)
#ifdef HAVE_LIBUSB
-/*
+/*
Internal CCID driver interface.
*/
@@ -1570,16 +1639,16 @@ close_ccid_reader (int slot)
ccid_close_reader (reader_table[slot].ccid.handle);
reader_table[slot].used = 0;
return 0;
-}
-
+}
+
static int
shutdown_ccid_reader (int slot)
{
ccid_shutdown_reader (reader_table[slot].ccid.handle);
return 0;
-}
-
+}
+
static int
reset_ccid_reader (int slot)
@@ -1596,10 +1665,10 @@ reset_ccid_reader (int slot)
assert (sizeof slotp->atr >= sizeof atr);
slotp->atrlen = atrlen;
memcpy (slotp->atr, atr, atrlen);
- dump_reader_status (slot);
+ dump_reader_status (slot);
return 0;
-}
-
+}
+
static int
get_status_ccid (int slot, unsigned int *status)
@@ -1615,7 +1684,7 @@ get_status_ccid (int slot, unsigned int *status)
*status = 1|2|4;
else if (bits == 1)
*status = 2;
- else
+ else
*status = 0;
return 0;
@@ -1647,8 +1716,8 @@ send_apdu_ccid (int slot, unsigned char *apdu, size_t apdulen,
if (err)
log_error ("ccid_transceive failed: (0x%lx)\n",
err);
-
- return err;
+
+ return err;
}
/* Open the reader and try to read an ATR. */
@@ -1692,7 +1761,7 @@ open_ccid_reader (const char *portstr)
reader_table[slot].send_apdu_reader = send_apdu_ccid;
reader_table[slot].dump_status_reader = dump_ccid_reader_status;
- dump_reader_status (slot);
+ dump_reader_status (slot);
return slot;
}
@@ -1703,7 +1772,7 @@ open_ccid_reader (const char *portstr)
#ifdef HAVE_OPENSC
-/*
+/*
OpenSC Interface.
This uses the OpenSC primitives to send APDUs. We need this
@@ -1764,7 +1833,7 @@ osc_send_apdu (int slot, unsigned char *apdu, size_t apdulen,
if (!apdulen)
a.cse = SC_APDU_CASE_1;
- else if (apdulen == 1)
+ else if (apdulen == 1)
{
a.le = *apdu? *apdu : 256;
apdu++; apdulen--;
@@ -1784,7 +1853,7 @@ osc_send_apdu (int slot, unsigned char *apdu, size_t apdulen,
a.data = data;
a.datalen = a.lc;
-
+
if (!apdulen)
a.cse = SC_APDU_CASE_3_SHORT;
else
@@ -1861,7 +1930,7 @@ open_osc_reader (int portno)
slotp->used = 0;
return -1;
}
-
+
/* We want the standard ISO driver. */
/*FIXME: OpenSC does not like "iso7816", so we use EMV for now. */
err = sc_set_card_driver(slotp->osc.ctx, "emv");
@@ -1912,7 +1981,7 @@ open_osc_reader (int portno)
reader_table[slot].send_apdu_reader = osc_send_apdu;
reader_table[slot].dump_status_reader = NULL;
- dump_reader_status (slot);
+ dump_reader_status (slot);
return slot;
}
@@ -1921,7 +1990,7 @@ open_osc_reader (int portno)
#ifdef USE_G10CODE_RAPDU
-/*
+/*
The Remote APDU Interface.
This uses the Remote APDU protocol to contact a reader.
@@ -1940,9 +2009,9 @@ rapdu_status_to_sw (int status)
{
case RAPDU_STATUS_SUCCESS: rc = 0; break;
- case RAPDU_STATUS_INVCMD:
- case RAPDU_STATUS_INVPROT:
- case RAPDU_STATUS_INVSEQ:
+ case RAPDU_STATUS_INVCMD:
+ case RAPDU_STATUS_INVPROT:
+ case RAPDU_STATUS_INVSEQ:
case RAPDU_STATUS_INVCOOKIE:
case RAPDU_STATUS_INVREADER: rc = SW_HOST_INV_VALUE; break;
@@ -2007,7 +2076,7 @@ reset_rapdu_reader (int slot)
{
log_error ("ATR returned by the RAPDU layer is too large\n");
rapdu_msg_release (msg);
- return SW_HOST_INV_VALUE;
+ return SW_HOST_INV_VALUE;
}
slotp->atrlen = msg->datalen;
memcpy (slotp->atr, msg->data, msg->datalen);
@@ -2107,12 +2176,12 @@ my_rapdu_send_apdu (int slot, unsigned char *apdu, size_t apdulen,
rapdu_msg_release (msg);
return sw;
}
-
+
if (msg->datalen > maxlen)
{
log_error ("rapdu response apdu too large\n");
rapdu_msg_release (msg);
- return SW_HOST_INV_VALUE;
+ return SW_HOST_INV_VALUE;
}
*buflen = msg->datalen;
@@ -2196,11 +2265,11 @@ open_rapdu_reader (int portno,
reader_table[slot].send_apdu_reader = my_rapdu_send_apdu;
reader_table[slot].dump_status_reader = NULL;
- dump_reader_status (slot);
+ dump_reader_status (slot);
rapdu_msg_release (msg);
return slot;
- failure:
+ failure:
rapdu_msg_release (msg);
rapdu_release (slotp->rapdu.handle);
slotp->used = 0;
@@ -2211,7 +2280,7 @@ open_rapdu_reader (int portno,
-/*
+/*
Driver Access
*/
@@ -2290,7 +2359,7 @@ apdu_open_reader (const char *portstr)
return open_osc_reader (port);
}
-#endif /* HAVE_OPENSC */
+#endif /* HAVE_OPENSC */
if (opt.ctapi_driver && *opt.ctapi_driver)
@@ -2300,7 +2369,7 @@ apdu_open_reader (const char *portstr)
if (!ct_api_loaded)
{
void *handle;
-
+
handle = dlopen (opt.ctapi_driver, RTLD_LAZY);
if (!handle)
{
@@ -2322,7 +2391,7 @@ apdu_open_reader (const char *portstr)
return open_ct_reader (port);
}
-
+
/* No ctAPI configured, so lets try the PC/SC API */
if (!pcsc_api_loaded)
{
@@ -2371,16 +2440,16 @@ apdu_open_reader (const char *portstr)
pcsc_set_timeout = dlsym (handle, "SCardSetTimeout");
if (!pcsc_establish_context
- || !pcsc_release_context
- || !pcsc_list_readers
+ || !pcsc_release_context
+ || !pcsc_list_readers
|| !pcsc_get_status_change
- || !pcsc_connect
+ || !pcsc_connect
|| !pcsc_reconnect
|| !pcsc_disconnect
|| !pcsc_status
|| !pcsc_begin_transaction
|| !pcsc_end_transaction
- || !pcsc_transmit
+ || !pcsc_transmit
/* || !pcsc_set_timeout */)
{
/* Note that set_timeout is currently not used and also not
@@ -2388,21 +2457,21 @@ apdu_open_reader (const char *portstr)
log_error ("apdu_open_reader: invalid PC/SC driver "
"(%d%d%d%d%d%d%d%d%d%d%d%d)\n",
!!pcsc_establish_context,
- !!pcsc_release_context,
- !!pcsc_list_readers,
- !!pcsc_get_status_change,
- !!pcsc_connect,
- !!pcsc_reconnect,
+ !!pcsc_release_context,
+ !!pcsc_list_readers,
+ !!pcsc_get_status_change,
+ !!pcsc_connect,
+ !!pcsc_reconnect,
!!pcsc_disconnect,
!!pcsc_status,
!!pcsc_begin_transaction,
!!pcsc_end_transaction,
- !!pcsc_transmit,
+ !!pcsc_transmit,
!!pcsc_set_timeout );
dlclose (handle);
return -1;
}
-#endif /*!NEED_PCSC_WRAPPER*/
+#endif /*!NEED_PCSC_WRAPPER*/
pcsc_api_loaded = 1;
}
@@ -2416,7 +2485,7 @@ apdu_open_reader (const char *portstr)
only be called once and the slot will not be valid afther this.
If PORTSTR is NULL we default to the first availabe port.
-*/
+*/
int
apdu_open_remote_reader (const char *portstr,
const unsigned char *cookie, size_t length,
@@ -2436,7 +2505,7 @@ apdu_open_remote_reader (const char *portstr,
writefnc, writefnc_value,
closefnc, closefnc_value);
#else
-#ifdef _WIN32
+#ifdef _WIN32
errno = ENOENT;
#else
errno = ENOSYS;
@@ -2489,7 +2558,7 @@ apdu_reset (int slot)
if (slot < 0 || slot >= MAX_READER || !reader_table[slot].used )
return SW_HOST_NO_DRIVER;
-
+
if ((sw = lock_slot (slot)))
return sw;
@@ -2521,7 +2590,7 @@ apdu_activate (int slot)
if (slot < 0 || slot >= MAX_READER || !reader_table[slot].used )
return SW_HOST_NO_DRIVER;
-
+
if ((sw = trylock_slot (slot)))
return sw;
@@ -2550,12 +2619,12 @@ apdu_activate (int slot)
}
}
}
-
+
unlock_slot (slot);
return sw;
}
-
+
unsigned char *
apdu_get_atr (int slot, size_t *atrlen)
@@ -2564,7 +2633,7 @@ apdu_get_atr (int slot, size_t *atrlen)
if (slot < 0 || slot >= MAX_READER || !reader_table[slot].used )
return NULL;
-
+
buf = xtrymalloc (reader_table[slot].atrlen);
if (!buf)
return NULL;
@@ -2574,7 +2643,7 @@ apdu_get_atr (int slot, size_t *atrlen)
}
-
+
/* Retrieve the status for SLOT. The function does only wait for the
card to become available if HANG is set to true. On success the
bits in STATUS will be set to
@@ -2660,7 +2729,7 @@ send_apdu (int slot, unsigned char *apdu, size_t apdulen,
returned data. The length of that data will be put into
*RETBUFLEN. The caller is reponsible for releasing the buffer even
in case of errors. */
-int
+int
apdu_send_le(int slot, int class, int ins, int p0, int p1,
int lc, const char *data, int le,
unsigned char **retbuf, size_t *retbuflen)
@@ -2682,9 +2751,9 @@ apdu_send_le(int slot, int class, int ins, int p0, int p1,
class, ins, p0, p1, lc, le);
if (lc != -1 && (lc > 255 || lc < 0))
- return SW_WRONG_LENGTH;
+ return SW_WRONG_LENGTH;
if (le != -1 && (le > 256 || le < 1))
- return SW_WRONG_LENGTH;
+ return SW_WRONG_LENGTH;
if ((!data && lc != -1) || (data && lc == -1))
return SW_HOST_INV_VALUE;
@@ -2764,7 +2833,7 @@ apdu_send_le(int slot, int class, int ins, int p0, int p1,
do
{
int len = (sw & 0x00ff);
-
+
if (DBG_CARD_IO)
log_debug ("apdu_send_simple(%d): %d more bytes available\n",
slot, len);
@@ -2773,7 +2842,7 @@ apdu_send_le(int slot, int class, int ins, int p0, int p1,
apdu[apdulen++] = 0xC0;
apdu[apdulen++] = 0;
apdu[apdulen++] = 0;
- apdu[apdulen++] = len;
+ apdu[apdulen++] = len;
memset (apdu+apdulen, 0, sizeof (apdu) - apdulen);
resultlen = RESULTLEN;
rc = send_apdu (slot, apdu, apdulen, result, &resultlen);
@@ -2822,7 +2891,7 @@ apdu_send_le(int slot, int class, int ins, int p0, int p1,
slot, sw);
}
while ((sw & 0xff00) == SW_MORE_DATA);
-
+
if (retbuf)
{
*retbuflen = p - *retbuf;
@@ -2836,7 +2905,7 @@ apdu_send_le(int slot, int class, int ins, int p0, int p1,
if (DBG_CARD_IO && retbuf && sw == SW_SUCCESS)
log_printhex (" dump: ", *retbuf, *retbuflen);
-
+
return sw;
#undef RESULTLEN
}
@@ -2850,11 +2919,11 @@ apdu_send_le(int slot, int class, int ins, int p0, int p1,
data. The length of that data will be put into *RETBUFLEN. The
caller is reponsible for releasing the buffer even in case of
errors. */
-int
+int
apdu_send (int slot, int class, int ins, int p0, int p1,
int lc, const char *data, unsigned char **retbuf, size_t *retbuflen)
{
- return apdu_send_le (slot, class, ins, p0, p1, lc, data, 256,
+ return apdu_send_le (slot, class, ins, p0, p1, lc, data, 256,
retbuf, retbuflen);
}
@@ -2864,7 +2933,7 @@ apdu_send (int slot, int class, int ins, int p0, int p1,
also be passed as NULL. The return value is the status word or -1
for an invalid SLOT or other non card related error. No data will be
returned. */
-int
+int
apdu_send_simple (int slot, int class, int ins, int p0, int p1,
int lc, const char *data)
{
@@ -2880,7 +2949,7 @@ apdu_send_simple (int slot, int class, int ins, int p0, int p1,
the end. The function does not return a regular status word but 0
on success. If the slot is locked, the fucntion returns
immediately.*/
-int
+int
apdu_send_direct (int slot, const unsigned char *apdudata, size_t apdudatalen,
int handle_more,
unsigned char **retbuf, size_t *retbuflen)
@@ -2951,7 +3020,7 @@ apdu_send_direct (int slot, const unsigned char *apdudata, size_t apdudatalen,
do
{
int len = (sw & 0x00ff);
-
+
if (DBG_CARD_IO)
log_debug ("apdu_send_direct(%d): %d more bytes available\n",
slot, len);
@@ -2960,7 +3029,7 @@ apdu_send_direct (int slot, const unsigned char *apdudata, size_t apdudatalen,
apdu[apdulen++] = 0xC0;
apdu[apdulen++] = 0;
apdu[apdulen++] = 0;
- apdu[apdulen++] = len;
+ apdu[apdulen++] = len;
memset (apdu+apdulen, 0, sizeof (apdu) - apdulen);
resultlen = RESULTLEN;
rc = send_apdu (slot, apdu, apdulen, result, &resultlen);
@@ -3009,7 +3078,7 @@ apdu_send_direct (int slot, const unsigned char *apdudata, size_t apdudatalen,
slot, sw);
}
while ((sw & 0xff00) == SW_MORE_DATA);
-
+
if (retbuf)
{
*retbuflen = p - *retbuf;
@@ -3045,9 +3114,7 @@ apdu_send_direct (int slot, const unsigned char *apdudata, size_t apdudatalen,
if (DBG_CARD_IO && retbuf)
log_printhex (" dump: ", *retbuf, *retbuflen);
-
+
return 0;
#undef RESULTLEN
}
-
-