diff --git a/README.md b/README.md index 48e3b58..40d012c 100644 --- a/README.md +++ b/README.md @@ -21,18 +21,37 @@ The APDUPlay project is providing following functionality: See more details at https://github.com/petrs/APDUPlay/wiki. ## Installation -1. Find out if your targeted application is 32- or 64-bit [(howto)](https://superuser.com/questions/103071/quick-way-to-tell-if-an-installed-application-is-64-bit-or-32-bit#103073). -1. Copy Winscard.dll from your system folder (c:\Windows\winscard.dll for 32-bit target application or c:\Windows\SysWOW64\winscard.dll for 64-bit application) to the folder with target application and rename it to original32.dll or original64.dll respectively +1. Find out if your targeted application is 32- or 64-bit [(Use Microsoft Sysinternals Sigcheck utility)](https://docs.microsoft.com/en-us/sysinternals/downloads/sigcheck). Run sigcheck.exe targetApp.exe and look for MachineType: 32-bit or 64-bit (works also for dll files) +1. Copy Winscard.dll from your system folder (c:\Windows\System32\winscard.dll for 64-bit target application (if you are running 64-bit OS) or c:\Windows\SysWOW64\winscard.dll for 32-bit application) to the folder with target application and rename it to original32.dll or original64.dll respectively. NOTE: c:\Windows\System32\ will contain either 32-bit or 64-bit version based on your OS. 2. Copy Winscard.dll from APDUPlay project to the folder with target application (the folder should contain winscard.dll binary from APDUPlay project AND originalXX.dll which is original Microsoft's winscard.dll) 3. Run the application and inspect resulting files winscard_log.txt and winscard_rules_log.txt 4. (Optional) Change configuration file winscard_rules.txt to modify default behavior (see below) -## Troubleshooting +## Examples - * If you use (wrongly) 64bit version of library Microsoft's winscard.dll (renamed as original32.dll), it will fail with "The procedure entry point original.g_rgSCardT1Pci could not be located in the dynamic link library WinSCard.dll." - +The localization of correct winscard.dll path can be tedious task for some applications. Here are some examples with increasing difficulty: + 1. Simple application directly using winscard.dll (CAProfiler) + 2. Application with persistent agent (gpg2.exe --card-edit) + 3. Java-based application accessing smartcards via JRE: (GlobalPlatformPro gp -l) +## Troubleshooting + + * Problem: After running target application, the following error message is displayed: "The procedure entry point original.g_rgSCardT1Pci could not be located in the dynamic link library WinSCard.dll.". You likely mismatched 64-bit and 32-bit versions of APDUPlay's winscard.dll and Microsoft's original library (renamed as original32.dll). Use [Microsoft Sysinternals Sigcheck utility](https://docs.microsoft.com/en-us/sysinternals/downloads/sigcheck) to verify that both libraries as either 64-bit or 32-bit (based on your target application needs). + + * Problem: Target application is (probably) not loading modified winscard.dll from APDUPlay project, but uses standard Microsoft's one from system folder (no files with logged communication are created). Use [Process Monitor utility]( https://docs.microsoft.com/en-us/sysinternals/downloads/procmon) from Microsoft to find location of loaded libraries (use Filter option to limit results only to target application: CTRL+L -> Process Name is 'targetApp.exe' -> Add). Search for event 'Load Image path_to_folder\Winscard.dll'. The path_to_folder should point to APDUPlay's version of winscard.dll, not Microsoft one. + + * Problem: Logging seems to work, but only for the first of application. When started again, changes done to winscard_rules.txt does not apply. Target application might have persistent component (e.g., GPG have gpg-agent.exe) which loads the dll (and rules from winscard_rules.txt) and runs even when target application is terminated. Try to locate and kill this component, or restart computer (will force component to restart again). + + * Problem: Target application always opens winscard.dll from system folders (either system32 or sysWOW64 folder). +Run cmd as admin, then: +``` +cd target_folder (either system32 or sysWOW64) +takeown /f winscard.dll +cacls winscard.dll /G your_username:F +rename winscard.dll to winscard_MS.dll (winscard.dll might be currently used by some other process so direct copy woudl not be possible) +copy APDUPlay's winscard.dll instead winscard.dll +``` Please, open an issue in case of any bug found. diff --git a/Winscard/CommonFnc.cpp b/Winscard/CommonFnc.cpp index 4a4ff35..0489001 100644 --- a/Winscard/CommonFnc.cpp +++ b/Winscard/CommonFnc.cpp @@ -224,8 +224,7 @@ int CCommonFnc::File_SaveMatrixInt(string_type filePath, INT_DATA_BLOB* pBlob, s } else { for (i = startOffset; i < endOffset; i++) { - //value.Format("%d\r\n", pBlob->pData[i]); - value = string_format(_CONV("%d\r\n"), pBlob->pData[i]); + value = string_format(_CONV("%d\n"), pBlob->pData[i]); values += value; if ((i % NUM_VALUES_PER_WRITE) == 0) { diff --git a/Winscard/ReadMe.txt b/Winscard/ReadMe.txt index 18ac1fb..e4c2211 100644 --- a/Winscard/ReadMe.txt +++ b/Winscard/ReadMe.txt @@ -34,7 +34,7 @@ res\Winscard.rc2 Visual C++. You should place all resources not editable by the resource editor in this file. -Winscard.def +Winscard32.def or Winscard64.def This file contains information about the DLL that must be provided to run with Microsoft Windows. It defines parameters such as the name and description of the DLL. It also exports diff --git a/Winscard/Winscard.cpp b/Winscard/Winscard.cpp index 5ee2e35..5bf4cf8 100644 --- a/Winscard/Winscard.cpp +++ b/Winscard/Winscard.cpp @@ -62,16 +62,6 @@ Please, report any bugs to author #include "wintypes.h" #endif -/* -BOOL bAUTO_REQUEST_DATA = TRUE; // DEFAULT: FALSE, SET TO TRUE IF APPLICATION IS NOT ABLE TO HANDLE GET DATA (00 0c 00 00 lc) COMMAND ON ITS OWN -BOOL bFORCE_CONNECT_SHARED_MODE = TRUE; // DEFAULT: FALSE, SET TO TRUE IF SOME APPLICATION IS BLOCKING CARD -BOOL bFORCE_APDU_NONZERO_INPUT_DATA = TRUE; // DEFAULT: FALSE, SET TO TRUE IF APPLET HAVE PROBLEM TO RETURN DATA (apdu.setOutgoingAndSend()) WHEN NO PREVIOUS setIncomingAndReceive() WAS CALLED. -BOOL bLOG_EXCHANGED_APDU = TRUE; // DEFAULT: FALSE, SET TO TRUE IF LOGGING OF APDU DATA IS REQUIRED -BOOL bMODIFY_APDU_BY_RULES = TRUE; // DEFAULT: FALSE, SET TO TRUE . -BOOL bLOG_FUNCTIONS_CALLS = TRUE; // DEFAULT: FALSE, SET TO TRUE . -/**/ - - /* extern SCard (.*?) STDCALL.*?(SCard.*?)\((.*?)\); static SCard \1 (STDCALL *Original_\2) @@ -80,7 +70,12 @@ static SCard \1 (STDCALL *Original_\2) /**/ #pragma warning(disable:4996) -static string_type ENV_WINSCARD_RULES_PATH = _CONV("APDUPLAY"); +static string_type LIBRARY_VERSION = _CONV("2.0.1"); + +static string_type ENV_APDUPLAY_WINSCARD_RULES_PATH = _CONV("APDUPLAY"); +static string_type ENV_APDUPLAY_DEBUG_PATH = _CONV("APDUPLAY_DEBUG"); +static string_type APDUPLAY_DEBUG_FILE = _CONV("c:\\Temp\\apduplay_debug.txt"); + static string_type RULE_FILE = _CONV("winscard_rules.txt"); static string_type WINSCARD_RULES_LOG = _CONV("winscard_rules_log.txt"); static string_type WINSCARD_LOG = _CONV("winscard_log.txt"); @@ -104,11 +99,9 @@ BYTE GET_APDU1[] = { 0x00, 0xC0, 0x00, 0x00 }; BYTE GET_APDU2[] = { 0xC0, 0xC0, 0x00, 0x00 }; //#define VIRT_READER_NAME "VirtOpenPGP" -#define VIRT_READER_NAME "Simona /111.222.123.033@07\nSimona /111.222.123.033@12\nSimona /111.222.123.033@123" +#define VIRT_READER_NAME "Simona /111.222.123.033@07" #define VIRTUAL_READERS_LEN strlen(VIRT_READER_NAME) -#define REMOTE_READER_PREFIX "Simona" - #define CMD_APDU "APDU" #define CMD_RESET "RESET" #define CMD_ENUM "ENUM" @@ -133,8 +126,8 @@ int apduCounter = 0; /* The following values variables MUST be defined here, but MUST NOT be referenced in this or any other program module. The DEF file is set to forward their linkage -to the "original.dll". If we need the data that these variables should be pointing -to, we must GetProcAddress on "original.dll" and use the data there. +to the "originalxx.dll". If we need the data that these variables should be pointing +to, we must GetProcAddress on "originalxx.dll" and use the data there. */ #if defined(_WIN32) @@ -154,12 +147,28 @@ const SCARD_IO_REQUEST g_rgSCardT0Pci, g_rgSCardT1Pci, g_rgSCardRawPci; /* ******************************************************************************* */ +void LogDebugString(string_type message, bool bInsertTime = true) { + string_type logLine; + + if (bInsertTime) { + string_type date_and_time = getCurrentTimeString(); + CCommonFnc::File_AppendString(APDUPLAY_DEBUG_FILE, string_format(_CONV("%s: %s"), date_and_time.c_str(), message.c_str())); + } + else { + CCommonFnc::File_AppendString(APDUPLAY_DEBUG_FILE, message); + } +} + +void LogWinscardRules(string_type message) { + LogDebugString(message); + CCommonFnc::File_AppendString(WINSCARD_RULES_LOG, message); +} void DumpMemory(LPCBYTE location, DWORD length) { string_type message; CCommonFnc::BYTE_ConvertFromArrayToHexString((BYTE*)location, length, &message); CCommonFnc::File_AppendString(WINSCARD_LOG, message); - CCommonFnc::File_AppendString(WINSCARD_LOG, _CONV("\r\n")); + CCommonFnc::File_AppendString(WINSCARD_LOG, _CONV("\n")); } static SCard LONG(STDCALL *Original_SCardEstablishContext)( @@ -177,10 +186,10 @@ SCard LONG STDCALL SCardEstablishContext( ) { string_type message; message = string_format(_CONV("SCardEstablishContext() called\n")); - if (theApp.m_winscardConfig.bLOG_FUNCTIONS_CALLS) CCommonFnc::File_AppendString(WINSCARD_RULES_LOG, message); + LogWinscardRules(message); LONG status = (*Original_SCardEstablishContext)(dwScope, pvReserved1, pvReserved2, phContext); message = string_format(_CONV("-> hContext:0x%x\n"), *phContext); - if (theApp.m_winscardConfig.bLOG_FUNCTIONS_CALLS) CCommonFnc::File_AppendString(WINSCARD_RULES_LOG, message); + LogWinscardRules(message); return status; } @@ -194,7 +203,7 @@ SCard LONG STDCALL SCardReleaseContext( ) { string_type message; message = string_format(_CONV("SCardReleaseContext(hContext:0x%x) called\n"), hContext); - if (theApp.m_winscardConfig.bLOG_FUNCTIONS_CALLS) CCommonFnc::File_AppendString(WINSCARD_RULES_LOG, message); + LogWinscardRules(message); return (*Original_SCardReleaseContext)(hContext); } @@ -208,7 +217,7 @@ SCard LONG STDCALL SCardIsValidContext( ) { string_type message; message = string_format(_CONV("SCardIsValidContext(hContext:0x%x) called\n"), hContext); - if (theApp.m_winscardConfig.bLOG_FUNCTIONS_CALLS) CCommonFnc::File_AppendString(WINSCARD_RULES_LOG, message); + LogWinscardRules(message); return (*Original_SCardIsValidContext)(hContext); } @@ -219,7 +228,7 @@ static SCard LONG(STDCALL *Original_SCardCancel)( SCard LONG STDCALL SCardCancel( IN SCARDCONTEXT hContext ) { - if (theApp.m_winscardConfig.bLOG_FUNCTIONS_CALLS) CCommonFnc::File_AppendString(WINSCARD_RULES_LOG, _CONV("SCardCancel called\n")); + LogWinscardRules(_CONV("SCardCancel called\n")); return (*Original_SCardCancel)(hContext); } @@ -240,8 +249,14 @@ SCard LONG STDCALL SCardReconnect( ) { string_type message; message = string_format(_CONV("SCardReconnect(hCard:0x%x) called\n"), hCard); - if (theApp.m_winscardConfig.bLOG_FUNCTIONS_CALLS) CCommonFnc::File_AppendString(WINSCARD_RULES_LOG, message); - return (*Original_SCardReconnect)(hCard, dwShareMode, dwPreferredProtocols, dwInitialization, pdwActiveProtocol); + LogWinscardRules(message); + if (theApp.IsRemoteCard(hCard)) { + return SCARD_S_SUCCESS; + *pdwActiveProtocol = SCARD_PROTOCOL_T1; + } + else { + return (*Original_SCardReconnect)(hCard, dwShareMode, dwPreferredProtocols, dwInitialization, pdwActiveProtocol); + } } static SCard LONG(STDCALL *Original_SCardBeginTransaction)( @@ -251,8 +266,13 @@ static SCard LONG(STDCALL *Original_SCardBeginTransaction)( SCard LONG STDCALL SCardBeginTransaction( IN SCARDHANDLE hCard ) { - CCommonFnc::File_AppendString(WINSCARD_RULES_LOG, _CONV("SCardBeginTransaction called\n")); - return (*Original_SCardBeginTransaction)(hCard); + LogWinscardRules(_CONV("SCardBeginTransaction called\n")); + if (theApp.IsRemoteCard(hCard)) { + return SCARD_S_SUCCESS; + } + else { + return (*Original_SCardBeginTransaction)(hCard); + } } @@ -265,8 +285,13 @@ SCard LONG STDCALL SCardEndTransaction( IN SCARDHANDLE hCard, IN DWORD dwDisposition ) { - if (theApp.m_winscardConfig.bLOG_FUNCTIONS_CALLS) CCommonFnc::File_AppendString(WINSCARD_RULES_LOG, _CONV("SCardEndTransaction called\n")); - return (*Original_SCardEndTransaction)(hCard, dwDisposition); + LogWinscardRules(_CONV("SCardEndTransaction called\n")); + if (theApp.IsRemoteCard(hCard)) { + return SCARD_S_SUCCESS; + } + else { + return (*Original_SCardEndTransaction)(hCard, dwDisposition); + } } static SCard LONG(STDCALL *Original_SCardControl)( @@ -288,8 +313,13 @@ SCard LONG STDCALL SCardControl( IN DWORD nOutBufferSize, OUT LPDWORD lpBytesReturned ) { - if (theApp.m_winscardConfig.bLOG_FUNCTIONS_CALLS) CCommonFnc::File_AppendString(WINSCARD_RULES_LOG, _CONV("SCardControl called\n")); - return (*Original_SCardControl)(hCard, dwControlCode, lpInBuffer, nInBufferSize, lpOutBuffer, nOutBufferSize, lpBytesReturned); + LogWinscardRules(_CONV("SCardControl called\n")); + if (theApp.IsRemoteCard(hCard)) { + return SCARD_S_SUCCESS; + } + else { + return (*Original_SCardControl)(hCard, dwControlCode, lpInBuffer, nInBufferSize, lpOutBuffer, nOutBufferSize, lpBytesReturned); + } } @@ -306,8 +336,13 @@ SCard LONG STDCALL SCardGetAttrib( OUT LPBYTE pbAttr, IN OUT LPDWORD pcbAttrLen ) { - if (theApp.m_winscardConfig.bLOG_FUNCTIONS_CALLS) CCommonFnc::File_AppendString(WINSCARD_RULES_LOG, _CONV("SCardGetAttrib called\n")); - return (*Original_SCardGetAttrib)(hCard, dwAttrId, pbAttr, pcbAttrLen); + LogWinscardRules(_CONV("SCardGetAttrib called\n")); + if (theApp.IsRemoteCard(hCard)) { + return SCARD_S_SUCCESS; + } + else { + return (*Original_SCardGetAttrib)(hCard, dwAttrId, pbAttr, pcbAttrLen); + } } @@ -324,8 +359,13 @@ SCard LONG STDCALL SCardSetAttrib( IN LPCBYTE pbAttr, IN DWORD cbAttrLen ) { - if (theApp.m_winscardConfig.bLOG_FUNCTIONS_CALLS) CCommonFnc::File_AppendString(WINSCARD_RULES_LOG, _CONV("SCardSetAttrib called\n")); - return (*Original_SCardSetAttrib)(hCard, dwAttrId, pbAttr, cbAttrLen); + LogWinscardRules(_CONV("SCardSetAttrib called\n")); + if (theApp.IsRemoteCard(hCard)) { + return SCARD_S_SUCCESS; + } + else { + return (*Original_SCardSetAttrib)(hCard, dwAttrId, pbAttr, cbAttrLen); + } } /* ******************************************************************************* */ @@ -341,7 +381,7 @@ CWinscardApp::~CWinscardApp() FreeLibrary(hOriginal); #endif // Reference to WINSCARD_LOG will fail with access to 0xfeefee (global CString WINSCARD_LOG does not exists at the time of dll release (strange)) - // if (theApp.m_winscardConfig.bLOG_EXCHANGED_APDU) CCommonFnc::File_AppendString(WINSCARD_LOG, "[end]\r\n"); + // if (theApp.m_winscardConfig.bLOG_EXCHANGED_APDU) CCommonFnc::File_AppendString(WINSCARD_LOG, "[end]\n"); #if defined(_WIN32) if (m_remoteConfig.pSocket != NULL) delete m_remoteConfig.pSocket; @@ -433,7 +473,7 @@ SCard LONG STDCALL SCardFreeMemory( { string_type message; message = string_format(_CONV("SCardFreeMemory(hContext:0x%x) called\n"), hContext); - if (theApp.m_winscardConfig.bLOG_FUNCTIONS_CALLS) CCommonFnc::File_AppendString(WINSCARD_RULES_LOG, message); + LogWinscardRules(message); LONG status = SCARD_S_SUCCESS; @@ -475,11 +515,10 @@ SCard LONG STDCALL SCardDisconnect( SCARDHANDLE hCard, DWORD dwDisposition) { - if (theApp.m_winscardConfig.bLOG_FUNCTIONS_CALLS) CCommonFnc::File_AppendString(WINSCARD_RULES_LOG, _CONV("SCardDisconnect called\n")); + LogWinscardRules(_CONV("SCardDisconnect called\n")); // DISCONNECT FROM CARD - if (hCard == HANDLE_VIRTUAL_CARD) { - // DO NOTHING + if (theApp.IsRemoteCard(hCard)) { return SCARD_S_SUCCESS; } else { @@ -507,13 +546,13 @@ SCard LONG STDCALL SCardTransmit( IN OUT LPDWORD pcbRecvLength ) { - if (theApp.m_winscardConfig.bLOG_FUNCTIONS_CALLS) CCommonFnc::File_AppendString(WINSCARD_RULES_LOG, _CONV("SCardTransmit called\n")); + LogWinscardRules(_CONV("SCardTransmit called\n")); LONG result = SCARD_S_SUCCESS; // DWORD written; char_type *txMsg = _CONV("transmitted:"); char_type *rxMsg = _CONV("received:"); - char_type *crlf = _CONV("\r\n"); + char_type *crlf = _CONV("\n"); const int bufferLength = 1024; //char_type buffer[bufferLength]; string_type buffer; @@ -528,16 +567,16 @@ SCard LONG STDCALL SCardTransmit( //elapsedLibrary = -clock(); auto lib_timestamp1 = Clock::now(); if (theApp.m_winscardConfig.bLOG_EXCHANGED_APDU) { - //sprintf(buffer, "SCardTransmit (handle 0x%0.8X)#\r\n", hCard); - buffer = string_format(_CONV("SCardTransmit (handle 0x%0.8X)#\r\n"), hCard); + //sprintf(buffer, "SCardTransmit (handle 0x%0.8X)#\n", hCard); + buffer = string_format(_CONV("SCardTransmit (handle 0x%0.8X)#\n"), hCard); CCommonFnc::File_AppendString(WINSCARD_LOG, buffer); - //sprintf(buffer, "apduCounter:%d#\r\n", apduCounter); - buffer = string_format(_CONV("apduCounter:%d#\r\n"), apduCounter); + //sprintf(buffer, "apduCounter:%d#\n", apduCounter); + buffer = string_format(_CONV("apduCounter:%d#\n"), apduCounter); CCommonFnc::File_AppendString(WINSCARD_LOG, buffer); - //sprintf(buffer, "totalBytesINCounter:%d#\r\n", theApp.m_processedApduByteCounter + 1); - buffer = string_format(_CONV("totalBytesINCounter:%d#\r\n"), theApp.m_processedApduByteCounter + 1); + //sprintf(buffer, "totalBytesINCounter:%d#\n", theApp.m_processedApduByteCounter + 1); + buffer = string_format(_CONV("totalBytesINCounter:%d#\n"), theApp.m_processedApduByteCounter + 1); CCommonFnc::File_AppendString(WINSCARD_LOG, buffer); if(theApp.m_winscardConfig.bLOG_WRITE_DESCRIPTION) @@ -559,11 +598,11 @@ SCard LONG STDCALL SCardTransmit( if (theApp.m_winscardConfig.bMODIFY_APDU_BY_RULES) { message = string_format(_CONV("\nIncoming rules applied for apduCounter %d: \n"), apduCounter); - CCommonFnc::File_AppendString(WINSCARD_RULES_LOG, message); + LogWinscardRules(message); CCommonFnc::BYTE_ConvertFromArrayToHexString((BYTE*)pbSendBuffer, cbSendLength, &message); //message.Insert(0, " "); message += "\n"; message.insert(0, _CONV(" ")); message += _CONV("\n"); - if (theApp.m_winscardConfig.bLOG_EXCHANGED_APDU) CCommonFnc::File_AppendString(WINSCARD_RULES_LOG, message); + if (theApp.m_winscardConfig.bLOG_EXCHANGED_APDU) LogWinscardRules(message); } // COPY INPUT DATA @@ -575,7 +614,7 @@ SCard LONG STDCALL SCardTransmit( CCommonFnc::BYTE_ConvertFromArrayToHexString((BYTE*)sendBuffer, cbSendLength, &message); //message.Insert(0, " "); message += "\n"; message.insert(0, _CONV(" ")); message += _CONV("\n"); - if (theApp.m_winscardConfig.bLOG_EXCHANGED_APDU) CCommonFnc::File_AppendString(WINSCARD_RULES_LOG, message); + if (theApp.m_winscardConfig.bLOG_EXCHANGED_APDU) LogWinscardRules(message); } //elapsedCard = -clock(); @@ -660,12 +699,12 @@ SCard LONG STDCALL SCardTransmit( auto card_timestamp2 = Clock::now(); auto elapsedCard = std::chrono::duration_cast(card_timestamp2 - card_timestamp1).count(); if (theApp.m_winscardConfig.bLOG_EXCHANGED_APDU) { - //sprintf(buffer, "responseTime:%d#\r\n", elapsedCard); - buffer = string_format(_CONV("responseTime:%d#\r\n"), elapsedCard); + //sprintf(buffer, "responseTime:%d#\n", elapsedCard); + buffer = string_format(_CONV("responseTime:%d#\n"), elapsedCard); CCommonFnc::File_AppendString(WINSCARD_LOG, buffer); - //sprintf(buffer, "SCardTransmit result:0x%x#\r\n", result); - buffer = string_format(_CONV("SCardTransmit result:0x%x#\r\n"), result); + //sprintf(buffer, "SCardTransmit result:0x%x#\n", result); + buffer = string_format(_CONV("SCardTransmit result:0x%x#\n"), result); CCommonFnc::File_AppendString(WINSCARD_LOG, buffer); } @@ -687,17 +726,17 @@ SCard LONG STDCALL SCardTransmit( if (theApp.m_winscardConfig.bMODIFY_APDU_BY_RULES) { message = string_format(_CONV("\nOutgoing rules applied for apduCounter %d: \n"), apduCounter); - CCommonFnc::File_AppendString(WINSCARD_RULES_LOG, message); + LogWinscardRules(message); CCommonFnc::BYTE_ConvertFromArrayToHexString(pbRecvBuffer, *pcbRecvLength, &message); //message.Insert(0, " "); message += "\n"; message.insert(0, _CONV(" ")); message += _CONV("\n"); - if (theApp.m_winscardConfig.bLOG_EXCHANGED_APDU) CCommonFnc::File_AppendString(WINSCARD_RULES_LOG, message); + if (theApp.m_winscardConfig.bLOG_EXCHANGED_APDU) LogWinscardRules(message); // APPLY OUTGOING RULES if (theApp.m_winscardConfig.bMODIFY_APDU_BY_RULES) theApp.ApplyRules(pbRecvBuffer, pcbRecvLength, OUTPUT_APDU); CCommonFnc::BYTE_ConvertFromArrayToHexString(pbRecvBuffer, *pcbRecvLength, &message); //message.Insert(0, " "); message += "\n"; message.insert(0, _CONV(" ")); message += _CONV("\n"); - if (theApp.m_winscardConfig.bLOG_EXCHANGED_APDU) CCommonFnc::File_AppendString(WINSCARD_RULES_LOG, message); + if (theApp.m_winscardConfig.bLOG_EXCHANGED_APDU) LogWinscardRules(message); } @@ -708,9 +747,8 @@ SCard LONG STDCALL SCardTransmit( auto lib_timestamp2 = Clock::now(); auto elapsedLibrary = std::chrono::duration_cast(lib_timestamp2 - lib_timestamp1).count(); if (theApp.m_winscardConfig.bLOG_EXCHANGED_APDU) { - message = string_format(_CONV("responseTimeLibrary:%d#\r\n"), elapsedLibrary); - CCommonFnc::File_AppendString(WINSCARD_RULES_LOG, message); - CCommonFnc::File_AppendString(WINSCARD_RULES_LOG, _CONV("...............................................\r\n")); + message = string_format(_CONV("responseTimeLibrary:%d#\n"), elapsedLibrary); + LogWinscardRules(message); } return result; @@ -738,9 +776,9 @@ SCard LONG STDCALL SCardConnect( dwShareMode = SCARD_SHARE_SHARED; } - // Detect remote cards (now only via reader prefix and assign virtual card handle) + // Detect remote cards (now only via reader prefix) and assign virtual card handle string_type readerName = szReader; - if (readerName.find(REMOTE_READER_PREFIX) != -1) { + if (readerName.find(theApp.m_remoteConfig.remoteReaderPrefix) != -1) { theApp.m_nextRemoteCardID++; *phCard = theApp.m_nextRemoteCardID; theApp.remoteReadersMap[*phCard] = szReader; @@ -751,7 +789,7 @@ SCard LONG STDCALL SCardConnect( status = (*Original_SCardConnect)(hContext, szReader, dwShareMode, dwPreferredProtocols, phCard, pdwActiveProtocol); string_type message; message = string_format(_CONV("SCardConnect(hContext:0x%x,%s,hCard:0x%x) called\n"), hContext, szReader, *phCard); - if (theApp.m_winscardConfig.bLOG_FUNCTIONS_CALLS) CCommonFnc::File_AppendString(WINSCARD_RULES_LOG, message); + LogWinscardRules(message); } // Store mapping between card handle and reader (used in card remoting) @@ -779,8 +817,18 @@ SCard LONG STDCALL SCardStatus( LPBYTE pbAtr, LPDWORD pcbAtrLen) { - if (theApp.m_winscardConfig.bLOG_FUNCTIONS_CALLS) CCommonFnc::File_AppendString(WINSCARD_RULES_LOG, _CONV("SCardStatus called\n")); - return (*Original_SCardStatus)(hCard, szReaderName, pcchReaderLen, pdwState, pdwProtocol, pbAtr, pcbAtrLen); + LogWinscardRules(string_format(_CONV("SCardStatus(hCard:0x%x,pcchReaderLen:%d,pcbAtrLen:%d) called\n"), hCard, *pcchReaderLen, *pcbAtrLen)); + + if (theApp.IsRemoteCard(hCard)) { + // According to https://docs.microsoft.com/en-us/windows/desktop/api/winscard/nf-winscard-scardstatusa + *pdwState = SCARD_SPECIFIC; + *pdwProtocol = SCARD_PROTOCOL_T1; + + return SCARD_S_SUCCESS; + } + else { + return (*Original_SCardStatus)(hCard, szReaderName, pcchReaderLen, pdwState, pdwProtocol, pbAtr, pcbAtrLen); + } } static SCard LONG(STDCALL *Original_SCardListReaders)( @@ -796,7 +844,7 @@ SCard LONG STDCALL SCardListReaders( OUT LPSTR mszReaders, IN OUT LPDWORD pcchReaders) { - if (theApp.m_winscardConfig.bLOG_FUNCTIONS_CALLS) CCommonFnc::File_AppendString(WINSCARD_RULES_LOG, _CONV("SCardListReaders called\n")); + LogWinscardRules(_CONV("SCardListReaders called\n")); int status = SCARD_S_SUCCESS; ls readersList; @@ -896,7 +944,7 @@ SCard LONG STDCALL SCardListReaders( availableReaders += ", "; } availableReaders += "\n"; - if (theApp.m_winscardConfig.bLOG_FUNCTIONS_CALLS) CCommonFnc::File_AppendString(WINSCARD_RULES_LOG, availableReaders); + LogWinscardRules(availableReaders); return status; } @@ -912,7 +960,7 @@ SCard LONG STDCALL SCardListReaderGroups( OUT LPSTR mszGroups, IN OUT LPDWORD pcchGroups ) { - if (theApp.m_winscardConfig.bLOG_FUNCTIONS_CALLS) CCommonFnc::File_AppendString(WINSCARD_RULES_LOG, _CONV("SCardListReaderGroups called\n")); + LogWinscardRules(_CONV("SCardListReaderGroups called\n")); return (*Original_SCardListReaderGroups)(hContext, mszGroups, pcchGroups); } @@ -939,7 +987,7 @@ SCard LONG STDCALL SCardConnectW( LONG status = SCARD_S_SUCCESS; string_type message; message = string_format(_CONV("SCardConnectW(hContext:0x%x, %S) called\n"), hContext, szReader); - if (theApp.m_winscardConfig.bLOG_FUNCTIONS_CALLS) CCommonFnc::File_AppendString(WINSCARD_RULES_LOG, message); + LogWinscardRules(message); // RESET APDU IN BYTE COUNTER theApp.m_processedApduByteCounter = 0; @@ -952,20 +1000,20 @@ SCard LONG STDCALL SCardConnectW( message = string_format(_CONV("\n:: %s"), l.c_str()); //message.Replace("\n", " "); replace(message.begin(), message.end(), '\n', ' '); - CCommonFnc::File_AppendString(WINSCARD_RULES_LOG, message); + LogWinscardRules(message); // PREPARE FOR MEASUREMENT message = string_format(_CONV("get params 1 %d %d"), theApp.m_remoteConfig.measureApduByteCounter, theApp.m_remoteConfig.measureApduByteDelay); theApp.m_remoteConfig.pSocket->SendLine(message); l = theApp.m_remoteConfig.pSocket->ReceiveResponse(REMOTE_SOCKET_ENDSEQ, REMOTE_SOCKET_TIMEOUT); message = string_format(_CONV(":: %s"), l.c_str()); - CCommonFnc::File_AppendString(WINSCARD_RULES_LOG, message); + LogWinscardRules(message); message = string_format(_CONV("post sampling %d"), theApp.m_remoteConfig.numSamples); theApp.m_remoteConfig.pSocket->SendLine(message); l = theApp.m_remoteConfig.pSocket->ReceiveResponse(REMOTE_SOCKET_ENDSEQ, REMOTE_SOCKET_TIMEOUT); message = string_format(_CONV(":: %s"), l.c_str()); - CCommonFnc::File_AppendString(WINSCARD_RULES_LOG, message); + LogWinscardRules(message); // PREPARE FOR MEASUREMENT READING IN FUTURE theApp.m_remoteConfig.sampleReaded = FALSE; @@ -981,7 +1029,7 @@ SCard LONG STDCALL SCardConnectW( } message = string_format(_CONV("-> hCard:0x%x\n"), *phCard); - CCommonFnc::File_AppendString(WINSCARD_RULES_LOG, message); + LogWinscardRules(message); return status; } @@ -1001,7 +1049,7 @@ SCard LONG STDCALL SCardListReadersW( { string_type message; message = string_format(_CONV("SCardListReadersW(hContext:0x%x) called\n"), hContext); - if (theApp.m_winscardConfig.bLOG_FUNCTIONS_CALLS) CCommonFnc::File_AppendString(WINSCARD_RULES_LOG, message); + LogWinscardRules(message); LONG status = SCARD_S_SUCCESS; lws readersList; @@ -1105,7 +1153,7 @@ SCard LONG STDCALL SCardListReadersW( availableReaders += L", "; } availableReaders += L"\n"; - if (theApp.m_winscardConfig.bLOG_FUNCTIONS_CALLS) CCommonFnc::File_AppendString(WINSCARD_RULES_LOG, availableReaders); + //LogWinscardRules(availableReaders); return status; } @@ -1121,7 +1169,7 @@ SCard LONG STDCALL SCardListReaderGroupsW( OUT LPWSTR mszGroups, IN OUT LPDWORD pcchGroups ) { - if (theApp.m_winscardConfig.bLOG_FUNCTIONS_CALLS) CCommonFnc::File_AppendString(WINSCARD_RULES_LOG, _CONV("SCardListReaderGroupsW called\n")); + LogWinscardRules(_CONV("SCardListReaderGroupsW called\n")); return (*Original_SCardListReaderGroupsW)(hContext, mszGroups, pcchGroups); } @@ -1145,7 +1193,7 @@ SCard LONG STDCALL SCardListCardsA( ) { string_type message; message = string_format(_CONV("SCardListCardsA(hContext:0x%x) called\n"), hContext); - if (theApp.m_winscardConfig.bLOG_FUNCTIONS_CALLS) CCommonFnc::File_AppendString(WINSCARD_RULES_LOG, message); + LogWinscardRules(message); return (*Original_SCardListCardsA)(hContext, pbAtr, rgquidInterfaces, cguidInterfaceCount, mszCards, pcchCards); } @@ -1169,7 +1217,7 @@ SCard LONG STDCALL SCardListCardsW( ) { string_type message; message = string_format(_CONV("SCardListCardsW(hContext:0x%x) called\n"), hContext); - if (theApp.m_winscardConfig.bLOG_FUNCTIONS_CALLS) CCommonFnc::File_AppendString(WINSCARD_RULES_LOG, message); + LogWinscardRules(message); return (*Original_SCardListCardsW)(hContext, pbAtr, rgquidInterfaces, cguidInterfaceCount, mszCards, pcchCards); } @@ -1187,7 +1235,7 @@ SCard LONG STDCALL SCardListInterfacesA( OUT LPGUID pguidInterfaces, IN OUT LPDWORD pcguidInterfaces ) { - if (theApp.m_winscardConfig.bLOG_FUNCTIONS_CALLS) CCommonFnc::File_AppendString(WINSCARD_RULES_LOG, _CONV("SCardListInterfacesA called\n")); + LogWinscardRules(_CONV("SCardListInterfacesA called\n")); return (*Original_SCardListInterfacesA)(hContext, szCard, pguidInterfaces, pcguidInterfaces); } @@ -1205,7 +1253,7 @@ SCard LONG STDCALL SCardListInterfacesW( OUT LPGUID pguidInterfaces, IN OUT LPDWORD pcguidInterfaces ) { - if (theApp.m_winscardConfig.bLOG_FUNCTIONS_CALLS) CCommonFnc::File_AppendString(WINSCARD_RULES_LOG, _CONV("SCardListInterfacesW called\n")); + LogWinscardRules(_CONV("SCardListInterfacesW called\n")); return (*Original_SCardListInterfacesW)(hContext, szCard, pguidInterfaces, pcguidInterfaces); } @@ -1221,7 +1269,7 @@ SCard LONG STDCALL SCardGetProviderIdA( IN LPCSTR szCard, OUT LPGUID pguidProviderId ) { - if (theApp.m_winscardConfig.bLOG_FUNCTIONS_CALLS) CCommonFnc::File_AppendString(WINSCARD_RULES_LOG, _CONV("SCardGetProviderIdA called\n")); + LogWinscardRules(_CONV("SCardGetProviderIdA called\n")); return (*Original_SCardGetProviderIdA)(hContext, szCard, pguidProviderId); } @@ -1237,7 +1285,7 @@ SCard LONG STDCALL SCardGetProviderIdW( IN LPCWSTR szCard, OUT LPGUID pguidProviderId ) { - if (theApp.m_winscardConfig.bLOG_FUNCTIONS_CALLS) CCommonFnc::File_AppendString(WINSCARD_RULES_LOG, _CONV("SCardGetProviderIdW called\n")); + LogWinscardRules(_CONV("SCardGetProviderIdW called\n")); return (*Original_SCardGetProviderIdW)(hContext, szCard, pguidProviderId); } @@ -1257,7 +1305,7 @@ SCard LONG STDCALL SCardGetCardTypeProviderNameA( OUT LPSTR szProvider, IN OUT LPDWORD pcchProvider ) { - if (theApp.m_winscardConfig.bLOG_FUNCTIONS_CALLS) CCommonFnc::File_AppendString(WINSCARD_RULES_LOG, _CONV("SCardGetCardTypeProviderNameA called\n")); + LogWinscardRules(_CONV("SCardGetCardTypeProviderNameA called\n")); return (*Original_SCardGetCardTypeProviderNameA)(hContext, szCardName, dwProviderId, szProvider, pcchProvider); } @@ -1277,7 +1325,7 @@ SCard LONG STDCALL SCardGetCardTypeProviderNameW( OUT LPWSTR szProvider, IN OUT LPDWORD pcchProvider ) { - if (theApp.m_winscardConfig.bLOG_FUNCTIONS_CALLS) CCommonFnc::File_AppendString(WINSCARD_RULES_LOG, _CONV("SCardGetCardTypeProviderNameW called\n")); + LogWinscardRules(_CONV("SCardGetCardTypeProviderNameW called\n")); return (*Original_SCardGetCardTypeProviderNameW)(hContext, szCardName, dwProviderId, szProvider, pcchProvider); } @@ -1291,7 +1339,7 @@ SCard LONG STDCALL SCardIntroduceReaderGroupA( IN SCARDCONTEXT hContext, IN LPCSTR szGroupName ) { - if (theApp.m_winscardConfig.bLOG_FUNCTIONS_CALLS) CCommonFnc::File_AppendString(WINSCARD_RULES_LOG, _CONV("SCardIntroduceReaderGroupA called\n")); + LogWinscardRules(_CONV("SCardIntroduceReaderGroupA called\n")); return (*Original_SCardIntroduceReaderGroupA)(hContext, szGroupName); } @@ -1305,7 +1353,7 @@ SCard LONG STDCALL SCardIntroduceReaderGroupW( IN SCARDCONTEXT hContext, IN LPCWSTR szGroupName ) { - if (theApp.m_winscardConfig.bLOG_FUNCTIONS_CALLS) CCommonFnc::File_AppendString(WINSCARD_RULES_LOG, _CONV("SCardIntroduceReaderGroupW called\n")); + LogWinscardRules(_CONV("SCardIntroduceReaderGroupW called\n")); return (*Original_SCardIntroduceReaderGroupW)(hContext, szGroupName); } @@ -1319,7 +1367,7 @@ SCard LONG STDCALL SCardForgetReaderGroupA( IN SCARDCONTEXT hContext, IN LPCSTR szGroupName ) { - if (theApp.m_winscardConfig.bLOG_FUNCTIONS_CALLS) CCommonFnc::File_AppendString(WINSCARD_RULES_LOG, _CONV("SCardForgetReaderGroupA called\n")); + LogWinscardRules(_CONV("SCardForgetReaderGroupA called\n")); return (*Original_SCardForgetReaderGroupA)(hContext, szGroupName); } @@ -1333,7 +1381,7 @@ SCard LONG STDCALL SCardForgetReaderGroupW( IN SCARDCONTEXT hContext, IN LPCWSTR szGroupName ) { - if (theApp.m_winscardConfig.bLOG_FUNCTIONS_CALLS) CCommonFnc::File_AppendString(WINSCARD_RULES_LOG, _CONV("SCardForgetReaderGroupW called\n")); + LogWinscardRules(_CONV("SCardForgetReaderGroupW called\n")); return (*Original_SCardForgetReaderGroupW)(hContext, szGroupName); } @@ -1349,7 +1397,7 @@ SCard LONG STDCALL SCardIntroduceReaderA( IN LPCSTR szReaderName, IN LPCSTR szDeviceName ) { - if (theApp.m_winscardConfig.bLOG_FUNCTIONS_CALLS) CCommonFnc::File_AppendString(WINSCARD_RULES_LOG, _CONV("SCardIntroduceReaderA called\n")); + LogWinscardRules(_CONV("SCardIntroduceReaderA called\n")); return (*Original_SCardIntroduceReaderA)(hContext, szReaderName, szDeviceName); } @@ -1365,7 +1413,7 @@ SCard LONG STDCALL SCardIntroduceReaderW( IN LPCWSTR szReaderName, IN LPCWSTR szDeviceName ) { - if (theApp.m_winscardConfig.bLOG_FUNCTIONS_CALLS) CCommonFnc::File_AppendString(WINSCARD_RULES_LOG, _CONV("SCardIntroduceReaderW called\n")); + LogWinscardRules(_CONV("SCardIntroduceReaderW called\n")); return (*Original_SCardIntroduceReaderW)(hContext, szReaderName, szDeviceName); } @@ -1379,7 +1427,7 @@ SCard LONG STDCALL SCardForgetReaderA( IN SCARDCONTEXT hContext, IN LPCSTR szReaderName ) { - if (theApp.m_winscardConfig.bLOG_FUNCTIONS_CALLS) CCommonFnc::File_AppendString(WINSCARD_RULES_LOG, _CONV("SCardForgetReaderA called\n")); + LogWinscardRules(_CONV("SCardForgetReaderA called\n")); return (*Original_SCardForgetReaderA)(hContext, szReaderName); } @@ -1393,7 +1441,7 @@ SCard LONG STDCALL SCardForgetReaderW( IN SCARDCONTEXT hContext, IN LPCWSTR szReaderName ) { - if (theApp.m_winscardConfig.bLOG_FUNCTIONS_CALLS) CCommonFnc::File_AppendString(WINSCARD_RULES_LOG, _CONV("SCardForgetReaderW called\n")); + LogWinscardRules(_CONV("SCardForgetReaderW called\n")); return (*Original_SCardForgetReaderW)(hContext, szReaderName); } @@ -1409,7 +1457,7 @@ SCard LONG STDCALL SCardAddReaderToGroupA( IN LPCSTR szReaderName, IN LPCSTR szGroupName ) { - if (theApp.m_winscardConfig.bLOG_FUNCTIONS_CALLS) CCommonFnc::File_AppendString(WINSCARD_RULES_LOG, _CONV("SCardAddReaderToGroupA called\n")); + LogWinscardRules(_CONV("SCardAddReaderToGroupA called\n")); return (*Original_SCardAddReaderToGroupA)(hContext, szReaderName, szGroupName); } @@ -1425,7 +1473,7 @@ SCard LONG STDCALL SCardAddReaderToGroupW( IN LPCWSTR szReaderName, IN LPCWSTR szGroupName ) { - if (theApp.m_winscardConfig.bLOG_FUNCTIONS_CALLS) CCommonFnc::File_AppendString(WINSCARD_RULES_LOG, _CONV("SCardAddReaderToGroupW called\n")); + LogWinscardRules(_CONV("SCardAddReaderToGroupW called\n")); return (*Original_SCardAddReaderToGroupW)(hContext, szReaderName, szGroupName); } @@ -1441,7 +1489,7 @@ SCard LONG STDCALL SCardRemoveReaderFromGroupA( IN LPCSTR szReaderName, IN LPCSTR szGroupName ) { - if (theApp.m_winscardConfig.bLOG_FUNCTIONS_CALLS) CCommonFnc::File_AppendString(WINSCARD_RULES_LOG, _CONV("SCardRemoveReaderFromGroupA called\n")); + LogWinscardRules(_CONV("SCardRemoveReaderFromGroupA called\n")); return (*Original_SCardRemoveReaderFromGroupA)(hContext, szReaderName, szGroupName); } @@ -1457,7 +1505,7 @@ SCard LONG STDCALL SCardRemoveReaderFromGroupW( IN LPCWSTR szReaderName, IN LPCWSTR szGroupName ) { - if (theApp.m_winscardConfig.bLOG_FUNCTIONS_CALLS) CCommonFnc::File_AppendString(WINSCARD_RULES_LOG, _CONV("SCardRemoveReaderFromGroupW called\n")); + LogWinscardRules(_CONV("SCardRemoveReaderFromGroupW called\n")); return (*Original_SCardRemoveReaderFromGroupW)(hContext, szReaderName, szGroupName); } @@ -1483,7 +1531,7 @@ SCard LONG STDCALL SCardIntroduceCardTypeA( IN LPCBYTE pbAtrMask, IN DWORD cbAtrLen ) { - if (theApp.m_winscardConfig.bLOG_FUNCTIONS_CALLS) CCommonFnc::File_AppendString(WINSCARD_RULES_LOG, _CONV("SCardIntroduceCardTypeA called\n")); + LogWinscardRules(_CONV("SCardIntroduceCardTypeA called\n")); return (*Original_SCardIntroduceCardTypeA)(hContext, szCardName, pguidPrimaryProvider, rgguidInterfaces, dwInterfaceCount, pbAtr, pbAtrMask, cbAtrLen); } @@ -1509,7 +1557,7 @@ SCard LONG STDCALL SCardIntroduceCardTypeW( IN LPCBYTE pbAtrMask, IN DWORD cbAtrLen ) { - if (theApp.m_winscardConfig.bLOG_FUNCTIONS_CALLS) CCommonFnc::File_AppendString(WINSCARD_RULES_LOG, _CONV("SCardIntroduceCardTypeW called\n")); + LogWinscardRules(_CONV("SCardIntroduceCardTypeW called\n")); return (*Original_SCardIntroduceCardTypeW)(hContext, szCardName, pguidPrimaryProvider, rgguidInterfaces, dwInterfaceCount, pbAtr, pbAtrMask, cbAtrLen); } @@ -1527,7 +1575,7 @@ SCard LONG STDCALL SCardSetCardTypeProviderNameA( IN DWORD dwProviderId, IN LPCSTR szProvider ) { - if (theApp.m_winscardConfig.bLOG_FUNCTIONS_CALLS) CCommonFnc::File_AppendString(WINSCARD_RULES_LOG, _CONV("SCardSetCardTypeProviderNameA called\n")); + LogWinscardRules(_CONV("SCardSetCardTypeProviderNameA called\n")); return (*Original_SCardSetCardTypeProviderNameA)(hContext, szCardName, dwProviderId, szProvider); } @@ -1545,7 +1593,7 @@ SCard LONG STDCALL SCardSetCardTypeProviderNameW( IN DWORD dwProviderId, IN LPCWSTR szProvider ) { - if (theApp.m_winscardConfig.bLOG_FUNCTIONS_CALLS) CCommonFnc::File_AppendString(WINSCARD_RULES_LOG, _CONV("SCardSetCardTypeProviderNameW called\n")); + LogWinscardRules(_CONV("SCardSetCardTypeProviderNameW called\n")); return (*Original_SCardSetCardTypeProviderNameW)(hContext, szCardName, dwProviderId, szProvider); } @@ -1559,7 +1607,7 @@ SCard LONG STDCALL SCardForgetCardTypeA( IN SCARDCONTEXT hContext, IN LPCSTR szCardName ) { - if (theApp.m_winscardConfig.bLOG_FUNCTIONS_CALLS) CCommonFnc::File_AppendString(WINSCARD_RULES_LOG, _CONV("SCardForgetCardTypeA called\n")); + LogWinscardRules(_CONV("SCardForgetCardTypeA called\n")); return (*Original_SCardForgetCardTypeA)(hContext, szCardName); } @@ -1573,7 +1621,7 @@ SCard LONG STDCALL SCardForgetCardTypeW( IN SCARDCONTEXT hContext, IN LPCWSTR szCardName ) { - if (theApp.m_winscardConfig.bLOG_FUNCTIONS_CALLS) CCommonFnc::File_AppendString(WINSCARD_RULES_LOG, _CONV("SCardForgetCardTypeW called\n")); + LogWinscardRules(_CONV("SCardForgetCardTypeW called\n")); return (*Original_SCardForgetCardTypeW)(hContext, szCardName); } @@ -1592,7 +1640,7 @@ SCard LONG STDCALL SCardLocateCardsA( ) { string_type message; message = string_format(_CONV("SCardLocateCardsA(%s,0x%x) called\n"), mszCards, hContext); - if (theApp.m_winscardConfig.bLOG_FUNCTIONS_CALLS) CCommonFnc::File_AppendString(WINSCARD_RULES_LOG, message); + LogWinscardRules(message); return (*Original_SCardLocateCardsA)(hContext, mszCards, rgReaderStates, cReaders); } @@ -1612,7 +1660,7 @@ SCard LONG STDCALL SCardLocateCardsW( ) { string_type message; message = string_format(_CONV("SCardLocateCardsW(%S,0x%x) called\n"), mszCards, hContext); - if (theApp.m_winscardConfig.bLOG_FUNCTIONS_CALLS) CCommonFnc::File_AppendString(WINSCARD_RULES_LOG, message); + LogWinscardRules(message); return (*Original_SCardLocateCardsW)(hContext, mszCards, rgReaderStates, cReaders); } @@ -1634,7 +1682,7 @@ SCard LONG STDCALL SCardLocateCardsByATRA( ) { string_type message; message = string_format(_CONV("SCardLocateCardsByATRA(hContext:0x%x) called\n"), hContext); - if (theApp.m_winscardConfig.bLOG_FUNCTIONS_CALLS) CCommonFnc::File_AppendString(WINSCARD_RULES_LOG, message); + LogWinscardRules(message); return (*Original_SCardLocateCardsByATRA)(hContext, rgAtrMasks, cAtrs, rgReaderStates, cReaders); } @@ -1656,7 +1704,7 @@ SCard LONG STDCALL SCardLocateCardsByATRW( ) { string_type message; message = string_format(_CONV("SCardLocateCardsByATRW(hContext:0x%x) called\n"), hContext); - if (theApp.m_winscardConfig.bLOG_FUNCTIONS_CALLS) CCommonFnc::File_AppendString(WINSCARD_RULES_LOG, message); + LogWinscardRules(message); return (*Original_SCardLocateCardsByATRW)(hContext, rgAtrMasks, cAtrs, rgReaderStates, cReaders); } @@ -1673,7 +1721,7 @@ SCard LONG STDCALL SCardGetStatusChangeA( IN OUT LPSCARD_READERSTATEA rgReaderStates, IN DWORD cReaders ) { - if (theApp.m_winscardConfig.bLOG_FUNCTIONS_CALLS) CCommonFnc::File_AppendString(WINSCARD_RULES_LOG, _CONV("SCardGetStatusChangeA called\n")); + LogWinscardRules(_CONV("SCardGetStatusChangeA called\n")); return (*Original_SCardGetStatusChangeA)(hContext, dwTimeout, rgReaderStates, cReaders); } @@ -1691,7 +1739,7 @@ SCard LONG STDCALL SCardGetStatusChangeW( IN OUT LPSCARD_READERSTATEW rgReaderStates, IN DWORD cReaders ) { - if (theApp.m_winscardConfig.bLOG_FUNCTIONS_CALLS) CCommonFnc::File_AppendString(WINSCARD_RULES_LOG, _CONV("SCardGetStatusChangeW called\n")); + LogWinscardRules(_CONV("SCardGetStatusChangeW called\n")); return (*Original_SCardGetStatusChangeW)(hContext, dwTimeout, rgReaderStates, cReaders); } @@ -1702,7 +1750,7 @@ static SCard LONG(STDCALL *Original_SCardUIDlgSelectCardA)( SCard LONG STDCALL SCardUIDlgSelectCardA( LPOPENCARDNAMEA_EX a ) { - if (theApp.m_winscardConfig.bLOG_FUNCTIONS_CALLS) CCommonFnc::File_AppendString(WINSCARD_RULES_LOG, _CONV("SCardUIDlgSelectCardA called\n")); + LogWinscardRules(_CONV("SCardUIDlgSelectCardA called\n")); return (*Original_SCardUIDlgSelectCardA)(a); } @@ -1714,7 +1762,7 @@ static SCard LONG(STDCALL *Original_SCardUIDlgSelectCardW)( SCard LONG STDCALL SCardUIDlgSelectCardW( LPOPENCARDNAMEW_EX a ) { - if (theApp.m_winscardConfig.bLOG_FUNCTIONS_CALLS) CCommonFnc::File_AppendString(WINSCARD_RULES_LOG, _CONV("SCardUIDlgSelectCardW called\n")); + LogWinscardRules(_CONV("SCardUIDlgSelectCardW called\n")); return (*Original_SCardUIDlgSelectCardW)(a); } @@ -1725,7 +1773,7 @@ static SCard LONG(STDCALL *Original_GetOpenCardNameA)( SCard LONG STDCALL GetOpenCardNameA( LPOPENCARDNAMEA a ) { - if (theApp.m_winscardConfig.bLOG_FUNCTIONS_CALLS) CCommonFnc::File_AppendString(WINSCARD_RULES_LOG, _CONV("GetOpenCardNameA called\n")); + LogWinscardRules(_CONV("GetOpenCardNameA called\n")); return (*Original_GetOpenCardNameA)(a); } @@ -1736,7 +1784,7 @@ static SCard LONG(STDCALL *Original_GetOpenCardNameW)( SCard LONG STDCALL GetOpenCardNameW( LPOPENCARDNAMEW a ) { - if (theApp.m_winscardConfig.bLOG_FUNCTIONS_CALLS) CCommonFnc::File_AppendString(WINSCARD_RULES_LOG, _CONV("GetOpenCardNameW called\n")); + LogWinscardRules(_CONV("GetOpenCardNameW called\n")); return (*Original_GetOpenCardNameW)(a); } @@ -1744,7 +1792,7 @@ SCard LONG STDCALL GetOpenCardNameW( static SCard LONG(STDCALL *Original_SCardDlgExtendedError)(void); SCard LONG STDCALL SCardDlgExtendedError(void) { - CCommonFnc::File_AppendString(WINSCARD_RULES_LOG, _CONV("SCardDlgExtendedError called\n")); + LogWinscardRules(_CONV("SCardDlgExtendedError called\n")); return (*Original_SCardDlgExtendedError)(); } @@ -1755,8 +1803,13 @@ static SCard LONG(STDCALL *Original_SCardCancelTransaction)( SCard LONG STDCALL SCardCancelTransaction( IN SCARDHANDLE hCard ) { - if (theApp.m_winscardConfig.bLOG_FUNCTIONS_CALLS) CCommonFnc::File_AppendString(WINSCARD_RULES_LOG, _CONV("SCardCancelTransaction called\n")); - return (*Original_SCardCancelTransaction)(hCard); + LogWinscardRules(_CONV("SCardCancelTransaction called\n")); + if (theApp.IsRemoteCard(hCard)) { + return SCARD_S_SUCCESS; + } + else { + return (*Original_SCardCancelTransaction)(hCard); + } } static SCard LONG(STDCALL *Original_SCardState)( @@ -1776,8 +1829,16 @@ SCard LONG STDCALL SCardState( ) { string_type message; message = string_format(_CONV("SCardState(hCard:0x%x) called\n"), hCard); - if (theApp.m_winscardConfig.bLOG_FUNCTIONS_CALLS) CCommonFnc::File_AppendString(WINSCARD_RULES_LOG, message); - return (*Original_SCardState)(hCard, pdwState, pdwProtocol, pbAtr, pcbAtrLen); + LogWinscardRules(message); + if (theApp.IsRemoteCard(hCard)) { + return SCARD_S_SUCCESS; + *pdwState = SCARD_SPECIFIC; + *pdwProtocol = SCARD_PROTOCOL_T1; + // TODO: fill pbAtr + } + else { + return (*Original_SCardState)(hCard, pdwState, pdwProtocol, pbAtr, pcbAtrLen); + } } @@ -1802,14 +1863,22 @@ SCard LONG STDCALL SCardStatusW( ) { string_type message; message = string_format(_CONV("SCardStatusW(hCard:0x%x) called\n"), hCard); - if (theApp.m_winscardConfig.bLOG_FUNCTIONS_CALLS) CCommonFnc::File_AppendString(WINSCARD_RULES_LOG, message); - return (*Original_SCardStatusW)(hCard, szReaderName, pcchReaderLen, pdwState, pdwProtocol, pbAtr, pcbAtrLen); + LogWinscardRules(message); + if (theApp.IsRemoteCard(hCard)) { + return SCARD_S_SUCCESS; + *pdwState = SCARD_SPECIFIC; + *pdwProtocol = SCARD_PROTOCOL_T1; + // TODO: fill pbAtr + } + else { + return (*Original_SCardStatusW)(hCard, szReaderName, pcchReaderLen, pdwState, pdwProtocol, pbAtr, pcbAtrLen); + } } static SCard HANDLE(STDCALL *Original_SCardAccessStartedEvent)(void); SCard HANDLE STDCALL SCardAccessStartedEvent(void) { - if (theApp.m_winscardConfig.bLOG_FUNCTIONS_CALLS) CCommonFnc::File_AppendString(WINSCARD_RULES_LOG, _CONV("SCardAccessStartedEvent called\n")); + LogWinscardRules(_CONV("SCardAccessStartedEvent called\n")); return (*Original_SCardAccessStartedEvent)(); } @@ -1817,7 +1886,7 @@ SCard HANDLE STDCALL SCardAccessStartedEvent(void) { static SCard void(STDCALL *Original_SCardReleaseStartedEvent)(void); SCard void STDCALL SCardReleaseStartedEvent(void) { - if (theApp.m_winscardConfig.bLOG_FUNCTIONS_CALLS) CCommonFnc::File_AppendString(WINSCARD_RULES_LOG, _CONV("SCardReleaseStartedEvent called\n")); + LogWinscardRules(_CONV("SCardReleaseStartedEvent called\n")); return (*Original_SCardReleaseStartedEvent)(); } #endif @@ -1839,7 +1908,7 @@ LONG SCardGetStatusChange( IN OUT SCARD_READERSTATE *rgReaderStates, IN DWORD cReaders ) { - if (theApp.m_winscardConfig.bLOG_FUNCTIONS_CALLS) CCommonFnc::File_AppendString(WINSCARD_RULES_LOG, _CONV("SCardGetStatusChange called\n")); + LogWinscardRules(_CONV("SCardGetStatusChange called\n")); return (*Original_SCardGetStatusChange)(hContext, dwTimeout, rgReaderStates, cReaders); } @@ -1849,13 +1918,6 @@ typedef FARPROC(STDCALL *q) (HMODULE, LPCSTR); static q load_func = GetProcAddress; #endif -/*linux specific funcions: -*SCardConnect -- DONE -*SCardStatus -- DONE -*SCardGetStatusChange -- DONE -*SCardListReaders -- DONE -*SCardListReaderGroups -- DONE -*/ int initialize() { char *error = ""; @@ -1878,7 +1940,7 @@ int initialize() #if __linux__ error = dlerror(); #endif - fprintf(stderr, "Failed to load original library%s%s\n", delimeter, error); + LogDebugString(string_format(_CONV("Failed to load original library%s%s\n"), delimeter, error)); return FALSE; } @@ -1905,7 +1967,7 @@ int initialize() #if __linux__ error = dlerror(); #endif - fprintf(stderr, "Could not find SCardTransmit procedure address%s%s\n", delimeter, error); + LogDebugString(string_format(_CONV("Could not find SCardTransmit procedure address%s%s\n"), delimeter, error)); return FALSE; } @@ -1916,7 +1978,7 @@ int initialize() #if __linux__ error = dlerror(); #endif - fprintf(stderr, "Could not find SCardDisconnect procedure address%s%s\n", delimeter, error); + LogDebugString(string_format(_CONV("Could not find SCardDisconnect procedure address%s%s\n"), delimeter, error)); return FALSE; } @@ -1927,7 +1989,7 @@ int initialize() #if __linux__ error = dlerror(); #endif - fprintf(stderr, "Could not find SCardFreeMemory procedure address%s%s\n", delimeter, error); + LogDebugString(string_format(_CONV("Could not find SCardFreeMemory procedure address%s%s\n"), delimeter, error)); return FALSE; } @@ -1941,7 +2003,7 @@ int initialize() #if __linux__ error = dlerror(); #endif - fprintf(stderr, "Could not find SCardEstablishContext procedure address%s%s\n", delimeter, error); + LogDebugString(string_format(_CONV("Could not find SCardEstablishContext procedure address%s%s\n"), delimeter, error)); return FALSE; } @@ -1953,7 +2015,7 @@ int initialize() #if __linux__ error = dlerror(); #endif - fprintf(stderr, "Could not find SCardReleaseContext procedure address%s%s\n", delimeter, error); + LogDebugString(string_format(_CONV("Could not find SCardReleaseContext procedure address%s%s\n"), delimeter, error)); return FALSE; } @@ -1965,7 +2027,7 @@ int initialize() #if __linux__ error = dlerror(); #endif - fprintf(stderr, "Could not find SCardIsValidContext procedure address%s%s\n", delimeter, error); + LogDebugString(string_format(_CONV("Could not find SCardIsValidContext procedure address%s%s\n"), delimeter, error)); return FALSE; } @@ -1978,7 +2040,7 @@ int initialize() #if __linux__ error = dlerror(); #endif - fprintf(stderr, "Could not find SCardFreeMemory procedure address%s%s\n", delimeter, error); + LogDebugString(string_format(_CONV("Could not find SCardFreeMemory procedure address%s%s\n"), delimeter, error)); return FALSE; } @@ -1990,7 +2052,7 @@ int initialize() #if __linux__ error = dlerror(); #endif - fprintf(stderr, "Could not find SCardCancel procedure address%s%s\n", delimeter, error); + LogDebugString(string_format(_CONV("Could not find SCardCancel procedure address%s%s\n"), delimeter, error)); return FALSE; } @@ -2006,7 +2068,7 @@ int initialize() #if __linux__ error = dlerror(); #endif - fprintf(stderr, "Could not find SCardReconnect procedure address%s%s\n", delimeter, error); + LogDebugString(string_format(_CONV("Could not find SCardReconnect procedure address%s%s\n"), delimeter, error)); return FALSE; } @@ -2019,7 +2081,7 @@ int initialize() #if __linux__ error = dlerror(); #endif - fprintf(stderr, "Could not find SCardDisconnect procedure address%s%s\n", delimeter, error); + LogDebugString(string_format(_CONV("Could not find SCardDisconnect procedure address%s%s\n"), delimeter, error)); return FALSE; } @@ -2031,7 +2093,7 @@ int initialize() #if __linux__ error = dlerror(); #endif - fprintf(stderr, "Could not find SCardBeginTransaction procedure address%s%s\n", delimeter, error); + LogDebugString(string_format(_CONV("Could not find SCardBeginTransaction procedure address%s%s\n"), delimeter, error)); return FALSE; } @@ -2044,7 +2106,7 @@ int initialize() #if __linux__ error = dlerror(); #endif - fprintf(stderr, "Could not find SCardEndTransaction procedure address%s%s\n", delimeter, error); + LogDebugString(string_format(_CONV("Could not find SCardEndTransaction procedure address%s%s\n"), delimeter, error)); return FALSE; } @@ -2062,7 +2124,7 @@ int initialize() #if __linux__ error = dlerror(); #endif - fprintf(stderr, "Could not find SCardTransmit procedure address%s%s\n", delimeter, error); + LogDebugString(string_format(_CONV("Could not find SCardTransmit procedure address%s%s\n"), delimeter, error)); return FALSE; } @@ -2080,7 +2142,7 @@ int initialize() #if __linux__ error = dlerror(); #endif - fprintf(stderr, "Could not find SCardControl procedure address%s%s\n", delimeter, error); + LogDebugString(string_format(_CONV("Could not find SCardControl procedure address%s%s\n"), delimeter, error)); return FALSE; } @@ -2095,7 +2157,7 @@ int initialize() #if __linux__ error = dlerror(); #endif - fprintf(stderr, "Could not find SCardGetAttrib procedure address%s%s\n", delimeter, error); + LogDebugString(string_format(_CONV("Could not find SCardGetAttrib procedure address%s%s\n"), delimeter, error)); return FALSE; } @@ -2110,7 +2172,7 @@ int initialize() #if __linux__ error = dlerror(); #endif - fprintf(stderr, "Could not find SCardSetAttrib procedure address%s%s\n", delimeter, error); + LogDebugString(_CONV("Could not find SCardSetAttrib procedure address%s%s\n")); return FALSE; } @@ -2120,7 +2182,7 @@ int initialize() (long(STDCALL *)(SCARDHANDLE hCard, LPSTR szReaderName, LPDWORD pcchReaderLen, LPDWORD pdwState, LPDWORD pdwProtocol, LPBYTE pbAtr, LPDWORD pcbAtrLen)) load_func(hOriginal, "SCardStatusA"); if ((!Original_SCardStatus)) { - fprintf(stderr, "Could not find SCardStatusA procedure address\n"); + LogDebugString(_CONV("Could not find SCardStatusA procedure address\n")); return FALSE; } @@ -2128,7 +2190,7 @@ int initialize() (long(STDCALL *)(SCARDCONTEXT hContext, LPCWSTR szReader, DWORD dwShareMode, DWORD dwPreferredProtocols, LPSCARDHANDLE phCard, LPDWORD pdwActiveProtocol)) load_func(hOriginal, "SCardConnectW"); if ((!Original_SCardConnectW)) { - fprintf(stderr, "Could not find SCardConnectW procedure address\n"); + LogDebugString(_CONV("Could not find SCardConnectW procedure address\n")); return FALSE; } @@ -2136,7 +2198,7 @@ int initialize() (long(STDCALL *)(SCARDCONTEXT hContext, LPCWSTR mszGroups, LPWSTR mszReaders, LPDWORD pcchReaders)) load_func(hOriginal, "SCardListReadersW"); if ((!Original_SCardListReadersW)) { - fprintf(stderr, "Could not find SCardListReadersW procedure address\n"); + LogDebugString(_CONV("Could not find SCardListReadersW procedure address\n")); return FALSE; } @@ -2144,7 +2206,7 @@ int initialize() (long(STDCALL *)(SCARDCONTEXT hContext, LPCSTR mszGroups, LPSTR mszReaders, LPDWORD pcchReaders)) load_func(hOriginal, "SCardListReadersA"); if ((!Original_SCardListReaders)) { - fprintf(stderr, "Could not find SCardListReadersA procedure address\n"); + LogDebugString(_CONV("Could not find SCardListReadersA procedure address\n")); return FALSE; } @@ -2155,7 +2217,7 @@ int initialize() IN OUT LPDWORD pcchGroups)) load_func(hOriginal, "SCardListReaderGroupsA"); if (!Original_SCardListReaderGroups) { - fprintf(stderr, "Could not find SCardListReaderGroupsA procedure address\n"); + LogDebugString(_CONV("Could not find SCardListReaderGroupsA procedure address\n")); return FALSE; } @@ -2166,7 +2228,7 @@ int initialize() IN OUT LPDWORD pcchGroups)) load_func(hOriginal, "SCardListReaderGroupsW"); if (!Original_SCardListReaderGroupsW) { - fprintf(stderr, "Could not find SCardListReaderGroupsW procedure address\n"); + LogDebugString(_CONV("Could not find SCardListReaderGroupsW procedure address\n")); return FALSE; } @@ -2178,7 +2240,7 @@ int initialize() IN OUT LPDWORD pcchReaders)) load_func(hOriginal, "SCardListReadersA"); if (!Original_SCardListReaders) { - fprintf(stderr, "Could not find SCardListReadersA procedure address\n"); + LogDebugString(_CONV("Could not find SCardListReadersA procedure address\n")); return FALSE; } @@ -2190,7 +2252,7 @@ int initialize() IN OUT LPDWORD pcchReaders)) load_func(hOriginal, "SCardListReadersW"); if (!Original_SCardListReadersW) { - fprintf(stderr, "Could not find SCardListReadersW procedure address\n"); + LogDebugString(_CONV("Could not find SCardListReadersW procedure address\n")); return FALSE; } @@ -2204,7 +2266,7 @@ int initialize() IN OUT LPDWORD pcchCards)) load_func(hOriginal, "SCardListCardsA"); if (!Original_SCardListCardsA) { - fprintf(stderr, "Could not find SCardListCardsA procedure address\n"); + LogDebugString(_CONV("Could not find SCardListCardsA procedure address\n")); return FALSE; } @@ -2218,7 +2280,7 @@ int initialize() IN OUT LPDWORD pcchCards)) load_func(hOriginal, "SCardListCardsW"); if (!Original_SCardListCardsW) { - fprintf(stderr, "Could not find SCardListCardsW procedure address\n"); + LogDebugString(_CONV("Could not find SCardListCardsW procedure address\n")); return FALSE; } @@ -2230,7 +2292,7 @@ int initialize() IN OUT LPDWORD pcguidInterfaces)) load_func(hOriginal, "SCardListInterfacesA"); if (!Original_SCardListInterfacesA) { - fprintf(stderr, "Could not find SCardListInterfacesA procedure address\n"); + LogDebugString(_CONV("Could not find SCardListInterfacesA procedure address\n")); return FALSE; } @@ -2242,7 +2304,7 @@ int initialize() IN OUT LPDWORD pcguidInterfaces)) load_func(hOriginal, "SCardListInterfacesW"); if (!Original_SCardListInterfacesW) { - fprintf(stderr, "Could not find SCardListInterfacesW procedure address\n"); + LogDebugString(_CONV("Could not find SCardListInterfacesW procedure address\n")); return FALSE; } @@ -2253,7 +2315,7 @@ int initialize() OUT LPGUID pguidProviderId)) load_func(hOriginal, "SCardGetProviderIdA"); if (!Original_SCardGetProviderIdA) { - fprintf(stderr, "Could not find SCardGetProviderIdA procedure address\n"); + LogDebugString(_CONV("Could not find SCardGetProviderIdA procedure address\n")); return FALSE; } @@ -2264,7 +2326,7 @@ int initialize() OUT LPGUID pguidProviderId)) load_func(hOriginal, "SCardGetProviderIdW"); if (!Original_SCardGetProviderIdW) { - fprintf(stderr, "Could not find SCardGetProviderIdW procedure address\n"); + LogDebugString(_CONV("Could not find SCardGetProviderIdW procedure address\n")); return FALSE; } @@ -2277,7 +2339,7 @@ int initialize() IN OUT LPDWORD pcchProvider)) load_func(hOriginal, "SCardGetCardTypeProviderNameA"); if (!Original_SCardGetCardTypeProviderNameA) { - fprintf(stderr, "Could not find SCardGetCardTypeProviderNameA procedure address\n"); + LogDebugString(_CONV("Could not find SCardGetCardTypeProviderNameA procedure address\n")); return FALSE; } @@ -2290,7 +2352,7 @@ int initialize() IN OUT LPDWORD pcchProvider)) load_func(hOriginal, "SCardGetCardTypeProviderNameW"); if (!Original_SCardGetCardTypeProviderNameW) { - fprintf(stderr, "Could not find SCardGetCardTypeProviderNameW procedure address\n"); + LogDebugString(_CONV("Could not find SCardGetCardTypeProviderNameW procedure address\n")); return FALSE; } @@ -2300,7 +2362,7 @@ int initialize() IN LPCSTR szGroupName)) load_func(hOriginal, "SCardIntroduceReaderGroupA"); if (!Original_SCardIntroduceReaderGroupA) { - fprintf(stderr, "Could not find SCardIntroduceReaderGroupA procedure address\n"); + LogDebugString(_CONV("Could not find SCardIntroduceReaderGroupA procedure address\n")); return FALSE; } @@ -2310,7 +2372,7 @@ int initialize() IN LPCWSTR szGroupName)) load_func(hOriginal, "SCardIntroduceReaderGroupW"); if (!Original_SCardIntroduceReaderGroupW) { - fprintf(stderr, "Could not find SCardIntroduceReaderGroupW procedure address\n"); + LogDebugString(_CONV("Could not find SCardIntroduceReaderGroupW procedure address\n")); return FALSE; } @@ -2320,7 +2382,7 @@ int initialize() IN LPCSTR szGroupName)) load_func(hOriginal, "SCardForgetReaderGroupA"); if (!Original_SCardForgetReaderGroupA) { - fprintf(stderr, "Could not find SCardForgetReaderGroupA procedure address\n"); + LogDebugString(_CONV("Could not find SCardForgetReaderGroupA procedure address\n")); return FALSE; } @@ -2330,7 +2392,7 @@ int initialize() IN LPCWSTR szGroupName)) load_func(hOriginal, "SCardForgetReaderGroupW"); if (!Original_SCardForgetReaderGroupW) { - fprintf(stderr, "Could not find SCardForgetReaderGroupW procedure address\n"); + LogDebugString(_CONV("Could not find SCardForgetReaderGroupW procedure address\n")); return FALSE; } @@ -2341,7 +2403,7 @@ int initialize() IN LPCSTR szDeviceName)) load_func(hOriginal, "SCardIntroduceReaderA"); if (!Original_SCardIntroduceReaderA) { - fprintf(stderr, "Could not find SCardIntroduceReaderA procedure address\n"); + LogDebugString(_CONV("Could not find SCardIntroduceReaderA procedure address\n")); return FALSE; } @@ -2352,7 +2414,7 @@ int initialize() IN LPCWSTR szDeviceName)) load_func(hOriginal, "SCardIntroduceReaderW"); if (!Original_SCardIntroduceReaderW) { - fprintf(stderr, "Could not find SCardIntroduceReaderW procedure address\n"); + LogDebugString(_CONV("Could not find SCardIntroduceReaderW procedure address\n")); return FALSE; } @@ -2362,7 +2424,7 @@ int initialize() IN LPCSTR szReaderName)) load_func(hOriginal, "SCardForgetReaderA"); if (!Original_SCardForgetReaderA) { - fprintf(stderr, "Could not find SCardForgetReaderA procedure address\n"); + LogDebugString(_CONV("Could not find SCardForgetReaderA procedure address\n")); return FALSE; } @@ -2372,7 +2434,7 @@ int initialize() IN LPCWSTR szReaderName)) load_func(hOriginal, "SCardForgetReaderW"); if (!Original_SCardForgetReaderW) { - fprintf(stderr, "Could not find SCardForgetReaderW procedure address\n"); + LogDebugString(_CONV("Could not find SCardForgetReaderW procedure address\n")); return FALSE; } @@ -2383,7 +2445,7 @@ int initialize() IN LPCSTR szGroupName)) load_func(hOriginal, "SCardAddReaderToGroupA"); if (!Original_SCardAddReaderToGroupA) { - fprintf(stderr, "Could not find SCardAddReaderToGroupA procedure address\n"); + LogDebugString(_CONV("Could not find SCardAddReaderToGroupA procedure address\n")); return FALSE; } @@ -2394,7 +2456,7 @@ int initialize() IN LPCWSTR szGroupName)) load_func(hOriginal, "SCardAddReaderToGroupW"); if (!Original_SCardAddReaderToGroupW) { - fprintf(stderr, "Could not find SCardAddReaderToGroupW procedure address\n"); + LogDebugString(_CONV("Could not find SCardAddReaderToGroupW procedure address\n")); return FALSE; } @@ -2405,7 +2467,7 @@ int initialize() IN LPCSTR szGroupName)) load_func(hOriginal, "SCardRemoveReaderFromGroupA"); if (!Original_SCardRemoveReaderFromGroupA) { - fprintf(stderr, "Could not find SCardRemoveReaderFromGroupA procedure address\n"); + LogDebugString(_CONV("Could not find SCardRemoveReaderFromGroupA procedure address\n")); return FALSE; } @@ -2416,7 +2478,7 @@ int initialize() IN LPCWSTR szGroupName)) load_func(hOriginal, "SCardRemoveReaderFromGroupW"); if (!Original_SCardRemoveReaderFromGroupW) { - fprintf(stderr, "Could not find SCardRemoveReaderFromGroupW procedure address\n"); + LogDebugString(_CONV("Could not find SCardRemoveReaderFromGroupW procedure address\n")); return FALSE; } @@ -2432,7 +2494,7 @@ int initialize() IN DWORD cbAtrLen)) load_func(hOriginal, "SCardIntroduceCardTypeA"); if (!Original_SCardIntroduceCardTypeA) { - fprintf(stderr, "Could not find SCardIntroduceCardTypeA procedure address\n"); + LogDebugString(_CONV("Could not find SCardIntroduceCardTypeA procedure address\n")); return FALSE; } @@ -2448,7 +2510,7 @@ int initialize() IN DWORD cbAtrLen)) load_func(hOriginal, "SCardIntroduceCardTypeW"); if (!Original_SCardIntroduceCardTypeW) { - fprintf(stderr, "Could not find SCardIntroduceCardTypeW procedure address\n"); + LogDebugString(_CONV("Could not find SCardIntroduceCardTypeW procedure address\n")); return FALSE; } @@ -2460,7 +2522,7 @@ int initialize() IN LPCSTR szProvider)) load_func(hOriginal, "SCardSetCardTypeProviderNameA"); if (!Original_SCardSetCardTypeProviderNameA) { - fprintf(stderr, "Could not find SCardSetCardTypeProviderNameA procedure address\n"); + LogDebugString(_CONV("Could not find SCardSetCardTypeProviderNameA procedure address\n")); return FALSE; } @@ -2472,7 +2534,7 @@ int initialize() IN LPCWSTR szProvider)) load_func(hOriginal, "SCardSetCardTypeProviderNameW"); if (!Original_SCardSetCardTypeProviderNameW) { - fprintf(stderr, "Could not find SCardSetCardTypeProviderNameW procedure address\n"); + LogDebugString(_CONV("Could not find SCardSetCardTypeProviderNameW procedure address\n")); return FALSE; } @@ -2482,7 +2544,7 @@ int initialize() IN LPCSTR szCardName)) load_func(hOriginal, "SCardForgetCardTypeA"); if (!Original_SCardForgetCardTypeA) { - fprintf(stderr, "Could not find SCardForgetCardTypeA procedure address\n"); + LogDebugString(_CONV("Could not find SCardForgetCardTypeA procedure address\n")); return FALSE; } @@ -2492,7 +2554,7 @@ int initialize() IN LPCWSTR szCardName)) load_func(hOriginal, "SCardForgetCardTypeW"); if (!Original_SCardForgetCardTypeW) { - fprintf(stderr, "Could not find SCardForgetCardTypeW procedure address\n"); + LogDebugString(_CONV("Could not find SCardForgetCardTypeW procedure address\n")); return FALSE; } @@ -2500,7 +2562,7 @@ int initialize() (HANDLE(STDCALL *)(void)) load_func(hOriginal, "SCardAccessStartedEvent"); if (!Original_SCardAccessStartedEvent) { - fprintf(stderr, "Could not find SCardAccessStartedEvent procedure address\n"); + LogDebugString(_CONV("Could not find SCardAccessStartedEvent procedure address\n")); return FALSE; } @@ -2508,7 +2570,7 @@ int initialize() (void(STDCALL *)(void)) load_func(hOriginal, "SCardReleaseStartedEvent"); if (!Original_SCardReleaseStartedEvent) { - fprintf(stderr, "Could not find SCardReleaseStartedEvent procedure address\n"); + LogDebugString(_CONV("Could not find SCardReleaseStartedEvent procedure address\n")); return FALSE; } @@ -2520,7 +2582,7 @@ int initialize() IN DWORD cReaders)) load_func(hOriginal, "SCardLocateCardsA"); if (!Original_SCardLocateCardsA) { - fprintf(stderr, "Could not find SCardLocateCardsA procedure address\n"); + LogDebugString(_CONV("Could not find SCardLocateCardsA procedure address\n")); return FALSE; } @@ -2532,7 +2594,7 @@ int initialize() IN DWORD cReaders)) load_func(hOriginal, "SCardLocateCardsW"); if (!Original_SCardLocateCardsW) { - fprintf(stderr, "Could not find SCardLocateCardsW procedure address\n"); + LogDebugString(_CONV("Could not find SCardLocateCardsW procedure address\n")); return FALSE; } @@ -2545,7 +2607,7 @@ int initialize() IN DWORD cReaders)) load_func(hOriginal, "SCardLocateCardsByATRA"); if (!Original_SCardLocateCardsByATRA) { - fprintf(stderr, "Could not find SCardLocateCardsByATRA procedure address\n"); + LogDebugString(_CONV("Could not find SCardLocateCardsByATRA procedure address\n")); return FALSE; } @@ -2558,7 +2620,7 @@ int initialize() IN DWORD cReaders)) load_func(hOriginal, "SCardLocateCardsByATRW"); if (!Original_SCardLocateCardsByATRW) { - fprintf(stderr, "Could not find SCardLocateCardsByATRW procedure address\n"); + LogDebugString(_CONV("Could not find SCardLocateCardsByATRW procedure address\n")); return FALSE; } @@ -2570,7 +2632,7 @@ int initialize() IN DWORD cReaders)) load_func(hOriginal, "SCardGetStatusChangeA"); if (!Original_SCardGetStatusChangeA) { - fprintf(stderr, "Could not find SCardGetStatusChangeA procedure address\n"); + LogDebugString(_CONV("Could not find SCardGetStatusChangeA procedure address\n")); return FALSE; } @@ -2582,7 +2644,7 @@ int initialize() IN DWORD cReaders)) load_func(hOriginal, "SCardGetStatusChangeW"); if (!Original_SCardGetStatusChangeW) { - fprintf(stderr, "Could not find SCardGetStatusChangeW procedure address\n"); + LogDebugString(_CONV("Could not find SCardGetStatusChangeW procedure address\n")); return FALSE; } @@ -2596,7 +2658,7 @@ int initialize() OUT LPDWORD pdwActiveProtocol)) load_func(hOriginal, "SCardConnectA"); if (!Original_SCardConnect) { - fprintf(stderr, "Could not find SCardConnectA procedure address\n"); + LogDebugString(_CONV("Could not find SCardConnectA procedure address\n")); return FALSE; } @@ -2610,7 +2672,7 @@ int initialize() OUT LPDWORD pdwActiveProtocol)) load_func(hOriginal, "SCardConnectW"); if (!Original_SCardConnectW) { - fprintf(stderr, "Could not find SCardConnectW procedure address\n"); + LogDebugString(_CONV("Could not find SCardConnectW procedure address\n")); return FALSE; } @@ -2619,7 +2681,7 @@ int initialize() IN SCARDHANDLE hCard)) load_func(hOriginal, "SCardCancelTransaction"); if (!Original_SCardCancelTransaction) { - CCommonFnc::File_AppendString(WINSCARD_RULES_LOG, _CONV("Could not find SCardCancelTransaction procedure address\n")); + LogDebugString(_CONV("Could not find SCardCancelTransaction procedure address\n")); } Original_SCardState = @@ -2631,7 +2693,7 @@ int initialize() IN OUT LPDWORD pcbAtrLen)) load_func(hOriginal, "SCardState"); if (!Original_SCardState) { - fprintf(stderr, "Could not find SCardState procedure address\n"); + LogDebugString(_CONV("Could not find SCardState procedure address\n")); return FALSE; } @@ -2646,7 +2708,7 @@ int initialize() IN OUT LPDWORD pcbAtrLen)) load_func(hOriginal, "SCardStatusA"); if (!Original_SCardStatus) { - fprintf(stderr, "Could not find SCardStatusA procedure address\n"); + LogDebugString(_CONV("Could not find SCardStatusA procedure address\n")); return FALSE; } @@ -2661,7 +2723,7 @@ int initialize() IN OUT LPDWORD pcbAtrLen)) load_func(hOriginal, "SCardStatusW"); if (!Original_SCardStatusW) { - fprintf(stderr, "Could not find SCardStatusW procedure address\n"); + LogDebugString(_CONV("Could not find SCardStatusW procedure address\n")); return FALSE; } @@ -2670,7 +2732,7 @@ int initialize() LPOPENCARDNAMEA_EX)) load_func(hOriginal, "SCardUIDlgSelectCardA"); if (!Original_SCardUIDlgSelectCardA) { - CCommonFnc::File_AppendString(WINSCARD_RULES_LOG, _CONV("Could not find SCardUIDlgSelectCardA procedure address\n")); + LogDebugString(_CONV("Could not find SCardUIDlgSelectCardA procedure address\n")); } Original_SCardUIDlgSelectCardW = @@ -2678,28 +2740,28 @@ int initialize() LPOPENCARDNAMEW_EX)) load_func(hOriginal, "SCardUIDlgSelectCardW"); if (!Original_SCardUIDlgSelectCardW) { - CCommonFnc::File_AppendString(WINSCARD_RULES_LOG, _CONV("Could not find SCardUIDlgSelectCardW procedure address\n")); + LogWinscardRules(_CONV("Could not find SCardUIDlgSelectCardW procedure address\n")); } Original_GetOpenCardNameA = (LONG(STDCALL *)(LPOPENCARDNAMEA)) load_func(hOriginal, "GetOpenCardNameA"); if (!Original_GetOpenCardNameA) { - CCommonFnc::File_AppendString(WINSCARD_RULES_LOG, _CONV("Could not find GetOpenCardNameA procedure address\n")); + LogWinscardRules(_CONV("Could not find GetOpenCardNameA procedure address\n")); } Original_GetOpenCardNameW = (LONG(STDCALL *)(LPOPENCARDNAMEW)) load_func(hOriginal, "GetOpenCardNameW"); if (!Original_GetOpenCardNameW) { - CCommonFnc::File_AppendString(WINSCARD_RULES_LOG, _CONV("Could not find GetOpenCardNameW procedure address\n")); + LogWinscardRules(_CONV("Could not find GetOpenCardNameW procedure address\n")); } Original_SCardDlgExtendedError = (LONG(STDCALL *)(void)) load_func(hOriginal, "SCardDlgExtendedError "); if (!Original_SCardDlgExtendedError) { - CCommonFnc::File_AppendString(WINSCARD_RULES_LOG, _CONV("Could not find SCardDlgExtendedError procedure address\n")); + LogWinscardRules(_CONV("Could not find SCardDlgExtendedError procedure address\n")); } #endif @@ -2718,7 +2780,7 @@ int initialize() load_func(hOriginal, "SCardConnect"); if (!Original_SCardConnect) { error = dlerror(); - fprintf(stderr, "Could not find SCardConnect procedure address: %s\n", error); + LogDebugString(_CONV("Could not find SCardConnect procedure address: %s\n", error); return FALSE; } @@ -2727,7 +2789,7 @@ int initialize() load_func(hOriginal, "SCardStatus"); if ((!Original_SCardStatus)) { error = dlerror(); - fprintf(stderr, "Could not find SCardStatus procedure address: %s\n", error); + LogDebugString(_CONV("Could not find SCardStatus procedure address: %s\n", error); return FALSE; } @@ -2808,7 +2870,7 @@ void GetDesktopPath(char_type* path) string_type stringPath = "/home/"; stringPath += login; stringPath += "/Desktop/APDUPlay/"; - type_copy(path, stringPath.c_str()); + type_copy(rulesFilePath, stringPath.c_str()); #else char_type appData[MAX_PATH]; SHGetFolderPath(NULL, CSIDL_DESKTOPDIRECTORY | CSIDL_FLAG_CREATE, NULL, SHGFP_TYPE_CURRENT, appData); @@ -2817,38 +2879,73 @@ void GetDesktopPath(char_type* path) #endif } + + #if defined (_WIN32) BOOL CWinscardApp::InitInstance() { CWinApp::InitInstance(); + // Check for user-defined path for debug file + char* debugPath = std::getenv(ENV_APDUPLAY_DEBUG_PATH.c_str()); + if (debugPath != NULL) { + APDUPLAY_DEBUG_FILE = debugPath; + } srand((int) time(NULL)); + LogDebugString(_CONV("#####################################################################################\n"), false); + LogDebugString(_CONV("InitInstance entered\n")); + +#if defined (_WIN32) && !defined(_WIN64) + LogDebugString(string_format(_CONV("APDUPlay winscard.dll v%s (32-bit version)\n"), LIBRARY_VERSION.c_str())); +#endif +#ifdef _WIN64 + LogDebugString(string_format(_CONV("APDUPlay winscard.dll v%s, (64-bit version)\n"), LIBRARY_VERSION.c_str())); +#endif + TCHAR szPath[MAX_PATH]; + memset(szPath, 0, sizeof(szPath) * sizeof(TCHAR)); + if (GetModuleFileName(NULL, szPath, MAX_PATH)) { + LogDebugString(string_format(_CONV("Target application: '%s'\n"), szPath)); + } + // LOAD MODIFICATION RULES LoadRules(); // CONNECT TO REMOTE SOCKET IF REQUIRED if (m_remoteConfig.bRedirect) { - Remote_Connect(&m_remoteConfig); + LogDebugString(_CONV("[REMOTE] Redirect = 1 => going to connect to specified socket (make sure socket is opened. If application terminates, try to set Redirect = 0 to disable remote redirecting)\n")); + Remote_Connect(&m_remoteConfig); } - if (theApp.m_winscardConfig.bLOG_EXCHANGED_APDU) CCommonFnc::File_AppendString(WINSCARD_LOG, _CONV("[begin]\r\n")); + if (theApp.m_winscardConfig.bLOG_EXCHANGED_APDU) CCommonFnc::File_AppendString(WINSCARD_LOG, _CONV("[begin]\n")); - return initialize(); + BOOL bStatus = initialize(); + LogDebugString(string_format(_CONV("Finalizing InitInstance with %s\n"), bStatus ? _CONV("true") : _CONV("false"))); + + return bStatus; } int CWinscardApp::Remote_Connect(REMOTE_CONFIG* pRemoteConfig) { string_type message; string_type sIP(pRemoteConfig->IP); - //pRemoteConfig->pSocket = new SocketClient(sIP, atoi(pRemoteConfig->port.c_str())); try { + message = string_format(_CONV("Connnecting to remote proxy with IP:port = %s:%s ... "), pRemoteConfig->IP.c_str(), pRemoteConfig->port.c_str()); + LogDebugString(message); pRemoteConfig->pSocket = new SocketClient(sIP, type_to_int(pRemoteConfig->port.c_str(), NULL, 10)); - message = string_format(_CONV("\n> Connnecting to remote proxy with IP:port = %s:%s"), pRemoteConfig->IP, pRemoteConfig->port); - CCommonFnc::File_AppendString(WINSCARD_RULES_LOG, message); + if (pRemoteConfig->pSocket != NULL) { + LogDebugString(_CONV("success"), false); + } + else { + LogDebugString(_CONV("failed"), false); + } } catch (std::string error) { - message = string_format(_CONV("\n> Failed to connect to %s:%s (error: %s)"), pRemoteConfig->IP, pRemoteConfig->port, error); - CCommonFnc::File_AppendString(WINSCARD_RULES_LOG, message); + message = string_format(_CONV("Failed to connect to %s:%s (error: %s)\n"), pRemoteConfig->IP.c_str(), pRemoteConfig->port.c_str(), error); + LogDebugString(message, false); + } + catch (...) { + message = string_format(_CONV("Failed to connect to %s:%s\n"), pRemoteConfig->IP.c_str(), pRemoteConfig->port.c_str()); + LogDebugString(message, false); } return STAT_OK; @@ -2904,7 +3001,7 @@ int CWinscardApp::ApplyRules(BYTE* pbBuffer, DWORD* pcbLength, int direction) { } } - // NEDETERMINISTIC SLEEP IF REQUIRED + // NONDETERMINISTIC SLEEP IF REQUIRED if (iter->msDelay > 0) { std::this_thread::sleep_for(std::chrono::milliseconds(iter->msDelay)); //_sleep(iter->msDelay); @@ -2987,8 +3084,8 @@ LONG CWinscardApp::Remote_SCardConnect(REMOTE_CONFIG* pRemoteConfig, string_type string_type l = Remote_FormatRequest(targetReader, theApp.m_remoteConfig.nextCommandID, CMD_RESET, "", "", CMD_LINE_SEPARATOR); pRemoteConfig->pSocket->SendLine(l); //message.Insert(0, "\n::-> "); - message.insert(0, _CONV("\n::-> ")); - CCommonFnc::File_AppendString(WINSCARD_RULES_LOG, message); + message.insert(0, _CONV("::-> ")); + LogWinscardRules(message); _sleep(500); // OBTAIN RESPONSE, PARSE BACK @@ -2999,18 +3096,18 @@ LONG CWinscardApp::Remote_SCardConnect(REMOTE_CONFIG* pRemoteConfig, string_type string_type response; status = Remote_ParseResponse(l, theApp.m_remoteConfig.nextCommandID, &response); - message = string_format(_CONV("\n::<- %s"), response.c_str()); - replace(message.begin(), message.end(), '\n', ' '); - CCommonFnc::File_AppendString(WINSCARD_RULES_LOG, message); + replace(response.begin(), response.end(), '\n', ' '); + message = string_format(_CONV("::<- %s\n"), response.c_str()); + LogWinscardRules(message); } catch (const char* s) { - message = string_format(_CONV("Remote_SCardConnect(), SendLine(%s), fail with (%s)"), message, s); - CCommonFnc::File_AppendString(WINSCARD_RULES_LOG, message); + message = string_format(_CONV("Remote_SCardConnect(), SendLine(%s), fail with (%s)\n"), message.c_str(), s); + LogWinscardRules(message); status = SCARD_F_UNKNOWN_ERROR; } catch (...) { - message = string_format(_CONV("Remote_SCardConnect(), SendLine(%s), fail with (unhandled exception)"), message); - CCommonFnc::File_AppendString(WINSCARD_RULES_LOG, message); + message = string_format(_CONV("Remote_SCardConnect(), SendLine(%s), fail with (unhandled exception)\n"), message.c_str()); + LogWinscardRules(message); status = SCARD_F_UNKNOWN_ERROR; } } @@ -3029,7 +3126,7 @@ LONG CWinscardApp::Remote_ParseResponse(string_type rawResponse, DWORD expectedC size_t pos = 0; if (rawResponse.at(pos) != '>') { - CCommonFnc::File_AppendString(WINSCARD_RULES_LOG, "'>'was expected at begin"); + LogWinscardRules("'>'was expected at begin"); status = SCARD_F_COMM_ERROR; } pos++; @@ -3038,7 +3135,7 @@ LONG CWinscardApp::Remote_ParseResponse(string_type rawResponse, DWORD expectedC size_t pos2 = rawResponse.find(CMD_SEPARATOR); string_type uniqueCmdID = rawResponse.substr(pos, pos2 - 1); if (expectedCommandID != atoi(uniqueCmdID.c_str())) { - CCommonFnc::File_AppendString(WINSCARD_RULES_LOG, "Unexpected commandID in response"); + LogWinscardRules("Unexpected commandID in response"); status = SCARD_F_COMM_ERROR; } pos = pos2 + 1; @@ -3067,8 +3164,8 @@ LONG CWinscardApp::Remote_SCardTransmit(REMOTE_CONFIG* pRemoteConfig, string_typ string_type l = Remote_FormatRequest(targetReader, theApp.m_remoteConfig.nextCommandID, CMD_APDU, value, "", CMD_LINE_SEPARATOR); pRemoteConfig->pSocket->SendLine(l); //message.Insert(0, "\n::-> "); - message.insert(0, _CONV("\n::-> ")); - CCommonFnc::File_AppendString(WINSCARD_RULES_LOG, message); + message.insert(0, _CONV("::-> ")); + LogWinscardRules(message); // SLEEP LONGER, IF MORE DATA WILL BE RETURNED BY SYSTEM 00 0c 00 00 xx CALL if (memcmp(pbSendBuffer, GET_APDU1, sizeof(GET_APDU1)) == 0 || memcmp(pbSendBuffer, GET_APDU2, sizeof(GET_APDU2)) == 0) { @@ -3082,10 +3179,9 @@ LONG CWinscardApp::Remote_SCardTransmit(REMOTE_CONFIG* pRemoteConfig, string_typ string_type response; status = Remote_ParseResponse(l, theApp.m_remoteConfig.nextCommandID, &response); - message = string_format(_CONV("\n::<- %s"), l.c_str()); - //message.Replace("\n", " "); - replace(message.begin(), message.end(), '\n', ' '); - CCommonFnc::File_AppendString(WINSCARD_RULES_LOG, message); + replace(l.begin(), l.end(), '\n', ' '); + message = string_format(_CONV("::<- %s\n"), l.c_str()); + LogWinscardRules(message); if (response.find(CMD_RESPONSE_FAIL) == string_type::npos) { // RESPONSE CORRECT @@ -3102,13 +3198,13 @@ LONG CWinscardApp::Remote_SCardTransmit(REMOTE_CONFIG* pRemoteConfig, string_typ } } catch (const char* s) { - message = string_format(_CONV("\nRemote_SCardTransmit(), SendLine(%s), fail with (%s)"), message, s); - CCommonFnc::File_AppendString(WINSCARD_RULES_LOG, message); + message = string_format(_CONV("\nRemote_SCardTransmit(), SendLine(%s), fail with (%s)\n"), message.c_str(), s); + LogWinscardRules(message); status = SCARD_F_UNKNOWN_ERROR; } catch (...) { - message = string_format(_CONV("\nRemote_SCardTransmit(), SendLine(%s), fail with (unhandled exception)"), message); - CCommonFnc::File_AppendString(WINSCARD_RULES_LOG, message); + message = string_format(_CONV("\nRemote_SCardTransmit(), SendLine(%s), fail with (unhandled exception)\n"), message.c_str()); + LogWinscardRules(message); status = SCARD_F_UNKNOWN_ERROR; } } @@ -3172,12 +3268,6 @@ int CWinscardApp::LoadRule(const char_type* section_name, dictionary* dict/*stri m_winscardConfig.bMODIFY_APDU_BY_RULES = value; } - type_copy(sec_and_key, section_name); - if ((value = iniparser_getboolean(dict, type_cat(sec_and_key, _CONV(":LOG_FUNCTIONS_CALLS")), 2)) != 2) - { - m_winscardConfig.bLOG_FUNCTIONS_CALLS = value; - } - type_copy(sec_and_key, section_name); char_value = iniparser_getstring(dict, type_cat(sec_and_key, _CONV(":READER_ORDERED_FIRST")), ""); if (type_length(char_value) != 0) @@ -3197,6 +3287,14 @@ int CWinscardApp::LoadRule(const char_type* section_name, dictionary* dict/*stri m_remoteConfig.bRedirect = value; } + type_copy(sec_and_key, section_name); + char_value = iniparser_getstring(dict, type_cat(sec_and_key, _CONV(":REMOTE_READER_PREFIX")), ""); + if (type_length(char_value) != 0) + { + m_remoteConfig.remoteReaderPrefix = char_value; + } + + type_copy(sec_and_key, section_name); char_value = iniparser_getstring(dict, type_cat(sec_and_key, _CONV(":IP")), ""); if (type_length(char_value) != 0) @@ -3486,8 +3584,9 @@ int CWinscardApp::LoadRule(const char_type* section_name, dictionary* dict/*stri int CWinscardApp::LoadRules() { int status = STAT_OK; FILE *file = NULL; - char_type path[256]; - type_copy(path, RULE_FILE.c_str()); + char_type rulesFilePath[256]; + char_type baseDir[256]; // base rulesFilePath for input/output files + type_copy(rulesFilePath, RULE_FILE.c_str()); string_type date_and_time = getCurrentTimeString(); @@ -3498,33 +3597,53 @@ int CWinscardApp::LoadRules() { // Searching for 'winscard_rules.txt' (priority) // 1. Lookup in local directory // 2. Lookup for APDUPLAY environmental variable - // 3. Lookup on user Desktop + // (UNUSED) 3. Lookup on user Desktop if (!file) { // 1. Lookup in local directory - file = fopen(path, "r"); + LogDebugString(string_format(_CONV("Going to open %s ... "), rulesFilePath)); + file = fopen(rulesFilePath, "r"); + if (file) { + LogDebugString(_CONV("success\n"), false); + type_copy(baseDir, ""); + } + else { + LogDebugString(_CONV("fail\n"), false); + } } if (!file) { // 2. Lookup for APDUPLAY environmental variable - char* configPath = std::getenv(ENV_WINSCARD_RULES_PATH.c_str()); + char* configPath = std::getenv(ENV_APDUPLAY_WINSCARD_RULES_PATH.c_str()); + LogDebugString(string_format(_CONV("Going to query %s env variable ... "), ENV_APDUPLAY_WINSCARD_RULES_PATH.c_str())); if (configPath != NULL) { + LogDebugString(string_format(_CONV(" defined (%s)\n"), configPath), false); // variable detected, try to open string newRuleFile = string_format(_CONV("%s\\%s"), configPath, RULE_FILE.c_str()); + LogDebugString(string_format(_CONV("Going to open %s ... "), newRuleFile.c_str())); file = fopen(newRuleFile.c_str(), "r"); if (file) { - type_copy(path, newRuleFile.c_str()); + LogDebugString(_CONV("success\n"), false); + type_copy(rulesFilePath, newRuleFile.c_str()); + type_copy(baseDir, configPath); } + else { + LogDebugString(_CONV("fail\n"), false); + } + } + else { + LogDebugString(_CONV("not found\n"), false); } } +/* if (!file) { // 3. Lookup on user Desktop - GetDesktopPath(path); - type_cat(path, RULE_FILE.c_str()); - file = fopen(path, "r"); // try to open file on desktop + GetDesktopPath(rulesFilePath); + type_cat(rulesFilePath, RULE_FILE.c_str()); + file = fopen(rulesFilePath, "r"); // try to open file on desktop } - +*/ if (file) { fclose(file); - dictionary* dict = iniparser_load(path); + dictionary* dict = iniparser_load(rulesFilePath); int number_of_sections = iniparser_getnsec(dict); @@ -3539,21 +3658,26 @@ int CWinscardApp::LoadRules() { iniparser_freedict(dict); } else { - CCommonFnc::File_AppendString(WINSCARD_RULES_LOG, _CONV("Rules file NOT found\n")); + LogWinscardRules(_CONV("Rules file NOT found\n")); } if (!m_winscardConfig.sLOG_BASE_PATH.empty()) { - WINSCARD_RULES_LOG = string_format(_CONV("%s%s"), m_winscardConfig.sLOG_BASE_PATH.c_str(), WINSCARD_RULES_LOG.c_str()); - WINSCARD_LOG = string_format(_CONV("%s%s"), m_winscardConfig.sLOG_BASE_PATH.c_str(), WINSCARD_LOG.c_str()); - INSTRUCTION_FILE = string_format(_CONV("%s%s"), m_winscardConfig.sLOG_BASE_PATH.c_str(), INSTRUCTION_FILE.c_str()); + LogDebugString(string_format(_CONV("Logging base path is forced as '%s' (LOG_BASE_PATH)\n"), m_winscardConfig.sLOG_BASE_PATH.c_str())); + + // Use provided directory to store output files + WINSCARD_RULES_LOG = string_format(_CONV("%s\\%s"), m_winscardConfig.sLOG_BASE_PATH.c_str(), WINSCARD_RULES_LOG.c_str()); + WINSCARD_LOG = string_format(_CONV("%s\\%s"), m_winscardConfig.sLOG_BASE_PATH.c_str(), WINSCARD_LOG.c_str()); + INSTRUCTION_FILE = string_format(_CONV("%s\\%s"), m_winscardConfig.sLOG_BASE_PATH.c_str(), INSTRUCTION_FILE.c_str()); } else { - GetDesktopPath(path); - WINSCARD_RULES_LOG = string_format(_CONV("%s%s"), path, WINSCARD_RULES_LOG.c_str()); - WINSCARD_LOG = string_format(_CONV("%s%s"), path, WINSCARD_LOG.c_str()); - INSTRUCTION_FILE = string_format(_CONV("%s%s"), path, INSTRUCTION_FILE.c_str()); + LogDebugString(string_format(_CONV("Logging base path not forced, using '%s'\n"), baseDir)); + + // Use base directory to store output files + WINSCARD_RULES_LOG = string_format(_CONV("%s%s"), baseDir, WINSCARD_RULES_LOG.c_str()); + WINSCARD_LOG = string_format(_CONV("%s%s"), baseDir, WINSCARD_LOG.c_str()); + INSTRUCTION_FILE = string_format(_CONV("%s%s"), baseDir, INSTRUCTION_FILE.c_str()); } std::fstream instruction_file; @@ -3563,11 +3687,13 @@ int CWinscardApp::LoadRules() { { theApp.m_winscardConfig.bLOG_WRITE_DESCRIPTION = TRUE; instruction_file.close(); - CCommonFnc::File_AppendString(WINSCARD_RULES_LOG, _CONV("Instruction file found")); - instructionDict = iniparser_load((const char*)INSTRUCTION_FILE.c_str()); + LogWinscardRules(_CONV("Instruction file found")); + instructionDict = iniparser_load((const char*) INSTRUCTION_FILE.c_str()); } - return status; + LogDebugString(string_format(_CONV("Finalizing LoadRules with status %s\n"), status)); + + return status; } #endif @@ -3620,9 +3746,6 @@ int CWinscardApp::LoadRule(string_type ruleName, string_type filePath) { if ((GetPrivateProfileString(ruleName.c_str(), _CONV("MODIFY_APDU_BY_RULES"), _CONV(""), buffer, cBuffer, filePath.c_str())) > 0) { m_winscardConfig.bMODIFY_APDU_BY_RULES = (type_to_int(buffer, NULL, 10) == 0) ? FALSE : TRUE; } - if ((GetPrivateProfileString(ruleName.c_str(), _CONV("LOG_FUNCTIONS_CALLS"), _CONV(""), buffer, cBuffer, filePath.c_str())) > 0) { - m_winscardConfig.bLOG_FUNCTIONS_CALLS = (type_to_int(buffer, NULL, 10) == 0) ? FALSE : TRUE; - } if ((GetPrivateProfileString(ruleName.c_str(), _CONV("READER_ORDERED_FIRST"), _CONV(""), buffer, cBuffer, filePath.c_str())) > 0) { m_winscardConfig.sREADER_ORDERED_FIRST = buffer; } @@ -3854,19 +3977,19 @@ int CWinscardApp::LoadRules() { memset(buffer, 0, cBuffer); - char_type path[256]; - type_copy(path, RULE_FILE.c_str()); + char_type rulesFilePath[256]; + type_copy(rulesFilePath, RULE_FILE.c_str()); string_type date_and_time = getCurrentTimeString(); WINSCARD_LOG += _CONV("_") + date_and_time + _CONV(".txt"); WINSCARD_RULES_LOG += _CONV("_") + date_and_time + _CONV(".txt"); - if (!(file = type_fopen(path, _CONV("r")))) // try to open file in current directory + if (!(file = type_fopen(rulesFilePath, _CONV("r")))) // try to open file in current directory { - type_copy(path, GetDesktopPath()); - type_cat(path, RULE_FILE.c_str()); - file = type_fopen(path, _CONV("r")); // try to open file on desktop + type_copy(rulesFilePath, GetDesktopPath()); + type_cat(rulesFilePath, RULE_FILE.c_str()); + file = type_fopen(rulesFilePath, _CONV("r")); // try to open file on desktop } if (file) { @@ -3879,22 +4002,22 @@ int CWinscardApp::LoadRules() { string_type message; message = string_format(_CONV("Rules file found: %s\n"), filePath.c_str()); - CCommonFnc::File_AppendString(WINSCARD_RULES_LOG, message); + LogWinscardRules(message); // OBTAIN SECTION NAMES - if ((cReaded = GetPrivateProfileString(NULL, NULL, _CONV(""), buffer, cBuffer, path)) != 0) { + if ((cReaded = GetPrivateProfileString(NULL, NULL, _CONV(""), buffer, cBuffer, rulesFilePath)) != 0) { // PARSE SECTION NAMES, TRY TO LOAD EACH RULE CCommonFnc::String_ParseNullSeparatedArray(buffer, cBuffer, &valuesList); for (auto iter = valuesList.begin(); iter != valuesList.end(); iter++) { - LoadRule(*iter, path); + LoadRule(*iter, rulesFilePath); } } m_bRulesActive = TRUE; } else { - CCommonFnc::File_AppendString(WINSCARD_RULES_LOG, _CONV("Rules file NOT found\n")); + LogWinscardRules(_CONV("Rules file NOT found\n")); } if (!m_winscardConfig.sLOG_BASE_PATH.empty()) @@ -3915,7 +4038,7 @@ int CWinscardApp::LoadRules() { { theApp.m_winscardConfig.bLOG_WRITE_DESCRIPTION = TRUE; instruction_file.close(); - CCommonFnc::File_AppendString(WINSCARD_RULES_LOG, _CONV("Instruction file found")); + LogWinscardRules(_CONV("Instruction file found")); instructionDict = iniparser_load((const char*)INSTRUCTION_FILE.c_str()); } diff --git a/Winscard/Winscard.def b/Winscard/Winscard.def deleted file mode 100644 index 7fc794d..0000000 --- a/Winscard/Winscard.def +++ /dev/null @@ -1,157 +0,0 @@ -; Copyright (c) 2001 Andrew Fernandes . All rights reserved. -; -; This work may be used and distributed under the terms of the GNU Public License, -; as written in the included file "gpl.txt" or online at . -; -LIBRARY "winscard" -EXPORTS -; -; functions to pass transparently -; -;SCardAccessNewReaderEvent=original.SCardAccessNewReaderEvent @1 -;SCardAccessStartedEvent=original.SCardAccessStartedEvent @2 -;SCardAddReaderToGroupA=original.SCardAddReaderToGroupA @6 -;SCardAddReaderToGroupW=original.SCardAddReaderToGroupW @7 -;SCardBeginTransaction=original.SCardBeginTransaction @8 -;SCardCancel=original.SCardCancel @9 -;SCardConnectA=original.SCardConnectA @10 -;SCardConnectW=original.SCardConnectW @11 -;SCardControl=original.SCardControl @12 -;SCardDisconnect=original.SCardDisconnect @13 -;SCardEndTransaction=original.SCardEndTransaction @14 -;SCardEstablishContext=original.SCardEstablishContext @15 -;SCardForgetCardTypeA=original.SCardForgetCardTypeA @16 -;SCardForgetCardTypeW=original.SCardForgetCardTypeW @17 -;SCardForgetReaderA=original.SCardForgetReaderA @18 -;SCardForgetReaderGroupA=original.SCardForgetReaderGroupA @19 -;SCardForgetReaderGroupW=original.SCardForgetReaderGroupW @20 -;SCardForgetReaderW=original.SCardForgetReaderW @21 -;SCardFreeMemory=original.SCardFreeMemory @22 -;SCardGetAttrib=original.SCardGetAttrib @23 -;SCardGetCardTypeProviderNameA=original.SCardGetCardTypeProviderNameA @24 -;SCardGetCardTypeProviderNameW=original.SCardGetCardTypeProviderNameW @25 -;SCardGetProviderIdA=original.SCardGetProviderIdA @26 -;SCardGetProviderIdW=original.SCardGetProviderIdW @27 -;SCardGetStatusChangeA=original.SCardGetStatusChangeA @28 -;SCardGetStatusChangeW=original.SCardGetStatusChangeW @29 -;SCardIntroduceCardTypeA=original.SCardIntroduceCardTypeA @30 -;SCardIntroduceCardTypeW=original.SCardIntroduceCardTypeW @31 -;SCardIntroduceReaderA=original.SCardIntroduceReaderA @32 -;SCardIntroduceReaderGroupA=original.SCardIntroduceReaderGroupA @33 -;SCardIntroduceReaderGroupW=original.SCardIntroduceReaderGroupW @34 -;SCardIntroduceReaderW=original.SCardIntroduceReaderW @35 -;SCardIsValidContext=original.SCardIsValidContext @36 -;SCardListCardsA=original.SCardListCardsA @37 -;SCardListCardsW=original.SCardListCardsW @38 -;SCardListInterfacesA=original.SCardListInterfacesA @39 -;SCardListInterfacesW=original.SCardListInterfacesW @40 -;SCardListReaderGroupsA=original.SCardListReaderGroupsA @41 -;SCardListReaderGroupsW=original.SCardListReaderGroupsW @42 -;SCardListReadersA=original.SCardListReadersA @43 -;SCardListReadersW=original.SCardListReadersW @44 -;SCardLocateCardsA=original.SCardLocateCardsA @45 -;SCardLocateCardsW=original.SCardLocateCardsW @46 -;SCardReconnect=original.SCardReconnect @47 -;SCardReleaseAllEvents=original.SCardReleaseAllEvents @3 -;SCardReleaseContext=original.SCardReleaseContext @48 -;SCardReleaseNewReaderEvent=original.SCardReleaseNewReaderEvent @4 -;SCardReleaseStartedEvent=original.SCardReleaseStartedEvent @5 -;SCardRemoveReaderFromGroupA=original.SCardRemoveReaderFromGroupA @49 -;SCardRemoveReaderFromGroupW=original.SCardRemoveReaderFromGroupW @50 -;SCardSetAttrib=original.SCardSetAttrib @51 -;SCardSetCardTypeProviderNameA=original.SCardSetCardTypeProviderNameA @52 -;SCardSetCardTypeProviderNameW=original.SCardSetCardTypeProviderNameW @53 -;SCardState=original.SCardState @54 -;SCardStatusA=original.SCardStatusA @55 -;SCardStatusW=original.SCardStatusW @56 -; -; initialized shared data symbols -; -;g_rgSCardRawPci=original.g_rgSCardRawPci @58 DATA -;g_rgSCardT0Pci=original.g_rgSCardT0Pci @59 DATA -;g_rgSCardT1Pci=original.g_rgSCardT1Pci @60 DATA -; -; functions to be intercepted -; - -SCardAudit=original.SCardAudit@8 -SCardGetDeviceTypeIdA=original.SCardGetDeviceTypeIdA@12 -SCardGetDeviceTypeIdW=original.SCardGetDeviceTypeIdW@12 -SCardGetReaderDeviceInstanceIdA=original.SCardGetReaderDeviceInstanceIdA@16 -SCardGetReaderDeviceInstanceIdW=original.SCardGetReaderDeviceInstanceIdW@16 -SCardGetReaderIconA=original.SCardGetReaderIconA@16 -SCardGetReaderIconW=original.SCardGetReaderIconW@16 -SCardGetTransmitCount=original.SCardGetTransmitCount@8 -SCardListReadersWithDeviceInstanceIdA=original.SCardListReadersWithDeviceInstanceIdA@16 -SCardListReadersWithDeviceInstanceIdW=original.SCardListReadersWithDeviceInstanceIdW@16 -SCardLocateCardsByATRA=original.SCardLocateCardsByATRA@20 -SCardLocateCardsByATRW=original.SCardLocateCardsByATRW@20 -SCardReadCacheA=original.SCardReadCacheA@24 -SCardReadCacheW=original.SCardReadCacheW@24 -SCardWriteCacheA=original.SCardWriteCacheA@24 -SCardWriteCacheW=original.SCardWriteCacheW@24 - -g_rgSCardRawPci=original.g_rgSCardRawPci DATA -g_rgSCardT0Pci=original.g_rgSCardT0Pci DATA -g_rgSCardT1Pci=original.g_rgSCardT1Pci DATA - -SCardTransmit @57 -SCardConnectW @11 -SCardDisconnect @13 -SCardListReadersA @43 -SCardListReadersW @44 -SCardFreeMemory @22 - -SCardAccessStartedEvent @2 -SCardAddReaderToGroupA @6 -SCardAddReaderToGroupW @7 -SCardBeginTransaction @8 -SCardCancel @9 -SCardConnectA @10 -SCardConnectW @11 -SCardControl @12 -SCardDisconnect @13 -SCardEndTransaction @14 -SCardEstablishContext @15 -SCardForgetCardTypeA @16 -SCardForgetCardTypeW @17 -SCardForgetReaderA @18 -SCardForgetReaderGroupA @19 -SCardForgetReaderGroupW @20 -SCardForgetReaderW @21 -SCardFreeMemory @22 -SCardGetAttrib @23 -SCardGetCardTypeProviderNameA @24 -SCardGetCardTypeProviderNameW @25 -SCardGetProviderIdA @26 -SCardGetProviderIdW @27 -SCardGetStatusChangeA @28 -SCardGetStatusChangeW @29 -SCardIntroduceCardTypeA @30 -SCardIntroduceCardTypeW @31 -SCardIntroduceReaderA @32 -SCardIntroduceReaderGroupA @33 -SCardIntroduceReaderGroupW @34 -SCardIntroduceReaderW @35 -SCardIsValidContext @36 -SCardListCardsA @37 -SCardListCardsW @38 -SCardListInterfacesA @39 -SCardListInterfacesW @40 -SCardListReaderGroupsA @41 -SCardListReaderGroupsW @42 -SCardListReadersA @43 -SCardListReadersW @44 -SCardLocateCardsA @45 -SCardLocateCardsW @46 -SCardReconnect @47 -SCardReleaseContext @48 -SCardReleaseStartedEvent @5 -SCardRemoveReaderFromGroupA @49 -SCardRemoveReaderFromGroupW @50 -SCardSetAttrib @51 -SCardSetCardTypeProviderNameA @52 -SCardSetCardTypeProviderNameW @53 -SCardState @54 -SCardStatusA @55 -SCardStatusW @56 diff --git a/Winscard/Winscard.h b/Winscard/Winscard.h index ab0bedd..393adf2 100644 --- a/Winscard/Winscard.h +++ b/Winscard/Winscard.h @@ -38,6 +38,7 @@ class CWinscardApp : public CWinApp unordered_map cardReaderMap; unordered_map remoteReadersMap; DWORD m_nextRemoteCardID = 1; + #if defined (_WIN32) diff --git a/Winscard/Winscard.rc b/Winscard/Winscard.rc index e8bc4fc..ea83711 100644 --- a/Winscard/Winscard.rc +++ b/Winscard/Winscard.rc @@ -25,25 +25,25 @@ END 2 TEXTINCLUDE BEGIN - "#include ""afxres.h""\r\n" + "#include ""afxres.h""\n" "\0" END 3 TEXTINCLUDE BEGIN - "#define _AFX_NO_SPLITTER_RESOURCES\r\n" - "#define _AFX_NO_OLE_RESOURCES\r\n" - "#define _AFX_NO_TRACKER_RESOURCES\r\n" - "#define _AFX_NO_PROPERTY_RESOURCES\r\n" - "\r\n" - "#if !defined(AFX_RESOURCE_DLL) || defined(AFX_TARG_CSY)\r\n" - "LANGUAGE 5, 1\r\n" - "#pragma code_page(1250)\r\n" - "#include ""res\\Winscard.rc2"" // non-Microsoft Visual C++ edited resources\r\n" + "#define _AFX_NO_SPLITTER_RESOURCES\n" + "#define _AFX_NO_OLE_RESOURCES\n" + "#define _AFX_NO_TRACKER_RESOURCES\n" + "#define _AFX_NO_PROPERTY_RESOURCES\n" + "\n" + "#if !defined(AFX_RESOURCE_DLL) || defined(AFX_TARG_CSY)\n" + "LANGUAGE 5, 1\n" + "#pragma code_page(1250)\n" + "#include ""res\\Winscard.rc2"" // non-Microsoft Visual C++ edited resources\n" #ifndef _AFXDLL - "#include ""afxres.rc"" // Standard components\r\n" + "#include ""afxres.rc"" // Standard components\n" #endif - "#endif\r\n" + "#endif\n" "\0" END diff --git a/Winscard/Winscard.vcproj b/Winscard/Winscard.vcproj index 0853e80..432e907 100644 --- a/Winscard/Winscard.vcproj +++ b/Winscard/Winscard.vcproj @@ -66,7 +66,6 @@ /> $(IntDir);%(AdditionalIncludeDirectories) - original.lib;%(AdditionalDependencies) - .\Winscard.def + %(AdditionalDependencies) + .\Winscard32.def true Windows MachineX86 @@ -248,7 +248,7 @@ $(IntDir);%(AdditionalIncludeDirectories) - original.lib;%(AdditionalDependencies) + %(AdditionalDependencies) .\Winscard.def true Windows @@ -277,8 +277,8 @@ $(IntDir);%(AdditionalIncludeDirectories) - original.lib;%(AdditionalDependencies) - .\Winscard.def + %(AdditionalDependencies) + .\Winscard64.def true Windows @@ -310,8 +310,8 @@ $(IntDir);%(AdditionalIncludeDirectories) - original.lib;%(AdditionalDependencies) - .\Winscard.def + %(AdditionalDependencies) + .\Winscard32.def true Windows true @@ -344,7 +344,7 @@ $(IntDir);%(AdditionalIncludeDirectories) - original.lib;%(AdditionalDependencies) + %(AdditionalDependencies) .\Winscard.def true Windows @@ -374,8 +374,8 @@ $(IntDir);%(AdditionalIncludeDirectories) - original.lib;%(AdditionalDependencies) - .\Winscard.def + %(AdditionalDependencies) + .\Winscard64.def true Windows true @@ -400,8 +400,8 @@ $(IntDir);%(AdditionalIncludeDirectories) - original.lib;%(AdditionalDependencies) - .\Winscard.def + %(AdditionalDependencies) + .\Winscard32.def true Windows true @@ -427,7 +427,7 @@ $(IntDir);%(AdditionalIncludeDirectories) - original.lib;%(AdditionalDependencies) + %(AdditionalDependencies) .\Winscard.def true Windows @@ -453,8 +453,8 @@ $(IntDir);%(AdditionalIncludeDirectories) - original.lib;%(AdditionalDependencies) - .\Winscard.def + %(AdditionalDependencies) + .\Winscard64.def true Windows true @@ -497,7 +497,6 @@ - diff --git a/Winscard/Winscard.vcxproj.filters b/Winscard/Winscard.vcxproj.filters index 3ebfb47..81fa3fc 100644 --- a/Winscard/Winscard.vcxproj.filters +++ b/Winscard/Winscard.vcxproj.filters @@ -32,9 +32,6 @@ - - Source Files - Resource Files diff --git a/Winscard/Winscard32.def b/Winscard/Winscard32.def new file mode 100644 index 0000000..bd09105 --- /dev/null +++ b/Winscard/Winscard32.def @@ -0,0 +1,157 @@ +; Copyright (c) 2001 Andrew Fernandes . All rights reserved. +; +; This work may be used and distributed under the terms of the GNU Public License, +; as written in the included file "gpl.txt" or online at . +; +LIBRARY "winscard" +EXPORTS +; +; functions to pass transparently +; +;SCardAccessNewReaderEvent=original32.SCardAccessNewReaderEvent @1 +;SCardAccessStartedEvent=original32.SCardAccessStartedEvent @2 +;SCardAddReaderToGroupA=original32.SCardAddReaderToGroupA @6 +;SCardAddReaderToGroupW=original32.SCardAddReaderToGroupW @7 +;SCardBeginTransaction=original32.SCardBeginTransaction @8 +;SCardCancel=original32.SCardCancel @9 +;SCardConnectA=original32.SCardConnectA @10 +;SCardConnectW=original32.SCardConnectW @11 +;SCardControl=original32.SCardControl @12 +;SCardDisconnect=original32.SCardDisconnect @13 +;SCardEndTransaction=original32.SCardEndTransaction @14 +;SCardEstablishContext=original32.SCardEstablishContext @15 +;SCardForgetCardTypeA=original32.SCardForgetCardTypeA @16 +;SCardForgetCardTypeW=original32.SCardForgetCardTypeW @17 +;SCardForgetReaderA=original32.SCardForgetReaderA @18 +;SCardForgetReaderGroupA=original32.SCardForgetReaderGroupA @19 +;SCardForgetReaderGroupW=original32.SCardForgetReaderGroupW @20 +;SCardForgetReaderW=original32.SCardForgetReaderW @21 +;SCardFreeMemory=original32.SCardFreeMemory @22 +;SCardGetAttrib=original32.SCardGetAttrib @23 +;SCardGetCardTypeProviderNameA=original32.SCardGetCardTypeProviderNameA @24 +;SCardGetCardTypeProviderNameW=original32.SCardGetCardTypeProviderNameW @25 +;SCardGetProviderIdA=original32.SCardGetProviderIdA @26 +;SCardGetProviderIdW=original32.SCardGetProviderIdW @27 +;SCardGetStatusChangeA=original32.SCardGetStatusChangeA @28 +;SCardGetStatusChangeW=original32.SCardGetStatusChangeW @29 +;SCardIntroduceCardTypeA=original32.SCardIntroduceCardTypeA @30 +;SCardIntroduceCardTypeW=original32.SCardIntroduceCardTypeW @31 +;SCardIntroduceReaderA=original32.SCardIntroduceReaderA @32 +;SCardIntroduceReaderGroupA=original32.SCardIntroduceReaderGroupA @33 +;SCardIntroduceReaderGroupW=original32.SCardIntroduceReaderGroupW @34 +;SCardIntroduceReaderW=original32.SCardIntroduceReaderW @35 +;SCardIsValidContext=original32.SCardIsValidContext @36 +;SCardListCardsA=original32.SCardListCardsA @37 +;SCardListCardsW=original32.SCardListCardsW @38 +;SCardListInterfacesA=original32.SCardListInterfacesA @39 +;SCardListInterfacesW=original32.SCardListInterfacesW @40 +;SCardListReaderGroupsA=original32.SCardListReaderGroupsA @41 +;SCardListReaderGroupsW=original32.SCardListReaderGroupsW @42 +;SCardListReadersA=original32.SCardListReadersA @43 +;SCardListReadersW=original32.SCardListReadersW @44 +;SCardLocateCardsA=original32.SCardLocateCardsA @45 +;SCardLocateCardsW=original32.SCardLocateCardsW @46 +;SCardReconnect=original32.SCardReconnect @47 +;SCardReleaseAllEvents=original32.SCardReleaseAllEvents @3 +;SCardReleaseContext=original32.SCardReleaseContext @48 +;SCardReleaseNewReaderEvent=original32.SCardReleaseNewReaderEvent @4 +;SCardReleaseStartedEvent=original32.SCardReleaseStartedEvent @5 +;SCardRemoveReaderFromGroupA=original32.SCardRemoveReaderFromGroupA @49 +;SCardRemoveReaderFromGroupW=original32.SCardRemoveReaderFromGroupW @50 +;SCardSetAttrib=original32.SCardSetAttrib @51 +;SCardSetCardTypeProviderNameA=original32.SCardSetCardTypeProviderNameA @52 +;SCardSetCardTypeProviderNameW=original32.SCardSetCardTypeProviderNameW @53 +;SCardState=original32.SCardState @54 +;SCardStatusA=original32.SCardStatusA @55 +;SCardStatusW=original32.SCardStatusW @56 +; +; initialized shared data symbols +; +;g_rgSCardRawPci=original32.g_rgSCardRawPci @58 DATA +;g_rgSCardT0Pci=original32.g_rgSCardT0Pci @59 DATA +;g_rgSCardT1Pci=original32.g_rgSCardT1Pci @60 DATA +; +; functions to be intercepted +; + +SCardAudit=original32.SCardAudit@8 +SCardGetDeviceTypeIdA=original32.SCardGetDeviceTypeIdA@12 +SCardGetDeviceTypeIdW=original32.SCardGetDeviceTypeIdW@12 +SCardGetReaderDeviceInstanceIdA=original32.SCardGetReaderDeviceInstanceIdA@16 +SCardGetReaderDeviceInstanceIdW=original32.SCardGetReaderDeviceInstanceIdW@16 +SCardGetReaderIconA=original32.SCardGetReaderIconA@16 +SCardGetReaderIconW=original32.SCardGetReaderIconW@16 +SCardGetTransmitCount=original32.SCardGetTransmitCount@8 +SCardListReadersWithDeviceInstanceIdA=original32.SCardListReadersWithDeviceInstanceIdA@16 +SCardListReadersWithDeviceInstanceIdW=original32.SCardListReadersWithDeviceInstanceIdW@16 +SCardLocateCardsByATRA=original32.SCardLocateCardsByATRA@20 +SCardLocateCardsByATRW=original32.SCardLocateCardsByATRW@20 +SCardReadCacheA=original32.SCardReadCacheA@24 +SCardReadCacheW=original32.SCardReadCacheW@24 +SCardWriteCacheA=original32.SCardWriteCacheA@24 +SCardWriteCacheW=original32.SCardWriteCacheW@24 + +g_rgSCardRawPci=original32.g_rgSCardRawPci DATA +g_rgSCardT0Pci=original32.g_rgSCardT0Pci DATA +g_rgSCardT1Pci=original32.g_rgSCardT1Pci DATA + +SCardTransmit @57 +SCardConnectW @11 +SCardDisconnect @13 +SCardListReadersA @43 +SCardListReadersW @44 +SCardFreeMemory @22 + +SCardAccessStartedEvent @2 +SCardAddReaderToGroupA @6 +SCardAddReaderToGroupW @7 +SCardBeginTransaction @8 +SCardCancel @9 +SCardConnectA @10 +SCardConnectW @11 +SCardControl @12 +SCardDisconnect @13 +SCardEndTransaction @14 +SCardEstablishContext @15 +SCardForgetCardTypeA @16 +SCardForgetCardTypeW @17 +SCardForgetReaderA @18 +SCardForgetReaderGroupA @19 +SCardForgetReaderGroupW @20 +SCardForgetReaderW @21 +SCardFreeMemory @22 +SCardGetAttrib @23 +SCardGetCardTypeProviderNameA @24 +SCardGetCardTypeProviderNameW @25 +SCardGetProviderIdA @26 +SCardGetProviderIdW @27 +SCardGetStatusChangeA @28 +SCardGetStatusChangeW @29 +SCardIntroduceCardTypeA @30 +SCardIntroduceCardTypeW @31 +SCardIntroduceReaderA @32 +SCardIntroduceReaderGroupA @33 +SCardIntroduceReaderGroupW @34 +SCardIntroduceReaderW @35 +SCardIsValidContext @36 +SCardListCardsA @37 +SCardListCardsW @38 +SCardListInterfacesA @39 +SCardListInterfacesW @40 +SCardListReaderGroupsA @41 +SCardListReaderGroupsW @42 +SCardListReadersA @43 +SCardListReadersW @44 +SCardLocateCardsA @45 +SCardLocateCardsW @46 +SCardReconnect @47 +SCardReleaseContext @48 +SCardReleaseStartedEvent @5 +SCardRemoveReaderFromGroupA @49 +SCardRemoveReaderFromGroupW @50 +SCardSetAttrib @51 +SCardSetCardTypeProviderNameA @52 +SCardSetCardTypeProviderNameW @53 +SCardState @54 +SCardStatusA @55 +SCardStatusW @56 diff --git a/Winscard/Winscard64.def b/Winscard/Winscard64.def new file mode 100644 index 0000000..b945139 --- /dev/null +++ b/Winscard/Winscard64.def @@ -0,0 +1,157 @@ +; Copyright (c) 2001 Andrew Fernandes . All rights reserved. +; +; This work may be used and distributed under the terms of the GNU Public License, +; as written in the included file "gpl.txt" or online at . +; +LIBRARY "winscard" +EXPORTS +; +; functions to pass transparently +; +;SCardAccessNewReaderEvent=original64.SCardAccessNewReaderEvent @1 +;SCardAccessStartedEvent=original64.SCardAccessStartedEvent @2 +;SCardAddReaderToGroupA=original64.SCardAddReaderToGroupA @6 +;SCardAddReaderToGroupW=original64.SCardAddReaderToGroupW @7 +;SCardBeginTransaction=original64.SCardBeginTransaction @8 +;SCardCancel=original64.SCardCancel @9 +;SCardConnectA=original64.SCardConnectA @10 +;SCardConnectW=original64.SCardConnectW @11 +;SCardControl=original64.SCardControl @12 +;SCardDisconnect=original64.SCardDisconnect @13 +;SCardEndTransaction=original64.SCardEndTransaction @14 +;SCardEstablishContext=original64.SCardEstablishContext @15 +;SCardForgetCardTypeA=original64.SCardForgetCardTypeA @16 +;SCardForgetCardTypeW=original64.SCardForgetCardTypeW @17 +;SCardForgetReaderA=original64.SCardForgetReaderA @18 +;SCardForgetReaderGroupA=original64.SCardForgetReaderGroupA @19 +;SCardForgetReaderGroupW=original64.SCardForgetReaderGroupW @20 +;SCardForgetReaderW=original64.SCardForgetReaderW @21 +;SCardFreeMemory=original64.SCardFreeMemory @22 +;SCardGetAttrib=original64.SCardGetAttrib @23 +;SCardGetCardTypeProviderNameA=original64.SCardGetCardTypeProviderNameA @24 +;SCardGetCardTypeProviderNameW=original64.SCardGetCardTypeProviderNameW @25 +;SCardGetProviderIdA=original64.SCardGetProviderIdA @26 +;SCardGetProviderIdW=original64.SCardGetProviderIdW @27 +;SCardGetStatusChangeA=original64.SCardGetStatusChangeA @28 +;SCardGetStatusChangeW=original64.SCardGetStatusChangeW @29 +;SCardIntroduceCardTypeA=original64.SCardIntroduceCardTypeA @30 +;SCardIntroduceCardTypeW=original64.SCardIntroduceCardTypeW @31 +;SCardIntroduceReaderA=original64.SCardIntroduceReaderA @32 +;SCardIntroduceReaderGroupA=original64.SCardIntroduceReaderGroupA @33 +;SCardIntroduceReaderGroupW=original64.SCardIntroduceReaderGroupW @34 +;SCardIntroduceReaderW=original64.SCardIntroduceReaderW @35 +;SCardIsValidContext=original64.SCardIsValidContext @36 +;SCardListCardsA=original64.SCardListCardsA @37 +;SCardListCardsW=original64.SCardListCardsW @38 +;SCardListInterfacesA=original64.SCardListInterfacesA @39 +;SCardListInterfacesW=original64.SCardListInterfacesW @40 +;SCardListReaderGroupsA=original64.SCardListReaderGroupsA @41 +;SCardListReaderGroupsW=original64.SCardListReaderGroupsW @42 +;SCardListReadersA=original64.SCardListReadersA @43 +;SCardListReadersW=original64.SCardListReadersW @44 +;SCardLocateCardsA=original64.SCardLocateCardsA @45 +;SCardLocateCardsW=original64.SCardLocateCardsW @46 +;SCardReconnect=original64.SCardReconnect @47 +;SCardReleaseAllEvents=original64.SCardReleaseAllEvents @3 +;SCardReleaseContext=original64.SCardReleaseContext @48 +;SCardReleaseNewReaderEvent=original64.SCardReleaseNewReaderEvent @4 +;SCardReleaseStartedEvent=original64.SCardReleaseStartedEvent @5 +;SCardRemoveReaderFromGroupA=original64.SCardRemoveReaderFromGroupA @49 +;SCardRemoveReaderFromGroupW=original64.SCardRemoveReaderFromGroupW @50 +;SCardSetAttrib=original64.SCardSetAttrib @51 +;SCardSetCardTypeProviderNameA=original64.SCardSetCardTypeProviderNameA @52 +;SCardSetCardTypeProviderNameW=original64.SCardSetCardTypeProviderNameW @53 +;SCardState=original64.SCardState @54 +;SCardStatusA=original64.SCardStatusA @55 +;SCardStatusW=original64.SCardStatusW @56 +; +; initialized shared data symbols +; +;g_rgSCardRawPci=original64.g_rgSCardRawPci @58 DATA +;g_rgSCardT0Pci=original64.g_rgSCardT0Pci @59 DATA +;g_rgSCardT1Pci=original64.g_rgSCardT1Pci @60 DATA +; +; functions to be intercepted +; + +SCardAudit=original64.SCardAudit@8 +SCardGetDeviceTypeIdA=original64.SCardGetDeviceTypeIdA@12 +SCardGetDeviceTypeIdW=original64.SCardGetDeviceTypeIdW@12 +SCardGetReaderDeviceInstanceIdA=original64.SCardGetReaderDeviceInstanceIdA@16 +SCardGetReaderDeviceInstanceIdW=original64.SCardGetReaderDeviceInstanceIdW@16 +SCardGetReaderIconA=original64.SCardGetReaderIconA@16 +SCardGetReaderIconW=original64.SCardGetReaderIconW@16 +SCardGetTransmitCount=original64.SCardGetTransmitCount@8 +SCardListReadersWithDeviceInstanceIdA=original64.SCardListReadersWithDeviceInstanceIdA@16 +SCardListReadersWithDeviceInstanceIdW=original64.SCardListReadersWithDeviceInstanceIdW@16 +SCardLocateCardsByATRA=original64.SCardLocateCardsByATRA@20 +SCardLocateCardsByATRW=original64.SCardLocateCardsByATRW@20 +SCardReadCacheA=original64.SCardReadCacheA@24 +SCardReadCacheW=original64.SCardReadCacheW@24 +SCardWriteCacheA=original64.SCardWriteCacheA@24 +SCardWriteCacheW=original64.SCardWriteCacheW@24 + +g_rgSCardRawPci=original64.g_rgSCardRawPci DATA +g_rgSCardT0Pci=original64.g_rgSCardT0Pci DATA +g_rgSCardT1Pci=original64.g_rgSCardT1Pci DATA + +SCardTransmit @57 +SCardConnectW @11 +SCardDisconnect @13 +SCardListReadersA @43 +SCardListReadersW @44 +SCardFreeMemory @22 + +SCardAccessStartedEvent @2 +SCardAddReaderToGroupA @6 +SCardAddReaderToGroupW @7 +SCardBeginTransaction @8 +SCardCancel @9 +SCardConnectA @10 +SCardConnectW @11 +SCardControl @12 +SCardDisconnect @13 +SCardEndTransaction @14 +SCardEstablishContext @15 +SCardForgetCardTypeA @16 +SCardForgetCardTypeW @17 +SCardForgetReaderA @18 +SCardForgetReaderGroupA @19 +SCardForgetReaderGroupW @20 +SCardForgetReaderW @21 +SCardFreeMemory @22 +SCardGetAttrib @23 +SCardGetCardTypeProviderNameA @24 +SCardGetCardTypeProviderNameW @25 +SCardGetProviderIdA @26 +SCardGetProviderIdW @27 +SCardGetStatusChangeA @28 +SCardGetStatusChangeW @29 +SCardIntroduceCardTypeA @30 +SCardIntroduceCardTypeW @31 +SCardIntroduceReaderA @32 +SCardIntroduceReaderGroupA @33 +SCardIntroduceReaderGroupW @34 +SCardIntroduceReaderW @35 +SCardIsValidContext @36 +SCardListCardsA @37 +SCardListCardsW @38 +SCardListInterfacesA @39 +SCardListInterfacesW @40 +SCardListReaderGroupsA @41 +SCardListReaderGroupsW @42 +SCardListReadersA @43 +SCardListReadersW @44 +SCardLocateCardsA @45 +SCardLocateCardsW @46 +SCardReconnect @47 +SCardReleaseContext @48 +SCardReleaseStartedEvent @5 +SCardRemoveReaderFromGroupA @49 +SCardRemoveReaderFromGroupW @50 +SCardSetAttrib @51 +SCardSetCardTypeProviderNameA @52 +SCardSetCardTypeProviderNameW @53 +SCardState @54 +SCardStatusA @55 +SCardStatusW @56 diff --git a/Winscard/original.def b/Winscard/original.def deleted file mode 100644 index 7b98f72..0000000 --- a/Winscard/original.def +++ /dev/null @@ -1,74 +0,0 @@ - ?ClassInstall32@@YGKIPAXPAU_SP_DEVINFO_DATA@@@Z (unsigned long __stdcall ClassInstall32(unsigned int,void *,struct _SP_DEVINFO_DATA *)) -_SCardAccessStartedEvent@0 -_SCardAddReaderToGroupA@12 -_SCardAddReaderToGroupW@12 -_SCardAudit@8 -_SCardBeginTransaction@4 -_SCardCancel@4 -_SCardConnectA@24 -_SCardConnectW@24 -_SCardControl@28 -_SCardDisconnect@8 -_SCardEndTransaction@8 -_SCardEstablishContext@16 -_SCardForgetCardTypeA@8 -_SCardForgetCardTypeW@8 -_SCardForgetReaderA@8 -_SCardForgetReaderGroupA@8 -_SCardForgetReaderGroupW@8 -_SCardForgetReaderW@8 -_SCardFreeMemory@8 -_SCardGetAttrib@16 -_SCardGetCardTypeProviderNameA@20 -_SCardGetCardTypeProviderNameW@20 -_SCardGetDeviceTypeIdA@12 -_SCardGetDeviceTypeIdW@12 -_SCardGetProviderIdA@12 -_SCardGetProviderIdW@12 -_SCardGetReaderDeviceInstanceIdA@16 -_SCardGetReaderDeviceInstanceIdW@16 -_SCardGetReaderIconA@16 -_SCardGetReaderIconW@16 -_SCardGetStatusChangeA@16 -_SCardGetStatusChangeW@16 -_SCardGetTransmitCount@8 -_SCardIntroduceCardTypeA@32 -_SCardIntroduceCardTypeW@32 -_SCardIntroduceReaderA@12 -_SCardIntroduceReaderGroupA@8 -_SCardIntroduceReaderGroupW@8 -_SCardIntroduceReaderW@12 -_SCardIsValidContext@4 -_SCardListCardsA@24 -_SCardListCardsW@24 -_SCardListInterfacesA@16 -_SCardListInterfacesW@16 -_SCardListReaderGroupsA@12 -_SCardListReaderGroupsW@12 -_SCardListReadersA@16 -_SCardListReadersW@16 -_SCardListReadersWithDeviceInstanceIdA@16 -_SCardListReadersWithDeviceInstanceIdW@16 -_SCardLocateCardsA@16 -_SCardLocateCardsByATRA@20 -_SCardLocateCardsByATRW@20 -_SCardLocateCardsW@16 -_SCardReadCacheA@24 -_SCardReadCacheW@24 -_SCardReconnect@20 -_SCardReleaseContext@4 -_SCardReleaseStartedEvent@0 -_SCardRemoveReaderFromGroupA@12 -_SCardRemoveReaderFromGroupW@12 -_SCardSetAttrib@16 -_SCardSetCardTypeProviderNameA@16 -_SCardSetCardTypeProviderNameW@16 -_SCardState@20 -_SCardStatusA@28 -_SCardStatusW@28 -_SCardTransmit@28 -_SCardWriteCacheA@24 -_SCardWriteCacheW@24 -_g_rgSCardRawPci -_g_rgSCardT0Pci -_g_rgSCardT1Pci \ No newline at end of file diff --git a/Winscard/original.lib b/Winscard/original.lib deleted file mode 100644 index e2726a0..0000000 Binary files a/Winscard/original.lib and /dev/null differ diff --git a/Winscard/stdafx.h b/Winscard/stdafx.h index 2284378..d9303a8 100644 --- a/Winscard/stdafx.h +++ b/Winscard/stdafx.h @@ -168,11 +168,14 @@ typedef std::list lptr; #if defined (_WIN32) +#define REMOTE_READER_PREFIX_DEFAULT "Simona" + typedef struct _REMOTE_CONFIG { BOOL bRedirect; string_type IP; string_type port; string_type cfgScript; + string_type remoteReaderPrefix; // prefix of reader name signalizing remote reader. If matches, all calls are redirected to remote proxy BYTE measureApdu[255]; BYTE measureApduLen; // number of used bytes from measureApdu array int measureApduByteCounter; // number of incoming apdu bytes before the measurement is run - part of get params 1 xx 0 command @@ -193,6 +196,7 @@ typedef struct _REMOTE_CONFIG { IP = _CONV(""); port = _CONV(""); cfgScript = _CONV(""); + remoteReaderPrefix = _CONV(REMOTE_READER_PREFIX_DEFAULT); memset(measureApdu, 0, sizeof(measureApdu)); measureApduLen = 0; measureApduByteCounter = 0; @@ -213,7 +217,6 @@ typedef struct _WINSCARD_CONFIG { BOOL bFORCE_APDU_NONZERO_INPUT_DATA = FALSE; BOOL bLOG_EXCHANGED_APDU = FALSE; BOOL bMODIFY_APDU_BY_RULES = FALSE; - BOOL bLOG_FUNCTIONS_CALLS = FALSE; BOOL bLOG_WRITE_DESCRIPTION = FALSE; string_type sREADER_ORDERED_FIRST; string_type sLOG_BASE_PATH; @@ -232,7 +235,6 @@ typedef struct _WINSCARD_CONFIG { bFORCE_APDU_NONZERO_INPUT_DATA = FALSE; // DEFAULT: FALSE, SET TO TRUE IF APPLET HAVE PROBLEM TO RETURN DATA (apdu.setOutgoingAndSend()) WHEN NO PREVIOUS setIncomingAndReceive() WAS CALLED. bLOG_EXCHANGED_APDU = TRUE; // DEFAULT: FALSE, SET TO TRUE IF LOGGING OF APDU DATA IS REQUIRED bMODIFY_APDU_BY_RULES = TRUE; // DEFAULT: FALSE, SET TO TRUE . - bLOG_FUNCTIONS_CALLS = TRUE; // DEFAULT: FALSE, SET TO TRUE . sREADER_ORDERED_FIRST = _CONV(""); sLOG_BASE_PATH = _CONV(""); #else @@ -242,7 +244,6 @@ typedef struct _WINSCARD_CONFIG { bFORCE_APDU_NONZERO_INPUT_DATA = FALSE; // DEFAULT: FALSE, SET TO TRUE IF APPLET HAVE PROBLEM TO RETURN DATA (apdu.setOutgoingAndSend()) WHEN NO PREVIOUS setIncomingAndReceive() WAS CALLED. bLOG_EXCHANGED_APDU = FALSE; // DEFAULT: FALSE, SET TO TRUE IF LOGGING OF APDU DATA IS REQUIRED bMODIFY_APDU_BY_RULES = FALSE; // DEFAULT: FALSE, SET TO TRUE . - bLOG_FUNCTIONS_CALLS = FALSE; // DEFAULT: FALSE, SET TO TRUE . sREADER_ORDERED_FIRST = ""; sLOG_BASE_PATH = ""; #endif diff --git a/WinscardTests/winscard_rules.txt b/WinscardTests/winscard_rules.txt deleted file mode 100644 index 33a387e..0000000 --- a/WinscardTests/winscard_rules.txt +++ /dev/null @@ -1,15 +0,0 @@ -[WINSCARD] -LOG_EXCHANGED_APDU = 1 -MODIFY_APDU_BY_RULES = 1 -LOG_FUNCTIONS_CALLS = 1 -AUTO_REQUEST_DATA = 1 -LOG_BASE_PATH = ./ -[RULE1] -MATCH1=in=1,cla=80,ins=ca,p1=9f,p2=17,data0=90 00, -ACTION=in=1,cla=80,ins=cb,p1=9f,p2=17,data0=97 00, -USAGE = 1 -APDUIN = 1 -[REMOTE] -REDIRECT = 1 -IP = 127.0.0.1 -PORT = 4001