Coverage Report

Created: 2025-07-01 06:46

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