FreeRDP
Loading...
Searching...
No Matches
smartcard_pack.c
1
23
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 (!Stream_EnsureRemainingCapacity(s, 8))
1963 return SCARD_E_NO_MEMORY;
1964
1965 if (context->cbContext != 0)
1966 {
1967 Stream_Write_UINT32(s, context->cbContext); /* cbContext (4 bytes) */
1968 Stream_Write_UINT32(s, pbContextNdrPtr); /* pbContextNdrPtr (4 bytes) */
1969 *index = *index + 1;
1970 }
1971 else
1972 Stream_Zero(s, 8);
1973
1974 return SCARD_S_SUCCESS;
1975}
1976
1977LONG smartcard_unpack_redir_scard_context_ref(wLog* log, wStream* s,
1978 WINPR_ATTR_UNUSED UINT32 pbContextNdrPtr,
1979 REDIR_SCARDCONTEXT* context)
1980{
1981 UINT32 length = 0;
1982
1983 WINPR_ASSERT(context);
1984
1985 if (context->cbContext == 0)
1986 return SCARD_S_SUCCESS;
1987
1988 if (!Stream_CheckAndLogRequiredLengthWLog(log, s, 4))
1989 return STATUS_BUFFER_TOO_SMALL;
1990
1991 Stream_Read_UINT32(s, length); /* Length (4 bytes) */
1992
1993 if (length != context->cbContext)
1994 {
1995 WLog_Print(log, WLOG_WARN,
1996 "REDIR_SCARDCONTEXT length (%" PRIu32 ") cbContext (%" PRIu32 ") mismatch",
1997 length, context->cbContext);
1998 return STATUS_INVALID_PARAMETER;
1999 }
2000
2001 const LONG status = smartcard_context_supported(log, context->cbContext);
2002 if (status != SCARD_S_SUCCESS)
2003 return status;
2004
2005 if (!Stream_CheckAndLogRequiredLengthWLog(log, s, context->cbContext))
2006 return STATUS_BUFFER_TOO_SMALL;
2007
2008 if (context->cbContext)
2009 Stream_Read(s, &(context->pbContext), context->cbContext);
2010 else
2011 ZeroMemory(&(context->pbContext), sizeof(context->pbContext));
2012
2013 return SCARD_S_SUCCESS;
2014}
2015
2016LONG smartcard_pack_redir_scard_context_ref(WINPR_ATTR_UNUSED wLog* log, wStream* s,
2017 const REDIR_SCARDCONTEXT* context)
2018{
2019 WINPR_ASSERT(context);
2020
2021 if (context->cbContext == 0)
2022 return SCARD_S_SUCCESS;
2023
2024 if (context->cbContext > sizeof(context->pbContext))
2025 return SCARD_E_INVALID_PARAMETER;
2026
2027 if (!Stream_EnsureRemainingCapacity(s, 4ull + context->cbContext))
2028 return SCARD_E_NO_MEMORY;
2029
2030 Stream_Write_UINT32(s, context->cbContext); /* Length (4 bytes) */
2031 Stream_Write(s, &(context->pbContext), context->cbContext);
2032
2033 return SCARD_S_SUCCESS;
2034}
2035
2036LONG smartcard_unpack_redir_scard_handle_(wLog* log, wStream* s, REDIR_SCARDHANDLE* handle,
2037 UINT32* index, const char* file, const char* function,
2038 size_t line)
2039{
2040 UINT32 pbHandleNdrPtr = 0;
2041
2042 WINPR_ASSERT(handle);
2043 ZeroMemory(handle, sizeof(REDIR_SCARDHANDLE));
2044
2045 if (!Stream_CheckAndLogRequiredLengthWLog(log, s, 4))
2046 return STATUS_BUFFER_TOO_SMALL;
2047
2048 Stream_Read_UINT32(s, handle->cbHandle); /* Length (4 bytes) */
2049
2050 if (!smartcard_ndr_pointer_read_(log, s, index, &pbHandleNdrPtr, file, function, line))
2051 return ERROR_INVALID_DATA;
2052
2053 if (((handle->cbHandle == 0) && pbHandleNdrPtr) ||
2054 ((handle->cbHandle != 0) && !pbHandleNdrPtr))
2055 {
2056 WLog_Print(log, WLOG_WARN,
2057 "REDIR_SCARDHANDLE cbHandle (%" PRIu32 ") pbHandleNdrPtr (%" PRIu32
2058 ") inconsistency",
2059 handle->cbHandle, pbHandleNdrPtr);
2060 return STATUS_INVALID_PARAMETER;
2061 }
2062
2063 return SCARD_S_SUCCESS;
2064}
2065
2066LONG smartcard_pack_redir_scard_handle(WINPR_ATTR_UNUSED wLog* log, wStream* s,
2067 const REDIR_SCARDHANDLE* handle, UINT32* index)
2068{
2069 const UINT32 pbContextNdrPtr = 0x00020000 + *index * 4;
2070
2071 WINPR_ASSERT(handle);
2072 if (!Stream_EnsureRemainingCapacity(s, 8))
2073 return SCARD_E_NO_MEMORY;
2074
2075 if (handle->cbHandle != 0)
2076 {
2077 Stream_Write_UINT32(s, handle->cbHandle); /* cbContext (4 bytes) */
2078 Stream_Write_UINT32(s, pbContextNdrPtr); /* pbContextNdrPtr (4 bytes) */
2079 *index = *index + 1;
2080 }
2081 else
2082 Stream_Zero(s, 8);
2083 return SCARD_S_SUCCESS;
2084}
2085
2086LONG smartcard_unpack_redir_scard_handle_ref(wLog* log, wStream* s, REDIR_SCARDHANDLE* handle)
2087{
2088 UINT32 length = 0;
2089
2090 WINPR_ASSERT(handle);
2091
2092 if (handle->cbHandle == 0)
2093 return SCARD_S_SUCCESS;
2094
2095 if (!Stream_CheckAndLogRequiredLengthWLog(log, s, 4))
2096 return STATUS_BUFFER_TOO_SMALL;
2097
2098 Stream_Read_UINT32(s, length); /* Length (4 bytes) */
2099
2100 if (length != handle->cbHandle)
2101 {
2102 WLog_Print(log, WLOG_WARN,
2103 "REDIR_SCARDHANDLE length (%" PRIu32 ") cbHandle (%" PRIu32 ") mismatch", length,
2104 handle->cbHandle);
2105 return STATUS_INVALID_PARAMETER;
2106 }
2107
2108 if ((handle->cbHandle != 4) && (handle->cbHandle != 8))
2109 {
2110 WLog_Print(log, WLOG_WARN, "REDIR_SCARDHANDLE length is not 4 or 8: %" PRIu32 "",
2111 handle->cbHandle);
2112 return STATUS_INVALID_PARAMETER;
2113 }
2114
2115 if (!Stream_CheckAndLogRequiredLengthWLog(log, s, handle->cbHandle))
2116 return STATUS_BUFFER_TOO_SMALL;
2117
2118 if (handle->cbHandle)
2119 Stream_Read(s, &(handle->pbHandle), handle->cbHandle);
2120
2121 return SCARD_S_SUCCESS;
2122}
2123
2124LONG smartcard_pack_redir_scard_handle_ref(WINPR_ATTR_UNUSED wLog* log, wStream* s,
2125 const REDIR_SCARDHANDLE* handle)
2126{
2127 WINPR_ASSERT(handle);
2128
2129 if (handle->cbHandle == 0)
2130 return SCARD_S_SUCCESS;
2131
2132 if (handle->cbHandle > sizeof(handle->pbHandle))
2133 return SCARD_E_INVALID_PARAMETER;
2134
2135 if (!Stream_EnsureRemainingCapacity(s, 4ull + handle->cbHandle))
2136 return SCARD_E_NO_MEMORY;
2137
2138 Stream_Write_UINT32(s, handle->cbHandle); /* Length (4 bytes) */
2139 Stream_Write(s, &(handle->pbHandle), handle->cbHandle);
2140
2141 return SCARD_S_SUCCESS;
2142}
2143
2144LONG smartcard_unpack_establish_context_call(wStream* s, EstablishContext_Call* call)
2145{
2146 WINPR_ASSERT(call);
2147 wLog* log = scard_log();
2148
2149 if (!Stream_CheckAndLogRequiredLengthWLog(log, s, 4))
2150 return STATUS_BUFFER_TOO_SMALL;
2151
2152 Stream_Read_UINT32(s, call->dwScope); /* dwScope (4 bytes) */
2153 smartcard_trace_establish_context_call(log, call);
2154 return SCARD_S_SUCCESS;
2155}
2156
2157LONG smartcard_pack_establish_context_call(wStream* s, const EstablishContext_Call* call)
2158{
2159 WINPR_ASSERT(call);
2160 wLog* log = scard_log();
2161
2162 smartcard_trace_establish_context_call(log, call);
2163
2164 if (!Stream_EnsureRemainingCapacity(s, 4))
2165 return SCARD_E_NO_MEMORY;
2166
2167 Stream_Write_UINT32(s, call->dwScope);
2168
2169 return SCARD_S_SUCCESS;
2170}
2171
2172LONG smartcard_pack_establish_context_return(wStream* s, const EstablishContext_Return* ret)
2173{
2174 WINPR_ASSERT(ret);
2175 wLog* log = scard_log();
2176 LONG status = 0;
2177 UINT32 index = 0;
2178
2179 smartcard_trace_establish_context_return(log, ret);
2180 if (ret->ReturnCode != SCARD_S_SUCCESS)
2181 return ret->ReturnCode;
2182
2183 status = smartcard_pack_redir_scard_context(log, s, &(ret->hContext), &index);
2184 if (status != SCARD_S_SUCCESS)
2185 return status;
2186
2187 return smartcard_pack_redir_scard_context_ref(log, s, &(ret->hContext));
2188}
2189
2190LONG smartcard_unpack_establish_context_return(wStream* s, EstablishContext_Return* ret)
2191{
2192 WINPR_ASSERT(ret);
2193 wLog* log = scard_log();
2194 UINT32 index = 0;
2195 UINT32 pbContextNdrPtr = 0;
2196
2197 LONG status =
2198 smartcard_unpack_redir_scard_context(log, s, &(ret->hContext), &index, &pbContextNdrPtr);
2199 if (status != SCARD_S_SUCCESS)
2200 return status;
2201
2202 status = smartcard_unpack_redir_scard_context_ref(log, s, pbContextNdrPtr, &(ret->hContext));
2203 if (status != SCARD_S_SUCCESS)
2204 return status;
2205
2206 smartcard_trace_establish_context_return(log, ret);
2207 return SCARD_S_SUCCESS;
2208}
2209
2210LONG smartcard_unpack_context_call(wStream* s, Context_Call* call, const char* name)
2211{
2212 UINT32 index = 0;
2213 UINT32 pbContextNdrPtr = 0;
2214 wLog* log = scard_log();
2215
2216 WINPR_ASSERT(call);
2217 LONG status = smartcard_unpack_redir_scard_context(log, s, &(call->handles.hContext), &index,
2218 &pbContextNdrPtr);
2219 if (status != SCARD_S_SUCCESS)
2220 return status;
2221
2222 status = smartcard_unpack_redir_scard_context_ref(log, s, pbContextNdrPtr,
2223 &(call->handles.hContext));
2224 if (status != SCARD_S_SUCCESS)
2225 WLog_Print(log, WLOG_ERROR,
2226 "smartcard_unpack_redir_scard_context_ref failed with error %" PRId32 "",
2227 status);
2228
2229 smartcard_trace_context_call(log, call, name);
2230 return status;
2231}
2232
2233LONG smartcard_unpack_list_reader_groups_call(wStream* s, ListReaderGroups_Call* call, BOOL unicode)
2234{
2235 UINT32 index = 0;
2236 UINT32 pbContextNdrPtr = 0;
2237 wLog* log = scard_log();
2238
2239 WINPR_ASSERT(call);
2240 LONG status = smartcard_unpack_redir_scard_context(log, s, &(call->handles.hContext), &index,
2241 &pbContextNdrPtr);
2242
2243 if (status != SCARD_S_SUCCESS)
2244 return status;
2245
2246 if (!Stream_CheckAndLogRequiredLengthWLog(log, s, 8))
2247 return STATUS_BUFFER_TOO_SMALL;
2248
2249 Stream_Read_INT32(s, call->fmszGroupsIsNULL); /* fmszGroupsIsNULL (4 bytes) */
2250 Stream_Read_UINT32(s, call->cchGroups); /* cchGroups (4 bytes) */
2251 status = smartcard_unpack_redir_scard_context_ref(log, s, pbContextNdrPtr,
2252 &(call->handles.hContext));
2253
2254 if (status != SCARD_S_SUCCESS)
2255 return status;
2256
2257 smartcard_trace_list_reader_groups_call(log, call, unicode);
2258 return SCARD_S_SUCCESS;
2259}
2260
2261LONG smartcard_pack_list_reader_groups_call(wStream* s, const ListReaderGroups_Call* call,
2262 BOOL unicode)
2263{
2264 WINPR_ASSERT(call);
2265 wLog* log = scard_log();
2266 UINT32 index = 0;
2267
2268 smartcard_trace_list_reader_groups_call(log, call, unicode);
2269
2270 LONG status = smartcard_pack_redir_scard_context(log, s, &call->handles.hContext, &index);
2271 if (status != SCARD_S_SUCCESS)
2272 return status;
2273
2274 if (!Stream_EnsureRemainingCapacity(s, 8))
2275 return SCARD_E_NO_MEMORY;
2276
2277 Stream_Write_INT32(s, call->fmszGroupsIsNULL);
2278 Stream_Write_UINT32(s, call->cchGroups);
2279
2280 return smartcard_pack_redir_scard_context_ref(log, s, &call->handles.hContext);
2281}
2282
2283LONG smartcard_pack_list_reader_groups_return(wStream* s, const ListReaderGroups_Return* ret,
2284 BOOL unicode)
2285{
2286 WINPR_ASSERT(ret);
2287 wLog* log = scard_log();
2288 LONG status = 0;
2289 UINT32 cBytes = ret->cBytes;
2290 UINT32 index = 0;
2291
2292 smartcard_trace_list_reader_groups_return(log, ret, unicode);
2293 if (ret->ReturnCode != SCARD_S_SUCCESS)
2294 cBytes = 0;
2295 if (cBytes == SCARD_AUTOALLOCATE)
2296 cBytes = 0;
2297
2298 if (!Stream_EnsureRemainingCapacity(s, 4))
2299 return SCARD_E_NO_MEMORY;
2300
2301 if (cBytes > 65536)
2302 {
2303 WLog_Print(log, WLOG_ERROR,
2304 "2.2.3.4 ListReaderGroups_Return and ListReaders_Return cBytes (%" PRIu32
2305 ") > 65536",
2306 cBytes);
2307 return SCARD_E_INVALID_PARAMETER;
2308 }
2309
2310 Stream_Write_UINT32(s, cBytes); /* cBytes (4 bytes) */
2311 if (!smartcard_ndr_pointer_write(s, &index, cBytes))
2312 return SCARD_E_NO_MEMORY;
2313
2314 status = smartcard_ndr_write(s, ret->msz, cBytes, 1, NDR_PTR_SIMPLE);
2315 if (status != SCARD_S_SUCCESS)
2316 return status;
2317 return ret->ReturnCode;
2318}
2319
2320LONG smartcard_unpack_list_reader_groups_return(wStream* s, ListReaderGroups_Return* ret,
2321 BOOL unicode)
2322{
2323 WINPR_ASSERT(ret);
2324 wLog* log = scard_log();
2325 UINT32 index = 0;
2326 UINT32 mszNdrPtr = 0;
2327
2328 if (!Stream_CheckAndLogRequiredLengthWLog(log, s, 4))
2329 return STATUS_BUFFER_TOO_SMALL;
2330
2331 const UINT32 cBytes = Stream_Get_UINT32(s);
2332 if (cBytes > 65536)
2333 {
2334 WLog_Print(log, WLOG_ERROR,
2335 "2.2.3.4 ListReaderGroups_Return and ListReaders_Return cBytes (%" PRIu32
2336 ") > 65536",
2337 cBytes);
2338 return SCARD_E_INVALID_PARAMETER;
2339 }
2340
2341 if (!smartcard_ndr_pointer_read(log, s, &index, &mszNdrPtr))
2342 return ERROR_INVALID_DATA;
2343
2344 if (mszNdrPtr)
2345 {
2346 LONG status = smartcard_ndr_read(log, s, &ret->msz, cBytes, 1, NDR_PTR_SIMPLE);
2347 if (status != SCARD_S_SUCCESS)
2348 return status;
2349 ret->cBytes = cBytes;
2350 }
2351
2352 smartcard_trace_list_reader_groups_return(log, ret, unicode);
2353 return SCARD_S_SUCCESS;
2354}
2355
2356LONG smartcard_unpack_list_readers_call(wStream* s, ListReaders_Call* call, BOOL unicode)
2357{
2358 UINT32 index = 0;
2359 UINT32 mszGroupsNdrPtr = 0;
2360 UINT32 pbContextNdrPtr = 0;
2361 wLog* log = scard_log();
2362
2363 WINPR_ASSERT(call);
2364 call->mszGroups = nullptr;
2365
2366 LONG status = smartcard_unpack_redir_scard_context(log, s, &(call->handles.hContext), &index,
2367 &pbContextNdrPtr);
2368 if (status != SCARD_S_SUCCESS)
2369 return status;
2370
2371 if (!Stream_CheckAndLogRequiredLengthWLog(log, s, 4))
2372 return STATUS_BUFFER_TOO_SMALL;
2373
2374 const UINT32 cBytes = Stream_Get_UINT32(s);
2375 if (cBytes > 65536)
2376 {
2377 WLog_Print(log, WLOG_ERROR,
2378 "2.2.3.4 ListReaderGroups_Return and ListReaders_Return cBytes (%" PRIu32
2379 ") > 65536",
2380 cBytes);
2381 return SCARD_E_INVALID_PARAMETER;
2382 }
2383 if (unicode && ((cBytes % 2) != 0))
2384 {
2385 WLog_Print(log, WLOG_ERROR,
2386 "2.2.3.4 ListReaderGroupsW_Return and ListReadersW_Return cBytes (%" PRIu32
2387 ") not even",
2388 cBytes);
2389 return SCARD_E_INVALID_PARAMETER;
2390 }
2391 if (!smartcard_ndr_pointer_read(log, s, &index, &mszGroupsNdrPtr))
2392 return ERROR_INVALID_DATA;
2393
2394 if (!Stream_CheckAndLogRequiredLengthWLog(log, s, 8))
2395 return STATUS_BUFFER_TOO_SMALL;
2396 Stream_Read_INT32(s, call->fmszReadersIsNULL); /* fmszReadersIsNULL (4 bytes) */
2397 Stream_Read_UINT32(s, call->cchReaders); /* cchReaders (4 bytes) */
2398
2399 status = smartcard_unpack_redir_scard_context_ref(log, s, pbContextNdrPtr,
2400 &(call->handles.hContext));
2401 if (status != SCARD_S_SUCCESS)
2402 return status;
2403
2404 if (mszGroupsNdrPtr)
2405 {
2406 status = smartcard_ndr_read(log, s, &call->mszGroups, cBytes, 1, NDR_PTR_SIMPLE);
2407 if (status != SCARD_S_SUCCESS)
2408 return status;
2409 call->cBytes = cBytes;
2410 }
2411
2412 smartcard_trace_list_readers_call(log, call, unicode);
2413 return SCARD_S_SUCCESS;
2414}
2415
2416LONG smartcard_pack_list_readers_return(wStream* s, const ListReaders_Return* ret, BOOL unicode)
2417{
2418 WINPR_ASSERT(ret);
2419 wLog* log = scard_log();
2420 LONG status = 0;
2421 UINT32 index = 0;
2422 UINT32 size = ret->cBytes;
2423
2424 smartcard_trace_list_readers_return(log, ret, unicode);
2425 if (ret->ReturnCode != SCARD_S_SUCCESS)
2426 size = 0;
2427
2428 if (!Stream_EnsureRemainingCapacity(s, 4))
2429 {
2430 WLog_Print(log, WLOG_ERROR, "Stream_EnsureRemainingCapacity failed!");
2431 return SCARD_F_INTERNAL_ERROR;
2432 }
2433
2434 if (size > 65536)
2435 {
2436 WLog_Print(log, WLOG_ERROR,
2437 "2.2.3.4 ListReaderGroups_Return and ListReaders_Return cBytes (%" PRIu32
2438 ") > 65536",
2439 size);
2440 return SCARD_E_INVALID_PARAMETER;
2441 }
2442
2443 Stream_Write_UINT32(s, size); /* cBytes (4 bytes) */
2444 if (!smartcard_ndr_pointer_write(s, &index, size))
2445 return SCARD_E_NO_MEMORY;
2446
2447 status = smartcard_ndr_write(s, ret->msz, size, 1, NDR_PTR_SIMPLE);
2448 if (status != SCARD_S_SUCCESS)
2449 return status;
2450 return ret->ReturnCode;
2451}
2452
2453WINPR_ATTR_NODISCARD static LONG smartcard_unpack_connect_common(wLog* log, wStream* s,
2454 Connect_Common_Call* common,
2455 UINT32* index,
2456 UINT32* ppbContextNdrPtr)
2457{
2458 WINPR_ASSERT(common);
2459 LONG status = smartcard_unpack_redir_scard_context(log, s, &(common->handles.hContext), index,
2460 ppbContextNdrPtr);
2461 if (status != SCARD_S_SUCCESS)
2462 return status;
2463
2464 if (!Stream_CheckAndLogRequiredLengthWLog(log, s, 8))
2465 return STATUS_BUFFER_TOO_SMALL;
2466
2467 Stream_Read_UINT32(s, common->dwShareMode); /* dwShareMode (4 bytes) */
2468 Stream_Read_UINT32(s, common->dwPreferredProtocols); /* dwPreferredProtocols (4 bytes) */
2469 return SCARD_S_SUCCESS;
2470}
2471
2472LONG smartcard_unpack_connect_a_call(wStream* s, ConnectA_Call* call)
2473{
2474 LONG status = 0;
2475 UINT32 index = 0;
2476 UINT32 pbContextNdrPtr = 0;
2477
2478 WINPR_ASSERT(call);
2479 wLog* log = scard_log();
2480
2481 call->szReader = nullptr;
2482
2483 if (!smartcard_ndr_pointer_read(log, s, &index, nullptr))
2484 return ERROR_INVALID_DATA;
2485
2486 status = smartcard_unpack_connect_common(log, s, &(call->Common), &index, &pbContextNdrPtr);
2487 if (status != SCARD_S_SUCCESS)
2488 {
2489 WLog_Print(log, WLOG_ERROR, "smartcard_unpack_connect_common failed with error %" PRId32 "",
2490 status);
2491 return status;
2492 }
2493
2494 status = smartcard_ndr_read_a(log, s, &call->szReader, NDR_PTR_FULL);
2495 if (status != SCARD_S_SUCCESS)
2496 return status;
2497
2498 status = smartcard_unpack_redir_scard_context_ref(log, s, pbContextNdrPtr,
2499 &(call->Common.handles.hContext));
2500 if (status != SCARD_S_SUCCESS)
2501 WLog_Print(log, WLOG_ERROR,
2502 "smartcard_unpack_redir_scard_context_ref failed with error %" PRId32 "",
2503 status);
2504
2505 smartcard_trace_connect_a_call(log, call);
2506 return status;
2507}
2508
2509LONG smartcard_unpack_connect_w_call(wStream* s, ConnectW_Call* call)
2510{
2511 LONG status = 0;
2512 UINT32 index = 0;
2513 UINT32 pbContextNdrPtr = 0;
2514
2515 WINPR_ASSERT(call);
2516 wLog* log = scard_log();
2517 call->szReader = nullptr;
2518
2519 if (!smartcard_ndr_pointer_read(log, s, &index, nullptr))
2520 return ERROR_INVALID_DATA;
2521
2522 status = smartcard_unpack_connect_common(log, s, &(call->Common), &index, &pbContextNdrPtr);
2523 if (status != SCARD_S_SUCCESS)
2524 {
2525 WLog_Print(log, WLOG_ERROR, "smartcard_unpack_connect_common failed with error %" PRId32 "",
2526 status);
2527 return status;
2528 }
2529
2530 status = smartcard_ndr_read_w(log, s, &call->szReader, NDR_PTR_FULL);
2531 if (status != SCARD_S_SUCCESS)
2532 return status;
2533
2534 status = smartcard_unpack_redir_scard_context_ref(log, s, pbContextNdrPtr,
2535 &(call->Common.handles.hContext));
2536 if (status != SCARD_S_SUCCESS)
2537 WLog_Print(log, WLOG_ERROR,
2538 "smartcard_unpack_redir_scard_context_ref failed with error %" PRId32 "",
2539 status);
2540
2541 smartcard_trace_connect_w_call(log, call);
2542 return status;
2543}
2544
2545LONG smartcard_pack_connect_return(wStream* s, const Connect_Return* ret)
2546{
2547 LONG status = 0;
2548 UINT32 index = 0;
2549
2550 WINPR_ASSERT(ret);
2551 wLog* log = scard_log();
2552 smartcard_trace_connect_return(log, ret);
2553
2554 status = smartcard_pack_redir_scard_context(log, s, &ret->hContext, &index);
2555 if (status != SCARD_S_SUCCESS)
2556 return status;
2557
2558 status = smartcard_pack_redir_scard_handle(log, s, &ret->hCard, &index);
2559 if (status != SCARD_S_SUCCESS)
2560 return status;
2561
2562 if (!Stream_EnsureRemainingCapacity(s, 4))
2563 return SCARD_E_NO_MEMORY;
2564
2565 Stream_Write_UINT32(s, ret->dwActiveProtocol); /* dwActiveProtocol (4 bytes) */
2566 status = smartcard_pack_redir_scard_context_ref(log, s, &ret->hContext);
2567 if (status != SCARD_S_SUCCESS)
2568 return status;
2569 return smartcard_pack_redir_scard_handle_ref(log, s, &(ret->hCard));
2570}
2571
2572LONG smartcard_unpack_reconnect_call(wStream* s, Reconnect_Call* call)
2573{
2574 UINT32 index = 0;
2575 UINT32 pbContextNdrPtr = 0;
2576
2577 WINPR_ASSERT(call);
2578 wLog* log = scard_log();
2579 LONG status = smartcard_unpack_redir_scard_context(log, s, &(call->handles.hContext), &index,
2580 &pbContextNdrPtr);
2581 if (status != SCARD_S_SUCCESS)
2582 return status;
2583
2584 status = smartcard_unpack_redir_scard_handle(log, s, &(call->handles.hCard), &index);
2585 if (status != SCARD_S_SUCCESS)
2586 return status;
2587
2588 if (!Stream_CheckAndLogRequiredLengthWLog(log, s, 12))
2589 return STATUS_BUFFER_TOO_SMALL;
2590
2591 Stream_Read_UINT32(s, call->dwShareMode); /* dwShareMode (4 bytes) */
2592 Stream_Read_UINT32(s, call->dwPreferredProtocols); /* dwPreferredProtocols (4 bytes) */
2593 Stream_Read_UINT32(s, call->dwInitialization); /* dwInitialization (4 bytes) */
2594
2595 status = smartcard_unpack_redir_scard_context_ref(log, s, pbContextNdrPtr,
2596 &(call->handles.hContext));
2597 if (status != SCARD_S_SUCCESS)
2598 {
2599 WLog_Print(log, WLOG_ERROR,
2600 "smartcard_unpack_redir_scard_context_ref failed with error %" PRId32 "",
2601 status);
2602 return status;
2603 }
2604
2605 status = smartcard_unpack_redir_scard_handle_ref(log, s, &(call->handles.hCard));
2606 if (status != SCARD_S_SUCCESS)
2607 WLog_Print(log, WLOG_ERROR,
2608 "smartcard_unpack_redir_scard_handle_ref failed with error %" PRId32 "", status);
2609
2610 smartcard_trace_reconnect_call(log, call);
2611 return status;
2612}
2613
2614LONG smartcard_pack_reconnect_call(wStream* s, const Reconnect_Call* call)
2615{
2616 WINPR_ASSERT(call);
2617 wLog* log = scard_log();
2618 UINT32 index = 0;
2619
2620 smartcard_trace_reconnect_call(log, call);
2621
2622 LONG status = smartcard_pack_redir_scard_context(log, s, &call->handles.hContext, &index);
2623 if (status != SCARD_S_SUCCESS)
2624 return status;
2625
2626 status = smartcard_pack_redir_scard_handle(log, s, &call->handles.hCard, &index);
2627 if (status != SCARD_S_SUCCESS)
2628 return status;
2629
2630 if (!Stream_EnsureRemainingCapacity(s, 12))
2631 return SCARD_E_NO_MEMORY;
2632
2633 Stream_Write_UINT32(s, call->dwShareMode);
2634 Stream_Write_UINT32(s, call->dwPreferredProtocols);
2635 Stream_Write_UINT32(s, call->dwInitialization);
2636
2637 status = smartcard_pack_redir_scard_context_ref(log, s, &call->handles.hContext);
2638 if (status != SCARD_S_SUCCESS)
2639 return status;
2640
2641 return smartcard_pack_redir_scard_handle_ref(log, s, &call->handles.hCard);
2642}
2643
2644LONG smartcard_pack_reconnect_return(wStream* s, const Reconnect_Return* ret)
2645{
2646 WINPR_ASSERT(ret);
2647 wLog* log = scard_log();
2648 smartcard_trace_reconnect_return(log, ret);
2649
2650 if (!Stream_EnsureRemainingCapacity(s, 4))
2651 return SCARD_E_NO_MEMORY;
2652 Stream_Write_UINT32(s, ret->dwActiveProtocol); /* dwActiveProtocol (4 bytes) */
2653 return ret->ReturnCode;
2654}
2655
2656LONG smartcard_unpack_reconnect_return(wStream* s, Reconnect_Return* ret)
2657{
2658 WINPR_ASSERT(ret);
2659 wLog* log = scard_log();
2660
2661 if (!Stream_CheckAndLogRequiredLengthWLog(log, s, 4))
2662 return STATUS_BUFFER_TOO_SMALL;
2663
2664 Stream_Read_UINT32(s, ret->dwActiveProtocol);
2665
2666 smartcard_trace_reconnect_return(log, ret);
2667 return SCARD_S_SUCCESS;
2668}
2669
2670LONG smartcard_unpack_hcard_and_disposition_call(wStream* s, HCardAndDisposition_Call* call,
2671 const char* name)
2672{
2673 UINT32 index = 0;
2674 UINT32 pbContextNdrPtr = 0;
2675
2676 WINPR_ASSERT(call);
2677 wLog* log = scard_log();
2678
2679 LONG status = smartcard_unpack_redir_scard_context(log, s, &(call->handles.hContext), &index,
2680 &pbContextNdrPtr);
2681 if (status != SCARD_S_SUCCESS)
2682 return status;
2683
2684 status = smartcard_unpack_redir_scard_handle(log, s, &(call->handles.hCard), &index);
2685 if (status != SCARD_S_SUCCESS)
2686 return status;
2687
2688 if (!Stream_CheckAndLogRequiredLengthWLog(log, s, 4))
2689 return STATUS_BUFFER_TOO_SMALL;
2690
2691 Stream_Read_UINT32(s, call->dwDisposition); /* dwDisposition (4 bytes) */
2692
2693 status = smartcard_unpack_redir_scard_context_ref(log, s, pbContextNdrPtr,
2694 &(call->handles.hContext));
2695 if (status != SCARD_S_SUCCESS)
2696 return status;
2697
2698 status = smartcard_unpack_redir_scard_handle_ref(log, s, &(call->handles.hCard));
2699 if (status != SCARD_S_SUCCESS)
2700 return status;
2701
2702 smartcard_trace_hcard_and_disposition_call(log, call, name);
2703 return status;
2704}
2705
2706static void smartcard_trace_get_status_change_a_call(wLog* log, const GetStatusChangeA_Call* call)
2707{
2708 WINPR_ASSERT(call);
2709
2710 if (!WLog_IsLevelActive(log, g_LogLevel))
2711 return;
2712
2713 WLog_Print(log, g_LogLevel, "GetStatusChangeA_Call {");
2714 smartcard_log_context(log, &call->handles.hContext);
2715
2716 WLog_Print(log, g_LogLevel, "dwTimeOut: 0x%08" PRIX32 " cReaders: %" PRIu32 "", call->dwTimeOut,
2717 call->cReaders);
2718
2719 dump_reader_states_a(log, call->rgReaderStates, call->cReaders);
2720
2721 WLog_Print(log, g_LogLevel, "}");
2722}
2723
2724WINPR_ATTR_NODISCARD static LONG smartcard_unpack_reader_state_a(wLog* log, wStream* s,
2725 LPSCARD_READERSTATEA* ppcReaders,
2726 UINT32 cReaders, UINT32* ptrIndex)
2727{
2728 LONG status = SCARD_E_NO_MEMORY;
2729
2730 WINPR_ASSERT(ppcReaders || (cReaders == 0));
2731 if (cReaders > UINT8_MAX)
2732 {
2733 WLog_Print(log, WLOG_ERROR,
2734 "Too many readers(%" PRIu32 "), rejecting request. Limited to <= %d", cReaders,
2735 UINT8_MAX);
2736 return SCARD_E_WRITE_TOO_MANY;
2737 }
2738
2739 if (!Stream_CheckAndLogRequiredLengthWLog(log, s, 4))
2740 return status;
2741
2742 const UINT32 len = Stream_Get_UINT32(s);
2743 if (len != cReaders)
2744 {
2745 WLog_Print(log, WLOG_ERROR, "Count mismatch when reading LPSCARD_READERSTATEA");
2746 return status;
2747 }
2748
2749 LPSCARD_READERSTATEA rgReaderStates =
2750 (LPSCARD_READERSTATEA)calloc(cReaders, sizeof(SCARD_READERSTATEA));
2751 BOOL* states = calloc(cReaders, sizeof(BOOL));
2752 if (!rgReaderStates || !states)
2753 goto fail;
2754 status = ERROR_INVALID_DATA;
2755
2756 for (UINT32 index = 0; index < cReaders; index++)
2757 {
2758 UINT32 ptr = UINT32_MAX;
2759 LPSCARD_READERSTATEA readerState = &rgReaderStates[index];
2760
2761 if (!Stream_CheckAndLogRequiredLengthWLog(log, s, 52))
2762 goto fail;
2763
2764 if (!smartcard_ndr_pointer_read(log, s, ptrIndex, &ptr))
2765 {
2766 if (ptr != 0)
2767 goto fail;
2768 }
2769 /* Ignore nullptr length strings */
2770 states[index] = ptr != 0;
2771 Stream_Read_UINT32(s, readerState->dwCurrentState); /* dwCurrentState (4 bytes) */
2772 Stream_Read_UINT32(s, readerState->dwEventState); /* dwEventState (4 bytes) */
2773 Stream_Read_UINT32(s, readerState->cbAtr); /* cbAtr (4 bytes) */
2774 if (readerState->cbAtr > ARRAYSIZE(readerState->rgbAtr))
2775 {
2776 WLog_Print(log, WLOG_WARN,
2777 "SCARD_READERSTATEA[%" PRIu32 "]::cbAtr %" PRIu32 " exceeds %" PRIuz, index,
2778 readerState->cbAtr, (size_t)ARRAYSIZE(readerState->rgbAtr));
2779 readerState->cbAtr = ARRAYSIZE(readerState->rgbAtr);
2780 }
2781 Stream_Read(s, readerState->rgbAtr, 36); /* rgbAtr [0..36] (36 bytes) */
2782 }
2783
2784 for (UINT32 index = 0; index < cReaders; index++)
2785 {
2786 LPSCARD_READERSTATEA readerState = &rgReaderStates[index];
2787
2788 /* Ignore empty strings */
2789 if (!states[index])
2790 continue;
2791 status = smartcard_ndr_read_a(log, s, &readerState->szReader, NDR_PTR_FULL);
2792 if (status != SCARD_S_SUCCESS)
2793 goto fail;
2794 if (!readerState->szReader)
2795 goto fail;
2796 }
2797
2798 *ppcReaders = rgReaderStates;
2799 free(states);
2800 return SCARD_S_SUCCESS;
2801fail:
2802 if (rgReaderStates)
2803 {
2804 for (UINT32 index = 0; index < cReaders; index++)
2805 {
2806 LPSCARD_READERSTATEA readerState = &rgReaderStates[index];
2807 free(readerState->szReader);
2808 }
2809 }
2810 free(rgReaderStates);
2811 free(states);
2812 return status;
2813}
2814
2815WINPR_ATTR_NODISCARD static LONG smartcard_unpack_reader_state_w(wLog* log, wStream* s,
2816 LPSCARD_READERSTATEW* ppcReaders,
2817 UINT32 cReaders, UINT32* ptrIndex)
2818{
2819 LONG status = SCARD_E_NO_MEMORY;
2820
2821 WINPR_ASSERT(ppcReaders || (cReaders == 0));
2822
2823 if (cReaders > UINT8_MAX)
2824 {
2825 WLog_Print(log, WLOG_ERROR,
2826 "Too many readers(%" PRIu32 "), rejecting request. Limited to <= %d", cReaders,
2827 UINT8_MAX);
2828 return SCARD_E_WRITE_TOO_MANY;
2829 }
2830
2831 if (!Stream_CheckAndLogRequiredLengthWLog(log, s, 4))
2832 return status;
2833
2834 const UINT32 len = Stream_Get_UINT32(s);
2835 if (len != cReaders)
2836 {
2837 WLog_Print(log, WLOG_ERROR, "Count mismatch when reading LPSCARD_READERSTATEW");
2838 return status;
2839 }
2840
2841 LPSCARD_READERSTATEW rgReaderStates =
2842 (LPSCARD_READERSTATEW)calloc(cReaders, sizeof(SCARD_READERSTATEW));
2843 BOOL* states = calloc(cReaders, sizeof(BOOL));
2844
2845 if (!rgReaderStates || !states)
2846 goto fail;
2847
2848 status = ERROR_INVALID_DATA;
2849 for (UINT32 index = 0; index < cReaders; index++)
2850 {
2851 UINT32 ptr = UINT32_MAX;
2852 LPSCARD_READERSTATEW readerState = &rgReaderStates[index];
2853
2854 if (!Stream_CheckAndLogRequiredLengthWLog(log, s, 52))
2855 goto fail;
2856
2857 if (!smartcard_ndr_pointer_read(log, s, ptrIndex, &ptr))
2858 {
2859 if (ptr != 0)
2860 goto fail;
2861 }
2862 /* Ignore nullptr length strings */
2863 states[index] = ptr != 0;
2864 Stream_Read_UINT32(s, readerState->dwCurrentState); /* dwCurrentState (4 bytes) */
2865 Stream_Read_UINT32(s, readerState->dwEventState); /* dwEventState (4 bytes) */
2866 Stream_Read_UINT32(s, readerState->cbAtr); /* cbAtr (4 bytes) */
2867 if (readerState->cbAtr > ARRAYSIZE(readerState->rgbAtr))
2868 {
2869 WLog_Print(log, WLOG_WARN,
2870 "SCARD_READERSTATEW[%" PRIu32 "]::cbAtr %" PRIu32 " exceeds %" PRIuz, index,
2871 readerState->cbAtr, ARRAYSIZE(readerState->rgbAtr));
2872 readerState->cbAtr = ARRAYSIZE(readerState->rgbAtr);
2873 }
2874 Stream_Read(s, readerState->rgbAtr, 36); /* rgbAtr [0..36] (36 bytes) */
2875 }
2876
2877 for (UINT32 index = 0; index < cReaders; index++)
2878 {
2879 LPSCARD_READERSTATEW readerState = &rgReaderStates[index];
2880
2881 /* Skip nullptr pointers */
2882 if (!states[index])
2883 continue;
2884
2885 status = smartcard_ndr_read_w(log, s, &readerState->szReader, NDR_PTR_FULL);
2886 if (status != SCARD_S_SUCCESS)
2887 goto fail;
2888 if (!readerState->szReader)
2889 goto fail;
2890 }
2891
2892 *ppcReaders = rgReaderStates;
2893 free(states);
2894 return SCARD_S_SUCCESS;
2895fail:
2896 if (rgReaderStates)
2897 {
2898 for (UINT32 index = 0; index < cReaders; index++)
2899 {
2900 LPSCARD_READERSTATEW readerState = &rgReaderStates[index];
2901 free(readerState->szReader);
2902 }
2903 }
2904 free(rgReaderStates);
2905 free(states);
2906 return status;
2907}
2908
2909/******************************************************************************/
2910/************************************* End Trace Functions ********************/
2911/******************************************************************************/
2912
2913LONG smartcard_unpack_get_status_change_a_call(wStream* s, GetStatusChangeA_Call* call)
2914{
2915 UINT32 ndrPtr = 0;
2916 UINT32 index = 0;
2917 UINT32 pbContextNdrPtr = 0;
2918
2919 WINPR_ASSERT(call);
2920 wLog* log = scard_log();
2921
2922 call->rgReaderStates = nullptr;
2923
2924 LONG status = smartcard_unpack_redir_scard_context(log, s, &(call->handles.hContext), &index,
2925 &pbContextNdrPtr);
2926 if (status != SCARD_S_SUCCESS)
2927 return status;
2928
2929 if (!Stream_CheckAndLogRequiredLengthWLog(log, s, 8))
2930 return STATUS_BUFFER_TOO_SMALL;
2931
2932 Stream_Read_UINT32(s, call->dwTimeOut); /* dwTimeOut (4 bytes) */
2933 const UINT32 cReaders = Stream_Get_UINT32(s); /* cReaders (4 bytes) */
2934 if (cReaders > 11)
2935 {
2936 WLog_Print(log, WLOG_WARN,
2937 "2.2.2.11 GetStatusChangeA_Call: cReaders must be <= 11, got %" PRIu32,
2938 cReaders);
2939 return ERROR_INVALID_DATA;
2940 }
2941
2942 if (!smartcard_ndr_pointer_read(log, s, &index, &ndrPtr))
2943 return ERROR_INVALID_DATA;
2944
2945 status = smartcard_unpack_redir_scard_context_ref(log, s, pbContextNdrPtr,
2946 &(call->handles.hContext));
2947 if (status != SCARD_S_SUCCESS)
2948 return status;
2949
2950 if (ndrPtr)
2951 {
2952 status = smartcard_unpack_reader_state_a(log, s, &call->rgReaderStates, cReaders, &index);
2953 if (status != SCARD_S_SUCCESS)
2954 return status;
2955 call->cReaders = cReaders;
2956 }
2957 else
2958 {
2959 WLog_Print(log, WLOG_WARN, "ndrPtr=0x%08" PRIx32 ", can not read rgReaderStates", ndrPtr);
2960 return SCARD_E_UNEXPECTED;
2961 }
2962
2963 smartcard_trace_get_status_change_a_call(log, call);
2964 return SCARD_S_SUCCESS;
2965}
2966
2967LONG smartcard_unpack_get_status_change_w_call(wStream* s, GetStatusChangeW_Call* call)
2968{
2969 UINT32 ndrPtr = 0;
2970 UINT32 index = 0;
2971 UINT32 pbContextNdrPtr = 0;
2972
2973 WINPR_ASSERT(call);
2974 wLog* log = scard_log();
2975 call->rgReaderStates = nullptr;
2976
2977 LONG status = smartcard_unpack_redir_scard_context(log, s, &(call->handles.hContext), &index,
2978 &pbContextNdrPtr);
2979 if (status != SCARD_S_SUCCESS)
2980 return status;
2981
2982 if (!Stream_CheckAndLogRequiredLengthWLog(log, s, 8))
2983 return STATUS_BUFFER_TOO_SMALL;
2984
2985 Stream_Read_UINT32(s, call->dwTimeOut); /* dwTimeOut (4 bytes) */
2986 const UINT32 cReaders = Stream_Get_UINT32(s); /* cReaders (4 bytes) */
2987 if (cReaders > 11)
2988 {
2989 WLog_Print(log, WLOG_WARN,
2990 "2.2.2.12 GetStatusChangeW_Call: cReaders must be <= 11, got %" PRIu32,
2991 cReaders);
2992 return ERROR_INVALID_DATA;
2993 }
2994
2995 if (!smartcard_ndr_pointer_read(log, s, &index, &ndrPtr))
2996 return ERROR_INVALID_DATA;
2997
2998 status = smartcard_unpack_redir_scard_context_ref(log, s, pbContextNdrPtr,
2999 &(call->handles.hContext));
3000 if (status != SCARD_S_SUCCESS)
3001 return status;
3002
3003 if (ndrPtr)
3004 {
3005 status = smartcard_unpack_reader_state_w(log, s, &call->rgReaderStates, cReaders, &index);
3006 if (status != SCARD_S_SUCCESS)
3007 return status;
3008 call->cReaders = cReaders;
3009 }
3010 else
3011 {
3012 WLog_Print(log, WLOG_WARN, "ndrPtr=0x%08" PRIx32 ", can not read rgReaderStates", ndrPtr);
3013 return SCARD_E_UNEXPECTED;
3014 }
3015
3016 smartcard_trace_get_status_change_w_call(log, call);
3017 return SCARD_S_SUCCESS;
3018}
3019
3020LONG smartcard_pack_get_status_change_return(wStream* s, const GetStatusChange_Return* ret,
3021 BOOL unicode)
3022{
3023 WINPR_ASSERT(ret);
3024 wLog* log = scard_log();
3025
3026 LONG status = 0;
3027 UINT32 cReaders = ret->cReaders;
3028 UINT32 index = 0;
3029
3030 if (ret->cReaders > 10)
3031 {
3032 WLog_Print(log, WLOG_ERROR,
3033 "2.2.3.5 LocateCards_Return and GetStatusChange_Return cReaders(%" PRIu32
3034 ") > 10",
3035 ret->cReaders);
3036 return SCARD_E_INVALID_PARAMETER;
3037 }
3038
3039 smartcard_trace_get_status_change_return(log, ret, unicode);
3040 if ((ret->ReturnCode != SCARD_S_SUCCESS) && (ret->ReturnCode != SCARD_E_TIMEOUT))
3041 cReaders = 0;
3042 if (cReaders == SCARD_AUTOALLOCATE)
3043 cReaders = 0;
3044
3045 if (!Stream_EnsureRemainingCapacity(s, 4))
3046 return SCARD_E_NO_MEMORY;
3047
3048 Stream_Write_UINT32(s, cReaders); /* cReaders (4 bytes) */
3049 if (!smartcard_ndr_pointer_write(s, &index, cReaders))
3050 return SCARD_E_NO_MEMORY;
3051 status = smartcard_ndr_write_state(s, ret->rgReaderStates, cReaders, NDR_PTR_SIMPLE);
3052 if (status != SCARD_S_SUCCESS)
3053 return status;
3054 return ret->ReturnCode;
3055}
3056
3057LONG smartcard_unpack_state_call(wStream* s, State_Call* call)
3058{
3059 UINT32 index = 0;
3060 UINT32 pbContextNdrPtr = 0;
3061
3062 wLog* log = scard_log();
3063
3064 WINPR_ASSERT(call);
3065 LONG status = smartcard_unpack_redir_scard_context(log, s, &(call->handles.hContext), &index,
3066 &pbContextNdrPtr);
3067 if (status != SCARD_S_SUCCESS)
3068 return status;
3069
3070 status = smartcard_unpack_redir_scard_handle(log, s, &(call->handles.hCard), &index);
3071 if (status != SCARD_S_SUCCESS)
3072 return status;
3073
3074 if (!Stream_CheckAndLogRequiredLengthWLog(log, s, 8))
3075 return STATUS_BUFFER_TOO_SMALL;
3076
3077 Stream_Read_INT32(s, call->fpbAtrIsNULL); /* fpbAtrIsNULL (4 bytes) */
3078 Stream_Read_UINT32(s, call->cbAtrLen); /* cbAtrLen (4 bytes) */
3079
3080 status = smartcard_unpack_redir_scard_context_ref(log, s, pbContextNdrPtr,
3081 &(call->handles.hContext));
3082 if (status != SCARD_S_SUCCESS)
3083 return status;
3084
3085 status = smartcard_unpack_redir_scard_handle_ref(log, s, &(call->handles.hCard));
3086 if (status != SCARD_S_SUCCESS)
3087 return status;
3088
3089 return status;
3090}
3091
3092LONG smartcard_pack_state_return(wStream* s, const State_Return* ret)
3093{
3094 WINPR_ASSERT(ret);
3095 wLog* log = scard_log();
3096 LONG status = 0;
3097 UINT32 cbAtrLen = ret->cbAtrLen;
3098 UINT32 index = 0;
3099
3100 smartcard_trace_state_return(log, ret);
3101 if (ret->ReturnCode != SCARD_S_SUCCESS)
3102 cbAtrLen = 0;
3103 if (cbAtrLen == SCARD_AUTOALLOCATE)
3104 cbAtrLen = 0;
3105
3106 if (!Stream_EnsureRemainingCapacity(s, 12))
3107 return SCARD_E_NO_MEMORY;
3108
3109 Stream_Write_UINT32(s, ret->dwState); /* dwState (4 bytes) */
3110 Stream_Write_UINT32(s, ret->dwProtocol); /* dwProtocol (4 bytes) */
3111 Stream_Write_UINT32(s, cbAtrLen); /* cbAtrLen (4 bytes) */
3112 if (!smartcard_ndr_pointer_write(s, &index, cbAtrLen))
3113 return SCARD_E_NO_MEMORY;
3114 status = smartcard_ndr_write(s, ret->rgAtr, cbAtrLen, 1, NDR_PTR_SIMPLE);
3115 if (status != SCARD_S_SUCCESS)
3116 return status;
3117 return ret->ReturnCode;
3118}
3119
3120LONG smartcard_unpack_status_call(wStream* s, Status_Call* call, BOOL unicode)
3121{
3122 UINT32 index = 0;
3123 UINT32 pbContextNdrPtr = 0;
3124
3125 WINPR_ASSERT(call);
3126 wLog* log = scard_log();
3127
3128 LONG status = smartcard_unpack_redir_scard_context(log, s, &(call->handles.hContext), &index,
3129 &pbContextNdrPtr);
3130 if (status != SCARD_S_SUCCESS)
3131 return status;
3132
3133 status = smartcard_unpack_redir_scard_handle(log, s, &(call->handles.hCard), &index);
3134 if (status != SCARD_S_SUCCESS)
3135 return status;
3136
3137 if (!Stream_CheckAndLogRequiredLengthWLog(log, s, 12))
3138 return STATUS_BUFFER_TOO_SMALL;
3139
3140 Stream_Read_INT32(s, call->fmszReaderNamesIsNULL); /* fmszReaderNamesIsNULL (4 bytes) */
3141 Stream_Read_UINT32(s, call->cchReaderLen); /* cchReaderLen (4 bytes) */
3142 Stream_Read_UINT32(s, call->cbAtrLen); /* cbAtrLen (4 bytes) */
3143
3144 status = smartcard_unpack_redir_scard_context_ref(log, s, pbContextNdrPtr,
3145 &(call->handles.hContext));
3146 if (status != SCARD_S_SUCCESS)
3147 return status;
3148
3149 status = smartcard_unpack_redir_scard_handle_ref(log, s, &(call->handles.hCard));
3150 if (status != SCARD_S_SUCCESS)
3151 return status;
3152
3153 smartcard_trace_status_call(log, call, unicode);
3154 return status;
3155}
3156
3157LONG smartcard_pack_status_call(wStream* s, const Status_Call* call, BOOL unicode)
3158{
3159 WINPR_ASSERT(call);
3160 wLog* log = scard_log();
3161 UINT32 index = 0;
3162
3163 smartcard_trace_status_call(log, call, unicode);
3164
3165 LONG status = smartcard_pack_redir_scard_context(log, s, &call->handles.hContext, &index);
3166 if (status != SCARD_S_SUCCESS)
3167 return status;
3168
3169 status = smartcard_pack_redir_scard_handle(log, s, &call->handles.hCard, &index);
3170 if (status != SCARD_S_SUCCESS)
3171 return status;
3172
3173 if (!Stream_EnsureRemainingCapacity(s, 12))
3174 return SCARD_E_NO_MEMORY;
3175
3176 Stream_Write_INT32(s, call->fmszReaderNamesIsNULL);
3177 Stream_Write_UINT32(s, call->cchReaderLen);
3178 Stream_Write_UINT32(s, call->cbAtrLen);
3179
3180 status = smartcard_pack_redir_scard_context_ref(log, s, &call->handles.hContext);
3181 if (status != SCARD_S_SUCCESS)
3182 return status;
3183
3184 return smartcard_pack_redir_scard_handle_ref(log, s, &call->handles.hCard);
3185}
3186
3187LONG smartcard_pack_status_return(wStream* s, const Status_Return* ret, BOOL unicode)
3188{
3189 WINPR_ASSERT(ret);
3190 wLog* log = scard_log();
3191
3192 LONG status = 0;
3193 UINT32 index = 0;
3194 UINT32 cBytes = ret->cBytes;
3195
3196 smartcard_trace_status_return(log, ret, unicode);
3197 if (ret->ReturnCode != SCARD_S_SUCCESS)
3198 cBytes = 0;
3199 if (cBytes == SCARD_AUTOALLOCATE)
3200 cBytes = 0;
3201
3202 if (!Stream_EnsureRemainingCapacity(s, 4))
3203 return SCARD_F_INTERNAL_ERROR;
3204
3205 if (cBytes > 65536)
3206 {
3207 WLog_Print(log, WLOG_ERROR, "2.2.3.10 Status_Return cBytes(%" PRIu32 ") > 65536", cBytes);
3208 return SCARD_E_INVALID_PARAMETER;
3209 }
3210
3211 Stream_Write_UINT32(s, cBytes); /* cBytes (4 bytes) */
3212 if (!smartcard_ndr_pointer_write(s, &index, cBytes))
3213 return SCARD_E_NO_MEMORY;
3214
3215 if (!Stream_EnsureRemainingCapacity(s, 44))
3216 return SCARD_F_INTERNAL_ERROR;
3217
3218 Stream_Write_UINT32(s, ret->dwState); /* dwState (4 bytes) */
3219 Stream_Write_UINT32(s, ret->dwProtocol); /* dwProtocol (4 bytes) */
3220 Stream_Write(s, ret->pbAtr, sizeof(ret->pbAtr)); /* pbAtr (32 bytes) */
3221
3222 if (ret->cbAtrLen > 32)
3223 {
3224 WLog_Print(log, WLOG_ERROR, "2.2.3.10 Status_Return cbAtrLen(%" PRIu32 ") > 32",
3225 ret->cbAtrLen);
3226 return SCARD_E_INVALID_PARAMETER;
3227 }
3228
3229 Stream_Write_UINT32(s, ret->cbAtrLen); /* cbAtrLen (4 bytes) */
3230 status = smartcard_ndr_write(s, ret->mszReaderNames, cBytes, 1, NDR_PTR_SIMPLE);
3231 if (status != SCARD_S_SUCCESS)
3232 return status;
3233 return ret->ReturnCode;
3234}
3235
3236LONG smartcard_unpack_status_return(wStream* s, Status_Return* ret, BOOL unicode)
3237{
3238 WINPR_ASSERT(ret);
3239 wLog* log = scard_log();
3240 UINT32 index = 0;
3241 UINT32 mszNdrPtr = 0;
3242
3243 if (!Stream_CheckAndLogRequiredLengthWLog(log, s, 4))
3244 return STATUS_BUFFER_TOO_SMALL;
3245
3246 const UINT32 cBytes = Stream_Get_UINT32(s);
3247 if (cBytes > 65536)
3248 {
3249 WLog_Print(log, WLOG_ERROR, "2.2.3.10 Status_Return cBytes(%" PRIu32 ") > 65536", cBytes);
3250 return SCARD_E_INVALID_PARAMETER;
3251 }
3252
3253 if (!smartcard_ndr_pointer_read(log, s, &index, &mszNdrPtr))
3254 return ERROR_INVALID_DATA;
3255
3256 if (!Stream_CheckAndLogRequiredLengthWLog(log, s, 44))
3257 return STATUS_BUFFER_TOO_SMALL;
3258
3259 Stream_Read_UINT32(s, ret->dwState);
3260 Stream_Read_UINT32(s, ret->dwProtocol);
3261 Stream_Read(s, ret->pbAtr, sizeof(ret->pbAtr));
3262 Stream_Read_UINT32(s, ret->cbAtrLen);
3263
3264 if (ret->cbAtrLen > ARRAYSIZE(ret->pbAtr))
3265 {
3266 WLog_Print(log, WLOG_ERROR, "Status_Return::cbAtrLen %" PRIu32 " exceeds %" PRIuz,
3267 ret->cbAtrLen, ARRAYSIZE(ret->pbAtr));
3268 return SCARD_E_INVALID_ATR;
3269 }
3270
3271 if (mszNdrPtr)
3272 {
3273 LONG status = smartcard_ndr_read(log, s, &ret->mszReaderNames, cBytes, 1, NDR_PTR_SIMPLE);
3274 if (status != SCARD_S_SUCCESS)
3275 return status;
3276 ret->cBytes = cBytes;
3277 }
3278
3279 smartcard_trace_status_return(log, ret, unicode);
3280 return SCARD_S_SUCCESS;
3281}
3282
3283LONG smartcard_unpack_get_attrib_call(wStream* s, GetAttrib_Call* call)
3284{
3285 WINPR_ASSERT(call);
3286 wLog* log = scard_log();
3287 UINT32 index = 0;
3288 UINT32 pbContextNdrPtr = 0;
3289
3290 LONG status = smartcard_unpack_redir_scard_context(log, s, &(call->handles.hContext), &index,
3291 &pbContextNdrPtr);
3292 if (status != SCARD_S_SUCCESS)
3293 return status;
3294
3295 status = smartcard_unpack_redir_scard_handle(log, s, &(call->handles.hCard), &index);
3296 if (status != SCARD_S_SUCCESS)
3297 return status;
3298
3299 if (!Stream_CheckAndLogRequiredLengthWLog(log, s, 12))
3300 return STATUS_BUFFER_TOO_SMALL;
3301
3302 Stream_Read_UINT32(s, call->dwAttrId); /* dwAttrId (4 bytes) */
3303 Stream_Read_INT32(s, call->fpbAttrIsNULL); /* fpbAttrIsNULL (4 bytes) */
3304 Stream_Read_UINT32(s, call->cbAttrLen); /* cbAttrLen (4 bytes) */
3305
3306 status = smartcard_unpack_redir_scard_context_ref(log, s, pbContextNdrPtr,
3307 &(call->handles.hContext));
3308 if (status != SCARD_S_SUCCESS)
3309 return status;
3310
3311 status = smartcard_unpack_redir_scard_handle_ref(log, s, &(call->handles.hCard));
3312 if (status != SCARD_S_SUCCESS)
3313 return status;
3314
3315 smartcard_trace_get_attrib_call(log, call);
3316 return status;
3317}
3318
3319LONG smartcard_pack_get_attrib_return(wStream* s, const GetAttrib_Return* ret, DWORD dwAttrId,
3320 DWORD cbAttrCallLen)
3321{
3322 WINPR_ASSERT(ret);
3323 wLog* log = scard_log();
3324 LONG status = 0;
3325 UINT32 cbAttrLen = 0;
3326 UINT32 index = 0;
3327 smartcard_trace_get_attrib_return(log, ret, dwAttrId);
3328
3329 if (!Stream_EnsureRemainingCapacity(s, 4))
3330 return SCARD_F_INTERNAL_ERROR;
3331
3332 cbAttrLen = ret->cbAttrLen;
3333 if (ret->ReturnCode != SCARD_S_SUCCESS)
3334 cbAttrLen = 0;
3335 if (cbAttrLen == SCARD_AUTOALLOCATE)
3336 cbAttrLen = 0;
3337
3338 if (ret->pbAttr)
3339 {
3340 if (cbAttrCallLen < cbAttrLen)
3341 cbAttrLen = cbAttrCallLen;
3342 }
3343 if (cbAttrLen > 65536)
3344 {
3345 WLog_Print(log, WLOG_ERROR, "2.2.3.12 GetAttrib_Return cbAttrLen(%" PRIu32 ") > 65536",
3346 cbAttrLen);
3347 return SCARD_E_INVALID_PARAMETER;
3348 }
3349 Stream_Write_UINT32(s, cbAttrLen); /* cbAttrLen (4 bytes) */
3350 if (!smartcard_ndr_pointer_write(s, &index, cbAttrLen))
3351 return SCARD_E_NO_MEMORY;
3352
3353 status = smartcard_ndr_write(s, ret->pbAttr, cbAttrLen, 1, NDR_PTR_SIMPLE);
3354 if (status != SCARD_S_SUCCESS)
3355 return status;
3356 return ret->ReturnCode;
3357}
3358
3359LONG smartcard_unpack_control_call(wStream* s, Control_Call* call)
3360{
3361 WINPR_ASSERT(call);
3362 wLog* log = scard_log();
3363
3364 UINT32 index = 0;
3365 UINT32 pvInBufferNdrPtr = 0;
3366 UINT32 pbContextNdrPtr = 0;
3367
3368 call->pvInBuffer = nullptr;
3369
3370 LONG status = smartcard_unpack_redir_scard_context(log, s, &(call->handles.hContext), &index,
3371 &pbContextNdrPtr);
3372 if (status != SCARD_S_SUCCESS)
3373 return status;
3374
3375 status = smartcard_unpack_redir_scard_handle(log, s, &(call->handles.hCard), &index);
3376 if (status != SCARD_S_SUCCESS)
3377 return status;
3378
3379 if (!Stream_CheckAndLogRequiredLengthWLog(log, s, 20))
3380 return STATUS_BUFFER_TOO_SMALL;
3381
3382 Stream_Read_UINT32(s, call->dwControlCode); /* dwControlCode (4 bytes) */
3383 const UINT32 cbInBufferSize = Stream_Get_UINT32(s); /* cbInBufferSize (4 bytes) */
3384 if (cbInBufferSize > 66560)
3385 {
3386 WLog_Print(log, WLOG_ERROR, "2.2.2.20 Control_Call cbInBufferSize (%" PRIu32 ") > 66560",
3387 cbInBufferSize);
3388 return SCARD_E_INVALID_PARAMETER;
3389 }
3390
3391 if (!smartcard_ndr_pointer_read(log, s, &index,
3392 &pvInBufferNdrPtr)) /* pvInBufferNdrPtr (4 bytes) */
3393 return ERROR_INVALID_DATA;
3394 Stream_Read_INT32(s, call->fpvOutBufferIsNULL); /* fpvOutBufferIsNULL (4 bytes) */
3395 Stream_Read_UINT32(s, call->cbOutBufferSize); /* cbOutBufferSize (4 bytes) */
3396
3397 status = smartcard_unpack_redir_scard_context_ref(log, s, pbContextNdrPtr,
3398 &(call->handles.hContext));
3399 if (status != SCARD_S_SUCCESS)
3400 return status;
3401
3402 status = smartcard_unpack_redir_scard_handle_ref(log, s, &(call->handles.hCard));
3403 if (status != SCARD_S_SUCCESS)
3404 return status;
3405
3406 if (pvInBufferNdrPtr)
3407 {
3408 status = smartcard_ndr_read(log, s, &call->pvInBuffer, cbInBufferSize, 1, NDR_PTR_SIMPLE);
3409 if (status != SCARD_S_SUCCESS)
3410 return status;
3411 call->cbInBufferSize = cbInBufferSize;
3412 }
3413
3414 smartcard_trace_control_call(log, call);
3415 return SCARD_S_SUCCESS;
3416}
3417
3418LONG smartcard_pack_control_return(wStream* s, const Control_Return* ret)
3419{
3420 WINPR_ASSERT(ret);
3421 wLog* log = scard_log();
3422
3423 LONG status = 0;
3424 UINT32 cbDataLen = ret->cbOutBufferSize;
3425 UINT32 index = 0;
3426
3427 smartcard_trace_control_return(log, ret);
3428 if (ret->ReturnCode != SCARD_S_SUCCESS)
3429 cbDataLen = 0;
3430 if (cbDataLen == SCARD_AUTOALLOCATE)
3431 cbDataLen = 0;
3432
3433 if (!Stream_EnsureRemainingCapacity(s, 4))
3434 return SCARD_F_INTERNAL_ERROR;
3435
3436 if (cbDataLen > 66560)
3437 {
3438 WLog_Print(log, WLOG_ERROR, "2.2.3.6 Control_Return cbOutBufferSize(%" PRIu32 ") > 66560",
3439 cbDataLen);
3440 return SCARD_E_INVALID_PARAMETER;
3441 }
3442
3443 Stream_Write_UINT32(s, cbDataLen); /* cbOutBufferSize (4 bytes) */
3444 if (!smartcard_ndr_pointer_write(s, &index, cbDataLen))
3445 return SCARD_E_NO_MEMORY;
3446
3447 status = smartcard_ndr_write(s, ret->pvOutBuffer, cbDataLen, 1, NDR_PTR_SIMPLE);
3448 if (status != SCARD_S_SUCCESS)
3449 return status;
3450 return ret->ReturnCode;
3451}
3452
3453LONG smartcard_unpack_transmit_call(wStream* s, Transmit_Call* call)
3454{
3455 UINT32 length = 0;
3456 BYTE* pbExtraBytes = nullptr;
3457 UINT32 pbExtraBytesNdrPtr = 0;
3458 UINT32 pbSendBufferNdrPtr = 0;
3459 UINT32 pioRecvPciNdrPtr = 0;
3460 SCardIO_Request ioSendPci;
3461 SCardIO_Request ioRecvPci;
3462 UINT32 index = 0;
3463 UINT32 pbContextNdrPtr = 0;
3464
3465 WINPR_ASSERT(call);
3466 wLog* log = scard_log();
3467
3468 call->pioSendPci = nullptr;
3469 call->pioRecvPci = nullptr;
3470 call->pbSendBuffer = nullptr;
3471
3472 LONG status = smartcard_unpack_redir_scard_context(log, s, &(call->handles.hContext), &index,
3473 &pbContextNdrPtr);
3474 if (status != SCARD_S_SUCCESS)
3475 return status;
3476
3477 status = smartcard_unpack_redir_scard_handle(log, s, &(call->handles.hCard), &index);
3478 if (status != SCARD_S_SUCCESS)
3479 return status;
3480
3481 if (!Stream_CheckAndLogRequiredLengthWLog(log, s, 32))
3482 return STATUS_BUFFER_TOO_SMALL;
3483
3484 Stream_Read_UINT32(s, ioSendPci.dwProtocol); /* dwProtocol (4 bytes) */
3485 Stream_Read_UINT32(s, ioSendPci.cbExtraBytes); /* cbExtraBytes (4 bytes) */
3486 if (!smartcard_ndr_pointer_read(log, s, &index,
3487 &pbExtraBytesNdrPtr)) /* pbExtraBytesNdrPtr (4 bytes) */
3488 return ERROR_INVALID_DATA;
3489
3490 const UINT32 cbSendLength = Stream_Get_UINT32(s); /* cbSendLength (4 bytes) */
3491 if (cbSendLength > 66560)
3492 {
3493 WLog_Print(log, WLOG_ERROR, "2.2.2.19 Transmit_Call cbSendLength (%" PRIu32 ") > 66560",
3494 cbSendLength);
3495 return SCARD_E_INVALID_PARAMETER;
3496 }
3497 if (!smartcard_ndr_pointer_read(log, s, &index,
3498 &pbSendBufferNdrPtr)) /* pbSendBufferNdrPtr (4 bytes) */
3499 return ERROR_INVALID_DATA;
3500
3501 if (!smartcard_ndr_pointer_read(log, s, &index,
3502 &pioRecvPciNdrPtr)) /* pioRecvPciNdrPtr (4 bytes) */
3503 return ERROR_INVALID_DATA;
3504
3505 Stream_Read_INT32(s, call->fpbRecvBufferIsNULL); /* fpbRecvBufferIsNULL (4 bytes) */
3506 Stream_Read_UINT32(s, call->cbRecvLength); /* cbRecvLength (4 bytes) */
3507
3508 if (ioSendPci.cbExtraBytes > 1024)
3509 {
3510 WLog_Print(log, WLOG_WARN,
3511 "Transmit_Call ioSendPci.cbExtraBytes is out of bounds: %" PRIu32 " (max: 1024)",
3512 ioSendPci.cbExtraBytes);
3513 return STATUS_INVALID_PARAMETER;
3514 }
3515
3516 if (cbSendLength > 66560)
3517 {
3518 WLog_Print(log, WLOG_WARN,
3519 "Transmit_Call cbSendLength is out of bounds: %" PRIu32 " (max: 66560)",
3520 ioSendPci.cbExtraBytes);
3521 return STATUS_INVALID_PARAMETER;
3522 }
3523
3524 status = smartcard_unpack_redir_scard_context_ref(log, s, pbContextNdrPtr,
3525 &(call->handles.hContext));
3526 if (status != SCARD_S_SUCCESS)
3527 return status;
3528
3529 status = smartcard_unpack_redir_scard_handle_ref(log, s, &(call->handles.hCard));
3530 if (status != SCARD_S_SUCCESS)
3531 return status;
3532
3533 if (ioSendPci.cbExtraBytes && !pbExtraBytesNdrPtr)
3534 {
3535 WLog_Print(
3536 log, WLOG_WARN,
3537 "Transmit_Call ioSendPci.cbExtraBytes is non-zero but pbExtraBytesNdrPtr is null");
3538 return STATUS_INVALID_PARAMETER;
3539 }
3540
3541 if (pbExtraBytesNdrPtr)
3542 {
3543 // TODO: Use unified pointer reading
3544 if (!Stream_CheckAndLogRequiredLengthWLog(log, s, 4))
3545 return STATUS_BUFFER_TOO_SMALL;
3546
3547 Stream_Read_UINT32(s, length); /* Length (4 bytes) */
3548
3549 if (!Stream_CheckAndLogRequiredLengthWLog(log, s, ioSendPci.cbExtraBytes))
3550 return STATUS_BUFFER_TOO_SMALL;
3551
3552 ioSendPci.pbExtraBytes = Stream_Pointer(s);
3553 call->pioSendPci =
3554 (LPSCARD_IO_REQUEST)malloc(sizeof(SCARD_IO_REQUEST) + ioSendPci.cbExtraBytes);
3555
3556 if (!call->pioSendPci)
3557 {
3558 WLog_Print(log, WLOG_WARN, "Transmit_Call out of memory error (pioSendPci)");
3559 return STATUS_NO_MEMORY;
3560 }
3561
3562 call->pioSendPci->dwProtocol = ioSendPci.dwProtocol;
3563 call->pioSendPci->cbPciLength = (DWORD)(ioSendPci.cbExtraBytes + sizeof(SCARD_IO_REQUEST));
3564 pbExtraBytes = &((BYTE*)call->pioSendPci)[sizeof(SCARD_IO_REQUEST)];
3565 Stream_Read(s, pbExtraBytes, ioSendPci.cbExtraBytes);
3566 if (smartcard_unpack_read_size_align(s, ioSendPci.cbExtraBytes, 4) < 0)
3567 return STATUS_INVALID_PARAMETER;
3568 }
3569 else
3570 {
3571 call->pioSendPci = (LPSCARD_IO_REQUEST)calloc(1, sizeof(SCARD_IO_REQUEST));
3572
3573 if (!call->pioSendPci)
3574 {
3575 WLog_Print(log, WLOG_WARN, "Transmit_Call out of memory error (pioSendPci)");
3576 return STATUS_NO_MEMORY;
3577 }
3578
3579 call->pioSendPci->dwProtocol = ioSendPci.dwProtocol;
3580 call->pioSendPci->cbPciLength = sizeof(SCARD_IO_REQUEST);
3581 }
3582
3583 if (pbSendBufferNdrPtr)
3584 {
3585 status = smartcard_ndr_read(log, s, &call->pbSendBuffer, cbSendLength, 1, NDR_PTR_SIMPLE);
3586 if (status != SCARD_S_SUCCESS)
3587 return status;
3588 call->cbSendLength = cbSendLength;
3589 }
3590
3591 if (pioRecvPciNdrPtr)
3592 {
3593 if (!Stream_CheckAndLogRequiredLengthWLog(log, s, 12))
3594 return STATUS_BUFFER_TOO_SMALL;
3595
3596 Stream_Read_UINT32(s, ioRecvPci.dwProtocol); /* dwProtocol (4 bytes) */
3597 Stream_Read_UINT32(s, ioRecvPci.cbExtraBytes); /* cbExtraBytes (4 bytes) */
3598 if (!smartcard_ndr_pointer_read(log, s, &index,
3599 &pbExtraBytesNdrPtr)) /* pbExtraBytesNdrPtr (4 bytes) */
3600 return ERROR_INVALID_DATA;
3601
3602 if (ioRecvPci.cbExtraBytes && !pbExtraBytesNdrPtr)
3603 {
3604 WLog_Print(
3605 log, WLOG_WARN,
3606 "Transmit_Call ioRecvPci.cbExtraBytes is non-zero but pbExtraBytesNdrPtr is null");
3607 return STATUS_INVALID_PARAMETER;
3608 }
3609
3610 if (pbExtraBytesNdrPtr)
3611 {
3612 // TODO: Unify ndr pointer reading
3613 if (!Stream_CheckAndLogRequiredLengthWLog(log, s, 4))
3614 return STATUS_BUFFER_TOO_SMALL;
3615
3616 Stream_Read_UINT32(s, length); /* Length (4 bytes) */
3617
3618 if (ioRecvPci.cbExtraBytes > 1024)
3619 {
3620 WLog_Print(log, WLOG_WARN,
3621 "Transmit_Call ioRecvPci.cbExtraBytes is out of bounds: %" PRIu32
3622 " (max: 1024)",
3623 ioRecvPci.cbExtraBytes);
3624 return STATUS_INVALID_PARAMETER;
3625 }
3626
3627 if (length != ioRecvPci.cbExtraBytes)
3628 {
3629 WLog_Print(log, WLOG_WARN,
3630 "Transmit_Call unexpected length: Actual: %" PRIu32
3631 ", Expected: %" PRIu32 " (ioRecvPci.cbExtraBytes)",
3632 length, ioRecvPci.cbExtraBytes);
3633 return STATUS_INVALID_PARAMETER;
3634 }
3635
3636 if (!Stream_CheckAndLogRequiredLengthWLog(log, s, ioRecvPci.cbExtraBytes))
3637 return STATUS_BUFFER_TOO_SMALL;
3638
3639 ioRecvPci.pbExtraBytes = Stream_Pointer(s);
3640 call->pioRecvPci =
3641 (LPSCARD_IO_REQUEST)malloc(sizeof(SCARD_IO_REQUEST) + ioRecvPci.cbExtraBytes);
3642
3643 if (!call->pioRecvPci)
3644 {
3645 WLog_Print(log, WLOG_WARN, "Transmit_Call out of memory error (pioRecvPci)");
3646 return STATUS_NO_MEMORY;
3647 }
3648
3649 call->pioRecvPci->dwProtocol = ioRecvPci.dwProtocol;
3650 call->pioRecvPci->cbPciLength =
3651 (DWORD)(ioRecvPci.cbExtraBytes + sizeof(SCARD_IO_REQUEST));
3652 pbExtraBytes = &((BYTE*)call->pioRecvPci)[sizeof(SCARD_IO_REQUEST)];
3653 Stream_Read(s, pbExtraBytes, ioRecvPci.cbExtraBytes);
3654 if (smartcard_unpack_read_size_align(s, ioRecvPci.cbExtraBytes, 4) < 0)
3655 return STATUS_INVALID_PARAMETER;
3656 }
3657 else
3658 {
3659 call->pioRecvPci = (LPSCARD_IO_REQUEST)calloc(1, sizeof(SCARD_IO_REQUEST));
3660
3661 if (!call->pioRecvPci)
3662 {
3663 WLog_Print(log, WLOG_WARN, "Transmit_Call out of memory error (pioRecvPci)");
3664 return STATUS_NO_MEMORY;
3665 }
3666
3667 call->pioRecvPci->dwProtocol = ioRecvPci.dwProtocol;
3668 call->pioRecvPci->cbPciLength = sizeof(SCARD_IO_REQUEST);
3669 }
3670 }
3671
3672 smartcard_trace_transmit_call(log, call);
3673 return SCARD_S_SUCCESS;
3674}
3675
3676LONG smartcard_pack_transmit_return(wStream* s, const Transmit_Return* ret)
3677{
3678 WINPR_ASSERT(ret);
3679 wLog* log = scard_log();
3680
3681 LONG status = 0;
3682 UINT32 index = 0;
3683 LONG error = 0;
3684 UINT32 cbRecvLength = ret->cbRecvLength;
3685 UINT32 cbRecvPci = ret->pioRecvPci ? ret->pioRecvPci->cbPciLength : 0;
3686
3687 smartcard_trace_transmit_return(log, ret);
3688
3689 if (!ret->pbRecvBuffer)
3690 cbRecvLength = 0;
3691
3692 if (!smartcard_ndr_pointer_write(s, &index, cbRecvPci))
3693 return SCARD_E_NO_MEMORY;
3694 if (!Stream_EnsureRemainingCapacity(s, 4))
3695 return SCARD_E_NO_MEMORY;
3696
3697 if (cbRecvLength > 66560)
3698 {
3699 WLog_Print(log, WLOG_ERROR, "2.2.3.11 Transmit_Return cbRecvLength(%" PRIu32 ") > 66560",
3700 cbRecvLength);
3701 return SCARD_E_INVALID_PARAMETER;
3702 }
3703
3704 Stream_Write_UINT32(s, cbRecvLength); /* cbRecvLength (4 bytes) */
3705 if (!smartcard_ndr_pointer_write(s, &index, cbRecvLength))
3706 return SCARD_E_NO_MEMORY;
3707
3708 if (ret->pioRecvPci)
3709 {
3710 UINT32 cbExtraBytes = (UINT32)(ret->pioRecvPci->cbPciLength - sizeof(SCARD_IO_REQUEST));
3711 BYTE* pbExtraBytes = &((BYTE*)ret->pioRecvPci)[sizeof(SCARD_IO_REQUEST)];
3712
3713 if (!Stream_EnsureRemainingCapacity(s, 8ull))
3714 {
3715 WLog_Print(log, WLOG_ERROR, "Stream_EnsureRemainingCapacity failed!");
3716 return SCARD_F_INTERNAL_ERROR;
3717 }
3718
3719 Stream_Write_UINT32(s, ret->pioRecvPci->dwProtocol); /* dwProtocol (4 bytes) */
3720 Stream_Write_UINT32(s, cbExtraBytes); /* cbExtraBytes (4 bytes) */
3721 if (!smartcard_ndr_pointer_write(s, &index, cbExtraBytes))
3722 return SCARD_E_NO_MEMORY;
3723 error = smartcard_ndr_write(s, pbExtraBytes, cbExtraBytes, 1, NDR_PTR_SIMPLE);
3724 if (error)
3725 return error;
3726 }
3727
3728 status = smartcard_ndr_write(s, ret->pbRecvBuffer, ret->cbRecvLength, 1, NDR_PTR_SIMPLE);
3729 if (status != SCARD_S_SUCCESS)
3730 return status;
3731 return ret->ReturnCode;
3732}
3733
3734LONG smartcard_unpack_locate_cards_by_atr_a_call(wStream* s, LocateCardsByATRA_Call* call)
3735{
3736 UINT32 rgReaderStatesNdrPtr = 0;
3737 UINT32 rgAtrMasksNdrPtr = 0;
3738 UINT32 index = 0;
3739 UINT32 pbContextNdrPtr = 0;
3740
3741 WINPR_ASSERT(call);
3742 wLog* log = scard_log();
3743
3744 call->rgReaderStates = nullptr;
3745
3746 LONG status = smartcard_unpack_redir_scard_context(log, s, &(call->handles.hContext), &index,
3747 &pbContextNdrPtr);
3748 if (status != SCARD_S_SUCCESS)
3749 return status;
3750
3751 if (!Stream_CheckAndLogRequiredLengthWLog(log, s, 16))
3752 return STATUS_BUFFER_TOO_SMALL;
3753
3754 Stream_Read_UINT32(s, call->cAtrs);
3755 if (call->cAtrs > 1000)
3756 {
3757 WLog_Print(log, WLOG_ERROR, "2.2.2.23 LocateCardsByATRA_Call cAtrs(%" PRIu32 ") > 1000",
3758 call->cAtrs);
3759 return SCARD_E_INVALID_PARAMETER;
3760 }
3761
3762 if (!smartcard_ndr_pointer_read(log, s, &index, &rgAtrMasksNdrPtr))
3763 return ERROR_INVALID_DATA;
3764 const UINT32 cReaders = Stream_Get_UINT32(s); /* cReaders (4 bytes) */
3765 if (cReaders > 10)
3766 {
3767 WLog_Print(log, WLOG_ERROR, "2.2.2.23 LocateCardsByATRA_Call cReaders(%" PRIu32 ") > 10",
3768 cReaders);
3769 return SCARD_E_INVALID_PARAMETER;
3770 }
3771 if (!smartcard_ndr_pointer_read(log, s, &index, &rgReaderStatesNdrPtr))
3772 return ERROR_INVALID_DATA;
3773
3774 status = smartcard_unpack_redir_scard_context_ref(log, s, pbContextNdrPtr,
3775 &(call->handles.hContext));
3776 if (status != SCARD_S_SUCCESS)
3777 return status;
3778
3779 if ((rgAtrMasksNdrPtr && !call->cAtrs) || (!rgAtrMasksNdrPtr && call->cAtrs))
3780 {
3781 WLog_Print(log, WLOG_WARN,
3782 "LocateCardsByATRA_Call rgAtrMasksNdrPtr (0x%08" PRIX32
3783 ") and cAtrs (0x%08" PRIX32 ") inconsistency",
3784 rgAtrMasksNdrPtr, call->cAtrs);
3785 return STATUS_INVALID_PARAMETER;
3786 }
3787
3788 if (rgAtrMasksNdrPtr)
3789 {
3790 status = smartcard_ndr_read_atrmask(log, s, &call->rgAtrMasks, call->cAtrs, NDR_PTR_SIMPLE);
3791 if (status != SCARD_S_SUCCESS)
3792 return status;
3793 }
3794
3795 if (rgReaderStatesNdrPtr)
3796 {
3797 status = smartcard_unpack_reader_state_a(log, s, &call->rgReaderStates, cReaders, &index);
3798 if (status != SCARD_S_SUCCESS)
3799 return status;
3800 call->cReaders = cReaders;
3801 }
3802
3803 smartcard_trace_locate_cards_by_atr_a_call(log, call);
3804 return SCARD_S_SUCCESS;
3805}
3806
3807LONG smartcard_unpack_context_and_two_strings_a_call(wStream* s, ContextAndTwoStringA_Call* call)
3808{
3809 UINT32 sz1NdrPtr = 0;
3810 UINT32 sz2NdrPtr = 0;
3811 UINT32 index = 0;
3812 UINT32 pbContextNdrPtr = 0;
3813
3814 WINPR_ASSERT(call);
3815 wLog* log = scard_log();
3816
3817 LONG status = smartcard_unpack_redir_scard_context(log, s, &(call->handles.hContext), &index,
3818 &pbContextNdrPtr);
3819 if (status != SCARD_S_SUCCESS)
3820 return status;
3821
3822 if (!smartcard_ndr_pointer_read(log, s, &index, &sz1NdrPtr))
3823 return ERROR_INVALID_DATA;
3824 if (!smartcard_ndr_pointer_read(log, s, &index, &sz2NdrPtr))
3825 return ERROR_INVALID_DATA;
3826
3827 status =
3828 smartcard_unpack_redir_scard_context_ref(log, s, pbContextNdrPtr, &call->handles.hContext);
3829 if (status != SCARD_S_SUCCESS)
3830 return status;
3831
3832 if (sz1NdrPtr)
3833 {
3834 status = smartcard_ndr_read_a(log, s, &call->sz1, NDR_PTR_FULL);
3835 if (status != SCARD_S_SUCCESS)
3836 return status;
3837 }
3838 if (sz2NdrPtr)
3839 {
3840 status = smartcard_ndr_read_a(log, s, &call->sz2, NDR_PTR_FULL);
3841 if (status != SCARD_S_SUCCESS)
3842 return status;
3843 }
3844 smartcard_trace_context_and_two_strings_a_call(log, call);
3845 return SCARD_S_SUCCESS;
3846}
3847
3848LONG smartcard_unpack_context_and_two_strings_w_call(wStream* s, ContextAndTwoStringW_Call* call)
3849{
3850 UINT32 sz1NdrPtr = 0;
3851 UINT32 sz2NdrPtr = 0;
3852 UINT32 index = 0;
3853 UINT32 pbContextNdrPtr = 0;
3854
3855 WINPR_ASSERT(call);
3856 wLog* log = scard_log();
3857
3858 LONG status = smartcard_unpack_redir_scard_context(log, s, &(call->handles.hContext), &index,
3859 &pbContextNdrPtr);
3860 if (status != SCARD_S_SUCCESS)
3861 return status;
3862
3863 if (!smartcard_ndr_pointer_read(log, s, &index, &sz1NdrPtr))
3864 return ERROR_INVALID_DATA;
3865 if (!smartcard_ndr_pointer_read(log, s, &index, &sz2NdrPtr))
3866 return ERROR_INVALID_DATA;
3867
3868 status =
3869 smartcard_unpack_redir_scard_context_ref(log, s, pbContextNdrPtr, &call->handles.hContext);
3870 if (status != SCARD_S_SUCCESS)
3871 return status;
3872
3873 if (sz1NdrPtr)
3874 {
3875 status = smartcard_ndr_read_w(log, s, &call->sz1, NDR_PTR_FULL);
3876 if (status != SCARD_S_SUCCESS)
3877 return status;
3878 }
3879 if (sz2NdrPtr)
3880 {
3881 status = smartcard_ndr_read_w(log, s, &call->sz2, NDR_PTR_FULL);
3882 if (status != SCARD_S_SUCCESS)
3883 return status;
3884 }
3885 smartcard_trace_context_and_two_strings_w_call(log, call);
3886 return SCARD_S_SUCCESS;
3887}
3888
3889LONG smartcard_unpack_locate_cards_a_call(wStream* s, LocateCardsA_Call* call)
3890{
3891 UINT32 sz1NdrPtr = 0;
3892 UINT32 sz2NdrPtr = 0;
3893 UINT32 index = 0;
3894 UINT32 pbContextNdrPtr = 0;
3895
3896 WINPR_ASSERT(call);
3897 wLog* log = scard_log();
3898
3899 LONG status = smartcard_unpack_redir_scard_context(log, s, &(call->handles.hContext), &index,
3900 &pbContextNdrPtr);
3901 if (status != SCARD_S_SUCCESS)
3902 return status;
3903
3904 if (!Stream_CheckAndLogRequiredLengthWLog(log, s, 16))
3905 return STATUS_BUFFER_TOO_SMALL;
3906
3907 const UINT32 cBytes = Stream_Get_UINT32(s);
3908 if (call->cBytes > 65536)
3909 {
3910 WLog_Print(log, WLOG_ERROR, "2.2.2.9 LocateCardsA_Call cBytes(%" PRIu32 ") > 65536",
3911 call->cBytes);
3912 return SCARD_E_INVALID_PARAMETER;
3913 }
3914
3915 if (!smartcard_ndr_pointer_read(log, s, &index, &sz1NdrPtr))
3916 return ERROR_INVALID_DATA;
3917
3918 const UINT32 cReaders = Stream_Get_UINT32(s);
3919 if (cReaders > 10)
3920 {
3921 WLog_Print(log, WLOG_ERROR, "2.2.2.9 LocateCardsA_Call cReaders(%" PRIu32 ") > 10",
3922 cReaders);
3923 return SCARD_E_INVALID_PARAMETER;
3924 }
3925 if (!smartcard_ndr_pointer_read(log, s, &index, &sz2NdrPtr))
3926 return ERROR_INVALID_DATA;
3927
3928 status = smartcard_unpack_redir_scard_context_ref(log, s, pbContextNdrPtr,
3929 &(call->handles.hContext));
3930 if (status != SCARD_S_SUCCESS)
3931 return status;
3932
3933 if (sz1NdrPtr)
3934 {
3935 status = smartcard_ndr_read_fixed_string_a(log, s, &call->mszCards, cBytes, NDR_PTR_SIMPLE);
3936 if (status != SCARD_S_SUCCESS)
3937 return status;
3938 call->cBytes = cBytes;
3939 }
3940 if (sz2NdrPtr)
3941 {
3942 status = smartcard_unpack_reader_state_a(log, s, &call->rgReaderStates, cReaders, &index);
3943 if (status != SCARD_S_SUCCESS)
3944 return status;
3945 call->cReaders = cReaders;
3946 }
3947 smartcard_trace_locate_cards_a_call(log, call);
3948 return SCARD_S_SUCCESS;
3949}
3950
3951LONG smartcard_unpack_locate_cards_w_call(wStream* s, LocateCardsW_Call* call)
3952{
3953 UINT32 sz1NdrPtr = 0;
3954 UINT32 sz2NdrPtr = 0;
3955 UINT32 index = 0;
3956 UINT32 pbContextNdrPtr = 0;
3957
3958 WINPR_ASSERT(call);
3959 wLog* log = scard_log();
3960
3961 LONG status = smartcard_unpack_redir_scard_context(log, s, &(call->handles.hContext), &index,
3962 &pbContextNdrPtr);
3963 if (status != SCARD_S_SUCCESS)
3964 return status;
3965
3966 if (!Stream_CheckAndLogRequiredLengthWLog(log, s, 16))
3967 return STATUS_BUFFER_TOO_SMALL;
3968
3969 const UINT32 cBytes = Stream_Get_UINT32(s);
3970 if (call->cBytes > 65536)
3971 {
3972 WLog_Print(log, WLOG_ERROR, "2.2.2.10 LocateCardsW_Call cBytes(%" PRIu32 ") > 65536",
3973 call->cBytes);
3974 return SCARD_E_INVALID_PARAMETER;
3975 }
3976
3977 if (!smartcard_ndr_pointer_read(log, s, &index, &sz1NdrPtr))
3978 return ERROR_INVALID_DATA;
3979
3980 const UINT32 cReaders = Stream_Get_UINT32(s);
3981 if (cReaders > 10)
3982 {
3983 WLog_Print(log, WLOG_ERROR, "2.2.2.10 LocateCardsW_Call cReaders(%" PRIu32 ") > 10",
3984 cReaders);
3985 return SCARD_E_INVALID_PARAMETER;
3986 }
3987
3988 if (!smartcard_ndr_pointer_read(log, s, &index, &sz2NdrPtr))
3989 return ERROR_INVALID_DATA;
3990
3991 status = smartcard_unpack_redir_scard_context_ref(log, s, pbContextNdrPtr,
3992 &(call->handles.hContext));
3993 if (status != SCARD_S_SUCCESS)
3994 return status;
3995
3996 if (sz1NdrPtr)
3997 {
3998 status = smartcard_ndr_read_fixed_string_w(log, s, &call->mszCards, cBytes, NDR_PTR_SIMPLE);
3999 if (status != SCARD_S_SUCCESS)
4000 return status;
4001 call->cBytes = cBytes;
4002 }
4003 if (sz2NdrPtr)
4004 {
4005 status = smartcard_unpack_reader_state_w(log, s, &call->rgReaderStates, cReaders, &index);
4006 if (status != SCARD_S_SUCCESS)
4007 return status;
4008 call->cReaders = cReaders;
4009 }
4010 smartcard_trace_locate_cards_w_call(log, call);
4011 return SCARD_S_SUCCESS;
4012}
4013
4014LONG smartcard_unpack_set_attrib_call(wStream* s, SetAttrib_Call* call)
4015{
4016 UINT32 index = 0;
4017 UINT32 ndrPtr = 0;
4018 UINT32 pbContextNdrPtr = 0;
4019
4020 WINPR_ASSERT(call);
4021 wLog* log = scard_log();
4022
4023 LONG status = smartcard_unpack_redir_scard_context(log, s, &(call->handles.hContext), &index,
4024 &pbContextNdrPtr);
4025 if (status != SCARD_S_SUCCESS)
4026 return status;
4027 status = smartcard_unpack_redir_scard_handle(log, s, &(call->handles.hCard), &index);
4028 if (status != SCARD_S_SUCCESS)
4029 return status;
4030
4031 if (!Stream_CheckAndLogRequiredLengthWLog(log, s, 12))
4032 return STATUS_BUFFER_TOO_SMALL;
4033 Stream_Read_UINT32(s, call->dwAttrId);
4034 Stream_Read_UINT32(s, call->cbAttrLen);
4035 if (call->cbAttrLen > 65536)
4036 {
4037 WLog_Print(log, WLOG_ERROR, "2.2.2.22 SetAttrib_Call call->cbAttrLen (%" PRIu32 ") > 65536",
4038 call->cbAttrLen);
4039 return SCARD_E_INVALID_PARAMETER;
4040 }
4041
4042 if (!smartcard_ndr_pointer_read(log, s, &index, &ndrPtr))
4043 return ERROR_INVALID_DATA;
4044
4045 status = smartcard_unpack_redir_scard_context_ref(log, s, pbContextNdrPtr,
4046 &(call->handles.hContext));
4047 if (status != SCARD_S_SUCCESS)
4048 return status;
4049
4050 status = smartcard_unpack_redir_scard_handle_ref(log, s, &(call->handles.hCard));
4051 if (status != SCARD_S_SUCCESS)
4052 return status;
4053
4054 if (ndrPtr)
4055 {
4056 size_t len = 0;
4057 status = smartcard_ndr_read_ex(log, s, &call->pbAttr, 0, 1, NDR_PTR_SIMPLE, &len);
4058 if (status != SCARD_S_SUCCESS)
4059 return status;
4060 if (call->cbAttrLen > len)
4061 call->cbAttrLen = WINPR_ASSERTING_INT_CAST(DWORD, len);
4062 }
4063 else
4064 call->cbAttrLen = 0;
4065 smartcard_trace_set_attrib_call(log, call);
4066 return SCARD_S_SUCCESS;
4067}
4068
4069LONG smartcard_pack_set_attrib_call(wStream* s, const SetAttrib_Call* call)
4070{
4071 WINPR_ASSERT(call);
4072 wLog* log = scard_log();
4073 UINT32 index = 0;
4074
4075 smartcard_trace_set_attrib_call(log, call);
4076
4077 LONG status = smartcard_pack_redir_scard_context(log, s, &call->handles.hContext, &index);
4078 if (status != SCARD_S_SUCCESS)
4079 return status;
4080
4081 status = smartcard_pack_redir_scard_handle(log, s, &call->handles.hCard, &index);
4082 if (status != SCARD_S_SUCCESS)
4083 return status;
4084
4085 if (!Stream_EnsureRemainingCapacity(s, 8))
4086 return SCARD_E_NO_MEMORY;
4087
4088 Stream_Write_UINT32(s, call->dwAttrId);
4089 Stream_Write_UINT32(s, call->cbAttrLen);
4090
4091 if (!smartcard_ndr_pointer_write(s, &index, call->cbAttrLen))
4092 return SCARD_E_NO_MEMORY;
4093
4094 status = smartcard_pack_redir_scard_context_ref(log, s, &call->handles.hContext);
4095 if (status != SCARD_S_SUCCESS)
4096 return status;
4097
4098 status = smartcard_pack_redir_scard_handle_ref(log, s, &call->handles.hCard);
4099 if (status != SCARD_S_SUCCESS)
4100 return status;
4101
4102 status = smartcard_ndr_write(s, call->pbAttr, call->cbAttrLen, 1, NDR_PTR_SIMPLE);
4103 if (status != SCARD_S_SUCCESS)
4104 return status;
4105
4106 return SCARD_S_SUCCESS;
4107}
4108
4109LONG smartcard_unpack_locate_cards_by_atr_w_call(wStream* s, LocateCardsByATRW_Call* call)
4110{
4111 UINT32 rgReaderStatesNdrPtr = 0;
4112 UINT32 rgAtrMasksNdrPtr = 0;
4113 UINT32 index = 0;
4114 UINT32 pbContextNdrPtr = 0;
4115
4116 WINPR_ASSERT(call);
4117 wLog* log = scard_log();
4118
4119 call->rgReaderStates = nullptr;
4120
4121 LONG status = smartcard_unpack_redir_scard_context(log, s, &(call->handles.hContext), &index,
4122 &pbContextNdrPtr);
4123 if (status != SCARD_S_SUCCESS)
4124 return status;
4125
4126 if (!Stream_CheckAndLogRequiredLengthWLog(log, s, 16))
4127 return STATUS_BUFFER_TOO_SMALL;
4128
4129 Stream_Read_UINT32(s, call->cAtrs);
4130 if (call->cAtrs > 1000)
4131 {
4132 WLog_Print(log, WLOG_ERROR, "2.2.2.24 LocateCardsByATRW_Call cAtrs(%" PRIu32 ") > 1000",
4133 call->cAtrs);
4134 return SCARD_E_INVALID_PARAMETER;
4135 }
4136
4137 if (!smartcard_ndr_pointer_read(log, s, &index, &rgAtrMasksNdrPtr))
4138 return ERROR_INVALID_DATA;
4139
4140 const UINT32 cReaders = Stream_Get_UINT32(s); /* cReaders (4 bytes) */
4141 if (cReaders > 10)
4142 {
4143 WLog_Print(log, WLOG_ERROR, "2.2.2.24 LocateCardsByATRW_Call cReaders(%" PRIu32 ") > 10",
4144 cReaders);
4145 return SCARD_E_INVALID_PARAMETER;
4146 }
4147 if (!smartcard_ndr_pointer_read(log, s, &index, &rgReaderStatesNdrPtr))
4148 return ERROR_INVALID_DATA;
4149
4150 status = smartcard_unpack_redir_scard_context_ref(log, s, pbContextNdrPtr,
4151 &(call->handles.hContext));
4152 if (status != SCARD_S_SUCCESS)
4153 return status;
4154
4155 if ((rgAtrMasksNdrPtr && !call->cAtrs) || (!rgAtrMasksNdrPtr && call->cAtrs))
4156 {
4157 WLog_Print(log, WLOG_WARN,
4158 "LocateCardsByATRW_Call rgAtrMasksNdrPtr (0x%08" PRIX32
4159 ") and cAtrs (0x%08" PRIX32 ") inconsistency",
4160 rgAtrMasksNdrPtr, call->cAtrs);
4161 return STATUS_INVALID_PARAMETER;
4162 }
4163
4164 if (rgAtrMasksNdrPtr)
4165 {
4166 status = smartcard_ndr_read_atrmask(log, s, &call->rgAtrMasks, call->cAtrs, NDR_PTR_SIMPLE);
4167 if (status != SCARD_S_SUCCESS)
4168 return status;
4169 }
4170
4171 if (rgReaderStatesNdrPtr)
4172 {
4173 status = smartcard_unpack_reader_state_w(log, s, &call->rgReaderStates, cReaders, &index);
4174 if (status != SCARD_S_SUCCESS)
4175 return status;
4176 call->cReaders = cReaders;
4177 }
4178
4179 smartcard_trace_locate_cards_by_atr_w_call(log, call);
4180 return SCARD_S_SUCCESS;
4181}
4182
4183LONG smartcard_unpack_read_cache_a_call(wStream* s, ReadCacheA_Call* call)
4184{
4185 UINT32 mszNdrPtr = 0;
4186 UINT32 contextNdrPtr = 0;
4187 UINT32 index = 0;
4188 UINT32 pbContextNdrPtr = 0;
4189
4190 WINPR_ASSERT(call);
4191 wLog* log = scard_log();
4192
4193 if (!smartcard_ndr_pointer_read(log, s, &index, &mszNdrPtr))
4194 return ERROR_INVALID_DATA;
4195
4196 LONG status = smartcard_unpack_redir_scard_context(log, s, &(call->Common.handles.hContext),
4197 &index, &pbContextNdrPtr);
4198 if (status != SCARD_S_SUCCESS)
4199 return status;
4200
4201 if (!smartcard_ndr_pointer_read(log, s, &index, &contextNdrPtr))
4202 return ERROR_INVALID_DATA;
4203
4204 if (!Stream_CheckAndLogRequiredLengthWLog(log, s, 12))
4205 return STATUS_BUFFER_TOO_SMALL;
4206 Stream_Read_UINT32(s, call->Common.FreshnessCounter);
4207 Stream_Read_INT32(s, call->Common.fPbDataIsNULL);
4208 Stream_Read_UINT32(s, call->Common.cbDataLen);
4209
4210 call->szLookupName = nullptr;
4211 if (mszNdrPtr)
4212 {
4213 status = smartcard_ndr_read_a(log, s, &call->szLookupName, NDR_PTR_FULL);
4214 if (status != SCARD_S_SUCCESS)
4215 return status;
4216 }
4217
4218 status = smartcard_unpack_redir_scard_context_ref(log, s, pbContextNdrPtr,
4219 &call->Common.handles.hContext);
4220 if (status != SCARD_S_SUCCESS)
4221 return status;
4222
4223 if (contextNdrPtr)
4224 {
4225 status = smartcard_ndr_read_u(log, s, &call->Common.CardIdentifier);
4226 if (status != SCARD_S_SUCCESS)
4227 return status;
4228 }
4229 smartcard_trace_read_cache_a_call(log, call);
4230 return SCARD_S_SUCCESS;
4231}
4232
4233LONG smartcard_unpack_read_cache_w_call(wStream* s, ReadCacheW_Call* call)
4234{
4235 UINT32 mszNdrPtr = 0;
4236 UINT32 contextNdrPtr = 0;
4237 UINT32 index = 0;
4238 UINT32 pbContextNdrPtr = 0;
4239
4240 WINPR_ASSERT(call);
4241 wLog* log = scard_log();
4242
4243 if (!smartcard_ndr_pointer_read(log, s, &index, &mszNdrPtr))
4244 return ERROR_INVALID_DATA;
4245
4246 LONG status = smartcard_unpack_redir_scard_context(log, s, &(call->Common.handles.hContext),
4247 &index, &pbContextNdrPtr);
4248 if (status != SCARD_S_SUCCESS)
4249 return status;
4250
4251 if (!smartcard_ndr_pointer_read(log, s, &index, &contextNdrPtr))
4252 return ERROR_INVALID_DATA;
4253
4254 if (!Stream_CheckAndLogRequiredLengthWLog(log, s, 12))
4255 return STATUS_BUFFER_TOO_SMALL;
4256 Stream_Read_UINT32(s, call->Common.FreshnessCounter);
4257 Stream_Read_INT32(s, call->Common.fPbDataIsNULL);
4258 Stream_Read_UINT32(s, call->Common.cbDataLen);
4259
4260 call->szLookupName = nullptr;
4261 if (mszNdrPtr)
4262 {
4263 status = smartcard_ndr_read_w(log, s, &call->szLookupName, NDR_PTR_FULL);
4264 if (status != SCARD_S_SUCCESS)
4265 return status;
4266 }
4267
4268 status = smartcard_unpack_redir_scard_context_ref(log, s, pbContextNdrPtr,
4269 &call->Common.handles.hContext);
4270 if (status != SCARD_S_SUCCESS)
4271 return status;
4272
4273 if (contextNdrPtr)
4274 {
4275 status = smartcard_ndr_read_u(log, s, &call->Common.CardIdentifier);
4276 if (status != SCARD_S_SUCCESS)
4277 return status;
4278 }
4279 smartcard_trace_read_cache_w_call(log, call);
4280 return SCARD_S_SUCCESS;
4281}
4282
4283LONG smartcard_unpack_write_cache_a_call(wStream* s, WriteCacheA_Call* call)
4284{
4285 UINT32 mszNdrPtr = 0;
4286 UINT32 contextNdrPtr = 0;
4287 UINT32 pbDataNdrPtr = 0;
4288 UINT32 index = 0;
4289 UINT32 pbContextNdrPtr = 0;
4290
4291 WINPR_ASSERT(call);
4292 wLog* log = scard_log();
4293
4294 if (!smartcard_ndr_pointer_read(log, s, &index, &mszNdrPtr))
4295 return ERROR_INVALID_DATA;
4296
4297 LONG status = smartcard_unpack_redir_scard_context(log, s, &(call->Common.handles.hContext),
4298 &index, &pbContextNdrPtr);
4299 if (status != SCARD_S_SUCCESS)
4300 return status;
4301
4302 if (!smartcard_ndr_pointer_read(log, s, &index, &contextNdrPtr))
4303 return ERROR_INVALID_DATA;
4304
4305 if (!Stream_CheckAndLogRequiredLengthWLog(log, s, 8))
4306 return STATUS_BUFFER_TOO_SMALL;
4307
4308 Stream_Read_UINT32(s, call->Common.FreshnessCounter);
4309 const UINT32 cbDataLen = Stream_Get_UINT32(s);
4310 if (cbDataLen > 65536)
4311 {
4312 WLog_Print(log, WLOG_ERROR, "2.2.1.10 WriteCache_Common cbDataLen(%" PRIu32 ") > 65536",
4313 cbDataLen);
4314 return ERROR_INVALID_DATA;
4315 }
4316
4317 if (!smartcard_ndr_pointer_read(log, s, &index, &pbDataNdrPtr))
4318 return ERROR_INVALID_DATA;
4319
4320 call->szLookupName = nullptr;
4321 if (mszNdrPtr)
4322 {
4323 status = smartcard_ndr_read_a(log, s, &call->szLookupName, NDR_PTR_FULL);
4324 if (status != SCARD_S_SUCCESS)
4325 return status;
4326 }
4327
4328 status = smartcard_unpack_redir_scard_context_ref(log, s, pbContextNdrPtr,
4329 &call->Common.handles.hContext);
4330 if (status != SCARD_S_SUCCESS)
4331 return status;
4332
4333 call->Common.CardIdentifier = nullptr;
4334 if (contextNdrPtr)
4335 {
4336 status = smartcard_ndr_read_u(log, s, &call->Common.CardIdentifier);
4337 if (status != SCARD_S_SUCCESS)
4338 return status;
4339 }
4340
4341 call->Common.pbData = nullptr;
4342 if (pbDataNdrPtr)
4343 {
4344 status = smartcard_ndr_read(log, s, &call->Common.pbData, cbDataLen, 1, NDR_PTR_SIMPLE);
4345 if (status != SCARD_S_SUCCESS)
4346 return status;
4347 call->Common.cbDataLen = cbDataLen;
4348 }
4349 smartcard_trace_write_cache_a_call(log, call);
4350 return SCARD_S_SUCCESS;
4351}
4352
4353LONG smartcard_unpack_write_cache_w_call(wStream* s, WriteCacheW_Call* call)
4354{
4355 UINT32 mszNdrPtr = 0;
4356 UINT32 contextNdrPtr = 0;
4357 UINT32 pbDataNdrPtr = 0;
4358 UINT32 index = 0;
4359 UINT32 pbContextNdrPtr = 0;
4360
4361 WINPR_ASSERT(call);
4362 wLog* log = scard_log();
4363
4364 if (!smartcard_ndr_pointer_read(log, s, &index, &mszNdrPtr))
4365 return ERROR_INVALID_DATA;
4366
4367 LONG status = smartcard_unpack_redir_scard_context(log, s, &(call->Common.handles.hContext),
4368 &index, &pbContextNdrPtr);
4369 if (status != SCARD_S_SUCCESS)
4370 return status;
4371
4372 if (!smartcard_ndr_pointer_read(log, s, &index, &contextNdrPtr))
4373 return ERROR_INVALID_DATA;
4374
4375 if (!Stream_CheckAndLogRequiredLengthWLog(log, s, 8))
4376 return STATUS_BUFFER_TOO_SMALL;
4377 Stream_Read_UINT32(s, call->Common.FreshnessCounter);
4378 const UINT32 cbDataLen = Stream_Get_UINT32(s);
4379
4380 if (cbDataLen > 65536)
4381 {
4382 WLog_Print(log, WLOG_ERROR, "2.2.1.10 WriteCache_Common cbDataLen(%" PRIu32 ") > 65536",
4383 cbDataLen);
4384 return ERROR_INVALID_DATA;
4385 }
4386
4387 if (!smartcard_ndr_pointer_read(log, s, &index, &pbDataNdrPtr))
4388 return ERROR_INVALID_DATA;
4389
4390 call->szLookupName = nullptr;
4391 if (mszNdrPtr)
4392 {
4393 status = smartcard_ndr_read_w(log, s, &call->szLookupName, NDR_PTR_FULL);
4394 if (status != SCARD_S_SUCCESS)
4395 return status;
4396 }
4397
4398 status = smartcard_unpack_redir_scard_context_ref(log, s, pbContextNdrPtr,
4399 &call->Common.handles.hContext);
4400 if (status != SCARD_S_SUCCESS)
4401 return status;
4402
4403 call->Common.CardIdentifier = nullptr;
4404 if (contextNdrPtr)
4405 {
4406 status = smartcard_ndr_read_u(log, s, &call->Common.CardIdentifier);
4407 if (status != SCARD_S_SUCCESS)
4408 return status;
4409 }
4410
4411 call->Common.pbData = nullptr;
4412 if (pbDataNdrPtr)
4413 {
4414 status = smartcard_ndr_read(log, s, &call->Common.pbData, cbDataLen, 1, NDR_PTR_SIMPLE);
4415 if (status != SCARD_S_SUCCESS)
4416 return status;
4417 call->Common.cbDataLen = cbDataLen;
4418 }
4419 smartcard_trace_write_cache_w_call(log, call);
4420 return status;
4421}
4422
4423LONG smartcard_unpack_get_transmit_count_call(wStream* s, GetTransmitCount_Call* call)
4424{
4425 WINPR_ASSERT(call);
4426 wLog* log = scard_log();
4427
4428 UINT32 index = 0;
4429 UINT32 pbContextNdrPtr = 0;
4430
4431 LONG status = smartcard_unpack_redir_scard_context(log, s, &(call->handles.hContext), &index,
4432 &pbContextNdrPtr);
4433 if (status != SCARD_S_SUCCESS)
4434 return status;
4435
4436 status = smartcard_unpack_redir_scard_handle(log, s, &(call->handles.hCard), &index);
4437 if (status != SCARD_S_SUCCESS)
4438 return status;
4439
4440 status = smartcard_unpack_redir_scard_context_ref(log, s, pbContextNdrPtr,
4441 &(call->handles.hContext));
4442 if (status != SCARD_S_SUCCESS)
4443 {
4444 WLog_Print(log, WLOG_ERROR,
4445 "smartcard_unpack_redir_scard_context_ref failed with error %" PRId32 "",
4446 status);
4447 return status;
4448 }
4449
4450 status = smartcard_unpack_redir_scard_handle_ref(log, s, &(call->handles.hCard));
4451 if (status != SCARD_S_SUCCESS)
4452 WLog_Print(log, WLOG_ERROR,
4453 "smartcard_unpack_redir_scard_handle_ref failed with error %" PRId32 "", status);
4454
4455 smartcard_trace_get_transmit_count_call(log, call);
4456 return status;
4457}
4458
4459LONG smartcard_unpack_get_reader_icon_call(wStream* s, GetReaderIcon_Call* call)
4460{
4461 WINPR_ASSERT(call);
4462 wLog* log = scard_log();
4463 return smartcard_unpack_common_context_and_string_w(log, s, &call->handles.hContext,
4464 &call->szReaderName);
4465}
4466
4467LONG smartcard_unpack_context_and_string_a_call(wStream* s, ContextAndStringA_Call* call)
4468{
4469 WINPR_ASSERT(call);
4470 wLog* log = scard_log();
4471 return smartcard_unpack_common_context_and_string_a(log, s, &call->handles.hContext, &call->sz);
4472}
4473
4474LONG smartcard_unpack_context_and_string_w_call(wStream* s, ContextAndStringW_Call* call)
4475{
4476 WINPR_ASSERT(call);
4477 wLog* log = scard_log();
4478 return smartcard_unpack_common_context_and_string_w(log, s, &call->handles.hContext, &call->sz);
4479}
4480
4481LONG smartcard_unpack_get_device_type_id_call(wStream* s, GetDeviceTypeId_Call* call)
4482{
4483 WINPR_ASSERT(call);
4484 wLog* log = scard_log();
4485 return smartcard_unpack_common_context_and_string_w(log, s, &call->handles.hContext,
4486 &call->szReaderName);
4487}
4488
4489LONG smartcard_pack_device_type_id_return(wStream* s, const GetDeviceTypeId_Return* ret)
4490{
4491 WINPR_ASSERT(ret);
4492 wLog* log = scard_log();
4493 smartcard_trace_device_type_id_return(log, ret);
4494
4495 if (!Stream_EnsureRemainingCapacity(s, 4))
4496 {
4497 WLog_Print(log, WLOG_ERROR, "Stream_EnsureRemainingCapacity failed!");
4498 return SCARD_F_INTERNAL_ERROR;
4499 }
4500
4501 Stream_Write_UINT32(s, ret->dwDeviceId); /* cBytes (4 bytes) */
4502
4503 return ret->ReturnCode;
4504}
4505
4506LONG smartcard_pack_locate_cards_return(wStream* s, const LocateCards_Return* ret)
4507{
4508 WINPR_ASSERT(ret);
4509 wLog* log = scard_log();
4510
4511 LONG status = 0;
4512 UINT32 cbDataLen = ret->cReaders;
4513 UINT32 index = 0;
4514
4515 smartcard_trace_locate_cards_return(log, ret);
4516 if (ret->ReturnCode != SCARD_S_SUCCESS)
4517 cbDataLen = 0;
4518 if (cbDataLen == SCARD_AUTOALLOCATE)
4519 cbDataLen = 0;
4520
4521 if (!Stream_EnsureRemainingCapacity(s, 4))
4522 {
4523 WLog_Print(log, WLOG_ERROR, "Stream_EnsureRemainingCapacity failed!");
4524 return SCARD_F_INTERNAL_ERROR;
4525 }
4526
4527 if (ret->cReaders > 10)
4528 {
4529 WLog_Print(log, WLOG_ERROR,
4530 "2.2.3.5 LocateCards_Return and GetStatusChange_Return cReaders(%" PRIu32
4531 ") > 10",
4532 ret->cReaders);
4533 return SCARD_E_INVALID_PARAMETER;
4534 }
4535
4536 Stream_Write_UINT32(s, cbDataLen); /* cBytes (4 cbDataLen) */
4537 if (!smartcard_ndr_pointer_write(s, &index, cbDataLen))
4538 return SCARD_E_NO_MEMORY;
4539
4540 status = smartcard_ndr_write_state(s, ret->rgReaderStates, cbDataLen, NDR_PTR_SIMPLE);
4541 if (status != SCARD_S_SUCCESS)
4542 return status;
4543 return ret->ReturnCode;
4544}
4545
4546LONG smartcard_pack_get_reader_icon_return(wStream* s, const GetReaderIcon_Return* ret)
4547{
4548 WINPR_ASSERT(ret);
4549 wLog* log = scard_log();
4550
4551 LONG status = 0;
4552 UINT32 index = 0;
4553 UINT32 cbDataLen = ret->cbDataLen;
4554 smartcard_trace_get_reader_icon_return(log, ret);
4555 if (ret->ReturnCode != SCARD_S_SUCCESS)
4556 cbDataLen = 0;
4557 if (cbDataLen == SCARD_AUTOALLOCATE)
4558 cbDataLen = 0;
4559
4560 if (!Stream_EnsureRemainingCapacity(s, 4))
4561 {
4562 WLog_Print(log, WLOG_ERROR, "Stream_EnsureRemainingCapacity failed!");
4563 return SCARD_F_INTERNAL_ERROR;
4564 }
4565
4566 Stream_Write_UINT32(s, cbDataLen); /* cBytes (4 cbDataLen) */
4567 if (!smartcard_ndr_pointer_write(s, &index, cbDataLen))
4568 return SCARD_E_NO_MEMORY;
4569
4570 status = smartcard_ndr_write(s, ret->pbData, cbDataLen, 1, NDR_PTR_SIMPLE);
4571 if (status != SCARD_S_SUCCESS)
4572 return status;
4573 return ret->ReturnCode;
4574}
4575
4576LONG smartcard_pack_get_transmit_count_return(wStream* s, const GetTransmitCount_Return* ret)
4577{
4578 WINPR_ASSERT(ret);
4579 wLog* log = scard_log();
4580
4581 smartcard_trace_get_transmit_count_return(log, ret);
4582
4583 if (!Stream_EnsureRemainingCapacity(s, 4))
4584 {
4585 WLog_Print(log, WLOG_ERROR, "Stream_EnsureRemainingCapacity failed!");
4586 return SCARD_F_INTERNAL_ERROR;
4587 }
4588
4589 Stream_Write_UINT32(s, ret->cTransmitCount); /* cBytes (4 cbDataLen) */
4590
4591 return ret->ReturnCode;
4592}
4593
4594LONG smartcard_pack_read_cache_return(wStream* s, const ReadCache_Return* ret)
4595{
4596 WINPR_ASSERT(ret);
4597 wLog* log = scard_log();
4598
4599 LONG status = 0;
4600 UINT32 index = 0;
4601 UINT32 cbDataLen = ret->cbDataLen;
4602 smartcard_trace_read_cache_return(log, ret);
4603 if (ret->ReturnCode != SCARD_S_SUCCESS)
4604 cbDataLen = 0;
4605
4606 if (cbDataLen == SCARD_AUTOALLOCATE)
4607 cbDataLen = 0;
4608
4609 if (!Stream_EnsureRemainingCapacity(s, 4))
4610 {
4611 WLog_Print(log, WLOG_ERROR, "Stream_EnsureRemainingCapacity failed!");
4612 return SCARD_F_INTERNAL_ERROR;
4613 }
4614
4615 if (cbDataLen > 65536)
4616 {
4617 WLog_Print(log, WLOG_ERROR, "2.2.3.1 ReadCache_Return cbDataLen (%" PRIu32 ") > 65536",
4618 cbDataLen);
4619 return SCARD_E_INVALID_PARAMETER;
4620 }
4621
4622 Stream_Write_UINT32(s, cbDataLen); /* cBytes (4 cbDataLen) */
4623 if (!smartcard_ndr_pointer_write(s, &index, cbDataLen))
4624 return SCARD_E_NO_MEMORY;
4625
4626 status = smartcard_ndr_write(s, ret->pbData, cbDataLen, 1, NDR_PTR_SIMPLE);
4627 if (status != SCARD_S_SUCCESS)
4628 return status;
4629 return ret->ReturnCode;
4630}
4631
4632LONG smartcard_pack_context_call(wStream* s, const Context_Call* call, const char* name)
4633{
4634 WINPR_ASSERT(call);
4635 wLog* log = scard_log();
4636 UINT32 index = 0;
4637
4638 smartcard_trace_context_call(log, call, name);
4639
4640 LONG status = smartcard_pack_redir_scard_context(log, s, &call->handles.hContext, &index);
4641 if (status != SCARD_S_SUCCESS)
4642 return status;
4643
4644 return smartcard_pack_redir_scard_context_ref(log, s, &call->handles.hContext);
4645}
4646
4647LONG smartcard_pack_list_readers_call(wStream* s, const ListReaders_Call* call, BOOL unicode)
4648{
4649 WINPR_ASSERT(call);
4650 wLog* log = scard_log();
4651 UINT32 index = 0;
4652
4653 smartcard_trace_list_readers_call(log, call, unicode);
4654
4655 LONG status = smartcard_pack_redir_scard_context(log, s, &call->handles.hContext, &index);
4656 if (status != SCARD_S_SUCCESS)
4657 return status;
4658
4659 if (!Stream_EnsureRemainingCapacity(s, 4))
4660 return SCARD_E_NO_MEMORY;
4661
4662 Stream_Write_UINT32(s, call->cBytes);
4663 if (call->cBytes > 65536)
4664 {
4665 WLog_Print(log, WLOG_ERROR, "2.2.2.4 ListReaders_Call cBytes(%" PRIu32 ") > 65536",
4666 call->cBytes);
4667 return SCARD_E_INVALID_PARAMETER;
4668 }
4669
4670 if (!smartcard_ndr_pointer_write(s, &index, call->cBytes))
4671 return SCARD_E_NO_MEMORY;
4672
4673 if (!Stream_EnsureRemainingCapacity(s, 8))
4674 return SCARD_E_NO_MEMORY;
4675
4676 Stream_Write_INT32(s, call->fmszReadersIsNULL);
4677 Stream_Write_UINT32(s, call->cchReaders);
4678
4679 status = smartcard_pack_redir_scard_context_ref(log, s, &call->handles.hContext);
4680 if (status != SCARD_S_SUCCESS)
4681 return status;
4682
4683 if (call->cBytes > 0)
4684 {
4685 status = smartcard_ndr_write(s, call->mszGroups, call->cBytes, 1, NDR_PTR_SIMPLE);
4686 if (status != SCARD_S_SUCCESS)
4687 return status;
4688 }
4689
4690 return SCARD_S_SUCCESS;
4691}
4692
4693WINPR_ATTR_NODISCARD static LONG
4694smartcard_pack_reader_state_a(wLog* log, wStream* s, const LPSCARD_READERSTATEA rgReaderStates,
4695 UINT32 cReaders, UINT32* ptrIndex)
4696{
4697 WINPR_ASSERT(rgReaderStates || (cReaders == 0));
4698
4699 if (!Stream_EnsureRemainingCapacity(s, 4))
4700 return SCARD_E_NO_MEMORY;
4701
4702 Stream_Write_UINT32(s, cReaders);
4703
4704 for (UINT32 i = 0; i < cReaders; i++)
4705 {
4706 const SCARD_READERSTATEA* state = &rgReaderStates[i];
4707
4708 if (!Stream_EnsureRemainingCapacity(s, 52))
4709 return SCARD_E_NO_MEMORY;
4710
4711 const UINT32 nameLen = state->szReader ? (UINT32)(strlen(state->szReader) + 1) : 0;
4712 if (!smartcard_ndr_pointer_write(s, ptrIndex, nameLen))
4713 return SCARD_E_NO_MEMORY;
4714
4715 Stream_Write_UINT32(s, state->dwCurrentState);
4716 Stream_Write_UINT32(s, state->dwEventState);
4717 if (state->cbAtr > 36)
4718 {
4719 WLog_Print(log, WLOG_WARN, "2.2.2.12 GetStatusChangeW_Call cbAtr(%" PRIu32 ") > 36",
4720 state->cbAtr);
4721 return SCARD_E_INVALID_PARAMETER;
4722 }
4723 Stream_Write_UINT32(s, state->cbAtr);
4724 Stream_Write(s, state->rgbAtr, 36);
4725 }
4726
4727 for (UINT32 i = 0; i < cReaders; i++)
4728 {
4729 const SCARD_READERSTATEA* state = &rgReaderStates[i];
4730
4731 if (state->szReader)
4732 {
4733 const UINT32 nameLen = (UINT32)(strlen(state->szReader) + 1);
4734 LONG status = smartcard_ndr_write_a(s, state->szReader, nameLen, NDR_PTR_FULL);
4735 if (status != SCARD_S_SUCCESS)
4736 return status;
4737 }
4738 }
4739
4740 return SCARD_S_SUCCESS;
4741}
4742
4743WINPR_ATTR_NODISCARD static LONG
4744smartcard_pack_reader_state_w(wLog* log, wStream* s, const LPSCARD_READERSTATEW rgReaderStates,
4745 UINT32 cReaders, UINT32* ptrIndex)
4746{
4747 WINPR_ASSERT(rgReaderStates || (cReaders == 0));
4748
4749 if (!Stream_EnsureRemainingCapacity(s, 4))
4750 return SCARD_E_NO_MEMORY;
4751
4752 Stream_Write_UINT32(s, cReaders);
4753
4754 for (UINT32 i = 0; i < cReaders; i++)
4755 {
4756 const SCARD_READERSTATEW* state = &rgReaderStates[i];
4757
4758 if (!Stream_EnsureRemainingCapacity(s, 52))
4759 return SCARD_E_NO_MEMORY;
4760
4761 const UINT32 nameLen = state->szReader ? (UINT32)(_wcslen(state->szReader) + 1) : 0;
4762 if (!smartcard_ndr_pointer_write(s, ptrIndex, nameLen))
4763 return SCARD_E_NO_MEMORY;
4764
4765 Stream_Write_UINT32(s, state->dwCurrentState);
4766 Stream_Write_UINT32(s, state->dwEventState);
4767 if (state->cbAtr > 36)
4768 {
4769 WLog_Print(log, WLOG_WARN, "2.2.2.12 GetStatusChangeW_Call cbAtr(%" PRIu32 ") > 36",
4770 state->cbAtr);
4771 return SCARD_E_INVALID_PARAMETER;
4772 }
4773 Stream_Write_UINT32(s, state->cbAtr);
4774 Stream_Write(s, state->rgbAtr, 36);
4775 }
4776
4777 for (UINT32 i = 0; i < cReaders; i++)
4778 {
4779 const SCARD_READERSTATEW* state = &rgReaderStates[i];
4780
4781 if (state->szReader)
4782 {
4783 const UINT32 nameLen = (UINT32)(_wcslen(state->szReader) + 1);
4784 LONG status = smartcard_ndr_write_w(s, state->szReader, nameLen, NDR_PTR_FULL);
4785 if (status != SCARD_S_SUCCESS)
4786 return status;
4787 }
4788 }
4789
4790 return SCARD_S_SUCCESS;
4791}
4792
4793LONG smartcard_pack_get_status_change_a_call(wStream* s, const GetStatusChangeA_Call* call)
4794{
4795 WINPR_ASSERT(call);
4796 wLog* log = scard_log();
4797 UINT32 index = 0;
4798
4799 smartcard_trace_get_status_change_a_call(log, call);
4800
4801 LONG status = smartcard_pack_redir_scard_context(log, s, &call->handles.hContext, &index);
4802 if (status != SCARD_S_SUCCESS)
4803 return status;
4804
4805 if (!Stream_EnsureRemainingCapacity(s, 8))
4806 return SCARD_E_NO_MEMORY;
4807
4808 Stream_Write_UINT32(s, call->dwTimeOut);
4809 Stream_Write_UINT32(s, call->cReaders);
4810 if (call->cReaders > 11)
4811 {
4812 WLog_Print(log, WLOG_ERROR, "2.2.2.11 GetStatusChangeA_Call cReaders (%" PRIu32 ") > 11",
4813 call->cReaders);
4814 return SCARD_E_INVALID_PARAMETER;
4815 }
4816 if (!smartcard_ndr_pointer_write(s, &index, call->cReaders))
4817 return SCARD_E_NO_MEMORY;
4818
4819 status = smartcard_pack_redir_scard_context_ref(log, s, &call->handles.hContext);
4820 if (status != SCARD_S_SUCCESS)
4821 return status;
4822
4823 if (call->cReaders > 0)
4824 {
4825 status =
4826 smartcard_pack_reader_state_a(log, s, call->rgReaderStates, call->cReaders, &index);
4827 if (status != SCARD_S_SUCCESS)
4828 return status;
4829 }
4830
4831 return SCARD_S_SUCCESS;
4832}
4833
4834LONG smartcard_pack_get_status_change_w_call(wStream* s, const GetStatusChangeW_Call* call)
4835{
4836 WINPR_ASSERT(call);
4837 wLog* log = scard_log();
4838 UINT32 index = 0;
4839
4840 smartcard_trace_get_status_change_w_call(log, call);
4841
4842 LONG status = smartcard_pack_redir_scard_context(log, s, &call->handles.hContext, &index);
4843 if (status != SCARD_S_SUCCESS)
4844 return status;
4845
4846 if (!Stream_EnsureRemainingCapacity(s, 8))
4847 return SCARD_E_NO_MEMORY;
4848
4849 Stream_Write_UINT32(s, call->dwTimeOut);
4850 Stream_Write_UINT32(s, call->cReaders);
4851 if (call->cReaders > 11)
4852 {
4853 WLog_Print(log, WLOG_ERROR, "2.2.2.12 GetStatusChangeW_Call cReaders (%" PRIu32 ") > 11",
4854 call->cReaders);
4855 return SCARD_E_INVALID_PARAMETER;
4856 }
4857
4858 if (!smartcard_ndr_pointer_write(s, &index, call->cReaders))
4859 return SCARD_E_NO_MEMORY;
4860
4861 status = smartcard_pack_redir_scard_context_ref(log, s, &call->handles.hContext);
4862 if (status != SCARD_S_SUCCESS)
4863 return status;
4864
4865 if (call->cReaders > 0)
4866 {
4867 status =
4868 smartcard_pack_reader_state_w(log, s, call->rgReaderStates, call->cReaders, &index);
4869 if (status != SCARD_S_SUCCESS)
4870 return status;
4871 }
4872
4873 return SCARD_S_SUCCESS;
4874}
4875
4876LONG smartcard_pack_connect_a_call(wStream* s, const ConnectA_Call* call)
4877{
4878 WINPR_ASSERT(call);
4879 wLog* log = scard_log();
4880 UINT32 index = 0;
4881
4882 smartcard_trace_connect_a_call(log, call);
4883
4884 const UINT32 readerLen = call->szReader ? (UINT32)(strlen(call->szReader) + 1) : 0;
4885 if (!smartcard_ndr_pointer_write(s, &index, readerLen))
4886 return SCARD_E_NO_MEMORY;
4887
4888 LONG status =
4889 smartcard_pack_redir_scard_context(log, s, &call->Common.handles.hContext, &index);
4890 if (status != SCARD_S_SUCCESS)
4891 return status;
4892
4893 if (!Stream_EnsureRemainingCapacity(s, 8))
4894 return SCARD_E_NO_MEMORY;
4895
4896 Stream_Write_UINT32(s, call->Common.dwShareMode);
4897 Stream_Write_UINT32(s, call->Common.dwPreferredProtocols);
4898
4899 if (call->szReader)
4900 {
4901 status = smartcard_ndr_write_a(s, call->szReader, readerLen, NDR_PTR_FULL);
4902 if (status != SCARD_S_SUCCESS)
4903 return status;
4904 }
4905
4906 return smartcard_pack_redir_scard_context_ref(log, s, &call->Common.handles.hContext);
4907}
4908
4909LONG smartcard_pack_connect_w_call(wStream* s, const ConnectW_Call* call)
4910{
4911 WINPR_ASSERT(call);
4912 wLog* log = scard_log();
4913 UINT32 index = 0;
4914
4915 smartcard_trace_connect_w_call(log, call);
4916
4917 const UINT32 readerLen = call->szReader ? (UINT32)(_wcslen(call->szReader) + 1) : 0;
4918 if (!smartcard_ndr_pointer_write(s, &index, readerLen))
4919 return SCARD_E_NO_MEMORY;
4920
4921 LONG status =
4922 smartcard_pack_redir_scard_context(log, s, &call->Common.handles.hContext, &index);
4923 if (status != SCARD_S_SUCCESS)
4924 return status;
4925
4926 if (!Stream_EnsureRemainingCapacity(s, 8))
4927 return SCARD_E_NO_MEMORY;
4928
4929 Stream_Write_UINT32(s, call->Common.dwShareMode);
4930 Stream_Write_UINT32(s, call->Common.dwPreferredProtocols);
4931
4932 if (call->szReader)
4933 {
4934 status = smartcard_ndr_write_w(s, call->szReader, readerLen, NDR_PTR_FULL);
4935 if (status != SCARD_S_SUCCESS)
4936 return status;
4937 }
4938
4939 return smartcard_pack_redir_scard_context_ref(log, s, &call->Common.handles.hContext);
4940}
4941
4942LONG smartcard_pack_control_call(wStream* s, const Control_Call* call)
4943{
4944 WINPR_ASSERT(call);
4945 wLog* log = scard_log();
4946 UINT32 index = 0;
4947
4948 smartcard_trace_control_call(log, call);
4949
4950 LONG status = smartcard_pack_redir_scard_context(log, s, &call->handles.hContext, &index);
4951 if (status != SCARD_S_SUCCESS)
4952 return status;
4953
4954 status = smartcard_pack_redir_scard_handle(log, s, &call->handles.hCard, &index);
4955 if (status != SCARD_S_SUCCESS)
4956 return status;
4957
4958 if (!Stream_EnsureRemainingCapacity(s, 8))
4959 return SCARD_E_NO_MEMORY;
4960
4961 Stream_Write_UINT32(s, call->dwControlCode);
4962 Stream_Write_UINT32(s, call->cbInBufferSize);
4963 if (!smartcard_ndr_pointer_write(s, &index, call->cbInBufferSize))
4964 return SCARD_E_NO_MEMORY;
4965
4966 if (!Stream_EnsureRemainingCapacity(s, 8))
4967 return SCARD_E_NO_MEMORY;
4968
4969 Stream_Write_INT32(s, call->fpvOutBufferIsNULL);
4970 Stream_Write_UINT32(s, call->cbOutBufferSize);
4971
4972 status = smartcard_pack_redir_scard_context_ref(log, s, &call->handles.hContext);
4973 if (status != SCARD_S_SUCCESS)
4974 return status;
4975
4976 status = smartcard_pack_redir_scard_handle_ref(log, s, &call->handles.hCard);
4977 if (status != SCARD_S_SUCCESS)
4978 return status;
4979
4980 if (call->cbInBufferSize)
4981 {
4982 status = smartcard_ndr_write(s, call->pvInBuffer, call->cbInBufferSize, 1, NDR_PTR_SIMPLE);
4983 if (status != SCARD_S_SUCCESS)
4984 return status;
4985 }
4986
4987 return status;
4988}
4989
4990LONG smartcard_pack_hcard_and_disposition_call(wStream* s, const HCardAndDisposition_Call* call,
4991 const char* name)
4992{
4993 WINPR_ASSERT(call);
4994 wLog* log = scard_log();
4995 UINT32 index = 0;
4996
4997 smartcard_trace_hcard_and_disposition_call(log, call, name);
4998
4999 LONG status = smartcard_pack_redir_scard_context(log, s, &call->handles.hContext, &index);
5000 if (status != SCARD_S_SUCCESS)
5001 return status;
5002
5003 status = smartcard_pack_redir_scard_handle(log, s, &call->handles.hCard, &index);
5004 if (status != SCARD_S_SUCCESS)
5005 return status;
5006
5007 if (!Stream_EnsureRemainingCapacity(s, 4))
5008 return SCARD_E_NO_MEMORY;
5009
5010 Stream_Write_UINT32(s, call->dwDisposition);
5011
5012 status = smartcard_pack_redir_scard_context_ref(log, s, &call->handles.hContext);
5013 if (status != SCARD_S_SUCCESS)
5014 return status;
5015
5016 return smartcard_pack_redir_scard_handle_ref(log, s, &call->handles.hCard);
5017}
5018
5019LONG smartcard_pack_transmit_call(wStream* s, const Transmit_Call* call)
5020{
5021 WINPR_ASSERT(call);
5022 wLog* log = scard_log();
5023 UINT32 index = 0;
5024
5025 smartcard_trace_transmit_call(log, call);
5026
5027 LONG status = smartcard_pack_redir_scard_context(log, s, &call->handles.hContext, &index);
5028 if (status != SCARD_S_SUCCESS)
5029 return status;
5030
5031 status = smartcard_pack_redir_scard_handle(log, s, &call->handles.hCard, &index);
5032 if (status != SCARD_S_SUCCESS)
5033 return status;
5034
5035 UINT32 cbExtraBytes = 0;
5036 const BYTE* pbExtraBytes = nullptr;
5037 if (call->pioSendPci)
5038 {
5039 cbExtraBytes = (UINT32)(call->pioSendPci->cbPciLength - sizeof(SCARD_IO_REQUEST));
5040 if (cbExtraBytes > 0)
5041 pbExtraBytes = &((const BYTE*)call->pioSendPci)[sizeof(SCARD_IO_REQUEST)];
5042 }
5043
5044 if (!Stream_EnsureRemainingCapacity(s, 32))
5045 return SCARD_E_NO_MEMORY;
5046
5047 Stream_Write_UINT32(s, call->pioSendPci ? call->pioSendPci->dwProtocol : 0);
5048 Stream_Write_UINT32(s, cbExtraBytes);
5049 if (!smartcard_ndr_pointer_write(s, &index, cbExtraBytes))
5050 return SCARD_E_NO_MEMORY;
5051
5052 Stream_Write_UINT32(s, call->cbSendLength);
5053 if (!smartcard_ndr_pointer_write(s, &index, call->cbSendLength))
5054 return SCARD_E_NO_MEMORY;
5055
5056 const UINT32 recvPciLen = call->pioRecvPci ? (UINT32)call->pioRecvPci->cbPciLength : 0;
5057 if (!smartcard_ndr_pointer_write(s, &index, recvPciLen))
5058 return SCARD_E_NO_MEMORY;
5059
5060 Stream_Write_INT32(s, call->fpbRecvBufferIsNULL);
5061 Stream_Write_UINT32(s, call->cbRecvLength);
5062
5063 status = smartcard_pack_redir_scard_context_ref(log, s, &call->handles.hContext);
5064 if (status != SCARD_S_SUCCESS)
5065 return status;
5066
5067 status = smartcard_pack_redir_scard_handle_ref(log, s, &call->handles.hCard);
5068 if (status != SCARD_S_SUCCESS)
5069 return status;
5070
5071 if (cbExtraBytes > 0)
5072 {
5073 status = smartcard_ndr_write(s, pbExtraBytes, cbExtraBytes, 1, NDR_PTR_SIMPLE);
5074 if (status != SCARD_S_SUCCESS)
5075 return status;
5076 }
5077
5078 if (call->cbSendLength > 0)
5079 {
5080 status = smartcard_ndr_write(s, call->pbSendBuffer, call->cbSendLength, 1, NDR_PTR_SIMPLE);
5081 if (status != SCARD_S_SUCCESS)
5082 return status;
5083 }
5084
5085 if (call->pioRecvPci && recvPciLen > 0)
5086 {
5087 UINT32 cbRecvExtra = (UINT32)(call->pioRecvPci->cbPciLength - sizeof(SCARD_IO_REQUEST));
5088 const BYTE* pbRecvExtra = &((const BYTE*)call->pioRecvPci)[sizeof(SCARD_IO_REQUEST)];
5089
5090 if (!Stream_EnsureRemainingCapacity(s, 12))
5091 return SCARD_E_NO_MEMORY;
5092
5093 Stream_Write_UINT32(s, call->pioRecvPci->dwProtocol);
5094 Stream_Write_UINT32(s, cbRecvExtra);
5095 if (!smartcard_ndr_pointer_write(s, &index, cbRecvExtra))
5096 return SCARD_E_NO_MEMORY;
5097
5098 if (cbRecvExtra > 0)
5099 {
5100 status = smartcard_ndr_write(s, pbRecvExtra, cbRecvExtra, 1, NDR_PTR_SIMPLE);
5101 if (status != SCARD_S_SUCCESS)
5102 return status;
5103 }
5104 }
5105
5106 return SCARD_S_SUCCESS;
5107}
5108
5109LONG smartcard_pack_get_attrib_call(wStream* s, const GetAttrib_Call* call)
5110{
5111 WINPR_ASSERT(call);
5112 wLog* log = scard_log();
5113 UINT32 index = 0;
5114
5115 smartcard_trace_get_attrib_call(log, call);
5116
5117 LONG status = smartcard_pack_redir_scard_context(log, s, &call->handles.hContext, &index);
5118 if (status != SCARD_S_SUCCESS)
5119 return status;
5120
5121 status = smartcard_pack_redir_scard_handle(log, s, &call->handles.hCard, &index);
5122 if (status != SCARD_S_SUCCESS)
5123 return status;
5124
5125 if (!Stream_EnsureRemainingCapacity(s, 12))
5126 return STATUS_NO_MEMORY;
5127
5128 Stream_Write_UINT32(s, call->dwAttrId);
5129 Stream_Write_INT32(s, call->fpbAttrIsNULL);
5130 Stream_Write_UINT32(s, call->cbAttrLen);
5131
5132 status = smartcard_pack_redir_scard_context_ref(log, s, &call->handles.hContext);
5133 if (status != SCARD_S_SUCCESS)
5134 return status;
5135
5136 return smartcard_pack_redir_scard_handle_ref(log, s, &call->handles.hCard);
5137}
5138
5139LONG smartcard_unpack_list_readers_return(wStream* s, ListReaders_Return* ret, BOOL unicode)
5140{
5141 WINPR_ASSERT(ret);
5142 wLog* log = scard_log();
5143 UINT32 index = 0;
5144 UINT32 mszNdrPtr = 0;
5145
5146 if (!Stream_CheckAndLogRequiredLengthWLog(log, s, 4))
5147 return STATUS_BUFFER_TOO_SMALL;
5148
5149 const UINT32 cBytes = Stream_Get_UINT32(s);
5150
5151 if (!smartcard_ndr_pointer_read(log, s, &index, &mszNdrPtr))
5152 return ERROR_INVALID_DATA;
5153
5154 if (mszNdrPtr)
5155 {
5156 LONG status = smartcard_ndr_read(log, s, &ret->msz, cBytes, 1, NDR_PTR_SIMPLE);
5157 if (status != SCARD_S_SUCCESS)
5158 return status;
5159 ret->cBytes = cBytes;
5160 }
5161
5162 smartcard_trace_list_readers_return(log, ret, unicode);
5163 return SCARD_S_SUCCESS;
5164}
5165
5166LONG smartcard_unpack_get_status_change_return(wStream* s, GetStatusChange_Return* ret,
5167 BOOL unicode)
5168{
5169 WINPR_ASSERT(ret);
5170 wLog* log = scard_log();
5171 UINT32 index = 0;
5172 UINT32 ndrPtr = 0;
5173
5174 ret->cReaders = 0;
5175 ret->rgReaderStates = nullptr;
5176 if (!Stream_CheckAndLogRequiredLengthWLog(log, s, 4))
5177 return STATUS_BUFFER_TOO_SMALL;
5178
5179 const UINT32 cReaders = Stream_Get_UINT32(s);
5180 if (cReaders > 10)
5181 {
5182 WLog_Print(log, WLOG_ERROR,
5183 "2.2.3.5 LocateCards_Return and GetStatusChange_Return cReaders(%" PRIu32
5184 ") > 10",
5185 ret->cReaders);
5186 return SCARD_E_INVALID_PARAMETER;
5187 }
5188
5189 if (!smartcard_ndr_pointer_read(log, s, &index, &ndrPtr))
5190 return ERROR_INVALID_DATA;
5191
5192 if ((cReaders > 0) && !ndrPtr)
5193 {
5194 WLog_Print(log, WLOG_WARN,
5195 "GetStatusChange_Return rgReaderStatesNdrPtr (0x%08" PRIX32
5196 ") and cReaders (0x%08" PRIX32 ") inconsistency",
5197 ndrPtr, cReaders);
5198 return SCARD_E_INVALID_VALUE;
5199 }
5200
5201 if (ndrPtr)
5202 {
5203 LONG status =
5204 smartcard_ndr_read_state(log, s, &ret->rgReaderStates, ret->cReaders, NDR_PTR_SIMPLE);
5205 if (status != SCARD_S_SUCCESS)
5206 return status;
5207 ret->cReaders = cReaders;
5208 }
5209
5210 smartcard_trace_get_status_change_return(log, ret, unicode);
5211 return SCARD_S_SUCCESS;
5212}
5213
5214LONG smartcard_unpack_connect_return(wStream* s, Connect_Return* ret)
5215{
5216 WINPR_ASSERT(ret);
5217 wLog* log = scard_log();
5218 UINT32 index = 0;
5219 UINT32 pbContextNdrPtr = 0;
5220
5221 LONG status =
5222 smartcard_unpack_redir_scard_context(log, s, &ret->hContext, &index, &pbContextNdrPtr);
5223 if (status != SCARD_S_SUCCESS)
5224 return status;
5225
5226 status = smartcard_unpack_redir_scard_handle(log, s, &ret->hCard, &index);
5227 if (status != SCARD_S_SUCCESS)
5228 return status;
5229
5230 if (!Stream_CheckAndLogRequiredLengthWLog(log, s, 4))
5231 return STATUS_BUFFER_TOO_SMALL;
5232
5233 Stream_Read_UINT32(s, ret->dwActiveProtocol);
5234
5235 status = smartcard_unpack_redir_scard_context_ref(log, s, pbContextNdrPtr, &ret->hContext);
5236 if (status != SCARD_S_SUCCESS)
5237 return status;
5238
5239 status = smartcard_unpack_redir_scard_handle_ref(log, s, &ret->hCard);
5240 if (status != SCARD_S_SUCCESS)
5241 return status;
5242
5243 smartcard_trace_connect_return(log, ret);
5244 return SCARD_S_SUCCESS;
5245}
5246
5247LONG smartcard_unpack_control_return(wStream* s, Control_Return* ret)
5248{
5249 WINPR_ASSERT(ret);
5250 wLog* log = scard_log();
5251 UINT32 index = 0;
5252 UINT32 pvOutBufferNdrPtr = 0;
5253
5254 if (!Stream_CheckAndLogRequiredLengthWLog(log, s, 8))
5255 return STATUS_BUFFER_TOO_SMALL;
5256
5257 const UINT32 cbOutBufferSize = Stream_Get_UINT32(s);
5258
5259 if (!smartcard_ndr_pointer_read(log, s, &index, &pvOutBufferNdrPtr))
5260 return ERROR_INVALID_DATA;
5261
5262 if (pvOutBufferNdrPtr && (cbOutBufferSize > 0))
5263 {
5264 LONG status =
5265 smartcard_ndr_read(log, s, &ret->pvOutBuffer, cbOutBufferSize, 1, NDR_PTR_SIMPLE);
5266 if (status != SCARD_S_SUCCESS)
5267 return status;
5268 ret->cbOutBufferSize = cbOutBufferSize;
5269 }
5270 smartcard_trace_control_return(log, ret);
5271 return SCARD_S_SUCCESS;
5272}
5273
5274LONG smartcard_unpack_transmit_return(wStream* s, Transmit_Return* ret)
5275{
5276 WINPR_ASSERT(ret);
5277 wLog* log = scard_log();
5278 UINT32 index = 0;
5279 UINT32 recvPciNdrPtr = 0;
5280 UINT32 recvBufferNdrPtr = 0;
5281
5282 if (!smartcard_ndr_pointer_read(log, s, &index, &recvPciNdrPtr))
5283 return ERROR_INVALID_DATA;
5284
5285 if (!Stream_CheckAndLogRequiredLengthWLog(log, s, 4))
5286 return STATUS_BUFFER_TOO_SMALL;
5287
5288 const UINT32 cbRecvLength = Stream_Get_UINT32(s);
5289 if (cbRecvLength > 66560)
5290 {
5291 WLog_Print(log, WLOG_ERROR, "2.2.3.11 Transmit_Return cbRecvLength(%" PRIu32 ") > 66560",
5292 cbRecvLength);
5293 return SCARD_E_INVALID_PARAMETER;
5294 }
5295
5296 if (!smartcard_ndr_pointer_read(log, s, &index, &recvBufferNdrPtr))
5297 return ERROR_INVALID_DATA;
5298
5299 if (recvPciNdrPtr)
5300 {
5301 if (!Stream_CheckAndLogRequiredLengthWLog(log, s, 8))
5302 return STATUS_BUFFER_TOO_SMALL;
5303
5304 UINT32 dwProtocol = 0;
5305 UINT32 cbExtraBytes = 0;
5306 Stream_Read_UINT32(s, dwProtocol);
5307 Stream_Read_UINT32(s, cbExtraBytes);
5308
5309 UINT32 pbExtraBytesNdrPtr = 0;
5310 if (!smartcard_ndr_pointer_read(log, s, &index, &pbExtraBytesNdrPtr))
5311 return ERROR_INVALID_DATA;
5312
5313 if (pbExtraBytesNdrPtr)
5314 {
5315 if (!Stream_CheckAndLogRequiredLengthWLog(log, s, 4))
5316 return STATUS_BUFFER_TOO_SMALL;
5317
5318 UINT32 length = 0;
5319 Stream_Read_UINT32(s, length);
5320 if (length != cbExtraBytes)
5321 {
5322 WLog_Print(log, WLOG_WARN,
5323 "Transmit_Return unexpected length: Actual: %" PRIu32
5324 ", Expected: %" PRIu32 " (pioRecvPci.cbExtraBytes)",
5325 length, cbExtraBytes);
5326 return STATUS_INVALID_PARAMETER;
5327 }
5328
5329 if (!Stream_CheckAndLogRequiredLengthWLog(log, s, cbExtraBytes))
5330 return STATUS_BUFFER_TOO_SMALL;
5331
5332 ret->pioRecvPci = (LPSCARD_IO_REQUEST)malloc(sizeof(SCARD_IO_REQUEST) + cbExtraBytes);
5333 if (!ret->pioRecvPci)
5334 return SCARD_E_NO_MEMORY;
5335
5336 ret->pioRecvPci->dwProtocol = dwProtocol;
5337 ret->pioRecvPci->cbPciLength = (DWORD)(sizeof(SCARD_IO_REQUEST) + cbExtraBytes);
5338
5339 BYTE* pbExtraBytes = &((BYTE*)ret->pioRecvPci)[sizeof(SCARD_IO_REQUEST)];
5340 Stream_Read(s, pbExtraBytes, length);
5341 if (smartcard_unpack_read_size_align(s, cbExtraBytes, 4) < 0)
5342 return STATUS_INVALID_PARAMETER;
5343 }
5344 else
5345 {
5346 ret->pioRecvPci = (LPSCARD_IO_REQUEST)malloc(sizeof(SCARD_IO_REQUEST));
5347 if (!ret->pioRecvPci)
5348 return SCARD_E_NO_MEMORY;
5349
5350 ret->pioRecvPci->dwProtocol = dwProtocol;
5351 ret->pioRecvPci->cbPciLength = sizeof(SCARD_IO_REQUEST);
5352 }
5353 }
5354
5355 if (recvBufferNdrPtr && (cbRecvLength > 0))
5356 {
5357 LONG status =
5358 smartcard_ndr_read(log, s, &ret->pbRecvBuffer, cbRecvLength, 1, NDR_PTR_SIMPLE);
5359 if (status != SCARD_S_SUCCESS)
5360 return status;
5361 ret->cbRecvLength = cbRecvLength;
5362 }
5363
5364 smartcard_trace_transmit_return(log, ret);
5365 return SCARD_S_SUCCESS;
5366}
5367
5368LONG smartcard_unpack_get_attrib_return(wStream* s, GetAttrib_Return* ret)
5369{
5370 WINPR_ASSERT(ret);
5371 wLog* log = scard_log();
5372 UINT32 index = 0;
5373 UINT32 pbAttrPtr = 0;
5374
5375 if (!Stream_CheckAndLogRequiredLengthWLog(log, s, 4))
5376 return STATUS_BUFFER_TOO_SMALL;
5377
5378 const UINT32 cbAttrLen = Stream_Get_UINT32(s);
5379 if (cbAttrLen > 65536)
5380 {
5381 WLog_Print(log, WLOG_ERROR, "2.2.3.12 GetAttrib_Return cbAttrLen(%" PRIu32 ") > 65536",
5382 cbAttrLen);
5383 return SCARD_E_INVALID_PARAMETER;
5384 }
5385
5386 if (!smartcard_ndr_pointer_read(log, s, &index, &pbAttrPtr))
5387 return ERROR_INVALID_DATA;
5388
5389 LONG status = smartcard_ndr_read(log, s, &ret->pbAttr, cbAttrLen, 1, NDR_PTR_SIMPLE);
5390 if (status != SCARD_S_SUCCESS)
5391 return status;
5392 ret->cbAttrLen = cbAttrLen;
5393
5394 smartcard_trace_get_attrib_return(log, ret, 0);
5395 return SCARD_S_SUCCESS;
5396}
5397
5398BOOL smartcard_reader_state_return_is_valid_ex(size_t pos, const ReaderState_Return* val,
5399 const char* file, size_t line, const char* fkt)
5400{
5401 if (!val)
5402 {
5403 WLog_Print_dbg_tag(SCARD_TAG, WLOG_WARN, line, file, fkt, "[%" PRIuz "] val = nullptr",
5404 pos);
5405 return FALSE;
5406 }
5407 if (val->cbAtr > sizeof(val->rgbAtr))
5408 {
5409 WLog_Print_dbg_tag(SCARD_TAG, WLOG_WARN, line, file, fkt,
5410 "[%" PRIuz "] val->cbAtr=%" PRIu32 ", max=%" PRIuz, pos, val->cbAtr,
5411 sizeof(val->rgbAtr));
5412 return FALSE;
5413 }
5414
5415 return TRUE;
5416}
Definition wtypes.h:263