This commit is contained in:
Amit Shani 2024-08-09 23:38:05 +03:00
parent 56ec83ea4a
commit a2c7745891

View File

@ -2,6 +2,8 @@
#include <winpr/crt.h>
#include <winpr/smartcard.h>
#define CACHE_VALUE_SIZE 17
int TestSmartCardCache(int argc, char* argv[])
{
LONG lStatus = 0;
@ -9,14 +11,16 @@ int TestSmartCardCache(int argc, char* argv[])
SCARDCONTEXT hSC = 0;
LPSTR mszReaders = NULL;
DWORD cchReaders = SCARD_AUTOALLOCATE;
char * cacheValue = calloc(17, sizeof(char));
memcpy(cacheValue, "test-cache-value", 16);
SCARDHANDLE phCard = 0;
DWORD pdwActiveProtocol = 0;
char * cacheValue = calloc(CACHE_VALUE_SIZE, sizeof(char));
memcpy(cacheValue, "test-cache-value", CACHE_VALUE_SIZE-1);
UUID cardUUID = {.Data1 = 0x12345678, .Data2 = 0x9ABC, .Data3 = 0xDEF0, .Data4 = {0x12, 0x34, 0x56, 0x78, 0x9A, 0xBC}};
WINPR_UNUSED(argc);
WINPR_UNUSED(argv);
lStatus = SCardEstablishContext(SCARD_SCOPE_SYSTEM, NULL, NULL, &hSC);
if (lStatus != SCARD_S_SUCCESS)
{
printf("SCardEstablishContext failure: %s (0x%08" PRIX32 ")\n",
@ -34,8 +38,6 @@ int TestSmartCardCache(int argc, char* argv[])
}
pReader = mszReaders;
printf("connecting to reader: %s\n", pReader);
SCARDHANDLE phCard = 0;
DWORD pdwActiveProtocol = 0;
lStatus = SCardConnectA(hSC, pReader, SCARD_SHARE_SHARED, SCARD_PROTOCOL_Tx, &phCard, &pdwActiveProtocol);
if (lStatus != SCARD_S_SUCCESS)
{
@ -44,9 +46,8 @@ int TestSmartCardCache(int argc, char* argv[])
return 0;
}
printf("reader connected: %s\n", pReader);
UUID cardUuid = {.Data1 = 0x12345678, .Data2 = 0x9ABC, .Data3 = 0xDEF0, .Data4 = {0x12, 0x34, 0x56, 0x78, 0x9A, 0xBC}};
printf("writing cache...\n");
lStatus = SCardWriteCacheA(hSC, &cardUuid, 1, "test-cache-key", cacheValue, 17);
lStatus = SCardWriteCacheA(hSC, &cardUUID, 1, "test-cache-key", cacheValue, CACHE_VALUE_SIZE);
if (lStatus != SCARD_S_SUCCESS)
{
printf("SCardWriteCacheA failure: %s (0x%08" PRIX32 ")\n",
@ -54,24 +55,17 @@ int TestSmartCardCache(int argc, char* argv[])
return -1;
}
printf("write cache success\n");
LPSTR pszCacheValue = calloc(17, sizeof(char));
DWORD dwCacheValueLength = 17;
LPSTR pszCacheValue = calloc(CACHE_VALUE_SIZE, sizeof(char));
DWORD dwCacheValueLength = CACHE_VALUE_SIZE;
printf("reading cache...\n");
lStatus = SCardReadCacheA(hSC, &cardUuid, 1, "test-cache-key", pszCacheValue, &dwCacheValueLength);
lStatus = SCardReadCacheA(hSC, &cardUUID, 1, "test-cache-key", pszCacheValue, &dwCacheValueLength);
if (lStatus != SCARD_S_SUCCESS)
{
printf("SCardReadCacheA failure: %s (0x%08" PRIX32 ")\n",
SCardGetErrorString(lStatus), lStatus);
return -1;
}
printf("read cache success. read: %d bytes\n", dwCacheValueLength);
for (int i = 0; i < dwCacheValueLength; i++)
{
printf("%02X ", pszCacheValue[i]);
}
printf("read cache success. read: %d bytes. data: %s\n", dwCacheValueLength, pszCacheValue);
// compare pszCacheValue with "test-cache-value"
if (dwCacheValueLength != 17 || _stricmp(pszCacheValue, cacheValue) != 0)
if (dwCacheValueLength != CACHE_VALUE_SIZE || _stricmp(pszCacheValue, cacheValue) != 0)
{
printf("Cache Value Mismatch\n");
return -1;
@ -104,7 +98,7 @@ int TestSmartCardCache(int argc, char* argv[])
}
printf("connected to card\n");
printf("reading cache\n");
lStatus = SCardReadCacheA(hSC2, &cardUuid, 1, "test-cache-key", &pszCacheValue, &dwCacheValueLength);
lStatus = SCardReadCacheA(hSC2, &cardUUID, 1, "test-cache-key", &pszCacheValue, &dwCacheValueLength);
if (lStatus != SCARD_S_SUCCESS)
{
printf("SCardReadCacheA failure: %s (0x%08" PRIX32 ")\n",