~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: 2004-06-13 21:45:56 UTC
  • mfrom: (1.1.1 upstream)
  • mto: This revision was merged to the branch mainline in revision 3.
  • Revision ID: james.westby@ubuntu.com-20040613214556-zio7hrzkz9wwtffx
Tags: 1.2.9-beta2-2
* debian/rules: add -lpthread to LDFLAGS so that pthread_* symbols are
  included in the library (problem only seen on mips and mipsel).
  Closes: #253629
* debian/control: make libpcsclite-dev and libpcsclite1 at Priority:
  optional so that other packages at Priority: optional can use them.
  Closes: #249374

Show diffs side-by-side

added added

removed removed

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