VeraCrypt
aboutsummaryrefslogtreecommitdiff
path: root/src/Common/SCardLoader.h
blob: b1ab114b257c71c23c62a93de447a25a17d5c3f7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
#ifndef TC_HEADER_Common_SCardLoader
#define TC_HEADER_Common_SCardLoader

#include "Platform/PlatformBase.h"

#ifdef TC_WINDOWS
#include <winscard.h>
#include <windows.h>
#else
#ifdef TC_MACOSX
#undef BOOL
#include <PCSC/pcsclite.h>
#include <PCSC/winscard.h>
#include <PCSC/wintypes.h>
#include "reader.h"
typedef LPSCARD_READERSTATE_A LPSCARD_READERSTATE;
using VeraCrypt::byte;
#define BOOL int
#else
#undef BOOL
#include "pcsclite.h"
#include <winscard.h>
#include <wintypes.h>
#include <reader.h>
using VeraCrypt::byte;
#define BOOL int
#endif
#endif

#ifndef TC_WINDOWS
typedef void* HMODULE;
#define SCARD_CALL_SPEC
#else
#define SCARD_CALL_SPEC	WINAPI
#endif

namespace VeraCrypt
{
	typedef LONG (SCARD_CALL_SPEC *SCardEstablishContextPtr)(DWORD dwScope, LPCVOID pvReserved1, LPCVOID pvReserved2, LPSCARDCONTEXT phContext);
	typedef LONG (SCARD_CALL_SPEC *SCardReleaseContextPtr)(SCARDCONTEXT hContext);
	typedef LONG (SCARD_CALL_SPEC *SCardIsValidContextPtr)(SCARDCONTEXT hContext);
#ifndef TC_MACOSX
	typedef LONG (SCARD_CALL_SPEC *SCardFreeMemoryPtr)(SCARDCONTEXT hContext, LPCVOID pvMem);
#endif
	typedef LONG (SCARD_CALL_SPEC *SCardConnectPtr)(SCARDCONTEXT hContext, LPCTSTR szReader, DWORD dwShareMode, DWORD dwPreferredProtocols, LPSCARDHANDLE phCard, LPDWORD pdwActiveProtocol);
	typedef LONG (SCARD_CALL_SPEC *SCardReconnectPtr)(SCARDHANDLE hCard, DWORD dwShareMode, DWORD dwPreferredProtocols, DWORD dwInitialization, LPDWORD pdwActiveProtocol);
	typedef LONG (SCARD_CALL_SPEC *SCardDisconnectPtr)(SCARDHANDLE hCard, DWORD dwDisposition);
	typedef LONG (SCARD_CALL_SPEC *SCardBeginTransactionPtr)(SCARDHANDLE hCard);
	typedef LONG (SCARD_CALL_SPEC *SCardEndTransactionPtr)(SCARDHANDLE hCard, DWORD dwDisposition);
	typedef LONG (SCARD_CALL_SPEC *SCardStatusPtr)(SCARDHANDLE hCard, LPTSTR mszReaderNames, LPDWORD pcchReaderLen, LPDWORD pdwState, LPDWORD pdwProtocol, BYTE* pbAtr, LPDWORD pcbAtrLen);
	typedef LONG (SCARD_CALL_SPEC *SCardGetStatusChangePtr)(SCARDCONTEXT hContext, DWORD dwTimeout, LPSCARD_READERSTATE rgReaderStates, DWORD cReaders);
	typedef LONG (SCARD_CALL_SPEC *SCardControlPtr)(SCARDHANDLE hCard, DWORD dwControlCode, LPCVOID pbSendBuffer, DWORD cbSendLength, LPVOID pbRecvBuffer, DWORD cbRecvLength, LPDWORD lpBytesReturned);
	typedef LONG (SCARD_CALL_SPEC *SCardTransmitPtr)(SCARDHANDLE hCard, LPCSCARD_IO_REQUEST pioSendPci, const BYTE* pbSendBuffer, DWORD cbSendLength, LPSCARD_IO_REQUEST pioRecvPci, BYTE* pbRecvBuffer, LPDWORD pcbRecvLength);
	typedef LONG (SCARD_CALL_SPEC *SCardListReaderGroupsPtr)(SCARDCONTEXT hContext, LPTSTR mszGroups, LPDWORD pcchGroups);
	typedef LONG (SCARD_CALL_SPEC *SCardListReadersPtr)(SCARDCONTEXT hContext, LPCTSTR mszGroups, LPTSTR mszReaders, LPDWORD pcchReaders);
	typedef LONG (SCARD_CALL_SPEC *SCardCancelPtr)(SCARDCONTEXT hContext);
	typedef LONG (SCARD_CALL_SPEC *SCardGetAttribPtr)(SCARDHANDLE hCard, DWORD dwAttrId, BYTE* pbAttr, LPDWORD pcbAttrLen);
	typedef LONG (SCARD_CALL_SPEC *SCardSetAttribPtr)(SCARDHANDLE hCard, DWORD dwAttrId, const BYTE* pbAttr, DWORD cbAttrLen);

	class SCardLoader 
	{
	protected:
		static HMODULE						hScardModule;
		static SCARDCONTEXT					hScardContext;
		static SCardEstablishContextPtr     scardEstablishContext;
		static SCardReleaseContextPtr		scardReleaseContext;
		static SCardIsValidContextPtr		scardIsValidContext;
#ifndef TC_MACOSX
		static SCardFreeMemoryPtr			scardFreeMemory;
#endif
		static SCardConnectPtr				scardConnect;
		static SCardReconnectPtr            scardReconnect;
		static SCardDisconnectPtr			scardDisconnect;     
		static SCardBeginTransactionPtr		scardBeginTransaction;
		static SCardEndTransactionPtr		scardEndTransaction;
		static SCardStatusPtr				scardStatus;
		static SCardGetStatusChangePtr		scardGetStatusChange;
		static SCardControlPtr				scardControl;
		static SCardTransmitPtr				scardTransmit;
		static SCardListReaderGroupsPtr		scardListReaderGroups;
		static SCardListReadersPtr			scardListReaders;
		static SCardCancelPtr				scardCancel;
		static SCardGetAttribPtr            scardGetAttrib;
		static SCardSetAttribPtr            scardSetAttrib;
		static bool							bInitialized;

	public:
		static SCARD_IO_REQUEST*			scardT0Pci;
		static SCARD_IO_REQUEST*			scardT1Pci;
		static SCARD_IO_REQUEST*			scardRawPci;

		SCardLoader() { };
		static void Initialize();
		static void Finalize();
#ifdef TC_WINDOWS
		static wstring GetSCardPath();
#else
		static string GetSCardPath();
#endif
		static SCARDCONTEXT GetSCardContext();

		static LONG SCardEstablishContext(DWORD dwScope, LPCVOID pvReserved1, LPCVOID pvReserved2, LPSCARDCONTEXT phContext);
		static LONG SCardReleaseContext(SCARDCONTEXT hContext);
		static LONG SCardIsValidContext(SCARDCONTEXT hContext);
#ifndef TC_MACOSX
		static LONG SCardFreeMemory(SCARDCONTEXT hContext, LPCVOID pvMem);
#endif
		static LONG SCardConnect(SCARDCONTEXT hContext, LPCTSTR szReader, DWORD dwShareMode, DWORD dwPreferredProtocols, LPSCARDHANDLE phCard, LPDWORD pdwActiveProtocol);
		static LONG SCardReconnect(SCARDHANDLE hCard, DWORD dwShareMode, DWORD dwPreferredProtocols, DWORD dwInitialization, LPDWORD pdwActiveProtocol);
		static LONG SCardDisconnect(SCARDHANDLE hCard, DWORD dwDisposition);
		static LONG SCardBeginTransaction(SCARDHANDLE hCard);
		static LONG SCardEndTransaction(SCARDHANDLE hCard, DWORD dwDisposition);
		static LONG SCardStatus(SCARDHANDLE hCard, LPTSTR mszReaderNames, LPDWORD pcchReaderLen, LPDWORD pdwState, LPDWORD pdwProtocol, BYTE* pbAtr, LPDWORD pcbAtrLen);
		static LONG SCardGetStatusChange(SCARDCONTEXT hContext, DWORD dwTimeout, LPSCARD_READERSTATE rgReaderStates, DWORD cReaders);
		static LONG SCardControl(SCARDHANDLE hCard, DWORD dwControlCode, LPCVOID pbSendBuffer, DWORD cbSendLength, LPVOID pbRecvBuffer, DWORD cbRecvLength, LPDWORD lpBytesReturned);
		static LONG SCardTransmit(SCARDHANDLE hCard, LPCSCARD_IO_REQUEST pioSendPci, const BYTE* pbSendBuffer, DWORD cbSendLength, LPSCARD_IO_REQUEST pioRecvPci, BYTE* pbRecvBuffer, LPDWORD pcbRecvLength);
		static LONG SCardListReaderGroups(SCARDCONTEXT hContext, LPTSTR mszGroups, LPDWORD pcchGroups);
		static LONG SCardListReaders(SCARDCONTEXT hContext, LPCTSTR mszGroups, LPTSTR mszReaders, LPDWORD pcchReaders);
		static LONG SCardCancel(SCARDCONTEXT hContext);
		static LONG SCardGetAttrib(SCARDHANDLE hCard, DWORD dwAttrId, BYTE* pbAttr, LPDWORD pcbAttrLen);
		static LONG SCardSetAttrib(SCARDHANDLE hCard, DWORD dwAttrId, const BYTE* pbAttr, DWORD cbAttrLen);
	};
};

#endif // TC_HEADER_Common_SCardLoader