FreeRDP
Loading...
Searching...
No Matches
smartcard_pack.c
1
24#include <freerdp/config.h>
25
26#include <winpr/crt.h>
27#include <winpr/print.h>
28
29#include <freerdp/channels/scard.h>
30#include <freerdp/utils/smartcard_pack.h>
31#include "smartcard_pack.h"
32
33#include <freerdp/log.h>
34#define SCARD_TAG FREERDP_TAG("scard.pack")
35
36static const DWORD g_LogLevel = WLOG_DEBUG;
37
38static wLog* scard_log(void)
39{
40 static wLog* log = nullptr;
41 if (!log)
42 log = WLog_Get(SCARD_TAG);
43 return log;
44}
45
46#define smartcard_unpack_redir_scard_context(log, s, context, index, ndr) \
47 smartcard_unpack_redir_scard_context_((log), (s), (context), (index), (ndr), __FILE__, \
48 __func__, __LINE__)
49#define smartcard_unpack_redir_scard_handle(log, s, context, index) \
50 smartcard_unpack_redir_scard_handle_((log), (s), (context), (index), __FILE__, __func__, \
51 __LINE__)
52
53WINPR_ATTR_NODISCARD static LONG
54smartcard_unpack_redir_scard_context_(wLog* log, wStream* s, REDIR_SCARDCONTEXT* context,
55 UINT32* index, UINT32* ppbContextNdrPtr, const char* file,
56 const char* function, size_t line);
57WINPR_ATTR_NODISCARD static LONG
58smartcard_pack_redir_scard_context(wLog* log, wStream* s, const REDIR_SCARDCONTEXT* context,
59 UINT32* index);
60WINPR_ATTR_NODISCARD static LONG
61smartcard_unpack_redir_scard_handle_(wLog* log, wStream* s, REDIR_SCARDHANDLE* handle,
62 UINT32* index, const char* file, const char* function,
63 size_t line);
64WINPR_ATTR_NODISCARD static LONG smartcard_pack_redir_scard_handle(wLog* log, wStream* s,
65 const REDIR_SCARDHANDLE* handle,
66 UINT32* index);
67WINPR_ATTR_NODISCARD static LONG
68smartcard_unpack_redir_scard_context_ref(wLog* log, wStream* s, UINT32 pbContextNdrPtr,
69 REDIR_SCARDCONTEXT* context);
70WINPR_ATTR_NODISCARD static LONG
71smartcard_pack_redir_scard_context_ref(wLog* log, wStream* s, const REDIR_SCARDCONTEXT* context);
72
73WINPR_ATTR_NODISCARD static LONG smartcard_unpack_redir_scard_handle_ref(wLog* log, wStream* s,
74 REDIR_SCARDHANDLE* handle);
75WINPR_ATTR_NODISCARD static LONG
76smartcard_pack_redir_scard_handle_ref(wLog* log, wStream* s, const REDIR_SCARDHANDLE* handle);
77
78typedef enum
79{
80 NDR_PTR_FULL,
81 NDR_PTR_SIMPLE,
82 NDR_PTR_FIXED
83} ndr_ptr_t;
84
85/* Reads a NDR pointer and checks if the value read has the expected relative
86 * addressing */
87#define smartcard_ndr_pointer_read(log, s, index, ptr) \
88 smartcard_ndr_pointer_read_((log), (s), (index), (ptr), __FILE__, __func__, __LINE__)
89static BOOL smartcard_ndr_pointer_read_(wLog* log, wStream* s, UINT32* index, UINT32* ptr,
90 const char* file, const char* fkt, size_t line)
91{
92 const UINT32 expect = 0x20000 + (*index) * 4;
93 UINT32 ndrPtr = 0;
94 WINPR_UNUSED(file);
95 if (!s)
96 return FALSE;
97 if (!Stream_CheckAndLogRequiredLengthWLog(log, s, 4))
98 return FALSE;
99
100 Stream_Read_UINT32(s, ndrPtr); /* mszGroupsNdrPtr (4 bytes) */
101 if (ptr)
102 *ptr = ndrPtr;
103 if (expect != ndrPtr)
104 {
105 /* Allow nullptr pointer if we read the result */
106 if (ptr && (ndrPtr == 0))
107 return TRUE;
108 WLog_Print(log, WLOG_WARN,
109 "[%s:%" PRIuz "] Read context pointer 0x%08" PRIx32 ", expected 0x%08" PRIx32,
110 fkt, line, ndrPtr, expect);
111 return FALSE;
112 }
113
114 (*index) = (*index) + 1;
115 return TRUE;
116}
117
118WINPR_ATTR_NODISCARD static LONG smartcard_ndr_read_ex(wLog* log, wStream* s, BYTE** data,
119 size_t min, size_t elementSize,
120 const ndr_ptr_t type, size_t* plen)
121{
122 size_t len = 0;
123 size_t offset = 0;
124 size_t len2 = 0;
125 void* r = nullptr;
126 size_t required = 0;
127
128 *data = nullptr;
129 if (plen)
130 *plen = 0;
131
132 switch (type)
133 {
134 case NDR_PTR_FULL:
135 required = 12;
136 break;
137 case NDR_PTR_SIMPLE:
138 required = 4;
139 break;
140 case NDR_PTR_FIXED:
141 required = min;
142 break;
143 default:
144 return STATUS_INVALID_PARAMETER;
145 }
146
147 if (!Stream_CheckAndLogRequiredLengthWLog(log, s, required))
148 return STATUS_BUFFER_TOO_SMALL;
149
150 switch (type)
151 {
152 case NDR_PTR_FULL:
153 Stream_Read_UINT32(s, len);
154 Stream_Read_UINT32(s, offset);
155 Stream_Read_UINT32(s, len2);
156 if (len != offset + len2)
157 {
158 WLog_Print(log, WLOG_ERROR,
159 "Invalid data when reading full NDR pointer: total=%" PRIuz
160 ", offset=%" PRIuz ", remaining=%" PRIuz,
161 len, offset, len2);
162 return STATUS_BUFFER_TOO_SMALL;
163 }
164 break;
165 case NDR_PTR_SIMPLE:
166 Stream_Read_UINT32(s, len);
167
168 if ((len != min) && (min > 0))
169 {
170 WLog_Print(log, WLOG_ERROR,
171 "Invalid data when reading simple NDR pointer: total=%" PRIuz
172 ", expected=%" PRIuz,
173 len, min);
174 return STATUS_BUFFER_TOO_SMALL;
175 }
176 break;
177 case NDR_PTR_FIXED:
178 len = (UINT32)min;
179 break;
180 default:
181 return STATUS_INVALID_PARAMETER;
182 }
183
184 if (min > len)
185 {
186 WLog_Print(log, WLOG_ERROR,
187 "Invalid length read from NDR pointer, minimum %" PRIuz ", got %" PRIuz, min,
188 len);
189 return STATUS_DATA_ERROR;
190 }
191
192 if (len > SIZE_MAX / 2)
193 return STATUS_BUFFER_TOO_SMALL;
194
195 if (!Stream_CheckAndLogRequiredLengthOfSizeWLog(log, s, len, elementSize))
196 return STATUS_BUFFER_TOO_SMALL;
197
198 len *= elementSize;
199
200 /* Ensure proper '\0' termination for all kinds of unicode strings
201 * as we do not know if the data from the wire contains one.
202 */
203 r = calloc(len + sizeof(WCHAR), sizeof(CHAR));
204 if (!r)
205 return SCARD_E_NO_MEMORY;
206 Stream_Read(s, r, len);
207 const LONG pad = smartcard_unpack_read_size_align(s, len, 4);
208 if (pad < 0)
209 {
210 free(r);
211 return STATUS_INVALID_PARAMETER;
212 }
213 *data = r;
214 if (plen)
215 *plen = len; /* payload bytes only, excluding the NDR alignment padding */
216 return STATUS_SUCCESS;
217}
218
219WINPR_ATTR_NODISCARD static LONG smartcard_ndr_read(wLog* log, wStream* s, BYTE** data, size_t min,
220 size_t elementSize, const ndr_ptr_t type)
221{
222 return smartcard_ndr_read_ex(log, s, data, min, elementSize, type, nullptr);
223}
224
225WINPR_ATTR_NODISCARD
226static BOOL smartcard_ndr_pointer_write(wStream* s, UINT32* index, UINT32 length)
227{
228 const UINT32 ndrPtr = 0x20000 + (*index) * 4;
229
230 if (!s)
231 return FALSE;
232 if (!Stream_EnsureRemainingCapacity(s, 4))
233 return FALSE;
234
235 if (length > 0)
236 {
237 Stream_Write_UINT32(s, ndrPtr); /* mszGroupsNdrPtr (4 bytes) */
238 (*index) = (*index) + 1;
239 }
240 else
241 Stream_Write_UINT32(s, 0);
242 return TRUE;
243}
244
245WINPR_ATTR_NODISCARD static LONG smartcard_ndr_write(wStream* s, const BYTE* data, UINT32 size,
246 UINT32 elementSize, const ndr_ptr_t type)
247{
248 const UINT32 offset = 0;
249 const UINT32 len = size;
250 const UINT32 dataLen = size * elementSize;
251 size_t required = 0;
252
253 if (size == 0)
254 return SCARD_S_SUCCESS;
255
256 switch (type)
257 {
258 case NDR_PTR_FULL:
259 required = 12;
260 break;
261 case NDR_PTR_SIMPLE:
262 required = 4;
263 break;
264 case NDR_PTR_FIXED:
265 required = 0;
266 break;
267 default:
268 return SCARD_E_INVALID_PARAMETER;
269 }
270
271 if (!Stream_EnsureRemainingCapacity(s, required))
272 return STATUS_BUFFER_TOO_SMALL;
273
274 switch (type)
275 {
276 case NDR_PTR_FULL:
277 Stream_Write_UINT32(s, len);
278 Stream_Write_UINT32(s, offset);
279 Stream_Write_UINT32(s, len);
280 break;
281 case NDR_PTR_SIMPLE:
282 Stream_Write_UINT32(s, len);
283 break;
284 case NDR_PTR_FIXED:
285 break;
286 default:
287 return SCARD_E_INVALID_PARAMETER;
288 }
289
290 if (!Stream_EnsureRemainingCapacity(s, dataLen))
291 return STATUS_BUFFER_TOO_SMALL;
292
293 if (data)
294 Stream_Write(s, data, dataLen);
295 else
296 Stream_Zero(s, dataLen);
297 return smartcard_pack_write_size_align(s, dataLen, 4);
298}
299
300WINPR_ATTR_NODISCARD static LONG smartcard_ndr_write_state(wStream* s,
301 const ReaderState_Return* data,
302 UINT32 size, const ndr_ptr_t type)
303{
304 union
305 {
306 const ReaderState_Return* reader;
307 const BYTE* data;
308 } cnv;
309
310 WINPR_ASSERT(data || (size == 0));
311 cnv.reader = data;
312
313 for (size_t x = 0; x < size; x++)
314 {
315 const ReaderState_Return* cur = &data[x];
316 if (!smartcard_reader_state_return_is_valid(x, cur))
317 return SCARD_E_INVALID_ATR;
318 }
319 return smartcard_ndr_write(s, cnv.data, size, sizeof(ReaderState_Return), type);
320}
321
322WINPR_ATTR_NODISCARD static LONG smartcard_ndr_read_state(wLog* log, wStream* s,
323 ReaderState_Return** data, size_t min,
324 const ndr_ptr_t type)
325{
326 union
327 {
328 ReaderState_Return** ppc;
329 BYTE** ppv;
330 } u;
331
332 WINPR_ASSERT(data);
333 u.ppc = data;
334
335 size_t len = 0;
336 const LONG status =
337 smartcard_ndr_read_ex(log, s, u.ppv, min, sizeof(ReaderState_Return), type, &len);
338 if (status != SCARD_S_SUCCESS)
339 return status;
340
341 if (len / sizeof(ReaderState_Return) < min)
342 return SCARD_E_INVALID_VALUE;
343
344 for (size_t x = 0; x < min; x++)
345 {
346 const ReaderState_Return* cur = &(*data)[x];
347 if (!smartcard_reader_state_return_is_valid(x, cur))
348 return SCARD_E_INVALID_ATR;
349 }
350 return status;
351}
352
353WINPR_ATTR_NODISCARD static LONG smartcard_ndr_read_atrmask(wLog* log, wStream* s,
354 LocateCards_ATRMask** data, size_t min,
355 const ndr_ptr_t type)
356{
357 union
358 {
360 BYTE** ppv;
361 } u;
362 u.ppc = data;
363 const LONG status = smartcard_ndr_read(log, s, u.ppv, min, sizeof(LocateCards_ATRMask), type);
364 if (status != SCARD_S_SUCCESS)
365 return status;
366
367 /* [MS-RDPESC] 2.2.1.5: cbAtr is range(0..36), the number of valid bytes in the fixed
368 * rgbAtr/rgbMask arrays. A larger value walks past them when the mask is compared byte
369 * by byte, so reject it here rather than trusting the wire value. */
370 LocateCards_ATRMask* masks = *data;
371 for (size_t x = 0; x < min; x++)
372 {
373 const UINT32 val = winpr_Data_Get_UINT32(&masks[x].cbAtr);
374 if (val > ARRAYSIZE(masks[x].rgbAtr))
375 {
376 WLog_Print(log, WLOG_ERROR,
377 "LocateCards_ATRMask[%" PRIuz "]::cbAtr %" PRIu32 " exceeds %" PRIuz, x, val,
378 (size_t)ARRAYSIZE(masks[x].rgbAtr));
379 free(*data);
380 *data = nullptr;
381 return STATUS_DATA_ERROR;
382 }
383 }
384 return SCARD_S_SUCCESS;
385}
386
387WINPR_ATTR_NODISCARD static LONG smartcard_ndr_read_fixed_string_a(wLog* log, wStream* s,
388 CHAR** data, size_t min,
389 const ndr_ptr_t type)
390{
391 union
392 {
393 CHAR** ppc;
394 BYTE** ppv;
395 } u;
396 u.ppc = data;
397 return smartcard_ndr_read(log, s, u.ppv, min, sizeof(CHAR), type);
398}
399
400WINPR_ATTR_NODISCARD static LONG smartcard_ndr_read_fixed_string_w(wLog* log, wStream* s,
401 WCHAR** data, size_t min,
402 const ndr_ptr_t type)
403{
404 union
405 {
406 WCHAR** ppc;
407 BYTE** ppv;
408 } u;
409 u.ppc = data;
410 return smartcard_ndr_read(log, s, u.ppv, min, sizeof(WCHAR), type);
411}
412
413WINPR_ATTR_NODISCARD static LONG smartcard_ndr_read_a(wLog* log, wStream* s, CHAR** data,
414 const ndr_ptr_t type)
415{
416 union
417 {
418 CHAR** ppc;
419 BYTE** ppv;
420 } u;
421 u.ppc = data;
422 return smartcard_ndr_read(log, s, u.ppv, 0, sizeof(CHAR), type);
423}
424
425WINPR_ATTR_NODISCARD static LONG smartcard_ndr_read_w(wLog* log, wStream* s, WCHAR** data,
426 const ndr_ptr_t type)
427{
428 union
429 {
430 WCHAR** ppc;
431 BYTE** ppv;
432 } u;
433 u.ppc = data;
434 return smartcard_ndr_read(log, s, u.ppv, 0, sizeof(WCHAR), type);
435}
436
437WINPR_ATTR_NODISCARD static LONG smartcard_ndr_write_w(wStream* s, const WCHAR* data, UINT32 size,
438 const ndr_ptr_t type)
439{
440 union
441 {
442 const WCHAR* wz;
443 const BYTE* bp;
444 } cnv;
445 cnv.wz = data;
446 return smartcard_ndr_write(s, cnv.bp, size, sizeof(WCHAR), type);
447}
448
449WINPR_ATTR_NODISCARD static LONG smartcard_ndr_write_a(wStream* s, const CHAR* data, UINT32 size,
450 const ndr_ptr_t type)
451{
452 union
453 {
454 const CHAR* sz;
455 const BYTE* bp;
456 } cnv;
457 cnv.sz = data;
458 return smartcard_ndr_write(s, cnv.bp, size, sizeof(CHAR), type);
459}
460
461WINPR_ATTR_NODISCARD static LONG smartcard_ndr_read_u(wLog* log, wStream* s, UUID** data)
462{
463 union
464 {
465 UUID** ppc;
466 BYTE** ppv;
467 } u;
468 u.ppc = data;
469 return smartcard_ndr_read(log, s, u.ppv, 1, sizeof(UUID), NDR_PTR_FIXED);
470}
471
472static char* smartcard_convert_string_list(const void* in, size_t bytes, BOOL unicode)
473{
474 size_t length = 0;
475 union
476 {
477 const void* pv;
478 const char* sz;
479 const WCHAR* wz;
480 } string;
481 char* mszA = nullptr;
482
483 string.pv = in;
484
485 if (bytes < 1)
486 return nullptr;
487
488 if (in == nullptr)
489 return nullptr;
490
491 if (unicode)
492 {
493 mszA = ConvertMszWCharNToUtf8Alloc(string.wz, bytes / sizeof(WCHAR), &length);
494 if (!mszA)
495 return nullptr;
496 }
497 else
498 {
499 mszA = (char*)calloc(bytes, sizeof(char));
500 if (!mszA)
501 return nullptr;
502 CopyMemory(mszA, string.sz, bytes - 1);
503 length = bytes;
504 }
505
506 if (length < 1)
507 {
508 free(mszA);
509 return nullptr;
510 }
511 for (size_t index = 0; index < length - 1; index++)
512 {
513 if (mszA[index] == '\0')
514 mszA[index] = ',';
515 }
516
517 return mszA;
518}
519
520WINPR_ATTR_MALLOC(free, 1)
521static char* smartcard_create_msz_dump(const char* msz, size_t len)
522{
523 size_t bufferLen = len;
524 char* buffer = calloc(len + 1, 1);
525 if (!buffer)
526 return nullptr;
527
528 char* buf = buffer;
529 const char* cur = msz;
530
531 while ((len > 0) && cur && cur[0] != '\0' && (bufferLen > 0))
532 {
533 size_t clen = strnlen(cur, len);
534 int rc = _snprintf(buf, bufferLen, "%s", cur);
535 bufferLen -= (size_t)rc;
536 buf += rc;
537
538 cur += clen;
539 }
540
541 return buffer;
542}
543
544static void smartcard_msz_dump(wLog* log, DWORD level, const char* prefix, const void* data,
545 size_t len, UINT32 wchar)
546{
547 if (!WLog_IsLevelActive(log, level))
548 return;
549
550 char* tmp = nullptr;
551 const char* msz = WINPR_CXX_COMPAT_CAST(const char*, data);
552 size_t mszlen = len;
553 if (wchar)
554 {
555 tmp = ConvertMszWCharNToUtf8Alloc(data, len, &mszlen);
556 msz = tmp;
557 }
558
559 char* array = smartcard_create_msz_dump(msz, mszlen);
560 WLog_Print(log, level, "%s%s", prefix, array);
561 free(array);
562 free(tmp);
563}
564
565WINPR_ATTR_MALLOC(free, 1)
566static char* smartcard_create_array_dump(const void* pd, size_t len)
567{
568 const BYTE* data = pd;
569 int rc = 0;
570
571 size_t bufferLen = len * 4;
572 if (bufferLen < 32)
573 bufferLen = 32;
574 char* buffer = calloc(bufferLen + 1, 1);
575 if (!buffer)
576 return nullptr;
577 char* start = buffer;
578
579 WINPR_ASSERT(buffer || (bufferLen == 0));
580
581 if (!data && (len > 0))
582 {
583 (void)_snprintf(buffer, bufferLen, "{ nullptr [%" PRIuz "] }", len);
584 goto fail;
585 }
586
587 rc = _snprintf(buffer, bufferLen, "{ ");
588 if ((rc < 0) || ((size_t)rc >= bufferLen))
589 goto fail;
590 buffer += rc;
591 bufferLen -= (size_t)rc;
592
593 for (size_t x = 0; x < len; x++)
594 {
595 rc = _snprintf(buffer, bufferLen, "%02X", data[x]);
596 if ((rc < 0) || ((size_t)rc >= bufferLen))
597 goto fail;
598 buffer += rc;
599 bufferLen -= (size_t)rc;
600 }
601
602 rc = _snprintf(buffer, bufferLen, " }");
603 if ((rc < 0) || ((size_t)rc >= bufferLen))
604 goto fail;
605
606fail:
607 return start;
608}
609
610WINPR_ATTR_FORMAT_ARG(3, 7)
611static void smartcard_dump_array(wLog* log, DWORD level, WINPR_FORMAT_ARG const char* prefix,
612 const char* postfix, const void* data, size_t len, ...)
613{
614 if (!WLog_IsLevelActive(log, level))
615 return;
616
617 char* buffer = smartcard_create_array_dump(data, len);
618
619 char* fprefix = nullptr;
620 size_t flen = 0;
621 va_list ap = WINPR_C_ARRAY_INIT;
622 va_start(ap, len);
623 winpr_vasprintf(&fprefix, &flen, prefix, ap);
624 va_end(ap);
625 WLog_Print(log, level, "%s%s%s", fprefix, buffer, postfix);
626 free(buffer);
627 free(fprefix);
628}
629
630static void smartcard_log_redir_handle(wLog* log, const REDIR_SCARDHANDLE* pHandle)
631{
632 WINPR_ASSERT(pHandle);
633 smartcard_dump_array(log, g_LogLevel, "handle: ", "", pHandle->pbHandle, pHandle->cbHandle);
634}
635
636static void smartcard_log_context(wLog* log, const REDIR_SCARDCONTEXT* phContext)
637{
638 WINPR_ASSERT(phContext);
639 smartcard_dump_array(log, g_LogLevel, "hContext: ", "", phContext->pbContext,
640 phContext->cbContext);
641}
642
643static void smartcard_trace_context_and_string_call_a(wLog* log, const char* name,
644 const REDIR_SCARDCONTEXT* phContext,
645 const CHAR* sz)
646{
647 if (!WLog_IsLevelActive(log, g_LogLevel))
648 return;
649
650 WLog_Print(log, g_LogLevel, "%s {", name);
651 smartcard_log_context(log, phContext);
652 WLog_Print(log, g_LogLevel, " sz=%s", sz);
653
654 WLog_Print(log, g_LogLevel, "}");
655}
656
657static void smartcard_trace_context_and_string_call_w(wLog* log, const char* name,
658 const REDIR_SCARDCONTEXT* phContext,
659 const WCHAR* sz)
660{
661 char* tmp = nullptr;
662
663 if (!WLog_IsLevelActive(log, g_LogLevel))
664 return;
665
666 if (sz)
667 tmp = ConvertWCharToUtf8Alloc(sz, nullptr);
668
669 WLog_Print(log, g_LogLevel, "%s {", name);
670 smartcard_log_context(log, phContext);
671 WLog_Print(log, g_LogLevel, " sz=%s", tmp);
672 WLog_Print(log, g_LogLevel, "}");
673 free(tmp);
674}
675
676static void smartcard_trace_context_call(wLog* log, const Context_Call* call, const char* name)
677{
678 WINPR_ASSERT(call);
679
680 if (!WLog_IsLevelActive(log, g_LogLevel))
681 return;
682
683 WLog_Print(log, g_LogLevel, "%s_Call {", name);
684 smartcard_log_context(log, &call->handles.hContext);
685
686 WLog_Print(log, g_LogLevel, "}");
687}
688
689static void smartcard_trace_list_reader_groups_call(wLog* log, const ListReaderGroups_Call* call,
690 BOOL unicode)
691{
692 WINPR_ASSERT(call);
693
694 if (!WLog_IsLevelActive(log, g_LogLevel))
695 return;
696
697 WLog_Print(log, g_LogLevel, "ListReaderGroups%s_Call {", unicode ? "W" : "A");
698 smartcard_log_context(log, &call->handles.hContext);
699
700 WLog_Print(log, g_LogLevel, "fmszGroupsIsNULL: %" PRId32 " cchGroups: 0x%08" PRIx32,
701 call->fmszGroupsIsNULL, call->cchGroups);
702 WLog_Print(log, g_LogLevel, "}");
703}
704
705static void dump_reader_states_return(wLog* log, const ReaderState_Return* rgReaderStates,
706 UINT32 cReaders)
707{
708 WINPR_ASSERT(rgReaderStates || (cReaders == 0));
709 for (UINT32 index = 0; index < cReaders; index++)
710 {
711 const ReaderState_Return* readerState = &rgReaderStates[index];
712 char* szCurrentState = SCardGetReaderStateString(readerState->dwCurrentState);
713 char* szEventState = SCardGetReaderStateString(readerState->dwEventState);
714 WLog_Print(log, g_LogLevel, "\t[%" PRIu32 "]: dwCurrentState: %s (0x%08" PRIX32 ")", index,
715 szCurrentState, readerState->dwCurrentState);
716 WLog_Print(log, g_LogLevel, "\t[%" PRIu32 "]: dwEventState: %s (0x%08" PRIX32 ")", index,
717 szEventState, readerState->dwEventState);
718 free(szCurrentState);
719 free(szEventState);
720
721 smartcard_dump_array(log, g_LogLevel, "\t[%" PRIu32 "]: cbAttr: %" PRIu32 " { ", " }",
722 readerState->rgbAtr, readerState->cbAtr, index, readerState->cbAtr);
723 }
724}
725
726static void dump_reader_states_a(wLog* log, const SCARD_READERSTATEA* rgReaderStates,
727 UINT32 cReaders)
728{
729 WINPR_ASSERT(rgReaderStates || (cReaders == 0));
730 for (UINT32 index = 0; index < cReaders; index++)
731 {
732 const SCARD_READERSTATEA* readerState = &rgReaderStates[index];
733
734 WLog_Print(log, g_LogLevel, "\t[%" PRIu32 "]: szReader: %s cbAtr: %" PRIu32 "", index,
735 readerState->szReader, readerState->cbAtr);
736 char* szCurrentState = SCardGetReaderStateString(readerState->dwCurrentState);
737 char* szEventState = SCardGetReaderStateString(readerState->dwEventState);
738 WLog_Print(log, g_LogLevel, "\t[%" PRIu32 "]: dwCurrentState: %s (0x%08" PRIX32 ")", index,
739 szCurrentState, readerState->dwCurrentState);
740 WLog_Print(log, g_LogLevel, "\t[%" PRIu32 "]: dwEventState: %s (0x%08" PRIX32 ")", index,
741 szEventState, readerState->dwEventState);
742 free(szCurrentState);
743 free(szEventState);
744
745 smartcard_dump_array(log, g_LogLevel, "\t[%" PRIu32 "]: cbAttr: %" PRIu32 " { ", " }",
746 readerState->rgbAtr, readerState->cbAtr, index, readerState->cbAtr);
747 }
748}
749
750static void dump_reader_states_w(wLog* log, const SCARD_READERSTATEW* rgReaderStates,
751 UINT32 cReaders)
752{
753 WINPR_ASSERT(rgReaderStates || (cReaders == 0));
754 for (UINT32 index = 0; index < cReaders; index++)
755 {
756 const SCARD_READERSTATEW* readerState = &rgReaderStates[index];
757 char* buffer = ConvertWCharToUtf8Alloc(readerState->szReader, nullptr);
758 WLog_Print(log, g_LogLevel, "\t[%" PRIu32 "]: szReader: %s cbAtr: %" PRIu32 "", index,
759 buffer, readerState->cbAtr);
760 free(buffer);
761 char* szCurrentState = SCardGetReaderStateString(readerState->dwCurrentState);
762 char* szEventState = SCardGetReaderStateString(readerState->dwEventState);
763 WLog_Print(log, g_LogLevel, "\t[%" PRIu32 "]: dwCurrentState: %s (0x%08" PRIX32 ")", index,
764 szCurrentState, readerState->dwCurrentState);
765 WLog_Print(log, g_LogLevel, "\t[%" PRIu32 "]: dwEventState: %s (0x%08" PRIX32 ")", index,
766 szEventState, readerState->dwEventState);
767 free(szCurrentState);
768 free(szEventState);
769
770 smartcard_dump_array(log, g_LogLevel, "\t[%" PRIu32 "]: cbAttr: %" PRIu32 " { ", " }",
771 readerState->rgbAtr, readerState->cbAtr, index, readerState->cbAtr);
772 }
773}
774
775static void smartcard_trace_get_status_change_w_call(wLog* log, const GetStatusChangeW_Call* call)
776{
777 WINPR_ASSERT(call);
778
779 if (!WLog_IsLevelActive(log, g_LogLevel))
780 return;
781
782 WLog_Print(log, g_LogLevel, "GetStatusChangeW_Call {");
783 smartcard_log_context(log, &call->handles.hContext);
784
785 WLog_Print(log, g_LogLevel, "dwTimeOut: 0x%08" PRIX32 " cReaders: %" PRIu32 "", call->dwTimeOut,
786 call->cReaders);
787
788 dump_reader_states_w(log, call->rgReaderStates, call->cReaders);
789
790 WLog_Print(log, g_LogLevel, "}");
791}
792
793static void smartcard_trace_list_reader_groups_return(wLog* log, const ListReaderGroups_Return* ret,
794 BOOL unicode)
795{
796 WINPR_ASSERT(ret);
797
798 if (!WLog_IsLevelActive(log, g_LogLevel))
799 return;
800
801 char* mszA = smartcard_convert_string_list(ret->msz, ret->cBytes, unicode);
802
803 WLog_Print(log, g_LogLevel, "ListReaderGroups%s_Return {", unicode ? "W" : "A");
804 WLog_Print(log, g_LogLevel, " ReturnCode: %s (0x%08" PRIx32 ")",
805 SCardGetErrorString(ret->ReturnCode),
806 WINPR_CXX_COMPAT_CAST(UINT32, ret->ReturnCode));
807 WLog_Print(log, g_LogLevel, " cBytes: %" PRIu32 " msz: %s", ret->cBytes, mszA);
808 WLog_Print(log, g_LogLevel, "}");
809 free(mszA);
810}
811
812static void smartcard_trace_list_readers_call(wLog* log, const ListReaders_Call* call, BOOL unicode)
813{
814 WINPR_ASSERT(call);
815
816 if (!WLog_IsLevelActive(log, g_LogLevel))
817 return;
818
819 char* mszGroupsA = smartcard_convert_string_list(call->mszGroups, call->cBytes, unicode);
820
821 WLog_Print(log, g_LogLevel, "ListReaders%s_Call {", unicode ? "W" : "A");
822 smartcard_log_context(log, &call->handles.hContext);
823
824 WLog_Print(log, g_LogLevel,
825 "cBytes: %" PRIu32 " mszGroups: %s fmszReadersIsNULL: %" PRId32
826 " cchReaders: 0x%08" PRIX32 "",
827 call->cBytes, mszGroupsA, call->fmszReadersIsNULL, call->cchReaders);
828 WLog_Print(log, g_LogLevel, "}");
829
830 free(mszGroupsA);
831}
832
833static void smartcard_trace_locate_cards_by_atr_a_call(wLog* log,
834 const LocateCardsByATRA_Call* call)
835{
836 WINPR_ASSERT(call);
837
838 if (!WLog_IsLevelActive(log, g_LogLevel))
839 return;
840
841 WLog_Print(log, g_LogLevel, "LocateCardsByATRA_Call {");
842 smartcard_log_context(log, &call->handles.hContext);
843
844 dump_reader_states_a(log, call->rgReaderStates, call->cReaders);
845
846 WLog_Print(log, g_LogLevel, "}");
847}
848
849static void smartcard_trace_locate_cards_a_call(wLog* log, const LocateCardsA_Call* call)
850{
851 WINPR_ASSERT(call);
852
853 if (!WLog_IsLevelActive(log, g_LogLevel))
854 return;
855
856 WLog_Print(log, g_LogLevel, "LocateCardsA_Call {");
857 smartcard_log_context(log, &call->handles.hContext);
858 WLog_Print(log, g_LogLevel, " cBytes=%" PRIu32, call->cBytes);
859 smartcard_msz_dump(log, g_LogLevel, " mszCards=", call->mszCards, call->cBytes, FALSE);
860 WLog_Print(log, g_LogLevel, " cReaders=%" PRIu32, call->cReaders);
861 dump_reader_states_a(log, call->rgReaderStates, call->cReaders);
862
863 WLog_Print(log, g_LogLevel, "}");
864}
865
866static void smartcard_trace_locate_cards_return(wLog* log, const LocateCards_Return* ret)
867{
868 WINPR_ASSERT(ret);
869
870 if (!WLog_IsLevelActive(log, g_LogLevel))
871 return;
872
873 WLog_Print(log, g_LogLevel, "LocateCards_Return {");
874 WLog_Print(log, g_LogLevel, " ReturnCode: %s (0x%08" PRIX32 ")",
875 SCardGetErrorString(ret->ReturnCode),
876 WINPR_CXX_COMPAT_CAST(UINT32, ret->ReturnCode));
877
878 if (ret->ReturnCode == SCARD_S_SUCCESS)
879 {
880 WLog_Print(log, g_LogLevel, " cReaders=%" PRIu32, ret->cReaders);
881 }
882 WLog_Print(log, g_LogLevel, "}");
883}
884
885static void smartcard_trace_get_reader_icon_return(wLog* log, const GetReaderIcon_Return* ret)
886{
887 WINPR_ASSERT(ret);
888
889 if (!WLog_IsLevelActive(log, g_LogLevel))
890 return;
891
892 WLog_Print(log, g_LogLevel, "GetReaderIcon_Return {");
893 WLog_Print(log, g_LogLevel, " ReturnCode: %s (0x%08" PRIX32 ")",
894 SCardGetErrorString(ret->ReturnCode),
895 WINPR_CXX_COMPAT_CAST(UINT32, ret->ReturnCode));
896
897 if (ret->ReturnCode == SCARD_S_SUCCESS)
898 {
899 WLog_Print(log, g_LogLevel, " cbDataLen=%" PRIu32, ret->cbDataLen);
900 }
901 WLog_Print(log, g_LogLevel, "}");
902}
903
904static void smartcard_trace_get_transmit_count_return(wLog* log, const GetTransmitCount_Return* ret)
905{
906 WINPR_ASSERT(ret);
907
908 if (!WLog_IsLevelActive(log, g_LogLevel))
909 return;
910
911 WLog_Print(log, g_LogLevel, "GetTransmitCount_Return {");
912 WLog_Print(log, g_LogLevel, " ReturnCode: %s (0x%08" PRIX32 ")",
913 SCardGetErrorString(ret->ReturnCode),
914 WINPR_CXX_COMPAT_CAST(UINT32, ret->ReturnCode));
915
916 WLog_Print(log, g_LogLevel, " cTransmitCount=%" PRIu32, ret->cTransmitCount);
917 WLog_Print(log, g_LogLevel, "}");
918}
919
920static void smartcard_trace_read_cache_return(wLog* log, const ReadCache_Return* ret)
921{
922 WINPR_ASSERT(ret);
923
924 if (!WLog_IsLevelActive(log, g_LogLevel))
925 return;
926
927 WLog_Print(log, g_LogLevel, "ReadCache_Return {");
928 WLog_Print(log, g_LogLevel, " ReturnCode: %s (0x%08" PRIX32 ")",
929 SCardGetErrorString(ret->ReturnCode),
930 WINPR_CXX_COMPAT_CAST(UINT32, ret->ReturnCode));
931
932 if (ret->ReturnCode == SCARD_S_SUCCESS)
933 {
934 WLog_Print(log, g_LogLevel, " cbDataLen=%" PRIu32, ret->cbDataLen);
935 smartcard_dump_array(log, g_LogLevel, " cbData: ", "", ret->pbData, ret->cbDataLen);
936 }
937 WLog_Print(log, g_LogLevel, "}");
938}
939
940static void smartcard_trace_locate_cards_w_call(wLog* log, const LocateCardsW_Call* call)
941{
942 WINPR_ASSERT(call);
943
944 if (!WLog_IsLevelActive(log, g_LogLevel))
945 return;
946
947 WLog_Print(log, g_LogLevel, "LocateCardsW_Call {");
948 smartcard_log_context(log, &call->handles.hContext);
949 WLog_Print(log, g_LogLevel, " cBytes=%" PRIu32, call->cBytes);
950 smartcard_msz_dump(log, g_LogLevel, " sz2=", call->mszCards, call->cBytes, TRUE);
951 WLog_Print(log, g_LogLevel, " cReaders=%" PRIu32, call->cReaders);
952 dump_reader_states_w(log, call->rgReaderStates, call->cReaders);
953 WLog_Print(log, g_LogLevel, "}");
954}
955
956static void smartcard_trace_list_readers_return(wLog* log, const ListReaders_Return* ret,
957 BOOL unicode)
958{
959 WINPR_ASSERT(ret);
960
961 if (!WLog_IsLevelActive(log, g_LogLevel))
962 return;
963
964 WLog_Print(log, g_LogLevel, "ListReaders%s_Return {", unicode ? "W" : "A");
965 WLog_Print(log, g_LogLevel, " ReturnCode: %s (0x%08" PRIX32 ")",
966 SCardGetErrorString(ret->ReturnCode),
967 WINPR_CXX_COMPAT_CAST(UINT32, ret->ReturnCode));
968
969 if (ret->ReturnCode != SCARD_S_SUCCESS)
970 {
971 WLog_Print(log, g_LogLevel, "}");
972 return;
973 }
974
975 char* mszA = smartcard_convert_string_list(ret->msz, ret->cBytes, unicode);
976
977 WLog_Print(log, g_LogLevel, " cBytes: %" PRIu32 " msz: %s", ret->cBytes, mszA);
978 WLog_Print(log, g_LogLevel, "}");
979 free(mszA);
980}
981
982static void smartcard_trace_get_status_change_return(wLog* log, const GetStatusChange_Return* ret,
983 BOOL unicode)
984{
985 WINPR_ASSERT(ret);
986
987 if (!WLog_IsLevelActive(log, g_LogLevel))
988 return;
989
990 WLog_Print(log, g_LogLevel, "GetStatusChange%s_Return {", unicode ? "W" : "A");
991 WLog_Print(log, g_LogLevel, " ReturnCode: %s (0x%08" PRIX32 ")",
992 SCardGetErrorString(ret->ReturnCode),
993 WINPR_CXX_COMPAT_CAST(UINT32, ret->ReturnCode));
994 WLog_Print(log, g_LogLevel, " cReaders: %" PRIu32 "", ret->cReaders);
995
996 dump_reader_states_return(log, ret->rgReaderStates, ret->cReaders);
997
998 if (!ret->rgReaderStates && (ret->cReaders > 0))
999 {
1000 WLog_Print(log, g_LogLevel, " [INVALID STATE] rgReaderStates=nullptr, cReaders=%" PRIu32,
1001 ret->cReaders);
1002 }
1003 else if (ret->ReturnCode != SCARD_S_SUCCESS)
1004 {
1005 WLog_Print(log, g_LogLevel, " [INVALID RETURN] rgReaderStates, cReaders=%" PRIu32,
1006 ret->cReaders);
1007 }
1008 else
1009 {
1010 for (UINT32 index = 0; index < ret->cReaders; index++)
1011 {
1012 const ReaderState_Return* rgReaderState = &(ret->rgReaderStates[index]);
1013 char* szCurrentState = SCardGetReaderStateString(rgReaderState->dwCurrentState);
1014 char* szEventState = SCardGetReaderStateString(rgReaderState->dwEventState);
1015 WLog_Print(log, g_LogLevel, " [%" PRIu32 "]: dwCurrentState: %s (0x%08" PRIX32 ")",
1016 index, szCurrentState, rgReaderState->dwCurrentState);
1017 WLog_Print(log, g_LogLevel, " [%" PRIu32 "]: dwEventState: %s (0x%08" PRIX32 ")",
1018 index, szEventState, rgReaderState->dwEventState);
1019 smartcard_dump_array(
1020 log, g_LogLevel, " [%" PRIu32 "]: cbAtr: %" PRIu32 " rgbAtr: ", "",
1021 rgReaderState->rgbAtr, rgReaderState->cbAtr, index, rgReaderState->cbAtr);
1022 free(szCurrentState);
1023 free(szEventState);
1024 }
1025 }
1026
1027 WLog_Print(log, g_LogLevel, "}");
1028}
1029
1030static void smartcard_trace_context_and_two_strings_a_call(wLog* log,
1031 const ContextAndTwoStringA_Call* call)
1032{
1033 WINPR_ASSERT(call);
1034
1035 if (!WLog_IsLevelActive(log, g_LogLevel))
1036 return;
1037
1038 WLog_Print(log, g_LogLevel, "ContextAndTwoStringW_Call {");
1039 smartcard_log_context(log, &call->handles.hContext);
1040 WLog_Print(log, g_LogLevel, " sz1=%s", call->sz1);
1041 WLog_Print(log, g_LogLevel, " sz2=%s", call->sz2);
1042 WLog_Print(log, g_LogLevel, "}");
1043}
1044
1045static void smartcard_trace_context_and_two_strings_w_call(wLog* log,
1046 const ContextAndTwoStringW_Call* call)
1047{
1048 WINPR_ASSERT(call);
1049 char sz1[1024] = WINPR_C_ARRAY_INIT;
1050 char sz2[1024] = WINPR_C_ARRAY_INIT;
1051
1052 if (!WLog_IsLevelActive(log, g_LogLevel))
1053 return;
1054 if (call->sz1)
1055 (void)ConvertWCharToUtf8(call->sz1, sz1, ARRAYSIZE(sz1));
1056 if (call->sz2)
1057 (void)ConvertWCharToUtf8(call->sz2, sz2, ARRAYSIZE(sz2));
1058
1059 WLog_Print(log, g_LogLevel, "ContextAndTwoStringW_Call {");
1060 smartcard_log_context(log, &call->handles.hContext);
1061 WLog_Print(log, g_LogLevel, " sz1=%s", sz1);
1062 WLog_Print(log, g_LogLevel, " sz2=%s", sz2);
1063 WLog_Print(log, g_LogLevel, "}");
1064}
1065
1066static void smartcard_trace_get_transmit_count_call(wLog* log, const GetTransmitCount_Call* call)
1067{
1068 WINPR_ASSERT(call);
1069
1070 if (!WLog_IsLevelActive(log, g_LogLevel))
1071 return;
1072
1073 WLog_Print(log, g_LogLevel, "GetTransmitCount_Call {");
1074 smartcard_log_context(log, &call->handles.hContext);
1075 smartcard_log_redir_handle(log, &call->handles.hCard);
1076
1077 WLog_Print(log, g_LogLevel, "}");
1078}
1079
1080static void smartcard_trace_write_cache_a_call(wLog* log, const WriteCacheA_Call* call)
1081{
1082 WINPR_ASSERT(call);
1083
1084 if (!WLog_IsLevelActive(log, g_LogLevel))
1085 return;
1086
1087 WLog_Print(log, g_LogLevel, "WriteCacheA_Call {");
1088
1089 WLog_Print(log, g_LogLevel, " szLookupName=%s", call->szLookupName);
1090
1091 smartcard_log_context(log, &call->Common.handles.hContext);
1092 smartcard_dump_array(log, g_LogLevel, "..CardIdentifier=", "", call->Common.CardIdentifier,
1093 sizeof(UUID));
1094 WLog_Print(log, g_LogLevel, " FreshnessCounter=%" PRIu32, call->Common.FreshnessCounter);
1095 WLog_Print(log, g_LogLevel, " cbDataLen=%" PRIu32, call->Common.cbDataLen);
1096 smartcard_dump_array(log, g_LogLevel, " pbData=", "", call->Common.pbData,
1097 call->Common.cbDataLen);
1098 WLog_Print(log, g_LogLevel, "}");
1099}
1100
1101static void smartcard_trace_write_cache_w_call(wLog* log, const WriteCacheW_Call* call)
1102{
1103 WINPR_ASSERT(call);
1104 char tmp[1024] = WINPR_C_ARRAY_INIT;
1105
1106 if (!WLog_IsLevelActive(log, g_LogLevel))
1107 return;
1108
1109 WLog_Print(log, g_LogLevel, "WriteCacheW_Call {");
1110
1111 if (call->szLookupName)
1112 (void)ConvertWCharToUtf8(call->szLookupName, tmp, ARRAYSIZE(tmp));
1113 WLog_Print(log, g_LogLevel, " szLookupName=%s", tmp);
1114
1115 smartcard_log_context(log, &call->Common.handles.hContext);
1116 smartcard_dump_array(log, g_LogLevel, "..CardIdentifier=", "", call->Common.CardIdentifier,
1117 sizeof(UUID));
1118 WLog_Print(log, g_LogLevel, " FreshnessCounter=%" PRIu32, call->Common.FreshnessCounter);
1119 WLog_Print(log, g_LogLevel, " cbDataLen=%" PRIu32, call->Common.cbDataLen);
1120 smartcard_dump_array(log, g_LogLevel, " pbData=", "", call->Common.pbData,
1121 call->Common.cbDataLen);
1122 WLog_Print(log, g_LogLevel, "}");
1123}
1124
1125static void smartcard_trace_read_cache_a_call(wLog* log, const ReadCacheA_Call* call)
1126{
1127 WINPR_ASSERT(call);
1128
1129 if (!WLog_IsLevelActive(log, g_LogLevel))
1130 return;
1131
1132 WLog_Print(log, g_LogLevel, "ReadCacheA_Call {");
1133
1134 WLog_Print(log, g_LogLevel, " szLookupName=%s", call->szLookupName);
1135 smartcard_log_context(log, &call->Common.handles.hContext);
1136 smartcard_dump_array(log, g_LogLevel, "..CardIdentifier=", "", call->Common.CardIdentifier,
1137 sizeof(UUID));
1138 WLog_Print(log, g_LogLevel, " FreshnessCounter=%" PRIu32, call->Common.FreshnessCounter);
1139 WLog_Print(log, g_LogLevel, " fPbDataIsNULL=%" PRId32, call->Common.fPbDataIsNULL);
1140 WLog_Print(log, g_LogLevel, " cbDataLen=%" PRIu32, call->Common.cbDataLen);
1141
1142 WLog_Print(log, g_LogLevel, "}");
1143}
1144
1145static void smartcard_trace_read_cache_w_call(wLog* log, const ReadCacheW_Call* call)
1146{
1147 WINPR_ASSERT(call);
1148 char tmp[1024] = WINPR_C_ARRAY_INIT;
1149
1150 if (!WLog_IsLevelActive(log, g_LogLevel))
1151 return;
1152
1153 WLog_Print(log, g_LogLevel, "ReadCacheW_Call {");
1154 if (call->szLookupName)
1155 (void)ConvertWCharToUtf8(call->szLookupName, tmp, ARRAYSIZE(tmp));
1156 WLog_Print(log, g_LogLevel, " szLookupName=%s", tmp);
1157
1158 smartcard_log_context(log, &call->Common.handles.hContext);
1159 smartcard_dump_array(log, g_LogLevel, "..CardIdentifier=", "", call->Common.CardIdentifier,
1160 sizeof(UUID));
1161 WLog_Print(log, g_LogLevel, " FreshnessCounter=%" PRIu32, call->Common.FreshnessCounter);
1162 WLog_Print(log, g_LogLevel, " fPbDataIsNULL=%" PRId32, call->Common.fPbDataIsNULL);
1163 WLog_Print(log, g_LogLevel, " cbDataLen=%" PRIu32, call->Common.cbDataLen);
1164
1165 WLog_Print(log, g_LogLevel, "}");
1166}
1167
1168static void smartcard_trace_transmit_call(wLog* log, const Transmit_Call* call)
1169{
1170 WINPR_ASSERT(call);
1171 UINT32 cbExtraBytes = 0;
1172 BYTE* pbExtraBytes = nullptr;
1173
1174 if (!WLog_IsLevelActive(log, g_LogLevel))
1175 return;
1176
1177 WLog_Print(log, g_LogLevel, "Transmit_Call {");
1178 smartcard_log_context(log, &call->handles.hContext);
1179 smartcard_log_redir_handle(log, &call->handles.hCard);
1180
1181 if (call->pioSendPci)
1182 {
1183 cbExtraBytes = (UINT32)(call->pioSendPci->cbPciLength - sizeof(SCARD_IO_REQUEST));
1184 pbExtraBytes = &((BYTE*)call->pioSendPci)[sizeof(SCARD_IO_REQUEST)];
1185 WLog_Print(log, g_LogLevel, "pioSendPci: dwProtocol: %" PRIu32 " cbExtraBytes: %" PRIu32 "",
1186 call->pioSendPci->dwProtocol, cbExtraBytes);
1187
1188 if (cbExtraBytes)
1189 {
1190 smartcard_dump_array(log, g_LogLevel, "pbExtraBytes: ", "", pbExtraBytes, cbExtraBytes);
1191 }
1192 }
1193 else
1194 {
1195 WLog_Print(log, g_LogLevel, "pioSendPci: null");
1196 }
1197
1198 WLog_Print(log, g_LogLevel, "cbSendLength: %" PRIu32 "", call->cbSendLength);
1199
1200 if (call->pbSendBuffer)
1201 {
1202 smartcard_dump_array(log, g_LogLevel, "pbSendBuffer: ", "", call->pbSendBuffer,
1203 call->cbSendLength);
1204 }
1205 else
1206 {
1207 WLog_Print(log, g_LogLevel, "pbSendBuffer: null");
1208 }
1209
1210 if (call->pioRecvPci)
1211 {
1212 cbExtraBytes = (UINT32)(call->pioRecvPci->cbPciLength - sizeof(SCARD_IO_REQUEST));
1213 pbExtraBytes = &((BYTE*)call->pioRecvPci)[sizeof(SCARD_IO_REQUEST)];
1214 WLog_Print(log, g_LogLevel, "pioRecvPci: dwProtocol: %" PRIu32 " cbExtraBytes: %" PRIu32 "",
1215 call->pioRecvPci->dwProtocol, cbExtraBytes);
1216
1217 if (cbExtraBytes)
1218 {
1219 smartcard_dump_array(log, g_LogLevel, "pbExtraBytes: ", "", pbExtraBytes, cbExtraBytes);
1220 }
1221 }
1222 else
1223 {
1224 WLog_Print(log, g_LogLevel, "pioRecvPci: null");
1225 }
1226
1227 WLog_Print(log, g_LogLevel, "fpbRecvBufferIsNULL: %" PRId32 " cbRecvLength: %" PRIu32 "",
1228 call->fpbRecvBufferIsNULL, call->cbRecvLength);
1229 WLog_Print(log, g_LogLevel, "}");
1230}
1231
1232static void smartcard_trace_locate_cards_by_atr_w_call(wLog* log,
1233 const LocateCardsByATRW_Call* call)
1234{
1235 WINPR_ASSERT(call);
1236
1237 if (!WLog_IsLevelActive(log, g_LogLevel))
1238 return;
1239
1240 WLog_Print(log, g_LogLevel, "LocateCardsByATRW_Call {");
1241 smartcard_log_context(log, &call->handles.hContext);
1242
1243 dump_reader_states_w(log, call->rgReaderStates, call->cReaders);
1244
1245 WLog_Print(log, g_LogLevel, "}");
1246}
1247
1248static void smartcard_trace_transmit_return(wLog* log, const Transmit_Return* ret)
1249{
1250 WINPR_ASSERT(ret);
1251 UINT32 cbExtraBytes = 0;
1252 BYTE* pbExtraBytes = nullptr;
1253
1254 if (!WLog_IsLevelActive(log, g_LogLevel))
1255 return;
1256
1257 WLog_Print(log, g_LogLevel, "Transmit_Return {");
1258 WLog_Print(log, g_LogLevel, " ReturnCode: %s (0x%08" PRIX32 ")",
1259 SCardGetErrorString(ret->ReturnCode),
1260 WINPR_CXX_COMPAT_CAST(UINT32, ret->ReturnCode));
1261
1262 if (ret->pioRecvPci)
1263 {
1264 cbExtraBytes = (UINT32)(ret->pioRecvPci->cbPciLength - sizeof(SCARD_IO_REQUEST));
1265 pbExtraBytes = &((BYTE*)ret->pioRecvPci)[sizeof(SCARD_IO_REQUEST)];
1266 WLog_Print(log, g_LogLevel,
1267 " pioRecvPci: dwProtocol: %" PRIu32 " cbExtraBytes: %" PRIu32 "",
1268 ret->pioRecvPci->dwProtocol, cbExtraBytes);
1269
1270 if (cbExtraBytes)
1271 {
1272 smartcard_dump_array(log, g_LogLevel, " pbExtraBytes: ", "", pbExtraBytes,
1273 cbExtraBytes);
1274 }
1275 }
1276 else
1277 {
1278 WLog_Print(log, g_LogLevel, " pioRecvPci: null");
1279 }
1280
1281 WLog_Print(log, g_LogLevel, " cbRecvLength: %" PRIu32 "", ret->cbRecvLength);
1282
1283 if (ret->pbRecvBuffer)
1284 {
1285 smartcard_dump_array(log, g_LogLevel, " pbRecvBuffer: ", "", ret->pbRecvBuffer,
1286 ret->cbRecvLength);
1287 }
1288 else
1289 {
1290 WLog_Print(log, g_LogLevel, " pbRecvBuffer: null");
1291 }
1292
1293 WLog_Print(log, g_LogLevel, "}");
1294}
1295
1296static void smartcard_trace_control_return(wLog* log, const Control_Return* ret)
1297{
1298 WINPR_ASSERT(ret);
1299
1300 if (!WLog_IsLevelActive(log, g_LogLevel))
1301 return;
1302
1303 WLog_Print(log, g_LogLevel, "Control_Return {");
1304 WLog_Print(log, g_LogLevel, " ReturnCode: %s (0x%08" PRIX32 ")",
1305 SCardGetErrorString(ret->ReturnCode),
1306 WINPR_CXX_COMPAT_CAST(UINT32, ret->ReturnCode));
1307 WLog_Print(log, g_LogLevel, " cbOutBufferSize: %" PRIu32 "", ret->cbOutBufferSize);
1308
1309 if (ret->pvOutBuffer)
1310 {
1311 smartcard_dump_array(log, g_LogLevel, "pvOutBuffer: ", "", ret->pvOutBuffer,
1312 ret->cbOutBufferSize);
1313 }
1314 else
1315 {
1316 WLog_Print(log, g_LogLevel, "pvOutBuffer: null");
1317 }
1318
1319 WLog_Print(log, g_LogLevel, "}");
1320}
1321
1322static void smartcard_trace_control_call(wLog* log, const Control_Call* call)
1323{
1324 WINPR_ASSERT(call);
1325
1326 if (!WLog_IsLevelActive(log, g_LogLevel))
1327 return;
1328
1329 WLog_Print(log, g_LogLevel, "Control_Call {");
1330 smartcard_log_context(log, &call->handles.hContext);
1331 smartcard_log_redir_handle(log, &call->handles.hCard);
1332
1333 WLog_Print(log, g_LogLevel,
1334 "dwControlCode: 0x%08" PRIX32 " cbInBufferSize: %" PRIu32
1335 " fpvOutBufferIsNULL: %" PRId32 " cbOutBufferSize: %" PRIu32 "",
1336 call->dwControlCode, call->cbInBufferSize, call->fpvOutBufferIsNULL,
1337 call->cbOutBufferSize);
1338
1339 if (call->pvInBuffer)
1340 {
1341 smartcard_dump_array(log, WLOG_DEBUG, "pbInBuffer: ", "", call->pvInBuffer,
1342 call->cbInBufferSize);
1343 }
1344 else
1345 {
1346 WLog_Print(log, g_LogLevel, "pvInBuffer: null");
1347 }
1348
1349 WLog_Print(log, g_LogLevel, "}");
1350}
1351
1352static void smartcard_trace_set_attrib_call(wLog* log, const SetAttrib_Call* call)
1353{
1354 WINPR_ASSERT(call);
1355
1356 if (!WLog_IsLevelActive(log, g_LogLevel))
1357 return;
1358
1359 WLog_Print(log, g_LogLevel, "GetAttrib_Call {");
1360 smartcard_log_context(log, &call->handles.hContext);
1361 smartcard_log_redir_handle(log, &call->handles.hCard);
1362 WLog_Print(log, g_LogLevel, "dwAttrId: 0x%08" PRIX32, call->dwAttrId);
1363 WLog_Print(log, g_LogLevel, "cbAttrLen: 0x%08" PRIx32, call->cbAttrLen);
1364 smartcard_dump_array(log, g_LogLevel, "pbAttr: ", "", call->pbAttr, call->cbAttrLen);
1365 WLog_Print(log, g_LogLevel, "}");
1366}
1367
1368static void smartcard_trace_get_attrib_return(wLog* log, const GetAttrib_Return* ret,
1369 DWORD dwAttrId)
1370{
1371 WINPR_ASSERT(ret);
1372
1373 if (!WLog_IsLevelActive(log, g_LogLevel))
1374 return;
1375
1376 WLog_Print(log, g_LogLevel, "GetAttrib_Return {");
1377 WLog_Print(log, g_LogLevel, " ReturnCode: %s (0x%08" PRIX32 ")",
1378 SCardGetErrorString(ret->ReturnCode),
1379 WINPR_CXX_COMPAT_CAST(UINT32, ret->ReturnCode));
1380 WLog_Print(log, g_LogLevel, " dwAttrId: %s (0x%08" PRIX32 ") cbAttrLen: 0x%08" PRIX32 "",
1381 SCardGetAttributeString(dwAttrId), dwAttrId, ret->cbAttrLen);
1382 smartcard_dump_array(log, g_LogLevel, " ", "", ret->pbAttr, ret->cbAttrLen);
1383
1384 WLog_Print(log, g_LogLevel, "}");
1385}
1386
1387static void smartcard_trace_get_attrib_call(wLog* log, const GetAttrib_Call* call)
1388{
1389 WINPR_ASSERT(call);
1390
1391 if (!WLog_IsLevelActive(log, g_LogLevel))
1392 return;
1393
1394 WLog_Print(log, g_LogLevel, "GetAttrib_Call {");
1395 smartcard_log_context(log, &call->handles.hContext);
1396 smartcard_log_redir_handle(log, &call->handles.hCard);
1397
1398 WLog_Print(log, g_LogLevel,
1399 "dwAttrId: %s (0x%08" PRIX32 ") fpbAttrIsNULL: %" PRId32 " cbAttrLen: 0x%08" PRIX32
1400 "",
1401 SCardGetAttributeString(call->dwAttrId), call->dwAttrId, call->fpbAttrIsNULL,
1402 call->cbAttrLen);
1403 WLog_Print(log, g_LogLevel, "}");
1404}
1405
1406static void smartcard_trace_status_call(wLog* log, const Status_Call* call, BOOL unicode)
1407{
1408 WINPR_ASSERT(call);
1409
1410 if (!WLog_IsLevelActive(log, g_LogLevel))
1411 return;
1412
1413 WLog_Print(log, g_LogLevel, "Status%s_Call {", unicode ? "W" : "A");
1414 smartcard_log_context(log, &call->handles.hContext);
1415 smartcard_log_redir_handle(log, &call->handles.hCard);
1416
1417 WLog_Print(log, g_LogLevel,
1418 "fmszReaderNamesIsNULL: %" PRId32 " cchReaderLen: %" PRIu32 " cbAtrLen: %" PRIu32 "",
1419 call->fmszReaderNamesIsNULL, call->cchReaderLen, call->cbAtrLen);
1420 WLog_Print(log, g_LogLevel, "}");
1421}
1422
1423static void smartcard_trace_status_return(wLog* log, const Status_Return* ret, BOOL unicode)
1424{
1425 WINPR_ASSERT(ret);
1426 char* mszReaderNamesA = nullptr;
1427 UINT32 cBytes = 0;
1428
1429 if (!WLog_IsLevelActive(log, g_LogLevel))
1430 return;
1431 cBytes = ret->cBytes;
1432 if (ret->ReturnCode != SCARD_S_SUCCESS)
1433 cBytes = 0;
1434 if (cBytes == SCARD_AUTOALLOCATE)
1435 cBytes = 0;
1436 mszReaderNamesA = smartcard_convert_string_list(ret->mszReaderNames, cBytes, unicode);
1437
1438 WLog_Print(log, g_LogLevel, "Status%s_Return {", unicode ? "W" : "A");
1439 WLog_Print(log, g_LogLevel, " ReturnCode: %s (0x%08" PRIX32 ")",
1440 SCardGetErrorString(ret->ReturnCode),
1441 WINPR_CXX_COMPAT_CAST(UINT32, ret->ReturnCode));
1442 WLog_Print(log, g_LogLevel, " dwState: %s (0x%08" PRIX32 ") dwProtocol: %s (0x%08" PRIX32 ")",
1443 SCardGetCardStateString(ret->dwState), ret->dwState,
1444 SCardGetProtocolString(ret->dwProtocol), ret->dwProtocol);
1445
1446 WLog_Print(log, g_LogLevel, " cBytes: %" PRIu32 " mszReaderNames: %s", ret->cBytes,
1447 mszReaderNamesA);
1448
1449 smartcard_dump_array(log, g_LogLevel, " cbAtrLen: %" PRIu32 " pbAtr: ", "", ret->pbAtr,
1450 ret->cbAtrLen, ret->cbAtrLen);
1451 WLog_Print(log, g_LogLevel, "}");
1452 free(mszReaderNamesA);
1453}
1454
1455static void smartcard_trace_state_return(wLog* log, const State_Return* ret)
1456{
1457 WINPR_ASSERT(ret);
1458 char* state = nullptr;
1459
1460 if (!WLog_IsLevelActive(log, g_LogLevel))
1461 return;
1462
1463 state = SCardGetReaderStateString(ret->dwState);
1464 WLog_Print(log, g_LogLevel, "Reconnect_Return {");
1465 WLog_Print(log, g_LogLevel, " ReturnCode: %s (0x%08" PRIX32 ")",
1466 SCardGetErrorString(ret->ReturnCode),
1467 WINPR_CXX_COMPAT_CAST(UINT32, ret->ReturnCode));
1468 WLog_Print(log, g_LogLevel, " dwState: %s (0x%08" PRIX32 ")", state, ret->dwState);
1469 WLog_Print(log, g_LogLevel, " dwProtocol: %s (0x%08" PRIX32 ")",
1470 SCardGetProtocolString(ret->dwProtocol), ret->dwProtocol);
1471 WLog_Print(log, g_LogLevel, " cbAtrLen: (0x%08" PRIX32 ")", ret->cbAtrLen);
1472 smartcard_dump_array(log, g_LogLevel, " rgAtr: ", "", ret->rgAtr, sizeof(ret->rgAtr));
1473 WLog_Print(log, g_LogLevel, "}");
1474 free(state);
1475}
1476
1477static void smartcard_trace_reconnect_return(wLog* log, const Reconnect_Return* ret)
1478{
1479 WINPR_ASSERT(ret);
1480
1481 if (!WLog_IsLevelActive(log, g_LogLevel))
1482 return;
1483
1484 WLog_Print(log, g_LogLevel, "Reconnect_Return {");
1485 WLog_Print(log, g_LogLevel, " ReturnCode: %s (0x%08" PRIX32 ")",
1486 SCardGetErrorString(ret->ReturnCode),
1487 WINPR_CXX_COMPAT_CAST(UINT32, ret->ReturnCode));
1488 WLog_Print(log, g_LogLevel, " dwActiveProtocol: %s (0x%08" PRIX32 ")",
1489 SCardGetProtocolString(ret->dwActiveProtocol), ret->dwActiveProtocol);
1490 WLog_Print(log, g_LogLevel, "}");
1491}
1492
1493static void smartcard_trace_connect_a_call(wLog* log, const ConnectA_Call* call)
1494{
1495 WINPR_ASSERT(call);
1496
1497 if (!WLog_IsLevelActive(log, g_LogLevel))
1498 return;
1499
1500 WLog_Print(log, g_LogLevel, "ConnectA_Call {");
1501 smartcard_log_context(log, &call->Common.handles.hContext);
1502
1503 WLog_Print(log, g_LogLevel,
1504 "szReader: %s dwShareMode: %s (0x%08" PRIX32
1505 ") dwPreferredProtocols: %s (0x%08" PRIX32 ")",
1506 call->szReader, SCardGetShareModeString(call->Common.dwShareMode),
1507 call->Common.dwShareMode, SCardGetProtocolString(call->Common.dwPreferredProtocols),
1508 call->Common.dwPreferredProtocols);
1509 WLog_Print(log, g_LogLevel, "}");
1510}
1511
1512static void smartcard_trace_connect_w_call(wLog* log, const ConnectW_Call* call)
1513{
1514 WINPR_ASSERT(call);
1515 char szReaderA[1024] = WINPR_C_ARRAY_INIT;
1516
1517 if (!WLog_IsLevelActive(log, g_LogLevel))
1518 return;
1519
1520 if (call->szReader)
1521 (void)ConvertWCharToUtf8(call->szReader, szReaderA, ARRAYSIZE(szReaderA));
1522 WLog_Print(log, g_LogLevel, "ConnectW_Call {");
1523 smartcard_log_context(log, &call->Common.handles.hContext);
1524
1525 WLog_Print(log, g_LogLevel,
1526 "szReader: %s dwShareMode: %s (0x%08" PRIX32
1527 ") dwPreferredProtocols: %s (0x%08" PRIX32 ")",
1528 szReaderA, SCardGetShareModeString(call->Common.dwShareMode),
1529 call->Common.dwShareMode, SCardGetProtocolString(call->Common.dwPreferredProtocols),
1530 call->Common.dwPreferredProtocols);
1531 WLog_Print(log, g_LogLevel, "}");
1532}
1533
1534static void smartcard_trace_hcard_and_disposition_call(wLog* log,
1535 const HCardAndDisposition_Call* call,
1536 const char* name)
1537{
1538 WINPR_ASSERT(call);
1539
1540 if (!WLog_IsLevelActive(log, g_LogLevel))
1541 return;
1542
1543 WLog_Print(log, g_LogLevel, "%s_Call {", name);
1544 smartcard_log_context(log, &call->handles.hContext);
1545 smartcard_log_redir_handle(log, &call->handles.hCard);
1546
1547 WLog_Print(log, g_LogLevel, "dwDisposition: %s (0x%08" PRIX32 ")",
1548 SCardGetDispositionString(call->dwDisposition), call->dwDisposition);
1549 WLog_Print(log, g_LogLevel, "}");
1550}
1551
1552static void smartcard_trace_establish_context_call(wLog* log, const EstablishContext_Call* call)
1553{
1554 WINPR_ASSERT(call);
1555
1556 if (!WLog_IsLevelActive(log, g_LogLevel))
1557 return;
1558
1559 WLog_Print(log, g_LogLevel, "EstablishContext_Call {");
1560 WLog_Print(log, g_LogLevel, "dwScope: %s (0x%08" PRIX32 ")", SCardGetScopeString(call->dwScope),
1561 call->dwScope);
1562 WLog_Print(log, g_LogLevel, "}");
1563}
1564
1565static void smartcard_trace_establish_context_return(wLog* log, const EstablishContext_Return* ret)
1566{
1567 WINPR_ASSERT(ret);
1568
1569 if (!WLog_IsLevelActive(log, g_LogLevel))
1570 return;
1571
1572 WLog_Print(log, g_LogLevel, "EstablishContext_Return {");
1573 WLog_Print(log, g_LogLevel, " ReturnCode: %s (0x%08" PRIX32 ")",
1574 SCardGetErrorString(ret->ReturnCode),
1575 WINPR_CXX_COMPAT_CAST(UINT32, ret->ReturnCode));
1576 smartcard_log_context(log, &ret->hContext);
1577
1578 WLog_Print(log, g_LogLevel, "}");
1579}
1580
1581void smartcard_trace_long_return(const Long_Return* ret, const char* name)
1582{
1583 wLog* log = scard_log();
1584 smartcard_trace_long_return_int(log, ret, name);
1585}
1586
1587void smartcard_trace_long_return_int(wLog* log, const Long_Return* ret, const char* name)
1588{
1589 WINPR_ASSERT(ret);
1590
1591 if (!WLog_IsLevelActive(log, g_LogLevel))
1592 return;
1593
1594 WLog_Print(log, g_LogLevel, "%s_Return {", name);
1595 WLog_Print(log, g_LogLevel, " ReturnCode: %s (0x%08" PRIX32 ")",
1596 SCardGetErrorString(ret->ReturnCode),
1597 WINPR_CXX_COMPAT_CAST(UINT32, ret->ReturnCode));
1598 WLog_Print(log, g_LogLevel, "}");
1599}
1600
1601static void smartcard_trace_connect_return(wLog* log, const Connect_Return* ret)
1602{
1603 WINPR_ASSERT(ret);
1604
1605 if (!WLog_IsLevelActive(log, g_LogLevel))
1606 return;
1607
1608 WLog_Print(log, g_LogLevel, "Connect_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 smartcard_log_context(log, &ret->hContext);
1613 smartcard_log_redir_handle(log, &ret->hCard);
1614
1615 WLog_Print(log, g_LogLevel, " dwActiveProtocol: %s (0x%08" PRIX32 ")",
1616 SCardGetProtocolString(ret->dwActiveProtocol), ret->dwActiveProtocol);
1617 WLog_Print(log, g_LogLevel, "}");
1618}
1619
1620static void smartcard_trace_reconnect_call(wLog* log, const Reconnect_Call* call)
1621{
1622 WINPR_ASSERT(call);
1623
1624 if (!WLog_IsLevelActive(log, g_LogLevel))
1625 return;
1626
1627 WLog_Print(log, g_LogLevel, "Reconnect_Call {");
1628 smartcard_log_context(log, &call->handles.hContext);
1629 smartcard_log_redir_handle(log, &call->handles.hCard);
1630
1631 WLog_Print(log, g_LogLevel,
1632 "dwShareMode: %s (0x%08" PRIX32 ") dwPreferredProtocols: %s (0x%08" PRIX32
1633 ") dwInitialization: %s (0x%08" PRIX32 ")",
1634 SCardGetShareModeString(call->dwShareMode), call->dwShareMode,
1635 SCardGetProtocolString(call->dwPreferredProtocols), call->dwPreferredProtocols,
1636 SCardGetDispositionString(call->dwInitialization), call->dwInitialization);
1637 WLog_Print(log, g_LogLevel, "}");
1638}
1639
1640static void smartcard_trace_device_type_id_return(wLog* log, const GetDeviceTypeId_Return* ret)
1641{
1642 WINPR_ASSERT(ret);
1643
1644 if (!WLog_IsLevelActive(log, g_LogLevel))
1645 return;
1646
1647 WLog_Print(log, g_LogLevel, "GetDeviceTypeId_Return {");
1648 WLog_Print(log, g_LogLevel, " ReturnCode: %s (0x%08" PRIX32 ")",
1649 SCardGetErrorString(ret->ReturnCode),
1650 WINPR_CXX_COMPAT_CAST(UINT32, ret->ReturnCode));
1651 WLog_Print(log, g_LogLevel, " dwDeviceId=%08" PRIx32, ret->dwDeviceId);
1652
1653 WLog_Print(log, g_LogLevel, "}");
1654}
1655
1656WINPR_ATTR_NODISCARD static LONG
1657smartcard_unpack_common_context_and_string_a(wLog* log, wStream* s, REDIR_SCARDCONTEXT* phContext,
1658 CHAR** pszReaderName)
1659{
1660 UINT32 index = 0;
1661 UINT32 pbContextNdrPtr = 0;
1662 LONG status = smartcard_unpack_redir_scard_context(log, s, phContext, &index, &pbContextNdrPtr);
1663 if (status != SCARD_S_SUCCESS)
1664 return status;
1665
1666 if (!smartcard_ndr_pointer_read(log, s, &index, nullptr))
1667 return ERROR_INVALID_DATA;
1668
1669 status = smartcard_unpack_redir_scard_context_ref(log, s, pbContextNdrPtr, phContext);
1670 if (status != SCARD_S_SUCCESS)
1671 return status;
1672
1673 status = smartcard_ndr_read_a(log, s, pszReaderName, NDR_PTR_FULL);
1674 if (status != SCARD_S_SUCCESS)
1675 return status;
1676
1677 smartcard_trace_context_and_string_call_a(log, __func__, phContext, *pszReaderName);
1678 return SCARD_S_SUCCESS;
1679}
1680
1681WINPR_ATTR_NODISCARD static LONG
1682smartcard_unpack_common_context_and_string_w(wLog* log, wStream* s, REDIR_SCARDCONTEXT* phContext,
1683 WCHAR** pszReaderName)
1684{
1685 UINT32 index = 0;
1686 UINT32 pbContextNdrPtr = 0;
1687
1688 LONG status = smartcard_unpack_redir_scard_context(log, s, phContext, &index, &pbContextNdrPtr);
1689 if (status != SCARD_S_SUCCESS)
1690 return status;
1691
1692 if (!smartcard_ndr_pointer_read(log, s, &index, nullptr))
1693 return ERROR_INVALID_DATA;
1694
1695 status = smartcard_unpack_redir_scard_context_ref(log, s, pbContextNdrPtr, phContext);
1696 if (status != SCARD_S_SUCCESS)
1697 return status;
1698
1699 status = smartcard_ndr_read_w(log, s, pszReaderName, NDR_PTR_FULL);
1700 if (status != SCARD_S_SUCCESS)
1701 return status;
1702
1703 smartcard_trace_context_and_string_call_w(log, __func__, phContext, *pszReaderName);
1704 return SCARD_S_SUCCESS;
1705}
1706
1707LONG smartcard_unpack_common_type_header(wStream* s)
1708{
1709 wLog* log = scard_log();
1710 UINT8 version = 0;
1711 UINT32 filler = 0;
1712 UINT8 endianness = 0;
1713 UINT16 commonHeaderLength = 0;
1714
1715 if (!Stream_CheckAndLogRequiredLengthWLog(log, s, 8))
1716 return STATUS_BUFFER_TOO_SMALL;
1717
1718 /* Process CommonTypeHeader */
1719 Stream_Read_UINT8(s, version); /* Version (1 byte) */
1720 Stream_Read_UINT8(s, endianness); /* Endianness (1 byte) */
1721 Stream_Read_UINT16(s, commonHeaderLength); /* CommonHeaderLength (2 bytes) */
1722 Stream_Read_UINT32(s, filler); /* Filler (4 bytes), should be 0xCCCCCCCC */
1723
1724 if (version != 1)
1725 {
1726 WLog_Print(log, WLOG_WARN, "Unsupported CommonTypeHeader Version %" PRIu8 "", version);
1727 return STATUS_INVALID_PARAMETER;
1728 }
1729
1730 if (endianness != 0x10)
1731 {
1732 WLog_Print(log, WLOG_WARN, "Unsupported CommonTypeHeader Endianness %" PRIu8 "",
1733 endianness);
1734 return STATUS_INVALID_PARAMETER;
1735 }
1736
1737 if (commonHeaderLength != 8)
1738 {
1739 WLog_Print(log, WLOG_WARN, "Unsupported CommonTypeHeader CommonHeaderLength %" PRIu16 "",
1740 commonHeaderLength);
1741 return STATUS_INVALID_PARAMETER;
1742 }
1743
1744 if (filler != 0xCCCCCCCC)
1745 {
1746 WLog_Print(log, WLOG_WARN, "Unexpected CommonTypeHeader Filler 0x%08" PRIX32 "", filler);
1747 return STATUS_INVALID_PARAMETER;
1748 }
1749
1750 return SCARD_S_SUCCESS;
1751}
1752
1753void smartcard_pack_common_type_header(wStream* s)
1754{
1755 Stream_Write_UINT8(s, 1); /* Version (1 byte) */
1756 Stream_Write_UINT8(s, 0x10); /* Endianness (1 byte) */
1757 Stream_Write_UINT16(s, 8); /* CommonHeaderLength (2 bytes) */
1758 Stream_Write_UINT32(s, 0xCCCCCCCC); /* Filler (4 bytes), should be 0xCCCCCCCC */
1759}
1760
1761LONG smartcard_unpack_private_type_header(wStream* s)
1762{
1763 wLog* log = scard_log();
1764 UINT32 filler = 0;
1765 UINT32 objectBufferLength = 0;
1766
1767 if (!Stream_CheckAndLogRequiredLengthWLog(log, s, 8))
1768 return STATUS_BUFFER_TOO_SMALL;
1769
1770 Stream_Read_UINT32(s, objectBufferLength); /* ObjectBufferLength (4 bytes) */
1771 Stream_Read_UINT32(s, filler); /* Filler (4 bytes), should be 0x00000000 */
1772
1773 if (filler != 0x00000000)
1774 {
1775 WLog_Print(log, WLOG_WARN, "Unexpected PrivateTypeHeader Filler 0x%08" PRIX32 "", filler);
1776 return STATUS_INVALID_PARAMETER;
1777 }
1778
1779 if (!Stream_CheckAndLogRequiredLengthWLog(log, s, objectBufferLength))
1780 return STATUS_INVALID_PARAMETER;
1781
1782 return SCARD_S_SUCCESS;
1783}
1784
1785void smartcard_pack_private_type_header(wStream* s, UINT32 objectBufferLength)
1786{
1787 Stream_Write_UINT32(s, objectBufferLength); /* ObjectBufferLength (4 bytes) */
1788 Stream_Write_UINT32(s, 0x00000000); /* Filler (4 bytes), should be 0x00000000 */
1789}
1790
1791LONG smartcard_unpack_read_size_align(wStream* s, size_t size, UINT32 alignment)
1792{
1793 const size_t padsize = (size + alignment - 1) & ~(alignment - 1);
1794 const size_t pad = padsize - size;
1795
1796 if (pad > 0)
1797 {
1798 if (!Stream_SafeSeek(s, pad))
1799 return -1;
1800 }
1801
1802 return (LONG)pad;
1803}
1804
1805LONG smartcard_pack_write_size_align(wStream* s, size_t size, UINT32 alignment)
1806{
1807 const size_t padsize = (size + alignment - 1) & ~(alignment - 1);
1808 const size_t pad = padsize - size;
1809
1810 if (pad)
1811 {
1812 if (!Stream_EnsureRemainingCapacity(s, pad))
1813 {
1814 wLog* log = scard_log();
1815 WLog_Print(log, WLOG_ERROR, "Stream_EnsureRemainingCapacity failed!");
1816 return SCARD_F_INTERNAL_ERROR;
1817 }
1818
1819 Stream_Zero(s, pad);
1820 }
1821
1822 return SCARD_S_SUCCESS;
1823}
1824
1825SCARDCONTEXT smartcard_scard_context_native_from_redir(const REDIR_SCARDCONTEXT* context)
1826{
1827 SCARDCONTEXT hContext = WINPR_C_ARRAY_INIT;
1828
1829 WINPR_ASSERT(context);
1830 if ((context->cbContext != sizeof(ULONG_PTR)) && (context->cbContext != 0))
1831 {
1832 wLog* log = scard_log();
1833 WLog_Print(log, WLOG_WARN,
1834 "REDIR_SCARDCONTEXT does not match native size: Actual: %" PRIu32
1835 ", Expected: %" PRIuz "",
1836 context->cbContext, sizeof(ULONG_PTR));
1837 return 0;
1838 }
1839
1840 if (context->cbContext)
1841 CopyMemory(&hContext, &(context->pbContext), context->cbContext);
1842
1843 return hContext;
1844}
1845
1846void smartcard_scard_context_native_to_redir(REDIR_SCARDCONTEXT* context, SCARDCONTEXT hContext)
1847{
1848 WINPR_ASSERT(context);
1849 ZeroMemory(context, sizeof(REDIR_SCARDCONTEXT));
1850 context->cbContext = sizeof(ULONG_PTR);
1851 CopyMemory(&(context->pbContext), &hContext, context->cbContext);
1852}
1853
1854SCARDHANDLE smartcard_scard_handle_native_from_redir(const REDIR_SCARDHANDLE* handle)
1855{
1856 SCARDHANDLE hCard = 0;
1857
1858 WINPR_ASSERT(handle);
1859 if (handle->cbHandle == 0)
1860 return hCard;
1861
1862 if (handle->cbHandle != sizeof(ULONG_PTR))
1863 {
1864 wLog* log = scard_log();
1865 WLog_Print(log, WLOG_WARN,
1866 "REDIR_SCARDHANDLE does not match native size: Actual: %" PRIu32
1867 ", Expected: %" PRIuz "",
1868 handle->cbHandle, sizeof(ULONG_PTR));
1869 return 0;
1870 }
1871
1872 if (handle->cbHandle)
1873 CopyMemory(&hCard, &(handle->pbHandle), handle->cbHandle);
1874
1875 return hCard;
1876}
1877
1878void smartcard_scard_handle_native_to_redir(REDIR_SCARDHANDLE* handle, SCARDHANDLE hCard)
1879{
1880 WINPR_ASSERT(handle);
1881 ZeroMemory(handle, sizeof(REDIR_SCARDHANDLE));
1882 handle->cbHandle = sizeof(ULONG_PTR);
1883 CopyMemory(&(handle->pbHandle), &hCard, handle->cbHandle);
1884}
1885
1886#define smartcard_context_supported(log, size) \
1887 smartcard_context_supported_((log), (size), __FILE__, __func__, __LINE__)
1888WINPR_ATTR_NODISCARD static LONG smartcard_context_supported_(wLog* log, uint32_t size,
1889 const char* file, const char* fkt,
1890 size_t line)
1891{
1892 switch (size)
1893 {
1894 case 0:
1895 case 4:
1896 case 8:
1897 return SCARD_S_SUCCESS;
1898 default:
1899 {
1900 const uint32_t level = WLOG_WARN;
1901 if (WLog_IsLevelActive(log, level))
1902 {
1903 WLog_PrintTextMessage(log, level, line, file, fkt,
1904 "REDIR_SCARDCONTEXT length is not 0, 4 or 8: %" PRIu32 "",
1905 size);
1906 }
1907 return STATUS_INVALID_PARAMETER;
1908 }
1909 }
1910}
1911
1912LONG smartcard_unpack_redir_scard_context_(wLog* log, wStream* s, REDIR_SCARDCONTEXT* context,
1913 UINT32* index, UINT32* ppbContextNdrPtr,
1914 const char* file, const char* function, size_t line)
1915{
1916 UINT32 pbContextNdrPtr = 0;
1917
1918 WINPR_UNUSED(file);
1919 WINPR_ASSERT(context);
1920
1921 ZeroMemory(context, sizeof(REDIR_SCARDCONTEXT));
1922
1923 if (!Stream_CheckAndLogRequiredLengthWLogEx(log, WLOG_WARN, s, 4, 1, "%s(%s:%" PRIuz ")", file,
1924 function, line))
1925 return STATUS_BUFFER_TOO_SMALL;
1926
1927 const LONG status = smartcard_context_supported_(log, context->cbContext, file, function, line);
1928 if (status != SCARD_S_SUCCESS)
1929 return status;
1930
1931 Stream_Read_UINT32(s, context->cbContext); /* cbContext (4 bytes) */
1932 if (context->cbContext > 16)
1933 {
1934 WLog_Print(log, WLOG_WARN, "REDIR_SCARDCONTEXT cbContext (%" PRIu32 ") > 16",
1935 context->cbContext);
1936 return ERROR_INVALID_DATA;
1937 }
1938
1939 if (!smartcard_ndr_pointer_read_(log, s, index, &pbContextNdrPtr, file, function, line))
1940 return ERROR_INVALID_DATA;
1941
1942 if (((context->cbContext == 0) && pbContextNdrPtr) ||
1943 ((context->cbContext != 0) && !pbContextNdrPtr))
1944 {
1945 WLog_Print(log, WLOG_WARN,
1946 "REDIR_SCARDCONTEXT cbContext (%" PRIu32 ") pbContextNdrPtr (%" PRIu32
1947 ") inconsistency",
1948 context->cbContext, pbContextNdrPtr);
1949 return STATUS_INVALID_PARAMETER;
1950 }
1951
1952 *ppbContextNdrPtr = pbContextNdrPtr;
1953 return SCARD_S_SUCCESS;
1954}
1955
1956LONG smartcard_pack_redir_scard_context(WINPR_ATTR_UNUSED wLog* log, wStream* s,
1957 const REDIR_SCARDCONTEXT* context, UINT32* index)
1958{
1959 const UINT32 pbContextNdrPtr = 0x00020000 + *index * 4;
1960
1961 WINPR_ASSERT(context);
1962 if (context->cbContext != 0)
1963 {
1964 Stream_Write_UINT32(s, context->cbContext); /* cbContext (4 bytes) */
1965 Stream_Write_UINT32(s, pbContextNdrPtr); /* pbContextNdrPtr (4 bytes) */
1966 *index = *index + 1;
1967 }
1968 else
1969 Stream_Zero(s, 8);
1970
1971 return SCARD_S_SUCCESS;
1972}
1973
1974LONG smartcard_unpack_redir_scard_context_ref(wLog* log, wStream* s,
1975 WINPR_ATTR_UNUSED UINT32 pbContextNdrPtr,
1976 REDIR_SCARDCONTEXT* context)
1977{
1978 UINT32 length = 0;
1979
1980 WINPR_ASSERT(context);
1981
1982 if (context->cbContext == 0)
1983 return SCARD_S_SUCCESS;
1984
1985 if (!Stream_CheckAndLogRequiredLengthWLog(log, s, 4))
1986 return STATUS_BUFFER_TOO_SMALL;
1987
1988 Stream_Read_UINT32(s, length); /* Length (4 bytes) */
1989
1990 if (length != context->cbContext)
1991 {
1992 WLog_Print(log, WLOG_WARN,
1993 "REDIR_SCARDCONTEXT length (%" PRIu32 ") cbContext (%" PRIu32 ") mismatch",
1994 length, context->cbContext);
1995 return STATUS_INVALID_PARAMETER;
1996 }
1997
1998 const LONG status = smartcard_context_supported(log, context->cbContext);
1999 if (status != SCARD_S_SUCCESS)
2000 return status;
2001
2002 if (!Stream_CheckAndLogRequiredLengthWLog(log, s, context->cbContext))
2003 return STATUS_BUFFER_TOO_SMALL;
2004
2005 if (context->cbContext)
2006 Stream_Read(s, &(context->pbContext), context->cbContext);
2007 else
2008 ZeroMemory(&(context->pbContext), sizeof(context->pbContext));
2009
2010 return SCARD_S_SUCCESS;
2011}
2012
2013LONG smartcard_pack_redir_scard_context_ref(WINPR_ATTR_UNUSED wLog* log, wStream* s,
2014 const REDIR_SCARDCONTEXT* context)
2015{
2016 WINPR_ASSERT(context);
2017
2018 if (context->cbContext == 0)
2019 return SCARD_S_SUCCESS;
2020
2021 Stream_Write_UINT32(s, context->cbContext); /* Length (4 bytes) */
2022 Stream_Write(s, &(context->pbContext), context->cbContext);
2023
2024 return SCARD_S_SUCCESS;
2025}
2026
2027LONG smartcard_unpack_redir_scard_handle_(wLog* log, wStream* s, REDIR_SCARDHANDLE* handle,
2028 UINT32* index, const char* file, const char* function,
2029 size_t line)
2030{
2031 UINT32 pbHandleNdrPtr = 0;
2032
2033 WINPR_ASSERT(handle);
2034 ZeroMemory(handle, sizeof(REDIR_SCARDHANDLE));
2035
2036 if (!Stream_CheckAndLogRequiredLengthWLog(log, s, 4))
2037 return STATUS_BUFFER_TOO_SMALL;
2038
2039 Stream_Read_UINT32(s, handle->cbHandle); /* Length (4 bytes) */
2040
2041 if (!smartcard_ndr_pointer_read_(log, s, index, &pbHandleNdrPtr, file, function, line))
2042 return ERROR_INVALID_DATA;
2043
2044 if (((handle->cbHandle == 0) && pbHandleNdrPtr) ||
2045 ((handle->cbHandle != 0) && !pbHandleNdrPtr))
2046 {
2047 WLog_Print(log, WLOG_WARN,
2048 "REDIR_SCARDHANDLE cbHandle (%" PRIu32 ") pbHandleNdrPtr (%" PRIu32
2049 ") inconsistency",
2050 handle->cbHandle, pbHandleNdrPtr);
2051 return STATUS_INVALID_PARAMETER;
2052 }
2053
2054 return SCARD_S_SUCCESS;
2055}
2056
2057LONG smartcard_pack_redir_scard_handle(WINPR_ATTR_UNUSED wLog* log, wStream* s,
2058 const REDIR_SCARDHANDLE* handle, UINT32* index)
2059{
2060 const UINT32 pbContextNdrPtr = 0x00020000 + *index * 4;
2061
2062 WINPR_ASSERT(handle);
2063 if (handle->cbHandle != 0)
2064 {
2065 Stream_Write_UINT32(s, handle->cbHandle); /* cbContext (4 bytes) */
2066 Stream_Write_UINT32(s, pbContextNdrPtr); /* pbContextNdrPtr (4 bytes) */
2067 *index = *index + 1;
2068 }
2069 else
2070 Stream_Zero(s, 8);
2071 return SCARD_S_SUCCESS;
2072}
2073
2074LONG smartcard_unpack_redir_scard_handle_ref(wLog* log, wStream* s, REDIR_SCARDHANDLE* handle)
2075{
2076 UINT32 length = 0;
2077
2078 WINPR_ASSERT(handle);
2079
2080 if (handle->cbHandle == 0)
2081 return SCARD_S_SUCCESS;
2082
2083 if (!Stream_CheckAndLogRequiredLengthWLog(log, s, 4))
2084 return STATUS_BUFFER_TOO_SMALL;
2085
2086 Stream_Read_UINT32(s, length); /* Length (4 bytes) */
2087
2088 if (length != handle->cbHandle)
2089 {
2090 WLog_Print(log, WLOG_WARN,
2091 "REDIR_SCARDHANDLE length (%" PRIu32 ") cbHandle (%" PRIu32 ") mismatch", length,
2092 handle->cbHandle);
2093 return STATUS_INVALID_PARAMETER;
2094 }
2095
2096 if ((handle->cbHandle != 4) && (handle->cbHandle != 8))
2097 {
2098 WLog_Print(log, WLOG_WARN, "REDIR_SCARDHANDLE length is not 4 or 8: %" PRIu32 "",
2099 handle->cbHandle);
2100 return STATUS_INVALID_PARAMETER;
2101 }
2102
2103 if (!Stream_CheckAndLogRequiredLengthWLog(log, s, handle->cbHandle))
2104 return STATUS_BUFFER_TOO_SMALL;
2105
2106 if (handle->cbHandle)
2107 Stream_Read(s, &(handle->pbHandle), handle->cbHandle);
2108
2109 return SCARD_S_SUCCESS;
2110}
2111
2112LONG smartcard_pack_redir_scard_handle_ref(WINPR_ATTR_UNUSED wLog* log, wStream* s,
2113 const REDIR_SCARDHANDLE* handle)
2114{
2115 WINPR_ASSERT(handle);
2116
2117 if (handle->cbHandle == 0)
2118 return SCARD_S_SUCCESS;
2119
2120 Stream_Write_UINT32(s, handle->cbHandle); /* Length (4 bytes) */
2121 Stream_Write(s, &(handle->pbHandle), handle->cbHandle);
2122
2123 return SCARD_S_SUCCESS;
2124}
2125
2126LONG smartcard_unpack_establish_context_call(wStream* s, EstablishContext_Call* call)
2127{
2128 WINPR_ASSERT(call);
2129 wLog* log = scard_log();
2130
2131 if (!Stream_CheckAndLogRequiredLengthWLog(log, s, 4))
2132 return STATUS_BUFFER_TOO_SMALL;
2133
2134 Stream_Read_UINT32(s, call->dwScope); /* dwScope (4 bytes) */
2135 smartcard_trace_establish_context_call(log, call);
2136 return SCARD_S_SUCCESS;
2137}
2138
2139LONG smartcard_pack_establish_context_call(wStream* s, const EstablishContext_Call* call)
2140{
2141 WINPR_ASSERT(call);
2142 wLog* log = scard_log();
2143
2144 smartcard_trace_establish_context_call(log, call);
2145
2146 if (!Stream_EnsureRemainingCapacity(s, 4))
2147 return SCARD_E_NO_MEMORY;
2148
2149 Stream_Write_UINT32(s, call->dwScope);
2150
2151 return SCARD_S_SUCCESS;
2152}
2153
2154LONG smartcard_pack_establish_context_return(wStream* s, const EstablishContext_Return* ret)
2155{
2156 WINPR_ASSERT(ret);
2157 wLog* log = scard_log();
2158 LONG status = 0;
2159 UINT32 index = 0;
2160
2161 smartcard_trace_establish_context_return(log, ret);
2162 if (ret->ReturnCode != SCARD_S_SUCCESS)
2163 return ret->ReturnCode;
2164
2165 status = smartcard_pack_redir_scard_context(log, s, &(ret->hContext), &index);
2166 if (status != SCARD_S_SUCCESS)
2167 return status;
2168
2169 return smartcard_pack_redir_scard_context_ref(log, s, &(ret->hContext));
2170}
2171
2172LONG smartcard_unpack_establish_context_return(wStream* s, EstablishContext_Return* ret)
2173{
2174 WINPR_ASSERT(ret);
2175 wLog* log = scard_log();
2176 UINT32 index = 0;
2177 UINT32 pbContextNdrPtr = 0;
2178
2179 LONG status =
2180 smartcard_unpack_redir_scard_context(log, s, &(ret->hContext), &index, &pbContextNdrPtr);
2181 if (status != SCARD_S_SUCCESS)
2182 return status;
2183
2184 status = smartcard_unpack_redir_scard_context_ref(log, s, pbContextNdrPtr, &(ret->hContext));
2185 if (status != SCARD_S_SUCCESS)
2186 return status;
2187
2188 smartcard_trace_establish_context_return(log, ret);
2189 return SCARD_S_SUCCESS;
2190}
2191
2192LONG smartcard_unpack_context_call(wStream* s, Context_Call* call, const char* name)
2193{
2194 UINT32 index = 0;
2195 UINT32 pbContextNdrPtr = 0;
2196 wLog* log = scard_log();
2197
2198 WINPR_ASSERT(call);
2199 LONG status = smartcard_unpack_redir_scard_context(log, s, &(call->handles.hContext), &index,
2200 &pbContextNdrPtr);
2201 if (status != SCARD_S_SUCCESS)
2202 return status;
2203
2204 status = smartcard_unpack_redir_scard_context_ref(log, s, pbContextNdrPtr,
2205 &(call->handles.hContext));
2206 if (status != SCARD_S_SUCCESS)
2207 WLog_Print(log, WLOG_ERROR,
2208 "smartcard_unpack_redir_scard_context_ref failed with error %" PRId32 "",
2209 status);
2210
2211 smartcard_trace_context_call(log, call, name);
2212 return status;
2213}
2214
2215LONG smartcard_unpack_list_reader_groups_call(wStream* s, ListReaderGroups_Call* call, BOOL unicode)
2216{
2217 UINT32 index = 0;
2218 UINT32 pbContextNdrPtr = 0;
2219 wLog* log = scard_log();
2220
2221 WINPR_ASSERT(call);
2222 LONG status = smartcard_unpack_redir_scard_context(log, s, &(call->handles.hContext), &index,
2223 &pbContextNdrPtr);
2224
2225 if (status != SCARD_S_SUCCESS)
2226 return status;
2227
2228 if (!Stream_CheckAndLogRequiredLengthWLog(log, s, 8))
2229 return STATUS_BUFFER_TOO_SMALL;
2230
2231 Stream_Read_INT32(s, call->fmszGroupsIsNULL); /* fmszGroupsIsNULL (4 bytes) */
2232 Stream_Read_UINT32(s, call->cchGroups); /* cchGroups (4 bytes) */
2233 status = smartcard_unpack_redir_scard_context_ref(log, s, pbContextNdrPtr,
2234 &(call->handles.hContext));
2235
2236 if (status != SCARD_S_SUCCESS)
2237 return status;
2238
2239 smartcard_trace_list_reader_groups_call(log, call, unicode);
2240 return SCARD_S_SUCCESS;
2241}
2242
2243LONG smartcard_pack_list_reader_groups_call(wStream* s, const ListReaderGroups_Call* call,
2244 BOOL unicode)
2245{
2246 WINPR_ASSERT(call);
2247 wLog* log = scard_log();
2248 UINT32 index = 0;
2249
2250 smartcard_trace_list_reader_groups_call(log, call, unicode);
2251
2252 LONG status = smartcard_pack_redir_scard_context(log, s, &call->handles.hContext, &index);
2253 if (status != SCARD_S_SUCCESS)
2254 return status;
2255
2256 if (!Stream_EnsureRemainingCapacity(s, 8))
2257 return SCARD_E_NO_MEMORY;
2258
2259 Stream_Write_INT32(s, call->fmszGroupsIsNULL);
2260 Stream_Write_UINT32(s, call->cchGroups);
2261
2262 return smartcard_pack_redir_scard_context_ref(log, s, &call->handles.hContext);
2263}
2264
2265LONG smartcard_pack_list_reader_groups_return(wStream* s, const ListReaderGroups_Return* ret,
2266 BOOL unicode)
2267{
2268 WINPR_ASSERT(ret);
2269 wLog* log = scard_log();
2270 LONG status = 0;
2271 UINT32 cBytes = ret->cBytes;
2272 UINT32 index = 0;
2273
2274 smartcard_trace_list_reader_groups_return(log, ret, unicode);
2275 if (ret->ReturnCode != SCARD_S_SUCCESS)
2276 cBytes = 0;
2277 if (cBytes == SCARD_AUTOALLOCATE)
2278 cBytes = 0;
2279
2280 if (!Stream_EnsureRemainingCapacity(s, 4))
2281 return SCARD_E_NO_MEMORY;
2282
2283 if (cBytes > 65536)
2284 {
2285 WLog_Print(log, WLOG_ERROR,
2286 "2.2.3.4 ListReaderGroups_Return and ListReaders_Return cBytes (%" PRIu32
2287 ") > 65536",
2288 cBytes);
2289 return SCARD_E_INVALID_PARAMETER;
2290 }
2291
2292 Stream_Write_UINT32(s, cBytes); /* cBytes (4 bytes) */
2293 if (!smartcard_ndr_pointer_write(s, &index, cBytes))
2294 return SCARD_E_NO_MEMORY;
2295
2296 status = smartcard_ndr_write(s, ret->msz, cBytes, 1, NDR_PTR_SIMPLE);
2297 if (status != SCARD_S_SUCCESS)
2298 return status;
2299 return ret->ReturnCode;
2300}
2301
2302LONG smartcard_unpack_list_reader_groups_return(wStream* s, ListReaderGroups_Return* ret,
2303 BOOL unicode)
2304{
2305 WINPR_ASSERT(ret);
2306 wLog* log = scard_log();
2307 UINT32 index = 0;
2308 UINT32 mszNdrPtr = 0;
2309
2310 if (!Stream_CheckAndLogRequiredLengthWLog(log, s, 4))
2311 return STATUS_BUFFER_TOO_SMALL;
2312
2313 const UINT32 cBytes = Stream_Get_UINT32(s);
2314 if (cBytes > 65536)
2315 {
2316 WLog_Print(log, WLOG_ERROR,
2317 "2.2.3.4 ListReaderGroups_Return and ListReaders_Return cBytes (%" PRIu32
2318 ") > 65536",
2319 cBytes);
2320 return SCARD_E_INVALID_PARAMETER;
2321 }
2322
2323 if (!smartcard_ndr_pointer_read(log, s, &index, &mszNdrPtr))
2324 return ERROR_INVALID_DATA;
2325
2326 if (mszNdrPtr)
2327 {
2328 LONG status = smartcard_ndr_read(log, s, &ret->msz, cBytes, 1, NDR_PTR_SIMPLE);
2329 if (status != SCARD_S_SUCCESS)
2330 return status;
2331 ret->cBytes = cBytes;
2332 }
2333
2334 smartcard_trace_list_reader_groups_return(log, ret, unicode);
2335 return SCARD_S_SUCCESS;
2336}
2337
2338LONG smartcard_unpack_list_readers_call(wStream* s, ListReaders_Call* call, BOOL unicode)
2339{
2340 UINT32 index = 0;
2341 UINT32 mszGroupsNdrPtr = 0;
2342 UINT32 pbContextNdrPtr = 0;
2343 wLog* log = scard_log();
2344
2345 WINPR_ASSERT(call);
2346 call->mszGroups = nullptr;
2347
2348 LONG status = smartcard_unpack_redir_scard_context(log, s, &(call->handles.hContext), &index,
2349 &pbContextNdrPtr);
2350 if (status != SCARD_S_SUCCESS)
2351 return status;
2352
2353 if (!Stream_CheckAndLogRequiredLengthWLog(log, s, 4))
2354 return STATUS_BUFFER_TOO_SMALL;
2355
2356 const UINT32 cBytes = Stream_Get_UINT32(s);
2357 if (cBytes > 65536)
2358 {
2359 WLog_Print(log, WLOG_ERROR,
2360 "2.2.3.4 ListReaderGroups_Return and ListReaders_Return cBytes (%" PRIu32
2361 ") > 65536",
2362 cBytes);
2363 return SCARD_E_INVALID_PARAMETER;
2364 }
2365 if (!smartcard_ndr_pointer_read(log, s, &index, &mszGroupsNdrPtr))
2366 return ERROR_INVALID_DATA;
2367
2368 if (!Stream_CheckAndLogRequiredLengthWLog(log, s, 8))
2369 return STATUS_BUFFER_TOO_SMALL;
2370 Stream_Read_INT32(s, call->fmszReadersIsNULL); /* fmszReadersIsNULL (4 bytes) */
2371 Stream_Read_UINT32(s, call->cchReaders); /* cchReaders (4 bytes) */
2372
2373 status = smartcard_unpack_redir_scard_context_ref(log, s, pbContextNdrPtr,
2374 &(call->handles.hContext));
2375 if (status != SCARD_S_SUCCESS)
2376 return status;
2377
2378 if (mszGroupsNdrPtr)
2379 {
2380 status = smartcard_ndr_read(log, s, &call->mszGroups, cBytes, 1, NDR_PTR_SIMPLE);
2381 if (status != SCARD_S_SUCCESS)
2382 return status;
2383 call->cBytes = cBytes;
2384 }
2385
2386 smartcard_trace_list_readers_call(log, call, unicode);
2387 return SCARD_S_SUCCESS;
2388}
2389
2390LONG smartcard_pack_list_readers_return(wStream* s, const ListReaders_Return* ret, BOOL unicode)
2391{
2392 WINPR_ASSERT(ret);
2393 wLog* log = scard_log();
2394 LONG status = 0;
2395 UINT32 index = 0;
2396 UINT32 size = ret->cBytes;
2397
2398 smartcard_trace_list_readers_return(log, ret, unicode);
2399 if (ret->ReturnCode != SCARD_S_SUCCESS)
2400 size = 0;
2401
2402 if (!Stream_EnsureRemainingCapacity(s, 4))
2403 {
2404 WLog_Print(log, WLOG_ERROR, "Stream_EnsureRemainingCapacity failed!");
2405 return SCARD_F_INTERNAL_ERROR;
2406 }
2407
2408 if (size > 65536)
2409 {
2410 WLog_Print(log, WLOG_ERROR,
2411 "2.2.3.4 ListReaderGroups_Return and ListReaders_Return cBytes (%" PRIu32
2412 ") > 65536",
2413 size);
2414 return SCARD_E_INVALID_PARAMETER;
2415 }
2416
2417 Stream_Write_UINT32(s, size); /* cBytes (4 bytes) */
2418 if (!smartcard_ndr_pointer_write(s, &index, size))
2419 return SCARD_E_NO_MEMORY;
2420
2421 status = smartcard_ndr_write(s, ret->msz, size, 1, NDR_PTR_SIMPLE);
2422 if (status != SCARD_S_SUCCESS)
2423 return status;
2424 return ret->ReturnCode;
2425}
2426
2427WINPR_ATTR_NODISCARD static LONG smartcard_unpack_connect_common(wLog* log, wStream* s,
2428 Connect_Common_Call* common,
2429 UINT32* index,
2430 UINT32* ppbContextNdrPtr)
2431{
2432 WINPR_ASSERT(common);
2433 LONG status = smartcard_unpack_redir_scard_context(log, s, &(common->handles.hContext), index,
2434 ppbContextNdrPtr);
2435 if (status != SCARD_S_SUCCESS)
2436 return status;
2437
2438 if (!Stream_CheckAndLogRequiredLengthWLog(log, s, 8))
2439 return STATUS_BUFFER_TOO_SMALL;
2440
2441 Stream_Read_UINT32(s, common->dwShareMode); /* dwShareMode (4 bytes) */
2442 Stream_Read_UINT32(s, common->dwPreferredProtocols); /* dwPreferredProtocols (4 bytes) */
2443 return SCARD_S_SUCCESS;
2444}
2445
2446LONG smartcard_unpack_connect_a_call(wStream* s, ConnectA_Call* call)
2447{
2448 LONG status = 0;
2449 UINT32 index = 0;
2450 UINT32 pbContextNdrPtr = 0;
2451
2452 WINPR_ASSERT(call);
2453 wLog* log = scard_log();
2454
2455 call->szReader = nullptr;
2456
2457 if (!smartcard_ndr_pointer_read(log, s, &index, nullptr))
2458 return ERROR_INVALID_DATA;
2459
2460 status = smartcard_unpack_connect_common(log, s, &(call->Common), &index, &pbContextNdrPtr);
2461 if (status != SCARD_S_SUCCESS)
2462 {
2463 WLog_Print(log, WLOG_ERROR, "smartcard_unpack_connect_common failed with error %" PRId32 "",
2464 status);
2465 return status;
2466 }
2467
2468 status = smartcard_ndr_read_a(log, s, &call->szReader, NDR_PTR_FULL);
2469 if (status != SCARD_S_SUCCESS)
2470 return status;
2471
2472 status = smartcard_unpack_redir_scard_context_ref(log, s, pbContextNdrPtr,
2473 &(call->Common.handles.hContext));
2474 if (status != SCARD_S_SUCCESS)
2475 WLog_Print(log, WLOG_ERROR,
2476 "smartcard_unpack_redir_scard_context_ref failed with error %" PRId32 "",
2477 status);
2478
2479 smartcard_trace_connect_a_call(log, call);
2480 return status;
2481}
2482
2483LONG smartcard_unpack_connect_w_call(wStream* s, ConnectW_Call* call)
2484{
2485 LONG status = 0;
2486 UINT32 index = 0;
2487 UINT32 pbContextNdrPtr = 0;
2488
2489 WINPR_ASSERT(call);
2490 wLog* log = scard_log();
2491 call->szReader = nullptr;
2492
2493 if (!smartcard_ndr_pointer_read(log, s, &index, nullptr))
2494 return ERROR_INVALID_DATA;
2495
2496 status = smartcard_unpack_connect_common(log, s, &(call->Common), &index, &pbContextNdrPtr);
2497 if (status != SCARD_S_SUCCESS)
2498 {
2499 WLog_Print(log, WLOG_ERROR, "smartcard_unpack_connect_common failed with error %" PRId32 "",
2500 status);
2501 return status;
2502 }
2503
2504 status = smartcard_ndr_read_w(log, s, &call->szReader, NDR_PTR_FULL);
2505 if (status != SCARD_S_SUCCESS)
2506 return status;
2507
2508 status = smartcard_unpack_redir_scard_context_ref(log, s, pbContextNdrPtr,
2509 &(call->Common.handles.hContext));
2510 if (status != SCARD_S_SUCCESS)
2511 WLog_Print(log, WLOG_ERROR,
2512 "smartcard_unpack_redir_scard_context_ref failed with error %" PRId32 "",
2513 status);
2514
2515 smartcard_trace_connect_w_call(log, call);
2516 return status;
2517}
2518
2519LONG smartcard_pack_connect_return(wStream* s, const Connect_Return* ret)
2520{
2521 LONG status = 0;
2522 UINT32 index = 0;
2523
2524 WINPR_ASSERT(ret);
2525 wLog* log = scard_log();
2526 smartcard_trace_connect_return(log, ret);
2527
2528 status = smartcard_pack_redir_scard_context(log, s, &ret->hContext, &index);
2529 if (status != SCARD_S_SUCCESS)
2530 return status;
2531
2532 status = smartcard_pack_redir_scard_handle(log, s, &ret->hCard, &index);
2533 if (status != SCARD_S_SUCCESS)
2534 return status;
2535
2536 if (!Stream_EnsureRemainingCapacity(s, 4))
2537 return SCARD_E_NO_MEMORY;
2538
2539 Stream_Write_UINT32(s, ret->dwActiveProtocol); /* dwActiveProtocol (4 bytes) */
2540 status = smartcard_pack_redir_scard_context_ref(log, s, &ret->hContext);
2541 if (status != SCARD_S_SUCCESS)
2542 return status;
2543 return smartcard_pack_redir_scard_handle_ref(log, s, &(ret->hCard));
2544}
2545
2546LONG smartcard_unpack_reconnect_call(wStream* s, Reconnect_Call* call)
2547{
2548 UINT32 index = 0;
2549 UINT32 pbContextNdrPtr = 0;
2550
2551 WINPR_ASSERT(call);
2552 wLog* log = scard_log();
2553 LONG status = smartcard_unpack_redir_scard_context(log, s, &(call->handles.hContext), &index,
2554 &pbContextNdrPtr);
2555 if (status != SCARD_S_SUCCESS)
2556 return status;
2557
2558 status = smartcard_unpack_redir_scard_handle(log, s, &(call->handles.hCard), &index);
2559 if (status != SCARD_S_SUCCESS)
2560 return status;
2561
2562 if (!Stream_CheckAndLogRequiredLengthWLog(log, s, 12))
2563 return STATUS_BUFFER_TOO_SMALL;
2564
2565 Stream_Read_UINT32(s, call->dwShareMode); /* dwShareMode (4 bytes) */
2566 Stream_Read_UINT32(s, call->dwPreferredProtocols); /* dwPreferredProtocols (4 bytes) */
2567 Stream_Read_UINT32(s, call->dwInitialization); /* dwInitialization (4 bytes) */
2568
2569 status = smartcard_unpack_redir_scard_context_ref(log, s, pbContextNdrPtr,
2570 &(call->handles.hContext));
2571 if (status != SCARD_S_SUCCESS)
2572 {
2573 WLog_Print(log, WLOG_ERROR,
2574 "smartcard_unpack_redir_scard_context_ref failed with error %" PRId32 "",
2575 status);
2576 return status;
2577 }
2578
2579 status = smartcard_unpack_redir_scard_handle_ref(log, s, &(call->handles.hCard));
2580 if (status != SCARD_S_SUCCESS)
2581 WLog_Print(log, WLOG_ERROR,
2582 "smartcard_unpack_redir_scard_handle_ref failed with error %" PRId32 "", status);
2583
2584 smartcard_trace_reconnect_call(log, call);
2585 return status;
2586}
2587
2588LONG smartcard_pack_reconnect_call(wStream* s, const Reconnect_Call* call)
2589{
2590 WINPR_ASSERT(call);
2591 wLog* log = scard_log();
2592 UINT32 index = 0;
2593
2594 smartcard_trace_reconnect_call(log, call);
2595
2596 LONG status = smartcard_pack_redir_scard_context(log, s, &call->handles.hContext, &index);
2597 if (status != SCARD_S_SUCCESS)
2598 return status;
2599
2600 status = smartcard_pack_redir_scard_handle(log, s, &call->handles.hCard, &index);
2601 if (status != SCARD_S_SUCCESS)
2602 return status;
2603
2604 if (!Stream_EnsureRemainingCapacity(s, 12))
2605 return SCARD_E_NO_MEMORY;
2606
2607 Stream_Write_UINT32(s, call->dwShareMode);
2608 Stream_Write_UINT32(s, call->dwPreferredProtocols);
2609 Stream_Write_UINT32(s, call->dwInitialization);
2610
2611 status = smartcard_pack_redir_scard_context_ref(log, s, &call->handles.hContext);
2612 if (status != SCARD_S_SUCCESS)
2613 return status;
2614
2615 return smartcard_pack_redir_scard_handle_ref(log, s, &call->handles.hCard);
2616}
2617
2618LONG smartcard_pack_reconnect_return(wStream* s, const Reconnect_Return* ret)
2619{
2620 WINPR_ASSERT(ret);
2621 wLog* log = scard_log();
2622 smartcard_trace_reconnect_return(log, ret);
2623
2624 if (!Stream_EnsureRemainingCapacity(s, 4))
2625 return SCARD_E_NO_MEMORY;
2626 Stream_Write_UINT32(s, ret->dwActiveProtocol); /* dwActiveProtocol (4 bytes) */
2627 return ret->ReturnCode;
2628}
2629
2630LONG smartcard_unpack_reconnect_return(wStream* s, Reconnect_Return* ret)
2631{
2632 WINPR_ASSERT(ret);
2633 wLog* log = scard_log();
2634
2635 if (!Stream_CheckAndLogRequiredLengthWLog(log, s, 4))
2636 return STATUS_BUFFER_TOO_SMALL;
2637
2638 Stream_Read_UINT32(s, ret->dwActiveProtocol);
2639
2640 smartcard_trace_reconnect_return(log, ret);
2641 return SCARD_S_SUCCESS;
2642}
2643
2644LONG smartcard_unpack_hcard_and_disposition_call(wStream* s, HCardAndDisposition_Call* call,
2645 const char* name)
2646{
2647 UINT32 index = 0;
2648 UINT32 pbContextNdrPtr = 0;
2649
2650 WINPR_ASSERT(call);
2651 wLog* log = scard_log();
2652
2653 LONG status = smartcard_unpack_redir_scard_context(log, s, &(call->handles.hContext), &index,
2654 &pbContextNdrPtr);
2655 if (status != SCARD_S_SUCCESS)
2656 return status;
2657
2658 status = smartcard_unpack_redir_scard_handle(log, s, &(call->handles.hCard), &index);
2659 if (status != SCARD_S_SUCCESS)
2660 return status;
2661
2662 if (!Stream_CheckAndLogRequiredLengthWLog(log, s, 4))
2663 return STATUS_BUFFER_TOO_SMALL;
2664
2665 Stream_Read_UINT32(s, call->dwDisposition); /* dwDisposition (4 bytes) */
2666
2667 status = smartcard_unpack_redir_scard_context_ref(log, s, pbContextNdrPtr,
2668 &(call->handles.hContext));
2669 if (status != SCARD_S_SUCCESS)
2670 return status;
2671
2672 status = smartcard_unpack_redir_scard_handle_ref(log, s, &(call->handles.hCard));
2673 if (status != SCARD_S_SUCCESS)
2674 return status;
2675
2676 smartcard_trace_hcard_and_disposition_call(log, call, name);
2677 return status;
2678}
2679
2680static void smartcard_trace_get_status_change_a_call(wLog* log, const GetStatusChangeA_Call* call)
2681{
2682 WINPR_ASSERT(call);
2683
2684 if (!WLog_IsLevelActive(log, g_LogLevel))
2685 return;
2686
2687 WLog_Print(log, g_LogLevel, "GetStatusChangeA_Call {");
2688 smartcard_log_context(log, &call->handles.hContext);
2689
2690 WLog_Print(log, g_LogLevel, "dwTimeOut: 0x%08" PRIX32 " cReaders: %" PRIu32 "", call->dwTimeOut,
2691 call->cReaders);
2692
2693 dump_reader_states_a(log, call->rgReaderStates, call->cReaders);
2694
2695 WLog_Print(log, g_LogLevel, "}");
2696}
2697
2698WINPR_ATTR_NODISCARD static LONG smartcard_unpack_reader_state_a(wLog* log, wStream* s,
2699 LPSCARD_READERSTATEA* ppcReaders,
2700 UINT32 cReaders, UINT32* ptrIndex)
2701{
2702 LONG status = SCARD_E_NO_MEMORY;
2703
2704 WINPR_ASSERT(ppcReaders || (cReaders == 0));
2705 if (cReaders > UINT8_MAX)
2706 {
2707 WLog_Print(log, WLOG_ERROR,
2708 "Too many readers(%" PRIu32 "), rejecting request. Limited to <= %d", cReaders,
2709 UINT8_MAX);
2710 return SCARD_E_WRITE_TOO_MANY;
2711 }
2712
2713 if (!Stream_CheckAndLogRequiredLengthWLog(log, s, 4))
2714 return status;
2715
2716 const UINT32 len = Stream_Get_UINT32(s);
2717 if (len != cReaders)
2718 {
2719 WLog_Print(log, WLOG_ERROR, "Count mismatch when reading LPSCARD_READERSTATEA");
2720 return status;
2721 }
2722
2723 LPSCARD_READERSTATEA rgReaderStates =
2724 (LPSCARD_READERSTATEA)calloc(cReaders, sizeof(SCARD_READERSTATEA));
2725 BOOL* states = calloc(cReaders, sizeof(BOOL));
2726 if (!rgReaderStates || !states)
2727 goto fail;
2728 status = ERROR_INVALID_DATA;
2729
2730 for (UINT32 index = 0; index < cReaders; index++)
2731 {
2732 UINT32 ptr = UINT32_MAX;
2733 LPSCARD_READERSTATEA readerState = &rgReaderStates[index];
2734
2735 if (!Stream_CheckAndLogRequiredLengthWLog(log, s, 52))
2736 goto fail;
2737
2738 if (!smartcard_ndr_pointer_read(log, s, ptrIndex, &ptr))
2739 {
2740 if (ptr != 0)
2741 goto fail;
2742 }
2743 /* Ignore nullptr length strings */
2744 states[index] = ptr != 0;
2745 Stream_Read_UINT32(s, readerState->dwCurrentState); /* dwCurrentState (4 bytes) */
2746 Stream_Read_UINT32(s, readerState->dwEventState); /* dwEventState (4 bytes) */
2747 Stream_Read_UINT32(s, readerState->cbAtr); /* cbAtr (4 bytes) */
2748 if (readerState->cbAtr > ARRAYSIZE(readerState->rgbAtr))
2749 {
2750 WLog_Print(log, WLOG_WARN,
2751 "SCARD_READERSTATEA[%" PRIu32 "]::cbAtr %" PRIu32 " exceeds %" PRIuz, index,
2752 readerState->cbAtr, (size_t)ARRAYSIZE(readerState->rgbAtr));
2753 readerState->cbAtr = ARRAYSIZE(readerState->rgbAtr);
2754 }
2755 Stream_Read(s, readerState->rgbAtr, 36); /* rgbAtr [0..36] (36 bytes) */
2756 }
2757
2758 for (UINT32 index = 0; index < cReaders; index++)
2759 {
2760 LPSCARD_READERSTATEA readerState = &rgReaderStates[index];
2761
2762 /* Ignore empty strings */
2763 if (!states[index])
2764 continue;
2765 status = smartcard_ndr_read_a(log, s, &readerState->szReader, NDR_PTR_FULL);
2766 if (status != SCARD_S_SUCCESS)
2767 goto fail;
2768 if (!readerState->szReader)
2769 goto fail;
2770 }
2771
2772 *ppcReaders = rgReaderStates;
2773 free(states);
2774 return SCARD_S_SUCCESS;
2775fail:
2776 if (rgReaderStates)
2777 {
2778 for (UINT32 index = 0; index < cReaders; index++)
2779 {
2780 LPSCARD_READERSTATEA readerState = &rgReaderStates[index];
2781 free(readerState->szReader);
2782 }
2783 }
2784 free(rgReaderStates);
2785 free(states);
2786 return status;
2787}
2788
2789WINPR_ATTR_NODISCARD static LONG smartcard_unpack_reader_state_w(wLog* log, wStream* s,
2790 LPSCARD_READERSTATEW* ppcReaders,
2791 UINT32 cReaders, UINT32* ptrIndex)
2792{
2793 LONG status = SCARD_E_NO_MEMORY;
2794
2795 WINPR_ASSERT(ppcReaders || (cReaders == 0));
2796
2797 if (cReaders > UINT8_MAX)
2798 {
2799 WLog_Print(log, WLOG_ERROR,
2800 "Too many readers(%" PRIu32 "), rejecting request. Limited to <= %d", cReaders,
2801 UINT8_MAX);
2802 return SCARD_E_WRITE_TOO_MANY;
2803 }
2804
2805 if (!Stream_CheckAndLogRequiredLengthWLog(log, s, 4))
2806 return status;
2807
2808 const UINT32 len = Stream_Get_UINT32(s);
2809 if (len != cReaders)
2810 {
2811 WLog_Print(log, WLOG_ERROR, "Count mismatch when reading LPSCARD_READERSTATEW");
2812 return status;
2813 }
2814
2815 LPSCARD_READERSTATEW rgReaderStates =
2816 (LPSCARD_READERSTATEW)calloc(cReaders, sizeof(SCARD_READERSTATEW));
2817 BOOL* states = calloc(cReaders, sizeof(BOOL));
2818
2819 if (!rgReaderStates || !states)
2820 goto fail;
2821
2822 status = ERROR_INVALID_DATA;
2823 for (UINT32 index = 0; index < cReaders; index++)
2824 {
2825 UINT32 ptr = UINT32_MAX;
2826 LPSCARD_READERSTATEW readerState = &rgReaderStates[index];
2827
2828 if (!Stream_CheckAndLogRequiredLengthWLog(log, s, 52))
2829 goto fail;
2830
2831 if (!smartcard_ndr_pointer_read(log, s, ptrIndex, &ptr))
2832 {
2833 if (ptr != 0)
2834 goto fail;
2835 }
2836 /* Ignore nullptr length strings */
2837 states[index] = ptr != 0;
2838 Stream_Read_UINT32(s, readerState->dwCurrentState); /* dwCurrentState (4 bytes) */
2839 Stream_Read_UINT32(s, readerState->dwEventState); /* dwEventState (4 bytes) */
2840 Stream_Read_UINT32(s, readerState->cbAtr); /* cbAtr (4 bytes) */
2841 if (readerState->cbAtr > ARRAYSIZE(readerState->rgbAtr))
2842 {
2843 WLog_Print(log, WLOG_WARN,
2844 "SCARD_READERSTATEW[%" PRIu32 "]::cbAtr %" PRIu32 " exceeds %" PRIuz, index,
2845 readerState->cbAtr, ARRAYSIZE(readerState->rgbAtr));
2846 readerState->cbAtr = ARRAYSIZE(readerState->rgbAtr);
2847 }
2848 Stream_Read(s, readerState->rgbAtr, 36); /* rgbAtr [0..36] (36 bytes) */
2849 }
2850
2851 for (UINT32 index = 0; index < cReaders; index++)
2852 {
2853 LPSCARD_READERSTATEW readerState = &rgReaderStates[index];
2854
2855 /* Skip nullptr pointers */
2856 if (!states[index])
2857 continue;
2858
2859 status = smartcard_ndr_read_w(log, s, &readerState->szReader, NDR_PTR_FULL);
2860 if (status != SCARD_S_SUCCESS)
2861 goto fail;
2862 if (!readerState->szReader)
2863 goto fail;
2864 }
2865
2866 *ppcReaders = rgReaderStates;
2867 free(states);
2868 return SCARD_S_SUCCESS;
2869fail:
2870 if (rgReaderStates)
2871 {
2872 for (UINT32 index = 0; index < cReaders; index++)
2873 {
2874 LPSCARD_READERSTATEW readerState = &rgReaderStates[index];
2875 free(readerState->szReader);
2876 }
2877 }
2878 free(rgReaderStates);
2879 free(states);
2880 return status;
2881}
2882
2883/******************************************************************************/
2884/************************************* End Trace Functions ********************/
2885/******************************************************************************/
2886
2887LONG smartcard_unpack_get_status_change_a_call(wStream* s, GetStatusChangeA_Call* call)
2888{
2889 UINT32 ndrPtr = 0;
2890 UINT32 index = 0;
2891 UINT32 pbContextNdrPtr = 0;
2892
2893 WINPR_ASSERT(call);
2894 wLog* log = scard_log();
2895
2896 call->rgReaderStates = nullptr;
2897
2898 LONG status = smartcard_unpack_redir_scard_context(log, s, &(call->handles.hContext), &index,
2899 &pbContextNdrPtr);
2900 if (status != SCARD_S_SUCCESS)
2901 return status;
2902
2903 if (!Stream_CheckAndLogRequiredLengthWLog(log, s, 8))
2904 return STATUS_BUFFER_TOO_SMALL;
2905
2906 Stream_Read_UINT32(s, call->dwTimeOut); /* dwTimeOut (4 bytes) */
2907 const UINT32 cReaders = Stream_Get_UINT32(s); /* cReaders (4 bytes) */
2908 if (cReaders > 11)
2909 {
2910 WLog_Print(log, WLOG_WARN,
2911 "2.2.2.11 GetStatusChangeA_Call: cReaders must be <= 11, got %" PRIu32,
2912 cReaders);
2913 return ERROR_INVALID_DATA;
2914 }
2915
2916 if (!smartcard_ndr_pointer_read(log, s, &index, &ndrPtr))
2917 return ERROR_INVALID_DATA;
2918
2919 status = smartcard_unpack_redir_scard_context_ref(log, s, pbContextNdrPtr,
2920 &(call->handles.hContext));
2921 if (status != SCARD_S_SUCCESS)
2922 return status;
2923
2924 if (ndrPtr)
2925 {
2926 status = smartcard_unpack_reader_state_a(log, s, &call->rgReaderStates, cReaders, &index);
2927 if (status != SCARD_S_SUCCESS)
2928 return status;
2929 call->cReaders = cReaders;
2930 }
2931 else
2932 {
2933 WLog_Print(log, WLOG_WARN, "ndrPtr=0x%08" PRIx32 ", can not read rgReaderStates", ndrPtr);
2934 return SCARD_E_UNEXPECTED;
2935 }
2936
2937 smartcard_trace_get_status_change_a_call(log, call);
2938 return SCARD_S_SUCCESS;
2939}
2940
2941LONG smartcard_unpack_get_status_change_w_call(wStream* s, GetStatusChangeW_Call* call)
2942{
2943 UINT32 ndrPtr = 0;
2944 UINT32 index = 0;
2945 UINT32 pbContextNdrPtr = 0;
2946
2947 WINPR_ASSERT(call);
2948 wLog* log = scard_log();
2949 call->rgReaderStates = nullptr;
2950
2951 LONG status = smartcard_unpack_redir_scard_context(log, s, &(call->handles.hContext), &index,
2952 &pbContextNdrPtr);
2953 if (status != SCARD_S_SUCCESS)
2954 return status;
2955
2956 if (!Stream_CheckAndLogRequiredLengthWLog(log, s, 8))
2957 return STATUS_BUFFER_TOO_SMALL;
2958
2959 Stream_Read_UINT32(s, call->dwTimeOut); /* dwTimeOut (4 bytes) */
2960 const UINT32 cReaders = Stream_Get_UINT32(s); /* cReaders (4 bytes) */
2961 if (cReaders > 11)
2962 {
2963 WLog_Print(log, WLOG_WARN,
2964 "2.2.2.12 GetStatusChangeW_Call: cReaders must be <= 11, got %" PRIu32,
2965 cReaders);
2966 return ERROR_INVALID_DATA;
2967 }
2968
2969 if (!smartcard_ndr_pointer_read(log, s, &index, &ndrPtr))
2970 return ERROR_INVALID_DATA;
2971
2972 status = smartcard_unpack_redir_scard_context_ref(log, s, pbContextNdrPtr,
2973 &(call->handles.hContext));
2974 if (status != SCARD_S_SUCCESS)
2975 return status;
2976
2977 if (ndrPtr)
2978 {
2979 status = smartcard_unpack_reader_state_w(log, s, &call->rgReaderStates, cReaders, &index);
2980 if (status != SCARD_S_SUCCESS)
2981 return status;
2982 call->cReaders = cReaders;
2983 }
2984 else
2985 {
2986 WLog_Print(log, WLOG_WARN, "ndrPtr=0x%08" PRIx32 ", can not read rgReaderStates", ndrPtr);
2987 return SCARD_E_UNEXPECTED;
2988 }
2989
2990 smartcard_trace_get_status_change_w_call(log, call);
2991 return SCARD_S_SUCCESS;
2992}
2993
2994LONG smartcard_pack_get_status_change_return(wStream* s, const GetStatusChange_Return* ret,
2995 BOOL unicode)
2996{
2997 WINPR_ASSERT(ret);
2998 wLog* log = scard_log();
2999
3000 LONG status = 0;
3001 UINT32 cReaders = ret->cReaders;
3002 UINT32 index = 0;
3003
3004 if (ret->cReaders > 10)
3005 {
3006 WLog_Print(log, WLOG_ERROR,
3007 "2.2.3.5 LocateCards_Return and GetStatusChange_Return cReaders(%" PRIu32
3008 ") > 10",
3009 ret->cReaders);
3010 return SCARD_E_INVALID_PARAMETER;
3011 }
3012
3013 smartcard_trace_get_status_change_return(log, ret, unicode);
3014 if ((ret->ReturnCode != SCARD_S_SUCCESS) && (ret->ReturnCode != SCARD_E_TIMEOUT))
3015 cReaders = 0;
3016 if (cReaders == SCARD_AUTOALLOCATE)
3017 cReaders = 0;
3018
3019 if (!Stream_EnsureRemainingCapacity(s, 4))
3020 return SCARD_E_NO_MEMORY;
3021
3022 Stream_Write_UINT32(s, cReaders); /* cReaders (4 bytes) */
3023 if (!smartcard_ndr_pointer_write(s, &index, cReaders))
3024 return SCARD_E_NO_MEMORY;
3025 status = smartcard_ndr_write_state(s, ret->rgReaderStates, cReaders, NDR_PTR_SIMPLE);
3026 if (status != SCARD_S_SUCCESS)
3027 return status;
3028 return ret->ReturnCode;
3029}
3030
3031LONG smartcard_unpack_state_call(wStream* s, State_Call* call)
3032{
3033 UINT32 index = 0;
3034 UINT32 pbContextNdrPtr = 0;
3035
3036 wLog* log = scard_log();
3037
3038 WINPR_ASSERT(call);
3039 LONG status = smartcard_unpack_redir_scard_context(log, s, &(call->handles.hContext), &index,
3040 &pbContextNdrPtr);
3041 if (status != SCARD_S_SUCCESS)
3042 return status;
3043
3044 status = smartcard_unpack_redir_scard_handle(log, s, &(call->handles.hCard), &index);
3045 if (status != SCARD_S_SUCCESS)
3046 return status;
3047
3048 if (!Stream_CheckAndLogRequiredLengthWLog(log, s, 8))
3049 return STATUS_BUFFER_TOO_SMALL;
3050
3051 Stream_Read_INT32(s, call->fpbAtrIsNULL); /* fpbAtrIsNULL (4 bytes) */
3052 Stream_Read_UINT32(s, call->cbAtrLen); /* cbAtrLen (4 bytes) */
3053
3054 status = smartcard_unpack_redir_scard_context_ref(log, s, pbContextNdrPtr,
3055 &(call->handles.hContext));
3056 if (status != SCARD_S_SUCCESS)
3057 return status;
3058
3059 status = smartcard_unpack_redir_scard_handle_ref(log, s, &(call->handles.hCard));
3060 if (status != SCARD_S_SUCCESS)
3061 return status;
3062
3063 return status;
3064}
3065
3066LONG smartcard_pack_state_return(wStream* s, const State_Return* ret)
3067{
3068 WINPR_ASSERT(ret);
3069 wLog* log = scard_log();
3070 LONG status = 0;
3071 UINT32 cbAtrLen = ret->cbAtrLen;
3072 UINT32 index = 0;
3073
3074 smartcard_trace_state_return(log, ret);
3075 if (ret->ReturnCode != SCARD_S_SUCCESS)
3076 cbAtrLen = 0;
3077 if (cbAtrLen == SCARD_AUTOALLOCATE)
3078 cbAtrLen = 0;
3079
3080 Stream_Write_UINT32(s, ret->dwState); /* dwState (4 bytes) */
3081 Stream_Write_UINT32(s, ret->dwProtocol); /* dwProtocol (4 bytes) */
3082 Stream_Write_UINT32(s, cbAtrLen); /* cbAtrLen (4 bytes) */
3083 if (!smartcard_ndr_pointer_write(s, &index, cbAtrLen))
3084 return SCARD_E_NO_MEMORY;
3085 status = smartcard_ndr_write(s, ret->rgAtr, cbAtrLen, 1, NDR_PTR_SIMPLE);
3086 if (status != SCARD_S_SUCCESS)
3087 return status;
3088 return ret->ReturnCode;
3089}
3090
3091LONG smartcard_unpack_status_call(wStream* s, Status_Call* call, BOOL unicode)
3092{
3093 UINT32 index = 0;
3094 UINT32 pbContextNdrPtr = 0;
3095
3096 WINPR_ASSERT(call);
3097 wLog* log = scard_log();
3098
3099 LONG status = smartcard_unpack_redir_scard_context(log, s, &(call->handles.hContext), &index,
3100 &pbContextNdrPtr);
3101 if (status != SCARD_S_SUCCESS)
3102 return status;
3103
3104 status = smartcard_unpack_redir_scard_handle(log, s, &(call->handles.hCard), &index);
3105 if (status != SCARD_S_SUCCESS)
3106 return status;
3107
3108 if (!Stream_CheckAndLogRequiredLengthWLog(log, s, 12))
3109 return STATUS_BUFFER_TOO_SMALL;
3110
3111 Stream_Read_INT32(s, call->fmszReaderNamesIsNULL); /* fmszReaderNamesIsNULL (4 bytes) */
3112 Stream_Read_UINT32(s, call->cchReaderLen); /* cchReaderLen (4 bytes) */
3113 Stream_Read_UINT32(s, call->cbAtrLen); /* cbAtrLen (4 bytes) */
3114
3115 status = smartcard_unpack_redir_scard_context_ref(log, s, pbContextNdrPtr,
3116 &(call->handles.hContext));
3117 if (status != SCARD_S_SUCCESS)
3118 return status;
3119
3120 status = smartcard_unpack_redir_scard_handle_ref(log, s, &(call->handles.hCard));
3121 if (status != SCARD_S_SUCCESS)
3122 return status;
3123
3124 smartcard_trace_status_call(log, call, unicode);
3125 return status;
3126}
3127
3128LONG smartcard_pack_status_call(wStream* s, const Status_Call* call, BOOL unicode)
3129{
3130 WINPR_ASSERT(call);
3131 wLog* log = scard_log();
3132 UINT32 index = 0;
3133
3134 smartcard_trace_status_call(log, call, unicode);
3135
3136 LONG status = smartcard_pack_redir_scard_context(log, s, &call->handles.hContext, &index);
3137 if (status != SCARD_S_SUCCESS)
3138 return status;
3139
3140 status = smartcard_pack_redir_scard_handle(log, s, &call->handles.hCard, &index);
3141 if (status != SCARD_S_SUCCESS)
3142 return status;
3143
3144 if (!Stream_EnsureRemainingCapacity(s, 12))
3145 return SCARD_E_NO_MEMORY;
3146
3147 Stream_Write_INT32(s, call->fmszReaderNamesIsNULL);
3148 Stream_Write_UINT32(s, call->cchReaderLen);
3149 Stream_Write_UINT32(s, call->cbAtrLen);
3150
3151 status = smartcard_pack_redir_scard_context_ref(log, s, &call->handles.hContext);
3152 if (status != SCARD_S_SUCCESS)
3153 return status;
3154
3155 return smartcard_pack_redir_scard_handle_ref(log, s, &call->handles.hCard);
3156}
3157
3158LONG smartcard_pack_status_return(wStream* s, const Status_Return* ret, BOOL unicode)
3159{
3160 WINPR_ASSERT(ret);
3161 wLog* log = scard_log();
3162
3163 LONG status = 0;
3164 UINT32 index = 0;
3165 UINT32 cBytes = ret->cBytes;
3166
3167 smartcard_trace_status_return(log, ret, unicode);
3168 if (ret->ReturnCode != SCARD_S_SUCCESS)
3169 cBytes = 0;
3170 if (cBytes == SCARD_AUTOALLOCATE)
3171 cBytes = 0;
3172
3173 if (!Stream_EnsureRemainingCapacity(s, 4))
3174 return SCARD_F_INTERNAL_ERROR;
3175
3176 if (cBytes > 65536)
3177 {
3178 WLog_Print(log, WLOG_ERROR, "2.2.3.10 Status_Return cBytes(%" PRIu32 ") > 65536", cBytes);
3179 return SCARD_E_INVALID_PARAMETER;
3180 }
3181
3182 Stream_Write_UINT32(s, cBytes); /* cBytes (4 bytes) */
3183 if (!smartcard_ndr_pointer_write(s, &index, cBytes))
3184 return SCARD_E_NO_MEMORY;
3185
3186 if (!Stream_EnsureRemainingCapacity(s, 44))
3187 return SCARD_F_INTERNAL_ERROR;
3188
3189 Stream_Write_UINT32(s, ret->dwState); /* dwState (4 bytes) */
3190 Stream_Write_UINT32(s, ret->dwProtocol); /* dwProtocol (4 bytes) */
3191 Stream_Write(s, ret->pbAtr, sizeof(ret->pbAtr)); /* pbAtr (32 bytes) */
3192
3193 if (ret->cbAtrLen > 32)
3194 {
3195 WLog_Print(log, WLOG_ERROR, "2.2.3.10 Status_Return cbAtrLen(%" PRIu32 ") > 32",
3196 ret->cbAtrLen);
3197 return SCARD_E_INVALID_PARAMETER;
3198 }
3199
3200 Stream_Write_UINT32(s, ret->cbAtrLen); /* cbAtrLen (4 bytes) */
3201 status = smartcard_ndr_write(s, ret->mszReaderNames, cBytes, 1, NDR_PTR_SIMPLE);
3202 if (status != SCARD_S_SUCCESS)
3203 return status;
3204 return ret->ReturnCode;
3205}
3206
3207LONG smartcard_unpack_status_return(wStream* s, Status_Return* ret, BOOL unicode)
3208{
3209 WINPR_ASSERT(ret);
3210 wLog* log = scard_log();
3211 UINT32 index = 0;
3212 UINT32 mszNdrPtr = 0;
3213
3214 if (!Stream_CheckAndLogRequiredLengthWLog(log, s, 4))
3215 return STATUS_BUFFER_TOO_SMALL;
3216
3217 const UINT32 cBytes = Stream_Get_UINT32(s);
3218 if (cBytes > 65536)
3219 {
3220 WLog_Print(log, WLOG_ERROR, "2.2.3.10 Status_Return cBytes(%" PRIu32 ") > 65536", cBytes);
3221 return SCARD_E_INVALID_PARAMETER;
3222 }
3223
3224 if (!smartcard_ndr_pointer_read(log, s, &index, &mszNdrPtr))
3225 return ERROR_INVALID_DATA;
3226
3227 if (!Stream_CheckAndLogRequiredLengthWLog(log, s, 44))
3228 return STATUS_BUFFER_TOO_SMALL;
3229
3230 Stream_Read_UINT32(s, ret->dwState);
3231 Stream_Read_UINT32(s, ret->dwProtocol);
3232 Stream_Read(s, ret->pbAtr, sizeof(ret->pbAtr));
3233 Stream_Read_UINT32(s, ret->cbAtrLen);
3234
3235 if (ret->cbAtrLen > ARRAYSIZE(ret->pbAtr))
3236 {
3237 WLog_Print(log, WLOG_ERROR, "Status_Return::cbAtrLen %" PRIu32 " exceeds %" PRIuz,
3238 ret->cbAtrLen, ARRAYSIZE(ret->pbAtr));
3239 return SCARD_E_INVALID_ATR;
3240 }
3241
3242 if (mszNdrPtr)
3243 {
3244 LONG status = smartcard_ndr_read(log, s, &ret->mszReaderNames, cBytes, 1, NDR_PTR_SIMPLE);
3245 if (status != SCARD_S_SUCCESS)
3246 return status;
3247 ret->cBytes = cBytes;
3248 }
3249
3250 smartcard_trace_status_return(log, ret, unicode);
3251 return SCARD_S_SUCCESS;
3252}
3253
3254LONG smartcard_unpack_get_attrib_call(wStream* s, GetAttrib_Call* call)
3255{
3256 WINPR_ASSERT(call);
3257 wLog* log = scard_log();
3258 UINT32 index = 0;
3259 UINT32 pbContextNdrPtr = 0;
3260
3261 LONG status = smartcard_unpack_redir_scard_context(log, s, &(call->handles.hContext), &index,
3262 &pbContextNdrPtr);
3263 if (status != SCARD_S_SUCCESS)
3264 return status;
3265
3266 status = smartcard_unpack_redir_scard_handle(log, s, &(call->handles.hCard), &index);
3267 if (status != SCARD_S_SUCCESS)
3268 return status;
3269
3270 if (!Stream_CheckAndLogRequiredLengthWLog(log, s, 12))
3271 return STATUS_BUFFER_TOO_SMALL;
3272
3273 Stream_Read_UINT32(s, call->dwAttrId); /* dwAttrId (4 bytes) */
3274 Stream_Read_INT32(s, call->fpbAttrIsNULL); /* fpbAttrIsNULL (4 bytes) */
3275 Stream_Read_UINT32(s, call->cbAttrLen); /* cbAttrLen (4 bytes) */
3276
3277 status = smartcard_unpack_redir_scard_context_ref(log, s, pbContextNdrPtr,
3278 &(call->handles.hContext));
3279 if (status != SCARD_S_SUCCESS)
3280 return status;
3281
3282 status = smartcard_unpack_redir_scard_handle_ref(log, s, &(call->handles.hCard));
3283 if (status != SCARD_S_SUCCESS)
3284 return status;
3285
3286 smartcard_trace_get_attrib_call(log, call);
3287 return status;
3288}
3289
3290LONG smartcard_pack_get_attrib_return(wStream* s, const GetAttrib_Return* ret, DWORD dwAttrId,
3291 DWORD cbAttrCallLen)
3292{
3293 WINPR_ASSERT(ret);
3294 wLog* log = scard_log();
3295 LONG status = 0;
3296 UINT32 cbAttrLen = 0;
3297 UINT32 index = 0;
3298 smartcard_trace_get_attrib_return(log, ret, dwAttrId);
3299
3300 if (!Stream_EnsureRemainingCapacity(s, 4))
3301 return SCARD_F_INTERNAL_ERROR;
3302
3303 cbAttrLen = ret->cbAttrLen;
3304 if (ret->ReturnCode != SCARD_S_SUCCESS)
3305 cbAttrLen = 0;
3306 if (cbAttrLen == SCARD_AUTOALLOCATE)
3307 cbAttrLen = 0;
3308
3309 if (ret->pbAttr)
3310 {
3311 if (cbAttrCallLen < cbAttrLen)
3312 cbAttrLen = cbAttrCallLen;
3313 }
3314 if (cbAttrLen > 65536)
3315 {
3316 WLog_Print(log, WLOG_ERROR, "2.2.3.12 GetAttrib_Return cbAttrLen(%" PRIu32 ") > 65536",
3317 cbAttrLen);
3318 return SCARD_E_INVALID_PARAMETER;
3319 }
3320 Stream_Write_UINT32(s, cbAttrLen); /* cbAttrLen (4 bytes) */
3321 if (!smartcard_ndr_pointer_write(s, &index, cbAttrLen))
3322 return SCARD_E_NO_MEMORY;
3323
3324 status = smartcard_ndr_write(s, ret->pbAttr, cbAttrLen, 1, NDR_PTR_SIMPLE);
3325 if (status != SCARD_S_SUCCESS)
3326 return status;
3327 return ret->ReturnCode;
3328}
3329
3330LONG smartcard_unpack_control_call(wStream* s, Control_Call* call)
3331{
3332 WINPR_ASSERT(call);
3333 wLog* log = scard_log();
3334
3335 UINT32 index = 0;
3336 UINT32 pvInBufferNdrPtr = 0;
3337 UINT32 pbContextNdrPtr = 0;
3338
3339 call->pvInBuffer = nullptr;
3340
3341 LONG status = smartcard_unpack_redir_scard_context(log, s, &(call->handles.hContext), &index,
3342 &pbContextNdrPtr);
3343 if (status != SCARD_S_SUCCESS)
3344 return status;
3345
3346 status = smartcard_unpack_redir_scard_handle(log, s, &(call->handles.hCard), &index);
3347 if (status != SCARD_S_SUCCESS)
3348 return status;
3349
3350 if (!Stream_CheckAndLogRequiredLengthWLog(log, s, 20))
3351 return STATUS_BUFFER_TOO_SMALL;
3352
3353 Stream_Read_UINT32(s, call->dwControlCode); /* dwControlCode (4 bytes) */
3354 const UINT32 cbInBufferSize = Stream_Get_UINT32(s); /* cbInBufferSize (4 bytes) */
3355 if (cbInBufferSize > 66560)
3356 {
3357 WLog_Print(log, WLOG_ERROR, "2.2.2.20 Control_Call cbInBufferSize (%" PRIu32 ") > 66560",
3358 cbInBufferSize);
3359 return SCARD_E_INVALID_PARAMETER;
3360 }
3361
3362 if (!smartcard_ndr_pointer_read(log, s, &index,
3363 &pvInBufferNdrPtr)) /* pvInBufferNdrPtr (4 bytes) */
3364 return ERROR_INVALID_DATA;
3365 Stream_Read_INT32(s, call->fpvOutBufferIsNULL); /* fpvOutBufferIsNULL (4 bytes) */
3366 Stream_Read_UINT32(s, call->cbOutBufferSize); /* cbOutBufferSize (4 bytes) */
3367
3368 status = smartcard_unpack_redir_scard_context_ref(log, s, pbContextNdrPtr,
3369 &(call->handles.hContext));
3370 if (status != SCARD_S_SUCCESS)
3371 return status;
3372
3373 status = smartcard_unpack_redir_scard_handle_ref(log, s, &(call->handles.hCard));
3374 if (status != SCARD_S_SUCCESS)
3375 return status;
3376
3377 if (pvInBufferNdrPtr)
3378 {
3379 status = smartcard_ndr_read(log, s, &call->pvInBuffer, cbInBufferSize, 1, NDR_PTR_SIMPLE);
3380 if (status != SCARD_S_SUCCESS)
3381 return status;
3382 call->cbInBufferSize = cbInBufferSize;
3383 }
3384
3385 smartcard_trace_control_call(log, call);
3386 return SCARD_S_SUCCESS;
3387}
3388
3389LONG smartcard_pack_control_return(wStream* s, const Control_Return* ret)
3390{
3391 WINPR_ASSERT(ret);
3392 wLog* log = scard_log();
3393
3394 LONG status = 0;
3395 UINT32 cbDataLen = ret->cbOutBufferSize;
3396 UINT32 index = 0;
3397
3398 smartcard_trace_control_return(log, ret);
3399 if (ret->ReturnCode != SCARD_S_SUCCESS)
3400 cbDataLen = 0;
3401 if (cbDataLen == SCARD_AUTOALLOCATE)
3402 cbDataLen = 0;
3403
3404 if (!Stream_EnsureRemainingCapacity(s, 4))
3405 return SCARD_F_INTERNAL_ERROR;
3406
3407 if (cbDataLen > 66560)
3408 {
3409 WLog_Print(log, WLOG_ERROR, "2.2.3.6 Control_Return cbOutBufferSize(%" PRIu32 ") > 66560",
3410 cbDataLen);
3411 return SCARD_E_INVALID_PARAMETER;
3412 }
3413
3414 Stream_Write_UINT32(s, cbDataLen); /* cbOutBufferSize (4 bytes) */
3415 if (!smartcard_ndr_pointer_write(s, &index, cbDataLen))
3416 return SCARD_E_NO_MEMORY;
3417
3418 status = smartcard_ndr_write(s, ret->pvOutBuffer, cbDataLen, 1, NDR_PTR_SIMPLE);
3419 if (status != SCARD_S_SUCCESS)
3420 return status;
3421 return ret->ReturnCode;
3422}
3423
3424LONG smartcard_unpack_transmit_call(wStream* s, Transmit_Call* call)
3425{
3426 UINT32 length = 0;
3427 BYTE* pbExtraBytes = nullptr;
3428 UINT32 pbExtraBytesNdrPtr = 0;
3429 UINT32 pbSendBufferNdrPtr = 0;
3430 UINT32 pioRecvPciNdrPtr = 0;
3431 SCardIO_Request ioSendPci;
3432 SCardIO_Request ioRecvPci;
3433 UINT32 index = 0;
3434 UINT32 pbContextNdrPtr = 0;
3435
3436 WINPR_ASSERT(call);
3437 wLog* log = scard_log();
3438
3439 call->pioSendPci = nullptr;
3440 call->pioRecvPci = nullptr;
3441 call->pbSendBuffer = nullptr;
3442
3443 LONG status = smartcard_unpack_redir_scard_context(log, s, &(call->handles.hContext), &index,
3444 &pbContextNdrPtr);
3445 if (status != SCARD_S_SUCCESS)
3446 return status;
3447
3448 status = smartcard_unpack_redir_scard_handle(log, s, &(call->handles.hCard), &index);
3449 if (status != SCARD_S_SUCCESS)
3450 return status;
3451
3452 if (!Stream_CheckAndLogRequiredLengthWLog(log, s, 32))
3453 return STATUS_BUFFER_TOO_SMALL;
3454
3455 Stream_Read_UINT32(s, ioSendPci.dwProtocol); /* dwProtocol (4 bytes) */
3456 Stream_Read_UINT32(s, ioSendPci.cbExtraBytes); /* cbExtraBytes (4 bytes) */
3457 if (!smartcard_ndr_pointer_read(log, s, &index,
3458 &pbExtraBytesNdrPtr)) /* pbExtraBytesNdrPtr (4 bytes) */
3459 return ERROR_INVALID_DATA;
3460
3461 const UINT32 cbSendLength = Stream_Get_UINT32(s); /* cbSendLength (4 bytes) */
3462 if (cbSendLength > 66560)
3463 {
3464 WLog_Print(log, WLOG_ERROR, "2.2.2.19 Transmit_Call cbSendLength (%" PRIu32 ") > 66560",
3465 cbSendLength);
3466 return SCARD_E_INVALID_PARAMETER;
3467 }
3468 if (!smartcard_ndr_pointer_read(log, s, &index,
3469 &pbSendBufferNdrPtr)) /* pbSendBufferNdrPtr (4 bytes) */
3470 return ERROR_INVALID_DATA;
3471
3472 if (!smartcard_ndr_pointer_read(log, s, &index,
3473 &pioRecvPciNdrPtr)) /* pioRecvPciNdrPtr (4 bytes) */
3474 return ERROR_INVALID_DATA;
3475
3476 Stream_Read_INT32(s, call->fpbRecvBufferIsNULL); /* fpbRecvBufferIsNULL (4 bytes) */
3477 Stream_Read_UINT32(s, call->cbRecvLength); /* cbRecvLength (4 bytes) */
3478
3479 if (ioSendPci.cbExtraBytes > 1024)
3480 {
3481 WLog_Print(log, WLOG_WARN,
3482 "Transmit_Call ioSendPci.cbExtraBytes is out of bounds: %" PRIu32 " (max: 1024)",
3483 ioSendPci.cbExtraBytes);
3484 return STATUS_INVALID_PARAMETER;
3485 }
3486
3487 if (cbSendLength > 66560)
3488 {
3489 WLog_Print(log, WLOG_WARN,
3490 "Transmit_Call cbSendLength is out of bounds: %" PRIu32 " (max: 66560)",
3491 ioSendPci.cbExtraBytes);
3492 return STATUS_INVALID_PARAMETER;
3493 }
3494
3495 status = smartcard_unpack_redir_scard_context_ref(log, s, pbContextNdrPtr,
3496 &(call->handles.hContext));
3497 if (status != SCARD_S_SUCCESS)
3498 return status;
3499
3500 status = smartcard_unpack_redir_scard_handle_ref(log, s, &(call->handles.hCard));
3501 if (status != SCARD_S_SUCCESS)
3502 return status;
3503
3504 if (ioSendPci.cbExtraBytes && !pbExtraBytesNdrPtr)
3505 {
3506 WLog_Print(
3507 log, WLOG_WARN,
3508 "Transmit_Call ioSendPci.cbExtraBytes is non-zero but pbExtraBytesNdrPtr is null");
3509 return STATUS_INVALID_PARAMETER;
3510 }
3511
3512 if (pbExtraBytesNdrPtr)
3513 {
3514 // TODO: Use unified pointer reading
3515 if (!Stream_CheckAndLogRequiredLengthWLog(log, s, 4))
3516 return STATUS_BUFFER_TOO_SMALL;
3517
3518 Stream_Read_UINT32(s, length); /* Length (4 bytes) */
3519
3520 if (!Stream_CheckAndLogRequiredLengthWLog(log, s, ioSendPci.cbExtraBytes))
3521 return STATUS_BUFFER_TOO_SMALL;
3522
3523 ioSendPci.pbExtraBytes = Stream_Pointer(s);
3524 call->pioSendPci =
3525 (LPSCARD_IO_REQUEST)malloc(sizeof(SCARD_IO_REQUEST) + ioSendPci.cbExtraBytes);
3526
3527 if (!call->pioSendPci)
3528 {
3529 WLog_Print(log, WLOG_WARN, "Transmit_Call out of memory error (pioSendPci)");
3530 return STATUS_NO_MEMORY;
3531 }
3532
3533 call->pioSendPci->dwProtocol = ioSendPci.dwProtocol;
3534 call->pioSendPci->cbPciLength = (DWORD)(ioSendPci.cbExtraBytes + sizeof(SCARD_IO_REQUEST));
3535 pbExtraBytes = &((BYTE*)call->pioSendPci)[sizeof(SCARD_IO_REQUEST)];
3536 Stream_Read(s, pbExtraBytes, ioSendPci.cbExtraBytes);
3537 if (smartcard_unpack_read_size_align(s, ioSendPci.cbExtraBytes, 4) < 0)
3538 return STATUS_INVALID_PARAMETER;
3539 }
3540 else
3541 {
3542 call->pioSendPci = (LPSCARD_IO_REQUEST)calloc(1, sizeof(SCARD_IO_REQUEST));
3543
3544 if (!call->pioSendPci)
3545 {
3546 WLog_Print(log, WLOG_WARN, "Transmit_Call out of memory error (pioSendPci)");
3547 return STATUS_NO_MEMORY;
3548 }
3549
3550 call->pioSendPci->dwProtocol = ioSendPci.dwProtocol;
3551 call->pioSendPci->cbPciLength = sizeof(SCARD_IO_REQUEST);
3552 }
3553
3554 if (pbSendBufferNdrPtr)
3555 {
3556 status = smartcard_ndr_read(log, s, &call->pbSendBuffer, cbSendLength, 1, NDR_PTR_SIMPLE);
3557 if (status != SCARD_S_SUCCESS)
3558 return status;
3559 call->cbSendLength = cbSendLength;
3560 }
3561
3562 if (pioRecvPciNdrPtr)
3563 {
3564 if (!Stream_CheckAndLogRequiredLengthWLog(log, s, 12))
3565 return STATUS_BUFFER_TOO_SMALL;
3566
3567 Stream_Read_UINT32(s, ioRecvPci.dwProtocol); /* dwProtocol (4 bytes) */
3568 Stream_Read_UINT32(s, ioRecvPci.cbExtraBytes); /* cbExtraBytes (4 bytes) */
3569 if (!smartcard_ndr_pointer_read(log, s, &index,
3570 &pbExtraBytesNdrPtr)) /* pbExtraBytesNdrPtr (4 bytes) */
3571 return ERROR_INVALID_DATA;
3572
3573 if (ioRecvPci.cbExtraBytes && !pbExtraBytesNdrPtr)
3574 {
3575 WLog_Print(
3576 log, WLOG_WARN,
3577 "Transmit_Call ioRecvPci.cbExtraBytes is non-zero but pbExtraBytesNdrPtr is null");
3578 return STATUS_INVALID_PARAMETER;
3579 }
3580
3581 if (pbExtraBytesNdrPtr)
3582 {
3583 // TODO: Unify ndr pointer reading
3584 if (!Stream_CheckAndLogRequiredLengthWLog(log, s, 4))
3585 return STATUS_BUFFER_TOO_SMALL;
3586
3587 Stream_Read_UINT32(s, length); /* Length (4 bytes) */
3588
3589 if (ioRecvPci.cbExtraBytes > 1024)
3590 {
3591 WLog_Print(log, WLOG_WARN,
3592 "Transmit_Call ioRecvPci.cbExtraBytes is out of bounds: %" PRIu32
3593 " (max: 1024)",
3594 ioRecvPci.cbExtraBytes);
3595 return STATUS_INVALID_PARAMETER;
3596 }
3597
3598 if (length != ioRecvPci.cbExtraBytes)
3599 {
3600 WLog_Print(log, WLOG_WARN,
3601 "Transmit_Call unexpected length: Actual: %" PRIu32
3602 ", Expected: %" PRIu32 " (ioRecvPci.cbExtraBytes)",
3603 length, ioRecvPci.cbExtraBytes);
3604 return STATUS_INVALID_PARAMETER;
3605 }
3606
3607 if (!Stream_CheckAndLogRequiredLengthWLog(log, s, ioRecvPci.cbExtraBytes))
3608 return STATUS_BUFFER_TOO_SMALL;
3609
3610 ioRecvPci.pbExtraBytes = Stream_Pointer(s);
3611 call->pioRecvPci =
3612 (LPSCARD_IO_REQUEST)malloc(sizeof(SCARD_IO_REQUEST) + ioRecvPci.cbExtraBytes);
3613
3614 if (!call->pioRecvPci)
3615 {
3616 WLog_Print(log, WLOG_WARN, "Transmit_Call out of memory error (pioRecvPci)");
3617 return STATUS_NO_MEMORY;
3618 }
3619
3620 call->pioRecvPci->dwProtocol = ioRecvPci.dwProtocol;
3621 call->pioRecvPci->cbPciLength =
3622 (DWORD)(ioRecvPci.cbExtraBytes + sizeof(SCARD_IO_REQUEST));
3623 pbExtraBytes = &((BYTE*)call->pioRecvPci)[sizeof(SCARD_IO_REQUEST)];
3624 Stream_Read(s, pbExtraBytes, ioRecvPci.cbExtraBytes);
3625 if (smartcard_unpack_read_size_align(s, ioRecvPci.cbExtraBytes, 4) < 0)
3626 return STATUS_INVALID_PARAMETER;
3627 }
3628 else
3629 {
3630 call->pioRecvPci = (LPSCARD_IO_REQUEST)calloc(1, sizeof(SCARD_IO_REQUEST));
3631
3632 if (!call->pioRecvPci)
3633 {
3634 WLog_Print(log, WLOG_WARN, "Transmit_Call out of memory error (pioRecvPci)");
3635 return STATUS_NO_MEMORY;
3636 }
3637
3638 call->pioRecvPci->dwProtocol = ioRecvPci.dwProtocol;
3639 call->pioRecvPci->cbPciLength = sizeof(SCARD_IO_REQUEST);
3640 }
3641 }
3642
3643 smartcard_trace_transmit_call(log, call);
3644 return SCARD_S_SUCCESS;
3645}
3646
3647LONG smartcard_pack_transmit_return(wStream* s, const Transmit_Return* ret)
3648{
3649 WINPR_ASSERT(ret);
3650 wLog* log = scard_log();
3651
3652 LONG status = 0;
3653 UINT32 index = 0;
3654 LONG error = 0;
3655 UINT32 cbRecvLength = ret->cbRecvLength;
3656 UINT32 cbRecvPci = ret->pioRecvPci ? ret->pioRecvPci->cbPciLength : 0;
3657
3658 smartcard_trace_transmit_return(log, ret);
3659
3660 if (!ret->pbRecvBuffer)
3661 cbRecvLength = 0;
3662
3663 if (!smartcard_ndr_pointer_write(s, &index, cbRecvPci))
3664 return SCARD_E_NO_MEMORY;
3665 if (!Stream_EnsureRemainingCapacity(s, 4))
3666 return SCARD_E_NO_MEMORY;
3667
3668 if (cbRecvLength > 66560)
3669 {
3670 WLog_Print(log, WLOG_ERROR, "2.2.3.11 Transmit_Return cbRecvLength(%" PRIu32 ") > 66560",
3671 cbRecvLength);
3672 return SCARD_E_INVALID_PARAMETER;
3673 }
3674
3675 Stream_Write_UINT32(s, cbRecvLength); /* cbRecvLength (4 bytes) */
3676 if (!smartcard_ndr_pointer_write(s, &index, cbRecvLength))
3677 return SCARD_E_NO_MEMORY;
3678
3679 if (ret->pioRecvPci)
3680 {
3681 UINT32 cbExtraBytes = (UINT32)(ret->pioRecvPci->cbPciLength - sizeof(SCARD_IO_REQUEST));
3682 BYTE* pbExtraBytes = &((BYTE*)ret->pioRecvPci)[sizeof(SCARD_IO_REQUEST)];
3683
3684 if (!Stream_EnsureRemainingCapacity(s, 8ull))
3685 {
3686 WLog_Print(log, WLOG_ERROR, "Stream_EnsureRemainingCapacity failed!");
3687 return SCARD_F_INTERNAL_ERROR;
3688 }
3689
3690 Stream_Write_UINT32(s, ret->pioRecvPci->dwProtocol); /* dwProtocol (4 bytes) */
3691 Stream_Write_UINT32(s, cbExtraBytes); /* cbExtraBytes (4 bytes) */
3692 if (!smartcard_ndr_pointer_write(s, &index, cbExtraBytes))
3693 return SCARD_E_NO_MEMORY;
3694 error = smartcard_ndr_write(s, pbExtraBytes, cbExtraBytes, 1, NDR_PTR_SIMPLE);
3695 if (error)
3696 return error;
3697 }
3698
3699 status = smartcard_ndr_write(s, ret->pbRecvBuffer, ret->cbRecvLength, 1, NDR_PTR_SIMPLE);
3700 if (status != SCARD_S_SUCCESS)
3701 return status;
3702 return ret->ReturnCode;
3703}
3704
3705LONG smartcard_unpack_locate_cards_by_atr_a_call(wStream* s, LocateCardsByATRA_Call* call)
3706{
3707 UINT32 rgReaderStatesNdrPtr = 0;
3708 UINT32 rgAtrMasksNdrPtr = 0;
3709 UINT32 index = 0;
3710 UINT32 pbContextNdrPtr = 0;
3711
3712 WINPR_ASSERT(call);
3713 wLog* log = scard_log();
3714
3715 call->rgReaderStates = nullptr;
3716
3717 LONG status = smartcard_unpack_redir_scard_context(log, s, &(call->handles.hContext), &index,
3718 &pbContextNdrPtr);
3719 if (status != SCARD_S_SUCCESS)
3720 return status;
3721
3722 if (!Stream_CheckAndLogRequiredLengthWLog(log, s, 16))
3723 return STATUS_BUFFER_TOO_SMALL;
3724
3725 Stream_Read_UINT32(s, call->cAtrs);
3726 if (call->cAtrs > 1000)
3727 {
3728 WLog_Print(log, WLOG_ERROR, "2.2.2.23 LocateCardsByATRA_Call cAtrs(%" PRIu32 ") > 1000",
3729 call->cAtrs);
3730 return SCARD_E_INVALID_PARAMETER;
3731 }
3732
3733 if (!smartcard_ndr_pointer_read(log, s, &index, &rgAtrMasksNdrPtr))
3734 return ERROR_INVALID_DATA;
3735 const UINT32 cReaders = Stream_Get_UINT32(s); /* cReaders (4 bytes) */
3736 if (cReaders > 10)
3737 {
3738 WLog_Print(log, WLOG_ERROR, "2.2.2.23 LocateCardsByATRA_Call cReaders(%" PRIu32 ") > 10",
3739 cReaders);
3740 return SCARD_E_INVALID_PARAMETER;
3741 }
3742 if (!smartcard_ndr_pointer_read(log, s, &index, &rgReaderStatesNdrPtr))
3743 return ERROR_INVALID_DATA;
3744
3745 status = smartcard_unpack_redir_scard_context_ref(log, s, pbContextNdrPtr,
3746 &(call->handles.hContext));
3747 if (status != SCARD_S_SUCCESS)
3748 return status;
3749
3750 if ((rgAtrMasksNdrPtr && !call->cAtrs) || (!rgAtrMasksNdrPtr && call->cAtrs))
3751 {
3752 WLog_Print(log, WLOG_WARN,
3753 "LocateCardsByATRA_Call rgAtrMasksNdrPtr (0x%08" PRIX32
3754 ") and cAtrs (0x%08" PRIX32 ") inconsistency",
3755 rgAtrMasksNdrPtr, call->cAtrs);
3756 return STATUS_INVALID_PARAMETER;
3757 }
3758
3759 if (rgAtrMasksNdrPtr)
3760 {
3761 status = smartcard_ndr_read_atrmask(log, s, &call->rgAtrMasks, call->cAtrs, NDR_PTR_SIMPLE);
3762 if (status != SCARD_S_SUCCESS)
3763 return status;
3764 }
3765
3766 if (rgReaderStatesNdrPtr)
3767 {
3768 status = smartcard_unpack_reader_state_a(log, s, &call->rgReaderStates, cReaders, &index);
3769 if (status != SCARD_S_SUCCESS)
3770 return status;
3771 call->cReaders = cReaders;
3772 }
3773
3774 smartcard_trace_locate_cards_by_atr_a_call(log, call);
3775 return SCARD_S_SUCCESS;
3776}
3777
3778LONG smartcard_unpack_context_and_two_strings_a_call(wStream* s, ContextAndTwoStringA_Call* call)
3779{
3780 UINT32 sz1NdrPtr = 0;
3781 UINT32 sz2NdrPtr = 0;
3782 UINT32 index = 0;
3783 UINT32 pbContextNdrPtr = 0;
3784
3785 WINPR_ASSERT(call);
3786 wLog* log = scard_log();
3787
3788 LONG status = smartcard_unpack_redir_scard_context(log, s, &(call->handles.hContext), &index,
3789 &pbContextNdrPtr);
3790 if (status != SCARD_S_SUCCESS)
3791 return status;
3792
3793 if (!smartcard_ndr_pointer_read(log, s, &index, &sz1NdrPtr))
3794 return ERROR_INVALID_DATA;
3795 if (!smartcard_ndr_pointer_read(log, s, &index, &sz2NdrPtr))
3796 return ERROR_INVALID_DATA;
3797
3798 status =
3799 smartcard_unpack_redir_scard_context_ref(log, s, pbContextNdrPtr, &call->handles.hContext);
3800 if (status != SCARD_S_SUCCESS)
3801 return status;
3802
3803 if (sz1NdrPtr)
3804 {
3805 status = smartcard_ndr_read_a(log, s, &call->sz1, NDR_PTR_FULL);
3806 if (status != SCARD_S_SUCCESS)
3807 return status;
3808 }
3809 if (sz2NdrPtr)
3810 {
3811 status = smartcard_ndr_read_a(log, s, &call->sz2, NDR_PTR_FULL);
3812 if (status != SCARD_S_SUCCESS)
3813 return status;
3814 }
3815 smartcard_trace_context_and_two_strings_a_call(log, call);
3816 return SCARD_S_SUCCESS;
3817}
3818
3819LONG smartcard_unpack_context_and_two_strings_w_call(wStream* s, ContextAndTwoStringW_Call* call)
3820{
3821 UINT32 sz1NdrPtr = 0;
3822 UINT32 sz2NdrPtr = 0;
3823 UINT32 index = 0;
3824 UINT32 pbContextNdrPtr = 0;
3825
3826 WINPR_ASSERT(call);
3827 wLog* log = scard_log();
3828
3829 LONG status = smartcard_unpack_redir_scard_context(log, s, &(call->handles.hContext), &index,
3830 &pbContextNdrPtr);
3831 if (status != SCARD_S_SUCCESS)
3832 return status;
3833
3834 if (!smartcard_ndr_pointer_read(log, s, &index, &sz1NdrPtr))
3835 return ERROR_INVALID_DATA;
3836 if (!smartcard_ndr_pointer_read(log, s, &index, &sz2NdrPtr))
3837 return ERROR_INVALID_DATA;
3838
3839 status =
3840 smartcard_unpack_redir_scard_context_ref(log, s, pbContextNdrPtr, &call->handles.hContext);
3841 if (status != SCARD_S_SUCCESS)
3842 return status;
3843
3844 if (sz1NdrPtr)
3845 {
3846 status = smartcard_ndr_read_w(log, s, &call->sz1, NDR_PTR_FULL);
3847 if (status != SCARD_S_SUCCESS)
3848 return status;
3849 }
3850 if (sz2NdrPtr)
3851 {
3852 status = smartcard_ndr_read_w(log, s, &call->sz2, NDR_PTR_FULL);
3853 if (status != SCARD_S_SUCCESS)
3854 return status;
3855 }
3856 smartcard_trace_context_and_two_strings_w_call(log, call);
3857 return SCARD_S_SUCCESS;
3858}
3859
3860LONG smartcard_unpack_locate_cards_a_call(wStream* s, LocateCardsA_Call* call)
3861{
3862 UINT32 sz1NdrPtr = 0;
3863 UINT32 sz2NdrPtr = 0;
3864 UINT32 index = 0;
3865 UINT32 pbContextNdrPtr = 0;
3866
3867 WINPR_ASSERT(call);
3868 wLog* log = scard_log();
3869
3870 LONG status = smartcard_unpack_redir_scard_context(log, s, &(call->handles.hContext), &index,
3871 &pbContextNdrPtr);
3872 if (status != SCARD_S_SUCCESS)
3873 return status;
3874
3875 if (!Stream_CheckAndLogRequiredLengthWLog(log, s, 16))
3876 return STATUS_BUFFER_TOO_SMALL;
3877
3878 const UINT32 cBytes = Stream_Get_UINT32(s);
3879 if (call->cBytes > 65536)
3880 {
3881 WLog_Print(log, WLOG_ERROR, "2.2.2.9 LocateCardsA_Call cBytes(%" PRIu32 ") > 65536",
3882 call->cBytes);
3883 return SCARD_E_INVALID_PARAMETER;
3884 }
3885
3886 if (!smartcard_ndr_pointer_read(log, s, &index, &sz1NdrPtr))
3887 return ERROR_INVALID_DATA;
3888
3889 const UINT32 cReaders = Stream_Get_UINT32(s);
3890 if (cReaders > 10)
3891 {
3892 WLog_Print(log, WLOG_ERROR, "2.2.2.9 LocateCardsA_Call cReaders(%" PRIu32 ") > 10",
3893 cReaders);
3894 return SCARD_E_INVALID_PARAMETER;
3895 }
3896 if (!smartcard_ndr_pointer_read(log, s, &index, &sz2NdrPtr))
3897 return ERROR_INVALID_DATA;
3898
3899 status = smartcard_unpack_redir_scard_context_ref(log, s, pbContextNdrPtr,
3900 &(call->handles.hContext));
3901 if (status != SCARD_S_SUCCESS)
3902 return status;
3903
3904 if (sz1NdrPtr)
3905 {
3906 status = smartcard_ndr_read_fixed_string_a(log, s, &call->mszCards, cBytes, NDR_PTR_SIMPLE);
3907 if (status != SCARD_S_SUCCESS)
3908 return status;
3909 call->cBytes = cBytes;
3910 }
3911 if (sz2NdrPtr)
3912 {
3913 status = smartcard_unpack_reader_state_a(log, s, &call->rgReaderStates, cReaders, &index);
3914 if (status != SCARD_S_SUCCESS)
3915 return status;
3916 call->cReaders = cReaders;
3917 }
3918 smartcard_trace_locate_cards_a_call(log, call);
3919 return SCARD_S_SUCCESS;
3920}
3921
3922LONG smartcard_unpack_locate_cards_w_call(wStream* s, LocateCardsW_Call* call)
3923{
3924 UINT32 sz1NdrPtr = 0;
3925 UINT32 sz2NdrPtr = 0;
3926 UINT32 index = 0;
3927 UINT32 pbContextNdrPtr = 0;
3928
3929 WINPR_ASSERT(call);
3930 wLog* log = scard_log();
3931
3932 LONG status = smartcard_unpack_redir_scard_context(log, s, &(call->handles.hContext), &index,
3933 &pbContextNdrPtr);
3934 if (status != SCARD_S_SUCCESS)
3935 return status;
3936
3937 if (!Stream_CheckAndLogRequiredLengthWLog(log, s, 16))
3938 return STATUS_BUFFER_TOO_SMALL;
3939
3940 const UINT32 cBytes = Stream_Get_UINT32(s);
3941 if (call->cBytes > 65536)
3942 {
3943 WLog_Print(log, WLOG_ERROR, "2.2.2.10 LocateCardsW_Call cBytes(%" PRIu32 ") > 65536",
3944 call->cBytes);
3945 return SCARD_E_INVALID_PARAMETER;
3946 }
3947
3948 if (!smartcard_ndr_pointer_read(log, s, &index, &sz1NdrPtr))
3949 return ERROR_INVALID_DATA;
3950
3951 const UINT32 cReaders = Stream_Get_UINT32(s);
3952 if (cReaders > 10)
3953 {
3954 WLog_Print(log, WLOG_ERROR, "2.2.2.10 LocateCardsW_Call cReaders(%" PRIu32 ") > 10",
3955 cReaders);
3956 return SCARD_E_INVALID_PARAMETER;
3957 }
3958
3959 if (!smartcard_ndr_pointer_read(log, s, &index, &sz2NdrPtr))
3960 return ERROR_INVALID_DATA;
3961
3962 status = smartcard_unpack_redir_scard_context_ref(log, s, pbContextNdrPtr,
3963 &(call->handles.hContext));
3964 if (status != SCARD_S_SUCCESS)
3965 return status;
3966
3967 if (sz1NdrPtr)
3968 {
3969 status = smartcard_ndr_read_fixed_string_w(log, s, &call->mszCards, cBytes, NDR_PTR_SIMPLE);
3970 if (status != SCARD_S_SUCCESS)
3971 return status;
3972 call->cBytes = cBytes;
3973 }
3974 if (sz2NdrPtr)
3975 {
3976 status = smartcard_unpack_reader_state_w(log, s, &call->rgReaderStates, cReaders, &index);
3977 if (status != SCARD_S_SUCCESS)
3978 return status;
3979 call->cReaders = cReaders;
3980 }
3981 smartcard_trace_locate_cards_w_call(log, call);
3982 return SCARD_S_SUCCESS;
3983}
3984
3985LONG smartcard_unpack_set_attrib_call(wStream* s, SetAttrib_Call* call)
3986{
3987 UINT32 index = 0;
3988 UINT32 ndrPtr = 0;
3989 UINT32 pbContextNdrPtr = 0;
3990
3991 WINPR_ASSERT(call);
3992 wLog* log = scard_log();
3993
3994 LONG status = smartcard_unpack_redir_scard_context(log, s, &(call->handles.hContext), &index,
3995 &pbContextNdrPtr);
3996 if (status != SCARD_S_SUCCESS)
3997 return status;
3998 status = smartcard_unpack_redir_scard_handle(log, s, &(call->handles.hCard), &index);
3999 if (status != SCARD_S_SUCCESS)
4000 return status;
4001
4002 if (!Stream_CheckAndLogRequiredLengthWLog(log, s, 12))
4003 return STATUS_BUFFER_TOO_SMALL;
4004 Stream_Read_UINT32(s, call->dwAttrId);
4005 Stream_Read_UINT32(s, call->cbAttrLen);
4006 if (call->cbAttrLen > 65536)
4007 {
4008 WLog_Print(log, WLOG_ERROR, "2.2.2.22 SetAttrib_Call call->cbAttrLen (%" PRIu32 ") > 65536",
4009 call->cbAttrLen);
4010 return SCARD_E_INVALID_PARAMETER;
4011 }
4012
4013 if (!smartcard_ndr_pointer_read(log, s, &index, &ndrPtr))
4014 return ERROR_INVALID_DATA;
4015
4016 status = smartcard_unpack_redir_scard_context_ref(log, s, pbContextNdrPtr,
4017 &(call->handles.hContext));
4018 if (status != SCARD_S_SUCCESS)
4019 return status;
4020
4021 status = smartcard_unpack_redir_scard_handle_ref(log, s, &(call->handles.hCard));
4022 if (status != SCARD_S_SUCCESS)
4023 return status;
4024
4025 if (ndrPtr)
4026 {
4027 size_t len = 0;
4028 status = smartcard_ndr_read_ex(log, s, &call->pbAttr, 0, 1, NDR_PTR_SIMPLE, &len);
4029 if (status != SCARD_S_SUCCESS)
4030 return status;
4031 if (call->cbAttrLen > len)
4032 call->cbAttrLen = WINPR_ASSERTING_INT_CAST(DWORD, len);
4033 }
4034 else
4035 call->cbAttrLen = 0;
4036 smartcard_trace_set_attrib_call(log, call);
4037 return SCARD_S_SUCCESS;
4038}
4039
4040LONG smartcard_pack_set_attrib_call(wStream* s, const SetAttrib_Call* call)
4041{
4042 WINPR_ASSERT(call);
4043 wLog* log = scard_log();
4044 UINT32 index = 0;
4045
4046 smartcard_trace_set_attrib_call(log, call);
4047
4048 LONG status = smartcard_pack_redir_scard_context(log, s, &call->handles.hContext, &index);
4049 if (status != SCARD_S_SUCCESS)
4050 return status;
4051
4052 status = smartcard_pack_redir_scard_handle(log, s, &call->handles.hCard, &index);
4053 if (status != SCARD_S_SUCCESS)
4054 return status;
4055
4056 if (!Stream_EnsureRemainingCapacity(s, 8))
4057 return SCARD_E_NO_MEMORY;
4058
4059 Stream_Write_UINT32(s, call->dwAttrId);
4060 Stream_Write_UINT32(s, call->cbAttrLen);
4061
4062 if (!smartcard_ndr_pointer_write(s, &index, call->cbAttrLen))
4063 return SCARD_E_NO_MEMORY;
4064
4065 status = smartcard_pack_redir_scard_context_ref(log, s, &call->handles.hContext);
4066 if (status != SCARD_S_SUCCESS)
4067 return status;
4068
4069 status = smartcard_pack_redir_scard_handle_ref(log, s, &call->handles.hCard);
4070 if (status != SCARD_S_SUCCESS)
4071 return status;
4072
4073 status = smartcard_ndr_write(s, call->pbAttr, call->cbAttrLen, 1, NDR_PTR_SIMPLE);
4074 if (status != SCARD_S_SUCCESS)
4075 return status;
4076
4077 return SCARD_S_SUCCESS;
4078}
4079
4080LONG smartcard_unpack_locate_cards_by_atr_w_call(wStream* s, LocateCardsByATRW_Call* call)
4081{
4082 UINT32 rgReaderStatesNdrPtr = 0;
4083 UINT32 rgAtrMasksNdrPtr = 0;
4084 UINT32 index = 0;
4085 UINT32 pbContextNdrPtr = 0;
4086
4087 WINPR_ASSERT(call);
4088 wLog* log = scard_log();
4089
4090 call->rgReaderStates = nullptr;
4091
4092 LONG status = smartcard_unpack_redir_scard_context(log, s, &(call->handles.hContext), &index,
4093 &pbContextNdrPtr);
4094 if (status != SCARD_S_SUCCESS)
4095 return status;
4096
4097 if (!Stream_CheckAndLogRequiredLengthWLog(log, s, 16))
4098 return STATUS_BUFFER_TOO_SMALL;
4099
4100 Stream_Read_UINT32(s, call->cAtrs);
4101 if (call->cAtrs > 1000)
4102 {
4103 WLog_Print(log, WLOG_ERROR, "2.2.2.24 LocateCardsByATRW_Call cAtrs(%" PRIu32 ") > 1000",
4104 call->cAtrs);
4105 return SCARD_E_INVALID_PARAMETER;
4106 }
4107
4108 if (!smartcard_ndr_pointer_read(log, s, &index, &rgAtrMasksNdrPtr))
4109 return ERROR_INVALID_DATA;
4110
4111 const UINT32 cReaders = Stream_Get_UINT32(s); /* cReaders (4 bytes) */
4112 if (cReaders > 10)
4113 {
4114 WLog_Print(log, WLOG_ERROR, "2.2.2.24 LocateCardsByATRW_Call cReaders(%" PRIu32 ") > 10",
4115 cReaders);
4116 return SCARD_E_INVALID_PARAMETER;
4117 }
4118 if (!smartcard_ndr_pointer_read(log, s, &index, &rgReaderStatesNdrPtr))
4119 return ERROR_INVALID_DATA;
4120
4121 status = smartcard_unpack_redir_scard_context_ref(log, s, pbContextNdrPtr,
4122 &(call->handles.hContext));
4123 if (status != SCARD_S_SUCCESS)
4124 return status;
4125
4126 if ((rgAtrMasksNdrPtr && !call->cAtrs) || (!rgAtrMasksNdrPtr && call->cAtrs))
4127 {
4128 WLog_Print(log, WLOG_WARN,
4129 "LocateCardsByATRW_Call rgAtrMasksNdrPtr (0x%08" PRIX32
4130 ") and cAtrs (0x%08" PRIX32 ") inconsistency",
4131 rgAtrMasksNdrPtr, call->cAtrs);
4132 return STATUS_INVALID_PARAMETER;
4133 }
4134
4135 if (rgAtrMasksNdrPtr)
4136 {
4137 status = smartcard_ndr_read_atrmask(log, s, &call->rgAtrMasks, call->cAtrs, NDR_PTR_SIMPLE);
4138 if (status != SCARD_S_SUCCESS)
4139 return status;
4140 }
4141
4142 if (rgReaderStatesNdrPtr)
4143 {
4144 status = smartcard_unpack_reader_state_w(log, s, &call->rgReaderStates, cReaders, &index);
4145 if (status != SCARD_S_SUCCESS)
4146 return status;
4147 call->cReaders = cReaders;
4148 }
4149
4150 smartcard_trace_locate_cards_by_atr_w_call(log, call);
4151 return SCARD_S_SUCCESS;
4152}
4153
4154LONG smartcard_unpack_read_cache_a_call(wStream* s, ReadCacheA_Call* call)
4155{
4156 UINT32 mszNdrPtr = 0;
4157 UINT32 contextNdrPtr = 0;
4158 UINT32 index = 0;
4159 UINT32 pbContextNdrPtr = 0;
4160
4161 WINPR_ASSERT(call);
4162 wLog* log = scard_log();
4163
4164 if (!smartcard_ndr_pointer_read(log, s, &index, &mszNdrPtr))
4165 return ERROR_INVALID_DATA;
4166
4167 LONG status = smartcard_unpack_redir_scard_context(log, s, &(call->Common.handles.hContext),
4168 &index, &pbContextNdrPtr);
4169 if (status != SCARD_S_SUCCESS)
4170 return status;
4171
4172 if (!smartcard_ndr_pointer_read(log, s, &index, &contextNdrPtr))
4173 return ERROR_INVALID_DATA;
4174
4175 if (!Stream_CheckAndLogRequiredLengthWLog(log, s, 12))
4176 return STATUS_BUFFER_TOO_SMALL;
4177 Stream_Read_UINT32(s, call->Common.FreshnessCounter);
4178 Stream_Read_INT32(s, call->Common.fPbDataIsNULL);
4179 Stream_Read_UINT32(s, call->Common.cbDataLen);
4180
4181 call->szLookupName = nullptr;
4182 if (mszNdrPtr)
4183 {
4184 status = smartcard_ndr_read_a(log, s, &call->szLookupName, NDR_PTR_FULL);
4185 if (status != SCARD_S_SUCCESS)
4186 return status;
4187 }
4188
4189 status = smartcard_unpack_redir_scard_context_ref(log, s, pbContextNdrPtr,
4190 &call->Common.handles.hContext);
4191 if (status != SCARD_S_SUCCESS)
4192 return status;
4193
4194 if (contextNdrPtr)
4195 {
4196 status = smartcard_ndr_read_u(log, s, &call->Common.CardIdentifier);
4197 if (status != SCARD_S_SUCCESS)
4198 return status;
4199 }
4200 smartcard_trace_read_cache_a_call(log, call);
4201 return SCARD_S_SUCCESS;
4202}
4203
4204LONG smartcard_unpack_read_cache_w_call(wStream* s, ReadCacheW_Call* call)
4205{
4206 UINT32 mszNdrPtr = 0;
4207 UINT32 contextNdrPtr = 0;
4208 UINT32 index = 0;
4209 UINT32 pbContextNdrPtr = 0;
4210
4211 WINPR_ASSERT(call);
4212 wLog* log = scard_log();
4213
4214 if (!smartcard_ndr_pointer_read(log, s, &index, &mszNdrPtr))
4215 return ERROR_INVALID_DATA;
4216
4217 LONG status = smartcard_unpack_redir_scard_context(log, s, &(call->Common.handles.hContext),
4218 &index, &pbContextNdrPtr);
4219 if (status != SCARD_S_SUCCESS)
4220 return status;
4221
4222 if (!smartcard_ndr_pointer_read(log, s, &index, &contextNdrPtr))
4223 return ERROR_INVALID_DATA;
4224
4225 if (!Stream_CheckAndLogRequiredLengthWLog(log, s, 12))
4226 return STATUS_BUFFER_TOO_SMALL;
4227 Stream_Read_UINT32(s, call->Common.FreshnessCounter);
4228 Stream_Read_INT32(s, call->Common.fPbDataIsNULL);
4229 Stream_Read_UINT32(s, call->Common.cbDataLen);
4230
4231 call->szLookupName = nullptr;
4232 if (mszNdrPtr)
4233 {
4234 status = smartcard_ndr_read_w(log, s, &call->szLookupName, NDR_PTR_FULL);
4235 if (status != SCARD_S_SUCCESS)
4236 return status;
4237 }
4238
4239 status = smartcard_unpack_redir_scard_context_ref(log, s, pbContextNdrPtr,
4240 &call->Common.handles.hContext);
4241 if (status != SCARD_S_SUCCESS)
4242 return status;
4243
4244 if (contextNdrPtr)
4245 {
4246 status = smartcard_ndr_read_u(log, s, &call->Common.CardIdentifier);
4247 if (status != SCARD_S_SUCCESS)
4248 return status;
4249 }
4250 smartcard_trace_read_cache_w_call(log, call);
4251 return SCARD_S_SUCCESS;
4252}
4253
4254LONG smartcard_unpack_write_cache_a_call(wStream* s, WriteCacheA_Call* call)
4255{
4256 UINT32 mszNdrPtr = 0;
4257 UINT32 contextNdrPtr = 0;
4258 UINT32 pbDataNdrPtr = 0;
4259 UINT32 index = 0;
4260 UINT32 pbContextNdrPtr = 0;
4261
4262 WINPR_ASSERT(call);
4263 wLog* log = scard_log();
4264
4265 if (!smartcard_ndr_pointer_read(log, s, &index, &mszNdrPtr))
4266 return ERROR_INVALID_DATA;
4267
4268 LONG status = smartcard_unpack_redir_scard_context(log, s, &(call->Common.handles.hContext),
4269 &index, &pbContextNdrPtr);
4270 if (status != SCARD_S_SUCCESS)
4271 return status;
4272
4273 if (!smartcard_ndr_pointer_read(log, s, &index, &contextNdrPtr))
4274 return ERROR_INVALID_DATA;
4275
4276 if (!Stream_CheckAndLogRequiredLengthWLog(log, s, 8))
4277 return STATUS_BUFFER_TOO_SMALL;
4278
4279 Stream_Read_UINT32(s, call->Common.FreshnessCounter);
4280 const UINT32 cbDataLen = Stream_Get_UINT32(s);
4281 if (cbDataLen > 65536)
4282 {
4283 WLog_Print(log, WLOG_ERROR, "2.2.1.10 WriteCache_Common cbDataLen(%" PRIu32 ") > 65536",
4284 cbDataLen);
4285 return ERROR_INVALID_DATA;
4286 }
4287
4288 if (!smartcard_ndr_pointer_read(log, s, &index, &pbDataNdrPtr))
4289 return ERROR_INVALID_DATA;
4290
4291 call->szLookupName = nullptr;
4292 if (mszNdrPtr)
4293 {
4294 status = smartcard_ndr_read_a(log, s, &call->szLookupName, NDR_PTR_FULL);
4295 if (status != SCARD_S_SUCCESS)
4296 return status;
4297 }
4298
4299 status = smartcard_unpack_redir_scard_context_ref(log, s, pbContextNdrPtr,
4300 &call->Common.handles.hContext);
4301 if (status != SCARD_S_SUCCESS)
4302 return status;
4303
4304 call->Common.CardIdentifier = nullptr;
4305 if (contextNdrPtr)
4306 {
4307 status = smartcard_ndr_read_u(log, s, &call->Common.CardIdentifier);
4308 if (status != SCARD_S_SUCCESS)
4309 return status;
4310 }
4311
4312 call->Common.pbData = nullptr;
4313 if (pbDataNdrPtr)
4314 {
4315 status = smartcard_ndr_read(log, s, &call->Common.pbData, cbDataLen, 1, NDR_PTR_SIMPLE);
4316 if (status != SCARD_S_SUCCESS)
4317 return status;
4318 call->Common.cbDataLen = cbDataLen;
4319 }
4320 smartcard_trace_write_cache_a_call(log, call);
4321 return SCARD_S_SUCCESS;
4322}
4323
4324LONG smartcard_unpack_write_cache_w_call(wStream* s, WriteCacheW_Call* call)
4325{
4326 UINT32 mszNdrPtr = 0;
4327 UINT32 contextNdrPtr = 0;
4328 UINT32 pbDataNdrPtr = 0;
4329 UINT32 index = 0;
4330 UINT32 pbContextNdrPtr = 0;
4331
4332 WINPR_ASSERT(call);
4333 wLog* log = scard_log();
4334
4335 if (!smartcard_ndr_pointer_read(log, s, &index, &mszNdrPtr))
4336 return ERROR_INVALID_DATA;
4337
4338 LONG status = smartcard_unpack_redir_scard_context(log, s, &(call->Common.handles.hContext),
4339 &index, &pbContextNdrPtr);
4340 if (status != SCARD_S_SUCCESS)
4341 return status;
4342
4343 if (!smartcard_ndr_pointer_read(log, s, &index, &contextNdrPtr))
4344 return ERROR_INVALID_DATA;
4345
4346 if (!Stream_CheckAndLogRequiredLengthWLog(log, s, 8))
4347 return STATUS_BUFFER_TOO_SMALL;
4348 Stream_Read_UINT32(s, call->Common.FreshnessCounter);
4349 const UINT32 cbDataLen = Stream_Get_UINT32(s);
4350
4351 if (cbDataLen > 65536)
4352 {
4353 WLog_Print(log, WLOG_ERROR, "2.2.1.10 WriteCache_Common cbDataLen(%" PRIu32 ") > 65536",
4354 cbDataLen);
4355 return ERROR_INVALID_DATA;
4356 }
4357
4358 if (!smartcard_ndr_pointer_read(log, s, &index, &pbDataNdrPtr))
4359 return ERROR_INVALID_DATA;
4360
4361 call->szLookupName = nullptr;
4362 if (mszNdrPtr)
4363 {
4364 status = smartcard_ndr_read_w(log, s, &call->szLookupName, NDR_PTR_FULL);
4365 if (status != SCARD_S_SUCCESS)
4366 return status;
4367 }
4368
4369 status = smartcard_unpack_redir_scard_context_ref(log, s, pbContextNdrPtr,
4370 &call->Common.handles.hContext);
4371 if (status != SCARD_S_SUCCESS)
4372 return status;
4373
4374 call->Common.CardIdentifier = nullptr;
4375 if (contextNdrPtr)
4376 {
4377 status = smartcard_ndr_read_u(log, s, &call->Common.CardIdentifier);
4378 if (status != SCARD_S_SUCCESS)
4379 return status;
4380 }
4381
4382 call->Common.pbData = nullptr;
4383 if (pbDataNdrPtr)
4384 {
4385 status = smartcard_ndr_read(log, s, &call->Common.pbData, cbDataLen, 1, NDR_PTR_SIMPLE);
4386 if (status != SCARD_S_SUCCESS)
4387 return status;
4388 call->Common.cbDataLen = cbDataLen;
4389 }
4390 smartcard_trace_write_cache_w_call(log, call);
4391 return status;
4392}
4393
4394LONG smartcard_unpack_get_transmit_count_call(wStream* s, GetTransmitCount_Call* call)
4395{
4396 WINPR_ASSERT(call);
4397 wLog* log = scard_log();
4398
4399 UINT32 index = 0;
4400 UINT32 pbContextNdrPtr = 0;
4401
4402 LONG status = smartcard_unpack_redir_scard_context(log, s, &(call->handles.hContext), &index,
4403 &pbContextNdrPtr);
4404 if (status != SCARD_S_SUCCESS)
4405 return status;
4406
4407 status = smartcard_unpack_redir_scard_handle(log, s, &(call->handles.hCard), &index);
4408 if (status != SCARD_S_SUCCESS)
4409 return status;
4410
4411 status = smartcard_unpack_redir_scard_context_ref(log, s, pbContextNdrPtr,
4412 &(call->handles.hContext));
4413 if (status != SCARD_S_SUCCESS)
4414 {
4415 WLog_Print(log, WLOG_ERROR,
4416 "smartcard_unpack_redir_scard_context_ref failed with error %" PRId32 "",
4417 status);
4418 return status;
4419 }
4420
4421 status = smartcard_unpack_redir_scard_handle_ref(log, s, &(call->handles.hCard));
4422 if (status != SCARD_S_SUCCESS)
4423 WLog_Print(log, WLOG_ERROR,
4424 "smartcard_unpack_redir_scard_handle_ref failed with error %" PRId32 "", status);
4425
4426 smartcard_trace_get_transmit_count_call(log, call);
4427 return status;
4428}
4429
4430LONG smartcard_unpack_get_reader_icon_call(wStream* s, GetReaderIcon_Call* call)
4431{
4432 WINPR_ASSERT(call);
4433 wLog* log = scard_log();
4434 return smartcard_unpack_common_context_and_string_w(log, s, &call->handles.hContext,
4435 &call->szReaderName);
4436}
4437
4438LONG smartcard_unpack_context_and_string_a_call(wStream* s, ContextAndStringA_Call* call)
4439{
4440 WINPR_ASSERT(call);
4441 wLog* log = scard_log();
4442 return smartcard_unpack_common_context_and_string_a(log, s, &call->handles.hContext, &call->sz);
4443}
4444
4445LONG smartcard_unpack_context_and_string_w_call(wStream* s, ContextAndStringW_Call* call)
4446{
4447 WINPR_ASSERT(call);
4448 wLog* log = scard_log();
4449 return smartcard_unpack_common_context_and_string_w(log, s, &call->handles.hContext, &call->sz);
4450}
4451
4452LONG smartcard_unpack_get_device_type_id_call(wStream* s, GetDeviceTypeId_Call* call)
4453{
4454 WINPR_ASSERT(call);
4455 wLog* log = scard_log();
4456 return smartcard_unpack_common_context_and_string_w(log, s, &call->handles.hContext,
4457 &call->szReaderName);
4458}
4459
4460LONG smartcard_pack_device_type_id_return(wStream* s, const GetDeviceTypeId_Return* ret)
4461{
4462 WINPR_ASSERT(ret);
4463 wLog* log = scard_log();
4464 smartcard_trace_device_type_id_return(log, ret);
4465
4466 if (!Stream_EnsureRemainingCapacity(s, 4))
4467 {
4468 WLog_Print(log, WLOG_ERROR, "Stream_EnsureRemainingCapacity failed!");
4469 return SCARD_F_INTERNAL_ERROR;
4470 }
4471
4472 Stream_Write_UINT32(s, ret->dwDeviceId); /* cBytes (4 bytes) */
4473
4474 return ret->ReturnCode;
4475}
4476
4477LONG smartcard_pack_locate_cards_return(wStream* s, const LocateCards_Return* ret)
4478{
4479 WINPR_ASSERT(ret);
4480 wLog* log = scard_log();
4481
4482 LONG status = 0;
4483 UINT32 cbDataLen = ret->cReaders;
4484 UINT32 index = 0;
4485
4486 smartcard_trace_locate_cards_return(log, ret);
4487 if (ret->ReturnCode != SCARD_S_SUCCESS)
4488 cbDataLen = 0;
4489 if (cbDataLen == SCARD_AUTOALLOCATE)
4490 cbDataLen = 0;
4491
4492 if (!Stream_EnsureRemainingCapacity(s, 4))
4493 {
4494 WLog_Print(log, WLOG_ERROR, "Stream_EnsureRemainingCapacity failed!");
4495 return SCARD_F_INTERNAL_ERROR;
4496 }
4497
4498 if (ret->cReaders > 10)
4499 {
4500 WLog_Print(log, WLOG_ERROR,
4501 "2.2.3.5 LocateCards_Return and GetStatusChange_Return cReaders(%" PRIu32
4502 ") > 10",
4503 ret->cReaders);
4504 return SCARD_E_INVALID_PARAMETER;
4505 }
4506
4507 Stream_Write_UINT32(s, cbDataLen); /* cBytes (4 cbDataLen) */
4508 if (!smartcard_ndr_pointer_write(s, &index, cbDataLen))
4509 return SCARD_E_NO_MEMORY;
4510
4511 status = smartcard_ndr_write_state(s, ret->rgReaderStates, cbDataLen, NDR_PTR_SIMPLE);
4512 if (status != SCARD_S_SUCCESS)
4513 return status;
4514 return ret->ReturnCode;
4515}
4516
4517LONG smartcard_pack_get_reader_icon_return(wStream* s, const GetReaderIcon_Return* ret)
4518{
4519 WINPR_ASSERT(ret);
4520 wLog* log = scard_log();
4521
4522 LONG status = 0;
4523 UINT32 index = 0;
4524 UINT32 cbDataLen = ret->cbDataLen;
4525 smartcard_trace_get_reader_icon_return(log, ret);
4526 if (ret->ReturnCode != SCARD_S_SUCCESS)
4527 cbDataLen = 0;
4528 if (cbDataLen == SCARD_AUTOALLOCATE)
4529 cbDataLen = 0;
4530
4531 if (!Stream_EnsureRemainingCapacity(s, 4))
4532 {
4533 WLog_Print(log, WLOG_ERROR, "Stream_EnsureRemainingCapacity failed!");
4534 return SCARD_F_INTERNAL_ERROR;
4535 }
4536
4537 Stream_Write_UINT32(s, cbDataLen); /* cBytes (4 cbDataLen) */
4538 if (!smartcard_ndr_pointer_write(s, &index, cbDataLen))
4539 return SCARD_E_NO_MEMORY;
4540
4541 status = smartcard_ndr_write(s, ret->pbData, cbDataLen, 1, NDR_PTR_SIMPLE);
4542 if (status != SCARD_S_SUCCESS)
4543 return status;
4544 return ret->ReturnCode;
4545}
4546
4547LONG smartcard_pack_get_transmit_count_return(wStream* s, const GetTransmitCount_Return* ret)
4548{
4549 WINPR_ASSERT(ret);
4550 wLog* log = scard_log();
4551
4552 smartcard_trace_get_transmit_count_return(log, ret);
4553
4554 if (!Stream_EnsureRemainingCapacity(s, 4))
4555 {
4556 WLog_Print(log, WLOG_ERROR, "Stream_EnsureRemainingCapacity failed!");
4557 return SCARD_F_INTERNAL_ERROR;
4558 }
4559
4560 Stream_Write_UINT32(s, ret->cTransmitCount); /* cBytes (4 cbDataLen) */
4561
4562 return ret->ReturnCode;
4563}
4564
4565LONG smartcard_pack_read_cache_return(wStream* s, const ReadCache_Return* ret)
4566{
4567 WINPR_ASSERT(ret);
4568 wLog* log = scard_log();
4569
4570 LONG status = 0;
4571 UINT32 index = 0;
4572 UINT32 cbDataLen = ret->cbDataLen;
4573 smartcard_trace_read_cache_return(log, ret);
4574 if (ret->ReturnCode != SCARD_S_SUCCESS)
4575 cbDataLen = 0;
4576
4577 if (cbDataLen == SCARD_AUTOALLOCATE)
4578 cbDataLen = 0;
4579
4580 if (!Stream_EnsureRemainingCapacity(s, 4))
4581 {
4582 WLog_Print(log, WLOG_ERROR, "Stream_EnsureRemainingCapacity failed!");
4583 return SCARD_F_INTERNAL_ERROR;
4584 }
4585
4586 if (cbDataLen > 65536)
4587 {
4588 WLog_Print(log, WLOG_ERROR, "2.2.3.1 ReadCache_Return cbDataLen (%" PRIu32 ") > 65536",
4589 cbDataLen);
4590 return SCARD_E_INVALID_PARAMETER;
4591 }
4592
4593 Stream_Write_UINT32(s, cbDataLen); /* cBytes (4 cbDataLen) */
4594 if (!smartcard_ndr_pointer_write(s, &index, cbDataLen))
4595 return SCARD_E_NO_MEMORY;
4596
4597 status = smartcard_ndr_write(s, ret->pbData, cbDataLen, 1, NDR_PTR_SIMPLE);
4598 if (status != SCARD_S_SUCCESS)
4599 return status;
4600 return ret->ReturnCode;
4601}
4602
4603LONG smartcard_pack_context_call(wStream* s, const Context_Call* call, const char* name)
4604{
4605 WINPR_ASSERT(call);
4606 wLog* log = scard_log();
4607 UINT32 index = 0;
4608
4609 smartcard_trace_context_call(log, call, name);
4610
4611 LONG status = smartcard_pack_redir_scard_context(log, s, &call->handles.hContext, &index);
4612 if (status != SCARD_S_SUCCESS)
4613 return status;
4614
4615 return smartcard_pack_redir_scard_context_ref(log, s, &call->handles.hContext);
4616}
4617
4618LONG smartcard_pack_list_readers_call(wStream* s, const ListReaders_Call* call, BOOL unicode)
4619{
4620 WINPR_ASSERT(call);
4621 wLog* log = scard_log();
4622 UINT32 index = 0;
4623
4624 smartcard_trace_list_readers_call(log, call, unicode);
4625
4626 LONG status = smartcard_pack_redir_scard_context(log, s, &call->handles.hContext, &index);
4627 if (status != SCARD_S_SUCCESS)
4628 return status;
4629
4630 if (!Stream_EnsureRemainingCapacity(s, 4))
4631 return SCARD_E_NO_MEMORY;
4632
4633 Stream_Write_UINT32(s, call->cBytes);
4634 if (call->cBytes > 65536)
4635 {
4636 WLog_Print(log, WLOG_ERROR, "2.2.2.4 ListReaders_Call cBytes(%" PRIu32 ") > 65536",
4637 call->cBytes);
4638 return SCARD_E_INVALID_PARAMETER;
4639 }
4640
4641 if (!smartcard_ndr_pointer_write(s, &index, call->cBytes))
4642 return SCARD_E_NO_MEMORY;
4643
4644 if (!Stream_EnsureRemainingCapacity(s, 8))
4645 return SCARD_E_NO_MEMORY;
4646
4647 Stream_Write_INT32(s, call->fmszReadersIsNULL);
4648 Stream_Write_UINT32(s, call->cchReaders);
4649
4650 status = smartcard_pack_redir_scard_context_ref(log, s, &call->handles.hContext);
4651 if (status != SCARD_S_SUCCESS)
4652 return status;
4653
4654 if (call->cBytes > 0)
4655 {
4656 status = smartcard_ndr_write(s, call->mszGroups, call->cBytes, 1, NDR_PTR_SIMPLE);
4657 if (status != SCARD_S_SUCCESS)
4658 return status;
4659 }
4660
4661 return SCARD_S_SUCCESS;
4662}
4663
4664WINPR_ATTR_NODISCARD static LONG
4665smartcard_pack_reader_state_a(wLog* log, wStream* s, const LPSCARD_READERSTATEA rgReaderStates,
4666 UINT32 cReaders, UINT32* ptrIndex)
4667{
4668 WINPR_ASSERT(rgReaderStates || (cReaders == 0));
4669
4670 if (!Stream_EnsureRemainingCapacity(s, 4))
4671 return SCARD_E_NO_MEMORY;
4672
4673 Stream_Write_UINT32(s, cReaders);
4674
4675 for (UINT32 i = 0; i < cReaders; i++)
4676 {
4677 const SCARD_READERSTATEA* state = &rgReaderStates[i];
4678
4679 if (!Stream_EnsureRemainingCapacity(s, 52))
4680 return SCARD_E_NO_MEMORY;
4681
4682 const UINT32 nameLen = state->szReader ? (UINT32)(strlen(state->szReader) + 1) : 0;
4683 if (!smartcard_ndr_pointer_write(s, ptrIndex, nameLen))
4684 return SCARD_E_NO_MEMORY;
4685
4686 Stream_Write_UINT32(s, state->dwCurrentState);
4687 Stream_Write_UINT32(s, state->dwEventState);
4688 if (state->cbAtr > 36)
4689 {
4690 WLog_Print(log, WLOG_WARN, "2.2.2.12 GetStatusChangeW_Call cbAtr(%" PRIu32 ") > 36",
4691 state->cbAtr);
4692 return SCARD_E_INVALID_PARAMETER;
4693 }
4694 Stream_Write_UINT32(s, state->cbAtr);
4695 Stream_Write(s, state->rgbAtr, 36);
4696 }
4697
4698 for (UINT32 i = 0; i < cReaders; i++)
4699 {
4700 const SCARD_READERSTATEA* state = &rgReaderStates[i];
4701
4702 if (state->szReader)
4703 {
4704 const UINT32 nameLen = (UINT32)(strlen(state->szReader) + 1);
4705 LONG status = smartcard_ndr_write_a(s, state->szReader, nameLen, NDR_PTR_FULL);
4706 if (status != SCARD_S_SUCCESS)
4707 return status;
4708 }
4709 }
4710
4711 return SCARD_S_SUCCESS;
4712}
4713
4714WINPR_ATTR_NODISCARD static LONG
4715smartcard_pack_reader_state_w(wLog* log, wStream* s, const LPSCARD_READERSTATEW rgReaderStates,
4716 UINT32 cReaders, UINT32* ptrIndex)
4717{
4718 WINPR_ASSERT(rgReaderStates || (cReaders == 0));
4719
4720 if (!Stream_EnsureRemainingCapacity(s, 4))
4721 return SCARD_E_NO_MEMORY;
4722
4723 Stream_Write_UINT32(s, cReaders);
4724
4725 for (UINT32 i = 0; i < cReaders; i++)
4726 {
4727 const SCARD_READERSTATEW* state = &rgReaderStates[i];
4728
4729 if (!Stream_EnsureRemainingCapacity(s, 52))
4730 return SCARD_E_NO_MEMORY;
4731
4732 const UINT32 nameLen = state->szReader ? (UINT32)(_wcslen(state->szReader) + 1) : 0;
4733 if (!smartcard_ndr_pointer_write(s, ptrIndex, nameLen))
4734 return SCARD_E_NO_MEMORY;
4735
4736 Stream_Write_UINT32(s, state->dwCurrentState);
4737 Stream_Write_UINT32(s, state->dwEventState);
4738 if (state->cbAtr > 36)
4739 {
4740 WLog_Print(log, WLOG_WARN, "2.2.2.12 GetStatusChangeW_Call cbAtr(%" PRIu32 ") > 36",
4741 state->cbAtr);
4742 return SCARD_E_INVALID_PARAMETER;
4743 }
4744 Stream_Write_UINT32(s, state->cbAtr);
4745 Stream_Write(s, state->rgbAtr, 36);
4746 }
4747
4748 for (UINT32 i = 0; i < cReaders; i++)
4749 {
4750 const SCARD_READERSTATEW* state = &rgReaderStates[i];
4751
4752 if (state->szReader)
4753 {
4754 const UINT32 nameLen = (UINT32)(_wcslen(state->szReader) + 1);
4755 LONG status = smartcard_ndr_write_w(s, state->szReader, nameLen, NDR_PTR_FULL);
4756 if (status != SCARD_S_SUCCESS)
4757 return status;
4758 }
4759 }
4760
4761 return SCARD_S_SUCCESS;
4762}
4763
4764LONG smartcard_pack_get_status_change_a_call(wStream* s, const GetStatusChangeA_Call* call)
4765{
4766 WINPR_ASSERT(call);
4767 wLog* log = scard_log();
4768 UINT32 index = 0;
4769
4770 smartcard_trace_get_status_change_a_call(log, call);
4771
4772 LONG status = smartcard_pack_redir_scard_context(log, s, &call->handles.hContext, &index);
4773 if (status != SCARD_S_SUCCESS)
4774 return status;
4775
4776 if (!Stream_EnsureRemainingCapacity(s, 8))
4777 return SCARD_E_NO_MEMORY;
4778
4779 Stream_Write_UINT32(s, call->dwTimeOut);
4780 Stream_Write_UINT32(s, call->cReaders);
4781 if (call->cReaders > 11)
4782 {
4783 WLog_Print(log, WLOG_ERROR, "2.2.2.11 GetStatusChangeA_Call cReaders (%" PRIu32 ") > 11",
4784 call->cReaders);
4785 return SCARD_E_INVALID_PARAMETER;
4786 }
4787 if (!smartcard_ndr_pointer_write(s, &index, call->cReaders))
4788 return SCARD_E_NO_MEMORY;
4789
4790 status = smartcard_pack_redir_scard_context_ref(log, s, &call->handles.hContext);
4791 if (status != SCARD_S_SUCCESS)
4792 return status;
4793
4794 if (call->cReaders > 0)
4795 {
4796 status =
4797 smartcard_pack_reader_state_a(log, s, call->rgReaderStates, call->cReaders, &index);
4798 if (status != SCARD_S_SUCCESS)
4799 return status;
4800 }
4801
4802 return SCARD_S_SUCCESS;
4803}
4804
4805LONG smartcard_pack_get_status_change_w_call(wStream* s, const GetStatusChangeW_Call* call)
4806{
4807 WINPR_ASSERT(call);
4808 wLog* log = scard_log();
4809 UINT32 index = 0;
4810
4811 smartcard_trace_get_status_change_w_call(log, call);
4812
4813 LONG status = smartcard_pack_redir_scard_context(log, s, &call->handles.hContext, &index);
4814 if (status != SCARD_S_SUCCESS)
4815 return status;
4816
4817 if (!Stream_EnsureRemainingCapacity(s, 8))
4818 return SCARD_E_NO_MEMORY;
4819
4820 Stream_Write_UINT32(s, call->dwTimeOut);
4821 Stream_Write_UINT32(s, call->cReaders);
4822 if (call->cReaders > 11)
4823 {
4824 WLog_Print(log, WLOG_ERROR, "2.2.2.12 GetStatusChangeW_Call cReaders (%" PRIu32 ") > 11",
4825 call->cReaders);
4826 return SCARD_E_INVALID_PARAMETER;
4827 }
4828
4829 if (!smartcard_ndr_pointer_write(s, &index, call->cReaders))
4830 return SCARD_E_NO_MEMORY;
4831
4832 status = smartcard_pack_redir_scard_context_ref(log, s, &call->handles.hContext);
4833 if (status != SCARD_S_SUCCESS)
4834 return status;
4835
4836 if (call->cReaders > 0)
4837 {
4838 status =
4839 smartcard_pack_reader_state_w(log, s, call->rgReaderStates, call->cReaders, &index);
4840 if (status != SCARD_S_SUCCESS)
4841 return status;
4842 }
4843
4844 return SCARD_S_SUCCESS;
4845}
4846
4847LONG smartcard_pack_connect_a_call(wStream* s, const ConnectA_Call* call)
4848{
4849 WINPR_ASSERT(call);
4850 wLog* log = scard_log();
4851 UINT32 index = 0;
4852
4853 smartcard_trace_connect_a_call(log, call);
4854
4855 const UINT32 readerLen = call->szReader ? (UINT32)(strlen(call->szReader) + 1) : 0;
4856 if (!smartcard_ndr_pointer_write(s, &index, readerLen))
4857 return SCARD_E_NO_MEMORY;
4858
4859 LONG status =
4860 smartcard_pack_redir_scard_context(log, s, &call->Common.handles.hContext, &index);
4861 if (status != SCARD_S_SUCCESS)
4862 return status;
4863
4864 if (!Stream_EnsureRemainingCapacity(s, 8))
4865 return SCARD_E_NO_MEMORY;
4866
4867 Stream_Write_UINT32(s, call->Common.dwShareMode);
4868 Stream_Write_UINT32(s, call->Common.dwPreferredProtocols);
4869
4870 if (call->szReader)
4871 {
4872 status = smartcard_ndr_write_a(s, call->szReader, readerLen, NDR_PTR_FULL);
4873 if (status != SCARD_S_SUCCESS)
4874 return status;
4875 }
4876
4877 return smartcard_pack_redir_scard_context_ref(log, s, &call->Common.handles.hContext);
4878}
4879
4880LONG smartcard_pack_connect_w_call(wStream* s, const ConnectW_Call* call)
4881{
4882 WINPR_ASSERT(call);
4883 wLog* log = scard_log();
4884 UINT32 index = 0;
4885
4886 smartcard_trace_connect_w_call(log, call);
4887
4888 const UINT32 readerLen = call->szReader ? (UINT32)(_wcslen(call->szReader) + 1) : 0;
4889 if (!smartcard_ndr_pointer_write(s, &index, readerLen))
4890 return SCARD_E_NO_MEMORY;
4891
4892 LONG status =
4893 smartcard_pack_redir_scard_context(log, s, &call->Common.handles.hContext, &index);
4894 if (status != SCARD_S_SUCCESS)
4895 return status;
4896
4897 if (!Stream_EnsureRemainingCapacity(s, 8))
4898 return SCARD_E_NO_MEMORY;
4899
4900 Stream_Write_UINT32(s, call->Common.dwShareMode);
4901 Stream_Write_UINT32(s, call->Common.dwPreferredProtocols);
4902
4903 if (call->szReader)
4904 {
4905 status = smartcard_ndr_write_w(s, call->szReader, readerLen, NDR_PTR_FULL);
4906 if (status != SCARD_S_SUCCESS)
4907 return status;
4908 }
4909
4910 return smartcard_pack_redir_scard_context_ref(log, s, &call->Common.handles.hContext);
4911}
4912
4913LONG smartcard_pack_control_call(wStream* s, const Control_Call* call)
4914{
4915 WINPR_ASSERT(call);
4916 wLog* log = scard_log();
4917 UINT32 index = 0;
4918
4919 smartcard_trace_control_call(log, call);
4920
4921 LONG status = smartcard_pack_redir_scard_context(log, s, &call->handles.hContext, &index);
4922 if (status != SCARD_S_SUCCESS)
4923 return status;
4924
4925 status = smartcard_pack_redir_scard_handle(log, s, &call->handles.hCard, &index);
4926 if (status != SCARD_S_SUCCESS)
4927 return status;
4928
4929 if (!Stream_EnsureRemainingCapacity(s, 8))
4930 return SCARD_E_NO_MEMORY;
4931
4932 Stream_Write_UINT32(s, call->dwControlCode);
4933 Stream_Write_UINT32(s, call->cbInBufferSize);
4934 if (!smartcard_ndr_pointer_write(s, &index, call->cbInBufferSize))
4935 return SCARD_E_NO_MEMORY;
4936
4937 Stream_Write_INT32(s, call->fpvOutBufferIsNULL);
4938 Stream_Write_UINT32(s, call->cbOutBufferSize);
4939
4940 status = smartcard_pack_redir_scard_context_ref(log, s, &call->handles.hContext);
4941 if (status != SCARD_S_SUCCESS)
4942 return status;
4943
4944 status = smartcard_pack_redir_scard_handle_ref(log, s, &call->handles.hCard);
4945 if (status != SCARD_S_SUCCESS)
4946 return status;
4947
4948 if (call->cbInBufferSize)
4949 {
4950 status = smartcard_ndr_write(s, call->pvInBuffer, call->cbInBufferSize, 1, NDR_PTR_SIMPLE);
4951 if (status != SCARD_S_SUCCESS)
4952 return status;
4953 }
4954
4955 return status;
4956}
4957
4958LONG smartcard_pack_hcard_and_disposition_call(wStream* s, const HCardAndDisposition_Call* call,
4959 const char* name)
4960{
4961 WINPR_ASSERT(call);
4962 wLog* log = scard_log();
4963 UINT32 index = 0;
4964
4965 smartcard_trace_hcard_and_disposition_call(log, call, name);
4966
4967 LONG status = smartcard_pack_redir_scard_context(log, s, &call->handles.hContext, &index);
4968 if (status != SCARD_S_SUCCESS)
4969 return status;
4970
4971 status = smartcard_pack_redir_scard_handle(log, s, &call->handles.hCard, &index);
4972 if (status != SCARD_S_SUCCESS)
4973 return status;
4974
4975 if (!Stream_EnsureRemainingCapacity(s, 4))
4976 return SCARD_E_NO_MEMORY;
4977
4978 Stream_Write_UINT32(s, call->dwDisposition);
4979
4980 status = smartcard_pack_redir_scard_context_ref(log, s, &call->handles.hContext);
4981 if (status != SCARD_S_SUCCESS)
4982 return status;
4983
4984 return smartcard_pack_redir_scard_handle_ref(log, s, &call->handles.hCard);
4985}
4986
4987LONG smartcard_pack_transmit_call(wStream* s, const Transmit_Call* call)
4988{
4989 WINPR_ASSERT(call);
4990 wLog* log = scard_log();
4991 UINT32 index = 0;
4992
4993 smartcard_trace_transmit_call(log, call);
4994
4995 LONG status = smartcard_pack_redir_scard_context(log, s, &call->handles.hContext, &index);
4996 if (status != SCARD_S_SUCCESS)
4997 return status;
4998
4999 status = smartcard_pack_redir_scard_handle(log, s, &call->handles.hCard, &index);
5000 if (status != SCARD_S_SUCCESS)
5001 return status;
5002
5003 UINT32 cbExtraBytes = 0;
5004 const BYTE* pbExtraBytes = nullptr;
5005 if (call->pioSendPci)
5006 {
5007 cbExtraBytes = (UINT32)(call->pioSendPci->cbPciLength - sizeof(SCARD_IO_REQUEST));
5008 if (cbExtraBytes > 0)
5009 pbExtraBytes = &((const BYTE*)call->pioSendPci)[sizeof(SCARD_IO_REQUEST)];
5010 }
5011
5012 if (!Stream_EnsureRemainingCapacity(s, 32))
5013 return SCARD_E_NO_MEMORY;
5014
5015 Stream_Write_UINT32(s, call->pioSendPci ? call->pioSendPci->dwProtocol : 0);
5016 Stream_Write_UINT32(s, cbExtraBytes);
5017 if (!smartcard_ndr_pointer_write(s, &index, cbExtraBytes))
5018 return SCARD_E_NO_MEMORY;
5019
5020 Stream_Write_UINT32(s, call->cbSendLength);
5021 if (!smartcard_ndr_pointer_write(s, &index, call->cbSendLength))
5022 return SCARD_E_NO_MEMORY;
5023
5024 const UINT32 recvPciLen = call->pioRecvPci ? (UINT32)call->pioRecvPci->cbPciLength : 0;
5025 if (!smartcard_ndr_pointer_write(s, &index, recvPciLen))
5026 return SCARD_E_NO_MEMORY;
5027
5028 Stream_Write_INT32(s, call->fpbRecvBufferIsNULL);
5029 Stream_Write_UINT32(s, call->cbRecvLength);
5030
5031 status = smartcard_pack_redir_scard_context_ref(log, s, &call->handles.hContext);
5032 if (status != SCARD_S_SUCCESS)
5033 return status;
5034
5035 status = smartcard_pack_redir_scard_handle_ref(log, s, &call->handles.hCard);
5036 if (status != SCARD_S_SUCCESS)
5037 return status;
5038
5039 if (cbExtraBytes > 0)
5040 {
5041 status = smartcard_ndr_write(s, pbExtraBytes, cbExtraBytes, 1, NDR_PTR_SIMPLE);
5042 if (status != SCARD_S_SUCCESS)
5043 return status;
5044 }
5045
5046 if (call->cbSendLength > 0)
5047 {
5048 status = smartcard_ndr_write(s, call->pbSendBuffer, call->cbSendLength, 1, NDR_PTR_SIMPLE);
5049 if (status != SCARD_S_SUCCESS)
5050 return status;
5051 }
5052
5053 if (call->pioRecvPci && recvPciLen > 0)
5054 {
5055 UINT32 cbRecvExtra = (UINT32)(call->pioRecvPci->cbPciLength - sizeof(SCARD_IO_REQUEST));
5056 const BYTE* pbRecvExtra = &((const BYTE*)call->pioRecvPci)[sizeof(SCARD_IO_REQUEST)];
5057
5058 if (!Stream_EnsureRemainingCapacity(s, 12))
5059 return SCARD_E_NO_MEMORY;
5060
5061 Stream_Write_UINT32(s, call->pioRecvPci->dwProtocol);
5062 Stream_Write_UINT32(s, cbRecvExtra);
5063 if (!smartcard_ndr_pointer_write(s, &index, cbRecvExtra))
5064 return SCARD_E_NO_MEMORY;
5065
5066 if (cbRecvExtra > 0)
5067 {
5068 status = smartcard_ndr_write(s, pbRecvExtra, cbRecvExtra, 1, NDR_PTR_SIMPLE);
5069 if (status != SCARD_S_SUCCESS)
5070 return status;
5071 }
5072 }
5073
5074 return SCARD_S_SUCCESS;
5075}
5076
5077LONG smartcard_pack_get_attrib_call(wStream* s, const GetAttrib_Call* call)
5078{
5079 WINPR_ASSERT(call);
5080 wLog* log = scard_log();
5081 UINT32 index = 0;
5082
5083 smartcard_trace_get_attrib_call(log, call);
5084
5085 LONG status = smartcard_pack_redir_scard_context(log, s, &call->handles.hContext, &index);
5086 if (status != SCARD_S_SUCCESS)
5087 return status;
5088
5089 status = smartcard_pack_redir_scard_handle(log, s, &call->handles.hCard, &index);
5090 if (status != SCARD_S_SUCCESS)
5091 return status;
5092
5093 if (!Stream_EnsureRemainingCapacity(s, 12))
5094 return STATUS_NO_MEMORY;
5095
5096 Stream_Write_UINT32(s, call->dwAttrId);
5097 Stream_Write_INT32(s, call->fpbAttrIsNULL);
5098 Stream_Write_UINT32(s, call->cbAttrLen);
5099
5100 status = smartcard_pack_redir_scard_context_ref(log, s, &call->handles.hContext);
5101 if (status != SCARD_S_SUCCESS)
5102 return status;
5103
5104 return smartcard_pack_redir_scard_handle_ref(log, s, &call->handles.hCard);
5105}
5106
5107LONG smartcard_unpack_list_readers_return(wStream* s, ListReaders_Return* ret, BOOL unicode)
5108{
5109 WINPR_ASSERT(ret);
5110 wLog* log = scard_log();
5111 UINT32 index = 0;
5112 UINT32 mszNdrPtr = 0;
5113
5114 if (!Stream_CheckAndLogRequiredLengthWLog(log, s, 4))
5115 return STATUS_BUFFER_TOO_SMALL;
5116
5117 const UINT32 cBytes = Stream_Get_UINT32(s);
5118
5119 if (!smartcard_ndr_pointer_read(log, s, &index, &mszNdrPtr))
5120 return ERROR_INVALID_DATA;
5121
5122 if (mszNdrPtr)
5123 {
5124 LONG status = smartcard_ndr_read(log, s, &ret->msz, cBytes, 1, NDR_PTR_SIMPLE);
5125 if (status != SCARD_S_SUCCESS)
5126 return status;
5127 ret->cBytes = cBytes;
5128 }
5129
5130 smartcard_trace_list_readers_return(log, ret, unicode);
5131 return SCARD_S_SUCCESS;
5132}
5133
5134LONG smartcard_unpack_get_status_change_return(wStream* s, GetStatusChange_Return* ret,
5135 BOOL unicode)
5136{
5137 WINPR_ASSERT(ret);
5138 wLog* log = scard_log();
5139 UINT32 index = 0;
5140 UINT32 ndrPtr = 0;
5141
5142 if (!Stream_CheckAndLogRequiredLengthWLog(log, s, 4))
5143 return STATUS_BUFFER_TOO_SMALL;
5144
5145 Stream_Read_UINT32(s, ret->cReaders);
5146 if (ret->cReaders > 10)
5147 {
5148 WLog_Print(log, WLOG_ERROR,
5149 "2.2.3.5 LocateCards_Return and GetStatusChange_Return cReaders(%" PRIu32
5150 ") > 10",
5151 ret->cReaders);
5152 return SCARD_E_INVALID_PARAMETER;
5153 }
5154
5155 if (!smartcard_ndr_pointer_read(log, s, &index, &ndrPtr))
5156 return ERROR_INVALID_DATA;
5157
5158 if (ndrPtr && ret->cReaders > 0)
5159 {
5160 LONG status =
5161 smartcard_ndr_read_state(log, s, &ret->rgReaderStates, ret->cReaders, NDR_PTR_SIMPLE);
5162 if (status != SCARD_S_SUCCESS)
5163 return status;
5164 }
5165 else
5166 ret->rgReaderStates = nullptr;
5167
5168 smartcard_trace_get_status_change_return(log, ret, unicode);
5169 return SCARD_S_SUCCESS;
5170}
5171
5172LONG smartcard_unpack_connect_return(wStream* s, Connect_Return* ret)
5173{
5174 WINPR_ASSERT(ret);
5175 wLog* log = scard_log();
5176 UINT32 index = 0;
5177 UINT32 pbContextNdrPtr = 0;
5178
5179 LONG status =
5180 smartcard_unpack_redir_scard_context(log, s, &ret->hContext, &index, &pbContextNdrPtr);
5181 if (status != SCARD_S_SUCCESS)
5182 return status;
5183
5184 status = smartcard_unpack_redir_scard_handle(log, s, &ret->hCard, &index);
5185 if (status != SCARD_S_SUCCESS)
5186 return status;
5187
5188 if (!Stream_CheckAndLogRequiredLengthWLog(log, s, 4))
5189 return STATUS_BUFFER_TOO_SMALL;
5190
5191 Stream_Read_UINT32(s, ret->dwActiveProtocol);
5192
5193 status = smartcard_unpack_redir_scard_context_ref(log, s, pbContextNdrPtr, &ret->hContext);
5194 if (status != SCARD_S_SUCCESS)
5195 return status;
5196
5197 status = smartcard_unpack_redir_scard_handle_ref(log, s, &ret->hCard);
5198 if (status != SCARD_S_SUCCESS)
5199 return status;
5200
5201 smartcard_trace_connect_return(log, ret);
5202 return SCARD_S_SUCCESS;
5203}
5204
5205LONG smartcard_unpack_control_return(wStream* s, Control_Return* ret)
5206{
5207 WINPR_ASSERT(ret);
5208 wLog* log = scard_log();
5209 UINT32 index = 0;
5210 UINT32 pvOutBufferNdrPtr = 0;
5211
5212 if (!Stream_CheckAndLogRequiredLengthWLog(log, s, 8))
5213 return STATUS_BUFFER_TOO_SMALL;
5214
5215 const UINT32 cbOutBufferSize = Stream_Get_UINT32(s);
5216
5217 if (!smartcard_ndr_pointer_read(log, s, &index, &pvOutBufferNdrPtr))
5218 return ERROR_INVALID_DATA;
5219
5220 if (pvOutBufferNdrPtr && (cbOutBufferSize > 0))
5221 {
5222 LONG status =
5223 smartcard_ndr_read(log, s, &ret->pvOutBuffer, cbOutBufferSize, 1, NDR_PTR_SIMPLE);
5224 if (status != SCARD_S_SUCCESS)
5225 return status;
5226 ret->cbOutBufferSize = cbOutBufferSize;
5227 }
5228 smartcard_trace_control_return(log, ret);
5229 return SCARD_S_SUCCESS;
5230}
5231
5232LONG smartcard_unpack_transmit_return(wStream* s, Transmit_Return* ret)
5233{
5234 WINPR_ASSERT(ret);
5235 wLog* log = scard_log();
5236 UINT32 index = 0;
5237 UINT32 recvPciNdrPtr = 0;
5238 UINT32 recvBufferNdrPtr = 0;
5239
5240 if (!smartcard_ndr_pointer_read(log, s, &index, &recvPciNdrPtr))
5241 return ERROR_INVALID_DATA;
5242
5243 if (!Stream_CheckAndLogRequiredLengthWLog(log, s, 4))
5244 return STATUS_BUFFER_TOO_SMALL;
5245
5246 const UINT32 cbRecvLength = Stream_Get_UINT32(s);
5247 if (cbRecvLength > 66560)
5248 {
5249 WLog_Print(log, WLOG_ERROR, "2.2.3.11 Transmit_Return cbRecvLength(%" PRIu32 ") > 66560",
5250 cbRecvLength);
5251 return SCARD_E_INVALID_PARAMETER;
5252 }
5253
5254 if (!smartcard_ndr_pointer_read(log, s, &index, &recvBufferNdrPtr))
5255 return ERROR_INVALID_DATA;
5256
5257 if (recvPciNdrPtr)
5258 {
5259 if (!Stream_CheckAndLogRequiredLengthWLog(log, s, 8))
5260 return STATUS_BUFFER_TOO_SMALL;
5261
5262 UINT32 dwProtocol = 0;
5263 UINT32 cbExtraBytes = 0;
5264 Stream_Read_UINT32(s, dwProtocol);
5265 Stream_Read_UINT32(s, cbExtraBytes);
5266
5267 UINT32 pbExtraBytesNdrPtr = 0;
5268 if (!smartcard_ndr_pointer_read(log, s, &index, &pbExtraBytesNdrPtr))
5269 return ERROR_INVALID_DATA;
5270
5271 if (pbExtraBytesNdrPtr)
5272 {
5273 if (!Stream_CheckAndLogRequiredLengthWLog(log, s, 4))
5274 return STATUS_BUFFER_TOO_SMALL;
5275
5276 UINT32 length = 0;
5277 Stream_Read_UINT32(s, length);
5278 if (length != cbExtraBytes)
5279 {
5280 WLog_Print(log, WLOG_WARN,
5281 "Transmit_Return unexpected length: Actual: %" PRIu32
5282 ", Expected: %" PRIu32 " (pioRecvPci.cbExtraBytes)",
5283 length, cbExtraBytes);
5284 return STATUS_INVALID_PARAMETER;
5285 }
5286
5287 if (!Stream_CheckAndLogRequiredLengthWLog(log, s, cbExtraBytes))
5288 return STATUS_BUFFER_TOO_SMALL;
5289
5290 ret->pioRecvPci = (LPSCARD_IO_REQUEST)malloc(sizeof(SCARD_IO_REQUEST) + cbExtraBytes);
5291 if (!ret->pioRecvPci)
5292 return SCARD_E_NO_MEMORY;
5293
5294 ret->pioRecvPci->dwProtocol = dwProtocol;
5295 ret->pioRecvPci->cbPciLength = (DWORD)(sizeof(SCARD_IO_REQUEST) + cbExtraBytes);
5296
5297 BYTE* pbExtraBytes = &((BYTE*)ret->pioRecvPci)[sizeof(SCARD_IO_REQUEST)];
5298 Stream_Read(s, pbExtraBytes, length);
5299 if (smartcard_unpack_read_size_align(s, cbExtraBytes, 4) < 0)
5300 return STATUS_INVALID_PARAMETER;
5301 }
5302 else
5303 {
5304 ret->pioRecvPci = (LPSCARD_IO_REQUEST)malloc(sizeof(SCARD_IO_REQUEST));
5305 if (!ret->pioRecvPci)
5306 return SCARD_E_NO_MEMORY;
5307
5308 ret->pioRecvPci->dwProtocol = dwProtocol;
5309 ret->pioRecvPci->cbPciLength = sizeof(SCARD_IO_REQUEST);
5310 }
5311 }
5312
5313 if (recvBufferNdrPtr && (cbRecvLength > 0))
5314 {
5315 LONG status =
5316 smartcard_ndr_read(log, s, &ret->pbRecvBuffer, cbRecvLength, 1, NDR_PTR_SIMPLE);
5317 if (status != SCARD_S_SUCCESS)
5318 return status;
5319 ret->cbRecvLength = cbRecvLength;
5320 }
5321
5322 smartcard_trace_transmit_return(log, ret);
5323 return SCARD_S_SUCCESS;
5324}
5325
5326LONG smartcard_unpack_get_attrib_return(wStream* s, GetAttrib_Return* ret)
5327{
5328 WINPR_ASSERT(ret);
5329 wLog* log = scard_log();
5330 UINT32 index = 0;
5331 UINT32 pbAttrPtr = 0;
5332
5333 if (!Stream_CheckAndLogRequiredLengthWLog(log, s, 4))
5334 return STATUS_BUFFER_TOO_SMALL;
5335
5336 const UINT32 cbAttrLen = Stream_Get_UINT32(s);
5337 if (cbAttrLen > 65536)
5338 {
5339 WLog_Print(log, WLOG_ERROR, "2.2.3.12 GetAttrib_Return cbAttrLen(%" PRIu32 ") > 65536",
5340 cbAttrLen);
5341 return SCARD_E_INVALID_PARAMETER;
5342 }
5343
5344 if (!smartcard_ndr_pointer_read(log, s, &index, &pbAttrPtr))
5345 return ERROR_INVALID_DATA;
5346
5347 LONG status = smartcard_ndr_read(log, s, &ret->pbAttr, cbAttrLen, 1, NDR_PTR_SIMPLE);
5348 if (status != SCARD_S_SUCCESS)
5349 return status;
5350 ret->cbAttrLen = cbAttrLen;
5351
5352 smartcard_trace_get_attrib_return(log, ret, 0);
5353 return SCARD_S_SUCCESS;
5354}
5355
5356BOOL smartcard_reader_state_return_is_valid_ex(size_t pos, const ReaderState_Return* val,
5357 const char* file, size_t line, const char* fkt)
5358{
5359 if (!val)
5360 {
5361 WLog_Print_dbg_tag(SCARD_TAG, WLOG_WARN, line, file, fkt, "[%" PRIuz "] val = nullptr",
5362 pos);
5363 return FALSE;
5364 }
5365 if (val->cbAtr > sizeof(val->rgbAtr))
5366 {
5367 WLog_Print_dbg_tag(SCARD_TAG, WLOG_WARN, line, file, fkt,
5368 "[%" PRIuz "] val->cbAtr=%" PRIu32 ", max=%" PRIuz, pos, val->cbAtr,
5369 sizeof(val->rgbAtr));
5370 return FALSE;
5371 }
5372
5373 return TRUE;
5374}
Definition wtypes.h:263