comctl32: Remove the masked background when 32bpp bitmaps have no alpha values.
[wine.git] / dlls / cryptui / main.c
blobe37b39b8ee16c03991130a8b0c2cdd5cf69d3414
1 /*
2 * Copyright 2008 Juan Lang
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.
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.
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
19 #include <stdarg.h>
20 #include <wchar.h>
22 #define COBJMACROS
23 #define NONAMELESSUNION
25 #include "windef.h"
26 #include "winbase.h"
27 #include "winnls.h"
28 #include "winuser.h"
29 #include "softpub.h"
30 #include "wingdi.h"
31 #include "richedit.h"
32 #include "ole2.h"
33 #include "richole.h"
34 #include "commdlg.h"
35 #include "commctrl.h"
36 #include "cryptuiapi.h"
37 #include "cryptuires.h"
38 #include "urlmon.h"
39 #include "hlink.h"
40 #include "winreg.h"
41 #include "wine/debug.h"
43 WINE_DEFAULT_DEBUG_CHANNEL(cryptui);
45 static HINSTANCE hInstance;
47 static const WCHAR empty[] = {0};
49 BOOL WINAPI DllMain(HINSTANCE hinstDLL, DWORD fdwReason, LPVOID lpvReserved)
51 TRACE("(0x%p, %d, %p)\n", hinstDLL, fdwReason, lpvReserved);
53 switch (fdwReason)
55 case DLL_WINE_PREATTACH:
56 return FALSE; /* prefer native version */
57 case DLL_PROCESS_ATTACH:
58 hInstance = hinstDLL;
59 DisableThreadLibraryCalls(hinstDLL);
60 break;
62 return TRUE;
65 static WCHAR *strdupAtoW( const char *str )
67 DWORD len = MultiByteToWideChar( CP_ACP, 0, str, -1, NULL, 0 );
68 WCHAR *ret = HeapAlloc( GetProcessHeap(), 0, len * sizeof(WCHAR) );
69 if (ret) MultiByteToWideChar( CP_ACP, 0, str, -1, ret, len );
70 return ret;
73 #define MAX_STRING_LEN 512
75 static void add_cert_columns(HWND hwnd)
77 HWND lv = GetDlgItem(hwnd, IDC_MGR_CERTS);
78 RECT rc;
79 WCHAR buf[MAX_STRING_LEN];
80 LVCOLUMNW column;
82 SendMessageW(lv, LVM_SETEXTENDEDLISTVIEWSTYLE, 0, LVS_EX_FULLROWSELECT);
83 GetWindowRect(lv, &rc);
84 LoadStringW(hInstance, IDS_SUBJECT_COLUMN, buf, ARRAY_SIZE(buf));
85 column.mask = LVCF_WIDTH | LVCF_TEXT;
86 column.cx = (rc.right - rc.left) * 29 / 100 - 2;
87 column.pszText = buf;
88 SendMessageW(lv, LVM_INSERTCOLUMNW, 0, (LPARAM)&column);
89 LoadStringW(hInstance, IDS_ISSUER_COLUMN, buf, ARRAY_SIZE(buf));
90 SendMessageW(lv, LVM_INSERTCOLUMNW, 1, (LPARAM)&column);
91 column.cx = (rc.right - rc.left) * 16 / 100 - 2;
92 LoadStringW(hInstance, IDS_EXPIRATION_COLUMN, buf, ARRAY_SIZE(buf));
93 SendMessageW(lv, LVM_INSERTCOLUMNW, 2, (LPARAM)&column);
94 column.cx = (rc.right - rc.left) * 23 / 100 - 1;
95 LoadStringW(hInstance, IDS_FRIENDLY_NAME_COLUMN, buf, ARRAY_SIZE(buf));
96 SendMessageW(lv, LVM_INSERTCOLUMNW, 3, (LPARAM)&column);
99 static void add_cert_to_view(HWND lv, PCCERT_CONTEXT cert, DWORD *allocatedLen,
100 LPWSTR *str)
102 DWORD len;
103 LVITEMW item;
104 WCHAR dateFmt[80]; /* sufficient for LOCALE_SSHORTDATE */
105 WCHAR date[80];
106 SYSTEMTIME sysTime;
107 LPWSTR none;
109 item.mask = LVIF_IMAGE | LVIF_PARAM | LVIF_TEXT;
110 item.iItem = SendMessageW(lv, LVM_GETITEMCOUNT, 0, 0);
111 item.iSubItem = 0;
112 item.iImage = 0;
113 item.lParam = (LPARAM)CertDuplicateCertificateContext(cert);
114 len = CertGetNameStringW(cert, CERT_NAME_SIMPLE_DISPLAY_TYPE, 0, NULL,
115 NULL, 0);
116 if (len > *allocatedLen)
118 HeapFree(GetProcessHeap(), 0, *str);
119 *str = HeapAlloc(GetProcessHeap(), 0, len * sizeof(WCHAR));
120 if (*str)
121 *allocatedLen = len;
123 if (*str)
125 CertGetNameStringW(cert, CERT_NAME_SIMPLE_DISPLAY_TYPE, 0, NULL,
126 *str, len);
127 item.pszText = *str;
128 SendMessageW(lv, LVM_INSERTITEMW, 0, (LPARAM)&item);
131 item.mask = LVIF_TEXT;
132 len = CertGetNameStringW(cert, CERT_NAME_SIMPLE_DISPLAY_TYPE,
133 CERT_NAME_ISSUER_FLAG, NULL, NULL, 0);
134 if (len > *allocatedLen)
136 HeapFree(GetProcessHeap(), 0, *str);
137 *str = HeapAlloc(GetProcessHeap(), 0, len * sizeof(WCHAR));
138 if (*str)
139 *allocatedLen = len;
141 if (*str)
143 CertGetNameStringW(cert, CERT_NAME_SIMPLE_DISPLAY_TYPE,
144 CERT_NAME_ISSUER_FLAG, NULL, *str, len);
145 item.pszText = *str;
146 item.iSubItem = 1;
147 SendMessageW(lv, LVM_SETITEMTEXTW, item.iItem, (LPARAM)&item);
150 GetLocaleInfoW(LOCALE_SYSTEM_DEFAULT, LOCALE_SSHORTDATE, dateFmt, ARRAY_SIZE(dateFmt));
151 FileTimeToSystemTime(&cert->pCertInfo->NotAfter, &sysTime);
152 GetDateFormatW(LOCALE_SYSTEM_DEFAULT, 0, &sysTime, dateFmt, date, ARRAY_SIZE(date));
153 item.pszText = date;
154 item.iSubItem = 2;
155 SendMessageW(lv, LVM_SETITEMTEXTW, item.iItem, (LPARAM)&item);
157 if (!CertGetCertificateContextProperty(cert, CERT_FRIENDLY_NAME_PROP_ID,
158 NULL, &len))
159 len = LoadStringW(hInstance, IDS_FRIENDLY_NAME_NONE, (LPWSTR)&none, 0);
160 if (len > *allocatedLen)
162 HeapFree(GetProcessHeap(), 0, *str);
163 *str = HeapAlloc(GetProcessHeap(), 0, len * sizeof(WCHAR));
164 if (*str)
165 *allocatedLen = len;
167 if (*str)
169 if (!CertGetCertificateContextProperty(cert, CERT_FRIENDLY_NAME_PROP_ID,
170 *str, &len))
171 item.pszText = none;
172 else
173 item.pszText = *str;
174 item.iSubItem = 3;
175 SendMessageW(lv, LVM_SETITEMTEXTW, item.iItem, (LPARAM)&item);
179 static LPSTR get_cert_mgr_usages(void)
181 static const WCHAR keyName[] = { 'S','o','f','t','w','a','r','e','\\','M',
182 'i','c','r','o','s','o','f','t','\\','C','r','y','p','t','o','g','r','a',
183 'p','h','y','\\','U','I','\\','C','e','r','t','m','g','r','\\','P','u',
184 'r','p','o','s','e',0 };
185 LPSTR str = NULL;
186 HKEY key;
188 if (!RegCreateKeyExW(HKEY_CURRENT_USER, keyName, 0, NULL, 0, KEY_READ,
189 NULL, &key, NULL))
191 LONG rc;
192 DWORD type, size;
194 rc = RegQueryValueExA(key, "Purpose", NULL, &type, NULL, &size);
195 if ((!rc || rc == ERROR_MORE_DATA) && type == REG_SZ)
197 str = HeapAlloc(GetProcessHeap(), 0, size);
198 if (str)
200 rc = RegQueryValueExA(key, "Purpose", NULL, NULL, (LPBYTE)str,
201 &size);
202 if (rc)
204 HeapFree(GetProcessHeap(), 0, str);
205 str = NULL;
209 RegCloseKey(key);
211 return str;
214 typedef enum {
215 PurposeFilterShowAll = 0,
216 PurposeFilterShowAdvanced = 1,
217 PurposeFilterShowOID = 2
218 } PurposeFilter;
220 static void initialize_purpose_selection(HWND hwnd)
222 HWND cb = GetDlgItem(hwnd, IDC_MGR_PURPOSE_SELECTION);
223 WCHAR buf[MAX_STRING_LEN];
224 LPSTR usages;
225 int index;
227 LoadStringW(hInstance, IDS_PURPOSE_ALL, buf, ARRAY_SIZE(buf));
228 index = SendMessageW(cb, CB_INSERTSTRING, -1, (LPARAM)buf);
229 SendMessageW(cb, CB_SETITEMDATA, index, (LPARAM)PurposeFilterShowAll);
230 LoadStringW(hInstance, IDS_PURPOSE_ADVANCED, buf, ARRAY_SIZE(buf));
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()))
236 LPSTR ptr, comma;
238 for (ptr = usages, comma = strchr(ptr, ','); ptr && *ptr;
239 ptr = comma ? comma + 1 : NULL,
240 comma = ptr ? strchr(ptr, ',') : NULL)
242 PCCRYPT_OID_INFO info;
244 if (comma)
245 *comma = 0;
246 if ((info = CryptFindOIDInfo(CRYPT_OID_INFO_OID_KEY, ptr, 0)))
248 index = SendMessageW(cb, CB_INSERTSTRING, 0,
249 (LPARAM)info->pwszName);
250 SendMessageW(cb, CB_SETITEMDATA, index, (LPARAM)info);
253 HeapFree(GetProcessHeap(), 0, usages);
257 extern BOOL WINAPI WTHelperGetKnownUsages(DWORD action,
258 PCCRYPT_OID_INFO **usages);
260 static CERT_ENHKEY_USAGE *add_oid_to_usage(CERT_ENHKEY_USAGE *usage, LPSTR oid)
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
273 HeapFree(GetProcessHeap(), 0, usage);
274 usage = NULL;
276 return usage;
279 static CERT_ENHKEY_USAGE *convert_usages_str_to_usage(LPSTR usageStr)
281 CERT_ENHKEY_USAGE *usage = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY,
282 sizeof(CERT_ENHKEY_USAGE));
284 if (usage)
286 LPSTR ptr, comma;
288 for (ptr = usageStr, comma = strchr(ptr, ','); usage && ptr && *ptr;
289 ptr = comma ? comma + 1 : NULL,
290 comma = ptr ? strchr(ptr, ',') : NULL)
292 if (comma)
293 *comma = 0;
294 usage = add_oid_to_usage(usage, ptr);
297 return usage;
300 static CERT_ENHKEY_USAGE *create_advanced_filter(void)
302 CERT_ENHKEY_USAGE *advancedUsage = HeapAlloc(GetProcessHeap(),
303 HEAP_ZERO_MEMORY, sizeof(CERT_ENHKEY_USAGE));
305 if (advancedUsage)
307 PCCRYPT_OID_INFO *usages;
309 if (WTHelperGetKnownUsages(1, &usages))
311 LPSTR disabledUsagesStr;
313 if ((disabledUsagesStr = get_cert_mgr_usages()))
315 CERT_ENHKEY_USAGE *disabledUsages =
316 convert_usages_str_to_usage(disabledUsagesStr);
318 if (disabledUsages)
320 PCCRYPT_OID_INFO *ptr;
322 for (ptr = usages; advancedUsage && *ptr; ptr++)
324 DWORD i;
325 BOOL disabled = FALSE;
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);
336 /* The individual strings are pointers to disabledUsagesStr,
337 * so they're freed when it is.
339 HeapFree(GetProcessHeap(), 0,
340 disabledUsages->rgpszUsageIdentifier);
341 HeapFree(GetProcessHeap(), 0, disabledUsages);
343 HeapFree(GetProcessHeap(), 0, disabledUsagesStr);
345 WTHelperGetKnownUsages(2, &usages);
348 return advancedUsage;
351 static int CALLBACK cert_mgr_sort_by_subject(LPARAM lp1, LPARAM lp2, LPARAM lp);
353 static void show_store_certs(HWND hwnd, HCERTSTORE store)
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;
365 index = SendMessageW(cb, CB_GETCURSEL, 0, 0);
366 if (index >= 0)
368 INT_PTR data = SendMessageW(cb, CB_GETITEMDATA, index, 0);
370 if (!HIWORD(data))
371 filter = data;
372 else
374 PCCRYPT_OID_INFO info = (PCCRYPT_OID_INFO)data;
376 filter = PurposeFilterShowOID;
377 oid = info->pszOID;
380 if (filter == PurposeFilterShowAdvanced)
381 advanced = create_advanced_filter();
382 do {
383 cert = CertEnumCertificatesInStore(store, cert);
384 if (cert)
386 BOOL show = FALSE;
388 if (filter == PurposeFilterShowAll)
389 show = TRUE;
390 else
392 int numOIDs;
393 DWORD cbOIDs = 0;
395 if (CertGetValidUsages(1, &cert, &numOIDs, NULL, &cbOIDs))
397 if (numOIDs == -1)
399 /* -1 implies all usages are valid */
400 show = TRUE;
402 else
404 LPSTR *oids = HeapAlloc(GetProcessHeap(), 0, cbOIDs);
406 if (oids)
408 if (CertGetValidUsages(1, &cert, &numOIDs, oids,
409 &cbOIDs))
411 int i;
413 if (filter == PurposeFilterShowOID)
415 for (i = 0; !show && i < numOIDs; i++)
416 if (!strcmp(oids[i], oid))
417 show = TRUE;
419 else
421 for (i = 0; !show && i < numOIDs; i++)
423 DWORD j;
425 for (j = 0; !show &&
426 j < advanced->cUsageIdentifier; j++)
427 if (!strcmp(oids[i],
428 advanced->rgpszUsageIdentifier[j]))
429 show = TRUE;
433 HeapFree(GetProcessHeap(), 0, oids);
438 if (show)
439 add_cert_to_view(lv, cert, &allocatedLen, &str);
441 } while (cert);
442 HeapFree(GetProcessHeap(), 0, str);
443 if (advanced)
445 HeapFree(GetProcessHeap(), 0, advanced->rgpszUsageIdentifier);
446 HeapFree(GetProcessHeap(), 0, advanced);
448 SendMessageW(lv, LVM_SORTITEMSEX, (WPARAM)lv,
449 (LPARAM)cert_mgr_sort_by_subject);
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 };
461 struct CertMgrStoreInfo
463 LPCWSTR name;
464 int removeWarning;
465 int removePluralWarning;
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 },
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 },
486 struct CertMgrData
488 HIMAGELIST imageList;
489 LPCWSTR title;
490 DWORD nStores;
491 const struct CertMgrStoreInfo *stores;
494 static void show_cert_stores(HWND hwnd, DWORD dwFlags, struct CertMgrData *data)
496 const struct CertMgrStoreInfo *storeList;
497 int cStores, i;
498 HWND tab = GetDlgItem(hwnd, IDC_MGR_STORES);
500 if (dwFlags & CRYPTUI_CERT_MGR_PUBLISHER_TAB)
502 storeList = publisherStoreList;
503 cStores = ARRAY_SIZE(publisherStoreList);
505 else
507 storeList = defaultStoreList;
508 cStores = ARRAY_SIZE(defaultStoreList);
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++)
516 LPCWSTR name;
517 TCITEMW item;
518 HCERTSTORE store;
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);
531 static void free_certs(HWND lv)
533 LVITEMW item;
534 int items = SendMessageW(lv, LVM_GETITEMCOUNT, 0, 0), i;
536 for (i = 0; i < items; i++)
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);
546 static HCERTSTORE cert_mgr_index_to_store(HWND tab, int index)
548 TCITEMW item;
550 item.mask = TCIF_PARAM;
551 SendMessageW(tab, TCM_GETITEMW, index, (LPARAM)&item);
552 return (HCERTSTORE)item.lParam;
555 static HCERTSTORE cert_mgr_current_store(HWND hwnd)
557 HWND tab = GetDlgItem(hwnd, IDC_MGR_STORES);
559 return cert_mgr_index_to_store(tab, SendMessageW(tab, TCM_GETCURSEL, 0, 0));
562 static void close_stores(HWND tab)
564 int i, tabs = SendMessageW(tab, TCM_GETITEMCOUNT, 0, 0);
566 for (i = 0; i < tabs; i++)
567 CertCloseStore(cert_mgr_index_to_store(tab, i), 0);
570 static void refresh_store_certs(HWND hwnd)
572 HWND lv = GetDlgItem(hwnd, IDC_MGR_CERTS);
574 free_certs(lv);
575 SendMessageW(lv, LVM_DELETEALLITEMS, 0, 0);
576 show_store_certs(hwnd, cert_mgr_current_store(hwnd));
579 typedef enum {
580 CheckBitmapIndexUnchecked = 1,
581 CheckBitmapIndexChecked = 2,
582 CheckBitmapIndexDisabledUnchecked = 3,
583 CheckBitmapIndexDisabledChecked = 4
584 } CheckBitmapIndex;
586 static void add_known_usage(HWND lv, PCCRYPT_OID_INFO info,
587 CheckBitmapIndex state)
589 LVITEMW item;
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);
601 static void add_known_usages_to_list(HWND lv, CheckBitmapIndex state)
603 PCCRYPT_OID_INFO *usages;
605 if (WTHelperGetKnownUsages(1, &usages))
607 PCCRYPT_OID_INFO *ptr;
609 for (ptr = usages; *ptr; ptr++)
610 add_known_usage(lv, *ptr, state);
611 WTHelperGetKnownUsages(2, &usages);
615 static void toggle_usage(HWND hwnd, int iItem)
617 LVITEMW item;
618 int res;
619 HWND lv = GetDlgItem(hwnd, IDC_CERTIFICATE_USAGES);
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)
628 int state = item.state >> 12;
630 item.state = INDEXTOSTATEIMAGEMASK(
631 state == CheckBitmapIndexChecked ? CheckBitmapIndexUnchecked :
632 CheckBitmapIndexChecked);
633 SendMessageW(lv, LVM_SETITEMSTATE, iItem, (LPARAM)&item);
637 static LONG_PTR find_oid_in_list(HWND lv, LPCSTR oid)
639 PCCRYPT_OID_INFO oidInfo = CryptFindOIDInfo(CRYPT_OID_INFO_OID_KEY,
640 (void *)oid, CRYPT_ENHKEY_USAGE_OID_GROUP_ID);
641 LONG_PTR ret;
643 if (oidInfo)
645 LVFINDINFOW findInfo;
647 findInfo.flags = LVFI_PARAM;
648 findInfo.lParam = (LPARAM)oidInfo;
649 ret = SendMessageW(lv, LVM_FINDITEMW, -1, (LPARAM)&findInfo);
651 else
653 LVFINDINFOA findInfo;
655 findInfo.flags = LVFI_STRING;
656 findInfo.psz = oid;
657 ret = SendMessageW(lv, LVM_FINDITEMA, -1, (LPARAM)&findInfo);
659 return ret;
662 static void save_cert_mgr_usages(HWND hwnd)
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;
674 item.mask = LVIF_STATE | LVIF_PARAM;
675 item.iSubItem = 0;
676 item.stateMask = LVIS_STATEIMAGEMASK;
677 for (i = 0; i < purposes; i++)
679 item.iItem = i;
680 if (SendMessageW(lv, LVM_GETITEMW, 0, (LPARAM)&item))
682 int state = item.state >> 12;
684 if (state == CheckBitmapIndexUnchecked)
686 CRYPT_OID_INFO *info = (CRYPT_OID_INFO *)item.lParam;
687 BOOL firstString = TRUE;
689 if (!str)
690 str = HeapAlloc(GetProcessHeap(), 0,
691 strlen(info->pszOID) + 1);
692 else
694 str = HeapReAlloc(GetProcessHeap(), 0, str,
695 strlen(str) + 1 + strlen(info->pszOID) + 1);
696 firstString = FALSE;
698 if (str)
700 LPSTR ptr = firstString ? str : str + strlen(str);
702 if (!firstString)
703 *ptr++ = ',';
704 strcpy(ptr, info->pszOID);
709 if (!RegCreateKeyExW(HKEY_CURRENT_USER, keyName, 0, NULL, 0, KEY_ALL_ACCESS,
710 NULL, &key, NULL))
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);
719 HeapFree(GetProcessHeap(), 0, str);
722 static LRESULT CALLBACK cert_mgr_advanced_dlg_proc(HWND hwnd, UINT msg,
723 WPARAM wp, LPARAM lp)
725 switch (msg)
727 case WM_INITDIALOG:
729 RECT rc;
730 LVCOLUMNW column;
731 HWND lv = GetDlgItem(hwnd, IDC_CERTIFICATE_USAGES);
732 HIMAGELIST imageList;
733 LPSTR disabledUsages;
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)
742 HBITMAP bmp;
743 COLORREF backColor = RGB(255, 0, 255);
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);
752 add_known_usages_to_list(lv, CheckBitmapIndexChecked);
753 if ((disabledUsages = get_cert_mgr_usages()))
755 LPSTR ptr, comma;
757 for (ptr = disabledUsages, comma = strchr(ptr, ','); ptr && *ptr;
758 ptr = comma ? comma + 1 : NULL,
759 comma = ptr ? strchr(ptr, ',') : NULL)
761 LONG_PTR index;
763 if (comma)
764 *comma = 0;
765 if ((index = find_oid_in_list(lv, ptr)) != -1)
766 toggle_usage(hwnd, index);
768 HeapFree(GetProcessHeap(), 0, disabledUsages);
770 break;
772 case WM_NOTIFY:
774 NMHDR *hdr = (NMHDR *)lp;
775 NMITEMACTIVATE *nm;
777 switch (hdr->code)
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;
785 break;
787 case WM_COMMAND:
788 switch (wp)
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;
800 break;
802 return 0;
805 static void cert_mgr_clear_cert_selection(HWND hwnd)
807 EnableWindow(GetDlgItem(hwnd, IDC_MGR_EXPORT), FALSE);
808 EnableWindow(GetDlgItem(hwnd, IDC_MGR_REMOVE), FALSE);
809 EnableWindow(GetDlgItem(hwnd, IDC_MGR_VIEW), FALSE);
810 SendMessageW(GetDlgItem(hwnd, IDC_MGR_PURPOSES), WM_SETTEXT, 0,
811 (LPARAM)empty);
812 refresh_store_certs(hwnd);
815 static PCCERT_CONTEXT cert_mgr_index_to_cert(HWND hwnd, int index)
817 PCCERT_CONTEXT cert = NULL;
818 LVITEMW item;
820 item.mask = LVIF_PARAM;
821 item.iItem = index;
822 item.iSubItem = 0;
823 if (SendMessageW(GetDlgItem(hwnd, IDC_MGR_CERTS), LVM_GETITEMW, 0,
824 (LPARAM)&item))
825 cert = (PCCERT_CONTEXT)item.lParam;
826 return cert;
829 static void show_selected_cert(HWND hwnd, int index)
831 PCCERT_CONTEXT cert = cert_mgr_index_to_cert(hwnd, index);
833 if (cert)
835 CRYPTUI_VIEWCERTIFICATE_STRUCTW viewInfo;
837 memset(&viewInfo, 0, sizeof(viewInfo));
838 viewInfo.dwSize = sizeof(viewInfo);
839 viewInfo.hwndParent = hwnd;
840 viewInfo.pCertContext = cert;
841 /* FIXME: this should be modal */
842 CryptUIDlgViewCertificateW(&viewInfo, NULL);
846 static void get_cert_usages(PCCERT_CONTEXT cert, LPWSTR *str)
848 PCERT_ENHKEY_USAGE usage;
849 DWORD size;
851 /* Get enhanced key usage. Have to check for a property and an extension
852 * separately, because CertGetEnhancedKeyUsage will succeed and return an
853 * empty usage if neither is set. Unfortunately an empty usage implies
854 * no usage is allowed, so we have to distinguish between the two cases.
856 if (CertGetEnhancedKeyUsage(cert, CERT_FIND_PROP_ONLY_ENHKEY_USAGE_FLAG,
857 NULL, &size))
859 usage = HeapAlloc(GetProcessHeap(), 0, size);
860 if (!CertGetEnhancedKeyUsage(cert,
861 CERT_FIND_PROP_ONLY_ENHKEY_USAGE_FLAG, usage, &size))
863 HeapFree(GetProcessHeap(), 0, usage);
864 usage = NULL;
867 else if (CertGetEnhancedKeyUsage(cert, CERT_FIND_EXT_ONLY_ENHKEY_USAGE_FLAG,
868 NULL, &size))
870 usage = HeapAlloc(GetProcessHeap(), 0, size);
871 if (!CertGetEnhancedKeyUsage(cert,
872 CERT_FIND_EXT_ONLY_ENHKEY_USAGE_FLAG, usage, &size))
874 HeapFree(GetProcessHeap(), 0, usage);
875 usage = NULL;
878 else
879 usage = NULL;
880 if (usage)
882 if (usage->cUsageIdentifier)
884 static const WCHAR commaSpace[] = { ',',' ',0 };
885 DWORD i, len = 1;
886 LPWSTR ptr;
888 for (i = 0; i < usage->cUsageIdentifier; i++)
890 PCCRYPT_OID_INFO info =
891 CryptFindOIDInfo(CRYPT_OID_INFO_OID_KEY,
892 usage->rgpszUsageIdentifier[i],
893 CRYPT_ENHKEY_USAGE_OID_GROUP_ID);
895 if (info)
896 len += lstrlenW(info->pwszName);
897 else
898 len += strlen(usage->rgpszUsageIdentifier[i]);
899 if (i < usage->cUsageIdentifier - 1)
900 len += lstrlenW(commaSpace);
902 *str = HeapAlloc(GetProcessHeap(), 0, len * sizeof(WCHAR));
903 if (*str)
905 for (i = 0, ptr = *str; i < usage->cUsageIdentifier; i++)
907 PCCRYPT_OID_INFO info =
908 CryptFindOIDInfo(CRYPT_OID_INFO_OID_KEY,
909 usage->rgpszUsageIdentifier[i],
910 CRYPT_ENHKEY_USAGE_OID_GROUP_ID);
912 if (info)
914 lstrcpyW(ptr, info->pwszName);
915 ptr += lstrlenW(info->pwszName);
917 else
919 LPCSTR src = usage->rgpszUsageIdentifier[i];
921 for (; *src; ptr++, src++)
922 *ptr = *src;
923 *ptr = 0;
925 if (i < usage->cUsageIdentifier - 1)
927 lstrcpyW(ptr, commaSpace);
928 ptr += lstrlenW(commaSpace);
931 *ptr = 0;
933 HeapFree(GetProcessHeap(), 0, usage);
935 else
937 size = MAX_STRING_LEN * sizeof(WCHAR);
938 *str = HeapAlloc(GetProcessHeap(), 0, size);
939 if (*str)
940 LoadStringW(hInstance, IDS_ALLOWED_PURPOSE_NONE, *str, size);
943 else
945 size = MAX_STRING_LEN * sizeof(WCHAR);
946 *str = HeapAlloc(GetProcessHeap(), 0, size);
947 if (*str)
948 LoadStringW(hInstance, IDS_ALLOWED_PURPOSE_ALL, *str, size);
952 static void cert_mgr_show_cert_usages(HWND hwnd, int index)
954 HWND text = GetDlgItem(hwnd, IDC_MGR_PURPOSES);
955 PCCERT_CONTEXT cert = cert_mgr_index_to_cert(hwnd, index);
956 LPWSTR str = NULL;
958 get_cert_usages(cert, &str);
959 if (str)
961 SendMessageW(text, WM_SETTEXT, 0, (LPARAM)str);
962 HeapFree(GetProcessHeap(), 0, str);
966 static void cert_mgr_do_remove(HWND hwnd)
968 int tabIndex = SendMessageW(GetDlgItem(hwnd, IDC_MGR_STORES),
969 TCM_GETCURSEL, 0, 0);
970 struct CertMgrData *data =
971 (struct CertMgrData *)GetWindowLongPtrW(hwnd, DWLP_USER);
973 if (tabIndex < data->nStores)
975 HWND lv = GetDlgItem(hwnd, IDC_MGR_CERTS);
976 WCHAR warning[MAX_STRING_LEN], title[MAX_STRING_LEN];
977 LPCWSTR pTitle;
978 int warningID;
980 if (SendMessageW(lv, LVM_GETSELECTEDCOUNT, 0, 0) > 1)
981 warningID = data->stores[tabIndex].removePluralWarning;
982 else
983 warningID = data->stores[tabIndex].removeWarning;
984 if (data->title)
985 pTitle = data->title;
986 else
988 LoadStringW(hInstance, IDS_CERT_MGR, title, ARRAY_SIZE(title));
989 pTitle = title;
991 LoadStringW(hInstance, warningID, warning, ARRAY_SIZE(warning));
992 if (MessageBoxW(hwnd, warning, pTitle, MB_YESNO) == IDYES)
994 int selection = -1;
996 do {
997 selection = SendMessageW(lv, LVM_GETNEXTITEM, selection,
998 LVNI_SELECTED);
999 if (selection >= 0)
1001 PCCERT_CONTEXT cert = cert_mgr_index_to_cert(hwnd,
1002 selection);
1004 CertDeleteCertificateFromStore(cert);
1006 } while (selection >= 0);
1007 cert_mgr_clear_cert_selection(hwnd);
1012 static void cert_mgr_do_export(HWND hwnd)
1014 HWND lv = GetDlgItem(hwnd, IDC_MGR_CERTS);
1015 int selectionCount = SendMessageW(lv, LVM_GETSELECTEDCOUNT, 0, 0);
1017 if (selectionCount == 1)
1019 int selection = SendMessageW(lv, LVM_GETNEXTITEM, -1,
1020 LVNI_SELECTED);
1022 if (selection >= 0)
1024 PCCERT_CONTEXT cert = cert_mgr_index_to_cert(hwnd, selection);
1026 if (cert)
1028 CRYPTUI_WIZ_EXPORT_INFO info;
1030 info.dwSize = sizeof(info);
1031 info.pwszExportFileName = NULL;
1032 info.dwSubjectChoice = CRYPTUI_WIZ_EXPORT_CERT_CONTEXT;
1033 info.u.pCertContext = cert;
1034 info.cStores = 0;
1035 CryptUIWizExport(0, hwnd, NULL, &info, NULL);
1039 else if (selectionCount > 1)
1041 HCERTSTORE store = CertOpenStore(CERT_STORE_PROV_MEMORY, 0, 0,
1042 CERT_STORE_CREATE_NEW_FLAG, NULL);
1044 if (store)
1046 CRYPTUI_WIZ_EXPORT_INFO info;
1047 int selection = -1;
1049 info.dwSize = sizeof(info);
1050 info.pwszExportFileName = NULL;
1051 info.dwSubjectChoice =
1052 CRYPTUI_WIZ_EXPORT_CERT_STORE_CERTIFICATES_ONLY;
1053 info.u.hCertStore = store;
1054 info.cStores = 0;
1055 do {
1056 selection = SendMessageW(lv, LVM_GETNEXTITEM, selection,
1057 LVNI_SELECTED);
1058 if (selection >= 0)
1060 PCCERT_CONTEXT cert = cert_mgr_index_to_cert(hwnd,
1061 selection);
1063 CertAddCertificateContextToStore(store, cert,
1064 CERT_STORE_ADD_ALWAYS, NULL);
1066 } while (selection >= 0);
1067 CryptUIWizExport(0, hwnd, NULL, &info, NULL);
1068 CertCloseStore(store, 0);
1073 static int cert_mgr_sort_by_text(HWND lv, int col, int index1, int index2)
1075 LVITEMW item;
1076 WCHAR buf1[MAX_STRING_LEN];
1077 WCHAR buf2[MAX_STRING_LEN];
1079 item.cchTextMax = ARRAY_SIZE(buf1);
1080 item.mask = LVIF_TEXT;
1081 item.pszText = buf1;
1082 item.iItem = index1;
1083 item.iSubItem = col;
1084 SendMessageW(lv, LVM_GETITEMW, 0, (LPARAM)&item);
1085 item.pszText = buf2;
1086 item.iItem = index2;
1087 SendMessageW(lv, LVM_GETITEMW, 0, (LPARAM)&item);
1088 return lstrcmpW(buf1, buf2);
1091 static int CALLBACK cert_mgr_sort_by_subject(LPARAM lp1, LPARAM lp2, LPARAM lp)
1093 return cert_mgr_sort_by_text((HWND)lp, 0, lp1, lp2);
1096 static int CALLBACK cert_mgr_sort_by_issuer(LPARAM lp1, LPARAM lp2, LPARAM lp)
1098 return cert_mgr_sort_by_text((HWND)lp, 1, lp1, lp2);
1101 static int CALLBACK cert_mgr_sort_by_date(LPARAM lp1, LPARAM lp2, LPARAM lp)
1103 PCCERT_CONTEXT cert1 = (PCCERT_CONTEXT)lp1;
1104 PCCERT_CONTEXT cert2 = (PCCERT_CONTEXT)lp2;
1105 return CompareFileTime(&cert1->pCertInfo->NotAfter,
1106 &cert2->pCertInfo->NotAfter);
1109 static int CALLBACK cert_mgr_sort_by_friendly_name(LPARAM lp1, LPARAM lp2,
1110 LPARAM lp)
1112 return cert_mgr_sort_by_text((HWND)lp, 3, lp1, lp2);
1115 static LRESULT CALLBACK cert_mgr_dlg_proc(HWND hwnd, UINT msg, WPARAM wp,
1116 LPARAM lp)
1118 struct CertMgrData *data;
1120 switch (msg)
1122 case WM_INITDIALOG:
1124 PCCRYPTUI_CERT_MGR_STRUCT pCryptUICertMgr =
1125 (PCCRYPTUI_CERT_MGR_STRUCT)lp;
1126 HWND tab = GetDlgItem(hwnd, IDC_MGR_STORES);
1128 data = HeapAlloc(GetProcessHeap(), 0, sizeof(struct CertMgrData));
1129 if (!data)
1130 return 0;
1131 data->imageList = ImageList_Create(16, 16, ILC_COLOR4 | ILC_MASK, 2, 0);
1132 if (data->imageList)
1134 HBITMAP bmp;
1135 COLORREF backColor = RGB(255, 0, 255);
1137 bmp = LoadBitmapW(hInstance, MAKEINTRESOURCEW(IDB_SMALL_ICONS));
1138 ImageList_AddMasked(data->imageList, bmp, backColor);
1139 DeleteObject(bmp);
1140 ImageList_SetBkColor(data->imageList, CLR_NONE);
1141 SendMessageW(GetDlgItem(hwnd, IDC_MGR_CERTS), LVM_SETIMAGELIST,
1142 LVSIL_SMALL, (LPARAM)data->imageList);
1144 SetWindowLongPtrW(hwnd, DWLP_USER, (LPARAM)data);
1145 data->title = pCryptUICertMgr->pwszTitle;
1147 initialize_purpose_selection(hwnd);
1148 add_cert_columns(hwnd);
1149 if (pCryptUICertMgr->pwszTitle)
1150 SendMessageW(hwnd, WM_SETTEXT, 0,
1151 (LPARAM)pCryptUICertMgr->pwszTitle);
1152 show_cert_stores(hwnd, pCryptUICertMgr->dwFlags, data);
1153 show_store_certs(hwnd, cert_mgr_index_to_store(tab, 0));
1154 break;
1156 case WM_NOTIFY:
1158 NMHDR *hdr = (NMHDR *)lp;
1160 switch (hdr->code)
1162 case TCN_SELCHANGE:
1163 cert_mgr_clear_cert_selection(hwnd);
1164 break;
1165 case LVN_ITEMCHANGED:
1167 NMITEMACTIVATE *nm = (NMITEMACTIVATE*)lp;
1168 HWND lv = GetDlgItem(hwnd, IDC_MGR_CERTS);
1169 int numSelected = SendMessageW(lv, LVM_GETSELECTEDCOUNT, 0, 0);
1171 EnableWindow(GetDlgItem(hwnd, IDC_MGR_EXPORT), numSelected > 0);
1172 EnableWindow(GetDlgItem(hwnd, IDC_MGR_REMOVE), numSelected > 0);
1173 EnableWindow(GetDlgItem(hwnd, IDC_MGR_VIEW), numSelected == 1);
1174 if (numSelected == 1)
1175 cert_mgr_show_cert_usages(hwnd, nm->iItem);
1176 else
1177 SendMessageW(GetDlgItem(hwnd, IDC_MGR_PURPOSES), WM_SETTEXT, 0,
1178 (LPARAM)empty);
1179 break;
1181 case NM_DBLCLK:
1182 show_selected_cert(hwnd, ((NMITEMACTIVATE *)lp)->iItem);
1183 break;
1184 case LVN_KEYDOWN:
1186 NMLVKEYDOWN *lvk = (NMLVKEYDOWN *)lp;
1188 if (lvk->wVKey == VK_DELETE)
1189 cert_mgr_do_remove(hwnd);
1190 break;
1192 case LVN_COLUMNCLICK:
1194 NMLISTVIEW *nmlv = (NMLISTVIEW *)lp;
1195 HWND lv = GetDlgItem(hwnd, IDC_MGR_CERTS);
1197 /* FIXME: doesn't support swapping sort order between ascending
1198 * and descending.
1200 switch (nmlv->iSubItem)
1202 case 0:
1203 SendMessageW(lv, LVM_SORTITEMSEX, (WPARAM)lv,
1204 (LPARAM)cert_mgr_sort_by_subject);
1205 break;
1206 case 1:
1207 SendMessageW(lv, LVM_SORTITEMSEX, (WPARAM)lv,
1208 (LPARAM)cert_mgr_sort_by_issuer);
1209 break;
1210 case 2:
1211 SendMessageW(lv, LVM_SORTITEMS, 0,
1212 (LPARAM)cert_mgr_sort_by_date);
1213 break;
1214 case 3:
1215 SendMessageW(lv, LVM_SORTITEMSEX, (WPARAM)lv,
1216 (LPARAM)cert_mgr_sort_by_friendly_name);
1217 break;
1219 break;
1222 break;
1224 case WM_COMMAND:
1225 switch (wp)
1227 case ((CBN_SELCHANGE << 16) | IDC_MGR_PURPOSE_SELECTION):
1228 cert_mgr_clear_cert_selection(hwnd);
1229 break;
1230 case IDC_MGR_IMPORT:
1231 if (CryptUIWizImport(0, hwnd, NULL, NULL,
1232 cert_mgr_current_store(hwnd)))
1233 refresh_store_certs(hwnd);
1234 break;
1235 case IDC_MGR_ADVANCED:
1236 if (DialogBoxW(hInstance, MAKEINTRESOURCEW(IDD_CERT_MGR_ADVANCED),
1237 hwnd, cert_mgr_advanced_dlg_proc) == IDOK)
1239 HWND cb = GetDlgItem(hwnd, IDC_MGR_PURPOSE_SELECTION);
1240 int index, len;
1241 LPWSTR curString = NULL;
1243 index = SendMessageW(cb, CB_GETCURSEL, 0, 0);
1244 if (index >= 0)
1246 len = SendMessageW(cb, CB_GETLBTEXTLEN, index, 0);
1247 curString = HeapAlloc(GetProcessHeap(), 0,
1248 (len + 1) * sizeof(WCHAR));
1249 SendMessageW(cb, CB_GETLBTEXT, index, (LPARAM)curString);
1251 SendMessageW(cb, CB_RESETCONTENT, 0, 0);
1252 initialize_purpose_selection(hwnd);
1253 if (curString)
1255 index = SendMessageW(cb, CB_FINDSTRINGEXACT, -1,
1256 (LPARAM)curString);
1257 if (index >= 0)
1258 SendMessageW(cb, CB_SETCURSEL, index, 0);
1259 HeapFree(GetProcessHeap(), 0, curString);
1261 refresh_store_certs(hwnd);
1263 break;
1264 case IDC_MGR_VIEW:
1266 HWND lv = GetDlgItem(hwnd, IDC_MGR_CERTS);
1267 int selection = SendMessageW(lv, LVM_GETNEXTITEM, -1,
1268 LVNI_SELECTED);
1270 if (selection >= 0)
1271 show_selected_cert(hwnd, selection);
1272 break;
1274 case IDC_MGR_EXPORT:
1275 cert_mgr_do_export(hwnd);
1276 break;
1277 case IDC_MGR_REMOVE:
1278 cert_mgr_do_remove(hwnd);
1279 break;
1280 case IDCANCEL:
1281 free_certs(GetDlgItem(hwnd, IDC_MGR_CERTS));
1282 close_stores(GetDlgItem(hwnd, IDC_MGR_STORES));
1283 data = (struct CertMgrData *)GetWindowLongPtrW(hwnd, DWLP_USER);
1284 ImageList_Destroy(data->imageList);
1285 HeapFree(GetProcessHeap(), 0, data);
1286 EndDialog(hwnd, IDCANCEL);
1287 break;
1289 break;
1291 return 0;
1294 /***********************************************************************
1295 * CryptUIDlgCertMgr (CRYPTUI.@)
1297 BOOL WINAPI CryptUIDlgCertMgr(PCCRYPTUI_CERT_MGR_STRUCT pCryptUICertMgr)
1299 TRACE("(%p)\n", pCryptUICertMgr);
1301 if (pCryptUICertMgr->dwSize != sizeof(CRYPTUI_CERT_MGR_STRUCT))
1303 WARN("unexpected size %d\n", pCryptUICertMgr->dwSize);
1304 SetLastError(E_INVALIDARG);
1305 return FALSE;
1307 DialogBoxParamW(hInstance, MAKEINTRESOURCEW(IDD_CERT_MGR),
1308 pCryptUICertMgr->hwndParent, cert_mgr_dlg_proc, (LPARAM)pCryptUICertMgr);
1309 return TRUE;
1312 /* FIXME: real names are unknown, functions are undocumented */
1313 typedef struct _CRYPTUI_ENUM_SYSTEM_STORE_ARGS
1315 DWORD dwFlags;
1316 void *pvSystemStoreLocationPara;
1317 } CRYPTUI_ENUM_SYSTEM_STORE_ARGS, *PCRYPTUI_ENUM_SYSTEM_STORE_ARGS;
1319 typedef struct _CRYPTUI_ENUM_DATA
1321 DWORD cStores;
1322 HCERTSTORE *rghStore;
1323 DWORD cEnumArgs;
1324 PCRYPTUI_ENUM_SYSTEM_STORE_ARGS rgEnumArgs;
1325 } CRYPTUI_ENUM_DATA, *PCRYPTUI_ENUM_DATA;
1327 typedef BOOL (WINAPI *PFN_SELECTED_STORE_CB)(HCERTSTORE store, HWND hwnd,
1328 void *pvArg);
1330 /* Values for dwFlags */
1331 #define CRYPTUI_ENABLE_SHOW_PHYSICAL_STORE 0x00000001
1333 typedef struct _CRYPTUI_SELECTSTORE_INFO_A
1335 DWORD dwSize;
1336 HWND parent;
1337 DWORD dwFlags;
1338 LPSTR pszTitle;
1339 LPSTR pszText;
1340 CRYPTUI_ENUM_DATA *pEnumData;
1341 PFN_SELECTED_STORE_CB pfnSelectedStoreCallback;
1342 void *pvArg;
1343 } CRYPTUI_SELECTSTORE_INFO_A, *PCRYPTUI_SELECTSTORE_INFO_A;
1345 typedef struct _CRYPTUI_SELECTSTORE_INFO_W
1347 DWORD dwSize;
1348 HWND parent;
1349 DWORD dwFlags;
1350 LPWSTR pwszTitle;
1351 LPWSTR pwszText;
1352 CRYPTUI_ENUM_DATA *pEnumData;
1353 PFN_SELECTED_STORE_CB pfnSelectedStoreCallback;
1354 void *pvArg;
1355 } CRYPTUI_SELECTSTORE_INFO_W, *PCRYPTUI_SELECTSTORE_INFO_W;
1357 struct StoreInfo
1359 enum {
1360 StoreHandle,
1361 SystemStore
1362 } type;
1363 union {
1364 HCERTSTORE store;
1365 LPWSTR name;
1366 } DUMMYUNIONNAME;
1369 static BOOL WINAPI enum_store_callback(const void *pvSystemStore,
1370 DWORD dwFlags, PCERT_SYSTEM_STORE_INFO pStoreInfo, void *pvReserved,
1371 void *pvArg)
1373 HWND tree = GetDlgItem(pvArg, IDC_STORE_LIST);
1374 TVINSERTSTRUCTW tvis;
1375 LPCWSTR localizedName;
1376 BOOL ret = TRUE;
1378 tvis.hParent = NULL;
1379 tvis.hInsertAfter = TVI_LAST;
1380 tvis.u.item.mask = TVIF_TEXT;
1381 if ((localizedName = CryptFindLocalizedName(pvSystemStore)))
1383 struct StoreInfo *storeInfo = HeapAlloc(GetProcessHeap(), 0,
1384 sizeof(struct StoreInfo));
1386 if (storeInfo)
1388 storeInfo->type = SystemStore;
1389 storeInfo->u.name = HeapAlloc(GetProcessHeap(), 0,
1390 (lstrlenW(pvSystemStore) + 1) * sizeof(WCHAR));
1391 if (storeInfo->u.name)
1393 tvis.u.item.mask |= TVIF_PARAM;
1394 tvis.u.item.lParam = (LPARAM)storeInfo;
1395 lstrcpyW(storeInfo->u.name, pvSystemStore);
1397 else
1399 HeapFree(GetProcessHeap(), 0, storeInfo);
1400 ret = FALSE;
1403 else
1404 ret = FALSE;
1405 tvis.u.item.pszText = (LPWSTR)localizedName;
1407 else
1408 tvis.u.item.pszText = (LPWSTR)pvSystemStore;
1409 /* FIXME: need a folder icon for the store too */
1410 if (ret)
1411 SendMessageW(tree, TVM_INSERTITEMW, 0, (LPARAM)&tvis);
1412 return ret;
1415 static void enumerate_stores(HWND hwnd, CRYPTUI_ENUM_DATA *pEnumData)
1417 DWORD i;
1418 HWND tree = GetDlgItem(hwnd, IDC_STORE_LIST);
1420 for (i = 0; i < pEnumData->cEnumArgs; i++)
1421 CertEnumSystemStore(pEnumData->rgEnumArgs[i].dwFlags,
1422 pEnumData->rgEnumArgs[i].pvSystemStoreLocationPara,
1423 hwnd, enum_store_callback);
1424 for (i = 0; i < pEnumData->cStores; i++)
1426 DWORD size;
1428 if (CertGetStoreProperty(pEnumData->rghStore[i],
1429 CERT_STORE_LOCALIZED_NAME_PROP_ID, NULL, &size))
1431 LPWSTR name = HeapAlloc(GetProcessHeap(), 0, size);
1433 if (name)
1435 if (CertGetStoreProperty(pEnumData->rghStore[i],
1436 CERT_STORE_LOCALIZED_NAME_PROP_ID, name, &size))
1438 struct StoreInfo *storeInfo = HeapAlloc(GetProcessHeap(),
1439 0, sizeof(struct StoreInfo));
1441 if (storeInfo)
1443 TVINSERTSTRUCTW tvis;
1445 storeInfo->type = StoreHandle;
1446 storeInfo->u.store = pEnumData->rghStore[i];
1447 tvis.hParent = NULL;
1448 tvis.hInsertAfter = TVI_LAST;
1449 tvis.u.item.mask = TVIF_TEXT | TVIF_PARAM;
1450 tvis.u.item.pszText = name;
1451 tvis.u.item.lParam = (LPARAM)storeInfo;
1452 SendMessageW(tree, TVM_INSERTITEMW, 0, (LPARAM)&tvis);
1455 HeapFree(GetProcessHeap(), 0, name);
1461 static void free_store_info(HWND tree)
1463 HTREEITEM next = (HTREEITEM)SendMessageW(tree, TVM_GETNEXTITEM, TVGN_CHILD,
1466 while (next)
1468 TVITEMW item;
1470 memset(&item, 0, sizeof(item));
1471 item.mask = TVIF_HANDLE | TVIF_PARAM;
1472 item.hItem = next;
1473 SendMessageW(tree, TVM_GETITEMW, 0, (LPARAM)&item);
1474 if (item.lParam)
1476 struct StoreInfo *storeInfo = (struct StoreInfo *)item.lParam;
1478 if (storeInfo->type == SystemStore)
1479 HeapFree(GetProcessHeap(), 0, storeInfo->u.name);
1480 HeapFree(GetProcessHeap(), 0, storeInfo);
1482 next = (HTREEITEM)SendMessageW(tree, TVM_GETNEXTITEM, TVGN_NEXT,
1483 (LPARAM)next);
1487 static HCERTSTORE selected_item_to_store(HWND tree, HTREEITEM hItem)
1489 WCHAR buf[MAX_STRING_LEN];
1490 TVITEMW item;
1491 HCERTSTORE store;
1493 memset(&item, 0, sizeof(item));
1494 item.mask = TVIF_HANDLE | TVIF_PARAM | TVIF_TEXT;
1495 item.hItem = hItem;
1496 item.cchTextMax = ARRAY_SIZE(buf);
1497 item.pszText = buf;
1498 SendMessageW(tree, TVM_GETITEMW, 0, (LPARAM)&item);
1499 if (item.lParam)
1501 struct StoreInfo *storeInfo = (struct StoreInfo *)item.lParam;
1503 if (storeInfo->type == StoreHandle)
1504 store = storeInfo->u.store;
1505 else
1506 store = CertOpenSystemStoreW(0, storeInfo->u.name);
1508 else
1510 /* It's implicitly a system store */
1511 store = CertOpenSystemStoreW(0, buf);
1513 return store;
1516 struct SelectStoreInfo
1518 PCRYPTUI_SELECTSTORE_INFO_W info;
1519 HCERTSTORE store;
1522 static LRESULT CALLBACK select_store_dlg_proc(HWND hwnd, UINT msg, WPARAM wp,
1523 LPARAM lp)
1525 struct SelectStoreInfo *selectInfo;
1526 LRESULT ret = 0;
1528 switch (msg)
1530 case WM_INITDIALOG:
1532 selectInfo = (struct SelectStoreInfo *)lp;
1533 SetWindowLongPtrW(hwnd, DWLP_USER, lp);
1534 if (selectInfo->info->pwszTitle)
1535 SendMessageW(hwnd, WM_SETTEXT, 0,
1536 (LPARAM)selectInfo->info->pwszTitle);
1537 if (selectInfo->info->pwszText)
1538 SendMessageW(GetDlgItem(hwnd, IDC_STORE_TEXT), WM_SETTEXT, 0,
1539 (LPARAM)selectInfo->info->pwszText);
1540 if (!(selectInfo->info->dwFlags & CRYPTUI_ENABLE_SHOW_PHYSICAL_STORE))
1541 ShowWindow(GetDlgItem(hwnd, IDC_SHOW_PHYSICAL_STORES), FALSE);
1542 enumerate_stores(hwnd, selectInfo->info->pEnumData);
1543 break;
1545 case WM_COMMAND:
1546 switch (wp)
1548 case IDOK:
1550 HWND tree = GetDlgItem(hwnd, IDC_STORE_LIST);
1551 HTREEITEM selection = (HTREEITEM)SendMessageW(tree,
1552 TVM_GETNEXTITEM, TVGN_CARET, 0);
1554 selectInfo = (struct SelectStoreInfo *)GetWindowLongPtrW(hwnd,
1555 DWLP_USER);
1556 if (!selection)
1558 WCHAR title[MAX_STRING_LEN], error[MAX_STRING_LEN], *pTitle;
1560 if (selectInfo->info->pwszTitle)
1561 pTitle = selectInfo->info->pwszTitle;
1562 else
1564 LoadStringW(hInstance, IDS_SELECT_STORE_TITLE, title, ARRAY_SIZE(title));
1565 pTitle = title;
1567 LoadStringW(hInstance, IDS_SELECT_STORE, error, ARRAY_SIZE(error));
1568 MessageBoxW(hwnd, error, pTitle, MB_ICONEXCLAMATION | MB_OK);
1570 else
1572 HCERTSTORE store = selected_item_to_store(tree, selection);
1574 if (!selectInfo->info->pfnSelectedStoreCallback ||
1575 selectInfo->info->pfnSelectedStoreCallback(store, hwnd,
1576 selectInfo->info->pvArg))
1578 selectInfo->store = store;
1579 free_store_info(tree);
1580 EndDialog(hwnd, IDOK);
1582 else
1583 CertCloseStore(store, 0);
1585 ret = TRUE;
1586 break;
1588 case IDCANCEL:
1589 free_store_info(GetDlgItem(hwnd, IDC_STORE_LIST));
1590 EndDialog(hwnd, IDCANCEL);
1591 ret = TRUE;
1592 break;
1594 break;
1596 return ret;
1599 /***********************************************************************
1600 * CryptUIDlgSelectStoreW (CRYPTUI.@)
1602 HCERTSTORE WINAPI CryptUIDlgSelectStoreW(PCRYPTUI_SELECTSTORE_INFO_W info)
1604 struct SelectStoreInfo selectInfo = { info, NULL };
1606 TRACE("(%p)\n", info);
1608 if (info->dwSize != sizeof(CRYPTUI_SELECTSTORE_INFO_W))
1610 WARN("unexpected size %d\n", info->dwSize);
1611 SetLastError(E_INVALIDARG);
1612 return NULL;
1614 DialogBoxParamW(hInstance, MAKEINTRESOURCEW(IDD_SELECT_STORE), info->parent,
1615 select_store_dlg_proc, (LPARAM)&selectInfo);
1616 return selectInfo.store;
1619 /***********************************************************************
1620 * CryptUIDlgSelectStoreA (CRYPTUI.@)
1622 HCERTSTORE WINAPI CryptUIDlgSelectStoreA(PCRYPTUI_SELECTSTORE_INFO_A info)
1624 CRYPTUI_SELECTSTORE_INFO_W infoW;
1625 HCERTSTORE ret;
1626 int len;
1628 TRACE("(%p)\n", info);
1630 if (info->dwSize != sizeof(CRYPTUI_SELECTSTORE_INFO_A))
1632 WARN("unexpected size %d\n", info->dwSize);
1633 SetLastError(E_INVALIDARG);
1634 return NULL;
1636 memcpy(&infoW, info, sizeof(*info));
1637 if (info->pszTitle)
1639 len = MultiByteToWideChar(CP_ACP, 0, info->pszTitle, -1, NULL, 0);
1640 infoW.pwszTitle = HeapAlloc(GetProcessHeap(), 0, len * sizeof(WCHAR));
1641 MultiByteToWideChar(CP_ACP, 0, info->pszTitle, -1, infoW.pwszTitle,
1642 len);
1644 if (info->pszText)
1646 len = MultiByteToWideChar(CP_ACP, 0, info->pszText, -1, NULL, 0);
1647 infoW.pwszText = HeapAlloc(GetProcessHeap(), 0, len * sizeof(WCHAR));
1648 MultiByteToWideChar(CP_ACP, 0, info->pszText, -1, infoW.pwszText, len);
1650 ret = CryptUIDlgSelectStoreW(&infoW);
1651 HeapFree(GetProcessHeap(), 0, infoW.pwszText);
1652 HeapFree(GetProcessHeap(), 0, infoW.pwszTitle);
1653 return ret;
1656 /***********************************************************************
1657 * CryptUIDlgViewCertificateA (CRYPTUI.@)
1659 BOOL WINAPI CryptUIDlgViewCertificateA(
1660 PCCRYPTUI_VIEWCERTIFICATE_STRUCTA pCertViewInfo, BOOL *pfPropertiesChanged)
1662 CRYPTUI_VIEWCERTIFICATE_STRUCTW viewInfo;
1663 LPWSTR title = NULL;
1664 BOOL ret;
1666 TRACE("(%p, %p)\n", pCertViewInfo, pfPropertiesChanged);
1668 memcpy(&viewInfo, pCertViewInfo, sizeof(viewInfo));
1669 if (pCertViewInfo->szTitle)
1671 int len = MultiByteToWideChar(CP_ACP, 0, pCertViewInfo->szTitle, -1,
1672 NULL, 0);
1674 title = HeapAlloc(GetProcessHeap(), 0, len * sizeof(WCHAR));
1675 if (title)
1677 MultiByteToWideChar(CP_ACP, 0, pCertViewInfo->szTitle, -1, title,
1678 len);
1679 viewInfo.szTitle = title;
1681 else
1683 ret = FALSE;
1684 goto error;
1687 if (pCertViewInfo->cPropSheetPages)
1689 FIXME("ignoring additional prop sheet pages\n");
1690 viewInfo.cPropSheetPages = 0;
1692 ret = CryptUIDlgViewCertificateW(&viewInfo, pfPropertiesChanged);
1693 HeapFree(GetProcessHeap(), 0, title);
1694 error:
1695 return ret;
1698 struct ReadStringStruct
1700 LPCWSTR buf;
1701 LONG pos;
1702 LONG len;
1705 static DWORD CALLBACK read_text_callback(DWORD_PTR dwCookie, LPBYTE buf,
1706 LONG cb, LONG *pcb)
1708 struct ReadStringStruct *string = (struct ReadStringStruct *)dwCookie;
1709 LONG cch = min(cb / sizeof(WCHAR), string->len - string->pos);
1711 TRACE("(%p, %p, %d, %p)\n", string, buf, cb, pcb);
1713 memmove(buf, string->buf + string->pos, cch * sizeof(WCHAR));
1714 string->pos += cch;
1715 *pcb = cch * sizeof(WCHAR);
1716 return 0;
1719 static void add_unformatted_text_to_control(HWND hwnd, LPCWSTR text, LONG len)
1721 struct ReadStringStruct string;
1722 EDITSTREAM editstream;
1724 TRACE("(%p, %s)\n", hwnd, debugstr_wn(text, len));
1726 string.buf = text;
1727 string.pos = 0;
1728 string.len = len;
1729 editstream.dwCookie = (DWORD_PTR)&string;
1730 editstream.dwError = 0;
1731 editstream.pfnCallback = read_text_callback;
1732 SendMessageW(hwnd, EM_STREAMIN, SF_TEXT | SFF_SELECTION | SF_UNICODE,
1733 (LPARAM)&editstream);
1736 static void add_string_resource_to_control(HWND hwnd, int id)
1738 LPWSTR str;
1739 LONG len;
1741 len = LoadStringW(hInstance, id, (LPWSTR)&str, 0);
1742 add_unformatted_text_to_control(hwnd, str, len);
1745 static void add_text_with_paraformat_to_control(HWND hwnd, LPCWSTR text,
1746 LONG len, const PARAFORMAT2 *fmt)
1748 add_unformatted_text_to_control(hwnd, text, len);
1749 SendMessageW(hwnd, EM_SETPARAFORMAT, 0, (LPARAM)fmt);
1752 static void add_string_resource_with_paraformat_to_control(HWND hwnd, int id,
1753 const PARAFORMAT2 *fmt)
1755 LPWSTR str;
1756 LONG len;
1758 len = LoadStringW(hInstance, id, (LPWSTR)&str, 0);
1759 add_text_with_paraformat_to_control(hwnd, str, len, fmt);
1762 static LPWSTR get_cert_name_string(PCCERT_CONTEXT pCertContext, DWORD dwType,
1763 DWORD dwFlags)
1765 LPWSTR buf = NULL;
1766 DWORD len;
1768 len = CertGetNameStringW(pCertContext, dwType, dwFlags, NULL, NULL, 0);
1769 if (len)
1771 buf = HeapAlloc(GetProcessHeap(), 0, len * sizeof(WCHAR));
1772 if (buf)
1773 CertGetNameStringW(pCertContext, dwType, dwFlags, NULL, buf, len);
1775 return buf;
1778 static void add_cert_string_to_control(HWND hwnd, PCCERT_CONTEXT pCertContext,
1779 DWORD dwType, DWORD dwFlags)
1781 LPWSTR name = get_cert_name_string(pCertContext, dwType, dwFlags);
1783 if (name)
1785 /* Don't include NULL-terminator in output */
1786 DWORD len = lstrlenW(name);
1788 add_unformatted_text_to_control(hwnd, name, len);
1789 HeapFree(GetProcessHeap(), 0, name);
1793 static void add_icon_to_control(HWND hwnd, int id)
1795 HRESULT hr;
1796 IRichEditOle *richEditOle = NULL;
1797 IOleObject *object = NULL;
1798 CLSID clsid;
1799 LPOLECACHE oleCache = NULL;
1800 FORMATETC formatEtc;
1801 DWORD conn;
1802 IDataObject *dataObject = NULL;
1803 HBITMAP bitmap = NULL;
1804 STGMEDIUM stgm;
1805 IOleClientSite *clientSite = NULL;
1806 REOBJECT reObject;
1808 TRACE("(%p, %d)\n", hwnd, id);
1810 SendMessageW(hwnd, EM_GETOLEINTERFACE, 0, (LPARAM)&richEditOle);
1811 if (!richEditOle)
1812 goto end;
1813 hr = OleCreateDefaultHandler(&CLSID_NULL, NULL, &IID_IOleObject,
1814 (void**)&object);
1815 if (FAILED(hr))
1816 goto end;
1817 hr = IOleObject_GetUserClassID(object, &clsid);
1818 if (FAILED(hr))
1819 goto end;
1820 hr = IOleObject_QueryInterface(object, &IID_IOleCache, (void**)&oleCache);
1821 if (FAILED(hr))
1822 goto end;
1823 formatEtc.cfFormat = CF_BITMAP;
1824 formatEtc.ptd = NULL;
1825 formatEtc.dwAspect = DVASPECT_CONTENT;
1826 formatEtc.lindex = -1;
1827 formatEtc.tymed = TYMED_GDI;
1828 hr = IOleCache_Cache(oleCache, &formatEtc, 0, &conn);
1829 if (FAILED(hr))
1830 goto end;
1831 hr = IOleObject_QueryInterface(object, &IID_IDataObject,
1832 (void**)&dataObject);
1833 if (FAILED(hr))
1834 goto end;
1835 hr = IRichEditOle_GetClientSite(richEditOle, &clientSite);
1836 if (FAILED(hr))
1837 goto end;
1838 bitmap = LoadImageW(hInstance, MAKEINTRESOURCEW(id), IMAGE_BITMAP, 0, 0,
1839 LR_DEFAULTSIZE | LR_LOADTRANSPARENT);
1840 if (!bitmap)
1841 goto end;
1842 stgm.tymed = TYMED_GDI;
1843 stgm.u.hBitmap = bitmap;
1844 stgm.pUnkForRelease = NULL;
1845 hr = IDataObject_SetData(dataObject, &formatEtc, &stgm, TRUE);
1846 if (FAILED(hr))
1847 goto end;
1849 reObject.cbStruct = sizeof(reObject);
1850 reObject.cp = REO_CP_SELECTION;
1851 reObject.clsid = clsid;
1852 reObject.poleobj = object;
1853 reObject.pstg = NULL;
1854 reObject.polesite = clientSite;
1855 reObject.sizel.cx = reObject.sizel.cy = 0;
1856 reObject.dvaspect = DVASPECT_CONTENT;
1857 reObject.dwFlags = 0;
1858 reObject.dwUser = 0;
1860 IRichEditOle_InsertObject(richEditOle, &reObject);
1862 end:
1863 if (clientSite)
1864 IOleClientSite_Release(clientSite);
1865 if (dataObject)
1866 IDataObject_Release(dataObject);
1867 if (oleCache)
1868 IOleCache_Release(oleCache);
1869 if (object)
1870 IOleObject_Release(object);
1871 if (richEditOle)
1872 IRichEditOle_Release(richEditOle);
1875 #define MY_INDENT 200
1877 static void add_oid_text_to_control(HWND hwnd, char *oid)
1879 WCHAR nl = '\n';
1880 PCCRYPT_OID_INFO oidInfo = CryptFindOIDInfo(CRYPT_OID_INFO_OID_KEY, oid, 0);
1881 PARAFORMAT2 parFmt;
1883 parFmt.cbSize = sizeof(parFmt);
1884 parFmt.dwMask = PFM_STARTINDENT;
1885 parFmt.dxStartIndent = MY_INDENT * 3;
1886 if (oidInfo)
1888 add_text_with_paraformat_to_control(hwnd, oidInfo->pwszName,
1889 lstrlenW(oidInfo->pwszName), &parFmt);
1890 add_unformatted_text_to_control(hwnd, &nl, 1);
1894 struct OIDToString
1896 LPCSTR oid;
1897 int id;
1900 /* The following list MUST be lexicographically sorted by OID */
1901 static struct OIDToString oidMap[] = {
1902 /* 1.3.6.1.4.1.311.10.3.1 */
1903 { szOID_KP_CTL_USAGE_SIGNING, IDS_PURPOSE_CTL_USAGE_SIGNING },
1904 /* 1.3.6.1.4.1.311.10.3.4 */
1905 { szOID_KP_EFS, IDS_PURPOSE_EFS },
1906 /* 1.3.6.1.4.1.311.10.3.4.1 */
1907 { szOID_EFS_RECOVERY, IDS_PURPOSE_EFS_RECOVERY },
1908 /* 1.3.6.1.4.1.311.10.3.5 */
1909 { szOID_WHQL_CRYPTO, IDS_PURPOSE_WHQL },
1910 /* 1.3.6.1.4.1.311.10.3.6 */
1911 { szOID_NT5_CRYPTO, IDS_PURPOSE_NT5 },
1912 /* 1.3.6.1.4.1.311.10.3.7 */
1913 { szOID_OEM_WHQL_CRYPTO, IDS_PURPOSE_OEM_WHQL },
1914 /* 1.3.6.1.4.1.311.10.3.8 */
1915 { szOID_EMBEDDED_NT_CRYPTO, IDS_PURPOSE_EMBEDDED_NT },
1916 /* 1.3.6.1.4.1.311.10.3.9 */
1917 { szOID_ROOT_LIST_SIGNER, IDS_PURPOSE_ROOT_LIST_SIGNER },
1918 /* 1.3.6.1.4.1.311.10.3.10 */
1919 { szOID_KP_QUALIFIED_SUBORDINATION, IDS_PURPOSE_QUALIFIED_SUBORDINATION },
1920 /* 1.3.6.1.4.1.311.10.3.11 */
1921 { szOID_KP_KEY_RECOVERY, IDS_PURPOSE_KEY_RECOVERY },
1922 /* 1.3.6.1.4.1.311.10.3.12 */
1923 { szOID_KP_DOCUMENT_SIGNING, IDS_PURPOSE_DOCUMENT_SIGNING },
1924 /* 1.3.6.1.4.1.311.10.3.13 */
1925 { szOID_KP_LIFETIME_SIGNING, IDS_PURPOSE_LIFETIME_SIGNING },
1926 /* 1.3.6.1.4.1.311.10.5.1 */
1927 { szOID_DRM, IDS_PURPOSE_DRM },
1928 /* 1.3.6.1.4.1.311.10.6.1 */
1929 { szOID_LICENSES, IDS_PURPOSE_LICENSES },
1930 /* 1.3.6.1.4.1.311.10.6.2 */
1931 { szOID_LICENSE_SERVER, IDS_PURPOSE_LICENSE_SERVER },
1932 /* 1.3.6.1.4.1.311.20.2.1 */
1933 { szOID_ENROLLMENT_AGENT, IDS_PURPOSE_ENROLLMENT_AGENT },
1934 /* 1.3.6.1.4.1.311.20.2.2 */
1935 { szOID_KP_SMARTCARD_LOGON, IDS_PURPOSE_SMARTCARD_LOGON },
1936 /* 1.3.6.1.4.1.311.21.5 */
1937 { szOID_KP_CA_EXCHANGE, IDS_PURPOSE_CA_EXCHANGE },
1938 /* 1.3.6.1.4.1.311.21.6 */
1939 { szOID_KP_KEY_RECOVERY_AGENT, IDS_PURPOSE_KEY_RECOVERY_AGENT },
1940 /* 1.3.6.1.4.1.311.21.19 */
1941 { szOID_DS_EMAIL_REPLICATION, IDS_PURPOSE_DS_EMAIL_REPLICATION },
1942 /* 1.3.6.1.5.5.7.3.1 */
1943 { szOID_PKIX_KP_SERVER_AUTH, IDS_PURPOSE_SERVER_AUTH },
1944 /* 1.3.6.1.5.5.7.3.2 */
1945 { szOID_PKIX_KP_CLIENT_AUTH, IDS_PURPOSE_CLIENT_AUTH },
1946 /* 1.3.6.1.5.5.7.3.3 */
1947 { szOID_PKIX_KP_CODE_SIGNING, IDS_PURPOSE_CODE_SIGNING },
1948 /* 1.3.6.1.5.5.7.3.4 */
1949 { szOID_PKIX_KP_EMAIL_PROTECTION, IDS_PURPOSE_EMAIL_PROTECTION },
1950 /* 1.3.6.1.5.5.7.3.5 */
1951 { szOID_PKIX_KP_IPSEC_END_SYSTEM, IDS_PURPOSE_IPSEC },
1952 /* 1.3.6.1.5.5.7.3.6 */
1953 { szOID_PKIX_KP_IPSEC_TUNNEL, IDS_PURPOSE_IPSEC },
1954 /* 1.3.6.1.5.5.7.3.7 */
1955 { szOID_PKIX_KP_IPSEC_USER, IDS_PURPOSE_IPSEC },
1956 /* 1.3.6.1.5.5.7.3.8 */
1957 { szOID_PKIX_KP_TIMESTAMP_SIGNING, IDS_PURPOSE_TIMESTAMP_SIGNING },
1960 static struct OIDToString *findSupportedOID(LPCSTR oid)
1962 int indexHigh = ARRAY_SIZE(oidMap) - 1, indexLow = 0;
1964 while (indexLow <= indexHigh)
1966 int cmp, i = (indexLow + indexHigh) / 2;
1967 if (!(cmp = strcmp(oid, oidMap[i].oid)))
1968 return &oidMap[i];
1969 if (cmp > 0)
1970 indexLow = i + 1;
1971 else
1972 indexHigh = i - 1;
1974 return NULL;
1977 static void add_local_oid_text_to_control(HWND text, LPCSTR oid)
1979 struct OIDToString *entry;
1980 WCHAR nl = '\n';
1981 PARAFORMAT2 parFmt;
1983 parFmt.cbSize = sizeof(parFmt);
1984 parFmt.dwMask = PFM_STARTINDENT;
1985 parFmt.dxStartIndent = MY_INDENT * 3;
1986 if ((entry = findSupportedOID(oid)))
1988 WCHAR *str, *linebreak, *ptr;
1989 BOOL multiline = FALSE;
1990 int len;
1992 len = LoadStringW(hInstance, entry->id, (LPWSTR)&str, 0);
1993 ptr = str;
1994 do {
1995 if ((linebreak = wmemchr(ptr, '\n', len)))
1997 WCHAR copy[MAX_STRING_LEN];
1999 multiline = TRUE;
2000 /* The source string contains a newline, which the richedit
2001 * control won't find since it's interpreted as a paragraph
2002 * break. Therefore copy up to the newline. lstrcpynW always
2003 * NULL-terminates, so pass one more than the length of the
2004 * source line so the copy includes the entire line and the
2005 * NULL-terminator.
2007 lstrcpynW(copy, ptr, linebreak - ptr + 1);
2008 add_text_with_paraformat_to_control(text, copy,
2009 linebreak - ptr, &parFmt);
2010 ptr = linebreak + 1;
2011 add_unformatted_text_to_control(text, &nl, 1);
2013 else if (multiline && *ptr)
2015 /* Add the last line */
2016 add_text_with_paraformat_to_control(text, ptr,
2017 len - (ptr - str), &parFmt);
2018 add_unformatted_text_to_control(text, &nl, 1);
2020 } while (linebreak);
2021 if (!multiline)
2023 add_text_with_paraformat_to_control(text, str, len, &parFmt);
2024 add_unformatted_text_to_control(text, &nl, 1);
2027 else
2029 WCHAR *oidW = HeapAlloc(GetProcessHeap(), 0,
2030 (strlen(oid) + 1) * sizeof(WCHAR));
2032 if (oidW)
2034 LPCSTR src;
2035 WCHAR *dst;
2037 for (src = oid, dst = oidW; *src; src++, dst++)
2038 *dst = *src;
2039 *dst = 0;
2040 add_text_with_paraformat_to_control(text, oidW, lstrlenW(oidW),
2041 &parFmt);
2042 add_unformatted_text_to_control(text, &nl, 1);
2043 HeapFree(GetProcessHeap(), 0, oidW);
2048 static void display_app_usages(HWND text, PCCERT_CONTEXT cert,
2049 BOOL *anyUsageAdded)
2051 static char any_app_policy[] = szOID_ANY_APPLICATION_POLICY;
2052 WCHAR nl = '\n';
2053 CHARFORMATW charFmt;
2054 PCERT_EXTENSION policyExt;
2055 if (!*anyUsageAdded)
2057 PARAFORMAT2 parFmt;
2059 parFmt.cbSize = sizeof(parFmt);
2060 parFmt.dwMask = PFM_STARTINDENT;
2061 parFmt.dxStartIndent = MY_INDENT;
2062 add_string_resource_with_paraformat_to_control(text,
2063 IDS_CERT_INFO_PURPOSES, &parFmt);
2064 add_unformatted_text_to_control(text, &nl, 1);
2065 *anyUsageAdded = TRUE;
2067 memset(&charFmt, 0, sizeof(charFmt));
2068 charFmt.cbSize = sizeof(charFmt);
2069 charFmt.dwMask = CFM_BOLD;
2070 charFmt.dwEffects = 0;
2071 SendMessageW(text, EM_SETCHARFORMAT, SCF_SELECTION, (LPARAM)&charFmt);
2072 if ((policyExt = CertFindExtension(szOID_APPLICATION_CERT_POLICIES,
2073 cert->pCertInfo->cExtension, cert->pCertInfo->rgExtension)))
2075 CERT_POLICIES_INFO *policies;
2076 DWORD size;
2078 if (CryptDecodeObjectEx(X509_ASN_ENCODING, X509_CERT_POLICIES,
2079 policyExt->Value.pbData, policyExt->Value.cbData,
2080 CRYPT_DECODE_ALLOC_FLAG, NULL, &policies, &size))
2082 DWORD i;
2084 for (i = 0; i < policies->cPolicyInfo; i++)
2086 DWORD j;
2088 for (j = 0; j < policies->rgPolicyInfo[i].cPolicyQualifier; j++)
2089 add_local_oid_text_to_control(text,
2090 policies->rgPolicyInfo[i].rgPolicyQualifier[j].
2091 pszPolicyQualifierId);
2093 LocalFree(policies);
2096 else
2097 add_oid_text_to_control(text, any_app_policy);
2100 static BOOL display_cert_usages(HWND text, PCCERT_CONTEXT cert,
2101 BOOL *anyUsageAdded)
2103 WCHAR nl = '\n';
2104 DWORD size;
2105 BOOL badUsages = FALSE;
2107 if (CertGetEnhancedKeyUsage(cert, 0, NULL, &size))
2109 CHARFORMATW charFmt;
2110 static char any_cert_policy[] = szOID_ANY_CERT_POLICY;
2111 PCERT_ENHKEY_USAGE usage = HeapAlloc(GetProcessHeap(), 0, size);
2113 if (usage)
2115 if (CertGetEnhancedKeyUsage(cert, 0, usage, &size))
2117 DWORD i;
2119 if (!*anyUsageAdded)
2121 PARAFORMAT2 parFmt;
2123 parFmt.cbSize = sizeof(parFmt);
2124 parFmt.dwMask = PFM_STARTINDENT;
2125 parFmt.dxStartIndent = MY_INDENT;
2126 add_string_resource_with_paraformat_to_control(text,
2127 IDS_CERT_INFO_PURPOSES, &parFmt);
2128 add_unformatted_text_to_control(text, &nl, 1);
2129 *anyUsageAdded = TRUE;
2131 memset(&charFmt, 0, sizeof(charFmt));
2132 charFmt.cbSize = sizeof(charFmt);
2133 charFmt.dwMask = CFM_BOLD;
2134 charFmt.dwEffects = 0;
2135 SendMessageW(text, EM_SETCHARFORMAT, SCF_SELECTION,
2136 (LPARAM)&charFmt);
2137 if (!usage->cUsageIdentifier)
2138 add_oid_text_to_control(text, any_cert_policy);
2139 else
2140 for (i = 0; i < usage->cUsageIdentifier; i++)
2141 add_local_oid_text_to_control(text,
2142 usage->rgpszUsageIdentifier[i]);
2144 else
2145 badUsages = TRUE;
2146 HeapFree(GetProcessHeap(), 0, usage);
2148 else
2149 badUsages = TRUE;
2151 else
2152 badUsages = TRUE;
2153 return badUsages;
2156 static void set_policy_text(HWND text,
2157 PCCRYPTUI_VIEWCERTIFICATE_STRUCTW pCertViewInfo)
2159 BOOL includeCertUsages = FALSE, includeAppUsages = FALSE;
2160 BOOL badUsages = FALSE, anyUsageAdded = FALSE;
2162 if (pCertViewInfo->cPurposes)
2164 DWORD i;
2166 for (i = 0; i < pCertViewInfo->cPurposes; i++)
2168 if (!strcmp(pCertViewInfo->rgszPurposes[i], szOID_ANY_CERT_POLICY))
2169 includeCertUsages = TRUE;
2170 else if (!strcmp(pCertViewInfo->rgszPurposes[i],
2171 szOID_ANY_APPLICATION_POLICY))
2172 includeAppUsages = TRUE;
2173 else
2174 badUsages = TRUE;
2177 else
2178 includeAppUsages = includeCertUsages = TRUE;
2179 if (includeAppUsages)
2180 display_app_usages(text, pCertViewInfo->pCertContext, &anyUsageAdded);
2181 if (includeCertUsages)
2182 badUsages = display_cert_usages(text, pCertViewInfo->pCertContext,
2183 &anyUsageAdded);
2184 if (badUsages)
2186 PARAFORMAT2 parFmt;
2188 parFmt.cbSize = sizeof(parFmt);
2189 parFmt.dwMask = PFM_STARTINDENT;
2190 parFmt.dxStartIndent = MY_INDENT;
2191 add_string_resource_with_paraformat_to_control(text,
2192 IDS_CERT_INFO_BAD_PURPOSES, &parFmt);
2196 static CRYPT_OBJID_BLOB *find_policy_qualifier(CERT_POLICIES_INFO *policies,
2197 LPCSTR policyOid)
2199 CRYPT_OBJID_BLOB *ret = NULL;
2200 DWORD i;
2202 for (i = 0; !ret && i < policies->cPolicyInfo; i++)
2204 DWORD j;
2206 for (j = 0; !ret && j < policies->rgPolicyInfo[i].cPolicyQualifier; j++)
2207 if (!strcmp(policies->rgPolicyInfo[i].rgPolicyQualifier[j].
2208 pszPolicyQualifierId, policyOid))
2209 ret = &policies->rgPolicyInfo[i].rgPolicyQualifier[j].
2210 Qualifier;
2212 return ret;
2215 static WCHAR *get_cps_str_from_qualifier(const CRYPT_OBJID_BLOB *qualifier)
2217 LPWSTR qualifierStr = NULL;
2218 CERT_NAME_VALUE *qualifierValue;
2219 DWORD size;
2221 if (CryptDecodeObjectEx(X509_ASN_ENCODING, X509_NAME_VALUE,
2222 qualifier->pbData, qualifier->cbData, CRYPT_DECODE_ALLOC_FLAG, NULL,
2223 &qualifierValue, &size))
2225 size = CertRDNValueToStrW(qualifierValue->dwValueType,
2226 &qualifierValue->Value, NULL, 0);
2227 qualifierStr = HeapAlloc(GetProcessHeap(), 0, size * sizeof(WCHAR));
2228 if (qualifierStr)
2229 CertRDNValueToStrW(qualifierValue->dwValueType,
2230 &qualifierValue->Value, qualifierStr, size);
2231 LocalFree(qualifierValue);
2233 return qualifierStr;
2236 static WCHAR *get_user_notice_from_qualifier(const CRYPT_OBJID_BLOB *qualifier)
2238 LPWSTR str = NULL;
2239 CERT_POLICY_QUALIFIER_USER_NOTICE *qualifierValue;
2240 DWORD size;
2242 if (CryptDecodeObjectEx(X509_ASN_ENCODING,
2243 X509_PKIX_POLICY_QUALIFIER_USERNOTICE,
2244 qualifier->pbData, qualifier->cbData, CRYPT_DECODE_ALLOC_FLAG, NULL,
2245 &qualifierValue, &size))
2247 str = HeapAlloc(GetProcessHeap(), 0,
2248 (lstrlenW(qualifierValue->pszDisplayText) + 1) * sizeof(WCHAR));
2249 if (str)
2250 lstrcpyW(str, qualifierValue->pszDisplayText);
2251 LocalFree(qualifierValue);
2253 return str;
2256 struct IssuerStatement
2258 LPWSTR cps;
2259 LPWSTR userNotice;
2262 static void set_issuer_statement(HWND hwnd,
2263 PCCRYPTUI_VIEWCERTIFICATE_STRUCTW pCertViewInfo)
2265 PCERT_EXTENSION policyExt;
2267 if (!(pCertViewInfo->dwFlags & CRYPTUI_DISABLE_ISSUERSTATEMENT) &&
2268 (policyExt = CertFindExtension(szOID_CERT_POLICIES,
2269 pCertViewInfo->pCertContext->pCertInfo->cExtension,
2270 pCertViewInfo->pCertContext->pCertInfo->rgExtension)))
2272 CERT_POLICIES_INFO *policies;
2273 DWORD size;
2275 if (CryptDecodeObjectEx(X509_ASN_ENCODING, policyExt->pszObjId,
2276 policyExt->Value.pbData, policyExt->Value.cbData,
2277 CRYPT_DECODE_ALLOC_FLAG, NULL, &policies, &size))
2279 CRYPT_OBJID_BLOB *qualifier;
2280 LPWSTR cps = NULL, userNotice = NULL;
2282 if ((qualifier = find_policy_qualifier(policies,
2283 szOID_PKIX_POLICY_QUALIFIER_CPS)))
2284 cps = get_cps_str_from_qualifier(qualifier);
2285 if ((qualifier = find_policy_qualifier(policies,
2286 szOID_PKIX_POLICY_QUALIFIER_USERNOTICE)))
2287 userNotice = get_user_notice_from_qualifier(qualifier);
2288 if (cps || userNotice)
2290 struct IssuerStatement *issuerStatement =
2291 HeapAlloc(GetProcessHeap(), 0, sizeof(struct IssuerStatement));
2293 if (issuerStatement)
2295 issuerStatement->cps = cps;
2296 issuerStatement->userNotice = userNotice;
2297 EnableWindow(GetDlgItem(hwnd, IDC_ISSUERSTATEMENT), TRUE);
2298 SetWindowLongPtrW(hwnd, DWLP_USER,
2299 (ULONG_PTR)issuerStatement);
2302 LocalFree(policies);
2307 static void set_cert_info(HWND hwnd,
2308 PCCRYPTUI_VIEWCERTIFICATE_STRUCTW pCertViewInfo)
2310 CHARFORMATW charFmt;
2311 PARAFORMAT2 parFmt;
2312 HWND icon = GetDlgItem(hwnd, IDC_CERTIFICATE_ICON);
2313 HWND text = GetDlgItem(hwnd, IDC_CERTIFICATE_INFO);
2314 CRYPT_PROVIDER_SGNR *provSigner = WTHelperGetProvSignerFromChain(
2315 (CRYPT_PROVIDER_DATA *)pCertViewInfo->u.pCryptProviderData,
2316 pCertViewInfo->idxSigner, pCertViewInfo->fCounterSigner,
2317 pCertViewInfo->idxCounterSigner);
2318 CRYPT_PROVIDER_CERT *root =
2319 &provSigner->pasCertChain[provSigner->csCertChain - 1];
2321 if (!provSigner->pChainContext ||
2322 (provSigner->pChainContext->TrustStatus.dwErrorStatus &
2323 CERT_TRUST_IS_PARTIAL_CHAIN))
2324 add_icon_to_control(icon, IDB_CERT_WARNING);
2325 else if (!root->fTrustedRoot)
2326 add_icon_to_control(icon, IDB_CERT_ERROR);
2327 else
2328 add_icon_to_control(icon, IDB_CERT);
2330 memset(&charFmt, 0, sizeof(charFmt));
2331 charFmt.cbSize = sizeof(charFmt);
2332 charFmt.dwMask = CFM_BOLD;
2333 charFmt.dwEffects = CFE_BOLD;
2334 SendMessageW(text, EM_SETCHARFORMAT, SCF_SELECTION, (LPARAM)&charFmt);
2335 /* FIXME: vertically center text */
2336 parFmt.cbSize = sizeof(parFmt);
2337 parFmt.dwMask = PFM_STARTINDENT;
2338 parFmt.dxStartIndent = MY_INDENT;
2339 add_string_resource_with_paraformat_to_control(text,
2340 IDS_CERTIFICATEINFORMATION, &parFmt);
2342 text = GetDlgItem(hwnd, IDC_CERTIFICATE_STATUS);
2343 SendMessageW(text, EM_SETCHARFORMAT, SCF_SELECTION, (LPARAM)&charFmt);
2344 if (provSigner->dwError == TRUST_E_CERT_SIGNATURE)
2345 add_string_resource_with_paraformat_to_control(text,
2346 IDS_CERT_INFO_BAD_SIG, &parFmt);
2347 else if (!provSigner->pChainContext ||
2348 (provSigner->pChainContext->TrustStatus.dwErrorStatus &
2349 CERT_TRUST_IS_PARTIAL_CHAIN))
2350 add_string_resource_with_paraformat_to_control(text,
2351 IDS_CERT_INFO_PARTIAL_CHAIN, &parFmt);
2352 else if (!root->fTrustedRoot)
2354 if (provSigner->csCertChain == 1 && root->fSelfSigned)
2355 add_string_resource_with_paraformat_to_control(text,
2356 IDS_CERT_INFO_UNTRUSTED_CA, &parFmt);
2357 else
2358 add_string_resource_with_paraformat_to_control(text,
2359 IDS_CERT_INFO_UNTRUSTED_ROOT, &parFmt);
2361 else
2363 set_policy_text(text, pCertViewInfo);
2364 set_issuer_statement(hwnd, pCertViewInfo);
2368 static void set_cert_name_string(HWND hwnd, PCCERT_CONTEXT cert,
2369 DWORD nameFlags, int heading)
2371 WCHAR nl = '\n';
2372 HWND text = GetDlgItem(hwnd, IDC_CERTIFICATE_NAMES);
2373 CHARFORMATW charFmt;
2374 PARAFORMAT2 parFmt;
2376 memset(&charFmt, 0, sizeof(charFmt));
2377 charFmt.cbSize = sizeof(charFmt);
2378 charFmt.dwMask = CFM_BOLD;
2379 charFmt.dwEffects = CFE_BOLD;
2380 SendMessageW(text, EM_SETCHARFORMAT, SCF_SELECTION, (LPARAM)&charFmt);
2381 parFmt.cbSize = sizeof(parFmt);
2382 parFmt.dwMask = PFM_STARTINDENT;
2383 parFmt.dxStartIndent = MY_INDENT * 3;
2384 add_string_resource_with_paraformat_to_control(text, heading, &parFmt);
2385 charFmt.dwEffects = 0;
2386 SendMessageW(text, EM_SETCHARFORMAT, SCF_SELECTION, (LPARAM)&charFmt);
2387 add_cert_string_to_control(text, cert, CERT_NAME_SIMPLE_DISPLAY_TYPE,
2388 nameFlags);
2389 add_unformatted_text_to_control(text, &nl, 1);
2390 add_unformatted_text_to_control(text, &nl, 1);
2391 add_unformatted_text_to_control(text, &nl, 1);
2395 static void add_date_string_to_control(HWND hwnd, const FILETIME *fileTime)
2397 WCHAR dateFmt[80]; /* sufficient for all versions of LOCALE_SSHORTDATE */
2398 WCHAR date[80];
2399 SYSTEMTIME sysTime;
2401 GetLocaleInfoW(LOCALE_SYSTEM_DEFAULT, LOCALE_SSHORTDATE, dateFmt, ARRAY_SIZE(dateFmt));
2402 FileTimeToSystemTime(fileTime, &sysTime);
2403 GetDateFormatW(LOCALE_SYSTEM_DEFAULT, 0, &sysTime, dateFmt, date, ARRAY_SIZE(date));
2404 add_unformatted_text_to_control(hwnd, date, lstrlenW(date));
2407 static void set_cert_validity_period(HWND hwnd, PCCERT_CONTEXT cert)
2409 WCHAR nl = '\n';
2410 HWND text = GetDlgItem(hwnd, IDC_CERTIFICATE_NAMES);
2411 CHARFORMATW charFmt;
2412 PARAFORMAT2 parFmt;
2414 memset(&charFmt, 0, sizeof(charFmt));
2415 charFmt.cbSize = sizeof(charFmt);
2416 charFmt.dwMask = CFM_BOLD;
2417 charFmt.dwEffects = CFE_BOLD;
2418 SendMessageW(text, EM_SETCHARFORMAT, SCF_SELECTION, (LPARAM)&charFmt);
2419 parFmt.cbSize = sizeof(parFmt);
2420 parFmt.dwMask = PFM_STARTINDENT;
2421 parFmt.dxStartIndent = MY_INDENT * 3;
2422 add_string_resource_with_paraformat_to_control(text, IDS_VALID_FROM,
2423 &parFmt);
2424 charFmt.dwEffects = 0;
2425 SendMessageW(text, EM_SETCHARFORMAT, SCF_SELECTION, (LPARAM)&charFmt);
2426 add_date_string_to_control(text, &cert->pCertInfo->NotBefore);
2427 charFmt.dwEffects = CFE_BOLD;
2428 SendMessageW(text, EM_SETCHARFORMAT, SCF_SELECTION, (LPARAM)&charFmt);
2429 add_string_resource_to_control(text, IDS_VALID_TO);
2430 charFmt.dwEffects = 0;
2431 SendMessageW(text, EM_SETCHARFORMAT, SCF_SELECTION, (LPARAM)&charFmt);
2432 add_date_string_to_control(text, &cert->pCertInfo->NotAfter);
2433 add_unformatted_text_to_control(text, &nl, 1);
2436 static void set_general_info(HWND hwnd,
2437 PCCRYPTUI_VIEWCERTIFICATE_STRUCTW pCertViewInfo)
2439 set_cert_info(hwnd, pCertViewInfo);
2440 set_cert_name_string(hwnd, pCertViewInfo->pCertContext, 0,
2441 IDS_SUBJECT_HEADING);
2442 set_cert_name_string(hwnd, pCertViewInfo->pCertContext,
2443 CERT_NAME_ISSUER_FLAG, IDS_ISSUER_HEADING);
2444 set_cert_validity_period(hwnd, pCertViewInfo->pCertContext);
2447 static LRESULT CALLBACK user_notice_dlg_proc(HWND hwnd, UINT msg, WPARAM wp,
2448 LPARAM lp)
2450 LRESULT ret = 0;
2451 HWND text;
2452 struct IssuerStatement *issuerStatement;
2454 switch (msg)
2456 case WM_INITDIALOG:
2457 text = GetDlgItem(hwnd, IDC_USERNOTICE);
2458 issuerStatement = (struct IssuerStatement *)lp;
2459 add_unformatted_text_to_control(text, issuerStatement->userNotice,
2460 lstrlenW(issuerStatement->userNotice));
2461 if (issuerStatement->cps)
2462 SetWindowLongPtrW(hwnd, DWLP_USER, (LPARAM)issuerStatement->cps);
2463 else
2464 EnableWindow(GetDlgItem(hwnd, IDC_CPS), FALSE);
2465 break;
2466 case WM_COMMAND:
2467 switch (wp)
2469 case IDOK:
2470 EndDialog(hwnd, IDOK);
2471 ret = TRUE;
2472 break;
2473 case IDC_CPS:
2475 IBindCtx *bctx = NULL;
2476 LPWSTR cps;
2478 CreateBindCtx(0, &bctx);
2479 cps = (LPWSTR)GetWindowLongPtrW(hwnd, DWLP_USER);
2480 HlinkSimpleNavigateToString(cps, NULL, NULL, NULL, bctx, NULL,
2481 HLNF_OPENINNEWWINDOW, 0);
2482 IBindCtx_Release(bctx);
2483 break;
2487 return ret;
2490 static void show_user_notice(HWND hwnd, struct IssuerStatement *issuerStatement)
2492 DialogBoxParamW(hInstance, MAKEINTRESOURCEW(IDD_USERNOTICE), hwnd,
2493 user_notice_dlg_proc, (LPARAM)issuerStatement);
2496 static LRESULT CALLBACK general_dlg_proc(HWND hwnd, UINT msg, WPARAM wp,
2497 LPARAM lp)
2499 PROPSHEETPAGEW *page;
2500 PCCRYPTUI_VIEWCERTIFICATE_STRUCTW pCertViewInfo;
2502 TRACE("(%p, %08x, %08lx, %08lx)\n", hwnd, msg, wp, lp);
2504 switch (msg)
2506 case WM_INITDIALOG:
2507 page = (PROPSHEETPAGEW *)lp;
2508 pCertViewInfo = (PCCRYPTUI_VIEWCERTIFICATE_STRUCTW)page->lParam;
2509 if (pCertViewInfo->dwFlags & CRYPTUI_DISABLE_ADDTOSTORE)
2510 ShowWindow(GetDlgItem(hwnd, IDC_ADDTOSTORE), FALSE);
2511 EnableWindow(GetDlgItem(hwnd, IDC_ISSUERSTATEMENT), FALSE);
2512 set_general_info(hwnd, pCertViewInfo);
2513 break;
2514 case WM_COMMAND:
2515 switch (wp)
2517 case IDC_ADDTOSTORE:
2518 CryptUIWizImport(0, hwnd, NULL, NULL, NULL);
2519 break;
2520 case IDC_ISSUERSTATEMENT:
2522 struct IssuerStatement *issuerStatement =
2523 (struct IssuerStatement *)GetWindowLongPtrW(hwnd, DWLP_USER);
2525 if (issuerStatement)
2527 if (issuerStatement->userNotice)
2528 show_user_notice(hwnd, issuerStatement);
2529 else if (issuerStatement->cps)
2531 IBindCtx *bctx = NULL;
2533 CreateBindCtx(0, &bctx);
2534 HlinkSimpleNavigateToString(issuerStatement->cps, NULL,
2535 NULL, NULL, bctx, NULL, HLNF_OPENINNEWWINDOW, 0);
2536 IBindCtx_Release(bctx);
2539 break;
2542 break;
2544 return 0;
2547 static UINT CALLBACK general_callback_proc(HWND hwnd, UINT msg,
2548 PROPSHEETPAGEW *page)
2550 struct IssuerStatement *issuerStatement;
2552 switch (msg)
2554 case PSPCB_RELEASE:
2555 issuerStatement =
2556 (struct IssuerStatement *)GetWindowLongPtrW(hwnd, DWLP_USER);
2557 if (issuerStatement)
2559 HeapFree(GetProcessHeap(), 0, issuerStatement->cps);
2560 HeapFree(GetProcessHeap(), 0, issuerStatement->userNotice);
2561 HeapFree(GetProcessHeap(), 0, issuerStatement);
2563 break;
2565 return 1;
2568 static void init_general_page(PCCRYPTUI_VIEWCERTIFICATE_STRUCTW pCertViewInfo,
2569 PROPSHEETPAGEW *page)
2571 memset(page, 0, sizeof(PROPSHEETPAGEW));
2572 page->dwSize = sizeof(PROPSHEETPAGEW);
2573 page->dwFlags = PSP_USECALLBACK;
2574 page->pfnCallback = general_callback_proc;
2575 page->hInstance = hInstance;
2576 page->u.pszTemplate = MAKEINTRESOURCEW(IDD_GENERAL);
2577 page->pfnDlgProc = general_dlg_proc;
2578 page->lParam = (LPARAM)pCertViewInfo;
2581 typedef WCHAR * (*field_format_func)(PCCERT_CONTEXT cert);
2583 static WCHAR *field_format_version(PCCERT_CONTEXT cert)
2585 static const WCHAR fmt[] = { 'V','%','d',0 };
2586 WCHAR *buf = HeapAlloc(GetProcessHeap(), 0, 12 * sizeof(WCHAR));
2588 if (buf)
2589 swprintf(buf, 12, fmt, cert->pCertInfo->dwVersion);
2590 return buf;
2593 static WCHAR *format_hex_string(void *pb, DWORD cb)
2595 WCHAR *buf = HeapAlloc(GetProcessHeap(), 0, (cb * 3 + 1) * sizeof(WCHAR));
2597 if (buf)
2599 static const WCHAR fmt[] = { '%','0','2','x',' ',0 };
2600 DWORD i;
2601 WCHAR *ptr;
2603 for (i = 0, ptr = buf; i < cb; i++, ptr += 3)
2604 swprintf(ptr, 4, fmt, ((BYTE *)pb)[i]);
2606 return buf;
2609 static WCHAR *field_format_serial_number(PCCERT_CONTEXT cert)
2611 return format_hex_string(cert->pCertInfo->SerialNumber.pbData,
2612 cert->pCertInfo->SerialNumber.cbData);
2615 static WCHAR *field_format_issuer(PCCERT_CONTEXT cert)
2617 return get_cert_name_string(cert, CERT_NAME_SIMPLE_DISPLAY_TYPE,
2618 CERT_NAME_ISSUER_FLAG);
2621 static WCHAR *field_format_detailed_cert_name(PCERT_NAME_BLOB name)
2623 WCHAR *str = NULL;
2624 DWORD len = CertNameToStrW(X509_ASN_ENCODING, name,
2625 CERT_X500_NAME_STR | CERT_NAME_STR_CRLF_FLAG, NULL, 0);
2627 if (len)
2629 str = HeapAlloc(GetProcessHeap(), 0, len * sizeof(WCHAR));
2630 if (str)
2631 CertNameToStrW(X509_ASN_ENCODING, name,
2632 CERT_X500_NAME_STR | CERT_NAME_STR_CRLF_FLAG, str, len);
2634 return str;
2637 static WCHAR *field_format_detailed_issuer(PCCERT_CONTEXT cert, void *param)
2639 return field_format_detailed_cert_name(&cert->pCertInfo->Issuer);
2642 static WCHAR *field_format_subject(PCCERT_CONTEXT cert)
2644 return get_cert_name_string(cert, CERT_NAME_SIMPLE_DISPLAY_TYPE, 0);
2647 static WCHAR *field_format_detailed_subject(PCCERT_CONTEXT cert, void *param)
2649 return field_format_detailed_cert_name(&cert->pCertInfo->Subject);
2652 static WCHAR *format_long_date(const FILETIME *fileTime)
2654 WCHAR dateFmt[80]; /* long enough for LOCALE_SLONGDATE */
2655 DWORD len;
2656 WCHAR *buf = NULL;
2657 SYSTEMTIME sysTime;
2659 /* FIXME: format isn't quite right, want time too */
2660 GetLocaleInfoW(LOCALE_SYSTEM_DEFAULT, LOCALE_SLONGDATE, dateFmt, ARRAY_SIZE(dateFmt));
2661 FileTimeToSystemTime(fileTime, &sysTime);
2662 len = GetDateFormatW(LOCALE_SYSTEM_DEFAULT, 0, &sysTime, dateFmt, NULL, 0);
2663 if (len)
2665 buf = HeapAlloc(GetProcessHeap(), 0, len * sizeof(WCHAR));
2666 if (buf)
2667 GetDateFormatW(LOCALE_SYSTEM_DEFAULT, 0, &sysTime, dateFmt, buf,
2668 len);
2670 return buf;
2673 static WCHAR *field_format_from_date(PCCERT_CONTEXT cert)
2675 return format_long_date(&cert->pCertInfo->NotBefore);
2678 static WCHAR *field_format_to_date(PCCERT_CONTEXT cert)
2680 return format_long_date(&cert->pCertInfo->NotAfter);
2683 static WCHAR *field_format_public_key(PCCERT_CONTEXT cert)
2685 PCCRYPT_OID_INFO oidInfo;
2686 WCHAR *buf = NULL;
2688 oidInfo = CryptFindOIDInfo(CRYPT_OID_INFO_OID_KEY,
2689 cert->pCertInfo->SubjectPublicKeyInfo.Algorithm.pszObjId, 0);
2690 if (oidInfo)
2692 WCHAR fmt[MAX_STRING_LEN];
2694 if (LoadStringW(hInstance, IDS_FIELD_PUBLIC_KEY_FORMAT, fmt, ARRAY_SIZE(fmt)))
2696 DWORD len;
2698 /* Allocate the output buffer. Use the number of bytes in the
2699 * public key as a conservative (high) estimate for the number of
2700 * digits in its output.
2701 * The output is of the form (in English)
2702 * "<public key algorithm> (<public key bit length> bits)".
2703 * Ordinarily having two positional parameters in a string is not a
2704 * good idea, but as this isn't a sentence fragment, it shouldn't
2705 * be word-order dependent.
2707 len = lstrlenW(fmt) + lstrlenW(oidInfo->pwszName) +
2708 cert->pCertInfo->SubjectPublicKeyInfo.PublicKey.cbData * 8;
2709 buf = HeapAlloc(GetProcessHeap(), 0, len * sizeof(*buf));
2710 if (buf)
2712 DWORD_PTR args[2];
2713 args[0] = (DWORD_PTR)oidInfo->pwszName;
2714 args[1] = CertGetPublicKeyLength(X509_ASN_ENCODING,
2715 &cert->pCertInfo->SubjectPublicKeyInfo);
2716 FormatMessageW(FORMAT_MESSAGE_FROM_STRING|FORMAT_MESSAGE_ARGUMENT_ARRAY,
2717 fmt, 0, 0, buf, len, (__ms_va_list*)args);
2721 return buf;
2724 static WCHAR *field_format_detailed_public_key(PCCERT_CONTEXT cert, void *param)
2726 return format_hex_string(
2727 cert->pCertInfo->SubjectPublicKeyInfo.PublicKey.pbData,
2728 cert->pCertInfo->SubjectPublicKeyInfo.PublicKey.cbData);
2731 struct field_value_data;
2732 struct detail_data
2734 PCCRYPTUI_VIEWCERTIFICATE_STRUCTW pCertViewInfo;
2735 BOOL *pfPropertiesChanged;
2736 int cFields;
2737 struct field_value_data *fields;
2740 typedef void (*add_fields_func)(HWND hwnd, struct detail_data *data);
2742 typedef WCHAR *(*create_detailed_value_func)(PCCERT_CONTEXT cert, void *param);
2744 struct field_value_data
2746 create_detailed_value_func create;
2747 LPWSTR detailed_value;
2748 void *param;
2751 static void add_field_value_data(struct detail_data *data,
2752 create_detailed_value_func create, void *param)
2754 if (data->cFields)
2755 data->fields = HeapReAlloc(GetProcessHeap(), 0, data->fields,
2756 (data->cFields + 1) * sizeof(struct field_value_data));
2757 else
2758 data->fields = HeapAlloc(GetProcessHeap(), 0,
2759 sizeof(struct field_value_data));
2760 if (data->fields)
2762 data->fields[data->cFields].create = create;
2763 data->fields[data->cFields].detailed_value = NULL;
2764 data->fields[data->cFields].param = param;
2765 data->cFields++;
2769 static void add_field_and_value_to_list(HWND hwnd, struct detail_data *data,
2770 LPWSTR field, LPWSTR value, create_detailed_value_func create, void *param)
2772 LVITEMW item;
2773 int iItem = SendMessageW(hwnd, LVM_GETITEMCOUNT, 0, 0);
2775 item.mask = LVIF_TEXT | LVIF_PARAM;
2776 item.iItem = iItem;
2777 item.iSubItem = 0;
2778 item.pszText = field;
2779 item.lParam = (LPARAM)data;
2780 SendMessageW(hwnd, LVM_INSERTITEMW, 0, (LPARAM)&item);
2781 if (value)
2783 item.pszText = value;
2784 item.iSubItem = 1;
2785 SendMessageW(hwnd, LVM_SETITEMTEXTW, iItem, (LPARAM)&item);
2787 add_field_value_data(data, create, param);
2790 static void add_string_id_and_value_to_list(HWND hwnd, struct detail_data *data,
2791 int id, LPWSTR value, create_detailed_value_func create, void *param)
2793 WCHAR buf[MAX_STRING_LEN];
2795 LoadStringW(hInstance, id, buf, ARRAY_SIZE(buf));
2796 add_field_and_value_to_list(hwnd, data, buf, value, create, param);
2799 struct v1_field
2801 int id;
2802 field_format_func format;
2803 create_detailed_value_func create_detailed_value;
2806 static void add_v1_field(HWND hwnd, struct detail_data *data,
2807 const struct v1_field *field)
2809 WCHAR *val = field->format(data->pCertViewInfo->pCertContext);
2811 if (val)
2813 add_string_id_and_value_to_list(hwnd, data, field->id, val,
2814 field->create_detailed_value, NULL);
2815 HeapFree(GetProcessHeap(), 0, val);
2819 static const struct v1_field v1_fields[] = {
2820 { IDS_FIELD_VERSION, field_format_version, NULL },
2821 { IDS_FIELD_SERIAL_NUMBER, field_format_serial_number, NULL },
2822 { IDS_FIELD_ISSUER, field_format_issuer, field_format_detailed_issuer },
2823 { IDS_FIELD_VALID_FROM, field_format_from_date, NULL },
2824 { IDS_FIELD_VALID_TO, field_format_to_date, NULL },
2825 { IDS_FIELD_SUBJECT, field_format_subject, field_format_detailed_subject },
2826 { IDS_FIELD_PUBLIC_KEY, field_format_public_key,
2827 field_format_detailed_public_key }
2830 static void add_v1_fields(HWND hwnd, struct detail_data *data)
2832 unsigned int i;
2833 PCCERT_CONTEXT cert = data->pCertViewInfo->pCertContext;
2835 /* The last item in v1_fields is the public key, which is not in the loop
2836 * because it's a special case.
2838 for (i = 0; i < ARRAY_SIZE(v1_fields) - 1; i++)
2839 add_v1_field(hwnd, data, &v1_fields[i]);
2840 if (cert->pCertInfo->SubjectPublicKeyInfo.PublicKey.cbData)
2841 add_v1_field(hwnd, data, &v1_fields[i]);
2844 static WCHAR *crypt_format_extension(const CERT_EXTENSION *ext, DWORD formatStrType)
2846 WCHAR *str = NULL;
2847 DWORD size;
2849 if (CryptFormatObject(X509_ASN_ENCODING, 0, formatStrType, NULL,
2850 ext->pszObjId, ext->Value.pbData, ext->Value.cbData, NULL, &size))
2852 str = HeapAlloc(GetProcessHeap(), 0, size);
2853 CryptFormatObject(X509_ASN_ENCODING, 0, formatStrType, NULL,
2854 ext->pszObjId, ext->Value.pbData, ext->Value.cbData, str, &size);
2856 return str;
2859 static WCHAR *field_format_extension_hex_with_ascii(const CERT_EXTENSION *ext)
2861 WCHAR *str = NULL;
2863 if (ext->Value.cbData)
2865 /* The output is formatted as:
2866 * <hex bytes> <ascii bytes>\n
2867 * where <hex bytes> is a string of up to 8 bytes, output as %02x,
2868 * and <ascii bytes> is the ASCII equivalent of each byte, or '.' if
2869 * the byte is not printable.
2870 * So, for example, the extension value consisting of the following
2871 * bytes:
2872 * 0x30,0x14,0x31,0x12,0x30,0x10,0x06,0x03,0x55,0x04,0x03,
2873 * 0x13,0x09,0x4a,0x75,0x61,0x6e,0x20,0x4c,0x61,0x6e,0x67
2874 * is output as:
2875 * 30 14 31 12 30 10 06 03 0.1.0...
2876 * 55 04 03 13 09 4a 75 61 U....Jua
2877 * 6e 20 4c 61 6e 67 n Lang
2878 * The allocation size therefore requires:
2879 * - 4 characters per character in an 8-byte line
2880 * (2 for the hex format, one for the space, one for the ASCII value)
2881 * - 3 more characters per 8-byte line (two spaces and a newline)
2882 * - 1 character for the terminating nul
2883 * FIXME: should use a fixed-width font for this
2885 DWORD lines = (ext->Value.cbData + 7) / 8;
2887 str = HeapAlloc(GetProcessHeap(), 0,
2888 (lines * 8 * 4 + lines * 3 + 1) * sizeof(WCHAR));
2889 if (str)
2891 static const WCHAR fmt[] = { '%','0','2','x',' ',0 };
2892 DWORD i, j;
2893 WCHAR *ptr;
2895 for (i = 0, ptr = str; i < ext->Value.cbData; i += 8)
2897 /* Output as hex bytes first */
2898 for (j = i; j < min(i + 8, ext->Value.cbData); j++, ptr += 3)
2899 swprintf(ptr, 4, fmt, ext->Value.pbData[j]);
2900 /* Pad the hex output with spaces for alignment */
2901 if (j == ext->Value.cbData && j % 8)
2903 static const WCHAR pad[] = { ' ',' ',' ' };
2905 for (; j % 8; j++, ptr += ARRAY_SIZE(pad))
2906 memcpy(ptr, pad, sizeof(pad));
2908 /* The last swprintf included a space, so just insert one
2909 * more space between the hex bytes and the ASCII output
2911 *ptr++ = ' ';
2912 /* Output as ASCII bytes */
2913 for (j = i; j < min(i + 8, ext->Value.cbData); j++, ptr++)
2915 if (iswprint(ext->Value.pbData[j]) &&
2916 !iswspace(ext->Value.pbData[j]))
2917 *ptr = ext->Value.pbData[j];
2918 else
2919 *ptr = '.';
2921 *ptr++ = '\n';
2923 *ptr++ = '\0';
2926 return str;
2929 static WCHAR *field_format_detailed_extension(PCCERT_CONTEXT cert, void *param)
2931 PCERT_EXTENSION ext = param;
2932 LPWSTR str = crypt_format_extension(ext,
2933 CRYPT_FORMAT_STR_MULTI_LINE | CRYPT_FORMAT_STR_NO_HEX);
2935 if (!str)
2936 str = field_format_extension_hex_with_ascii(ext);
2937 return str;
2940 static void add_cert_extension_detail(HWND hwnd, struct detail_data *data,
2941 PCERT_EXTENSION ext)
2943 PCCRYPT_OID_INFO oidInfo = CryptFindOIDInfo(CRYPT_OID_INFO_OID_KEY,
2944 ext->pszObjId, 0);
2945 LPWSTR val = crypt_format_extension(ext, 0);
2947 if (oidInfo)
2948 add_field_and_value_to_list(hwnd, data, (LPWSTR)oidInfo->pwszName,
2949 val, field_format_detailed_extension, ext);
2950 else
2952 DWORD len = strlen(ext->pszObjId);
2953 LPWSTR oidW = HeapAlloc(GetProcessHeap(), 0, (len + 1) * sizeof(WCHAR));
2955 if (oidW)
2957 DWORD i;
2959 for (i = 0; i <= len; i++)
2960 oidW[i] = ext->pszObjId[i];
2961 add_field_and_value_to_list(hwnd, data, oidW, val,
2962 field_format_detailed_extension, ext);
2963 HeapFree(GetProcessHeap(), 0, oidW);
2966 HeapFree(GetProcessHeap(), 0, val);
2969 static void add_all_extensions(HWND hwnd, struct detail_data *data)
2971 DWORD i;
2972 PCCERT_CONTEXT cert = data->pCertViewInfo->pCertContext;
2974 for (i = 0; i < cert->pCertInfo->cExtension; i++)
2975 add_cert_extension_detail(hwnd, data, &cert->pCertInfo->rgExtension[i]);
2978 static void add_critical_extensions(HWND hwnd, struct detail_data *data)
2980 DWORD i;
2981 PCCERT_CONTEXT cert = data->pCertViewInfo->pCertContext;
2983 for (i = 0; i < cert->pCertInfo->cExtension; i++)
2984 if (cert->pCertInfo->rgExtension[i].fCritical)
2985 add_cert_extension_detail(hwnd, data,
2986 &cert->pCertInfo->rgExtension[i]);
2989 typedef WCHAR * (*prop_to_value_func)(void *pb, DWORD cb);
2991 struct prop_id_to_string_id
2993 DWORD prop;
2994 int id;
2995 BOOL prop_is_string;
2996 prop_to_value_func prop_to_value;
2999 static WCHAR *format_enhanced_key_usage_value(void *pb, DWORD cb)
3001 CERT_EXTENSION ext;
3003 ext.pszObjId = (LPSTR)X509_ENHANCED_KEY_USAGE;
3004 ext.fCritical = FALSE;
3005 ext.Value.pbData = pb;
3006 ext.Value.cbData = cb;
3007 return crypt_format_extension(&ext, 0);
3010 /* Logically the access state should also be checked, and IDC_EDITPROPERTIES
3011 * disabled for read-only certificates, but native doesn't appear to do that.
3013 static const struct prop_id_to_string_id prop_id_map[] = {
3014 { CERT_HASH_PROP_ID, IDS_PROP_HASH, FALSE, format_hex_string },
3015 { CERT_FRIENDLY_NAME_PROP_ID, IDS_PROP_FRIENDLY_NAME, TRUE, NULL },
3016 { CERT_DESCRIPTION_PROP_ID, IDS_PROP_DESCRIPTION, TRUE, NULL },
3017 { CERT_ENHKEY_USAGE_PROP_ID, IDS_PROP_ENHKEY_USAGE, FALSE,
3018 format_enhanced_key_usage_value },
3021 static void add_properties(HWND hwnd, struct detail_data *data)
3023 DWORD i;
3024 PCCERT_CONTEXT cert = data->pCertViewInfo->pCertContext;
3026 for (i = 0; i < ARRAY_SIZE(prop_id_map); i++)
3028 DWORD cb;
3030 if (CertGetCertificateContextProperty(cert, prop_id_map[i].prop, NULL,
3031 &cb))
3033 BYTE *pb;
3034 WCHAR *val = NULL;
3036 /* FIXME: MS adds a separate value for the signature hash
3037 * algorithm.
3039 pb = HeapAlloc(GetProcessHeap(), 0, cb);
3040 if (pb)
3042 if (CertGetCertificateContextProperty(cert,
3043 prop_id_map[i].prop, pb, &cb))
3045 if (prop_id_map[i].prop_is_string)
3047 val = (LPWSTR)pb;
3048 /* Don't double-free pb */
3049 pb = NULL;
3051 else
3052 val = prop_id_map[i].prop_to_value(pb, cb);
3054 HeapFree(GetProcessHeap(), 0, pb);
3056 add_string_id_and_value_to_list(hwnd, data, prop_id_map[i].id, val,
3057 NULL, NULL);
3062 static void add_all_fields(HWND hwnd, struct detail_data *data)
3064 add_v1_fields(hwnd, data);
3065 add_all_extensions(hwnd, data);
3066 add_properties(hwnd, data);
3069 struct selection_list_item
3071 int id;
3072 add_fields_func add;
3075 static const struct selection_list_item listItems[] = {
3076 { IDS_FIELDS_ALL, add_all_fields },
3077 { IDS_FIELDS_V1, add_v1_fields },
3078 { IDS_FIELDS_EXTENSIONS, add_all_extensions },
3079 { IDS_FIELDS_CRITICAL_EXTENSIONS, add_critical_extensions },
3080 { IDS_FIELDS_PROPERTIES, add_properties },
3083 static void create_show_list(HWND hwnd, struct detail_data *data)
3085 HWND cb = GetDlgItem(hwnd, IDC_DETAIL_SELECT);
3086 WCHAR buf[MAX_STRING_LEN];
3087 int i;
3089 for (i = 0; i < ARRAY_SIZE(listItems); i++)
3091 int index;
3093 LoadStringW(hInstance, listItems[i].id, buf, ARRAY_SIZE(buf));
3094 index = SendMessageW(cb, CB_INSERTSTRING, -1, (LPARAM)buf);
3095 SendMessageW(cb, CB_SETITEMDATA, index, (LPARAM)data);
3097 SendMessageW(cb, CB_SETCURSEL, 0, 0);
3100 static void create_listview_columns(HWND hwnd)
3102 HWND lv = GetDlgItem(hwnd, IDC_DETAIL_LIST);
3103 RECT rc;
3104 WCHAR buf[MAX_STRING_LEN];
3105 LVCOLUMNW column;
3107 SendMessageW(lv, LVM_SETEXTENDEDLISTVIEWSTYLE, 0, LVS_EX_FULLROWSELECT);
3108 GetWindowRect(lv, &rc);
3109 LoadStringW(hInstance, IDS_FIELD, buf, ARRAY_SIZE(buf));
3110 column.mask = LVCF_WIDTH | LVCF_TEXT;
3111 column.cx = (rc.right - rc.left) / 2 - 2;
3112 column.pszText = buf;
3113 SendMessageW(lv, LVM_INSERTCOLUMNW, 0, (LPARAM)&column);
3114 LoadStringW(hInstance, IDS_VALUE, buf, ARRAY_SIZE(buf));
3115 SendMessageW(lv, LVM_INSERTCOLUMNW, 1, (LPARAM)&column);
3118 static void set_fields_selection(HWND hwnd, struct detail_data *data, int sel)
3120 HWND list = GetDlgItem(hwnd, IDC_DETAIL_LIST);
3122 if (sel >= 0 && sel < ARRAY_SIZE(listItems))
3124 SendMessageW(list, LVM_DELETEALLITEMS, 0, 0);
3125 listItems[sel].add(list, data);
3129 static void create_cert_details_list(HWND hwnd, struct detail_data *data)
3131 create_show_list(hwnd, data);
3132 create_listview_columns(hwnd);
3133 set_fields_selection(hwnd, data, 0);
3136 static void add_purpose(HWND hwnd, LPCSTR oid)
3138 HWND lv = GetDlgItem(hwnd, IDC_CERTIFICATE_USAGES);
3139 PCRYPT_OID_INFO info = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY,
3140 sizeof(CRYPT_OID_INFO));
3142 if (info)
3144 char *oidCopy = HeapAlloc(GetProcessHeap(), 0, strlen(oid) + 1);
3146 if (oidCopy)
3148 LVITEMA item;
3150 strcpy(oidCopy, oid);
3151 info->cbSize = sizeof(CRYPT_OID_INFO);
3152 info->pszOID = oidCopy;
3153 item.mask = LVIF_TEXT | LVIF_STATE | LVIF_PARAM;
3154 item.state = INDEXTOSTATEIMAGEMASK(CheckBitmapIndexChecked);
3155 item.stateMask = LVIS_STATEIMAGEMASK;
3156 item.iItem = SendMessageW(lv, LVM_GETITEMCOUNT, 0, 0);
3157 item.iSubItem = 0;
3158 item.lParam = (LPARAM)info;
3159 item.pszText = oidCopy;
3160 SendMessageA(lv, LVM_INSERTITEMA, 0, (LPARAM)&item);
3162 else
3163 HeapFree(GetProcessHeap(), 0, info);
3167 static BOOL is_valid_oid(LPCSTR oid)
3169 BOOL ret;
3171 if (oid[0] != '0' && oid[0] != '1' && oid[0] != '2')
3172 ret = FALSE;
3173 else if (oid[1] != '.')
3174 ret = FALSE;
3175 else if (!oid[2])
3176 ret = FALSE;
3177 else
3179 const char *ptr;
3180 BOOL expectNum = TRUE;
3182 for (ptr = oid + 2, ret = TRUE; ret && *ptr; ptr++)
3184 if (expectNum)
3186 if (!isdigit(*ptr))
3187 ret = FALSE;
3188 else if (*(ptr + 1) == '.')
3189 expectNum = FALSE;
3191 else
3193 if (*ptr != '.')
3194 ret = FALSE;
3195 else if (!(*(ptr + 1)))
3196 ret = FALSE;
3197 else
3198 expectNum = TRUE;
3202 return ret;
3205 static BOOL is_oid_in_list(HWND hwnd, LPCSTR oid)
3207 return find_oid_in_list(GetDlgItem(hwnd, IDC_CERTIFICATE_USAGES), oid)
3208 != -1;
3211 #define MAX_PURPOSE 255
3213 static LRESULT CALLBACK add_purpose_dlg_proc(HWND hwnd, UINT msg,
3214 WPARAM wp, LPARAM lp)
3216 LRESULT ret = 0;
3217 char buf[MAX_PURPOSE + 1];
3219 switch (msg)
3221 case WM_INITDIALOG:
3222 SendMessageW(GetDlgItem(hwnd, IDC_NEW_PURPOSE), EM_SETLIMITTEXT,
3223 MAX_PURPOSE, 0);
3224 ShowScrollBar(GetDlgItem(hwnd, IDC_NEW_PURPOSE), SB_VERT, FALSE);
3225 SetWindowLongPtrW(hwnd, DWLP_USER, lp);
3226 break;
3227 case WM_COMMAND:
3228 switch (HIWORD(wp))
3230 case EN_CHANGE:
3231 if (LOWORD(wp) == IDC_NEW_PURPOSE)
3233 /* Show/hide scroll bar on description depending on how much
3234 * text it has.
3236 HWND description = GetDlgItem(hwnd, IDC_NEW_PURPOSE);
3237 int lines = SendMessageW(description, EM_GETLINECOUNT, 0, 0);
3239 ShowScrollBar(description, SB_VERT, lines > 1);
3241 break;
3242 case BN_CLICKED:
3243 switch (LOWORD(wp))
3245 case IDOK:
3246 SendMessageA(GetDlgItem(hwnd, IDC_NEW_PURPOSE), WM_GETTEXT, ARRAY_SIZE(buf),
3247 (LPARAM)buf);
3248 if (!buf[0])
3250 /* An empty purpose is the same as cancelling */
3251 EndDialog(hwnd, IDCANCEL);
3252 ret = TRUE;
3254 else if (!is_valid_oid(buf))
3256 WCHAR title[MAX_STRING_LEN], error[MAX_STRING_LEN];
3258 LoadStringW(hInstance, IDS_CERTIFICATE_PURPOSE_ERROR, error, ARRAY_SIZE(error));
3259 LoadStringW(hInstance, IDS_CERTIFICATE_PROPERTIES, title, ARRAY_SIZE(title));
3260 MessageBoxW(hwnd, error, title, MB_ICONERROR | MB_OK);
3262 else if (is_oid_in_list(
3263 (HWND)GetWindowLongPtrW(hwnd, DWLP_USER), buf))
3265 WCHAR title[MAX_STRING_LEN], error[MAX_STRING_LEN];
3267 LoadStringW(hInstance, IDS_CERTIFICATE_PURPOSE_EXISTS, error,
3268 ARRAY_SIZE(error));
3269 LoadStringW(hInstance, IDS_CERTIFICATE_PROPERTIES, title, ARRAY_SIZE(title));
3270 MessageBoxW(hwnd, error, title, MB_ICONEXCLAMATION | MB_OK);
3272 else
3274 HWND parent = (HWND)GetWindowLongPtrW(hwnd, DWLP_USER);
3276 add_purpose(parent, buf);
3277 EndDialog(hwnd, wp);
3278 ret = TRUE;
3280 break;
3281 case IDCANCEL:
3282 EndDialog(hwnd, wp);
3283 ret = TRUE;
3284 break;
3286 break;
3288 break;
3290 return ret;
3293 static WCHAR *get_cert_property_as_string(PCCERT_CONTEXT cert, DWORD prop)
3295 WCHAR *name = NULL;
3296 DWORD cb;
3298 if (CertGetCertificateContextProperty(cert, prop, NULL, &cb))
3300 name = HeapAlloc(GetProcessHeap(), 0, cb);
3301 if (name)
3303 if (!CertGetCertificateContextProperty(cert, prop, name, &cb))
3305 HeapFree(GetProcessHeap(), 0, name);
3306 name = NULL;
3310 return name;
3313 static void redraw_states(HWND list, BOOL enabled)
3315 int items = SendMessageW(list, LVM_GETITEMCOUNT, 0, 0), i;
3317 for (i = 0; i < items; i++)
3319 BOOL change = FALSE;
3320 int state;
3322 state = SendMessageW(list, LVM_GETITEMSTATE, i, LVIS_STATEIMAGEMASK);
3323 /* This reverses the INDEXTOSTATEIMAGEMASK shift. There doesn't appear
3324 * to be a handy macro for it.
3326 state >>= 12;
3327 if (enabled)
3329 if (state == CheckBitmapIndexDisabledChecked)
3331 state = CheckBitmapIndexChecked;
3332 change = TRUE;
3334 if (state == CheckBitmapIndexDisabledUnchecked)
3336 state = CheckBitmapIndexUnchecked;
3337 change = TRUE;
3340 else
3342 if (state == CheckBitmapIndexChecked)
3344 state = CheckBitmapIndexDisabledChecked;
3345 change = TRUE;
3347 if (state == CheckBitmapIndexUnchecked)
3349 state = CheckBitmapIndexDisabledUnchecked;
3350 change = TRUE;
3353 if (change)
3355 LVITEMW item;
3357 item.state = INDEXTOSTATEIMAGEMASK(state);
3358 item.stateMask = LVIS_STATEIMAGEMASK;
3359 SendMessageW(list, LVM_SETITEMSTATE, i, (LPARAM)&item);
3364 typedef enum {
3365 PurposeEnableAll = 0,
3366 PurposeDisableAll,
3367 PurposeEnableSelected
3368 } PurposeSelection;
3370 static void select_purposes(HWND hwnd, PurposeSelection selection)
3372 HWND lv = GetDlgItem(hwnd, IDC_CERTIFICATE_USAGES);
3374 switch (selection)
3376 case PurposeEnableAll:
3377 case PurposeDisableAll:
3378 EnableWindow(lv, FALSE);
3379 redraw_states(lv, FALSE);
3380 EnableWindow(GetDlgItem(hwnd, IDC_ADD_PURPOSE), FALSE);
3381 break;
3382 case PurposeEnableSelected:
3383 EnableWindow(lv, TRUE);
3384 redraw_states(lv, TRUE);
3385 EnableWindow(GetDlgItem(hwnd, IDC_ADD_PURPOSE), TRUE);
3389 struct edit_cert_data
3391 PCCERT_CONTEXT cert;
3392 BOOL *pfPropertiesChanged;
3393 HIMAGELIST imageList;
3396 static void show_cert_usages(HWND hwnd, struct edit_cert_data *data)
3398 PCCERT_CONTEXT cert = data->cert;
3399 HWND lv = GetDlgItem(hwnd, IDC_CERTIFICATE_USAGES);
3400 PCERT_ENHKEY_USAGE usage;
3401 DWORD size;
3402 RECT rc;
3403 LVCOLUMNW column;
3404 PurposeSelection purposeSelection = PurposeEnableAll;
3406 GetWindowRect(lv, &rc);
3407 column.mask = LVCF_WIDTH;
3408 column.cx = rc.right - rc.left;
3409 SendMessageW(lv, LVM_INSERTCOLUMNW, 0, (LPARAM)&column);
3410 SendMessageW(lv, LVM_SETIMAGELIST, LVSIL_STATE, (LPARAM)data->imageList);
3412 /* Get enhanced key usage. Have to check for a property and an extension
3413 * separately, because CertGetEnhancedKeyUsage will succeed and return an
3414 * empty usage if neither is set. Unfortunately an empty usage implies
3415 * no usage is allowed, so we have to distinguish between the two cases.
3417 if (CertGetEnhancedKeyUsage(cert, CERT_FIND_PROP_ONLY_ENHKEY_USAGE_FLAG,
3418 NULL, &size))
3420 usage = HeapAlloc(GetProcessHeap(), 0, size);
3421 if (!CertGetEnhancedKeyUsage(cert,
3422 CERT_FIND_PROP_ONLY_ENHKEY_USAGE_FLAG, usage, &size))
3424 HeapFree(GetProcessHeap(), 0, usage);
3425 usage = NULL;
3427 else if (usage->cUsageIdentifier)
3428 purposeSelection = PurposeEnableSelected;
3429 else
3430 purposeSelection = PurposeDisableAll;
3432 else if (CertGetEnhancedKeyUsage(cert, CERT_FIND_EXT_ONLY_ENHKEY_USAGE_FLAG,
3433 NULL, &size))
3435 usage = HeapAlloc(GetProcessHeap(), 0, size);
3436 if (!CertGetEnhancedKeyUsage(cert,
3437 CERT_FIND_EXT_ONLY_ENHKEY_USAGE_FLAG, usage, &size))
3439 HeapFree(GetProcessHeap(), 0, usage);
3440 usage = NULL;
3442 else if (usage->cUsageIdentifier)
3443 purposeSelection = PurposeEnableAll;
3444 else
3445 purposeSelection = PurposeDisableAll;
3447 else
3449 purposeSelection = PurposeEnableAll;
3450 usage = NULL;
3452 if (usage)
3454 DWORD i;
3456 for (i = 0; i < usage->cUsageIdentifier; i++)
3458 PCCRYPT_OID_INFO info = CryptFindOIDInfo(CRYPT_OID_INFO_OID_KEY,
3459 usage->rgpszUsageIdentifier[i], CRYPT_ENHKEY_USAGE_OID_GROUP_ID);
3461 if (info)
3462 add_known_usage(lv, info, CheckBitmapIndexDisabledChecked);
3463 else
3464 add_purpose(hwnd, usage->rgpszUsageIdentifier[i]);
3466 HeapFree(GetProcessHeap(), 0, usage);
3468 else
3469 add_known_usages_to_list(lv, CheckBitmapIndexDisabledChecked);
3470 select_purposes(hwnd, purposeSelection);
3471 SendMessageW(GetDlgItem(hwnd, IDC_ENABLE_ALL_PURPOSES + purposeSelection),
3472 BM_CLICK, 0, 0);
3475 static void set_general_cert_properties(HWND hwnd, struct edit_cert_data *data)
3477 PCCERT_CONTEXT cert = data->cert;
3478 WCHAR *str;
3480 if ((str = get_cert_property_as_string(cert, CERT_FRIENDLY_NAME_PROP_ID)))
3482 SendMessageW(GetDlgItem(hwnd, IDC_FRIENDLY_NAME), WM_SETTEXT, 0,
3483 (LPARAM)str);
3484 HeapFree(GetProcessHeap(), 0, str);
3486 if ((str = get_cert_property_as_string(cert, CERT_DESCRIPTION_PROP_ID)))
3488 SendMessageW(GetDlgItem(hwnd, IDC_DESCRIPTION), WM_SETTEXT, 0,
3489 (LPARAM)str);
3490 HeapFree(GetProcessHeap(), 0, str);
3492 show_cert_usages(hwnd, data);
3495 static void set_cert_string_property(PCCERT_CONTEXT cert, DWORD prop,
3496 LPWSTR str)
3498 if (str && *str)
3500 CRYPT_DATA_BLOB blob;
3502 blob.pbData = (BYTE *)str;
3503 blob.cbData = (lstrlenW(str) + 1) * sizeof(WCHAR);
3504 CertSetCertificateContextProperty(cert, prop, 0, &blob);
3506 else
3507 CertSetCertificateContextProperty(cert, prop, 0, NULL);
3510 #define WM_REFRESH_VIEW WM_USER + 0
3512 static BOOL CALLBACK refresh_propsheet_pages(HWND hwnd, LPARAM lParam)
3514 if ((GetClassLongW(hwnd, GCW_ATOM) == WC_DIALOG))
3515 SendMessageW(hwnd, WM_REFRESH_VIEW, 0, 0);
3516 return TRUE;
3519 #define MAX_FRIENDLY_NAME 40
3520 #define MAX_DESCRIPTION 255
3522 static void apply_general_changes(HWND hwnd)
3524 WCHAR buf[MAX_DESCRIPTION + 1];
3525 struct edit_cert_data *data =
3526 (struct edit_cert_data *)GetWindowLongPtrW(hwnd, DWLP_USER);
3528 SendMessageW(GetDlgItem(hwnd, IDC_FRIENDLY_NAME), WM_GETTEXT, ARRAY_SIZE(buf), (LPARAM)buf);
3529 set_cert_string_property(data->cert, CERT_FRIENDLY_NAME_PROP_ID, buf);
3530 SendMessageW(GetDlgItem(hwnd, IDC_DESCRIPTION), WM_GETTEXT, ARRAY_SIZE(buf), (LPARAM)buf);
3531 set_cert_string_property(data->cert, CERT_DESCRIPTION_PROP_ID, buf);
3532 if (IsDlgButtonChecked(hwnd, IDC_ENABLE_ALL_PURPOSES))
3534 /* Setting a NULL usage removes the enhanced key usage property. */
3535 CertSetEnhancedKeyUsage(data->cert, NULL);
3537 else if (IsDlgButtonChecked(hwnd, IDC_DISABLE_ALL_PURPOSES))
3539 CERT_ENHKEY_USAGE usage = { 0, NULL };
3541 CertSetEnhancedKeyUsage(data->cert, &usage);
3543 else if (IsDlgButtonChecked(hwnd, IDC_ENABLE_SELECTED_PURPOSES))
3545 HWND lv = GetDlgItem(hwnd, IDC_CERTIFICATE_USAGES);
3546 CERT_ENHKEY_USAGE usage = { 0, NULL };
3547 int purposes = SendMessageW(lv, LVM_GETITEMCOUNT, 0, 0), i;
3548 LVITEMW item;
3550 item.mask = LVIF_STATE | LVIF_PARAM;
3551 item.iSubItem = 0;
3552 item.stateMask = LVIS_STATEIMAGEMASK;
3553 for (i = 0; i < purposes; i++)
3555 item.iItem = i;
3556 if (SendMessageW(lv, LVM_GETITEMW, 0, (LPARAM)&item))
3558 int state = item.state >> 12;
3560 if (state == CheckBitmapIndexChecked)
3562 CRYPT_OID_INFO *info = (CRYPT_OID_INFO *)item.lParam;
3564 if (usage.cUsageIdentifier)
3565 usage.rgpszUsageIdentifier =
3566 HeapReAlloc(GetProcessHeap(), 0,
3567 usage.rgpszUsageIdentifier,
3568 (usage.cUsageIdentifier + 1) * sizeof(LPSTR));
3569 else
3570 usage.rgpszUsageIdentifier =
3571 HeapAlloc(GetProcessHeap(), 0, sizeof(LPSTR));
3572 if (usage.rgpszUsageIdentifier)
3573 usage.rgpszUsageIdentifier[usage.cUsageIdentifier++] =
3574 (LPSTR)info->pszOID;
3578 CertSetEnhancedKeyUsage(data->cert, &usage);
3579 HeapFree(GetProcessHeap(), 0, usage.rgpszUsageIdentifier);
3581 EnumChildWindows(GetParent(GetParent(hwnd)), refresh_propsheet_pages, 0);
3582 if (data->pfPropertiesChanged)
3583 *data->pfPropertiesChanged = TRUE;
3586 static LRESULT CALLBACK cert_properties_general_dlg_proc(HWND hwnd, UINT msg,
3587 WPARAM wp, LPARAM lp)
3589 PROPSHEETPAGEW *page;
3591 TRACE("(%p, %08x, %08lx, %08lx)\n", hwnd, msg, wp, lp);
3593 switch (msg)
3595 case WM_INITDIALOG:
3597 HWND description = GetDlgItem(hwnd, IDC_DESCRIPTION);
3598 struct detail_data *detailData;
3599 struct edit_cert_data *editData;
3601 page = (PROPSHEETPAGEW *)lp;
3602 detailData = (struct detail_data *)page->lParam;
3603 SendMessageW(GetDlgItem(hwnd, IDC_FRIENDLY_NAME), EM_SETLIMITTEXT,
3604 MAX_FRIENDLY_NAME, 0);
3605 SendMessageW(description, EM_SETLIMITTEXT, MAX_DESCRIPTION, 0);
3606 ShowScrollBar(description, SB_VERT, FALSE);
3607 editData = HeapAlloc(GetProcessHeap(), 0,
3608 sizeof(struct edit_cert_data));
3609 if (editData)
3611 editData->imageList = ImageList_Create(16, 16,
3612 ILC_COLOR4 | ILC_MASK, 4, 0);
3613 if (editData->imageList)
3615 HBITMAP bmp;
3616 COLORREF backColor = RGB(255, 0, 255);
3618 bmp = LoadBitmapW(hInstance, MAKEINTRESOURCEW(IDB_CHECKS));
3619 ImageList_AddMasked(editData->imageList, bmp, backColor);
3620 DeleteObject(bmp);
3621 ImageList_SetBkColor(editData->imageList, CLR_NONE);
3623 editData->cert = detailData->pCertViewInfo->pCertContext;
3624 editData->pfPropertiesChanged = detailData->pfPropertiesChanged;
3625 SetWindowLongPtrW(hwnd, DWLP_USER, (LPARAM)editData);
3626 set_general_cert_properties(hwnd, editData);
3628 break;
3630 case WM_NOTIFY:
3632 NMHDR *hdr = (NMHDR *)lp;
3633 NMITEMACTIVATE *nm;
3635 switch (hdr->code)
3637 case NM_CLICK:
3638 nm = (NMITEMACTIVATE *)lp;
3639 toggle_usage(hwnd, nm->iItem);
3640 SendMessageW(GetParent(hwnd), PSM_CHANGED, (WPARAM)hwnd, 0);
3641 break;
3642 case PSN_APPLY:
3643 apply_general_changes(hwnd);
3644 break;
3646 break;
3648 case WM_COMMAND:
3649 switch (HIWORD(wp))
3651 case EN_CHANGE:
3652 SendMessageW(GetParent(hwnd), PSM_CHANGED, (WPARAM)hwnd, 0);
3653 if (LOWORD(wp) == IDC_DESCRIPTION)
3655 /* Show/hide scroll bar on description depending on how much
3656 * text it has.
3658 HWND description = GetDlgItem(hwnd, IDC_DESCRIPTION);
3659 int lines = SendMessageW(description, EM_GETLINECOUNT, 0, 0);
3661 ShowScrollBar(description, SB_VERT, lines > 1);
3663 break;
3664 case BN_CLICKED:
3665 switch (LOWORD(wp))
3667 case IDC_ADD_PURPOSE:
3668 if (DialogBoxParamW(hInstance,
3669 MAKEINTRESOURCEW(IDD_ADD_CERT_PURPOSE), hwnd,
3670 add_purpose_dlg_proc, (LPARAM)hwnd) == IDOK)
3671 SendMessageW(GetParent(hwnd), PSM_CHANGED, (WPARAM)hwnd, 0);
3672 break;
3673 case IDC_ENABLE_ALL_PURPOSES:
3674 case IDC_DISABLE_ALL_PURPOSES:
3675 case IDC_ENABLE_SELECTED_PURPOSES:
3676 SendMessageW(GetParent(hwnd), PSM_CHANGED, (WPARAM)hwnd, 0);
3677 select_purposes(hwnd, LOWORD(wp) - IDC_ENABLE_ALL_PURPOSES);
3678 break;
3680 break;
3682 break;
3684 return 0;
3687 static UINT CALLBACK cert_properties_general_callback(HWND hwnd, UINT msg,
3688 PROPSHEETPAGEW *page)
3690 HWND lv;
3691 int cItem, i;
3692 struct edit_cert_data *data;
3694 switch (msg)
3696 case PSPCB_RELEASE:
3697 lv = GetDlgItem(hwnd, IDC_CERTIFICATE_USAGES);
3698 cItem = SendMessageW(lv, LVM_GETITEMCOUNT, 0, 0);
3699 for (i = 0; i < cItem; i++)
3701 LVITEMW item;
3703 item.mask = LVIF_PARAM;
3704 item.iItem = i;
3705 item.iSubItem = 0;
3706 if (SendMessageW(lv, LVM_GETITEMW, 0, (LPARAM)&item) && item.lParam)
3708 PCRYPT_OID_INFO info = (PCRYPT_OID_INFO)item.lParam;
3710 if (info->cbSize == sizeof(CRYPT_OID_INFO) && !info->dwGroupId)
3712 HeapFree(GetProcessHeap(), 0, (LPSTR)info->pszOID);
3713 HeapFree(GetProcessHeap(), 0, info);
3717 data = (struct edit_cert_data *)GetWindowLongPtrW(hwnd, DWLP_USER);
3718 if (data)
3720 ImageList_Destroy(data->imageList);
3721 HeapFree(GetProcessHeap(), 0, data);
3723 break;
3725 return 1;
3728 static void show_edit_cert_properties_dialog(HWND parent,
3729 struct detail_data *data)
3731 PROPSHEETHEADERW hdr;
3732 PROPSHEETPAGEW page; /* FIXME: need to add a cross-certificate page */
3734 TRACE("(%p)\n", data);
3736 memset(&page, 0, sizeof(PROPSHEETPAGEW));
3737 page.dwSize = sizeof(page);
3738 page.dwFlags = PSP_USECALLBACK;
3739 page.pfnCallback = cert_properties_general_callback;
3740 page.hInstance = hInstance;
3741 page.u.pszTemplate = MAKEINTRESOURCEW(IDD_CERT_PROPERTIES_GENERAL);
3742 page.pfnDlgProc = cert_properties_general_dlg_proc;
3743 page.lParam = (LPARAM)data;
3745 memset(&hdr, 0, sizeof(hdr));
3746 hdr.dwSize = sizeof(hdr);
3747 hdr.hwndParent = parent;
3748 hdr.dwFlags = PSH_PROPSHEETPAGE;
3749 hdr.hInstance = hInstance;
3750 hdr.pszCaption = MAKEINTRESOURCEW(IDS_CERTIFICATE_PROPERTIES);
3751 hdr.u3.ppsp = &page;
3752 hdr.nPages = 1;
3753 PropertySheetW(&hdr);
3756 static void free_detail_fields(struct detail_data *data)
3758 int i;
3760 for (i = 0; i < data->cFields; i++)
3761 HeapFree(GetProcessHeap(), 0, data->fields[i].detailed_value);
3762 HeapFree(GetProcessHeap(), 0, data->fields);
3763 data->fields = NULL;
3764 data->cFields = 0;
3767 static void refresh_details_view(HWND hwnd)
3769 HWND cb = GetDlgItem(hwnd, IDC_DETAIL_SELECT);
3770 int curSel;
3771 struct detail_data *data;
3773 curSel = SendMessageW(cb, CB_GETCURSEL, 0, 0);
3774 /* Actually, any index will do, since they all store the same data value */
3775 data = (struct detail_data *)SendMessageW(cb, CB_GETITEMDATA, curSel, 0);
3776 free_detail_fields(data);
3777 set_fields_selection(hwnd, data, curSel);
3780 static LRESULT CALLBACK detail_dlg_proc(HWND hwnd, UINT msg, WPARAM wp,
3781 LPARAM lp)
3783 PROPSHEETPAGEW *page;
3784 struct detail_data *data;
3786 TRACE("(%p, %08x, %08lx, %08lx)\n", hwnd, msg, wp, lp);
3788 switch (msg)
3790 case WM_INITDIALOG:
3791 page = (PROPSHEETPAGEW *)lp;
3792 data = (struct detail_data *)page->lParam;
3793 create_cert_details_list(hwnd, data);
3794 if (!(data->pCertViewInfo->dwFlags & CRYPTUI_ENABLE_EDITPROPERTIES))
3795 EnableWindow(GetDlgItem(hwnd, IDC_EDITPROPERTIES), FALSE);
3796 if (data->pCertViewInfo->dwFlags & CRYPTUI_DISABLE_EXPORT)
3797 EnableWindow(GetDlgItem(hwnd, IDC_EXPORT), FALSE);
3798 break;
3799 case WM_NOTIFY:
3801 NMITEMACTIVATE *nm;
3802 HWND list = GetDlgItem(hwnd, IDC_DETAIL_LIST);
3804 nm = (NMITEMACTIVATE*)lp;
3805 if (nm->hdr.hwndFrom == list && nm->uNewState & LVN_ITEMACTIVATE
3806 && nm->hdr.code == LVN_ITEMCHANGED)
3808 data = (struct detail_data *)nm->lParam;
3809 if (nm->iItem >= 0 && data && nm->iItem < data->cFields)
3811 WCHAR buf[MAX_STRING_LEN], *val = NULL;
3812 HWND valueCtl = GetDlgItem(hwnd, IDC_DETAIL_VALUE);
3814 if (data->fields[nm->iItem].create)
3815 val = data->fields[nm->iItem].create(
3816 data->pCertViewInfo->pCertContext,
3817 data->fields[nm->iItem].param);
3818 else
3820 LVITEMW item;
3821 int res;
3823 item.cchTextMax = ARRAY_SIZE(buf);
3824 item.mask = LVIF_TEXT;
3825 item.pszText = buf;
3826 item.iItem = nm->iItem;
3827 item.iSubItem = 1;
3828 res = SendMessageW(list, LVM_GETITEMW, 0, (LPARAM)&item);
3829 if (res)
3830 val = buf;
3832 /* Select all the text in the control, the next update will
3833 * replace it
3835 SendMessageW(valueCtl, EM_SETSEL, 0, -1);
3836 add_unformatted_text_to_control(valueCtl, val,
3837 val ? lstrlenW(val) : 0);
3838 if (val != buf)
3839 HeapFree(GetProcessHeap(), 0, val);
3842 break;
3844 case WM_COMMAND:
3845 switch (wp)
3847 case IDC_EXPORT:
3849 HWND cb = GetDlgItem(hwnd, IDC_DETAIL_SELECT);
3850 CRYPTUI_WIZ_EXPORT_INFO info;
3852 data = (struct detail_data *)SendMessageW(cb, CB_GETITEMDATA, 0, 0);
3853 info.dwSize = sizeof(info);
3854 info.pwszExportFileName = NULL;
3855 info.dwSubjectChoice = CRYPTUI_WIZ_EXPORT_CERT_CONTEXT;
3856 info.u.pCertContext = data->pCertViewInfo->pCertContext;
3857 info.cStores = 0;
3858 CryptUIWizExport(0, hwnd, NULL, &info, NULL);
3859 break;
3861 case IDC_EDITPROPERTIES:
3863 HWND cb = GetDlgItem(hwnd, IDC_DETAIL_SELECT);
3864 int curSel;
3866 curSel = SendMessageW(cb, CB_GETCURSEL, 0, 0);
3867 /* Actually, any index will do, since they all store the same
3868 * data value
3870 data = (struct detail_data *)SendMessageW(cb, CB_GETITEMDATA,
3871 curSel, 0);
3872 show_edit_cert_properties_dialog(GetParent(hwnd), data);
3873 break;
3875 case ((CBN_SELCHANGE << 16) | IDC_DETAIL_SELECT):
3876 refresh_details_view(hwnd);
3877 break;
3879 break;
3880 case WM_REFRESH_VIEW:
3881 refresh_details_view(hwnd);
3882 break;
3884 return 0;
3887 static UINT CALLBACK detail_callback(HWND hwnd, UINT msg,
3888 PROPSHEETPAGEW *page)
3890 struct detail_data *data;
3892 switch (msg)
3894 case PSPCB_RELEASE:
3895 data = (struct detail_data *)page->lParam;
3896 free_detail_fields(data);
3897 HeapFree(GetProcessHeap(), 0, data);
3898 break;
3900 return 0;
3903 static BOOL init_detail_page(PCCRYPTUI_VIEWCERTIFICATE_STRUCTW pCertViewInfo,
3904 BOOL *pfPropertiesChanged, PROPSHEETPAGEW *page)
3906 BOOL ret;
3907 struct detail_data *data = HeapAlloc(GetProcessHeap(), 0,
3908 sizeof(struct detail_data));
3910 if (data)
3912 data->pCertViewInfo = pCertViewInfo;
3913 data->pfPropertiesChanged = pfPropertiesChanged;
3914 data->cFields = 0;
3915 data->fields = NULL;
3916 memset(page, 0, sizeof(PROPSHEETPAGEW));
3917 page->dwSize = sizeof(PROPSHEETPAGEW);
3918 page->dwFlags = PSP_USECALLBACK;
3919 page->pfnCallback = detail_callback;
3920 page->hInstance = hInstance;
3921 page->u.pszTemplate = MAKEINTRESOURCEW(IDD_DETAIL);
3922 page->pfnDlgProc = detail_dlg_proc;
3923 page->lParam = (LPARAM)data;
3924 ret = TRUE;
3926 else
3927 ret = FALSE;
3928 return ret;
3931 struct hierarchy_data
3933 PCCRYPTUI_VIEWCERTIFICATE_STRUCTW pCertViewInfo;
3934 HIMAGELIST imageList;
3935 DWORD selectedCert;
3938 static LPARAM index_to_lparam(struct hierarchy_data *data, DWORD index)
3940 CRYPT_PROVIDER_SGNR *provSigner = WTHelperGetProvSignerFromChain(
3941 (CRYPT_PROVIDER_DATA *)data->pCertViewInfo->u.pCryptProviderData,
3942 data->pCertViewInfo->idxSigner, data->pCertViewInfo->fCounterSigner,
3943 data->pCertViewInfo->idxCounterSigner);
3945 /* Takes advantage of the fact that a pointer is 32-bit aligned, and
3946 * therefore always even.
3948 if (index == provSigner->csCertChain - 1)
3949 return (LPARAM)data;
3950 return index << 1 | 1;
3953 static inline DWORD lparam_to_index(struct hierarchy_data *data, LPARAM lp)
3955 CRYPT_PROVIDER_SGNR *provSigner = WTHelperGetProvSignerFromChain(
3956 (CRYPT_PROVIDER_DATA *)data->pCertViewInfo->u.pCryptProviderData,
3957 data->pCertViewInfo->idxSigner, data->pCertViewInfo->fCounterSigner,
3958 data->pCertViewInfo->idxCounterSigner);
3960 if (!(lp & 1))
3961 return provSigner->csCertChain - 1;
3962 return lp >> 1;
3965 static struct hierarchy_data *get_hierarchy_data_from_tree_item(HWND tree,
3966 HTREEITEM hItem)
3968 struct hierarchy_data *data = NULL;
3969 HTREEITEM root = NULL;
3971 do {
3972 HTREEITEM parent = (HTREEITEM)SendMessageW(tree, TVM_GETNEXTITEM,
3973 TVGN_PARENT, (LPARAM)hItem);
3975 if (!parent)
3976 root = hItem;
3977 hItem = parent;
3978 } while (hItem);
3979 if (root)
3981 TVITEMW item;
3983 item.mask = TVIF_PARAM;
3984 item.hItem = root;
3985 SendMessageW(tree, TVM_GETITEMW, 0, (LPARAM)&item);
3986 data = (struct hierarchy_data *)item.lParam;
3988 return data;
3991 static WCHAR *get_cert_display_name(PCCERT_CONTEXT cert)
3993 WCHAR *name = get_cert_property_as_string(cert, CERT_FRIENDLY_NAME_PROP_ID);
3995 if (!name)
3996 name = get_cert_name_string(cert, CERT_NAME_SIMPLE_DISPLAY_TYPE, 0);
3997 return name;
4000 static void show_cert_chain(HWND hwnd, struct hierarchy_data *data)
4002 HWND tree = GetDlgItem(hwnd, IDC_CERTPATH);
4003 CRYPT_PROVIDER_SGNR *provSigner = WTHelperGetProvSignerFromChain(
4004 (CRYPT_PROVIDER_DATA *)data->pCertViewInfo->u.pCryptProviderData,
4005 data->pCertViewInfo->idxSigner, data->pCertViewInfo->fCounterSigner,
4006 data->pCertViewInfo->idxCounterSigner);
4007 DWORD i;
4008 HTREEITEM parent = NULL;
4010 SendMessageW(tree, TVM_SETIMAGELIST, TVSIL_NORMAL, (LPARAM)data->imageList);
4011 for (i = provSigner->csCertChain; i; i--)
4013 LPWSTR name;
4015 name = get_cert_display_name(provSigner->pasCertChain[i - 1].pCert);
4016 if (name)
4018 TVINSERTSTRUCTW tvis;
4020 tvis.hParent = parent;
4021 tvis.hInsertAfter = TVI_LAST;
4022 tvis.u.item.mask = TVIF_TEXT | TVIF_STATE | TVIF_IMAGE |
4023 TVIF_SELECTEDIMAGE | TVIF_PARAM;
4024 tvis.u.item.pszText = name;
4025 tvis.u.item.state = TVIS_EXPANDED;
4026 tvis.u.item.stateMask = TVIS_EXPANDED;
4027 if (i == 1 && (!provSigner->pChainContext ||
4028 provSigner->pChainContext->TrustStatus.dwErrorStatus &
4029 CERT_TRUST_IS_PARTIAL_CHAIN))
4031 /* The root of the chain has a special case: if the chain is
4032 * a partial chain, the icon is a warning icon rather than an
4033 * error icon.
4035 tvis.u.item.iImage = 2;
4037 else if (provSigner->pasCertChain[i - 1].pChainElement->TrustStatus.
4038 dwErrorStatus == 0)
4039 tvis.u.item.iImage = 0;
4040 else
4041 tvis.u.item.iImage = 1;
4042 tvis.u.item.iSelectedImage = tvis.u.item.iImage;
4043 tvis.u.item.lParam = index_to_lparam(data, i - 1);
4044 parent = (HTREEITEM)SendMessageW(tree, TVM_INSERTITEMW, 0,
4045 (LPARAM)&tvis);
4046 HeapFree(GetProcessHeap(), 0, name);
4051 static void set_certificate_status(HWND hwnd, const CRYPT_PROVIDER_CERT *cert)
4053 /* Select all the text in the control, the next update will replace it */
4054 SendMessageW(hwnd, EM_SETSEL, 0, -1);
4055 /* Set the highest priority error messages first. */
4056 if (!(cert->dwConfidence & CERT_CONFIDENCE_SIG))
4057 add_string_resource_to_control(hwnd, IDS_CERTIFICATE_BAD_SIGNATURE);
4058 else if (!(cert->dwConfidence & CERT_CONFIDENCE_TIME))
4059 add_string_resource_to_control(hwnd, IDS_CERTIFICATE_BAD_TIME);
4060 else if (!(cert->dwConfidence & CERT_CONFIDENCE_TIMENEST))
4061 add_string_resource_to_control(hwnd, IDS_CERTIFICATE_BAD_TIMENEST);
4062 else if (cert->dwRevokedReason)
4063 add_string_resource_to_control(hwnd, IDS_CERTIFICATE_REVOKED);
4064 else
4065 add_string_resource_to_control(hwnd, IDS_CERTIFICATE_VALID);
4068 static void set_certificate_status_for_end_cert(HWND hwnd,
4069 PCCRYPTUI_VIEWCERTIFICATE_STRUCTW pCertViewInfo)
4071 HWND status = GetDlgItem(hwnd, IDC_CERTIFICATESTATUSTEXT);
4072 CRYPT_PROVIDER_SGNR *provSigner = WTHelperGetProvSignerFromChain(
4073 (CRYPT_PROVIDER_DATA *)pCertViewInfo->u.pCryptProviderData,
4074 pCertViewInfo->idxSigner, pCertViewInfo->fCounterSigner,
4075 pCertViewInfo->idxCounterSigner);
4076 CRYPT_PROVIDER_CERT *provCert = WTHelperGetProvCertFromChain(provSigner,
4077 pCertViewInfo->idxCert);
4079 set_certificate_status(status, provCert);
4082 static void show_cert_hierarchy(HWND hwnd, struct hierarchy_data *data)
4084 /* Disable view certificate button until a certificate is selected */
4085 EnableWindow(GetDlgItem(hwnd, IDC_VIEWCERTIFICATE), FALSE);
4086 show_cert_chain(hwnd, data);
4087 set_certificate_status_for_end_cert(hwnd, data->pCertViewInfo);
4090 static void show_dialog_for_selected_cert(HWND hwnd)
4092 HWND tree = GetDlgItem(hwnd, IDC_CERTPATH);
4093 TVITEMW item;
4094 struct hierarchy_data *data;
4095 DWORD selection;
4097 memset(&item, 0, sizeof(item));
4098 item.mask = TVIF_HANDLE | TVIF_PARAM;
4099 item.hItem = (HTREEITEM)SendMessageW(tree, TVM_GETNEXTITEM, TVGN_CARET, 0);
4100 SendMessageW(tree, TVM_GETITEMW, 0, (LPARAM)&item);
4101 data = get_hierarchy_data_from_tree_item(tree, item.hItem);
4102 selection = lparam_to_index(data, item.lParam);
4103 if (selection != 0)
4105 CRYPT_PROVIDER_SGNR *provSigner;
4106 CRYPTUI_VIEWCERTIFICATE_STRUCTW viewInfo;
4107 BOOL changed = FALSE;
4109 provSigner = WTHelperGetProvSignerFromChain(
4110 (CRYPT_PROVIDER_DATA *)data->pCertViewInfo->u.pCryptProviderData,
4111 data->pCertViewInfo->idxSigner,
4112 data->pCertViewInfo->fCounterSigner,
4113 data->pCertViewInfo->idxCounterSigner);
4114 memset(&viewInfo, 0, sizeof(viewInfo));
4115 viewInfo.dwSize = sizeof(viewInfo);
4116 viewInfo.dwFlags = data->pCertViewInfo->dwFlags;
4117 viewInfo.szTitle = data->pCertViewInfo->szTitle;
4118 viewInfo.pCertContext = provSigner->pasCertChain[selection].pCert;
4119 viewInfo.cStores = data->pCertViewInfo->cStores;
4120 viewInfo.rghStores = data->pCertViewInfo->rghStores;
4121 viewInfo.cPropSheetPages = data->pCertViewInfo->cPropSheetPages;
4122 viewInfo.rgPropSheetPages = data->pCertViewInfo->rgPropSheetPages;
4123 viewInfo.nStartPage = data->pCertViewInfo->nStartPage;
4124 CryptUIDlgViewCertificateW(&viewInfo, &changed);
4125 if (changed)
4127 /* Delete the contents of the tree */
4128 SendMessageW(tree, TVM_DELETEITEM, 0, (LPARAM)TVI_ROOT);
4129 /* Reinitialize the tree */
4130 show_cert_hierarchy(hwnd, data);
4135 static LRESULT CALLBACK hierarchy_dlg_proc(HWND hwnd, UINT msg, WPARAM wp,
4136 LPARAM lp)
4138 PROPSHEETPAGEW *page;
4139 struct hierarchy_data *data;
4140 LRESULT ret = 0;
4141 HWND tree = GetDlgItem(hwnd, IDC_CERTPATH);
4143 TRACE("(%p, %08x, %08lx, %08lx)\n", hwnd, msg, wp, lp);
4145 switch (msg)
4147 case WM_INITDIALOG:
4148 page = (PROPSHEETPAGEW *)lp;
4149 data = (struct hierarchy_data *)page->lParam;
4150 show_cert_hierarchy(hwnd, data);
4151 break;
4152 case WM_NOTIFY:
4154 NMHDR *hdr;
4156 hdr = (NMHDR *)lp;
4157 switch (hdr->code)
4159 case TVN_SELCHANGEDW:
4161 NMTREEVIEWW *nm = (NMTREEVIEWW*)lp;
4162 DWORD selection;
4163 CRYPT_PROVIDER_SGNR *provSigner;
4165 data = get_hierarchy_data_from_tree_item(tree, nm->itemNew.hItem);
4166 selection = lparam_to_index(data, nm->itemNew.lParam);
4167 provSigner = WTHelperGetProvSignerFromChain(
4168 (CRYPT_PROVIDER_DATA *)data->pCertViewInfo->u.pCryptProviderData,
4169 data->pCertViewInfo->idxSigner,
4170 data->pCertViewInfo->fCounterSigner,
4171 data->pCertViewInfo->idxCounterSigner);
4172 EnableWindow(GetDlgItem(hwnd, IDC_VIEWCERTIFICATE), selection != 0);
4173 set_certificate_status(GetDlgItem(hwnd, IDC_CERTIFICATESTATUSTEXT),
4174 &provSigner->pasCertChain[selection]);
4175 break;
4177 case NM_DBLCLK:
4178 show_dialog_for_selected_cert(hwnd);
4179 SetWindowLongPtrW(hwnd, DWLP_MSGRESULT, 1);
4180 ret = 1;
4181 break;
4183 break;
4185 case WM_COMMAND:
4186 switch (wp)
4188 case IDC_VIEWCERTIFICATE:
4189 show_dialog_for_selected_cert(hwnd);
4190 break;
4192 break;
4193 case WM_REFRESH_VIEW:
4195 TVITEMW item;
4197 /* Get hierarchy data */
4198 memset(&item, 0, sizeof(item));
4199 item.mask = TVIF_HANDLE | TVIF_PARAM;
4200 item.hItem = (HTREEITEM)SendMessageW(tree, TVM_GETNEXTITEM, TVGN_ROOT,
4202 data = get_hierarchy_data_from_tree_item(tree, item.hItem);
4203 /* Delete the contents of the tree */
4204 SendMessageW(tree, TVM_DELETEITEM, 0, (LPARAM)TVI_ROOT);
4205 /* Reinitialize the tree */
4206 show_cert_hierarchy(hwnd, data);
4207 break;
4210 return ret;
4213 static UINT CALLBACK hierarchy_callback(HWND hwnd, UINT msg,
4214 PROPSHEETPAGEW *page)
4216 struct hierarchy_data *data;
4218 switch (msg)
4220 case PSPCB_RELEASE:
4221 data = (struct hierarchy_data *)page->lParam;
4222 ImageList_Destroy(data->imageList);
4223 HeapFree(GetProcessHeap(), 0, data);
4224 break;
4226 return 0;
4229 static BOOL init_hierarchy_page(PCCRYPTUI_VIEWCERTIFICATE_STRUCTW pCertViewInfo,
4230 PROPSHEETPAGEW *page)
4232 struct hierarchy_data *data = HeapAlloc(GetProcessHeap(), 0,
4233 sizeof(struct hierarchy_data));
4234 BOOL ret = FALSE;
4236 if (data)
4238 data->imageList = ImageList_Create(16, 16, ILC_COLOR4 | ILC_MASK, 2, 0);
4239 if (data->imageList)
4241 HBITMAP bmp;
4242 COLORREF backColor = RGB(255, 0, 255);
4244 data->pCertViewInfo = pCertViewInfo;
4245 data->selectedCert = 0xffffffff;
4247 bmp = LoadBitmapW(hInstance, MAKEINTRESOURCEW(IDB_SMALL_ICONS));
4248 ImageList_AddMasked(data->imageList, bmp, backColor);
4249 DeleteObject(bmp);
4250 ImageList_SetBkColor(data->imageList, CLR_NONE);
4252 memset(page, 0, sizeof(PROPSHEETPAGEW));
4253 page->dwSize = sizeof(PROPSHEETPAGEW);
4254 page->dwFlags = PSP_USECALLBACK;
4255 page->hInstance = hInstance;
4256 page->u.pszTemplate = MAKEINTRESOURCEW(IDD_HIERARCHY);
4257 page->pfnDlgProc = hierarchy_dlg_proc;
4258 page->lParam = (LPARAM)data;
4259 page->pfnCallback = hierarchy_callback;
4260 ret = TRUE;
4262 else
4263 HeapFree(GetProcessHeap(), 0, data);
4265 return ret;
4268 static int CALLBACK cert_prop_sheet_proc(HWND hwnd, UINT msg, LPARAM lp)
4270 RECT rc;
4272 TRACE("(%p, %08x, %08lx)\n", hwnd, msg, lp);
4274 switch (msg)
4276 case PSCB_INITIALIZED:
4277 /* Get cancel button's position.. */
4278 GetWindowRect(GetDlgItem(hwnd, IDCANCEL), &rc);
4279 MapWindowPoints( 0, hwnd, (POINT *)&rc, 2 );
4280 /* hide the cancel button.. */
4281 ShowWindow(GetDlgItem(hwnd, IDCANCEL), FALSE);
4282 /* and move the OK button to the cancel button's original position. */
4283 SetWindowPos(GetDlgItem(hwnd, IDOK), 0, rc.left, rc.top, 0, 0,
4284 SWP_NOSIZE | SWP_NOZORDER | SWP_NOACTIVATE | SWP_NOREDRAW );
4285 break;
4287 return 0;
4290 static BOOL show_cert_dialog(PCCRYPTUI_VIEWCERTIFICATE_STRUCTW pCertViewInfo,
4291 CRYPT_PROVIDER_CERT *provCert, BOOL *pfPropertiesChanged)
4293 static const WCHAR riched[] = { 'r','i','c','h','e','d','2','0',0 };
4294 DWORD nPages;
4295 PROPSHEETPAGEW *pages;
4296 BOOL ret = FALSE;
4297 HMODULE lib = LoadLibraryW(riched);
4299 nPages = pCertViewInfo->cPropSheetPages + 1; /* one for the General tab */
4300 if (!(pCertViewInfo->dwFlags & CRYPTUI_HIDE_DETAILPAGE))
4301 nPages++;
4302 if (!(pCertViewInfo->dwFlags & CRYPTUI_HIDE_HIERARCHYPAGE))
4303 nPages++;
4304 pages = HeapAlloc(GetProcessHeap(), 0, nPages * sizeof(PROPSHEETPAGEW));
4305 if (pages)
4307 PROPSHEETHEADERW hdr;
4308 CRYPTUI_INITDIALOG_STRUCT *init = NULL;
4309 DWORD i;
4311 memset(&hdr, 0, sizeof(hdr));
4312 hdr.dwSize = sizeof(hdr);
4313 hdr.dwFlags = PSH_NOAPPLYNOW | PSH_PROPSHEETPAGE | PSH_USECALLBACK;
4314 hdr.hInstance = hInstance;
4315 if (pCertViewInfo->szTitle)
4316 hdr.pszCaption = pCertViewInfo->szTitle;
4317 else
4318 hdr.pszCaption = MAKEINTRESOURCEW(IDS_CERTIFICATE);
4319 init_general_page(pCertViewInfo, &pages[hdr.nPages++]);
4320 if (!(pCertViewInfo->dwFlags & CRYPTUI_HIDE_DETAILPAGE))
4322 if (init_detail_page(pCertViewInfo, pfPropertiesChanged,
4323 &pages[hdr.nPages]))
4324 hdr.nPages++;
4326 if (!(pCertViewInfo->dwFlags & CRYPTUI_HIDE_HIERARCHYPAGE))
4328 if (init_hierarchy_page(pCertViewInfo, &pages[hdr.nPages]))
4329 hdr.nPages++;
4331 /* Copy each additional page, and create the init dialog struct for it
4333 if (pCertViewInfo->cPropSheetPages)
4335 init = HeapAlloc(GetProcessHeap(), 0,
4336 pCertViewInfo->cPropSheetPages *
4337 sizeof(CRYPTUI_INITDIALOG_STRUCT));
4338 if (init)
4340 for (i = 0; i < pCertViewInfo->cPropSheetPages; i++)
4342 memcpy(&pages[hdr.nPages + i],
4343 &pCertViewInfo->rgPropSheetPages[i],
4344 sizeof(PROPSHEETPAGEW));
4345 init[i].lParam = pCertViewInfo->rgPropSheetPages[i].lParam;
4346 init[i].pCertContext = pCertViewInfo->pCertContext;
4347 pages[hdr.nPages + i].lParam = (LPARAM)&init[i];
4349 if (pCertViewInfo->nStartPage & 0x8000)
4351 /* Start page index is relative to the number of default
4352 * pages
4354 hdr.u2.nStartPage = pCertViewInfo->nStartPage + hdr.nPages;
4356 else
4357 hdr.u2.nStartPage = pCertViewInfo->nStartPage;
4358 hdr.nPages = nPages;
4359 ret = TRUE;
4361 else
4362 SetLastError(ERROR_OUTOFMEMORY);
4364 else
4366 /* Ignore the relative flag if there aren't any additional pages */
4367 hdr.u2.nStartPage = pCertViewInfo->nStartPage & 0x7fff;
4368 ret = TRUE;
4370 if (ret)
4372 INT_PTR l;
4374 hdr.u3.ppsp = pages;
4375 hdr.pfnCallback = cert_prop_sheet_proc;
4376 l = PropertySheetW(&hdr);
4377 if (l == 0)
4379 SetLastError(ERROR_CANCELLED);
4380 ret = FALSE;
4383 HeapFree(GetProcessHeap(), 0, init);
4384 HeapFree(GetProcessHeap(), 0, pages);
4386 else
4387 SetLastError(ERROR_OUTOFMEMORY);
4388 FreeLibrary(lib);
4389 return ret;
4392 /***********************************************************************
4393 * CryptUIDlgViewCertificateW (CRYPTUI.@)
4395 BOOL WINAPI CryptUIDlgViewCertificateW(
4396 PCCRYPTUI_VIEWCERTIFICATE_STRUCTW pCertViewInfo, BOOL *pfPropertiesChanged)
4398 static GUID generic_cert_verify = WINTRUST_ACTION_GENERIC_CERT_VERIFY;
4399 CRYPTUI_VIEWCERTIFICATE_STRUCTW viewInfo;
4400 WINTRUST_DATA wvt;
4401 WINTRUST_CERT_INFO cert;
4402 BOOL ret = FALSE;
4403 CRYPT_PROVIDER_SGNR *signer;
4404 CRYPT_PROVIDER_CERT *provCert = NULL;
4406 TRACE("(%p, %p)\n", pCertViewInfo, pfPropertiesChanged);
4408 if (pCertViewInfo->dwSize != sizeof(CRYPTUI_VIEWCERTIFICATE_STRUCTW))
4410 SetLastError(ERROR_INVALID_PARAMETER);
4411 return FALSE;
4413 /* Make a local copy in case we have to call WinVerifyTrust ourselves */
4414 memcpy(&viewInfo, pCertViewInfo, sizeof(viewInfo));
4415 if (!pCertViewInfo->u.hWVTStateData)
4417 memset(&wvt, 0, sizeof(wvt));
4418 wvt.cbStruct = sizeof(wvt);
4419 wvt.dwUIChoice = WTD_UI_NONE;
4420 if (viewInfo.dwFlags &
4421 CRYPTUI_ENABLE_REVOCATION_CHECK_CHAIN_EXCLUDE_ROOT)
4422 wvt.fdwRevocationChecks |= WTD_REVOCATION_CHECK_CHAIN_EXCLUDE_ROOT;
4423 if (viewInfo.dwFlags & CRYPTUI_ENABLE_REVOCATION_CHECK_END_CERT)
4424 wvt.fdwRevocationChecks |= WTD_REVOCATION_CHECK_END_CERT;
4425 if (viewInfo.dwFlags & CRYPTUI_ENABLE_REVOCATION_CHECK_CHAIN)
4426 wvt.fdwRevocationChecks |= WTD_REVOCATION_CHECK_CHAIN;
4427 wvt.dwUnionChoice = WTD_CHOICE_CERT;
4428 memset(&cert, 0, sizeof(cert));
4429 cert.cbStruct = sizeof(cert);
4430 cert.psCertContext = (CERT_CONTEXT *)viewInfo.pCertContext;
4431 cert.chStores = viewInfo.cStores;
4432 cert.pahStores = viewInfo.rghStores;
4433 wvt.u.pCert = &cert;
4434 wvt.dwStateAction = WTD_STATEACTION_VERIFY;
4435 WinVerifyTrust(NULL, &generic_cert_verify, &wvt);
4436 viewInfo.u.pCryptProviderData =
4437 WTHelperProvDataFromStateData(wvt.hWVTStateData);
4438 signer = WTHelperGetProvSignerFromChain(
4439 (CRYPT_PROVIDER_DATA *)viewInfo.u.pCryptProviderData, 0, FALSE, 0);
4440 provCert = WTHelperGetProvCertFromChain(signer, 0);
4441 ret = TRUE;
4443 else
4445 viewInfo.u.pCryptProviderData =
4446 WTHelperProvDataFromStateData(viewInfo.u.hWVTStateData);
4447 signer = WTHelperGetProvSignerFromChain(
4448 (CRYPT_PROVIDER_DATA *)viewInfo.u.pCryptProviderData,
4449 viewInfo.idxSigner, viewInfo.fCounterSigner,
4450 viewInfo.idxCounterSigner);
4451 provCert = WTHelperGetProvCertFromChain(signer, viewInfo.idxCert);
4452 ret = TRUE;
4454 if (ret)
4456 ret = show_cert_dialog(&viewInfo, provCert, pfPropertiesChanged);
4457 if (!pCertViewInfo->u.hWVTStateData)
4459 wvt.dwStateAction = WTD_STATEACTION_CLOSE;
4460 WinVerifyTrust(NULL, &generic_cert_verify, &wvt);
4463 return ret;
4466 /***********************************************************************
4467 * CryptUIDlgViewContext (CRYPTUI.@)
4469 BOOL WINAPI CryptUIDlgViewContext(DWORD dwContextType, LPVOID pvContext,
4470 HWND hwnd, LPCWSTR pwszTitle, DWORD dwFlags, LPVOID pvReserved)
4472 BOOL ret;
4474 TRACE("(%d, %p, %p, %s, %08x, %p)\n", dwContextType, pvContext, hwnd,
4475 debugstr_w(pwszTitle), dwFlags, pvReserved);
4477 switch (dwContextType)
4479 case CERT_STORE_CERTIFICATE_CONTEXT:
4481 CRYPTUI_VIEWCERTIFICATE_STRUCTW viewInfo;
4483 memset(&viewInfo, 0, sizeof(viewInfo));
4484 viewInfo.dwSize = sizeof(viewInfo);
4485 viewInfo.hwndParent = hwnd;
4486 viewInfo.szTitle = pwszTitle;
4487 viewInfo.pCertContext = pvContext;
4488 ret = CryptUIDlgViewCertificateW(&viewInfo, NULL);
4489 break;
4491 default:
4492 FIXME("unimplemented for context type %d\n", dwContextType);
4493 SetLastError(E_INVALIDARG);
4494 ret = FALSE;
4496 return ret;
4499 /* Decodes a cert's basic constraints extension (either szOID_BASIC_CONSTRAINTS
4500 * or szOID_BASIC_CONSTRAINTS2, whichever is present) to determine if it
4501 * should be a CA. If neither extension is present, returns
4502 * defaultIfNotSpecified.
4504 static BOOL is_ca_cert(PCCERT_CONTEXT cert, BOOL defaultIfNotSpecified)
4506 BOOL isCA = defaultIfNotSpecified;
4507 PCERT_EXTENSION ext = CertFindExtension(szOID_BASIC_CONSTRAINTS,
4508 cert->pCertInfo->cExtension, cert->pCertInfo->rgExtension);
4510 if (ext)
4512 CERT_BASIC_CONSTRAINTS_INFO *info;
4513 DWORD size = 0;
4515 if (CryptDecodeObjectEx(X509_ASN_ENCODING, szOID_BASIC_CONSTRAINTS,
4516 ext->Value.pbData, ext->Value.cbData, CRYPT_DECODE_ALLOC_FLAG,
4517 NULL, &info, &size))
4519 if (info->SubjectType.cbData == 1)
4520 isCA = info->SubjectType.pbData[0] & CERT_CA_SUBJECT_FLAG;
4521 LocalFree(info);
4524 else
4526 ext = CertFindExtension(szOID_BASIC_CONSTRAINTS2,
4527 cert->pCertInfo->cExtension, cert->pCertInfo->rgExtension);
4528 if (ext)
4530 CERT_BASIC_CONSTRAINTS2_INFO info;
4531 DWORD size = sizeof(CERT_BASIC_CONSTRAINTS2_INFO);
4533 if (CryptDecodeObjectEx(X509_ASN_ENCODING,
4534 szOID_BASIC_CONSTRAINTS2, ext->Value.pbData, ext->Value.cbData,
4535 0, NULL, &info, &size))
4536 isCA = info.fCA;
4539 return isCA;
4542 static HCERTSTORE choose_store_for_cert(PCCERT_CONTEXT cert)
4544 LPCWSTR storeName;
4546 if (is_ca_cert(cert, TRUE))
4547 storeName = ca;
4548 else
4549 storeName = addressBook;
4550 return CertOpenStore(CERT_STORE_PROV_SYSTEM_W, 0, 0,
4551 CERT_SYSTEM_STORE_CURRENT_USER, storeName);
4554 static BOOL import_cert(PCCERT_CONTEXT cert, HCERTSTORE hDestCertStore)
4556 HCERTSTORE store;
4557 BOOL ret;
4559 if (!cert)
4561 SetLastError(E_INVALIDARG);
4562 return FALSE;
4564 if (hDestCertStore) store = hDestCertStore;
4565 else
4567 if (!(store = choose_store_for_cert(cert)))
4569 WARN("unable to open certificate store\n");
4570 return FALSE;
4573 ret = CertAddCertificateContextToStore(store, cert,
4574 CERT_STORE_ADD_REPLACE_EXISTING_INHERIT_PROPERTIES, NULL);
4575 if (!hDestCertStore) CertCloseStore(store, 0);
4576 return ret;
4579 static BOOL import_crl(PCCRL_CONTEXT crl, HCERTSTORE hDestCertStore)
4581 HCERTSTORE store;
4582 BOOL ret;
4584 if (!crl)
4586 SetLastError(E_INVALIDARG);
4587 return FALSE;
4589 if (hDestCertStore) store = hDestCertStore;
4590 else
4592 if (!(store = CertOpenStore(CERT_STORE_PROV_SYSTEM_W, 0, 0,
4593 CERT_SYSTEM_STORE_CURRENT_USER, ca)))
4595 WARN("unable to open certificate store\n");
4596 return FALSE;
4599 ret = CertAddCRLContextToStore(store, crl,
4600 CERT_STORE_ADD_REPLACE_EXISTING_INHERIT_PROPERTIES, NULL);
4601 if (!hDestCertStore) CertCloseStore(store, 0);
4602 return ret;
4605 static BOOL import_ctl(PCCTL_CONTEXT ctl, HCERTSTORE hDestCertStore)
4607 HCERTSTORE store;
4608 BOOL ret;
4610 if (!ctl)
4612 SetLastError(E_INVALIDARG);
4613 return FALSE;
4615 if (hDestCertStore) store = hDestCertStore;
4616 else
4618 static const WCHAR trust[] = { 'T','r','u','s','t',0 };
4620 if (!(store = CertOpenStore(CERT_STORE_PROV_SYSTEM_W, 0, 0,
4621 CERT_SYSTEM_STORE_CURRENT_USER, trust)))
4623 WARN("unable to open certificate store\n");
4624 return FALSE;
4627 ret = CertAddCTLContextToStore(store, ctl,
4628 CERT_STORE_ADD_REPLACE_EXISTING_INHERIT_PROPERTIES, NULL);
4629 if (!hDestCertStore) CertCloseStore(store, 0);
4630 return ret;
4633 /* Checks type, a type such as CERT_QUERY_CONTENT_CERT returned by
4634 * CryptQueryObject, against the allowed types. Returns TRUE if the
4635 * type is allowed, FALSE otherwise.
4637 static BOOL check_context_type(DWORD dwFlags, DWORD type)
4639 BOOL ret;
4641 if (dwFlags &
4642 (CRYPTUI_WIZ_IMPORT_ALLOW_CERT | CRYPTUI_WIZ_IMPORT_ALLOW_CRL |
4643 CRYPTUI_WIZ_IMPORT_ALLOW_CTL))
4645 switch (type)
4647 case CERT_QUERY_CONTENT_CERT:
4648 case CERT_QUERY_CONTENT_SERIALIZED_CERT:
4649 ret = dwFlags & CRYPTUI_WIZ_IMPORT_ALLOW_CERT;
4650 break;
4651 case CERT_QUERY_CONTENT_CRL:
4652 case CERT_QUERY_CONTENT_SERIALIZED_CRL:
4653 ret = dwFlags & CRYPTUI_WIZ_IMPORT_ALLOW_CRL;
4654 break;
4655 case CERT_QUERY_CONTENT_CTL:
4656 case CERT_QUERY_CONTENT_SERIALIZED_CTL:
4657 ret = dwFlags & CRYPTUI_WIZ_IMPORT_ALLOW_CTL;
4658 break;
4659 default:
4660 /* The remaining types contain more than one type, so allow
4661 * any combination.
4663 ret = TRUE;
4666 else
4668 /* No allowed types specified, so any type is allowed */
4669 ret = TRUE;
4671 if (!ret)
4672 SetLastError(E_INVALIDARG);
4673 return ret;
4677 static void import_warning(DWORD dwFlags, HWND hwnd, LPCWSTR szTitle,
4678 int warningID)
4680 if (!(dwFlags & CRYPTUI_WIZ_NO_UI))
4682 WCHAR title[MAX_STRING_LEN], error[MAX_STRING_LEN];
4683 LPCWSTR pTitle;
4685 if (szTitle)
4686 pTitle = szTitle;
4687 else
4689 LoadStringW(hInstance, IDS_IMPORT_WIZARD, title, ARRAY_SIZE(title));
4690 pTitle = title;
4692 LoadStringW(hInstance, warningID, error, ARRAY_SIZE(error));
4693 MessageBoxW(hwnd, error, pTitle, MB_ICONERROR | MB_OK);
4697 static void import_warn_type_mismatch(DWORD dwFlags, HWND hwnd, LPCWSTR szTitle)
4699 import_warning(dwFlags, hwnd, szTitle, IDS_IMPORT_TYPE_MISMATCH);
4702 static BOOL check_store_context_type(DWORD dwFlags, HCERTSTORE store)
4704 BOOL ret;
4706 if (dwFlags &
4707 (CRYPTUI_WIZ_IMPORT_ALLOW_CERT | CRYPTUI_WIZ_IMPORT_ALLOW_CRL |
4708 CRYPTUI_WIZ_IMPORT_ALLOW_CTL))
4710 PCCERT_CONTEXT cert;
4711 PCCRL_CONTEXT crl;
4712 PCCTL_CONTEXT ctl;
4714 ret = TRUE;
4715 if ((cert = CertEnumCertificatesInStore(store, NULL)))
4717 CertFreeCertificateContext(cert);
4718 if (!(dwFlags & CRYPTUI_WIZ_IMPORT_ALLOW_CERT))
4719 ret = FALSE;
4721 if (ret && (crl = CertEnumCRLsInStore(store, NULL)))
4723 CertFreeCRLContext(crl);
4724 if (!(dwFlags & CRYPTUI_WIZ_IMPORT_ALLOW_CRL))
4725 ret = FALSE;
4727 if (ret && (ctl = CertEnumCTLsInStore(store, NULL)))
4729 CertFreeCTLContext(ctl);
4730 if (!(dwFlags & CRYPTUI_WIZ_IMPORT_ALLOW_CTL))
4731 ret = FALSE;
4734 else
4735 ret = TRUE;
4736 if (!ret)
4737 SetLastError(E_INVALIDARG);
4738 return ret;
4741 static BOOL import_store(DWORD dwFlags, HWND hwnd, LPCWSTR szTitle,
4742 HCERTSTORE source, HCERTSTORE dest)
4744 BOOL ret;
4746 if ((ret = check_store_context_type(dwFlags, source)))
4748 PCCERT_CONTEXT cert = NULL;
4749 PCCRL_CONTEXT crl = NULL;
4750 PCCTL_CONTEXT ctl = NULL;
4752 do {
4753 cert = CertEnumCertificatesInStore(source, cert);
4754 if (cert)
4755 ret = import_cert(cert, dest);
4756 } while (ret && cert);
4757 do {
4758 crl = CertEnumCRLsInStore(source, crl);
4759 if (crl)
4760 ret = import_crl(crl, dest);
4761 } while (ret && crl);
4762 do {
4763 ctl = CertEnumCTLsInStore(source, ctl);
4764 if (ctl)
4765 ret = import_ctl(ctl, dest);
4766 } while (ret && ctl);
4768 else
4769 import_warn_type_mismatch(dwFlags, hwnd, szTitle);
4770 return ret;
4773 static HCERTSTORE open_store_from_file(DWORD dwFlags, LPCWSTR fileName,
4774 DWORD *pContentType)
4776 HCERTSTORE store = NULL;
4777 DWORD contentType = 0, expectedContentTypeFlags;
4779 if (dwFlags &
4780 (CRYPTUI_WIZ_IMPORT_ALLOW_CERT | CRYPTUI_WIZ_IMPORT_ALLOW_CRL |
4781 CRYPTUI_WIZ_IMPORT_ALLOW_CTL))
4783 expectedContentTypeFlags =
4784 CERT_QUERY_CONTENT_FLAG_SERIALIZED_STORE |
4785 CERT_QUERY_CONTENT_FLAG_PKCS7_SIGNED |
4786 CERT_QUERY_CONTENT_FLAG_PFX;
4787 if (dwFlags & CRYPTUI_WIZ_IMPORT_ALLOW_CERT)
4788 expectedContentTypeFlags |=
4789 CERT_QUERY_CONTENT_FLAG_CERT |
4790 CERT_QUERY_CONTENT_FLAG_SERIALIZED_CERT;
4791 if (dwFlags & CRYPTUI_WIZ_IMPORT_ALLOW_CRL)
4792 expectedContentTypeFlags |=
4793 CERT_QUERY_CONTENT_FLAG_SERIALIZED_CRL |
4794 CERT_QUERY_CONTENT_FLAG_CRL;
4795 if (dwFlags & CRYPTUI_WIZ_IMPORT_ALLOW_CTL)
4796 expectedContentTypeFlags |=
4797 CERT_QUERY_CONTENT_FLAG_CTL |
4798 CERT_QUERY_CONTENT_FLAG_SERIALIZED_CTL;
4800 else
4801 expectedContentTypeFlags =
4802 CERT_QUERY_CONTENT_FLAG_CERT |
4803 CERT_QUERY_CONTENT_FLAG_CTL |
4804 CERT_QUERY_CONTENT_FLAG_CRL |
4805 CERT_QUERY_CONTENT_FLAG_SERIALIZED_STORE |
4806 CERT_QUERY_CONTENT_FLAG_SERIALIZED_CERT |
4807 CERT_QUERY_CONTENT_FLAG_SERIALIZED_CTL |
4808 CERT_QUERY_CONTENT_FLAG_SERIALIZED_CRL |
4809 CERT_QUERY_CONTENT_FLAG_PKCS7_SIGNED |
4810 CERT_QUERY_CONTENT_FLAG_PFX;
4812 CryptQueryObject(CERT_QUERY_OBJECT_FILE, fileName,
4813 expectedContentTypeFlags, CERT_QUERY_FORMAT_FLAG_ALL, 0, NULL,
4814 &contentType, NULL, &store, NULL, NULL);
4815 if (pContentType)
4816 *pContentType = contentType;
4817 return store;
4820 static BOOL import_file(DWORD dwFlags, HWND hwnd, LPCWSTR szTitle,
4821 LPCWSTR fileName, HCERTSTORE dest)
4823 HCERTSTORE source;
4824 BOOL ret;
4826 if ((source = open_store_from_file(dwFlags, fileName, NULL)))
4828 ret = import_store(dwFlags, hwnd, szTitle, source, dest);
4829 CertCloseStore(source, 0);
4831 else
4832 ret = FALSE;
4833 return ret;
4836 struct ImportWizData
4838 HFONT titleFont;
4839 DWORD dwFlags;
4840 LPCWSTR pwszWizardTitle;
4841 CRYPTUI_WIZ_IMPORT_SRC_INFO importSrc;
4842 LPWSTR fileName;
4843 DWORD contentType;
4844 BOOL freeSource;
4845 HCERTSTORE hDestCertStore;
4846 BOOL freeDest;
4847 BOOL autoDest;
4848 BOOL success;
4851 static LRESULT CALLBACK import_welcome_dlg_proc(HWND hwnd, UINT msg, WPARAM wp,
4852 LPARAM lp)
4854 LRESULT ret = 0;
4856 switch (msg)
4858 case WM_INITDIALOG:
4860 struct ImportWizData *data;
4861 PROPSHEETPAGEW *page = (PROPSHEETPAGEW *)lp;
4862 WCHAR fontFace[MAX_STRING_LEN];
4863 HDC hDC = GetDC(hwnd);
4864 int height;
4866 data = (struct ImportWizData *)page->lParam;
4867 LoadStringW(hInstance, IDS_WIZARD_TITLE_FONT, fontFace, ARRAY_SIZE(fontFace));
4868 height = -MulDiv(12, GetDeviceCaps(hDC, LOGPIXELSY), 72);
4869 data->titleFont = CreateFontW(height, 0, 0, 0, FW_BOLD, 0, 0, 0,
4870 DEFAULT_CHARSET, OUT_DEFAULT_PRECIS, CLIP_DEFAULT_PRECIS,
4871 DEFAULT_QUALITY, DEFAULT_PITCH | FF_DONTCARE, fontFace);
4872 SendMessageW(GetDlgItem(hwnd, IDC_IMPORT_TITLE), WM_SETFONT,
4873 (WPARAM)data->titleFont, TRUE);
4874 ReleaseDC(hwnd, hDC);
4875 break;
4877 case WM_NOTIFY:
4879 NMHDR *hdr = (NMHDR *)lp;
4881 switch (hdr->code)
4883 case PSN_SETACTIVE:
4884 PostMessageW(GetParent(hwnd), PSM_SETWIZBUTTONS, 0, PSWIZB_NEXT);
4885 ret = TRUE;
4886 break;
4888 break;
4891 return ret;
4894 static const WCHAR filter_cert[] = { '*','.','c','e','r',';','*','.',
4895 'c','r','t',0 };
4896 static const WCHAR filter_pfx[] = { '*','.','p','f','x',';','*','.',
4897 'p','1','2',0 };
4898 static const WCHAR filter_crl[] = { '*','.','c','r','l',0 };
4899 static const WCHAR filter_ctl[] = { '*','.','s','t','l',0 };
4900 static const WCHAR filter_serialized_store[] = { '*','.','s','s','t',0 };
4901 static const WCHAR filter_cms[] = { '*','.','s','p','c',';','*','.',
4902 'p','7','b',0 };
4903 static const WCHAR filter_all[] = { '*','.','*',0 };
4905 static const struct
4907 int id;
4908 DWORD allowFlags;
4909 LPCWSTR filter;
4910 } import_filters[] = {
4911 { IDS_IMPORT_FILTER_CERT, CRYPTUI_WIZ_IMPORT_ALLOW_CERT, filter_cert },
4912 { IDS_IMPORT_FILTER_PFX, 0, filter_pfx },
4913 { IDS_IMPORT_FILTER_CRL, CRYPTUI_WIZ_IMPORT_ALLOW_CRL, filter_crl },
4914 { IDS_IMPORT_FILTER_CTL, CRYPTUI_WIZ_IMPORT_ALLOW_CTL, filter_ctl },
4915 { IDS_IMPORT_FILTER_SERIALIZED_STORE, 0, filter_serialized_store },
4916 { IDS_IMPORT_FILTER_CMS, 0, filter_cms },
4917 { IDS_IMPORT_FILTER_ALL, 0, filter_all },
4920 static WCHAR *make_import_file_filter(DWORD dwFlags)
4922 DWORD i;
4923 int len, totalLen = 2;
4924 LPWSTR filter = NULL, str;
4926 for (i = 0; i < ARRAY_SIZE(import_filters); i++)
4928 if (!import_filters[i].allowFlags || !dwFlags ||
4929 (dwFlags & import_filters[i].allowFlags))
4931 len = LoadStringW(hInstance, import_filters[i].id, (LPWSTR)&str, 0);
4932 totalLen += len + lstrlenW(import_filters[i].filter) + 2;
4935 filter = HeapAlloc(GetProcessHeap(), 0, totalLen * sizeof(WCHAR));
4936 if (filter)
4938 LPWSTR ptr;
4940 ptr = filter;
4941 for (i = 0; i < ARRAY_SIZE(import_filters); i++)
4943 if (!import_filters[i].allowFlags || !dwFlags ||
4944 (dwFlags & import_filters[i].allowFlags))
4946 len = LoadStringW(hInstance, import_filters[i].id,
4947 (LPWSTR)&str, 0);
4948 memcpy(ptr, str, len * sizeof(WCHAR));
4949 ptr += len;
4950 *ptr++ = 0;
4951 lstrcpyW(ptr, import_filters[i].filter);
4952 ptr += lstrlenW(import_filters[i].filter) + 1;
4955 *ptr++ = 0;
4957 return filter;
4960 static BOOL import_validate_filename(HWND hwnd, struct ImportWizData *data,
4961 LPCWSTR fileName)
4963 HANDLE file;
4964 BOOL ret = FALSE;
4966 file = CreateFileW(fileName, GENERIC_READ, FILE_SHARE_READ, NULL,
4967 OPEN_EXISTING, 0, NULL);
4968 if (file != INVALID_HANDLE_VALUE)
4970 HCERTSTORE source = open_store_from_file(data->dwFlags, fileName,
4971 &data->contentType);
4972 int warningID = 0;
4974 if (!source)
4975 warningID = IDS_IMPORT_BAD_FORMAT;
4976 else if (!check_store_context_type(data->dwFlags, source))
4977 warningID = IDS_IMPORT_TYPE_MISMATCH;
4978 else
4980 data->importSrc.dwSubjectChoice =
4981 CRYPTUI_WIZ_IMPORT_SUBJECT_CERT_STORE;
4982 data->importSrc.u.hCertStore = source;
4983 data->freeSource = TRUE;
4984 ret = TRUE;
4986 if (warningID)
4988 import_warning(data->dwFlags, hwnd, data->pwszWizardTitle,
4989 warningID);
4991 CloseHandle(file);
4993 else
4995 WCHAR title[MAX_STRING_LEN], error[MAX_STRING_LEN];
4996 LPCWSTR pTitle;
4997 LPWSTR msgBuf, fullError;
4999 if (data->pwszWizardTitle)
5000 pTitle = data->pwszWizardTitle;
5001 else
5003 LoadStringW(hInstance, IDS_IMPORT_WIZARD, title, ARRAY_SIZE(title));
5004 pTitle = title;
5006 LoadStringW(hInstance, IDS_IMPORT_OPEN_FAILED, error, ARRAY_SIZE(error));
5007 FormatMessageW(
5008 FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM, NULL,
5009 GetLastError(), 0, (LPWSTR) &msgBuf, 0, NULL);
5010 fullError = HeapAlloc(GetProcessHeap(), 0,
5011 (lstrlenW(error) + lstrlenW(fileName) + lstrlenW(msgBuf) + 3)
5012 * sizeof(WCHAR));
5013 if (fullError)
5015 LPWSTR ptr = fullError;
5017 lstrcpyW(ptr, error);
5018 ptr += lstrlenW(error);
5019 lstrcpyW(ptr, fileName);
5020 ptr += lstrlenW(fileName);
5021 *ptr++ = ':';
5022 *ptr++ = '\n';
5023 lstrcpyW(ptr, msgBuf);
5024 MessageBoxW(hwnd, fullError, pTitle, MB_ICONERROR | MB_OK);
5025 HeapFree(GetProcessHeap(), 0, fullError);
5027 LocalFree(msgBuf);
5029 return ret;
5032 static LRESULT CALLBACK import_file_dlg_proc(HWND hwnd, UINT msg, WPARAM wp,
5033 LPARAM lp)
5035 LRESULT ret = 0;
5036 struct ImportWizData *data;
5038 switch (msg)
5040 case WM_INITDIALOG:
5042 PROPSHEETPAGEW *page = (PROPSHEETPAGEW *)lp;
5044 data = (struct ImportWizData *)page->lParam;
5045 SetWindowLongPtrW(hwnd, DWLP_USER, (LPARAM)data);
5046 if (data->fileName)
5048 HWND fileNameEdit = GetDlgItem(hwnd, IDC_IMPORT_FILENAME);
5050 SendMessageW(fileNameEdit, WM_SETTEXT, 0, (LPARAM)data->fileName);
5052 break;
5054 case WM_NOTIFY:
5056 NMHDR *hdr = (NMHDR *)lp;
5058 switch (hdr->code)
5060 case PSN_SETACTIVE:
5061 PostMessageW(GetParent(hwnd), PSM_SETWIZBUTTONS, 0,
5062 PSWIZB_BACK | PSWIZB_NEXT);
5063 ret = TRUE;
5064 break;
5065 case PSN_WIZNEXT:
5067 HWND fileNameEdit = GetDlgItem(hwnd, IDC_IMPORT_FILENAME);
5068 DWORD len = SendMessageW(fileNameEdit, WM_GETTEXTLENGTH, 0, 0);
5070 data = (struct ImportWizData *)GetWindowLongPtrW(hwnd, DWLP_USER);
5071 if (!len)
5073 import_warning(data->dwFlags, hwnd, data->pwszWizardTitle,
5074 IDS_IMPORT_EMPTY_FILE);
5075 SetWindowLongPtrW(hwnd, DWLP_MSGRESULT, 1);
5076 ret = 1;
5078 else
5080 LPWSTR fileName = HeapAlloc(GetProcessHeap(), 0,
5081 (len + 1) * sizeof(WCHAR));
5083 if (fileName)
5085 SendMessageW(fileNameEdit, WM_GETTEXT, len + 1,
5086 (LPARAM)fileName);
5087 if (!import_validate_filename(hwnd, data, fileName))
5089 HeapFree(GetProcessHeap(), 0, fileName);
5090 SetWindowLongPtrW(hwnd, DWLP_MSGRESULT, 1);
5091 ret = 1;
5093 else
5094 data->fileName = fileName;
5097 break;
5100 break;
5102 case WM_COMMAND:
5103 switch (wp)
5105 case IDC_IMPORT_BROWSE_FILE:
5107 OPENFILENAMEW ofn;
5108 WCHAR fileBuf[MAX_PATH];
5110 data = (struct ImportWizData *)GetWindowLongPtrW(hwnd, DWLP_USER);
5111 memset(&ofn, 0, sizeof(ofn));
5112 ofn.lStructSize = sizeof(ofn);
5113 ofn.hwndOwner = hwnd;
5114 ofn.lpstrFilter = make_import_file_filter(data->dwFlags);
5115 ofn.lpstrFile = fileBuf;
5116 ofn.nMaxFile = ARRAY_SIZE(fileBuf);
5117 fileBuf[0] = 0;
5118 if (GetOpenFileNameW(&ofn))
5119 SendMessageW(GetDlgItem(hwnd, IDC_IMPORT_FILENAME), WM_SETTEXT,
5120 0, (LPARAM)ofn.lpstrFile);
5121 HeapFree(GetProcessHeap(), 0, (LPWSTR)ofn.lpstrFilter);
5122 break;
5125 break;
5127 return ret;
5130 static LRESULT CALLBACK import_store_dlg_proc(HWND hwnd, UINT msg, WPARAM wp,
5131 LPARAM lp)
5133 LRESULT ret = 0;
5134 struct ImportWizData *data;
5136 switch (msg)
5138 case WM_INITDIALOG:
5140 PROPSHEETPAGEW *page = (PROPSHEETPAGEW *)lp;
5142 data = (struct ImportWizData *)page->lParam;
5143 SetWindowLongPtrW(hwnd, DWLP_USER, (LPARAM)data);
5144 if (!data->hDestCertStore)
5146 SendMessageW(GetDlgItem(hwnd, IDC_IMPORT_AUTO_STORE), BM_CLICK,
5147 0, 0);
5148 EnableWindow(GetDlgItem(hwnd, IDC_IMPORT_STORE), FALSE);
5149 EnableWindow(GetDlgItem(hwnd, IDC_IMPORT_BROWSE_STORE), FALSE);
5150 EnableWindow(GetDlgItem(hwnd, IDC_IMPORT_SPECIFY_STORE), FALSE);
5152 else
5154 WCHAR storeTitle[MAX_STRING_LEN];
5156 SendMessageW(GetDlgItem(hwnd, IDC_IMPORT_SPECIFY_STORE), BM_CLICK,
5157 0, 0);
5158 EnableWindow(GetDlgItem(hwnd, IDC_IMPORT_STORE), TRUE);
5159 EnableWindow(GetDlgItem(hwnd, IDC_IMPORT_BROWSE_STORE), TRUE);
5160 EnableWindow(GetDlgItem(hwnd, IDC_IMPORT_SPECIFY_STORE),
5161 !(data->dwFlags & CRYPTUI_WIZ_IMPORT_NO_CHANGE_DEST_STORE));
5162 LoadStringW(hInstance, IDS_IMPORT_DEST_DETERMINED, storeTitle, ARRAY_SIZE(storeTitle));
5163 SendMessageW(GetDlgItem(hwnd, IDC_IMPORT_STORE), WM_SETTEXT,
5164 0, (LPARAM)storeTitle);
5166 break;
5168 case WM_NOTIFY:
5170 NMHDR *hdr = (NMHDR *)lp;
5172 switch (hdr->code)
5174 case PSN_SETACTIVE:
5175 PostMessageW(GetParent(hwnd), PSM_SETWIZBUTTONS, 0,
5176 PSWIZB_BACK | PSWIZB_NEXT);
5177 ret = TRUE;
5178 break;
5179 case PSN_WIZNEXT:
5181 data = (struct ImportWizData *)GetWindowLongPtrW(hwnd, DWLP_USER);
5182 if (IsDlgButtonChecked(hwnd, IDC_IMPORT_SPECIFY_STORE) &&
5183 !data->hDestCertStore)
5185 import_warning(data->dwFlags, hwnd, data->pwszWizardTitle,
5186 IDS_IMPORT_SELECT_STORE);
5187 SetWindowLongPtrW(hwnd, DWLP_MSGRESULT, 1);
5188 ret = 1;
5190 break;
5193 break;
5195 case WM_COMMAND:
5196 switch (wp)
5198 case IDC_IMPORT_AUTO_STORE:
5199 data = (struct ImportWizData *)GetWindowLongPtrW(hwnd, DWLP_USER);
5200 data->autoDest = TRUE;
5201 EnableWindow(GetDlgItem(hwnd, IDC_IMPORT_STORE), FALSE);
5202 EnableWindow(GetDlgItem(hwnd, IDC_IMPORT_BROWSE_STORE), FALSE);
5203 break;
5204 case IDC_IMPORT_SPECIFY_STORE:
5205 data = (struct ImportWizData *)GetWindowLongPtrW(hwnd, DWLP_USER);
5206 data->autoDest = FALSE;
5207 EnableWindow(GetDlgItem(hwnd, IDC_IMPORT_STORE), TRUE);
5208 EnableWindow(GetDlgItem(hwnd, IDC_IMPORT_BROWSE_STORE), TRUE);
5209 break;
5210 case IDC_IMPORT_BROWSE_STORE:
5212 CRYPTUI_ENUM_SYSTEM_STORE_ARGS enumArgs = {
5213 CERT_SYSTEM_STORE_CURRENT_USER, NULL };
5214 CRYPTUI_ENUM_DATA enumData = { 0, NULL, 1, &enumArgs };
5215 CRYPTUI_SELECTSTORE_INFO_W selectInfo;
5216 HCERTSTORE store;
5218 data = (struct ImportWizData *)GetWindowLongPtrW(hwnd, DWLP_USER);
5219 selectInfo.dwSize = sizeof(selectInfo);
5220 selectInfo.parent = hwnd;
5221 selectInfo.dwFlags = CRYPTUI_ENABLE_SHOW_PHYSICAL_STORE;
5222 selectInfo.pwszTitle = NULL;
5223 selectInfo.pwszText = NULL;
5224 selectInfo.pEnumData = &enumData;
5225 selectInfo.pfnSelectedStoreCallback = NULL;
5226 if ((store = CryptUIDlgSelectStoreW(&selectInfo)))
5228 WCHAR storeTitle[MAX_STRING_LEN];
5230 LoadStringW(hInstance, IDS_IMPORT_DEST_DETERMINED, storeTitle,
5231 ARRAY_SIZE(storeTitle));
5232 SendMessageW(GetDlgItem(hwnd, IDC_IMPORT_STORE), WM_SETTEXT,
5233 0, (LPARAM)storeTitle);
5234 data->hDestCertStore = store;
5235 data->freeDest = TRUE;
5237 break;
5240 break;
5242 return ret;
5245 static void show_import_details(HWND lv, struct ImportWizData *data)
5247 WCHAR text[MAX_STRING_LEN];
5248 LVITEMW item;
5249 int contentID;
5251 item.mask = LVIF_TEXT;
5252 item.iItem = SendMessageW(lv, LVM_GETITEMCOUNT, 0, 0);
5253 item.iSubItem = 0;
5254 LoadStringW(hInstance, IDS_IMPORT_STORE_SELECTION, text, ARRAY_SIZE(text));
5255 item.pszText = text;
5256 SendMessageW(lv, LVM_INSERTITEMW, 0, (LPARAM)&item);
5257 item.iSubItem = 1;
5258 if (data->autoDest)
5259 LoadStringW(hInstance, IDS_IMPORT_DEST_AUTOMATIC, text, ARRAY_SIZE(text));
5260 else
5261 LoadStringW(hInstance, IDS_IMPORT_DEST_DETERMINED, text, ARRAY_SIZE(text));
5262 SendMessageW(lv, LVM_SETITEMTEXTW, item.iItem, (LPARAM)&item);
5263 item.iItem = SendMessageW(lv, LVM_GETITEMCOUNT, 0, 0);
5264 item.iSubItem = 0;
5265 LoadStringW(hInstance, IDS_IMPORT_CONTENT, text, ARRAY_SIZE(text));
5266 SendMessageW(lv, LVM_INSERTITEMW, 0, (LPARAM)&item);
5267 switch (data->contentType)
5269 case CERT_QUERY_CONTENT_CERT:
5270 case CERT_QUERY_CONTENT_SERIALIZED_CERT:
5271 contentID = IDS_IMPORT_CONTENT_CERT;
5272 break;
5273 case CERT_QUERY_CONTENT_CRL:
5274 case CERT_QUERY_CONTENT_SERIALIZED_CRL:
5275 contentID = IDS_IMPORT_CONTENT_CRL;
5276 break;
5277 case CERT_QUERY_CONTENT_CTL:
5278 case CERT_QUERY_CONTENT_SERIALIZED_CTL:
5279 contentID = IDS_IMPORT_CONTENT_CTL;
5280 break;
5281 case CERT_QUERY_CONTENT_PKCS7_SIGNED:
5282 contentID = IDS_IMPORT_CONTENT_CMS;
5283 break;
5284 case CERT_QUERY_CONTENT_FLAG_PFX:
5285 contentID = IDS_IMPORT_CONTENT_PFX;
5286 break;
5287 default:
5288 contentID = IDS_IMPORT_CONTENT_STORE;
5289 break;
5291 LoadStringW(hInstance, contentID, text, ARRAY_SIZE(text));
5292 item.iSubItem = 1;
5293 SendMessageW(lv, LVM_SETITEMTEXTW, item.iItem, (LPARAM)&item);
5294 if (data->fileName)
5296 item.iItem = SendMessageW(lv, LVM_GETITEMCOUNT, 0, 0);
5297 item.iSubItem = 0;
5298 LoadStringW(hInstance, IDS_IMPORT_FILE, text, ARRAY_SIZE(text));
5299 SendMessageW(lv, LVM_INSERTITEMW, 0, (LPARAM)&item);
5300 item.iSubItem = 1;
5301 item.pszText = data->fileName;
5302 SendMessageW(lv, LVM_SETITEMTEXTW, item.iItem, (LPARAM)&item);
5306 static BOOL do_import(DWORD dwFlags, HWND hwndParent, LPCWSTR pwszWizardTitle,
5307 PCCRYPTUI_WIZ_IMPORT_SRC_INFO pImportSrc, HCERTSTORE hDestCertStore)
5309 BOOL ret;
5311 switch (pImportSrc->dwSubjectChoice)
5313 case CRYPTUI_WIZ_IMPORT_SUBJECT_FILE:
5314 ret = import_file(dwFlags, hwndParent, pwszWizardTitle,
5315 pImportSrc->u.pwszFileName, hDestCertStore);
5316 break;
5317 case CRYPTUI_WIZ_IMPORT_SUBJECT_CERT_CONTEXT:
5318 if ((ret = check_context_type(dwFlags, CERT_QUERY_CONTENT_CERT)))
5319 ret = import_cert(pImportSrc->u.pCertContext, hDestCertStore);
5320 else
5321 import_warn_type_mismatch(dwFlags, hwndParent, pwszWizardTitle);
5322 break;
5323 case CRYPTUI_WIZ_IMPORT_SUBJECT_CRL_CONTEXT:
5324 if ((ret = check_context_type(dwFlags, CERT_QUERY_CONTENT_CRL)))
5325 ret = import_crl(pImportSrc->u.pCRLContext, hDestCertStore);
5326 else
5327 import_warn_type_mismatch(dwFlags, hwndParent, pwszWizardTitle);
5328 break;
5329 case CRYPTUI_WIZ_IMPORT_SUBJECT_CTL_CONTEXT:
5330 if ((ret = check_context_type(dwFlags, CERT_QUERY_CONTENT_CTL)))
5331 ret = import_ctl(pImportSrc->u.pCTLContext, hDestCertStore);
5332 else
5333 import_warn_type_mismatch(dwFlags, hwndParent, pwszWizardTitle);
5334 break;
5335 case CRYPTUI_WIZ_IMPORT_SUBJECT_CERT_STORE:
5336 ret = import_store(dwFlags, hwndParent, pwszWizardTitle,
5337 pImportSrc->u.hCertStore, hDestCertStore);
5338 break;
5339 default:
5340 WARN("unknown source type: %u\n", pImportSrc->dwSubjectChoice);
5341 SetLastError(E_INVALIDARG);
5342 ret = FALSE;
5344 return ret;
5347 static LRESULT CALLBACK import_finish_dlg_proc(HWND hwnd, UINT msg, WPARAM wp,
5348 LPARAM lp)
5350 LRESULT ret = 0;
5351 struct ImportWizData *data;
5353 switch (msg)
5355 case WM_INITDIALOG:
5357 PROPSHEETPAGEW *page = (PROPSHEETPAGEW *)lp;
5358 HWND lv = GetDlgItem(hwnd, IDC_IMPORT_SETTINGS);
5359 RECT rc;
5360 LVCOLUMNW column;
5362 data = (struct ImportWizData *)page->lParam;
5363 SetWindowLongPtrW(hwnd, DWLP_USER, (LPARAM)data);
5364 SendMessageW(GetDlgItem(hwnd, IDC_IMPORT_TITLE), WM_SETFONT,
5365 (WPARAM)data->titleFont, TRUE);
5366 GetWindowRect(lv, &rc);
5367 column.mask = LVCF_WIDTH;
5368 column.cx = (rc.right - rc.left) / 2 - 2;
5369 SendMessageW(lv, LVM_INSERTCOLUMNW, 0, (LPARAM)&column);
5370 SendMessageW(lv, LVM_INSERTCOLUMNW, 1, (LPARAM)&column);
5371 show_import_details(lv, data);
5372 break;
5374 case WM_NOTIFY:
5376 NMHDR *hdr = (NMHDR *)lp;
5378 switch (hdr->code)
5380 case PSN_SETACTIVE:
5382 HWND lv = GetDlgItem(hwnd, IDC_IMPORT_SETTINGS);
5384 data = (struct ImportWizData *)GetWindowLongPtrW(hwnd, DWLP_USER);
5385 SendMessageW(lv, LVM_DELETEALLITEMS, 0, 0);
5386 show_import_details(lv, data);
5387 PostMessageW(GetParent(hwnd), PSM_SETWIZBUTTONS, 0,
5388 PSWIZB_BACK | PSWIZB_FINISH);
5389 ret = TRUE;
5390 break;
5392 case PSN_WIZFINISH:
5394 data = (struct ImportWizData *)GetWindowLongPtrW(hwnd, DWLP_USER);
5395 if ((data->success = do_import(data->dwFlags, hwnd,
5396 data->pwszWizardTitle, &data->importSrc, data->hDestCertStore)))
5398 WCHAR title[MAX_STRING_LEN], message[MAX_STRING_LEN];
5399 LPCWSTR pTitle;
5401 if (data->pwszWizardTitle)
5402 pTitle = data->pwszWizardTitle;
5403 else
5405 LoadStringW(hInstance, IDS_IMPORT_WIZARD, title, ARRAY_SIZE(title));
5406 pTitle = title;
5408 LoadStringW(hInstance, IDS_IMPORT_SUCCEEDED, message, ARRAY_SIZE(message));
5409 MessageBoxW(hwnd, message, pTitle, MB_OK);
5411 else
5412 import_warning(data->dwFlags, hwnd, data->pwszWizardTitle,
5413 IDS_IMPORT_FAILED);
5414 break;
5417 break;
5420 return ret;
5423 static BOOL show_import_ui(DWORD dwFlags, HWND hwndParent,
5424 LPCWSTR pwszWizardTitle, PCCRYPTUI_WIZ_IMPORT_SRC_INFO pImportSrc,
5425 HCERTSTORE hDestCertStore)
5427 PROPSHEETHEADERW hdr;
5428 PROPSHEETPAGEW pages[4];
5429 struct ImportWizData data;
5430 int nPages = 0;
5432 data.dwFlags = dwFlags;
5433 data.pwszWizardTitle = pwszWizardTitle;
5434 if (pImportSrc)
5436 memcpy(&data.importSrc, pImportSrc, sizeof(data.importSrc));
5437 data.fileName = (LPWSTR)pImportSrc->u.pwszFileName;
5439 else
5441 memset(&data.importSrc, 0, sizeof(data.importSrc));
5442 data.fileName = NULL;
5444 data.freeSource = FALSE;
5445 data.hDestCertStore = hDestCertStore;
5446 data.freeDest = FALSE;
5447 data.autoDest = TRUE;
5448 data.success = TRUE;
5450 memset(pages, 0, sizeof(pages));
5452 pages[nPages].dwSize = sizeof(pages[0]);
5453 pages[nPages].hInstance = hInstance;
5454 pages[nPages].u.pszTemplate = MAKEINTRESOURCEW(IDD_IMPORT_WELCOME);
5455 pages[nPages].pfnDlgProc = import_welcome_dlg_proc;
5456 pages[nPages].dwFlags = PSP_HIDEHEADER;
5457 pages[nPages].lParam = (LPARAM)&data;
5458 nPages++;
5460 if (!pImportSrc ||
5461 pImportSrc->dwSubjectChoice == CRYPTUI_WIZ_IMPORT_SUBJECT_FILE)
5463 pages[nPages].dwSize = sizeof(pages[0]);
5464 pages[nPages].hInstance = hInstance;
5465 pages[nPages].u.pszTemplate = MAKEINTRESOURCEW(IDD_IMPORT_FILE);
5466 pages[nPages].pfnDlgProc = import_file_dlg_proc;
5467 pages[nPages].dwFlags = PSP_USEHEADERTITLE | PSP_USEHEADERSUBTITLE;
5468 pages[nPages].pszHeaderTitle = MAKEINTRESOURCEW(IDS_IMPORT_FILE_TITLE);
5469 pages[nPages].pszHeaderSubTitle =
5470 MAKEINTRESOURCEW(IDS_IMPORT_FILE_SUBTITLE);
5471 pages[nPages].lParam = (LPARAM)&data;
5472 nPages++;
5474 else
5476 switch (pImportSrc->dwSubjectChoice)
5478 case CRYPTUI_WIZ_IMPORT_SUBJECT_CERT_CONTEXT:
5479 data.contentType = CERT_QUERY_CONTENT_CERT;
5480 break;
5481 case CRYPTUI_WIZ_IMPORT_SUBJECT_CRL_CONTEXT:
5482 data.contentType = CERT_QUERY_CONTENT_CRL;
5483 break;
5484 case CRYPTUI_WIZ_IMPORT_SUBJECT_CTL_CONTEXT:
5485 data.contentType = CERT_QUERY_CONTENT_CTL;
5486 break;
5487 case CRYPTUI_WIZ_IMPORT_SUBJECT_CERT_STORE:
5488 data.contentType = CERT_QUERY_CONTENT_SERIALIZED_STORE;
5489 break;
5493 pages[nPages].dwSize = sizeof(pages[0]);
5494 pages[nPages].hInstance = hInstance;
5495 pages[nPages].u.pszTemplate = MAKEINTRESOURCEW(IDD_IMPORT_STORE);
5496 pages[nPages].pfnDlgProc = import_store_dlg_proc;
5497 pages[nPages].dwFlags = PSP_USEHEADERTITLE | PSP_USEHEADERSUBTITLE;
5498 pages[nPages].pszHeaderTitle = MAKEINTRESOURCEW(IDS_IMPORT_STORE_TITLE);
5499 pages[nPages].pszHeaderSubTitle =
5500 MAKEINTRESOURCEW(IDS_IMPORT_STORE_SUBTITLE);
5501 pages[nPages].lParam = (LPARAM)&data;
5502 nPages++;
5504 pages[nPages].dwSize = sizeof(pages[0]);
5505 pages[nPages].hInstance = hInstance;
5506 pages[nPages].u.pszTemplate = MAKEINTRESOURCEW(IDD_IMPORT_FINISH);
5507 pages[nPages].pfnDlgProc = import_finish_dlg_proc;
5508 pages[nPages].dwFlags = PSP_HIDEHEADER;
5509 pages[nPages].lParam = (LPARAM)&data;
5510 nPages++;
5512 memset(&hdr, 0, sizeof(hdr));
5513 hdr.dwSize = sizeof(hdr);
5514 hdr.hwndParent = hwndParent;
5515 hdr.dwFlags = PSH_PROPSHEETPAGE | PSH_WIZARD97_NEW | PSH_HEADER |
5516 PSH_WATERMARK;
5517 hdr.hInstance = hInstance;
5518 if (pwszWizardTitle)
5519 hdr.pszCaption = pwszWizardTitle;
5520 else
5521 hdr.pszCaption = MAKEINTRESOURCEW(IDS_IMPORT_WIZARD);
5522 hdr.u3.ppsp = pages;
5523 hdr.nPages = nPages;
5524 hdr.u4.pszbmWatermark = MAKEINTRESOURCEW(IDB_CERT_WATERMARK);
5525 hdr.u5.pszbmHeader = MAKEINTRESOURCEW(IDB_CERT_HEADER);
5526 PropertySheetW(&hdr);
5527 if (data.fileName != data.importSrc.u.pwszFileName)
5528 HeapFree(GetProcessHeap(), 0, data.fileName);
5529 if (data.freeSource &&
5530 data.importSrc.dwSubjectChoice == CRYPTUI_WIZ_IMPORT_SUBJECT_CERT_STORE)
5531 CertCloseStore(data.importSrc.u.hCertStore, 0);
5532 DeleteObject(data.titleFont);
5533 return data.success;
5536 BOOL WINAPI CryptUIWizImport(DWORD dwFlags, HWND hwndParent, LPCWSTR pwszWizardTitle,
5537 PCCRYPTUI_WIZ_IMPORT_SRC_INFO pImportSrc, HCERTSTORE hDestCertStore)
5539 BOOL ret;
5541 TRACE("(0x%08x, %p, %s, %p, %p)\n", dwFlags, hwndParent, debugstr_w(pwszWizardTitle),
5542 pImportSrc, hDestCertStore);
5544 if (pImportSrc &&
5545 pImportSrc->dwSize != sizeof(CRYPTUI_WIZ_IMPORT_SRC_INFO))
5547 SetLastError(E_INVALIDARG);
5548 return FALSE;
5551 if (!(dwFlags & CRYPTUI_WIZ_NO_UI))
5552 ret = show_import_ui(dwFlags, hwndParent, pwszWizardTitle, pImportSrc,
5553 hDestCertStore);
5554 else if (pImportSrc)
5555 ret = do_import(dwFlags, hwndParent, pwszWizardTitle, pImportSrc,
5556 hDestCertStore);
5557 else
5559 /* Can't have no UI without specifying source */
5560 SetLastError(E_INVALIDARG);
5561 ret = FALSE;
5564 return ret;
5567 struct ExportWizData
5569 HFONT titleFont;
5570 DWORD dwFlags;
5571 LPCWSTR pwszWizardTitle;
5572 CRYPTUI_WIZ_EXPORT_INFO exportInfo;
5573 CRYPTUI_WIZ_EXPORT_CERTCONTEXT_INFO contextInfo;
5574 BOOL freePassword;
5575 PCRYPT_KEY_PROV_INFO keyProvInfo;
5576 BOOL deleteKeys;
5577 LPWSTR fileName;
5578 HANDLE file;
5579 BOOL success;
5582 static LRESULT CALLBACK export_welcome_dlg_proc(HWND hwnd, UINT msg, WPARAM wp,
5583 LPARAM lp)
5585 LRESULT ret = 0;
5587 switch (msg)
5589 case WM_INITDIALOG:
5591 struct ExportWizData *data;
5592 PROPSHEETPAGEW *page = (PROPSHEETPAGEW *)lp;
5593 WCHAR fontFace[MAX_STRING_LEN];
5594 HDC hDC = GetDC(hwnd);
5595 int height;
5597 data = (struct ExportWizData *)page->lParam;
5598 LoadStringW(hInstance, IDS_WIZARD_TITLE_FONT, fontFace, ARRAY_SIZE(fontFace));
5599 height = -MulDiv(12, GetDeviceCaps(hDC, LOGPIXELSY), 72);
5600 data->titleFont = CreateFontW(height, 0, 0, 0, FW_BOLD, 0, 0, 0,
5601 DEFAULT_CHARSET, OUT_DEFAULT_PRECIS, CLIP_DEFAULT_PRECIS,
5602 DEFAULT_QUALITY, DEFAULT_PITCH | FF_DONTCARE, fontFace);
5603 SendMessageW(GetDlgItem(hwnd, IDC_EXPORT_TITLE), WM_SETFONT,
5604 (WPARAM)data->titleFont, TRUE);
5605 ReleaseDC(hwnd, hDC);
5606 break;
5608 case WM_NOTIFY:
5610 NMHDR *hdr = (NMHDR *)lp;
5612 switch (hdr->code)
5614 case PSN_SETACTIVE:
5615 PostMessageW(GetParent(hwnd), PSM_SETWIZBUTTONS, 0, PSWIZB_NEXT);
5616 ret = TRUE;
5617 break;
5619 break;
5622 return ret;
5625 static PCRYPT_KEY_PROV_INFO export_get_private_key_info(PCCERT_CONTEXT cert)
5627 PCRYPT_KEY_PROV_INFO info = NULL;
5628 DWORD size;
5630 if (CertGetCertificateContextProperty(cert, CERT_KEY_PROV_INFO_PROP_ID,
5631 NULL, &size))
5633 info = HeapAlloc(GetProcessHeap(), 0, size);
5634 if (info)
5636 if (!CertGetCertificateContextProperty(cert,
5637 CERT_KEY_PROV_INFO_PROP_ID, info, &size))
5639 HeapFree(GetProcessHeap(), 0, info);
5640 info = NULL;
5644 return info;
5647 static BOOL export_acquire_private_key(const CRYPT_KEY_PROV_INFO *info,
5648 HCRYPTPROV *phProv)
5650 BOOL ret;
5652 ret = CryptAcquireContextW(phProv, info->pwszContainerName,
5653 info->pwszProvName, info->dwProvType, 0);
5654 if (ret)
5656 DWORD i;
5658 for (i = 0; i < info->cProvParam; i++)
5659 CryptSetProvParam(*phProv, info->rgProvParam[i].dwParam,
5660 info->rgProvParam[i].pbData, info->rgProvParam[i].dwFlags);
5662 return ret;
5665 static BOOL export_is_key_exportable(HCRYPTPROV hProv, DWORD keySpec)
5667 BOOL ret;
5668 HCRYPTKEY key;
5670 if ((ret = CryptGetUserKey(hProv, keySpec, &key)))
5672 DWORD permissions, size = sizeof(permissions);
5674 if ((ret = CryptGetKeyParam(key, KP_PERMISSIONS, (BYTE *)&permissions,
5675 &size, 0)) && !(permissions & CRYPT_EXPORT))
5676 ret = FALSE;
5677 CryptDestroyKey(key);
5679 return ret;
5682 static LRESULT CALLBACK export_private_key_dlg_proc(HWND hwnd, UINT msg,
5683 WPARAM wp, LPARAM lp)
5685 LRESULT ret = 0;
5686 struct ExportWizData *data;
5688 switch (msg)
5690 case WM_INITDIALOG:
5692 PROPSHEETPAGEW *page = (PROPSHEETPAGEW *)lp;
5693 PCRYPT_KEY_PROV_INFO info;
5694 HCRYPTPROV hProv = 0;
5695 int errorID = 0;
5697 data = (struct ExportWizData *)page->lParam;
5698 SetWindowLongPtrW(hwnd, DWLP_USER, (LPARAM)data);
5699 /* Get enough information about a key to see whether it's exportable.
5701 if (!(info = export_get_private_key_info(
5702 data->exportInfo.u.pCertContext)))
5703 errorID = IDS_EXPORT_PRIVATE_KEY_UNAVAILABLE;
5704 else if (!export_acquire_private_key(info, &hProv))
5705 errorID = IDS_EXPORT_PRIVATE_KEY_UNAVAILABLE;
5706 else if (!export_is_key_exportable(hProv, info->dwKeySpec))
5707 errorID = IDS_EXPORT_PRIVATE_KEY_NON_EXPORTABLE;
5709 if (errorID)
5711 WCHAR error[MAX_STRING_LEN];
5713 LoadStringW(hInstance, errorID, error, ARRAY_SIZE(error));
5714 SendMessageW(GetDlgItem(hwnd, IDC_EXPORT_PRIVATE_KEY_UNAVAILABLE),
5715 WM_SETTEXT, 0, (LPARAM)error);
5716 EnableWindow(GetDlgItem(hwnd, IDC_EXPORT_PRIVATE_KEY_YES), FALSE);
5718 else
5719 data->keyProvInfo = info;
5720 if (hProv)
5721 CryptReleaseContext(hProv, 0);
5722 SendMessageW(GetDlgItem(hwnd, IDC_EXPORT_PRIVATE_KEY_NO), BM_CLICK,
5723 0, 0);
5724 break;
5726 case WM_NOTIFY:
5728 NMHDR *hdr = (NMHDR *)lp;
5730 switch (hdr->code)
5732 case PSN_SETACTIVE:
5733 PostMessageW(GetParent(hwnd), PSM_SETWIZBUTTONS, 0,
5734 PSWIZB_BACK | PSWIZB_NEXT);
5735 ret = TRUE;
5736 break;
5737 case PSN_WIZNEXT:
5738 data = (struct ExportWizData *)GetWindowLongPtrW(hwnd, DWLP_USER);
5739 if (IsDlgButtonChecked(hwnd, IDC_EXPORT_PRIVATE_KEY_NO))
5741 data->contextInfo.dwExportFormat =
5742 CRYPTUI_WIZ_EXPORT_FORMAT_DER;
5743 data->contextInfo.fExportPrivateKeys = FALSE;
5745 else
5747 data->contextInfo.dwExportFormat =
5748 CRYPTUI_WIZ_EXPORT_FORMAT_PFX;
5749 data->contextInfo.fExportPrivateKeys = TRUE;
5751 break;
5753 break;
5756 return ret;
5759 static BOOL export_info_has_private_key(PCCRYPTUI_WIZ_EXPORT_INFO pExportInfo)
5761 BOOL ret = FALSE;
5763 if (pExportInfo->dwSubjectChoice == CRYPTUI_WIZ_EXPORT_CERT_CONTEXT)
5765 DWORD size;
5767 /* If there's a CRYPT_KEY_PROV_INFO set for this cert, assume the
5768 * cert has a private key.
5770 if (CertGetCertificateContextProperty(pExportInfo->u.pCertContext,
5771 CERT_KEY_PROV_INFO_PROP_ID, NULL, &size))
5772 ret = TRUE;
5774 return ret;
5777 static void export_format_enable_controls(HWND hwnd, const struct ExportWizData *data)
5779 int defaultFormatID;
5781 switch (data->contextInfo.dwExportFormat)
5783 case CRYPTUI_WIZ_EXPORT_FORMAT_BASE64:
5784 defaultFormatID = IDC_EXPORT_FORMAT_BASE64;
5785 break;
5786 case CRYPTUI_WIZ_EXPORT_FORMAT_PKCS7:
5787 defaultFormatID = IDC_EXPORT_FORMAT_CMS;
5788 break;
5789 case CRYPTUI_WIZ_EXPORT_FORMAT_PFX:
5790 defaultFormatID = IDC_EXPORT_FORMAT_PFX;
5791 break;
5792 default:
5793 defaultFormatID = IDC_EXPORT_FORMAT_DER;
5795 SendMessageW(GetDlgItem(hwnd, defaultFormatID), BM_CLICK, 0, 0);
5796 if (defaultFormatID == IDC_EXPORT_FORMAT_PFX)
5798 EnableWindow(GetDlgItem(hwnd, IDC_EXPORT_FORMAT_DER), FALSE);
5799 EnableWindow(GetDlgItem(hwnd, IDC_EXPORT_FORMAT_BASE64), FALSE);
5800 EnableWindow(GetDlgItem(hwnd, IDC_EXPORT_FORMAT_CMS), FALSE);
5801 EnableWindow(GetDlgItem(hwnd, IDC_EXPORT_FORMAT_PFX), TRUE);
5803 else
5805 EnableWindow(GetDlgItem(hwnd, IDC_EXPORT_FORMAT_DER), TRUE);
5806 EnableWindow(GetDlgItem(hwnd, IDC_EXPORT_FORMAT_BASE64), TRUE);
5807 EnableWindow(GetDlgItem(hwnd, IDC_EXPORT_FORMAT_CMS), TRUE);
5808 EnableWindow(GetDlgItem(hwnd, IDC_EXPORT_FORMAT_PFX), FALSE);
5812 static LRESULT CALLBACK export_format_dlg_proc(HWND hwnd, UINT msg, WPARAM wp,
5813 LPARAM lp)
5815 LRESULT ret = 0;
5816 struct ExportWizData *data;
5818 switch (msg)
5820 case WM_INITDIALOG:
5822 PROPSHEETPAGEW *page = (PROPSHEETPAGEW *)lp;
5824 data = (struct ExportWizData *)page->lParam;
5825 SetWindowLongPtrW(hwnd, DWLP_USER, (LPARAM)data);
5826 export_format_enable_controls(hwnd, data);
5827 break;
5829 case WM_NOTIFY:
5831 NMHDR *hdr = (NMHDR *)lp;
5833 switch (hdr->code)
5835 case PSN_SETACTIVE:
5836 PostMessageW(GetParent(hwnd), PSM_SETWIZBUTTONS, 0,
5837 PSWIZB_BACK | PSWIZB_NEXT);
5838 data = (struct ExportWizData *)GetWindowLongPtrW(hwnd, DWLP_USER);
5839 export_format_enable_controls(hwnd, data);
5840 ret = TRUE;
5841 break;
5842 case PSN_WIZNEXT:
5844 BOOL skipPasswordPage = TRUE;
5846 data = (struct ExportWizData *)GetWindowLongPtrW(hwnd, DWLP_USER);
5847 if (IsDlgButtonChecked(hwnd, IDC_EXPORT_FORMAT_DER))
5848 data->contextInfo.dwExportFormat =
5849 CRYPTUI_WIZ_EXPORT_FORMAT_DER;
5850 else if (IsDlgButtonChecked(hwnd, IDC_EXPORT_FORMAT_BASE64))
5851 data->contextInfo.dwExportFormat =
5852 CRYPTUI_WIZ_EXPORT_FORMAT_BASE64;
5853 else if (IsDlgButtonChecked(hwnd, IDC_EXPORT_FORMAT_CMS))
5855 data->contextInfo.dwExportFormat =
5856 CRYPTUI_WIZ_EXPORT_FORMAT_PKCS7;
5857 if (IsDlgButtonChecked(hwnd, IDC_EXPORT_CMS_INCLUDE_CHAIN))
5858 data->contextInfo.fExportChain =
5859 CRYPTUI_WIZ_EXPORT_FORMAT_PKCS7;
5861 else
5863 data->contextInfo.dwExportFormat =
5864 CRYPTUI_WIZ_EXPORT_FORMAT_PFX;
5865 if (IsDlgButtonChecked(hwnd, IDC_EXPORT_PFX_INCLUDE_CHAIN))
5866 data->contextInfo.fExportChain = TRUE;
5867 if (IsDlgButtonChecked(hwnd, IDC_EXPORT_PFX_STRONG_ENCRYPTION))
5868 data->contextInfo.fStrongEncryption = TRUE;
5869 if (IsDlgButtonChecked(hwnd, IDC_EXPORT_PFX_DELETE_PRIVATE_KEY))
5870 data->deleteKeys = TRUE;
5871 skipPasswordPage = FALSE;
5873 SetWindowLongPtrW(hwnd, DWLP_MSGRESULT,
5874 skipPasswordPage ? IDD_EXPORT_FILE : 0);
5875 ret = 1;
5876 break;
5879 break;
5881 case WM_COMMAND:
5882 switch (HIWORD(wp))
5884 case BN_CLICKED:
5885 switch (LOWORD(wp))
5887 case IDC_EXPORT_FORMAT_DER:
5888 case IDC_EXPORT_FORMAT_BASE64:
5889 EnableWindow(GetDlgItem(hwnd, IDC_EXPORT_CMS_INCLUDE_CHAIN),
5890 FALSE);
5891 EnableWindow(GetDlgItem(hwnd, IDC_EXPORT_PFX_INCLUDE_CHAIN),
5892 FALSE);
5893 EnableWindow(GetDlgItem(hwnd, IDC_EXPORT_PFX_STRONG_ENCRYPTION),
5894 FALSE);
5895 EnableWindow(GetDlgItem(hwnd,
5896 IDC_EXPORT_PFX_DELETE_PRIVATE_KEY), FALSE);
5897 break;
5898 case IDC_EXPORT_FORMAT_CMS:
5899 EnableWindow(GetDlgItem(hwnd, IDC_EXPORT_CMS_INCLUDE_CHAIN),
5900 TRUE);
5901 break;
5902 case IDC_EXPORT_FORMAT_PFX:
5903 EnableWindow(GetDlgItem(hwnd, IDC_EXPORT_PFX_INCLUDE_CHAIN),
5904 TRUE);
5905 EnableWindow(GetDlgItem(hwnd, IDC_EXPORT_PFX_STRONG_ENCRYPTION),
5906 TRUE);
5907 EnableWindow(GetDlgItem(hwnd,
5908 IDC_EXPORT_PFX_DELETE_PRIVATE_KEY), TRUE);
5909 break;
5911 break;
5913 break;
5915 return ret;
5918 static void export_password_mismatch(HWND hwnd, const struct ExportWizData *data)
5920 WCHAR title[MAX_STRING_LEN], error[MAX_STRING_LEN];
5921 LPCWSTR pTitle;
5923 if (data->pwszWizardTitle)
5924 pTitle = data->pwszWizardTitle;
5925 else
5927 LoadStringW(hInstance, IDS_EXPORT_WIZARD, title, ARRAY_SIZE(title));
5928 pTitle = title;
5930 LoadStringW(hInstance, IDS_EXPORT_PASSWORD_MISMATCH, error, ARRAY_SIZE(error));
5931 MessageBoxW(hwnd, error, pTitle, MB_ICONERROR | MB_OK);
5932 SetFocus(GetDlgItem(hwnd, IDC_EXPORT_PASSWORD));
5935 static LRESULT CALLBACK export_password_dlg_proc(HWND hwnd, UINT msg,
5936 WPARAM wp, LPARAM lp)
5938 LRESULT ret = 0;
5939 struct ExportWizData *data;
5941 switch (msg)
5943 case WM_INITDIALOG:
5945 PROPSHEETPAGEW *page = (PROPSHEETPAGEW *)lp;
5947 data = (struct ExportWizData *)page->lParam;
5948 SetWindowLongPtrW(hwnd, DWLP_USER, (LPARAM)data);
5949 break;
5951 case WM_NOTIFY:
5953 NMHDR *hdr = (NMHDR *)lp;
5955 switch (hdr->code)
5957 case PSN_SETACTIVE:
5958 PostMessageW(GetParent(hwnd), PSM_SETWIZBUTTONS, 0,
5959 PSWIZB_BACK | PSWIZB_NEXT);
5960 ret = TRUE;
5961 break;
5962 case PSN_WIZNEXT:
5964 HWND passwordEdit = GetDlgItem(hwnd, IDC_EXPORT_PASSWORD);
5965 HWND passwordConfirmEdit = GetDlgItem(hwnd,
5966 IDC_EXPORT_PASSWORD_CONFIRM);
5967 DWORD passwordLen = SendMessageW(passwordEdit, WM_GETTEXTLENGTH,
5968 0, 0);
5969 DWORD passwordConfirmLen = SendMessageW(passwordConfirmEdit,
5970 WM_GETTEXTLENGTH, 0, 0);
5972 data = (struct ExportWizData *)GetWindowLongPtrW(hwnd, DWLP_USER);
5973 if (!passwordLen && !passwordConfirmLen)
5974 data->contextInfo.pwszPassword = NULL;
5975 else if (passwordLen != passwordConfirmLen)
5977 export_password_mismatch(hwnd, data);
5978 SetWindowLongPtrW(hwnd, DWLP_MSGRESULT, 1);
5979 ret = 1;
5981 else
5983 LPWSTR password = HeapAlloc(GetProcessHeap(), 0,
5984 (passwordLen + 1) * sizeof(WCHAR));
5985 LPWSTR passwordConfirm = HeapAlloc(GetProcessHeap(), 0,
5986 (passwordConfirmLen + 1) * sizeof(WCHAR));
5987 BOOL freePassword = TRUE;
5989 if (password && passwordConfirm)
5991 SendMessageW(passwordEdit, WM_GETTEXT, passwordLen + 1,
5992 (LPARAM)password);
5993 SendMessageW(passwordConfirmEdit, WM_GETTEXT,
5994 passwordConfirmLen + 1, (LPARAM)passwordConfirm);
5995 if (lstrcmpW(password, passwordConfirm))
5997 export_password_mismatch(hwnd, data);
5998 SetWindowLongPtrW(hwnd, DWLP_MSGRESULT, 1);
5999 ret = 1;
6001 else
6003 data->contextInfo.pwszPassword = password;
6004 freePassword = FALSE;
6005 data->freePassword = TRUE;
6008 if (freePassword)
6009 HeapFree(GetProcessHeap(), 0, password);
6010 HeapFree(GetProcessHeap(), 0, passwordConfirm);
6012 break;
6015 break;
6018 return ret;
6021 static LPWSTR export_append_extension(const struct ExportWizData *data,
6022 LPWSTR fileName)
6024 static const WCHAR cer[] = { '.','c','e','r',0 };
6025 static const WCHAR crl[] = { '.','c','r','l',0 };
6026 static const WCHAR ctl[] = { '.','c','t','l',0 };
6027 static const WCHAR p7b[] = { '.','p','7','b',0 };
6028 static const WCHAR pfx[] = { '.','p','f','x',0 };
6029 static const WCHAR sst[] = { '.','s','s','t',0 };
6030 LPCWSTR extension;
6031 LPWSTR dot;
6032 BOOL appendExtension;
6034 switch (data->contextInfo.dwExportFormat)
6036 case CRYPTUI_WIZ_EXPORT_FORMAT_PKCS7:
6037 extension = p7b;
6038 break;
6039 case CRYPTUI_WIZ_EXPORT_FORMAT_PFX:
6040 extension = pfx;
6041 break;
6042 default:
6043 switch (data->exportInfo.dwSubjectChoice)
6045 case CRYPTUI_WIZ_EXPORT_CRL_CONTEXT:
6046 extension = crl;
6047 break;
6048 case CRYPTUI_WIZ_EXPORT_CTL_CONTEXT:
6049 extension = ctl;
6050 break;
6051 case CRYPTUI_WIZ_EXPORT_CERT_STORE:
6052 extension = sst;
6053 break;
6054 default:
6055 extension = cer;
6058 dot = wcsrchr(fileName, '.');
6059 if (dot)
6060 appendExtension = wcsicmp(dot, extension) != 0;
6061 else
6062 appendExtension = TRUE;
6063 if (appendExtension)
6065 fileName = HeapReAlloc(GetProcessHeap(), 0, fileName,
6066 (lstrlenW(fileName) + lstrlenW(extension) + 1) * sizeof(WCHAR));
6067 if (fileName)
6068 lstrcatW(fileName, extension);
6070 return fileName;
6073 static BOOL export_validate_filename(HWND hwnd, struct ExportWizData *data,
6074 LPCWSTR fileName)
6076 HANDLE file;
6077 BOOL tryCreate = TRUE, forceCreate = FALSE, ret = FALSE;
6079 file = CreateFileW(fileName, GENERIC_WRITE,
6080 FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, OPEN_EXISTING, 0, NULL);
6081 if (file != INVALID_HANDLE_VALUE)
6083 WCHAR warning[MAX_STRING_LEN], title[MAX_STRING_LEN];
6084 LPCWSTR pTitle;
6086 if (data->pwszWizardTitle)
6087 pTitle = data->pwszWizardTitle;
6088 else
6090 LoadStringW(hInstance, IDS_EXPORT_WIZARD, title, ARRAY_SIZE(title));
6091 pTitle = title;
6093 LoadStringW(hInstance, IDS_EXPORT_FILE_EXISTS, warning, ARRAY_SIZE(warning));
6094 if (MessageBoxW(hwnd, warning, pTitle, MB_YESNO) == IDYES)
6095 forceCreate = TRUE;
6096 else
6097 tryCreate = FALSE;
6098 CloseHandle(file);
6100 if (tryCreate)
6102 file = CreateFileW(fileName, GENERIC_WRITE,
6103 FILE_SHARE_READ | FILE_SHARE_WRITE, NULL,
6104 forceCreate ? CREATE_ALWAYS : CREATE_NEW,
6105 0, NULL);
6106 if (file != INVALID_HANDLE_VALUE)
6108 data->file = file;
6109 ret = TRUE;
6111 else
6113 WCHAR title[MAX_STRING_LEN], error[MAX_STRING_LEN];
6114 LPCWSTR pTitle;
6115 LPWSTR msgBuf, fullError;
6117 if (data->pwszWizardTitle)
6118 pTitle = data->pwszWizardTitle;
6119 else
6121 LoadStringW(hInstance, IDS_EXPORT_WIZARD, title, ARRAY_SIZE(title));
6122 pTitle = title;
6124 LoadStringW(hInstance, IDS_IMPORT_OPEN_FAILED, error, ARRAY_SIZE(error));
6125 FormatMessageW(
6126 FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM, NULL,
6127 GetLastError(), 0, (LPWSTR) &msgBuf, 0, NULL);
6128 fullError = HeapAlloc(GetProcessHeap(), 0,
6129 (lstrlenW(error) + lstrlenW(fileName) + lstrlenW(msgBuf) + 3)
6130 * sizeof(WCHAR));
6131 if (fullError)
6133 LPWSTR ptr = fullError;
6135 lstrcpyW(ptr, error);
6136 ptr += lstrlenW(error);
6137 lstrcpyW(ptr, fileName);
6138 ptr += lstrlenW(fileName);
6139 *ptr++ = ':';
6140 *ptr++ = '\n';
6141 lstrcpyW(ptr, msgBuf);
6142 MessageBoxW(hwnd, fullError, pTitle, MB_ICONERROR | MB_OK);
6143 HeapFree(GetProcessHeap(), 0, fullError);
6145 LocalFree(msgBuf);
6148 return ret;
6151 static const WCHAR export_filter_cert[] = { '*','.','c','e','r',0 };
6152 static const WCHAR export_filter_crl[] = { '*','.','c','r','l',0 };
6153 static const WCHAR export_filter_ctl[] = { '*','.','s','t','l',0 };
6154 static const WCHAR export_filter_cms[] = { '*','.','p','7','b',0 };
6155 static const WCHAR export_filter_pfx[] = { '*','.','p','f','x',0 };
6156 static const WCHAR export_filter_sst[] = { '*','.','s','s','t',0 };
6158 static WCHAR *make_export_file_filter(DWORD exportFormat, DWORD subjectChoice)
6160 int baseLen, allLen, totalLen = 2, baseID;
6161 LPWSTR filter = NULL, baseFilter, all;
6162 LPCWSTR filterStr;
6164 switch (exportFormat)
6166 case CRYPTUI_WIZ_EXPORT_FORMAT_BASE64:
6167 baseID = IDS_EXPORT_FILTER_BASE64_CERT;
6168 filterStr = export_filter_cert;
6169 break;
6170 case CRYPTUI_WIZ_EXPORT_FORMAT_PFX:
6171 baseID = IDS_EXPORT_FILTER_PFX;
6172 filterStr = export_filter_pfx;
6173 break;
6174 case CRYPTUI_WIZ_EXPORT_FORMAT_PKCS7:
6175 baseID = IDS_EXPORT_FILTER_CMS;
6176 filterStr = export_filter_cms;
6177 break;
6178 default:
6179 switch (subjectChoice)
6181 case CRYPTUI_WIZ_EXPORT_CRL_CONTEXT:
6182 baseID = IDS_EXPORT_FILTER_CRL;
6183 filterStr = export_filter_crl;
6184 break;
6185 case CRYPTUI_WIZ_EXPORT_CTL_CONTEXT:
6186 baseID = IDS_EXPORT_FILTER_CTL;
6187 filterStr = export_filter_ctl;
6188 break;
6189 case CRYPTUI_WIZ_EXPORT_CERT_STORE:
6190 baseID = IDS_EXPORT_FILTER_SERIALIZED_CERT_STORE;
6191 filterStr = export_filter_sst;
6192 break;
6193 default:
6194 baseID = IDS_EXPORT_FILTER_CERT;
6195 filterStr = export_filter_cert;
6196 break;
6199 baseLen = LoadStringW(hInstance, baseID, (LPWSTR)&baseFilter, 0);
6200 totalLen += baseLen + lstrlenW(filterStr) + 2;
6201 allLen = LoadStringW(hInstance, IDS_IMPORT_FILTER_ALL, (LPWSTR)&all, 0);
6202 totalLen += allLen + lstrlenW(filter_all) + 2;
6203 filter = HeapAlloc(GetProcessHeap(), 0, totalLen * sizeof(WCHAR));
6204 if (filter)
6206 LPWSTR ptr;
6208 ptr = filter;
6209 memcpy(ptr, baseFilter, baseLen * sizeof(WCHAR));
6210 ptr += baseLen;
6211 *ptr++ = 0;
6212 lstrcpyW(ptr, filterStr);
6213 ptr += lstrlenW(filterStr) + 1;
6214 memcpy(ptr, all, allLen * sizeof(WCHAR));
6215 ptr += allLen;
6216 *ptr++ = 0;
6217 lstrcpyW(ptr, filter_all);
6218 ptr += lstrlenW(filter_all) + 1;
6219 *ptr++ = 0;
6221 return filter;
6224 static LRESULT CALLBACK export_file_dlg_proc(HWND hwnd, UINT msg, WPARAM wp,
6225 LPARAM lp)
6227 LRESULT ret = 0;
6228 struct ExportWizData *data;
6230 switch (msg)
6232 case WM_INITDIALOG:
6234 PROPSHEETPAGEW *page = (PROPSHEETPAGEW *)lp;
6236 data = (struct ExportWizData *)page->lParam;
6237 SetWindowLongPtrW(hwnd, DWLP_USER, (LPARAM)data);
6238 if (data->exportInfo.pwszExportFileName)
6239 SendMessageW(GetDlgItem(hwnd, IDC_EXPORT_FILENAME), WM_SETTEXT, 0,
6240 (LPARAM)data->exportInfo.pwszExportFileName);
6241 break;
6243 case WM_NOTIFY:
6245 NMHDR *hdr = (NMHDR *)lp;
6247 switch (hdr->code)
6249 case PSN_WIZBACK:
6250 data = (struct ExportWizData *)GetWindowLongPtrW(hwnd, DWLP_USER);
6251 if (data->contextInfo.dwExportFormat !=
6252 CRYPTUI_WIZ_EXPORT_FORMAT_PFX)
6254 SetWindowLongPtrW(hwnd, DWLP_MSGRESULT, IDD_EXPORT_FORMAT);
6255 ret = 1;
6257 break;
6258 case PSN_WIZNEXT:
6260 HWND fileNameEdit = GetDlgItem(hwnd, IDC_EXPORT_FILENAME);
6261 DWORD len = SendMessageW(fileNameEdit, WM_GETTEXTLENGTH, 0, 0);
6263 data = (struct ExportWizData *)GetWindowLongPtrW(hwnd, DWLP_USER);
6264 if (!len)
6266 WCHAR title[MAX_STRING_LEN], error[MAX_STRING_LEN];
6267 LPCWSTR pTitle;
6269 if (data->pwszWizardTitle)
6270 pTitle = data->pwszWizardTitle;
6271 else
6273 LoadStringW(hInstance, IDS_EXPORT_WIZARD, title, ARRAY_SIZE(title));
6274 pTitle = title;
6276 LoadStringW(hInstance, IDS_IMPORT_EMPTY_FILE, error, ARRAY_SIZE(error));
6277 MessageBoxW(hwnd, error, pTitle, MB_ICONERROR | MB_OK);
6278 SetWindowLongPtrW(hwnd, DWLP_MSGRESULT, 1);
6279 ret = 1;
6281 else
6283 LPWSTR fileName = HeapAlloc(GetProcessHeap(), 0,
6284 (len + 1) * sizeof(WCHAR));
6286 if (fileName)
6288 SendMessageW(fileNameEdit, WM_GETTEXT, len + 1,
6289 (LPARAM)fileName);
6290 fileName = export_append_extension(data, fileName);
6291 if (!export_validate_filename(hwnd, data, fileName))
6293 HeapFree(GetProcessHeap(), 0, fileName);
6294 SetWindowLongPtrW(hwnd, DWLP_MSGRESULT, 1);
6295 ret = 1;
6297 else
6298 data->fileName = fileName;
6301 break;
6303 case PSN_SETACTIVE:
6304 PostMessageW(GetParent(hwnd), PSM_SETWIZBUTTONS, 0,
6305 PSWIZB_BACK | PSWIZB_NEXT);
6306 ret = TRUE;
6307 break;
6309 break;
6311 case WM_COMMAND:
6312 switch (wp)
6314 case IDC_EXPORT_BROWSE_FILE:
6316 OPENFILENAMEW ofn;
6317 WCHAR fileBuf[MAX_PATH];
6319 data = (struct ExportWizData *)GetWindowLongPtrW(hwnd, DWLP_USER);
6320 memset(&ofn, 0, sizeof(ofn));
6321 ofn.lStructSize = sizeof(ofn);
6322 ofn.hwndOwner = hwnd;
6323 ofn.lpstrFilter = make_export_file_filter(
6324 data->contextInfo.dwExportFormat,
6325 data->exportInfo.dwSubjectChoice);
6326 ofn.lpstrFile = fileBuf;
6327 ofn.nMaxFile = ARRAY_SIZE(fileBuf);
6328 fileBuf[0] = 0;
6329 if (GetSaveFileNameW(&ofn))
6330 SendMessageW(GetDlgItem(hwnd, IDC_EXPORT_FILENAME), WM_SETTEXT,
6331 0, (LPARAM)ofn.lpstrFile);
6332 HeapFree(GetProcessHeap(), 0, (LPWSTR)ofn.lpstrFilter);
6333 break;
6336 break;
6338 return ret;
6341 static void show_export_details(HWND lv, const struct ExportWizData *data)
6343 WCHAR text[MAX_STRING_LEN];
6344 LVITEMW item;
6345 int contentID;
6347 item.mask = LVIF_TEXT;
6348 if (data->fileName)
6350 item.iItem = SendMessageW(lv, LVM_GETITEMCOUNT, 0, 0);
6351 item.iSubItem = 0;
6352 LoadStringW(hInstance, IDS_IMPORT_FILE, text, ARRAY_SIZE(text));
6353 item.pszText = text;
6354 SendMessageW(lv, LVM_INSERTITEMW, 0, (LPARAM)&item);
6355 item.iSubItem = 1;
6356 item.pszText = data->fileName;
6357 SendMessageW(lv, LVM_SETITEMTEXTW, item.iItem, (LPARAM)&item);
6360 item.pszText = text;
6361 switch (data->exportInfo.dwSubjectChoice)
6363 case CRYPTUI_WIZ_EXPORT_CRL_CONTEXT:
6364 case CRYPTUI_WIZ_EXPORT_CTL_CONTEXT:
6365 case CRYPTUI_WIZ_EXPORT_CERT_STORE:
6366 case CRYPTUI_WIZ_EXPORT_CERT_STORE_CERTIFICATES_ONLY:
6367 /* do nothing */
6368 break;
6369 default:
6371 item.iItem = SendMessageW(lv, LVM_GETITEMCOUNT, 0, 0);
6372 item.iSubItem = 0;
6373 LoadStringW(hInstance, IDS_EXPORT_INCLUDE_CHAIN, text, ARRAY_SIZE(text));
6374 SendMessageW(lv, LVM_INSERTITEMW, item.iItem, (LPARAM)&item);
6375 item.iSubItem = 1;
6376 LoadStringW(hInstance, data->contextInfo.fExportChain ? IDS_YES : IDS_NO, text,
6377 ARRAY_SIZE(text));
6378 SendMessageW(lv, LVM_SETITEMTEXTW, item.iItem, (LPARAM)&item);
6380 item.iItem = SendMessageW(lv, LVM_GETITEMCOUNT, 0, 0);
6381 item.iSubItem = 0;
6382 LoadStringW(hInstance, IDS_EXPORT_KEYS, text, ARRAY_SIZE(text));
6383 SendMessageW(lv, LVM_INSERTITEMW, item.iItem, (LPARAM)&item);
6384 item.iSubItem = 1;
6385 LoadStringW(hInstance, data->contextInfo.fExportPrivateKeys ? IDS_YES : IDS_NO, text,
6386 ARRAY_SIZE(text));
6387 SendMessageW(lv, LVM_SETITEMTEXTW, item.iItem, (LPARAM)&item);
6391 item.iItem = SendMessageW(lv, LVM_GETITEMCOUNT, 0, 0);
6392 item.iSubItem = 0;
6393 LoadStringW(hInstance, IDS_EXPORT_FORMAT, text, ARRAY_SIZE(text));
6394 SendMessageW(lv, LVM_INSERTITEMW, 0, (LPARAM)&item);
6396 item.iSubItem = 1;
6397 switch (data->exportInfo.dwSubjectChoice)
6399 case CRYPTUI_WIZ_EXPORT_CRL_CONTEXT:
6400 contentID = IDS_EXPORT_FILTER_CRL;
6401 break;
6402 case CRYPTUI_WIZ_EXPORT_CTL_CONTEXT:
6403 contentID = IDS_EXPORT_FILTER_CTL;
6404 break;
6405 case CRYPTUI_WIZ_EXPORT_CERT_STORE:
6406 contentID = IDS_EXPORT_FILTER_SERIALIZED_CERT_STORE;
6407 break;
6408 default:
6409 switch (data->contextInfo.dwExportFormat)
6411 case CRYPTUI_WIZ_EXPORT_FORMAT_BASE64:
6412 contentID = IDS_EXPORT_FILTER_BASE64_CERT;
6413 break;
6414 case CRYPTUI_WIZ_EXPORT_FORMAT_PKCS7:
6415 contentID = IDS_EXPORT_FILTER_CMS;
6416 break;
6417 case CRYPTUI_WIZ_EXPORT_FORMAT_PFX:
6418 contentID = IDS_EXPORT_FILTER_PFX;
6419 break;
6420 default:
6421 contentID = IDS_EXPORT_FILTER_CERT;
6424 LoadStringW(hInstance, contentID, text, ARRAY_SIZE(text));
6425 SendMessageW(lv, LVM_SETITEMTEXTW, item.iItem, (LPARAM)&item);
6428 static inline BOOL save_der(HANDLE file, const BYTE *pb, DWORD cb)
6430 DWORD bytesWritten;
6432 return WriteFile(file, pb, cb, &bytesWritten, NULL);
6435 static BOOL save_base64(HANDLE file, const BYTE *pb, DWORD cb)
6437 BOOL ret;
6438 DWORD size = 0;
6440 if ((ret = CryptBinaryToStringA(pb, cb, CRYPT_STRING_BASE64, NULL, &size)))
6442 LPSTR buf = HeapAlloc(GetProcessHeap(), 0, size);
6444 if (buf)
6446 if ((ret = CryptBinaryToStringA(pb, cb, CRYPT_STRING_BASE64, buf,
6447 &size)))
6448 ret = WriteFile(file, buf, size, &size, NULL);
6449 HeapFree(GetProcessHeap(), 0, buf);
6451 else
6453 SetLastError(ERROR_OUTOFMEMORY);
6454 ret = FALSE;
6457 return ret;
6460 static inline BOOL save_store_as_cms(HANDLE file, HCERTSTORE store)
6462 return CertSaveStore(store, PKCS_7_ASN_ENCODING | X509_ASN_ENCODING,
6463 CERT_STORE_SAVE_AS_PKCS7, CERT_STORE_SAVE_TO_FILE, file, 0);
6466 static BOOL save_cert_as_cms(HANDLE file, PCCRYPTUI_WIZ_EXPORT_INFO pExportInfo,
6467 BOOL includeChain)
6469 BOOL ret;
6470 HCERTSTORE store = CertOpenStore(CERT_STORE_PROV_MEMORY, 0, 0,
6471 CERT_STORE_CREATE_NEW_FLAG, NULL);
6473 if (store)
6475 if (includeChain)
6477 HCERTSTORE addlStore = CertOpenStore(CERT_STORE_PROV_COLLECTION,
6478 0, 0, CERT_STORE_CREATE_NEW_FLAG, NULL);
6480 if (addlStore)
6482 DWORD i;
6484 ret = TRUE;
6485 for (i = 0; ret && i < pExportInfo->cStores; i++)
6486 ret = CertAddStoreToCollection(addlStore,
6487 pExportInfo->rghStores, 0, 0);
6488 if (ret)
6490 PCCERT_CHAIN_CONTEXT chain;
6492 ret = CertGetCertificateChain(NULL,
6493 pExportInfo->u.pCertContext, NULL, addlStore, NULL, 0,
6494 NULL, &chain);
6495 if (ret)
6497 DWORD j;
6499 for (i = 0; ret && i < chain->cChain; i++)
6500 for (j = 0; ret && j < chain->rgpChain[i]->cElement;
6501 j++)
6502 ret = CertAddCertificateContextToStore(store,
6503 chain->rgpChain[i]->rgpElement[j]->pCertContext,
6504 CERT_STORE_ADD_ALWAYS, NULL);
6505 CertFreeCertificateChain(chain);
6507 else
6509 /* No chain could be created, just add the individual
6510 * cert to the message.
6512 ret = CertAddCertificateContextToStore(store,
6513 pExportInfo->u.pCertContext, CERT_STORE_ADD_ALWAYS,
6514 NULL);
6517 CertCloseStore(addlStore, 0);
6519 else
6520 ret = FALSE;
6522 else
6523 ret = CertAddCertificateContextToStore(store,
6524 pExportInfo->u.pCertContext, CERT_STORE_ADD_ALWAYS, NULL);
6525 if (ret)
6526 ret = save_store_as_cms(file, store);
6527 CertCloseStore(store, 0);
6529 else
6530 ret = FALSE;
6531 return ret;
6534 static BOOL save_serialized_store(HANDLE file, HCERTSTORE store)
6536 return CertSaveStore(store, PKCS_7_ASN_ENCODING | X509_ASN_ENCODING,
6537 CERT_STORE_SAVE_AS_STORE, CERT_STORE_SAVE_TO_FILE, file, 0);
6540 static BOOL save_pfx(HANDLE file, PCCRYPTUI_WIZ_EXPORT_INFO pExportInfo,
6541 PCCRYPTUI_WIZ_EXPORT_CERTCONTEXT_INFO pContextInfo,
6542 PCRYPT_KEY_PROV_INFO keyProvInfo, BOOL deleteKeys)
6544 HCERTSTORE store = CertOpenStore(CERT_STORE_PROV_MEMORY, X509_ASN_ENCODING,
6545 0, CERT_STORE_CREATE_NEW_FLAG, NULL);
6546 BOOL ret = FALSE;
6548 if (store)
6550 CRYPT_DATA_BLOB pfxBlob = { 0, NULL };
6551 PCCERT_CONTEXT cert = NULL;
6552 BOOL freeKeyProvInfo = FALSE;
6554 if (pContextInfo->fExportChain)
6556 HCERTCHAINENGINE engine = NULL;
6558 if (pExportInfo->cStores)
6560 CERT_CHAIN_ENGINE_CONFIG config;
6562 memset(&config, 0, sizeof(config));
6563 config.cbSize = sizeof(config);
6564 config.cAdditionalStore = pExportInfo->cStores;
6565 config.rghAdditionalStore = pExportInfo->rghStores;
6566 ret = CertCreateCertificateChainEngine(&config, &engine);
6568 else
6569 ret = TRUE;
6570 if (ret)
6572 CERT_CHAIN_PARA chainPara;
6573 PCCERT_CHAIN_CONTEXT chain;
6575 memset(&chainPara, 0, sizeof(chainPara));
6576 chainPara.cbSize = sizeof(chainPara);
6577 ret = CertGetCertificateChain(engine,
6578 pExportInfo->u.pCertContext, NULL, NULL, &chainPara, 0, NULL,
6579 &chain);
6580 if (ret)
6582 DWORD i, j;
6584 for (i = 0; ret && i < chain->cChain; i++)
6585 for (j = 0; ret && j < chain->rgpChain[i]->cElement;
6586 j++)
6588 if (i == 0 && j == 0)
6589 ret = CertAddCertificateContextToStore(store,
6590 chain->rgpChain[i]->rgpElement[j]->pCertContext,
6591 CERT_STORE_ADD_ALWAYS, &cert);
6592 else
6593 ret = CertAddCertificateContextToStore(store,
6594 chain->rgpChain[i]->rgpElement[j]->pCertContext,
6595 CERT_STORE_ADD_ALWAYS, NULL);
6597 CertFreeCertificateChain(chain);
6600 if (engine)
6601 CertFreeCertificateChainEngine(engine);
6603 else
6604 ret = CertAddCertificateContextToStore(store,
6605 pExportInfo->u.pCertContext, CERT_STORE_ADD_ALWAYS, &cert);
6606 /* Copy private key info to newly created cert, so it'll get exported
6607 * along with the cert.
6609 if (ret && pContextInfo->fExportPrivateKeys)
6611 if (keyProvInfo)
6612 ret = CertSetCertificateContextProperty(cert,
6613 CERT_KEY_PROV_INFO_PROP_ID, 0, keyProvInfo);
6614 else
6616 if (!(keyProvInfo = export_get_private_key_info(cert)))
6617 ret = FALSE;
6618 else
6620 ret = CertSetCertificateContextProperty(cert,
6621 CERT_KEY_PROV_INFO_PROP_ID, 0, keyProvInfo);
6622 freeKeyProvInfo = TRUE;
6626 if (ret)
6628 DWORD exportFlags =
6629 REPORT_NOT_ABLE_TO_EXPORT_PRIVATE_KEY | EXPORT_PRIVATE_KEYS;
6631 ret = PFXExportCertStore(store, &pfxBlob,
6632 pContextInfo->pwszPassword, exportFlags);
6633 if (ret)
6635 pfxBlob.pbData = HeapAlloc(GetProcessHeap(), 0, pfxBlob.cbData);
6636 if (pfxBlob.pbData)
6638 ret = PFXExportCertStore(store, &pfxBlob,
6639 pContextInfo->pwszPassword, exportFlags);
6640 if (ret)
6642 DWORD bytesWritten;
6644 ret = WriteFile(file, pfxBlob.pbData, pfxBlob.cbData,
6645 &bytesWritten, NULL);
6648 else
6650 SetLastError(ERROR_OUTOFMEMORY);
6651 ret = FALSE;
6655 if (ret && deleteKeys)
6657 HCRYPTPROV prov;
6659 CryptAcquireContextW(&prov, keyProvInfo->pwszContainerName,
6660 keyProvInfo->pwszProvName, keyProvInfo->dwProvType,
6661 CRYPT_DELETEKEYSET);
6663 if (freeKeyProvInfo)
6664 HeapFree(GetProcessHeap(), 0, keyProvInfo);
6665 CertFreeCertificateContext(cert);
6666 CertCloseStore(store, 0);
6668 return ret;
6671 static BOOL do_export(HANDLE file, PCCRYPTUI_WIZ_EXPORT_INFO pExportInfo,
6672 PCCRYPTUI_WIZ_EXPORT_CERTCONTEXT_INFO pContextInfo,
6673 PCRYPT_KEY_PROV_INFO keyProvInfo, BOOL deleteKeys)
6675 BOOL ret;
6677 if (pContextInfo->dwSize != sizeof(CRYPTUI_WIZ_EXPORT_CERTCONTEXT_INFO))
6679 SetLastError(E_INVALIDARG);
6680 return FALSE;
6682 switch (pExportInfo->dwSubjectChoice)
6684 case CRYPTUI_WIZ_EXPORT_CRL_CONTEXT:
6685 ret = save_der(file,
6686 pExportInfo->u.pCRLContext->pbCrlEncoded,
6687 pExportInfo->u.pCRLContext->cbCrlEncoded);
6688 break;
6689 case CRYPTUI_WIZ_EXPORT_CTL_CONTEXT:
6690 ret = save_der(file,
6691 pExportInfo->u.pCTLContext->pbCtlEncoded,
6692 pExportInfo->u.pCTLContext->cbCtlEncoded);
6693 break;
6694 case CRYPTUI_WIZ_EXPORT_CERT_STORE:
6695 ret = save_serialized_store(file, pExportInfo->u.hCertStore);
6696 break;
6697 case CRYPTUI_WIZ_EXPORT_CERT_STORE_CERTIFICATES_ONLY:
6698 ret = save_store_as_cms(file, pExportInfo->u.hCertStore);
6699 break;
6700 default:
6701 switch (pContextInfo->dwExportFormat)
6703 case CRYPTUI_WIZ_EXPORT_FORMAT_DER:
6704 ret = save_der(file, pExportInfo->u.pCertContext->pbCertEncoded,
6705 pExportInfo->u.pCertContext->cbCertEncoded);
6706 break;
6707 case CRYPTUI_WIZ_EXPORT_FORMAT_BASE64:
6708 ret = save_base64(file,
6709 pExportInfo->u.pCertContext->pbCertEncoded,
6710 pExportInfo->u.pCertContext->cbCertEncoded);
6711 break;
6712 case CRYPTUI_WIZ_EXPORT_FORMAT_PKCS7:
6713 ret = save_cert_as_cms(file, pExportInfo,
6714 pContextInfo->fExportChain);
6715 break;
6716 case CRYPTUI_WIZ_EXPORT_FORMAT_PFX:
6717 ret = save_pfx(file, pExportInfo, pContextInfo, keyProvInfo,
6718 deleteKeys);
6719 break;
6720 default:
6721 SetLastError(E_FAIL);
6722 ret = FALSE;
6725 return ret;
6728 static LRESULT CALLBACK export_finish_dlg_proc(HWND hwnd, UINT msg, WPARAM wp,
6729 LPARAM lp)
6731 LRESULT ret = 0;
6732 struct ExportWizData *data;
6734 switch (msg)
6736 case WM_INITDIALOG:
6738 PROPSHEETPAGEW *page = (PROPSHEETPAGEW *)lp;
6739 HWND lv = GetDlgItem(hwnd, IDC_EXPORT_SETTINGS);
6740 RECT rc;
6741 LVCOLUMNW column;
6743 data = (struct ExportWizData *)page->lParam;
6744 SetWindowLongPtrW(hwnd, DWLP_USER, (LPARAM)data);
6745 SendMessageW(GetDlgItem(hwnd, IDC_EXPORT_TITLE), WM_SETFONT,
6746 (WPARAM)data->titleFont, TRUE);
6747 GetWindowRect(lv, &rc);
6748 column.mask = LVCF_WIDTH;
6749 column.cx = (rc.right - rc.left) / 2 - 2;
6750 SendMessageW(lv, LVM_INSERTCOLUMNW, 0, (LPARAM)&column);
6751 SendMessageW(lv, LVM_INSERTCOLUMNW, 1, (LPARAM)&column);
6752 show_export_details(lv, data);
6753 break;
6755 case WM_NOTIFY:
6757 NMHDR *hdr = (NMHDR *)lp;
6759 switch (hdr->code)
6761 case PSN_SETACTIVE:
6763 HWND lv = GetDlgItem(hwnd, IDC_EXPORT_SETTINGS);
6765 data = (struct ExportWizData *)GetWindowLongPtrW(hwnd, DWLP_USER);
6766 SendMessageW(lv, LVM_DELETEALLITEMS, 0, 0);
6767 show_export_details(lv, data);
6768 PostMessageW(GetParent(hwnd), PSM_SETWIZBUTTONS, 0,
6769 PSWIZB_BACK | PSWIZB_FINISH);
6770 ret = TRUE;
6771 break;
6773 case PSN_WIZFINISH:
6775 int messageID;
6776 WCHAR title[MAX_STRING_LEN], message[MAX_STRING_LEN];
6777 LPCWSTR pTitle;
6778 DWORD mbFlags;
6780 data = (struct ExportWizData *)GetWindowLongPtrW(hwnd, DWLP_USER);
6781 if ((data->success = do_export(data->file, &data->exportInfo,
6782 &data->contextInfo, data->keyProvInfo, data->deleteKeys)))
6784 messageID = IDS_EXPORT_SUCCEEDED;
6785 mbFlags = MB_OK;
6787 else
6789 messageID = IDS_EXPORT_FAILED;
6790 mbFlags = MB_OK | MB_ICONERROR;
6792 if (data->pwszWizardTitle)
6793 pTitle = data->pwszWizardTitle;
6794 else
6796 LoadStringW(hInstance, IDS_EXPORT_WIZARD, title, ARRAY_SIZE(title));
6797 pTitle = title;
6799 LoadStringW(hInstance, messageID, message, ARRAY_SIZE(message));
6800 MessageBoxW(hwnd, message, pTitle, mbFlags);
6801 break;
6804 break;
6807 return ret;
6810 static BOOL show_export_ui(DWORD dwFlags, HWND hwndParent,
6811 LPCWSTR pwszWizardTitle, PCCRYPTUI_WIZ_EXPORT_INFO pExportInfo, const void *pvoid)
6813 PROPSHEETHEADERW hdr;
6814 PROPSHEETPAGEW pages[6];
6815 struct ExportWizData data;
6816 int nPages = 0;
6817 BOOL hasPrivateKey, showFormatPage = TRUE;
6818 INT_PTR l;
6820 data.dwFlags = dwFlags;
6821 data.pwszWizardTitle = pwszWizardTitle;
6822 memset(&data.exportInfo, 0, sizeof(data.exportInfo));
6823 memcpy(&data.exportInfo, pExportInfo,
6824 min(sizeof(data.exportInfo), pExportInfo->dwSize));
6825 if (pExportInfo->dwSize > sizeof(data.exportInfo))
6826 data.exportInfo.dwSize = sizeof(data.exportInfo);
6827 data.contextInfo.dwSize = sizeof(data.contextInfo);
6828 data.contextInfo.dwExportFormat = CRYPTUI_WIZ_EXPORT_FORMAT_DER;
6829 data.contextInfo.fExportChain = FALSE;
6830 data.contextInfo.fStrongEncryption = FALSE;
6831 data.contextInfo.fExportPrivateKeys = FALSE;
6832 data.contextInfo.pwszPassword = NULL;
6833 data.freePassword = FALSE;
6834 if (pExportInfo->dwSubjectChoice == CRYPTUI_WIZ_EXPORT_CERT_CONTEXT &&
6835 pvoid)
6836 memcpy(&data.contextInfo, pvoid,
6837 min(((PCCRYPTUI_WIZ_EXPORT_CERTCONTEXT_INFO)pvoid)->dwSize,
6838 sizeof(data.contextInfo)));
6839 data.keyProvInfo = NULL;
6840 data.deleteKeys = FALSE;
6841 data.fileName = NULL;
6842 data.file = INVALID_HANDLE_VALUE;
6843 data.success = FALSE;
6845 memset(pages, 0, sizeof(pages));
6847 pages[nPages].dwSize = sizeof(pages[0]);
6848 pages[nPages].hInstance = hInstance;
6849 pages[nPages].u.pszTemplate = MAKEINTRESOURCEW(IDD_EXPORT_WELCOME);
6850 pages[nPages].pfnDlgProc = export_welcome_dlg_proc;
6851 pages[nPages].dwFlags = PSP_HIDEHEADER;
6852 pages[nPages].lParam = (LPARAM)&data;
6853 nPages++;
6855 hasPrivateKey = export_info_has_private_key(pExportInfo);
6856 switch (pExportInfo->dwSubjectChoice)
6858 case CRYPTUI_WIZ_EXPORT_CRL_CONTEXT:
6859 case CRYPTUI_WIZ_EXPORT_CTL_CONTEXT:
6860 showFormatPage = FALSE;
6861 data.contextInfo.dwExportFormat = CRYPTUI_WIZ_EXPORT_FORMAT_DER;
6862 break;
6863 case CRYPTUI_WIZ_EXPORT_CERT_STORE:
6864 showFormatPage = FALSE;
6865 data.contextInfo.dwExportFormat =
6866 CRYPTUI_WIZ_EXPORT_FORMAT_SERIALIZED_CERT_STORE;
6867 break;
6868 case CRYPTUI_WIZ_EXPORT_CERT_STORE_CERTIFICATES_ONLY:
6869 showFormatPage = FALSE;
6870 data.contextInfo.dwExportFormat = CRYPTUI_WIZ_EXPORT_FORMAT_PKCS7;
6871 break;
6874 if (hasPrivateKey && showFormatPage)
6876 pages[nPages].dwSize = sizeof(pages[0]);
6877 pages[nPages].hInstance = hInstance;
6878 pages[nPages].u.pszTemplate = MAKEINTRESOURCEW(IDD_EXPORT_PRIVATE_KEY);
6879 pages[nPages].pfnDlgProc = export_private_key_dlg_proc;
6880 pages[nPages].dwFlags = PSP_USEHEADERTITLE | PSP_USEHEADERSUBTITLE;
6881 pages[nPages].pszHeaderTitle =
6882 MAKEINTRESOURCEW(IDS_EXPORT_PRIVATE_KEY_TITLE);
6883 pages[nPages].pszHeaderSubTitle =
6884 MAKEINTRESOURCEW(IDS_EXPORT_PRIVATE_KEY_SUBTITLE);
6885 pages[nPages].lParam = (LPARAM)&data;
6886 nPages++;
6888 if (showFormatPage)
6890 pages[nPages].dwSize = sizeof(pages[0]);
6891 pages[nPages].hInstance = hInstance;
6892 pages[nPages].u.pszTemplate = MAKEINTRESOURCEW(IDD_EXPORT_FORMAT);
6893 pages[nPages].pfnDlgProc = export_format_dlg_proc;
6894 pages[nPages].dwFlags = PSP_USEHEADERTITLE | PSP_USEHEADERSUBTITLE;
6895 pages[nPages].pszHeaderTitle =
6896 MAKEINTRESOURCEW(IDS_EXPORT_FORMAT_TITLE);
6897 pages[nPages].pszHeaderSubTitle =
6898 MAKEINTRESOURCEW(IDS_EXPORT_FORMAT_SUBTITLE);
6899 pages[nPages].lParam = (LPARAM)&data;
6900 nPages++;
6902 if (hasPrivateKey && showFormatPage)
6904 pages[nPages].dwSize = sizeof(pages[0]);
6905 pages[nPages].hInstance = hInstance;
6906 pages[nPages].u.pszTemplate = MAKEINTRESOURCEW(IDD_EXPORT_PASSWORD);
6907 pages[nPages].pfnDlgProc = export_password_dlg_proc;
6908 pages[nPages].dwFlags = PSP_USEHEADERTITLE | PSP_USEHEADERSUBTITLE;
6909 pages[nPages].pszHeaderTitle =
6910 MAKEINTRESOURCEW(IDS_EXPORT_PASSWORD_TITLE);
6911 pages[nPages].pszHeaderSubTitle =
6912 MAKEINTRESOURCEW(IDS_EXPORT_PASSWORD_SUBTITLE);
6913 pages[nPages].lParam = (LPARAM)&data;
6914 nPages++;
6917 pages[nPages].dwSize = sizeof(pages[0]);
6918 pages[nPages].hInstance = hInstance;
6919 pages[nPages].u.pszTemplate = MAKEINTRESOURCEW(IDD_EXPORT_FILE);
6920 pages[nPages].pfnDlgProc = export_file_dlg_proc;
6921 pages[nPages].dwFlags = PSP_USEHEADERTITLE | PSP_USEHEADERSUBTITLE;
6922 pages[nPages].pszHeaderTitle = MAKEINTRESOURCEW(IDS_EXPORT_FILE_TITLE);
6923 pages[nPages].pszHeaderSubTitle =
6924 MAKEINTRESOURCEW(IDS_EXPORT_FILE_SUBTITLE);
6925 pages[nPages].lParam = (LPARAM)&data;
6926 nPages++;
6928 pages[nPages].dwSize = sizeof(pages[0]);
6929 pages[nPages].hInstance = hInstance;
6930 pages[nPages].u.pszTemplate = MAKEINTRESOURCEW(IDD_EXPORT_FINISH);
6931 pages[nPages].pfnDlgProc = export_finish_dlg_proc;
6932 pages[nPages].dwFlags = PSP_HIDEHEADER;
6933 pages[nPages].lParam = (LPARAM)&data;
6934 nPages++;
6936 memset(&hdr, 0, sizeof(hdr));
6937 hdr.dwSize = sizeof(hdr);
6938 hdr.hwndParent = hwndParent;
6939 hdr.dwFlags = PSH_PROPSHEETPAGE | PSH_WIZARD97_NEW | PSH_HEADER |
6940 PSH_WATERMARK;
6941 hdr.hInstance = hInstance;
6942 if (pwszWizardTitle)
6943 hdr.pszCaption = pwszWizardTitle;
6944 else
6945 hdr.pszCaption = MAKEINTRESOURCEW(IDS_EXPORT_WIZARD);
6946 hdr.u3.ppsp = pages;
6947 hdr.nPages = nPages;
6948 hdr.u4.pszbmWatermark = MAKEINTRESOURCEW(IDB_CERT_WATERMARK);
6949 hdr.u5.pszbmHeader = MAKEINTRESOURCEW(IDB_CERT_HEADER);
6950 l = PropertySheetW(&hdr);
6951 DeleteObject(data.titleFont);
6952 if (data.freePassword)
6953 HeapFree(GetProcessHeap(), 0,
6954 (LPWSTR)data.contextInfo.pwszPassword);
6955 HeapFree(GetProcessHeap(), 0, data.keyProvInfo);
6956 CloseHandle(data.file);
6957 HeapFree(GetProcessHeap(), 0, data.fileName);
6958 if (l == 0)
6960 SetLastError(ERROR_CANCELLED);
6961 return FALSE;
6963 else
6964 return data.success;
6967 BOOL WINAPI CryptUIWizExport(DWORD dwFlags, HWND hwndParent,
6968 LPCWSTR pwszWizardTitle, PCCRYPTUI_WIZ_EXPORT_INFO pExportInfo, void *pvoid)
6970 BOOL ret;
6972 TRACE("(%08x, %p, %s, %p, %p)\n", dwFlags, hwndParent,
6973 debugstr_w(pwszWizardTitle), pExportInfo, pvoid);
6975 if (!(dwFlags & CRYPTUI_WIZ_NO_UI))
6976 ret = show_export_ui(dwFlags, hwndParent, pwszWizardTitle, pExportInfo,
6977 pvoid);
6978 else
6980 HANDLE file = CreateFileW(pExportInfo->pwszExportFileName,
6981 GENERIC_WRITE, FILE_SHARE_READ | FILE_SHARE_WRITE, NULL,
6982 CREATE_ALWAYS, 0, NULL);
6984 if (file != INVALID_HANDLE_VALUE)
6986 ret = do_export(file, pExportInfo, pvoid, NULL, FALSE);
6987 CloseHandle(file);
6989 else
6990 ret = FALSE;
6992 return ret;
6995 BOOL WINAPI CryptUIDlgViewSignerInfoA(CRYPTUI_VIEWSIGNERINFO_STRUCTA *pcvsi)
6997 FIXME("%p: stub\n", pcvsi);
6998 return FALSE;
7001 static void init_columns(HWND lv, DWORD flags)
7003 WCHAR buf[MAX_STRING_LEN];
7004 LVCOLUMNW column;
7005 DWORD i = 0;
7007 SendMessageW(lv, LVM_SETEXTENDEDLISTVIEWSTYLE, 0, LVS_EX_FULLROWSELECT);
7008 column.mask = LVCF_WIDTH | LVCF_TEXT;
7009 column.cx = 90;
7010 column.pszText = buf;
7011 if (!(flags & CRYPTUI_SELECT_ISSUEDTO_COLUMN))
7013 LoadStringW(hInstance, IDS_SUBJECT_COLUMN, buf, ARRAY_SIZE(buf));
7014 SendMessageW(lv, LVM_INSERTCOLUMNW, i++, (LPARAM)&column);
7016 if (!(flags & CRYPTUI_SELECT_ISSUEDBY_COLUMN))
7018 LoadStringW(hInstance, IDS_ISSUER_COLUMN, buf, ARRAY_SIZE(buf));
7019 SendMessageW(lv, LVM_INSERTCOLUMNW, i++, (LPARAM)&column);
7021 if (!(flags & CRYPTUI_SELECT_INTENDEDUSE_COLUMN))
7023 LoadStringW(hInstance, IDS_INTENDED_USE_COLUMN, buf, ARRAY_SIZE(buf));
7024 SendMessageW(lv, LVM_INSERTCOLUMNW, i++, (LPARAM)&column);
7026 if (!(flags & CRYPTUI_SELECT_FRIENDLYNAME_COLUMN))
7028 LoadStringW(hInstance, IDS_FRIENDLY_NAME_COLUMN, buf, ARRAY_SIZE(buf));
7029 SendMessageW(lv, LVM_INSERTCOLUMNW, i++, (LPARAM)&column);
7031 if (!(flags & CRYPTUI_SELECT_EXPIRATION_COLUMN))
7033 LoadStringW(hInstance, IDS_EXPIRATION_COLUMN, buf, ARRAY_SIZE(buf));
7034 SendMessageW(lv, LVM_INSERTCOLUMNW, i++, (LPARAM)&column);
7036 if (!(flags & CRYPTUI_SELECT_LOCATION_COLUMN))
7038 LoadStringW(hInstance, IDS_LOCATION_COLUMN, buf, ARRAY_SIZE(buf));
7039 SendMessageW(lv, LVM_INSERTCOLUMNW, i++, (LPARAM)&column);
7043 static void add_cert_to_list(HWND lv, PCCERT_CONTEXT cert, DWORD flags, DWORD *allocatedLen,
7044 LPWSTR *str)
7046 DWORD len;
7047 LVITEMW item;
7048 WCHAR dateFmt[80]; /* sufficient for LOCALE_SSHORTDATE */
7049 WCHAR buf[80];
7050 SYSTEMTIME sysTime;
7051 LPWSTR none, usages;
7053 item.mask = LVIF_IMAGE | LVIF_PARAM | LVIF_TEXT;
7054 item.iItem = SendMessageW(lv, LVM_GETITEMCOUNT, 0, 0);
7055 item.iSubItem = 0;
7056 item.iImage = 0;
7057 item.lParam = (LPARAM)CertDuplicateCertificateContext(cert);
7058 if (!item.iItem)
7060 item.mask |= LVIF_STATE;
7061 item.state = LVIS_SELECTED;
7062 item.stateMask = -1;
7064 if (!(flags & CRYPTUI_SELECT_ISSUEDTO_COLUMN))
7066 len = CertGetNameStringW(cert, CERT_NAME_SIMPLE_DISPLAY_TYPE, 0, NULL, NULL, 0);
7067 if (len > *allocatedLen)
7069 HeapFree(GetProcessHeap(), 0, *str);
7070 *str = HeapAlloc(GetProcessHeap(), 0, len * sizeof(WCHAR));
7071 if (*str)
7072 *allocatedLen = len;
7074 if (*str)
7076 CertGetNameStringW(cert, CERT_NAME_SIMPLE_DISPLAY_TYPE, 0, NULL, *str, len);
7077 item.pszText = *str;
7078 SendMessageW(lv, LVM_INSERTITEMW, 0, (LPARAM)&item);
7080 item.mask = LVIF_TEXT;
7081 ++item.iSubItem;
7083 if (!(flags & CRYPTUI_SELECT_ISSUEDBY_COLUMN))
7085 len = CertGetNameStringW(cert, CERT_NAME_SIMPLE_DISPLAY_TYPE, CERT_NAME_ISSUER_FLAG, NULL,
7086 NULL, 0);
7087 if (len > *allocatedLen)
7089 HeapFree(GetProcessHeap(), 0, *str);
7090 *str = HeapAlloc(GetProcessHeap(), 0, len * sizeof(WCHAR));
7091 if (*str)
7092 *allocatedLen = len;
7094 if (*str)
7096 CertGetNameStringW(cert, CERT_NAME_SIMPLE_DISPLAY_TYPE, CERT_NAME_ISSUER_FLAG, NULL,
7097 *str, len);
7098 item.pszText = *str;
7099 if (!item.iSubItem)
7100 SendMessageW(lv, LVM_INSERTITEMW, 0, (LPARAM)&item);
7101 else
7102 SendMessageW(lv, LVM_SETITEMTEXTW, item.iItem, (LPARAM)&item);
7104 item.mask = LVIF_TEXT;
7105 ++item.iSubItem;
7107 if (!(flags & CRYPTUI_SELECT_INTENDEDUSE_COLUMN))
7109 get_cert_usages(cert, &usages);
7110 if (usages)
7112 item.pszText = usages;
7113 if (!item.iSubItem)
7114 SendMessageW(lv, LVM_INSERTITEMW, 0, (LPARAM)&item);
7115 else
7116 SendMessageW(lv, LVM_SETITEMTEXTW, item.iItem, (LPARAM)&item);
7117 HeapFree(GetProcessHeap(), 0, usages);
7119 item.mask = LVIF_TEXT;
7120 ++item.iSubItem;
7122 if (!(flags & CRYPTUI_SELECT_FRIENDLYNAME_COLUMN))
7124 if (!CertGetCertificateContextProperty(cert, CERT_FRIENDLY_NAME_PROP_ID, NULL, &len))
7125 len = LoadStringW(hInstance, IDS_FRIENDLY_NAME_NONE, (LPWSTR)&none, 0);
7126 if (len > *allocatedLen)
7128 HeapFree(GetProcessHeap(), 0, *str);
7129 *str = HeapAlloc(GetProcessHeap(), 0, len * sizeof(WCHAR));
7130 if (*str)
7131 *allocatedLen = len;
7133 if (*str)
7135 if (!CertGetCertificateContextProperty(cert, CERT_FRIENDLY_NAME_PROP_ID, *str, &len))
7136 item.pszText = none;
7137 else
7138 item.pszText = *str;
7139 if (!item.iSubItem)
7140 SendMessageW(lv, LVM_INSERTITEMW, 0, (LPARAM)&item);
7141 else
7142 SendMessageW(lv, LVM_SETITEMTEXTW, item.iItem, (LPARAM)&item);
7144 item.mask = LVIF_TEXT;
7145 ++item.iSubItem;
7147 if (!(flags & CRYPTUI_SELECT_EXPIRATION_COLUMN))
7149 GetLocaleInfoW(LOCALE_SYSTEM_DEFAULT, LOCALE_SSHORTDATE, dateFmt, ARRAY_SIZE(dateFmt));
7150 FileTimeToSystemTime(&cert->pCertInfo->NotAfter, &sysTime);
7151 GetDateFormatW(LOCALE_SYSTEM_DEFAULT, 0, &sysTime, dateFmt, buf, ARRAY_SIZE(buf));
7152 item.pszText = buf;
7153 if (!item.iSubItem)
7154 SendMessageW(lv, LVM_INSERTITEMW, 0, (LPARAM)&item);
7155 else
7156 SendMessageW(lv, LVM_SETITEMTEXTW, item.iItem, (LPARAM)&item);
7157 item.mask = LVIF_TEXT;
7158 ++item.iSubItem;
7160 if (!(flags & CRYPTUI_SELECT_LOCATION_COLUMN))
7162 static int show_fixme;
7163 if (!show_fixme++)
7164 FIXME("showing location is not implemented\n");
7165 LoadStringW(hInstance, IDS_NO_IMPL, buf, ARRAY_SIZE(buf));
7166 if (!item.iSubItem)
7167 SendMessageW(lv, LVM_INSERTITEMW, 0, (LPARAM)&item);
7168 else
7169 SendMessageW(lv, LVM_SETITEMTEXTW, item.iItem, (LPARAM)&item);
7173 static void add_store_certs(HWND lv, HCERTSTORE store, DWORD flags, PFNCFILTERPROC filter,
7174 void *callback_data)
7176 PCCERT_CONTEXT cert = NULL;
7177 BOOL select = FALSE;
7178 DWORD allocatedLen = 0;
7179 LPWSTR str = NULL;
7181 do {
7182 cert = CertEnumCertificatesInStore(store, cert);
7183 if (cert && (!filter || filter(cert, &select, callback_data)))
7184 add_cert_to_list(lv, cert, flags, &allocatedLen, &str);
7185 } while (cert);
7186 HeapFree(GetProcessHeap(), 0, str);
7189 static PCCERT_CONTEXT select_cert_get_selected(HWND hwnd, int selection)
7191 HWND lv = GetDlgItem(hwnd, IDC_SELECT_CERTS);
7192 PCCERT_CONTEXT cert = NULL;
7193 LVITEMW item;
7195 if (selection < 0)
7196 selection = SendMessageW(lv, LVM_GETNEXTITEM, -1, LVNI_SELECTED);
7197 if (selection < 0)
7198 return NULL;
7199 item.mask = LVIF_PARAM;
7200 item.iItem = selection;
7201 item.iSubItem = 0;
7202 if (SendMessageW(lv, LVM_GETITEMW, 0, (LPARAM)&item))
7203 cert = (PCCERT_CONTEXT)item.lParam;
7204 return cert;
7207 static void select_cert_update_view_button(HWND hwnd)
7209 HWND lv = GetDlgItem(hwnd, IDC_SELECT_CERTS);
7210 int numSelected = SendMessageW(lv, LVM_GETSELECTEDCOUNT, 0, 0);
7212 EnableWindow(GetDlgItem(hwnd, IDC_SELECT_VIEW_CERT), numSelected == 1);
7215 struct SelectCertData
7217 PCCERT_CONTEXT *cert;
7218 DWORD dateColumn;
7219 HIMAGELIST imageList;
7220 LPCWSTR title;
7221 DWORD cStores;
7222 HCERTSTORE *rghStores;
7223 DWORD cPropSheetPages;
7224 LPCPROPSHEETPAGEW rgPropSheetPages;
7225 PFNCCERTDISPLAYPROC displayProc;
7226 void *callbackData;
7229 static void select_cert_view(HWND hwnd, PCCERT_CONTEXT cert, struct SelectCertData *data)
7231 CRYPTUI_VIEWCERTIFICATE_STRUCTW viewInfo;
7233 if (data->displayProc && data->displayProc(cert, hwnd, data->callbackData))
7234 return;
7235 memset(&viewInfo, 0, sizeof(viewInfo));
7236 viewInfo.dwSize = sizeof(viewInfo);
7237 viewInfo.hwndParent = hwnd;
7238 viewInfo.pCertContext = cert;
7239 viewInfo.cStores = data->cStores;
7240 viewInfo.rghStores = data->rghStores;
7241 viewInfo.cPropSheetPages = data->cPropSheetPages;
7242 viewInfo.rgPropSheetPages = data->rgPropSheetPages;
7243 /* FIXME: this should be modal */
7244 CryptUIDlgViewCertificateW(&viewInfo, NULL);
7247 struct SortData
7249 HWND hwnd;
7250 int column;
7253 static int CALLBACK select_cert_sort_by_text(LPARAM lp1, LPARAM lp2, LPARAM lp)
7255 struct SortData *data = (struct SortData *)lp;
7256 return cert_mgr_sort_by_text(data->hwnd, data->column, lp1, lp2);
7259 struct SelectCertParam
7261 PCCRYPTUI_SELECTCERTIFICATE_STRUCTW pcsc;
7262 PCCERT_CONTEXT cert;
7265 static LRESULT CALLBACK select_cert_dlg_proc(HWND hwnd, UINT msg, WPARAM wp, LPARAM lp)
7267 struct SelectCertData *data;
7269 switch (msg)
7271 case WM_INITDIALOG:
7273 struct SelectCertParam *param = (struct SelectCertParam *)lp;
7274 PCCRYPTUI_SELECTCERTIFICATE_STRUCTW pcsc = param->pcsc;
7275 HWND lv = GetDlgItem(hwnd, IDC_SELECT_CERTS);
7276 DWORD i = 0;
7278 data = HeapAlloc(GetProcessHeap(), 0, sizeof(*data));
7279 if (!data)
7280 return 0;
7281 data->cert = &param->cert;
7282 data->dateColumn = 4 -
7283 ((pcsc->dwDontUseColumn & CRYPTUI_SELECT_ISSUEDTO_COLUMN) ? 1 : 0) -
7284 ((pcsc->dwDontUseColumn & CRYPTUI_SELECT_ISSUEDBY_COLUMN) ? 1 : 0) -
7285 ((pcsc->dwDontUseColumn & CRYPTUI_SELECT_INTENDEDUSE_COLUMN) ? 1 : 0) -
7286 ((pcsc->dwDontUseColumn & CRYPTUI_SELECT_FRIENDLYNAME_COLUMN) ? 1 : 0);
7287 data->imageList = ImageList_Create(16, 16, ILC_COLOR4 | ILC_MASK, 2, 0);
7288 if (data->imageList)
7290 HBITMAP bmp;
7291 COLORREF backColor = RGB(255, 0, 255);
7293 bmp = LoadBitmapW(hInstance, MAKEINTRESOURCEW(IDB_SMALL_ICONS));
7294 ImageList_AddMasked(data->imageList, bmp, backColor);
7295 DeleteObject(bmp);
7296 ImageList_SetBkColor(data->imageList, CLR_NONE);
7297 SendMessageW(GetDlgItem(hwnd, IDC_SELECT_CERTS), LVM_SETIMAGELIST, LVSIL_SMALL,
7298 (LPARAM)data->imageList);
7300 data->title = pcsc->szTitle;
7301 data->cStores = pcsc->cStores;
7302 data->rghStores = pcsc->rghStores;
7303 data->cPropSheetPages = pcsc->cPropSheetPages;
7304 data->rgPropSheetPages = pcsc->rgPropSheetPages;
7305 data->displayProc = pcsc->pDisplayCallback;
7306 data->callbackData = pcsc->pvCallbackData;
7307 SetWindowLongPtrW(hwnd, DWLP_USER, (LPARAM)data);
7309 if (pcsc->szTitle)
7310 SendMessageW(hwnd, WM_SETTEXT, 0, (LPARAM)pcsc->szTitle);
7311 if (pcsc->szDisplayString)
7312 SendMessageW(GetDlgItem(hwnd, IDC_SELECT_DISPLAY_STRING), WM_SETTEXT, 0,
7313 (LPARAM)pcsc->szDisplayString);
7314 init_columns(lv, pcsc->dwDontUseColumn);
7315 while (i < pcsc->cDisplayStores)
7316 add_store_certs(lv, pcsc->rghDisplayStores[i++], pcsc->dwDontUseColumn,
7317 pcsc->pFilterCallback, pcsc->pvCallbackData);
7318 select_cert_update_view_button(hwnd);
7319 break;
7321 case WM_NOTIFY:
7323 NMHDR *hdr = (NMHDR *)lp;
7325 switch (hdr->code)
7327 case NM_DBLCLK:
7329 PCCERT_CONTEXT cert = select_cert_get_selected(hwnd, ((NMITEMACTIVATE *)lp)->iItem);
7331 data = (struct SelectCertData *)GetWindowLongPtrW(hwnd, DWLP_USER);
7332 if (cert)
7333 select_cert_view(hwnd, cert, data);
7334 break;
7336 case LVN_COLUMNCLICK:
7338 NMLISTVIEW *nmlv = (NMLISTVIEW *)lp;
7339 HWND lv = GetDlgItem(hwnd, IDC_SELECT_CERTS);
7341 /* FIXME: doesn't support swapping sort order between ascending and descending. */
7342 data = (struct SelectCertData *)GetWindowLongPtrW(hwnd, DWLP_USER);
7343 if (nmlv->iSubItem == data->dateColumn)
7344 SendMessageW(lv, LVM_SORTITEMS, 0, (LPARAM)cert_mgr_sort_by_date);
7345 else
7347 struct SortData sortData;
7349 sortData.hwnd = lv;
7350 sortData.column = nmlv->iSubItem;
7351 SendMessageW(lv, LVM_SORTITEMSEX, (WPARAM)&sortData,
7352 (LPARAM)select_cert_sort_by_text);
7354 break;
7357 break;
7359 case WM_COMMAND:
7360 switch (wp)
7362 case IDOK:
7364 PCCERT_CONTEXT cert = select_cert_get_selected(hwnd, -1);
7366 data = (struct SelectCertData *)GetWindowLongPtrW(hwnd, DWLP_USER);
7367 if (!cert)
7369 WCHAR buf[40], title[40];
7371 LoadStringW(hInstance, IDS_SELECT_CERT, buf, ARRAY_SIZE(buf));
7372 if (!data->title)
7373 LoadStringW(hInstance, IDS_SELECT_CERT_TITLE, title, ARRAY_SIZE(title));
7374 MessageBoxW(hwnd, buf, data->title ? data->title : title, MB_OK | MB_ICONWARNING);
7375 break;
7377 *data->cert = CertDuplicateCertificateContext(cert);
7378 free_certs(GetDlgItem(hwnd, IDC_SELECT_CERTS));
7379 ImageList_Destroy(data->imageList);
7380 HeapFree(GetProcessHeap(), 0, data);
7381 EndDialog(hwnd, IDOK);
7382 break;
7384 case IDCANCEL:
7385 data = (struct SelectCertData *)GetWindowLongPtrW(hwnd, DWLP_USER);
7386 free_certs(GetDlgItem(hwnd, IDC_SELECT_CERTS));
7387 ImageList_Destroy(data->imageList);
7388 HeapFree(GetProcessHeap(), 0, data);
7389 EndDialog(hwnd, IDCANCEL);
7390 break;
7391 case IDC_SELECT_VIEW_CERT:
7393 PCCERT_CONTEXT cert = select_cert_get_selected(hwnd, -1);
7395 data = (struct SelectCertData *)GetWindowLongPtrW(hwnd, DWLP_USER);
7396 if (cert)
7397 select_cert_view(hwnd, cert, data);
7398 break;
7401 break;
7403 return 0;
7406 PCCERT_CONTEXT WINAPI CryptUIDlgSelectCertificateW(PCCRYPTUI_SELECTCERTIFICATE_STRUCTW pcsc)
7408 struct SelectCertParam param;
7410 TRACE("%p\n", pcsc);
7412 if (pcsc->dwSize != sizeof(*pcsc) && pcsc->dwSize != sizeof(*pcsc) - sizeof(HCERTSTORE))
7414 WARN("unexpected size %d\n", pcsc->dwSize);
7415 SetLastError(E_INVALIDARG);
7416 return NULL;
7418 if (pcsc->dwFlags & CRYPTUI_SELECTCERT_MULTISELECT)
7419 FIXME("ignoring CRYPTUI_SELECTCERT_MULTISELECT\n");
7420 param.pcsc = pcsc;
7421 param.cert = NULL;
7422 DialogBoxParamW(hInstance, MAKEINTRESOURCEW(IDD_SELECT_CERT), pcsc->hwndParent,
7423 select_cert_dlg_proc, (LPARAM)&param);
7424 return param.cert;
7427 static void free_prop_sheet_pages(PROPSHEETPAGEW *pages, DWORD num)
7429 DWORD i;
7431 for (i = 0; i < num; i++)
7433 if (!(pages[i].dwFlags & PSP_DLGINDIRECT) && !IS_INTRESOURCE(pages[i].u.pszTemplate))
7434 HeapFree(GetProcessHeap(), 0, (void *)pages[i].u.pszTemplate);
7435 if ((pages[i].dwFlags & PSP_USEICONID) && !IS_INTRESOURCE(pages[i].u2.pszIcon))
7436 HeapFree(GetProcessHeap(), 0, (void *)pages[i].u2.pszIcon);
7437 if ((pages[i].dwFlags & PSP_USETITLE) && !IS_INTRESOURCE(pages[i].pszTitle))
7438 HeapFree(GetProcessHeap(), 0, (void *)pages[i].pszTitle);
7439 if ((pages[i].dwFlags & PSP_USEHEADERTITLE) && !IS_INTRESOURCE(pages[i].pszHeaderTitle))
7440 HeapFree(GetProcessHeap(), 0, (void *)pages[i].pszHeaderTitle);
7441 if ((pages[i].dwFlags & PSP_USEHEADERSUBTITLE) &&
7442 !IS_INTRESOURCE(pages[i].pszHeaderSubTitle))
7443 HeapFree(GetProcessHeap(), 0, (void *)pages[i].pszHeaderSubTitle);
7445 HeapFree(GetProcessHeap(), 0, pages);
7448 static PROPSHEETPAGEW *prop_sheet_pages_AtoW(LPCPROPSHEETPAGEA pages, DWORD num)
7450 PROPSHEETPAGEW *psp;
7451 DWORD i, size = sizeof(*psp) * num;
7453 psp = HeapAlloc(GetProcessHeap(), 0, size);
7454 if (!psp)
7455 return NULL;
7456 memcpy(psp, pages, size);
7457 for (i = 0; i < num; i++)
7459 if (!(pages[i].dwFlags & PSP_DLGINDIRECT) && !IS_INTRESOURCE(pages[i].u.pszTemplate))
7460 psp[i].u.pszTemplate = NULL;
7461 if ((pages[i].dwFlags & PSP_USEICONID) && !IS_INTRESOURCE(pages[i].u2.pszIcon))
7462 psp[i].u2.pszIcon = NULL;
7463 if ((pages[i].dwFlags & PSP_USETITLE) && !IS_INTRESOURCE(pages[i].pszTitle))
7464 psp[i].pszTitle = NULL;
7465 if (pages[i].dwFlags & PSP_USECALLBACK)
7466 psp[i].pfnCallback = NULL;
7467 if ((pages[i].dwFlags & PSP_USEHEADERTITLE) && !IS_INTRESOURCE(pages[i].pszHeaderTitle))
7468 psp[i].pszHeaderTitle = NULL;
7469 if ((pages[i].dwFlags & PSP_USEHEADERSUBTITLE) &&
7470 !IS_INTRESOURCE(pages[i].pszHeaderSubTitle))
7471 psp[i].pszHeaderSubTitle = NULL;
7473 for (i = 0; i < num; i++)
7475 if (!(pages[i].dwFlags & PSP_DLGINDIRECT) && !IS_INTRESOURCE(pages[i].u.pszTemplate))
7477 if (!(psp[i].u.pszTemplate = strdupAtoW( pages[i].u.pszTemplate ))) goto error;
7479 if ((pages[i].dwFlags & PSP_USEICONID) && !IS_INTRESOURCE(pages[i].u2.pszIcon))
7481 if (!(psp[i].u2.pszIcon = strdupAtoW( pages[i].u2.pszIcon ))) goto error;
7483 if ((pages[i].dwFlags & PSP_USETITLE) && !IS_INTRESOURCE(pages[i].pszTitle))
7485 if (!(psp[i].pszTitle = strdupAtoW( pages[i].pszTitle ))) goto error;
7487 if (pages[i].dwFlags & PSP_USECALLBACK)
7488 FIXME("ignoring pfnCallback\n");
7489 if ((pages[i].dwFlags & PSP_USEHEADERTITLE) && !IS_INTRESOURCE(pages[i].pszHeaderTitle))
7491 if (!(psp[i].pszHeaderTitle = strdupAtoW( pages[i].pszHeaderTitle ))) goto error;
7493 if ((pages[i].dwFlags & PSP_USEHEADERSUBTITLE) &&
7494 !IS_INTRESOURCE(pages[i].pszHeaderSubTitle))
7496 if (!(psp[i].pszHeaderSubTitle = strdupAtoW( pages[i].pszHeaderSubTitle ))) goto error;
7499 return psp;
7500 error:
7501 free_prop_sheet_pages(psp, num);
7502 return NULL;
7505 PCCERT_CONTEXT WINAPI CryptUIDlgSelectCertificateA(PCCRYPTUI_SELECTCERTIFICATE_STRUCTA pcsc)
7507 PCCERT_CONTEXT cert = NULL;
7508 CRYPTUI_SELECTCERTIFICATE_STRUCTW selCertInfo;
7509 LPWSTR title = NULL, display_str = NULL;
7510 PROPSHEETPAGEW *pages = NULL;
7512 TRACE("%p\n", pcsc);
7514 if (pcsc->dwSize != sizeof(*pcsc) && pcsc->dwSize != sizeof(*pcsc) - sizeof(HCERTSTORE))
7516 WARN("unexpected size %d\n", pcsc->dwSize);
7517 SetLastError(E_INVALIDARG);
7518 return NULL;
7520 memcpy(&selCertInfo, pcsc, pcsc->dwSize);
7521 if (pcsc->szTitle)
7523 if (!(title = strdupAtoW( pcsc->szTitle ))) goto error;
7524 selCertInfo.szTitle = title;
7526 if (pcsc->szDisplayString)
7528 if (!(display_str = strdupAtoW( pcsc->szDisplayString ))) goto error;
7529 selCertInfo.szDisplayString = display_str;
7531 if (pcsc->cPropSheetPages)
7533 pages = prop_sheet_pages_AtoW(pcsc->rgPropSheetPages, pcsc->cPropSheetPages);
7534 if (!pages)
7535 goto error;
7536 selCertInfo.rgPropSheetPages = pages;
7538 cert = CryptUIDlgSelectCertificateW(&selCertInfo);
7539 error:
7540 HeapFree(GetProcessHeap(), 0, title);
7541 HeapFree(GetProcessHeap(), 0, display_str);
7542 if (pcsc->cPropSheetPages)
7543 free_prop_sheet_pages(pages, pcsc->cPropSheetPages);
7544 return cert;
7547 PCCERT_CONTEXT WINAPI CryptUIDlgSelectCertificateFromStore(HCERTSTORE hCertStore, HWND hwnd, LPCWSTR pwszTitle,
7548 LPCWSTR pwszDisplayString, DWORD dwDontUseColumn,
7549 DWORD dwFlags, void *pvReserved)
7551 CRYPTUI_SELECTCERTIFICATE_STRUCTW sc;
7553 TRACE("%p %p %s %s %x %x %p\n", hCertStore, hwnd, debugstr_w(pwszTitle), debugstr_w(pwszDisplayString), dwDontUseColumn, dwFlags, pvReserved);
7555 memset(&sc, 0, sizeof(sc));
7557 sc.dwSize = sizeof(sc);
7558 sc.hwndParent = hwnd;
7559 sc.dwFlags = dwFlags;
7560 sc.szTitle = pwszTitle;
7561 sc.szDisplayString = pwszDisplayString;
7562 sc.dwDontUseColumn = dwDontUseColumn;
7563 sc.cDisplayStores = 1;
7564 sc.rghDisplayStores = &hCertStore;
7565 return CryptUIDlgSelectCertificateW(&sc);
7568 BOOL WINAPI CryptUIWizDigitalSign(DWORD flags, HWND parent, LPCWSTR title, PCCRYPTUI_WIZ_DIGITAL_SIGN_INFO info,
7569 PCCRYPTUI_WIZ_DIGITAL_SIGN_CONTEXT *context)
7571 FIXME("%d %p %s %p %p: stub\n", flags, parent, debugstr_w(title), info, context);
7572 return FALSE;