Coverage Report

Created: 2026-07-16 07:09

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