Coverage Report

Created: 2024-05-20 06:11

/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(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
      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
    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 = (UINT32)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
    }
459
460
0
    pContext = smartcard_call_get_context(smartcard->callctx, element->operation.hContext);
461
462
0
    if (!pContext)
463
0
      asyncIrp = FALSE;
464
465
0
    if (!asyncIrp)
466
0
    {
467
0
      UINT error = 0;
468
469
0
      status =
470
0
          smartcard_irp_device_control_call(smartcard->callctx, element->irp->output,
471
0
                                            &element->irp->IoStatus, &element->operation);
472
0
      smartcard_operation_free(&element->operation, TRUE);
473
474
0
      if (status)
475
0
      {
476
0
        WLog_ERR(TAG, "smartcard_irp_device_control_call failed with error %" PRId32 "!",
477
0
                 status);
478
0
        return (UINT32)status;
479
0
      }
480
481
0
      if ((error = smartcard_complete_irp(smartcard, irp, handled)))
482
0
      {
483
0
        WLog_ERR(TAG, "Queue_Enqueue failed!");
484
0
        return error;
485
0
      }
486
0
    }
487
0
    else
488
0
    {
489
0
      if (pContext)
490
0
      {
491
0
        if (!MessageQueue_Post(pContext->IrpQueue, NULL, 0, (void*)element, NULL))
492
0
        {
493
0
          smartcard_operation_free(&element->operation, TRUE);
494
0
          WLog_ERR(TAG, "MessageQueue_Post failed!");
495
0
          return ERROR_INTERNAL_ERROR;
496
0
        }
497
0
        *handled = TRUE;
498
0
      }
499
0
    }
500
0
  }
501
0
  else
502
0
  {
503
0
    UINT ustatus = 0;
504
0
    WLog_ERR(TAG, "Unexpected SmartCard IRP: MajorFunction %s, MinorFunction: 0x%08" PRIX32 "",
505
0
             rdpdr_irp_string(irp->MajorFunction), irp->MinorFunction);
506
0
    irp->IoStatus = (UINT32)STATUS_NOT_SUPPORTED;
507
508
0
    if ((ustatus = smartcard_complete_irp(smartcard, irp, handled)))
509
0
    {
510
0
      WLog_ERR(TAG, "Queue_Enqueue failed!");
511
0
      return ustatus;
512
0
    }
513
0
  }
514
515
0
  return CHANNEL_RC_OK;
516
0
}
517
518
static DWORD WINAPI smartcard_thread_func(LPVOID arg)
519
0
{
520
0
  IRP* irp = NULL;
521
0
  DWORD nCount = 0;
522
0
  DWORD status = 0;
523
0
  HANDLE hEvents[1] = { 0 };
524
0
  wMessage message = { 0 };
525
0
  UINT error = CHANNEL_RC_OK;
526
0
  SMARTCARD_DEVICE* smartcard = CAST_FROM_DEVICE(arg);
527
528
0
  if (!smartcard)
529
0
    return ERROR_INVALID_PARAMETER;
530
531
0
  hEvents[nCount++] = MessageQueue_Event(smartcard->IrpQueue);
532
533
0
  while (1)
534
0
  {
535
0
    status = WaitForMultipleObjects(nCount, hEvents, FALSE, INFINITE);
536
537
0
    if (status == WAIT_FAILED)
538
0
    {
539
0
      error = GetLastError();
540
0
      WLog_ERR(TAG, "WaitForMultipleObjects failed with error %" PRIu32 "!", error);
541
0
      break;
542
0
    }
543
544
0
    if (status == WAIT_OBJECT_0)
545
0
    {
546
0
      if (!MessageQueue_Peek(smartcard->IrpQueue, &message, TRUE))
547
0
      {
548
0
        WLog_ERR(TAG, "MessageQueue_Peek failed!");
549
0
        error = ERROR_INTERNAL_ERROR;
550
0
        break;
551
0
      }
552
553
0
      if (message.id == WMQ_QUIT)
554
0
        break;
555
556
0
      irp = (IRP*)message.wParam;
557
558
0
      if (irp)
559
0
      {
560
0
        BOOL handled = FALSE;
561
0
        if ((error = smartcard_process_irp(smartcard, irp, &handled)))
562
0
        {
563
0
          WLog_ERR(TAG, "smartcard_process_irp failed with error %" PRIu32 "!", error);
564
0
          goto out;
565
0
        }
566
0
        if (!handled)
567
0
        {
568
0
          WINPR_ASSERT(irp->Discard);
569
0
          irp->Discard(irp);
570
0
        }
571
0
      }
572
0
    }
573
0
  }
574
575
0
out:
576
577
0
  if (error && smartcard->rdpcontext)
578
0
    setChannelError(smartcard->rdpcontext, error, "smartcard_thread_func reported an error");
579
580
0
  ExitThread(error);
581
0
  return error;
582
0
}
583
584
/**
585
 * Function description
586
 *
587
 * @return 0 on success, otherwise a Win32 error code
588
 */
589
static UINT smartcard_irp_request(DEVICE* device, IRP* irp)
590
0
{
591
0
  SMARTCARD_DEVICE* smartcard = CAST_FROM_DEVICE(device);
592
593
0
  if (!smartcard)
594
0
    return ERROR_INVALID_PARAMETER;
595
596
0
  if (!MessageQueue_Post(smartcard->IrpQueue, NULL, 0, (void*)irp, NULL))
597
0
  {
598
0
    WLog_ERR(TAG, "MessageQueue_Post failed!");
599
0
    return ERROR_INTERNAL_ERROR;
600
0
  }
601
602
0
  return CHANNEL_RC_OK;
603
0
}
604
605
static void smartcard_free_irp(void* obj)
606
0
{
607
0
  wMessage* msg = obj;
608
0
  if (!msg)
609
0
    return;
610
0
  if (msg->id != 0)
611
0
    return;
612
613
0
  IRP* irp = (IRP*)msg->wParam;
614
0
  if (!irp)
615
0
    return;
616
0
  WINPR_ASSERT(irp->Discard);
617
0
  irp->Discard(irp);
618
0
}
619
620
/* smartcard is always built-in */
621
#define DeviceServiceEntry smartcard_DeviceServiceEntry
622
623
/**
624
 * Function description
625
 *
626
 * @return 0 on success, otherwise a Win32 error code
627
 */
628
extern UINT DeviceServiceEntry(PDEVICE_SERVICE_ENTRY_POINTS pEntryPoints);
629
UINT DeviceServiceEntry(PDEVICE_SERVICE_ENTRY_POINTS pEntryPoints)
630
0
{
631
0
  SMARTCARD_DEVICE* smartcard = NULL;
632
0
  size_t length = 0;
633
0
  UINT error = CHANNEL_RC_NO_MEMORY;
634
635
0
  if (!sSmartcard)
636
0
  {
637
0
    smartcard = (SMARTCARD_DEVICE*)calloc(1, sizeof(SMARTCARD_DEVICE));
638
639
0
    if (!smartcard)
640
0
    {
641
0
      WLog_ERR(TAG, "calloc failed!");
642
0
      return CHANNEL_RC_NO_MEMORY;
643
0
    }
644
645
0
    smartcard->device.type = RDPDR_DTYP_SMARTCARD;
646
0
    smartcard->device.name = "SCARD";
647
0
    smartcard->device.IRPRequest = smartcard_irp_request;
648
0
    smartcard->device.Init = smartcard_init;
649
0
    smartcard->device.Free = smartcard_free;
650
0
    smartcard->rdpcontext = pEntryPoints->rdpcontext;
651
0
    length = strlen(smartcard->device.name);
652
0
    smartcard->device.data = Stream_New(NULL, length + 1);
653
654
0
    if (!smartcard->device.data)
655
0
    {
656
0
      WLog_ERR(TAG, "Stream_New failed!");
657
0
      goto fail;
658
0
    }
659
660
0
    Stream_Write(smartcard->device.data, "SCARD", 6);
661
0
    smartcard->IrpQueue = MessageQueue_New(NULL);
662
663
0
    if (!smartcard->IrpQueue)
664
0
    {
665
0
      WLog_ERR(TAG, "MessageQueue_New failed!");
666
0
      goto fail;
667
0
    }
668
669
0
    wObject* obj = MessageQueue_Object(smartcard->IrpQueue);
670
0
    WINPR_ASSERT(obj);
671
0
    obj->fnObjectFree = smartcard_free_irp;
672
673
0
    smartcard->rgOutstandingMessages = ListDictionary_New(TRUE);
674
675
0
    if (!smartcard->rgOutstandingMessages)
676
0
    {
677
0
      WLog_ERR(TAG, "ListDictionary_New failed!");
678
0
      goto fail;
679
0
    }
680
681
0
    smartcard->callctx = smartcard_call_context_new(smartcard->rdpcontext->settings);
682
0
    if (!smartcard->callctx)
683
0
      goto fail;
684
685
0
    if (!smarcard_call_set_callbacks(smartcard->callctx, smartcard, smartcard_context_new,
686
0
                                     smartcard_context_free))
687
0
      goto fail;
688
689
0
    if ((error = pEntryPoints->RegisterDevice(pEntryPoints->devman, &smartcard->device)))
690
0
    {
691
0
      WLog_ERR(TAG, "RegisterDevice failed!");
692
0
      goto fail;
693
0
    }
694
695
0
    smartcard->thread =
696
0
        CreateThread(NULL, 0, smartcard_thread_func, smartcard, CREATE_SUSPENDED, NULL);
697
698
0
    if (!smartcard->thread)
699
0
    {
700
0
      WLog_ERR(TAG, "ListDictionary_New failed!");
701
0
      error = ERROR_INTERNAL_ERROR;
702
0
      goto fail;
703
0
    }
704
705
0
    ResumeThread(smartcard->thread);
706
0
  }
707
0
  else
708
0
    smartcard = sSmartcard;
709
710
0
  if (pEntryPoints->device->Name)
711
0
  {
712
0
    smartcard_call_context_add(smartcard->callctx, pEntryPoints->device->Name);
713
0
  }
714
715
0
  sSmartcard = smartcard;
716
0
  return CHANNEL_RC_OK;
717
0
fail:
718
0
  smartcard_free_(smartcard);
719
0
  return error;
720
0
}