21 #include <freerdp/config.h>
22 #include <freerdp/freerdp.h>
24 #include <winpr/crt.h>
25 #include <winpr/wlog.h>
26 #include <winpr/file.h>
27 #include <winpr/path.h>
28 #include <winpr/library.h>
29 #include <winpr/smartcard.h>
30 #include <winpr/collections.h>
31 #include <winpr/crypto.h>
33 #include <freerdp/emulate/scard/smartcard_emulate.h>
34 #include "FreeRDP.ico.h"
36 #include "smartcard_virtual_gids.h"
38 #define MAX_CACHE_ITEM_SIZE 4096
39 #define MAX_CACHE_ITEM_VALUES 4096
41 static CHAR g_ReaderNameA[] = {
'F',
'r',
'e',
'e',
'R',
'D',
'P',
' ',
'E',
42 'm',
'u',
'l',
'a',
't',
'o',
'r',
'\0',
'\0' };
43 static INIT_ONCE g_ReaderNameWGuard = INIT_ONCE_STATIC_INIT;
44 static WCHAR g_ReaderNameW[32] = { 0 };
45 static size_t g_ReaderNameWLen = 0;
47 static char* card_id_and_name_a(
const UUID* CardIdentifier, LPCSTR LookupName)
49 WINPR_ASSERT(CardIdentifier);
50 WINPR_ASSERT(LookupName);
52 size_t len = strlen(LookupName) + 34;
53 char*
id = malloc(len);
57 (void)snprintf(
id, len,
"%08X%04X%04X%02X%02X%02X%02X%02X%02X%02X%02X\\%s",
58 CardIdentifier->Data1, CardIdentifier->Data2, CardIdentifier->Data3,
59 CardIdentifier->Data4[0], CardIdentifier->Data4[1], CardIdentifier->Data4[2],
60 CardIdentifier->Data4[3], CardIdentifier->Data4[4], CardIdentifier->Data4[5],
61 CardIdentifier->Data4[6], CardIdentifier->Data4[7], LookupName);
65 static char* card_id_and_name_w(
const UUID* CardIdentifier, LPCWSTR LookupName)
68 char* tmp = ConvertWCharToUtf8Alloc(LookupName, NULL);
71 res = card_id_and_name_a(CardIdentifier, tmp);
76 static BOOL CALLBACK g_ReaderNameWInit(
PINIT_ONCE InitOnce, PVOID Parameter, PVOID* Context)
78 WINPR_UNUSED(InitOnce);
79 WINPR_UNUSED(Parameter);
80 WINPR_UNUSED(Context);
81 InitializeConstWCharFromUtf8(g_ReaderNameA, g_ReaderNameW, ARRAYSIZE(g_ReaderNameW));
82 g_ReaderNameWLen = _wcsnlen(g_ReaderNameW, ARRAYSIZE(g_ReaderNameW) - 2) + 2;
86 struct smartcard_emulation_context
88 const rdpSettings* settings;
89 DWORD log_default_level;
99 #define MAX_EMULATED_READERS 1
123 DWORD dwActiveProtocol;
124 SCARDCONTEXT hContext;
127 size_t referencecount;
134 char data[MAX_CACHE_ITEM_SIZE];
137 static SCardHandle* find_reader(SmartcardEmulationContext* smartcard,
const void* szReader,
140 static const BYTE ATR[] = { 0x3b, 0xf7, 0x18, 0x00, 0x00, 0x80, 0x31, 0xfe, 0x45,
141 0x73, 0x66, 0x74, 0x65, 0x2d, 0x6e, 0x66, 0xc4 };
143 static BOOL scard_status_transition(SCardContext* context)
145 WINPR_ASSERT(context);
147 switch (context->readerState)
153 reader->szReader = g_ReaderNameA;
154 reader->dwEventState = SCARD_STATE_PRESENT;
155 reader->cbAtr =
sizeof(ATR);
156 memcpy(reader->rgbAtr, ATR,
sizeof(ATR));
159 InitOnceExecuteOnce(&g_ReaderNameWGuard, g_ReaderNameWInit, NULL, NULL);
161 reader->szReader = g_ReaderNameW;
162 reader->dwEventState = SCARD_STATE_PRESENT;
163 reader->cbAtr =
sizeof(ATR);
164 memcpy(reader->rgbAtr, ATR,
sizeof(ATR));
166 context->readerState = 42;
173 static UINT32 scard_copy_strings(SCardContext* ctx,
void* dst,
size_t dstSize,
const void* src,
179 WINPR_ASSERT(srcSize <= UINT32_MAX);
180 WINPR_ASSERT(dstSize <= UINT32_MAX);
182 if (dstSize == SCARD_AUTOALLOCATE)
184 void* tmp = malloc(srcSize);
185 memcpy(tmp, src, srcSize);
186 ArrayList_Append(ctx->strings, tmp);
187 *((
void**)dst) = tmp;
188 return (UINT32)srcSize;
192 const size_t min = MIN(dstSize, srcSize);
193 memcpy(dst, src, min);
198 static void scard_context_free(
void* context)
200 SCardContext* ctx = context;
203 HashTable_Free(ctx->cards);
204 ArrayList_Free(ctx->strings);
205 HashTable_Free(ctx->cache);
210 static SCardContext* scard_context_new(
void)
212 SCardContext* ctx = calloc(1,
sizeof(SCardContext));
216 ctx->strings = ArrayList_New(FALSE);
221 wObject* obj = ArrayList_Object(ctx->strings);
223 obj->fnObjectFree = free;
226 ctx->cache = HashTable_New(FALSE);
229 if (!HashTable_SetupForStringData(ctx->cache, FALSE))
233 wObject* val = HashTable_ValueObject(ctx->cache);
235 val->fnObjectFree = free;
238 scard_status_transition(ctx);
241 scard_context_free(ctx);
245 static void scard_handle_free(
void* handle)
247 SCardHandle* hdl = handle;
250 free(hdl->szReader.pv);
251 vgids_free(hdl->vgids);
256 static SCardHandle* scard_handle_new(SmartcardEmulationContext* smartcard, SCARDCONTEXT context,
257 const void* name, BOOL unicode)
259 SCardHandle* hdl = NULL;
261 WINPR_ASSERT(smartcard);
263 hdl = calloc(1,
sizeof(SCardHandle));
272 size_t s = _wcslen(name);
274 hdl->szReader.pw = calloc(s + 2,
sizeof(WCHAR));
275 if (!hdl->szReader.pw)
277 memcpy(hdl->szReader.pv, name, s *
sizeof(WCHAR));
281 size_t s = strlen(name);
283 hdl->szReader.pc = calloc(s + 2,
sizeof(CHAR));
284 if (!hdl->szReader.pc)
286 memcpy(hdl->szReader.pv, name, s *
sizeof(CHAR));
289 if (!hdl->szReader.pv)
292 hdl->vgids = vgids_new();
304 if (!vgids_init(hdl->vgids, pem, key, pin))
308 hdl->unicode = unicode;
309 hdl->hContext = context;
313 scard_handle_free(hdl);
317 static LONG scard_handle_valid(SmartcardEmulationContext* smartcard, SCARDHANDLE handle)
319 SCardHandle* ctx = NULL;
321 WINPR_ASSERT(smartcard);
323 ctx = HashTable_GetItemValue(smartcard->handles, (
const void*)handle);
325 return SCARD_E_INVALID_HANDLE;
327 return SCARD_S_SUCCESS;
330 static LONG scard_reader_name_valid_a(SmartcardEmulationContext* smartcard, SCARDCONTEXT context,
333 SCardContext* ctx = NULL;
335 WINPR_ASSERT(smartcard);
336 ctx = HashTable_GetItemValue(smartcard->contexts, (
const void*)context);
341 for (
size_t x = 0; x < MAX_EMULATED_READERS; x++)
344 if (strcmp(reader->szReader, name) == 0)
345 return SCARD_S_SUCCESS;
348 return SCARD_E_UNKNOWN_READER;
351 static LONG scard_reader_name_valid_w(SmartcardEmulationContext* smartcard, SCARDCONTEXT context,
354 SCardContext* ctx = NULL;
356 WINPR_ASSERT(smartcard);
357 ctx = HashTable_GetItemValue(smartcard->contexts, (
const void*)context);
362 for (
size_t x = 0; x < MAX_EMULATED_READERS; x++)
365 if (_wcscmp(reader->szReader, name) == 0)
366 return SCARD_S_SUCCESS;
369 return SCARD_E_UNKNOWN_READER;
376 LONG WINAPI Emulate_SCardEstablishContext(SmartcardEmulationContext* smartcard, DWORD dwScope,
377 LPCVOID pvReserved1, LPCVOID pvReserved2,
378 LPSCARDCONTEXT phContext)
380 LONG status = SCARD_E_NO_MEMORY;
381 SCardContext* ctx = NULL;
383 WINPR_ASSERT(smartcard);
385 ctx = scard_context_new();
387 WINPR_UNUSED(pvReserved1);
388 WINPR_UNUSED(pvReserved2);
390 WLog_Print(smartcard->log, smartcard->log_default_level,
391 "SCardEstablishContext { dwScope: %s (0x%08" PRIX32
")",
392 SCardGetScopeString(dwScope), dwScope);
396 SCARDCONTEXT context = { 0 };
398 winpr_RAND(&context,
sizeof(SCARDCONTEXT));
399 if (HashTable_Insert(smartcard->contexts, (
const void*)context, ctx))
401 *phContext = context;
402 status = SCARD_S_SUCCESS;
406 WLog_Print(smartcard->log, smartcard->log_default_level,
407 "SCardEstablishContext } status: %s (0x%08" PRIX32
")", SCardGetErrorString(status),
410 if (status != SCARD_S_SUCCESS)
411 scard_context_free(ctx);
416 LONG WINAPI Emulate_SCardReleaseContext(SmartcardEmulationContext* smartcard, SCARDCONTEXT hContext)
419 SCardContext* value = NULL;
421 WINPR_ASSERT(smartcard);
423 value = HashTable_GetItemValue(smartcard->contexts, (
const void*)hContext);
425 WLog_Print(smartcard->log, smartcard->log_default_level,
"SCardReleaseContext { hContext: %p",
429 HashTable_Remove(smartcard->contexts, (
const void*)hContext);
431 status = SCARD_S_SUCCESS;
433 WLog_Print(smartcard->log, smartcard->log_default_level,
434 "SCardReleaseContext } status: %s (0x%08" PRIX32
")", SCardGetErrorString(status),
440 LONG WINAPI Emulate_SCardIsValidContext(SmartcardEmulationContext* smartcard, SCARDCONTEXT hContext)
444 WINPR_ASSERT(smartcard);
446 WLog_Print(smartcard->log, smartcard->log_default_level,
"SCardIsValidContext { hContext: %p",
449 status = HashTable_Contains(smartcard->contexts, (
const void*)hContext)
451 : SCARD_E_INVALID_HANDLE;
452 if (status == SCARD_S_SUCCESS)
454 SCardContext* value = HashTable_GetItemValue(smartcard->contexts, (
const void*)hContext);
457 return SCARD_F_INTERNAL_ERROR;
460 WLog_Print(smartcard->log, smartcard->log_default_level,
461 "SCardIsValidContext } status: %s (0x%08" PRIX32
")", SCardGetErrorString(status),
467 LONG WINAPI Emulate_SCardListReaderGroupsA(
468 SmartcardEmulationContext* smartcard, SCARDCONTEXT hContext,
469 LPSTR mszGroups , LPDWORD pcchGroups)
471 LONG status = Emulate_SCardIsValidContext(smartcard, hContext);
473 WLog_Print(smartcard->log, smartcard->log_default_level,
474 "SCardListReaderGroupsA { hContext: %p", (
void*)hContext);
476 WINPR_UNUSED(mszGroups);
477 WINPR_UNUSED(pcchGroups);
480 if (status == SCARD_S_SUCCESS)
481 status = SCARD_E_UNSUPPORTED_FEATURE;
483 WLog_Print(smartcard->log, smartcard->log_default_level,
484 "SCardListReaderGroupsA } status: %s (0x%08" PRIX32
")", SCardGetErrorString(status),
490 LONG WINAPI Emulate_SCardListReaderGroupsW(
491 SmartcardEmulationContext* smartcard, SCARDCONTEXT hContext,
492 LPWSTR mszGroups , LPDWORD pcchGroups)
494 LONG status = Emulate_SCardIsValidContext(smartcard, hContext);
496 WLog_Print(smartcard->log, smartcard->log_default_level,
497 "SCardListReaderGroupsW { hContext: %p", (
void*)hContext);
499 WINPR_UNUSED(mszGroups);
500 WINPR_UNUSED(pcchGroups);
503 if (status == SCARD_S_SUCCESS)
504 status = SCARD_E_UNSUPPORTED_FEATURE;
506 WLog_Print(smartcard->log, smartcard->log_default_level,
507 "SCardListReaderGroupsW } status: %s (0x%08" PRIX32
")", SCardGetErrorString(status),
513 LONG WINAPI Emulate_SCardListReadersA(SmartcardEmulationContext* smartcard, SCARDCONTEXT hContext,
514 LPCSTR mszGroups, LPSTR mszReaders, LPDWORD pcchReaders)
516 LONG status = Emulate_SCardIsValidContext(smartcard, hContext);
518 status = SCARD_E_INVALID_PARAMETER;
520 WLog_Print(smartcard->log, smartcard->log_default_level,
"SCardListReadersA { hContext: %p",
523 WINPR_UNUSED(mszGroups);
525 if (SCARD_S_SUCCESS == status)
527 SCardContext* value =
528 (SCardContext*)HashTable_GetItemValue(smartcard->contexts, (
const void*)hContext);
537 *pcchReaders = ARRAYSIZE(g_ReaderNameA);
540 *pcchReaders = scard_copy_strings(value, mszReaders, *pcchReaders, g_ReaderNameA,
541 sizeof(g_ReaderNameA));
545 WLog_Print(smartcard->log, smartcard->log_default_level,
546 "SCardListReadersA } status: %s (0x%08" PRIX32
")", SCardGetErrorString(status),
552 LONG WINAPI Emulate_SCardListReadersW(SmartcardEmulationContext* smartcard, SCARDCONTEXT hContext,
553 LPCWSTR mszGroups, LPWSTR mszReaders, LPDWORD pcchReaders)
555 LONG status = Emulate_SCardIsValidContext(smartcard, hContext);
558 status = SCARD_E_INVALID_PARAMETER;
560 WLog_Print(smartcard->log, smartcard->log_default_level,
"SCardListReadersW { hContext: %p",
563 WINPR_UNUSED(mszGroups);
565 InitOnceExecuteOnce(&g_ReaderNameWGuard, g_ReaderNameWInit, NULL, NULL);
566 if (SCARD_S_SUCCESS == status)
568 SCardContext* value = HashTable_GetItemValue(smartcard->contexts, (
const void*)hContext);
578 WINPR_ASSERT(g_ReaderNameWLen <= UINT32_MAX);
579 *pcchReaders = (UINT32)g_ReaderNameWLen;
583 *pcchReaders = scard_copy_strings(value, mszReaders, *pcchReaders, g_ReaderNameW,
584 g_ReaderNameWLen *
sizeof(WCHAR)) /
589 WLog_Print(smartcard->log, smartcard->log_default_level,
590 "SCardListReadersW } status: %s (0x%08" PRIX32
")", SCardGetErrorString(status),
596 LONG WINAPI Emulate_SCardListCardsA(SmartcardEmulationContext* smartcard, SCARDCONTEXT hContext,
597 LPCBYTE pbAtr, LPCGUID rgquidInterfaces,
598 DWORD cguidInterfaceCount,
602 LONG status = Emulate_SCardIsValidContext(smartcard, hContext);
604 WLog_Print(smartcard->log, smartcard->log_default_level,
"SCardListCardsA { hContext: %p",
608 WINPR_UNUSED(rgquidInterfaces);
609 WINPR_UNUSED(cguidInterfaceCount);
610 WINPR_UNUSED(mszCards);
611 WINPR_UNUSED(pcchCards);
614 if (status == SCARD_S_SUCCESS)
615 status = SCARD_E_UNSUPPORTED_FEATURE;
617 WLog_Print(smartcard->log, smartcard->log_default_level,
618 "SCardListCardsA } status: %s (0x%08" PRIX32
")", SCardGetErrorString(status),
624 LONG WINAPI Emulate_SCardListCardsW(SmartcardEmulationContext* smartcard, SCARDCONTEXT hContext,
625 LPCBYTE pbAtr, LPCGUID rgquidInterfaces,
626 DWORD cguidInterfaceCount,
630 LONG status = Emulate_SCardIsValidContext(smartcard, hContext);
632 WLog_Print(smartcard->log, smartcard->log_default_level,
"SCardListCardsW { hContext: %p",
636 WINPR_UNUSED(rgquidInterfaces);
637 WINPR_UNUSED(cguidInterfaceCount);
638 WINPR_UNUSED(mszCards);
639 WINPR_UNUSED(pcchCards);
642 if (status == SCARD_S_SUCCESS)
643 status = SCARD_E_UNSUPPORTED_FEATURE;
645 WLog_Print(smartcard->log, smartcard->log_default_level,
646 "SCardListCardsW } status: %s (0x%08" PRIX32
")", SCardGetErrorString(status),
652 LONG WINAPI Emulate_SCardListInterfacesA(
653 SmartcardEmulationContext* smartcard, SCARDCONTEXT hContext, LPCSTR szCard,
654 LPGUID pguidInterfaces, LPDWORD pcguidInterfaces )
656 LONG status = Emulate_SCardIsValidContext(smartcard, hContext);
658 WLog_Print(smartcard->log, smartcard->log_default_level,
"SCardListInterfacesA { hContext: %p",
661 WINPR_UNUSED(szCard);
662 WINPR_UNUSED(pguidInterfaces);
663 WINPR_UNUSED(pcguidInterfaces);
666 if (status == SCARD_S_SUCCESS)
667 status = SCARD_E_UNSUPPORTED_FEATURE;
669 WLog_Print(smartcard->log, smartcard->log_default_level,
670 "SCardListInterfacesA } status: %s (0x%08" PRIX32
")", SCardGetErrorString(status),
676 LONG WINAPI Emulate_SCardListInterfacesW(
677 SmartcardEmulationContext* smartcard, SCARDCONTEXT hContext, LPCWSTR szCard,
678 LPGUID pguidInterfaces, LPDWORD pcguidInterfaces )
680 LONG status = Emulate_SCardIsValidContext(smartcard, hContext);
682 WLog_Print(smartcard->log, smartcard->log_default_level,
"SCardListInterfacesW { hContext: %p",
685 WINPR_UNUSED(szCard);
686 WINPR_UNUSED(pguidInterfaces);
687 WINPR_UNUSED(pcguidInterfaces);
690 if (status == SCARD_S_SUCCESS)
691 status = SCARD_E_UNSUPPORTED_FEATURE;
693 WLog_Print(smartcard->log, smartcard->log_default_level,
694 "SCardListInterfacesW } status: %s (0x%08" PRIX32
")", SCardGetErrorString(status),
700 LONG WINAPI Emulate_SCardGetProviderIdA(SmartcardEmulationContext* smartcard, SCARDCONTEXT hContext,
701 LPCSTR szCard, LPGUID pguidProviderId)
703 LONG status = Emulate_SCardIsValidContext(smartcard, hContext);
705 WLog_Print(smartcard->log, smartcard->log_default_level,
"SCardGetProviderIdA { hContext: %p",
708 WINPR_UNUSED(szCard);
709 WINPR_UNUSED(pguidProviderId);
712 if (status == SCARD_S_SUCCESS)
713 status = SCARD_E_UNSUPPORTED_FEATURE;
715 WLog_Print(smartcard->log, smartcard->log_default_level,
716 "SCardGetProviderIdA } status: %s (0x%08" PRIX32
")", SCardGetErrorString(status),
722 LONG WINAPI Emulate_SCardGetProviderIdW(SmartcardEmulationContext* smartcard, SCARDCONTEXT hContext,
723 LPCWSTR szCard, LPGUID pguidProviderId)
725 LONG status = Emulate_SCardIsValidContext(smartcard, hContext);
727 WLog_Print(smartcard->log, smartcard->log_default_level,
"SCardGetProviderIdW { hContext: %p",
730 WINPR_UNUSED(szCard);
731 WINPR_UNUSED(pguidProviderId);
734 if (status == SCARD_S_SUCCESS)
735 status = SCARD_E_UNSUPPORTED_FEATURE;
737 WLog_Print(smartcard->log, smartcard->log_default_level,
738 "SCardGetProviderIdW } status: %s (0x%08" PRIX32
")", SCardGetErrorString(status),
744 LONG WINAPI Emulate_SCardGetCardTypeProviderNameA(
745 SmartcardEmulationContext* smartcard, SCARDCONTEXT hContext, LPCSTR szCardName,
746 DWORD dwProviderId, CHAR* szProvider ,
747 LPDWORD pcchProvider )
749 LONG status = Emulate_SCardIsValidContext(smartcard, hContext);
751 WLog_Print(smartcard->log, smartcard->log_default_level,
752 "SCardGetCardTypeProviderNameA { hContext: %p", (
void*)hContext);
754 WINPR_UNUSED(szCardName);
755 WINPR_UNUSED(dwProviderId);
756 WINPR_UNUSED(szProvider);
757 WINPR_UNUSED(pcchProvider);
760 if (status == SCARD_S_SUCCESS)
761 status = SCARD_E_UNSUPPORTED_FEATURE;
763 WLog_Print(smartcard->log, smartcard->log_default_level,
764 "SCardGetCardTypeProviderNameA } status: %s (0x%08" PRIX32
")",
765 SCardGetErrorString(status), status);
770 LONG WINAPI Emulate_SCardGetCardTypeProviderNameW(
771 SmartcardEmulationContext* smartcard, SCARDCONTEXT hContext, LPCWSTR szCardName,
772 DWORD dwProviderId, WCHAR* szProvider ,
773 LPDWORD pcchProvider )
775 LONG status = Emulate_SCardIsValidContext(smartcard, hContext);
777 WLog_Print(smartcard->log, smartcard->log_default_level,
778 "SCardGetCardTypeProviderNameW { hContext: %p", (
void*)hContext);
780 WINPR_UNUSED(szCardName);
781 WINPR_UNUSED(dwProviderId);
782 WINPR_UNUSED(szProvider);
783 WINPR_UNUSED(pcchProvider);
786 if (status == SCARD_S_SUCCESS)
787 status = SCARD_E_UNSUPPORTED_FEATURE;
789 WLog_Print(smartcard->log, smartcard->log_default_level,
790 "SCardGetCardTypeProviderNameW } status: %s (0x%08" PRIX32
")",
791 SCardGetErrorString(status), status);
796 LONG WINAPI Emulate_SCardIntroduceReaderGroupA(SmartcardEmulationContext* smartcard,
797 SCARDCONTEXT hContext, LPCSTR szGroupName)
799 LONG status = Emulate_SCardIsValidContext(smartcard, hContext);
801 WLog_Print(smartcard->log, smartcard->log_default_level,
802 "SCardIntroduceReaderGroupA { hContext: %p", (
void*)hContext);
804 WINPR_UNUSED(szGroupName);
807 if (status == SCARD_S_SUCCESS)
808 status = SCARD_E_UNSUPPORTED_FEATURE;
810 WLog_Print(smartcard->log, smartcard->log_default_level,
811 "SCardIntroduceReaderGroupA } status: %s (0x%08" PRIX32
")",
812 SCardGetErrorString(status), status);
817 LONG WINAPI Emulate_SCardIntroduceReaderGroupW(SmartcardEmulationContext* smartcard,
818 SCARDCONTEXT hContext, LPCWSTR szGroupName)
820 LONG status = Emulate_SCardIsValidContext(smartcard, hContext);
822 WLog_Print(smartcard->log, smartcard->log_default_level,
823 "SCardIntroduceReaderGroupW { hContext: %p", (
void*)hContext);
825 WINPR_UNUSED(szGroupName);
828 if (status == SCARD_S_SUCCESS)
829 status = SCARD_E_UNSUPPORTED_FEATURE;
831 WLog_Print(smartcard->log, smartcard->log_default_level,
832 "SCardIntroduceReaderGroupW } status: %s (0x%08" PRIX32
")",
833 SCardGetErrorString(status), status);
838 LONG WINAPI Emulate_SCardForgetReaderGroupA(SmartcardEmulationContext* smartcard,
839 SCARDCONTEXT hContext, LPCSTR szGroupName)
841 LONG status = Emulate_SCardIsValidContext(smartcard, hContext);
843 WLog_Print(smartcard->log, smartcard->log_default_level,
844 "SCardForgetReaderGroupA { hContext: %p", (
void*)hContext);
846 WINPR_UNUSED(szGroupName);
849 if (status == SCARD_S_SUCCESS)
850 status = SCARD_E_UNSUPPORTED_FEATURE;
852 WLog_Print(smartcard->log, smartcard->log_default_level,
853 "SCardForgetReaderGroupA } status: %s (0x%08" PRIX32
")",
854 SCardGetErrorString(status), status);
859 LONG WINAPI Emulate_SCardForgetReaderGroupW(SmartcardEmulationContext* smartcard,
860 SCARDCONTEXT hContext, LPCWSTR szGroupName)
862 LONG status = Emulate_SCardIsValidContext(smartcard, hContext);
864 WLog_Print(smartcard->log, smartcard->log_default_level,
865 "SCardForgetReaderGroupW { hContext: %p", (
void*)hContext);
867 WINPR_UNUSED(szGroupName);
870 if (status == SCARD_S_SUCCESS)
871 status = SCARD_E_UNSUPPORTED_FEATURE;
873 WLog_Print(smartcard->log, smartcard->log_default_level,
874 "SCardForgetReaderGroupW } status: %s (0x%08" PRIX32
")",
875 SCardGetErrorString(status), status);
880 LONG WINAPI Emulate_SCardIntroduceReaderA(SmartcardEmulationContext* smartcard,
881 SCARDCONTEXT hContext, LPCSTR szReaderName,
884 LONG status = Emulate_SCardIsValidContext(smartcard, hContext);
886 if (status == SCARD_S_SUCCESS)
887 status = scard_reader_name_valid_a(smartcard, hContext, szReaderName);
889 WLog_Print(smartcard->log, smartcard->log_default_level,
"SCardIntroduceReaderA { hContext: %p",
892 WINPR_UNUSED(szDeviceName);
895 if (status == SCARD_S_SUCCESS)
896 status = SCARD_E_UNSUPPORTED_FEATURE;
898 WLog_Print(smartcard->log, smartcard->log_default_level,
899 "SCardIntroduceReaderA } status: %s (0x%08" PRIX32
")", SCardGetErrorString(status),
905 LONG WINAPI Emulate_SCardIntroduceReaderW(SmartcardEmulationContext* smartcard,
906 SCARDCONTEXT hContext, LPCWSTR szReaderName,
907 LPCWSTR szDeviceName)
909 LONG status = Emulate_SCardIsValidContext(smartcard, hContext);
911 if (status == SCARD_S_SUCCESS)
912 status = scard_reader_name_valid_w(smartcard, hContext, szReaderName);
914 WLog_Print(smartcard->log, smartcard->log_default_level,
"SCardIntroduceReaderW { hContext: %p",
917 WINPR_UNUSED(szDeviceName);
920 if (status == SCARD_S_SUCCESS)
921 status = SCARD_E_UNSUPPORTED_FEATURE;
923 WLog_Print(smartcard->log, smartcard->log_default_level,
924 "SCardIntroduceReaderW } status: %s (0x%08" PRIX32
")", SCardGetErrorString(status),
930 LONG WINAPI Emulate_SCardForgetReaderA(SmartcardEmulationContext* smartcard, SCARDCONTEXT hContext,
933 LONG status = Emulate_SCardIsValidContext(smartcard, hContext);
935 if (status == SCARD_S_SUCCESS)
936 status = scard_reader_name_valid_a(smartcard, hContext, szReaderName);
938 WLog_Print(smartcard->log, smartcard->log_default_level,
"SCardForgetReaderA { hContext: %p",
942 if (status == SCARD_S_SUCCESS)
943 status = SCARD_E_UNSUPPORTED_FEATURE;
945 WLog_Print(smartcard->log, smartcard->log_default_level,
946 "SCardForgetReaderA } status: %s (0x%08" PRIX32
")", SCardGetErrorString(status),
952 LONG WINAPI Emulate_SCardForgetReaderW(SmartcardEmulationContext* smartcard, SCARDCONTEXT hContext,
953 LPCWSTR szReaderName)
955 LONG status = Emulate_SCardIsValidContext(smartcard, hContext);
957 if (status == SCARD_S_SUCCESS)
958 status = scard_reader_name_valid_w(smartcard, hContext, szReaderName);
960 WLog_Print(smartcard->log, smartcard->log_default_level,
"SCardForgetReaderW { hContext: %p",
964 if (status == SCARD_S_SUCCESS)
965 status = SCARD_E_UNSUPPORTED_FEATURE;
967 WLog_Print(smartcard->log, smartcard->log_default_level,
968 "SCardForgetReaderW } status: %s (0x%08" PRIX32
")", SCardGetErrorString(status),
974 LONG WINAPI Emulate_SCardAddReaderToGroupA(SmartcardEmulationContext* smartcard,
975 SCARDCONTEXT hContext, LPCSTR szReaderName,
978 LONG status = Emulate_SCardIsValidContext(smartcard, hContext);
980 if (status == SCARD_S_SUCCESS)
981 status = scard_reader_name_valid_a(smartcard, hContext, szReaderName);
983 WLog_Print(smartcard->log, smartcard->log_default_level,
984 "SCardAddReaderToGroupA { hContext: %p", (
void*)hContext);
986 WINPR_UNUSED(szGroupName);
989 if (status == SCARD_S_SUCCESS)
990 status = SCARD_E_UNSUPPORTED_FEATURE;
992 WLog_Print(smartcard->log, smartcard->log_default_level,
993 "SCardAddReaderToGroupA } status: %s (0x%08" PRIX32
")", SCardGetErrorString(status),
999 LONG WINAPI Emulate_SCardAddReaderToGroupW(SmartcardEmulationContext* smartcard,
1000 SCARDCONTEXT hContext, LPCWSTR szReaderName,
1001 LPCWSTR szGroupName)
1003 LONG status = Emulate_SCardIsValidContext(smartcard, hContext);
1005 if (status == SCARD_S_SUCCESS)
1006 status = scard_reader_name_valid_w(smartcard, hContext, szReaderName);
1008 WLog_Print(smartcard->log, smartcard->log_default_level,
1009 "SCardAddReaderToGroupW { hContext: %p", (
void*)hContext);
1011 WINPR_UNUSED(szGroupName);
1014 if (status == SCARD_S_SUCCESS)
1015 status = SCARD_E_UNSUPPORTED_FEATURE;
1017 WLog_Print(smartcard->log, smartcard->log_default_level,
1018 "SCardAddReaderToGroupW } status: %s (0x%08" PRIX32
")", SCardGetErrorString(status),
1024 LONG WINAPI Emulate_SCardRemoveReaderFromGroupA(SmartcardEmulationContext* smartcard,
1025 SCARDCONTEXT hContext, LPCSTR szReaderName,
1028 LONG status = Emulate_SCardIsValidContext(smartcard, hContext);
1030 if (status == SCARD_S_SUCCESS)
1031 status = scard_reader_name_valid_a(smartcard, hContext, szReaderName);
1033 WLog_Print(smartcard->log, smartcard->log_default_level,
1034 "SCardRemoveReaderFromGroupA { hContext: %p", (
void*)hContext);
1036 WINPR_UNUSED(szGroupName);
1039 if (status == SCARD_S_SUCCESS)
1040 status = SCARD_E_UNSUPPORTED_FEATURE;
1042 WLog_Print(smartcard->log, smartcard->log_default_level,
1043 "SCardRemoveReaderFromGroupA } status: %s (0x%08" PRIX32
")",
1044 SCardGetErrorString(status), status);
1049 LONG WINAPI Emulate_SCardRemoveReaderFromGroupW(SmartcardEmulationContext* smartcard,
1050 SCARDCONTEXT hContext, LPCWSTR szReaderName,
1051 LPCWSTR szGroupName)
1053 LONG status = Emulate_SCardIsValidContext(smartcard, hContext);
1055 if (status == SCARD_S_SUCCESS)
1056 status = scard_reader_name_valid_w(smartcard, hContext, szReaderName);
1058 WLog_Print(smartcard->log, smartcard->log_default_level,
1059 "SCardRemoveReaderFromGroupW { hContext: %p", (
void*)hContext);
1061 WINPR_UNUSED(szGroupName);
1064 if (status == SCARD_S_SUCCESS)
1065 status = SCARD_E_UNSUPPORTED_FEATURE;
1067 WLog_Print(smartcard->log, smartcard->log_default_level,
1068 "SCardRemoveReaderFromGroupW } status: %s (0x%08" PRIX32
")",
1069 SCardGetErrorString(status), status);
1074 LONG WINAPI Emulate_SCardIntroduceCardTypeA(SmartcardEmulationContext* smartcard,
1075 SCARDCONTEXT hContext, LPCSTR szCardName,
1076 LPCGUID pguidPrimaryProvider, LPCGUID rgguidInterfaces,
1077 DWORD dwInterfaceCount, LPCBYTE pbAtr,
1078 LPCBYTE pbAtrMask, DWORD cbAtrLen)
1080 LONG status = Emulate_SCardIsValidContext(smartcard, hContext);
1082 WLog_Print(smartcard->log, smartcard->log_default_level,
1083 "SCardIntroduceCardTypeA { hContext: %p", (
void*)hContext);
1085 WINPR_UNUSED(szCardName);
1086 WINPR_UNUSED(pguidPrimaryProvider);
1087 WINPR_UNUSED(rgguidInterfaces);
1088 WINPR_UNUSED(dwInterfaceCount);
1089 WINPR_UNUSED(pbAtr);
1090 WINPR_UNUSED(pbAtrMask);
1091 WINPR_UNUSED(cbAtrLen);
1094 if (status == SCARD_S_SUCCESS)
1095 status = SCARD_E_UNSUPPORTED_FEATURE;
1097 WLog_Print(smartcard->log, smartcard->log_default_level,
1098 "SCardIntroduceCardTypeA } status: %s (0x%08" PRIX32
")",
1099 SCardGetErrorString(status), status);
1104 LONG WINAPI Emulate_SCardIntroduceCardTypeW(SmartcardEmulationContext* smartcard,
1105 SCARDCONTEXT hContext, LPCWSTR szCardName,
1106 LPCGUID pguidPrimaryProvider, LPCGUID rgguidInterfaces,
1107 DWORD dwInterfaceCount, LPCBYTE pbAtr,
1108 LPCBYTE pbAtrMask, DWORD cbAtrLen)
1110 LONG status = Emulate_SCardIsValidContext(smartcard, hContext);
1112 WLog_Print(smartcard->log, smartcard->log_default_level,
1113 "SCardIntroduceCardTypeW { hContext: %p", (
void*)hContext);
1115 WINPR_UNUSED(szCardName);
1116 WINPR_UNUSED(pguidPrimaryProvider);
1117 WINPR_UNUSED(rgguidInterfaces);
1118 WINPR_UNUSED(dwInterfaceCount);
1119 WINPR_UNUSED(pbAtr);
1120 WINPR_UNUSED(pbAtrMask);
1121 WINPR_UNUSED(cbAtrLen);
1124 if (status == SCARD_S_SUCCESS)
1125 status = SCARD_E_UNSUPPORTED_FEATURE;
1127 WLog_Print(smartcard->log, smartcard->log_default_level,
1128 "SCardIntroduceCardTypeW } status: %s (0x%08" PRIX32
")",
1129 SCardGetErrorString(status), status);
1134 LONG WINAPI Emulate_SCardSetCardTypeProviderNameA(SmartcardEmulationContext* smartcard,
1135 SCARDCONTEXT hContext, LPCSTR szCardName,
1136 DWORD dwProviderId, LPCSTR szProvider)
1138 LONG status = Emulate_SCardIsValidContext(smartcard, hContext);
1140 WLog_Print(smartcard->log, smartcard->log_default_level,
1141 "SCardSetCardTypeProviderNameA { hContext: %p", (
void*)hContext);
1143 WINPR_UNUSED(szCardName);
1144 WINPR_UNUSED(dwProviderId);
1145 WINPR_UNUSED(szProvider);
1148 if (status == SCARD_S_SUCCESS)
1149 status = SCARD_E_UNSUPPORTED_FEATURE;
1151 WLog_Print(smartcard->log, smartcard->log_default_level,
1152 "SCardSetCardTypeProviderNameA } status: %s (0x%08" PRIX32
")",
1153 SCardGetErrorString(status), status);
1158 LONG WINAPI Emulate_SCardSetCardTypeProviderNameW(SmartcardEmulationContext* smartcard,
1159 SCARDCONTEXT hContext, LPCWSTR szCardName,
1160 DWORD dwProviderId, LPCWSTR szProvider)
1162 LONG status = Emulate_SCardIsValidContext(smartcard, hContext);
1164 WLog_Print(smartcard->log, smartcard->log_default_level,
1165 "SCardSetCardTypeProviderNameA { hContext: %p", (
void*)hContext);
1167 WINPR_UNUSED(szCardName);
1168 WINPR_UNUSED(dwProviderId);
1169 WINPR_UNUSED(szProvider);
1172 if (status == SCARD_S_SUCCESS)
1173 status = SCARD_E_UNSUPPORTED_FEATURE;
1175 WLog_Print(smartcard->log, smartcard->log_default_level,
1176 "SCardSetCardTypeProviderNameW } status: %s (0x%08" PRIX32
")",
1177 SCardGetErrorString(status), status);
1182 LONG WINAPI Emulate_SCardForgetCardTypeA(SmartcardEmulationContext* smartcard,
1183 SCARDCONTEXT hContext, LPCSTR szCardName)
1185 LONG status = Emulate_SCardIsValidContext(smartcard, hContext);
1187 WLog_Print(smartcard->log, smartcard->log_default_level,
"SCardForgetCardTypeA { hContext: %p",
1190 WINPR_UNUSED(szCardName);
1193 if (status == SCARD_S_SUCCESS)
1194 status = SCARD_E_UNSUPPORTED_FEATURE;
1196 WLog_Print(smartcard->log, smartcard->log_default_level,
1197 "SCardForgetCardTypeA } status: %s (0x%08" PRIX32
")", SCardGetErrorString(status),
1203 LONG WINAPI Emulate_SCardForgetCardTypeW(SmartcardEmulationContext* smartcard,
1204 SCARDCONTEXT hContext, LPCWSTR szCardName)
1206 LONG status = Emulate_SCardIsValidContext(smartcard, hContext);
1208 WLog_Print(smartcard->log, smartcard->log_default_level,
"SCardForgetCardTypeW { hContext: %p",
1211 WINPR_UNUSED(szCardName);
1214 if (status == SCARD_S_SUCCESS)
1215 status = SCARD_E_UNSUPPORTED_FEATURE;
1217 WLog_Print(smartcard->log, smartcard->log_default_level,
1218 "SCardForgetCardTypeW } status: %s (0x%08" PRIX32
")", SCardGetErrorString(status),
1224 LONG WINAPI Emulate_SCardFreeMemory(SmartcardEmulationContext* smartcard, SCARDCONTEXT hContext,
1227 LONG status = Emulate_SCardIsValidContext(smartcard, hContext);
1229 WLog_Print(smartcard->log, smartcard->log_default_level,
"SCardFreeMemory { hContext: %p",
1232 if (status == SCARD_S_SUCCESS)
1234 SCardContext* value = HashTable_GetItemValue(smartcard->contexts, (
const void*)hContext);
1235 WINPR_ASSERT(value);
1237 ArrayList_Remove(value->strings, pvMem);
1240 WLog_Print(smartcard->log, smartcard->log_default_level,
1241 "SCardFreeMemory } status: %s (0x%08" PRIX32
")", SCardGetErrorString(status),
1247 HANDLE WINAPI Emulate_SCardAccessStartedEvent(SmartcardEmulationContext* smartcard)
1249 HANDLE hEvent = NULL;
1251 WINPR_ASSERT(smartcard);
1253 WLog_Print(smartcard->log, smartcard->log_default_level,
"SCardAccessStartedEvent {");
1256 winpr_RAND(&hEvent,
sizeof(hEvent));
1258 WLog_Print(smartcard->log, smartcard->log_default_level,
"SCardAccessStartedEvent } hEvent: %p",
1264 void WINAPI Emulate_SCardReleaseStartedEvent(SmartcardEmulationContext* smartcard)
1266 WINPR_ASSERT(smartcard);
1268 WLog_Print(smartcard->log, smartcard->log_default_level,
"SCardReleaseStartedEvent {");
1272 WLog_Print(smartcard->log, smartcard->log_default_level,
"SCardReleaseStartedEvent }");
1275 LONG WINAPI Emulate_SCardLocateCardsA(SmartcardEmulationContext* smartcard, SCARDCONTEXT hContext,
1279 LONG status = Emulate_SCardIsValidContext(smartcard, hContext);
1281 WLog_Print(smartcard->log, smartcard->log_default_level,
"SCardLocateCardsA { hContext: %p",
1284 WINPR_UNUSED(mszCards);
1285 WINPR_UNUSED(rgReaderStates);
1286 WINPR_UNUSED(cReaders);
1289 if (status == SCARD_S_SUCCESS)
1290 status = SCARD_E_UNSUPPORTED_FEATURE;
1292 WLog_Print(smartcard->log, smartcard->log_default_level,
1293 "SCardLocateCardsA } status: %s (0x%08" PRIX32
")", SCardGetErrorString(status),
1299 LONG WINAPI Emulate_SCardLocateCardsW(SmartcardEmulationContext* smartcard, SCARDCONTEXT hContext,
1303 LONG status = Emulate_SCardIsValidContext(smartcard, hContext);
1305 WLog_Print(smartcard->log, smartcard->log_default_level,
"SCardLocateCardsW { hContext: %p",
1308 WINPR_UNUSED(mszCards);
1309 WINPR_UNUSED(rgReaderStates);
1310 WINPR_UNUSED(cReaders);
1313 if (status == SCARD_S_SUCCESS)
1314 status = SCARD_E_UNSUPPORTED_FEATURE;
1316 WLog_Print(smartcard->log, smartcard->log_default_level,
1317 "SCardLocateCardsW } status: %s (0x%08" PRIX32
")", SCardGetErrorString(status),
1323 LONG WINAPI Emulate_SCardLocateCardsByATRA(SmartcardEmulationContext* smartcard,
1328 LONG status = Emulate_SCardIsValidContext(smartcard, hContext);
1330 WLog_Print(smartcard->log, smartcard->log_default_level,
1331 "SCardLocateCardsByATRA { hContext: %p", (
void*)hContext);
1333 WINPR_UNUSED(rgAtrMasks);
1334 WINPR_UNUSED(cAtrs);
1335 WINPR_UNUSED(rgReaderStates);
1336 WINPR_UNUSED(cReaders);
1339 if (status == SCARD_S_SUCCESS)
1340 status = SCARD_E_UNSUPPORTED_FEATURE;
1342 WLog_Print(smartcard->log, smartcard->log_default_level,
1343 "SCardLocateCardsByATRA } status: %s (0x%08" PRIX32
")", SCardGetErrorString(status),
1349 LONG WINAPI Emulate_SCardLocateCardsByATRW(SmartcardEmulationContext* smartcard,
1354 LONG status = Emulate_SCardIsValidContext(smartcard, hContext);
1356 WLog_Print(smartcard->log, smartcard->log_default_level,
1357 "SCardLocateCardsByATRW { hContext: %p", (
void*)hContext);
1359 WINPR_UNUSED(rgAtrMasks);
1360 WINPR_UNUSED(cAtrs);
1361 WINPR_UNUSED(rgReaderStates);
1362 WINPR_UNUSED(cReaders);
1365 if (status == SCARD_S_SUCCESS)
1366 status = SCARD_E_UNSUPPORTED_FEATURE;
1368 WLog_Print(smartcard->log, smartcard->log_default_level,
1369 "SCardLocateCardsByATRW } status: %s (0x%08" PRIX32
")", SCardGetErrorString(status),
1375 LONG WINAPI Emulate_SCardGetStatusChangeA(SmartcardEmulationContext* smartcard,
1376 SCARDCONTEXT hContext, DWORD dwTimeout,
1379 LONG status = Emulate_SCardIsValidContext(smartcard, hContext);
1381 WLog_Print(smartcard->log, smartcard->log_default_level,
"SCardGetStatusChangeA { hContext: %p",
1384 if (status == SCARD_S_SUCCESS)
1386 const DWORD diff = 100;
1387 size_t eventCount = 0;
1388 SCardContext* value = HashTable_GetItemValue(smartcard->contexts, (
const void*)hContext);
1389 WINPR_ASSERT(value);
1394 status = SCARD_E_TIMEOUT;
1397 for (
size_t x = 0; x < cReaders; x++)
1401 for (
size_t y = 0; y < MAX_EMULATED_READERS; y++)
1404 if (strcmp(out->szReader, in->szReader) == 0)
1406 const SCardHandle* hdl = find_reader(smartcard, in->szReader, FALSE);
1407 out->dwEventState = in->dwEventState;
1410 out->dwEventState |= SCARD_STATE_INUSE;
1411 if (hdl->dwShareMode == SCARD_SHARE_EXCLUSIVE)
1412 out->dwEventState |= SCARD_STATE_EXCLUSIVE;
1415 if ((out->dwEventState & SCARD_STATE_EMPTY) !=
1416 (out->dwCurrentState & SCARD_STATE_EMPTY))
1417 out->dwEventState |= SCARD_STATE_CHANGED;
1418 if ((out->dwEventState & SCARD_STATE_PRESENT) !=
1419 (out->dwCurrentState & SCARD_STATE_PRESENT))
1420 out->dwEventState |= SCARD_STATE_CHANGED;
1422 out->cbAtr = in->cbAtr;
1423 memcpy(out->rgbAtr, in->rgbAtr, out->cbAtr);
1424 if (out->dwEventState & SCARD_STATE_CHANGED)
1429 if (value->canceled)
1431 status = SCARD_E_CANCELLED;
1434 if (eventCount != 0)
1436 status = SCARD_S_SUCCESS;
1440 if (dwTimeout != INFINITE)
1441 dwTimeout -= MIN(dwTimeout, diff);
1442 if (freerdp_shall_disconnect_context(inst->context))
1444 status = SCARD_E_CANCELLED;
1447 }
while (dwTimeout > 0);
1450 WLog_Print(smartcard->log, smartcard->log_default_level,
1451 "SCardGetStatusChangeA } status: %s (0x%08" PRIX32
")", SCardGetErrorString(status),
1457 LONG WINAPI Emulate_SCardGetStatusChangeW(SmartcardEmulationContext* smartcard,
1458 SCARDCONTEXT hContext, DWORD dwTimeout,
1461 LONG status = Emulate_SCardIsValidContext(smartcard, hContext);
1463 WLog_Print(smartcard->log, smartcard->log_default_level,
"SCardGetStatusChangeW { hContext: %p",
1466 if (status == SCARD_S_SUCCESS)
1468 const DWORD diff = 100;
1469 size_t eventCount = 0;
1470 SCardContext* value = HashTable_GetItemValue(smartcard->contexts, (
const void*)hContext);
1471 WINPR_ASSERT(value);
1476 status = SCARD_E_TIMEOUT;
1479 for (
size_t x = 0; x < cReaders; x++)
1483 for (
size_t y = 0; y < MAX_EMULATED_READERS; y++)
1486 if (_wcscmp(out->szReader, in->szReader) == 0)
1488 const SCardHandle* hdl = find_reader(smartcard, in->szReader, TRUE);
1489 out->dwEventState = in->dwEventState;
1492 out->dwEventState |= SCARD_STATE_INUSE;
1493 if (hdl->dwShareMode == SCARD_SHARE_EXCLUSIVE)
1494 out->dwEventState |= SCARD_STATE_EXCLUSIVE;
1496 if ((out->dwEventState & SCARD_STATE_EMPTY) !=
1497 (out->dwCurrentState & SCARD_STATE_EMPTY))
1498 out->dwEventState |= SCARD_STATE_CHANGED;
1499 if ((out->dwEventState & SCARD_STATE_PRESENT) !=
1500 (out->dwCurrentState & SCARD_STATE_PRESENT))
1501 out->dwEventState |= SCARD_STATE_CHANGED;
1502 out->cbAtr = in->cbAtr;
1503 memcpy(out->rgbAtr, in->rgbAtr, out->cbAtr);
1505 if (out->dwEventState & SCARD_STATE_CHANGED)
1510 if (value->canceled)
1512 status = SCARD_E_CANCELLED;
1515 if (eventCount != 0)
1517 status = SCARD_S_SUCCESS;
1521 if (dwTimeout != INFINITE)
1522 dwTimeout -= MIN(dwTimeout, diff);
1523 if (freerdp_shall_disconnect_context(inst->context))
1525 status = SCARD_E_CANCELLED;
1528 }
while (dwTimeout > 0);
1531 WLog_Print(smartcard->log, smartcard->log_default_level,
1532 "SCardGetStatusChangeW } status: %s (0x%08" PRIX32
")", SCardGetErrorString(status),
1538 LONG WINAPI Emulate_SCardCancel(SmartcardEmulationContext* smartcard, SCARDCONTEXT hContext)
1540 LONG status = Emulate_SCardIsValidContext(smartcard, hContext);
1542 WLog_Print(smartcard->log, smartcard->log_default_level,
"SCardCancel { hContext: %p",
1545 if (status == SCARD_S_SUCCESS)
1547 SCardContext* value = HashTable_GetItemValue(smartcard->contexts, (
const void*)hContext);
1548 WINPR_ASSERT(value);
1549 value->canceled = TRUE;
1552 WLog_Print(smartcard->log, smartcard->log_default_level,
1553 "SCardCancel } status: %s (0x%08" PRIX32
")", SCardGetErrorString(status), status);
1558 SCardHandle* find_reader(SmartcardEmulationContext* smartcard,
const void* szReader, BOOL unicode)
1560 SCardHandle* hdl = NULL;
1561 UINT_PTR* keys = NULL;
1564 WINPR_ASSERT(smartcard);
1565 count = HashTable_GetKeys(smartcard->handles, &keys);
1566 for (
size_t x = 0; x < count; x++)
1568 SCardHandle* cur = HashTable_GetItemValue(smartcard->handles, (
const void*)keys[x]);
1571 if (cur->unicode != unicode)
1573 if (!unicode && (strcmp(cur->szReader.pc, szReader) != 0))
1575 if (unicode && (_wcscmp(cur->szReader.pw, szReader) != 0))
1584 static SCardHandle* reader2handle(SmartcardEmulationContext* smartcard, SCARDCONTEXT hContext,
1585 const void* szReader, BOOL unicode, DWORD dwShareMode,
1586 SCARDHANDLE* phCard, DWORD dwPreferredProtocols,
1587 LPDWORD pdwActiveProtocol)
1589 SCardHandle* hdl = NULL;
1591 WINPR_ASSERT(phCard);
1594 if (Emulate_SCardIsValidContext(smartcard, hContext) != SCARD_S_SUCCESS)
1597 hdl = scard_handle_new(smartcard, hContext, szReader, unicode);
1600 winpr_RAND(&hdl->card,
sizeof(hdl->card));
1601 hdl->dwActiveProtocol = SCARD_PROTOCOL_T1;
1602 hdl->dwShareMode = dwShareMode;
1604 if (!HashTable_Insert(smartcard->handles, (
const void*)hdl->card, hdl))
1606 scard_handle_free(hdl);
1611 if (pdwActiveProtocol)
1613 if ((hdl->dwActiveProtocol & dwPreferredProtocols) == 0)
1615 scard_handle_free(hdl);
1619 *pdwActiveProtocol = hdl->dwActiveProtocol;
1623 hdl->referencecount++;
1624 *phCard = hdl->card;
1628 WLog_Print(smartcard->log, smartcard->log_default_level,
"{ %p }", (
void*)*phCard);
1632 LONG WINAPI Emulate_SCardConnectA(SmartcardEmulationContext* smartcard, SCARDCONTEXT hContext,
1633 LPCSTR szReader, DWORD dwShareMode, DWORD dwPreferredProtocols,
1634 LPSCARDHANDLE phCard, LPDWORD pdwActiveProtocol)
1636 LONG status = Emulate_SCardIsValidContext(smartcard, hContext);
1638 if (!phCard || !pdwActiveProtocol)
1639 status = SCARD_E_INVALID_PARAMETER;
1641 WLog_Print(smartcard->log, smartcard->log_default_level,
"SCardConnectA { hContext: %p",
1644 if (status == SCARD_S_SUCCESS)
1646 if (!reader2handle(smartcard, hContext, szReader, FALSE, dwShareMode, phCard,
1647 dwPreferredProtocols, pdwActiveProtocol))
1648 status = SCARD_E_NO_MEMORY;
1651 WLog_Print(smartcard->log, smartcard->log_default_level,
1652 "SCardConnectA } status: %s (0x%08" PRIX32
")", SCardGetErrorString(status), status);
1657 LONG WINAPI Emulate_SCardConnectW(SmartcardEmulationContext* smartcard, SCARDCONTEXT hContext,
1658 LPCWSTR szReader, DWORD dwShareMode, DWORD dwPreferredProtocols,
1659 LPSCARDHANDLE phCard, LPDWORD pdwActiveProtocol)
1661 LONG status = Emulate_SCardIsValidContext(smartcard, hContext);
1663 if (!phCard || !pdwActiveProtocol)
1664 status = SCARD_E_INVALID_PARAMETER;
1666 WLog_Print(smartcard->log, smartcard->log_default_level,
"SCardConnectW { hContext: %p",
1669 if (status == SCARD_S_SUCCESS)
1671 if (!reader2handle(smartcard, hContext, szReader, TRUE, dwShareMode, phCard,
1672 dwPreferredProtocols, pdwActiveProtocol))
1673 status = SCARD_E_NO_MEMORY;
1676 WLog_Print(smartcard->log, smartcard->log_default_level,
1677 "SCardConnectW } status: %s (0x%08" PRIX32
")", SCardGetErrorString(status), status);
1682 LONG WINAPI Emulate_SCardReconnect(SmartcardEmulationContext* smartcard, SCARDHANDLE hCard,
1683 DWORD dwShareMode, DWORD dwPreferredProtocols,
1684 DWORD dwInitialization, LPDWORD pdwActiveProtocol)
1686 LONG status = scard_handle_valid(smartcard, hCard);
1688 if (!pdwActiveProtocol)
1689 status = SCARD_E_INVALID_PARAMETER;
1691 WLog_Print(smartcard->log, smartcard->log_default_level,
"SCardReconnect { hCard: %p",
1694 if (status == SCARD_S_SUCCESS)
1696 SCardHandle* hdl = HashTable_GetItemValue(smartcard->handles, (
const void*)hCard);
1700 hdl->dwShareMode = dwShareMode;
1701 hdl->transaction = FALSE;
1703 *pdwActiveProtocol = hdl->dwActiveProtocol;
1706 WLog_Print(smartcard->log, smartcard->log_default_level,
1707 "SCardReconnect } status: %s (0x%08" PRIX32
")", SCardGetErrorString(status),
1713 LONG WINAPI Emulate_SCardDisconnect(SmartcardEmulationContext* smartcard, SCARDHANDLE hCard,
1714 DWORD dwDisposition)
1716 LONG status = scard_handle_valid(smartcard, hCard);
1718 WLog_Print(smartcard->log, smartcard->log_default_level,
"SCardDisconnect { hCard: %p",
1721 WINPR_UNUSED(dwDisposition);
1723 if (status == SCARD_S_SUCCESS)
1725 SCardHandle* hdl = HashTable_GetItemValue(smartcard->handles, (
const void*)hCard);
1728 hdl->referencecount--;
1729 if (hdl->referencecount == 0)
1730 HashTable_Remove(smartcard->handles, (
const void*)hCard);
1733 WLog_Print(smartcard->log, smartcard->log_default_level,
1734 "SCardDisconnect } status: %s (0x%08" PRIX32
")", SCardGetErrorString(status),
1740 LONG WINAPI Emulate_SCardBeginTransaction(SmartcardEmulationContext* smartcard, SCARDHANDLE hCard)
1742 LONG status = scard_handle_valid(smartcard, hCard);
1744 WLog_Print(smartcard->log, smartcard->log_default_level,
"SCardBeginTransaction { hCard: %p",
1747 if (status == SCARD_S_SUCCESS)
1749 SCardHandle* hdl = HashTable_GetItemValue(smartcard->handles, (
const void*)hCard);
1751 if (hdl->transaction)
1752 status = SCARD_E_INVALID_VALUE;
1754 hdl->transaction = TRUE;
1757 WLog_Print(smartcard->log, smartcard->log_default_level,
1758 "SCardBeginTransaction } status: %s (0x%08" PRIX32
")", SCardGetErrorString(status),
1764 LONG WINAPI Emulate_SCardEndTransaction(SmartcardEmulationContext* smartcard, SCARDHANDLE hCard,
1765 DWORD dwDisposition)
1767 LONG status = scard_handle_valid(smartcard, hCard);
1769 WLog_Print(smartcard->log, smartcard->log_default_level,
"SCardEndTransaction { hCard: %p",
1772 WINPR_UNUSED(dwDisposition);
1774 if (status == SCARD_S_SUCCESS)
1776 SCardHandle* hdl = HashTable_GetItemValue(smartcard->handles, (
const void*)hCard);
1778 if (!hdl->transaction)
1779 status = SCARD_E_NOT_TRANSACTED;
1781 hdl->transaction = FALSE;
1784 WLog_Print(smartcard->log, smartcard->log_default_level,
1785 "SCardEndTransaction } status: %s (0x%08" PRIX32
")", SCardGetErrorString(status),
1791 LONG WINAPI Emulate_SCardCancelTransaction(SmartcardEmulationContext* smartcard, SCARDHANDLE hCard)
1793 LONG status = scard_handle_valid(smartcard, hCard);
1795 WLog_Print(smartcard->log, smartcard->log_default_level,
"SCardCancelTransaction { hCard: %p",
1798 if (status == SCARD_S_SUCCESS)
1800 SCardHandle* hdl = HashTable_GetItemValue(smartcard->handles, (
const void*)hCard);
1802 if (!hdl->transaction)
1803 status = SCARD_E_NOT_TRANSACTED;
1805 hdl->transaction = FALSE;
1808 WLog_Print(smartcard->log, smartcard->log_default_level,
1809 "SCardCancelTransaction } status: %s (0x%08" PRIX32
")", SCardGetErrorString(status),
1815 LONG WINAPI Emulate_SCardState(SmartcardEmulationContext* smartcard, SCARDHANDLE hCard,
1816 LPDWORD pdwState, LPDWORD pdwProtocol, LPBYTE pbAtr,
1819 LONG status = scard_handle_valid(smartcard, hCard);
1821 if (!pdwState || !pdwProtocol)
1822 status = SCARD_E_INVALID_PARAMETER;
1824 WLog_Print(smartcard->log, smartcard->log_default_level,
"SCardState { hCard: %p",
1827 if (status == SCARD_S_SUCCESS)
1829 SCardHandle* hdl = HashTable_GetItemValue(smartcard->handles, (
const void*)hCard);
1833 *pdwState = SCARD_SPECIFIC;
1835 *pdwProtocol = SCARD_PROTOCOL_T1;
1840 HashTable_GetItemValue(smartcard->contexts, (
const void*)hdl->hContext);
1843 for (
size_t x = 0; x < MAX_EMULATED_READERS; x++)
1849 if (_wcscmp(readerW->szReader, hdl->szReader.pw) == 0)
1851 *pcbAtrLen = scard_copy_strings(ctx, pbAtr, *pcbAtrLen, readerW->rgbAtr,
1857 if (strcmp(readerA->szReader, hdl->szReader.pc) == 0)
1859 *pcbAtrLen = scard_copy_strings(ctx, pbAtr, *pcbAtrLen, readerA->rgbAtr,
1867 WLog_Print(smartcard->log, smartcard->log_default_level,
1868 "SCardState } status: %s (0x%08" PRIX32
")", SCardGetErrorString(status), status);
1873 LONG WINAPI Emulate_SCardStatusA(SmartcardEmulationContext* smartcard, SCARDHANDLE hCard,
1874 LPSTR mszReaderNames, LPDWORD pcchReaderLen, LPDWORD pdwState,
1875 LPDWORD pdwProtocol, LPBYTE pbAtr, LPDWORD pcbAtrLen)
1877 LONG status = scard_handle_valid(smartcard, hCard);
1879 WLog_Print(smartcard->log, smartcard->log_default_level,
"SCardStatusA { hCard: %p",
1882 if (status == SCARD_S_SUCCESS)
1884 SCardContext* ctx = NULL;
1885 SCardHandle* hdl = HashTable_GetItemValue(smartcard->handles, (
const void*)hCard);
1888 ctx = HashTable_GetItemValue(smartcard->contexts, (
const void*)hdl->hContext);
1893 scard_copy_strings(ctx, mszReaderNames, *pcchReaderLen, hdl->szReader.pc,
1894 (UINT32)strlen(hdl->szReader.pc) + 2);
1897 *pdwState = SCARD_SPECIFIC;
1899 *pdwProtocol = SCARD_PROTOCOL_T1;
1903 for (
size_t x = 0; x < MAX_EMULATED_READERS; x++)
1906 if (strcmp(reader->szReader, hdl->szReader.pc) == 0)
1909 scard_copy_strings(ctx, pbAtr, *pcbAtrLen, reader->rgbAtr, reader->cbAtr);
1915 WLog_Print(smartcard->log, smartcard->log_default_level,
1916 "SCardStatusA } status: %s (0x%08" PRIX32
")", SCardGetErrorString(status), status);
1921 LONG WINAPI Emulate_SCardStatusW(SmartcardEmulationContext* smartcard, SCARDHANDLE hCard,
1922 LPWSTR mszReaderNames, LPDWORD pcchReaderLen, LPDWORD pdwState,
1923 LPDWORD pdwProtocol, LPBYTE pbAtr, LPDWORD pcbAtrLen)
1925 LONG status = scard_handle_valid(smartcard, hCard);
1927 WLog_Print(smartcard->log, smartcard->log_default_level,
"SCardStatusW { hCard: %p",
1930 if (status == SCARD_S_SUCCESS)
1932 SCardContext* ctx = NULL;
1933 SCardHandle* hdl = HashTable_GetItemValue(smartcard->handles, (
const void*)hCard);
1936 ctx = HashTable_GetItemValue(smartcard->contexts, (
const void*)hdl->hContext);
1941 scard_copy_strings(ctx, mszReaderNames, *pcchReaderLen, hdl->szReader.pw,
1942 (UINT32)(_wcslen(hdl->szReader.pw) + 2) *
sizeof(WCHAR)) /
1946 *pdwState = SCARD_SPECIFIC;
1948 *pdwProtocol = SCARD_PROTOCOL_T1;
1952 for (
size_t x = 0; x < MAX_EMULATED_READERS; x++)
1955 if (_wcscmp(reader->szReader, hdl->szReader.pw) == 0)
1957 scard_copy_strings(ctx, pbAtr, *pcbAtrLen, reader->rgbAtr, reader->cbAtr);
1962 WLog_Print(smartcard->log, smartcard->log_default_level,
1963 "SCardStatusW } status: %s (0x%08" PRIX32
")", SCardGetErrorString(status), status);
1968 LONG WINAPI Emulate_SCardTransmit(SmartcardEmulationContext* smartcard, SCARDHANDLE hCard,
1971 LPBYTE pbRecvBuffer, LPDWORD pcbRecvLength)
1973 LONG status = scard_handle_valid(smartcard, hCard);
1975 if (!pioSendPci || !pbSendBuffer || !pbRecvBuffer || !pcbRecvLength)
1976 status = SCARD_E_INVALID_PARAMETER;
1978 WLog_Print(smartcard->log, smartcard->log_default_level,
"SCardTransmit { hCard: %p",
1981 if (status == SCARD_S_SUCCESS)
1983 BYTE* response = NULL;
1984 DWORD responseSize = 0;
1985 SCardHandle* hdl = HashTable_GetItemValue(smartcard->handles, (
const void*)hCard);
1988 hdl->transmitcount++;
1990 if (!vgids_process_apdu(hdl->vgids, pbSendBuffer, cbSendLength, &response, &responseSize))
1991 status = SCARD_E_NO_SMARTCARD;
1995 HashTable_GetItemValue(smartcard->contexts, (
const void*)hdl->hContext);
1999 scard_copy_strings(ctx, pbRecvBuffer, *pcbRecvLength, response, responseSize);
2004 pioRecvPci->dwProtocol = hdl->dwActiveProtocol;
2008 WLog_Print(smartcard->log, smartcard->log_default_level,
2009 "SCardTransmit } status: %s (0x%08" PRIX32
")", SCardGetErrorString(status), status);
2014 LONG WINAPI Emulate_SCardGetTransmitCount(SmartcardEmulationContext* smartcard, SCARDHANDLE hCard,
2015 LPDWORD pcTransmitCount)
2017 LONG status = scard_handle_valid(smartcard, hCard);
2019 if (!pcTransmitCount)
2020 status = SCARD_E_INVALID_PARAMETER;
2022 WLog_Print(smartcard->log, smartcard->log_default_level,
"SCardGetTransmitCount { hCard: %p",
2025 if (status == SCARD_S_SUCCESS)
2027 SCardHandle* hdl = HashTable_GetItemValue(smartcard->handles, (
const void*)hCard);
2030 *pcTransmitCount = hdl->transmitcount;
2033 WLog_Print(smartcard->log, smartcard->log_default_level,
2034 "SCardGetTransmitCount } status: %s (0x%08" PRIX32
")", SCardGetErrorString(status),
2040 LONG WINAPI Emulate_SCardControl(
2041 SmartcardEmulationContext* smartcard, SCARDHANDLE hCard, DWORD dwControlCode,
2042 LPCVOID lpInBuffer, DWORD cbInBufferSize, LPVOID lpOutBuffer, DWORD cbOutBufferSize,
2043 LPDWORD lpBytesReturned )
2045 LONG status = scard_handle_valid(smartcard, hCard);
2047 WLog_Print(smartcard->log, smartcard->log_default_level,
"SCardControl { hCard: %p",
2050 if (status == SCARD_S_SUCCESS)
2052 WINPR_UNUSED(dwControlCode);
2053 WINPR_UNUSED(lpInBuffer);
2054 WINPR_UNUSED(cbInBufferSize);
2055 WINPR_UNUSED(lpOutBuffer);
2056 WINPR_UNUSED(cbOutBufferSize);
2057 WINPR_UNUSED(lpBytesReturned);
2060 status = SCARD_E_UNSUPPORTED_FEATURE;
2063 WLog_Print(smartcard->log, smartcard->log_default_level,
2064 "SCardControl } status: %s (0x%08" PRIX32
")", SCardGetErrorString(status), status);
2069 LONG WINAPI Emulate_SCardGetAttrib(SmartcardEmulationContext* smartcard, SCARDHANDLE hCard,
2072 LPDWORD pcbAttrLen )
2074 LONG status = scard_handle_valid(smartcard, hCard);
2076 WLog_Print(smartcard->log, smartcard->log_default_level,
"SCardGetAttrib { hCard: %p",
2079 WINPR_UNUSED(dwAttrId);
2080 WINPR_UNUSED(pbAttr);
2081 WINPR_UNUSED(pcbAttrLen);
2084 if (status == SCARD_S_SUCCESS)
2085 status = SCARD_F_INTERNAL_ERROR;
2087 WLog_Print(smartcard->log, smartcard->log_default_level,
2088 "SCardGetAttrib } status: %s (0x%08" PRIX32
")", SCardGetErrorString(status),
2094 LONG WINAPI Emulate_SCardSetAttrib(SmartcardEmulationContext* smartcard, SCARDHANDLE hCard,
2095 DWORD dwAttrId, LPCBYTE pbAttr, DWORD cbAttrLen)
2097 LONG status = scard_handle_valid(smartcard, hCard);
2099 WLog_Print(smartcard->log, smartcard->log_default_level,
"SCardSetAttrib { hCard: %p",
2102 WINPR_UNUSED(dwAttrId);
2103 WINPR_UNUSED(pbAttr);
2104 WINPR_UNUSED(cbAttrLen);
2107 if (status == SCARD_S_SUCCESS)
2108 status = SCARD_F_INTERNAL_ERROR;
2110 WLog_Print(smartcard->log, smartcard->log_default_level,
2111 "SCardSetAttrib } status: %s (0x%08" PRIX32
")", SCardGetErrorString(status),
2117 LONG WINAPI Emulate_SCardUIDlgSelectCardA(SmartcardEmulationContext* smartcard,
2118 LPOPENCARDNAMEA_EX pDlgStruc)
2122 WINPR_ASSERT(smartcard);
2124 WLog_Print(smartcard->log, smartcard->log_default_level,
"SCardUIDlgSelectCardA {");
2126 WINPR_UNUSED(pDlgStruc);
2129 status = SCARD_E_UNSUPPORTED_FEATURE;
2131 WLog_Print(smartcard->log, smartcard->log_default_level,
2132 "SCardUIDlgSelectCardA } status: %s (0x%08" PRIX32
")", SCardGetErrorString(status),
2138 LONG WINAPI Emulate_SCardUIDlgSelectCardW(SmartcardEmulationContext* smartcard,
2139 LPOPENCARDNAMEW_EX pDlgStruc)
2143 WINPR_ASSERT(smartcard);
2145 WLog_Print(smartcard->log, smartcard->log_default_level,
"SCardUIDlgSelectCardW {");
2147 WINPR_UNUSED(pDlgStruc);
2150 status = SCARD_E_UNSUPPORTED_FEATURE;
2152 WLog_Print(smartcard->log, smartcard->log_default_level,
2153 "SCardUIDlgSelectCardW } status: %s (0x%08" PRIX32
")", SCardGetErrorString(status),
2159 LONG WINAPI Emulate_GetOpenCardNameA(SmartcardEmulationContext* smartcard,
2164 WINPR_ASSERT(smartcard);
2166 WLog_Print(smartcard->log, smartcard->log_default_level,
"GetOpenCardNameA {");
2168 WINPR_UNUSED(pDlgStruc);
2171 status = SCARD_E_UNSUPPORTED_FEATURE;
2173 WLog_Print(smartcard->log, smartcard->log_default_level,
2174 "GetOpenCardNameA } status: %s (0x%08" PRIX32
")", SCardGetErrorString(status),
2180 LONG WINAPI Emulate_GetOpenCardNameW(SmartcardEmulationContext* smartcard,
2185 WINPR_ASSERT(smartcard);
2187 WLog_Print(smartcard->log, smartcard->log_default_level,
"GetOpenCardNameW {");
2189 WINPR_UNUSED(pDlgStruc);
2192 status = SCARD_E_UNSUPPORTED_FEATURE;
2194 WLog_Print(smartcard->log, smartcard->log_default_level,
2195 "GetOpenCardNameW } status: %s (0x%08" PRIX32
")", SCardGetErrorString(status),
2201 LONG WINAPI Emulate_SCardDlgExtendedError(SmartcardEmulationContext* smartcard)
2205 WINPR_ASSERT(smartcard);
2207 WLog_Print(smartcard->log, smartcard->log_default_level,
"SCardDlgExtendedError {");
2210 status = SCARD_E_UNSUPPORTED_FEATURE;
2212 WLog_Print(smartcard->log, smartcard->log_default_level,
2213 "SCardDlgExtendedError } status: %s (0x%08" PRIX32
")", SCardGetErrorString(status),
2219 LONG WINAPI Emulate_SCardReadCacheA(SmartcardEmulationContext* smartcard, SCARDCONTEXT hContext,
2220 UUID* CardIdentifier, DWORD FreshnessCounter, LPSTR LookupName,
2221 PBYTE Data, DWORD* DataLen)
2224 LONG status = Emulate_SCardIsValidContext(smartcard, hContext);
2226 if (!CardIdentifier || !DataLen)
2227 status = SCARD_E_INVALID_PARAMETER;
2229 WLog_Print(smartcard->log, smartcard->log_default_level,
"SCardReadCacheA { hContext: %p",
2238 if (status == SCARD_S_SUCCESS)
2240 SCardCacheItem* data = NULL;
2241 SCardContext* value = HashTable_GetItemValue(smartcard->contexts, (
const void*)hContext);
2242 WINPR_ASSERT(value);
2244 char*
id = card_id_and_name_a(CardIdentifier, LookupName);
2245 data = HashTable_GetItemValue(value->cache,
id);
2249 status = SCARD_W_CACHE_ITEM_NOT_FOUND;
2250 else if (data->freshness != FreshnessCounter)
2251 status = SCARD_W_CACHE_ITEM_STALE;
2253 *DataLen = scard_copy_strings(value, Data, count, data->data, data->size);
2256 WLog_Print(smartcard->log, smartcard->log_default_level,
2257 "SCardReadCacheA } status: %s (0x%08" PRIX32
")", SCardGetErrorString(status),
2263 LONG WINAPI Emulate_SCardReadCacheW(SmartcardEmulationContext* smartcard, SCARDCONTEXT hContext,
2264 UUID* CardIdentifier, DWORD FreshnessCounter, LPWSTR LookupName,
2265 PBYTE Data, DWORD* DataLen)
2268 LONG status = Emulate_SCardIsValidContext(smartcard, hContext);
2270 if (!CardIdentifier || !DataLen)
2271 status = SCARD_E_INVALID_PARAMETER;
2273 WLog_Print(smartcard->log, smartcard->log_default_level,
"SCardReadCacheW { hContext: %p",
2282 if (status == SCARD_S_SUCCESS)
2284 SCardCacheItem* data = NULL;
2285 SCardContext* value = HashTable_GetItemValue(smartcard->contexts, (
const void*)hContext);
2286 WINPR_ASSERT(value);
2288 char*
id = card_id_and_name_w(CardIdentifier, LookupName);
2289 data = HashTable_GetItemValue(value->cache,
id);
2292 status = SCARD_W_CACHE_ITEM_NOT_FOUND;
2293 else if (data->freshness != FreshnessCounter)
2294 status = SCARD_W_CACHE_ITEM_STALE;
2296 *DataLen = scard_copy_strings(value, Data, count, data->data, data->size);
2299 WLog_Print(smartcard->log, smartcard->log_default_level,
2300 "SCardReadCacheW } status: %s (0x%08" PRIX32
")", SCardGetErrorString(status),
2306 static LONG insert_data(wHashTable* table, DWORD FreshnessCounter,
const char* key,
2307 const PBYTE Data, DWORD DataLen)
2310 SCardCacheItem* item = NULL;
2312 WINPR_ASSERT(table);
2315 if (DataLen > MAX_CACHE_ITEM_SIZE)
2316 return SCARD_W_CACHE_ITEM_TOO_BIG;
2318 if (HashTable_Count(table) > MAX_CACHE_ITEM_VALUES)
2319 return SCARD_E_WRITE_TOO_MANY;
2321 item = HashTable_GetItemValue(table, key);
2324 item = calloc(1,
sizeof(SCardCacheItem));
2326 return SCARD_E_NO_MEMORY;
2328 rc = HashTable_Insert(table, key, item);
2332 return SCARD_E_NO_MEMORY;
2336 if (item->freshness > FreshnessCounter)
2337 return SCARD_W_CACHE_ITEM_STALE;
2338 item->freshness = FreshnessCounter;
2339 item->size = DataLen;
2340 memcpy(item->data, Data, DataLen);
2343 return SCARD_S_SUCCESS;
2346 LONG WINAPI Emulate_SCardWriteCacheA(SmartcardEmulationContext* smartcard, SCARDCONTEXT hContext,
2347 UUID* CardIdentifier, DWORD FreshnessCounter, LPSTR LookupName,
2348 PBYTE Data, DWORD DataLen)
2350 LONG status = Emulate_SCardIsValidContext(smartcard, hContext);
2352 if (!CardIdentifier)
2353 status = SCARD_E_INVALID_PARAMETER;
2355 WLog_Print(smartcard->log, smartcard->log_default_level,
"SCardWriteCacheA { hContext: %p",
2358 if (status == SCARD_S_SUCCESS)
2360 SCardContext* value = HashTable_GetItemValue(smartcard->contexts, (
const void*)hContext);
2361 WINPR_ASSERT(value);
2363 char*
id = card_id_and_name_a(CardIdentifier, LookupName);
2365 status = SCARD_E_NO_MEMORY;
2368 status = insert_data(value->cache, FreshnessCounter,
id, Data, DataLen);
2373 WLog_Print(smartcard->log, smartcard->log_default_level,
2374 "SCardWriteCacheA } status: %s (0x%08" PRIX32
")", SCardGetErrorString(status),
2380 LONG WINAPI Emulate_SCardWriteCacheW(SmartcardEmulationContext* smartcard, SCARDCONTEXT hContext,
2381 UUID* CardIdentifier, DWORD FreshnessCounter,
2382 LPWSTR LookupName, PBYTE Data, DWORD DataLen)
2384 LONG status = Emulate_SCardIsValidContext(smartcard, hContext);
2386 if (!CardIdentifier)
2387 status = SCARD_E_INVALID_PARAMETER;
2389 WLog_Print(smartcard->log, smartcard->log_default_level,
"SCardWriteCacheW { hContext: %p",
2392 if (status == SCARD_S_SUCCESS)
2394 SCardContext* value = HashTable_GetItemValue(smartcard->contexts, (
const void*)hContext);
2395 WINPR_ASSERT(value);
2397 char*
id = card_id_and_name_w(CardIdentifier, LookupName);
2399 status = SCARD_E_NO_MEMORY;
2402 status = insert_data(value->cache, FreshnessCounter,
id, Data, DataLen);
2407 WLog_Print(smartcard->log, smartcard->log_default_level,
2408 "SCardWriteCacheW } status: %s (0x%08" PRIX32
")", SCardGetErrorString(status),
2414 LONG WINAPI Emulate_SCardGetReaderIconA(SmartcardEmulationContext* smartcard, SCARDCONTEXT hContext,
2415 LPCSTR szReaderName, LPBYTE pbIcon, LPDWORD pcbIcon)
2417 LONG status = Emulate_SCardIsValidContext(smartcard, hContext);
2419 if (!szReaderName || !pcbIcon)
2420 status = SCARD_E_INVALID_PARAMETER;
2422 WLog_Print(smartcard->log, smartcard->log_default_level,
"SCardGetReaderIconA { hContext: %p",
2425 if (status == SCARD_S_SUCCESS)
2426 status = scard_reader_name_valid_a(smartcard, hContext, szReaderName);
2428 if (status == SCARD_S_SUCCESS)
2430 SCardContext* ctx = HashTable_GetItemValue(smartcard->contexts, (
const void*)hContext);
2434 *pcbIcon = scard_copy_strings(ctx, pbIcon, *pcbIcon, resources_FreeRDP_ico,
2435 resources_FreeRDP_ico_len);
2437 *pcbIcon = resources_FreeRDP_ico_len;
2440 WLog_Print(smartcard->log, smartcard->log_default_level,
2441 "SCardGetReaderIconA } status: %s (0x%08" PRIX32
")", SCardGetErrorString(status),
2447 LONG WINAPI Emulate_SCardGetReaderIconW(SmartcardEmulationContext* smartcard, SCARDCONTEXT hContext,
2448 LPCWSTR szReaderName, LPBYTE pbIcon, LPDWORD pcbIcon)
2450 LONG status = Emulate_SCardIsValidContext(smartcard, hContext);
2452 if (!szReaderName || !pcbIcon)
2453 status = SCARD_E_INVALID_PARAMETER;
2455 WLog_Print(smartcard->log, smartcard->log_default_level,
"SCardGetReaderIconW { hContext: %p",
2458 if (status == SCARD_S_SUCCESS)
2459 status = scard_reader_name_valid_w(smartcard, hContext, szReaderName);
2461 if (status == SCARD_S_SUCCESS)
2463 SCardContext* ctx = HashTable_GetItemValue(smartcard->contexts, (
const void*)hContext);
2467 *pcbIcon = scard_copy_strings(ctx, pbIcon, *pcbIcon, resources_FreeRDP_ico,
2468 resources_FreeRDP_ico_len);
2470 *pcbIcon = resources_FreeRDP_ico_len;
2473 WLog_Print(smartcard->log, smartcard->log_default_level,
2474 "SCardGetReaderIconW } status: %s (0x%08" PRIX32
")", SCardGetErrorString(status),
2480 LONG WINAPI Emulate_SCardGetDeviceTypeIdA(SmartcardEmulationContext* smartcard,
2481 SCARDCONTEXT hContext, LPCSTR szReaderName,
2482 LPDWORD pdwDeviceTypeId)
2484 LONG status = Emulate_SCardIsValidContext(smartcard, hContext);
2486 if (!pdwDeviceTypeId)
2487 status = SCARD_E_INVALID_PARAMETER;
2489 if (status == SCARD_S_SUCCESS)
2490 status = scard_reader_name_valid_a(smartcard, hContext, szReaderName);
2492 WLog_Print(smartcard->log, smartcard->log_default_level,
"SCardGetDeviceTypeIdA { hContext: %p",
2495 if (status == SCARD_S_SUCCESS)
2497 *pdwDeviceTypeId = SCARD_READER_TYPE_USB;
2500 WLog_Print(smartcard->log, smartcard->log_default_level,
2501 "SCardGetDeviceTypeIdA } status: %s (0x%08" PRIX32
")", SCardGetErrorString(status),
2507 LONG WINAPI Emulate_SCardGetDeviceTypeIdW(SmartcardEmulationContext* smartcard,
2508 SCARDCONTEXT hContext, LPCWSTR szReaderName,
2509 LPDWORD pdwDeviceTypeId)
2511 LONG status = Emulate_SCardIsValidContext(smartcard, hContext);
2513 if (!pdwDeviceTypeId)
2514 status = SCARD_E_INVALID_PARAMETER;
2516 if (status == SCARD_S_SUCCESS)
2517 status = scard_reader_name_valid_w(smartcard, hContext, szReaderName);
2519 WLog_Print(smartcard->log, smartcard->log_default_level,
"SCardGetDeviceTypeIdW { hContext: %p",
2522 if (status == SCARD_S_SUCCESS)
2524 *pdwDeviceTypeId = SCARD_READER_TYPE_USB;
2527 WLog_Print(smartcard->log, smartcard->log_default_level,
2528 "SCardGetDeviceTypeIdW } status: %s (0x%08" PRIX32
")", SCardGetErrorString(status),
2534 LONG WINAPI Emulate_SCardGetReaderDeviceInstanceIdA(
2535 SmartcardEmulationContext* smartcard, SCARDCONTEXT hContext, LPCSTR szReaderName,
2536 LPSTR szDeviceInstanceId ,
2537 LPDWORD pcchDeviceInstanceId )
2539 LONG status = Emulate_SCardIsValidContext(smartcard, hContext);
2541 if (status == SCARD_S_SUCCESS)
2542 status = scard_reader_name_valid_a(smartcard, hContext, szReaderName);
2544 WLog_Print(smartcard->log, smartcard->log_default_level,
2545 "SCardGetReaderDeviceInstanceIdA { hContext: %p", (
void*)hContext);
2547 WINPR_UNUSED(szDeviceInstanceId);
2548 WINPR_UNUSED(pcchDeviceInstanceId);
2551 if (status == SCARD_S_SUCCESS)
2552 status = SCARD_E_UNSUPPORTED_FEATURE;
2554 WLog_Print(smartcard->log, smartcard->log_default_level,
2555 "SCardGetReaderDeviceInstanceIdA } status: %s (0x%08" PRIX32
")",
2556 SCardGetErrorString(status), status);
2561 LONG WINAPI Emulate_SCardGetReaderDeviceInstanceIdW(
2562 SmartcardEmulationContext* smartcard, SCARDCONTEXT hContext, LPCWSTR szReaderName,
2563 LPWSTR szDeviceInstanceId ,
2564 LPDWORD pcchDeviceInstanceId )
2566 LONG status = Emulate_SCardIsValidContext(smartcard, hContext);
2568 if (status == SCARD_S_SUCCESS)
2569 status = scard_reader_name_valid_w(smartcard, hContext, szReaderName);
2571 WLog_Print(smartcard->log, smartcard->log_default_level,
2572 "SCardGetReaderDeviceInstanceIdW { hContext: %p", (
void*)hContext);
2574 WINPR_UNUSED(szDeviceInstanceId);
2575 WINPR_UNUSED(pcchDeviceInstanceId);
2578 if (status == SCARD_S_SUCCESS)
2579 status = SCARD_E_UNSUPPORTED_FEATURE;
2581 WLog_Print(smartcard->log, smartcard->log_default_level,
2582 "SCardGetReaderDeviceInstanceIdW } status: %s (0x%08" PRIX32
")",
2583 SCardGetErrorString(status), status);
2588 LONG WINAPI Emulate_SCardListReadersWithDeviceInstanceIdA(
2589 SmartcardEmulationContext* smartcard, SCARDCONTEXT hContext, LPCSTR szDeviceInstanceId,
2591 LPDWORD pcchReaders )
2593 LONG status = Emulate_SCardIsValidContext(smartcard, hContext);
2595 WLog_Print(smartcard->log, smartcard->log_default_level,
2596 "SCardListReadersWithDeviceInstanceIdA { hContext: %p", (
void*)hContext);
2598 WINPR_UNUSED(szDeviceInstanceId);
2599 WINPR_UNUSED(mszReaders);
2600 WINPR_UNUSED(pcchReaders);
2603 if (status == SCARD_S_SUCCESS)
2604 status = SCARD_E_UNSUPPORTED_FEATURE;
2606 WLog_Print(smartcard->log, smartcard->log_default_level,
2607 "SCardListReadersWithDeviceInstanceIdA } status: %s (0x%08" PRIX32
")",
2608 SCardGetErrorString(status), status);
2613 LONG WINAPI Emulate_SCardListReadersWithDeviceInstanceIdW(
2614 SmartcardEmulationContext* smartcard, SCARDCONTEXT hContext, LPCWSTR szDeviceInstanceId,
2615 LPWSTR mszReaders , LPDWORD pcchReaders)
2617 LONG status = Emulate_SCardIsValidContext(smartcard, hContext);
2619 WLog_Print(smartcard->log, smartcard->log_default_level,
2620 "SCardListReadersWithDeviceInstanceIdW { hContext: %p", (
void*)hContext);
2622 WINPR_UNUSED(szDeviceInstanceId);
2623 WINPR_UNUSED(mszReaders);
2624 WINPR_UNUSED(pcchReaders);
2627 if (status == SCARD_S_SUCCESS)
2628 status = SCARD_E_UNSUPPORTED_FEATURE;
2630 WLog_Print(smartcard->log, smartcard->log_default_level,
2631 "SCardListReadersWithDeviceInstanceIdW } status: %s (0x%08" PRIX32
")",
2632 SCardGetErrorString(status), status);
2637 LONG WINAPI Emulate_SCardAudit(SmartcardEmulationContext* smartcard, SCARDCONTEXT hContext,
2640 LONG status = Emulate_SCardIsValidContext(smartcard, hContext);
2642 WINPR_UNUSED(dwEvent);
2644 WLog_Print(smartcard->log, smartcard->log_default_level,
"SCardAudit { hContext: %p",
2648 if (status == SCARD_S_SUCCESS)
2649 status = SCARD_E_UNSUPPORTED_FEATURE;
2651 WLog_Print(smartcard->log, smartcard->log_default_level,
2652 "SCardAudit } status: %s (0x%08" PRIX32
")", SCardGetErrorString(status), status);
2657 static BOOL context_equals(
const void* pva,
const void* pvb)
2659 const SCARDCONTEXT a = (
const SCARDCONTEXT)pva;
2660 const SCARDCONTEXT b = (
const SCARDCONTEXT)pvb;
2669 static BOOL handle_equals(
const void* pva,
const void* pvb)
2671 const SCARDHANDLE a = (
const SCARDHANDLE)pva;
2672 const SCARDHANDLE b = (
const SCARDHANDLE)pvb;
2681 SmartcardEmulationContext* Emulate_New(
const rdpSettings* settings)
2683 SmartcardEmulationContext* smartcard = NULL;
2685 WINPR_ASSERT(settings);
2687 smartcard = calloc(1,
sizeof(SmartcardEmulationContext));
2691 smartcard->settings = settings;
2692 smartcard->log = WLog_Get(
"EmulateSCard");
2693 if (!smartcard->log)
2695 smartcard->log_default_level = WLOG_TRACE;
2697 smartcard->contexts = HashTable_New(FALSE);
2698 if (!smartcard->contexts)
2702 wObject* obj = HashTable_KeyObject(smartcard->contexts);
2704 obj->fnObjectEquals = context_equals;
2706 if (!smartcard->contexts)
2710 wObject* obj = HashTable_ValueObject(smartcard->contexts);
2712 obj->fnObjectFree = scard_context_free;
2715 smartcard->handles = HashTable_New(FALSE);
2716 if (!smartcard->handles)
2720 wObject* obj = HashTable_KeyObject(smartcard->handles);
2722 obj->fnObjectEquals = handle_equals;
2724 if (!smartcard->handles)
2728 wObject* obj = HashTable_ValueObject(smartcard->handles);
2730 obj->fnObjectFree = scard_handle_free;
2736 WINPR_PRAGMA_DIAG_PUSH
2737 WINPR_PRAGMA_DIAG_IGNORED_MISMATCHED_DEALLOC
2738 Emulate_Free(smartcard);
2739 WINPR_PRAGMA_DIAG_POP
2743 void Emulate_Free(SmartcardEmulationContext* context)
2748 HashTable_Free(context->handles);
2749 HashTable_Free(context->contexts);
2753 BOOL Emulate_IsConfigured(SmartcardEmulationContext* context)
2756 vgidsContext* vgids = NULL;
2757 const char* pem = NULL;
2758 const char* key = NULL;
2759 const char* pin = NULL;
2761 WINPR_ASSERT(context);
2768 if ((context->pem == pem) && (context->key == key) && (context->pin == pin))
2769 return context->configured;
2775 vgids = vgids_new();
2777 rc = vgids_init(vgids, context->pem, context->key, context->pin);
2780 context->configured = rc;
FREERDP_API const void * freerdp_settings_get_pointer(const rdpSettings *settings, FreeRDP_Settings_Keys_Pointer id)
Returns a immutable pointer settings value.
FREERDP_API const char * freerdp_settings_get_string(const rdpSettings *settings, FreeRDP_Settings_Keys_String id)
Returns a immutable string settings value.
This struct contains function pointer to initialize/free objects.