Coverage Report

Created: 2025-08-26 06:37

/src/FreeRDP/libfreerdp/utils/smartcard_pack.c
Line
Count
Source (jump to first uncovered line)
1
/**
2
 * FreeRDP: A Remote Desktop Protocol Implementation
3
 * Smart Card Structure Packing
4
 *
5
 * Copyright 2014 Marc-Andre Moreau <marcandre.moreau@gmail.com>
6
 * Copyright 2015 Thincast Technologies GmbH
7
 * Copyright 2015 DI (FH) Martin Haimberger <martin.haimberger@thincast.com>
8
 * Copyright 2020 Armin Novak <armin.novak@thincast.com>
9
 * Copyright 2020 Thincast Technologies GmbH
10
 *
11
 * Licensed under the Apache License, Version 2.0 (the "License");
12
 * you may not use this file except in compliance with the License.
13
 * You may obtain a copy of the License at
14
 *
15
 *     http://www.apache.org/licenses/LICENSE-2.0
16
 *
17
 * Unless required by applicable law or agreed to in writing, software
18
 * distributed under the License is distributed on an "AS IS" BASIS,
19
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
20
 * See the License for the specific language governing permissions and
21
 * limitations under the License.
22
 */
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
0
#define SCARD_TAG FREERDP_TAG("scard.pack")
35
36
static const DWORD g_LogLevel = WLOG_DEBUG;
37
38
static wLog* scard_log(void)
39
0
{
40
0
  static wLog* log = NULL;
41
0
  if (!log)
42
0
    log = WLog_Get(SCARD_TAG);
43
0
  return log;
44
0
}
45
46
#define smartcard_unpack_redir_scard_context(log, s, context, index, ndr)                  \
47
0
  smartcard_unpack_redir_scard_context_((log), (s), (context), (index), (ndr), __FILE__, \
48
0
                                        __func__, __LINE__)
49
#define smartcard_unpack_redir_scard_handle(log, s, context, index)                          \
50
0
  smartcard_unpack_redir_scard_handle_((log), (s), (context), (index), __FILE__, __func__, \
51
0
                                       __LINE__)
52
53
static LONG smartcard_unpack_redir_scard_context_(wLog* log, wStream* s,
54
                                                  REDIR_SCARDCONTEXT* context, UINT32* index,
55
                                                  UINT32* ppbContextNdrPtr, const char* file,
56
                                                  const char* function, size_t line);
57
static LONG smartcard_pack_redir_scard_context(wLog* log, wStream* s,
58
                                               const REDIR_SCARDCONTEXT* context, DWORD* index);
59
static LONG smartcard_unpack_redir_scard_handle_(wLog* log, wStream* s, REDIR_SCARDHANDLE* handle,
60
                                                 UINT32* index, const char* file,
61
                                                 const char* function, size_t line);
62
static LONG smartcard_pack_redir_scard_handle(wLog* log, wStream* s,
63
                                              const REDIR_SCARDHANDLE* handle, DWORD* index);
64
static LONG smartcard_unpack_redir_scard_context_ref(wLog* log, wStream* s, UINT32 pbContextNdrPtr,
65
                                                     REDIR_SCARDCONTEXT* context);
66
static LONG smartcard_pack_redir_scard_context_ref(wLog* log, wStream* s,
67
                                                   const REDIR_SCARDCONTEXT* context);
68
69
static LONG smartcard_unpack_redir_scard_handle_ref(wLog* log, wStream* s,
70
                                                    REDIR_SCARDHANDLE* handle);
71
static LONG smartcard_pack_redir_scard_handle_ref(wLog* log, wStream* s,
72
                                                  const REDIR_SCARDHANDLE* handle);
73
74
typedef enum
75
{
76
  NDR_PTR_FULL,
77
  NDR_PTR_SIMPLE,
78
  NDR_PTR_FIXED
79
} ndr_ptr_t;
80
81
/* Reads a NDR pointer and checks if the value read has the expected relative
82
 * addressing */
83
#define smartcard_ndr_pointer_read(log, s, index, ptr) \
84
0
  smartcard_ndr_pointer_read_((log), (s), (index), (ptr), __FILE__, __func__, __LINE__)
85
static BOOL smartcard_ndr_pointer_read_(wLog* log, wStream* s, UINT32* index, UINT32* ptr,
86
                                        const char* file, const char* fkt, size_t line)
87
0
{
88
0
  const UINT32 expect = 0x20000 + (*index) * 4;
89
0
  UINT32 ndrPtr = 0;
90
0
  WINPR_UNUSED(file);
91
0
  if (!s)
92
0
    return FALSE;
93
0
  if (!Stream_CheckAndLogRequiredLengthWLog(log, s, 4))
94
0
    return FALSE;
95
96
0
  Stream_Read_UINT32(s, ndrPtr); /* mszGroupsNdrPtr (4 bytes) */
97
0
  if (ptr)
98
0
    *ptr = ndrPtr;
99
0
  if (expect != ndrPtr)
100
0
  {
101
    /* Allow NULL pointer if we read the result */
102
0
    if (ptr && (ndrPtr == 0))
103
0
      return TRUE;
104
0
    WLog_Print(log, WLOG_WARN,
105
0
               "[%s:%" PRIuz "] Read context pointer 0x%08" PRIx32 ", expected 0x%08" PRIx32,
106
0
               fkt, line, ndrPtr, expect);
107
0
    return FALSE;
108
0
  }
109
110
0
  (*index) = (*index) + 1;
111
0
  return TRUE;
112
0
}
113
114
static LONG smartcard_ndr_read(wLog* log, wStream* s, BYTE** data, size_t min, size_t elementSize,
115
                               ndr_ptr_t type)
116
0
{
117
0
  size_t len = 0;
118
0
  size_t offset = 0;
119
0
  size_t len2 = 0;
120
0
  void* r = NULL;
121
0
  size_t required = 0;
122
123
0
  *data = NULL;
124
0
  switch (type)
125
0
  {
126
0
    case NDR_PTR_FULL:
127
0
      required = 12;
128
0
      break;
129
0
    case NDR_PTR_SIMPLE:
130
0
      required = 4;
131
0
      break;
132
0
    case NDR_PTR_FIXED:
133
0
      required = min;
134
0
      break;
135
0
    default:
136
0
      return STATUS_INVALID_PARAMETER;
137
0
  }
138
139
0
  if (!Stream_CheckAndLogRequiredLengthWLog(log, s, required))
140
0
    return STATUS_BUFFER_TOO_SMALL;
141
142
0
  switch (type)
143
0
  {
144
0
    case NDR_PTR_FULL:
145
0
      Stream_Read_UINT32(s, len);
146
0
      Stream_Read_UINT32(s, offset);
147
0
      Stream_Read_UINT32(s, len2);
148
0
      if (len != offset + len2)
149
0
      {
150
0
        WLog_Print(log, WLOG_ERROR,
151
0
                   "Invalid data when reading full NDR pointer: total=%" PRIuz
152
0
                   ", offset=%" PRIuz ", remaining=%" PRIuz,
153
0
                   len, offset, len2);
154
0
        return STATUS_BUFFER_TOO_SMALL;
155
0
      }
156
0
      break;
157
0
    case NDR_PTR_SIMPLE:
158
0
      Stream_Read_UINT32(s, len);
159
160
0
      if ((len != min) && (min > 0))
161
0
      {
162
0
        WLog_Print(log, WLOG_ERROR,
163
0
                   "Invalid data when reading simple NDR pointer: total=%" PRIuz
164
0
                   ", expected=%" PRIuz,
165
0
                   len, min);
166
0
        return STATUS_BUFFER_TOO_SMALL;
167
0
      }
168
0
      break;
169
0
    case NDR_PTR_FIXED:
170
0
      len = (UINT32)min;
171
0
      break;
172
0
    default:
173
0
      return STATUS_INVALID_PARAMETER;
174
0
  }
175
176
0
  if (min > len)
177
0
  {
178
0
    WLog_Print(log, WLOG_ERROR,
179
0
               "Invalid length read from NDR pointer, minimum %" PRIuz ", got %" PRIuz, min,
180
0
               len);
181
0
    return STATUS_DATA_ERROR;
182
0
  }
183
184
0
  if (len > SIZE_MAX / 2)
185
0
    return STATUS_BUFFER_TOO_SMALL;
186
187
0
  if (!Stream_CheckAndLogRequiredLengthOfSizeWLog(log, s, len, elementSize))
188
0
    return STATUS_BUFFER_TOO_SMALL;
189
190
0
  len *= elementSize;
191
192
  /* Ensure proper '\0' termination for all kinds of unicode strings
193
   * as we do not know if the data from the wire contains one.
194
   */
195
0
  r = calloc(len + sizeof(WCHAR), sizeof(CHAR));
196
0
  if (!r)
197
0
    return SCARD_E_NO_MEMORY;
198
0
  Stream_Read(s, r, len);
199
0
  smartcard_unpack_read_size_align(s, len, 4);
200
0
  *data = r;
201
0
  return STATUS_SUCCESS;
202
0
}
203
204
static BOOL smartcard_ndr_pointer_write(wStream* s, UINT32* index, DWORD length)
205
0
{
206
0
  const UINT32 ndrPtr = 0x20000 + (*index) * 4;
207
208
0
  if (!s)
209
0
    return FALSE;
210
0
  if (!Stream_EnsureRemainingCapacity(s, 4))
211
0
    return FALSE;
212
213
0
  if (length > 0)
214
0
  {
215
0
    Stream_Write_UINT32(s, ndrPtr); /* mszGroupsNdrPtr (4 bytes) */
216
0
    (*index) = (*index) + 1;
217
0
  }
218
0
  else
219
0
    Stream_Write_UINT32(s, 0);
220
0
  return TRUE;
221
0
}
222
223
static LONG smartcard_ndr_write(wStream* s, const BYTE* data, UINT32 size, UINT32 elementSize,
224
                                ndr_ptr_t type)
225
0
{
226
0
  const UINT32 offset = 0;
227
0
  const UINT32 len = size;
228
0
  const UINT32 dataLen = size * elementSize;
229
0
  size_t required = 0;
230
231
0
  if (size == 0)
232
0
    return SCARD_S_SUCCESS;
233
234
0
  switch (type)
235
0
  {
236
0
    case NDR_PTR_FULL:
237
0
      required = 12;
238
0
      break;
239
0
    case NDR_PTR_SIMPLE:
240
0
      required = 4;
241
0
      break;
242
0
    case NDR_PTR_FIXED:
243
0
      required = 0;
244
0
      break;
245
0
    default:
246
0
      return SCARD_E_INVALID_PARAMETER;
247
0
  }
248
249
0
  if (!Stream_EnsureRemainingCapacity(s, required + dataLen + 4))
250
0
    return STATUS_BUFFER_TOO_SMALL;
251
252
0
  switch (type)
253
0
  {
254
0
    case NDR_PTR_FULL:
255
0
      Stream_Write_UINT32(s, len);
256
0
      Stream_Write_UINT32(s, offset);
257
0
      Stream_Write_UINT32(s, len);
258
0
      break;
259
0
    case NDR_PTR_SIMPLE:
260
0
      Stream_Write_UINT32(s, len);
261
0
      break;
262
0
    case NDR_PTR_FIXED:
263
0
      break;
264
0
    default:
265
0
      return SCARD_E_INVALID_PARAMETER;
266
0
  }
267
268
0
  if (data)
269
0
    Stream_Write(s, data, dataLen);
270
0
  else
271
0
    Stream_Zero(s, dataLen);
272
0
  return smartcard_pack_write_size_align(s, len, 4);
273
0
}
274
275
static LONG smartcard_ndr_write_state(wStream* s, const ReaderState_Return* data, UINT32 size,
276
                                      ndr_ptr_t type)
277
0
{
278
0
  union
279
0
  {
280
0
    const ReaderState_Return* reader;
281
0
    const BYTE* data;
282
0
  } cnv;
283
284
0
  WINPR_ASSERT(data || (size == 0));
285
0
  cnv.reader = data;
286
0
  return smartcard_ndr_write(s, cnv.data, size, sizeof(ReaderState_Return), type);
287
0
}
288
289
static LONG smartcard_ndr_read_atrmask(wLog* log, wStream* s, LocateCards_ATRMask** data,
290
                                       size_t min, ndr_ptr_t type)
291
0
{
292
0
  union
293
0
  {
294
0
    LocateCards_ATRMask** ppc;
295
0
    BYTE** ppv;
296
0
  } u;
297
0
  u.ppc = data;
298
0
  return smartcard_ndr_read(log, s, u.ppv, min, sizeof(LocateCards_ATRMask), type);
299
0
}
300
301
static LONG smartcard_ndr_read_fixed_string_a(wLog* log, wStream* s, CHAR** data, size_t min,
302
                                              ndr_ptr_t type)
303
0
{
304
0
  union
305
0
  {
306
0
    CHAR** ppc;
307
0
    BYTE** ppv;
308
0
  } u;
309
0
  u.ppc = data;
310
0
  return smartcard_ndr_read(log, s, u.ppv, min, sizeof(CHAR), type);
311
0
}
312
313
static LONG smartcard_ndr_read_fixed_string_w(wLog* log, wStream* s, WCHAR** data, size_t min,
314
                                              ndr_ptr_t type)
315
0
{
316
0
  union
317
0
  {
318
0
    WCHAR** ppc;
319
0
    BYTE** ppv;
320
0
  } u;
321
0
  u.ppc = data;
322
0
  return smartcard_ndr_read(log, s, u.ppv, min, sizeof(WCHAR), type);
323
0
}
324
325
static LONG smartcard_ndr_read_a(wLog* log, wStream* s, CHAR** data, ndr_ptr_t type)
326
0
{
327
0
  union
328
0
  {
329
0
    CHAR** ppc;
330
0
    BYTE** ppv;
331
0
  } u;
332
0
  u.ppc = data;
333
0
  return smartcard_ndr_read(log, s, u.ppv, 0, sizeof(CHAR), type);
334
0
}
335
336
static LONG smartcard_ndr_read_w(wLog* log, wStream* s, WCHAR** data, ndr_ptr_t type)
337
0
{
338
0
  union
339
0
  {
340
0
    WCHAR** ppc;
341
0
    BYTE** ppv;
342
0
  } u;
343
0
  u.ppc = data;
344
0
  return smartcard_ndr_read(log, s, u.ppv, 0, sizeof(WCHAR), type);
345
0
}
346
347
static LONG smartcard_ndr_read_u(wLog* log, wStream* s, UUID** data)
348
0
{
349
0
  union
350
0
  {
351
0
    UUID** ppc;
352
0
    BYTE** ppv;
353
0
  } u;
354
0
  u.ppc = data;
355
0
  return smartcard_ndr_read(log, s, u.ppv, 1, sizeof(UUID), NDR_PTR_FIXED);
356
0
}
357
358
static char* smartcard_convert_string_list(const void* in, size_t bytes, BOOL unicode)
359
0
{
360
0
  size_t length = 0;
361
0
  union
362
0
  {
363
0
    const void* pv;
364
0
    const char* sz;
365
0
    const WCHAR* wz;
366
0
  } string;
367
0
  char* mszA = NULL;
368
369
0
  string.pv = in;
370
371
0
  if (bytes < 1)
372
0
    return NULL;
373
374
0
  if (in == NULL)
375
0
    return NULL;
376
377
0
  if (unicode)
378
0
  {
379
0
    mszA = ConvertMszWCharNToUtf8Alloc(string.wz, bytes / sizeof(WCHAR), &length);
380
0
    if (!mszA)
381
0
      return NULL;
382
0
  }
383
0
  else
384
0
  {
385
0
    mszA = (char*)calloc(bytes, sizeof(char));
386
0
    if (!mszA)
387
0
      return NULL;
388
0
    CopyMemory(mszA, string.sz, bytes - 1);
389
0
    length = bytes;
390
0
  }
391
392
0
  if (length < 1)
393
0
  {
394
0
    free(mszA);
395
0
    return NULL;
396
0
  }
397
0
  for (size_t index = 0; index < length - 1; index++)
398
0
  {
399
0
    if (mszA[index] == '\0')
400
0
      mszA[index] = ',';
401
0
  }
402
403
0
  return mszA;
404
0
}
405
406
static char* smartcard_msz_dump_a(const char* msz, size_t len, char* buffer, size_t bufferLen)
407
0
{
408
0
  char* buf = buffer;
409
0
  const char* cur = msz;
410
411
0
  while ((len > 0) && cur && cur[0] != '\0' && (bufferLen > 0))
412
0
  {
413
0
    size_t clen = strnlen(cur, len);
414
0
    int rc = _snprintf(buf, bufferLen, "%s", cur);
415
0
    bufferLen -= (size_t)rc;
416
0
    buf += rc;
417
418
0
    cur += clen;
419
0
  }
420
421
0
  return buffer;
422
0
}
423
424
static char* smartcard_msz_dump_w(const WCHAR* msz, size_t len, char* buffer, size_t bufferLen)
425
0
{
426
0
  size_t szlen = 0;
427
0
  if (!msz)
428
0
    return NULL;
429
0
  char* sz = ConvertMszWCharNToUtf8Alloc(msz, len, &szlen);
430
0
  if (!sz)
431
0
    return NULL;
432
433
0
  smartcard_msz_dump_a(sz, szlen, buffer, bufferLen);
434
0
  free(sz);
435
0
  return buffer;
436
0
}
437
438
static char* smartcard_array_dump(const void* pd, size_t len, char* buffer, size_t bufferLen)
439
0
{
440
0
  const BYTE* data = pd;
441
0
  int rc = 0;
442
0
  char* start = buffer;
443
444
0
  WINPR_ASSERT(buffer || (bufferLen == 0));
445
446
0
  if (!data && (len > 0))
447
0
  {
448
0
    (void)_snprintf(buffer, bufferLen, "{ NULL [%" PRIuz "] }", len);
449
0
    goto fail;
450
0
  }
451
452
0
  rc = _snprintf(buffer, bufferLen, "{ ");
453
0
  if ((rc < 0) || ((size_t)rc >= bufferLen))
454
0
    goto fail;
455
0
  buffer += rc;
456
0
  bufferLen -= (size_t)rc;
457
458
0
  for (size_t x = 0; x < len; x++)
459
0
  {
460
0
    rc = _snprintf(buffer, bufferLen, "%02X", data[x]);
461
0
    if ((rc < 0) || ((size_t)rc >= bufferLen))
462
0
      goto fail;
463
0
    buffer += rc;
464
0
    bufferLen -= (size_t)rc;
465
0
  }
466
467
0
  rc = _snprintf(buffer, bufferLen, " }");
468
0
  if ((rc < 0) || ((size_t)rc >= bufferLen))
469
0
    goto fail;
470
471
0
fail:
472
0
  return start;
473
0
}
474
475
static void smartcard_log_redir_handle(wLog* log, const REDIR_SCARDHANDLE* pHandle)
476
0
{
477
0
  char buffer[128] = { 0 };
478
479
0
  WINPR_ASSERT(pHandle);
480
0
  WLog_Print(log, g_LogLevel, "  hContext: %s",
481
0
             smartcard_array_dump(pHandle->pbHandle, pHandle->cbHandle, buffer, sizeof(buffer)));
482
0
}
483
484
static void smartcard_log_context(wLog* log, const REDIR_SCARDCONTEXT* phContext)
485
0
{
486
0
  char buffer[128] = { 0 };
487
488
0
  WINPR_ASSERT(phContext);
489
0
  WLog_Print(
490
0
      log, g_LogLevel, "hContext: %s",
491
0
      smartcard_array_dump(phContext->pbContext, phContext->cbContext, buffer, sizeof(buffer)));
492
0
}
493
494
static void smartcard_trace_context_and_string_call_a(wLog* log, const char* name,
495
                                                      const REDIR_SCARDCONTEXT* phContext,
496
                                                      const CHAR* sz)
497
0
{
498
0
  if (!WLog_IsLevelActive(log, g_LogLevel))
499
0
    return;
500
501
0
  WLog_Print(log, g_LogLevel, "%s {", name);
502
0
  smartcard_log_context(log, phContext);
503
0
  WLog_Print(log, g_LogLevel, "  sz=%s", sz);
504
505
0
  WLog_Print(log, g_LogLevel, "}");
506
0
}
507
508
static void smartcard_trace_context_and_string_call_w(wLog* log, const char* name,
509
                                                      const REDIR_SCARDCONTEXT* phContext,
510
                                                      const WCHAR* sz)
511
0
{
512
0
  char tmp[1024] = { 0 };
513
514
0
  if (!WLog_IsLevelActive(log, g_LogLevel))
515
0
    return;
516
517
0
  if (sz)
518
0
    (void)ConvertWCharToUtf8(sz, tmp, ARRAYSIZE(tmp));
519
520
0
  WLog_Print(log, g_LogLevel, "%s {", name);
521
0
  smartcard_log_context(log, phContext);
522
0
  WLog_Print(log, g_LogLevel, "  sz=%s", tmp);
523
0
  WLog_Print(log, g_LogLevel, "}");
524
0
}
525
526
static void smartcard_trace_context_call(wLog* log, const Context_Call* call, const char* name)
527
0
{
528
0
  WINPR_ASSERT(call);
529
530
0
  if (!WLog_IsLevelActive(log, g_LogLevel))
531
0
    return;
532
533
0
  WLog_Print(log, g_LogLevel, "%s_Call {", name);
534
0
  smartcard_log_context(log, &call->handles.hContext);
535
536
0
  WLog_Print(log, g_LogLevel, "}");
537
0
}
538
539
static void smartcard_trace_list_reader_groups_call(wLog* log, const ListReaderGroups_Call* call,
540
                                                    BOOL unicode)
541
0
{
542
0
  WINPR_ASSERT(call);
543
544
0
  if (!WLog_IsLevelActive(log, g_LogLevel))
545
0
    return;
546
547
0
  WLog_Print(log, g_LogLevel, "ListReaderGroups%s_Call {", unicode ? "W" : "A");
548
0
  smartcard_log_context(log, &call->handles.hContext);
549
550
0
  WLog_Print(log, g_LogLevel, "fmszGroupsIsNULL: %" PRId32 " cchGroups: 0x%08" PRIx32,
551
0
             call->fmszGroupsIsNULL, call->cchGroups);
552
0
  WLog_Print(log, g_LogLevel, "}");
553
0
}
554
555
static void dump_reader_states_return(wLog* log, const ReaderState_Return* rgReaderStates,
556
                                      UINT32 cReaders)
557
0
{
558
0
  WINPR_ASSERT(rgReaderStates || (cReaders == 0));
559
0
  for (UINT32 index = 0; index < cReaders; index++)
560
0
  {
561
0
    char buffer[1024] = { 0 };
562
563
0
    const ReaderState_Return* readerState = &rgReaderStates[index];
564
0
    char* szCurrentState = SCardGetReaderStateString(readerState->dwCurrentState);
565
0
    char* szEventState = SCardGetReaderStateString(readerState->dwEventState);
566
0
    WLog_Print(log, g_LogLevel, "\t[%" PRIu32 "]: dwCurrentState: %s (0x%08" PRIX32 ")", index,
567
0
               szCurrentState, readerState->dwCurrentState);
568
0
    WLog_Print(log, g_LogLevel, "\t[%" PRIu32 "]: dwEventState: %s (0x%08" PRIX32 ")", index,
569
0
               szEventState, readerState->dwEventState);
570
0
    free(szCurrentState);
571
0
    free(szEventState);
572
573
0
    WLog_Print(
574
0
        log, g_LogLevel, "\t[%" PRIu32 "]: cbAttr: %" PRIu32 " { %s }", index,
575
0
        readerState->cbAtr,
576
0
        smartcard_array_dump(readerState->rgbAtr, readerState->cbAtr, buffer, sizeof(buffer)));
577
0
  }
578
0
}
579
580
static void dump_reader_states_a(wLog* log, const SCARD_READERSTATEA* rgReaderStates,
581
                                 UINT32 cReaders)
582
0
{
583
0
  WINPR_ASSERT(rgReaderStates || (cReaders == 0));
584
0
  for (UINT32 index = 0; index < cReaders; index++)
585
0
  {
586
0
    char buffer[1024] = { 0 };
587
588
0
    const SCARD_READERSTATEA* readerState = &rgReaderStates[index];
589
590
0
    WLog_Print(log, g_LogLevel, "\t[%" PRIu32 "]: szReader: %s cbAtr: %" PRIu32 "", index,
591
0
               readerState->szReader, readerState->cbAtr);
592
0
    char* szCurrentState = SCardGetReaderStateString(readerState->dwCurrentState);
593
0
    char* szEventState = SCardGetReaderStateString(readerState->dwEventState);
594
0
    WLog_Print(log, g_LogLevel, "\t[%" PRIu32 "]: dwCurrentState: %s (0x%08" PRIX32 ")", index,
595
0
               szCurrentState, readerState->dwCurrentState);
596
0
    WLog_Print(log, g_LogLevel, "\t[%" PRIu32 "]: dwEventState: %s (0x%08" PRIX32 ")", index,
597
0
               szEventState, readerState->dwEventState);
598
0
    free(szCurrentState);
599
0
    free(szEventState);
600
601
0
    WLog_Print(
602
0
        log, g_LogLevel, "\t[%" PRIu32 "]: cbAttr: %" PRIu32 " { %s }", index,
603
0
        readerState->cbAtr,
604
0
        smartcard_array_dump(readerState->rgbAtr, readerState->cbAtr, buffer, sizeof(buffer)));
605
0
  }
606
0
}
607
608
static void dump_reader_states_w(wLog* log, const SCARD_READERSTATEW* rgReaderStates,
609
                                 UINT32 cReaders)
610
0
{
611
0
  WINPR_ASSERT(rgReaderStates || (cReaders == 0));
612
0
  for (UINT32 index = 0; index < cReaders; index++)
613
0
  {
614
0
    char buffer[1024] = { 0 };
615
616
0
    const SCARD_READERSTATEW* readerState = &rgReaderStates[index];
617
0
    (void)ConvertWCharToUtf8(readerState->szReader, buffer, sizeof(buffer));
618
0
    WLog_Print(log, g_LogLevel, "\t[%" PRIu32 "]: szReader: %s cbAtr: %" PRIu32 "", index,
619
0
               buffer, readerState->cbAtr);
620
0
    char* szCurrentState = SCardGetReaderStateString(readerState->dwCurrentState);
621
0
    char* szEventState = SCardGetReaderStateString(readerState->dwEventState);
622
0
    WLog_Print(log, g_LogLevel, "\t[%" PRIu32 "]: dwCurrentState: %s (0x%08" PRIX32 ")", index,
623
0
               szCurrentState, readerState->dwCurrentState);
624
0
    WLog_Print(log, g_LogLevel, "\t[%" PRIu32 "]: dwEventState: %s (0x%08" PRIX32 ")", index,
625
0
               szEventState, readerState->dwEventState);
626
0
    free(szCurrentState);
627
0
    free(szEventState);
628
629
0
    WLog_Print(
630
0
        log, g_LogLevel, "\t[%" PRIu32 "]: cbAttr: %" PRIu32 " { %s }", index,
631
0
        readerState->cbAtr,
632
0
        smartcard_array_dump(readerState->rgbAtr, readerState->cbAtr, buffer, sizeof(buffer)));
633
0
  }
634
0
}
635
636
static void smartcard_trace_get_status_change_w_call(wLog* log, const GetStatusChangeW_Call* call)
637
0
{
638
0
  WINPR_ASSERT(call);
639
640
0
  if (!WLog_IsLevelActive(log, g_LogLevel))
641
0
    return;
642
643
0
  WLog_Print(log, g_LogLevel, "GetStatusChangeW_Call {");
644
0
  smartcard_log_context(log, &call->handles.hContext);
645
646
0
  WLog_Print(log, g_LogLevel, "dwTimeOut: 0x%08" PRIX32 " cReaders: %" PRIu32 "", call->dwTimeOut,
647
0
             call->cReaders);
648
649
0
  dump_reader_states_w(log, call->rgReaderStates, call->cReaders);
650
651
0
  WLog_Print(log, g_LogLevel, "}");
652
0
}
653
654
static void smartcard_trace_list_reader_groups_return(wLog* log, const ListReaderGroups_Return* ret,
655
                                                      BOOL unicode)
656
0
{
657
0
  WINPR_ASSERT(ret);
658
659
0
  if (!WLog_IsLevelActive(log, g_LogLevel))
660
0
    return;
661
662
0
  char* mszA = smartcard_convert_string_list(ret->msz, ret->cBytes, unicode);
663
664
0
  WLog_Print(log, g_LogLevel, "ListReaderGroups%s_Return {", unicode ? "W" : "A");
665
0
  WLog_Print(log, g_LogLevel, "  ReturnCode: %s (0x%08" PRIx32 ")",
666
0
             SCardGetErrorString(ret->ReturnCode),
667
0
             WINPR_CXX_COMPAT_CAST(UINT32, ret->ReturnCode));
668
0
  WLog_Print(log, g_LogLevel, "  cBytes: %" PRIu32 " msz: %s", ret->cBytes, mszA);
669
0
  WLog_Print(log, g_LogLevel, "}");
670
0
  free(mszA);
671
0
}
672
673
static void smartcard_trace_list_readers_call(wLog* log, const ListReaders_Call* call, BOOL unicode)
674
0
{
675
0
  WINPR_ASSERT(call);
676
677
0
  if (!WLog_IsLevelActive(log, g_LogLevel))
678
0
    return;
679
680
0
  char* mszGroupsA = smartcard_convert_string_list(call->mszGroups, call->cBytes, unicode);
681
682
0
  WLog_Print(log, g_LogLevel, "ListReaders%s_Call {", unicode ? "W" : "A");
683
0
  smartcard_log_context(log, &call->handles.hContext);
684
685
0
  WLog_Print(log, g_LogLevel,
686
0
             "cBytes: %" PRIu32 " mszGroups: %s fmszReadersIsNULL: %" PRId32
687
0
             " cchReaders: 0x%08" PRIX32 "",
688
0
             call->cBytes, mszGroupsA, call->fmszReadersIsNULL, call->cchReaders);
689
0
  WLog_Print(log, g_LogLevel, "}");
690
691
0
  free(mszGroupsA);
692
0
}
693
694
static void smartcard_trace_locate_cards_by_atr_a_call(wLog* log,
695
                                                       const LocateCardsByATRA_Call* call)
696
0
{
697
0
  WINPR_ASSERT(call);
698
699
0
  if (!WLog_IsLevelActive(log, g_LogLevel))
700
0
    return;
701
702
0
  WLog_Print(log, g_LogLevel, "LocateCardsByATRA_Call {");
703
0
  smartcard_log_context(log, &call->handles.hContext);
704
705
0
  dump_reader_states_a(log, call->rgReaderStates, call->cReaders);
706
707
0
  WLog_Print(log, g_LogLevel, "}");
708
0
}
709
710
static void smartcard_trace_locate_cards_a_call(wLog* log, const LocateCardsA_Call* call)
711
0
{
712
0
  char buffer[8192] = { 0 };
713
714
0
  WINPR_ASSERT(call);
715
716
0
  if (!WLog_IsLevelActive(log, g_LogLevel))
717
0
    return;
718
719
0
  WLog_Print(log, g_LogLevel, "LocateCardsA_Call {");
720
0
  smartcard_log_context(log, &call->handles.hContext);
721
0
  WLog_Print(log, g_LogLevel, " cBytes=%" PRIu32, call->cBytes);
722
0
  WLog_Print(log, g_LogLevel, " mszCards=%s",
723
0
             smartcard_msz_dump_a(call->mszCards, call->cBytes, buffer, sizeof(buffer)));
724
0
  WLog_Print(log, g_LogLevel, " cReaders=%" PRIu32, call->cReaders);
725
0
  dump_reader_states_a(log, call->rgReaderStates, call->cReaders);
726
727
0
  WLog_Print(log, g_LogLevel, "}");
728
0
}
729
730
static void smartcard_trace_locate_cards_return(wLog* log, const LocateCards_Return* ret)
731
0
{
732
0
  WINPR_ASSERT(ret);
733
734
0
  if (!WLog_IsLevelActive(log, g_LogLevel))
735
0
    return;
736
737
0
  WLog_Print(log, g_LogLevel, "LocateCards_Return {");
738
0
  WLog_Print(log, g_LogLevel, "  ReturnCode: %s (0x%08" PRIX32 ")",
739
0
             SCardGetErrorString(ret->ReturnCode),
740
0
             WINPR_CXX_COMPAT_CAST(UINT32, ret->ReturnCode));
741
742
0
  if (ret->ReturnCode == SCARD_S_SUCCESS)
743
0
  {
744
0
    WLog_Print(log, g_LogLevel, "  cReaders=%" PRIu32, ret->cReaders);
745
0
  }
746
0
  WLog_Print(log, g_LogLevel, "}");
747
0
}
748
749
static void smartcard_trace_get_reader_icon_return(wLog* log, const GetReaderIcon_Return* ret)
750
0
{
751
0
  WINPR_ASSERT(ret);
752
753
0
  if (!WLog_IsLevelActive(log, g_LogLevel))
754
0
    return;
755
756
0
  WLog_Print(log, g_LogLevel, "GetReaderIcon_Return {");
757
0
  WLog_Print(log, g_LogLevel, "  ReturnCode: %s (0x%08" PRIX32 ")",
758
0
             SCardGetErrorString(ret->ReturnCode),
759
0
             WINPR_CXX_COMPAT_CAST(UINT32, ret->ReturnCode));
760
761
0
  if (ret->ReturnCode == SCARD_S_SUCCESS)
762
0
  {
763
0
    WLog_Print(log, g_LogLevel, "  cbDataLen=%" PRIu32, ret->cbDataLen);
764
0
  }
765
0
  WLog_Print(log, g_LogLevel, "}");
766
0
}
767
768
static void smartcard_trace_get_transmit_count_return(wLog* log, const GetTransmitCount_Return* ret)
769
0
{
770
0
  WINPR_ASSERT(ret);
771
772
0
  if (!WLog_IsLevelActive(log, g_LogLevel))
773
0
    return;
774
775
0
  WLog_Print(log, g_LogLevel, "GetTransmitCount_Return {");
776
0
  WLog_Print(log, g_LogLevel, "  ReturnCode: %s (0x%08" PRIX32 ")",
777
0
             SCardGetErrorString(ret->ReturnCode),
778
0
             WINPR_CXX_COMPAT_CAST(UINT32, ret->ReturnCode));
779
780
0
  WLog_Print(log, g_LogLevel, "  cTransmitCount=%" PRIu32, ret->cTransmitCount);
781
0
  WLog_Print(log, g_LogLevel, "}");
782
0
}
783
784
static void smartcard_trace_read_cache_return(wLog* log, const ReadCache_Return* ret)
785
0
{
786
0
  WINPR_ASSERT(ret);
787
788
0
  if (!WLog_IsLevelActive(log, g_LogLevel))
789
0
    return;
790
791
0
  WLog_Print(log, g_LogLevel, "ReadCache_Return {");
792
0
  WLog_Print(log, g_LogLevel, "  ReturnCode: %s (0x%08" PRIX32 ")",
793
0
             SCardGetErrorString(ret->ReturnCode),
794
0
             WINPR_CXX_COMPAT_CAST(UINT32, ret->ReturnCode));
795
796
0
  if (ret->ReturnCode == SCARD_S_SUCCESS)
797
0
  {
798
0
    char buffer[1024] = { 0 };
799
0
    WLog_Print(log, g_LogLevel, " cbDataLen=%" PRIu32, ret->cbDataLen);
800
0
    WLog_Print(log, g_LogLevel, "  cbData: %s",
801
0
               smartcard_array_dump(ret->pbData, ret->cbDataLen, buffer, sizeof(buffer)));
802
0
  }
803
0
  WLog_Print(log, g_LogLevel, "}");
804
0
}
805
806
static void smartcard_trace_locate_cards_w_call(wLog* log, const LocateCardsW_Call* call)
807
0
{
808
0
  WINPR_ASSERT(call);
809
0
  char buffer[8192] = { 0 };
810
811
0
  if (!WLog_IsLevelActive(log, g_LogLevel))
812
0
    return;
813
814
0
  WLog_Print(log, g_LogLevel, "LocateCardsW_Call {");
815
0
  smartcard_log_context(log, &call->handles.hContext);
816
0
  WLog_Print(log, g_LogLevel, " cBytes=%" PRIu32, call->cBytes);
817
0
  WLog_Print(log, g_LogLevel, " sz2=%s",
818
0
             smartcard_msz_dump_w(call->mszCards, call->cBytes, buffer, sizeof(buffer)));
819
0
  WLog_Print(log, g_LogLevel, " cReaders=%" PRIu32, call->cReaders);
820
0
  dump_reader_states_w(log, call->rgReaderStates, call->cReaders);
821
0
  WLog_Print(log, g_LogLevel, "}");
822
0
}
823
824
static void smartcard_trace_list_readers_return(wLog* log, const ListReaders_Return* ret,
825
                                                BOOL unicode)
826
0
{
827
0
  WINPR_ASSERT(ret);
828
829
0
  if (!WLog_IsLevelActive(log, g_LogLevel))
830
0
    return;
831
832
0
  WLog_Print(log, g_LogLevel, "ListReaders%s_Return {", unicode ? "W" : "A");
833
0
  WLog_Print(log, g_LogLevel, "  ReturnCode: %s (0x%08" PRIX32 ")",
834
0
             SCardGetErrorString(ret->ReturnCode),
835
0
             WINPR_CXX_COMPAT_CAST(UINT32, ret->ReturnCode));
836
837
0
  if (ret->ReturnCode != SCARD_S_SUCCESS)
838
0
  {
839
0
    WLog_Print(log, g_LogLevel, "}");
840
0
    return;
841
0
  }
842
843
0
  char* mszA = smartcard_convert_string_list(ret->msz, ret->cBytes, unicode);
844
845
0
  WLog_Print(log, g_LogLevel, "  cBytes: %" PRIu32 " msz: %s", ret->cBytes, mszA);
846
0
  WLog_Print(log, g_LogLevel, "}");
847
0
  free(mszA);
848
0
}
849
850
static void smartcard_trace_get_status_change_return(wLog* log, const GetStatusChange_Return* ret,
851
                                                     BOOL unicode)
852
0
{
853
0
  WINPR_ASSERT(ret);
854
855
0
  if (!WLog_IsLevelActive(log, g_LogLevel))
856
0
    return;
857
858
0
  WLog_Print(log, g_LogLevel, "GetStatusChange%s_Return {", unicode ? "W" : "A");
859
0
  WLog_Print(log, g_LogLevel, "  ReturnCode: %s (0x%08" PRIX32 ")",
860
0
             SCardGetErrorString(ret->ReturnCode),
861
0
             WINPR_CXX_COMPAT_CAST(UINT32, ret->ReturnCode));
862
0
  WLog_Print(log, g_LogLevel, "  cReaders: %" PRIu32 "", ret->cReaders);
863
864
0
  dump_reader_states_return(log, ret->rgReaderStates, ret->cReaders);
865
866
0
  if (!ret->rgReaderStates && (ret->cReaders > 0))
867
0
  {
868
0
    WLog_Print(log, g_LogLevel, "    [INVALID STATE] rgReaderStates=NULL, cReaders=%" PRIu32,
869
0
               ret->cReaders);
870
0
  }
871
0
  else if (ret->ReturnCode != SCARD_S_SUCCESS)
872
0
  {
873
0
    WLog_Print(log, g_LogLevel, "    [INVALID RETURN] rgReaderStates, cReaders=%" PRIu32,
874
0
               ret->cReaders);
875
0
  }
876
0
  else
877
0
  {
878
0
    for (UINT32 index = 0; index < ret->cReaders; index++)
879
0
    {
880
0
      char buffer[1024] = { 0 };
881
0
      const ReaderState_Return* rgReaderState = &(ret->rgReaderStates[index]);
882
0
      char* szCurrentState = SCardGetReaderStateString(rgReaderState->dwCurrentState);
883
0
      char* szEventState = SCardGetReaderStateString(rgReaderState->dwEventState);
884
0
      WLog_Print(log, g_LogLevel, "    [%" PRIu32 "]: dwCurrentState: %s (0x%08" PRIX32 ")",
885
0
                 index, szCurrentState, rgReaderState->dwCurrentState);
886
0
      WLog_Print(log, g_LogLevel, "    [%" PRIu32 "]: dwEventState: %s (0x%08" PRIX32 ")",
887
0
                 index, szEventState, rgReaderState->dwEventState);
888
0
      WLog_Print(log, g_LogLevel, "    [%" PRIu32 "]: cbAtr: %" PRIu32 " rgbAtr: %s", index,
889
0
                 rgReaderState->cbAtr,
890
0
                 smartcard_array_dump(rgReaderState->rgbAtr, rgReaderState->cbAtr, buffer,
891
0
                                      sizeof(buffer)));
892
0
      free(szCurrentState);
893
0
      free(szEventState);
894
0
    }
895
0
  }
896
897
0
  WLog_Print(log, g_LogLevel, "}");
898
0
}
899
900
static void smartcard_trace_context_and_two_strings_a_call(wLog* log,
901
                                                           const ContextAndTwoStringA_Call* call)
902
0
{
903
0
  WINPR_ASSERT(call);
904
905
0
  if (!WLog_IsLevelActive(log, g_LogLevel))
906
0
    return;
907
908
0
  WLog_Print(log, g_LogLevel, "ContextAndTwoStringW_Call {");
909
0
  smartcard_log_context(log, &call->handles.hContext);
910
0
  WLog_Print(log, g_LogLevel, " sz1=%s", call->sz1);
911
0
  WLog_Print(log, g_LogLevel, " sz2=%s", call->sz2);
912
0
  WLog_Print(log, g_LogLevel, "}");
913
0
}
914
915
static void smartcard_trace_context_and_two_strings_w_call(wLog* log,
916
                                                           const ContextAndTwoStringW_Call* call)
917
0
{
918
0
  WINPR_ASSERT(call);
919
0
  char sz1[1024] = { 0 };
920
0
  char sz2[1024] = { 0 };
921
922
0
  if (!WLog_IsLevelActive(log, g_LogLevel))
923
0
    return;
924
0
  if (call->sz1)
925
0
    (void)ConvertWCharToUtf8(call->sz1, sz1, ARRAYSIZE(sz1));
926
0
  if (call->sz2)
927
0
    (void)ConvertWCharToUtf8(call->sz2, sz2, ARRAYSIZE(sz2));
928
929
0
  WLog_Print(log, g_LogLevel, "ContextAndTwoStringW_Call {");
930
0
  smartcard_log_context(log, &call->handles.hContext);
931
0
  WLog_Print(log, g_LogLevel, " sz1=%s", sz1);
932
0
  WLog_Print(log, g_LogLevel, " sz2=%s", sz2);
933
0
  WLog_Print(log, g_LogLevel, "}");
934
0
}
935
936
static void smartcard_trace_get_transmit_count_call(wLog* log, const GetTransmitCount_Call* call)
937
0
{
938
0
  WINPR_ASSERT(call);
939
940
0
  if (!WLog_IsLevelActive(log, g_LogLevel))
941
0
    return;
942
943
0
  WLog_Print(log, g_LogLevel, "GetTransmitCount_Call {");
944
0
  smartcard_log_context(log, &call->handles.hContext);
945
0
  smartcard_log_redir_handle(log, &call->handles.hCard);
946
947
0
  WLog_Print(log, g_LogLevel, "}");
948
0
}
949
950
static void smartcard_trace_write_cache_a_call(wLog* log, const WriteCacheA_Call* call)
951
0
{
952
0
  WINPR_ASSERT(call);
953
0
  char buffer[1024] = { 0 };
954
955
0
  if (!WLog_IsLevelActive(log, g_LogLevel))
956
0
    return;
957
958
0
  WLog_Print(log, g_LogLevel, "WriteCacheA_Call {");
959
960
0
  WLog_Print(log, g_LogLevel, "  szLookupName=%s", call->szLookupName);
961
962
0
  smartcard_log_context(log, &call->Common.handles.hContext);
963
0
  WLog_Print(
964
0
      log, g_LogLevel, "..CardIdentifier=%s",
965
0
      smartcard_array_dump(call->Common.CardIdentifier, sizeof(UUID), buffer, sizeof(buffer)));
966
0
  WLog_Print(log, g_LogLevel, "  FreshnessCounter=%" PRIu32, call->Common.FreshnessCounter);
967
0
  WLog_Print(log, g_LogLevel, "  cbDataLen=%" PRIu32, call->Common.cbDataLen);
968
0
  WLog_Print(
969
0
      log, g_LogLevel, "  pbData=%s",
970
0
      smartcard_array_dump(call->Common.pbData, call->Common.cbDataLen, buffer, sizeof(buffer)));
971
0
  WLog_Print(log, g_LogLevel, "}");
972
0
}
973
974
static void smartcard_trace_write_cache_w_call(wLog* log, const WriteCacheW_Call* call)
975
0
{
976
0
  WINPR_ASSERT(call);
977
0
  char tmp[1024] = { 0 };
978
0
  char buffer[1024] = { 0 };
979
980
0
  if (!WLog_IsLevelActive(log, g_LogLevel))
981
0
    return;
982
983
0
  WLog_Print(log, g_LogLevel, "WriteCacheW_Call {");
984
985
0
  if (call->szLookupName)
986
0
    (void)ConvertWCharToUtf8(call->szLookupName, tmp, ARRAYSIZE(tmp));
987
0
  WLog_Print(log, g_LogLevel, "  szLookupName=%s", tmp);
988
989
0
  smartcard_log_context(log, &call->Common.handles.hContext);
990
0
  WLog_Print(
991
0
      log, g_LogLevel, "..CardIdentifier=%s",
992
0
      smartcard_array_dump(call->Common.CardIdentifier, sizeof(UUID), buffer, sizeof(buffer)));
993
0
  WLog_Print(log, g_LogLevel, "  FreshnessCounter=%" PRIu32, call->Common.FreshnessCounter);
994
0
  WLog_Print(log, g_LogLevel, "  cbDataLen=%" PRIu32, call->Common.cbDataLen);
995
0
  WLog_Print(
996
0
      log, g_LogLevel, "  pbData=%s",
997
0
      smartcard_array_dump(call->Common.pbData, call->Common.cbDataLen, buffer, sizeof(buffer)));
998
0
  WLog_Print(log, g_LogLevel, "}");
999
0
}
1000
1001
static void smartcard_trace_read_cache_a_call(wLog* log, const ReadCacheA_Call* call)
1002
0
{
1003
0
  WINPR_ASSERT(call);
1004
0
  char buffer[1024] = { 0 };
1005
1006
0
  if (!WLog_IsLevelActive(log, g_LogLevel))
1007
0
    return;
1008
1009
0
  WLog_Print(log, g_LogLevel, "ReadCacheA_Call {");
1010
1011
0
  WLog_Print(log, g_LogLevel, "  szLookupName=%s", call->szLookupName);
1012
0
  smartcard_log_context(log, &call->Common.handles.hContext);
1013
0
  WLog_Print(
1014
0
      log, g_LogLevel, "..CardIdentifier=%s",
1015
0
      smartcard_array_dump(call->Common.CardIdentifier, sizeof(UUID), buffer, sizeof(buffer)));
1016
0
  WLog_Print(log, g_LogLevel, "  FreshnessCounter=%" PRIu32, call->Common.FreshnessCounter);
1017
0
  WLog_Print(log, g_LogLevel, "  fPbDataIsNULL=%" PRId32, call->Common.fPbDataIsNULL);
1018
0
  WLog_Print(log, g_LogLevel, "  cbDataLen=%" PRIu32, call->Common.cbDataLen);
1019
1020
0
  WLog_Print(log, g_LogLevel, "}");
1021
0
}
1022
1023
static void smartcard_trace_read_cache_w_call(wLog* log, const ReadCacheW_Call* call)
1024
0
{
1025
0
  WINPR_ASSERT(call);
1026
0
  char tmp[1024] = { 0 };
1027
0
  char buffer[1024] = { 0 };
1028
1029
0
  if (!WLog_IsLevelActive(log, g_LogLevel))
1030
0
    return;
1031
1032
0
  WLog_Print(log, g_LogLevel, "ReadCacheW_Call {");
1033
0
  if (call->szLookupName)
1034
0
    (void)ConvertWCharToUtf8(call->szLookupName, tmp, ARRAYSIZE(tmp));
1035
0
  WLog_Print(log, g_LogLevel, "  szLookupName=%s", tmp);
1036
1037
0
  smartcard_log_context(log, &call->Common.handles.hContext);
1038
0
  WLog_Print(
1039
0
      log, g_LogLevel, "..CardIdentifier=%s",
1040
0
      smartcard_array_dump(call->Common.CardIdentifier, sizeof(UUID), buffer, sizeof(buffer)));
1041
0
  WLog_Print(log, g_LogLevel, "  FreshnessCounter=%" PRIu32, call->Common.FreshnessCounter);
1042
0
  WLog_Print(log, g_LogLevel, "  fPbDataIsNULL=%" PRId32, call->Common.fPbDataIsNULL);
1043
0
  WLog_Print(log, g_LogLevel, "  cbDataLen=%" PRIu32, call->Common.cbDataLen);
1044
1045
0
  WLog_Print(log, g_LogLevel, "}");
1046
0
}
1047
1048
static void smartcard_trace_transmit_call(wLog* log, const Transmit_Call* call)
1049
0
{
1050
0
  WINPR_ASSERT(call);
1051
0
  UINT32 cbExtraBytes = 0;
1052
0
  BYTE* pbExtraBytes = NULL;
1053
1054
0
  if (!WLog_IsLevelActive(log, g_LogLevel))
1055
0
    return;
1056
1057
0
  WLog_Print(log, g_LogLevel, "Transmit_Call {");
1058
0
  smartcard_log_context(log, &call->handles.hContext);
1059
0
  smartcard_log_redir_handle(log, &call->handles.hCard);
1060
1061
0
  if (call->pioSendPci)
1062
0
  {
1063
0
    cbExtraBytes = (UINT32)(call->pioSendPci->cbPciLength - sizeof(SCARD_IO_REQUEST));
1064
0
    pbExtraBytes = &((BYTE*)call->pioSendPci)[sizeof(SCARD_IO_REQUEST)];
1065
0
    WLog_Print(log, g_LogLevel, "pioSendPci: dwProtocol: %" PRIu32 " cbExtraBytes: %" PRIu32 "",
1066
0
               call->pioSendPci->dwProtocol, cbExtraBytes);
1067
1068
0
    if (cbExtraBytes)
1069
0
    {
1070
0
      char buffer[1024] = { 0 };
1071
0
      WLog_Print(log, g_LogLevel, "pbExtraBytes: %s",
1072
0
                 smartcard_array_dump(pbExtraBytes, cbExtraBytes, buffer, sizeof(buffer)));
1073
0
    }
1074
0
  }
1075
0
  else
1076
0
  {
1077
0
    WLog_Print(log, g_LogLevel, "pioSendPci: null");
1078
0
  }
1079
1080
0
  WLog_Print(log, g_LogLevel, "cbSendLength: %" PRIu32 "", call->cbSendLength);
1081
1082
0
  if (call->pbSendBuffer)
1083
0
  {
1084
0
    char buffer[1024] = { 0 };
1085
0
    WLog_Print(
1086
0
        log, g_LogLevel, "pbSendBuffer: %s",
1087
0
        smartcard_array_dump(call->pbSendBuffer, call->cbSendLength, buffer, sizeof(buffer)));
1088
0
  }
1089
0
  else
1090
0
  {
1091
0
    WLog_Print(log, g_LogLevel, "pbSendBuffer: null");
1092
0
  }
1093
1094
0
  if (call->pioRecvPci)
1095
0
  {
1096
0
    cbExtraBytes = (UINT32)(call->pioRecvPci->cbPciLength - sizeof(SCARD_IO_REQUEST));
1097
0
    pbExtraBytes = &((BYTE*)call->pioRecvPci)[sizeof(SCARD_IO_REQUEST)];
1098
0
    WLog_Print(log, g_LogLevel, "pioRecvPci: dwProtocol: %" PRIu32 " cbExtraBytes: %" PRIu32 "",
1099
0
               call->pioRecvPci->dwProtocol, cbExtraBytes);
1100
1101
0
    if (cbExtraBytes)
1102
0
    {
1103
0
      char buffer[1024] = { 0 };
1104
0
      WLog_Print(log, g_LogLevel, "pbExtraBytes: %s",
1105
0
                 smartcard_array_dump(pbExtraBytes, cbExtraBytes, buffer, sizeof(buffer)));
1106
0
    }
1107
0
  }
1108
0
  else
1109
0
  {
1110
0
    WLog_Print(log, g_LogLevel, "pioRecvPci: null");
1111
0
  }
1112
1113
0
  WLog_Print(log, g_LogLevel, "fpbRecvBufferIsNULL: %" PRId32 " cbRecvLength: %" PRIu32 "",
1114
0
             call->fpbRecvBufferIsNULL, call->cbRecvLength);
1115
0
  WLog_Print(log, g_LogLevel, "}");
1116
0
}
1117
1118
static void smartcard_trace_locate_cards_by_atr_w_call(wLog* log,
1119
                                                       const LocateCardsByATRW_Call* call)
1120
0
{
1121
0
  WINPR_ASSERT(call);
1122
1123
0
  if (!WLog_IsLevelActive(log, g_LogLevel))
1124
0
    return;
1125
1126
0
  WLog_Print(log, g_LogLevel, "LocateCardsByATRW_Call {");
1127
0
  smartcard_log_context(log, &call->handles.hContext);
1128
1129
0
  dump_reader_states_w(log, call->rgReaderStates, call->cReaders);
1130
1131
0
  WLog_Print(log, g_LogLevel, "}");
1132
0
}
1133
1134
static void smartcard_trace_transmit_return(wLog* log, const Transmit_Return* ret)
1135
0
{
1136
0
  WINPR_ASSERT(ret);
1137
0
  UINT32 cbExtraBytes = 0;
1138
0
  BYTE* pbExtraBytes = NULL;
1139
1140
0
  if (!WLog_IsLevelActive(log, g_LogLevel))
1141
0
    return;
1142
1143
0
  WLog_Print(log, g_LogLevel, "Transmit_Return {");
1144
0
  WLog_Print(log, g_LogLevel, "  ReturnCode: %s (0x%08" PRIX32 ")",
1145
0
             SCardGetErrorString(ret->ReturnCode),
1146
0
             WINPR_CXX_COMPAT_CAST(UINT32, ret->ReturnCode));
1147
1148
0
  if (ret->pioRecvPci)
1149
0
  {
1150
0
    cbExtraBytes = (UINT32)(ret->pioRecvPci->cbPciLength - sizeof(SCARD_IO_REQUEST));
1151
0
    pbExtraBytes = &((BYTE*)ret->pioRecvPci)[sizeof(SCARD_IO_REQUEST)];
1152
0
    WLog_Print(log, g_LogLevel,
1153
0
               "  pioRecvPci: dwProtocol: %" PRIu32 " cbExtraBytes: %" PRIu32 "",
1154
0
               ret->pioRecvPci->dwProtocol, cbExtraBytes);
1155
1156
0
    if (cbExtraBytes)
1157
0
    {
1158
0
      char buffer[1024] = { 0 };
1159
0
      WLog_Print(log, g_LogLevel, "  pbExtraBytes: %s",
1160
0
                 smartcard_array_dump(pbExtraBytes, cbExtraBytes, buffer, sizeof(buffer)));
1161
0
    }
1162
0
  }
1163
0
  else
1164
0
  {
1165
0
    WLog_Print(log, g_LogLevel, "  pioRecvPci: null");
1166
0
  }
1167
1168
0
  WLog_Print(log, g_LogLevel, "  cbRecvLength: %" PRIu32 "", ret->cbRecvLength);
1169
1170
0
  if (ret->pbRecvBuffer)
1171
0
  {
1172
0
    char buffer[1024] = { 0 };
1173
0
    WLog_Print(
1174
0
        log, g_LogLevel, "  pbRecvBuffer: %s",
1175
0
        smartcard_array_dump(ret->pbRecvBuffer, ret->cbRecvLength, buffer, sizeof(buffer)));
1176
0
  }
1177
0
  else
1178
0
  {
1179
0
    WLog_Print(log, g_LogLevel, "  pbRecvBuffer: null");
1180
0
  }
1181
1182
0
  WLog_Print(log, g_LogLevel, "}");
1183
0
}
1184
1185
static void smartcard_trace_control_return(wLog* log, const Control_Return* ret)
1186
0
{
1187
0
  WINPR_ASSERT(ret);
1188
1189
0
  if (!WLog_IsLevelActive(log, g_LogLevel))
1190
0
    return;
1191
1192
0
  WLog_Print(log, g_LogLevel, "Control_Return {");
1193
0
  WLog_Print(log, g_LogLevel, "  ReturnCode: %s (0x%08" PRIX32 ")",
1194
0
             SCardGetErrorString(ret->ReturnCode),
1195
0
             WINPR_CXX_COMPAT_CAST(UINT32, ret->ReturnCode));
1196
0
  WLog_Print(log, g_LogLevel, "  cbOutBufferSize: %" PRIu32 "", ret->cbOutBufferSize);
1197
1198
0
  if (ret->pvOutBuffer)
1199
0
  {
1200
0
    char buffer[1024] = { 0 };
1201
0
    WLog_Print(
1202
0
        log, g_LogLevel, "pvOutBuffer: %s",
1203
0
        smartcard_array_dump(ret->pvOutBuffer, ret->cbOutBufferSize, buffer, sizeof(buffer)));
1204
0
  }
1205
0
  else
1206
0
  {
1207
0
    WLog_Print(log, g_LogLevel, "pvOutBuffer: null");
1208
0
  }
1209
1210
0
  WLog_Print(log, g_LogLevel, "}");
1211
0
}
1212
1213
static void smartcard_trace_control_call(wLog* log, const Control_Call* call)
1214
0
{
1215
0
  WINPR_ASSERT(call);
1216
1217
0
  if (!WLog_IsLevelActive(log, g_LogLevel))
1218
0
    return;
1219
1220
0
  WLog_Print(log, g_LogLevel, "Control_Call {");
1221
0
  smartcard_log_context(log, &call->handles.hContext);
1222
0
  smartcard_log_redir_handle(log, &call->handles.hCard);
1223
1224
0
  WLog_Print(log, g_LogLevel,
1225
0
             "dwControlCode: 0x%08" PRIX32 " cbInBufferSize: %" PRIu32
1226
0
             " fpvOutBufferIsNULL: %" PRId32 " cbOutBufferSize: %" PRIu32 "",
1227
0
             call->dwControlCode, call->cbInBufferSize, call->fpvOutBufferIsNULL,
1228
0
             call->cbOutBufferSize);
1229
1230
0
  if (call->pvInBuffer)
1231
0
  {
1232
0
    char buffer[1024] = { 0 };
1233
0
    WLog_Print(
1234
0
        log, WLOG_DEBUG, "pbInBuffer: %s",
1235
0
        smartcard_array_dump(call->pvInBuffer, call->cbInBufferSize, buffer, sizeof(buffer)));
1236
0
  }
1237
0
  else
1238
0
  {
1239
0
    WLog_Print(log, g_LogLevel, "pvInBuffer: null");
1240
0
  }
1241
1242
0
  WLog_Print(log, g_LogLevel, "}");
1243
0
}
1244
1245
static void smartcard_trace_set_attrib_call(wLog* log, const SetAttrib_Call* call)
1246
0
{
1247
0
  WINPR_ASSERT(call);
1248
0
  char buffer[8192] = { 0 };
1249
1250
0
  if (!WLog_IsLevelActive(log, g_LogLevel))
1251
0
    return;
1252
1253
0
  WLog_Print(log, g_LogLevel, "GetAttrib_Call {");
1254
0
  smartcard_log_context(log, &call->handles.hContext);
1255
0
  smartcard_log_redir_handle(log, &call->handles.hCard);
1256
0
  WLog_Print(log, g_LogLevel, "dwAttrId: 0x%08" PRIX32, call->dwAttrId);
1257
0
  WLog_Print(log, g_LogLevel, "cbAttrLen: 0x%08" PRIx32, call->cbAttrLen);
1258
0
  WLog_Print(log, g_LogLevel, "pbAttr: %s",
1259
0
             smartcard_array_dump(call->pbAttr, call->cbAttrLen, buffer, sizeof(buffer)));
1260
0
  WLog_Print(log, g_LogLevel, "}");
1261
0
}
1262
1263
static void smartcard_trace_get_attrib_return(wLog* log, const GetAttrib_Return* ret,
1264
                                              DWORD dwAttrId)
1265
0
{
1266
0
  WINPR_ASSERT(ret);
1267
0
  char buffer[1024] = { 0 };
1268
1269
0
  if (!WLog_IsLevelActive(log, g_LogLevel))
1270
0
    return;
1271
1272
0
  WLog_Print(log, g_LogLevel, "GetAttrib_Return {");
1273
0
  WLog_Print(log, g_LogLevel, "  ReturnCode: %s (0x%08" PRIX32 ")",
1274
0
             SCardGetErrorString(ret->ReturnCode),
1275
0
             WINPR_CXX_COMPAT_CAST(UINT32, ret->ReturnCode));
1276
0
  WLog_Print(log, g_LogLevel, "  dwAttrId: %s (0x%08" PRIX32 ") cbAttrLen: 0x%08" PRIX32 "",
1277
0
             SCardGetAttributeString(dwAttrId), dwAttrId, ret->cbAttrLen);
1278
0
  WLog_Print(log, g_LogLevel, "  %s",
1279
0
             smartcard_array_dump(ret->pbAttr, ret->cbAttrLen, buffer, sizeof(buffer)));
1280
1281
0
  WLog_Print(log, g_LogLevel, "}");
1282
0
}
1283
1284
static void smartcard_trace_get_attrib_call(wLog* log, const GetAttrib_Call* call)
1285
0
{
1286
0
  WINPR_ASSERT(call);
1287
1288
0
  if (!WLog_IsLevelActive(log, g_LogLevel))
1289
0
    return;
1290
1291
0
  WLog_Print(log, g_LogLevel, "GetAttrib_Call {");
1292
0
  smartcard_log_context(log, &call->handles.hContext);
1293
0
  smartcard_log_redir_handle(log, &call->handles.hCard);
1294
1295
0
  WLog_Print(log, g_LogLevel,
1296
0
             "dwAttrId: %s (0x%08" PRIX32 ") fpbAttrIsNULL: %" PRId32 " cbAttrLen: 0x%08" PRIX32
1297
0
             "",
1298
0
             SCardGetAttributeString(call->dwAttrId), call->dwAttrId, call->fpbAttrIsNULL,
1299
0
             call->cbAttrLen);
1300
0
  WLog_Print(log, g_LogLevel, "}");
1301
0
}
1302
1303
static void smartcard_trace_status_call(wLog* log, const Status_Call* call, BOOL unicode)
1304
0
{
1305
0
  WINPR_ASSERT(call);
1306
1307
0
  if (!WLog_IsLevelActive(log, g_LogLevel))
1308
0
    return;
1309
1310
0
  WLog_Print(log, g_LogLevel, "Status%s_Call {", unicode ? "W" : "A");
1311
0
  smartcard_log_context(log, &call->handles.hContext);
1312
0
  smartcard_log_redir_handle(log, &call->handles.hCard);
1313
1314
0
  WLog_Print(log, g_LogLevel,
1315
0
             "fmszReaderNamesIsNULL: %" PRId32 " cchReaderLen: %" PRIu32 " cbAtrLen: %" PRIu32 "",
1316
0
             call->fmszReaderNamesIsNULL, call->cchReaderLen, call->cbAtrLen);
1317
0
  WLog_Print(log, g_LogLevel, "}");
1318
0
}
1319
1320
static void smartcard_trace_status_return(wLog* log, const Status_Return* ret, BOOL unicode)
1321
0
{
1322
0
  WINPR_ASSERT(ret);
1323
0
  char* mszReaderNamesA = NULL;
1324
0
  char buffer[1024] = { 0 };
1325
0
  DWORD cBytes = 0;
1326
1327
0
  if (!WLog_IsLevelActive(log, g_LogLevel))
1328
0
    return;
1329
0
  cBytes = ret->cBytes;
1330
0
  if (ret->ReturnCode != SCARD_S_SUCCESS)
1331
0
    cBytes = 0;
1332
0
  if (cBytes == SCARD_AUTOALLOCATE)
1333
0
    cBytes = 0;
1334
0
  mszReaderNamesA = smartcard_convert_string_list(ret->mszReaderNames, cBytes, unicode);
1335
1336
0
  WLog_Print(log, g_LogLevel, "Status%s_Return {", unicode ? "W" : "A");
1337
0
  WLog_Print(log, g_LogLevel, "  ReturnCode: %s (0x%08" PRIX32 ")",
1338
0
             SCardGetErrorString(ret->ReturnCode),
1339
0
             WINPR_CXX_COMPAT_CAST(UINT32, ret->ReturnCode));
1340
0
  WLog_Print(log, g_LogLevel, "  dwState: %s (0x%08" PRIX32 ") dwProtocol: %s (0x%08" PRIX32 ")",
1341
0
             SCardGetCardStateString(ret->dwState), ret->dwState,
1342
0
             SCardGetProtocolString(ret->dwProtocol), ret->dwProtocol);
1343
1344
0
  WLog_Print(log, g_LogLevel, "  cBytes: %" PRIu32 " mszReaderNames: %s", ret->cBytes,
1345
0
             mszReaderNamesA);
1346
1347
0
  WLog_Print(log, g_LogLevel, "  cbAtrLen: %" PRIu32 " pbAtr: %s", ret->cbAtrLen,
1348
0
             smartcard_array_dump(ret->pbAtr, ret->cbAtrLen, buffer, sizeof(buffer)));
1349
0
  WLog_Print(log, g_LogLevel, "}");
1350
0
  free(mszReaderNamesA);
1351
0
}
1352
1353
static void smartcard_trace_state_return(wLog* log, const State_Return* ret)
1354
0
{
1355
0
  WINPR_ASSERT(ret);
1356
0
  char buffer[1024] = { 0 };
1357
0
  char* state = NULL;
1358
1359
0
  if (!WLog_IsLevelActive(log, g_LogLevel))
1360
0
    return;
1361
1362
0
  state = SCardGetReaderStateString(ret->dwState);
1363
0
  WLog_Print(log, g_LogLevel, "Reconnect_Return {");
1364
0
  WLog_Print(log, g_LogLevel, "  ReturnCode: %s (0x%08" PRIX32 ")",
1365
0
             SCardGetErrorString(ret->ReturnCode),
1366
0
             WINPR_CXX_COMPAT_CAST(UINT32, ret->ReturnCode));
1367
0
  WLog_Print(log, g_LogLevel, "  dwState:    %s (0x%08" PRIX32 ")", state, ret->dwState);
1368
0
  WLog_Print(log, g_LogLevel, "  dwProtocol: %s (0x%08" PRIX32 ")",
1369
0
             SCardGetProtocolString(ret->dwProtocol), ret->dwProtocol);
1370
0
  WLog_Print(log, g_LogLevel, "  cbAtrLen:      (0x%08" PRIX32 ")", ret->cbAtrLen);
1371
0
  WLog_Print(log, g_LogLevel, "  rgAtr:      %s",
1372
0
             smartcard_array_dump(ret->rgAtr, sizeof(ret->rgAtr), buffer, sizeof(buffer)));
1373
0
  WLog_Print(log, g_LogLevel, "}");
1374
0
  free(state);
1375
0
}
1376
1377
static void smartcard_trace_reconnect_return(wLog* log, const Reconnect_Return* ret)
1378
0
{
1379
0
  WINPR_ASSERT(ret);
1380
1381
0
  if (!WLog_IsLevelActive(log, g_LogLevel))
1382
0
    return;
1383
1384
0
  WLog_Print(log, g_LogLevel, "Reconnect_Return {");
1385
0
  WLog_Print(log, g_LogLevel, "  ReturnCode: %s (0x%08" PRIX32 ")",
1386
0
             SCardGetErrorString(ret->ReturnCode),
1387
0
             WINPR_CXX_COMPAT_CAST(UINT32, ret->ReturnCode));
1388
0
  WLog_Print(log, g_LogLevel, "  dwActiveProtocol: %s (0x%08" PRIX32 ")",
1389
0
             SCardGetProtocolString(ret->dwActiveProtocol), ret->dwActiveProtocol);
1390
0
  WLog_Print(log, g_LogLevel, "}");
1391
0
}
1392
1393
static void smartcard_trace_connect_a_call(wLog* log, const ConnectA_Call* call)
1394
0
{
1395
0
  WINPR_ASSERT(call);
1396
1397
0
  if (!WLog_IsLevelActive(log, g_LogLevel))
1398
0
    return;
1399
1400
0
  WLog_Print(log, g_LogLevel, "ConnectA_Call {");
1401
0
  smartcard_log_context(log, &call->Common.handles.hContext);
1402
1403
0
  WLog_Print(log, g_LogLevel,
1404
0
             "szReader: %s dwShareMode: %s (0x%08" PRIX32
1405
0
             ") dwPreferredProtocols: %s (0x%08" PRIX32 ")",
1406
0
             call->szReader, SCardGetShareModeString(call->Common.dwShareMode),
1407
0
             call->Common.dwShareMode, SCardGetProtocolString(call->Common.dwPreferredProtocols),
1408
0
             call->Common.dwPreferredProtocols);
1409
0
  WLog_Print(log, g_LogLevel, "}");
1410
0
}
1411
1412
static void smartcard_trace_connect_w_call(wLog* log, const ConnectW_Call* call)
1413
0
{
1414
0
  WINPR_ASSERT(call);
1415
0
  char szReaderA[1024] = { 0 };
1416
1417
0
  if (!WLog_IsLevelActive(log, g_LogLevel))
1418
0
    return;
1419
1420
0
  if (call->szReader)
1421
0
    (void)ConvertWCharToUtf8(call->szReader, szReaderA, ARRAYSIZE(szReaderA));
1422
0
  WLog_Print(log, g_LogLevel, "ConnectW_Call {");
1423
0
  smartcard_log_context(log, &call->Common.handles.hContext);
1424
1425
0
  WLog_Print(log, g_LogLevel,
1426
0
             "szReader: %s dwShareMode: %s (0x%08" PRIX32
1427
0
             ") dwPreferredProtocols: %s (0x%08" PRIX32 ")",
1428
0
             szReaderA, SCardGetShareModeString(call->Common.dwShareMode),
1429
0
             call->Common.dwShareMode, SCardGetProtocolString(call->Common.dwPreferredProtocols),
1430
0
             call->Common.dwPreferredProtocols);
1431
0
  WLog_Print(log, g_LogLevel, "}");
1432
0
}
1433
1434
static void smartcard_trace_hcard_and_disposition_call(wLog* log,
1435
                                                       const HCardAndDisposition_Call* call,
1436
                                                       const char* name)
1437
0
{
1438
0
  WINPR_ASSERT(call);
1439
1440
0
  if (!WLog_IsLevelActive(log, g_LogLevel))
1441
0
    return;
1442
1443
0
  WLog_Print(log, g_LogLevel, "%s_Call {", name);
1444
0
  smartcard_log_context(log, &call->handles.hContext);
1445
0
  smartcard_log_redir_handle(log, &call->handles.hCard);
1446
1447
0
  WLog_Print(log, g_LogLevel, "dwDisposition: %s (0x%08" PRIX32 ")",
1448
0
             SCardGetDispositionString(call->dwDisposition), call->dwDisposition);
1449
0
  WLog_Print(log, g_LogLevel, "}");
1450
0
}
1451
1452
static void smartcard_trace_establish_context_call(wLog* log, const EstablishContext_Call* call)
1453
0
{
1454
0
  WINPR_ASSERT(call);
1455
1456
0
  if (!WLog_IsLevelActive(log, g_LogLevel))
1457
0
    return;
1458
1459
0
  WLog_Print(log, g_LogLevel, "EstablishContext_Call {");
1460
0
  WLog_Print(log, g_LogLevel, "dwScope: %s (0x%08" PRIX32 ")", SCardGetScopeString(call->dwScope),
1461
0
             call->dwScope);
1462
0
  WLog_Print(log, g_LogLevel, "}");
1463
0
}
1464
1465
static void smartcard_trace_establish_context_return(wLog* log, const EstablishContext_Return* ret)
1466
0
{
1467
0
  WINPR_ASSERT(ret);
1468
1469
0
  if (!WLog_IsLevelActive(log, g_LogLevel))
1470
0
    return;
1471
1472
0
  WLog_Print(log, g_LogLevel, "EstablishContext_Return {");
1473
0
  WLog_Print(log, g_LogLevel, "  ReturnCode: %s (0x%08" PRIX32 ")",
1474
0
             SCardGetErrorString(ret->ReturnCode),
1475
0
             WINPR_CXX_COMPAT_CAST(UINT32, ret->ReturnCode));
1476
0
  smartcard_log_context(log, &ret->hContext);
1477
1478
0
  WLog_Print(log, g_LogLevel, "}");
1479
0
}
1480
1481
void smartcard_trace_long_return(const Long_Return* ret, const char* name)
1482
0
{
1483
0
  wLog* log = scard_log();
1484
0
  smartcard_trace_long_return_int(log, ret, name);
1485
0
}
1486
1487
void smartcard_trace_long_return_int(wLog* log, const Long_Return* ret, const char* name)
1488
0
{
1489
0
  WINPR_ASSERT(ret);
1490
1491
0
  if (!WLog_IsLevelActive(log, g_LogLevel))
1492
0
    return;
1493
1494
0
  WLog_Print(log, g_LogLevel, "%s_Return {", name);
1495
0
  WLog_Print(log, g_LogLevel, "  ReturnCode: %s (0x%08" PRIX32 ")",
1496
0
             SCardGetErrorString(ret->ReturnCode),
1497
0
             WINPR_CXX_COMPAT_CAST(UINT32, ret->ReturnCode));
1498
0
  WLog_Print(log, g_LogLevel, "}");
1499
0
}
1500
1501
static void smartcard_trace_connect_return(wLog* log, const Connect_Return* ret)
1502
0
{
1503
0
  WINPR_ASSERT(ret);
1504
1505
0
  if (!WLog_IsLevelActive(log, g_LogLevel))
1506
0
    return;
1507
1508
0
  WLog_Print(log, g_LogLevel, "Connect_Return {");
1509
0
  WLog_Print(log, g_LogLevel, "  ReturnCode: %s (0x%08" PRIX32 ")",
1510
0
             SCardGetErrorString(ret->ReturnCode),
1511
0
             WINPR_CXX_COMPAT_CAST(UINT32, ret->ReturnCode));
1512
0
  smartcard_log_context(log, &ret->hContext);
1513
0
  smartcard_log_redir_handle(log, &ret->hCard);
1514
1515
0
  WLog_Print(log, g_LogLevel, "  dwActiveProtocol: %s (0x%08" PRIX32 ")",
1516
0
             SCardGetProtocolString(ret->dwActiveProtocol), ret->dwActiveProtocol);
1517
0
  WLog_Print(log, g_LogLevel, "}");
1518
0
}
1519
1520
static void smartcard_trace_reconnect_call(wLog* log, const Reconnect_Call* call)
1521
0
{
1522
0
  WINPR_ASSERT(call);
1523
1524
0
  if (!WLog_IsLevelActive(log, g_LogLevel))
1525
0
    return;
1526
1527
0
  WLog_Print(log, g_LogLevel, "Reconnect_Call {");
1528
0
  smartcard_log_context(log, &call->handles.hContext);
1529
0
  smartcard_log_redir_handle(log, &call->handles.hCard);
1530
1531
0
  WLog_Print(log, g_LogLevel,
1532
0
             "dwShareMode: %s (0x%08" PRIX32 ") dwPreferredProtocols: %s (0x%08" PRIX32
1533
0
             ") dwInitialization: %s (0x%08" PRIX32 ")",
1534
0
             SCardGetShareModeString(call->dwShareMode), call->dwShareMode,
1535
0
             SCardGetProtocolString(call->dwPreferredProtocols), call->dwPreferredProtocols,
1536
0
             SCardGetDispositionString(call->dwInitialization), call->dwInitialization);
1537
0
  WLog_Print(log, g_LogLevel, "}");
1538
0
}
1539
1540
static void smartcard_trace_device_type_id_return(wLog* log, const GetDeviceTypeId_Return* ret)
1541
0
{
1542
0
  WINPR_ASSERT(ret);
1543
1544
0
  if (!WLog_IsLevelActive(log, g_LogLevel))
1545
0
    return;
1546
1547
0
  WLog_Print(log, g_LogLevel, "GetDeviceTypeId_Return {");
1548
0
  WLog_Print(log, g_LogLevel, "  ReturnCode: %s (0x%08" PRIX32 ")",
1549
0
             SCardGetErrorString(ret->ReturnCode),
1550
0
             WINPR_CXX_COMPAT_CAST(UINT32, ret->ReturnCode));
1551
0
  WLog_Print(log, g_LogLevel, "  dwDeviceId=%08" PRIx32, ret->dwDeviceId);
1552
1553
0
  WLog_Print(log, g_LogLevel, "}");
1554
0
}
1555
1556
static LONG smartcard_unpack_common_context_and_string_a(wLog* log, wStream* s,
1557
                                                         REDIR_SCARDCONTEXT* phContext,
1558
                                                         CHAR** pszReaderName)
1559
0
{
1560
0
  UINT32 index = 0;
1561
0
  UINT32 pbContextNdrPtr = 0;
1562
0
  LONG status = smartcard_unpack_redir_scard_context(log, s, phContext, &index, &pbContextNdrPtr);
1563
0
  if (status != SCARD_S_SUCCESS)
1564
0
    return status;
1565
1566
0
  if (!smartcard_ndr_pointer_read(log, s, &index, NULL))
1567
0
    return ERROR_INVALID_DATA;
1568
1569
0
  status = smartcard_unpack_redir_scard_context_ref(log, s, pbContextNdrPtr, phContext);
1570
0
  if (status != SCARD_S_SUCCESS)
1571
0
    return status;
1572
1573
0
  status = smartcard_ndr_read_a(log, s, pszReaderName, NDR_PTR_FULL);
1574
0
  if (status != SCARD_S_SUCCESS)
1575
0
    return status;
1576
1577
0
  smartcard_trace_context_and_string_call_a(log, __func__, phContext, *pszReaderName);
1578
0
  return SCARD_S_SUCCESS;
1579
0
}
1580
1581
static LONG smartcard_unpack_common_context_and_string_w(wLog* log, wStream* s,
1582
                                                         REDIR_SCARDCONTEXT* phContext,
1583
                                                         WCHAR** pszReaderName)
1584
0
{
1585
0
  UINT32 index = 0;
1586
0
  UINT32 pbContextNdrPtr = 0;
1587
1588
0
  LONG status = smartcard_unpack_redir_scard_context(log, s, phContext, &index, &pbContextNdrPtr);
1589
0
  if (status != SCARD_S_SUCCESS)
1590
0
    return status;
1591
1592
0
  if (!smartcard_ndr_pointer_read(log, s, &index, NULL))
1593
0
    return ERROR_INVALID_DATA;
1594
1595
0
  status = smartcard_unpack_redir_scard_context_ref(log, s, pbContextNdrPtr, phContext);
1596
0
  if (status != SCARD_S_SUCCESS)
1597
0
    return status;
1598
1599
0
  status = smartcard_ndr_read_w(log, s, pszReaderName, NDR_PTR_FULL);
1600
0
  if (status != SCARD_S_SUCCESS)
1601
0
    return status;
1602
1603
0
  smartcard_trace_context_and_string_call_w(log, __func__, phContext, *pszReaderName);
1604
0
  return SCARD_S_SUCCESS;
1605
0
}
1606
1607
LONG smartcard_unpack_common_type_header(wStream* s)
1608
0
{
1609
0
  wLog* log = scard_log();
1610
0
  UINT8 version = 0;
1611
0
  UINT32 filler = 0;
1612
0
  UINT8 endianness = 0;
1613
0
  UINT16 commonHeaderLength = 0;
1614
1615
0
  if (!Stream_CheckAndLogRequiredLengthWLog(log, s, 8))
1616
0
    return STATUS_BUFFER_TOO_SMALL;
1617
1618
  /* Process CommonTypeHeader */
1619
0
  Stream_Read_UINT8(s, version);             /* Version (1 byte) */
1620
0
  Stream_Read_UINT8(s, endianness);          /* Endianness (1 byte) */
1621
0
  Stream_Read_UINT16(s, commonHeaderLength); /* CommonHeaderLength (2 bytes) */
1622
0
  Stream_Read_UINT32(s, filler);             /* Filler (4 bytes), should be 0xCCCCCCCC */
1623
1624
0
  if (version != 1)
1625
0
  {
1626
0
    WLog_Print(log, WLOG_WARN, "Unsupported CommonTypeHeader Version %" PRIu8 "", version);
1627
0
    return STATUS_INVALID_PARAMETER;
1628
0
  }
1629
1630
0
  if (endianness != 0x10)
1631
0
  {
1632
0
    WLog_Print(log, WLOG_WARN, "Unsupported CommonTypeHeader Endianness %" PRIu8 "",
1633
0
               endianness);
1634
0
    return STATUS_INVALID_PARAMETER;
1635
0
  }
1636
1637
0
  if (commonHeaderLength != 8)
1638
0
  {
1639
0
    WLog_Print(log, WLOG_WARN, "Unsupported CommonTypeHeader CommonHeaderLength %" PRIu16 "",
1640
0
               commonHeaderLength);
1641
0
    return STATUS_INVALID_PARAMETER;
1642
0
  }
1643
1644
0
  if (filler != 0xCCCCCCCC)
1645
0
  {
1646
0
    WLog_Print(log, WLOG_WARN, "Unexpected CommonTypeHeader Filler 0x%08" PRIX32 "", filler);
1647
0
    return STATUS_INVALID_PARAMETER;
1648
0
  }
1649
1650
0
  return SCARD_S_SUCCESS;
1651
0
}
1652
1653
void smartcard_pack_common_type_header(wStream* s)
1654
0
{
1655
0
  Stream_Write_UINT8(s, 1);           /* Version (1 byte) */
1656
0
  Stream_Write_UINT8(s, 0x10);        /* Endianness (1 byte) */
1657
0
  Stream_Write_UINT16(s, 8);          /* CommonHeaderLength (2 bytes) */
1658
0
  Stream_Write_UINT32(s, 0xCCCCCCCC); /* Filler (4 bytes), should be 0xCCCCCCCC */
1659
0
}
1660
1661
LONG smartcard_unpack_private_type_header(wStream* s)
1662
0
{
1663
0
  wLog* log = scard_log();
1664
0
  UINT32 filler = 0;
1665
0
  UINT32 objectBufferLength = 0;
1666
1667
0
  if (!Stream_CheckAndLogRequiredLengthWLog(log, s, 8))
1668
0
    return STATUS_BUFFER_TOO_SMALL;
1669
1670
0
  Stream_Read_UINT32(s, objectBufferLength); /* ObjectBufferLength (4 bytes) */
1671
0
  Stream_Read_UINT32(s, filler);             /* Filler (4 bytes), should be 0x00000000 */
1672
1673
0
  if (filler != 0x00000000)
1674
0
  {
1675
0
    WLog_Print(log, WLOG_WARN, "Unexpected PrivateTypeHeader Filler 0x%08" PRIX32 "", filler);
1676
0
    return STATUS_INVALID_PARAMETER;
1677
0
  }
1678
1679
0
  if (!Stream_CheckAndLogRequiredLengthWLog(log, s, objectBufferLength))
1680
0
    return STATUS_INVALID_PARAMETER;
1681
1682
0
  return SCARD_S_SUCCESS;
1683
0
}
1684
1685
void smartcard_pack_private_type_header(wStream* s, UINT32 objectBufferLength)
1686
0
{
1687
0
  Stream_Write_UINT32(s, objectBufferLength); /* ObjectBufferLength (4 bytes) */
1688
0
  Stream_Write_UINT32(s, 0x00000000);         /* Filler (4 bytes), should be 0x00000000 */
1689
0
}
1690
1691
LONG smartcard_unpack_read_size_align(wStream* s, size_t size, UINT32 alignment)
1692
0
{
1693
0
  size_t pad = 0;
1694
1695
0
  pad = size;
1696
0
  size = (size + alignment - 1) & ~(alignment - 1);
1697
0
  pad = size - pad;
1698
1699
0
  if (pad)
1700
0
    Stream_Seek(s, pad);
1701
1702
0
  return (LONG)pad;
1703
0
}
1704
1705
LONG smartcard_pack_write_size_align(wStream* s, size_t size, UINT32 alignment)
1706
0
{
1707
0
  size_t pad = 0;
1708
1709
0
  pad = size;
1710
0
  size = (size + alignment - 1) & ~(alignment - 1);
1711
0
  pad = size - pad;
1712
1713
0
  if (pad)
1714
0
  {
1715
0
    if (!Stream_EnsureRemainingCapacity(s, pad))
1716
0
    {
1717
0
      wLog* log = scard_log();
1718
0
      WLog_Print(log, WLOG_ERROR, "Stream_EnsureRemainingCapacity failed!");
1719
0
      return SCARD_F_INTERNAL_ERROR;
1720
0
    }
1721
1722
0
    Stream_Zero(s, pad);
1723
0
  }
1724
1725
0
  return SCARD_S_SUCCESS;
1726
0
}
1727
1728
SCARDCONTEXT smartcard_scard_context_native_from_redir(REDIR_SCARDCONTEXT* context)
1729
0
{
1730
0
  SCARDCONTEXT hContext = { 0 };
1731
1732
0
  WINPR_ASSERT(context);
1733
0
  if ((context->cbContext != sizeof(ULONG_PTR)) && (context->cbContext != 0))
1734
0
  {
1735
0
    wLog* log = scard_log();
1736
0
    WLog_Print(log, WLOG_WARN,
1737
0
               "REDIR_SCARDCONTEXT does not match native size: Actual: %" PRIu32
1738
0
               ", Expected: %" PRIuz "",
1739
0
               context->cbContext, sizeof(ULONG_PTR));
1740
0
    return 0;
1741
0
  }
1742
1743
0
  if (context->cbContext)
1744
0
    CopyMemory(&hContext, &(context->pbContext), context->cbContext);
1745
1746
0
  return hContext;
1747
0
}
1748
1749
void smartcard_scard_context_native_to_redir(REDIR_SCARDCONTEXT* context, SCARDCONTEXT hContext)
1750
0
{
1751
0
  WINPR_ASSERT(context);
1752
0
  ZeroMemory(context, sizeof(REDIR_SCARDCONTEXT));
1753
0
  context->cbContext = sizeof(ULONG_PTR);
1754
0
  CopyMemory(&(context->pbContext), &hContext, context->cbContext);
1755
0
}
1756
1757
SCARDHANDLE smartcard_scard_handle_native_from_redir(REDIR_SCARDHANDLE* handle)
1758
0
{
1759
0
  SCARDHANDLE hCard = 0;
1760
1761
0
  WINPR_ASSERT(handle);
1762
0
  if (handle->cbHandle == 0)
1763
0
    return hCard;
1764
1765
0
  if (handle->cbHandle != sizeof(ULONG_PTR))
1766
0
  {
1767
0
    wLog* log = scard_log();
1768
0
    WLog_Print(log, WLOG_WARN,
1769
0
               "REDIR_SCARDHANDLE does not match native size: Actual: %" PRIu32
1770
0
               ", Expected: %" PRIuz "",
1771
0
               handle->cbHandle, sizeof(ULONG_PTR));
1772
0
    return 0;
1773
0
  }
1774
1775
0
  if (handle->cbHandle)
1776
0
    CopyMemory(&hCard, &(handle->pbHandle), handle->cbHandle);
1777
1778
0
  return hCard;
1779
0
}
1780
1781
void smartcard_scard_handle_native_to_redir(REDIR_SCARDHANDLE* handle, SCARDHANDLE hCard)
1782
0
{
1783
0
  WINPR_ASSERT(handle);
1784
0
  ZeroMemory(handle, sizeof(REDIR_SCARDHANDLE));
1785
0
  handle->cbHandle = sizeof(ULONG_PTR);
1786
0
  CopyMemory(&(handle->pbHandle), &hCard, handle->cbHandle);
1787
0
}
1788
1789
#define smartcard_context_supported(log, size) \
1790
0
  smartcard_context_supported_((log), (size), __FILE__, __func__, __LINE__)
1791
static LONG smartcard_context_supported_(wLog* log, uint32_t size, const char* file,
1792
                                         const char* fkt, size_t line)
1793
0
{
1794
0
  switch (size)
1795
0
  {
1796
0
    case 0:
1797
0
    case 4:
1798
0
    case 8:
1799
0
      return SCARD_S_SUCCESS;
1800
0
    default:
1801
0
    {
1802
0
      const uint32_t level = WLOG_WARN;
1803
0
      if (WLog_IsLevelActive(log, level))
1804
0
      {
1805
0
        WLog_PrintTextMessage(log, level, line, file, fkt,
1806
0
                              "REDIR_SCARDCONTEXT length is not 0, 4 or 8: %" PRIu32 "",
1807
0
                              size);
1808
0
      }
1809
0
      return STATUS_INVALID_PARAMETER;
1810
0
    }
1811
0
  }
1812
0
}
1813
1814
LONG smartcard_unpack_redir_scard_context_(wLog* log, wStream* s, REDIR_SCARDCONTEXT* context,
1815
                                           UINT32* index, UINT32* ppbContextNdrPtr,
1816
                                           const char* file, const char* function, size_t line)
1817
0
{
1818
0
  UINT32 pbContextNdrPtr = 0;
1819
1820
0
  WINPR_UNUSED(file);
1821
0
  WINPR_ASSERT(context);
1822
1823
0
  ZeroMemory(context, sizeof(REDIR_SCARDCONTEXT));
1824
1825
0
  if (!Stream_CheckAndLogRequiredLengthWLogEx(log, WLOG_WARN, s, 4, 1, "%s(%s:%" PRIuz ")", file,
1826
0
                                              function, line))
1827
0
    return STATUS_BUFFER_TOO_SMALL;
1828
1829
0
  const LONG status = smartcard_context_supported_(log, context->cbContext, file, function, line);
1830
0
  if (status != SCARD_S_SUCCESS)
1831
0
    return status;
1832
1833
0
  Stream_Read_UINT32(s, context->cbContext); /* cbContext (4 bytes) */
1834
1835
0
  if (!smartcard_ndr_pointer_read_(log, s, index, &pbContextNdrPtr, file, function, line))
1836
0
    return ERROR_INVALID_DATA;
1837
1838
0
  if (((context->cbContext == 0) && pbContextNdrPtr) ||
1839
0
      ((context->cbContext != 0) && !pbContextNdrPtr))
1840
0
  {
1841
0
    WLog_Print(log, WLOG_WARN,
1842
0
               "REDIR_SCARDCONTEXT cbContext (%" PRIu32 ") pbContextNdrPtr (%" PRIu32
1843
0
               ") inconsistency",
1844
0
               context->cbContext, pbContextNdrPtr);
1845
0
    return STATUS_INVALID_PARAMETER;
1846
0
  }
1847
1848
0
  if (!Stream_CheckAndLogRequiredLengthWLogEx(log, WLOG_WARN, s, context->cbContext, 1,
1849
0
                                              "%s(%s:%" PRIuz ")", file, function, line))
1850
0
    return STATUS_INVALID_PARAMETER;
1851
1852
0
  *ppbContextNdrPtr = pbContextNdrPtr;
1853
0
  return SCARD_S_SUCCESS;
1854
0
}
1855
1856
LONG smartcard_pack_redir_scard_context(WINPR_ATTR_UNUSED wLog* log, wStream* s,
1857
                                        const REDIR_SCARDCONTEXT* context, DWORD* index)
1858
0
{
1859
0
  const UINT32 pbContextNdrPtr = 0x00020000 + *index * 4;
1860
1861
0
  WINPR_ASSERT(context);
1862
0
  if (context->cbContext != 0)
1863
0
  {
1864
0
    Stream_Write_UINT32(s, context->cbContext); /* cbContext (4 bytes) */
1865
0
    Stream_Write_UINT32(s, pbContextNdrPtr);    /* pbContextNdrPtr (4 bytes) */
1866
0
    *index = *index + 1;
1867
0
  }
1868
0
  else
1869
0
    Stream_Zero(s, 8);
1870
1871
0
  return SCARD_S_SUCCESS;
1872
0
}
1873
1874
LONG smartcard_unpack_redir_scard_context_ref(wLog* log, wStream* s,
1875
                                              WINPR_ATTR_UNUSED UINT32 pbContextNdrPtr,
1876
                                              REDIR_SCARDCONTEXT* context)
1877
0
{
1878
0
  UINT32 length = 0;
1879
1880
0
  WINPR_ASSERT(context);
1881
0
  if (context->cbContext == 0)
1882
0
    return SCARD_S_SUCCESS;
1883
1884
0
  if (!Stream_CheckAndLogRequiredLengthWLog(log, s, 4))
1885
0
    return STATUS_BUFFER_TOO_SMALL;
1886
1887
0
  Stream_Read_UINT32(s, length); /* Length (4 bytes) */
1888
1889
0
  if (length != context->cbContext)
1890
0
  {
1891
0
    WLog_Print(log, WLOG_WARN,
1892
0
               "REDIR_SCARDCONTEXT length (%" PRIu32 ") cbContext (%" PRIu32 ") mismatch",
1893
0
               length, context->cbContext);
1894
0
    return STATUS_INVALID_PARAMETER;
1895
0
  }
1896
1897
0
  const LONG status = smartcard_context_supported(log, context->cbContext);
1898
0
  if (status != SCARD_S_SUCCESS)
1899
0
    return status;
1900
1901
0
  if (!Stream_CheckAndLogRequiredLengthWLog(log, s, context->cbContext))
1902
0
    return STATUS_BUFFER_TOO_SMALL;
1903
1904
0
  if (context->cbContext)
1905
0
    Stream_Read(s, &(context->pbContext), context->cbContext);
1906
0
  else
1907
0
    ZeroMemory(&(context->pbContext), sizeof(context->pbContext));
1908
1909
0
  return SCARD_S_SUCCESS;
1910
0
}
1911
1912
LONG smartcard_pack_redir_scard_context_ref(WINPR_ATTR_UNUSED wLog* log, wStream* s,
1913
                                            const REDIR_SCARDCONTEXT* context)
1914
0
{
1915
0
  WINPR_ASSERT(context);
1916
0
  Stream_Write_UINT32(s, context->cbContext); /* Length (4 bytes) */
1917
1918
0
  if (context->cbContext)
1919
0
  {
1920
0
    Stream_Write(s, &(context->pbContext), context->cbContext);
1921
0
  }
1922
1923
0
  return SCARD_S_SUCCESS;
1924
0
}
1925
1926
LONG smartcard_unpack_redir_scard_handle_(wLog* log, wStream* s, REDIR_SCARDHANDLE* handle,
1927
                                          UINT32* index, const char* file, const char* function,
1928
                                          size_t line)
1929
0
{
1930
0
  WINPR_ASSERT(handle);
1931
0
  ZeroMemory(handle, sizeof(REDIR_SCARDHANDLE));
1932
1933
0
  if (!Stream_CheckAndLogRequiredLengthWLog(log, s, 4))
1934
0
    return STATUS_BUFFER_TOO_SMALL;
1935
1936
0
  Stream_Read_UINT32(s, handle->cbHandle); /* Length (4 bytes) */
1937
1938
0
  if (!Stream_CheckAndLogRequiredLengthWLog(log, s, handle->cbHandle))
1939
0
    return STATUS_BUFFER_TOO_SMALL;
1940
1941
0
  if (!smartcard_ndr_pointer_read_(log, s, index, NULL, file, function, line))
1942
0
    return ERROR_INVALID_DATA;
1943
1944
0
  return SCARD_S_SUCCESS;
1945
0
}
1946
1947
LONG smartcard_pack_redir_scard_handle(WINPR_ATTR_UNUSED wLog* log, wStream* s,
1948
                                       const REDIR_SCARDHANDLE* handle, DWORD* index)
1949
0
{
1950
0
  const UINT32 pbContextNdrPtr = 0x00020000 + *index * 4;
1951
1952
0
  WINPR_ASSERT(handle);
1953
0
  if (handle->cbHandle != 0)
1954
0
  {
1955
0
    Stream_Write_UINT32(s, handle->cbHandle); /* cbContext (4 bytes) */
1956
0
    Stream_Write_UINT32(s, pbContextNdrPtr);  /* pbContextNdrPtr (4 bytes) */
1957
0
    *index = *index + 1;
1958
0
  }
1959
0
  else
1960
0
    Stream_Zero(s, 8);
1961
0
  return SCARD_S_SUCCESS;
1962
0
}
1963
1964
LONG smartcard_unpack_redir_scard_handle_ref(wLog* log, wStream* s, REDIR_SCARDHANDLE* handle)
1965
0
{
1966
0
  UINT32 length = 0;
1967
1968
0
  WINPR_ASSERT(handle);
1969
0
  if (!Stream_CheckAndLogRequiredLengthWLog(log, s, 4))
1970
0
    return STATUS_BUFFER_TOO_SMALL;
1971
1972
0
  Stream_Read_UINT32(s, length); /* Length (4 bytes) */
1973
1974
0
  if (length != handle->cbHandle)
1975
0
  {
1976
0
    WLog_Print(log, WLOG_WARN,
1977
0
               "REDIR_SCARDHANDLE length (%" PRIu32 ") cbHandle (%" PRIu32 ") mismatch", length,
1978
0
               handle->cbHandle);
1979
0
    return STATUS_INVALID_PARAMETER;
1980
0
  }
1981
1982
0
  if ((handle->cbHandle != 4) && (handle->cbHandle != 8))
1983
0
  {
1984
0
    WLog_Print(log, WLOG_WARN, "REDIR_SCARDHANDLE length is not 4 or 8: %" PRIu32 "",
1985
0
               handle->cbHandle);
1986
0
    return STATUS_INVALID_PARAMETER;
1987
0
  }
1988
1989
0
  if (!Stream_CheckAndLogRequiredLengthWLog(log, s, handle->cbHandle))
1990
0
    return STATUS_BUFFER_TOO_SMALL;
1991
1992
0
  if (handle->cbHandle)
1993
0
    Stream_Read(s, &(handle->pbHandle), handle->cbHandle);
1994
1995
0
  return SCARD_S_SUCCESS;
1996
0
}
1997
1998
LONG smartcard_pack_redir_scard_handle_ref(WINPR_ATTR_UNUSED wLog* log, wStream* s,
1999
                                           const REDIR_SCARDHANDLE* handle)
2000
0
{
2001
0
  WINPR_ASSERT(handle);
2002
0
  Stream_Write_UINT32(s, handle->cbHandle); /* Length (4 bytes) */
2003
2004
0
  if (handle->cbHandle)
2005
0
    Stream_Write(s, &(handle->pbHandle), handle->cbHandle);
2006
2007
0
  return SCARD_S_SUCCESS;
2008
0
}
2009
2010
LONG smartcard_unpack_establish_context_call(wStream* s, EstablishContext_Call* call)
2011
0
{
2012
0
  WINPR_ASSERT(call);
2013
0
  wLog* log = scard_log();
2014
2015
0
  if (!Stream_CheckAndLogRequiredLengthWLog(log, s, 4))
2016
0
    return STATUS_BUFFER_TOO_SMALL;
2017
2018
0
  Stream_Read_UINT32(s, call->dwScope); /* dwScope (4 bytes) */
2019
0
  smartcard_trace_establish_context_call(log, call);
2020
0
  return SCARD_S_SUCCESS;
2021
0
}
2022
2023
LONG smartcard_pack_establish_context_return(wStream* s, const EstablishContext_Return* ret)
2024
0
{
2025
0
  WINPR_ASSERT(ret);
2026
0
  wLog* log = scard_log();
2027
0
  LONG status = 0;
2028
0
  DWORD index = 0;
2029
2030
0
  smartcard_trace_establish_context_return(log, ret);
2031
0
  if (ret->ReturnCode != SCARD_S_SUCCESS)
2032
0
    return ret->ReturnCode;
2033
2034
0
  status = smartcard_pack_redir_scard_context(log, s, &(ret->hContext), &index);
2035
0
  if (status != SCARD_S_SUCCESS)
2036
0
    return status;
2037
2038
0
  return smartcard_pack_redir_scard_context_ref(log, s, &(ret->hContext));
2039
0
}
2040
2041
LONG smartcard_unpack_context_call(wStream* s, Context_Call* call, const char* name)
2042
0
{
2043
0
  UINT32 index = 0;
2044
0
  UINT32 pbContextNdrPtr = 0;
2045
0
  wLog* log = scard_log();
2046
2047
0
  WINPR_ASSERT(call);
2048
0
  LONG status = smartcard_unpack_redir_scard_context(log, s, &(call->handles.hContext), &index,
2049
0
                                                     &pbContextNdrPtr);
2050
0
  if (status != SCARD_S_SUCCESS)
2051
0
    return status;
2052
2053
0
  status = smartcard_unpack_redir_scard_context_ref(log, s, pbContextNdrPtr,
2054
0
                                                    &(call->handles.hContext));
2055
0
  if (status != SCARD_S_SUCCESS)
2056
0
    WLog_Print(log, WLOG_ERROR,
2057
0
               "smartcard_unpack_redir_scard_context_ref failed with error %" PRId32 "",
2058
0
               status);
2059
2060
0
  smartcard_trace_context_call(log, call, name);
2061
0
  return status;
2062
0
}
2063
2064
LONG smartcard_unpack_list_reader_groups_call(wStream* s, ListReaderGroups_Call* call, BOOL unicode)
2065
0
{
2066
0
  UINT32 index = 0;
2067
0
  UINT32 pbContextNdrPtr = 0;
2068
0
  wLog* log = scard_log();
2069
2070
0
  WINPR_ASSERT(call);
2071
0
  LONG status = smartcard_unpack_redir_scard_context(log, s, &(call->handles.hContext), &index,
2072
0
                                                     &pbContextNdrPtr);
2073
2074
0
  if (status != SCARD_S_SUCCESS)
2075
0
    return status;
2076
2077
0
  if (!Stream_CheckAndLogRequiredLengthWLog(log, s, 8))
2078
0
    return STATUS_BUFFER_TOO_SMALL;
2079
2080
0
  Stream_Read_INT32(s, call->fmszGroupsIsNULL); /* fmszGroupsIsNULL (4 bytes) */
2081
0
  Stream_Read_UINT32(s, call->cchGroups);       /* cchGroups (4 bytes) */
2082
0
  status = smartcard_unpack_redir_scard_context_ref(log, s, pbContextNdrPtr,
2083
0
                                                    &(call->handles.hContext));
2084
2085
0
  if (status != SCARD_S_SUCCESS)
2086
0
    return status;
2087
2088
0
  smartcard_trace_list_reader_groups_call(log, call, unicode);
2089
0
  return SCARD_S_SUCCESS;
2090
0
}
2091
2092
LONG smartcard_pack_list_reader_groups_return(wStream* s, const ListReaderGroups_Return* ret,
2093
                                              BOOL unicode)
2094
0
{
2095
0
  WINPR_ASSERT(ret);
2096
0
  wLog* log = scard_log();
2097
0
  LONG status = 0;
2098
0
  DWORD cBytes = ret->cBytes;
2099
0
  UINT32 index = 0;
2100
2101
0
  smartcard_trace_list_reader_groups_return(log, ret, unicode);
2102
0
  if (ret->ReturnCode != SCARD_S_SUCCESS)
2103
0
    cBytes = 0;
2104
0
  if (cBytes == SCARD_AUTOALLOCATE)
2105
0
    cBytes = 0;
2106
2107
0
  if (!Stream_EnsureRemainingCapacity(s, 4))
2108
0
    return SCARD_E_NO_MEMORY;
2109
2110
0
  Stream_Write_UINT32(s, cBytes); /* cBytes (4 bytes) */
2111
0
  if (!smartcard_ndr_pointer_write(s, &index, cBytes))
2112
0
    return SCARD_E_NO_MEMORY;
2113
2114
0
  status = smartcard_ndr_write(s, ret->msz, cBytes, 1, NDR_PTR_SIMPLE);
2115
0
  if (status != SCARD_S_SUCCESS)
2116
0
    return status;
2117
0
  return ret->ReturnCode;
2118
0
}
2119
2120
LONG smartcard_unpack_list_readers_call(wStream* s, ListReaders_Call* call, BOOL unicode)
2121
0
{
2122
0
  UINT32 index = 0;
2123
0
  UINT32 mszGroupsNdrPtr = 0;
2124
0
  UINT32 pbContextNdrPtr = 0;
2125
0
  wLog* log = scard_log();
2126
2127
0
  WINPR_ASSERT(call);
2128
0
  call->mszGroups = NULL;
2129
2130
0
  LONG status = smartcard_unpack_redir_scard_context(log, s, &(call->handles.hContext), &index,
2131
0
                                                     &pbContextNdrPtr);
2132
0
  if (status != SCARD_S_SUCCESS)
2133
0
    return status;
2134
2135
0
  if (!Stream_CheckAndLogRequiredLengthWLog(log, s, 4))
2136
0
    return STATUS_BUFFER_TOO_SMALL;
2137
2138
0
  Stream_Read_UINT32(s, call->cBytes); /* cBytes (4 bytes) */
2139
0
  if (!smartcard_ndr_pointer_read(log, s, &index, &mszGroupsNdrPtr))
2140
0
    return ERROR_INVALID_DATA;
2141
2142
0
  if (!Stream_CheckAndLogRequiredLengthWLog(log, s, 8))
2143
0
    return STATUS_BUFFER_TOO_SMALL;
2144
0
  Stream_Read_INT32(s, call->fmszReadersIsNULL); /* fmszReadersIsNULL (4 bytes) */
2145
0
  Stream_Read_UINT32(s, call->cchReaders);       /* cchReaders (4 bytes) */
2146
2147
0
  status = smartcard_unpack_redir_scard_context_ref(log, s, pbContextNdrPtr,
2148
0
                                                    &(call->handles.hContext));
2149
0
  if (status != SCARD_S_SUCCESS)
2150
0
    return status;
2151
2152
0
  if (mszGroupsNdrPtr)
2153
0
  {
2154
0
    status = smartcard_ndr_read(log, s, &call->mszGroups, call->cBytes, 1, NDR_PTR_SIMPLE);
2155
0
    if (status != SCARD_S_SUCCESS)
2156
0
      return status;
2157
0
  }
2158
2159
0
  smartcard_trace_list_readers_call(log, call, unicode);
2160
0
  return SCARD_S_SUCCESS;
2161
0
}
2162
2163
LONG smartcard_pack_list_readers_return(wStream* s, const ListReaders_Return* ret, BOOL unicode)
2164
0
{
2165
0
  WINPR_ASSERT(ret);
2166
0
  wLog* log = scard_log();
2167
0
  LONG status = 0;
2168
0
  UINT32 index = 0;
2169
0
  UINT32 size = ret->cBytes;
2170
2171
0
  smartcard_trace_list_readers_return(log, ret, unicode);
2172
0
  if (ret->ReturnCode != SCARD_S_SUCCESS)
2173
0
    size = 0;
2174
2175
0
  if (!Stream_EnsureRemainingCapacity(s, 4))
2176
0
  {
2177
0
    WLog_Print(log, WLOG_ERROR, "Stream_EnsureRemainingCapacity failed!");
2178
0
    return SCARD_F_INTERNAL_ERROR;
2179
0
  }
2180
2181
0
  Stream_Write_UINT32(s, size); /* cBytes (4 bytes) */
2182
0
  if (!smartcard_ndr_pointer_write(s, &index, size))
2183
0
    return SCARD_E_NO_MEMORY;
2184
2185
0
  status = smartcard_ndr_write(s, ret->msz, size, 1, NDR_PTR_SIMPLE);
2186
0
  if (status != SCARD_S_SUCCESS)
2187
0
    return status;
2188
0
  return ret->ReturnCode;
2189
0
}
2190
2191
static LONG smartcard_unpack_connect_common(wLog* log, wStream* s, Connect_Common_Call* common,
2192
                                            UINT32* index, UINT32* ppbContextNdrPtr)
2193
0
{
2194
0
  WINPR_ASSERT(common);
2195
0
  LONG status = smartcard_unpack_redir_scard_context(log, s, &(common->handles.hContext), index,
2196
0
                                                     ppbContextNdrPtr);
2197
0
  if (status != SCARD_S_SUCCESS)
2198
0
    return status;
2199
2200
0
  if (!Stream_CheckAndLogRequiredLengthWLog(log, s, 8))
2201
0
    return STATUS_BUFFER_TOO_SMALL;
2202
2203
0
  Stream_Read_UINT32(s, common->dwShareMode);          /* dwShareMode (4 bytes) */
2204
0
  Stream_Read_UINT32(s, common->dwPreferredProtocols); /* dwPreferredProtocols (4 bytes) */
2205
0
  return SCARD_S_SUCCESS;
2206
0
}
2207
2208
LONG smartcard_unpack_connect_a_call(wStream* s, ConnectA_Call* call)
2209
0
{
2210
0
  LONG status = 0;
2211
0
  UINT32 index = 0;
2212
0
  UINT32 pbContextNdrPtr = 0;
2213
2214
0
  WINPR_ASSERT(call);
2215
0
  wLog* log = scard_log();
2216
2217
0
  call->szReader = NULL;
2218
2219
0
  if (!smartcard_ndr_pointer_read(log, s, &index, NULL))
2220
0
    return ERROR_INVALID_DATA;
2221
2222
0
  status = smartcard_unpack_connect_common(log, s, &(call->Common), &index, &pbContextNdrPtr);
2223
0
  if (status != SCARD_S_SUCCESS)
2224
0
  {
2225
0
    WLog_Print(log, WLOG_ERROR, "smartcard_unpack_connect_common failed with error %" PRId32 "",
2226
0
               status);
2227
0
    return status;
2228
0
  }
2229
2230
0
  status = smartcard_ndr_read_a(log, s, &call->szReader, NDR_PTR_FULL);
2231
0
  if (status != SCARD_S_SUCCESS)
2232
0
    return status;
2233
2234
0
  status = smartcard_unpack_redir_scard_context_ref(log, s, pbContextNdrPtr,
2235
0
                                                    &(call->Common.handles.hContext));
2236
0
  if (status != SCARD_S_SUCCESS)
2237
0
    WLog_Print(log, WLOG_ERROR,
2238
0
               "smartcard_unpack_redir_scard_context_ref failed with error %" PRId32 "",
2239
0
               status);
2240
2241
0
  smartcard_trace_connect_a_call(log, call);
2242
0
  return status;
2243
0
}
2244
2245
LONG smartcard_unpack_connect_w_call(wStream* s, ConnectW_Call* call)
2246
0
{
2247
0
  LONG status = 0;
2248
0
  UINT32 index = 0;
2249
0
  UINT32 pbContextNdrPtr = 0;
2250
2251
0
  WINPR_ASSERT(call);
2252
0
  wLog* log = scard_log();
2253
0
  call->szReader = NULL;
2254
2255
0
  if (!smartcard_ndr_pointer_read(log, s, &index, NULL))
2256
0
    return ERROR_INVALID_DATA;
2257
2258
0
  status = smartcard_unpack_connect_common(log, s, &(call->Common), &index, &pbContextNdrPtr);
2259
0
  if (status != SCARD_S_SUCCESS)
2260
0
  {
2261
0
    WLog_Print(log, WLOG_ERROR, "smartcard_unpack_connect_common failed with error %" PRId32 "",
2262
0
               status);
2263
0
    return status;
2264
0
  }
2265
2266
0
  status = smartcard_ndr_read_w(log, s, &call->szReader, NDR_PTR_FULL);
2267
0
  if (status != SCARD_S_SUCCESS)
2268
0
    return status;
2269
2270
0
  status = smartcard_unpack_redir_scard_context_ref(log, s, pbContextNdrPtr,
2271
0
                                                    &(call->Common.handles.hContext));
2272
0
  if (status != SCARD_S_SUCCESS)
2273
0
    WLog_Print(log, WLOG_ERROR,
2274
0
               "smartcard_unpack_redir_scard_context_ref failed with error %" PRId32 "",
2275
0
               status);
2276
2277
0
  smartcard_trace_connect_w_call(log, call);
2278
0
  return status;
2279
0
}
2280
2281
LONG smartcard_pack_connect_return(wStream* s, const Connect_Return* ret)
2282
0
{
2283
0
  LONG status = 0;
2284
0
  DWORD index = 0;
2285
2286
0
  WINPR_ASSERT(ret);
2287
0
  wLog* log = scard_log();
2288
0
  smartcard_trace_connect_return(log, ret);
2289
2290
0
  status = smartcard_pack_redir_scard_context(log, s, &ret->hContext, &index);
2291
0
  if (status != SCARD_S_SUCCESS)
2292
0
    return status;
2293
2294
0
  status = smartcard_pack_redir_scard_handle(log, s, &ret->hCard, &index);
2295
0
  if (status != SCARD_S_SUCCESS)
2296
0
    return status;
2297
2298
0
  if (!Stream_EnsureRemainingCapacity(s, 4))
2299
0
    return SCARD_E_NO_MEMORY;
2300
2301
0
  Stream_Write_UINT32(s, ret->dwActiveProtocol); /* dwActiveProtocol (4 bytes) */
2302
0
  status = smartcard_pack_redir_scard_context_ref(log, s, &ret->hContext);
2303
0
  if (status != SCARD_S_SUCCESS)
2304
0
    return status;
2305
0
  return smartcard_pack_redir_scard_handle_ref(log, s, &(ret->hCard));
2306
0
}
2307
2308
LONG smartcard_unpack_reconnect_call(wStream* s, Reconnect_Call* call)
2309
0
{
2310
0
  UINT32 index = 0;
2311
0
  UINT32 pbContextNdrPtr = 0;
2312
2313
0
  WINPR_ASSERT(call);
2314
0
  wLog* log = scard_log();
2315
0
  LONG status = smartcard_unpack_redir_scard_context(log, s, &(call->handles.hContext), &index,
2316
0
                                                     &pbContextNdrPtr);
2317
0
  if (status != SCARD_S_SUCCESS)
2318
0
    return status;
2319
2320
0
  status = smartcard_unpack_redir_scard_handle(log, s, &(call->handles.hCard), &index);
2321
0
  if (status != SCARD_S_SUCCESS)
2322
0
    return status;
2323
2324
0
  if (!Stream_CheckAndLogRequiredLengthWLog(log, s, 12))
2325
0
    return STATUS_BUFFER_TOO_SMALL;
2326
2327
0
  Stream_Read_UINT32(s, call->dwShareMode);          /* dwShareMode (4 bytes) */
2328
0
  Stream_Read_UINT32(s, call->dwPreferredProtocols); /* dwPreferredProtocols (4 bytes) */
2329
0
  Stream_Read_UINT32(s, call->dwInitialization);     /* dwInitialization (4 bytes) */
2330
2331
0
  status = smartcard_unpack_redir_scard_context_ref(log, s, pbContextNdrPtr,
2332
0
                                                    &(call->handles.hContext));
2333
0
  if (status != SCARD_S_SUCCESS)
2334
0
  {
2335
0
    WLog_Print(log, WLOG_ERROR,
2336
0
               "smartcard_unpack_redir_scard_context_ref failed with error %" PRId32 "",
2337
0
               status);
2338
0
    return status;
2339
0
  }
2340
2341
0
  status = smartcard_unpack_redir_scard_handle_ref(log, s, &(call->handles.hCard));
2342
0
  if (status != SCARD_S_SUCCESS)
2343
0
    WLog_Print(log, WLOG_ERROR,
2344
0
               "smartcard_unpack_redir_scard_handle_ref failed with error %" PRId32 "", status);
2345
2346
0
  smartcard_trace_reconnect_call(log, call);
2347
0
  return status;
2348
0
}
2349
2350
LONG smartcard_pack_reconnect_return(wStream* s, const Reconnect_Return* ret)
2351
0
{
2352
0
  WINPR_ASSERT(ret);
2353
0
  wLog* log = scard_log();
2354
0
  smartcard_trace_reconnect_return(log, ret);
2355
2356
0
  if (!Stream_EnsureRemainingCapacity(s, 4))
2357
0
    return SCARD_E_NO_MEMORY;
2358
0
  Stream_Write_UINT32(s, ret->dwActiveProtocol); /* dwActiveProtocol (4 bytes) */
2359
0
  return ret->ReturnCode;
2360
0
}
2361
2362
LONG smartcard_unpack_hcard_and_disposition_call(wStream* s, HCardAndDisposition_Call* call,
2363
                                                 const char* name)
2364
0
{
2365
0
  UINT32 index = 0;
2366
0
  UINT32 pbContextNdrPtr = 0;
2367
2368
0
  WINPR_ASSERT(call);
2369
0
  wLog* log = scard_log();
2370
2371
0
  LONG status = smartcard_unpack_redir_scard_context(log, s, &(call->handles.hContext), &index,
2372
0
                                                     &pbContextNdrPtr);
2373
0
  if (status != SCARD_S_SUCCESS)
2374
0
    return status;
2375
2376
0
  status = smartcard_unpack_redir_scard_handle(log, s, &(call->handles.hCard), &index);
2377
0
  if (status != SCARD_S_SUCCESS)
2378
0
    return status;
2379
2380
0
  if (!Stream_CheckAndLogRequiredLengthWLog(log, s, 4))
2381
0
    return STATUS_BUFFER_TOO_SMALL;
2382
2383
0
  Stream_Read_UINT32(s, call->dwDisposition); /* dwDisposition (4 bytes) */
2384
2385
0
  status = smartcard_unpack_redir_scard_context_ref(log, s, pbContextNdrPtr,
2386
0
                                                    &(call->handles.hContext));
2387
0
  if (status != SCARD_S_SUCCESS)
2388
0
    return status;
2389
2390
0
  status = smartcard_unpack_redir_scard_handle_ref(log, s, &(call->handles.hCard));
2391
0
  if (status != SCARD_S_SUCCESS)
2392
0
    return status;
2393
2394
0
  smartcard_trace_hcard_and_disposition_call(log, call, name);
2395
0
  return status;
2396
0
}
2397
2398
static void smartcard_trace_get_status_change_a_call(wLog* log, const GetStatusChangeA_Call* call)
2399
0
{
2400
0
  WINPR_ASSERT(call);
2401
2402
0
  if (!WLog_IsLevelActive(log, g_LogLevel))
2403
0
    return;
2404
2405
0
  WLog_Print(log, g_LogLevel, "GetStatusChangeA_Call {");
2406
0
  smartcard_log_context(log, &call->handles.hContext);
2407
2408
0
  WLog_Print(log, g_LogLevel, "dwTimeOut: 0x%08" PRIX32 " cReaders: %" PRIu32 "", call->dwTimeOut,
2409
0
             call->cReaders);
2410
2411
0
  dump_reader_states_a(log, call->rgReaderStates, call->cReaders);
2412
2413
0
  WLog_Print(log, g_LogLevel, "}");
2414
0
}
2415
2416
static LONG smartcard_unpack_reader_state_a(wLog* log, wStream* s, LPSCARD_READERSTATEA* ppcReaders,
2417
                                            UINT32 cReaders, UINT32* ptrIndex)
2418
0
{
2419
0
  LONG status = SCARD_E_NO_MEMORY;
2420
2421
0
  WINPR_ASSERT(ppcReaders || (cReaders == 0));
2422
0
  if (!Stream_CheckAndLogRequiredLengthWLog(log, s, 4))
2423
0
    return status;
2424
2425
0
  const UINT32 len = Stream_Get_UINT32(s);
2426
0
  if (len != cReaders)
2427
0
  {
2428
0
    WLog_Print(log, WLOG_ERROR, "Count mismatch when reading LPSCARD_READERSTATEA");
2429
0
    return status;
2430
0
  }
2431
2432
0
  LPSCARD_READERSTATEA rgReaderStates =
2433
0
      (LPSCARD_READERSTATEA)calloc(cReaders, sizeof(SCARD_READERSTATEA));
2434
0
  BOOL* states = calloc(cReaders, sizeof(BOOL));
2435
0
  if (!rgReaderStates || !states)
2436
0
    goto fail;
2437
0
  status = ERROR_INVALID_DATA;
2438
2439
0
  for (UINT32 index = 0; index < cReaders; index++)
2440
0
  {
2441
0
    UINT32 ptr = UINT32_MAX;
2442
0
    LPSCARD_READERSTATEA readerState = &rgReaderStates[index];
2443
2444
0
    if (!Stream_CheckAndLogRequiredLengthWLog(log, s, 52))
2445
0
      goto fail;
2446
2447
0
    if (!smartcard_ndr_pointer_read(log, s, ptrIndex, &ptr))
2448
0
    {
2449
0
      if (ptr != 0)
2450
0
        goto fail;
2451
0
    }
2452
    /* Ignore NULL length strings */
2453
0
    states[index] = ptr != 0;
2454
0
    Stream_Read_UINT32(s, readerState->dwCurrentState); /* dwCurrentState (4 bytes) */
2455
0
    Stream_Read_UINT32(s, readerState->dwEventState);   /* dwEventState (4 bytes) */
2456
0
    Stream_Read_UINT32(s, readerState->cbAtr);          /* cbAtr (4 bytes) */
2457
0
    Stream_Read(s, readerState->rgbAtr, 36);            /* rgbAtr [0..36] (36 bytes) */
2458
0
  }
2459
2460
0
  for (UINT32 index = 0; index < cReaders; index++)
2461
0
  {
2462
0
    LPSCARD_READERSTATEA readerState = &rgReaderStates[index];
2463
2464
    /* Ignore empty strings */
2465
0
    if (!states[index])
2466
0
      continue;
2467
0
    status = smartcard_ndr_read_a(log, s, &readerState->szReader, NDR_PTR_FULL);
2468
0
    if (status != SCARD_S_SUCCESS)
2469
0
      goto fail;
2470
0
  }
2471
2472
0
  *ppcReaders = rgReaderStates;
2473
0
  free(states);
2474
0
  return SCARD_S_SUCCESS;
2475
0
fail:
2476
0
  if (rgReaderStates)
2477
0
  {
2478
0
    for (UINT32 index = 0; index < cReaders; index++)
2479
0
    {
2480
0
      LPSCARD_READERSTATEA readerState = &rgReaderStates[index];
2481
0
      free(readerState->szReader);
2482
0
    }
2483
0
  }
2484
0
  free(rgReaderStates);
2485
0
  free(states);
2486
0
  return status;
2487
0
}
2488
2489
static LONG smartcard_unpack_reader_state_w(wLog* log, wStream* s, LPSCARD_READERSTATEW* ppcReaders,
2490
                                            UINT32 cReaders, UINT32* ptrIndex)
2491
0
{
2492
0
  LONG status = SCARD_E_NO_MEMORY;
2493
2494
0
  WINPR_ASSERT(ppcReaders || (cReaders == 0));
2495
0
  if (!Stream_CheckAndLogRequiredLengthWLog(log, s, 4))
2496
0
    return status;
2497
2498
0
  const UINT32 len = Stream_Get_UINT32(s);
2499
0
  if (len != cReaders)
2500
0
  {
2501
0
    WLog_Print(log, WLOG_ERROR, "Count mismatch when reading LPSCARD_READERSTATEW");
2502
0
    return status;
2503
0
  }
2504
2505
0
  LPSCARD_READERSTATEW rgReaderStates =
2506
0
      (LPSCARD_READERSTATEW)calloc(cReaders, sizeof(SCARD_READERSTATEW));
2507
0
  BOOL* states = calloc(cReaders, sizeof(BOOL));
2508
2509
0
  if (!rgReaderStates || !states)
2510
0
    goto fail;
2511
2512
0
  status = ERROR_INVALID_DATA;
2513
0
  for (UINT32 index = 0; index < cReaders; index++)
2514
0
  {
2515
0
    UINT32 ptr = UINT32_MAX;
2516
0
    LPSCARD_READERSTATEW readerState = &rgReaderStates[index];
2517
2518
0
    if (!Stream_CheckAndLogRequiredLengthWLog(log, s, 52))
2519
0
      goto fail;
2520
2521
0
    if (!smartcard_ndr_pointer_read(log, s, ptrIndex, &ptr))
2522
0
    {
2523
0
      if (ptr != 0)
2524
0
        goto fail;
2525
0
    }
2526
    /* Ignore NULL length strings */
2527
0
    states[index] = ptr != 0;
2528
0
    Stream_Read_UINT32(s, readerState->dwCurrentState); /* dwCurrentState (4 bytes) */
2529
0
    Stream_Read_UINT32(s, readerState->dwEventState);   /* dwEventState (4 bytes) */
2530
0
    Stream_Read_UINT32(s, readerState->cbAtr);          /* cbAtr (4 bytes) */
2531
0
    Stream_Read(s, readerState->rgbAtr, 36);            /* rgbAtr [0..36] (36 bytes) */
2532
0
  }
2533
2534
0
  for (UINT32 index = 0; index < cReaders; index++)
2535
0
  {
2536
0
    LPSCARD_READERSTATEW readerState = &rgReaderStates[index];
2537
2538
    /* Skip NULL pointers */
2539
0
    if (!states[index])
2540
0
      continue;
2541
2542
0
    status = smartcard_ndr_read_w(log, s, &readerState->szReader, NDR_PTR_FULL);
2543
0
    if (status != SCARD_S_SUCCESS)
2544
0
      goto fail;
2545
0
  }
2546
2547
0
  *ppcReaders = rgReaderStates;
2548
0
  free(states);
2549
0
  return SCARD_S_SUCCESS;
2550
0
fail:
2551
0
  if (rgReaderStates)
2552
0
  {
2553
0
    for (UINT32 index = 0; index < cReaders; index++)
2554
0
    {
2555
0
      LPSCARD_READERSTATEW readerState = &rgReaderStates[index];
2556
0
      free(readerState->szReader);
2557
0
    }
2558
0
  }
2559
0
  free(rgReaderStates);
2560
0
  free(states);
2561
0
  return status;
2562
0
}
2563
2564
/******************************************************************************/
2565
/************************************* End Trace Functions ********************/
2566
/******************************************************************************/
2567
2568
LONG smartcard_unpack_get_status_change_a_call(wStream* s, GetStatusChangeA_Call* call)
2569
0
{
2570
0
  UINT32 ndrPtr = 0;
2571
0
  UINT32 index = 0;
2572
0
  UINT32 pbContextNdrPtr = 0;
2573
2574
0
  WINPR_ASSERT(call);
2575
0
  wLog* log = scard_log();
2576
2577
0
  call->rgReaderStates = NULL;
2578
2579
0
  LONG status = smartcard_unpack_redir_scard_context(log, s, &(call->handles.hContext), &index,
2580
0
                                                     &pbContextNdrPtr);
2581
0
  if (status != SCARD_S_SUCCESS)
2582
0
    return status;
2583
2584
0
  if (!Stream_CheckAndLogRequiredLengthWLog(log, s, 8))
2585
0
    return STATUS_BUFFER_TOO_SMALL;
2586
2587
0
  Stream_Read_UINT32(s, call->dwTimeOut); /* dwTimeOut (4 bytes) */
2588
0
  Stream_Read_UINT32(s, call->cReaders);  /* cReaders (4 bytes) */
2589
0
  if (!smartcard_ndr_pointer_read(log, s, &index, &ndrPtr))
2590
0
    return ERROR_INVALID_DATA;
2591
2592
0
  status = smartcard_unpack_redir_scard_context_ref(log, s, pbContextNdrPtr,
2593
0
                                                    &(call->handles.hContext));
2594
0
  if (status != SCARD_S_SUCCESS)
2595
0
    return status;
2596
2597
0
  if (ndrPtr)
2598
0
  {
2599
0
    status =
2600
0
        smartcard_unpack_reader_state_a(log, s, &call->rgReaderStates, call->cReaders, &index);
2601
0
    if (status != SCARD_S_SUCCESS)
2602
0
      return status;
2603
0
  }
2604
0
  else
2605
0
  {
2606
0
    WLog_Print(log, WLOG_WARN, "ndrPtr=0x%08" PRIx32 ", can not read rgReaderStates", ndrPtr);
2607
0
    return SCARD_E_UNEXPECTED;
2608
0
  }
2609
2610
0
  smartcard_trace_get_status_change_a_call(log, call);
2611
0
  return SCARD_S_SUCCESS;
2612
0
}
2613
2614
LONG smartcard_unpack_get_status_change_w_call(wStream* s, GetStatusChangeW_Call* call)
2615
0
{
2616
0
  UINT32 ndrPtr = 0;
2617
0
  UINT32 index = 0;
2618
0
  UINT32 pbContextNdrPtr = 0;
2619
2620
0
  WINPR_ASSERT(call);
2621
0
  wLog* log = scard_log();
2622
0
  call->rgReaderStates = NULL;
2623
2624
0
  LONG status = smartcard_unpack_redir_scard_context(log, s, &(call->handles.hContext), &index,
2625
0
                                                     &pbContextNdrPtr);
2626
0
  if (status != SCARD_S_SUCCESS)
2627
0
    return status;
2628
2629
0
  if (!Stream_CheckAndLogRequiredLengthWLog(log, s, 8))
2630
0
    return STATUS_BUFFER_TOO_SMALL;
2631
2632
0
  Stream_Read_UINT32(s, call->dwTimeOut); /* dwTimeOut (4 bytes) */
2633
0
  Stream_Read_UINT32(s, call->cReaders);  /* cReaders (4 bytes) */
2634
0
  if (!smartcard_ndr_pointer_read(log, s, &index, &ndrPtr))
2635
0
    return ERROR_INVALID_DATA;
2636
2637
0
  status = smartcard_unpack_redir_scard_context_ref(log, s, pbContextNdrPtr,
2638
0
                                                    &(call->handles.hContext));
2639
0
  if (status != SCARD_S_SUCCESS)
2640
0
    return status;
2641
2642
0
  if (ndrPtr)
2643
0
  {
2644
0
    status =
2645
0
        smartcard_unpack_reader_state_w(log, s, &call->rgReaderStates, call->cReaders, &index);
2646
0
    if (status != SCARD_S_SUCCESS)
2647
0
      return status;
2648
0
  }
2649
0
  else
2650
0
  {
2651
0
    WLog_Print(log, WLOG_WARN, "ndrPtr=0x%08" PRIx32 ", can not read rgReaderStates", ndrPtr);
2652
0
    return SCARD_E_UNEXPECTED;
2653
0
  }
2654
2655
0
  smartcard_trace_get_status_change_w_call(log, call);
2656
0
  return SCARD_S_SUCCESS;
2657
0
}
2658
2659
LONG smartcard_pack_get_status_change_return(wStream* s, const GetStatusChange_Return* ret,
2660
                                             BOOL unicode)
2661
0
{
2662
0
  WINPR_ASSERT(ret);
2663
0
  wLog* log = scard_log();
2664
2665
0
  LONG status = 0;
2666
0
  DWORD cReaders = ret->cReaders;
2667
0
  UINT32 index = 0;
2668
2669
0
  smartcard_trace_get_status_change_return(log, ret, unicode);
2670
0
  if (ret->ReturnCode != SCARD_S_SUCCESS)
2671
0
    cReaders = 0;
2672
0
  if (cReaders == SCARD_AUTOALLOCATE)
2673
0
    cReaders = 0;
2674
2675
0
  if (!Stream_EnsureRemainingCapacity(s, 4))
2676
0
    return SCARD_E_NO_MEMORY;
2677
2678
0
  Stream_Write_UINT32(s, cReaders); /* cReaders (4 bytes) */
2679
0
  if (!smartcard_ndr_pointer_write(s, &index, cReaders))
2680
0
    return SCARD_E_NO_MEMORY;
2681
0
  status = smartcard_ndr_write_state(s, ret->rgReaderStates, cReaders, NDR_PTR_SIMPLE);
2682
0
  if (status != SCARD_S_SUCCESS)
2683
0
    return status;
2684
0
  return ret->ReturnCode;
2685
0
}
2686
2687
LONG smartcard_unpack_state_call(wStream* s, State_Call* call)
2688
0
{
2689
0
  UINT32 index = 0;
2690
0
  UINT32 pbContextNdrPtr = 0;
2691
2692
0
  wLog* log = scard_log();
2693
2694
0
  WINPR_ASSERT(call);
2695
0
  LONG status = smartcard_unpack_redir_scard_context(log, s, &(call->handles.hContext), &index,
2696
0
                                                     &pbContextNdrPtr);
2697
0
  if (status != SCARD_S_SUCCESS)
2698
0
    return status;
2699
2700
0
  status = smartcard_unpack_redir_scard_handle(log, s, &(call->handles.hCard), &index);
2701
0
  if (status != SCARD_S_SUCCESS)
2702
0
    return status;
2703
2704
0
  if (!Stream_CheckAndLogRequiredLengthWLog(log, s, 8))
2705
0
    return STATUS_BUFFER_TOO_SMALL;
2706
2707
0
  Stream_Read_INT32(s, call->fpbAtrIsNULL); /* fpbAtrIsNULL (4 bytes) */
2708
0
  Stream_Read_UINT32(s, call->cbAtrLen);    /* cbAtrLen (4 bytes) */
2709
2710
0
  status = smartcard_unpack_redir_scard_context_ref(log, s, pbContextNdrPtr,
2711
0
                                                    &(call->handles.hContext));
2712
0
  if (status != SCARD_S_SUCCESS)
2713
0
    return status;
2714
2715
0
  status = smartcard_unpack_redir_scard_handle_ref(log, s, &(call->handles.hCard));
2716
0
  if (status != SCARD_S_SUCCESS)
2717
0
    return status;
2718
2719
0
  return status;
2720
0
}
2721
2722
LONG smartcard_pack_state_return(wStream* s, const State_Return* ret)
2723
0
{
2724
0
  WINPR_ASSERT(ret);
2725
0
  wLog* log = scard_log();
2726
0
  LONG status = 0;
2727
0
  DWORD cbAtrLen = ret->cbAtrLen;
2728
0
  UINT32 index = 0;
2729
2730
0
  smartcard_trace_state_return(log, ret);
2731
0
  if (ret->ReturnCode != SCARD_S_SUCCESS)
2732
0
    cbAtrLen = 0;
2733
0
  if (cbAtrLen == SCARD_AUTOALLOCATE)
2734
0
    cbAtrLen = 0;
2735
2736
0
  Stream_Write_UINT32(s, ret->dwState);    /* dwState (4 bytes) */
2737
0
  Stream_Write_UINT32(s, ret->dwProtocol); /* dwProtocol (4 bytes) */
2738
0
  Stream_Write_UINT32(s, cbAtrLen);        /* cbAtrLen (4 bytes) */
2739
0
  if (!smartcard_ndr_pointer_write(s, &index, cbAtrLen))
2740
0
    return SCARD_E_NO_MEMORY;
2741
0
  status = smartcard_ndr_write(s, ret->rgAtr, cbAtrLen, 1, NDR_PTR_SIMPLE);
2742
0
  if (status != SCARD_S_SUCCESS)
2743
0
    return status;
2744
0
  return ret->ReturnCode;
2745
0
}
2746
2747
LONG smartcard_unpack_status_call(wStream* s, Status_Call* call, BOOL unicode)
2748
0
{
2749
0
  UINT32 index = 0;
2750
0
  UINT32 pbContextNdrPtr = 0;
2751
2752
0
  WINPR_ASSERT(call);
2753
0
  wLog* log = scard_log();
2754
2755
0
  LONG status = smartcard_unpack_redir_scard_context(log, s, &(call->handles.hContext), &index,
2756
0
                                                     &pbContextNdrPtr);
2757
0
  if (status != SCARD_S_SUCCESS)
2758
0
    return status;
2759
2760
0
  status = smartcard_unpack_redir_scard_handle(log, s, &(call->handles.hCard), &index);
2761
0
  if (status != SCARD_S_SUCCESS)
2762
0
    return status;
2763
2764
0
  if (!Stream_CheckAndLogRequiredLengthWLog(log, s, 12))
2765
0
    return STATUS_BUFFER_TOO_SMALL;
2766
2767
0
  Stream_Read_INT32(s, call->fmszReaderNamesIsNULL); /* fmszReaderNamesIsNULL (4 bytes) */
2768
0
  Stream_Read_UINT32(s, call->cchReaderLen);         /* cchReaderLen (4 bytes) */
2769
0
  Stream_Read_UINT32(s, call->cbAtrLen);             /* cbAtrLen (4 bytes) */
2770
2771
0
  status = smartcard_unpack_redir_scard_context_ref(log, s, pbContextNdrPtr,
2772
0
                                                    &(call->handles.hContext));
2773
0
  if (status != SCARD_S_SUCCESS)
2774
0
    return status;
2775
2776
0
  status = smartcard_unpack_redir_scard_handle_ref(log, s, &(call->handles.hCard));
2777
0
  if (status != SCARD_S_SUCCESS)
2778
0
    return status;
2779
2780
0
  smartcard_trace_status_call(log, call, unicode);
2781
0
  return status;
2782
0
}
2783
2784
LONG smartcard_pack_status_return(wStream* s, const Status_Return* ret, BOOL unicode)
2785
0
{
2786
0
  WINPR_ASSERT(ret);
2787
0
  wLog* log = scard_log();
2788
2789
0
  LONG status = 0;
2790
0
  UINT32 index = 0;
2791
0
  DWORD cBytes = ret->cBytes;
2792
2793
0
  smartcard_trace_status_return(log, ret, unicode);
2794
0
  if (ret->ReturnCode != SCARD_S_SUCCESS)
2795
0
    cBytes = 0;
2796
0
  if (cBytes == SCARD_AUTOALLOCATE)
2797
0
    cBytes = 0;
2798
2799
0
  if (!Stream_EnsureRemainingCapacity(s, 4))
2800
0
    return SCARD_F_INTERNAL_ERROR;
2801
2802
0
  Stream_Write_UINT32(s, cBytes); /* cBytes (4 bytes) */
2803
0
  if (!smartcard_ndr_pointer_write(s, &index, cBytes))
2804
0
    return SCARD_E_NO_MEMORY;
2805
2806
0
  if (!Stream_EnsureRemainingCapacity(s, 44))
2807
0
    return SCARD_F_INTERNAL_ERROR;
2808
2809
0
  Stream_Write_UINT32(s, ret->dwState);            /* dwState (4 bytes) */
2810
0
  Stream_Write_UINT32(s, ret->dwProtocol);         /* dwProtocol (4 bytes) */
2811
0
  Stream_Write(s, ret->pbAtr, sizeof(ret->pbAtr)); /* pbAtr (32 bytes) */
2812
0
  Stream_Write_UINT32(s, ret->cbAtrLen);           /* cbAtrLen (4 bytes) */
2813
0
  status = smartcard_ndr_write(s, ret->mszReaderNames, cBytes, 1, NDR_PTR_SIMPLE);
2814
0
  if (status != SCARD_S_SUCCESS)
2815
0
    return status;
2816
0
  return ret->ReturnCode;
2817
0
}
2818
2819
LONG smartcard_unpack_get_attrib_call(wStream* s, GetAttrib_Call* call)
2820
0
{
2821
0
  WINPR_ASSERT(call);
2822
0
  wLog* log = scard_log();
2823
0
  UINT32 index = 0;
2824
0
  UINT32 pbContextNdrPtr = 0;
2825
2826
0
  LONG status = smartcard_unpack_redir_scard_context(log, s, &(call->handles.hContext), &index,
2827
0
                                                     &pbContextNdrPtr);
2828
0
  if (status != SCARD_S_SUCCESS)
2829
0
    return status;
2830
2831
0
  status = smartcard_unpack_redir_scard_handle(log, s, &(call->handles.hCard), &index);
2832
0
  if (status != SCARD_S_SUCCESS)
2833
0
    return status;
2834
2835
0
  if (!Stream_CheckAndLogRequiredLengthWLog(log, s, 12))
2836
0
    return STATUS_BUFFER_TOO_SMALL;
2837
2838
0
  Stream_Read_UINT32(s, call->dwAttrId);     /* dwAttrId (4 bytes) */
2839
0
  Stream_Read_INT32(s, call->fpbAttrIsNULL); /* fpbAttrIsNULL (4 bytes) */
2840
0
  Stream_Read_UINT32(s, call->cbAttrLen);    /* cbAttrLen (4 bytes) */
2841
2842
0
  status = smartcard_unpack_redir_scard_context_ref(log, s, pbContextNdrPtr,
2843
0
                                                    &(call->handles.hContext));
2844
0
  if (status != SCARD_S_SUCCESS)
2845
0
    return status;
2846
2847
0
  status = smartcard_unpack_redir_scard_handle_ref(log, s, &(call->handles.hCard));
2848
0
  if (status != SCARD_S_SUCCESS)
2849
0
    return status;
2850
2851
0
  smartcard_trace_get_attrib_call(log, call);
2852
0
  return status;
2853
0
}
2854
2855
LONG smartcard_pack_get_attrib_return(wStream* s, const GetAttrib_Return* ret, DWORD dwAttrId,
2856
                                      DWORD cbAttrCallLen)
2857
0
{
2858
0
  WINPR_ASSERT(ret);
2859
0
  wLog* log = scard_log();
2860
0
  LONG status = 0;
2861
0
  DWORD cbAttrLen = 0;
2862
0
  UINT32 index = 0;
2863
0
  smartcard_trace_get_attrib_return(log, ret, dwAttrId);
2864
2865
0
  if (!Stream_EnsureRemainingCapacity(s, 4))
2866
0
    return SCARD_F_INTERNAL_ERROR;
2867
2868
0
  cbAttrLen = ret->cbAttrLen;
2869
0
  if (ret->ReturnCode != SCARD_S_SUCCESS)
2870
0
    cbAttrLen = 0;
2871
0
  if (cbAttrLen == SCARD_AUTOALLOCATE)
2872
0
    cbAttrLen = 0;
2873
2874
0
  if (ret->pbAttr)
2875
0
  {
2876
0
    if (cbAttrCallLen < cbAttrLen)
2877
0
      cbAttrLen = cbAttrCallLen;
2878
0
  }
2879
0
  Stream_Write_UINT32(s, cbAttrLen); /* cbAttrLen (4 bytes) */
2880
0
  if (!smartcard_ndr_pointer_write(s, &index, cbAttrLen))
2881
0
    return SCARD_E_NO_MEMORY;
2882
2883
0
  status = smartcard_ndr_write(s, ret->pbAttr, cbAttrLen, 1, NDR_PTR_SIMPLE);
2884
0
  if (status != SCARD_S_SUCCESS)
2885
0
    return status;
2886
0
  return ret->ReturnCode;
2887
0
}
2888
2889
LONG smartcard_unpack_control_call(wStream* s, Control_Call* call)
2890
0
{
2891
0
  WINPR_ASSERT(call);
2892
0
  wLog* log = scard_log();
2893
2894
0
  UINT32 index = 0;
2895
0
  UINT32 pvInBufferNdrPtr = 0;
2896
0
  UINT32 pbContextNdrPtr = 0;
2897
2898
0
  call->pvInBuffer = NULL;
2899
2900
0
  LONG status = smartcard_unpack_redir_scard_context(log, s, &(call->handles.hContext), &index,
2901
0
                                                     &pbContextNdrPtr);
2902
0
  if (status != SCARD_S_SUCCESS)
2903
0
    return status;
2904
2905
0
  status = smartcard_unpack_redir_scard_handle(log, s, &(call->handles.hCard), &index);
2906
0
  if (status != SCARD_S_SUCCESS)
2907
0
    return status;
2908
2909
0
  if (!Stream_CheckAndLogRequiredLengthWLog(log, s, 20))
2910
0
    return STATUS_BUFFER_TOO_SMALL;
2911
2912
0
  Stream_Read_UINT32(s, call->dwControlCode);                    /* dwControlCode (4 bytes) */
2913
0
  Stream_Read_UINT32(s, call->cbInBufferSize);                   /* cbInBufferSize (4 bytes) */
2914
0
  if (!smartcard_ndr_pointer_read(log, s, &index,
2915
0
                                  &pvInBufferNdrPtr)) /* pvInBufferNdrPtr (4 bytes) */
2916
0
    return ERROR_INVALID_DATA;
2917
0
  Stream_Read_INT32(s, call->fpvOutBufferIsNULL); /* fpvOutBufferIsNULL (4 bytes) */
2918
0
  Stream_Read_UINT32(s, call->cbOutBufferSize);   /* cbOutBufferSize (4 bytes) */
2919
2920
0
  status = smartcard_unpack_redir_scard_context_ref(log, s, pbContextNdrPtr,
2921
0
                                                    &(call->handles.hContext));
2922
0
  if (status != SCARD_S_SUCCESS)
2923
0
    return status;
2924
2925
0
  status = smartcard_unpack_redir_scard_handle_ref(log, s, &(call->handles.hCard));
2926
0
  if (status != SCARD_S_SUCCESS)
2927
0
    return status;
2928
2929
0
  if (pvInBufferNdrPtr)
2930
0
  {
2931
0
    status =
2932
0
        smartcard_ndr_read(log, s, &call->pvInBuffer, call->cbInBufferSize, 1, NDR_PTR_SIMPLE);
2933
0
    if (status != SCARD_S_SUCCESS)
2934
0
      return status;
2935
0
  }
2936
2937
0
  smartcard_trace_control_call(log, call);
2938
0
  return SCARD_S_SUCCESS;
2939
0
}
2940
2941
LONG smartcard_pack_control_return(wStream* s, const Control_Return* ret)
2942
0
{
2943
0
  WINPR_ASSERT(ret);
2944
0
  wLog* log = scard_log();
2945
2946
0
  LONG status = 0;
2947
0
  DWORD cbDataLen = ret->cbOutBufferSize;
2948
0
  UINT32 index = 0;
2949
2950
0
  smartcard_trace_control_return(log, ret);
2951
0
  if (ret->ReturnCode != SCARD_S_SUCCESS)
2952
0
    cbDataLen = 0;
2953
0
  if (cbDataLen == SCARD_AUTOALLOCATE)
2954
0
    cbDataLen = 0;
2955
2956
0
  if (!Stream_EnsureRemainingCapacity(s, 4))
2957
0
    return SCARD_F_INTERNAL_ERROR;
2958
2959
0
  Stream_Write_UINT32(s, cbDataLen); /* cbOutBufferSize (4 bytes) */
2960
0
  if (!smartcard_ndr_pointer_write(s, &index, cbDataLen))
2961
0
    return SCARD_E_NO_MEMORY;
2962
2963
0
  status = smartcard_ndr_write(s, ret->pvOutBuffer, cbDataLen, 1, NDR_PTR_SIMPLE);
2964
0
  if (status != SCARD_S_SUCCESS)
2965
0
    return status;
2966
0
  return ret->ReturnCode;
2967
0
}
2968
2969
LONG smartcard_unpack_transmit_call(wStream* s, Transmit_Call* call)
2970
0
{
2971
0
  UINT32 length = 0;
2972
0
  BYTE* pbExtraBytes = NULL;
2973
0
  UINT32 pbExtraBytesNdrPtr = 0;
2974
0
  UINT32 pbSendBufferNdrPtr = 0;
2975
0
  UINT32 pioRecvPciNdrPtr = 0;
2976
0
  SCardIO_Request ioSendPci;
2977
0
  SCardIO_Request ioRecvPci;
2978
0
  UINT32 index = 0;
2979
0
  UINT32 pbContextNdrPtr = 0;
2980
2981
0
  WINPR_ASSERT(call);
2982
0
  wLog* log = scard_log();
2983
2984
0
  call->pioSendPci = NULL;
2985
0
  call->pioRecvPci = NULL;
2986
0
  call->pbSendBuffer = NULL;
2987
2988
0
  LONG status = smartcard_unpack_redir_scard_context(log, s, &(call->handles.hContext), &index,
2989
0
                                                     &pbContextNdrPtr);
2990
0
  if (status != SCARD_S_SUCCESS)
2991
0
    return status;
2992
2993
0
  status = smartcard_unpack_redir_scard_handle(log, s, &(call->handles.hCard), &index);
2994
0
  if (status != SCARD_S_SUCCESS)
2995
0
    return status;
2996
2997
0
  if (!Stream_CheckAndLogRequiredLengthWLog(log, s, 32))
2998
0
    return STATUS_BUFFER_TOO_SMALL;
2999
3000
0
  Stream_Read_UINT32(s, ioSendPci.dwProtocol);   /* dwProtocol (4 bytes) */
3001
0
  Stream_Read_UINT32(s, ioSendPci.cbExtraBytes); /* cbExtraBytes (4 bytes) */
3002
0
  if (!smartcard_ndr_pointer_read(log, s, &index,
3003
0
                                  &pbExtraBytesNdrPtr)) /* pbExtraBytesNdrPtr (4 bytes) */
3004
0
    return ERROR_INVALID_DATA;
3005
3006
0
  Stream_Read_UINT32(s, call->cbSendLength); /* cbSendLength (4 bytes) */
3007
0
  if (!smartcard_ndr_pointer_read(log, s, &index,
3008
0
                                  &pbSendBufferNdrPtr)) /* pbSendBufferNdrPtr (4 bytes) */
3009
0
    return ERROR_INVALID_DATA;
3010
3011
0
  if (!smartcard_ndr_pointer_read(log, s, &index,
3012
0
                                  &pioRecvPciNdrPtr)) /* pioRecvPciNdrPtr (4 bytes) */
3013
0
    return ERROR_INVALID_DATA;
3014
3015
0
  Stream_Read_INT32(s, call->fpbRecvBufferIsNULL); /* fpbRecvBufferIsNULL (4 bytes) */
3016
0
  Stream_Read_UINT32(s, call->cbRecvLength);       /* cbRecvLength (4 bytes) */
3017
3018
0
  if (ioSendPci.cbExtraBytes > 1024)
3019
0
  {
3020
0
    WLog_Print(log, WLOG_WARN,
3021
0
               "Transmit_Call ioSendPci.cbExtraBytes is out of bounds: %" PRIu32 " (max: 1024)",
3022
0
               ioSendPci.cbExtraBytes);
3023
0
    return STATUS_INVALID_PARAMETER;
3024
0
  }
3025
3026
0
  if (call->cbSendLength > 66560)
3027
0
  {
3028
0
    WLog_Print(log, WLOG_WARN,
3029
0
               "Transmit_Call cbSendLength is out of bounds: %" PRIu32 " (max: 66560)",
3030
0
               ioSendPci.cbExtraBytes);
3031
0
    return STATUS_INVALID_PARAMETER;
3032
0
  }
3033
3034
0
  status = smartcard_unpack_redir_scard_context_ref(log, s, pbContextNdrPtr,
3035
0
                                                    &(call->handles.hContext));
3036
0
  if (status != SCARD_S_SUCCESS)
3037
0
    return status;
3038
3039
0
  status = smartcard_unpack_redir_scard_handle_ref(log, s, &(call->handles.hCard));
3040
0
  if (status != SCARD_S_SUCCESS)
3041
0
    return status;
3042
3043
0
  if (ioSendPci.cbExtraBytes && !pbExtraBytesNdrPtr)
3044
0
  {
3045
0
    WLog_Print(
3046
0
        log, WLOG_WARN,
3047
0
        "Transmit_Call ioSendPci.cbExtraBytes is non-zero but pbExtraBytesNdrPtr is null");
3048
0
    return STATUS_INVALID_PARAMETER;
3049
0
  }
3050
3051
0
  if (pbExtraBytesNdrPtr)
3052
0
  {
3053
    // TODO: Use unified pointer reading
3054
0
    if (!Stream_CheckAndLogRequiredLengthWLog(log, s, 4))
3055
0
      return STATUS_BUFFER_TOO_SMALL;
3056
3057
0
    Stream_Read_UINT32(s, length); /* Length (4 bytes) */
3058
3059
0
    if (!Stream_CheckAndLogRequiredLengthWLog(log, s, ioSendPci.cbExtraBytes))
3060
0
      return STATUS_BUFFER_TOO_SMALL;
3061
3062
0
    ioSendPci.pbExtraBytes = Stream_Pointer(s);
3063
0
    call->pioSendPci =
3064
0
        (LPSCARD_IO_REQUEST)malloc(sizeof(SCARD_IO_REQUEST) + ioSendPci.cbExtraBytes);
3065
3066
0
    if (!call->pioSendPci)
3067
0
    {
3068
0
      WLog_Print(log, WLOG_WARN, "Transmit_Call out of memory error (pioSendPci)");
3069
0
      return STATUS_NO_MEMORY;
3070
0
    }
3071
3072
0
    call->pioSendPci->dwProtocol = ioSendPci.dwProtocol;
3073
0
    call->pioSendPci->cbPciLength = (DWORD)(ioSendPci.cbExtraBytes + sizeof(SCARD_IO_REQUEST));
3074
0
    pbExtraBytes = &((BYTE*)call->pioSendPci)[sizeof(SCARD_IO_REQUEST)];
3075
0
    Stream_Read(s, pbExtraBytes, ioSendPci.cbExtraBytes);
3076
0
    smartcard_unpack_read_size_align(s, ioSendPci.cbExtraBytes, 4);
3077
0
  }
3078
0
  else
3079
0
  {
3080
0
    call->pioSendPci = (LPSCARD_IO_REQUEST)calloc(1, sizeof(SCARD_IO_REQUEST));
3081
3082
0
    if (!call->pioSendPci)
3083
0
    {
3084
0
      WLog_Print(log, WLOG_WARN, "Transmit_Call out of memory error (pioSendPci)");
3085
0
      return STATUS_NO_MEMORY;
3086
0
    }
3087
3088
0
    call->pioSendPci->dwProtocol = ioSendPci.dwProtocol;
3089
0
    call->pioSendPci->cbPciLength = sizeof(SCARD_IO_REQUEST);
3090
0
  }
3091
3092
0
  if (pbSendBufferNdrPtr)
3093
0
  {
3094
0
    status =
3095
0
        smartcard_ndr_read(log, s, &call->pbSendBuffer, call->cbSendLength, 1, NDR_PTR_SIMPLE);
3096
0
    if (status != SCARD_S_SUCCESS)
3097
0
      return status;
3098
0
  }
3099
3100
0
  if (pioRecvPciNdrPtr)
3101
0
  {
3102
0
    if (!Stream_CheckAndLogRequiredLengthWLog(log, s, 12))
3103
0
      return STATUS_BUFFER_TOO_SMALL;
3104
3105
0
    Stream_Read_UINT32(s, ioRecvPci.dwProtocol);   /* dwProtocol (4 bytes) */
3106
0
    Stream_Read_UINT32(s, ioRecvPci.cbExtraBytes); /* cbExtraBytes (4 bytes) */
3107
0
    if (!smartcard_ndr_pointer_read(log, s, &index,
3108
0
                                    &pbExtraBytesNdrPtr)) /* pbExtraBytesNdrPtr (4 bytes) */
3109
0
      return ERROR_INVALID_DATA;
3110
3111
0
    if (ioRecvPci.cbExtraBytes && !pbExtraBytesNdrPtr)
3112
0
    {
3113
0
      WLog_Print(
3114
0
          log, WLOG_WARN,
3115
0
          "Transmit_Call ioRecvPci.cbExtraBytes is non-zero but pbExtraBytesNdrPtr is null");
3116
0
      return STATUS_INVALID_PARAMETER;
3117
0
    }
3118
3119
0
    if (pbExtraBytesNdrPtr)
3120
0
    {
3121
      // TODO: Unify ndr pointer reading
3122
0
      if (!Stream_CheckAndLogRequiredLengthWLog(log, s, 4))
3123
0
        return STATUS_BUFFER_TOO_SMALL;
3124
3125
0
      Stream_Read_UINT32(s, length); /* Length (4 bytes) */
3126
3127
0
      if (ioRecvPci.cbExtraBytes > 1024)
3128
0
      {
3129
0
        WLog_Print(log, WLOG_WARN,
3130
0
                   "Transmit_Call ioRecvPci.cbExtraBytes is out of bounds: %" PRIu32
3131
0
                   " (max: 1024)",
3132
0
                   ioRecvPci.cbExtraBytes);
3133
0
        return STATUS_INVALID_PARAMETER;
3134
0
      }
3135
3136
0
      if (length != ioRecvPci.cbExtraBytes)
3137
0
      {
3138
0
        WLog_Print(log, WLOG_WARN,
3139
0
                   "Transmit_Call unexpected length: Actual: %" PRIu32
3140
0
                   ", Expected: %" PRIu32 " (ioRecvPci.cbExtraBytes)",
3141
0
                   length, ioRecvPci.cbExtraBytes);
3142
0
        return STATUS_INVALID_PARAMETER;
3143
0
      }
3144
3145
0
      if (!Stream_CheckAndLogRequiredLengthWLog(log, s, ioRecvPci.cbExtraBytes))
3146
0
        return STATUS_BUFFER_TOO_SMALL;
3147
3148
0
      ioRecvPci.pbExtraBytes = Stream_Pointer(s);
3149
0
      call->pioRecvPci =
3150
0
          (LPSCARD_IO_REQUEST)malloc(sizeof(SCARD_IO_REQUEST) + ioRecvPci.cbExtraBytes);
3151
3152
0
      if (!call->pioRecvPci)
3153
0
      {
3154
0
        WLog_Print(log, WLOG_WARN, "Transmit_Call out of memory error (pioRecvPci)");
3155
0
        return STATUS_NO_MEMORY;
3156
0
      }
3157
3158
0
      call->pioRecvPci->dwProtocol = ioRecvPci.dwProtocol;
3159
0
      call->pioRecvPci->cbPciLength =
3160
0
          (DWORD)(ioRecvPci.cbExtraBytes + sizeof(SCARD_IO_REQUEST));
3161
0
      pbExtraBytes = &((BYTE*)call->pioRecvPci)[sizeof(SCARD_IO_REQUEST)];
3162
0
      Stream_Read(s, pbExtraBytes, ioRecvPci.cbExtraBytes);
3163
0
      smartcard_unpack_read_size_align(s, ioRecvPci.cbExtraBytes, 4);
3164
0
    }
3165
0
    else
3166
0
    {
3167
0
      call->pioRecvPci = (LPSCARD_IO_REQUEST)calloc(1, sizeof(SCARD_IO_REQUEST));
3168
3169
0
      if (!call->pioRecvPci)
3170
0
      {
3171
0
        WLog_Print(log, WLOG_WARN, "Transmit_Call out of memory error (pioRecvPci)");
3172
0
        return STATUS_NO_MEMORY;
3173
0
      }
3174
3175
0
      call->pioRecvPci->dwProtocol = ioRecvPci.dwProtocol;
3176
0
      call->pioRecvPci->cbPciLength = sizeof(SCARD_IO_REQUEST);
3177
0
    }
3178
0
  }
3179
3180
0
  smartcard_trace_transmit_call(log, call);
3181
0
  return SCARD_S_SUCCESS;
3182
0
}
3183
3184
LONG smartcard_pack_transmit_return(wStream* s, const Transmit_Return* ret)
3185
0
{
3186
0
  WINPR_ASSERT(ret);
3187
0
  wLog* log = scard_log();
3188
3189
0
  LONG status = 0;
3190
0
  UINT32 index = 0;
3191
0
  LONG error = 0;
3192
0
  UINT32 cbRecvLength = ret->cbRecvLength;
3193
0
  UINT32 cbRecvPci = ret->pioRecvPci ? ret->pioRecvPci->cbPciLength : 0;
3194
3195
0
  smartcard_trace_transmit_return(log, ret);
3196
3197
0
  if (!ret->pbRecvBuffer)
3198
0
    cbRecvLength = 0;
3199
3200
0
  if (!smartcard_ndr_pointer_write(s, &index, cbRecvPci))
3201
0
    return SCARD_E_NO_MEMORY;
3202
0
  if (!Stream_EnsureRemainingCapacity(s, 4))
3203
0
    return SCARD_E_NO_MEMORY;
3204
0
  Stream_Write_UINT32(s, cbRecvLength); /* cbRecvLength (4 bytes) */
3205
0
  if (!smartcard_ndr_pointer_write(s, &index, cbRecvLength))
3206
0
    return SCARD_E_NO_MEMORY;
3207
3208
0
  if (ret->pioRecvPci)
3209
0
  {
3210
0
    UINT32 cbExtraBytes = (UINT32)(ret->pioRecvPci->cbPciLength - sizeof(SCARD_IO_REQUEST));
3211
0
    BYTE* pbExtraBytes = &((BYTE*)ret->pioRecvPci)[sizeof(SCARD_IO_REQUEST)];
3212
3213
0
    if (!Stream_EnsureRemainingCapacity(s, cbExtraBytes + 16))
3214
0
    {
3215
0
      WLog_Print(log, WLOG_ERROR, "Stream_EnsureRemainingCapacity failed!");
3216
0
      return SCARD_F_INTERNAL_ERROR;
3217
0
    }
3218
3219
0
    Stream_Write_UINT32(s, ret->pioRecvPci->dwProtocol); /* dwProtocol (4 bytes) */
3220
0
    Stream_Write_UINT32(s, cbExtraBytes);                /* cbExtraBytes (4 bytes) */
3221
0
    if (!smartcard_ndr_pointer_write(s, &index, cbExtraBytes))
3222
0
      return SCARD_E_NO_MEMORY;
3223
0
    error = smartcard_ndr_write(s, pbExtraBytes, cbExtraBytes, 1, NDR_PTR_SIMPLE);
3224
0
    if (error)
3225
0
      return error;
3226
0
  }
3227
3228
0
  status = smartcard_ndr_write(s, ret->pbRecvBuffer, ret->cbRecvLength, 1, NDR_PTR_SIMPLE);
3229
0
  if (status != SCARD_S_SUCCESS)
3230
0
    return status;
3231
0
  return ret->ReturnCode;
3232
0
}
3233
3234
LONG smartcard_unpack_locate_cards_by_atr_a_call(wStream* s, LocateCardsByATRA_Call* call)
3235
0
{
3236
0
  UINT32 rgReaderStatesNdrPtr = 0;
3237
0
  UINT32 rgAtrMasksNdrPtr = 0;
3238
0
  UINT32 index = 0;
3239
0
  UINT32 pbContextNdrPtr = 0;
3240
3241
0
  WINPR_ASSERT(call);
3242
0
  wLog* log = scard_log();
3243
3244
0
  call->rgReaderStates = NULL;
3245
3246
0
  LONG status = smartcard_unpack_redir_scard_context(log, s, &(call->handles.hContext), &index,
3247
0
                                                     &pbContextNdrPtr);
3248
0
  if (status != SCARD_S_SUCCESS)
3249
0
    return status;
3250
3251
0
  if (!Stream_CheckAndLogRequiredLengthWLog(log, s, 16))
3252
0
    return STATUS_BUFFER_TOO_SMALL;
3253
3254
0
  Stream_Read_UINT32(s, call->cAtrs);
3255
0
  if (!smartcard_ndr_pointer_read(log, s, &index, &rgAtrMasksNdrPtr))
3256
0
    return ERROR_INVALID_DATA;
3257
0
  Stream_Read_UINT32(s, call->cReaders); /* cReaders (4 bytes) */
3258
0
  if (!smartcard_ndr_pointer_read(log, s, &index, &rgReaderStatesNdrPtr))
3259
0
    return ERROR_INVALID_DATA;
3260
3261
0
  status = smartcard_unpack_redir_scard_context_ref(log, s, pbContextNdrPtr,
3262
0
                                                    &(call->handles.hContext));
3263
0
  if (status != SCARD_S_SUCCESS)
3264
0
    return status;
3265
3266
0
  if ((rgAtrMasksNdrPtr && !call->cAtrs) || (!rgAtrMasksNdrPtr && call->cAtrs))
3267
0
  {
3268
0
    WLog_Print(log, WLOG_WARN,
3269
0
               "LocateCardsByATRA_Call rgAtrMasksNdrPtr (0x%08" PRIX32
3270
0
               ") and cAtrs (0x%08" PRIX32 ") inconsistency",
3271
0
               rgAtrMasksNdrPtr, call->cAtrs);
3272
0
    return STATUS_INVALID_PARAMETER;
3273
0
  }
3274
3275
0
  if (rgAtrMasksNdrPtr)
3276
0
  {
3277
0
    status = smartcard_ndr_read_atrmask(log, s, &call->rgAtrMasks, call->cAtrs, NDR_PTR_SIMPLE);
3278
0
    if (status != SCARD_S_SUCCESS)
3279
0
      return status;
3280
0
  }
3281
3282
0
  if (rgReaderStatesNdrPtr)
3283
0
  {
3284
0
    status =
3285
0
        smartcard_unpack_reader_state_a(log, s, &call->rgReaderStates, call->cReaders, &index);
3286
0
    if (status != SCARD_S_SUCCESS)
3287
0
      return status;
3288
0
  }
3289
3290
0
  smartcard_trace_locate_cards_by_atr_a_call(log, call);
3291
0
  return SCARD_S_SUCCESS;
3292
0
}
3293
3294
LONG smartcard_unpack_context_and_two_strings_a_call(wStream* s, ContextAndTwoStringA_Call* call)
3295
0
{
3296
0
  UINT32 sz1NdrPtr = 0;
3297
0
  UINT32 sz2NdrPtr = 0;
3298
0
  UINT32 index = 0;
3299
0
  UINT32 pbContextNdrPtr = 0;
3300
3301
0
  WINPR_ASSERT(call);
3302
0
  wLog* log = scard_log();
3303
3304
0
  LONG status = smartcard_unpack_redir_scard_context(log, s, &(call->handles.hContext), &index,
3305
0
                                                     &pbContextNdrPtr);
3306
0
  if (status != SCARD_S_SUCCESS)
3307
0
    return status;
3308
3309
0
  if (!smartcard_ndr_pointer_read(log, s, &index, &sz1NdrPtr))
3310
0
    return ERROR_INVALID_DATA;
3311
0
  if (!smartcard_ndr_pointer_read(log, s, &index, &sz2NdrPtr))
3312
0
    return ERROR_INVALID_DATA;
3313
3314
0
  status =
3315
0
      smartcard_unpack_redir_scard_context_ref(log, s, pbContextNdrPtr, &call->handles.hContext);
3316
0
  if (status != SCARD_S_SUCCESS)
3317
0
    return status;
3318
3319
0
  if (sz1NdrPtr)
3320
0
  {
3321
0
    status = smartcard_ndr_read_a(log, s, &call->sz1, NDR_PTR_FULL);
3322
0
    if (status != SCARD_S_SUCCESS)
3323
0
      return status;
3324
0
  }
3325
0
  if (sz2NdrPtr)
3326
0
  {
3327
0
    status = smartcard_ndr_read_a(log, s, &call->sz2, NDR_PTR_FULL);
3328
0
    if (status != SCARD_S_SUCCESS)
3329
0
      return status;
3330
0
  }
3331
0
  smartcard_trace_context_and_two_strings_a_call(log, call);
3332
0
  return SCARD_S_SUCCESS;
3333
0
}
3334
3335
LONG smartcard_unpack_context_and_two_strings_w_call(wStream* s, ContextAndTwoStringW_Call* call)
3336
0
{
3337
0
  UINT32 sz1NdrPtr = 0;
3338
0
  UINT32 sz2NdrPtr = 0;
3339
0
  UINT32 index = 0;
3340
0
  UINT32 pbContextNdrPtr = 0;
3341
3342
0
  WINPR_ASSERT(call);
3343
0
  wLog* log = scard_log();
3344
3345
0
  LONG status = smartcard_unpack_redir_scard_context(log, s, &(call->handles.hContext), &index,
3346
0
                                                     &pbContextNdrPtr);
3347
0
  if (status != SCARD_S_SUCCESS)
3348
0
    return status;
3349
3350
0
  if (!smartcard_ndr_pointer_read(log, s, &index, &sz1NdrPtr))
3351
0
    return ERROR_INVALID_DATA;
3352
0
  if (!smartcard_ndr_pointer_read(log, s, &index, &sz2NdrPtr))
3353
0
    return ERROR_INVALID_DATA;
3354
3355
0
  status =
3356
0
      smartcard_unpack_redir_scard_context_ref(log, s, pbContextNdrPtr, &call->handles.hContext);
3357
0
  if (status != SCARD_S_SUCCESS)
3358
0
    return status;
3359
3360
0
  if (sz1NdrPtr)
3361
0
  {
3362
0
    status = smartcard_ndr_read_w(log, s, &call->sz1, NDR_PTR_FULL);
3363
0
    if (status != SCARD_S_SUCCESS)
3364
0
      return status;
3365
0
  }
3366
0
  if (sz2NdrPtr)
3367
0
  {
3368
0
    status = smartcard_ndr_read_w(log, s, &call->sz2, NDR_PTR_FULL);
3369
0
    if (status != SCARD_S_SUCCESS)
3370
0
      return status;
3371
0
  }
3372
0
  smartcard_trace_context_and_two_strings_w_call(log, call);
3373
0
  return SCARD_S_SUCCESS;
3374
0
}
3375
3376
LONG smartcard_unpack_locate_cards_a_call(wStream* s, LocateCardsA_Call* call)
3377
0
{
3378
0
  UINT32 sz1NdrPtr = 0;
3379
0
  UINT32 sz2NdrPtr = 0;
3380
0
  UINT32 index = 0;
3381
0
  UINT32 pbContextNdrPtr = 0;
3382
3383
0
  WINPR_ASSERT(call);
3384
0
  wLog* log = scard_log();
3385
3386
0
  LONG status = smartcard_unpack_redir_scard_context(log, s, &(call->handles.hContext), &index,
3387
0
                                                     &pbContextNdrPtr);
3388
0
  if (status != SCARD_S_SUCCESS)
3389
0
    return status;
3390
3391
0
  if (!Stream_CheckAndLogRequiredLengthWLog(log, s, 16))
3392
0
    return STATUS_BUFFER_TOO_SMALL;
3393
3394
0
  Stream_Read_UINT32(s, call->cBytes);
3395
0
  if (!smartcard_ndr_pointer_read(log, s, &index, &sz1NdrPtr))
3396
0
    return ERROR_INVALID_DATA;
3397
3398
0
  Stream_Read_UINT32(s, call->cReaders);
3399
0
  if (!smartcard_ndr_pointer_read(log, s, &index, &sz2NdrPtr))
3400
0
    return ERROR_INVALID_DATA;
3401
3402
0
  status = smartcard_unpack_redir_scard_context_ref(log, s, pbContextNdrPtr,
3403
0
                                                    &(call->handles.hContext));
3404
0
  if (status != SCARD_S_SUCCESS)
3405
0
    return status;
3406
3407
0
  if (sz1NdrPtr)
3408
0
  {
3409
0
    status = smartcard_ndr_read_fixed_string_a(log, s, &call->mszCards, call->cBytes,
3410
0
                                               NDR_PTR_SIMPLE);
3411
0
    if (status != SCARD_S_SUCCESS)
3412
0
      return status;
3413
0
  }
3414
0
  if (sz2NdrPtr)
3415
0
  {
3416
0
    status =
3417
0
        smartcard_unpack_reader_state_a(log, s, &call->rgReaderStates, call->cReaders, &index);
3418
0
    if (status != SCARD_S_SUCCESS)
3419
0
      return status;
3420
0
  }
3421
0
  smartcard_trace_locate_cards_a_call(log, call);
3422
0
  return SCARD_S_SUCCESS;
3423
0
}
3424
3425
LONG smartcard_unpack_locate_cards_w_call(wStream* s, LocateCardsW_Call* call)
3426
0
{
3427
0
  UINT32 sz1NdrPtr = 0;
3428
0
  UINT32 sz2NdrPtr = 0;
3429
0
  UINT32 index = 0;
3430
0
  UINT32 pbContextNdrPtr = 0;
3431
3432
0
  WINPR_ASSERT(call);
3433
0
  wLog* log = scard_log();
3434
3435
0
  LONG status = smartcard_unpack_redir_scard_context(log, s, &(call->handles.hContext), &index,
3436
0
                                                     &pbContextNdrPtr);
3437
0
  if (status != SCARD_S_SUCCESS)
3438
0
    return status;
3439
3440
0
  if (!Stream_CheckAndLogRequiredLengthWLog(log, s, 16))
3441
0
    return STATUS_BUFFER_TOO_SMALL;
3442
3443
0
  Stream_Read_UINT32(s, call->cBytes);
3444
0
  if (!smartcard_ndr_pointer_read(log, s, &index, &sz1NdrPtr))
3445
0
    return ERROR_INVALID_DATA;
3446
3447
0
  Stream_Read_UINT32(s, call->cReaders);
3448
0
  if (!smartcard_ndr_pointer_read(log, s, &index, &sz2NdrPtr))
3449
0
    return ERROR_INVALID_DATA;
3450
3451
0
  status = smartcard_unpack_redir_scard_context_ref(log, s, pbContextNdrPtr,
3452
0
                                                    &(call->handles.hContext));
3453
0
  if (status != SCARD_S_SUCCESS)
3454
0
    return status;
3455
3456
0
  if (sz1NdrPtr)
3457
0
  {
3458
0
    status = smartcard_ndr_read_fixed_string_w(log, s, &call->mszCards, call->cBytes,
3459
0
                                               NDR_PTR_SIMPLE);
3460
0
    if (status != SCARD_S_SUCCESS)
3461
0
      return status;
3462
0
  }
3463
0
  if (sz2NdrPtr)
3464
0
  {
3465
0
    status =
3466
0
        smartcard_unpack_reader_state_w(log, s, &call->rgReaderStates, call->cReaders, &index);
3467
0
    if (status != SCARD_S_SUCCESS)
3468
0
      return status;
3469
0
  }
3470
0
  smartcard_trace_locate_cards_w_call(log, call);
3471
0
  return SCARD_S_SUCCESS;
3472
0
}
3473
3474
LONG smartcard_unpack_set_attrib_call(wStream* s, SetAttrib_Call* call)
3475
0
{
3476
0
  UINT32 index = 0;
3477
0
  UINT32 ndrPtr = 0;
3478
0
  UINT32 pbContextNdrPtr = 0;
3479
3480
0
  WINPR_ASSERT(call);
3481
0
  wLog* log = scard_log();
3482
3483
0
  LONG status = smartcard_unpack_redir_scard_context(log, s, &(call->handles.hContext), &index,
3484
0
                                                     &pbContextNdrPtr);
3485
0
  if (status != SCARD_S_SUCCESS)
3486
0
    return status;
3487
0
  status = smartcard_unpack_redir_scard_handle(log, s, &(call->handles.hCard), &index);
3488
0
  if (status != SCARD_S_SUCCESS)
3489
0
    return status;
3490
3491
0
  if (!Stream_CheckAndLogRequiredLengthWLog(log, s, 12))
3492
0
    return STATUS_BUFFER_TOO_SMALL;
3493
0
  Stream_Read_UINT32(s, call->dwAttrId);
3494
0
  Stream_Read_UINT32(s, call->cbAttrLen);
3495
3496
0
  if (!smartcard_ndr_pointer_read(log, s, &index, &ndrPtr))
3497
0
    return ERROR_INVALID_DATA;
3498
3499
0
  status = smartcard_unpack_redir_scard_context_ref(log, s, pbContextNdrPtr,
3500
0
                                                    &(call->handles.hContext));
3501
0
  if (status != SCARD_S_SUCCESS)
3502
0
    return status;
3503
3504
0
  status = smartcard_unpack_redir_scard_handle_ref(log, s, &(call->handles.hCard));
3505
0
  if (status != SCARD_S_SUCCESS)
3506
0
    return status;
3507
3508
0
  if (ndrPtr)
3509
0
  {
3510
    // TODO: call->cbAttrLen was larger than the pointer value.
3511
    // TODO: Maybe need to refine the checks?
3512
0
    status = smartcard_ndr_read(log, s, &call->pbAttr, 0, 1, NDR_PTR_SIMPLE);
3513
0
    if (status != SCARD_S_SUCCESS)
3514
0
      return status;
3515
0
  }
3516
0
  smartcard_trace_set_attrib_call(log, call);
3517
0
  return SCARD_S_SUCCESS;
3518
0
}
3519
3520
LONG smartcard_unpack_locate_cards_by_atr_w_call(wStream* s, LocateCardsByATRW_Call* call)
3521
0
{
3522
0
  UINT32 rgReaderStatesNdrPtr = 0;
3523
0
  UINT32 rgAtrMasksNdrPtr = 0;
3524
0
  UINT32 index = 0;
3525
0
  UINT32 pbContextNdrPtr = 0;
3526
3527
0
  WINPR_ASSERT(call);
3528
0
  wLog* log = scard_log();
3529
3530
0
  call->rgReaderStates = NULL;
3531
3532
0
  LONG status = smartcard_unpack_redir_scard_context(log, s, &(call->handles.hContext), &index,
3533
0
                                                     &pbContextNdrPtr);
3534
0
  if (status != SCARD_S_SUCCESS)
3535
0
    return status;
3536
3537
0
  if (!Stream_CheckAndLogRequiredLengthWLog(log, s, 16))
3538
0
    return STATUS_BUFFER_TOO_SMALL;
3539
3540
0
  Stream_Read_UINT32(s, call->cAtrs);
3541
0
  if (!smartcard_ndr_pointer_read(log, s, &index, &rgAtrMasksNdrPtr))
3542
0
    return ERROR_INVALID_DATA;
3543
3544
0
  Stream_Read_UINT32(s, call->cReaders); /* cReaders (4 bytes) */
3545
0
  if (!smartcard_ndr_pointer_read(log, s, &index, &rgReaderStatesNdrPtr))
3546
0
    return ERROR_INVALID_DATA;
3547
3548
0
  status = smartcard_unpack_redir_scard_context_ref(log, s, pbContextNdrPtr,
3549
0
                                                    &(call->handles.hContext));
3550
0
  if (status != SCARD_S_SUCCESS)
3551
0
    return status;
3552
3553
0
  if ((rgAtrMasksNdrPtr && !call->cAtrs) || (!rgAtrMasksNdrPtr && call->cAtrs))
3554
0
  {
3555
0
    WLog_Print(log, WLOG_WARN,
3556
0
               "LocateCardsByATRW_Call rgAtrMasksNdrPtr (0x%08" PRIX32
3557
0
               ") and cAtrs (0x%08" PRIX32 ") inconsistency",
3558
0
               rgAtrMasksNdrPtr, call->cAtrs);
3559
0
    return STATUS_INVALID_PARAMETER;
3560
0
  }
3561
3562
0
  if (rgAtrMasksNdrPtr)
3563
0
  {
3564
0
    status = smartcard_ndr_read_atrmask(log, s, &call->rgAtrMasks, call->cAtrs, NDR_PTR_SIMPLE);
3565
0
    if (status != SCARD_S_SUCCESS)
3566
0
      return status;
3567
0
  }
3568
3569
0
  if (rgReaderStatesNdrPtr)
3570
0
  {
3571
0
    status =
3572
0
        smartcard_unpack_reader_state_w(log, s, &call->rgReaderStates, call->cReaders, &index);
3573
0
    if (status != SCARD_S_SUCCESS)
3574
0
      return status;
3575
0
  }
3576
3577
0
  smartcard_trace_locate_cards_by_atr_w_call(log, call);
3578
0
  return SCARD_S_SUCCESS;
3579
0
}
3580
3581
LONG smartcard_unpack_read_cache_a_call(wStream* s, ReadCacheA_Call* call)
3582
0
{
3583
0
  UINT32 mszNdrPtr = 0;
3584
0
  UINT32 contextNdrPtr = 0;
3585
0
  UINT32 index = 0;
3586
0
  UINT32 pbContextNdrPtr = 0;
3587
3588
0
  WINPR_ASSERT(call);
3589
0
  wLog* log = scard_log();
3590
3591
0
  if (!smartcard_ndr_pointer_read(log, s, &index, &mszNdrPtr))
3592
0
    return ERROR_INVALID_DATA;
3593
3594
0
  LONG status = smartcard_unpack_redir_scard_context(log, s, &(call->Common.handles.hContext),
3595
0
                                                     &index, &pbContextNdrPtr);
3596
0
  if (status != SCARD_S_SUCCESS)
3597
0
    return status;
3598
3599
0
  if (!smartcard_ndr_pointer_read(log, s, &index, &contextNdrPtr))
3600
0
    return ERROR_INVALID_DATA;
3601
3602
0
  if (!Stream_CheckAndLogRequiredLengthWLog(log, s, 12))
3603
0
    return STATUS_BUFFER_TOO_SMALL;
3604
0
  Stream_Read_UINT32(s, call->Common.FreshnessCounter);
3605
0
  Stream_Read_INT32(s, call->Common.fPbDataIsNULL);
3606
0
  Stream_Read_UINT32(s, call->Common.cbDataLen);
3607
3608
0
  call->szLookupName = NULL;
3609
0
  if (mszNdrPtr)
3610
0
  {
3611
0
    status = smartcard_ndr_read_a(log, s, &call->szLookupName, NDR_PTR_FULL);
3612
0
    if (status != SCARD_S_SUCCESS)
3613
0
      return status;
3614
0
  }
3615
3616
0
  status = smartcard_unpack_redir_scard_context_ref(log, s, pbContextNdrPtr,
3617
0
                                                    &call->Common.handles.hContext);
3618
0
  if (status != SCARD_S_SUCCESS)
3619
0
    return status;
3620
3621
0
  if (contextNdrPtr)
3622
0
  {
3623
0
    status = smartcard_ndr_read_u(log, s, &call->Common.CardIdentifier);
3624
0
    if (status != SCARD_S_SUCCESS)
3625
0
      return status;
3626
0
  }
3627
0
  smartcard_trace_read_cache_a_call(log, call);
3628
0
  return SCARD_S_SUCCESS;
3629
0
}
3630
3631
LONG smartcard_unpack_read_cache_w_call(wStream* s, ReadCacheW_Call* call)
3632
0
{
3633
0
  UINT32 mszNdrPtr = 0;
3634
0
  UINT32 contextNdrPtr = 0;
3635
0
  UINT32 index = 0;
3636
0
  UINT32 pbContextNdrPtr = 0;
3637
3638
0
  WINPR_ASSERT(call);
3639
0
  wLog* log = scard_log();
3640
3641
0
  if (!smartcard_ndr_pointer_read(log, s, &index, &mszNdrPtr))
3642
0
    return ERROR_INVALID_DATA;
3643
3644
0
  LONG status = smartcard_unpack_redir_scard_context(log, s, &(call->Common.handles.hContext),
3645
0
                                                     &index, &pbContextNdrPtr);
3646
0
  if (status != SCARD_S_SUCCESS)
3647
0
    return status;
3648
3649
0
  if (!smartcard_ndr_pointer_read(log, s, &index, &contextNdrPtr))
3650
0
    return ERROR_INVALID_DATA;
3651
3652
0
  if (!Stream_CheckAndLogRequiredLengthWLog(log, s, 12))
3653
0
    return STATUS_BUFFER_TOO_SMALL;
3654
0
  Stream_Read_UINT32(s, call->Common.FreshnessCounter);
3655
0
  Stream_Read_INT32(s, call->Common.fPbDataIsNULL);
3656
0
  Stream_Read_UINT32(s, call->Common.cbDataLen);
3657
3658
0
  call->szLookupName = NULL;
3659
0
  if (mszNdrPtr)
3660
0
  {
3661
0
    status = smartcard_ndr_read_w(log, s, &call->szLookupName, NDR_PTR_FULL);
3662
0
    if (status != SCARD_S_SUCCESS)
3663
0
      return status;
3664
0
  }
3665
3666
0
  status = smartcard_unpack_redir_scard_context_ref(log, s, pbContextNdrPtr,
3667
0
                                                    &call->Common.handles.hContext);
3668
0
  if (status != SCARD_S_SUCCESS)
3669
0
    return status;
3670
3671
0
  if (contextNdrPtr)
3672
0
  {
3673
0
    status = smartcard_ndr_read_u(log, s, &call->Common.CardIdentifier);
3674
0
    if (status != SCARD_S_SUCCESS)
3675
0
      return status;
3676
0
  }
3677
0
  smartcard_trace_read_cache_w_call(log, call);
3678
0
  return SCARD_S_SUCCESS;
3679
0
}
3680
3681
LONG smartcard_unpack_write_cache_a_call(wStream* s, WriteCacheA_Call* call)
3682
0
{
3683
0
  UINT32 mszNdrPtr = 0;
3684
0
  UINT32 contextNdrPtr = 0;
3685
0
  UINT32 pbDataNdrPtr = 0;
3686
0
  UINT32 index = 0;
3687
0
  UINT32 pbContextNdrPtr = 0;
3688
3689
0
  WINPR_ASSERT(call);
3690
0
  wLog* log = scard_log();
3691
3692
0
  if (!smartcard_ndr_pointer_read(log, s, &index, &mszNdrPtr))
3693
0
    return ERROR_INVALID_DATA;
3694
3695
0
  LONG status = smartcard_unpack_redir_scard_context(log, s, &(call->Common.handles.hContext),
3696
0
                                                     &index, &pbContextNdrPtr);
3697
0
  if (status != SCARD_S_SUCCESS)
3698
0
    return status;
3699
3700
0
  if (!smartcard_ndr_pointer_read(log, s, &index, &contextNdrPtr))
3701
0
    return ERROR_INVALID_DATA;
3702
3703
0
  if (!Stream_CheckAndLogRequiredLengthWLog(log, s, 8))
3704
0
    return STATUS_BUFFER_TOO_SMALL;
3705
3706
0
  Stream_Read_UINT32(s, call->Common.FreshnessCounter);
3707
0
  Stream_Read_UINT32(s, call->Common.cbDataLen);
3708
3709
0
  if (!smartcard_ndr_pointer_read(log, s, &index, &pbDataNdrPtr))
3710
0
    return ERROR_INVALID_DATA;
3711
3712
0
  call->szLookupName = NULL;
3713
0
  if (mszNdrPtr)
3714
0
  {
3715
0
    status = smartcard_ndr_read_a(log, s, &call->szLookupName, NDR_PTR_FULL);
3716
0
    if (status != SCARD_S_SUCCESS)
3717
0
      return status;
3718
0
  }
3719
3720
0
  status = smartcard_unpack_redir_scard_context_ref(log, s, pbContextNdrPtr,
3721
0
                                                    &call->Common.handles.hContext);
3722
0
  if (status != SCARD_S_SUCCESS)
3723
0
    return status;
3724
3725
0
  call->Common.CardIdentifier = NULL;
3726
0
  if (contextNdrPtr)
3727
0
  {
3728
0
    status = smartcard_ndr_read_u(log, s, &call->Common.CardIdentifier);
3729
0
    if (status != SCARD_S_SUCCESS)
3730
0
      return status;
3731
0
  }
3732
3733
0
  call->Common.pbData = NULL;
3734
0
  if (pbDataNdrPtr)
3735
0
  {
3736
0
    status = smartcard_ndr_read(log, s, &call->Common.pbData, call->Common.cbDataLen, 1,
3737
0
                                NDR_PTR_SIMPLE);
3738
0
    if (status != SCARD_S_SUCCESS)
3739
0
      return status;
3740
0
  }
3741
0
  smartcard_trace_write_cache_a_call(log, call);
3742
0
  return SCARD_S_SUCCESS;
3743
0
}
3744
3745
LONG smartcard_unpack_write_cache_w_call(wStream* s, WriteCacheW_Call* call)
3746
0
{
3747
0
  UINT32 mszNdrPtr = 0;
3748
0
  UINT32 contextNdrPtr = 0;
3749
0
  UINT32 pbDataNdrPtr = 0;
3750
0
  UINT32 index = 0;
3751
0
  UINT32 pbContextNdrPtr = 0;
3752
3753
0
  WINPR_ASSERT(call);
3754
0
  wLog* log = scard_log();
3755
3756
0
  if (!smartcard_ndr_pointer_read(log, s, &index, &mszNdrPtr))
3757
0
    return ERROR_INVALID_DATA;
3758
3759
0
  LONG status = smartcard_unpack_redir_scard_context(log, s, &(call->Common.handles.hContext),
3760
0
                                                     &index, &pbContextNdrPtr);
3761
0
  if (status != SCARD_S_SUCCESS)
3762
0
    return status;
3763
3764
0
  if (!smartcard_ndr_pointer_read(log, s, &index, &contextNdrPtr))
3765
0
    return ERROR_INVALID_DATA;
3766
3767
0
  if (!Stream_CheckAndLogRequiredLengthWLog(log, s, 8))
3768
0
    return STATUS_BUFFER_TOO_SMALL;
3769
0
  Stream_Read_UINT32(s, call->Common.FreshnessCounter);
3770
0
  Stream_Read_UINT32(s, call->Common.cbDataLen);
3771
3772
0
  if (!smartcard_ndr_pointer_read(log, s, &index, &pbDataNdrPtr))
3773
0
    return ERROR_INVALID_DATA;
3774
3775
0
  call->szLookupName = NULL;
3776
0
  if (mszNdrPtr)
3777
0
  {
3778
0
    status = smartcard_ndr_read_w(log, s, &call->szLookupName, NDR_PTR_FULL);
3779
0
    if (status != SCARD_S_SUCCESS)
3780
0
      return status;
3781
0
  }
3782
3783
0
  status = smartcard_unpack_redir_scard_context_ref(log, s, pbContextNdrPtr,
3784
0
                                                    &call->Common.handles.hContext);
3785
0
  if (status != SCARD_S_SUCCESS)
3786
0
    return status;
3787
3788
0
  call->Common.CardIdentifier = NULL;
3789
0
  if (contextNdrPtr)
3790
0
  {
3791
0
    status = smartcard_ndr_read_u(log, s, &call->Common.CardIdentifier);
3792
0
    if (status != SCARD_S_SUCCESS)
3793
0
      return status;
3794
0
  }
3795
3796
0
  call->Common.pbData = NULL;
3797
0
  if (pbDataNdrPtr)
3798
0
  {
3799
0
    status = smartcard_ndr_read(log, s, &call->Common.pbData, call->Common.cbDataLen, 1,
3800
0
                                NDR_PTR_SIMPLE);
3801
0
    if (status != SCARD_S_SUCCESS)
3802
0
      return status;
3803
0
  }
3804
0
  smartcard_trace_write_cache_w_call(log, call);
3805
0
  return status;
3806
0
}
3807
3808
LONG smartcard_unpack_get_transmit_count_call(wStream* s, GetTransmitCount_Call* call)
3809
0
{
3810
0
  WINPR_ASSERT(call);
3811
0
  wLog* log = scard_log();
3812
3813
0
  UINT32 index = 0;
3814
0
  UINT32 pbContextNdrPtr = 0;
3815
3816
0
  LONG status = smartcard_unpack_redir_scard_context(log, s, &(call->handles.hContext), &index,
3817
0
                                                     &pbContextNdrPtr);
3818
0
  if (status != SCARD_S_SUCCESS)
3819
0
    return status;
3820
3821
0
  status = smartcard_unpack_redir_scard_handle(log, s, &(call->handles.hCard), &index);
3822
0
  if (status != SCARD_S_SUCCESS)
3823
0
    return status;
3824
3825
0
  status = smartcard_unpack_redir_scard_context_ref(log, s, pbContextNdrPtr,
3826
0
                                                    &(call->handles.hContext));
3827
0
  if (status != SCARD_S_SUCCESS)
3828
0
  {
3829
0
    WLog_Print(log, WLOG_ERROR,
3830
0
               "smartcard_unpack_redir_scard_context_ref failed with error %" PRId32 "",
3831
0
               status);
3832
0
    return status;
3833
0
  }
3834
3835
0
  status = smartcard_unpack_redir_scard_handle_ref(log, s, &(call->handles.hCard));
3836
0
  if (status != SCARD_S_SUCCESS)
3837
0
    WLog_Print(log, WLOG_ERROR,
3838
0
               "smartcard_unpack_redir_scard_handle_ref failed with error %" PRId32 "", status);
3839
3840
0
  smartcard_trace_get_transmit_count_call(log, call);
3841
0
  return status;
3842
0
}
3843
3844
LONG smartcard_unpack_get_reader_icon_call(wStream* s, GetReaderIcon_Call* call)
3845
0
{
3846
0
  WINPR_ASSERT(call);
3847
0
  wLog* log = scard_log();
3848
0
  return smartcard_unpack_common_context_and_string_w(log, s, &call->handles.hContext,
3849
0
                                                      &call->szReaderName);
3850
0
}
3851
3852
LONG smartcard_unpack_context_and_string_a_call(wStream* s, ContextAndStringA_Call* call)
3853
0
{
3854
0
  WINPR_ASSERT(call);
3855
0
  wLog* log = scard_log();
3856
0
  return smartcard_unpack_common_context_and_string_a(log, s, &call->handles.hContext, &call->sz);
3857
0
}
3858
3859
LONG smartcard_unpack_context_and_string_w_call(wStream* s, ContextAndStringW_Call* call)
3860
0
{
3861
0
  WINPR_ASSERT(call);
3862
0
  wLog* log = scard_log();
3863
0
  return smartcard_unpack_common_context_and_string_w(log, s, &call->handles.hContext, &call->sz);
3864
0
}
3865
3866
LONG smartcard_unpack_get_device_type_id_call(wStream* s, GetDeviceTypeId_Call* call)
3867
0
{
3868
0
  WINPR_ASSERT(call);
3869
0
  wLog* log = scard_log();
3870
0
  return smartcard_unpack_common_context_and_string_w(log, s, &call->handles.hContext,
3871
0
                                                      &call->szReaderName);
3872
0
}
3873
3874
LONG smartcard_pack_device_type_id_return(wStream* s, const GetDeviceTypeId_Return* ret)
3875
0
{
3876
0
  WINPR_ASSERT(ret);
3877
0
  wLog* log = scard_log();
3878
0
  smartcard_trace_device_type_id_return(log, ret);
3879
3880
0
  if (!Stream_EnsureRemainingCapacity(s, 4))
3881
0
  {
3882
0
    WLog_Print(log, WLOG_ERROR, "Stream_EnsureRemainingCapacity failed!");
3883
0
    return SCARD_F_INTERNAL_ERROR;
3884
0
  }
3885
3886
0
  Stream_Write_UINT32(s, ret->dwDeviceId); /* cBytes (4 bytes) */
3887
3888
0
  return ret->ReturnCode;
3889
0
}
3890
3891
LONG smartcard_pack_locate_cards_return(wStream* s, const LocateCards_Return* ret)
3892
0
{
3893
0
  WINPR_ASSERT(ret);
3894
0
  wLog* log = scard_log();
3895
3896
0
  LONG status = 0;
3897
0
  DWORD cbDataLen = ret->cReaders;
3898
0
  UINT32 index = 0;
3899
3900
0
  smartcard_trace_locate_cards_return(log, ret);
3901
0
  if (ret->ReturnCode != SCARD_S_SUCCESS)
3902
0
    cbDataLen = 0;
3903
0
  if (cbDataLen == SCARD_AUTOALLOCATE)
3904
0
    cbDataLen = 0;
3905
3906
0
  if (!Stream_EnsureRemainingCapacity(s, 4))
3907
0
  {
3908
0
    WLog_Print(log, WLOG_ERROR, "Stream_EnsureRemainingCapacity failed!");
3909
0
    return SCARD_F_INTERNAL_ERROR;
3910
0
  }
3911
3912
0
  Stream_Write_UINT32(s, cbDataLen); /* cBytes (4 cbDataLen) */
3913
0
  if (!smartcard_ndr_pointer_write(s, &index, cbDataLen))
3914
0
    return SCARD_E_NO_MEMORY;
3915
3916
0
  status = smartcard_ndr_write_state(s, ret->rgReaderStates, cbDataLen, NDR_PTR_SIMPLE);
3917
0
  if (status != SCARD_S_SUCCESS)
3918
0
    return status;
3919
0
  return ret->ReturnCode;
3920
0
}
3921
3922
LONG smartcard_pack_get_reader_icon_return(wStream* s, const GetReaderIcon_Return* ret)
3923
0
{
3924
0
  WINPR_ASSERT(ret);
3925
0
  wLog* log = scard_log();
3926
3927
0
  LONG status = 0;
3928
0
  UINT32 index = 0;
3929
0
  DWORD cbDataLen = ret->cbDataLen;
3930
0
  smartcard_trace_get_reader_icon_return(log, ret);
3931
0
  if (ret->ReturnCode != SCARD_S_SUCCESS)
3932
0
    cbDataLen = 0;
3933
0
  if (cbDataLen == SCARD_AUTOALLOCATE)
3934
0
    cbDataLen = 0;
3935
3936
0
  if (!Stream_EnsureRemainingCapacity(s, 4))
3937
0
  {
3938
0
    WLog_Print(log, WLOG_ERROR, "Stream_EnsureRemainingCapacity failed!");
3939
0
    return SCARD_F_INTERNAL_ERROR;
3940
0
  }
3941
3942
0
  Stream_Write_UINT32(s, cbDataLen); /* cBytes (4 cbDataLen) */
3943
0
  if (!smartcard_ndr_pointer_write(s, &index, cbDataLen))
3944
0
    return SCARD_E_NO_MEMORY;
3945
3946
0
  status = smartcard_ndr_write(s, ret->pbData, cbDataLen, 1, NDR_PTR_SIMPLE);
3947
0
  if (status != SCARD_S_SUCCESS)
3948
0
    return status;
3949
0
  return ret->ReturnCode;
3950
0
}
3951
3952
LONG smartcard_pack_get_transmit_count_return(wStream* s, const GetTransmitCount_Return* ret)
3953
0
{
3954
0
  WINPR_ASSERT(ret);
3955
0
  wLog* log = scard_log();
3956
3957
0
  smartcard_trace_get_transmit_count_return(log, ret);
3958
3959
0
  if (!Stream_EnsureRemainingCapacity(s, 4))
3960
0
  {
3961
0
    WLog_Print(log, WLOG_ERROR, "Stream_EnsureRemainingCapacity failed!");
3962
0
    return SCARD_F_INTERNAL_ERROR;
3963
0
  }
3964
3965
0
  Stream_Write_UINT32(s, ret->cTransmitCount); /* cBytes (4 cbDataLen) */
3966
3967
0
  return ret->ReturnCode;
3968
0
}
3969
3970
LONG smartcard_pack_read_cache_return(wStream* s, const ReadCache_Return* ret)
3971
0
{
3972
0
  WINPR_ASSERT(ret);
3973
0
  wLog* log = scard_log();
3974
3975
0
  LONG status = 0;
3976
0
  UINT32 index = 0;
3977
0
  DWORD cbDataLen = ret->cbDataLen;
3978
0
  smartcard_trace_read_cache_return(log, ret);
3979
0
  if (ret->ReturnCode != SCARD_S_SUCCESS)
3980
0
    cbDataLen = 0;
3981
3982
0
  if (cbDataLen == SCARD_AUTOALLOCATE)
3983
0
    cbDataLen = 0;
3984
3985
0
  if (!Stream_EnsureRemainingCapacity(s, 4))
3986
0
  {
3987
0
    WLog_Print(log, WLOG_ERROR, "Stream_EnsureRemainingCapacity failed!");
3988
0
    return SCARD_F_INTERNAL_ERROR;
3989
0
  }
3990
3991
0
  Stream_Write_UINT32(s, cbDataLen); /* cBytes (4 cbDataLen) */
3992
0
  if (!smartcard_ndr_pointer_write(s, &index, cbDataLen))
3993
0
    return SCARD_E_NO_MEMORY;
3994
3995
0
  status = smartcard_ndr_write(s, ret->pbData, cbDataLen, 1, NDR_PTR_SIMPLE);
3996
0
  if (status != SCARD_S_SUCCESS)
3997
0
    return status;
3998
0
  return ret->ReturnCode;
3999
0
}