2
* MUSCLE SmartCard Development ( http://www.linuxnet.com )
4
* Copyright (C) 2000-2002
5
* David Corcoran <corcoran@linuxnet.com>
6
* Ludovic Rousseau <ludovic.rousseau@free.fr>
8
* $Id: formaticc.c 1421 2005-04-12 12:09:21Z rousseau $
13
* @brief This is an APDU robot for pcsc-lite.
23
#ifndef MAXHOSTNAMELEN
24
#define MAXHOSTNAMELEN 64
27
int main(int argc, char *argv[])
30
SCARDCONTEXT hContext;
31
SCARD_IO_REQUEST sRecvPci;
32
SCARD_READERSTATE_A rgReaderStates[1];
33
DWORD dwSendLength, dwRecvLength, dwPref, dwReaders;
35
BYTE s[MAX_BUFFER_SIZE], r[MAX_BUFFER_SIZE];
40
int i, p, iReader, cnum, iProtocol;
42
char pcHost[MAXHOSTNAMELEN];
43
char pcAFile[FILENAME_MAX];
44
char pcOFile[FILENAME_MAX];
49
printf("\nWinscard PC/SC Lite Test Program\n\n");
51
printf("Please enter the desired host (localhost for this machine) [localhost]: ");
52
fgets(line, sizeof(line), stdin);
54
strncpy(pcHost, "localhost", sizeof(pcHost)-1);
56
strncpy(pcHost, line, sizeof(pcHost)-1);
58
printf("Please input the desired transmit protocol (0/1) [0]: ");
59
fgets(line, sizeof(line), stdin);
63
sscanf(line, "%d", &iProtocol);
65
printf("Please input the desired input apdu file: ");
66
fgets(line, sizeof(line), stdin);
67
sscanf(line, "%s", pcAFile);
69
printf("Please input the desired output apdu file: ");
70
fgets(line, sizeof(line), stdin);
71
sscanf(line, "%s", pcOFile);
73
fp = fopen(pcAFile, "r");
80
fo = fopen(pcOFile, "w");
87
rv = SCardEstablishContext(SCARD_SCOPE_GLOBAL, pcHost, NULL, &hContext);
89
if (rv != SCARD_S_SUCCESS)
91
printf("ERROR :: Cannot Connect to Resource Manager\n");
96
SCardListReaders(hContext, mszGroups, 0, &dwReaders);
97
mszReaders = (char *) malloc(sizeof(char) * dwReaders);
98
SCardListReaders(hContext, mszGroups, mszReaders, &dwReaders);
101
* Have to understand the multi-string here
104
for (i = 0; i < dwReaders - 1; i++)
107
printf("Reader %02d: %s\n", p, &mszReaders[i]);
109
while (mszReaders[++i] != 0) ;
114
printf("Enter the desired reader number: ");
115
fgets(line, sizeof(line), stdin);
116
sscanf(line, "%d", &iReader);
119
if (iReader > p || iReader <= 0)
121
printf("Invalid Value - try again\n");
124
while (iReader > p || iReader <= 0);
126
rgReaderStates[0].szReader = &mszReaders[iList[iReader]];
127
rgReaderStates[0].dwCurrentState = SCARD_STATE_EMPTY;
129
printf("Please insert a smart card\n");
130
SCardGetStatusChange(hContext, INFINITE, rgReaderStates, 1);
131
rv = SCardConnect(hContext, &mszReaders[iList[iReader]],
132
SCARD_SHARE_SHARED, SCARD_PROTOCOL_T0 | SCARD_PROTOCOL_T1,
135
if (rv != SCARD_S_SUCCESS)
137
SCardReleaseContext(hContext);
138
printf("Error connecting to reader %ld\n", rv);
143
* Now lets get some work done
146
SCardBeginTransaction(hCard);
154
if (fgets(line, sizeof(line), fp) == NULL)
158
if (sscanf(line_ptr, "%x", &x) == 0)
162
line_ptr = strchr(line_ptr, ' ');
163
if (line_ptr == NULL)
167
for (i = 0; i < dwSendLength; i++)
169
if (sscanf(line_ptr, "%x", &x) == 0)
171
printf("Corrupt APDU: %s\n", line);
172
SCardDisconnect(hCard, SCARD_RESET_CARD);
173
SCardReleaseContext(hContext);
178
line_ptr = strchr(line_ptr, ' ');
180
if (line_ptr == NULL)
186
printf("Processing Command %03d of length %03lX: %s", cnum,
189
memset(r, 0x00, MAX_BUFFER_SIZE);
190
dwRecvLength = MAX_BUFFER_SIZE;
194
rv = SCardTransmit(hCard, SCARD_PCI_T0, s, dwSendLength,
195
&sRecvPci, r, &dwRecvLength);
201
rv = SCardTransmit(hCard, SCARD_PCI_T1, s, dwSendLength,
202
&sRecvPci, r, &dwRecvLength);
206
printf("Invalid Protocol\n");
207
SCardDisconnect(hCard, SCARD_RESET_CARD);
208
SCardReleaseContext(hContext);
213
if (rv != SCARD_S_SUCCESS)
214
fprintf(fo, ".error %ld\n", rv);
217
fprintf(fo, "%02ld ", dwRecvLength);
219
for (i = 0; i < dwRecvLength; i++)
220
fprintf(fo, "%02X ", r[i]);
225
if (rv == SCARD_W_RESET_CARD)
227
SCardReconnect(hCard, SCARD_SHARE_SHARED,
228
SCARD_PROTOCOL_T0 | SCARD_PROTOCOL_T1,
229
SCARD_RESET_CARD, &dwPref);
235
SCardEndTransaction(hCard, SCARD_LEAVE_CARD);
236
SCardDisconnect(hCard, SCARD_UNPOWER_CARD);
237
SCardReleaseContext(hContext);