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;
50static void smartcard_context_free(
void* pCtx);
52static UINT smartcard_complete_irp(
SMARTCARD_DEVICE* smartcard, IRP* irp, BOOL* handled);
54static SMARTCARD_DEVICE* cast_device_from(DEVICE* device,
const char* fkt,
const char* file,
59 WLog_ERR(TAG,
"%s [%s:%" PRIuz
"] Called smartcard channel with NULL device", fkt, file,
64 if (device->type != RDPDR_DTYP_SMARTCARD)
67 "%s [%s:%" PRIuz
"] Called smartcard channel with invalid device of type %" PRIx32,
68 fkt, file, line, device->type);
75static DWORD WINAPI smartcard_context_thread(LPVOID arg)
81 HANDLE hEvents[2] = { 0 };
82 wMessage message = { 0 };
84 UINT error = CHANNEL_RC_OK;
85 smartcard = pContext->smartcard;
87 hEvents[nCount++] = MessageQueue_Event(pContext->IrpQueue);
91 waitStatus = WaitForMultipleObjects(nCount, hEvents, FALSE, INFINITE);
93 if (waitStatus == WAIT_FAILED)
95 error = GetLastError();
96 WLog_ERR(TAG,
"WaitForMultipleObjects failed with error %" PRIu32
"!", error);
100 waitStatus = WaitForSingleObject(MessageQueue_Event(pContext->IrpQueue), 0);
102 if (waitStatus == WAIT_FAILED)
104 error = GetLastError();
105 WLog_ERR(TAG,
"WaitForSingleObject failed with error %" PRIu32
"!", error);
109 if (waitStatus == WAIT_OBJECT_0)
111 scard_irp_queue_element* element = NULL;
113 if (!MessageQueue_Peek(pContext->IrpQueue, &message, TRUE))
115 WLog_ERR(TAG,
"MessageQueue_Peek failed!");
116 status = ERROR_INTERNAL_ERROR;
120 if (message.id == WMQ_QUIT)
123 element = (scard_irp_queue_element*)message.wParam;
127 BOOL handled = FALSE;
128 WINPR_ASSERT(smartcard);
130 if ((status = smartcard_irp_device_control_call(
131 smartcard->callctx, element->irp->output, &element->irp->IoStatus,
132 &element->operation)))
134 element->irp->Discard(element->irp);
135 smartcard_operation_free(&element->operation, TRUE);
136 WLog_ERR(TAG,
"smartcard_irp_device_control_call failed with error %" PRIu32
"",
141 error = smartcard_complete_irp(smartcard, element->irp, &handled);
143 element->irp->Discard(element->irp);
144 smartcard_operation_free(&element->operation, TRUE);
148 WLog_ERR(TAG,
"Queue_Enqueue failed!");
155 if (status && smartcard->rdpcontext)
156 setChannelError(smartcard->rdpcontext, error,
"smartcard_context_thread reported an error");
158 ExitThread((uint32_t)status);
162static void smartcard_operation_queue_free(
void* obj)
170 scard_irp_queue_element* element = (scard_irp_queue_element*)msg->wParam;
173 WINPR_ASSERT(element->irp);
174 WINPR_ASSERT(element->irp->Discard);
175 element->irp->Discard(element->irp);
176 smartcard_operation_free(&element->operation, TRUE);
179static void* smartcard_context_new(
void* smartcard, SCARDCONTEXT hContext)
186 WLog_ERR(TAG,
"calloc failed!");
190 pContext->smartcard = smartcard;
191 pContext->hContext = hContext;
192 pContext->IrpQueue = MessageQueue_New(NULL);
194 if (!pContext->IrpQueue)
196 WLog_ERR(TAG,
"MessageQueue_New failed!");
201 wObject* obj = MessageQueue_Object(pContext->IrpQueue);
203 obj->fnObjectFree = smartcard_operation_queue_free;
206 pContext->thread = CreateThread(NULL, 0, smartcard_context_thread, pContext, 0, NULL);
208 if (!pContext->thread)
210 WLog_ERR(TAG,
"CreateThread failed!");
216 smartcard_context_free(pContext);
220void smartcard_context_free(
void* pCtx)
228 WINPR_ASSERT(pContext->smartcard);
229 smartcard_call_cancel_context(pContext->smartcard->callctx, pContext->hContext);
231 if (pContext->IrpQueue)
233 if (MessageQueue_PostQuit(pContext->IrpQueue, 0))
235 if (WaitForSingleObject(pContext->thread, INFINITE) == WAIT_FAILED)
236 WLog_ERR(TAG,
"WaitForSingleObject failed with error %" PRIu32
"!", GetLastError());
238 (void)CloseHandle(pContext->thread);
240 MessageQueue_Free(pContext->IrpQueue);
242 smartcard_call_release_context(pContext->smartcard->callctx, pContext->hContext);
248 smartcard_call_cancel_all_context(smartcard->callctx);
254 return CHANNEL_RC_OK;
256 if (smartcard->IrpQueue)
258 MessageQueue_Free(smartcard->IrpQueue);
259 (void)CloseHandle(smartcard->thread);
262 Stream_Free(smartcard->device.data, TRUE);
263 ListDictionary_Free(smartcard->rgOutstandingMessages);
265 smartcard_call_context_free(smartcard->callctx);
268 return CHANNEL_RC_OK;
275static UINT smartcard_free(DEVICE* device)
280 return ERROR_INVALID_PARAMETER;
286 smartcard_release_all_contexts(smartcard);
290 if (smartcard->IrpQueue)
292 if (MessageQueue_PostQuit(smartcard->IrpQueue, 0) &&
293 (WaitForSingleObject(smartcard->thread, INFINITE) == WAIT_FAILED))
295 DWORD error = GetLastError();
296 WLog_ERR(TAG,
"WaitForSingleObject failed with error %" PRIu32
"!", error);
301 if (sSmartcard == smartcard)
304 return smartcard_free_(smartcard);
317static UINT smartcard_init(DEVICE* device)
322 return ERROR_INVALID_PARAMETER;
324 smartcard_release_all_contexts(smartcard);
325 return CHANNEL_RC_OK;
333UINT smartcard_complete_irp(
SMARTCARD_DEVICE* smartcard, IRP* irp, BOOL* handled)
335 WINPR_ASSERT(smartcard);
337 WINPR_ASSERT(handled);
339 uintptr_t key = (uintptr_t)irp->CompletionId + 1;
340 ListDictionary_Remove(smartcard->rgOutstandingMessages, (
void*)key);
342 WINPR_ASSERT(irp->Complete);
344 return irp->Complete(irp);
357static UINT smartcard_process_irp(
SMARTCARD_DEVICE* smartcard, IRP* irp, BOOL* handled)
360 BOOL asyncIrp = FALSE;
363 WINPR_ASSERT(smartcard);
364 WINPR_ASSERT(handled);
366 WINPR_ASSERT(irp->Complete);
368 uintptr_t key = (uintptr_t)irp->CompletionId + 1;
370 if (!ListDictionary_Add(smartcard->rgOutstandingMessages, (
void*)key, irp))
372 WLog_ERR(TAG,
"ListDictionary_Add failed!");
373 return ERROR_INTERNAL_ERROR;
376 if (irp->MajorFunction == IRP_MJ_DEVICE_CONTROL)
378 scard_irp_queue_element* element = calloc(1,
sizeof(scard_irp_queue_element));
380 return ERROR_OUTOFMEMORY;
383 element->operation.completionID = irp->CompletionId;
385 status = smartcard_irp_device_control_decode(irp->input, irp->CompletionId, irp->FileId,
386 &element->operation);
388 if (status != SCARD_S_SUCCESS)
392 smartcard_operation_free(&element->operation, TRUE);
393 irp->IoStatus = STATUS_UNSUCCESSFUL;
395 if ((error = smartcard_complete_irp(smartcard, irp, handled)))
397 WLog_ERR(TAG,
"Queue_Enqueue failed!");
401 return CHANNEL_RC_OK;
406 switch (element->operation.ioControlCode)
408 case SCARD_IOCTL_ESTABLISHCONTEXT:
409 case SCARD_IOCTL_RELEASECONTEXT:
410 case SCARD_IOCTL_ISVALIDCONTEXT:
411 case SCARD_IOCTL_CANCEL:
412 case SCARD_IOCTL_ACCESSSTARTEDEVENT:
413 case SCARD_IOCTL_RELEASETARTEDEVENT:
417 case SCARD_IOCTL_LISTREADERGROUPSA:
418 case SCARD_IOCTL_LISTREADERGROUPSW:
419 case SCARD_IOCTL_LISTREADERSA:
420 case SCARD_IOCTL_LISTREADERSW:
421 case SCARD_IOCTL_INTRODUCEREADERGROUPA:
422 case SCARD_IOCTL_INTRODUCEREADERGROUPW:
423 case SCARD_IOCTL_FORGETREADERGROUPA:
424 case SCARD_IOCTL_FORGETREADERGROUPW:
425 case SCARD_IOCTL_INTRODUCEREADERA:
426 case SCARD_IOCTL_INTRODUCEREADERW:
427 case SCARD_IOCTL_FORGETREADERA:
428 case SCARD_IOCTL_FORGETREADERW:
429 case SCARD_IOCTL_ADDREADERTOGROUPA:
430 case SCARD_IOCTL_ADDREADERTOGROUPW:
431 case SCARD_IOCTL_REMOVEREADERFROMGROUPA:
432 case SCARD_IOCTL_REMOVEREADERFROMGROUPW:
433 case SCARD_IOCTL_LOCATECARDSA:
434 case SCARD_IOCTL_LOCATECARDSW:
435 case SCARD_IOCTL_LOCATECARDSBYATRA:
436 case SCARD_IOCTL_LOCATECARDSBYATRW:
437 case SCARD_IOCTL_READCACHEA:
438 case SCARD_IOCTL_READCACHEW:
439 case SCARD_IOCTL_WRITECACHEA:
440 case SCARD_IOCTL_WRITECACHEW:
441 case SCARD_IOCTL_GETREADERICON:
442 case SCARD_IOCTL_GETDEVICETYPEID:
443 case SCARD_IOCTL_GETSTATUSCHANGEA:
444 case SCARD_IOCTL_GETSTATUSCHANGEW:
445 case SCARD_IOCTL_CONNECTA:
446 case SCARD_IOCTL_CONNECTW:
447 case SCARD_IOCTL_RECONNECT:
448 case SCARD_IOCTL_DISCONNECT:
449 case SCARD_IOCTL_BEGINTRANSACTION:
450 case SCARD_IOCTL_ENDTRANSACTION:
451 case SCARD_IOCTL_STATE:
452 case SCARD_IOCTL_STATUSA:
453 case SCARD_IOCTL_STATUSW:
454 case SCARD_IOCTL_TRANSMIT:
455 case SCARD_IOCTL_CONTROL:
456 case SCARD_IOCTL_GETATTRIB:
457 case SCARD_IOCTL_SETATTRIB:
458 case SCARD_IOCTL_GETTRANSMITCOUNT:
465 pContext = smartcard_call_get_context(smartcard->callctx, element->operation.hContext);
475 smartcard_irp_device_control_call(smartcard->callctx, element->irp->output,
476 &element->irp->IoStatus, &element->operation);
477 smartcard_operation_free(&element->operation, TRUE);
481 WLog_ERR(TAG,
"smartcard_irp_device_control_call failed with error %" PRId32
"!",
483 return (UINT32)status;
486 if ((error = smartcard_complete_irp(smartcard, irp, handled)))
488 WLog_ERR(TAG,
"Queue_Enqueue failed!");
496 if (!MessageQueue_Post(pContext->IrpQueue, NULL, 0, (
void*)element, NULL))
498 smartcard_operation_free(&element->operation, TRUE);
499 WLog_ERR(TAG,
"MessageQueue_Post failed!");
500 return ERROR_INTERNAL_ERROR;
509 WLog_ERR(TAG,
"Unexpected SmartCard IRP: MajorFunction %s, MinorFunction: 0x%08" PRIX32
"",
510 rdpdr_irp_string(irp->MajorFunction), irp->MinorFunction);
511 irp->IoStatus = STATUS_NOT_SUPPORTED;
513 if ((ustatus = smartcard_complete_irp(smartcard, irp, handled)))
515 WLog_ERR(TAG,
"Queue_Enqueue failed!");
520 return CHANNEL_RC_OK;
523static DWORD WINAPI smartcard_thread_func(LPVOID arg)
528 HANDLE hEvents[1] = { 0 };
529 wMessage message = { 0 };
530 UINT error = CHANNEL_RC_OK;
534 return ERROR_INVALID_PARAMETER;
536 hEvents[nCount++] = MessageQueue_Event(smartcard->IrpQueue);
540 status = WaitForMultipleObjects(nCount, hEvents, FALSE, INFINITE);
542 if (status == WAIT_FAILED)
544 error = GetLastError();
545 WLog_ERR(TAG,
"WaitForMultipleObjects failed with error %" PRIu32
"!", error);
549 if (status == WAIT_OBJECT_0)
551 if (!MessageQueue_Peek(smartcard->IrpQueue, &message, TRUE))
553 WLog_ERR(TAG,
"MessageQueue_Peek failed!");
554 error = ERROR_INTERNAL_ERROR;
558 if (message.id == WMQ_QUIT)
561 irp = (IRP*)message.wParam;
565 BOOL handled = FALSE;
566 if ((error = smartcard_process_irp(smartcard, irp, &handled)))
568 WLog_ERR(TAG,
"smartcard_process_irp failed with error %" PRIu32
"!", error);
573 WINPR_ASSERT(irp->Discard);
582 if (error && smartcard->rdpcontext)
583 setChannelError(smartcard->rdpcontext, error,
"smartcard_thread_func reported an error");
594static UINT smartcard_irp_request(DEVICE* device, IRP* irp)
599 return ERROR_INVALID_PARAMETER;
601 if (!MessageQueue_Post(smartcard->IrpQueue, NULL, 0, (
void*)irp, NULL))
603 WLog_ERR(TAG,
"MessageQueue_Post failed!");
604 return ERROR_INTERNAL_ERROR;
607 return CHANNEL_RC_OK;
610static void smartcard_free_irp(
void* obj)
618 IRP* irp = (IRP*)msg->wParam;
621 WINPR_ASSERT(irp->Discard);
626#define DeviceServiceEntry smartcard_DeviceServiceEntry
637 UINT error = CHANNEL_RC_NO_MEMORY;
645 WLog_ERR(TAG,
"calloc failed!");
646 return CHANNEL_RC_NO_MEMORY;
649 smartcard->device.type = RDPDR_DTYP_SMARTCARD;
650 smartcard->device.name =
"SCARD";
651 smartcard->device.IRPRequest = smartcard_irp_request;
652 smartcard->device.Init = smartcard_init;
653 smartcard->device.Free = smartcard_free;
654 smartcard->rdpcontext = pEntryPoints->rdpcontext;
655 length = strlen(smartcard->device.name);
656 smartcard->device.data = Stream_New(NULL, length + 1);
658 if (!smartcard->device.data)
660 WLog_ERR(TAG,
"Stream_New failed!");
664 Stream_Write(smartcard->device.data,
"SCARD", 6);
665 smartcard->IrpQueue = MessageQueue_New(NULL);
667 if (!smartcard->IrpQueue)
669 WLog_ERR(TAG,
"MessageQueue_New failed!");
673 wObject* obj = MessageQueue_Object(smartcard->IrpQueue);
675 obj->fnObjectFree = smartcard_free_irp;
677 smartcard->rgOutstandingMessages = ListDictionary_New(TRUE);
679 if (!smartcard->rgOutstandingMessages)
681 WLog_ERR(TAG,
"ListDictionary_New failed!");
685 smartcard->callctx = smartcard_call_context_new(smartcard->rdpcontext->settings);
686 if (!smartcard->callctx)
689 if (!smarcard_call_set_callbacks(smartcard->callctx, smartcard, smartcard_context_new,
690 smartcard_context_free))
693 if ((error = pEntryPoints->RegisterDevice(pEntryPoints->devman, &smartcard->device)))
695 WLog_ERR(TAG,
"RegisterDevice failed!");
700 CreateThread(NULL, 0, smartcard_thread_func, smartcard, CREATE_SUSPENDED, NULL);
702 if (!smartcard->thread)
704 WLog_ERR(TAG,
"ListDictionary_New failed!");
705 error = ERROR_INTERNAL_ERROR;
709 ResumeThread(smartcard->thread);
712 smartcard = sSmartcard;
714 if (pEntryPoints->device->Name)
716 smartcard_call_context_add(smartcard->callctx, pEntryPoints->device->Name);
719 sSmartcard = smartcard;
720 return CHANNEL_RC_OK;
722 smartcard_free_(smartcard);
This struct contains function pointer to initialize/free objects.