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

Diff of /trunk/OpenPGPminidriverTest/main.cpp

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 7 by vletoux, Thu Mar 4 21:50:46 2010 UTC revision 11 by vletoux, Thu Mar 18 16:03:39 2010 UTC
# Line 19  Line 19 
19  #include <tchar.h>  #include <tchar.h>
20  #include <cardmod.h>  #include <cardmod.h>
21  #include <commctrl.h>  #include <commctrl.h>
22    #include <Cryptuiapi.h>
23  #include "dialog.h"  #include "dialog.h"
24  #include "global.h"  #include "global.h"
25    
26    #pragma comment(lib,"comctl32")
27    
28  #ifdef UNICODE  #ifdef UNICODE
29  #if defined _M_IX86  #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='*'\"")  #pragma comment(linker,"/manifestdependency:\"type='win32' name='Microsoft.Windows.Common-Controls' version='6.0.0.0' processorArchitecture='x86' publicKeyToken='6595b64144ccf1df' language='*'\"")
# Line 35  Line 38 
38  #endif  #endif
39    
40  // Variables globales�:  // Variables globales�:
41  HINSTANCE hInst;                                                                // instance actuelle  HINSTANCE g_hinst;                                                              // instance actuelle
 HWND hMainWnd;  
42    
43  INT_PTR CALLBACK        WndProc(HWND, UINT, WPARAM, LPARAM);  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,  int APIENTRY _tWinMain(HINSTANCE hInstance,
50                       HINSTANCE hPrevInstance,                       HINSTANCE hPrevInstance,
# Line 47  int APIENTRY _tWinMain(HINSTANCE hInstan Line 53  int APIENTRY _tWinMain(HINSTANCE hInstan
53  {  {
54          UNREFERENCED_PARAMETER(hPrevInstance);          UNREFERENCED_PARAMETER(hPrevInstance);
55          UNREFERENCED_PARAMETER(lpCmdLine);          UNREFERENCED_PARAMETER(lpCmdLine);
56          hInst = hInstance;          g_hinst = hInstance;
57                PROPSHEETPAGE psp[5];
58      DialogBox (hInst, MAKEINTRESOURCE (IDD_DLG), 0, WndProc);      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;      return 0;
116    
117  }  }
# Line 62  void MessageBoxWin32(DWORD status) { Line 124  void MessageBoxWin32(DWORD status) {
124          LocalFree(Error);          LocalFree(Error);
125  }  }
126    
127  BOOL GetContainerName(PWSTR szContainer, PDWORD pdwKeySpec)  
128    void ViewCertificate(HWND hWnd, PCCERT_CONTEXT pCertContext)
129  {  {
130          DWORD iItem = (DWORD)SendMessage(GetDlgItem(hMainWnd, IDC_LSTCONTAINER),LB_GETCURSEL,0,0);          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;          if (iItem == LB_ERR) return FALSE;
170          SendMessage( GetDlgItem(hMainWnd,IDC_LSTCONTAINER), LB_GETTEXT,iItem,(LPARAM)szContainer);          SendMessage( GetDlgItem(hWnd,IDC_LSTCONTAINER), LB_GETTEXT,iItem,(LPARAM)szContainer);
171          *pdwKeySpec = _tstoi(szContainer + _tcslen(szContainer) - 1);          *pdwKeySpec = _tstoi(szContainer + _tcslen(szContainer) - 1);
172          szContainer[ _tcslen(szContainer) - 2] = 0;          szContainer[ _tcslen(szContainer) - 2] = 0;
173          return TRUE;          return TRUE;
174  }  }
175    
176  INT_PTR CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)  
177    INT_PTR CALLBACK WndProcConnect(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
178  {  {
179          int wmId, wmEvent;          int wmId, wmEvent;
180          DWORD dwReturn;          DWORD dwReturn;
         TCHAR szPin[256];  
         TCHAR szContainer[256];  
         DWORD dwKeySpec;  
181          switch (message)          switch (message)
182          {          {
183          case WM_INITDIALOG:          case WM_INITDIALOG:
                 hMainWnd = hWnd;  
184                  CheckDlgButton(hWnd,IDC_CurrentDll,BST_CHECKED);                  CheckDlgButton(hWnd,IDC_CurrentDll,BST_CHECKED);
                 CheckDlgButton(hWnd,IDC_PINUSER,BST_CHECKED);  
                 SendDlgItemMessage(hMainWnd,IDC_CONTAINERINDEX,CB_ADDSTRING,0,(LPARAM)TEXT("Signature"));  
                 SendDlgItemMessage(hMainWnd,IDC_CONTAINERINDEX,CB_ADDSTRING,0,(LPARAM)TEXT("Authentication"));  
                 SendDlgItemMessage(hMainWnd,IDC_CONTAINERINDEX,CB_ADDSTRING,0,(LPARAM)TEXT("Confidentiality"));  
                 SendDlgItemMessage(hMainWnd,IDC_CONTAINERINDEX, CB_SETCURSEL, 0, 0);  
185                  break;                  break;
   
         case WM_CLOSE:  
                 EndDialog(hWnd, IDOK);  
                 return TRUE;  
186          case WM_COMMAND:          case WM_COMMAND:
187                  wmId    = LOWORD(wParam);                  wmId    = LOWORD(wParam);
188                  wmEvent = HIWORD(wParam);                  wmEvent = HIWORD(wParam);
# Line 121  INT_PTR CALLBACK WndProc(HWND hWnd, UINT Line 210  INT_PTR CALLBACK WndProc(HWND hWnd, UINT
210                                          MessageBoxWin32(dwReturn);                                          MessageBoxWin32(dwReturn);
211                                  }                                  }
212                                  break;                                  break;
213                          case IDC_PIN:                  }
214                                  GetDlgItemText(hWnd,IDC_TXTPIN,szPin,ARRAYSIZE(szPin));                  break;
215                                  DWORD dwRemaining;          }
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))                                  if (IsDlgButtonChecked(hWnd,IDC_PINADMIN))
251                                  {                                  {
252                                          dwReturn = Authenticate(szPin, wszCARD_USER_ADMIN, &dwRemaining);                                          dwReturn = Authenticate(szPin, wszCARD_USER_ADMIN, &dwRemaining);
# Line 134  INT_PTR CALLBACK WndProc(HWND hWnd, UINT Line 257  INT_PTR CALLBACK WndProc(HWND hWnd, UINT
257                                  }                                  }
258                                  MessageBoxWin32(dwReturn);                                  MessageBoxWin32(dwReturn);
259                                  break;                                  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:                          case IDC_LISTFILES:
325                                  dwReturn = ListFiles();                                  dwReturn = ListFiles(hWnd);
326                                  if (dwReturn)                                  if (dwReturn)
327                                  {                                  {
328                                          MessageBoxWin32(dwReturn);                                          MessageBoxWin32(dwReturn);
329                                  }                                  }
330                                  break;                                  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:                          case IDC_NEWKEY:
368                                  dwReturn = GenerateNewKey((DWORD)SendDlgItemMessage(hMainWnd,IDC_CONTAINERINDEX, CB_GETCURSEL, 0, 0));                                  dwReturn = GenerateNewKey((DWORD)SendDlgItemMessage(hWnd,IDC_CONTAINERINDEX, CB_GETCURSEL, 0, 0));
369                                  MessageBoxWin32(dwReturn);                                  MessageBoxWin32(dwReturn);
370                                  break;                                  break;
371                          case IDC_IMPORTKEY:                          case IDC_IMPORTKEY:
372                                  dwReturn = ImportKey((DWORD)SendDlgItemMessage(hMainWnd,IDC_CONTAINERINDEX, CB_GETCURSEL, 0, 0));                                  dwReturn = ImportKey((DWORD)SendDlgItemMessage(hWnd,IDC_CONTAINERINDEX, CB_GETCURSEL, 0, 0));
373                                  MessageBoxWin32(dwReturn);                                  MessageBoxWin32(dwReturn);
374                                  break;                                  break;
375                          case IDC_SAMEKEY:                          case IDC_SAMEKEY:
376                                  dwReturn = SetTheSameKeyForAllContainers();                                  dwReturn = SetTheSameKeyForAllContainers();
377                                  MessageBoxWin32(dwReturn);                                  MessageBoxWin32(dwReturn);
378                                  break;                                  break;
379                                  ////////// base CSP                          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:                          case IDC_CONTAINER:
410                                  dwReturn = ListContainer();                                  dwReturn = ListContainer(hWnd);
411                                  if (dwReturn)                                  if (dwReturn)
412                                  {                                  {
413                                          MessageBoxWin32(dwReturn);                                          MessageBoxWin32(dwReturn);
414                                  }                                  }
415                                  break;                                  break;
416                          case IDC_SIGN:                          case IDC_SIGN:
417                                  if (GetContainerName(szContainer, &dwKeySpec))                                  if (GetContainerName(hWnd, szContainer, &dwKeySpec))
418                                  {                                  {
419                                          dwReturn = Sign(szContainer, dwKeySpec);                                          dwReturn = Sign(szContainer, dwKeySpec);
420                                          MessageBoxWin32(dwReturn);                                          MessageBoxWin32(dwReturn);
421                                  }                                  }
422                                  break;                                  break;
423                          case IDC_DECRYPT:                          case IDC_DECRYPT:
424                                  if (GetContainerName(szContainer, &dwKeySpec))                                  if (GetContainerName(hWnd, szContainer, &dwKeySpec))
425                                  {                                  {
426                                          dwReturn = Decrypt(szContainer, dwKeySpec);                                          dwReturn = Decrypt(szContainer, dwKeySpec);
427                                          MessageBoxWin32(dwReturn);                                          MessageBoxWin32(dwReturn);
# Line 179  INT_PTR CALLBACK WndProc(HWND hWnd, UINT Line 431  INT_PTR CALLBACK WndProc(HWND hWnd, UINT
431                                  switch(wmEvent)                                  switch(wmEvent)
432                                  {                                  {
433                                          case LBN_DBLCLK:                                          case LBN_DBLCLK:
434                                                  if (GetContainerName(szContainer, &dwKeySpec))                                                  if (GetContainerName(hWnd, szContainer, &dwKeySpec))
435                                                  {                                                  {
436                                                          dwReturn = ViewCertificate(szContainer, dwKeySpec);                                                          dwReturn = ViewCertificate(hWnd, szContainer, dwKeySpec);
437                                                          if (dwReturn)                                                          if (dwReturn)
438                                                          {                                                          {
439                                                                  MessageBoxWin32(dwReturn);                                                                  MessageBoxWin32(dwReturn);

Legend:
Removed from v.7  
changed lines
  Added in v.11

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26