Coverage Report

Created: 2026-03-04 06:17

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/FreeRDP/libfreerdp/utils/smartcard_call.c
Line
Count
Source
1
/**
2
 * FreeRDP: A Remote Desktop Protocol Implementation
3
 * Smartcard Device Service Virtual Channel
4
 *
5
 * Copyright (C) Alexi Volkov <alexi@myrealbox.com> 2006
6
 * Copyright 2011 O.S. Systems Software Ltda.
7
 * Copyright 2011 Anthony Tong <atong@trustedcs.com>
8
 * Copyright 2015 Thincast Technologies GmbH
9
 * Copyright 2015 DI (FH) Martin Haimberger <martin.haimberger@thincast.com>
10
 * Copyright 2017 Armin Novak <armin.novak@thincast.com>
11
 * Copyright 2017 Thincast Technologies GmbH
12
 *
13
 * Licensed under the Apache License, Version 2.0 (the "License");
14
 * you may not use this file except in compliance with the License.
15
 * You may obtain a copy of the License at
16
 *
17
 *     http://www.apache.org/licenses/LICENSE-2.0
18
 *
19
 * Unless required by applicable law or agreed to in writing, software
20
 * distributed under the License is distributed on an "AS IS" BASIS,
21
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
22
 * See the License for the specific language governing permissions and
23
 * limitations under the License.
24
 */
25
26
#include <freerdp/config.h>
27
28
#include <winpr/assert.h>
29
30
#include <winpr/crt.h>
31
#include <winpr/print.h>
32
#include <winpr/stream.h>
33
#include <winpr/library.h>
34
#include <winpr/smartcard.h>
35
36
#include <freerdp/freerdp.h>
37
#include <freerdp/channels/rdpdr.h>
38
#include <freerdp/channels/scard.h>
39
40
#include <freerdp/utils/rdpdr_utils.h>
41
#include <freerdp/utils/smartcard_pack.h>
42
#include <freerdp/utils/smartcard_call.h>
43
44
#include "smartcard_pack.h"
45
46
#include <freerdp/log.h>
47
0
#define SCARD_TAG FREERDP_TAG("utils.smartcard.call")
48
49
#if defined(WITH_SMARTCARD_EMULATE)
50
#include <freerdp/emulate/scard/smartcard_emulate.h>
51
52
#define wrap_raw(ctx, fkt, ...)                                         \
53
0
  ctx->useEmulatedCard ? Emulate_##fkt(ctx->emulation, ##__VA_ARGS__) \
54
0
                       : ctx->pWinSCardApi->pfn##fkt(__VA_ARGS__)
55
0
#define wrap_ptr(ctx, fkt, ...) wrap_raw(ctx, fkt, ##__VA_ARGS__)
56
#else
57
#define wrap_raw(ctx, fkt, ...) \
58
  ctx->useEmulatedCard ? SCARD_F_INTERNAL_ERROR : ctx->pWinSCardApi->pfn##fkt(__VA_ARGS__)
59
#define wrap_ptr(ctx, fkt, ...) \
60
  ctx->useEmulatedCard ? nullptr : ctx->pWinSCardApi->pfn##fkt(__VA_ARGS__)
61
#endif
62
63
#if defined(_WIN32)
64
#define wrap(ctx, fkt, ...) wrap_raw(ctx, fkt, ##__VA_ARGS__)
65
#else
66
#define wrap(ctx, fkt, ...)                                               \
67
0
  __extension__({                                                       \
68
0
    LONG defstatus = wrap_raw(ctx, fkt, ##__VA_ARGS__);               \
69
0
    if (defstatus != SCARD_S_SUCCESS)                                 \
70
0
      WLog_Print(ctx->log, WLOG_TRACE, "[" #fkt "] failed with %s", \
71
0
                 SCardGetErrorString(defstatus));                   \
72
0
    defstatus;                                                        \
73
0
  })
74
#endif
75
76
struct s_scard_call_context
77
{
78
  BOOL useEmulatedCard;
79
  HANDLE StartedEvent;
80
  wLinkedList* names;
81
  wHashTable* rgSCardContextList;
82
#if defined(WITH_SMARTCARD_EMULATE)
83
  SmartcardEmulationContext* emulation;
84
#endif
85
  HANDLE hWinSCardLibrary;
86
  SCardApiFunctionTable WinSCardApi;
87
  const SCardApiFunctionTable* pWinSCardApi;
88
  HANDLE stopEvent;
89
  void* userdata;
90
91
  void* (*fn_new)(void*, SCARDCONTEXT);
92
  void (*fn_free)(void*);
93
  wLog* log;
94
};
95
96
struct s_scard_context_element
97
{
98
  void* context;
99
  void (*fn_free)(void*);
100
};
101
102
static void context_free(void* arg);
103
104
static LONG smartcard_EstablishContext_Call(scard_call_context* smartcard, wStream* out,
105
                                            SMARTCARD_OPERATION* operation)
106
0
{
107
0
  LONG status = 0;
108
0
  SCARDCONTEXT hContext = WINPR_C_ARRAY_INIT;
109
0
  EstablishContext_Return ret = WINPR_C_ARRAY_INIT;
110
0
  EstablishContext_Call* call = &operation->call.establishContext;
111
0
  status = ret.ReturnCode =
112
0
      wrap(smartcard, SCardEstablishContext, call->dwScope, nullptr, nullptr, &hContext);
113
114
0
  if (ret.ReturnCode == SCARD_S_SUCCESS)
115
0
  {
116
0
    const void* key = (void*)(size_t)hContext;
117
0
    struct s_scard_context_element* pContext =
118
0
        calloc(1, sizeof(struct s_scard_context_element));
119
0
    if (!pContext)
120
0
      return STATUS_NO_MEMORY;
121
122
0
    pContext->fn_free = smartcard->fn_free;
123
124
0
    if (smartcard->fn_new)
125
0
    {
126
0
      pContext->context = smartcard->fn_new(smartcard->userdata, hContext);
127
0
      if (!pContext->context)
128
0
      {
129
0
        free(pContext);
130
0
        return STATUS_NO_MEMORY;
131
0
      }
132
0
    }
133
134
0
    if (!HashTable_Insert(smartcard->rgSCardContextList, key, (void*)pContext))
135
0
    {
136
0
      WLog_Print(smartcard->log, WLOG_ERROR, "HashTable_Insert failed!");
137
0
      context_free(pContext);
138
0
      return STATUS_INTERNAL_ERROR;
139
0
    }
140
0
  }
141
0
  else
142
0
  {
143
0
    return scard_log_status_error_wlog(smartcard->log, "SCardEstablishContext", status);
144
0
  }
145
146
  // NOLINTNEXTLINE(clang-analyzer-unix.Malloc): HashTable_Insert takes ownership of pContext
147
0
  smartcard_scard_context_native_to_redir(&(ret.hContext), hContext);
148
149
0
  status = smartcard_pack_establish_context_return(out, &ret);
150
0
  if (status != SCARD_S_SUCCESS)
151
0
  {
152
0
    return scard_log_status_error_wlog(smartcard->log,
153
0
                                       "smartcard_pack_establish_context_return", status);
154
0
  }
155
156
0
  return ret.ReturnCode;
157
0
}
158
159
static LONG smartcard_ReleaseContext_Call(scard_call_context* smartcard,
160
                                          WINPR_ATTR_UNUSED wStream* out,
161
                                          SMARTCARD_OPERATION* operation)
162
0
{
163
0
  Long_Return ret = WINPR_C_ARRAY_INIT;
164
165
0
  WINPR_ASSERT(smartcard);
166
0
  WINPR_ASSERT(out);
167
0
  WINPR_ASSERT(operation);
168
169
0
  ret.ReturnCode = wrap(smartcard, SCardReleaseContext, operation->hContext);
170
171
0
  if (ret.ReturnCode == SCARD_S_SUCCESS)
172
0
    HashTable_Remove(smartcard->rgSCardContextList, (void*)operation->hContext);
173
0
  else
174
0
  {
175
0
    return scard_log_status_error_wlog(smartcard->log, "SCardReleaseContext", ret.ReturnCode);
176
0
  }
177
178
0
  smartcard_trace_long_return_int(smartcard->log, &ret, "ReleaseContext");
179
0
  return ret.ReturnCode;
180
0
}
181
182
static LONG smartcard_IsValidContext_Call(scard_call_context* smartcard,
183
                                          WINPR_ATTR_UNUSED wStream* out,
184
                                          SMARTCARD_OPERATION* operation)
185
0
{
186
0
  Long_Return ret = WINPR_C_ARRAY_INIT;
187
188
0
  WINPR_ASSERT(smartcard);
189
0
  WINPR_ASSERT(out);
190
0
  WINPR_ASSERT(operation);
191
192
0
  ret.ReturnCode = wrap(smartcard, SCardIsValidContext, operation->hContext);
193
0
  smartcard_trace_long_return_int(smartcard->log, &ret, "IsValidContext");
194
0
  return ret.ReturnCode;
195
0
}
196
197
static LONG smartcard_ListReaderGroupsA_Call(scard_call_context* smartcard, wStream* out,
198
                                             SMARTCARD_OPERATION* operation)
199
0
{
200
0
  LONG status = 0;
201
0
  ListReaderGroups_Return ret = WINPR_C_ARRAY_INIT;
202
0
  LPSTR mszGroups = nullptr;
203
0
  DWORD cchGroups = 0;
204
205
0
  WINPR_ASSERT(smartcard);
206
0
  WINPR_ASSERT(out);
207
0
  WINPR_ASSERT(operation);
208
209
0
  cchGroups = SCARD_AUTOALLOCATE;
210
0
  ret.ReturnCode =
211
0
      wrap(smartcard, SCardListReaderGroupsA, operation->hContext, (LPSTR)&mszGroups, &cchGroups);
212
0
  if ((ret.ReturnCode == SCARD_S_SUCCESS) && (cchGroups == SCARD_AUTOALLOCATE))
213
0
    return SCARD_F_UNKNOWN_ERROR;
214
215
0
  ret.msz = (BYTE*)mszGroups;
216
0
  ret.cBytes = cchGroups;
217
218
0
  status = smartcard_pack_list_reader_groups_return(out, &ret, FALSE);
219
220
0
  if (status != SCARD_S_SUCCESS)
221
0
    return status;
222
223
0
  if (mszGroups)
224
0
    wrap(smartcard, SCardFreeMemory, operation->hContext, mszGroups);
225
226
0
  return ret.ReturnCode;
227
0
}
228
229
static LONG smartcard_ListReaderGroupsW_Call(scard_call_context* smartcard, wStream* out,
230
                                             SMARTCARD_OPERATION* operation)
231
0
{
232
0
  LONG status = 0;
233
0
  ListReaderGroups_Return ret = WINPR_C_ARRAY_INIT;
234
0
  LPWSTR mszGroups = nullptr;
235
0
  DWORD cchGroups = 0;
236
237
0
  WINPR_ASSERT(smartcard);
238
0
  WINPR_ASSERT(out);
239
0
  WINPR_ASSERT(operation);
240
241
0
  cchGroups = SCARD_AUTOALLOCATE;
242
0
  status = ret.ReturnCode = wrap(smartcard, SCardListReaderGroupsW, operation->hContext,
243
0
                                 (LPWSTR)&mszGroups, &cchGroups);
244
0
  if ((ret.ReturnCode == SCARD_S_SUCCESS) && (cchGroups == SCARD_AUTOALLOCATE))
245
0
    return SCARD_F_UNKNOWN_ERROR;
246
247
0
  ret.msz = (BYTE*)mszGroups;
248
249
0
  WINPR_ASSERT(cchGroups < SCARD_AUTOALLOCATE / sizeof(WCHAR));
250
0
  const size_t blen = sizeof(WCHAR) * cchGroups;
251
0
  WINPR_ASSERT(blen <= UINT32_MAX);
252
0
  ret.cBytes = (UINT32)blen;
253
254
0
  if (status != SCARD_S_SUCCESS)
255
0
    return status;
256
257
0
  status = smartcard_pack_list_reader_groups_return(out, &ret, TRUE);
258
259
0
  if (status != SCARD_S_SUCCESS)
260
0
    return status;
261
262
0
  if (mszGroups)
263
0
    wrap(smartcard, SCardFreeMemory, operation->hContext, mszGroups);
264
265
0
  return ret.ReturnCode;
266
0
}
267
268
static BOOL filter_match(wLinkedList* list, LPCSTR reader, size_t readerLen)
269
0
{
270
0
  if (readerLen < 1)
271
0
    return FALSE;
272
273
0
  LinkedList_Enumerator_Reset(list);
274
275
0
  while (LinkedList_Enumerator_MoveNext(list))
276
0
  {
277
0
    const char* filter = LinkedList_Enumerator_Current(list);
278
279
0
    if (filter)
280
0
    {
281
0
      if (strstr(reader, filter) != nullptr)
282
0
        return TRUE;
283
0
    }
284
0
  }
285
286
0
  return FALSE;
287
0
}
288
289
static DWORD filter_device_by_name_a(wLinkedList* list, LPSTR* mszReaders, DWORD cchReaders)
290
0
{
291
0
  size_t rpos = 0;
292
0
  size_t wpos = 0;
293
294
0
  if (*mszReaders == nullptr || LinkedList_Count(list) < 1)
295
0
    return cchReaders;
296
297
0
  do
298
0
  {
299
0
    LPCSTR rreader = &(*mszReaders)[rpos];
300
0
    LPSTR wreader = &(*mszReaders)[wpos];
301
0
    size_t readerLen = strnlen(rreader, cchReaders - rpos);
302
303
0
    rpos += readerLen + 1;
304
305
0
    if (filter_match(list, rreader, readerLen))
306
0
    {
307
0
      if (rreader != wreader)
308
0
        memmove(wreader, rreader, readerLen + 1);
309
310
0
      wpos += readerLen + 1;
311
0
    }
312
0
  } while (rpos < cchReaders);
313
314
  /* this string must be double 0 terminated */
315
0
  if (rpos != wpos)
316
0
  {
317
0
    if (wpos >= cchReaders)
318
0
      return 0;
319
320
0
    (*mszReaders)[wpos++] = '\0';
321
0
  }
322
323
0
  return (DWORD)wpos;
324
0
}
325
326
static DWORD filter_device_by_name_w(wLinkedList* list, LPWSTR* mszReaders, DWORD cchReaders)
327
0
{
328
0
  DWORD rc = 0;
329
0
  LPSTR readers = nullptr;
330
331
0
  if (LinkedList_Count(list) < 1)
332
0
    return cchReaders;
333
334
0
  readers = ConvertMszWCharNToUtf8Alloc(*mszReaders, cchReaders, nullptr);
335
336
0
  if (!readers)
337
0
  {
338
0
    free(readers);
339
0
    return 0;
340
0
  }
341
342
0
  free(*mszReaders);
343
0
  *mszReaders = nullptr;
344
0
  rc = filter_device_by_name_a(list, &readers, cchReaders);
345
346
0
  *mszReaders = ConvertMszUtf8NToWCharAlloc(readers, rc, nullptr);
347
0
  if (!*mszReaders)
348
0
    rc = 0;
349
350
0
  free(readers);
351
0
  return rc;
352
0
}
353
354
static LONG smartcard_ListReadersA_Call(scard_call_context* smartcard, wStream* out,
355
                                        SMARTCARD_OPERATION* operation)
356
0
{
357
0
  ListReaders_Return ret = WINPR_C_ARRAY_INIT;
358
0
  LPSTR mszReaders = nullptr;
359
360
0
  WINPR_ASSERT(smartcard);
361
0
  WINPR_ASSERT(out);
362
0
  WINPR_ASSERT(operation);
363
364
0
  ListReaders_Call* call = &operation->call.listReaders;
365
0
  DWORD cchReaders = SCARD_AUTOALLOCATE;
366
0
  LONG status = ret.ReturnCode = wrap(smartcard, SCardListReadersA, operation->hContext,
367
0
                                      (LPCSTR)call->mszGroups, (LPSTR)&mszReaders, &cchReaders);
368
0
  if (status == SCARD_S_SUCCESS)
369
0
  {
370
0
    if (cchReaders == SCARD_AUTOALLOCATE)
371
0
      status = SCARD_F_UNKNOWN_ERROR;
372
0
  }
373
374
0
  if (status != SCARD_S_SUCCESS)
375
0
  {
376
0
    (void)scard_log_status_error_wlog(smartcard->log, "SCardListReadersA", status);
377
0
    return smartcard_pack_list_readers_return(out, &ret, FALSE);
378
0
  }
379
380
0
  cchReaders = filter_device_by_name_a(smartcard->names, &mszReaders, cchReaders);
381
0
  ret.msz = (BYTE*)mszReaders;
382
0
  ret.cBytes = cchReaders;
383
384
0
  status = smartcard_pack_list_readers_return(out, &ret, FALSE);
385
0
  if (mszReaders)
386
0
    wrap(smartcard, SCardFreeMemory, operation->hContext, mszReaders);
387
388
0
  if (status != SCARD_S_SUCCESS)
389
0
    return scard_log_status_error_wlog(smartcard->log, "smartcard_pack_list_readers_return",
390
0
                                       status);
391
392
0
  return ret.ReturnCode;
393
0
}
394
395
static LONG smartcard_ListReadersW_Call(scard_call_context* smartcard, wStream* out,
396
                                        SMARTCARD_OPERATION* operation)
397
0
{
398
0
  LONG status = 0;
399
0
  ListReaders_Return ret = WINPR_C_ARRAY_INIT;
400
0
  DWORD cchReaders = 0;
401
0
  ListReaders_Call* call = nullptr;
402
0
  union
403
0
  {
404
0
    const BYTE* bp;
405
0
    const char* sz;
406
0
    const WCHAR* wz;
407
0
  } string;
408
0
  union
409
0
  {
410
0
    WCHAR** ppw;
411
0
    WCHAR* pw;
412
0
    CHAR* pc;
413
0
    BYTE* pb;
414
0
  } mszReaders;
415
416
0
  WINPR_ASSERT(smartcard);
417
0
  WINPR_ASSERT(operation);
418
419
0
  call = &operation->call.listReaders;
420
421
0
  string.bp = call->mszGroups;
422
0
  cchReaders = SCARD_AUTOALLOCATE;
423
0
  status = ret.ReturnCode = wrap(smartcard, SCardListReadersW, operation->hContext, string.wz,
424
0
                                 (LPWSTR)&mszReaders.pw, &cchReaders);
425
0
  if (status == SCARD_S_SUCCESS)
426
0
  {
427
0
    if (cchReaders == SCARD_AUTOALLOCATE)
428
0
      status = SCARD_F_UNKNOWN_ERROR;
429
0
  }
430
431
0
  if (status != SCARD_S_SUCCESS)
432
0
  {
433
0
    (void)scard_log_status_error_wlog(smartcard->log, "SCardListReadersW", status);
434
0
    return smartcard_pack_list_readers_return(out, &ret, TRUE);
435
0
  }
436
437
0
  cchReaders = filter_device_by_name_w(smartcard->names, &mszReaders.pw, cchReaders);
438
0
  ret.msz = mszReaders.pb;
439
0
  ret.cBytes = cchReaders * sizeof(WCHAR);
440
0
  status = smartcard_pack_list_readers_return(out, &ret, TRUE);
441
442
0
  if (mszReaders.pb)
443
0
    wrap(smartcard, SCardFreeMemory, operation->hContext, mszReaders.pb);
444
445
0
  if (status != SCARD_S_SUCCESS)
446
0
    return status;
447
448
0
  return ret.ReturnCode;
449
0
}
450
451
static LONG smartcard_IntroduceReaderGroupA_Call(scard_call_context* smartcard,
452
                                                 WINPR_ATTR_UNUSED wStream* out,
453
                                                 SMARTCARD_OPERATION* operation)
454
0
{
455
0
  Long_Return ret = WINPR_C_ARRAY_INIT;
456
0
  ContextAndStringA_Call* call = nullptr;
457
458
0
  WINPR_ASSERT(smartcard);
459
0
  WINPR_ASSERT(out);
460
0
  WINPR_ASSERT(operation);
461
462
0
  call = &operation->call.contextAndStringA;
463
0
  ret.ReturnCode = wrap(smartcard, SCardIntroduceReaderGroupA, operation->hContext, call->sz);
464
0
  scard_log_status_error_wlog(smartcard->log, "SCardIntroduceReaderGroupA", ret.ReturnCode);
465
0
  smartcard_trace_long_return_int(smartcard->log, &ret, "IntroduceReaderGroupA");
466
0
  return ret.ReturnCode;
467
0
}
468
469
static LONG smartcard_IntroduceReaderGroupW_Call(scard_call_context* smartcard,
470
                                                 WINPR_ATTR_UNUSED wStream* out,
471
                                                 SMARTCARD_OPERATION* operation)
472
0
{
473
0
  Long_Return ret = WINPR_C_ARRAY_INIT;
474
0
  ContextAndStringW_Call* call = nullptr;
475
476
0
  WINPR_ASSERT(smartcard);
477
0
  WINPR_ASSERT(out);
478
0
  WINPR_ASSERT(operation);
479
480
0
  call = &operation->call.contextAndStringW;
481
0
  ret.ReturnCode = wrap(smartcard, SCardIntroduceReaderGroupW, operation->hContext, call->sz);
482
0
  scard_log_status_error_wlog(smartcard->log, "SCardIntroduceReaderGroupW", ret.ReturnCode);
483
0
  smartcard_trace_long_return_int(smartcard->log, &ret, "IntroduceReaderGroupW");
484
0
  return ret.ReturnCode;
485
0
}
486
487
static LONG smartcard_IntroduceReaderA_Call(scard_call_context* smartcard,
488
                                            WINPR_ATTR_UNUSED wStream* out,
489
                                            SMARTCARD_OPERATION* operation)
490
0
{
491
0
  Long_Return ret = WINPR_C_ARRAY_INIT;
492
0
  ContextAndTwoStringA_Call* call = nullptr;
493
494
0
  WINPR_ASSERT(smartcard);
495
0
  WINPR_ASSERT(out);
496
0
  WINPR_ASSERT(operation);
497
498
0
  call = &operation->call.contextAndTwoStringA;
499
0
  ret.ReturnCode =
500
0
      wrap(smartcard, SCardIntroduceReaderA, operation->hContext, call->sz1, call->sz2);
501
0
  scard_log_status_error_wlog(smartcard->log, "SCardIntroduceReaderA", ret.ReturnCode);
502
0
  smartcard_trace_long_return_int(smartcard->log, &ret, "IntroduceReaderA");
503
0
  return ret.ReturnCode;
504
0
}
505
506
static LONG smartcard_IntroduceReaderW_Call(scard_call_context* smartcard,
507
                                            WINPR_ATTR_UNUSED wStream* out,
508
                                            SMARTCARD_OPERATION* operation)
509
0
{
510
0
  Long_Return ret = WINPR_C_ARRAY_INIT;
511
0
  ContextAndTwoStringW_Call* call = nullptr;
512
513
0
  WINPR_ASSERT(smartcard);
514
0
  WINPR_ASSERT(out);
515
0
  WINPR_ASSERT(operation);
516
517
0
  call = &operation->call.contextAndTwoStringW;
518
0
  ret.ReturnCode =
519
0
      wrap(smartcard, SCardIntroduceReaderW, operation->hContext, call->sz1, call->sz2);
520
0
  scard_log_status_error_wlog(smartcard->log, "SCardIntroduceReaderW", ret.ReturnCode);
521
0
  smartcard_trace_long_return_int(smartcard->log, &ret, "IntroduceReaderW");
522
0
  return ret.ReturnCode;
523
0
}
524
525
static LONG smartcard_ForgetReaderA_Call(scard_call_context* smartcard,
526
                                         WINPR_ATTR_UNUSED wStream* out,
527
                                         SMARTCARD_OPERATION* operation)
528
0
{
529
0
  Long_Return ret = WINPR_C_ARRAY_INIT;
530
0
  ContextAndStringA_Call* call = nullptr;
531
532
0
  WINPR_ASSERT(smartcard);
533
0
  WINPR_ASSERT(out);
534
0
  WINPR_ASSERT(operation);
535
536
0
  call = &operation->call.contextAndStringA;
537
0
  ret.ReturnCode = wrap(smartcard, SCardForgetReaderA, operation->hContext, call->sz);
538
0
  scard_log_status_error_wlog(smartcard->log, "SCardForgetReaderA", ret.ReturnCode);
539
0
  smartcard_trace_long_return_int(smartcard->log, &ret, "SCardForgetReaderA");
540
0
  return ret.ReturnCode;
541
0
}
542
543
static LONG smartcard_ForgetReaderW_Call(scard_call_context* smartcard,
544
                                         WINPR_ATTR_UNUSED wStream* out,
545
                                         SMARTCARD_OPERATION* operation)
546
0
{
547
0
  Long_Return ret = WINPR_C_ARRAY_INIT;
548
0
  ContextAndStringW_Call* call = nullptr;
549
550
0
  WINPR_ASSERT(smartcard);
551
0
  WINPR_ASSERT(out);
552
0
  WINPR_ASSERT(operation);
553
554
0
  call = &operation->call.contextAndStringW;
555
0
  ret.ReturnCode = wrap(smartcard, SCardForgetReaderW, operation->hContext, call->sz);
556
0
  scard_log_status_error_wlog(smartcard->log, "SCardForgetReaderW", ret.ReturnCode);
557
0
  smartcard_trace_long_return_int(smartcard->log, &ret, "SCardForgetReaderW");
558
0
  return ret.ReturnCode;
559
0
}
560
561
static LONG smartcard_AddReaderToGroupA_Call(scard_call_context* smartcard,
562
                                             WINPR_ATTR_UNUSED wStream* out,
563
                                             SMARTCARD_OPERATION* operation)
564
0
{
565
0
  Long_Return ret = WINPR_C_ARRAY_INIT;
566
0
  ContextAndTwoStringA_Call* call = nullptr;
567
568
0
  WINPR_ASSERT(smartcard);
569
0
  WINPR_ASSERT(out);
570
0
  WINPR_ASSERT(operation);
571
572
0
  call = &operation->call.contextAndTwoStringA;
573
0
  ret.ReturnCode =
574
0
      wrap(smartcard, SCardAddReaderToGroupA, operation->hContext, call->sz1, call->sz2);
575
0
  scard_log_status_error_wlog(smartcard->log, "SCardAddReaderToGroupA", ret.ReturnCode);
576
0
  smartcard_trace_long_return_int(smartcard->log, &ret, "SCardAddReaderToGroupA");
577
0
  return ret.ReturnCode;
578
0
}
579
580
static LONG smartcard_AddReaderToGroupW_Call(scard_call_context* smartcard,
581
                                             WINPR_ATTR_UNUSED wStream* out,
582
                                             SMARTCARD_OPERATION* operation)
583
0
{
584
0
  Long_Return ret = WINPR_C_ARRAY_INIT;
585
0
  ContextAndTwoStringW_Call* call = nullptr;
586
587
0
  WINPR_ASSERT(smartcard);
588
0
  WINPR_ASSERT(out);
589
0
  WINPR_ASSERT(operation);
590
591
0
  call = &operation->call.contextAndTwoStringW;
592
0
  ret.ReturnCode =
593
0
      wrap(smartcard, SCardAddReaderToGroupW, operation->hContext, call->sz1, call->sz2);
594
0
  scard_log_status_error_wlog(smartcard->log, "SCardAddReaderToGroupW", ret.ReturnCode);
595
0
  smartcard_trace_long_return_int(smartcard->log, &ret, "SCardAddReaderToGroupA");
596
0
  return ret.ReturnCode;
597
0
}
598
599
static LONG smartcard_RemoveReaderFromGroupA_Call(scard_call_context* smartcard,
600
                                                  WINPR_ATTR_UNUSED wStream* out,
601
                                                  SMARTCARD_OPERATION* operation)
602
0
{
603
0
  Long_Return ret = WINPR_C_ARRAY_INIT;
604
0
  ContextAndTwoStringA_Call* call = nullptr;
605
606
0
  WINPR_ASSERT(smartcard);
607
0
  WINPR_ASSERT(out);
608
0
  WINPR_ASSERT(operation);
609
610
0
  call = &operation->call.contextAndTwoStringA;
611
0
  ret.ReturnCode =
612
0
      wrap(smartcard, SCardRemoveReaderFromGroupA, operation->hContext, call->sz1, call->sz2);
613
0
  scard_log_status_error_wlog(smartcard->log, "SCardRemoveReaderFromGroupA", ret.ReturnCode);
614
0
  smartcard_trace_long_return_int(smartcard->log, &ret, "SCardRemoveReaderFromGroupA");
615
0
  return ret.ReturnCode;
616
0
}
617
618
static LONG smartcard_RemoveReaderFromGroupW_Call(scard_call_context* smartcard,
619
                                                  WINPR_ATTR_UNUSED wStream* out,
620
                                                  SMARTCARD_OPERATION* operation)
621
0
{
622
0
  Long_Return ret = WINPR_C_ARRAY_INIT;
623
0
  ContextAndTwoStringW_Call* call = nullptr;
624
625
0
  WINPR_ASSERT(smartcard);
626
0
  WINPR_ASSERT(out);
627
0
  WINPR_ASSERT(operation);
628
629
0
  call = &operation->call.contextAndTwoStringW;
630
0
  ret.ReturnCode =
631
0
      wrap(smartcard, SCardRemoveReaderFromGroupW, operation->hContext, call->sz1, call->sz2);
632
0
  scard_log_status_error_wlog(smartcard->log, "SCardRemoveReaderFromGroupW", ret.ReturnCode);
633
0
  smartcard_trace_long_return_int(smartcard->log, &ret, "SCardRemoveReaderFromGroupW");
634
0
  return ret.ReturnCode;
635
0
}
636
637
static LONG smartcard_LocateCardsA_Call(scard_call_context* smartcard, wStream* out,
638
                                        SMARTCARD_OPERATION* operation)
639
0
{
640
0
  LONG status = 0;
641
0
  LocateCards_Return ret = WINPR_C_ARRAY_INIT;
642
0
  LocateCardsA_Call* call = nullptr;
643
644
0
  WINPR_ASSERT(smartcard);
645
0
  WINPR_ASSERT(operation);
646
647
0
  call = &operation->call.locateCardsA;
648
649
0
  ret.ReturnCode = wrap(smartcard, SCardLocateCardsA, operation->hContext, call->mszCards,
650
0
                        call->rgReaderStates, call->cReaders);
651
0
  scard_log_status_error_wlog(smartcard->log, "SCardLocateCardsA", ret.ReturnCode);
652
0
  ret.cReaders = call->cReaders;
653
0
  ret.rgReaderStates = nullptr;
654
655
0
  if (ret.cReaders > 0)
656
0
  {
657
0
    ret.rgReaderStates = (ReaderState_Return*)calloc(ret.cReaders, sizeof(ReaderState_Return));
658
659
0
    if (!ret.rgReaderStates)
660
0
      return STATUS_NO_MEMORY;
661
0
  }
662
663
0
  for (UINT32 x = 0; x < ret.cReaders; x++)
664
0
  {
665
0
    ret.rgReaderStates[x].dwCurrentState = call->rgReaderStates[x].dwCurrentState;
666
0
    ret.rgReaderStates[x].dwEventState = call->rgReaderStates[x].dwEventState;
667
0
    ret.rgReaderStates[x].cbAtr = call->rgReaderStates[x].cbAtr;
668
0
    CopyMemory(&(ret.rgReaderStates[x].rgbAtr), &(call->rgReaderStates[x].rgbAtr),
669
0
               sizeof(ret.rgReaderStates[x].rgbAtr));
670
0
  }
671
672
0
  status = smartcard_pack_locate_cards_return(out, &ret);
673
674
0
  if (status != SCARD_S_SUCCESS)
675
0
    return status;
676
677
0
  return ret.ReturnCode;
678
0
}
679
680
static LONG smartcard_LocateCardsW_Call(scard_call_context* smartcard, wStream* out,
681
                                        SMARTCARD_OPERATION* operation)
682
0
{
683
0
  LONG status = 0;
684
0
  LocateCards_Return ret = WINPR_C_ARRAY_INIT;
685
0
  LocateCardsW_Call* call = nullptr;
686
687
0
  WINPR_ASSERT(smartcard);
688
0
  WINPR_ASSERT(operation);
689
690
0
  call = &operation->call.locateCardsW;
691
692
0
  ret.ReturnCode = wrap(smartcard, SCardLocateCardsW, operation->hContext, call->mszCards,
693
0
                        call->rgReaderStates, call->cReaders);
694
0
  scard_log_status_error_wlog(smartcard->log, "SCardLocateCardsW", ret.ReturnCode);
695
0
  ret.cReaders = call->cReaders;
696
0
  ret.rgReaderStates = nullptr;
697
698
0
  if (ret.cReaders > 0)
699
0
  {
700
0
    ret.rgReaderStates = (ReaderState_Return*)calloc(ret.cReaders, sizeof(ReaderState_Return));
701
702
0
    if (!ret.rgReaderStates)
703
0
      return STATUS_NO_MEMORY;
704
0
  }
705
706
0
  for (UINT32 x = 0; x < ret.cReaders; x++)
707
0
  {
708
0
    ret.rgReaderStates[x].dwCurrentState = call->rgReaderStates[x].dwCurrentState;
709
0
    ret.rgReaderStates[x].dwEventState = call->rgReaderStates[x].dwEventState;
710
0
    ret.rgReaderStates[x].cbAtr = call->rgReaderStates[x].cbAtr;
711
0
    CopyMemory(&(ret.rgReaderStates[x].rgbAtr), &(call->rgReaderStates[x].rgbAtr),
712
0
               sizeof(ret.rgReaderStates[x].rgbAtr));
713
0
  }
714
715
0
  status = smartcard_pack_locate_cards_return(out, &ret);
716
717
0
  if (status != SCARD_S_SUCCESS)
718
0
    return status;
719
720
0
  return ret.ReturnCode;
721
0
}
722
723
static LONG smartcard_ReadCacheA_Call(scard_call_context* smartcard, wStream* out,
724
                                      SMARTCARD_OPERATION* operation)
725
0
{
726
0
  LONG status = 0;
727
0
  BOOL autoalloc = 0;
728
0
  ReadCache_Return ret = WINPR_C_ARRAY_INIT;
729
0
  ReadCacheA_Call* call = nullptr;
730
731
0
  WINPR_ASSERT(smartcard);
732
0
  WINPR_ASSERT(out);
733
0
  WINPR_ASSERT(operation);
734
735
0
  call = &operation->call.readCacheA;
736
0
  autoalloc = (call->Common.cbDataLen == SCARD_AUTOALLOCATE);
737
738
0
  if (!call->Common.fPbDataIsNULL)
739
0
  {
740
0
    ret.cbDataLen = call->Common.cbDataLen;
741
0
    if (!autoalloc)
742
0
    {
743
0
      ret.pbData = malloc(ret.cbDataLen);
744
0
      if (!ret.pbData)
745
0
        return SCARD_F_INTERNAL_ERROR;
746
0
    }
747
0
  }
748
749
0
  if (autoalloc)
750
0
    ret.ReturnCode = wrap(smartcard, SCardReadCacheA, operation->hContext,
751
0
                          call->Common.CardIdentifier, call->Common.FreshnessCounter,
752
0
                          call->szLookupName, (BYTE*)&ret.pbData, &ret.cbDataLen);
753
0
  else
754
0
    ret.ReturnCode =
755
0
        wrap(smartcard, SCardReadCacheA, operation->hContext, call->Common.CardIdentifier,
756
0
             call->Common.FreshnessCounter, call->szLookupName, ret.pbData, &ret.cbDataLen);
757
0
  if ((ret.ReturnCode != SCARD_W_CACHE_ITEM_NOT_FOUND) &&
758
0
      (ret.ReturnCode != SCARD_W_CACHE_ITEM_STALE))
759
0
  {
760
0
    scard_log_status_error_wlog(smartcard->log, "SCardReadCacheA", ret.ReturnCode);
761
0
  }
762
763
0
  status = smartcard_pack_read_cache_return(out, &ret);
764
0
  if (autoalloc)
765
0
    wrap(smartcard, SCardFreeMemory, operation->hContext, ret.pbData);
766
0
  else
767
0
    free(ret.pbData);
768
0
  if (status != SCARD_S_SUCCESS)
769
0
    return status;
770
771
0
  return ret.ReturnCode;
772
0
}
773
774
static LONG smartcard_ReadCacheW_Call(scard_call_context* smartcard, wStream* out,
775
                                      SMARTCARD_OPERATION* operation)
776
0
{
777
0
  LONG status = 0;
778
0
  ReadCache_Return ret = WINPR_C_ARRAY_INIT;
779
0
  ReadCacheW_Call* call = nullptr;
780
781
0
  WINPR_ASSERT(smartcard);
782
0
  WINPR_ASSERT(out);
783
0
  WINPR_ASSERT(operation);
784
785
0
  call = &operation->call.readCacheW;
786
787
0
  if (!call->Common.fPbDataIsNULL)
788
0
    ret.cbDataLen = SCARD_AUTOALLOCATE;
789
790
0
  ret.ReturnCode =
791
0
      wrap(smartcard, SCardReadCacheW, operation->hContext, call->Common.CardIdentifier,
792
0
           call->Common.FreshnessCounter, call->szLookupName, (BYTE*)&ret.pbData, &ret.cbDataLen);
793
794
0
  if ((ret.ReturnCode != SCARD_W_CACHE_ITEM_NOT_FOUND) &&
795
0
      (ret.ReturnCode != SCARD_W_CACHE_ITEM_STALE))
796
0
  {
797
0
    scard_log_status_error_wlog(smartcard->log, "SCardReadCacheW", ret.ReturnCode);
798
0
  }
799
800
0
  status = smartcard_pack_read_cache_return(out, &ret);
801
802
0
  wrap(smartcard, SCardFreeMemory, operation->hContext, ret.pbData);
803
804
0
  if (status != SCARD_S_SUCCESS)
805
0
    return status;
806
807
0
  return ret.ReturnCode;
808
0
}
809
810
static LONG smartcard_WriteCacheA_Call(scard_call_context* smartcard,
811
                                       WINPR_ATTR_UNUSED wStream* out,
812
                                       SMARTCARD_OPERATION* operation)
813
0
{
814
0
  Long_Return ret = WINPR_C_ARRAY_INIT;
815
0
  WriteCacheA_Call* call = nullptr;
816
817
0
  WINPR_ASSERT(smartcard);
818
0
  WINPR_ASSERT(out);
819
0
  WINPR_ASSERT(operation);
820
821
0
  call = &operation->call.writeCacheA;
822
823
0
  ret.ReturnCode = wrap(smartcard, SCardWriteCacheA, operation->hContext,
824
0
                        call->Common.CardIdentifier, call->Common.FreshnessCounter,
825
0
                        call->szLookupName, call->Common.pbData, call->Common.cbDataLen);
826
0
  scard_log_status_error_wlog(smartcard->log, "SCardWriteCacheA", ret.ReturnCode);
827
0
  smartcard_trace_long_return_int(smartcard->log, &ret, "SCardWriteCacheA");
828
0
  return ret.ReturnCode;
829
0
}
830
831
static LONG smartcard_WriteCacheW_Call(scard_call_context* smartcard,
832
                                       WINPR_ATTR_UNUSED wStream* out,
833
                                       SMARTCARD_OPERATION* operation)
834
0
{
835
0
  Long_Return ret = WINPR_C_ARRAY_INIT;
836
0
  WriteCacheW_Call* call = nullptr;
837
838
0
  WINPR_ASSERT(smartcard);
839
0
  WINPR_ASSERT(out);
840
0
  WINPR_ASSERT(operation);
841
842
0
  call = &operation->call.writeCacheW;
843
844
0
  ret.ReturnCode = wrap(smartcard, SCardWriteCacheW, operation->hContext,
845
0
                        call->Common.CardIdentifier, call->Common.FreshnessCounter,
846
0
                        call->szLookupName, call->Common.pbData, call->Common.cbDataLen);
847
0
  scard_log_status_error_wlog(smartcard->log, "SCardWriteCacheW", ret.ReturnCode);
848
0
  smartcard_trace_long_return_int(smartcard->log, &ret, "SCardWriteCacheW");
849
0
  return ret.ReturnCode;
850
0
}
851
852
static LONG smartcard_GetTransmitCount_Call(scard_call_context* smartcard, wStream* out,
853
                                            SMARTCARD_OPERATION* operation)
854
0
{
855
0
  LONG status = 0;
856
0
  GetTransmitCount_Return ret = WINPR_C_ARRAY_INIT;
857
858
0
  WINPR_ASSERT(smartcard);
859
0
  WINPR_ASSERT(out);
860
0
  WINPR_ASSERT(operation);
861
862
0
  ret.ReturnCode = wrap(smartcard, SCardGetTransmitCount, operation->hCard, &ret.cTransmitCount);
863
0
  scard_log_status_error_wlog(smartcard->log, "SCardGetTransmitCount", ret.ReturnCode);
864
0
  status = smartcard_pack_get_transmit_count_return(out, &ret);
865
0
  if (status != SCARD_S_SUCCESS)
866
0
    return status;
867
868
0
  return ret.ReturnCode;
869
0
}
870
871
static LONG smartcard_ReleaseStartedEvent_Call(scard_call_context* smartcard, wStream* out,
872
                                               SMARTCARD_OPERATION* operation)
873
0
{
874
0
  WINPR_UNUSED(smartcard);
875
0
  WINPR_UNUSED(out);
876
0
  WINPR_UNUSED(operation);
877
878
0
  WLog_Print(smartcard->log, WLOG_WARN,
879
0
             "According to [MS-RDPESC] 3.1.4 Message Processing Events and Sequencing Rules "
880
0
             "this is not supported?!?");
881
0
  return SCARD_E_UNSUPPORTED_FEATURE;
882
0
}
883
884
static LONG smartcard_GetReaderIcon_Call(scard_call_context* smartcard, wStream* out,
885
                                         SMARTCARD_OPERATION* operation)
886
0
{
887
0
  LONG status = 0;
888
0
  GetReaderIcon_Return ret = WINPR_C_ARRAY_INIT;
889
0
  GetReaderIcon_Call* call = nullptr;
890
891
0
  WINPR_ASSERT(smartcard);
892
0
  WINPR_ASSERT(out);
893
0
  WINPR_ASSERT(operation);
894
895
0
  call = &operation->call.getReaderIcon;
896
897
0
  ret.cbDataLen = SCARD_AUTOALLOCATE;
898
0
  ret.ReturnCode = wrap(smartcard, SCardGetReaderIconW, operation->hContext, call->szReaderName,
899
0
                        (LPBYTE)&ret.pbData, &ret.cbDataLen);
900
0
  scard_log_status_error_wlog(smartcard->log, "SCardGetReaderIconW", ret.ReturnCode);
901
0
  if ((ret.ReturnCode == SCARD_S_SUCCESS) && (ret.cbDataLen == SCARD_AUTOALLOCATE))
902
0
    return SCARD_F_UNKNOWN_ERROR;
903
904
0
  status = smartcard_pack_get_reader_icon_return(out, &ret);
905
0
  wrap(smartcard, SCardFreeMemory, operation->hContext, ret.pbData);
906
0
  if (status != SCARD_S_SUCCESS)
907
0
    return status;
908
909
0
  return ret.ReturnCode;
910
0
}
911
912
static LONG smartcard_GetDeviceTypeId_Call(scard_call_context* smartcard, wStream* out,
913
                                           SMARTCARD_OPERATION* operation)
914
0
{
915
0
  LONG status = 0;
916
0
  GetDeviceTypeId_Return ret = WINPR_C_ARRAY_INIT;
917
0
  GetDeviceTypeId_Call* call = nullptr;
918
919
0
  WINPR_ASSERT(smartcard);
920
0
  WINPR_ASSERT(out);
921
0
  WINPR_ASSERT(operation);
922
923
0
  call = &operation->call.getDeviceTypeId;
924
925
0
  ret.ReturnCode = wrap(smartcard, SCardGetDeviceTypeIdW, operation->hContext, call->szReaderName,
926
0
                        &ret.dwDeviceId);
927
0
  scard_log_status_error_wlog(smartcard->log, "SCardGetDeviceTypeIdW", ret.ReturnCode);
928
929
0
  status = smartcard_pack_device_type_id_return(out, &ret);
930
0
  if (status != SCARD_S_SUCCESS)
931
0
    return status;
932
933
0
  return ret.ReturnCode;
934
0
}
935
936
static LONG smartcard_GetStatusChangeA_Call(scard_call_context* smartcard, wStream* out,
937
                                            SMARTCARD_OPERATION* operation)
938
0
{
939
0
  LONG status = STATUS_NO_MEMORY;
940
0
  DWORD dwTimeOut = 0;
941
0
  const DWORD dwTimeStep = 100;
942
0
  GetStatusChange_Return ret = WINPR_C_ARRAY_INIT;
943
0
  GetStatusChangeA_Call* call = nullptr;
944
0
  LPSCARD_READERSTATEA rgReaderStates = nullptr;
945
946
0
  WINPR_ASSERT(smartcard);
947
0
  WINPR_ASSERT(out);
948
0
  WINPR_ASSERT(operation);
949
950
0
  call = &operation->call.getStatusChangeA;
951
0
  dwTimeOut = call->dwTimeOut;
952
953
0
  if (call->cReaders > 0)
954
0
  {
955
0
    ret.cReaders = call->cReaders;
956
0
    rgReaderStates = calloc(ret.cReaders, sizeof(SCARD_READERSTATEA));
957
0
    ret.rgReaderStates = (ReaderState_Return*)calloc(ret.cReaders, sizeof(ReaderState_Return));
958
0
    if (!rgReaderStates || !ret.rgReaderStates)
959
0
      goto fail;
960
0
  }
961
962
0
  for (UINT32 x = 0; x < MAX(1, dwTimeOut);)
963
0
  {
964
0
    if (call->cReaders > 0)
965
0
      memcpy(rgReaderStates, call->rgReaderStates,
966
0
             call->cReaders * sizeof(SCARD_READERSTATEA));
967
0
    ret.ReturnCode = wrap(smartcard, SCardGetStatusChangeA, operation->hContext,
968
0
                          MIN(dwTimeOut, dwTimeStep), rgReaderStates, call->cReaders);
969
0
    if (ret.ReturnCode != SCARD_E_TIMEOUT)
970
0
      break;
971
0
    if (WaitForSingleObject(smartcard->stopEvent, 0) == WAIT_OBJECT_0)
972
0
      break;
973
0
    if (dwTimeOut != INFINITE)
974
0
      x += dwTimeStep;
975
0
  }
976
0
  scard_log_status_error_wlog(smartcard->log, "SCardGetStatusChangeA", ret.ReturnCode);
977
978
0
  for (UINT32 index = 0; index < ret.cReaders; index++)
979
0
  {
980
0
    const SCARD_READERSTATEA* cur = &rgReaderStates[index];
981
0
    ReaderState_Return* rout = &ret.rgReaderStates[index];
982
983
0
    rout->dwCurrentState = cur->dwCurrentState;
984
0
    rout->dwEventState = cur->dwEventState;
985
0
    rout->cbAtr = cur->cbAtr;
986
0
    CopyMemory(&(rout->rgbAtr), cur->rgbAtr, sizeof(rout->rgbAtr));
987
0
  }
988
989
0
  status = smartcard_pack_get_status_change_return(out, &ret, FALSE);
990
0
fail:
991
0
  free(ret.rgReaderStates);
992
0
  free(rgReaderStates);
993
0
  if (status != SCARD_S_SUCCESS)
994
0
    return status;
995
0
  return ret.ReturnCode;
996
0
}
997
998
static LONG smartcard_GetStatusChangeW_Call(scard_call_context* smartcard, wStream* out,
999
                                            SMARTCARD_OPERATION* operation)
1000
0
{
1001
0
  LONG status = STATUS_NO_MEMORY;
1002
0
  DWORD dwTimeOut = 0;
1003
0
  const DWORD dwTimeStep = 100;
1004
0
  GetStatusChange_Return ret = WINPR_C_ARRAY_INIT;
1005
0
  LPSCARD_READERSTATEW rgReaderStates = nullptr;
1006
1007
0
  WINPR_ASSERT(smartcard);
1008
0
  WINPR_ASSERT(out);
1009
0
  WINPR_ASSERT(operation);
1010
1011
0
  GetStatusChangeW_Call* call = &operation->call.getStatusChangeW;
1012
0
  dwTimeOut = call->dwTimeOut;
1013
1014
0
  if (call->cReaders > 0)
1015
0
  {
1016
0
    ret.cReaders = call->cReaders;
1017
0
    rgReaderStates = calloc(ret.cReaders, sizeof(SCARD_READERSTATEW));
1018
0
    ret.rgReaderStates = (ReaderState_Return*)calloc(ret.cReaders, sizeof(ReaderState_Return));
1019
0
    if (!rgReaderStates || !ret.rgReaderStates)
1020
0
      goto fail;
1021
0
  }
1022
1023
0
  for (UINT32 x = 0; x < MAX(1, dwTimeOut);)
1024
0
  {
1025
0
    if (call->cReaders > 0)
1026
0
      memcpy(rgReaderStates, call->rgReaderStates,
1027
0
             call->cReaders * sizeof(SCARD_READERSTATEW));
1028
0
    {
1029
0
      ret.ReturnCode = wrap(smartcard, SCardGetStatusChangeW, operation->hContext,
1030
0
                            MIN(dwTimeOut, dwTimeStep), rgReaderStates, call->cReaders);
1031
0
    }
1032
0
    if (ret.ReturnCode != SCARD_E_TIMEOUT)
1033
0
      break;
1034
0
    if (WaitForSingleObject(smartcard->stopEvent, 0) == WAIT_OBJECT_0)
1035
0
      break;
1036
0
    if (dwTimeOut != INFINITE)
1037
0
      x += dwTimeStep;
1038
0
  }
1039
0
  scard_log_status_error_wlog(smartcard->log, "SCardGetStatusChangeW", ret.ReturnCode);
1040
1041
0
  for (UINT32 index = 0; index < ret.cReaders; index++)
1042
0
  {
1043
0
    const SCARD_READERSTATEW* cur = &rgReaderStates[index];
1044
0
    ReaderState_Return* rout = &ret.rgReaderStates[index];
1045
1046
0
    rout->dwCurrentState = cur->dwCurrentState;
1047
0
    rout->dwEventState = cur->dwEventState;
1048
0
    rout->cbAtr = cur->cbAtr;
1049
0
    CopyMemory(&(rout->rgbAtr), cur->rgbAtr, sizeof(rout->rgbAtr));
1050
0
  }
1051
1052
0
  status = smartcard_pack_get_status_change_return(out, &ret, TRUE);
1053
0
fail:
1054
0
  free(ret.rgReaderStates);
1055
0
  free(rgReaderStates);
1056
0
  if (status != SCARD_S_SUCCESS)
1057
0
    return status;
1058
0
  return ret.ReturnCode;
1059
0
}
1060
1061
static LONG smartcard_Cancel_Call(scard_call_context* smartcard, WINPR_ATTR_UNUSED wStream* out,
1062
                                  SMARTCARD_OPERATION* operation)
1063
0
{
1064
0
  Long_Return ret = WINPR_C_ARRAY_INIT;
1065
1066
0
  WINPR_ASSERT(smartcard);
1067
0
  WINPR_ASSERT(out);
1068
0
  WINPR_ASSERT(operation);
1069
1070
0
  ret.ReturnCode = wrap(smartcard, SCardCancel, operation->hContext);
1071
0
  scard_log_status_error_wlog(smartcard->log, "SCardCancel", ret.ReturnCode);
1072
0
  smartcard_trace_long_return_int(smartcard->log, &ret, "Cancel");
1073
0
  return ret.ReturnCode;
1074
0
}
1075
1076
static LONG smartcard_ConnectA_Call(scard_call_context* smartcard, wStream* out,
1077
                                    SMARTCARD_OPERATION* operation)
1078
0
{
1079
0
  LONG status = 0;
1080
0
  SCARDHANDLE hCard = 0;
1081
0
  Connect_Return ret = WINPR_C_ARRAY_INIT;
1082
0
  ConnectA_Call* call = nullptr;
1083
1084
0
  WINPR_ASSERT(smartcard);
1085
0
  WINPR_ASSERT(out);
1086
0
  WINPR_ASSERT(operation);
1087
1088
0
  call = &operation->call.connectA;
1089
1090
0
  if ((call->Common.dwPreferredProtocols == SCARD_PROTOCOL_UNDEFINED) &&
1091
0
      (call->Common.dwShareMode != SCARD_SHARE_DIRECT))
1092
0
  {
1093
0
    call->Common.dwPreferredProtocols = SCARD_PROTOCOL_Tx;
1094
0
  }
1095
1096
0
  ret.ReturnCode = wrap(smartcard, SCardConnectA, operation->hContext, (char*)call->szReader,
1097
0
                        call->Common.dwShareMode, call->Common.dwPreferredProtocols, &hCard,
1098
0
                        &ret.dwActiveProtocol);
1099
0
  smartcard_scard_context_native_to_redir(&(ret.hContext), operation->hContext);
1100
0
  smartcard_scard_handle_native_to_redir(&(ret.hCard), hCard);
1101
1102
0
  status = smartcard_pack_connect_return(out, &ret);
1103
0
  if (status != SCARD_S_SUCCESS)
1104
0
    goto out_fail;
1105
1106
0
  status = ret.ReturnCode;
1107
0
out_fail:
1108
1109
0
  return status;
1110
0
}
1111
1112
static LONG smartcard_ConnectW_Call(scard_call_context* smartcard, wStream* out,
1113
                                    SMARTCARD_OPERATION* operation)
1114
0
{
1115
0
  LONG status = 0;
1116
0
  SCARDHANDLE hCard = 0;
1117
0
  Connect_Return ret = WINPR_C_ARRAY_INIT;
1118
0
  ConnectW_Call* call = nullptr;
1119
1120
0
  WINPR_ASSERT(smartcard);
1121
0
  WINPR_ASSERT(out);
1122
0
  WINPR_ASSERT(operation);
1123
1124
0
  call = &operation->call.connectW;
1125
1126
0
  if ((call->Common.dwPreferredProtocols == SCARD_PROTOCOL_UNDEFINED) &&
1127
0
      (call->Common.dwShareMode != SCARD_SHARE_DIRECT))
1128
0
  {
1129
0
    call->Common.dwPreferredProtocols = SCARD_PROTOCOL_Tx;
1130
0
  }
1131
1132
0
  ret.ReturnCode = wrap(smartcard, SCardConnectW, operation->hContext, (WCHAR*)call->szReader,
1133
0
                        call->Common.dwShareMode, call->Common.dwPreferredProtocols, &hCard,
1134
0
                        &ret.dwActiveProtocol);
1135
0
  smartcard_scard_context_native_to_redir(&(ret.hContext), operation->hContext);
1136
0
  smartcard_scard_handle_native_to_redir(&(ret.hCard), hCard);
1137
1138
0
  status = smartcard_pack_connect_return(out, &ret);
1139
0
  if (status != SCARD_S_SUCCESS)
1140
0
    goto out_fail;
1141
1142
0
  status = ret.ReturnCode;
1143
0
out_fail:
1144
1145
0
  return status;
1146
0
}
1147
1148
static LONG smartcard_Reconnect_Call(scard_call_context* smartcard, wStream* out,
1149
                                     SMARTCARD_OPERATION* operation)
1150
0
{
1151
0
  LONG status = 0;
1152
0
  Reconnect_Return ret = WINPR_C_ARRAY_INIT;
1153
0
  Reconnect_Call* call = nullptr;
1154
1155
0
  WINPR_ASSERT(smartcard);
1156
0
  WINPR_ASSERT(out);
1157
0
  WINPR_ASSERT(operation);
1158
1159
0
  call = &operation->call.reconnect;
1160
0
  ret.ReturnCode =
1161
0
      wrap(smartcard, SCardReconnect, operation->hCard, call->dwShareMode,
1162
0
           call->dwPreferredProtocols, call->dwInitialization, &ret.dwActiveProtocol);
1163
0
  scard_log_status_error_wlog(smartcard->log, "SCardReconnect", ret.ReturnCode);
1164
0
  status = smartcard_pack_reconnect_return(out, &ret);
1165
0
  if (status != SCARD_S_SUCCESS)
1166
0
    return status;
1167
1168
0
  return ret.ReturnCode;
1169
0
}
1170
1171
static LONG smartcard_Disconnect_Call(scard_call_context* smartcard, WINPR_ATTR_UNUSED wStream* out,
1172
                                      SMARTCARD_OPERATION* operation)
1173
0
{
1174
0
  Long_Return ret = WINPR_C_ARRAY_INIT;
1175
0
  HCardAndDisposition_Call* call = nullptr;
1176
1177
0
  WINPR_ASSERT(smartcard);
1178
0
  WINPR_ASSERT(out);
1179
0
  WINPR_ASSERT(operation);
1180
1181
0
  call = &operation->call.hCardAndDisposition;
1182
1183
0
  ret.ReturnCode = wrap(smartcard, SCardDisconnect, operation->hCard, call->dwDisposition);
1184
0
  scard_log_status_error_wlog(smartcard->log, "SCardDisconnect", ret.ReturnCode);
1185
0
  smartcard_trace_long_return_int(smartcard->log, &ret, "Disconnect");
1186
1187
0
  return ret.ReturnCode;
1188
0
}
1189
1190
static LONG smartcard_BeginTransaction_Call(scard_call_context* smartcard,
1191
                                            WINPR_ATTR_UNUSED wStream* out,
1192
                                            SMARTCARD_OPERATION* operation)
1193
0
{
1194
0
  Long_Return ret = WINPR_C_ARRAY_INIT;
1195
1196
0
  WINPR_ASSERT(smartcard);
1197
0
  WINPR_ASSERT(out);
1198
0
  WINPR_ASSERT(operation);
1199
1200
0
  ret.ReturnCode = wrap(smartcard, SCardBeginTransaction, operation->hCard);
1201
0
  scard_log_status_error_wlog(smartcard->log, "SCardBeginTransaction", ret.ReturnCode);
1202
0
  smartcard_trace_long_return_int(smartcard->log, &ret, "BeginTransaction");
1203
0
  return ret.ReturnCode;
1204
0
}
1205
1206
static LONG smartcard_EndTransaction_Call(scard_call_context* smartcard,
1207
                                          WINPR_ATTR_UNUSED wStream* out,
1208
                                          SMARTCARD_OPERATION* operation)
1209
0
{
1210
0
  Long_Return ret = WINPR_C_ARRAY_INIT;
1211
0
  HCardAndDisposition_Call* call = nullptr;
1212
1213
0
  WINPR_ASSERT(smartcard);
1214
0
  WINPR_ASSERT(out);
1215
0
  WINPR_ASSERT(operation);
1216
1217
0
  call = &operation->call.hCardAndDisposition;
1218
1219
0
  ret.ReturnCode = wrap(smartcard, SCardEndTransaction, operation->hCard, call->dwDisposition);
1220
0
  scard_log_status_error_wlog(smartcard->log, "SCardEndTransaction", ret.ReturnCode);
1221
0
  smartcard_trace_long_return_int(smartcard->log, &ret, "EndTransaction");
1222
0
  return ret.ReturnCode;
1223
0
}
1224
1225
static LONG smartcard_State_Call(scard_call_context* smartcard, wStream* out,
1226
                                 SMARTCARD_OPERATION* operation)
1227
0
{
1228
0
  LONG status = 0;
1229
0
  State_Return ret = WINPR_C_ARRAY_INIT;
1230
1231
0
  WINPR_ASSERT(smartcard);
1232
0
  WINPR_ASSERT(out);
1233
0
  WINPR_ASSERT(operation);
1234
1235
0
  ret.cbAtrLen = SCARD_ATR_LENGTH;
1236
0
  ret.ReturnCode = wrap(smartcard, SCardState, operation->hCard, &ret.dwState, &ret.dwProtocol,
1237
0
                        (BYTE*)&ret.rgAtr, &ret.cbAtrLen);
1238
1239
0
  scard_log_status_error_wlog(smartcard->log, "SCardState", ret.ReturnCode);
1240
0
  status = smartcard_pack_state_return(out, &ret);
1241
0
  if (status != SCARD_S_SUCCESS)
1242
0
    return status;
1243
1244
0
  return ret.ReturnCode;
1245
0
}
1246
1247
static LONG smartcard_StatusA_Call(scard_call_context* smartcard, wStream* out,
1248
                                   SMARTCARD_OPERATION* operation)
1249
0
{
1250
0
  LONG status = 0;
1251
0
  Status_Return ret = WINPR_C_ARRAY_INIT;
1252
0
  DWORD cchReaderLen = 0;
1253
0
  DWORD cbAtrLen = 0;
1254
0
  LPSTR mszReaderNames = nullptr;
1255
0
  Status_Call* call = nullptr;
1256
1257
0
  WINPR_ASSERT(smartcard);
1258
0
  WINPR_ASSERT(out);
1259
0
  WINPR_ASSERT(operation);
1260
1261
0
  call = &operation->call.status;
1262
1263
0
  call->cbAtrLen = 32;
1264
0
  cbAtrLen = call->cbAtrLen;
1265
1266
0
  if (call->fmszReaderNamesIsNULL)
1267
0
    cchReaderLen = 0;
1268
0
  else
1269
0
    cchReaderLen = SCARD_AUTOALLOCATE;
1270
1271
0
  status = ret.ReturnCode =
1272
0
      wrap(smartcard, SCardStatusA, operation->hCard,
1273
0
           call->fmszReaderNamesIsNULL ? nullptr : (LPSTR)&mszReaderNames, &cchReaderLen,
1274
0
           &ret.dwState, &ret.dwProtocol, cbAtrLen ? (BYTE*)&ret.pbAtr : nullptr, &cbAtrLen);
1275
1276
0
  scard_log_status_error_wlog(smartcard->log, "SCardStatusA", status);
1277
0
  if ((ret.ReturnCode == SCARD_S_SUCCESS) && (cchReaderLen == SCARD_AUTOALLOCATE))
1278
0
    return SCARD_F_UNKNOWN_ERROR;
1279
1280
0
  if (status == SCARD_S_SUCCESS)
1281
0
  {
1282
0
    if (!call->fmszReaderNamesIsNULL)
1283
0
      ret.mszReaderNames = (BYTE*)mszReaderNames;
1284
1285
0
    ret.cBytes = cchReaderLen;
1286
1287
0
    if (call->cbAtrLen)
1288
0
      ret.cbAtrLen = cbAtrLen;
1289
0
  }
1290
1291
0
  status = smartcard_pack_status_return(out, &ret, FALSE);
1292
1293
0
  if (mszReaderNames)
1294
0
    wrap(smartcard, SCardFreeMemory, operation->hContext, mszReaderNames);
1295
1296
0
  if (status != SCARD_S_SUCCESS)
1297
0
    return status;
1298
0
  return ret.ReturnCode;
1299
0
}
1300
1301
static LONG smartcard_StatusW_Call(scard_call_context* smartcard, wStream* out,
1302
                                   SMARTCARD_OPERATION* operation)
1303
0
{
1304
0
  LONG status = 0;
1305
0
  Status_Return ret = WINPR_C_ARRAY_INIT;
1306
0
  LPWSTR mszReaderNames = nullptr;
1307
0
  Status_Call* call = nullptr;
1308
0
  DWORD cbAtrLen = 0;
1309
1310
0
  WINPR_ASSERT(smartcard);
1311
0
  WINPR_ASSERT(out);
1312
0
  WINPR_ASSERT(operation);
1313
1314
0
  call = &operation->call.status;
1315
1316
  /**
1317
   * [MS-RDPESC]
1318
   * According to 2.2.2.18 Status_Call cbAtrLen is unused an must be ignored upon receipt.
1319
   */
1320
0
  cbAtrLen = call->cbAtrLen = 32;
1321
1322
0
  if (call->fmszReaderNamesIsNULL)
1323
0
    ret.cBytes = 0;
1324
0
  else
1325
0
    ret.cBytes = SCARD_AUTOALLOCATE;
1326
1327
0
  status = ret.ReturnCode =
1328
0
      wrap(smartcard, SCardStatusW, operation->hCard,
1329
0
           call->fmszReaderNamesIsNULL ? nullptr : (LPWSTR)&mszReaderNames, &ret.cBytes,
1330
0
           &ret.dwState, &ret.dwProtocol, (BYTE*)&ret.pbAtr, &cbAtrLen);
1331
0
  scard_log_status_error_wlog(smartcard->log, "SCardStatusW", status);
1332
0
  if ((ret.ReturnCode == SCARD_S_SUCCESS) && (ret.cBytes == SCARD_AUTOALLOCATE))
1333
0
    return SCARD_F_UNKNOWN_ERROR;
1334
1335
0
  size_t blen = 0;
1336
0
  if (status == SCARD_S_SUCCESS)
1337
0
  {
1338
0
    if (!call->fmszReaderNamesIsNULL)
1339
0
      ret.mszReaderNames = (BYTE*)mszReaderNames;
1340
1341
0
    ret.cbAtrLen = cbAtrLen;
1342
0
  }
1343
1344
0
  if (ret.cBytes != SCARD_AUTOALLOCATE)
1345
0
  {
1346
    /* SCardStatusW returns number of characters, we need number of bytes */
1347
0
    WINPR_ASSERT(ret.cBytes < SCARD_AUTOALLOCATE / sizeof(WCHAR));
1348
0
    blen = sizeof(WCHAR) * ret.cBytes;
1349
0
    WINPR_ASSERT(blen <= UINT32_MAX);
1350
0
    ret.cBytes = (UINT32)blen;
1351
0
  }
1352
1353
0
  status = smartcard_pack_status_return(out, &ret, TRUE);
1354
0
  if (status != SCARD_S_SUCCESS)
1355
0
    return status;
1356
1357
0
  if (mszReaderNames)
1358
0
    wrap(smartcard, SCardFreeMemory, operation->hContext, mszReaderNames);
1359
1360
0
  return ret.ReturnCode;
1361
0
}
1362
1363
static LONG smartcard_Transmit_Call(scard_call_context* smartcard, wStream* out,
1364
                                    SMARTCARD_OPERATION* operation)
1365
0
{
1366
0
  LONG status = 0;
1367
0
  Transmit_Return ret = WINPR_C_ARRAY_INIT;
1368
0
  Transmit_Call* call = nullptr;
1369
1370
0
  WINPR_ASSERT(smartcard);
1371
0
  WINPR_ASSERT(out);
1372
0
  WINPR_ASSERT(operation);
1373
1374
0
  call = &operation->call.transmit;
1375
0
  ret.cbRecvLength = 0;
1376
0
  ret.pbRecvBuffer = nullptr;
1377
1378
0
  if (call->cbRecvLength && !call->fpbRecvBufferIsNULL)
1379
0
  {
1380
0
    if (call->cbRecvLength >= 66560)
1381
0
      call->cbRecvLength = 66560;
1382
1383
0
    ret.cbRecvLength = call->cbRecvLength;
1384
0
    ret.pbRecvBuffer = (BYTE*)malloc(ret.cbRecvLength);
1385
1386
0
    if (!ret.pbRecvBuffer)
1387
0
      return STATUS_NO_MEMORY;
1388
0
  }
1389
1390
0
  ret.pioRecvPci = call->pioRecvPci;
1391
0
  ret.ReturnCode =
1392
0
      wrap(smartcard, SCardTransmit, operation->hCard, call->pioSendPci, call->pbSendBuffer,
1393
0
           call->cbSendLength, ret.pioRecvPci, ret.pbRecvBuffer, &(ret.cbRecvLength));
1394
1395
0
  scard_log_status_error_wlog(smartcard->log, "SCardTransmit", ret.ReturnCode);
1396
1397
0
  status = smartcard_pack_transmit_return(out, &ret);
1398
0
  free(ret.pbRecvBuffer);
1399
1400
0
  if (status != SCARD_S_SUCCESS)
1401
0
    return status;
1402
0
  return ret.ReturnCode;
1403
0
}
1404
1405
static LONG smartcard_Control_Call(scard_call_context* smartcard, wStream* out,
1406
                                   SMARTCARD_OPERATION* operation)
1407
0
{
1408
0
  LONG status = 0;
1409
0
  Control_Return ret = WINPR_C_ARRAY_INIT;
1410
0
  Control_Call* call = nullptr;
1411
1412
0
  WINPR_ASSERT(smartcard);
1413
0
  WINPR_ASSERT(out);
1414
0
  WINPR_ASSERT(operation);
1415
1416
0
  call = &operation->call.control;
1417
0
  ret.cbOutBufferSize = call->cbOutBufferSize;
1418
0
  ret.pvOutBuffer = (BYTE*)malloc(call->cbOutBufferSize);
1419
1420
0
  if (!ret.pvOutBuffer)
1421
0
    return SCARD_E_NO_MEMORY;
1422
1423
0
  ret.ReturnCode =
1424
0
      wrap(smartcard, SCardControl, operation->hCard, call->dwControlCode, call->pvInBuffer,
1425
0
           call->cbInBufferSize, ret.pvOutBuffer, call->cbOutBufferSize, &ret.cbOutBufferSize);
1426
0
  scard_log_status_error_wlog(smartcard->log, "SCardControl", ret.ReturnCode);
1427
0
  status = smartcard_pack_control_return(out, &ret);
1428
1429
0
  free(ret.pvOutBuffer);
1430
0
  if (status != SCARD_S_SUCCESS)
1431
0
    return status;
1432
0
  return ret.ReturnCode;
1433
0
}
1434
1435
static LONG smartcard_GetAttrib_Call(scard_call_context* smartcard, wStream* out,
1436
                                     SMARTCARD_OPERATION* operation)
1437
0
{
1438
0
  BOOL autoAllocate = FALSE;
1439
0
  LONG status = 0;
1440
0
  DWORD cbAttrLen = 0;
1441
0
  LPBYTE pbAttr = nullptr;
1442
0
  GetAttrib_Return ret = WINPR_C_ARRAY_INIT;
1443
0
  const GetAttrib_Call* call = nullptr;
1444
1445
0
  WINPR_ASSERT(smartcard);
1446
0
  WINPR_ASSERT(operation);
1447
1448
0
  call = &operation->call.getAttrib;
1449
1450
0
  if (!call->fpbAttrIsNULL)
1451
0
  {
1452
0
    autoAllocate = (call->cbAttrLen == SCARD_AUTOALLOCATE);
1453
0
    cbAttrLen = call->cbAttrLen;
1454
0
    if (cbAttrLen && !autoAllocate)
1455
0
    {
1456
0
      ret.pbAttr = (BYTE*)malloc(cbAttrLen);
1457
1458
0
      if (!ret.pbAttr)
1459
0
        return SCARD_E_NO_MEMORY;
1460
0
    }
1461
1462
0
    pbAttr = autoAllocate ? (LPBYTE) & (ret.pbAttr) : ret.pbAttr;
1463
0
  }
1464
1465
0
  ret.ReturnCode =
1466
0
      wrap(smartcard, SCardGetAttrib, operation->hCard, call->dwAttrId, pbAttr, &cbAttrLen);
1467
0
  scard_log_status_error_wlog(smartcard->log, "SCardGetAttrib", ret.ReturnCode);
1468
0
  if ((ret.ReturnCode == SCARD_S_SUCCESS) && (cbAttrLen == SCARD_AUTOALLOCATE))
1469
0
    return SCARD_F_UNKNOWN_ERROR;
1470
1471
0
  ret.cbAttrLen = cbAttrLen;
1472
1473
0
  status = smartcard_pack_get_attrib_return(out, &ret, call->dwAttrId, call->cbAttrLen);
1474
1475
0
  if (autoAllocate)
1476
0
    wrap(smartcard, SCardFreeMemory, operation->hContext, ret.pbAttr);
1477
0
  else
1478
0
    free(ret.pbAttr);
1479
0
  return status;
1480
0
}
1481
1482
static LONG smartcard_SetAttrib_Call(scard_call_context* smartcard, WINPR_ATTR_UNUSED wStream* out,
1483
                                     SMARTCARD_OPERATION* operation)
1484
0
{
1485
0
  Long_Return ret = WINPR_C_ARRAY_INIT;
1486
0
  SetAttrib_Call* call = nullptr;
1487
1488
0
  WINPR_ASSERT(smartcard);
1489
0
  WINPR_ASSERT(out);
1490
0
  WINPR_ASSERT(operation);
1491
1492
0
  call = &operation->call.setAttrib;
1493
1494
0
  ret.ReturnCode = wrap(smartcard, SCardSetAttrib, operation->hCard, call->dwAttrId, call->pbAttr,
1495
0
                        call->cbAttrLen);
1496
0
  scard_log_status_error_wlog(smartcard->log, "SCardSetAttrib", ret.ReturnCode);
1497
0
  smartcard_trace_long_return_int(smartcard->log, &ret, "SetAttrib");
1498
1499
0
  return ret.ReturnCode;
1500
0
}
1501
1502
static LONG smartcard_AccessStartedEvent_Call(scard_call_context* smartcard,
1503
                                              WINPR_ATTR_UNUSED wStream* out,
1504
                                              SMARTCARD_OPERATION* operation)
1505
0
{
1506
0
  LONG status = SCARD_S_SUCCESS;
1507
1508
0
  WINPR_ASSERT(smartcard);
1509
0
  WINPR_ASSERT(out);
1510
0
  WINPR_UNUSED(operation);
1511
1512
0
  if (!smartcard->StartedEvent)
1513
0
    smartcard->StartedEvent = wrap_ptr(smartcard, SCardAccessStartedEvent);
1514
1515
0
  if (!smartcard->StartedEvent)
1516
0
    status = SCARD_E_NO_SERVICE;
1517
1518
0
  return status;
1519
0
}
1520
1521
static LONG smartcard_LocateCardsByATRA_Call(scard_call_context* smartcard, wStream* out,
1522
                                             SMARTCARD_OPERATION* operation)
1523
0
{
1524
0
  LONG status = 0;
1525
0
  GetStatusChange_Return ret = WINPR_C_ARRAY_INIT;
1526
0
  LPSCARD_READERSTATEA states = nullptr;
1527
0
  LocateCardsByATRA_Call* call = nullptr;
1528
1529
0
  WINPR_ASSERT(smartcard);
1530
0
  WINPR_ASSERT(operation);
1531
1532
0
  call = &operation->call.locateCardsByATRA;
1533
0
  states = (LPSCARD_READERSTATEA)calloc(call->cReaders, sizeof(SCARD_READERSTATEA));
1534
1535
0
  if (!states)
1536
0
    return STATUS_NO_MEMORY;
1537
1538
0
  for (UINT32 i = 0; i < call->cReaders; i++)
1539
0
  {
1540
0
    LPSCARD_READERSTATEA state = &states[i];
1541
0
    state->szReader = call->rgReaderStates[i].szReader;
1542
0
    state->dwCurrentState = call->rgReaderStates[i].dwCurrentState;
1543
0
    state->dwEventState = call->rgReaderStates[i].dwEventState;
1544
0
    state->cbAtr = call->rgReaderStates[i].cbAtr;
1545
0
    CopyMemory(&(state->rgbAtr), &(call->rgReaderStates[i].rgbAtr), 36);
1546
0
  }
1547
1548
0
  status = ret.ReturnCode = wrap(smartcard, SCardGetStatusChangeA, operation->hContext,
1549
0
                                 0x000001F4, states, call->cReaders);
1550
1551
0
  scard_log_status_error_wlog(smartcard->log, "SCardGetStatusChangeA", status);
1552
0
  for (UINT32 i = 0; i < call->cAtrs; i++)
1553
0
  {
1554
0
    for (UINT32 j = 0; j < call->cReaders; j++)
1555
0
    {
1556
0
      for (UINT32 k = 0; k < call->rgAtrMasks[i].cbAtr; k++)
1557
0
      {
1558
0
        if ((call->rgAtrMasks[i].rgbAtr[k] & call->rgAtrMasks[i].rgbMask[k]) !=
1559
0
            (states[j].rgbAtr[k] & call->rgAtrMasks[i].rgbMask[k]))
1560
0
        {
1561
0
          break;
1562
0
        }
1563
1564
0
        states[j].dwEventState |= SCARD_STATE_ATRMATCH;
1565
0
      }
1566
0
    }
1567
0
  }
1568
1569
0
  ret.cReaders = call->cReaders;
1570
0
  ret.rgReaderStates = nullptr;
1571
1572
0
  if (ret.cReaders > 0)
1573
0
    ret.rgReaderStates = (ReaderState_Return*)calloc(ret.cReaders, sizeof(ReaderState_Return));
1574
1575
0
  if (!ret.rgReaderStates)
1576
0
  {
1577
0
    free(states);
1578
0
    return STATUS_NO_MEMORY;
1579
0
  }
1580
1581
0
  for (UINT32 i = 0; i < ret.cReaders; i++)
1582
0
  {
1583
0
    LPSCARD_READERSTATEA state = &states[i];
1584
0
    ret.rgReaderStates[i].dwCurrentState = state->dwCurrentState;
1585
0
    ret.rgReaderStates[i].dwEventState = state->dwEventState;
1586
0
    ret.rgReaderStates[i].cbAtr = state->cbAtr;
1587
0
    CopyMemory(&(ret.rgReaderStates[i].rgbAtr), &(state->rgbAtr),
1588
0
               sizeof(ret.rgReaderStates[i].rgbAtr));
1589
0
  }
1590
1591
0
  free(states);
1592
1593
0
  status = smartcard_pack_get_status_change_return(out, &ret, FALSE);
1594
1595
0
  free(ret.rgReaderStates);
1596
0
  if (status != SCARD_S_SUCCESS)
1597
0
    return status;
1598
0
  return ret.ReturnCode;
1599
0
}
1600
1601
LONG smartcard_irp_device_control_call(scard_call_context* ctx, wStream* out, NTSTATUS* pIoStatus,
1602
                                       SMARTCARD_OPERATION* operation)
1603
0
{
1604
0
  LONG result = 0;
1605
0
  UINT32 offset = 0;
1606
0
  size_t objectBufferLength = 0;
1607
1608
0
  WINPR_ASSERT(ctx);
1609
0
  WINPR_ASSERT(out);
1610
0
  WINPR_ASSERT(pIoStatus);
1611
0
  WINPR_ASSERT(operation);
1612
1613
0
  const UINT32 ioControlCode = operation->ioControlCode;
1614
  /**
1615
   * [MS-RDPESC] 3.2.5.1: Sending Outgoing Messages:
1616
   * the output buffer length SHOULD be set to 2048
1617
   *
1618
   * Since it's a SHOULD and not a MUST, we don't care
1619
   * about it, but we still reserve at least 2048 bytes.
1620
   */
1621
0
  const size_t outMaxLen = MAX(2048, operation->outputBufferLength);
1622
0
  if (!Stream_EnsureRemainingCapacity(out, outMaxLen))
1623
0
    return SCARD_E_NO_MEMORY;
1624
1625
  /* Device Control Response */
1626
0
  Stream_Write_UINT32(out, 0);                            /* OutputBufferLength (4 bytes) */
1627
0
  Stream_Zero(out, SMARTCARD_COMMON_TYPE_HEADER_LENGTH);  /* CommonTypeHeader (8 bytes) */
1628
0
  Stream_Zero(out, SMARTCARD_PRIVATE_TYPE_HEADER_LENGTH); /* PrivateTypeHeader (8 bytes) */
1629
0
  Stream_Write_UINT32(out, 0);                            /* Result (4 bytes) */
1630
1631
  /* Call */
1632
0
  switch (ioControlCode)
1633
0
  {
1634
0
    case SCARD_IOCTL_ESTABLISHCONTEXT:
1635
0
      result = smartcard_EstablishContext_Call(ctx, out, operation);
1636
0
      break;
1637
1638
0
    case SCARD_IOCTL_RELEASECONTEXT:
1639
0
      result = smartcard_ReleaseContext_Call(ctx, out, operation);
1640
0
      break;
1641
1642
0
    case SCARD_IOCTL_ISVALIDCONTEXT:
1643
0
      result = smartcard_IsValidContext_Call(ctx, out, operation);
1644
0
      break;
1645
1646
0
    case SCARD_IOCTL_LISTREADERGROUPSA:
1647
0
      result = smartcard_ListReaderGroupsA_Call(ctx, out, operation);
1648
0
      break;
1649
1650
0
    case SCARD_IOCTL_LISTREADERGROUPSW:
1651
0
      result = smartcard_ListReaderGroupsW_Call(ctx, out, operation);
1652
0
      break;
1653
1654
0
    case SCARD_IOCTL_LISTREADERSA:
1655
0
      result = smartcard_ListReadersA_Call(ctx, out, operation);
1656
0
      break;
1657
1658
0
    case SCARD_IOCTL_LISTREADERSW:
1659
0
      result = smartcard_ListReadersW_Call(ctx, out, operation);
1660
0
      break;
1661
1662
0
    case SCARD_IOCTL_INTRODUCEREADERGROUPA:
1663
0
      result = smartcard_IntroduceReaderGroupA_Call(ctx, out, operation);
1664
0
      break;
1665
1666
0
    case SCARD_IOCTL_INTRODUCEREADERGROUPW:
1667
0
      result = smartcard_IntroduceReaderGroupW_Call(ctx, out, operation);
1668
0
      break;
1669
1670
0
    case SCARD_IOCTL_FORGETREADERGROUPA:
1671
0
      result = smartcard_ForgetReaderA_Call(ctx, out, operation);
1672
0
      break;
1673
1674
0
    case SCARD_IOCTL_FORGETREADERGROUPW:
1675
0
      result = smartcard_ForgetReaderW_Call(ctx, out, operation);
1676
0
      break;
1677
1678
0
    case SCARD_IOCTL_INTRODUCEREADERA:
1679
0
      result = smartcard_IntroduceReaderA_Call(ctx, out, operation);
1680
0
      break;
1681
1682
0
    case SCARD_IOCTL_INTRODUCEREADERW:
1683
0
      result = smartcard_IntroduceReaderW_Call(ctx, out, operation);
1684
0
      break;
1685
1686
0
    case SCARD_IOCTL_FORGETREADERA:
1687
0
      result = smartcard_ForgetReaderA_Call(ctx, out, operation);
1688
0
      break;
1689
1690
0
    case SCARD_IOCTL_FORGETREADERW:
1691
0
      result = smartcard_ForgetReaderW_Call(ctx, out, operation);
1692
0
      break;
1693
1694
0
    case SCARD_IOCTL_ADDREADERTOGROUPA:
1695
0
      result = smartcard_AddReaderToGroupA_Call(ctx, out, operation);
1696
0
      break;
1697
1698
0
    case SCARD_IOCTL_ADDREADERTOGROUPW:
1699
0
      result = smartcard_AddReaderToGroupW_Call(ctx, out, operation);
1700
0
      break;
1701
1702
0
    case SCARD_IOCTL_REMOVEREADERFROMGROUPA:
1703
0
      result = smartcard_RemoveReaderFromGroupA_Call(ctx, out, operation);
1704
0
      break;
1705
1706
0
    case SCARD_IOCTL_REMOVEREADERFROMGROUPW:
1707
0
      result = smartcard_RemoveReaderFromGroupW_Call(ctx, out, operation);
1708
0
      break;
1709
1710
0
    case SCARD_IOCTL_LOCATECARDSA:
1711
0
      result = smartcard_LocateCardsA_Call(ctx, out, operation);
1712
0
      break;
1713
1714
0
    case SCARD_IOCTL_LOCATECARDSW:
1715
0
      result = smartcard_LocateCardsW_Call(ctx, out, operation);
1716
0
      break;
1717
1718
0
    case SCARD_IOCTL_GETSTATUSCHANGEA:
1719
0
      result = smartcard_GetStatusChangeA_Call(ctx, out, operation);
1720
0
      break;
1721
1722
0
    case SCARD_IOCTL_GETSTATUSCHANGEW:
1723
0
      result = smartcard_GetStatusChangeW_Call(ctx, out, operation);
1724
0
      break;
1725
1726
0
    case SCARD_IOCTL_CANCEL:
1727
0
      result = smartcard_Cancel_Call(ctx, out, operation);
1728
0
      break;
1729
1730
0
    case SCARD_IOCTL_CONNECTA:
1731
0
      result = smartcard_ConnectA_Call(ctx, out, operation);
1732
0
      break;
1733
1734
0
    case SCARD_IOCTL_CONNECTW:
1735
0
      result = smartcard_ConnectW_Call(ctx, out, operation);
1736
0
      break;
1737
1738
0
    case SCARD_IOCTL_RECONNECT:
1739
0
      result = smartcard_Reconnect_Call(ctx, out, operation);
1740
0
      break;
1741
1742
0
    case SCARD_IOCTL_DISCONNECT:
1743
0
      result = smartcard_Disconnect_Call(ctx, out, operation);
1744
0
      break;
1745
1746
0
    case SCARD_IOCTL_BEGINTRANSACTION:
1747
0
      result = smartcard_BeginTransaction_Call(ctx, out, operation);
1748
0
      break;
1749
1750
0
    case SCARD_IOCTL_ENDTRANSACTION:
1751
0
      result = smartcard_EndTransaction_Call(ctx, out, operation);
1752
0
      break;
1753
1754
0
    case SCARD_IOCTL_STATE:
1755
0
      result = smartcard_State_Call(ctx, out, operation);
1756
0
      break;
1757
1758
0
    case SCARD_IOCTL_STATUSA:
1759
0
      result = smartcard_StatusA_Call(ctx, out, operation);
1760
0
      break;
1761
1762
0
    case SCARD_IOCTL_STATUSW:
1763
0
      result = smartcard_StatusW_Call(ctx, out, operation);
1764
0
      break;
1765
1766
0
    case SCARD_IOCTL_TRANSMIT:
1767
0
      result = smartcard_Transmit_Call(ctx, out, operation);
1768
0
      break;
1769
1770
0
    case SCARD_IOCTL_CONTROL:
1771
0
      result = smartcard_Control_Call(ctx, out, operation);
1772
0
      break;
1773
1774
0
    case SCARD_IOCTL_GETATTRIB:
1775
0
      result = smartcard_GetAttrib_Call(ctx, out, operation);
1776
0
      break;
1777
1778
0
    case SCARD_IOCTL_SETATTRIB:
1779
0
      result = smartcard_SetAttrib_Call(ctx, out, operation);
1780
0
      break;
1781
1782
0
    case SCARD_IOCTL_ACCESSSTARTEDEVENT:
1783
0
      result = smartcard_AccessStartedEvent_Call(ctx, out, operation);
1784
0
      break;
1785
1786
0
    case SCARD_IOCTL_LOCATECARDSBYATRA:
1787
0
      result = smartcard_LocateCardsByATRA_Call(ctx, out, operation);
1788
0
      break;
1789
1790
0
    case SCARD_IOCTL_LOCATECARDSBYATRW:
1791
0
      result = smartcard_LocateCardsW_Call(ctx, out, operation);
1792
0
      break;
1793
1794
0
    case SCARD_IOCTL_READCACHEA:
1795
0
      result = smartcard_ReadCacheA_Call(ctx, out, operation);
1796
0
      break;
1797
1798
0
    case SCARD_IOCTL_READCACHEW:
1799
0
      result = smartcard_ReadCacheW_Call(ctx, out, operation);
1800
0
      break;
1801
1802
0
    case SCARD_IOCTL_WRITECACHEA:
1803
0
      result = smartcard_WriteCacheA_Call(ctx, out, operation);
1804
0
      break;
1805
1806
0
    case SCARD_IOCTL_WRITECACHEW:
1807
0
      result = smartcard_WriteCacheW_Call(ctx, out, operation);
1808
0
      break;
1809
1810
0
    case SCARD_IOCTL_GETTRANSMITCOUNT:
1811
0
      result = smartcard_GetTransmitCount_Call(ctx, out, operation);
1812
0
      break;
1813
1814
0
    case SCARD_IOCTL_RELEASETARTEDEVENT:
1815
0
      result = smartcard_ReleaseStartedEvent_Call(ctx, out, operation);
1816
0
      break;
1817
1818
0
    case SCARD_IOCTL_GETREADERICON:
1819
0
      result = smartcard_GetReaderIcon_Call(ctx, out, operation);
1820
0
      break;
1821
1822
0
    case SCARD_IOCTL_GETDEVICETYPEID:
1823
0
      result = smartcard_GetDeviceTypeId_Call(ctx, out, operation);
1824
0
      break;
1825
1826
0
    default:
1827
0
      result = STATUS_UNSUCCESSFUL;
1828
0
      break;
1829
0
  }
1830
1831
  /**
1832
   * [MS-RPCE] 2.2.6.3 Primitive Type Serialization
1833
   * The type MUST be aligned on an 8-byte boundary. If the size of the
1834
   * primitive type is not a multiple of 8 bytes, the data MUST be padded.
1835
   */
1836
1837
0
  if ((ioControlCode != SCARD_IOCTL_ACCESSSTARTEDEVENT) &&
1838
0
      (ioControlCode != SCARD_IOCTL_RELEASETARTEDEVENT))
1839
0
  {
1840
0
    offset = (RDPDR_DEVICE_IO_RESPONSE_LENGTH + RDPDR_DEVICE_IO_CONTROL_RSP_HDR_LENGTH);
1841
0
    const LONG rc = smartcard_pack_write_size_align(out, Stream_GetPosition(out) - offset, 8);
1842
0
    if (rc != SCARD_S_SUCCESS)
1843
0
      result = rc;
1844
0
  }
1845
1846
0
  if ((result != SCARD_S_SUCCESS) && (result != SCARD_E_TIMEOUT) &&
1847
0
      (result != SCARD_E_NO_READERS_AVAILABLE) && (result != SCARD_E_NO_SERVICE) &&
1848
0
      (result != SCARD_W_CACHE_ITEM_NOT_FOUND) && (result != SCARD_W_CACHE_ITEM_STALE))
1849
0
  {
1850
0
    WLog_Print(ctx->log, WLOG_WARN,
1851
0
               "IRP failure: %s (0x%08" PRIX32 "), status: %s (0x%08" PRIX32 ")",
1852
0
               scard_get_ioctl_string(ioControlCode, TRUE), ioControlCode,
1853
0
               SCardGetErrorString(result), WINPR_CXX_COMPAT_CAST(UINT32, result));
1854
0
  }
1855
1856
0
  *pIoStatus = STATUS_SUCCESS;
1857
1858
0
  if ((result & 0xC0000000L) == 0xC0000000L)
1859
0
  {
1860
    /* NTSTATUS error */
1861
0
    *pIoStatus = result;
1862
0
    WLog_Print(ctx->log, WLOG_WARN,
1863
0
               "IRP failure: %s (0x%08" PRIX32 "), ntstatus: 0x%08" PRIX32 "",
1864
0
               scard_get_ioctl_string(ioControlCode, TRUE), ioControlCode,
1865
0
               WINPR_CXX_COMPAT_CAST(UINT32, result));
1866
0
  }
1867
1868
0
  Stream_SealLength(out);
1869
0
  size_t outputBufferLength = Stream_Length(out);
1870
0
  WINPR_ASSERT(outputBufferLength >= RDPDR_DEVICE_IO_RESPONSE_LENGTH + 4U);
1871
0
  outputBufferLength -= (RDPDR_DEVICE_IO_RESPONSE_LENGTH + 4U);
1872
0
  WINPR_ASSERT(outputBufferLength >= RDPDR_DEVICE_IO_RESPONSE_LENGTH);
1873
0
  objectBufferLength = outputBufferLength - RDPDR_DEVICE_IO_RESPONSE_LENGTH;
1874
0
  WINPR_ASSERT(outputBufferLength <= UINT32_MAX);
1875
0
  WINPR_ASSERT(objectBufferLength <= UINT32_MAX);
1876
0
  if (!Stream_SetPosition(out, RDPDR_DEVICE_IO_RESPONSE_LENGTH))
1877
0
    return SCARD_E_BAD_SEEK;
1878
1879
  /* [MS-RDPESC] 3.2.5.2 Processing Incoming Replies
1880
   *
1881
   * if the output buffer is too small, reply with STATUS_BUFFER_TOO_SMALL
1882
   * and a outputBufferLength of 0.
1883
   * The message should then be retransmitted from the server with a doubled
1884
   * buffer size.
1885
   */
1886
0
  if (outputBufferLength > operation->outputBufferLength)
1887
0
  {
1888
0
    WLog_Print(ctx->log, WLOG_WARN,
1889
0
               "IRP warn: expected outputBufferLength %" PRIu32 ", but current limit %" PRIuz
1890
0
               ", respond with STATUS_BUFFER_TOO_SMALL",
1891
0
               operation->outputBufferLength, outputBufferLength);
1892
1893
0
    *pIoStatus = STATUS_BUFFER_TOO_SMALL;
1894
0
    result = *pIoStatus;
1895
0
    outputBufferLength = 0;
1896
0
    objectBufferLength = 0;
1897
0
  }
1898
1899
  /* Device Control Response */
1900
0
  Stream_Write_UINT32(out, (UINT32)outputBufferLength); /* OutputBufferLength (4 bytes) */
1901
0
  smartcard_pack_common_type_header(out);               /* CommonTypeHeader (8 bytes) */
1902
0
  smartcard_pack_private_type_header(
1903
0
      out, (UINT32)objectBufferLength); /* PrivateTypeHeader (8 bytes) */
1904
0
  Stream_Write_INT32(out, result);      /* Result (4 bytes) */
1905
0
  if (!Stream_SetPosition(out, Stream_Length(out)))
1906
0
    return SCARD_E_BAD_SEEK;
1907
0
  return SCARD_S_SUCCESS;
1908
0
}
1909
1910
void context_free(void* arg)
1911
0
{
1912
0
  struct s_scard_context_element* element = arg;
1913
0
  if (!arg)
1914
0
    return;
1915
1916
0
  if (element->fn_free)
1917
0
    element->fn_free(element->context);
1918
0
  free(element);
1919
0
}
1920
1921
scard_call_context* smartcard_call_context_new(const rdpSettings* settings)
1922
0
{
1923
0
  wObject* obj = nullptr;
1924
0
  scard_call_context* ctx = nullptr;
1925
1926
0
  WINPR_ASSERT(settings);
1927
0
  ctx = calloc(1, sizeof(scard_call_context));
1928
0
  if (!ctx)
1929
0
    goto fail;
1930
1931
0
  ctx->log = WLog_Get(SCARD_TAG);
1932
0
  WINPR_ASSERT(ctx->log);
1933
1934
0
  ctx->stopEvent = CreateEventA(nullptr, TRUE, FALSE, nullptr);
1935
0
  if (!ctx->stopEvent)
1936
0
    goto fail;
1937
1938
0
  ctx->names = LinkedList_New();
1939
0
  if (!ctx->names)
1940
0
    goto fail;
1941
1942
0
#if defined(WITH_SMARTCARD_EMULATE)
1943
0
  ctx->useEmulatedCard = freerdp_settings_get_bool(settings, FreeRDP_SmartcardEmulation);
1944
0
#endif
1945
1946
0
  if (ctx->useEmulatedCard)
1947
0
  {
1948
0
#if defined(WITH_SMARTCARD_EMULATE)
1949
0
    ctx->emulation = Emulate_New(settings);
1950
0
    if (!ctx->emulation)
1951
0
      goto fail;
1952
#else
1953
    WLog_Print(ctx->log, WLOG_ERROR, "Smartcard emulation requested, but not supported!");
1954
    goto fail;
1955
#endif
1956
0
  }
1957
0
  else
1958
0
  {
1959
0
    const char* WinSCardModule = freerdp_settings_get_string(settings, FreeRDP_WinSCardModule);
1960
0
    if (WinSCardModule)
1961
0
    {
1962
0
      ctx->hWinSCardLibrary = LoadLibraryX(WinSCardModule);
1963
1964
0
      if (!ctx->hWinSCardLibrary)
1965
0
      {
1966
0
        WLog_Print(ctx->log, WLOG_ERROR, "Failed to load WinSCard library: '%s'",
1967
0
                   WinSCardModule);
1968
0
        goto fail;
1969
0
      }
1970
1971
0
      if (!WinSCard_LoadApiTableFunctions(&ctx->WinSCardApi, ctx->hWinSCardLibrary))
1972
0
        goto fail;
1973
0
      ctx->pWinSCardApi = &ctx->WinSCardApi;
1974
0
    }
1975
0
    else
1976
0
    {
1977
0
      ctx->pWinSCardApi = WinPR_GetSCardApiFunctionTable();
1978
0
    }
1979
1980
0
    if (!ctx->pWinSCardApi)
1981
0
    {
1982
0
      WLog_Print(ctx->log, WLOG_ERROR, "Failed to load WinSCard API!");
1983
0
      goto fail;
1984
0
    }
1985
0
  }
1986
1987
0
  ctx->rgSCardContextList = HashTable_New(FALSE);
1988
0
  if (!ctx->rgSCardContextList)
1989
0
    goto fail;
1990
1991
0
  obj = HashTable_ValueObject(ctx->rgSCardContextList);
1992
0
  WINPR_ASSERT(obj);
1993
0
  obj->fnObjectFree = context_free;
1994
1995
0
  return ctx;
1996
0
fail:
1997
0
  WINPR_PRAGMA_DIAG_PUSH
1998
0
  WINPR_PRAGMA_DIAG_IGNORED_MISMATCHED_DEALLOC
1999
0
  smartcard_call_context_free(ctx);
2000
0
  WINPR_PRAGMA_DIAG_POP
2001
0
  return nullptr;
2002
0
}
2003
2004
void smartcard_call_context_free(scard_call_context* ctx)
2005
0
{
2006
0
  if (!ctx)
2007
0
    return;
2008
2009
0
  smartcard_call_context_signal_stop(ctx, FALSE);
2010
2011
0
  LinkedList_Free(ctx->names);
2012
0
  if (ctx->StartedEvent)
2013
0
  {
2014
0
    WINPR_ASSERT(ctx->useEmulatedCard || ctx->pWinSCardApi);
2015
0
    wrap_raw(ctx, SCardReleaseStartedEvent);
2016
0
  }
2017
2018
0
  if (ctx->useEmulatedCard)
2019
0
  {
2020
0
#ifdef WITH_SMARTCARD_EMULATE
2021
0
    if (ctx->emulation)
2022
0
    {
2023
0
      Emulate_Free(ctx->emulation);
2024
0
      ctx->emulation = nullptr;
2025
0
    }
2026
0
#endif
2027
0
  }
2028
2029
0
  if (ctx->hWinSCardLibrary)
2030
0
  {
2031
0
    ZeroMemory(&ctx->WinSCardApi, sizeof(SCardApiFunctionTable));
2032
0
    FreeLibrary(ctx->hWinSCardLibrary);
2033
0
    ctx->hWinSCardLibrary = nullptr;
2034
0
  }
2035
2036
0
  ctx->pWinSCardApi = nullptr;
2037
2038
0
  HashTable_Free(ctx->rgSCardContextList);
2039
0
  (void)CloseHandle(ctx->stopEvent);
2040
0
  free(ctx);
2041
0
}
2042
2043
BOOL smartcard_call_context_add(scard_call_context* ctx, const char* name)
2044
0
{
2045
0
  WINPR_ASSERT(ctx);
2046
0
  WINPR_ASSERT(name);
2047
0
  return LinkedList_AddLast(ctx->names, name);
2048
0
}
2049
2050
BOOL smartcard_call_cancel_context(scard_call_context* ctx, SCARDCONTEXT hContext)
2051
0
{
2052
0
  WINPR_ASSERT(ctx);
2053
0
  if (wrap(ctx, SCardIsValidContext, hContext) == SCARD_S_SUCCESS)
2054
0
  {
2055
0
    wrap(ctx, SCardCancel, hContext);
2056
0
  }
2057
0
  return TRUE;
2058
0
}
2059
2060
BOOL smartcard_call_release_context(scard_call_context* ctx, SCARDCONTEXT hContext)
2061
0
{
2062
0
  WINPR_ASSERT(ctx);
2063
0
  wrap(ctx, SCardReleaseContext, hContext);
2064
0
  return TRUE;
2065
0
}
2066
2067
BOOL smartcard_call_cancel_all_context(scard_call_context* ctx)
2068
0
{
2069
0
  if (!ctx)
2070
0
    return FALSE;
2071
2072
0
  smartcard_call_context_signal_stop(ctx, FALSE);
2073
0
  HashTable_Clear(ctx->rgSCardContextList);
2074
0
  return TRUE;
2075
0
}
2076
2077
BOOL smarcard_call_set_callbacks(scard_call_context* ctx, void* userdata,
2078
                                 void* (*fn_new)(void*, SCARDCONTEXT), void (*fn_free)(void*))
2079
0
{
2080
0
  WINPR_ASSERT(ctx);
2081
0
  ctx->userdata = userdata;
2082
0
  ctx->fn_new = fn_new;
2083
0
  ctx->fn_free = fn_free;
2084
0
  return TRUE;
2085
0
}
2086
2087
void* smartcard_call_get_context(scard_call_context* ctx, SCARDCONTEXT hContext)
2088
0
{
2089
0
  struct s_scard_context_element* element = nullptr;
2090
2091
0
  WINPR_ASSERT(ctx);
2092
0
  element = HashTable_GetItemValue(ctx->rgSCardContextList, (void*)hContext);
2093
0
  if (!element)
2094
0
    return nullptr;
2095
0
  return element->context;
2096
0
}
2097
2098
BOOL smartcard_call_is_configured(scard_call_context* ctx)
2099
0
{
2100
0
  WINPR_ASSERT(ctx);
2101
2102
0
#if defined(WITH_SMARTCARD_EMULATE)
2103
0
  if (ctx->useEmulatedCard)
2104
0
    return Emulate_IsConfigured(ctx->emulation);
2105
0
#endif
2106
2107
0
  return FALSE;
2108
0
}
2109
2110
BOOL smartcard_call_context_signal_stop(scard_call_context* ctx, BOOL reset)
2111
0
{
2112
0
  WINPR_ASSERT(ctx);
2113
2114
0
  if (!ctx->stopEvent)
2115
0
    return TRUE;
2116
2117
0
  if (reset)
2118
0
    return ResetEvent(ctx->stopEvent);
2119
0
  else
2120
0
    return SetEvent(ctx->stopEvent);
2121
0
}