-
Notifications
You must be signed in to change notification settings - Fork 5
/
pcsc_scan.c
561 lines (480 loc) · 15.9 KB
/
pcsc_scan.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
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
/*
Scan and print all the PC/SC devices available
Copyright (C) 2001-2011 Ludovic Rousseau <[email protected]>
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
/* $Id: pcsc_scan.c 5892 2011-08-19 09:32:46Z rousseau $ */
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <unistd.h>
#include <string.h>
#ifdef __APPLE__
#include <PCSC/wintypes.h>
#include <PCSC/winscard.h>
#else
#include <winscard.h>
#endif
#ifndef TRUE
#define TRUE 1
#define FALSE 0
#endif
#define TIMEOUT 1000 /* 1 second timeout */
/* command used to parse (on screen) the ATR */
#define ATR_PARSER "ATR_analysis"
#ifndef SCARD_E_NO_READERS_AVAILABLE
#define SCARD_E_NO_READERS_AVAILABLE 0x8010002E
#endif
#define CHECK(f, rv) \
if (SCARD_S_SUCCESS != rv) \
{ \
printf(f ": %s\n", pcsc_stringify_error(rv)); \
return -1; \
}
#define test_rv(fct, rv, hContext) \
do { \
if (rv != SCARD_S_SUCCESS) \
{ \
printf("%s%s: %s%s\n", red, fct, pcsc_stringify_error(rv), color_end); \
(void)SCardReleaseContext(hContext); \
exit(-1); \
} \
} while(0)
void usage(void)
{
printf("usage: pcsc_scan [-n] [-V] [-h]\n");
printf(" -n : no ATR analysis\n");
printf(" -V : print version number\n");
printf(" -h : this help\n");
} /* usage */
/**
* Given various Card parameters this fetches the current GPII token
* as saved on the NFC Card. Currently this is based off of using the
* 'text only' flashing feature of a Windows App we used to flash the
* cards, which seems to be starting the text at position 9 on block
* 4.
*/
char* get_gpii_token(SCARDCONTEXT hContext, LPTSTR mszReaders, SCARDHANDLE hCard) {
printf("Get GPII Token stuff 8\n");
LONG rv;
DWORD dwActiveProtocol, dwRecvLength;
SCARD_IO_REQUEST pioSendPci;
BYTE load_key_cmd[] = {0xFF, 0x82, 0x00, 0x00, 0x06, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF};
BYTE auth_block_cmd[] = {0xFF, 0x86, 0x00, 0x00, 0x05, 0x01, 0x00, 0x04, 0x61, 0x00};
BYTE read_block_cmd[] = {0xFF,0xB0,0x00,0x04,0x10};
BYTE pbRecvBuffer[32768];
int block_count = 0;
int i = 0;
char token[512];
char token_loc = 0;
char url[2048];
rv = SCardConnect(hContext, mszReaders, SCARD_SHARE_SHARED,
SCARD_PROTOCOL_T0 | SCARD_PROTOCOL_T1, &hCard, &dwActiveProtocol);
CHECK("SCardConnect", rv);
switch(dwActiveProtocol)
{
case SCARD_PROTOCOL_T0:
pioSendPci = *SCARD_PCI_T0;
break;
case SCARD_PROTOCOL_T1:
pioSendPci = *SCARD_PCI_T1;
break;
}
dwRecvLength = sizeof(pbRecvBuffer);
rv = SCardTransmit(hCard, &pioSendPci, load_key_cmd, sizeof(load_key_cmd),
NULL, pbRecvBuffer, &dwRecvLength);
CHECK("SCardTransmit", rv);
printf("response: ");
for(i=0; i<dwRecvLength; i++)
printf("%02X ", pbRecvBuffer[i]);
printf("\n");
dwRecvLength = sizeof(pbRecvBuffer);
rv = SCardTransmit(hCard, &pioSendPci, auth_block_cmd, sizeof(auth_block_cmd),
NULL, pbRecvBuffer, &dwRecvLength);
CHECK("SCardTransmit", rv);
printf("response: ");
for(i=0; i<dwRecvLength; i++)
printf("%02X ", pbRecvBuffer[i]);
printf("\n");
for (block_count = 4; block_count<7; block_count++) {
dwRecvLength = sizeof(pbRecvBuffer);
auth_block_cmd[7] = block_count;
read_block_cmd[3] = block_count;
rv = SCardTransmit(hCard, &pioSendPci, read_block_cmd, sizeof(read_block_cmd),
NULL, pbRecvBuffer, &dwRecvLength);
CHECK("SCardTransmit", rv);
if (block_count == 4) {
i = 9;
}
else {
i = 0;
}
for(i; i<(dwRecvLength-2); i++) {
printf("%c", pbRecvBuffer[i]);
token[token_loc] = pbRecvBuffer[i];
token_loc++;
}
}
return &token;
}
int main(int argc, char *argv[])
{
int current_reader;
LONG rv;
SCARDCONTEXT hContext;
SCARD_READERSTATE *rgReaderStates_t = NULL;
SCARD_READERSTATE rgReaderStates[1];
SCARDHANDLE hCard;
DWORD dwReaders = 0, dwReadersOld;
DWORD timeout;
LPSTR mszReaders = NULL;
char *ptr, **readers = NULL;
int nbReaders, i;
char atr[MAX_ATR_SIZE*3+1]; /* ATR in ASCII */
char atr_packed[MAX_ATR_SIZE*3+1];
char atr_command[sizeof(atr)+sizeof(ATR_PARSER)+2+1];
int opt;
int analyse_atr = TRUE;
char *blue = "";
char *red = "";
char *magenta = "";
char *color_end = "";
int pnp = TRUE;
int logged_in = FALSE;
time_t logged_in_time;
char *gpii_token;
printf("PC/SC device scanner\n");
printf("V " VERSION " (c) 2001-2011, Ludovic Rousseau <[email protected]>\n");
printf("Compiled with PC/SC lite version: " PCSCLITE_VERSION_NUMBER "\n");
while ((opt = getopt(argc, argv, "Vhn")) != EOF)
{
switch (opt)
{
case 'n':
analyse_atr = FALSE;
break;
case 'V':
/* the version number is printed by default */
return 1;
break;
case 'h':
default:
usage();
return 1;
break;
}
}
if (argc - optind != 0)
{
usage();
return 1;
}
/* check if terminal supports color */
{
const char *terms[] = { "linux", "xterm", "xterm-color", "Eterm", "rxvt", "rxvt-unicode" };
char *term;
term = getenv("TERM");
if (term)
{
int i;
/* for each known color terminal */
for (i = 0; i < sizeof(terms) / sizeof(terms[0]); i++)
{
/* we found a supported term? */
if (0 == strcmp(terms[i], term))
{
blue = "\33[34m";
red = "\33[31m";
magenta = "\33[35m";
color_end = "\33[0m";
break;
}
}
}
}
rv = SCardEstablishContext(SCARD_SCOPE_SYSTEM, NULL, NULL, &hContext);
test_rv("SCardEstablishContext", rv, hContext);
rgReaderStates[0].szReader = "\\\\?PnP?\\Notification";
rgReaderStates[0].dwCurrentState = SCARD_STATE_UNAWARE;
rv = SCardGetStatusChange(hContext, 0, rgReaderStates, 1);
if (rgReaderStates[0].dwEventState & SCARD_STATE_UNKNOWN)
{
timeout = TIMEOUT;
printf("%sPlug'n play reader name not supported. Using polling every %ld ms.%s\n", magenta, timeout, color_end);
pnp = FALSE;
}
else
{
timeout = INFINITE;
printf("%sUsing reader plug'n play mechanism%s\n", magenta, color_end);
}
get_readers:
/* free memory possibly allocated in a previous loop */
if (NULL != readers)
{
free(readers);
readers = NULL;
}
if (NULL != rgReaderStates_t)
{
free(rgReaderStates_t);
rgReaderStates_t = NULL;
}
/* Retrieve the available readers list.
*
* 1. Call with a null buffer to get the number of bytes to allocate
* 2. malloc the necessary storage
* 3. call with the real allocated buffer
*/
printf("%sScanning present readers...%s\n", red, color_end);
rv = SCardListReaders(hContext, NULL, NULL, &dwReaders);
if (rv != SCARD_E_NO_READERS_AVAILABLE)
test_rv("SCardListReaders", rv, hContext);
dwReadersOld = dwReaders;
/* if non NULL we came back so free first */
if (mszReaders)
{
free(mszReaders);
mszReaders = NULL;
}
mszReaders = malloc(sizeof(char)*dwReaders);
if (mszReaders == NULL)
{
printf("malloc: not enough memory\n");
return 1;
}
*mszReaders = '\0';
rv = SCardListReaders(hContext, NULL, mszReaders, &dwReaders);
/* Extract readers from the null separated string and get the total
* number of readers */
nbReaders = 0;
ptr = mszReaders;
while (*ptr != '\0')
{
ptr += strlen(ptr)+1;
nbReaders++;
}
if (SCARD_E_NO_READERS_AVAILABLE == rv || 0 == nbReaders)
{
printf("%sWaiting for the first reader...%s", red, color_end);
fflush(stdout);
if (pnp)
{
rgReaderStates[0].szReader = "\\\\?PnP?\\Notification";
rgReaderStates[0].dwCurrentState = SCARD_STATE_UNAWARE;
rv = SCardGetStatusChange(hContext, INFINITE, rgReaderStates, 1);
test_rv("SCardGetStatusChange", rv, hContext);
}
else
{
rv = SCARD_S_SUCCESS;
while ((SCARD_S_SUCCESS == rv) && (dwReaders == dwReadersOld))
{
rv = SCardListReaders(hContext, NULL, NULL, &dwReaders);
if (SCARD_E_NO_READERS_AVAILABLE == rv)
rv = SCARD_S_SUCCESS;
sleep(1);
}
}
printf("found one\n");
goto get_readers;
}
else
test_rv("SCardListReader", rv, hContext);
/* allocate the readers table */
readers = calloc(nbReaders+1, sizeof(char *));
if (NULL == readers)
{
printf("Not enough memory for readers table\n");
return -1;
}
/* fill the readers table */
nbReaders = 0;
ptr = mszReaders;
while (*ptr != '\0')
{
printf("%s%d: %s%s\n", blue, nbReaders, ptr, color_end);
readers[nbReaders] = ptr;
ptr += strlen(ptr)+1;
nbReaders++;
}
/* allocate the ReaderStates table */
rgReaderStates_t = calloc(nbReaders+1, sizeof(* rgReaderStates_t));
if (NULL == rgReaderStates_t)
{
printf("Not enough memory for readers states\n");
return -1;
}
/* Set the initial states to something we do not know
* The loop below will include this state to the dwCurrentState
*/
for (i=0; i<nbReaders; i++)
{
rgReaderStates_t[i].szReader = readers[i];
rgReaderStates_t[i].dwCurrentState = SCARD_STATE_UNAWARE;
}
rgReaderStates_t[nbReaders].szReader = "\\\\?PnP?\\Notification";
rgReaderStates_t[nbReaders].dwCurrentState = SCARD_STATE_UNAWARE;
/* Wait endlessly for all events in the list of readers
* We only stop in case of an error
*/
if (pnp)
nbReaders++;
rv = SCardGetStatusChange(hContext, timeout, rgReaderStates_t, nbReaders);
while ((rv == SCARD_S_SUCCESS) || (rv == SCARD_E_TIMEOUT))
{
time_t t;
if (pnp)
{
if (rgReaderStates_t[nbReaders-1].dwEventState &
SCARD_STATE_CHANGED)
goto get_readers;
}
else
{
/* A new reader appeared? */
if ((SCardListReaders(hContext, NULL, NULL, &dwReaders)
== SCARD_S_SUCCESS) && (dwReaders != dwReadersOld))
goto get_readers;
}
/* Timestamp the event as we get notified */
t = time(NULL);
printf("\n%s", ctime(&t));
/* Now we have an event, check all the readers in the list to see what
* happened */
for (current_reader=0; current_reader < nbReaders; current_reader++)
{
if (rgReaderStates_t[current_reader].dwEventState &
SCARD_STATE_CHANGED)
{
/* If something has changed the new state is now the current
* state */
rgReaderStates_t[current_reader].dwCurrentState =
rgReaderStates_t[current_reader].dwEventState;
}
else
/* If nothing changed then skip to the next reader */
continue;
/* From here we know that the state for the current reader has
* changed because we did not pass through the continue statement
* above.
*/
/* Specify the current reader's number and name */
printf("Reader %d: %s%s%s\n", current_reader,
magenta, rgReaderStates_t[current_reader].szReader,
color_end);
/* Dump the full current state */
printf(" Card state: %s", red);
if (rgReaderStates_t[current_reader].dwEventState &
SCARD_STATE_IGNORE)
printf("Ignore this reader, ");
if (rgReaderStates_t[current_reader].dwEventState &
SCARD_STATE_UNKNOWN)
{
printf("Reader unknown\n");
goto get_readers;
}
if (rgReaderStates_t[current_reader].dwEventState &
SCARD_STATE_UNAVAILABLE)
printf("Status unavailable, ");
if (rgReaderStates_t[current_reader].dwEventState &
SCARD_STATE_EMPTY)
{
printf("Card removed, ");
/* if (logged_in) { */
/* printf("Logging out"); */
/* system("curl http://localhost:8081/user/intergalacticSheepherder/logout"); */
/* logged_in = FALSE; */
/* } */
}
if (rgReaderStates_t[current_reader].dwEventState &
SCARD_STATE_PRESENT) {
if (rgReaderStates_t[current_reader].cbAtr > 0)
{
printf(" ATR: ");
if (rgReaderStates_t[current_reader].cbAtr)
{
for (i=0; i<rgReaderStates_t[current_reader].cbAtr; i++) {
sprintf(&atr[i*3], "%02X ",
rgReaderStates_t[current_reader].rgbAtr[i]);
sprintf(&atr_packed[i*2], "%02X",
rgReaderStates_t[current_reader].rgbAtr[i]);
}
atr[i*3-1] = '\0';
atr_packed[i*2] = '\0';
}
else
{
atr[0] = '\0';
atr_packed[0] = '\0';
}
printf("Card inserted. ");
printf("%s%s%s\n", magenta, atr, color_end);
// sprintf("The comp: %d", strncmp(atr,gpii_atrs[count],4));
time_t current_time = time(NULL);
if (logged_in) {
time_t session_duration = current_time-logged_in_time;
printf("Session Duration: %i", (int)session_duration);
if (session_duration > 5) {
char logout_cmd[1024];
gpii_token = get_gpii_token(hContext, mszReaders,&hCard);
sprintf(&logout_cmd, "curl http://localhost:8081/user/%s/logout", gpii_token);
system(logout_cmd);
printf("\nFinal Command: %s", logout_cmd);
printf("\n");
logged_in = FALSE;
}
}
else {
gpii_token = get_gpii_token(hContext, mszReaders,&hCard);
char login_cmd[1024];
sprintf(&login_cmd, "curl http://localhost:8081/user/%s/login", gpii_token);
system(login_cmd);
printf("\nFinal Command: %s", login_cmd);
printf("\n");
logged_in = TRUE;
logged_in_time = time(NULL);
}
}
}
if (rgReaderStates_t[current_reader].dwEventState &
SCARD_STATE_ATRMATCH)
printf("ATR matches card, ");
if (rgReaderStates_t[current_reader].dwEventState &
SCARD_STATE_EXCLUSIVE)
printf("Exclusive Mode, ");
if (rgReaderStates_t[current_reader].dwEventState &
SCARD_STATE_INUSE)
printf("Shared Mode, ");
if (rgReaderStates_t[current_reader].dwEventState &
SCARD_STATE_MUTE)
printf("Unresponsive card, ");
printf("%s\n", color_end);
/* force display */
fflush(stdout);
} /* for */
rv = SCardGetStatusChange(hContext, timeout, rgReaderStates_t,
nbReaders);
} /* while */
/* If we get out the loop, GetStatusChange() was unsuccessful */
test_rv("SCardGetStatusChange", rv, hContext);
/* We try to leave things as clean as possible */
rv = SCardReleaseContext(hContext);
test_rv("SCardReleaseContext", rv, hContext);
/* free memory possibly allocated */
if (NULL != readers)
free(readers);
if (NULL != rgReaderStates_t)
free(rgReaderStates_t);
return 0;
} /* main */