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 |
|
|
SendDlgItemMessage(hWnd,IDC_CONTAINERINDEX,CB_ADDSTRING,0,(LPARAM)TEXT("Authentication")); |
319 |
|
|
SendDlgItemMessage(hWnd,IDC_CONTAINERINDEX,CB_ADDSTRING,0,(LPARAM)TEXT("Confidentiality")); |
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 |
8 |
} |
341 |
|
|
break; |
342 |
|
|
} |
343 |
|
|
return FALSE; |
344 |
|
|
} |
345 |
|
|
|
346 |
|
|
INT_PTR CALLBACK WndProcCryptoApi(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam) |
347 |
|
|
{ |
348 |
|
|
int wmId, wmEvent; |
349 |
|
|
DWORD dwReturn; |
350 |
|
|
TCHAR szContainer[256]; |
351 |
|
|
DWORD dwKeySpec; |
352 |
|
|
switch (message) |
353 |
|
|
{ |
354 |
|
|
case WM_INITDIALOG: |
355 |
|
|
break; |
356 |
|
|
case WM_COMMAND: |
357 |
|
|
wmId = LOWORD(wParam); |
358 |
|
|
wmEvent = HIWORD(wParam); |
359 |
|
|
|
360 |
|
|
switch (wmId) |
361 |
|
|
{ |
362 |
vletoux |
1 |
case IDC_CONTAINER: |
363 |
vletoux |
8 |
dwReturn = ListContainer(hWnd); |
364 |
vletoux |
1 |
if (dwReturn) |
365 |
|
|
{ |
366 |
|
|
MessageBoxWin32(dwReturn); |
367 |
|
|
} |
368 |
|
|
break; |
369 |
|
|
case IDC_SIGN: |
370 |
vletoux |
8 |
if (GetContainerName(hWnd, szContainer, &dwKeySpec)) |
371 |
vletoux |
1 |
{ |
372 |
|
|
dwReturn = Sign(szContainer, dwKeySpec); |
373 |
|
|
MessageBoxWin32(dwReturn); |
374 |
|
|
} |
375 |
|
|
break; |
376 |
|
|
case IDC_DECRYPT: |
377 |
vletoux |
8 |
if (GetContainerName(hWnd, szContainer, &dwKeySpec)) |
378 |
vletoux |
1 |
{ |
379 |
|
|
dwReturn = Decrypt(szContainer, dwKeySpec); |
380 |
|
|
MessageBoxWin32(dwReturn); |
381 |
|
|
} |
382 |
|
|
break; |
383 |
|
|
case IDC_LSTCONTAINER: |
384 |
|
|
switch(wmEvent) |
385 |
|
|
{ |
386 |
|
|
case LBN_DBLCLK: |
387 |
vletoux |
8 |
if (GetContainerName(hWnd, szContainer, &dwKeySpec)) |
388 |
vletoux |
1 |
{ |
389 |
vletoux |
8 |
dwReturn = ViewCertificate(hWnd, szContainer, dwKeySpec); |
390 |
vletoux |
1 |
if (dwReturn) |
391 |
|
|
{ |
392 |
|
|
MessageBoxWin32(dwReturn); |
393 |
|
|
} |
394 |
|
|
} |
395 |
|
|
break; |
396 |
|
|
} |
397 |
|
|
break; |
398 |
|
|
} |
399 |
|
|
break; |
400 |
|
|
} |
401 |
|
|
return FALSE; |
402 |
|
|
} |
403 |
|
|
|