formaticc.c

Go to the documentation of this file.
00001 /*
00002  * MUSCLE SmartCard Development ( http://www.linuxnet.com )
00003  *
00004  * Copyright (C) 2000-2002
00005  *  David Corcoran <corcoran@linuxnet.com>
00006  *  Ludovic Rousseau <ludovic.rousseau@free.fr>
00007  *
00008  * $Id: formaticc.c 1421 2005-04-12 12:09:21Z rousseau $
00009  */
00010 
00016 #include <stdio.h>
00017 #include <stdlib.h>
00018 #include <string.h>
00019 
00020 #include <wintypes.h>
00021 #include <winscard.h>
00022 
00023 #ifndef MAXHOSTNAMELEN
00024 #define MAXHOSTNAMELEN 64
00025 #endif
00026 
00027 int main(int argc, char *argv[])
00028 {
00029     SCARDHANDLE hCard;
00030     SCARDCONTEXT hContext;
00031     SCARD_IO_REQUEST sRecvPci;
00032     SCARD_READERSTATE_A rgReaderStates[1];
00033     DWORD dwSendLength, dwRecvLength, dwPref, dwReaders;
00034     LPTSTR mszReaders;
00035     BYTE s[MAX_BUFFER_SIZE], r[MAX_BUFFER_SIZE];
00036     LPCTSTR mszGroups;
00037     LONG rv;
00038     FILE *fp;
00039     FILE *fo;
00040     int i, p, iReader, cnum, iProtocol;
00041     int iList[16];
00042     char pcHost[MAXHOSTNAMELEN];
00043     char pcAFile[FILENAME_MAX];
00044     char pcOFile[FILENAME_MAX];
00045     char line[80];
00046     char *line_ptr;
00047     unsigned int x;
00048 
00049     printf("\nWinscard PC/SC Lite Test Program\n\n");
00050 
00051     printf("Please enter the desired host (localhost for this machine) [localhost]: ");
00052     fgets(line, sizeof(line), stdin);
00053     if (line[0] == '\n')
00054         strncpy(pcHost, "localhost", sizeof(pcHost)-1);
00055     else
00056         strncpy(pcHost, line, sizeof(pcHost)-1);
00057 
00058     printf("Please input the desired transmit protocol (0/1) [0]: ");
00059     fgets(line, sizeof(line), stdin);
00060     if (line[0] == '\n')
00061         iProtocol = 0;
00062     else
00063         sscanf(line, "%d", &iProtocol);
00064 
00065     printf("Please input the desired input apdu file: ");
00066     fgets(line, sizeof(line), stdin);
00067     sscanf(line, "%s", pcAFile);
00068 
00069     printf("Please input the desired output apdu file: ");
00070     fgets(line, sizeof(line), stdin);
00071     sscanf(line, "%s", pcOFile);
00072 
00073     fp = fopen(pcAFile, "r");
00074     if (fp == NULL)
00075     {
00076         perror(pcAFile);
00077         return 1;
00078     }
00079 
00080     fo = fopen(pcOFile, "w");
00081     if (fo == NULL)
00082     {
00083         perror(pcOFile);
00084         return 1;
00085     }
00086 
00087     rv = SCardEstablishContext(SCARD_SCOPE_GLOBAL, pcHost, NULL, &hContext);
00088 
00089     if (rv != SCARD_S_SUCCESS)
00090     {
00091         printf("ERROR :: Cannot Connect to Resource Manager\n");
00092         return 1;
00093     }
00094 
00095     mszGroups = 0;
00096     SCardListReaders(hContext, mszGroups, 0, &dwReaders);
00097     mszReaders = (char *) malloc(sizeof(char) * dwReaders);
00098     SCardListReaders(hContext, mszGroups, mszReaders, &dwReaders);
00099 
00100     /*
00101      * Have to understand the multi-string here 
00102      */
00103     p = 0;
00104     for (i = 0; i < dwReaders - 1; i++)
00105     {
00106         ++p;
00107         printf("Reader %02d: %s\n", p, &mszReaders[i]);
00108         iList[p] = i;
00109         while (mszReaders[++i] != 0) ;
00110     }
00111 
00112     do
00113     {
00114         printf("Enter the desired reader number: ");
00115         fgets(line, sizeof(line), stdin);
00116         sscanf(line, "%d", &iReader);
00117         printf("\n");
00118 
00119         if (iReader > p || iReader <= 0)
00120         {
00121             printf("Invalid Value - try again\n");
00122         }
00123     }
00124     while (iReader > p || iReader <= 0);
00125 
00126     rgReaderStates[0].szReader = &mszReaders[iList[iReader]];
00127     rgReaderStates[0].dwCurrentState = SCARD_STATE_EMPTY;
00128 
00129     printf("Please insert a smart card\n");
00130     SCardGetStatusChange(hContext, INFINITE, rgReaderStates, 1);
00131     rv = SCardConnect(hContext, &mszReaders[iList[iReader]],
00132         SCARD_SHARE_SHARED, SCARD_PROTOCOL_T0 | SCARD_PROTOCOL_T1,
00133         &hCard, &dwPref);
00134 
00135     if (rv != SCARD_S_SUCCESS)
00136     {
00137         SCardReleaseContext(hContext);
00138         printf("Error connecting to reader %ld\n", rv);
00139         return 1;
00140     }
00141 
00142     /*
00143      * Now lets get some work done 
00144      */
00145 
00146     SCardBeginTransaction(hCard);
00147 
00148     cnum = 0;
00149 
00150     do
00151     {
00152         cnum += 1;
00153 
00154         if (fgets(line, sizeof(line), fp) == NULL)
00155             break;
00156 
00157         line_ptr = line;
00158         if (sscanf(line_ptr, "%x", &x) == 0)
00159             break;
00160         dwSendLength = x;
00161 
00162         line_ptr = strchr(line_ptr, ' ');
00163         if (line_ptr == NULL)
00164             break;
00165         line_ptr++;
00166 
00167         for (i = 0; i < dwSendLength; i++)
00168         {
00169             if (sscanf(line_ptr, "%x", &x) == 0)
00170             {
00171                 printf("Corrupt APDU: %s\n", line);
00172                 SCardDisconnect(hCard, SCARD_RESET_CARD);
00173                 SCardReleaseContext(hContext);
00174                 return 1;
00175             }
00176             s[i] = x;
00177 
00178             line_ptr = strchr(line_ptr, ' ');
00179 
00180             if (line_ptr == NULL)
00181                 break;
00182 
00183             line_ptr++;
00184         }
00185 
00186         printf("Processing Command %03d of length %03lX: %s", cnum,
00187             dwSendLength, line);
00188 
00189         memset(r, 0x00, MAX_BUFFER_SIZE);
00190         dwRecvLength = MAX_BUFFER_SIZE;
00191 
00192         if (iProtocol == 0)
00193         {
00194             rv = SCardTransmit(hCard, SCARD_PCI_T0, s, dwSendLength,
00195                 &sRecvPci, r, &dwRecvLength);
00196         }
00197         else
00198         {
00199             if (iProtocol == 1)
00200             {
00201                 rv = SCardTransmit(hCard, SCARD_PCI_T1, s, dwSendLength,
00202                     &sRecvPci, r, &dwRecvLength);
00203             }
00204             else
00205             {
00206                 printf("Invalid Protocol\n");
00207                 SCardDisconnect(hCard, SCARD_RESET_CARD);
00208                 SCardReleaseContext(hContext);
00209                 return 1;
00210             }
00211         }
00212 
00213         if (rv != SCARD_S_SUCCESS)
00214             fprintf(fo, ".error %ld\n", rv);
00215         else
00216         {
00217             fprintf(fo, "%02ld ", dwRecvLength);
00218 
00219             for (i = 0; i < dwRecvLength; i++)
00220                 fprintf(fo, "%02X ", r[i]);
00221 
00222             fprintf(fo, "\n");
00223         }
00224 
00225         if (rv == SCARD_W_RESET_CARD)
00226         {
00227             SCardReconnect(hCard, SCARD_SHARE_SHARED,
00228                 SCARD_PROTOCOL_T0 | SCARD_PROTOCOL_T1,
00229                 SCARD_RESET_CARD, &dwPref);
00230         }
00231 
00232     }
00233     while (1);
00234 
00235     SCardEndTransaction(hCard, SCARD_LEAVE_CARD);
00236     SCardDisconnect(hCard, SCARD_UNPOWER_CARD);
00237     SCardReleaseContext(hContext);
00238 
00239     return 0;
00240 }

Generated on Sat Apr 22 16:38:15 2006 for pcsc-lite by  doxygen 1.4.6