158
153
static char (* DLSTDCALL CT_close) (unsigned short ctn);
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
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
170
165
#define PCSC_SHARE_EXCLUSIVE 1
171
166
#define PCSC_SHARE_SHARED 2
196
191
#define PCSC_STATE_INUSE 0x0100 /* Shared mode. */
197
192
#define PCSC_STATE_MUTE 0x0200 /* Unresponsive card. */
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
217
struct pcsc_io_request_s
202
unsigned long protocol;
219
unsigned long protocol;
203
220
unsigned long pci_len;
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;
723
/* Map PC/SC error codes to our special host status words. */
725
pcsc_error_to_sw (long ec)
731
case 0: rc = 0; break;
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;
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;
746
default: rc = SW_HOST_GENERAL_ERROR; break;
703
753
dump_pcsc_reader_status (int slot)
762
816
len -= 4; /* Already read the error code. */
763
817
if (len > DIM (slotp->atr))
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",
821
sw = SW_HOST_GENERAL_ERROR;
766
822
goto command_failed;
768
824
err = (msgbuf[5] << 24) | (msgbuf[6] << 16) | (msgbuf[7] << 8 ) | msgbuf[8];
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)
772
834
goto command_failed;
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. */
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);
914
n = 8 < len ? 8 : len;
915
if ((i=readn (slotp->pcsc.rsp_fd, buffer, n, &len)) || len != 8)
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))
917
988
log_error ("error receiving PC/SC STATUS response: %s\n",
918
989
i? strerror (errno) : "premature EOF");
919
990
goto command_failed;
993
slotp->is_t0 = (len == 12 && !!(buffer[11] & PCSC_PROTOCOL_T0));
923
997
/* Newer versions of the wrapper might send more status bytes.
950
1024
kill (slotp->pcsc.pid, SIGTERM);
951
1025
slotp->pcsc.pid = (pid_t)(-1);
952
1026
slotp->used = 0;
955
1029
#else /*!NEED_PCSC_WRAPPER*/
958
1032
struct pcsc_readerstate_s rdrstates[1];
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,
966
if (err == 0x8010000a) /* Timeout. */
1040
if (err == PCSC_E_TIMEOUT)
1041
err = 0; /* Timeout is no error error here. */
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);
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.
1716
close_osc_reader (int slot)
1718
/* FIXME: Implement. */
1719
reader_table[slot].used = 0;
1724
reset_osc_reader (int slot)
1726
return SW_HOST_NOT_SUPPORTED;
1731
osc_get_status (int slot, unsigned int *status)
1733
return SW_HOST_NOT_SUPPORTED;
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. */
1741
osc_send_apdu (int slot, unsigned char *apdu, size_t apdulen,
1742
unsigned char *buffer, size_t *buflen)
1746
unsigned char data[SC_MAX_APDU_BUFFER_SIZE];
1747
unsigned char result[SC_MAX_APDU_BUFFER_SIZE];
1750
log_printhex (" APDU_data:", apdu, apdulen);
1754
log_error ("osc_send_apdu: APDU is too short\n");
1755
return SW_HOST_INV_VALUE;
1758
memset(&a, 0, sizeof a);
1766
a.cse = SC_APDU_CASE_1;
1767
else if (apdulen == 1)
1769
a.le = *apdu? *apdu : 256;
1771
a.cse = SC_APDU_CASE_2_SHORT;
1775
a.lc = *apdu++; apdulen--;
1778
log_error ("osc_send_apdu: APDU shorter than specified in Lc\n");
1779
return SW_HOST_INV_VALUE;
1782
memcpy(data, apdu, a.lc);
1783
apdu += a.lc; apdulen -= a.lc;
1789
a.cse = SC_APDU_CASE_3_SHORT;
1792
a.le = *apdu? *apdu : 256;
1796
log_error ("osc_send_apdu: APDU larger than specified\n");
1797
return SW_HOST_INV_VALUE;
1799
a.cse = SC_APDU_CASE_4_SHORT;
1804
a.resplen = DIM(result);
1806
err = sc_transmit_apdu (reader_table[slot].osc.scard, &a);
1809
log_error ("sc_apdu_transmit failed: %s\n", sc_strerror (err));
1810
return SW_HOST_CARD_IO_ERROR;
1813
if (*buflen < 2 || a.resplen > *buflen - 2)
1815
log_error ("osc_send_apdu: provided buffer too short to store result\n");
1816
return SW_HOST_INV_VALUE;
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;
1826
open_osc_reader (int portno)
1830
reader_table_t slotp;
1832
slot = new_reader_slot ();
1835
slotp = reader_table + slot;
1837
err = sc_establish_context (&slotp->osc.ctx, "scdaemon");
1840
log_error ("failed to establish SC context: %s\n", sc_strerror (err));
1844
if (portno < 0 || portno >= slotp->osc.ctx->reader_count)
1846
log_error ("no card reader available\n");
1847
sc_release_context (slotp->osc.ctx);
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 ();
1857
if (sc_detect_card_presence (slotp->osc.ctx->reader[portno], 0) != 1)
1859
log_error ("no card present\n");
1860
sc_release_context (slotp->osc.ctx);
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");
1870
log_error ("failed to select the iso7816 driver: %s\n",
1872
sc_release_context (slotp->osc.ctx);
1877
/* Now connect the card and hope that OpenSC won't try to be too
1879
err = sc_connect_card (slotp->osc.ctx->reader[portno], 0,
1883
log_error ("failed to connect card in reader %d: %s\n",
1884
portno, sc_strerror (err));
1885
sc_release_context (slotp->osc.ctx);
1890
log_info ("connected to card in opensc reader %d using driver `%s'\n",
1891
portno, slotp->osc.scard->driver->name);
1893
err = sc_lock (slotp->osc.scard);
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);
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);
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;
1915
dump_reader_status (slot);
1919
#endif /* HAVE_OPENSC */
1923
1791
#ifdef USE_G10CODE_RAPDU
1925
1793
The Remote APDU Interface.
1927
1795
This uses the Remote APDU protocol to contact a reader.
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
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)
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);