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__, \
53static LONG smartcard_unpack_redir_scard_context_(wLog* log,
wStream* s,
55 UINT32* ppbContextNdrPtr,
const char* file,
56 const char* function,
size_t line);
57static LONG smartcard_pack_redir_scard_context(wLog* log,
wStream* s,
60 UINT32* index,
const char* file,
61 const char* function,
size_t line);
62static LONG smartcard_pack_redir_scard_handle(wLog* log,
wStream* s,
64static LONG smartcard_unpack_redir_scard_context_ref(wLog* log,
wStream* s, UINT32 pbContextNdrPtr,
66static LONG smartcard_pack_redir_scard_context_ref(wLog* log,
wStream* s,
69static LONG smartcard_unpack_redir_scard_handle_ref(wLog* log,
wStream* s,
71static LONG smartcard_pack_redir_scard_handle_ref(wLog* log,
wStream* s,
83#define smartcard_ndr_pointer_read(log, s, index, ptr) \
84 smartcard_ndr_pointer_read_((log), (s), (index), (ptr), __FILE__, __func__, __LINE__)
85static BOOL smartcard_ndr_pointer_read_(wLog* log,
wStream* s, UINT32* index, UINT32* ptr,
86 const char* file,
const char* fkt,
size_t line)
88 const UINT32 expect = 0x20000 + (*index) * 4;
93 if (!Stream_CheckAndLogRequiredLengthWLog(log, s, 4))
96 Stream_Read_UINT32(s, ndrPtr);
102 if (ptr && (ndrPtr == 0))
104 WLog_Print(log, WLOG_WARN,
105 "[%s:%" PRIuz
"] Read context pointer 0x%08" PRIx32
", expected 0x%08" PRIx32,
106 fkt, line, ndrPtr, expect);
110 (*index) = (*index) + 1;
114static LONG smartcard_ndr_read_ex(wLog* log,
wStream* s, BYTE** data,
size_t min,
115 size_t elementSize,
const ndr_ptr_t type,
size_t* plen)
139 return STATUS_INVALID_PARAMETER;
142 if (!Stream_CheckAndLogRequiredLengthWLog(log, s, required))
143 return STATUS_BUFFER_TOO_SMALL;
148 Stream_Read_UINT32(s, len);
149 Stream_Read_UINT32(s, offset);
150 Stream_Read_UINT32(s, len2);
151 if (len != offset + len2)
153 WLog_Print(log, WLOG_ERROR,
154 "Invalid data when reading full NDR pointer: total=%" PRIuz
155 ", offset=%" PRIuz
", remaining=%" PRIuz,
157 return STATUS_BUFFER_TOO_SMALL;
161 Stream_Read_UINT32(s, len);
163 if ((len != min) && (min > 0))
165 WLog_Print(log, WLOG_ERROR,
166 "Invalid data when reading simple NDR pointer: total=%" PRIuz
167 ", expected=%" PRIuz,
169 return STATUS_BUFFER_TOO_SMALL;
176 return STATUS_INVALID_PARAMETER;
181 WLog_Print(log, WLOG_ERROR,
182 "Invalid length read from NDR pointer, minimum %" PRIuz
", got %" PRIuz, min,
184 return STATUS_DATA_ERROR;
187 if (len > SIZE_MAX / 2)
188 return STATUS_BUFFER_TOO_SMALL;
190 if (!Stream_CheckAndLogRequiredLengthOfSizeWLog(log, s, len, elementSize))
191 return STATUS_BUFFER_TOO_SMALL;
198 r = calloc(len +
sizeof(WCHAR),
sizeof(CHAR));
200 return SCARD_E_NO_MEMORY;
201 Stream_Read(s, r, len);
202 const LONG pad = smartcard_unpack_read_size_align(s, len, 4);
206 return STATUS_INVALID_PARAMETER;
211 return STATUS_SUCCESS;
214static LONG smartcard_ndr_read(wLog* log,
wStream* s, BYTE** data,
size_t min,
size_t elementSize,
215 const ndr_ptr_t type)
217 return smartcard_ndr_read_ex(log, s, data, min, elementSize, type,
nullptr);
220static BOOL smartcard_ndr_pointer_write(
wStream* s, UINT32* index, DWORD length)
222 const UINT32 ndrPtr = 0x20000 + (*index) * 4;
226 if (!Stream_EnsureRemainingCapacity(s, 4))
231 Stream_Write_UINT32(s, ndrPtr);
232 (*index) = (*index) + 1;
235 Stream_Write_UINT32(s, 0);
239static LONG smartcard_ndr_write(
wStream* s,
const BYTE* data, UINT32 size, UINT32 elementSize,
240 const ndr_ptr_t type)
242 const UINT32 offset = 0;
243 const UINT32 len = size;
244 const UINT32 dataLen = size * elementSize;
248 return SCARD_S_SUCCESS;
262 return SCARD_E_INVALID_PARAMETER;
265 if (!Stream_EnsureRemainingCapacity(s, required + dataLen + 4))
266 return STATUS_BUFFER_TOO_SMALL;
271 Stream_Write_UINT32(s, len);
272 Stream_Write_UINT32(s, offset);
273 Stream_Write_UINT32(s, len);
276 Stream_Write_UINT32(s, len);
281 return SCARD_E_INVALID_PARAMETER;
285 Stream_Write(s, data, dataLen);
287 Stream_Zero(s, dataLen);
288 return smartcard_pack_write_size_align(s, dataLen, 4);
292 const ndr_ptr_t type)
300 WINPR_ASSERT(data || (size == 0));
306 const ndr_ptr_t type)
318 size_t min,
const ndr_ptr_t type)
326 const LONG status = smartcard_ndr_read(log, s, u.ppv, min,
sizeof(
LocateCards_ATRMask), type);
327 if (status != SCARD_S_SUCCESS)
334 for (
size_t x = 0; x < min; x++)
336 const UINT32 val = winpr_Data_Get_UINT32(&masks[x].cbAtr);
337 if (val > ARRAYSIZE(masks[x].rgbAtr))
339 WLog_Print(log, WLOG_ERROR,
340 "LocateCards_ATRMask[%" PRIuz
"]::cbAtr %" PRIu32
" exceeds %" PRIuz, x, val,
341 (
size_t)ARRAYSIZE(masks[x].rgbAtr));
344 return STATUS_DATA_ERROR;
347 return SCARD_S_SUCCESS;
350static LONG smartcard_ndr_read_fixed_string_a(wLog* log,
wStream* s, CHAR** data,
size_t min,
351 const ndr_ptr_t type)
359 return smartcard_ndr_read(log, s, u.ppv, min,
sizeof(CHAR), type);
362static LONG smartcard_ndr_read_fixed_string_w(wLog* log,
wStream* s, WCHAR** data,
size_t min,
363 const ndr_ptr_t type)
371 return smartcard_ndr_read(log, s, u.ppv, min,
sizeof(WCHAR), type);
374static LONG smartcard_ndr_read_a(wLog* log,
wStream* s, CHAR** data,
const ndr_ptr_t type)
382 return smartcard_ndr_read(log, s, u.ppv, 0,
sizeof(CHAR), type);
385static LONG smartcard_ndr_read_w(wLog* log,
wStream* s, WCHAR** data,
const ndr_ptr_t type)
393 return smartcard_ndr_read(log, s, u.ppv, 0,
sizeof(WCHAR), type);
396static LONG smartcard_ndr_write_w(
wStream* s,
const WCHAR* data, UINT32 size,
const ndr_ptr_t type)
404 return smartcard_ndr_write(s, cnv.bp, size,
sizeof(WCHAR), type);
407static LONG smartcard_ndr_write_a(
wStream* s,
const CHAR* data, UINT32 size,
const ndr_ptr_t type)
415 return smartcard_ndr_write(s, cnv.bp, size,
sizeof(CHAR), type);
418static LONG smartcard_ndr_read_u(wLog* log,
wStream* s,
UUID** data)
426 return smartcard_ndr_read(log, s, u.ppv, 1,
sizeof(
UUID), NDR_PTR_FIXED);
429static char* smartcard_convert_string_list(
const void* in,
size_t bytes, BOOL unicode)
438 char* mszA =
nullptr;
450 mszA = ConvertMszWCharNToUtf8Alloc(
string.wz, bytes /
sizeof(WCHAR), &length);
456 mszA = (
char*)calloc(bytes,
sizeof(
char));
459 CopyMemory(mszA,
string.sz, bytes - 1);
468 for (
size_t index = 0; index < length - 1; index++)
470 if (mszA[index] ==
'\0')
477WINPR_ATTR_MALLOC(free, 1)
478static
char* smartcard_create_msz_dump(const
char* msz,
size_t len)
480 size_t bufferLen = len;
481 char* buffer = calloc(len + 1, 1);
486 const char* cur = msz;
488 while ((len > 0) && cur && cur[0] !=
'\0' && (bufferLen > 0))
490 size_t clen = strnlen(cur, len);
491 int rc = _snprintf(buf, bufferLen,
"%s", cur);
492 bufferLen -= (size_t)rc;
501static void smartcard_msz_dump(wLog* log, DWORD level,
const char* prefix,
const void* data,
502 size_t len, UINT32 wchar)
504 if (!WLog_IsLevelActive(log, level))
508 const char* msz = WINPR_CXX_COMPAT_CAST(
const char*, data);
512 tmp = ConvertMszWCharNToUtf8Alloc(data, len, &mszlen);
516 char* array = smartcard_create_msz_dump(msz, mszlen);
517 WLog_Print(log, level,
"%s%s", prefix, array);
522WINPR_ATTR_MALLOC(free, 1)
523static
char* smartcard_create_array_dump(const
void* pd,
size_t len)
525 const BYTE* data = pd;
528 size_t bufferLen = len * 4;
531 char* buffer = calloc(bufferLen + 1, 1);
534 char* start = buffer;
536 WINPR_ASSERT(buffer || (bufferLen == 0));
538 if (!data && (len > 0))
540 (void)_snprintf(buffer, bufferLen,
"{ nullptr [%" PRIuz
"] }", len);
544 rc = _snprintf(buffer, bufferLen,
"{ ");
545 if ((rc < 0) || ((
size_t)rc >= bufferLen))
548 bufferLen -= (size_t)rc;
550 for (
size_t x = 0; x < len; x++)
552 rc = _snprintf(buffer, bufferLen,
"%02X", data[x]);
553 if ((rc < 0) || ((
size_t)rc >= bufferLen))
556 bufferLen -= (size_t)rc;
559 rc = _snprintf(buffer, bufferLen,
" }");
560 if ((rc < 0) || ((
size_t)rc >= bufferLen))
567WINPR_ATTR_FORMAT_ARG(3, 7)
568static
void smartcard_dump_array(wLog* log, DWORD level, WINPR_FORMAT_ARG const
char* prefix,
569 const
char* postfix, const
void* data,
size_t len, ...)
571 if (!WLog_IsLevelActive(log, level))
574 char* buffer = smartcard_create_array_dump(data, len);
576 char* fprefix =
nullptr;
578 va_list ap = WINPR_C_ARRAY_INIT;
580 winpr_vasprintf(&fprefix, &flen, prefix, ap);
582 WLog_Print(log, level,
"%s%s%s", fprefix, buffer, postfix);
587static void smartcard_log_redir_handle(wLog* log,
const REDIR_SCARDHANDLE* pHandle)
589 WINPR_ASSERT(pHandle);
590 smartcard_dump_array(log, g_LogLevel,
"handle: ",
"", pHandle->pbHandle, pHandle->cbHandle);
595 WINPR_ASSERT(phContext);
596 smartcard_dump_array(log, g_LogLevel,
"hContext: ",
"", phContext->pbContext,
597 phContext->cbContext);
600static void smartcard_trace_context_and_string_call_a(wLog* log,
const char* name,
604 if (!WLog_IsLevelActive(log, g_LogLevel))
607 WLog_Print(log, g_LogLevel,
"%s {", name);
608 smartcard_log_context(log, phContext);
609 WLog_Print(log, g_LogLevel,
" sz=%s", sz);
611 WLog_Print(log, g_LogLevel,
"}");
614static void smartcard_trace_context_and_string_call_w(wLog* log,
const char* name,
620 if (!WLog_IsLevelActive(log, g_LogLevel))
624 tmp = ConvertWCharToUtf8Alloc(sz,
nullptr);
626 WLog_Print(log, g_LogLevel,
"%s {", name);
627 smartcard_log_context(log, phContext);
628 WLog_Print(log, g_LogLevel,
" sz=%s", tmp);
629 WLog_Print(log, g_LogLevel,
"}");
633static void smartcard_trace_context_call(wLog* log,
const Context_Call* call,
const char* name)
637 if (!WLog_IsLevelActive(log, g_LogLevel))
640 WLog_Print(log, g_LogLevel,
"%s_Call {", name);
641 smartcard_log_context(log, &call->handles.hContext);
643 WLog_Print(log, g_LogLevel,
"}");
651 if (!WLog_IsLevelActive(log, g_LogLevel))
654 WLog_Print(log, g_LogLevel,
"ListReaderGroups%s_Call {", unicode ?
"W" :
"A");
655 smartcard_log_context(log, &call->handles.hContext);
657 WLog_Print(log, g_LogLevel,
"fmszGroupsIsNULL: %" PRId32
" cchGroups: 0x%08" PRIx32,
658 call->fmszGroupsIsNULL, call->cchGroups);
659 WLog_Print(log, g_LogLevel,
"}");
662static void dump_reader_states_return(wLog* log,
const ReaderState_Return* rgReaderStates,
665 WINPR_ASSERT(rgReaderStates || (cReaders == 0));
666 for (UINT32 index = 0; index < cReaders; index++)
669 char* szCurrentState = SCardGetReaderStateString(readerState->dwCurrentState);
670 char* szEventState = SCardGetReaderStateString(readerState->dwEventState);
671 WLog_Print(log, g_LogLevel,
"\t[%" PRIu32
"]: dwCurrentState: %s (0x%08" PRIX32
")", index,
672 szCurrentState, readerState->dwCurrentState);
673 WLog_Print(log, g_LogLevel,
"\t[%" PRIu32
"]: dwEventState: %s (0x%08" PRIX32
")", index,
674 szEventState, readerState->dwEventState);
675 free(szCurrentState);
678 smartcard_dump_array(log, g_LogLevel,
"\t[%" PRIu32
"]: cbAttr: %" PRIu32
" { ",
" }",
679 readerState->rgbAtr, readerState->cbAtr, index, readerState->cbAtr);
686 WINPR_ASSERT(rgReaderStates || (cReaders == 0));
687 for (UINT32 index = 0; index < cReaders; index++)
691 WLog_Print(log, g_LogLevel,
"\t[%" PRIu32
"]: szReader: %s cbAtr: %" PRIu32
"", index,
692 readerState->szReader, readerState->cbAtr);
693 char* szCurrentState = SCardGetReaderStateString(readerState->dwCurrentState);
694 char* szEventState = SCardGetReaderStateString(readerState->dwEventState);
695 WLog_Print(log, g_LogLevel,
"\t[%" PRIu32
"]: dwCurrentState: %s (0x%08" PRIX32
")", index,
696 szCurrentState, readerState->dwCurrentState);
697 WLog_Print(log, g_LogLevel,
"\t[%" PRIu32
"]: dwEventState: %s (0x%08" PRIX32
")", index,
698 szEventState, readerState->dwEventState);
699 free(szCurrentState);
702 smartcard_dump_array(log, g_LogLevel,
"\t[%" PRIu32
"]: cbAttr: %" PRIu32
" { ",
" }",
703 readerState->rgbAtr, readerState->cbAtr, index, readerState->cbAtr);
710 WINPR_ASSERT(rgReaderStates || (cReaders == 0));
711 for (UINT32 index = 0; index < cReaders; index++)
714 char* buffer = ConvertWCharToUtf8Alloc(readerState->szReader,
nullptr);
715 WLog_Print(log, g_LogLevel,
"\t[%" PRIu32
"]: szReader: %s cbAtr: %" PRIu32
"", index,
716 buffer, readerState->cbAtr);
718 char* szCurrentState = SCardGetReaderStateString(readerState->dwCurrentState);
719 char* szEventState = SCardGetReaderStateString(readerState->dwEventState);
720 WLog_Print(log, g_LogLevel,
"\t[%" PRIu32
"]: dwCurrentState: %s (0x%08" PRIX32
")", index,
721 szCurrentState, readerState->dwCurrentState);
722 WLog_Print(log, g_LogLevel,
"\t[%" PRIu32
"]: dwEventState: %s (0x%08" PRIX32
")", index,
723 szEventState, readerState->dwEventState);
724 free(szCurrentState);
727 smartcard_dump_array(log, g_LogLevel,
"\t[%" PRIu32
"]: cbAttr: %" PRIu32
" { ",
" }",
728 readerState->rgbAtr, readerState->cbAtr, index, readerState->cbAtr);
736 if (!WLog_IsLevelActive(log, g_LogLevel))
739 WLog_Print(log, g_LogLevel,
"GetStatusChangeW_Call {");
740 smartcard_log_context(log, &call->handles.hContext);
742 WLog_Print(log, g_LogLevel,
"dwTimeOut: 0x%08" PRIX32
" cReaders: %" PRIu32
"", call->dwTimeOut,
745 dump_reader_states_w(log, call->rgReaderStates, call->cReaders);
747 WLog_Print(log, g_LogLevel,
"}");
755 if (!WLog_IsLevelActive(log, g_LogLevel))
758 char* mszA = smartcard_convert_string_list(ret->msz, ret->cBytes, unicode);
760 WLog_Print(log, g_LogLevel,
"ListReaderGroups%s_Return {", unicode ?
"W" :
"A");
761 WLog_Print(log, g_LogLevel,
" ReturnCode: %s (0x%08" PRIx32
")",
762 SCardGetErrorString(ret->ReturnCode),
763 WINPR_CXX_COMPAT_CAST(UINT32, ret->ReturnCode));
764 WLog_Print(log, g_LogLevel,
" cBytes: %" PRIu32
" msz: %s", ret->cBytes, mszA);
765 WLog_Print(log, g_LogLevel,
"}");
769static void smartcard_trace_list_readers_call(wLog* log,
const ListReaders_Call* call, BOOL unicode)
773 if (!WLog_IsLevelActive(log, g_LogLevel))
776 char* mszGroupsA = smartcard_convert_string_list(call->mszGroups, call->cBytes, unicode);
778 WLog_Print(log, g_LogLevel,
"ListReaders%s_Call {", unicode ?
"W" :
"A");
779 smartcard_log_context(log, &call->handles.hContext);
781 WLog_Print(log, g_LogLevel,
782 "cBytes: %" PRIu32
" mszGroups: %s fmszReadersIsNULL: %" PRId32
783 " cchReaders: 0x%08" PRIX32
"",
784 call->cBytes, mszGroupsA, call->fmszReadersIsNULL, call->cchReaders);
785 WLog_Print(log, g_LogLevel,
"}");
790static void smartcard_trace_locate_cards_by_atr_a_call(wLog* log,
795 if (!WLog_IsLevelActive(log, g_LogLevel))
798 WLog_Print(log, g_LogLevel,
"LocateCardsByATRA_Call {");
799 smartcard_log_context(log, &call->handles.hContext);
801 dump_reader_states_a(log, call->rgReaderStates, call->cReaders);
803 WLog_Print(log, g_LogLevel,
"}");
806static void smartcard_trace_locate_cards_a_call(wLog* log,
const LocateCardsA_Call* call)
810 if (!WLog_IsLevelActive(log, g_LogLevel))
813 WLog_Print(log, g_LogLevel,
"LocateCardsA_Call {");
814 smartcard_log_context(log, &call->handles.hContext);
815 WLog_Print(log, g_LogLevel,
" cBytes=%" PRIu32, call->cBytes);
816 smartcard_msz_dump(log, g_LogLevel,
" mszCards=", call->mszCards, call->cBytes, FALSE);
817 WLog_Print(log, g_LogLevel,
" cReaders=%" PRIu32, call->cReaders);
818 dump_reader_states_a(log, call->rgReaderStates, call->cReaders);
820 WLog_Print(log, g_LogLevel,
"}");
823static void smartcard_trace_locate_cards_return(wLog* log,
const LocateCards_Return* ret)
827 if (!WLog_IsLevelActive(log, g_LogLevel))
830 WLog_Print(log, g_LogLevel,
"LocateCards_Return {");
831 WLog_Print(log, g_LogLevel,
" ReturnCode: %s (0x%08" PRIX32
")",
832 SCardGetErrorString(ret->ReturnCode),
833 WINPR_CXX_COMPAT_CAST(UINT32, ret->ReturnCode));
835 if (ret->ReturnCode == SCARD_S_SUCCESS)
837 WLog_Print(log, g_LogLevel,
" cReaders=%" PRIu32, ret->cReaders);
839 WLog_Print(log, g_LogLevel,
"}");
846 if (!WLog_IsLevelActive(log, g_LogLevel))
849 WLog_Print(log, g_LogLevel,
"GetReaderIcon_Return {");
850 WLog_Print(log, g_LogLevel,
" ReturnCode: %s (0x%08" PRIX32
")",
851 SCardGetErrorString(ret->ReturnCode),
852 WINPR_CXX_COMPAT_CAST(UINT32, ret->ReturnCode));
854 if (ret->ReturnCode == SCARD_S_SUCCESS)
856 WLog_Print(log, g_LogLevel,
" cbDataLen=%" PRIu32, ret->cbDataLen);
858 WLog_Print(log, g_LogLevel,
"}");
865 if (!WLog_IsLevelActive(log, g_LogLevel))
868 WLog_Print(log, g_LogLevel,
"GetTransmitCount_Return {");
869 WLog_Print(log, g_LogLevel,
" ReturnCode: %s (0x%08" PRIX32
")",
870 SCardGetErrorString(ret->ReturnCode),
871 WINPR_CXX_COMPAT_CAST(UINT32, ret->ReturnCode));
873 WLog_Print(log, g_LogLevel,
" cTransmitCount=%" PRIu32, ret->cTransmitCount);
874 WLog_Print(log, g_LogLevel,
"}");
877static void smartcard_trace_read_cache_return(wLog* log,
const ReadCache_Return* ret)
881 if (!WLog_IsLevelActive(log, g_LogLevel))
884 WLog_Print(log, g_LogLevel,
"ReadCache_Return {");
885 WLog_Print(log, g_LogLevel,
" ReturnCode: %s (0x%08" PRIX32
")",
886 SCardGetErrorString(ret->ReturnCode),
887 WINPR_CXX_COMPAT_CAST(UINT32, ret->ReturnCode));
889 if (ret->ReturnCode == SCARD_S_SUCCESS)
891 WLog_Print(log, g_LogLevel,
" cbDataLen=%" PRIu32, ret->cbDataLen);
892 smartcard_dump_array(log, g_LogLevel,
" cbData: ",
"", ret->pbData, ret->cbDataLen);
894 WLog_Print(log, g_LogLevel,
"}");
897static void smartcard_trace_locate_cards_w_call(wLog* log,
const LocateCardsW_Call* call)
901 if (!WLog_IsLevelActive(log, g_LogLevel))
904 WLog_Print(log, g_LogLevel,
"LocateCardsW_Call {");
905 smartcard_log_context(log, &call->handles.hContext);
906 WLog_Print(log, g_LogLevel,
" cBytes=%" PRIu32, call->cBytes);
907 smartcard_msz_dump(log, g_LogLevel,
" sz2=", call->mszCards, call->cBytes, TRUE);
908 WLog_Print(log, g_LogLevel,
" cReaders=%" PRIu32, call->cReaders);
909 dump_reader_states_w(log, call->rgReaderStates, call->cReaders);
910 WLog_Print(log, g_LogLevel,
"}");
913static void smartcard_trace_list_readers_return(wLog* log,
const ListReaders_Return* ret,
918 if (!WLog_IsLevelActive(log, g_LogLevel))
921 WLog_Print(log, g_LogLevel,
"ListReaders%s_Return {", unicode ?
"W" :
"A");
922 WLog_Print(log, g_LogLevel,
" ReturnCode: %s (0x%08" PRIX32
")",
923 SCardGetErrorString(ret->ReturnCode),
924 WINPR_CXX_COMPAT_CAST(UINT32, ret->ReturnCode));
926 if (ret->ReturnCode != SCARD_S_SUCCESS)
928 WLog_Print(log, g_LogLevel,
"}");
932 char* mszA = smartcard_convert_string_list(ret->msz, ret->cBytes, unicode);
934 WLog_Print(log, g_LogLevel,
" cBytes: %" PRIu32
" msz: %s", ret->cBytes, mszA);
935 WLog_Print(log, g_LogLevel,
"}");
944 if (!WLog_IsLevelActive(log, g_LogLevel))
947 WLog_Print(log, g_LogLevel,
"GetStatusChange%s_Return {", unicode ?
"W" :
"A");
948 WLog_Print(log, g_LogLevel,
" ReturnCode: %s (0x%08" PRIX32
")",
949 SCardGetErrorString(ret->ReturnCode),
950 WINPR_CXX_COMPAT_CAST(UINT32, ret->ReturnCode));
951 WLog_Print(log, g_LogLevel,
" cReaders: %" PRIu32
"", ret->cReaders);
953 dump_reader_states_return(log, ret->rgReaderStates, ret->cReaders);
955 if (!ret->rgReaderStates && (ret->cReaders > 0))
957 WLog_Print(log, g_LogLevel,
" [INVALID STATE] rgReaderStates=nullptr, cReaders=%" PRIu32,
960 else if (ret->ReturnCode != SCARD_S_SUCCESS)
962 WLog_Print(log, g_LogLevel,
" [INVALID RETURN] rgReaderStates, cReaders=%" PRIu32,
967 for (UINT32 index = 0; index < ret->cReaders; index++)
970 char* szCurrentState = SCardGetReaderStateString(rgReaderState->dwCurrentState);
971 char* szEventState = SCardGetReaderStateString(rgReaderState->dwEventState);
972 WLog_Print(log, g_LogLevel,
" [%" PRIu32
"]: dwCurrentState: %s (0x%08" PRIX32
")",
973 index, szCurrentState, rgReaderState->dwCurrentState);
974 WLog_Print(log, g_LogLevel,
" [%" PRIu32
"]: dwEventState: %s (0x%08" PRIX32
")",
975 index, szEventState, rgReaderState->dwEventState);
976 smartcard_dump_array(
977 log, g_LogLevel,
" [%" PRIu32
"]: cbAtr: %" PRIu32
" rgbAtr: ",
"",
978 rgReaderState->rgbAtr, rgReaderState->cbAtr, index, rgReaderState->cbAtr);
979 free(szCurrentState);
984 WLog_Print(log, g_LogLevel,
"}");
987static void smartcard_trace_context_and_two_strings_a_call(wLog* log,
992 if (!WLog_IsLevelActive(log, g_LogLevel))
995 WLog_Print(log, g_LogLevel,
"ContextAndTwoStringW_Call {");
996 smartcard_log_context(log, &call->handles.hContext);
997 WLog_Print(log, g_LogLevel,
" sz1=%s", call->sz1);
998 WLog_Print(log, g_LogLevel,
" sz2=%s", call->sz2);
999 WLog_Print(log, g_LogLevel,
"}");
1002static void smartcard_trace_context_and_two_strings_w_call(wLog* log,
1006 char sz1[1024] = WINPR_C_ARRAY_INIT;
1007 char sz2[1024] = WINPR_C_ARRAY_INIT;
1009 if (!WLog_IsLevelActive(log, g_LogLevel))
1012 (void)ConvertWCharToUtf8(call->sz1, sz1, ARRAYSIZE(sz1));
1014 (void)ConvertWCharToUtf8(call->sz2, sz2, ARRAYSIZE(sz2));
1016 WLog_Print(log, g_LogLevel,
"ContextAndTwoStringW_Call {");
1017 smartcard_log_context(log, &call->handles.hContext);
1018 WLog_Print(log, g_LogLevel,
" sz1=%s", sz1);
1019 WLog_Print(log, g_LogLevel,
" sz2=%s", sz2);
1020 WLog_Print(log, g_LogLevel,
"}");
1027 if (!WLog_IsLevelActive(log, g_LogLevel))
1030 WLog_Print(log, g_LogLevel,
"GetTransmitCount_Call {");
1031 smartcard_log_context(log, &call->handles.hContext);
1032 smartcard_log_redir_handle(log, &call->handles.hCard);
1034 WLog_Print(log, g_LogLevel,
"}");
1037static void smartcard_trace_write_cache_a_call(wLog* log,
const WriteCacheA_Call* call)
1041 if (!WLog_IsLevelActive(log, g_LogLevel))
1044 WLog_Print(log, g_LogLevel,
"WriteCacheA_Call {");
1046 WLog_Print(log, g_LogLevel,
" szLookupName=%s", call->szLookupName);
1048 smartcard_log_context(log, &call->Common.handles.hContext);
1049 smartcard_dump_array(log, g_LogLevel,
"..CardIdentifier=",
"", call->Common.CardIdentifier,
1051 WLog_Print(log, g_LogLevel,
" FreshnessCounter=%" PRIu32, call->Common.FreshnessCounter);
1052 WLog_Print(log, g_LogLevel,
" cbDataLen=%" PRIu32, call->Common.cbDataLen);
1053 smartcard_dump_array(log, g_LogLevel,
" pbData=",
"", call->Common.pbData,
1054 call->Common.cbDataLen);
1055 WLog_Print(log, g_LogLevel,
"}");
1058static void smartcard_trace_write_cache_w_call(wLog* log,
const WriteCacheW_Call* call)
1061 char tmp[1024] = WINPR_C_ARRAY_INIT;
1063 if (!WLog_IsLevelActive(log, g_LogLevel))
1066 WLog_Print(log, g_LogLevel,
"WriteCacheW_Call {");
1068 if (call->szLookupName)
1069 (void)ConvertWCharToUtf8(call->szLookupName, tmp, ARRAYSIZE(tmp));
1070 WLog_Print(log, g_LogLevel,
" szLookupName=%s", tmp);
1072 smartcard_log_context(log, &call->Common.handles.hContext);
1073 smartcard_dump_array(log, g_LogLevel,
"..CardIdentifier=",
"", call->Common.CardIdentifier,
1075 WLog_Print(log, g_LogLevel,
" FreshnessCounter=%" PRIu32, call->Common.FreshnessCounter);
1076 WLog_Print(log, g_LogLevel,
" cbDataLen=%" PRIu32, call->Common.cbDataLen);
1077 smartcard_dump_array(log, g_LogLevel,
" pbData=",
"", call->Common.pbData,
1078 call->Common.cbDataLen);
1079 WLog_Print(log, g_LogLevel,
"}");
1082static void smartcard_trace_read_cache_a_call(wLog* log,
const ReadCacheA_Call* call)
1086 if (!WLog_IsLevelActive(log, g_LogLevel))
1089 WLog_Print(log, g_LogLevel,
"ReadCacheA_Call {");
1091 WLog_Print(log, g_LogLevel,
" szLookupName=%s", call->szLookupName);
1092 smartcard_log_context(log, &call->Common.handles.hContext);
1093 smartcard_dump_array(log, g_LogLevel,
"..CardIdentifier=",
"", call->Common.CardIdentifier,
1095 WLog_Print(log, g_LogLevel,
" FreshnessCounter=%" PRIu32, call->Common.FreshnessCounter);
1096 WLog_Print(log, g_LogLevel,
" fPbDataIsNULL=%" PRId32, call->Common.fPbDataIsNULL);
1097 WLog_Print(log, g_LogLevel,
" cbDataLen=%" PRIu32, call->Common.cbDataLen);
1099 WLog_Print(log, g_LogLevel,
"}");
1102static void smartcard_trace_read_cache_w_call(wLog* log,
const ReadCacheW_Call* call)
1105 char tmp[1024] = WINPR_C_ARRAY_INIT;
1107 if (!WLog_IsLevelActive(log, g_LogLevel))
1110 WLog_Print(log, g_LogLevel,
"ReadCacheW_Call {");
1111 if (call->szLookupName)
1112 (void)ConvertWCharToUtf8(call->szLookupName, tmp, ARRAYSIZE(tmp));
1113 WLog_Print(log, g_LogLevel,
" szLookupName=%s", tmp);
1115 smartcard_log_context(log, &call->Common.handles.hContext);
1116 smartcard_dump_array(log, g_LogLevel,
"..CardIdentifier=",
"", call->Common.CardIdentifier,
1118 WLog_Print(log, g_LogLevel,
" FreshnessCounter=%" PRIu32, call->Common.FreshnessCounter);
1119 WLog_Print(log, g_LogLevel,
" fPbDataIsNULL=%" PRId32, call->Common.fPbDataIsNULL);
1120 WLog_Print(log, g_LogLevel,
" cbDataLen=%" PRIu32, call->Common.cbDataLen);
1122 WLog_Print(log, g_LogLevel,
"}");
1125static void smartcard_trace_transmit_call(wLog* log,
const Transmit_Call* call)
1128 UINT32 cbExtraBytes = 0;
1129 BYTE* pbExtraBytes =
nullptr;
1131 if (!WLog_IsLevelActive(log, g_LogLevel))
1134 WLog_Print(log, g_LogLevel,
"Transmit_Call {");
1135 smartcard_log_context(log, &call->handles.hContext);
1136 smartcard_log_redir_handle(log, &call->handles.hCard);
1138 if (call->pioSendPci)
1140 cbExtraBytes = (UINT32)(call->pioSendPci->cbPciLength -
sizeof(
SCARD_IO_REQUEST));
1142 WLog_Print(log, g_LogLevel,
"pioSendPci: dwProtocol: %" PRIu32
" cbExtraBytes: %" PRIu32
"",
1143 call->pioSendPci->dwProtocol, cbExtraBytes);
1147 smartcard_dump_array(log, g_LogLevel,
"pbExtraBytes: ",
"", pbExtraBytes, cbExtraBytes);
1152 WLog_Print(log, g_LogLevel,
"pioSendPci: null");
1155 WLog_Print(log, g_LogLevel,
"cbSendLength: %" PRIu32
"", call->cbSendLength);
1157 if (call->pbSendBuffer)
1159 smartcard_dump_array(log, g_LogLevel,
"pbSendBuffer: ",
"", call->pbSendBuffer,
1160 call->cbSendLength);
1164 WLog_Print(log, g_LogLevel,
"pbSendBuffer: null");
1167 if (call->pioRecvPci)
1169 cbExtraBytes = (UINT32)(call->pioRecvPci->cbPciLength -
sizeof(
SCARD_IO_REQUEST));
1171 WLog_Print(log, g_LogLevel,
"pioRecvPci: dwProtocol: %" PRIu32
" cbExtraBytes: %" PRIu32
"",
1172 call->pioRecvPci->dwProtocol, cbExtraBytes);
1176 smartcard_dump_array(log, g_LogLevel,
"pbExtraBytes: ",
"", pbExtraBytes, cbExtraBytes);
1181 WLog_Print(log, g_LogLevel,
"pioRecvPci: null");
1184 WLog_Print(log, g_LogLevel,
"fpbRecvBufferIsNULL: %" PRId32
" cbRecvLength: %" PRIu32
"",
1185 call->fpbRecvBufferIsNULL, call->cbRecvLength);
1186 WLog_Print(log, g_LogLevel,
"}");
1189static void smartcard_trace_locate_cards_by_atr_w_call(wLog* log,
1194 if (!WLog_IsLevelActive(log, g_LogLevel))
1197 WLog_Print(log, g_LogLevel,
"LocateCardsByATRW_Call {");
1198 smartcard_log_context(log, &call->handles.hContext);
1200 dump_reader_states_w(log, call->rgReaderStates, call->cReaders);
1202 WLog_Print(log, g_LogLevel,
"}");
1205static void smartcard_trace_transmit_return(wLog* log,
const Transmit_Return* ret)
1208 UINT32 cbExtraBytes = 0;
1209 BYTE* pbExtraBytes =
nullptr;
1211 if (!WLog_IsLevelActive(log, g_LogLevel))
1214 WLog_Print(log, g_LogLevel,
"Transmit_Return {");
1215 WLog_Print(log, g_LogLevel,
" ReturnCode: %s (0x%08" PRIX32
")",
1216 SCardGetErrorString(ret->ReturnCode),
1217 WINPR_CXX_COMPAT_CAST(UINT32, ret->ReturnCode));
1219 if (ret->pioRecvPci)
1221 cbExtraBytes = (UINT32)(ret->pioRecvPci->cbPciLength -
sizeof(
SCARD_IO_REQUEST));
1223 WLog_Print(log, g_LogLevel,
1224 " pioRecvPci: dwProtocol: %" PRIu32
" cbExtraBytes: %" PRIu32
"",
1225 ret->pioRecvPci->dwProtocol, cbExtraBytes);
1229 smartcard_dump_array(log, g_LogLevel,
" pbExtraBytes: ",
"", pbExtraBytes,
1235 WLog_Print(log, g_LogLevel,
" pioRecvPci: null");
1238 WLog_Print(log, g_LogLevel,
" cbRecvLength: %" PRIu32
"", ret->cbRecvLength);
1240 if (ret->pbRecvBuffer)
1242 smartcard_dump_array(log, g_LogLevel,
" pbRecvBuffer: ",
"", ret->pbRecvBuffer,
1247 WLog_Print(log, g_LogLevel,
" pbRecvBuffer: null");
1250 WLog_Print(log, g_LogLevel,
"}");
1253static void smartcard_trace_control_return(wLog* log,
const Control_Return* ret)
1257 if (!WLog_IsLevelActive(log, g_LogLevel))
1260 WLog_Print(log, g_LogLevel,
"Control_Return {");
1261 WLog_Print(log, g_LogLevel,
" ReturnCode: %s (0x%08" PRIX32
")",
1262 SCardGetErrorString(ret->ReturnCode),
1263 WINPR_CXX_COMPAT_CAST(UINT32, ret->ReturnCode));
1264 WLog_Print(log, g_LogLevel,
" cbOutBufferSize: %" PRIu32
"", ret->cbOutBufferSize);
1266 if (ret->pvOutBuffer)
1268 smartcard_dump_array(log, g_LogLevel,
"pvOutBuffer: ",
"", ret->pvOutBuffer,
1269 ret->cbOutBufferSize);
1273 WLog_Print(log, g_LogLevel,
"pvOutBuffer: null");
1276 WLog_Print(log, g_LogLevel,
"}");
1279static void smartcard_trace_control_call(wLog* log,
const Control_Call* call)
1283 if (!WLog_IsLevelActive(log, g_LogLevel))
1286 WLog_Print(log, g_LogLevel,
"Control_Call {");
1287 smartcard_log_context(log, &call->handles.hContext);
1288 smartcard_log_redir_handle(log, &call->handles.hCard);
1290 WLog_Print(log, g_LogLevel,
1291 "dwControlCode: 0x%08" PRIX32
" cbInBufferSize: %" PRIu32
1292 " fpvOutBufferIsNULL: %" PRId32
" cbOutBufferSize: %" PRIu32
"",
1293 call->dwControlCode, call->cbInBufferSize, call->fpvOutBufferIsNULL,
1294 call->cbOutBufferSize);
1296 if (call->pvInBuffer)
1298 smartcard_dump_array(log, WLOG_DEBUG,
"pbInBuffer: ",
"", call->pvInBuffer,
1299 call->cbInBufferSize);
1303 WLog_Print(log, g_LogLevel,
"pvInBuffer: null");
1306 WLog_Print(log, g_LogLevel,
"}");
1309static void smartcard_trace_set_attrib_call(wLog* log,
const SetAttrib_Call* call)
1313 if (!WLog_IsLevelActive(log, g_LogLevel))
1316 WLog_Print(log, g_LogLevel,
"GetAttrib_Call {");
1317 smartcard_log_context(log, &call->handles.hContext);
1318 smartcard_log_redir_handle(log, &call->handles.hCard);
1319 WLog_Print(log, g_LogLevel,
"dwAttrId: 0x%08" PRIX32, call->dwAttrId);
1320 WLog_Print(log, g_LogLevel,
"cbAttrLen: 0x%08" PRIx32, call->cbAttrLen);
1321 smartcard_dump_array(log, g_LogLevel,
"pbAttr: ",
"", call->pbAttr, call->cbAttrLen);
1322 WLog_Print(log, g_LogLevel,
"}");
1325static void smartcard_trace_get_attrib_return(wLog* log,
const GetAttrib_Return* ret,
1330 if (!WLog_IsLevelActive(log, g_LogLevel))
1333 WLog_Print(log, g_LogLevel,
"GetAttrib_Return {");
1334 WLog_Print(log, g_LogLevel,
" ReturnCode: %s (0x%08" PRIX32
")",
1335 SCardGetErrorString(ret->ReturnCode),
1336 WINPR_CXX_COMPAT_CAST(UINT32, ret->ReturnCode));
1337 WLog_Print(log, g_LogLevel,
" dwAttrId: %s (0x%08" PRIX32
") cbAttrLen: 0x%08" PRIX32
"",
1338 SCardGetAttributeString(dwAttrId), dwAttrId, ret->cbAttrLen);
1339 smartcard_dump_array(log, g_LogLevel,
" ",
"", ret->pbAttr, ret->cbAttrLen);
1341 WLog_Print(log, g_LogLevel,
"}");
1344static void smartcard_trace_get_attrib_call(wLog* log,
const GetAttrib_Call* call)
1348 if (!WLog_IsLevelActive(log, g_LogLevel))
1351 WLog_Print(log, g_LogLevel,
"GetAttrib_Call {");
1352 smartcard_log_context(log, &call->handles.hContext);
1353 smartcard_log_redir_handle(log, &call->handles.hCard);
1355 WLog_Print(log, g_LogLevel,
1356 "dwAttrId: %s (0x%08" PRIX32
") fpbAttrIsNULL: %" PRId32
" cbAttrLen: 0x%08" PRIX32
1358 SCardGetAttributeString(call->dwAttrId), call->dwAttrId, call->fpbAttrIsNULL,
1360 WLog_Print(log, g_LogLevel,
"}");
1363static void smartcard_trace_status_call(wLog* log,
const Status_Call* call, BOOL unicode)
1367 if (!WLog_IsLevelActive(log, g_LogLevel))
1370 WLog_Print(log, g_LogLevel,
"Status%s_Call {", unicode ?
"W" :
"A");
1371 smartcard_log_context(log, &call->handles.hContext);
1372 smartcard_log_redir_handle(log, &call->handles.hCard);
1374 WLog_Print(log, g_LogLevel,
1375 "fmszReaderNamesIsNULL: %" PRId32
" cchReaderLen: %" PRIu32
" cbAtrLen: %" PRIu32
"",
1376 call->fmszReaderNamesIsNULL, call->cchReaderLen, call->cbAtrLen);
1377 WLog_Print(log, g_LogLevel,
"}");
1380static void smartcard_trace_status_return(wLog* log,
const Status_Return* ret, BOOL unicode)
1383 char* mszReaderNamesA =
nullptr;
1386 if (!WLog_IsLevelActive(log, g_LogLevel))
1388 cBytes = ret->cBytes;
1389 if (ret->ReturnCode != SCARD_S_SUCCESS)
1391 if (cBytes == SCARD_AUTOALLOCATE)
1393 mszReaderNamesA = smartcard_convert_string_list(ret->mszReaderNames, cBytes, unicode);
1395 WLog_Print(log, g_LogLevel,
"Status%s_Return {", unicode ?
"W" :
"A");
1396 WLog_Print(log, g_LogLevel,
" ReturnCode: %s (0x%08" PRIX32
")",
1397 SCardGetErrorString(ret->ReturnCode),
1398 WINPR_CXX_COMPAT_CAST(UINT32, ret->ReturnCode));
1399 WLog_Print(log, g_LogLevel,
" dwState: %s (0x%08" PRIX32
") dwProtocol: %s (0x%08" PRIX32
")",
1400 SCardGetCardStateString(ret->dwState), ret->dwState,
1401 SCardGetProtocolString(ret->dwProtocol), ret->dwProtocol);
1403 WLog_Print(log, g_LogLevel,
" cBytes: %" PRIu32
" mszReaderNames: %s", ret->cBytes,
1406 smartcard_dump_array(log, g_LogLevel,
" cbAtrLen: %" PRIu32
" pbAtr: ",
"", ret->pbAtr,
1407 ret->cbAtrLen, ret->cbAtrLen);
1408 WLog_Print(log, g_LogLevel,
"}");
1409 free(mszReaderNamesA);
1412static void smartcard_trace_state_return(wLog* log,
const State_Return* ret)
1415 char* state =
nullptr;
1417 if (!WLog_IsLevelActive(log, g_LogLevel))
1420 state = SCardGetReaderStateString(ret->dwState);
1421 WLog_Print(log, g_LogLevel,
"Reconnect_Return {");
1422 WLog_Print(log, g_LogLevel,
" ReturnCode: %s (0x%08" PRIX32
")",
1423 SCardGetErrorString(ret->ReturnCode),
1424 WINPR_CXX_COMPAT_CAST(UINT32, ret->ReturnCode));
1425 WLog_Print(log, g_LogLevel,
" dwState: %s (0x%08" PRIX32
")", state, ret->dwState);
1426 WLog_Print(log, g_LogLevel,
" dwProtocol: %s (0x%08" PRIX32
")",
1427 SCardGetProtocolString(ret->dwProtocol), ret->dwProtocol);
1428 WLog_Print(log, g_LogLevel,
" cbAtrLen: (0x%08" PRIX32
")", ret->cbAtrLen);
1429 smartcard_dump_array(log, g_LogLevel,
" rgAtr: ",
"", ret->rgAtr,
sizeof(ret->rgAtr));
1430 WLog_Print(log, g_LogLevel,
"}");
1434static void smartcard_trace_reconnect_return(wLog* log,
const Reconnect_Return* ret)
1438 if (!WLog_IsLevelActive(log, g_LogLevel))
1441 WLog_Print(log, g_LogLevel,
"Reconnect_Return {");
1442 WLog_Print(log, g_LogLevel,
" ReturnCode: %s (0x%08" PRIX32
")",
1443 SCardGetErrorString(ret->ReturnCode),
1444 WINPR_CXX_COMPAT_CAST(UINT32, ret->ReturnCode));
1445 WLog_Print(log, g_LogLevel,
" dwActiveProtocol: %s (0x%08" PRIX32
")",
1446 SCardGetProtocolString(ret->dwActiveProtocol), ret->dwActiveProtocol);
1447 WLog_Print(log, g_LogLevel,
"}");
1450static void smartcard_trace_connect_a_call(wLog* log,
const ConnectA_Call* call)
1454 if (!WLog_IsLevelActive(log, g_LogLevel))
1457 WLog_Print(log, g_LogLevel,
"ConnectA_Call {");
1458 smartcard_log_context(log, &call->Common.handles.hContext);
1460 WLog_Print(log, g_LogLevel,
1461 "szReader: %s dwShareMode: %s (0x%08" PRIX32
1462 ") dwPreferredProtocols: %s (0x%08" PRIX32
")",
1463 call->szReader, SCardGetShareModeString(call->Common.dwShareMode),
1464 call->Common.dwShareMode, SCardGetProtocolString(call->Common.dwPreferredProtocols),
1465 call->Common.dwPreferredProtocols);
1466 WLog_Print(log, g_LogLevel,
"}");
1469static void smartcard_trace_connect_w_call(wLog* log,
const ConnectW_Call* call)
1472 char szReaderA[1024] = WINPR_C_ARRAY_INIT;
1474 if (!WLog_IsLevelActive(log, g_LogLevel))
1478 (void)ConvertWCharToUtf8(call->szReader, szReaderA, ARRAYSIZE(szReaderA));
1479 WLog_Print(log, g_LogLevel,
"ConnectW_Call {");
1480 smartcard_log_context(log, &call->Common.handles.hContext);
1482 WLog_Print(log, g_LogLevel,
1483 "szReader: %s dwShareMode: %s (0x%08" PRIX32
1484 ") dwPreferredProtocols: %s (0x%08" PRIX32
")",
1485 szReaderA, SCardGetShareModeString(call->Common.dwShareMode),
1486 call->Common.dwShareMode, SCardGetProtocolString(call->Common.dwPreferredProtocols),
1487 call->Common.dwPreferredProtocols);
1488 WLog_Print(log, g_LogLevel,
"}");
1491static void smartcard_trace_hcard_and_disposition_call(wLog* log,
1497 if (!WLog_IsLevelActive(log, g_LogLevel))
1500 WLog_Print(log, g_LogLevel,
"%s_Call {", name);
1501 smartcard_log_context(log, &call->handles.hContext);
1502 smartcard_log_redir_handle(log, &call->handles.hCard);
1504 WLog_Print(log, g_LogLevel,
"dwDisposition: %s (0x%08" PRIX32
")",
1505 SCardGetDispositionString(call->dwDisposition), call->dwDisposition);
1506 WLog_Print(log, g_LogLevel,
"}");
1513 if (!WLog_IsLevelActive(log, g_LogLevel))
1516 WLog_Print(log, g_LogLevel,
"EstablishContext_Call {");
1517 WLog_Print(log, g_LogLevel,
"dwScope: %s (0x%08" PRIX32
")", SCardGetScopeString(call->dwScope),
1519 WLog_Print(log, g_LogLevel,
"}");
1526 if (!WLog_IsLevelActive(log, g_LogLevel))
1529 WLog_Print(log, g_LogLevel,
"EstablishContext_Return {");
1530 WLog_Print(log, g_LogLevel,
" ReturnCode: %s (0x%08" PRIX32
")",
1531 SCardGetErrorString(ret->ReturnCode),
1532 WINPR_CXX_COMPAT_CAST(UINT32, ret->ReturnCode));
1533 smartcard_log_context(log, &ret->hContext);
1535 WLog_Print(log, g_LogLevel,
"}");
1538void smartcard_trace_long_return(
const Long_Return* ret,
const char* name)
1540 wLog* log = scard_log();
1541 smartcard_trace_long_return_int(log, ret, name);
1544void smartcard_trace_long_return_int(wLog* log,
const Long_Return* ret,
const char* name)
1548 if (!WLog_IsLevelActive(log, g_LogLevel))
1551 WLog_Print(log, g_LogLevel,
"%s_Return {", name);
1552 WLog_Print(log, g_LogLevel,
" ReturnCode: %s (0x%08" PRIX32
")",
1553 SCardGetErrorString(ret->ReturnCode),
1554 WINPR_CXX_COMPAT_CAST(UINT32, ret->ReturnCode));
1555 WLog_Print(log, g_LogLevel,
"}");
1558static void smartcard_trace_connect_return(wLog* log,
const Connect_Return* ret)
1562 if (!WLog_IsLevelActive(log, g_LogLevel))
1565 WLog_Print(log, g_LogLevel,
"Connect_Return {");
1566 WLog_Print(log, g_LogLevel,
" ReturnCode: %s (0x%08" PRIX32
")",
1567 SCardGetErrorString(ret->ReturnCode),
1568 WINPR_CXX_COMPAT_CAST(UINT32, ret->ReturnCode));
1569 smartcard_log_context(log, &ret->hContext);
1570 smartcard_log_redir_handle(log, &ret->hCard);
1572 WLog_Print(log, g_LogLevel,
" dwActiveProtocol: %s (0x%08" PRIX32
")",
1573 SCardGetProtocolString(ret->dwActiveProtocol), ret->dwActiveProtocol);
1574 WLog_Print(log, g_LogLevel,
"}");
1577static void smartcard_trace_reconnect_call(wLog* log,
const Reconnect_Call* call)
1581 if (!WLog_IsLevelActive(log, g_LogLevel))
1584 WLog_Print(log, g_LogLevel,
"Reconnect_Call {");
1585 smartcard_log_context(log, &call->handles.hContext);
1586 smartcard_log_redir_handle(log, &call->handles.hCard);
1588 WLog_Print(log, g_LogLevel,
1589 "dwShareMode: %s (0x%08" PRIX32
") dwPreferredProtocols: %s (0x%08" PRIX32
1590 ") dwInitialization: %s (0x%08" PRIX32
")",
1591 SCardGetShareModeString(call->dwShareMode), call->dwShareMode,
1592 SCardGetProtocolString(call->dwPreferredProtocols), call->dwPreferredProtocols,
1593 SCardGetDispositionString(call->dwInitialization), call->dwInitialization);
1594 WLog_Print(log, g_LogLevel,
"}");
1601 if (!WLog_IsLevelActive(log, g_LogLevel))
1604 WLog_Print(log, g_LogLevel,
"GetDeviceTypeId_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 WLog_Print(log, g_LogLevel,
" dwDeviceId=%08" PRIx32, ret->dwDeviceId);
1610 WLog_Print(log, g_LogLevel,
"}");
1613static LONG smartcard_unpack_common_context_and_string_a(wLog* log,
wStream* s,
1615 CHAR** pszReaderName)
1618 UINT32 pbContextNdrPtr = 0;
1619 LONG status = smartcard_unpack_redir_scard_context(log, s, phContext, &index, &pbContextNdrPtr);
1620 if (status != SCARD_S_SUCCESS)
1623 if (!smartcard_ndr_pointer_read(log, s, &index,
nullptr))
1624 return ERROR_INVALID_DATA;
1626 status = smartcard_unpack_redir_scard_context_ref(log, s, pbContextNdrPtr, phContext);
1627 if (status != SCARD_S_SUCCESS)
1630 status = smartcard_ndr_read_a(log, s, pszReaderName, NDR_PTR_FULL);
1631 if (status != SCARD_S_SUCCESS)
1634 smartcard_trace_context_and_string_call_a(log, __func__, phContext, *pszReaderName);
1635 return SCARD_S_SUCCESS;
1638static LONG smartcard_unpack_common_context_and_string_w(wLog* log,
wStream* s,
1640 WCHAR** pszReaderName)
1643 UINT32 pbContextNdrPtr = 0;
1645 LONG status = smartcard_unpack_redir_scard_context(log, s, phContext, &index, &pbContextNdrPtr);
1646 if (status != SCARD_S_SUCCESS)
1649 if (!smartcard_ndr_pointer_read(log, s, &index,
nullptr))
1650 return ERROR_INVALID_DATA;
1652 status = smartcard_unpack_redir_scard_context_ref(log, s, pbContextNdrPtr, phContext);
1653 if (status != SCARD_S_SUCCESS)
1656 status = smartcard_ndr_read_w(log, s, pszReaderName, NDR_PTR_FULL);
1657 if (status != SCARD_S_SUCCESS)
1660 smartcard_trace_context_and_string_call_w(log, __func__, phContext, *pszReaderName);
1661 return SCARD_S_SUCCESS;
1664LONG smartcard_unpack_common_type_header(
wStream* s)
1666 wLog* log = scard_log();
1669 UINT8 endianness = 0;
1670 UINT16 commonHeaderLength = 0;
1672 if (!Stream_CheckAndLogRequiredLengthWLog(log, s, 8))
1673 return STATUS_BUFFER_TOO_SMALL;
1676 Stream_Read_UINT8(s, version);
1677 Stream_Read_UINT8(s, endianness);
1678 Stream_Read_UINT16(s, commonHeaderLength);
1679 Stream_Read_UINT32(s, filler);
1683 WLog_Print(log, WLOG_WARN,
"Unsupported CommonTypeHeader Version %" PRIu8
"", version);
1684 return STATUS_INVALID_PARAMETER;
1687 if (endianness != 0x10)
1689 WLog_Print(log, WLOG_WARN,
"Unsupported CommonTypeHeader Endianness %" PRIu8
"",
1691 return STATUS_INVALID_PARAMETER;
1694 if (commonHeaderLength != 8)
1696 WLog_Print(log, WLOG_WARN,
"Unsupported CommonTypeHeader CommonHeaderLength %" PRIu16
"",
1697 commonHeaderLength);
1698 return STATUS_INVALID_PARAMETER;
1701 if (filler != 0xCCCCCCCC)
1703 WLog_Print(log, WLOG_WARN,
"Unexpected CommonTypeHeader Filler 0x%08" PRIX32
"", filler);
1704 return STATUS_INVALID_PARAMETER;
1707 return SCARD_S_SUCCESS;
1710void smartcard_pack_common_type_header(
wStream* s)
1712 Stream_Write_UINT8(s, 1);
1713 Stream_Write_UINT8(s, 0x10);
1714 Stream_Write_UINT16(s, 8);
1715 Stream_Write_UINT32(s, 0xCCCCCCCC);
1718LONG smartcard_unpack_private_type_header(
wStream* s)
1720 wLog* log = scard_log();
1722 UINT32 objectBufferLength = 0;
1724 if (!Stream_CheckAndLogRequiredLengthWLog(log, s, 8))
1725 return STATUS_BUFFER_TOO_SMALL;
1727 Stream_Read_UINT32(s, objectBufferLength);
1728 Stream_Read_UINT32(s, filler);
1730 if (filler != 0x00000000)
1732 WLog_Print(log, WLOG_WARN,
"Unexpected PrivateTypeHeader Filler 0x%08" PRIX32
"", filler);
1733 return STATUS_INVALID_PARAMETER;
1736 if (!Stream_CheckAndLogRequiredLengthWLog(log, s, objectBufferLength))
1737 return STATUS_INVALID_PARAMETER;
1739 return SCARD_S_SUCCESS;
1742void smartcard_pack_private_type_header(
wStream* s, UINT32 objectBufferLength)
1744 Stream_Write_UINT32(s, objectBufferLength);
1745 Stream_Write_UINT32(s, 0x00000000);
1748LONG smartcard_unpack_read_size_align(
wStream* s,
size_t size, UINT32 alignment)
1750 const size_t padsize = (size + alignment - 1) & ~(alignment - 1);
1751 const size_t pad = padsize - size;
1755 if (!Stream_SafeSeek(s, pad))
1762LONG smartcard_pack_write_size_align(
wStream* s,
size_t size, UINT32 alignment)
1764 const size_t padsize = (size + alignment - 1) & ~(alignment - 1);
1765 const size_t pad = padsize - size;
1769 if (!Stream_EnsureRemainingCapacity(s, pad))
1771 wLog* log = scard_log();
1772 WLog_Print(log, WLOG_ERROR,
"Stream_EnsureRemainingCapacity failed!");
1773 return SCARD_F_INTERNAL_ERROR;
1776 Stream_Zero(s, pad);
1779 return SCARD_S_SUCCESS;
1782SCARDCONTEXT smartcard_scard_context_native_from_redir(
const REDIR_SCARDCONTEXT* context)
1784 SCARDCONTEXT hContext = WINPR_C_ARRAY_INIT;
1786 WINPR_ASSERT(context);
1787 if ((context->cbContext !=
sizeof(ULONG_PTR)) && (context->cbContext != 0))
1789 wLog* log = scard_log();
1790 WLog_Print(log, WLOG_WARN,
1791 "REDIR_SCARDCONTEXT does not match native size: Actual: %" PRIu32
1792 ", Expected: %" PRIuz
"",
1793 context->cbContext,
sizeof(ULONG_PTR));
1797 if (context->cbContext)
1798 CopyMemory(&hContext, &(context->pbContext), context->cbContext);
1803void smartcard_scard_context_native_to_redir(
REDIR_SCARDCONTEXT* context, SCARDCONTEXT hContext)
1805 WINPR_ASSERT(context);
1807 context->cbContext =
sizeof(ULONG_PTR);
1808 CopyMemory(&(context->pbContext), &hContext, context->cbContext);
1811SCARDHANDLE smartcard_scard_handle_native_from_redir(
const REDIR_SCARDHANDLE* handle)
1813 SCARDHANDLE hCard = 0;
1815 WINPR_ASSERT(handle);
1816 if (handle->cbHandle == 0)
1819 if (handle->cbHandle !=
sizeof(ULONG_PTR))
1821 wLog* log = scard_log();
1822 WLog_Print(log, WLOG_WARN,
1823 "REDIR_SCARDHANDLE does not match native size: Actual: %" PRIu32
1824 ", Expected: %" PRIuz
"",
1825 handle->cbHandle,
sizeof(ULONG_PTR));
1829 if (handle->cbHandle)
1830 CopyMemory(&hCard, &(handle->pbHandle), handle->cbHandle);
1835void smartcard_scard_handle_native_to_redir(
REDIR_SCARDHANDLE* handle, SCARDHANDLE hCard)
1837 WINPR_ASSERT(handle);
1839 handle->cbHandle =
sizeof(ULONG_PTR);
1840 CopyMemory(&(handle->pbHandle), &hCard, handle->cbHandle);
1843#define smartcard_context_supported(log, size) \
1844 smartcard_context_supported_((log), (size), __FILE__, __func__, __LINE__)
1845static LONG smartcard_context_supported_(wLog* log, uint32_t size,
const char* file,
1846 const char* fkt,
size_t line)
1853 return SCARD_S_SUCCESS;
1856 const uint32_t level = WLOG_WARN;
1857 if (WLog_IsLevelActive(log, level))
1859 WLog_PrintTextMessage(log, level, line, file, fkt,
1860 "REDIR_SCARDCONTEXT length is not 0, 4 or 8: %" PRIu32
"",
1863 return STATUS_INVALID_PARAMETER;
1869 UINT32* index, UINT32* ppbContextNdrPtr,
1870 const char* file,
const char* function,
size_t line)
1872 UINT32 pbContextNdrPtr = 0;
1875 WINPR_ASSERT(context);
1879 if (!Stream_CheckAndLogRequiredLengthWLogEx(log, WLOG_WARN, s, 4, 1,
"%s(%s:%" PRIuz
")", file,
1881 return STATUS_BUFFER_TOO_SMALL;
1883 const LONG status = smartcard_context_supported_(log, context->cbContext, file, function, line);
1884 if (status != SCARD_S_SUCCESS)
1887 Stream_Read_UINT32(s, context->cbContext);
1889 if (!smartcard_ndr_pointer_read_(log, s, index, &pbContextNdrPtr, file, function, line))
1890 return ERROR_INVALID_DATA;
1892 if (((context->cbContext == 0) && pbContextNdrPtr) ||
1893 ((context->cbContext != 0) && !pbContextNdrPtr))
1895 WLog_Print(log, WLOG_WARN,
1896 "REDIR_SCARDCONTEXT cbContext (%" PRIu32
") pbContextNdrPtr (%" PRIu32
1898 context->cbContext, pbContextNdrPtr);
1899 return STATUS_INVALID_PARAMETER;
1902 if (!Stream_CheckAndLogRequiredLengthWLogEx(log, WLOG_WARN, s, context->cbContext, 1,
1903 "%s(%s:%" PRIuz
")", file, function, line))
1904 return STATUS_INVALID_PARAMETER;
1906 *ppbContextNdrPtr = pbContextNdrPtr;
1907 return SCARD_S_SUCCESS;
1910LONG smartcard_pack_redir_scard_context(WINPR_ATTR_UNUSED wLog* log,
wStream* s,
1913 const UINT32 pbContextNdrPtr = 0x00020000 + *index * 4;
1915 WINPR_ASSERT(context);
1916 if (context->cbContext != 0)
1918 Stream_Write_UINT32(s, context->cbContext);
1919 Stream_Write_UINT32(s, pbContextNdrPtr);
1920 *index = *index + 1;
1925 return SCARD_S_SUCCESS;
1928LONG smartcard_unpack_redir_scard_context_ref(wLog* log,
wStream* s,
1929 WINPR_ATTR_UNUSED UINT32 pbContextNdrPtr,
1934 WINPR_ASSERT(context);
1936 if (context->cbContext == 0)
1937 return SCARD_S_SUCCESS;
1939 if (!Stream_CheckAndLogRequiredLengthWLog(log, s, 4))
1940 return STATUS_BUFFER_TOO_SMALL;
1942 Stream_Read_UINT32(s, length);
1944 if (length != context->cbContext)
1946 WLog_Print(log, WLOG_WARN,
1947 "REDIR_SCARDCONTEXT length (%" PRIu32
") cbContext (%" PRIu32
") mismatch",
1948 length, context->cbContext);
1949 return STATUS_INVALID_PARAMETER;
1952 const LONG status = smartcard_context_supported(log, context->cbContext);
1953 if (status != SCARD_S_SUCCESS)
1956 if (!Stream_CheckAndLogRequiredLengthWLog(log, s, context->cbContext))
1957 return STATUS_BUFFER_TOO_SMALL;
1959 if (context->cbContext)
1960 Stream_Read(s, &(context->pbContext), context->cbContext);
1962 ZeroMemory(&(context->pbContext),
sizeof(context->pbContext));
1964 return SCARD_S_SUCCESS;
1967LONG smartcard_pack_redir_scard_context_ref(WINPR_ATTR_UNUSED wLog* log,
wStream* s,
1970 WINPR_ASSERT(context);
1972 if (context->cbContext == 0)
1973 return SCARD_S_SUCCESS;
1975 Stream_Write_UINT32(s, context->cbContext);
1976 Stream_Write(s, &(context->pbContext), context->cbContext);
1978 return SCARD_S_SUCCESS;
1982 UINT32* index,
const char* file,
const char* function,
1985 WINPR_ASSERT(handle);
1988 if (!Stream_CheckAndLogRequiredLengthWLog(log, s, 4))
1989 return STATUS_BUFFER_TOO_SMALL;
1991 Stream_Read_UINT32(s, handle->cbHandle);
1993 if (!Stream_CheckAndLogRequiredLengthWLog(log, s, handle->cbHandle))
1994 return STATUS_BUFFER_TOO_SMALL;
1996 if (!smartcard_ndr_pointer_read_(log, s, index,
nullptr, file, function, line))
1997 return ERROR_INVALID_DATA;
1999 return SCARD_S_SUCCESS;
2002LONG smartcard_pack_redir_scard_handle(WINPR_ATTR_UNUSED wLog* log,
wStream* s,
2005 const UINT32 pbContextNdrPtr = 0x00020000 + *index * 4;
2007 WINPR_ASSERT(handle);
2008 if (handle->cbHandle != 0)
2010 Stream_Write_UINT32(s, handle->cbHandle);
2011 Stream_Write_UINT32(s, pbContextNdrPtr);
2012 *index = *index + 1;
2016 return SCARD_S_SUCCESS;
2023 WINPR_ASSERT(handle);
2025 if (handle->cbHandle == 0)
2026 return SCARD_S_SUCCESS;
2028 if (!Stream_CheckAndLogRequiredLengthWLog(log, s, 4))
2029 return STATUS_BUFFER_TOO_SMALL;
2031 Stream_Read_UINT32(s, length);
2033 if (length != handle->cbHandle)
2035 WLog_Print(log, WLOG_WARN,
2036 "REDIR_SCARDHANDLE length (%" PRIu32
") cbHandle (%" PRIu32
") mismatch", length,
2038 return STATUS_INVALID_PARAMETER;
2041 if ((handle->cbHandle != 4) && (handle->cbHandle != 8))
2043 WLog_Print(log, WLOG_WARN,
"REDIR_SCARDHANDLE length is not 4 or 8: %" PRIu32
"",
2045 return STATUS_INVALID_PARAMETER;
2048 if (!Stream_CheckAndLogRequiredLengthWLog(log, s, handle->cbHandle))
2049 return STATUS_BUFFER_TOO_SMALL;
2051 if (handle->cbHandle)
2052 Stream_Read(s, &(handle->pbHandle), handle->cbHandle);
2054 return SCARD_S_SUCCESS;
2057LONG smartcard_pack_redir_scard_handle_ref(WINPR_ATTR_UNUSED wLog* log,
wStream* s,
2060 WINPR_ASSERT(handle);
2062 if (handle->cbHandle == 0)
2063 return SCARD_S_SUCCESS;
2065 Stream_Write_UINT32(s, handle->cbHandle);
2066 Stream_Write(s, &(handle->pbHandle), handle->cbHandle);
2068 return SCARD_S_SUCCESS;
2074 wLog* log = scard_log();
2076 if (!Stream_CheckAndLogRequiredLengthWLog(log, s, 4))
2077 return STATUS_BUFFER_TOO_SMALL;
2079 Stream_Read_UINT32(s, call->dwScope);
2080 smartcard_trace_establish_context_call(log, call);
2081 return SCARD_S_SUCCESS;
2087 wLog* log = scard_log();
2089 smartcard_trace_establish_context_call(log, call);
2091 if (!Stream_EnsureRemainingCapacity(s, 4))
2092 return SCARD_E_NO_MEMORY;
2094 Stream_Write_UINT32(s, call->dwScope);
2096 return SCARD_S_SUCCESS;
2102 wLog* log = scard_log();
2106 smartcard_trace_establish_context_return(log, ret);
2107 if (ret->ReturnCode != SCARD_S_SUCCESS)
2108 return ret->ReturnCode;
2110 status = smartcard_pack_redir_scard_context(log, s, &(ret->hContext), &index);
2111 if (status != SCARD_S_SUCCESS)
2114 return smartcard_pack_redir_scard_context_ref(log, s, &(ret->hContext));
2120 wLog* log = scard_log();
2122 UINT32 pbContextNdrPtr = 0;
2125 smartcard_unpack_redir_scard_context(log, s, &(ret->hContext), &index, &pbContextNdrPtr);
2126 if (status != SCARD_S_SUCCESS)
2129 status = smartcard_unpack_redir_scard_context_ref(log, s, pbContextNdrPtr, &(ret->hContext));
2130 if (status != SCARD_S_SUCCESS)
2133 smartcard_trace_establish_context_return(log, ret);
2134 return SCARD_S_SUCCESS;
2140 UINT32 pbContextNdrPtr = 0;
2141 wLog* log = scard_log();
2144 LONG status = smartcard_unpack_redir_scard_context(log, s, &(call->handles.hContext), &index,
2146 if (status != SCARD_S_SUCCESS)
2149 status = smartcard_unpack_redir_scard_context_ref(log, s, pbContextNdrPtr,
2150 &(call->handles.hContext));
2151 if (status != SCARD_S_SUCCESS)
2152 WLog_Print(log, WLOG_ERROR,
2153 "smartcard_unpack_redir_scard_context_ref failed with error %" PRId32
"",
2156 smartcard_trace_context_call(log, call, name);
2163 UINT32 pbContextNdrPtr = 0;
2164 wLog* log = scard_log();
2167 LONG status = smartcard_unpack_redir_scard_context(log, s, &(call->handles.hContext), &index,
2170 if (status != SCARD_S_SUCCESS)
2173 if (!Stream_CheckAndLogRequiredLengthWLog(log, s, 8))
2174 return STATUS_BUFFER_TOO_SMALL;
2176 Stream_Read_INT32(s, call->fmszGroupsIsNULL);
2177 Stream_Read_UINT32(s, call->cchGroups);
2178 status = smartcard_unpack_redir_scard_context_ref(log, s, pbContextNdrPtr,
2179 &(call->handles.hContext));
2181 if (status != SCARD_S_SUCCESS)
2184 smartcard_trace_list_reader_groups_call(log, call, unicode);
2185 return SCARD_S_SUCCESS;
2192 wLog* log = scard_log();
2195 smartcard_trace_list_reader_groups_call(log, call, unicode);
2197 LONG status = smartcard_pack_redir_scard_context(log, s, &call->handles.hContext, &index);
2198 if (status != SCARD_S_SUCCESS)
2201 if (!Stream_EnsureRemainingCapacity(s, 8))
2202 return SCARD_E_NO_MEMORY;
2204 Stream_Write_INT32(s, call->fmszGroupsIsNULL);
2205 Stream_Write_UINT32(s, call->cchGroups);
2207 return smartcard_pack_redir_scard_context_ref(log, s, &call->handles.hContext);
2214 wLog* log = scard_log();
2216 DWORD cBytes = ret->cBytes;
2219 smartcard_trace_list_reader_groups_return(log, ret, unicode);
2220 if (ret->ReturnCode != SCARD_S_SUCCESS)
2222 if (cBytes == SCARD_AUTOALLOCATE)
2225 if (!Stream_EnsureRemainingCapacity(s, 4))
2226 return SCARD_E_NO_MEMORY;
2228 Stream_Write_UINT32(s, cBytes);
2229 if (!smartcard_ndr_pointer_write(s, &index, cBytes))
2230 return SCARD_E_NO_MEMORY;
2232 status = smartcard_ndr_write(s, ret->msz, cBytes, 1, NDR_PTR_SIMPLE);
2233 if (status != SCARD_S_SUCCESS)
2235 return ret->ReturnCode;
2242 wLog* log = scard_log();
2244 UINT32 mszNdrPtr = 0;
2246 if (!Stream_CheckAndLogRequiredLengthWLog(log, s, 4))
2247 return STATUS_BUFFER_TOO_SMALL;
2249 const UINT32 cBytes = Stream_Get_UINT32(s);
2251 if (!smartcard_ndr_pointer_read(log, s, &index, &mszNdrPtr))
2252 return ERROR_INVALID_DATA;
2256 LONG status = smartcard_ndr_read(log, s, &ret->msz, cBytes, 1, NDR_PTR_SIMPLE);
2257 if (status != SCARD_S_SUCCESS)
2259 ret->cBytes = cBytes;
2262 smartcard_trace_list_reader_groups_return(log, ret, unicode);
2263 return SCARD_S_SUCCESS;
2269 UINT32 mszGroupsNdrPtr = 0;
2270 UINT32 pbContextNdrPtr = 0;
2271 wLog* log = scard_log();
2274 call->mszGroups =
nullptr;
2276 LONG status = smartcard_unpack_redir_scard_context(log, s, &(call->handles.hContext), &index,
2278 if (status != SCARD_S_SUCCESS)
2281 if (!Stream_CheckAndLogRequiredLengthWLog(log, s, 4))
2282 return STATUS_BUFFER_TOO_SMALL;
2284 const UINT32 cBytes = Stream_Get_UINT32(s);
2285 if (!smartcard_ndr_pointer_read(log, s, &index, &mszGroupsNdrPtr))
2286 return ERROR_INVALID_DATA;
2288 if (!Stream_CheckAndLogRequiredLengthWLog(log, s, 8))
2289 return STATUS_BUFFER_TOO_SMALL;
2290 Stream_Read_INT32(s, call->fmszReadersIsNULL);
2291 Stream_Read_UINT32(s, call->cchReaders);
2293 status = smartcard_unpack_redir_scard_context_ref(log, s, pbContextNdrPtr,
2294 &(call->handles.hContext));
2295 if (status != SCARD_S_SUCCESS)
2298 if (mszGroupsNdrPtr)
2300 status = smartcard_ndr_read(log, s, &call->mszGroups, cBytes, 1, NDR_PTR_SIMPLE);
2301 if (status != SCARD_S_SUCCESS)
2303 call->cBytes = cBytes;
2306 smartcard_trace_list_readers_call(log, call, unicode);
2307 return SCARD_S_SUCCESS;
2313 wLog* log = scard_log();
2316 UINT32 size = ret->cBytes;
2318 smartcard_trace_list_readers_return(log, ret, unicode);
2319 if (ret->ReturnCode != SCARD_S_SUCCESS)
2322 if (!Stream_EnsureRemainingCapacity(s, 4))
2324 WLog_Print(log, WLOG_ERROR,
"Stream_EnsureRemainingCapacity failed!");
2325 return SCARD_F_INTERNAL_ERROR;
2328 Stream_Write_UINT32(s, size);
2329 if (!smartcard_ndr_pointer_write(s, &index, size))
2330 return SCARD_E_NO_MEMORY;
2332 status = smartcard_ndr_write(s, ret->msz, size, 1, NDR_PTR_SIMPLE);
2333 if (status != SCARD_S_SUCCESS)
2335 return ret->ReturnCode;
2339 UINT32* index, UINT32* ppbContextNdrPtr)
2341 WINPR_ASSERT(common);
2342 LONG status = smartcard_unpack_redir_scard_context(log, s, &(common->handles.hContext), index,
2344 if (status != SCARD_S_SUCCESS)
2347 if (!Stream_CheckAndLogRequiredLengthWLog(log, s, 8))
2348 return STATUS_BUFFER_TOO_SMALL;
2350 Stream_Read_UINT32(s, common->dwShareMode);
2351 Stream_Read_UINT32(s, common->dwPreferredProtocols);
2352 return SCARD_S_SUCCESS;
2359 UINT32 pbContextNdrPtr = 0;
2362 wLog* log = scard_log();
2364 call->szReader =
nullptr;
2366 if (!smartcard_ndr_pointer_read(log, s, &index,
nullptr))
2367 return ERROR_INVALID_DATA;
2369 status = smartcard_unpack_connect_common(log, s, &(call->Common), &index, &pbContextNdrPtr);
2370 if (status != SCARD_S_SUCCESS)
2372 WLog_Print(log, WLOG_ERROR,
"smartcard_unpack_connect_common failed with error %" PRId32
"",
2377 status = smartcard_ndr_read_a(log, s, &call->szReader, NDR_PTR_FULL);
2378 if (status != SCARD_S_SUCCESS)
2381 status = smartcard_unpack_redir_scard_context_ref(log, s, pbContextNdrPtr,
2382 &(call->Common.handles.hContext));
2383 if (status != SCARD_S_SUCCESS)
2384 WLog_Print(log, WLOG_ERROR,
2385 "smartcard_unpack_redir_scard_context_ref failed with error %" PRId32
"",
2388 smartcard_trace_connect_a_call(log, call);
2396 UINT32 pbContextNdrPtr = 0;
2399 wLog* log = scard_log();
2400 call->szReader =
nullptr;
2402 if (!smartcard_ndr_pointer_read(log, s, &index,
nullptr))
2403 return ERROR_INVALID_DATA;
2405 status = smartcard_unpack_connect_common(log, s, &(call->Common), &index, &pbContextNdrPtr);
2406 if (status != SCARD_S_SUCCESS)
2408 WLog_Print(log, WLOG_ERROR,
"smartcard_unpack_connect_common failed with error %" PRId32
"",
2413 status = smartcard_ndr_read_w(log, s, &call->szReader, NDR_PTR_FULL);
2414 if (status != SCARD_S_SUCCESS)
2417 status = smartcard_unpack_redir_scard_context_ref(log, s, pbContextNdrPtr,
2418 &(call->Common.handles.hContext));
2419 if (status != SCARD_S_SUCCESS)
2420 WLog_Print(log, WLOG_ERROR,
2421 "smartcard_unpack_redir_scard_context_ref failed with error %" PRId32
"",
2424 smartcard_trace_connect_w_call(log, call);
2434 wLog* log = scard_log();
2435 smartcard_trace_connect_return(log, ret);
2437 status = smartcard_pack_redir_scard_context(log, s, &ret->hContext, &index);
2438 if (status != SCARD_S_SUCCESS)
2441 status = smartcard_pack_redir_scard_handle(log, s, &ret->hCard, &index);
2442 if (status != SCARD_S_SUCCESS)
2445 if (!Stream_EnsureRemainingCapacity(s, 4))
2446 return SCARD_E_NO_MEMORY;
2448 Stream_Write_UINT32(s, ret->dwActiveProtocol);
2449 status = smartcard_pack_redir_scard_context_ref(log, s, &ret->hContext);
2450 if (status != SCARD_S_SUCCESS)
2452 return smartcard_pack_redir_scard_handle_ref(log, s, &(ret->hCard));
2458 UINT32 pbContextNdrPtr = 0;
2461 wLog* log = scard_log();
2462 LONG status = smartcard_unpack_redir_scard_context(log, s, &(call->handles.hContext), &index,
2464 if (status != SCARD_S_SUCCESS)
2467 status = smartcard_unpack_redir_scard_handle(log, s, &(call->handles.hCard), &index);
2468 if (status != SCARD_S_SUCCESS)
2471 if (!Stream_CheckAndLogRequiredLengthWLog(log, s, 12))
2472 return STATUS_BUFFER_TOO_SMALL;
2474 Stream_Read_UINT32(s, call->dwShareMode);
2475 Stream_Read_UINT32(s, call->dwPreferredProtocols);
2476 Stream_Read_UINT32(s, call->dwInitialization);
2478 status = smartcard_unpack_redir_scard_context_ref(log, s, pbContextNdrPtr,
2479 &(call->handles.hContext));
2480 if (status != SCARD_S_SUCCESS)
2482 WLog_Print(log, WLOG_ERROR,
2483 "smartcard_unpack_redir_scard_context_ref failed with error %" PRId32
"",
2488 status = smartcard_unpack_redir_scard_handle_ref(log, s, &(call->handles.hCard));
2489 if (status != SCARD_S_SUCCESS)
2490 WLog_Print(log, WLOG_ERROR,
2491 "smartcard_unpack_redir_scard_handle_ref failed with error %" PRId32
"", status);
2493 smartcard_trace_reconnect_call(log, call);
2500 wLog* log = scard_log();
2503 smartcard_trace_reconnect_call(log, call);
2505 LONG status = smartcard_pack_redir_scard_context(log, s, &call->handles.hContext, &index);
2506 if (status != SCARD_S_SUCCESS)
2509 status = smartcard_pack_redir_scard_handle(log, s, &call->handles.hCard, &index);
2510 if (status != SCARD_S_SUCCESS)
2513 if (!Stream_EnsureRemainingCapacity(s, 12))
2514 return SCARD_E_NO_MEMORY;
2516 Stream_Write_UINT32(s, call->dwShareMode);
2517 Stream_Write_UINT32(s, call->dwPreferredProtocols);
2518 Stream_Write_UINT32(s, call->dwInitialization);
2520 status = smartcard_pack_redir_scard_context_ref(log, s, &call->handles.hContext);
2521 if (status != SCARD_S_SUCCESS)
2524 return smartcard_pack_redir_scard_handle_ref(log, s, &call->handles.hCard);
2530 wLog* log = scard_log();
2531 smartcard_trace_reconnect_return(log, ret);
2533 if (!Stream_EnsureRemainingCapacity(s, 4))
2534 return SCARD_E_NO_MEMORY;
2535 Stream_Write_UINT32(s, ret->dwActiveProtocol);
2536 return ret->ReturnCode;
2542 wLog* log = scard_log();
2544 if (!Stream_CheckAndLogRequiredLengthWLog(log, s, 4))
2545 return STATUS_BUFFER_TOO_SMALL;
2547 Stream_Read_UINT32(s, ret->dwActiveProtocol);
2549 smartcard_trace_reconnect_return(log, ret);
2550 return SCARD_S_SUCCESS;
2557 UINT32 pbContextNdrPtr = 0;
2560 wLog* log = scard_log();
2562 LONG status = smartcard_unpack_redir_scard_context(log, s, &(call->handles.hContext), &index,
2564 if (status != SCARD_S_SUCCESS)
2567 status = smartcard_unpack_redir_scard_handle(log, s, &(call->handles.hCard), &index);
2568 if (status != SCARD_S_SUCCESS)
2571 if (!Stream_CheckAndLogRequiredLengthWLog(log, s, 4))
2572 return STATUS_BUFFER_TOO_SMALL;
2574 Stream_Read_UINT32(s, call->dwDisposition);
2576 status = smartcard_unpack_redir_scard_context_ref(log, s, pbContextNdrPtr,
2577 &(call->handles.hContext));
2578 if (status != SCARD_S_SUCCESS)
2581 status = smartcard_unpack_redir_scard_handle_ref(log, s, &(call->handles.hCard));
2582 if (status != SCARD_S_SUCCESS)
2585 smartcard_trace_hcard_and_disposition_call(log, call, name);
2593 if (!WLog_IsLevelActive(log, g_LogLevel))
2596 WLog_Print(log, g_LogLevel,
"GetStatusChangeA_Call {");
2597 smartcard_log_context(log, &call->handles.hContext);
2599 WLog_Print(log, g_LogLevel,
"dwTimeOut: 0x%08" PRIX32
" cReaders: %" PRIu32
"", call->dwTimeOut,
2602 dump_reader_states_a(log, call->rgReaderStates, call->cReaders);
2604 WLog_Print(log, g_LogLevel,
"}");
2608 UINT32 cReaders, UINT32* ptrIndex)
2610 LONG status = SCARD_E_NO_MEMORY;
2612 WINPR_ASSERT(ppcReaders || (cReaders == 0));
2613 if (!Stream_CheckAndLogRequiredLengthWLog(log, s, 4))
2616 const UINT32 len = Stream_Get_UINT32(s);
2617 if (len != cReaders)
2619 WLog_Print(log, WLOG_ERROR,
"Count mismatch when reading LPSCARD_READERSTATEA");
2625 BOOL* states = calloc(cReaders,
sizeof(BOOL));
2626 if (!rgReaderStates || !states)
2628 status = ERROR_INVALID_DATA;
2630 for (UINT32 index = 0; index < cReaders; index++)
2632 UINT32 ptr = UINT32_MAX;
2635 if (!Stream_CheckAndLogRequiredLengthWLog(log, s, 52))
2638 if (!smartcard_ndr_pointer_read(log, s, ptrIndex, &ptr))
2644 states[index] = ptr != 0;
2645 Stream_Read_UINT32(s, readerState->dwCurrentState);
2646 Stream_Read_UINT32(s, readerState->dwEventState);
2647 Stream_Read_UINT32(s, readerState->cbAtr);
2648 if (readerState->cbAtr > ARRAYSIZE(readerState->rgbAtr))
2650 WLog_Print(log, WLOG_ERROR,
2651 "SCARD_READERSTATEA[%" PRIu32
"]::cbAtr %" PRIu32
" exceeds %" PRIuz, index,
2652 readerState->cbAtr, (
size_t)ARRAYSIZE(readerState->rgbAtr));
2655 Stream_Read(s, readerState->rgbAtr, 36);
2658 for (UINT32 index = 0; index < cReaders; index++)
2665 status = smartcard_ndr_read_a(log, s, &readerState->szReader, NDR_PTR_FULL);
2666 if (status != SCARD_S_SUCCESS)
2670 *ppcReaders = rgReaderStates;
2672 return SCARD_S_SUCCESS;
2676 for (UINT32 index = 0; index < cReaders; index++)
2679 free(readerState->szReader);
2682 free(rgReaderStates);
2688 UINT32 cReaders, UINT32* ptrIndex)
2690 LONG status = SCARD_E_NO_MEMORY;
2692 WINPR_ASSERT(ppcReaders || (cReaders == 0));
2693 if (!Stream_CheckAndLogRequiredLengthWLog(log, s, 4))
2696 const UINT32 len = Stream_Get_UINT32(s);
2697 if (len != cReaders)
2699 WLog_Print(log, WLOG_ERROR,
"Count mismatch when reading LPSCARD_READERSTATEW");
2705 BOOL* states = calloc(cReaders,
sizeof(BOOL));
2707 if (!rgReaderStates || !states)
2710 status = ERROR_INVALID_DATA;
2711 for (UINT32 index = 0; index < cReaders; index++)
2713 UINT32 ptr = UINT32_MAX;
2716 if (!Stream_CheckAndLogRequiredLengthWLog(log, s, 52))
2719 if (!smartcard_ndr_pointer_read(log, s, ptrIndex, &ptr))
2725 states[index] = ptr != 0;
2726 Stream_Read_UINT32(s, readerState->dwCurrentState);
2727 Stream_Read_UINT32(s, readerState->dwEventState);
2728 Stream_Read_UINT32(s, readerState->cbAtr);
2729 if (readerState->cbAtr > ARRAYSIZE(readerState->rgbAtr))
2731 WLog_Print(log, WLOG_ERROR,
2732 "SCARD_READERSTATEW[%" PRIu32
"]::cbAtr %" PRIu32
" exceeds %" PRIuz, index,
2733 readerState->cbAtr, (
size_t)ARRAYSIZE(readerState->rgbAtr));
2736 Stream_Read(s, readerState->rgbAtr, 36);
2739 for (UINT32 index = 0; index < cReaders; index++)
2747 status = smartcard_ndr_read_w(log, s, &readerState->szReader, NDR_PTR_FULL);
2748 if (status != SCARD_S_SUCCESS)
2752 *ppcReaders = rgReaderStates;
2754 return SCARD_S_SUCCESS;
2758 for (UINT32 index = 0; index < cReaders; index++)
2761 free(readerState->szReader);
2764 free(rgReaderStates);
2777 UINT32 pbContextNdrPtr = 0;
2780 wLog* log = scard_log();
2782 call->rgReaderStates =
nullptr;
2784 LONG status = smartcard_unpack_redir_scard_context(log, s, &(call->handles.hContext), &index,
2786 if (status != SCARD_S_SUCCESS)
2789 if (!Stream_CheckAndLogRequiredLengthWLog(log, s, 8))
2790 return STATUS_BUFFER_TOO_SMALL;
2792 Stream_Read_UINT32(s, call->dwTimeOut);
2793 const DWORD cReaders = Stream_Get_UINT32(s);
2794 if (!smartcard_ndr_pointer_read(log, s, &index, &ndrPtr))
2795 return ERROR_INVALID_DATA;
2797 status = smartcard_unpack_redir_scard_context_ref(log, s, pbContextNdrPtr,
2798 &(call->handles.hContext));
2799 if (status != SCARD_S_SUCCESS)
2804 status = smartcard_unpack_reader_state_a(log, s, &call->rgReaderStates, cReaders, &index);
2805 if (status != SCARD_S_SUCCESS)
2807 call->cReaders = cReaders;
2811 WLog_Print(log, WLOG_WARN,
"ndrPtr=0x%08" PRIx32
", can not read rgReaderStates", ndrPtr);
2812 return SCARD_E_UNEXPECTED;
2815 smartcard_trace_get_status_change_a_call(log, call);
2816 return SCARD_S_SUCCESS;
2823 UINT32 pbContextNdrPtr = 0;
2826 wLog* log = scard_log();
2827 call->rgReaderStates =
nullptr;
2829 LONG status = smartcard_unpack_redir_scard_context(log, s, &(call->handles.hContext), &index,
2831 if (status != SCARD_S_SUCCESS)
2834 if (!Stream_CheckAndLogRequiredLengthWLog(log, s, 8))
2835 return STATUS_BUFFER_TOO_SMALL;
2837 Stream_Read_UINT32(s, call->dwTimeOut);
2838 const DWORD cReaders = Stream_Get_UINT32(s);
2839 if (!smartcard_ndr_pointer_read(log, s, &index, &ndrPtr))
2840 return ERROR_INVALID_DATA;
2842 status = smartcard_unpack_redir_scard_context_ref(log, s, pbContextNdrPtr,
2843 &(call->handles.hContext));
2844 if (status != SCARD_S_SUCCESS)
2849 status = smartcard_unpack_reader_state_w(log, s, &call->rgReaderStates, cReaders, &index);
2850 if (status != SCARD_S_SUCCESS)
2852 call->cReaders = cReaders;
2856 WLog_Print(log, WLOG_WARN,
"ndrPtr=0x%08" PRIx32
", can not read rgReaderStates", ndrPtr);
2857 return SCARD_E_UNEXPECTED;
2860 smartcard_trace_get_status_change_w_call(log, call);
2861 return SCARD_S_SUCCESS;
2868 wLog* log = scard_log();
2871 DWORD cReaders = ret->cReaders;
2874 smartcard_trace_get_status_change_return(log, ret, unicode);
2875 if ((ret->ReturnCode != SCARD_S_SUCCESS) && (ret->ReturnCode != SCARD_E_TIMEOUT))
2877 if (cReaders == SCARD_AUTOALLOCATE)
2880 if (!Stream_EnsureRemainingCapacity(s, 4))
2881 return SCARD_E_NO_MEMORY;
2883 Stream_Write_UINT32(s, cReaders);
2884 if (!smartcard_ndr_pointer_write(s, &index, cReaders))
2885 return SCARD_E_NO_MEMORY;
2886 status = smartcard_ndr_write_state(s, ret->rgReaderStates, cReaders, NDR_PTR_SIMPLE);
2887 if (status != SCARD_S_SUCCESS)
2889 return ret->ReturnCode;
2895 UINT32 pbContextNdrPtr = 0;
2897 wLog* log = scard_log();
2900 LONG status = smartcard_unpack_redir_scard_context(log, s, &(call->handles.hContext), &index,
2902 if (status != SCARD_S_SUCCESS)
2905 status = smartcard_unpack_redir_scard_handle(log, s, &(call->handles.hCard), &index);
2906 if (status != SCARD_S_SUCCESS)
2909 if (!Stream_CheckAndLogRequiredLengthWLog(log, s, 8))
2910 return STATUS_BUFFER_TOO_SMALL;
2912 Stream_Read_INT32(s, call->fpbAtrIsNULL);
2913 Stream_Read_UINT32(s, call->cbAtrLen);
2915 status = smartcard_unpack_redir_scard_context_ref(log, s, pbContextNdrPtr,
2916 &(call->handles.hContext));
2917 if (status != SCARD_S_SUCCESS)
2920 status = smartcard_unpack_redir_scard_handle_ref(log, s, &(call->handles.hCard));
2921 if (status != SCARD_S_SUCCESS)
2930 wLog* log = scard_log();
2932 DWORD cbAtrLen = ret->cbAtrLen;
2935 smartcard_trace_state_return(log, ret);
2936 if (ret->ReturnCode != SCARD_S_SUCCESS)
2938 if (cbAtrLen == SCARD_AUTOALLOCATE)
2941 Stream_Write_UINT32(s, ret->dwState);
2942 Stream_Write_UINT32(s, ret->dwProtocol);
2943 Stream_Write_UINT32(s, cbAtrLen);
2944 if (!smartcard_ndr_pointer_write(s, &index, cbAtrLen))
2945 return SCARD_E_NO_MEMORY;
2946 status = smartcard_ndr_write(s, ret->rgAtr, cbAtrLen, 1, NDR_PTR_SIMPLE);
2947 if (status != SCARD_S_SUCCESS)
2949 return ret->ReturnCode;
2955 UINT32 pbContextNdrPtr = 0;
2958 wLog* log = scard_log();
2960 LONG status = smartcard_unpack_redir_scard_context(log, s, &(call->handles.hContext), &index,
2962 if (status != SCARD_S_SUCCESS)
2965 status = smartcard_unpack_redir_scard_handle(log, s, &(call->handles.hCard), &index);
2966 if (status != SCARD_S_SUCCESS)
2969 if (!Stream_CheckAndLogRequiredLengthWLog(log, s, 12))
2970 return STATUS_BUFFER_TOO_SMALL;
2972 Stream_Read_INT32(s, call->fmszReaderNamesIsNULL);
2973 Stream_Read_UINT32(s, call->cchReaderLen);
2974 Stream_Read_UINT32(s, call->cbAtrLen);
2976 status = smartcard_unpack_redir_scard_context_ref(log, s, pbContextNdrPtr,
2977 &(call->handles.hContext));
2978 if (status != SCARD_S_SUCCESS)
2981 status = smartcard_unpack_redir_scard_handle_ref(log, s, &(call->handles.hCard));
2982 if (status != SCARD_S_SUCCESS)
2985 smartcard_trace_status_call(log, call, unicode);
2992 wLog* log = scard_log();
2995 smartcard_trace_status_call(log, call, unicode);
2997 LONG status = smartcard_pack_redir_scard_context(log, s, &call->handles.hContext, &index);
2998 if (status != SCARD_S_SUCCESS)
3001 status = smartcard_pack_redir_scard_handle(log, s, &call->handles.hCard, &index);
3002 if (status != SCARD_S_SUCCESS)
3005 if (!Stream_EnsureRemainingCapacity(s, 12))
3006 return SCARD_E_NO_MEMORY;
3008 Stream_Write_INT32(s, call->fmszReaderNamesIsNULL);
3009 Stream_Write_UINT32(s, call->cchReaderLen);
3010 Stream_Write_UINT32(s, call->cbAtrLen);
3012 status = smartcard_pack_redir_scard_context_ref(log, s, &call->handles.hContext);
3013 if (status != SCARD_S_SUCCESS)
3016 return smartcard_pack_redir_scard_handle_ref(log, s, &call->handles.hCard);
3022 wLog* log = scard_log();
3026 DWORD cBytes = ret->cBytes;
3028 smartcard_trace_status_return(log, ret, unicode);
3029 if (ret->ReturnCode != SCARD_S_SUCCESS)
3031 if (cBytes == SCARD_AUTOALLOCATE)
3034 if (!Stream_EnsureRemainingCapacity(s, 4))
3035 return SCARD_F_INTERNAL_ERROR;
3037 Stream_Write_UINT32(s, cBytes);
3038 if (!smartcard_ndr_pointer_write(s, &index, cBytes))
3039 return SCARD_E_NO_MEMORY;
3041 if (!Stream_EnsureRemainingCapacity(s, 44))
3042 return SCARD_F_INTERNAL_ERROR;
3044 Stream_Write_UINT32(s, ret->dwState);
3045 Stream_Write_UINT32(s, ret->dwProtocol);
3046 Stream_Write(s, ret->pbAtr,
sizeof(ret->pbAtr));
3047 Stream_Write_UINT32(s, ret->cbAtrLen);
3048 status = smartcard_ndr_write(s, ret->mszReaderNames, cBytes, 1, NDR_PTR_SIMPLE);
3049 if (status != SCARD_S_SUCCESS)
3051 return ret->ReturnCode;
3057 wLog* log = scard_log();
3059 UINT32 mszNdrPtr = 0;
3061 if (!Stream_CheckAndLogRequiredLengthWLog(log, s, 4))
3062 return STATUS_BUFFER_TOO_SMALL;
3064 const UINT32 cBytes = Stream_Get_UINT32(s);
3066 if (!smartcard_ndr_pointer_read(log, s, &index, &mszNdrPtr))
3067 return ERROR_INVALID_DATA;
3069 if (!Stream_CheckAndLogRequiredLengthWLog(log, s, 44))
3070 return STATUS_BUFFER_TOO_SMALL;
3072 Stream_Read_UINT32(s, ret->dwState);
3073 Stream_Read_UINT32(s, ret->dwProtocol);
3074 Stream_Read(s, ret->pbAtr,
sizeof(ret->pbAtr));
3075 Stream_Read_UINT32(s, ret->cbAtrLen);
3079 LONG status = smartcard_ndr_read(log, s, &ret->mszReaderNames, cBytes, 1, NDR_PTR_SIMPLE);
3080 if (status != SCARD_S_SUCCESS)
3082 ret->cBytes = cBytes;
3085 smartcard_trace_status_return(log, ret, unicode);
3086 return SCARD_S_SUCCESS;
3092 wLog* log = scard_log();
3094 UINT32 pbContextNdrPtr = 0;
3096 LONG status = smartcard_unpack_redir_scard_context(log, s, &(call->handles.hContext), &index,
3098 if (status != SCARD_S_SUCCESS)
3101 status = smartcard_unpack_redir_scard_handle(log, s, &(call->handles.hCard), &index);
3102 if (status != SCARD_S_SUCCESS)
3105 if (!Stream_CheckAndLogRequiredLengthWLog(log, s, 12))
3106 return STATUS_BUFFER_TOO_SMALL;
3108 Stream_Read_UINT32(s, call->dwAttrId);
3109 Stream_Read_INT32(s, call->fpbAttrIsNULL);
3110 Stream_Read_UINT32(s, call->cbAttrLen);
3112 status = smartcard_unpack_redir_scard_context_ref(log, s, pbContextNdrPtr,
3113 &(call->handles.hContext));
3114 if (status != SCARD_S_SUCCESS)
3117 status = smartcard_unpack_redir_scard_handle_ref(log, s, &(call->handles.hCard));
3118 if (status != SCARD_S_SUCCESS)
3121 smartcard_trace_get_attrib_call(log, call);
3126 DWORD cbAttrCallLen)
3129 wLog* log = scard_log();
3131 DWORD cbAttrLen = 0;
3133 smartcard_trace_get_attrib_return(log, ret, dwAttrId);
3135 if (!Stream_EnsureRemainingCapacity(s, 4))
3136 return SCARD_F_INTERNAL_ERROR;
3138 cbAttrLen = ret->cbAttrLen;
3139 if (ret->ReturnCode != SCARD_S_SUCCESS)
3141 if (cbAttrLen == SCARD_AUTOALLOCATE)
3146 if (cbAttrCallLen < cbAttrLen)
3147 cbAttrLen = cbAttrCallLen;
3149 Stream_Write_UINT32(s, cbAttrLen);
3150 if (!smartcard_ndr_pointer_write(s, &index, cbAttrLen))
3151 return SCARD_E_NO_MEMORY;
3153 status = smartcard_ndr_write(s, ret->pbAttr, cbAttrLen, 1, NDR_PTR_SIMPLE);
3154 if (status != SCARD_S_SUCCESS)
3156 return ret->ReturnCode;
3162 wLog* log = scard_log();
3165 UINT32 pvInBufferNdrPtr = 0;
3166 UINT32 pbContextNdrPtr = 0;
3168 call->pvInBuffer =
nullptr;
3170 LONG status = smartcard_unpack_redir_scard_context(log, s, &(call->handles.hContext), &index,
3172 if (status != SCARD_S_SUCCESS)
3175 status = smartcard_unpack_redir_scard_handle(log, s, &(call->handles.hCard), &index);
3176 if (status != SCARD_S_SUCCESS)
3179 if (!Stream_CheckAndLogRequiredLengthWLog(log, s, 20))
3180 return STATUS_BUFFER_TOO_SMALL;
3182 Stream_Read_UINT32(s, call->dwControlCode);
3183 const UINT32 cbInBufferSize = Stream_Get_UINT32(s);
3184 if (!smartcard_ndr_pointer_read(log, s, &index,
3186 return ERROR_INVALID_DATA;
3187 Stream_Read_INT32(s, call->fpvOutBufferIsNULL);
3188 Stream_Read_UINT32(s, call->cbOutBufferSize);
3190 status = smartcard_unpack_redir_scard_context_ref(log, s, pbContextNdrPtr,
3191 &(call->handles.hContext));
3192 if (status != SCARD_S_SUCCESS)
3195 status = smartcard_unpack_redir_scard_handle_ref(log, s, &(call->handles.hCard));
3196 if (status != SCARD_S_SUCCESS)
3199 if (pvInBufferNdrPtr)
3201 status = smartcard_ndr_read(log, s, &call->pvInBuffer, cbInBufferSize, 1, NDR_PTR_SIMPLE);
3202 if (status != SCARD_S_SUCCESS)
3204 call->cbInBufferSize = cbInBufferSize;
3207 smartcard_trace_control_call(log, call);
3208 return SCARD_S_SUCCESS;
3214 wLog* log = scard_log();
3217 DWORD cbDataLen = ret->cbOutBufferSize;
3220 smartcard_trace_control_return(log, ret);
3221 if (ret->ReturnCode != SCARD_S_SUCCESS)
3223 if (cbDataLen == SCARD_AUTOALLOCATE)
3226 if (!Stream_EnsureRemainingCapacity(s, 4))
3227 return SCARD_F_INTERNAL_ERROR;
3229 Stream_Write_UINT32(s, cbDataLen);
3230 if (!smartcard_ndr_pointer_write(s, &index, cbDataLen))
3231 return SCARD_E_NO_MEMORY;
3233 status = smartcard_ndr_write(s, ret->pvOutBuffer, cbDataLen, 1, NDR_PTR_SIMPLE);
3234 if (status != SCARD_S_SUCCESS)
3236 return ret->ReturnCode;
3242 BYTE* pbExtraBytes =
nullptr;
3243 UINT32 pbExtraBytesNdrPtr = 0;
3244 UINT32 pbSendBufferNdrPtr = 0;
3245 UINT32 pioRecvPciNdrPtr = 0;
3249 UINT32 pbContextNdrPtr = 0;
3252 wLog* log = scard_log();
3254 call->pioSendPci =
nullptr;
3255 call->pioRecvPci =
nullptr;
3256 call->pbSendBuffer =
nullptr;
3258 LONG status = smartcard_unpack_redir_scard_context(log, s, &(call->handles.hContext), &index,
3260 if (status != SCARD_S_SUCCESS)
3263 status = smartcard_unpack_redir_scard_handle(log, s, &(call->handles.hCard), &index);
3264 if (status != SCARD_S_SUCCESS)
3267 if (!Stream_CheckAndLogRequiredLengthWLog(log, s, 32))
3268 return STATUS_BUFFER_TOO_SMALL;
3270 Stream_Read_UINT32(s, ioSendPci.dwProtocol);
3271 Stream_Read_UINT32(s, ioSendPci.cbExtraBytes);
3272 if (!smartcard_ndr_pointer_read(log, s, &index,
3273 &pbExtraBytesNdrPtr))
3274 return ERROR_INVALID_DATA;
3276 const UINT32 cbSendLength = Stream_Get_UINT32(s);
3277 if (!smartcard_ndr_pointer_read(log, s, &index,
3278 &pbSendBufferNdrPtr))
3279 return ERROR_INVALID_DATA;
3281 if (!smartcard_ndr_pointer_read(log, s, &index,
3283 return ERROR_INVALID_DATA;
3285 Stream_Read_INT32(s, call->fpbRecvBufferIsNULL);
3286 Stream_Read_UINT32(s, call->cbRecvLength);
3288 if (ioSendPci.cbExtraBytes > 1024)
3290 WLog_Print(log, WLOG_WARN,
3291 "Transmit_Call ioSendPci.cbExtraBytes is out of bounds: %" PRIu32
" (max: 1024)",
3292 ioSendPci.cbExtraBytes);
3293 return STATUS_INVALID_PARAMETER;
3296 if (cbSendLength > 66560)
3298 WLog_Print(log, WLOG_WARN,
3299 "Transmit_Call cbSendLength is out of bounds: %" PRIu32
" (max: 66560)",
3300 ioSendPci.cbExtraBytes);
3301 return STATUS_INVALID_PARAMETER;
3304 status = smartcard_unpack_redir_scard_context_ref(log, s, pbContextNdrPtr,
3305 &(call->handles.hContext));
3306 if (status != SCARD_S_SUCCESS)
3309 status = smartcard_unpack_redir_scard_handle_ref(log, s, &(call->handles.hCard));
3310 if (status != SCARD_S_SUCCESS)
3313 if (ioSendPci.cbExtraBytes && !pbExtraBytesNdrPtr)
3317 "Transmit_Call ioSendPci.cbExtraBytes is non-zero but pbExtraBytesNdrPtr is null");
3318 return STATUS_INVALID_PARAMETER;
3321 if (pbExtraBytesNdrPtr)
3324 if (!Stream_CheckAndLogRequiredLengthWLog(log, s, 4))
3325 return STATUS_BUFFER_TOO_SMALL;
3327 Stream_Read_UINT32(s, length);
3329 if (!Stream_CheckAndLogRequiredLengthWLog(log, s, ioSendPci.cbExtraBytes))
3330 return STATUS_BUFFER_TOO_SMALL;
3332 ioSendPci.pbExtraBytes = Stream_Pointer(s);
3336 if (!call->pioSendPci)
3338 WLog_Print(log, WLOG_WARN,
"Transmit_Call out of memory error (pioSendPci)");
3339 return STATUS_NO_MEMORY;
3342 call->pioSendPci->dwProtocol = ioSendPci.dwProtocol;
3343 call->pioSendPci->cbPciLength = (DWORD)(ioSendPci.cbExtraBytes +
sizeof(
SCARD_IO_REQUEST));
3345 Stream_Read(s, pbExtraBytes, ioSendPci.cbExtraBytes);
3346 if (smartcard_unpack_read_size_align(s, ioSendPci.cbExtraBytes, 4) < 0)
3347 return STATUS_INVALID_PARAMETER;
3353 if (!call->pioSendPci)
3355 WLog_Print(log, WLOG_WARN,
"Transmit_Call out of memory error (pioSendPci)");
3356 return STATUS_NO_MEMORY;
3359 call->pioSendPci->dwProtocol = ioSendPci.dwProtocol;
3363 if (pbSendBufferNdrPtr)
3365 status = smartcard_ndr_read(log, s, &call->pbSendBuffer, cbSendLength, 1, NDR_PTR_SIMPLE);
3366 if (status != SCARD_S_SUCCESS)
3368 call->cbSendLength = cbSendLength;
3371 if (pioRecvPciNdrPtr)
3373 if (!Stream_CheckAndLogRequiredLengthWLog(log, s, 12))
3374 return STATUS_BUFFER_TOO_SMALL;
3376 Stream_Read_UINT32(s, ioRecvPci.dwProtocol);
3377 Stream_Read_UINT32(s, ioRecvPci.cbExtraBytes);
3378 if (!smartcard_ndr_pointer_read(log, s, &index,
3379 &pbExtraBytesNdrPtr))
3380 return ERROR_INVALID_DATA;
3382 if (ioRecvPci.cbExtraBytes && !pbExtraBytesNdrPtr)
3386 "Transmit_Call ioRecvPci.cbExtraBytes is non-zero but pbExtraBytesNdrPtr is null");
3387 return STATUS_INVALID_PARAMETER;
3390 if (pbExtraBytesNdrPtr)
3393 if (!Stream_CheckAndLogRequiredLengthWLog(log, s, 4))
3394 return STATUS_BUFFER_TOO_SMALL;
3396 Stream_Read_UINT32(s, length);
3398 if (ioRecvPci.cbExtraBytes > 1024)
3400 WLog_Print(log, WLOG_WARN,
3401 "Transmit_Call ioRecvPci.cbExtraBytes is out of bounds: %" PRIu32
3403 ioRecvPci.cbExtraBytes);
3404 return STATUS_INVALID_PARAMETER;
3407 if (length != ioRecvPci.cbExtraBytes)
3409 WLog_Print(log, WLOG_WARN,
3410 "Transmit_Call unexpected length: Actual: %" PRIu32
3411 ", Expected: %" PRIu32
" (ioRecvPci.cbExtraBytes)",
3412 length, ioRecvPci.cbExtraBytes);
3413 return STATUS_INVALID_PARAMETER;
3416 if (!Stream_CheckAndLogRequiredLengthWLog(log, s, ioRecvPci.cbExtraBytes))
3417 return STATUS_BUFFER_TOO_SMALL;
3419 ioRecvPci.pbExtraBytes = Stream_Pointer(s);
3423 if (!call->pioRecvPci)
3425 WLog_Print(log, WLOG_WARN,
"Transmit_Call out of memory error (pioRecvPci)");
3426 return STATUS_NO_MEMORY;
3429 call->pioRecvPci->dwProtocol = ioRecvPci.dwProtocol;
3430 call->pioRecvPci->cbPciLength =
3433 Stream_Read(s, pbExtraBytes, ioRecvPci.cbExtraBytes);
3434 if (smartcard_unpack_read_size_align(s, ioRecvPci.cbExtraBytes, 4) < 0)
3435 return STATUS_INVALID_PARAMETER;
3441 if (!call->pioRecvPci)
3443 WLog_Print(log, WLOG_WARN,
"Transmit_Call out of memory error (pioRecvPci)");
3444 return STATUS_NO_MEMORY;
3447 call->pioRecvPci->dwProtocol = ioRecvPci.dwProtocol;
3452 smartcard_trace_transmit_call(log, call);
3453 return SCARD_S_SUCCESS;
3459 wLog* log = scard_log();
3464 UINT32 cbRecvLength = ret->cbRecvLength;
3465 UINT32 cbRecvPci = ret->pioRecvPci ? ret->pioRecvPci->cbPciLength : 0;
3467 smartcard_trace_transmit_return(log, ret);
3469 if (!ret->pbRecvBuffer)
3472 if (!smartcard_ndr_pointer_write(s, &index, cbRecvPci))
3473 return SCARD_E_NO_MEMORY;
3474 if (!Stream_EnsureRemainingCapacity(s, 4))
3475 return SCARD_E_NO_MEMORY;
3476 Stream_Write_UINT32(s, cbRecvLength);
3477 if (!smartcard_ndr_pointer_write(s, &index, cbRecvLength))
3478 return SCARD_E_NO_MEMORY;
3480 if (ret->pioRecvPci)
3482 UINT32 cbExtraBytes = (UINT32)(ret->pioRecvPci->cbPciLength -
sizeof(
SCARD_IO_REQUEST));
3485 if (!Stream_EnsureRemainingCapacity(s, cbExtraBytes + 16))
3487 WLog_Print(log, WLOG_ERROR,
"Stream_EnsureRemainingCapacity failed!");
3488 return SCARD_F_INTERNAL_ERROR;
3491 Stream_Write_UINT32(s, ret->pioRecvPci->dwProtocol);
3492 Stream_Write_UINT32(s, cbExtraBytes);
3493 if (!smartcard_ndr_pointer_write(s, &index, cbExtraBytes))
3494 return SCARD_E_NO_MEMORY;
3495 error = smartcard_ndr_write(s, pbExtraBytes, cbExtraBytes, 1, NDR_PTR_SIMPLE);
3500 status = smartcard_ndr_write(s, ret->pbRecvBuffer, ret->cbRecvLength, 1, NDR_PTR_SIMPLE);
3501 if (status != SCARD_S_SUCCESS)
3503 return ret->ReturnCode;
3508 UINT32 rgReaderStatesNdrPtr = 0;
3509 UINT32 rgAtrMasksNdrPtr = 0;
3511 UINT32 pbContextNdrPtr = 0;
3514 wLog* log = scard_log();
3516 call->rgReaderStates =
nullptr;
3518 LONG status = smartcard_unpack_redir_scard_context(log, s, &(call->handles.hContext), &index,
3520 if (status != SCARD_S_SUCCESS)
3523 if (!Stream_CheckAndLogRequiredLengthWLog(log, s, 16))
3524 return STATUS_BUFFER_TOO_SMALL;
3526 Stream_Read_UINT32(s, call->cAtrs);
3527 if (!smartcard_ndr_pointer_read(log, s, &index, &rgAtrMasksNdrPtr))
3528 return ERROR_INVALID_DATA;
3529 const DWORD cReaders = Stream_Get_UINT32(s);
3530 if (!smartcard_ndr_pointer_read(log, s, &index, &rgReaderStatesNdrPtr))
3531 return ERROR_INVALID_DATA;
3533 status = smartcard_unpack_redir_scard_context_ref(log, s, pbContextNdrPtr,
3534 &(call->handles.hContext));
3535 if (status != SCARD_S_SUCCESS)
3538 if ((rgAtrMasksNdrPtr && !call->cAtrs) || (!rgAtrMasksNdrPtr && call->cAtrs))
3540 WLog_Print(log, WLOG_WARN,
3541 "LocateCardsByATRA_Call rgAtrMasksNdrPtr (0x%08" PRIX32
3542 ") and cAtrs (0x%08" PRIX32
") inconsistency",
3543 rgAtrMasksNdrPtr, call->cAtrs);
3544 return STATUS_INVALID_PARAMETER;
3547 if (rgAtrMasksNdrPtr)
3549 status = smartcard_ndr_read_atrmask(log, s, &call->rgAtrMasks, call->cAtrs, NDR_PTR_SIMPLE);
3550 if (status != SCARD_S_SUCCESS)
3554 if (rgReaderStatesNdrPtr)
3556 status = smartcard_unpack_reader_state_a(log, s, &call->rgReaderStates, cReaders, &index);
3557 if (status != SCARD_S_SUCCESS)
3559 call->cReaders = cReaders;
3562 smartcard_trace_locate_cards_by_atr_a_call(log, call);
3563 return SCARD_S_SUCCESS;
3568 UINT32 sz1NdrPtr = 0;
3569 UINT32 sz2NdrPtr = 0;
3571 UINT32 pbContextNdrPtr = 0;
3574 wLog* log = scard_log();
3576 LONG status = smartcard_unpack_redir_scard_context(log, s, &(call->handles.hContext), &index,
3578 if (status != SCARD_S_SUCCESS)
3581 if (!smartcard_ndr_pointer_read(log, s, &index, &sz1NdrPtr))
3582 return ERROR_INVALID_DATA;
3583 if (!smartcard_ndr_pointer_read(log, s, &index, &sz2NdrPtr))
3584 return ERROR_INVALID_DATA;
3587 smartcard_unpack_redir_scard_context_ref(log, s, pbContextNdrPtr, &call->handles.hContext);
3588 if (status != SCARD_S_SUCCESS)
3593 status = smartcard_ndr_read_a(log, s, &call->sz1, NDR_PTR_FULL);
3594 if (status != SCARD_S_SUCCESS)
3599 status = smartcard_ndr_read_a(log, s, &call->sz2, NDR_PTR_FULL);
3600 if (status != SCARD_S_SUCCESS)
3603 smartcard_trace_context_and_two_strings_a_call(log, call);
3604 return SCARD_S_SUCCESS;
3609 UINT32 sz1NdrPtr = 0;
3610 UINT32 sz2NdrPtr = 0;
3612 UINT32 pbContextNdrPtr = 0;
3615 wLog* log = scard_log();
3617 LONG status = smartcard_unpack_redir_scard_context(log, s, &(call->handles.hContext), &index,
3619 if (status != SCARD_S_SUCCESS)
3622 if (!smartcard_ndr_pointer_read(log, s, &index, &sz1NdrPtr))
3623 return ERROR_INVALID_DATA;
3624 if (!smartcard_ndr_pointer_read(log, s, &index, &sz2NdrPtr))
3625 return ERROR_INVALID_DATA;
3628 smartcard_unpack_redir_scard_context_ref(log, s, pbContextNdrPtr, &call->handles.hContext);
3629 if (status != SCARD_S_SUCCESS)
3634 status = smartcard_ndr_read_w(log, s, &call->sz1, NDR_PTR_FULL);
3635 if (status != SCARD_S_SUCCESS)
3640 status = smartcard_ndr_read_w(log, s, &call->sz2, NDR_PTR_FULL);
3641 if (status != SCARD_S_SUCCESS)
3644 smartcard_trace_context_and_two_strings_w_call(log, call);
3645 return SCARD_S_SUCCESS;
3650 UINT32 sz1NdrPtr = 0;
3651 UINT32 sz2NdrPtr = 0;
3653 UINT32 pbContextNdrPtr = 0;
3656 wLog* log = scard_log();
3658 LONG status = smartcard_unpack_redir_scard_context(log, s, &(call->handles.hContext), &index,
3660 if (status != SCARD_S_SUCCESS)
3663 if (!Stream_CheckAndLogRequiredLengthWLog(log, s, 16))
3664 return STATUS_BUFFER_TOO_SMALL;
3666 const UINT32 cBytes = Stream_Get_UINT32(s);
3667 if (!smartcard_ndr_pointer_read(log, s, &index, &sz1NdrPtr))
3668 return ERROR_INVALID_DATA;
3670 const DWORD cReaders = Stream_Get_UINT32(s);
3671 if (!smartcard_ndr_pointer_read(log, s, &index, &sz2NdrPtr))
3672 return ERROR_INVALID_DATA;
3674 status = smartcard_unpack_redir_scard_context_ref(log, s, pbContextNdrPtr,
3675 &(call->handles.hContext));
3676 if (status != SCARD_S_SUCCESS)
3681 status = smartcard_ndr_read_fixed_string_a(log, s, &call->mszCards, cBytes, NDR_PTR_SIMPLE);
3682 if (status != SCARD_S_SUCCESS)
3684 call->cBytes = cBytes;
3688 status = smartcard_unpack_reader_state_a(log, s, &call->rgReaderStates, cReaders, &index);
3689 if (status != SCARD_S_SUCCESS)
3691 call->cReaders = cReaders;
3693 smartcard_trace_locate_cards_a_call(log, call);
3694 return SCARD_S_SUCCESS;
3699 UINT32 sz1NdrPtr = 0;
3700 UINT32 sz2NdrPtr = 0;
3702 UINT32 pbContextNdrPtr = 0;
3705 wLog* log = scard_log();
3707 LONG status = smartcard_unpack_redir_scard_context(log, s, &(call->handles.hContext), &index,
3709 if (status != SCARD_S_SUCCESS)
3712 if (!Stream_CheckAndLogRequiredLengthWLog(log, s, 16))
3713 return STATUS_BUFFER_TOO_SMALL;
3715 const UINT32 cBytes = Stream_Get_UINT32(s);
3716 if (!smartcard_ndr_pointer_read(log, s, &index, &sz1NdrPtr))
3717 return ERROR_INVALID_DATA;
3719 const DWORD cReaders = Stream_Get_UINT32(s);
3720 if (!smartcard_ndr_pointer_read(log, s, &index, &sz2NdrPtr))
3721 return ERROR_INVALID_DATA;
3723 status = smartcard_unpack_redir_scard_context_ref(log, s, pbContextNdrPtr,
3724 &(call->handles.hContext));
3725 if (status != SCARD_S_SUCCESS)
3730 status = smartcard_ndr_read_fixed_string_w(log, s, &call->mszCards, cBytes, NDR_PTR_SIMPLE);
3731 if (status != SCARD_S_SUCCESS)
3733 call->cBytes = cBytes;
3737 status = smartcard_unpack_reader_state_w(log, s, &call->rgReaderStates, cReaders, &index);
3738 if (status != SCARD_S_SUCCESS)
3740 call->cReaders = cReaders;
3742 smartcard_trace_locate_cards_w_call(log, call);
3743 return SCARD_S_SUCCESS;
3750 UINT32 pbContextNdrPtr = 0;
3753 wLog* log = scard_log();
3755 LONG status = smartcard_unpack_redir_scard_context(log, s, &(call->handles.hContext), &index,
3757 if (status != SCARD_S_SUCCESS)
3759 status = smartcard_unpack_redir_scard_handle(log, s, &(call->handles.hCard), &index);
3760 if (status != SCARD_S_SUCCESS)
3763 if (!Stream_CheckAndLogRequiredLengthWLog(log, s, 12))
3764 return STATUS_BUFFER_TOO_SMALL;
3765 Stream_Read_UINT32(s, call->dwAttrId);
3766 Stream_Read_UINT32(s, call->cbAttrLen);
3768 if (!smartcard_ndr_pointer_read(log, s, &index, &ndrPtr))
3769 return ERROR_INVALID_DATA;
3771 status = smartcard_unpack_redir_scard_context_ref(log, s, pbContextNdrPtr,
3772 &(call->handles.hContext));
3773 if (status != SCARD_S_SUCCESS)
3776 status = smartcard_unpack_redir_scard_handle_ref(log, s, &(call->handles.hCard));
3777 if (status != SCARD_S_SUCCESS)
3783 status = smartcard_ndr_read_ex(log, s, &call->pbAttr, 0, 1, NDR_PTR_SIMPLE, &len);
3784 if (status != SCARD_S_SUCCESS)
3786 if (call->cbAttrLen > len)
3787 call->cbAttrLen = WINPR_ASSERTING_INT_CAST(DWORD, len);
3790 call->cbAttrLen = 0;
3791 smartcard_trace_set_attrib_call(log, call);
3792 return SCARD_S_SUCCESS;
3798 wLog* log = scard_log();
3801 smartcard_trace_set_attrib_call(log, call);
3803 LONG status = smartcard_pack_redir_scard_context(log, s, &call->handles.hContext, &index);
3804 if (status != SCARD_S_SUCCESS)
3807 status = smartcard_pack_redir_scard_handle(log, s, &call->handles.hCard, &index);
3808 if (status != SCARD_S_SUCCESS)
3811 if (!Stream_EnsureRemainingCapacity(s, 8))
3812 return SCARD_E_NO_MEMORY;
3814 Stream_Write_UINT32(s, call->dwAttrId);
3815 Stream_Write_UINT32(s, call->cbAttrLen);
3817 if (!smartcard_ndr_pointer_write(s, &index, call->cbAttrLen))
3818 return SCARD_E_NO_MEMORY;
3820 status = smartcard_pack_redir_scard_context_ref(log, s, &call->handles.hContext);
3821 if (status != SCARD_S_SUCCESS)
3824 status = smartcard_pack_redir_scard_handle_ref(log, s, &call->handles.hCard);
3825 if (status != SCARD_S_SUCCESS)
3828 status = smartcard_ndr_write(s, call->pbAttr, call->cbAttrLen, 1, NDR_PTR_SIMPLE);
3829 if (status != SCARD_S_SUCCESS)
3832 return SCARD_S_SUCCESS;
3837 UINT32 rgReaderStatesNdrPtr = 0;
3838 UINT32 rgAtrMasksNdrPtr = 0;
3840 UINT32 pbContextNdrPtr = 0;
3843 wLog* log = scard_log();
3845 call->rgReaderStates =
nullptr;
3847 LONG status = smartcard_unpack_redir_scard_context(log, s, &(call->handles.hContext), &index,
3849 if (status != SCARD_S_SUCCESS)
3852 if (!Stream_CheckAndLogRequiredLengthWLog(log, s, 16))
3853 return STATUS_BUFFER_TOO_SMALL;
3855 Stream_Read_UINT32(s, call->cAtrs);
3856 if (!smartcard_ndr_pointer_read(log, s, &index, &rgAtrMasksNdrPtr))
3857 return ERROR_INVALID_DATA;
3859 const DWORD cReaders = Stream_Get_UINT32(s);
3860 if (!smartcard_ndr_pointer_read(log, s, &index, &rgReaderStatesNdrPtr))
3861 return ERROR_INVALID_DATA;
3863 status = smartcard_unpack_redir_scard_context_ref(log, s, pbContextNdrPtr,
3864 &(call->handles.hContext));
3865 if (status != SCARD_S_SUCCESS)
3868 if ((rgAtrMasksNdrPtr && !call->cAtrs) || (!rgAtrMasksNdrPtr && call->cAtrs))
3870 WLog_Print(log, WLOG_WARN,
3871 "LocateCardsByATRW_Call rgAtrMasksNdrPtr (0x%08" PRIX32
3872 ") and cAtrs (0x%08" PRIX32
") inconsistency",
3873 rgAtrMasksNdrPtr, call->cAtrs);
3874 return STATUS_INVALID_PARAMETER;
3877 if (rgAtrMasksNdrPtr)
3879 status = smartcard_ndr_read_atrmask(log, s, &call->rgAtrMasks, call->cAtrs, NDR_PTR_SIMPLE);
3880 if (status != SCARD_S_SUCCESS)
3884 if (rgReaderStatesNdrPtr)
3886 status = smartcard_unpack_reader_state_w(log, s, &call->rgReaderStates, cReaders, &index);
3887 if (status != SCARD_S_SUCCESS)
3889 call->cReaders = cReaders;
3892 smartcard_trace_locate_cards_by_atr_w_call(log, call);
3893 return SCARD_S_SUCCESS;
3898 UINT32 mszNdrPtr = 0;
3899 UINT32 contextNdrPtr = 0;
3901 UINT32 pbContextNdrPtr = 0;
3904 wLog* log = scard_log();
3906 if (!smartcard_ndr_pointer_read(log, s, &index, &mszNdrPtr))
3907 return ERROR_INVALID_DATA;
3909 LONG status = smartcard_unpack_redir_scard_context(log, s, &(call->Common.handles.hContext),
3910 &index, &pbContextNdrPtr);
3911 if (status != SCARD_S_SUCCESS)
3914 if (!smartcard_ndr_pointer_read(log, s, &index, &contextNdrPtr))
3915 return ERROR_INVALID_DATA;
3917 if (!Stream_CheckAndLogRequiredLengthWLog(log, s, 12))
3918 return STATUS_BUFFER_TOO_SMALL;
3919 Stream_Read_UINT32(s, call->Common.FreshnessCounter);
3920 Stream_Read_INT32(s, call->Common.fPbDataIsNULL);
3921 Stream_Read_UINT32(s, call->Common.cbDataLen);
3923 call->szLookupName =
nullptr;
3926 status = smartcard_ndr_read_a(log, s, &call->szLookupName, NDR_PTR_FULL);
3927 if (status != SCARD_S_SUCCESS)
3931 status = smartcard_unpack_redir_scard_context_ref(log, s, pbContextNdrPtr,
3932 &call->Common.handles.hContext);
3933 if (status != SCARD_S_SUCCESS)
3938 status = smartcard_ndr_read_u(log, s, &call->Common.CardIdentifier);
3939 if (status != SCARD_S_SUCCESS)
3942 smartcard_trace_read_cache_a_call(log, call);
3943 return SCARD_S_SUCCESS;
3948 UINT32 mszNdrPtr = 0;
3949 UINT32 contextNdrPtr = 0;
3951 UINT32 pbContextNdrPtr = 0;
3954 wLog* log = scard_log();
3956 if (!smartcard_ndr_pointer_read(log, s, &index, &mszNdrPtr))
3957 return ERROR_INVALID_DATA;
3959 LONG status = smartcard_unpack_redir_scard_context(log, s, &(call->Common.handles.hContext),
3960 &index, &pbContextNdrPtr);
3961 if (status != SCARD_S_SUCCESS)
3964 if (!smartcard_ndr_pointer_read(log, s, &index, &contextNdrPtr))
3965 return ERROR_INVALID_DATA;
3967 if (!Stream_CheckAndLogRequiredLengthWLog(log, s, 12))
3968 return STATUS_BUFFER_TOO_SMALL;
3969 Stream_Read_UINT32(s, call->Common.FreshnessCounter);
3970 Stream_Read_INT32(s, call->Common.fPbDataIsNULL);
3971 Stream_Read_UINT32(s, call->Common.cbDataLen);
3973 call->szLookupName =
nullptr;
3976 status = smartcard_ndr_read_w(log, s, &call->szLookupName, NDR_PTR_FULL);
3977 if (status != SCARD_S_SUCCESS)
3981 status = smartcard_unpack_redir_scard_context_ref(log, s, pbContextNdrPtr,
3982 &call->Common.handles.hContext);
3983 if (status != SCARD_S_SUCCESS)
3988 status = smartcard_ndr_read_u(log, s, &call->Common.CardIdentifier);
3989 if (status != SCARD_S_SUCCESS)
3992 smartcard_trace_read_cache_w_call(log, call);
3993 return SCARD_S_SUCCESS;
3998 UINT32 mszNdrPtr = 0;
3999 UINT32 contextNdrPtr = 0;
4000 UINT32 pbDataNdrPtr = 0;
4002 UINT32 pbContextNdrPtr = 0;
4005 wLog* log = scard_log();
4007 if (!smartcard_ndr_pointer_read(log, s, &index, &mszNdrPtr))
4008 return ERROR_INVALID_DATA;
4010 LONG status = smartcard_unpack_redir_scard_context(log, s, &(call->Common.handles.hContext),
4011 &index, &pbContextNdrPtr);
4012 if (status != SCARD_S_SUCCESS)
4015 if (!smartcard_ndr_pointer_read(log, s, &index, &contextNdrPtr))
4016 return ERROR_INVALID_DATA;
4018 if (!Stream_CheckAndLogRequiredLengthWLog(log, s, 8))
4019 return STATUS_BUFFER_TOO_SMALL;
4021 Stream_Read_UINT32(s, call->Common.FreshnessCounter);
4022 const UINT32 cbDataLen = Stream_Get_UINT32(s);
4024 if (!smartcard_ndr_pointer_read(log, s, &index, &pbDataNdrPtr))
4025 return ERROR_INVALID_DATA;
4027 call->szLookupName =
nullptr;
4030 status = smartcard_ndr_read_a(log, s, &call->szLookupName, NDR_PTR_FULL);
4031 if (status != SCARD_S_SUCCESS)
4035 status = smartcard_unpack_redir_scard_context_ref(log, s, pbContextNdrPtr,
4036 &call->Common.handles.hContext);
4037 if (status != SCARD_S_SUCCESS)
4040 call->Common.CardIdentifier =
nullptr;
4043 status = smartcard_ndr_read_u(log, s, &call->Common.CardIdentifier);
4044 if (status != SCARD_S_SUCCESS)
4048 call->Common.pbData =
nullptr;
4051 status = smartcard_ndr_read(log, s, &call->Common.pbData, cbDataLen, 1, NDR_PTR_SIMPLE);
4052 if (status != SCARD_S_SUCCESS)
4054 call->Common.cbDataLen = cbDataLen;
4056 smartcard_trace_write_cache_a_call(log, call);
4057 return SCARD_S_SUCCESS;
4062 UINT32 mszNdrPtr = 0;
4063 UINT32 contextNdrPtr = 0;
4064 UINT32 pbDataNdrPtr = 0;
4066 UINT32 pbContextNdrPtr = 0;
4069 wLog* log = scard_log();
4071 if (!smartcard_ndr_pointer_read(log, s, &index, &mszNdrPtr))
4072 return ERROR_INVALID_DATA;
4074 LONG status = smartcard_unpack_redir_scard_context(log, s, &(call->Common.handles.hContext),
4075 &index, &pbContextNdrPtr);
4076 if (status != SCARD_S_SUCCESS)
4079 if (!smartcard_ndr_pointer_read(log, s, &index, &contextNdrPtr))
4080 return ERROR_INVALID_DATA;
4082 if (!Stream_CheckAndLogRequiredLengthWLog(log, s, 8))
4083 return STATUS_BUFFER_TOO_SMALL;
4084 Stream_Read_UINT32(s, call->Common.FreshnessCounter);
4085 const UINT32 cbDataLen = Stream_Get_UINT32(s);
4087 if (!smartcard_ndr_pointer_read(log, s, &index, &pbDataNdrPtr))
4088 return ERROR_INVALID_DATA;
4090 call->szLookupName =
nullptr;
4093 status = smartcard_ndr_read_w(log, s, &call->szLookupName, NDR_PTR_FULL);
4094 if (status != SCARD_S_SUCCESS)
4098 status = smartcard_unpack_redir_scard_context_ref(log, s, pbContextNdrPtr,
4099 &call->Common.handles.hContext);
4100 if (status != SCARD_S_SUCCESS)
4103 call->Common.CardIdentifier =
nullptr;
4106 status = smartcard_ndr_read_u(log, s, &call->Common.CardIdentifier);
4107 if (status != SCARD_S_SUCCESS)
4111 call->Common.pbData =
nullptr;
4114 status = smartcard_ndr_read(log, s, &call->Common.pbData, cbDataLen, 1, NDR_PTR_SIMPLE);
4115 if (status != SCARD_S_SUCCESS)
4117 call->Common.cbDataLen = cbDataLen;
4119 smartcard_trace_write_cache_w_call(log, call);
4126 wLog* log = scard_log();
4129 UINT32 pbContextNdrPtr = 0;
4131 LONG status = smartcard_unpack_redir_scard_context(log, s, &(call->handles.hContext), &index,
4133 if (status != SCARD_S_SUCCESS)
4136 status = smartcard_unpack_redir_scard_handle(log, s, &(call->handles.hCard), &index);
4137 if (status != SCARD_S_SUCCESS)
4140 status = smartcard_unpack_redir_scard_context_ref(log, s, pbContextNdrPtr,
4141 &(call->handles.hContext));
4142 if (status != SCARD_S_SUCCESS)
4144 WLog_Print(log, WLOG_ERROR,
4145 "smartcard_unpack_redir_scard_context_ref failed with error %" PRId32
"",
4150 status = smartcard_unpack_redir_scard_handle_ref(log, s, &(call->handles.hCard));
4151 if (status != SCARD_S_SUCCESS)
4152 WLog_Print(log, WLOG_ERROR,
4153 "smartcard_unpack_redir_scard_handle_ref failed with error %" PRId32
"", status);
4155 smartcard_trace_get_transmit_count_call(log, call);
4162 wLog* log = scard_log();
4163 return smartcard_unpack_common_context_and_string_w(log, s, &call->handles.hContext,
4164 &call->szReaderName);
4170 wLog* log = scard_log();
4171 return smartcard_unpack_common_context_and_string_a(log, s, &call->handles.hContext, &call->sz);
4177 wLog* log = scard_log();
4178 return smartcard_unpack_common_context_and_string_w(log, s, &call->handles.hContext, &call->sz);
4184 wLog* log = scard_log();
4185 return smartcard_unpack_common_context_and_string_w(log, s, &call->handles.hContext,
4186 &call->szReaderName);
4192 wLog* log = scard_log();
4193 smartcard_trace_device_type_id_return(log, ret);
4195 if (!Stream_EnsureRemainingCapacity(s, 4))
4197 WLog_Print(log, WLOG_ERROR,
"Stream_EnsureRemainingCapacity failed!");
4198 return SCARD_F_INTERNAL_ERROR;
4201 Stream_Write_UINT32(s, ret->dwDeviceId);
4203 return ret->ReturnCode;
4209 wLog* log = scard_log();
4212 DWORD cbDataLen = ret->cReaders;
4215 smartcard_trace_locate_cards_return(log, ret);
4216 if (ret->ReturnCode != SCARD_S_SUCCESS)
4218 if (cbDataLen == SCARD_AUTOALLOCATE)
4221 if (!Stream_EnsureRemainingCapacity(s, 4))
4223 WLog_Print(log, WLOG_ERROR,
"Stream_EnsureRemainingCapacity failed!");
4224 return SCARD_F_INTERNAL_ERROR;
4227 Stream_Write_UINT32(s, cbDataLen);
4228 if (!smartcard_ndr_pointer_write(s, &index, cbDataLen))
4229 return SCARD_E_NO_MEMORY;
4231 status = smartcard_ndr_write_state(s, ret->rgReaderStates, cbDataLen, NDR_PTR_SIMPLE);
4232 if (status != SCARD_S_SUCCESS)
4234 return ret->ReturnCode;
4240 wLog* log = scard_log();
4244 DWORD cbDataLen = ret->cbDataLen;
4245 smartcard_trace_get_reader_icon_return(log, ret);
4246 if (ret->ReturnCode != SCARD_S_SUCCESS)
4248 if (cbDataLen == SCARD_AUTOALLOCATE)
4251 if (!Stream_EnsureRemainingCapacity(s, 4))
4253 WLog_Print(log, WLOG_ERROR,
"Stream_EnsureRemainingCapacity failed!");
4254 return SCARD_F_INTERNAL_ERROR;
4257 Stream_Write_UINT32(s, cbDataLen);
4258 if (!smartcard_ndr_pointer_write(s, &index, cbDataLen))
4259 return SCARD_E_NO_MEMORY;
4261 status = smartcard_ndr_write(s, ret->pbData, cbDataLen, 1, NDR_PTR_SIMPLE);
4262 if (status != SCARD_S_SUCCESS)
4264 return ret->ReturnCode;
4270 wLog* log = scard_log();
4272 smartcard_trace_get_transmit_count_return(log, ret);
4274 if (!Stream_EnsureRemainingCapacity(s, 4))
4276 WLog_Print(log, WLOG_ERROR,
"Stream_EnsureRemainingCapacity failed!");
4277 return SCARD_F_INTERNAL_ERROR;
4280 Stream_Write_UINT32(s, ret->cTransmitCount);
4282 return ret->ReturnCode;
4288 wLog* log = scard_log();
4292 DWORD cbDataLen = ret->cbDataLen;
4293 smartcard_trace_read_cache_return(log, ret);
4294 if (ret->ReturnCode != SCARD_S_SUCCESS)
4297 if (cbDataLen == SCARD_AUTOALLOCATE)
4300 if (!Stream_EnsureRemainingCapacity(s, 4))
4302 WLog_Print(log, WLOG_ERROR,
"Stream_EnsureRemainingCapacity failed!");
4303 return SCARD_F_INTERNAL_ERROR;
4306 Stream_Write_UINT32(s, cbDataLen);
4307 if (!smartcard_ndr_pointer_write(s, &index, cbDataLen))
4308 return SCARD_E_NO_MEMORY;
4310 status = smartcard_ndr_write(s, ret->pbData, cbDataLen, 1, NDR_PTR_SIMPLE);
4311 if (status != SCARD_S_SUCCESS)
4313 return ret->ReturnCode;
4319 wLog* log = scard_log();
4322 smartcard_trace_context_call(log, call, name);
4324 LONG status = smartcard_pack_redir_scard_context(log, s, &call->handles.hContext, &index);
4325 if (status != SCARD_S_SUCCESS)
4328 return smartcard_pack_redir_scard_context_ref(log, s, &call->handles.hContext);
4334 wLog* log = scard_log();
4337 smartcard_trace_list_readers_call(log, call, unicode);
4339 LONG status = smartcard_pack_redir_scard_context(log, s, &call->handles.hContext, &index);
4340 if (status != SCARD_S_SUCCESS)
4343 if (!Stream_EnsureRemainingCapacity(s, 4))
4344 return SCARD_E_NO_MEMORY;
4346 Stream_Write_UINT32(s, call->cBytes);
4347 if (!smartcard_ndr_pointer_write(s, &index, call->cBytes))
4348 return SCARD_E_NO_MEMORY;
4350 if (!Stream_EnsureRemainingCapacity(s, 8))
4351 return SCARD_E_NO_MEMORY;
4353 Stream_Write_INT32(s, call->fmszReadersIsNULL);
4354 Stream_Write_UINT32(s, call->cchReaders);
4356 status = smartcard_pack_redir_scard_context_ref(log, s, &call->handles.hContext);
4357 if (status != SCARD_S_SUCCESS)
4360 if (call->cBytes > 0)
4362 status = smartcard_ndr_write(s, call->mszGroups, call->cBytes, 1, NDR_PTR_SIMPLE);
4363 if (status != SCARD_S_SUCCESS)
4367 return SCARD_S_SUCCESS;
4371 UINT32 cReaders, UINT32* ptrIndex)
4373 WINPR_ASSERT(rgReaderStates || (cReaders == 0));
4375 if (!Stream_EnsureRemainingCapacity(s, 4))
4376 return SCARD_E_NO_MEMORY;
4378 Stream_Write_UINT32(s, cReaders);
4380 for (UINT32 i = 0; i < cReaders; i++)
4384 if (!Stream_EnsureRemainingCapacity(s, 52))
4385 return SCARD_E_NO_MEMORY;
4387 const DWORD nameLen = state->szReader ? (DWORD)(strlen(state->szReader) + 1) : 0;
4388 if (!smartcard_ndr_pointer_write(s, ptrIndex, nameLen))
4389 return SCARD_E_NO_MEMORY;
4391 Stream_Write_UINT32(s, state->dwCurrentState);
4392 Stream_Write_UINT32(s, state->dwEventState);
4393 Stream_Write_UINT32(s, state->cbAtr);
4394 Stream_Write(s, state->rgbAtr, 36);
4397 for (UINT32 i = 0; i < cReaders; i++)
4401 if (state->szReader)
4403 const UINT32 nameLen = (UINT32)(strlen(state->szReader) + 1);
4404 LONG status = smartcard_ndr_write_a(s, state->szReader, nameLen, NDR_PTR_FULL);
4405 if (status != SCARD_S_SUCCESS)
4410 return SCARD_S_SUCCESS;
4414 UINT32 cReaders, UINT32* ptrIndex)
4416 WINPR_ASSERT(rgReaderStates || (cReaders == 0));
4418 if (!Stream_EnsureRemainingCapacity(s, 4))
4419 return SCARD_E_NO_MEMORY;
4421 Stream_Write_UINT32(s, cReaders);
4423 for (UINT32 i = 0; i < cReaders; i++)
4427 if (!Stream_EnsureRemainingCapacity(s, 52))
4428 return SCARD_E_NO_MEMORY;
4430 const DWORD nameLen = state->szReader ? (DWORD)(_wcslen(state->szReader) + 1) : 0;
4431 if (!smartcard_ndr_pointer_write(s, ptrIndex, nameLen))
4432 return SCARD_E_NO_MEMORY;
4434 Stream_Write_UINT32(s, state->dwCurrentState);
4435 Stream_Write_UINT32(s, state->dwEventState);
4436 Stream_Write_UINT32(s, state->cbAtr);
4437 Stream_Write(s, state->rgbAtr, 36);
4440 for (UINT32 i = 0; i < cReaders; i++)
4444 if (state->szReader)
4446 const UINT32 nameLen = (UINT32)(_wcslen(state->szReader) + 1);
4447 LONG status = smartcard_ndr_write_w(s, state->szReader, nameLen, NDR_PTR_FULL);
4448 if (status != SCARD_S_SUCCESS)
4453 return SCARD_S_SUCCESS;
4459 wLog* log = scard_log();
4462 smartcard_trace_get_status_change_a_call(log, call);
4464 LONG status = smartcard_pack_redir_scard_context(log, s, &call->handles.hContext, &index);
4465 if (status != SCARD_S_SUCCESS)
4468 if (!Stream_EnsureRemainingCapacity(s, 8))
4469 return SCARD_E_NO_MEMORY;
4471 Stream_Write_UINT32(s, call->dwTimeOut);
4472 Stream_Write_UINT32(s, call->cReaders);
4474 if (!smartcard_ndr_pointer_write(s, &index, call->cReaders))
4475 return SCARD_E_NO_MEMORY;
4477 status = smartcard_pack_redir_scard_context_ref(log, s, &call->handles.hContext);
4478 if (status != SCARD_S_SUCCESS)
4481 if (call->cReaders > 0)
4483 status = smartcard_pack_reader_state_a(s, call->rgReaderStates, call->cReaders, &index);
4484 if (status != SCARD_S_SUCCESS)
4488 return SCARD_S_SUCCESS;
4494 wLog* log = scard_log();
4497 smartcard_trace_get_status_change_w_call(log, call);
4499 LONG status = smartcard_pack_redir_scard_context(log, s, &call->handles.hContext, &index);
4500 if (status != SCARD_S_SUCCESS)
4503 if (!Stream_EnsureRemainingCapacity(s, 8))
4504 return SCARD_E_NO_MEMORY;
4506 Stream_Write_UINT32(s, call->dwTimeOut);
4507 Stream_Write_UINT32(s, call->cReaders);
4509 if (!smartcard_ndr_pointer_write(s, &index, call->cReaders))
4510 return SCARD_E_NO_MEMORY;
4512 status = smartcard_pack_redir_scard_context_ref(log, s, &call->handles.hContext);
4513 if (status != SCARD_S_SUCCESS)
4516 if (call->cReaders > 0)
4518 status = smartcard_pack_reader_state_w(s, call->rgReaderStates, call->cReaders, &index);
4519 if (status != SCARD_S_SUCCESS)
4523 return SCARD_S_SUCCESS;
4529 wLog* log = scard_log();
4532 smartcard_trace_connect_a_call(log, call);
4534 const DWORD readerLen = call->szReader ? (DWORD)(strlen(call->szReader) + 1) : 0;
4535 if (!smartcard_ndr_pointer_write(s, &index, readerLen))
4536 return SCARD_E_NO_MEMORY;
4539 smartcard_pack_redir_scard_context(log, s, &call->Common.handles.hContext, &index);
4540 if (status != SCARD_S_SUCCESS)
4543 if (!Stream_EnsureRemainingCapacity(s, 8))
4544 return SCARD_E_NO_MEMORY;
4546 Stream_Write_UINT32(s, call->Common.dwShareMode);
4547 Stream_Write_UINT32(s, call->Common.dwPreferredProtocols);
4551 status = smartcard_ndr_write_a(s, call->szReader, readerLen, NDR_PTR_FULL);
4552 if (status != SCARD_S_SUCCESS)
4556 return smartcard_pack_redir_scard_context_ref(log, s, &call->Common.handles.hContext);
4562 wLog* log = scard_log();
4565 smartcard_trace_connect_w_call(log, call);
4567 const DWORD readerLen = call->szReader ? (DWORD)(_wcslen(call->szReader) + 1) : 0;
4568 if (!smartcard_ndr_pointer_write(s, &index, readerLen))
4569 return SCARD_E_NO_MEMORY;
4572 smartcard_pack_redir_scard_context(log, s, &call->Common.handles.hContext, &index);
4573 if (status != SCARD_S_SUCCESS)
4576 if (!Stream_EnsureRemainingCapacity(s, 8))
4577 return SCARD_E_NO_MEMORY;
4579 Stream_Write_UINT32(s, call->Common.dwShareMode);
4580 Stream_Write_UINT32(s, call->Common.dwPreferredProtocols);
4584 status = smartcard_ndr_write_w(s, call->szReader, readerLen, NDR_PTR_FULL);
4585 if (status != SCARD_S_SUCCESS)
4589 return smartcard_pack_redir_scard_context_ref(log, s, &call->Common.handles.hContext);
4595 wLog* log = scard_log();
4598 smartcard_trace_control_call(log, call);
4600 LONG status = smartcard_pack_redir_scard_context(log, s, &call->handles.hContext, &index);
4601 if (status != SCARD_S_SUCCESS)
4604 status = smartcard_pack_redir_scard_handle(log, s, &call->handles.hCard, &index);
4605 if (status != SCARD_S_SUCCESS)
4608 if (!Stream_EnsureRemainingCapacity(s, 8))
4609 return SCARD_E_NO_MEMORY;
4611 Stream_Write_UINT32(s, call->dwControlCode);
4612 Stream_Write_UINT32(s, call->cbInBufferSize);
4613 if (!smartcard_ndr_pointer_write(s, &index, call->cbInBufferSize))
4614 return SCARD_E_NO_MEMORY;
4616 Stream_Write_INT32(s, call->fpvOutBufferIsNULL);
4617 Stream_Write_UINT32(s, call->cbOutBufferSize);
4619 status = smartcard_pack_redir_scard_context_ref(log, s, &call->handles.hContext);
4620 if (status != SCARD_S_SUCCESS)
4623 status = smartcard_pack_redir_scard_handle_ref(log, s, &call->handles.hCard);
4624 if (status != SCARD_S_SUCCESS)
4627 if (call->cbInBufferSize)
4629 status = smartcard_ndr_write(s, call->pvInBuffer, call->cbInBufferSize, 1, NDR_PTR_SIMPLE);
4630 if (status != SCARD_S_SUCCESS)
4641 wLog* log = scard_log();
4644 smartcard_trace_hcard_and_disposition_call(log, call, name);
4646 LONG status = smartcard_pack_redir_scard_context(log, s, &call->handles.hContext, &index);
4647 if (status != SCARD_S_SUCCESS)
4650 status = smartcard_pack_redir_scard_handle(log, s, &call->handles.hCard, &index);
4651 if (status != SCARD_S_SUCCESS)
4654 if (!Stream_EnsureRemainingCapacity(s, 4))
4655 return SCARD_E_NO_MEMORY;
4657 Stream_Write_UINT32(s, call->dwDisposition);
4659 status = smartcard_pack_redir_scard_context_ref(log, s, &call->handles.hContext);
4660 if (status != SCARD_S_SUCCESS)
4663 return smartcard_pack_redir_scard_handle_ref(log, s, &call->handles.hCard);
4669 wLog* log = scard_log();
4672 smartcard_trace_transmit_call(log, call);
4674 LONG status = smartcard_pack_redir_scard_context(log, s, &call->handles.hContext, &index);
4675 if (status != SCARD_S_SUCCESS)
4678 status = smartcard_pack_redir_scard_handle(log, s, &call->handles.hCard, &index);
4679 if (status != SCARD_S_SUCCESS)
4682 UINT32 cbExtraBytes = 0;
4683 const BYTE* pbExtraBytes =
nullptr;
4684 if (call->pioSendPci)
4686 cbExtraBytes = (UINT32)(call->pioSendPci->cbPciLength -
sizeof(
SCARD_IO_REQUEST));
4687 if (cbExtraBytes > 0)
4691 if (!Stream_EnsureRemainingCapacity(s, 32))
4692 return SCARD_E_NO_MEMORY;
4694 Stream_Write_UINT32(s, call->pioSendPci ? call->pioSendPci->dwProtocol : 0);
4695 Stream_Write_UINT32(s, cbExtraBytes);
4696 if (!smartcard_ndr_pointer_write(s, &index, cbExtraBytes))
4697 return SCARD_E_NO_MEMORY;
4699 Stream_Write_UINT32(s, call->cbSendLength);
4700 if (!smartcard_ndr_pointer_write(s, &index, call->cbSendLength))
4701 return SCARD_E_NO_MEMORY;
4703 const UINT32 recvPciLen = call->pioRecvPci ? (UINT32)call->pioRecvPci->cbPciLength : 0;
4704 if (!smartcard_ndr_pointer_write(s, &index, recvPciLen))
4705 return SCARD_E_NO_MEMORY;
4707 Stream_Write_INT32(s, call->fpbRecvBufferIsNULL);
4708 Stream_Write_UINT32(s, call->cbRecvLength);
4710 status = smartcard_pack_redir_scard_context_ref(log, s, &call->handles.hContext);
4711 if (status != SCARD_S_SUCCESS)
4714 status = smartcard_pack_redir_scard_handle_ref(log, s, &call->handles.hCard);
4715 if (status != SCARD_S_SUCCESS)
4718 if (cbExtraBytes > 0)
4720 status = smartcard_ndr_write(s, pbExtraBytes, cbExtraBytes, 1, NDR_PTR_SIMPLE);
4721 if (status != SCARD_S_SUCCESS)
4725 if (call->cbSendLength > 0)
4727 status = smartcard_ndr_write(s, call->pbSendBuffer, call->cbSendLength, 1, NDR_PTR_SIMPLE);
4728 if (status != SCARD_S_SUCCESS)
4732 if (call->pioRecvPci && recvPciLen > 0)
4734 UINT32 cbRecvExtra = (UINT32)(call->pioRecvPci->cbPciLength -
sizeof(
SCARD_IO_REQUEST));
4735 const BYTE* pbRecvExtra = &((
const BYTE*)call->pioRecvPci)[
sizeof(
SCARD_IO_REQUEST)];
4737 if (!Stream_EnsureRemainingCapacity(s, 12))
4738 return SCARD_E_NO_MEMORY;
4740 Stream_Write_UINT32(s, call->pioRecvPci->dwProtocol);
4741 Stream_Write_UINT32(s, cbRecvExtra);
4742 if (!smartcard_ndr_pointer_write(s, &index, cbRecvExtra))
4743 return SCARD_E_NO_MEMORY;
4745 if (cbRecvExtra > 0)
4747 status = smartcard_ndr_write(s, pbRecvExtra, cbRecvExtra, 1, NDR_PTR_SIMPLE);
4748 if (status != SCARD_S_SUCCESS)
4753 return SCARD_S_SUCCESS;
4759 wLog* log = scard_log();
4762 smartcard_trace_get_attrib_call(log, call);
4764 LONG status = smartcard_pack_redir_scard_context(log, s, &call->handles.hContext, &index);
4765 if (status != SCARD_S_SUCCESS)
4768 status = smartcard_pack_redir_scard_handle(log, s, &call->handles.hCard, &index);
4769 if (status != SCARD_S_SUCCESS)
4772 if (!Stream_EnsureRemainingCapacity(s, 12))
4773 return STATUS_NO_MEMORY;
4775 Stream_Write_UINT32(s, call->dwAttrId);
4776 Stream_Write_INT32(s, call->fpbAttrIsNULL);
4777 Stream_Write_UINT32(s, call->cbAttrLen);
4779 status = smartcard_pack_redir_scard_context_ref(log, s, &call->handles.hContext);
4780 if (status != SCARD_S_SUCCESS)
4783 return smartcard_pack_redir_scard_handle_ref(log, s, &call->handles.hCard);
4789 wLog* log = scard_log();
4791 UINT32 mszNdrPtr = 0;
4793 if (!Stream_CheckAndLogRequiredLengthWLog(log, s, 4))
4794 return STATUS_BUFFER_TOO_SMALL;
4796 const UINT32 cBytes = Stream_Get_UINT32(s);
4798 if (!smartcard_ndr_pointer_read(log, s, &index, &mszNdrPtr))
4799 return ERROR_INVALID_DATA;
4803 LONG status = smartcard_ndr_read(log, s, &ret->msz, cBytes, 1, NDR_PTR_SIMPLE);
4804 if (status != SCARD_S_SUCCESS)
4806 ret->cBytes = cBytes;
4809 smartcard_trace_list_readers_return(log, ret, unicode);
4810 return SCARD_S_SUCCESS;
4817 wLog* log = scard_log();
4821 if (!Stream_CheckAndLogRequiredLengthWLog(log, s, 4))
4822 return STATUS_BUFFER_TOO_SMALL;
4824 Stream_Read_UINT32(s, ret->cReaders);
4826 if (!smartcard_ndr_pointer_read(log, s, &index, &ndrPtr))
4827 return ERROR_INVALID_DATA;
4829 if (ndrPtr && ret->cReaders > 0)
4832 smartcard_ndr_read_state(log, s, &ret->rgReaderStates, ret->cReaders, NDR_PTR_SIMPLE);
4833 if (status != SCARD_S_SUCCESS)
4837 ret->rgReaderStates =
nullptr;
4839 smartcard_trace_get_status_change_return(log, ret, unicode);
4840 return SCARD_S_SUCCESS;
4846 wLog* log = scard_log();
4848 UINT32 pbContextNdrPtr = 0;
4851 smartcard_unpack_redir_scard_context(log, s, &ret->hContext, &index, &pbContextNdrPtr);
4852 if (status != SCARD_S_SUCCESS)
4855 status = smartcard_unpack_redir_scard_handle(log, s, &ret->hCard, &index);
4856 if (status != SCARD_S_SUCCESS)
4859 if (!Stream_CheckAndLogRequiredLengthWLog(log, s, 4))
4860 return STATUS_BUFFER_TOO_SMALL;
4862 Stream_Read_UINT32(s, ret->dwActiveProtocol);
4864 status = smartcard_unpack_redir_scard_context_ref(log, s, pbContextNdrPtr, &ret->hContext);
4865 if (status != SCARD_S_SUCCESS)
4868 status = smartcard_unpack_redir_scard_handle_ref(log, s, &ret->hCard);
4869 if (status != SCARD_S_SUCCESS)
4872 smartcard_trace_connect_return(log, ret);
4873 return SCARD_S_SUCCESS;
4879 wLog* log = scard_log();
4881 UINT32 pvOutBufferNdrPtr = 0;
4883 if (!Stream_CheckAndLogRequiredLengthWLog(log, s, 8))
4884 return STATUS_BUFFER_TOO_SMALL;
4886 const UINT32 cbOutBufferSize = Stream_Get_UINT32(s);
4888 if (!smartcard_ndr_pointer_read(log, s, &index, &pvOutBufferNdrPtr))
4889 return ERROR_INVALID_DATA;
4891 if (pvOutBufferNdrPtr && (cbOutBufferSize > 0))
4894 smartcard_ndr_read(log, s, &ret->pvOutBuffer, cbOutBufferSize, 1, NDR_PTR_SIMPLE);
4895 if (status != SCARD_S_SUCCESS)
4897 ret->cbOutBufferSize = cbOutBufferSize;
4899 smartcard_trace_control_return(log, ret);
4900 return SCARD_S_SUCCESS;
4906 wLog* log = scard_log();
4908 UINT32 recvPciNdrPtr = 0;
4909 UINT32 recvBufferNdrPtr = 0;
4911 if (!smartcard_ndr_pointer_read(log, s, &index, &recvPciNdrPtr))
4912 return ERROR_INVALID_DATA;
4914 if (!Stream_CheckAndLogRequiredLengthWLog(log, s, 4))
4915 return STATUS_BUFFER_TOO_SMALL;
4917 const UINT32 cbRecvLength = Stream_Get_UINT32(s);
4919 if (!smartcard_ndr_pointer_read(log, s, &index, &recvBufferNdrPtr))
4920 return ERROR_INVALID_DATA;
4924 if (!Stream_CheckAndLogRequiredLengthWLog(log, s, 8))
4925 return STATUS_BUFFER_TOO_SMALL;
4927 UINT32 dwProtocol = 0;
4928 UINT32 cbExtraBytes = 0;
4929 Stream_Read_UINT32(s, dwProtocol);
4930 Stream_Read_UINT32(s, cbExtraBytes);
4932 UINT32 pbExtraBytesNdrPtr = 0;
4933 if (!smartcard_ndr_pointer_read(log, s, &index, &pbExtraBytesNdrPtr))
4934 return ERROR_INVALID_DATA;
4936 if (pbExtraBytesNdrPtr)
4938 if (!Stream_CheckAndLogRequiredLengthWLog(log, s, 4))
4939 return STATUS_BUFFER_TOO_SMALL;
4942 Stream_Read_UINT32(s, length);
4943 if (length != cbExtraBytes)
4945 WLog_Print(log, WLOG_WARN,
4946 "Transmit_Return unexpected length: Actual: %" PRIu32
4947 ", Expected: %" PRIu32
" (pioRecvPci.cbExtraBytes)",
4948 length, cbExtraBytes);
4949 return STATUS_INVALID_PARAMETER;
4952 if (!Stream_CheckAndLogRequiredLengthWLog(log, s, cbExtraBytes))
4953 return STATUS_BUFFER_TOO_SMALL;
4956 if (!ret->pioRecvPci)
4957 return SCARD_E_NO_MEMORY;
4959 ret->pioRecvPci->dwProtocol = dwProtocol;
4960 ret->pioRecvPci->cbPciLength = (DWORD)(
sizeof(
SCARD_IO_REQUEST) + cbExtraBytes);
4963 Stream_Read(s, pbExtraBytes, length);
4964 if (smartcard_unpack_read_size_align(s, cbExtraBytes, 4) < 0)
4965 return STATUS_INVALID_PARAMETER;
4970 if (!ret->pioRecvPci)
4971 return SCARD_E_NO_MEMORY;
4973 ret->pioRecvPci->dwProtocol = dwProtocol;
4978 if (recvBufferNdrPtr && (cbRecvLength > 0))
4981 smartcard_ndr_read(log, s, &ret->pbRecvBuffer, cbRecvLength, 1, NDR_PTR_SIMPLE);
4982 if (status != SCARD_S_SUCCESS)
4984 ret->cbRecvLength = cbRecvLength;
4987 smartcard_trace_transmit_return(log, ret);
4988 return SCARD_S_SUCCESS;
4994 wLog* log = scard_log();
4996 UINT32 pbAttrPtr = 0;
4998 if (!Stream_CheckAndLogRequiredLengthWLog(log, s, 4))
4999 return STATUS_BUFFER_TOO_SMALL;
5001 const UINT32 cbAttrLen = Stream_Get_UINT32(s);
5003 if (!smartcard_ndr_pointer_read(log, s, &index, &pbAttrPtr))
5004 return ERROR_INVALID_DATA;
5006 LONG status = smartcard_ndr_read(log, s, &ret->pbAttr, cbAttrLen, 1, NDR_PTR_SIMPLE);
5007 if (status != SCARD_S_SUCCESS)
5009 ret->cbAttrLen = cbAttrLen;
5011 smartcard_trace_get_attrib_return(log, ret, 0);
5012 return SCARD_S_SUCCESS;