~ubuntu-branches/ubuntu/jaunty/pcsc-lite/jaunty-security

« back to all changes in this revision

Viewing changes to src/utils/formaticc.c

  • Committer: Bazaar Package Importer
  • Author(s): Ludovic Rousseau
  • Date: 2005-11-27 18:04:59 UTC
  • mfrom: (1.2.2 upstream)
  • Revision ID: james.westby@ubuntu.com-20051127180459-qrex2gzpq9d8jexd
Tags: 1.2.9-beta9-1
* New upstream version
* debian/compat: change from 3 to 4

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * MUSCLE SmartCard Development ( http://www.linuxnet.com )
 
3
 *
 
4
 * Copyright (C) 2000-2002
 
5
 *  David Corcoran <corcoran@linuxnet.com>
 
6
 *  Ludovic Rousseau <ludovic.rousseau@free.fr>
 
7
 *
 
8
 * $Id: formaticc.c 1421 2005-04-12 12:09:21Z rousseau $
 
9
 */
 
10
 
 
11
/**
 
12
 * @file
 
13
 * @brief This is an APDU robot for pcsc-lite.
 
14
 */
 
15
 
 
16
#include <stdio.h>
 
17
#include <stdlib.h>
 
18
#include <string.h>
 
19
 
 
20
#include <wintypes.h>
 
21
#include <winscard.h>
 
22
 
 
23
#ifndef MAXHOSTNAMELEN
 
24
#define MAXHOSTNAMELEN 64
 
25
#endif
 
26
 
 
27
int main(int argc, char *argv[])
 
28
{
 
29
        SCARDHANDLE hCard;
 
30
        SCARDCONTEXT hContext;
 
31
        SCARD_IO_REQUEST sRecvPci;
 
32
        SCARD_READERSTATE_A rgReaderStates[1];
 
33
        DWORD dwSendLength, dwRecvLength, dwPref, dwReaders;
 
34
        LPTSTR mszReaders;
 
35
        BYTE s[MAX_BUFFER_SIZE], r[MAX_BUFFER_SIZE];
 
36
        LPCTSTR mszGroups;
 
37
        LONG rv;
 
38
        FILE *fp;
 
39
        FILE *fo;
 
40
        int i, p, iReader, cnum, iProtocol;
 
41
        int iList[16];
 
42
        char pcHost[MAXHOSTNAMELEN];
 
43
        char pcAFile[FILENAME_MAX];
 
44
        char pcOFile[FILENAME_MAX];
 
45
        char line[80];
 
46
        char *line_ptr;
 
47
        unsigned int x;
 
48
 
 
49
        printf("\nWinscard PC/SC Lite Test Program\n\n");
 
50
 
 
51
        printf("Please enter the desired host (localhost for this machine) [localhost]: ");
 
52
        fgets(line, sizeof(line), stdin);
 
53
        if (line[0] == '\n')
 
54
                strncpy(pcHost, "localhost", sizeof(pcHost)-1);
 
55
        else
 
56
                strncpy(pcHost, line, sizeof(pcHost)-1);
 
57
 
 
58
        printf("Please input the desired transmit protocol (0/1) [0]: ");
 
59
        fgets(line, sizeof(line), stdin);
 
60
        if (line[0] == '\n')
 
61
                iProtocol = 0;
 
62
        else
 
63
                sscanf(line, "%d", &iProtocol);
 
64
 
 
65
        printf("Please input the desired input apdu file: ");
 
66
        fgets(line, sizeof(line), stdin);
 
67
        sscanf(line, "%s", pcAFile);
 
68
 
 
69
        printf("Please input the desired output apdu file: ");
 
70
        fgets(line, sizeof(line), stdin);
 
71
        sscanf(line, "%s", pcOFile);
 
72
 
 
73
        fp = fopen(pcAFile, "r");
 
74
        if (fp == NULL)
 
75
        {
 
76
                perror(pcAFile);
 
77
                return 1;
 
78
        }
 
79
 
 
80
        fo = fopen(pcOFile, "w");
 
81
        if (fo == NULL)
 
82
        {
 
83
                perror(pcOFile);
 
84
                return 1;
 
85
        }
 
86
 
 
87
        rv = SCardEstablishContext(SCARD_SCOPE_GLOBAL, pcHost, NULL, &hContext);
 
88
 
 
89
        if (rv != SCARD_S_SUCCESS)
 
90
        {
 
91
                printf("ERROR :: Cannot Connect to Resource Manager\n");
 
92
                return 1;
 
93
        }
 
94
 
 
95
        mszGroups = 0;
 
96
        SCardListReaders(hContext, mszGroups, 0, &dwReaders);
 
97
        mszReaders = (char *) malloc(sizeof(char) * dwReaders);
 
98
        SCardListReaders(hContext, mszGroups, mszReaders, &dwReaders);
 
99
 
 
100
        /*
 
101
         * Have to understand the multi-string here 
 
102
         */
 
103
        p = 0;
 
104
        for (i = 0; i < dwReaders - 1; i++)
 
105
        {
 
106
                ++p;
 
107
                printf("Reader %02d: %s\n", p, &mszReaders[i]);
 
108
                iList[p] = i;
 
109
                while (mszReaders[++i] != 0) ;
 
110
        }
 
111
 
 
112
        do
 
113
        {
 
114
                printf("Enter the desired reader number: ");
 
115
                fgets(line, sizeof(line), stdin);
 
116
                sscanf(line, "%d", &iReader);
 
117
                printf("\n");
 
118
 
 
119
                if (iReader > p || iReader <= 0)
 
120
                {
 
121
                        printf("Invalid Value - try again\n");
 
122
                }
 
123
        }
 
124
        while (iReader > p || iReader <= 0);
 
125
 
 
126
        rgReaderStates[0].szReader = &mszReaders[iList[iReader]];
 
127
        rgReaderStates[0].dwCurrentState = SCARD_STATE_EMPTY;
 
128
 
 
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,
 
133
                &hCard, &dwPref);
 
134
 
 
135
        if (rv != SCARD_S_SUCCESS)
 
136
        {
 
137
                SCardReleaseContext(hContext);
 
138
                printf("Error connecting to reader %ld\n", rv);
 
139
                return 1;
 
140
        }
 
141
 
 
142
        /*
 
143
         * Now lets get some work done 
 
144
         */
 
145
 
 
146
        SCardBeginTransaction(hCard);
 
147
 
 
148
        cnum = 0;
 
149
 
 
150
        do
 
151
        {
 
152
                cnum += 1;
 
153
 
 
154
                if (fgets(line, sizeof(line), fp) == NULL)
 
155
                        break;
 
156
 
 
157
                line_ptr = line;
 
158
                if (sscanf(line_ptr, "%x", &x) == 0)
 
159
                        break;
 
160
                dwSendLength = x;
 
161
 
 
162
                line_ptr = strchr(line_ptr, ' ');
 
163
                if (line_ptr == NULL)
 
164
                        break;
 
165
                line_ptr++;
 
166
 
 
167
                for (i = 0; i < dwSendLength; i++)
 
168
                {
 
169
                        if (sscanf(line_ptr, "%x", &x) == 0)
 
170
                        {
 
171
                                printf("Corrupt APDU: %s\n", line);
 
172
                                SCardDisconnect(hCard, SCARD_RESET_CARD);
 
173
                                SCardReleaseContext(hContext);
 
174
                                return 1;
 
175
                        }
 
176
                        s[i] = x;
 
177
 
 
178
                        line_ptr = strchr(line_ptr, ' ');
 
179
 
 
180
                        if (line_ptr == NULL)
 
181
                                break;
 
182
 
 
183
                        line_ptr++;
 
184
                }
 
185
 
 
186
                printf("Processing Command %03d of length %03lX: %s", cnum,
 
187
                        dwSendLength, line);
 
188
 
 
189
                memset(r, 0x00, MAX_BUFFER_SIZE);
 
190
                dwRecvLength = MAX_BUFFER_SIZE;
 
191
 
 
192
                if (iProtocol == 0)
 
193
                {
 
194
                        rv = SCardTransmit(hCard, SCARD_PCI_T0, s, dwSendLength,
 
195
                                &sRecvPci, r, &dwRecvLength);
 
196
                }
 
197
                else
 
198
                {
 
199
                        if (iProtocol == 1)
 
200
                        {
 
201
                                rv = SCardTransmit(hCard, SCARD_PCI_T1, s, dwSendLength,
 
202
                                        &sRecvPci, r, &dwRecvLength);
 
203
                        }
 
204
                        else
 
205
                        {
 
206
                                printf("Invalid Protocol\n");
 
207
                                SCardDisconnect(hCard, SCARD_RESET_CARD);
 
208
                                SCardReleaseContext(hContext);
 
209
                                return 1;
 
210
                        }
 
211
                }
 
212
 
 
213
                if (rv != SCARD_S_SUCCESS)
 
214
                        fprintf(fo, ".error %ld\n", rv);
 
215
                else
 
216
                {
 
217
                        fprintf(fo, "%02ld ", dwRecvLength);
 
218
 
 
219
                        for (i = 0; i < dwRecvLength; i++)
 
220
                                fprintf(fo, "%02X ", r[i]);
 
221
 
 
222
                        fprintf(fo, "\n");
 
223
                }
 
224
 
 
225
                if (rv == SCARD_W_RESET_CARD)
 
226
                {
 
227
                        SCardReconnect(hCard, SCARD_SHARE_SHARED,
 
228
                                SCARD_PROTOCOL_T0 | SCARD_PROTOCOL_T1,
 
229
                                SCARD_RESET_CARD, &dwPref);
 
230
                }
 
231
 
 
232
        }
 
233
        while (1);
 
234
 
 
235
        SCardEndTransaction(hCard, SCARD_LEAVE_CARD);
 
236
        SCardDisconnect(hCard, SCARD_UNPOWER_CARD);
 
237
        SCardReleaseContext(hContext);
 
238
 
 
239
        return 0;
 
240
}