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