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
24 #define NONAMELESSUNION
37 #include "cryptuiapi.h"
38 #include "cryptuires.h"
42 #include "wine/debug.h"
43 #include "wine/unicode.h"
45 WINE_DEFAULT_DEBUG_CHANNEL(cryptui
);
47 static HINSTANCE hInstance
;
49 BOOL WINAPI
DllMain(HINSTANCE hinstDLL
, DWORD fdwReason
, LPVOID lpvReserved
)
51 TRACE("(0x%p, %d, %p)\n", hinstDLL
, fdwReason
, lpvReserved
);
55 case DLL_WINE_PREATTACH
:
56 return FALSE
; /* prefer native version */
57 case DLL_PROCESS_ATTACH
:
59 DisableThreadLibraryCalls(hinstDLL
);
61 case DLL_PROCESS_DETACH
:
69 #define MAX_STRING_LEN 512
71 static void add_cert_columns(HWND hwnd
)
73 HWND lv
= GetDlgItem(hwnd
, IDC_MGR_CERTS
);
75 WCHAR buf
[MAX_STRING_LEN
];
78 SendMessageW(lv
, LVM_SETEXTENDEDLISTVIEWSTYLE
, 0, LVS_EX_FULLROWSELECT
);
79 GetWindowRect(lv
, &rc
);
80 LoadStringW(hInstance
, IDS_SUBJECT_COLUMN
, buf
,
81 sizeof(buf
) / sizeof(buf
[0]));
82 column
.mask
= LVCF_WIDTH
| LVCF_TEXT
;
83 column
.cx
= (rc
.right
- rc
.left
) * 29 / 100 - 2;
85 SendMessageW(lv
, LVM_INSERTCOLUMNW
, 0, (LPARAM
)&column
);
86 LoadStringW(hInstance
, IDS_ISSUER_COLUMN
, buf
,
87 sizeof(buf
) / sizeof(buf
[0]));
88 SendMessageW(lv
, LVM_INSERTCOLUMNW
, 1, (LPARAM
)&column
);
89 column
.cx
= (rc
.right
- rc
.left
) * 16 / 100 - 2;
90 LoadStringW(hInstance
, IDS_EXPIRATION_COLUMN
, buf
,
91 sizeof(buf
) / sizeof(buf
[0]));
92 SendMessageW(lv
, LVM_INSERTCOLUMNW
, 2, (LPARAM
)&column
);
93 column
.cx
= (rc
.right
- rc
.left
) * 23 / 100 - 1;
94 LoadStringW(hInstance
, IDS_FRIENDLY_NAME_COLUMN
, buf
,
95 sizeof(buf
) / sizeof(buf
[0]));
96 SendMessageW(lv
, LVM_INSERTCOLUMNW
, 3, (LPARAM
)&column
);
99 static void add_cert_to_view(HWND lv
, PCCERT_CONTEXT cert
, DWORD
*allocatedLen
,
104 WCHAR dateFmt
[80]; /* sufficient for LOCALE_SSHORTDATE */
108 item
.mask
= LVIF_IMAGE
| LVIF_PARAM
| LVIF_TEXT
;
109 item
.iItem
= SendMessageW(lv
, LVM_GETITEMCOUNT
, 0, 0);
112 item
.lParam
= (LPARAM
)CertDuplicateCertificateContext(cert
);
113 len
= CertGetNameStringW(cert
, CERT_NAME_SIMPLE_DISPLAY_TYPE
, 0, NULL
,
115 if (len
> *allocatedLen
)
117 HeapFree(GetProcessHeap(), 0, *str
);
118 *str
= HeapAlloc(GetProcessHeap(), 0, len
* sizeof(WCHAR
));
124 CertGetNameStringW(cert
, CERT_NAME_SIMPLE_DISPLAY_TYPE
, 0, NULL
,
127 SendMessageW(lv
, LVM_INSERTITEMW
, 0, (LPARAM
)&item
);
130 item
.mask
= LVIF_TEXT
;
131 len
= CertGetNameStringW(cert
, CERT_NAME_SIMPLE_DISPLAY_TYPE
,
132 CERT_NAME_ISSUER_FLAG
, NULL
, NULL
, 0);
133 if (len
> *allocatedLen
)
135 HeapFree(GetProcessHeap(), 0, *str
);
136 *str
= HeapAlloc(GetProcessHeap(), 0, len
* sizeof(WCHAR
));
142 CertGetNameStringW(cert
, CERT_NAME_SIMPLE_DISPLAY_TYPE
,
143 CERT_NAME_ISSUER_FLAG
, NULL
, *str
, len
);
146 SendMessageW(lv
, LVM_SETITEMTEXTW
, item
.iItem
, (LPARAM
)&item
);
149 GetLocaleInfoW(LOCALE_SYSTEM_DEFAULT
, LOCALE_SSHORTDATE
, dateFmt
,
150 sizeof(dateFmt
) / sizeof(dateFmt
[0]));
151 FileTimeToSystemTime(&cert
->pCertInfo
->NotAfter
, &sysTime
);
152 GetDateFormatW(LOCALE_SYSTEM_DEFAULT
, 0, &sysTime
, dateFmt
, date
,
153 sizeof(date
) / sizeof(date
[0]));
156 SendMessageW(lv
, LVM_SETITEMTEXTW
, item
.iItem
, (LPARAM
)&item
);
158 len
= CertGetNameStringW(cert
, CERT_NAME_FRIENDLY_DISPLAY_TYPE
, 0, NULL
,
160 if (len
> *allocatedLen
)
162 HeapFree(GetProcessHeap(), 0, *str
);
163 *str
= HeapAlloc(GetProcessHeap(), 0, len
* sizeof(WCHAR
));
169 CertGetNameStringW(cert
, CERT_NAME_FRIENDLY_DISPLAY_TYPE
, 0, NULL
,
173 SendMessageW(lv
, LVM_SETITEMTEXTW
, item
.iItem
, (LPARAM
)&item
);
177 static LPSTR
get_cert_mgr_usages(void)
179 static const WCHAR keyName
[] = { 'S','o','f','t','w','a','r','e','\\','M',
180 'i','c','r','o','s','o','f','t','\\','C','r','y','p','t','o','g','r','a',
181 'p','h','y','\\','U','I','\\','C','e','r','t','m','g','r','\\','P','u',
182 'r','p','o','s','e',0 };
186 if (!RegCreateKeyExW(HKEY_CURRENT_USER
, keyName
, 0, NULL
, 0, KEY_READ
,
192 rc
= RegQueryValueExA(key
, "Purpose", NULL
, &type
, NULL
, &size
);
193 if ((!rc
|| rc
== ERROR_MORE_DATA
) && type
== REG_SZ
)
195 str
= HeapAlloc(GetProcessHeap(), 0, size
);
198 rc
= RegQueryValueExA(key
, "Purpose", NULL
, NULL
, (LPBYTE
)str
,
202 HeapFree(GetProcessHeap(), 0, str
);
213 PurposeFilterShowAll
= 0,
214 PurposeFilterShowAdvanced
= 1,
215 PurposeFilterShowOID
= 2
218 static void initialize_purpose_selection(HWND hwnd
)
220 HWND cb
= GetDlgItem(hwnd
, IDC_MGR_PURPOSE_SELECTION
);
221 WCHAR buf
[MAX_STRING_LEN
];
225 LoadStringW(hInstance
, IDS_PURPOSE_ALL
, buf
,
226 sizeof(buf
) / sizeof(buf
[0]));
227 index
= SendMessageW(cb
, CB_INSERTSTRING
, -1, (LPARAM
)buf
);
228 SendMessageW(cb
, CB_SETITEMDATA
, index
, (LPARAM
)PurposeFilterShowAll
);
229 LoadStringW(hInstance
, IDS_PURPOSE_ADVANCED
, buf
,
230 sizeof(buf
) / sizeof(buf
[0]));
231 index
= SendMessageW(cb
, CB_INSERTSTRING
, -1, (LPARAM
)buf
);
232 SendMessageW(cb
, CB_SETITEMDATA
, index
, (LPARAM
)PurposeFilterShowAdvanced
);
233 SendMessageW(cb
, CB_SETCURSEL
, 0, 0);
234 if ((usages
= get_cert_mgr_usages()))
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
;
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,
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
;
273 HeapFree(GetProcessHeap(), 0, 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
));
288 for (ptr
= usageStr
, comma
= strchr(ptr
, ','); usage
&& ptr
&& *ptr
;
289 ptr
= comma
? comma
+ 1 : NULL
,
290 comma
= ptr
? strchr(ptr
, ',') : NULL
)
294 add_oid_to_usage(usage
, ptr
);
300 static CERT_ENHKEY_USAGE
*create_advanced_filter(void)
302 CERT_ENHKEY_USAGE
*advancedUsage
= HeapAlloc(GetProcessHeap(),
303 HEAP_ZERO_MEMORY
, sizeof(CERT_ENHKEY_USAGE
));
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
);
320 PCCRYPT_OID_INFO
*ptr
;
322 for (ptr
= usages
; *ptr
; ptr
++)
325 BOOL disabled
= FALSE
;
327 for (i
= 0; !disabled
&&
328 i
< disabledUsages
->cUsageIdentifier
; i
++)
329 if (!strcmp(disabledUsages
->rgpszUsageIdentifier
[i
],
333 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 void show_store_certs(HWND hwnd
, HCERTSTORE store
)
353 HWND lv
= GetDlgItem(hwnd
, IDC_MGR_CERTS
);
354 HWND cb
= GetDlgItem(hwnd
, IDC_MGR_PURPOSE_SELECTION
);
355 PCCERT_CONTEXT cert
= NULL
;
356 DWORD allocatedLen
= 0;
359 PurposeFilter filter
= PurposeFilterShowAll
;
361 CERT_ENHKEY_USAGE
*advanced
= NULL
;
363 index
= SendMessageW(cb
, CB_GETCURSEL
, 0, 0);
366 INT_PTR data
= SendMessageW(cb
, CB_GETITEMDATA
, index
, 0);
372 PCCRYPT_OID_INFO info
= (PCCRYPT_OID_INFO
)data
;
374 filter
= PurposeFilterShowOID
;
378 if (filter
== PurposeFilterShowAdvanced
)
379 advanced
= create_advanced_filter();
381 cert
= CertEnumCertificatesInStore(store
, cert
);
386 if (filter
== PurposeFilterShowAll
)
393 if (CertGetValidUsages(1, &cert
, &numOIDs
, NULL
, &cbOIDs
))
397 /* -1 implies all usages are valid */
402 LPSTR
*oids
= HeapAlloc(GetProcessHeap(), 0, cbOIDs
);
406 if (CertGetValidUsages(1, &cert
, &numOIDs
, oids
,
411 if (filter
== PurposeFilterShowOID
)
413 for (i
= 0; !show
&& i
< numOIDs
; i
++)
414 if (!strcmp(oids
[i
], oid
))
419 for (i
= 0; !show
&& i
< numOIDs
; i
++)
424 j
< advanced
->cUsageIdentifier
; j
++)
426 advanced
->rgpszUsageIdentifier
[j
]))
431 HeapFree(GetProcessHeap(), 0, oids
);
437 add_cert_to_view(lv
, cert
, &allocatedLen
, &str
);
440 HeapFree(GetProcessHeap(), 0, str
);
443 HeapFree(GetProcessHeap(), 0, advanced
->rgpszUsageIdentifier
);
444 HeapFree(GetProcessHeap(), 0, advanced
);
448 static const WCHAR my
[] = { 'M','y',0 };
449 static const WCHAR addressBook
[] = {
450 'A','d','d','r','e','s','s','B','o','o','k',0 };
451 static const WCHAR ca
[] = { 'C','A',0 };
452 static const WCHAR root
[] = { 'R','o','o','t',0 };
453 static const WCHAR trustedPublisher
[] = {
454 'T','r','u','s','t','e','d','P','u','b','l','i','s','h','e','r',0 };
455 static const WCHAR disallowed
[] = { 'D','i','s','a','l','l','o','w','e','d',0 };
457 struct CertMgrStoreInfo
461 int removePluralWarning
;
464 static const struct CertMgrStoreInfo defaultStoreList
[] = {
465 { my
, IDS_WARN_REMOVE_MY
, IDS_WARN_REMOVE_PLURAL_MY
},
466 { addressBook
, IDS_WARN_REMOVE_ADDRESSBOOK
,
467 IDS_WARN_REMOVE_PLURAL_ADDRESSBOOK
},
468 { ca
, IDS_WARN_REMOVE_CA
, IDS_WARN_REMOVE_PLURAL_CA
},
469 { root
, IDS_WARN_REMOVE_ROOT
, IDS_WARN_REMOVE_PLURAL_ROOT
},
470 { trustedPublisher
, IDS_WARN_REMOVE_TRUSTEDPUBLISHER
,
471 IDS_WARN_REMOVE_PLURAL_TRUSTEDPUBLISHER
},
472 { disallowed
, IDS_WARN_REMOVE_DEFAULT
},
475 static const struct CertMgrStoreInfo publisherStoreList
[] = {
476 { root
, IDS_WARN_REMOVE_ROOT
, IDS_WARN_REMOVE_PLURAL_ROOT
},
477 { trustedPublisher
, IDS_WARN_REMOVE_TRUSTEDPUBLISHER
,
478 IDS_WARN_REMOVE_PLURAL_TRUSTEDPUBLISHER
},
479 { disallowed
, IDS_WARN_REMOVE_PLURAL_DEFAULT
},
484 HIMAGELIST imageList
;
487 const struct CertMgrStoreInfo
*stores
;
490 static void show_cert_stores(HWND hwnd
, DWORD dwFlags
, struct CertMgrData
*data
)
492 const struct CertMgrStoreInfo
*storeList
;
494 HWND tab
= GetDlgItem(hwnd
, IDC_MGR_STORES
);
496 if (dwFlags
& CRYPTUI_CERT_MGR_PUBLISHER_TAB
)
498 storeList
= publisherStoreList
;
499 cStores
= sizeof(publisherStoreList
) / sizeof(publisherStoreList
[0]);
503 storeList
= defaultStoreList
;
504 cStores
= sizeof(defaultStoreList
) / sizeof(defaultStoreList
[0]);
506 if (dwFlags
& CRYPTUI_CERT_MGR_SINGLE_TAB_FLAG
)
508 data
->nStores
= cStores
;
509 data
->stores
= storeList
;
510 for (i
= 0; i
< cStores
; i
++)
516 if (!(name
= CryptFindLocalizedName(storeList
[i
].name
)))
517 name
= storeList
[i
].name
;
518 store
= CertOpenStore(CERT_STORE_PROV_SYSTEM_W
, 0, 0,
519 CERT_SYSTEM_STORE_CURRENT_USER
, storeList
[i
].name
);
520 item
.mask
= TCIF_TEXT
| TCIF_PARAM
;
521 item
.pszText
= (LPWSTR
)name
;
522 item
.lParam
= (LPARAM
)store
;
523 SendMessageW(tab
, TCM_INSERTITEMW
, i
, (LPARAM
)&item
);
527 static void free_certs(HWND lv
)
530 int items
= SendMessageW(lv
, LVM_GETITEMCOUNT
, 0, 0), i
;
532 for (i
= 0; i
< items
; i
++)
534 item
.mask
= LVIF_PARAM
;
537 SendMessageW(lv
, LVM_GETITEMW
, 0, (LPARAM
)&item
);
538 CertFreeCertificateContext((PCCERT_CONTEXT
)item
.lParam
);
542 static HCERTSTORE
cert_mgr_index_to_store(HWND tab
, int index
)
546 item
.mask
= TCIF_PARAM
;
547 SendMessageW(tab
, TCM_GETITEMW
, index
, (LPARAM
)&item
);
548 return (HCERTSTORE
)item
.lParam
;
551 static HCERTSTORE
cert_mgr_current_store(HWND hwnd
)
553 HWND tab
= GetDlgItem(hwnd
, IDC_MGR_STORES
);
555 return cert_mgr_index_to_store(tab
, SendMessageW(tab
, TCM_GETCURSEL
, 0, 0));
558 static void close_stores(HWND tab
)
560 int i
, tabs
= SendMessageW(tab
, TCM_GETITEMCOUNT
, 0, 0);
562 for (i
= 0; i
< tabs
; i
++)
563 CertCloseStore(cert_mgr_index_to_store(tab
, i
), 0);
566 static void refresh_store_certs(HWND hwnd
)
568 HWND lv
= GetDlgItem(hwnd
, IDC_MGR_CERTS
);
571 SendMessageW(lv
, LVM_DELETEALLITEMS
, 0, 0);
572 show_store_certs(hwnd
, cert_mgr_current_store(hwnd
));
576 CheckBitmapIndexUnchecked
= 1,
577 CheckBitmapIndexChecked
= 2,
578 CheckBitmapIndexDisabledUnchecked
= 3,
579 CheckBitmapIndexDisabledChecked
= 4
582 static void add_known_usage(HWND lv
, PCCRYPT_OID_INFO info
,
583 CheckBitmapIndex state
)
587 item
.mask
= LVIF_TEXT
| LVIF_STATE
| LVIF_PARAM
;
588 item
.state
= INDEXTOSTATEIMAGEMASK(state
);
589 item
.stateMask
= LVIS_STATEIMAGEMASK
;
590 item
.iItem
= SendMessageW(lv
, LVM_GETITEMCOUNT
, 0, 0);
592 item
.lParam
= (LPARAM
)info
;
593 item
.pszText
= (LPWSTR
)info
->pwszName
;
594 SendMessageW(lv
, LVM_INSERTITEMW
, 0, (LPARAM
)&item
);
597 static void add_known_usages_to_list(HWND lv
, CheckBitmapIndex state
)
599 PCCRYPT_OID_INFO
*usages
;
601 if (WTHelperGetKnownUsages(1, &usages
))
603 PCCRYPT_OID_INFO
*ptr
;
605 for (ptr
= usages
; *ptr
; ptr
++)
606 add_known_usage(lv
, *ptr
, state
);
607 WTHelperGetKnownUsages(2, &usages
);
611 static void toggle_usage(HWND hwnd
, int iItem
)
615 HWND lv
= GetDlgItem(hwnd
, IDC_CERTIFICATE_USAGES
);
617 item
.mask
= LVIF_STATE
;
620 item
.stateMask
= LVIS_STATEIMAGEMASK
;
621 res
= SendMessageW(lv
, LVM_GETITEMW
, 0, (LPARAM
)&item
);
624 int state
= item
.state
>> 12;
626 item
.state
= INDEXTOSTATEIMAGEMASK(
627 state
== CheckBitmapIndexChecked
? CheckBitmapIndexUnchecked
:
628 CheckBitmapIndexChecked
);
629 SendMessageW(lv
, LVM_SETITEMSTATE
, iItem
, (LPARAM
)&item
);
633 static LONG_PTR
find_oid_in_list(HWND lv
, LPCSTR oid
)
635 PCCRYPT_OID_INFO oidInfo
= CryptFindOIDInfo(CRYPT_OID_INFO_OID_KEY
,
636 (void *)oid
, CRYPT_ENHKEY_USAGE_OID_GROUP_ID
);
641 LVFINDINFOW findInfo
;
643 findInfo
.flags
= LVFI_PARAM
;
644 findInfo
.lParam
= (LPARAM
)oidInfo
;
645 ret
= SendMessageW(lv
, LVM_FINDITEMW
, -1, (LPARAM
)&findInfo
);
649 LVFINDINFOA findInfo
;
651 findInfo
.flags
= LVFI_STRING
;
653 ret
= SendMessageW(lv
, LVM_FINDITEMA
, -1, (LPARAM
)&findInfo
);
658 static void save_cert_mgr_usages(HWND hwnd
)
660 static const WCHAR keyName
[] = { 'S','o','f','t','w','a','r','e','\\','M',
661 'i','c','r','o','s','o','f','t','\\','C','r','y','p','t','o','g','r','a',
662 'p','h','y','\\','U','I','\\','C','e','r','t','m','g','r','\\','P','u',
663 'r','p','o','s','e',0 };
665 HWND lv
= GetDlgItem(hwnd
, IDC_CERTIFICATE_USAGES
);
666 int purposes
= SendMessageW(lv
, LVM_GETITEMCOUNT
, 0, 0), i
;
670 item
.mask
= LVIF_STATE
| LVIF_PARAM
;
672 item
.stateMask
= LVIS_STATEIMAGEMASK
;
673 for (i
= 0; i
< purposes
; i
++)
676 if (SendMessageW(lv
, LVM_GETITEMW
, 0, (LPARAM
)&item
))
678 int state
= item
.state
>> 12;
680 if (state
== CheckBitmapIndexUnchecked
)
682 CRYPT_OID_INFO
*info
= (CRYPT_OID_INFO
*)item
.lParam
;
683 BOOL firstString
= TRUE
;
686 str
= HeapAlloc(GetProcessHeap(), 0,
687 strlen(info
->pszOID
) + 1);
690 str
= HeapReAlloc(GetProcessHeap(), 0, str
,
691 strlen(str
) + 1 + strlen(info
->pszOID
) + 1);
696 LPSTR ptr
= firstString
? str
: str
+ strlen(str
);
700 strcpy(ptr
, info
->pszOID
);
705 if (!RegCreateKeyExW(HKEY_CURRENT_USER
, keyName
, 0, NULL
, 0, KEY_ALL_ACCESS
,
709 RegSetValueExA(key
, "Purpose", 0, REG_SZ
, (const BYTE
*)str
,
712 RegDeleteValueA(key
, "Purpose");
715 HeapFree(GetProcessHeap(), 0, str
);
718 static LRESULT CALLBACK
cert_mgr_advanced_dlg_proc(HWND hwnd
, UINT msg
,
719 WPARAM wp
, LPARAM lp
)
727 HWND lv
= GetDlgItem(hwnd
, IDC_CERTIFICATE_USAGES
);
728 HIMAGELIST imageList
;
729 LPSTR disabledUsages
;
731 GetWindowRect(lv
, &rc
);
732 column
.mask
= LVCF_WIDTH
;
733 column
.cx
= rc
.right
- rc
.left
;
734 SendMessageW(lv
, LVM_INSERTCOLUMNW
, 0, (LPARAM
)&column
);
735 imageList
= ImageList_Create(16, 16, ILC_COLOR4
| ILC_MASK
, 4, 0);
739 COLORREF backColor
= RGB(255, 0, 255);
741 bmp
= LoadBitmapW(hInstance
, MAKEINTRESOURCEW(IDB_CHECKS
));
742 ImageList_AddMasked(imageList
, bmp
, backColor
);
744 ImageList_SetBkColor(imageList
, CLR_NONE
);
745 SendMessageW(lv
, LVM_SETIMAGELIST
, LVSIL_STATE
, (LPARAM
)imageList
);
746 SetWindowLongPtrW(hwnd
, DWLP_USER
, (LPARAM
)imageList
);
748 add_known_usages_to_list(lv
, CheckBitmapIndexChecked
);
749 if ((disabledUsages
= get_cert_mgr_usages()))
753 for (ptr
= disabledUsages
, comma
= strchr(ptr
, ','); ptr
&& *ptr
;
754 ptr
= comma
? comma
+ 1 : NULL
,
755 comma
= ptr
? strchr(ptr
, ',') : NULL
)
761 if ((index
= find_oid_in_list(lv
, ptr
)) != -1)
762 toggle_usage(hwnd
, index
);
764 HeapFree(GetProcessHeap(), 0, disabledUsages
);
770 NMHDR
*hdr
= (NMHDR
*)lp
;
776 nm
= (NMITEMACTIVATE
*)lp
;
777 toggle_usage(hwnd
, nm
->iItem
);
778 SendMessageW(GetParent(hwnd
), PSM_CHANGED
, (WPARAM
)hwnd
, 0);
787 save_cert_mgr_usages(hwnd
);
788 ImageList_Destroy((HIMAGELIST
)GetWindowLongPtrW(hwnd
, DWLP_USER
));
789 EndDialog(hwnd
, IDOK
);
792 ImageList_Destroy((HIMAGELIST
)GetWindowLongPtrW(hwnd
, DWLP_USER
));
793 EndDialog(hwnd
, IDCANCEL
);
801 static void cert_mgr_clear_cert_selection(HWND hwnd
)
803 WCHAR empty
[] = { 0 };
805 EnableWindow(GetDlgItem(hwnd
, IDC_MGR_EXPORT
), FALSE
);
806 EnableWindow(GetDlgItem(hwnd
, IDC_MGR_REMOVE
), FALSE
);
807 EnableWindow(GetDlgItem(hwnd
, IDC_MGR_VIEW
), FALSE
);
808 SendMessageW(GetDlgItem(hwnd
, IDC_MGR_PURPOSES
), WM_SETTEXT
, 0,
810 refresh_store_certs(hwnd
);
813 static PCCERT_CONTEXT
cert_mgr_index_to_cert(HWND hwnd
, int index
)
815 PCCERT_CONTEXT cert
= NULL
;
818 item
.mask
= LVIF_PARAM
;
821 if (SendMessageW(GetDlgItem(hwnd
, IDC_MGR_CERTS
), LVM_GETITEMW
, 0,
823 cert
= (PCCERT_CONTEXT
)item
.lParam
;
827 static void show_selected_cert(HWND hwnd
, int index
)
829 PCCERT_CONTEXT cert
= cert_mgr_index_to_cert(hwnd
, index
);
833 CRYPTUI_VIEWCERTIFICATE_STRUCTW viewInfo
;
835 memset(&viewInfo
, 0, sizeof(viewInfo
));
836 viewInfo
.dwSize
= sizeof(viewInfo
);
837 viewInfo
.hwndParent
= hwnd
;
838 viewInfo
.pCertContext
= cert
;
839 /* FIXME: this should be modal */
840 CryptUIDlgViewCertificateW(&viewInfo
, NULL
);
844 static void cert_mgr_show_cert_usages(HWND hwnd
, int index
)
846 HWND text
= GetDlgItem(hwnd
, IDC_MGR_PURPOSES
);
847 PCCERT_CONTEXT cert
= cert_mgr_index_to_cert(hwnd
, index
);
848 PCERT_ENHKEY_USAGE usage
;
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
,
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
);
867 else if (CertGetEnhancedKeyUsage(cert
, CERT_FIND_EXT_ONLY_ENHKEY_USAGE_FLAG
,
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
);
882 if (usage
->cUsageIdentifier
)
884 static const WCHAR commaSpace
[] = { ',',' ',0 };
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
);
896 len
+= strlenW(info
->pwszName
);
898 len
+= strlen(usage
->rgpszUsageIdentifier
[i
]);
899 if (i
< usage
->cUsageIdentifier
- 1)
900 len
+= strlenW(commaSpace
);
902 str
= HeapAlloc(GetProcessHeap(), 0, len
* sizeof(WCHAR
));
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
);
914 strcpyW(ptr
, info
->pwszName
);
915 ptr
+= strlenW(info
->pwszName
);
919 LPCSTR src
= usage
->rgpszUsageIdentifier
[i
];
921 for (; *src
; ptr
++, src
++)
925 if (i
< usage
->cUsageIdentifier
- 1)
927 strcpyW(ptr
, commaSpace
);
928 ptr
+= strlenW(commaSpace
);
932 SendMessageW(text
, WM_SETTEXT
, 0, (LPARAM
)str
);
933 HeapFree(GetProcessHeap(), 0, str
);
935 HeapFree(GetProcessHeap(), 0, usage
);
939 WCHAR buf
[MAX_STRING_LEN
];
941 LoadStringW(hInstance
, IDS_ALLOWED_PURPOSE_NONE
, buf
,
942 sizeof(buf
) / sizeof(buf
[0]));
943 SendMessageW(text
, WM_SETTEXT
, 0, (LPARAM
)buf
);
948 WCHAR buf
[MAX_STRING_LEN
];
950 LoadStringW(hInstance
, IDS_ALLOWED_PURPOSE_ALL
, buf
,
951 sizeof(buf
) / sizeof(buf
[0]));
952 SendMessageW(text
, WM_SETTEXT
, 0, (LPARAM
)buf
);
956 static void cert_mgr_do_remove(HWND hwnd
)
958 int tabIndex
= SendMessageW(GetDlgItem(hwnd
, IDC_MGR_STORES
),
959 TCM_GETCURSEL
, 0, 0);
960 struct CertMgrData
*data
=
961 (struct CertMgrData
*)GetWindowLongPtrW(hwnd
, DWLP_USER
);
963 if (tabIndex
< data
->nStores
)
965 HWND lv
= GetDlgItem(hwnd
, IDC_MGR_CERTS
);
966 WCHAR warning
[MAX_STRING_LEN
], title
[MAX_STRING_LEN
];
970 if (SendMessageW(lv
, LVM_GETSELECTEDCOUNT
, 0, 0) > 1)
971 warningID
= data
->stores
[tabIndex
].removePluralWarning
;
973 warningID
= data
->stores
[tabIndex
].removeWarning
;
975 pTitle
= data
->title
;
978 LoadStringW(hInstance
, IDS_CERT_MGR
, title
,
979 sizeof(title
) / sizeof(title
[0]));
982 LoadStringW(hInstance
, warningID
, warning
,
983 sizeof(warning
) / sizeof(warning
[0]));
984 if (MessageBoxW(hwnd
, warning
, pTitle
, MB_YESNO
) == IDYES
)
989 selection
= SendMessageW(lv
, LVM_GETNEXTITEM
, selection
,
993 PCCERT_CONTEXT cert
= cert_mgr_index_to_cert(hwnd
,
996 CertDeleteCertificateFromStore(cert
);
998 } while (selection
>= 0);
999 cert_mgr_clear_cert_selection(hwnd
);
1004 static void cert_mgr_do_export(HWND hwnd
)
1006 HWND lv
= GetDlgItem(hwnd
, IDC_MGR_CERTS
);
1007 int selectionCount
= SendMessageW(lv
, LVM_GETSELECTEDCOUNT
, 0, 0);
1009 if (selectionCount
== 1)
1011 int selection
= SendMessageW(lv
, LVM_GETNEXTITEM
, -1,
1016 PCCERT_CONTEXT cert
= cert_mgr_index_to_cert(hwnd
, selection
);
1020 CRYPTUI_WIZ_EXPORT_INFO info
;
1022 info
.dwSize
= sizeof(info
);
1023 info
.pwszExportFileName
= NULL
;
1024 info
.dwSubjectChoice
= CRYPTUI_WIZ_EXPORT_CERT_CONTEXT
;
1025 info
.u
.pCertContext
= cert
;
1027 CryptUIWizExport(0, hwnd
, NULL
, &info
, NULL
);
1031 else if (selectionCount
> 1)
1033 HCERTSTORE store
= CertOpenStore(CERT_STORE_PROV_MEMORY
, 0, 0,
1034 CERT_STORE_CREATE_NEW_FLAG
, NULL
);
1038 CRYPTUI_WIZ_EXPORT_INFO info
;
1041 info
.dwSize
= sizeof(info
);
1042 info
.pwszExportFileName
= NULL
;
1043 info
.dwSubjectChoice
=
1044 CRYPTUI_WIZ_EXPORT_CERT_STORE_CERTIFICATES_ONLY
;
1045 info
.u
.hCertStore
= store
;
1048 selection
= SendMessageW(lv
, LVM_GETNEXTITEM
, selection
,
1052 PCCERT_CONTEXT cert
= cert_mgr_index_to_cert(hwnd
,
1055 CertAddCertificateContextToStore(store
, cert
,
1056 CERT_STORE_ADD_ALWAYS
, NULL
);
1058 } while (selection
>= 0);
1059 CryptUIWizExport(0, hwnd
, NULL
, &info
, NULL
);
1060 CertCloseStore(store
, 0);
1065 static LRESULT CALLBACK
cert_mgr_dlg_proc(HWND hwnd
, UINT msg
, WPARAM wp
,
1068 struct CertMgrData
*data
;
1074 PCCRYPTUI_CERT_MGR_STRUCT pCryptUICertMgr
=
1075 (PCCRYPTUI_CERT_MGR_STRUCT
)lp
;
1076 HWND tab
= GetDlgItem(hwnd
, IDC_MGR_STORES
);
1078 data
= HeapAlloc(GetProcessHeap(), 0, sizeof(struct CertMgrData
));
1081 data
->imageList
= ImageList_Create(16, 16, ILC_COLOR4
| ILC_MASK
, 2, 0);
1082 if (data
->imageList
)
1085 COLORREF backColor
= RGB(255, 0, 255);
1087 bmp
= LoadBitmapW(hInstance
, MAKEINTRESOURCEW(IDB_SMALL_ICONS
));
1088 ImageList_AddMasked(data
->imageList
, bmp
, backColor
);
1090 ImageList_SetBkColor(data
->imageList
, CLR_NONE
);
1091 SendMessageW(GetDlgItem(hwnd
, IDC_MGR_CERTS
), LVM_SETIMAGELIST
,
1092 LVSIL_SMALL
, (LPARAM
)data
->imageList
);
1094 SetWindowLongPtrW(hwnd
, DWLP_USER
, (LPARAM
)data
);
1095 data
->title
= pCryptUICertMgr
->pwszTitle
;
1097 initialize_purpose_selection(hwnd
);
1098 add_cert_columns(hwnd
);
1099 if (pCryptUICertMgr
->pwszTitle
)
1100 SendMessageW(hwnd
, WM_SETTEXT
, 0,
1101 (LPARAM
)pCryptUICertMgr
->pwszTitle
);
1102 show_cert_stores(hwnd
, pCryptUICertMgr
->dwFlags
, data
);
1103 show_store_certs(hwnd
, cert_mgr_index_to_store(tab
, 0));
1108 NMHDR
*hdr
= (NMHDR
*)lp
;
1113 cert_mgr_clear_cert_selection(hwnd
);
1115 case LVN_ITEMCHANGED
:
1118 HWND lv
= GetDlgItem(hwnd
, IDC_MGR_CERTS
);
1120 nm
= (NMITEMACTIVATE
*)lp
;
1121 if (nm
->uNewState
& LVN_ITEMACTIVATE
)
1123 int numSelected
= SendMessageW(lv
, LVM_GETSELECTEDCOUNT
, 0, 0);
1125 EnableWindow(GetDlgItem(hwnd
, IDC_MGR_EXPORT
), numSelected
> 0);
1126 EnableWindow(GetDlgItem(hwnd
, IDC_MGR_REMOVE
), numSelected
> 0);
1127 EnableWindow(GetDlgItem(hwnd
, IDC_MGR_VIEW
), numSelected
== 1);
1128 if (numSelected
== 1)
1129 cert_mgr_show_cert_usages(hwnd
, nm
->iItem
);
1134 show_selected_cert(hwnd
, ((NMITEMACTIVATE
*)lp
)->iItem
);
1138 NMLVKEYDOWN
*lvk
= (NMLVKEYDOWN
*)lp
;
1140 if (lvk
->wVKey
== VK_DELETE
)
1141 cert_mgr_do_remove(hwnd
);
1150 case ((CBN_SELCHANGE
<< 16) | IDC_MGR_PURPOSE_SELECTION
):
1151 cert_mgr_clear_cert_selection(hwnd
);
1153 case IDC_MGR_IMPORT
:
1154 if (CryptUIWizImport(0, hwnd
, NULL
, NULL
,
1155 cert_mgr_current_store(hwnd
)))
1156 refresh_store_certs(hwnd
);
1158 case IDC_MGR_ADVANCED
:
1159 if (DialogBoxW(hInstance
, MAKEINTRESOURCEW(IDD_CERT_MGR_ADVANCED
),
1160 hwnd
, cert_mgr_advanced_dlg_proc
) == IDOK
)
1162 HWND cb
= GetDlgItem(hwnd
, IDC_MGR_PURPOSE_SELECTION
);
1164 LPWSTR curString
= NULL
;
1166 index
= SendMessageW(cb
, CB_GETCURSEL
, 0, 0);
1169 len
= SendMessageW(cb
, CB_GETLBTEXTLEN
, index
, 0);
1170 curString
= HeapAlloc(GetProcessHeap(), 0,
1171 (len
+ 1) * sizeof(WCHAR
));
1172 SendMessageW(cb
, CB_GETLBTEXT
, index
, (LPARAM
)curString
);
1174 SendMessageW(cb
, CB_RESETCONTENT
, 0, 0);
1175 initialize_purpose_selection(hwnd
);
1178 index
= SendMessageW(cb
, CB_FINDSTRINGEXACT
, -1,
1181 SendMessageW(cb
, CB_SETCURSEL
, index
, 0);
1182 HeapFree(GetProcessHeap(), 0, curString
);
1184 refresh_store_certs(hwnd
);
1189 HWND lv
= GetDlgItem(hwnd
, IDC_MGR_CERTS
);
1190 int selection
= SendMessageW(lv
, LVM_GETNEXTITEM
, -1,
1194 show_selected_cert(hwnd
, selection
);
1197 case IDC_MGR_EXPORT
:
1198 cert_mgr_do_export(hwnd
);
1200 case IDC_MGR_REMOVE
:
1201 cert_mgr_do_remove(hwnd
);
1204 free_certs(GetDlgItem(hwnd
, IDC_MGR_CERTS
));
1205 close_stores(GetDlgItem(hwnd
, IDC_MGR_STORES
));
1206 close_stores(GetDlgItem(hwnd
, IDC_MGR_STORES
));
1207 data
= (struct CertMgrData
*)GetWindowLongPtrW(hwnd
, DWLP_USER
);
1208 ImageList_Destroy(data
->imageList
);
1209 HeapFree(GetProcessHeap(), 0, data
);
1210 EndDialog(hwnd
, IDCANCEL
);
1218 /***********************************************************************
1219 * CryptUIDlgCertMgr (CRYPTUI.@)
1221 BOOL WINAPI
CryptUIDlgCertMgr(PCCRYPTUI_CERT_MGR_STRUCT pCryptUICertMgr
)
1223 TRACE("(%p)\n", pCryptUICertMgr
);
1225 if (pCryptUICertMgr
->dwSize
!= sizeof(CRYPTUI_CERT_MGR_STRUCT
))
1227 WARN("unexpected size %d\n", pCryptUICertMgr
->dwSize
);
1228 SetLastError(E_INVALIDARG
);
1231 DialogBoxParamW(hInstance
, MAKEINTRESOURCEW(IDD_CERT_MGR
),
1232 pCryptUICertMgr
->hwndParent
, cert_mgr_dlg_proc
, (LPARAM
)pCryptUICertMgr
);
1236 /* FIXME: real names are unknown, functions are undocumented */
1237 typedef struct _CRYPTUI_ENUM_SYSTEM_STORE_ARGS
1240 void *pvSystemStoreLocationPara
;
1241 } CRYPTUI_ENUM_SYSTEM_STORE_ARGS
, *PCRYPTUI_ENUM_SYSTEM_STORE_ARGS
;
1243 typedef struct _CRYPTUI_ENUM_DATA
1246 HCERTSTORE
*rghStore
;
1248 PCRYPTUI_ENUM_SYSTEM_STORE_ARGS rgEnumArgs
;
1249 } CRYPTUI_ENUM_DATA
, *PCRYPTUI_ENUM_DATA
;
1251 typedef BOOL (WINAPI
*PFN_SELECTED_STORE_CB
)(HCERTSTORE store
, HWND hwnd
,
1254 /* Values for dwFlags */
1255 #define CRYPTUI_ENABLE_SHOW_PHYSICAL_STORE 0x00000001
1257 typedef struct _CRYPTUI_SELECTSTORE_INFO_A
1264 CRYPTUI_ENUM_DATA
*pEnumData
;
1265 PFN_SELECTED_STORE_CB pfnSelectedStoreCallback
;
1267 } CRYPTUI_SELECTSTORE_INFO_A
, *PCRYPTUI_SELECTSTORE_INFO_A
;
1269 typedef struct _CRYPTUI_SELECTSTORE_INFO_W
1276 CRYPTUI_ENUM_DATA
*pEnumData
;
1277 PFN_SELECTED_STORE_CB pfnSelectedStoreCallback
;
1279 } CRYPTUI_SELECTSTORE_INFO_W
, *PCRYPTUI_SELECTSTORE_INFO_W
;
1293 static BOOL WINAPI
enum_store_callback(const void *pvSystemStore
,
1294 DWORD dwFlags
, PCERT_SYSTEM_STORE_INFO pStoreInfo
, void *pvReserved
,
1297 HWND tree
= GetDlgItem(pvArg
, IDC_STORE_LIST
);
1298 TVINSERTSTRUCTW tvis
;
1299 LPCWSTR localizedName
;
1302 tvis
.hParent
= NULL
;
1303 tvis
.hInsertAfter
= TVI_LAST
;
1304 tvis
.u
.item
.mask
= TVIF_TEXT
;
1305 if ((localizedName
= CryptFindLocalizedName(pvSystemStore
)))
1307 struct StoreInfo
*storeInfo
= HeapAlloc(GetProcessHeap(), 0,
1308 sizeof(struct StoreInfo
));
1312 storeInfo
->type
= SystemStore
;
1313 storeInfo
->u
.name
= HeapAlloc(GetProcessHeap(), 0,
1314 (strlenW(pvSystemStore
) + 1) * sizeof(WCHAR
));
1315 if (storeInfo
->u
.name
)
1317 tvis
.u
.item
.mask
|= TVIF_PARAM
;
1318 tvis
.u
.item
.lParam
= (LPARAM
)storeInfo
;
1319 strcpyW(storeInfo
->u
.name
, pvSystemStore
);
1323 HeapFree(GetProcessHeap(), 0, storeInfo
);
1329 tvis
.u
.item
.pszText
= (LPWSTR
)localizedName
;
1332 tvis
.u
.item
.pszText
= (LPWSTR
)pvSystemStore
;
1333 /* FIXME: need a folder icon for the store too */
1335 SendMessageW(tree
, TVM_INSERTITEMW
, 0, (LPARAM
)&tvis
);
1339 static void enumerate_stores(HWND hwnd
, CRYPTUI_ENUM_DATA
*pEnumData
)
1342 HWND tree
= GetDlgItem(hwnd
, IDC_STORE_LIST
);
1344 for (i
= 0; i
< pEnumData
->cEnumArgs
; i
++)
1345 CertEnumSystemStore(pEnumData
->rgEnumArgs
[i
].dwFlags
,
1346 pEnumData
->rgEnumArgs
[i
].pvSystemStoreLocationPara
,
1347 hwnd
, enum_store_callback
);
1348 for (i
= 0; i
< pEnumData
->cStores
; i
++)
1352 if (CertGetStoreProperty(pEnumData
->rghStore
[i
],
1353 CERT_STORE_LOCALIZED_NAME_PROP_ID
, NULL
, &size
))
1355 LPWSTR name
= HeapAlloc(GetProcessHeap(), 0, size
);
1359 if (CertGetStoreProperty(pEnumData
->rghStore
[i
],
1360 CERT_STORE_LOCALIZED_NAME_PROP_ID
, name
, &size
))
1362 struct StoreInfo
*storeInfo
= HeapAlloc(GetProcessHeap(),
1363 0, sizeof(struct StoreInfo
));
1367 TVINSERTSTRUCTW tvis
;
1369 storeInfo
->type
= StoreHandle
;
1370 storeInfo
->u
.store
= pEnumData
->rghStore
[i
];
1371 tvis
.hParent
= NULL
;
1372 tvis
.hInsertAfter
= TVI_LAST
;
1373 tvis
.u
.item
.mask
= TVIF_TEXT
| TVIF_PARAM
;
1374 tvis
.u
.item
.pszText
= name
;
1375 tvis
.u
.item
.lParam
= (LPARAM
)storeInfo
;
1376 SendMessageW(tree
, TVM_INSERTITEMW
, 0, (LPARAM
)&tvis
);
1379 HeapFree(GetProcessHeap(), 0, name
);
1385 static void free_store_info(HWND tree
)
1387 HTREEITEM next
= (HTREEITEM
)SendMessageW(tree
, TVM_GETNEXTITEM
, TVGN_CHILD
,
1394 memset(&item
, 0, sizeof(item
));
1395 item
.mask
= TVIF_HANDLE
| TVIF_PARAM
;
1397 SendMessageW(tree
, TVM_GETITEMW
, 0, (LPARAM
)&item
);
1400 struct StoreInfo
*storeInfo
= (struct StoreInfo
*)item
.lParam
;
1402 if (storeInfo
->type
== SystemStore
)
1403 HeapFree(GetProcessHeap(), 0, storeInfo
->u
.name
);
1404 HeapFree(GetProcessHeap(), 0, storeInfo
);
1406 next
= (HTREEITEM
)SendMessageW(tree
, TVM_GETNEXTITEM
, TVGN_NEXT
,
1411 static HCERTSTORE
selected_item_to_store(HWND tree
, HTREEITEM hItem
)
1413 WCHAR buf
[MAX_STRING_LEN
];
1417 memset(&item
, 0, sizeof(item
));
1418 item
.mask
= TVIF_HANDLE
| TVIF_PARAM
| TVIF_TEXT
;
1420 item
.cchTextMax
= sizeof(buf
) / sizeof(buf
[0]);
1422 SendMessageW(tree
, TVM_GETITEMW
, 0, (LPARAM
)&item
);
1425 struct StoreInfo
*storeInfo
= (struct StoreInfo
*)item
.lParam
;
1427 if (storeInfo
->type
== StoreHandle
)
1428 store
= storeInfo
->u
.store
;
1430 store
= CertOpenSystemStoreW(0, storeInfo
->u
.name
);
1434 /* It's implicitly a system store */
1435 store
= CertOpenSystemStoreW(0, buf
);
1440 struct SelectStoreInfo
1442 PCRYPTUI_SELECTSTORE_INFO_W info
;
1446 static LRESULT CALLBACK
select_store_dlg_proc(HWND hwnd
, UINT msg
, WPARAM wp
,
1449 struct SelectStoreInfo
*selectInfo
;
1456 selectInfo
= (struct SelectStoreInfo
*)lp
;
1457 SetWindowLongPtrW(hwnd
, DWLP_USER
, lp
);
1458 if (selectInfo
->info
->pwszTitle
)
1459 SendMessageW(hwnd
, WM_SETTEXT
, 0,
1460 (LPARAM
)selectInfo
->info
->pwszTitle
);
1461 if (selectInfo
->info
->pwszText
)
1462 SendMessageW(GetDlgItem(hwnd
, IDC_STORE_TEXT
), WM_SETTEXT
, 0,
1463 (LPARAM
)selectInfo
->info
->pwszText
);
1464 if (!(selectInfo
->info
->dwFlags
& CRYPTUI_ENABLE_SHOW_PHYSICAL_STORE
))
1465 ShowWindow(GetDlgItem(hwnd
, IDC_SHOW_PHYSICAL_STORES
), FALSE
);
1466 enumerate_stores(hwnd
, selectInfo
->info
->pEnumData
);
1474 HWND tree
= GetDlgItem(hwnd
, IDC_STORE_LIST
);
1475 HTREEITEM selection
= (HTREEITEM
)SendMessageW(tree
,
1476 TVM_GETNEXTITEM
, TVGN_CARET
, (LPARAM
)NULL
);
1478 selectInfo
= (struct SelectStoreInfo
*)GetWindowLongPtrW(hwnd
,
1482 WCHAR title
[MAX_STRING_LEN
], error
[MAX_STRING_LEN
], *pTitle
;
1484 if (selectInfo
->info
->pwszTitle
)
1485 pTitle
= selectInfo
->info
->pwszTitle
;
1488 LoadStringW(hInstance
, IDS_SELECT_STORE_TITLE
, title
,
1489 sizeof(title
) / sizeof(title
[0]));
1492 LoadStringW(hInstance
, IDS_SELECT_STORE
, error
,
1493 sizeof(error
) / sizeof(error
[0]));
1494 MessageBoxW(hwnd
, error
, pTitle
, MB_ICONEXCLAMATION
| MB_OK
);
1498 HCERTSTORE store
= selected_item_to_store(tree
, selection
);
1500 if (!selectInfo
->info
->pfnSelectedStoreCallback
||
1501 selectInfo
->info
->pfnSelectedStoreCallback(store
, hwnd
,
1502 selectInfo
->info
->pvArg
))
1504 selectInfo
->store
= store
;
1505 free_store_info(tree
);
1506 EndDialog(hwnd
, IDOK
);
1509 CertCloseStore(store
, 0);
1515 free_store_info(GetDlgItem(hwnd
, IDC_STORE_LIST
));
1516 EndDialog(hwnd
, IDCANCEL
);
1525 /***********************************************************************
1526 * CryptUIDlgSelectStoreW (CRYPTUI.@)
1528 HCERTSTORE WINAPI
CryptUIDlgSelectStoreW(PCRYPTUI_SELECTSTORE_INFO_W info
)
1530 struct SelectStoreInfo selectInfo
= { info
, NULL
};
1532 TRACE("(%p)\n", info
);
1534 if (info
->dwSize
!= sizeof(CRYPTUI_SELECTSTORE_INFO_W
))
1536 WARN("unexpected size %d\n", info
->dwSize
);
1537 SetLastError(E_INVALIDARG
);
1540 DialogBoxParamW(hInstance
, MAKEINTRESOURCEW(IDD_SELECT_STORE
), info
->parent
,
1541 select_store_dlg_proc
, (LPARAM
)&selectInfo
);
1542 return selectInfo
.store
;
1545 /***********************************************************************
1546 * CryptUIDlgSelectStoreA (CRYPTUI.@)
1548 HCERTSTORE WINAPI
CryptUIDlgSelectStoreA(PCRYPTUI_SELECTSTORE_INFO_A info
)
1550 CRYPTUI_SELECTSTORE_INFO_W infoW
;
1554 TRACE("(%p)\n", info
);
1556 if (info
->dwSize
!= sizeof(CRYPTUI_SELECTSTORE_INFO_A
))
1558 WARN("unexpected size %d\n", info
->dwSize
);
1559 SetLastError(E_INVALIDARG
);
1562 memcpy(&infoW
, &info
, sizeof(info
));
1565 len
= MultiByteToWideChar(CP_ACP
, 0, info
->pszTitle
, -1, NULL
, 0);
1566 infoW
.pwszTitle
= HeapAlloc(GetProcessHeap(), 0, len
* sizeof(WCHAR
));
1567 MultiByteToWideChar(CP_ACP
, 0, info
->pszTitle
, -1, infoW
.pwszTitle
,
1572 len
= MultiByteToWideChar(CP_ACP
, 0, info
->pszText
, -1, NULL
, 0);
1573 infoW
.pwszText
= HeapAlloc(GetProcessHeap(), 0, len
* sizeof(WCHAR
));
1574 MultiByteToWideChar(CP_ACP
, 0, info
->pszText
, -1, infoW
.pwszText
, len
);
1576 ret
= CryptUIDlgSelectStoreW(&infoW
);
1577 HeapFree(GetProcessHeap(), 0, infoW
.pwszText
);
1578 HeapFree(GetProcessHeap(), 0, infoW
.pwszTitle
);
1582 /***********************************************************************
1583 * CryptUIDlgViewCertificateA (CRYPTUI.@)
1585 BOOL WINAPI
CryptUIDlgViewCertificateA(
1586 PCCRYPTUI_VIEWCERTIFICATE_STRUCTA pCertViewInfo
, BOOL
*pfPropertiesChanged
)
1588 CRYPTUI_VIEWCERTIFICATE_STRUCTW viewInfo
;
1589 LPWSTR title
= NULL
;
1592 TRACE("(%p, %p)\n", pCertViewInfo
, pfPropertiesChanged
);
1594 memcpy(&viewInfo
, pCertViewInfo
, sizeof(viewInfo
));
1595 if (pCertViewInfo
->szTitle
)
1597 int len
= MultiByteToWideChar(CP_ACP
, 0, pCertViewInfo
->szTitle
, -1,
1600 title
= HeapAlloc(GetProcessHeap(), 0, len
* sizeof(WCHAR
));
1603 MultiByteToWideChar(CP_ACP
, 0, pCertViewInfo
->szTitle
, -1, title
,
1605 viewInfo
.szTitle
= title
;
1613 if (pCertViewInfo
->cPropSheetPages
)
1615 FIXME("ignoring additional prop sheet pages\n");
1616 viewInfo
.cPropSheetPages
= 0;
1618 ret
= CryptUIDlgViewCertificateW(&viewInfo
, pfPropertiesChanged
);
1619 HeapFree(GetProcessHeap(), 0, title
);
1624 struct ReadStringStruct
1631 static DWORD CALLBACK
read_text_callback(DWORD_PTR dwCookie
, LPBYTE buf
,
1634 struct ReadStringStruct
*string
= (struct ReadStringStruct
*)dwCookie
;
1635 LONG cch
= min(cb
/ sizeof(WCHAR
), string
->len
- string
->pos
);
1637 TRACE("(%p, %p, %d, %p)\n", string
, buf
, cb
, pcb
);
1639 memmove(buf
, string
->buf
+ string
->pos
, cch
* sizeof(WCHAR
));
1641 *pcb
= cch
* sizeof(WCHAR
);
1645 static void add_unformatted_text_to_control(HWND hwnd
, LPCWSTR text
, LONG len
)
1647 struct ReadStringStruct string
;
1648 EDITSTREAM editstream
;
1650 TRACE("(%p, %s)\n", hwnd
, debugstr_wn(text
, len
));
1655 editstream
.dwCookie
= (DWORD_PTR
)&string
;
1656 editstream
.dwError
= 0;
1657 editstream
.pfnCallback
= read_text_callback
;
1658 SendMessageW(hwnd
, EM_STREAMIN
, SF_TEXT
| SFF_SELECTION
| SF_UNICODE
,
1659 (LPARAM
)&editstream
);
1662 static void add_string_resource_to_control(HWND hwnd
, int id
)
1667 len
= LoadStringW(hInstance
, id
, (LPWSTR
)&str
, 0);
1668 add_unformatted_text_to_control(hwnd
, str
, len
);
1671 static void add_text_with_paraformat_to_control(HWND hwnd
, LPCWSTR text
,
1672 LONG len
, const PARAFORMAT2
*fmt
)
1674 add_unformatted_text_to_control(hwnd
, text
, len
);
1675 SendMessageW(hwnd
, EM_SETPARAFORMAT
, 0, (LPARAM
)fmt
);
1678 static void add_string_resource_with_paraformat_to_control(HWND hwnd
, int id
,
1679 const PARAFORMAT2
*fmt
)
1684 len
= LoadStringW(hInstance
, id
, (LPWSTR
)&str
, 0);
1685 add_text_with_paraformat_to_control(hwnd
, str
, len
, fmt
);
1688 static LPWSTR
get_cert_name_string(PCCERT_CONTEXT pCertContext
, DWORD dwType
,
1694 len
= CertGetNameStringW(pCertContext
, dwType
, dwFlags
, NULL
, NULL
, 0);
1697 buf
= HeapAlloc(GetProcessHeap(), 0, len
* sizeof(WCHAR
));
1699 CertGetNameStringW(pCertContext
, dwType
, dwFlags
, NULL
, buf
, len
);
1704 static void add_cert_string_to_control(HWND hwnd
, PCCERT_CONTEXT pCertContext
,
1705 DWORD dwType
, DWORD dwFlags
)
1707 LPWSTR name
= get_cert_name_string(pCertContext
, dwType
, dwFlags
);
1711 /* Don't include NULL-terminator in output */
1712 DWORD len
= lstrlenW(name
);
1714 add_unformatted_text_to_control(hwnd
, name
, len
);
1715 HeapFree(GetProcessHeap(), 0, name
);
1719 static void add_icon_to_control(HWND hwnd
, int id
)
1722 LPRICHEDITOLE richEditOle
= NULL
;
1723 LPOLEOBJECT object
= NULL
;
1725 LPOLECACHE oleCache
= NULL
;
1726 FORMATETC formatEtc
;
1728 LPDATAOBJECT dataObject
= NULL
;
1729 HBITMAP bitmap
= NULL
;
1732 LPOLECLIENTSITE clientSite
= NULL
;
1735 TRACE("(%p, %d)\n", hwnd
, id
);
1737 SendMessageW(hwnd
, EM_GETOLEINTERFACE
, 0, (LPARAM
)&richEditOle
);
1740 hr
= OleCreateDefaultHandler(&CLSID_NULL
, NULL
, &IID_IOleObject
,
1744 hr
= IOleObject_GetUserClassID(object
, &clsid
);
1747 hr
= IOleObject_QueryInterface(object
, &IID_IOleCache
, (void**)&oleCache
);
1750 formatEtc
.cfFormat
= CF_BITMAP
;
1751 formatEtc
.ptd
= NULL
;
1752 formatEtc
.dwAspect
= DVASPECT_CONTENT
;
1753 formatEtc
.lindex
= -1;
1754 formatEtc
.tymed
= TYMED_GDI
;
1755 hr
= IOleCache_Cache(oleCache
, &formatEtc
, 0, &conn
);
1758 hr
= IOleObject_QueryInterface(object
, &IID_IDataObject
,
1759 (void**)&dataObject
);
1762 hr
= IRichEditOle_GetClientSite(richEditOle
, &clientSite
);
1765 bitmap
= LoadImageW(hInstance
, MAKEINTRESOURCEW(id
), IMAGE_BITMAP
, 0, 0,
1766 LR_DEFAULTSIZE
| LR_LOADTRANSPARENT
);
1769 rect
.left
= rect
.top
= 0;
1770 rect
.right
= GetSystemMetrics(SM_CXICON
);
1771 rect
.bottom
= GetSystemMetrics(SM_CYICON
);
1772 stgm
.tymed
= TYMED_GDI
;
1773 stgm
.u
.hBitmap
= bitmap
;
1774 stgm
.pUnkForRelease
= NULL
;
1775 hr
= IDataObject_SetData(dataObject
, &formatEtc
, &stgm
, TRUE
);
1779 reObject
.cbStruct
= sizeof(reObject
);
1780 reObject
.cp
= REO_CP_SELECTION
;
1781 reObject
.clsid
= clsid
;
1782 reObject
.poleobj
= object
;
1783 reObject
.pstg
= NULL
;
1784 reObject
.polesite
= clientSite
;
1785 reObject
.sizel
.cx
= reObject
.sizel
.cy
= 0;
1786 reObject
.dvaspect
= DVASPECT_CONTENT
;
1787 reObject
.dwFlags
= 0;
1788 reObject
.dwUser
= 0;
1790 IRichEditOle_InsertObject(richEditOle
, &reObject
);
1794 IOleClientSite_Release(clientSite
);
1796 IDataObject_Release(dataObject
);
1798 IOleCache_Release(oleCache
);
1800 IOleObject_Release(object
);
1802 IRichEditOle_Release(richEditOle
);
1805 #define MY_INDENT 200
1807 static void add_oid_text_to_control(HWND hwnd
, char *oid
)
1810 PCCRYPT_OID_INFO oidInfo
= CryptFindOIDInfo(CRYPT_OID_INFO_OID_KEY
, oid
, 0);
1813 parFmt
.cbSize
= sizeof(parFmt
);
1814 parFmt
.dwMask
= PFM_STARTINDENT
;
1815 parFmt
.dxStartIndent
= MY_INDENT
* 3;
1818 add_text_with_paraformat_to_control(hwnd
, oidInfo
->pwszName
,
1819 lstrlenW(oidInfo
->pwszName
), &parFmt
);
1820 add_unformatted_text_to_control(hwnd
, &nl
, 1);
1830 /* The following list MUST be lexicographically sorted by OID */
1831 static struct OIDToString oidMap
[] = {
1832 /* 1.3.6.1.4.1.311.10.3.1 */
1833 { szOID_KP_CTL_USAGE_SIGNING
, IDS_PURPOSE_CTL_USAGE_SIGNING
},
1834 /* 1.3.6.1.4.1.311.10.3.4 */
1835 { szOID_KP_EFS
, IDS_PURPOSE_EFS
},
1836 /* 1.3.6.1.4.1.311.10.3.4.1 */
1837 { szOID_EFS_RECOVERY
, IDS_PURPOSE_EFS_RECOVERY
},
1838 /* 1.3.6.1.4.1.311.10.3.5 */
1839 { szOID_WHQL_CRYPTO
, IDS_PURPOSE_WHQL
},
1840 /* 1.3.6.1.4.1.311.10.3.6 */
1841 { szOID_NT5_CRYPTO
, IDS_PURPOSE_NT5
},
1842 /* 1.3.6.1.4.1.311.10.3.7 */
1843 { szOID_OEM_WHQL_CRYPTO
, IDS_PURPOSE_OEM_WHQL
},
1844 /* 1.3.6.1.4.1.311.10.3.8 */
1845 { szOID_EMBEDDED_NT_CRYPTO
, IDS_PURPOSE_EMBEDDED_NT
},
1846 /* 1.3.6.1.4.1.311.10.3.9 */
1847 { szOID_ROOT_LIST_SIGNER
, IDS_PURPOSE_ROOT_LIST_SIGNER
},
1848 /* 1.3.6.1.4.1.311.10.3.10 */
1849 { szOID_KP_QUALIFIED_SUBORDINATION
, IDS_PURPOSE_QUALIFIED_SUBORDINATION
},
1850 /* 1.3.6.1.4.1.311.10.3.11 */
1851 { szOID_KP_KEY_RECOVERY
, IDS_PURPOSE_KEY_RECOVERY
},
1852 /* 1.3.6.1.4.1.311.10.3.12 */
1853 { szOID_KP_DOCUMENT_SIGNING
, IDS_PURPOSE_DOCUMENT_SIGNING
},
1854 /* 1.3.6.1.4.1.311.10.3.13 */
1855 { szOID_KP_LIFETIME_SIGNING
, IDS_PURPOSE_LIFETIME_SIGNING
},
1856 /* 1.3.6.1.4.1.311.10.5.1 */
1857 { szOID_DRM
, IDS_PURPOSE_DRM
},
1858 /* 1.3.6.1.4.1.311.10.6.1 */
1859 { szOID_LICENSES
, IDS_PURPOSE_LICENSES
},
1860 /* 1.3.6.1.4.1.311.10.6.2 */
1861 { szOID_LICENSE_SERVER
, IDS_PURPOSE_LICENSE_SERVER
},
1862 /* 1.3.6.1.4.1.311.20.2.1 */
1863 { szOID_ENROLLMENT_AGENT
, IDS_PURPOSE_ENROLLMENT_AGENT
},
1864 /* 1.3.6.1.4.1.311.20.2.2 */
1865 { szOID_KP_SMARTCARD_LOGON
, IDS_PURPOSE_SMARTCARD_LOGON
},
1866 /* 1.3.6.1.4.1.311.21.5 */
1867 { szOID_KP_CA_EXCHANGE
, IDS_PURPOSE_CA_EXCHANGE
},
1868 /* 1.3.6.1.4.1.311.21.6 */
1869 { szOID_KP_KEY_RECOVERY_AGENT
, IDS_PURPOSE_KEY_RECOVERY_AGENT
},
1870 /* 1.3.6.1.4.1.311.21.19 */
1871 { szOID_DS_EMAIL_REPLICATION
, IDS_PURPOSE_DS_EMAIL_REPLICATION
},
1872 /* 1.3.6.1.5.5.7.3.1 */
1873 { szOID_PKIX_KP_SERVER_AUTH
, IDS_PURPOSE_SERVER_AUTH
},
1874 /* 1.3.6.1.5.5.7.3.2 */
1875 { szOID_PKIX_KP_CLIENT_AUTH
, IDS_PURPOSE_CLIENT_AUTH
},
1876 /* 1.3.6.1.5.5.7.3.3 */
1877 { szOID_PKIX_KP_CODE_SIGNING
, IDS_PURPOSE_CODE_SIGNING
},
1878 /* 1.3.6.1.5.5.7.3.4 */
1879 { szOID_PKIX_KP_EMAIL_PROTECTION
, IDS_PURPOSE_EMAIL_PROTECTION
},
1880 /* 1.3.6.1.5.5.7.3.5 */
1881 { szOID_PKIX_KP_IPSEC_END_SYSTEM
, IDS_PURPOSE_IPSEC
},
1882 /* 1.3.6.1.5.5.7.3.6 */
1883 { szOID_PKIX_KP_IPSEC_TUNNEL
, IDS_PURPOSE_IPSEC
},
1884 /* 1.3.6.1.5.5.7.3.7 */
1885 { szOID_PKIX_KP_IPSEC_USER
, IDS_PURPOSE_IPSEC
},
1886 /* 1.3.6.1.5.5.7.3.8 */
1887 { szOID_PKIX_KP_TIMESTAMP_SIGNING
, IDS_PURPOSE_TIMESTAMP_SIGNING
},
1890 static struct OIDToString
*findSupportedOID(LPCSTR oid
)
1892 int indexHigh
= sizeof(oidMap
) / sizeof(oidMap
[0]) - 1, indexLow
= 0, i
;
1893 struct OIDToString
*ret
= NULL
;
1895 for (i
= (indexLow
+ indexHigh
) / 2; !ret
&& indexLow
<= indexHigh
;
1896 i
= (indexLow
+ indexHigh
) / 2)
1900 cmp
= strcmp(oid
, oidMap
[i
].oid
);
1911 static void add_local_oid_text_to_control(HWND text
, LPCSTR oid
)
1913 struct OIDToString
*entry
;
1917 parFmt
.cbSize
= sizeof(parFmt
);
1918 parFmt
.dwMask
= PFM_STARTINDENT
;
1919 parFmt
.dxStartIndent
= MY_INDENT
* 3;
1920 if ((entry
= findSupportedOID(oid
)))
1922 WCHAR
*str
, *linebreak
, *ptr
;
1923 BOOL multiline
= FALSE
;
1926 len
= LoadStringW(hInstance
, entry
->id
, (LPWSTR
)&str
, 0);
1929 if ((linebreak
= memchrW(ptr
, '\n', len
)))
1931 WCHAR copy
[MAX_STRING_LEN
];
1934 /* The source string contains a newline, which the richedit
1935 * control won't find since it's interpreted as a paragraph
1936 * break. Therefore copy up to the newline. lstrcpynW always
1937 * NULL-terminates, so pass one more than the length of the
1938 * source line so the copy includes the entire line and the
1941 lstrcpynW(copy
, ptr
, linebreak
- ptr
+ 1);
1942 add_text_with_paraformat_to_control(text
, copy
,
1943 linebreak
- ptr
, &parFmt
);
1944 ptr
= linebreak
+ 1;
1945 add_unformatted_text_to_control(text
, &nl
, 1);
1947 else if (multiline
&& *ptr
)
1949 /* Add the last line */
1950 add_text_with_paraformat_to_control(text
, ptr
,
1951 len
- (ptr
- str
), &parFmt
);
1952 add_unformatted_text_to_control(text
, &nl
, 1);
1954 } while (linebreak
);
1957 add_text_with_paraformat_to_control(text
, str
, len
, &parFmt
);
1958 add_unformatted_text_to_control(text
, &nl
, 1);
1963 WCHAR
*oidW
= HeapAlloc(GetProcessHeap(), 0,
1964 (strlen(oid
) + 1) * sizeof(WCHAR
));
1971 for (src
= oid
, dst
= oidW
; *src
; src
++, dst
++)
1974 add_text_with_paraformat_to_control(text
, oidW
, lstrlenW(oidW
),
1976 add_unformatted_text_to_control(text
, &nl
, 1);
1977 HeapFree(GetProcessHeap(), 0, oidW
);
1982 static void display_app_usages(HWND text
, PCCERT_CONTEXT cert
,
1983 BOOL
*anyUsageAdded
)
1985 static char any_app_policy
[] = szOID_ANY_APPLICATION_POLICY
;
1987 CHARFORMATW charFmt
;
1988 PCERT_EXTENSION policyExt
;
1989 if (!*anyUsageAdded
)
1993 parFmt
.cbSize
= sizeof(parFmt
);
1994 parFmt
.dwMask
= PFM_STARTINDENT
;
1995 parFmt
.dxStartIndent
= MY_INDENT
;
1996 add_string_resource_with_paraformat_to_control(text
,
1997 IDS_CERT_INFO_PURPOSES
, &parFmt
);
1998 add_unformatted_text_to_control(text
, &nl
, 1);
1999 *anyUsageAdded
= TRUE
;
2001 memset(&charFmt
, 0, sizeof(charFmt
));
2002 charFmt
.cbSize
= sizeof(charFmt
);
2003 charFmt
.dwMask
= CFM_BOLD
;
2004 charFmt
.dwEffects
= 0;
2005 SendMessageW(text
, EM_SETCHARFORMAT
, SCF_SELECTION
, (LPARAM
)&charFmt
);
2006 if ((policyExt
= CertFindExtension(szOID_APPLICATION_CERT_POLICIES
,
2007 cert
->pCertInfo
->cExtension
, cert
->pCertInfo
->rgExtension
)))
2009 CERT_POLICIES_INFO
*policies
;
2012 if (CryptDecodeObjectEx(X509_ASN_ENCODING
, X509_CERT_POLICIES
,
2013 policyExt
->Value
.pbData
, policyExt
->Value
.cbData
,
2014 CRYPT_DECODE_ALLOC_FLAG
, NULL
, &policies
, &size
))
2018 for (i
= 0; i
< policies
->cPolicyInfo
; i
++)
2022 for (j
= 0; j
< policies
->rgPolicyInfo
[i
].cPolicyQualifier
; j
++)
2023 add_local_oid_text_to_control(text
,
2024 policies
->rgPolicyInfo
[i
].rgPolicyQualifier
[j
].
2025 pszPolicyQualifierId
);
2027 LocalFree(policies
);
2031 add_oid_text_to_control(text
, any_app_policy
);
2034 static BOOL
display_cert_usages(HWND text
, PCCERT_CONTEXT cert
,
2035 BOOL
*anyUsageAdded
)
2039 BOOL badUsages
= FALSE
;
2041 if (CertGetEnhancedKeyUsage(cert
, 0, NULL
, &size
))
2043 CHARFORMATW charFmt
;
2044 static char any_cert_policy
[] = szOID_ANY_CERT_POLICY
;
2045 PCERT_ENHKEY_USAGE usage
= HeapAlloc(GetProcessHeap(), 0, size
);
2049 if (CertGetEnhancedKeyUsage(cert
, 0, usage
, &size
))
2053 if (!*anyUsageAdded
)
2057 parFmt
.cbSize
= sizeof(parFmt
);
2058 parFmt
.dwMask
= PFM_STARTINDENT
;
2059 parFmt
.dxStartIndent
= MY_INDENT
;
2060 add_string_resource_with_paraformat_to_control(text
,
2061 IDS_CERT_INFO_PURPOSES
, &parFmt
);
2062 add_unformatted_text_to_control(text
, &nl
, 1);
2063 *anyUsageAdded
= TRUE
;
2065 memset(&charFmt
, 0, sizeof(charFmt
));
2066 charFmt
.cbSize
= sizeof(charFmt
);
2067 charFmt
.dwMask
= CFM_BOLD
;
2068 charFmt
.dwEffects
= 0;
2069 SendMessageW(text
, EM_SETCHARFORMAT
, SCF_SELECTION
,
2071 if (!usage
->cUsageIdentifier
)
2072 add_oid_text_to_control(text
, any_cert_policy
);
2074 for (i
= 0; i
< usage
->cUsageIdentifier
; i
++)
2075 add_local_oid_text_to_control(text
,
2076 usage
->rgpszUsageIdentifier
[i
]);
2080 HeapFree(GetProcessHeap(), 0, usage
);
2090 static void set_policy_text(HWND text
,
2091 PCCRYPTUI_VIEWCERTIFICATE_STRUCTW pCertViewInfo
)
2093 BOOL includeCertUsages
= FALSE
, includeAppUsages
= FALSE
;
2094 BOOL badUsages
= FALSE
, anyUsageAdded
= FALSE
;
2096 if (pCertViewInfo
->cPurposes
)
2100 for (i
= 0; i
< pCertViewInfo
->cPurposes
; i
++)
2102 if (!strcmp(pCertViewInfo
->rgszPurposes
[i
], szOID_ANY_CERT_POLICY
))
2103 includeCertUsages
= TRUE
;
2104 else if (!strcmp(pCertViewInfo
->rgszPurposes
[i
],
2105 szOID_ANY_APPLICATION_POLICY
))
2106 includeAppUsages
= TRUE
;
2112 includeAppUsages
= includeCertUsages
= TRUE
;
2113 if (includeAppUsages
)
2114 display_app_usages(text
, pCertViewInfo
->pCertContext
, &anyUsageAdded
);
2115 if (includeCertUsages
)
2116 badUsages
= display_cert_usages(text
, pCertViewInfo
->pCertContext
,
2122 parFmt
.cbSize
= sizeof(parFmt
);
2123 parFmt
.dwMask
= PFM_STARTINDENT
;
2124 parFmt
.dxStartIndent
= MY_INDENT
;
2125 add_string_resource_with_paraformat_to_control(text
,
2126 IDS_CERT_INFO_BAD_PURPOSES
, &parFmt
);
2130 static CRYPT_OBJID_BLOB
*find_policy_qualifier(CERT_POLICIES_INFO
*policies
,
2133 CRYPT_OBJID_BLOB
*ret
= NULL
;
2136 for (i
= 0; !ret
&& i
< policies
->cPolicyInfo
; i
++)
2140 for (j
= 0; !ret
&& j
< policies
->rgPolicyInfo
[i
].cPolicyQualifier
; j
++)
2141 if (!strcmp(policies
->rgPolicyInfo
[i
].rgPolicyQualifier
[j
].
2142 pszPolicyQualifierId
, policyOid
))
2143 ret
= &policies
->rgPolicyInfo
[i
].rgPolicyQualifier
[j
].
2149 static WCHAR
*get_cps_str_from_qualifier(CRYPT_OBJID_BLOB
*qualifier
)
2151 LPWSTR qualifierStr
= NULL
;
2152 CERT_NAME_VALUE
*qualifierValue
;
2155 if (CryptDecodeObjectEx(X509_ASN_ENCODING
, X509_NAME_VALUE
,
2156 qualifier
->pbData
, qualifier
->cbData
, CRYPT_DECODE_ALLOC_FLAG
, NULL
,
2157 &qualifierValue
, &size
))
2159 size
= CertRDNValueToStrW(qualifierValue
->dwValueType
,
2160 &qualifierValue
->Value
, NULL
, 0);
2161 qualifierStr
= HeapAlloc(GetProcessHeap(), 0, size
* sizeof(WCHAR
));
2163 CertRDNValueToStrW(qualifierValue
->dwValueType
,
2164 &qualifierValue
->Value
, qualifierStr
, size
);
2165 LocalFree(qualifierValue
);
2167 return qualifierStr
;
2170 static WCHAR
*get_user_notice_from_qualifier(CRYPT_OBJID_BLOB
*qualifier
)
2173 CERT_POLICY_QUALIFIER_USER_NOTICE
*qualifierValue
;
2176 if (CryptDecodeObjectEx(X509_ASN_ENCODING
,
2177 X509_PKIX_POLICY_QUALIFIER_USERNOTICE
,
2178 qualifier
->pbData
, qualifier
->cbData
, CRYPT_DECODE_ALLOC_FLAG
, NULL
,
2179 &qualifierValue
, &size
))
2181 str
= HeapAlloc(GetProcessHeap(), 0,
2182 (strlenW(qualifierValue
->pszDisplayText
) + 1) * sizeof(WCHAR
));
2184 strcpyW(str
, qualifierValue
->pszDisplayText
);
2185 LocalFree(qualifierValue
);
2190 struct IssuerStatement
2196 static void set_issuer_statement(HWND hwnd
,
2197 PCCRYPTUI_VIEWCERTIFICATE_STRUCTW pCertViewInfo
)
2199 PCERT_EXTENSION policyExt
;
2201 if (!(pCertViewInfo
->dwFlags
& CRYPTUI_DISABLE_ISSUERSTATEMENT
) &&
2202 (policyExt
= CertFindExtension(szOID_CERT_POLICIES
,
2203 pCertViewInfo
->pCertContext
->pCertInfo
->cExtension
,
2204 pCertViewInfo
->pCertContext
->pCertInfo
->rgExtension
)))
2206 CERT_POLICIES_INFO
*policies
;
2209 if (CryptDecodeObjectEx(X509_ASN_ENCODING
, policyExt
->pszObjId
,
2210 policyExt
->Value
.pbData
, policyExt
->Value
.cbData
,
2211 CRYPT_DECODE_ALLOC_FLAG
, NULL
, &policies
, &size
))
2213 CRYPT_OBJID_BLOB
*qualifier
;
2214 LPWSTR cps
= NULL
, userNotice
= NULL
;
2216 if ((qualifier
= find_policy_qualifier(policies
,
2217 szOID_PKIX_POLICY_QUALIFIER_CPS
)))
2218 cps
= get_cps_str_from_qualifier(qualifier
);
2219 if ((qualifier
= find_policy_qualifier(policies
,
2220 szOID_PKIX_POLICY_QUALIFIER_USERNOTICE
)))
2221 userNotice
= get_user_notice_from_qualifier(qualifier
);
2222 if (cps
|| userNotice
)
2224 struct IssuerStatement
*issuerStatement
=
2225 HeapAlloc(GetProcessHeap(), 0, sizeof(struct IssuerStatement
));
2227 if (issuerStatement
)
2229 issuerStatement
->cps
= cps
;
2230 issuerStatement
->userNotice
= userNotice
;
2231 EnableWindow(GetDlgItem(hwnd
, IDC_ISSUERSTATEMENT
), TRUE
);
2232 SetWindowLongPtrW(hwnd
, DWLP_USER
,
2233 (ULONG_PTR
)issuerStatement
);
2236 LocalFree(policies
);
2241 static void set_cert_info(HWND hwnd
,
2242 PCCRYPTUI_VIEWCERTIFICATE_STRUCTW pCertViewInfo
)
2244 CHARFORMATW charFmt
;
2246 HWND icon
= GetDlgItem(hwnd
, IDC_CERTIFICATE_ICON
);
2247 HWND text
= GetDlgItem(hwnd
, IDC_CERTIFICATE_INFO
);
2248 CRYPT_PROVIDER_SGNR
*provSigner
= WTHelperGetProvSignerFromChain(
2249 (CRYPT_PROVIDER_DATA
*)pCertViewInfo
->u
.pCryptProviderData
,
2250 pCertViewInfo
->idxSigner
, pCertViewInfo
->fCounterSigner
,
2251 pCertViewInfo
->idxCounterSigner
);
2252 CRYPT_PROVIDER_CERT
*root
=
2253 &provSigner
->pasCertChain
[provSigner
->csCertChain
- 1];
2255 if (!provSigner
->pChainContext
||
2256 (provSigner
->pChainContext
->TrustStatus
.dwErrorStatus
&
2257 CERT_TRUST_IS_PARTIAL_CHAIN
))
2258 add_icon_to_control(icon
, IDB_CERT_WARNING
);
2259 else if (!root
->fTrustedRoot
)
2260 add_icon_to_control(icon
, IDB_CERT_ERROR
);
2262 add_icon_to_control(icon
, IDB_CERT
);
2264 memset(&charFmt
, 0, sizeof(charFmt
));
2265 charFmt
.cbSize
= sizeof(charFmt
);
2266 charFmt
.dwMask
= CFM_BOLD
;
2267 charFmt
.dwEffects
= CFE_BOLD
;
2268 SendMessageW(text
, EM_SETCHARFORMAT
, SCF_SELECTION
, (LPARAM
)&charFmt
);
2269 /* FIXME: vertically center text */
2270 parFmt
.cbSize
= sizeof(parFmt
);
2271 parFmt
.dwMask
= PFM_STARTINDENT
;
2272 parFmt
.dxStartIndent
= MY_INDENT
;
2273 add_string_resource_with_paraformat_to_control(text
,
2274 IDS_CERTIFICATEINFORMATION
, &parFmt
);
2276 text
= GetDlgItem(hwnd
, IDC_CERTIFICATE_STATUS
);
2277 SendMessageW(text
, EM_SETCHARFORMAT
, SCF_SELECTION
, (LPARAM
)&charFmt
);
2278 if (provSigner
->dwError
== TRUST_E_CERT_SIGNATURE
)
2279 add_string_resource_with_paraformat_to_control(text
,
2280 IDS_CERT_INFO_BAD_SIG
, &parFmt
);
2281 else if (!provSigner
->pChainContext
||
2282 (provSigner
->pChainContext
->TrustStatus
.dwErrorStatus
&
2283 CERT_TRUST_IS_PARTIAL_CHAIN
))
2284 add_string_resource_with_paraformat_to_control(text
,
2285 IDS_CERT_INFO_PARTIAL_CHAIN
, &parFmt
);
2286 else if (!root
->fTrustedRoot
)
2288 if (provSigner
->csCertChain
== 1 && root
->fSelfSigned
)
2289 add_string_resource_with_paraformat_to_control(text
,
2290 IDS_CERT_INFO_UNTRUSTED_CA
, &parFmt
);
2292 add_string_resource_with_paraformat_to_control(text
,
2293 IDS_CERT_INFO_UNTRUSTED_ROOT
, &parFmt
);
2297 set_policy_text(text
, pCertViewInfo
);
2298 set_issuer_statement(hwnd
, pCertViewInfo
);
2302 static void set_cert_name_string(HWND hwnd
, PCCERT_CONTEXT cert
,
2303 DWORD nameFlags
, int heading
)
2306 HWND text
= GetDlgItem(hwnd
, IDC_CERTIFICATE_NAMES
);
2307 CHARFORMATW charFmt
;
2310 memset(&charFmt
, 0, sizeof(charFmt
));
2311 charFmt
.cbSize
= sizeof(charFmt
);
2312 charFmt
.dwMask
= CFM_BOLD
;
2313 charFmt
.dwEffects
= CFE_BOLD
;
2314 SendMessageW(text
, EM_SETCHARFORMAT
, SCF_SELECTION
, (LPARAM
)&charFmt
);
2315 parFmt
.cbSize
= sizeof(parFmt
);
2316 parFmt
.dwMask
= PFM_STARTINDENT
;
2317 parFmt
.dxStartIndent
= MY_INDENT
* 3;
2318 add_string_resource_with_paraformat_to_control(text
, heading
, &parFmt
);
2319 charFmt
.dwEffects
= 0;
2320 SendMessageW(text
, EM_SETCHARFORMAT
, SCF_SELECTION
, (LPARAM
)&charFmt
);
2321 add_cert_string_to_control(text
, cert
, CERT_NAME_SIMPLE_DISPLAY_TYPE
,
2323 add_unformatted_text_to_control(text
, &nl
, 1);
2324 add_unformatted_text_to_control(text
, &nl
, 1);
2325 add_unformatted_text_to_control(text
, &nl
, 1);
2329 static void add_date_string_to_control(HWND hwnd
, const FILETIME
*fileTime
)
2331 WCHAR dateFmt
[80]; /* sufficient for all versions of LOCALE_SSHORTDATE */
2335 GetLocaleInfoW(LOCALE_SYSTEM_DEFAULT
, LOCALE_SSHORTDATE
, dateFmt
,
2336 sizeof(dateFmt
) / sizeof(dateFmt
[0]));
2337 FileTimeToSystemTime(fileTime
, &sysTime
);
2338 GetDateFormatW(LOCALE_SYSTEM_DEFAULT
, 0, &sysTime
, dateFmt
, date
,
2339 sizeof(date
) / sizeof(date
[0]));
2340 add_unformatted_text_to_control(hwnd
, date
, lstrlenW(date
));
2343 static void set_cert_validity_period(HWND hwnd
, PCCERT_CONTEXT cert
)
2346 HWND text
= GetDlgItem(hwnd
, IDC_CERTIFICATE_NAMES
);
2347 CHARFORMATW charFmt
;
2350 memset(&charFmt
, 0, sizeof(charFmt
));
2351 charFmt
.cbSize
= sizeof(charFmt
);
2352 charFmt
.dwMask
= CFM_BOLD
;
2353 charFmt
.dwEffects
= CFE_BOLD
;
2354 SendMessageW(text
, EM_SETCHARFORMAT
, SCF_SELECTION
, (LPARAM
)&charFmt
);
2355 parFmt
.cbSize
= sizeof(parFmt
);
2356 parFmt
.dwMask
= PFM_STARTINDENT
;
2357 parFmt
.dxStartIndent
= MY_INDENT
* 3;
2358 add_string_resource_with_paraformat_to_control(text
, IDS_VALID_FROM
,
2360 charFmt
.dwEffects
= 0;
2361 SendMessageW(text
, EM_SETCHARFORMAT
, SCF_SELECTION
, (LPARAM
)&charFmt
);
2362 add_date_string_to_control(text
, &cert
->pCertInfo
->NotBefore
);
2363 charFmt
.dwEffects
= CFE_BOLD
;
2364 SendMessageW(text
, EM_SETCHARFORMAT
, SCF_SELECTION
, (LPARAM
)&charFmt
);
2365 add_string_resource_to_control(text
, IDS_VALID_TO
);
2366 charFmt
.dwEffects
= 0;
2367 SendMessageW(text
, EM_SETCHARFORMAT
, SCF_SELECTION
, (LPARAM
)&charFmt
);
2368 add_date_string_to_control(text
, &cert
->pCertInfo
->NotAfter
);
2369 add_unformatted_text_to_control(text
, &nl
, 1);
2372 static void set_general_info(HWND hwnd
,
2373 PCCRYPTUI_VIEWCERTIFICATE_STRUCTW pCertViewInfo
)
2375 set_cert_info(hwnd
, pCertViewInfo
);
2376 set_cert_name_string(hwnd
, pCertViewInfo
->pCertContext
, 0,
2377 IDS_SUBJECT_HEADING
);
2378 set_cert_name_string(hwnd
, pCertViewInfo
->pCertContext
,
2379 CERT_NAME_ISSUER_FLAG
, IDS_ISSUER_HEADING
);
2380 set_cert_validity_period(hwnd
, pCertViewInfo
->pCertContext
);
2383 static LRESULT CALLBACK
user_notice_dlg_proc(HWND hwnd
, UINT msg
, WPARAM wp
,
2388 struct IssuerStatement
*issuerStatement
;
2393 text
= GetDlgItem(hwnd
, IDC_USERNOTICE
);
2394 issuerStatement
= (struct IssuerStatement
*)lp
;
2395 add_unformatted_text_to_control(text
, issuerStatement
->userNotice
,
2396 strlenW(issuerStatement
->userNotice
));
2397 if (issuerStatement
->cps
)
2398 SetWindowLongPtrW(hwnd
, DWLP_USER
, (LPARAM
)issuerStatement
->cps
);
2400 EnableWindow(GetDlgItem(hwnd
, IDC_CPS
), FALSE
);
2406 EndDialog(hwnd
, IDOK
);
2411 IBindCtx
*bctx
= NULL
;
2414 CreateBindCtx(0, &bctx
);
2415 cps
= (LPWSTR
)GetWindowLongPtrW(hwnd
, DWLP_USER
);
2416 HlinkSimpleNavigateToString(cps
, NULL
, NULL
, NULL
, bctx
, NULL
,
2417 HLNF_OPENINNEWWINDOW
, 0);
2418 IBindCtx_Release(bctx
);
2426 static void show_user_notice(HWND hwnd
, struct IssuerStatement
*issuerStatement
)
2428 DialogBoxParamW(hInstance
, MAKEINTRESOURCEW(IDD_USERNOTICE
), hwnd
,
2429 user_notice_dlg_proc
, (LPARAM
)issuerStatement
);
2432 static LRESULT CALLBACK
general_dlg_proc(HWND hwnd
, UINT msg
, WPARAM wp
,
2435 PROPSHEETPAGEW
*page
;
2436 PCCRYPTUI_VIEWCERTIFICATE_STRUCTW pCertViewInfo
;
2438 TRACE("(%p, %08x, %08lx, %08lx)\n", hwnd
, msg
, wp
, lp
);
2443 page
= (PROPSHEETPAGEW
*)lp
;
2444 pCertViewInfo
= (PCCRYPTUI_VIEWCERTIFICATE_STRUCTW
)page
->lParam
;
2445 if (pCertViewInfo
->dwFlags
& CRYPTUI_DISABLE_ADDTOSTORE
)
2446 ShowWindow(GetDlgItem(hwnd
, IDC_ADDTOSTORE
), FALSE
);
2447 EnableWindow(GetDlgItem(hwnd
, IDC_ISSUERSTATEMENT
), FALSE
);
2448 set_general_info(hwnd
, pCertViewInfo
);
2453 case IDC_ADDTOSTORE
:
2454 CryptUIWizImport(0, hwnd
, NULL
, NULL
, NULL
);
2456 case IDC_ISSUERSTATEMENT
:
2458 struct IssuerStatement
*issuerStatement
=
2459 (struct IssuerStatement
*)GetWindowLongPtrW(hwnd
, DWLP_USER
);
2461 if (issuerStatement
)
2463 if (issuerStatement
->userNotice
)
2464 show_user_notice(hwnd
, issuerStatement
);
2465 else if (issuerStatement
->cps
)
2467 IBindCtx
*bctx
= NULL
;
2469 CreateBindCtx(0, &bctx
);
2470 HlinkSimpleNavigateToString(issuerStatement
->cps
, NULL
,
2471 NULL
, NULL
, bctx
, NULL
, HLNF_OPENINNEWWINDOW
, 0);
2472 IBindCtx_Release(bctx
);
2483 static UINT CALLBACK
general_callback_proc(HWND hwnd
, UINT msg
,
2484 PROPSHEETPAGEW
*page
)
2486 struct IssuerStatement
*issuerStatement
;
2492 (struct IssuerStatement
*)GetWindowLongPtrW(hwnd
, DWLP_USER
);
2493 if (issuerStatement
)
2495 HeapFree(GetProcessHeap(), 0, issuerStatement
->cps
);
2496 HeapFree(GetProcessHeap(), 0, issuerStatement
->userNotice
);
2497 HeapFree(GetProcessHeap(), 0, issuerStatement
);
2504 static void init_general_page(PCCRYPTUI_VIEWCERTIFICATE_STRUCTW pCertViewInfo
,
2505 PROPSHEETPAGEW
*page
)
2507 memset(page
, 0, sizeof(PROPSHEETPAGEW
));
2508 page
->dwSize
= sizeof(PROPSHEETPAGEW
);
2509 page
->dwFlags
= PSP_USECALLBACK
;
2510 page
->pfnCallback
= general_callback_proc
;
2511 page
->hInstance
= hInstance
;
2512 page
->u
.pszTemplate
= MAKEINTRESOURCEW(IDD_GENERAL
);
2513 page
->pfnDlgProc
= general_dlg_proc
;
2514 page
->lParam
= (LPARAM
)pCertViewInfo
;
2517 typedef WCHAR
* (*field_format_func
)(PCCERT_CONTEXT cert
);
2519 static WCHAR
*field_format_version(PCCERT_CONTEXT cert
)
2521 static const WCHAR fmt
[] = { 'V','%','d',0 };
2522 WCHAR
*buf
= HeapAlloc(GetProcessHeap(), 0, 12 * sizeof(WCHAR
));
2525 sprintfW(buf
, fmt
, cert
->pCertInfo
->dwVersion
);
2529 static WCHAR
*format_hex_string(void *pb
, DWORD cb
)
2531 WCHAR
*buf
= HeapAlloc(GetProcessHeap(), 0, (cb
* 3 + 1) * sizeof(WCHAR
));
2535 static const WCHAR fmt
[] = { '%','0','2','x',' ',0 };
2539 for (i
= 0, ptr
= buf
; i
< cb
; i
++, ptr
+= 3)
2540 sprintfW(ptr
, fmt
, ((BYTE
*)pb
)[i
]);
2545 static WCHAR
*field_format_serial_number(PCCERT_CONTEXT cert
)
2547 return format_hex_string(cert
->pCertInfo
->SerialNumber
.pbData
,
2548 cert
->pCertInfo
->SerialNumber
.cbData
);
2551 static WCHAR
*field_format_issuer(PCCERT_CONTEXT cert
)
2553 return get_cert_name_string(cert
, CERT_NAME_SIMPLE_DISPLAY_TYPE
,
2554 CERT_NAME_ISSUER_FLAG
);
2557 static WCHAR
*field_format_detailed_cert_name(PCERT_NAME_BLOB name
)
2560 DWORD len
= CertNameToStrW(X509_ASN_ENCODING
, name
,
2561 CERT_X500_NAME_STR
| CERT_NAME_STR_CRLF_FLAG
, NULL
, 0);
2565 str
= HeapAlloc(GetProcessHeap(), 0, len
* sizeof(WCHAR
));
2567 CertNameToStrW(X509_ASN_ENCODING
, name
,
2568 CERT_X500_NAME_STR
| CERT_NAME_STR_CRLF_FLAG
, str
, len
);
2573 static WCHAR
*field_format_detailed_issuer(PCCERT_CONTEXT cert
, void *param
)
2575 return field_format_detailed_cert_name(&cert
->pCertInfo
->Issuer
);
2578 static WCHAR
*field_format_subject(PCCERT_CONTEXT cert
)
2580 return get_cert_name_string(cert
, CERT_NAME_SIMPLE_DISPLAY_TYPE
, 0);
2583 static WCHAR
*field_format_detailed_subject(PCCERT_CONTEXT cert
, void *param
)
2585 return field_format_detailed_cert_name(&cert
->pCertInfo
->Subject
);
2588 static WCHAR
*format_long_date(const FILETIME
*fileTime
)
2590 WCHAR dateFmt
[80]; /* long enough for LOCALE_SLONGDATE */
2595 /* FIXME: format isn't quite right, want time too */
2596 GetLocaleInfoW(LOCALE_SYSTEM_DEFAULT
, LOCALE_SLONGDATE
, dateFmt
,
2597 sizeof(dateFmt
) / sizeof(dateFmt
[0]));
2598 FileTimeToSystemTime(fileTime
, &sysTime
);
2599 len
= GetDateFormatW(LOCALE_SYSTEM_DEFAULT
, 0, &sysTime
, dateFmt
, NULL
, 0);
2602 buf
= HeapAlloc(GetProcessHeap(), 0, len
* sizeof(WCHAR
));
2604 GetDateFormatW(LOCALE_SYSTEM_DEFAULT
, 0, &sysTime
, dateFmt
, buf
,
2610 static WCHAR
*field_format_from_date(PCCERT_CONTEXT cert
)
2612 return format_long_date(&cert
->pCertInfo
->NotBefore
);
2615 static WCHAR
*field_format_to_date(PCCERT_CONTEXT cert
)
2617 return format_long_date(&cert
->pCertInfo
->NotAfter
);
2620 static WCHAR
*field_format_public_key(PCCERT_CONTEXT cert
)
2622 PCCRYPT_OID_INFO oidInfo
;
2625 oidInfo
= CryptFindOIDInfo(CRYPT_OID_INFO_OID_KEY
,
2626 cert
->pCertInfo
->SubjectPublicKeyInfo
.Algorithm
.pszObjId
, 0);
2629 WCHAR fmt
[MAX_STRING_LEN
];
2631 if (LoadStringW(hInstance
, IDS_FIELD_PUBLIC_KEY_FORMAT
, fmt
,
2632 sizeof(fmt
) / sizeof(fmt
[0])))
2634 /* Allocate the output buffer. Use the number of bytes in the
2635 * public key as a conservative (high) estimate for the number of
2636 * digits in its output.
2637 * The output is of the form (in English)
2638 * "<public key algorithm> (<public key bit length> bits)".
2639 * Ordinarily having two positional parameters in a string is not a
2640 * good idea, but as this isn't a sentence fragment, it shouldn't
2641 * be word-order dependent.
2643 buf
= HeapAlloc(GetProcessHeap(), 0,
2644 (strlenW(fmt
) + strlenW(oidInfo
->pwszName
) +
2645 cert
->pCertInfo
->SubjectPublicKeyInfo
.PublicKey
.cbData
* 8)
2648 sprintfW(buf
, fmt
, oidInfo
->pwszName
,
2649 CertGetPublicKeyLength(X509_ASN_ENCODING
,
2650 &cert
->pCertInfo
->SubjectPublicKeyInfo
));
2656 static WCHAR
*field_format_detailed_public_key(PCCERT_CONTEXT cert
, void *param
)
2658 return format_hex_string(
2659 cert
->pCertInfo
->SubjectPublicKeyInfo
.PublicKey
.pbData
,
2660 cert
->pCertInfo
->SubjectPublicKeyInfo
.PublicKey
.cbData
);
2663 struct field_value_data
;
2666 PCCRYPTUI_VIEWCERTIFICATE_STRUCTW pCertViewInfo
;
2667 BOOL
*pfPropertiesChanged
;
2669 struct field_value_data
*fields
;
2672 typedef void (*add_fields_func
)(HWND hwnd
, struct detail_data
*data
);
2674 typedef WCHAR
*(*create_detailed_value_func
)(PCCERT_CONTEXT cert
, void *param
);
2676 struct field_value_data
2678 create_detailed_value_func create
;
2679 LPWSTR detailed_value
;
2683 static void add_field_value_data(struct detail_data
*data
,
2684 create_detailed_value_func create
, void *param
)
2687 data
->fields
= HeapReAlloc(GetProcessHeap(), 0, data
->fields
,
2688 (data
->cFields
+ 1) * sizeof(struct field_value_data
));
2690 data
->fields
= HeapAlloc(GetProcessHeap(), 0,
2691 sizeof(struct field_value_data
));
2694 data
->fields
[data
->cFields
].create
= create
;
2695 data
->fields
[data
->cFields
].detailed_value
= NULL
;
2696 data
->fields
[data
->cFields
].param
= param
;
2701 static void add_field_and_value_to_list(HWND hwnd
, struct detail_data
*data
,
2702 LPWSTR field
, LPWSTR value
, create_detailed_value_func create
, void *param
)
2705 int iItem
= SendMessageW(hwnd
, LVM_GETITEMCOUNT
, 0, 0);
2707 item
.mask
= LVIF_TEXT
| LVIF_PARAM
;
2710 item
.pszText
= field
;
2711 item
.lParam
= (LPARAM
)data
;
2712 SendMessageW(hwnd
, LVM_INSERTITEMW
, 0, (LPARAM
)&item
);
2715 item
.pszText
= value
;
2717 SendMessageW(hwnd
, LVM_SETITEMTEXTW
, iItem
, (LPARAM
)&item
);
2719 add_field_value_data(data
, create
, param
);
2722 static void add_string_id_and_value_to_list(HWND hwnd
, struct detail_data
*data
,
2723 int id
, LPWSTR value
, create_detailed_value_func create
, void *param
)
2725 WCHAR buf
[MAX_STRING_LEN
];
2727 LoadStringW(hInstance
, id
, buf
, sizeof(buf
) / sizeof(buf
[0]));
2728 add_field_and_value_to_list(hwnd
, data
, buf
, value
, create
, param
);
2734 field_format_func format
;
2735 create_detailed_value_func create_detailed_value
;
2738 static void add_v1_field(HWND hwnd
, struct detail_data
*data
,
2739 const struct v1_field
*field
)
2741 WCHAR
*val
= field
->format(data
->pCertViewInfo
->pCertContext
);
2745 add_string_id_and_value_to_list(hwnd
, data
, field
->id
, val
,
2746 field
->create_detailed_value
, NULL
);
2747 HeapFree(GetProcessHeap(), 0, val
);
2751 static const struct v1_field v1_fields
[] = {
2752 { IDS_FIELD_VERSION
, field_format_version
, NULL
},
2753 { IDS_FIELD_SERIAL_NUMBER
, field_format_serial_number
, NULL
},
2754 { IDS_FIELD_ISSUER
, field_format_issuer
, field_format_detailed_issuer
},
2755 { IDS_FIELD_VALID_FROM
, field_format_from_date
, NULL
},
2756 { IDS_FIELD_VALID_TO
, field_format_to_date
, NULL
},
2757 { IDS_FIELD_SUBJECT
, field_format_subject
, field_format_detailed_subject
},
2758 { IDS_FIELD_PUBLIC_KEY
, field_format_public_key
,
2759 field_format_detailed_public_key
}
2762 static void add_v1_fields(HWND hwnd
, struct detail_data
*data
)
2765 PCCERT_CONTEXT cert
= data
->pCertViewInfo
->pCertContext
;
2767 /* The last item in v1_fields is the public key, which is not in the loop
2768 * because it's a special case.
2770 for (i
= 0; i
< sizeof(v1_fields
) / sizeof(v1_fields
[0]) - 1; i
++)
2771 add_v1_field(hwnd
, data
, &v1_fields
[i
]);
2772 if (cert
->pCertInfo
->SubjectPublicKeyInfo
.PublicKey
.cbData
)
2773 add_v1_field(hwnd
, data
, &v1_fields
[i
]);
2776 static WCHAR
*crypt_format_extension(PCERT_EXTENSION ext
, DWORD formatStrType
)
2781 if (CryptFormatObject(X509_ASN_ENCODING
, 0, formatStrType
, NULL
,
2782 ext
->pszObjId
, ext
->Value
.pbData
, ext
->Value
.cbData
, NULL
, &size
))
2784 str
= HeapAlloc(GetProcessHeap(), 0, size
);
2785 CryptFormatObject(X509_ASN_ENCODING
, 0, formatStrType
, NULL
,
2786 ext
->pszObjId
, ext
->Value
.pbData
, ext
->Value
.cbData
, str
, &size
);
2791 static WCHAR
*field_format_extension_hex_with_ascii(PCERT_EXTENSION ext
)
2795 if (ext
->Value
.cbData
)
2797 /* The output is formatted as:
2798 * <hex bytes> <ascii bytes>\n
2799 * where <hex bytes> is a string of up to 8 bytes, output as %02x,
2800 * and <ascii bytes> is the ASCII equivalent of each byte, or '.' if
2801 * the byte is not printable.
2802 * So, for example, the extension value consisting of the following
2804 * 0x30,0x14,0x31,0x12,0x30,0x10,0x06,0x03,0x55,0x04,0x03,
2805 * 0x13,0x09,0x4a,0x75,0x61,0x6e,0x20,0x4c,0x61,0x6e,0x67
2807 * 30 14 31 12 30 10 06 03 0.1.0...
2808 * 55 04 03 13 09 4a 75 61 U....Jua
2809 * 6e 20 4c 61 6e 67 n Lang
2810 * The allocation size therefore requires:
2811 * - 4 characters per character in an 8-byte line
2812 * (2 for the hex format, one for the space, one for the ASCII value)
2813 * - 3 more characters per 8-byte line (two spaces and a newline)
2814 * - 1 character for the terminating nul
2815 * FIXME: should use a fixed-width font for this
2817 DWORD lines
= (ext
->Value
.cbData
+ 7) / 8;
2819 str
= HeapAlloc(GetProcessHeap(), 0,
2820 (lines
* 8 * 4 + lines
* 3 + 1) * sizeof(WCHAR
));
2823 static const WCHAR fmt
[] = { '%','0','2','x',' ',0 };
2827 for (i
= 0, ptr
= str
; i
< ext
->Value
.cbData
; i
+= 8)
2829 /* Output as hex bytes first */
2830 for (j
= i
; j
< min(i
+ 8, ext
->Value
.cbData
); j
++, ptr
+= 3)
2831 sprintfW(ptr
, fmt
, ext
->Value
.pbData
[j
]);
2832 /* Pad the hex output with spaces for alignment */
2833 if (j
== ext
->Value
.cbData
&& j
% 8)
2835 static const WCHAR pad
[] = { ' ',' ',' ' };
2837 for (; j
% 8; j
++, ptr
+= sizeof(pad
) / sizeof(pad
[0]))
2838 memcpy(ptr
, pad
, sizeof(pad
));
2840 /* The last sprintfW included a space, so just insert one
2841 * more space between the hex bytes and the ASCII output
2844 /* Output as ASCII bytes */
2845 for (j
= i
; j
< min(i
+ 8, ext
->Value
.cbData
); j
++, ptr
++)
2847 if (isprintW(ext
->Value
.pbData
[j
]) &&
2848 !isspaceW(ext
->Value
.pbData
[j
]))
2849 *ptr
= ext
->Value
.pbData
[j
];
2861 static WCHAR
*field_format_detailed_extension(PCCERT_CONTEXT cert
, void *param
)
2863 PCERT_EXTENSION ext
= param
;
2864 LPWSTR str
= crypt_format_extension(ext
,
2865 CRYPT_FORMAT_STR_MULTI_LINE
| CRYPT_FORMAT_STR_NO_HEX
);
2868 str
= field_format_extension_hex_with_ascii(ext
);
2872 static void add_cert_extension_detail(HWND hwnd
, struct detail_data
*data
,
2873 PCERT_EXTENSION ext
)
2875 PCCRYPT_OID_INFO oidInfo
= CryptFindOIDInfo(CRYPT_OID_INFO_OID_KEY
,
2877 LPWSTR val
= crypt_format_extension(ext
, 0);
2880 add_field_and_value_to_list(hwnd
, data
, (LPWSTR
)oidInfo
->pwszName
,
2881 val
, field_format_detailed_extension
, ext
);
2884 DWORD len
= strlen(ext
->pszObjId
);
2885 LPWSTR oidW
= HeapAlloc(GetProcessHeap(), 0, (len
+ 1) * sizeof(WCHAR
));
2891 for (i
= 0; i
<= len
; i
++)
2892 oidW
[i
] = ext
->pszObjId
[i
];
2893 add_field_and_value_to_list(hwnd
, data
, oidW
, val
,
2894 field_format_detailed_extension
, ext
);
2895 HeapFree(GetProcessHeap(), 0, oidW
);
2898 HeapFree(GetProcessHeap(), 0, val
);
2901 static void add_all_extensions(HWND hwnd
, struct detail_data
*data
)
2904 PCCERT_CONTEXT cert
= data
->pCertViewInfo
->pCertContext
;
2906 for (i
= 0; i
< cert
->pCertInfo
->cExtension
; i
++)
2907 add_cert_extension_detail(hwnd
, data
, &cert
->pCertInfo
->rgExtension
[i
]);
2910 static void add_critical_extensions(HWND hwnd
, struct detail_data
*data
)
2913 PCCERT_CONTEXT cert
= data
->pCertViewInfo
->pCertContext
;
2915 for (i
= 0; i
< cert
->pCertInfo
->cExtension
; i
++)
2916 if (cert
->pCertInfo
->rgExtension
[i
].fCritical
)
2917 add_cert_extension_detail(hwnd
, data
,
2918 &cert
->pCertInfo
->rgExtension
[i
]);
2921 typedef WCHAR
* (*prop_to_value_func
)(void *pb
, DWORD cb
);
2923 struct prop_id_to_string_id
2927 BOOL prop_is_string
;
2928 prop_to_value_func prop_to_value
;
2931 static WCHAR
*format_enhanced_key_usage_value(void *pb
, DWORD cb
)
2935 ext
.pszObjId
= (LPSTR
)X509_ENHANCED_KEY_USAGE
;
2936 ext
.fCritical
= FALSE
;
2937 ext
.Value
.pbData
= pb
;
2938 ext
.Value
.cbData
= cb
;
2939 return crypt_format_extension(&ext
, 0);
2942 /* Logically the access state should also be checked, and IDC_EDITPROPERTIES
2943 * disabled for read-only certificates, but native doesn't appear to do that.
2945 static const struct prop_id_to_string_id prop_id_map
[] = {
2946 { CERT_HASH_PROP_ID
, IDS_PROP_HASH
, FALSE
, format_hex_string
},
2947 { CERT_FRIENDLY_NAME_PROP_ID
, IDS_PROP_FRIENDLY_NAME
, TRUE
, NULL
},
2948 { CERT_DESCRIPTION_PROP_ID
, IDS_PROP_DESCRIPTION
, TRUE
, NULL
},
2949 { CERT_ENHKEY_USAGE_PROP_ID
, IDS_PROP_ENHKEY_USAGE
, FALSE
,
2950 format_enhanced_key_usage_value
},
2953 static void add_properties(HWND hwnd
, struct detail_data
*data
)
2956 PCCERT_CONTEXT cert
= data
->pCertViewInfo
->pCertContext
;
2958 for (i
= 0; i
< sizeof(prop_id_map
) / sizeof(prop_id_map
[0]); i
++)
2962 if (CertGetCertificateContextProperty(cert
, prop_id_map
[i
].prop
, NULL
,
2968 /* FIXME: MS adds a separate value for the signature hash
2971 pb
= HeapAlloc(GetProcessHeap(), 0, cb
);
2974 if (CertGetCertificateContextProperty(cert
,
2975 prop_id_map
[i
].prop
, pb
, &cb
))
2977 if (prop_id_map
[i
].prop_is_string
)
2980 /* Don't double-free pb */
2984 val
= prop_id_map
[i
].prop_to_value(pb
, cb
);
2986 HeapFree(GetProcessHeap(), 0, pb
);
2988 add_string_id_and_value_to_list(hwnd
, data
, prop_id_map
[i
].id
, val
,
2994 static void add_all_fields(HWND hwnd
, struct detail_data
*data
)
2996 add_v1_fields(hwnd
, data
);
2997 add_all_extensions(hwnd
, data
);
2998 add_properties(hwnd
, data
);
3001 struct selection_list_item
3004 add_fields_func add
;
3007 const struct selection_list_item listItems
[] = {
3008 { IDS_FIELDS_ALL
, add_all_fields
},
3009 { IDS_FIELDS_V1
, add_v1_fields
},
3010 { IDS_FIELDS_EXTENSIONS
, add_all_extensions
},
3011 { IDS_FIELDS_CRITICAL_EXTENSIONS
, add_critical_extensions
},
3012 { IDS_FIELDS_PROPERTIES
, add_properties
},
3015 static void create_show_list(HWND hwnd
, struct detail_data
*data
)
3017 HWND cb
= GetDlgItem(hwnd
, IDC_DETAIL_SELECT
);
3018 WCHAR buf
[MAX_STRING_LEN
];
3021 for (i
= 0; i
< sizeof(listItems
) / sizeof(listItems
[0]); i
++)
3025 LoadStringW(hInstance
, listItems
[i
].id
, buf
,
3026 sizeof(buf
) / sizeof(buf
[0]));
3027 index
= SendMessageW(cb
, CB_INSERTSTRING
, -1, (LPARAM
)buf
);
3028 SendMessageW(cb
, CB_SETITEMDATA
, index
, (LPARAM
)data
);
3030 SendMessageW(cb
, CB_SETCURSEL
, 0, 0);
3033 static void create_listview_columns(HWND hwnd
)
3035 HWND lv
= GetDlgItem(hwnd
, IDC_DETAIL_LIST
);
3037 WCHAR buf
[MAX_STRING_LEN
];
3040 SendMessageW(lv
, LVM_SETEXTENDEDLISTVIEWSTYLE
, 0, LVS_EX_FULLROWSELECT
);
3041 GetWindowRect(lv
, &rc
);
3042 LoadStringW(hInstance
, IDS_FIELD
, buf
, sizeof(buf
) / sizeof(buf
[0]));
3043 column
.mask
= LVCF_WIDTH
| LVCF_TEXT
;
3044 column
.cx
= (rc
.right
- rc
.left
) / 2 - 2;
3045 column
.pszText
= buf
;
3046 SendMessageW(lv
, LVM_INSERTCOLUMNW
, 0, (LPARAM
)&column
);
3047 LoadStringW(hInstance
, IDS_VALUE
, buf
, sizeof(buf
) / sizeof(buf
[0]));
3048 SendMessageW(lv
, LVM_INSERTCOLUMNW
, 1, (LPARAM
)&column
);
3051 static void set_fields_selection(HWND hwnd
, struct detail_data
*data
, int sel
)
3053 HWND list
= GetDlgItem(hwnd
, IDC_DETAIL_LIST
);
3055 if (sel
>= 0 && sel
< sizeof(listItems
) / sizeof(listItems
[0]))
3057 SendMessageW(list
, LVM_DELETEALLITEMS
, 0, 0);
3058 listItems
[sel
].add(list
, data
);
3062 static void create_cert_details_list(HWND hwnd
, struct detail_data
*data
)
3064 create_show_list(hwnd
, data
);
3065 create_listview_columns(hwnd
);
3066 set_fields_selection(hwnd
, data
, 0);
3069 static void add_purpose(HWND hwnd
, LPCSTR oid
)
3071 HWND lv
= GetDlgItem(hwnd
, IDC_CERTIFICATE_USAGES
);
3072 PCRYPT_OID_INFO info
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
,
3073 sizeof(CRYPT_OID_INFO
));
3077 char *oidCopy
= HeapAlloc(GetProcessHeap(), 0, strlen(oid
) + 1);
3083 strcpy(oidCopy
, oid
);
3084 info
->cbSize
= sizeof(CRYPT_OID_INFO
);
3085 info
->pszOID
= oidCopy
;
3086 item
.mask
= LVIF_TEXT
| LVIF_STATE
| LVIF_PARAM
;
3087 item
.state
= INDEXTOSTATEIMAGEMASK(CheckBitmapIndexChecked
);
3088 item
.stateMask
= LVIS_STATEIMAGEMASK
;
3089 item
.iItem
= SendMessageW(lv
, LVM_GETITEMCOUNT
, 0, 0);
3091 item
.lParam
= (LPARAM
)info
;
3092 item
.pszText
= oidCopy
;
3093 SendMessageA(lv
, LVM_INSERTITEMA
, 0, (LPARAM
)&item
);
3096 HeapFree(GetProcessHeap(), 0, info
);
3100 static BOOL
is_valid_oid(LPCSTR oid
)
3104 if (oid
[0] != '0' && oid
[0] != '1' && oid
[0] != '2')
3106 else if (oid
[1] != '.')
3113 BOOL expectNum
= TRUE
;
3115 for (ptr
= oid
+ 2, ret
= TRUE
; ret
&& *ptr
; ptr
++)
3121 else if (*(ptr
+ 1) == '.')
3128 else if (!(*(ptr
+ 1)))
3138 static BOOL
is_oid_in_list(HWND hwnd
, LPCSTR oid
)
3140 return find_oid_in_list(GetDlgItem(hwnd
, IDC_CERTIFICATE_USAGES
), oid
)
3144 #define MAX_PURPOSE 255
3146 static LRESULT CALLBACK
add_purpose_dlg_proc(HWND hwnd
, UINT msg
,
3147 WPARAM wp
, LPARAM lp
)
3150 char buf
[MAX_PURPOSE
+ 1];
3155 SendMessageW(GetDlgItem(hwnd
, IDC_NEW_PURPOSE
), EM_SETLIMITTEXT
,
3157 ShowScrollBar(GetDlgItem(hwnd
, IDC_NEW_PURPOSE
), SB_VERT
, FALSE
);
3158 SetWindowLongPtrW(hwnd
, DWLP_USER
, lp
);
3164 if (LOWORD(wp
) == IDC_NEW_PURPOSE
)
3166 /* Show/hide scroll bar on description depending on how much
3169 HWND description
= GetDlgItem(hwnd
, IDC_NEW_PURPOSE
);
3170 int lines
= SendMessageW(description
, EM_GETLINECOUNT
, 0, 0);
3172 ShowScrollBar(description
, SB_VERT
, lines
> 1);
3179 SendMessageA(GetDlgItem(hwnd
, IDC_NEW_PURPOSE
), WM_GETTEXT
,
3180 sizeof(buf
) / sizeof(buf
[0]), (LPARAM
)buf
);
3183 /* An empty purpose is the same as cancelling */
3184 EndDialog(hwnd
, IDCANCEL
);
3187 else if (!is_valid_oid(buf
))
3189 WCHAR title
[MAX_STRING_LEN
], error
[MAX_STRING_LEN
];
3191 LoadStringW(hInstance
, IDS_CERTIFICATE_PURPOSE_ERROR
, error
,
3192 sizeof(error
) / sizeof(error
[0]));
3193 LoadStringW(hInstance
, IDS_CERTIFICATE_PROPERTIES
, title
,
3194 sizeof(title
) / sizeof(title
[0]));
3195 MessageBoxW(hwnd
, error
, title
, MB_ICONERROR
| MB_OK
);
3197 else if (is_oid_in_list(
3198 (HWND
)GetWindowLongPtrW(hwnd
, DWLP_USER
), buf
))
3200 WCHAR title
[MAX_STRING_LEN
], error
[MAX_STRING_LEN
];
3202 LoadStringW(hInstance
, IDS_CERTIFICATE_PURPOSE_EXISTS
,
3203 error
, sizeof(error
) / sizeof(error
[0]));
3204 LoadStringW(hInstance
, IDS_CERTIFICATE_PROPERTIES
, title
,
3205 sizeof(title
) / sizeof(title
[0]));
3206 MessageBoxW(hwnd
, error
, title
, MB_ICONEXCLAMATION
| MB_OK
);
3210 HWND parent
= (HWND
)GetWindowLongPtrW(hwnd
, DWLP_USER
);
3212 add_purpose(parent
, buf
);
3213 EndDialog(hwnd
, wp
);
3218 EndDialog(hwnd
, wp
);
3229 static WCHAR
*get_cert_property_as_string(PCCERT_CONTEXT cert
, DWORD prop
)
3234 if (CertGetCertificateContextProperty(cert
, prop
, NULL
, &cb
))
3236 name
= HeapAlloc(GetProcessHeap(), 0, cb
);
3239 if (!CertGetCertificateContextProperty(cert
, prop
, (LPBYTE
)name
,
3242 HeapFree(GetProcessHeap(), 0, name
);
3250 static void redraw_states(HWND list
, BOOL enabled
)
3252 int items
= SendMessageW(list
, LVM_GETITEMCOUNT
, 0, 0), i
;
3254 for (i
= 0; i
< items
; i
++)
3256 BOOL change
= FALSE
;
3259 state
= SendMessageW(list
, LVM_GETITEMSTATE
, i
, LVIS_STATEIMAGEMASK
);
3260 /* This reverses the INDEXTOSTATEIMAGEMASK shift. There doesn't appear
3261 * to be a handy macro for it.
3266 if (state
== CheckBitmapIndexDisabledChecked
)
3268 state
= CheckBitmapIndexChecked
;
3271 if (state
== CheckBitmapIndexDisabledUnchecked
)
3273 state
= CheckBitmapIndexUnchecked
;
3279 if (state
== CheckBitmapIndexChecked
)
3281 state
= CheckBitmapIndexDisabledChecked
;
3284 if (state
== CheckBitmapIndexUnchecked
)
3286 state
= CheckBitmapIndexDisabledUnchecked
;
3294 item
.state
= INDEXTOSTATEIMAGEMASK(state
);
3295 item
.stateMask
= LVIS_STATEIMAGEMASK
;
3296 SendMessageW(list
, LVM_SETITEMSTATE
, i
, (LPARAM
)&item
);
3302 PurposeEnableAll
= 0,
3304 PurposeEnableSelected
3307 static void select_purposes(HWND hwnd
, PurposeSelection selection
)
3309 HWND lv
= GetDlgItem(hwnd
, IDC_CERTIFICATE_USAGES
);
3313 case PurposeEnableAll
:
3314 case PurposeDisableAll
:
3315 EnableWindow(lv
, FALSE
);
3316 redraw_states(lv
, FALSE
);
3317 EnableWindow(GetDlgItem(hwnd
, IDC_ADD_PURPOSE
), FALSE
);
3319 case PurposeEnableSelected
:
3320 EnableWindow(lv
, TRUE
);
3321 redraw_states(lv
, TRUE
);
3322 EnableWindow(GetDlgItem(hwnd
, IDC_ADD_PURPOSE
), TRUE
);
3326 struct edit_cert_data
3328 PCCERT_CONTEXT cert
;
3329 BOOL
*pfPropertiesChanged
;
3330 HIMAGELIST imageList
;
3333 static void show_cert_usages(HWND hwnd
, struct edit_cert_data
*data
)
3335 PCCERT_CONTEXT cert
= data
->cert
;
3336 HWND lv
= GetDlgItem(hwnd
, IDC_CERTIFICATE_USAGES
);
3337 PCERT_ENHKEY_USAGE usage
;
3341 PurposeSelection purposeSelection
;
3343 GetWindowRect(lv
, &rc
);
3344 column
.mask
= LVCF_WIDTH
;
3345 column
.cx
= rc
.right
- rc
.left
;
3346 SendMessageW(lv
, LVM_INSERTCOLUMNW
, 0, (LPARAM
)&column
);
3347 SendMessageW(lv
, LVM_SETIMAGELIST
, LVSIL_STATE
, (LPARAM
)data
->imageList
);
3349 /* Get enhanced key usage. Have to check for a property and an extension
3350 * separately, because CertGetEnhancedKeyUsage will succeed and return an
3351 * empty usage if neither is set. Unfortunately an empty usage implies
3352 * no usage is allowed, so we have to distinguish between the two cases.
3354 if (CertGetEnhancedKeyUsage(cert
, CERT_FIND_PROP_ONLY_ENHKEY_USAGE_FLAG
,
3357 usage
= HeapAlloc(GetProcessHeap(), 0, size
);
3358 if (!CertGetEnhancedKeyUsage(cert
,
3359 CERT_FIND_PROP_ONLY_ENHKEY_USAGE_FLAG
, usage
, &size
))
3361 HeapFree(GetProcessHeap(), 0, usage
);
3364 else if (usage
->cUsageIdentifier
)
3365 purposeSelection
= PurposeEnableSelected
;
3367 purposeSelection
= PurposeDisableAll
;
3369 else if (CertGetEnhancedKeyUsage(cert
, CERT_FIND_EXT_ONLY_ENHKEY_USAGE_FLAG
,
3372 usage
= HeapAlloc(GetProcessHeap(), 0, size
);
3373 if (!CertGetEnhancedKeyUsage(cert
,
3374 CERT_FIND_EXT_ONLY_ENHKEY_USAGE_FLAG
, usage
, &size
))
3376 HeapFree(GetProcessHeap(), 0, usage
);
3379 else if (usage
->cUsageIdentifier
)
3380 purposeSelection
= PurposeEnableAll
;
3382 purposeSelection
= PurposeDisableAll
;
3386 purposeSelection
= PurposeEnableAll
;
3393 for (i
= 0; i
< usage
->cUsageIdentifier
; i
++)
3395 PCCRYPT_OID_INFO info
= CryptFindOIDInfo(CRYPT_OID_INFO_OID_KEY
,
3396 usage
->rgpszUsageIdentifier
[i
], CRYPT_ENHKEY_USAGE_OID_GROUP_ID
);
3399 add_known_usage(lv
, info
, CheckBitmapIndexDisabledChecked
);
3401 add_purpose(hwnd
, usage
->rgpszUsageIdentifier
[i
]);
3403 HeapFree(GetProcessHeap(), 0, usage
);
3406 add_known_usages_to_list(lv
, CheckBitmapIndexDisabledChecked
);
3407 select_purposes(hwnd
, purposeSelection
);
3408 SendMessageW(GetDlgItem(hwnd
, IDC_ENABLE_ALL_PURPOSES
+ purposeSelection
),
3412 static void set_general_cert_properties(HWND hwnd
, struct edit_cert_data
*data
)
3414 PCCERT_CONTEXT cert
= data
->cert
;
3417 if ((str
= get_cert_property_as_string(cert
, CERT_FRIENDLY_NAME_PROP_ID
)))
3419 SendMessageW(GetDlgItem(hwnd
, IDC_FRIENDLY_NAME
), WM_SETTEXT
, 0,
3421 HeapFree(GetProcessHeap(), 0, str
);
3423 if ((str
= get_cert_property_as_string(cert
, CERT_DESCRIPTION_PROP_ID
)))
3425 SendMessageW(GetDlgItem(hwnd
, IDC_DESCRIPTION
), WM_SETTEXT
, 0,
3427 HeapFree(GetProcessHeap(), 0, str
);
3429 show_cert_usages(hwnd
, data
);
3432 static void set_cert_string_property(PCCERT_CONTEXT cert
, DWORD prop
,
3435 if (str
&& strlenW(str
))
3437 CRYPT_DATA_BLOB blob
;
3439 blob
.pbData
= (BYTE
*)str
;
3440 blob
.cbData
= (strlenW(str
) + 1) * sizeof(WCHAR
);
3441 CertSetCertificateContextProperty(cert
, prop
, 0, &blob
);
3444 CertSetCertificateContextProperty(cert
, prop
, 0, NULL
);
3447 #define WM_REFRESH_VIEW WM_USER + 0
3449 static BOOL CALLBACK
refresh_propsheet_pages(HWND hwnd
, LPARAM lParam
)
3451 if ((GetClassLongW(hwnd
, GCW_ATOM
) == WC_DIALOG
))
3452 SendMessageW(hwnd
, WM_REFRESH_VIEW
, 0, 0);
3456 #define MAX_FRIENDLY_NAME 40
3457 #define MAX_DESCRIPTION 255
3459 static void apply_general_changes(HWND hwnd
)
3461 WCHAR buf
[MAX_DESCRIPTION
+ 1];
3462 struct edit_cert_data
*data
=
3463 (struct edit_cert_data
*)GetWindowLongPtrW(hwnd
, DWLP_USER
);
3465 SendMessageW(GetDlgItem(hwnd
, IDC_FRIENDLY_NAME
), WM_GETTEXT
,
3466 sizeof(buf
) / sizeof(buf
[0]), (LPARAM
)buf
);
3467 set_cert_string_property(data
->cert
, CERT_FRIENDLY_NAME_PROP_ID
, buf
);
3468 SendMessageW(GetDlgItem(hwnd
, IDC_DESCRIPTION
), WM_GETTEXT
,
3469 sizeof(buf
) / sizeof(buf
[0]), (LPARAM
)buf
);
3470 set_cert_string_property(data
->cert
, CERT_DESCRIPTION_PROP_ID
, buf
);
3471 if (IsDlgButtonChecked(hwnd
, IDC_ENABLE_ALL_PURPOSES
))
3473 /* Setting a NULL usage removes the enhanced key usage property. */
3474 CertSetEnhancedKeyUsage(data
->cert
, NULL
);
3476 else if (IsDlgButtonChecked(hwnd
, IDC_DISABLE_ALL_PURPOSES
))
3478 CERT_ENHKEY_USAGE usage
= { 0, NULL
};
3480 CertSetEnhancedKeyUsage(data
->cert
, &usage
);
3482 else if (IsDlgButtonChecked(hwnd
, IDC_ENABLE_SELECTED_PURPOSES
))
3484 HWND lv
= GetDlgItem(hwnd
, IDC_CERTIFICATE_USAGES
);
3485 CERT_ENHKEY_USAGE usage
= { 0, NULL
};
3486 int purposes
= SendMessageW(lv
, LVM_GETITEMCOUNT
, 0, 0), i
;
3489 item
.mask
= LVIF_STATE
| LVIF_PARAM
;
3491 item
.stateMask
= LVIS_STATEIMAGEMASK
;
3492 for (i
= 0; i
< purposes
; i
++)
3495 if (SendMessageW(lv
, LVM_GETITEMW
, 0, (LPARAM
)&item
))
3497 int state
= item
.state
>> 12;
3499 if (state
== CheckBitmapIndexChecked
)
3501 CRYPT_OID_INFO
*info
= (CRYPT_OID_INFO
*)item
.lParam
;
3503 if (usage
.cUsageIdentifier
)
3504 usage
.rgpszUsageIdentifier
=
3505 HeapReAlloc(GetProcessHeap(), 0,
3506 usage
.rgpszUsageIdentifier
,
3507 (usage
.cUsageIdentifier
+ 1) * sizeof(LPSTR
));
3509 usage
.rgpszUsageIdentifier
=
3510 HeapAlloc(GetProcessHeap(), 0, sizeof(LPSTR
));
3511 if (usage
.rgpszUsageIdentifier
)
3512 usage
.rgpszUsageIdentifier
[usage
.cUsageIdentifier
++] =
3513 (LPSTR
)info
->pszOID
;
3517 CertSetEnhancedKeyUsage(data
->cert
, &usage
);
3518 HeapFree(GetProcessHeap(), 0, usage
.rgpszUsageIdentifier
);
3520 EnumChildWindows(GetParent(GetParent(hwnd
)), refresh_propsheet_pages
, 0);
3521 if (data
->pfPropertiesChanged
)
3522 *data
->pfPropertiesChanged
= TRUE
;
3525 static LRESULT CALLBACK
cert_properties_general_dlg_proc(HWND hwnd
, UINT msg
,
3526 WPARAM wp
, LPARAM lp
)
3528 PROPSHEETPAGEW
*page
;
3530 TRACE("(%p, %08x, %08lx, %08lx)\n", hwnd
, msg
, wp
, lp
);
3536 HWND description
= GetDlgItem(hwnd
, IDC_DESCRIPTION
);
3537 struct detail_data
*detailData
;
3538 struct edit_cert_data
*editData
;
3540 page
= (PROPSHEETPAGEW
*)lp
;
3541 detailData
= (struct detail_data
*)page
->lParam
;
3542 SendMessageW(GetDlgItem(hwnd
, IDC_FRIENDLY_NAME
), EM_SETLIMITTEXT
,
3543 MAX_FRIENDLY_NAME
, 0);
3544 SendMessageW(description
, EM_SETLIMITTEXT
, MAX_DESCRIPTION
, 0);
3545 ShowScrollBar(description
, SB_VERT
, FALSE
);
3546 editData
= HeapAlloc(GetProcessHeap(), 0,
3547 sizeof(struct edit_cert_data
));
3550 editData
->imageList
= ImageList_Create(16, 16,
3551 ILC_COLOR4
| ILC_MASK
, 4, 0);
3552 if (editData
->imageList
)
3555 COLORREF backColor
= RGB(255, 0, 255);
3557 bmp
= LoadBitmapW(hInstance
, MAKEINTRESOURCEW(IDB_CHECKS
));
3558 ImageList_AddMasked(editData
->imageList
, bmp
, backColor
);
3560 ImageList_SetBkColor(editData
->imageList
, CLR_NONE
);
3562 editData
->cert
= detailData
->pCertViewInfo
->pCertContext
;
3563 editData
->pfPropertiesChanged
= detailData
->pfPropertiesChanged
;
3564 SetWindowLongPtrW(hwnd
, DWLP_USER
, (LPARAM
)editData
);
3565 set_general_cert_properties(hwnd
, editData
);
3571 NMHDR
*hdr
= (NMHDR
*)lp
;
3577 nm
= (NMITEMACTIVATE
*)lp
;
3578 toggle_usage(hwnd
, nm
->iItem
);
3579 SendMessageW(GetParent(hwnd
), PSM_CHANGED
, (WPARAM
)hwnd
, 0);
3582 apply_general_changes(hwnd
);
3591 SendMessageW(GetParent(hwnd
), PSM_CHANGED
, (WPARAM
)hwnd
, 0);
3592 if (LOWORD(wp
) == IDC_DESCRIPTION
)
3594 /* Show/hide scroll bar on description depending on how much
3597 HWND description
= GetDlgItem(hwnd
, IDC_DESCRIPTION
);
3598 int lines
= SendMessageW(description
, EM_GETLINECOUNT
, 0, 0);
3600 ShowScrollBar(description
, SB_VERT
, lines
> 1);
3606 case IDC_ADD_PURPOSE
:
3607 if (DialogBoxParamW(hInstance
,
3608 MAKEINTRESOURCEW(IDD_ADD_CERT_PURPOSE
), hwnd
,
3609 add_purpose_dlg_proc
, (LPARAM
)hwnd
) == IDOK
)
3610 SendMessageW(GetParent(hwnd
), PSM_CHANGED
, (WPARAM
)hwnd
, 0);
3612 case IDC_ENABLE_ALL_PURPOSES
:
3613 case IDC_DISABLE_ALL_PURPOSES
:
3614 case IDC_ENABLE_SELECTED_PURPOSES
:
3615 SendMessageW(GetParent(hwnd
), PSM_CHANGED
, (WPARAM
)hwnd
, 0);
3616 select_purposes(hwnd
, LOWORD(wp
) - IDC_ENABLE_ALL_PURPOSES
);
3626 static UINT CALLBACK
cert_properties_general_callback(HWND hwnd
, UINT msg
,
3627 PROPSHEETPAGEW
*page
)
3631 struct edit_cert_data
*data
;
3636 lv
= GetDlgItem(hwnd
, IDC_CERTIFICATE_USAGES
);
3637 cItem
= SendMessageW(lv
, LVM_GETITEMCOUNT
, 0, 0);
3638 for (i
= 0; i
< cItem
; i
++)
3642 item
.mask
= LVIF_PARAM
;
3645 if (SendMessageW(lv
, LVM_GETITEMW
, 0, (LPARAM
)&item
) && item
.lParam
)
3647 PCRYPT_OID_INFO info
= (PCRYPT_OID_INFO
)item
.lParam
;
3649 if (info
->cbSize
== sizeof(CRYPT_OID_INFO
) && !info
->dwGroupId
)
3651 HeapFree(GetProcessHeap(), 0, (LPSTR
)info
->pszOID
);
3652 HeapFree(GetProcessHeap(), 0, info
);
3656 data
= (struct edit_cert_data
*)GetWindowLongPtrW(hwnd
, DWLP_USER
);
3659 ImageList_Destroy(data
->imageList
);
3660 HeapFree(GetProcessHeap(), 0, data
);
3667 static void show_edit_cert_properties_dialog(HWND parent
,
3668 struct detail_data
*data
)
3670 PROPSHEETHEADERW hdr
;
3671 PROPSHEETPAGEW page
; /* FIXME: need to add a cross-certificate page */
3673 TRACE("(%p)\n", data
);
3675 memset(&page
, 0, sizeof(PROPSHEETPAGEW
));
3676 page
.dwSize
= sizeof(page
);
3677 page
.dwFlags
= PSP_USECALLBACK
;
3678 page
.pfnCallback
= cert_properties_general_callback
;
3679 page
.hInstance
= hInstance
;
3680 page
.u
.pszTemplate
= MAKEINTRESOURCEW(IDD_CERT_PROPERTIES_GENERAL
);
3681 page
.pfnDlgProc
= cert_properties_general_dlg_proc
;
3682 page
.lParam
= (LPARAM
)data
;
3684 memset(&hdr
, 0, sizeof(hdr
));
3685 hdr
.dwSize
= sizeof(hdr
);
3686 hdr
.hwndParent
= parent
;
3687 hdr
.dwFlags
= PSH_PROPSHEETPAGE
;
3688 hdr
.hInstance
= hInstance
;
3689 hdr
.pszCaption
= MAKEINTRESOURCEW(IDS_CERTIFICATE_PROPERTIES
);
3690 hdr
.u3
.ppsp
= &page
;
3692 PropertySheetW(&hdr
);
3695 static void free_detail_fields(struct detail_data
*data
)
3699 for (i
= 0; i
< data
->cFields
; i
++)
3700 HeapFree(GetProcessHeap(), 0, data
->fields
[i
].detailed_value
);
3701 HeapFree(GetProcessHeap(), 0, data
->fields
);
3702 data
->fields
= NULL
;
3706 static void refresh_details_view(HWND hwnd
)
3708 HWND cb
= GetDlgItem(hwnd
, IDC_DETAIL_SELECT
);
3710 struct detail_data
*data
;
3712 curSel
= SendMessageW(cb
, CB_GETCURSEL
, 0, 0);
3713 /* Actually, any index will do, since they all store the same data value */
3714 data
= (struct detail_data
*)SendMessageW(cb
, CB_GETITEMDATA
, curSel
, 0);
3715 free_detail_fields(data
);
3716 set_fields_selection(hwnd
, data
, curSel
);
3719 static LRESULT CALLBACK
detail_dlg_proc(HWND hwnd
, UINT msg
, WPARAM wp
,
3722 PROPSHEETPAGEW
*page
;
3723 struct detail_data
*data
;
3725 TRACE("(%p, %08x, %08lx, %08lx)\n", hwnd
, msg
, wp
, lp
);
3730 page
= (PROPSHEETPAGEW
*)lp
;
3731 data
= (struct detail_data
*)page
->lParam
;
3732 create_cert_details_list(hwnd
, data
);
3733 if (!(data
->pCertViewInfo
->dwFlags
& CRYPTUI_ENABLE_EDITPROPERTIES
))
3734 EnableWindow(GetDlgItem(hwnd
, IDC_EDITPROPERTIES
), FALSE
);
3735 if (data
->pCertViewInfo
->dwFlags
& CRYPTUI_DISABLE_EXPORT
)
3736 EnableWindow(GetDlgItem(hwnd
, IDC_EXPORT
), FALSE
);
3741 HWND list
= GetDlgItem(hwnd
, IDC_DETAIL_LIST
);
3743 nm
= (NMITEMACTIVATE
*)lp
;
3744 if (nm
->hdr
.hwndFrom
== list
&& nm
->uNewState
& LVN_ITEMACTIVATE
3745 && nm
->hdr
.code
== LVN_ITEMCHANGED
)
3747 data
= (struct detail_data
*)nm
->lParam
;
3748 if (nm
->iItem
>= 0 && data
&& nm
->iItem
< data
->cFields
)
3750 WCHAR buf
[MAX_STRING_LEN
], *val
= NULL
;
3751 HWND valueCtl
= GetDlgItem(hwnd
, IDC_DETAIL_VALUE
);
3753 if (data
->fields
[nm
->iItem
].create
)
3754 val
= data
->fields
[nm
->iItem
].create(
3755 data
->pCertViewInfo
->pCertContext
,
3756 data
->fields
[nm
->iItem
].param
);
3762 item
.cchTextMax
= sizeof(buf
) / sizeof(buf
[0]);
3763 item
.mask
= LVIF_TEXT
;
3765 item
.iItem
= nm
->iItem
;
3767 res
= SendMessageW(list
, LVM_GETITEMW
, 0, (LPARAM
)&item
);
3771 /* Select all the text in the control, the next update will
3774 SendMessageW(valueCtl
, EM_SETSEL
, 0, -1);
3775 add_unformatted_text_to_control(valueCtl
, val
,
3776 val
? strlenW(val
) : 0);
3778 HeapFree(GetProcessHeap(), 0, val
);
3788 HWND cb
= GetDlgItem(hwnd
, IDC_DETAIL_SELECT
);
3789 CRYPTUI_WIZ_EXPORT_INFO info
;
3791 data
= (struct detail_data
*)SendMessageW(cb
, CB_GETITEMDATA
, 0, 0);
3792 info
.dwSize
= sizeof(info
);
3793 info
.pwszExportFileName
= NULL
;
3794 info
.dwSubjectChoice
= CRYPTUI_WIZ_EXPORT_CERT_CONTEXT
;
3795 info
.u
.pCertContext
= data
->pCertViewInfo
->pCertContext
;
3797 CryptUIWizExport(0, hwnd
, NULL
, &info
, NULL
);
3800 case IDC_EDITPROPERTIES
:
3802 HWND cb
= GetDlgItem(hwnd
, IDC_DETAIL_SELECT
);
3805 curSel
= SendMessageW(cb
, CB_GETCURSEL
, 0, 0);
3806 /* Actually, any index will do, since they all store the same
3809 data
= (struct detail_data
*)SendMessageW(cb
, CB_GETITEMDATA
,
3811 show_edit_cert_properties_dialog(GetParent(hwnd
), data
);
3814 case ((CBN_SELCHANGE
<< 16) | IDC_DETAIL_SELECT
):
3815 refresh_details_view(hwnd
);
3819 case WM_REFRESH_VIEW
:
3820 refresh_details_view(hwnd
);
3826 static UINT CALLBACK
detail_callback(HWND hwnd
, UINT msg
,
3827 PROPSHEETPAGEW
*page
)
3829 struct detail_data
*data
;
3834 data
= (struct detail_data
*)page
->lParam
;
3835 free_detail_fields(data
);
3836 HeapFree(GetProcessHeap(), 0, data
);
3842 static BOOL
init_detail_page(PCCRYPTUI_VIEWCERTIFICATE_STRUCTW pCertViewInfo
,
3843 BOOL
*pfPropertiesChanged
, PROPSHEETPAGEW
*page
)
3846 struct detail_data
*data
= HeapAlloc(GetProcessHeap(), 0,
3847 sizeof(struct detail_data
));
3851 data
->pCertViewInfo
= pCertViewInfo
;
3852 data
->pfPropertiesChanged
= pfPropertiesChanged
;
3854 data
->fields
= NULL
;
3855 memset(page
, 0, sizeof(PROPSHEETPAGEW
));
3856 page
->dwSize
= sizeof(PROPSHEETPAGEW
);
3857 page
->dwFlags
= PSP_USECALLBACK
;
3858 page
->pfnCallback
= detail_callback
;
3859 page
->hInstance
= hInstance
;
3860 page
->u
.pszTemplate
= MAKEINTRESOURCEW(IDD_DETAIL
);
3861 page
->pfnDlgProc
= detail_dlg_proc
;
3862 page
->lParam
= (LPARAM
)data
;
3870 struct hierarchy_data
3872 PCCRYPTUI_VIEWCERTIFICATE_STRUCTW pCertViewInfo
;
3873 HIMAGELIST imageList
;
3877 static LPARAM
index_to_lparam(struct hierarchy_data
*data
, DWORD index
)
3879 CRYPT_PROVIDER_SGNR
*provSigner
= WTHelperGetProvSignerFromChain(
3880 (CRYPT_PROVIDER_DATA
*)data
->pCertViewInfo
->u
.pCryptProviderData
,
3881 data
->pCertViewInfo
->idxSigner
, data
->pCertViewInfo
->fCounterSigner
,
3882 data
->pCertViewInfo
->idxCounterSigner
);
3884 /* Takes advantage of the fact that a pointer is 32-bit aligned, and
3885 * therefore always even.
3887 if (index
== provSigner
->csCertChain
- 1)
3888 return (LPARAM
)data
;
3889 return index
<< 1 | 1;
3892 static inline DWORD
lparam_to_index(struct hierarchy_data
*data
, LPARAM lp
)
3894 CRYPT_PROVIDER_SGNR
*provSigner
= WTHelperGetProvSignerFromChain(
3895 (CRYPT_PROVIDER_DATA
*)data
->pCertViewInfo
->u
.pCryptProviderData
,
3896 data
->pCertViewInfo
->idxSigner
, data
->pCertViewInfo
->fCounterSigner
,
3897 data
->pCertViewInfo
->idxCounterSigner
);
3900 return provSigner
->csCertChain
- 1;
3904 static struct hierarchy_data
*get_hierarchy_data_from_tree_item(HWND tree
,
3907 struct hierarchy_data
*data
= NULL
;
3908 HTREEITEM root
= NULL
;
3911 HTREEITEM parent
= (HTREEITEM
)SendMessageW(tree
, TVM_GETNEXTITEM
,
3912 TVGN_PARENT
, (LPARAM
)hItem
);
3922 item
.mask
= TVIF_PARAM
;
3924 SendMessageW(tree
, TVM_GETITEMW
, 0, (LPARAM
)&item
);
3925 data
= (struct hierarchy_data
*)item
.lParam
;
3930 static WCHAR
*get_cert_display_name(PCCERT_CONTEXT cert
)
3932 WCHAR
*name
= get_cert_property_as_string(cert
, CERT_FRIENDLY_NAME_PROP_ID
);
3935 name
= get_cert_name_string(cert
, CERT_NAME_SIMPLE_DISPLAY_TYPE
, 0);
3939 static void show_cert_chain(HWND hwnd
, struct hierarchy_data
*data
)
3941 HWND tree
= GetDlgItem(hwnd
, IDC_CERTPATH
);
3942 CRYPT_PROVIDER_SGNR
*provSigner
= WTHelperGetProvSignerFromChain(
3943 (CRYPT_PROVIDER_DATA
*)data
->pCertViewInfo
->u
.pCryptProviderData
,
3944 data
->pCertViewInfo
->idxSigner
, data
->pCertViewInfo
->fCounterSigner
,
3945 data
->pCertViewInfo
->idxCounterSigner
);
3947 HTREEITEM parent
= NULL
;
3949 SendMessageW(tree
, TVM_SETIMAGELIST
, TVSIL_NORMAL
, (LPARAM
)data
->imageList
);
3950 for (i
= provSigner
->csCertChain
; i
; i
--)
3954 name
= get_cert_display_name(provSigner
->pasCertChain
[i
- 1].pCert
);
3957 TVINSERTSTRUCTW tvis
;
3959 tvis
.hParent
= parent
;
3960 tvis
.hInsertAfter
= TVI_LAST
;
3961 tvis
.u
.item
.mask
= TVIF_TEXT
| TVIF_STATE
| TVIF_IMAGE
|
3962 TVIF_SELECTEDIMAGE
| TVIF_PARAM
;
3963 tvis
.u
.item
.pszText
= name
;
3964 tvis
.u
.item
.state
= TVIS_EXPANDED
;
3965 tvis
.u
.item
.stateMask
= TVIS_EXPANDED
;
3967 (provSigner
->pChainContext
->TrustStatus
.dwErrorStatus
&
3968 CERT_TRUST_IS_PARTIAL_CHAIN
))
3970 /* The root of the chain has a special case: if the chain is
3971 * a partial chain, the icon is a warning icon rather than an
3974 tvis
.u
.item
.iImage
= 2;
3976 else if (provSigner
->pasCertChain
[i
- 1].pChainElement
->TrustStatus
.
3978 tvis
.u
.item
.iImage
= 0;
3980 tvis
.u
.item
.iImage
= 1;
3981 tvis
.u
.item
.iSelectedImage
= tvis
.u
.item
.iImage
;
3982 tvis
.u
.item
.lParam
= index_to_lparam(data
, i
- 1);
3983 parent
= (HTREEITEM
)SendMessageW(tree
, TVM_INSERTITEMW
, 0,
3985 HeapFree(GetProcessHeap(), 0, name
);
3990 static void set_certificate_status(HWND hwnd
, CRYPT_PROVIDER_CERT
*cert
)
3992 /* Select all the text in the control, the next update will replace it */
3993 SendMessageW(hwnd
, EM_SETSEL
, 0, -1);
3994 /* Set the highest priority error messages first. */
3995 if (!(cert
->dwConfidence
& CERT_CONFIDENCE_SIG
))
3996 add_string_resource_to_control(hwnd
, IDS_CERTIFICATE_BAD_SIGNATURE
);
3997 else if (!(cert
->dwConfidence
& CERT_CONFIDENCE_TIME
))
3998 add_string_resource_to_control(hwnd
, IDS_CERTIFICATE_BAD_TIME
);
3999 else if (!(cert
->dwConfidence
& CERT_CONFIDENCE_TIMENEST
))
4000 add_string_resource_to_control(hwnd
, IDS_CERTIFICATE_BAD_TIMENEST
);
4001 else if (cert
->dwRevokedReason
)
4002 add_string_resource_to_control(hwnd
, IDS_CERTIFICATE_REVOKED
);
4004 add_string_resource_to_control(hwnd
, IDS_CERTIFICATE_VALID
);
4007 static void set_certificate_status_for_end_cert(HWND hwnd
,
4008 PCCRYPTUI_VIEWCERTIFICATE_STRUCTW pCertViewInfo
)
4010 HWND status
= GetDlgItem(hwnd
, IDC_CERTIFICATESTATUSTEXT
);
4011 CRYPT_PROVIDER_SGNR
*provSigner
= WTHelperGetProvSignerFromChain(
4012 (CRYPT_PROVIDER_DATA
*)pCertViewInfo
->u
.pCryptProviderData
,
4013 pCertViewInfo
->idxSigner
, pCertViewInfo
->fCounterSigner
,
4014 pCertViewInfo
->idxCounterSigner
);
4015 CRYPT_PROVIDER_CERT
*provCert
= WTHelperGetProvCertFromChain(provSigner
,
4016 pCertViewInfo
->idxCert
);
4018 set_certificate_status(status
, provCert
);
4021 static void show_cert_hierarchy(HWND hwnd
, struct hierarchy_data
*data
)
4023 /* Disable view certificate button until a certificate is selected */
4024 EnableWindow(GetDlgItem(hwnd
, IDC_VIEWCERTIFICATE
), FALSE
);
4025 show_cert_chain(hwnd
, data
);
4026 set_certificate_status_for_end_cert(hwnd
, data
->pCertViewInfo
);
4029 static void show_dialog_for_selected_cert(HWND hwnd
)
4031 HWND tree
= GetDlgItem(hwnd
, IDC_CERTPATH
);
4033 struct hierarchy_data
*data
;
4036 memset(&item
, 0, sizeof(item
));
4037 item
.mask
= TVIF_HANDLE
| TVIF_PARAM
;
4038 item
.hItem
= (HTREEITEM
)SendMessageW(tree
, TVM_GETNEXTITEM
, TVGN_CARET
,
4040 SendMessageW(tree
, TVM_GETITEMW
, 0, (LPARAM
)&item
);
4041 data
= get_hierarchy_data_from_tree_item(tree
, item
.hItem
);
4042 selection
= lparam_to_index(data
, item
.lParam
);
4045 CRYPT_PROVIDER_SGNR
*provSigner
;
4046 CRYPTUI_VIEWCERTIFICATE_STRUCTW viewInfo
;
4047 BOOL changed
= FALSE
;
4049 provSigner
= WTHelperGetProvSignerFromChain(
4050 (CRYPT_PROVIDER_DATA
*)data
->pCertViewInfo
->u
.pCryptProviderData
,
4051 data
->pCertViewInfo
->idxSigner
,
4052 data
->pCertViewInfo
->fCounterSigner
,
4053 data
->pCertViewInfo
->idxCounterSigner
);
4054 memset(&viewInfo
, 0, sizeof(viewInfo
));
4055 viewInfo
.dwSize
= sizeof(viewInfo
);
4056 viewInfo
.dwFlags
= data
->pCertViewInfo
->dwFlags
;
4057 viewInfo
.szTitle
= data
->pCertViewInfo
->szTitle
;
4058 viewInfo
.pCertContext
= provSigner
->pasCertChain
[selection
].pCert
;
4059 viewInfo
.cStores
= data
->pCertViewInfo
->cStores
;
4060 viewInfo
.rghStores
= data
->pCertViewInfo
->rghStores
;
4061 viewInfo
.cPropSheetPages
= data
->pCertViewInfo
->cPropSheetPages
;
4062 viewInfo
.rgPropSheetPages
= data
->pCertViewInfo
->rgPropSheetPages
;
4063 viewInfo
.nStartPage
= data
->pCertViewInfo
->nStartPage
;
4064 CryptUIDlgViewCertificateW(&viewInfo
, &changed
);
4067 /* Delete the contents of the tree */
4068 SendMessageW(tree
, TVM_DELETEITEM
, 0, (LPARAM
)TVI_ROOT
);
4069 /* Reinitialize the tree */
4070 show_cert_hierarchy(hwnd
, data
);
4075 static LRESULT CALLBACK
hierarchy_dlg_proc(HWND hwnd
, UINT msg
, WPARAM wp
,
4078 PROPSHEETPAGEW
*page
;
4079 struct hierarchy_data
*data
;
4081 HWND tree
= GetDlgItem(hwnd
, IDC_CERTPATH
);
4083 TRACE("(%p, %08x, %08lx, %08lx)\n", hwnd
, msg
, wp
, lp
);
4088 page
= (PROPSHEETPAGEW
*)lp
;
4089 data
= (struct hierarchy_data
*)page
->lParam
;
4090 show_cert_hierarchy(hwnd
, data
);
4099 case TVN_SELCHANGEDW
:
4101 NMTREEVIEWW
*nm
= (NMTREEVIEWW
*)lp
;
4103 CRYPT_PROVIDER_SGNR
*provSigner
;
4105 data
= get_hierarchy_data_from_tree_item(tree
, nm
->itemNew
.hItem
);
4106 selection
= lparam_to_index(data
, nm
->itemNew
.lParam
);
4107 provSigner
= WTHelperGetProvSignerFromChain(
4108 (CRYPT_PROVIDER_DATA
*)data
->pCertViewInfo
->u
.pCryptProviderData
,
4109 data
->pCertViewInfo
->idxSigner
,
4110 data
->pCertViewInfo
->fCounterSigner
,
4111 data
->pCertViewInfo
->idxCounterSigner
);
4112 EnableWindow(GetDlgItem(hwnd
, IDC_VIEWCERTIFICATE
), selection
!= 0);
4113 set_certificate_status(GetDlgItem(hwnd
, IDC_CERTIFICATESTATUSTEXT
),
4114 &provSigner
->pasCertChain
[selection
]);
4118 show_dialog_for_selected_cert(hwnd
);
4119 SetWindowLongPtrW(hwnd
, DWLP_MSGRESULT
, 1);
4128 case IDC_VIEWCERTIFICATE
:
4129 show_dialog_for_selected_cert(hwnd
);
4133 case WM_REFRESH_VIEW
:
4137 /* Get hierarchy data */
4138 memset(&item
, 0, sizeof(item
));
4139 item
.mask
= TVIF_HANDLE
| TVIF_PARAM
;
4140 item
.hItem
= (HTREEITEM
)SendMessageW(tree
, TVM_GETNEXTITEM
, TVGN_ROOT
,
4142 data
= get_hierarchy_data_from_tree_item(tree
, item
.hItem
);
4143 /* Delete the contents of the tree */
4144 SendMessageW(tree
, TVM_DELETEITEM
, 0, (LPARAM
)TVI_ROOT
);
4145 /* Reinitialize the tree */
4146 show_cert_hierarchy(hwnd
, data
);
4153 static UINT CALLBACK
hierarchy_callback(HWND hwnd
, UINT msg
,
4154 PROPSHEETPAGEW
*page
)
4156 struct hierarchy_data
*data
;
4161 data
= (struct hierarchy_data
*)page
->lParam
;
4162 ImageList_Destroy(data
->imageList
);
4163 HeapFree(GetProcessHeap(), 0, data
);
4169 static BOOL
init_hierarchy_page(PCCRYPTUI_VIEWCERTIFICATE_STRUCTW pCertViewInfo
,
4170 PROPSHEETPAGEW
*page
)
4172 struct hierarchy_data
*data
= HeapAlloc(GetProcessHeap(), 0,
4173 sizeof(struct hierarchy_data
));
4178 data
->imageList
= ImageList_Create(16, 16, ILC_COLOR4
| ILC_MASK
, 2, 0);
4179 if (data
->imageList
)
4182 COLORREF backColor
= RGB(255, 0, 255);
4184 data
->pCertViewInfo
= pCertViewInfo
;
4185 data
->selectedCert
= 0xffffffff;
4187 bmp
= LoadBitmapW(hInstance
, MAKEINTRESOURCEW(IDB_SMALL_ICONS
));
4188 ImageList_AddMasked(data
->imageList
, bmp
, backColor
);
4190 ImageList_SetBkColor(data
->imageList
, CLR_NONE
);
4192 memset(page
, 0, sizeof(PROPSHEETPAGEW
));
4193 page
->dwSize
= sizeof(PROPSHEETPAGEW
);
4194 page
->dwFlags
= PSP_USECALLBACK
;
4195 page
->hInstance
= hInstance
;
4196 page
->u
.pszTemplate
= MAKEINTRESOURCEW(IDD_HIERARCHY
);
4197 page
->pfnDlgProc
= hierarchy_dlg_proc
;
4198 page
->lParam
= (LPARAM
)data
;
4199 page
->pfnCallback
= hierarchy_callback
;
4203 HeapFree(GetProcessHeap(), 0, data
);
4208 static int CALLBACK
cert_prop_sheet_proc(HWND hwnd
, UINT msg
, LPARAM lp
)
4213 TRACE("(%p, %08x, %08lx)\n", hwnd
, msg
, lp
);
4217 case PSCB_INITIALIZED
:
4218 /* Get cancel button's position.. */
4219 GetWindowRect(GetDlgItem(hwnd
, IDCANCEL
), &rc
);
4220 topLeft
.x
= rc
.left
;
4222 ScreenToClient(hwnd
, &topLeft
);
4223 /* hide the cancel button.. */
4224 ShowWindow(GetDlgItem(hwnd
, IDCANCEL
), FALSE
);
4225 /* get the OK button's size.. */
4226 GetWindowRect(GetDlgItem(hwnd
, IDOK
), &rc
);
4227 /* and move the OK button to the cancel button's original position. */
4228 MoveWindow(GetDlgItem(hwnd
, IDOK
), topLeft
.x
, topLeft
.y
,
4229 rc
.right
- rc
.left
, rc
.bottom
- rc
.top
, FALSE
);
4230 GetWindowRect(GetDlgItem(hwnd
, IDOK
), &rc
);
4236 static BOOL
show_cert_dialog(PCCRYPTUI_VIEWCERTIFICATE_STRUCTW pCertViewInfo
,
4237 CRYPT_PROVIDER_CERT
*provCert
, BOOL
*pfPropertiesChanged
)
4239 static const WCHAR riched
[] = { 'r','i','c','h','e','d','2','0',0 };
4241 PROPSHEETPAGEW
*pages
;
4243 HMODULE lib
= LoadLibraryW(riched
);
4245 nPages
= pCertViewInfo
->cPropSheetPages
+ 1; /* one for the General tab */
4246 if (!(pCertViewInfo
->dwFlags
& CRYPTUI_HIDE_DETAILPAGE
))
4248 if (!(pCertViewInfo
->dwFlags
& CRYPTUI_HIDE_HIERARCHYPAGE
))
4250 pages
= HeapAlloc(GetProcessHeap(), 0, nPages
* sizeof(PROPSHEETPAGEW
));
4253 PROPSHEETHEADERW hdr
;
4254 CRYPTUI_INITDIALOG_STRUCT
*init
= NULL
;
4257 memset(&hdr
, 0, sizeof(hdr
));
4258 hdr
.dwSize
= sizeof(hdr
);
4259 hdr
.dwFlags
= PSH_NOAPPLYNOW
| PSH_PROPSHEETPAGE
| PSH_USECALLBACK
;
4260 hdr
.hInstance
= hInstance
;
4261 if (pCertViewInfo
->szTitle
)
4262 hdr
.pszCaption
= pCertViewInfo
->szTitle
;
4264 hdr
.pszCaption
= MAKEINTRESOURCEW(IDS_CERTIFICATE
);
4265 init_general_page(pCertViewInfo
, &pages
[hdr
.nPages
++]);
4266 if (!(pCertViewInfo
->dwFlags
& CRYPTUI_HIDE_DETAILPAGE
))
4268 if (init_detail_page(pCertViewInfo
, pfPropertiesChanged
,
4269 &pages
[hdr
.nPages
]))
4272 if (!(pCertViewInfo
->dwFlags
& CRYPTUI_HIDE_HIERARCHYPAGE
))
4274 if (init_hierarchy_page(pCertViewInfo
, &pages
[hdr
.nPages
]))
4277 /* Copy each additional page, and create the init dialog struct for it
4279 if (pCertViewInfo
->cPropSheetPages
)
4281 init
= HeapAlloc(GetProcessHeap(), 0,
4282 pCertViewInfo
->cPropSheetPages
*
4283 sizeof(CRYPTUI_INITDIALOG_STRUCT
));
4286 for (i
= 0; i
< pCertViewInfo
->cPropSheetPages
; i
++)
4288 memcpy(&pages
[hdr
.nPages
+ i
],
4289 &pCertViewInfo
->rgPropSheetPages
[i
],
4290 sizeof(PROPSHEETPAGEW
));
4291 init
[i
].lParam
= pCertViewInfo
->rgPropSheetPages
[i
].lParam
;
4292 init
[i
].pCertContext
= pCertViewInfo
->pCertContext
;
4293 pages
[hdr
.nPages
+ i
].lParam
= (LPARAM
)&init
[i
];
4295 if (pCertViewInfo
->nStartPage
& 0x8000)
4297 /* Start page index is relative to the number of default
4300 hdr
.u2
.nStartPage
= pCertViewInfo
->nStartPage
+ hdr
.nPages
;
4303 hdr
.u2
.nStartPage
= pCertViewInfo
->nStartPage
;
4304 hdr
.nPages
= nPages
;
4308 SetLastError(ERROR_OUTOFMEMORY
);
4312 /* Ignore the relative flag if there aren't any additional pages */
4313 hdr
.u2
.nStartPage
= pCertViewInfo
->nStartPage
& 0x7fff;
4320 hdr
.u3
.ppsp
= pages
;
4321 hdr
.pfnCallback
= cert_prop_sheet_proc
;
4322 l
= PropertySheetW(&hdr
);
4325 SetLastError(ERROR_CANCELLED
);
4329 HeapFree(GetProcessHeap(), 0, init
);
4330 HeapFree(GetProcessHeap(), 0, pages
);
4333 SetLastError(ERROR_OUTOFMEMORY
);
4338 /***********************************************************************
4339 * CryptUIDlgViewCertificateW (CRYPTUI.@)
4341 BOOL WINAPI
CryptUIDlgViewCertificateW(
4342 PCCRYPTUI_VIEWCERTIFICATE_STRUCTW pCertViewInfo
, BOOL
*pfPropertiesChanged
)
4344 static GUID generic_cert_verify
= WINTRUST_ACTION_GENERIC_CERT_VERIFY
;
4345 CRYPTUI_VIEWCERTIFICATE_STRUCTW viewInfo
;
4347 WINTRUST_CERT_INFO cert
;
4349 CRYPT_PROVIDER_SGNR
*signer
;
4350 CRYPT_PROVIDER_CERT
*provCert
= NULL
;
4352 TRACE("(%p, %p)\n", pCertViewInfo
, pfPropertiesChanged
);
4354 if (pCertViewInfo
->dwSize
!= sizeof(CRYPTUI_VIEWCERTIFICATE_STRUCTW
))
4356 SetLastError(ERROR_INVALID_PARAMETER
);
4359 /* Make a local copy in case we have to call WinVerifyTrust ourselves */
4360 memcpy(&viewInfo
, pCertViewInfo
, sizeof(viewInfo
));
4361 if (!viewInfo
.u
.hWVTStateData
)
4363 memset(&wvt
, 0, sizeof(wvt
));
4364 wvt
.cbStruct
= sizeof(wvt
);
4365 wvt
.dwUIChoice
= WTD_UI_NONE
;
4366 if (viewInfo
.dwFlags
&
4367 CRYPTUI_ENABLE_REVOCATION_CHECK_CHAIN_EXCLUDE_ROOT
)
4368 wvt
.fdwRevocationChecks
|= WTD_REVOCATION_CHECK_CHAIN_EXCLUDE_ROOT
;
4369 if (viewInfo
.dwFlags
& CRYPTUI_ENABLE_REVOCATION_CHECK_END_CERT
)
4370 wvt
.fdwRevocationChecks
|= WTD_REVOCATION_CHECK_END_CERT
;
4371 if (viewInfo
.dwFlags
& CRYPTUI_ENABLE_REVOCATION_CHECK_CHAIN
)
4372 wvt
.fdwRevocationChecks
|= WTD_REVOCATION_CHECK_CHAIN
;
4373 wvt
.dwUnionChoice
= WTD_CHOICE_CERT
;
4374 memset(&cert
, 0, sizeof(cert
));
4375 cert
.cbStruct
= sizeof(cert
);
4376 cert
.psCertContext
= (CERT_CONTEXT
*)viewInfo
.pCertContext
;
4377 cert
.chStores
= viewInfo
.cStores
;
4378 cert
.pahStores
= viewInfo
.rghStores
;
4379 wvt
.u
.pCert
= &cert
;
4380 wvt
.dwStateAction
= WTD_STATEACTION_VERIFY
;
4381 WinVerifyTrust(NULL
, &generic_cert_verify
, &wvt
);
4382 viewInfo
.u
.pCryptProviderData
=
4383 WTHelperProvDataFromStateData(wvt
.hWVTStateData
);
4384 signer
= WTHelperGetProvSignerFromChain(
4385 (CRYPT_PROVIDER_DATA
*)viewInfo
.u
.pCryptProviderData
, 0, FALSE
, 0);
4386 provCert
= WTHelperGetProvCertFromChain(signer
, 0);
4391 viewInfo
.u
.pCryptProviderData
=
4392 WTHelperProvDataFromStateData(viewInfo
.u
.hWVTStateData
);
4393 signer
= WTHelperGetProvSignerFromChain(
4394 (CRYPT_PROVIDER_DATA
*)viewInfo
.u
.pCryptProviderData
,
4395 viewInfo
.idxSigner
, viewInfo
.fCounterSigner
,
4396 viewInfo
.idxCounterSigner
);
4397 provCert
= WTHelperGetProvCertFromChain(signer
, viewInfo
.idxCert
);
4402 ret
= show_cert_dialog(&viewInfo
, provCert
, pfPropertiesChanged
);
4403 if (!viewInfo
.u
.hWVTStateData
)
4405 wvt
.dwStateAction
= WTD_STATEACTION_CLOSE
;
4406 WinVerifyTrust(NULL
, &generic_cert_verify
, &wvt
);
4412 /***********************************************************************
4413 * CryptUIDlgViewContext (CRYPTUI.@)
4415 BOOL WINAPI
CryptUIDlgViewContext(DWORD dwContextType
, LPVOID pvContext
,
4416 HWND hwnd
, LPCWSTR pwszTitle
, DWORD dwFlags
, LPVOID pvReserved
)
4420 TRACE("(%d, %p, %p, %s, %08x, %p)\n", dwContextType
, pvContext
, hwnd
,
4421 debugstr_w(pwszTitle
), dwFlags
, pvReserved
);
4423 switch (dwContextType
)
4425 case CERT_STORE_CERTIFICATE_CONTEXT
:
4427 CRYPTUI_VIEWCERTIFICATE_STRUCTW viewInfo
;
4429 memset(&viewInfo
, 0, sizeof(viewInfo
));
4430 viewInfo
.dwSize
= sizeof(viewInfo
);
4431 viewInfo
.hwndParent
= hwnd
;
4432 viewInfo
.szTitle
= pwszTitle
;
4433 viewInfo
.pCertContext
= pvContext
;
4434 ret
= CryptUIDlgViewCertificateW(&viewInfo
, NULL
);
4438 FIXME("unimplemented for context type %d\n", dwContextType
);
4439 SetLastError(E_INVALIDARG
);
4445 /* Decodes a cert's basic constraints extension (either szOID_BASIC_CONSTRAINTS
4446 * or szOID_BASIC_CONSTRAINTS2, whichever is present) to determine if it
4447 * should be a CA. If neither extension is present, returns
4448 * defaultIfNotSpecified.
4450 static BOOL
is_ca_cert(PCCERT_CONTEXT cert
, BOOL defaultIfNotSpecified
)
4452 BOOL isCA
= defaultIfNotSpecified
;
4453 PCERT_EXTENSION ext
= CertFindExtension(szOID_BASIC_CONSTRAINTS
,
4454 cert
->pCertInfo
->cExtension
, cert
->pCertInfo
->rgExtension
);
4458 CERT_BASIC_CONSTRAINTS_INFO
*info
;
4461 if (CryptDecodeObjectEx(X509_ASN_ENCODING
, szOID_BASIC_CONSTRAINTS
,
4462 ext
->Value
.pbData
, ext
->Value
.cbData
, CRYPT_DECODE_ALLOC_FLAG
,
4463 NULL
, (LPBYTE
)&info
, &size
))
4465 if (info
->SubjectType
.cbData
== 1)
4466 isCA
= info
->SubjectType
.pbData
[0] & CERT_CA_SUBJECT_FLAG
;
4472 ext
= CertFindExtension(szOID_BASIC_CONSTRAINTS2
,
4473 cert
->pCertInfo
->cExtension
, cert
->pCertInfo
->rgExtension
);
4476 CERT_BASIC_CONSTRAINTS2_INFO info
;
4477 DWORD size
= sizeof(CERT_BASIC_CONSTRAINTS2_INFO
);
4479 if (CryptDecodeObjectEx(X509_ASN_ENCODING
,
4480 szOID_BASIC_CONSTRAINTS2
, ext
->Value
.pbData
, ext
->Value
.cbData
,
4481 0, NULL
, &info
, &size
))
4488 static HCERTSTORE
choose_store_for_cert(PCCERT_CONTEXT cert
)
4492 if (is_ca_cert(cert
, TRUE
))
4495 storeName
= addressBook
;
4496 return CertOpenStore(CERT_STORE_PROV_SYSTEM_W
, 0, 0,
4497 CERT_SYSTEM_STORE_CURRENT_USER
, storeName
);
4500 static BOOL
import_cert(PCCERT_CONTEXT cert
, HCERTSTORE hDestCertStore
)
4507 SetLastError(E_INVALIDARG
);
4510 if (hDestCertStore
) store
= hDestCertStore
;
4513 if (!(store
= choose_store_for_cert(cert
)))
4515 WARN("unable to open certificate store\n");
4519 ret
= CertAddCertificateContextToStore(store
, cert
,
4520 CERT_STORE_ADD_REPLACE_EXISTING_INHERIT_PROPERTIES
, NULL
);
4521 if (!hDestCertStore
) CertCloseStore(store
, 0);
4525 static BOOL
import_crl(PCCRL_CONTEXT crl
, HCERTSTORE hDestCertStore
)
4532 SetLastError(E_INVALIDARG
);
4535 if (hDestCertStore
) store
= hDestCertStore
;
4538 if (!(store
= CertOpenStore(CERT_STORE_PROV_SYSTEM_W
, 0, 0,
4539 CERT_SYSTEM_STORE_CURRENT_USER
, ca
)))
4541 WARN("unable to open certificate store\n");
4545 ret
= CertAddCRLContextToStore(store
, crl
,
4546 CERT_STORE_ADD_REPLACE_EXISTING_INHERIT_PROPERTIES
, NULL
);
4547 if (!hDestCertStore
) CertCloseStore(store
, 0);
4551 static BOOL
import_ctl(PCCTL_CONTEXT ctl
, HCERTSTORE hDestCertStore
)
4558 SetLastError(E_INVALIDARG
);
4561 if (hDestCertStore
) store
= hDestCertStore
;
4564 static const WCHAR trust
[] = { 'T','r','u','s','t',0 };
4566 if (!(store
= CertOpenStore(CERT_STORE_PROV_SYSTEM_W
, 0, 0,
4567 CERT_SYSTEM_STORE_CURRENT_USER
, trust
)))
4569 WARN("unable to open certificate store\n");
4573 ret
= CertAddCTLContextToStore(store
, ctl
,
4574 CERT_STORE_ADD_REPLACE_EXISTING_INHERIT_PROPERTIES
, NULL
);
4575 if (!hDestCertStore
) CertCloseStore(store
, 0);
4579 /* Checks type, a type such as CERT_QUERY_CONTENT_CERT returned by
4580 * CryptQueryObject, against the allowed types. Returns TRUE if the
4581 * type is allowed, FALSE otherwise.
4583 static BOOL
check_context_type(DWORD dwFlags
, DWORD type
)
4588 (CRYPTUI_WIZ_IMPORT_ALLOW_CERT
| CRYPTUI_WIZ_IMPORT_ALLOW_CRL
|
4589 CRYPTUI_WIZ_IMPORT_ALLOW_CTL
))
4593 case CERT_QUERY_CONTENT_CERT
:
4594 case CERT_QUERY_CONTENT_SERIALIZED_CERT
:
4595 ret
= dwFlags
& CRYPTUI_WIZ_IMPORT_ALLOW_CERT
;
4597 case CERT_QUERY_CONTENT_CRL
:
4598 case CERT_QUERY_CONTENT_SERIALIZED_CRL
:
4599 ret
= dwFlags
& CRYPTUI_WIZ_IMPORT_ALLOW_CRL
;
4601 case CERT_QUERY_CONTENT_CTL
:
4602 case CERT_QUERY_CONTENT_SERIALIZED_CTL
:
4603 ret
= dwFlags
& CRYPTUI_WIZ_IMPORT_ALLOW_CTL
;
4606 /* The remaining types contain more than one type, so allow
4614 /* No allowed types specified, so any type is allowed */
4618 SetLastError(E_INVALIDARG
);
4623 static void import_warning(DWORD dwFlags
, HWND hwnd
, LPCWSTR szTitle
,
4626 if (!(dwFlags
& CRYPTUI_WIZ_NO_UI
))
4628 WCHAR title
[MAX_STRING_LEN
], error
[MAX_STRING_LEN
];
4635 LoadStringW(hInstance
, IDS_IMPORT_WIZARD
, title
,
4636 sizeof(title
) / sizeof(title
[0]));
4639 LoadStringW(hInstance
, warningID
, error
,
4640 sizeof(error
) / sizeof(error
[0]));
4641 MessageBoxW(hwnd
, error
, pTitle
, MB_ICONERROR
| MB_OK
);
4645 static void import_warn_type_mismatch(DWORD dwFlags
, HWND hwnd
, LPCWSTR szTitle
)
4647 import_warning(dwFlags
, hwnd
, szTitle
, IDS_IMPORT_TYPE_MISMATCH
);
4650 static BOOL
check_store_context_type(DWORD dwFlags
, HCERTSTORE store
)
4655 (CRYPTUI_WIZ_IMPORT_ALLOW_CERT
| CRYPTUI_WIZ_IMPORT_ALLOW_CRL
|
4656 CRYPTUI_WIZ_IMPORT_ALLOW_CTL
))
4658 PCCERT_CONTEXT cert
;
4663 if ((cert
= CertEnumCertificatesInStore(store
, NULL
)))
4665 CertFreeCertificateContext(cert
);
4666 if (!(dwFlags
& CRYPTUI_WIZ_IMPORT_ALLOW_CERT
))
4669 if (ret
&& (crl
= CertEnumCRLsInStore(store
, NULL
)))
4671 CertFreeCRLContext(crl
);
4672 if (!(dwFlags
& CRYPTUI_WIZ_IMPORT_ALLOW_CRL
))
4675 if (ret
&& (ctl
= CertEnumCTLsInStore(store
, NULL
)))
4677 CertFreeCTLContext(ctl
);
4678 if (!(dwFlags
& CRYPTUI_WIZ_IMPORT_ALLOW_CTL
))
4685 SetLastError(E_INVALIDARG
);
4689 static BOOL
import_store(DWORD dwFlags
, HWND hwnd
, LPCWSTR szTitle
,
4690 HCERTSTORE source
, HCERTSTORE dest
)
4694 if ((ret
= check_store_context_type(dwFlags
, source
)))
4696 PCCERT_CONTEXT cert
= NULL
;
4697 PCCRL_CONTEXT crl
= NULL
;
4698 PCCTL_CONTEXT ctl
= NULL
;
4701 cert
= CertEnumCertificatesInStore(source
, cert
);
4703 ret
= import_cert(cert
, dest
);
4704 } while (ret
&& cert
);
4706 crl
= CertEnumCRLsInStore(source
, crl
);
4708 ret
= import_crl(crl
, dest
);
4709 } while (ret
&& crl
);
4711 ctl
= CertEnumCTLsInStore(source
, ctl
);
4713 ret
= import_ctl(ctl
, dest
);
4714 } while (ret
&& ctl
);
4717 import_warn_type_mismatch(dwFlags
, hwnd
, szTitle
);
4721 static HCERTSTORE
open_store_from_file(DWORD dwFlags
, LPCWSTR fileName
,
4722 DWORD
*pContentType
)
4724 HCERTSTORE store
= NULL
;
4725 DWORD contentType
= 0, expectedContentTypeFlags
;
4728 (CRYPTUI_WIZ_IMPORT_ALLOW_CERT
| CRYPTUI_WIZ_IMPORT_ALLOW_CRL
|
4729 CRYPTUI_WIZ_IMPORT_ALLOW_CTL
))
4731 expectedContentTypeFlags
=
4732 CERT_QUERY_CONTENT_FLAG_SERIALIZED_STORE
|
4733 CERT_QUERY_CONTENT_FLAG_PKCS7_SIGNED
|
4734 CERT_QUERY_CONTENT_FLAG_PFX
;
4735 if (dwFlags
& CRYPTUI_WIZ_IMPORT_ALLOW_CERT
)
4736 expectedContentTypeFlags
|=
4737 CERT_QUERY_CONTENT_FLAG_CERT
|
4738 CERT_QUERY_CONTENT_FLAG_SERIALIZED_CERT
;
4739 if (dwFlags
& CRYPTUI_WIZ_IMPORT_ALLOW_CRL
)
4740 expectedContentTypeFlags
|=
4741 CERT_QUERY_CONTENT_FLAG_SERIALIZED_CRL
|
4742 CERT_QUERY_CONTENT_FLAG_CRL
;
4743 if (dwFlags
& CRYPTUI_WIZ_IMPORT_ALLOW_CTL
)
4744 expectedContentTypeFlags
|=
4745 CERT_QUERY_CONTENT_FLAG_CTL
|
4746 CERT_QUERY_CONTENT_FLAG_SERIALIZED_CTL
;
4749 expectedContentTypeFlags
=
4750 CERT_QUERY_CONTENT_FLAG_CERT
|
4751 CERT_QUERY_CONTENT_FLAG_CTL
|
4752 CERT_QUERY_CONTENT_FLAG_CRL
|
4753 CERT_QUERY_CONTENT_FLAG_SERIALIZED_STORE
|
4754 CERT_QUERY_CONTENT_FLAG_SERIALIZED_CERT
|
4755 CERT_QUERY_CONTENT_FLAG_SERIALIZED_CTL
|
4756 CERT_QUERY_CONTENT_FLAG_SERIALIZED_CRL
|
4757 CERT_QUERY_CONTENT_FLAG_PKCS7_SIGNED
|
4758 CERT_QUERY_CONTENT_FLAG_PFX
;
4760 CryptQueryObject(CERT_QUERY_OBJECT_FILE
, fileName
,
4761 expectedContentTypeFlags
, CERT_QUERY_FORMAT_FLAG_ALL
, 0, NULL
,
4762 &contentType
, NULL
, &store
, NULL
, NULL
);
4764 *pContentType
= contentType
;
4768 static BOOL
import_file(DWORD dwFlags
, HWND hwnd
, LPCWSTR szTitle
,
4769 LPCWSTR fileName
, HCERTSTORE dest
)
4774 if ((source
= open_store_from_file(dwFlags
, fileName
, NULL
)))
4776 ret
= import_store(dwFlags
, hwnd
, szTitle
, source
, dest
);
4777 CertCloseStore(source
, 0);
4784 struct ImportWizData
4788 LPCWSTR pwszWizardTitle
;
4789 CRYPTUI_WIZ_IMPORT_SRC_INFO importSrc
;
4793 HCERTSTORE hDestCertStore
;
4799 static LRESULT CALLBACK
import_welcome_dlg_proc(HWND hwnd
, UINT msg
, WPARAM wp
,
4808 struct ImportWizData
*data
;
4809 PROPSHEETPAGEW
*page
= (PROPSHEETPAGEW
*)lp
;
4810 WCHAR fontFace
[MAX_STRING_LEN
];
4811 HDC hDC
= GetDC(hwnd
);
4814 data
= (struct ImportWizData
*)page
->lParam
;
4815 LoadStringW(hInstance
, IDS_WIZARD_TITLE_FONT
, fontFace
,
4816 sizeof(fontFace
) / sizeof(fontFace
[0]));
4817 height
= -MulDiv(12, GetDeviceCaps(hDC
, LOGPIXELSY
), 72);
4818 data
->titleFont
= CreateFontW(height
, 0, 0, 0, FW_BOLD
, 0, 0, 0,
4819 DEFAULT_CHARSET
, OUT_DEFAULT_PRECIS
, CLIP_DEFAULT_PRECIS
,
4820 DEFAULT_QUALITY
, DEFAULT_PITCH
| FF_DONTCARE
, fontFace
);
4821 SendMessageW(GetDlgItem(hwnd
, IDC_IMPORT_TITLE
), WM_SETFONT
,
4822 (WPARAM
)data
->titleFont
, TRUE
);
4823 ReleaseDC(hwnd
, hDC
);
4828 NMHDR
*hdr
= (NMHDR
*)lp
;
4833 PostMessageW(GetParent(hwnd
), PSM_SETWIZBUTTONS
, 0, PSWIZB_NEXT
);
4843 static const WCHAR filter_cert
[] = { '*','.','c','e','r',';','*','.',
4845 static const WCHAR filter_pfx
[] = { '*','.','p','f','x',';','*','.',
4847 static const WCHAR filter_crl
[] = { '*','.','c','r','l',0 };
4848 static const WCHAR filter_ctl
[] = { '*','.','s','t','l',0 };
4849 static const WCHAR filter_serialized_store
[] = { '*','.','s','s','t',0 };
4850 static const WCHAR filter_cms
[] = { '*','.','s','p','c',';','*','.',
4852 static const WCHAR filter_all
[] = { '*','.','*',0 };
4854 struct StringToFilter
4859 } import_filters
[] = {
4860 { IDS_IMPORT_FILTER_CERT
, CRYPTUI_WIZ_IMPORT_ALLOW_CERT
, filter_cert
},
4861 { IDS_IMPORT_FILTER_PFX
, 0, filter_pfx
},
4862 { IDS_IMPORT_FILTER_CRL
, CRYPTUI_WIZ_IMPORT_ALLOW_CRL
, filter_crl
},
4863 { IDS_IMPORT_FILTER_CTL
, CRYPTUI_WIZ_IMPORT_ALLOW_CTL
, filter_ctl
},
4864 { IDS_IMPORT_FILTER_SERIALIZED_STORE
, 0, filter_serialized_store
},
4865 { IDS_IMPORT_FILTER_CMS
, 0, filter_cms
},
4866 { IDS_IMPORT_FILTER_ALL
, 0, filter_all
},
4869 static WCHAR
*make_import_file_filter(DWORD dwFlags
)
4872 int len
, totalLen
= 2;
4873 LPWSTR filter
= NULL
, str
;
4875 for (i
= 0; i
< sizeof(import_filters
) / sizeof(import_filters
[0]); i
++)
4877 if (!import_filters
[i
].allowFlags
|| !dwFlags
||
4878 (dwFlags
& import_filters
[i
].allowFlags
))
4880 len
= LoadStringW(hInstance
, import_filters
[i
].id
, (LPWSTR
)&str
, 0);
4881 totalLen
+= len
+ strlenW(import_filters
[i
].filter
) + 2;
4884 filter
= HeapAlloc(GetProcessHeap(), 0, totalLen
* sizeof(WCHAR
));
4890 for (i
= 0; i
< sizeof(import_filters
) / sizeof(import_filters
[0]); i
++)
4892 if (!import_filters
[i
].allowFlags
|| !dwFlags
||
4893 (dwFlags
& import_filters
[i
].allowFlags
))
4895 len
= LoadStringW(hInstance
, import_filters
[i
].id
,
4897 memcpy(ptr
, str
, len
* sizeof(WCHAR
));
4900 strcpyW(ptr
, import_filters
[i
].filter
);
4901 ptr
+= strlenW(import_filters
[i
].filter
) + 1;
4909 static BOOL
import_validate_filename(HWND hwnd
, struct ImportWizData
*data
,
4915 file
= CreateFileW(fileName
, GENERIC_READ
, FILE_SHARE_READ
, NULL
,
4916 OPEN_EXISTING
, 0, NULL
);
4917 if (file
!= INVALID_HANDLE_VALUE
)
4919 HCERTSTORE source
= open_store_from_file(data
->dwFlags
, fileName
,
4920 &data
->contentType
);
4924 warningID
= IDS_IMPORT_BAD_FORMAT
;
4925 else if (!check_store_context_type(data
->dwFlags
, source
))
4926 warningID
= IDS_IMPORT_TYPE_MISMATCH
;
4929 data
->importSrc
.dwSubjectChoice
=
4930 CRYPTUI_WIZ_IMPORT_SUBJECT_CERT_STORE
;
4931 data
->importSrc
.u
.hCertStore
= source
;
4932 data
->freeSource
= TRUE
;
4937 import_warning(data
->dwFlags
, hwnd
, data
->pwszWizardTitle
,
4944 WCHAR title
[MAX_STRING_LEN
], error
[MAX_STRING_LEN
];
4946 LPWSTR msgBuf
, fullError
;
4948 if (data
->pwszWizardTitle
)
4949 pTitle
= data
->pwszWizardTitle
;
4952 LoadStringW(hInstance
, IDS_IMPORT_WIZARD
, title
,
4953 sizeof(title
) / sizeof(title
[0]));
4956 LoadStringW(hInstance
, IDS_IMPORT_OPEN_FAILED
, error
,
4957 sizeof(error
) / sizeof(error
[0]));
4959 FORMAT_MESSAGE_ALLOCATE_BUFFER
| FORMAT_MESSAGE_FROM_SYSTEM
, NULL
,
4960 GetLastError(), 0, (LPWSTR
) &msgBuf
, 0, NULL
);
4961 fullError
= HeapAlloc(GetProcessHeap(), 0,
4962 (strlenW(error
) + strlenW(fileName
) + strlenW(msgBuf
) + 3)
4966 LPWSTR ptr
= fullError
;
4968 strcpyW(ptr
, error
);
4969 ptr
+= strlenW(error
);
4970 strcpyW(ptr
, fileName
);
4971 ptr
+= strlenW(fileName
);
4974 strcpyW(ptr
, msgBuf
);
4975 MessageBoxW(hwnd
, fullError
, pTitle
, MB_ICONERROR
| MB_OK
);
4976 HeapFree(GetProcessHeap(), 0, fullError
);
4983 static LRESULT CALLBACK
import_file_dlg_proc(HWND hwnd
, UINT msg
, WPARAM wp
,
4987 struct ImportWizData
*data
;
4993 PROPSHEETPAGEW
*page
= (PROPSHEETPAGEW
*)lp
;
4995 data
= (struct ImportWizData
*)page
->lParam
;
4996 SetWindowLongPtrW(hwnd
, DWLP_USER
, (LPARAM
)data
);
5001 NMHDR
*hdr
= (NMHDR
*)lp
;
5006 PostMessageW(GetParent(hwnd
), PSM_SETWIZBUTTONS
, 0,
5007 PSWIZB_BACK
| PSWIZB_NEXT
);
5012 HWND fileNameEdit
= GetDlgItem(hwnd
, IDC_IMPORT_FILENAME
);
5013 DWORD len
= SendMessageW(fileNameEdit
, WM_GETTEXTLENGTH
, 0, 0);
5015 data
= (struct ImportWizData
*)GetWindowLongPtrW(hwnd
, DWLP_USER
);
5018 import_warning(data
->dwFlags
, hwnd
, data
->pwszWizardTitle
,
5019 IDS_IMPORT_EMPTY_FILE
);
5020 SetWindowLongPtrW(hwnd
, DWLP_MSGRESULT
, 1);
5025 LPWSTR fileName
= HeapAlloc(GetProcessHeap(), 0,
5026 (len
+ 1) * sizeof(WCHAR
));
5030 SendMessageW(fileNameEdit
, WM_GETTEXT
, len
+ 1,
5032 if (!import_validate_filename(hwnd
, data
, fileName
))
5034 HeapFree(GetProcessHeap(), 0, fileName
);
5035 SetWindowLongPtrW(hwnd
, DWLP_MSGRESULT
, 1);
5039 data
->fileName
= fileName
;
5050 case IDC_IMPORT_BROWSE_FILE
:
5053 WCHAR fileBuf
[MAX_PATH
];
5055 data
= (struct ImportWizData
*)GetWindowLongPtrW(hwnd
, DWLP_USER
);
5056 memset(&ofn
, 0, sizeof(ofn
));
5057 ofn
.lStructSize
= sizeof(ofn
);
5058 ofn
.hwndOwner
= hwnd
;
5059 ofn
.lpstrFilter
= make_import_file_filter(data
->dwFlags
);
5060 ofn
.lpstrFile
= fileBuf
;
5061 ofn
.nMaxFile
= sizeof(fileBuf
) / sizeof(fileBuf
[0]);
5063 if (GetOpenFileNameW(&ofn
))
5064 SendMessageW(GetDlgItem(hwnd
, IDC_IMPORT_FILENAME
), WM_SETTEXT
,
5065 0, (LPARAM
)ofn
.lpstrFile
);
5066 HeapFree(GetProcessHeap(), 0, (LPWSTR
)ofn
.lpstrFilter
);
5075 static LRESULT CALLBACK
import_store_dlg_proc(HWND hwnd
, UINT msg
, WPARAM wp
,
5079 struct ImportWizData
*data
;
5085 PROPSHEETPAGEW
*page
= (PROPSHEETPAGEW
*)lp
;
5087 data
= (struct ImportWizData
*)page
->lParam
;
5088 SetWindowLongPtrW(hwnd
, DWLP_USER
, (LPARAM
)data
);
5089 if (!data
->hDestCertStore
)
5091 SendMessageW(GetDlgItem(hwnd
, IDC_IMPORT_AUTO_STORE
), BM_CLICK
,
5093 EnableWindow(GetDlgItem(hwnd
, IDC_IMPORT_STORE
), FALSE
);
5094 EnableWindow(GetDlgItem(hwnd
, IDC_IMPORT_BROWSE_STORE
), FALSE
);
5095 EnableWindow(GetDlgItem(hwnd
, IDC_IMPORT_SPECIFY_STORE
), FALSE
);
5099 WCHAR storeTitle
[MAX_STRING_LEN
];
5101 SendMessageW(GetDlgItem(hwnd
, IDC_IMPORT_SPECIFY_STORE
), BM_CLICK
,
5103 EnableWindow(GetDlgItem(hwnd
, IDC_IMPORT_STORE
), TRUE
);
5104 EnableWindow(GetDlgItem(hwnd
, IDC_IMPORT_BROWSE_STORE
), TRUE
);
5105 EnableWindow(GetDlgItem(hwnd
, IDC_IMPORT_SPECIFY_STORE
),
5106 !(data
->dwFlags
& CRYPTUI_WIZ_IMPORT_NO_CHANGE_DEST_STORE
));
5107 LoadStringW(hInstance
, IDS_IMPORT_DEST_DETERMINED
,
5108 storeTitle
, sizeof(storeTitle
) / sizeof(storeTitle
[0]));
5109 SendMessageW(GetDlgItem(hwnd
, IDC_IMPORT_STORE
), WM_SETTEXT
,
5110 0, (LPARAM
)storeTitle
);
5116 NMHDR
*hdr
= (NMHDR
*)lp
;
5121 PostMessageW(GetParent(hwnd
), PSM_SETWIZBUTTONS
, 0,
5122 PSWIZB_BACK
| PSWIZB_NEXT
);
5127 data
= (struct ImportWizData
*)GetWindowLongPtrW(hwnd
, DWLP_USER
);
5128 if (IsDlgButtonChecked(hwnd
, IDC_IMPORT_SPECIFY_STORE
) &&
5129 !data
->hDestCertStore
)
5131 import_warning(data
->dwFlags
, hwnd
, data
->pwszWizardTitle
,
5132 IDS_IMPORT_SELECT_STORE
);
5133 SetWindowLongPtrW(hwnd
, DWLP_MSGRESULT
, 1);
5144 case IDC_IMPORT_AUTO_STORE
:
5145 data
= (struct ImportWizData
*)GetWindowLongPtrW(hwnd
, DWLP_USER
);
5146 data
->autoDest
= TRUE
;
5147 EnableWindow(GetDlgItem(hwnd
, IDC_IMPORT_STORE
), FALSE
);
5148 EnableWindow(GetDlgItem(hwnd
, IDC_IMPORT_BROWSE_STORE
), FALSE
);
5150 case IDC_IMPORT_SPECIFY_STORE
:
5151 data
= (struct ImportWizData
*)GetWindowLongPtrW(hwnd
, DWLP_USER
);
5152 data
->autoDest
= FALSE
;
5153 EnableWindow(GetDlgItem(hwnd
, IDC_IMPORT_STORE
), TRUE
);
5154 EnableWindow(GetDlgItem(hwnd
, IDC_IMPORT_BROWSE_STORE
), TRUE
);
5156 case IDC_IMPORT_BROWSE_STORE
:
5158 CRYPTUI_ENUM_SYSTEM_STORE_ARGS enumArgs
= {
5159 CERT_SYSTEM_STORE_CURRENT_USER
, NULL
};
5160 CRYPTUI_ENUM_DATA enumData
= { 0, NULL
, 1, &enumArgs
};
5161 CRYPTUI_SELECTSTORE_INFO_W selectInfo
;
5164 data
= (struct ImportWizData
*)GetWindowLongPtrW(hwnd
, DWLP_USER
);
5165 selectInfo
.dwSize
= sizeof(selectInfo
);
5166 selectInfo
.parent
= hwnd
;
5167 selectInfo
.dwFlags
= CRYPTUI_ENABLE_SHOW_PHYSICAL_STORE
;
5168 selectInfo
.pwszTitle
= selectInfo
.pwszTitle
= NULL
;
5169 selectInfo
.pEnumData
= &enumData
;
5170 selectInfo
.pfnSelectedStoreCallback
= NULL
;
5171 if ((store
= CryptUIDlgSelectStoreW(&selectInfo
)))
5173 WCHAR storeTitle
[MAX_STRING_LEN
];
5175 LoadStringW(hInstance
, IDS_IMPORT_DEST_DETERMINED
,
5176 storeTitle
, sizeof(storeTitle
) / sizeof(storeTitle
[0]));
5177 SendMessageW(GetDlgItem(hwnd
, IDC_IMPORT_STORE
), WM_SETTEXT
,
5178 0, (LPARAM
)storeTitle
);
5179 data
->hDestCertStore
= store
;
5180 data
->freeDest
= TRUE
;
5190 static void show_import_details(HWND lv
, struct ImportWizData
*data
)
5192 WCHAR text
[MAX_STRING_LEN
];
5196 item
.mask
= LVIF_TEXT
;
5197 item
.iItem
= SendMessageW(lv
, LVM_GETITEMCOUNT
, 0, 0);
5199 LoadStringW(hInstance
, IDS_IMPORT_STORE_SELECTION
, text
,
5200 sizeof(text
)/ sizeof(text
[0]));
5201 item
.pszText
= text
;
5202 SendMessageW(lv
, LVM_INSERTITEMW
, 0, (LPARAM
)&item
);
5205 LoadStringW(hInstance
, IDS_IMPORT_DEST_AUTOMATIC
, text
,
5206 sizeof(text
)/ sizeof(text
[0]));
5208 LoadStringW(hInstance
, IDS_IMPORT_DEST_DETERMINED
, text
,
5209 sizeof(text
)/ sizeof(text
[0]));
5210 SendMessageW(lv
, LVM_SETITEMTEXTW
, item
.iItem
, (LPARAM
)&item
);
5211 item
.iItem
= SendMessageW(lv
, LVM_GETITEMCOUNT
, 0, 0);
5213 LoadStringW(hInstance
, IDS_IMPORT_CONTENT
, text
,
5214 sizeof(text
)/ sizeof(text
[0]));
5215 SendMessageW(lv
, LVM_INSERTITEMW
, 0, (LPARAM
)&item
);
5216 switch (data
->contentType
)
5218 case CERT_QUERY_CONTENT_CERT
:
5219 case CERT_QUERY_CONTENT_SERIALIZED_CERT
:
5220 contentID
= IDS_IMPORT_CONTENT_CERT
;
5222 case CERT_QUERY_CONTENT_CRL
:
5223 case CERT_QUERY_CONTENT_SERIALIZED_CRL
:
5224 contentID
= IDS_IMPORT_CONTENT_CRL
;
5226 case CERT_QUERY_CONTENT_CTL
:
5227 case CERT_QUERY_CONTENT_SERIALIZED_CTL
:
5228 contentID
= IDS_IMPORT_CONTENT_CTL
;
5230 case CERT_QUERY_CONTENT_PKCS7_SIGNED
:
5231 contentID
= IDS_IMPORT_CONTENT_CMS
;
5233 case CERT_QUERY_CONTENT_FLAG_PFX
:
5234 contentID
= IDS_IMPORT_CONTENT_PFX
;
5237 contentID
= IDS_IMPORT_CONTENT_STORE
;
5240 LoadStringW(hInstance
, contentID
, text
, sizeof(text
)/ sizeof(text
[0]));
5242 SendMessageW(lv
, LVM_SETITEMTEXTW
, item
.iItem
, (LPARAM
)&item
);
5245 item
.iItem
= SendMessageW(lv
, LVM_GETITEMCOUNT
, 0, 0);
5247 LoadStringW(hInstance
, IDS_IMPORT_FILE
, text
,
5248 sizeof(text
)/ sizeof(text
[0]));
5249 SendMessageW(lv
, LVM_INSERTITEMW
, 0, (LPARAM
)&item
);
5251 item
.pszText
= data
->fileName
;
5252 SendMessageW(lv
, LVM_SETITEMTEXTW
, item
.iItem
, (LPARAM
)&item
);
5256 static BOOL
do_import(DWORD dwFlags
, HWND hwndParent
, LPCWSTR pwszWizardTitle
,
5257 PCCRYPTUI_WIZ_IMPORT_SRC_INFO pImportSrc
, HCERTSTORE hDestCertStore
)
5261 switch (pImportSrc
->dwSubjectChoice
)
5263 case CRYPTUI_WIZ_IMPORT_SUBJECT_FILE
:
5264 ret
= import_file(dwFlags
, hwndParent
, pwszWizardTitle
,
5265 pImportSrc
->u
.pwszFileName
, hDestCertStore
);
5267 case CRYPTUI_WIZ_IMPORT_SUBJECT_CERT_CONTEXT
:
5268 if ((ret
= check_context_type(dwFlags
, CERT_QUERY_CONTENT_CERT
)))
5269 ret
= import_cert(pImportSrc
->u
.pCertContext
, hDestCertStore
);
5271 import_warn_type_mismatch(dwFlags
, hwndParent
, pwszWizardTitle
);
5273 case CRYPTUI_WIZ_IMPORT_SUBJECT_CRL_CONTEXT
:
5274 if ((ret
= check_context_type(dwFlags
, CERT_QUERY_CONTENT_CRL
)))
5275 ret
= import_crl(pImportSrc
->u
.pCRLContext
, hDestCertStore
);
5277 import_warn_type_mismatch(dwFlags
, hwndParent
, pwszWizardTitle
);
5279 case CRYPTUI_WIZ_IMPORT_SUBJECT_CTL_CONTEXT
:
5280 if ((ret
= check_context_type(dwFlags
, CERT_QUERY_CONTENT_CTL
)))
5281 ret
= import_ctl(pImportSrc
->u
.pCTLContext
, hDestCertStore
);
5283 import_warn_type_mismatch(dwFlags
, hwndParent
, pwszWizardTitle
);
5285 case CRYPTUI_WIZ_IMPORT_SUBJECT_CERT_STORE
:
5286 ret
= import_store(dwFlags
, hwndParent
, pwszWizardTitle
,
5287 pImportSrc
->u
.hCertStore
, hDestCertStore
);
5290 WARN("unknown source type: %u\n", pImportSrc
->dwSubjectChoice
);
5291 SetLastError(E_INVALIDARG
);
5297 static LRESULT CALLBACK
import_finish_dlg_proc(HWND hwnd
, UINT msg
, WPARAM wp
,
5301 struct ImportWizData
*data
;
5307 PROPSHEETPAGEW
*page
= (PROPSHEETPAGEW
*)lp
;
5308 HWND lv
= GetDlgItem(hwnd
, IDC_IMPORT_SETTINGS
);
5312 data
= (struct ImportWizData
*)page
->lParam
;
5313 SetWindowLongPtrW(hwnd
, DWLP_USER
, (LPARAM
)data
);
5314 SendMessageW(GetDlgItem(hwnd
, IDC_IMPORT_TITLE
), WM_SETFONT
,
5315 (WPARAM
)data
->titleFont
, TRUE
);
5316 GetWindowRect(lv
, &rc
);
5317 column
.mask
= LVCF_WIDTH
;
5318 column
.cx
= (rc
.right
- rc
.left
) / 2 - 2;
5319 SendMessageW(lv
, LVM_INSERTCOLUMNW
, 0, (LPARAM
)&column
);
5320 SendMessageW(lv
, LVM_INSERTCOLUMNW
, 1, (LPARAM
)&column
);
5321 show_import_details(lv
, data
);
5326 NMHDR
*hdr
= (NMHDR
*)lp
;
5332 HWND lv
= GetDlgItem(hwnd
, IDC_IMPORT_SETTINGS
);
5334 data
= (struct ImportWizData
*)GetWindowLongPtrW(hwnd
, DWLP_USER
);
5335 SendMessageW(lv
, LVM_DELETEALLITEMS
, 0, 0);
5336 show_import_details(lv
, data
);
5337 PostMessageW(GetParent(hwnd
), PSM_SETWIZBUTTONS
, 0,
5338 PSWIZB_BACK
| PSWIZB_FINISH
);
5344 data
= (struct ImportWizData
*)GetWindowLongPtrW(hwnd
, DWLP_USER
);
5345 if ((data
->success
= do_import(data
->dwFlags
, hwnd
,
5346 data
->pwszWizardTitle
, &data
->importSrc
, data
->hDestCertStore
)))
5348 WCHAR title
[MAX_STRING_LEN
], message
[MAX_STRING_LEN
];
5351 if (data
->pwszWizardTitle
)
5352 pTitle
= data
->pwszWizardTitle
;
5355 LoadStringW(hInstance
, IDS_IMPORT_WIZARD
, title
,
5356 sizeof(title
) / sizeof(title
[0]));
5359 LoadStringW(hInstance
, IDS_IMPORT_SUCCEEDED
, message
,
5360 sizeof(message
) / sizeof(message
[0]));
5361 MessageBoxW(hwnd
, message
, pTitle
, MB_OK
);
5364 import_warning(data
->dwFlags
, hwnd
, data
->pwszWizardTitle
,
5375 static BOOL
show_import_ui(DWORD dwFlags
, HWND hwndParent
,
5376 LPCWSTR pwszWizardTitle
, PCCRYPTUI_WIZ_IMPORT_SRC_INFO pImportSrc
,
5377 HCERTSTORE hDestCertStore
)
5379 PROPSHEETHEADERW hdr
;
5380 PROPSHEETPAGEW pages
[4];
5381 struct ImportWizData data
;
5384 data
.dwFlags
= dwFlags
;
5385 data
.pwszWizardTitle
= pwszWizardTitle
;
5387 memcpy(&data
.importSrc
, pImportSrc
, sizeof(data
.importSrc
));
5389 memset(&data
.importSrc
, 0, sizeof(data
.importSrc
));
5390 data
.fileName
= NULL
;
5391 data
.freeSource
= FALSE
;
5392 data
.hDestCertStore
= hDestCertStore
;
5393 data
.freeDest
= FALSE
;
5394 data
.autoDest
= TRUE
;
5395 data
.success
= TRUE
;
5397 memset(&pages
, 0, sizeof(pages
));
5399 pages
[nPages
].dwSize
= sizeof(pages
[0]);
5400 pages
[nPages
].hInstance
= hInstance
;
5401 pages
[nPages
].u
.pszTemplate
= MAKEINTRESOURCEW(IDD_IMPORT_WELCOME
);
5402 pages
[nPages
].pfnDlgProc
= import_welcome_dlg_proc
;
5403 pages
[nPages
].dwFlags
= PSP_HIDEHEADER
;
5404 pages
[nPages
].lParam
= (LPARAM
)&data
;
5408 pImportSrc
->dwSubjectChoice
== CRYPTUI_WIZ_IMPORT_SUBJECT_FILE
)
5410 pages
[nPages
].dwSize
= sizeof(pages
[0]);
5411 pages
[nPages
].hInstance
= hInstance
;
5412 pages
[nPages
].u
.pszTemplate
= MAKEINTRESOURCEW(IDD_IMPORT_FILE
);
5413 pages
[nPages
].pfnDlgProc
= import_file_dlg_proc
;
5414 pages
[nPages
].dwFlags
= PSP_USEHEADERTITLE
| PSP_USEHEADERSUBTITLE
;
5415 pages
[nPages
].pszHeaderTitle
= MAKEINTRESOURCEW(IDS_IMPORT_FILE_TITLE
);
5416 pages
[nPages
].pszHeaderSubTitle
=
5417 MAKEINTRESOURCEW(IDS_IMPORT_FILE_SUBTITLE
);
5418 pages
[nPages
].lParam
= (LPARAM
)&data
;
5423 switch (pImportSrc
->dwSubjectChoice
)
5425 case CRYPTUI_WIZ_IMPORT_SUBJECT_CERT_CONTEXT
:
5426 data
.contentType
= CERT_QUERY_CONTENT_CERT
;
5428 case CRYPTUI_WIZ_IMPORT_SUBJECT_CRL_CONTEXT
:
5429 data
.contentType
= CERT_QUERY_CONTENT_CRL
;
5431 case CRYPTUI_WIZ_IMPORT_SUBJECT_CTL_CONTEXT
:
5432 data
.contentType
= CERT_QUERY_CONTENT_CTL
;
5434 case CRYPTUI_WIZ_IMPORT_SUBJECT_CERT_STORE
:
5435 data
.contentType
= CERT_QUERY_CONTENT_SERIALIZED_STORE
;
5440 pages
[nPages
].dwSize
= sizeof(pages
[0]);
5441 pages
[nPages
].hInstance
= hInstance
;
5442 pages
[nPages
].u
.pszTemplate
= MAKEINTRESOURCEW(IDD_IMPORT_STORE
);
5443 pages
[nPages
].pfnDlgProc
= import_store_dlg_proc
;
5444 pages
[nPages
].dwFlags
= PSP_USEHEADERTITLE
| PSP_USEHEADERSUBTITLE
;
5445 pages
[nPages
].pszHeaderTitle
= MAKEINTRESOURCEW(IDS_IMPORT_STORE_TITLE
);
5446 pages
[nPages
].pszHeaderSubTitle
=
5447 MAKEINTRESOURCEW(IDS_IMPORT_STORE_SUBTITLE
);
5448 pages
[nPages
].lParam
= (LPARAM
)&data
;
5451 pages
[nPages
].dwSize
= sizeof(pages
[0]);
5452 pages
[nPages
].hInstance
= hInstance
;
5453 pages
[nPages
].u
.pszTemplate
= MAKEINTRESOURCEW(IDD_IMPORT_FINISH
);
5454 pages
[nPages
].pfnDlgProc
= import_finish_dlg_proc
;
5455 pages
[nPages
].dwFlags
= PSP_HIDEHEADER
;
5456 pages
[nPages
].lParam
= (LPARAM
)&data
;
5459 memset(&hdr
, 0, sizeof(hdr
));
5460 hdr
.dwSize
= sizeof(hdr
);
5461 hdr
.hwndParent
= hwndParent
;
5462 hdr
.dwFlags
= PSH_PROPSHEETPAGE
| PSH_WIZARD97_NEW
| PSH_HEADER
|
5464 hdr
.hInstance
= hInstance
;
5465 if (pwszWizardTitle
)
5466 hdr
.pszCaption
= pwszWizardTitle
;
5468 hdr
.pszCaption
= MAKEINTRESOURCEW(IDS_IMPORT_WIZARD
);
5469 hdr
.u3
.ppsp
= pages
;
5470 hdr
.nPages
= nPages
;
5471 hdr
.u4
.pszbmWatermark
= MAKEINTRESOURCEW(IDB_CERT_WATERMARK
);
5472 hdr
.u5
.pszbmHeader
= MAKEINTRESOURCEW(IDB_CERT_HEADER
);
5473 PropertySheetW(&hdr
);
5474 HeapFree(GetProcessHeap(), 0, data
.fileName
);
5475 if (data
.freeSource
&&
5476 data
.importSrc
.dwSubjectChoice
== CRYPTUI_WIZ_IMPORT_SUBJECT_CERT_STORE
)
5477 CertCloseStore(data
.importSrc
.u
.hCertStore
, 0);
5478 DeleteObject(data
.titleFont
);
5479 return data
.success
;
5482 BOOL WINAPI
CryptUIWizImport(DWORD dwFlags
, HWND hwndParent
, LPCWSTR pwszWizardTitle
,
5483 PCCRYPTUI_WIZ_IMPORT_SRC_INFO pImportSrc
, HCERTSTORE hDestCertStore
)
5487 TRACE("(0x%08x, %p, %s, %p, %p)\n", dwFlags
, hwndParent
, debugstr_w(pwszWizardTitle
),
5488 pImportSrc
, hDestCertStore
);
5491 pImportSrc
->dwSize
!= sizeof(CRYPTUI_WIZ_IMPORT_SRC_INFO
))
5493 SetLastError(E_INVALIDARG
);
5497 if (!(dwFlags
& CRYPTUI_WIZ_NO_UI
))
5498 ret
= show_import_ui(dwFlags
, hwndParent
, pwszWizardTitle
, pImportSrc
,
5500 else if (pImportSrc
)
5501 ret
= do_import(dwFlags
, hwndParent
, pwszWizardTitle
, pImportSrc
,
5505 /* Can't have no UI without specifying source */
5506 SetLastError(E_INVALIDARG
);
5513 struct ExportWizData
5517 LPCWSTR pwszWizardTitle
;
5518 CRYPTUI_WIZ_EXPORT_INFO exportInfo
;
5519 CRYPTUI_WIZ_EXPORT_CERTCONTEXT_INFO contextInfo
;
5521 PCRYPT_KEY_PROV_INFO keyProvInfo
;
5528 static LRESULT CALLBACK
export_welcome_dlg_proc(HWND hwnd
, UINT msg
, WPARAM wp
,
5537 struct ExportWizData
*data
;
5538 PROPSHEETPAGEW
*page
= (PROPSHEETPAGEW
*)lp
;
5539 WCHAR fontFace
[MAX_STRING_LEN
];
5540 HDC hDC
= GetDC(hwnd
);
5543 data
= (struct ExportWizData
*)page
->lParam
;
5544 LoadStringW(hInstance
, IDS_WIZARD_TITLE_FONT
, fontFace
,
5545 sizeof(fontFace
) / sizeof(fontFace
[0]));
5546 height
= -MulDiv(12, GetDeviceCaps(hDC
, LOGPIXELSY
), 72);
5547 data
->titleFont
= CreateFontW(height
, 0, 0, 0, FW_BOLD
, 0, 0, 0,
5548 DEFAULT_CHARSET
, OUT_DEFAULT_PRECIS
, CLIP_DEFAULT_PRECIS
,
5549 DEFAULT_QUALITY
, DEFAULT_PITCH
| FF_DONTCARE
, fontFace
);
5550 SendMessageW(GetDlgItem(hwnd
, IDC_EXPORT_TITLE
), WM_SETFONT
,
5551 (WPARAM
)data
->titleFont
, TRUE
);
5552 ReleaseDC(hwnd
, hDC
);
5557 NMHDR
*hdr
= (NMHDR
*)lp
;
5562 PostMessageW(GetParent(hwnd
), PSM_SETWIZBUTTONS
, 0, PSWIZB_NEXT
);
5572 static PCRYPT_KEY_PROV_INFO
export_get_private_key_info(PCCERT_CONTEXT cert
)
5574 PCRYPT_KEY_PROV_INFO info
= NULL
;
5577 if (CertGetCertificateContextProperty(cert
, CERT_KEY_PROV_INFO_PROP_ID
,
5580 info
= HeapAlloc(GetProcessHeap(), 0, size
);
5583 if (!CertGetCertificateContextProperty(cert
,
5584 CERT_KEY_PROV_INFO_PROP_ID
, info
, &size
))
5586 HeapFree(GetProcessHeap(), 0, info
);
5594 static BOOL
export_acquire_private_key(PCRYPT_KEY_PROV_INFO info
,
5599 ret
= CryptAcquireContextW(phProv
, info
->pwszContainerName
,
5600 info
->pwszProvName
, info
->dwProvType
, 0);
5605 for (i
= 0; i
< info
->cProvParam
; i
++)
5606 CryptSetProvParam(*phProv
, info
->rgProvParam
[i
].dwParam
,
5607 info
->rgProvParam
[i
].pbData
, info
->rgProvParam
[i
].dwFlags
);
5612 static BOOL
export_is_key_exportable(HCRYPTPROV hProv
, DWORD keySpec
)
5617 if ((ret
= CryptGetUserKey(hProv
, keySpec
, &key
)))
5619 DWORD permissions
, size
= sizeof(permissions
);
5621 if ((ret
= CryptGetKeyParam(key
, KP_PERMISSIONS
, (BYTE
*)&permissions
,
5622 &size
, 0)) && !(permissions
& CRYPT_EXPORT
))
5624 CryptDestroyKey(key
);
5629 static LRESULT CALLBACK
export_private_key_dlg_proc(HWND hwnd
, UINT msg
,
5630 WPARAM wp
, LPARAM lp
)
5633 struct ExportWizData
*data
;
5639 PROPSHEETPAGEW
*page
= (PROPSHEETPAGEW
*)lp
;
5640 PCRYPT_KEY_PROV_INFO info
;
5641 HCRYPTPROV hProv
= 0;
5644 data
= (struct ExportWizData
*)page
->lParam
;
5645 SetWindowLongPtrW(hwnd
, DWLP_USER
, (LPARAM
)data
);
5646 /* Get enough information about a key to see whether it's exportable.
5648 if (!(info
= export_get_private_key_info(
5649 data
->exportInfo
.u
.pCertContext
)))
5650 errorID
= IDS_EXPORT_PRIVATE_KEY_UNAVAILABLE
;
5651 else if (!export_acquire_private_key(info
, &hProv
))
5652 errorID
= IDS_EXPORT_PRIVATE_KEY_UNAVAILABLE
;
5653 else if (!export_is_key_exportable(hProv
, info
->dwKeySpec
))
5654 errorID
= IDS_EXPORT_PRIVATE_KEY_NON_EXPORTABLE
;
5658 WCHAR error
[MAX_STRING_LEN
];
5660 LoadStringW(hInstance
, errorID
, error
,
5661 sizeof(error
) / sizeof(error
[0]));
5662 SendMessageW(GetDlgItem(hwnd
, IDC_EXPORT_PRIVATE_KEY_UNAVAILABLE
),
5663 WM_SETTEXT
, 0, (LPARAM
)error
);
5664 EnableWindow(GetDlgItem(hwnd
, IDC_EXPORT_PRIVATE_KEY_YES
), FALSE
);
5667 data
->keyProvInfo
= info
;
5669 CryptReleaseContext(hProv
, 0);
5670 SendMessageW(GetDlgItem(hwnd
, IDC_EXPORT_PRIVATE_KEY_NO
), BM_CLICK
,
5676 NMHDR
*hdr
= (NMHDR
*)lp
;
5681 PostMessageW(GetParent(hwnd
), PSM_SETWIZBUTTONS
, 0,
5682 PSWIZB_BACK
| PSWIZB_NEXT
);
5686 data
= (struct ExportWizData
*)GetWindowLongPtrW(hwnd
, DWLP_USER
);
5687 if (IsDlgButtonChecked(hwnd
, IDC_EXPORT_PRIVATE_KEY_NO
))
5689 data
->contextInfo
.dwExportFormat
=
5690 CRYPTUI_WIZ_EXPORT_FORMAT_DER
;
5691 data
->contextInfo
.fExportPrivateKeys
= FALSE
;
5695 data
->contextInfo
.dwExportFormat
=
5696 CRYPTUI_WIZ_EXPORT_FORMAT_PFX
;
5697 data
->contextInfo
.fExportPrivateKeys
= TRUE
;
5707 static BOOL
export_info_has_private_key(PCCRYPTUI_WIZ_EXPORT_INFO pExportInfo
)
5711 if (pExportInfo
->dwSubjectChoice
== CRYPTUI_WIZ_EXPORT_CERT_CONTEXT
)
5715 /* If there's a CRYPT_KEY_PROV_INFO set for this cert, assume the
5716 * cert has a private key.
5718 if (CertGetCertificateContextProperty(pExportInfo
->u
.pCertContext
,
5719 CERT_KEY_PROV_INFO_PROP_ID
, NULL
, &size
))
5725 static void export_format_enable_controls(HWND hwnd
, struct ExportWizData
*data
)
5727 int defaultFormatID
;
5729 switch (data
->contextInfo
.dwExportFormat
)
5731 case CRYPTUI_WIZ_EXPORT_FORMAT_BASE64
:
5732 defaultFormatID
= IDC_EXPORT_FORMAT_BASE64
;
5734 case CRYPTUI_WIZ_EXPORT_FORMAT_PKCS7
:
5735 defaultFormatID
= IDC_EXPORT_FORMAT_CMS
;
5737 case CRYPTUI_WIZ_EXPORT_FORMAT_PFX
:
5738 defaultFormatID
= IDC_EXPORT_FORMAT_PFX
;
5741 defaultFormatID
= IDC_EXPORT_FORMAT_DER
;
5743 SendMessageW(GetDlgItem(hwnd
, defaultFormatID
), BM_CLICK
, 0, 0);
5744 if (defaultFormatID
== IDC_EXPORT_FORMAT_PFX
)
5746 EnableWindow(GetDlgItem(hwnd
, IDC_EXPORT_FORMAT_DER
), FALSE
);
5747 EnableWindow(GetDlgItem(hwnd
, IDC_EXPORT_FORMAT_BASE64
), FALSE
);
5748 EnableWindow(GetDlgItem(hwnd
, IDC_EXPORT_FORMAT_CMS
), FALSE
);
5749 EnableWindow(GetDlgItem(hwnd
, IDC_EXPORT_FORMAT_PFX
), TRUE
);
5753 EnableWindow(GetDlgItem(hwnd
, IDC_EXPORT_FORMAT_DER
), TRUE
);
5754 EnableWindow(GetDlgItem(hwnd
, IDC_EXPORT_FORMAT_BASE64
), TRUE
);
5755 EnableWindow(GetDlgItem(hwnd
, IDC_EXPORT_FORMAT_CMS
), TRUE
);
5756 EnableWindow(GetDlgItem(hwnd
, IDC_EXPORT_FORMAT_PFX
), FALSE
);
5760 static LRESULT CALLBACK
export_format_dlg_proc(HWND hwnd
, UINT msg
, WPARAM wp
,
5764 struct ExportWizData
*data
;
5770 PROPSHEETPAGEW
*page
= (PROPSHEETPAGEW
*)lp
;
5772 data
= (struct ExportWizData
*)page
->lParam
;
5773 SetWindowLongPtrW(hwnd
, DWLP_USER
, (LPARAM
)data
);
5774 export_format_enable_controls(hwnd
, data
);
5779 NMHDR
*hdr
= (NMHDR
*)lp
;
5784 PostMessageW(GetParent(hwnd
), PSM_SETWIZBUTTONS
, 0,
5785 PSWIZB_BACK
| PSWIZB_NEXT
);
5786 data
= (struct ExportWizData
*)GetWindowLongPtrW(hwnd
, DWLP_USER
);
5787 export_format_enable_controls(hwnd
, data
);
5792 BOOL skipPasswordPage
= TRUE
;
5794 data
= (struct ExportWizData
*)GetWindowLongPtrW(hwnd
, DWLP_USER
);
5795 if (IsDlgButtonChecked(hwnd
, IDC_EXPORT_FORMAT_DER
))
5796 data
->contextInfo
.dwExportFormat
=
5797 CRYPTUI_WIZ_EXPORT_FORMAT_DER
;
5798 else if (IsDlgButtonChecked(hwnd
, IDC_EXPORT_FORMAT_BASE64
))
5799 data
->contextInfo
.dwExportFormat
=
5800 CRYPTUI_WIZ_EXPORT_FORMAT_BASE64
;
5801 else if (IsDlgButtonChecked(hwnd
, IDC_EXPORT_FORMAT_CMS
))
5803 data
->contextInfo
.dwExportFormat
=
5804 CRYPTUI_WIZ_EXPORT_FORMAT_PKCS7
;
5805 if (IsDlgButtonChecked(hwnd
, IDC_EXPORT_CMS_INCLUDE_CHAIN
))
5806 data
->contextInfo
.fExportChain
=
5807 CRYPTUI_WIZ_EXPORT_FORMAT_PKCS7
;
5811 data
->contextInfo
.dwExportFormat
=
5812 CRYPTUI_WIZ_EXPORT_FORMAT_PFX
;
5813 if (IsDlgButtonChecked(hwnd
, IDC_EXPORT_PFX_INCLUDE_CHAIN
))
5814 data
->contextInfo
.fExportChain
= TRUE
;
5815 if (IsDlgButtonChecked(hwnd
, IDC_EXPORT_PFX_STRONG_ENCRYPTION
))
5816 data
->contextInfo
.fStrongEncryption
= TRUE
;
5817 if (IsDlgButtonChecked(hwnd
, IDC_EXPORT_PFX_DELETE_PRIVATE_KEY
))
5818 data
->deleteKeys
= TRUE
;
5819 skipPasswordPage
= FALSE
;
5821 SetWindowLongPtrW(hwnd
, DWLP_MSGRESULT
,
5822 skipPasswordPage
? IDD_EXPORT_FILE
: 0);
5835 case IDC_EXPORT_FORMAT_DER
:
5836 case IDC_EXPORT_FORMAT_BASE64
:
5837 EnableWindow(GetDlgItem(hwnd
, IDC_EXPORT_CMS_INCLUDE_CHAIN
),
5839 EnableWindow(GetDlgItem(hwnd
, IDC_EXPORT_PFX_INCLUDE_CHAIN
),
5841 EnableWindow(GetDlgItem(hwnd
, IDC_EXPORT_PFX_STRONG_ENCRYPTION
),
5843 EnableWindow(GetDlgItem(hwnd
,
5844 IDC_EXPORT_PFX_DELETE_PRIVATE_KEY
), FALSE
);
5846 case IDC_EXPORT_FORMAT_CMS
:
5847 EnableWindow(GetDlgItem(hwnd
, IDC_EXPORT_CMS_INCLUDE_CHAIN
),
5850 case IDC_EXPORT_FORMAT_PFX
:
5851 EnableWindow(GetDlgItem(hwnd
, IDC_EXPORT_PFX_INCLUDE_CHAIN
),
5853 EnableWindow(GetDlgItem(hwnd
, IDC_EXPORT_PFX_STRONG_ENCRYPTION
),
5855 EnableWindow(GetDlgItem(hwnd
,
5856 IDC_EXPORT_PFX_DELETE_PRIVATE_KEY
), TRUE
);
5866 static void export_password_mismatch(HWND hwnd
, struct ExportWizData
*data
)
5868 WCHAR title
[MAX_STRING_LEN
], error
[MAX_STRING_LEN
];
5871 if (data
->pwszWizardTitle
)
5872 pTitle
= data
->pwszWizardTitle
;
5875 LoadStringW(hInstance
, IDS_EXPORT_WIZARD
, title
,
5876 sizeof(title
) / sizeof(title
[0]));
5879 LoadStringW(hInstance
, IDS_EXPORT_PASSWORD_MISMATCH
, error
,
5880 sizeof(error
) / sizeof(error
[0]));
5881 MessageBoxW(hwnd
, error
, pTitle
, MB_ICONERROR
| MB_OK
);
5882 SetFocus(GetDlgItem(hwnd
, IDC_EXPORT_PASSWORD
));
5885 static LRESULT CALLBACK
export_password_dlg_proc(HWND hwnd
, UINT msg
,
5886 WPARAM wp
, LPARAM lp
)
5889 struct ExportWizData
*data
;
5895 PROPSHEETPAGEW
*page
= (PROPSHEETPAGEW
*)lp
;
5897 data
= (struct ExportWizData
*)page
->lParam
;
5898 SetWindowLongPtrW(hwnd
, DWLP_USER
, (LPARAM
)data
);
5903 NMHDR
*hdr
= (NMHDR
*)lp
;
5908 PostMessageW(GetParent(hwnd
), PSM_SETWIZBUTTONS
, 0,
5909 PSWIZB_BACK
| PSWIZB_NEXT
);
5914 HWND passwordEdit
= GetDlgItem(hwnd
, IDC_EXPORT_PASSWORD
);
5915 HWND passwordConfirmEdit
= GetDlgItem(hwnd
,
5916 IDC_EXPORT_PASSWORD_CONFIRM
);
5917 DWORD passwordLen
= SendMessageW(passwordEdit
, WM_GETTEXTLENGTH
,
5919 DWORD passwordConfirmLen
= SendMessageW(passwordConfirmEdit
,
5920 WM_GETTEXTLENGTH
, 0, 0);
5922 data
= (struct ExportWizData
*)GetWindowLongPtrW(hwnd
, DWLP_USER
);
5923 if (!passwordLen
&& !passwordConfirmLen
)
5924 data
->contextInfo
.pwszPassword
= NULL
;
5925 else if (passwordLen
!= passwordConfirmLen
)
5927 export_password_mismatch(hwnd
, data
);
5928 SetWindowLongPtrW(hwnd
, DWLP_MSGRESULT
, 1);
5933 LPWSTR password
= HeapAlloc(GetProcessHeap(), 0,
5934 (passwordLen
+ 1) * sizeof(WCHAR
));
5935 LPWSTR passwordConfirm
= HeapAlloc(GetProcessHeap(), 0,
5936 (passwordConfirmLen
+ 1) * sizeof(WCHAR
));
5937 BOOL freePassword
= TRUE
;
5939 if (password
&& passwordConfirm
)
5941 SendMessageW(passwordEdit
, WM_GETTEXT
, passwordLen
+ 1,
5943 SendMessageW(passwordConfirmEdit
, WM_GETTEXT
,
5944 passwordConfirmLen
+ 1, (LPARAM
)passwordConfirm
);
5945 if (strcmpW(password
, passwordConfirm
))
5947 export_password_mismatch(hwnd
, data
);
5948 SetWindowLongPtrW(hwnd
, DWLP_MSGRESULT
, 1);
5953 data
->contextInfo
.pwszPassword
= password
;
5954 freePassword
= FALSE
;
5955 data
->freePassword
= TRUE
;
5959 HeapFree(GetProcessHeap(), 0, password
);
5960 HeapFree(GetProcessHeap(), 0, passwordConfirm
);
5971 static LPWSTR
export_append_extension(struct ExportWizData
*data
,
5974 static const WCHAR cer
[] = { '.','c','e','r',0 };
5975 static const WCHAR crl
[] = { '.','c','r','l',0 };
5976 static const WCHAR ctl
[] = { '.','c','t','l',0 };
5977 static const WCHAR p7b
[] = { '.','p','7','b',0 };
5978 static const WCHAR pfx
[] = { '.','p','f','x',0 };
5979 static const WCHAR sst
[] = { '.','s','s','t',0 };
5982 BOOL appendExtension
;
5984 switch (data
->contextInfo
.dwExportFormat
)
5986 case CRYPTUI_WIZ_EXPORT_FORMAT_PKCS7
:
5989 case CRYPTUI_WIZ_EXPORT_FORMAT_PFX
:
5993 switch (data
->exportInfo
.dwSubjectChoice
)
5995 case CRYPTUI_WIZ_EXPORT_CRL_CONTEXT
:
5998 case CRYPTUI_WIZ_EXPORT_CTL_CONTEXT
:
6001 case CRYPTUI_WIZ_EXPORT_CERT_STORE
:
6008 dot
= strrchrW(fileName
, '.');
6010 appendExtension
= strcmpiW(dot
, extension
) != 0;
6012 appendExtension
= TRUE
;
6013 if (appendExtension
)
6015 fileName
= HeapReAlloc(GetProcessHeap(), 0, fileName
,
6016 (strlenW(fileName
) + strlenW(extension
) + 1) * sizeof(WCHAR
));
6018 strcatW(fileName
, extension
);
6023 static BOOL
export_validate_filename(HWND hwnd
, struct ExportWizData
*data
,
6027 BOOL tryCreate
= TRUE
, forceCreate
= FALSE
, ret
= FALSE
;
6029 file
= CreateFileW(fileName
, GENERIC_WRITE
,
6030 FILE_SHARE_READ
| FILE_SHARE_WRITE
, NULL
, OPEN_EXISTING
, 0, NULL
);
6031 if (file
!= INVALID_HANDLE_VALUE
)
6033 WCHAR warning
[MAX_STRING_LEN
], title
[MAX_STRING_LEN
];
6036 if (data
->pwszWizardTitle
)
6037 pTitle
= data
->pwszWizardTitle
;
6040 LoadStringW(hInstance
, IDS_EXPORT_WIZARD
, title
,
6041 sizeof(title
) / sizeof(title
[0]));
6044 LoadStringW(hInstance
, IDS_EXPORT_FILE_EXISTS
, warning
,
6045 sizeof(warning
) / sizeof(warning
[0]));
6046 if (MessageBoxW(hwnd
, warning
, pTitle
, MB_YESNO
) == IDYES
)
6054 file
= CreateFileW(fileName
, GENERIC_WRITE
,
6055 FILE_SHARE_READ
| FILE_SHARE_WRITE
, NULL
,
6056 forceCreate
? CREATE_ALWAYS
: CREATE_NEW
,
6058 if (file
!= INVALID_HANDLE_VALUE
)
6065 WCHAR title
[MAX_STRING_LEN
], error
[MAX_STRING_LEN
];
6067 LPWSTR msgBuf
, fullError
;
6069 if (data
->pwszWizardTitle
)
6070 pTitle
= data
->pwszWizardTitle
;
6073 LoadStringW(hInstance
, IDS_EXPORT_WIZARD
, title
,
6074 sizeof(title
) / sizeof(title
[0]));
6077 LoadStringW(hInstance
, IDS_IMPORT_OPEN_FAILED
, error
,
6078 sizeof(error
) / sizeof(error
[0]));
6080 FORMAT_MESSAGE_ALLOCATE_BUFFER
| FORMAT_MESSAGE_FROM_SYSTEM
, NULL
,
6081 GetLastError(), 0, (LPWSTR
) &msgBuf
, 0, NULL
);
6082 fullError
= HeapAlloc(GetProcessHeap(), 0,
6083 (strlenW(error
) + strlenW(fileName
) + strlenW(msgBuf
) + 3)
6087 LPWSTR ptr
= fullError
;
6089 strcpyW(ptr
, error
);
6090 ptr
+= strlenW(error
);
6091 strcpyW(ptr
, fileName
);
6092 ptr
+= strlenW(fileName
);
6095 strcpyW(ptr
, msgBuf
);
6096 MessageBoxW(hwnd
, fullError
, pTitle
, MB_ICONERROR
| MB_OK
);
6097 HeapFree(GetProcessHeap(), 0, fullError
);
6105 static const WCHAR export_filter_cert
[] = { '*','.','c','e','r',0 };
6106 static const WCHAR export_filter_crl
[] = { '*','.','c','r','l',0 };
6107 static const WCHAR export_filter_ctl
[] = { '*','.','s','t','l',0 };
6108 static const WCHAR export_filter_cms
[] = { '*','.','p','7','b',0 };
6109 static const WCHAR export_filter_pfx
[] = { '*','.','p','f','x',0 };
6110 static const WCHAR export_filter_sst
[] = { '*','.','s','s','t',0 };
6112 static WCHAR
*make_export_file_filter(DWORD exportFormat
, DWORD subjectChoice
)
6114 int baseLen
, allLen
, totalLen
= 2, baseID
;
6115 LPWSTR filter
= NULL
, baseFilter
, all
;
6118 switch (exportFormat
)
6120 case CRYPTUI_WIZ_EXPORT_FORMAT_BASE64
:
6121 baseID
= IDS_EXPORT_FILTER_BASE64_CERT
;
6122 filterStr
= export_filter_cert
;
6124 case CRYPTUI_WIZ_EXPORT_FORMAT_PFX
:
6125 baseID
= IDS_EXPORT_FILTER_PFX
;
6126 filterStr
= export_filter_pfx
;
6128 case CRYPTUI_WIZ_EXPORT_FORMAT_PKCS7
:
6129 baseID
= IDS_EXPORT_FILTER_CMS
;
6130 filterStr
= export_filter_cms
;
6133 switch (subjectChoice
)
6135 case CRYPTUI_WIZ_EXPORT_CRL_CONTEXT
:
6136 baseID
= IDS_EXPORT_FILTER_CRL
;
6137 filterStr
= export_filter_crl
;
6139 case CRYPTUI_WIZ_EXPORT_CTL_CONTEXT
:
6140 baseID
= IDS_EXPORT_FILTER_CTL
;
6141 filterStr
= export_filter_ctl
;
6143 case CRYPTUI_WIZ_EXPORT_CERT_STORE
:
6144 baseID
= IDS_EXPORT_FILTER_SERIALIZED_CERT_STORE
;
6145 filterStr
= export_filter_sst
;
6148 baseID
= IDS_EXPORT_FILTER_CERT
;
6149 filterStr
= export_filter_cert
;
6153 baseLen
= LoadStringW(hInstance
, baseID
, (LPWSTR
)&baseFilter
, 0);
6154 totalLen
+= baseLen
+ strlenW(filterStr
) + 2;
6155 allLen
= LoadStringW(hInstance
, IDS_IMPORT_FILTER_ALL
, (LPWSTR
)&all
, 0);
6156 totalLen
+= allLen
+ strlenW(filter_all
) + 2;
6157 filter
= HeapAlloc(GetProcessHeap(), 0, totalLen
* sizeof(WCHAR
));
6163 memcpy(ptr
, baseFilter
, baseLen
* sizeof(WCHAR
));
6166 strcpyW(ptr
, filterStr
);
6167 ptr
+= strlenW(filterStr
) + 1;
6168 memcpy(ptr
, all
, allLen
* sizeof(WCHAR
));
6171 strcpyW(ptr
, filter_all
);
6172 ptr
+= strlenW(filter_all
) + 1;
6178 static LRESULT CALLBACK
export_file_dlg_proc(HWND hwnd
, UINT msg
, WPARAM wp
,
6182 struct ExportWizData
*data
;
6188 PROPSHEETPAGEW
*page
= (PROPSHEETPAGEW
*)lp
;
6190 data
= (struct ExportWizData
*)page
->lParam
;
6191 SetWindowLongPtrW(hwnd
, DWLP_USER
, (LPARAM
)data
);
6192 if (data
->exportInfo
.pwszExportFileName
)
6193 SendMessageW(GetDlgItem(hwnd
, IDC_EXPORT_FILENAME
), WM_SETTEXT
, 0,
6194 (LPARAM
)data
->exportInfo
.pwszExportFileName
);
6199 NMHDR
*hdr
= (NMHDR
*)lp
;
6204 data
= (struct ExportWizData
*)GetWindowLongPtrW(hwnd
, DWLP_USER
);
6205 if (data
->contextInfo
.dwExportFormat
!=
6206 CRYPTUI_WIZ_EXPORT_FORMAT_PFX
)
6208 SetWindowLongPtrW(hwnd
, DWLP_MSGRESULT
, IDD_EXPORT_FORMAT
);
6214 HWND fileNameEdit
= GetDlgItem(hwnd
, IDC_EXPORT_FILENAME
);
6215 DWORD len
= SendMessageW(fileNameEdit
, WM_GETTEXTLENGTH
, 0, 0);
6217 data
= (struct ExportWizData
*)GetWindowLongPtrW(hwnd
, DWLP_USER
);
6220 WCHAR title
[MAX_STRING_LEN
], error
[MAX_STRING_LEN
];
6223 if (data
->pwszWizardTitle
)
6224 pTitle
= data
->pwszWizardTitle
;
6227 LoadStringW(hInstance
, IDS_EXPORT_WIZARD
, title
,
6228 sizeof(title
) / sizeof(title
[0]));
6231 LoadStringW(hInstance
, IDS_IMPORT_EMPTY_FILE
, error
,
6232 sizeof(error
) / sizeof(error
[0]));
6233 MessageBoxW(hwnd
, error
, pTitle
, MB_ICONERROR
| MB_OK
);
6234 SetWindowLongPtrW(hwnd
, DWLP_MSGRESULT
, 1);
6239 LPWSTR fileName
= HeapAlloc(GetProcessHeap(), 0,
6240 (len
+ 1) * sizeof(WCHAR
));
6244 SendMessageW(fileNameEdit
, WM_GETTEXT
, len
+ 1,
6246 fileName
= export_append_extension(data
, fileName
);
6247 if (!export_validate_filename(hwnd
, data
, fileName
))
6249 HeapFree(GetProcessHeap(), 0, fileName
);
6250 SetWindowLongPtrW(hwnd
, DWLP_MSGRESULT
, 1);
6254 data
->fileName
= fileName
;
6260 PostMessageW(GetParent(hwnd
), PSM_SETWIZBUTTONS
, 0,
6261 PSWIZB_BACK
| PSWIZB_NEXT
);
6270 case IDC_EXPORT_BROWSE_FILE
:
6273 WCHAR fileBuf
[MAX_PATH
];
6275 data
= (struct ExportWizData
*)GetWindowLongPtrW(hwnd
, DWLP_USER
);
6276 memset(&ofn
, 0, sizeof(ofn
));
6277 ofn
.lStructSize
= sizeof(ofn
);
6278 ofn
.hwndOwner
= hwnd
;
6279 ofn
.lpstrFilter
= make_export_file_filter(
6280 data
->contextInfo
.dwExportFormat
,
6281 data
->exportInfo
.dwSubjectChoice
);
6282 ofn
.lpstrFile
= fileBuf
;
6283 ofn
.nMaxFile
= sizeof(fileBuf
) / sizeof(fileBuf
[0]);
6285 if (GetSaveFileNameW(&ofn
))
6286 SendMessageW(GetDlgItem(hwnd
, IDC_EXPORT_FILENAME
), WM_SETTEXT
,
6287 0, (LPARAM
)ofn
.lpstrFile
);
6288 HeapFree(GetProcessHeap(), 0, (LPWSTR
)ofn
.lpstrFilter
);
6297 static void show_export_details(HWND lv
, struct ExportWizData
*data
)
6299 WCHAR text
[MAX_STRING_LEN
];
6303 item
.mask
= LVIF_TEXT
;
6306 item
.iItem
= SendMessageW(lv
, LVM_GETITEMCOUNT
, 0, 0);
6308 LoadStringW(hInstance
, IDS_IMPORT_FILE
, text
,
6309 sizeof(text
)/ sizeof(text
[0]));
6310 item
.pszText
= text
;
6311 SendMessageW(lv
, LVM_INSERTITEMW
, 0, (LPARAM
)&item
);
6313 item
.pszText
= data
->fileName
;
6314 SendMessageW(lv
, LVM_SETITEMTEXTW
, item
.iItem
, (LPARAM
)&item
);
6317 item
.pszText
= text
;
6318 switch (data
->exportInfo
.dwSubjectChoice
)
6320 case CRYPTUI_WIZ_EXPORT_CRL_CONTEXT
:
6321 case CRYPTUI_WIZ_EXPORT_CTL_CONTEXT
:
6322 case CRYPTUI_WIZ_EXPORT_CERT_STORE
:
6323 case CRYPTUI_WIZ_EXPORT_CERT_STORE_CERTIFICATES_ONLY
:
6328 item
.iItem
= SendMessageW(lv
, LVM_GETITEMCOUNT
, 0, 0);
6330 LoadStringW(hInstance
, IDS_EXPORT_INCLUDE_CHAIN
, text
,
6331 sizeof(text
) / sizeof(text
[0]));
6332 SendMessageW(lv
, LVM_INSERTITEMW
, item
.iItem
, (LPARAM
)&item
);
6334 LoadStringW(hInstance
,
6335 data
->contextInfo
.fExportChain
? IDS_YES
: IDS_NO
, text
,
6336 sizeof(text
) / sizeof(text
[0]));
6337 SendMessageW(lv
, LVM_SETITEMTEXTW
, item
.iItem
, (LPARAM
)&item
);
6339 item
.iItem
= SendMessageW(lv
, LVM_GETITEMCOUNT
, 0, 0);
6341 LoadStringW(hInstance
, IDS_EXPORT_KEYS
, text
,
6342 sizeof(text
) / sizeof(text
[0]));
6343 SendMessageW(lv
, LVM_INSERTITEMW
, item
.iItem
, (LPARAM
)&item
);
6345 LoadStringW(hInstance
,
6346 data
->contextInfo
.fExportPrivateKeys
? IDS_YES
: IDS_NO
, text
,
6347 sizeof(text
) / sizeof(text
[0]));
6348 SendMessageW(lv
, LVM_SETITEMTEXTW
, item
.iItem
, (LPARAM
)&item
);
6352 item
.iItem
= SendMessageW(lv
, LVM_GETITEMCOUNT
, 0, 0);
6354 LoadStringW(hInstance
, IDS_EXPORT_FORMAT
, text
,
6355 sizeof(text
)/ sizeof(text
[0]));
6356 SendMessageW(lv
, LVM_INSERTITEMW
, 0, (LPARAM
)&item
);
6359 switch (data
->exportInfo
.dwSubjectChoice
)
6361 case CRYPTUI_WIZ_EXPORT_CRL_CONTEXT
:
6362 contentID
= IDS_EXPORT_FILTER_CRL
;
6364 case CRYPTUI_WIZ_EXPORT_CTL_CONTEXT
:
6365 contentID
= IDS_EXPORT_FILTER_CTL
;
6367 case CRYPTUI_WIZ_EXPORT_CERT_STORE
:
6368 contentID
= IDS_EXPORT_FILTER_SERIALIZED_CERT_STORE
;
6371 switch (data
->contextInfo
.dwExportFormat
)
6373 case CRYPTUI_WIZ_EXPORT_FORMAT_BASE64
:
6374 contentID
= IDS_EXPORT_FILTER_BASE64_CERT
;
6376 case CRYPTUI_WIZ_EXPORT_FORMAT_PKCS7
:
6377 contentID
= IDS_EXPORT_FILTER_CMS
;
6379 case CRYPTUI_WIZ_EXPORT_FORMAT_PFX
:
6380 contentID
= IDS_EXPORT_FILTER_PFX
;
6383 contentID
= IDS_EXPORT_FILTER_CERT
;
6386 LoadStringW(hInstance
, contentID
, text
, sizeof(text
) / sizeof(text
[0]));
6387 SendMessageW(lv
, LVM_SETITEMTEXTW
, item
.iItem
, (LPARAM
)&item
);
6390 static inline BOOL
save_der(HANDLE file
, const BYTE
*pb
, DWORD cb
)
6394 return WriteFile(file
, pb
, cb
, &bytesWritten
, NULL
);
6397 static BOOL
save_base64(HANDLE file
, const BYTE
*pb
, DWORD cb
)
6402 if ((ret
= CryptBinaryToStringA(pb
, cb
, CRYPT_STRING_BASE64
, NULL
, &size
)))
6404 LPSTR buf
= HeapAlloc(GetProcessHeap(), 0, size
);
6408 if ((ret
= CryptBinaryToStringA(pb
, cb
, CRYPT_STRING_BASE64
, buf
,
6410 ret
= WriteFile(file
, buf
, size
, &size
, NULL
);
6411 HeapFree(GetProcessHeap(), 0, buf
);
6415 SetLastError(ERROR_OUTOFMEMORY
);
6422 static inline BOOL
save_store_as_cms(HANDLE file
, HCERTSTORE store
)
6424 return CertSaveStore(store
, PKCS_7_ASN_ENCODING
| X509_ASN_ENCODING
,
6425 CERT_STORE_SAVE_AS_PKCS7
, CERT_STORE_SAVE_TO_FILE
, file
, 0);
6428 static BOOL
save_cert_as_cms(HANDLE file
, PCCRYPTUI_WIZ_EXPORT_INFO pExportInfo
,
6432 HCERTSTORE store
= CertOpenStore(CERT_STORE_PROV_MEMORY
, 0, 0,
6433 CERT_STORE_CREATE_NEW_FLAG
, NULL
);
6439 HCERTSTORE addlStore
= CertOpenStore(CERT_STORE_PROV_COLLECTION
,
6440 0, 0, CERT_STORE_CREATE_NEW_FLAG
, NULL
);
6447 for (i
= 0; ret
&& i
< pExportInfo
->cStores
; i
++)
6448 ret
= CertAddStoreToCollection(addlStore
,
6449 pExportInfo
->rghStores
, 0, 0);
6452 PCCERT_CHAIN_CONTEXT chain
;
6454 ret
= CertGetCertificateChain(NULL
,
6455 pExportInfo
->u
.pCertContext
, NULL
, addlStore
, NULL
, 0,
6461 for (i
= 0; ret
&& i
< chain
->cChain
; i
++)
6462 for (j
= 0; ret
&& j
< chain
->rgpChain
[i
]->cElement
;
6464 ret
= CertAddCertificateContextToStore(store
,
6465 chain
->rgpChain
[i
]->rgpElement
[j
]->pCertContext
,
6466 CERT_STORE_ADD_ALWAYS
, NULL
);
6467 CertFreeCertificateChain(chain
);
6471 /* No chain could be created, just add the individual
6472 * cert to the message.
6474 ret
= CertAddCertificateContextToStore(store
,
6475 pExportInfo
->u
.pCertContext
, CERT_STORE_ADD_ALWAYS
,
6479 CertCloseStore(addlStore
, 0);
6485 ret
= CertAddCertificateContextToStore(store
,
6486 pExportInfo
->u
.pCertContext
, CERT_STORE_ADD_ALWAYS
, NULL
);
6488 ret
= save_store_as_cms(file
, store
);
6489 CertCloseStore(store
, 0);
6496 static BOOL
save_serialized_store(HANDLE file
, HCERTSTORE store
)
6498 return CertSaveStore(store
, PKCS_7_ASN_ENCODING
| X509_ASN_ENCODING
,
6499 CERT_STORE_SAVE_AS_STORE
, CERT_STORE_SAVE_TO_FILE
, file
, 0);
6502 static BOOL
save_pfx(HANDLE file
, PCCRYPTUI_WIZ_EXPORT_INFO pExportInfo
,
6503 PCCRYPTUI_WIZ_EXPORT_CERTCONTEXT_INFO pContextInfo
,
6504 PCRYPT_KEY_PROV_INFO keyProvInfo
, BOOL deleteKeys
)
6506 HCERTSTORE store
= CertOpenStore(CERT_STORE_PROV_MEMORY
, X509_ASN_ENCODING
,
6507 0, CERT_STORE_CREATE_NEW_FLAG
, NULL
);
6512 CRYPT_DATA_BLOB pfxBlob
= { 0, NULL
};
6513 PCCERT_CONTEXT cert
= NULL
;
6514 BOOL freeKeyProvInfo
= FALSE
;
6516 if (pContextInfo
->fExportChain
)
6518 HCERTCHAINENGINE engine
= NULL
;
6520 if (pExportInfo
->cStores
)
6522 CERT_CHAIN_ENGINE_CONFIG config
;
6524 memset(&config
, 0, sizeof(config
));
6525 config
.cbSize
= sizeof(config
);
6526 config
.cAdditionalStore
= pExportInfo
->cStores
;
6527 config
.rghAdditionalStore
= pExportInfo
->rghStores
;
6528 ret
= CertCreateCertificateChainEngine(&config
, &engine
);
6534 CERT_CHAIN_PARA chainPara
;
6535 PCCERT_CHAIN_CONTEXT chain
;
6537 memset(&chainPara
, 0, sizeof(chainPara
));
6538 chainPara
.cbSize
= sizeof(chainPara
);
6539 ret
= CertGetCertificateChain(engine
,
6540 pExportInfo
->u
.pCertContext
, NULL
, NULL
, &chainPara
, 0, NULL
,
6546 for (i
= 0; ret
&& i
< chain
->cChain
; i
++)
6547 for (j
= 0; ret
&& j
< chain
->rgpChain
[i
]->cElement
;
6550 if (i
== 0 && j
== 0)
6551 ret
= CertAddCertificateContextToStore(store
,
6552 chain
->rgpChain
[i
]->rgpElement
[j
]->pCertContext
,
6553 CERT_STORE_ADD_ALWAYS
, &cert
);
6555 ret
= CertAddCertificateContextToStore(store
,
6556 chain
->rgpChain
[i
]->rgpElement
[j
]->pCertContext
,
6557 CERT_STORE_ADD_ALWAYS
, NULL
);
6559 CertFreeCertificateChain(chain
);
6563 CertFreeCertificateChainEngine(engine
);
6566 ret
= CertAddCertificateContextToStore(store
,
6567 pExportInfo
->u
.pCertContext
, CERT_STORE_ADD_ALWAYS
, &cert
);
6568 /* Copy private key info to newly created cert, so it'll get exported
6569 * along with the cert.
6571 if (ret
&& pContextInfo
->fExportPrivateKeys
)
6574 ret
= CertSetCertificateContextProperty(cert
,
6575 CERT_KEY_PROV_INFO_PROP_ID
, 0, keyProvInfo
);
6578 if (!(keyProvInfo
= export_get_private_key_info(cert
)))
6582 ret
= CertSetCertificateContextProperty(cert
,
6583 CERT_KEY_PROV_INFO_PROP_ID
, 0, keyProvInfo
);
6584 freeKeyProvInfo
= TRUE
;
6591 REPORT_NOT_ABLE_TO_EXPORT_PRIVATE_KEY
| EXPORT_PRIVATE_KEYS
;
6593 ret
= PFXExportCertStore(store
, &pfxBlob
,
6594 pContextInfo
->pwszPassword
, exportFlags
);
6597 pfxBlob
.pbData
= HeapAlloc(GetProcessHeap(), 0, pfxBlob
.cbData
);
6600 ret
= PFXExportCertStore(store
, &pfxBlob
,
6601 pContextInfo
->pwszPassword
, exportFlags
);
6606 ret
= WriteFile(file
, pfxBlob
.pbData
, pfxBlob
.cbData
,
6607 &bytesWritten
, NULL
);
6612 SetLastError(ERROR_OUTOFMEMORY
);
6617 if (ret
&& deleteKeys
)
6621 CryptAcquireContextW(&prov
, keyProvInfo
->pwszContainerName
,
6622 keyProvInfo
->pwszProvName
, keyProvInfo
->dwProvType
,
6623 CRYPT_DELETEKEYSET
);
6625 if (freeKeyProvInfo
)
6626 HeapFree(GetProcessHeap(), 0, keyProvInfo
);
6627 CertFreeCertificateContext(cert
);
6628 CertCloseStore(store
, 0);
6633 static BOOL
do_export(HANDLE file
, PCCRYPTUI_WIZ_EXPORT_INFO pExportInfo
,
6634 PCCRYPTUI_WIZ_EXPORT_CERTCONTEXT_INFO pContextInfo
,
6635 PCRYPT_KEY_PROV_INFO keyProvInfo
, BOOL deleteKeys
)
6639 if (pContextInfo
->dwSize
!= sizeof(CRYPTUI_WIZ_EXPORT_CERTCONTEXT_INFO
))
6641 SetLastError(E_INVALIDARG
);
6644 switch (pExportInfo
->dwSubjectChoice
)
6646 case CRYPTUI_WIZ_EXPORT_CRL_CONTEXT
:
6647 ret
= save_der(file
,
6648 pExportInfo
->u
.pCRLContext
->pbCrlEncoded
,
6649 pExportInfo
->u
.pCRLContext
->cbCrlEncoded
);
6651 case CRYPTUI_WIZ_EXPORT_CTL_CONTEXT
:
6652 ret
= save_der(file
,
6653 pExportInfo
->u
.pCTLContext
->pbCtlEncoded
,
6654 pExportInfo
->u
.pCTLContext
->cbCtlEncoded
);
6656 case CRYPTUI_WIZ_EXPORT_CERT_STORE
:
6657 ret
= save_serialized_store(file
, pExportInfo
->u
.hCertStore
);
6659 case CRYPTUI_WIZ_EXPORT_CERT_STORE_CERTIFICATES_ONLY
:
6660 ret
= save_store_as_cms(file
, pExportInfo
->u
.hCertStore
);
6663 switch (pContextInfo
->dwExportFormat
)
6665 case CRYPTUI_WIZ_EXPORT_FORMAT_DER
:
6666 ret
= save_der(file
, pExportInfo
->u
.pCertContext
->pbCertEncoded
,
6667 pExportInfo
->u
.pCertContext
->cbCertEncoded
);
6669 case CRYPTUI_WIZ_EXPORT_FORMAT_BASE64
:
6670 ret
= save_base64(file
,
6671 pExportInfo
->u
.pCertContext
->pbCertEncoded
,
6672 pExportInfo
->u
.pCertContext
->cbCertEncoded
);
6674 case CRYPTUI_WIZ_EXPORT_FORMAT_PKCS7
:
6675 ret
= save_cert_as_cms(file
, pExportInfo
,
6676 pContextInfo
->fExportChain
);
6678 case CRYPTUI_WIZ_EXPORT_FORMAT_PFX
:
6679 ret
= save_pfx(file
, pExportInfo
, pContextInfo
, keyProvInfo
,
6683 SetLastError(E_FAIL
);
6690 static LRESULT CALLBACK
export_finish_dlg_proc(HWND hwnd
, UINT msg
, WPARAM wp
,
6694 struct ExportWizData
*data
;
6700 PROPSHEETPAGEW
*page
= (PROPSHEETPAGEW
*)lp
;
6701 HWND lv
= GetDlgItem(hwnd
, IDC_EXPORT_SETTINGS
);
6705 data
= (struct ExportWizData
*)page
->lParam
;
6706 SetWindowLongPtrW(hwnd
, DWLP_USER
, (LPARAM
)data
);
6707 SendMessageW(GetDlgItem(hwnd
, IDC_EXPORT_TITLE
), WM_SETFONT
,
6708 (WPARAM
)data
->titleFont
, TRUE
);
6709 GetWindowRect(lv
, &rc
);
6710 column
.mask
= LVCF_WIDTH
;
6711 column
.cx
= (rc
.right
- rc
.left
) / 2 - 2;
6712 SendMessageW(lv
, LVM_INSERTCOLUMNW
, 0, (LPARAM
)&column
);
6713 SendMessageW(lv
, LVM_INSERTCOLUMNW
, 1, (LPARAM
)&column
);
6714 show_export_details(lv
, data
);
6719 NMHDR
*hdr
= (NMHDR
*)lp
;
6725 HWND lv
= GetDlgItem(hwnd
, IDC_EXPORT_SETTINGS
);
6727 data
= (struct ExportWizData
*)GetWindowLongPtrW(hwnd
, DWLP_USER
);
6728 SendMessageW(lv
, LVM_DELETEALLITEMS
, 0, 0);
6729 show_export_details(lv
, data
);
6730 PostMessageW(GetParent(hwnd
), PSM_SETWIZBUTTONS
, 0,
6731 PSWIZB_BACK
| PSWIZB_FINISH
);
6738 WCHAR title
[MAX_STRING_LEN
], message
[MAX_STRING_LEN
];
6742 data
= (struct ExportWizData
*)GetWindowLongPtrW(hwnd
, DWLP_USER
);
6743 if ((data
->success
= do_export(data
->file
, &data
->exportInfo
,
6744 &data
->contextInfo
, data
->keyProvInfo
, data
->deleteKeys
)))
6746 messageID
= IDS_EXPORT_SUCCEEDED
;
6751 messageID
= IDS_EXPORT_FAILED
;
6752 mbFlags
= MB_OK
| MB_ICONERROR
;
6754 if (data
->pwszWizardTitle
)
6755 pTitle
= data
->pwszWizardTitle
;
6758 LoadStringW(hInstance
, IDS_EXPORT_WIZARD
, title
,
6759 sizeof(title
) / sizeof(title
[0]));
6762 LoadStringW(hInstance
, messageID
, message
,
6763 sizeof(message
) / sizeof(message
[0]));
6764 MessageBoxW(hwnd
, message
, pTitle
, mbFlags
);
6774 static BOOL
show_export_ui(DWORD dwFlags
, HWND hwndParent
,
6775 LPCWSTR pwszWizardTitle
, PCCRYPTUI_WIZ_EXPORT_INFO pExportInfo
, void *pvoid
)
6777 PROPSHEETHEADERW hdr
;
6778 PROPSHEETPAGEW pages
[6];
6779 struct ExportWizData data
;
6781 BOOL hasPrivateKey
, showFormatPage
= TRUE
;
6784 data
.dwFlags
= dwFlags
;
6785 data
.pwszWizardTitle
= pwszWizardTitle
;
6786 memset(&data
.exportInfo
, 0, sizeof(data
.exportInfo
));
6787 memcpy(&data
.exportInfo
, pExportInfo
,
6788 min(sizeof(data
.exportInfo
), pExportInfo
->dwSize
));
6789 if (pExportInfo
->dwSize
> sizeof(data
.exportInfo
))
6790 data
.exportInfo
.dwSize
= sizeof(data
.exportInfo
);
6791 data
.contextInfo
.dwSize
= sizeof(data
.contextInfo
);
6792 data
.contextInfo
.dwExportFormat
= CRYPTUI_WIZ_EXPORT_FORMAT_DER
;
6793 data
.contextInfo
.fExportChain
= FALSE
;
6794 data
.contextInfo
.fStrongEncryption
= FALSE
;
6795 data
.contextInfo
.fExportPrivateKeys
= FALSE
;
6796 data
.contextInfo
.pwszPassword
= NULL
;
6797 data
.freePassword
= FALSE
;
6798 if (pExportInfo
->dwSubjectChoice
== CRYPTUI_WIZ_EXPORT_CERT_CONTEXT
&&
6800 memcpy(&data
.contextInfo
, pvoid
,
6801 min(((PCCRYPTUI_WIZ_EXPORT_CERTCONTEXT_INFO
)pvoid
)->dwSize
,
6802 sizeof(data
.contextInfo
)));
6803 data
.keyProvInfo
= NULL
;
6804 data
.deleteKeys
= FALSE
;
6805 data
.fileName
= NULL
;
6806 data
.file
= INVALID_HANDLE_VALUE
;
6807 data
.success
= FALSE
;
6809 memset(&pages
, 0, sizeof(pages
));
6811 pages
[nPages
].dwSize
= sizeof(pages
[0]);
6812 pages
[nPages
].hInstance
= hInstance
;
6813 pages
[nPages
].u
.pszTemplate
= MAKEINTRESOURCEW(IDD_EXPORT_WELCOME
);
6814 pages
[nPages
].pfnDlgProc
= export_welcome_dlg_proc
;
6815 pages
[nPages
].dwFlags
= PSP_HIDEHEADER
;
6816 pages
[nPages
].lParam
= (LPARAM
)&data
;
6819 hasPrivateKey
= export_info_has_private_key(pExportInfo
);
6820 switch (pExportInfo
->dwSubjectChoice
)
6822 case CRYPTUI_WIZ_EXPORT_CRL_CONTEXT
:
6823 case CRYPTUI_WIZ_EXPORT_CTL_CONTEXT
:
6824 showFormatPage
= FALSE
;
6825 data
.contextInfo
.dwExportFormat
= CRYPTUI_WIZ_EXPORT_FORMAT_DER
;
6827 case CRYPTUI_WIZ_EXPORT_CERT_STORE
:
6828 showFormatPage
= FALSE
;
6829 data
.contextInfo
.dwExportFormat
=
6830 CRYPTUI_WIZ_EXPORT_FORMAT_SERIALIZED_CERT_STORE
;
6832 case CRYPTUI_WIZ_EXPORT_CERT_STORE_CERTIFICATES_ONLY
:
6833 showFormatPage
= FALSE
;
6834 data
.contextInfo
.dwExportFormat
= CRYPTUI_WIZ_EXPORT_FORMAT_PKCS7
;
6838 if (hasPrivateKey
&& showFormatPage
)
6840 pages
[nPages
].dwSize
= sizeof(pages
[0]);
6841 pages
[nPages
].hInstance
= hInstance
;
6842 pages
[nPages
].u
.pszTemplate
= MAKEINTRESOURCEW(IDD_EXPORT_PRIVATE_KEY
);
6843 pages
[nPages
].pfnDlgProc
= export_private_key_dlg_proc
;
6844 pages
[nPages
].dwFlags
= PSP_USEHEADERTITLE
| PSP_USEHEADERSUBTITLE
;
6845 pages
[nPages
].pszHeaderTitle
=
6846 MAKEINTRESOURCEW(IDS_EXPORT_PRIVATE_KEY_TITLE
);
6847 pages
[nPages
].pszHeaderSubTitle
=
6848 MAKEINTRESOURCEW(IDS_EXPORT_PRIVATE_KEY_SUBTITLE
);
6849 pages
[nPages
].lParam
= (LPARAM
)&data
;
6854 pages
[nPages
].dwSize
= sizeof(pages
[0]);
6855 pages
[nPages
].hInstance
= hInstance
;
6856 pages
[nPages
].u
.pszTemplate
= MAKEINTRESOURCEW(IDD_EXPORT_FORMAT
);
6857 pages
[nPages
].pfnDlgProc
= export_format_dlg_proc
;
6858 pages
[nPages
].dwFlags
= PSP_USEHEADERTITLE
| PSP_USEHEADERSUBTITLE
;
6859 pages
[nPages
].pszHeaderTitle
=
6860 MAKEINTRESOURCEW(IDS_EXPORT_FORMAT_TITLE
);
6861 pages
[nPages
].pszHeaderSubTitle
=
6862 MAKEINTRESOURCEW(IDS_EXPORT_FORMAT_SUBTITLE
);
6863 pages
[nPages
].lParam
= (LPARAM
)&data
;
6866 if (hasPrivateKey
&& showFormatPage
)
6868 pages
[nPages
].dwSize
= sizeof(pages
[0]);
6869 pages
[nPages
].hInstance
= hInstance
;
6870 pages
[nPages
].u
.pszTemplate
= MAKEINTRESOURCEW(IDD_EXPORT_PASSWORD
);
6871 pages
[nPages
].pfnDlgProc
= export_password_dlg_proc
;
6872 pages
[nPages
].dwFlags
= PSP_USEHEADERTITLE
| PSP_USEHEADERSUBTITLE
;
6873 pages
[nPages
].pszHeaderTitle
=
6874 MAKEINTRESOURCEW(IDS_EXPORT_PASSWORD_TITLE
);
6875 pages
[nPages
].pszHeaderSubTitle
=
6876 MAKEINTRESOURCEW(IDS_EXPORT_PASSWORD_SUBTITLE
);
6877 pages
[nPages
].lParam
= (LPARAM
)&data
;
6881 pages
[nPages
].dwSize
= sizeof(pages
[0]);
6882 pages
[nPages
].hInstance
= hInstance
;
6883 pages
[nPages
].u
.pszTemplate
= MAKEINTRESOURCEW(IDD_EXPORT_FILE
);
6884 pages
[nPages
].pfnDlgProc
= export_file_dlg_proc
;
6885 pages
[nPages
].dwFlags
= PSP_USEHEADERTITLE
| PSP_USEHEADERSUBTITLE
;
6886 pages
[nPages
].pszHeaderTitle
= MAKEINTRESOURCEW(IDS_EXPORT_FILE_TITLE
);
6887 pages
[nPages
].pszHeaderSubTitle
=
6888 MAKEINTRESOURCEW(IDS_EXPORT_FILE_SUBTITLE
);
6889 pages
[nPages
].lParam
= (LPARAM
)&data
;
6892 pages
[nPages
].dwSize
= sizeof(pages
[0]);
6893 pages
[nPages
].hInstance
= hInstance
;
6894 pages
[nPages
].u
.pszTemplate
= MAKEINTRESOURCEW(IDD_EXPORT_FINISH
);
6895 pages
[nPages
].pfnDlgProc
= export_finish_dlg_proc
;
6896 pages
[nPages
].dwFlags
= PSP_HIDEHEADER
;
6897 pages
[nPages
].lParam
= (LPARAM
)&data
;
6900 memset(&hdr
, 0, sizeof(hdr
));
6901 hdr
.dwSize
= sizeof(hdr
);
6902 hdr
.hwndParent
= hwndParent
;
6903 hdr
.dwFlags
= PSH_PROPSHEETPAGE
| PSH_WIZARD97_NEW
| PSH_HEADER
|
6905 hdr
.hInstance
= hInstance
;
6906 if (pwszWizardTitle
)
6907 hdr
.pszCaption
= pwszWizardTitle
;
6909 hdr
.pszCaption
= MAKEINTRESOURCEW(IDS_EXPORT_WIZARD
);
6910 hdr
.u3
.ppsp
= pages
;
6911 hdr
.nPages
= nPages
;
6912 hdr
.u4
.pszbmWatermark
= MAKEINTRESOURCEW(IDB_CERT_WATERMARK
);
6913 hdr
.u5
.pszbmHeader
= MAKEINTRESOURCEW(IDB_CERT_HEADER
);
6914 l
= PropertySheetW(&hdr
);
6915 DeleteObject(data
.titleFont
);
6916 if (data
.freePassword
)
6917 HeapFree(GetProcessHeap(), 0,
6918 (LPWSTR
)data
.contextInfo
.pwszPassword
);
6919 HeapFree(GetProcessHeap(), 0, data
.keyProvInfo
);
6920 CloseHandle(data
.file
);
6921 HeapFree(GetProcessHeap(), 0, data
.fileName
);
6924 SetLastError(ERROR_CANCELLED
);
6928 return data
.success
;
6931 BOOL WINAPI
CryptUIWizExport(DWORD dwFlags
, HWND hwndParent
,
6932 LPCWSTR pwszWizardTitle
, PCCRYPTUI_WIZ_EXPORT_INFO pExportInfo
, void *pvoid
)
6936 TRACE("(%08x, %p, %s, %p, %p)\n", dwFlags
, hwndParent
,
6937 debugstr_w(pwszWizardTitle
), pExportInfo
, pvoid
);
6939 if (!(dwFlags
& CRYPTUI_WIZ_NO_UI
))
6940 ret
= show_export_ui(dwFlags
, hwndParent
, pwszWizardTitle
, pExportInfo
,
6944 HANDLE file
= CreateFileW(pExportInfo
->pwszExportFileName
,
6945 GENERIC_WRITE
, FILE_SHARE_READ
| FILE_SHARE_WRITE
, NULL
,
6946 CREATE_ALWAYS
, 0, NULL
);
6948 if (file
!= INVALID_HANDLE_VALUE
)
6950 ret
= do_export(file
, pExportInfo
, pvoid
, NULL
, FALSE
);