Coverage Report

Created: 2026-11-10 06:40

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