Coverage Report

Created: 2026-07-16 07:09

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/FreeRDP/libfreerdp/utils/smartcard_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
  rdpContext* context;
95
};
96
97
struct s_scard_context_element
98
{
99
  void* context;
100
  void (*fn_free)(void*);
101
};
102
103
static void context_free(void* arg);
104
105
static LONG smartcard_EstablishContext_Call(scard_call_context* smartcard, wStream* out,
106
                                            SMARTCARD_OPERATION* operation)
107
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
  LONG 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
  *mszReaders = nullptr;
343
0
  rc = filter_device_by_name_a(list, &readers, cchReaders);
344
345
0
  *mszReaders = ConvertMszUtf8NToWCharAlloc(readers, rc, nullptr);
346
0
  if (!*mszReaders)
347
0
    rc = 0;
348
349
0
  free(readers);
350
0
  return rc;
351
0
}
352
353
static LONG smartcard_ListReadersA_Call(scard_call_context* smartcard, wStream* out,
354
                                        SMARTCARD_OPERATION* operation)
355
0
{
356
0
  ListReaders_Return ret = WINPR_C_ARRAY_INIT;
357
0
  LPSTR mszReaders = nullptr;
358
359
0
  WINPR_ASSERT(smartcard);
360
0
  WINPR_ASSERT(out);
361
0
  WINPR_ASSERT(operation);
362
363
0
  ListReaders_Call* call = &operation->call.listReaders;
364
0
  DWORD cchReaders = SCARD_AUTOALLOCATE;
365
0
  LONG status = ret.ReturnCode = wrap(smartcard, SCardListReadersA, operation->hContext,
366
0
                                      (LPCSTR)call->mszGroups, (LPSTR)&mszReaders, &cchReaders);
367
0
  if (status == SCARD_S_SUCCESS)
368
0
  {
369
0
    if (cchReaders == SCARD_AUTOALLOCATE)
370
0
      status = SCARD_F_UNKNOWN_ERROR;
371
0
  }
372
373
0
  if (status != SCARD_S_SUCCESS)
374
0
  {
375
0
    (void)scard_log_status_error_wlog(smartcard->log, "SCardListReadersA", status);
376
0
    return smartcard_pack_list_readers_return(out, &ret, FALSE);
377
0
  }
378
379
0
  void* original = mszReaders;
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 (original)
386
0
    wrap(smartcard, SCardFreeMemory, operation->hContext, original);
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
  void* original = mszReaders.pb;
438
0
  cchReaders = filter_device_by_name_w(smartcard->names, &mszReaders.pw, cchReaders);
439
0
  ret.msz = mszReaders.pb;
440
0
  ret.cBytes = cchReaders * sizeof(WCHAR);
441
0
  status = smartcard_pack_list_readers_return(out, &ret, TRUE);
442
443
0
  if (original)
444
0
    wrap(smartcard, SCardFreeMemory, operation->hContext, original);
445
446
0
  if (status != SCARD_S_SUCCESS)
447
0
    return status;
448
449
0
  return ret.ReturnCode;
450
0
}
451
452
static LONG smartcard_IntroduceReaderGroupA_Call(scard_call_context* smartcard,
453
                                                 WINPR_ATTR_UNUSED wStream* out,
454
                                                 SMARTCARD_OPERATION* operation)
455
0
{
456
0
  Long_Return ret = WINPR_C_ARRAY_INIT;
457
0
  ContextAndStringA_Call* call = nullptr;
458
459
0
  WINPR_ASSERT(smartcard);
460
0
  WINPR_ASSERT(out);
461
0
  WINPR_ASSERT(operation);
462
463
0
  call = &operation->call.contextAndStringA;
464
0
  ret.ReturnCode = wrap(smartcard, SCardIntroduceReaderGroupA, operation->hContext, call->sz);
465
0
  scard_log_status_error_wlog(smartcard->log, "SCardIntroduceReaderGroupA", ret.ReturnCode);
466
0
  smartcard_trace_long_return_int(smartcard->log, &ret, "IntroduceReaderGroupA");
467
0
  return ret.ReturnCode;
468
0
}
469
470
static LONG smartcard_IntroduceReaderGroupW_Call(scard_call_context* smartcard,
471
                                                 WINPR_ATTR_UNUSED wStream* out,
472
                                                 SMARTCARD_OPERATION* operation)
473
0
{
474
0
  Long_Return ret = WINPR_C_ARRAY_INIT;
475
0
  ContextAndStringW_Call* call = nullptr;
476
477
0
  WINPR_ASSERT(smartcard);
478
0
  WINPR_ASSERT(out);
479
0
  WINPR_ASSERT(operation);
480
481
0
  call = &operation->call.contextAndStringW;
482
0
  ret.ReturnCode = wrap(smartcard, SCardIntroduceReaderGroupW, operation->hContext, call->sz);
483
0
  scard_log_status_error_wlog(smartcard->log, "SCardIntroduceReaderGroupW", ret.ReturnCode);
484
0
  smartcard_trace_long_return_int(smartcard->log, &ret, "IntroduceReaderGroupW");
485
0
  return ret.ReturnCode;
486
0
}
487
488
static LONG smartcard_IntroduceReaderA_Call(scard_call_context* smartcard,
489
                                            WINPR_ATTR_UNUSED wStream* out,
490
                                            SMARTCARD_OPERATION* operation)
491
0
{
492
0
  Long_Return ret = WINPR_C_ARRAY_INIT;
493
0
  ContextAndTwoStringA_Call* call = nullptr;
494
495
0
  WINPR_ASSERT(smartcard);
496
0
  WINPR_ASSERT(out);
497
0
  WINPR_ASSERT(operation);
498
499
0
  call = &operation->call.contextAndTwoStringA;
500
0
  ret.ReturnCode =
501
0
      wrap(smartcard, SCardIntroduceReaderA, operation->hContext, call->sz1, call->sz2);
502
0
  scard_log_status_error_wlog(smartcard->log, "SCardIntroduceReaderA", ret.ReturnCode);
503
0
  smartcard_trace_long_return_int(smartcard->log, &ret, "IntroduceReaderA");
504
0
  return ret.ReturnCode;
505
0
}
506
507
static LONG smartcard_IntroduceReaderW_Call(scard_call_context* smartcard,
508
                                            WINPR_ATTR_UNUSED wStream* out,
509
                                            SMARTCARD_OPERATION* operation)
510
0
{
511
0
  Long_Return ret = WINPR_C_ARRAY_INIT;
512
0
  ContextAndTwoStringW_Call* call = nullptr;
513
514
0
  WINPR_ASSERT(smartcard);
515
0
  WINPR_ASSERT(out);
516
0
  WINPR_ASSERT(operation);
517
518
0
  call = &operation->call.contextAndTwoStringW;
519
0
  ret.ReturnCode =
520
0
      wrap(smartcard, SCardIntroduceReaderW, operation->hContext, call->sz1, call->sz2);
521
0
  scard_log_status_error_wlog(smartcard->log, "SCardIntroduceReaderW", ret.ReturnCode);
522
0
  smartcard_trace_long_return_int(smartcard->log, &ret, "IntroduceReaderW");
523
0
  return ret.ReturnCode;
524
0
}
525
526
static LONG smartcard_ForgetReaderA_Call(scard_call_context* smartcard,
527
                                         WINPR_ATTR_UNUSED wStream* out,
528
                                         SMARTCARD_OPERATION* operation)
529
0
{
530
0
  Long_Return ret = WINPR_C_ARRAY_INIT;
531
0
  ContextAndStringA_Call* call = nullptr;
532
533
0
  WINPR_ASSERT(smartcard);
534
0
  WINPR_ASSERT(out);
535
0
  WINPR_ASSERT(operation);
536
537
0
  call = &operation->call.contextAndStringA;
538
0
  ret.ReturnCode = wrap(smartcard, SCardForgetReaderA, operation->hContext, call->sz);
539
0
  scard_log_status_error_wlog(smartcard->log, "SCardForgetReaderA", ret.ReturnCode);
540
0
  smartcard_trace_long_return_int(smartcard->log, &ret, "SCardForgetReaderA");
541
0
  return ret.ReturnCode;
542
0
}
543
544
static LONG smartcard_ForgetReaderW_Call(scard_call_context* smartcard,
545
                                         WINPR_ATTR_UNUSED wStream* out,
546
                                         SMARTCARD_OPERATION* operation)
547
0
{
548
0
  Long_Return ret = WINPR_C_ARRAY_INIT;
549
0
  ContextAndStringW_Call* call = nullptr;
550
551
0
  WINPR_ASSERT(smartcard);
552
0
  WINPR_ASSERT(out);
553
0
  WINPR_ASSERT(operation);
554
555
0
  call = &operation->call.contextAndStringW;
556
0
  ret.ReturnCode = wrap(smartcard, SCardForgetReaderW, operation->hContext, call->sz);
557
0
  scard_log_status_error_wlog(smartcard->log, "SCardForgetReaderW", ret.ReturnCode);
558
0
  smartcard_trace_long_return_int(smartcard->log, &ret, "SCardForgetReaderW");
559
0
  return ret.ReturnCode;
560
0
}
561
562
static LONG smartcard_AddReaderToGroupA_Call(scard_call_context* smartcard,
563
                                             WINPR_ATTR_UNUSED wStream* out,
564
                                             SMARTCARD_OPERATION* operation)
565
0
{
566
0
  Long_Return ret = WINPR_C_ARRAY_INIT;
567
0
  ContextAndTwoStringA_Call* call = nullptr;
568
569
0
  WINPR_ASSERT(smartcard);
570
0
  WINPR_ASSERT(out);
571
0
  WINPR_ASSERT(operation);
572
573
0
  call = &operation->call.contextAndTwoStringA;
574
0
  ret.ReturnCode =
575
0
      wrap(smartcard, SCardAddReaderToGroupA, operation->hContext, call->sz1, call->sz2);
576
0
  scard_log_status_error_wlog(smartcard->log, "SCardAddReaderToGroupA", ret.ReturnCode);
577
0
  smartcard_trace_long_return_int(smartcard->log, &ret, "SCardAddReaderToGroupA");
578
0
  return ret.ReturnCode;
579
0
}
580
581
static LONG smartcard_AddReaderToGroupW_Call(scard_call_context* smartcard,
582
                                             WINPR_ATTR_UNUSED wStream* out,
583
                                             SMARTCARD_OPERATION* operation)
584
0
{
585
0
  Long_Return ret = WINPR_C_ARRAY_INIT;
586
0
  ContextAndTwoStringW_Call* call = nullptr;
587
588
0
  WINPR_ASSERT(smartcard);
589
0
  WINPR_ASSERT(out);
590
0
  WINPR_ASSERT(operation);
591
592
0
  call = &operation->call.contextAndTwoStringW;
593
0
  ret.ReturnCode =
594
0
      wrap(smartcard, SCardAddReaderToGroupW, operation->hContext, call->sz1, call->sz2);
595
0
  scard_log_status_error_wlog(smartcard->log, "SCardAddReaderToGroupW", ret.ReturnCode);
596
0
  smartcard_trace_long_return_int(smartcard->log, &ret, "SCardAddReaderToGroupA");
597
0
  return ret.ReturnCode;
598
0
}
599
600
static LONG smartcard_RemoveReaderFromGroupA_Call(scard_call_context* smartcard,
601
                                                  WINPR_ATTR_UNUSED wStream* out,
602
                                                  SMARTCARD_OPERATION* operation)
603
0
{
604
0
  Long_Return ret = WINPR_C_ARRAY_INIT;
605
0
  ContextAndTwoStringA_Call* call = nullptr;
606
607
0
  WINPR_ASSERT(smartcard);
608
0
  WINPR_ASSERT(out);
609
0
  WINPR_ASSERT(operation);
610
611
0
  call = &operation->call.contextAndTwoStringA;
612
0
  ret.ReturnCode =
613
0
      wrap(smartcard, SCardRemoveReaderFromGroupA, operation->hContext, call->sz1, call->sz2);
614
0
  scard_log_status_error_wlog(smartcard->log, "SCardRemoveReaderFromGroupA", ret.ReturnCode);
615
0
  smartcard_trace_long_return_int(smartcard->log, &ret, "SCardRemoveReaderFromGroupA");
616
0
  return ret.ReturnCode;
617
0
}
618
619
static LONG smartcard_RemoveReaderFromGroupW_Call(scard_call_context* smartcard,
620
                                                  WINPR_ATTR_UNUSED wStream* out,
621
                                                  SMARTCARD_OPERATION* operation)
622
0
{
623
0
  Long_Return ret = WINPR_C_ARRAY_INIT;
624
0
  ContextAndTwoStringW_Call* call = nullptr;
625
626
0
  WINPR_ASSERT(smartcard);
627
0
  WINPR_ASSERT(out);
628
0
  WINPR_ASSERT(operation);
629
630
0
  call = &operation->call.contextAndTwoStringW;
631
0
  ret.ReturnCode =
632
0
      wrap(smartcard, SCardRemoveReaderFromGroupW, operation->hContext, call->sz1, call->sz2);
633
0
  scard_log_status_error_wlog(smartcard->log, "SCardRemoveReaderFromGroupW", ret.ReturnCode);
634
0
  smartcard_trace_long_return_int(smartcard->log, &ret, "SCardRemoveReaderFromGroupW");
635
0
  return ret.ReturnCode;
636
0
}
637
638
static LONG smartcard_LocateCardsA_Call(scard_call_context* smartcard, wStream* out,
639
                                        SMARTCARD_OPERATION* operation)
640
0
{
641
0
  LONG status = 0;
642
0
  LocateCards_Return ret = WINPR_C_ARRAY_INIT;
643
0
  LocateCardsA_Call* call = nullptr;
644
645
0
  WINPR_ASSERT(smartcard);
646
0
  WINPR_ASSERT(operation);
647
648
0
  call = &operation->call.locateCardsA;
649
650
0
  ret.ReturnCode = wrap(smartcard, SCardLocateCardsA, operation->hContext, call->mszCards,
651
0
                        call->rgReaderStates, call->cReaders);
652
0
  scard_log_status_error_wlog(smartcard->log, "SCardLocateCardsA", ret.ReturnCode);
653
0
  ret.rgReaderStates = nullptr;
654
655
0
  if (call->cReaders > 0)
656
0
  {
657
0
    ret.rgReaderStates =
658
0
        (ReaderState_Return*)calloc(call->cReaders, sizeof(ReaderState_Return));
659
660
0
    if (!ret.rgReaderStates)
661
0
      return STATUS_NO_MEMORY;
662
0
    ret.cReaders = call->cReaders;
663
0
  }
664
665
0
  for (UINT32 x = 0; x < ret.cReaders; x++)
666
0
  {
667
0
    ret.rgReaderStates[x].dwCurrentState = call->rgReaderStates[x].dwCurrentState;
668
0
    ret.rgReaderStates[x].dwEventState = call->rgReaderStates[x].dwEventState;
669
0
    ret.rgReaderStates[x].cbAtr = call->rgReaderStates[x].cbAtr;
670
0
    CopyMemory(&(ret.rgReaderStates[x].rgbAtr), &(call->rgReaderStates[x].rgbAtr),
671
0
               sizeof(ret.rgReaderStates[x].rgbAtr));
672
0
  }
673
674
0
  status = smartcard_pack_locate_cards_return(out, &ret);
675
676
0
  if (status != SCARD_S_SUCCESS)
677
0
    return status;
678
679
0
  return ret.ReturnCode;
680
0
}
681
682
static LONG smartcard_LocateCardsW_Call(scard_call_context* smartcard, wStream* out,
683
                                        SMARTCARD_OPERATION* operation)
684
0
{
685
0
  LONG status = 0;
686
0
  LocateCards_Return ret = WINPR_C_ARRAY_INIT;
687
0
  LocateCardsW_Call* call = nullptr;
688
689
0
  WINPR_ASSERT(smartcard);
690
0
  WINPR_ASSERT(operation);
691
692
0
  call = &operation->call.locateCardsW;
693
694
0
  ret.ReturnCode = wrap(smartcard, SCardLocateCardsW, operation->hContext, call->mszCards,
695
0
                        call->rgReaderStates, call->cReaders);
696
0
  scard_log_status_error_wlog(smartcard->log, "SCardLocateCardsW", ret.ReturnCode);
697
0
  ret.rgReaderStates = nullptr;
698
699
0
  if (call->cReaders > 0)
700
0
  {
701
0
    ret.rgReaderStates =
702
0
        (ReaderState_Return*)calloc(call->cReaders, sizeof(ReaderState_Return));
703
704
0
    if (!ret.rgReaderStates)
705
0
      return STATUS_NO_MEMORY;
706
0
    ret.cReaders = call->cReaders;
707
0
  }
708
709
0
  for (UINT32 x = 0; x < ret.cReaders; x++)
710
0
  {
711
0
    ret.rgReaderStates[x].dwCurrentState = call->rgReaderStates[x].dwCurrentState;
712
0
    ret.rgReaderStates[x].dwEventState = call->rgReaderStates[x].dwEventState;
713
0
    ret.rgReaderStates[x].cbAtr = call->rgReaderStates[x].cbAtr;
714
0
    CopyMemory(&(ret.rgReaderStates[x].rgbAtr), &(call->rgReaderStates[x].rgbAtr),
715
0
               sizeof(ret.rgReaderStates[x].rgbAtr));
716
0
  }
717
718
0
  status = smartcard_pack_locate_cards_return(out, &ret);
719
720
0
  if (status != SCARD_S_SUCCESS)
721
0
    return status;
722
723
0
  return ret.ReturnCode;
724
0
}
725
726
static LONG smartcard_ReadCacheA_Call(scard_call_context* smartcard, wStream* out,
727
                                      SMARTCARD_OPERATION* operation)
728
0
{
729
0
  LONG status = 0;
730
0
  BOOL autoalloc = 0;
731
0
  ReadCache_Return ret = WINPR_C_ARRAY_INIT;
732
0
  ReadCacheA_Call* call = nullptr;
733
734
0
  WINPR_ASSERT(smartcard);
735
0
  WINPR_ASSERT(out);
736
0
  WINPR_ASSERT(operation);
737
738
0
  call = &operation->call.readCacheA;
739
0
  autoalloc = (call->Common.cbDataLen == SCARD_AUTOALLOCATE);
740
741
0
  if (!call->Common.fPbDataIsNULL)
742
0
  {
743
0
    if (!autoalloc)
744
0
    {
745
0
      ret.pbData = malloc(call->Common.cbDataLen);
746
0
      if (!ret.pbData)
747
0
        return SCARD_F_INTERNAL_ERROR;
748
0
    }
749
0
    ret.cbDataLen = call->Common.cbDataLen;
750
0
  }
751
752
0
  if (autoalloc)
753
0
    ret.ReturnCode = wrap(smartcard, SCardReadCacheA, operation->hContext,
754
0
                          call->Common.CardIdentifier, call->Common.FreshnessCounter,
755
0
                          call->szLookupName, (BYTE*)&ret.pbData, &ret.cbDataLen);
756
0
  else
757
0
    ret.ReturnCode =
758
0
        wrap(smartcard, SCardReadCacheA, operation->hContext, call->Common.CardIdentifier,
759
0
             call->Common.FreshnessCounter, call->szLookupName, ret.pbData, &ret.cbDataLen);
760
761
0
  WLog_Print(smartcard->log, WLOG_TRACE, "key=%s, length=%" PRIu32, call->szLookupName,
762
0
             ret.cbDataLen);
763
764
0
  if ((ret.ReturnCode != SCARD_W_CACHE_ITEM_NOT_FOUND) &&
765
0
      (ret.ReturnCode != SCARD_W_CACHE_ITEM_STALE))
766
0
  {
767
0
    scard_log_status_error_wlog(smartcard->log, "SCardReadCacheA", ret.ReturnCode);
768
0
  }
769
770
0
  status = smartcard_pack_read_cache_return(out, &ret);
771
0
  if (autoalloc)
772
0
    wrap(smartcard, SCardFreeMemory, operation->hContext, ret.pbData);
773
0
  else
774
0
    free(ret.pbData);
775
0
  if (status != SCARD_S_SUCCESS)
776
0
    return status;
777
778
0
  return ret.ReturnCode;
779
0
}
780
781
static LONG smartcard_ReadCacheW_Call(scard_call_context* smartcard, wStream* out,
782
                                      SMARTCARD_OPERATION* operation)
783
0
{
784
0
  LONG status = 0;
785
0
  ReadCache_Return ret = WINPR_C_ARRAY_INIT;
786
0
  ReadCacheW_Call* call = nullptr;
787
788
0
  WINPR_ASSERT(smartcard);
789
0
  WINPR_ASSERT(out);
790
0
  WINPR_ASSERT(operation);
791
792
0
  call = &operation->call.readCacheW;
793
794
0
  if (!call->Common.fPbDataIsNULL)
795
0
    ret.cbDataLen = SCARD_AUTOALLOCATE;
796
797
0
  ret.ReturnCode =
798
0
      wrap(smartcard, SCardReadCacheW, operation->hContext, call->Common.CardIdentifier,
799
0
           call->Common.FreshnessCounter, call->szLookupName, (BYTE*)&ret.pbData, &ret.cbDataLen);
800
801
0
  if (WLog_IsLevelActive(smartcard->log, WLOG_TRACE))
802
0
  {
803
0
    char buffer[128] = WINPR_C_ARRAY_INIT;
804
0
    (void)ConvertWCharToUtf8(call->szLookupName, buffer, sizeof(buffer));
805
0
    WLog_Print(smartcard->log, WLOG_TRACE, "key=%s, length=%" PRIu32, buffer, ret.cbDataLen);
806
0
  }
807
0
  if ((ret.ReturnCode != SCARD_W_CACHE_ITEM_NOT_FOUND) &&
808
0
      (ret.ReturnCode != SCARD_W_CACHE_ITEM_STALE))
809
0
  {
810
0
    scard_log_status_error_wlog(smartcard->log, "SCardReadCacheW", ret.ReturnCode);
811
0
  }
812
813
0
  status = smartcard_pack_read_cache_return(out, &ret);
814
815
0
  wrap(smartcard, SCardFreeMemory, operation->hContext, ret.pbData);
816
817
0
  if (status != SCARD_S_SUCCESS)
818
0
    return status;
819
820
0
  return ret.ReturnCode;
821
0
}
822
823
static LONG smartcard_WriteCacheA_Call(scard_call_context* smartcard,
824
                                       WINPR_ATTR_UNUSED wStream* out,
825
                                       SMARTCARD_OPERATION* operation)
826
0
{
827
0
  Long_Return ret = WINPR_C_ARRAY_INIT;
828
0
  WriteCacheA_Call* call = nullptr;
829
830
0
  WINPR_ASSERT(smartcard);
831
0
  WINPR_ASSERT(out);
832
0
  WINPR_ASSERT(operation);
833
834
0
  call = &operation->call.writeCacheA;
835
836
0
  ret.ReturnCode = wrap(smartcard, SCardWriteCacheA, operation->hContext,
837
0
                        call->Common.CardIdentifier, call->Common.FreshnessCounter,
838
0
                        call->szLookupName, call->Common.pbData, call->Common.cbDataLen);
839
0
  scard_log_status_error_wlog(smartcard->log, "SCardWriteCacheA", ret.ReturnCode);
840
0
  smartcard_trace_long_return_int(smartcard->log, &ret, "SCardWriteCacheA");
841
0
  WLog_Print(smartcard->log, WLOG_TRACE, "key=%s, length=%" PRIu32, call->szLookupName,
842
0
             call->Common.cbDataLen);
843
0
  return ret.ReturnCode;
844
0
}
845
846
static LONG smartcard_WriteCacheW_Call(scard_call_context* smartcard,
847
                                       WINPR_ATTR_UNUSED wStream* out,
848
                                       SMARTCARD_OPERATION* operation)
849
0
{
850
0
  Long_Return ret = WINPR_C_ARRAY_INIT;
851
0
  WriteCacheW_Call* call = nullptr;
852
853
0
  WINPR_ASSERT(smartcard);
854
0
  WINPR_ASSERT(out);
855
0
  WINPR_ASSERT(operation);
856
857
0
  call = &operation->call.writeCacheW;
858
859
0
  ret.ReturnCode = wrap(smartcard, SCardWriteCacheW, operation->hContext,
860
0
                        call->Common.CardIdentifier, call->Common.FreshnessCounter,
861
0
                        call->szLookupName, call->Common.pbData, call->Common.cbDataLen);
862
0
  scard_log_status_error_wlog(smartcard->log, "SCardWriteCacheW", ret.ReturnCode);
863
0
  smartcard_trace_long_return_int(smartcard->log, &ret, "SCardWriteCacheW");
864
865
0
  if (WLog_IsLevelActive(smartcard->log, WLOG_TRACE))
866
0
  {
867
0
    char buffer[128] = WINPR_C_ARRAY_INIT;
868
0
    (void)ConvertWCharToUtf8(call->szLookupName, buffer, sizeof(buffer));
869
0
    WLog_Print(smartcard->log, WLOG_TRACE, "key=%s, length=%" PRIu32, buffer,
870
0
               call->Common.cbDataLen);
871
0
  }
872
0
  return ret.ReturnCode;
873
0
}
874
875
static LONG smartcard_GetTransmitCount_Call(scard_call_context* smartcard, wStream* out,
876
                                            SMARTCARD_OPERATION* operation)
877
0
{
878
0
  LONG status = 0;
879
0
  GetTransmitCount_Return ret = WINPR_C_ARRAY_INIT;
880
881
0
  WINPR_ASSERT(smartcard);
882
0
  WINPR_ASSERT(out);
883
0
  WINPR_ASSERT(operation);
884
885
0
  ret.ReturnCode = wrap(smartcard, SCardGetTransmitCount, operation->hCard, &ret.cTransmitCount);
886
0
  scard_log_status_error_wlog(smartcard->log, "SCardGetTransmitCount", ret.ReturnCode);
887
0
  status = smartcard_pack_get_transmit_count_return(out, &ret);
888
0
  if (status != SCARD_S_SUCCESS)
889
0
    return status;
890
891
0
  return ret.ReturnCode;
892
0
}
893
894
static LONG smartcard_ReleaseStartedEvent_Call(scard_call_context* smartcard, wStream* out,
895
                                               SMARTCARD_OPERATION* operation)
896
0
{
897
0
  WINPR_UNUSED(smartcard);
898
0
  WINPR_UNUSED(out);
899
0
  WINPR_UNUSED(operation);
900
901
0
  WLog_Print(smartcard->log, WLOG_WARN,
902
0
             "According to [MS-RDPESC] 3.1.4 Message Processing Events and Sequencing Rules "
903
0
             "this is not supported?!?");
904
0
  return SCARD_E_UNSUPPORTED_FEATURE;
905
0
}
906
907
static LONG smartcard_GetReaderIcon_Call(scard_call_context* smartcard, wStream* out,
908
                                         SMARTCARD_OPERATION* operation)
909
0
{
910
0
  LONG status = 0;
911
0
  GetReaderIcon_Return ret = WINPR_C_ARRAY_INIT;
912
0
  GetReaderIcon_Call* call = nullptr;
913
914
0
  WINPR_ASSERT(smartcard);
915
0
  WINPR_ASSERT(out);
916
0
  WINPR_ASSERT(operation);
917
918
0
  call = &operation->call.getReaderIcon;
919
920
0
  ret.cbDataLen = SCARD_AUTOALLOCATE;
921
0
  ret.ReturnCode = wrap(smartcard, SCardGetReaderIconW, operation->hContext, call->szReaderName,
922
0
                        (LPBYTE)&ret.pbData, &ret.cbDataLen);
923
0
  scard_log_status_error_wlog(smartcard->log, "SCardGetReaderIconW", ret.ReturnCode);
924
0
  if ((ret.ReturnCode == SCARD_S_SUCCESS) && (ret.cbDataLen == SCARD_AUTOALLOCATE))
925
0
    return SCARD_F_UNKNOWN_ERROR;
926
927
0
  status = smartcard_pack_get_reader_icon_return(out, &ret);
928
0
  wrap(smartcard, SCardFreeMemory, operation->hContext, ret.pbData);
929
0
  if (status != SCARD_S_SUCCESS)
930
0
    return status;
931
932
0
  return ret.ReturnCode;
933
0
}
934
935
static LONG smartcard_GetDeviceTypeId_Call(scard_call_context* smartcard, wStream* out,
936
                                           SMARTCARD_OPERATION* operation)
937
0
{
938
0
  LONG status = 0;
939
0
  GetDeviceTypeId_Return ret = WINPR_C_ARRAY_INIT;
940
0
  GetDeviceTypeId_Call* call = nullptr;
941
942
0
  WINPR_ASSERT(smartcard);
943
0
  WINPR_ASSERT(out);
944
0
  WINPR_ASSERT(operation);
945
946
0
  call = &operation->call.getDeviceTypeId;
947
948
0
  ret.ReturnCode = wrap(smartcard, SCardGetDeviceTypeIdW, operation->hContext, call->szReaderName,
949
0
                        &ret.dwDeviceId);
950
0
  scard_log_status_error_wlog(smartcard->log, "SCardGetDeviceTypeIdW", ret.ReturnCode);
951
952
0
  status = smartcard_pack_device_type_id_return(out, &ret);
953
0
  if (status != SCARD_S_SUCCESS)
954
0
    return status;
955
956
0
  return ret.ReturnCode;
957
0
}
958
959
static BOOL smartcard_context_was_aborted(scard_call_context* smartcard)
960
0
{
961
0
  WINPR_ASSERT(smartcard);
962
963
0
  HANDLE handles[] = { smartcard->stopEvent, freerdp_abort_event(smartcard->context) };
964
0
  const DWORD rc = WaitForMultipleObjects(ARRAYSIZE(handles), handles, FALSE, 0);
965
0
  return (rc >= WAIT_OBJECT_0) && (rc <= WAIT_OBJECT_0 + ARRAYSIZE(handles));
966
0
}
967
968
static LONG smartcard_GetStatusChangeA_Call(scard_call_context* smartcard, wStream* out,
969
                                            SMARTCARD_OPERATION* operation)
970
0
{
971
0
  LONG status = STATUS_NO_MEMORY;
972
0
  DWORD dwTimeOut = 0;
973
0
  const DWORD dwTimeStep = 100;
974
0
  GetStatusChange_Return ret = WINPR_C_ARRAY_INIT;
975
0
  GetStatusChangeA_Call* call = nullptr;
976
0
  LPSCARD_READERSTATEA rgReaderStates = nullptr;
977
978
0
  WINPR_ASSERT(smartcard);
979
0
  WINPR_ASSERT(out);
980
0
  WINPR_ASSERT(operation);
981
982
0
  call = &operation->call.getStatusChangeA;
983
0
  dwTimeOut = call->dwTimeOut;
984
985
0
  if (call->cReaders > 0)
986
0
  {
987
0
    rgReaderStates = calloc(call->cReaders, sizeof(SCARD_READERSTATEA));
988
0
    ret.rgReaderStates =
989
0
        (ReaderState_Return*)calloc(call->cReaders, sizeof(ReaderState_Return));
990
0
    if (!rgReaderStates || !ret.rgReaderStates)
991
0
      goto fail;
992
993
0
    ret.cReaders = call->cReaders;
994
0
  }
995
996
0
  for (UINT32 x = 0; x < MAX(1, dwTimeOut);)
997
0
  {
998
0
    if (call->cReaders > 0)
999
0
      memcpy(rgReaderStates, call->rgReaderStates,
1000
0
             call->cReaders * sizeof(SCARD_READERSTATEA));
1001
0
    ret.ReturnCode = wrap(smartcard, SCardGetStatusChangeA, operation->hContext,
1002
0
                          MIN(dwTimeOut, dwTimeStep), rgReaderStates, call->cReaders);
1003
0
    if (ret.ReturnCode != SCARD_E_TIMEOUT)
1004
0
      break;
1005
0
    if (smartcard_context_was_aborted(smartcard))
1006
0
      break;
1007
0
    if (dwTimeOut != INFINITE)
1008
0
      x += dwTimeStep;
1009
0
  }
1010
0
  scard_log_status_error_wlog(smartcard->log, "SCardGetStatusChangeA", ret.ReturnCode);
1011
1012
0
  for (UINT32 index = 0; index < ret.cReaders; index++)
1013
0
  {
1014
0
    const SCARD_READERSTATEA* cur = &rgReaderStates[index];
1015
0
    ReaderState_Return* rout = &ret.rgReaderStates[index];
1016
1017
0
    rout->dwCurrentState = cur->dwCurrentState;
1018
0
    rout->dwEventState = cur->dwEventState;
1019
0
    rout->cbAtr = cur->cbAtr;
1020
0
    CopyMemory(&(rout->rgbAtr), cur->rgbAtr, sizeof(rout->rgbAtr));
1021
0
  }
1022
1023
0
  status = smartcard_pack_get_status_change_return(out, &ret, FALSE);
1024
0
fail:
1025
0
  free(ret.rgReaderStates);
1026
0
  free(rgReaderStates);
1027
0
  if (status != SCARD_S_SUCCESS)
1028
0
    return status;
1029
0
  return ret.ReturnCode;
1030
0
}
1031
1032
static LONG smartcard_GetStatusChangeW_Call(scard_call_context* smartcard, wStream* out,
1033
                                            SMARTCARD_OPERATION* operation)
1034
0
{
1035
0
  LONG status = STATUS_NO_MEMORY;
1036
0
  DWORD dwTimeOut = 0;
1037
0
  const DWORD dwTimeStep = 100;
1038
0
  GetStatusChange_Return ret = WINPR_C_ARRAY_INIT;
1039
0
  LPSCARD_READERSTATEW rgReaderStates = nullptr;
1040
1041
0
  WINPR_ASSERT(smartcard);
1042
0
  WINPR_ASSERT(out);
1043
0
  WINPR_ASSERT(operation);
1044
1045
0
  GetStatusChangeW_Call* call = &operation->call.getStatusChangeW;
1046
0
  dwTimeOut = call->dwTimeOut;
1047
1048
0
  if (call->cReaders > 0)
1049
0
  {
1050
0
    rgReaderStates = calloc(call->cReaders, sizeof(SCARD_READERSTATEW));
1051
0
    ret.rgReaderStates =
1052
0
        (ReaderState_Return*)calloc(call->cReaders, sizeof(ReaderState_Return));
1053
0
    if (!rgReaderStates || !ret.rgReaderStates)
1054
0
      goto fail;
1055
1056
0
    ret.cReaders = call->cReaders;
1057
0
  }
1058
1059
0
  for (UINT32 x = 0; x < MAX(1, dwTimeOut);)
1060
0
  {
1061
0
    if (call->cReaders > 0)
1062
0
      memcpy(rgReaderStates, call->rgReaderStates,
1063
0
             call->cReaders * sizeof(SCARD_READERSTATEW));
1064
0
    {
1065
0
      ret.ReturnCode = wrap(smartcard, SCardGetStatusChangeW, operation->hContext,
1066
0
                            MIN(dwTimeOut, dwTimeStep), rgReaderStates, call->cReaders);
1067
0
    }
1068
0
    if (ret.ReturnCode != SCARD_E_TIMEOUT)
1069
0
      break;
1070
0
    if (smartcard_context_was_aborted(smartcard))
1071
0
      break;
1072
0
    if (dwTimeOut != INFINITE)
1073
0
      x += dwTimeStep;
1074
0
  }
1075
0
  scard_log_status_error_wlog(smartcard->log, "SCardGetStatusChangeW", ret.ReturnCode);
1076
1077
0
  for (UINT32 index = 0; index < ret.cReaders; index++)
1078
0
  {
1079
0
    const SCARD_READERSTATEW* cur = &rgReaderStates[index];
1080
0
    ReaderState_Return* rout = &ret.rgReaderStates[index];
1081
1082
0
    rout->dwCurrentState = cur->dwCurrentState;
1083
0
    rout->dwEventState = cur->dwEventState;
1084
0
    rout->cbAtr = cur->cbAtr;
1085
0
    CopyMemory(&(rout->rgbAtr), cur->rgbAtr, sizeof(rout->rgbAtr));
1086
0
  }
1087
1088
0
  status = smartcard_pack_get_status_change_return(out, &ret, TRUE);
1089
0
fail:
1090
0
  free(ret.rgReaderStates);
1091
0
  free(rgReaderStates);
1092
0
  if (status != SCARD_S_SUCCESS)
1093
0
    return status;
1094
0
  return ret.ReturnCode;
1095
0
}
1096
1097
static LONG smartcard_Cancel_Call(scard_call_context* smartcard, WINPR_ATTR_UNUSED wStream* out,
1098
                                  SMARTCARD_OPERATION* operation)
1099
0
{
1100
0
  Long_Return ret = WINPR_C_ARRAY_INIT;
1101
1102
0
  WINPR_ASSERT(smartcard);
1103
0
  WINPR_ASSERT(out);
1104
0
  WINPR_ASSERT(operation);
1105
1106
0
  ret.ReturnCode = wrap(smartcard, SCardCancel, operation->hContext);
1107
0
  scard_log_status_error_wlog(smartcard->log, "SCardCancel", ret.ReturnCode);
1108
0
  smartcard_trace_long_return_int(smartcard->log, &ret, "Cancel");
1109
0
  return ret.ReturnCode;
1110
0
}
1111
1112
static LONG smartcard_ConnectA_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
  ConnectA_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.connectA;
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, SCardConnectA, operation->hContext, (char*)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_ConnectW_Call(scard_call_context* smartcard, wStream* out,
1149
                                    SMARTCARD_OPERATION* operation)
1150
0
{
1151
0
  LONG status = 0;
1152
0
  SCARDHANDLE hCard = 0;
1153
0
  Connect_Return ret = WINPR_C_ARRAY_INIT;
1154
0
  ConnectW_Call* call = nullptr;
1155
1156
0
  WINPR_ASSERT(smartcard);
1157
0
  WINPR_ASSERT(out);
1158
0
  WINPR_ASSERT(operation);
1159
1160
0
  call = &operation->call.connectW;
1161
1162
0
  if ((call->Common.dwPreferredProtocols == SCARD_PROTOCOL_UNDEFINED) &&
1163
0
      (call->Common.dwShareMode != SCARD_SHARE_DIRECT))
1164
0
  {
1165
0
    call->Common.dwPreferredProtocols = SCARD_PROTOCOL_Tx;
1166
0
  }
1167
1168
0
  ret.ReturnCode = wrap(smartcard, SCardConnectW, operation->hContext, (WCHAR*)call->szReader,
1169
0
                        call->Common.dwShareMode, call->Common.dwPreferredProtocols, &hCard,
1170
0
                        &ret.dwActiveProtocol);
1171
0
  smartcard_scard_context_native_to_redir(&(ret.hContext), operation->hContext);
1172
0
  smartcard_scard_handle_native_to_redir(&(ret.hCard), hCard);
1173
1174
0
  status = smartcard_pack_connect_return(out, &ret);
1175
0
  if (status != SCARD_S_SUCCESS)
1176
0
    goto out_fail;
1177
1178
0
  status = ret.ReturnCode;
1179
0
out_fail:
1180
1181
0
  return status;
1182
0
}
1183
1184
static LONG smartcard_Reconnect_Call(scard_call_context* smartcard, wStream* out,
1185
                                     SMARTCARD_OPERATION* operation)
1186
0
{
1187
0
  LONG status = 0;
1188
0
  Reconnect_Return ret = WINPR_C_ARRAY_INIT;
1189
0
  Reconnect_Call* call = nullptr;
1190
1191
0
  WINPR_ASSERT(smartcard);
1192
0
  WINPR_ASSERT(out);
1193
0
  WINPR_ASSERT(operation);
1194
1195
0
  call = &operation->call.reconnect;
1196
0
  ret.ReturnCode =
1197
0
      wrap(smartcard, SCardReconnect, operation->hCard, call->dwShareMode,
1198
0
           call->dwPreferredProtocols, call->dwInitialization, &ret.dwActiveProtocol);
1199
0
  scard_log_status_error_wlog(smartcard->log, "SCardReconnect", ret.ReturnCode);
1200
0
  status = smartcard_pack_reconnect_return(out, &ret);
1201
0
  if (status != SCARD_S_SUCCESS)
1202
0
    return status;
1203
1204
0
  return ret.ReturnCode;
1205
0
}
1206
1207
static LONG smartcard_Disconnect_Call(scard_call_context* smartcard, 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, SCardDisconnect, operation->hCard, call->dwDisposition);
1220
0
  scard_log_status_error_wlog(smartcard->log, "SCardDisconnect", ret.ReturnCode);
1221
0
  smartcard_trace_long_return_int(smartcard->log, &ret, "Disconnect");
1222
1223
0
  return ret.ReturnCode;
1224
0
}
1225
1226
static LONG smartcard_BeginTransaction_Call(scard_call_context* smartcard,
1227
                                            WINPR_ATTR_UNUSED wStream* out,
1228
                                            SMARTCARD_OPERATION* operation)
1229
0
{
1230
0
  Long_Return ret = WINPR_C_ARRAY_INIT;
1231
1232
0
  WINPR_ASSERT(smartcard);
1233
0
  WINPR_ASSERT(out);
1234
0
  WINPR_ASSERT(operation);
1235
1236
0
  ret.ReturnCode = wrap(smartcard, SCardBeginTransaction, operation->hCard);
1237
0
  scard_log_status_error_wlog(smartcard->log, "SCardBeginTransaction", ret.ReturnCode);
1238
0
  smartcard_trace_long_return_int(smartcard->log, &ret, "BeginTransaction");
1239
0
  return ret.ReturnCode;
1240
0
}
1241
1242
static LONG smartcard_EndTransaction_Call(scard_call_context* smartcard,
1243
                                          WINPR_ATTR_UNUSED wStream* out,
1244
                                          SMARTCARD_OPERATION* operation)
1245
0
{
1246
0
  Long_Return ret = WINPR_C_ARRAY_INIT;
1247
0
  HCardAndDisposition_Call* call = nullptr;
1248
1249
0
  WINPR_ASSERT(smartcard);
1250
0
  WINPR_ASSERT(out);
1251
0
  WINPR_ASSERT(operation);
1252
1253
0
  call = &operation->call.hCardAndDisposition;
1254
1255
0
  ret.ReturnCode = wrap(smartcard, SCardEndTransaction, operation->hCard, call->dwDisposition);
1256
0
  scard_log_status_error_wlog(smartcard->log, "SCardEndTransaction", ret.ReturnCode);
1257
0
  smartcard_trace_long_return_int(smartcard->log, &ret, "EndTransaction");
1258
0
  return ret.ReturnCode;
1259
0
}
1260
1261
static LONG smartcard_State_Call(scard_call_context* smartcard, wStream* out,
1262
                                 SMARTCARD_OPERATION* operation)
1263
0
{
1264
0
  LONG status = 0;
1265
0
  State_Return ret = WINPR_C_ARRAY_INIT;
1266
1267
0
  WINPR_ASSERT(smartcard);
1268
0
  WINPR_ASSERT(out);
1269
0
  WINPR_ASSERT(operation);
1270
1271
0
  ret.cbAtrLen = SCARD_ATR_LENGTH;
1272
0
  ret.ReturnCode = wrap(smartcard, SCardState, operation->hCard, &ret.dwState, &ret.dwProtocol,
1273
0
                        (BYTE*)&ret.rgAtr, &ret.cbAtrLen);
1274
1275
0
  scard_log_status_error_wlog(smartcard->log, "SCardState", ret.ReturnCode);
1276
0
  status = smartcard_pack_state_return(out, &ret);
1277
0
  if (status != SCARD_S_SUCCESS)
1278
0
    return status;
1279
1280
0
  return ret.ReturnCode;
1281
0
}
1282
1283
static LONG smartcard_StatusA_Call(scard_call_context* smartcard, wStream* out,
1284
                                   SMARTCARD_OPERATION* operation)
1285
0
{
1286
0
  LONG status = 0;
1287
0
  Status_Return ret = WINPR_C_ARRAY_INIT;
1288
0
  DWORD cchReaderLen = 0;
1289
0
  DWORD cbAtrLen = 0;
1290
0
  LPSTR mszReaderNames = nullptr;
1291
0
  Status_Call* call = nullptr;
1292
1293
0
  WINPR_ASSERT(smartcard);
1294
0
  WINPR_ASSERT(out);
1295
0
  WINPR_ASSERT(operation);
1296
1297
0
  call = &operation->call.status;
1298
1299
0
  call->cbAtrLen = 32;
1300
0
  cbAtrLen = call->cbAtrLen;
1301
1302
0
  if (call->fmszReaderNamesIsNULL)
1303
0
    cchReaderLen = 0;
1304
0
  else
1305
0
    cchReaderLen = SCARD_AUTOALLOCATE;
1306
1307
0
  status = ret.ReturnCode =
1308
0
      wrap(smartcard, SCardStatusA, operation->hCard,
1309
0
           call->fmszReaderNamesIsNULL ? nullptr : (LPSTR)&mszReaderNames, &cchReaderLen,
1310
0
           &ret.dwState, &ret.dwProtocol, cbAtrLen ? (BYTE*)&ret.pbAtr : nullptr, &cbAtrLen);
1311
1312
0
  scard_log_status_error_wlog(smartcard->log, "SCardStatusA", status);
1313
0
  if ((ret.ReturnCode == SCARD_S_SUCCESS) && (cchReaderLen == SCARD_AUTOALLOCATE))
1314
0
    return SCARD_F_UNKNOWN_ERROR;
1315
1316
0
  if (status == SCARD_S_SUCCESS)
1317
0
  {
1318
0
    if (!call->fmszReaderNamesIsNULL)
1319
0
      ret.mszReaderNames = (BYTE*)mszReaderNames;
1320
1321
0
    ret.cBytes = cchReaderLen;
1322
1323
0
    if (call->cbAtrLen)
1324
0
      ret.cbAtrLen = cbAtrLen;
1325
0
  }
1326
1327
0
  status = smartcard_pack_status_return(out, &ret, FALSE);
1328
1329
0
  if (mszReaderNames)
1330
0
    wrap(smartcard, SCardFreeMemory, operation->hContext, mszReaderNames);
1331
1332
0
  if (status != SCARD_S_SUCCESS)
1333
0
    return status;
1334
0
  return ret.ReturnCode;
1335
0
}
1336
1337
static LONG smartcard_StatusW_Call(scard_call_context* smartcard, wStream* out,
1338
                                   SMARTCARD_OPERATION* operation)
1339
0
{
1340
0
  LONG status = 0;
1341
0
  Status_Return ret = WINPR_C_ARRAY_INIT;
1342
0
  LPWSTR mszReaderNames = nullptr;
1343
0
  Status_Call* call = nullptr;
1344
0
  DWORD cbAtrLen = 0;
1345
1346
0
  WINPR_ASSERT(smartcard);
1347
0
  WINPR_ASSERT(out);
1348
0
  WINPR_ASSERT(operation);
1349
1350
0
  call = &operation->call.status;
1351
1352
  /**
1353
   * [MS-RDPESC]
1354
   * According to 2.2.2.18 Status_Call cbAtrLen is unused an must be ignored upon receipt.
1355
   */
1356
0
  cbAtrLen = call->cbAtrLen = 32;
1357
1358
0
  if (call->fmszReaderNamesIsNULL)
1359
0
    ret.cBytes = 0;
1360
0
  else
1361
0
    ret.cBytes = SCARD_AUTOALLOCATE;
1362
1363
0
  status = ret.ReturnCode =
1364
0
      wrap(smartcard, SCardStatusW, operation->hCard,
1365
0
           call->fmszReaderNamesIsNULL ? nullptr : (LPWSTR)&mszReaderNames, &ret.cBytes,
1366
0
           &ret.dwState, &ret.dwProtocol, (BYTE*)&ret.pbAtr, &cbAtrLen);
1367
0
  scard_log_status_error_wlog(smartcard->log, "SCardStatusW", status);
1368
0
  if ((ret.ReturnCode == SCARD_S_SUCCESS) && (ret.cBytes == SCARD_AUTOALLOCATE))
1369
0
    return SCARD_F_UNKNOWN_ERROR;
1370
1371
0
  size_t blen = 0;
1372
0
  if (status == SCARD_S_SUCCESS)
1373
0
  {
1374
0
    if (!call->fmszReaderNamesIsNULL)
1375
0
      ret.mszReaderNames = (BYTE*)mszReaderNames;
1376
1377
0
    ret.cbAtrLen = cbAtrLen;
1378
0
  }
1379
1380
0
  if (ret.cBytes != SCARD_AUTOALLOCATE)
1381
0
  {
1382
    /* SCardStatusW returns number of characters, we need number of bytes */
1383
0
    WINPR_ASSERT(ret.cBytes < SCARD_AUTOALLOCATE / sizeof(WCHAR));
1384
0
    blen = sizeof(WCHAR) * ret.cBytes;
1385
0
    WINPR_ASSERT(blen <= UINT32_MAX);
1386
0
    ret.cBytes = (UINT32)blen;
1387
0
  }
1388
1389
0
  status = smartcard_pack_status_return(out, &ret, TRUE);
1390
0
  if (status != SCARD_S_SUCCESS)
1391
0
    return status;
1392
1393
0
  if (mszReaderNames)
1394
0
    wrap(smartcard, SCardFreeMemory, operation->hContext, mszReaderNames);
1395
1396
0
  return ret.ReturnCode;
1397
0
}
1398
1399
static LONG smartcard_Transmit_Call(scard_call_context* smartcard, wStream* out,
1400
                                    SMARTCARD_OPERATION* operation)
1401
0
{
1402
0
  LONG status = 0;
1403
0
  Transmit_Return ret = WINPR_C_ARRAY_INIT;
1404
0
  Transmit_Call* call = nullptr;
1405
1406
0
  WINPR_ASSERT(smartcard);
1407
0
  WINPR_ASSERT(out);
1408
0
  WINPR_ASSERT(operation);
1409
1410
0
  call = &operation->call.transmit;
1411
0
  ret.cbRecvLength = 0;
1412
0
  ret.pbRecvBuffer = nullptr;
1413
1414
0
  if (call->cbRecvLength && !call->fpbRecvBufferIsNULL)
1415
0
  {
1416
0
    if (call->cbRecvLength >= 66560)
1417
0
      call->cbRecvLength = 66560;
1418
1419
0
    const UINT32 cbRecvLength = call->cbRecvLength;
1420
0
    ret.pbRecvBuffer = (BYTE*)malloc(cbRecvLength);
1421
1422
0
    if (!ret.pbRecvBuffer)
1423
0
      return STATUS_NO_MEMORY;
1424
0
    ret.cbRecvLength = cbRecvLength;
1425
0
  }
1426
1427
0
  ret.pioRecvPci = call->pioRecvPci;
1428
0
  ret.ReturnCode =
1429
0
      wrap(smartcard, SCardTransmit, operation->hCard, call->pioSendPci, call->pbSendBuffer,
1430
0
           call->cbSendLength, ret.pioRecvPci, ret.pbRecvBuffer, &(ret.cbRecvLength));
1431
1432
0
  scard_log_status_error_wlog(smartcard->log, "SCardTransmit", ret.ReturnCode);
1433
1434
0
  status = smartcard_pack_transmit_return(out, &ret);
1435
0
  free(ret.pbRecvBuffer);
1436
1437
0
  if (status != SCARD_S_SUCCESS)
1438
0
    return status;
1439
0
  return ret.ReturnCode;
1440
0
}
1441
1442
static LONG smartcard_Control_Call(scard_call_context* smartcard, wStream* out,
1443
                                   SMARTCARD_OPERATION* operation)
1444
0
{
1445
0
  LONG status = 0;
1446
0
  Control_Return ret = WINPR_C_ARRAY_INIT;
1447
0
  Control_Call* call = nullptr;
1448
1449
0
  WINPR_ASSERT(smartcard);
1450
0
  WINPR_ASSERT(out);
1451
0
  WINPR_ASSERT(operation);
1452
1453
0
  call = &operation->call.control;
1454
0
  ret.pvOutBuffer = (BYTE*)malloc(call->cbOutBufferSize);
1455
1456
0
  if (!ret.pvOutBuffer)
1457
0
    return SCARD_E_NO_MEMORY;
1458
0
  ret.cbOutBufferSize = call->cbOutBufferSize;
1459
1460
0
  ret.ReturnCode =
1461
0
      wrap(smartcard, SCardControl, operation->hCard, call->dwControlCode, call->pvInBuffer,
1462
0
           call->cbInBufferSize, ret.pvOutBuffer, call->cbOutBufferSize, &ret.cbOutBufferSize);
1463
0
  scard_log_status_error_wlog(smartcard->log, "SCardControl", ret.ReturnCode);
1464
0
  status = smartcard_pack_control_return(out, &ret);
1465
1466
0
  free(ret.pvOutBuffer);
1467
0
  if (status != SCARD_S_SUCCESS)
1468
0
    return status;
1469
0
  return ret.ReturnCode;
1470
0
}
1471
1472
static LONG smartcard_GetAttrib_Call(scard_call_context* smartcard, wStream* out,
1473
                                     SMARTCARD_OPERATION* operation)
1474
0
{
1475
0
  BOOL autoAllocate = FALSE;
1476
0
  LONG status = 0;
1477
0
  DWORD cbAttrLen = 0;
1478
0
  LPBYTE pbAttr = nullptr;
1479
0
  GetAttrib_Return ret = WINPR_C_ARRAY_INIT;
1480
0
  const GetAttrib_Call* call = nullptr;
1481
1482
0
  WINPR_ASSERT(smartcard);
1483
0
  WINPR_ASSERT(operation);
1484
1485
0
  call = &operation->call.getAttrib;
1486
1487
0
  if (!call->fpbAttrIsNULL)
1488
0
  {
1489
0
    autoAllocate = (call->cbAttrLen == SCARD_AUTOALLOCATE);
1490
0
    cbAttrLen = call->cbAttrLen;
1491
0
    if (cbAttrLen && !autoAllocate)
1492
0
    {
1493
0
      ret.pbAttr = (BYTE*)malloc(cbAttrLen);
1494
1495
0
      if (!ret.pbAttr)
1496
0
        return SCARD_E_NO_MEMORY;
1497
0
    }
1498
1499
0
    pbAttr = autoAllocate ? (LPBYTE) & (ret.pbAttr) : ret.pbAttr;
1500
0
  }
1501
1502
0
  ret.ReturnCode =
1503
0
      wrap(smartcard, SCardGetAttrib, operation->hCard, call->dwAttrId, pbAttr, &cbAttrLen);
1504
0
  scard_log_status_error_wlog(smartcard->log, "SCardGetAttrib", ret.ReturnCode);
1505
0
  if ((ret.ReturnCode == SCARD_S_SUCCESS) && (cbAttrLen == SCARD_AUTOALLOCATE))
1506
0
    return SCARD_F_UNKNOWN_ERROR;
1507
1508
0
  ret.cbAttrLen = cbAttrLen;
1509
1510
0
  status = smartcard_pack_get_attrib_return(out, &ret, call->dwAttrId, call->cbAttrLen);
1511
1512
0
  if (autoAllocate)
1513
0
    wrap(smartcard, SCardFreeMemory, operation->hContext, ret.pbAttr);
1514
0
  else
1515
0
    free(ret.pbAttr);
1516
0
  return status;
1517
0
}
1518
1519
static LONG smartcard_SetAttrib_Call(scard_call_context* smartcard, WINPR_ATTR_UNUSED wStream* out,
1520
                                     SMARTCARD_OPERATION* operation)
1521
0
{
1522
0
  Long_Return ret = WINPR_C_ARRAY_INIT;
1523
0
  SetAttrib_Call* call = nullptr;
1524
1525
0
  WINPR_ASSERT(smartcard);
1526
0
  WINPR_ASSERT(out);
1527
0
  WINPR_ASSERT(operation);
1528
1529
0
  call = &operation->call.setAttrib;
1530
1531
0
  ret.ReturnCode = wrap(smartcard, SCardSetAttrib, operation->hCard, call->dwAttrId, call->pbAttr,
1532
0
                        call->cbAttrLen);
1533
0
  scard_log_status_error_wlog(smartcard->log, "SCardSetAttrib", ret.ReturnCode);
1534
0
  smartcard_trace_long_return_int(smartcard->log, &ret, "SetAttrib");
1535
1536
0
  return ret.ReturnCode;
1537
0
}
1538
1539
static LONG smartcard_AccessStartedEvent_Call(scard_call_context* smartcard,
1540
                                              WINPR_ATTR_UNUSED wStream* out,
1541
                                              SMARTCARD_OPERATION* operation)
1542
0
{
1543
0
  LONG status = SCARD_S_SUCCESS;
1544
1545
0
  WINPR_ASSERT(smartcard);
1546
0
  WINPR_ASSERT(out);
1547
0
  WINPR_UNUSED(operation);
1548
1549
0
  if (!smartcard->StartedEvent)
1550
0
    smartcard->StartedEvent = wrap_ptr(smartcard, SCardAccessStartedEvent);
1551
1552
0
  if (!smartcard->StartedEvent)
1553
0
    status = SCARD_E_NO_SERVICE;
1554
1555
0
  return status;
1556
0
}
1557
1558
static LONG smartcard_LocateCardsByATRA_Call(scard_call_context* smartcard, wStream* out,
1559
                                             SMARTCARD_OPERATION* operation)
1560
0
{
1561
0
  LONG status = 0;
1562
0
  GetStatusChange_Return ret = WINPR_C_ARRAY_INIT;
1563
0
  LPSCARD_READERSTATEA states = nullptr;
1564
0
  LocateCardsByATRA_Call* call = nullptr;
1565
1566
0
  WINPR_ASSERT(smartcard);
1567
0
  WINPR_ASSERT(operation);
1568
1569
0
  call = &operation->call.locateCardsByATRA;
1570
0
  states = (LPSCARD_READERSTATEA)calloc(call->cReaders, sizeof(SCARD_READERSTATEA));
1571
1572
0
  if (!states)
1573
0
    return STATUS_NO_MEMORY;
1574
1575
0
  for (UINT32 i = 0; i < call->cReaders; i++)
1576
0
  {
1577
0
    LPSCARD_READERSTATEA state = &states[i];
1578
0
    state->szReader = call->rgReaderStates[i].szReader;
1579
0
    state->dwCurrentState = call->rgReaderStates[i].dwCurrentState;
1580
0
    state->dwEventState = call->rgReaderStates[i].dwEventState;
1581
0
    state->cbAtr = call->rgReaderStates[i].cbAtr;
1582
0
    CopyMemory(&(state->rgbAtr), &(call->rgReaderStates[i].rgbAtr), 36);
1583
0
  }
1584
1585
0
  status = ret.ReturnCode = wrap(smartcard, SCardGetStatusChangeA, operation->hContext,
1586
0
                                 0x000001F4, states, call->cReaders);
1587
1588
0
  scard_log_status_error_wlog(smartcard->log, "SCardGetStatusChangeA", status);
1589
0
  for (UINT32 i = 0; i < call->cAtrs; i++)
1590
0
  {
1591
0
    for (UINT32 j = 0; j < call->cReaders; j++)
1592
0
    {
1593
0
      for (UINT32 k = 0; k < call->rgAtrMasks[i].cbAtr; k++)
1594
0
      {
1595
0
        if ((call->rgAtrMasks[i].rgbAtr[k] & call->rgAtrMasks[i].rgbMask[k]) !=
1596
0
            (states[j].rgbAtr[k] & call->rgAtrMasks[i].rgbMask[k]))
1597
0
        {
1598
0
          break;
1599
0
        }
1600
1601
0
        states[j].dwEventState |= SCARD_STATE_ATRMATCH;
1602
0
      }
1603
0
    }
1604
0
  }
1605
1606
0
  ret.rgReaderStates = nullptr;
1607
1608
0
  if (call->cReaders > 0)
1609
0
    ret.rgReaderStates =
1610
0
        (ReaderState_Return*)calloc(call->cReaders, sizeof(ReaderState_Return));
1611
1612
0
  if (!ret.rgReaderStates)
1613
0
  {
1614
0
    free(states);
1615
0
    return STATUS_NO_MEMORY;
1616
0
  }
1617
1618
0
  ret.cReaders = call->cReaders;
1619
1620
0
  for (UINT32 i = 0; i < ret.cReaders; i++)
1621
0
  {
1622
0
    LPSCARD_READERSTATEA state = &states[i];
1623
0
    ret.rgReaderStates[i].dwCurrentState = state->dwCurrentState;
1624
0
    ret.rgReaderStates[i].dwEventState = state->dwEventState;
1625
0
    ret.rgReaderStates[i].cbAtr = state->cbAtr;
1626
0
    CopyMemory(&(ret.rgReaderStates[i].rgbAtr), &(state->rgbAtr),
1627
0
               sizeof(ret.rgReaderStates[i].rgbAtr));
1628
0
  }
1629
1630
0
  free(states);
1631
1632
0
  status = smartcard_pack_get_status_change_return(out, &ret, FALSE);
1633
1634
0
  free(ret.rgReaderStates);
1635
0
  if (status != SCARD_S_SUCCESS)
1636
0
    return status;
1637
0
  return ret.ReturnCode;
1638
0
}
1639
1640
LONG smartcard_irp_device_control_call(scard_call_context* ctx, wStream* out, NTSTATUS* pIoStatus,
1641
                                       SMARTCARD_OPERATION* operation)
1642
0
{
1643
0
  LONG result = 0;
1644
0
  UINT32 offset = 0;
1645
0
  size_t objectBufferLength = 0;
1646
1647
0
  WINPR_ASSERT(ctx);
1648
0
  WINPR_ASSERT(out);
1649
0
  WINPR_ASSERT(pIoStatus);
1650
0
  WINPR_ASSERT(operation);
1651
1652
0
  const UINT32 ioControlCode = operation->ioControlCode;
1653
  /**
1654
   * [MS-RDPESC] 3.2.5.1: Sending Outgoing Messages:
1655
   * the output buffer length SHOULD be set to 2048
1656
   *
1657
   * Since it's a SHOULD and not a MUST, we don't care
1658
   * about it, but we still reserve at least 2048 bytes.
1659
   */
1660
0
  const size_t outMaxLen = MAX(2048, operation->outputBufferLength);
1661
0
  if (!Stream_EnsureRemainingCapacity(out, outMaxLen))
1662
0
    return SCARD_E_NO_MEMORY;
1663
1664
  /* Device Control Response */
1665
0
  Stream_Write_UINT32(out, 0);                            /* OutputBufferLength (4 bytes) */
1666
0
  Stream_Zero(out, SMARTCARD_COMMON_TYPE_HEADER_LENGTH);  /* CommonTypeHeader (8 bytes) */
1667
0
  Stream_Zero(out, SMARTCARD_PRIVATE_TYPE_HEADER_LENGTH); /* PrivateTypeHeader (8 bytes) */
1668
0
  Stream_Write_UINT32(out, 0);                            /* Result (4 bytes) */
1669
1670
  /* Call */
1671
0
  switch (ioControlCode)
1672
0
  {
1673
0
    case SCARD_IOCTL_ESTABLISHCONTEXT:
1674
0
      result = smartcard_EstablishContext_Call(ctx, out, operation);
1675
0
      break;
1676
1677
0
    case SCARD_IOCTL_RELEASECONTEXT:
1678
0
      result = smartcard_ReleaseContext_Call(ctx, out, operation);
1679
0
      break;
1680
1681
0
    case SCARD_IOCTL_ISVALIDCONTEXT:
1682
0
      result = smartcard_IsValidContext_Call(ctx, out, operation);
1683
0
      break;
1684
1685
0
    case SCARD_IOCTL_LISTREADERGROUPSA:
1686
0
      result = smartcard_ListReaderGroupsA_Call(ctx, out, operation);
1687
0
      break;
1688
1689
0
    case SCARD_IOCTL_LISTREADERGROUPSW:
1690
0
      result = smartcard_ListReaderGroupsW_Call(ctx, out, operation);
1691
0
      break;
1692
1693
0
    case SCARD_IOCTL_LISTREADERSA:
1694
0
      result = smartcard_ListReadersA_Call(ctx, out, operation);
1695
0
      break;
1696
1697
0
    case SCARD_IOCTL_LISTREADERSW:
1698
0
      result = smartcard_ListReadersW_Call(ctx, out, operation);
1699
0
      break;
1700
1701
0
    case SCARD_IOCTL_INTRODUCEREADERGROUPA:
1702
0
      result = smartcard_IntroduceReaderGroupA_Call(ctx, out, operation);
1703
0
      break;
1704
1705
0
    case SCARD_IOCTL_INTRODUCEREADERGROUPW:
1706
0
      result = smartcard_IntroduceReaderGroupW_Call(ctx, out, operation);
1707
0
      break;
1708
1709
0
    case SCARD_IOCTL_FORGETREADERGROUPA:
1710
0
      result = smartcard_ForgetReaderA_Call(ctx, out, operation);
1711
0
      break;
1712
1713
0
    case SCARD_IOCTL_FORGETREADERGROUPW:
1714
0
      result = smartcard_ForgetReaderW_Call(ctx, out, operation);
1715
0
      break;
1716
1717
0
    case SCARD_IOCTL_INTRODUCEREADERA:
1718
0
      result = smartcard_IntroduceReaderA_Call(ctx, out, operation);
1719
0
      break;
1720
1721
0
    case SCARD_IOCTL_INTRODUCEREADERW:
1722
0
      result = smartcard_IntroduceReaderW_Call(ctx, out, operation);
1723
0
      break;
1724
1725
0
    case SCARD_IOCTL_FORGETREADERA:
1726
0
      result = smartcard_ForgetReaderA_Call(ctx, out, operation);
1727
0
      break;
1728
1729
0
    case SCARD_IOCTL_FORGETREADERW:
1730
0
      result = smartcard_ForgetReaderW_Call(ctx, out, operation);
1731
0
      break;
1732
1733
0
    case SCARD_IOCTL_ADDREADERTOGROUPA:
1734
0
      result = smartcard_AddReaderToGroupA_Call(ctx, out, operation);
1735
0
      break;
1736
1737
0
    case SCARD_IOCTL_ADDREADERTOGROUPW:
1738
0
      result = smartcard_AddReaderToGroupW_Call(ctx, out, operation);
1739
0
      break;
1740
1741
0
    case SCARD_IOCTL_REMOVEREADERFROMGROUPA:
1742
0
      result = smartcard_RemoveReaderFromGroupA_Call(ctx, out, operation);
1743
0
      break;
1744
1745
0
    case SCARD_IOCTL_REMOVEREADERFROMGROUPW:
1746
0
      result = smartcard_RemoveReaderFromGroupW_Call(ctx, out, operation);
1747
0
      break;
1748
1749
0
    case SCARD_IOCTL_LOCATECARDSA:
1750
0
      result = smartcard_LocateCardsA_Call(ctx, out, operation);
1751
0
      break;
1752
1753
0
    case SCARD_IOCTL_LOCATECARDSW:
1754
0
      result = smartcard_LocateCardsW_Call(ctx, out, operation);
1755
0
      break;
1756
1757
0
    case SCARD_IOCTL_GETSTATUSCHANGEA:
1758
0
      result = smartcard_GetStatusChangeA_Call(ctx, out, operation);
1759
0
      break;
1760
1761
0
    case SCARD_IOCTL_GETSTATUSCHANGEW:
1762
0
      result = smartcard_GetStatusChangeW_Call(ctx, out, operation);
1763
0
      break;
1764
1765
0
    case SCARD_IOCTL_CANCEL:
1766
0
      result = smartcard_Cancel_Call(ctx, out, operation);
1767
0
      break;
1768
1769
0
    case SCARD_IOCTL_CONNECTA:
1770
0
      result = smartcard_ConnectA_Call(ctx, out, operation);
1771
0
      break;
1772
1773
0
    case SCARD_IOCTL_CONNECTW:
1774
0
      result = smartcard_ConnectW_Call(ctx, out, operation);
1775
0
      break;
1776
1777
0
    case SCARD_IOCTL_RECONNECT:
1778
0
      result = smartcard_Reconnect_Call(ctx, out, operation);
1779
0
      break;
1780
1781
0
    case SCARD_IOCTL_DISCONNECT:
1782
0
      result = smartcard_Disconnect_Call(ctx, out, operation);
1783
0
      break;
1784
1785
0
    case SCARD_IOCTL_BEGINTRANSACTION:
1786
0
      result = smartcard_BeginTransaction_Call(ctx, out, operation);
1787
0
      break;
1788
1789
0
    case SCARD_IOCTL_ENDTRANSACTION:
1790
0
      result = smartcard_EndTransaction_Call(ctx, out, operation);
1791
0
      break;
1792
1793
0
    case SCARD_IOCTL_STATE:
1794
0
      result = smartcard_State_Call(ctx, out, operation);
1795
0
      break;
1796
1797
0
    case SCARD_IOCTL_STATUSA:
1798
0
      result = smartcard_StatusA_Call(ctx, out, operation);
1799
0
      break;
1800
1801
0
    case SCARD_IOCTL_STATUSW:
1802
0
      result = smartcard_StatusW_Call(ctx, out, operation);
1803
0
      break;
1804
1805
0
    case SCARD_IOCTL_TRANSMIT:
1806
0
      result = smartcard_Transmit_Call(ctx, out, operation);
1807
0
      break;
1808
1809
0
    case SCARD_IOCTL_CONTROL:
1810
0
      result = smartcard_Control_Call(ctx, out, operation);
1811
0
      break;
1812
1813
0
    case SCARD_IOCTL_GETATTRIB:
1814
0
      result = smartcard_GetAttrib_Call(ctx, out, operation);
1815
0
      break;
1816
1817
0
    case SCARD_IOCTL_SETATTRIB:
1818
0
      result = smartcard_SetAttrib_Call(ctx, out, operation);
1819
0
      break;
1820
1821
0
    case SCARD_IOCTL_ACCESSSTARTEDEVENT:
1822
0
      result = smartcard_AccessStartedEvent_Call(ctx, out, operation);
1823
0
      break;
1824
1825
0
    case SCARD_IOCTL_LOCATECARDSBYATRA:
1826
0
      result = smartcard_LocateCardsByATRA_Call(ctx, out, operation);
1827
0
      break;
1828
1829
0
    case SCARD_IOCTL_LOCATECARDSBYATRW:
1830
0
      result = smartcard_LocateCardsW_Call(ctx, out, operation);
1831
0
      break;
1832
1833
0
    case SCARD_IOCTL_READCACHEA:
1834
0
      result = smartcard_ReadCacheA_Call(ctx, out, operation);
1835
0
      break;
1836
1837
0
    case SCARD_IOCTL_READCACHEW:
1838
0
      result = smartcard_ReadCacheW_Call(ctx, out, operation);
1839
0
      break;
1840
1841
0
    case SCARD_IOCTL_WRITECACHEA:
1842
0
      result = smartcard_WriteCacheA_Call(ctx, out, operation);
1843
0
      break;
1844
1845
0
    case SCARD_IOCTL_WRITECACHEW:
1846
0
      result = smartcard_WriteCacheW_Call(ctx, out, operation);
1847
0
      break;
1848
1849
0
    case SCARD_IOCTL_GETTRANSMITCOUNT:
1850
0
      result = smartcard_GetTransmitCount_Call(ctx, out, operation);
1851
0
      break;
1852
1853
0
    case SCARD_IOCTL_RELEASETARTEDEVENT:
1854
0
      result = smartcard_ReleaseStartedEvent_Call(ctx, out, operation);
1855
0
      break;
1856
1857
0
    case SCARD_IOCTL_GETREADERICON:
1858
0
      result = smartcard_GetReaderIcon_Call(ctx, out, operation);
1859
0
      break;
1860
1861
0
    case SCARD_IOCTL_GETDEVICETYPEID:
1862
0
      result = smartcard_GetDeviceTypeId_Call(ctx, out, operation);
1863
0
      break;
1864
1865
0
    default:
1866
0
      result = STATUS_UNSUCCESSFUL;
1867
0
      break;
1868
0
  }
1869
1870
  /**
1871
   * [MS-RPCE] 2.2.6.3 Primitive Type Serialization
1872
   * The type MUST be aligned on an 8-byte boundary. If the size of the
1873
   * primitive type is not a multiple of 8 bytes, the data MUST be padded.
1874
   */
1875
1876
0
  if ((ioControlCode != SCARD_IOCTL_ACCESSSTARTEDEVENT) &&
1877
0
      (ioControlCode != SCARD_IOCTL_RELEASETARTEDEVENT))
1878
0
  {
1879
0
    offset = (RDPDR_DEVICE_IO_RESPONSE_LENGTH + RDPDR_DEVICE_IO_CONTROL_RSP_HDR_LENGTH);
1880
0
    const LONG rc = smartcard_pack_write_size_align(out, Stream_GetPosition(out) - offset, 8);
1881
0
    if (rc != SCARD_S_SUCCESS)
1882
0
      result = rc;
1883
0
  }
1884
1885
0
  if ((result != SCARD_S_SUCCESS) && (result != SCARD_E_TIMEOUT) &&
1886
0
      (result != SCARD_E_NO_READERS_AVAILABLE) && (result != SCARD_E_NO_SERVICE) &&
1887
0
      (result != SCARD_W_CACHE_ITEM_NOT_FOUND) && (result != SCARD_W_CACHE_ITEM_STALE))
1888
0
  {
1889
0
    scard_log_status_error_wlog(ctx->log, "IRP failure: %s (0x%08" PRIX32 ")", result,
1890
0
                                scard_get_ioctl_string(ioControlCode, TRUE), ioControlCode);
1891
0
  }
1892
1893
0
  *pIoStatus = STATUS_SUCCESS;
1894
1895
0
  if ((result & 0xC0000000L) == 0xC0000000L)
1896
0
  {
1897
    /* NTSTATUS error */
1898
0
    *pIoStatus = result;
1899
1900
0
    scard_log_status_error_wlog(ctx->log, "IRP failure: %s (0x%08" PRIX32 ")", result,
1901
0
                                scard_get_ioctl_string(ioControlCode, TRUE), ioControlCode);
1902
0
  }
1903
1904
0
  Stream_SealLength(out);
1905
0
  size_t outputBufferLength = Stream_Length(out);
1906
0
  size_t dataEndPos = outputBufferLength;
1907
1908
0
  WINPR_ASSERT(outputBufferLength >= RDPDR_DEVICE_IO_RESPONSE_LENGTH + 4U);
1909
0
  outputBufferLength -= (RDPDR_DEVICE_IO_RESPONSE_LENGTH + 4U);
1910
0
  WINPR_ASSERT(outputBufferLength >= RDPDR_DEVICE_IO_RESPONSE_LENGTH);
1911
0
  objectBufferLength = outputBufferLength - RDPDR_DEVICE_IO_RESPONSE_LENGTH;
1912
0
  WINPR_ASSERT(outputBufferLength <= UINT32_MAX);
1913
0
  WINPR_ASSERT(objectBufferLength <= UINT32_MAX);
1914
0
  if (!Stream_SetPosition(out, RDPDR_DEVICE_IO_RESPONSE_LENGTH))
1915
0
    return SCARD_E_BAD_SEEK;
1916
1917
  /* [MS-RDPESC] 3.2.5.2 Processing Incoming Replies
1918
   *
1919
   * if the output buffer is too small, reply with STATUS_BUFFER_TOO_SMALL
1920
   * and a outputBufferLength of 0.
1921
   * The message should then be retransmitted from the server with a doubled
1922
   * buffer size.
1923
   */
1924
0
  if (outputBufferLength > operation->outputBufferLength)
1925
0
  {
1926
0
    WLog_Print(ctx->log, WLOG_DEBUG,
1927
0
               "IRP  warn: expected outputBufferLength %" PRIuz ", but current limit %" PRIu32
1928
0
               ", respond with STATUS_BUFFER_TOO_SMALL for retransmit with larger buffer",
1929
0
               outputBufferLength, operation->outputBufferLength);
1930
1931
0
    *pIoStatus = STATUS_BUFFER_TOO_SMALL;
1932
0
    result = *pIoStatus;
1933
0
    outputBufferLength = 0;
1934
0
    objectBufferLength = 0;
1935
0
  }
1936
0
  else
1937
0
  {
1938
0
    WLog_Print(ctx->log, WLOG_TRACE, "IRP trace: outputBufferLength %" PRIuz ", limit %" PRIu32,
1939
0
               outputBufferLength, operation->outputBufferLength);
1940
0
  }
1941
1942
  /* Device Control Response */
1943
0
  Stream_Write_UINT32(out, (UINT32)outputBufferLength); /* OutputBufferLength (4 bytes) */
1944
0
  smartcard_pack_common_type_header(out);               /* CommonTypeHeader (8 bytes) */
1945
0
  smartcard_pack_private_type_header(
1946
0
      out, (UINT32)objectBufferLength); /* PrivateTypeHeader (8 bytes) */
1947
0
  Stream_Write_INT32(out, result);      /* Result (4 bytes) */
1948
0
  if (result == STATUS_BUFFER_TOO_SMALL)
1949
0
    Stream_SealLength(out);
1950
0
  else if (!Stream_SetPosition(out, dataEndPos))
1951
0
    return SCARD_E_BAD_SEEK;
1952
0
  return SCARD_S_SUCCESS;
1953
0
}
1954
1955
void context_free(void* arg)
1956
0
{
1957
0
  struct s_scard_context_element* element = arg;
1958
0
  if (!arg)
1959
0
    return;
1960
1961
0
  if (element->fn_free)
1962
0
    element->fn_free(element->context);
1963
0
  free(element);
1964
0
}
1965
1966
#if !defined(WITHOUT_FREERDP_3x_DEPRECATED)
1967
scard_call_context* smartcard_call_context_new(const rdpSettings* settings)
1968
0
{
1969
0
  const freerdp* inst = freerdp_settings_get_pointer(settings, FreeRDP_instance);
1970
0
  if (!inst || !inst->context)
1971
0
    return nullptr;
1972
0
  return smartcard_call_context_new_with_context(inst->context);
1973
0
}
1974
#endif
1975
1976
scard_call_context* smartcard_call_context_new_with_context(rdpContext* context)
1977
0
{
1978
0
  WINPR_ASSERT(context);
1979
0
  scard_call_context* ctx = calloc(1, sizeof(scard_call_context));
1980
0
  if (!ctx)
1981
0
    goto fail;
1982
1983
0
  ctx->context = context;
1984
1985
0
  const rdpSettings* settings = context->settings;
1986
0
  WINPR_ASSERT(settings);
1987
1988
0
  ctx->log = WLog_Get(SCARD_TAG);
1989
0
  WINPR_ASSERT(ctx->log);
1990
1991
0
  ctx->stopEvent = CreateEventA(nullptr, TRUE, FALSE, nullptr);
1992
0
  if (!ctx->stopEvent)
1993
0
    goto fail;
1994
1995
0
  ctx->names = LinkedList_New();
1996
0
  if (!ctx->names)
1997
0
    goto fail;
1998
1999
0
#if defined(WITH_SMARTCARD_EMULATE)
2000
0
  ctx->useEmulatedCard = freerdp_settings_get_bool(settings, FreeRDP_SmartcardEmulation);
2001
0
#endif
2002
2003
0
  if (ctx->useEmulatedCard)
2004
0
  {
2005
0
#if defined(WITH_SMARTCARD_EMULATE)
2006
0
    ctx->emulation = Emulate_New(settings);
2007
0
    if (!ctx->emulation)
2008
0
      goto fail;
2009
#else
2010
    WLog_Print(ctx->log, WLOG_ERROR, "Smartcard emulation requested, but not supported!");
2011
    goto fail;
2012
#endif
2013
0
  }
2014
0
  else
2015
0
  {
2016
0
    const char* WinSCardModule = freerdp_settings_get_string(settings, FreeRDP_WinSCardModule);
2017
0
    if (WinSCardModule)
2018
0
    {
2019
0
      ctx->hWinSCardLibrary = LoadLibraryX(WinSCardModule);
2020
2021
0
      if (!ctx->hWinSCardLibrary)
2022
0
      {
2023
0
        WLog_Print(ctx->log, WLOG_ERROR, "Failed to load WinSCard library: '%s'",
2024
0
                   WinSCardModule);
2025
0
        goto fail;
2026
0
      }
2027
2028
0
      if (!WinSCard_LoadApiTableFunctions(&ctx->WinSCardApi, ctx->hWinSCardLibrary))
2029
0
        goto fail;
2030
0
      ctx->pWinSCardApi = &ctx->WinSCardApi;
2031
0
    }
2032
0
    else
2033
0
    {
2034
0
      ctx->pWinSCardApi = WinPR_GetSCardApiFunctionTable();
2035
0
    }
2036
2037
0
    if (!ctx->pWinSCardApi)
2038
0
    {
2039
0
      WLog_Print(ctx->log, WLOG_ERROR, "Failed to load WinSCard API!");
2040
0
      goto fail;
2041
0
    }
2042
0
  }
2043
2044
0
  ctx->rgSCardContextList = HashTable_New(FALSE);
2045
0
  if (!ctx->rgSCardContextList)
2046
0
    goto fail;
2047
2048
0
  {
2049
0
    wObject* obj = HashTable_ValueObject(ctx->rgSCardContextList);
2050
0
    WINPR_ASSERT(obj);
2051
0
    obj->fnObjectFree = context_free;
2052
0
  }
2053
2054
0
  return ctx;
2055
0
fail:
2056
0
  WINPR_PRAGMA_DIAG_PUSH
2057
0
  WINPR_PRAGMA_DIAG_IGNORED_MISMATCHED_DEALLOC
2058
0
  smartcard_call_context_free(ctx);
2059
0
  WINPR_PRAGMA_DIAG_POP
2060
0
  return nullptr;
2061
0
}
2062
2063
void smartcard_call_context_free(scard_call_context* ctx)
2064
0
{
2065
0
  if (!ctx)
2066
0
    return;
2067
2068
0
  smartcard_call_context_signal_stop(ctx, FALSE);
2069
2070
0
  LinkedList_Free(ctx->names);
2071
0
  if (ctx->StartedEvent)
2072
0
  {
2073
0
    WINPR_ASSERT(ctx->useEmulatedCard || ctx->pWinSCardApi);
2074
0
    wrap_raw(ctx, SCardReleaseStartedEvent);
2075
0
  }
2076
2077
0
  if (ctx->useEmulatedCard)
2078
0
  {
2079
0
#ifdef WITH_SMARTCARD_EMULATE
2080
0
    if (ctx->emulation)
2081
0
    {
2082
0
      Emulate_Free(ctx->emulation);
2083
0
      ctx->emulation = nullptr;
2084
0
    }
2085
0
#endif
2086
0
  }
2087
2088
0
  if (ctx->hWinSCardLibrary)
2089
0
  {
2090
0
    ZeroMemory(&ctx->WinSCardApi, sizeof(SCardApiFunctionTable));
2091
0
    FreeLibrary(ctx->hWinSCardLibrary);
2092
0
    ctx->hWinSCardLibrary = nullptr;
2093
0
  }
2094
2095
0
  ctx->pWinSCardApi = nullptr;
2096
2097
0
  HashTable_Free(ctx->rgSCardContextList);
2098
0
  (void)CloseHandle(ctx->stopEvent);
2099
0
  free(ctx);
2100
0
}
2101
2102
BOOL smartcard_call_context_add(scard_call_context* ctx, const char* name)
2103
0
{
2104
0
  WINPR_ASSERT(ctx);
2105
0
  WINPR_ASSERT(name);
2106
0
  return LinkedList_AddLast(ctx->names, name);
2107
0
}
2108
2109
BOOL smartcard_call_cancel_context(scard_call_context* ctx, SCARDCONTEXT hContext)
2110
0
{
2111
0
  WINPR_ASSERT(ctx);
2112
0
  if (wrap(ctx, SCardIsValidContext, hContext) == SCARD_S_SUCCESS)
2113
0
  {
2114
0
    wrap(ctx, SCardCancel, hContext);
2115
0
  }
2116
0
  return TRUE;
2117
0
}
2118
2119
BOOL smartcard_call_release_context(scard_call_context* ctx, SCARDCONTEXT hContext)
2120
0
{
2121
0
  WINPR_ASSERT(ctx);
2122
0
  wrap(ctx, SCardReleaseContext, hContext);
2123
0
  return TRUE;
2124
0
}
2125
2126
BOOL smartcard_call_cancel_all_context(scard_call_context* ctx)
2127
0
{
2128
0
  if (!ctx)
2129
0
    return FALSE;
2130
2131
0
  HashTable_Clear(ctx->rgSCardContextList);
2132
0
  return TRUE;
2133
0
}
2134
2135
BOOL smarcard_call_set_callbacks(scard_call_context* ctx, void* userdata,
2136
                                 void* (*fn_new)(void*, SCARDCONTEXT), void (*fn_free)(void*))
2137
0
{
2138
0
  WINPR_ASSERT(ctx);
2139
0
  ctx->userdata = userdata;
2140
0
  ctx->fn_new = fn_new;
2141
0
  ctx->fn_free = fn_free;
2142
0
  return TRUE;
2143
0
}
2144
2145
void* smartcard_call_get_context(scard_call_context* ctx, SCARDCONTEXT hContext)
2146
0
{
2147
0
  struct s_scard_context_element* element = nullptr;
2148
2149
0
  WINPR_ASSERT(ctx);
2150
0
  element = HashTable_GetItemValue(ctx->rgSCardContextList, (void*)hContext);
2151
0
  if (!element)
2152
0
    return nullptr;
2153
0
  return element->context;
2154
0
}
2155
2156
BOOL smartcard_call_is_configured(scard_call_context* ctx)
2157
0
{
2158
0
  WINPR_ASSERT(ctx);
2159
2160
0
#if defined(WITH_SMARTCARD_EMULATE)
2161
0
  if (ctx->useEmulatedCard)
2162
0
    return Emulate_IsConfigured(ctx->emulation);
2163
0
#endif
2164
2165
0
  return FALSE;
2166
0
}
2167
2168
BOOL smartcard_call_context_signal_stop(scard_call_context* ctx, BOOL reset)
2169
0
{
2170
0
  WINPR_ASSERT(ctx);
2171
2172
0
  if (!ctx->stopEvent)
2173
0
    return TRUE;
2174
2175
0
  if (reset)
2176
0
    return ResetEvent(ctx->stopEvent);
2177
0
  else
2178
0
    return SetEvent(ctx->stopEvent);
2179
0
}