Click here to Skip to main content
15,886,110 members
Articles / Programming Languages / C++

An UMDF Driver for a Virtual Smart Card Reader

Rate me:
Please Sign up or sign in to vote.
4.89/5 (70 votes)
30 Jan 2014CPOL23 min read 589.3K   10.5K   104  
A simple implementation of a driver for a virtual smart card reader, based on UMDF
#pragma once

#include <Winsock2.h>

class CMyDevice;

class Reader {
public:
	CMyDevice *device;
	CComPtr<IWDFIoRequest> waitRemoveIpr;
	CComPtr<IWDFIoRequest> waitInsertIpr;

	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;
};

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Engineer
Italy Italy
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions