2 Copyright (c) 2013 Eugene Crosser
4 This software is provided 'as-is', without any express or implied
5 warranty. In no event will the authors be held liable for any damages
6 arising from the use of this software.
8 Permission is granted to anyone to use this software for any purpose,
9 including commercial applications, and to alter it and redistribute it
10 freely, subject to the following restrictions:
12 1. The origin of this software must not be misrepresented; you must
13 not claim that you wrote the original software. If you use this
14 software in a product, an acknowledgment in the product documentation
15 would be appreciated but is not required.
17 2. Altered source versions must be plainly marked as such, and must
18 not be misrepresented as being the original software.
20 3. This notice may not be removed or altered from any source
33 extern struct token_interface ykneo_interface;
35 static struct token_interface *types[] = {
40 SCARD_IO_REQUEST pioSendPci;
42 static LONG find_hb(BYTE *atr, DWORD atrsize, BYTE **hb, LPDWORD hbsize)
46 if (atrsize < 2) return SCARD_W_UNSUPPORTED_CARD;
50 default: return SCARD_W_UNSUPPORTED_CARD;
52 *hbsize = atr[1]&0x0f;
56 for (j = 0; j < 4; j++) if (cont & (0x01 << j)) i++;
57 } while ((cont & 0x08) && (i < atrsize));
58 if ((i + (*hbsize) + 2) != atrsize)
59 return SCARD_W_UNSUPPORTED_CARD;
61 return SCARD_S_SUCCESS;
64 long pcsc_cr(const unsigned char *chal, const int csize,
65 unsigned char *resp, int *rsize)
67 struct token_interface *type;
69 SCARDCONTEXT hContext;
72 DWORD nrdrs = SCARD_AUTOALLOCATE, activeproto;
80 rc = SCardEstablishContext(SCARD_SCOPE_SYSTEM, NULL, NULL, &hContext);
82 rc = SCardListReaders(hContext, NULL, (LPTSTR)&readers, &nrdrs);
84 for (rdr=readers; rdr < readers + nrdrs - 1; rdr += strlen(rdr) + 1) {
85 rc = SCardConnect(hContext, rdr, SCARD_SHARE_SHARED,
86 SCARD_PROTOCOL_T0 | SCARD_PROTOCOL_T1,
87 &hCard, &activeproto);
89 switch (activeproto) {
90 case SCARD_PROTOCOL_T0:
91 pioSendPci = *SCARD_PCI_T0;
93 case SCARD_PROTOCOL_T1:
94 pioSendPci = *SCARD_PCI_T1;
97 atrsize = sizeof(atr);
98 rc = SCardGetAttrib(hCard, SCARD_ATTR_ATR_STRING,
100 if (rc) goto disconnect;
101 rc = find_hb(atr, atrsize, &hb, &hbsize);
102 if (rc) goto disconnect;
103 for (i = 0; types[i]; i++) {
105 rc = type->check_atr_hb(hb, hbsize);
108 if (rc) goto disconnect;
109 rc = type->prologue(hCard);
112 (void)SCardDisconnect(hCard, SCARD_LEAVE_CARD);
114 if (rc) goto free_out;
117 memset(serial, 'z', sizeof(serial));
118 serial[sizeof(serial) - 1] - '\0';
119 lrsize = sizeof(serial);
120 rc = type->getserial(hCard, &serial, &lrsize);
121 if (rc) goto disc_free_out;
122 printf("Serial is %s\n", serial);
125 rc = type->trancieve(hCard, (BYTE*)chal, csize, resp, &lrsize);
126 if (rc) goto disc_free_out;
128 rc = type->epilogue(hCard);
130 (void)SCardDisconnect(hCard, SCARD_EJECT_CARD);
132 (void)SCardFreeMemory(hContext, readers);
136 const char *pcsc_errstr(long err) {
137 return pcsc_stringify_error(err);
140 int pcsc_option(const char *option)
142 char *name, *key, *val;
144 struct token_interface *type;
146 name=(char *)alloca(strlen(option)+1);
147 strcpy(name, option);
148 if ((key = strchr(name, ':'))) *(key++) = '\0';
150 if ((val = strchr(key, '='))) *(val++) = '\0';
152 if (*val == '\0') return -1;
153 for (i = 0; types[i]; i++) {
155 if (!strcmp(type->name,name)) {
156 rc = type->parse_option(key, val);