/[openpgpmdrv]/trunk/OpenPGPminidriverTest/InitializationAndDeconstruct.cpp
ViewVC logotype

Contents of /trunk/OpenPGPminidriverTest/InitializationAndDeconstruct.cpp

Parent Directory Parent Directory | Revision Log Revision Log


Revision 8 - (show annotations)
Thu Mar 11 20:32:26 2010 UTC (15 years, 1 month ago) by vletoux
File size: 6800 byte(s)
improvement of the quality of the project.
More test for the qualification of the driver success but not all ...

1 /* OpenPGP Smart Card Mini Driver
2 Copyright (C) 2009 Vincent Le Toux
3
4 This library is Free software; you can redistribute it and/or
5 modify it under the terms of the GNU Lesser General Public
6 License version 2.1 as published by the Free Software Foundation.
7
8 This library is distributed in the hope that it will be useful,
9 but WITHOUT ANY WARRANTY; without even the implied warranty of
10 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 Lesser General Public License for more details.
12
13 You should have received a copy of the GNU Lesser General Public
14 License along with this library; if not, write to the Free Software
15 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
16 */
17
18 #include <windows.h>
19 #include <tchar.h>
20 #include <cardmod.h>
21
22 #pragma comment(lib,"Scarddlg")
23 #pragma comment(lib,"Winscard")
24
25 HMODULE hModule = NULL;
26 CARD_DATA CardData;
27 PCARD_DATA pCardData = NULL;
28 SCARD_ATRMASK atr;
29 TCHAR szCard[256];
30
31 extern "C" {
32
33 //
34 // Heap helpers
35 //
36
37 LPVOID WINAPI _Alloc(__in SIZE_T cBytes)
38 {
39 return HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, cBytes);
40 }
41
42 LPVOID WINAPI _ReAlloc(
43 __in LPVOID pvMem,
44 __in SIZE_T cBytes)
45 {
46 return HeapReAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, pvMem, cBytes);
47 }
48
49 void WINAPI _Free(
50 __in LPVOID pvMem)
51 {
52 HeapFree(GetProcessHeap(), 0, pvMem);
53 }
54
55 //
56 // Dummy data caching stubs to satisfy the card module callback requirements
57 //
58
59 DWORD WINAPI _CacheAddFileStub(
60 IN PVOID pvCacheContext,
61 IN LPWSTR wszTag,
62 IN DWORD dwFlags,
63 IN PBYTE pbData,
64 IN DWORD cbData)
65 {
66 UNREFERENCED_PARAMETER(pvCacheContext);
67 UNREFERENCED_PARAMETER(wszTag);
68 UNREFERENCED_PARAMETER(dwFlags);
69 UNREFERENCED_PARAMETER(pbData);
70 UNREFERENCED_PARAMETER(cbData);
71 return ERROR_SUCCESS;
72 }
73
74 DWORD WINAPI _CacheLookupFileStub(
75 IN PVOID pvCacheContext,
76 IN LPWSTR wszTag,
77 IN DWORD dwFlags,
78 IN PBYTE *ppbData,
79 IN PDWORD pcbData)
80 {
81 UNREFERENCED_PARAMETER(pvCacheContext);
82 UNREFERENCED_PARAMETER(wszTag);
83 UNREFERENCED_PARAMETER(dwFlags);
84 UNREFERENCED_PARAMETER(ppbData);
85 UNREFERENCED_PARAMETER(pcbData);
86 return ERROR_NOT_FOUND;
87 }
88
89 DWORD WINAPI _CacheDeleteFileStub(
90 IN PVOID pvCacheContext,
91 IN LPWSTR wszTag,
92 IN DWORD dwFlags)
93 {
94 UNREFERENCED_PARAMETER(pvCacheContext);
95 UNREFERENCED_PARAMETER(wszTag);
96 UNREFERENCED_PARAMETER(dwFlags);
97 return ERROR_SUCCESS;
98 }
99 }
100
101 DWORD Connect(BOOL fSystemDll)
102 {
103 DWORD dwReturn = 0;
104 SCARDCONTEXT hSCardContext = NULL;
105 SCARDHANDLE hSCardHandle = NULL;
106 TCHAR szCardModule[256];
107 TCHAR szReader[256];
108 DWORD dwCardModuleSize = ARRAYSIZE(szCardModule);
109 DWORD dwReaderSize = ARRAYSIZE(szReader);
110 OPENCARDNAME_EX dlgStruct;
111 PFN_CARD_ACQUIRE_CONTEXT pfnCardAcquireContext;
112
113 __try
114 {
115 // find a smart card
116 /////////////////////
117
118 dwReturn = SCardEstablishContext(SCARD_SCOPE_USER,
119 NULL,
120 NULL,
121 &hSCardContext );
122 if ( SCARD_S_SUCCESS != dwReturn )
123 {
124 __leave;
125 }
126
127 // Initialize the structure.
128 memset(&dlgStruct, 0, sizeof(dlgStruct));
129 dlgStruct.dwStructSize = sizeof(dlgStruct);
130 dlgStruct.hSCardContext = hSCardContext;
131 dlgStruct.dwFlags = SC_DLG_MINIMAL_UI;
132 dlgStruct.lpstrRdr = szReader;
133 dlgStruct.nMaxRdr = dwReaderSize;
134 dlgStruct.lpstrCard = szCard;
135 dlgStruct.nMaxCard = ARRAYSIZE(szCard);
136 dlgStruct.lpstrTitle = L"Select Card";
137 dlgStruct.dwShareMode = 0;
138 // Display the select card dialog box.
139 dwReturn = SCardUIDlgSelectCard(&dlgStruct);
140 if ( SCARD_S_SUCCESS != dwReturn )
141 {
142 __leave;
143 }
144
145 // find the dll path / name
146 ////////////////////////////
147 if (fSystemDll)
148 {
149
150 dwReturn = SCardGetCardTypeProviderName(
151 hSCardContext,
152 szCard,
153 SCARD_PROVIDER_CARD_MODULE,
154 (PTSTR) &szCardModule,
155 &dwCardModuleSize);
156 if (0 == dwCardModuleSize)
157 {
158 dwReturn = (DWORD) SCARD_E_UNKNOWN_CARD;
159 __leave;
160 }
161 }
162 else
163 {
164 #ifdef _M_X64
165 _tcscpy_s(szCardModule, dwCardModuleSize, TEXT("openpgpmdrv64.dll"));
166 #else
167 _tcscpy_s(szCardModule, dwCardModuleSize, TEXT("openpgpmdrv32.dll"));
168 #endif
169 }
170 // connect to the smart card
171 ////////////////////////////
172 DWORD dwProtocol, dwState;
173 dwReturn = SCardConnect(hSCardContext,szReader,SCARD_SHARE_SHARED,SCARD_PROTOCOL_T1|SCARD_PROTOCOL_T0, &hSCardHandle, &dwProtocol);
174 if ( SCARD_S_SUCCESS != dwReturn )
175 {
176 __leave;
177 }
178 atr.cbAtr = 32;
179 dwReturn = SCardStatus(hSCardHandle, szReader, &dwReaderSize, &dwState, &dwProtocol, atr.rgbAtr,&atr.cbAtr);
180 if ( SCARD_S_SUCCESS != dwReturn )
181 {
182 __leave;
183 }
184 // load
185 ////////
186 if (NULL == (hModule = LoadLibrary(szCardModule)))
187 {
188 dwReturn = GetLastError();
189 __leave;
190 }
191
192 if (NULL == (pfnCardAcquireContext =
193 (PFN_CARD_ACQUIRE_CONTEXT) GetProcAddress(
194 hModule, "CardAcquireContext")))
195 {
196 dwReturn = GetLastError();
197 __leave;
198 }
199 // initialize context
200 //////////////////////
201 pCardData = &CardData;
202 pCardData->dwVersion = CARD_DATA_CURRENT_VERSION;
203 pCardData->pfnCspAlloc = _Alloc;
204 pCardData->pfnCspFree = _Free;
205 pCardData->pfnCspReAlloc = _ReAlloc;
206 pCardData->pfnCspCacheAddFile = _CacheAddFileStub;
207 pCardData->pfnCspCacheLookupFile = _CacheLookupFileStub;
208 pCardData->pfnCspCacheDeleteFile = _CacheDeleteFileStub;
209 pCardData->hScard = hSCardHandle;
210 pCardData->hSCardCtx = hSCardContext;
211 pCardData->cbAtr = atr.cbAtr;
212 pCardData->pbAtr = atr.rgbAtr;
213 pCardData->pwszCardName = szCard;
214 //dwReturn = SCardBeginTransaction(hSCardHandle);
215 if ( SCARD_S_SUCCESS != dwReturn )
216 {
217 __leave;
218 }
219 dwReturn = pfnCardAcquireContext(pCardData, 0);
220 }
221 __finally
222 {
223 if (dwReturn != 0)
224 {
225 if (hSCardHandle)
226 {
227 SCardEndTransaction(hSCardHandle,SCARD_LEAVE_CARD);
228 SCardDisconnect(hSCardHandle,0);
229 }
230 if (hSCardContext)
231 SCardReleaseContext(hSCardContext);
232 }
233 }
234 return dwReturn;
235 }
236
237 DWORD Disconnect()
238 {
239 DWORD dwReturn = 0;
240 if (pCardData)
241 {
242 if (pCardData->hScard)
243 {
244 SCardEndTransaction(pCardData->hScard,SCARD_LEAVE_CARD);
245 SCardDisconnect(pCardData->hScard,0);
246 }
247 if (pCardData->hSCardCtx)
248 SCardReleaseContext(pCardData->hSCardCtx);
249 pCardData = NULL;
250 }
251 else
252 {
253 dwReturn = SCARD_E_COMM_DATA_LOST;
254 }
255 return dwReturn;
256 }

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26