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

Annotation of /trunk/OpenPGPminidriverTest/main.cpp

Parent Directory Parent Directory | Revision Log Revision Log


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

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26