1
/******************************************************************
3
MUSCLE SmartCard Development ( http://www.linuxnet.com )
6
Author : David Corcoran
8
License: Copyright (C) 2000 David Corcoran
9
<corcoran@linuxnet.com>
10
Purpose: This is an APDU robot for pcsc-lite.
12
********************************************************************/
2
* This is an APDU robot for pcsc-lite.
4
* MUSCLE SmartCard Development ( http://www.linuxnet.com )
6
* Copyright (C) 2000-2002
7
* David Corcoran <corcoran@linuxnet.com>
8
* Ludovic Rousseau <ludovic.rousseau@free.fr>
10
* $Id: formaticc.c,v 1.10 2004/05/10 13:14:30 rousseau Exp $
15
14
#include <stdlib.h>
18
#include "configfile.h"
20
int main( int argc, char *argv[] ) {
22
SCARDCONTEXT hContext;
23
//struct ReaderContext *rContext;
24
//SCARD_IO_REQUEST sSendPci;
25
SCARD_IO_REQUEST sRecvPci;
26
SCARD_READERSTATE_A rgReaderStates[1];
27
//DWORD dwReaderLen, dwState, dwProt, dwAtrLen;
28
DWORD dwSendLength, dwRecvLength, dwPref, dwReaders;
31
//BYTE pbAtr[MAX_ATR_SIZE];
32
BYTE s[MAX_BUFFER_SIZE], r[MAX_BUFFER_SIZE];
37
int i, p, iReader, cnum, iProtocol;
45
printf("\nWinscard PC/SC Lite Test Program\n\n");
47
printf("Please enter the desired host (localhost for this machine): ");
51
printf("Please input the desired transmit protocol (0/1): ");
52
scanf("%d", &iProtocol);
54
printf("Please input the desired input apdu file: ");
57
printf("Please input the desired output apdu file: ");
60
fp = fopen( pcAFile, "r" );
61
fo = fopen( pcOFile, "w" );
63
if ( fp == NULL || fo == NULL ) {
64
printf("File not found\n");
68
rv = SCardEstablishContext( SCARD_SCOPE_GLOBAL, pcHost, NULL, &hContext );
70
if ( rv != SCARD_S_SUCCESS ) {
71
printf("ERROR :: Cannot Connect to Resource Manager\n");
76
SCardListReaders( hContext, mszGroups, 0, &dwReaders );
77
mszReaders = (char *)malloc(sizeof(char)*dwReaders);
78
SCardListReaders( hContext, mszGroups, mszReaders, &dwReaders );
80
/* Have to understand the multi-string here */
82
for ( i=0; i < dwReaders - 1; i++ ) {
84
printf("Reader %02d: %s\n", p, &mszReaders[i]);
86
while ( mszReaders[++i] != 0 );
90
printf("Enter the desired reader number : " );
91
scanf("%d", &iReader);
94
if ( iReader > p || iReader <= 0 ) {
95
printf("Invalid Value - try again\n");
97
} while ( iReader > p || iReader <= 0 );
99
rgReaderStates[0].szReader = &mszReaders[iList[iReader]];
100
rgReaderStates[0].dwCurrentState = SCARD_STATE_EMPTY;
102
printf("Please input a smartcard\n");
103
SCardGetStatusChange( hContext, INFINITE, rgReaderStates, 1 );
104
rv = SCardConnect(hContext, &mszReaders[iList[iReader]],
105
SCARD_SHARE_SHARED, SCARD_PROTOCOL_T0 | SCARD_PROTOCOL_T1,
109
if ( rv != SCARD_S_SUCCESS ) {
110
SCardReleaseContext( hContext );
111
printf("Error connecting to reader %ld\n", rv);
115
/* Now lets get some work done */
117
SCardBeginTransaction( hCard );
125
if ( fscanf( fp, "%x", (int *)&dwSendLength ) == EOF ) {
129
for (i=0; i < dwSendLength; i++) {
130
if ( fscanf( fp, "%x", (int *)&s[i] ) == EOF ) {
131
printf("Corrupt APDU\n");
132
SCardDisconnect( hCard, SCARD_RESET_CARD );
133
SCardReleaseContext( hContext );
138
printf("Processing Command %03d of length %03lx\n", cnum, dwSendLength);
140
memset( r, 0x00, MAX_BUFFER_SIZE );
141
dwRecvLength = MAX_BUFFER_SIZE;
143
if ( iProtocol == 0 ) {
144
rv = SCardTransmit( hCard, SCARD_PCI_T0, s, dwSendLength,
145
&sRecvPci, r, &dwRecvLength );
146
} else if ( iProtocol == 1 ) {
147
rv = SCardTransmit( hCard, SCARD_PCI_T1, s, dwSendLength,
148
&sRecvPci, r, &dwRecvLength );
150
printf("Invalid Protocol\n");
151
SCardDisconnect( hCard, SCARD_RESET_CARD );
152
SCardReleaseContext( hContext );
156
if ( rv != SCARD_S_SUCCESS ) {
157
fprintf(fo, ".error %ld\n", rv);
159
fprintf(fo, "%02ld ", dwRecvLength);
161
for (i=0; i < dwRecvLength; i++) {
162
fprintf(fo, "%02x ", r[i]);
166
if ( rv == SCARD_W_RESET_CARD ) {
167
SCardReconnect( hCard, SCARD_SHARE_SHARED,
168
SCARD_PROTOCOL_T0 | SCARD_PROTOCOL_T1,
169
SCARD_RESET_CARD, &dwPref );
174
SCardEndTransaction( hCard, SCARD_LEAVE_CARD );
175
SCardDisconnect( hCard, SCARD_UNPOWER_CARD );
176
SCardReleaseContext( hContext );
19
#ifndef MAXHOSTNAMELEN
20
#define MAXHOSTNAMELEN 64
23
int main(int argc, char *argv[])
26
SCARDCONTEXT hContext;
27
SCARD_IO_REQUEST sRecvPci;
28
SCARD_READERSTATE_A rgReaderStates[1];
29
DWORD dwSendLength, dwRecvLength, dwPref, dwReaders;
31
BYTE s[MAX_BUFFER_SIZE], r[MAX_BUFFER_SIZE];
36
int i, p, iReader, cnum, iProtocol;
38
char pcHost[MAXHOSTNAMELEN];
39
char pcAFile[FILENAME_MAX];
40
char pcOFile[FILENAME_MAX];
45
printf("\nWinscard PC/SC Lite Test Program\n\n");
47
printf("Please enter the desired host (localhost for this machine) [localhost]: ");
48
fgets(line, sizeof(line), stdin);
50
strncpy(pcHost, "localhost", sizeof(pcHost)-1);
52
strncpy(pcHost, line, sizeof(pcHost)-1);
54
printf("Please input the desired transmit protocol (0/1) [0]: ");
55
fgets(line, sizeof(line), stdin);
59
sscanf(line, "%d", &iProtocol);
61
printf("Please input the desired input apdu file: ");
62
fgets(line, sizeof(line), stdin);
63
sscanf(line, "%s", pcAFile);
65
printf("Please input the desired output apdu file: ");
66
fgets(line, sizeof(line), stdin);
67
sscanf(line, "%s", pcOFile);
69
fp = fopen(pcAFile, "r");
76
fo = fopen(pcOFile, "w");
83
rv = SCardEstablishContext(SCARD_SCOPE_GLOBAL, pcHost, NULL, &hContext);
85
if (rv != SCARD_S_SUCCESS)
87
printf("ERROR :: Cannot Connect to Resource Manager\n");
92
SCardListReaders(hContext, mszGroups, 0, &dwReaders);
93
mszReaders = (char *) malloc(sizeof(char) * dwReaders);
94
SCardListReaders(hContext, mszGroups, mszReaders, &dwReaders);
97
* Have to understand the multi-string here
100
for (i = 0; i < dwReaders - 1; i++)
103
printf("Reader %02d: %s\n", p, &mszReaders[i]);
105
while (mszReaders[++i] != 0) ;
110
printf("Enter the desired reader number: ");
111
fgets(line, sizeof(line), stdin);
112
sscanf(line, "%d", &iReader);
115
if (iReader > p || iReader <= 0)
117
printf("Invalid Value - try again\n");
120
while (iReader > p || iReader <= 0);
122
rgReaderStates[0].szReader = &mszReaders[iList[iReader]];
123
rgReaderStates[0].dwCurrentState = SCARD_STATE_EMPTY;
125
printf("Please insert a smart card\n");
126
SCardGetStatusChange(hContext, INFINITE, rgReaderStates, 1);
127
rv = SCardConnect(hContext, &mszReaders[iList[iReader]],
128
SCARD_SHARE_SHARED, SCARD_PROTOCOL_T0 | SCARD_PROTOCOL_T1,
131
if (rv != SCARD_S_SUCCESS)
133
SCardReleaseContext(hContext);
134
printf("Error connecting to reader %ld\n", rv);
139
* Now lets get some work done
142
SCardBeginTransaction(hCard);
150
if (fgets(line, sizeof(line), fp) == NULL)
154
if (sscanf(line_ptr, "%x", &x) == 0)
158
line_ptr = strchr(line_ptr, ' ') +1;
159
if (line_ptr == (char *)(NULL+1))
162
for (i = 0; i < dwSendLength; i++)
164
if (sscanf(line_ptr, "%x", &x) == 0)
166
printf("Corrupt APDU: %s\n", line);
167
SCardDisconnect(hCard, SCARD_RESET_CARD);
168
SCardReleaseContext(hContext);
173
line_ptr = strchr(line_ptr, ' ') +1;
175
if (line_ptr == (char *)(NULL+1))
179
printf("Processing Command %03d of length %03lX: %s", cnum,
182
memset(r, 0x00, MAX_BUFFER_SIZE);
183
dwRecvLength = MAX_BUFFER_SIZE;
187
rv = SCardTransmit(hCard, SCARD_PCI_T0, s, dwSendLength,
188
&sRecvPci, r, &dwRecvLength);
194
rv = SCardTransmit(hCard, SCARD_PCI_T1, s, dwSendLength,
195
&sRecvPci, r, &dwRecvLength);
199
printf("Invalid Protocol\n");
200
SCardDisconnect(hCard, SCARD_RESET_CARD);
201
SCardReleaseContext(hContext);
206
if (rv != SCARD_S_SUCCESS)
207
fprintf(fo, ".error %ld\n", rv);
210
fprintf(fo, "%02ld ", dwRecvLength);
212
for (i = 0; i < dwRecvLength; i++)
213
fprintf(fo, "%02X ", r[i]);
218
if (rv == SCARD_W_RESET_CARD)
220
SCardReconnect(hCard, SCARD_SHARE_SHARED,
221
SCARD_PROTOCOL_T0 | SCARD_PROTOCOL_T1,
222
SCARD_RESET_CARD, &dwPref);
228
SCardEndTransaction(hCard, SCARD_LEAVE_CARD);
229
SCardDisconnect(hCard, SCARD_UNPOWER_CARD);
230
SCardReleaseContext(hContext);