a652239bb39f4f08589951c07f8b5861fe14fc1d
[reactos.git] / dll / win32 / cryptui / main.c
1 /*
2 * Copyright 2008 Juan Lang
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 as published by the Free Software Foundation; either
7 * version 2.1 of the License, or (at your option) any later version.
8 *
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
13 *
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with this library; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
17 */
18
19 #include "config.h"
20
21 #include <stdarg.h>
22
23 #define COBJMACROS
24 #define NONAMELESSUNION
25
26 #include "windef.h"
27 #include "winbase.h"
28 #include "winnls.h"
29 #include "winuser.h"
30 #include "softpub.h"
31 #include "wingdi.h"
32 #include "richedit.h"
33 #include "ole2.h"
34 #include "richole.h"
35 #include "commdlg.h"
36 #include "commctrl.h"
37 #include "cryptuiapi.h"
38 #include "cryptuires.h"
39 #include "urlmon.h"
40 #include "hlink.h"
41 #include "winreg.h"
42 #include "wine/debug.h"
43 #include "wine/unicode.h"
44
45 WINE_DEFAULT_DEBUG_CHANNEL(cryptui);
46
47 static HINSTANCE hInstance;
48
49 BOOL WINAPI DllMain(HINSTANCE hinstDLL, DWORD fdwReason, LPVOID lpvReserved)
50 {
51 TRACE("(0x%p, %d, %p)\n", hinstDLL, fdwReason, lpvReserved);
52
53 switch (fdwReason)
54 {
55 case DLL_WINE_PREATTACH:
56 return FALSE; /* prefer native version */
57 case DLL_PROCESS_ATTACH:
58 hInstance = hinstDLL;
59 DisableThreadLibraryCalls(hinstDLL);
60 break;
61 }
62 return TRUE;
63 }
64
65 #define MAX_STRING_LEN 512
66
67 static void add_cert_columns(HWND hwnd)
68 {
69 HWND lv = GetDlgItem(hwnd, IDC_MGR_CERTS);
70 RECT rc;
71 WCHAR buf[MAX_STRING_LEN];
72 LVCOLUMNW column;
73
74 SendMessageW(lv, LVM_SETEXTENDEDLISTVIEWSTYLE, 0, LVS_EX_FULLROWSELECT);
75 GetWindowRect(lv, &rc);
76 LoadStringW(hInstance, IDS_SUBJECT_COLUMN, buf,
77 sizeof(buf) / sizeof(buf[0]));
78 column.mask = LVCF_WIDTH | LVCF_TEXT;
79 column.cx = (rc.right - rc.left) * 29 / 100 - 2;
80 column.pszText = buf;
81 SendMessageW(lv, LVM_INSERTCOLUMNW, 0, (LPARAM)&column);
82 LoadStringW(hInstance, IDS_ISSUER_COLUMN, buf,
83 sizeof(buf) / sizeof(buf[0]));
84 SendMessageW(lv, LVM_INSERTCOLUMNW, 1, (LPARAM)&column);
85 column.cx = (rc.right - rc.left) * 16 / 100 - 2;
86 LoadStringW(hInstance, IDS_EXPIRATION_COLUMN, buf,
87 sizeof(buf) / sizeof(buf[0]));
88 SendMessageW(lv, LVM_INSERTCOLUMNW, 2, (LPARAM)&column);
89 column.cx = (rc.right - rc.left) * 23 / 100 - 1;
90 LoadStringW(hInstance, IDS_FRIENDLY_NAME_COLUMN, buf,
91 sizeof(buf) / sizeof(buf[0]));
92 SendMessageW(lv, LVM_INSERTCOLUMNW, 3, (LPARAM)&column);
93 }
94
95 static void add_cert_to_view(HWND lv, PCCERT_CONTEXT cert, DWORD *allocatedLen,
96 LPWSTR *str)
97 {
98 DWORD len;
99 LVITEMW item;
100 WCHAR dateFmt[80]; /* sufficient for LOCALE_SSHORTDATE */
101 WCHAR date[80];
102 SYSTEMTIME sysTime;
103 LPWSTR none;
104
105 item.mask = LVIF_IMAGE | LVIF_PARAM | LVIF_TEXT;
106 item.iItem = SendMessageW(lv, LVM_GETITEMCOUNT, 0, 0);
107 item.iSubItem = 0;
108 item.iImage = 0;
109 item.lParam = (LPARAM)CertDuplicateCertificateContext(cert);
110 len = CertGetNameStringW(cert, CERT_NAME_SIMPLE_DISPLAY_TYPE, 0, NULL,
111 NULL, 0);
112 if (len > *allocatedLen)
113 {
114 HeapFree(GetProcessHeap(), 0, *str);
115 *str = HeapAlloc(GetProcessHeap(), 0, len * sizeof(WCHAR));
116 if (*str)
117 *allocatedLen = len;
118 }
119 if (*str)
120 {
121 CertGetNameStringW(cert, CERT_NAME_SIMPLE_DISPLAY_TYPE, 0, NULL,
122 *str, len);
123 item.pszText = *str;
124 SendMessageW(lv, LVM_INSERTITEMW, 0, (LPARAM)&item);
125 }
126
127 item.mask = LVIF_TEXT;
128 len = CertGetNameStringW(cert, CERT_NAME_SIMPLE_DISPLAY_TYPE,
129 CERT_NAME_ISSUER_FLAG, NULL, NULL, 0);
130 if (len > *allocatedLen)
131 {
132 HeapFree(GetProcessHeap(), 0, *str);
133 *str = HeapAlloc(GetProcessHeap(), 0, len * sizeof(WCHAR));
134 if (*str)
135 *allocatedLen = len;
136 }
137 if (*str)
138 {
139 CertGetNameStringW(cert, CERT_NAME_SIMPLE_DISPLAY_TYPE,
140 CERT_NAME_ISSUER_FLAG, NULL, *str, len);
141 item.pszText = *str;
142 item.iSubItem = 1;
143 SendMessageW(lv, LVM_SETITEMTEXTW, item.iItem, (LPARAM)&item);
144 }
145
146 GetLocaleInfoW(LOCALE_SYSTEM_DEFAULT, LOCALE_SSHORTDATE, dateFmt,
147 sizeof(dateFmt) / sizeof(dateFmt[0]));
148 FileTimeToSystemTime(&cert->pCertInfo->NotAfter, &sysTime);
149 GetDateFormatW(LOCALE_SYSTEM_DEFAULT, 0, &sysTime, dateFmt, date,
150 sizeof(date) / sizeof(date[0]));
151 item.pszText = date;
152 item.iSubItem = 2;
153 SendMessageW(lv, LVM_SETITEMTEXTW, item.iItem, (LPARAM)&item);
154
155 if (!CertGetCertificateContextProperty(cert, CERT_FRIENDLY_NAME_PROP_ID,
156 NULL, &len))
157 len = LoadStringW(hInstance, IDS_FRIENDLY_NAME_NONE, (LPWSTR)&none, 0);
158 if (len > *allocatedLen)
159 {
160 HeapFree(GetProcessHeap(), 0, *str);
161 *str = HeapAlloc(GetProcessHeap(), 0, len * sizeof(WCHAR));
162 if (*str)
163 *allocatedLen = len;
164 }
165 if (*str)
166 {
167 if (!CertGetCertificateContextProperty(cert, CERT_FRIENDLY_NAME_PROP_ID,
168 *str, &len))
169 item.pszText = none;
170 else
171 item.pszText = *str;
172 item.iSubItem = 3;
173 SendMessageW(lv, LVM_SETITEMTEXTW, item.iItem, (LPARAM)&item);
174 }
175 }
176
177 static LPSTR get_cert_mgr_usages(void)
178 {
179 static const WCHAR keyName[] = { 'S','o','f','t','w','a','r','e','\\','M',
180 'i','c','r','o','s','o','f','t','\\','C','r','y','p','t','o','g','r','a',
181 'p','h','y','\\','U','I','\\','C','e','r','t','m','g','r','\\','P','u',
182 'r','p','o','s','e',0 };
183 LPSTR str = NULL;
184 HKEY key;
185
186 if (!RegCreateKeyExW(HKEY_CURRENT_USER, keyName, 0, NULL, 0, KEY_READ,
187 NULL, &key, NULL))
188 {
189 LONG rc;
190 DWORD type, size;
191
192 rc = RegQueryValueExA(key, "Purpose", NULL, &type, NULL, &size);
193 if ((!rc || rc == ERROR_MORE_DATA) && type == REG_SZ)
194 {
195 str = HeapAlloc(GetProcessHeap(), 0, size);
196 if (str)
197 {
198 rc = RegQueryValueExA(key, "Purpose", NULL, NULL, (LPBYTE)str,
199 &size);
200 if (rc)
201 {
202 HeapFree(GetProcessHeap(), 0, str);
203 str = NULL;
204 }
205 }
206 }
207 RegCloseKey(key);
208 }
209 return str;
210 }
211
212 typedef enum {
213 PurposeFilterShowAll = 0,
214 PurposeFilterShowAdvanced = 1,
215 PurposeFilterShowOID = 2
216 } PurposeFilter;
217
218 static void initialize_purpose_selection(HWND hwnd)
219 {
220 HWND cb = GetDlgItem(hwnd, IDC_MGR_PURPOSE_SELECTION);
221 WCHAR buf[MAX_STRING_LEN];
222 LPSTR usages;
223 int index;
224
225 LoadStringW(hInstance, IDS_PURPOSE_ALL, buf,
226 sizeof(buf) / sizeof(buf[0]));
227 index = SendMessageW(cb, CB_INSERTSTRING, -1, (LPARAM)buf);
228 SendMessageW(cb, CB_SETITEMDATA, index, (LPARAM)PurposeFilterShowAll);
229 LoadStringW(hInstance, IDS_PURPOSE_ADVANCED, buf,
230 sizeof(buf) / sizeof(buf[0]));
231 index = SendMessageW(cb, CB_INSERTSTRING, -1, (LPARAM)buf);
232 SendMessageW(cb, CB_SETITEMDATA, index, (LPARAM)PurposeFilterShowAdvanced);
233 SendMessageW(cb, CB_SETCURSEL, 0, 0);
234 if ((usages = get_cert_mgr_usages()))
235 {
236 LPSTR ptr, comma;
237
238 for (ptr = usages, comma = strchr(ptr, ','); ptr && *ptr;
239 ptr = comma ? comma + 1 : NULL,
240 comma = ptr ? strchr(ptr, ',') : NULL)
241 {
242 PCCRYPT_OID_INFO info;
243
244 if (comma)
245 *comma = 0;
246 if ((info = CryptFindOIDInfo(CRYPT_OID_INFO_OID_KEY, ptr, 0)))
247 {
248 index = SendMessageW(cb, CB_INSERTSTRING, 0,
249 (LPARAM)info->pwszName);
250 SendMessageW(cb, CB_SETITEMDATA, index, (LPARAM)info);
251 }
252 }
253 HeapFree(GetProcessHeap(), 0, usages);
254 }
255 }
256
257 extern BOOL WINAPI WTHelperGetKnownUsages(DWORD action,
258 PCCRYPT_OID_INFO **usages);
259
260 static CERT_ENHKEY_USAGE *add_oid_to_usage(CERT_ENHKEY_USAGE *usage, LPSTR oid)
261 {
262 if (!usage->cUsageIdentifier)
263 usage->rgpszUsageIdentifier = HeapAlloc(GetProcessHeap(), 0,
264 sizeof(LPSTR));
265 else
266 usage->rgpszUsageIdentifier = HeapReAlloc(GetProcessHeap(), 0,
267 usage->rgpszUsageIdentifier,
268 (usage->cUsageIdentifier + 1) * sizeof(LPSTR));
269 if (usage->rgpszUsageIdentifier)
270 usage->rgpszUsageIdentifier[usage->cUsageIdentifier++] = oid;
271 else
272 {
273 HeapFree(GetProcessHeap(), 0, usage);
274 usage = NULL;
275 }
276 return usage;
277 }
278
279 static CERT_ENHKEY_USAGE *convert_usages_str_to_usage(LPSTR usageStr)
280 {
281 CERT_ENHKEY_USAGE *usage = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY,
282 sizeof(CERT_ENHKEY_USAGE));
283
284 if (usage)
285 {
286 LPSTR ptr, comma;
287
288 for (ptr = usageStr, comma = strchr(ptr, ','); usage && ptr && *ptr;
289 ptr = comma ? comma + 1 : NULL,
290 comma = ptr ? strchr(ptr, ',') : NULL)
291 {
292 if (comma)
293 *comma = 0;
294 usage = add_oid_to_usage(usage, ptr);
295 }
296 }
297 return usage;
298 }
299
300 static CERT_ENHKEY_USAGE *create_advanced_filter(void)
301 {
302 CERT_ENHKEY_USAGE *advancedUsage = HeapAlloc(GetProcessHeap(),
303 HEAP_ZERO_MEMORY, sizeof(CERT_ENHKEY_USAGE));
304
305 if (advancedUsage)
306 {
307 PCCRYPT_OID_INFO *usages;
308
309 if (WTHelperGetKnownUsages(1, &usages))
310 {
311 LPSTR disabledUsagesStr;
312
313 if ((disabledUsagesStr = get_cert_mgr_usages()))
314 {
315 CERT_ENHKEY_USAGE *disabledUsages =
316 convert_usages_str_to_usage(disabledUsagesStr);
317
318 if (disabledUsages)
319 {
320 PCCRYPT_OID_INFO *ptr;
321
322 for (ptr = usages; advancedUsage && *ptr; ptr++)
323 {
324 DWORD i;
325 BOOL disabled = FALSE;
326
327 for (i = 0; !disabled &&
328 i < disabledUsages->cUsageIdentifier; i++)
329 if (!strcmp(disabledUsages->rgpszUsageIdentifier[i],
330 (*ptr)->pszOID))
331 disabled = TRUE;
332 if (!disabled)
333 advancedUsage = add_oid_to_usage(advancedUsage,
334 (LPSTR)(*ptr)->pszOID);
335 }
336 /* The individual strings are pointers to disabledUsagesStr,
337 * so they're freed when it is.
338 */
339 HeapFree(GetProcessHeap(), 0,
340 disabledUsages->rgpszUsageIdentifier);
341 HeapFree(GetProcessHeap(), 0, disabledUsages);
342 }
343 HeapFree(GetProcessHeap(), 0, disabledUsagesStr);
344 }
345 WTHelperGetKnownUsages(2, &usages);
346 }
347 }
348 return advancedUsage;
349 }
350
351 static int CALLBACK cert_mgr_sort_by_subject(LPARAM lp1, LPARAM lp2, LPARAM lp);
352
353 static void show_store_certs(HWND hwnd, HCERTSTORE store)
354 {
355 HWND lv = GetDlgItem(hwnd, IDC_MGR_CERTS);
356 HWND cb = GetDlgItem(hwnd, IDC_MGR_PURPOSE_SELECTION);
357 PCCERT_CONTEXT cert = NULL;
358 DWORD allocatedLen = 0;
359 LPWSTR str = NULL;
360 int index;
361 PurposeFilter filter = PurposeFilterShowAll;
362 LPCSTR oid = NULL;
363 CERT_ENHKEY_USAGE *advanced = NULL;
364
365 index = SendMessageW(cb, CB_GETCURSEL, 0, 0);
366 if (index >= 0)
367 {
368 INT_PTR data = SendMessageW(cb, CB_GETITEMDATA, index, 0);
369
370 if (!HIWORD(data))
371 filter = data;
372 else
373 {
374 PCCRYPT_OID_INFO info = (PCCRYPT_OID_INFO)data;
375
376 filter = PurposeFilterShowOID;
377 oid = info->pszOID;
378 }
379 }
380 if (filter == PurposeFilterShowAdvanced)
381 advanced = create_advanced_filter();
382 do {
383 cert = CertEnumCertificatesInStore(store, cert);
384 if (cert)
385 {
386 BOOL show = FALSE;
387
388 if (filter == PurposeFilterShowAll)
389 show = TRUE;
390 else
391 {
392 int numOIDs;
393 DWORD cbOIDs = 0;
394
395 if (CertGetValidUsages(1, &cert, &numOIDs, NULL, &cbOIDs))
396 {
397 if (numOIDs == -1)
398 {
399 /* -1 implies all usages are valid */
400 show = TRUE;
401 }
402 else
403 {
404 LPSTR *oids = HeapAlloc(GetProcessHeap(), 0, cbOIDs);
405
406 if (oids)
407 {
408 if (CertGetValidUsages(1, &cert, &numOIDs, oids,
409 &cbOIDs))
410 {
411 int i;
412
413 if (filter == PurposeFilterShowOID)
414 {
415 for (i = 0; !show && i < numOIDs; i++)
416 if (!strcmp(oids[i], oid))
417 show = TRUE;
418 }
419 else
420 {
421 for (i = 0; !show && i < numOIDs; i++)
422 {
423 DWORD j;
424
425 for (j = 0; !show &&
426 j < advanced->cUsageIdentifier; j++)
427 if (!strcmp(oids[i],
428 advanced->rgpszUsageIdentifier[j]))
429 show = TRUE;
430 }
431 }
432 }
433 HeapFree(GetProcessHeap(), 0, oids);
434 }
435 }
436 }
437 }
438 if (show)
439 add_cert_to_view(lv, cert, &allocatedLen, &str);
440 }
441 } while (cert);
442 HeapFree(GetProcessHeap(), 0, str);
443 if (advanced)
444 {
445 HeapFree(GetProcessHeap(), 0, advanced->rgpszUsageIdentifier);
446 HeapFree(GetProcessHeap(), 0, advanced);
447 }
448 SendMessageW(lv, LVM_SORTITEMSEX, (WPARAM)lv,
449 (LPARAM)cert_mgr_sort_by_subject);
450 }
451
452 static const WCHAR my[] = { 'M','y',0 };
453 static const WCHAR addressBook[] = {
454 'A','d','d','r','e','s','s','B','o','o','k',0 };
455 static const WCHAR ca[] = { 'C','A',0 };
456 static const WCHAR root[] = { 'R','o','o','t',0 };
457 static const WCHAR trustedPublisher[] = {
458 'T','r','u','s','t','e','d','P','u','b','l','i','s','h','e','r',0 };
459 static const WCHAR disallowed[] = { 'D','i','s','a','l','l','o','w','e','d',0 };
460
461 struct CertMgrStoreInfo
462 {
463 LPCWSTR name;
464 int removeWarning;
465 int removePluralWarning;
466 };
467
468 static const struct CertMgrStoreInfo defaultStoreList[] = {
469 { my, IDS_WARN_REMOVE_MY, IDS_WARN_REMOVE_PLURAL_MY },
470 { addressBook, IDS_WARN_REMOVE_ADDRESSBOOK,
471 IDS_WARN_REMOVE_PLURAL_ADDRESSBOOK },
472 { ca, IDS_WARN_REMOVE_CA, IDS_WARN_REMOVE_PLURAL_CA },
473 { root, IDS_WARN_REMOVE_ROOT, IDS_WARN_REMOVE_PLURAL_ROOT },
474 { trustedPublisher, IDS_WARN_REMOVE_TRUSTEDPUBLISHER,
475 IDS_WARN_REMOVE_PLURAL_TRUSTEDPUBLISHER },
476 { disallowed, IDS_WARN_REMOVE_DEFAULT },
477 };
478
479 static const struct CertMgrStoreInfo publisherStoreList[] = {
480 { root, IDS_WARN_REMOVE_ROOT, IDS_WARN_REMOVE_PLURAL_ROOT },
481 { trustedPublisher, IDS_WARN_REMOVE_TRUSTEDPUBLISHER,
482 IDS_WARN_REMOVE_PLURAL_TRUSTEDPUBLISHER },
483 { disallowed, IDS_WARN_REMOVE_PLURAL_DEFAULT },
484 };
485
486 struct CertMgrData
487 {
488 HIMAGELIST imageList;
489 LPCWSTR title;
490 DWORD nStores;
491 const struct CertMgrStoreInfo *stores;
492 };
493
494 static void show_cert_stores(HWND hwnd, DWORD dwFlags, struct CertMgrData *data)
495 {
496 const struct CertMgrStoreInfo *storeList;
497 int cStores, i;
498 HWND tab = GetDlgItem(hwnd, IDC_MGR_STORES);
499
500 if (dwFlags & CRYPTUI_CERT_MGR_PUBLISHER_TAB)
501 {
502 storeList = publisherStoreList;
503 cStores = sizeof(publisherStoreList) / sizeof(publisherStoreList[0]);
504 }
505 else
506 {
507 storeList = defaultStoreList;
508 cStores = sizeof(defaultStoreList) / sizeof(defaultStoreList[0]);
509 }
510 if (dwFlags & CRYPTUI_CERT_MGR_SINGLE_TAB_FLAG)
511 cStores = 1;
512 data->nStores = cStores;
513 data->stores = storeList;
514 for (i = 0; i < cStores; i++)
515 {
516 LPCWSTR name;
517 TCITEMW item;
518 HCERTSTORE store;
519
520 if (!(name = CryptFindLocalizedName(storeList[i].name)))
521 name = storeList[i].name;
522 store = CertOpenStore(CERT_STORE_PROV_SYSTEM_W, 0, 0,
523 CERT_SYSTEM_STORE_CURRENT_USER, storeList[i].name);
524 item.mask = TCIF_TEXT | TCIF_PARAM;
525 item.pszText = (LPWSTR)name;
526 item.lParam = (LPARAM)store;
527 SendMessageW(tab, TCM_INSERTITEMW, i, (LPARAM)&item);
528 }
529 }
530
531 static void free_certs(HWND lv)
532 {
533 LVITEMW item;
534 int items = SendMessageW(lv, LVM_GETITEMCOUNT, 0, 0), i;
535
536 for (i = 0; i < items; i++)
537 {
538 item.mask = LVIF_PARAM;
539 item.iItem = i;
540 item.iSubItem = 0;
541 SendMessageW(lv, LVM_GETITEMW, 0, (LPARAM)&item);
542 CertFreeCertificateContext((PCCERT_CONTEXT)item.lParam);
543 }
544 }
545
546 static HCERTSTORE cert_mgr_index_to_store(HWND tab, int index)
547 {
548 TCITEMW item;
549
550 item.mask = TCIF_PARAM;
551 SendMessageW(tab, TCM_GETITEMW, index, (LPARAM)&item);
552 return (HCERTSTORE)item.lParam;
553 }
554
555 static HCERTSTORE cert_mgr_current_store(HWND hwnd)
556 {
557 HWND tab = GetDlgItem(hwnd, IDC_MGR_STORES);
558
559 return cert_mgr_index_to_store(tab, SendMessageW(tab, TCM_GETCURSEL, 0, 0));
560 }
561
562 static void close_stores(HWND tab)
563 {
564 int i, tabs = SendMessageW(tab, TCM_GETITEMCOUNT, 0, 0);
565
566 for (i = 0; i < tabs; i++)
567 CertCloseStore(cert_mgr_index_to_store(tab, i), 0);
568 }
569
570 static void refresh_store_certs(HWND hwnd)
571 {
572 HWND lv = GetDlgItem(hwnd, IDC_MGR_CERTS);
573
574 free_certs(lv);
575 SendMessageW(lv, LVM_DELETEALLITEMS, 0, 0);
576 show_store_certs(hwnd, cert_mgr_current_store(hwnd));
577 }
578
579 typedef enum {
580 CheckBitmapIndexUnchecked = 1,
581 CheckBitmapIndexChecked = 2,
582 CheckBitmapIndexDisabledUnchecked = 3,
583 CheckBitmapIndexDisabledChecked = 4
584 } CheckBitmapIndex;
585
586 static void add_known_usage(HWND lv, PCCRYPT_OID_INFO info,
587 CheckBitmapIndex state)
588 {
589 LVITEMW item;
590
591 item.mask = LVIF_TEXT | LVIF_STATE | LVIF_PARAM;
592 item.state = INDEXTOSTATEIMAGEMASK(state);
593 item.stateMask = LVIS_STATEIMAGEMASK;
594 item.iItem = SendMessageW(lv, LVM_GETITEMCOUNT, 0, 0);
595 item.iSubItem = 0;
596 item.lParam = (LPARAM)info;
597 item.pszText = (LPWSTR)info->pwszName;
598 SendMessageW(lv, LVM_INSERTITEMW, 0, (LPARAM)&item);
599 }
600
601 static void add_known_usages_to_list(HWND lv, CheckBitmapIndex state)
602 {
603 PCCRYPT_OID_INFO *usages;
604
605 if (WTHelperGetKnownUsages(1, &usages))
606 {
607 PCCRYPT_OID_INFO *ptr;
608
609 for (ptr = usages; *ptr; ptr++)
610 add_known_usage(lv, *ptr, state);
611 WTHelperGetKnownUsages(2, &usages);
612 }
613 }
614
615 static void toggle_usage(HWND hwnd, int iItem)
616 {
617 LVITEMW item;
618 int res;
619 HWND lv = GetDlgItem(hwnd, IDC_CERTIFICATE_USAGES);
620
621 item.mask = LVIF_STATE;
622 item.iItem = iItem;
623 item.iSubItem = 0;
624 item.stateMask = LVIS_STATEIMAGEMASK;
625 res = SendMessageW(lv, LVM_GETITEMW, 0, (LPARAM)&item);
626 if (res)
627 {
628 int state = item.state >> 12;
629
630 item.state = INDEXTOSTATEIMAGEMASK(
631 state == CheckBitmapIndexChecked ? CheckBitmapIndexUnchecked :
632 CheckBitmapIndexChecked);
633 SendMessageW(lv, LVM_SETITEMSTATE, iItem, (LPARAM)&item);
634 }
635 }
636
637 static LONG_PTR find_oid_in_list(HWND lv, LPCSTR oid)
638 {
639 PCCRYPT_OID_INFO oidInfo = CryptFindOIDInfo(CRYPT_OID_INFO_OID_KEY,
640 (void *)oid, CRYPT_ENHKEY_USAGE_OID_GROUP_ID);
641 LONG_PTR ret;
642
643 if (oidInfo)
644 {
645 LVFINDINFOW findInfo;
646
647 findInfo.flags = LVFI_PARAM;
648 findInfo.lParam = (LPARAM)oidInfo;
649 ret = SendMessageW(lv, LVM_FINDITEMW, -1, (LPARAM)&findInfo);
650 }
651 else
652 {
653 LVFINDINFOA findInfo;
654
655 findInfo.flags = LVFI_STRING;
656 findInfo.psz = oid;
657 ret = SendMessageW(lv, LVM_FINDITEMA, -1, (LPARAM)&findInfo);
658 }
659 return ret;
660 }
661
662 static void save_cert_mgr_usages(HWND hwnd)
663 {
664 static const WCHAR keyName[] = { 'S','o','f','t','w','a','r','e','\\','M',
665 'i','c','r','o','s','o','f','t','\\','C','r','y','p','t','o','g','r','a',
666 'p','h','y','\\','U','I','\\','C','e','r','t','m','g','r','\\','P','u',
667 'r','p','o','s','e',0 };
668 HKEY key;
669 HWND lv = GetDlgItem(hwnd, IDC_CERTIFICATE_USAGES);
670 int purposes = SendMessageW(lv, LVM_GETITEMCOUNT, 0, 0), i;
671 LVITEMW item;
672 LPSTR str = NULL;
673
674 item.mask = LVIF_STATE | LVIF_PARAM;
675 item.iSubItem = 0;
676 item.stateMask = LVIS_STATEIMAGEMASK;
677 for (i = 0; i < purposes; i++)
678 {
679 item.iItem = i;
680 if (SendMessageW(lv, LVM_GETITEMW, 0, (LPARAM)&item))
681 {
682 int state = item.state >> 12;
683
684 if (state == CheckBitmapIndexUnchecked)
685 {
686 CRYPT_OID_INFO *info = (CRYPT_OID_INFO *)item.lParam;
687 BOOL firstString = TRUE;
688
689 if (!str)
690 str = HeapAlloc(GetProcessHeap(), 0,
691 strlen(info->pszOID) + 1);
692 else
693 {
694 str = HeapReAlloc(GetProcessHeap(), 0, str,
695 strlen(str) + 1 + strlen(info->pszOID) + 1);
696 firstString = FALSE;
697 }
698 if (str)
699 {
700 LPSTR ptr = firstString ? str : str + strlen(str);
701
702 if (!firstString)
703 *ptr++ = ',';
704 strcpy(ptr, info->pszOID);
705 }
706 }
707 }
708 }
709 if (!RegCreateKeyExW(HKEY_CURRENT_USER, keyName, 0, NULL, 0, KEY_ALL_ACCESS,
710 NULL, &key, NULL))
711 {
712 if (str)
713 RegSetValueExA(key, "Purpose", 0, REG_SZ, (const BYTE *)str,
714 strlen(str) + 1);
715 else
716 RegDeleteValueA(key, "Purpose");
717 RegCloseKey(key);
718 }
719 HeapFree(GetProcessHeap(), 0, str);
720 }
721
722 static LRESULT CALLBACK cert_mgr_advanced_dlg_proc(HWND hwnd, UINT msg,
723 WPARAM wp, LPARAM lp)
724 {
725 switch (msg)
726 {
727 case WM_INITDIALOG:
728 {
729 RECT rc;
730 LVCOLUMNW column;
731 HWND lv = GetDlgItem(hwnd, IDC_CERTIFICATE_USAGES);
732 HIMAGELIST imageList;
733 LPSTR disabledUsages;
734
735 GetWindowRect(lv, &rc);
736 column.mask = LVCF_WIDTH;
737 column.cx = rc.right - rc.left;
738 SendMessageW(lv, LVM_INSERTCOLUMNW, 0, (LPARAM)&column);
739 imageList = ImageList_Create(16, 16, ILC_COLOR4 | ILC_MASK, 4, 0);
740 if (imageList)
741 {
742 HBITMAP bmp;
743 COLORREF backColor = RGB(255, 0, 255);
744
745 bmp = LoadBitmapW(hInstance, MAKEINTRESOURCEW(IDB_CHECKS));
746 ImageList_AddMasked(imageList, bmp, backColor);
747 DeleteObject(bmp);
748 ImageList_SetBkColor(imageList, CLR_NONE);
749 SendMessageW(lv, LVM_SETIMAGELIST, LVSIL_STATE, (LPARAM)imageList);
750 SetWindowLongPtrW(hwnd, DWLP_USER, (LPARAM)imageList);
751 }
752 add_known_usages_to_list(lv, CheckBitmapIndexChecked);
753 if ((disabledUsages = get_cert_mgr_usages()))
754 {
755 LPSTR ptr, comma;
756
757 for (ptr = disabledUsages, comma = strchr(ptr, ','); ptr && *ptr;
758 ptr = comma ? comma + 1 : NULL,
759 comma = ptr ? strchr(ptr, ',') : NULL)
760 {
761 LONG_PTR index;
762
763 if (comma)
764 *comma = 0;
765 if ((index = find_oid_in_list(lv, ptr)) != -1)
766 toggle_usage(hwnd, index);
767 }
768 HeapFree(GetProcessHeap(), 0, disabledUsages);
769 }
770 break;
771 }
772 case WM_NOTIFY:
773 {
774 NMHDR *hdr = (NMHDR *)lp;
775 NMITEMACTIVATE *nm;
776
777 switch (hdr->code)
778 {
779 case NM_CLICK:
780 nm = (NMITEMACTIVATE *)lp;
781 toggle_usage(hwnd, nm->iItem);
782 SendMessageW(GetParent(hwnd), PSM_CHANGED, (WPARAM)hwnd, 0);
783 break;
784 }
785 break;
786 }
787 case WM_COMMAND:
788 switch (wp)
789 {
790 case IDOK:
791 save_cert_mgr_usages(hwnd);
792 ImageList_Destroy((HIMAGELIST)GetWindowLongPtrW(hwnd, DWLP_USER));
793 EndDialog(hwnd, IDOK);
794 break;
795 case IDCANCEL:
796 ImageList_Destroy((HIMAGELIST)GetWindowLongPtrW(hwnd, DWLP_USER));
797 EndDialog(hwnd, IDCANCEL);
798 break;
799 }
800 break;
801 }
802 return 0;
803 }
804
805 static void cert_mgr_clear_cert_selection(HWND hwnd)
806 {
807 WCHAR empty[] = { 0 };
808
809 EnableWindow(GetDlgItem(hwnd, IDC_MGR_EXPORT), FALSE);
810 EnableWindow(GetDlgItem(hwnd, IDC_MGR_REMOVE), FALSE);
811 EnableWindow(GetDlgItem(hwnd, IDC_MGR_VIEW), FALSE);
812 SendMessageW(GetDlgItem(hwnd, IDC_MGR_PURPOSES), WM_SETTEXT, 0,
813 (LPARAM)empty);
814 refresh_store_certs(hwnd);
815 }
816
817 static PCCERT_CONTEXT cert_mgr_index_to_cert(HWND hwnd, int index)
818 {
819 PCCERT_CONTEXT cert = NULL;
820 LVITEMW item;
821
822 item.mask = LVIF_PARAM;
823 item.iItem = index;
824 item.iSubItem = 0;
825 if (SendMessageW(GetDlgItem(hwnd, IDC_MGR_CERTS), LVM_GETITEMW, 0,
826 (LPARAM)&item))
827 cert = (PCCERT_CONTEXT)item.lParam;
828 return cert;
829 }
830
831 static void show_selected_cert(HWND hwnd, int index)
832 {
833 PCCERT_CONTEXT cert = cert_mgr_index_to_cert(hwnd, index);
834
835 if (cert)
836 {
837 CRYPTUI_VIEWCERTIFICATE_STRUCTW viewInfo;
838
839 memset(&viewInfo, 0, sizeof(viewInfo));
840 viewInfo.dwSize = sizeof(viewInfo);
841 viewInfo.hwndParent = hwnd;
842 viewInfo.pCertContext = cert;
843 /* FIXME: this should be modal */
844 CryptUIDlgViewCertificateW(&viewInfo, NULL);
845 }
846 }
847
848 static void cert_mgr_show_cert_usages(HWND hwnd, int index)
849 {
850 HWND text = GetDlgItem(hwnd, IDC_MGR_PURPOSES);
851 PCCERT_CONTEXT cert = cert_mgr_index_to_cert(hwnd, index);
852 PCERT_ENHKEY_USAGE usage;
853 DWORD size;
854
855 /* Get enhanced key usage. Have to check for a property and an extension
856 * separately, because CertGetEnhancedKeyUsage will succeed and return an
857 * empty usage if neither is set. Unfortunately an empty usage implies
858 * no usage is allowed, so we have to distinguish between the two cases.
859 */
860 if (CertGetEnhancedKeyUsage(cert, CERT_FIND_PROP_ONLY_ENHKEY_USAGE_FLAG,
861 NULL, &size))
862 {
863 usage = HeapAlloc(GetProcessHeap(), 0, size);
864 if (!CertGetEnhancedKeyUsage(cert,
865 CERT_FIND_PROP_ONLY_ENHKEY_USAGE_FLAG, usage, &size))
866 {
867 HeapFree(GetProcessHeap(), 0, usage);
868 usage = NULL;
869 }
870 }
871 else if (CertGetEnhancedKeyUsage(cert, CERT_FIND_EXT_ONLY_ENHKEY_USAGE_FLAG,
872 NULL, &size))
873 {
874 usage = HeapAlloc(GetProcessHeap(), 0, size);
875 if (!CertGetEnhancedKeyUsage(cert,
876 CERT_FIND_EXT_ONLY_ENHKEY_USAGE_FLAG, usage, &size))
877 {
878 HeapFree(GetProcessHeap(), 0, usage);
879 usage = NULL;
880 }
881 }
882 else
883 usage = NULL;
884 if (usage)
885 {
886 if (usage->cUsageIdentifier)
887 {
888 static const WCHAR commaSpace[] = { ',',' ',0 };
889 DWORD i, len = 1;
890 LPWSTR str, ptr;
891
892 for (i = 0; i < usage->cUsageIdentifier; i++)
893 {
894 PCCRYPT_OID_INFO info =
895 CryptFindOIDInfo(CRYPT_OID_INFO_OID_KEY,
896 usage->rgpszUsageIdentifier[i],
897 CRYPT_ENHKEY_USAGE_OID_GROUP_ID);
898
899 if (info)
900 len += strlenW(info->pwszName);
901 else
902 len += strlen(usage->rgpszUsageIdentifier[i]);
903 if (i < usage->cUsageIdentifier - 1)
904 len += strlenW(commaSpace);
905 }
906 str = HeapAlloc(GetProcessHeap(), 0, len * sizeof(WCHAR));
907 if (str)
908 {
909 for (i = 0, ptr = str; i < usage->cUsageIdentifier; i++)
910 {
911 PCCRYPT_OID_INFO info =
912 CryptFindOIDInfo(CRYPT_OID_INFO_OID_KEY,
913 usage->rgpszUsageIdentifier[i],
914 CRYPT_ENHKEY_USAGE_OID_GROUP_ID);
915
916 if (info)
917 {
918 strcpyW(ptr, info->pwszName);
919 ptr += strlenW(info->pwszName);
920 }
921 else
922 {
923 LPCSTR src = usage->rgpszUsageIdentifier[i];
924
925 for (; *src; ptr++, src++)
926 *ptr = *src;
927 *ptr = 0;
928 }
929 if (i < usage->cUsageIdentifier - 1)
930 {
931 strcpyW(ptr, commaSpace);
932 ptr += strlenW(commaSpace);
933 }
934 }
935 *ptr = 0;
936 SendMessageW(text, WM_SETTEXT, 0, (LPARAM)str);
937 HeapFree(GetProcessHeap(), 0, str);
938 }
939 HeapFree(GetProcessHeap(), 0, usage);
940 }
941 else
942 {
943 WCHAR buf[MAX_STRING_LEN];
944
945 LoadStringW(hInstance, IDS_ALLOWED_PURPOSE_NONE, buf,
946 sizeof(buf) / sizeof(buf[0]));
947 SendMessageW(text, WM_SETTEXT, 0, (LPARAM)buf);
948 }
949 }
950 else
951 {
952 WCHAR buf[MAX_STRING_LEN];
953
954 LoadStringW(hInstance, IDS_ALLOWED_PURPOSE_ALL, buf,
955 sizeof(buf) / sizeof(buf[0]));
956 SendMessageW(text, WM_SETTEXT, 0, (LPARAM)buf);
957 }
958 }
959
960 static void cert_mgr_do_remove(HWND hwnd)
961 {
962 int tabIndex = SendMessageW(GetDlgItem(hwnd, IDC_MGR_STORES),
963 TCM_GETCURSEL, 0, 0);
964 struct CertMgrData *data =
965 (struct CertMgrData *)GetWindowLongPtrW(hwnd, DWLP_USER);
966
967 if (tabIndex < data->nStores)
968 {
969 HWND lv = GetDlgItem(hwnd, IDC_MGR_CERTS);
970 WCHAR warning[MAX_STRING_LEN], title[MAX_STRING_LEN];
971 LPCWSTR pTitle;
972 int warningID;
973
974 if (SendMessageW(lv, LVM_GETSELECTEDCOUNT, 0, 0) > 1)
975 warningID = data->stores[tabIndex].removePluralWarning;
976 else
977 warningID = data->stores[tabIndex].removeWarning;
978 if (data->title)
979 pTitle = data->title;
980 else
981 {
982 LoadStringW(hInstance, IDS_CERT_MGR, title,
983 sizeof(title) / sizeof(title[0]));
984 pTitle = title;
985 }
986 LoadStringW(hInstance, warningID, warning,
987 sizeof(warning) / sizeof(warning[0]));
988 if (MessageBoxW(hwnd, warning, pTitle, MB_YESNO) == IDYES)
989 {
990 int selection = -1;
991
992 do {
993 selection = SendMessageW(lv, LVM_GETNEXTITEM, selection,
994 LVNI_SELECTED);
995 if (selection >= 0)
996 {
997 PCCERT_CONTEXT cert = cert_mgr_index_to_cert(hwnd,
998 selection);
999
1000 CertDeleteCertificateFromStore(cert);
1001 }
1002 } while (selection >= 0);
1003 cert_mgr_clear_cert_selection(hwnd);
1004 }
1005 }
1006 }
1007
1008 static void cert_mgr_do_export(HWND hwnd)
1009 {
1010 HWND lv = GetDlgItem(hwnd, IDC_MGR_CERTS);
1011 int selectionCount = SendMessageW(lv, LVM_GETSELECTEDCOUNT, 0, 0);
1012
1013 if (selectionCount == 1)
1014 {
1015 int selection = SendMessageW(lv, LVM_GETNEXTITEM, -1,
1016 LVNI_SELECTED);
1017
1018 if (selection >= 0)
1019 {
1020 PCCERT_CONTEXT cert = cert_mgr_index_to_cert(hwnd, selection);
1021
1022 if (cert)
1023 {
1024 CRYPTUI_WIZ_EXPORT_INFO info;
1025
1026 info.dwSize = sizeof(info);
1027 info.pwszExportFileName = NULL;
1028 info.dwSubjectChoice = CRYPTUI_WIZ_EXPORT_CERT_CONTEXT;
1029 info.u.pCertContext = cert;
1030 info.cStores = 0;
1031 CryptUIWizExport(0, hwnd, NULL, &info, NULL);
1032 }
1033 }
1034 }
1035 else if (selectionCount > 1)
1036 {
1037 HCERTSTORE store = CertOpenStore(CERT_STORE_PROV_MEMORY, 0, 0,
1038 CERT_STORE_CREATE_NEW_FLAG, NULL);
1039
1040 if (store)
1041 {
1042 CRYPTUI_WIZ_EXPORT_INFO info;
1043 int selection = -1;
1044
1045 info.dwSize = sizeof(info);
1046 info.pwszExportFileName = NULL;
1047 info.dwSubjectChoice =
1048 CRYPTUI_WIZ_EXPORT_CERT_STORE_CERTIFICATES_ONLY;
1049 info.u.hCertStore = store;
1050 info.cStores = 0;
1051 do {
1052 selection = SendMessageW(lv, LVM_GETNEXTITEM, selection,
1053 LVNI_SELECTED);
1054 if (selection >= 0)
1055 {
1056 PCCERT_CONTEXT cert = cert_mgr_index_to_cert(hwnd,
1057 selection);
1058
1059 CertAddCertificateContextToStore(store, cert,
1060 CERT_STORE_ADD_ALWAYS, NULL);
1061 }
1062 } while (selection >= 0);
1063 CryptUIWizExport(0, hwnd, NULL, &info, NULL);
1064 CertCloseStore(store, 0);
1065 }
1066 }
1067 }
1068
1069 static int cert_mgr_sort_by_text(HWND lv, int col, int index1, int index2)
1070 {
1071 LVITEMW item;
1072 WCHAR buf1[MAX_STRING_LEN];
1073 WCHAR buf2[MAX_STRING_LEN];
1074
1075 item.cchTextMax = sizeof(buf1) / sizeof(buf1[0]);
1076 item.mask = LVIF_TEXT;
1077 item.pszText = buf1;
1078 item.iItem = index1;
1079 item.iSubItem = col;
1080 SendMessageW(lv, LVM_GETITEMW, 0, (LPARAM)&item);
1081 item.pszText = buf2;
1082 item.iItem = index2;
1083 SendMessageW(lv, LVM_GETITEMW, 0, (LPARAM)&item);
1084 return strcmpW(buf1, buf2);
1085 }
1086
1087 static int CALLBACK cert_mgr_sort_by_subject(LPARAM lp1, LPARAM lp2, LPARAM lp)
1088 {
1089 return cert_mgr_sort_by_text((HWND)lp, 0, lp1, lp2);
1090 }
1091
1092 static int CALLBACK cert_mgr_sort_by_issuer(LPARAM lp1, LPARAM lp2, LPARAM lp)
1093 {
1094 return cert_mgr_sort_by_text((HWND)lp, 1, lp1, lp2);
1095 }
1096
1097 static int CALLBACK cert_mgr_sort_by_date(LPARAM lp1, LPARAM lp2, LPARAM lp)
1098 {
1099 PCCERT_CONTEXT cert1 = (PCCERT_CONTEXT)lp1;
1100 PCCERT_CONTEXT cert2 = (PCCERT_CONTEXT)lp2;
1101 return CompareFileTime(&cert1->pCertInfo->NotAfter,
1102 &cert2->pCertInfo->NotAfter);
1103 }
1104
1105 static int CALLBACK cert_mgr_sort_by_friendly_name(LPARAM lp1, LPARAM lp2,
1106 LPARAM lp)
1107 {
1108 return cert_mgr_sort_by_text((HWND)lp, 3, lp1, lp2);
1109 }
1110
1111 static LRESULT CALLBACK cert_mgr_dlg_proc(HWND hwnd, UINT msg, WPARAM wp,
1112 LPARAM lp)
1113 {
1114 struct CertMgrData *data;
1115
1116 switch (msg)
1117 {
1118 case WM_INITDIALOG:
1119 {
1120 PCCRYPTUI_CERT_MGR_STRUCT pCryptUICertMgr =
1121 (PCCRYPTUI_CERT_MGR_STRUCT)lp;
1122 HWND tab = GetDlgItem(hwnd, IDC_MGR_STORES);
1123
1124 data = HeapAlloc(GetProcessHeap(), 0, sizeof(struct CertMgrData));
1125 if (!data)
1126 return 0;
1127 data->imageList = ImageList_Create(16, 16, ILC_COLOR4 | ILC_MASK, 2, 0);
1128 if (data->imageList)
1129 {
1130 HBITMAP bmp;
1131 COLORREF backColor = RGB(255, 0, 255);
1132
1133 bmp = LoadBitmapW(hInstance, MAKEINTRESOURCEW(IDB_SMALL_ICONS));
1134 ImageList_AddMasked(data->imageList, bmp, backColor);
1135 DeleteObject(bmp);
1136 ImageList_SetBkColor(data->imageList, CLR_NONE);
1137 SendMessageW(GetDlgItem(hwnd, IDC_MGR_CERTS), LVM_SETIMAGELIST,
1138 LVSIL_SMALL, (LPARAM)data->imageList);
1139 }
1140 SetWindowLongPtrW(hwnd, DWLP_USER, (LPARAM)data);
1141 data->title = pCryptUICertMgr->pwszTitle;
1142
1143 initialize_purpose_selection(hwnd);
1144 add_cert_columns(hwnd);
1145 if (pCryptUICertMgr->pwszTitle)
1146 SendMessageW(hwnd, WM_SETTEXT, 0,
1147 (LPARAM)pCryptUICertMgr->pwszTitle);
1148 show_cert_stores(hwnd, pCryptUICertMgr->dwFlags, data);
1149 show_store_certs(hwnd, cert_mgr_index_to_store(tab, 0));
1150 break;
1151 }
1152 case WM_NOTIFY:
1153 {
1154 NMHDR *hdr = (NMHDR *)lp;
1155
1156 switch (hdr->code)
1157 {
1158 case TCN_SELCHANGE:
1159 cert_mgr_clear_cert_selection(hwnd);
1160 break;
1161 case LVN_ITEMCHANGED:
1162 {
1163 WCHAR empty[] = { 0 };
1164 NMITEMACTIVATE *nm = (NMITEMACTIVATE*)lp;
1165 HWND lv = GetDlgItem(hwnd, IDC_MGR_CERTS);
1166 int numSelected = SendMessageW(lv, LVM_GETSELECTEDCOUNT, 0, 0);
1167
1168 EnableWindow(GetDlgItem(hwnd, IDC_MGR_EXPORT), numSelected > 0);
1169 EnableWindow(GetDlgItem(hwnd, IDC_MGR_REMOVE), numSelected > 0);
1170 EnableWindow(GetDlgItem(hwnd, IDC_MGR_VIEW), numSelected == 1);
1171 if (numSelected == 1)
1172 cert_mgr_show_cert_usages(hwnd, nm->iItem);
1173 else
1174 SendMessageW(GetDlgItem(hwnd, IDC_MGR_PURPOSES), WM_SETTEXT, 0,
1175 (LPARAM)empty);
1176 break;
1177 }
1178 case NM_DBLCLK:
1179 show_selected_cert(hwnd, ((NMITEMACTIVATE *)lp)->iItem);
1180 break;
1181 case LVN_KEYDOWN:
1182 {
1183 NMLVKEYDOWN *lvk = (NMLVKEYDOWN *)lp;
1184
1185 if (lvk->wVKey == VK_DELETE)
1186 cert_mgr_do_remove(hwnd);
1187 break;
1188 }
1189 case LVN_COLUMNCLICK:
1190 {
1191 NMLISTVIEW *nmlv = (NMLISTVIEW *)lp;
1192 HWND lv = GetDlgItem(hwnd, IDC_MGR_CERTS);
1193
1194 /* FIXME: doesn't support swapping sort order between ascending
1195 * and descending.
1196 */
1197 switch (nmlv->iSubItem)
1198 {
1199 case 0:
1200 SendMessageW(lv, LVM_SORTITEMSEX, (WPARAM)lv,
1201 (LPARAM)cert_mgr_sort_by_subject);
1202 break;
1203 case 1:
1204 SendMessageW(lv, LVM_SORTITEMSEX, (WPARAM)lv,
1205 (LPARAM)cert_mgr_sort_by_issuer);
1206 break;
1207 case 2:
1208 SendMessageW(lv, LVM_SORTITEMS, 0,
1209 (LPARAM)cert_mgr_sort_by_date);
1210 break;
1211 case 3:
1212 SendMessageW(lv, LVM_SORTITEMSEX, (WPARAM)lv,
1213 (LPARAM)cert_mgr_sort_by_friendly_name);
1214 break;
1215 }
1216 break;
1217 }
1218 }
1219 break;
1220 }
1221 case WM_COMMAND:
1222 switch (wp)
1223 {
1224 case ((CBN_SELCHANGE << 16) | IDC_MGR_PURPOSE_SELECTION):
1225 cert_mgr_clear_cert_selection(hwnd);
1226 break;
1227 case IDC_MGR_IMPORT:
1228 if (CryptUIWizImport(0, hwnd, NULL, NULL,
1229 cert_mgr_current_store(hwnd)))
1230 refresh_store_certs(hwnd);
1231 break;
1232 case IDC_MGR_ADVANCED:
1233 if (DialogBoxW(hInstance, MAKEINTRESOURCEW(IDD_CERT_MGR_ADVANCED),
1234 hwnd, cert_mgr_advanced_dlg_proc) == IDOK)
1235 {
1236 HWND cb = GetDlgItem(hwnd, IDC_MGR_PURPOSE_SELECTION);
1237 int index, len;
1238 LPWSTR curString = NULL;
1239
1240 index = SendMessageW(cb, CB_GETCURSEL, 0, 0);
1241 if (index >= 0)
1242 {
1243 len = SendMessageW(cb, CB_GETLBTEXTLEN, index, 0);
1244 curString = HeapAlloc(GetProcessHeap(), 0,
1245 (len + 1) * sizeof(WCHAR));
1246 SendMessageW(cb, CB_GETLBTEXT, index, (LPARAM)curString);
1247 }
1248 SendMessageW(cb, CB_RESETCONTENT, 0, 0);
1249 initialize_purpose_selection(hwnd);
1250 if (curString)
1251 {
1252 index = SendMessageW(cb, CB_FINDSTRINGEXACT, -1,
1253 (LPARAM)curString);
1254 if (index >= 0)
1255 SendMessageW(cb, CB_SETCURSEL, index, 0);
1256 HeapFree(GetProcessHeap(), 0, curString);
1257 }
1258 refresh_store_certs(hwnd);
1259 }
1260 break;
1261 case IDC_MGR_VIEW:
1262 {
1263 HWND lv = GetDlgItem(hwnd, IDC_MGR_CERTS);
1264 int selection = SendMessageW(lv, LVM_GETNEXTITEM, -1,
1265 LVNI_SELECTED);
1266
1267 if (selection >= 0)
1268 show_selected_cert(hwnd, selection);
1269 break;
1270 }
1271 case IDC_MGR_EXPORT:
1272 cert_mgr_do_export(hwnd);
1273 break;
1274 case IDC_MGR_REMOVE:
1275 cert_mgr_do_remove(hwnd);
1276 break;
1277 case IDCANCEL:
1278 free_certs(GetDlgItem(hwnd, IDC_MGR_CERTS));
1279 close_stores(GetDlgItem(hwnd, IDC_MGR_STORES));
1280 data = (struct CertMgrData *)GetWindowLongPtrW(hwnd, DWLP_USER);
1281 ImageList_Destroy(data->imageList);
1282 HeapFree(GetProcessHeap(), 0, data);
1283 EndDialog(hwnd, IDCANCEL);
1284 break;
1285 }
1286 break;
1287 }
1288 return 0;
1289 }
1290
1291 /***********************************************************************
1292 * CryptUIDlgCertMgr (CRYPTUI.@)
1293 */
1294 BOOL WINAPI CryptUIDlgCertMgr(PCCRYPTUI_CERT_MGR_STRUCT pCryptUICertMgr)
1295 {
1296 TRACE("(%p)\n", pCryptUICertMgr);
1297
1298 if (pCryptUICertMgr->dwSize != sizeof(CRYPTUI_CERT_MGR_STRUCT))
1299 {
1300 WARN("unexpected size %d\n", pCryptUICertMgr->dwSize);
1301 SetLastError(E_INVALIDARG);
1302 return FALSE;
1303 }
1304 DialogBoxParamW(hInstance, MAKEINTRESOURCEW(IDD_CERT_MGR),
1305 pCryptUICertMgr->hwndParent, cert_mgr_dlg_proc, (LPARAM)pCryptUICertMgr);
1306 return TRUE;
1307 }
1308
1309 /* FIXME: real names are unknown, functions are undocumented */
1310 typedef struct _CRYPTUI_ENUM_SYSTEM_STORE_ARGS
1311 {
1312 DWORD dwFlags;
1313 void *pvSystemStoreLocationPara;
1314 } CRYPTUI_ENUM_SYSTEM_STORE_ARGS, *PCRYPTUI_ENUM_SYSTEM_STORE_ARGS;
1315
1316 typedef struct _CRYPTUI_ENUM_DATA
1317 {
1318 DWORD cStores;
1319 HCERTSTORE *rghStore;
1320 DWORD cEnumArgs;
1321 PCRYPTUI_ENUM_SYSTEM_STORE_ARGS rgEnumArgs;
1322 } CRYPTUI_ENUM_DATA, *PCRYPTUI_ENUM_DATA;
1323
1324 typedef BOOL (WINAPI *PFN_SELECTED_STORE_CB)(HCERTSTORE store, HWND hwnd,
1325 void *pvArg);
1326
1327 /* Values for dwFlags */
1328 #define CRYPTUI_ENABLE_SHOW_PHYSICAL_STORE 0x00000001
1329
1330 typedef struct _CRYPTUI_SELECTSTORE_INFO_A
1331 {
1332 DWORD dwSize;
1333 HWND parent;
1334 DWORD dwFlags;
1335 LPSTR pszTitle;
1336 LPSTR pszText;
1337 CRYPTUI_ENUM_DATA *pEnumData;
1338 PFN_SELECTED_STORE_CB pfnSelectedStoreCallback;
1339 void *pvArg;
1340 } CRYPTUI_SELECTSTORE_INFO_A, *PCRYPTUI_SELECTSTORE_INFO_A;
1341
1342 typedef struct _CRYPTUI_SELECTSTORE_INFO_W
1343 {
1344 DWORD dwSize;
1345 HWND parent;
1346 DWORD dwFlags;
1347 LPWSTR pwszTitle;
1348 LPWSTR pwszText;
1349 CRYPTUI_ENUM_DATA *pEnumData;
1350 PFN_SELECTED_STORE_CB pfnSelectedStoreCallback;
1351 void *pvArg;
1352 } CRYPTUI_SELECTSTORE_INFO_W, *PCRYPTUI_SELECTSTORE_INFO_W;
1353
1354 struct StoreInfo
1355 {
1356 enum {
1357 StoreHandle,
1358 SystemStore
1359 } type;
1360 union {
1361 HCERTSTORE store;
1362 LPWSTR name;
1363 } DUMMYUNIONNAME;
1364 };
1365
1366 static BOOL WINAPI enum_store_callback(const void *pvSystemStore,
1367 DWORD dwFlags, PCERT_SYSTEM_STORE_INFO pStoreInfo, void *pvReserved,
1368 void *pvArg)
1369 {
1370 HWND tree = GetDlgItem(pvArg, IDC_STORE_LIST);
1371 TVINSERTSTRUCTW tvis;
1372 LPCWSTR localizedName;
1373 BOOL ret = TRUE;
1374
1375 tvis.hParent = NULL;
1376 tvis.hInsertAfter = TVI_LAST;
1377 tvis.u.item.mask = TVIF_TEXT;
1378 if ((localizedName = CryptFindLocalizedName(pvSystemStore)))
1379 {
1380 struct StoreInfo *storeInfo = HeapAlloc(GetProcessHeap(), 0,
1381 sizeof(struct StoreInfo));
1382
1383 if (storeInfo)
1384 {
1385 storeInfo->type = SystemStore;
1386 storeInfo->u.name = HeapAlloc(GetProcessHeap(), 0,
1387 (strlenW(pvSystemStore) + 1) * sizeof(WCHAR));
1388 if (storeInfo->u.name)
1389 {
1390 tvis.u.item.mask |= TVIF_PARAM;
1391 tvis.u.item.lParam = (LPARAM)storeInfo;
1392 strcpyW(storeInfo->u.name, pvSystemStore);
1393 }
1394 else
1395 {
1396 HeapFree(GetProcessHeap(), 0, storeInfo);
1397 ret = FALSE;
1398 }
1399 }
1400 else
1401 ret = FALSE;
1402 tvis.u.item.pszText = (LPWSTR)localizedName;
1403 }
1404 else
1405 tvis.u.item.pszText = (LPWSTR)pvSystemStore;
1406 /* FIXME: need a folder icon for the store too */
1407 if (ret)
1408 SendMessageW(tree, TVM_INSERTITEMW, 0, (LPARAM)&tvis);
1409 return ret;
1410 }
1411
1412 static void enumerate_stores(HWND hwnd, CRYPTUI_ENUM_DATA *pEnumData)
1413 {
1414 DWORD i;
1415 HWND tree = GetDlgItem(hwnd, IDC_STORE_LIST);
1416
1417 for (i = 0; i < pEnumData->cEnumArgs; i++)
1418 CertEnumSystemStore(pEnumData->rgEnumArgs[i].dwFlags,
1419 pEnumData->rgEnumArgs[i].pvSystemStoreLocationPara,
1420 hwnd, enum_store_callback);
1421 for (i = 0; i < pEnumData->cStores; i++)
1422 {
1423 DWORD size;
1424
1425 if (CertGetStoreProperty(pEnumData->rghStore[i],
1426 CERT_STORE_LOCALIZED_NAME_PROP_ID, NULL, &size))
1427 {
1428 LPWSTR name = HeapAlloc(GetProcessHeap(), 0, size);
1429
1430 if (name)
1431 {
1432 if (CertGetStoreProperty(pEnumData->rghStore[i],
1433 CERT_STORE_LOCALIZED_NAME_PROP_ID, name, &size))
1434 {
1435 struct StoreInfo *storeInfo = HeapAlloc(GetProcessHeap(),
1436 0, sizeof(struct StoreInfo));
1437
1438 if (storeInfo)
1439 {
1440 TVINSERTSTRUCTW tvis;
1441
1442 storeInfo->type = StoreHandle;
1443 storeInfo->u.store = pEnumData->rghStore[i];
1444 tvis.hParent = NULL;
1445 tvis.hInsertAfter = TVI_LAST;
1446 tvis.u.item.mask = TVIF_TEXT | TVIF_PARAM;
1447 tvis.u.item.pszText = name;
1448 tvis.u.item.lParam = (LPARAM)storeInfo;
1449 SendMessageW(tree, TVM_INSERTITEMW, 0, (LPARAM)&tvis);
1450 }
1451 }
1452 HeapFree(GetProcessHeap(), 0, name);
1453 }
1454 }
1455 }
1456 }
1457
1458 static void free_store_info(HWND tree)
1459 {
1460 HTREEITEM next = (HTREEITEM)SendMessageW(tree, TVM_GETNEXTITEM, TVGN_CHILD,
1461 0);
1462
1463 while (next)
1464 {
1465 TVITEMW item;
1466
1467 memset(&item, 0, sizeof(item));
1468 item.mask = TVIF_HANDLE | TVIF_PARAM;
1469 item.hItem = next;
1470 SendMessageW(tree, TVM_GETITEMW, 0, (LPARAM)&item);
1471 if (item.lParam)
1472 {
1473 struct StoreInfo *storeInfo = (struct StoreInfo *)item.lParam;
1474
1475 if (storeInfo->type == SystemStore)
1476 HeapFree(GetProcessHeap(), 0, storeInfo->u.name);
1477 HeapFree(GetProcessHeap(), 0, storeInfo);
1478 }
1479 next = (HTREEITEM)SendMessageW(tree, TVM_GETNEXTITEM, TVGN_NEXT,
1480 (LPARAM)next);
1481 }
1482 }
1483
1484 static HCERTSTORE selected_item_to_store(HWND tree, HTREEITEM hItem)
1485 {
1486 WCHAR buf[MAX_STRING_LEN];
1487 TVITEMW item;
1488 HCERTSTORE store;
1489
1490 memset(&item, 0, sizeof(item));
1491 item.mask = TVIF_HANDLE | TVIF_PARAM | TVIF_TEXT;
1492 item.hItem = hItem;
1493 item.cchTextMax = sizeof(buf) / sizeof(buf[0]);
1494 item.pszText = buf;
1495 SendMessageW(tree, TVM_GETITEMW, 0, (LPARAM)&item);
1496 if (item.lParam)
1497 {
1498 struct StoreInfo *storeInfo = (struct StoreInfo *)item.lParam;
1499
1500 if (storeInfo->type == StoreHandle)
1501 store = storeInfo->u.store;
1502 else
1503 store = CertOpenSystemStoreW(0, storeInfo->u.name);
1504 }
1505 else
1506 {
1507 /* It's implicitly a system store */
1508 store = CertOpenSystemStoreW(0, buf);
1509 }
1510 return store;
1511 }
1512
1513 struct SelectStoreInfo
1514 {
1515 PCRYPTUI_SELECTSTORE_INFO_W info;
1516 HCERTSTORE store;
1517 };
1518
1519 static LRESULT CALLBACK select_store_dlg_proc(HWND hwnd, UINT msg, WPARAM wp,
1520 LPARAM lp)
1521 {
1522 struct SelectStoreInfo *selectInfo;
1523 LRESULT ret = 0;
1524
1525 switch (msg)
1526 {
1527 case WM_INITDIALOG:
1528 {
1529 selectInfo = (struct SelectStoreInfo *)lp;
1530 SetWindowLongPtrW(hwnd, DWLP_USER, lp);
1531 if (selectInfo->info->pwszTitle)
1532 SendMessageW(hwnd, WM_SETTEXT, 0,
1533 (LPARAM)selectInfo->info->pwszTitle);
1534 if (selectInfo->info->pwszText)
1535 SendMessageW(GetDlgItem(hwnd, IDC_STORE_TEXT), WM_SETTEXT, 0,
1536 (LPARAM)selectInfo->info->pwszText);
1537 if (!(selectInfo->info->dwFlags & CRYPTUI_ENABLE_SHOW_PHYSICAL_STORE))
1538 ShowWindow(GetDlgItem(hwnd, IDC_SHOW_PHYSICAL_STORES), FALSE);
1539 enumerate_stores(hwnd, selectInfo->info->pEnumData);
1540 break;
1541 }
1542 case WM_COMMAND:
1543 switch (wp)
1544 {
1545 case IDOK:
1546 {
1547 HWND tree = GetDlgItem(hwnd, IDC_STORE_LIST);
1548 HTREEITEM selection = (HTREEITEM)SendMessageW(tree,
1549 TVM_GETNEXTITEM, TVGN_CARET, 0);
1550
1551 selectInfo = (struct SelectStoreInfo *)GetWindowLongPtrW(hwnd,
1552 DWLP_USER);
1553 if (!selection)
1554 {
1555 WCHAR title[MAX_STRING_LEN], error[MAX_STRING_LEN], *pTitle;
1556
1557 if (selectInfo->info->pwszTitle)
1558 pTitle = selectInfo->info->pwszTitle;
1559 else
1560 {
1561 LoadStringW(hInstance, IDS_SELECT_STORE_TITLE, title,
1562 sizeof(title) / sizeof(title[0]));
1563 pTitle = title;
1564 }
1565 LoadStringW(hInstance, IDS_SELECT_STORE, error,
1566 sizeof(error) / sizeof(error[0]));
1567 MessageBoxW(hwnd, error, pTitle, MB_ICONEXCLAMATION | MB_OK);
1568 }
1569 else
1570 {
1571 HCERTSTORE store = selected_item_to_store(tree, selection);
1572
1573 if (!selectInfo->info->pfnSelectedStoreCallback ||
1574 selectInfo->info->pfnSelectedStoreCallback(store, hwnd,
1575 selectInfo->info->pvArg))
1576 {
1577 selectInfo->store = store;
1578 free_store_info(tree);
1579 EndDialog(hwnd, IDOK);
1580 }
1581 else
1582 CertCloseStore(store, 0);
1583 }
1584 ret = TRUE;
1585 break;
1586 }
1587 case IDCANCEL:
1588 free_store_info(GetDlgItem(hwnd, IDC_STORE_LIST));
1589 EndDialog(hwnd, IDCANCEL);
1590 ret = TRUE;
1591 break;
1592 }
1593 break;
1594 }
1595 return ret;
1596 }
1597
1598 /***********************************************************************
1599 * CryptUIDlgSelectStoreW (CRYPTUI.@)
1600 */
1601 HCERTSTORE WINAPI CryptUIDlgSelectStoreW(PCRYPTUI_SELECTSTORE_INFO_W info)
1602 {
1603 struct SelectStoreInfo selectInfo = { info, NULL };
1604
1605 TRACE("(%p)\n", info);
1606
1607 if (info->dwSize != sizeof(CRYPTUI_SELECTSTORE_INFO_W))
1608 {
1609 WARN("unexpected size %d\n", info->dwSize);
1610 SetLastError(E_INVALIDARG);
1611 return NULL;
1612 }
1613 DialogBoxParamW(hInstance, MAKEINTRESOURCEW(IDD_SELECT_STORE), info->parent,
1614 select_store_dlg_proc, (LPARAM)&selectInfo);
1615 return selectInfo.store;
1616 }
1617
1618 /***********************************************************************
1619 * CryptUIDlgSelectStoreA (CRYPTUI.@)
1620 */
1621 HCERTSTORE WINAPI CryptUIDlgSelectStoreA(PCRYPTUI_SELECTSTORE_INFO_A info)
1622 {
1623 CRYPTUI_SELECTSTORE_INFO_W infoW;
1624 HCERTSTORE ret;
1625 int len;
1626
1627 TRACE("(%p)\n", info);
1628
1629 if (info->dwSize != sizeof(CRYPTUI_SELECTSTORE_INFO_A))
1630 {
1631 WARN("unexpected size %d\n", info->dwSize);
1632 SetLastError(E_INVALIDARG);
1633 return NULL;
1634 }
1635 memcpy(&infoW, info, sizeof(*info));
1636 if (info->pszTitle)
1637 {
1638 len = MultiByteToWideChar(CP_ACP, 0, info->pszTitle, -1, NULL, 0);
1639 infoW.pwszTitle = HeapAlloc(GetProcessHeap(), 0, len * sizeof(WCHAR));
1640 MultiByteToWideChar(CP_ACP, 0, info->pszTitle, -1, infoW.pwszTitle,
1641 len);
1642 }
1643 if (info->pszText)
1644 {
1645 len = MultiByteToWideChar(CP_ACP, 0, info->pszText, -1, NULL, 0);
1646 infoW.pwszText = HeapAlloc(GetProcessHeap(), 0, len * sizeof(WCHAR));
1647 MultiByteToWideChar(CP_ACP, 0, info->pszText, -1, infoW.pwszText, len);
1648 }
1649 ret = CryptUIDlgSelectStoreW(&infoW);
1650 HeapFree(GetProcessHeap(), 0, infoW.pwszText);
1651 HeapFree(GetProcessHeap(), 0, infoW.pwszTitle);
1652 return ret;
1653 }
1654
1655 /***********************************************************************
1656 * CryptUIDlgViewCertificateA (CRYPTUI.@)
1657 */
1658 BOOL WINAPI CryptUIDlgViewCertificateA(
1659 PCCRYPTUI_VIEWCERTIFICATE_STRUCTA pCertViewInfo, BOOL *pfPropertiesChanged)
1660 {
1661 CRYPTUI_VIEWCERTIFICATE_STRUCTW viewInfo;
1662 LPWSTR title = NULL;
1663 BOOL ret;
1664
1665 TRACE("(%p, %p)\n", pCertViewInfo, pfPropertiesChanged);
1666
1667 memcpy(&viewInfo, pCertViewInfo, sizeof(viewInfo));
1668 if (pCertViewInfo->szTitle)
1669 {
1670 int len = MultiByteToWideChar(CP_ACP, 0, pCertViewInfo->szTitle, -1,
1671 NULL, 0);
1672
1673 title = HeapAlloc(GetProcessHeap(), 0, len * sizeof(WCHAR));
1674 if (title)
1675 {
1676 MultiByteToWideChar(CP_ACP, 0, pCertViewInfo->szTitle, -1, title,
1677 len);
1678 viewInfo.szTitle = title;
1679 }
1680 else
1681 {
1682 ret = FALSE;
1683 goto error;
1684 }
1685 }
1686 if (pCertViewInfo->cPropSheetPages)
1687 {
1688 FIXME("ignoring additional prop sheet pages\n");
1689 viewInfo.cPropSheetPages = 0;
1690 }
1691 ret = CryptUIDlgViewCertificateW(&viewInfo, pfPropertiesChanged);
1692 HeapFree(GetProcessHeap(), 0, title);
1693 error:
1694 return ret;
1695 }
1696
1697 struct ReadStringStruct
1698 {
1699 LPCWSTR buf;
1700 LONG pos;
1701 LONG len;
1702 };
1703
1704 static DWORD CALLBACK read_text_callback(DWORD_PTR dwCookie, LPBYTE buf,
1705 LONG cb, LONG *pcb)
1706 {
1707 struct ReadStringStruct *string = (struct ReadStringStruct *)dwCookie;
1708 LONG cch = min(cb / sizeof(WCHAR), string->len - string->pos);
1709
1710 TRACE("(%p, %p, %d, %p)\n", string, buf, cb, pcb);
1711
1712 memmove(buf, string->buf + string->pos, cch * sizeof(WCHAR));
1713 string->pos += cch;
1714 *pcb = cch * sizeof(WCHAR);
1715 return 0;
1716 }
1717
1718 static void add_unformatted_text_to_control(HWND hwnd, LPCWSTR text, LONG len)
1719 {
1720 struct ReadStringStruct string;
1721 EDITSTREAM editstream;
1722
1723 TRACE("(%p, %s)\n", hwnd, debugstr_wn(text, len));
1724
1725 string.buf = text;
1726 string.pos = 0;
1727 string.len = len;
1728 editstream.dwCookie = (DWORD_PTR)&string;
1729 editstream.dwError = 0;
1730 editstream.pfnCallback = read_text_callback;
1731 SendMessageW(hwnd, EM_STREAMIN, SF_TEXT | SFF_SELECTION | SF_UNICODE,
1732 (LPARAM)&editstream);
1733 }
1734
1735 static void add_string_resource_to_control(HWND hwnd, int id)
1736 {
1737 LPWSTR str;
1738 LONG len;
1739
1740 len = LoadStringW(hInstance, id, (LPWSTR)&str, 0);
1741 add_unformatted_text_to_control(hwnd, str, len);
1742 }
1743
1744 static void add_text_with_paraformat_to_control(HWND hwnd, LPCWSTR text,
1745 LONG len, const PARAFORMAT2 *fmt)
1746 {
1747 add_unformatted_text_to_control(hwnd, text, len);
1748 SendMessageW(hwnd, EM_SETPARAFORMAT, 0, (LPARAM)fmt);
1749 }
1750
1751 static void add_string_resource_with_paraformat_to_control(HWND hwnd, int id,
1752 const PARAFORMAT2 *fmt)
1753 {
1754 LPWSTR str;
1755 LONG len;
1756
1757 len = LoadStringW(hInstance, id, (LPWSTR)&str, 0);
1758 add_text_with_paraformat_to_control(hwnd, str, len, fmt);
1759 }
1760
1761 static LPWSTR get_cert_name_string(PCCERT_CONTEXT pCertContext, DWORD dwType,
1762 DWORD dwFlags)
1763 {
1764 LPWSTR buf = NULL;
1765 DWORD len;
1766
1767 len = CertGetNameStringW(pCertContext, dwType, dwFlags, NULL, NULL, 0);
1768 if (len)
1769 {
1770 buf = HeapAlloc(GetProcessHeap(), 0, len * sizeof(WCHAR));
1771 if (buf)
1772 CertGetNameStringW(pCertContext, dwType, dwFlags, NULL, buf, len);
1773 }
1774 return buf;
1775 }
1776
1777 static void add_cert_string_to_control(HWND hwnd, PCCERT_CONTEXT pCertContext,
1778 DWORD dwType, DWORD dwFlags)
1779 {
1780 LPWSTR name = get_cert_name_string(pCertContext, dwType, dwFlags);
1781
1782 if (name)
1783 {
1784 /* Don't include NULL-terminator in output */
1785 DWORD len = lstrlenW(name);
1786
1787 add_unformatted_text_to_control(hwnd, name, len);
1788 HeapFree(GetProcessHeap(), 0, name);
1789 }
1790 }
1791
1792 static void add_icon_to_control(HWND hwnd, int id)
1793 {
1794 HRESULT hr;
1795 IRichEditOle *richEditOle = NULL;
1796 IOleObject *object = NULL;
1797 CLSID clsid;
1798 LPOLECACHE oleCache = NULL;
1799 FORMATETC formatEtc;
1800 DWORD conn;
1801 IDataObject *dataObject = NULL;
1802 HBITMAP bitmap = NULL;
1803 STGMEDIUM stgm;
1804 IOleClientSite *clientSite = NULL;
1805 REOBJECT reObject;
1806
1807 TRACE("(%p, %d)\n", hwnd, id);
1808
1809 SendMessageW(hwnd, EM_GETOLEINTERFACE, 0, (LPARAM)&richEditOle);
1810 if (!richEditOle)
1811 goto end;
1812 hr = OleCreateDefaultHandler(&CLSID_NULL, NULL, &IID_IOleObject,
1813 (void**)&object);
1814 if (FAILED(hr))
1815 goto end;
1816 hr = IOleObject_GetUserClassID(object, &clsid);
1817 if (FAILED(hr))
1818 goto end;
1819 hr = IOleObject_QueryInterface(object, &IID_IOleCache, (void**)&oleCache);
1820 if (FAILED(hr))
1821 goto end;
1822 formatEtc.cfFormat = CF_BITMAP;
1823 formatEtc.ptd = NULL;
1824 formatEtc.dwAspect = DVASPECT_CONTENT;
1825 formatEtc.lindex = -1;
1826 formatEtc.tymed = TYMED_GDI;
1827 hr = IOleCache_Cache(oleCache, &formatEtc, 0, &conn);
1828 if (FAILED(hr))
1829 goto end;
1830 hr = IOleObject_QueryInterface(object, &IID_IDataObject,
1831 (void**)&dataObject);
1832 if (FAILED(hr))
1833 goto end;
1834 hr = IRichEditOle_GetClientSite(richEditOle, &clientSite);
1835 if (FAILED(hr))
1836 goto end;
1837 bitmap = LoadImageW(hInstance, MAKEINTRESOURCEW(id), IMAGE_BITMAP, 0, 0,
1838 LR_DEFAULTSIZE | LR_LOADTRANSPARENT);
1839 if (!bitmap)
1840 goto end;
1841 stgm.tymed = TYMED_GDI;
1842 stgm.u.hBitmap = bitmap;
1843 stgm.pUnkForRelease = NULL;
1844 hr = IDataObject_SetData(dataObject, &formatEtc, &stgm, TRUE);
1845 if (FAILED(hr))
1846 goto end;
1847
1848 reObject.cbStruct = sizeof(reObject);
1849 reObject.cp = REO_CP_SELECTION;
1850 reObject.clsid = clsid;
1851 reObject.poleobj = object;
1852 reObject.pstg = NULL;
1853 reObject.polesite = clientSite;
1854 reObject.sizel.cx = reObject.sizel.cy = 0;
1855 reObject.dvaspect = DVASPECT_CONTENT;
1856 reObject.dwFlags = 0;
1857 reObject.dwUser = 0;
1858
1859 IRichEditOle_InsertObject(richEditOle, &reObject);
1860
1861 end:
1862 if (clientSite)
1863 IOleClientSite_Release(clientSite);
1864 if (dataObject)
1865 IDataObject_Release(dataObject);
1866 if (oleCache)
1867 IOleCache_Release(oleCache);
1868 if (object)
1869 IOleObject_Release(object);
1870 if (richEditOle)
1871 IRichEditOle_Release(richEditOle);
1872 }
1873
1874 #define MY_INDENT 200
1875
1876 static void add_oid_text_to_control(HWND hwnd, char *oid)
1877 {
1878 WCHAR nl = '\n';
1879 PCCRYPT_OID_INFO oidInfo = CryptFindOIDInfo(CRYPT_OID_INFO_OID_KEY, oid, 0);
1880 PARAFORMAT2 parFmt;
1881
1882 parFmt.cbSize = sizeof(parFmt);
1883 parFmt.dwMask = PFM_STARTINDENT;
1884 parFmt.dxStartIndent = MY_INDENT * 3;
1885 if (oidInfo)
1886 {
1887 add_text_with_paraformat_to_control(hwnd, oidInfo->pwszName,
1888 lstrlenW(oidInfo->pwszName), &parFmt);
1889 add_unformatted_text_to_control(hwnd, &nl, 1);
1890 }
1891 }
1892
1893 struct OIDToString
1894 {
1895 LPCSTR oid;
1896 int id;
1897 };
1898
1899 /* The following list MUST be lexicographically sorted by OID */
1900 static struct OIDToString oidMap[] = {
1901 /* 1.3.6.1.4.1.311.10.3.1 */
1902 { szOID_KP_CTL_USAGE_SIGNING, IDS_PURPOSE_CTL_USAGE_SIGNING },
1903 /* 1.3.6.1.4.1.311.10.3.4 */
1904 { szOID_KP_EFS, IDS_PURPOSE_EFS },
1905 /* 1.3.6.1.4.1.311.10.3.4.1 */
1906 { szOID_EFS_RECOVERY, IDS_PURPOSE_EFS_RECOVERY },
1907 /* 1.3.6.1.4.1.311.10.3.5 */
1908 { szOID_WHQL_CRYPTO, IDS_PURPOSE_WHQL },
1909 /* 1.3.6.1.4.1.311.10.3.6 */
1910 { szOID_NT5_CRYPTO, IDS_PURPOSE_NT5 },
1911 /* 1.3.6.1.4.1.311.10.3.7 */
1912 { szOID_OEM_WHQL_CRYPTO, IDS_PURPOSE_OEM_WHQL },
1913 /* 1.3.6.1.4.1.311.10.3.8 */
1914 { szOID_EMBEDDED_NT_CRYPTO, IDS_PURPOSE_EMBEDDED_NT },
1915 /* 1.3.6.1.4.1.311.10.3.9 */
1916 { szOID_ROOT_LIST_SIGNER, IDS_PURPOSE_ROOT_LIST_SIGNER },
1917 /* 1.3.6.1.4.1.311.10.3.10 */
1918 { szOID_KP_QUALIFIED_SUBORDINATION, IDS_PURPOSE_QUALIFIED_SUBORDINATION },
1919 /* 1.3.6.1.4.1.311.10.3.11 */
1920 { szOID_KP_KEY_RECOVERY, IDS_PURPOSE_KEY_RECOVERY },
1921 /* 1.3.6.1.4.1.311.10.3.12 */
1922 { szOID_KP_DOCUMENT_SIGNING, IDS_PURPOSE_DOCUMENT_SIGNING },
1923 /* 1.3.6.1.4.1.311.10.3.13 */
1924 { szOID_KP_LIFETIME_SIGNING, IDS_PURPOSE_LIFETIME_SIGNING },
1925 /* 1.3.6.1.4.1.311.10.5.1 */
1926 { szOID_DRM, IDS_PURPOSE_DRM },
1927 /* 1.3.6.1.4.1.311.10.6.1 */
1928 { szOID_LICENSES, IDS_PURPOSE_LICENSES },
1929 /* 1.3.6.1.4.1.311.10.6.2 */
1930 { szOID_LICENSE_SERVER, IDS_PURPOSE_LICENSE_SERVER },
1931 /* 1.3.6.1.4.1.311.20.2.1 */
1932 { szOID_ENROLLMENT_AGENT, IDS_PURPOSE_ENROLLMENT_AGENT },
1933 /* 1.3.6.1.4.1.311.20.2.2 */
1934 { szOID_KP_SMARTCARD_LOGON, IDS_PURPOSE_SMARTCARD_LOGON },
1935 /* 1.3.6.1.4.1.311.21.5 */
1936 { szOID_KP_CA_EXCHANGE, IDS_PURPOSE_CA_EXCHANGE },
1937 /* 1.3.6.1.4.1.311.21.6 */
1938 { szOID_KP_KEY_RECOVERY_AGENT, IDS_PURPOSE_KEY_RECOVERY_AGENT },
1939 /* 1.3.6.1.4.1.311.21.19 */
1940 { szOID_DS_EMAIL_REPLICATION, IDS_PURPOSE_DS_EMAIL_REPLICATION },
1941 /* 1.3.6.1.5.5.7.3.1 */
1942 { szOID_PKIX_KP_SERVER_AUTH, IDS_PURPOSE_SERVER_AUTH },
1943 /* 1.3.6.1.5.5.7.3.2 */
1944 { szOID_PKIX_KP_CLIENT_AUTH, IDS_PURPOSE_CLIENT_AUTH },
1945 /* 1.3.6.1.5.5.7.3.3 */
1946 { szOID_PKIX_KP_CODE_SIGNING, IDS_PURPOSE_CODE_SIGNING },
1947 /* 1.3.6.1.5.5.7.3.4 */
1948 { szOID_PKIX_KP_EMAIL_PROTECTION, IDS_PURPOSE_EMAIL_PROTECTION },
1949 /* 1.3.6.1.5.5.7.3.5 */
1950 { szOID_PKIX_KP_IPSEC_END_SYSTEM, IDS_PURPOSE_IPSEC },
1951 /* 1.3.6.1.5.5.7.3.6 */
1952 { szOID_PKIX_KP_IPSEC_TUNNEL, IDS_PURPOSE_IPSEC },
1953 /* 1.3.6.1.5.5.7.3.7 */
1954 { szOID_PKIX_KP_IPSEC_USER, IDS_PURPOSE_IPSEC },
1955 /* 1.3.6.1.5.5.7.3.8 */
1956 { szOID_PKIX_KP_TIMESTAMP_SIGNING, IDS_PURPOSE_TIMESTAMP_SIGNING },
1957 };
1958
1959 static struct OIDToString *findSupportedOID(LPCSTR oid)
1960 {
1961 int indexHigh = sizeof(oidMap) / sizeof(oidMap[0]) - 1, indexLow = 0;
1962
1963 while (indexLow <= indexHigh)
1964 {
1965 int cmp, i = (indexLow + indexHigh) / 2;
1966 if (!(cmp = strcmp(oid, oidMap[i].oid)))
1967 return &oidMap[i];
1968 if (cmp > 0)
1969 indexLow = i + 1;
1970 else
1971 indexHigh = i - 1;
1972 }
1973 return NULL;
1974 }
1975
1976 static void add_local_oid_text_to_control(HWND text, LPCSTR oid)
1977 {
1978 struct OIDToString *entry;
1979 WCHAR nl = '\n';
1980 PARAFORMAT2 parFmt;
1981
1982 parFmt.cbSize = sizeof(parFmt);
1983 parFmt.dwMask = PFM_STARTINDENT;
1984 parFmt.dxStartIndent = MY_INDENT * 3;
1985 if ((entry = findSupportedOID(oid)))
1986 {
1987 WCHAR *str, *linebreak, *ptr;
1988 BOOL multiline = FALSE;
1989 int len;
1990
1991 len = LoadStringW(hInstance, entry->id, (LPWSTR)&str, 0);
1992 ptr = str;
1993 do {
1994 if ((linebreak = memchrW(ptr, '\n', len)))
1995 {
1996 WCHAR copy[MAX_STRING_LEN];
1997
1998 multiline = TRUE;
1999 /* The source string contains a newline, which the richedit
2000 * control won't find since it's interpreted as a paragraph
2001 * break. Therefore copy up to the newline. lstrcpynW always
2002 * NULL-terminates, so pass one more than the length of the
2003 * source line so the copy includes the entire line and the
2004 * NULL-terminator.
2005 */
2006 lstrcpynW(copy, ptr, linebreak - ptr + 1);
2007 add_text_with_paraformat_to_control(text, copy,
2008 linebreak - ptr, &parFmt);
2009 ptr = linebreak + 1;
2010 add_unformatted_text_to_control(text, &nl, 1);
2011 }
2012 else if (multiline && *ptr)
2013 {
2014 /* Add the last line */
2015 add_text_with_paraformat_to_control(text, ptr,
2016 len - (ptr - str), &parFmt);
2017 add_unformatted_text_to_control(text, &nl, 1);
2018 }
2019 } while (linebreak);
2020 if (!multiline)
2021 {
2022 add_text_with_paraformat_to_control(text, str, len, &parFmt);
2023 add_unformatted_text_to_control(text, &nl, 1);
2024 }
2025 }
2026 else
2027 {
2028 WCHAR *oidW = HeapAlloc(GetProcessHeap(), 0,
2029 (strlen(oid) + 1) * sizeof(WCHAR));
2030
2031 if (oidW)
2032 {
2033 LPCSTR src;
2034 WCHAR *dst;
2035
2036 for (src = oid, dst = oidW; *src; src++, dst++)
2037 *dst = *src;
2038 *dst = 0;
2039 add_text_with_paraformat_to_control(text, oidW, lstrlenW(oidW),
2040 &parFmt);
2041 add_unformatted_text_to_control(text, &nl, 1);
2042 HeapFree(GetProcessHeap(), 0, oidW);
2043 }
2044 }
2045 }
2046
2047 static void display_app_usages(HWND text, PCCERT_CONTEXT cert,
2048 BOOL *anyUsageAdded)
2049 {
2050 static char any_app_policy[] = szOID_ANY_APPLICATION_POLICY;
2051 WCHAR nl = '\n';
2052 CHARFORMATW charFmt;
2053 PCERT_EXTENSION policyExt;
2054 if (!*anyUsageAdded)
2055 {
2056 PARAFORMAT2 parFmt;
2057
2058 parFmt.cbSize = sizeof(parFmt);
2059 parFmt.dwMask = PFM_STARTINDENT;
2060 parFmt.dxStartIndent = MY_INDENT;
2061 add_string_resource_with_paraformat_to_control(text,
2062 IDS_CERT_INFO_PURPOSES, &parFmt);
2063 add_unformatted_text_to_control(text, &nl, 1);
2064 *anyUsageAdded = TRUE;
2065 }
2066 memset(&charFmt, 0, sizeof(charFmt));
2067 charFmt.cbSize = sizeof(charFmt);
2068 charFmt.dwMask = CFM_BOLD;
2069 charFmt.dwEffects = 0;
2070 SendMessageW(text, EM_SETCHARFORMAT, SCF_SELECTION, (LPARAM)&charFmt);
2071 if ((policyExt = CertFindExtension(szOID_APPLICATION_CERT_POLICIES,
2072 cert->pCertInfo->cExtension, cert->pCertInfo->rgExtension)))
2073 {
2074 CERT_POLICIES_INFO *policies;
2075 DWORD size;
2076
2077 if (CryptDecodeObjectEx(X509_ASN_ENCODING, X509_CERT_POLICIES,
2078 policyExt->Value.pbData, policyExt->Value.cbData,
2079 CRYPT_DECODE_ALLOC_FLAG, NULL, &policies, &size))
2080 {
2081 DWORD i;
2082
2083 for (i = 0; i < policies->cPolicyInfo; i++)
2084 {
2085 DWORD j;
2086
2087 for (j = 0; j < policies->rgPolicyInfo[i].cPolicyQualifier; j++)
2088 add_local_oid_text_to_control(text,
2089 policies->rgPolicyInfo[i].rgPolicyQualifier[j].
2090 pszPolicyQualifierId);
2091 }
2092 LocalFree(policies);
2093 }
2094 }
2095 else
2096 add_oid_text_to_control(text, any_app_policy);
2097 }
2098
2099 static BOOL display_cert_usages(HWND text, PCCERT_CONTEXT cert,
2100 BOOL *anyUsageAdded)
2101 {
2102 WCHAR nl = '\n';
2103 DWORD size;
2104 BOOL badUsages = FALSE;
2105
2106 if (CertGetEnhancedKeyUsage(cert, 0, NULL, &size))
2107 {
2108 CHARFORMATW charFmt;
2109 static char any_cert_policy[] = szOID_ANY_CERT_POLICY;
2110 PCERT_ENHKEY_USAGE usage = HeapAlloc(GetProcessHeap(), 0, size);
2111
2112 if (usage)
2113 {
2114 if (CertGetEnhancedKeyUsage(cert, 0, usage, &size))
2115 {
2116 DWORD i;
2117
2118 if (!*anyUsageAdded)
2119 {
2120 PARAFORMAT2 parFmt;
2121
2122 parFmt.cbSize = sizeof(parFmt);
2123 parFmt.dwMask = PFM_STARTINDENT;
2124 parFmt.dxStartIndent = MY_INDENT;
2125 add_string_resource_with_paraformat_to_control(text,
2126 IDS_CERT_INFO_PURPOSES, &parFmt);
2127 add_unformatted_text_to_control(text, &nl, 1);
2128 *anyUsageAdded = TRUE;
2129 }
2130 memset(&charFmt, 0, sizeof(charFmt));
2131 charFmt.cbSize = sizeof(charFmt);
2132 charFmt.dwMask = CFM_BOLD;
2133 charFmt.dwEffects = 0;
2134 SendMessageW(text, EM_SETCHARFORMAT, SCF_SELECTION,
2135 (LPARAM)&charFmt);
2136 if (!usage->cUsageIdentifier)
2137 add_oid_text_to_control(text, any_cert_policy);
2138 else
2139 for (i = 0; i < usage->cUsageIdentifier; i++)
2140 add_local_oid_text_to_control(text,
2141 usage->rgpszUsageIdentifier[i]);
2142 }
2143 else
2144 badUsages = TRUE;
2145 HeapFree(GetProcessHeap(), 0, usage);
2146 }
2147 else
2148 badUsages = TRUE;
2149 }
2150 else
2151 badUsages = TRUE;
2152 return badUsages;
2153 }
2154
2155 static void set_policy_text(HWND text,
2156 PCCRYPTUI_VIEWCERTIFICATE_STRUCTW pCertViewInfo)
2157 {
2158 BOOL includeCertUsages = FALSE, includeAppUsages = FALSE;
2159 BOOL badUsages = FALSE, anyUsageAdded = FALSE;
2160
2161 if (pCertViewInfo->cPurposes)
2162 {
2163 DWORD i;
2164
2165 for (i = 0; i < pCertViewInfo->cPurposes; i++)
2166 {
2167 if (!strcmp(pCertViewInfo->rgszPurposes[i], szOID_ANY_CERT_POLICY))
2168 includeCertUsages = TRUE;
2169 else if (!strcmp(pCertViewInfo->rgszPurposes[i],
2170 szOID_ANY_APPLICATION_POLICY))
2171 includeAppUsages = TRUE;
2172 else
2173 badUsages = TRUE;
2174 }
2175 }
2176 else
2177 includeAppUsages = includeCertUsages = TRUE;
2178 if (includeAppUsages)
2179 display_app_usages(text, pCertViewInfo->pCertContext, &anyUsageAdded);
2180 if (includeCertUsages)
2181 badUsages = display_cert_usages(text, pCertViewInfo->pCertContext,
2182 &anyUsageAdded);
2183 if (badUsages)
2184 {
2185 PARAFORMAT2 parFmt;
2186
2187 parFmt.cbSize = sizeof(parFmt);
2188 parFmt.dwMask = PFM_STARTINDENT;
2189 parFmt.dxStartIndent = MY_INDENT;
2190 add_string_resource_with_paraformat_to_control(text,
2191 IDS_CERT_INFO_BAD_PURPOSES, &parFmt);
2192 }
2193 }
2194
2195 static CRYPT_OBJID_BLOB *find_policy_qualifier(CERT_POLICIES_INFO *policies,
2196 LPCSTR policyOid)
2197 {
2198 CRYPT_OBJID_BLOB *ret = NULL;
2199 DWORD i;
2200
2201 for (i = 0; !ret && i < policies->cPolicyInfo; i++)
2202 {
2203 DWORD j;
2204
2205 for (j = 0; !ret && j < policies->rgPolicyInfo[i].cPolicyQualifier; j++)
2206 if (!strcmp(policies->rgPolicyInfo[i].rgPolicyQualifier[j].
2207 pszPolicyQualifierId, policyOid))
2208 ret = &policies->rgPolicyInfo[i].rgPolicyQualifier[j].
2209 Qualifier;
2210 }
2211 return ret;
2212 }
2213
2214 static WCHAR *get_cps_str_from_qualifier(const CRYPT_OBJID_BLOB *qualifier)
2215 {
2216 LPWSTR qualifierStr = NULL;
2217 CERT_NAME_VALUE *qualifierValue;
2218 DWORD size;
2219
2220 if (CryptDecodeObjectEx(X509_ASN_ENCODING, X509_NAME_VALUE,
2221 qualifier->pbData, qualifier->cbData, CRYPT_DECODE_ALLOC_FLAG, NULL,
2222 &qualifierValue, &size))
2223 {
2224 size = CertRDNValueToStrW(qualifierValue->dwValueType,
2225 &qualifierValue->Value, NULL, 0);
2226 qualifierStr = HeapAlloc(GetProcessHeap(), 0, size * sizeof(WCHAR));
2227 if (qualifierStr)
2228 CertRDNValueToStrW(qualifierValue->dwValueType,
2229 &qualifierValue->Value, qualifierStr, size);
2230 LocalFree(qualifierValue);
2231 }
2232 return qualifierStr;
2233 }
2234
2235 static WCHAR *get_user_notice_from_qualifier(const CRYPT_OBJID_BLOB *qualifier)
2236 {
2237 LPWSTR str = NULL;
2238 CERT_POLICY_QUALIFIER_USER_NOTICE *qualifierValue;
2239 DWORD size;
2240
2241 if (CryptDecodeObjectEx(X509_ASN_ENCODING,
2242 X509_PKIX_POLICY_QUALIFIER_USERNOTICE,
2243 qualifier->pbData, qualifier->cbData, CRYPT_DECODE_ALLOC_FLAG, NULL,
2244 &qualifierValue, &size))
2245 {
2246 str = HeapAlloc(GetProcessHeap(), 0,
2247 (strlenW(qualifierValue->pszDisplayText) + 1) * sizeof(WCHAR));
2248 if (str)
2249 strcpyW(str, qualifierValue->pszDisplayText);
2250 LocalFree(qualifierValue);
2251 }
2252 return str;
2253 }
2254
2255 struct IssuerStatement
2256 {
2257 LPWSTR cps;
2258 LPWSTR userNotice;
2259 };
2260
2261 static void set_issuer_statement(HWND hwnd,
2262 PCCRYPTUI_VIEWCERTIFICATE_STRUCTW pCertViewInfo)
2263 {
2264 PCERT_EXTENSION policyExt;
2265
2266 if (!(pCertViewInfo->dwFlags & CRYPTUI_DISABLE_ISSUERSTATEMENT) &&
2267 (policyExt = CertFindExtension(szOID_CERT_POLICIES,
2268 pCertViewInfo->pCertContext->pCertInfo->cExtension,
2269 pCertViewInfo->pCertContext->pCertInfo->rgExtension)))
2270 {
2271 CERT_POLICIES_INFO *policies;
2272 DWORD size;
2273
2274 if (CryptDecodeObjectEx(X509_ASN_ENCODING, policyExt->pszObjId,
2275 policyExt->Value.pbData, policyExt->Value.cbData,
2276 CRYPT_DECODE_ALLOC_FLAG, NULL, &policies, &size))
2277 {
2278 CRYPT_OBJID_BLOB *qualifier;
2279 LPWSTR cps = NULL, userNotice = NULL;
2280
2281 if ((qualifier = find_policy_qualifier(policies,
2282 szOID_PKIX_POLICY_QUALIFIER_CPS)))
2283 cps = get_cps_str_from_qualifier(qualifier);
2284 if ((qualifier = find_policy_qualifier(policies,
2285 szOID_PKIX_POLICY_QUALIFIER_USERNOTICE)))
2286 userNotice = get_user_notice_from_qualifier(qualifier);
2287 if (cps || userNotice)
2288 {
2289 struct IssuerStatement *issuerStatement =
2290 HeapAlloc(GetProcessHeap(), 0, sizeof(struct IssuerStatement));
2291
2292 if (issuerStatement)
2293 {
2294 issuerStatement->cps = cps;
2295 issuerStatement->userNotice = userNotice;
2296 EnableWindow(GetDlgItem(hwnd, IDC_ISSUERSTATEMENT), TRUE);
2297 SetWindowLongPtrW(hwnd, DWLP_USER,
2298 (ULONG_PTR)issuerStatement);
2299 }
2300 }
2301 LocalFree(policies);
2302 }
2303 }
2304 }
2305
2306 static void set_cert_info(HWND hwnd,
2307 PCCRYPTUI_VIEWCERTIFICATE_STRUCTW pCertViewInfo)
2308 {
2309 CHARFORMATW charFmt;
2310 PARAFORMAT2 parFmt;
2311 HWND icon = GetDlgItem(hwnd, IDC_CERTIFICATE_ICON);
2312 HWND text = GetDlgItem(hwnd, IDC_CERTIFICATE_INFO);
2313 CRYPT_PROVIDER_SGNR *provSigner = WTHelperGetProvSignerFromChain(
2314 (CRYPT_PROVIDER_DATA *)pCertViewInfo->u.pCryptProviderData,
2315 pCertViewInfo->idxSigner, pCertViewInfo->fCounterSigner,
2316 pCertViewInfo->idxCounterSigner);
2317 CRYPT_PROVIDER_CERT *root =
2318 &provSigner->pasCertChain[provSigner->csCertChain - 1];
2319
2320 if (!provSigner->pChainContext ||
2321 (provSigner->pChainContext->TrustStatus.dwErrorStatus &
2322 CERT_TRUST_IS_PARTIAL_CHAIN))
2323 add_icon_to_control(icon, IDB_CERT_WARNING);
2324 else if (!root->fTrustedRoot)
2325 add_icon_to_control(icon, IDB_CERT_ERROR);
2326 else
2327 add_icon_to_control(icon, IDB_CERT);
2328
2329 memset(&charFmt, 0, sizeof(charFmt));
2330 charFmt.cbSize = sizeof(charFmt);
2331 charFmt.dwMask = CFM_BOLD;
2332 charFmt.dwEffects = CFE_BOLD;
2333 SendMessageW(text, EM_SETCHARFORMAT, SCF_SELECTION, (LPARAM)&charFmt);
2334 /* FIXME: vertically center text */
2335 parFmt.cbSize = sizeof(parFmt);
2336 parFmt.dwMask = PFM_STARTINDENT;
2337 parFmt.dxStartIndent = MY_INDENT;
2338 add_string_resource_with_paraformat_to_control(text,
2339 IDS_CERTIFICATEINFORMATION, &parFmt);
2340
2341 text = GetDlgItem(hwnd, IDC_CERTIFICATE_STATUS);
2342 SendMessageW(text, EM_SETCHARFORMAT, SCF_SELECTION, (LPARAM)&charFmt);
2343 if (provSigner->dwError == TRUST_E_CERT_SIGNATURE)
2344 add_string_resource_with_paraformat_to_control(text,
2345 IDS_CERT_INFO_BAD_SIG, &parFmt);
2346 else if (!provSigner->pChainContext ||
2347 (provSigner->pChainContext->TrustStatus.dwErrorStatus &
2348 CERT_TRUST_IS_PARTIAL_CHAIN))
2349 add_string_resource_with_paraformat_to_control(text,
2350 IDS_CERT_INFO_PARTIAL_CHAIN, &parFmt);
2351 else if (!root->fTrustedRoot)
2352 {
2353 if (provSigner->csCertChain == 1 && root->fSelfSigned)
2354 add_string_resource_with_paraformat_to_control(text,
2355 IDS_CERT_INFO_UNTRUSTED_CA, &parFmt);
2356 else
2357 add_string_resource_with_paraformat_to_control(text,
2358 IDS_CERT_INFO_UNTRUSTED_ROOT, &parFmt);
2359 }
2360 else
2361 {
2362 set_policy_text(text, pCertViewInfo);
2363 set_issuer_statement(hwnd, pCertViewInfo);
2364 }
2365 }
2366
2367 static void set_cert_name_string(HWND hwnd, PCCERT_CONTEXT cert,
2368 DWORD nameFlags, int heading)
2369 {
2370 WCHAR nl = '\n';
2371 HWND text = GetDlgItem(hwnd, IDC_CERTIFICATE_NAMES);
2372 CHARFORMATW charFmt;
2373 PARAFORMAT2 parFmt;
2374
2375 memset(&charFmt, 0, sizeof(charFmt));
2376 charFmt.cbSize = sizeof(charFmt);
2377 charFmt.dwMask = CFM_BOLD;
2378 charFmt.dwEffects = CFE_BOLD;
2379 SendMessageW(text, EM_SETCHARFORMAT, SCF_SELECTION, (LPARAM)&charFmt);
2380 parFmt.cbSize = sizeof(parFmt);
2381 parFmt.dwMask = PFM_STARTINDENT;
2382 parFmt.dxStartIndent = MY_INDENT * 3;
2383 add_string_resource_with_paraformat_to_control(text, heading, &parFmt);
2384 charFmt.dwEffects = 0;
2385 SendMessageW(text, EM_SETCHARFORMAT, SCF_SELECTION, (LPARAM)&charFmt);
2386 add_cert_string_to_control(text, cert, CERT_NAME_SIMPLE_DISPLAY_TYPE,
2387 nameFlags);
2388 add_unformatted_text_to_control(text, &nl, 1);
2389 add_unformatted_text_to_control(text, &nl, 1);
2390 add_unformatted_text_to_control(text, &nl, 1);
2391
2392 }
2393
2394 static void add_date_string_to_control(HWND hwnd, const FILETIME *fileTime)
2395 {
2396 WCHAR dateFmt[80]; /* sufficient for all versions of LOCALE_SSHORTDATE */
2397 WCHAR date[80];
2398 SYSTEMTIME sysTime;
2399
2400 GetLocaleInfoW(LOCALE_SYSTEM_DEFAULT, LOCALE_SSHORTDATE, dateFmt,
2401 sizeof(dateFmt) / sizeof(dateFmt[0]));
2402 FileTimeToSystemTime(fileTime, &sysTime);
2403 GetDateFormatW(LOCALE_SYSTEM_DEFAULT, 0, &sysTime, dateFmt, date,
2404 sizeof(date) / sizeof(date[0]));
2405 add_unformatted_text_to_control(hwnd, date, lstrlenW(date));
2406 }
2407
2408 static void set_cert_validity_period(HWND hwnd, PCCERT_CONTEXT cert)
2409 {
2410 WCHAR nl = '\n';
2411 HWND text = GetDlgItem(hwnd, IDC_CERTIFICATE_NAMES);
2412 CHARFORMATW charFmt;
2413 PARAFORMAT2 parFmt;
2414
2415 memset(&charFmt, 0, sizeof(charFmt));
2416 charFmt.cbSize = sizeof(charFmt);
2417 charFmt.dwMask = CFM_BOLD;
2418 charFmt.dwEffects = CFE_BOLD;
2419 SendMessageW(text, EM_SETCHARFORMAT, SCF_SELECTION, (LPARAM)&charFmt);
2420 parFmt.cbSize = sizeof(parFmt);
2421 parFmt.dwMask = PFM_STARTINDENT;
2422 parFmt.dxStartIndent = MY_INDENT * 3;
2423 add_string_resource_with_paraformat_to_control(text, IDS_VALID_FROM,
2424 &parFmt);
2425 charFmt.dwEffects = 0;
2426 SendMessageW(text, EM_SETCHARFORMAT, SCF_SELECTION, (LPARAM)&charFmt);
2427 add_date_string_to_control(text, &cert->pCertInfo->NotBefore);
2428 charFmt.dwEffects = CFE_BOLD;
2429 SendMessageW(text, EM_SETCHARFORMAT, SCF_SELECTION, (LPARAM)&charFmt);
2430 add_string_resource_to_control(text, IDS_VALID_TO);
2431 charFmt.dwEffects = 0;
2432 SendMessageW(text, EM_SETCHARFORMAT, SCF_SELECTION, (LPARAM)&charFmt);
2433 add_date_string_to_control(text, &cert->pCertInfo->NotAfter);
2434 add_unformatted_text_to_control(text, &nl, 1);
2435 }
2436
2437 static void set_general_info(HWND hwnd,
2438 PCCRYPTUI_VIEWCERTIFICATE_STRUCTW pCertViewInfo)
2439 {
2440 set_cert_info(hwnd, pCertViewInfo);
2441 set_cert_name_string(hwnd, pCertViewInfo->pCertContext, 0,
2442 IDS_SUBJECT_HEADING);
2443 set_cert_name_string(hwnd, pCertViewInfo->pCertContext,
2444 CERT_NAME_ISSUER_FLAG, IDS_ISSUER_HEADING);
2445 set_cert_validity_period(hwnd, pCertViewInfo->pCertContext);
2446 }
2447
2448 static LRESULT CALLBACK user_notice_dlg_proc(HWND hwnd, UINT msg, WPARAM wp,
2449 LPARAM lp)
2450 {
2451 LRESULT ret = 0;
2452 HWND text;
2453 struct IssuerStatement *issuerStatement;
2454
2455 switch (msg)
2456 {
2457 case WM_INITDIALOG:
2458 text = GetDlgItem(hwnd, IDC_USERNOTICE);
2459 issuerStatement = (struct IssuerStatement *)lp;
2460 add_unformatted_text_to_control(text, issuerStatement->userNotice,
2461 strlenW(issuerStatement->userNotice));
2462 if (issuerStatement->cps)
2463 SetWindowLongPtrW(hwnd, DWLP_USER, (LPARAM)issuerStatement->cps);
2464 else
2465 EnableWindow(GetDlgItem(hwnd, IDC_CPS), FALSE);
2466 break;
2467 case WM_COMMAND:
2468 switch (wp)
2469 {
2470 case IDOK:
2471 EndDialog(hwnd, IDOK);
2472 ret = TRUE;
2473 break;
2474 case IDC_CPS:
2475 {
2476 IBindCtx *bctx = NULL;
2477 LPWSTR cps;
2478
2479 CreateBindCtx(0, &bctx);
2480 cps = (LPWSTR)GetWindowLongPtrW(hwnd, DWLP_USER);
2481 HlinkSimpleNavigateToString(cps, NULL, NULL, NULL, bctx, NULL,
2482 HLNF_OPENINNEWWINDOW, 0);
2483 IBindCtx_Release(bctx);
2484 break;
2485 }
2486 }
2487 }
2488 return ret;
2489 }
2490
2491 static void show_user_notice(HWND hwnd, struct IssuerStatement *issuerStatement)
2492 {
2493 DialogBoxParamW(hInstance, MAKEINTRESOURCEW(IDD_USERNOTICE), hwnd,
2494 user_notice_dlg_proc, (LPARAM)issuerStatement);
2495 }
2496
2497 static LRESULT CALLBACK general_dlg_proc(HWND hwnd, UINT msg, WPARAM wp,
2498 LPARAM lp)
2499 {
2500 PROPSHEETPAGEW *page;
2501 PCCRYPTUI_VIEWCERTIFICATE_STRUCTW pCertViewInfo;
2502
2503 TRACE("(%p, %08x, %08lx, %08lx)\n", hwnd, msg, wp, lp);
2504
2505 switch (msg)
2506 {
2507 case WM_INITDIALOG:
2508 page = (PROPSHEETPAGEW *)lp;
2509 pCertViewInfo = (PCCRYPTUI_VIEWCERTIFICATE_STRUCTW)page->lParam;
2510 if (pCertViewInfo->dwFlags & CRYPTUI_DISABLE_ADDTOSTORE)
2511 ShowWindow(GetDlgItem(hwnd, IDC_ADDTOSTORE), FALSE);
2512 EnableWindow(GetDlgItem(hwnd, IDC_ISSUERSTATEMENT), FALSE);
2513 set_general_info(hwnd, pCertViewInfo);
2514 break;
2515 case WM_COMMAND:
2516 switch (wp)
2517 {
2518 case IDC_ADDTOSTORE:
2519 CryptUIWizImport(0, hwnd, NULL, NULL, NULL);
2520 break;
2521 case IDC_ISSUERSTATEMENT:
2522 {
2523 struct IssuerStatement *issuerStatement =
2524 (struct IssuerStatement *)GetWindowLongPtrW(hwnd, DWLP_USER);
2525
2526 if (issuerStatement)
2527 {
2528 if (issuerStatement->userNotice)
2529 show_user_notice(hwnd, issuerStatement);
2530 else if (issuerStatement->cps)
2531 {
2532 IBindCtx *bctx = NULL;
2533
2534 CreateBindCtx(0, &bctx);
2535 HlinkSimpleNavigateToString(issuerStatement->cps, NULL,
2536 NULL, NULL, bctx, NULL, HLNF_OPENINNEWWINDOW, 0);
2537 IBindCtx_Release(bctx);
2538 }
2539 }
2540 break;
2541 }
2542 }
2543 break;
2544 }
2545 return 0;
2546 }
2547
2548 static UINT CALLBACK general_callback_proc(HWND hwnd, UINT msg,
2549 PROPSHEETPAGEW *page)
2550 {
2551 struct IssuerStatement *issuerStatement;
2552
2553 switch (msg)
2554 {
2555 case PSPCB_RELEASE:
2556 issuerStatement =
2557 (struct IssuerStatement *)GetWindowLongPtrW(hwnd, DWLP_USER);
2558 if (issuerStatement)
2559 {
2560 HeapFree(GetProcessHeap(), 0, issuerStatement->cps);
2561 HeapFree(GetProcessHeap(), 0, issuerStatement->userNotice);
2562 HeapFree(GetProcessHeap(), 0, issuerStatement);
2563 }
2564 break;
2565 }
2566 return 1;
2567 }
2568
2569 static void init_general_page(PCCRYPTUI_VIEWCERTIFICATE_STRUCTW pCertViewInfo,
2570 PROPSHEETPAGEW *page)
2571 {
2572 memset(page, 0, sizeof(PROPSHEETPAGEW));
2573 page->dwSize = sizeof(PROPSHEETPAGEW);
2574 page->dwFlags = PSP_USECALLBACK;
2575 page->pfnCallback = general_callback_proc;
2576 page->hInstance = hInstance;
2577 page->u.pszTemplate = MAKEINTRESOURCEW(IDD_GENERAL);
2578 page->pfnDlgProc = general_dlg_proc;
2579 page->lParam = (LPARAM)pCertViewInfo;
2580 }
2581
2582 typedef WCHAR * (*field_format_func)(PCCERT_CONTEXT cert);
2583
2584 static WCHAR *field_format_version(PCCERT_CONTEXT cert)
2585 {
2586 static const WCHAR fmt[] = { 'V','%','d',0 };
2587 WCHAR *buf = HeapAlloc(GetProcessHeap(), 0, 12 * sizeof(WCHAR));
2588
2589 if (buf)
2590 sprintfW(buf, fmt, cert->pCertInfo->dwVersion);
2591 return buf;
2592 }
2593
2594 static WCHAR *format_hex_string(void *pb, DWORD cb)
2595 {
2596 WCHAR *buf = HeapAlloc(GetProcessHeap(), 0, (cb * 3 + 1) * sizeof(WCHAR));
2597
2598 if (buf)
2599 {
2600 static const WCHAR fmt[] = { '%','0','2','x',' ',0 };
2601 DWORD i;
2602 WCHAR *ptr;
2603
2604 for (i = 0, ptr = buf; i < cb; i++, ptr += 3)
2605 sprintfW(ptr, fmt, ((BYTE *)pb)[i]);
2606 }
2607 return buf;
2608 }
2609
2610 static WCHAR *field_format_serial_number(PCCERT_CONTEXT cert)
2611 {
2612 return format_hex_string(cert->pCertInfo->SerialNumber.pbData,
2613 cert->pCertInfo->SerialNumber.cbData);
2614 }
2615
2616 static WCHAR *field_format_issuer(PCCERT_CONTEXT cert)
2617 {
2618 return get_cert_name_string(cert, CERT_NAME_SIMPLE_DISPLAY_TYPE,
2619 CERT_NAME_ISSUER_FLAG);
2620 }
2621
2622 static WCHAR *field_format_detailed_cert_name(PCERT_NAME_BLOB name)
2623 {
2624 WCHAR *str = NULL;
2625 DWORD len = CertNameToStrW(X509_ASN_ENCODING, name,
2626 CERT_X500_NAME_STR | CERT_NAME_STR_CRLF_FLAG, NULL, 0);
2627
2628 if (len)
2629 {
2630 str = HeapAlloc(GetProcessHeap(), 0, len * sizeof(WCHAR));
2631 if (str)
2632 CertNameToStrW(X509_ASN_ENCODING, name,
2633 CERT_X500_NAME_STR | CERT_NAME_STR_CRLF_FLAG, str, len);
2634 }
2635 return str;
2636 }
2637
2638 static WCHAR *field_format_detailed_issuer(PCCERT_CONTEXT cert, void *param)
2639 {
2640 return field_format_detailed_cert_name(&cert->pCertInfo->Issuer);
2641 }
2642
2643 static WCHAR *field_format_subject(PCCERT_CONTEXT cert)
2644 {
2645 return get_cert_name_string(cert, CERT_NAME_SIMPLE_DISPLAY_TYPE, 0);
2646 }
2647
2648 static WCHAR *field_format_detailed_subject(PCCERT_CONTEXT cert, void *param)
2649 {
2650 return field_format_detailed_cert_name(&cert->pCertInfo->Subject);
2651 }
2652
2653 static WCHAR *format_long_date(const FILETIME *fileTime)
2654 {
2655 WCHAR dateFmt[80]; /* long enough for LOCALE_SLONGDATE */
2656 DWORD len;
2657 WCHAR *buf = NULL;
2658 SYSTEMTIME sysTime;
2659
2660 /* FIXME: format isn't quite right, want time too */
2661 GetLocaleInfoW(LOCALE_SYSTEM_DEFAULT, LOCALE_SLONGDATE, dateFmt,
2662 sizeof(dateFmt) / sizeof(dateFmt[0]));
2663 FileTimeToSystemTime(fileTime, &sysTime);
2664 len = GetDateFormatW(LOCALE_SYSTEM_DEFAULT, 0, &sysTime, dateFmt, NULL, 0);
2665 if (len)
2666 {
2667 buf = HeapAlloc(GetProcessHeap(), 0, len * sizeof(WCHAR));
2668 if (buf)
2669 GetDateFormatW(LOCALE_SYSTEM_DEFAULT, 0, &sysTime, dateFmt, buf,
2670 len);
2671 }
2672 return buf;
2673 }
2674
2675 static WCHAR *field_format_from_date(PCCERT_CONTEXT cert)
2676 {
2677 return format_long_date(&cert->pCertInfo->NotBefore);
2678 }
2679
2680 static WCHAR *field_format_to_date(PCCERT_CONTEXT cert)
2681 {
2682 return format_long_date(&cert->pCertInfo->NotAfter);
2683 }
2684
2685 static WCHAR *field_format_public_key(PCCERT_CONTEXT cert)
2686 {
2687 PCCRYPT_OID_INFO oidInfo;
2688 WCHAR *buf = NULL;
2689
2690 oidInfo = CryptFindOIDInfo(CRYPT_OID_INFO_OID_KEY,
2691 cert->pCertInfo->SubjectPublicKeyInfo.Algorithm.pszObjId, 0);
2692 if (oidInfo)
2693 {
2694 WCHAR fmt[MAX_STRING_LEN];
2695
2696 if (LoadStringW(hInstance, IDS_FIELD_PUBLIC_KEY_FORMAT, fmt,
2697 sizeof(fmt) / sizeof(fmt[0])))
2698 {
2699 DWORD len;
2700
2701 /* Allocate the output buffer. Use the number of bytes in the
2702 * public key as a conservative (high) estimate for the number of
2703 * digits in its output.
2704 * The output is of the form (in English)
2705 * "<public key algorithm> (<public key bit length> bits)".
2706 * Ordinarily having two positional parameters in a string is not a
2707 * good idea, but as this isn't a sentence fragment, it shouldn't
2708 * be word-order dependent.
2709 */
2710 len = strlenW(fmt) + strlenW(oidInfo->pwszName) +
2711 cert->pCertInfo->SubjectPublicKeyInfo.PublicKey.cbData * 8;
2712 buf = HeapAlloc(GetProcessHeap(), 0, len * sizeof(*buf));
2713 if (buf)
2714 {
2715 DWORD_PTR args[2];
2716 args[0] = (DWORD_PTR)oidInfo->pwszName;
2717 args[1] = CertGetPublicKeyLength(X509_ASN_ENCODING,
2718 &cert->pCertInfo->SubjectPublicKeyInfo);
2719 FormatMessageW(FORMAT_MESSAGE_FROM_STRING|FORMAT_MESSAGE_ARGUMENT_ARRAY,
2720 fmt, 0, 0, buf, len, (__ms_va_list*)args);
2721 }
2722 }
2723 }
2724 return buf;
2725 }
2726
2727 static WCHAR *field_format_detailed_public_key(PCCERT_CONTEXT cert, void *param)
2728 {
2729 return format_hex_string(
2730 cert->pCertInfo->SubjectPublicKeyInfo.PublicKey.pbData,
2731 cert->pCertInfo->SubjectPublicKeyInfo.PublicKey.cbData);
2732 }
2733
2734 struct field_value_data;
2735 struct detail_data
2736 {
2737 PCCRYPTUI_VIEWCERTIFICATE_STRUCTW pCertViewInfo;
2738 BOOL *pfPropertiesChanged;
2739 int cFields;
2740 struct field_value_data *fields;
2741 };
2742
2743 typedef void (*add_fields_func)(HWND hwnd, struct detail_data *data);
2744
2745 typedef WCHAR *(*create_detailed_value_func)(PCCERT_CONTEXT cert, void *param);
2746
2747 struct field_value_data
2748 {
2749 create_detailed_value_func create;
2750 LPWSTR detailed_value;
2751 void *param;
2752 };
2753
2754 static void add_field_value_data(struct detail_data *data,
2755 create_detailed_value_func create, void *param)
2756 {
2757 if (data->cFields)
2758 data->fields = HeapReAlloc(GetProcessHeap(), 0, data->fields,
2759 (data->cFields + 1) * sizeof(struct field_value_data));
2760 else
2761 data->fields = HeapAlloc(GetProcessHeap(), 0,
2762 sizeof(struct field_value_data));
2763 if (data->fields)
2764 {
2765 data->fields[data->cFields].create = create;
2766 data->fields[data->cFields].detailed_value = NULL;
2767 data->fields[data->cFields].param = param;
2768 data->cFields++;
2769 }
2770 }
2771
2772 static void add_field_and_value_to_list(HWND hwnd, struct detail_data *data,
2773 LPWSTR field, LPWSTR value, create_detailed_value_func create, void *param)
2774 {
2775 LVITEMW item;
2776 int iItem = SendMessageW(hwnd, LVM_GETITEMCOUNT, 0, 0);
2777
2778 item.mask = LVIF_TEXT | LVIF_PARAM;
2779 item.iItem = iItem;
2780 item.iSubItem = 0;
2781 item.pszText = field;
2782 item.lParam = (LPARAM)data;
2783 SendMessageW(hwnd, LVM_INSERTITEMW, 0, (LPARAM)&item);
2784 if (value)
2785 {
2786 item.pszText = value;
2787 item.iSubItem = 1;
2788 SendMessageW(hwnd, LVM_SETITEMTEXTW, iItem, (LPARAM)&item);
2789 }
2790 add_field_value_data(data, create, param);
2791 }
2792
2793 static void add_string_id_and_value_to_list(HWND hwnd, struct detail_data *data,
2794 int id, LPWSTR value, create_detailed_value_func create, void *param)
2795 {
2796 WCHAR buf[MAX_STRING_LEN];
2797
2798 LoadStringW(hInstance, id, buf, sizeof(buf) / sizeof(buf[0]));
2799 add_field_and_value_to_list(hwnd, data, buf, value, create, param);
2800 }
2801
2802 struct v1_field
2803 {
2804 int id;
2805 field_format_func format;
2806 create_detailed_value_func create_detailed_value;
2807 };
2808
2809 static void add_v1_field(HWND hwnd, struct detail_data *data,
2810 const struct v1_field *field)
2811 {
2812 WCHAR *val = field->format(data->pCertViewInfo->pCertContext);
2813
2814 if (val)
2815 {
2816 add_string_id_and_value_to_list(hwnd, data, field->id, val,
2817 field->create_detailed_value, NULL);
2818 HeapFree(GetProcessHeap(), 0, val);
2819 }
2820 }
2821
2822 static const struct v1_field v1_fields[] = {
2823 { IDS_FIELD_VERSION, field_format_version, NULL },
2824 { IDS_FIELD_SERIAL_NUMBER, field_format_serial_number, NULL },
2825 { IDS_FIELD_ISSUER, field_format_issuer, field_format_detailed_issuer },
2826 { IDS_FIELD_VALID_FROM, field_format_from_date, NULL },
2827 { IDS_FIELD_VALID_TO, field_format_to_date, NULL },
2828 { IDS_FIELD_SUBJECT, field_format_subject, field_format_detailed_subject },
2829 { IDS_FIELD_PUBLIC_KEY, field_format_public_key,
2830 field_format_detailed_public_key }
2831 };
2832
2833 static void add_v1_fields(HWND hwnd, struct detail_data *data)
2834 {
2835 unsigned int i;
2836 PCCERT_CONTEXT cert = data->pCertViewInfo->pCertContext;
2837
2838 /* The last item in v1_fields is the public key, which is not in the loop
2839 * because it's a special case.
2840 */
2841 for (i = 0; i < sizeof(v1_fields) / sizeof(v1_fields[0]) - 1; i++)
2842 add_v1_field(hwnd, data, &v1_fields[i]);
2843 if (cert->pCertInfo->SubjectPublicKeyInfo.PublicKey.cbData)
2844 add_v1_field(hwnd, data, &v1_fields[i]);
2845 }
2846
2847 static WCHAR *crypt_format_extension(const CERT_EXTENSION *ext, DWORD formatStrType)
2848 {
2849 WCHAR *str = NULL;
2850 DWORD size;
2851
2852 if (CryptFormatObject(X509_ASN_ENCODING, 0, formatStrType, NULL,
2853 ext->pszObjId, ext->Value.pbData, ext->Value.cbData, NULL, &size))
2854 {
2855 str = HeapAlloc(GetProcessHeap(), 0, size);
2856 CryptFormatObject(X509_ASN_ENCODING, 0, formatStrType, NULL,
2857 ext->pszObjId, ext->Value.pbData, ext->Value.cbData, str, &size);
2858 }
2859 return str;
2860 }
2861
2862 static WCHAR *field_format_extension_hex_with_ascii(const CERT_EXTENSION *ext)
2863 {
2864 WCHAR *str = NULL;
2865
2866 if (ext->Value.cbData)
2867 {
2868 /* The output is formatted as:
2869 * <hex bytes> <ascii bytes>\n
2870 * where <hex bytes> is a string of up to 8 bytes, output as %02x,
2871 * and <ascii bytes> is the ASCII equivalent of each byte, or '.' if
2872 * the byte is not printable.
2873 * So, for example, the extension value consisting of the following
2874 * bytes:
2875 * 0x30,0x14,0x31,0x12,0x30,0x10,0x06,0x03,0x55,0x04,0x03,
2876 * 0x13,0x09,0x4a,0x75,0x61,0x6e,0x20,0x4c,0x61,0x6e,0x67
2877 * is output as:
2878 * 30 14 31 12 30 10 06 03 0.1.0...
2879 * 55 04 03 13 09 4a 75 61 U....Jua
2880 * 6e 20 4c 61 6e 67 n Lang
2881 * The allocation size therefore requires:
2882 * - 4 characters per character in an 8-byte line
2883 * (2 for the hex format, one for the space, one for the ASCII value)
2884 * - 3 more characters per 8-byte line (two spaces and a newline)
2885 * - 1 character for the terminating nul
2886 * FIXME: should use a fixed-width font for this
2887 */
2888 DWORD lines = (ext->Value.cbData + 7) / 8;
2889
2890 str = HeapAlloc(GetProcessHeap(), 0,
2891 (lines * 8 * 4 + lines * 3 + 1) * sizeof(WCHAR));
2892 if (str)
2893 {
2894 static const WCHAR fmt[] = { '%','0','2','x',' ',0 };
2895 DWORD i, j;
2896 WCHAR *ptr;
2897
2898 for (i = 0, ptr = str; i < ext->Value.cbData; i += 8)
2899 {
2900 /* Output as hex bytes first */
2901 for (j = i; j < min(i + 8, ext->Value.cbData); j++, ptr += 3)
2902 sprintfW(ptr, fmt, ext->Value.pbData[j]);
2903 /* Pad the hex output with spaces for alignment */
2904 if (j == ext->Value.cbData && j % 8)
2905 {
2906 static const WCHAR pad[] = { ' ',' ',' ' };
2907
2908 for (; j % 8; j++, ptr += sizeof(pad) / sizeof(pad[0]))
2909 memcpy(ptr, pad, sizeof(pad));
2910 }
2911 /* The last sprintfW included a space, so just insert one
2912 * more space between the hex bytes and the ASCII output
2913 */
2914 *ptr++ = ' ';
2915 /* Output as ASCII bytes */
2916 for (j = i; j < min(i + 8, ext->Value.cbData); j++, ptr++)
2917 {
2918 if (isprintW(ext->Value.pbData[j]) &&
2919 !isspaceW(ext->Value.pbData[j]))
2920 *ptr = ext->Value.pbData[j];
2921 else
2922 *ptr = '.';
2923 }
2924 *ptr++ = '\n';
2925 }
2926 *ptr++ = '\0';
2927 }
2928 }
2929 return str;
2930 }
2931
2932 static WCHAR *field_format_detailed_extension(PCCERT_CONTEXT cert, void *param)
2933 {
2934 PCERT_EXTENSION ext = param;
2935 LPWSTR str = crypt_format_extension(ext,
2936 CRYPT_FORMAT_STR_MULTI_LINE | CRYPT_FORMAT_STR_NO_HEX);
2937
2938 if (!str)
2939 str = field_format_extension_hex_with_ascii(ext);
2940 return str;
2941 }
2942
2943 static void add_cert_extension_detail(HWND hwnd, struct detail_data *data,
2944 PCERT_EXTENSION ext)
2945 {
2946 PCCRYPT_OID_INFO oidInfo = CryptFindOIDInfo(CRYPT_OID_INFO_OID_KEY,
2947 ext->pszObjId, 0);
2948 LPWSTR val = crypt_format_extension(ext, 0);
2949
2950 if (oidInfo)
2951 add_field_and_value_to_list(hwnd, data, (LPWSTR)oidInfo->pwszName,
2952 val, field_format_detailed_extension, ext);
2953 else
2954 {
2955 DWORD len = strlen(ext->pszObjId);
2956 LPWSTR oidW = HeapAlloc(GetProcessHeap(), 0, (len + 1) * sizeof(WCHAR));
2957
2958 if (oidW)
2959 {
2960 DWORD i;
2961
2962 for (i = 0; i <= len; i++)
2963 oidW[i] = ext->pszObjId[i];
2964 add_field_and_value_to_list(hwnd, data, oidW, val,
2965 field_format_detailed_extension, ext);
2966 HeapFree(GetProcessHeap(), 0, oidW);
2967 }
2968 }
2969 HeapFree(GetProcessHeap(), 0, val);
2970 }
2971
2972 static void add_all_extensions(HWND hwnd, struct detail_data *data)
2973 {
2974 DWORD i;
2975 PCCERT_CONTEXT cert = data->pCertViewInfo->pCertContext;
2976
2977 for (i = 0; i < cert->pCertInfo->cExtension; i++)
2978 add_cert_extension_detail(hwnd, data, &cert->pCertInfo->rgExtension[i]);
2979 }
2980
2981 static void add_critical_extensions(HWND hwnd, struct detail_data *data)
2982 {
2983 DWORD i;
2984 PCCERT_CONTEXT cert = data->pCertViewInfo->pCertContext;
2985
2986 for (i = 0; i < cert->pCertInfo->cExtension; i++)
2987 if (cert->pCertInfo->rgExtension[i].fCritical)
2988 add_cert_extension_detail(hwnd, data,
2989 &cert->pCertInfo->rgExtension[i]);
2990 }
2991
2992 typedef WCHAR * (*prop_to_value_func)(void *pb, DWORD cb);
2993
2994 struct prop_id_to_string_id
2995 {
2996 DWORD prop;
2997 int id;
2998 BOOL prop_is_string;
2999 prop_to_value_func prop_to_value;
3000 };
3001
3002 static WCHAR *format_enhanced_key_usage_value(void *pb, DWORD cb)
3003 {
3004 CERT_EXTENSION ext;
3005
3006 ext.pszObjId = (LPSTR)X509_ENHANCED_KEY_USAGE;
3007 ext.fCritical = FALSE;
3008 ext.Value.pbData = pb;
3009 ext.Value.cbData = cb;
3010 return crypt_format_extension(&ext, 0);
3011 }
3012
3013 /* Logically the access state should also be checked, and IDC_EDITPROPERTIES
3014 * disabled for read-only certificates, but native doesn't appear to do that.
3015 */
3016 static const struct prop_id_to_string_id prop_id_map[] = {
3017 { CERT_HASH_PROP_ID, IDS_PROP_HASH, FALSE, format_hex_string },
3018 { CERT_FRIENDLY_NAME_PROP_ID, IDS_PROP_FRIENDLY_NAME, TRUE, NULL },
3019 { CERT_DESCRIPTION_PROP_ID, IDS_PROP_DESCRIPTION, TRUE, NULL },
3020 { CERT_ENHKEY_USAGE_PROP_ID, IDS_PROP_ENHKEY_USAGE, FALSE,
3021 format_enhanced_key_usage_value },
3022 };
3023
3024 static void add_properties(HWND hwnd, struct detail_data *data)
3025 {
3026 DWORD i;
3027 PCCERT_CONTEXT cert = data->pCertViewInfo->pCertContext;
3028
3029 for (i = 0; i < sizeof(prop_id_map) / sizeof(prop_id_map[0]); i++)
3030 {
3031 DWORD cb;
3032
3033 if (CertGetCertificateContextProperty(cert, prop_id_map[i].prop, NULL,
3034 &cb))
3035 {
3036 BYTE *pb;
3037 WCHAR *val = NULL;
3038
3039 /* FIXME: MS adds a separate value for the signature hash
3040 * algorithm.
3041 */
3042 pb = HeapAlloc(GetProcessHeap(), 0, cb);
3043 if (pb)
3044 {
3045 if (CertGetCertificateContextProperty(cert,
3046 prop_id_map[i].prop, pb, &cb))
3047 {
3048 if (prop_id_map[i].prop_is_string)
3049 {
3050 val = (LPWSTR)pb;
3051 /* Don't double-free pb */
3052 pb = NULL;
3053 }
3054 else
3055 val = prop_id_map[i].prop_to_value(pb, cb);
3056 }
3057 HeapFree(GetProcessHeap(), 0, pb);
3058 }
3059 add_string_id_and_value_to_list(hwnd, data, prop_id_map[i].id, val,
3060 NULL, NULL);
3061 }
3062 }
3063 }
3064
3065 static void add_all_fields(HWND hwnd, struct detail_data *data)
3066 {
3067 add_v1_fields(hwnd, data);
3068 add_all_extensions(hwnd, data);
3069 add_properties(hwnd, data);
3070 }
3071
3072 struct selection_list_item
3073 {
3074 int id;
3075 add_fields_func add;
3076 };
3077
3078 static const struct selection_list_item listItems[] = {
3079 { IDS_FIELDS_ALL, add_all_fields },
3080 { IDS_FIELDS_V1, add_v1_fields },
3081 { IDS_FIELDS_EXTENSIONS, add_all_extensions },
3082 { IDS_FIELDS_CRITICAL_EXTENSIONS, add_critical_extensions },
3083 { IDS_FIELDS_PROPERTIES, add_properties },
3084 };
3085
3086 static void create_show_list(HWND hwnd, struct detail_data *data)
3087 {
3088 HWND cb = GetDlgItem(hwnd, IDC_DETAIL_SELECT);
3089 WCHAR buf[MAX_STRING_LEN];
3090 int i;
3091
3092 for (i = 0; i < sizeof(listItems) / sizeof(listItems[0]); i++)
3093 {
3094 int index;
3095
3096 LoadStringW(hInstance, listItems[i].id, buf,
3097 sizeof(buf) / sizeof(buf[0]));
3098 index = SendMessageW(cb, CB_INSERTSTRING, -1, (LPARAM)buf);
3099 SendMessageW(cb, CB_SETITEMDATA, index, (LPARAM)data);
3100 }
3101 SendMessageW(cb, CB_SETCURSEL, 0, 0);
3102 }
3103
3104 static void create_listview_columns(HWND hwnd)
3105 {
3106 HWND lv = GetDlgItem(hwnd, IDC_DETAIL_LIST);
3107 RECT rc;
3108 WCHAR buf[MAX_STRING_LEN];
3109 LVCOLUMNW column;
3110
3111 SendMessageW(lv, LVM_SETEXTENDEDLISTVIEWSTYLE, 0, LVS_EX_FULLROWSELECT);
3112 GetWindowRect(lv, &rc);
3113 LoadStringW(hInstance, IDS_FIELD, buf, sizeof(buf) / sizeof(buf[0]));
3114 column.mask = LVCF_WIDTH | LVCF_TEXT;
3115 column.cx = (rc.right - rc.left) / 2 - 2;
3116 column.pszText = buf;
3117 SendMessageW(lv, LVM_INSERTCOLUMNW, 0, (LPARAM)&column);
3118 LoadStringW(hInstance, IDS_VALUE, buf, sizeof(buf) / sizeof(buf[0]));
3119 SendMessageW(lv, LVM_INSERTCOLUMNW, 1, (LPARAM)&column);
3120 }
3121
3122 static void set_fields_selection(HWND hwnd, struct detail_data *data, int sel)
3123 {
3124 HWND list = GetDlgItem(hwnd, IDC_DETAIL_LIST);
3125
3126 if (sel >= 0 && sel < sizeof(listItems) / sizeof(listItems[0]))
3127 {
3128 SendMessageW(list, LVM_DELETEALLITEMS, 0, 0);
3129 listItems[sel].add(list, data);
3130 }
3131 }
3132
3133 static void create_cert_details_list(HWND hwnd, struct detail_data *data)
3134 {
3135 create_show_list(hwnd, data);
3136 create_listview_columns(hwnd);
3137 set_fields_selection(hwnd, data, 0);
3138 }
3139
3140 static void add_purpose(HWND hwnd, LPCSTR oid)
3141 {
3142 HWND lv = GetDlgItem(hwnd, IDC_CERTIFICATE_USAGES);
3143 PCRYPT_OID_INFO info = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY,
3144 sizeof(CRYPT_OID_INFO));
3145
3146 if (info)
3147 {
3148 char *oidCopy = HeapAlloc(GetProcessHeap(), 0, strlen(oid) + 1);
3149
3150 if (oidCopy)
3151 {
3152 LVITEMA item;
3153
3154 strcpy(oidCopy, oid);
3155 info->cbSize = sizeof(CRYPT_OID_INFO);
3156 info->pszOID = oidCopy;
3157 item.mask = LVIF_TEXT | LVIF_STATE | LVIF_PARAM;
3158 item.state = INDEXTOSTATEIMAGEMASK(CheckBitmapIndexChecked);
3159 item.stateMask = LVIS_STATEIMAGEMASK;
3160 item.iItem = SendMessageW(lv, LVM_GETITEMCOUNT, 0, 0);
3161 item.iSubItem = 0;
3162 item.lParam = (LPARAM)info;
3163 item.pszText = oidCopy;
3164 SendMessageA(lv, LVM_INSERTITEMA, 0, (LPARAM)&item);
3165 }
3166 else
3167 HeapFree(GetProcessHeap(), 0, info);
3168 }
3169 }
3170
3171 static BOOL is_valid_oid(LPCSTR oid)
3172 {
3173 BOOL ret;
3174
3175 if (oid[0] != '0' && oid[0] != '1' && oid[0] != '2')
3176 ret = FALSE;
3177 else if (oid[1] != '.')
3178 ret = FALSE;
3179 else if (!oid[2])
3180 ret = FALSE;
3181 else
3182 {
3183 const char *ptr;
3184 BOOL expectNum = TRUE;
3185
3186 for (ptr = oid + 2, ret = TRUE; ret && *ptr; ptr++)
3187 {
3188 if (expectNum)
3189 {
3190 if (!isdigit(*ptr))
3191 ret = FALSE;
3192 else if (*(ptr + 1) == '.')
3193 expectNum = FALSE;
3194 }
3195 else
3196 {
3197 if (*ptr != '.')
3198 ret = FALSE;
3199 else if (!(*(ptr + 1)))
3200 ret = FALSE;
3201 else
3202 expectNum = TRUE;
3203 }
3204 }
3205 }
3206 return ret;
3207 }
3208
3209 static BOOL is_oid_in_list(HWND hwnd, LPCSTR oid)
3210 {
3211 return find_oid_in_list(GetDlgItem(hwnd, IDC_CERTIFICATE_USAGES), oid)
3212 != -1;
3213 }
3214
3215 #define MAX_PURPOSE 255
3216
3217 static LRESULT CALLBACK add_purpose_dlg_proc(HWND hwnd, UINT msg,
3218 WPARAM wp, LPARAM lp)
3219 {
3220 LRESULT ret = 0;
3221 char buf[MAX_PURPOSE + 1];
3222
3223 switch (msg)
3224 {
3225 case WM_INITDIALOG:
3226 SendMessageW(GetDlgItem(hwnd, IDC_NEW_PURPOSE), EM_SETLIMITTEXT,
3227 MAX_PURPOSE, 0);
3228 ShowScrollBar(GetDlgItem(hwnd, IDC_NEW_PURPOSE), SB_VERT, FALSE);
3229 SetWindowLongPtrW(hwnd, DWLP_USER, lp);
3230 break;
3231 case WM_COMMAND:
3232 switch (HIWORD(wp))
3233 {
3234 case EN_CHANGE:
3235 if (LOWORD(wp) == IDC_NEW_PURPOSE)
3236 {
3237 /* Show/hide scroll bar on description depending on how much
3238 * text it has.
3239 */
3240 HWND description = GetDlgItem(hwnd, IDC_NEW_PURPOSE);
3241 int lines = SendMessageW(description, EM_GETLINECOUNT, 0, 0);
3242
3243 ShowScrollBar(description, SB_VERT, lines > 1);
3244 }
3245 break;
3246 case BN_CLICKED:
3247 switch (LOWORD(wp))
3248 {
3249 case IDOK:
3250 SendMessageA(GetDlgItem(hwnd, IDC_NEW_PURPOSE), WM_GETTEXT,
3251 sizeof(buf) / sizeof(buf[0]), (LPARAM)buf);
3252 if (!buf[0])
3253 {
3254 /* An empty purpose is the same as cancelling */
3255 EndDialog(hwnd, IDCANCEL);
3256 ret = TRUE;
3257 }
3258 else if (!is_valid_oid(buf))
3259 {
3260 WCHAR title[MAX_STRING_LEN], error[MAX_STRING_LEN];
3261
3262 LoadStringW(hInstance, IDS_CERTIFICATE_PURPOSE_ERROR, error,
3263 sizeof(error) / sizeof(error[0]));
3264 LoadStringW(hInstance, IDS_CERTIFICATE_PROPERTIES, title,
3265 sizeof(title) / sizeof(title[0]));
3266 MessageBoxW(hwnd, error, title, MB_ICONERROR | MB_OK);
3267 }
3268 else if (is_oid_in_list(
3269 (HWND)GetWindowLongPtrW(hwnd, DWLP_USER), buf))
3270 {
3271 WCHAR title[MAX_STRING_LEN], error[MAX_STRING_LEN];
3272
3273 LoadStringW(hInstance, IDS_CERTIFICATE_PURPOSE_EXISTS,
3274 error, sizeof(error) / sizeof(error[0]));
3275 LoadStringW(hInstance, IDS_CERTIFICATE_PROPERTIES, title,
3276 sizeof(title) / sizeof(title[0]));
3277 MessageBoxW(hwnd, error, title, MB_ICONEXCLAMATION | MB_OK);
3278 }
3279 else
3280 {
3281 HWND parent = (HWND)GetWindowLongPtrW(hwnd, DWLP_USER);
3282
3283 add_purpose(parent, buf);
3284 EndDialog(hwnd, wp);
3285 ret = TRUE;
3286 }
3287 break;
3288 case IDCANCEL:
3289 EndDialog(hwnd, wp);
3290 ret = TRUE;
3291 break;
3292 }
3293 break;
3294 }
3295 break;
3296 }
3297 return ret;
3298 }
3299
3300 static WCHAR *get_cert_property_as_string(PCCERT_CONTEXT cert, DWORD prop)
3301 {
3302 WCHAR *name = NULL;
3303 DWORD cb;
3304
3305 if (CertGetCertificateContextProperty(cert, prop, NULL, &cb))
3306 {
3307 name = HeapAlloc(GetProcessHeap(), 0, cb);
3308 if (name)
3309 {
3310 if (!CertGetCertificateContextProperty(cert, prop, name, &cb))
3311 {
3312 HeapFree(GetProcessHeap(), 0, name);
3313 name = NULL;
3314 }
3315 }
3316 }
3317 return name;
3318 }
3319
3320 static void redraw_states(HWND list, BOOL enabled)
3321 {
3322 int items = SendMessageW(list, LVM_GETITEMCOUNT, 0, 0), i;
3323
3324 for (i = 0; i < items; i++)
3325 {
3326 BOOL change = FALSE;
3327 int state;
3328
3329 state = SendMessageW(list, LVM_GETITEMSTATE, i, LVIS_STATEIMAGEMASK);
3330 /* This reverses the INDEXTOSTATEIMAGEMASK shift. There doesn't appear
3331 * to be a handy macro for it.
3332 */
3333 state >>= 12;
3334 if (enabled)
3335 {
3336 if (state == CheckBitmapIndexDisabledChecked)
3337 {
3338 state = CheckBitmapIndexChecked;
3339 change = TRUE;
3340 }
3341 if (state == CheckBitmapIndexDisabledUnchecked)
3342 {
3343 state = CheckBitmapIndexUnchecked;
3344 change = TRUE;
3345 }
3346 }
3347 else
3348 {
3349 if (state == CheckBitmapIndexChecked)
3350 {
3351 state = CheckBitmapIndexDisabledChecked;
3352 change = TRUE;
3353 }
3354 if (state == CheckBitmapIndexUnchecked)
3355 {
3356 state = CheckBitmapIndexDisabledUnchecked;
3357 change = TRUE;
3358 }
3359 }
3360 if (change)
3361 {
3362 LVITEMW item;
3363
3364 item.state = INDEXTOSTATEIMAGEMASK(state);
3365 item.stateMask = LVIS_STATEIMAGEMASK;
3366 SendMessageW(list, LVM_SETITEMSTATE, i, (LPARAM)&item);
3367 }
3368 }
3369 }
3370
3371 typedef enum {
3372 PurposeEnableAll = 0,
3373 PurposeDisableAll,
3374 PurposeEnableSelected
3375 } PurposeSelection;
3376
3377 static void select_purposes(HWND hwnd, PurposeSelection selection)
3378 {
3379 HWND lv = GetDlgItem(hwnd, IDC_CERTIFICATE_USAGES);
3380
3381 switch (selection)
3382 {
3383 case PurposeEnableAll:
3384 case PurposeDisableAll:
3385 EnableWindow(lv, FALSE);
3386 redraw_states(lv, FALSE);
3387 EnableWindow(GetDlgItem(hwnd, IDC_ADD_PURPOSE), FALSE);
3388 break;
3389 case PurposeEnableSelected:
3390 EnableWindow(lv, TRUE);
3391 redraw_states(lv, TRUE);
3392 EnableWindow(GetDlgItem(hwnd, IDC_ADD_PURPOSE), TRUE);
3393 }
3394 }
3395
3396 struct edit_cert_data
3397 {
3398 PCCERT_CONTEXT cert;
3399 BOOL *pfPropertiesChanged;
3400 HIMAGELIST imageList;
3401 };
3402
3403 static void show_cert_usages(HWND hwnd, struct edit_cert_data *data)
3404 {
3405 PCCERT_CONTEXT cert = data->cert;
3406 HWND lv = GetDlgItem(hwnd, IDC_CERTIFICATE_USAGES);
3407 PCERT_ENHKEY_USAGE usage;
3408 DWORD size;
3409 RECT rc;
3410 LVCOLUMNW column;
3411 PurposeSelection purposeSelection = PurposeEnableAll;
3412
3413 GetWindowRect(lv, &rc);
3414 column.mask = LVCF_WIDTH;
3415 column.cx = rc.right - rc.left;
3416 SendMessageW(lv, LVM_INSERTCOLUMNW, 0, (LPARAM)&column);
3417 SendMessageW(lv, LVM_SETIMAGELIST, LVSIL_STATE, (LPARAM)data->imageList);
3418
3419 /* Get enhanced key usage. Have to check for a property and an extension
3420 * separately, because CertGetEnhancedKeyUsage will succeed and return an
3421 * empty usage if neither is set. Unfortunately an empty usage implies
3422 * no usage is allowed, so we have to distinguish between the two cases.
3423 */
3424 if (CertGetEnhancedKeyUsage(cert, CERT_FIND_PROP_ONLY_ENHKEY_USAGE_FLAG,
3425 NULL, &size))
3426 {
3427 usage = HeapAlloc(GetProcessHeap(), 0, size);
3428 if (!CertGetEnhancedKeyUsage(cert,
3429 CERT_FIND_PROP_ONLY_ENHKEY_USAGE_FLAG, usage, &size))
3430 {
3431 HeapFree(GetProcessHeap(), 0, usage);
3432 usage = NULL;
3433 }
3434 else if (usage->cUsageIdentifier)
3435 purposeSelection = PurposeEnableSelected;
3436 else
3437 purposeSelection = PurposeDisableAll;
3438 }
3439 else if (CertGetEnhancedKeyUsage(cert, CERT_FIND_EXT_ONLY_ENHKEY_USAGE_FLAG,
3440 NULL, &size))
3441 {
3442 usage = HeapAlloc(GetProcessHeap(), 0, size);
3443 if (!CertGetEnhancedKeyUsage(cert,
3444 CERT_FIND_EXT_ONLY_ENHKEY_USAGE_FLAG, usage, &size))
3445 {
3446 HeapFree(GetProcessHeap(), 0, usage);
3447 usage = NULL;
3448 }
3449 else if (usage->cUsageIdentifier)
3450 purposeSelection = PurposeEnableAll;
3451 else
3452 purposeSelection = PurposeDisableAll;
3453 }
3454 else
3455 {
3456 purposeSelection = PurposeEnableAll;
3457 usage = NULL;
3458 }
3459 if (usage)
3460 {
3461 DWORD i;
3462
3463 for (i = 0; i < usage->cUsageIdentifier; i++)
3464 {
3465 PCCRYPT_OID_INFO info = CryptFindOIDInfo(CRYPT_OID_INFO_OID_KEY,
3466 usage->rgpszUsageIdentifier[i], CRYPT_ENHKEY_USAGE_OID_GROUP_ID);
3467
3468 if (info)
3469 add_known_usage(lv, info, CheckBitmapIndexDisabledChecked);
3470 else
3471 add_purpose(hwnd, usage->rgpszUsageIdentifier[i]);
3472 }
3473 HeapFree(GetProcessHeap(), 0, usage);
3474 }
3475 else
3476 add_known_usages_to_list(lv, CheckBitmapIndexDisabledChecked);
3477 select_purposes(hwnd, purposeSelection);
3478 SendMessageW(GetDlgItem(hwnd, IDC_ENABLE_ALL_PURPOSES + purposeSelection),
3479 BM_CLICK, 0, 0);
3480 }
3481
3482 static void set_general_cert_properties(HWND hwnd, struct edit_cert_data *data)
3483 {
3484 PCCERT_CONTEXT cert = data->cert;
3485 WCHAR *str;
3486
3487 if ((str = get_cert_property_as_string(cert, CERT_FRIENDLY_NAME_PROP_ID)))
3488 {
3489 SendMessageW(GetDlgItem(hwnd, IDC_FRIENDLY_NAME), WM_SETTEXT, 0,
3490 (LPARAM)str);
3491 HeapFree(GetProcessHeap(), 0, str);
3492 }
3493 if ((str = get_cert_property_as_string(cert, CERT_DESCRIPTION_PROP_ID)))
3494 {
3495 SendMessageW(GetDlgItem(hwnd, IDC_DESCRIPTION), WM_SETTEXT, 0,
3496 (LPARAM)str);
3497 HeapFree(GetProcessHeap(), 0, str);
3498 }
3499 show_cert_usages(hwnd, data);
3500 }
3501
3502 static void set_cert_string_property(PCCERT_CONTEXT cert, DWORD prop,
3503 LPWSTR str)
3504 {
3505 if (str && strlenW(str))
3506 {
3507 CRYPT_DATA_BLOB blob;
3508
3509 blob.pbData = (BYTE *)str;
3510 blob.cbData = (strlenW(str) + 1) * sizeof(WCHAR);
3511 CertSetCertificateContextProperty(cert, prop, 0, &blob);
3512 }
3513 else
3514 CertSetCertificateContextProperty(cert, prop, 0, NULL);
3515 }
3516
3517 #define WM_REFRESH_VIEW WM_USER + 0
3518
3519 static BOOL CALLBACK refresh_propsheet_pages(HWND hwnd, LPARAM lParam)
3520 {
3521 if ((GetClassLongW(hwnd, GCW_ATOM) == WC_DIALOG))
3522 SendMessageW(hwnd, WM_REFRESH_VIEW, 0, 0);
3523 return TRUE;
3524 }
3525
3526 #define MAX_FRIENDLY_NAME 40
3527 #define MAX_DESCRIPTION 255
3528
3529 static void apply_general_changes(HWND hwnd)
3530 {
3531 WCHAR buf[MAX_DESCRIPTION + 1];
3532 struct edit_cert_data *data =
3533 (struct edit_cert_data *)GetWindowLongPtrW(hwnd, DWLP_USER);
3534
3535 SendMessageW(GetDlgItem(hwnd, IDC_FRIENDLY_NAME), WM_GETTEXT,
3536 sizeof(buf) / sizeof(buf[0]), (LPARAM)buf);
3537 set_cert_string_property(data->cert, CERT_FRIENDLY_NAME_PROP_ID, buf);
3538 SendMessageW(GetDlgItem(hwnd, IDC_DESCRIPTION), WM_GETTEXT,
3539 sizeof(buf) / sizeof(buf[0]), (LPARAM)buf);
3540 set_cert_string_property(data->cert, CERT_DESCRIPTION_PROP_ID, buf);
3541 if (IsDlgButtonChecked(hwnd, IDC_ENABLE_ALL_PURPOSES))
3542 {
3543 /* Setting a NULL usage removes the enhanced key usage property. */
3544 CertSetEnhancedKeyUsage(data->cert, NULL);
3545 }
3546 else if (IsDlgButtonChecked(hwnd, IDC_DISABLE_ALL_PURPOSES))
3547 {
3548 CERT_ENHKEY_USAGE usage = { 0, NULL };
3549
3550 CertSetEnhancedKeyUsage(data->cert, &usage);
3551 }
3552 else if (IsDlgButtonChecked(hwnd, IDC_ENABLE_SELECTED_PURPOSES))
3553 {
3554 HWND lv = GetDlgItem(hwnd, IDC_CERTIFICATE_USAGES);
3555 CERT_ENHKEY_USAGE usage = { 0, NULL };
3556 int purposes = SendMessageW(lv, LVM_GETITEMCOUNT, 0, 0), i;
3557 LVITEMW item;
3558
3559 item.mask = LVIF_STATE | LVIF_PARAM;
3560 item.iSubItem = 0;
3561 item.stateMask = LVIS_STATEIMAGEMASK;
3562 for (i = 0; i < purposes; i++)
3563 {
3564 item.iItem = i;
3565 if (SendMessageW(lv, LVM_GETITEMW, 0, (LPARAM)&item))
3566 {
3567 int state = item.state >> 12;
3568
3569 if (state == CheckBitmapIndexChecked)
3570 {
3571 CRYPT_OID_INFO *info = (CRYPT_OID_INFO *)item.lParam;
3572
3573 if (usage.cUsageIdentifier)
3574 usage.rgpszUsageIdentifier =
3575 HeapReAlloc(GetProcessHeap(), 0,
3576 usage.rgpszUsageIdentifier,
3577 (usage.cUsageIdentifier + 1) * sizeof(LPSTR));
3578 else
3579 usage.rgpszUsageIdentifier =
3580 HeapAlloc(GetProcessHeap(), 0, sizeof(LPSTR));
3581 if (usage.rgpszUsageIdentifier)
3582 usage.rgpszUsageIdentifier[usage.cUsageIdentifier++] =
3583 (LPSTR)info->pszOID;
3584 }
3585 }
3586 }
3587 CertSetEnhancedKeyUsage(data->cert, &usage);
3588 HeapFree(GetProcessHeap(), 0, usage.rgpszUsageIdentifier);
3589 }
3590 EnumChildWindows(GetParent(GetParent(hwnd)), refresh_propsheet_pages, 0);
3591 if (data->pfPropertiesChanged)
3592 *data->pfPropertiesChanged = TRUE;
3593 }
3594
3595 static LRESULT CALLBACK cert_properties_general_dlg_proc(HWND hwnd, UINT msg,
3596 WPARAM wp, LPARAM lp)
3597 {
3598 PROPSHEETPAGEW *page;
3599
3600 TRACE("(%p, %08x, %08lx, %08lx)\n", hwnd, msg, wp, lp);
3601
3602 switch (msg)
3603 {
3604 case WM_INITDIALOG:
3605 {
3606 HWND description = GetDlgItem(hwnd, IDC_DESCRIPTION);
3607 struct detail_data *detailData;
3608 struct edit_cert_data *editData;
3609
3610 page = (PROPSHEETPAGEW *)lp;
3611 detailData = (struct detail_data *)page->lParam;
3612 SendMessageW(GetDlgItem(hwnd, IDC_FRIENDLY_NAME), EM_SETLIMITTEXT,
3613 MAX_FRIENDLY_NAME, 0);
3614 SendMessageW(description, EM_SETLIMITTEXT, MAX_DESCRIPTION, 0);
3615 ShowScrollBar(description, SB_VERT, FALSE);
3616 editData = HeapAlloc(GetProcessHeap(), 0,
3617 sizeof(struct edit_cert_data));
3618 if (editData)
3619 {
3620 editData->imageList = ImageList_Create(16, 16,
3621 ILC_COLOR4 | ILC_MASK, 4, 0);
3622 if (editData->imageList)
3623 {
3624 HBITMAP bmp;
3625 COLORREF backColor = RGB(255, 0, 255);
3626
3627 bmp = LoadBitmapW(hInstance, MAKEINTRESOURCEW(IDB_CHECKS));
3628 ImageList_AddMasked(editData->imageList, bmp, backColor);
3629 DeleteObject(bmp);
3630 ImageList_SetBkColor(editData->imageList, CLR_NONE);
3631 }
3632 editData->cert = detailData->pCertViewInfo->pCertContext;