Migrated to new Version of Flavio's Windows driver

This commit is contained in:
Frank Morgner
2014-03-24 08:23:53 +01:00
parent e15307d662
commit bef01d2a31
37 changed files with 4388 additions and 3445 deletions

View File

@@ -0,0 +1,84 @@
[Driver]
NumReaders=12
[Reader0]
RPC_TYPE=0
VENDOR_NAME=VirtualCard
VENDOR_IFD_TYPE=BixReader
DECIVE_UNIT=0
[Reader1]
RPC_TYPE=1
VENDOR_NAME=VirtualCard
VENDOR_IFD_TYPE=BixReader
DECIVE_UNIT=1
[Reader2]
RPC_TYPE=2
VENDOR_NAME=Virtual Smart Card Architecture
VENDOR_IFD_TYPE=Virtual PCD
TCP_PORT=35963
DECIVE_UNIT=2
[Reader3]
RPC_TYPE=2
VENDOR_NAME=Virtual Smart Card Architecture
VENDOR_IFD_TYPE=Virtual PCD
TCP_PORT=35964
DECIVE_UNIT=3
[Reader4]
RPC_TYPE=2
VENDOR_NAME=Virtual Smart Card Architecture
VENDOR_IFD_TYPE=Virtual PCD
TCP_PORT=35965
DECIVE_UNIT=4
[Reader5]
RPC_TYPE=2
VENDOR_NAME=Virtual Smart Card Architecture
VENDOR_IFD_TYPE=Virtual PCD
TCP_PORT=35966
DECIVE_UNIT=5
[Reader6]
RPC_TYPE=2
VENDOR_NAME=Virtual Smart Card Architecture
VENDOR_IFD_TYPE=Virtual PCD
TCP_PORT=35967
DECIVE_UNIT=6
[Reader7]
RPC_TYPE=2
VENDOR_NAME=Virtual Smart Card Architecture
VENDOR_IFD_TYPE=Virtual PCD
TCP_PORT=35968
DECIVE_UNIT=7
[Reader8]
RPC_TYPE=2
VENDOR_NAME=Virtual Smart Card Architecture
VENDOR_IFD_TYPE=Virtual PCD
TCP_PORT=35969
DECIVE_UNIT=8
[Reader9]
RPC_TYPE=2
VENDOR_NAME=Virtual Smart Card Architecture
VENDOR_IFD_TYPE=Virtual PCD
TCP_PORT=35970
DECIVE_UNIT=9
[Reader10]
RPC_TYPE=2
VENDOR_NAME=Virtual Smart Card Architecture
VENDOR_IFD_TYPE=Virtual PCD
TCP_PORT=35971
DECIVE_UNIT=10
[Reader11]
RPC_TYPE=2
VENDOR_NAME=Virtual Smart Card Architecture
VENDOR_IFD_TYPE=Virtual PCD
TCP_PORT=35972
DECIVE_UNIT=11

View File

@@ -0,0 +1,100 @@
// Microsoft Visual C++ generated resource script.
//
#include "resource.h"
#define APSTUDIO_READONLY_SYMBOLS
/////////////////////////////////////////////////////////////////////////////
//
// Generated from the TEXTINCLUDE 2 resource.
//
/////////////////////////////////////////////////////////////////////////////
#undef APSTUDIO_READONLY_SYMBOLS
/////////////////////////////////////////////////////////////////////////////
// Italian (Italy) resources
#if !defined(AFX_RESOURCE_DLL) || defined(AFX_TARG_ITA)
#ifdef _WIN32
LANGUAGE LANG_ITALIAN, SUBLANG_ITALIAN
#pragma code_page(1252)
#endif //_WIN32
/////////////////////////////////////////////////////////////////////////////
//
// Version
//
VS_VERSION_INFO VERSIONINFO
FILEVERSION 6,0,6000,16384
PRODUCTVERSION 6,0,6000,16384
FILEFLAGSMASK 0x3fL
#ifdef _DEBUG
FILEFLAGS 0x9L
#else
FILEFLAGS 0x8L
#endif
FILEOS 0x40004L
FILETYPE 0x2L
FILESUBTYPE 0x0L
BEGIN
BLOCK "StringFileInfo"
BEGIN
BLOCK "001004b0"
BEGIN
VALUE "CompanyName", "Windows (R) Codename Longhorn DDK provider"
VALUE "FileDescription", "Bix Virtual Smart Card Driver"
VALUE "FileVersion", "6.0.6000.16384"
VALUE "InternalName", "BixVReader"
VALUE "LegalCopyright", "<22> Microsoft Corporation. All rights reserved."
VALUE "OriginalFilename", "BixVReader.dll"
VALUE "ProductName", "Windows (R) Codename Longhorn DDK driver"
VALUE "ProductVersion", "6.0.6000.16384"
END
END
BLOCK "VarFileInfo"
BEGIN
VALUE "Translation", 0x10, 1200
END
END
#ifdef APSTUDIO_INVOKED
/////////////////////////////////////////////////////////////////////////////
//
// TEXTINCLUDE
//
1 TEXTINCLUDE
BEGIN
"resource.h\0"
END
2 TEXTINCLUDE
BEGIN
"\0"
END
3 TEXTINCLUDE
BEGIN
"\r\n"
"\0"
END
#endif // APSTUDIO_INVOKED
#endif // Italian (Italy) resources
/////////////////////////////////////////////////////////////////////////////
#ifndef APSTUDIO_INVOKED
/////////////////////////////////////////////////////////////////////////////
//
// Generated from the TEXTINCLUDE 3 resource.
//
P
/////////////////////////////////////////////////////////////////////////////
#endif // not APSTUDIO_INVOKED

View File

@@ -0,0 +1,138 @@
#include "internal.h"
class CVirtualSmartCardDriverModule : public CAtlDllModuleT<CVirtualSmartCardDriverModule> {} _AtlModule;
/////////////////////////////////////////////////////////////////////////
//
// DllMain
//
// This is the main DLL Entry Point.
//
// Parameters:
// hInstance - Handle to the DLL module
// dwReason - Indicates why the DLL entry point is being called
// lpReserved - Additional information based on dwReason
//
// Return Values:
// TRUE = initialization succeeds
// FALSE = initialization fails
//
/////////////////////////////////////////////////////////////////////////
extern "C" BOOL WINAPI DllMain(HINSTANCE hInstance,
DWORD dwReason,
LPVOID lpReserved)
{
inFunc
(lpReserved);
switch (dwReason)
{
case DLL_PROCESS_ATTACH:
DisableThreadLibraryCalls(hInstance);
break;
case DLL_PROCESS_DETACH:
_AtlModule.Term();
break;
default:
break;
}
// Call the ATL module class so it can initialize
return _AtlModule.DllMain(dwReason, lpReserved);
}
/////////////////////////////////////////////////////////////////////////
//
// DllCanUnloadNow
//
// Used to determine whether the DLL can be unloaded by OLE
//
// Parameters:
// void - (unused argument)
//
// Return Values:
// S_OK: DLL can be unloaded
// S_FALSE: DLL cannot be unloaded now
//
/////////////////////////////////////////////////////////////////////////
STDAPI DllCanUnloadNow(void)
{
inFunc
return _AtlModule.DllCanUnloadNow();
}
/////////////////////////////////////////////////////////////////////////
//
// DllGetClassObject
//
// Returns a class factory to create an object of the requested type
//
// Parameters:
// rclsid - CLSID that will associate the correct data and code
// riid - Reference to the IID the caller will use
// ppv - pointer to an interface pointer requested in riid
//
// Return Values:
// S_OK: The object was retrieved successfully.
// CLASS_E_CLASSNOTAVAILABLE: The DLL does not support the class
//
/////////////////////////////////////////////////////////////////////////
STDAPI DllGetClassObject(__in REFCLSID rclsid, __in REFIID riid, __deref_out LPVOID* ppv)
{
inFunc
return _AtlModule.DllGetClassObject(rclsid, riid, ppv);
}
/////////////////////////////////////////////////////////////////////////
//
// DllRegisterServer
//
// Adds entries to the system registry
//
// Parameters:
// void - (unused argument)
//
// Return Values:
// S_OK: The registry entries were created successfully
// SELFREG_E_TYPELIB: The server was unable to complete the
// registration of all the type libraries used by its classes
// SELFREG_E_CLASS: The server was unable to complete the
// registration of all the object classes
//
/////////////////////////////////////////////////////////////////////////
STDAPI DllRegisterServer(void)
{
inFunc
return S_OK;
}
/////////////////////////////////////////////////////////////////////////
//
// DllUnregisterServer
//
// Removes entries from the system registry
//
// Parameters:
// void - (unused argument)
//
// Return Values:
// S_OK: The registry entries were removed successfully
// S_FALSE: Unregistration of known entries was successful, but
// other entries still exist for this server's classes
// SELFREG_E_TYPELIB: The server was unable to remove the entries
// of all the type libraries used by its classes
// SELFREG_E_CLASS: The server was unable to to remove the entries
// of all the object classes
//
/////////////////////////////////////////////////////////////////////////
STDAPI DllUnregisterServer(void)
{
inFunc
return S_OK;
}

View File

@@ -0,0 +1,264 @@
#include "internal.h"
#include "VirtualSCReader.h"
#include "reader.h"
#include "device.h"
#include <winscard.h>
#include "memory.h"
#include <Sddl.h>
#include "sectionLocker.h"
BOOL CreateMyDACL(SECURITY_ATTRIBUTES * pSA)
{
TCHAR * szSD = TEXT("D:") // Discretionary ACL
TEXT("(D;OICI;GA;;;BG)") // Deny access to
// built-in guests
TEXT("(D;OICI;GA;;;AN)") // Deny access to
// anonymous logon
TEXT("(A;OICI;GRGWGX;;;AU)") // Allow
// read/write/execute
// to authenticated
// users
TEXT("(A;OICI;GA;;;BA)"); // Allow full control
// to administrators
if (NULL == pSA)
return FALSE;
return ConvertStringSecurityDescriptorToSecurityDescriptor(
szSD,
SDDL_REVISION_1,
&(pSA->lpSecurityDescriptor),
NULL);
}
PipeReader::PipeReader() {
rpcType=0;
state=SCARD_ABSENT;
InitializeCriticalSection(&eventSection);
InitializeCriticalSection(&dataSection);
}
void PipeReader::init(wchar_t *section) {
wchar_t temp[300];
swprintf(temp,L"SCardSimulatorDriver%i",instance);
GetPrivateProfileStringW(section,L"PIPE_NAME",temp,pipeName,300,L"BixVReader.ini");
swprintf(temp,L"SCardSimulatorDriverEvents%i",instance);
GetPrivateProfileStringW(section,L"PIPE_EVENT_NAME",temp,pipeEventName,300,L"BixVReader.ini");
}
bool PipeReader::CheckATR() {
//SectionLocker lock(dataSection);
if (pipe==NULL)
return false;
DWORD read=0;
DWORD command=1;
if (!WriteFile(pipe,&command,sizeof(DWORD),&read,NULL)) {
return false;
}
FlushFileBuffers(pipe);
DWORD size=0;
if (!ReadFile(pipe,&size,sizeof(DWORD),&read,NULL)) {
return false;
}
if (size==0)
return false;
BYTE ATR[100];
if (!ReadFile(pipe,ATR,size,&read,NULL)) {
return false;
}
return true;
}
bool PipeReader::QueryTransmit(BYTE *APDU,int APDUlen,BYTE *Resp,int *Resplen) {
//SectionLocker lock(dataSection);
if (pipe==NULL)
return false;
DWORD command=2;
DWORD read=0;
if (!WriteFile(pipe,&command,sizeof(DWORD),&read,NULL)) {
pipe=NULL;
return false;
}
DWORD dwAPDUlen=(DWORD)APDUlen;
if (!WriteFile(pipe,&dwAPDUlen,sizeof(DWORD),&read,NULL)) {
pipe=NULL;
return false;
}
if (!WriteFile(pipe,APDU,APDUlen,&read,NULL)) {
pipe=NULL;
return false;
}
FlushFileBuffers(pipe);
DWORD dwRespLen=0;
if (!ReadFile(pipe,&dwRespLen,sizeof(DWORD),&read,NULL)) {
pipe=NULL;
return false;
}
if (!ReadFile(pipe,Resp,dwRespLen,&read,NULL)) {
pipe=NULL;
return false;
}
(*Resplen)=(int)dwRespLen;
return true;
}
bool PipeReader::QueryATR(BYTE *ATR,DWORD *ATRsize,bool reset) {
//SectionLocker lock(dataSection);
if (pipe==NULL)
return false;
DWORD command=reset ? 0 : 1;
DWORD read=0;
if (!WriteFile(pipe,&command,sizeof(DWORD),&read,NULL)) {
pipe=NULL;
return false;
}
FlushFileBuffers(pipe);
DWORD size=0;
if (!ReadFile(pipe,&size,sizeof(DWORD),&read,NULL)) {
pipe=NULL;
return false;
}
if (size==0)
return false;
if (!ReadFile(pipe,ATR,size,&read,NULL)) {
pipe=NULL;
return false;
}
(*ATRsize)=size;
return true;
}
DWORD PipeReader::startServer() {
SECURITY_ATTRIBUTES sa;
sa.nLength = sizeof(SECURITY_ATTRIBUTES);
sa.bInheritHandle = FALSE;
CreateMyDACL(&sa);
wchar_t temp[300];
swprintf(temp,L"\\\\.\\pipe\\%s",pipeName);
HANDLE _pipe=CreateNamedPipe(temp,PIPE_ACCESS_DUPLEX|FILE_FLAG_OVERLAPPED,PIPE_TYPE_BYTE,PIPE_UNLIMITED_INSTANCES,0,0,0,&sa);
swprintf(temp,L"\\\\.\\pipe\\%s",pipeEventName);
HANDLE _eventpipe=CreateNamedPipe(temp,PIPE_ACCESS_DUPLEX|FILE_FLAG_OVERLAPPED,PIPE_TYPE_BYTE,PIPE_UNLIMITED_INSTANCES,0,0,0,&sa);
wchar_t log[300];
swprintf(log,L"[BixVReader]Pipe created:%s:%08x",pipeName,_pipe);
OutputDebugString(log);
while (true) {
//__try {
BOOL ris=ConnectNamedPipe(_pipe,NULL);
if (ris==0) {
swprintf(log,L"[BixVReader]Pipe NOT connected:%x",GetLastError());
OutputDebugString(log);
}
else {
swprintf(log,L"[BixVReader]Pipe connected");
OutputDebugString(log);
}
ris=ConnectNamedPipe(_eventpipe,NULL);
if (ris==0) {
swprintf(log,L"[BixVReader]Event Pipe NOT connected:%x",GetLastError());
OutputDebugString(log);
}
else {
swprintf(log,L"[BixVReader]Event Pipe connected");
OutputDebugString(log);
}
pipe=_pipe;
eventpipe=_eventpipe;
if (waitInsertIpr!=NULL) {
SectionLocker lock(device->m_RequestLock);
// if I'm waiting for card insertion, verify if there's a card present
if (initProtocols()) {
if (waitInsertIpr->UnmarkCancelable()==S_OK)
waitInsertIpr->CompleteWithInformation(STATUS_SUCCESS, 0);
waitInsertIpr=NULL;
state=SCARD_SWALLOWED;
}
}
while (true) {
// wait for a command
DWORD command=0;
DWORD read=0;
if (!ReadFile(eventpipe,&command,sizeof(DWORD),&read,NULL)) {
state=SCARD_ABSENT;
OutputDebugString(L"[BixVReader]Pipe error");
powered=0;
pipe=NULL;
eventpipe=NULL;
if (waitRemoveIpr!=NULL) {// card inserted
SectionLocker lock(device->m_RequestLock);
OutputDebugString(L"[BixVReader]complete Wait Remove");
if (waitRemoveIpr->UnmarkCancelable()==S_OK) {
OutputDebugString(L"[BixVReader]Wait Remove Unmarked");
waitRemoveIpr->CompleteWithInformation(STATUS_SUCCESS, 0);
OutputDebugString(L"[BixVReader]Wait Remove Completed");
}
waitRemoveIpr=NULL;
}
if (waitInsertIpr!=NULL) {// card removed
SectionLocker lock(device->m_RequestLock);
OutputDebugString(L"[BixVReader]cancel Wait Remove");
if (waitInsertIpr->UnmarkCancelable()==S_OK) {
OutputDebugString(L"[BixVReader]Wait Insert Unmarked");
waitInsertIpr->CompleteWithInformation(HRESULT_FROM_WIN32(ERROR_CANCELLED), 0);
OutputDebugString(L"[BixVReader]Wait Insert Cancelled");
}
waitInsertIpr=NULL;
}
DisconnectNamedPipe(_pipe);
DisconnectNamedPipe(_eventpipe);
break;
}
OutputDebugString(L"[BixVReader]Pipe data");
if (command==0)
powered=0;
if (command==0 && waitRemoveIpr!=NULL) {// card removed
SectionLocker lock(device->m_RequestLock);
state=SCARD_ABSENT;
if (waitRemoveIpr->UnmarkCancelable()==S_OK) {
waitRemoveIpr->CompleteWithInformation(STATUS_SUCCESS, 0);
}
waitRemoveIpr=NULL;
}
else if (command==1 && waitInsertIpr!=NULL) {// card inserted
SectionLocker lock(device->m_RequestLock);
state=SCARD_SWALLOWED;
initProtocols();
if (waitInsertIpr->UnmarkCancelable()==S_OK) {
waitInsertIpr->CompleteWithInformation(STATUS_SUCCESS, 0);
}
waitInsertIpr=NULL;
}
}
//}
//__except(EXCEPTION_EXECUTE_HANDLER) {
// wchar_t log[300];
// DWORD err=GetExceptionCode();
// swprintf(log,L"Exception:%08X",err);
// OutputDebugString(log);
//}
}
OutputDebugString(L"[BixVReader]Pipe quit!!!");
return 0;
}
void PipeReader::shutdown() {
state=SCARD_ABSENT;
if (waitRemoveIpr!=NULL) {
SectionLocker lock(device->m_RequestLock);
if (waitRemoveIpr->UnmarkCancelable()==S_OK)
waitRemoveIpr->Complete(HRESULT_FROM_WIN32(ERROR_CANCELLED));
waitRemoveIpr=NULL;
}
if (waitInsertIpr!=NULL) {
SectionLocker lock(device->m_RequestLock);
if (waitInsertIpr->UnmarkCancelable()==S_OK)
waitInsertIpr->Complete(HRESULT_FROM_WIN32(ERROR_CANCELLED));
waitInsertIpr=NULL;
}
}

View File

@@ -0,0 +1,124 @@
#include "internal.h"
#include "Device.h"
#include "Queue.h"
#include "SectionLocker.h"
/////////////////////////////////////////////////////////////////////////
//
// CMyQueue::CMyQueue
//
// Object constructor function
//
// Initialize member variables
//
/////////////////////////////////////////////////////////////////////////
CMyQueue::CMyQueue() :
m_pParentDevice(NULL)
{
}
/////////////////////////////////////////////////////////////////////////
//
// CMyQueue::~CMyQueue
//
// Object destructor function
//
//
/////////////////////////////////////////////////////////////////////////
CMyQueue::~CMyQueue()
{
// Release the reference that was incremented in CreateInstance()
SAFE_RELEASE(m_pParentDevice);
}
/////////////////////////////////////////////////////////////////////////
//
// CMyQueue::CreateInstance
//
// This function supports the COM factory creation system
//
// Parameters:
// parentDevice - A pointer to the CWSSDevice object
// ppUkwn - pointer to a pointer to the queue to be returned
//
// Return Values:
// S_OK: The queue was created successfully
//
/////////////////////////////////////////////////////////////////////////
HRESULT CMyQueue::CreateInstance(__in IWDFDevice* pWdfDevice, CMyDevice* pMyDevice)
{
inFunc
SectionLogger a(__FUNCTION__);
CComObject<CMyQueue>* pMyQueue = NULL;
if(NULL == pMyDevice)
{
return E_INVALIDARG;
}
HRESULT hr = CComObject<CMyQueue>::CreateInstance(&pMyQueue);
if(SUCCEEDED(hr))
{
// AddRef the object
pMyQueue->AddRef();
// Store the parent device object
pMyQueue->m_pParentDevice = pMyDevice;
// Increment the reference for the lifetime of the CMyQueue object.
pMyQueue->m_pParentDevice->AddRef();
CComPtr<IUnknown> spIUnknown;
hr = pMyQueue->QueryInterface(IID_IUnknown, (void**)&spIUnknown);
if(SUCCEEDED(hr))
{
// Create the framework queue
CComPtr<IWDFIoQueue> spDefaultQueue;
hr = pWdfDevice->CreateIoQueue( spIUnknown,
TRUE, // DefaultQueue
WdfIoQueueDispatchParallel, // Parallel queue handling
FALSE, // PowerManaged
TRUE, // AllowZeroLengthRequests
&spDefaultQueue
);
if (FAILED(hr))
{
OutputDebugString (L"[BixVReader]IoQueue NOT Created\n");
}
else
OutputDebugString (L"[BixVReader]IoQueue Created\n");
}
// Release the pMyQueue pointer when done. Note: UMDF holds a reference to it above
SAFE_RELEASE(pMyQueue);
}
return hr;
}
/////////////////////////////////////////////////////////////////////////
//
// CMyQueue::OnDeviceIoControl
//
// This method is called when an IOCTL is sent to the device
//
// Parameters:
// pQueue - pointer to an IO queue
// pRequest - pointer to an IO request
// ControlCode - The IOCTL to process
// InputBufferSizeInBytes - the size of the input buffer
// OutputBufferSizeInBytes - the size of the output buffer
//
/////////////////////////////////////////////////////////////////////////
STDMETHODIMP_ (void) CMyQueue::OnDeviceIoControl(
__in IWDFIoQueue* pQueue,
__in IWDFIoRequest* pRequest,
__in ULONG ControlCode,
SIZE_T InputBufferSizeInBytes,
SIZE_T OutputBufferSizeInBytes
)
{
m_pParentDevice->ProcessIoControl(pQueue,pRequest,ControlCode,InputBufferSizeInBytes,OutputBufferSizeInBytes);
}

View File

@@ -0,0 +1,36 @@
#pragma once
class ATL_NO_VTABLE CMyQueue :
public CComObjectRootEx<CComMultiThreadModel>,
public IQueueCallbackDeviceIoControl
{
public:
virtual ~CMyQueue();
DECLARE_NOT_AGGREGATABLE(CMyQueue)
BEGIN_COM_MAP(CMyQueue)
COM_INTERFACE_ENTRY(IQueueCallbackDeviceIoControl)
END_COM_MAP()
static HRESULT CreateInstance(__in IWDFDevice* pWdfDevice, CMyDevice* pMyDevice);
protected:
CMyQueue();
// COM Interface methods
public:
// IQueueCallbackDeviceIoControl
STDMETHOD_ (void, OnDeviceIoControl)(
__in IWDFIoQueue* pQueue,
__in IWDFIoRequest* pRequest,
__in ULONG ControlCode,
SIZE_T InputBufferSizeInBytes,
SIZE_T OutputBufferSizeInBytes
);
private:
CMyDevice* m_pParentDevice; // Parent device object
};

View File

@@ -0,0 +1,426 @@
#include "internal.h"
#include "VirtualSCReader.h"
#include "reader.h"
#include "device.h"
#include <winscard.h>
#include "memory.h"
#include <Sddl.h>
#include "SectionLocker.h"
void Reader::init(wchar_t *section) {
section;
state=SCARD_ABSENT;
}
void Reader::IoSmartCardIsPresent(IWDFIoRequest* pRequest,SIZE_T inBufSize,SIZE_T outBufSize) {
UNREFERENCED_PARAMETER(inBufSize);
UNREFERENCED_PARAMETER(outBufSize);
OutputDebugString(L"[BixVReader][IPRE]IOCTL_SMARTCARD_IS_PRESENT");
if (CheckATR()) {
// there's a smart card present, so complete the request
SectionLocker lock(device->m_RequestLock);
pRequest->CompleteWithInformation(STATUS_SUCCESS, 0);
}
else {
// there's no smart card present, so leave the request pending; it will be completed later
SectionLocker lock(device->m_RequestLock);
waitInsertIpr=pRequest;
IRequestCallbackCancel *callback;
device->QueryInterface(__uuidof(IRequestCallbackCancel),(void**)&callback);
pRequest->MarkCancelable(callback);
callback->Release();
}
}
void Reader::IoSmartCardGetState(IWDFIoRequest* pRequest,SIZE_T inBufSize,SIZE_T outBufSize) {
UNREFERENCED_PARAMETER(inBufSize);
UNREFERENCED_PARAMETER(outBufSize);
OutputDebugString(L"[BixVReader][GSTA]IOCTL_SMARTCARD_GET_STATE");
wchar_t log[300];
swprintf(log,L"[BixVReader]STATE:%08X",state);
OutputDebugString(log);
setInt(device,pRequest,state);
}
void Reader::IoSmartCardIsAbsent(IWDFIoRequest* pRequest,SIZE_T inBufSize,SIZE_T outBufSize) {
UNREFERENCED_PARAMETER(inBufSize);
UNREFERENCED_PARAMETER(outBufSize);
OutputDebugString(L"[BixVReader][IABS]IOCTL_SMARTCARD_IS_ABSENT");
if (!CheckATR()) {
// there's no smart card present, so complete the request
SectionLocker lock(device->m_RequestLock);
pRequest->CompleteWithInformation(STATUS_SUCCESS, 0);
}
else {
SectionLocker lock(device->m_RequestLock);
// there's a smart card present, so leave the request pending; it will be completed later
waitRemoveIpr=pRequest;
IRequestCallbackCancel *callback;
device->QueryInterface(__uuidof(IRequestCallbackCancel),(void**)&callback);
pRequest->MarkCancelable(callback);
callback->Release();
}
}
void Reader::IoSmartCardPower(IWDFIoRequest* pRequest,SIZE_T inBufSize,SIZE_T outBufSize) {
UNREFERENCED_PARAMETER(inBufSize);
UNREFERENCED_PARAMETER(outBufSize);
OutputDebugString(L"[BixVReader][POWR]IOCTL_SMARTCARD_POWER");
DWORD code=getInt(pRequest);
if (code==SCARD_COLD_RESET) {
OutputDebugString(L"[BixVReader][POWR]SCARD_COLD_RESET");
protocol=0;
powered=1;
state=SCARD_NEGOTIABLE;
}
else if (code==SCARD_WARM_RESET) {
OutputDebugString(L"[BixVReader][POWR]SCARD_WARM_RESET");
protocol=0;
powered=1;
state=SCARD_NEGOTIABLE;
}
else if (code==SCARD_POWER_DOWN) {
OutputDebugString(L"[BixVReader][POWR]SCARD_POWER_DOWN");
protocol=0;
powered=0;
state=SCARD_SWALLOWED;
}
if (code==SCARD_COLD_RESET || code==SCARD_WARM_RESET) {
BYTE ATR[100];
DWORD ATRsize;
if (!QueryATR(ATR,&ATRsize,true))
{
pRequest->CompleteWithInformation(STATUS_NO_MEDIA, 0);
return;
}
setBuffer(device,pRequest,ATR,ATRsize);
}
else {
SectionLocker lock(device->m_RequestLock);
pRequest->CompleteWithInformation(STATUS_SUCCESS, 0);
}
}
void Reader::IoSmartCardSetProtocol(IWDFIoRequest* pRequest,SIZE_T inBufSize,SIZE_T outBufSize) {
UNREFERENCED_PARAMETER(inBufSize);
UNREFERENCED_PARAMETER(outBufSize);
UNREFERENCED_PARAMETER(instance);
DWORD requestedProtocol=getInt(pRequest);
wchar_t log[300];
swprintf(log,L"[BixVReader][SPRT]IOCTL_SMARTCARD_SET_PROTOCOL:%08X",requestedProtocol);
OutputDebugString(log);
BYTE ATR[100];
DWORD ATRsize;
state=SCARD_SPECIFIC;
if (!QueryATR(ATR,&ATRsize,true))
{
SectionLocker lock(device->m_RequestLock);
pRequest->CompleteWithInformation(STATUS_NO_MEDIA, 0);
return;
}
if (((requestedProtocol & SCARD_PROTOCOL_T1) != 0) &&
((availableProtocol & SCARD_PROTOCOL_T1) != 0)) {
protocol = SCARD_PROTOCOL_T1;
SectionLocker lock(device->m_RequestLock);
pRequest->CompleteWithInformation(STATUS_SUCCESS, 0);
OutputDebugString(L"[BixVReader]PROTOCOL SET: T1");
return;
}
if (((requestedProtocol & SCARD_PROTOCOL_T0) != 0) &&
((availableProtocol & SCARD_PROTOCOL_T0) != 0)) {
protocol = SCARD_PROTOCOL_T0;
SectionLocker lock(device->m_RequestLock);
pRequest->CompleteWithInformation(STATUS_SUCCESS, 0);
OutputDebugString(L"[BixVReader]PROTOCOL SET: T0");
return;
}
if (((requestedProtocol & (SCARD_PROTOCOL_DEFAULT)) != 0) &&
((availableProtocol & SCARD_PROTOCOL_T1) != 0)) {
protocol = SCARD_PROTOCOL_T1;
SectionLocker lock(device->m_RequestLock);
pRequest->CompleteWithInformation(STATUS_SUCCESS, 0);
OutputDebugString(L"[BixVReader]PROTOCOL SET: T1");
return;
}
if (((requestedProtocol & (SCARD_PROTOCOL_DEFAULT)) != 0) &&
((availableProtocol & SCARD_PROTOCOL_T0) != 0)) {
protocol = SCARD_PROTOCOL_T0;
SectionLocker lock(device->m_RequestLock);
pRequest->CompleteWithInformation(STATUS_SUCCESS, 0);
OutputDebugString(L"[BixVReader]PROTOCOL SET: T0");
return;
}
{
state=SCARD_NEGOTIABLE;
SectionLocker lock(device->m_RequestLock);
pRequest->CompleteWithInformation(HRESULT_FROM_WIN32(ERROR_NOT_SUPPORTED), 0);
}
}
void Reader::IoSmartCardSetAttribute(IWDFIoRequest* pRequest,SIZE_T inBufSize,SIZE_T outBufSize) {
UNREFERENCED_PARAMETER(inBufSize);
UNREFERENCED_PARAMETER(outBufSize);
UNREFERENCED_PARAMETER(instance);
OutputDebugString(L"[BixVReader][SATT]IOCTL_SMARTCARD_SET_ATTRIBUTE");
IWDFMemory *inmem=NULL;
pRequest->GetInputMemory(&inmem);
SIZE_T size;
BYTE *data=(BYTE *)inmem->GetDataBuffer(&size);
DWORD minCode=*(DWORD*)(data);
bool handled=false;
if (minCode==SCARD_ATTR_DEVICE_IN_USE) {
SectionLocker lock(device->m_RequestLock);
OutputDebugString(L"[BixVReader][SATT]SCARD_ATTR_DEVICE_IN_USE");
pRequest->CompleteWithInformation(STATUS_SUCCESS, 0);
handled=true;
}
inmem->Release();
if (!handled) {
SectionLocker lock(device->m_RequestLock);
wchar_t log[300];
swprintf(log,L"[BixVReader][SATT]ERROR_NOT_SUPPORTED:%08X",minCode);
OutputDebugString(log);
pRequest->CompleteWithInformation(HRESULT_FROM_WIN32(ERROR_NOT_SUPPORTED), 0);
}
}
void Reader::IoSmartCardTransmit(IWDFIoRequest* pRequest,SIZE_T inBufSize,SIZE_T outBufSize) {
UNREFERENCED_PARAMETER(inBufSize);
UNREFERENCED_PARAMETER(outBufSize);
OutputDebugString(L"[BixVReader][TRSM]IOCTL_SMARTCARD_TRANSMIT");
BYTE APDU[1000];
int APDUSize;
getBuffer(pRequest,APDU,&APDUSize);
if (((SCARD_IO_REQUEST*)APDU)->dwProtocol!=protocol) {
SectionLocker lock(device->m_RequestLock);
pRequest->CompleteWithInformation(STATUS_INVALID_DEVICE_STATE, 0);
return;
}
BYTE Resp[1000];
int RespSize;
if (!QueryTransmit(APDU+sizeof(SCARD_IO_REQUEST),APDUSize-sizeof(SCARD_IO_REQUEST),Resp+sizeof(SCARD_IO_REQUEST),&RespSize))
{
SectionLocker lock(device->m_RequestLock);
pRequest->CompleteWithInformation(STATUS_NO_MEDIA, 0);
return;
}
((SCARD_IO_REQUEST*)Resp)->cbPciLength=sizeof(SCARD_IO_REQUEST);
((SCARD_IO_REQUEST*)Resp)->dwProtocol=protocol;
setBuffer(device,pRequest,Resp,RespSize+sizeof(SCARD_IO_REQUEST));
}
void Reader::IoSmartCardGetAttribute(IWDFIoRequest* pRequest,SIZE_T inBufSize,SIZE_T outBufSize) {
UNREFERENCED_PARAMETER(inBufSize);
wchar_t log[300]=L"";
char temp[300];
DWORD code=getInt(pRequest);
swprintf(log,L"[BixVReader][GATT] - code %0X",code);
OutputDebugString(log);
switch(code) {
case SCARD_ATTR_VALUE(SCARD_CLASS_VENDOR_DEFINED, 0xA009):
// custom attribute; RPC_TYPE
OutputDebugString(L"[BixVReader][GATT]RPC_TYPE");
setInt(device,pRequest,rpcType);
return;
case SCARD_ATTR_VALUE(SCARD_CLASS_VENDOR_DEFINED, 0xA00a):
// custom attribute; PipeName
if (rpcType==0) {
PipeReader *pipe=(PipeReader *)this;
OutputDebugString(L"[BixVReader][GATT]PIPE_NAME");
sprintf(temp,"%S",pipe->pipeName);
setString(device,pRequest,(char*)temp,(int)outBufSize);
}
else {
SectionLocker lock(device->m_RequestLock);
pRequest->CompleteWithInformation(HRESULT_FROM_WIN32(ERROR_NOT_SUPPORTED), 0);
}
return;
case SCARD_ATTR_VALUE(SCARD_CLASS_VENDOR_DEFINED, 0xA00b):
// custom attribute; EventPipeName
if (rpcType==0) {
PipeReader *pipe=(PipeReader *)this;
OutputDebugString(L"[BixVReader][GATT]EVENT_PIPE_NAME");
sprintf(temp,"%S",pipe->pipeEventName);
setString(device,pRequest,(char*)temp,(int)outBufSize);
}
else {
SectionLocker lock(device->m_RequestLock);
pRequest->CompleteWithInformation(HRESULT_FROM_WIN32(ERROR_NOT_SUPPORTED), 0);
}
return;
case SCARD_ATTR_VALUE(SCARD_CLASS_VENDOR_DEFINED, 0xA00c):
// custom attribute; TCP port
if (rpcType==1) {
TcpIpReader *tcpIp=(TcpIpReader *)this;
OutputDebugString(L"[BixVReader][GATT]PORT");
setInt(device,pRequest,tcpIp->port);
}
else {
SectionLocker lock(device->m_RequestLock);
pRequest->CompleteWithInformation(HRESULT_FROM_WIN32(ERROR_NOT_SUPPORTED), 0);
}
return;
case SCARD_ATTR_VALUE(SCARD_CLASS_VENDOR_DEFINED, 0xA00d):
// custom attribute; TCP event port
if (rpcType==1) {
TcpIpReader *tcpIp=(TcpIpReader *)this;
OutputDebugString(L"[BixVReader][GATT]EVENT_PORT");
setInt(device,pRequest,tcpIp->eventPort);
}
else {
SectionLocker lock(device->m_RequestLock);
pRequest->CompleteWithInformation(HRESULT_FROM_WIN32(ERROR_NOT_SUPPORTED), 0);
}
return;
case SCARD_ATTR_VALUE(SCARD_CLASS_VENDOR_DEFINED, 0xA00e):
// custom attribute; TCP base port
if (rpcType==1) {
TcpIpReader *tcpIp=(TcpIpReader *)this;
OutputDebugString(L"[BixVReader][GATT]BASE_PORT");
setInt(device,pRequest,tcpIp->portBase);
tcpIp;
}
else {
SectionLocker lock(device->m_RequestLock);
pRequest->CompleteWithInformation(HRESULT_FROM_WIN32(ERROR_NOT_SUPPORTED), 0);
}
return;
case SCARD_ATTR_CHARACTERISTICS:
// 0x00000000 No special characteristics
OutputDebugString(L"[BixVReader][GATT]SCARD_ATTR_CHARACTERISTICS");
setInt(device,pRequest,0);
return;
case SCARD_ATTR_VENDOR_NAME:
OutputDebugString(L"[BixVReader][GATT]SCARD_ATTR_VENDOR_NAME");
setString(device,pRequest,vendorName,(int)outBufSize);
return;
case SCARD_ATTR_VENDOR_IFD_TYPE:
OutputDebugString(L"[BixVReader][GATT]SCARD_ATTR_VENDOR_IFD_TYPE");
setString(device,pRequest,vendorIfdType,(int)outBufSize);
return;
case SCARD_ATTR_DEVICE_UNIT:
OutputDebugString(L"[BixVReader][GATT]SCARD_ATTR_DEVICE_UNIT");
setInt(device,pRequest,deviceUnit);
return;
case SCARD_ATTR_ATR_STRING:
OutputDebugString(L"[BixVReader][GATT]SCARD_ATTR_ATR_STRING");
BYTE ATR[100];
DWORD ATRsize;
if (!QueryATR(ATR,&ATRsize))
{
SectionLocker lock(device->m_RequestLock);
pRequest->CompleteWithInformation(STATUS_NO_MEDIA, 0);
return;
}
setBuffer(device,pRequest,ATR,ATRsize);
return;
case SCARD_ATTR_CURRENT_PROTOCOL_TYPE:
OutputDebugString(L"[BixVReader][GATT]SCARD_ATTR_CURRENT_PROTOCOL_TYPE");
setInt(device,pRequest,protocol); // T=0 or T=1
return;
default: {
swprintf(log,L"[BixVReader][GATT]ERROR_NOT_SUPPORTED:%08X",code);
OutputDebugString(log);
SectionLocker lock(device->m_RequestLock);
pRequest->CompleteWithInformation(HRESULT_FROM_WIN32(ERROR_NOT_SUPPORTED), 0);
}
}
}
bool Reader::CheckATR() {
return false;
}
bool Reader::QueryTransmit(BYTE *APDU,int APDUlen,BYTE *Resp,int *Resplen) {
APDU;
APDUlen;
Resp;
Resplen;
return false;
}
bool Reader::QueryATR(BYTE *ATR,DWORD *ATRsize,bool reset) {
ATR;
ATRsize;
reset;
return false;
}
bool Reader::initProtocols() {
// ask ATR to determine available protocols
BYTE ATR[100];
DWORD ATRsize=100;
availableProtocol=0;
if (QueryATR(ATR,&ATRsize,true))
{
availableProtocol=0;
int iNumHistBytes=0;
int iTotHistBytes=0;
int y=0;
int block=1;
bool isHist=false;
char let='A';
for (unsigned int i=0;i<ATRsize && ! isHist;i++) {
if (i==0) {
// we are in TS
}
else if (i==1) {
// we are in T0; read number of historical bytes
let='A';
y=ATR[i] >> 4;
iTotHistBytes=ATR[i] & 0xF;
}
else {
while ((y & 1)==0) {
let++;
y>>=1;
if (y==0) {
isHist=true;
iNumHistBytes=1;
i--;
goto end;
}
}
// we are in T(let)(block)
y=y & 0xE;
if (let=='D') {
// check available protocol(s)
int protocol=(ATR[i] & 0x0f);
availableProtocol|=1 << protocol;
// prepare for next block
let='A';
y=ATR[i] >> 4;
block++;
}
}
end:
;
}
//support at least T=0
if (availableProtocol==0)
availableProtocol=1;
return true;
}
return false;
}
DWORD Reader::startServer() {
return 0;
}
void Reader::shutdown() {
}

View File

@@ -0,0 +1,104 @@
#pragma once
#include <Winsock2.h>
class CMyDevice;
class Reader {
public:
CMyDevice *device;
CComPtr<IWDFIoRequest> waitRemoveIpr;
CComPtr<IWDFIoRequest> waitInsertIpr;
inbuf;
outbuf;
HANDLE serverThread;
char vendorName[300];
char vendorIfdType[300];
int deviceUnit;
int powered;
int instance;
int rpcType;
int state;
DWORD protocol; // T0 or T1 - protocol in use
DWORD availableProtocol; // T0, T1 or Both - protocols available
void IoSmartCardGetAttribute(IWDFIoRequest* pRequest,SIZE_T inBufSize,SIZE_T outBufSize);
void IoSmartCardIsPresent(IWDFIoRequest* pRequest,SIZE_T inBufSize,SIZE_T outBufSize);
void IoSmartCardGetState(IWDFIoRequest* pRequest,SIZE_T inBufSize,SIZE_T outBufSize);
void IoSmartCardIsAbsent(IWDFIoRequest* pRequest,SIZE_T inBufSize,SIZE_T outBufSize);
void IoSmartCardPower(IWDFIoRequest* pRequest,SIZE_T inBufSize,SIZE_T outBufSize);
void IoSmartCardSetAttribute(IWDFIoRequest* pRequest,SIZE_T inBufSize,SIZE_T outBufSize);
void IoSmartCardSetProtocol(IWDFIoRequest* pRequest,SIZE_T inBufSize,SIZE_T outBufSize);
void IoSmartCardTransmit(IWDFIoRequest* pRequest,SIZE_T inBufSize,SIZE_T outBufSize);
bool initProtocols();
virtual bool QueryTransmit(BYTE *APDU,int APDUlen,BYTE *Resp,int *Resplen);
virtual bool QueryATR(BYTE *ATR,DWORD *ATRsize,bool reset=false);
virtual bool CheckATR();
virtual DWORD startServer();
virtual void shutdown();
virtual void init(wchar_t *section);
};
class PipeReader : public Reader {
public:
wchar_t pipeName[300];
wchar_t pipeEventName[300];
HANDLE pipe;
HANDLE eventpipe;
PipeReader();
bool QueryTransmit(BYTE *APDU,int APDUlen,BYTE *Resp,int *Resplen);
bool QueryATR(BYTE *ATR,DWORD *ATRsize,bool reset=false);
bool CheckATR();
DWORD startServer();
void shutdown();
void init(wchar_t *section);
CRITICAL_SECTION eventSection;
CRITICAL_SECTION dataSection;
};
class TcpIpReader : public Reader {
public:
static int portBase;
int port;
int eventPort;
SOCKET socket;
SOCKET AcceptSocket;
SOCKET eventsocket;
bool breakSocket;
TcpIpReader();
bool QueryTransmit(BYTE *APDU,int APDUlen,BYTE *Resp,int *Resplen);
bool QueryATR(BYTE *ATR,DWORD *ATRsize,bool reset=false);
bool CheckATR();
DWORD startServer();
void shutdown();
void init(wchar_t *section);
CRITICAL_SECTION eventSection;
CRITICAL_SECTION dataSection;
};
class VpcdReader : public Reader {
public:
static int portBase;
short port;
void *ctx;
bool breakSocket;
bool cardPresent;
VpcdReader();
bool QueryTransmit(BYTE *APDU,int APDUlen,BYTE *Resp,int *Resplen);
bool QueryATR(BYTE *ATR,DWORD *ATRsize,bool reset=false);
bool CheckATR();
DWORD startServer();
void shutdown();
void init(wchar_t *section);
void signalRemoval(void);
void signalInsertion(void);
};

View File

@@ -0,0 +1,306 @@
#include "internal.h"
#include "VirtualSCReader.h"
#include "reader.h"
#include "device.h"
#include <winscard.h>
#include "memory.h"
#include <Sddl.h>
#include "sectionLocker.h"
int TcpIpReader::portBase;
TcpIpReader::TcpIpReader() {
rpcType=1;
state=SCARD_ABSENT;
}
void TcpIpReader::init(wchar_t *section) {
portBase=GetPrivateProfileInt(L"Driver",L"RPC_PORT_BASE",29500,L"BixVReader.ini");
port=GetPrivateProfileInt(section,L"TCP_PORT",portBase+(instance<<1),L"BixVReader.ini");
eventPort=GetPrivateProfileInt(section,L"TCP_EVENT_PORT",portBase+1+(instance<<1),L"BixVReader.ini");
InitializeCriticalSection(&eventSection);
InitializeCriticalSection(&dataSection);
}
bool TcpIpReader::CheckATR() {
//SectionLocker lock(dataSection);
if (AcceptSocket==NULL)
return false;
int read=0;
DWORD command=1;
if ((read=send(AcceptSocket,(char*)&command,sizeof(DWORD),NULL))<=0) {
return false;
}
DWORD size=0;
if ((read=recv(AcceptSocket,(char*)&size,sizeof(DWORD),MSG_WAITALL))<=0) {
return false;
}
if (size==0)
return false;
BYTE ATR[100];
if ((read=recv(AcceptSocket,(char*)ATR,size,MSG_WAITALL))<=0) {
return false;
}
return true;
}
bool TcpIpReader::QueryTransmit(BYTE *APDU,int APDUlen,BYTE *Resp,int *Resplen) {
//SectionLocker lock(dataSection);
if (AcceptSocket==NULL)
return false;
DWORD command=2;
DWORD read=0;
if ((read=send(AcceptSocket,(char*)&command,sizeof(DWORD),NULL))<=0) {
::shutdown(AcceptSocket,SD_BOTH);
AcceptSocket=NULL;
return false;
}
DWORD dwAPDUlen=(DWORD)APDUlen;
if ((read=send(AcceptSocket,(char*)&dwAPDUlen,sizeof(DWORD),NULL))<=0) {
::shutdown(AcceptSocket,SD_BOTH);
AcceptSocket=NULL;
return false;
}
if ((read=send(AcceptSocket,(char*)APDU,APDUlen,NULL))<=0) {
::shutdown(AcceptSocket,SD_BOTH);
AcceptSocket=NULL;
return false;
}
DWORD dwRespLen=0;
if ((read=recv(AcceptSocket,(char*)&dwRespLen,sizeof(DWORD),MSG_WAITALL))<=0) {
::shutdown(AcceptSocket,SD_BOTH);
AcceptSocket=NULL;
return false;
}
if ((read=recv(AcceptSocket,(char*)Resp,dwRespLen,MSG_WAITALL))<=0) {
::shutdown(AcceptSocket,SD_BOTH);
AcceptSocket=NULL;
return false;
}
(*Resplen)=(int)dwRespLen;
return true;
}
bool TcpIpReader::QueryATR(BYTE *ATR,DWORD *ATRsize,bool reset) {
//SectionLocker lock(dataSection);
if (AcceptSocket==NULL)
return false;
int read=0;
DWORD command=reset ? 0 : 1;
if ((read=send(AcceptSocket,(char*)&command,sizeof(DWORD),NULL))<=0) {
::shutdown(AcceptSocket,SD_BOTH);
AcceptSocket=NULL;
return false;
}
DWORD size=0;
if ((read=recv(AcceptSocket,(char*)&size,sizeof(DWORD),MSG_WAITALL))<=0) {
::shutdown(AcceptSocket,SD_BOTH);
AcceptSocket=NULL;
return false;
}
if (size==0)
return false;
if ((read=recv(AcceptSocket,(char*)ATR,size,MSG_WAITALL))<=0) {
::shutdown(AcceptSocket,SD_BOTH);
AcceptSocket=NULL;
return false;
}
(*ATRsize)=size;
return true;
}
DWORD TcpIpReader::startServer() {
breakSocket=false;
wchar_t log[300];
while (true) {
//__try {
fd_set readfds;
FD_ZERO(&readfds);
// Set server socket to set
FD_SET(socket, &readfds);
// Timeout parameter
timeval tv = { 0 };
tv.tv_sec = 5;
while(true) {
if (breakSocket)
return 0;
FD_SET(socket, &readfds);
int ret = select(0, &readfds, NULL, NULL, &tv);
if (ret > 0)
break;
if (ret<0) {
wchar_t log[100];
DWORD err=WSAGetLastError();
swprintf(log,L"[BixVReader]wsa err:%x",err);
OutputDebugString(log);
if (err==0x2736) {
socket=WSASocket(AF_INET,SOCK_STREAM,IPPROTO_TCP,NULL,0,0);
sockaddr_in Service;
Service.sin_family = AF_INET;
Service.sin_addr.s_addr = inet_addr("127.0.0.1");
Service.sin_port = htons((u_short)(port));
bind(socket, (SOCKADDR *) &Service, sizeof (Service));
listen(socket, 1);
FD_ZERO(&readfds);
// Set server socket to set
FD_SET(socket, &readfds);
}
}
}
SOCKET AcceptEventSocket;
AcceptSocket = accept(socket, NULL, NULL);
closesocket(socket);
socket=NULL;
if (AcceptSocket == INVALID_SOCKET)
return 0;
swprintf(log,L"[BixVReader]Socket connected:%i",AcceptSocket);
OutputDebugString(log);
FD_ZERO(&readfds);
// Set server socket to set
FD_SET(eventsocket, &readfds);
while(true) {
if (breakSocket)
return 0;
FD_SET(eventsocket, &readfds);
int ret = select(0, &readfds, NULL, NULL, &tv);
if (ret > 0)
break;
if (ret<0) {
DWORD err=WSAGetLastError();
swprintf(log,L"[BixVReader]wsa err:%x",err);
OutputDebugString(log);
if (err==0x2736) {
eventsocket=WSASocket(AF_INET,SOCK_STREAM,IPPROTO_TCP,NULL,0,0);
sockaddr_in eventService;
eventService.sin_family = AF_INET;
eventService.sin_addr.s_addr = inet_addr("127.0.0.1");
eventService.sin_port = htons((u_short)(eventPort));
bind(eventsocket, (SOCKADDR *) &eventService, sizeof (eventService));
listen(eventsocket, 1);
FD_ZERO(&readfds);
// Set server socket to set
FD_SET(eventsocket, &readfds);
}
}
}
AcceptEventSocket = accept(eventsocket, NULL, NULL);
closesocket(eventsocket);
eventsocket=NULL;
if (AcceptEventSocket == INVALID_SOCKET)
return 0;
swprintf(log,L"[BixVReader]Event Socket connected:%i",AcceptEventSocket);
OutputDebugString(log);
if (waitInsertIpr!=NULL) {
// if I'm waiting for card insertion, verify if there's a card present
if (initProtocols()) {
SectionLocker lock(device->m_RequestLock);
if (waitInsertIpr->UnmarkCancelable()==S_OK)
waitInsertIpr->CompleteWithInformation(STATUS_SUCCESS, 0);
waitInsertIpr=NULL;
state=SCARD_SWALLOWED;
}
}
while (true) {
// wait for a command
DWORD command=0;
int read=0;
if ((read=recv(AcceptEventSocket,(char*)&command,sizeof(DWORD),MSG_WAITALL))<=0) {
state=SCARD_ABSENT;
OutputDebugString(L"[BixVReader]Socket error");
powered=0;
::shutdown(AcceptSocket,SD_BOTH);
::shutdown(AcceptEventSocket,SD_BOTH);
if (waitRemoveIpr!=NULL) {// card inserted
OutputDebugString(L"[BixVReader]complete Wait Remove");
SectionLocker lock(device->m_RequestLock);
if (waitRemoveIpr->UnmarkCancelable()==S_OK) {
OutputDebugString(L"[BixVReader]Wait Remove Unmarked");
waitRemoveIpr->CompleteWithInformation(STATUS_SUCCESS, 0);
OutputDebugString(L"[BixVReader]Wait Remove Completed");
}
waitRemoveIpr=NULL;
}
if (waitInsertIpr!=NULL) {// card removed
OutputDebugString(L"[BixVReader]cancel Wait Remove");
SectionLocker lock(device->m_RequestLock);
if (waitInsertIpr->UnmarkCancelable()==S_OK) {
OutputDebugString(L"[BixVReader]Wait Insert Unmarked");
waitInsertIpr->CompleteWithInformation(HRESULT_FROM_WIN32(ERROR_CANCELLED), 0);
OutputDebugString(L"[BixVReader]Wait Insert Cancelled");
}
waitInsertIpr=NULL;
}
break;
}
OutputDebugString(L"[BixVReader]Socket data");
if (command==0)
powered=0;
if (command==0 && waitRemoveIpr!=NULL) {// card removed
SectionLocker lock(device->m_RequestLock);
state=SCARD_ABSENT;
if (waitRemoveIpr->UnmarkCancelable()==S_OK)
waitRemoveIpr->CompleteWithInformation(STATUS_SUCCESS, 0);
waitRemoveIpr=NULL;
}
else if (command==1 && waitInsertIpr!=NULL) {// card inserted
SectionLocker lock(device->m_RequestLock);
state=SCARD_SWALLOWED;
initProtocols();
if (waitInsertIpr->UnmarkCancelable()==S_OK)
waitInsertIpr->CompleteWithInformation(STATUS_SUCCESS, 0);
waitInsertIpr=NULL;
}
}
//}
//__except(EXCEPTION_EXECUTE_HANDLER) {
// wchar_t log[300];
// DWORD err=GetExceptionCode();
// swprintf(log,L"Exception:%08X",err);
// OutputDebugString(log);
//}
}
OutputDebugString(L"[BixVReader]Socket quit!!!");
return 0;
}
void TcpIpReader::shutdown() {
state=SCARD_ABSENT;
breakSocket=true;
{
//SectionLocker lock(dataSection);
closesocket(eventsocket);
closesocket(socket);
}
WaitForSingleObject(serverThread,10000);
serverThread=NULL;
eventsocket=NULL;
socket=NULL;
if (waitRemoveIpr!=NULL) {
SectionLocker lock(device->m_RequestLock);
if (waitRemoveIpr->UnmarkCancelable()==S_OK)
waitRemoveIpr->Complete(HRESULT_FROM_WIN32(ERROR_CANCELLED));
waitRemoveIpr=NULL;
}
if (waitInsertIpr!=NULL) {
SectionLocker lock(device->m_RequestLock);
if (waitInsertIpr->UnmarkCancelable()==S_OK)
waitInsertIpr->Complete(HRESULT_FROM_WIN32(ERROR_CANCELLED));
waitInsertIpr=NULL;
}
}

View File

@@ -0,0 +1,20 @@
import "wudfddi.idl";
[
uuid(8B9C88E9-7E63-46c7-832B-D48D0290E4BF),
version(1.0),
helpstring("VirtualSCReader Driver Type Library")
]
library VirtualSCReaderDriverLib
{
importlib("stdole2.tlb");
[
uuid(d4112073-d09b-458f-a5aa-35ef21eef5de),
//uuid(10654262-B9BD-4636-8890-F66362CCC207),
helpstring("VirtualSCReader Class")
]
coclass VirtualSCReaderDriver
{
[default] interface IDriverEntry;
};
};

View File

@@ -0,0 +1,147 @@
#include "internal.h"
#include "VirtualSCReader.h"
#include "reader.h"
#include "device.h"
#include <winscard.h>
#include "memory.h"
#include <Sddl.h>
#include "sectionLocker.h"
#include "../../src/vpcd/vpcd.h"
int VpcdReader::portBase;
VpcdReader::VpcdReader() {
rpcType=2;
state=SCARD_ABSENT;
cardPresent = false;
}
void VpcdReader::init(wchar_t *section) {
portBase=GetPrivateProfileInt(L"Driver",L"RPC_PORT_BASE",VPCDPORT,L"BixVReader.ini");
port=(short) GetPrivateProfileInt(section,L"TCP_PORT",portBase+instance,L"BixVReader.ini");
}
bool VpcdReader::CheckATR() {
bool r = false;
struct vicc_ctx *vicc_ctx = (struct vicc_ctx *) ctx;
if (vicc_present((struct vicc_ctx *) ctx) == 1) {
signalInsertion();
r = true;
} else {
signalRemoval();
}
return r;
}
bool VpcdReader::QueryTransmit(BYTE *APDU,int APDUlen,BYTE *Resp,int *Resplen) {
unsigned char *rapdu = NULL;
ssize_t rapdu_len;
bool r = false;
if (APDU && APDUlen && Resp && Resplen) {
rapdu_len = vicc_transmit((struct vicc_ctx *) ctx, APDUlen, APDU, &rapdu);
if (rapdu_len > 0) {
memcpy(Resp, rapdu, rapdu_len);
*Resplen = rapdu_len;
free(rapdu);
r = true;
} else {
signalRemoval();
}
}
return r;
}
bool VpcdReader::QueryATR(BYTE *ATR,DWORD *ATRsize,bool reset) {
unsigned char *atr = NULL;
int atr_len;
bool r = false;
if (ATR && ATRsize) {
atr_len = vicc_getatr((struct vicc_ctx *) ctx, &atr);
if (atr_len > 0) {
/* TODO do length checking on length of ATR when ATRsize is
* correctly initialized by Reader.cpp */
memcpy(ATR, atr, atr_len);
*ATRsize = atr_len;
free(atr);
r = true;
if (reset)
vicc_reset((struct vicc_ctx *) ctx);
} else {
signalRemoval();
}
}
return r;
}
DWORD VpcdReader::startServer() {
breakSocket = false;
ctx = vicc_init(NULL, port);
while (!breakSocket) {
CheckATR();
Sleep(1000);
}
return 0;
}
void VpcdReader::shutdown() {
breakSocket=true;
WaitForSingleObject(serverThread,10000);
serverThread=NULL;
vicc_exit((struct vicc_ctx *) ctx);
state=SCARD_ABSENT;
ctx = NULL;
if (waitRemoveIpr!=NULL) {
SectionLocker lock(device->m_RequestLock);
if (waitRemoveIpr->UnmarkCancelable()==S_OK)
waitRemoveIpr->Complete(HRESULT_FROM_WIN32(ERROR_CANCELLED));
waitRemoveIpr=NULL;
}
if (waitInsertIpr!=NULL) {
SectionLocker lock(device->m_RequestLock);
if (waitInsertIpr->UnmarkCancelable()==S_OK)
waitInsertIpr->Complete(HRESULT_FROM_WIN32(ERROR_CANCELLED));
waitInsertIpr=NULL;
}
}
void VpcdReader::signalRemoval(void) {
if (cardPresent) {
cardPresent = false;
state=SCARD_ABSENT;
if (waitRemoveIpr!=NULL) {
SectionLocker lock(device->m_RequestLock);
if (waitRemoveIpr->UnmarkCancelable()==S_OK) {
waitRemoveIpr->CompleteWithInformation(STATUS_SUCCESS, 0);
}
waitRemoveIpr=NULL;
}
if (waitInsertIpr!=NULL) {
SectionLocker lock(device->m_RequestLock);
if (waitInsertIpr->UnmarkCancelable()==S_OK) {
waitInsertIpr->CompleteWithInformation(HRESULT_FROM_WIN32(ERROR_CANCELLED), 0);
}
waitInsertIpr=NULL;
}
}
}
void VpcdReader::signalInsertion(void) {
if (!cardPresent) {
cardPresent = true;
if (waitInsertIpr!=NULL) {
if (initProtocols()) {
SectionLocker lock(device->m_RequestLock);
if (waitInsertIpr->UnmarkCancelable()==S_OK)
waitInsertIpr->CompleteWithInformation(STATUS_SUCCESS, 0);
waitInsertIpr=NULL;
state=SCARD_SWALLOWED;
}
}
}
}

View File

@@ -0,0 +1,80 @@
;
; VirtualCardReader.inf
;
[Version]
Signature="$Windows NT$"
Class=SmartCardReader
ClassGuid={50dd5230-ba8a-11d1-bf5d-0000f805f530}
Provider=%MSFTUMDF%
CatalogFile=WUDF.cat
DriverVer=11/22/2010,6.1.7600.16385
[Manufacturer]
%MSFTUMDF%=Microsoft,NT$ARCH$
[Microsoft.NT$ARCH$]
%ReaderDeviceName%=VReader_Install,root\BixVirtualReader
; =================== Virtual Reader Device ==================================
[VReader_Install.NT]
CopyFiles=UMDriverCopy
[VReader_Install.NT.hw]
[VReader_Install.NT.Services]
AddService=WUDFRd,0x000001fa,WUDFRD_ServiceInstall
[VReader_Install.NT.CoInstallers]
AddReg=CoInstallers_AddReg
CopyFiles=CoInstallers_CopyFiles
[VReader_Install.NT.Wdf]
UmdfService=BixVReader,UMDFVReader_Install
UmdfServiceOrder=BixVReader
UmdfKernelModeClientPolicy=AllowKernelModeClients
; =================== UMDF Service Install section ===================
[UMDFVReader_Install]
UmdfLibraryVersion=$UMDFVERSION$
ServiceBinary=%12%\UMDF\BixVReader.dll
DriverCLSID={d4112073-d09b-458f-a5aa-35ef21eef5de}
[WUDFRD_ServiceInstall]
DisplayName = %WudfRdDisplayName%
ServiceType = 1
StartType = 3
ErrorControl = 1
ServiceBinary = %12%\WUDFRd.sys
LoadOrderGroup = Base
[SourceDisksFiles]
BixVReader.dll=1
WudfUpdate_01009.dll=1
[SourceDisksNames]
1 = %MediaDescription%
[CoInstallers_CopyFiles]
WudfUpdate_$UMDFCOINSTALLERVERSION$.dll
[CoInstallers_AddReg]
HKR,,CoInstallers32,0x00010000,"WUDFUpdate_$UMDFCOINSTALLERVERSION$.dll"
[DestinationDirs]
UMDriverCopy=12,UMDF ; copy to driversMdf
CoInstallers_CopyFiles=11
[UMDriverCopy]
BixVReader.dll
; =================== Generic ==================================
[Strings]
MSFTUMDF="Fabio Ottavi - developer"
MediaDescription="Bix Virtual Card Reader disk 1"
WudfRdDisplayName="Bix UMDF Virtual Smart Card Reader for ScardVisual"
ReaderDeviceName="Bix Virtual Smart Card Reader"

View File

@@ -0,0 +1,7 @@
#
# Build using BUILD.EXE (Do not edit this section of this file, edit SOURCES)
#
MINIMUM_NT_TARGET_VERSION=0x501
!INCLUDE $(NTMAKEENV)\makefile.def

View File

@@ -0,0 +1,8 @@
.SUFFIXES: .inx
STAMP=stampinf
.inx{$(OBJ_PATH)\$(O)}.inf:
copy $(@B).inx $@
$(STAMP) -f $@ -a $(_BUILDARCH) -k $(KMDF_VERSION_MAJOR).$(KMDF_VERSION_MINOR) -u $(UMDF_VERSION_MAJOR).$(UMDF_VERSION_MINOR).0

View File

@@ -0,0 +1,79 @@
#
# UMDF_VERSION_MAJOR controls the headers that the driver uses.
# UMDF_VERSION_MAJOR + UMDF_VERSION_MINOR control which version
# of UMDF the driver is bound to in the INF and which
# update coinstaller it requires (through stampinf).
#
UMDF_VERSION_MAJOR=1
UMDF_VERSION_MINOR=9
KMDF_VERSION_MAJOR=1
KMDF_VERSION_MINOR=9
TARGETNAME=BixVReader
TARGETTYPE=DYNLINK
USE_ATL = 1
USE_STL = 1
ATL_VER = 71
USE_MSVCRT = 1
USE_NATIVE_EH = 1
C_DEFINES = $(C_DEFINES) /D_UNICODE /DUNICODE
WIN32_WINNT_VERSION=$(LATEST_WIN32_WINNT_VERSION)
_NT_TARGET_VERSION=$(_NT_TARGET_VERSION_WIN7)
NTDDI_VERSION=$(LATEST_NTDDI_VERSION)
DLLENTRY=_DllMainCRTStartup
DLLDEF=..\exports.def
SOURCES=\
..\BixVReader.rc \
..\driver.cpp \
..\device.cpp \
..\reader.cpp \
..\pipereader.cpp \
..\tcpipreader.cpp \
..\..\..\src\vpcd\vpcd.c \
..\vpcdreader.cpp \
..\queue.cpp \
..\memory.cpp \
..\sectionLocker.cpp \
..\DllMain.cpp \
..\VirtualSCReader.idl
INCLUDES= $(INCLUDES); \
$(SDK_INC_PATH); \
$(DDK_INC_PATH); \
$(UMDF_INC_PATH)\10;
MSC_WARNING_LEVEL=/W4 /WX
TARGETLIBS=\
$(SDK_LIB_PATH)\strsafe.lib \
$(SDK_LIB_PATH)\kernel32.lib \
$(SDK_LIB_PATH)\ole32.lib \
$(SDK_LIB_PATH)\oleaut32.lib \
$(SDK_LIB_PATH)\uuid.lib \
$(SDK_LIB_PATH)\user32.lib \
$(SDK_LIB_PATH)\advapi32.lib \
$(SDK_LIB_PATH)\shlwapi.lib \
$(SDK_LIB_PATH)\Ws2_32.lib \
NTTARGETFILE1=$(OBJ_PATH)\$(O)\BixVReader.inf
#
# This sets up the WPP preprocessor and tells it to scan internal.h to find
# the trace function definition that's in there.
#
#RUN_WPP= $(SOURCES) -dll -scan:internal.h
TARGET_DESTINATION=wudf

View File

@@ -0,0 +1,80 @@
;
; VirtualCardReader.inf
;
[Version]
Signature="$Windows NT$"
Class=SmartCardReader
ClassGuid={50dd5230-ba8a-11d1-bf5d-0000f805f530}
Provider=%MSFTUMDF%
CatalogFile=WUDF.cat
DriverVer=11/22/2010,6.1.7600.16385
[Manufacturer]
%MSFTUMDF%=Microsoft,NT$ARCH$
[Microsoft.NT$ARCH$]
%ReaderDeviceName%=VReader_Install,root\BixVirtualReader
; =================== Virtual Reader Device ==================================
[VReader_Install.NT]
CopyFiles=UMDriverCopy
[VReader_Install.NT.hw]
[VReader_Install.NT.Services]
AddService=WUDFRd,0x000001fa,WUDFRD_ServiceInstall
[VReader_Install.NT.CoInstallers]
AddReg=CoInstallers_AddReg
CopyFiles=CoInstallers_CopyFiles
[VReader_Install.NT.Wdf]
UmdfService=BixVReader,UMDFVReader_Install
UmdfServiceOrder=BixVReader
UmdfKernelModeClientPolicy=AllowKernelModeClients
; =================== UMDF Service Install section ===================
[UMDFVReader_Install]
UmdfLibraryVersion=$UMDFVERSION$
ServiceBinary=%12%\UMDF\BixVReader.dll
DriverCLSID={d4112073-d09b-458f-a5aa-35ef21eef5de}
[WUDFRD_ServiceInstall]
DisplayName = %WudfRdDisplayName%
ServiceType = 1
StartType = 3
ErrorControl = 1
ServiceBinary = %12%\WUDFRd.sys
LoadOrderGroup = Base
[SourceDisksFiles]
BixVReader.dll=1
WudfUpdate_01009.dll=1
[SourceDisksNames]
1 = %MediaDescription%
[CoInstallers_CopyFiles]
WudfUpdate_$UMDFCOINSTALLERVERSION$.dll
[CoInstallers_AddReg]
HKR,,CoInstallers32,0x00010000,"WUDFUpdate_$UMDFCOINSTALLERVERSION$.dll"
[DestinationDirs]
UMDriverCopy=12,UMDF ; copy to driversMdf
CoInstallers_CopyFiles=11
[UMDriverCopy]
BixVReader.dll
; =================== Generic ==================================
[Strings]
MSFTUMDF="Fabio Ottavi - developer"
MediaDescription="Bix Virtual Card Reader disk 1"
WudfRdDisplayName="Bix UMDF Virtual Smart Card Reader for ScardVisual"
ReaderDeviceName="Bix Virtual Smart Card Reader"

View File

@@ -0,0 +1,7 @@
#
# Build using BUILD.EXE (Do not edit this section of this file, edit SOURCES)
#
MINIMUM_NT_TARGET_VERSION=0x501
!INCLUDE $(NTMAKEENV)\makefile.def

View File

@@ -0,0 +1,8 @@
.SUFFIXES: .inx
STAMP=stampinf
.inx{$(OBJ_PATH)\$(O)}.inf:
copy $(@B).inx $@
$(STAMP) -f $@ -a $(_BUILDARCH) -k $(KMDF_VERSION_MAJOR).$(KMDF_VERSION_MINOR) -u $(UMDF_VERSION_MAJOR).$(UMDF_VERSION_MINOR).0

View File

@@ -0,0 +1,79 @@
#
# UMDF_VERSION_MAJOR controls the headers that the driver uses.
# UMDF_VERSION_MAJOR + UMDF_VERSION_MINOR control which version
# of UMDF the driver is bound to in the INF and which
# update coinstaller it requires (through stampinf).
#
UMDF_VERSION_MAJOR=1
UMDF_VERSION_MINOR=9
KMDF_VERSION_MAJOR=1
KMDF_VERSION_MINOR=9
TARGETNAME=BixVReader
TARGETTYPE=DYNLINK
USE_ATL = 1
USE_STL = 1
ATL_VER = 71
USE_MSVCRT = 1
USE_NATIVE_EH = 1
C_DEFINES = $(C_DEFINES) /D_UNICODE /DUNICODE
WIN32_WINNT_VERSION=$(LATEST_WIN32_WINNT_VERSION)
_NT_TARGET_VERSION=$(_NT_TARGET_VERSION_WINXP)
NTDDI_VERSION=$(LATEST_NTDDI_VERSION)
DLLENTRY=_DllMainCRTStartup
DLLDEF=..\exports.def
SOURCES=\
..\BixVReader.rc \
..\driver.cpp \
..\device.cpp \
..\reader.cpp \
..\pipereader.cpp \
..\tcpipreader.cpp \
..\..\..\src\vpcd\vpcd.c \
..\vpcdreader.cpp \
..\queue.cpp \
..\memory.cpp \
..\sectionLocker.cpp \
..\DllMain.cpp \
..\VirtualSCReader.idl
INCLUDES= $(INCLUDES); \
$(SDK_INC_PATH); \
$(DDK_INC_PATH); \
$(UMDF_INC_PATH)\10;
MSC_WARNING_LEVEL=/W4 /WX
TARGETLIBS=\
$(SDK_LIB_PATH)\strsafe.lib \
$(SDK_LIB_PATH)\kernel32.lib \
$(SDK_LIB_PATH)\ole32.lib \
$(SDK_LIB_PATH)\oleaut32.lib \
$(SDK_LIB_PATH)\uuid.lib \
$(SDK_LIB_PATH)\user32.lib \
$(SDK_LIB_PATH)\advapi32.lib \
$(SDK_LIB_PATH)\shlwapi.lib \
$(SDK_LIB_PATH)\Ws2_32.lib \
NTTARGETFILE1=$(OBJ_PATH)\$(O)\BixVReader.inf
#
# This sets up the WPP preprocessor and tells it to scan internal.h to find
# the trace function definition that's in there.
#
#RUN_WPP= $(SOURCES) -dll -scan:internal.h
TARGET_DESTINATION=wudf

View File

@@ -0,0 +1,331 @@
#include "internal.h"
#include "VirtualSCReader.h"
#include "queue.h"
#include "device.h"
#include "driver.h"
#include <stdio.h>
#include <winscard.h>
#include "memory.h"
#include "SectionLocker.h"
HRESULT
CMyDevice::CreateInstance(
__in IWDFDriver *FxDriver,
__in IWDFDeviceInitialize * FxDeviceInit
)
/*++
Routine Description:
This method creates and initializs an instance of the virtual smart card reader driver's
device callback object.
Arguments:
FxDeviceInit - the settings for the device.
Device - a location to store the referenced pointer to the device object.
Return Value:
Status
--*/
{
inFunc
SectionLogger a(__FUNCTION__);
CComObject<CMyDevice>* device = NULL;
HRESULT hr;
OutputDebugString(L"[BixVReader]CreateInstance"); //
// Allocate a new instance of the device class.
//
hr = CComObject<CMyDevice>::CreateInstance(&device);
if (device==NULL)
{
return E_OUTOFMEMORY;
}
//
// Initialize the instance.
//
device->AddRef();
OutputDebugString(L"[BixVReader]SetLockingConstraint"); //
FxDeviceInit->SetLockingConstraint(WdfDeviceLevel);
CComPtr<IUnknown> spCallback;
OutputDebugString(L"[BixVReader]QueryInterface"); //
hr = device->QueryInterface(IID_IUnknown, (void**)&spCallback);
CComPtr<IWDFDevice> spIWDFDevice;
if (SUCCEEDED(hr))
{
OutputDebugString(L"[BixVReader]CreateDevice"); //
hr = FxDriver->CreateDevice(FxDeviceInit, spCallback, &spIWDFDevice);
}
device->numInstances=GetPrivateProfileInt(L"Driver",L"NumReaders",1,L"BixVReader.ini");
WSADATA wsaData;
int iResult = WSAStartup(MAKEWORD(2, 2), &wsaData);
if (iResult != NO_ERROR) {
OutputDebugString(L"[BixVReader]Error at WSAStartup()\n");
}
for (int i=0;i<device->numInstances;i++) {
OutputDebugString(L"[BixVReader]CreateDeviceInterface"); //
wchar_t name[10];
swprintf(name,L"DEV%i",i);
if (spIWDFDevice->CreateDeviceInterface(&SmartCardReaderGuid,name)!=0)
OutputDebugString(L"[BixVReader]CreateDeviceInterface Failed");
}
SAFE_RELEASE(device);
return hr;
}
void CMyDevice::ProcessIoControl(__in IWDFIoQueue* pQueue,
__in IWDFIoRequest* pRequest,
__in ULONG ControlCode,
SIZE_T inBufSize,
SIZE_T outBufSize)
{
inFunc
SectionLogger a(__FUNCTION__);
UNREFERENCED_PARAMETER(pQueue);
wchar_t log[300];
swprintf(log,L"[BixVReader][IOCT]IOCTL %08X - In %i Out %i",ControlCode,inBufSize,outBufSize);
OutputDebugString(log);
//SectionLocker lock(m_RequestLock);
int instance=0;
{
CComPtr<IWDFFile> pFileObject;
pRequest->GetFileObject(&pFileObject);
if (pFileObject != NULL)
{
DWORD logLen=300;
pFileObject->RetrieveFileName(log,&logLen);
instance=_wtoi(log+(logLen-2));
}
}
Reader &reader=*readers[instance];
if (ControlCode==IOCTL_SMARTCARD_GET_ATTRIBUTE) {
reader.IoSmartCardGetAttribute(pRequest,inBufSize,outBufSize);
return;
}
else if (ControlCode==IOCTL_SMARTCARD_IS_PRESENT) {
reader.IoSmartCardIsPresent(pRequest,inBufSize,outBufSize);
return;
}
else if (ControlCode==IOCTL_SMARTCARD_GET_STATE) {
reader.IoSmartCardGetState(pRequest,inBufSize,outBufSize);
return;
}
else if (ControlCode==IOCTL_SMARTCARD_IS_ABSENT) {
reader.IoSmartCardIsAbsent(pRequest,inBufSize,outBufSize);
return;
}
else if (ControlCode==IOCTL_SMARTCARD_POWER) {
reader.IoSmartCardPower(pRequest,inBufSize,outBufSize);
return;
}
else if (ControlCode==IOCTL_SMARTCARD_SET_ATTRIBUTE) {
reader.IoSmartCardSetAttribute(pRequest,inBufSize,outBufSize);
return;
}
else if (ControlCode==IOCTL_SMARTCARD_SET_PROTOCOL) {
reader.IoSmartCardSetProtocol(pRequest,inBufSize,outBufSize);
return;
}
else if (ControlCode==IOCTL_SMARTCARD_TRANSMIT) {
reader.IoSmartCardTransmit(pRequest,inBufSize,outBufSize);
return;
}
swprintf(log,L"[BixVReader][IOCT]ERROR_NOT_SUPPORTED:%08X",ControlCode);
OutputDebugString(log);
pRequest->CompleteWithInformation(HRESULT_FROM_WIN32(ERROR_NOT_SUPPORTED), 0);
return;
}
DWORD WINAPI ServerFunc(Reader *reader) {
return reader->startServer();
}
/////////////////////////////////////////////////////////////////////////
//
// CMyDevice::OnPrepareHardware
//
// Called by UMDF to prepare the hardware for use.
//
// Parameters:
// pWdfDevice - pointer to an IWDFDevice object representing the
// device
//
// Return Values:
// S_OK: success
//
/////////////////////////////////////////////////////////////////////////
HRESULT CMyDevice::OnPrepareHardware(
__in IWDFDevice* pWdfDevice
)
{
inFunc
SectionLogger a(__FUNCTION__);
// Store the IWDFDevice pointer
m_pWdfDevice = pWdfDevice;
// Configure the default IO Queue
HRESULT hr = CMyQueue::CreateInstance(m_pWdfDevice, this);
return hr;
}
/////////////////////////////////////////////////////////////////////////
//
// CMyDevice::OnReleaseHardware
//
// Called by WUDF to uninitialize the hardware.
//
// Parameters:
// pWdfDevice - pointer to an IWDFDevice object for the device
//
// Return Values:
// S_OK:
//
/////////////////////////////////////////////////////////////////////////
HRESULT CMyDevice::OnReleaseHardware(
__in IWDFDevice* pWdfDevice
)
{
inFunc
SectionLogger a(__FUNCTION__);
UNREFERENCED_PARAMETER(pWdfDevice);
// Release the IWDFDevice handle, if it matches
if (pWdfDevice == m_pWdfDevice.p)
{
m_pWdfDevice.Release();
}
return S_OK;
}
HRESULT CMyDevice::OnD0Entry(IN IWDFDevice* pWdfDevice,IN WDF_POWER_DEVICE_STATE previousState) {
SectionLogger a(__FUNCTION__);
UNREFERENCED_PARAMETER(pWdfDevice);
UNREFERENCED_PARAMETER(previousState);
numInstances=GetPrivateProfileInt(L"Driver",L"NumReaders",1,L"BixVReader.ini");
readers.resize(numInstances);
for (int i=0;i<numInstances;i++) {
wchar_t section[300];
char sectionA[300];
swprintf(section,L"Reader%i",i);
sprintf(sectionA,"Reader%i",i);
int rpcType=GetPrivateProfileInt(section,L"RPC_TYPE",0,L"BixVReader.ini");
if (rpcType==0)
readers[i]=new PipeReader();
else if (rpcType==1)
readers[i]=new TcpIpReader();
else if (rpcType==2)
readers[i]=new VpcdReader();
readers[i]->instance=i;
readers[i]->device=this;
GetPrivateProfileStringA(sectionA,"VENDOR_NAME","Bix",readers[i]->vendorName,sizeof readers[i]->vendorName,"BixVReader.ini");
GetPrivateProfileStringA(sectionA,"VENDOR_IFD_TYPE","VIRTUAL_CARD_READER",readers[i]->vendorIfdType,sizeof readers[i]->vendorIfdType,"BixVReader.ini");
readers[i]->deviceUnit=GetPrivateProfileInt(section,L"DEVICE_UNIT",i,L"BixVReader.ini");
readers[i]->protocol=0;
readers[i]->init(section);
DWORD pipeThreadID;
readers[i]->serverThread=CreateThread(NULL,0,(LPTHREAD_START_ROUTINE)ServerFunc,readers[i],0,&pipeThreadID);
}
return S_OK;
}
HRESULT CMyDevice::OnD0Exit(IN IWDFDevice* pWdfDevice,IN WDF_POWER_DEVICE_STATE newState) {
SectionLogger a(__FUNCTION__);
UNREFERENCED_PARAMETER(pWdfDevice);
UNREFERENCED_PARAMETER(newState);
// dovrei fermare tutti i thread in ascolto dei lettori
shutDown();
return S_OK;
}
void CMyDevice::shutDown() {
SectionLogger a(__FUNCTION__);
for (int i=0;i<numInstances;i++) {
readers[i]->shutdown();
delete readers[i];
}
numInstances=0;
}
HRESULT CMyDevice::OnQueryRemove(IN IWDFDevice* pWdfDevice) {
SectionLogger a(__FUNCTION__);
UNREFERENCED_PARAMETER(pWdfDevice);
OutputDebugString(L"[BixVReader]OnQueryRemove");
shutDown();
return S_OK;
}
HRESULT CMyDevice::OnQueryStop(IN IWDFDevice* pWdfDevice) {
SectionLogger a(__FUNCTION__);
UNREFERENCED_PARAMETER(pWdfDevice);
OutputDebugString(L"[BixVReader]OnQueryStop");
shutDown();
return S_OK;
}
void CMyDevice::OnSurpriseRemoval(IN IWDFDevice* pWdfDevice) {
SectionLogger a(__FUNCTION__);
UNREFERENCED_PARAMETER(pWdfDevice);
OutputDebugString(L"[BixVReader]OnSurpriseRemoval");
shutDown();
}
STDMETHODIMP_ (void) CMyDevice::OnCancel(IN IWDFIoRequest* pWdfRequest) {
SectionLogger a(__FUNCTION__);
wchar_t log[300];
SectionLocker lock(m_RequestLock);
int instance=0;
{
CComPtr<IWDFFile> pFileObject;
pWdfRequest->GetFileObject(&pFileObject);
if (pFileObject != NULL)
{
DWORD logLen=300;
pFileObject->RetrieveFileName(log,&logLen);
instance=_wtoi(log+(logLen-2));
}
}
Reader &reader=*readers[instance];
if (pWdfRequest==reader.waitRemoveIpr) {
OutputDebugString(L"[BixVReader]Cancel Remove");
reader.waitRemoveIpr=NULL;
}
else if (pWdfRequest==reader.waitInsertIpr) {
OutputDebugString(L"[BixVReader]Cancel Insert");
reader.waitInsertIpr=NULL;
}
pWdfRequest->CompleteWithInformation(HRESULT_FROM_WIN32(ERROR_CANCELLED), 0);
}

View File

@@ -0,0 +1,98 @@
#pragma once
#include <vector>
#include "reader.h"
//
// Class for the iotrace driver.
//
class CMyDevice;
class ATL_NO_VTABLE CMyDevice :
public CComObjectRootEx<CComMultiThreadModel>,
public IPnpCallbackHardware,
public IPnpCallback,
public IRequestCallbackCancel
{
public:
~CMyDevice() {
DeleteCriticalSection(&m_RequestLock);
}
DECLARE_NOT_AGGREGATABLE(CMyDevice)
BEGIN_COM_MAP(CMyDevice)
COM_INTERFACE_ENTRY(IPnpCallbackHardware)
COM_INTERFACE_ENTRY(IPnpCallback)
COM_INTERFACE_ENTRY(IRequestCallbackCancel)
END_COM_MAP()
CMyDevice(VOID)
{
InitializeCriticalSection(&m_RequestLock);
m_pWdfDevice = NULL;
}
//
// Private data members.
//
private:
CComPtr<IWDFDevice> m_pWdfDevice;
std::vector<Reader*> readers;
int numInstances;
//
// Private methods.
//
private:
HRESULT ConfigureQueue();
//
// Public methods
//
public:
CRITICAL_SECTION m_RequestLock;
//
// The factory method used to create an instance of this driver.
//
static
HRESULT CreateInstance(
__in IWDFDriver *FxDriver,
__in IWDFDeviceInitialize *FxDeviceInit
);
//
// COM methods
//
public:
//
// IUnknown methods.
//
void ProcessIoControl(__in IWDFIoQueue* pQueue,
__in IWDFIoRequest* pRequest,
__in ULONG ControlCode,
SIZE_T InputBufferSizeInBytes,
SIZE_T OutputBufferSizeInBytes);
STDMETHOD_ (HRESULT, OnPrepareHardware)(__in IWDFDevice* pWdfDevice);
STDMETHOD_ (HRESULT, OnReleaseHardware)(__in IWDFDevice* pWdfDevice);
STDMETHOD_ (HRESULT, OnD0Entry)(IN IWDFDevice* pWdfDevice,IN WDF_POWER_DEVICE_STATE previousState);
STDMETHOD_ (HRESULT, OnD0Exit )(IN IWDFDevice* pWdfDevice,IN WDF_POWER_DEVICE_STATE newState);
STDMETHOD_ (HRESULT, OnQueryRemove )(IN IWDFDevice* pWdfDevice);
STDMETHOD_ (HRESULT, OnQueryStop)(IN IWDFDevice* pWdfDevice);
STDMETHOD_ (void, OnSurpriseRemoval)(IN IWDFDevice* pWdfDevice);
STDMETHOD_ (void, OnCancel)(IN IWDFIoRequest* pWdfRequest);
void shutDown();
};

View File

@@ -0,0 +1,75 @@
#include "internal.h"
#include "VirtualSCReader.h"
#include "driver.h"
#include "device.h"
#include <stdio.h>
#include "SectionLocker.h"
CMyDriver::CMyDriver()
{
inFunc
}
/////////////////////////////////////////////////////////////////////////
//
// CMyDriver::OnDeviceAdd
//
// The framework call this function when device is detected. This driver
// creates a device callback object
//
// Parameters:
// pDriver - pointer to an IWDFDriver object
// pDeviceInit - pointer to a device initialization object
//
// Return Values:
// S_OK: device initialized successfully
//
/////////////////////////////////////////////////////////////////////////
HRESULT CMyDriver::OnDeviceAdd(
__in IWDFDriver* pDriver,
__in IWDFDeviceInitialize* pDeviceInit
)
{
inFunc
SectionLogger a(__FUNCTION__);
HRESULT hr = CMyDevice::CreateInstance(pDriver, pDeviceInit);
return hr;
}
/////////////////////////////////////////////////////////////////////////
//
// CMyDriver::OnInitialize
//
// The framework calls this function just after loading the driver. The driver
// can perform any global, device independent intialization in this routine.
//
/////////////////////////////////////////////////////////////////////////
HRESULT CMyDriver::OnInitialize(
__in IWDFDriver* pDriver
)
{
inFunc
SectionLogger a(__FUNCTION__);
UNREFERENCED_PARAMETER(pDriver);
return S_OK;
}
/////////////////////////////////////////////////////////////////////////
//
// CMyDriver::OnDeinitialize
//
// The framework calls this function just before de-initializing itself. All
// WDF framework resources should be released by driver before returning
// from this call.
//
/////////////////////////////////////////////////////////////////////////
void CMyDriver::OnDeinitialize(
__in IWDFDriver* pDriver
)
{
inFunc
SectionLogger a(__FUNCTION__);
UNREFERENCED_PARAMETER(pDriver);
return;
}

View File

@@ -0,0 +1,37 @@
#pragma once
//
// This class handles driver events for the skeleton sample. In particular
// it supports the OnDeviceAdd event, which occurs when the driver is called
// to setup per-device handlers for a new device stack.
//
class ATL_NO_VTABLE CMyDriver :
public CComObjectRootEx<CComMultiThreadModel>,
public CComCoClass<CMyDriver, &CLSID_VirtualSCReaderDriver>,
public IDriverEntry
{
public:
CMyDriver();
DECLARE_NO_REGISTRY()
DECLARE_CLASSFACTORY()
DECLARE_NOT_AGGREGATABLE(CMyDriver)
BEGIN_COM_MAP(CMyDriver)
COM_INTERFACE_ENTRY(IDriverEntry)
END_COM_MAP()
public:
//
// IDriverEntry
//
STDMETHOD (OnInitialize)(__in IWDFDriver* pDriver);
STDMETHOD (OnDeviceAdd)(__in IWDFDriver* pDriver, __in IWDFDeviceInitialize* pDeviceInit);
STDMETHOD_ (void, OnDeinitialize)(__in IWDFDriver* pDriver);
};
OBJECT_ENTRY_AUTO(__uuidof(VirtualSCReaderDriver), CMyDriver)

View File

@@ -0,0 +1,15 @@
; Skeleton.def : Declares the module parameters.
;
; TODO: Change the library name here to match your binary name.
;
LIBRARY "UMDFSkeleton.DLL"
EXPORTS
DllCanUnloadNow PRIVATE
DllGetClassObject PRIVATE
DllRegisterServer PRIVATE
DllUnregisterServer PRIVATE

View File

@@ -0,0 +1,75 @@
#pragma once
#include <windows.h>
#include <atlbase.h>
#include <atlcom.h>
#include <atlstr.h>
//__user_driver; // Macro letting the compiler know this is not a kernel driver (this will help surpress needless warnings)
// Common WPD and WUDF headers
#ifndef ARRAY_SIZE
#define ARRAY_SIZE(x) (sizeof(x) / sizeof(x[0]))
#endif
#ifndef SAFE_RELEASE
#define SAFE_RELEASE(p) {if ((p)) { (p)->Release(); (p) = NULL; }}
#endif
#define STATUS_SUCCESS ((NTSTATUS)0x00000000L) // ntsubauth
#define STATUS_NO_MEDIA ((NTSTATUS)0xC0000178L)
#define STATUS_INVALID_DEVICE_STATE ((NTSTATUS)0xC0000184L)
//
// Include the WUDF DDI
//
#include <devioctl.h>
#include <initguid.h>
#include <propkeydef.h>
#include <propvarutil.h>
#include "PortableDeviceTypes.h"
#include "PortableDeviceClassExtension.h"
#include "PortableDevice.h"
#include "wudfddi.h"
//
// Use specstrings for in/out annotation of function parameters.
//
#include "specstrings.h"
//
// Forward definitions of classes in the other header files.
//
typedef class CMyDriver *PCMyDriver;
typedef class CMyDevice *PCMyDevice;
//
DEFINE_GUID(SmartCardReaderGuid, 0x50DD5230, 0xBA8A, 0x11D1, 0xBF,0x5D,0x00,0x00,0xF8,0x05,0xF5,0x30);
//
// Include the type specific headers.
//
class funcTrace {
public:
wchar_t funcN[500];
funcTrace (char *func) {
wchar_t funcName[500];
wsprintf(funcN,L"%S",func);
wsprintf(funcName,L"[BixVReader]IN -> %s",funcN);
OutputDebugString(funcName);
}
~funcTrace () {
wchar_t funcName[500];
wsprintf(funcName,L"[BixVReader]OUT -> %s",funcN);
OutputDebugString(funcName);
}
};
//#define inFunc funcTrace _ftrace(__FUNCTION__);
#define inFunc

View File

@@ -0,0 +1,83 @@
#include "memory.h"
#include "SectionLocker.h"
bool getBuffer(IWDFIoRequest* pRequest,BYTE *buffer,int *bufferLen) {
IWDFMemory *inmem=NULL;
pRequest->GetInputMemory(&inmem);
if (inmem==NULL) {
OutputDebugString(L"GetInputMemory failed");
return false;
}
else {
SIZE_T size;
void *data=inmem->GetDataBuffer(&size);
memcpy(buffer,data,size);
(*bufferLen)=(int)size;
inmem->Release();
return true;
}
}
void setBuffer(CMyDevice *device,IWDFIoRequest* pRequest,BYTE *result,int inSize) {
IWDFMemory *outmem=NULL;
pRequest->GetOutputMemory (&outmem);
if (outmem==NULL) {
SectionLocker lock(device->m_RequestLock);
OutputDebugString(L"GetOutputMemory failed");
pRequest->Complete(HRESULT_FROM_WIN32(ERROR_GEN_FAILURE));
}
else {
SectionLocker lock(device->m_RequestLock);
outmem->CopyFromBuffer(0,result,inSize);
outmem->Release();
pRequest->CompleteWithInformation(0,(SIZE_T)inSize);
}
}
void setString(CMyDevice *device,IWDFIoRequest* pRequest,char *result,int outSize) {
IWDFMemory *outmem=NULL;
pRequest->GetOutputMemory (&outmem);
if (outmem==NULL) {
SectionLocker lock(device->m_RequestLock);
OutputDebugString(L"GetOutputMemory failed");
pRequest->Complete(HRESULT_FROM_WIN32(ERROR_GEN_FAILURE));
}
else {
SectionLocker lock(device->m_RequestLock);
int size=min(outSize,(int)strlen(result)+1);
outmem->CopyFromBuffer(0,result,size);
outmem->Release();
pRequest->CompleteWithInformation(0,(SIZE_T)size);
}
}
void setInt(CMyDevice *device,IWDFIoRequest* pRequest,DWORD result) {
IWDFMemory *outmem=NULL;
pRequest->GetOutputMemory (&outmem);
if (outmem==NULL) {
SectionLocker lock(device->m_RequestLock);
OutputDebugString(L"GetOutputMemory failed");
pRequest->Complete(HRESULT_FROM_WIN32(ERROR_GEN_FAILURE));
}
else {
SectionLocker lock(device->m_RequestLock);
outmem->CopyFromBuffer(0,&result,sizeof(result));
outmem->Release();
pRequest->CompleteWithInformation(0,sizeof(result));
}
}
DWORD getInt(IWDFIoRequest* pRequest) {
IWDFMemory *inmem=NULL;
pRequest->GetInputMemory(&inmem);
if (inmem==NULL) {
OutputDebugString(L"GetInputMemory failed");
return 0xffffffff;
}
else {
SIZE_T size;
void *data=inmem->GetDataBuffer(&size);
DWORD d=*(DWORD *)data;
inmem->Release();
return d;
}
}

View File

@@ -0,0 +1,10 @@
#pragma once
#include "internal.h"
#include "device.h"
bool getBuffer(IWDFIoRequest* pRequest,BYTE *buffer,int *bufferLen);
void setString(CMyDevice *device,IWDFIoRequest* pRequest,char *result,int outSize);
void setBuffer(CMyDevice *device,IWDFIoRequest* pRequest,BYTE *result,int inSize);
void setInt(CMyDevice *device,IWDFIoRequest* pRequest,DWORD result);
DWORD getInt(IWDFIoRequest* pRequest);

File diff suppressed because it is too large Load Diff

View File

@@ -0,0 +1,47 @@
#include "sectionLocker.h"
void *ShowMe=NULL;
SectionLocker::SectionLocker(CRITICAL_SECTION &critsection,char *function,int line,void* object) {
critsection;
this->section=&critsection;
Function=function;
Line=line;
Object=object;
DWORD start=GetTickCount();
if (TryEnterCriticalSection(section)==0) {
OutputDebugStringA("[BixVReader]RACE!");
char logBuffer[500];
sprintf(logBuffer,"[BixVReader]Richiesto da:Function:%s,Line:%i,Object:%x,Lock:%x",Function,Line,Object,section);
OutputDebugStringA(logBuffer);
ShowMe=section;
EnterCriticalSection(section);
DWORD end=GetTickCount();
sprintf(logBuffer,"[BixVReader]Elapsed:%i ms",end-start);
OutputDebugStringA(logBuffer);
}
}
SectionLocker::~SectionLocker() {
if (section==ShowMe) {
char logBuffer[500];
sprintf(logBuffer,"[BixVReader]Posseduto da:Function:%s,Line:%i,Object:%x,Lock:%x",Function,Line,Object,section);
OutputDebugStringA(logBuffer);
ShowMe=NULL;
}
LeaveCriticalSection(section);
}
SectionLogger::SectionLogger(char *section) {
section;
start=GetTickCount();
char logBuffer[500];
SectionName=section;
sprintf(logBuffer,"[BixVReader]Start section:%s",SectionName);
OutputDebugStringA(logBuffer);
}
SectionLogger::~SectionLogger() {
DWORD end=GetTickCount();
char logBuffer[500];
sprintf(logBuffer,"[BixVReader]End section:%s elapsed:%i",SectionName,end-start);
OutputDebugStringA(logBuffer);
}

View File

@@ -0,0 +1,24 @@
#pragma once
#include "internal.h"
class SectionLocker {
char *Function;
int Line;
void *Object;
public:
CRITICAL_SECTION *section;
SectionLocker(CRITICAL_SECTION &critsection,char *function,int line,void* object);
//SectionLocker(CRITICAL_SECTION &section);
~SectionLocker();
};
class SectionLogger {
DWORD start;
char *SectionName;
public:
SectionLogger(char *section);
~SectionLogger();
};
#define lock(x) lockObj(x,__FUNCTION__,__LINE__,NULL)