Coverage Report

Created: 2026-01-09 06:49

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/FreeRDP/channels/smartcard/client/smartcard_main.c
Line
Count
Source
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
200
0
  {
201
0
    wObject* obj = MessageQueue_Object(pContext->IrpQueue);
202
0
    WINPR_ASSERT(obj);
203
0
    obj->fnObjectFree = smartcard_operation_queue_free;
204
0
  }
205
206
0
  pContext->thread = CreateThread(NULL, 0, smartcard_context_thread, pContext, 0, NULL);
207
208
0
  if (!pContext->thread)
209
0
  {
210
0
    WLog_ERR(TAG, "CreateThread failed!");
211
0
    goto fail;
212
0
  }
213
214
0
  return pContext;
215
0
fail:
216
0
  smartcard_context_free(pContext);
217
0
  return NULL;
218
0
}
219
220
void smartcard_context_free(void* pCtx)
221
0
{
222
0
  SMARTCARD_CONTEXT* pContext = pCtx;
223
224
0
  if (!pContext)
225
0
    return;
226
227
  /* cancel blocking calls like SCardGetStatusChange */
228
0
  WINPR_ASSERT(pContext->smartcard);
229
0
  smartcard_call_cancel_context(pContext->smartcard->callctx, pContext->hContext);
230
231
0
  if (pContext->IrpQueue)
232
0
  {
233
0
    if (MessageQueue_PostQuit(pContext->IrpQueue, 0))
234
0
    {
235
0
      if (WaitForSingleObject(pContext->thread, INFINITE) == WAIT_FAILED)
236
0
        WLog_ERR(TAG, "WaitForSingleObject failed with error %" PRIu32 "!", GetLastError());
237
238
0
      (void)CloseHandle(pContext->thread);
239
0
    }
240
0
    MessageQueue_Free(pContext->IrpQueue);
241
0
  }
242
0
  smartcard_call_release_context(pContext->smartcard->callctx, pContext->hContext);
243
0
  free(pContext);
244
0
}
245
246
static void smartcard_release_all_contexts(SMARTCARD_DEVICE* smartcard)
247
0
{
248
0
  smartcard_call_cancel_all_context(smartcard->callctx);
249
0
}
250
251
static UINT smartcard_free_(SMARTCARD_DEVICE* smartcard)
252
0
{
253
0
  if (!smartcard)
254
0
    return CHANNEL_RC_OK;
255
256
0
  if (smartcard->IrpQueue)
257
0
  {
258
0
    MessageQueue_Free(smartcard->IrpQueue);
259
0
    (void)CloseHandle(smartcard->thread);
260
0
  }
261
262
0
  Stream_Free(smartcard->device.data, TRUE);
263
0
  ListDictionary_Free(smartcard->rgOutstandingMessages);
264
265
0
  smartcard_call_context_free(smartcard->callctx);
266
267
0
  free(smartcard);
268
0
  return CHANNEL_RC_OK;
269
0
}
270
/**
271
 * Function description
272
 *
273
 * @return 0 on success, otherwise a Win32 error code
274
 */
275
static UINT smartcard_free(DEVICE* device)
276
0
{
277
0
  SMARTCARD_DEVICE* smartcard = CAST_FROM_DEVICE(device);
278
279
0
  if (!smartcard)
280
0
    return ERROR_INVALID_PARAMETER;
281
282
  /**
283
   * Calling smartcard_release_all_contexts to unblock all operations waiting for transactions
284
   * to unlock.
285
   */
286
0
  smartcard_release_all_contexts(smartcard);
287
288
  /* Stopping all threads and cancelling all IRPs */
289
290
0
  if (smartcard->IrpQueue)
291
0
  {
292
0
    if (MessageQueue_PostQuit(smartcard->IrpQueue, 0) &&
293
0
        (WaitForSingleObject(smartcard->thread, INFINITE) == WAIT_FAILED))
294
0
    {
295
0
      DWORD error = GetLastError();
296
0
      WLog_ERR(TAG, "WaitForSingleObject failed with error %" PRIu32 "!", error);
297
0
      return error;
298
0
    }
299
0
  }
300
301
0
  if (sSmartcard == smartcard)
302
0
    sSmartcard = NULL;
303
304
0
  return smartcard_free_(smartcard);
305
0
}
306
307
/**
308
 * Initialization occurs when the protocol server sends a device announce message.
309
 * At that time, we need to cancel all outstanding IRPs.
310
 */
311
312
/**
313
 * Function description
314
 *
315
 * @return 0 on success, otherwise a Win32 error code
316
 */
317
static UINT smartcard_init(DEVICE* device)
318
0
{
319
0
  SMARTCARD_DEVICE* smartcard = CAST_FROM_DEVICE(device);
320
321
0
  if (!smartcard)
322
0
    return ERROR_INVALID_PARAMETER;
323
324
0
  smartcard_release_all_contexts(smartcard);
325
0
  return CHANNEL_RC_OK;
326
0
}
327
328
/**
329
 * Function description
330
 *
331
 * @return 0 on success, otherwise a Win32 error code
332
 */
333
UINT smartcard_complete_irp(SMARTCARD_DEVICE* smartcard, IRP* irp, BOOL* handled)
334
0
{
335
0
  WINPR_ASSERT(smartcard);
336
0
  WINPR_ASSERT(irp);
337
0
  WINPR_ASSERT(handled);
338
339
0
  uintptr_t key = (uintptr_t)irp->CompletionId + 1;
340
0
  ListDictionary_Remove(smartcard->rgOutstandingMessages, (void*)key);
341
342
0
  WINPR_ASSERT(irp->Complete);
343
0
  *handled = TRUE;
344
0
  return irp->Complete(irp);
345
0
}
346
347
/**
348
 * Multiple threads and SCardGetStatusChange:
349
 * http://musclecard.996296.n3.nabble.com/Multiple-threads-and-SCardGetStatusChange-td4430.html
350
 */
351
352
/**
353
 * Function description
354
 *
355
 * @return 0 on success, otherwise a Win32 error code
356
 */
357
static UINT smartcard_process_irp(SMARTCARD_DEVICE* smartcard, IRP* irp, BOOL* handled)
358
0
{
359
0
  LONG status = 0;
360
0
  BOOL asyncIrp = FALSE;
361
0
  SMARTCARD_CONTEXT* pContext = NULL;
362
363
0
  WINPR_ASSERT(smartcard);
364
0
  WINPR_ASSERT(handled);
365
0
  WINPR_ASSERT(irp);
366
0
  WINPR_ASSERT(irp->Complete);
367
368
0
  uintptr_t key = (uintptr_t)irp->CompletionId + 1;
369
370
0
  if (!ListDictionary_Add(smartcard->rgOutstandingMessages, (void*)key, irp))
371
0
  {
372
0
    WLog_ERR(TAG, "ListDictionary_Add failed!");
373
0
    return ERROR_INTERNAL_ERROR;
374
0
  }
375
376
0
  if (irp->MajorFunction == IRP_MJ_DEVICE_CONTROL)
377
0
  {
378
0
    scard_irp_queue_element* element = calloc(1, sizeof(scard_irp_queue_element));
379
0
    if (!element)
380
0
      return ERROR_OUTOFMEMORY;
381
382
0
    element->irp = irp;
383
0
    element->operation.completionID = irp->CompletionId;
384
385
0
    status = smartcard_irp_device_control_decode(irp->input, irp->CompletionId, irp->FileId,
386
0
                                                 &element->operation);
387
388
0
    if (status != SCARD_S_SUCCESS)
389
0
    {
390
0
      UINT error = 0;
391
392
0
      smartcard_operation_free(&element->operation, TRUE);
393
0
      irp->IoStatus = STATUS_UNSUCCESSFUL;
394
395
0
      if ((error = smartcard_complete_irp(smartcard, irp, handled)))
396
0
      {
397
0
        WLog_ERR(TAG, "Queue_Enqueue failed!");
398
0
        return error;
399
0
      }
400
401
0
      return CHANNEL_RC_OK;
402
0
    }
403
404
0
    asyncIrp = TRUE;
405
406
0
    switch (element->operation.ioControlCode)
407
0
    {
408
0
      case SCARD_IOCTL_ESTABLISHCONTEXT:
409
0
      case SCARD_IOCTL_RELEASECONTEXT:
410
0
      case SCARD_IOCTL_ISVALIDCONTEXT:
411
0
      case SCARD_IOCTL_CANCEL:
412
0
      case SCARD_IOCTL_ACCESSSTARTEDEVENT:
413
0
      case SCARD_IOCTL_RELEASETARTEDEVENT:
414
0
        asyncIrp = FALSE;
415
0
        break;
416
417
0
      case SCARD_IOCTL_LISTREADERGROUPSA:
418
0
      case SCARD_IOCTL_LISTREADERGROUPSW:
419
0
      case SCARD_IOCTL_LISTREADERSA:
420
0
      case SCARD_IOCTL_LISTREADERSW:
421
0
      case SCARD_IOCTL_INTRODUCEREADERGROUPA:
422
0
      case SCARD_IOCTL_INTRODUCEREADERGROUPW:
423
0
      case SCARD_IOCTL_FORGETREADERGROUPA:
424
0
      case SCARD_IOCTL_FORGETREADERGROUPW:
425
0
      case SCARD_IOCTL_INTRODUCEREADERA:
426
0
      case SCARD_IOCTL_INTRODUCEREADERW:
427
0
      case SCARD_IOCTL_FORGETREADERA:
428
0
      case SCARD_IOCTL_FORGETREADERW:
429
0
      case SCARD_IOCTL_ADDREADERTOGROUPA:
430
0
      case SCARD_IOCTL_ADDREADERTOGROUPW:
431
0
      case SCARD_IOCTL_REMOVEREADERFROMGROUPA:
432
0
      case SCARD_IOCTL_REMOVEREADERFROMGROUPW:
433
0
      case SCARD_IOCTL_LOCATECARDSA:
434
0
      case SCARD_IOCTL_LOCATECARDSW:
435
0
      case SCARD_IOCTL_LOCATECARDSBYATRA:
436
0
      case SCARD_IOCTL_LOCATECARDSBYATRW:
437
0
      case SCARD_IOCTL_READCACHEA:
438
0
      case SCARD_IOCTL_READCACHEW:
439
0
      case SCARD_IOCTL_WRITECACHEA:
440
0
      case SCARD_IOCTL_WRITECACHEW:
441
0
      case SCARD_IOCTL_GETREADERICON:
442
0
      case SCARD_IOCTL_GETDEVICETYPEID:
443
0
      case SCARD_IOCTL_GETSTATUSCHANGEA:
444
0
      case SCARD_IOCTL_GETSTATUSCHANGEW:
445
0
      case SCARD_IOCTL_CONNECTA:
446
0
      case SCARD_IOCTL_CONNECTW:
447
0
      case SCARD_IOCTL_RECONNECT:
448
0
      case SCARD_IOCTL_DISCONNECT:
449
0
      case SCARD_IOCTL_BEGINTRANSACTION:
450
0
      case SCARD_IOCTL_ENDTRANSACTION:
451
0
      case SCARD_IOCTL_STATE:
452
0
      case SCARD_IOCTL_STATUSA:
453
0
      case SCARD_IOCTL_STATUSW:
454
0
      case SCARD_IOCTL_TRANSMIT:
455
0
      case SCARD_IOCTL_CONTROL:
456
0
      case SCARD_IOCTL_GETATTRIB:
457
0
      case SCARD_IOCTL_SETATTRIB:
458
0
      case SCARD_IOCTL_GETTRANSMITCOUNT:
459
0
        asyncIrp = TRUE;
460
0
        break;
461
0
      default:
462
0
        break;
463
0
    }
464
465
0
    pContext = smartcard_call_get_context(smartcard->callctx, element->operation.hContext);
466
467
0
    if (!pContext)
468
0
      asyncIrp = FALSE;
469
470
0
    if (!asyncIrp)
471
0
    {
472
0
      UINT error = 0;
473
474
0
      status =
475
0
          smartcard_irp_device_control_call(smartcard->callctx, element->irp->output,
476
0
                                            &element->irp->IoStatus, &element->operation);
477
0
      smartcard_operation_free(&element->operation, TRUE);
478
479
0
      if (status)
480
0
      {
481
0
        WLog_ERR(TAG, "smartcard_irp_device_control_call failed with error %" PRId32 "!",
482
0
                 status);
483
0
        return (UINT32)status;
484
0
      }
485
486
0
      if ((error = smartcard_complete_irp(smartcard, irp, handled)))
487
0
      {
488
0
        WLog_ERR(TAG, "Queue_Enqueue failed!");
489
0
        return error;
490
0
      }
491
0
    }
492
0
    else
493
0
    {
494
0
      if (pContext)
495
0
      {
496
0
        if (!MessageQueue_Post(pContext->IrpQueue, NULL, 0, (void*)element, NULL))
497
0
        {
498
0
          smartcard_operation_free(&element->operation, TRUE);
499
0
          WLog_ERR(TAG, "MessageQueue_Post failed!");
500
0
          return ERROR_INTERNAL_ERROR;
501
0
        }
502
0
        *handled = TRUE;
503
0
      }
504
0
    }
505
0
  }
506
0
  else
507
0
  {
508
0
    UINT ustatus = 0;
509
0
    WLog_ERR(TAG, "Unexpected SmartCard IRP: MajorFunction %s, MinorFunction: 0x%08" PRIX32 "",
510
0
             rdpdr_irp_string(irp->MajorFunction), irp->MinorFunction);
511
0
    irp->IoStatus = STATUS_NOT_SUPPORTED;
512
513
0
    if ((ustatus = smartcard_complete_irp(smartcard, irp, handled)))
514
0
    {
515
0
      WLog_ERR(TAG, "Queue_Enqueue failed!");
516
0
      return ustatus;
517
0
    }
518
0
  }
519
520
0
  return CHANNEL_RC_OK;
521
0
}
522
523
static DWORD WINAPI smartcard_thread_func(LPVOID arg)
524
0
{
525
0
  IRP* irp = NULL;
526
0
  DWORD nCount = 0;
527
0
  DWORD status = 0;
528
0
  HANDLE hEvents[1] = { 0 };
529
0
  wMessage message = { 0 };
530
0
  UINT error = CHANNEL_RC_OK;
531
0
  SMARTCARD_DEVICE* smartcard = CAST_FROM_DEVICE(arg);
532
533
0
  if (!smartcard)
534
0
    return ERROR_INVALID_PARAMETER;
535
536
0
  hEvents[nCount++] = MessageQueue_Event(smartcard->IrpQueue);
537
538
0
  while (1)
539
0
  {
540
0
    status = WaitForMultipleObjects(nCount, hEvents, FALSE, INFINITE);
541
542
0
    if (status == WAIT_FAILED)
543
0
    {
544
0
      error = GetLastError();
545
0
      WLog_ERR(TAG, "WaitForMultipleObjects failed with error %" PRIu32 "!", error);
546
0
      break;
547
0
    }
548
549
0
    if (status == WAIT_OBJECT_0)
550
0
    {
551
0
      if (!MessageQueue_Peek(smartcard->IrpQueue, &message, TRUE))
552
0
      {
553
0
        WLog_ERR(TAG, "MessageQueue_Peek failed!");
554
0
        error = ERROR_INTERNAL_ERROR;
555
0
        break;
556
0
      }
557
558
0
      if (message.id == WMQ_QUIT)
559
0
        break;
560
561
0
      irp = (IRP*)message.wParam;
562
563
0
      if (irp)
564
0
      {
565
0
        BOOL handled = FALSE;
566
0
        if ((error = smartcard_process_irp(smartcard, irp, &handled)))
567
0
        {
568
0
          WLog_ERR(TAG, "smartcard_process_irp failed with error %" PRIu32 "!", error);
569
0
          goto out;
570
0
        }
571
0
        if (!handled)
572
0
        {
573
0
          WINPR_ASSERT(irp->Discard);
574
0
          irp->Discard(irp);
575
0
        }
576
0
      }
577
0
    }
578
0
  }
579
580
0
out:
581
582
0
  if (error && smartcard->rdpcontext)
583
0
    setChannelError(smartcard->rdpcontext, error, "smartcard_thread_func reported an error");
584
585
0
  ExitThread(error);
586
0
  return error;
587
0
}
588
589
/**
590
 * Function description
591
 *
592
 * @return 0 on success, otherwise a Win32 error code
593
 */
594
static UINT smartcard_irp_request(DEVICE* device, IRP* irp)
595
0
{
596
0
  SMARTCARD_DEVICE* smartcard = CAST_FROM_DEVICE(device);
597
598
0
  if (!smartcard)
599
0
    return ERROR_INVALID_PARAMETER;
600
601
0
  if (!MessageQueue_Post(smartcard->IrpQueue, NULL, 0, (void*)irp, NULL))
602
0
  {
603
0
    WLog_ERR(TAG, "MessageQueue_Post failed!");
604
0
    return ERROR_INTERNAL_ERROR;
605
0
  }
606
607
0
  return CHANNEL_RC_OK;
608
0
}
609
610
static void smartcard_free_irp(void* obj)
611
0
{
612
0
  wMessage* msg = obj;
613
0
  if (!msg)
614
0
    return;
615
0
  if (msg->id != 0)
616
0
    return;
617
618
0
  IRP* irp = (IRP*)msg->wParam;
619
0
  if (!irp)
620
0
    return;
621
0
  WINPR_ASSERT(irp->Discard);
622
0
  irp->Discard(irp);
623
0
}
624
625
/* smartcard is always built-in */
626
#define DeviceServiceEntry smartcard_DeviceServiceEntry
627
628
/**
629
 * Function description
630
 *
631
 * @return 0 on success, otherwise a Win32 error code
632
 */
633
FREERDP_ENTRY_POINT(UINT VCAPITYPE DeviceServiceEntry(PDEVICE_SERVICE_ENTRY_POINTS pEntryPoints))
634
0
{
635
0
  SMARTCARD_DEVICE* smartcard = NULL;
636
0
  size_t length = 0;
637
0
  UINT error = CHANNEL_RC_NO_MEMORY;
638
639
0
  if (!sSmartcard)
640
0
  {
641
0
    smartcard = (SMARTCARD_DEVICE*)calloc(1, sizeof(SMARTCARD_DEVICE));
642
643
0
    if (!smartcard)
644
0
    {
645
0
      WLog_ERR(TAG, "calloc failed!");
646
0
      return CHANNEL_RC_NO_MEMORY;
647
0
    }
648
649
0
    smartcard->device.type = RDPDR_DTYP_SMARTCARD;
650
0
    smartcard->device.name = "SCARD";
651
0
    smartcard->device.IRPRequest = smartcard_irp_request;
652
0
    smartcard->device.Init = smartcard_init;
653
0
    smartcard->device.Free = smartcard_free;
654
0
    smartcard->rdpcontext = pEntryPoints->rdpcontext;
655
0
    length = strlen(smartcard->device.name);
656
0
    smartcard->device.data = Stream_New(NULL, length + 1);
657
658
0
    if (!smartcard->device.data)
659
0
    {
660
0
      WLog_ERR(TAG, "Stream_New failed!");
661
0
      goto fail;
662
0
    }
663
664
0
    Stream_Write(smartcard->device.data, "SCARD", 6);
665
0
    smartcard->IrpQueue = MessageQueue_New(NULL);
666
667
0
    if (!smartcard->IrpQueue)
668
0
    {
669
0
      WLog_ERR(TAG, "MessageQueue_New failed!");
670
0
      goto fail;
671
0
    }
672
673
0
    wObject* obj = MessageQueue_Object(smartcard->IrpQueue);
674
0
    WINPR_ASSERT(obj);
675
0
    obj->fnObjectFree = smartcard_free_irp;
676
677
0
    smartcard->rgOutstandingMessages = ListDictionary_New(TRUE);
678
679
0
    if (!smartcard->rgOutstandingMessages)
680
0
    {
681
0
      WLog_ERR(TAG, "ListDictionary_New failed!");
682
0
      goto fail;
683
0
    }
684
685
0
    smartcard->callctx = smartcard_call_context_new(smartcard->rdpcontext->settings);
686
0
    if (!smartcard->callctx)
687
0
      goto fail;
688
689
0
    if (!smarcard_call_set_callbacks(smartcard->callctx, smartcard, smartcard_context_new,
690
0
                                     smartcard_context_free))
691
0
      goto fail;
692
693
0
    if ((error = pEntryPoints->RegisterDevice(pEntryPoints->devman, &smartcard->device)))
694
0
    {
695
0
      WLog_ERR(TAG, "RegisterDevice failed!");
696
0
      goto fail;
697
0
    }
698
699
0
    smartcard->thread =
700
0
        CreateThread(NULL, 0, smartcard_thread_func, smartcard, CREATE_SUSPENDED, NULL);
701
702
0
    if (!smartcard->thread)
703
0
    {
704
0
      WLog_ERR(TAG, "ListDictionary_New failed!");
705
0
      error = ERROR_INTERNAL_ERROR;
706
0
      goto fail;
707
0
    }
708
709
0
    ResumeThread(smartcard->thread);
710
0
  }
711
0
  else
712
0
    smartcard = sSmartcard;
713
714
0
  if (pEntryPoints->device->Name)
715
0
  {
716
0
    smartcard_call_context_add(smartcard->callctx, pEntryPoints->device->Name);
717
0
  }
718
719
0
  sSmartcard = smartcard;
720
0
  return CHANNEL_RC_OK;
721
0
fail:
722
0
  smartcard_free_(smartcard);
723
0
  return error;
724
0
}