25#include <freerdp/config.h>
28#include <winpr/smartcard.h>
29#include <winpr/environment.h>
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>
38#include "smartcard_main.h"
40#define CAST_FROM_DEVICE(device) cast_device_from(device, __func__, __FILE__, __LINE__)
46} scard_irp_queue_element;
48static void smartcard_context_free(
void* pCtx);
50static UINT smartcard_complete_irp(
SMARTCARD_DEVICE* smartcard, IRP* irp, BOOL* handled);
52static SMARTCARD_DEVICE* cast_device_from(DEVICE* device,
const char* fkt,
const char* file,
57 WLog_ERR(TAG,
"%s [%s:%" PRIuz
"] Called smartcard channel with nullptr device", fkt, file,
62 if (device->type != RDPDR_DTYP_SMARTCARD)
65 "%s [%s:%" PRIuz
"] Called smartcard channel with invalid device of type %" PRIx32,
66 fkt, file, line, device->type);
73static DWORD smartcard_context_queue_loop(
SMARTCARD_CONTEXT* pContext, wMessageQueue* queue)
77 HANDLE hEvents[2] = WINPR_C_ARRAY_INIT;
78 wMessage message = WINPR_C_ARRAY_INIT;
80 UINT error = CHANNEL_RC_OK;
81 smartcard = pContext->smartcard;
83 hEvents[nCount++] = MessageQueue_Event(queue);
87 waitStatus = WaitForMultipleObjects(nCount, hEvents, FALSE, INFINITE);
89 if (waitStatus == WAIT_FAILED)
91 error = GetLastError();
92 WLog_ERR(TAG,
"WaitForMultipleObjects failed with error %" PRIu32
"!", error);
96 waitStatus = WaitForSingleObject(MessageQueue_Event(queue), 0);
98 if (waitStatus == WAIT_FAILED)
100 error = GetLastError();
101 WLog_ERR(TAG,
"WaitForSingleObject failed with error %" PRIu32
"!", error);
105 if (waitStatus == WAIT_OBJECT_0)
107 scard_irp_queue_element* element =
nullptr;
109 if (!MessageQueue_Peek(queue, &message, TRUE))
111 WLog_ERR(TAG,
"MessageQueue_Peek failed!");
112 error = ERROR_INTERNAL_ERROR;
116 if (message.id == WMQ_QUIT)
119 element = (scard_irp_queue_element*)message.wParam;
123 BOOL handled = FALSE;
124 WINPR_ASSERT(smartcard);
127 smartcard_irp_device_control_call(smartcard->callctx, element->irp->output,
128 &element->irp->IoStatus, &element->operation);
131 element->irp->Discard(element->irp);
132 smartcard_operation_free(&element->operation, TRUE);
134 "smartcard_irp_device_control_call failed with error %s [%" PRId32
"]",
135 NtStatus2Tag(status), status);
136 error = (UINT)status;
140 error = smartcard_complete_irp(smartcard, element->irp, &handled);
142 element->irp->Discard(element->irp);
143 smartcard_operation_free(&element->operation, TRUE);
147 WLog_ERR(TAG,
"smartcard_complete_irp failed with %s [%" PRIu32
"]",
148 WTSErrorToString(error), error);
155 if (error && smartcard->rdpcontext)
156 setChannelError(smartcard->rdpcontext, error,
"smartcard_context_thread reported an error");
161static DWORD WINAPI smartcard_context_thread(LPVOID arg)
164 const DWORD error = smartcard_context_queue_loop(pContext, pContext->IrpQueue);
169static DWORD WINAPI smartcard_context_tx_thread(LPVOID arg)
172 const DWORD error = smartcard_context_queue_loop(pContext, pContext->TxQueue);
177static void smartcard_operation_queue_free(
void* obj)
185 scard_irp_queue_element* element = (scard_irp_queue_element*)msg->wParam;
188 WINPR_ASSERT(element->irp);
189 WINPR_ASSERT(element->irp->Discard);
190 element->irp->Discard(element->irp);
191 smartcard_operation_free(&element->operation, TRUE);
194static void* smartcard_context_new(
void* smartcard, SCARDCONTEXT hContext)
198 WLog_VRB(TAG,
"smartcard context create %p", (
const void*)pContext);
201 WLog_ERR(TAG,
"calloc failed!");
205 pContext->smartcard = smartcard;
206 pContext->hContext = hContext;
207 pContext->IrpQueue = MessageQueue_New(
nullptr);
209 if (!pContext->IrpQueue)
211 WLog_ERR(TAG,
"MessageQueue_New failed!");
216 wObject* obj = MessageQueue_Object(pContext->IrpQueue);
221 pContext->thread = CreateThread(
nullptr, 0, smartcard_context_thread, pContext, 0,
nullptr);
223 if (!pContext->thread)
225 WLog_ERR(TAG,
"CreateThread failed!");
229 pContext->TxQueue = MessageQueue_New(
nullptr);
231 if (!pContext->TxQueue)
233 WLog_ERR(TAG,
"MessageQueue_New failed!");
238 wObject* obj = MessageQueue_Object(pContext->TxQueue);
244 CreateThread(
nullptr, 0, smartcard_context_tx_thread, pContext, 0,
nullptr);
246 if (!pContext->txThread)
248 WLog_ERR(TAG,
"CreateThread failed!");
254 smartcard_context_free(pContext);
258void smartcard_context_free(
void* pCtx)
262 WLog_VRB(TAG,
"smartcard context destroy %p", pCtx);
267 WINPR_ASSERT(pContext->smartcard);
268 smartcard_call_cancel_context(pContext->smartcard->callctx, pContext->hContext);
270 if (pContext->IrpQueue)
272 if (MessageQueue_PostQuit(pContext->IrpQueue, 0))
274 if (WaitForSingleObject(pContext->thread, INFINITE) == WAIT_FAILED)
275 WLog_ERR(TAG,
"WaitForSingleObject failed with error %" PRIu32
"!", GetLastError());
277 (void)CloseHandle(pContext->thread);
279 MessageQueue_Free(pContext->IrpQueue);
281 if (pContext->TxQueue)
283 if (pContext->txThread && MessageQueue_PostQuit(pContext->TxQueue, 0))
285 if (WaitForSingleObject(pContext->txThread, INFINITE) == WAIT_FAILED)
286 WLog_ERR(TAG,
"WaitForSingleObject failed with error %" PRIu32
"!", GetLastError());
288 (void)CloseHandle(pContext->txThread);
290 MessageQueue_Free(pContext->TxQueue);
292 smartcard_call_release_context(pContext->smartcard->callctx, pContext->hContext);
303 WINPR_ASSERT(smartcard);
305 EnterCriticalSection(&smartcard->lock);
306 smartcard_call_cancel_all_context(smartcard->callctx);
307 LeaveCriticalSection(&smartcard->lock);
313 return CHANNEL_RC_OK;
315 if (smartcard->IrpQueue)
317 MessageQueue_Free(smartcard->IrpQueue);
318 (void)CloseHandle(smartcard->thread);
321 Stream_Free(smartcard->device.data, TRUE);
322 ListDictionary_Free(smartcard->rgOutstandingMessages);
324 smartcard_call_context_free(smartcard->callctx);
325 DeleteCriticalSection(&smartcard->lock);
328 return CHANNEL_RC_OK;
335static UINT smartcard_free(DEVICE* device)
339 WLog_VRB(TAG,
"smartcard device destroy: %p", (
const void*)smartcard);
341 return ERROR_INVALID_PARAMETER;
347 smartcard_cancel_all_context(smartcard);
351 if (smartcard->IrpQueue)
353 if (MessageQueue_PostQuit(smartcard->IrpQueue, 0) &&
354 (WaitForSingleObject(smartcard->thread, INFINITE) == WAIT_FAILED))
356 DWORD error = GetLastError();
357 WLog_ERR(TAG,
"WaitForSingleObject failed with error %" PRIu32
"!", error);
362 return smartcard_free_(smartcard);
375static UINT smartcard_init(DEVICE* device)
379 WLog_VRB(TAG,
"smartcard device init %p", (
const void*)smartcard);
381 return ERROR_INVALID_PARAMETER;
383 smartcard_cancel_all_context(smartcard);
384 return CHANNEL_RC_OK;
392UINT smartcard_complete_irp(
SMARTCARD_DEVICE* smartcard, IRP* irp, BOOL* handled)
394 WINPR_ASSERT(smartcard);
396 WINPR_ASSERT(handled);
398 uintptr_t key = (uintptr_t)irp->CompletionId + 1;
399 ListDictionary_Remove(smartcard->rgOutstandingMessages, (
void*)key);
401 WINPR_ASSERT(irp->Complete);
403 return irp->Complete(irp);
416static UINT smartcard_process_irp(
SMARTCARD_DEVICE* smartcard, IRP* irp, BOOL* handled)
419 BOOL asyncIrp = FALSE;
422 WINPR_ASSERT(smartcard);
423 WINPR_ASSERT(handled);
425 WINPR_ASSERT(irp->Complete);
427 uintptr_t key = (uintptr_t)irp->CompletionId + 1;
429 if (!ListDictionary_Add(smartcard->rgOutstandingMessages, (
void*)key, irp))
431 WLog_ERR(TAG,
"ListDictionary_Add failed!");
432 return ERROR_INTERNAL_ERROR;
435 if (irp->MajorFunction == IRP_MJ_DEVICE_CONTROL)
437 scard_irp_queue_element* element = calloc(1,
sizeof(scard_irp_queue_element));
439 return ERROR_OUTOFMEMORY;
442 element->operation.completionID = irp->CompletionId;
444 status = smartcard_irp_device_control_decode_request(irp->input, irp->CompletionId,
445 irp->FileId, &element->operation);
447 if (status != SCARD_S_SUCCESS)
451 smartcard_operation_free(&element->operation, TRUE);
452 irp->IoStatus = STATUS_UNSUCCESSFUL;
454 if ((error = smartcard_complete_irp(smartcard, irp, handled)))
456 WLog_ERR(TAG,
"Queue_Enqueue failed!");
460 return CHANNEL_RC_OK;
465 switch (element->operation.ioControlCode)
467 case SCARD_IOCTL_ESTABLISHCONTEXT:
468 case SCARD_IOCTL_RELEASECONTEXT:
469 case SCARD_IOCTL_ISVALIDCONTEXT:
470 case SCARD_IOCTL_CANCEL:
471 case SCARD_IOCTL_ACCESSSTARTEDEVENT:
472 case SCARD_IOCTL_RELEASETARTEDEVENT:
476 case SCARD_IOCTL_LISTREADERGROUPSA:
477 case SCARD_IOCTL_LISTREADERGROUPSW:
478 case SCARD_IOCTL_LISTREADERSA:
479 case SCARD_IOCTL_LISTREADERSW:
480 case SCARD_IOCTL_INTRODUCEREADERGROUPA:
481 case SCARD_IOCTL_INTRODUCEREADERGROUPW:
482 case SCARD_IOCTL_FORGETREADERGROUPA:
483 case SCARD_IOCTL_FORGETREADERGROUPW:
484 case SCARD_IOCTL_INTRODUCEREADERA:
485 case SCARD_IOCTL_INTRODUCEREADERW:
486 case SCARD_IOCTL_FORGETREADERA:
487 case SCARD_IOCTL_FORGETREADERW:
488 case SCARD_IOCTL_ADDREADERTOGROUPA:
489 case SCARD_IOCTL_ADDREADERTOGROUPW:
490 case SCARD_IOCTL_REMOVEREADERFROMGROUPA:
491 case SCARD_IOCTL_REMOVEREADERFROMGROUPW:
492 case SCARD_IOCTL_LOCATECARDSA:
493 case SCARD_IOCTL_LOCATECARDSW:
494 case SCARD_IOCTL_LOCATECARDSBYATRA:
495 case SCARD_IOCTL_LOCATECARDSBYATRW:
496 case SCARD_IOCTL_READCACHEA:
497 case SCARD_IOCTL_READCACHEW:
498 case SCARD_IOCTL_WRITECACHEA:
499 case SCARD_IOCTL_WRITECACHEW:
500 case SCARD_IOCTL_GETREADERICON:
501 case SCARD_IOCTL_GETDEVICETYPEID:
502 case SCARD_IOCTL_GETSTATUSCHANGEA:
503 case SCARD_IOCTL_GETSTATUSCHANGEW:
504 case SCARD_IOCTL_CONNECTA:
505 case SCARD_IOCTL_CONNECTW:
506 case SCARD_IOCTL_RECONNECT:
507 case SCARD_IOCTL_DISCONNECT:
508 case SCARD_IOCTL_BEGINTRANSACTION:
509 case SCARD_IOCTL_ENDTRANSACTION:
510 case SCARD_IOCTL_STATE:
511 case SCARD_IOCTL_STATUSA:
512 case SCARD_IOCTL_STATUSW:
513 case SCARD_IOCTL_TRANSMIT:
514 case SCARD_IOCTL_CONTROL:
515 case SCARD_IOCTL_GETATTRIB:
516 case SCARD_IOCTL_SETATTRIB:
517 case SCARD_IOCTL_GETTRANSMITCOUNT:
524 pContext = smartcard_call_get_context(smartcard->callctx, element->operation.hContext);
534 smartcard_irp_device_control_call(smartcard->callctx, element->irp->output,
535 &element->irp->IoStatus, &element->operation);
536 smartcard_operation_free(&element->operation, TRUE);
540 WLog_ERR(TAG,
"smartcard_irp_device_control_call failed with error %" PRId32
"!",
542 return (UINT32)status;
545 if ((error = smartcard_complete_irp(smartcard, irp, handled)))
547 WLog_ERR(TAG,
"Queue_Enqueue failed!");
555 wMessageQueue* queue = pContext->IrpQueue;
557 switch (element->operation.ioControlCode)
560 case SCARD_IOCTL_BEGINTRANSACTION:
561 case SCARD_IOCTL_CONNECTA:
562 case SCARD_IOCTL_CONNECTW:
563 case SCARD_IOCTL_RECONNECT:
564 queue = pContext->TxQueue;
570 if (!MessageQueue_Post(queue,
nullptr, 0, (
void*)element,
nullptr))
572 smartcard_operation_free(&element->operation, TRUE);
573 WLog_ERR(TAG,
"MessageQueue_Post failed!");
574 return ERROR_INTERNAL_ERROR;
583 WLog_ERR(TAG,
"Unexpected SmartCard IRP: MajorFunction %s, MinorFunction: 0x%08" PRIX32
"",
584 rdpdr_irp_string(irp->MajorFunction), irp->MinorFunction);
585 irp->IoStatus = STATUS_NOT_SUPPORTED;
587 if ((ustatus = smartcard_complete_irp(smartcard, irp, handled)))
589 WLog_ERR(TAG,
"Queue_Enqueue failed!");
594 return CHANNEL_RC_OK;
597static DWORD WINAPI smartcard_thread_func(LPVOID arg)
602 HANDLE hEvents[1] = WINPR_C_ARRAY_INIT;
603 wMessage message = WINPR_C_ARRAY_INIT;
604 UINT error = CHANNEL_RC_OK;
608 return ERROR_INVALID_PARAMETER;
610 hEvents[nCount++] = MessageQueue_Event(smartcard->IrpQueue);
614 status = WaitForMultipleObjects(nCount, hEvents, FALSE, INFINITE);
616 if (status == WAIT_FAILED)
618 error = GetLastError();
619 WLog_ERR(TAG,
"WaitForMultipleObjects failed with error %" PRIu32
"!", error);
623 if (status == WAIT_OBJECT_0)
625 if (!MessageQueue_Peek(smartcard->IrpQueue, &message, TRUE))
627 WLog_ERR(TAG,
"MessageQueue_Peek failed!");
628 error = ERROR_INTERNAL_ERROR;
632 if (message.id == WMQ_QUIT)
635 irp = (IRP*)message.wParam;
639 BOOL handled = FALSE;
642 EnterCriticalSection(&smartcard->lock);
643 error = smartcard_process_irp(smartcard, irp, &handled);
644 LeaveCriticalSection(&smartcard->lock);
648 WLog_ERR(TAG,
"smartcard_process_irp failed with error %" PRIu32
"!", error);
653 WINPR_ASSERT(irp->Discard);
662 if (error && smartcard->rdpcontext)
663 setChannelError(smartcard->rdpcontext, error,
"smartcard_thread_func reported an error");
674static UINT smartcard_irp_request(DEVICE* device, IRP* irp)
681 return ERROR_INVALID_PARAMETER;
684 if (!MessageQueue_Post(smartcard->IrpQueue,
nullptr, 0, (
void*)irp,
nullptr))
686 WLog_ERR(TAG,
"MessageQueue_Post failed!");
688 return ERROR_INTERNAL_ERROR;
691 return CHANNEL_RC_OK;
694static void smartcard_free_irp(
void* obj)
702 IRP* irp = (IRP*)msg->wParam;
705 WINPR_ASSERT(irp->Discard);
710#define DeviceServiceEntry smartcard_DeviceServiceEntry
719 BOOL registered = FALSE;
720 UINT error = CHANNEL_RC_NO_MEMORY;
723 WLog_VRB(TAG,
"smartcard device create: %p", (
const void*)smartcard);
726 WLog_ERR(TAG,
"calloc failed!");
727 return CHANNEL_RC_NO_MEMORY;
730 smartcard->device.type = RDPDR_DTYP_SMARTCARD;
731 smartcard->device.name =
"SCARD";
732 smartcard->device.IRPRequest = smartcard_irp_request;
733 smartcard->device.Init = smartcard_init;
734 smartcard->device.Free = smartcard_free;
735 smartcard->rdpcontext = pEntryPoints->rdpcontext;
737 if (!InitializeCriticalSectionEx(&smartcard->lock, 0, 0))
739 WLog_ERR(TAG,
"InitializeCriticalSectionEx failed!");
743 smartcard->IrpQueue = MessageQueue_New(
nullptr);
745 if (!smartcard->IrpQueue)
747 WLog_ERR(TAG,
"MessageQueue_New failed!");
751 wObject* obj = MessageQueue_Object(smartcard->IrpQueue);
755 smartcard->rgOutstandingMessages = ListDictionary_New(TRUE);
757 if (!smartcard->rgOutstandingMessages)
759 WLog_ERR(TAG,
"ListDictionary_New failed!");
763 smartcard->callctx = smartcard_call_context_new_with_context(smartcard->rdpcontext);
764 if (!smartcard->callctx)
767 if (!smarcard_call_set_callbacks(smartcard->callctx, smartcard, smartcard_context_new,
768 smartcard_context_free))
771 error = pEntryPoints->RegisterDevice(pEntryPoints->devman, &smartcard->device);
774 WLog_ERR(TAG,
"RegisterDevice failed!");
780 CreateThread(
nullptr, 0, smartcard_thread_func, smartcard, CREATE_SUSPENDED,
nullptr);
782 if (!smartcard->thread)
784 WLog_ERR(TAG,
"CreateThread failed!");
785 error = ERROR_INTERNAL_ERROR;
789 ResumeThread(smartcard->thread);
791 if (pEntryPoints->device->Name)
793 if (!smartcard_call_context_add(smartcard->callctx, pEntryPoints->device->Name))
797 return CHANNEL_RC_OK;
800 smartcard_free_(smartcard);
This struct contains function pointer to initialize/free objects.
OBJECT_FREE_FN fnObjectFree