2012-08-15 01:09:01 +04:00
|
|
|
/**
|
2012-10-09 07:02:04 +04:00
|
|
|
* FreeRDP: A Remote Desktop Protocol Implementation
|
2012-08-15 01:09:01 +04:00
|
|
|
* Smartcard Device Service Virtual Channel
|
|
|
|
*
|
|
|
|
* Copyright 2011 O.S. Systems Software Ltda.
|
|
|
|
* Copyright 2011 Eduardo Fiss Beloni <beloni@ossystems.com.br>
|
|
|
|
* Copyright 2011 Anthony Tong <atong@trustedcs.com>
|
|
|
|
*
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifdef HAVE_CONFIG_H
|
2011-10-15 19:30:10 +04:00
|
|
|
#include "config.h"
|
2012-08-15 01:09:01 +04:00
|
|
|
#endif
|
2011-10-15 19:30:10 +04:00
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
|
2012-11-21 20:56:40 +04:00
|
|
|
#include <winpr/crt.h>
|
2014-04-03 23:27:55 +04:00
|
|
|
#include <winpr/smartcard.h>
|
2012-11-21 20:56:40 +04:00
|
|
|
|
2012-10-09 05:00:07 +04:00
|
|
|
#include <freerdp/channels/rdpdr.h>
|
2011-10-15 19:30:10 +04:00
|
|
|
|
2012-11-21 20:56:40 +04:00
|
|
|
#include "smartcard_main.h"
|
2011-10-15 19:30:10 +04:00
|
|
|
|
2014-04-06 01:51:13 +04:00
|
|
|
#define SMARTCARD_ASYNC_IRP 1
|
|
|
|
|
2013-11-04 05:46:40 +04:00
|
|
|
static void smartcard_free(DEVICE* device)
|
2011-10-15 19:30:10 +04:00
|
|
|
{
|
2013-11-04 05:46:40 +04:00
|
|
|
SMARTCARD_DEVICE* smartcard = (SMARTCARD_DEVICE*) device;
|
2011-10-15 19:30:10 +04:00
|
|
|
|
2013-11-04 05:46:40 +04:00
|
|
|
MessageQueue_PostQuit(smartcard->IrpQueue, 0);
|
|
|
|
WaitForSingleObject(smartcard->thread, INFINITE);
|
2013-08-13 18:29:19 +04:00
|
|
|
|
|
|
|
CloseHandle(smartcard->thread);
|
2012-09-23 21:54:14 +04:00
|
|
|
|
2013-08-13 18:29:19 +04:00
|
|
|
Stream_Free(smartcard->device.data, TRUE);
|
2012-08-02 02:05:45 +04:00
|
|
|
|
2014-04-06 00:57:31 +04:00
|
|
|
MessageQueue_Free(smartcard->IrpQueue);
|
|
|
|
ListDictionary_Free(smartcard->OutstandingIrps);
|
|
|
|
|
2013-11-04 05:46:40 +04:00
|
|
|
free(device);
|
2011-10-15 19:30:10 +04:00
|
|
|
}
|
|
|
|
|
2014-04-06 00:57:31 +04:00
|
|
|
/**
|
|
|
|
* Initialization occurs when the protocol server sends a device announce message.
|
2014-04-06 01:15:17 +04:00
|
|
|
* At that time, we need to cancel all outstanding IRPs.
|
2014-04-06 00:57:31 +04:00
|
|
|
*/
|
|
|
|
|
|
|
|
static void smartcard_init(DEVICE* device)
|
|
|
|
{
|
2014-04-06 01:15:17 +04:00
|
|
|
IRP* irp;
|
|
|
|
int index;
|
|
|
|
int keyCount;
|
|
|
|
ULONG_PTR* pKeys;
|
2014-04-06 00:57:31 +04:00
|
|
|
SMARTCARD_DEVICE* smartcard = (SMARTCARD_DEVICE*) device;
|
|
|
|
|
|
|
|
if (ListDictionary_Count(smartcard->OutstandingIrps) > 0)
|
|
|
|
{
|
2014-04-06 01:15:17 +04:00
|
|
|
pKeys = NULL;
|
|
|
|
keyCount = ListDictionary_GetKeys(smartcard->OutstandingIrps, &pKeys);
|
2014-04-06 00:57:31 +04:00
|
|
|
|
2014-04-06 01:15:17 +04:00
|
|
|
for (index = 0; index < keyCount; index++)
|
|
|
|
{
|
|
|
|
irp = (IRP*) ListDictionary_GetItemValue(smartcard->OutstandingIrps, (void*) pKeys[index]);
|
2014-04-06 00:57:31 +04:00
|
|
|
|
2014-04-06 01:15:17 +04:00
|
|
|
if (irp)
|
|
|
|
irp->cancelled = TRUE;
|
|
|
|
}
|
2014-04-06 00:57:31 +04:00
|
|
|
|
2014-04-06 01:15:17 +04:00
|
|
|
free(pKeys);
|
|
|
|
}
|
2014-04-06 00:57:31 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
void smartcard_complete_irp(SMARTCARD_DEVICE* smartcard, IRP* irp)
|
|
|
|
{
|
|
|
|
void* key;
|
|
|
|
|
|
|
|
key = (void*) (size_t) irp->CompletionId;
|
|
|
|
ListDictionary_Remove(smartcard->OutstandingIrps, key);
|
|
|
|
|
2014-04-06 01:15:17 +04:00
|
|
|
if (!irp->cancelled)
|
|
|
|
irp->Complete(irp);
|
|
|
|
else
|
|
|
|
irp->Discard(irp);
|
2014-04-06 00:57:31 +04:00
|
|
|
}
|
|
|
|
|
2014-04-06 01:51:13 +04:00
|
|
|
void* smartcard_process_irp_worker_proc(IRP* irp)
|
2011-10-15 19:30:10 +04:00
|
|
|
{
|
2014-04-06 01:51:13 +04:00
|
|
|
SMARTCARD_DEVICE* smartcard;
|
2014-04-06 00:57:31 +04:00
|
|
|
|
2014-04-06 01:51:13 +04:00
|
|
|
smartcard = (SMARTCARD_DEVICE*) irp->device;
|
2014-04-06 00:57:31 +04:00
|
|
|
|
2014-04-06 01:51:13 +04:00
|
|
|
smartcard_irp_device_control(smartcard, irp);
|
2011-10-15 19:30:10 +04:00
|
|
|
|
2014-04-06 01:51:13 +04:00
|
|
|
ExitThread(0);
|
|
|
|
return NULL;
|
2011-10-15 19:30:10 +04:00
|
|
|
}
|
|
|
|
|
2014-04-06 00:06:46 +04:00
|
|
|
/**
|
|
|
|
* Multiple threads and SCardGetStatusChange:
|
|
|
|
* http://musclecard.996296.n3.nabble.com/Multiple-threads-and-SCardGetStatusChange-td4430.html
|
|
|
|
*/
|
|
|
|
|
2014-04-06 01:51:13 +04:00
|
|
|
void smartcard_process_irp(SMARTCARD_DEVICE* smartcard, IRP* irp)
|
|
|
|
{
|
|
|
|
void* key;
|
|
|
|
BOOL asyncIrp = FALSE;
|
|
|
|
UINT32 ioControlCode = 0;
|
|
|
|
|
|
|
|
key = (void*) (size_t) irp->CompletionId;
|
|
|
|
ListDictionary_Add(smartcard->OutstandingIrps, key, irp);
|
|
|
|
|
|
|
|
if (irp->MajorFunction == IRP_MJ_DEVICE_CONTROL)
|
|
|
|
{
|
|
|
|
smartcard_irp_device_control_peek_io_control_code(smartcard, irp, &ioControlCode);
|
|
|
|
|
|
|
|
if (!ioControlCode)
|
|
|
|
return;
|
|
|
|
|
2014-04-07 00:29:11 +04:00
|
|
|
#ifdef SMARTCARD_ASYNC_IRP
|
|
|
|
asyncIrp = TRUE;
|
|
|
|
|
2014-04-06 01:51:13 +04:00
|
|
|
switch (ioControlCode)
|
|
|
|
{
|
2014-04-07 00:29:11 +04:00
|
|
|
case SCARD_IOCTL_ESTABLISHCONTEXT:
|
|
|
|
case SCARD_IOCTL_RELEASECONTEXT:
|
|
|
|
case SCARD_IOCTL_ISVALIDCONTEXT:
|
|
|
|
case SCARD_IOCTL_ACCESSSTARTEDEVENT:
|
|
|
|
case SCARD_IOCTL_RELEASESTARTEDEVENT:
|
|
|
|
asyncIrp = FALSE;
|
|
|
|
break;
|
|
|
|
|
2014-04-06 01:51:13 +04:00
|
|
|
case SCARD_IOCTL_TRANSMIT:
|
|
|
|
case SCARD_IOCTL_STATUSA:
|
|
|
|
case SCARD_IOCTL_STATUSW:
|
|
|
|
case SCARD_IOCTL_GETSTATUSCHANGEA:
|
|
|
|
case SCARD_IOCTL_GETSTATUSCHANGEW:
|
|
|
|
asyncIrp = TRUE;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
if (!asyncIrp)
|
|
|
|
{
|
|
|
|
smartcard_irp_device_control(smartcard, irp);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
irp->thread = CreateThread(NULL, 0,
|
|
|
|
(LPTHREAD_START_ROUTINE) smartcard_process_irp_worker_proc,
|
|
|
|
irp, 0, NULL);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
fprintf(stderr, "Unexpected SmartCard IRP: MajorFunction 0x%08X MinorFunction: 0x%08X",
|
|
|
|
irp->MajorFunction, irp->MinorFunction);
|
|
|
|
irp->IoStatus = STATUS_NOT_SUPPORTED;
|
|
|
|
smartcard_complete_irp(smartcard, irp);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-11-04 05:46:40 +04:00
|
|
|
static void* smartcard_thread_func(void* arg)
|
2011-10-15 19:30:10 +04:00
|
|
|
{
|
2012-09-23 21:54:14 +04:00
|
|
|
IRP* irp;
|
2013-11-04 05:46:40 +04:00
|
|
|
wMessage message;
|
|
|
|
SMARTCARD_DEVICE* smartcard = (SMARTCARD_DEVICE*) arg;
|
2011-10-15 19:30:10 +04:00
|
|
|
|
2012-11-21 21:53:54 +04:00
|
|
|
while (1)
|
2011-10-15 19:30:10 +04:00
|
|
|
{
|
2013-11-04 05:46:40 +04:00
|
|
|
if (!MessageQueue_Wait(smartcard->IrpQueue))
|
2012-11-21 21:53:54 +04:00
|
|
|
break;
|
|
|
|
|
2013-11-04 05:46:40 +04:00
|
|
|
if (!MessageQueue_Peek(smartcard->IrpQueue, &message, TRUE))
|
2011-10-15 19:30:10 +04:00
|
|
|
break;
|
|
|
|
|
2013-11-04 05:46:40 +04:00
|
|
|
if (message.id == WMQ_QUIT)
|
2011-10-15 19:30:10 +04:00
|
|
|
break;
|
2012-11-21 20:56:40 +04:00
|
|
|
|
2013-11-04 05:46:40 +04:00
|
|
|
irp = (IRP*) message.wParam;
|
2012-08-02 02:05:45 +04:00
|
|
|
|
2013-11-04 05:46:40 +04:00
|
|
|
if (irp)
|
|
|
|
smartcard_process_irp(smartcard, irp);
|
2012-08-02 02:05:45 +04:00
|
|
|
}
|
2012-09-23 21:54:14 +04:00
|
|
|
|
2013-11-04 05:46:40 +04:00
|
|
|
ExitThread(0);
|
|
|
|
return NULL;
|
2012-08-02 02:05:45 +04:00
|
|
|
}
|
|
|
|
|
2012-11-21 20:56:40 +04:00
|
|
|
static void smartcard_irp_request(DEVICE* device, IRP* irp)
|
2011-10-15 19:30:10 +04:00
|
|
|
{
|
2012-11-21 21:53:54 +04:00
|
|
|
SMARTCARD_DEVICE* smartcard = (SMARTCARD_DEVICE*) device;
|
2013-11-04 05:46:40 +04:00
|
|
|
MessageQueue_Post(smartcard->IrpQueue, NULL, 0, (void*) irp, NULL);
|
2011-10-15 19:30:10 +04:00
|
|
|
}
|
|
|
|
|
2014-04-06 04:05:51 +04:00
|
|
|
/* smartcard is always built-in */
|
2012-10-14 10:38:58 +04:00
|
|
|
#define DeviceServiceEntry smartcard_DeviceServiceEntry
|
|
|
|
|
2012-09-19 01:33:52 +04:00
|
|
|
int DeviceServiceEntry(PDEVICE_SERVICE_ENTRY_POINTS pEntryPoints)
|
2011-10-15 19:30:10 +04:00
|
|
|
{
|
|
|
|
char* name;
|
|
|
|
char* path;
|
2013-10-24 23:12:11 +04:00
|
|
|
int length, ck;
|
2012-11-09 04:01:52 +04:00
|
|
|
RDPDR_SMARTCARD* device;
|
2012-11-21 20:56:40 +04:00
|
|
|
SMARTCARD_DEVICE* smartcard;
|
2011-10-15 19:30:10 +04:00
|
|
|
|
2012-11-09 04:01:52 +04:00
|
|
|
device = (RDPDR_SMARTCARD*) pEntryPoints->device;
|
2014-04-04 01:29:12 +04:00
|
|
|
|
2012-11-09 04:01:52 +04:00
|
|
|
name = device->Name;
|
|
|
|
path = device->Path;
|
2011-10-15 19:30:10 +04:00
|
|
|
|
2014-03-26 19:05:12 +04:00
|
|
|
smartcard = (SMARTCARD_DEVICE*) calloc(1, sizeof(SMARTCARD_DEVICE));
|
|
|
|
|
|
|
|
if (!smartcard)
|
|
|
|
return -1;
|
2011-10-15 19:30:10 +04:00
|
|
|
|
2013-09-12 16:34:18 +04:00
|
|
|
smartcard->device.type = RDPDR_DTYP_SMARTCARD;
|
|
|
|
smartcard->device.name = "SCARD";
|
|
|
|
smartcard->device.IRPRequest = smartcard_irp_request;
|
2014-04-06 00:57:31 +04:00
|
|
|
smartcard->device.Init = smartcard_init;
|
2013-09-12 16:34:18 +04:00
|
|
|
smartcard->device.Free = smartcard_free;
|
2011-10-15 19:30:10 +04:00
|
|
|
|
2013-09-12 16:34:18 +04:00
|
|
|
length = strlen(smartcard->device.name);
|
|
|
|
smartcard->device.data = Stream_New(NULL, length + 1);
|
2011-10-15 19:30:10 +04:00
|
|
|
|
2013-09-12 16:55:25 +04:00
|
|
|
Stream_Write(smartcard->device.data, "SCARD", 6);
|
2011-10-15 19:30:10 +04:00
|
|
|
|
2013-09-12 16:41:11 +04:00
|
|
|
smartcard->name = NULL;
|
|
|
|
smartcard->path = NULL;
|
2013-10-24 23:12:11 +04:00
|
|
|
|
2013-09-12 16:41:11 +04:00
|
|
|
if (path)
|
|
|
|
{
|
2012-11-21 20:56:40 +04:00
|
|
|
smartcard->path = path;
|
2013-09-12 16:41:11 +04:00
|
|
|
smartcard->name = name;
|
|
|
|
}
|
2013-09-12 16:55:25 +04:00
|
|
|
else if (name)
|
2013-09-12 16:41:11 +04:00
|
|
|
{
|
|
|
|
if (1 == sscanf(name, "%d", &ck))
|
|
|
|
smartcard->path = name;
|
|
|
|
else
|
|
|
|
smartcard->name = name;
|
|
|
|
}
|
2011-10-15 19:30:10 +04:00
|
|
|
|
2014-04-04 01:29:12 +04:00
|
|
|
smartcard->log = WLog_Get("com.freerdp.channel.smartcard.client");
|
|
|
|
|
2014-04-03 23:27:55 +04:00
|
|
|
smartcard->IrpQueue = MessageQueue_New(NULL);
|
2014-04-06 00:57:31 +04:00
|
|
|
smartcard->OutstandingIrps = ListDictionary_New(TRUE);
|
|
|
|
|
2013-09-12 16:34:18 +04:00
|
|
|
smartcard->thread = CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE) smartcard_thread_func,
|
|
|
|
smartcard, CREATE_SUSPENDED, NULL);
|
2011-10-15 19:30:10 +04:00
|
|
|
|
2013-09-12 16:34:18 +04:00
|
|
|
pEntryPoints->RegisterDevice(pEntryPoints->devman, (DEVICE*) smartcard);
|
2011-10-15 19:30:10 +04:00
|
|
|
|
2013-09-12 16:34:18 +04:00
|
|
|
ResumeThread(smartcard->thread);
|
2011-10-15 19:30:10 +04:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
2012-11-21 20:56:40 +04:00
|
|
|
|