Coverage Report

Created: 2025-07-01 06:46

/src/FreeRDP/channels/smartcard/client/smartcard_main.c
Line
Count
Source (jump to first uncovered line)
1
/**
2
 * FreeRDP: A Remote Desktop Protocol Implementation
3
 * Smartcard Device Service Virtual Channel
4
 *
5
 * Copyright 2011 O.S. Systems Software Ltda.
6
 * Copyright 2011 Eduardo Fiss Beloni <beloni@ossystems.com.br>
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 2016 David PHAM-VAN <d.phamvan@inuvika.com>
11
 *
12
 * Licensed under the Apache License, Version 2.0 (the "License");
13
 * you may not use this file except in compliance with the License.
14
 * You may obtain a copy of the License at
15
 *
16
 *     http://www.apache.org/licenses/LICENSE-2.0
17
 *
18
 * Unless required by applicable law or agreed to in writing, software
19
 * distributed under the License is distributed on an "AS IS" BASIS,
20
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
21
 * See the License for the specific language governing permissions and
22
 * limitations under the License.
23
 */
24
25
#include <freerdp/config.h>
26
27
#include <winpr/crt.h>
28
#include <winpr/smartcard.h>
29
#include <winpr/environment.h>
30
31
#include <freerdp/freerdp.h>
32
#include <freerdp/channels/rdpdr.h>
33
#include <freerdp/channels/scard.h>
34
#include <freerdp/utils/smartcard_call.h>
35
#include <freerdp/utils/smartcard_operations.h>
36
#include <freerdp/utils/rdpdr_utils.h>
37
38
#include "smartcard_main.h"
39
40
0
#define CAST_FROM_DEVICE(device) cast_device_from(device, __func__, __FILE__, __LINE__)
41
42
typedef struct
43
{
44
  SMARTCARD_OPERATION operation;
45
  IRP* irp;
46
} scard_irp_queue_element;
47
48
static SMARTCARD_DEVICE* sSmartcard = NULL;
49
50
static void smartcard_context_free(void* pCtx);
51
52
static UINT smartcard_complete_irp(SMARTCARD_DEVICE* smartcard, IRP* irp, BOOL* handled);
53
54
static SMARTCARD_DEVICE* cast_device_from(DEVICE* device, const char* fkt, const char* file,
55
                                          size_t line)
56
0
{
57
0
  if (!device)
58
0
  {
59
0
    WLog_ERR(TAG, "%s [%s:%" PRIuz "] Called smartcard channel with NULL device", fkt, file,
60
0
             line);
61
0
    return NULL;
62
0
  }
63
64
0
  if (device->type != RDPDR_DTYP_SMARTCARD)
65
0
  {
66
0
    WLog_ERR(TAG,
67
0
             "%s [%s:%" PRIuz "] Called smartcard channel with invalid device of type %" PRIx32,
68
0
             fkt, file, line, device->type);
69
0
    return NULL;
70
0
  }
71
72
0
  return (SMARTCARD_DEVICE*)device;
73
0
}
74
75
static DWORD WINAPI smartcard_context_thread(LPVOID arg)
76
0
{
77
0
  SMARTCARD_CONTEXT* pContext = (SMARTCARD_CONTEXT*)arg;
78
0
  DWORD nCount = 0;
79
0
  LONG status = 0;
80
0
  DWORD waitStatus = 0;
81
0
  HANDLE hEvents[2] = { 0 };
82
0
  wMessage message = { 0 };
83
0
  SMARTCARD_DEVICE* smartcard = NULL;
84
0
  UINT error = CHANNEL_RC_OK;
85
0
  smartcard = pContext->smartcard;
86
87
0
  hEvents[nCount++] = MessageQueue_Event(pContext->IrpQueue);
88
89
0
  while (1)
90
0
  {
91
0
    waitStatus = WaitForMultipleObjects(nCount, hEvents, FALSE, INFINITE);
92
93
0
    if (waitStatus == WAIT_FAILED)
94
0
    {
95
0
      error = GetLastError();
96
0
      WLog_ERR(TAG, "WaitForMultipleObjects failed with error %" PRIu32 "!", error);
97
0
      break;
98
0
    }
99
100
0
    waitStatus = WaitForSingleObject(MessageQueue_Event(pContext->IrpQueue), 0);
101
102
0
    if (waitStatus == WAIT_FAILED)
103
0
    {
104
0
      error = GetLastError();
105
0
      WLog_ERR(TAG, "WaitForSingleObject failed with error %" PRIu32 "!", error);
106
0
      break;
107
0
    }
108
109
0
    if (waitStatus == WAIT_OBJECT_0)
110
0
    {
111
0
      scard_irp_queue_element* element = NULL;
112
113
0
      if (!MessageQueue_Peek(pContext->IrpQueue, &message, TRUE))
114
0
      {
115
0
        WLog_ERR(TAG, "MessageQueue_Peek failed!");
116
0
        status = ERROR_INTERNAL_ERROR;
117
0
        break;
118
0
      }
119
120
0
      if (message.id == WMQ_QUIT)
121
0
        break;
122
123
0
      element = (scard_irp_queue_element*)message.wParam;
124
125
0
      if (element)
126
0
      {
127
0
        BOOL handled = FALSE;
128
0
        WINPR_ASSERT(smartcard);
129
130
0
        if ((status = smartcard_irp_device_control_call(
131
0
                 smartcard->callctx, element->irp->output, &element->irp->IoStatus,
132
0
                 &element->operation)))
133
0
        {
134
0
          element->irp->Discard(element->irp);
135
0
          smartcard_operation_free(&element->operation, TRUE);
136
0
          WLog_ERR(TAG, "smartcard_irp_device_control_call failed with error %" PRIu32 "",
137
0
                   status);
138
0
          break;
139
0
        }
140
141
0
        error = smartcard_complete_irp(smartcard, element->irp, &handled);
142
0
        if (!handled)
143
0
          element->irp->Discard(element->irp);
144
0
        smartcard_operation_free(&element->operation, TRUE);
145
146
0
        if (error)
147
0
        {
148
0
          WLog_ERR(TAG, "Queue_Enqueue failed!");
149
0
          break;
150
0
        }
151
0
      }
152
0
    }
153
0
  }
154
155
0
  if (status && smartcard->rdpcontext)
156
0
    setChannelError(smartcard->rdpcontext, error, "smartcard_context_thread reported an error");
157
158
0
  ExitThread((uint32_t)status);
159
0
  return error;
160
0
}
161
162
static void smartcard_operation_queue_free(void* obj)
163
0
{
164
0
  wMessage* msg = obj;
165
0
  if (!msg)
166
0
    return;
167
0
  if (msg->id != 0)
168
0
    return;
169
170
0
  scard_irp_queue_element* element = (scard_irp_queue_element*)msg->wParam;
171
0
  if (!element)
172
0
    return;
173
0
  WINPR_ASSERT(element->irp);
174
0
  WINPR_ASSERT(element->irp->Discard);
175
0
  element->irp->Discard(element->irp);
176
0
  smartcard_operation_free(&element->operation, TRUE);
177
0
}
178
179
static void* smartcard_context_new(void* smartcard, SCARDCONTEXT hContext)
180
0
{
181
0
  SMARTCARD_CONTEXT* pContext = NULL;
182
0
  pContext = (SMARTCARD_CONTEXT*)calloc(1, sizeof(SMARTCARD_CONTEXT));
183
184
0
  if (!pContext)
185
0
  {
186
0
    WLog_ERR(TAG, "calloc failed!");
187
0
    return pContext;
188
0
  }
189
190
0
  pContext->smartcard = smartcard;
191
0
  pContext->hContext = hContext;
192
0
  pContext->IrpQueue = MessageQueue_New(NULL);
193
194
0
  if (!pContext->IrpQueue)
195
0
  {
196
0
    WLog_ERR(TAG, "MessageQueue_New failed!");
197
0
    goto fail;
198
0
  }
199
0
  wObject* obj = MessageQueue_Object(pContext->IrpQueue);
200
0
  WINPR_ASSERT(obj);
201
0
  obj->fnObjectFree = smartcard_operation_queue_free;
202
203
0
  pContext->thread = CreateThread(NULL, 0, smartcard_context_thread, pContext, 0, NULL);
204
205
0
  if (!pContext->thread)
206
0
  {
207
0
    WLog_ERR(TAG, "CreateThread failed!");
208
0
    goto fail;
209
0
  }
210
211
0
  return pContext;
212
0
fail:
213
0
  smartcard_context_free(pContext);
214
0
  return NULL;
215
0
}
216
217
void smartcard_context_free(void* pCtx)
218
0
{
219
0
  SMARTCARD_CONTEXT* pContext = pCtx;
220
221
0
  if (!pContext)
222
0
    return;
223
224
  /* cancel blocking calls like SCardGetStatusChange */
225
0
  WINPR_ASSERT(pContext->smartcard);
226
0
  smartcard_call_cancel_context(pContext->smartcard->callctx, pContext->hContext);
227
228
0
  if (pContext->IrpQueue)
229
0
  {
230
0
    if (MessageQueue_PostQuit(pContext->IrpQueue, 0))
231
0
    {
232
0
      if (WaitForSingleObject(pContext->thread, INFINITE) == WAIT_FAILED)
233
0
        WLog_ERR(TAG, "WaitForSingleObject failed with error %" PRIu32 "!", GetLastError());
234
235
0
      (void)CloseHandle(pContext->thread);
236
0
    }
237
0
    MessageQueue_Free(pContext->IrpQueue);
238
0
  }
239
0
  smartcard_call_release_context(pContext->smartcard->callctx, pContext->hContext);
240
0
  free(pContext);
241
0
}
242
243
static void smartcard_release_all_contexts(SMARTCARD_DEVICE* smartcard)
244
0
{
245
0
  smartcard_call_cancel_all_context(smartcard->callctx);
246
0
}
247
248
static UINT smartcard_free_(SMARTCARD_DEVICE* smartcard)
249
0
{
250
0
  if (!smartcard)
251
0
    return CHANNEL_RC_OK;
252
253
0
  if (smartcard->IrpQueue)
254
0
  {
255
0
    MessageQueue_Free(smartcard->IrpQueue);
256
0
    (void)CloseHandle(smartcard->thread);
257
0
  }
258
259
0
  Stream_Free(smartcard->device.data, TRUE);
260
0
  ListDictionary_Free(smartcard->rgOutstandingMessages);
261
262
0
  smartcard_call_context_free(smartcard->callctx);
263
264
0
  free(smartcard);
265
0
  return CHANNEL_RC_OK;
266
0
}
267
/**
268
 * Function description
269
 *
270
 * @return 0 on success, otherwise a Win32 error code
271
 */
272
static UINT smartcard_free(DEVICE* device)
273
0
{
274
0
  SMARTCARD_DEVICE* smartcard = CAST_FROM_DEVICE(device);
275
276
0
  if (!smartcard)
277
0
    return ERROR_INVALID_PARAMETER;
278
279
  /**
280
   * Calling smartcard_release_all_contexts to unblock all operations waiting for transactions
281
   * to unlock.
282
   */
283
0
  smartcard_release_all_contexts(smartcard);
284
285
  /* Stopping all threads and cancelling all IRPs */
286
287
0
  if (smartcard->IrpQueue)
288
0
  {
289
0
    if (MessageQueue_PostQuit(smartcard->IrpQueue, 0) &&
290
0
        (WaitForSingleObject(smartcard->thread, INFINITE) == WAIT_FAILED))
291
0
    {
292
0
      DWORD error = GetLastError();
293
0
      WLog_ERR(TAG, "WaitForSingleObject failed with error %" PRIu32 "!", error);
294
0
      return error;
295
0
    }
296
0
  }
297
298
0
  if (sSmartcard == smartcard)
299
0
    sSmartcard = NULL;
300
301
0
  return smartcard_free_(smartcard);
302
0
}
303
304
/**
305
 * Initialization occurs when the protocol server sends a device announce message.
306
 * At that time, we need to cancel all outstanding IRPs.
307
 */
308
309
/**
310
 * Function description
311
 *
312
 * @return 0 on success, otherwise a Win32 error code
313
 */
314
static UINT smartcard_init(DEVICE* device)
315
0
{
316
0
  SMARTCARD_DEVICE* smartcard = CAST_FROM_DEVICE(device);
317
318
0
  if (!smartcard)
319
0
    return ERROR_INVALID_PARAMETER;
320
321
0
  smartcard_release_all_contexts(smartcard);
322
0
  return CHANNEL_RC_OK;
323
0
}
324
325
/**
326
 * Function description
327
 *
328
 * @return 0 on success, otherwise a Win32 error code
329
 */
330
UINT smartcard_complete_irp(SMARTCARD_DEVICE* smartcard, IRP* irp, BOOL* handled)
331
0
{
332
0
  WINPR_ASSERT(smartcard);
333
0
  WINPR_ASSERT(irp);
334
0
  WINPR_ASSERT(handled);
335
336
0
  uintptr_t key = (uintptr_t)irp->CompletionId + 1;
337
0
  ListDictionary_Remove(smartcard->rgOutstandingMessages, (void*)key);
338
339
0
  WINPR_ASSERT(irp->Complete);
340
0
  *handled = TRUE;
341
0
  return irp->Complete(irp);
342
0
}
343
344
/**
345
 * Multiple threads and SCardGetStatusChange:
346
 * http://musclecard.996296.n3.nabble.com/Multiple-threads-and-SCardGetStatusChange-td4430.html
347
 */
348
349
/**
350
 * Function description
351
 *
352
 * @return 0 on success, otherwise a Win32 error code
353
 */
354
static UINT smartcard_process_irp(SMARTCARD_DEVICE* smartcard, IRP* irp, BOOL* handled)
355
0
{
356
0
  LONG status = 0;
357
0
  BOOL asyncIrp = FALSE;
358
0
  SMARTCARD_CONTEXT* pContext = NULL;
359
360
0
  WINPR_ASSERT(smartcard);
361
0
  WINPR_ASSERT(handled);
362
0
  WINPR_ASSERT(irp);
363
0
  WINPR_ASSERT(irp->Complete);
364
365
0
  uintptr_t key = (uintptr_t)irp->CompletionId + 1;
366
367
0
  if (!ListDictionary_Add(smartcard->rgOutstandingMessages, (void*)key, irp))
368
0
  {
369
0
    WLog_ERR(TAG, "ListDictionary_Add failed!");
370
0
    return ERROR_INTERNAL_ERROR;
371
0
  }
372
373
0
  if (irp->MajorFunction == IRP_MJ_DEVICE_CONTROL)
374
0
  {
375
0
    scard_irp_queue_element* element = calloc(1, sizeof(scard_irp_queue_element));
376
0
    if (!element)
377
0
      return ERROR_OUTOFMEMORY;
378
379
0
    element->irp = irp;
380
0
    element->operation.completionID = irp->CompletionId;
381
382
0
    status = smartcard_irp_device_control_decode(irp->input, irp->CompletionId, irp->FileId,
383
0
                                                 &element->operation);
384
385
0
    if (status != SCARD_S_SUCCESS)
386
0
    {
387
0
      UINT error = 0;
388
389
0
      smartcard_operation_free(&element->operation, TRUE);
390
0
      irp->IoStatus = STATUS_UNSUCCESSFUL;
391
392
0
      if ((error = smartcard_complete_irp(smartcard, irp, handled)))
393
0
      {
394
0
        WLog_ERR(TAG, "Queue_Enqueue failed!");
395
0
        return error;
396
0
      }
397
398
0
      return CHANNEL_RC_OK;
399
0
    }
400
401
0
    asyncIrp = TRUE;
402
403
0
    switch (element->operation.ioControlCode)
404
0
    {
405
0
      case SCARD_IOCTL_ESTABLISHCONTEXT:
406
0
      case SCARD_IOCTL_RELEASECONTEXT:
407
0
      case SCARD_IOCTL_ISVALIDCONTEXT:
408
0
      case SCARD_IOCTL_CANCEL:
409
0
      case SCARD_IOCTL_ACCESSSTARTEDEVENT:
410
0
      case SCARD_IOCTL_RELEASETARTEDEVENT:
411
0
        asyncIrp = FALSE;
412
0
        break;
413
414
0
      case SCARD_IOCTL_LISTREADERGROUPSA:
415
0
      case SCARD_IOCTL_LISTREADERGROUPSW:
416
0
      case SCARD_IOCTL_LISTREADERSA:
417
0
      case SCARD_IOCTL_LISTREADERSW:
418
0
      case SCARD_IOCTL_INTRODUCEREADERGROUPA:
419
0
      case SCARD_IOCTL_INTRODUCEREADERGROUPW:
420
0
      case SCARD_IOCTL_FORGETREADERGROUPA:
421
0
      case SCARD_IOCTL_FORGETREADERGROUPW:
422
0
      case SCARD_IOCTL_INTRODUCEREADERA:
423
0
      case SCARD_IOCTL_INTRODUCEREADERW:
424
0
      case SCARD_IOCTL_FORGETREADERA:
425
0
      case SCARD_IOCTL_FORGETREADERW:
426
0
      case SCARD_IOCTL_ADDREADERTOGROUPA:
427
0
      case SCARD_IOCTL_ADDREADERTOGROUPW:
428
0
      case SCARD_IOCTL_REMOVEREADERFROMGROUPA:
429
0
      case SCARD_IOCTL_REMOVEREADERFROMGROUPW:
430
0
      case SCARD_IOCTL_LOCATECARDSA:
431
0
      case SCARD_IOCTL_LOCATECARDSW:
432
0
      case SCARD_IOCTL_LOCATECARDSBYATRA:
433
0
      case SCARD_IOCTL_LOCATECARDSBYATRW:
434
0
      case SCARD_IOCTL_READCACHEA:
435
0
      case SCARD_IOCTL_READCACHEW:
436
0
      case SCARD_IOCTL_WRITECACHEA:
437
0
      case SCARD_IOCTL_WRITECACHEW:
438
0
      case SCARD_IOCTL_GETREADERICON:
439
0
      case SCARD_IOCTL_GETDEVICETYPEID:
440
0
      case SCARD_IOCTL_GETSTATUSCHANGEA:
441
0
      case SCARD_IOCTL_GETSTATUSCHANGEW:
442
0
      case SCARD_IOCTL_CONNECTA:
443
0
      case SCARD_IOCTL_CONNECTW:
444
0
      case SCARD_IOCTL_RECONNECT:
445
0
      case SCARD_IOCTL_DISCONNECT:
446
0
      case SCARD_IOCTL_BEGINTRANSACTION:
447
0
      case SCARD_IOCTL_ENDTRANSACTION:
448
0
      case SCARD_IOCTL_STATE:
449
0
      case SCARD_IOCTL_STATUSA:
450
0
      case SCARD_IOCTL_STATUSW:
451
0
      case SCARD_IOCTL_TRANSMIT:
452
0
      case SCARD_IOCTL_CONTROL:
453
0
      case SCARD_IOCTL_GETATTRIB:
454
0
      case SCARD_IOCTL_SETATTRIB:
455
0
      case SCARD_IOCTL_GETTRANSMITCOUNT:
456
0
        asyncIrp = TRUE;
457
0
        break;
458
0
      default:
459
0
        break;
460
0
    }
461
462
0
    pContext = smartcard_call_get_context(smartcard->callctx, element->operation.hContext);
463
464
0
    if (!pContext)
465
0
      asyncIrp = FALSE;
466
467
0
    if (!asyncIrp)
468
0
    {
469
0
      UINT error = 0;
470
471
0
      status =
472
0
          smartcard_irp_device_control_call(smartcard->callctx, element->irp->output,
473
0
                                            &element->irp->IoStatus, &element->operation);
474
0
      smartcard_operation_free(&element->operation, TRUE);
475
476
0
      if (status)
477
0
      {
478
0
        WLog_ERR(TAG, "smartcard_irp_device_control_call failed with error %" PRId32 "!",
479
0
                 status);
480
0
        return (UINT32)status;
481
0
      }
482
483
0
      if ((error = smartcard_complete_irp(smartcard, irp, handled)))
484
0
      {
485
0
        WLog_ERR(TAG, "Queue_Enqueue failed!");
486
0
        return error;
487
0
      }
488
0
    }
489
0
    else
490
0
    {
491
0
      if (pContext)
492
0
      {
493
0
        if (!MessageQueue_Post(pContext->IrpQueue, NULL, 0, (void*)element, NULL))
494
0
        {
495
0
          smartcard_operation_free(&element->operation, TRUE);
496
0
          WLog_ERR(TAG, "MessageQueue_Post failed!");
497
0
          return ERROR_INTERNAL_ERROR;
498
0
        }
499
0
        *handled = TRUE;
500
0
      }
501
0
    }
502
0
  }
503
0
  else
504
0
  {
505
0
    UINT ustatus = 0;
506
0
    WLog_ERR(TAG, "Unexpected SmartCard IRP: MajorFunction %s, MinorFunction: 0x%08" PRIX32 "",
507
0
             rdpdr_irp_string(irp->MajorFunction), irp->MinorFunction);
508
0
    irp->IoStatus = STATUS_NOT_SUPPORTED;
509
510
0
    if ((ustatus = smartcard_complete_irp(smartcard, irp, handled)))
511
0
    {
512
0
      WLog_ERR(TAG, "Queue_Enqueue failed!");
513
0
      return ustatus;
514
0
    }
515
0
  }
516
517
0
  return CHANNEL_RC_OK;
518
0
}
519
520
static DWORD WINAPI smartcard_thread_func(LPVOID arg)
521
0
{
522
0
  IRP* irp = NULL;
523
0
  DWORD nCount = 0;
524
0
  DWORD status = 0;
525
0
  HANDLE hEvents[1] = { 0 };
526
0
  wMessage message = { 0 };
527
0
  UINT error = CHANNEL_RC_OK;
528
0
  SMARTCARD_DEVICE* smartcard = CAST_FROM_DEVICE(arg);
529
530
0
  if (!smartcard)
531
0
    return ERROR_INVALID_PARAMETER;
532
533
0
  hEvents[nCount++] = MessageQueue_Event(smartcard->IrpQueue);
534
535
0
  while (1)
536
0
  {
537
0
    status = WaitForMultipleObjects(nCount, hEvents, FALSE, INFINITE);
538
539
0
    if (status == WAIT_FAILED)
540
0
    {
541
0
      error = GetLastError();
542
0
      WLog_ERR(TAG, "WaitForMultipleObjects failed with error %" PRIu32 "!", error);
543
0
      break;
544
0
    }
545
546
0
    if (status == WAIT_OBJECT_0)
547
0
    {
548
0
      if (!MessageQueue_Peek(smartcard->IrpQueue, &message, TRUE))
549
0
      {
550
0
        WLog_ERR(TAG, "MessageQueue_Peek failed!");
551
0
        error = ERROR_INTERNAL_ERROR;
552
0
        break;
553
0
      }
554
555
0
      if (message.id == WMQ_QUIT)
556
0
        break;
557
558
0
      irp = (IRP*)message.wParam;
559
560
0
      if (irp)
561
0
      {
562
0
        BOOL handled = FALSE;
563
0
        if ((error = smartcard_process_irp(smartcard, irp, &handled)))
564
0
        {
565
0
          WLog_ERR(TAG, "smartcard_process_irp failed with error %" PRIu32 "!", error);
566
0
          goto out;
567
0
        }
568
0
        if (!handled)
569
0
        {
570
0
          WINPR_ASSERT(irp->Discard);
571
0
          irp->Discard(irp);
572
0
        }
573
0
      }
574
0
    }
575
0
  }
576
577
0
out:
578
579
0
  if (error && smartcard->rdpcontext)
580
0
    setChannelError(smartcard->rdpcontext, error, "smartcard_thread_func reported an error");
581
582
0
  ExitThread(error);
583
0
  return error;
584
0
}
585
586
/**
587
 * Function description
588
 *
589
 * @return 0 on success, otherwise a Win32 error code
590
 */
591
static UINT smartcard_irp_request(DEVICE* device, IRP* irp)
592
0
{
593
0
  SMARTCARD_DEVICE* smartcard = CAST_FROM_DEVICE(device);
594
595
0
  if (!smartcard)
596
0
    return ERROR_INVALID_PARAMETER;
597
598
0
  if (!MessageQueue_Post(smartcard->IrpQueue, NULL, 0, (void*)irp, NULL))
599
0
  {
600
0
    WLog_ERR(TAG, "MessageQueue_Post failed!");
601
0
    return ERROR_INTERNAL_ERROR;
602
0
  }
603
604
0
  return CHANNEL_RC_OK;
605
0
}
606
607
static void smartcard_free_irp(void* obj)
608
0
{
609
0
  wMessage* msg = obj;
610
0
  if (!msg)
611
0
    return;
612
0
  if (msg->id != 0)
613
0
    return;
614
615
0
  IRP* irp = (IRP*)msg->wParam;
616
0
  if (!irp)
617
0
    return;
618
0
  WINPR_ASSERT(irp->Discard);
619
0
  irp->Discard(irp);
620
0
}
621
622
/* smartcard is always built-in */
623
#define DeviceServiceEntry smartcard_DeviceServiceEntry
624
625
/**
626
 * Function description
627
 *
628
 * @return 0 on success, otherwise a Win32 error code
629
 */
630
FREERDP_ENTRY_POINT(UINT VCAPITYPE DeviceServiceEntry(PDEVICE_SERVICE_ENTRY_POINTS pEntryPoints))
631
0
{
632
0
  SMARTCARD_DEVICE* smartcard = NULL;
633
0
  size_t length = 0;
634
0
  UINT error = CHANNEL_RC_NO_MEMORY;
635
636
0
  if (!sSmartcard)
637
0
  {
638
0
    smartcard = (SMARTCARD_DEVICE*)calloc(1, sizeof(SMARTCARD_DEVICE));
639
640
0
    if (!smartcard)
641
0
    {
642
0
      WLog_ERR(TAG, "calloc failed!");
643
0
      return CHANNEL_RC_NO_MEMORY;
644
0
    }
645
646
0
    smartcard->device.type = RDPDR_DTYP_SMARTCARD;
647
0
    smartcard->device.name = "SCARD";
648
0
    smartcard->device.IRPRequest = smartcard_irp_request;
649
0
    smartcard->device.Init = smartcard_init;
650
0
    smartcard->device.Free = smartcard_free;
651
0
    smartcard->rdpcontext = pEntryPoints->rdpcontext;
652
0
    length = strlen(smartcard->device.name);
653
0
    smartcard->device.data = Stream_New(NULL, length + 1);
654
655
0
    if (!smartcard->device.data)
656
0
    {
657
0
      WLog_ERR(TAG, "Stream_New failed!");
658
0
      goto fail;
659
0
    }
660
661
0
    Stream_Write(smartcard->device.data, "SCARD", 6);
662
0
    smartcard->IrpQueue = MessageQueue_New(NULL);
663
664
0
    if (!smartcard->IrpQueue)
665
0
    {
666
0
      WLog_ERR(TAG, "MessageQueue_New failed!");
667
0
      goto fail;
668
0
    }
669
670
0
    wObject* obj = MessageQueue_Object(smartcard->IrpQueue);
671
0
    WINPR_ASSERT(obj);
672
0
    obj->fnObjectFree = smartcard_free_irp;
673
674
0
    smartcard->rgOutstandingMessages = ListDictionary_New(TRUE);
675
676
0
    if (!smartcard->rgOutstandingMessages)
677
0
    {
678
0
      WLog_ERR(TAG, "ListDictionary_New failed!");
679
0
      goto fail;
680
0
    }
681
682
0
    smartcard->callctx = smartcard_call_context_new(smartcard->rdpcontext->settings);
683
0
    if (!smartcard->callctx)
684
0
      goto fail;
685
686
0
    if (!smarcard_call_set_callbacks(smartcard->callctx, smartcard, smartcard_context_new,
687
0
                                     smartcard_context_free))
688
0
      goto fail;
689
690
0
    if ((error = pEntryPoints->RegisterDevice(pEntryPoints->devman, &smartcard->device)))
691
0
    {
692
0
      WLog_ERR(TAG, "RegisterDevice failed!");
693
0
      goto fail;
694
0
    }
695
696
0
    smartcard->thread =
697
0
        CreateThread(NULL, 0, smartcard_thread_func, smartcard, CREATE_SUSPENDED, NULL);
698
699
0
    if (!smartcard->thread)
700
0
    {
701
0
      WLog_ERR(TAG, "ListDictionary_New failed!");
702
0
      error = ERROR_INTERNAL_ERROR;
703
0
      goto fail;
704
0
    }
705
706
0
    ResumeThread(smartcard->thread);
707
0
  }
708
0
  else
709
0
    smartcard = sSmartcard;
710
711
0
  if (pEntryPoints->device->Name)
712
0
  {
713
0
    smartcard_call_context_add(smartcard->callctx, pEntryPoints->device->Name);
714
0
  }
715
716
0
  sSmartcard = smartcard;
717
0
  return CHANNEL_RC_OK;
718
0
fail:
719
0
  smartcard_free_(smartcard);
720
0
  return error;
721
0
}