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

Contents of /trunk/OpenPGPminidriverTest/main.cpp

Parent Directory Parent Directory | Revision Log Revision Log


Revision 11 - (show annotations)
Thu Mar 18 16:03:39 2010 UTC (15 years, 1 month ago) by vletoux
File size: 13223 byte(s)
first working & stable version
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 #include <commctrl.h>
22 #include <Cryptuiapi.h>
23 #include "dialog.h"
24 #include "global.h"
25
26 #pragma comment(lib,"comctl32")
27
28 #ifdef UNICODE
29 #if defined _M_IX86
30 #pragma comment(linker,"/manifestdependency:\"type='win32' name='Microsoft.Windows.Common-Controls' version='6.0.0.0' processorArchitecture='x86' publicKeyToken='6595b64144ccf1df' language='*'\"")
31 #elif defined _M_IA64
32 #pragma comment(linker,"/manifestdependency:\"type='win32' name='Microsoft.Windows.Common-Controls' version='6.0.0.0' processorArchitecture='ia64' publicKeyToken='6595b64144ccf1df' language='*'\"")
33 #elif defined _M_X64
34 #pragma comment(linker,"/manifestdependency:\"type='win32' name='Microsoft.Windows.Common-Controls' version='6.0.0.0' processorArchitecture='amd64' publicKeyToken='6595b64144ccf1df' language='*'\"")
35 #else
36 #pragma comment(linker,"/manifestdependency:\"type='win32' name='Microsoft.Windows.Common-Controls' version='6.0.0.0' processorArchitecture='*' publicKeyToken='6595b64144ccf1df' language='*'\"")
37 #endif
38 #endif
39
40 // Variables globales :
41 HINSTANCE g_hinst; // instance actuelle
42
43 INT_PTR CALLBACK WndProcConnect(HWND, UINT, WPARAM, LPARAM);
44 INT_PTR CALLBACK WndProcPin(HWND, UINT, WPARAM, LPARAM);
45 INT_PTR CALLBACK WndProcFile(HWND, UINT, WPARAM, LPARAM);
46 INT_PTR CALLBACK WndProcCrypto(HWND, UINT, WPARAM, LPARAM);
47 INT_PTR CALLBACK WndProcCryptoApi(HWND, UINT, WPARAM, LPARAM);
48
49 int APIENTRY _tWinMain(HINSTANCE hInstance,
50 HINSTANCE hPrevInstance,
51 LPTSTR lpCmdLine,
52 int nCmdShow)
53 {
54 UNREFERENCED_PARAMETER(hPrevInstance);
55 UNREFERENCED_PARAMETER(lpCmdLine);
56 g_hinst = hInstance;
57 PROPSHEETPAGE psp[5];
58 PROPSHEETHEADER psh;
59 psp[0].dwSize = sizeof(PROPSHEETPAGE);
60 psp[0].dwFlags = PSP_USETITLE;
61 psp[0].hInstance = g_hinst;
62 psp[0].pszTemplate = MAKEINTRESOURCE(IDD_CONNECT);
63 psp[0].pszIcon = NULL;
64 psp[0].pfnDlgProc = WndProcConnect;
65 psp[0].pszTitle = TEXT("Connect");
66 psp[0].lParam = 0;
67 psp[0].pfnCallback = NULL;
68 psp[1].dwSize = sizeof(PROPSHEETPAGE);
69 psp[1].dwFlags = PSP_USETITLE;
70 psp[1].hInstance = g_hinst;
71 psp[1].pszTemplate = MAKEINTRESOURCE(IDD_PIN);
72 psp[1].pszIcon = NULL;
73 psp[1].pfnDlgProc = WndProcPin;
74 psp[1].pszTitle = TEXT("Pin");
75 psp[1].lParam = 0;
76 psp[2].pfnCallback = NULL;
77 psp[2].dwSize = sizeof(PROPSHEETPAGE);
78 psp[2].dwFlags = PSP_USETITLE;
79 psp[2].hInstance = g_hinst;
80 psp[2].pszTemplate = MAKEINTRESOURCE(IDD_FILE);
81 psp[2].pszIcon = NULL;
82 psp[2].pfnDlgProc = WndProcFile;
83 psp[2].pszTitle = TEXT("File");
84 psp[2].lParam = 0;
85 psp[2].pfnCallback = NULL;
86 psp[3].dwSize = sizeof(PROPSHEETPAGE);
87 psp[3].dwFlags = PSP_USETITLE;
88 psp[3].hInstance = g_hinst;
89 psp[3].pszTemplate = MAKEINTRESOURCE(IDD_CRYPTO);
90 psp[3].pszIcon = NULL;
91 psp[3].pfnDlgProc = WndProcCrypto;
92 psp[3].pszTitle = TEXT("Crypto");
93 psp[3].lParam = 0;
94 psp[3].pfnCallback = NULL;
95 psp[4].dwSize = sizeof(PROPSHEETPAGE);
96 psp[4].dwFlags = PSP_USETITLE;
97 psp[4].hInstance = g_hinst;
98 psp[4].pszTemplate = MAKEINTRESOURCE(IDD_CRYPTOAPI);
99 psp[4].pszIcon = NULL;
100 psp[4].pfnDlgProc = WndProcCryptoApi;
101 psp[4].pszTitle = TEXT("CryptoApi");
102 psp[4].lParam = 0;
103 psp[4].pfnCallback = NULL;
104 psh.dwSize = sizeof(PROPSHEETHEADER);
105 psh.dwFlags = PSH_USEICONID | PSH_PROPSHEETPAGE;
106 psh.hwndParent = NULL;
107 psh.hInstance = g_hinst;
108 psh.pszIcon =NULL;
109 psh.pszCaption = TEXT("Test");
110 psh.nPages = ARRAYSIZE(psp);
111 psh.nStartPage = 0;
112 psh.ppsp = (LPCPROPSHEETPAGE) &psp;
113 psh.pfnCallback = NULL;
114 PropertySheet(&psh);
115 return 0;
116
117 }
118
119 void MessageBoxWin32(DWORD status) {
120 LPVOID Error;
121 FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER|FORMAT_MESSAGE_FROM_SYSTEM,
122 NULL,status,0,(LPTSTR)&Error,0,NULL);
123 MessageBox(NULL,(LPCTSTR)Error,NULL,MB_ICONASTERISK);
124 LocalFree(Error);
125 }
126
127
128 void ViewCertificate(HWND hWnd, PCCERT_CONTEXT pCertContext)
129 {
130 CRYPTUI_VIEWCERTIFICATE_STRUCT certViewInfo;
131 BOOL fPropertiesChanged = FALSE;
132 certViewInfo.dwSize = sizeof(CRYPTUI_VIEWCERTIFICATE_STRUCT);
133 certViewInfo.hwndParent = hWnd;
134 certViewInfo.dwFlags = CRYPTUI_DISABLE_EDITPROPERTIES | CRYPTUI_DISABLE_ADDTOSTORE | CRYPTUI_DISABLE_EXPORT | CRYPTUI_DISABLE_HTMLLINK;
135 certViewInfo.szTitle = TEXT("Info");
136 certViewInfo.pCertContext = pCertContext;
137 certViewInfo.cPurposes = 0;
138 certViewInfo.rgszPurposes = 0;
139 certViewInfo.pCryptProviderData = NULL;
140 certViewInfo.hWVTStateData = NULL;
141 certViewInfo.fpCryptProviderDataTrustedUsage = FALSE;
142 certViewInfo.idxSigner = 0;
143 certViewInfo.idxCert = 0;
144 certViewInfo.fCounterSigner = FALSE;
145 certViewInfo.idxCounterSigner = 0;
146 certViewInfo.cStores = 0;
147 certViewInfo.rghStores = NULL;
148 certViewInfo.cPropSheetPages = 0;
149 certViewInfo.rgPropSheetPages = NULL;
150 certViewInfo.nStartPage = 0;
151
152 CryptUIDlgViewCertificate(&certViewInfo,&fPropertiesChanged);
153 }
154
155 #define C_PAGES 5
156
157 typedef struct tag_dlghdr {
158 HWND hwndTab; // tab control
159 HWND hwndDisplay; // current child dialog box
160 RECT rcDisplay; // display rectangle for the tab control
161 DLGTEMPLATE *apRes[C_PAGES];
162 DLGPROC pDialogFunc[C_PAGES];
163 } DLGHDR;
164
165
166 BOOL GetContainerName(HWND hWnd, PWSTR szContainer, PDWORD pdwKeySpec)
167 {
168 DWORD iItem = (DWORD)SendMessage(GetDlgItem(hWnd, IDC_LSTCONTAINER),LB_GETCURSEL,0,0);
169 if (iItem == LB_ERR) return FALSE;
170 SendMessage( GetDlgItem(hWnd,IDC_LSTCONTAINER), LB_GETTEXT,iItem,(LPARAM)szContainer);
171 *pdwKeySpec = _tstoi(szContainer + _tcslen(szContainer) - 1);
172 szContainer[ _tcslen(szContainer) - 2] = 0;
173 return TRUE;
174 }
175
176
177 INT_PTR CALLBACK WndProcConnect(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
178 {
179 int wmId, wmEvent;
180 DWORD dwReturn;
181 switch (message)
182 {
183 case WM_INITDIALOG:
184 CheckDlgButton(hWnd,IDC_CurrentDll,BST_CHECKED);
185 break;
186 case WM_COMMAND:
187 wmId = LOWORD(wParam);
188 wmEvent = HIWORD(wParam);
189
190 switch (wmId)
191 {
192 case IDC_CONNECT:
193 if (IsDlgButtonChecked(hWnd,IDC_SystemDll))
194 {
195 dwReturn = Connect(TRUE);
196 }
197 else
198 {
199 dwReturn = Connect(FALSE);
200 }
201 if (dwReturn)
202 {
203 MessageBoxWin32(dwReturn);
204 }
205 break;
206 case IDC_DISCONNECT:
207 dwReturn = Disconnect();
208 if (dwReturn)
209 {
210 MessageBoxWin32(dwReturn);
211 }
212 break;
213 }
214 break;
215 }
216 return FALSE;
217 }
218
219 INT_PTR CALLBACK WndProcPin(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
220 {
221 int wmId, wmEvent;
222 DWORD dwReturn;
223 CHAR szPin[256];
224 CHAR szPin2[256];
225 DWORD dwRemaining;
226 switch (message)
227 {
228 case WM_INITDIALOG:
229 CheckDlgButton(hWnd,IDC_PINUSER,BST_CHECKED);
230 SendMessage( GetDlgItem(hWnd, IDC_TXTPIN), WM_SETTEXT,0,(LPARAM) TEXT("123456"));
231 break;
232 case WM_COMMAND:
233 wmId = LOWORD(wParam);
234 wmEvent = HIWORD(wParam);
235
236 switch (wmId)
237 {
238 case IDC_PINUSER:
239 SendMessage( GetDlgItem(hWnd, IDC_TXTPIN), WM_SETTEXT,0,(LPARAM) TEXT("123456"));
240 break;
241 case IDC_PINADMIN:
242 SendMessage( GetDlgItem(hWnd, IDC_TXTPIN), WM_SETTEXT,0,(LPARAM) TEXT("12345678"));
243 break;
244 case IDC_PUK:
245 SendMessage( GetDlgItem(hWnd, IDC_TXTPIN), WM_SETTEXT,0,(LPARAM) TEXT("000000"));
246 break;
247 case IDC_CHECKPIN:
248 GetDlgItemTextA(hWnd,IDC_TXTPIN,szPin,ARRAYSIZE(szPin));
249
250 if (IsDlgButtonChecked(hWnd,IDC_PINADMIN))
251 {
252 dwReturn = Authenticate(szPin, wszCARD_USER_ADMIN, &dwRemaining);
253 }
254 else
255 {
256 dwReturn = Authenticate(szPin, wszCARD_USER_USER, &dwRemaining);
257 }
258 MessageBoxWin32(dwReturn);
259 break;
260 case IDC_UNBLOCKPIN:
261 GetDlgItemTextA(hWnd,IDC_TXTPIN,szPin,ARRAYSIZE(szPin));
262 GetDlgItemTextA(hWnd,IDC_TXTPIN2,szPin2,ARRAYSIZE(szPin2));
263 if (IsDlgButtonChecked(hWnd,IDC_PINADMIN))
264 {
265 dwReturn = ResetPin(szPin, szPin2, FALSE, &dwRemaining);
266 }
267 else if (IsDlgButtonChecked(hWnd,IDC_PUK))
268 {
269 dwReturn = ResetPin(szPin, szPin2, TRUE, &dwRemaining);
270 }
271 else
272 {
273 dwReturn = E_INVALIDARG;
274 }
275 MessageBoxWin32(dwReturn);
276 break;
277 case IDC_CHANGEPIN:
278 GetDlgItemTextA(hWnd,IDC_TXTPIN,szPin,ARRAYSIZE(szPin));
279 GetDlgItemTextA(hWnd,IDC_TXTPIN2,szPin2,ARRAYSIZE(szPin2));
280 if (IsDlgButtonChecked(hWnd,IDC_PINADMIN))
281 {
282 dwReturn = ChangePin(szPin, szPin2, wszCARD_USER_ADMIN, &dwRemaining);
283 }
284 else if (IsDlgButtonChecked(hWnd,IDC_PINUSER))
285 {
286 dwReturn = ChangePin(szPin, szPin2, wszCARD_USER_USER, &dwRemaining);
287 }
288 else
289 {
290 dwReturn = E_INVALIDARG;
291 }
292 MessageBoxWin32(dwReturn);
293 break;
294 case IDC_SETPUK:
295 GetDlgItemTextA(hWnd,IDC_TXTPIN,szPin,ARRAYSIZE(szPin));
296 GetDlgItemTextA(hWnd,IDC_TXTPIN2,szPin2,ARRAYSIZE(szPin2));
297 dwReturn = SetPuk(szPin, szPin2, &dwRemaining);
298 MessageBoxWin32(dwReturn);
299 break;
300 case IDC_PERSONNALIZE:
301 dwReturn = Personnalize();
302 MessageBoxWin32(dwReturn);
303 break;
304 }
305 break;
306 }
307 return FALSE;
308 }
309
310 INT_PTR CALLBACK WndProcFile(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
311 {
312 int wmId, wmEvent;
313 DWORD dwReturn;
314 switch (message)
315 {
316 case WM_INITDIALOG:
317 break;
318 case WM_COMMAND:
319 wmId = LOWORD(wParam);
320 wmEvent = HIWORD(wParam);
321
322 switch (wmId)
323 {
324 case IDC_LISTFILES:
325 dwReturn = ListFiles(hWnd);
326 if (dwReturn)
327 {
328 MessageBoxWin32(dwReturn);
329 }
330 break;
331 case IDC_FILES:
332 switch(wmEvent)
333 {
334 case LBN_SELCHANGE:
335 dwReturn = ViewFile(hWnd);
336 if (dwReturn)
337 {
338 MessageBoxWin32(dwReturn);
339 }
340 break;
341 }
342 break;
343 }
344 break;
345 }
346 return FALSE;
347 }
348
349 INT_PTR CALLBACK WndProcCrypto(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
350 {
351 int wmId, wmEvent;
352 DWORD dwReturn;
353 switch (message)
354 {
355 case WM_INITDIALOG:
356 SendDlgItemMessage(hWnd,IDC_CONTAINERINDEX,CB_ADDSTRING,0,(LPARAM)TEXT("Signature"));
357 SendDlgItemMessage(hWnd,IDC_CONTAINERINDEX,CB_ADDSTRING,0,(LPARAM)TEXT("Confidentiality"));
358 SendDlgItemMessage(hWnd,IDC_CONTAINERINDEX,CB_ADDSTRING,0,(LPARAM)TEXT("Authentication"));
359 SendDlgItemMessage(hWnd,IDC_CONTAINERINDEX, CB_SETCURSEL, 0, 0);
360 break;
361 case WM_COMMAND:
362 wmId = LOWORD(wParam);
363 wmEvent = HIWORD(wParam);
364
365 switch (wmId)
366 {
367 case IDC_NEWKEY:
368 dwReturn = GenerateNewKey((DWORD)SendDlgItemMessage(hWnd,IDC_CONTAINERINDEX, CB_GETCURSEL, 0, 0));
369 MessageBoxWin32(dwReturn);
370 break;
371 case IDC_IMPORTKEY:
372 dwReturn = ImportKey((DWORD)SendDlgItemMessage(hWnd,IDC_CONTAINERINDEX, CB_GETCURSEL, 0, 0));
373 MessageBoxWin32(dwReturn);
374 break;
375 case IDC_SAMEKEY:
376 dwReturn = SetTheSameKeyForAllContainers();
377 MessageBoxWin32(dwReturn);
378 break;
379 case IDC_SETREADONLY:
380 dwReturn = SetReadOnly(TRUE);
381 MessageBoxWin32(dwReturn);
382 break;
383 case IDC_UNSETREADONLY:
384 dwReturn = SetReadOnly(FALSE);
385 MessageBoxWin32(dwReturn);
386 break;
387 }
388 break;
389 }
390 return FALSE;
391 }
392
393 INT_PTR CALLBACK WndProcCryptoApi(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
394 {
395 int wmId, wmEvent;
396 DWORD dwReturn;
397 TCHAR szContainer[256];
398 DWORD dwKeySpec;
399 switch (message)
400 {
401 case WM_INITDIALOG:
402 break;
403 case WM_COMMAND:
404 wmId = LOWORD(wParam);
405 wmEvent = HIWORD(wParam);
406
407 switch (wmId)
408 {
409 case IDC_CONTAINER:
410 dwReturn = ListContainer(hWnd);
411 if (dwReturn)
412 {
413 MessageBoxWin32(dwReturn);
414 }
415 break;
416 case IDC_SIGN:
417 if (GetContainerName(hWnd, szContainer, &dwKeySpec))
418 {
419 dwReturn = Sign(szContainer, dwKeySpec);
420 MessageBoxWin32(dwReturn);
421 }
422 break;
423 case IDC_DECRYPT:
424 if (GetContainerName(hWnd, szContainer, &dwKeySpec))
425 {
426 dwReturn = Decrypt(szContainer, dwKeySpec);
427 MessageBoxWin32(dwReturn);
428 }
429 break;
430 case IDC_LSTCONTAINER:
431 switch(wmEvent)
432 {
433 case LBN_DBLCLK:
434 if (GetContainerName(hWnd, szContainer, &dwKeySpec))
435 {
436 dwReturn = ViewCertificate(hWnd, szContainer, dwKeySpec);
437 if (dwReturn)
438 {
439 MessageBoxWin32(dwReturn);
440 }
441 }
442 break;
443 }
444 break;
445 }
446 break;
447 }
448 return FALSE;
449 }
450

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26