24 #include <freerdp/config.h>
26 #include <winpr/crt.h>
27 #include <winpr/print.h>
29 #include <freerdp/channels/scard.h>
30 #include <freerdp/utils/smartcard_pack.h>
32 #include <freerdp/log.h>
33 #define TAG FREERDP_TAG("scard.pack")
35 static const DWORD g_LogLevel = WLOG_DEBUG;
37 #define smartcard_unpack_redir_scard_context(s, context, index, ndr) \
38 smartcard_unpack_redir_scard_context_((s), (context), (index), (ndr), __FILE__, __func__, \
40 #define smartcard_unpack_redir_scard_handle(s, context, index) \
41 smartcard_unpack_redir_scard_handle_((s), (context), (index), __FILE__, __func__, __LINE__)
44 UINT32* index, UINT32* ppbContextNdrPtr,
45 const char* file,
const char*
function,
int line);
49 UINT32* index,
const char* file,
50 const char*
function,
int line);
53 static LONG smartcard_unpack_redir_scard_context_ref(
wStream* s, UINT32 pbContextNdrPtr,
69 #define smartcard_ndr_pointer_read(s, index, ptr) \
70 smartcard_ndr_pointer_read_((s), (index), (ptr), __FILE__, __func__, __LINE__)
71 static BOOL smartcard_ndr_pointer_read_(
wStream* s, UINT32* index, UINT32* ptr,
const char* file,
72 const char* fkt,
size_t line)
74 const UINT32 expect = 0x20000 + (*index) * 4;
79 if (!Stream_CheckAndLogRequiredLength(TAG, s, 4))
82 Stream_Read_UINT32(s, ndrPtr);
88 if (ptr && (ndrPtr == 0))
91 "[%s:%" PRIuz
"] Read context pointer 0x%08" PRIx32
", expected 0x%08" PRIx32,
92 fkt, line, ndrPtr, expect);
96 (*index) = (*index) + 1;
100 static LONG smartcard_ndr_read(
wStream* s, BYTE** data,
size_t min,
size_t elementSize,
122 return STATUS_INVALID_PARAMETER;
125 if (!Stream_CheckAndLogRequiredLength(TAG, s, required))
126 return STATUS_BUFFER_TOO_SMALL;
131 Stream_Read_UINT32(s, len);
132 Stream_Read_UINT32(s, offset);
133 Stream_Read_UINT32(s, len2);
134 if (len != offset + len2)
137 "Invalid data when reading full NDR pointer: total=%" PRIu32
138 ", offset=%" PRIu32
", remaining=%" PRIu32,
140 return STATUS_BUFFER_TOO_SMALL;
144 Stream_Read_UINT32(s, len);
146 if ((len != min) && (min > 0))
149 "Invalid data when reading simple NDR pointer: total=%" PRIu32
150 ", expected=%" PRIu32,
152 return STATUS_BUFFER_TOO_SMALL;
159 return STATUS_INVALID_PARAMETER;
164 WLog_ERR(TAG,
"Invalid length read from NDR pointer, minimum %" PRIu32
", got %" PRIu32,
166 return STATUS_DATA_ERROR;
169 if (len > SIZE_MAX / 2)
170 return STATUS_BUFFER_TOO_SMALL;
172 if (!Stream_CheckAndLogRequiredLengthOfSize(TAG, s, len, elementSize))
173 return STATUS_BUFFER_TOO_SMALL;
180 r = calloc(len +
sizeof(WCHAR),
sizeof(CHAR));
182 return SCARD_E_NO_MEMORY;
183 Stream_Read(s, r, len);
184 smartcard_unpack_read_size_align(s, len, 4);
186 return STATUS_SUCCESS;
189 static BOOL smartcard_ndr_pointer_write(
wStream* s, UINT32* index, DWORD length)
191 const UINT32 ndrPtr = 0x20000 + (*index) * 4;
195 if (!Stream_EnsureRemainingCapacity(s, 4))
200 Stream_Write_UINT32(s, ndrPtr);
201 (*index) = (*index) + 1;
204 Stream_Write_UINT32(s, 0);
208 static LONG smartcard_ndr_write(
wStream* s,
const BYTE* data, UINT32 size, UINT32 elementSize,
211 const UINT32 offset = 0;
212 const UINT32 len = size;
213 const UINT32 dataLen = size * elementSize;
217 return SCARD_S_SUCCESS;
231 return SCARD_E_INVALID_PARAMETER;
234 if (!Stream_EnsureRemainingCapacity(s, required + dataLen + 4))
235 return STATUS_BUFFER_TOO_SMALL;
240 Stream_Write_UINT32(s, len);
241 Stream_Write_UINT32(s, offset);
242 Stream_Write_UINT32(s, len);
245 Stream_Write_UINT32(s, len);
250 return SCARD_E_INVALID_PARAMETER;
254 Stream_Write(s, data, dataLen);
256 Stream_Zero(s, dataLen);
257 return smartcard_pack_write_size_align(s, len, 4);
285 static LONG smartcard_ndr_read_fixed_string_a(
wStream* s, CHAR** data,
size_t min, ndr_ptr_t type)
293 return smartcard_ndr_read(s, u.ppv, min,
sizeof(CHAR), type);
296 static LONG smartcard_ndr_read_fixed_string_w(
wStream* s, WCHAR** data,
size_t min, ndr_ptr_t type)
304 return smartcard_ndr_read(s, u.ppv, min,
sizeof(WCHAR), type);
307 static LONG smartcard_ndr_read_a(
wStream* s, CHAR** data, ndr_ptr_t type)
315 return smartcard_ndr_read(s, u.ppv, 0,
sizeof(CHAR), type);
318 static LONG smartcard_ndr_read_w(
wStream* s, WCHAR** data, ndr_ptr_t type)
326 return smartcard_ndr_read(s, u.ppv, 0,
sizeof(WCHAR), type);
329 static LONG smartcard_ndr_read_u(
wStream* s,
UUID** data)
337 return smartcard_ndr_read(s, u.ppv, 1,
sizeof(
UUID), NDR_PTR_FIXED);
340 static char* smartcard_convert_string_list(
const void* in,
size_t bytes, BOOL unicode)
361 mszA = ConvertMszWCharNToUtf8Alloc(
string.wz, bytes /
sizeof(WCHAR), &length);
367 mszA = (
char*)calloc(bytes,
sizeof(
char));
370 CopyMemory(mszA,
string.sz, bytes - 1);
379 for (
size_t index = 0; index < length - 1; index++)
381 if (mszA[index] ==
'\0')
388 static char* smartcard_msz_dump_a(
const char* msz,
size_t len,
char* buffer,
size_t bufferLen)
391 const char* cur = msz;
393 while ((len > 0) && cur && cur[0] !=
'\0' && (bufferLen > 0))
395 size_t clen = strnlen(cur, len);
396 int rc = _snprintf(buf, bufferLen,
"%s", cur);
397 bufferLen -= (size_t)rc;
406 static char* smartcard_msz_dump_w(
const WCHAR* msz,
size_t len,
char* buffer,
size_t bufferLen)
411 char* sz = ConvertMszWCharNToUtf8Alloc(msz, len, &szlen);
415 smartcard_msz_dump_a(sz, szlen, buffer, bufferLen);
420 static char* smartcard_array_dump(
const void* pd,
size_t len,
char* buffer,
size_t bufferLen)
422 const BYTE* data = pd;
424 char* start = buffer;
429 buffer[bufferLen - 1] =
'\0';
433 rc = _snprintf(buffer, bufferLen,
"{ ");
434 if ((rc < 0) || ((
size_t)rc > bufferLen))
437 bufferLen -= (size_t)rc;
439 for (
size_t x = 0; x < len; x++)
441 rc = _snprintf(buffer, bufferLen,
"%02X", data[x]);
442 if ((rc < 0) || ((
size_t)rc > bufferLen))
445 bufferLen -= (size_t)rc;
448 rc = _snprintf(buffer, bufferLen,
" }");
449 if ((rc < 0) || ((
size_t)rc > bufferLen))
455 static void smartcard_log_redir_handle(
const char* tag,
const REDIR_SCARDHANDLE* pHandle)
459 WLog_LVL(tag, g_LogLevel,
" hContext: %s",
460 smartcard_array_dump(pHandle->pbHandle, pHandle->cbHandle, buffer,
sizeof(buffer)));
463 static void smartcard_log_context(
const char* tag,
const REDIR_SCARDCONTEXT* phContext)
468 smartcard_array_dump(phContext->pbContext, phContext->cbContext, buffer,
sizeof(buffer)));
471 static void smartcard_trace_context_and_string_call_a(
const char* name,
475 if (!WLog_IsLevelActive(WLog_Get(TAG), g_LogLevel))
478 WLog_LVL(TAG, g_LogLevel,
"%s {", name);
479 smartcard_log_context(TAG, phContext);
480 WLog_LVL(TAG, g_LogLevel,
" sz=%s", sz);
482 WLog_LVL(TAG, g_LogLevel,
"}");
485 static void smartcard_trace_context_and_string_call_w(
const char* name,
489 char tmp[1024] = { 0 };
490 if (!WLog_IsLevelActive(WLog_Get(TAG), g_LogLevel))
494 (void)ConvertWCharToUtf8(sz, tmp, ARRAYSIZE(tmp));
496 WLog_LVL(TAG, g_LogLevel,
"%s {", name);
497 smartcard_log_context(TAG, phContext);
498 WLog_LVL(TAG, g_LogLevel,
" sz=%s", tmp);
499 WLog_LVL(TAG, g_LogLevel,
"}");
502 static void smartcard_trace_context_call(
const Context_Call* call,
const char* name)
505 if (!WLog_IsLevelActive(WLog_Get(TAG), g_LogLevel))
508 WLog_LVL(TAG, g_LogLevel,
"%s_Call {", name);
509 smartcard_log_context(TAG, &call->handles.hContext);
511 WLog_LVL(TAG, g_LogLevel,
"}");
517 if (!WLog_IsLevelActive(WLog_Get(TAG), g_LogLevel))
520 WLog_LVL(TAG, g_LogLevel,
"ListReaderGroups%S_Call {", unicode ?
"W" :
"A");
521 smartcard_log_context(TAG, &call->handles.hContext);
523 WLog_LVL(TAG, g_LogLevel,
"fmszGroupsIsNULL: %" PRId32
" cchGroups: 0x%08" PRIx32,
524 call->fmszGroupsIsNULL, call->cchGroups);
525 WLog_LVL(TAG, g_LogLevel,
"}");
530 char* szEventState = NULL;
531 char* szCurrentState = NULL;
533 if (!WLog_IsLevelActive(WLog_Get(TAG), g_LogLevel))
536 WLog_LVL(TAG, g_LogLevel,
"GetStatusChangeW_Call {");
537 smartcard_log_context(TAG, &call->handles.hContext);
539 WLog_LVL(TAG, g_LogLevel,
"dwTimeOut: 0x%08" PRIX32
" cReaders: %" PRIu32
"", call->dwTimeOut,
542 for (UINT32 index = 0; index < call->cReaders; index++)
545 char szReaderA[1024] = { 0 };
547 (void)ConvertWCharToUtf8(readerState->szReader, szReaderA, ARRAYSIZE(szReaderA));
549 WLog_LVL(TAG, g_LogLevel,
"\t[%" PRIu32
"]: szReader: %s cbAtr: %" PRIu32
"", index,
550 szReaderA, readerState->cbAtr);
551 szCurrentState = SCardGetReaderStateString(readerState->dwCurrentState);
552 szEventState = SCardGetReaderStateString(readerState->dwEventState);
553 WLog_LVL(TAG, g_LogLevel,
"\t[%" PRIu32
"]: dwCurrentState: %s (0x%08" PRIX32
")", index,
554 szCurrentState, readerState->dwCurrentState);
555 WLog_LVL(TAG, g_LogLevel,
"\t[%" PRIu32
"]: dwEventState: %s (0x%08" PRIX32
")", index,
556 szEventState, readerState->dwEventState);
557 free(szCurrentState);
561 WLog_LVL(TAG, g_LogLevel,
"}");
569 if (!WLog_IsLevelActive(WLog_Get(TAG), g_LogLevel))
572 mszA = smartcard_convert_string_list(ret->msz, ret->cBytes, unicode);
574 WLog_LVL(TAG, g_LogLevel,
"ListReaderGroups%s_Return {", unicode ?
"W" :
"A");
575 WLog_LVL(TAG, g_LogLevel,
" ReturnCode: %s (0x%08" PRIx32
")",
576 SCardGetErrorString(ret->ReturnCode), ret->ReturnCode);
577 WLog_LVL(TAG, g_LogLevel,
" cBytes: %" PRIu32
" msz: %s", ret->cBytes, mszA);
578 WLog_LVL(TAG, g_LogLevel,
"}");
582 static void smartcard_trace_list_readers_call(
const ListReaders_Call* call, BOOL unicode)
584 char* mszGroupsA = NULL;
586 if (!WLog_IsLevelActive(WLog_Get(TAG), g_LogLevel))
589 mszGroupsA = smartcard_convert_string_list(call->mszGroups, call->cBytes, unicode);
591 WLog_LVL(TAG, g_LogLevel,
"ListReaders%s_Call {", unicode ?
"W" :
"A");
592 smartcard_log_context(TAG, &call->handles.hContext);
594 WLog_LVL(TAG, g_LogLevel,
595 "cBytes: %" PRIu32
" mszGroups: %s fmszReadersIsNULL: %" PRId32
596 " cchReaders: 0x%08" PRIX32
"",
597 call->cBytes, mszGroupsA, call->fmszReadersIsNULL, call->cchReaders);
598 WLog_LVL(TAG, g_LogLevel,
"}");
605 char* szEventState = NULL;
606 char* szCurrentState = NULL;
608 if (!WLog_IsLevelActive(WLog_Get(TAG), g_LogLevel))
611 WLog_LVL(TAG, g_LogLevel,
"LocateCardsByATRA_Call {");
612 smartcard_log_context(TAG, &call->handles.hContext);
614 for (UINT32 index = 0; index < call->cReaders; index++)
619 WLog_LVL(TAG, g_LogLevel,
"\t[%" PRIu32
"]: szReader: %s cbAtr: %" PRIu32
"", index,
620 readerState->szReader, readerState->cbAtr);
621 szCurrentState = SCardGetReaderStateString(readerState->dwCurrentState);
622 szEventState = SCardGetReaderStateString(readerState->dwEventState);
623 WLog_LVL(TAG, g_LogLevel,
"\t[%" PRIu32
"]: dwCurrentState: %s (0x%08" PRIX32
")", index,
624 szCurrentState, readerState->dwCurrentState);
625 WLog_LVL(TAG, g_LogLevel,
"\t[%" PRIu32
"]: dwEventState: %s (0x%08" PRIX32
")", index,
626 szEventState, readerState->dwEventState);
629 TAG,
"\t[%" PRIu32
"]: cbAtr: %" PRIu32
" rgbAtr: %s", index, readerState->cbAtr,
630 smartcard_array_dump(readerState->rgbAtr, readerState->cbAtr, buffer,
sizeof(buffer)));
632 free(szCurrentState);
636 WLog_LVL(TAG, g_LogLevel,
"}");
643 if (!WLog_IsLevelActive(WLog_Get(TAG), g_LogLevel))
646 WLog_LVL(TAG, g_LogLevel,
"LocateCardsA_Call {");
647 smartcard_log_context(TAG, &call->handles.hContext);
648 WLog_LVL(TAG, g_LogLevel,
" cBytes=%" PRId32, call->cBytes);
649 WLog_LVL(TAG, g_LogLevel,
" mszCards=%s",
650 smartcard_msz_dump_a(call->mszCards, call->cBytes, buffer,
sizeof(buffer)));
651 WLog_LVL(TAG, g_LogLevel,
" cReaders=%" PRId32, call->cReaders);
654 WLog_LVL(TAG, g_LogLevel,
"}");
660 if (!WLog_IsLevelActive(WLog_Get(TAG), g_LogLevel))
663 WLog_LVL(TAG, g_LogLevel,
"LocateCards_Return {");
664 WLog_LVL(TAG, g_LogLevel,
" ReturnCode: %s (0x%08" PRIX32
")",
665 SCardGetErrorString(ret->ReturnCode), ret->ReturnCode);
667 if (ret->ReturnCode == SCARD_S_SUCCESS)
669 WLog_LVL(TAG, g_LogLevel,
" cReaders=%" PRId32, ret->cReaders);
671 WLog_LVL(TAG, g_LogLevel,
"}");
677 if (!WLog_IsLevelActive(WLog_Get(TAG), g_LogLevel))
680 WLog_LVL(TAG, g_LogLevel,
"GetReaderIcon_Return {");
681 WLog_LVL(TAG, g_LogLevel,
" ReturnCode: %s (0x%08" PRIX32
")",
682 SCardGetErrorString(ret->ReturnCode), ret->ReturnCode);
684 if (ret->ReturnCode == SCARD_S_SUCCESS)
686 WLog_LVL(TAG, g_LogLevel,
" cbDataLen=%" PRId32, ret->cbDataLen);
688 WLog_LVL(TAG, g_LogLevel,
"}");
694 if (!WLog_IsLevelActive(WLog_Get(TAG), g_LogLevel))
697 WLog_LVL(TAG, g_LogLevel,
"GetTransmitCount_Return {");
698 WLog_LVL(TAG, g_LogLevel,
" ReturnCode: %s (0x%08" PRIX32
")",
699 SCardGetErrorString(ret->ReturnCode), ret->ReturnCode);
701 WLog_LVL(TAG, g_LogLevel,
" cTransmitCount=%" PRIu32, ret->cTransmitCount);
702 WLog_LVL(TAG, g_LogLevel,
"}");
708 if (!WLog_IsLevelActive(WLog_Get(TAG), g_LogLevel))
711 WLog_LVL(TAG, g_LogLevel,
"ReadCache_Return {");
712 WLog_LVL(TAG, g_LogLevel,
" ReturnCode: %s (0x%08" PRIX32
")",
713 SCardGetErrorString(ret->ReturnCode), ret->ReturnCode);
715 if (ret->ReturnCode == SCARD_S_SUCCESS)
718 WLog_LVL(TAG, g_LogLevel,
" cbDataLen=%" PRId32, ret->cbDataLen);
719 WLog_LVL(TAG, g_LogLevel,
" cbData: %s",
720 smartcard_array_dump(ret->pbData, ret->cbDataLen, buffer,
sizeof(buffer)));
722 WLog_LVL(TAG, g_LogLevel,
"}");
729 if (!WLog_IsLevelActive(WLog_Get(TAG), g_LogLevel))
732 WLog_LVL(TAG, g_LogLevel,
"LocateCardsW_Call {");
733 smartcard_log_context(TAG, &call->handles.hContext);
734 WLog_LVL(TAG, g_LogLevel,
" cBytes=%" PRId32, call->cBytes);
735 WLog_LVL(TAG, g_LogLevel,
" sz2=%s",
736 smartcard_msz_dump_w(call->mszCards, call->cBytes, buffer,
sizeof(buffer)));
737 WLog_LVL(TAG, g_LogLevel,
" cReaders=%" PRId32, call->cReaders);
739 WLog_LVL(TAG, g_LogLevel,
"}");
742 static void smartcard_trace_list_readers_return(
const ListReaders_Return* ret, BOOL unicode)
746 if (!WLog_IsLevelActive(WLog_Get(TAG), g_LogLevel))
749 WLog_LVL(TAG, g_LogLevel,
"ListReaders%s_Return {", unicode ?
"W" :
"A");
750 WLog_LVL(TAG, g_LogLevel,
" ReturnCode: %s (0x%08" PRIX32
")",
751 SCardGetErrorString(ret->ReturnCode), ret->ReturnCode);
753 if (ret->ReturnCode != SCARD_S_SUCCESS)
755 WLog_LVL(TAG, g_LogLevel,
"}");
759 mszA = smartcard_convert_string_list(ret->msz, ret->cBytes, unicode);
761 WLog_LVL(TAG, g_LogLevel,
" cBytes: %" PRIu32
" msz: %s", ret->cBytes, mszA);
762 WLog_LVL(TAG, g_LogLevel,
"}");
769 char* szEventState = NULL;
770 char* szCurrentState = NULL;
772 if (!WLog_IsLevelActive(WLog_Get(TAG), g_LogLevel))
775 WLog_LVL(TAG, g_LogLevel,
"GetStatusChange%s_Return {", unicode ?
"W" :
"A");
776 WLog_LVL(TAG, g_LogLevel,
" ReturnCode: %s (0x%08" PRIX32
")",
777 SCardGetErrorString(ret->ReturnCode), ret->ReturnCode);
778 WLog_LVL(TAG, g_LogLevel,
" cReaders: %" PRIu32
"", ret->cReaders);
780 for (UINT32 index = 0; index < ret->cReaders; index++)
784 szCurrentState = SCardGetReaderStateString(rgReaderState->dwCurrentState);
785 szEventState = SCardGetReaderStateString(rgReaderState->dwEventState);
786 WLog_LVL(TAG, g_LogLevel,
" [%" PRIu32
"]: dwCurrentState: %s (0x%08" PRIX32
")", index,
787 szCurrentState, rgReaderState->dwCurrentState);
788 WLog_LVL(TAG, g_LogLevel,
" [%" PRIu32
"]: dwEventState: %s (0x%08" PRIX32
")", index,
789 szEventState, rgReaderState->dwEventState);
790 WLog_LVL(TAG, g_LogLevel,
" [%" PRIu32
"]: cbAtr: %" PRIu32
" rgbAtr: %s", index,
791 rgReaderState->cbAtr,
792 smartcard_array_dump(rgReaderState->rgbAtr, rgReaderState->cbAtr, buffer,
794 free(szCurrentState);
798 WLog_LVL(TAG, g_LogLevel,
"}");
803 if (!WLog_IsLevelActive(WLog_Get(TAG), g_LogLevel))
806 WLog_LVL(TAG, g_LogLevel,
"ContextAndTwoStringW_Call {");
807 smartcard_log_context(TAG, &call->handles.hContext);
808 WLog_LVL(TAG, g_LogLevel,
" sz1=%s", call->sz1);
809 WLog_LVL(TAG, g_LogLevel,
" sz2=%s", call->sz2);
810 WLog_LVL(TAG, g_LogLevel,
"}");
815 char sz1[1024] = { 0 };
816 char sz2[1024] = { 0 };
818 if (!WLog_IsLevelActive(WLog_Get(TAG), g_LogLevel))
821 (void)ConvertWCharToUtf8(call->sz1, sz1, ARRAYSIZE(sz1));
823 (void)ConvertWCharToUtf8(call->sz2, sz2, ARRAYSIZE(sz2));
825 WLog_LVL(TAG, g_LogLevel,
"ContextAndTwoStringW_Call {");
826 smartcard_log_context(TAG, &call->handles.hContext);
827 WLog_LVL(TAG, g_LogLevel,
" sz1=%s", sz1);
828 WLog_LVL(TAG, g_LogLevel,
" sz2=%s", sz2);
829 WLog_LVL(TAG, g_LogLevel,
"}");
834 if (!WLog_IsLevelActive(WLog_Get(TAG), g_LogLevel))
837 WLog_LVL(TAG, g_LogLevel,
"GetTransmitCount_Call {");
838 smartcard_log_context(TAG, &call->handles.hContext);
839 smartcard_log_redir_handle(TAG, &call->handles.hCard);
841 WLog_LVL(TAG, g_LogLevel,
"}");
844 static void smartcard_trace_write_cache_a_call(
const WriteCacheA_Call* call)
848 if (!WLog_IsLevelActive(WLog_Get(TAG), g_LogLevel))
851 WLog_LVL(TAG, g_LogLevel,
"WriteCacheA_Call {");
853 WLog_LVL(TAG, g_LogLevel,
" szLookupName=%s", call->szLookupName);
855 smartcard_log_context(TAG, &call->Common.handles.hContext);
857 TAG,
"..CardIdentifier=%s",
858 smartcard_array_dump(call->Common.CardIdentifier,
sizeof(
UUID), buffer,
sizeof(buffer)));
859 WLog_LVL(TAG, g_LogLevel,
" FreshnessCounter=%" PRIu32, call->Common.FreshnessCounter);
860 WLog_LVL(TAG, g_LogLevel,
" cbDataLen=%" PRIu32, call->Common.cbDataLen);
863 smartcard_array_dump(call->Common.pbData, call->Common.cbDataLen, buffer,
sizeof(buffer)));
864 WLog_LVL(TAG, g_LogLevel,
"}");
867 static void smartcard_trace_write_cache_w_call(
const WriteCacheW_Call* call)
869 char tmp[1024] = { 0 };
870 char buffer[1024] = { 0 };
872 if (!WLog_IsLevelActive(WLog_Get(TAG), g_LogLevel))
875 WLog_LVL(TAG, g_LogLevel,
"WriteCacheW_Call {");
877 if (call->szLookupName)
878 (void)ConvertWCharToUtf8(call->szLookupName, tmp, ARRAYSIZE(tmp));
879 WLog_LVL(TAG, g_LogLevel,
" szLookupName=%s", tmp);
881 smartcard_log_context(TAG, &call->Common.handles.hContext);
883 TAG,
"..CardIdentifier=%s",
884 smartcard_array_dump(call->Common.CardIdentifier,
sizeof(
UUID), buffer,
sizeof(buffer)));
885 WLog_LVL(TAG, g_LogLevel,
" FreshnessCounter=%" PRIu32, call->Common.FreshnessCounter);
886 WLog_LVL(TAG, g_LogLevel,
" cbDataLen=%" PRIu32, call->Common.cbDataLen);
889 smartcard_array_dump(call->Common.pbData, call->Common.cbDataLen, buffer,
sizeof(buffer)));
890 WLog_LVL(TAG, g_LogLevel,
"}");
893 static void smartcard_trace_read_cache_a_call(
const ReadCacheA_Call* call)
897 if (!WLog_IsLevelActive(WLog_Get(TAG), g_LogLevel))
900 WLog_LVL(TAG, g_LogLevel,
"ReadCacheA_Call {");
902 WLog_LVL(TAG, g_LogLevel,
" szLookupName=%s", call->szLookupName);
903 smartcard_log_context(TAG, &call->Common.handles.hContext);
905 TAG,
"..CardIdentifier=%s",
906 smartcard_array_dump(call->Common.CardIdentifier,
sizeof(
UUID), buffer,
sizeof(buffer)));
907 WLog_LVL(TAG, g_LogLevel,
" FreshnessCounter=%" PRIu32, call->Common.FreshnessCounter);
908 WLog_LVL(TAG, g_LogLevel,
" fPbDataIsNULL=%" PRId32, call->Common.fPbDataIsNULL);
909 WLog_LVL(TAG, g_LogLevel,
" cbDataLen=%" PRIu32, call->Common.cbDataLen);
911 WLog_LVL(TAG, g_LogLevel,
"}");
914 static void smartcard_trace_read_cache_w_call(
const ReadCacheW_Call* call)
916 char tmp[1024] = { 0 };
917 char buffer[1024] = { 0 };
919 if (!WLog_IsLevelActive(WLog_Get(TAG), g_LogLevel))
922 WLog_LVL(TAG, g_LogLevel,
"ReadCacheW_Call {");
923 if (call->szLookupName)
924 (void)ConvertWCharToUtf8(call->szLookupName, tmp, ARRAYSIZE(tmp));
925 WLog_LVL(TAG, g_LogLevel,
" szLookupName=%s", tmp);
927 smartcard_log_context(TAG, &call->Common.handles.hContext);
929 TAG,
"..CardIdentifier=%s",
930 smartcard_array_dump(call->Common.CardIdentifier,
sizeof(
UUID), buffer,
sizeof(buffer)));
931 WLog_LVL(TAG, g_LogLevel,
" FreshnessCounter=%" PRIu32, call->Common.FreshnessCounter);
932 WLog_LVL(TAG, g_LogLevel,
" fPbDataIsNULL=%" PRId32, call->Common.fPbDataIsNULL);
933 WLog_LVL(TAG, g_LogLevel,
" cbDataLen=%" PRIu32, call->Common.cbDataLen);
935 WLog_LVL(TAG, g_LogLevel,
"}");
938 static void smartcard_trace_transmit_call(
const Transmit_Call* call)
940 UINT32 cbExtraBytes = 0;
941 BYTE* pbExtraBytes = NULL;
943 if (!WLog_IsLevelActive(WLog_Get(TAG), g_LogLevel))
946 WLog_LVL(TAG, g_LogLevel,
"Transmit_Call {");
947 smartcard_log_context(TAG, &call->handles.hContext);
948 smartcard_log_redir_handle(TAG, &call->handles.hCard);
950 if (call->pioSendPci)
952 cbExtraBytes = (UINT32)(call->pioSendPci->cbPciLength -
sizeof(
SCARD_IO_REQUEST));
954 WLog_LVL(TAG, g_LogLevel,
"pioSendPci: dwProtocol: %" PRIu32
" cbExtraBytes: %" PRIu32
"",
955 call->pioSendPci->dwProtocol, cbExtraBytes);
960 WLog_LVL(TAG, g_LogLevel,
"pbExtraBytes: %s",
961 smartcard_array_dump(pbExtraBytes, cbExtraBytes, buffer,
sizeof(buffer)));
966 WLog_LVL(TAG, g_LogLevel,
"pioSendPci: null");
969 WLog_LVL(TAG, g_LogLevel,
"cbSendLength: %" PRIu32
"", call->cbSendLength);
971 if (call->pbSendBuffer)
975 TAG,
"pbSendBuffer: %s",
976 smartcard_array_dump(call->pbSendBuffer, call->cbSendLength, buffer,
sizeof(buffer)));
980 WLog_LVL(TAG, g_LogLevel,
"pbSendBuffer: null");
983 if (call->pioRecvPci)
985 cbExtraBytes = (UINT32)(call->pioRecvPci->cbPciLength -
sizeof(
SCARD_IO_REQUEST));
987 WLog_LVL(TAG, g_LogLevel,
"pioRecvPci: dwProtocol: %" PRIu32
" cbExtraBytes: %" PRIu32
"",
988 call->pioRecvPci->dwProtocol, cbExtraBytes);
993 WLog_LVL(TAG, g_LogLevel,
"pbExtraBytes: %s",
994 smartcard_array_dump(pbExtraBytes, cbExtraBytes, buffer,
sizeof(buffer)));
999 WLog_LVL(TAG, g_LogLevel,
"pioRecvPci: null");
1002 WLog_LVL(TAG, g_LogLevel,
"fpbRecvBufferIsNULL: %" PRId32
" cbRecvLength: %" PRIu32
"",
1003 call->fpbRecvBufferIsNULL, call->cbRecvLength);
1004 WLog_LVL(TAG, g_LogLevel,
"}");
1009 char* szEventState = NULL;
1010 char* szCurrentState = NULL;
1012 if (!WLog_IsLevelActive(WLog_Get(TAG), g_LogLevel))
1015 WLog_LVL(TAG, g_LogLevel,
"LocateCardsByATRW_Call {");
1016 smartcard_log_context(TAG, &call->handles.hContext);
1018 for (UINT32 index = 0; index < call->cReaders; index++)
1020 char buffer[1024] = { 0 };
1021 char tmp[1024] = { 0 };
1025 if (readerState->szReader)
1026 (void)ConvertWCharToUtf8(readerState->szReader, tmp, ARRAYSIZE(tmp));
1027 WLog_LVL(TAG, g_LogLevel,
"\t[%" PRIu32
"]: szReader: %s cbAtr: %" PRIu32
"", index, tmp,
1028 readerState->cbAtr);
1029 szCurrentState = SCardGetReaderStateString(readerState->dwCurrentState);
1030 szEventState = SCardGetReaderStateString(readerState->dwEventState);
1031 WLog_LVL(TAG, g_LogLevel,
"\t[%" PRIu32
"]: dwCurrentState: %s (0x%08" PRIX32
")", index,
1032 szCurrentState, readerState->dwCurrentState);
1033 WLog_LVL(TAG, g_LogLevel,
"\t[%" PRIu32
"]: dwEventState: %s (0x%08" PRIX32
")", index,
1034 szEventState, readerState->dwEventState);
1037 TAG,
"\t[%" PRIu32
"]: cbAtr: %" PRIu32
" rgbAtr: %s", index, readerState->cbAtr,
1038 smartcard_array_dump(readerState->rgbAtr, readerState->cbAtr, buffer,
sizeof(buffer)));
1040 free(szCurrentState);
1044 WLog_LVL(TAG, g_LogLevel,
"}");
1047 static void smartcard_trace_transmit_return(
const Transmit_Return* ret)
1049 UINT32 cbExtraBytes = 0;
1050 BYTE* pbExtraBytes = NULL;
1052 if (!WLog_IsLevelActive(WLog_Get(TAG), g_LogLevel))
1055 WLog_LVL(TAG, g_LogLevel,
"Transmit_Return {");
1056 WLog_LVL(TAG, g_LogLevel,
" ReturnCode: %s (0x%08" PRIX32
")",
1057 SCardGetErrorString(ret->ReturnCode), ret->ReturnCode);
1059 if (ret->pioRecvPci)
1061 cbExtraBytes = (UINT32)(ret->pioRecvPci->cbPciLength -
sizeof(
SCARD_IO_REQUEST));
1063 WLog_LVL(TAG, g_LogLevel,
" pioRecvPci: dwProtocol: %" PRIu32
" cbExtraBytes: %" PRIu32
"",
1064 ret->pioRecvPci->dwProtocol, cbExtraBytes);
1069 WLog_LVL(TAG, g_LogLevel,
" pbExtraBytes: %s",
1070 smartcard_array_dump(pbExtraBytes, cbExtraBytes, buffer,
sizeof(buffer)));
1075 WLog_LVL(TAG, g_LogLevel,
" pioRecvPci: null");
1078 WLog_LVL(TAG, g_LogLevel,
" cbRecvLength: %" PRIu32
"", ret->cbRecvLength);
1080 if (ret->pbRecvBuffer)
1084 TAG,
" pbRecvBuffer: %s",
1085 smartcard_array_dump(ret->pbRecvBuffer, ret->cbRecvLength, buffer,
sizeof(buffer)));
1089 WLog_LVL(TAG, g_LogLevel,
" pbRecvBuffer: null");
1092 WLog_LVL(TAG, g_LogLevel,
"}");
1095 static void smartcard_trace_control_return(
const Control_Return* ret)
1097 if (!WLog_IsLevelActive(WLog_Get(TAG), g_LogLevel))
1100 WLog_LVL(TAG, g_LogLevel,
"Control_Return {");
1101 WLog_LVL(TAG, g_LogLevel,
" ReturnCode: %s (0x%08" PRIX32
")",
1102 SCardGetErrorString(ret->ReturnCode), ret->ReturnCode);
1103 WLog_LVL(TAG, g_LogLevel,
" cbOutBufferSize: %" PRIu32
"", ret->cbOutBufferSize);
1105 if (ret->pvOutBuffer)
1109 TAG,
"pvOutBuffer: %s",
1110 smartcard_array_dump(ret->pvOutBuffer, ret->cbOutBufferSize, buffer,
sizeof(buffer)));
1114 WLog_LVL(TAG, g_LogLevel,
"pvOutBuffer: null");
1117 WLog_LVL(TAG, g_LogLevel,
"}");
1120 static void smartcard_trace_control_call(
const Control_Call* call)
1122 if (!WLog_IsLevelActive(WLog_Get(TAG), g_LogLevel))
1125 WLog_LVL(TAG, g_LogLevel,
"Control_Call {");
1126 smartcard_log_context(TAG, &call->handles.hContext);
1127 smartcard_log_redir_handle(TAG, &call->handles.hCard);
1129 WLog_LVL(TAG, g_LogLevel,
1130 "dwControlCode: 0x%08" PRIX32
" cbInBufferSize: %" PRIu32
1131 " fpvOutBufferIsNULL: %" PRId32
" cbOutBufferSize: %" PRIu32
"",
1132 call->dwControlCode, call->cbInBufferSize, call->fpvOutBufferIsNULL,
1133 call->cbOutBufferSize);
1135 if (call->pvInBuffer)
1139 TAG,
"pbInBuffer: %s",
1140 smartcard_array_dump(call->pvInBuffer, call->cbInBufferSize, buffer,
sizeof(buffer)));
1144 WLog_LVL(TAG, g_LogLevel,
"pvInBuffer: null");
1147 WLog_LVL(TAG, g_LogLevel,
"}");
1150 static void smartcard_trace_set_attrib_call(
const SetAttrib_Call* call)
1154 if (!WLog_IsLevelActive(WLog_Get(TAG), g_LogLevel))
1157 WLog_LVL(TAG, g_LogLevel,
"GetAttrib_Call {");
1158 smartcard_log_context(TAG, &call->handles.hContext);
1159 smartcard_log_redir_handle(TAG, &call->handles.hCard);
1160 WLog_LVL(TAG, g_LogLevel,
"dwAttrId: 0x%08" PRIX32, call->dwAttrId);
1161 WLog_LVL(TAG, g_LogLevel,
"cbAttrLen: 0x%08" PRId32, call->cbAttrLen);
1162 WLog_LVL(TAG, g_LogLevel,
"pbAttr: %s",
1163 smartcard_array_dump(call->pbAttr, call->cbAttrLen, buffer,
sizeof(buffer)));
1164 WLog_LVL(TAG, g_LogLevel,
"}");
1167 static void smartcard_trace_get_attrib_return(
const GetAttrib_Return* ret, DWORD dwAttrId)
1170 if (!WLog_IsLevelActive(WLog_Get(TAG), g_LogLevel))
1173 WLog_LVL(TAG, g_LogLevel,
"GetAttrib_Return {");
1174 WLog_LVL(TAG, g_LogLevel,
" ReturnCode: %s (0x%08" PRIX32
")",
1175 SCardGetErrorString(ret->ReturnCode), ret->ReturnCode);
1176 WLog_LVL(TAG, g_LogLevel,
" dwAttrId: %s (0x%08" PRIX32
") cbAttrLen: 0x%08" PRIX32
"",
1177 SCardGetAttributeString(dwAttrId), dwAttrId, ret->cbAttrLen);
1179 if (dwAttrId == SCARD_ATTR_VENDOR_NAME)
1181 WLog_LVL(TAG, g_LogLevel,
" pbAttr: %.*s", ret->cbAttrLen, (
char*)ret->pbAttr);
1183 else if (dwAttrId == SCARD_ATTR_CURRENT_PROTOCOL_TYPE)
1190 attr.pb = ret->pbAttr;
1191 WLog_LVL(TAG, g_LogLevel,
" dwProtocolType: %s (0x%08" PRIX32
")",
1192 SCardGetProtocolString(*attr.pd), *attr.pd);
1195 WLog_LVL(TAG, g_LogLevel,
"}");
1198 static void smartcard_trace_get_attrib_call(
const GetAttrib_Call* call)
1200 if (!WLog_IsLevelActive(WLog_Get(TAG), g_LogLevel))
1203 WLog_LVL(TAG, g_LogLevel,
"GetAttrib_Call {");
1204 smartcard_log_context(TAG, &call->handles.hContext);
1205 smartcard_log_redir_handle(TAG, &call->handles.hCard);
1207 WLog_LVL(TAG, g_LogLevel,
1208 "dwAttrId: %s (0x%08" PRIX32
") fpbAttrIsNULL: %" PRId32
" cbAttrLen: 0x%08" PRIX32
"",
1209 SCardGetAttributeString(call->dwAttrId), call->dwAttrId, call->fpbAttrIsNULL,
1211 WLog_LVL(TAG, g_LogLevel,
"}");
1214 static void smartcard_trace_status_call(
const Status_Call* call, BOOL unicode)
1216 if (!WLog_IsLevelActive(WLog_Get(TAG), g_LogLevel))
1219 WLog_LVL(TAG, g_LogLevel,
"Status%s_Call {", unicode ?
"W" :
"A");
1220 smartcard_log_context(TAG, &call->handles.hContext);
1221 smartcard_log_redir_handle(TAG, &call->handles.hCard);
1223 WLog_LVL(TAG, g_LogLevel,
1224 "fmszReaderNamesIsNULL: %" PRId32
" cchReaderLen: %" PRIu32
" cbAtrLen: %" PRIu32
"",
1225 call->fmszReaderNamesIsNULL, call->cchReaderLen, call->cbAtrLen);
1226 WLog_LVL(TAG, g_LogLevel,
"}");
1229 static void smartcard_trace_status_return(
const Status_Return* ret, BOOL unicode)
1231 char* mszReaderNamesA = NULL;
1235 if (!WLog_IsLevelActive(WLog_Get(TAG), g_LogLevel))
1237 cBytes = ret->cBytes;
1238 if (ret->ReturnCode != SCARD_S_SUCCESS)
1240 if (cBytes == SCARD_AUTOALLOCATE)
1242 mszReaderNamesA = smartcard_convert_string_list(ret->mszReaderNames, cBytes, unicode);
1244 WLog_LVL(TAG, g_LogLevel,
"Status%s_Return {", unicode ?
"W" :
"A");
1245 WLog_LVL(TAG, g_LogLevel,
" ReturnCode: %s (0x%08" PRIX32
")",
1246 SCardGetErrorString(ret->ReturnCode), ret->ReturnCode);
1247 WLog_LVL(TAG, g_LogLevel,
" dwState: %s (0x%08" PRIX32
") dwProtocol: %s (0x%08" PRIX32
")",
1248 SCardGetCardStateString(ret->dwState), ret->dwState,
1249 SCardGetProtocolString(ret->dwProtocol), ret->dwProtocol);
1251 WLog_LVL(TAG, g_LogLevel,
" cBytes: %" PRIu32
" mszReaderNames: %s", ret->cBytes,
1254 WLog_LVL(TAG, g_LogLevel,
" cbAtrLen: %" PRIu32
" pbAtr: %s", ret->cbAtrLen,
1255 smartcard_array_dump(ret->pbAtr, ret->cbAtrLen, buffer,
sizeof(buffer)));
1256 WLog_LVL(TAG, g_LogLevel,
"}");
1257 free(mszReaderNamesA);
1260 static void smartcard_trace_state_return(
const State_Return* ret)
1265 if (!WLog_IsLevelActive(WLog_Get(TAG), g_LogLevel))
1268 state = SCardGetReaderStateString(ret->dwState);
1269 WLog_LVL(TAG, g_LogLevel,
"Reconnect_Return {");
1270 WLog_LVL(TAG, g_LogLevel,
" ReturnCode: %s (0x%08" PRIX32
")",
1271 SCardGetErrorString(ret->ReturnCode), ret->ReturnCode);
1272 WLog_LVL(TAG, g_LogLevel,
" dwState: %s (0x%08" PRIX32
")", state, ret->dwState);
1273 WLog_LVL(TAG, g_LogLevel,
" dwProtocol: %s (0x%08" PRIX32
")",
1274 SCardGetProtocolString(ret->dwProtocol), ret->dwProtocol);
1275 WLog_LVL(TAG, g_LogLevel,
" cbAtrLen: (0x%08" PRIX32
")", ret->cbAtrLen);
1276 WLog_LVL(TAG, g_LogLevel,
" rgAtr: %s",
1277 smartcard_array_dump(ret->rgAtr,
sizeof(ret->rgAtr), buffer,
sizeof(buffer)));
1278 WLog_LVL(TAG, g_LogLevel,
"}");
1285 if (!WLog_IsLevelActive(WLog_Get(TAG), g_LogLevel))
1288 WLog_LVL(TAG, g_LogLevel,
"Reconnect_Return {");
1289 WLog_LVL(TAG, g_LogLevel,
" ReturnCode: %s (0x%08" PRIX32
")",
1290 SCardGetErrorString(ret->ReturnCode), ret->ReturnCode);
1291 WLog_LVL(TAG, g_LogLevel,
" dwActiveProtocol: %s (0x%08" PRIX32
")",
1292 SCardGetProtocolString(ret->dwActiveProtocol), ret->dwActiveProtocol);
1293 WLog_LVL(TAG, g_LogLevel,
"}");
1296 static void smartcard_trace_connect_a_call(
const ConnectA_Call* call)
1299 if (!WLog_IsLevelActive(WLog_Get(TAG), g_LogLevel))
1302 WLog_LVL(TAG, g_LogLevel,
"ConnectA_Call {");
1303 smartcard_log_context(TAG, &call->Common.handles.hContext);
1305 WLog_LVL(TAG, g_LogLevel,
1306 "szReader: %s dwShareMode: %s (0x%08" PRIX32
") dwPreferredProtocols: %s (0x%08" PRIX32
1308 call->szReader, SCardGetShareModeString(call->Common.dwShareMode),
1309 call->Common.dwShareMode, SCardGetProtocolString(call->Common.dwPreferredProtocols),
1310 call->Common.dwPreferredProtocols);
1311 WLog_LVL(TAG, g_LogLevel,
"}");
1314 static void smartcard_trace_connect_w_call(
const ConnectW_Call* call)
1316 char szReaderA[1024] = { 0 };
1318 if (!WLog_IsLevelActive(WLog_Get(TAG), g_LogLevel))
1322 (void)ConvertWCharToUtf8(call->szReader, szReaderA, ARRAYSIZE(szReaderA));
1323 WLog_LVL(TAG, g_LogLevel,
"ConnectW_Call {");
1324 smartcard_log_context(TAG, &call->Common.handles.hContext);
1326 WLog_LVL(TAG, g_LogLevel,
1327 "szReader: %s dwShareMode: %s (0x%08" PRIX32
") dwPreferredProtocols: %s (0x%08" PRIX32
1329 szReaderA, SCardGetShareModeString(call->Common.dwShareMode), call->Common.dwShareMode,
1330 SCardGetProtocolString(call->Common.dwPreferredProtocols),
1331 call->Common.dwPreferredProtocols);
1332 WLog_LVL(TAG, g_LogLevel,
"}");
1338 if (!WLog_IsLevelActive(WLog_Get(TAG), g_LogLevel))
1341 WLog_LVL(TAG, g_LogLevel,
"%s_Call {", name);
1342 smartcard_log_context(TAG, &call->handles.hContext);
1343 smartcard_log_redir_handle(TAG, &call->handles.hCard);
1345 WLog_LVL(TAG, g_LogLevel,
"dwDisposition: %s (0x%08" PRIX32
")",
1346 SCardGetDispositionString(call->dwDisposition), call->dwDisposition);
1347 WLog_LVL(TAG, g_LogLevel,
"}");
1353 if (!WLog_IsLevelActive(WLog_Get(TAG), g_LogLevel))
1356 WLog_LVL(TAG, g_LogLevel,
"EstablishContext_Call {");
1357 WLog_LVL(TAG, g_LogLevel,
"dwScope: %s (0x%08" PRIX32
")", SCardGetScopeString(call->dwScope),
1359 WLog_LVL(TAG, g_LogLevel,
"}");
1365 if (!WLog_IsLevelActive(WLog_Get(TAG), g_LogLevel))
1368 WLog_LVL(TAG, g_LogLevel,
"EstablishContext_Return {");
1369 WLog_LVL(TAG, g_LogLevel,
" ReturnCode: %s (0x%08" PRIX32
")",
1370 SCardGetErrorString(ret->ReturnCode), ret->ReturnCode);
1371 smartcard_log_context(TAG, &ret->hContext);
1373 WLog_LVL(TAG, g_LogLevel,
"}");
1376 void smartcard_trace_long_return(
const Long_Return* ret,
const char* name)
1378 if (!WLog_IsLevelActive(WLog_Get(TAG), g_LogLevel))
1381 WLog_LVL(TAG, g_LogLevel,
"%s_Return {", name);
1382 WLog_LVL(TAG, g_LogLevel,
" ReturnCode: %s (0x%08" PRIX32
")",
1383 SCardGetErrorString(ret->ReturnCode), ret->ReturnCode);
1384 WLog_LVL(TAG, g_LogLevel,
"}");
1387 static void smartcard_trace_connect_return(
const Connect_Return* ret)
1389 if (!WLog_IsLevelActive(WLog_Get(TAG), g_LogLevel))
1392 WLog_LVL(TAG, g_LogLevel,
"Connect_Return {");
1393 WLog_LVL(TAG, g_LogLevel,
" ReturnCode: %s (0x%08" PRIX32
")",
1394 SCardGetErrorString(ret->ReturnCode), ret->ReturnCode);
1395 smartcard_log_context(TAG, &ret->hContext);
1396 smartcard_log_redir_handle(TAG, &ret->hCard);
1398 WLog_LVL(TAG, g_LogLevel,
" dwActiveProtocol: %s (0x%08" PRIX32
")",
1399 SCardGetProtocolString(ret->dwActiveProtocol), ret->dwActiveProtocol);
1400 WLog_LVL(TAG, g_LogLevel,
"}");
1403 static void smartcard_trace_reconnect_call(
const Reconnect_Call* call)
1405 if (!WLog_IsLevelActive(WLog_Get(TAG), g_LogLevel))
1408 WLog_LVL(TAG, g_LogLevel,
"Reconnect_Call {");
1409 smartcard_log_context(TAG, &call->handles.hContext);
1410 smartcard_log_redir_handle(TAG, &call->handles.hCard);
1412 WLog_LVL(TAG, g_LogLevel,
1413 "dwShareMode: %s (0x%08" PRIX32
") dwPreferredProtocols: %s (0x%08" PRIX32
1414 ") dwInitialization: %s (0x%08" PRIX32
")",
1415 SCardGetShareModeString(call->dwShareMode), call->dwShareMode,
1416 SCardGetProtocolString(call->dwPreferredProtocols), call->dwPreferredProtocols,
1417 SCardGetDispositionString(call->dwInitialization), call->dwInitialization);
1418 WLog_LVL(TAG, g_LogLevel,
"}");
1423 if (!WLog_IsLevelActive(WLog_Get(TAG), g_LogLevel))
1426 WLog_LVL(TAG, g_LogLevel,
"GetDeviceTypeId_Return {");
1427 WLog_LVL(TAG, g_LogLevel,
" ReturnCode: %s (0x%08" PRIX32
")",
1428 SCardGetErrorString(ret->ReturnCode), ret->ReturnCode);
1429 WLog_LVL(TAG, g_LogLevel,
" dwDeviceId=%08" PRIx32, ret->dwDeviceId);
1431 WLog_LVL(TAG, g_LogLevel,
"}");
1435 CHAR** pszReaderName)
1438 UINT32 pbContextNdrPtr = 0;
1439 LONG status = smartcard_unpack_redir_scard_context(s, phContext, &index, &pbContextNdrPtr);
1440 if (status != SCARD_S_SUCCESS)
1443 if (!smartcard_ndr_pointer_read(s, &index, NULL))
1444 return ERROR_INVALID_DATA;
1446 status = smartcard_unpack_redir_scard_context_ref(s, pbContextNdrPtr, phContext);
1447 if (status != SCARD_S_SUCCESS)
1450 status = smartcard_ndr_read_a(s, pszReaderName, NDR_PTR_FULL);
1451 if (status != SCARD_S_SUCCESS)
1454 smartcard_trace_context_and_string_call_a(__func__, phContext, *pszReaderName);
1455 return SCARD_S_SUCCESS;
1459 WCHAR** pszReaderName)
1462 UINT32 pbContextNdrPtr = 0;
1464 LONG status = smartcard_unpack_redir_scard_context(s, phContext, &index, &pbContextNdrPtr);
1465 if (status != SCARD_S_SUCCESS)
1468 if (!smartcard_ndr_pointer_read(s, &index, NULL))
1469 return ERROR_INVALID_DATA;
1471 status = smartcard_unpack_redir_scard_context_ref(s, pbContextNdrPtr, phContext);
1472 if (status != SCARD_S_SUCCESS)
1475 status = smartcard_ndr_read_w(s, pszReaderName, NDR_PTR_FULL);
1476 if (status != SCARD_S_SUCCESS)
1479 smartcard_trace_context_and_string_call_w(__func__, phContext, *pszReaderName);
1480 return SCARD_S_SUCCESS;
1483 LONG smartcard_unpack_common_type_header(
wStream* s)
1487 UINT8 endianness = 0;
1488 UINT16 commonHeaderLength = 0;
1490 if (!Stream_CheckAndLogRequiredLength(TAG, s, 8))
1491 return STATUS_BUFFER_TOO_SMALL;
1494 Stream_Read_UINT8(s, version);
1495 Stream_Read_UINT8(s, endianness);
1496 Stream_Read_UINT16(s, commonHeaderLength);
1497 Stream_Read_UINT32(s, filler);
1501 WLog_WARN(TAG,
"Unsupported CommonTypeHeader Version %" PRIu8
"", version);
1502 return STATUS_INVALID_PARAMETER;
1505 if (endianness != 0x10)
1507 WLog_WARN(TAG,
"Unsupported CommonTypeHeader Endianness %" PRIu8
"", endianness);
1508 return STATUS_INVALID_PARAMETER;
1511 if (commonHeaderLength != 8)
1513 WLog_WARN(TAG,
"Unsupported CommonTypeHeader CommonHeaderLength %" PRIu16
"",
1514 commonHeaderLength);
1515 return STATUS_INVALID_PARAMETER;
1518 if (filler != 0xCCCCCCCC)
1520 WLog_WARN(TAG,
"Unexpected CommonTypeHeader Filler 0x%08" PRIX32
"", filler);
1521 return STATUS_INVALID_PARAMETER;
1524 return SCARD_S_SUCCESS;
1527 void smartcard_pack_common_type_header(
wStream* s)
1529 Stream_Write_UINT8(s, 1);
1530 Stream_Write_UINT8(s, 0x10);
1531 Stream_Write_UINT16(s, 8);
1532 Stream_Write_UINT32(s, 0xCCCCCCCC);
1535 LONG smartcard_unpack_private_type_header(
wStream* s)
1538 UINT32 objectBufferLength = 0;
1540 if (!Stream_CheckAndLogRequiredLength(TAG, s, 8))
1541 return STATUS_BUFFER_TOO_SMALL;
1543 Stream_Read_UINT32(s, objectBufferLength);
1544 Stream_Read_UINT32(s, filler);
1546 if (filler != 0x00000000)
1548 WLog_WARN(TAG,
"Unexpected PrivateTypeHeader Filler 0x%08" PRIX32
"", filler);
1549 return STATUS_INVALID_PARAMETER;
1552 if (!Stream_CheckAndLogRequiredLength(TAG, s, objectBufferLength))
1553 return STATUS_INVALID_PARAMETER;
1555 return SCARD_S_SUCCESS;
1558 void smartcard_pack_private_type_header(
wStream* s, UINT32 objectBufferLength)
1560 Stream_Write_UINT32(s, objectBufferLength);
1561 Stream_Write_UINT32(s, 0x00000000);
1564 LONG smartcard_unpack_read_size_align(
wStream* s,
size_t size, UINT32 alignment)
1569 size = (size + alignment - 1) & ~(alignment - 1);
1573 Stream_Seek(s, pad);
1578 LONG smartcard_pack_write_size_align(
wStream* s,
size_t size, UINT32 alignment)
1583 size = (size + alignment - 1) & ~(alignment - 1);
1588 if (!Stream_EnsureRemainingCapacity(s, pad))
1590 WLog_ERR(TAG,
"Stream_EnsureRemainingCapacity failed!");
1591 return SCARD_F_INTERNAL_ERROR;
1594 Stream_Zero(s, pad);
1597 return SCARD_S_SUCCESS;
1602 SCARDCONTEXT hContext = { 0 };
1604 if ((context->cbContext !=
sizeof(ULONG_PTR)) && (context->cbContext != 0))
1607 "REDIR_SCARDCONTEXT does not match native size: Actual: %" PRIu32
1608 ", Expected: %" PRIuz
"",
1609 context->cbContext,
sizeof(ULONG_PTR));
1613 if (context->cbContext)
1614 CopyMemory(&hContext, &(context->pbContext), context->cbContext);
1619 void smartcard_scard_context_native_to_redir(
REDIR_SCARDCONTEXT* context, SCARDCONTEXT hContext)
1621 WINPR_ASSERT(context);
1623 context->cbContext =
sizeof(ULONG_PTR);
1624 CopyMemory(&(context->pbContext), &hContext, context->cbContext);
1629 SCARDHANDLE hCard = 0;
1631 if (handle->cbHandle == 0)
1634 if (handle->cbHandle !=
sizeof(ULONG_PTR))
1637 "REDIR_SCARDHANDLE does not match native size: Actual: %" PRIu32
1638 ", Expected: %" PRIuz
"",
1639 handle->cbHandle,
sizeof(ULONG_PTR));
1643 if (handle->cbHandle)
1644 CopyMemory(&hCard, &(handle->pbHandle), handle->cbHandle);
1649 void smartcard_scard_handle_native_to_redir(
REDIR_SCARDHANDLE* handle, SCARDHANDLE hCard)
1651 WINPR_ASSERT(handle);
1653 handle->cbHandle =
sizeof(ULONG_PTR);
1654 CopyMemory(&(handle->pbHandle), &hCard, handle->cbHandle);
1658 UINT32* ppbContextNdrPtr,
const char* file,
1659 const char*
function,
int line)
1661 UINT32 pbContextNdrPtr = 0;
1664 WINPR_ASSERT(context);
1668 if (!Stream_CheckAndLogRequiredLength(TAG, s, 4))
1669 return STATUS_BUFFER_TOO_SMALL;
1671 Stream_Read_UINT32(s, context->cbContext);
1673 if (!Stream_CheckAndLogRequiredLength(TAG, s, context->cbContext))
1674 return STATUS_BUFFER_TOO_SMALL;
1676 if ((context->cbContext != 0) && (context->cbContext != 4) && (context->cbContext != 8))
1678 WLog_WARN(TAG,
"REDIR_SCARDCONTEXT length is not 0, 4 or 8: %" PRIu32
"",
1679 context->cbContext);
1680 return STATUS_INVALID_PARAMETER;
1683 if (!smartcard_ndr_pointer_read_(s, index, &pbContextNdrPtr, file,
function,
1684 WINPR_ASSERTING_INT_CAST(
size_t, line)))
1685 return ERROR_INVALID_DATA;
1687 if (((context->cbContext == 0) && pbContextNdrPtr) ||
1688 ((context->cbContext != 0) && !pbContextNdrPtr))
1691 "REDIR_SCARDCONTEXT cbContext (%" PRIu32
") pbContextNdrPtr (%" PRIu32
1693 context->cbContext, pbContextNdrPtr);
1694 return STATUS_INVALID_PARAMETER;
1697 if (!Stream_CheckAndLogRequiredLength(TAG, s, context->cbContext))
1698 return STATUS_INVALID_PARAMETER;
1700 *ppbContextNdrPtr = pbContextNdrPtr;
1701 return SCARD_S_SUCCESS;
1706 const UINT32 pbContextNdrPtr = 0x00020000 + *index * 4;
1708 if (context->cbContext != 0)
1710 Stream_Write_UINT32(s, context->cbContext);
1711 Stream_Write_UINT32(s, pbContextNdrPtr);
1712 *index = *index + 1;
1717 return SCARD_S_SUCCESS;
1720 LONG smartcard_unpack_redir_scard_context_ref(
wStream* s, UINT32 pbContextNdrPtr,
1725 WINPR_ASSERT(context);
1726 if (context->cbContext == 0)
1727 return SCARD_S_SUCCESS;
1729 if (!Stream_CheckAndLogRequiredLength(TAG, s, 4))
1730 return STATUS_BUFFER_TOO_SMALL;
1732 Stream_Read_UINT32(s, length);
1734 if (length != context->cbContext)
1736 WLog_WARN(TAG,
"REDIR_SCARDCONTEXT length (%" PRIu32
") cbContext (%" PRIu32
") mismatch",
1737 length, context->cbContext);
1738 return STATUS_INVALID_PARAMETER;
1741 if ((context->cbContext != 0) && (context->cbContext != 4) && (context->cbContext != 8))
1743 WLog_WARN(TAG,
"REDIR_SCARDCONTEXT length is not 4 or 8: %" PRIu32
"", context->cbContext);
1744 return STATUS_INVALID_PARAMETER;
1747 if (!Stream_CheckAndLogRequiredLength(TAG, s, context->cbContext))
1748 return STATUS_BUFFER_TOO_SMALL;
1750 if (context->cbContext)
1751 Stream_Read(s, &(context->pbContext), context->cbContext);
1753 ZeroMemory(&(context->pbContext),
sizeof(context->pbContext));
1755 return SCARD_S_SUCCESS;
1761 Stream_Write_UINT32(s, context->cbContext);
1763 if (context->cbContext)
1765 Stream_Write(s, &(context->pbContext), context->cbContext);
1768 return SCARD_S_SUCCESS;
1772 const char* file,
const char*
function,
int line)
1774 WINPR_ASSERT(handle);
1777 if (!Stream_CheckAndLogRequiredLength(TAG, s, 4))
1778 return STATUS_BUFFER_TOO_SMALL;
1780 Stream_Read_UINT32(s, handle->cbHandle);
1782 if (!Stream_CheckAndLogRequiredLength(TAG, s, handle->cbHandle))
1783 return STATUS_BUFFER_TOO_SMALL;
1785 if (!smartcard_ndr_pointer_read_(s, index, NULL, file,
function,
1786 WINPR_ASSERTING_INT_CAST(
size_t, line)))
1787 return ERROR_INVALID_DATA;
1789 return SCARD_S_SUCCESS;
1794 const UINT32 pbContextNdrPtr = 0x00020000 + *index * 4;
1796 if (handle->cbHandle != 0)
1798 Stream_Write_UINT32(s, handle->cbHandle);
1799 Stream_Write_UINT32(s, pbContextNdrPtr);
1800 *index = *index + 1;
1804 return SCARD_S_SUCCESS;
1811 if (!Stream_CheckAndLogRequiredLength(TAG, s, 4))
1812 return STATUS_BUFFER_TOO_SMALL;
1814 Stream_Read_UINT32(s, length);
1816 if (length != handle->cbHandle)
1818 WLog_WARN(TAG,
"REDIR_SCARDHANDLE length (%" PRIu32
") cbHandle (%" PRIu32
") mismatch",
1819 length, handle->cbHandle);
1820 return STATUS_INVALID_PARAMETER;
1823 if ((handle->cbHandle != 4) && (handle->cbHandle != 8))
1825 WLog_WARN(TAG,
"REDIR_SCARDHANDLE length is not 4 or 8: %" PRIu32
"", handle->cbHandle);
1826 return STATUS_INVALID_PARAMETER;
1829 if (!Stream_CheckAndLogRequiredLength(TAG, s, handle->cbHandle))
1830 return STATUS_BUFFER_TOO_SMALL;
1832 if (handle->cbHandle)
1833 Stream_Read(s, &(handle->pbHandle), handle->cbHandle);
1835 return SCARD_S_SUCCESS;
1841 Stream_Write_UINT32(s, handle->cbHandle);
1843 if (handle->cbHandle)
1844 Stream_Write(s, &(handle->pbHandle), handle->cbHandle);
1846 return SCARD_S_SUCCESS;
1851 if (!Stream_CheckAndLogRequiredLength(TAG, s, 4))
1852 return STATUS_BUFFER_TOO_SMALL;
1854 Stream_Read_UINT32(s, call->dwScope);
1855 smartcard_trace_establish_context_call(call);
1856 return SCARD_S_SUCCESS;
1864 smartcard_trace_establish_context_return(ret);
1865 if (ret->ReturnCode != SCARD_S_SUCCESS)
1866 return ret->ReturnCode;
1868 if ((status = smartcard_pack_redir_scard_context(s, &(ret->hContext), &index)))
1871 return smartcard_pack_redir_scard_context_ref(s, &(ret->hContext));
1877 UINT32 pbContextNdrPtr = 0;
1879 LONG status = smartcard_unpack_redir_scard_context(s, &(call->handles.hContext), &index,
1881 if (status != SCARD_S_SUCCESS)
1884 if ((status = smartcard_unpack_redir_scard_context_ref(s, pbContextNdrPtr,
1885 &(call->handles.hContext))))
1886 WLog_ERR(TAG,
"smartcard_unpack_redir_scard_context_ref failed with error %" PRId32
"",
1889 smartcard_trace_context_call(call, name);
1896 UINT32 pbContextNdrPtr = 0;
1898 LONG status = smartcard_unpack_redir_scard_context(s, &(call->handles.hContext), &index,
1901 if (status != SCARD_S_SUCCESS)
1904 if (!Stream_CheckAndLogRequiredLength(TAG, s, 8))
1905 return STATUS_BUFFER_TOO_SMALL;
1907 Stream_Read_INT32(s, call->fmszGroupsIsNULL);
1908 Stream_Read_UINT32(s, call->cchGroups);
1910 smartcard_unpack_redir_scard_context_ref(s, pbContextNdrPtr, &(call->handles.hContext));
1912 if (status != SCARD_S_SUCCESS)
1915 smartcard_trace_list_reader_groups_call(call, unicode);
1916 return SCARD_S_SUCCESS;
1923 DWORD cBytes = ret->cBytes;
1926 smartcard_trace_list_reader_groups_return(ret, unicode);
1927 if (ret->ReturnCode != SCARD_S_SUCCESS)
1929 if (cBytes == SCARD_AUTOALLOCATE)
1932 if (!Stream_EnsureRemainingCapacity(s, 4))
1933 return SCARD_E_NO_MEMORY;
1935 Stream_Write_UINT32(s, cBytes);
1936 if (!smartcard_ndr_pointer_write(s, &index, cBytes))
1937 return SCARD_E_NO_MEMORY;
1939 status = smartcard_ndr_write(s, ret->msz, cBytes, 1, NDR_PTR_SIMPLE);
1940 if (status != SCARD_S_SUCCESS)
1942 return ret->ReturnCode;
1948 UINT32 mszGroupsNdrPtr = 0;
1949 UINT32 pbContextNdrPtr = 0;
1951 call->mszGroups = NULL;
1953 LONG status = smartcard_unpack_redir_scard_context(s, &(call->handles.hContext), &index,
1955 if (status != SCARD_S_SUCCESS)
1958 if (!Stream_CheckAndLogRequiredLength(TAG, s, 16))
1959 return STATUS_BUFFER_TOO_SMALL;
1961 Stream_Read_UINT32(s, call->cBytes);
1962 if (!smartcard_ndr_pointer_read(s, &index, &mszGroupsNdrPtr))
1963 return ERROR_INVALID_DATA;
1964 Stream_Read_INT32(s, call->fmszReadersIsNULL);
1965 Stream_Read_UINT32(s, call->cchReaders);
1967 if ((status = smartcard_unpack_redir_scard_context_ref(s, pbContextNdrPtr,
1968 &(call->handles.hContext))))
1971 if (mszGroupsNdrPtr)
1973 status = smartcard_ndr_read(s, &call->mszGroups, call->cBytes, 1, NDR_PTR_SIMPLE);
1974 if (status != SCARD_S_SUCCESS)
1978 smartcard_trace_list_readers_call(call, unicode);
1979 return SCARD_S_SUCCESS;
1986 UINT32 size = ret->cBytes;
1988 smartcard_trace_list_readers_return(ret, unicode);
1989 if (ret->ReturnCode != SCARD_S_SUCCESS)
1992 if (!Stream_EnsureRemainingCapacity(s, 4))
1994 WLog_ERR(TAG,
"Stream_EnsureRemainingCapacity failed!");
1995 return SCARD_F_INTERNAL_ERROR;
1998 Stream_Write_UINT32(s, size);
1999 if (!smartcard_ndr_pointer_write(s, &index, size))
2000 return SCARD_E_NO_MEMORY;
2002 status = smartcard_ndr_write(s, ret->msz, size, 1, NDR_PTR_SIMPLE);
2003 if (status != SCARD_S_SUCCESS)
2005 return ret->ReturnCode;
2009 UINT32* ppbContextNdrPtr)
2011 LONG status = smartcard_unpack_redir_scard_context(s, &(common->handles.hContext), index,
2013 if (status != SCARD_S_SUCCESS)
2016 if (!Stream_CheckAndLogRequiredLength(TAG, s, 8))
2017 return STATUS_BUFFER_TOO_SMALL;
2019 Stream_Read_UINT32(s, common->dwShareMode);
2020 Stream_Read_UINT32(s, common->dwPreferredProtocols);
2021 return SCARD_S_SUCCESS;
2028 UINT32 pbContextNdrPtr = 0;
2029 call->szReader = NULL;
2031 if (!smartcard_ndr_pointer_read(s, &index, NULL))
2032 return ERROR_INVALID_DATA;
2034 if ((status = smartcard_unpack_connect_common(s, &(call->Common), &index, &pbContextNdrPtr)))
2036 WLog_ERR(TAG,
"smartcard_unpack_connect_common failed with error %" PRId32
"", status);
2040 status = smartcard_ndr_read_a(s, &call->szReader, NDR_PTR_FULL);
2041 if (status != SCARD_S_SUCCESS)
2044 if ((status = smartcard_unpack_redir_scard_context_ref(s, pbContextNdrPtr,
2045 &(call->Common.handles.hContext))))
2046 WLog_ERR(TAG,
"smartcard_unpack_redir_scard_context_ref failed with error %" PRId32
"",
2049 smartcard_trace_connect_a_call(call);
2057 UINT32 pbContextNdrPtr = 0;
2059 call->szReader = NULL;
2061 if (!smartcard_ndr_pointer_read(s, &index, NULL))
2062 return ERROR_INVALID_DATA;
2064 if ((status = smartcard_unpack_connect_common(s, &(call->Common), &index, &pbContextNdrPtr)))
2066 WLog_ERR(TAG,
"smartcard_unpack_connect_common failed with error %" PRId32
"", status);
2070 status = smartcard_ndr_read_w(s, &call->szReader, NDR_PTR_FULL);
2071 if (status != SCARD_S_SUCCESS)
2074 if ((status = smartcard_unpack_redir_scard_context_ref(s, pbContextNdrPtr,
2075 &(call->Common.handles.hContext))))
2076 WLog_ERR(TAG,
"smartcard_unpack_redir_scard_context_ref failed with error %" PRId32
"",
2079 smartcard_trace_connect_w_call(call);
2088 smartcard_trace_connect_return(ret);
2090 status = smartcard_pack_redir_scard_context(s, &ret->hContext, &index);
2091 if (status != SCARD_S_SUCCESS)
2094 status = smartcard_pack_redir_scard_handle(s, &ret->hCard, &index);
2095 if (status != SCARD_S_SUCCESS)
2098 if (!Stream_EnsureRemainingCapacity(s, 4))
2099 return SCARD_E_NO_MEMORY;
2101 Stream_Write_UINT32(s, ret->dwActiveProtocol);
2102 status = smartcard_pack_redir_scard_context_ref(s, &ret->hContext);
2103 if (status != SCARD_S_SUCCESS)
2105 return smartcard_pack_redir_scard_handle_ref(s, &(ret->hCard));
2111 UINT32 pbContextNdrPtr = 0;
2112 LONG status = smartcard_unpack_redir_scard_context(s, &(call->handles.hContext), &index,
2114 if (status != SCARD_S_SUCCESS)
2117 status = smartcard_unpack_redir_scard_handle(s, &(call->handles.hCard), &index);
2118 if (status != SCARD_S_SUCCESS)
2121 if (!Stream_CheckAndLogRequiredLength(TAG, s, 12))
2122 return STATUS_BUFFER_TOO_SMALL;
2124 Stream_Read_UINT32(s, call->dwShareMode);
2125 Stream_Read_UINT32(s, call->dwPreferredProtocols);
2126 Stream_Read_UINT32(s, call->dwInitialization);
2128 if ((status = smartcard_unpack_redir_scard_context_ref(s, pbContextNdrPtr,
2129 &(call->handles.hContext))))
2131 WLog_ERR(TAG,
"smartcard_unpack_redir_scard_context_ref failed with error %" PRId32
"",
2136 if ((status = smartcard_unpack_redir_scard_handle_ref(s, &(call->handles.hCard))))
2137 WLog_ERR(TAG,
"smartcard_unpack_redir_scard_handle_ref failed with error %" PRId32
"",
2140 smartcard_trace_reconnect_call(call);
2146 smartcard_trace_reconnect_return(ret);
2148 if (!Stream_EnsureRemainingCapacity(s, 4))
2149 return SCARD_E_NO_MEMORY;
2150 Stream_Write_UINT32(s, ret->dwActiveProtocol);
2151 return ret->ReturnCode;
2158 UINT32 pbContextNdrPtr = 0;
2160 LONG status = smartcard_unpack_redir_scard_context(s, &(call->handles.hContext), &index,
2162 if (status != SCARD_S_SUCCESS)
2165 status = smartcard_unpack_redir_scard_handle(s, &(call->handles.hCard), &index);
2166 if (status != SCARD_S_SUCCESS)
2169 if (!Stream_CheckAndLogRequiredLength(TAG, s, 4))
2170 return STATUS_BUFFER_TOO_SMALL;
2172 Stream_Read_UINT32(s, call->dwDisposition);
2174 if ((status = smartcard_unpack_redir_scard_context_ref(s, pbContextNdrPtr,
2175 &(call->handles.hContext))))
2178 if ((status = smartcard_unpack_redir_scard_handle_ref(s, &(call->handles.hCard))))
2181 smartcard_trace_hcard_and_disposition_call(call, name);
2187 if (!WLog_IsLevelActive(WLog_Get(TAG), g_LogLevel))
2190 WLog_LVL(TAG, g_LogLevel,
"GetStatusChangeA_Call {");
2191 smartcard_log_context(TAG, &call->handles.hContext);
2193 WLog_LVL(TAG, g_LogLevel,
"dwTimeOut: 0x%08" PRIX32
" cReaders: %" PRIu32
"", call->dwTimeOut,
2196 for (UINT32 index = 0; index < call->cReaders; index++)
2199 WLog_LVL(TAG, g_LogLevel,
"\t[%" PRIu32
"]: szReader: %s cbAtr: %" PRIu32
"", index,
2200 readerState->szReader, readerState->cbAtr);
2201 char* szCurrentState = SCardGetReaderStateString(readerState->dwCurrentState);
2202 char* szEventState = SCardGetReaderStateString(readerState->dwEventState);
2203 WLog_LVL(TAG, g_LogLevel,
"\t[%" PRIu32
"]: dwCurrentState: %s (0x%08" PRIX32
")", index,
2204 szCurrentState, readerState->dwCurrentState);
2205 WLog_LVL(TAG, g_LogLevel,
"\t[%" PRIu32
"]: dwEventState: %s (0x%08" PRIX32
")", index,
2206 szEventState, readerState->dwEventState);
2207 free(szCurrentState);
2211 WLog_LVL(TAG, g_LogLevel,
"}");
2215 UINT32 cReaders, UINT32* ptrIndex)
2217 LONG status = SCARD_E_NO_MEMORY;
2219 if (!Stream_CheckAndLogRequiredLength(TAG, s, 4))
2222 const UINT32 len = Stream_Get_UINT32(s);
2223 if (len != cReaders)
2225 WLog_ERR(TAG,
"Count mismatch when reading LPSCARD_READERSTATEA");
2231 BOOL* states = calloc(cReaders,
sizeof(BOOL));
2232 if (!rgReaderStates || !states)
2234 status = ERROR_INVALID_DATA;
2236 for (UINT32 index = 0; index < cReaders; index++)
2238 UINT32 ptr = UINT32_MAX;
2241 if (!Stream_CheckAndLogRequiredLength(TAG, s, 52))
2244 if (!smartcard_ndr_pointer_read(s, ptrIndex, &ptr))
2250 states[index] = ptr != 0;
2251 Stream_Read_UINT32(s, readerState->dwCurrentState);
2252 Stream_Read_UINT32(s, readerState->dwEventState);
2253 Stream_Read_UINT32(s, readerState->cbAtr);
2254 Stream_Read(s, readerState->rgbAtr, 36);
2257 for (UINT32 index = 0; index < cReaders; index++)
2264 status = smartcard_ndr_read_a(s, &readerState->szReader, NDR_PTR_FULL);
2265 if (status != SCARD_S_SUCCESS)
2269 *ppcReaders = rgReaderStates;
2271 return SCARD_S_SUCCESS;
2275 for (UINT32 index = 0; index < cReaders; index++)
2278 free(readerState->szReader);
2281 free(rgReaderStates);
2287 UINT32 cReaders, UINT32* ptrIndex)
2289 LONG status = SCARD_E_NO_MEMORY;
2291 if (!Stream_CheckAndLogRequiredLength(TAG, s, 4))
2294 const UINT32 len = Stream_Get_UINT32(s);
2295 if (len != cReaders)
2297 WLog_ERR(TAG,
"Count mismatch when reading LPSCARD_READERSTATEW");
2303 BOOL* states = calloc(cReaders,
sizeof(BOOL));
2305 if (!rgReaderStates || !states)
2308 status = ERROR_INVALID_DATA;
2309 for (UINT32 index = 0; index < cReaders; index++)
2311 UINT32 ptr = UINT32_MAX;
2314 if (!Stream_CheckAndLogRequiredLength(TAG, s, 52))
2317 if (!smartcard_ndr_pointer_read(s, ptrIndex, &ptr))
2323 states[index] = ptr != 0;
2324 Stream_Read_UINT32(s, readerState->dwCurrentState);
2325 Stream_Read_UINT32(s, readerState->dwEventState);
2326 Stream_Read_UINT32(s, readerState->cbAtr);
2327 Stream_Read(s, readerState->rgbAtr, 36);
2330 for (UINT32 index = 0; index < cReaders; index++)
2338 status = smartcard_ndr_read_w(s, &readerState->szReader, NDR_PTR_FULL);
2339 if (status != SCARD_S_SUCCESS)
2343 *ppcReaders = rgReaderStates;
2345 return SCARD_S_SUCCESS;
2349 for (UINT32 index = 0; index < cReaders; index++)
2352 free(readerState->szReader);
2355 free(rgReaderStates);
2368 UINT32 pbContextNdrPtr = 0;
2370 call->rgReaderStates = NULL;
2372 LONG status = smartcard_unpack_redir_scard_context(s, &(call->handles.hContext), &index,
2374 if (status != SCARD_S_SUCCESS)
2377 if (!Stream_CheckAndLogRequiredLength(TAG, s, 12))
2378 return STATUS_BUFFER_TOO_SMALL;
2380 Stream_Read_UINT32(s, call->dwTimeOut);
2381 Stream_Read_UINT32(s, call->cReaders);
2382 if (!smartcard_ndr_pointer_read(s, &index, &ndrPtr))
2383 return ERROR_INVALID_DATA;
2385 if ((status = smartcard_unpack_redir_scard_context_ref(s, pbContextNdrPtr,
2386 &(call->handles.hContext))))
2391 status = smartcard_unpack_reader_state_a(s, &call->rgReaderStates, call->cReaders, &index);
2392 if (status != SCARD_S_SUCCESS)
2396 smartcard_trace_get_status_change_a_call(call);
2397 return SCARD_S_SUCCESS;
2404 UINT32 pbContextNdrPtr = 0;
2406 call->rgReaderStates = NULL;
2408 LONG status = smartcard_unpack_redir_scard_context(s, &(call->handles.hContext), &index,
2410 if (status != SCARD_S_SUCCESS)
2413 if (!Stream_CheckAndLogRequiredLength(TAG, s, 12))
2414 return STATUS_BUFFER_TOO_SMALL;
2416 Stream_Read_UINT32(s, call->dwTimeOut);
2417 Stream_Read_UINT32(s, call->cReaders);
2418 if (!smartcard_ndr_pointer_read(s, &index, &ndrPtr))
2419 return ERROR_INVALID_DATA;
2421 if ((status = smartcard_unpack_redir_scard_context_ref(s, pbContextNdrPtr,
2422 &(call->handles.hContext))))
2427 status = smartcard_unpack_reader_state_w(s, &call->rgReaderStates, call->cReaders, &index);
2428 if (status != SCARD_S_SUCCESS)
2432 smartcard_trace_get_status_change_w_call(call);
2433 return SCARD_S_SUCCESS;
2440 DWORD cReaders = ret->cReaders;
2443 smartcard_trace_get_status_change_return(ret, unicode);
2444 if (ret->ReturnCode != SCARD_S_SUCCESS)
2446 if (cReaders == SCARD_AUTOALLOCATE)
2449 if (!Stream_EnsureRemainingCapacity(s, 4))
2450 return SCARD_E_NO_MEMORY;
2452 Stream_Write_UINT32(s, cReaders);
2453 if (!smartcard_ndr_pointer_write(s, &index, cReaders))
2454 return SCARD_E_NO_MEMORY;
2455 status = smartcard_ndr_write_state(s, ret->rgReaderStates, cReaders, NDR_PTR_SIMPLE);
2456 if (status != SCARD_S_SUCCESS)
2458 return ret->ReturnCode;
2464 UINT32 pbContextNdrPtr = 0;
2465 LONG status = smartcard_unpack_redir_scard_context(s, &(call->handles.hContext), &index,
2467 if (status != SCARD_S_SUCCESS)
2470 status = smartcard_unpack_redir_scard_handle(s, &(call->handles.hCard), &index);
2471 if (status != SCARD_S_SUCCESS)
2474 if (!Stream_CheckAndLogRequiredLength(TAG, s, 8))
2475 return STATUS_BUFFER_TOO_SMALL;
2477 Stream_Read_INT32(s, call->fpbAtrIsNULL);
2478 Stream_Read_UINT32(s, call->cbAtrLen);
2480 if ((status = smartcard_unpack_redir_scard_context_ref(s, pbContextNdrPtr,
2481 &(call->handles.hContext))))
2484 if ((status = smartcard_unpack_redir_scard_handle_ref(s, &(call->handles.hCard))))
2493 DWORD cbAtrLen = ret->cbAtrLen;
2496 smartcard_trace_state_return(ret);
2497 if (ret->ReturnCode != SCARD_S_SUCCESS)
2499 if (cbAtrLen == SCARD_AUTOALLOCATE)
2502 Stream_Write_UINT32(s, ret->dwState);
2503 Stream_Write_UINT32(s, ret->dwProtocol);
2504 Stream_Write_UINT32(s, cbAtrLen);
2505 if (!smartcard_ndr_pointer_write(s, &index, cbAtrLen))
2506 return SCARD_E_NO_MEMORY;
2507 status = smartcard_ndr_write(s, ret->rgAtr, cbAtrLen, 1, NDR_PTR_SIMPLE);
2508 if (status != SCARD_S_SUCCESS)
2510 return ret->ReturnCode;
2516 UINT32 pbContextNdrPtr = 0;
2518 LONG status = smartcard_unpack_redir_scard_context(s, &(call->handles.hContext), &index,
2520 if (status != SCARD_S_SUCCESS)
2523 status = smartcard_unpack_redir_scard_handle(s, &(call->handles.hCard), &index);
2524 if (status != SCARD_S_SUCCESS)
2527 if (!Stream_CheckAndLogRequiredLength(TAG, s, 12))
2528 return STATUS_BUFFER_TOO_SMALL;
2530 Stream_Read_INT32(s, call->fmszReaderNamesIsNULL);
2531 Stream_Read_UINT32(s, call->cchReaderLen);
2532 Stream_Read_UINT32(s, call->cbAtrLen);
2534 if ((status = smartcard_unpack_redir_scard_context_ref(s, pbContextNdrPtr,
2535 &(call->handles.hContext))))
2538 if ((status = smartcard_unpack_redir_scard_handle_ref(s, &(call->handles.hCard))))
2541 smartcard_trace_status_call(call, unicode);
2549 DWORD cBytes = ret->cBytes;
2551 smartcard_trace_status_return(ret, unicode);
2552 if (ret->ReturnCode != SCARD_S_SUCCESS)
2554 if (cBytes == SCARD_AUTOALLOCATE)
2557 if (!Stream_EnsureRemainingCapacity(s, 4))
2558 return SCARD_F_INTERNAL_ERROR;
2560 Stream_Write_UINT32(s, cBytes);
2561 if (!smartcard_ndr_pointer_write(s, &index, cBytes))
2562 return SCARD_E_NO_MEMORY;
2564 if (!Stream_EnsureRemainingCapacity(s, 44))
2565 return SCARD_F_INTERNAL_ERROR;
2567 Stream_Write_UINT32(s, ret->dwState);
2568 Stream_Write_UINT32(s, ret->dwProtocol);
2569 Stream_Write(s, ret->pbAtr,
sizeof(ret->pbAtr));
2570 Stream_Write_UINT32(s, ret->cbAtrLen);
2571 status = smartcard_ndr_write(s, ret->mszReaderNames, cBytes, 1, NDR_PTR_SIMPLE);
2572 if (status != SCARD_S_SUCCESS)
2574 return ret->ReturnCode;
2580 UINT32 pbContextNdrPtr = 0;
2582 LONG status = smartcard_unpack_redir_scard_context(s, &(call->handles.hContext), &index,
2584 if (status != SCARD_S_SUCCESS)
2587 status = smartcard_unpack_redir_scard_handle(s, &(call->handles.hCard), &index);
2588 if (status != SCARD_S_SUCCESS)
2591 if (!Stream_CheckAndLogRequiredLength(TAG, s, 12))
2592 return STATUS_BUFFER_TOO_SMALL;
2594 Stream_Read_UINT32(s, call->dwAttrId);
2595 Stream_Read_INT32(s, call->fpbAttrIsNULL);
2596 Stream_Read_UINT32(s, call->cbAttrLen);
2598 if ((status = smartcard_unpack_redir_scard_context_ref(s, pbContextNdrPtr,
2599 &(call->handles.hContext))))
2602 if ((status = smartcard_unpack_redir_scard_handle_ref(s, &(call->handles.hCard))))
2605 smartcard_trace_get_attrib_call(call);
2610 DWORD cbAttrCallLen)
2613 DWORD cbAttrLen = 0;
2615 smartcard_trace_get_attrib_return(ret, dwAttrId);
2617 if (!Stream_EnsureRemainingCapacity(s, 4))
2618 return SCARD_F_INTERNAL_ERROR;
2620 cbAttrLen = ret->cbAttrLen;
2621 if (ret->ReturnCode != SCARD_S_SUCCESS)
2623 if (cbAttrLen == SCARD_AUTOALLOCATE)
2628 if (cbAttrCallLen < cbAttrLen)
2629 cbAttrLen = cbAttrCallLen;
2631 Stream_Write_UINT32(s, cbAttrLen);
2632 if (!smartcard_ndr_pointer_write(s, &index, cbAttrLen))
2633 return SCARD_E_NO_MEMORY;
2635 status = smartcard_ndr_write(s, ret->pbAttr, cbAttrLen, 1, NDR_PTR_SIMPLE);
2636 if (status != SCARD_S_SUCCESS)
2638 return ret->ReturnCode;
2644 UINT32 pvInBufferNdrPtr = 0;
2645 UINT32 pbContextNdrPtr = 0;
2647 call->pvInBuffer = NULL;
2649 LONG status = smartcard_unpack_redir_scard_context(s, &(call->handles.hContext), &index,
2651 if (status != SCARD_S_SUCCESS)
2654 status = smartcard_unpack_redir_scard_handle(s, &(call->handles.hCard), &index);
2655 if (status != SCARD_S_SUCCESS)
2658 if (!Stream_CheckAndLogRequiredLength(TAG, s, 20))
2659 return STATUS_BUFFER_TOO_SMALL;
2661 Stream_Read_UINT32(s, call->dwControlCode);
2662 Stream_Read_UINT32(s, call->cbInBufferSize);
2663 if (!smartcard_ndr_pointer_read(s, &index, &pvInBufferNdrPtr))
2664 return ERROR_INVALID_DATA;
2665 Stream_Read_INT32(s, call->fpvOutBufferIsNULL);
2666 Stream_Read_UINT32(s, call->cbOutBufferSize);
2668 if ((status = smartcard_unpack_redir_scard_context_ref(s, pbContextNdrPtr,
2669 &(call->handles.hContext))))
2672 if ((status = smartcard_unpack_redir_scard_handle_ref(s, &(call->handles.hCard))))
2675 if (pvInBufferNdrPtr)
2677 status = smartcard_ndr_read(s, &call->pvInBuffer, call->cbInBufferSize, 1, NDR_PTR_SIMPLE);
2678 if (status != SCARD_S_SUCCESS)
2682 smartcard_trace_control_call(call);
2683 return SCARD_S_SUCCESS;
2689 DWORD cbDataLen = ret->cbOutBufferSize;
2692 smartcard_trace_control_return(ret);
2693 if (ret->ReturnCode != SCARD_S_SUCCESS)
2695 if (cbDataLen == SCARD_AUTOALLOCATE)
2698 if (!Stream_EnsureRemainingCapacity(s, 4))
2699 return SCARD_F_INTERNAL_ERROR;
2701 Stream_Write_UINT32(s, cbDataLen);
2702 if (!smartcard_ndr_pointer_write(s, &index, cbDataLen))
2703 return SCARD_E_NO_MEMORY;
2705 status = smartcard_ndr_write(s, ret->pvOutBuffer, cbDataLen, 1, NDR_PTR_SIMPLE);
2706 if (status != SCARD_S_SUCCESS)
2708 return ret->ReturnCode;
2714 BYTE* pbExtraBytes = NULL;
2715 UINT32 pbExtraBytesNdrPtr = 0;
2716 UINT32 pbSendBufferNdrPtr = 0;
2717 UINT32 pioRecvPciNdrPtr = 0;
2721 UINT32 pbContextNdrPtr = 0;
2723 call->pioSendPci = NULL;
2724 call->pioRecvPci = NULL;
2725 call->pbSendBuffer = NULL;
2727 LONG status = smartcard_unpack_redir_scard_context(s, &(call->handles.hContext), &index,
2729 if (status != SCARD_S_SUCCESS)
2732 status = smartcard_unpack_redir_scard_handle(s, &(call->handles.hCard), &index);
2733 if (status != SCARD_S_SUCCESS)
2736 if (!Stream_CheckAndLogRequiredLength(TAG, s, 32))
2737 return STATUS_BUFFER_TOO_SMALL;
2739 Stream_Read_UINT32(s, ioSendPci.dwProtocol);
2740 Stream_Read_UINT32(s, ioSendPci.cbExtraBytes);
2741 if (!smartcard_ndr_pointer_read(s, &index,
2742 &pbExtraBytesNdrPtr))
2743 return ERROR_INVALID_DATA;
2745 Stream_Read_UINT32(s, call->cbSendLength);
2746 if (!smartcard_ndr_pointer_read(s, &index,
2747 &pbSendBufferNdrPtr))
2748 return ERROR_INVALID_DATA;
2750 if (!smartcard_ndr_pointer_read(s, &index, &pioRecvPciNdrPtr))
2751 return ERROR_INVALID_DATA;
2753 Stream_Read_INT32(s, call->fpbRecvBufferIsNULL);
2754 Stream_Read_UINT32(s, call->cbRecvLength);
2756 if (ioSendPci.cbExtraBytes > 1024)
2759 "Transmit_Call ioSendPci.cbExtraBytes is out of bounds: %" PRIu32
" (max: 1024)",
2760 ioSendPci.cbExtraBytes);
2761 return STATUS_INVALID_PARAMETER;
2764 if (call->cbSendLength > 66560)
2766 WLog_WARN(TAG,
"Transmit_Call cbSendLength is out of bounds: %" PRIu32
" (max: 66560)",
2767 ioSendPci.cbExtraBytes);
2768 return STATUS_INVALID_PARAMETER;
2771 if ((status = smartcard_unpack_redir_scard_context_ref(s, pbContextNdrPtr,
2772 &(call->handles.hContext))))
2775 if ((status = smartcard_unpack_redir_scard_handle_ref(s, &(call->handles.hCard))))
2778 if (ioSendPci.cbExtraBytes && !pbExtraBytesNdrPtr)
2781 TAG,
"Transmit_Call ioSendPci.cbExtraBytes is non-zero but pbExtraBytesNdrPtr is null");
2782 return STATUS_INVALID_PARAMETER;
2785 if (pbExtraBytesNdrPtr)
2788 if (!Stream_CheckAndLogRequiredLength(TAG, s, 4))
2789 return STATUS_BUFFER_TOO_SMALL;
2791 Stream_Read_UINT32(s, length);
2793 if (!Stream_CheckAndLogRequiredLength(TAG, s, ioSendPci.cbExtraBytes))
2794 return STATUS_BUFFER_TOO_SMALL;
2796 ioSendPci.pbExtraBytes = Stream_Pointer(s);
2800 if (!call->pioSendPci)
2802 WLog_WARN(TAG,
"Transmit_Call out of memory error (pioSendPci)");
2803 return STATUS_NO_MEMORY;
2806 call->pioSendPci->dwProtocol = ioSendPci.dwProtocol;
2807 call->pioSendPci->cbPciLength = (DWORD)(ioSendPci.cbExtraBytes +
sizeof(
SCARD_IO_REQUEST));
2809 Stream_Read(s, pbExtraBytes, ioSendPci.cbExtraBytes);
2810 smartcard_unpack_read_size_align(s, ioSendPci.cbExtraBytes, 4);
2816 if (!call->pioSendPci)
2818 WLog_WARN(TAG,
"Transmit_Call out of memory error (pioSendPci)");
2819 return STATUS_NO_MEMORY;
2822 call->pioSendPci->dwProtocol = ioSendPci.dwProtocol;
2826 if (pbSendBufferNdrPtr)
2828 status = smartcard_ndr_read(s, &call->pbSendBuffer, call->cbSendLength, 1, NDR_PTR_SIMPLE);
2829 if (status != SCARD_S_SUCCESS)
2833 if (pioRecvPciNdrPtr)
2835 if (!Stream_CheckAndLogRequiredLength(TAG, s, 12))
2836 return STATUS_BUFFER_TOO_SMALL;
2838 Stream_Read_UINT32(s, ioRecvPci.dwProtocol);
2839 Stream_Read_UINT32(s, ioRecvPci.cbExtraBytes);
2840 if (!smartcard_ndr_pointer_read(s, &index,
2841 &pbExtraBytesNdrPtr))
2842 return ERROR_INVALID_DATA;
2844 if (ioRecvPci.cbExtraBytes && !pbExtraBytesNdrPtr)
2848 "Transmit_Call ioRecvPci.cbExtraBytes is non-zero but pbExtraBytesNdrPtr is null");
2849 return STATUS_INVALID_PARAMETER;
2852 if (pbExtraBytesNdrPtr)
2855 if (!Stream_CheckAndLogRequiredLength(TAG, s, 4))
2856 return STATUS_BUFFER_TOO_SMALL;
2858 Stream_Read_UINT32(s, length);
2860 if (ioRecvPci.cbExtraBytes > 1024)
2863 "Transmit_Call ioRecvPci.cbExtraBytes is out of bounds: %" PRIu32
2865 ioRecvPci.cbExtraBytes);
2866 return STATUS_INVALID_PARAMETER;
2869 if (length != ioRecvPci.cbExtraBytes)
2872 "Transmit_Call unexpected length: Actual: %" PRIu32
", Expected: %" PRIu32
2873 " (ioRecvPci.cbExtraBytes)",
2874 length, ioRecvPci.cbExtraBytes);
2875 return STATUS_INVALID_PARAMETER;
2878 if (!Stream_CheckAndLogRequiredLength(TAG, s, ioRecvPci.cbExtraBytes))
2879 return STATUS_BUFFER_TOO_SMALL;
2881 ioRecvPci.pbExtraBytes = Stream_Pointer(s);
2885 if (!call->pioRecvPci)
2887 WLog_WARN(TAG,
"Transmit_Call out of memory error (pioRecvPci)");
2888 return STATUS_NO_MEMORY;
2891 call->pioRecvPci->dwProtocol = ioRecvPci.dwProtocol;
2892 call->pioRecvPci->cbPciLength =
2895 Stream_Read(s, pbExtraBytes, ioRecvPci.cbExtraBytes);
2896 smartcard_unpack_read_size_align(s, ioRecvPci.cbExtraBytes, 4);
2902 if (!call->pioRecvPci)
2904 WLog_WARN(TAG,
"Transmit_Call out of memory error (pioRecvPci)");
2905 return STATUS_NO_MEMORY;
2908 call->pioRecvPci->dwProtocol = ioRecvPci.dwProtocol;
2913 smartcard_trace_transmit_call(call);
2914 return SCARD_S_SUCCESS;
2922 UINT32 cbRecvLength = ret->cbRecvLength;
2923 UINT32 cbRecvPci = ret->pioRecvPci ? ret->pioRecvPci->cbPciLength : 0;
2925 smartcard_trace_transmit_return(ret);
2927 if (!ret->pbRecvBuffer)
2930 if (!smartcard_ndr_pointer_write(s, &index, cbRecvPci))
2931 return SCARD_E_NO_MEMORY;
2932 if (!Stream_EnsureRemainingCapacity(s, 4))
2933 return SCARD_E_NO_MEMORY;
2934 Stream_Write_UINT32(s, cbRecvLength);
2935 if (!smartcard_ndr_pointer_write(s, &index, cbRecvLength))
2936 return SCARD_E_NO_MEMORY;
2938 if (ret->pioRecvPci)
2940 UINT32 cbExtraBytes = (UINT32)(ret->pioRecvPci->cbPciLength -
sizeof(
SCARD_IO_REQUEST));
2943 if (!Stream_EnsureRemainingCapacity(s, cbExtraBytes + 16))
2945 WLog_ERR(TAG,
"Stream_EnsureRemainingCapacity failed!");
2946 return SCARD_F_INTERNAL_ERROR;
2949 Stream_Write_UINT32(s, ret->pioRecvPci->dwProtocol);
2950 Stream_Write_UINT32(s, cbExtraBytes);
2951 if (!smartcard_ndr_pointer_write(s, &index, cbExtraBytes))
2952 return SCARD_E_NO_MEMORY;
2953 error = smartcard_ndr_write(s, pbExtraBytes, cbExtraBytes, 1, NDR_PTR_SIMPLE);
2958 status = smartcard_ndr_write(s, ret->pbRecvBuffer, ret->cbRecvLength, 1, NDR_PTR_SIMPLE);
2959 if (status != SCARD_S_SUCCESS)
2961 return ret->ReturnCode;
2966 UINT32 rgReaderStatesNdrPtr = 0;
2967 UINT32 rgAtrMasksNdrPtr = 0;
2969 UINT32 pbContextNdrPtr = 0;
2971 call->rgReaderStates = NULL;
2973 LONG status = smartcard_unpack_redir_scard_context(s, &(call->handles.hContext), &index,
2975 if (status != SCARD_S_SUCCESS)
2978 if (!Stream_CheckAndLogRequiredLength(TAG, s, 16))
2979 return STATUS_BUFFER_TOO_SMALL;
2981 Stream_Read_UINT32(s, call->cAtrs);
2982 if (!smartcard_ndr_pointer_read(s, &index, &rgAtrMasksNdrPtr))
2983 return ERROR_INVALID_DATA;
2984 Stream_Read_UINT32(s, call->cReaders);
2985 if (!smartcard_ndr_pointer_read(s, &index, &rgReaderStatesNdrPtr))
2986 return ERROR_INVALID_DATA;
2988 if ((status = smartcard_unpack_redir_scard_context_ref(s, pbContextNdrPtr,
2989 &(call->handles.hContext))))
2992 if ((rgAtrMasksNdrPtr && !call->cAtrs) || (!rgAtrMasksNdrPtr && call->cAtrs))
2995 "LocateCardsByATRA_Call rgAtrMasksNdrPtr (0x%08" PRIX32
2996 ") and cAtrs (0x%08" PRIX32
") inconsistency",
2997 rgAtrMasksNdrPtr, call->cAtrs);
2998 return STATUS_INVALID_PARAMETER;
3001 if (rgAtrMasksNdrPtr)
3003 status = smartcard_ndr_read_atrmask(s, &call->rgAtrMasks, call->cAtrs, NDR_PTR_SIMPLE);
3004 if (status != SCARD_S_SUCCESS)
3008 if (rgReaderStatesNdrPtr)
3010 status = smartcard_unpack_reader_state_a(s, &call->rgReaderStates, call->cReaders, &index);
3011 if (status != SCARD_S_SUCCESS)
3015 smartcard_trace_locate_cards_by_atr_a_call(call);
3016 return SCARD_S_SUCCESS;
3021 UINT32 sz1NdrPtr = 0;
3022 UINT32 sz2NdrPtr = 0;
3024 UINT32 pbContextNdrPtr = 0;
3026 LONG status = smartcard_unpack_redir_scard_context(s, &(call->handles.hContext), &index,
3028 if (status != SCARD_S_SUCCESS)
3031 if (!smartcard_ndr_pointer_read(s, &index, &sz1NdrPtr))
3032 return ERROR_INVALID_DATA;
3033 if (!smartcard_ndr_pointer_read(s, &index, &sz2NdrPtr))
3034 return ERROR_INVALID_DATA;
3036 status = smartcard_unpack_redir_scard_context_ref(s, pbContextNdrPtr, &call->handles.hContext);
3037 if (status != SCARD_S_SUCCESS)
3042 status = smartcard_ndr_read_a(s, &call->sz1, NDR_PTR_FULL);
3043 if (status != SCARD_S_SUCCESS)
3048 status = smartcard_ndr_read_a(s, &call->sz2, NDR_PTR_FULL);
3049 if (status != SCARD_S_SUCCESS)
3052 smartcard_trace_context_and_two_strings_a_call(call);
3053 return SCARD_S_SUCCESS;
3058 UINT32 sz1NdrPtr = 0;
3059 UINT32 sz2NdrPtr = 0;
3061 UINT32 pbContextNdrPtr = 0;
3063 LONG status = smartcard_unpack_redir_scard_context(s, &(call->handles.hContext), &index,
3065 if (status != SCARD_S_SUCCESS)
3068 if (!smartcard_ndr_pointer_read(s, &index, &sz1NdrPtr))
3069 return ERROR_INVALID_DATA;
3070 if (!smartcard_ndr_pointer_read(s, &index, &sz2NdrPtr))
3071 return ERROR_INVALID_DATA;
3073 status = smartcard_unpack_redir_scard_context_ref(s, pbContextNdrPtr, &call->handles.hContext);
3074 if (status != SCARD_S_SUCCESS)
3079 status = smartcard_ndr_read_w(s, &call->sz1, NDR_PTR_FULL);
3080 if (status != SCARD_S_SUCCESS)
3085 status = smartcard_ndr_read_w(s, &call->sz2, NDR_PTR_FULL);
3086 if (status != SCARD_S_SUCCESS)
3089 smartcard_trace_context_and_two_strings_w_call(call);
3090 return SCARD_S_SUCCESS;
3095 UINT32 sz1NdrPtr = 0;
3096 UINT32 sz2NdrPtr = 0;
3098 UINT32 pbContextNdrPtr = 0;
3100 LONG status = smartcard_unpack_redir_scard_context(s, &(call->handles.hContext), &index,
3102 if (status != SCARD_S_SUCCESS)
3105 if (!Stream_CheckAndLogRequiredLength(TAG, s, 16))
3106 return STATUS_BUFFER_TOO_SMALL;
3108 Stream_Read_UINT32(s, call->cBytes);
3109 if (!smartcard_ndr_pointer_read(s, &index, &sz1NdrPtr))
3110 return ERROR_INVALID_DATA;
3112 Stream_Read_UINT32(s, call->cReaders);
3113 if (!smartcard_ndr_pointer_read(s, &index, &sz2NdrPtr))
3114 return ERROR_INVALID_DATA;
3116 if ((status = smartcard_unpack_redir_scard_context_ref(s, pbContextNdrPtr,
3117 &(call->handles.hContext))))
3123 smartcard_ndr_read_fixed_string_a(s, &call->mszCards, call->cBytes, NDR_PTR_SIMPLE);
3124 if (status != SCARD_S_SUCCESS)
3129 status = smartcard_unpack_reader_state_a(s, &call->rgReaderStates, call->cReaders, &index);
3130 if (status != SCARD_S_SUCCESS)
3133 smartcard_trace_locate_cards_a_call(call);
3134 return SCARD_S_SUCCESS;
3139 UINT32 sz1NdrPtr = 0;
3140 UINT32 sz2NdrPtr = 0;
3142 UINT32 pbContextNdrPtr = 0;
3144 LONG status = smartcard_unpack_redir_scard_context(s, &(call->handles.hContext), &index,
3146 if (status != SCARD_S_SUCCESS)
3149 if (!Stream_CheckAndLogRequiredLength(TAG, s, 16))
3150 return STATUS_BUFFER_TOO_SMALL;
3152 Stream_Read_UINT32(s, call->cBytes);
3153 if (!smartcard_ndr_pointer_read(s, &index, &sz1NdrPtr))
3154 return ERROR_INVALID_DATA;
3156 Stream_Read_UINT32(s, call->cReaders);
3157 if (!smartcard_ndr_pointer_read(s, &index, &sz2NdrPtr))
3158 return ERROR_INVALID_DATA;
3160 if ((status = smartcard_unpack_redir_scard_context_ref(s, pbContextNdrPtr,
3161 &(call->handles.hContext))))
3167 smartcard_ndr_read_fixed_string_w(s, &call->mszCards, call->cBytes, NDR_PTR_SIMPLE);
3168 if (status != SCARD_S_SUCCESS)
3173 status = smartcard_unpack_reader_state_w(s, &call->rgReaderStates, call->cReaders, &index);
3174 if (status != SCARD_S_SUCCESS)
3177 smartcard_trace_locate_cards_w_call(call);
3178 return SCARD_S_SUCCESS;
3185 UINT32 pbContextNdrPtr = 0;
3187 LONG status = smartcard_unpack_redir_scard_context(s, &(call->handles.hContext), &index,
3189 if (status != SCARD_S_SUCCESS)
3191 status = smartcard_unpack_redir_scard_handle(s, &(call->handles.hCard), &index);
3192 if (status != SCARD_S_SUCCESS)
3195 if (!Stream_CheckAndLogRequiredLength(TAG, s, 12))
3196 return STATUS_BUFFER_TOO_SMALL;
3197 Stream_Read_UINT32(s, call->dwAttrId);
3198 Stream_Read_UINT32(s, call->cbAttrLen);
3200 if (!smartcard_ndr_pointer_read(s, &index, &ndrPtr))
3201 return ERROR_INVALID_DATA;
3203 if ((status = smartcard_unpack_redir_scard_context_ref(s, pbContextNdrPtr,
3204 &(call->handles.hContext))))
3207 if ((status = smartcard_unpack_redir_scard_handle_ref(s, &(call->handles.hCard))))
3214 status = smartcard_ndr_read(s, &call->pbAttr, 0, 1, NDR_PTR_SIMPLE);
3215 if (status != SCARD_S_SUCCESS)
3218 smartcard_trace_set_attrib_call(call);
3219 return SCARD_S_SUCCESS;
3224 UINT32 rgReaderStatesNdrPtr = 0;
3225 UINT32 rgAtrMasksNdrPtr = 0;
3227 UINT32 pbContextNdrPtr = 0;
3229 call->rgReaderStates = NULL;
3231 LONG status = smartcard_unpack_redir_scard_context(s, &(call->handles.hContext), &index,
3233 if (status != SCARD_S_SUCCESS)
3236 if (!Stream_CheckAndLogRequiredLength(TAG, s, 16))
3237 return STATUS_BUFFER_TOO_SMALL;
3239 Stream_Read_UINT32(s, call->cAtrs);
3240 if (!smartcard_ndr_pointer_read(s, &index, &rgAtrMasksNdrPtr))
3241 return ERROR_INVALID_DATA;
3243 Stream_Read_UINT32(s, call->cReaders);
3244 if (!smartcard_ndr_pointer_read(s, &index, &rgReaderStatesNdrPtr))
3245 return ERROR_INVALID_DATA;
3247 if ((status = smartcard_unpack_redir_scard_context_ref(s, pbContextNdrPtr,
3248 &(call->handles.hContext))))
3251 if ((rgAtrMasksNdrPtr && !call->cAtrs) || (!rgAtrMasksNdrPtr && call->cAtrs))
3254 "LocateCardsByATRW_Call rgAtrMasksNdrPtr (0x%08" PRIX32
3255 ") and cAtrs (0x%08" PRIX32
") inconsistency",
3256 rgAtrMasksNdrPtr, call->cAtrs);
3257 return STATUS_INVALID_PARAMETER;
3260 if (rgAtrMasksNdrPtr)
3262 status = smartcard_ndr_read_atrmask(s, &call->rgAtrMasks, call->cAtrs, NDR_PTR_SIMPLE);
3263 if (status != SCARD_S_SUCCESS)
3267 if (rgReaderStatesNdrPtr)
3269 status = smartcard_unpack_reader_state_w(s, &call->rgReaderStates, call->cReaders, &index);
3270 if (status != SCARD_S_SUCCESS)
3274 smartcard_trace_locate_cards_by_atr_w_call(call);
3275 return SCARD_S_SUCCESS;
3280 UINT32 mszNdrPtr = 0;
3281 UINT32 contextNdrPtr = 0;
3283 UINT32 pbContextNdrPtr = 0;
3285 if (!smartcard_ndr_pointer_read(s, &index, &mszNdrPtr))
3286 return ERROR_INVALID_DATA;
3288 LONG status = smartcard_unpack_redir_scard_context(s, &(call->Common.handles.hContext), &index,
3290 if (status != SCARD_S_SUCCESS)
3293 if (!smartcard_ndr_pointer_read(s, &index, &contextNdrPtr))
3294 return ERROR_INVALID_DATA;
3296 if (!Stream_CheckAndLogRequiredLength(TAG, s, 12))
3297 return STATUS_BUFFER_TOO_SMALL;
3298 Stream_Read_UINT32(s, call->Common.FreshnessCounter);
3299 Stream_Read_INT32(s, call->Common.fPbDataIsNULL);
3300 Stream_Read_UINT32(s, call->Common.cbDataLen);
3302 call->szLookupName = NULL;
3305 status = smartcard_ndr_read_a(s, &call->szLookupName, NDR_PTR_FULL);
3306 if (status != SCARD_S_SUCCESS)
3310 status = smartcard_unpack_redir_scard_context_ref(s, pbContextNdrPtr,
3311 &call->Common.handles.hContext);
3312 if (status != SCARD_S_SUCCESS)
3317 status = smartcard_ndr_read_u(s, &call->Common.CardIdentifier);
3318 if (status != SCARD_S_SUCCESS)
3321 smartcard_trace_read_cache_a_call(call);
3322 return SCARD_S_SUCCESS;
3327 UINT32 mszNdrPtr = 0;
3328 UINT32 contextNdrPtr = 0;
3330 UINT32 pbContextNdrPtr = 0;
3332 if (!smartcard_ndr_pointer_read(s, &index, &mszNdrPtr))
3333 return ERROR_INVALID_DATA;
3335 LONG status = smartcard_unpack_redir_scard_context(s, &(call->Common.handles.hContext), &index,
3337 if (status != SCARD_S_SUCCESS)
3340 if (!smartcard_ndr_pointer_read(s, &index, &contextNdrPtr))
3341 return ERROR_INVALID_DATA;
3343 if (!Stream_CheckAndLogRequiredLength(TAG, s, 12))
3344 return STATUS_BUFFER_TOO_SMALL;
3345 Stream_Read_UINT32(s, call->Common.FreshnessCounter);
3346 Stream_Read_INT32(s, call->Common.fPbDataIsNULL);
3347 Stream_Read_UINT32(s, call->Common.cbDataLen);
3349 call->szLookupName = NULL;
3352 status = smartcard_ndr_read_w(s, &call->szLookupName, NDR_PTR_FULL);
3353 if (status != SCARD_S_SUCCESS)
3357 status = smartcard_unpack_redir_scard_context_ref(s, pbContextNdrPtr,
3358 &call->Common.handles.hContext);
3359 if (status != SCARD_S_SUCCESS)
3364 status = smartcard_ndr_read_u(s, &call->Common.CardIdentifier);
3365 if (status != SCARD_S_SUCCESS)
3368 smartcard_trace_read_cache_w_call(call);
3369 return SCARD_S_SUCCESS;
3374 UINT32 mszNdrPtr = 0;
3375 UINT32 contextNdrPtr = 0;
3376 UINT32 pbDataNdrPtr = 0;
3378 UINT32 pbContextNdrPtr = 0;
3380 if (!smartcard_ndr_pointer_read(s, &index, &mszNdrPtr))
3381 return ERROR_INVALID_DATA;
3383 LONG status = smartcard_unpack_redir_scard_context(s, &(call->Common.handles.hContext), &index,
3385 if (status != SCARD_S_SUCCESS)
3388 if (!smartcard_ndr_pointer_read(s, &index, &contextNdrPtr))
3389 return ERROR_INVALID_DATA;
3391 if (!Stream_CheckAndLogRequiredLength(TAG, s, 8))
3392 return STATUS_BUFFER_TOO_SMALL;
3394 Stream_Read_UINT32(s, call->Common.FreshnessCounter);
3395 Stream_Read_UINT32(s, call->Common.cbDataLen);
3397 if (!smartcard_ndr_pointer_read(s, &index, &pbDataNdrPtr))
3398 return ERROR_INVALID_DATA;
3400 call->szLookupName = NULL;
3403 status = smartcard_ndr_read_a(s, &call->szLookupName, NDR_PTR_FULL);
3404 if (status != SCARD_S_SUCCESS)
3408 status = smartcard_unpack_redir_scard_context_ref(s, pbContextNdrPtr,
3409 &call->Common.handles.hContext);
3410 if (status != SCARD_S_SUCCESS)
3413 call->Common.CardIdentifier = NULL;
3416 status = smartcard_ndr_read_u(s, &call->Common.CardIdentifier);
3417 if (status != SCARD_S_SUCCESS)
3421 call->Common.pbData = NULL;
3425 smartcard_ndr_read(s, &call->Common.pbData, call->Common.cbDataLen, 1, NDR_PTR_SIMPLE);
3426 if (status != SCARD_S_SUCCESS)
3429 smartcard_trace_write_cache_a_call(call);
3430 return SCARD_S_SUCCESS;
3435 UINT32 mszNdrPtr = 0;
3436 UINT32 contextNdrPtr = 0;
3437 UINT32 pbDataNdrPtr = 0;
3439 UINT32 pbContextNdrPtr = 0;
3441 if (!smartcard_ndr_pointer_read(s, &index, &mszNdrPtr))
3442 return ERROR_INVALID_DATA;
3444 LONG status = smartcard_unpack_redir_scard_context(s, &(call->Common.handles.hContext), &index,
3446 if (status != SCARD_S_SUCCESS)
3449 if (!smartcard_ndr_pointer_read(s, &index, &contextNdrPtr))
3450 return ERROR_INVALID_DATA;
3452 if (!Stream_CheckAndLogRequiredLength(TAG, s, 8))
3453 return STATUS_BUFFER_TOO_SMALL;
3454 Stream_Read_UINT32(s, call->Common.FreshnessCounter);
3455 Stream_Read_UINT32(s, call->Common.cbDataLen);
3457 if (!smartcard_ndr_pointer_read(s, &index, &pbDataNdrPtr))
3458 return ERROR_INVALID_DATA;
3460 call->szLookupName = NULL;
3463 status = smartcard_ndr_read_w(s, &call->szLookupName, NDR_PTR_FULL);
3464 if (status != SCARD_S_SUCCESS)
3468 status = smartcard_unpack_redir_scard_context_ref(s, pbContextNdrPtr,
3469 &call->Common.handles.hContext);
3470 if (status != SCARD_S_SUCCESS)
3473 call->Common.CardIdentifier = NULL;
3476 status = smartcard_ndr_read_u(s, &call->Common.CardIdentifier);
3477 if (status != SCARD_S_SUCCESS)
3481 call->Common.pbData = NULL;
3485 smartcard_ndr_read(s, &call->Common.pbData, call->Common.cbDataLen, 1, NDR_PTR_SIMPLE);
3486 if (status != SCARD_S_SUCCESS)
3489 smartcard_trace_write_cache_w_call(call);
3496 UINT32 pbContextNdrPtr = 0;
3498 LONG status = smartcard_unpack_redir_scard_context(s, &(call->handles.hContext), &index,
3500 if (status != SCARD_S_SUCCESS)
3503 status = smartcard_unpack_redir_scard_handle(s, &(call->handles.hCard), &index);
3504 if (status != SCARD_S_SUCCESS)
3507 if ((status = smartcard_unpack_redir_scard_context_ref(s, pbContextNdrPtr,
3508 &(call->handles.hContext))))
3510 WLog_ERR(TAG,
"smartcard_unpack_redir_scard_context_ref failed with error %" PRId32
"",
3515 if ((status = smartcard_unpack_redir_scard_handle_ref(s, &(call->handles.hCard))))
3516 WLog_ERR(TAG,
"smartcard_unpack_redir_scard_handle_ref failed with error %" PRId32
"",
3519 smartcard_trace_get_transmit_count_call(call);
3525 return smartcard_unpack_common_context_and_string_w(s, &call->handles.hContext,
3526 &call->szReaderName);
3531 return smartcard_unpack_common_context_and_string_a(s, &call->handles.hContext, &call->sz);
3536 return smartcard_unpack_common_context_and_string_w(s, &call->handles.hContext, &call->sz);
3541 return smartcard_unpack_common_context_and_string_w(s, &call->handles.hContext,
3542 &call->szReaderName);
3547 smartcard_trace_device_type_id_return(ret);
3549 if (!Stream_EnsureRemainingCapacity(s, 4))
3551 WLog_ERR(TAG,
"Stream_EnsureRemainingCapacity failed!");
3552 return SCARD_F_INTERNAL_ERROR;
3555 Stream_Write_UINT32(s, ret->dwDeviceId);
3557 return ret->ReturnCode;
3563 DWORD cbDataLen = ret->cReaders;
3566 smartcard_trace_locate_cards_return(ret);
3567 if (ret->ReturnCode != SCARD_S_SUCCESS)
3569 if (cbDataLen == SCARD_AUTOALLOCATE)
3572 if (!Stream_EnsureRemainingCapacity(s, 4))
3574 WLog_ERR(TAG,
"Stream_EnsureRemainingCapacity failed!");
3575 return SCARD_F_INTERNAL_ERROR;
3578 Stream_Write_UINT32(s, cbDataLen);
3579 if (!smartcard_ndr_pointer_write(s, &index, cbDataLen))
3580 return SCARD_E_NO_MEMORY;
3582 status = smartcard_ndr_write_state(s, ret->rgReaderStates, cbDataLen, NDR_PTR_SIMPLE);
3583 if (status != SCARD_S_SUCCESS)
3585 return ret->ReturnCode;
3592 DWORD cbDataLen = ret->cbDataLen;
3593 smartcard_trace_get_reader_icon_return(ret);
3594 if (ret->ReturnCode != SCARD_S_SUCCESS)
3596 if (cbDataLen == SCARD_AUTOALLOCATE)
3599 if (!Stream_EnsureRemainingCapacity(s, 4))
3601 WLog_ERR(TAG,
"Stream_EnsureRemainingCapacity failed!");
3602 return SCARD_F_INTERNAL_ERROR;
3605 Stream_Write_UINT32(s, cbDataLen);
3606 if (!smartcard_ndr_pointer_write(s, &index, cbDataLen))
3607 return SCARD_E_NO_MEMORY;
3609 status = smartcard_ndr_write(s, ret->pbData, cbDataLen, 1, NDR_PTR_SIMPLE);
3610 if (status != SCARD_S_SUCCESS)
3612 return ret->ReturnCode;
3617 smartcard_trace_get_transmit_count_return(ret);
3619 if (!Stream_EnsureRemainingCapacity(s, 4))
3621 WLog_ERR(TAG,
"Stream_EnsureRemainingCapacity failed!");
3622 return SCARD_F_INTERNAL_ERROR;
3625 Stream_Write_UINT32(s, ret->cTransmitCount);
3627 return ret->ReturnCode;
3634 DWORD cbDataLen = ret->cbDataLen;
3635 smartcard_trace_read_cache_return(ret);
3636 if (ret->ReturnCode != SCARD_S_SUCCESS)
3639 if (cbDataLen == SCARD_AUTOALLOCATE)
3642 if (!Stream_EnsureRemainingCapacity(s, 4))
3644 WLog_ERR(TAG,
"Stream_EnsureRemainingCapacity failed!");
3645 return SCARD_F_INTERNAL_ERROR;
3648 Stream_Write_UINT32(s, cbDataLen);
3649 if (!smartcard_ndr_pointer_write(s, &index, cbDataLen))
3650 return SCARD_E_NO_MEMORY;
3652 status = smartcard_ndr_write(s, ret->pbData, cbDataLen, 1, NDR_PTR_SIMPLE);
3653 if (status != SCARD_S_SUCCESS)
3655 return ret->ReturnCode;