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,
const 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,
const size_t min,
115 const 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;
212 return STATUS_SUCCESS;
215static LONG smartcard_ndr_read(wLog* log,
wStream* s, BYTE** data,
const size_t min,
216 const size_t elementSize,
const ndr_ptr_t type)
218 return smartcard_ndr_read_ex(log, s, data, min, elementSize, type,
nullptr);
221static BOOL smartcard_ndr_pointer_write(
wStream* s, UINT32* index,
const DWORD length)
223 const UINT32 ndrPtr = 0x20000 + (*index) * 4;
227 if (!Stream_EnsureRemainingCapacity(s, 4))
232 Stream_Write_UINT32(s, ndrPtr);
233 (*index) = (*index) + 1;
236 Stream_Write_UINT32(s, 0);
240static LONG smartcard_ndr_write(
wStream* s,
const BYTE* data,
const UINT32 size,
241 const UINT32 elementSize,
const ndr_ptr_t type)
243 const UINT32 offset = 0;
244 const UINT32 len = size;
245 const UINT32 dataLen = size * elementSize;
249 return SCARD_S_SUCCESS;
263 return SCARD_E_INVALID_PARAMETER;
266 if (!Stream_EnsureRemainingCapacity(s, required + dataLen + 4))
267 return STATUS_BUFFER_TOO_SMALL;
272 Stream_Write_UINT32(s, len);
273 Stream_Write_UINT32(s, offset);
274 Stream_Write_UINT32(s, len);
277 Stream_Write_UINT32(s, len);
282 return SCARD_E_INVALID_PARAMETER;
286 Stream_Write(s, data, dataLen);
288 Stream_Zero(s, dataLen);
289 return smartcard_pack_write_size_align(s, dataLen, 4);
293 const ndr_ptr_t type)
301 WINPR_ASSERT(data || (size == 0));
307 const size_t min,
const ndr_ptr_t type)
319 const size_t min,
const ndr_ptr_t type)
327 const LONG status = smartcard_ndr_read(log, s, u.ppv, min,
sizeof(
LocateCards_ATRMask), type);
328 if (status != SCARD_S_SUCCESS)
335 for (
size_t x = 0; x < min; x++)
337 const UINT32 val = winpr_Data_Get_UINT32(&masks[x].cbAtr);
338 if (val > ARRAYSIZE(masks[x].rgbAtr))
340 WLog_Print(log, WLOG_ERROR,
341 "LocateCards_ATRMask[%" PRIuz
"]::cbAtr %" PRIu32
" exceeds %" PRIuz, x, val,
342 (
size_t)ARRAYSIZE(masks[x].rgbAtr));
345 return STATUS_DATA_ERROR;
348 return SCARD_S_SUCCESS;
351static LONG smartcard_ndr_read_fixed_string_a(wLog* log,
wStream* s, CHAR** data,
const size_t min,
352 const ndr_ptr_t type)
360 return smartcard_ndr_read(log, s, u.ppv, min,
sizeof(CHAR), type);
363static LONG smartcard_ndr_read_fixed_string_w(wLog* log,
wStream* s, WCHAR** data,
const size_t min,
364 const ndr_ptr_t type)
372 return smartcard_ndr_read(log, s, u.ppv, min,
sizeof(WCHAR), type);
375static LONG smartcard_ndr_read_a(wLog* log,
wStream* s, CHAR** data,
const ndr_ptr_t type)
383 return smartcard_ndr_read(log, s, u.ppv, 0,
sizeof(CHAR), type);
386static LONG smartcard_ndr_read_w(wLog* log,
wStream* s, WCHAR** data,
const ndr_ptr_t type)
394 return smartcard_ndr_read(log, s, u.ppv, 0,
sizeof(WCHAR), type);
397static LONG smartcard_ndr_write_w(
wStream* s,
const WCHAR* data,
const UINT32 size,
398 const ndr_ptr_t type)
406 return smartcard_ndr_write(s, cnv.bp, size,
sizeof(WCHAR), type);
409static LONG smartcard_ndr_write_a(
wStream* s,
const CHAR* data,
const UINT32 size,
410 const ndr_ptr_t type)
418 return smartcard_ndr_write(s, cnv.bp, size,
sizeof(CHAR), type);
421static LONG smartcard_ndr_read_u(wLog* log,
wStream* s,
UUID** data)
429 return smartcard_ndr_read(log, s, u.ppv, 1,
sizeof(
UUID), NDR_PTR_FIXED);
432static char* smartcard_convert_string_list(
const void* in,
const size_t bytes,
const BOOL unicode)
441 char* mszA =
nullptr;
453 mszA = ConvertMszWCharNToUtf8Alloc(
string.wz, bytes /
sizeof(WCHAR), &length);
459 mszA = (
char*)calloc(bytes,
sizeof(
char));
462 CopyMemory(mszA,
string.sz, bytes - 1);
471 for (
size_t index = 0; index < length - 1; index++)
473 if (mszA[index] ==
'\0')
480WINPR_ATTR_MALLOC(free, 1)
481static
char* smartcard_create_msz_dump(const
char* msz,
size_t len)
483 size_t bufferLen = len;
484 char* buffer = calloc(len + 1, 1);
489 const char* cur = msz;
491 while ((len > 0) && cur && cur[0] !=
'\0' && (bufferLen > 0))
493 size_t clen = strnlen(cur, len);
494 int rc = _snprintf(buf, bufferLen,
"%s", cur);
495 bufferLen -= (size_t)rc;
504static void smartcard_msz_dump(wLog* log,
const DWORD level,
const char* prefix,
const void* data,
505 const size_t len,
const BOOL wchar)
507 if (!WLog_IsLevelActive(log, level))
511 const char* msz = WINPR_CXX_COMPAT_CAST(
const char*, data);
515 tmp = ConvertMszWCharNToUtf8Alloc(data, len, &mszlen);
519 char* array = smartcard_create_msz_dump(msz, mszlen);
520 WLog_Print(log, level,
"%s%s", prefix, array);
525WINPR_ATTR_MALLOC(free, 1)
526static
char* smartcard_create_array_dump(const
void* pd,
size_t len)
528 const BYTE* data = pd;
531 size_t bufferLen = len * 4;
534 char* buffer = calloc(bufferLen + 1, 1);
537 char* start = buffer;
539 WINPR_ASSERT(buffer || (bufferLen == 0));
541 if (!data && (len > 0))
543 (void)_snprintf(buffer, bufferLen,
"{ nullptr [%" PRIuz
"] }", len);
547 rc = _snprintf(buffer, bufferLen,
"{ ");
548 if ((rc < 0) || ((
size_t)rc >= bufferLen))
551 bufferLen -= (size_t)rc;
553 for (
size_t x = 0; x < len; x++)
555 rc = _snprintf(buffer, bufferLen,
"%02X", data[x]);
556 if ((rc < 0) || ((
size_t)rc >= bufferLen))
559 bufferLen -= (size_t)rc;
562 rc = _snprintf(buffer, bufferLen,
" }");
563 if ((rc < 0) || ((
size_t)rc >= bufferLen))
570WINPR_ATTR_FORMAT_ARG(3, 7)
571static
void smartcard_dump_array(wLog* log, DWORD level, WINPR_FORMAT_ARG const
char* prefix,
572 const
char* postfix, const
void* data,
size_t len, ...)
574 if (!WLog_IsLevelActive(log, level))
577 char* buffer = smartcard_create_array_dump(data, len);
579 char* fprefix =
nullptr;
581 va_list ap = WINPR_C_ARRAY_INIT;
583 winpr_vasprintf(&fprefix, &flen, prefix, ap);
585 WLog_Print(log, level,
"%s%s%s", fprefix, buffer, postfix);
590static void smartcard_log_redir_handle(wLog* log,
const REDIR_SCARDHANDLE* pHandle)
592 WINPR_ASSERT(pHandle);
593 smartcard_dump_array(log, g_LogLevel,
"handle: ",
"", pHandle->pbHandle, pHandle->cbHandle);
598 WINPR_ASSERT(phContext);
599 smartcard_dump_array(log, g_LogLevel,
"hContext: ",
"", phContext->pbContext,
600 phContext->cbContext);
603static void smartcard_trace_context_and_string_call_a(wLog* log,
const char* name,
607 if (!WLog_IsLevelActive(log, g_LogLevel))
610 WLog_Print(log, g_LogLevel,
"%s {", name);
611 smartcard_log_context(log, phContext);
612 WLog_Print(log, g_LogLevel,
" sz=%s", sz);
614 WLog_Print(log, g_LogLevel,
"}");
617static void smartcard_trace_context_and_string_call_w(wLog* log,
const char* name,
623 if (!WLog_IsLevelActive(log, g_LogLevel))
627 tmp = ConvertWCharToUtf8Alloc(sz,
nullptr);
629 WLog_Print(log, g_LogLevel,
"%s {", name);
630 smartcard_log_context(log, phContext);
631 WLog_Print(log, g_LogLevel,
" sz=%s", tmp);
632 WLog_Print(log, g_LogLevel,
"}");
636static void smartcard_trace_context_call(wLog* log,
const Context_Call* call,
const char* name)
640 if (!WLog_IsLevelActive(log, g_LogLevel))
643 WLog_Print(log, g_LogLevel,
"%s_Call {", name);
644 smartcard_log_context(log, &call->handles.hContext);
646 WLog_Print(log, g_LogLevel,
"}");
654 if (!WLog_IsLevelActive(log, g_LogLevel))
657 WLog_Print(log, g_LogLevel,
"ListReaderGroups%s_Call {", unicode ?
"W" :
"A");
658 smartcard_log_context(log, &call->handles.hContext);
660 WLog_Print(log, g_LogLevel,
"fmszGroupsIsNULL: %" PRId32
" cchGroups: 0x%08" PRIx32,
661 call->fmszGroupsIsNULL, call->cchGroups);
662 WLog_Print(log, g_LogLevel,
"}");
665static void dump_reader_states_return(wLog* log,
const ReaderState_Return* rgReaderStates,
666 const UINT32 cReaders)
668 WINPR_ASSERT(rgReaderStates || (cReaders == 0));
669 for (UINT32 index = 0; index < cReaders; index++)
672 char* szCurrentState = SCardGetReaderStateString(readerState->dwCurrentState);
673 char* szEventState = SCardGetReaderStateString(readerState->dwEventState);
674 WLog_Print(log, g_LogLevel,
"\t[%" PRIu32
"]: dwCurrentState: %s (0x%08" PRIX32
")", index,
675 szCurrentState, readerState->dwCurrentState);
676 WLog_Print(log, g_LogLevel,
"\t[%" PRIu32
"]: dwEventState: %s (0x%08" PRIX32
")", index,
677 szEventState, readerState->dwEventState);
678 free(szCurrentState);
681 smartcard_dump_array(log, g_LogLevel,
"\t[%" PRIu32
"]: cbAttr: %" PRIu32
" { ",
" }",
682 readerState->rgbAtr, readerState->cbAtr, index, readerState->cbAtr);
687 const UINT32 cReaders)
689 WINPR_ASSERT(rgReaderStates || (cReaders == 0));
690 for (UINT32 index = 0; index < cReaders; index++)
694 WLog_Print(log, g_LogLevel,
"\t[%" PRIu32
"]: szReader: %s cbAtr: %" PRIu32
"", index,
695 readerState->szReader, readerState->cbAtr);
696 char* szCurrentState = SCardGetReaderStateString(readerState->dwCurrentState);
697 char* szEventState = SCardGetReaderStateString(readerState->dwEventState);
698 WLog_Print(log, g_LogLevel,
"\t[%" PRIu32
"]: dwCurrentState: %s (0x%08" PRIX32
")", index,
699 szCurrentState, readerState->dwCurrentState);
700 WLog_Print(log, g_LogLevel,
"\t[%" PRIu32
"]: dwEventState: %s (0x%08" PRIX32
")", index,
701 szEventState, readerState->dwEventState);
702 free(szCurrentState);
705 smartcard_dump_array(log, g_LogLevel,
"\t[%" PRIu32
"]: cbAttr: %" PRIu32
" { ",
" }",
706 readerState->rgbAtr, readerState->cbAtr, index, readerState->cbAtr);
711 const UINT32 cReaders)
713 WINPR_ASSERT(rgReaderStates || (cReaders == 0));
714 for (UINT32 index = 0; index < cReaders; index++)
717 char* buffer = ConvertWCharToUtf8Alloc(readerState->szReader,
nullptr);
718 WLog_Print(log, g_LogLevel,
"\t[%" PRIu32
"]: szReader: %s cbAtr: %" PRIu32
"", index,
719 buffer, readerState->cbAtr);
721 char* szCurrentState = SCardGetReaderStateString(readerState->dwCurrentState);
722 char* szEventState = SCardGetReaderStateString(readerState->dwEventState);
723 WLog_Print(log, g_LogLevel,
"\t[%" PRIu32
"]: dwCurrentState: %s (0x%08" PRIX32
")", index,
724 szCurrentState, readerState->dwCurrentState);
725 WLog_Print(log, g_LogLevel,
"\t[%" PRIu32
"]: dwEventState: %s (0x%08" PRIX32
")", index,
726 szEventState, readerState->dwEventState);
727 free(szCurrentState);
730 smartcard_dump_array(log, g_LogLevel,
"\t[%" PRIu32
"]: cbAttr: %" PRIu32
" { ",
" }",
731 readerState->rgbAtr, readerState->cbAtr, index, readerState->cbAtr);
739 if (!WLog_IsLevelActive(log, g_LogLevel))
742 WLog_Print(log, g_LogLevel,
"GetStatusChangeW_Call {");
743 smartcard_log_context(log, &call->handles.hContext);
745 WLog_Print(log, g_LogLevel,
"dwTimeOut: 0x%08" PRIX32
" cReaders: %" PRIu32
"", call->dwTimeOut,
748 dump_reader_states_w(log, call->rgReaderStates, call->cReaders);
750 WLog_Print(log, g_LogLevel,
"}");
758 if (!WLog_IsLevelActive(log, g_LogLevel))
761 char* mszA = smartcard_convert_string_list(ret->msz, ret->cBytes, unicode);
763 WLog_Print(log, g_LogLevel,
"ListReaderGroups%s_Return {", unicode ?
"W" :
"A");
764 WLog_Print(log, g_LogLevel,
" ReturnCode: %s (0x%08" PRIx32
")",
765 SCardGetErrorString(ret->ReturnCode),
766 WINPR_CXX_COMPAT_CAST(UINT32, ret->ReturnCode));
767 WLog_Print(log, g_LogLevel,
" cBytes: %" PRIu32
" msz: %s", ret->cBytes, mszA);
768 WLog_Print(log, g_LogLevel,
"}");
772static void smartcard_trace_list_readers_call(wLog* log,
const ListReaders_Call* call,
777 if (!WLog_IsLevelActive(log, g_LogLevel))
780 char* mszGroupsA = smartcard_convert_string_list(call->mszGroups, call->cBytes, unicode);
782 WLog_Print(log, g_LogLevel,
"ListReaders%s_Call {", unicode ?
"W" :
"A");
783 smartcard_log_context(log, &call->handles.hContext);
785 WLog_Print(log, g_LogLevel,
786 "cBytes: %" PRIu32
" mszGroups: %s fmszReadersIsNULL: %" PRId32
787 " cchReaders: 0x%08" PRIX32
"",
788 call->cBytes, mszGroupsA, call->fmszReadersIsNULL, call->cchReaders);
789 WLog_Print(log, g_LogLevel,
"}");
794static void smartcard_trace_locate_cards_by_atr_a_call(wLog* log,
799 if (!WLog_IsLevelActive(log, g_LogLevel))
802 WLog_Print(log, g_LogLevel,
"LocateCardsByATRA_Call {");
803 smartcard_log_context(log, &call->handles.hContext);
805 dump_reader_states_a(log, call->rgReaderStates, call->cReaders);
807 WLog_Print(log, g_LogLevel,
"}");
810static void smartcard_trace_locate_cards_a_call(wLog* log,
const LocateCardsA_Call* call)
814 if (!WLog_IsLevelActive(log, g_LogLevel))
817 WLog_Print(log, g_LogLevel,
"LocateCardsA_Call {");
818 smartcard_log_context(log, &call->handles.hContext);
819 WLog_Print(log, g_LogLevel,
" cBytes=%" PRIu32, call->cBytes);
820 smartcard_msz_dump(log, g_LogLevel,
" mszCards=", call->mszCards, call->cBytes, FALSE);
821 WLog_Print(log, g_LogLevel,
" cReaders=%" PRIu32, call->cReaders);
822 dump_reader_states_a(log, call->rgReaderStates, call->cReaders);
824 WLog_Print(log, g_LogLevel,
"}");
827static void smartcard_trace_locate_cards_return(wLog* log,
const LocateCards_Return* ret)
831 if (!WLog_IsLevelActive(log, g_LogLevel))
834 WLog_Print(log, g_LogLevel,
"LocateCards_Return {");
835 WLog_Print(log, g_LogLevel,
" ReturnCode: %s (0x%08" PRIX32
")",
836 SCardGetErrorString(ret->ReturnCode),
837 WINPR_CXX_COMPAT_CAST(UINT32, ret->ReturnCode));
839 if (ret->ReturnCode == SCARD_S_SUCCESS)
841 WLog_Print(log, g_LogLevel,
" cReaders=%" PRIu32, ret->cReaders);
843 WLog_Print(log, g_LogLevel,
"}");
850 if (!WLog_IsLevelActive(log, g_LogLevel))
853 WLog_Print(log, g_LogLevel,
"GetReaderIcon_Return {");
854 WLog_Print(log, g_LogLevel,
" ReturnCode: %s (0x%08" PRIX32
")",
855 SCardGetErrorString(ret->ReturnCode),
856 WINPR_CXX_COMPAT_CAST(UINT32, ret->ReturnCode));
858 if (ret->ReturnCode == SCARD_S_SUCCESS)
860 WLog_Print(log, g_LogLevel,
" cbDataLen=%" PRIu32, ret->cbDataLen);
862 WLog_Print(log, g_LogLevel,
"}");
869 if (!WLog_IsLevelActive(log, g_LogLevel))
872 WLog_Print(log, g_LogLevel,
"GetTransmitCount_Return {");
873 WLog_Print(log, g_LogLevel,
" ReturnCode: %s (0x%08" PRIX32
")",
874 SCardGetErrorString(ret->ReturnCode),
875 WINPR_CXX_COMPAT_CAST(UINT32, ret->ReturnCode));
877 WLog_Print(log, g_LogLevel,
" cTransmitCount=%" PRIu32, ret->cTransmitCount);
878 WLog_Print(log, g_LogLevel,
"}");
881static void smartcard_trace_read_cache_return(wLog* log,
const ReadCache_Return* ret)
885 if (!WLog_IsLevelActive(log, g_LogLevel))
888 WLog_Print(log, g_LogLevel,
"ReadCache_Return {");
889 WLog_Print(log, g_LogLevel,
" ReturnCode: %s (0x%08" PRIX32
")",
890 SCardGetErrorString(ret->ReturnCode),
891 WINPR_CXX_COMPAT_CAST(UINT32, ret->ReturnCode));
893 if (ret->ReturnCode == SCARD_S_SUCCESS)
895 WLog_Print(log, g_LogLevel,
" cbDataLen=%" PRIu32, ret->cbDataLen);
896 smartcard_dump_array(log, g_LogLevel,
" cbData: ",
"", ret->pbData, ret->cbDataLen);
898 WLog_Print(log, g_LogLevel,
"}");
901static void smartcard_trace_locate_cards_w_call(wLog* log,
const LocateCardsW_Call* call)
905 if (!WLog_IsLevelActive(log, g_LogLevel))
908 WLog_Print(log, g_LogLevel,
"LocateCardsW_Call {");
909 smartcard_log_context(log, &call->handles.hContext);
910 WLog_Print(log, g_LogLevel,
" cBytes=%" PRIu32, call->cBytes);
911 smartcard_msz_dump(log, g_LogLevel,
" sz2=", call->mszCards, call->cBytes, TRUE);
912 WLog_Print(log, g_LogLevel,
" cReaders=%" PRIu32, call->cReaders);
913 dump_reader_states_w(log, call->rgReaderStates, call->cReaders);
914 WLog_Print(log, g_LogLevel,
"}");
917static void smartcard_trace_list_readers_return(wLog* log,
const ListReaders_Return* ret,
922 if (!WLog_IsLevelActive(log, g_LogLevel))
925 WLog_Print(log, g_LogLevel,
"ListReaders%s_Return {", unicode ?
"W" :
"A");
926 WLog_Print(log, g_LogLevel,
" ReturnCode: %s (0x%08" PRIX32
")",
927 SCardGetErrorString(ret->ReturnCode),
928 WINPR_CXX_COMPAT_CAST(UINT32, ret->ReturnCode));
930 if (ret->ReturnCode != SCARD_S_SUCCESS)
932 WLog_Print(log, g_LogLevel,
"}");
936 char* mszA = smartcard_convert_string_list(ret->msz, ret->cBytes, unicode);
938 WLog_Print(log, g_LogLevel,
" cBytes: %" PRIu32
" msz: %s", ret->cBytes, mszA);
939 WLog_Print(log, g_LogLevel,
"}");
948 if (!WLog_IsLevelActive(log, g_LogLevel))
951 WLog_Print(log, g_LogLevel,
"GetStatusChange%s_Return {", unicode ?
"W" :
"A");
952 WLog_Print(log, g_LogLevel,
" ReturnCode: %s (0x%08" PRIX32
")",
953 SCardGetErrorString(ret->ReturnCode),
954 WINPR_CXX_COMPAT_CAST(UINT32, ret->ReturnCode));
955 WLog_Print(log, g_LogLevel,
" cReaders: %" PRIu32
"", ret->cReaders);
957 dump_reader_states_return(log, ret->rgReaderStates, ret->cReaders);
959 if (!ret->rgReaderStates && (ret->cReaders > 0))
961 WLog_Print(log, g_LogLevel,
" [INVALID STATE] rgReaderStates=nullptr, cReaders=%" PRIu32,
964 else if (ret->ReturnCode != SCARD_S_SUCCESS)
966 WLog_Print(log, g_LogLevel,
" [INVALID RETURN] rgReaderStates, cReaders=%" PRIu32,
971 for (UINT32 index = 0; index < ret->cReaders; index++)
974 char* szCurrentState = SCardGetReaderStateString(rgReaderState->dwCurrentState);
975 char* szEventState = SCardGetReaderStateString(rgReaderState->dwEventState);
976 WLog_Print(log, g_LogLevel,
" [%" PRIu32
"]: dwCurrentState: %s (0x%08" PRIX32
")",
977 index, szCurrentState, rgReaderState->dwCurrentState);
978 WLog_Print(log, g_LogLevel,
" [%" PRIu32
"]: dwEventState: %s (0x%08" PRIX32
")",
979 index, szEventState, rgReaderState->dwEventState);
980 smartcard_dump_array(
981 log, g_LogLevel,
" [%" PRIu32
"]: cbAtr: %" PRIu32
" rgbAtr: ",
"",
982 rgReaderState->rgbAtr, rgReaderState->cbAtr, index, rgReaderState->cbAtr);
983 free(szCurrentState);
988 WLog_Print(log, g_LogLevel,
"}");
991static void smartcard_trace_context_and_two_strings_a_call(wLog* log,
996 if (!WLog_IsLevelActive(log, g_LogLevel))
999 WLog_Print(log, g_LogLevel,
"ContextAndTwoStringW_Call {");
1000 smartcard_log_context(log, &call->handles.hContext);
1001 WLog_Print(log, g_LogLevel,
" sz1=%s", call->sz1);
1002 WLog_Print(log, g_LogLevel,
" sz2=%s", call->sz2);
1003 WLog_Print(log, g_LogLevel,
"}");
1006static void smartcard_trace_context_and_two_strings_w_call(wLog* log,
1010 char sz1[1024] = WINPR_C_ARRAY_INIT;
1011 char sz2[1024] = WINPR_C_ARRAY_INIT;
1013 if (!WLog_IsLevelActive(log, g_LogLevel))
1016 (void)ConvertWCharToUtf8(call->sz1, sz1, ARRAYSIZE(sz1));
1018 (void)ConvertWCharToUtf8(call->sz2, sz2, ARRAYSIZE(sz2));
1020 WLog_Print(log, g_LogLevel,
"ContextAndTwoStringW_Call {");
1021 smartcard_log_context(log, &call->handles.hContext);
1022 WLog_Print(log, g_LogLevel,
" sz1=%s", sz1);
1023 WLog_Print(log, g_LogLevel,
" sz2=%s", sz2);
1024 WLog_Print(log, g_LogLevel,
"}");
1031 if (!WLog_IsLevelActive(log, g_LogLevel))
1034 WLog_Print(log, g_LogLevel,
"GetTransmitCount_Call {");
1035 smartcard_log_context(log, &call->handles.hContext);
1036 smartcard_log_redir_handle(log, &call->handles.hCard);
1038 WLog_Print(log, g_LogLevel,
"}");
1041static void smartcard_trace_write_cache_a_call(wLog* log,
const WriteCacheA_Call* call)
1045 if (!WLog_IsLevelActive(log, g_LogLevel))
1048 WLog_Print(log, g_LogLevel,
"WriteCacheA_Call {");
1050 WLog_Print(log, g_LogLevel,
" szLookupName=%s", call->szLookupName);
1052 smartcard_log_context(log, &call->Common.handles.hContext);
1053 smartcard_dump_array(log, g_LogLevel,
"..CardIdentifier=",
"", call->Common.CardIdentifier,
1055 WLog_Print(log, g_LogLevel,
" FreshnessCounter=%" PRIu32, call->Common.FreshnessCounter);
1056 WLog_Print(log, g_LogLevel,
" cbDataLen=%" PRIu32, call->Common.cbDataLen);
1057 smartcard_dump_array(log, g_LogLevel,
" pbData=",
"", call->Common.pbData,
1058 call->Common.cbDataLen);
1059 WLog_Print(log, g_LogLevel,
"}");
1062static void smartcard_trace_write_cache_w_call(wLog* log,
const WriteCacheW_Call* call)
1065 char tmp[1024] = WINPR_C_ARRAY_INIT;
1067 if (!WLog_IsLevelActive(log, g_LogLevel))
1070 WLog_Print(log, g_LogLevel,
"WriteCacheW_Call {");
1072 if (call->szLookupName)
1073 (void)ConvertWCharToUtf8(call->szLookupName, tmp, ARRAYSIZE(tmp));
1074 WLog_Print(log, g_LogLevel,
" szLookupName=%s", tmp);
1076 smartcard_log_context(log, &call->Common.handles.hContext);
1077 smartcard_dump_array(log, g_LogLevel,
"..CardIdentifier=",
"", call->Common.CardIdentifier,
1079 WLog_Print(log, g_LogLevel,
" FreshnessCounter=%" PRIu32, call->Common.FreshnessCounter);
1080 WLog_Print(log, g_LogLevel,
" cbDataLen=%" PRIu32, call->Common.cbDataLen);
1081 smartcard_dump_array(log, g_LogLevel,
" pbData=",
"", call->Common.pbData,
1082 call->Common.cbDataLen);
1083 WLog_Print(log, g_LogLevel,
"}");
1086static void smartcard_trace_read_cache_a_call(wLog* log,
const ReadCacheA_Call* call)
1090 if (!WLog_IsLevelActive(log, g_LogLevel))
1093 WLog_Print(log, g_LogLevel,
"ReadCacheA_Call {");
1095 WLog_Print(log, g_LogLevel,
" szLookupName=%s", call->szLookupName);
1096 smartcard_log_context(log, &call->Common.handles.hContext);
1097 smartcard_dump_array(log, g_LogLevel,
"..CardIdentifier=",
"", call->Common.CardIdentifier,
1099 WLog_Print(log, g_LogLevel,
" FreshnessCounter=%" PRIu32, call->Common.FreshnessCounter);
1100 WLog_Print(log, g_LogLevel,
" fPbDataIsNULL=%" PRId32, call->Common.fPbDataIsNULL);
1101 WLog_Print(log, g_LogLevel,
" cbDataLen=%" PRIu32, call->Common.cbDataLen);
1103 WLog_Print(log, g_LogLevel,
"}");
1106static void smartcard_trace_read_cache_w_call(wLog* log,
const ReadCacheW_Call* call)
1109 char tmp[1024] = WINPR_C_ARRAY_INIT;
1111 if (!WLog_IsLevelActive(log, g_LogLevel))
1114 WLog_Print(log, g_LogLevel,
"ReadCacheW_Call {");
1115 if (call->szLookupName)
1116 (void)ConvertWCharToUtf8(call->szLookupName, tmp, ARRAYSIZE(tmp));
1117 WLog_Print(log, g_LogLevel,
" szLookupName=%s", tmp);
1119 smartcard_log_context(log, &call->Common.handles.hContext);
1120 smartcard_dump_array(log, g_LogLevel,
"..CardIdentifier=",
"", call->Common.CardIdentifier,
1122 WLog_Print(log, g_LogLevel,
" FreshnessCounter=%" PRIu32, call->Common.FreshnessCounter);
1123 WLog_Print(log, g_LogLevel,
" fPbDataIsNULL=%" PRId32, call->Common.fPbDataIsNULL);
1124 WLog_Print(log, g_LogLevel,
" cbDataLen=%" PRIu32, call->Common.cbDataLen);
1126 WLog_Print(log, g_LogLevel,
"}");
1129static void smartcard_trace_transmit_call(wLog* log,
const Transmit_Call* call)
1132 UINT32 cbExtraBytes = 0;
1133 BYTE* pbExtraBytes =
nullptr;
1135 if (!WLog_IsLevelActive(log, g_LogLevel))
1138 WLog_Print(log, g_LogLevel,
"Transmit_Call {");
1139 smartcard_log_context(log, &call->handles.hContext);
1140 smartcard_log_redir_handle(log, &call->handles.hCard);
1142 if (call->pioSendPci)
1144 cbExtraBytes = (UINT32)(call->pioSendPci->cbPciLength -
sizeof(
SCARD_IO_REQUEST));
1146 WLog_Print(log, g_LogLevel,
"pioSendPci: dwProtocol: %" PRIu32
" cbExtraBytes: %" PRIu32
"",
1147 call->pioSendPci->dwProtocol, cbExtraBytes);
1151 smartcard_dump_array(log, g_LogLevel,
"pbExtraBytes: ",
"", pbExtraBytes, cbExtraBytes);
1156 WLog_Print(log, g_LogLevel,
"pioSendPci: null");
1159 WLog_Print(log, g_LogLevel,
"cbSendLength: %" PRIu32
"", call->cbSendLength);
1161 if (call->pbSendBuffer)
1163 smartcard_dump_array(log, g_LogLevel,
"pbSendBuffer: ",
"", call->pbSendBuffer,
1164 call->cbSendLength);
1168 WLog_Print(log, g_LogLevel,
"pbSendBuffer: null");
1171 if (call->pioRecvPci)
1173 cbExtraBytes = (UINT32)(call->pioRecvPci->cbPciLength -
sizeof(
SCARD_IO_REQUEST));
1175 WLog_Print(log, g_LogLevel,
"pioRecvPci: dwProtocol: %" PRIu32
" cbExtraBytes: %" PRIu32
"",
1176 call->pioRecvPci->dwProtocol, cbExtraBytes);
1180 smartcard_dump_array(log, g_LogLevel,
"pbExtraBytes: ",
"", pbExtraBytes, cbExtraBytes);
1185 WLog_Print(log, g_LogLevel,
"pioRecvPci: null");
1188 WLog_Print(log, g_LogLevel,
"fpbRecvBufferIsNULL: %" PRId32
" cbRecvLength: %" PRIu32
"",
1189 call->fpbRecvBufferIsNULL, call->cbRecvLength);
1190 WLog_Print(log, g_LogLevel,
"}");
1193static void smartcard_trace_locate_cards_by_atr_w_call(wLog* log,
1198 if (!WLog_IsLevelActive(log, g_LogLevel))
1201 WLog_Print(log, g_LogLevel,
"LocateCardsByATRW_Call {");
1202 smartcard_log_context(log, &call->handles.hContext);
1204 dump_reader_states_w(log, call->rgReaderStates, call->cReaders);
1206 WLog_Print(log, g_LogLevel,
"}");
1209static void smartcard_trace_transmit_return(wLog* log,
const Transmit_Return* ret)
1212 UINT32 cbExtraBytes = 0;
1213 BYTE* pbExtraBytes =
nullptr;
1215 if (!WLog_IsLevelActive(log, g_LogLevel))
1218 WLog_Print(log, g_LogLevel,
"Transmit_Return {");
1219 WLog_Print(log, g_LogLevel,
" ReturnCode: %s (0x%08" PRIX32
")",
1220 SCardGetErrorString(ret->ReturnCode),
1221 WINPR_CXX_COMPAT_CAST(UINT32, ret->ReturnCode));
1223 if (ret->pioRecvPci)
1225 cbExtraBytes = (UINT32)(ret->pioRecvPci->cbPciLength -
sizeof(
SCARD_IO_REQUEST));
1227 WLog_Print(log, g_LogLevel,
1228 " pioRecvPci: dwProtocol: %" PRIu32
" cbExtraBytes: %" PRIu32
"",
1229 ret->pioRecvPci->dwProtocol, cbExtraBytes);
1233 smartcard_dump_array(log, g_LogLevel,
" pbExtraBytes: ",
"", pbExtraBytes,
1239 WLog_Print(log, g_LogLevel,
" pioRecvPci: null");
1242 WLog_Print(log, g_LogLevel,
" cbRecvLength: %" PRIu32
"", ret->cbRecvLength);
1244 if (ret->pbRecvBuffer)
1246 smartcard_dump_array(log, g_LogLevel,
" pbRecvBuffer: ",
"", ret->pbRecvBuffer,
1251 WLog_Print(log, g_LogLevel,
" pbRecvBuffer: null");
1254 WLog_Print(log, g_LogLevel,
"}");
1257static void smartcard_trace_control_return(wLog* log,
const Control_Return* ret)
1261 if (!WLog_IsLevelActive(log, g_LogLevel))
1264 WLog_Print(log, g_LogLevel,
"Control_Return {");
1265 WLog_Print(log, g_LogLevel,
" ReturnCode: %s (0x%08" PRIX32
")",
1266 SCardGetErrorString(ret->ReturnCode),
1267 WINPR_CXX_COMPAT_CAST(UINT32, ret->ReturnCode));
1268 WLog_Print(log, g_LogLevel,
" cbOutBufferSize: %" PRIu32
"", ret->cbOutBufferSize);
1270 if (ret->pvOutBuffer)
1272 smartcard_dump_array(log, g_LogLevel,
"pvOutBuffer: ",
"", ret->pvOutBuffer,
1273 ret->cbOutBufferSize);
1277 WLog_Print(log, g_LogLevel,
"pvOutBuffer: null");
1280 WLog_Print(log, g_LogLevel,
"}");
1283static void smartcard_trace_control_call(wLog* log,
const Control_Call* call)
1287 if (!WLog_IsLevelActive(log, g_LogLevel))
1290 WLog_Print(log, g_LogLevel,
"Control_Call {");
1291 smartcard_log_context(log, &call->handles.hContext);
1292 smartcard_log_redir_handle(log, &call->handles.hCard);
1294 WLog_Print(log, g_LogLevel,
1295 "dwControlCode: 0x%08" PRIX32
" cbInBufferSize: %" PRIu32
1296 " fpvOutBufferIsNULL: %" PRId32
" cbOutBufferSize: %" PRIu32
"",
1297 call->dwControlCode, call->cbInBufferSize, call->fpvOutBufferIsNULL,
1298 call->cbOutBufferSize);
1300 if (call->pvInBuffer)
1302 smartcard_dump_array(log, WLOG_DEBUG,
"pbInBuffer: ",
"", call->pvInBuffer,
1303 call->cbInBufferSize);
1307 WLog_Print(log, g_LogLevel,
"pvInBuffer: null");
1310 WLog_Print(log, g_LogLevel,
"}");
1313static void smartcard_trace_set_attrib_call(wLog* log,
const SetAttrib_Call* call)
1317 if (!WLog_IsLevelActive(log, g_LogLevel))
1320 WLog_Print(log, g_LogLevel,
"GetAttrib_Call {");
1321 smartcard_log_context(log, &call->handles.hContext);
1322 smartcard_log_redir_handle(log, &call->handles.hCard);
1323 WLog_Print(log, g_LogLevel,
"dwAttrId: 0x%08" PRIX32, call->dwAttrId);
1324 WLog_Print(log, g_LogLevel,
"cbAttrLen: 0x%08" PRIx32, call->cbAttrLen);
1325 smartcard_dump_array(log, g_LogLevel,
"pbAttr: ",
"", call->pbAttr, call->cbAttrLen);
1326 WLog_Print(log, g_LogLevel,
"}");
1329static void smartcard_trace_get_attrib_return(wLog* log,
const GetAttrib_Return* ret,
1334 if (!WLog_IsLevelActive(log, g_LogLevel))
1337 WLog_Print(log, g_LogLevel,
"GetAttrib_Return {");
1338 WLog_Print(log, g_LogLevel,
" ReturnCode: %s (0x%08" PRIX32
")",
1339 SCardGetErrorString(ret->ReturnCode),
1340 WINPR_CXX_COMPAT_CAST(UINT32, ret->ReturnCode));
1341 WLog_Print(log, g_LogLevel,
" dwAttrId: %s (0x%08" PRIX32
") cbAttrLen: 0x%08" PRIX32
"",
1342 SCardGetAttributeString(dwAttrId), dwAttrId, ret->cbAttrLen);
1343 smartcard_dump_array(log, g_LogLevel,
" ",
"", ret->pbAttr, ret->cbAttrLen);
1345 WLog_Print(log, g_LogLevel,
"}");
1348static void smartcard_trace_get_attrib_call(wLog* log,
const GetAttrib_Call* call)
1352 if (!WLog_IsLevelActive(log, g_LogLevel))
1355 WLog_Print(log, g_LogLevel,
"GetAttrib_Call {");
1356 smartcard_log_context(log, &call->handles.hContext);
1357 smartcard_log_redir_handle(log, &call->handles.hCard);
1359 WLog_Print(log, g_LogLevel,
1360 "dwAttrId: %s (0x%08" PRIX32
") fpbAttrIsNULL: %" PRId32
" cbAttrLen: 0x%08" PRIX32
1362 SCardGetAttributeString(call->dwAttrId), call->dwAttrId, call->fpbAttrIsNULL,
1364 WLog_Print(log, g_LogLevel,
"}");
1367static void smartcard_trace_status_call(wLog* log,
const Status_Call* call,
const BOOL unicode)
1371 if (!WLog_IsLevelActive(log, g_LogLevel))
1374 WLog_Print(log, g_LogLevel,
"Status%s_Call {", unicode ?
"W" :
"A");
1375 smartcard_log_context(log, &call->handles.hContext);
1376 smartcard_log_redir_handle(log, &call->handles.hCard);
1378 WLog_Print(log, g_LogLevel,
1379 "fmszReaderNamesIsNULL: %" PRId32
" cchReaderLen: %" PRIu32
" cbAtrLen: %" PRIu32
"",
1380 call->fmszReaderNamesIsNULL, call->cchReaderLen, call->cbAtrLen);
1381 WLog_Print(log, g_LogLevel,
"}");
1384static void smartcard_trace_status_return(wLog* log,
const Status_Return* ret,
const BOOL unicode)
1387 char* mszReaderNamesA =
nullptr;
1390 if (!WLog_IsLevelActive(log, g_LogLevel))
1392 cBytes = ret->cBytes;
1393 if (ret->ReturnCode != SCARD_S_SUCCESS)
1395 if (cBytes == SCARD_AUTOALLOCATE)
1397 mszReaderNamesA = smartcard_convert_string_list(ret->mszReaderNames, cBytes, unicode);
1399 WLog_Print(log, g_LogLevel,
"Status%s_Return {", unicode ?
"W" :
"A");
1400 WLog_Print(log, g_LogLevel,
" ReturnCode: %s (0x%08" PRIX32
")",
1401 SCardGetErrorString(ret->ReturnCode),
1402 WINPR_CXX_COMPAT_CAST(UINT32, ret->ReturnCode));
1403 WLog_Print(log, g_LogLevel,
" dwState: %s (0x%08" PRIX32
") dwProtocol: %s (0x%08" PRIX32
")",
1404 SCardGetCardStateString(ret->dwState), ret->dwState,
1405 SCardGetProtocolString(ret->dwProtocol), ret->dwProtocol);
1407 WLog_Print(log, g_LogLevel,
" cBytes: %" PRIu32
" mszReaderNames: %s", ret->cBytes,
1410 smartcard_dump_array(log, g_LogLevel,
" cbAtrLen: %" PRIu32
" pbAtr: ",
"", ret->pbAtr,
1411 ret->cbAtrLen, ret->cbAtrLen);
1412 WLog_Print(log, g_LogLevel,
"}");
1413 free(mszReaderNamesA);
1416static void smartcard_trace_state_return(wLog* log,
const State_Return* ret)
1419 char* state =
nullptr;
1421 if (!WLog_IsLevelActive(log, g_LogLevel))
1424 state = SCardGetReaderStateString(ret->dwState);
1425 WLog_Print(log, g_LogLevel,
"Reconnect_Return {");
1426 WLog_Print(log, g_LogLevel,
" ReturnCode: %s (0x%08" PRIX32
")",
1427 SCardGetErrorString(ret->ReturnCode),
1428 WINPR_CXX_COMPAT_CAST(UINT32, ret->ReturnCode));
1429 WLog_Print(log, g_LogLevel,
" dwState: %s (0x%08" PRIX32
")", state, ret->dwState);
1430 WLog_Print(log, g_LogLevel,
" dwProtocol: %s (0x%08" PRIX32
")",
1431 SCardGetProtocolString(ret->dwProtocol), ret->dwProtocol);
1432 WLog_Print(log, g_LogLevel,
" cbAtrLen: (0x%08" PRIX32
")", ret->cbAtrLen);
1433 smartcard_dump_array(log, g_LogLevel,
" rgAtr: ",
"", ret->rgAtr,
sizeof(ret->rgAtr));
1434 WLog_Print(log, g_LogLevel,
"}");
1438static void smartcard_trace_reconnect_return(wLog* log,
const Reconnect_Return* ret)
1442 if (!WLog_IsLevelActive(log, g_LogLevel))
1445 WLog_Print(log, g_LogLevel,
"Reconnect_Return {");
1446 WLog_Print(log, g_LogLevel,
" ReturnCode: %s (0x%08" PRIX32
")",
1447 SCardGetErrorString(ret->ReturnCode),
1448 WINPR_CXX_COMPAT_CAST(UINT32, ret->ReturnCode));
1449 WLog_Print(log, g_LogLevel,
" dwActiveProtocol: %s (0x%08" PRIX32
")",
1450 SCardGetProtocolString(ret->dwActiveProtocol), ret->dwActiveProtocol);
1451 WLog_Print(log, g_LogLevel,
"}");
1454static void smartcard_trace_connect_a_call(wLog* log,
const ConnectA_Call* call)
1458 if (!WLog_IsLevelActive(log, g_LogLevel))
1461 WLog_Print(log, g_LogLevel,
"ConnectA_Call {");
1462 smartcard_log_context(log, &call->Common.handles.hContext);
1464 WLog_Print(log, g_LogLevel,
1465 "szReader: %s dwShareMode: %s (0x%08" PRIX32
1466 ") dwPreferredProtocols: %s (0x%08" PRIX32
")",
1467 call->szReader, SCardGetShareModeString(call->Common.dwShareMode),
1468 call->Common.dwShareMode, SCardGetProtocolString(call->Common.dwPreferredProtocols),
1469 call->Common.dwPreferredProtocols);
1470 WLog_Print(log, g_LogLevel,
"}");
1473static void smartcard_trace_connect_w_call(wLog* log,
const ConnectW_Call* call)
1476 char szReaderA[1024] = WINPR_C_ARRAY_INIT;
1478 if (!WLog_IsLevelActive(log, g_LogLevel))
1482 (void)ConvertWCharToUtf8(call->szReader, szReaderA, ARRAYSIZE(szReaderA));
1483 WLog_Print(log, g_LogLevel,
"ConnectW_Call {");
1484 smartcard_log_context(log, &call->Common.handles.hContext);
1486 WLog_Print(log, g_LogLevel,
1487 "szReader: %s dwShareMode: %s (0x%08" PRIX32
1488 ") dwPreferredProtocols: %s (0x%08" PRIX32
")",
1489 szReaderA, SCardGetShareModeString(call->Common.dwShareMode),
1490 call->Common.dwShareMode, SCardGetProtocolString(call->Common.dwPreferredProtocols),
1491 call->Common.dwPreferredProtocols);
1492 WLog_Print(log, g_LogLevel,
"}");
1495static void smartcard_trace_hcard_and_disposition_call(wLog* log,
1501 if (!WLog_IsLevelActive(log, g_LogLevel))
1504 WLog_Print(log, g_LogLevel,
"%s_Call {", name);
1505 smartcard_log_context(log, &call->handles.hContext);
1506 smartcard_log_redir_handle(log, &call->handles.hCard);
1508 WLog_Print(log, g_LogLevel,
"dwDisposition: %s (0x%08" PRIX32
")",
1509 SCardGetDispositionString(call->dwDisposition), call->dwDisposition);
1510 WLog_Print(log, g_LogLevel,
"}");
1517 if (!WLog_IsLevelActive(log, g_LogLevel))
1520 WLog_Print(log, g_LogLevel,
"EstablishContext_Call {");
1521 WLog_Print(log, g_LogLevel,
"dwScope: %s (0x%08" PRIX32
")", SCardGetScopeString(call->dwScope),
1523 WLog_Print(log, g_LogLevel,
"}");
1530 if (!WLog_IsLevelActive(log, g_LogLevel))
1533 WLog_Print(log, g_LogLevel,
"EstablishContext_Return {");
1534 WLog_Print(log, g_LogLevel,
" ReturnCode: %s (0x%08" PRIX32
")",
1535 SCardGetErrorString(ret->ReturnCode),
1536 WINPR_CXX_COMPAT_CAST(UINT32, ret->ReturnCode));
1537 smartcard_log_context(log, &ret->hContext);
1539 WLog_Print(log, g_LogLevel,
"}");
1542void smartcard_trace_long_return(
const Long_Return* ret,
const char* name)
1544 wLog* log = scard_log();
1545 smartcard_trace_long_return_int(log, ret, name);
1548void smartcard_trace_long_return_int(wLog* log,
const Long_Return* ret,
const char* name)
1552 if (!WLog_IsLevelActive(log, g_LogLevel))
1555 WLog_Print(log, g_LogLevel,
"%s_Return {", name);
1556 WLog_Print(log, g_LogLevel,
" ReturnCode: %s (0x%08" PRIX32
")",
1557 SCardGetErrorString(ret->ReturnCode),
1558 WINPR_CXX_COMPAT_CAST(UINT32, ret->ReturnCode));
1559 WLog_Print(log, g_LogLevel,
"}");
1562static void smartcard_trace_connect_return(wLog* log,
const Connect_Return* ret)
1566 if (!WLog_IsLevelActive(log, g_LogLevel))
1569 WLog_Print(log, g_LogLevel,
"Connect_Return {");
1570 WLog_Print(log, g_LogLevel,
" ReturnCode: %s (0x%08" PRIX32
")",
1571 SCardGetErrorString(ret->ReturnCode),
1572 WINPR_CXX_COMPAT_CAST(UINT32, ret->ReturnCode));
1573 smartcard_log_context(log, &ret->hContext);
1574 smartcard_log_redir_handle(log, &ret->hCard);
1576 WLog_Print(log, g_LogLevel,
" dwActiveProtocol: %s (0x%08" PRIX32
")",
1577 SCardGetProtocolString(ret->dwActiveProtocol), ret->dwActiveProtocol);
1578 WLog_Print(log, g_LogLevel,
"}");
1581static void smartcard_trace_reconnect_call(wLog* log,
const Reconnect_Call* call)
1585 if (!WLog_IsLevelActive(log, g_LogLevel))
1588 WLog_Print(log, g_LogLevel,
"Reconnect_Call {");
1589 smartcard_log_context(log, &call->handles.hContext);
1590 smartcard_log_redir_handle(log, &call->handles.hCard);
1592 WLog_Print(log, g_LogLevel,
1593 "dwShareMode: %s (0x%08" PRIX32
") dwPreferredProtocols: %s (0x%08" PRIX32
1594 ") dwInitialization: %s (0x%08" PRIX32
")",
1595 SCardGetShareModeString(call->dwShareMode), call->dwShareMode,
1596 SCardGetProtocolString(call->dwPreferredProtocols), call->dwPreferredProtocols,
1597 SCardGetDispositionString(call->dwInitialization), call->dwInitialization);
1598 WLog_Print(log, g_LogLevel,
"}");
1605 if (!WLog_IsLevelActive(log, g_LogLevel))
1608 WLog_Print(log, g_LogLevel,
"GetDeviceTypeId_Return {");
1609 WLog_Print(log, g_LogLevel,
" ReturnCode: %s (0x%08" PRIX32
")",
1610 SCardGetErrorString(ret->ReturnCode),
1611 WINPR_CXX_COMPAT_CAST(UINT32, ret->ReturnCode));
1612 WLog_Print(log, g_LogLevel,
" dwDeviceId=%08" PRIx32, ret->dwDeviceId);
1614 WLog_Print(log, g_LogLevel,
"}");
1617static LONG smartcard_unpack_common_context_and_string_a(wLog* log,
wStream* s,
1619 CHAR** pszReaderName)
1622 UINT32 pbContextNdrPtr = 0;
1623 LONG status = smartcard_unpack_redir_scard_context(log, s, phContext, &index, &pbContextNdrPtr);
1624 if (status != SCARD_S_SUCCESS)
1627 if (!smartcard_ndr_pointer_read(log, s, &index,
nullptr))
1628 return ERROR_INVALID_DATA;
1630 status = smartcard_unpack_redir_scard_context_ref(log, s, pbContextNdrPtr, phContext);
1631 if (status != SCARD_S_SUCCESS)
1634 status = smartcard_ndr_read_a(log, s, pszReaderName, NDR_PTR_FULL);
1635 if (status != SCARD_S_SUCCESS)
1638 smartcard_trace_context_and_string_call_a(log, __func__, phContext, *pszReaderName);
1639 return SCARD_S_SUCCESS;
1642static LONG smartcard_unpack_common_context_and_string_w(wLog* log,
wStream* s,
1644 WCHAR** pszReaderName)
1647 UINT32 pbContextNdrPtr = 0;
1649 LONG status = smartcard_unpack_redir_scard_context(log, s, phContext, &index, &pbContextNdrPtr);
1650 if (status != SCARD_S_SUCCESS)
1653 if (!smartcard_ndr_pointer_read(log, s, &index,
nullptr))
1654 return ERROR_INVALID_DATA;
1656 status = smartcard_unpack_redir_scard_context_ref(log, s, pbContextNdrPtr, phContext);
1657 if (status != SCARD_S_SUCCESS)
1660 status = smartcard_ndr_read_w(log, s, pszReaderName, NDR_PTR_FULL);
1661 if (status != SCARD_S_SUCCESS)
1664 smartcard_trace_context_and_string_call_w(log, __func__, phContext, *pszReaderName);
1665 return SCARD_S_SUCCESS;
1668LONG smartcard_unpack_common_type_header(
wStream* s)
1670 wLog* log = scard_log();
1673 UINT8 endianness = 0;
1674 UINT16 commonHeaderLength = 0;
1676 if (!Stream_CheckAndLogRequiredLengthWLog(log, s, 8))
1677 return STATUS_BUFFER_TOO_SMALL;
1680 Stream_Read_UINT8(s, version);
1681 Stream_Read_UINT8(s, endianness);
1682 Stream_Read_UINT16(s, commonHeaderLength);
1683 Stream_Read_UINT32(s, filler);
1687 WLog_Print(log, WLOG_WARN,
"Unsupported CommonTypeHeader Version %" PRIu8
"", version);
1688 return STATUS_INVALID_PARAMETER;
1691 if (endianness != 0x10)
1693 WLog_Print(log, WLOG_WARN,
"Unsupported CommonTypeHeader Endianness %" PRIu8
"",
1695 return STATUS_INVALID_PARAMETER;
1698 if (commonHeaderLength != 8)
1700 WLog_Print(log, WLOG_WARN,
"Unsupported CommonTypeHeader CommonHeaderLength %" PRIu16
"",
1701 commonHeaderLength);
1702 return STATUS_INVALID_PARAMETER;
1705 if (filler != 0xCCCCCCCC)
1707 WLog_Print(log, WLOG_WARN,
"Unexpected CommonTypeHeader Filler 0x%08" PRIX32
"", filler);
1708 return STATUS_INVALID_PARAMETER;
1711 return SCARD_S_SUCCESS;
1714void smartcard_pack_common_type_header(
wStream* s)
1716 Stream_Write_UINT8(s, 1);
1717 Stream_Write_UINT8(s, 0x10);
1718 Stream_Write_UINT16(s, 8);
1719 Stream_Write_UINT32(s, 0xCCCCCCCC);
1722LONG smartcard_unpack_private_type_header(
wStream* s)
1724 wLog* log = scard_log();
1726 UINT32 objectBufferLength = 0;
1728 if (!Stream_CheckAndLogRequiredLengthWLog(log, s, 8))
1729 return STATUS_BUFFER_TOO_SMALL;
1731 Stream_Read_UINT32(s, objectBufferLength);
1732 Stream_Read_UINT32(s, filler);
1734 if (filler != 0x00000000)
1736 WLog_Print(log, WLOG_WARN,
"Unexpected PrivateTypeHeader Filler 0x%08" PRIX32
"", filler);
1737 return STATUS_INVALID_PARAMETER;
1740 if (!Stream_CheckAndLogRequiredLengthWLog(log, s, objectBufferLength))
1741 return STATUS_INVALID_PARAMETER;
1743 return SCARD_S_SUCCESS;
1746void smartcard_pack_private_type_header(
wStream* s,
const UINT32 objectBufferLength)
1748 Stream_Write_UINT32(s, objectBufferLength);
1749 Stream_Write_UINT32(s, 0x00000000);
1752LONG smartcard_unpack_read_size_align(
wStream* s,
const size_t size,
const UINT32 alignment)
1754 const size_t padsize = (size + alignment - 1) & ~(alignment - 1);
1755 const size_t pad = padsize - size;
1759 if (!Stream_SafeSeek(s, pad))
1766LONG smartcard_pack_write_size_align(
wStream* s,
const size_t size,
const UINT32 alignment)
1768 const size_t padsize = (size + alignment - 1) & ~(alignment - 1);
1769 const size_t pad = padsize - size;
1773 if (!Stream_EnsureRemainingCapacity(s, pad))
1775 wLog* log = scard_log();
1776 WLog_Print(log, WLOG_ERROR,
"Stream_EnsureRemainingCapacity failed!");
1777 return SCARD_F_INTERNAL_ERROR;
1780 Stream_Zero(s, pad);
1783 return SCARD_S_SUCCESS;
1786SCARDCONTEXT smartcard_scard_context_native_from_redir(
const REDIR_SCARDCONTEXT* context)
1788 SCARDCONTEXT hContext = WINPR_C_ARRAY_INIT;
1790 WINPR_ASSERT(context);
1791 if ((context->cbContext !=
sizeof(ULONG_PTR)) && (context->cbContext != 0))
1793 wLog* log = scard_log();
1794 WLog_Print(log, WLOG_WARN,
1795 "REDIR_SCARDCONTEXT does not match native size: Actual: %" PRIu32
1796 ", Expected: %" PRIuz
"",
1797 context->cbContext,
sizeof(ULONG_PTR));
1801 if (context->cbContext)
1802 CopyMemory(&hContext, &(context->pbContext), context->cbContext);
1808 const SCARDCONTEXT hContext)
1810 WINPR_ASSERT(context);
1812 context->cbContext =
sizeof(ULONG_PTR);
1813 CopyMemory(&(context->pbContext), &hContext, context->cbContext);
1816SCARDHANDLE smartcard_scard_handle_native_from_redir(
const REDIR_SCARDHANDLE* handle)
1818 SCARDHANDLE hCard = 0;
1820 WINPR_ASSERT(handle);
1821 if (handle->cbHandle == 0)
1824 if (handle->cbHandle !=
sizeof(ULONG_PTR))
1826 wLog* log = scard_log();
1827 WLog_Print(log, WLOG_WARN,
1828 "REDIR_SCARDHANDLE does not match native size: Actual: %" PRIu32
1829 ", Expected: %" PRIuz
"",
1830 handle->cbHandle,
sizeof(ULONG_PTR));
1834 if (handle->cbHandle)
1835 CopyMemory(&hCard, &(handle->pbHandle), handle->cbHandle);
1840void smartcard_scard_handle_native_to_redir(
REDIR_SCARDHANDLE* handle,
const SCARDHANDLE hCard)
1842 WINPR_ASSERT(handle);
1844 handle->cbHandle =
sizeof(ULONG_PTR);
1845 CopyMemory(&(handle->pbHandle), &hCard, handle->cbHandle);
1848#define smartcard_context_supported(log, size) \
1849 smartcard_context_supported_((log), (size), __FILE__, __func__, __LINE__)
1850static LONG smartcard_context_supported_(wLog* log,
const uint32_t size,
const char* file,
1851 const char* fkt,
const size_t line)
1858 return SCARD_S_SUCCESS;
1861 const uint32_t level = WLOG_WARN;
1862 if (WLog_IsLevelActive(log, level))
1864 WLog_PrintTextMessage(log, level, line, file, fkt,
1865 "REDIR_SCARDCONTEXT length is not 0, 4 or 8: %" PRIu32
"",
1868 return STATUS_INVALID_PARAMETER;
1874 UINT32* index, UINT32* ppbContextNdrPtr,
1875 const char* file,
const char* function,
1878 UINT32 pbContextNdrPtr = 0;
1881 WINPR_ASSERT(context);
1885 if (!Stream_CheckAndLogRequiredLengthWLogEx(log, WLOG_WARN, s, 4, 1,
"%s(%s:%" PRIuz
")", file,
1887 return STATUS_BUFFER_TOO_SMALL;
1889 const LONG status = smartcard_context_supported_(log, context->cbContext, file, function, line);
1890 if (status != SCARD_S_SUCCESS)
1893 Stream_Read_UINT32(s, context->cbContext);
1895 if (!smartcard_ndr_pointer_read_(log, s, index, &pbContextNdrPtr, file, function, line))
1896 return ERROR_INVALID_DATA;
1898 if (((context->cbContext == 0) && pbContextNdrPtr) ||
1899 ((context->cbContext != 0) && !pbContextNdrPtr))
1901 WLog_Print(log, WLOG_WARN,
1902 "REDIR_SCARDCONTEXT cbContext (%" PRIu32
") pbContextNdrPtr (%" PRIu32
1904 context->cbContext, pbContextNdrPtr);
1905 return STATUS_INVALID_PARAMETER;
1908 if (!Stream_CheckAndLogRequiredLengthWLogEx(log, WLOG_WARN, s, context->cbContext, 1,
1909 "%s(%s:%" PRIuz
")", file, function, line))
1910 return STATUS_INVALID_PARAMETER;
1912 *ppbContextNdrPtr = pbContextNdrPtr;
1913 return SCARD_S_SUCCESS;
1916LONG smartcard_pack_redir_scard_context(WINPR_ATTR_UNUSED wLog* log,
wStream* s,
1919 const UINT32 pbContextNdrPtr = 0x00020000 + *index * 4;
1921 WINPR_ASSERT(context);
1922 if (context->cbContext != 0)
1924 Stream_Write_UINT32(s, context->cbContext);
1925 Stream_Write_UINT32(s, pbContextNdrPtr);
1926 *index = *index + 1;
1931 return SCARD_S_SUCCESS;
1934LONG smartcard_unpack_redir_scard_context_ref(wLog* log,
wStream* s,
1935 WINPR_ATTR_UNUSED
const UINT32 pbContextNdrPtr,
1940 WINPR_ASSERT(context);
1942 if (context->cbContext == 0)
1943 return SCARD_S_SUCCESS;
1945 if (!Stream_CheckAndLogRequiredLengthWLog(log, s, 4))
1946 return STATUS_BUFFER_TOO_SMALL;
1948 Stream_Read_UINT32(s, length);
1950 if (length != context->cbContext)
1952 WLog_Print(log, WLOG_WARN,
1953 "REDIR_SCARDCONTEXT length (%" PRIu32
") cbContext (%" PRIu32
") mismatch",
1954 length, context->cbContext);
1955 return STATUS_INVALID_PARAMETER;
1958 const LONG status = smartcard_context_supported(log, context->cbContext);
1959 if (status != SCARD_S_SUCCESS)
1962 if (!Stream_CheckAndLogRequiredLengthWLog(log, s, context->cbContext))
1963 return STATUS_BUFFER_TOO_SMALL;
1965 if (context->cbContext)
1966 Stream_Read(s, &(context->pbContext), context->cbContext);
1968 ZeroMemory(&(context->pbContext),
sizeof(context->pbContext));
1970 return SCARD_S_SUCCESS;
1973LONG smartcard_pack_redir_scard_context_ref(WINPR_ATTR_UNUSED wLog* log,
wStream* s,
1976 WINPR_ASSERT(context);
1978 if (context->cbContext == 0)
1979 return SCARD_S_SUCCESS;
1981 Stream_Write_UINT32(s, context->cbContext);
1982 Stream_Write(s, &(context->pbContext), context->cbContext);
1984 return SCARD_S_SUCCESS;
1988 UINT32* index,
const char* file,
const char* function,
1991 WINPR_ASSERT(handle);
1994 if (!Stream_CheckAndLogRequiredLengthWLog(log, s, 4))
1995 return STATUS_BUFFER_TOO_SMALL;
1997 Stream_Read_UINT32(s, handle->cbHandle);
1999 if (!Stream_CheckAndLogRequiredLengthWLog(log, s, handle->cbHandle))
2000 return STATUS_BUFFER_TOO_SMALL;
2002 if (!smartcard_ndr_pointer_read_(log, s, index,
nullptr, file, function, line))
2003 return ERROR_INVALID_DATA;
2005 return SCARD_S_SUCCESS;
2008LONG smartcard_pack_redir_scard_handle(WINPR_ATTR_UNUSED wLog* log,
wStream* s,
2011 const UINT32 pbContextNdrPtr = 0x00020000 + *index * 4;
2013 WINPR_ASSERT(handle);
2014 if (handle->cbHandle != 0)
2016 Stream_Write_UINT32(s, handle->cbHandle);
2017 Stream_Write_UINT32(s, pbContextNdrPtr);
2018 *index = *index + 1;
2022 return SCARD_S_SUCCESS;
2029 WINPR_ASSERT(handle);
2031 if (handle->cbHandle == 0)
2032 return SCARD_S_SUCCESS;
2034 if (!Stream_CheckAndLogRequiredLengthWLog(log, s, 4))
2035 return STATUS_BUFFER_TOO_SMALL;
2037 Stream_Read_UINT32(s, length);
2039 if (length != handle->cbHandle)
2041 WLog_Print(log, WLOG_WARN,
2042 "REDIR_SCARDHANDLE length (%" PRIu32
") cbHandle (%" PRIu32
") mismatch", length,
2044 return STATUS_INVALID_PARAMETER;
2047 if ((handle->cbHandle != 4) && (handle->cbHandle != 8))
2049 WLog_Print(log, WLOG_WARN,
"REDIR_SCARDHANDLE length is not 4 or 8: %" PRIu32
"",
2051 return STATUS_INVALID_PARAMETER;
2054 if (!Stream_CheckAndLogRequiredLengthWLog(log, s, handle->cbHandle))
2055 return STATUS_BUFFER_TOO_SMALL;
2057 if (handle->cbHandle)
2058 Stream_Read(s, &(handle->pbHandle), handle->cbHandle);
2060 return SCARD_S_SUCCESS;
2063LONG smartcard_pack_redir_scard_handle_ref(WINPR_ATTR_UNUSED wLog* log,
wStream* s,
2066 WINPR_ASSERT(handle);
2068 if (handle->cbHandle == 0)
2069 return SCARD_S_SUCCESS;
2071 Stream_Write_UINT32(s, handle->cbHandle);
2072 Stream_Write(s, &(handle->pbHandle), handle->cbHandle);
2074 return SCARD_S_SUCCESS;
2080 wLog* log = scard_log();
2082 if (!Stream_CheckAndLogRequiredLengthWLog(log, s, 4))
2083 return STATUS_BUFFER_TOO_SMALL;
2085 Stream_Read_UINT32(s, call->dwScope);
2086 smartcard_trace_establish_context_call(log, call);
2087 return SCARD_S_SUCCESS;
2093 wLog* log = scard_log();
2095 smartcard_trace_establish_context_call(log, call);
2097 if (!Stream_EnsureRemainingCapacity(s, 4))
2098 return SCARD_E_NO_MEMORY;
2100 Stream_Write_UINT32(s, call->dwScope);
2102 return SCARD_S_SUCCESS;
2108 wLog* log = scard_log();
2112 smartcard_trace_establish_context_return(log, ret);
2113 if (ret->ReturnCode != SCARD_S_SUCCESS)
2114 return ret->ReturnCode;
2116 status = smartcard_pack_redir_scard_context(log, s, &(ret->hContext), &index);
2117 if (status != SCARD_S_SUCCESS)
2120 return smartcard_pack_redir_scard_context_ref(log, s, &(ret->hContext));
2126 wLog* log = scard_log();
2128 UINT32 pbContextNdrPtr = 0;
2131 smartcard_unpack_redir_scard_context(log, s, &(ret->hContext), &index, &pbContextNdrPtr);
2132 if (status != SCARD_S_SUCCESS)
2135 status = smartcard_unpack_redir_scard_context_ref(log, s, pbContextNdrPtr, &(ret->hContext));
2136 if (status != SCARD_S_SUCCESS)
2139 smartcard_trace_establish_context_return(log, ret);
2140 return SCARD_S_SUCCESS;
2146 UINT32 pbContextNdrPtr = 0;
2147 wLog* log = scard_log();
2150 LONG status = smartcard_unpack_redir_scard_context(log, s, &(call->handles.hContext), &index,
2152 if (status != SCARD_S_SUCCESS)
2155 status = smartcard_unpack_redir_scard_context_ref(log, s, pbContextNdrPtr,
2156 &(call->handles.hContext));
2157 if (status != SCARD_S_SUCCESS)
2158 WLog_Print(log, WLOG_ERROR,
2159 "smartcard_unpack_redir_scard_context_ref failed with error %" PRId32
"",
2162 smartcard_trace_context_call(log, call, name);
2170 UINT32 pbContextNdrPtr = 0;
2171 wLog* log = scard_log();
2174 LONG status = smartcard_unpack_redir_scard_context(log, s, &(call->handles.hContext), &index,
2177 if (status != SCARD_S_SUCCESS)
2180 if (!Stream_CheckAndLogRequiredLengthWLog(log, s, 8))
2181 return STATUS_BUFFER_TOO_SMALL;
2183 Stream_Read_INT32(s, call->fmszGroupsIsNULL);
2184 Stream_Read_UINT32(s, call->cchGroups);
2185 status = smartcard_unpack_redir_scard_context_ref(log, s, pbContextNdrPtr,
2186 &(call->handles.hContext));
2188 if (status != SCARD_S_SUCCESS)
2191 smartcard_trace_list_reader_groups_call(log, call, unicode);
2192 return SCARD_S_SUCCESS;
2199 wLog* log = scard_log();
2202 smartcard_trace_list_reader_groups_call(log, call, unicode);
2204 LONG status = smartcard_pack_redir_scard_context(log, s, &call->handles.hContext, &index);
2205 if (status != SCARD_S_SUCCESS)
2208 if (!Stream_EnsureRemainingCapacity(s, 8))
2209 return SCARD_E_NO_MEMORY;
2211 Stream_Write_INT32(s, call->fmszGroupsIsNULL);
2212 Stream_Write_UINT32(s, call->cchGroups);
2214 return smartcard_pack_redir_scard_context_ref(log, s, &call->handles.hContext);
2221 wLog* log = scard_log();
2223 DWORD cBytes = ret->cBytes;
2226 smartcard_trace_list_reader_groups_return(log, ret, unicode);
2227 if (ret->ReturnCode != SCARD_S_SUCCESS)
2229 if (cBytes == SCARD_AUTOALLOCATE)
2232 if (!Stream_EnsureRemainingCapacity(s, 4))
2233 return SCARD_E_NO_MEMORY;
2235 Stream_Write_UINT32(s, cBytes);
2236 if (!smartcard_ndr_pointer_write(s, &index, cBytes))
2237 return SCARD_E_NO_MEMORY;
2239 status = smartcard_ndr_write(s, ret->msz, cBytes, 1, NDR_PTR_SIMPLE);
2240 if (status != SCARD_S_SUCCESS)
2242 return ret->ReturnCode;
2249 wLog* log = scard_log();
2251 UINT32 mszNdrPtr = 0;
2253 if (!Stream_CheckAndLogRequiredLengthWLog(log, s, 4))
2254 return STATUS_BUFFER_TOO_SMALL;
2256 const UINT32 cBytes = Stream_Get_UINT32(s);
2258 if (!smartcard_ndr_pointer_read(log, s, &index, &mszNdrPtr))
2259 return ERROR_INVALID_DATA;
2263 LONG status = smartcard_ndr_read(log, s, &ret->msz, cBytes, 1, NDR_PTR_SIMPLE);
2264 if (status != SCARD_S_SUCCESS)
2266 ret->cBytes = cBytes;
2269 smartcard_trace_list_reader_groups_return(log, ret, unicode);
2270 return SCARD_S_SUCCESS;
2276 UINT32 mszGroupsNdrPtr = 0;
2277 UINT32 pbContextNdrPtr = 0;
2278 wLog* log = scard_log();
2281 call->mszGroups =
nullptr;
2283 LONG status = smartcard_unpack_redir_scard_context(log, s, &(call->handles.hContext), &index,
2285 if (status != SCARD_S_SUCCESS)
2288 if (!Stream_CheckAndLogRequiredLengthWLog(log, s, 4))
2289 return STATUS_BUFFER_TOO_SMALL;
2291 const UINT32 cBytes = Stream_Get_UINT32(s);
2292 if (!smartcard_ndr_pointer_read(log, s, &index, &mszGroupsNdrPtr))
2293 return ERROR_INVALID_DATA;
2295 if (!Stream_CheckAndLogRequiredLengthWLog(log, s, 8))
2296 return STATUS_BUFFER_TOO_SMALL;
2297 Stream_Read_INT32(s, call->fmszReadersIsNULL);
2298 Stream_Read_UINT32(s, call->cchReaders);
2300 status = smartcard_unpack_redir_scard_context_ref(log, s, pbContextNdrPtr,
2301 &(call->handles.hContext));
2302 if (status != SCARD_S_SUCCESS)
2305 if (mszGroupsNdrPtr)
2307 status = smartcard_ndr_read(log, s, &call->mszGroups, cBytes, 1, NDR_PTR_SIMPLE);
2308 if (status != SCARD_S_SUCCESS)
2310 call->cBytes = cBytes;
2313 smartcard_trace_list_readers_call(log, call, unicode);
2314 return SCARD_S_SUCCESS;
2321 wLog* log = scard_log();
2324 UINT32 size = ret->cBytes;
2326 smartcard_trace_list_readers_return(log, ret, unicode);
2327 if (ret->ReturnCode != SCARD_S_SUCCESS)
2330 if (!Stream_EnsureRemainingCapacity(s, 4))
2332 WLog_Print(log, WLOG_ERROR,
"Stream_EnsureRemainingCapacity failed!");
2333 return SCARD_F_INTERNAL_ERROR;
2336 Stream_Write_UINT32(s, size);
2337 if (!smartcard_ndr_pointer_write(s, &index, size))
2338 return SCARD_E_NO_MEMORY;
2340 status = smartcard_ndr_write(s, ret->msz, size, 1, NDR_PTR_SIMPLE);
2341 if (status != SCARD_S_SUCCESS)
2343 return ret->ReturnCode;
2347 UINT32* index, UINT32* ppbContextNdrPtr)
2349 WINPR_ASSERT(common);
2350 LONG status = smartcard_unpack_redir_scard_context(log, s, &(common->handles.hContext), index,
2352 if (status != SCARD_S_SUCCESS)
2355 if (!Stream_CheckAndLogRequiredLengthWLog(log, s, 8))
2356 return STATUS_BUFFER_TOO_SMALL;
2358 Stream_Read_UINT32(s, common->dwShareMode);
2359 Stream_Read_UINT32(s, common->dwPreferredProtocols);
2360 return SCARD_S_SUCCESS;
2367 UINT32 pbContextNdrPtr = 0;
2370 wLog* log = scard_log();
2372 call->szReader =
nullptr;
2374 if (!smartcard_ndr_pointer_read(log, s, &index,
nullptr))
2375 return ERROR_INVALID_DATA;
2377 status = smartcard_unpack_connect_common(log, s, &(call->Common), &index, &pbContextNdrPtr);
2378 if (status != SCARD_S_SUCCESS)
2380 WLog_Print(log, WLOG_ERROR,
"smartcard_unpack_connect_common failed with error %" PRId32
"",
2385 status = smartcard_ndr_read_a(log, s, &call->szReader, NDR_PTR_FULL);
2386 if (status != SCARD_S_SUCCESS)
2389 status = smartcard_unpack_redir_scard_context_ref(log, s, pbContextNdrPtr,
2390 &(call->Common.handles.hContext));
2391 if (status != SCARD_S_SUCCESS)
2392 WLog_Print(log, WLOG_ERROR,
2393 "smartcard_unpack_redir_scard_context_ref failed with error %" PRId32
"",
2396 smartcard_trace_connect_a_call(log, call);
2404 UINT32 pbContextNdrPtr = 0;
2407 wLog* log = scard_log();
2408 call->szReader =
nullptr;
2410 if (!smartcard_ndr_pointer_read(log, s, &index,
nullptr))
2411 return ERROR_INVALID_DATA;
2413 status = smartcard_unpack_connect_common(log, s, &(call->Common), &index, &pbContextNdrPtr);
2414 if (status != SCARD_S_SUCCESS)
2416 WLog_Print(log, WLOG_ERROR,
"smartcard_unpack_connect_common failed with error %" PRId32
"",
2421 status = smartcard_ndr_read_w(log, s, &call->szReader, NDR_PTR_FULL);
2422 if (status != SCARD_S_SUCCESS)
2425 status = smartcard_unpack_redir_scard_context_ref(log, s, pbContextNdrPtr,
2426 &(call->Common.handles.hContext));
2427 if (status != SCARD_S_SUCCESS)
2428 WLog_Print(log, WLOG_ERROR,
2429 "smartcard_unpack_redir_scard_context_ref failed with error %" PRId32
"",
2432 smartcard_trace_connect_w_call(log, call);
2442 wLog* log = scard_log();
2443 smartcard_trace_connect_return(log, ret);
2445 status = smartcard_pack_redir_scard_context(log, s, &ret->hContext, &index);
2446 if (status != SCARD_S_SUCCESS)
2449 status = smartcard_pack_redir_scard_handle(log, s, &ret->hCard, &index);
2450 if (status != SCARD_S_SUCCESS)
2453 if (!Stream_EnsureRemainingCapacity(s, 4))
2454 return SCARD_E_NO_MEMORY;
2456 Stream_Write_UINT32(s, ret->dwActiveProtocol);
2457 status = smartcard_pack_redir_scard_context_ref(log, s, &ret->hContext);
2458 if (status != SCARD_S_SUCCESS)
2460 return smartcard_pack_redir_scard_handle_ref(log, s, &(ret->hCard));
2466 UINT32 pbContextNdrPtr = 0;
2469 wLog* log = scard_log();
2470 LONG status = smartcard_unpack_redir_scard_context(log, s, &(call->handles.hContext), &index,
2472 if (status != SCARD_S_SUCCESS)
2475 status = smartcard_unpack_redir_scard_handle(log, s, &(call->handles.hCard), &index);
2476 if (status != SCARD_S_SUCCESS)
2479 if (!Stream_CheckAndLogRequiredLengthWLog(log, s, 12))
2480 return STATUS_BUFFER_TOO_SMALL;
2482 Stream_Read_UINT32(s, call->dwShareMode);
2483 Stream_Read_UINT32(s, call->dwPreferredProtocols);
2484 Stream_Read_UINT32(s, call->dwInitialization);
2486 status = smartcard_unpack_redir_scard_context_ref(log, s, pbContextNdrPtr,
2487 &(call->handles.hContext));
2488 if (status != SCARD_S_SUCCESS)
2490 WLog_Print(log, WLOG_ERROR,
2491 "smartcard_unpack_redir_scard_context_ref failed with error %" PRId32
"",
2496 status = smartcard_unpack_redir_scard_handle_ref(log, s, &(call->handles.hCard));
2497 if (status != SCARD_S_SUCCESS)
2498 WLog_Print(log, WLOG_ERROR,
2499 "smartcard_unpack_redir_scard_handle_ref failed with error %" PRId32
"", status);
2501 smartcard_trace_reconnect_call(log, call);
2508 wLog* log = scard_log();
2511 smartcard_trace_reconnect_call(log, call);
2513 LONG status = smartcard_pack_redir_scard_context(log, s, &call->handles.hContext, &index);
2514 if (status != SCARD_S_SUCCESS)
2517 status = smartcard_pack_redir_scard_handle(log, s, &call->handles.hCard, &index);
2518 if (status != SCARD_S_SUCCESS)
2521 if (!Stream_EnsureRemainingCapacity(s, 12))
2522 return SCARD_E_NO_MEMORY;
2524 Stream_Write_UINT32(s, call->dwShareMode);
2525 Stream_Write_UINT32(s, call->dwPreferredProtocols);
2526 Stream_Write_UINT32(s, call->dwInitialization);
2528 status = smartcard_pack_redir_scard_context_ref(log, s, &call->handles.hContext);
2529 if (status != SCARD_S_SUCCESS)
2532 return smartcard_pack_redir_scard_handle_ref(log, s, &call->handles.hCard);
2538 wLog* log = scard_log();
2539 smartcard_trace_reconnect_return(log, ret);
2541 if (!Stream_EnsureRemainingCapacity(s, 4))
2542 return SCARD_E_NO_MEMORY;
2543 Stream_Write_UINT32(s, ret->dwActiveProtocol);
2544 return ret->ReturnCode;
2550 wLog* log = scard_log();
2552 if (!Stream_CheckAndLogRequiredLengthWLog(log, s, 4))
2553 return STATUS_BUFFER_TOO_SMALL;
2555 Stream_Read_UINT32(s, ret->dwActiveProtocol);
2557 smartcard_trace_reconnect_return(log, ret);
2558 return SCARD_S_SUCCESS;
2565 UINT32 pbContextNdrPtr = 0;
2568 wLog* log = scard_log();
2570 LONG status = smartcard_unpack_redir_scard_context(log, s, &(call->handles.hContext), &index,
2572 if (status != SCARD_S_SUCCESS)
2575 status = smartcard_unpack_redir_scard_handle(log, s, &(call->handles.hCard), &index);
2576 if (status != SCARD_S_SUCCESS)
2579 if (!Stream_CheckAndLogRequiredLengthWLog(log, s, 4))
2580 return STATUS_BUFFER_TOO_SMALL;
2582 Stream_Read_UINT32(s, call->dwDisposition);
2584 status = smartcard_unpack_redir_scard_context_ref(log, s, pbContextNdrPtr,
2585 &(call->handles.hContext));
2586 if (status != SCARD_S_SUCCESS)
2589 status = smartcard_unpack_redir_scard_handle_ref(log, s, &(call->handles.hCard));
2590 if (status != SCARD_S_SUCCESS)
2593 smartcard_trace_hcard_and_disposition_call(log, call, name);
2601 if (!WLog_IsLevelActive(log, g_LogLevel))
2604 WLog_Print(log, g_LogLevel,
"GetStatusChangeA_Call {");
2605 smartcard_log_context(log, &call->handles.hContext);
2607 WLog_Print(log, g_LogLevel,
"dwTimeOut: 0x%08" PRIX32
" cReaders: %" PRIu32
"", call->dwTimeOut,
2610 dump_reader_states_a(log, call->rgReaderStates, call->cReaders);
2612 WLog_Print(log, g_LogLevel,
"}");
2616 const UINT32 cReaders, UINT32* ptrIndex)
2618 LONG status = SCARD_E_NO_MEMORY;
2620 WINPR_ASSERT(ppcReaders || (cReaders == 0));
2621 if (!Stream_CheckAndLogRequiredLengthWLog(log, s, 4))
2624 const UINT32 len = Stream_Get_UINT32(s);
2625 if (len != cReaders)
2627 WLog_Print(log, WLOG_ERROR,
"Count mismatch when reading LPSCARD_READERSTATEA");
2633 BOOL* states = calloc(cReaders,
sizeof(BOOL));
2634 if (!rgReaderStates || !states)
2636 status = ERROR_INVALID_DATA;
2638 for (UINT32 index = 0; index < cReaders; index++)
2640 UINT32 ptr = UINT32_MAX;
2643 if (!Stream_CheckAndLogRequiredLengthWLog(log, s, 52))
2646 if (!smartcard_ndr_pointer_read(log, s, ptrIndex, &ptr))
2652 states[index] = ptr != 0;
2653 Stream_Read_UINT32(s, readerState->dwCurrentState);
2654 Stream_Read_UINT32(s, readerState->dwEventState);
2655 Stream_Read_UINT32(s, readerState->cbAtr);
2656 if (readerState->cbAtr > ARRAYSIZE(readerState->rgbAtr))
2658 WLog_Print(log, WLOG_ERROR,
2659 "SCARD_READERSTATEA[%" PRIu32
"]::cbAtr %" PRIu32
" exceeds %" PRIuz, index,
2660 readerState->cbAtr, (
size_t)ARRAYSIZE(readerState->rgbAtr));
2663 Stream_Read(s, readerState->rgbAtr, 36);
2666 for (UINT32 index = 0; index < cReaders; index++)
2673 status = smartcard_ndr_read_a(log, s, &readerState->szReader, NDR_PTR_FULL);
2674 if (status != SCARD_S_SUCCESS)
2678 *ppcReaders = rgReaderStates;
2680 return SCARD_S_SUCCESS;
2684 for (UINT32 index = 0; index < cReaders; index++)
2687 free(readerState->szReader);
2690 free(rgReaderStates);
2696 const UINT32 cReaders, UINT32* ptrIndex)
2698 LONG status = SCARD_E_NO_MEMORY;
2700 WINPR_ASSERT(ppcReaders || (cReaders == 0));
2701 if (!Stream_CheckAndLogRequiredLengthWLog(log, s, 4))
2704 const UINT32 len = Stream_Get_UINT32(s);
2705 if (len != cReaders)
2707 WLog_Print(log, WLOG_ERROR,
"Count mismatch when reading LPSCARD_READERSTATEW");
2713 BOOL* states = calloc(cReaders,
sizeof(BOOL));
2715 if (!rgReaderStates || !states)
2718 status = ERROR_INVALID_DATA;
2719 for (UINT32 index = 0; index < cReaders; index++)
2721 UINT32 ptr = UINT32_MAX;
2724 if (!Stream_CheckAndLogRequiredLengthWLog(log, s, 52))
2727 if (!smartcard_ndr_pointer_read(log, s, ptrIndex, &ptr))
2733 states[index] = ptr != 0;
2734 Stream_Read_UINT32(s, readerState->dwCurrentState);
2735 Stream_Read_UINT32(s, readerState->dwEventState);
2736 Stream_Read_UINT32(s, readerState->cbAtr);
2737 if (readerState->cbAtr > ARRAYSIZE(readerState->rgbAtr))
2739 WLog_Print(log, WLOG_ERROR,
2740 "SCARD_READERSTATEW[%" PRIu32
"]::cbAtr %" PRIu32
" exceeds %" PRIuz, index,
2741 readerState->cbAtr, (
size_t)ARRAYSIZE(readerState->rgbAtr));
2744 Stream_Read(s, readerState->rgbAtr, 36);
2747 for (UINT32 index = 0; index < cReaders; index++)
2755 status = smartcard_ndr_read_w(log, s, &readerState->szReader, NDR_PTR_FULL);
2756 if (status != SCARD_S_SUCCESS)
2760 *ppcReaders = rgReaderStates;
2762 return SCARD_S_SUCCESS;
2766 for (UINT32 index = 0; index < cReaders; index++)
2769 free(readerState->szReader);
2772 free(rgReaderStates);
2785 UINT32 pbContextNdrPtr = 0;
2788 wLog* log = scard_log();
2790 call->rgReaderStates =
nullptr;
2792 LONG status = smartcard_unpack_redir_scard_context(log, s, &(call->handles.hContext), &index,
2794 if (status != SCARD_S_SUCCESS)
2797 if (!Stream_CheckAndLogRequiredLengthWLog(log, s, 8))
2798 return STATUS_BUFFER_TOO_SMALL;
2800 Stream_Read_UINT32(s, call->dwTimeOut);
2801 const DWORD cReaders = Stream_Get_UINT32(s);
2802 if (!smartcard_ndr_pointer_read(log, s, &index, &ndrPtr))
2803 return ERROR_INVALID_DATA;
2805 status = smartcard_unpack_redir_scard_context_ref(log, s, pbContextNdrPtr,
2806 &(call->handles.hContext));
2807 if (status != SCARD_S_SUCCESS)
2812 status = smartcard_unpack_reader_state_a(log, s, &call->rgReaderStates, cReaders, &index);
2813 if (status != SCARD_S_SUCCESS)
2815 call->cReaders = cReaders;
2819 WLog_Print(log, WLOG_WARN,
"ndrPtr=0x%08" PRIx32
", can not read rgReaderStates", ndrPtr);
2820 return SCARD_E_UNEXPECTED;
2823 smartcard_trace_get_status_change_a_call(log, call);
2824 return SCARD_S_SUCCESS;
2831 UINT32 pbContextNdrPtr = 0;
2834 wLog* log = scard_log();
2835 call->rgReaderStates =
nullptr;
2837 LONG status = smartcard_unpack_redir_scard_context(log, s, &(call->handles.hContext), &index,
2839 if (status != SCARD_S_SUCCESS)
2842 if (!Stream_CheckAndLogRequiredLengthWLog(log, s, 8))
2843 return STATUS_BUFFER_TOO_SMALL;
2845 Stream_Read_UINT32(s, call->dwTimeOut);
2846 const DWORD cReaders = Stream_Get_UINT32(s);
2847 if (!smartcard_ndr_pointer_read(log, s, &index, &ndrPtr))
2848 return ERROR_INVALID_DATA;
2850 status = smartcard_unpack_redir_scard_context_ref(log, s, pbContextNdrPtr,
2851 &(call->handles.hContext));
2852 if (status != SCARD_S_SUCCESS)
2857 status = smartcard_unpack_reader_state_w(log, s, &call->rgReaderStates, cReaders, &index);
2858 if (status != SCARD_S_SUCCESS)
2860 call->cReaders = cReaders;
2864 WLog_Print(log, WLOG_WARN,
"ndrPtr=0x%08" PRIx32
", can not read rgReaderStates", ndrPtr);
2865 return SCARD_E_UNEXPECTED;
2868 smartcard_trace_get_status_change_w_call(log, call);
2869 return SCARD_S_SUCCESS;
2876 wLog* log = scard_log();
2879 DWORD cReaders = ret->cReaders;
2882 smartcard_trace_get_status_change_return(log, ret, unicode);
2883 if ((ret->ReturnCode != SCARD_S_SUCCESS) && (ret->ReturnCode != SCARD_E_TIMEOUT))
2885 if (cReaders == SCARD_AUTOALLOCATE)
2888 if (!Stream_EnsureRemainingCapacity(s, 4))
2889 return SCARD_E_NO_MEMORY;
2891 Stream_Write_UINT32(s, cReaders);
2892 if (!smartcard_ndr_pointer_write(s, &index, cReaders))
2893 return SCARD_E_NO_MEMORY;
2894 status = smartcard_ndr_write_state(s, ret->rgReaderStates, cReaders, NDR_PTR_SIMPLE);
2895 if (status != SCARD_S_SUCCESS)
2897 return ret->ReturnCode;
2903 UINT32 pbContextNdrPtr = 0;
2905 wLog* log = scard_log();
2908 LONG status = smartcard_unpack_redir_scard_context(log, s, &(call->handles.hContext), &index,
2910 if (status != SCARD_S_SUCCESS)
2913 status = smartcard_unpack_redir_scard_handle(log, s, &(call->handles.hCard), &index);
2914 if (status != SCARD_S_SUCCESS)
2917 if (!Stream_CheckAndLogRequiredLengthWLog(log, s, 8))
2918 return STATUS_BUFFER_TOO_SMALL;
2920 Stream_Read_INT32(s, call->fpbAtrIsNULL);
2921 Stream_Read_UINT32(s, call->cbAtrLen);
2923 status = smartcard_unpack_redir_scard_context_ref(log, s, pbContextNdrPtr,
2924 &(call->handles.hContext));
2925 if (status != SCARD_S_SUCCESS)
2928 status = smartcard_unpack_redir_scard_handle_ref(log, s, &(call->handles.hCard));
2929 if (status != SCARD_S_SUCCESS)
2938 wLog* log = scard_log();
2940 DWORD cbAtrLen = ret->cbAtrLen;
2943 smartcard_trace_state_return(log, ret);
2944 if (ret->ReturnCode != SCARD_S_SUCCESS)
2946 if (cbAtrLen == SCARD_AUTOALLOCATE)
2949 Stream_Write_UINT32(s, ret->dwState);
2950 Stream_Write_UINT32(s, ret->dwProtocol);
2951 Stream_Write_UINT32(s, cbAtrLen);
2952 if (!smartcard_ndr_pointer_write(s, &index, cbAtrLen))
2953 return SCARD_E_NO_MEMORY;
2954 status = smartcard_ndr_write(s, ret->rgAtr, cbAtrLen, 1, NDR_PTR_SIMPLE);
2955 if (status != SCARD_S_SUCCESS)
2957 return ret->ReturnCode;
2963 UINT32 pbContextNdrPtr = 0;
2966 wLog* log = scard_log();
2968 LONG status = smartcard_unpack_redir_scard_context(log, s, &(call->handles.hContext), &index,
2970 if (status != SCARD_S_SUCCESS)
2973 status = smartcard_unpack_redir_scard_handle(log, s, &(call->handles.hCard), &index);
2974 if (status != SCARD_S_SUCCESS)
2977 if (!Stream_CheckAndLogRequiredLengthWLog(log, s, 12))
2978 return STATUS_BUFFER_TOO_SMALL;
2980 Stream_Read_INT32(s, call->fmszReaderNamesIsNULL);
2981 Stream_Read_UINT32(s, call->cchReaderLen);
2982 Stream_Read_UINT32(s, call->cbAtrLen);
2984 status = smartcard_unpack_redir_scard_context_ref(log, s, pbContextNdrPtr,
2985 &(call->handles.hContext));
2986 if (status != SCARD_S_SUCCESS)
2989 status = smartcard_unpack_redir_scard_handle_ref(log, s, &(call->handles.hCard));
2990 if (status != SCARD_S_SUCCESS)
2993 smartcard_trace_status_call(log, call, unicode);
2997LONG smartcard_pack_status_call(
wStream* s,
const Status_Call* call,
const BOOL unicode)
3000 wLog* log = scard_log();
3003 smartcard_trace_status_call(log, call, unicode);
3005 LONG status = smartcard_pack_redir_scard_context(log, s, &call->handles.hContext, &index);
3006 if (status != SCARD_S_SUCCESS)
3009 status = smartcard_pack_redir_scard_handle(log, s, &call->handles.hCard, &index);
3010 if (status != SCARD_S_SUCCESS)
3013 if (!Stream_EnsureRemainingCapacity(s, 12))
3014 return SCARD_E_NO_MEMORY;
3016 Stream_Write_INT32(s, call->fmszReaderNamesIsNULL);
3017 Stream_Write_UINT32(s, call->cchReaderLen);
3018 Stream_Write_UINT32(s, call->cbAtrLen);
3020 status = smartcard_pack_redir_scard_context_ref(log, s, &call->handles.hContext);
3021 if (status != SCARD_S_SUCCESS)
3024 return smartcard_pack_redir_scard_handle_ref(log, s, &call->handles.hCard);
3030 wLog* log = scard_log();
3034 DWORD cBytes = ret->cBytes;
3036 smartcard_trace_status_return(log, ret, unicode);
3037 if (ret->ReturnCode != SCARD_S_SUCCESS)
3039 if (cBytes == SCARD_AUTOALLOCATE)
3042 if (!Stream_EnsureRemainingCapacity(s, 4))
3043 return SCARD_F_INTERNAL_ERROR;
3045 Stream_Write_UINT32(s, cBytes);
3046 if (!smartcard_ndr_pointer_write(s, &index, cBytes))
3047 return SCARD_E_NO_MEMORY;
3049 if (!Stream_EnsureRemainingCapacity(s, 44))
3050 return SCARD_F_INTERNAL_ERROR;
3052 Stream_Write_UINT32(s, ret->dwState);
3053 Stream_Write_UINT32(s, ret->dwProtocol);
3054 Stream_Write(s, ret->pbAtr,
sizeof(ret->pbAtr));
3055 Stream_Write_UINT32(s, ret->cbAtrLen);
3056 status = smartcard_ndr_write(s, ret->mszReaderNames, cBytes, 1, NDR_PTR_SIMPLE);
3057 if (status != SCARD_S_SUCCESS)
3059 return ret->ReturnCode;
3065 wLog* log = scard_log();
3067 UINT32 mszNdrPtr = 0;
3069 if (!Stream_CheckAndLogRequiredLengthWLog(log, s, 4))
3070 return STATUS_BUFFER_TOO_SMALL;
3072 const UINT32 cBytes = Stream_Get_UINT32(s);
3074 if (!smartcard_ndr_pointer_read(log, s, &index, &mszNdrPtr))
3075 return ERROR_INVALID_DATA;
3077 if (!Stream_CheckAndLogRequiredLengthWLog(log, s, 44))
3078 return STATUS_BUFFER_TOO_SMALL;
3080 Stream_Read_UINT32(s, ret->dwState);
3081 Stream_Read_UINT32(s, ret->dwProtocol);
3082 Stream_Read(s, ret->pbAtr,
sizeof(ret->pbAtr));
3083 Stream_Read_UINT32(s, ret->cbAtrLen);
3087 LONG status = smartcard_ndr_read(log, s, &ret->mszReaderNames, cBytes, 1, NDR_PTR_SIMPLE);
3088 if (status != SCARD_S_SUCCESS)
3090 ret->cBytes = cBytes;
3093 smartcard_trace_status_return(log, ret, unicode);
3094 return SCARD_S_SUCCESS;
3100 wLog* log = scard_log();
3102 UINT32 pbContextNdrPtr = 0;
3104 LONG status = smartcard_unpack_redir_scard_context(log, s, &(call->handles.hContext), &index,
3106 if (status != SCARD_S_SUCCESS)
3109 status = smartcard_unpack_redir_scard_handle(log, s, &(call->handles.hCard), &index);
3110 if (status != SCARD_S_SUCCESS)
3113 if (!Stream_CheckAndLogRequiredLengthWLog(log, s, 12))
3114 return STATUS_BUFFER_TOO_SMALL;
3116 Stream_Read_UINT32(s, call->dwAttrId);
3117 Stream_Read_INT32(s, call->fpbAttrIsNULL);
3118 Stream_Read_UINT32(s, call->cbAttrLen);
3120 status = smartcard_unpack_redir_scard_context_ref(log, s, pbContextNdrPtr,
3121 &(call->handles.hContext));
3122 if (status != SCARD_S_SUCCESS)
3125 status = smartcard_unpack_redir_scard_handle_ref(log, s, &(call->handles.hCard));
3126 if (status != SCARD_S_SUCCESS)
3129 smartcard_trace_get_attrib_call(log, call);
3134 const DWORD cbAttrCallLen)
3137 wLog* log = scard_log();
3139 DWORD cbAttrLen = 0;
3141 smartcard_trace_get_attrib_return(log, ret, dwAttrId);
3143 if (!Stream_EnsureRemainingCapacity(s, 4))
3144 return SCARD_F_INTERNAL_ERROR;
3146 cbAttrLen = ret->cbAttrLen;
3147 if (ret->ReturnCode != SCARD_S_SUCCESS)
3149 if (cbAttrLen == SCARD_AUTOALLOCATE)
3154 if (cbAttrCallLen < cbAttrLen)
3155 cbAttrLen = cbAttrCallLen;
3157 Stream_Write_UINT32(s, cbAttrLen);
3158 if (!smartcard_ndr_pointer_write(s, &index, cbAttrLen))
3159 return SCARD_E_NO_MEMORY;
3161 status = smartcard_ndr_write(s, ret->pbAttr, cbAttrLen, 1, NDR_PTR_SIMPLE);
3162 if (status != SCARD_S_SUCCESS)
3164 return ret->ReturnCode;
3170 wLog* log = scard_log();
3173 UINT32 pvInBufferNdrPtr = 0;
3174 UINT32 pbContextNdrPtr = 0;
3176 call->pvInBuffer =
nullptr;
3178 LONG status = smartcard_unpack_redir_scard_context(log, s, &(call->handles.hContext), &index,
3180 if (status != SCARD_S_SUCCESS)
3183 status = smartcard_unpack_redir_scard_handle(log, s, &(call->handles.hCard), &index);
3184 if (status != SCARD_S_SUCCESS)
3187 if (!Stream_CheckAndLogRequiredLengthWLog(log, s, 20))
3188 return STATUS_BUFFER_TOO_SMALL;
3190 Stream_Read_UINT32(s, call->dwControlCode);
3191 const UINT32 cbInBufferSize = Stream_Get_UINT32(s);
3192 if (!smartcard_ndr_pointer_read(log, s, &index,
3194 return ERROR_INVALID_DATA;
3195 Stream_Read_INT32(s, call->fpvOutBufferIsNULL);
3196 Stream_Read_UINT32(s, call->cbOutBufferSize);
3198 status = smartcard_unpack_redir_scard_context_ref(log, s, pbContextNdrPtr,
3199 &(call->handles.hContext));
3200 if (status != SCARD_S_SUCCESS)
3203 status = smartcard_unpack_redir_scard_handle_ref(log, s, &(call->handles.hCard));
3204 if (status != SCARD_S_SUCCESS)
3207 if (pvInBufferNdrPtr)
3209 status = smartcard_ndr_read(log, s, &call->pvInBuffer, cbInBufferSize, 1, NDR_PTR_SIMPLE);
3210 if (status != SCARD_S_SUCCESS)
3212 call->cbInBufferSize = cbInBufferSize;
3215 smartcard_trace_control_call(log, call);
3216 return SCARD_S_SUCCESS;
3222 wLog* log = scard_log();
3225 DWORD cbDataLen = ret->cbOutBufferSize;
3228 smartcard_trace_control_return(log, ret);
3229 if (ret->ReturnCode != SCARD_S_SUCCESS)
3231 if (cbDataLen == SCARD_AUTOALLOCATE)
3234 if (!Stream_EnsureRemainingCapacity(s, 4))
3235 return SCARD_F_INTERNAL_ERROR;
3237 Stream_Write_UINT32(s, cbDataLen);
3238 if (!smartcard_ndr_pointer_write(s, &index, cbDataLen))
3239 return SCARD_E_NO_MEMORY;
3241 status = smartcard_ndr_write(s, ret->pvOutBuffer, cbDataLen, 1, NDR_PTR_SIMPLE);
3242 if (status != SCARD_S_SUCCESS)
3244 return ret->ReturnCode;
3250 BYTE* pbExtraBytes =
nullptr;
3251 UINT32 pbExtraBytesNdrPtr = 0;
3252 UINT32 pbSendBufferNdrPtr = 0;
3253 UINT32 pioRecvPciNdrPtr = 0;
3257 UINT32 pbContextNdrPtr = 0;
3260 wLog* log = scard_log();
3262 call->pioSendPci =
nullptr;
3263 call->pioRecvPci =
nullptr;
3264 call->pbSendBuffer =
nullptr;
3266 LONG status = smartcard_unpack_redir_scard_context(log, s, &(call->handles.hContext), &index,
3268 if (status != SCARD_S_SUCCESS)
3271 status = smartcard_unpack_redir_scard_handle(log, s, &(call->handles.hCard), &index);
3272 if (status != SCARD_S_SUCCESS)
3275 if (!Stream_CheckAndLogRequiredLengthWLog(log, s, 32))
3276 return STATUS_BUFFER_TOO_SMALL;
3278 Stream_Read_UINT32(s, ioSendPci.dwProtocol);
3279 Stream_Read_UINT32(s, ioSendPci.cbExtraBytes);
3280 if (!smartcard_ndr_pointer_read(log, s, &index,
3281 &pbExtraBytesNdrPtr))
3282 return ERROR_INVALID_DATA;
3284 const UINT32 cbSendLength = Stream_Get_UINT32(s);
3285 if (!smartcard_ndr_pointer_read(log, s, &index,
3286 &pbSendBufferNdrPtr))
3287 return ERROR_INVALID_DATA;
3289 if (!smartcard_ndr_pointer_read(log, s, &index,
3291 return ERROR_INVALID_DATA;
3293 Stream_Read_INT32(s, call->fpbRecvBufferIsNULL);
3294 Stream_Read_UINT32(s, call->cbRecvLength);
3296 if (ioSendPci.cbExtraBytes > 1024)
3298 WLog_Print(log, WLOG_WARN,
3299 "Transmit_Call ioSendPci.cbExtraBytes is out of bounds: %" PRIu32
" (max: 1024)",
3300 ioSendPci.cbExtraBytes);
3301 return STATUS_INVALID_PARAMETER;
3304 if (cbSendLength > 66560)
3306 WLog_Print(log, WLOG_WARN,
3307 "Transmit_Call cbSendLength is out of bounds: %" PRIu32
" (max: 66560)",
3308 ioSendPci.cbExtraBytes);
3309 return STATUS_INVALID_PARAMETER;
3312 status = smartcard_unpack_redir_scard_context_ref(log, s, pbContextNdrPtr,
3313 &(call->handles.hContext));
3314 if (status != SCARD_S_SUCCESS)
3317 status = smartcard_unpack_redir_scard_handle_ref(log, s, &(call->handles.hCard));
3318 if (status != SCARD_S_SUCCESS)
3321 if (ioSendPci.cbExtraBytes && !pbExtraBytesNdrPtr)
3325 "Transmit_Call ioSendPci.cbExtraBytes is non-zero but pbExtraBytesNdrPtr is null");
3326 return STATUS_INVALID_PARAMETER;
3329 if (pbExtraBytesNdrPtr)
3332 if (!Stream_CheckAndLogRequiredLengthWLog(log, s, 4))
3333 return STATUS_BUFFER_TOO_SMALL;
3335 Stream_Read_UINT32(s, length);
3337 if (!Stream_CheckAndLogRequiredLengthWLog(log, s, ioSendPci.cbExtraBytes))
3338 return STATUS_BUFFER_TOO_SMALL;
3340 ioSendPci.pbExtraBytes = Stream_Pointer(s);
3344 if (!call->pioSendPci)
3346 WLog_Print(log, WLOG_WARN,
"Transmit_Call out of memory error (pioSendPci)");
3347 return STATUS_NO_MEMORY;
3350 call->pioSendPci->dwProtocol = ioSendPci.dwProtocol;
3351 call->pioSendPci->cbPciLength = (DWORD)(ioSendPci.cbExtraBytes +
sizeof(
SCARD_IO_REQUEST));
3353 Stream_Read(s, pbExtraBytes, ioSendPci.cbExtraBytes);
3354 if (smartcard_unpack_read_size_align(s, ioSendPci.cbExtraBytes, 4) < 0)
3355 return STATUS_INVALID_PARAMETER;
3361 if (!call->pioSendPci)
3363 WLog_Print(log, WLOG_WARN,
"Transmit_Call out of memory error (pioSendPci)");
3364 return STATUS_NO_MEMORY;
3367 call->pioSendPci->dwProtocol = ioSendPci.dwProtocol;
3371 if (pbSendBufferNdrPtr)
3373 status = smartcard_ndr_read(log, s, &call->pbSendBuffer, cbSendLength, 1, NDR_PTR_SIMPLE);
3374 if (status != SCARD_S_SUCCESS)
3376 call->cbSendLength = cbSendLength;
3379 if (pioRecvPciNdrPtr)
3381 if (!Stream_CheckAndLogRequiredLengthWLog(log, s, 12))
3382 return STATUS_BUFFER_TOO_SMALL;
3384 Stream_Read_UINT32(s, ioRecvPci.dwProtocol);
3385 Stream_Read_UINT32(s, ioRecvPci.cbExtraBytes);
3386 if (!smartcard_ndr_pointer_read(log, s, &index,
3387 &pbExtraBytesNdrPtr))
3388 return ERROR_INVALID_DATA;
3390 if (ioRecvPci.cbExtraBytes && !pbExtraBytesNdrPtr)
3394 "Transmit_Call ioRecvPci.cbExtraBytes is non-zero but pbExtraBytesNdrPtr is null");
3395 return STATUS_INVALID_PARAMETER;
3398 if (pbExtraBytesNdrPtr)
3401 if (!Stream_CheckAndLogRequiredLengthWLog(log, s, 4))
3402 return STATUS_BUFFER_TOO_SMALL;
3404 Stream_Read_UINT32(s, length);
3406 if (ioRecvPci.cbExtraBytes > 1024)
3408 WLog_Print(log, WLOG_WARN,
3409 "Transmit_Call ioRecvPci.cbExtraBytes is out of bounds: %" PRIu32
3411 ioRecvPci.cbExtraBytes);
3412 return STATUS_INVALID_PARAMETER;
3415 if (length != ioRecvPci.cbExtraBytes)
3417 WLog_Print(log, WLOG_WARN,
3418 "Transmit_Call unexpected length: Actual: %" PRIu32
3419 ", Expected: %" PRIu32
" (ioRecvPci.cbExtraBytes)",
3420 length, ioRecvPci.cbExtraBytes);
3421 return STATUS_INVALID_PARAMETER;
3424 if (!Stream_CheckAndLogRequiredLengthWLog(log, s, ioRecvPci.cbExtraBytes))
3425 return STATUS_BUFFER_TOO_SMALL;
3427 ioRecvPci.pbExtraBytes = Stream_Pointer(s);
3431 if (!call->pioRecvPci)
3433 WLog_Print(log, WLOG_WARN,
"Transmit_Call out of memory error (pioRecvPci)");
3434 return STATUS_NO_MEMORY;
3437 call->pioRecvPci->dwProtocol = ioRecvPci.dwProtocol;
3438 call->pioRecvPci->cbPciLength =
3441 Stream_Read(s, pbExtraBytes, ioRecvPci.cbExtraBytes);
3442 if (smartcard_unpack_read_size_align(s, ioRecvPci.cbExtraBytes, 4) < 0)
3443 return STATUS_INVALID_PARAMETER;
3449 if (!call->pioRecvPci)
3451 WLog_Print(log, WLOG_WARN,
"Transmit_Call out of memory error (pioRecvPci)");
3452 return STATUS_NO_MEMORY;
3455 call->pioRecvPci->dwProtocol = ioRecvPci.dwProtocol;
3460 smartcard_trace_transmit_call(log, call);
3461 return SCARD_S_SUCCESS;
3467 wLog* log = scard_log();
3472 UINT32 cbRecvLength = ret->cbRecvLength;
3473 UINT32 cbRecvPci = ret->pioRecvPci ? ret->pioRecvPci->cbPciLength : 0;
3475 smartcard_trace_transmit_return(log, ret);
3477 if (!ret->pbRecvBuffer)
3480 if (!smartcard_ndr_pointer_write(s, &index, cbRecvPci))
3481 return SCARD_E_NO_MEMORY;
3482 if (!Stream_EnsureRemainingCapacity(s, 4))
3483 return SCARD_E_NO_MEMORY;
3484 Stream_Write_UINT32(s, cbRecvLength);
3485 if (!smartcard_ndr_pointer_write(s, &index, cbRecvLength))
3486 return SCARD_E_NO_MEMORY;
3488 if (ret->pioRecvPci)
3490 UINT32 cbExtraBytes = (UINT32)(ret->pioRecvPci->cbPciLength -
sizeof(
SCARD_IO_REQUEST));
3493 if (!Stream_EnsureRemainingCapacity(s, cbExtraBytes + 16))
3495 WLog_Print(log, WLOG_ERROR,
"Stream_EnsureRemainingCapacity failed!");
3496 return SCARD_F_INTERNAL_ERROR;
3499 Stream_Write_UINT32(s, ret->pioRecvPci->dwProtocol);
3500 Stream_Write_UINT32(s, cbExtraBytes);
3501 if (!smartcard_ndr_pointer_write(s, &index, cbExtraBytes))
3502 return SCARD_E_NO_MEMORY;
3503 error = smartcard_ndr_write(s, pbExtraBytes, cbExtraBytes, 1, NDR_PTR_SIMPLE);
3508 status = smartcard_ndr_write(s, ret->pbRecvBuffer, ret->cbRecvLength, 1, NDR_PTR_SIMPLE);
3509 if (status != SCARD_S_SUCCESS)
3511 return ret->ReturnCode;
3516 UINT32 rgReaderStatesNdrPtr = 0;
3517 UINT32 rgAtrMasksNdrPtr = 0;
3519 UINT32 pbContextNdrPtr = 0;
3522 wLog* log = scard_log();
3524 call->rgReaderStates =
nullptr;
3526 LONG status = smartcard_unpack_redir_scard_context(log, s, &(call->handles.hContext), &index,
3528 if (status != SCARD_S_SUCCESS)
3531 if (!Stream_CheckAndLogRequiredLengthWLog(log, s, 16))
3532 return STATUS_BUFFER_TOO_SMALL;
3534 Stream_Read_UINT32(s, call->cAtrs);
3535 if (!smartcard_ndr_pointer_read(log, s, &index, &rgAtrMasksNdrPtr))
3536 return ERROR_INVALID_DATA;
3537 const DWORD cReaders = Stream_Get_UINT32(s);
3538 if (!smartcard_ndr_pointer_read(log, s, &index, &rgReaderStatesNdrPtr))
3539 return ERROR_INVALID_DATA;
3541 status = smartcard_unpack_redir_scard_context_ref(log, s, pbContextNdrPtr,
3542 &(call->handles.hContext));
3543 if (status != SCARD_S_SUCCESS)
3546 if ((rgAtrMasksNdrPtr && !call->cAtrs) || (!rgAtrMasksNdrPtr && call->cAtrs))
3548 WLog_Print(log, WLOG_WARN,
3549 "LocateCardsByATRA_Call rgAtrMasksNdrPtr (0x%08" PRIX32
3550 ") and cAtrs (0x%08" PRIX32
") inconsistency",
3551 rgAtrMasksNdrPtr, call->cAtrs);
3552 return STATUS_INVALID_PARAMETER;
3555 if (rgAtrMasksNdrPtr)
3557 status = smartcard_ndr_read_atrmask(log, s, &call->rgAtrMasks, call->cAtrs, NDR_PTR_SIMPLE);
3558 if (status != SCARD_S_SUCCESS)
3562 if (rgReaderStatesNdrPtr)
3564 status = smartcard_unpack_reader_state_a(log, s, &call->rgReaderStates, cReaders, &index);
3565 if (status != SCARD_S_SUCCESS)
3567 call->cReaders = cReaders;
3570 smartcard_trace_locate_cards_by_atr_a_call(log, call);
3571 return SCARD_S_SUCCESS;
3576 UINT32 sz1NdrPtr = 0;
3577 UINT32 sz2NdrPtr = 0;
3579 UINT32 pbContextNdrPtr = 0;
3582 wLog* log = scard_log();
3584 LONG status = smartcard_unpack_redir_scard_context(log, s, &(call->handles.hContext), &index,
3586 if (status != SCARD_S_SUCCESS)
3589 if (!smartcard_ndr_pointer_read(log, s, &index, &sz1NdrPtr))
3590 return ERROR_INVALID_DATA;
3591 if (!smartcard_ndr_pointer_read(log, s, &index, &sz2NdrPtr))
3592 return ERROR_INVALID_DATA;
3595 smartcard_unpack_redir_scard_context_ref(log, s, pbContextNdrPtr, &call->handles.hContext);
3596 if (status != SCARD_S_SUCCESS)
3601 status = smartcard_ndr_read_a(log, s, &call->sz1, NDR_PTR_FULL);
3602 if (status != SCARD_S_SUCCESS)
3607 status = smartcard_ndr_read_a(log, s, &call->sz2, NDR_PTR_FULL);
3608 if (status != SCARD_S_SUCCESS)
3611 smartcard_trace_context_and_two_strings_a_call(log, call);
3612 return SCARD_S_SUCCESS;
3617 UINT32 sz1NdrPtr = 0;
3618 UINT32 sz2NdrPtr = 0;
3620 UINT32 pbContextNdrPtr = 0;
3623 wLog* log = scard_log();
3625 LONG status = smartcard_unpack_redir_scard_context(log, s, &(call->handles.hContext), &index,
3627 if (status != SCARD_S_SUCCESS)
3630 if (!smartcard_ndr_pointer_read(log, s, &index, &sz1NdrPtr))
3631 return ERROR_INVALID_DATA;
3632 if (!smartcard_ndr_pointer_read(log, s, &index, &sz2NdrPtr))
3633 return ERROR_INVALID_DATA;
3636 smartcard_unpack_redir_scard_context_ref(log, s, pbContextNdrPtr, &call->handles.hContext);
3637 if (status != SCARD_S_SUCCESS)
3642 status = smartcard_ndr_read_w(log, s, &call->sz1, NDR_PTR_FULL);
3643 if (status != SCARD_S_SUCCESS)
3648 status = smartcard_ndr_read_w(log, s, &call->sz2, NDR_PTR_FULL);
3649 if (status != SCARD_S_SUCCESS)
3652 smartcard_trace_context_and_two_strings_w_call(log, call);
3653 return SCARD_S_SUCCESS;
3658 UINT32 sz1NdrPtr = 0;
3659 UINT32 sz2NdrPtr = 0;
3661 UINT32 pbContextNdrPtr = 0;
3664 wLog* log = scard_log();
3666 LONG status = smartcard_unpack_redir_scard_context(log, s, &(call->handles.hContext), &index,
3668 if (status != SCARD_S_SUCCESS)
3671 if (!Stream_CheckAndLogRequiredLengthWLog(log, s, 16))
3672 return STATUS_BUFFER_TOO_SMALL;
3674 const UINT32 cBytes = Stream_Get_UINT32(s);
3675 if (!smartcard_ndr_pointer_read(log, s, &index, &sz1NdrPtr))
3676 return ERROR_INVALID_DATA;
3678 const DWORD cReaders = Stream_Get_UINT32(s);
3679 if (!smartcard_ndr_pointer_read(log, s, &index, &sz2NdrPtr))
3680 return ERROR_INVALID_DATA;
3682 status = smartcard_unpack_redir_scard_context_ref(log, s, pbContextNdrPtr,
3683 &(call->handles.hContext));
3684 if (status != SCARD_S_SUCCESS)
3689 status = smartcard_ndr_read_fixed_string_a(log, s, &call->mszCards, cBytes, NDR_PTR_SIMPLE);
3690 if (status != SCARD_S_SUCCESS)
3692 call->cBytes = cBytes;
3696 status = smartcard_unpack_reader_state_a(log, s, &call->rgReaderStates, cReaders, &index);
3697 if (status != SCARD_S_SUCCESS)
3699 call->cReaders = cReaders;
3701 smartcard_trace_locate_cards_a_call(log, call);
3702 return SCARD_S_SUCCESS;
3707 UINT32 sz1NdrPtr = 0;
3708 UINT32 sz2NdrPtr = 0;
3710 UINT32 pbContextNdrPtr = 0;
3713 wLog* log = scard_log();
3715 LONG status = smartcard_unpack_redir_scard_context(log, s, &(call->handles.hContext), &index,
3717 if (status != SCARD_S_SUCCESS)
3720 if (!Stream_CheckAndLogRequiredLengthWLog(log, s, 16))
3721 return STATUS_BUFFER_TOO_SMALL;
3723 const UINT32 cBytes = Stream_Get_UINT32(s);
3724 if (!smartcard_ndr_pointer_read(log, s, &index, &sz1NdrPtr))
3725 return ERROR_INVALID_DATA;
3727 const DWORD cReaders = Stream_Get_UINT32(s);
3728 if (!smartcard_ndr_pointer_read(log, s, &index, &sz2NdrPtr))
3729 return ERROR_INVALID_DATA;
3731 status = smartcard_unpack_redir_scard_context_ref(log, s, pbContextNdrPtr,
3732 &(call->handles.hContext));
3733 if (status != SCARD_S_SUCCESS)
3738 status = smartcard_ndr_read_fixed_string_w(log, s, &call->mszCards, cBytes, NDR_PTR_SIMPLE);
3739 if (status != SCARD_S_SUCCESS)
3741 call->cBytes = cBytes;
3745 status = smartcard_unpack_reader_state_w(log, s, &call->rgReaderStates, cReaders, &index);
3746 if (status != SCARD_S_SUCCESS)
3748 call->cReaders = cReaders;
3750 smartcard_trace_locate_cards_w_call(log, call);
3751 return SCARD_S_SUCCESS;
3758 UINT32 pbContextNdrPtr = 0;
3761 wLog* log = scard_log();
3763 LONG status = smartcard_unpack_redir_scard_context(log, s, &(call->handles.hContext), &index,
3765 if (status != SCARD_S_SUCCESS)
3767 status = smartcard_unpack_redir_scard_handle(log, s, &(call->handles.hCard), &index);
3768 if (status != SCARD_S_SUCCESS)
3771 if (!Stream_CheckAndLogRequiredLengthWLog(log, s, 12))
3772 return STATUS_BUFFER_TOO_SMALL;
3773 Stream_Read_UINT32(s, call->dwAttrId);
3774 Stream_Read_UINT32(s, call->cbAttrLen);
3776 if (!smartcard_ndr_pointer_read(log, s, &index, &ndrPtr))
3777 return ERROR_INVALID_DATA;
3779 status = smartcard_unpack_redir_scard_context_ref(log, s, pbContextNdrPtr,
3780 &(call->handles.hContext));
3781 if (status != SCARD_S_SUCCESS)
3784 status = smartcard_unpack_redir_scard_handle_ref(log, s, &(call->handles.hCard));
3785 if (status != SCARD_S_SUCCESS)
3791 status = smartcard_ndr_read_ex(log, s, &call->pbAttr, 0, 1, NDR_PTR_SIMPLE, &len);
3792 if (status != SCARD_S_SUCCESS)
3794 if (call->cbAttrLen > len)
3795 call->cbAttrLen = WINPR_ASSERTING_INT_CAST(DWORD, len);
3798 call->cbAttrLen = 0;
3799 smartcard_trace_set_attrib_call(log, call);
3800 return SCARD_S_SUCCESS;
3806 wLog* log = scard_log();
3809 smartcard_trace_set_attrib_call(log, call);
3811 LONG status = smartcard_pack_redir_scard_context(log, s, &call->handles.hContext, &index);
3812 if (status != SCARD_S_SUCCESS)
3815 status = smartcard_pack_redir_scard_handle(log, s, &call->handles.hCard, &index);
3816 if (status != SCARD_S_SUCCESS)
3819 if (!Stream_EnsureRemainingCapacity(s, 8))
3820 return SCARD_E_NO_MEMORY;
3822 Stream_Write_UINT32(s, call->dwAttrId);
3823 Stream_Write_UINT32(s, call->cbAttrLen);
3825 if (!smartcard_ndr_pointer_write(s, &index, call->cbAttrLen))
3826 return SCARD_E_NO_MEMORY;
3828 status = smartcard_pack_redir_scard_context_ref(log, s, &call->handles.hContext);
3829 if (status != SCARD_S_SUCCESS)
3832 status = smartcard_pack_redir_scard_handle_ref(log, s, &call->handles.hCard);
3833 if (status != SCARD_S_SUCCESS)
3836 status = smartcard_ndr_write(s, call->pbAttr, call->cbAttrLen, 1, NDR_PTR_SIMPLE);
3837 if (status != SCARD_S_SUCCESS)
3840 return SCARD_S_SUCCESS;
3845 UINT32 rgReaderStatesNdrPtr = 0;
3846 UINT32 rgAtrMasksNdrPtr = 0;
3848 UINT32 pbContextNdrPtr = 0;
3851 wLog* log = scard_log();
3853 call->rgReaderStates =
nullptr;
3855 LONG status = smartcard_unpack_redir_scard_context(log, s, &(call->handles.hContext), &index,
3857 if (status != SCARD_S_SUCCESS)
3860 if (!Stream_CheckAndLogRequiredLengthWLog(log, s, 16))
3861 return STATUS_BUFFER_TOO_SMALL;
3863 Stream_Read_UINT32(s, call->cAtrs);
3864 if (!smartcard_ndr_pointer_read(log, s, &index, &rgAtrMasksNdrPtr))
3865 return ERROR_INVALID_DATA;
3867 const DWORD cReaders = Stream_Get_UINT32(s);
3868 if (!smartcard_ndr_pointer_read(log, s, &index, &rgReaderStatesNdrPtr))
3869 return ERROR_INVALID_DATA;
3871 status = smartcard_unpack_redir_scard_context_ref(log, s, pbContextNdrPtr,
3872 &(call->handles.hContext));
3873 if (status != SCARD_S_SUCCESS)
3876 if ((rgAtrMasksNdrPtr && !call->cAtrs) || (!rgAtrMasksNdrPtr && call->cAtrs))
3878 WLog_Print(log, WLOG_WARN,
3879 "LocateCardsByATRW_Call rgAtrMasksNdrPtr (0x%08" PRIX32
3880 ") and cAtrs (0x%08" PRIX32
") inconsistency",
3881 rgAtrMasksNdrPtr, call->cAtrs);
3882 return STATUS_INVALID_PARAMETER;
3885 if (rgAtrMasksNdrPtr)
3887 status = smartcard_ndr_read_atrmask(log, s, &call->rgAtrMasks, call->cAtrs, NDR_PTR_SIMPLE);
3888 if (status != SCARD_S_SUCCESS)
3892 if (rgReaderStatesNdrPtr)
3894 status = smartcard_unpack_reader_state_w(log, s, &call->rgReaderStates, cReaders, &index);
3895 if (status != SCARD_S_SUCCESS)
3897 call->cReaders = cReaders;
3900 smartcard_trace_locate_cards_by_atr_w_call(log, call);
3901 return SCARD_S_SUCCESS;
3906 UINT32 mszNdrPtr = 0;
3907 UINT32 contextNdrPtr = 0;
3909 UINT32 pbContextNdrPtr = 0;
3912 wLog* log = scard_log();
3914 if (!smartcard_ndr_pointer_read(log, s, &index, &mszNdrPtr))
3915 return ERROR_INVALID_DATA;
3917 LONG status = smartcard_unpack_redir_scard_context(log, s, &(call->Common.handles.hContext),
3918 &index, &pbContextNdrPtr);
3919 if (status != SCARD_S_SUCCESS)
3922 if (!smartcard_ndr_pointer_read(log, s, &index, &contextNdrPtr))
3923 return ERROR_INVALID_DATA;
3925 if (!Stream_CheckAndLogRequiredLengthWLog(log, s, 12))
3926 return STATUS_BUFFER_TOO_SMALL;
3927 Stream_Read_UINT32(s, call->Common.FreshnessCounter);
3928 Stream_Read_INT32(s, call->Common.fPbDataIsNULL);
3929 Stream_Read_UINT32(s, call->Common.cbDataLen);
3931 call->szLookupName =
nullptr;
3934 status = smartcard_ndr_read_a(log, s, &call->szLookupName, NDR_PTR_FULL);
3935 if (status != SCARD_S_SUCCESS)
3939 status = smartcard_unpack_redir_scard_context_ref(log, s, pbContextNdrPtr,
3940 &call->Common.handles.hContext);
3941 if (status != SCARD_S_SUCCESS)
3946 status = smartcard_ndr_read_u(log, s, &call->Common.CardIdentifier);
3947 if (status != SCARD_S_SUCCESS)
3950 smartcard_trace_read_cache_a_call(log, call);
3951 return SCARD_S_SUCCESS;
3956 UINT32 mszNdrPtr = 0;
3957 UINT32 contextNdrPtr = 0;
3959 UINT32 pbContextNdrPtr = 0;
3962 wLog* log = scard_log();
3964 if (!smartcard_ndr_pointer_read(log, s, &index, &mszNdrPtr))
3965 return ERROR_INVALID_DATA;
3967 LONG status = smartcard_unpack_redir_scard_context(log, s, &(call->Common.handles.hContext),
3968 &index, &pbContextNdrPtr);
3969 if (status != SCARD_S_SUCCESS)
3972 if (!smartcard_ndr_pointer_read(log, s, &index, &contextNdrPtr))
3973 return ERROR_INVALID_DATA;
3975 if (!Stream_CheckAndLogRequiredLengthWLog(log, s, 12))
3976 return STATUS_BUFFER_TOO_SMALL;
3977 Stream_Read_UINT32(s, call->Common.FreshnessCounter);
3978 Stream_Read_INT32(s, call->Common.fPbDataIsNULL);
3979 Stream_Read_UINT32(s, call->Common.cbDataLen);
3981 call->szLookupName =
nullptr;
3984 status = smartcard_ndr_read_w(log, s, &call->szLookupName, NDR_PTR_FULL);
3985 if (status != SCARD_S_SUCCESS)
3989 status = smartcard_unpack_redir_scard_context_ref(log, s, pbContextNdrPtr,
3990 &call->Common.handles.hContext);
3991 if (status != SCARD_S_SUCCESS)
3996 status = smartcard_ndr_read_u(log, s, &call->Common.CardIdentifier);
3997 if (status != SCARD_S_SUCCESS)
4000 smartcard_trace_read_cache_w_call(log, call);
4001 return SCARD_S_SUCCESS;
4006 UINT32 mszNdrPtr = 0;
4007 UINT32 contextNdrPtr = 0;
4008 UINT32 pbDataNdrPtr = 0;
4010 UINT32 pbContextNdrPtr = 0;
4013 wLog* log = scard_log();
4015 if (!smartcard_ndr_pointer_read(log, s, &index, &mszNdrPtr))
4016 return ERROR_INVALID_DATA;
4018 LONG status = smartcard_unpack_redir_scard_context(log, s, &(call->Common.handles.hContext),
4019 &index, &pbContextNdrPtr);
4020 if (status != SCARD_S_SUCCESS)
4023 if (!smartcard_ndr_pointer_read(log, s, &index, &contextNdrPtr))
4024 return ERROR_INVALID_DATA;
4026 if (!Stream_CheckAndLogRequiredLengthWLog(log, s, 8))
4027 return STATUS_BUFFER_TOO_SMALL;
4029 Stream_Read_UINT32(s, call->Common.FreshnessCounter);
4030 const UINT32 cbDataLen = Stream_Get_UINT32(s);
4032 if (!smartcard_ndr_pointer_read(log, s, &index, &pbDataNdrPtr))
4033 return ERROR_INVALID_DATA;
4035 call->szLookupName =
nullptr;
4038 status = smartcard_ndr_read_a(log, s, &call->szLookupName, NDR_PTR_FULL);
4039 if (status != SCARD_S_SUCCESS)
4043 status = smartcard_unpack_redir_scard_context_ref(log, s, pbContextNdrPtr,
4044 &call->Common.handles.hContext);
4045 if (status != SCARD_S_SUCCESS)
4048 call->Common.CardIdentifier =
nullptr;
4051 status = smartcard_ndr_read_u(log, s, &call->Common.CardIdentifier);
4052 if (status != SCARD_S_SUCCESS)
4056 call->Common.pbData =
nullptr;
4059 status = smartcard_ndr_read(log, s, &call->Common.pbData, cbDataLen, 1, NDR_PTR_SIMPLE);
4060 if (status != SCARD_S_SUCCESS)
4062 call->Common.cbDataLen = cbDataLen;
4064 smartcard_trace_write_cache_a_call(log, call);
4065 return SCARD_S_SUCCESS;
4070 UINT32 mszNdrPtr = 0;
4071 UINT32 contextNdrPtr = 0;
4072 UINT32 pbDataNdrPtr = 0;
4074 UINT32 pbContextNdrPtr = 0;
4077 wLog* log = scard_log();
4079 if (!smartcard_ndr_pointer_read(log, s, &index, &mszNdrPtr))
4080 return ERROR_INVALID_DATA;
4082 LONG status = smartcard_unpack_redir_scard_context(log, s, &(call->Common.handles.hContext),
4083 &index, &pbContextNdrPtr);
4084 if (status != SCARD_S_SUCCESS)
4087 if (!smartcard_ndr_pointer_read(log, s, &index, &contextNdrPtr))
4088 return ERROR_INVALID_DATA;
4090 if (!Stream_CheckAndLogRequiredLengthWLog(log, s, 8))
4091 return STATUS_BUFFER_TOO_SMALL;
4092 Stream_Read_UINT32(s, call->Common.FreshnessCounter);
4093 const UINT32 cbDataLen = Stream_Get_UINT32(s);
4095 if (!smartcard_ndr_pointer_read(log, s, &index, &pbDataNdrPtr))
4096 return ERROR_INVALID_DATA;
4098 call->szLookupName =
nullptr;
4101 status = smartcard_ndr_read_w(log, s, &call->szLookupName, NDR_PTR_FULL);
4102 if (status != SCARD_S_SUCCESS)
4106 status = smartcard_unpack_redir_scard_context_ref(log, s, pbContextNdrPtr,
4107 &call->Common.handles.hContext);
4108 if (status != SCARD_S_SUCCESS)
4111 call->Common.CardIdentifier =
nullptr;
4114 status = smartcard_ndr_read_u(log, s, &call->Common.CardIdentifier);
4115 if (status != SCARD_S_SUCCESS)
4119 call->Common.pbData =
nullptr;
4122 status = smartcard_ndr_read(log, s, &call->Common.pbData, cbDataLen, 1, NDR_PTR_SIMPLE);
4123 if (status != SCARD_S_SUCCESS)
4125 call->Common.cbDataLen = cbDataLen;
4127 smartcard_trace_write_cache_w_call(log, call);
4134 wLog* log = scard_log();
4137 UINT32 pbContextNdrPtr = 0;
4139 LONG status = smartcard_unpack_redir_scard_context(log, s, &(call->handles.hContext), &index,
4141 if (status != SCARD_S_SUCCESS)
4144 status = smartcard_unpack_redir_scard_handle(log, s, &(call->handles.hCard), &index);
4145 if (status != SCARD_S_SUCCESS)
4148 status = smartcard_unpack_redir_scard_context_ref(log, s, pbContextNdrPtr,
4149 &(call->handles.hContext));
4150 if (status != SCARD_S_SUCCESS)
4152 WLog_Print(log, WLOG_ERROR,
4153 "smartcard_unpack_redir_scard_context_ref failed with error %" PRId32
"",
4158 status = smartcard_unpack_redir_scard_handle_ref(log, s, &(call->handles.hCard));
4159 if (status != SCARD_S_SUCCESS)
4160 WLog_Print(log, WLOG_ERROR,
4161 "smartcard_unpack_redir_scard_handle_ref failed with error %" PRId32
"", status);
4163 smartcard_trace_get_transmit_count_call(log, call);
4170 wLog* log = scard_log();
4171 return smartcard_unpack_common_context_and_string_w(log, s, &call->handles.hContext,
4172 &call->szReaderName);
4178 wLog* log = scard_log();
4179 return smartcard_unpack_common_context_and_string_a(log, s, &call->handles.hContext, &call->sz);
4185 wLog* log = scard_log();
4186 return smartcard_unpack_common_context_and_string_w(log, s, &call->handles.hContext, &call->sz);
4192 wLog* log = scard_log();
4193 return smartcard_unpack_common_context_and_string_w(log, s, &call->handles.hContext,
4194 &call->szReaderName);
4200 wLog* log = scard_log();
4201 smartcard_trace_device_type_id_return(log, ret);
4203 if (!Stream_EnsureRemainingCapacity(s, 4))
4205 WLog_Print(log, WLOG_ERROR,
"Stream_EnsureRemainingCapacity failed!");
4206 return SCARD_F_INTERNAL_ERROR;
4209 Stream_Write_UINT32(s, ret->dwDeviceId);
4211 return ret->ReturnCode;
4217 wLog* log = scard_log();
4220 DWORD cbDataLen = ret->cReaders;
4223 smartcard_trace_locate_cards_return(log, ret);
4224 if (ret->ReturnCode != SCARD_S_SUCCESS)
4226 if (cbDataLen == SCARD_AUTOALLOCATE)
4229 if (!Stream_EnsureRemainingCapacity(s, 4))
4231 WLog_Print(log, WLOG_ERROR,
"Stream_EnsureRemainingCapacity failed!");
4232 return SCARD_F_INTERNAL_ERROR;
4235 Stream_Write_UINT32(s, cbDataLen);
4236 if (!smartcard_ndr_pointer_write(s, &index, cbDataLen))
4237 return SCARD_E_NO_MEMORY;
4239 status = smartcard_ndr_write_state(s, ret->rgReaderStates, cbDataLen, NDR_PTR_SIMPLE);
4240 if (status != SCARD_S_SUCCESS)
4242 return ret->ReturnCode;
4248 wLog* log = scard_log();
4252 DWORD cbDataLen = ret->cbDataLen;
4253 smartcard_trace_get_reader_icon_return(log, ret);
4254 if (ret->ReturnCode != SCARD_S_SUCCESS)
4256 if (cbDataLen == SCARD_AUTOALLOCATE)
4259 if (!Stream_EnsureRemainingCapacity(s, 4))
4261 WLog_Print(log, WLOG_ERROR,
"Stream_EnsureRemainingCapacity failed!");
4262 return SCARD_F_INTERNAL_ERROR;
4265 Stream_Write_UINT32(s, cbDataLen);
4266 if (!smartcard_ndr_pointer_write(s, &index, cbDataLen))
4267 return SCARD_E_NO_MEMORY;
4269 status = smartcard_ndr_write(s, ret->pbData, cbDataLen, 1, NDR_PTR_SIMPLE);
4270 if (status != SCARD_S_SUCCESS)
4272 return ret->ReturnCode;
4278 wLog* log = scard_log();
4280 smartcard_trace_get_transmit_count_return(log, ret);
4282 if (!Stream_EnsureRemainingCapacity(s, 4))
4284 WLog_Print(log, WLOG_ERROR,
"Stream_EnsureRemainingCapacity failed!");
4285 return SCARD_F_INTERNAL_ERROR;
4288 Stream_Write_UINT32(s, ret->cTransmitCount);
4290 return ret->ReturnCode;
4296 wLog* log = scard_log();
4300 DWORD cbDataLen = ret->cbDataLen;
4301 smartcard_trace_read_cache_return(log, ret);
4302 if (ret->ReturnCode != SCARD_S_SUCCESS)
4305 if (cbDataLen == SCARD_AUTOALLOCATE)
4308 if (!Stream_EnsureRemainingCapacity(s, 4))
4310 WLog_Print(log, WLOG_ERROR,
"Stream_EnsureRemainingCapacity failed!");
4311 return SCARD_F_INTERNAL_ERROR;
4314 Stream_Write_UINT32(s, cbDataLen);
4315 if (!smartcard_ndr_pointer_write(s, &index, cbDataLen))
4316 return SCARD_E_NO_MEMORY;
4318 status = smartcard_ndr_write(s, ret->pbData, cbDataLen, 1, NDR_PTR_SIMPLE);
4319 if (status != SCARD_S_SUCCESS)
4321 return ret->ReturnCode;
4327 wLog* log = scard_log();
4330 smartcard_trace_context_call(log, call, name);
4332 LONG status = smartcard_pack_redir_scard_context(log, s, &call->handles.hContext, &index);
4333 if (status != SCARD_S_SUCCESS)
4336 return smartcard_pack_redir_scard_context_ref(log, s, &call->handles.hContext);
4342 wLog* log = scard_log();
4345 smartcard_trace_list_readers_call(log, call, unicode);
4347 LONG status = smartcard_pack_redir_scard_context(log, s, &call->handles.hContext, &index);
4348 if (status != SCARD_S_SUCCESS)
4351 if (!Stream_EnsureRemainingCapacity(s, 4))
4352 return SCARD_E_NO_MEMORY;
4354 Stream_Write_UINT32(s, call->cBytes);
4355 if (!smartcard_ndr_pointer_write(s, &index, call->cBytes))
4356 return SCARD_E_NO_MEMORY;
4358 if (!Stream_EnsureRemainingCapacity(s, 8))
4359 return SCARD_E_NO_MEMORY;
4361 Stream_Write_INT32(s, call->fmszReadersIsNULL);
4362 Stream_Write_UINT32(s, call->cchReaders);
4364 status = smartcard_pack_redir_scard_context_ref(log, s, &call->handles.hContext);
4365 if (status != SCARD_S_SUCCESS)
4368 if (call->cBytes > 0)
4370 status = smartcard_ndr_write(s, call->mszGroups, call->cBytes, 1, NDR_PTR_SIMPLE);
4371 if (status != SCARD_S_SUCCESS)
4375 return SCARD_S_SUCCESS;
4379 const UINT32 cReaders, UINT32* ptrIndex)
4381 WINPR_ASSERT(rgReaderStates || (cReaders == 0));
4383 if (!Stream_EnsureRemainingCapacity(s, 4))
4384 return SCARD_E_NO_MEMORY;
4386 Stream_Write_UINT32(s, cReaders);
4388 for (UINT32 i = 0; i < cReaders; i++)
4392 if (!Stream_EnsureRemainingCapacity(s, 52))
4393 return SCARD_E_NO_MEMORY;
4395 const DWORD nameLen = state->szReader ? (DWORD)(strlen(state->szReader) + 1) : 0;
4396 if (!smartcard_ndr_pointer_write(s, ptrIndex, nameLen))
4397 return SCARD_E_NO_MEMORY;
4399 Stream_Write_UINT32(s, state->dwCurrentState);
4400 Stream_Write_UINT32(s, state->dwEventState);
4401 Stream_Write_UINT32(s, state->cbAtr);
4402 Stream_Write(s, state->rgbAtr, 36);
4405 for (UINT32 i = 0; i < cReaders; i++)
4409 if (state->szReader)
4411 const UINT32 nameLen = (UINT32)(strlen(state->szReader) + 1);
4412 LONG status = smartcard_ndr_write_a(s, state->szReader, nameLen, NDR_PTR_FULL);
4413 if (status != SCARD_S_SUCCESS)
4418 return SCARD_S_SUCCESS;
4422 const UINT32 cReaders, UINT32* ptrIndex)
4424 WINPR_ASSERT(rgReaderStates || (cReaders == 0));
4426 if (!Stream_EnsureRemainingCapacity(s, 4))
4427 return SCARD_E_NO_MEMORY;
4429 Stream_Write_UINT32(s, cReaders);
4431 for (UINT32 i = 0; i < cReaders; i++)
4435 if (!Stream_EnsureRemainingCapacity(s, 52))
4436 return SCARD_E_NO_MEMORY;
4438 const DWORD nameLen = state->szReader ? (DWORD)(_wcslen(state->szReader) + 1) : 0;
4439 if (!smartcard_ndr_pointer_write(s, ptrIndex, nameLen))
4440 return SCARD_E_NO_MEMORY;
4442 Stream_Write_UINT32(s, state->dwCurrentState);
4443 Stream_Write_UINT32(s, state->dwEventState);
4444 Stream_Write_UINT32(s, state->cbAtr);
4445 Stream_Write(s, state->rgbAtr, 36);
4448 for (UINT32 i = 0; i < cReaders; i++)
4452 if (state->szReader)
4454 const UINT32 nameLen = (UINT32)(_wcslen(state->szReader) + 1);
4455 LONG status = smartcard_ndr_write_w(s, state->szReader, nameLen, NDR_PTR_FULL);
4456 if (status != SCARD_S_SUCCESS)
4461 return SCARD_S_SUCCESS;
4467 wLog* log = scard_log();
4470 smartcard_trace_get_status_change_a_call(log, call);
4472 LONG status = smartcard_pack_redir_scard_context(log, s, &call->handles.hContext, &index);
4473 if (status != SCARD_S_SUCCESS)
4476 if (!Stream_EnsureRemainingCapacity(s, 8))
4477 return SCARD_E_NO_MEMORY;
4479 Stream_Write_UINT32(s, call->dwTimeOut);
4480 Stream_Write_UINT32(s, call->cReaders);
4482 if (!smartcard_ndr_pointer_write(s, &index, call->cReaders))
4483 return SCARD_E_NO_MEMORY;
4485 status = smartcard_pack_redir_scard_context_ref(log, s, &call->handles.hContext);
4486 if (status != SCARD_S_SUCCESS)
4489 if (call->cReaders > 0)
4491 status = smartcard_pack_reader_state_a(s, call->rgReaderStates, call->cReaders, &index);
4492 if (status != SCARD_S_SUCCESS)
4496 return SCARD_S_SUCCESS;
4502 wLog* log = scard_log();
4505 smartcard_trace_get_status_change_w_call(log, call);
4507 LONG status = smartcard_pack_redir_scard_context(log, s, &call->handles.hContext, &index);
4508 if (status != SCARD_S_SUCCESS)
4511 if (!Stream_EnsureRemainingCapacity(s, 8))
4512 return SCARD_E_NO_MEMORY;
4514 Stream_Write_UINT32(s, call->dwTimeOut);
4515 Stream_Write_UINT32(s, call->cReaders);
4517 if (!smartcard_ndr_pointer_write(s, &index, call->cReaders))
4518 return SCARD_E_NO_MEMORY;
4520 status = smartcard_pack_redir_scard_context_ref(log, s, &call->handles.hContext);
4521 if (status != SCARD_S_SUCCESS)
4524 if (call->cReaders > 0)
4526 status = smartcard_pack_reader_state_w(s, call->rgReaderStates, call->cReaders, &index);
4527 if (status != SCARD_S_SUCCESS)
4531 return SCARD_S_SUCCESS;
4537 wLog* log = scard_log();
4540 smartcard_trace_connect_a_call(log, call);
4542 const DWORD readerLen = call->szReader ? (DWORD)(strlen(call->szReader) + 1) : 0;
4543 if (!smartcard_ndr_pointer_write(s, &index, readerLen))
4544 return SCARD_E_NO_MEMORY;
4547 smartcard_pack_redir_scard_context(log, s, &call->Common.handles.hContext, &index);
4548 if (status != SCARD_S_SUCCESS)
4551 if (!Stream_EnsureRemainingCapacity(s, 8))
4552 return SCARD_E_NO_MEMORY;
4554 Stream_Write_UINT32(s, call->Common.dwShareMode);
4555 Stream_Write_UINT32(s, call->Common.dwPreferredProtocols);
4559 status = smartcard_ndr_write_a(s, call->szReader, readerLen, NDR_PTR_FULL);
4560 if (status != SCARD_S_SUCCESS)
4564 return smartcard_pack_redir_scard_context_ref(log, s, &call->Common.handles.hContext);
4570 wLog* log = scard_log();
4573 smartcard_trace_connect_w_call(log, call);
4575 const DWORD readerLen = call->szReader ? (DWORD)(_wcslen(call->szReader) + 1) : 0;
4576 if (!smartcard_ndr_pointer_write(s, &index, readerLen))
4577 return SCARD_E_NO_MEMORY;
4580 smartcard_pack_redir_scard_context(log, s, &call->Common.handles.hContext, &index);
4581 if (status != SCARD_S_SUCCESS)
4584 if (!Stream_EnsureRemainingCapacity(s, 8))
4585 return SCARD_E_NO_MEMORY;
4587 Stream_Write_UINT32(s, call->Common.dwShareMode);
4588 Stream_Write_UINT32(s, call->Common.dwPreferredProtocols);
4592 status = smartcard_ndr_write_w(s, call->szReader, readerLen, NDR_PTR_FULL);
4593 if (status != SCARD_S_SUCCESS)
4597 return smartcard_pack_redir_scard_context_ref(log, s, &call->Common.handles.hContext);
4603 wLog* log = scard_log();
4606 smartcard_trace_control_call(log, call);
4608 LONG status = smartcard_pack_redir_scard_context(log, s, &call->handles.hContext, &index);
4609 if (status != SCARD_S_SUCCESS)
4612 status = smartcard_pack_redir_scard_handle(log, s, &call->handles.hCard, &index);
4613 if (status != SCARD_S_SUCCESS)
4616 if (!Stream_EnsureRemainingCapacity(s, 8))
4617 return SCARD_E_NO_MEMORY;
4619 Stream_Write_UINT32(s, call->dwControlCode);
4620 Stream_Write_UINT32(s, call->cbInBufferSize);
4621 if (!smartcard_ndr_pointer_write(s, &index, call->cbInBufferSize))
4622 return SCARD_E_NO_MEMORY;
4624 Stream_Write_INT32(s, call->fpvOutBufferIsNULL);
4625 Stream_Write_UINT32(s, call->cbOutBufferSize);
4627 status = smartcard_pack_redir_scard_context_ref(log, s, &call->handles.hContext);
4628 if (status != SCARD_S_SUCCESS)
4631 status = smartcard_pack_redir_scard_handle_ref(log, s, &call->handles.hCard);
4632 if (status != SCARD_S_SUCCESS)
4635 if (call->cbInBufferSize)
4637 status = smartcard_ndr_write(s, call->pvInBuffer, call->cbInBufferSize, 1, NDR_PTR_SIMPLE);
4638 if (status != SCARD_S_SUCCESS)
4649 wLog* log = scard_log();
4652 smartcard_trace_hcard_and_disposition_call(log, call, name);
4654 LONG status = smartcard_pack_redir_scard_context(log, s, &call->handles.hContext, &index);
4655 if (status != SCARD_S_SUCCESS)
4658 status = smartcard_pack_redir_scard_handle(log, s, &call->handles.hCard, &index);
4659 if (status != SCARD_S_SUCCESS)
4662 if (!Stream_EnsureRemainingCapacity(s, 4))
4663 return SCARD_E_NO_MEMORY;
4665 Stream_Write_UINT32(s, call->dwDisposition);
4667 status = smartcard_pack_redir_scard_context_ref(log, s, &call->handles.hContext);
4668 if (status != SCARD_S_SUCCESS)
4671 return smartcard_pack_redir_scard_handle_ref(log, s, &call->handles.hCard);
4677 wLog* log = scard_log();
4680 smartcard_trace_transmit_call(log, call);
4682 LONG status = smartcard_pack_redir_scard_context(log, s, &call->handles.hContext, &index);
4683 if (status != SCARD_S_SUCCESS)
4686 status = smartcard_pack_redir_scard_handle(log, s, &call->handles.hCard, &index);
4687 if (status != SCARD_S_SUCCESS)
4690 UINT32 cbExtraBytes = 0;
4691 const BYTE* pbExtraBytes =
nullptr;
4692 if (call->pioSendPci)
4694 cbExtraBytes = (UINT32)(call->pioSendPci->cbPciLength -
sizeof(
SCARD_IO_REQUEST));
4695 if (cbExtraBytes > 0)
4699 if (!Stream_EnsureRemainingCapacity(s, 32))
4700 return SCARD_E_NO_MEMORY;
4702 Stream_Write_UINT32(s, call->pioSendPci ? call->pioSendPci->dwProtocol : 0);
4703 Stream_Write_UINT32(s, cbExtraBytes);
4704 if (!smartcard_ndr_pointer_write(s, &index, cbExtraBytes))
4705 return SCARD_E_NO_MEMORY;
4707 Stream_Write_UINT32(s, call->cbSendLength);
4708 if (!smartcard_ndr_pointer_write(s, &index, call->cbSendLength))
4709 return SCARD_E_NO_MEMORY;
4711 const UINT32 recvPciLen = call->pioRecvPci ? (UINT32)call->pioRecvPci->cbPciLength : 0;
4712 if (!smartcard_ndr_pointer_write(s, &index, recvPciLen))
4713 return SCARD_E_NO_MEMORY;
4715 Stream_Write_INT32(s, call->fpbRecvBufferIsNULL);
4716 Stream_Write_UINT32(s, call->cbRecvLength);
4718 status = smartcard_pack_redir_scard_context_ref(log, s, &call->handles.hContext);
4719 if (status != SCARD_S_SUCCESS)
4722 status = smartcard_pack_redir_scard_handle_ref(log, s, &call->handles.hCard);
4723 if (status != SCARD_S_SUCCESS)
4726 if (cbExtraBytes > 0)
4728 status = smartcard_ndr_write(s, pbExtraBytes, cbExtraBytes, 1, NDR_PTR_SIMPLE);
4729 if (status != SCARD_S_SUCCESS)
4733 if (call->cbSendLength > 0)
4735 status = smartcard_ndr_write(s, call->pbSendBuffer, call->cbSendLength, 1, NDR_PTR_SIMPLE);
4736 if (status != SCARD_S_SUCCESS)
4740 if (call->pioRecvPci && recvPciLen > 0)
4742 UINT32 cbRecvExtra = (UINT32)(call->pioRecvPci->cbPciLength -
sizeof(
SCARD_IO_REQUEST));
4743 const BYTE* pbRecvExtra = &((
const BYTE*)call->pioRecvPci)[
sizeof(
SCARD_IO_REQUEST)];
4745 if (!Stream_EnsureRemainingCapacity(s, 12))
4746 return SCARD_E_NO_MEMORY;
4748 Stream_Write_UINT32(s, call->pioRecvPci->dwProtocol);
4749 Stream_Write_UINT32(s, cbRecvExtra);
4750 if (!smartcard_ndr_pointer_write(s, &index, cbRecvExtra))
4751 return SCARD_E_NO_MEMORY;
4753 if (cbRecvExtra > 0)
4755 status = smartcard_ndr_write(s, pbRecvExtra, cbRecvExtra, 1, NDR_PTR_SIMPLE);
4756 if (status != SCARD_S_SUCCESS)
4761 return SCARD_S_SUCCESS;
4767 wLog* log = scard_log();
4770 smartcard_trace_get_attrib_call(log, call);
4772 LONG status = smartcard_pack_redir_scard_context(log, s, &call->handles.hContext, &index);
4773 if (status != SCARD_S_SUCCESS)
4776 status = smartcard_pack_redir_scard_handle(log, s, &call->handles.hCard, &index);
4777 if (status != SCARD_S_SUCCESS)
4780 if (!Stream_EnsureRemainingCapacity(s, 12))
4781 return STATUS_NO_MEMORY;
4783 Stream_Write_UINT32(s, call->dwAttrId);
4784 Stream_Write_INT32(s, call->fpbAttrIsNULL);
4785 Stream_Write_UINT32(s, call->cbAttrLen);
4787 status = smartcard_pack_redir_scard_context_ref(log, s, &call->handles.hContext);
4788 if (status != SCARD_S_SUCCESS)
4791 return smartcard_pack_redir_scard_handle_ref(log, s, &call->handles.hCard);
4797 wLog* log = scard_log();
4799 UINT32 mszNdrPtr = 0;
4801 if (!Stream_CheckAndLogRequiredLengthWLog(log, s, 4))
4802 return STATUS_BUFFER_TOO_SMALL;
4804 const UINT32 cBytes = Stream_Get_UINT32(s);
4806 if (!smartcard_ndr_pointer_read(log, s, &index, &mszNdrPtr))
4807 return ERROR_INVALID_DATA;
4811 LONG status = smartcard_ndr_read(log, s, &ret->msz, cBytes, 1, NDR_PTR_SIMPLE);
4812 if (status != SCARD_S_SUCCESS)
4814 ret->cBytes = cBytes;
4817 smartcard_trace_list_readers_return(log, ret, unicode);
4818 return SCARD_S_SUCCESS;
4825 wLog* log = scard_log();
4829 if (!Stream_CheckAndLogRequiredLengthWLog(log, s, 4))
4830 return STATUS_BUFFER_TOO_SMALL;
4832 Stream_Read_UINT32(s, ret->cReaders);
4834 if (!smartcard_ndr_pointer_read(log, s, &index, &ndrPtr))
4835 return ERROR_INVALID_DATA;
4837 if (ndrPtr && ret->cReaders > 0)
4840 smartcard_ndr_read_state(log, s, &ret->rgReaderStates, ret->cReaders, NDR_PTR_SIMPLE);
4841 if (status != SCARD_S_SUCCESS)
4845 ret->rgReaderStates =
nullptr;
4847 smartcard_trace_get_status_change_return(log, ret, unicode);
4848 return SCARD_S_SUCCESS;
4854 wLog* log = scard_log();
4856 UINT32 pbContextNdrPtr = 0;
4859 smartcard_unpack_redir_scard_context(log, s, &ret->hContext, &index, &pbContextNdrPtr);
4860 if (status != SCARD_S_SUCCESS)
4863 status = smartcard_unpack_redir_scard_handle(log, s, &ret->hCard, &index);
4864 if (status != SCARD_S_SUCCESS)
4867 if (!Stream_CheckAndLogRequiredLengthWLog(log, s, 4))
4868 return STATUS_BUFFER_TOO_SMALL;
4870 Stream_Read_UINT32(s, ret->dwActiveProtocol);
4872 status = smartcard_unpack_redir_scard_context_ref(log, s, pbContextNdrPtr, &ret->hContext);
4873 if (status != SCARD_S_SUCCESS)
4876 status = smartcard_unpack_redir_scard_handle_ref(log, s, &ret->hCard);
4877 if (status != SCARD_S_SUCCESS)
4880 smartcard_trace_connect_return(log, ret);
4881 return SCARD_S_SUCCESS;
4887 wLog* log = scard_log();
4889 UINT32 pvOutBufferNdrPtr = 0;
4891 if (!Stream_CheckAndLogRequiredLengthWLog(log, s, 8))
4892 return STATUS_BUFFER_TOO_SMALL;
4894 const UINT32 cbOutBufferSize = Stream_Get_UINT32(s);
4896 if (!smartcard_ndr_pointer_read(log, s, &index, &pvOutBufferNdrPtr))
4897 return ERROR_INVALID_DATA;
4899 if (pvOutBufferNdrPtr && (cbOutBufferSize > 0))
4902 smartcard_ndr_read(log, s, &ret->pvOutBuffer, cbOutBufferSize, 1, NDR_PTR_SIMPLE);
4903 if (status != SCARD_S_SUCCESS)
4905 ret->cbOutBufferSize = cbOutBufferSize;
4907 smartcard_trace_control_return(log, ret);
4908 return SCARD_S_SUCCESS;
4914 wLog* log = scard_log();
4916 UINT32 recvPciNdrPtr = 0;
4917 UINT32 recvBufferNdrPtr = 0;
4919 if (!smartcard_ndr_pointer_read(log, s, &index, &recvPciNdrPtr))
4920 return ERROR_INVALID_DATA;
4922 if (!Stream_CheckAndLogRequiredLengthWLog(log, s, 4))
4923 return STATUS_BUFFER_TOO_SMALL;
4925 const UINT32 cbRecvLength = Stream_Get_UINT32(s);
4927 if (!smartcard_ndr_pointer_read(log, s, &index, &recvBufferNdrPtr))
4928 return ERROR_INVALID_DATA;
4932 if (!Stream_CheckAndLogRequiredLengthWLog(log, s, 8))
4933 return STATUS_BUFFER_TOO_SMALL;
4935 UINT32 dwProtocol = 0;
4936 UINT32 cbExtraBytes = 0;
4937 Stream_Read_UINT32(s, dwProtocol);
4938 Stream_Read_UINT32(s, cbExtraBytes);
4940 UINT32 pbExtraBytesNdrPtr = 0;
4941 if (!smartcard_ndr_pointer_read(log, s, &index, &pbExtraBytesNdrPtr))
4942 return ERROR_INVALID_DATA;
4944 if (pbExtraBytesNdrPtr)
4946 if (!Stream_CheckAndLogRequiredLengthWLog(log, s, 4))
4947 return STATUS_BUFFER_TOO_SMALL;
4950 Stream_Read_UINT32(s, length);
4951 if (length != cbExtraBytes)
4953 WLog_Print(log, WLOG_WARN,
4954 "Transmit_Return unexpected length: Actual: %" PRIu32
4955 ", Expected: %" PRIu32
" (pioRecvPci.cbExtraBytes)",
4956 length, cbExtraBytes);
4957 return STATUS_INVALID_PARAMETER;
4960 if (!Stream_CheckAndLogRequiredLengthWLog(log, s, cbExtraBytes))
4961 return STATUS_BUFFER_TOO_SMALL;
4964 if (!ret->pioRecvPci)
4965 return SCARD_E_NO_MEMORY;
4967 ret->pioRecvPci->dwProtocol = dwProtocol;
4968 ret->pioRecvPci->cbPciLength = (DWORD)(
sizeof(
SCARD_IO_REQUEST) + cbExtraBytes);
4971 Stream_Read(s, pbExtraBytes, length);
4972 if (smartcard_unpack_read_size_align(s, cbExtraBytes, 4) < 0)
4973 return STATUS_INVALID_PARAMETER;
4978 if (!ret->pioRecvPci)
4979 return SCARD_E_NO_MEMORY;
4981 ret->pioRecvPci->dwProtocol = dwProtocol;
4986 if (recvBufferNdrPtr && (cbRecvLength > 0))
4989 smartcard_ndr_read(log, s, &ret->pbRecvBuffer, cbRecvLength, 1, NDR_PTR_SIMPLE);
4990 if (status != SCARD_S_SUCCESS)
4992 ret->cbRecvLength = cbRecvLength;
4995 smartcard_trace_transmit_return(log, ret);
4996 return SCARD_S_SUCCESS;
5002 wLog* log = scard_log();
5004 UINT32 pbAttrPtr = 0;
5006 if (!Stream_CheckAndLogRequiredLengthWLog(log, s, 4))
5007 return STATUS_BUFFER_TOO_SMALL;
5009 const UINT32 cbAttrLen = Stream_Get_UINT32(s);
5011 if (!smartcard_ndr_pointer_read(log, s, &index, &pbAttrPtr))
5012 return ERROR_INVALID_DATA;
5014 LONG status = smartcard_ndr_read(log, s, &ret->pbAttr, cbAttrLen, 1, NDR_PTR_SIMPLE);
5015 if (status != SCARD_S_SUCCESS)
5017 ret->cbAttrLen = cbAttrLen;
5019 smartcard_trace_get_attrib_return(log, ret, 0);
5020 return SCARD_S_SUCCESS;