24#include <freerdp/config.h>
27#include <winpr/print.h>
29#include <freerdp/channels/scard.h>
30#include <freerdp/utils/smartcard_pack.h>
31#include "smartcard_pack.h"
33#include <freerdp/log.h>
34#define SCARD_TAG FREERDP_TAG("scard.pack")
36static const DWORD g_LogLevel = WLOG_DEBUG;
38static wLog* scard_log(
void)
40 static wLog* log =
nullptr;
42 log = WLog_Get(SCARD_TAG);
46#define smartcard_unpack_redir_scard_context(log, s, context, index, ndr) \
47 smartcard_unpack_redir_scard_context_((log), (s), (context), (index), (ndr), __FILE__, \
49#define smartcard_unpack_redir_scard_handle(log, s, context, index) \
50 smartcard_unpack_redir_scard_handle_((log), (s), (context), (index), __FILE__, __func__, \
53WINPR_ATTR_NODISCARD
static LONG
55 UINT32* index, UINT32* ppbContextNdrPtr,
const char* file,
56 const char* function,
size_t line);
57WINPR_ATTR_NODISCARD
static LONG
60WINPR_ATTR_NODISCARD
static LONG
62 UINT32* index,
const char* file,
const char* function,
64WINPR_ATTR_NODISCARD
static LONG smartcard_pack_redir_scard_handle(wLog* log,
wStream* s,
67WINPR_ATTR_NODISCARD
static LONG
68smartcard_unpack_redir_scard_context_ref(wLog* log,
wStream* s, UINT32 pbContextNdrPtr,
70WINPR_ATTR_NODISCARD
static LONG
73WINPR_ATTR_NODISCARD
static LONG smartcard_unpack_redir_scard_handle_ref(wLog* log,
wStream* s,
75WINPR_ATTR_NODISCARD
static LONG
87#define smartcard_ndr_pointer_read(log, s, index, ptr) \
88 smartcard_ndr_pointer_read_((log), (s), (index), (ptr), __FILE__, __func__, __LINE__)
89static BOOL smartcard_ndr_pointer_read_(wLog* log,
wStream* s, UINT32* index, UINT32* ptr,
90 const char* file,
const char* fkt,
size_t line)
92 const UINT32 expect = 0x20000 + (*index) * 4;
97 if (!Stream_CheckAndLogRequiredLengthWLog(log, s, 4))
100 Stream_Read_UINT32(s, ndrPtr);
103 if (expect != ndrPtr)
106 if (ptr && (ndrPtr == 0))
108 WLog_Print(log, WLOG_WARN,
109 "[%s:%" PRIuz
"] Read context pointer 0x%08" PRIx32
", expected 0x%08" PRIx32,
110 fkt, line, ndrPtr, expect);
114 (*index) = (*index) + 1;
118WINPR_ATTR_NODISCARD
static LONG smartcard_ndr_read_ex(wLog* log,
wStream* s, BYTE** data,
119 size_t min,
size_t elementSize,
120 const ndr_ptr_t type,
size_t* plen)
144 return STATUS_INVALID_PARAMETER;
147 if (!Stream_CheckAndLogRequiredLengthWLog(log, s, required))
148 return STATUS_BUFFER_TOO_SMALL;
153 Stream_Read_UINT32(s, len);
154 Stream_Read_UINT32(s, offset);
155 Stream_Read_UINT32(s, len2);
156 if (len != offset + len2)
158 WLog_Print(log, WLOG_ERROR,
159 "Invalid data when reading full NDR pointer: total=%" PRIuz
160 ", offset=%" PRIuz
", remaining=%" PRIuz,
162 return STATUS_BUFFER_TOO_SMALL;
166 Stream_Read_UINT32(s, len);
168 if ((len != min) && (min > 0))
170 WLog_Print(log, WLOG_ERROR,
171 "Invalid data when reading simple NDR pointer: total=%" PRIuz
172 ", expected=%" PRIuz,
174 return STATUS_BUFFER_TOO_SMALL;
181 return STATUS_INVALID_PARAMETER;
186 WLog_Print(log, WLOG_ERROR,
187 "Invalid length read from NDR pointer, minimum %" PRIuz
", got %" PRIuz, min,
189 return STATUS_DATA_ERROR;
192 if (len > SIZE_MAX / 2)
193 return STATUS_BUFFER_TOO_SMALL;
195 if (!Stream_CheckAndLogRequiredLengthOfSizeWLog(log, s, len, elementSize))
196 return STATUS_BUFFER_TOO_SMALL;
203 r = calloc(len +
sizeof(WCHAR),
sizeof(CHAR));
205 return SCARD_E_NO_MEMORY;
206 Stream_Read(s, r, len);
207 const LONG pad = smartcard_unpack_read_size_align(s, len, 4);
211 return STATUS_INVALID_PARAMETER;
216 return STATUS_SUCCESS;
219WINPR_ATTR_NODISCARD
static LONG smartcard_ndr_read(wLog* log,
wStream* s, BYTE** data,
size_t min,
220 size_t elementSize,
const ndr_ptr_t type)
222 return smartcard_ndr_read_ex(log, s, data, min, elementSize, type,
nullptr);
225static BOOL smartcard_ndr_pointer_write(
wStream* s, UINT32* index, UINT32 length)
227 const UINT32 ndrPtr = 0x20000 + (*index) * 4;
231 if (!Stream_EnsureRemainingCapacity(s, 4))
236 Stream_Write_UINT32(s, ndrPtr);
237 (*index) = (*index) + 1;
240 Stream_Write_UINT32(s, 0);
244WINPR_ATTR_NODISCARD
static LONG smartcard_ndr_write(
wStream* s,
const BYTE* data, UINT32 size,
245 UINT32 elementSize,
const ndr_ptr_t type)
247 const UINT32 offset = 0;
248 const UINT32 len = size;
249 const UINT32 dataLen = size * elementSize;
253 return SCARD_S_SUCCESS;
267 return SCARD_E_INVALID_PARAMETER;
270 if (!Stream_EnsureRemainingCapacity(s, required + dataLen + 4ull))
271 return STATUS_BUFFER_TOO_SMALL;
276 Stream_Write_UINT32(s, len);
277 Stream_Write_UINT32(s, offset);
278 Stream_Write_UINT32(s, len);
281 Stream_Write_UINT32(s, len);
286 return SCARD_E_INVALID_PARAMETER;
290 Stream_Write(s, data, dataLen);
292 Stream_Zero(s, dataLen);
293 return smartcard_pack_write_size_align(s, dataLen, 4);
296WINPR_ATTR_NODISCARD
static LONG smartcard_ndr_write_state(
wStream* s,
298 UINT32 size,
const ndr_ptr_t type)
306 WINPR_ASSERT(data || (size == 0));
309 for (
size_t x = 0; x < size; x++)
312 if (!smartcard_reader_state_return_is_valid(x, cur))
313 return SCARD_E_INVALID_ATR;
318WINPR_ATTR_NODISCARD
static LONG smartcard_ndr_read_state(wLog* log,
wStream* s,
320 const ndr_ptr_t type)
334 if (status != SCARD_S_SUCCESS)
338 return SCARD_E_INVALID_VALUE;
340 for (
size_t x = 0; x < min; x++)
343 if (!smartcard_reader_state_return_is_valid(x, cur))
344 return SCARD_E_INVALID_ATR;
349WINPR_ATTR_NODISCARD
static LONG smartcard_ndr_read_atrmask(wLog* log,
wStream* s,
351 const ndr_ptr_t type)
359 const LONG status = smartcard_ndr_read(log, s, u.ppv, min,
sizeof(
LocateCards_ATRMask), type);
360 if (status != SCARD_S_SUCCESS)
367 for (
size_t x = 0; x < min; x++)
369 const UINT32 val = winpr_Data_Get_UINT32(&masks[x].cbAtr);
370 if (val > ARRAYSIZE(masks[x].rgbAtr))
372 WLog_Print(log, WLOG_ERROR,
373 "LocateCards_ATRMask[%" PRIuz
"]::cbAtr %" PRIu32
" exceeds %" PRIuz, x, val,
374 (
size_t)ARRAYSIZE(masks[x].rgbAtr));
377 return STATUS_DATA_ERROR;
380 return SCARD_S_SUCCESS;
383WINPR_ATTR_NODISCARD
static LONG smartcard_ndr_read_fixed_string_a(wLog* log,
wStream* s,
384 CHAR** data,
size_t min,
385 const ndr_ptr_t type)
393 return smartcard_ndr_read(log, s, u.ppv, min,
sizeof(CHAR), type);
396WINPR_ATTR_NODISCARD
static LONG smartcard_ndr_read_fixed_string_w(wLog* log,
wStream* s,
397 WCHAR** data,
size_t min,
398 const ndr_ptr_t type)
406 return smartcard_ndr_read(log, s, u.ppv, min,
sizeof(WCHAR), type);
409WINPR_ATTR_NODISCARD
static LONG smartcard_ndr_read_a(wLog* log,
wStream* s, CHAR** data,
410 const ndr_ptr_t type)
418 return smartcard_ndr_read(log, s, u.ppv, 0,
sizeof(CHAR), type);
421WINPR_ATTR_NODISCARD
static LONG smartcard_ndr_read_w(wLog* log,
wStream* s, WCHAR** data,
422 const ndr_ptr_t type)
430 return smartcard_ndr_read(log, s, u.ppv, 0,
sizeof(WCHAR), type);
433WINPR_ATTR_NODISCARD
static LONG smartcard_ndr_write_w(
wStream* s,
const WCHAR* data, UINT32 size,
434 const ndr_ptr_t type)
442 return smartcard_ndr_write(s, cnv.bp, size,
sizeof(WCHAR), type);
445WINPR_ATTR_NODISCARD
static LONG smartcard_ndr_write_a(
wStream* s,
const CHAR* data, UINT32 size,
446 const ndr_ptr_t type)
454 return smartcard_ndr_write(s, cnv.bp, size,
sizeof(CHAR), type);
457WINPR_ATTR_NODISCARD
static LONG smartcard_ndr_read_u(wLog* log,
wStream* s,
UUID** data)
465 return smartcard_ndr_read(log, s, u.ppv, 1,
sizeof(
UUID), NDR_PTR_FIXED);
468static char* smartcard_convert_string_list(
const void* in,
size_t bytes, BOOL unicode)
477 char* mszA =
nullptr;
489 mszA = ConvertMszWCharNToUtf8Alloc(
string.wz, bytes /
sizeof(WCHAR), &length);
495 mszA = (
char*)calloc(bytes,
sizeof(
char));
498 CopyMemory(mszA,
string.sz, bytes - 1);
507 for (
size_t index = 0; index < length - 1; index++)
509 if (mszA[index] ==
'\0')
516WINPR_ATTR_MALLOC(free, 1)
517static
char* smartcard_create_msz_dump(const
char* msz,
size_t len)
519 size_t bufferLen = len;
520 char* buffer = calloc(len + 1, 1);
525 const char* cur = msz;
527 while ((len > 0) && cur && cur[0] !=
'\0' && (bufferLen > 0))
529 size_t clen = strnlen(cur, len);
530 int rc = _snprintf(buf, bufferLen,
"%s", cur);
531 bufferLen -= (size_t)rc;
540static void smartcard_msz_dump(wLog* log, DWORD level,
const char* prefix,
const void* data,
541 size_t len, UINT32 wchar)
543 if (!WLog_IsLevelActive(log, level))
547 const char* msz = WINPR_CXX_COMPAT_CAST(
const char*, data);
551 tmp = ConvertMszWCharNToUtf8Alloc(data, len, &mszlen);
555 char* array = smartcard_create_msz_dump(msz, mszlen);
556 WLog_Print(log, level,
"%s%s", prefix, array);
561WINPR_ATTR_MALLOC(free, 1)
562static
char* smartcard_create_array_dump(const
void* pd,
size_t len)
564 const BYTE* data = pd;
567 size_t bufferLen = len * 4;
570 char* buffer = calloc(bufferLen + 1, 1);
573 char* start = buffer;
575 WINPR_ASSERT(buffer || (bufferLen == 0));
577 if (!data && (len > 0))
579 (void)_snprintf(buffer, bufferLen,
"{ nullptr [%" PRIuz
"] }", len);
583 rc = _snprintf(buffer, bufferLen,
"{ ");
584 if ((rc < 0) || ((
size_t)rc >= bufferLen))
587 bufferLen -= (size_t)rc;
589 for (
size_t x = 0; x < len; x++)
591 rc = _snprintf(buffer, bufferLen,
"%02X", data[x]);
592 if ((rc < 0) || ((
size_t)rc >= bufferLen))
595 bufferLen -= (size_t)rc;
598 rc = _snprintf(buffer, bufferLen,
" }");
599 if ((rc < 0) || ((
size_t)rc >= bufferLen))
606WINPR_ATTR_FORMAT_ARG(3, 7)
607static
void smartcard_dump_array(wLog* log, DWORD level, WINPR_FORMAT_ARG const
char* prefix,
608 const
char* postfix, const
void* data,
size_t len, ...)
610 if (!WLog_IsLevelActive(log, level))
613 char* buffer = smartcard_create_array_dump(data, len);
615 char* fprefix =
nullptr;
617 va_list ap = WINPR_C_ARRAY_INIT;
619 winpr_vasprintf(&fprefix, &flen, prefix, ap);
621 WLog_Print(log, level,
"%s%s%s", fprefix, buffer, postfix);
626static void smartcard_log_redir_handle(wLog* log,
const REDIR_SCARDHANDLE* pHandle)
628 WINPR_ASSERT(pHandle);
629 smartcard_dump_array(log, g_LogLevel,
"handle: ",
"", pHandle->pbHandle, pHandle->cbHandle);
634 WINPR_ASSERT(phContext);
635 smartcard_dump_array(log, g_LogLevel,
"hContext: ",
"", phContext->pbContext,
636 phContext->cbContext);
639static void smartcard_trace_context_and_string_call_a(wLog* log,
const char* name,
643 if (!WLog_IsLevelActive(log, g_LogLevel))
646 WLog_Print(log, g_LogLevel,
"%s {", name);
647 smartcard_log_context(log, phContext);
648 WLog_Print(log, g_LogLevel,
" sz=%s", sz);
650 WLog_Print(log, g_LogLevel,
"}");
653static void smartcard_trace_context_and_string_call_w(wLog* log,
const char* name,
659 if (!WLog_IsLevelActive(log, g_LogLevel))
663 tmp = ConvertWCharToUtf8Alloc(sz,
nullptr);
665 WLog_Print(log, g_LogLevel,
"%s {", name);
666 smartcard_log_context(log, phContext);
667 WLog_Print(log, g_LogLevel,
" sz=%s", tmp);
668 WLog_Print(log, g_LogLevel,
"}");
672static void smartcard_trace_context_call(wLog* log,
const Context_Call* call,
const char* name)
676 if (!WLog_IsLevelActive(log, g_LogLevel))
679 WLog_Print(log, g_LogLevel,
"%s_Call {", name);
680 smartcard_log_context(log, &call->handles.hContext);
682 WLog_Print(log, g_LogLevel,
"}");
690 if (!WLog_IsLevelActive(log, g_LogLevel))
693 WLog_Print(log, g_LogLevel,
"ListReaderGroups%s_Call {", unicode ?
"W" :
"A");
694 smartcard_log_context(log, &call->handles.hContext);
696 WLog_Print(log, g_LogLevel,
"fmszGroupsIsNULL: %" PRId32
" cchGroups: 0x%08" PRIx32,
697 call->fmszGroupsIsNULL, call->cchGroups);
698 WLog_Print(log, g_LogLevel,
"}");
701static void dump_reader_states_return(wLog* log,
const ReaderState_Return* rgReaderStates,
704 WINPR_ASSERT(rgReaderStates || (cReaders == 0));
705 for (UINT32 index = 0; index < cReaders; index++)
708 char* szCurrentState = SCardGetReaderStateString(readerState->dwCurrentState);
709 char* szEventState = SCardGetReaderStateString(readerState->dwEventState);
710 WLog_Print(log, g_LogLevel,
"\t[%" PRIu32
"]: dwCurrentState: %s (0x%08" PRIX32
")", index,
711 szCurrentState, readerState->dwCurrentState);
712 WLog_Print(log, g_LogLevel,
"\t[%" PRIu32
"]: dwEventState: %s (0x%08" PRIX32
")", index,
713 szEventState, readerState->dwEventState);
714 free(szCurrentState);
717 smartcard_dump_array(log, g_LogLevel,
"\t[%" PRIu32
"]: cbAttr: %" PRIu32
" { ",
" }",
718 readerState->rgbAtr, readerState->cbAtr, index, readerState->cbAtr);
725 WINPR_ASSERT(rgReaderStates || (cReaders == 0));
726 for (UINT32 index = 0; index < cReaders; index++)
730 WLog_Print(log, g_LogLevel,
"\t[%" PRIu32
"]: szReader: %s cbAtr: %" PRIu32
"", index,
731 readerState->szReader, readerState->cbAtr);
732 char* szCurrentState = SCardGetReaderStateString(readerState->dwCurrentState);
733 char* szEventState = SCardGetReaderStateString(readerState->dwEventState);
734 WLog_Print(log, g_LogLevel,
"\t[%" PRIu32
"]: dwCurrentState: %s (0x%08" PRIX32
")", index,
735 szCurrentState, readerState->dwCurrentState);
736 WLog_Print(log, g_LogLevel,
"\t[%" PRIu32
"]: dwEventState: %s (0x%08" PRIX32
")", index,
737 szEventState, readerState->dwEventState);
738 free(szCurrentState);
741 smartcard_dump_array(log, g_LogLevel,
"\t[%" PRIu32
"]: cbAttr: %" PRIu32
" { ",
" }",
742 readerState->rgbAtr, readerState->cbAtr, index, readerState->cbAtr);
749 WINPR_ASSERT(rgReaderStates || (cReaders == 0));
750 for (UINT32 index = 0; index < cReaders; index++)
753 char* buffer = ConvertWCharToUtf8Alloc(readerState->szReader,
nullptr);
754 WLog_Print(log, g_LogLevel,
"\t[%" PRIu32
"]: szReader: %s cbAtr: %" PRIu32
"", index,
755 buffer, readerState->cbAtr);
757 char* szCurrentState = SCardGetReaderStateString(readerState->dwCurrentState);
758 char* szEventState = SCardGetReaderStateString(readerState->dwEventState);
759 WLog_Print(log, g_LogLevel,
"\t[%" PRIu32
"]: dwCurrentState: %s (0x%08" PRIX32
")", index,
760 szCurrentState, readerState->dwCurrentState);
761 WLog_Print(log, g_LogLevel,
"\t[%" PRIu32
"]: dwEventState: %s (0x%08" PRIX32
")", index,
762 szEventState, readerState->dwEventState);
763 free(szCurrentState);
766 smartcard_dump_array(log, g_LogLevel,
"\t[%" PRIu32
"]: cbAttr: %" PRIu32
" { ",
" }",
767 readerState->rgbAtr, readerState->cbAtr, index, readerState->cbAtr);
775 if (!WLog_IsLevelActive(log, g_LogLevel))
778 WLog_Print(log, g_LogLevel,
"GetStatusChangeW_Call {");
779 smartcard_log_context(log, &call->handles.hContext);
781 WLog_Print(log, g_LogLevel,
"dwTimeOut: 0x%08" PRIX32
" cReaders: %" PRIu32
"", call->dwTimeOut,
784 dump_reader_states_w(log, call->rgReaderStates, call->cReaders);
786 WLog_Print(log, g_LogLevel,
"}");
794 if (!WLog_IsLevelActive(log, g_LogLevel))
797 char* mszA = smartcard_convert_string_list(ret->msz, ret->cBytes, unicode);
799 WLog_Print(log, g_LogLevel,
"ListReaderGroups%s_Return {", unicode ?
"W" :
"A");
800 WLog_Print(log, g_LogLevel,
" ReturnCode: %s (0x%08" PRIx32
")",
801 SCardGetErrorString(ret->ReturnCode),
802 WINPR_CXX_COMPAT_CAST(UINT32, ret->ReturnCode));
803 WLog_Print(log, g_LogLevel,
" cBytes: %" PRIu32
" msz: %s", ret->cBytes, mszA);
804 WLog_Print(log, g_LogLevel,
"}");
808static void smartcard_trace_list_readers_call(wLog* log,
const ListReaders_Call* call, BOOL unicode)
812 if (!WLog_IsLevelActive(log, g_LogLevel))
815 char* mszGroupsA = smartcard_convert_string_list(call->mszGroups, call->cBytes, unicode);
817 WLog_Print(log, g_LogLevel,
"ListReaders%s_Call {", unicode ?
"W" :
"A");
818 smartcard_log_context(log, &call->handles.hContext);
820 WLog_Print(log, g_LogLevel,
821 "cBytes: %" PRIu32
" mszGroups: %s fmszReadersIsNULL: %" PRId32
822 " cchReaders: 0x%08" PRIX32
"",
823 call->cBytes, mszGroupsA, call->fmszReadersIsNULL, call->cchReaders);
824 WLog_Print(log, g_LogLevel,
"}");
829static void smartcard_trace_locate_cards_by_atr_a_call(wLog* log,
834 if (!WLog_IsLevelActive(log, g_LogLevel))
837 WLog_Print(log, g_LogLevel,
"LocateCardsByATRA_Call {");
838 smartcard_log_context(log, &call->handles.hContext);
840 dump_reader_states_a(log, call->rgReaderStates, call->cReaders);
842 WLog_Print(log, g_LogLevel,
"}");
845static void smartcard_trace_locate_cards_a_call(wLog* log,
const LocateCardsA_Call* call)
849 if (!WLog_IsLevelActive(log, g_LogLevel))
852 WLog_Print(log, g_LogLevel,
"LocateCardsA_Call {");
853 smartcard_log_context(log, &call->handles.hContext);
854 WLog_Print(log, g_LogLevel,
" cBytes=%" PRIu32, call->cBytes);
855 smartcard_msz_dump(log, g_LogLevel,
" mszCards=", call->mszCards, call->cBytes, FALSE);
856 WLog_Print(log, g_LogLevel,
" cReaders=%" PRIu32, call->cReaders);
857 dump_reader_states_a(log, call->rgReaderStates, call->cReaders);
859 WLog_Print(log, g_LogLevel,
"}");
862static void smartcard_trace_locate_cards_return(wLog* log,
const LocateCards_Return* ret)
866 if (!WLog_IsLevelActive(log, g_LogLevel))
869 WLog_Print(log, g_LogLevel,
"LocateCards_Return {");
870 WLog_Print(log, g_LogLevel,
" ReturnCode: %s (0x%08" PRIX32
")",
871 SCardGetErrorString(ret->ReturnCode),
872 WINPR_CXX_COMPAT_CAST(UINT32, ret->ReturnCode));
874 if (ret->ReturnCode == SCARD_S_SUCCESS)
876 WLog_Print(log, g_LogLevel,
" cReaders=%" PRIu32, ret->cReaders);
878 WLog_Print(log, g_LogLevel,
"}");
885 if (!WLog_IsLevelActive(log, g_LogLevel))
888 WLog_Print(log, g_LogLevel,
"GetReaderIcon_Return {");
889 WLog_Print(log, g_LogLevel,
" ReturnCode: %s (0x%08" PRIX32
")",
890 SCardGetErrorString(ret->ReturnCode),
891 WINPR_CXX_COMPAT_CAST(UINT32, ret->ReturnCode));
893 if (ret->ReturnCode == SCARD_S_SUCCESS)
895 WLog_Print(log, g_LogLevel,
" cbDataLen=%" PRIu32, ret->cbDataLen);
897 WLog_Print(log, g_LogLevel,
"}");
904 if (!WLog_IsLevelActive(log, g_LogLevel))
907 WLog_Print(log, g_LogLevel,
"GetTransmitCount_Return {");
908 WLog_Print(log, g_LogLevel,
" ReturnCode: %s (0x%08" PRIX32
")",
909 SCardGetErrorString(ret->ReturnCode),
910 WINPR_CXX_COMPAT_CAST(UINT32, ret->ReturnCode));
912 WLog_Print(log, g_LogLevel,
" cTransmitCount=%" PRIu32, ret->cTransmitCount);
913 WLog_Print(log, g_LogLevel,
"}");
916static void smartcard_trace_read_cache_return(wLog* log,
const ReadCache_Return* ret)
920 if (!WLog_IsLevelActive(log, g_LogLevel))
923 WLog_Print(log, g_LogLevel,
"ReadCache_Return {");
924 WLog_Print(log, g_LogLevel,
" ReturnCode: %s (0x%08" PRIX32
")",
925 SCardGetErrorString(ret->ReturnCode),
926 WINPR_CXX_COMPAT_CAST(UINT32, ret->ReturnCode));
928 if (ret->ReturnCode == SCARD_S_SUCCESS)
930 WLog_Print(log, g_LogLevel,
" cbDataLen=%" PRIu32, ret->cbDataLen);
931 smartcard_dump_array(log, g_LogLevel,
" cbData: ",
"", ret->pbData, ret->cbDataLen);
933 WLog_Print(log, g_LogLevel,
"}");
936static void smartcard_trace_locate_cards_w_call(wLog* log,
const LocateCardsW_Call* call)
940 if (!WLog_IsLevelActive(log, g_LogLevel))
943 WLog_Print(log, g_LogLevel,
"LocateCardsW_Call {");
944 smartcard_log_context(log, &call->handles.hContext);
945 WLog_Print(log, g_LogLevel,
" cBytes=%" PRIu32, call->cBytes);
946 smartcard_msz_dump(log, g_LogLevel,
" sz2=", call->mszCards, call->cBytes, TRUE);
947 WLog_Print(log, g_LogLevel,
" cReaders=%" PRIu32, call->cReaders);
948 dump_reader_states_w(log, call->rgReaderStates, call->cReaders);
949 WLog_Print(log, g_LogLevel,
"}");
952static void smartcard_trace_list_readers_return(wLog* log,
const ListReaders_Return* ret,
957 if (!WLog_IsLevelActive(log, g_LogLevel))
960 WLog_Print(log, g_LogLevel,
"ListReaders%s_Return {", unicode ?
"W" :
"A");
961 WLog_Print(log, g_LogLevel,
" ReturnCode: %s (0x%08" PRIX32
")",
962 SCardGetErrorString(ret->ReturnCode),
963 WINPR_CXX_COMPAT_CAST(UINT32, ret->ReturnCode));
965 if (ret->ReturnCode != SCARD_S_SUCCESS)
967 WLog_Print(log, g_LogLevel,
"}");
971 char* mszA = smartcard_convert_string_list(ret->msz, ret->cBytes, unicode);
973 WLog_Print(log, g_LogLevel,
" cBytes: %" PRIu32
" msz: %s", ret->cBytes, mszA);
974 WLog_Print(log, g_LogLevel,
"}");
983 if (!WLog_IsLevelActive(log, g_LogLevel))
986 WLog_Print(log, g_LogLevel,
"GetStatusChange%s_Return {", unicode ?
"W" :
"A");
987 WLog_Print(log, g_LogLevel,
" ReturnCode: %s (0x%08" PRIX32
")",
988 SCardGetErrorString(ret->ReturnCode),
989 WINPR_CXX_COMPAT_CAST(UINT32, ret->ReturnCode));
990 WLog_Print(log, g_LogLevel,
" cReaders: %" PRIu32
"", ret->cReaders);
992 dump_reader_states_return(log, ret->rgReaderStates, ret->cReaders);
994 if (!ret->rgReaderStates && (ret->cReaders > 0))
996 WLog_Print(log, g_LogLevel,
" [INVALID STATE] rgReaderStates=nullptr, cReaders=%" PRIu32,
999 else if (ret->ReturnCode != SCARD_S_SUCCESS)
1001 WLog_Print(log, g_LogLevel,
" [INVALID RETURN] rgReaderStates, cReaders=%" PRIu32,
1006 for (UINT32 index = 0; index < ret->cReaders; index++)
1009 char* szCurrentState = SCardGetReaderStateString(rgReaderState->dwCurrentState);
1010 char* szEventState = SCardGetReaderStateString(rgReaderState->dwEventState);
1011 WLog_Print(log, g_LogLevel,
" [%" PRIu32
"]: dwCurrentState: %s (0x%08" PRIX32
")",
1012 index, szCurrentState, rgReaderState->dwCurrentState);
1013 WLog_Print(log, g_LogLevel,
" [%" PRIu32
"]: dwEventState: %s (0x%08" PRIX32
")",
1014 index, szEventState, rgReaderState->dwEventState);
1015 smartcard_dump_array(
1016 log, g_LogLevel,
" [%" PRIu32
"]: cbAtr: %" PRIu32
" rgbAtr: ",
"",
1017 rgReaderState->rgbAtr, rgReaderState->cbAtr, index, rgReaderState->cbAtr);
1018 free(szCurrentState);
1023 WLog_Print(log, g_LogLevel,
"}");
1026static void smartcard_trace_context_and_two_strings_a_call(wLog* log,
1031 if (!WLog_IsLevelActive(log, g_LogLevel))
1034 WLog_Print(log, g_LogLevel,
"ContextAndTwoStringW_Call {");
1035 smartcard_log_context(log, &call->handles.hContext);
1036 WLog_Print(log, g_LogLevel,
" sz1=%s", call->sz1);
1037 WLog_Print(log, g_LogLevel,
" sz2=%s", call->sz2);
1038 WLog_Print(log, g_LogLevel,
"}");
1041static void smartcard_trace_context_and_two_strings_w_call(wLog* log,
1045 char sz1[1024] = WINPR_C_ARRAY_INIT;
1046 char sz2[1024] = WINPR_C_ARRAY_INIT;
1048 if (!WLog_IsLevelActive(log, g_LogLevel))
1051 (void)ConvertWCharToUtf8(call->sz1, sz1, ARRAYSIZE(sz1));
1053 (void)ConvertWCharToUtf8(call->sz2, sz2, ARRAYSIZE(sz2));
1055 WLog_Print(log, g_LogLevel,
"ContextAndTwoStringW_Call {");
1056 smartcard_log_context(log, &call->handles.hContext);
1057 WLog_Print(log, g_LogLevel,
" sz1=%s", sz1);
1058 WLog_Print(log, g_LogLevel,
" sz2=%s", sz2);
1059 WLog_Print(log, g_LogLevel,
"}");
1066 if (!WLog_IsLevelActive(log, g_LogLevel))
1069 WLog_Print(log, g_LogLevel,
"GetTransmitCount_Call {");
1070 smartcard_log_context(log, &call->handles.hContext);
1071 smartcard_log_redir_handle(log, &call->handles.hCard);
1073 WLog_Print(log, g_LogLevel,
"}");
1076static void smartcard_trace_write_cache_a_call(wLog* log,
const WriteCacheA_Call* call)
1080 if (!WLog_IsLevelActive(log, g_LogLevel))
1083 WLog_Print(log, g_LogLevel,
"WriteCacheA_Call {");
1085 WLog_Print(log, g_LogLevel,
" szLookupName=%s", call->szLookupName);
1087 smartcard_log_context(log, &call->Common.handles.hContext);
1088 smartcard_dump_array(log, g_LogLevel,
"..CardIdentifier=",
"", call->Common.CardIdentifier,
1090 WLog_Print(log, g_LogLevel,
" FreshnessCounter=%" PRIu32, call->Common.FreshnessCounter);
1091 WLog_Print(log, g_LogLevel,
" cbDataLen=%" PRIu32, call->Common.cbDataLen);
1092 smartcard_dump_array(log, g_LogLevel,
" pbData=",
"", call->Common.pbData,
1093 call->Common.cbDataLen);
1094 WLog_Print(log, g_LogLevel,
"}");
1097static void smartcard_trace_write_cache_w_call(wLog* log,
const WriteCacheW_Call* call)
1100 char tmp[1024] = WINPR_C_ARRAY_INIT;
1102 if (!WLog_IsLevelActive(log, g_LogLevel))
1105 WLog_Print(log, g_LogLevel,
"WriteCacheW_Call {");
1107 if (call->szLookupName)
1108 (void)ConvertWCharToUtf8(call->szLookupName, tmp, ARRAYSIZE(tmp));
1109 WLog_Print(log, g_LogLevel,
" szLookupName=%s", tmp);
1111 smartcard_log_context(log, &call->Common.handles.hContext);
1112 smartcard_dump_array(log, g_LogLevel,
"..CardIdentifier=",
"", call->Common.CardIdentifier,
1114 WLog_Print(log, g_LogLevel,
" FreshnessCounter=%" PRIu32, call->Common.FreshnessCounter);
1115 WLog_Print(log, g_LogLevel,
" cbDataLen=%" PRIu32, call->Common.cbDataLen);
1116 smartcard_dump_array(log, g_LogLevel,
" pbData=",
"", call->Common.pbData,
1117 call->Common.cbDataLen);
1118 WLog_Print(log, g_LogLevel,
"}");
1121static void smartcard_trace_read_cache_a_call(wLog* log,
const ReadCacheA_Call* call)
1125 if (!WLog_IsLevelActive(log, g_LogLevel))
1128 WLog_Print(log, g_LogLevel,
"ReadCacheA_Call {");
1130 WLog_Print(log, g_LogLevel,
" szLookupName=%s", call->szLookupName);
1131 smartcard_log_context(log, &call->Common.handles.hContext);
1132 smartcard_dump_array(log, g_LogLevel,
"..CardIdentifier=",
"", call->Common.CardIdentifier,
1134 WLog_Print(log, g_LogLevel,
" FreshnessCounter=%" PRIu32, call->Common.FreshnessCounter);
1135 WLog_Print(log, g_LogLevel,
" fPbDataIsNULL=%" PRId32, call->Common.fPbDataIsNULL);
1136 WLog_Print(log, g_LogLevel,
" cbDataLen=%" PRIu32, call->Common.cbDataLen);
1138 WLog_Print(log, g_LogLevel,
"}");
1141static void smartcard_trace_read_cache_w_call(wLog* log,
const ReadCacheW_Call* call)
1144 char tmp[1024] = WINPR_C_ARRAY_INIT;
1146 if (!WLog_IsLevelActive(log, g_LogLevel))
1149 WLog_Print(log, g_LogLevel,
"ReadCacheW_Call {");
1150 if (call->szLookupName)
1151 (void)ConvertWCharToUtf8(call->szLookupName, tmp, ARRAYSIZE(tmp));
1152 WLog_Print(log, g_LogLevel,
" szLookupName=%s", tmp);
1154 smartcard_log_context(log, &call->Common.handles.hContext);
1155 smartcard_dump_array(log, g_LogLevel,
"..CardIdentifier=",
"", call->Common.CardIdentifier,
1157 WLog_Print(log, g_LogLevel,
" FreshnessCounter=%" PRIu32, call->Common.FreshnessCounter);
1158 WLog_Print(log, g_LogLevel,
" fPbDataIsNULL=%" PRId32, call->Common.fPbDataIsNULL);
1159 WLog_Print(log, g_LogLevel,
" cbDataLen=%" PRIu32, call->Common.cbDataLen);
1161 WLog_Print(log, g_LogLevel,
"}");
1164static void smartcard_trace_transmit_call(wLog* log,
const Transmit_Call* call)
1167 UINT32 cbExtraBytes = 0;
1168 BYTE* pbExtraBytes =
nullptr;
1170 if (!WLog_IsLevelActive(log, g_LogLevel))
1173 WLog_Print(log, g_LogLevel,
"Transmit_Call {");
1174 smartcard_log_context(log, &call->handles.hContext);
1175 smartcard_log_redir_handle(log, &call->handles.hCard);
1177 if (call->pioSendPci)
1179 cbExtraBytes = (UINT32)(call->pioSendPci->cbPciLength -
sizeof(
SCARD_IO_REQUEST));
1181 WLog_Print(log, g_LogLevel,
"pioSendPci: dwProtocol: %" PRIu32
" cbExtraBytes: %" PRIu32
"",
1182 call->pioSendPci->dwProtocol, cbExtraBytes);
1186 smartcard_dump_array(log, g_LogLevel,
"pbExtraBytes: ",
"", pbExtraBytes, cbExtraBytes);
1191 WLog_Print(log, g_LogLevel,
"pioSendPci: null");
1194 WLog_Print(log, g_LogLevel,
"cbSendLength: %" PRIu32
"", call->cbSendLength);
1196 if (call->pbSendBuffer)
1198 smartcard_dump_array(log, g_LogLevel,
"pbSendBuffer: ",
"", call->pbSendBuffer,
1199 call->cbSendLength);
1203 WLog_Print(log, g_LogLevel,
"pbSendBuffer: null");
1206 if (call->pioRecvPci)
1208 cbExtraBytes = (UINT32)(call->pioRecvPci->cbPciLength -
sizeof(
SCARD_IO_REQUEST));
1210 WLog_Print(log, g_LogLevel,
"pioRecvPci: dwProtocol: %" PRIu32
" cbExtraBytes: %" PRIu32
"",
1211 call->pioRecvPci->dwProtocol, cbExtraBytes);
1215 smartcard_dump_array(log, g_LogLevel,
"pbExtraBytes: ",
"", pbExtraBytes, cbExtraBytes);
1220 WLog_Print(log, g_LogLevel,
"pioRecvPci: null");
1223 WLog_Print(log, g_LogLevel,
"fpbRecvBufferIsNULL: %" PRId32
" cbRecvLength: %" PRIu32
"",
1224 call->fpbRecvBufferIsNULL, call->cbRecvLength);
1225 WLog_Print(log, g_LogLevel,
"}");
1228static void smartcard_trace_locate_cards_by_atr_w_call(wLog* log,
1233 if (!WLog_IsLevelActive(log, g_LogLevel))
1236 WLog_Print(log, g_LogLevel,
"LocateCardsByATRW_Call {");
1237 smartcard_log_context(log, &call->handles.hContext);
1239 dump_reader_states_w(log, call->rgReaderStates, call->cReaders);
1241 WLog_Print(log, g_LogLevel,
"}");
1244static void smartcard_trace_transmit_return(wLog* log,
const Transmit_Return* ret)
1247 UINT32 cbExtraBytes = 0;
1248 BYTE* pbExtraBytes =
nullptr;
1250 if (!WLog_IsLevelActive(log, g_LogLevel))
1253 WLog_Print(log, g_LogLevel,
"Transmit_Return {");
1254 WLog_Print(log, g_LogLevel,
" ReturnCode: %s (0x%08" PRIX32
")",
1255 SCardGetErrorString(ret->ReturnCode),
1256 WINPR_CXX_COMPAT_CAST(UINT32, ret->ReturnCode));
1258 if (ret->pioRecvPci)
1260 cbExtraBytes = (UINT32)(ret->pioRecvPci->cbPciLength -
sizeof(
SCARD_IO_REQUEST));
1262 WLog_Print(log, g_LogLevel,
1263 " pioRecvPci: dwProtocol: %" PRIu32
" cbExtraBytes: %" PRIu32
"",
1264 ret->pioRecvPci->dwProtocol, cbExtraBytes);
1268 smartcard_dump_array(log, g_LogLevel,
" pbExtraBytes: ",
"", pbExtraBytes,
1274 WLog_Print(log, g_LogLevel,
" pioRecvPci: null");
1277 WLog_Print(log, g_LogLevel,
" cbRecvLength: %" PRIu32
"", ret->cbRecvLength);
1279 if (ret->pbRecvBuffer)
1281 smartcard_dump_array(log, g_LogLevel,
" pbRecvBuffer: ",
"", ret->pbRecvBuffer,
1286 WLog_Print(log, g_LogLevel,
" pbRecvBuffer: null");
1289 WLog_Print(log, g_LogLevel,
"}");
1292static void smartcard_trace_control_return(wLog* log,
const Control_Return* ret)
1296 if (!WLog_IsLevelActive(log, g_LogLevel))
1299 WLog_Print(log, g_LogLevel,
"Control_Return {");
1300 WLog_Print(log, g_LogLevel,
" ReturnCode: %s (0x%08" PRIX32
")",
1301 SCardGetErrorString(ret->ReturnCode),
1302 WINPR_CXX_COMPAT_CAST(UINT32, ret->ReturnCode));
1303 WLog_Print(log, g_LogLevel,
" cbOutBufferSize: %" PRIu32
"", ret->cbOutBufferSize);
1305 if (ret->pvOutBuffer)
1307 smartcard_dump_array(log, g_LogLevel,
"pvOutBuffer: ",
"", ret->pvOutBuffer,
1308 ret->cbOutBufferSize);
1312 WLog_Print(log, g_LogLevel,
"pvOutBuffer: null");
1315 WLog_Print(log, g_LogLevel,
"}");
1318static void smartcard_trace_control_call(wLog* log,
const Control_Call* call)
1322 if (!WLog_IsLevelActive(log, g_LogLevel))
1325 WLog_Print(log, g_LogLevel,
"Control_Call {");
1326 smartcard_log_context(log, &call->handles.hContext);
1327 smartcard_log_redir_handle(log, &call->handles.hCard);
1329 WLog_Print(log, g_LogLevel,
1330 "dwControlCode: 0x%08" PRIX32
" cbInBufferSize: %" PRIu32
1331 " fpvOutBufferIsNULL: %" PRId32
" cbOutBufferSize: %" PRIu32
"",
1332 call->dwControlCode, call->cbInBufferSize, call->fpvOutBufferIsNULL,
1333 call->cbOutBufferSize);
1335 if (call->pvInBuffer)
1337 smartcard_dump_array(log, WLOG_DEBUG,
"pbInBuffer: ",
"", call->pvInBuffer,
1338 call->cbInBufferSize);
1342 WLog_Print(log, g_LogLevel,
"pvInBuffer: null");
1345 WLog_Print(log, g_LogLevel,
"}");
1348static void smartcard_trace_set_attrib_call(wLog* log,
const SetAttrib_Call* call)
1352 if (!WLog_IsLevelActive(log, g_LogLevel))
1355 WLog_Print(log, g_LogLevel,
"GetAttrib_Call {");
1356 smartcard_log_context(log, &call->handles.hContext);
1357 smartcard_log_redir_handle(log, &call->handles.hCard);
1358 WLog_Print(log, g_LogLevel,
"dwAttrId: 0x%08" PRIX32, call->dwAttrId);
1359 WLog_Print(log, g_LogLevel,
"cbAttrLen: 0x%08" PRIx32, call->cbAttrLen);
1360 smartcard_dump_array(log, g_LogLevel,
"pbAttr: ",
"", call->pbAttr, call->cbAttrLen);
1361 WLog_Print(log, g_LogLevel,
"}");
1364static void smartcard_trace_get_attrib_return(wLog* log,
const GetAttrib_Return* ret,
1369 if (!WLog_IsLevelActive(log, g_LogLevel))
1372 WLog_Print(log, g_LogLevel,
"GetAttrib_Return {");
1373 WLog_Print(log, g_LogLevel,
" ReturnCode: %s (0x%08" PRIX32
")",
1374 SCardGetErrorString(ret->ReturnCode),
1375 WINPR_CXX_COMPAT_CAST(UINT32, ret->ReturnCode));
1376 WLog_Print(log, g_LogLevel,
" dwAttrId: %s (0x%08" PRIX32
") cbAttrLen: 0x%08" PRIX32
"",
1377 SCardGetAttributeString(dwAttrId), dwAttrId, ret->cbAttrLen);
1378 smartcard_dump_array(log, g_LogLevel,
" ",
"", ret->pbAttr, ret->cbAttrLen);
1380 WLog_Print(log, g_LogLevel,
"}");
1383static void smartcard_trace_get_attrib_call(wLog* log,
const GetAttrib_Call* call)
1387 if (!WLog_IsLevelActive(log, g_LogLevel))
1390 WLog_Print(log, g_LogLevel,
"GetAttrib_Call {");
1391 smartcard_log_context(log, &call->handles.hContext);
1392 smartcard_log_redir_handle(log, &call->handles.hCard);
1394 WLog_Print(log, g_LogLevel,
1395 "dwAttrId: %s (0x%08" PRIX32
") fpbAttrIsNULL: %" PRId32
" cbAttrLen: 0x%08" PRIX32
1397 SCardGetAttributeString(call->dwAttrId), call->dwAttrId, call->fpbAttrIsNULL,
1399 WLog_Print(log, g_LogLevel,
"}");
1402static void smartcard_trace_status_call(wLog* log,
const Status_Call* call, BOOL unicode)
1406 if (!WLog_IsLevelActive(log, g_LogLevel))
1409 WLog_Print(log, g_LogLevel,
"Status%s_Call {", unicode ?
"W" :
"A");
1410 smartcard_log_context(log, &call->handles.hContext);
1411 smartcard_log_redir_handle(log, &call->handles.hCard);
1413 WLog_Print(log, g_LogLevel,
1414 "fmszReaderNamesIsNULL: %" PRId32
" cchReaderLen: %" PRIu32
" cbAtrLen: %" PRIu32
"",
1415 call->fmszReaderNamesIsNULL, call->cchReaderLen, call->cbAtrLen);
1416 WLog_Print(log, g_LogLevel,
"}");
1419static void smartcard_trace_status_return(wLog* log,
const Status_Return* ret, BOOL unicode)
1422 char* mszReaderNamesA =
nullptr;
1425 if (!WLog_IsLevelActive(log, g_LogLevel))
1427 cBytes = ret->cBytes;
1428 if (ret->ReturnCode != SCARD_S_SUCCESS)
1430 if (cBytes == SCARD_AUTOALLOCATE)
1432 mszReaderNamesA = smartcard_convert_string_list(ret->mszReaderNames, cBytes, unicode);
1434 WLog_Print(log, g_LogLevel,
"Status%s_Return {", unicode ?
"W" :
"A");
1435 WLog_Print(log, g_LogLevel,
" ReturnCode: %s (0x%08" PRIX32
")",
1436 SCardGetErrorString(ret->ReturnCode),
1437 WINPR_CXX_COMPAT_CAST(UINT32, ret->ReturnCode));
1438 WLog_Print(log, g_LogLevel,
" dwState: %s (0x%08" PRIX32
") dwProtocol: %s (0x%08" PRIX32
")",
1439 SCardGetCardStateString(ret->dwState), ret->dwState,
1440 SCardGetProtocolString(ret->dwProtocol), ret->dwProtocol);
1442 WLog_Print(log, g_LogLevel,
" cBytes: %" PRIu32
" mszReaderNames: %s", ret->cBytes,
1445 smartcard_dump_array(log, g_LogLevel,
" cbAtrLen: %" PRIu32
" pbAtr: ",
"", ret->pbAtr,
1446 ret->cbAtrLen, ret->cbAtrLen);
1447 WLog_Print(log, g_LogLevel,
"}");
1448 free(mszReaderNamesA);
1451static void smartcard_trace_state_return(wLog* log,
const State_Return* ret)
1454 char* state =
nullptr;
1456 if (!WLog_IsLevelActive(log, g_LogLevel))
1459 state = SCardGetReaderStateString(ret->dwState);
1460 WLog_Print(log, g_LogLevel,
"Reconnect_Return {");
1461 WLog_Print(log, g_LogLevel,
" ReturnCode: %s (0x%08" PRIX32
")",
1462 SCardGetErrorString(ret->ReturnCode),
1463 WINPR_CXX_COMPAT_CAST(UINT32, ret->ReturnCode));
1464 WLog_Print(log, g_LogLevel,
" dwState: %s (0x%08" PRIX32
")", state, ret->dwState);
1465 WLog_Print(log, g_LogLevel,
" dwProtocol: %s (0x%08" PRIX32
")",
1466 SCardGetProtocolString(ret->dwProtocol), ret->dwProtocol);
1467 WLog_Print(log, g_LogLevel,
" cbAtrLen: (0x%08" PRIX32
")", ret->cbAtrLen);
1468 smartcard_dump_array(log, g_LogLevel,
" rgAtr: ",
"", ret->rgAtr,
sizeof(ret->rgAtr));
1469 WLog_Print(log, g_LogLevel,
"}");
1473static void smartcard_trace_reconnect_return(wLog* log,
const Reconnect_Return* ret)
1477 if (!WLog_IsLevelActive(log, g_LogLevel))
1480 WLog_Print(log, g_LogLevel,
"Reconnect_Return {");
1481 WLog_Print(log, g_LogLevel,
" ReturnCode: %s (0x%08" PRIX32
")",
1482 SCardGetErrorString(ret->ReturnCode),
1483 WINPR_CXX_COMPAT_CAST(UINT32, ret->ReturnCode));
1484 WLog_Print(log, g_LogLevel,
" dwActiveProtocol: %s (0x%08" PRIX32
")",
1485 SCardGetProtocolString(ret->dwActiveProtocol), ret->dwActiveProtocol);
1486 WLog_Print(log, g_LogLevel,
"}");
1489static void smartcard_trace_connect_a_call(wLog* log,
const ConnectA_Call* call)
1493 if (!WLog_IsLevelActive(log, g_LogLevel))
1496 WLog_Print(log, g_LogLevel,
"ConnectA_Call {");
1497 smartcard_log_context(log, &call->Common.handles.hContext);
1499 WLog_Print(log, g_LogLevel,
1500 "szReader: %s dwShareMode: %s (0x%08" PRIX32
1501 ") dwPreferredProtocols: %s (0x%08" PRIX32
")",
1502 call->szReader, SCardGetShareModeString(call->Common.dwShareMode),
1503 call->Common.dwShareMode, SCardGetProtocolString(call->Common.dwPreferredProtocols),
1504 call->Common.dwPreferredProtocols);
1505 WLog_Print(log, g_LogLevel,
"}");
1508static void smartcard_trace_connect_w_call(wLog* log,
const ConnectW_Call* call)
1511 char szReaderA[1024] = WINPR_C_ARRAY_INIT;
1513 if (!WLog_IsLevelActive(log, g_LogLevel))
1517 (void)ConvertWCharToUtf8(call->szReader, szReaderA, ARRAYSIZE(szReaderA));
1518 WLog_Print(log, g_LogLevel,
"ConnectW_Call {");
1519 smartcard_log_context(log, &call->Common.handles.hContext);
1521 WLog_Print(log, g_LogLevel,
1522 "szReader: %s dwShareMode: %s (0x%08" PRIX32
1523 ") dwPreferredProtocols: %s (0x%08" PRIX32
")",
1524 szReaderA, SCardGetShareModeString(call->Common.dwShareMode),
1525 call->Common.dwShareMode, SCardGetProtocolString(call->Common.dwPreferredProtocols),
1526 call->Common.dwPreferredProtocols);
1527 WLog_Print(log, g_LogLevel,
"}");
1530static void smartcard_trace_hcard_and_disposition_call(wLog* log,
1536 if (!WLog_IsLevelActive(log, g_LogLevel))
1539 WLog_Print(log, g_LogLevel,
"%s_Call {", name);
1540 smartcard_log_context(log, &call->handles.hContext);
1541 smartcard_log_redir_handle(log, &call->handles.hCard);
1543 WLog_Print(log, g_LogLevel,
"dwDisposition: %s (0x%08" PRIX32
")",
1544 SCardGetDispositionString(call->dwDisposition), call->dwDisposition);
1545 WLog_Print(log, g_LogLevel,
"}");
1552 if (!WLog_IsLevelActive(log, g_LogLevel))
1555 WLog_Print(log, g_LogLevel,
"EstablishContext_Call {");
1556 WLog_Print(log, g_LogLevel,
"dwScope: %s (0x%08" PRIX32
")", SCardGetScopeString(call->dwScope),
1558 WLog_Print(log, g_LogLevel,
"}");
1565 if (!WLog_IsLevelActive(log, g_LogLevel))
1568 WLog_Print(log, g_LogLevel,
"EstablishContext_Return {");
1569 WLog_Print(log, g_LogLevel,
" ReturnCode: %s (0x%08" PRIX32
")",
1570 SCardGetErrorString(ret->ReturnCode),
1571 WINPR_CXX_COMPAT_CAST(UINT32, ret->ReturnCode));
1572 smartcard_log_context(log, &ret->hContext);
1574 WLog_Print(log, g_LogLevel,
"}");
1577void smartcard_trace_long_return(
const Long_Return* ret,
const char* name)
1579 wLog* log = scard_log();
1580 smartcard_trace_long_return_int(log, ret, name);
1583void smartcard_trace_long_return_int(wLog* log,
const Long_Return* ret,
const char* name)
1587 if (!WLog_IsLevelActive(log, g_LogLevel))
1590 WLog_Print(log, g_LogLevel,
"%s_Return {", name);
1591 WLog_Print(log, g_LogLevel,
" ReturnCode: %s (0x%08" PRIX32
")",
1592 SCardGetErrorString(ret->ReturnCode),
1593 WINPR_CXX_COMPAT_CAST(UINT32, ret->ReturnCode));
1594 WLog_Print(log, g_LogLevel,
"}");
1597static void smartcard_trace_connect_return(wLog* log,
const Connect_Return* ret)
1601 if (!WLog_IsLevelActive(log, g_LogLevel))
1604 WLog_Print(log, g_LogLevel,
"Connect_Return {");
1605 WLog_Print(log, g_LogLevel,
" ReturnCode: %s (0x%08" PRIX32
")",
1606 SCardGetErrorString(ret->ReturnCode),
1607 WINPR_CXX_COMPAT_CAST(UINT32, ret->ReturnCode));
1608 smartcard_log_context(log, &ret->hContext);
1609 smartcard_log_redir_handle(log, &ret->hCard);
1611 WLog_Print(log, g_LogLevel,
" dwActiveProtocol: %s (0x%08" PRIX32
")",
1612 SCardGetProtocolString(ret->dwActiveProtocol), ret->dwActiveProtocol);
1613 WLog_Print(log, g_LogLevel,
"}");
1616static void smartcard_trace_reconnect_call(wLog* log,
const Reconnect_Call* call)
1620 if (!WLog_IsLevelActive(log, g_LogLevel))
1623 WLog_Print(log, g_LogLevel,
"Reconnect_Call {");
1624 smartcard_log_context(log, &call->handles.hContext);
1625 smartcard_log_redir_handle(log, &call->handles.hCard);
1627 WLog_Print(log, g_LogLevel,
1628 "dwShareMode: %s (0x%08" PRIX32
") dwPreferredProtocols: %s (0x%08" PRIX32
1629 ") dwInitialization: %s (0x%08" PRIX32
")",
1630 SCardGetShareModeString(call->dwShareMode), call->dwShareMode,
1631 SCardGetProtocolString(call->dwPreferredProtocols), call->dwPreferredProtocols,
1632 SCardGetDispositionString(call->dwInitialization), call->dwInitialization);
1633 WLog_Print(log, g_LogLevel,
"}");
1640 if (!WLog_IsLevelActive(log, g_LogLevel))
1643 WLog_Print(log, g_LogLevel,
"GetDeviceTypeId_Return {");
1644 WLog_Print(log, g_LogLevel,
" ReturnCode: %s (0x%08" PRIX32
")",
1645 SCardGetErrorString(ret->ReturnCode),
1646 WINPR_CXX_COMPAT_CAST(UINT32, ret->ReturnCode));
1647 WLog_Print(log, g_LogLevel,
" dwDeviceId=%08" PRIx32, ret->dwDeviceId);
1649 WLog_Print(log, g_LogLevel,
"}");
1652WINPR_ATTR_NODISCARD
static LONG
1654 CHAR** pszReaderName)
1657 UINT32 pbContextNdrPtr = 0;
1658 LONG status = smartcard_unpack_redir_scard_context(log, s, phContext, &index, &pbContextNdrPtr);
1659 if (status != SCARD_S_SUCCESS)
1662 if (!smartcard_ndr_pointer_read(log, s, &index,
nullptr))
1663 return ERROR_INVALID_DATA;
1665 status = smartcard_unpack_redir_scard_context_ref(log, s, pbContextNdrPtr, phContext);
1666 if (status != SCARD_S_SUCCESS)
1669 status = smartcard_ndr_read_a(log, s, pszReaderName, NDR_PTR_FULL);
1670 if (status != SCARD_S_SUCCESS)
1673 smartcard_trace_context_and_string_call_a(log, __func__, phContext, *pszReaderName);
1674 return SCARD_S_SUCCESS;
1677WINPR_ATTR_NODISCARD
static LONG
1679 WCHAR** pszReaderName)
1682 UINT32 pbContextNdrPtr = 0;
1684 LONG status = smartcard_unpack_redir_scard_context(log, s, phContext, &index, &pbContextNdrPtr);
1685 if (status != SCARD_S_SUCCESS)
1688 if (!smartcard_ndr_pointer_read(log, s, &index,
nullptr))
1689 return ERROR_INVALID_DATA;
1691 status = smartcard_unpack_redir_scard_context_ref(log, s, pbContextNdrPtr, phContext);
1692 if (status != SCARD_S_SUCCESS)
1695 status = smartcard_ndr_read_w(log, s, pszReaderName, NDR_PTR_FULL);
1696 if (status != SCARD_S_SUCCESS)
1699 smartcard_trace_context_and_string_call_w(log, __func__, phContext, *pszReaderName);
1700 return SCARD_S_SUCCESS;
1703LONG smartcard_unpack_common_type_header(
wStream* s)
1705 wLog* log = scard_log();
1708 UINT8 endianness = 0;
1709 UINT16 commonHeaderLength = 0;
1711 if (!Stream_CheckAndLogRequiredLengthWLog(log, s, 8))
1712 return STATUS_BUFFER_TOO_SMALL;
1715 Stream_Read_UINT8(s, version);
1716 Stream_Read_UINT8(s, endianness);
1717 Stream_Read_UINT16(s, commonHeaderLength);
1718 Stream_Read_UINT32(s, filler);
1722 WLog_Print(log, WLOG_WARN,
"Unsupported CommonTypeHeader Version %" PRIu8
"", version);
1723 return STATUS_INVALID_PARAMETER;
1726 if (endianness != 0x10)
1728 WLog_Print(log, WLOG_WARN,
"Unsupported CommonTypeHeader Endianness %" PRIu8
"",
1730 return STATUS_INVALID_PARAMETER;
1733 if (commonHeaderLength != 8)
1735 WLog_Print(log, WLOG_WARN,
"Unsupported CommonTypeHeader CommonHeaderLength %" PRIu16
"",
1736 commonHeaderLength);
1737 return STATUS_INVALID_PARAMETER;
1740 if (filler != 0xCCCCCCCC)
1742 WLog_Print(log, WLOG_WARN,
"Unexpected CommonTypeHeader Filler 0x%08" PRIX32
"", filler);
1743 return STATUS_INVALID_PARAMETER;
1746 return SCARD_S_SUCCESS;
1749void smartcard_pack_common_type_header(
wStream* s)
1751 Stream_Write_UINT8(s, 1);
1752 Stream_Write_UINT8(s, 0x10);
1753 Stream_Write_UINT16(s, 8);
1754 Stream_Write_UINT32(s, 0xCCCCCCCC);
1757LONG smartcard_unpack_private_type_header(
wStream* s)
1759 wLog* log = scard_log();
1761 UINT32 objectBufferLength = 0;
1763 if (!Stream_CheckAndLogRequiredLengthWLog(log, s, 8))
1764 return STATUS_BUFFER_TOO_SMALL;
1766 Stream_Read_UINT32(s, objectBufferLength);
1767 Stream_Read_UINT32(s, filler);
1769 if (filler != 0x00000000)
1771 WLog_Print(log, WLOG_WARN,
"Unexpected PrivateTypeHeader Filler 0x%08" PRIX32
"", filler);
1772 return STATUS_INVALID_PARAMETER;
1775 if (!Stream_CheckAndLogRequiredLengthWLog(log, s, objectBufferLength))
1776 return STATUS_INVALID_PARAMETER;
1778 return SCARD_S_SUCCESS;
1781void smartcard_pack_private_type_header(
wStream* s, UINT32 objectBufferLength)
1783 Stream_Write_UINT32(s, objectBufferLength);
1784 Stream_Write_UINT32(s, 0x00000000);
1787LONG smartcard_unpack_read_size_align(
wStream* s,
size_t size, UINT32 alignment)
1789 const size_t padsize = (size + alignment - 1) & ~(alignment - 1);
1790 const size_t pad = padsize - size;
1794 if (!Stream_SafeSeek(s, pad))
1801LONG smartcard_pack_write_size_align(
wStream* s,
size_t size, UINT32 alignment)
1803 const size_t padsize = (size + alignment - 1) & ~(alignment - 1);
1804 const size_t pad = padsize - size;
1808 if (!Stream_EnsureRemainingCapacity(s, pad))
1810 wLog* log = scard_log();
1811 WLog_Print(log, WLOG_ERROR,
"Stream_EnsureRemainingCapacity failed!");
1812 return SCARD_F_INTERNAL_ERROR;
1815 Stream_Zero(s, pad);
1818 return SCARD_S_SUCCESS;
1821SCARDCONTEXT smartcard_scard_context_native_from_redir(
const REDIR_SCARDCONTEXT* context)
1823 SCARDCONTEXT hContext = WINPR_C_ARRAY_INIT;
1825 WINPR_ASSERT(context);
1826 if ((context->cbContext !=
sizeof(ULONG_PTR)) && (context->cbContext != 0))
1828 wLog* log = scard_log();
1829 WLog_Print(log, WLOG_WARN,
1830 "REDIR_SCARDCONTEXT does not match native size: Actual: %" PRIu32
1831 ", Expected: %" PRIuz
"",
1832 context->cbContext,
sizeof(ULONG_PTR));
1836 if (context->cbContext)
1837 CopyMemory(&hContext, &(context->pbContext), context->cbContext);
1842void smartcard_scard_context_native_to_redir(
REDIR_SCARDCONTEXT* context, SCARDCONTEXT hContext)
1844 WINPR_ASSERT(context);
1846 context->cbContext =
sizeof(ULONG_PTR);
1847 CopyMemory(&(context->pbContext), &hContext, context->cbContext);
1850SCARDHANDLE smartcard_scard_handle_native_from_redir(
const REDIR_SCARDHANDLE* handle)
1852 SCARDHANDLE hCard = 0;
1854 WINPR_ASSERT(handle);
1855 if (handle->cbHandle == 0)
1858 if (handle->cbHandle !=
sizeof(ULONG_PTR))
1860 wLog* log = scard_log();
1861 WLog_Print(log, WLOG_WARN,
1862 "REDIR_SCARDHANDLE does not match native size: Actual: %" PRIu32
1863 ", Expected: %" PRIuz
"",
1864 handle->cbHandle,
sizeof(ULONG_PTR));
1868 if (handle->cbHandle)
1869 CopyMemory(&hCard, &(handle->pbHandle), handle->cbHandle);
1874void smartcard_scard_handle_native_to_redir(
REDIR_SCARDHANDLE* handle, SCARDHANDLE hCard)
1876 WINPR_ASSERT(handle);
1878 handle->cbHandle =
sizeof(ULONG_PTR);
1879 CopyMemory(&(handle->pbHandle), &hCard, handle->cbHandle);
1882#define smartcard_context_supported(log, size) \
1883 smartcard_context_supported_((log), (size), __FILE__, __func__, __LINE__)
1884WINPR_ATTR_NODISCARD
static LONG smartcard_context_supported_(wLog* log, uint32_t size,
1885 const char* file,
const char* fkt,
1893 return SCARD_S_SUCCESS;
1896 const uint32_t level = WLOG_WARN;
1897 if (WLog_IsLevelActive(log, level))
1899 WLog_PrintTextMessage(log, level, line, file, fkt,
1900 "REDIR_SCARDCONTEXT length is not 0, 4 or 8: %" PRIu32
"",
1903 return STATUS_INVALID_PARAMETER;
1909 UINT32* index, UINT32* ppbContextNdrPtr,
1910 const char* file,
const char* function,
size_t line)
1912 UINT32 pbContextNdrPtr = 0;
1915 WINPR_ASSERT(context);
1919 if (!Stream_CheckAndLogRequiredLengthWLogEx(log, WLOG_WARN, s, 4, 1,
"%s(%s:%" PRIuz
")", file,
1921 return STATUS_BUFFER_TOO_SMALL;
1923 const LONG status = smartcard_context_supported_(log, context->cbContext, file, function, line);
1924 if (status != SCARD_S_SUCCESS)
1927 Stream_Read_UINT32(s, context->cbContext);
1929 if (!smartcard_ndr_pointer_read_(log, s, index, &pbContextNdrPtr, file, function, line))
1930 return ERROR_INVALID_DATA;
1932 if (((context->cbContext == 0) && pbContextNdrPtr) ||
1933 ((context->cbContext != 0) && !pbContextNdrPtr))
1935 WLog_Print(log, WLOG_WARN,
1936 "REDIR_SCARDCONTEXT cbContext (%" PRIu32
") pbContextNdrPtr (%" PRIu32
1938 context->cbContext, pbContextNdrPtr);
1939 return STATUS_INVALID_PARAMETER;
1942 *ppbContextNdrPtr = pbContextNdrPtr;
1943 return SCARD_S_SUCCESS;
1946LONG smartcard_pack_redir_scard_context(WINPR_ATTR_UNUSED wLog* log,
wStream* s,
1949 const UINT32 pbContextNdrPtr = 0x00020000 + *index * 4;
1951 WINPR_ASSERT(context);
1952 if (context->cbContext != 0)
1954 Stream_Write_UINT32(s, context->cbContext);
1955 Stream_Write_UINT32(s, pbContextNdrPtr);
1956 *index = *index + 1;
1961 return SCARD_S_SUCCESS;
1964LONG smartcard_unpack_redir_scard_context_ref(wLog* log,
wStream* s,
1965 WINPR_ATTR_UNUSED UINT32 pbContextNdrPtr,
1970 WINPR_ASSERT(context);
1972 if (context->cbContext == 0)
1973 return SCARD_S_SUCCESS;
1975 if (!Stream_CheckAndLogRequiredLengthWLog(log, s, 4))
1976 return STATUS_BUFFER_TOO_SMALL;
1978 Stream_Read_UINT32(s, length);
1980 if (length != context->cbContext)
1982 WLog_Print(log, WLOG_WARN,
1983 "REDIR_SCARDCONTEXT length (%" PRIu32
") cbContext (%" PRIu32
") mismatch",
1984 length, context->cbContext);
1985 return STATUS_INVALID_PARAMETER;
1988 const LONG status = smartcard_context_supported(log, context->cbContext);
1989 if (status != SCARD_S_SUCCESS)
1992 if (!Stream_CheckAndLogRequiredLengthWLog(log, s, context->cbContext))
1993 return STATUS_BUFFER_TOO_SMALL;
1995 if (context->cbContext)
1996 Stream_Read(s, &(context->pbContext), context->cbContext);
1998 ZeroMemory(&(context->pbContext),
sizeof(context->pbContext));
2000 return SCARD_S_SUCCESS;
2003LONG smartcard_pack_redir_scard_context_ref(WINPR_ATTR_UNUSED wLog* log,
wStream* s,
2006 WINPR_ASSERT(context);
2008 if (context->cbContext == 0)
2009 return SCARD_S_SUCCESS;
2011 Stream_Write_UINT32(s, context->cbContext);
2012 Stream_Write(s, &(context->pbContext), context->cbContext);
2014 return SCARD_S_SUCCESS;
2018 UINT32* index,
const char* file,
const char* function,
2021 UINT32 pbHandleNdrPtr = 0;
2023 WINPR_ASSERT(handle);
2026 if (!Stream_CheckAndLogRequiredLengthWLog(log, s, 4))
2027 return STATUS_BUFFER_TOO_SMALL;
2029 Stream_Read_UINT32(s, handle->cbHandle);
2031 if (!smartcard_ndr_pointer_read_(log, s, index, &pbHandleNdrPtr, file, function, line))
2032 return ERROR_INVALID_DATA;
2034 if (((handle->cbHandle == 0) && pbHandleNdrPtr) ||
2035 ((handle->cbHandle != 0) && !pbHandleNdrPtr))
2037 WLog_Print(log, WLOG_WARN,
2038 "REDIR_SCARDHANDLE cbHandle (%" PRIu32
") pbHandleNdrPtr (%" PRIu32
2040 handle->cbHandle, pbHandleNdrPtr);
2041 return STATUS_INVALID_PARAMETER;
2044 return SCARD_S_SUCCESS;
2047LONG smartcard_pack_redir_scard_handle(WINPR_ATTR_UNUSED wLog* log,
wStream* s,
2050 const UINT32 pbContextNdrPtr = 0x00020000 + *index * 4;
2052 WINPR_ASSERT(handle);
2053 if (handle->cbHandle != 0)
2055 Stream_Write_UINT32(s, handle->cbHandle);
2056 Stream_Write_UINT32(s, pbContextNdrPtr);
2057 *index = *index + 1;
2061 return SCARD_S_SUCCESS;
2068 WINPR_ASSERT(handle);
2070 if (handle->cbHandle == 0)
2071 return SCARD_S_SUCCESS;
2073 if (!Stream_CheckAndLogRequiredLengthWLog(log, s, 4))
2074 return STATUS_BUFFER_TOO_SMALL;
2076 Stream_Read_UINT32(s, length);
2078 if (length != handle->cbHandle)
2080 WLog_Print(log, WLOG_WARN,
2081 "REDIR_SCARDHANDLE length (%" PRIu32
") cbHandle (%" PRIu32
") mismatch", length,
2083 return STATUS_INVALID_PARAMETER;
2086 if ((handle->cbHandle != 4) && (handle->cbHandle != 8))
2088 WLog_Print(log, WLOG_WARN,
"REDIR_SCARDHANDLE length is not 4 or 8: %" PRIu32
"",
2090 return STATUS_INVALID_PARAMETER;
2093 if (!Stream_CheckAndLogRequiredLengthWLog(log, s, handle->cbHandle))
2094 return STATUS_BUFFER_TOO_SMALL;
2096 if (handle->cbHandle)
2097 Stream_Read(s, &(handle->pbHandle), handle->cbHandle);
2099 return SCARD_S_SUCCESS;
2102LONG smartcard_pack_redir_scard_handle_ref(WINPR_ATTR_UNUSED wLog* log,
wStream* s,
2105 WINPR_ASSERT(handle);
2107 if (handle->cbHandle == 0)
2108 return SCARD_S_SUCCESS;
2110 Stream_Write_UINT32(s, handle->cbHandle);
2111 Stream_Write(s, &(handle->pbHandle), handle->cbHandle);
2113 return SCARD_S_SUCCESS;
2119 wLog* log = scard_log();
2121 if (!Stream_CheckAndLogRequiredLengthWLog(log, s, 4))
2122 return STATUS_BUFFER_TOO_SMALL;
2124 Stream_Read_UINT32(s, call->dwScope);
2125 smartcard_trace_establish_context_call(log, call);
2126 return SCARD_S_SUCCESS;
2132 wLog* log = scard_log();
2134 smartcard_trace_establish_context_call(log, call);
2136 if (!Stream_EnsureRemainingCapacity(s, 4))
2137 return SCARD_E_NO_MEMORY;
2139 Stream_Write_UINT32(s, call->dwScope);
2141 return SCARD_S_SUCCESS;
2147 wLog* log = scard_log();
2151 smartcard_trace_establish_context_return(log, ret);
2152 if (ret->ReturnCode != SCARD_S_SUCCESS)
2153 return ret->ReturnCode;
2155 status = smartcard_pack_redir_scard_context(log, s, &(ret->hContext), &index);
2156 if (status != SCARD_S_SUCCESS)
2159 return smartcard_pack_redir_scard_context_ref(log, s, &(ret->hContext));
2165 wLog* log = scard_log();
2167 UINT32 pbContextNdrPtr = 0;
2170 smartcard_unpack_redir_scard_context(log, s, &(ret->hContext), &index, &pbContextNdrPtr);
2171 if (status != SCARD_S_SUCCESS)
2174 status = smartcard_unpack_redir_scard_context_ref(log, s, pbContextNdrPtr, &(ret->hContext));
2175 if (status != SCARD_S_SUCCESS)
2178 smartcard_trace_establish_context_return(log, ret);
2179 return SCARD_S_SUCCESS;
2185 UINT32 pbContextNdrPtr = 0;
2186 wLog* log = scard_log();
2189 LONG status = smartcard_unpack_redir_scard_context(log, s, &(call->handles.hContext), &index,
2191 if (status != SCARD_S_SUCCESS)
2194 status = smartcard_unpack_redir_scard_context_ref(log, s, pbContextNdrPtr,
2195 &(call->handles.hContext));
2196 if (status != SCARD_S_SUCCESS)
2197 WLog_Print(log, WLOG_ERROR,
2198 "smartcard_unpack_redir_scard_context_ref failed with error %" PRId32
"",
2201 smartcard_trace_context_call(log, call, name);
2208 UINT32 pbContextNdrPtr = 0;
2209 wLog* log = scard_log();
2212 LONG status = smartcard_unpack_redir_scard_context(log, s, &(call->handles.hContext), &index,
2215 if (status != SCARD_S_SUCCESS)
2218 if (!Stream_CheckAndLogRequiredLengthWLog(log, s, 8))
2219 return STATUS_BUFFER_TOO_SMALL;
2221 Stream_Read_INT32(s, call->fmszGroupsIsNULL);
2222 Stream_Read_UINT32(s, call->cchGroups);
2223 status = smartcard_unpack_redir_scard_context_ref(log, s, pbContextNdrPtr,
2224 &(call->handles.hContext));
2226 if (status != SCARD_S_SUCCESS)
2229 smartcard_trace_list_reader_groups_call(log, call, unicode);
2230 return SCARD_S_SUCCESS;
2237 wLog* log = scard_log();
2240 smartcard_trace_list_reader_groups_call(log, call, unicode);
2242 LONG status = smartcard_pack_redir_scard_context(log, s, &call->handles.hContext, &index);
2243 if (status != SCARD_S_SUCCESS)
2246 if (!Stream_EnsureRemainingCapacity(s, 8))
2247 return SCARD_E_NO_MEMORY;
2249 Stream_Write_INT32(s, call->fmszGroupsIsNULL);
2250 Stream_Write_UINT32(s, call->cchGroups);
2252 return smartcard_pack_redir_scard_context_ref(log, s, &call->handles.hContext);
2259 wLog* log = scard_log();
2261 UINT32 cBytes = ret->cBytes;
2264 smartcard_trace_list_reader_groups_return(log, ret, unicode);
2265 if (ret->ReturnCode != SCARD_S_SUCCESS)
2267 if (cBytes == SCARD_AUTOALLOCATE)
2270 if (!Stream_EnsureRemainingCapacity(s, 4))
2271 return SCARD_E_NO_MEMORY;
2273 Stream_Write_UINT32(s, cBytes);
2274 if (!smartcard_ndr_pointer_write(s, &index, cBytes))
2275 return SCARD_E_NO_MEMORY;
2277 status = smartcard_ndr_write(s, ret->msz, cBytes, 1, NDR_PTR_SIMPLE);
2278 if (status != SCARD_S_SUCCESS)
2280 return ret->ReturnCode;
2287 wLog* log = scard_log();
2289 UINT32 mszNdrPtr = 0;
2291 if (!Stream_CheckAndLogRequiredLengthWLog(log, s, 4))
2292 return STATUS_BUFFER_TOO_SMALL;
2294 const UINT32 cBytes = Stream_Get_UINT32(s);
2296 if (!smartcard_ndr_pointer_read(log, s, &index, &mszNdrPtr))
2297 return ERROR_INVALID_DATA;
2301 LONG status = smartcard_ndr_read(log, s, &ret->msz, cBytes, 1, NDR_PTR_SIMPLE);
2302 if (status != SCARD_S_SUCCESS)
2304 ret->cBytes = cBytes;
2307 smartcard_trace_list_reader_groups_return(log, ret, unicode);
2308 return SCARD_S_SUCCESS;
2314 UINT32 mszGroupsNdrPtr = 0;
2315 UINT32 pbContextNdrPtr = 0;
2316 wLog* log = scard_log();
2319 call->mszGroups =
nullptr;
2321 LONG status = smartcard_unpack_redir_scard_context(log, s, &(call->handles.hContext), &index,
2323 if (status != SCARD_S_SUCCESS)
2326 if (!Stream_CheckAndLogRequiredLengthWLog(log, s, 4))
2327 return STATUS_BUFFER_TOO_SMALL;
2329 const UINT32 cBytes = Stream_Get_UINT32(s);
2330 if (!smartcard_ndr_pointer_read(log, s, &index, &mszGroupsNdrPtr))
2331 return ERROR_INVALID_DATA;
2333 if (!Stream_CheckAndLogRequiredLengthWLog(log, s, 8))
2334 return STATUS_BUFFER_TOO_SMALL;
2335 Stream_Read_INT32(s, call->fmszReadersIsNULL);
2336 Stream_Read_UINT32(s, call->cchReaders);
2338 status = smartcard_unpack_redir_scard_context_ref(log, s, pbContextNdrPtr,
2339 &(call->handles.hContext));
2340 if (status != SCARD_S_SUCCESS)
2343 if (mszGroupsNdrPtr)
2345 status = smartcard_ndr_read(log, s, &call->mszGroups, cBytes, 1, NDR_PTR_SIMPLE);
2346 if (status != SCARD_S_SUCCESS)
2348 call->cBytes = cBytes;
2351 smartcard_trace_list_readers_call(log, call, unicode);
2352 return SCARD_S_SUCCESS;
2358 wLog* log = scard_log();
2361 UINT32 size = ret->cBytes;
2363 smartcard_trace_list_readers_return(log, ret, unicode);
2364 if (ret->ReturnCode != SCARD_S_SUCCESS)
2367 if (!Stream_EnsureRemainingCapacity(s, 4))
2369 WLog_Print(log, WLOG_ERROR,
"Stream_EnsureRemainingCapacity failed!");
2370 return SCARD_F_INTERNAL_ERROR;
2373 Stream_Write_UINT32(s, size);
2374 if (!smartcard_ndr_pointer_write(s, &index, size))
2375 return SCARD_E_NO_MEMORY;
2377 status = smartcard_ndr_write(s, ret->msz, size, 1, NDR_PTR_SIMPLE);
2378 if (status != SCARD_S_SUCCESS)
2380 return ret->ReturnCode;
2383WINPR_ATTR_NODISCARD
static LONG smartcard_unpack_connect_common(wLog* log,
wStream* s,
2386 UINT32* ppbContextNdrPtr)
2388 WINPR_ASSERT(common);
2389 LONG status = smartcard_unpack_redir_scard_context(log, s, &(common->handles.hContext), index,
2391 if (status != SCARD_S_SUCCESS)
2394 if (!Stream_CheckAndLogRequiredLengthWLog(log, s, 8))
2395 return STATUS_BUFFER_TOO_SMALL;
2397 Stream_Read_UINT32(s, common->dwShareMode);
2398 Stream_Read_UINT32(s, common->dwPreferredProtocols);
2399 return SCARD_S_SUCCESS;
2406 UINT32 pbContextNdrPtr = 0;
2409 wLog* log = scard_log();
2411 call->szReader =
nullptr;
2413 if (!smartcard_ndr_pointer_read(log, s, &index,
nullptr))
2414 return ERROR_INVALID_DATA;
2416 status = smartcard_unpack_connect_common(log, s, &(call->Common), &index, &pbContextNdrPtr);
2417 if (status != SCARD_S_SUCCESS)
2419 WLog_Print(log, WLOG_ERROR,
"smartcard_unpack_connect_common failed with error %" PRId32
"",
2424 status = smartcard_ndr_read_a(log, s, &call->szReader, NDR_PTR_FULL);
2425 if (status != SCARD_S_SUCCESS)
2428 status = smartcard_unpack_redir_scard_context_ref(log, s, pbContextNdrPtr,
2429 &(call->Common.handles.hContext));
2430 if (status != SCARD_S_SUCCESS)
2431 WLog_Print(log, WLOG_ERROR,
2432 "smartcard_unpack_redir_scard_context_ref failed with error %" PRId32
"",
2435 smartcard_trace_connect_a_call(log, call);
2443 UINT32 pbContextNdrPtr = 0;
2446 wLog* log = scard_log();
2447 call->szReader =
nullptr;
2449 if (!smartcard_ndr_pointer_read(log, s, &index,
nullptr))
2450 return ERROR_INVALID_DATA;
2452 status = smartcard_unpack_connect_common(log, s, &(call->Common), &index, &pbContextNdrPtr);
2453 if (status != SCARD_S_SUCCESS)
2455 WLog_Print(log, WLOG_ERROR,
"smartcard_unpack_connect_common failed with error %" PRId32
"",
2460 status = smartcard_ndr_read_w(log, s, &call->szReader, NDR_PTR_FULL);
2461 if (status != SCARD_S_SUCCESS)
2464 status = smartcard_unpack_redir_scard_context_ref(log, s, pbContextNdrPtr,
2465 &(call->Common.handles.hContext));
2466 if (status != SCARD_S_SUCCESS)
2467 WLog_Print(log, WLOG_ERROR,
2468 "smartcard_unpack_redir_scard_context_ref failed with error %" PRId32
"",
2471 smartcard_trace_connect_w_call(log, call);
2481 wLog* log = scard_log();
2482 smartcard_trace_connect_return(log, ret);
2484 status = smartcard_pack_redir_scard_context(log, s, &ret->hContext, &index);
2485 if (status != SCARD_S_SUCCESS)
2488 status = smartcard_pack_redir_scard_handle(log, s, &ret->hCard, &index);
2489 if (status != SCARD_S_SUCCESS)
2492 if (!Stream_EnsureRemainingCapacity(s, 4))
2493 return SCARD_E_NO_MEMORY;
2495 Stream_Write_UINT32(s, ret->dwActiveProtocol);
2496 status = smartcard_pack_redir_scard_context_ref(log, s, &ret->hContext);
2497 if (status != SCARD_S_SUCCESS)
2499 return smartcard_pack_redir_scard_handle_ref(log, s, &(ret->hCard));
2505 UINT32 pbContextNdrPtr = 0;
2508 wLog* log = scard_log();
2509 LONG status = smartcard_unpack_redir_scard_context(log, s, &(call->handles.hContext), &index,
2511 if (status != SCARD_S_SUCCESS)
2514 status = smartcard_unpack_redir_scard_handle(log, s, &(call->handles.hCard), &index);
2515 if (status != SCARD_S_SUCCESS)
2518 if (!Stream_CheckAndLogRequiredLengthWLog(log, s, 12))
2519 return STATUS_BUFFER_TOO_SMALL;
2521 Stream_Read_UINT32(s, call->dwShareMode);
2522 Stream_Read_UINT32(s, call->dwPreferredProtocols);
2523 Stream_Read_UINT32(s, call->dwInitialization);
2525 status = smartcard_unpack_redir_scard_context_ref(log, s, pbContextNdrPtr,
2526 &(call->handles.hContext));
2527 if (status != SCARD_S_SUCCESS)
2529 WLog_Print(log, WLOG_ERROR,
2530 "smartcard_unpack_redir_scard_context_ref failed with error %" PRId32
"",
2535 status = smartcard_unpack_redir_scard_handle_ref(log, s, &(call->handles.hCard));
2536 if (status != SCARD_S_SUCCESS)
2537 WLog_Print(log, WLOG_ERROR,
2538 "smartcard_unpack_redir_scard_handle_ref failed with error %" PRId32
"", status);
2540 smartcard_trace_reconnect_call(log, call);
2547 wLog* log = scard_log();
2550 smartcard_trace_reconnect_call(log, call);
2552 LONG status = smartcard_pack_redir_scard_context(log, s, &call->handles.hContext, &index);
2553 if (status != SCARD_S_SUCCESS)
2556 status = smartcard_pack_redir_scard_handle(log, s, &call->handles.hCard, &index);
2557 if (status != SCARD_S_SUCCESS)
2560 if (!Stream_EnsureRemainingCapacity(s, 12))
2561 return SCARD_E_NO_MEMORY;
2563 Stream_Write_UINT32(s, call->dwShareMode);
2564 Stream_Write_UINT32(s, call->dwPreferredProtocols);
2565 Stream_Write_UINT32(s, call->dwInitialization);
2567 status = smartcard_pack_redir_scard_context_ref(log, s, &call->handles.hContext);
2568 if (status != SCARD_S_SUCCESS)
2571 return smartcard_pack_redir_scard_handle_ref(log, s, &call->handles.hCard);
2577 wLog* log = scard_log();
2578 smartcard_trace_reconnect_return(log, ret);
2580 if (!Stream_EnsureRemainingCapacity(s, 4))
2581 return SCARD_E_NO_MEMORY;
2582 Stream_Write_UINT32(s, ret->dwActiveProtocol);
2583 return ret->ReturnCode;
2589 wLog* log = scard_log();
2591 if (!Stream_CheckAndLogRequiredLengthWLog(log, s, 4))
2592 return STATUS_BUFFER_TOO_SMALL;
2594 Stream_Read_UINT32(s, ret->dwActiveProtocol);
2596 smartcard_trace_reconnect_return(log, ret);
2597 return SCARD_S_SUCCESS;
2604 UINT32 pbContextNdrPtr = 0;
2607 wLog* log = scard_log();
2609 LONG status = smartcard_unpack_redir_scard_context(log, s, &(call->handles.hContext), &index,
2611 if (status != SCARD_S_SUCCESS)
2614 status = smartcard_unpack_redir_scard_handle(log, s, &(call->handles.hCard), &index);
2615 if (status != SCARD_S_SUCCESS)
2618 if (!Stream_CheckAndLogRequiredLengthWLog(log, s, 4))
2619 return STATUS_BUFFER_TOO_SMALL;
2621 Stream_Read_UINT32(s, call->dwDisposition);
2623 status = smartcard_unpack_redir_scard_context_ref(log, s, pbContextNdrPtr,
2624 &(call->handles.hContext));
2625 if (status != SCARD_S_SUCCESS)
2628 status = smartcard_unpack_redir_scard_handle_ref(log, s, &(call->handles.hCard));
2629 if (status != SCARD_S_SUCCESS)
2632 smartcard_trace_hcard_and_disposition_call(log, call, name);
2640 if (!WLog_IsLevelActive(log, g_LogLevel))
2643 WLog_Print(log, g_LogLevel,
"GetStatusChangeA_Call {");
2644 smartcard_log_context(log, &call->handles.hContext);
2646 WLog_Print(log, g_LogLevel,
"dwTimeOut: 0x%08" PRIX32
" cReaders: %" PRIu32
"", call->dwTimeOut,
2649 dump_reader_states_a(log, call->rgReaderStates, call->cReaders);
2651 WLog_Print(log, g_LogLevel,
"}");
2654WINPR_ATTR_NODISCARD
static LONG smartcard_unpack_reader_state_a(wLog* log,
wStream* s,
2656 UINT32 cReaders, UINT32* ptrIndex)
2658 LONG status = SCARD_E_NO_MEMORY;
2660 WINPR_ASSERT(ppcReaders || (cReaders == 0));
2661 if (cReaders > UINT8_MAX)
2663 WLog_Print(log, WLOG_ERROR,
2664 "Too many readers(%" PRIu32
"), rejecting request. Limited to <= %d", cReaders,
2666 return SCARD_E_WRITE_TOO_MANY;
2669 if (!Stream_CheckAndLogRequiredLengthWLog(log, s, 4))
2672 const UINT32 len = Stream_Get_UINT32(s);
2673 if (len != cReaders)
2675 WLog_Print(log, WLOG_ERROR,
"Count mismatch when reading LPSCARD_READERSTATEA");
2681 BOOL* states = calloc(cReaders,
sizeof(BOOL));
2682 if (!rgReaderStates || !states)
2684 status = ERROR_INVALID_DATA;
2686 for (UINT32 index = 0; index < cReaders; index++)
2688 UINT32 ptr = UINT32_MAX;
2691 if (!Stream_CheckAndLogRequiredLengthWLog(log, s, 52))
2694 if (!smartcard_ndr_pointer_read(log, s, ptrIndex, &ptr))
2700 states[index] = ptr != 0;
2701 Stream_Read_UINT32(s, readerState->dwCurrentState);
2702 Stream_Read_UINT32(s, readerState->dwEventState);
2703 Stream_Read_UINT32(s, readerState->cbAtr);
2704 if (readerState->cbAtr > ARRAYSIZE(readerState->rgbAtr))
2706 WLog_Print(log, WLOG_ERROR,
2707 "SCARD_READERSTATEA[%" PRIu32
"]::cbAtr %" PRIu32
" exceeds %" PRIuz, index,
2708 readerState->cbAtr, (
size_t)ARRAYSIZE(readerState->rgbAtr));
2711 Stream_Read(s, readerState->rgbAtr, 36);
2714 for (UINT32 index = 0; index < cReaders; index++)
2721 status = smartcard_ndr_read_a(log, s, &readerState->szReader, NDR_PTR_FULL);
2722 if (status != SCARD_S_SUCCESS)
2724 if (!readerState->szReader)
2728 *ppcReaders = rgReaderStates;
2730 return SCARD_S_SUCCESS;
2734 for (UINT32 index = 0; index < cReaders; index++)
2737 free(readerState->szReader);
2740 free(rgReaderStates);
2745WINPR_ATTR_NODISCARD
static LONG smartcard_unpack_reader_state_w(wLog* log,
wStream* s,
2747 UINT32 cReaders, UINT32* ptrIndex)
2749 LONG status = SCARD_E_NO_MEMORY;
2751 WINPR_ASSERT(ppcReaders || (cReaders == 0));
2753 if (cReaders > UINT8_MAX)
2755 WLog_Print(log, WLOG_ERROR,
2756 "Too many readers(%" PRIu32
"), rejecting request. Limited to <= %d", cReaders,
2758 return SCARD_E_WRITE_TOO_MANY;
2761 if (!Stream_CheckAndLogRequiredLengthWLog(log, s, 4))
2764 const UINT32 len = Stream_Get_UINT32(s);
2765 if (len != cReaders)
2767 WLog_Print(log, WLOG_ERROR,
"Count mismatch when reading LPSCARD_READERSTATEW");
2773 BOOL* states = calloc(cReaders,
sizeof(BOOL));
2775 if (!rgReaderStates || !states)
2778 status = ERROR_INVALID_DATA;
2779 for (UINT32 index = 0; index < cReaders; index++)
2781 UINT32 ptr = UINT32_MAX;
2784 if (!Stream_CheckAndLogRequiredLengthWLog(log, s, 52))
2787 if (!smartcard_ndr_pointer_read(log, s, ptrIndex, &ptr))
2793 states[index] = ptr != 0;
2794 Stream_Read_UINT32(s, readerState->dwCurrentState);
2795 Stream_Read_UINT32(s, readerState->dwEventState);
2796 Stream_Read_UINT32(s, readerState->cbAtr);
2797 if (readerState->cbAtr > ARRAYSIZE(readerState->rgbAtr))
2799 WLog_Print(log, WLOG_ERROR,
2800 "SCARD_READERSTATEW[%" PRIu32
"]::cbAtr %" PRIu32
" exceeds %" PRIuz, index,
2801 readerState->cbAtr, (
size_t)ARRAYSIZE(readerState->rgbAtr));
2804 Stream_Read(s, readerState->rgbAtr, 36);
2807 for (UINT32 index = 0; index < cReaders; index++)
2815 status = smartcard_ndr_read_w(log, s, &readerState->szReader, NDR_PTR_FULL);
2816 if (status != SCARD_S_SUCCESS)
2818 if (!readerState->szReader)
2822 *ppcReaders = rgReaderStates;
2824 return SCARD_S_SUCCESS;
2828 for (UINT32 index = 0; index < cReaders; index++)
2831 free(readerState->szReader);
2834 free(rgReaderStates);
2847 UINT32 pbContextNdrPtr = 0;
2850 wLog* log = scard_log();
2852 call->rgReaderStates =
nullptr;
2854 LONG status = smartcard_unpack_redir_scard_context(log, s, &(call->handles.hContext), &index,
2856 if (status != SCARD_S_SUCCESS)
2859 if (!Stream_CheckAndLogRequiredLengthWLog(log, s, 8))
2860 return STATUS_BUFFER_TOO_SMALL;
2862 Stream_Read_UINT32(s, call->dwTimeOut);
2863 const UINT32 cReaders = Stream_Get_UINT32(s);
2864 if (!smartcard_ndr_pointer_read(log, s, &index, &ndrPtr))
2865 return ERROR_INVALID_DATA;
2867 status = smartcard_unpack_redir_scard_context_ref(log, s, pbContextNdrPtr,
2868 &(call->handles.hContext));
2869 if (status != SCARD_S_SUCCESS)
2874 status = smartcard_unpack_reader_state_a(log, s, &call->rgReaderStates, cReaders, &index);
2875 if (status != SCARD_S_SUCCESS)
2877 call->cReaders = cReaders;
2881 WLog_Print(log, WLOG_WARN,
"ndrPtr=0x%08" PRIx32
", can not read rgReaderStates", ndrPtr);
2882 return SCARD_E_UNEXPECTED;
2885 smartcard_trace_get_status_change_a_call(log, call);
2886 return SCARD_S_SUCCESS;
2893 UINT32 pbContextNdrPtr = 0;
2896 wLog* log = scard_log();
2897 call->rgReaderStates =
nullptr;
2899 LONG status = smartcard_unpack_redir_scard_context(log, s, &(call->handles.hContext), &index,
2901 if (status != SCARD_S_SUCCESS)
2904 if (!Stream_CheckAndLogRequiredLengthWLog(log, s, 8))
2905 return STATUS_BUFFER_TOO_SMALL;
2907 Stream_Read_UINT32(s, call->dwTimeOut);
2908 const UINT32 cReaders = Stream_Get_UINT32(s);
2909 if (!smartcard_ndr_pointer_read(log, s, &index, &ndrPtr))
2910 return ERROR_INVALID_DATA;
2912 status = smartcard_unpack_redir_scard_context_ref(log, s, pbContextNdrPtr,
2913 &(call->handles.hContext));
2914 if (status != SCARD_S_SUCCESS)
2919 status = smartcard_unpack_reader_state_w(log, s, &call->rgReaderStates, cReaders, &index);
2920 if (status != SCARD_S_SUCCESS)
2922 call->cReaders = cReaders;
2926 WLog_Print(log, WLOG_WARN,
"ndrPtr=0x%08" PRIx32
", can not read rgReaderStates", ndrPtr);
2927 return SCARD_E_UNEXPECTED;
2930 smartcard_trace_get_status_change_w_call(log, call);
2931 return SCARD_S_SUCCESS;
2938 wLog* log = scard_log();
2941 UINT32 cReaders = ret->cReaders;
2944 smartcard_trace_get_status_change_return(log, ret, unicode);
2945 if ((ret->ReturnCode != SCARD_S_SUCCESS) && (ret->ReturnCode != SCARD_E_TIMEOUT))
2947 if (cReaders == SCARD_AUTOALLOCATE)
2950 if (!Stream_EnsureRemainingCapacity(s, 4))
2951 return SCARD_E_NO_MEMORY;
2953 Stream_Write_UINT32(s, cReaders);
2954 if (!smartcard_ndr_pointer_write(s, &index, cReaders))
2955 return SCARD_E_NO_MEMORY;
2956 status = smartcard_ndr_write_state(s, ret->rgReaderStates, cReaders, NDR_PTR_SIMPLE);
2957 if (status != SCARD_S_SUCCESS)
2959 return ret->ReturnCode;
2965 UINT32 pbContextNdrPtr = 0;
2967 wLog* log = scard_log();
2970 LONG status = smartcard_unpack_redir_scard_context(log, s, &(call->handles.hContext), &index,
2972 if (status != SCARD_S_SUCCESS)
2975 status = smartcard_unpack_redir_scard_handle(log, s, &(call->handles.hCard), &index);
2976 if (status != SCARD_S_SUCCESS)
2979 if (!Stream_CheckAndLogRequiredLengthWLog(log, s, 8))
2980 return STATUS_BUFFER_TOO_SMALL;
2982 Stream_Read_INT32(s, call->fpbAtrIsNULL);
2983 Stream_Read_UINT32(s, call->cbAtrLen);
2985 status = smartcard_unpack_redir_scard_context_ref(log, s, pbContextNdrPtr,
2986 &(call->handles.hContext));
2987 if (status != SCARD_S_SUCCESS)
2990 status = smartcard_unpack_redir_scard_handle_ref(log, s, &(call->handles.hCard));
2991 if (status != SCARD_S_SUCCESS)
3000 wLog* log = scard_log();
3002 UINT32 cbAtrLen = ret->cbAtrLen;
3005 smartcard_trace_state_return(log, ret);
3006 if (ret->ReturnCode != SCARD_S_SUCCESS)
3008 if (cbAtrLen == SCARD_AUTOALLOCATE)
3011 Stream_Write_UINT32(s, ret->dwState);
3012 Stream_Write_UINT32(s, ret->dwProtocol);
3013 Stream_Write_UINT32(s, cbAtrLen);
3014 if (!smartcard_ndr_pointer_write(s, &index, cbAtrLen))
3015 return SCARD_E_NO_MEMORY;
3016 status = smartcard_ndr_write(s, ret->rgAtr, cbAtrLen, 1, NDR_PTR_SIMPLE);
3017 if (status != SCARD_S_SUCCESS)
3019 return ret->ReturnCode;
3025 UINT32 pbContextNdrPtr = 0;
3028 wLog* log = scard_log();
3030 LONG status = smartcard_unpack_redir_scard_context(log, s, &(call->handles.hContext), &index,
3032 if (status != SCARD_S_SUCCESS)
3035 status = smartcard_unpack_redir_scard_handle(log, s, &(call->handles.hCard), &index);
3036 if (status != SCARD_S_SUCCESS)
3039 if (!Stream_CheckAndLogRequiredLengthWLog(log, s, 12))
3040 return STATUS_BUFFER_TOO_SMALL;
3042 Stream_Read_INT32(s, call->fmszReaderNamesIsNULL);
3043 Stream_Read_UINT32(s, call->cchReaderLen);
3044 Stream_Read_UINT32(s, call->cbAtrLen);
3046 status = smartcard_unpack_redir_scard_context_ref(log, s, pbContextNdrPtr,
3047 &(call->handles.hContext));
3048 if (status != SCARD_S_SUCCESS)
3051 status = smartcard_unpack_redir_scard_handle_ref(log, s, &(call->handles.hCard));
3052 if (status != SCARD_S_SUCCESS)
3055 smartcard_trace_status_call(log, call, unicode);
3062 wLog* log = scard_log();
3065 smartcard_trace_status_call(log, call, unicode);
3067 LONG status = smartcard_pack_redir_scard_context(log, s, &call->handles.hContext, &index);
3068 if (status != SCARD_S_SUCCESS)
3071 status = smartcard_pack_redir_scard_handle(log, s, &call->handles.hCard, &index);
3072 if (status != SCARD_S_SUCCESS)
3075 if (!Stream_EnsureRemainingCapacity(s, 12))
3076 return SCARD_E_NO_MEMORY;
3078 Stream_Write_INT32(s, call->fmszReaderNamesIsNULL);
3079 Stream_Write_UINT32(s, call->cchReaderLen);
3080 Stream_Write_UINT32(s, call->cbAtrLen);
3082 status = smartcard_pack_redir_scard_context_ref(log, s, &call->handles.hContext);
3083 if (status != SCARD_S_SUCCESS)
3086 return smartcard_pack_redir_scard_handle_ref(log, s, &call->handles.hCard);
3092 wLog* log = scard_log();
3096 UINT32 cBytes = ret->cBytes;
3098 smartcard_trace_status_return(log, ret, unicode);
3099 if (ret->ReturnCode != SCARD_S_SUCCESS)
3101 if (cBytes == SCARD_AUTOALLOCATE)
3104 if (!Stream_EnsureRemainingCapacity(s, 4))
3105 return SCARD_F_INTERNAL_ERROR;
3107 Stream_Write_UINT32(s, cBytes);
3108 if (!smartcard_ndr_pointer_write(s, &index, cBytes))
3109 return SCARD_E_NO_MEMORY;
3111 if (!Stream_EnsureRemainingCapacity(s, 44))
3112 return SCARD_F_INTERNAL_ERROR;
3114 Stream_Write_UINT32(s, ret->dwState);
3115 Stream_Write_UINT32(s, ret->dwProtocol);
3116 Stream_Write(s, ret->pbAtr,
sizeof(ret->pbAtr));
3117 Stream_Write_UINT32(s, ret->cbAtrLen);
3118 status = smartcard_ndr_write(s, ret->mszReaderNames, cBytes, 1, NDR_PTR_SIMPLE);
3119 if (status != SCARD_S_SUCCESS)
3121 return ret->ReturnCode;
3127 wLog* log = scard_log();
3129 UINT32 mszNdrPtr = 0;
3131 if (!Stream_CheckAndLogRequiredLengthWLog(log, s, 4))
3132 return STATUS_BUFFER_TOO_SMALL;
3134 const UINT32 cBytes = Stream_Get_UINT32(s);
3136 if (!smartcard_ndr_pointer_read(log, s, &index, &mszNdrPtr))
3137 return ERROR_INVALID_DATA;
3139 if (!Stream_CheckAndLogRequiredLengthWLog(log, s, 44))
3140 return STATUS_BUFFER_TOO_SMALL;
3142 Stream_Read_UINT32(s, ret->dwState);
3143 Stream_Read_UINT32(s, ret->dwProtocol);
3144 Stream_Read(s, ret->pbAtr,
sizeof(ret->pbAtr));
3145 Stream_Read_UINT32(s, ret->cbAtrLen);
3147 if (ret->cbAtrLen > ARRAYSIZE(ret->pbAtr))
3149 WLog_Print(log, WLOG_ERROR,
"Status_Return::cbAtrLen %" PRIu32
" exceeds %" PRIuz,
3150 ret->cbAtrLen, ARRAYSIZE(ret->pbAtr));
3151 return SCARD_E_INVALID_ATR;
3156 LONG status = smartcard_ndr_read(log, s, &ret->mszReaderNames, cBytes, 1, NDR_PTR_SIMPLE);
3157 if (status != SCARD_S_SUCCESS)
3159 ret->cBytes = cBytes;
3162 smartcard_trace_status_return(log, ret, unicode);
3163 return SCARD_S_SUCCESS;
3169 wLog* log = scard_log();
3171 UINT32 pbContextNdrPtr = 0;
3173 LONG status = smartcard_unpack_redir_scard_context(log, s, &(call->handles.hContext), &index,
3175 if (status != SCARD_S_SUCCESS)
3178 status = smartcard_unpack_redir_scard_handle(log, s, &(call->handles.hCard), &index);
3179 if (status != SCARD_S_SUCCESS)
3182 if (!Stream_CheckAndLogRequiredLengthWLog(log, s, 12))
3183 return STATUS_BUFFER_TOO_SMALL;
3185 Stream_Read_UINT32(s, call->dwAttrId);
3186 Stream_Read_INT32(s, call->fpbAttrIsNULL);
3187 Stream_Read_UINT32(s, call->cbAttrLen);
3189 status = smartcard_unpack_redir_scard_context_ref(log, s, pbContextNdrPtr,
3190 &(call->handles.hContext));
3191 if (status != SCARD_S_SUCCESS)
3194 status = smartcard_unpack_redir_scard_handle_ref(log, s, &(call->handles.hCard));
3195 if (status != SCARD_S_SUCCESS)
3198 smartcard_trace_get_attrib_call(log, call);
3203 DWORD cbAttrCallLen)
3206 wLog* log = scard_log();
3208 UINT32 cbAttrLen = 0;
3210 smartcard_trace_get_attrib_return(log, ret, dwAttrId);
3212 if (!Stream_EnsureRemainingCapacity(s, 4))
3213 return SCARD_F_INTERNAL_ERROR;
3215 cbAttrLen = ret->cbAttrLen;
3216 if (ret->ReturnCode != SCARD_S_SUCCESS)
3218 if (cbAttrLen == SCARD_AUTOALLOCATE)
3223 if (cbAttrCallLen < cbAttrLen)
3224 cbAttrLen = cbAttrCallLen;
3226 Stream_Write_UINT32(s, cbAttrLen);
3227 if (!smartcard_ndr_pointer_write(s, &index, cbAttrLen))
3228 return SCARD_E_NO_MEMORY;
3230 status = smartcard_ndr_write(s, ret->pbAttr, cbAttrLen, 1, NDR_PTR_SIMPLE);
3231 if (status != SCARD_S_SUCCESS)
3233 return ret->ReturnCode;
3239 wLog* log = scard_log();
3242 UINT32 pvInBufferNdrPtr = 0;
3243 UINT32 pbContextNdrPtr = 0;
3245 call->pvInBuffer =
nullptr;
3247 LONG status = smartcard_unpack_redir_scard_context(log, s, &(call->handles.hContext), &index,
3249 if (status != SCARD_S_SUCCESS)
3252 status = smartcard_unpack_redir_scard_handle(log, s, &(call->handles.hCard), &index);
3253 if (status != SCARD_S_SUCCESS)
3256 if (!Stream_CheckAndLogRequiredLengthWLog(log, s, 20))
3257 return STATUS_BUFFER_TOO_SMALL;
3259 Stream_Read_UINT32(s, call->dwControlCode);
3260 const UINT32 cbInBufferSize = Stream_Get_UINT32(s);
3261 if (!smartcard_ndr_pointer_read(log, s, &index,
3263 return ERROR_INVALID_DATA;
3264 Stream_Read_INT32(s, call->fpvOutBufferIsNULL);
3265 Stream_Read_UINT32(s, call->cbOutBufferSize);
3267 status = smartcard_unpack_redir_scard_context_ref(log, s, pbContextNdrPtr,
3268 &(call->handles.hContext));
3269 if (status != SCARD_S_SUCCESS)
3272 status = smartcard_unpack_redir_scard_handle_ref(log, s, &(call->handles.hCard));
3273 if (status != SCARD_S_SUCCESS)
3276 if (pvInBufferNdrPtr)
3278 status = smartcard_ndr_read(log, s, &call->pvInBuffer, cbInBufferSize, 1, NDR_PTR_SIMPLE);
3279 if (status != SCARD_S_SUCCESS)
3281 call->cbInBufferSize = cbInBufferSize;
3284 smartcard_trace_control_call(log, call);
3285 return SCARD_S_SUCCESS;
3291 wLog* log = scard_log();
3294 UINT32 cbDataLen = ret->cbOutBufferSize;
3297 smartcard_trace_control_return(log, ret);
3298 if (ret->ReturnCode != SCARD_S_SUCCESS)
3300 if (cbDataLen == SCARD_AUTOALLOCATE)
3303 if (!Stream_EnsureRemainingCapacity(s, 4))
3304 return SCARD_F_INTERNAL_ERROR;
3306 Stream_Write_UINT32(s, cbDataLen);
3307 if (!smartcard_ndr_pointer_write(s, &index, cbDataLen))
3308 return SCARD_E_NO_MEMORY;
3310 status = smartcard_ndr_write(s, ret->pvOutBuffer, cbDataLen, 1, NDR_PTR_SIMPLE);
3311 if (status != SCARD_S_SUCCESS)
3313 return ret->ReturnCode;
3319 BYTE* pbExtraBytes =
nullptr;
3320 UINT32 pbExtraBytesNdrPtr = 0;
3321 UINT32 pbSendBufferNdrPtr = 0;
3322 UINT32 pioRecvPciNdrPtr = 0;
3326 UINT32 pbContextNdrPtr = 0;
3329 wLog* log = scard_log();
3331 call->pioSendPci =
nullptr;
3332 call->pioRecvPci =
nullptr;
3333 call->pbSendBuffer =
nullptr;
3335 LONG status = smartcard_unpack_redir_scard_context(log, s, &(call->handles.hContext), &index,
3337 if (status != SCARD_S_SUCCESS)
3340 status = smartcard_unpack_redir_scard_handle(log, s, &(call->handles.hCard), &index);
3341 if (status != SCARD_S_SUCCESS)
3344 if (!Stream_CheckAndLogRequiredLengthWLog(log, s, 32))
3345 return STATUS_BUFFER_TOO_SMALL;
3347 Stream_Read_UINT32(s, ioSendPci.dwProtocol);
3348 Stream_Read_UINT32(s, ioSendPci.cbExtraBytes);
3349 if (!smartcard_ndr_pointer_read(log, s, &index,
3350 &pbExtraBytesNdrPtr))
3351 return ERROR_INVALID_DATA;
3353 const UINT32 cbSendLength = Stream_Get_UINT32(s);
3354 if (!smartcard_ndr_pointer_read(log, s, &index,
3355 &pbSendBufferNdrPtr))
3356 return ERROR_INVALID_DATA;
3358 if (!smartcard_ndr_pointer_read(log, s, &index,
3360 return ERROR_INVALID_DATA;
3362 Stream_Read_INT32(s, call->fpbRecvBufferIsNULL);
3363 Stream_Read_UINT32(s, call->cbRecvLength);
3365 if (ioSendPci.cbExtraBytes > 1024)
3367 WLog_Print(log, WLOG_WARN,
3368 "Transmit_Call ioSendPci.cbExtraBytes is out of bounds: %" PRIu32
" (max: 1024)",
3369 ioSendPci.cbExtraBytes);
3370 return STATUS_INVALID_PARAMETER;
3373 if (cbSendLength > 66560)
3375 WLog_Print(log, WLOG_WARN,
3376 "Transmit_Call cbSendLength is out of bounds: %" PRIu32
" (max: 66560)",
3377 ioSendPci.cbExtraBytes);
3378 return STATUS_INVALID_PARAMETER;
3381 status = smartcard_unpack_redir_scard_context_ref(log, s, pbContextNdrPtr,
3382 &(call->handles.hContext));
3383 if (status != SCARD_S_SUCCESS)
3386 status = smartcard_unpack_redir_scard_handle_ref(log, s, &(call->handles.hCard));
3387 if (status != SCARD_S_SUCCESS)
3390 if (ioSendPci.cbExtraBytes && !pbExtraBytesNdrPtr)
3394 "Transmit_Call ioSendPci.cbExtraBytes is non-zero but pbExtraBytesNdrPtr is null");
3395 return STATUS_INVALID_PARAMETER;
3398 if (pbExtraBytesNdrPtr)
3401 if (!Stream_CheckAndLogRequiredLengthWLog(log, s, 4))
3402 return STATUS_BUFFER_TOO_SMALL;
3404 Stream_Read_UINT32(s, length);
3406 if (!Stream_CheckAndLogRequiredLengthWLog(log, s, ioSendPci.cbExtraBytes))
3407 return STATUS_BUFFER_TOO_SMALL;
3409 ioSendPci.pbExtraBytes = Stream_Pointer(s);
3413 if (!call->pioSendPci)
3415 WLog_Print(log, WLOG_WARN,
"Transmit_Call out of memory error (pioSendPci)");
3416 return STATUS_NO_MEMORY;
3419 call->pioSendPci->dwProtocol = ioSendPci.dwProtocol;
3420 call->pioSendPci->cbPciLength = (DWORD)(ioSendPci.cbExtraBytes +
sizeof(
SCARD_IO_REQUEST));
3422 Stream_Read(s, pbExtraBytes, ioSendPci.cbExtraBytes);
3423 if (smartcard_unpack_read_size_align(s, ioSendPci.cbExtraBytes, 4) < 0)
3424 return STATUS_INVALID_PARAMETER;
3430 if (!call->pioSendPci)
3432 WLog_Print(log, WLOG_WARN,
"Transmit_Call out of memory error (pioSendPci)");
3433 return STATUS_NO_MEMORY;
3436 call->pioSendPci->dwProtocol = ioSendPci.dwProtocol;
3440 if (pbSendBufferNdrPtr)
3442 status = smartcard_ndr_read(log, s, &call->pbSendBuffer, cbSendLength, 1, NDR_PTR_SIMPLE);
3443 if (status != SCARD_S_SUCCESS)
3445 call->cbSendLength = cbSendLength;
3448 if (pioRecvPciNdrPtr)
3450 if (!Stream_CheckAndLogRequiredLengthWLog(log, s, 12))
3451 return STATUS_BUFFER_TOO_SMALL;
3453 Stream_Read_UINT32(s, ioRecvPci.dwProtocol);
3454 Stream_Read_UINT32(s, ioRecvPci.cbExtraBytes);
3455 if (!smartcard_ndr_pointer_read(log, s, &index,
3456 &pbExtraBytesNdrPtr))
3457 return ERROR_INVALID_DATA;
3459 if (ioRecvPci.cbExtraBytes && !pbExtraBytesNdrPtr)
3463 "Transmit_Call ioRecvPci.cbExtraBytes is non-zero but pbExtraBytesNdrPtr is null");
3464 return STATUS_INVALID_PARAMETER;
3467 if (pbExtraBytesNdrPtr)
3470 if (!Stream_CheckAndLogRequiredLengthWLog(log, s, 4))
3471 return STATUS_BUFFER_TOO_SMALL;
3473 Stream_Read_UINT32(s, length);
3475 if (ioRecvPci.cbExtraBytes > 1024)
3477 WLog_Print(log, WLOG_WARN,
3478 "Transmit_Call ioRecvPci.cbExtraBytes is out of bounds: %" PRIu32
3480 ioRecvPci.cbExtraBytes);
3481 return STATUS_INVALID_PARAMETER;
3484 if (length != ioRecvPci.cbExtraBytes)
3486 WLog_Print(log, WLOG_WARN,
3487 "Transmit_Call unexpected length: Actual: %" PRIu32
3488 ", Expected: %" PRIu32
" (ioRecvPci.cbExtraBytes)",
3489 length, ioRecvPci.cbExtraBytes);
3490 return STATUS_INVALID_PARAMETER;
3493 if (!Stream_CheckAndLogRequiredLengthWLog(log, s, ioRecvPci.cbExtraBytes))
3494 return STATUS_BUFFER_TOO_SMALL;
3496 ioRecvPci.pbExtraBytes = Stream_Pointer(s);
3500 if (!call->pioRecvPci)
3502 WLog_Print(log, WLOG_WARN,
"Transmit_Call out of memory error (pioRecvPci)");
3503 return STATUS_NO_MEMORY;
3506 call->pioRecvPci->dwProtocol = ioRecvPci.dwProtocol;
3507 call->pioRecvPci->cbPciLength =
3510 Stream_Read(s, pbExtraBytes, ioRecvPci.cbExtraBytes);
3511 if (smartcard_unpack_read_size_align(s, ioRecvPci.cbExtraBytes, 4) < 0)
3512 return STATUS_INVALID_PARAMETER;
3518 if (!call->pioRecvPci)
3520 WLog_Print(log, WLOG_WARN,
"Transmit_Call out of memory error (pioRecvPci)");
3521 return STATUS_NO_MEMORY;
3524 call->pioRecvPci->dwProtocol = ioRecvPci.dwProtocol;
3529 smartcard_trace_transmit_call(log, call);
3530 return SCARD_S_SUCCESS;
3536 wLog* log = scard_log();
3541 UINT32 cbRecvLength = ret->cbRecvLength;
3542 UINT32 cbRecvPci = ret->pioRecvPci ? ret->pioRecvPci->cbPciLength : 0;
3544 smartcard_trace_transmit_return(log, ret);
3546 if (!ret->pbRecvBuffer)
3549 if (!smartcard_ndr_pointer_write(s, &index, cbRecvPci))
3550 return SCARD_E_NO_MEMORY;
3551 if (!Stream_EnsureRemainingCapacity(s, 4))
3552 return SCARD_E_NO_MEMORY;
3553 Stream_Write_UINT32(s, cbRecvLength);
3554 if (!smartcard_ndr_pointer_write(s, &index, cbRecvLength))
3555 return SCARD_E_NO_MEMORY;
3557 if (ret->pioRecvPci)
3559 UINT32 cbExtraBytes = (UINT32)(ret->pioRecvPci->cbPciLength -
sizeof(
SCARD_IO_REQUEST));
3562 if (!Stream_EnsureRemainingCapacity(s, cbExtraBytes + 16ull))
3564 WLog_Print(log, WLOG_ERROR,
"Stream_EnsureRemainingCapacity failed!");
3565 return SCARD_F_INTERNAL_ERROR;
3568 Stream_Write_UINT32(s, ret->pioRecvPci->dwProtocol);
3569 Stream_Write_UINT32(s, cbExtraBytes);
3570 if (!smartcard_ndr_pointer_write(s, &index, cbExtraBytes))
3571 return SCARD_E_NO_MEMORY;
3572 error = smartcard_ndr_write(s, pbExtraBytes, cbExtraBytes, 1, NDR_PTR_SIMPLE);
3577 status = smartcard_ndr_write(s, ret->pbRecvBuffer, ret->cbRecvLength, 1, NDR_PTR_SIMPLE);
3578 if (status != SCARD_S_SUCCESS)
3580 return ret->ReturnCode;
3585 UINT32 rgReaderStatesNdrPtr = 0;
3586 UINT32 rgAtrMasksNdrPtr = 0;
3588 UINT32 pbContextNdrPtr = 0;
3591 wLog* log = scard_log();
3593 call->rgReaderStates =
nullptr;
3595 LONG status = smartcard_unpack_redir_scard_context(log, s, &(call->handles.hContext), &index,
3597 if (status != SCARD_S_SUCCESS)
3600 if (!Stream_CheckAndLogRequiredLengthWLog(log, s, 16))
3601 return STATUS_BUFFER_TOO_SMALL;
3603 Stream_Read_UINT32(s, call->cAtrs);
3604 if (!smartcard_ndr_pointer_read(log, s, &index, &rgAtrMasksNdrPtr))
3605 return ERROR_INVALID_DATA;
3606 const UINT32 cReaders = Stream_Get_UINT32(s);
3607 if (!smartcard_ndr_pointer_read(log, s, &index, &rgReaderStatesNdrPtr))
3608 return ERROR_INVALID_DATA;
3610 status = smartcard_unpack_redir_scard_context_ref(log, s, pbContextNdrPtr,
3611 &(call->handles.hContext));
3612 if (status != SCARD_S_SUCCESS)
3615 if ((rgAtrMasksNdrPtr && !call->cAtrs) || (!rgAtrMasksNdrPtr && call->cAtrs))
3617 WLog_Print(log, WLOG_WARN,
3618 "LocateCardsByATRA_Call rgAtrMasksNdrPtr (0x%08" PRIX32
3619 ") and cAtrs (0x%08" PRIX32
") inconsistency",
3620 rgAtrMasksNdrPtr, call->cAtrs);
3621 return STATUS_INVALID_PARAMETER;
3624 if (rgAtrMasksNdrPtr)
3626 status = smartcard_ndr_read_atrmask(log, s, &call->rgAtrMasks, call->cAtrs, NDR_PTR_SIMPLE);
3627 if (status != SCARD_S_SUCCESS)
3631 if (rgReaderStatesNdrPtr)
3633 status = smartcard_unpack_reader_state_a(log, s, &call->rgReaderStates, cReaders, &index);
3634 if (status != SCARD_S_SUCCESS)
3636 call->cReaders = cReaders;
3639 smartcard_trace_locate_cards_by_atr_a_call(log, call);
3640 return SCARD_S_SUCCESS;
3645 UINT32 sz1NdrPtr = 0;
3646 UINT32 sz2NdrPtr = 0;
3648 UINT32 pbContextNdrPtr = 0;
3651 wLog* log = scard_log();
3653 LONG status = smartcard_unpack_redir_scard_context(log, s, &(call->handles.hContext), &index,
3655 if (status != SCARD_S_SUCCESS)
3658 if (!smartcard_ndr_pointer_read(log, s, &index, &sz1NdrPtr))
3659 return ERROR_INVALID_DATA;
3660 if (!smartcard_ndr_pointer_read(log, s, &index, &sz2NdrPtr))
3661 return ERROR_INVALID_DATA;
3664 smartcard_unpack_redir_scard_context_ref(log, s, pbContextNdrPtr, &call->handles.hContext);
3665 if (status != SCARD_S_SUCCESS)
3670 status = smartcard_ndr_read_a(log, s, &call->sz1, NDR_PTR_FULL);
3671 if (status != SCARD_S_SUCCESS)
3676 status = smartcard_ndr_read_a(log, s, &call->sz2, NDR_PTR_FULL);
3677 if (status != SCARD_S_SUCCESS)
3680 smartcard_trace_context_and_two_strings_a_call(log, call);
3681 return SCARD_S_SUCCESS;
3686 UINT32 sz1NdrPtr = 0;
3687 UINT32 sz2NdrPtr = 0;
3689 UINT32 pbContextNdrPtr = 0;
3692 wLog* log = scard_log();
3694 LONG status = smartcard_unpack_redir_scard_context(log, s, &(call->handles.hContext), &index,
3696 if (status != SCARD_S_SUCCESS)
3699 if (!smartcard_ndr_pointer_read(log, s, &index, &sz1NdrPtr))
3700 return ERROR_INVALID_DATA;
3701 if (!smartcard_ndr_pointer_read(log, s, &index, &sz2NdrPtr))
3702 return ERROR_INVALID_DATA;
3705 smartcard_unpack_redir_scard_context_ref(log, s, pbContextNdrPtr, &call->handles.hContext);
3706 if (status != SCARD_S_SUCCESS)
3711 status = smartcard_ndr_read_w(log, s, &call->sz1, NDR_PTR_FULL);
3712 if (status != SCARD_S_SUCCESS)
3717 status = smartcard_ndr_read_w(log, s, &call->sz2, NDR_PTR_FULL);
3718 if (status != SCARD_S_SUCCESS)
3721 smartcard_trace_context_and_two_strings_w_call(log, call);
3722 return SCARD_S_SUCCESS;
3727 UINT32 sz1NdrPtr = 0;
3728 UINT32 sz2NdrPtr = 0;
3730 UINT32 pbContextNdrPtr = 0;
3733 wLog* log = scard_log();
3735 LONG status = smartcard_unpack_redir_scard_context(log, s, &(call->handles.hContext), &index,
3737 if (status != SCARD_S_SUCCESS)
3740 if (!Stream_CheckAndLogRequiredLengthWLog(log, s, 16))
3741 return STATUS_BUFFER_TOO_SMALL;
3743 const UINT32 cBytes = Stream_Get_UINT32(s);
3744 if (!smartcard_ndr_pointer_read(log, s, &index, &sz1NdrPtr))
3745 return ERROR_INVALID_DATA;
3747 const UINT32 cReaders = Stream_Get_UINT32(s);
3748 if (!smartcard_ndr_pointer_read(log, s, &index, &sz2NdrPtr))
3749 return ERROR_INVALID_DATA;
3751 status = smartcard_unpack_redir_scard_context_ref(log, s, pbContextNdrPtr,
3752 &(call->handles.hContext));
3753 if (status != SCARD_S_SUCCESS)
3758 status = smartcard_ndr_read_fixed_string_a(log, s, &call->mszCards, cBytes, NDR_PTR_SIMPLE);
3759 if (status != SCARD_S_SUCCESS)
3761 call->cBytes = cBytes;
3765 status = smartcard_unpack_reader_state_a(log, s, &call->rgReaderStates, cReaders, &index);
3766 if (status != SCARD_S_SUCCESS)
3768 call->cReaders = cReaders;
3770 smartcard_trace_locate_cards_a_call(log, call);
3771 return SCARD_S_SUCCESS;
3776 UINT32 sz1NdrPtr = 0;
3777 UINT32 sz2NdrPtr = 0;
3779 UINT32 pbContextNdrPtr = 0;
3782 wLog* log = scard_log();
3784 LONG status = smartcard_unpack_redir_scard_context(log, s, &(call->handles.hContext), &index,
3786 if (status != SCARD_S_SUCCESS)
3789 if (!Stream_CheckAndLogRequiredLengthWLog(log, s, 16))
3790 return STATUS_BUFFER_TOO_SMALL;
3792 const UINT32 cBytes = Stream_Get_UINT32(s);
3793 if (!smartcard_ndr_pointer_read(log, s, &index, &sz1NdrPtr))
3794 return ERROR_INVALID_DATA;
3796 const UINT32 cReaders = Stream_Get_UINT32(s);
3797 if (!smartcard_ndr_pointer_read(log, s, &index, &sz2NdrPtr))
3798 return ERROR_INVALID_DATA;
3800 status = smartcard_unpack_redir_scard_context_ref(log, s, pbContextNdrPtr,
3801 &(call->handles.hContext));
3802 if (status != SCARD_S_SUCCESS)
3807 status = smartcard_ndr_read_fixed_string_w(log, s, &call->mszCards, cBytes, NDR_PTR_SIMPLE);
3808 if (status != SCARD_S_SUCCESS)
3810 call->cBytes = cBytes;
3814 status = smartcard_unpack_reader_state_w(log, s, &call->rgReaderStates, cReaders, &index);
3815 if (status != SCARD_S_SUCCESS)
3817 call->cReaders = cReaders;
3819 smartcard_trace_locate_cards_w_call(log, call);
3820 return SCARD_S_SUCCESS;
3827 UINT32 pbContextNdrPtr = 0;
3830 wLog* log = scard_log();
3832 LONG status = smartcard_unpack_redir_scard_context(log, s, &(call->handles.hContext), &index,
3834 if (status != SCARD_S_SUCCESS)
3836 status = smartcard_unpack_redir_scard_handle(log, s, &(call->handles.hCard), &index);
3837 if (status != SCARD_S_SUCCESS)
3840 if (!Stream_CheckAndLogRequiredLengthWLog(log, s, 12))
3841 return STATUS_BUFFER_TOO_SMALL;
3842 Stream_Read_UINT32(s, call->dwAttrId);
3843 Stream_Read_UINT32(s, call->cbAttrLen);
3845 if (!smartcard_ndr_pointer_read(log, s, &index, &ndrPtr))
3846 return ERROR_INVALID_DATA;
3848 status = smartcard_unpack_redir_scard_context_ref(log, s, pbContextNdrPtr,
3849 &(call->handles.hContext));
3850 if (status != SCARD_S_SUCCESS)
3853 status = smartcard_unpack_redir_scard_handle_ref(log, s, &(call->handles.hCard));
3854 if (status != SCARD_S_SUCCESS)
3860 status = smartcard_ndr_read_ex(log, s, &call->pbAttr, 0, 1, NDR_PTR_SIMPLE, &len);
3861 if (status != SCARD_S_SUCCESS)
3863 if (call->cbAttrLen > len)
3864 call->cbAttrLen = WINPR_ASSERTING_INT_CAST(DWORD, len);
3867 call->cbAttrLen = 0;
3868 smartcard_trace_set_attrib_call(log, call);
3869 return SCARD_S_SUCCESS;
3875 wLog* log = scard_log();
3878 smartcard_trace_set_attrib_call(log, call);
3880 LONG status = smartcard_pack_redir_scard_context(log, s, &call->handles.hContext, &index);
3881 if (status != SCARD_S_SUCCESS)
3884 status = smartcard_pack_redir_scard_handle(log, s, &call->handles.hCard, &index);
3885 if (status != SCARD_S_SUCCESS)
3888 if (!Stream_EnsureRemainingCapacity(s, 8))
3889 return SCARD_E_NO_MEMORY;
3891 Stream_Write_UINT32(s, call->dwAttrId);
3892 Stream_Write_UINT32(s, call->cbAttrLen);
3894 if (!smartcard_ndr_pointer_write(s, &index, call->cbAttrLen))
3895 return SCARD_E_NO_MEMORY;
3897 status = smartcard_pack_redir_scard_context_ref(log, s, &call->handles.hContext);
3898 if (status != SCARD_S_SUCCESS)
3901 status = smartcard_pack_redir_scard_handle_ref(log, s, &call->handles.hCard);
3902 if (status != SCARD_S_SUCCESS)
3905 status = smartcard_ndr_write(s, call->pbAttr, call->cbAttrLen, 1, NDR_PTR_SIMPLE);
3906 if (status != SCARD_S_SUCCESS)
3909 return SCARD_S_SUCCESS;
3914 UINT32 rgReaderStatesNdrPtr = 0;
3915 UINT32 rgAtrMasksNdrPtr = 0;
3917 UINT32 pbContextNdrPtr = 0;
3920 wLog* log = scard_log();
3922 call->rgReaderStates =
nullptr;
3924 LONG status = smartcard_unpack_redir_scard_context(log, s, &(call->handles.hContext), &index,
3926 if (status != SCARD_S_SUCCESS)
3929 if (!Stream_CheckAndLogRequiredLengthWLog(log, s, 16))
3930 return STATUS_BUFFER_TOO_SMALL;
3932 Stream_Read_UINT32(s, call->cAtrs);
3933 if (!smartcard_ndr_pointer_read(log, s, &index, &rgAtrMasksNdrPtr))
3934 return ERROR_INVALID_DATA;
3936 const UINT32 cReaders = Stream_Get_UINT32(s);
3937 if (!smartcard_ndr_pointer_read(log, s, &index, &rgReaderStatesNdrPtr))
3938 return ERROR_INVALID_DATA;
3940 status = smartcard_unpack_redir_scard_context_ref(log, s, pbContextNdrPtr,
3941 &(call->handles.hContext));
3942 if (status != SCARD_S_SUCCESS)
3945 if ((rgAtrMasksNdrPtr && !call->cAtrs) || (!rgAtrMasksNdrPtr && call->cAtrs))
3947 WLog_Print(log, WLOG_WARN,
3948 "LocateCardsByATRW_Call rgAtrMasksNdrPtr (0x%08" PRIX32
3949 ") and cAtrs (0x%08" PRIX32
") inconsistency",
3950 rgAtrMasksNdrPtr, call->cAtrs);
3951 return STATUS_INVALID_PARAMETER;
3954 if (rgAtrMasksNdrPtr)
3956 status = smartcard_ndr_read_atrmask(log, s, &call->rgAtrMasks, call->cAtrs, NDR_PTR_SIMPLE);
3957 if (status != SCARD_S_SUCCESS)
3961 if (rgReaderStatesNdrPtr)
3963 status = smartcard_unpack_reader_state_w(log, s, &call->rgReaderStates, cReaders, &index);
3964 if (status != SCARD_S_SUCCESS)
3966 call->cReaders = cReaders;
3969 smartcard_trace_locate_cards_by_atr_w_call(log, call);
3970 return SCARD_S_SUCCESS;
3975 UINT32 mszNdrPtr = 0;
3976 UINT32 contextNdrPtr = 0;
3978 UINT32 pbContextNdrPtr = 0;
3981 wLog* log = scard_log();
3983 if (!smartcard_ndr_pointer_read(log, s, &index, &mszNdrPtr))
3984 return ERROR_INVALID_DATA;
3986 LONG status = smartcard_unpack_redir_scard_context(log, s, &(call->Common.handles.hContext),
3987 &index, &pbContextNdrPtr);
3988 if (status != SCARD_S_SUCCESS)
3991 if (!smartcard_ndr_pointer_read(log, s, &index, &contextNdrPtr))
3992 return ERROR_INVALID_DATA;
3994 if (!Stream_CheckAndLogRequiredLengthWLog(log, s, 12))
3995 return STATUS_BUFFER_TOO_SMALL;
3996 Stream_Read_UINT32(s, call->Common.FreshnessCounter);
3997 Stream_Read_INT32(s, call->Common.fPbDataIsNULL);
3998 Stream_Read_UINT32(s, call->Common.cbDataLen);
4000 call->szLookupName =
nullptr;
4003 status = smartcard_ndr_read_a(log, s, &call->szLookupName, NDR_PTR_FULL);
4004 if (status != SCARD_S_SUCCESS)
4008 status = smartcard_unpack_redir_scard_context_ref(log, s, pbContextNdrPtr,
4009 &call->Common.handles.hContext);
4010 if (status != SCARD_S_SUCCESS)
4015 status = smartcard_ndr_read_u(log, s, &call->Common.CardIdentifier);
4016 if (status != SCARD_S_SUCCESS)
4019 smartcard_trace_read_cache_a_call(log, call);
4020 return SCARD_S_SUCCESS;
4025 UINT32 mszNdrPtr = 0;
4026 UINT32 contextNdrPtr = 0;
4028 UINT32 pbContextNdrPtr = 0;
4031 wLog* log = scard_log();
4033 if (!smartcard_ndr_pointer_read(log, s, &index, &mszNdrPtr))
4034 return ERROR_INVALID_DATA;
4036 LONG status = smartcard_unpack_redir_scard_context(log, s, &(call->Common.handles.hContext),
4037 &index, &pbContextNdrPtr);
4038 if (status != SCARD_S_SUCCESS)
4041 if (!smartcard_ndr_pointer_read(log, s, &index, &contextNdrPtr))
4042 return ERROR_INVALID_DATA;
4044 if (!Stream_CheckAndLogRequiredLengthWLog(log, s, 12))
4045 return STATUS_BUFFER_TOO_SMALL;
4046 Stream_Read_UINT32(s, call->Common.FreshnessCounter);
4047 Stream_Read_INT32(s, call->Common.fPbDataIsNULL);
4048 Stream_Read_UINT32(s, call->Common.cbDataLen);
4050 call->szLookupName =
nullptr;
4053 status = smartcard_ndr_read_w(log, s, &call->szLookupName, NDR_PTR_FULL);
4054 if (status != SCARD_S_SUCCESS)
4058 status = smartcard_unpack_redir_scard_context_ref(log, s, pbContextNdrPtr,
4059 &call->Common.handles.hContext);
4060 if (status != SCARD_S_SUCCESS)
4065 status = smartcard_ndr_read_u(log, s, &call->Common.CardIdentifier);
4066 if (status != SCARD_S_SUCCESS)
4069 smartcard_trace_read_cache_w_call(log, call);
4070 return SCARD_S_SUCCESS;
4075 UINT32 mszNdrPtr = 0;
4076 UINT32 contextNdrPtr = 0;
4077 UINT32 pbDataNdrPtr = 0;
4079 UINT32 pbContextNdrPtr = 0;
4082 wLog* log = scard_log();
4084 if (!smartcard_ndr_pointer_read(log, s, &index, &mszNdrPtr))
4085 return ERROR_INVALID_DATA;
4087 LONG status = smartcard_unpack_redir_scard_context(log, s, &(call->Common.handles.hContext),
4088 &index, &pbContextNdrPtr);
4089 if (status != SCARD_S_SUCCESS)
4092 if (!smartcard_ndr_pointer_read(log, s, &index, &contextNdrPtr))
4093 return ERROR_INVALID_DATA;
4095 if (!Stream_CheckAndLogRequiredLengthWLog(log, s, 8))
4096 return STATUS_BUFFER_TOO_SMALL;
4098 Stream_Read_UINT32(s, call->Common.FreshnessCounter);
4099 const UINT32 cbDataLen = Stream_Get_UINT32(s);
4101 if (!smartcard_ndr_pointer_read(log, s, &index, &pbDataNdrPtr))
4102 return ERROR_INVALID_DATA;
4104 call->szLookupName =
nullptr;
4107 status = smartcard_ndr_read_a(log, s, &call->szLookupName, NDR_PTR_FULL);
4108 if (status != SCARD_S_SUCCESS)
4112 status = smartcard_unpack_redir_scard_context_ref(log, s, pbContextNdrPtr,
4113 &call->Common.handles.hContext);
4114 if (status != SCARD_S_SUCCESS)
4117 call->Common.CardIdentifier =
nullptr;
4120 status = smartcard_ndr_read_u(log, s, &call->Common.CardIdentifier);
4121 if (status != SCARD_S_SUCCESS)
4125 call->Common.pbData =
nullptr;
4128 status = smartcard_ndr_read(log, s, &call->Common.pbData, cbDataLen, 1, NDR_PTR_SIMPLE);
4129 if (status != SCARD_S_SUCCESS)
4131 call->Common.cbDataLen = cbDataLen;
4133 smartcard_trace_write_cache_a_call(log, call);
4134 return SCARD_S_SUCCESS;
4139 UINT32 mszNdrPtr = 0;
4140 UINT32 contextNdrPtr = 0;
4141 UINT32 pbDataNdrPtr = 0;
4143 UINT32 pbContextNdrPtr = 0;
4146 wLog* log = scard_log();
4148 if (!smartcard_ndr_pointer_read(log, s, &index, &mszNdrPtr))
4149 return ERROR_INVALID_DATA;
4151 LONG status = smartcard_unpack_redir_scard_context(log, s, &(call->Common.handles.hContext),
4152 &index, &pbContextNdrPtr);
4153 if (status != SCARD_S_SUCCESS)
4156 if (!smartcard_ndr_pointer_read(log, s, &index, &contextNdrPtr))
4157 return ERROR_INVALID_DATA;
4159 if (!Stream_CheckAndLogRequiredLengthWLog(log, s, 8))
4160 return STATUS_BUFFER_TOO_SMALL;
4161 Stream_Read_UINT32(s, call->Common.FreshnessCounter);
4162 const UINT32 cbDataLen = Stream_Get_UINT32(s);
4164 if (!smartcard_ndr_pointer_read(log, s, &index, &pbDataNdrPtr))
4165 return ERROR_INVALID_DATA;
4167 call->szLookupName =
nullptr;
4170 status = smartcard_ndr_read_w(log, s, &call->szLookupName, NDR_PTR_FULL);
4171 if (status != SCARD_S_SUCCESS)
4175 status = smartcard_unpack_redir_scard_context_ref(log, s, pbContextNdrPtr,
4176 &call->Common.handles.hContext);
4177 if (status != SCARD_S_SUCCESS)
4180 call->Common.CardIdentifier =
nullptr;
4183 status = smartcard_ndr_read_u(log, s, &call->Common.CardIdentifier);
4184 if (status != SCARD_S_SUCCESS)
4188 call->Common.pbData =
nullptr;
4191 status = smartcard_ndr_read(log, s, &call->Common.pbData, cbDataLen, 1, NDR_PTR_SIMPLE);
4192 if (status != SCARD_S_SUCCESS)
4194 call->Common.cbDataLen = cbDataLen;
4196 smartcard_trace_write_cache_w_call(log, call);
4203 wLog* log = scard_log();
4206 UINT32 pbContextNdrPtr = 0;
4208 LONG status = smartcard_unpack_redir_scard_context(log, s, &(call->handles.hContext), &index,
4210 if (status != SCARD_S_SUCCESS)
4213 status = smartcard_unpack_redir_scard_handle(log, s, &(call->handles.hCard), &index);
4214 if (status != SCARD_S_SUCCESS)
4217 status = smartcard_unpack_redir_scard_context_ref(log, s, pbContextNdrPtr,
4218 &(call->handles.hContext));
4219 if (status != SCARD_S_SUCCESS)
4221 WLog_Print(log, WLOG_ERROR,
4222 "smartcard_unpack_redir_scard_context_ref failed with error %" PRId32
"",
4227 status = smartcard_unpack_redir_scard_handle_ref(log, s, &(call->handles.hCard));
4228 if (status != SCARD_S_SUCCESS)
4229 WLog_Print(log, WLOG_ERROR,
4230 "smartcard_unpack_redir_scard_handle_ref failed with error %" PRId32
"", status);
4232 smartcard_trace_get_transmit_count_call(log, call);
4239 wLog* log = scard_log();
4240 return smartcard_unpack_common_context_and_string_w(log, s, &call->handles.hContext,
4241 &call->szReaderName);
4247 wLog* log = scard_log();
4248 return smartcard_unpack_common_context_and_string_a(log, s, &call->handles.hContext, &call->sz);
4254 wLog* log = scard_log();
4255 return smartcard_unpack_common_context_and_string_w(log, s, &call->handles.hContext, &call->sz);
4261 wLog* log = scard_log();
4262 return smartcard_unpack_common_context_and_string_w(log, s, &call->handles.hContext,
4263 &call->szReaderName);
4269 wLog* log = scard_log();
4270 smartcard_trace_device_type_id_return(log, ret);
4272 if (!Stream_EnsureRemainingCapacity(s, 4))
4274 WLog_Print(log, WLOG_ERROR,
"Stream_EnsureRemainingCapacity failed!");
4275 return SCARD_F_INTERNAL_ERROR;
4278 Stream_Write_UINT32(s, ret->dwDeviceId);
4280 return ret->ReturnCode;
4286 wLog* log = scard_log();
4289 UINT32 cbDataLen = ret->cReaders;
4292 smartcard_trace_locate_cards_return(log, ret);
4293 if (ret->ReturnCode != SCARD_S_SUCCESS)
4295 if (cbDataLen == SCARD_AUTOALLOCATE)
4298 if (!Stream_EnsureRemainingCapacity(s, 4))
4300 WLog_Print(log, WLOG_ERROR,
"Stream_EnsureRemainingCapacity failed!");
4301 return SCARD_F_INTERNAL_ERROR;
4304 Stream_Write_UINT32(s, cbDataLen);
4305 if (!smartcard_ndr_pointer_write(s, &index, cbDataLen))
4306 return SCARD_E_NO_MEMORY;
4308 status = smartcard_ndr_write_state(s, ret->rgReaderStates, cbDataLen, NDR_PTR_SIMPLE);
4309 if (status != SCARD_S_SUCCESS)
4311 return ret->ReturnCode;
4317 wLog* log = scard_log();
4321 UINT32 cbDataLen = ret->cbDataLen;
4322 smartcard_trace_get_reader_icon_return(log, ret);
4323 if (ret->ReturnCode != SCARD_S_SUCCESS)
4325 if (cbDataLen == SCARD_AUTOALLOCATE)
4328 if (!Stream_EnsureRemainingCapacity(s, 4))
4330 WLog_Print(log, WLOG_ERROR,
"Stream_EnsureRemainingCapacity failed!");
4331 return SCARD_F_INTERNAL_ERROR;
4334 Stream_Write_UINT32(s, cbDataLen);
4335 if (!smartcard_ndr_pointer_write(s, &index, cbDataLen))
4336 return SCARD_E_NO_MEMORY;
4338 status = smartcard_ndr_write(s, ret->pbData, cbDataLen, 1, NDR_PTR_SIMPLE);
4339 if (status != SCARD_S_SUCCESS)
4341 return ret->ReturnCode;
4347 wLog* log = scard_log();
4349 smartcard_trace_get_transmit_count_return(log, ret);
4351 if (!Stream_EnsureRemainingCapacity(s, 4))
4353 WLog_Print(log, WLOG_ERROR,
"Stream_EnsureRemainingCapacity failed!");
4354 return SCARD_F_INTERNAL_ERROR;
4357 Stream_Write_UINT32(s, ret->cTransmitCount);
4359 return ret->ReturnCode;
4365 wLog* log = scard_log();
4369 UINT32 cbDataLen = ret->cbDataLen;
4370 smartcard_trace_read_cache_return(log, ret);
4371 if (ret->ReturnCode != SCARD_S_SUCCESS)
4374 if (cbDataLen == SCARD_AUTOALLOCATE)
4377 if (!Stream_EnsureRemainingCapacity(s, 4))
4379 WLog_Print(log, WLOG_ERROR,
"Stream_EnsureRemainingCapacity failed!");
4380 return SCARD_F_INTERNAL_ERROR;
4383 Stream_Write_UINT32(s, cbDataLen);
4384 if (!smartcard_ndr_pointer_write(s, &index, cbDataLen))
4385 return SCARD_E_NO_MEMORY;
4387 status = smartcard_ndr_write(s, ret->pbData, cbDataLen, 1, NDR_PTR_SIMPLE);
4388 if (status != SCARD_S_SUCCESS)
4390 return ret->ReturnCode;
4396 wLog* log = scard_log();
4399 smartcard_trace_context_call(log, call, name);
4401 LONG status = smartcard_pack_redir_scard_context(log, s, &call->handles.hContext, &index);
4402 if (status != SCARD_S_SUCCESS)
4405 return smartcard_pack_redir_scard_context_ref(log, s, &call->handles.hContext);
4411 wLog* log = scard_log();
4414 smartcard_trace_list_readers_call(log, call, unicode);
4416 LONG status = smartcard_pack_redir_scard_context(log, s, &call->handles.hContext, &index);
4417 if (status != SCARD_S_SUCCESS)
4420 if (!Stream_EnsureRemainingCapacity(s, 4))
4421 return SCARD_E_NO_MEMORY;
4423 Stream_Write_UINT32(s, call->cBytes);
4424 if (!smartcard_ndr_pointer_write(s, &index, call->cBytes))
4425 return SCARD_E_NO_MEMORY;
4427 if (!Stream_EnsureRemainingCapacity(s, 8))
4428 return SCARD_E_NO_MEMORY;
4430 Stream_Write_INT32(s, call->fmszReadersIsNULL);
4431 Stream_Write_UINT32(s, call->cchReaders);
4433 status = smartcard_pack_redir_scard_context_ref(log, s, &call->handles.hContext);
4434 if (status != SCARD_S_SUCCESS)
4437 if (call->cBytes > 0)
4439 status = smartcard_ndr_write(s, call->mszGroups, call->cBytes, 1, NDR_PTR_SIMPLE);
4440 if (status != SCARD_S_SUCCESS)
4444 return SCARD_S_SUCCESS;
4447WINPR_ATTR_NODISCARD
static LONG
4449 UINT32 cReaders, UINT32* ptrIndex)
4451 WINPR_ASSERT(rgReaderStates || (cReaders == 0));
4453 if (!Stream_EnsureRemainingCapacity(s, 4))
4454 return SCARD_E_NO_MEMORY;
4456 Stream_Write_UINT32(s, cReaders);
4458 for (UINT32 i = 0; i < cReaders; i++)
4462 if (!Stream_EnsureRemainingCapacity(s, 52))
4463 return SCARD_E_NO_MEMORY;
4465 const UINT32 nameLen = state->szReader ? (UINT32)(strlen(state->szReader) + 1) : 0;
4466 if (!smartcard_ndr_pointer_write(s, ptrIndex, nameLen))
4467 return SCARD_E_NO_MEMORY;
4469 Stream_Write_UINT32(s, state->dwCurrentState);
4470 Stream_Write_UINT32(s, state->dwEventState);
4471 Stream_Write_UINT32(s, state->cbAtr);
4472 Stream_Write(s, state->rgbAtr, 36);
4475 for (UINT32 i = 0; i < cReaders; i++)
4479 if (state->szReader)
4481 const UINT32 nameLen = (UINT32)(strlen(state->szReader) + 1);
4482 LONG status = smartcard_ndr_write_a(s, state->szReader, nameLen, NDR_PTR_FULL);
4483 if (status != SCARD_S_SUCCESS)
4488 return SCARD_S_SUCCESS;
4491WINPR_ATTR_NODISCARD
static LONG
4493 UINT32 cReaders, UINT32* ptrIndex)
4495 WINPR_ASSERT(rgReaderStates || (cReaders == 0));
4497 if (!Stream_EnsureRemainingCapacity(s, 4))
4498 return SCARD_E_NO_MEMORY;
4500 Stream_Write_UINT32(s, cReaders);
4502 for (UINT32 i = 0; i < cReaders; i++)
4506 if (!Stream_EnsureRemainingCapacity(s, 52))
4507 return SCARD_E_NO_MEMORY;
4509 const UINT32 nameLen = state->szReader ? (UINT32)(_wcslen(state->szReader) + 1) : 0;
4510 if (!smartcard_ndr_pointer_write(s, ptrIndex, nameLen))
4511 return SCARD_E_NO_MEMORY;
4513 Stream_Write_UINT32(s, state->dwCurrentState);
4514 Stream_Write_UINT32(s, state->dwEventState);
4515 Stream_Write_UINT32(s, state->cbAtr);
4516 Stream_Write(s, state->rgbAtr, 36);
4519 for (UINT32 i = 0; i < cReaders; i++)
4523 if (state->szReader)
4525 const UINT32 nameLen = (UINT32)(_wcslen(state->szReader) + 1);
4526 LONG status = smartcard_ndr_write_w(s, state->szReader, nameLen, NDR_PTR_FULL);
4527 if (status != SCARD_S_SUCCESS)
4532 return SCARD_S_SUCCESS;
4538 wLog* log = scard_log();
4541 smartcard_trace_get_status_change_a_call(log, call);
4543 LONG status = smartcard_pack_redir_scard_context(log, s, &call->handles.hContext, &index);
4544 if (status != SCARD_S_SUCCESS)
4547 if (!Stream_EnsureRemainingCapacity(s, 8))
4548 return SCARD_E_NO_MEMORY;
4550 Stream_Write_UINT32(s, call->dwTimeOut);
4551 Stream_Write_UINT32(s, call->cReaders);
4553 if (!smartcard_ndr_pointer_write(s, &index, call->cReaders))
4554 return SCARD_E_NO_MEMORY;
4556 status = smartcard_pack_redir_scard_context_ref(log, s, &call->handles.hContext);
4557 if (status != SCARD_S_SUCCESS)
4560 if (call->cReaders > 0)
4562 status = smartcard_pack_reader_state_a(s, call->rgReaderStates, call->cReaders, &index);
4563 if (status != SCARD_S_SUCCESS)
4567 return SCARD_S_SUCCESS;
4573 wLog* log = scard_log();
4576 smartcard_trace_get_status_change_w_call(log, call);
4578 LONG status = smartcard_pack_redir_scard_context(log, s, &call->handles.hContext, &index);
4579 if (status != SCARD_S_SUCCESS)
4582 if (!Stream_EnsureRemainingCapacity(s, 8))
4583 return SCARD_E_NO_MEMORY;
4585 Stream_Write_UINT32(s, call->dwTimeOut);
4586 Stream_Write_UINT32(s, call->cReaders);
4588 if (!smartcard_ndr_pointer_write(s, &index, call->cReaders))
4589 return SCARD_E_NO_MEMORY;
4591 status = smartcard_pack_redir_scard_context_ref(log, s, &call->handles.hContext);
4592 if (status != SCARD_S_SUCCESS)
4595 if (call->cReaders > 0)
4597 status = smartcard_pack_reader_state_w(s, call->rgReaderStates, call->cReaders, &index);
4598 if (status != SCARD_S_SUCCESS)
4602 return SCARD_S_SUCCESS;
4608 wLog* log = scard_log();
4611 smartcard_trace_connect_a_call(log, call);
4613 const UINT32 readerLen = call->szReader ? (UINT32)(strlen(call->szReader) + 1) : 0;
4614 if (!smartcard_ndr_pointer_write(s, &index, readerLen))
4615 return SCARD_E_NO_MEMORY;
4618 smartcard_pack_redir_scard_context(log, s, &call->Common.handles.hContext, &index);
4619 if (status != SCARD_S_SUCCESS)
4622 if (!Stream_EnsureRemainingCapacity(s, 8))
4623 return SCARD_E_NO_MEMORY;
4625 Stream_Write_UINT32(s, call->Common.dwShareMode);
4626 Stream_Write_UINT32(s, call->Common.dwPreferredProtocols);
4630 status = smartcard_ndr_write_a(s, call->szReader, readerLen, NDR_PTR_FULL);
4631 if (status != SCARD_S_SUCCESS)
4635 return smartcard_pack_redir_scard_context_ref(log, s, &call->Common.handles.hContext);
4641 wLog* log = scard_log();
4644 smartcard_trace_connect_w_call(log, call);
4646 const UINT32 readerLen = call->szReader ? (UINT32)(_wcslen(call->szReader) + 1) : 0;
4647 if (!smartcard_ndr_pointer_write(s, &index, readerLen))
4648 return SCARD_E_NO_MEMORY;
4651 smartcard_pack_redir_scard_context(log, s, &call->Common.handles.hContext, &index);
4652 if (status != SCARD_S_SUCCESS)
4655 if (!Stream_EnsureRemainingCapacity(s, 8))
4656 return SCARD_E_NO_MEMORY;
4658 Stream_Write_UINT32(s, call->Common.dwShareMode);
4659 Stream_Write_UINT32(s, call->Common.dwPreferredProtocols);
4663 status = smartcard_ndr_write_w(s, call->szReader, readerLen, NDR_PTR_FULL);
4664 if (status != SCARD_S_SUCCESS)
4668 return smartcard_pack_redir_scard_context_ref(log, s, &call->Common.handles.hContext);
4674 wLog* log = scard_log();
4677 smartcard_trace_control_call(log, call);
4679 LONG status = smartcard_pack_redir_scard_context(log, s, &call->handles.hContext, &index);
4680 if (status != SCARD_S_SUCCESS)
4683 status = smartcard_pack_redir_scard_handle(log, s, &call->handles.hCard, &index);
4684 if (status != SCARD_S_SUCCESS)
4687 if (!Stream_EnsureRemainingCapacity(s, 8))
4688 return SCARD_E_NO_MEMORY;
4690 Stream_Write_UINT32(s, call->dwControlCode);
4691 Stream_Write_UINT32(s, call->cbInBufferSize);
4692 if (!smartcard_ndr_pointer_write(s, &index, call->cbInBufferSize))
4693 return SCARD_E_NO_MEMORY;
4695 Stream_Write_INT32(s, call->fpvOutBufferIsNULL);
4696 Stream_Write_UINT32(s, call->cbOutBufferSize);
4698 status = smartcard_pack_redir_scard_context_ref(log, s, &call->handles.hContext);
4699 if (status != SCARD_S_SUCCESS)
4702 status = smartcard_pack_redir_scard_handle_ref(log, s, &call->handles.hCard);
4703 if (status != SCARD_S_SUCCESS)
4706 if (call->cbInBufferSize)
4708 status = smartcard_ndr_write(s, call->pvInBuffer, call->cbInBufferSize, 1, NDR_PTR_SIMPLE);
4709 if (status != SCARD_S_SUCCESS)
4720 wLog* log = scard_log();
4723 smartcard_trace_hcard_and_disposition_call(log, call, name);
4725 LONG status = smartcard_pack_redir_scard_context(log, s, &call->handles.hContext, &index);
4726 if (status != SCARD_S_SUCCESS)
4729 status = smartcard_pack_redir_scard_handle(log, s, &call->handles.hCard, &index);
4730 if (status != SCARD_S_SUCCESS)
4733 if (!Stream_EnsureRemainingCapacity(s, 4))
4734 return SCARD_E_NO_MEMORY;
4736 Stream_Write_UINT32(s, call->dwDisposition);
4738 status = smartcard_pack_redir_scard_context_ref(log, s, &call->handles.hContext);
4739 if (status != SCARD_S_SUCCESS)
4742 return smartcard_pack_redir_scard_handle_ref(log, s, &call->handles.hCard);
4748 wLog* log = scard_log();
4751 smartcard_trace_transmit_call(log, call);
4753 LONG status = smartcard_pack_redir_scard_context(log, s, &call->handles.hContext, &index);
4754 if (status != SCARD_S_SUCCESS)
4757 status = smartcard_pack_redir_scard_handle(log, s, &call->handles.hCard, &index);
4758 if (status != SCARD_S_SUCCESS)
4761 UINT32 cbExtraBytes = 0;
4762 const BYTE* pbExtraBytes =
nullptr;
4763 if (call->pioSendPci)
4765 cbExtraBytes = (UINT32)(call->pioSendPci->cbPciLength -
sizeof(
SCARD_IO_REQUEST));
4766 if (cbExtraBytes > 0)
4770 if (!Stream_EnsureRemainingCapacity(s, 32))
4771 return SCARD_E_NO_MEMORY;
4773 Stream_Write_UINT32(s, call->pioSendPci ? call->pioSendPci->dwProtocol : 0);
4774 Stream_Write_UINT32(s, cbExtraBytes);
4775 if (!smartcard_ndr_pointer_write(s, &index, cbExtraBytes))
4776 return SCARD_E_NO_MEMORY;
4778 Stream_Write_UINT32(s, call->cbSendLength);
4779 if (!smartcard_ndr_pointer_write(s, &index, call->cbSendLength))
4780 return SCARD_E_NO_MEMORY;
4782 const UINT32 recvPciLen = call->pioRecvPci ? (UINT32)call->pioRecvPci->cbPciLength : 0;
4783 if (!smartcard_ndr_pointer_write(s, &index, recvPciLen))
4784 return SCARD_E_NO_MEMORY;
4786 Stream_Write_INT32(s, call->fpbRecvBufferIsNULL);
4787 Stream_Write_UINT32(s, call->cbRecvLength);
4789 status = smartcard_pack_redir_scard_context_ref(log, s, &call->handles.hContext);
4790 if (status != SCARD_S_SUCCESS)
4793 status = smartcard_pack_redir_scard_handle_ref(log, s, &call->handles.hCard);
4794 if (status != SCARD_S_SUCCESS)
4797 if (cbExtraBytes > 0)
4799 status = smartcard_ndr_write(s, pbExtraBytes, cbExtraBytes, 1, NDR_PTR_SIMPLE);
4800 if (status != SCARD_S_SUCCESS)
4804 if (call->cbSendLength > 0)
4806 status = smartcard_ndr_write(s, call->pbSendBuffer, call->cbSendLength, 1, NDR_PTR_SIMPLE);
4807 if (status != SCARD_S_SUCCESS)
4811 if (call->pioRecvPci && recvPciLen > 0)
4813 UINT32 cbRecvExtra = (UINT32)(call->pioRecvPci->cbPciLength -
sizeof(
SCARD_IO_REQUEST));
4814 const BYTE* pbRecvExtra = &((
const BYTE*)call->pioRecvPci)[
sizeof(
SCARD_IO_REQUEST)];
4816 if (!Stream_EnsureRemainingCapacity(s, 12))
4817 return SCARD_E_NO_MEMORY;
4819 Stream_Write_UINT32(s, call->pioRecvPci->dwProtocol);
4820 Stream_Write_UINT32(s, cbRecvExtra);
4821 if (!smartcard_ndr_pointer_write(s, &index, cbRecvExtra))
4822 return SCARD_E_NO_MEMORY;
4824 if (cbRecvExtra > 0)
4826 status = smartcard_ndr_write(s, pbRecvExtra, cbRecvExtra, 1, NDR_PTR_SIMPLE);
4827 if (status != SCARD_S_SUCCESS)
4832 return SCARD_S_SUCCESS;
4838 wLog* log = scard_log();
4841 smartcard_trace_get_attrib_call(log, call);
4843 LONG status = smartcard_pack_redir_scard_context(log, s, &call->handles.hContext, &index);
4844 if (status != SCARD_S_SUCCESS)
4847 status = smartcard_pack_redir_scard_handle(log, s, &call->handles.hCard, &index);
4848 if (status != SCARD_S_SUCCESS)
4851 if (!Stream_EnsureRemainingCapacity(s, 12))
4852 return STATUS_NO_MEMORY;
4854 Stream_Write_UINT32(s, call->dwAttrId);
4855 Stream_Write_INT32(s, call->fpbAttrIsNULL);
4856 Stream_Write_UINT32(s, call->cbAttrLen);
4858 status = smartcard_pack_redir_scard_context_ref(log, s, &call->handles.hContext);
4859 if (status != SCARD_S_SUCCESS)
4862 return smartcard_pack_redir_scard_handle_ref(log, s, &call->handles.hCard);
4868 wLog* log = scard_log();
4870 UINT32 mszNdrPtr = 0;
4872 if (!Stream_CheckAndLogRequiredLengthWLog(log, s, 4))
4873 return STATUS_BUFFER_TOO_SMALL;
4875 const UINT32 cBytes = Stream_Get_UINT32(s);
4877 if (!smartcard_ndr_pointer_read(log, s, &index, &mszNdrPtr))
4878 return ERROR_INVALID_DATA;
4882 LONG status = smartcard_ndr_read(log, s, &ret->msz, cBytes, 1, NDR_PTR_SIMPLE);
4883 if (status != SCARD_S_SUCCESS)
4885 ret->cBytes = cBytes;
4888 smartcard_trace_list_readers_return(log, ret, unicode);
4889 return SCARD_S_SUCCESS;
4896 wLog* log = scard_log();
4900 if (!Stream_CheckAndLogRequiredLengthWLog(log, s, 4))
4901 return STATUS_BUFFER_TOO_SMALL;
4903 Stream_Read_UINT32(s, ret->cReaders);
4905 if (!smartcard_ndr_pointer_read(log, s, &index, &ndrPtr))
4906 return ERROR_INVALID_DATA;
4908 if (ndrPtr && ret->cReaders > 0)
4911 smartcard_ndr_read_state(log, s, &ret->rgReaderStates, ret->cReaders, NDR_PTR_SIMPLE);
4912 if (status != SCARD_S_SUCCESS)
4916 ret->rgReaderStates =
nullptr;
4918 smartcard_trace_get_status_change_return(log, ret, unicode);
4919 return SCARD_S_SUCCESS;
4925 wLog* log = scard_log();
4927 UINT32 pbContextNdrPtr = 0;
4930 smartcard_unpack_redir_scard_context(log, s, &ret->hContext, &index, &pbContextNdrPtr);
4931 if (status != SCARD_S_SUCCESS)
4934 status = smartcard_unpack_redir_scard_handle(log, s, &ret->hCard, &index);
4935 if (status != SCARD_S_SUCCESS)
4938 if (!Stream_CheckAndLogRequiredLengthWLog(log, s, 4))
4939 return STATUS_BUFFER_TOO_SMALL;
4941 Stream_Read_UINT32(s, ret->dwActiveProtocol);
4943 status = smartcard_unpack_redir_scard_context_ref(log, s, pbContextNdrPtr, &ret->hContext);
4944 if (status != SCARD_S_SUCCESS)
4947 status = smartcard_unpack_redir_scard_handle_ref(log, s, &ret->hCard);
4948 if (status != SCARD_S_SUCCESS)
4951 smartcard_trace_connect_return(log, ret);
4952 return SCARD_S_SUCCESS;
4958 wLog* log = scard_log();
4960 UINT32 pvOutBufferNdrPtr = 0;
4962 if (!Stream_CheckAndLogRequiredLengthWLog(log, s, 8))
4963 return STATUS_BUFFER_TOO_SMALL;
4965 const UINT32 cbOutBufferSize = Stream_Get_UINT32(s);
4967 if (!smartcard_ndr_pointer_read(log, s, &index, &pvOutBufferNdrPtr))
4968 return ERROR_INVALID_DATA;
4970 if (pvOutBufferNdrPtr && (cbOutBufferSize > 0))
4973 smartcard_ndr_read(log, s, &ret->pvOutBuffer, cbOutBufferSize, 1, NDR_PTR_SIMPLE);
4974 if (status != SCARD_S_SUCCESS)
4976 ret->cbOutBufferSize = cbOutBufferSize;
4978 smartcard_trace_control_return(log, ret);
4979 return SCARD_S_SUCCESS;
4985 wLog* log = scard_log();
4987 UINT32 recvPciNdrPtr = 0;
4988 UINT32 recvBufferNdrPtr = 0;
4990 if (!smartcard_ndr_pointer_read(log, s, &index, &recvPciNdrPtr))
4991 return ERROR_INVALID_DATA;
4993 if (!Stream_CheckAndLogRequiredLengthWLog(log, s, 4))
4994 return STATUS_BUFFER_TOO_SMALL;
4996 const UINT32 cbRecvLength = Stream_Get_UINT32(s);
4998 if (!smartcard_ndr_pointer_read(log, s, &index, &recvBufferNdrPtr))
4999 return ERROR_INVALID_DATA;
5003 if (!Stream_CheckAndLogRequiredLengthWLog(log, s, 8))
5004 return STATUS_BUFFER_TOO_SMALL;
5006 UINT32 dwProtocol = 0;
5007 UINT32 cbExtraBytes = 0;
5008 Stream_Read_UINT32(s, dwProtocol);
5009 Stream_Read_UINT32(s, cbExtraBytes);
5011 UINT32 pbExtraBytesNdrPtr = 0;
5012 if (!smartcard_ndr_pointer_read(log, s, &index, &pbExtraBytesNdrPtr))
5013 return ERROR_INVALID_DATA;
5015 if (pbExtraBytesNdrPtr)
5017 if (!Stream_CheckAndLogRequiredLengthWLog(log, s, 4))
5018 return STATUS_BUFFER_TOO_SMALL;
5021 Stream_Read_UINT32(s, length);
5022 if (length != cbExtraBytes)
5024 WLog_Print(log, WLOG_WARN,
5025 "Transmit_Return unexpected length: Actual: %" PRIu32
5026 ", Expected: %" PRIu32
" (pioRecvPci.cbExtraBytes)",
5027 length, cbExtraBytes);
5028 return STATUS_INVALID_PARAMETER;
5031 if (!Stream_CheckAndLogRequiredLengthWLog(log, s, cbExtraBytes))
5032 return STATUS_BUFFER_TOO_SMALL;
5035 if (!ret->pioRecvPci)
5036 return SCARD_E_NO_MEMORY;
5038 ret->pioRecvPci->dwProtocol = dwProtocol;
5039 ret->pioRecvPci->cbPciLength = (DWORD)(
sizeof(
SCARD_IO_REQUEST) + cbExtraBytes);
5042 Stream_Read(s, pbExtraBytes, length);
5043 if (smartcard_unpack_read_size_align(s, cbExtraBytes, 4) < 0)
5044 return STATUS_INVALID_PARAMETER;
5049 if (!ret->pioRecvPci)
5050 return SCARD_E_NO_MEMORY;
5052 ret->pioRecvPci->dwProtocol = dwProtocol;
5057 if (recvBufferNdrPtr && (cbRecvLength > 0))
5060 smartcard_ndr_read(log, s, &ret->pbRecvBuffer, cbRecvLength, 1, NDR_PTR_SIMPLE);
5061 if (status != SCARD_S_SUCCESS)
5063 ret->cbRecvLength = cbRecvLength;
5066 smartcard_trace_transmit_return(log, ret);
5067 return SCARD_S_SUCCESS;
5073 wLog* log = scard_log();
5075 UINT32 pbAttrPtr = 0;
5077 if (!Stream_CheckAndLogRequiredLengthWLog(log, s, 4))
5078 return STATUS_BUFFER_TOO_SMALL;
5080 const UINT32 cbAttrLen = Stream_Get_UINT32(s);
5082 if (!smartcard_ndr_pointer_read(log, s, &index, &pbAttrPtr))
5083 return ERROR_INVALID_DATA;
5085 LONG status = smartcard_ndr_read(log, s, &ret->pbAttr, cbAttrLen, 1, NDR_PTR_SIMPLE);
5086 if (status != SCARD_S_SUCCESS)
5088 ret->cbAttrLen = cbAttrLen;
5090 smartcard_trace_get_attrib_return(log, ret, 0);
5091 return SCARD_S_SUCCESS;
5094BOOL smartcard_reader_state_return_is_valid_ex(
size_t pos,
const ReaderState_Return* val,
5095 const char* file,
size_t line,
const char* fkt)
5099 WLog_Print_dbg_tag(SCARD_TAG, WLOG_WARN, line, file, fkt,
"[%" PRIuz
"] val = nullptr",
5103 if (val->cbAtr >
sizeof(val->rgbAtr))
5105 WLog_Print_dbg_tag(SCARD_TAG, WLOG_WARN, line, file, fkt,
5106 "[%" PRIuz
"] val->cbAtr=%" PRIu32
", max=%" PRIuz, pos, val->cbAtr,
5107 sizeof(val->rgbAtr));