24#include <freerdp/config.h>
27#include <winpr/print.h>
29#include <freerdp/channels/scard.h>
30#include <freerdp/utils/smartcard_pack.h>
32#include <freerdp/log.h>
33#define TAG FREERDP_TAG("scard.pack")
35static const DWORD g_LogLevel = WLOG_DEBUG;
37#define smartcard_unpack_redir_scard_context(s, context, index, ndr) \
38 smartcard_unpack_redir_scard_context_((s), (context), (index), (ndr), __FILE__, __func__, \
40#define smartcard_unpack_redir_scard_handle(s, context, index) \
41 smartcard_unpack_redir_scard_handle_((s), (context), (index), __FILE__, __func__, __LINE__)
44 UINT32* index, UINT32* ppbContextNdrPtr,
45 const char* file,
const char* function,
int line);
49 UINT32* index,
const char* file,
50 const char* function,
int line);
53static LONG smartcard_unpack_redir_scard_context_ref(
wStream* s, UINT32 pbContextNdrPtr,
69#define smartcard_ndr_pointer_read(s, index, ptr) \
70 smartcard_ndr_pointer_read_((s), (index), (ptr), __FILE__, __func__, __LINE__)
71static BOOL smartcard_ndr_pointer_read_(
wStream* s, UINT32* index, UINT32* ptr,
const char* file,
72 const char* fkt,
size_t line)
74 const UINT32 expect = 0x20000 + (*index) * 4;
79 if (!Stream_CheckAndLogRequiredLength(TAG, s, 4))
82 Stream_Read_UINT32(s, ndrPtr);
88 if (ptr && (ndrPtr == 0))
91 "[%s:%" PRIuz
"] Read context pointer 0x%08" PRIx32
", expected 0x%08" PRIx32,
92 fkt, line, ndrPtr, expect);
96 (*index) = (*index) + 1;
100static LONG smartcard_ndr_read(
wStream* s, BYTE** data,
size_t min,
size_t elementSize,
122 return STATUS_INVALID_PARAMETER;
125 if (!Stream_CheckAndLogRequiredLength(TAG, s, required))
126 return STATUS_BUFFER_TOO_SMALL;
131 Stream_Read_UINT32(s, len);
132 Stream_Read_UINT32(s, offset);
133 Stream_Read_UINT32(s, len2);
134 if (len != offset + len2)
137 "Invalid data when reading full NDR pointer: total=%" PRIu32
138 ", offset=%" PRIu32
", remaining=%" PRIu32,
140 return STATUS_BUFFER_TOO_SMALL;
144 Stream_Read_UINT32(s, len);
146 if ((len != min) && (min > 0))
149 "Invalid data when reading simple NDR pointer: total=%" PRIu32
150 ", expected=%" PRIu32,
152 return STATUS_BUFFER_TOO_SMALL;
159 return STATUS_INVALID_PARAMETER;
164 WLog_ERR(TAG,
"Invalid length read from NDR pointer, minimum %" PRIu32
", got %" PRIu32,
166 return STATUS_DATA_ERROR;
169 if (len > SIZE_MAX / 2)
170 return STATUS_BUFFER_TOO_SMALL;
172 if (!Stream_CheckAndLogRequiredLengthOfSize(TAG, s, len, elementSize))
173 return STATUS_BUFFER_TOO_SMALL;
180 r = calloc(len +
sizeof(WCHAR),
sizeof(CHAR));
182 return SCARD_E_NO_MEMORY;
183 Stream_Read(s, r, len);
184 smartcard_unpack_read_size_align(s, len, 4);
186 return STATUS_SUCCESS;
189static BOOL smartcard_ndr_pointer_write(
wStream* s, UINT32* index, DWORD length)
191 const UINT32 ndrPtr = 0x20000 + (*index) * 4;
195 if (!Stream_EnsureRemainingCapacity(s, 4))
200 Stream_Write_UINT32(s, ndrPtr);
201 (*index) = (*index) + 1;
204 Stream_Write_UINT32(s, 0);
208static LONG smartcard_ndr_write(
wStream* s,
const BYTE* data, UINT32 size, UINT32 elementSize,
211 const UINT32 offset = 0;
212 const UINT32 len = size;
213 const UINT32 dataLen = size * elementSize;
217 return SCARD_S_SUCCESS;
231 return SCARD_E_INVALID_PARAMETER;
234 if (!Stream_EnsureRemainingCapacity(s, required + dataLen + 4))
235 return STATUS_BUFFER_TOO_SMALL;
240 Stream_Write_UINT32(s, len);
241 Stream_Write_UINT32(s, offset);
242 Stream_Write_UINT32(s, len);
245 Stream_Write_UINT32(s, len);
250 return SCARD_E_INVALID_PARAMETER;
254 Stream_Write(s, data, dataLen);
256 Stream_Zero(s, dataLen);
257 return smartcard_pack_write_size_align(s, len, 4);
269 WINPR_ASSERT(data || (size == 0));
286static LONG smartcard_ndr_read_fixed_string_a(
wStream* s, CHAR** data,
size_t min, ndr_ptr_t type)
294 return smartcard_ndr_read(s, u.ppv, min,
sizeof(CHAR), type);
297static LONG smartcard_ndr_read_fixed_string_w(
wStream* s, WCHAR** data,
size_t min, ndr_ptr_t type)
305 return smartcard_ndr_read(s, u.ppv, min,
sizeof(WCHAR), type);
308static LONG smartcard_ndr_read_a(
wStream* s, CHAR** data, ndr_ptr_t type)
316 return smartcard_ndr_read(s, u.ppv, 0,
sizeof(CHAR), type);
319static LONG smartcard_ndr_read_w(
wStream* s, WCHAR** data, ndr_ptr_t type)
327 return smartcard_ndr_read(s, u.ppv, 0,
sizeof(WCHAR), type);
330static LONG smartcard_ndr_read_u(
wStream* s,
UUID** data)
338 return smartcard_ndr_read(s, u.ppv, 1,
sizeof(
UUID), NDR_PTR_FIXED);
341static char* smartcard_convert_string_list(
const void* in,
size_t bytes, BOOL unicode)
362 mszA = ConvertMszWCharNToUtf8Alloc(
string.wz, bytes /
sizeof(WCHAR), &length);
368 mszA = (
char*)calloc(bytes,
sizeof(
char));
371 CopyMemory(mszA,
string.sz, bytes - 1);
380 for (
size_t index = 0; index < length - 1; index++)
382 if (mszA[index] ==
'\0')
389static char* smartcard_msz_dump_a(
const char* msz,
size_t len,
char* buffer,
size_t bufferLen)
392 const char* cur = msz;
394 while ((len > 0) && cur && cur[0] !=
'\0' && (bufferLen > 0))
396 size_t clen = strnlen(cur, len);
397 int rc = _snprintf(buf, bufferLen,
"%s", cur);
398 bufferLen -= (size_t)rc;
407static char* smartcard_msz_dump_w(
const WCHAR* msz,
size_t len,
char* buffer,
size_t bufferLen)
412 char* sz = ConvertMszWCharNToUtf8Alloc(msz, len, &szlen);
416 smartcard_msz_dump_a(sz, szlen, buffer, bufferLen);
421static char* smartcard_array_dump(
const void* pd,
size_t len,
char* buffer,
size_t bufferLen)
423 const BYTE* data = pd;
425 char* start = buffer;
427 WINPR_ASSERT(buffer || (bufferLen == 0));
429 if (!data && (len > 0))
431 (void)_snprintf(buffer, bufferLen,
"{ NULL [%" PRIuz
"] }", len);
438 buffer[bufferLen - 1] =
'\0';
442 rc = _snprintf(buffer, bufferLen,
"{ ");
443 if ((rc < 0) || ((
size_t)rc > bufferLen))
446 bufferLen -= (size_t)rc;
448 for (
size_t x = 0; x < len; x++)
450 rc = _snprintf(buffer, bufferLen,
"%02X", data[x]);
451 if ((rc < 0) || ((
size_t)rc > bufferLen))
454 bufferLen -= (size_t)rc;
457 rc = _snprintf(buffer, bufferLen,
" }");
458 if ((rc < 0) || ((
size_t)rc > bufferLen))
465static void smartcard_log_redir_handle(wLog* log,
const REDIR_SCARDHANDLE* pHandle)
467 char buffer[128] = { 0 };
469 WINPR_ASSERT(pHandle);
470 WLog_Print(log, g_LogLevel,
" hContext: %s",
471 smartcard_array_dump(pHandle->pbHandle, pHandle->cbHandle, buffer,
sizeof(buffer)));
476 char buffer[128] = { 0 };
478 WINPR_ASSERT(phContext);
480 log, g_LogLevel,
"hContext: %s",
481 smartcard_array_dump(phContext->pbContext, phContext->cbContext, buffer,
sizeof(buffer)));
484static void smartcard_trace_context_and_string_call_a(
const char* name,
488 wLog* log = WLog_Get(TAG);
489 if (!WLog_IsLevelActive(log, g_LogLevel))
492 WLog_Print(log, g_LogLevel,
"%s {", name);
493 smartcard_log_context(log, phContext);
494 WLog_Print(log, g_LogLevel,
" sz=%s", sz);
496 WLog_Print(log, g_LogLevel,
"}");
499static void smartcard_trace_context_and_string_call_w(
const char* name,
503 char tmp[1024] = { 0 };
504 wLog* log = WLog_Get(TAG);
505 if (!WLog_IsLevelActive(log, g_LogLevel))
509 (void)ConvertWCharToUtf8(sz, tmp, ARRAYSIZE(tmp));
511 WLog_Print(log, g_LogLevel,
"%s {", name);
512 smartcard_log_context(log, phContext);
513 WLog_Print(log, g_LogLevel,
" sz=%s", tmp);
514 WLog_Print(log, g_LogLevel,
"}");
517static void smartcard_trace_context_call(
const Context_Call* call,
const char* name)
521 wLog* log = WLog_Get(TAG);
522 if (!WLog_IsLevelActive(log, g_LogLevel))
525 WLog_Print(log, g_LogLevel,
"%s_Call {", name);
526 smartcard_log_context(log, &call->handles.hContext);
528 WLog_Print(log, g_LogLevel,
"}");
535 wLog* log = WLog_Get(TAG);
536 if (!WLog_IsLevelActive(log, g_LogLevel))
539 WLog_Print(log, g_LogLevel,
"ListReaderGroups%S_Call {", unicode ?
"W" :
"A");
540 smartcard_log_context(log, &call->handles.hContext);
542 WLog_Print(log, g_LogLevel,
"fmszGroupsIsNULL: %" PRId32
" cchGroups: 0x%08" PRIx32,
543 call->fmszGroupsIsNULL, call->cchGroups);
544 WLog_Print(log, g_LogLevel,
"}");
547static void dump_reader_states_return(wLog* log,
const ReaderState_Return* rgReaderStates,
550 WINPR_ASSERT(rgReaderStates || (cReaders == 0));
551 for (UINT32 index = 0; index < cReaders; index++)
553 char buffer[1024] = { 0 };
556 char* szCurrentState = SCardGetReaderStateString(readerState->dwCurrentState);
557 char* szEventState = SCardGetReaderStateString(readerState->dwEventState);
558 WLog_Print(log, g_LogLevel,
"\t[%" PRIu32
"]: dwCurrentState: %s (0x%08" PRIX32
")", index,
559 szCurrentState, readerState->dwCurrentState);
560 WLog_Print(log, g_LogLevel,
"\t[%" PRIu32
"]: dwEventState: %s (0x%08" PRIX32
")", index,
561 szEventState, readerState->dwEventState);
562 free(szCurrentState);
566 log, g_LogLevel,
"\t[%" PRIu32
"]: cbAttr: %" PRIu32
" { %s }", index,
568 smartcard_array_dump(readerState->rgbAtr, readerState->cbAtr, buffer,
sizeof(buffer)));
575 WINPR_ASSERT(rgReaderStates || (cReaders == 0));
576 for (UINT32 index = 0; index < cReaders; index++)
578 char buffer[1024] = { 0 };
581 WLog_Print(log, g_LogLevel,
"\t[%" PRIu32
"]: szReader: %s cbAtr: %" PRIu32
"", index,
582 readerState->szReader, readerState->cbAtr);
583 char* szCurrentState = SCardGetReaderStateString(readerState->dwCurrentState);
584 char* szEventState = SCardGetReaderStateString(readerState->dwEventState);
585 WLog_Print(log, g_LogLevel,
"\t[%" PRIu32
"]: dwCurrentState: %s (0x%08" PRIX32
")", index,
586 szCurrentState, readerState->dwCurrentState);
587 WLog_Print(log, g_LogLevel,
"\t[%" PRIu32
"]: dwEventState: %s (0x%08" PRIX32
")", index,
588 szEventState, readerState->dwEventState);
589 free(szCurrentState);
593 log, g_LogLevel,
"\t[%" PRIu32
"]: cbAttr: %" PRIu32
" { %s }", index,
595 smartcard_array_dump(readerState->rgbAtr, readerState->cbAtr, buffer,
sizeof(buffer)));
602 WINPR_ASSERT(rgReaderStates || (cReaders == 0));
603 for (UINT32 index = 0; index < cReaders; index++)
605 char buffer[1024] = { 0 };
608 (void)ConvertWCharToUtf8(readerState->szReader, buffer,
sizeof(buffer));
609 WLog_Print(log, g_LogLevel,
"\t[%" PRIu32
"]: szReader: %s cbAtr: %" PRIu32
"", index,
610 buffer, readerState->cbAtr);
611 char* szCurrentState = SCardGetReaderStateString(readerState->dwCurrentState);
612 char* szEventState = SCardGetReaderStateString(readerState->dwEventState);
613 WLog_Print(log, g_LogLevel,
"\t[%" PRIu32
"]: dwCurrentState: %s (0x%08" PRIX32
")", index,
614 szCurrentState, readerState->dwCurrentState);
615 WLog_Print(log, g_LogLevel,
"\t[%" PRIu32
"]: dwEventState: %s (0x%08" PRIX32
")", index,
616 szEventState, readerState->dwEventState);
617 free(szCurrentState);
621 log, g_LogLevel,
"\t[%" PRIu32
"]: cbAttr: %" PRIu32
" { %s }", index,
623 smartcard_array_dump(readerState->rgbAtr, readerState->cbAtr, buffer,
sizeof(buffer)));
631 wLog* log = WLog_Get(TAG);
632 if (!WLog_IsLevelActive(log, g_LogLevel))
635 WLog_Print(log, g_LogLevel,
"GetStatusChangeW_Call {");
636 smartcard_log_context(log, &call->handles.hContext);
638 WLog_Print(log, g_LogLevel,
"dwTimeOut: 0x%08" PRIX32
" cReaders: %" PRIu32
"", call->dwTimeOut,
641 dump_reader_states_w(log, call->rgReaderStates, call->cReaders);
643 WLog_Print(log, g_LogLevel,
"}");
651 wLog* log = WLog_Get(TAG);
652 if (!WLog_IsLevelActive(log, g_LogLevel))
655 char* mszA = smartcard_convert_string_list(ret->msz, ret->cBytes, unicode);
657 WLog_Print(log, g_LogLevel,
"ListReaderGroups%s_Return {", unicode ?
"W" :
"A");
658 WLog_Print(log, g_LogLevel,
" ReturnCode: %s (0x%08" PRIx32
")",
659 SCardGetErrorString(ret->ReturnCode), ret->ReturnCode);
660 WLog_Print(log, g_LogLevel,
" cBytes: %" PRIu32
" msz: %s", ret->cBytes, mszA);
661 WLog_Print(log, g_LogLevel,
"}");
665static void smartcard_trace_list_readers_call(
const ListReaders_Call* call, BOOL unicode)
669 wLog* log = WLog_Get(TAG);
670 if (!WLog_IsLevelActive(log, g_LogLevel))
673 char* mszGroupsA = smartcard_convert_string_list(call->mszGroups, call->cBytes, unicode);
675 WLog_Print(log, g_LogLevel,
"ListReaders%s_Call {", unicode ?
"W" :
"A");
676 smartcard_log_context(log, &call->handles.hContext);
678 WLog_Print(log, g_LogLevel,
679 "cBytes: %" PRIu32
" mszGroups: %s fmszReadersIsNULL: %" PRId32
680 " cchReaders: 0x%08" PRIX32
"",
681 call->cBytes, mszGroupsA, call->fmszReadersIsNULL, call->cchReaders);
682 WLog_Print(log, g_LogLevel,
"}");
691 wLog* log = WLog_Get(TAG);
692 if (!WLog_IsLevelActive(log, g_LogLevel))
695 WLog_Print(log, g_LogLevel,
"LocateCardsByATRA_Call {");
696 smartcard_log_context(log, &call->handles.hContext);
698 dump_reader_states_a(log, call->rgReaderStates, call->cReaders);
700 WLog_Print(log, g_LogLevel,
"}");
705 char buffer[8192] = { 0 };
709 wLog* log = WLog_Get(TAG);
710 if (!WLog_IsLevelActive(log, g_LogLevel))
713 WLog_Print(log, g_LogLevel,
"LocateCardsA_Call {");
714 smartcard_log_context(log, &call->handles.hContext);
715 WLog_Print(log, g_LogLevel,
" cBytes=%" PRId32, call->cBytes);
716 WLog_Print(log, g_LogLevel,
" mszCards=%s",
717 smartcard_msz_dump_a(call->mszCards, call->cBytes, buffer,
sizeof(buffer)));
718 WLog_Print(log, g_LogLevel,
" cReaders=%" PRId32, call->cReaders);
719 dump_reader_states_a(log, call->rgReaderStates, call->cReaders);
721 WLog_Print(log, g_LogLevel,
"}");
728 wLog* log = WLog_Get(TAG);
729 if (!WLog_IsLevelActive(log, g_LogLevel))
732 WLog_Print(log, g_LogLevel,
"LocateCards_Return {");
733 WLog_Print(log, g_LogLevel,
" ReturnCode: %s (0x%08" PRIX32
")",
734 SCardGetErrorString(ret->ReturnCode), ret->ReturnCode);
736 if (ret->ReturnCode == SCARD_S_SUCCESS)
738 WLog_Print(log, g_LogLevel,
" cReaders=%" PRId32, ret->cReaders);
740 WLog_Print(log, g_LogLevel,
"}");
747 wLog* log = WLog_Get(TAG);
748 if (!WLog_IsLevelActive(log, g_LogLevel))
751 WLog_Print(log, g_LogLevel,
"GetReaderIcon_Return {");
752 WLog_Print(log, g_LogLevel,
" ReturnCode: %s (0x%08" PRIX32
")",
753 SCardGetErrorString(ret->ReturnCode), ret->ReturnCode);
755 if (ret->ReturnCode == SCARD_S_SUCCESS)
757 WLog_Print(log, g_LogLevel,
" cbDataLen=%" PRId32, ret->cbDataLen);
759 WLog_Print(log, g_LogLevel,
"}");
765 wLog* log = WLog_Get(TAG);
766 if (!WLog_IsLevelActive(log, g_LogLevel))
769 WLog_Print(log, g_LogLevel,
"GetTransmitCount_Return {");
770 WLog_Print(log, g_LogLevel,
" ReturnCode: %s (0x%08" PRIX32
")",
771 SCardGetErrorString(ret->ReturnCode), ret->ReturnCode);
773 WLog_Print(log, g_LogLevel,
" cTransmitCount=%" PRIu32, ret->cTransmitCount);
774 WLog_Print(log, g_LogLevel,
"}");
780 wLog* log = WLog_Get(TAG);
781 if (!WLog_IsLevelActive(log, g_LogLevel))
784 WLog_Print(log, g_LogLevel,
"ReadCache_Return {");
785 WLog_Print(log, g_LogLevel,
" ReturnCode: %s (0x%08" PRIX32
")",
786 SCardGetErrorString(ret->ReturnCode), ret->ReturnCode);
788 if (ret->ReturnCode == SCARD_S_SUCCESS)
790 char buffer[1024] = { 0 };
791 WLog_Print(log, g_LogLevel,
" cbDataLen=%" PRId32, ret->cbDataLen);
792 WLog_Print(log, g_LogLevel,
" cbData: %s",
793 smartcard_array_dump(ret->pbData, ret->cbDataLen, buffer,
sizeof(buffer)));
795 WLog_Print(log, g_LogLevel,
"}");
801 char buffer[8192] = { 0 };
803 wLog* log = WLog_Get(TAG);
804 if (!WLog_IsLevelActive(log, g_LogLevel))
807 WLog_Print(log, g_LogLevel,
"LocateCardsW_Call {");
808 smartcard_log_context(log, &call->handles.hContext);
809 WLog_Print(log, g_LogLevel,
" cBytes=%" PRId32, call->cBytes);
810 WLog_Print(log, g_LogLevel,
" sz2=%s",
811 smartcard_msz_dump_w(call->mszCards, call->cBytes, buffer,
sizeof(buffer)));
812 WLog_Print(log, g_LogLevel,
" cReaders=%" PRId32, call->cReaders);
813 dump_reader_states_w(log, call->rgReaderStates, call->cReaders);
814 WLog_Print(log, g_LogLevel,
"}");
817static void smartcard_trace_list_readers_return(
const ListReaders_Return* ret, BOOL unicode)
821 wLog* log = WLog_Get(TAG);
822 if (!WLog_IsLevelActive(log, g_LogLevel))
825 WLog_Print(log, g_LogLevel,
"ListReaders%s_Return {", unicode ?
"W" :
"A");
826 WLog_Print(log, g_LogLevel,
" ReturnCode: %s (0x%08" PRIX32
")",
827 SCardGetErrorString(ret->ReturnCode), ret->ReturnCode);
829 if (ret->ReturnCode != SCARD_S_SUCCESS)
831 WLog_Print(log, g_LogLevel,
"}");
835 char* mszA = smartcard_convert_string_list(ret->msz, ret->cBytes, unicode);
837 WLog_Print(log, g_LogLevel,
" cBytes: %" PRIu32
" msz: %s", ret->cBytes, mszA);
838 WLog_Print(log, g_LogLevel,
"}");
846 wLog* log = WLog_Get(TAG);
847 if (!WLog_IsLevelActive(log, g_LogLevel))
850 WLog_Print(log, g_LogLevel,
"GetStatusChange%s_Return {", unicode ?
"W" :
"A");
851 WLog_Print(log, g_LogLevel,
" ReturnCode: %s (0x%08" PRIX32
")",
852 SCardGetErrorString(ret->ReturnCode), ret->ReturnCode);
853 WLog_Print(log, g_LogLevel,
" cReaders: %" PRIu32
"", ret->cReaders);
855 dump_reader_states_return(log, ret->rgReaderStates, ret->cReaders);
857 if (!ret->rgReaderStates && (ret->cReaders > 0))
859 WLog_Print(log, g_LogLevel,
" [INVALID STATE] rgReaderStates=NULL, cReaders=%" PRIu32,
862 else if (ret->ReturnCode != SCARD_S_SUCCESS)
864 WLog_Print(log, g_LogLevel,
" [INVALID RETURN] rgReaderStates, cReaders=%" PRIu32,
869 for (UINT32 index = 0; index < ret->cReaders; index++)
871 char buffer[1024] = { 0 };
873 char* szCurrentState = SCardGetReaderStateString(rgReaderState->dwCurrentState);
874 char* szEventState = SCardGetReaderStateString(rgReaderState->dwEventState);
875 WLog_Print(log, g_LogLevel,
" [%" PRIu32
"]: dwCurrentState: %s (0x%08" PRIX32
")",
876 index, szCurrentState, rgReaderState->dwCurrentState);
877 WLog_Print(log, g_LogLevel,
" [%" PRIu32
"]: dwEventState: %s (0x%08" PRIX32
")",
878 index, szEventState, rgReaderState->dwEventState);
879 WLog_Print(log, g_LogLevel,
" [%" PRIu32
"]: cbAtr: %" PRIu32
" rgbAtr: %s", index,
880 rgReaderState->cbAtr,
881 smartcard_array_dump(rgReaderState->rgbAtr, rgReaderState->cbAtr, buffer,
883 free(szCurrentState);
888 WLog_Print(log, g_LogLevel,
"}");
894 wLog* log = WLog_Get(TAG);
895 if (!WLog_IsLevelActive(log, g_LogLevel))
898 WLog_Print(log, g_LogLevel,
"ContextAndTwoStringW_Call {");
899 smartcard_log_context(log, &call->handles.hContext);
900 WLog_Print(log, g_LogLevel,
" sz1=%s", call->sz1);
901 WLog_Print(log, g_LogLevel,
" sz2=%s", call->sz2);
902 WLog_Print(log, g_LogLevel,
"}");
908 char sz1[1024] = { 0 };
909 char sz2[1024] = { 0 };
911 wLog* log = WLog_Get(TAG);
912 if (!WLog_IsLevelActive(log, g_LogLevel))
915 (void)ConvertWCharToUtf8(call->sz1, sz1, ARRAYSIZE(sz1));
917 (void)ConvertWCharToUtf8(call->sz2, sz2, ARRAYSIZE(sz2));
919 WLog_Print(log, g_LogLevel,
"ContextAndTwoStringW_Call {");
920 smartcard_log_context(log, &call->handles.hContext);
921 WLog_Print(log, g_LogLevel,
" sz1=%s", sz1);
922 WLog_Print(log, g_LogLevel,
" sz2=%s", sz2);
923 WLog_Print(log, g_LogLevel,
"}");
929 wLog* log = WLog_Get(TAG);
930 if (!WLog_IsLevelActive(log, g_LogLevel))
933 WLog_Print(log, g_LogLevel,
"GetTransmitCount_Call {");
934 smartcard_log_context(log, &call->handles.hContext);
935 smartcard_log_redir_handle(log, &call->handles.hCard);
937 WLog_Print(log, g_LogLevel,
"}");
943 char buffer[1024] = { 0 };
945 wLog* log = WLog_Get(TAG);
946 if (!WLog_IsLevelActive(log, g_LogLevel))
949 WLog_Print(log, g_LogLevel,
"WriteCacheA_Call {");
951 WLog_Print(log, g_LogLevel,
" szLookupName=%s", call->szLookupName);
953 smartcard_log_context(log, &call->Common.handles.hContext);
955 TAG,
"..CardIdentifier=%s",
956 smartcard_array_dump(call->Common.CardIdentifier,
sizeof(
UUID), buffer,
sizeof(buffer)));
957 WLog_Print(log, g_LogLevel,
" FreshnessCounter=%" PRIu32, call->Common.FreshnessCounter);
958 WLog_Print(log, g_LogLevel,
" cbDataLen=%" PRIu32, call->Common.cbDataLen);
961 smartcard_array_dump(call->Common.pbData, call->Common.cbDataLen, buffer,
sizeof(buffer)));
962 WLog_Print(log, g_LogLevel,
"}");
968 char tmp[1024] = { 0 };
969 char buffer[1024] = { 0 };
971 wLog* log = WLog_Get(TAG);
972 if (!WLog_IsLevelActive(log, g_LogLevel))
975 WLog_Print(log, g_LogLevel,
"WriteCacheW_Call {");
977 if (call->szLookupName)
978 (void)ConvertWCharToUtf8(call->szLookupName, tmp, ARRAYSIZE(tmp));
979 WLog_Print(log, g_LogLevel,
" szLookupName=%s", tmp);
981 smartcard_log_context(log, &call->Common.handles.hContext);
983 TAG,
"..CardIdentifier=%s",
984 smartcard_array_dump(call->Common.CardIdentifier,
sizeof(
UUID), buffer,
sizeof(buffer)));
985 WLog_Print(log, g_LogLevel,
" FreshnessCounter=%" PRIu32, call->Common.FreshnessCounter);
986 WLog_Print(log, g_LogLevel,
" cbDataLen=%" PRIu32, call->Common.cbDataLen);
989 smartcard_array_dump(call->Common.pbData, call->Common.cbDataLen, buffer,
sizeof(buffer)));
990 WLog_Print(log, g_LogLevel,
"}");
993static void smartcard_trace_read_cache_a_call(
const ReadCacheA_Call* call)
996 char buffer[1024] = { 0 };
998 wLog* log = WLog_Get(TAG);
999 if (!WLog_IsLevelActive(log, g_LogLevel))
1002 WLog_Print(log, g_LogLevel,
"ReadCacheA_Call {");
1004 WLog_Print(log, g_LogLevel,
" szLookupName=%s", call->szLookupName);
1005 smartcard_log_context(log, &call->Common.handles.hContext);
1007 TAG,
"..CardIdentifier=%s",
1008 smartcard_array_dump(call->Common.CardIdentifier,
sizeof(
UUID), buffer,
sizeof(buffer)));
1009 WLog_Print(log, g_LogLevel,
" FreshnessCounter=%" PRIu32, call->Common.FreshnessCounter);
1010 WLog_Print(log, g_LogLevel,
" fPbDataIsNULL=%" PRId32, call->Common.fPbDataIsNULL);
1011 WLog_Print(log, g_LogLevel,
" cbDataLen=%" PRIu32, call->Common.cbDataLen);
1013 WLog_Print(log, g_LogLevel,
"}");
1016static void smartcard_trace_read_cache_w_call(
const ReadCacheW_Call* call)
1019 char tmp[1024] = { 0 };
1020 char buffer[1024] = { 0 };
1022 wLog* log = WLog_Get(TAG);
1023 if (!WLog_IsLevelActive(log, g_LogLevel))
1026 WLog_Print(log, g_LogLevel,
"ReadCacheW_Call {");
1027 if (call->szLookupName)
1028 (void)ConvertWCharToUtf8(call->szLookupName, tmp, ARRAYSIZE(tmp));
1029 WLog_Print(log, g_LogLevel,
" szLookupName=%s", tmp);
1031 smartcard_log_context(log, &call->Common.handles.hContext);
1033 TAG,
"..CardIdentifier=%s",
1034 smartcard_array_dump(call->Common.CardIdentifier,
sizeof(
UUID), buffer,
sizeof(buffer)));
1035 WLog_Print(log, g_LogLevel,
" FreshnessCounter=%" PRIu32, call->Common.FreshnessCounter);
1036 WLog_Print(log, g_LogLevel,
" fPbDataIsNULL=%" PRId32, call->Common.fPbDataIsNULL);
1037 WLog_Print(log, g_LogLevel,
" cbDataLen=%" PRIu32, call->Common.cbDataLen);
1039 WLog_Print(log, g_LogLevel,
"}");
1042static void smartcard_trace_transmit_call(
const Transmit_Call* call)
1045 UINT32 cbExtraBytes = 0;
1046 BYTE* pbExtraBytes = NULL;
1048 wLog* log = WLog_Get(TAG);
1049 if (!WLog_IsLevelActive(log, g_LogLevel))
1052 WLog_Print(log, g_LogLevel,
"Transmit_Call {");
1053 smartcard_log_context(log, &call->handles.hContext);
1054 smartcard_log_redir_handle(log, &call->handles.hCard);
1056 if (call->pioSendPci)
1058 cbExtraBytes = (UINT32)(call->pioSendPci->cbPciLength -
sizeof(
SCARD_IO_REQUEST));
1060 WLog_Print(log, g_LogLevel,
"pioSendPci: dwProtocol: %" PRIu32
" cbExtraBytes: %" PRIu32
"",
1061 call->pioSendPci->dwProtocol, cbExtraBytes);
1065 char buffer[1024] = { 0 };
1066 WLog_Print(log, g_LogLevel,
"pbExtraBytes: %s",
1067 smartcard_array_dump(pbExtraBytes, cbExtraBytes, buffer,
sizeof(buffer)));
1072 WLog_Print(log, g_LogLevel,
"pioSendPci: null");
1075 WLog_Print(log, g_LogLevel,
"cbSendLength: %" PRIu32
"", call->cbSendLength);
1077 if (call->pbSendBuffer)
1079 char buffer[1024] = { 0 };
1081 TAG,
"pbSendBuffer: %s",
1082 smartcard_array_dump(call->pbSendBuffer, call->cbSendLength, buffer,
sizeof(buffer)));
1086 WLog_Print(log, g_LogLevel,
"pbSendBuffer: null");
1089 if (call->pioRecvPci)
1091 cbExtraBytes = (UINT32)(call->pioRecvPci->cbPciLength -
sizeof(
SCARD_IO_REQUEST));
1093 WLog_Print(log, g_LogLevel,
"pioRecvPci: dwProtocol: %" PRIu32
" cbExtraBytes: %" PRIu32
"",
1094 call->pioRecvPci->dwProtocol, cbExtraBytes);
1098 char buffer[1024] = { 0 };
1099 WLog_Print(log, g_LogLevel,
"pbExtraBytes: %s",
1100 smartcard_array_dump(pbExtraBytes, cbExtraBytes, buffer,
sizeof(buffer)));
1105 WLog_Print(log, g_LogLevel,
"pioRecvPci: null");
1108 WLog_Print(log, g_LogLevel,
"fpbRecvBufferIsNULL: %" PRId32
" cbRecvLength: %" PRIu32
"",
1109 call->fpbRecvBufferIsNULL, call->cbRecvLength);
1110 WLog_Print(log, g_LogLevel,
"}");
1116 wLog* log = WLog_Get(TAG);
1117 if (!WLog_IsLevelActive(log, g_LogLevel))
1120 WLog_Print(log, g_LogLevel,
"LocateCardsByATRW_Call {");
1121 smartcard_log_context(log, &call->handles.hContext);
1123 dump_reader_states_w(log, call->rgReaderStates, call->cReaders);
1125 WLog_Print(log, g_LogLevel,
"}");
1128static void smartcard_trace_transmit_return(
const Transmit_Return* ret)
1131 UINT32 cbExtraBytes = 0;
1132 BYTE* pbExtraBytes = NULL;
1134 wLog* log = WLog_Get(TAG);
1135 if (!WLog_IsLevelActive(log, g_LogLevel))
1138 WLog_Print(log, g_LogLevel,
"Transmit_Return {");
1139 WLog_Print(log, g_LogLevel,
" ReturnCode: %s (0x%08" PRIX32
")",
1140 SCardGetErrorString(ret->ReturnCode), ret->ReturnCode);
1142 if (ret->pioRecvPci)
1144 cbExtraBytes = (UINT32)(ret->pioRecvPci->cbPciLength -
sizeof(
SCARD_IO_REQUEST));
1146 WLog_Print(log, g_LogLevel,
1147 " pioRecvPci: dwProtocol: %" PRIu32
" cbExtraBytes: %" PRIu32
"",
1148 ret->pioRecvPci->dwProtocol, cbExtraBytes);
1152 char buffer[1024] = { 0 };
1153 WLog_Print(log, g_LogLevel,
" pbExtraBytes: %s",
1154 smartcard_array_dump(pbExtraBytes, cbExtraBytes, buffer,
sizeof(buffer)));
1159 WLog_Print(log, g_LogLevel,
" pioRecvPci: null");
1162 WLog_Print(log, g_LogLevel,
" cbRecvLength: %" PRIu32
"", ret->cbRecvLength);
1164 if (ret->pbRecvBuffer)
1166 char buffer[1024] = { 0 };
1168 TAG,
" pbRecvBuffer: %s",
1169 smartcard_array_dump(ret->pbRecvBuffer, ret->cbRecvLength, buffer,
sizeof(buffer)));
1173 WLog_Print(log, g_LogLevel,
" pbRecvBuffer: null");
1176 WLog_Print(log, g_LogLevel,
"}");
1179static void smartcard_trace_control_return(
const Control_Return* ret)
1182 wLog* log = WLog_Get(TAG);
1183 if (!WLog_IsLevelActive(log, g_LogLevel))
1186 WLog_Print(log, g_LogLevel,
"Control_Return {");
1187 WLog_Print(log, g_LogLevel,
" ReturnCode: %s (0x%08" PRIX32
")",
1188 SCardGetErrorString(ret->ReturnCode), ret->ReturnCode);
1189 WLog_Print(log, g_LogLevel,
" cbOutBufferSize: %" PRIu32
"", ret->cbOutBufferSize);
1191 if (ret->pvOutBuffer)
1193 char buffer[1024] = { 0 };
1195 TAG,
"pvOutBuffer: %s",
1196 smartcard_array_dump(ret->pvOutBuffer, ret->cbOutBufferSize, buffer,
sizeof(buffer)));
1200 WLog_Print(log, g_LogLevel,
"pvOutBuffer: null");
1203 WLog_Print(log, g_LogLevel,
"}");
1206static void smartcard_trace_control_call(
const Control_Call* call)
1209 wLog* log = WLog_Get(TAG);
1210 if (!WLog_IsLevelActive(log, g_LogLevel))
1213 WLog_Print(log, g_LogLevel,
"Control_Call {");
1214 smartcard_log_context(log, &call->handles.hContext);
1215 smartcard_log_redir_handle(log, &call->handles.hCard);
1217 WLog_Print(log, g_LogLevel,
1218 "dwControlCode: 0x%08" PRIX32
" cbInBufferSize: %" PRIu32
1219 " fpvOutBufferIsNULL: %" PRId32
" cbOutBufferSize: %" PRIu32
"",
1220 call->dwControlCode, call->cbInBufferSize, call->fpvOutBufferIsNULL,
1221 call->cbOutBufferSize);
1223 if (call->pvInBuffer)
1225 char buffer[1024] = { 0 };
1227 TAG,
"pbInBuffer: %s",
1228 smartcard_array_dump(call->pvInBuffer, call->cbInBufferSize, buffer,
sizeof(buffer)));
1232 WLog_Print(log, g_LogLevel,
"pvInBuffer: null");
1235 WLog_Print(log, g_LogLevel,
"}");
1238static void smartcard_trace_set_attrib_call(
const SetAttrib_Call* call)
1241 char buffer[8192] = { 0 };
1243 wLog* log = WLog_Get(TAG);
1244 if (!WLog_IsLevelActive(log, g_LogLevel))
1247 WLog_Print(log, g_LogLevel,
"GetAttrib_Call {");
1248 smartcard_log_context(log, &call->handles.hContext);
1249 smartcard_log_redir_handle(log, &call->handles.hCard);
1250 WLog_Print(log, g_LogLevel,
"dwAttrId: 0x%08" PRIX32, call->dwAttrId);
1251 WLog_Print(log, g_LogLevel,
"cbAttrLen: 0x%08" PRId32, call->cbAttrLen);
1252 WLog_Print(log, g_LogLevel,
"pbAttr: %s",
1253 smartcard_array_dump(call->pbAttr, call->cbAttrLen, buffer,
sizeof(buffer)));
1254 WLog_Print(log, g_LogLevel,
"}");
1257static void smartcard_trace_get_attrib_return(
const GetAttrib_Return* ret, DWORD dwAttrId)
1260 char buffer[1024] = { 0 };
1261 wLog* log = WLog_Get(TAG);
1262 if (!WLog_IsLevelActive(log, g_LogLevel))
1265 WLog_Print(log, g_LogLevel,
"GetAttrib_Return {");
1266 WLog_Print(log, g_LogLevel,
" ReturnCode: %s (0x%08" PRIX32
")",
1267 SCardGetErrorString(ret->ReturnCode), ret->ReturnCode);
1268 WLog_Print(log, g_LogLevel,
" dwAttrId: %s (0x%08" PRIX32
") cbAttrLen: 0x%08" PRIX32
"",
1269 SCardGetAttributeString(dwAttrId), dwAttrId, ret->cbAttrLen);
1270 WLog_Print(log, g_LogLevel,
" %s",
1271 smartcard_array_dump(ret->pbAttr, ret->cbAttrLen, buffer,
sizeof(buffer)));
1273 WLog_Print(log, g_LogLevel,
"}");
1276static void smartcard_trace_get_attrib_call(
const GetAttrib_Call* call)
1279 wLog* log = WLog_Get(TAG);
1280 if (!WLog_IsLevelActive(log, g_LogLevel))
1283 WLog_Print(log, g_LogLevel,
"GetAttrib_Call {");
1284 smartcard_log_context(log, &call->handles.hContext);
1285 smartcard_log_redir_handle(log, &call->handles.hCard);
1287 WLog_Print(log, g_LogLevel,
1288 "dwAttrId: %s (0x%08" PRIX32
") fpbAttrIsNULL: %" PRId32
" cbAttrLen: 0x%08" PRIX32
1290 SCardGetAttributeString(call->dwAttrId), call->dwAttrId, call->fpbAttrIsNULL,
1292 WLog_Print(log, g_LogLevel,
"}");
1295static void smartcard_trace_status_call(
const Status_Call* call, BOOL unicode)
1298 wLog* log = WLog_Get(TAG);
1299 if (!WLog_IsLevelActive(log, g_LogLevel))
1302 WLog_Print(log, g_LogLevel,
"Status%s_Call {", unicode ?
"W" :
"A");
1303 smartcard_log_context(log, &call->handles.hContext);
1304 smartcard_log_redir_handle(log, &call->handles.hCard);
1306 WLog_Print(log, g_LogLevel,
1307 "fmszReaderNamesIsNULL: %" PRId32
" cchReaderLen: %" PRIu32
" cbAtrLen: %" PRIu32
"",
1308 call->fmszReaderNamesIsNULL, call->cchReaderLen, call->cbAtrLen);
1309 WLog_Print(log, g_LogLevel,
"}");
1312static void smartcard_trace_status_return(
const Status_Return* ret, BOOL unicode)
1315 char* mszReaderNamesA = NULL;
1316 char buffer[1024] = { 0 };
1319 wLog* log = WLog_Get(TAG);
1320 if (!WLog_IsLevelActive(log, g_LogLevel))
1322 cBytes = ret->cBytes;
1323 if (ret->ReturnCode != SCARD_S_SUCCESS)
1325 if (cBytes == SCARD_AUTOALLOCATE)
1327 mszReaderNamesA = smartcard_convert_string_list(ret->mszReaderNames, cBytes, unicode);
1329 WLog_Print(log, g_LogLevel,
"Status%s_Return {", unicode ?
"W" :
"A");
1330 WLog_Print(log, g_LogLevel,
" ReturnCode: %s (0x%08" PRIX32
")",
1331 SCardGetErrorString(ret->ReturnCode), ret->ReturnCode);
1332 WLog_Print(log, g_LogLevel,
" dwState: %s (0x%08" PRIX32
") dwProtocol: %s (0x%08" PRIX32
")",
1333 SCardGetCardStateString(ret->dwState), ret->dwState,
1334 SCardGetProtocolString(ret->dwProtocol), ret->dwProtocol);
1336 WLog_Print(log, g_LogLevel,
" cBytes: %" PRIu32
" mszReaderNames: %s", ret->cBytes,
1339 WLog_Print(log, g_LogLevel,
" cbAtrLen: %" PRIu32
" pbAtr: %s", ret->cbAtrLen,
1340 smartcard_array_dump(ret->pbAtr, ret->cbAtrLen, buffer,
sizeof(buffer)));
1341 WLog_Print(log, g_LogLevel,
"}");
1342 free(mszReaderNamesA);
1345static void smartcard_trace_state_return(
const State_Return* ret)
1348 char buffer[1024] = { 0 };
1351 wLog* log = WLog_Get(TAG);
1352 if (!WLog_IsLevelActive(log, g_LogLevel))
1355 state = SCardGetReaderStateString(ret->dwState);
1356 WLog_Print(log, g_LogLevel,
"Reconnect_Return {");
1357 WLog_Print(log, g_LogLevel,
" ReturnCode: %s (0x%08" PRIX32
")",
1358 SCardGetErrorString(ret->ReturnCode), ret->ReturnCode);
1359 WLog_Print(log, g_LogLevel,
" dwState: %s (0x%08" PRIX32
")", state, ret->dwState);
1360 WLog_Print(log, g_LogLevel,
" dwProtocol: %s (0x%08" PRIX32
")",
1361 SCardGetProtocolString(ret->dwProtocol), ret->dwProtocol);
1362 WLog_Print(log, g_LogLevel,
" cbAtrLen: (0x%08" PRIX32
")", ret->cbAtrLen);
1363 WLog_Print(log, g_LogLevel,
" rgAtr: %s",
1364 smartcard_array_dump(ret->rgAtr,
sizeof(ret->rgAtr), buffer,
sizeof(buffer)));
1365 WLog_Print(log, g_LogLevel,
"}");
1372 wLog* log = WLog_Get(TAG);
1373 if (!WLog_IsLevelActive(log, g_LogLevel))
1376 WLog_Print(log, g_LogLevel,
"Reconnect_Return {");
1377 WLog_Print(log, g_LogLevel,
" ReturnCode: %s (0x%08" PRIX32
")",
1378 SCardGetErrorString(ret->ReturnCode), ret->ReturnCode);
1379 WLog_Print(log, g_LogLevel,
" dwActiveProtocol: %s (0x%08" PRIX32
")",
1380 SCardGetProtocolString(ret->dwActiveProtocol), ret->dwActiveProtocol);
1381 WLog_Print(log, g_LogLevel,
"}");
1384static void smartcard_trace_connect_a_call(
const ConnectA_Call* call)
1387 wLog* log = WLog_Get(TAG);
1388 if (!WLog_IsLevelActive(log, g_LogLevel))
1391 WLog_Print(log, g_LogLevel,
"ConnectA_Call {");
1392 smartcard_log_context(log, &call->Common.handles.hContext);
1394 WLog_Print(log, g_LogLevel,
1395 "szReader: %s dwShareMode: %s (0x%08" PRIX32
1396 ") dwPreferredProtocols: %s (0x%08" PRIX32
")",
1397 call->szReader, SCardGetShareModeString(call->Common.dwShareMode),
1398 call->Common.dwShareMode, SCardGetProtocolString(call->Common.dwPreferredProtocols),
1399 call->Common.dwPreferredProtocols);
1400 WLog_Print(log, g_LogLevel,
"}");
1403static void smartcard_trace_connect_w_call(
const ConnectW_Call* call)
1406 char szReaderA[1024] = { 0 };
1408 wLog* log = WLog_Get(TAG);
1409 if (!WLog_IsLevelActive(log, g_LogLevel))
1413 (void)ConvertWCharToUtf8(call->szReader, szReaderA, ARRAYSIZE(szReaderA));
1414 WLog_Print(log, g_LogLevel,
"ConnectW_Call {");
1415 smartcard_log_context(log, &call->Common.handles.hContext);
1417 WLog_Print(log, g_LogLevel,
1418 "szReader: %s dwShareMode: %s (0x%08" PRIX32
1419 ") dwPreferredProtocols: %s (0x%08" PRIX32
")",
1420 szReaderA, SCardGetShareModeString(call->Common.dwShareMode),
1421 call->Common.dwShareMode, SCardGetProtocolString(call->Common.dwPreferredProtocols),
1422 call->Common.dwPreferredProtocols);
1423 WLog_Print(log, g_LogLevel,
"}");
1430 wLog* log = WLog_Get(TAG);
1431 if (!WLog_IsLevelActive(log, g_LogLevel))
1434 WLog_Print(log, g_LogLevel,
"%s_Call {", name);
1435 smartcard_log_context(log, &call->handles.hContext);
1436 smartcard_log_redir_handle(log, &call->handles.hCard);
1438 WLog_Print(log, g_LogLevel,
"dwDisposition: %s (0x%08" PRIX32
")",
1439 SCardGetDispositionString(call->dwDisposition), call->dwDisposition);
1440 WLog_Print(log, g_LogLevel,
"}");
1446 wLog* log = WLog_Get(TAG);
1447 if (!WLog_IsLevelActive(log, g_LogLevel))
1450 WLog_Print(log, g_LogLevel,
"EstablishContext_Call {");
1451 WLog_Print(log, g_LogLevel,
"dwScope: %s (0x%08" PRIX32
")", SCardGetScopeString(call->dwScope),
1453 WLog_Print(log, g_LogLevel,
"}");
1459 wLog* log = WLog_Get(TAG);
1460 if (!WLog_IsLevelActive(log, g_LogLevel))
1463 WLog_Print(log, g_LogLevel,
"EstablishContext_Return {");
1464 WLog_Print(log, g_LogLevel,
" ReturnCode: %s (0x%08" PRIX32
")",
1465 SCardGetErrorString(ret->ReturnCode), ret->ReturnCode);
1466 smartcard_log_context(log, &ret->hContext);
1468 WLog_Print(log, g_LogLevel,
"}");
1471void smartcard_trace_long_return(
const Long_Return* ret,
const char* name)
1474 wLog* log = WLog_Get(TAG);
1475 if (!WLog_IsLevelActive(log, g_LogLevel))
1478 WLog_Print(log, g_LogLevel,
"%s_Return {", name);
1479 WLog_Print(log, g_LogLevel,
" ReturnCode: %s (0x%08" PRIX32
")",
1480 SCardGetErrorString(ret->ReturnCode), ret->ReturnCode);
1481 WLog_Print(log, g_LogLevel,
"}");
1484static void smartcard_trace_connect_return(
const Connect_Return* ret)
1487 wLog* log = WLog_Get(TAG);
1488 if (!WLog_IsLevelActive(log, g_LogLevel))
1491 WLog_Print(log, g_LogLevel,
"Connect_Return {");
1492 WLog_Print(log, g_LogLevel,
" ReturnCode: %s (0x%08" PRIX32
")",
1493 SCardGetErrorString(ret->ReturnCode), ret->ReturnCode);
1494 smartcard_log_context(log, &ret->hContext);
1495 smartcard_log_redir_handle(log, &ret->hCard);
1497 WLog_Print(log, g_LogLevel,
" dwActiveProtocol: %s (0x%08" PRIX32
")",
1498 SCardGetProtocolString(ret->dwActiveProtocol), ret->dwActiveProtocol);
1499 WLog_Print(log, g_LogLevel,
"}");
1502static void smartcard_trace_reconnect_call(
const Reconnect_Call* call)
1505 wLog* log = WLog_Get(TAG);
1506 if (!WLog_IsLevelActive(log, g_LogLevel))
1509 WLog_Print(log, g_LogLevel,
"Reconnect_Call {");
1510 smartcard_log_context(log, &call->handles.hContext);
1511 smartcard_log_redir_handle(log, &call->handles.hCard);
1513 WLog_Print(log, g_LogLevel,
1514 "dwShareMode: %s (0x%08" PRIX32
") dwPreferredProtocols: %s (0x%08" PRIX32
1515 ") dwInitialization: %s (0x%08" PRIX32
")",
1516 SCardGetShareModeString(call->dwShareMode), call->dwShareMode,
1517 SCardGetProtocolString(call->dwPreferredProtocols), call->dwPreferredProtocols,
1518 SCardGetDispositionString(call->dwInitialization), call->dwInitialization);
1519 WLog_Print(log, g_LogLevel,
"}");
1525 wLog* log = WLog_Get(TAG);
1526 if (!WLog_IsLevelActive(log, g_LogLevel))
1529 WLog_Print(log, g_LogLevel,
"GetDeviceTypeId_Return {");
1530 WLog_Print(log, g_LogLevel,
" ReturnCode: %s (0x%08" PRIX32
")",
1531 SCardGetErrorString(ret->ReturnCode), ret->ReturnCode);
1532 WLog_Print(log, g_LogLevel,
" dwDeviceId=%08" PRIx32, ret->dwDeviceId);
1534 WLog_Print(log, g_LogLevel,
"}");
1538 CHAR** pszReaderName)
1541 UINT32 pbContextNdrPtr = 0;
1542 LONG status = smartcard_unpack_redir_scard_context(s, phContext, &index, &pbContextNdrPtr);
1543 if (status != SCARD_S_SUCCESS)
1546 if (!smartcard_ndr_pointer_read(s, &index, NULL))
1547 return ERROR_INVALID_DATA;
1549 status = smartcard_unpack_redir_scard_context_ref(s, pbContextNdrPtr, phContext);
1550 if (status != SCARD_S_SUCCESS)
1553 status = smartcard_ndr_read_a(s, pszReaderName, NDR_PTR_FULL);
1554 if (status != SCARD_S_SUCCESS)
1557 smartcard_trace_context_and_string_call_a(__func__, phContext, *pszReaderName);
1558 return SCARD_S_SUCCESS;
1562 WCHAR** pszReaderName)
1565 UINT32 pbContextNdrPtr = 0;
1567 LONG status = smartcard_unpack_redir_scard_context(s, phContext, &index, &pbContextNdrPtr);
1568 if (status != SCARD_S_SUCCESS)
1571 if (!smartcard_ndr_pointer_read(s, &index, NULL))
1572 return ERROR_INVALID_DATA;
1574 status = smartcard_unpack_redir_scard_context_ref(s, pbContextNdrPtr, phContext);
1575 if (status != SCARD_S_SUCCESS)
1578 status = smartcard_ndr_read_w(s, pszReaderName, NDR_PTR_FULL);
1579 if (status != SCARD_S_SUCCESS)
1582 smartcard_trace_context_and_string_call_w(__func__, phContext, *pszReaderName);
1583 return SCARD_S_SUCCESS;
1586LONG smartcard_unpack_common_type_header(
wStream* s)
1590 UINT8 endianness = 0;
1591 UINT16 commonHeaderLength = 0;
1593 if (!Stream_CheckAndLogRequiredLength(TAG, s, 8))
1594 return STATUS_BUFFER_TOO_SMALL;
1597 Stream_Read_UINT8(s, version);
1598 Stream_Read_UINT8(s, endianness);
1599 Stream_Read_UINT16(s, commonHeaderLength);
1600 Stream_Read_UINT32(s, filler);
1604 WLog_WARN(TAG,
"Unsupported CommonTypeHeader Version %" PRIu8
"", version);
1605 return STATUS_INVALID_PARAMETER;
1608 if (endianness != 0x10)
1610 WLog_WARN(TAG,
"Unsupported CommonTypeHeader Endianness %" PRIu8
"", endianness);
1611 return STATUS_INVALID_PARAMETER;
1614 if (commonHeaderLength != 8)
1616 WLog_WARN(TAG,
"Unsupported CommonTypeHeader CommonHeaderLength %" PRIu16
"",
1617 commonHeaderLength);
1618 return STATUS_INVALID_PARAMETER;
1621 if (filler != 0xCCCCCCCC)
1623 WLog_WARN(TAG,
"Unexpected CommonTypeHeader Filler 0x%08" PRIX32
"", filler);
1624 return STATUS_INVALID_PARAMETER;
1627 return SCARD_S_SUCCESS;
1630void smartcard_pack_common_type_header(
wStream* s)
1632 Stream_Write_UINT8(s, 1);
1633 Stream_Write_UINT8(s, 0x10);
1634 Stream_Write_UINT16(s, 8);
1635 Stream_Write_UINT32(s, 0xCCCCCCCC);
1638LONG smartcard_unpack_private_type_header(
wStream* s)
1641 UINT32 objectBufferLength = 0;
1643 if (!Stream_CheckAndLogRequiredLength(TAG, s, 8))
1644 return STATUS_BUFFER_TOO_SMALL;
1646 Stream_Read_UINT32(s, objectBufferLength);
1647 Stream_Read_UINT32(s, filler);
1649 if (filler != 0x00000000)
1651 WLog_WARN(TAG,
"Unexpected PrivateTypeHeader Filler 0x%08" PRIX32
"", filler);
1652 return STATUS_INVALID_PARAMETER;
1655 if (!Stream_CheckAndLogRequiredLength(TAG, s, objectBufferLength))
1656 return STATUS_INVALID_PARAMETER;
1658 return SCARD_S_SUCCESS;
1661void smartcard_pack_private_type_header(
wStream* s, UINT32 objectBufferLength)
1663 Stream_Write_UINT32(s, objectBufferLength);
1664 Stream_Write_UINT32(s, 0x00000000);
1667LONG smartcard_unpack_read_size_align(
wStream* s,
size_t size, UINT32 alignment)
1672 size = (size + alignment - 1) & ~(alignment - 1);
1676 Stream_Seek(s, pad);
1681LONG smartcard_pack_write_size_align(
wStream* s,
size_t size, UINT32 alignment)
1686 size = (size + alignment - 1) & ~(alignment - 1);
1691 if (!Stream_EnsureRemainingCapacity(s, pad))
1693 WLog_ERR(TAG,
"Stream_EnsureRemainingCapacity failed!");
1694 return SCARD_F_INTERNAL_ERROR;
1697 Stream_Zero(s, pad);
1700 return SCARD_S_SUCCESS;
1705 SCARDCONTEXT hContext = { 0 };
1707 WINPR_ASSERT(context);
1708 if ((context->cbContext !=
sizeof(ULONG_PTR)) && (context->cbContext != 0))
1711 "REDIR_SCARDCONTEXT does not match native size: Actual: %" PRIu32
1712 ", Expected: %" PRIuz
"",
1713 context->cbContext,
sizeof(ULONG_PTR));
1717 if (context->cbContext)
1718 CopyMemory(&hContext, &(context->pbContext), context->cbContext);
1723void smartcard_scard_context_native_to_redir(
REDIR_SCARDCONTEXT* context, SCARDCONTEXT hContext)
1725 WINPR_ASSERT(context);
1727 context->cbContext =
sizeof(ULONG_PTR);
1728 CopyMemory(&(context->pbContext), &hContext, context->cbContext);
1733 SCARDHANDLE hCard = 0;
1735 WINPR_ASSERT(handle);
1736 if (handle->cbHandle == 0)
1739 if (handle->cbHandle !=
sizeof(ULONG_PTR))
1742 "REDIR_SCARDHANDLE does not match native size: Actual: %" PRIu32
1743 ", Expected: %" PRIuz
"",
1744 handle->cbHandle,
sizeof(ULONG_PTR));
1748 if (handle->cbHandle)
1749 CopyMemory(&hCard, &(handle->pbHandle), handle->cbHandle);
1754void smartcard_scard_handle_native_to_redir(
REDIR_SCARDHANDLE* handle, SCARDHANDLE hCard)
1756 WINPR_ASSERT(handle);
1758 handle->cbHandle =
sizeof(ULONG_PTR);
1759 CopyMemory(&(handle->pbHandle), &hCard, handle->cbHandle);
1763 UINT32* ppbContextNdrPtr,
const char* file,
1764 const char* function,
int line)
1766 UINT32 pbContextNdrPtr = 0;
1769 WINPR_ASSERT(context);
1773 if (!Stream_CheckAndLogRequiredLength(TAG, s, 4))
1774 return STATUS_BUFFER_TOO_SMALL;
1776 Stream_Read_UINT32(s, context->cbContext);
1778 if (!Stream_CheckAndLogRequiredLength(TAG, s, context->cbContext))
1779 return STATUS_BUFFER_TOO_SMALL;
1781 if ((context->cbContext != 0) && (context->cbContext != 4) && (context->cbContext != 8))
1783 WLog_WARN(TAG,
"REDIR_SCARDCONTEXT length is not 0, 4 or 8: %" PRIu32
"",
1784 context->cbContext);
1785 return STATUS_INVALID_PARAMETER;
1788 if (!smartcard_ndr_pointer_read_(s, index, &pbContextNdrPtr, file, function,
1789 WINPR_ASSERTING_INT_CAST(
size_t, line)))
1790 return ERROR_INVALID_DATA;
1792 if (((context->cbContext == 0) && pbContextNdrPtr) ||
1793 ((context->cbContext != 0) && !pbContextNdrPtr))
1796 "REDIR_SCARDCONTEXT cbContext (%" PRIu32
") pbContextNdrPtr (%" PRIu32
1798 context->cbContext, pbContextNdrPtr);
1799 return STATUS_INVALID_PARAMETER;
1802 if (!Stream_CheckAndLogRequiredLength(TAG, s, context->cbContext))
1803 return STATUS_INVALID_PARAMETER;
1805 *ppbContextNdrPtr = pbContextNdrPtr;
1806 return SCARD_S_SUCCESS;
1811 const UINT32 pbContextNdrPtr = 0x00020000 + *index * 4;
1813 WINPR_ASSERT(context);
1814 if (context->cbContext != 0)
1816 Stream_Write_UINT32(s, context->cbContext);
1817 Stream_Write_UINT32(s, pbContextNdrPtr);
1818 *index = *index + 1;
1823 return SCARD_S_SUCCESS;
1826LONG smartcard_unpack_redir_scard_context_ref(
wStream* s, WINPR_ATTR_UNUSED UINT32 pbContextNdrPtr,
1831 WINPR_ASSERT(context);
1832 if (context->cbContext == 0)
1833 return SCARD_S_SUCCESS;
1835 if (!Stream_CheckAndLogRequiredLength(TAG, s, 4))
1836 return STATUS_BUFFER_TOO_SMALL;
1838 Stream_Read_UINT32(s, length);
1840 if (length != context->cbContext)
1842 WLog_WARN(TAG,
"REDIR_SCARDCONTEXT length (%" PRIu32
") cbContext (%" PRIu32
") mismatch",
1843 length, context->cbContext);
1844 return STATUS_INVALID_PARAMETER;
1847 if ((context->cbContext != 0) && (context->cbContext != 4) && (context->cbContext != 8))
1849 WLog_WARN(TAG,
"REDIR_SCARDCONTEXT length is not 4 or 8: %" PRIu32
"", context->cbContext);
1850 return STATUS_INVALID_PARAMETER;
1853 if (!Stream_CheckAndLogRequiredLength(TAG, s, context->cbContext))
1854 return STATUS_BUFFER_TOO_SMALL;
1856 if (context->cbContext)
1857 Stream_Read(s, &(context->pbContext), context->cbContext);
1859 ZeroMemory(&(context->pbContext),
sizeof(context->pbContext));
1861 return SCARD_S_SUCCESS;
1866 WINPR_ASSERT(context);
1867 Stream_Write_UINT32(s, context->cbContext);
1869 if (context->cbContext)
1871 Stream_Write(s, &(context->pbContext), context->cbContext);
1874 return SCARD_S_SUCCESS;
1878 const char* file,
const char* function,
int line)
1880 WINPR_ASSERT(handle);
1883 if (!Stream_CheckAndLogRequiredLength(TAG, s, 4))
1884 return STATUS_BUFFER_TOO_SMALL;
1886 Stream_Read_UINT32(s, handle->cbHandle);
1888 if (!Stream_CheckAndLogRequiredLength(TAG, s, handle->cbHandle))
1889 return STATUS_BUFFER_TOO_SMALL;
1891 if (!smartcard_ndr_pointer_read_(s, index, NULL, file, function,
1892 WINPR_ASSERTING_INT_CAST(
size_t, line)))
1893 return ERROR_INVALID_DATA;
1895 return SCARD_S_SUCCESS;
1900 const UINT32 pbContextNdrPtr = 0x00020000 + *index * 4;
1902 WINPR_ASSERT(handle);
1903 if (handle->cbHandle != 0)
1905 Stream_Write_UINT32(s, handle->cbHandle);
1906 Stream_Write_UINT32(s, pbContextNdrPtr);
1907 *index = *index + 1;
1911 return SCARD_S_SUCCESS;
1918 WINPR_ASSERT(handle);
1919 if (!Stream_CheckAndLogRequiredLength(TAG, s, 4))
1920 return STATUS_BUFFER_TOO_SMALL;
1922 Stream_Read_UINT32(s, length);
1924 if (length != handle->cbHandle)
1926 WLog_WARN(TAG,
"REDIR_SCARDHANDLE length (%" PRIu32
") cbHandle (%" PRIu32
") mismatch",
1927 length, handle->cbHandle);
1928 return STATUS_INVALID_PARAMETER;
1931 if ((handle->cbHandle != 4) && (handle->cbHandle != 8))
1933 WLog_WARN(TAG,
"REDIR_SCARDHANDLE length is not 4 or 8: %" PRIu32
"", handle->cbHandle);
1934 return STATUS_INVALID_PARAMETER;
1937 if (!Stream_CheckAndLogRequiredLength(TAG, s, handle->cbHandle))
1938 return STATUS_BUFFER_TOO_SMALL;
1940 if (handle->cbHandle)
1941 Stream_Read(s, &(handle->pbHandle), handle->cbHandle);
1943 return SCARD_S_SUCCESS;
1948 WINPR_ASSERT(handle);
1949 Stream_Write_UINT32(s, handle->cbHandle);
1951 if (handle->cbHandle)
1952 Stream_Write(s, &(handle->pbHandle), handle->cbHandle);
1954 return SCARD_S_SUCCESS;
1960 if (!Stream_CheckAndLogRequiredLength(TAG, s, 4))
1961 return STATUS_BUFFER_TOO_SMALL;
1963 Stream_Read_UINT32(s, call->dwScope);
1964 smartcard_trace_establish_context_call(call);
1965 return SCARD_S_SUCCESS;
1974 smartcard_trace_establish_context_return(ret);
1975 if (ret->ReturnCode != SCARD_S_SUCCESS)
1976 return ret->ReturnCode;
1978 if ((status = smartcard_pack_redir_scard_context(s, &(ret->hContext), &index)))
1981 return smartcard_pack_redir_scard_context_ref(s, &(ret->hContext));
1987 UINT32 pbContextNdrPtr = 0;
1990 LONG status = smartcard_unpack_redir_scard_context(s, &(call->handles.hContext), &index,
1992 if (status != SCARD_S_SUCCESS)
1995 if ((status = smartcard_unpack_redir_scard_context_ref(s, pbContextNdrPtr,
1996 &(call->handles.hContext))))
1997 WLog_ERR(TAG,
"smartcard_unpack_redir_scard_context_ref failed with error %" PRId32
"",
2000 smartcard_trace_context_call(call, name);
2007 UINT32 pbContextNdrPtr = 0;
2010 LONG status = smartcard_unpack_redir_scard_context(s, &(call->handles.hContext), &index,
2013 if (status != SCARD_S_SUCCESS)
2016 if (!Stream_CheckAndLogRequiredLength(TAG, s, 8))
2017 return STATUS_BUFFER_TOO_SMALL;
2019 Stream_Read_INT32(s, call->fmszGroupsIsNULL);
2020 Stream_Read_UINT32(s, call->cchGroups);
2022 smartcard_unpack_redir_scard_context_ref(s, pbContextNdrPtr, &(call->handles.hContext));
2024 if (status != SCARD_S_SUCCESS)
2027 smartcard_trace_list_reader_groups_call(call, unicode);
2028 return SCARD_S_SUCCESS;
2036 DWORD cBytes = ret->cBytes;
2039 smartcard_trace_list_reader_groups_return(ret, unicode);
2040 if (ret->ReturnCode != SCARD_S_SUCCESS)
2042 if (cBytes == SCARD_AUTOALLOCATE)
2045 if (!Stream_EnsureRemainingCapacity(s, 4))
2046 return SCARD_E_NO_MEMORY;
2048 Stream_Write_UINT32(s, cBytes);
2049 if (!smartcard_ndr_pointer_write(s, &index, cBytes))
2050 return SCARD_E_NO_MEMORY;
2052 status = smartcard_ndr_write(s, ret->msz, cBytes, 1, NDR_PTR_SIMPLE);
2053 if (status != SCARD_S_SUCCESS)
2055 return ret->ReturnCode;
2061 UINT32 mszGroupsNdrPtr = 0;
2062 UINT32 pbContextNdrPtr = 0;
2065 call->mszGroups = NULL;
2067 LONG status = smartcard_unpack_redir_scard_context(s, &(call->handles.hContext), &index,
2069 if (status != SCARD_S_SUCCESS)
2072 if (!Stream_CheckAndLogRequiredLength(TAG, s, 16))
2073 return STATUS_BUFFER_TOO_SMALL;
2075 Stream_Read_UINT32(s, call->cBytes);
2076 if (!smartcard_ndr_pointer_read(s, &index, &mszGroupsNdrPtr))
2077 return ERROR_INVALID_DATA;
2078 Stream_Read_INT32(s, call->fmszReadersIsNULL);
2079 Stream_Read_UINT32(s, call->cchReaders);
2081 if ((status = smartcard_unpack_redir_scard_context_ref(s, pbContextNdrPtr,
2082 &(call->handles.hContext))))
2085 if (mszGroupsNdrPtr)
2087 status = smartcard_ndr_read(s, &call->mszGroups, call->cBytes, 1, NDR_PTR_SIMPLE);
2088 if (status != SCARD_S_SUCCESS)
2092 smartcard_trace_list_readers_call(call, unicode);
2093 return SCARD_S_SUCCESS;
2101 UINT32 size = ret->cBytes;
2103 smartcard_trace_list_readers_return(ret, unicode);
2104 if (ret->ReturnCode != SCARD_S_SUCCESS)
2107 if (!Stream_EnsureRemainingCapacity(s, 4))
2109 WLog_ERR(TAG,
"Stream_EnsureRemainingCapacity failed!");
2110 return SCARD_F_INTERNAL_ERROR;
2113 Stream_Write_UINT32(s, size);
2114 if (!smartcard_ndr_pointer_write(s, &index, size))
2115 return SCARD_E_NO_MEMORY;
2117 status = smartcard_ndr_write(s, ret->msz, size, 1, NDR_PTR_SIMPLE);
2118 if (status != SCARD_S_SUCCESS)
2120 return ret->ReturnCode;
2124 UINT32* ppbContextNdrPtr)
2126 WINPR_ASSERT(common);
2127 LONG status = smartcard_unpack_redir_scard_context(s, &(common->handles.hContext), index,
2129 if (status != SCARD_S_SUCCESS)
2132 if (!Stream_CheckAndLogRequiredLength(TAG, s, 8))
2133 return STATUS_BUFFER_TOO_SMALL;
2135 Stream_Read_UINT32(s, common->dwShareMode);
2136 Stream_Read_UINT32(s, common->dwPreferredProtocols);
2137 return SCARD_S_SUCCESS;
2144 UINT32 pbContextNdrPtr = 0;
2147 call->szReader = NULL;
2149 if (!smartcard_ndr_pointer_read(s, &index, NULL))
2150 return ERROR_INVALID_DATA;
2152 if ((status = smartcard_unpack_connect_common(s, &(call->Common), &index, &pbContextNdrPtr)))
2154 WLog_ERR(TAG,
"smartcard_unpack_connect_common failed with error %" PRId32
"", status);
2158 status = smartcard_ndr_read_a(s, &call->szReader, NDR_PTR_FULL);
2159 if (status != SCARD_S_SUCCESS)
2162 if ((status = smartcard_unpack_redir_scard_context_ref(s, pbContextNdrPtr,
2163 &(call->Common.handles.hContext))))
2164 WLog_ERR(TAG,
"smartcard_unpack_redir_scard_context_ref failed with error %" PRId32
"",
2167 smartcard_trace_connect_a_call(call);
2175 UINT32 pbContextNdrPtr = 0;
2178 call->szReader = NULL;
2180 if (!smartcard_ndr_pointer_read(s, &index, NULL))
2181 return ERROR_INVALID_DATA;
2183 if ((status = smartcard_unpack_connect_common(s, &(call->Common), &index, &pbContextNdrPtr)))
2185 WLog_ERR(TAG,
"smartcard_unpack_connect_common failed with error %" PRId32
"", status);
2189 status = smartcard_ndr_read_w(s, &call->szReader, NDR_PTR_FULL);
2190 if (status != SCARD_S_SUCCESS)
2193 if ((status = smartcard_unpack_redir_scard_context_ref(s, pbContextNdrPtr,
2194 &(call->Common.handles.hContext))))
2195 WLog_ERR(TAG,
"smartcard_unpack_redir_scard_context_ref failed with error %" PRId32
"",
2198 smartcard_trace_connect_w_call(call);
2208 smartcard_trace_connect_return(ret);
2210 status = smartcard_pack_redir_scard_context(s, &ret->hContext, &index);
2211 if (status != SCARD_S_SUCCESS)
2214 status = smartcard_pack_redir_scard_handle(s, &ret->hCard, &index);
2215 if (status != SCARD_S_SUCCESS)
2218 if (!Stream_EnsureRemainingCapacity(s, 4))
2219 return SCARD_E_NO_MEMORY;
2221 Stream_Write_UINT32(s, ret->dwActiveProtocol);
2222 status = smartcard_pack_redir_scard_context_ref(s, &ret->hContext);
2223 if (status != SCARD_S_SUCCESS)
2225 return smartcard_pack_redir_scard_handle_ref(s, &(ret->hCard));
2231 UINT32 pbContextNdrPtr = 0;
2234 LONG status = smartcard_unpack_redir_scard_context(s, &(call->handles.hContext), &index,
2236 if (status != SCARD_S_SUCCESS)
2239 status = smartcard_unpack_redir_scard_handle(s, &(call->handles.hCard), &index);
2240 if (status != SCARD_S_SUCCESS)
2243 if (!Stream_CheckAndLogRequiredLength(TAG, s, 12))
2244 return STATUS_BUFFER_TOO_SMALL;
2246 Stream_Read_UINT32(s, call->dwShareMode);
2247 Stream_Read_UINT32(s, call->dwPreferredProtocols);
2248 Stream_Read_UINT32(s, call->dwInitialization);
2250 if ((status = smartcard_unpack_redir_scard_context_ref(s, pbContextNdrPtr,
2251 &(call->handles.hContext))))
2253 WLog_ERR(TAG,
"smartcard_unpack_redir_scard_context_ref failed with error %" PRId32
"",
2258 if ((status = smartcard_unpack_redir_scard_handle_ref(s, &(call->handles.hCard))))
2259 WLog_ERR(TAG,
"smartcard_unpack_redir_scard_handle_ref failed with error %" PRId32
"",
2262 smartcard_trace_reconnect_call(call);
2269 smartcard_trace_reconnect_return(ret);
2271 if (!Stream_EnsureRemainingCapacity(s, 4))
2272 return SCARD_E_NO_MEMORY;
2273 Stream_Write_UINT32(s, ret->dwActiveProtocol);
2274 return ret->ReturnCode;
2281 UINT32 pbContextNdrPtr = 0;
2284 LONG status = smartcard_unpack_redir_scard_context(s, &(call->handles.hContext), &index,
2286 if (status != SCARD_S_SUCCESS)
2289 status = smartcard_unpack_redir_scard_handle(s, &(call->handles.hCard), &index);
2290 if (status != SCARD_S_SUCCESS)
2293 if (!Stream_CheckAndLogRequiredLength(TAG, s, 4))
2294 return STATUS_BUFFER_TOO_SMALL;
2296 Stream_Read_UINT32(s, call->dwDisposition);
2298 if ((status = smartcard_unpack_redir_scard_context_ref(s, pbContextNdrPtr,
2299 &(call->handles.hContext))))
2302 if ((status = smartcard_unpack_redir_scard_handle_ref(s, &(call->handles.hCard))))
2305 smartcard_trace_hcard_and_disposition_call(call, name);
2312 wLog* log = WLog_Get(TAG);
2313 if (!WLog_IsLevelActive(log, g_LogLevel))
2316 WLog_Print(log, g_LogLevel,
"GetStatusChangeA_Call {");
2317 smartcard_log_context(log, &call->handles.hContext);
2319 WLog_Print(log, g_LogLevel,
"dwTimeOut: 0x%08" PRIX32
" cReaders: %" PRIu32
"", call->dwTimeOut,
2322 dump_reader_states_a(log, call->rgReaderStates, call->cReaders);
2324 WLog_Print(log, g_LogLevel,
"}");
2328 UINT32 cReaders, UINT32* ptrIndex)
2330 LONG status = SCARD_E_NO_MEMORY;
2332 WINPR_ASSERT(ppcReaders || (cReaders == 0));
2333 if (!Stream_CheckAndLogRequiredLength(TAG, s, 4))
2336 const UINT32 len = Stream_Get_UINT32(s);
2337 if (len != cReaders)
2339 WLog_ERR(TAG,
"Count mismatch when reading LPSCARD_READERSTATEA");
2345 BOOL* states = calloc(cReaders,
sizeof(BOOL));
2346 if (!rgReaderStates || !states)
2348 status = ERROR_INVALID_DATA;
2350 for (UINT32 index = 0; index < cReaders; index++)
2352 UINT32 ptr = UINT32_MAX;
2355 if (!Stream_CheckAndLogRequiredLength(TAG, s, 52))
2358 if (!smartcard_ndr_pointer_read(s, ptrIndex, &ptr))
2364 states[index] = ptr != 0;
2365 Stream_Read_UINT32(s, readerState->dwCurrentState);
2366 Stream_Read_UINT32(s, readerState->dwEventState);
2367 Stream_Read_UINT32(s, readerState->cbAtr);
2368 Stream_Read(s, readerState->rgbAtr, 36);
2371 for (UINT32 index = 0; index < cReaders; index++)
2378 status = smartcard_ndr_read_a(s, &readerState->szReader, NDR_PTR_FULL);
2379 if (status != SCARD_S_SUCCESS)
2383 *ppcReaders = rgReaderStates;
2385 return SCARD_S_SUCCESS;
2389 for (UINT32 index = 0; index < cReaders; index++)
2392 free(readerState->szReader);
2395 free(rgReaderStates);
2401 UINT32 cReaders, UINT32* ptrIndex)
2403 LONG status = SCARD_E_NO_MEMORY;
2405 WINPR_ASSERT(ppcReaders || (cReaders == 0));
2406 if (!Stream_CheckAndLogRequiredLength(TAG, s, 4))
2409 const UINT32 len = Stream_Get_UINT32(s);
2410 if (len != cReaders)
2412 WLog_ERR(TAG,
"Count mismatch when reading LPSCARD_READERSTATEW");
2418 BOOL* states = calloc(cReaders,
sizeof(BOOL));
2420 if (!rgReaderStates || !states)
2423 status = ERROR_INVALID_DATA;
2424 for (UINT32 index = 0; index < cReaders; index++)
2426 UINT32 ptr = UINT32_MAX;
2429 if (!Stream_CheckAndLogRequiredLength(TAG, s, 52))
2432 if (!smartcard_ndr_pointer_read(s, ptrIndex, &ptr))
2438 states[index] = ptr != 0;
2439 Stream_Read_UINT32(s, readerState->dwCurrentState);
2440 Stream_Read_UINT32(s, readerState->dwEventState);
2441 Stream_Read_UINT32(s, readerState->cbAtr);
2442 Stream_Read(s, readerState->rgbAtr, 36);
2445 for (UINT32 index = 0; index < cReaders; index++)
2453 status = smartcard_ndr_read_w(s, &readerState->szReader, NDR_PTR_FULL);
2454 if (status != SCARD_S_SUCCESS)
2458 *ppcReaders = rgReaderStates;
2460 return SCARD_S_SUCCESS;
2464 for (UINT32 index = 0; index < cReaders; index++)
2467 free(readerState->szReader);
2470 free(rgReaderStates);
2483 UINT32 pbContextNdrPtr = 0;
2486 call->rgReaderStates = NULL;
2488 LONG status = smartcard_unpack_redir_scard_context(s, &(call->handles.hContext), &index,
2490 if (status != SCARD_S_SUCCESS)
2493 if (!Stream_CheckAndLogRequiredLength(TAG, s, 12))
2494 return STATUS_BUFFER_TOO_SMALL;
2496 Stream_Read_UINT32(s, call->dwTimeOut);
2497 Stream_Read_UINT32(s, call->cReaders);
2498 if (!smartcard_ndr_pointer_read(s, &index, &ndrPtr))
2499 return ERROR_INVALID_DATA;
2501 if ((status = smartcard_unpack_redir_scard_context_ref(s, pbContextNdrPtr,
2502 &(call->handles.hContext))))
2507 status = smartcard_unpack_reader_state_a(s, &call->rgReaderStates, call->cReaders, &index);
2508 if (status != SCARD_S_SUCCESS)
2512 smartcard_trace_get_status_change_a_call(call);
2513 return SCARD_S_SUCCESS;
2520 UINT32 pbContextNdrPtr = 0;
2523 call->rgReaderStates = NULL;
2525 LONG status = smartcard_unpack_redir_scard_context(s, &(call->handles.hContext), &index,
2527 if (status != SCARD_S_SUCCESS)
2530 if (!Stream_CheckAndLogRequiredLength(TAG, s, 12))
2531 return STATUS_BUFFER_TOO_SMALL;
2533 Stream_Read_UINT32(s, call->dwTimeOut);
2534 Stream_Read_UINT32(s, call->cReaders);
2535 if (!smartcard_ndr_pointer_read(s, &index, &ndrPtr))
2536 return ERROR_INVALID_DATA;
2538 if ((status = smartcard_unpack_redir_scard_context_ref(s, pbContextNdrPtr,
2539 &(call->handles.hContext))))
2544 status = smartcard_unpack_reader_state_w(s, &call->rgReaderStates, call->cReaders, &index);
2545 if (status != SCARD_S_SUCCESS)
2549 smartcard_trace_get_status_change_w_call(call);
2550 return SCARD_S_SUCCESS;
2559 DWORD cReaders = ret->cReaders;
2562 smartcard_trace_get_status_change_return(ret, unicode);
2563 if (ret->ReturnCode != SCARD_S_SUCCESS)
2565 if (cReaders == SCARD_AUTOALLOCATE)
2568 if (!Stream_EnsureRemainingCapacity(s, 4))
2569 return SCARD_E_NO_MEMORY;
2571 Stream_Write_UINT32(s, cReaders);
2572 if (!smartcard_ndr_pointer_write(s, &index, cReaders))
2573 return SCARD_E_NO_MEMORY;
2574 status = smartcard_ndr_write_state(s, ret->rgReaderStates, cReaders, NDR_PTR_SIMPLE);
2575 if (status != SCARD_S_SUCCESS)
2577 return ret->ReturnCode;
2583 UINT32 pbContextNdrPtr = 0;
2586 LONG status = smartcard_unpack_redir_scard_context(s, &(call->handles.hContext), &index,
2588 if (status != SCARD_S_SUCCESS)
2591 status = smartcard_unpack_redir_scard_handle(s, &(call->handles.hCard), &index);
2592 if (status != SCARD_S_SUCCESS)
2595 if (!Stream_CheckAndLogRequiredLength(TAG, s, 8))
2596 return STATUS_BUFFER_TOO_SMALL;
2598 Stream_Read_INT32(s, call->fpbAtrIsNULL);
2599 Stream_Read_UINT32(s, call->cbAtrLen);
2601 if ((status = smartcard_unpack_redir_scard_context_ref(s, pbContextNdrPtr,
2602 &(call->handles.hContext))))
2605 if ((status = smartcard_unpack_redir_scard_handle_ref(s, &(call->handles.hCard))))
2615 DWORD cbAtrLen = ret->cbAtrLen;
2618 smartcard_trace_state_return(ret);
2619 if (ret->ReturnCode != SCARD_S_SUCCESS)
2621 if (cbAtrLen == SCARD_AUTOALLOCATE)
2624 Stream_Write_UINT32(s, ret->dwState);
2625 Stream_Write_UINT32(s, ret->dwProtocol);
2626 Stream_Write_UINT32(s, cbAtrLen);
2627 if (!smartcard_ndr_pointer_write(s, &index, cbAtrLen))
2628 return SCARD_E_NO_MEMORY;
2629 status = smartcard_ndr_write(s, ret->rgAtr, cbAtrLen, 1, NDR_PTR_SIMPLE);
2630 if (status != SCARD_S_SUCCESS)
2632 return ret->ReturnCode;
2638 UINT32 pbContextNdrPtr = 0;
2641 LONG status = smartcard_unpack_redir_scard_context(s, &(call->handles.hContext), &index,
2643 if (status != SCARD_S_SUCCESS)
2646 status = smartcard_unpack_redir_scard_handle(s, &(call->handles.hCard), &index);
2647 if (status != SCARD_S_SUCCESS)
2650 if (!Stream_CheckAndLogRequiredLength(TAG, s, 12))
2651 return STATUS_BUFFER_TOO_SMALL;
2653 Stream_Read_INT32(s, call->fmszReaderNamesIsNULL);
2654 Stream_Read_UINT32(s, call->cchReaderLen);
2655 Stream_Read_UINT32(s, call->cbAtrLen);
2657 if ((status = smartcard_unpack_redir_scard_context_ref(s, pbContextNdrPtr,
2658 &(call->handles.hContext))))
2661 if ((status = smartcard_unpack_redir_scard_handle_ref(s, &(call->handles.hCard))))
2664 smartcard_trace_status_call(call, unicode);
2673 DWORD cBytes = ret->cBytes;
2675 smartcard_trace_status_return(ret, unicode);
2676 if (ret->ReturnCode != SCARD_S_SUCCESS)
2678 if (cBytes == SCARD_AUTOALLOCATE)
2681 if (!Stream_EnsureRemainingCapacity(s, 4))
2682 return SCARD_F_INTERNAL_ERROR;
2684 Stream_Write_UINT32(s, cBytes);
2685 if (!smartcard_ndr_pointer_write(s, &index, cBytes))
2686 return SCARD_E_NO_MEMORY;
2688 if (!Stream_EnsureRemainingCapacity(s, 44))
2689 return SCARD_F_INTERNAL_ERROR;
2691 Stream_Write_UINT32(s, ret->dwState);
2692 Stream_Write_UINT32(s, ret->dwProtocol);
2693 Stream_Write(s, ret->pbAtr,
sizeof(ret->pbAtr));
2694 Stream_Write_UINT32(s, ret->cbAtrLen);
2695 status = smartcard_ndr_write(s, ret->mszReaderNames, cBytes, 1, NDR_PTR_SIMPLE);
2696 if (status != SCARD_S_SUCCESS)
2698 return ret->ReturnCode;
2705 UINT32 pbContextNdrPtr = 0;
2707 LONG status = smartcard_unpack_redir_scard_context(s, &(call->handles.hContext), &index,
2709 if (status != SCARD_S_SUCCESS)
2712 status = smartcard_unpack_redir_scard_handle(s, &(call->handles.hCard), &index);
2713 if (status != SCARD_S_SUCCESS)
2716 if (!Stream_CheckAndLogRequiredLength(TAG, s, 12))
2717 return STATUS_BUFFER_TOO_SMALL;
2719 Stream_Read_UINT32(s, call->dwAttrId);
2720 Stream_Read_INT32(s, call->fpbAttrIsNULL);
2721 Stream_Read_UINT32(s, call->cbAttrLen);
2723 if ((status = smartcard_unpack_redir_scard_context_ref(s, pbContextNdrPtr,
2724 &(call->handles.hContext))))
2727 if ((status = smartcard_unpack_redir_scard_handle_ref(s, &(call->handles.hCard))))
2730 smartcard_trace_get_attrib_call(call);
2735 DWORD cbAttrCallLen)
2739 DWORD cbAttrLen = 0;
2741 smartcard_trace_get_attrib_return(ret, dwAttrId);
2743 if (!Stream_EnsureRemainingCapacity(s, 4))
2744 return SCARD_F_INTERNAL_ERROR;
2746 cbAttrLen = ret->cbAttrLen;
2747 if (ret->ReturnCode != SCARD_S_SUCCESS)
2749 if (cbAttrLen == SCARD_AUTOALLOCATE)
2754 if (cbAttrCallLen < cbAttrLen)
2755 cbAttrLen = cbAttrCallLen;
2757 Stream_Write_UINT32(s, cbAttrLen);
2758 if (!smartcard_ndr_pointer_write(s, &index, cbAttrLen))
2759 return SCARD_E_NO_MEMORY;
2761 status = smartcard_ndr_write(s, ret->pbAttr, cbAttrLen, 1, NDR_PTR_SIMPLE);
2762 if (status != SCARD_S_SUCCESS)
2764 return ret->ReturnCode;
2771 UINT32 pvInBufferNdrPtr = 0;
2772 UINT32 pbContextNdrPtr = 0;
2774 call->pvInBuffer = NULL;
2776 LONG status = smartcard_unpack_redir_scard_context(s, &(call->handles.hContext), &index,
2778 if (status != SCARD_S_SUCCESS)
2781 status = smartcard_unpack_redir_scard_handle(s, &(call->handles.hCard), &index);
2782 if (status != SCARD_S_SUCCESS)
2785 if (!Stream_CheckAndLogRequiredLength(TAG, s, 20))
2786 return STATUS_BUFFER_TOO_SMALL;
2788 Stream_Read_UINT32(s, call->dwControlCode);
2789 Stream_Read_UINT32(s, call->cbInBufferSize);
2790 if (!smartcard_ndr_pointer_read(s, &index, &pvInBufferNdrPtr))
2791 return ERROR_INVALID_DATA;
2792 Stream_Read_INT32(s, call->fpvOutBufferIsNULL);
2793 Stream_Read_UINT32(s, call->cbOutBufferSize);
2795 if ((status = smartcard_unpack_redir_scard_context_ref(s, pbContextNdrPtr,
2796 &(call->handles.hContext))))
2799 if ((status = smartcard_unpack_redir_scard_handle_ref(s, &(call->handles.hCard))))
2802 if (pvInBufferNdrPtr)
2804 status = smartcard_ndr_read(s, &call->pvInBuffer, call->cbInBufferSize, 1, NDR_PTR_SIMPLE);
2805 if (status != SCARD_S_SUCCESS)
2809 smartcard_trace_control_call(call);
2810 return SCARD_S_SUCCESS;
2817 DWORD cbDataLen = ret->cbOutBufferSize;
2820 smartcard_trace_control_return(ret);
2821 if (ret->ReturnCode != SCARD_S_SUCCESS)
2823 if (cbDataLen == SCARD_AUTOALLOCATE)
2826 if (!Stream_EnsureRemainingCapacity(s, 4))
2827 return SCARD_F_INTERNAL_ERROR;
2829 Stream_Write_UINT32(s, cbDataLen);
2830 if (!smartcard_ndr_pointer_write(s, &index, cbDataLen))
2831 return SCARD_E_NO_MEMORY;
2833 status = smartcard_ndr_write(s, ret->pvOutBuffer, cbDataLen, 1, NDR_PTR_SIMPLE);
2834 if (status != SCARD_S_SUCCESS)
2836 return ret->ReturnCode;
2842 BYTE* pbExtraBytes = NULL;
2843 UINT32 pbExtraBytesNdrPtr = 0;
2844 UINT32 pbSendBufferNdrPtr = 0;
2845 UINT32 pioRecvPciNdrPtr = 0;
2849 UINT32 pbContextNdrPtr = 0;
2852 call->pioSendPci = NULL;
2853 call->pioRecvPci = NULL;
2854 call->pbSendBuffer = NULL;
2856 LONG status = smartcard_unpack_redir_scard_context(s, &(call->handles.hContext), &index,
2858 if (status != SCARD_S_SUCCESS)
2861 status = smartcard_unpack_redir_scard_handle(s, &(call->handles.hCard), &index);
2862 if (status != SCARD_S_SUCCESS)
2865 if (!Stream_CheckAndLogRequiredLength(TAG, s, 32))
2866 return STATUS_BUFFER_TOO_SMALL;
2868 Stream_Read_UINT32(s, ioSendPci.dwProtocol);
2869 Stream_Read_UINT32(s, ioSendPci.cbExtraBytes);
2870 if (!smartcard_ndr_pointer_read(s, &index,
2871 &pbExtraBytesNdrPtr))
2872 return ERROR_INVALID_DATA;
2874 Stream_Read_UINT32(s, call->cbSendLength);
2875 if (!smartcard_ndr_pointer_read(s, &index,
2876 &pbSendBufferNdrPtr))
2877 return ERROR_INVALID_DATA;
2879 if (!smartcard_ndr_pointer_read(s, &index, &pioRecvPciNdrPtr))
2880 return ERROR_INVALID_DATA;
2882 Stream_Read_INT32(s, call->fpbRecvBufferIsNULL);
2883 Stream_Read_UINT32(s, call->cbRecvLength);
2885 if (ioSendPci.cbExtraBytes > 1024)
2888 "Transmit_Call ioSendPci.cbExtraBytes is out of bounds: %" PRIu32
" (max: 1024)",
2889 ioSendPci.cbExtraBytes);
2890 return STATUS_INVALID_PARAMETER;
2893 if (call->cbSendLength > 66560)
2895 WLog_WARN(TAG,
"Transmit_Call cbSendLength is out of bounds: %" PRIu32
" (max: 66560)",
2896 ioSendPci.cbExtraBytes);
2897 return STATUS_INVALID_PARAMETER;
2900 if ((status = smartcard_unpack_redir_scard_context_ref(s, pbContextNdrPtr,
2901 &(call->handles.hContext))))
2904 if ((status = smartcard_unpack_redir_scard_handle_ref(s, &(call->handles.hCard))))
2907 if (ioSendPci.cbExtraBytes && !pbExtraBytesNdrPtr)
2910 TAG,
"Transmit_Call ioSendPci.cbExtraBytes is non-zero but pbExtraBytesNdrPtr is null");
2911 return STATUS_INVALID_PARAMETER;
2914 if (pbExtraBytesNdrPtr)
2917 if (!Stream_CheckAndLogRequiredLength(TAG, s, 4))
2918 return STATUS_BUFFER_TOO_SMALL;
2920 Stream_Read_UINT32(s, length);
2922 if (!Stream_CheckAndLogRequiredLength(TAG, s, ioSendPci.cbExtraBytes))
2923 return STATUS_BUFFER_TOO_SMALL;
2925 ioSendPci.pbExtraBytes = Stream_Pointer(s);
2929 if (!call->pioSendPci)
2931 WLog_WARN(TAG,
"Transmit_Call out of memory error (pioSendPci)");
2932 return STATUS_NO_MEMORY;
2935 call->pioSendPci->dwProtocol = ioSendPci.dwProtocol;
2936 call->pioSendPci->cbPciLength = (DWORD)(ioSendPci.cbExtraBytes +
sizeof(
SCARD_IO_REQUEST));
2938 Stream_Read(s, pbExtraBytes, ioSendPci.cbExtraBytes);
2939 smartcard_unpack_read_size_align(s, ioSendPci.cbExtraBytes, 4);
2945 if (!call->pioSendPci)
2947 WLog_WARN(TAG,
"Transmit_Call out of memory error (pioSendPci)");
2948 return STATUS_NO_MEMORY;
2951 call->pioSendPci->dwProtocol = ioSendPci.dwProtocol;
2955 if (pbSendBufferNdrPtr)
2957 status = smartcard_ndr_read(s, &call->pbSendBuffer, call->cbSendLength, 1, NDR_PTR_SIMPLE);
2958 if (status != SCARD_S_SUCCESS)
2962 if (pioRecvPciNdrPtr)
2964 if (!Stream_CheckAndLogRequiredLength(TAG, s, 12))
2965 return STATUS_BUFFER_TOO_SMALL;
2967 Stream_Read_UINT32(s, ioRecvPci.dwProtocol);
2968 Stream_Read_UINT32(s, ioRecvPci.cbExtraBytes);
2969 if (!smartcard_ndr_pointer_read(s, &index,
2970 &pbExtraBytesNdrPtr))
2971 return ERROR_INVALID_DATA;
2973 if (ioRecvPci.cbExtraBytes && !pbExtraBytesNdrPtr)
2977 "Transmit_Call ioRecvPci.cbExtraBytes is non-zero but pbExtraBytesNdrPtr is null");
2978 return STATUS_INVALID_PARAMETER;
2981 if (pbExtraBytesNdrPtr)
2984 if (!Stream_CheckAndLogRequiredLength(TAG, s, 4))
2985 return STATUS_BUFFER_TOO_SMALL;
2987 Stream_Read_UINT32(s, length);
2989 if (ioRecvPci.cbExtraBytes > 1024)
2992 "Transmit_Call ioRecvPci.cbExtraBytes is out of bounds: %" PRIu32
2994 ioRecvPci.cbExtraBytes);
2995 return STATUS_INVALID_PARAMETER;
2998 if (length != ioRecvPci.cbExtraBytes)
3001 "Transmit_Call unexpected length: Actual: %" PRIu32
", Expected: %" PRIu32
3002 " (ioRecvPci.cbExtraBytes)",
3003 length, ioRecvPci.cbExtraBytes);
3004 return STATUS_INVALID_PARAMETER;
3007 if (!Stream_CheckAndLogRequiredLength(TAG, s, ioRecvPci.cbExtraBytes))
3008 return STATUS_BUFFER_TOO_SMALL;
3010 ioRecvPci.pbExtraBytes = Stream_Pointer(s);
3014 if (!call->pioRecvPci)
3016 WLog_WARN(TAG,
"Transmit_Call out of memory error (pioRecvPci)");
3017 return STATUS_NO_MEMORY;
3020 call->pioRecvPci->dwProtocol = ioRecvPci.dwProtocol;
3021 call->pioRecvPci->cbPciLength =
3024 Stream_Read(s, pbExtraBytes, ioRecvPci.cbExtraBytes);
3025 smartcard_unpack_read_size_align(s, ioRecvPci.cbExtraBytes, 4);
3031 if (!call->pioRecvPci)
3033 WLog_WARN(TAG,
"Transmit_Call out of memory error (pioRecvPci)");
3034 return STATUS_NO_MEMORY;
3037 call->pioRecvPci->dwProtocol = ioRecvPci.dwProtocol;
3042 smartcard_trace_transmit_call(call);
3043 return SCARD_S_SUCCESS;
3052 UINT32 cbRecvLength = ret->cbRecvLength;
3053 UINT32 cbRecvPci = ret->pioRecvPci ? ret->pioRecvPci->cbPciLength : 0;
3055 smartcard_trace_transmit_return(ret);
3057 if (!ret->pbRecvBuffer)
3060 if (!smartcard_ndr_pointer_write(s, &index, cbRecvPci))
3061 return SCARD_E_NO_MEMORY;
3062 if (!Stream_EnsureRemainingCapacity(s, 4))
3063 return SCARD_E_NO_MEMORY;
3064 Stream_Write_UINT32(s, cbRecvLength);
3065 if (!smartcard_ndr_pointer_write(s, &index, cbRecvLength))
3066 return SCARD_E_NO_MEMORY;
3068 if (ret->pioRecvPci)
3070 UINT32 cbExtraBytes = (UINT32)(ret->pioRecvPci->cbPciLength -
sizeof(
SCARD_IO_REQUEST));
3073 if (!Stream_EnsureRemainingCapacity(s, cbExtraBytes + 16))
3075 WLog_ERR(TAG,
"Stream_EnsureRemainingCapacity failed!");
3076 return SCARD_F_INTERNAL_ERROR;
3079 Stream_Write_UINT32(s, ret->pioRecvPci->dwProtocol);
3080 Stream_Write_UINT32(s, cbExtraBytes);
3081 if (!smartcard_ndr_pointer_write(s, &index, cbExtraBytes))
3082 return SCARD_E_NO_MEMORY;
3083 error = smartcard_ndr_write(s, pbExtraBytes, cbExtraBytes, 1, NDR_PTR_SIMPLE);
3088 status = smartcard_ndr_write(s, ret->pbRecvBuffer, ret->cbRecvLength, 1, NDR_PTR_SIMPLE);
3089 if (status != SCARD_S_SUCCESS)
3091 return ret->ReturnCode;
3096 UINT32 rgReaderStatesNdrPtr = 0;
3097 UINT32 rgAtrMasksNdrPtr = 0;
3099 UINT32 pbContextNdrPtr = 0;
3102 call->rgReaderStates = NULL;
3104 LONG status = smartcard_unpack_redir_scard_context(s, &(call->handles.hContext), &index,
3106 if (status != SCARD_S_SUCCESS)
3109 if (!Stream_CheckAndLogRequiredLength(TAG, s, 16))
3110 return STATUS_BUFFER_TOO_SMALL;
3112 Stream_Read_UINT32(s, call->cAtrs);
3113 if (!smartcard_ndr_pointer_read(s, &index, &rgAtrMasksNdrPtr))
3114 return ERROR_INVALID_DATA;
3115 Stream_Read_UINT32(s, call->cReaders);
3116 if (!smartcard_ndr_pointer_read(s, &index, &rgReaderStatesNdrPtr))
3117 return ERROR_INVALID_DATA;
3119 if ((status = smartcard_unpack_redir_scard_context_ref(s, pbContextNdrPtr,
3120 &(call->handles.hContext))))
3123 if ((rgAtrMasksNdrPtr && !call->cAtrs) || (!rgAtrMasksNdrPtr && call->cAtrs))
3126 "LocateCardsByATRA_Call rgAtrMasksNdrPtr (0x%08" PRIX32
3127 ") and cAtrs (0x%08" PRIX32
") inconsistency",
3128 rgAtrMasksNdrPtr, call->cAtrs);
3129 return STATUS_INVALID_PARAMETER;
3132 if (rgAtrMasksNdrPtr)
3134 status = smartcard_ndr_read_atrmask(s, &call->rgAtrMasks, call->cAtrs, NDR_PTR_SIMPLE);
3135 if (status != SCARD_S_SUCCESS)
3139 if (rgReaderStatesNdrPtr)
3141 status = smartcard_unpack_reader_state_a(s, &call->rgReaderStates, call->cReaders, &index);
3142 if (status != SCARD_S_SUCCESS)
3146 smartcard_trace_locate_cards_by_atr_a_call(call);
3147 return SCARD_S_SUCCESS;
3152 UINT32 sz1NdrPtr = 0;
3153 UINT32 sz2NdrPtr = 0;
3155 UINT32 pbContextNdrPtr = 0;
3158 LONG status = smartcard_unpack_redir_scard_context(s, &(call->handles.hContext), &index,
3160 if (status != SCARD_S_SUCCESS)
3163 if (!smartcard_ndr_pointer_read(s, &index, &sz1NdrPtr))
3164 return ERROR_INVALID_DATA;
3165 if (!smartcard_ndr_pointer_read(s, &index, &sz2NdrPtr))
3166 return ERROR_INVALID_DATA;
3168 status = smartcard_unpack_redir_scard_context_ref(s, pbContextNdrPtr, &call->handles.hContext);
3169 if (status != SCARD_S_SUCCESS)
3174 status = smartcard_ndr_read_a(s, &call->sz1, NDR_PTR_FULL);
3175 if (status != SCARD_S_SUCCESS)
3180 status = smartcard_ndr_read_a(s, &call->sz2, NDR_PTR_FULL);
3181 if (status != SCARD_S_SUCCESS)
3184 smartcard_trace_context_and_two_strings_a_call(call);
3185 return SCARD_S_SUCCESS;
3190 UINT32 sz1NdrPtr = 0;
3191 UINT32 sz2NdrPtr = 0;
3193 UINT32 pbContextNdrPtr = 0;
3196 LONG status = smartcard_unpack_redir_scard_context(s, &(call->handles.hContext), &index,
3198 if (status != SCARD_S_SUCCESS)
3201 if (!smartcard_ndr_pointer_read(s, &index, &sz1NdrPtr))
3202 return ERROR_INVALID_DATA;
3203 if (!smartcard_ndr_pointer_read(s, &index, &sz2NdrPtr))
3204 return ERROR_INVALID_DATA;
3206 status = smartcard_unpack_redir_scard_context_ref(s, pbContextNdrPtr, &call->handles.hContext);
3207 if (status != SCARD_S_SUCCESS)
3212 status = smartcard_ndr_read_w(s, &call->sz1, NDR_PTR_FULL);
3213 if (status != SCARD_S_SUCCESS)
3218 status = smartcard_ndr_read_w(s, &call->sz2, NDR_PTR_FULL);
3219 if (status != SCARD_S_SUCCESS)
3222 smartcard_trace_context_and_two_strings_w_call(call);
3223 return SCARD_S_SUCCESS;
3228 UINT32 sz1NdrPtr = 0;
3229 UINT32 sz2NdrPtr = 0;
3231 UINT32 pbContextNdrPtr = 0;
3234 LONG status = smartcard_unpack_redir_scard_context(s, &(call->handles.hContext), &index,
3236 if (status != SCARD_S_SUCCESS)
3239 if (!Stream_CheckAndLogRequiredLength(TAG, s, 16))
3240 return STATUS_BUFFER_TOO_SMALL;
3242 Stream_Read_UINT32(s, call->cBytes);
3243 if (!smartcard_ndr_pointer_read(s, &index, &sz1NdrPtr))
3244 return ERROR_INVALID_DATA;
3246 Stream_Read_UINT32(s, call->cReaders);
3247 if (!smartcard_ndr_pointer_read(s, &index, &sz2NdrPtr))
3248 return ERROR_INVALID_DATA;
3250 if ((status = smartcard_unpack_redir_scard_context_ref(s, pbContextNdrPtr,
3251 &(call->handles.hContext))))
3257 smartcard_ndr_read_fixed_string_a(s, &call->mszCards, call->cBytes, NDR_PTR_SIMPLE);
3258 if (status != SCARD_S_SUCCESS)
3263 status = smartcard_unpack_reader_state_a(s, &call->rgReaderStates, call->cReaders, &index);
3264 if (status != SCARD_S_SUCCESS)
3267 smartcard_trace_locate_cards_a_call(call);
3268 return SCARD_S_SUCCESS;
3273 UINT32 sz1NdrPtr = 0;
3274 UINT32 sz2NdrPtr = 0;
3276 UINT32 pbContextNdrPtr = 0;
3279 LONG status = smartcard_unpack_redir_scard_context(s, &(call->handles.hContext), &index,
3281 if (status != SCARD_S_SUCCESS)
3284 if (!Stream_CheckAndLogRequiredLength(TAG, s, 16))
3285 return STATUS_BUFFER_TOO_SMALL;
3287 Stream_Read_UINT32(s, call->cBytes);
3288 if (!smartcard_ndr_pointer_read(s, &index, &sz1NdrPtr))
3289 return ERROR_INVALID_DATA;
3291 Stream_Read_UINT32(s, call->cReaders);
3292 if (!smartcard_ndr_pointer_read(s, &index, &sz2NdrPtr))
3293 return ERROR_INVALID_DATA;
3295 if ((status = smartcard_unpack_redir_scard_context_ref(s, pbContextNdrPtr,
3296 &(call->handles.hContext))))
3302 smartcard_ndr_read_fixed_string_w(s, &call->mszCards, call->cBytes, NDR_PTR_SIMPLE);
3303 if (status != SCARD_S_SUCCESS)
3308 status = smartcard_unpack_reader_state_w(s, &call->rgReaderStates, call->cReaders, &index);
3309 if (status != SCARD_S_SUCCESS)
3312 smartcard_trace_locate_cards_w_call(call);
3313 return SCARD_S_SUCCESS;
3320 UINT32 pbContextNdrPtr = 0;
3323 LONG status = smartcard_unpack_redir_scard_context(s, &(call->handles.hContext), &index,
3325 if (status != SCARD_S_SUCCESS)
3327 status = smartcard_unpack_redir_scard_handle(s, &(call->handles.hCard), &index);
3328 if (status != SCARD_S_SUCCESS)
3331 if (!Stream_CheckAndLogRequiredLength(TAG, s, 12))
3332 return STATUS_BUFFER_TOO_SMALL;
3333 Stream_Read_UINT32(s, call->dwAttrId);
3334 Stream_Read_UINT32(s, call->cbAttrLen);
3336 if (!smartcard_ndr_pointer_read(s, &index, &ndrPtr))
3337 return ERROR_INVALID_DATA;
3339 if ((status = smartcard_unpack_redir_scard_context_ref(s, pbContextNdrPtr,
3340 &(call->handles.hContext))))
3343 if ((status = smartcard_unpack_redir_scard_handle_ref(s, &(call->handles.hCard))))
3350 status = smartcard_ndr_read(s, &call->pbAttr, 0, 1, NDR_PTR_SIMPLE);
3351 if (status != SCARD_S_SUCCESS)
3354 smartcard_trace_set_attrib_call(call);
3355 return SCARD_S_SUCCESS;
3360 UINT32 rgReaderStatesNdrPtr = 0;
3361 UINT32 rgAtrMasksNdrPtr = 0;
3363 UINT32 pbContextNdrPtr = 0;
3366 call->rgReaderStates = NULL;
3368 LONG status = smartcard_unpack_redir_scard_context(s, &(call->handles.hContext), &index,
3370 if (status != SCARD_S_SUCCESS)
3373 if (!Stream_CheckAndLogRequiredLength(TAG, s, 16))
3374 return STATUS_BUFFER_TOO_SMALL;
3376 Stream_Read_UINT32(s, call->cAtrs);
3377 if (!smartcard_ndr_pointer_read(s, &index, &rgAtrMasksNdrPtr))
3378 return ERROR_INVALID_DATA;
3380 Stream_Read_UINT32(s, call->cReaders);
3381 if (!smartcard_ndr_pointer_read(s, &index, &rgReaderStatesNdrPtr))
3382 return ERROR_INVALID_DATA;
3384 if ((status = smartcard_unpack_redir_scard_context_ref(s, pbContextNdrPtr,
3385 &(call->handles.hContext))))
3388 if ((rgAtrMasksNdrPtr && !call->cAtrs) || (!rgAtrMasksNdrPtr && call->cAtrs))
3391 "LocateCardsByATRW_Call rgAtrMasksNdrPtr (0x%08" PRIX32
3392 ") and cAtrs (0x%08" PRIX32
") inconsistency",
3393 rgAtrMasksNdrPtr, call->cAtrs);
3394 return STATUS_INVALID_PARAMETER;
3397 if (rgAtrMasksNdrPtr)
3399 status = smartcard_ndr_read_atrmask(s, &call->rgAtrMasks, call->cAtrs, NDR_PTR_SIMPLE);
3400 if (status != SCARD_S_SUCCESS)
3404 if (rgReaderStatesNdrPtr)
3406 status = smartcard_unpack_reader_state_w(s, &call->rgReaderStates, call->cReaders, &index);
3407 if (status != SCARD_S_SUCCESS)
3411 smartcard_trace_locate_cards_by_atr_w_call(call);
3412 return SCARD_S_SUCCESS;
3417 UINT32 mszNdrPtr = 0;
3418 UINT32 contextNdrPtr = 0;
3420 UINT32 pbContextNdrPtr = 0;
3423 if (!smartcard_ndr_pointer_read(s, &index, &mszNdrPtr))
3424 return ERROR_INVALID_DATA;
3426 LONG status = smartcard_unpack_redir_scard_context(s, &(call->Common.handles.hContext), &index,
3428 if (status != SCARD_S_SUCCESS)
3431 if (!smartcard_ndr_pointer_read(s, &index, &contextNdrPtr))
3432 return ERROR_INVALID_DATA;
3434 if (!Stream_CheckAndLogRequiredLength(TAG, s, 12))
3435 return STATUS_BUFFER_TOO_SMALL;
3436 Stream_Read_UINT32(s, call->Common.FreshnessCounter);
3437 Stream_Read_INT32(s, call->Common.fPbDataIsNULL);
3438 Stream_Read_UINT32(s, call->Common.cbDataLen);
3440 call->szLookupName = NULL;
3443 status = smartcard_ndr_read_a(s, &call->szLookupName, NDR_PTR_FULL);
3444 if (status != SCARD_S_SUCCESS)
3448 status = smartcard_unpack_redir_scard_context_ref(s, pbContextNdrPtr,
3449 &call->Common.handles.hContext);
3450 if (status != SCARD_S_SUCCESS)
3455 status = smartcard_ndr_read_u(s, &call->Common.CardIdentifier);
3456 if (status != SCARD_S_SUCCESS)
3459 smartcard_trace_read_cache_a_call(call);
3460 return SCARD_S_SUCCESS;
3465 UINT32 mszNdrPtr = 0;
3466 UINT32 contextNdrPtr = 0;
3468 UINT32 pbContextNdrPtr = 0;
3471 if (!smartcard_ndr_pointer_read(s, &index, &mszNdrPtr))
3472 return ERROR_INVALID_DATA;
3474 LONG status = smartcard_unpack_redir_scard_context(s, &(call->Common.handles.hContext), &index,
3476 if (status != SCARD_S_SUCCESS)
3479 if (!smartcard_ndr_pointer_read(s, &index, &contextNdrPtr))
3480 return ERROR_INVALID_DATA;
3482 if (!Stream_CheckAndLogRequiredLength(TAG, s, 12))
3483 return STATUS_BUFFER_TOO_SMALL;
3484 Stream_Read_UINT32(s, call->Common.FreshnessCounter);
3485 Stream_Read_INT32(s, call->Common.fPbDataIsNULL);
3486 Stream_Read_UINT32(s, call->Common.cbDataLen);
3488 call->szLookupName = NULL;
3491 status = smartcard_ndr_read_w(s, &call->szLookupName, NDR_PTR_FULL);
3492 if (status != SCARD_S_SUCCESS)
3496 status = smartcard_unpack_redir_scard_context_ref(s, pbContextNdrPtr,
3497 &call->Common.handles.hContext);
3498 if (status != SCARD_S_SUCCESS)
3503 status = smartcard_ndr_read_u(s, &call->Common.CardIdentifier);
3504 if (status != SCARD_S_SUCCESS)
3507 smartcard_trace_read_cache_w_call(call);
3508 return SCARD_S_SUCCESS;
3513 UINT32 mszNdrPtr = 0;
3514 UINT32 contextNdrPtr = 0;
3515 UINT32 pbDataNdrPtr = 0;
3517 UINT32 pbContextNdrPtr = 0;
3520 if (!smartcard_ndr_pointer_read(s, &index, &mszNdrPtr))
3521 return ERROR_INVALID_DATA;
3523 LONG status = smartcard_unpack_redir_scard_context(s, &(call->Common.handles.hContext), &index,
3525 if (status != SCARD_S_SUCCESS)
3528 if (!smartcard_ndr_pointer_read(s, &index, &contextNdrPtr))
3529 return ERROR_INVALID_DATA;
3531 if (!Stream_CheckAndLogRequiredLength(TAG, s, 8))
3532 return STATUS_BUFFER_TOO_SMALL;
3534 Stream_Read_UINT32(s, call->Common.FreshnessCounter);
3535 Stream_Read_UINT32(s, call->Common.cbDataLen);
3537 if (!smartcard_ndr_pointer_read(s, &index, &pbDataNdrPtr))
3538 return ERROR_INVALID_DATA;
3540 call->szLookupName = NULL;
3543 status = smartcard_ndr_read_a(s, &call->szLookupName, NDR_PTR_FULL);
3544 if (status != SCARD_S_SUCCESS)
3548 status = smartcard_unpack_redir_scard_context_ref(s, pbContextNdrPtr,
3549 &call->Common.handles.hContext);
3550 if (status != SCARD_S_SUCCESS)
3553 call->Common.CardIdentifier = NULL;
3556 status = smartcard_ndr_read_u(s, &call->Common.CardIdentifier);
3557 if (status != SCARD_S_SUCCESS)
3561 call->Common.pbData = NULL;
3565 smartcard_ndr_read(s, &call->Common.pbData, call->Common.cbDataLen, 1, NDR_PTR_SIMPLE);
3566 if (status != SCARD_S_SUCCESS)
3569 smartcard_trace_write_cache_a_call(call);
3570 return SCARD_S_SUCCESS;
3575 UINT32 mszNdrPtr = 0;
3576 UINT32 contextNdrPtr = 0;
3577 UINT32 pbDataNdrPtr = 0;
3579 UINT32 pbContextNdrPtr = 0;
3583 if (!smartcard_ndr_pointer_read(s, &index, &mszNdrPtr))
3584 return ERROR_INVALID_DATA;
3586 LONG status = smartcard_unpack_redir_scard_context(s, &(call->Common.handles.hContext), &index,
3588 if (status != SCARD_S_SUCCESS)
3591 if (!smartcard_ndr_pointer_read(s, &index, &contextNdrPtr))
3592 return ERROR_INVALID_DATA;
3594 if (!Stream_CheckAndLogRequiredLength(TAG, s, 8))
3595 return STATUS_BUFFER_TOO_SMALL;
3596 Stream_Read_UINT32(s, call->Common.FreshnessCounter);
3597 Stream_Read_UINT32(s, call->Common.cbDataLen);
3599 if (!smartcard_ndr_pointer_read(s, &index, &pbDataNdrPtr))
3600 return ERROR_INVALID_DATA;
3602 call->szLookupName = NULL;
3605 status = smartcard_ndr_read_w(s, &call->szLookupName, NDR_PTR_FULL);
3606 if (status != SCARD_S_SUCCESS)
3610 status = smartcard_unpack_redir_scard_context_ref(s, pbContextNdrPtr,
3611 &call->Common.handles.hContext);
3612 if (status != SCARD_S_SUCCESS)
3615 call->Common.CardIdentifier = NULL;
3618 status = smartcard_ndr_read_u(s, &call->Common.CardIdentifier);
3619 if (status != SCARD_S_SUCCESS)
3623 call->Common.pbData = NULL;
3627 smartcard_ndr_read(s, &call->Common.pbData, call->Common.cbDataLen, 1, NDR_PTR_SIMPLE);
3628 if (status != SCARD_S_SUCCESS)
3631 smartcard_trace_write_cache_w_call(call);
3639 UINT32 pbContextNdrPtr = 0;
3641 LONG status = smartcard_unpack_redir_scard_context(s, &(call->handles.hContext), &index,
3643 if (status != SCARD_S_SUCCESS)
3646 status = smartcard_unpack_redir_scard_handle(s, &(call->handles.hCard), &index);
3647 if (status != SCARD_S_SUCCESS)
3650 if ((status = smartcard_unpack_redir_scard_context_ref(s, pbContextNdrPtr,
3651 &(call->handles.hContext))))
3653 WLog_ERR(TAG,
"smartcard_unpack_redir_scard_context_ref failed with error %" PRId32
"",
3658 if ((status = smartcard_unpack_redir_scard_handle_ref(s, &(call->handles.hCard))))
3659 WLog_ERR(TAG,
"smartcard_unpack_redir_scard_handle_ref failed with error %" PRId32
"",
3662 smartcard_trace_get_transmit_count_call(call);
3669 return smartcard_unpack_common_context_and_string_w(s, &call->handles.hContext,
3670 &call->szReaderName);
3676 return smartcard_unpack_common_context_and_string_a(s, &call->handles.hContext, &call->sz);
3682 return smartcard_unpack_common_context_and_string_w(s, &call->handles.hContext, &call->sz);
3688 return smartcard_unpack_common_context_and_string_w(s, &call->handles.hContext,
3689 &call->szReaderName);
3695 smartcard_trace_device_type_id_return(ret);
3697 if (!Stream_EnsureRemainingCapacity(s, 4))
3699 WLog_ERR(TAG,
"Stream_EnsureRemainingCapacity failed!");
3700 return SCARD_F_INTERNAL_ERROR;
3703 Stream_Write_UINT32(s, ret->dwDeviceId);
3705 return ret->ReturnCode;
3713 DWORD cbDataLen = ret->cReaders;
3716 smartcard_trace_locate_cards_return(ret);
3717 if (ret->ReturnCode != SCARD_S_SUCCESS)
3719 if (cbDataLen == SCARD_AUTOALLOCATE)
3722 if (!Stream_EnsureRemainingCapacity(s, 4))
3724 WLog_ERR(TAG,
"Stream_EnsureRemainingCapacity failed!");
3725 return SCARD_F_INTERNAL_ERROR;
3728 Stream_Write_UINT32(s, cbDataLen);
3729 if (!smartcard_ndr_pointer_write(s, &index, cbDataLen))
3730 return SCARD_E_NO_MEMORY;
3732 status = smartcard_ndr_write_state(s, ret->rgReaderStates, cbDataLen, NDR_PTR_SIMPLE);
3733 if (status != SCARD_S_SUCCESS)
3735 return ret->ReturnCode;
3744 DWORD cbDataLen = ret->cbDataLen;
3745 smartcard_trace_get_reader_icon_return(ret);
3746 if (ret->ReturnCode != SCARD_S_SUCCESS)
3748 if (cbDataLen == SCARD_AUTOALLOCATE)
3751 if (!Stream_EnsureRemainingCapacity(s, 4))
3753 WLog_ERR(TAG,
"Stream_EnsureRemainingCapacity failed!");
3754 return SCARD_F_INTERNAL_ERROR;
3757 Stream_Write_UINT32(s, cbDataLen);
3758 if (!smartcard_ndr_pointer_write(s, &index, cbDataLen))
3759 return SCARD_E_NO_MEMORY;
3761 status = smartcard_ndr_write(s, ret->pbData, cbDataLen, 1, NDR_PTR_SIMPLE);
3762 if (status != SCARD_S_SUCCESS)
3764 return ret->ReturnCode;
3770 smartcard_trace_get_transmit_count_return(ret);
3772 if (!Stream_EnsureRemainingCapacity(s, 4))
3774 WLog_ERR(TAG,
"Stream_EnsureRemainingCapacity failed!");
3775 return SCARD_F_INTERNAL_ERROR;
3778 Stream_Write_UINT32(s, ret->cTransmitCount);
3780 return ret->ReturnCode;
3789 DWORD cbDataLen = ret->cbDataLen;
3790 smartcard_trace_read_cache_return(ret);
3791 if (ret->ReturnCode != SCARD_S_SUCCESS)
3794 if (cbDataLen == SCARD_AUTOALLOCATE)
3797 if (!Stream_EnsureRemainingCapacity(s, 4))
3799 WLog_ERR(TAG,
"Stream_EnsureRemainingCapacity failed!");
3800 return SCARD_F_INTERNAL_ERROR;
3803 Stream_Write_UINT32(s, cbDataLen);
3804 if (!smartcard_ndr_pointer_write(s, &index, cbDataLen))
3805 return SCARD_E_NO_MEMORY;
3807 status = smartcard_ndr_write(s, ret->pbData, cbDataLen, 1, NDR_PTR_SIMPLE);
3808 if (status != SCARD_S_SUCCESS)
3810 return ret->ReturnCode;