-
Notifications
You must be signed in to change notification settings - Fork 0
/
testpcsc.c
478 lines (418 loc) · 13.2 KB
/
testpcsc.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
/*
* MUSCLE SmartCard Development ( http://pcsclite.alioth.debian.org/pcsclite.html )
*
* Copyright (C) 1999
* David Corcoran <corcoran@musclecard.com>
* Copyright (C) 2004-2010
* Ludovic Rousseau <ludovic.rousseau@free.fr>
*
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions
are met:
1. Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in the
documentation and/or other materials provided with the distribution.
3. The name of the author may not be used to endorse or promote products
derived from this software without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
* $Id: testpcsc.c 7004 2014-10-02 09:26:36Z rousseau $
*/
/**
* @file
* @brief This is a test program for pcsc-lite.
*/
#include <PCSC/config.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <PCSC/pcsclite.h>
#include <PCSC/winscard.h>
#include <PCSC/reader.h>
#define PANIC 0
#define DONT_PANIC 1
#define USE_AUTOALLOCATE
#define BLUE "\33[34m"
#define RED "\33[31m"
#define BRIGHT_RED "\33[01;31m"
#define GREEN "\33[32m"
#define NORMAL "\33[0m"
#define MAGENTA "\33[35m"
static void test_rv(LONG rv, SCARDCONTEXT hContext, int dont_panic)
{
if (rv != SCARD_S_SUCCESS)
{
if (dont_panic)
printf(BLUE "%s (don't panic)\n" NORMAL, pcsc_stringify_error(rv));
else
{
printf(RED "%s\n" NORMAL, pcsc_stringify_error(rv));
(void)SCardReleaseContext(hContext);
exit(-1);
}
}
else
(void)puts(pcsc_stringify_error(rv));
}
int main(/*@unused@*/ int argc, /*@unused@*/ char **argv)
{
SCARDHANDLE hCard;
SCARDCONTEXT hContext;
SCARD_READERSTATE rgReaderStates[1];
DWORD dwReaderLen, dwState, dwProt, dwAtrLen;
DWORD dwPref, dwReaders = 0;
char *pcReaders = NULL, *mszReaders;
#ifdef USE_AUTOALLOCATE
unsigned char *pbAtr = NULL;
#else
unsigned char pbAtr[MAX_ATR_SIZE];
#endif
union {
unsigned char as_char[100];
DWORD as_DWORD;
uint32_t as_uint32_t;
} buf;
DWORD dwBufLen;
unsigned char *pbAttr = NULL;
DWORD pcbAttrLen;
char *mszGroups;
DWORD dwGroups = 0;
long rv;
DWORD i;
int p, iReader;
int iList[16] = {0};
SCARD_IO_REQUEST ioRecvPci = *SCARD_PCI_T0; /* use a default value */
const SCARD_IO_REQUEST *pioSendPci;
unsigned char bSendBuffer[MAX_BUFFER_SIZE];
unsigned char bRecvBuffer[MAX_BUFFER_SIZE];
DWORD send_length, length;
(void)argc;
(void)argv;
printf("\nMUSCLE PC/SC Lite unitary test Program\n\n");
printf(MAGENTA "THIS PROGRAM IS NOT DESIGNED AS A TESTING TOOL FOR END USERS!\n");
printf("Do NOT use it unless you really know what you do.\n\n" NORMAL);
printf("Testing SCardEstablishContext\t: ");
rv = SCardEstablishContext(SCARD_SCOPE_SYSTEM, NULL, NULL, &hContext);
test_rv(rv, hContext, PANIC);
printf("Testing SCardIsValidContext\t: ");
rv = SCardIsValidContext(hContext);
test_rv(rv, hContext, PANIC);
printf("Testing SCardIsValidContext\t: ");
rv = SCardIsValidContext(hContext+1);
test_rv(rv, hContext, DONT_PANIC);
printf("Testing SCardListReaderGroups\t: ");
#ifdef USE_AUTOALLOCATE
dwGroups = SCARD_AUTOALLOCATE;
rv = SCardListReaderGroups(hContext, (LPSTR)&mszGroups, &dwGroups);
#else
rv = SCardListReaderGroups(hContext, NULL, &dwGroups);
test_rv(rv, hContext, PANIC);
printf("Testing SCardListReaderGroups\t: ");
mszGroups = calloc(dwGroups, sizeof(char));
rv = SCardListReaderGroups(hContext, mszGroups, &dwGroups);
#endif
test_rv(rv, hContext, PANIC);
/*
* Have to understand the multi-string here
*/
p = 0;
for (i = 0; i+1 < dwGroups; i++)
{
++p;
printf(GREEN "Group %02d: %s\n" NORMAL, p, &mszGroups[i]);
while (mszGroups[++i] != 0) ;
}
#ifdef USE_AUTOALLOCATE
printf("Testing SCardFreeMemory\t\t: ");
rv = SCardFreeMemory(hContext, mszGroups);
test_rv(rv, hContext, PANIC);
#else
free(mszGroups);
#endif
wait_for_card_again:
mszGroups = NULL;
printf("Testing SCardListReaders\t: ");
rv = SCardListReaders(hContext, mszGroups, NULL, &dwReaders);
test_rv(rv, hContext, DONT_PANIC);
if (SCARD_E_NO_READERS_AVAILABLE == rv)
{
printf("Testing SCardGetStatusChange \n");
printf("Please insert a working reader\t: ");
(void)fflush(stdout);
rgReaderStates[0].szReader = "\\\\?PnP?\\Notification";
rgReaderStates[0].dwCurrentState = SCARD_STATE_EMPTY;
rv = SCardGetStatusChange(hContext, INFINITE, rgReaderStates, 1);
test_rv(rv, hContext, PANIC);
}
printf("Testing SCardListReaders\t: ");
#ifdef USE_AUTOALLOCATE
dwReaders = SCARD_AUTOALLOCATE;
rv = SCardListReaders(hContext, mszGroups, (LPSTR)&mszReaders, &dwReaders);
#else
rv = SCardListReaders(hContext, mszGroups, NULL, &dwReaders);
test_rv(rv, hContext, PANIC);
printf("Testing SCardListReaders\t: ");
mszReaders = calloc(dwReaders, sizeof(char));
rv = SCardListReaders(hContext, mszGroups, mszReaders, &dwReaders);
#endif
test_rv(rv, hContext, DONT_PANIC);
/*
* Have to understand the multi-string here
*/
p = 0;
for (i = 0; i+1 < dwReaders; i++)
{
++p;
printf(GREEN "Reader %02d: %s\n" NORMAL, p, &mszReaders[i]);
iList[p] = i;
while (mszReaders[++i] != 0) ;
}
if (p > 1)
do
{
char input[80];
char *r;
printf("Enter the reader number\t\t: ");
r = fgets(input, sizeof(input), stdin);
if (NULL == r)
iReader = -1;
else
iReader = atoi(input);
if (iReader > p || iReader <= 0)
printf("Invalid Value - try again\n");
}
while (iReader > p || iReader <= 0);
else
iReader = 1;
rgReaderStates[0].szReader = &mszReaders[iList[iReader]];
rgReaderStates[0].dwCurrentState = SCARD_STATE_EMPTY;
printf("Waiting for card insertion\t: ");
(void)fflush(stdout);
rv = SCardGetStatusChange(hContext, INFINITE, rgReaderStates, 1);
test_rv(rv, hContext, PANIC);
if (rgReaderStates[0].dwEventState & SCARD_STATE_UNKNOWN)
{
printf("\nA reader has been connected/disconnected\n");
goto wait_for_card_again;
}
printf("Testing SCardConnect\t\t: ");
rv = SCardConnect(hContext, &mszReaders[iList[iReader]],
SCARD_SHARE_SHARED, SCARD_PROTOCOL_T0 | SCARD_PROTOCOL_T1,
&hCard, &dwPref);
test_rv(rv, hContext, PANIC);
switch(dwPref)
{
case SCARD_PROTOCOL_T0:
pioSendPci = SCARD_PCI_T0;
break;
case SCARD_PROTOCOL_T1:
pioSendPci = SCARD_PCI_T1;
break;
case SCARD_PROTOCOL_RAW:
pioSendPci = SCARD_PCI_RAW;
break;
default:
printf("Unknown protocol\n");
return -1;
}
/* APDU select file */
printf("Select file:");
send_length = 7;
memcpy(bSendBuffer, "\x00\xA4\x00\x00\x02\x3F\x00", send_length);
for (i=0; i<send_length; i++)
printf(" %02X", bSendBuffer[i]);
printf("\n");
length = sizeof(bRecvBuffer);
printf("Testing SCardTransmit\t\t: ");
rv = SCardTransmit(hCard, pioSendPci, bSendBuffer, send_length,
&ioRecvPci, bRecvBuffer, &length);
test_rv(rv, hContext, PANIC);
printf(" card response:" GREEN);
for (i=0; i<length; i++)
printf(" %02X", bRecvBuffer[i]);
printf("\n" NORMAL);
printf("Testing SCardControl\t\t: ");
#ifdef PCSC_PRE_120
{
char buffer[1024] = "Foobar";
DWORD cbRecvLength = sizeof(buffer);
rv = SCardControl(hCard, buffer, 7, buffer, &cbRecvLength);
}
#else
{
char buffer[1024] = { 0x02 };
DWORD cbRecvLength = sizeof(buffer);
rv = SCardControl(hCard, SCARD_CTL_CODE(1), buffer, 1, buffer,
sizeof(buffer), &cbRecvLength);
if (cbRecvLength && (SCARD_S_SUCCESS == rv))
{
for (i=0; i<cbRecvLength; i++)
printf("%c", buffer[i]);
printf(" ");
}
}
#endif
test_rv(rv, hContext, DONT_PANIC);
printf("Testing SCardGetAttrib\t\t: ");
#ifdef USE_AUTOALLOCATE
pcbAttrLen = SCARD_AUTOALLOCATE;
rv = SCardGetAttrib(hCard, SCARD_ATTR_DEVICE_FRIENDLY_NAME, (unsigned char *)&pbAttr,
&pcbAttrLen);
#else
rv = SCardGetAttrib(hCard, SCARD_ATTR_DEVICE_FRIENDLY_NAME, NULL, &pcbAttrLen);
test_rv(rv, hContext, DONT_PANIC);
if (rv == SCARD_S_SUCCESS)
{
printf("SCARD_ATTR_DEVICE_FRIENDLY_NAME length: " GREEN "%ld\n" NORMAL, pcbAttrLen);
pbAttr = malloc(pcbAttrLen);
}
printf("Testing SCardGetAttrib\t\t: ");
rv = SCardGetAttrib(hCard, SCARD_ATTR_DEVICE_FRIENDLY_NAME, pbAttr, &pcbAttrLen);
#endif
test_rv(rv, hContext, DONT_PANIC);
if (rv == SCARD_S_SUCCESS)
printf("SCARD_ATTR_DEVICE_FRIENDLY_NAME: " GREEN "%s\n" NORMAL, pbAttr);
#ifdef USE_AUTOALLOCATE
printf("Testing SCardFreeMemory\t\t: ");
rv = SCardFreeMemory(hContext, pbAttr);
test_rv(rv, hContext, PANIC);
#else
if (pbAttr)
free(pbAttr);
#endif
printf("Testing SCardGetAttrib\t\t: ");
#ifdef USE_AUTOALLOCATE
pcbAttrLen = SCARD_AUTOALLOCATE;
rv = SCardGetAttrib(hCard, SCARD_ATTR_ATR_STRING, (unsigned char *)&pbAttr,
&pcbAttrLen);
#else
rv = SCardGetAttrib(hCard, SCARD_ATTR_ATR_STRING, NULL, &pcbAttrLen);
test_rv(rv, hContext, DONT_PANIC);
if (rv == SCARD_S_SUCCESS)
{
printf("SCARD_ATTR_ATR_STRING length: " GREEN "%ld\n" NORMAL, pcbAttrLen);
pbAttr = malloc(pcbAttrLen);
}
printf("Testing SCardGetAttrib\t\t: ");
rv = SCardGetAttrib(hCard, SCARD_ATTR_ATR_STRING, pbAttr, &pcbAttrLen);
#endif
test_rv(rv, hContext, DONT_PANIC);
if (rv == SCARD_S_SUCCESS)
{
printf("SCARD_ATTR_ATR_STRING length: " GREEN "%ld\n" NORMAL, pcbAttrLen);
printf("SCARD_ATTR_ATR_STRING: " GREEN);
for (i = 0; i < pcbAttrLen; i++)
printf("%02X ", pbAttr[i]);
printf("\n" NORMAL);
}
#ifdef USE_AUTOALLOCATE
printf("Testing SCardFreeMemory\t\t: ");
rv = SCardFreeMemory(hContext, pbAttr);
test_rv(rv, hContext, PANIC);
#else
if (pbAttr)
free(pbAttr);
#endif
printf("Testing SCardGetAttrib\t\t: ");
dwBufLen = sizeof(buf);
rv = SCardGetAttrib(hCard, SCARD_ATTR_VENDOR_IFD_VERSION, buf.as_char, &dwBufLen);
test_rv(rv, hContext, DONT_PANIC);
if (rv == SCARD_S_SUCCESS)
printf("Vendor IFD version\t\t: " GREEN "0x%08lX\n" NORMAL,
buf.as_DWORD);
printf("Testing SCardGetAttrib\t\t: ");
dwBufLen = sizeof(buf);
rv = SCardGetAttrib(hCard, SCARD_ATTR_MAXINPUT, buf.as_char, &dwBufLen);
test_rv(rv, hContext, DONT_PANIC);
if (rv == SCARD_S_SUCCESS)
{
if (dwBufLen == sizeof(uint32_t))
printf("Max message length\t\t: " GREEN "%d\n" NORMAL,
buf.as_uint32_t);
else
printf(RED "Wrong size" NORMAL);
}
printf("Testing SCardGetAttrib\t\t: ");
dwBufLen = sizeof(buf);
rv = SCardGetAttrib(hCard, SCARD_ATTR_VENDOR_NAME, buf.as_char, &dwBufLen);
test_rv(rv, hContext, DONT_PANIC);
if (rv == SCARD_S_SUCCESS)
printf("Vendor name\t\t\t: " GREEN "%s\n" NORMAL, buf.as_char);
printf("Testing SCardSetAttrib\t\t: ");
rv = SCardSetAttrib(hCard, SCARD_ATTR_ATR_STRING, (LPCBYTE)"", 1);
test_rv(rv, hContext, DONT_PANIC);
printf("Testing SCardStatus\t\t: ");
#ifdef USE_AUTOALLOCATE
dwReaderLen = SCARD_AUTOALLOCATE;
dwAtrLen = SCARD_AUTOALLOCATE;
rv = SCardStatus(hCard, (LPSTR)&pcReaders, &dwReaderLen, &dwState, &dwProt,
(LPBYTE)&pbAtr, &dwAtrLen);
#else
dwReaderLen = 100;
pcReaders = malloc(sizeof(char) * 100);
dwAtrLen = MAX_ATR_SIZE;
rv = SCardStatus(hCard, pcReaders, &dwReaderLen, &dwState, &dwProt,
pbAtr, &dwAtrLen);
#endif
test_rv(rv, hContext, PANIC);
printf("Current Reader Name\t\t: " GREEN "%s\n" NORMAL, pcReaders);
printf("Current Reader State\t\t: " GREEN "0x%.4lx\n" NORMAL, dwState);
printf("Current Reader Protocol\t\t: T=" GREEN "%ld\n" NORMAL, dwProt - 1);
printf("Current Reader ATR Size\t\t: " GREEN "%ld" NORMAL " bytes\n",
dwAtrLen);
printf("Current Reader ATR Value\t: " GREEN);
for (i = 0; i < dwAtrLen; i++)
{
printf("%02X ", pbAtr[i]);
}
printf(NORMAL "\n");
#ifdef USE_AUTOALLOCATE
printf("Testing SCardFreeMemory\t\t: ");
rv = SCardFreeMemory(hContext, pcReaders);
test_rv(rv, hContext, PANIC);
printf("Testing SCardFreeMemory\t\t: ");
rv = SCardFreeMemory(hContext, pbAtr);
test_rv(rv, hContext, PANIC);
#else
if (pcReaders)
free(pcReaders);
#endif
if (rv != SCARD_S_SUCCESS)
{
(void)SCardDisconnect(hCard, SCARD_RESET_CARD);
(void)SCardReleaseContext(hContext);
}
printf("Press enter: ");
(void)getchar();
printf("Testing SCardReconnect\t\t: ");
rv = SCardReconnect(hCard, SCARD_SHARE_SHARED,
SCARD_PROTOCOL_T0 | SCARD_PROTOCOL_T1, SCARD_UNPOWER_CARD, &dwPref);
test_rv(rv, hContext, PANIC);
printf("Testing SCardDisconnect\t\t: ");
rv = SCardDisconnect(hCard, SCARD_UNPOWER_CARD);
test_rv(rv, hContext, PANIC);
#ifdef USE_AUTOALLOCATE
printf("Testing SCardFreeMemory\t\t: ");
rv = SCardFreeMemory(hContext, mszReaders);
test_rv(rv, hContext, PANIC);
#else
free(mszReaders);
#endif
printf("Testing SCardReleaseContext\t: ");
rv = SCardReleaseContext(hContext);
test_rv(rv, hContext, PANIC);
printf("\n");
printf("PC/SC Test Completed Successfully !\n");
return 0;
}