cryptui: Don't crash if a chain can't be created for the viewed cert.
[wine/wine64.git] / dlls / cryptui / main.c
blob169d0cd33dcc996e00d873474f4c43f3d2606b41
1 /*
2 * Copyright 2008 Juan Lang
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2.1 of the License, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with this library; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
19 #include "config.h"
21 #include <stdarg.h>
23 #define COBJMACROS
24 #define NONAMELESSUNION
26 #include "windef.h"
27 #include "winbase.h"
28 #include "winnls.h"
29 #include "winuser.h"
30 #include "softpub.h"
31 #include "wingdi.h"
32 #include "richedit.h"
33 #include "ole2.h"
34 #include "richole.h"
35 #include "commctrl.h"
36 #include "cryptuiapi.h"
37 #include "cryptuires.h"
38 #include "urlmon.h"
39 #include "hlink.h"
40 #include "wine/debug.h"
41 #include "wine/unicode.h"
43 WINE_DEFAULT_DEBUG_CHANNEL(cryptui);
45 static HINSTANCE hInstance;
47 BOOL WINAPI DllMain(HINSTANCE hinstDLL, DWORD fdwReason, LPVOID lpvReserved)
49 TRACE("(0x%p, %d, %p)\n", hinstDLL, fdwReason, lpvReserved);
51 switch (fdwReason)
53 case DLL_WINE_PREATTACH:
54 return FALSE; /* prefer native version */
55 case DLL_PROCESS_ATTACH:
56 hInstance = hinstDLL;
57 DisableThreadLibraryCalls(hinstDLL);
58 break;
59 case DLL_PROCESS_DETACH:
60 break;
61 default:
62 break;
64 return TRUE;
67 /***********************************************************************
68 * CryptUIDlgCertMgr (CRYPTUI.@)
70 BOOL WINAPI CryptUIDlgCertMgr(PCCRYPTUI_CERT_MGR_STRUCT pCryptUICertMgr)
72 FIXME("(%p): stub\n", pCryptUICertMgr);
73 return FALSE;
76 /***********************************************************************
77 * CryptUIDlgViewCertificateA (CRYPTUI.@)
79 BOOL WINAPI CryptUIDlgViewCertificateA(
80 PCCRYPTUI_VIEWCERTIFICATE_STRUCTA pCertViewInfo, BOOL *pfPropertiesChanged)
82 CRYPTUI_VIEWCERTIFICATE_STRUCTW viewInfo;
83 LPWSTR title = NULL;
84 BOOL ret;
86 TRACE("(%p, %p)\n", pCertViewInfo, pfPropertiesChanged);
88 memcpy(&viewInfo, pCertViewInfo, sizeof(viewInfo));
89 if (pCertViewInfo->szTitle)
91 int len = MultiByteToWideChar(CP_ACP, 0, pCertViewInfo->szTitle, -1,
92 NULL, 0);
94 title = HeapAlloc(GetProcessHeap(), 0, len * sizeof(WCHAR));
95 if (title)
97 MultiByteToWideChar(CP_ACP, 0, pCertViewInfo->szTitle, -1, title,
98 len);
99 viewInfo.szTitle = title;
101 else
103 ret = FALSE;
104 goto error;
107 if (pCertViewInfo->cPropSheetPages)
109 FIXME("ignoring additional prop sheet pages\n");
110 viewInfo.cPropSheetPages = 0;
112 ret = CryptUIDlgViewCertificateW(&viewInfo, pfPropertiesChanged);
113 HeapFree(GetProcessHeap(), 0, title);
114 error:
115 return ret;
118 struct ReadStringStruct
120 LPCWSTR buf;
121 LONG pos;
122 LONG len;
125 static DWORD CALLBACK read_text_callback(DWORD_PTR dwCookie, LPBYTE buf,
126 LONG cb, LONG *pcb)
128 struct ReadStringStruct *string = (struct ReadStringStruct *)dwCookie;
129 LONG cch = min(cb / sizeof(WCHAR), string->len - string->pos);
131 TRACE("(%p, %p, %d, %p)\n", string, buf, cb, pcb);
133 memmove(buf, string->buf + string->pos, cch * sizeof(WCHAR));
134 string->pos += cch;
135 *pcb = cch * sizeof(WCHAR);
136 return 0;
139 static void add_unformatted_text_to_control(HWND hwnd, LPCWSTR text, LONG len)
141 struct ReadStringStruct string;
142 EDITSTREAM editstream;
144 TRACE("(%p, %s)\n", hwnd, debugstr_wn(text, len));
146 string.buf = text;
147 string.pos = 0;
148 string.len = len;
149 editstream.dwCookie = (DWORD_PTR)&string;
150 editstream.dwError = 0;
151 editstream.pfnCallback = read_text_callback;
152 SendMessageW(hwnd, EM_STREAMIN, SF_TEXT | SFF_SELECTION | SF_UNICODE,
153 (LPARAM)&editstream);
156 static void add_string_resource_to_control(HWND hwnd, int id)
158 LPWSTR str;
159 LONG len;
161 len = LoadStringW(hInstance, id, (LPWSTR)&str, 0);
162 add_unformatted_text_to_control(hwnd, str, len);
165 static void add_text_with_paraformat_to_control(HWND hwnd, LPCWSTR text,
166 LONG len, const PARAFORMAT2 *fmt)
168 add_unformatted_text_to_control(hwnd, text, len);
169 SendMessageW(hwnd, EM_SETPARAFORMAT, 0, (LPARAM)fmt);
172 static void add_string_resource_with_paraformat_to_control(HWND hwnd, int id,
173 const PARAFORMAT2 *fmt)
175 LPWSTR str;
176 LONG len;
178 len = LoadStringW(hInstance, id, (LPWSTR)&str, 0);
179 add_text_with_paraformat_to_control(hwnd, str, len, fmt);
182 static LPWSTR get_cert_name_string(PCCERT_CONTEXT pCertContext, DWORD dwType,
183 DWORD dwFlags)
185 LPWSTR buf = NULL;
186 DWORD len;
188 len = CertGetNameStringW(pCertContext, dwType, dwFlags, NULL, NULL, 0);
189 if (len)
191 buf = HeapAlloc(GetProcessHeap(), 0, len * sizeof(WCHAR));
192 if (buf)
193 CertGetNameStringW(pCertContext, dwType, dwFlags, NULL, buf, len);
195 return buf;
198 static void add_cert_string_to_control(HWND hwnd, PCCERT_CONTEXT pCertContext,
199 DWORD dwType, DWORD dwFlags)
201 LPWSTR name = get_cert_name_string(pCertContext, dwType, dwFlags);
203 if (name)
205 /* Don't include NULL-terminator in output */
206 DWORD len = lstrlenW(name);
208 add_unformatted_text_to_control(hwnd, name, len);
209 HeapFree(GetProcessHeap(), 0, name);
213 static void add_icon_to_control(HWND hwnd, int id)
215 HRESULT hr;
216 LPRICHEDITOLE richEditOle = NULL;
217 LPOLEOBJECT object = NULL;
218 CLSID clsid;
219 LPOLECACHE oleCache = NULL;
220 FORMATETC formatEtc;
221 DWORD conn;
222 LPDATAOBJECT dataObject = NULL;
223 HBITMAP bitmap = NULL;
224 RECT rect;
225 STGMEDIUM stgm;
226 REOBJECT reObject;
228 TRACE("(%p, %d)\n", hwnd, id);
230 SendMessageW(hwnd, EM_GETOLEINTERFACE, 0, (LPARAM)&richEditOle);
231 if (!richEditOle)
232 goto end;
233 hr = OleCreateDefaultHandler(&CLSID_NULL, NULL, &IID_IOleObject,
234 (void**)&object);
235 if (FAILED(hr))
236 goto end;
237 hr = IOleObject_GetUserClassID(object, &clsid);
238 if (FAILED(hr))
239 goto end;
240 hr = IOleObject_QueryInterface(object, &IID_IOleCache, (void**)&oleCache);
241 if (FAILED(hr))
242 goto end;
243 formatEtc.cfFormat = CF_BITMAP;
244 formatEtc.ptd = NULL;
245 formatEtc.dwAspect = DVASPECT_CONTENT;
246 formatEtc.lindex = -1;
247 formatEtc.tymed = TYMED_GDI;
248 hr = IOleCache_Cache(oleCache, &formatEtc, 0, &conn);
249 if (FAILED(hr))
250 goto end;
251 hr = IOleObject_QueryInterface(object, &IID_IDataObject,
252 (void**)&dataObject);
253 if (FAILED(hr))
254 goto end;
255 bitmap = LoadImageW(hInstance, MAKEINTRESOURCEW(id), IMAGE_BITMAP, 0, 0,
256 LR_DEFAULTSIZE | LR_LOADTRANSPARENT);
257 if (!bitmap)
258 goto end;
259 rect.left = rect.top = 0;
260 rect.right = GetSystemMetrics(SM_CXICON);
261 rect.bottom = GetSystemMetrics(SM_CYICON);
262 stgm.tymed = TYMED_GDI;
263 stgm.u.hBitmap = bitmap;
264 stgm.pUnkForRelease = NULL;
265 hr = IDataObject_SetData(dataObject, &formatEtc, &stgm, TRUE);
266 if (FAILED(hr))
267 goto end;
269 reObject.cbStruct = sizeof(reObject);
270 reObject.cp = REO_CP_SELECTION;
271 reObject.clsid = clsid;
272 reObject.poleobj = object;
273 reObject.pstg = NULL;
274 reObject.polesite = NULL;
275 reObject.sizel.cx = reObject.sizel.cy = 0;
276 reObject.dvaspect = DVASPECT_CONTENT;
277 reObject.dwFlags = 0;
278 reObject.dwUser = 0;
280 IRichEditOle_InsertObject(richEditOle, &reObject);
282 end:
283 if (dataObject)
284 IDataObject_Release(dataObject);
285 if (oleCache)
286 IOleCache_Release(oleCache);
287 if (object)
288 IOleObject_Release(object);
289 if (richEditOle)
290 IRichEditOle_Release(richEditOle);
293 #define MY_INDENT 200
295 static void add_oid_text_to_control(HWND hwnd, char *oid)
297 WCHAR nl = '\n';
298 PCCRYPT_OID_INFO oidInfo = CryptFindOIDInfo(CRYPT_OID_INFO_OID_KEY, oid, 0);
299 PARAFORMAT2 parFmt;
301 parFmt.cbSize = sizeof(parFmt);
302 parFmt.dwMask = PFM_STARTINDENT;
303 parFmt.dxStartIndent = MY_INDENT * 3;
304 if (oidInfo)
306 add_text_with_paraformat_to_control(hwnd, oidInfo->pwszName,
307 lstrlenW(oidInfo->pwszName), &parFmt);
308 add_unformatted_text_to_control(hwnd, &nl, 1);
312 #define MAX_STRING_LEN 512
314 struct OIDToString
316 LPCSTR oid;
317 int id;
320 /* The following list MUST be lexicographically sorted by OID */
321 static struct OIDToString oidMap[] = {
322 /* 1.3.6.1.4.1.311.10.3.1 */
323 { szOID_KP_CTL_USAGE_SIGNING, IDS_PURPOSE_CTL_USAGE_SIGNING },
324 /* 1.3.6.1.4.1.311.10.3.4 */
325 { szOID_KP_EFS, IDS_PURPOSE_EFS },
326 /* 1.3.6.1.4.1.311.10.3.4.1 */
327 { szOID_EFS_RECOVERY, IDS_PURPOSE_EFS_RECOVERY },
328 /* 1.3.6.1.4.1.311.10.3.5 */
329 { szOID_WHQL_CRYPTO, IDS_PURPOSE_WHQL },
330 /* 1.3.6.1.4.1.311.10.3.6 */
331 { szOID_NT5_CRYPTO, IDS_PURPOSE_NT5 },
332 /* 1.3.6.1.4.1.311.10.3.7 */
333 { szOID_OEM_WHQL_CRYPTO, IDS_PURPOSE_OEM_WHQL },
334 /* 1.3.6.1.4.1.311.10.3.8 */
335 { szOID_EMBEDDED_NT_CRYPTO, IDS_PURPOSE_EMBEDDED_NT },
336 /* 1.3.6.1.4.1.311.10.3.9 */
337 { szOID_ROOT_LIST_SIGNER, IDS_PURPOSE_ROOT_LIST_SIGNER },
338 /* 1.3.6.1.4.1.311.10.3.10 */
339 { szOID_KP_QUALIFIED_SUBORDINATION, IDS_PURPOSE_QUALIFIED_SUBORDINATION },
340 /* 1.3.6.1.4.1.311.10.3.11 */
341 { szOID_KP_KEY_RECOVERY, IDS_PURPOSE_KEY_RECOVERY },
342 /* 1.3.6.1.4.1.311.10.3.12 */
343 { szOID_KP_DOCUMENT_SIGNING, IDS_PURPOSE_DOCUMENT_SIGNING },
344 /* 1.3.6.1.4.1.311.10.3.13 */
345 { szOID_KP_LIFETIME_SIGNING, IDS_PURPOSE_LIFETIME_SIGNING },
346 /* 1.3.6.1.4.1.311.10.5.1 */
347 { szOID_DRM, IDS_PURPOSE_DRM },
348 /* 1.3.6.1.4.1.311.10.6.1 */
349 { szOID_LICENSES, IDS_PURPOSE_LICENSES },
350 /* 1.3.6.1.4.1.311.10.6.2 */
351 { szOID_LICENSE_SERVER, IDS_PURPOSE_LICENSE_SERVER },
352 /* 1.3.6.1.4.1.311.20.2.1 */
353 { szOID_ENROLLMENT_AGENT, IDS_PURPOSE_ENROLLMENT_AGENT },
354 /* 1.3.6.1.4.1.311.20.2.2 */
355 { szOID_KP_SMARTCARD_LOGON, IDS_PURPOSE_SMARTCARD_LOGON },
356 /* 1.3.6.1.4.1.311.21.5 */
357 { szOID_KP_CA_EXCHANGE, IDS_PURPOSE_CA_EXCHANGE },
358 /* 1.3.6.1.4.1.311.21.6 */
359 { szOID_KP_KEY_RECOVERY_AGENT, IDS_PURPOSE_KEY_RECOVERY_AGENT },
360 /* 1.3.6.1.4.1.311.21.19 */
361 { szOID_DS_EMAIL_REPLICATION, IDS_PURPOSE_DS_EMAIL_REPLICATION },
362 /* 1.3.6.1.5.5.7.3.1 */
363 { szOID_PKIX_KP_SERVER_AUTH, IDS_PURPOSE_SERVER_AUTH },
364 /* 1.3.6.1.5.5.7.3.2 */
365 { szOID_PKIX_KP_CLIENT_AUTH, IDS_PURPOSE_CLIENT_AUTH },
366 /* 1.3.6.1.5.5.7.3.3 */
367 { szOID_PKIX_KP_CODE_SIGNING, IDS_PURPOSE_CODE_SIGNING },
368 /* 1.3.6.1.5.5.7.3.4 */
369 { szOID_PKIX_KP_EMAIL_PROTECTION, IDS_PURPOSE_EMAIL_PROTECTION },
370 /* 1.3.6.1.5.5.7.3.5 */
371 { szOID_PKIX_KP_IPSEC_END_SYSTEM, IDS_PURPOSE_IPSEC },
372 /* 1.3.6.1.5.5.7.3.6 */
373 { szOID_PKIX_KP_IPSEC_TUNNEL, IDS_PURPOSE_IPSEC },
374 /* 1.3.6.1.5.5.7.3.7 */
375 { szOID_PKIX_KP_IPSEC_USER, IDS_PURPOSE_IPSEC },
376 /* 1.3.6.1.5.5.7.3.8 */
377 { szOID_PKIX_KP_TIMESTAMP_SIGNING, IDS_PURPOSE_TIMESTAMP_SIGNING },
380 static struct OIDToString *findSupportedOID(LPCSTR oid)
382 int indexHigh = sizeof(oidMap) / sizeof(oidMap[0]) - 1, indexLow = 0, i;
383 struct OIDToString *ret = NULL;
385 for (i = (indexLow + indexHigh) / 2; !ret && indexLow <= indexHigh;
386 i = (indexLow + indexHigh) / 2)
388 int cmp;
390 cmp = strcmp(oid, oidMap[i].oid);
391 if (!cmp)
392 ret = &oidMap[i];
393 else if (cmp > 0)
394 indexLow = i + 1;
395 else
396 indexHigh = i - 1;
398 return ret;
401 static void add_local_oid_text_to_control(HWND text, LPCSTR oid)
403 struct OIDToString *entry;
404 WCHAR nl = '\n';
405 PARAFORMAT2 parFmt;
407 parFmt.cbSize = sizeof(parFmt);
408 parFmt.dwMask = PFM_STARTINDENT;
409 parFmt.dxStartIndent = MY_INDENT * 3;
410 if ((entry = findSupportedOID(oid)))
412 WCHAR *str, *linebreak, *ptr;
413 BOOL multiline = FALSE;
414 int len;
416 len = LoadStringW(hInstance, entry->id, (LPWSTR)&str, 0);
417 ptr = str;
418 do {
419 if ((linebreak = memchrW(ptr, '\n', len)))
421 WCHAR copy[MAX_STRING_LEN];
423 multiline = TRUE;
424 /* The source string contains a newline, which the richedit
425 * control won't find since it's interpreted as a paragraph
426 * break. Therefore copy up to the newline. lstrcpynW always
427 * NULL-terminates, so pass one more than the length of the
428 * source line so the copy includes the entire line and the
429 * NULL-terminator.
431 lstrcpynW(copy, ptr, linebreak - ptr + 1);
432 add_text_with_paraformat_to_control(text, copy,
433 linebreak - ptr, &parFmt);
434 ptr = linebreak + 1;
435 add_unformatted_text_to_control(text, &nl, 1);
437 else if (multiline && *ptr)
439 /* Add the last line */
440 add_text_with_paraformat_to_control(text, ptr,
441 len - (ptr - str), &parFmt);
442 add_unformatted_text_to_control(text, &nl, 1);
444 } while (linebreak);
445 if (!multiline)
447 add_text_with_paraformat_to_control(text, str, len, &parFmt);
448 add_unformatted_text_to_control(text, &nl, 1);
451 else
453 WCHAR *oidW = HeapAlloc(GetProcessHeap(), 0,
454 (strlen(oid) + 1) * sizeof(WCHAR));
456 if (oidW)
458 LPCSTR src;
459 WCHAR *dst;
461 for (src = oid, dst = oidW; *src; src++, dst++)
462 *dst = *src;
463 *dst = 0;
464 add_text_with_paraformat_to_control(text, oidW, lstrlenW(oidW),
465 &parFmt);
466 add_unformatted_text_to_control(text, &nl, 1);
467 HeapFree(GetProcessHeap(), 0, oidW);
472 static void display_app_usages(HWND text, PCCERT_CONTEXT cert,
473 BOOL *anyUsageAdded)
475 static char any_app_policy[] = szOID_ANY_APPLICATION_POLICY;
476 WCHAR nl = '\n';
477 CHARFORMATW charFmt;
478 PCERT_EXTENSION policyExt;
479 if (!*anyUsageAdded)
481 PARAFORMAT2 parFmt;
483 parFmt.cbSize = sizeof(parFmt);
484 parFmt.dwMask = PFM_STARTINDENT;
485 parFmt.dxStartIndent = MY_INDENT;
486 add_string_resource_with_paraformat_to_control(text,
487 IDS_CERT_INFO_PURPOSES, &parFmt);
488 add_unformatted_text_to_control(text, &nl, 1);
489 *anyUsageAdded = TRUE;
491 memset(&charFmt, 0, sizeof(charFmt));
492 charFmt.cbSize = sizeof(charFmt);
493 charFmt.dwMask = CFM_BOLD;
494 charFmt.dwEffects = 0;
495 SendMessageW(text, EM_SETCHARFORMAT, SCF_SELECTION, (LPARAM)&charFmt);
496 if ((policyExt = CertFindExtension(szOID_APPLICATION_CERT_POLICIES,
497 cert->pCertInfo->cExtension, cert->pCertInfo->rgExtension)))
499 CERT_POLICIES_INFO *policies;
500 DWORD size;
502 if (CryptDecodeObjectEx(X509_ASN_ENCODING, X509_CERT_POLICIES,
503 policyExt->Value.pbData, policyExt->Value.cbData,
504 CRYPT_DECODE_ALLOC_FLAG, NULL, &policies, &size))
506 DWORD i;
508 for (i = 0; i < policies->cPolicyInfo; i++)
510 DWORD j;
512 for (j = 0; j < policies->rgPolicyInfo[i].cPolicyQualifier; j++)
513 add_local_oid_text_to_control(text,
514 policies->rgPolicyInfo[i].rgPolicyQualifier[j].
515 pszPolicyQualifierId);
517 LocalFree(policies);
520 else
521 add_oid_text_to_control(text, any_app_policy);
524 static BOOL display_cert_usages(HWND text, PCCERT_CONTEXT cert,
525 BOOL *anyUsageAdded)
527 WCHAR nl = '\n';
528 DWORD size;
529 BOOL badUsages = FALSE;
531 if (CertGetEnhancedKeyUsage(cert, 0, NULL, &size))
533 CHARFORMATW charFmt;
534 static char any_cert_policy[] = szOID_ANY_CERT_POLICY;
535 PCERT_ENHKEY_USAGE usage = HeapAlloc(GetProcessHeap(), 0, size);
537 if (usage)
539 if (CertGetEnhancedKeyUsage(cert, 0, usage, &size))
541 DWORD i;
543 if (!*anyUsageAdded)
545 PARAFORMAT2 parFmt;
547 parFmt.cbSize = sizeof(parFmt);
548 parFmt.dwMask = PFM_STARTINDENT;
549 parFmt.dxStartIndent = MY_INDENT;
550 add_string_resource_with_paraformat_to_control(text,
551 IDS_CERT_INFO_PURPOSES, &parFmt);
552 add_unformatted_text_to_control(text, &nl, 1);
553 *anyUsageAdded = TRUE;
555 memset(&charFmt, 0, sizeof(charFmt));
556 charFmt.cbSize = sizeof(charFmt);
557 charFmt.dwMask = CFM_BOLD;
558 charFmt.dwEffects = 0;
559 SendMessageW(text, EM_SETCHARFORMAT, SCF_SELECTION,
560 (LPARAM)&charFmt);
561 if (!usage->cUsageIdentifier)
562 add_oid_text_to_control(text, any_cert_policy);
563 else
564 for (i = 0; i < usage->cUsageIdentifier; i++)
565 add_local_oid_text_to_control(text,
566 usage->rgpszUsageIdentifier[i]);
568 else
569 badUsages = TRUE;
570 HeapFree(GetProcessHeap(), 0, usage);
572 else
573 badUsages = TRUE;
575 else
576 badUsages = TRUE;
577 return badUsages;
580 static void set_policy_text(HWND text,
581 PCCRYPTUI_VIEWCERTIFICATE_STRUCTW pCertViewInfo)
583 BOOL includeCertUsages = FALSE, includeAppUsages = FALSE;
584 BOOL badUsages = FALSE, anyUsageAdded = FALSE;
586 if (pCertViewInfo->cPurposes)
588 DWORD i;
590 for (i = 0; i < pCertViewInfo->cPurposes; i++)
592 if (!strcmp(pCertViewInfo->rgszPurposes[i], szOID_ANY_CERT_POLICY))
593 includeCertUsages = TRUE;
594 else if (!strcmp(pCertViewInfo->rgszPurposes[i],
595 szOID_ANY_APPLICATION_POLICY))
596 includeAppUsages = TRUE;
597 else
598 badUsages = TRUE;
601 else
602 includeAppUsages = includeCertUsages = TRUE;
603 if (includeAppUsages)
604 display_app_usages(text, pCertViewInfo->pCertContext, &anyUsageAdded);
605 if (includeCertUsages)
606 badUsages = display_cert_usages(text, pCertViewInfo->pCertContext,
607 &anyUsageAdded);
608 if (badUsages)
610 PARAFORMAT2 parFmt;
612 parFmt.cbSize = sizeof(parFmt);
613 parFmt.dwMask = PFM_STARTINDENT;
614 parFmt.dxStartIndent = MY_INDENT;
615 add_string_resource_with_paraformat_to_control(text,
616 IDS_CERT_INFO_BAD_PURPOSES, &parFmt);
620 static CRYPT_OBJID_BLOB *find_policy_qualifier(CERT_POLICIES_INFO *policies,
621 LPCSTR policyOid)
623 CRYPT_OBJID_BLOB *ret = NULL;
624 DWORD i;
626 for (i = 0; !ret && i < policies->cPolicyInfo; i++)
628 DWORD j;
630 for (j = 0; !ret && j < policies->rgPolicyInfo[i].cPolicyQualifier; j++)
631 if (!strcmp(policies->rgPolicyInfo[i].rgPolicyQualifier[j].
632 pszPolicyQualifierId, policyOid))
633 ret = &policies->rgPolicyInfo[i].rgPolicyQualifier[j].
634 Qualifier;
636 return ret;
639 static WCHAR *get_cps_str_from_qualifier(CRYPT_OBJID_BLOB *qualifier)
641 LPWSTR qualifierStr = NULL;
642 CERT_NAME_VALUE *qualifierValue;
643 DWORD size;
645 if (CryptDecodeObjectEx(X509_ASN_ENCODING, X509_NAME_VALUE,
646 qualifier->pbData, qualifier->cbData, CRYPT_DECODE_ALLOC_FLAG, NULL,
647 &qualifierValue, &size))
649 size = CertRDNValueToStrW(qualifierValue->dwValueType,
650 &qualifierValue->Value, NULL, 0);
651 qualifierStr = HeapAlloc(GetProcessHeap(), 0, size * sizeof(WCHAR));
652 if (qualifierStr)
653 CertRDNValueToStrW(qualifierValue->dwValueType,
654 &qualifierValue->Value, qualifierStr, size);
655 LocalFree(qualifierValue);
657 return qualifierStr;
660 static WCHAR *get_user_notice_from_qualifier(CRYPT_OBJID_BLOB *qualifier)
662 LPWSTR str = NULL;
663 CERT_POLICY_QUALIFIER_USER_NOTICE *qualifierValue;
664 DWORD size;
666 if (CryptDecodeObjectEx(X509_ASN_ENCODING,
667 X509_PKIX_POLICY_QUALIFIER_USERNOTICE,
668 qualifier->pbData, qualifier->cbData, CRYPT_DECODE_ALLOC_FLAG, NULL,
669 &qualifierValue, &size))
671 str = HeapAlloc(GetProcessHeap(), 0,
672 (strlenW(qualifierValue->pszDisplayText) + 1) * sizeof(WCHAR));
673 if (str)
674 strcpyW(str, qualifierValue->pszDisplayText);
675 LocalFree(qualifierValue);
677 return str;
680 struct IssuerStatement
682 LPWSTR cps;
683 LPWSTR userNotice;
686 static void set_issuer_statement(HWND hwnd,
687 PCCRYPTUI_VIEWCERTIFICATE_STRUCTW pCertViewInfo)
689 PCERT_EXTENSION policyExt;
691 if (!(pCertViewInfo->dwFlags & CRYPTUI_DISABLE_ISSUERSTATEMENT) &&
692 (policyExt = CertFindExtension(szOID_CERT_POLICIES,
693 pCertViewInfo->pCertContext->pCertInfo->cExtension,
694 pCertViewInfo->pCertContext->pCertInfo->rgExtension)))
696 CERT_POLICIES_INFO *policies;
697 DWORD size;
699 if (CryptDecodeObjectEx(X509_ASN_ENCODING, policyExt->pszObjId,
700 policyExt->Value.pbData, policyExt->Value.cbData,
701 CRYPT_DECODE_ALLOC_FLAG, NULL, &policies, &size))
703 CRYPT_OBJID_BLOB *qualifier;
704 LPWSTR cps = NULL, userNotice = NULL;
706 if ((qualifier = find_policy_qualifier(policies,
707 szOID_PKIX_POLICY_QUALIFIER_CPS)))
708 cps = get_cps_str_from_qualifier(qualifier);
709 if ((qualifier = find_policy_qualifier(policies,
710 szOID_PKIX_POLICY_QUALIFIER_USERNOTICE)))
711 userNotice = get_user_notice_from_qualifier(qualifier);
712 if (cps || userNotice)
714 struct IssuerStatement *issuerStatement =
715 HeapAlloc(GetProcessHeap(), 0, sizeof(struct IssuerStatement));
717 if (issuerStatement)
719 issuerStatement->cps = cps;
720 issuerStatement->userNotice = userNotice;
721 EnableWindow(GetDlgItem(hwnd, IDC_ISSUERSTATEMENT), TRUE);
722 SetWindowLongPtrW(hwnd, DWLP_USER,
723 (ULONG_PTR)issuerStatement);
726 LocalFree(policies);
731 static void set_cert_info(HWND hwnd,
732 PCCRYPTUI_VIEWCERTIFICATE_STRUCTW pCertViewInfo)
734 CHARFORMATW charFmt;
735 PARAFORMAT2 parFmt;
736 HWND icon = GetDlgItem(hwnd, IDC_CERTIFICATE_ICON);
737 HWND text = GetDlgItem(hwnd, IDC_CERTIFICATE_INFO);
738 CRYPT_PROVIDER_SGNR *provSigner = WTHelperGetProvSignerFromChain(
739 (CRYPT_PROVIDER_DATA *)pCertViewInfo->u.pCryptProviderData,
740 pCertViewInfo->idxSigner, pCertViewInfo->fCounterSigner,
741 pCertViewInfo->idxCounterSigner);
742 CRYPT_PROVIDER_CERT *root =
743 &provSigner->pasCertChain[provSigner->csCertChain - 1];
745 if (!provSigner->pChainContext ||
746 (provSigner->pChainContext->TrustStatus.dwErrorStatus &
747 CERT_TRUST_IS_PARTIAL_CHAIN))
748 add_icon_to_control(icon, IDB_CERT_WARNING);
749 else if (!root->fTrustedRoot)
750 add_icon_to_control(icon, IDB_CERT_ERROR);
751 else
752 add_icon_to_control(icon, IDB_CERT);
754 memset(&charFmt, 0, sizeof(charFmt));
755 charFmt.cbSize = sizeof(charFmt);
756 charFmt.dwMask = CFM_BOLD;
757 charFmt.dwEffects = CFE_BOLD;
758 SendMessageW(text, EM_SETCHARFORMAT, SCF_SELECTION, (LPARAM)&charFmt);
759 /* FIXME: vertically center text */
760 parFmt.cbSize = sizeof(parFmt);
761 parFmt.dwMask = PFM_STARTINDENT;
762 parFmt.dxStartIndent = MY_INDENT;
763 add_string_resource_with_paraformat_to_control(text,
764 IDS_CERTIFICATEINFORMATION, &parFmt);
766 text = GetDlgItem(hwnd, IDC_CERTIFICATE_STATUS);
767 SendMessageW(text, EM_SETCHARFORMAT, SCF_SELECTION, (LPARAM)&charFmt);
768 if (provSigner->dwError == TRUST_E_CERT_SIGNATURE)
769 add_string_resource_with_paraformat_to_control(text,
770 IDS_CERT_INFO_BAD_SIG, &parFmt);
771 else if (!provSigner->pChainContext ||
772 (provSigner->pChainContext->TrustStatus.dwErrorStatus &
773 CERT_TRUST_IS_PARTIAL_CHAIN))
774 add_string_resource_with_paraformat_to_control(text,
775 IDS_CERT_INFO_PARTIAL_CHAIN, &parFmt);
776 else if (!root->fTrustedRoot)
778 if (provSigner->csCertChain == 1 && root->fSelfSigned)
779 add_string_resource_with_paraformat_to_control(text,
780 IDS_CERT_INFO_UNTRUSTED_CA, &parFmt);
781 else
782 add_string_resource_with_paraformat_to_control(text,
783 IDS_CERT_INFO_UNTRUSTED_ROOT, &parFmt);
785 else
787 set_policy_text(text, pCertViewInfo);
788 set_issuer_statement(hwnd, pCertViewInfo);
792 static void set_cert_name_string(HWND hwnd, PCCERT_CONTEXT cert,
793 DWORD nameFlags, int heading)
795 WCHAR nl = '\n';
796 HWND text = GetDlgItem(hwnd, IDC_CERTIFICATE_NAMES);
797 CHARFORMATW charFmt;
798 PARAFORMAT2 parFmt;
800 memset(&charFmt, 0, sizeof(charFmt));
801 charFmt.cbSize = sizeof(charFmt);
802 charFmt.dwMask = CFM_BOLD;
803 charFmt.dwEffects = CFE_BOLD;
804 SendMessageW(text, EM_SETCHARFORMAT, SCF_SELECTION, (LPARAM)&charFmt);
805 parFmt.cbSize = sizeof(parFmt);
806 parFmt.dwMask = PFM_STARTINDENT;
807 parFmt.dxStartIndent = MY_INDENT * 3;
808 add_string_resource_with_paraformat_to_control(text, heading, &parFmt);
809 charFmt.dwEffects = 0;
810 SendMessageW(text, EM_SETCHARFORMAT, SCF_SELECTION, (LPARAM)&charFmt);
811 add_cert_string_to_control(text, cert, CERT_NAME_SIMPLE_DISPLAY_TYPE,
812 nameFlags);
813 add_unformatted_text_to_control(text, &nl, 1);
814 add_unformatted_text_to_control(text, &nl, 1);
815 add_unformatted_text_to_control(text, &nl, 1);
819 static void add_date_string_to_control(HWND hwnd, const FILETIME *fileTime)
821 WCHAR dateFmt[80]; /* sufficient for all versions of LOCALE_SSHORTDATE */
822 WCHAR date[80];
823 SYSTEMTIME sysTime;
825 GetLocaleInfoW(LOCALE_SYSTEM_DEFAULT, LOCALE_SSHORTDATE, dateFmt,
826 sizeof(dateFmt) / sizeof(dateFmt[0]));
827 FileTimeToSystemTime(fileTime, &sysTime);
828 GetDateFormatW(LOCALE_SYSTEM_DEFAULT, 0, &sysTime, dateFmt, date,
829 sizeof(date) / sizeof(date[0]));
830 add_unformatted_text_to_control(hwnd, date, lstrlenW(date));
833 static void set_cert_validity_period(HWND hwnd, PCCERT_CONTEXT cert)
835 WCHAR nl = '\n';
836 HWND text = GetDlgItem(hwnd, IDC_CERTIFICATE_NAMES);
837 CHARFORMATW charFmt;
838 PARAFORMAT2 parFmt;
840 memset(&charFmt, 0, sizeof(charFmt));
841 charFmt.cbSize = sizeof(charFmt);
842 charFmt.dwMask = CFM_BOLD;
843 charFmt.dwEffects = CFE_BOLD;
844 SendMessageW(text, EM_SETCHARFORMAT, SCF_SELECTION, (LPARAM)&charFmt);
845 parFmt.cbSize = sizeof(parFmt);
846 parFmt.dwMask = PFM_STARTINDENT;
847 parFmt.dxStartIndent = MY_INDENT * 3;
848 add_string_resource_with_paraformat_to_control(text, IDS_VALID_FROM,
849 &parFmt);
850 charFmt.dwEffects = 0;
851 SendMessageW(text, EM_SETCHARFORMAT, SCF_SELECTION, (LPARAM)&charFmt);
852 add_date_string_to_control(text, &cert->pCertInfo->NotBefore);
853 charFmt.dwEffects = CFE_BOLD;
854 SendMessageW(text, EM_SETCHARFORMAT, SCF_SELECTION, (LPARAM)&charFmt);
855 add_string_resource_to_control(text, IDS_VALID_TO);
856 charFmt.dwEffects = 0;
857 SendMessageW(text, EM_SETCHARFORMAT, SCF_SELECTION, (LPARAM)&charFmt);
858 add_date_string_to_control(text, &cert->pCertInfo->NotAfter);
859 add_unformatted_text_to_control(text, &nl, 1);
862 static void set_general_info(HWND hwnd,
863 PCCRYPTUI_VIEWCERTIFICATE_STRUCTW pCertViewInfo)
865 set_cert_info(hwnd, pCertViewInfo);
866 set_cert_name_string(hwnd, pCertViewInfo->pCertContext, 0,
867 IDS_SUBJECT_HEADING);
868 set_cert_name_string(hwnd, pCertViewInfo->pCertContext,
869 CERT_NAME_ISSUER_FLAG, IDS_ISSUER_HEADING);
870 set_cert_validity_period(hwnd, pCertViewInfo->pCertContext);
873 static LRESULT CALLBACK user_notice_dlg_proc(HWND hwnd, UINT msg, WPARAM wp,
874 LPARAM lp)
876 LRESULT ret = 0;
877 HWND text;
878 struct IssuerStatement *issuerStatement;
880 switch (msg)
882 case WM_INITDIALOG:
883 text = GetDlgItem(hwnd, IDC_USERNOTICE);
884 issuerStatement = (struct IssuerStatement *)lp;
885 add_unformatted_text_to_control(text, issuerStatement->userNotice,
886 strlenW(issuerStatement->userNotice));
887 if (issuerStatement->cps)
888 SetWindowLongPtrW(hwnd, DWLP_USER, (LPARAM)issuerStatement->cps);
889 else
890 EnableWindow(GetDlgItem(hwnd, IDC_CPS), FALSE);
891 break;
892 case WM_COMMAND:
893 switch (wp)
895 case IDOK:
896 EndDialog(hwnd, IDOK);
897 ret = TRUE;
898 break;
899 case IDC_CPS:
901 IBindCtx *bctx = NULL;
902 LPWSTR cps;
904 CreateBindCtx(0, &bctx);
905 cps = (LPWSTR)GetWindowLongPtrW(hwnd, DWLP_USER);
906 HlinkSimpleNavigateToString(cps, NULL, NULL, NULL, bctx, NULL,
907 HLNF_OPENINNEWWINDOW, 0);
908 IBindCtx_Release(bctx);
909 break;
913 return ret;
916 static void show_user_notice(HWND hwnd, struct IssuerStatement *issuerStatement)
918 DialogBoxParamW(hInstance, MAKEINTRESOURCEW(IDD_USERNOTICE), hwnd,
919 user_notice_dlg_proc, (LPARAM)issuerStatement);
922 static LRESULT CALLBACK general_dlg_proc(HWND hwnd, UINT msg, WPARAM wp,
923 LPARAM lp)
925 PROPSHEETPAGEW *page;
926 PCCRYPTUI_VIEWCERTIFICATE_STRUCTW pCertViewInfo;
928 TRACE("(%p, %08x, %08lx, %08lx)\n", hwnd, msg, wp, lp);
930 switch (msg)
932 case WM_INITDIALOG:
933 page = (PROPSHEETPAGEW *)lp;
934 pCertViewInfo = (PCCRYPTUI_VIEWCERTIFICATE_STRUCTW)page->lParam;
935 if (pCertViewInfo->dwFlags & CRYPTUI_DISABLE_ADDTOSTORE)
936 ShowWindow(GetDlgItem(hwnd, IDC_ADDTOSTORE), FALSE);
937 EnableWindow(GetDlgItem(hwnd, IDC_ISSUERSTATEMENT), FALSE);
938 set_general_info(hwnd, pCertViewInfo);
939 break;
940 case WM_COMMAND:
941 switch (wp)
943 case IDC_ADDTOSTORE:
944 FIXME("call CryptUIWizImport\n");
945 break;
946 case IDC_ISSUERSTATEMENT:
948 struct IssuerStatement *issuerStatement =
949 (struct IssuerStatement *)GetWindowLongPtrW(hwnd, DWLP_USER);
951 if (issuerStatement)
953 if (issuerStatement->userNotice)
954 show_user_notice(hwnd, issuerStatement);
955 else if (issuerStatement->cps)
957 IBindCtx *bctx = NULL;
959 CreateBindCtx(0, &bctx);
960 HlinkSimpleNavigateToString(issuerStatement->cps, NULL,
961 NULL, NULL, bctx, NULL, HLNF_OPENINNEWWINDOW, 0);
962 IBindCtx_Release(bctx);
965 break;
968 break;
970 return 0;
973 static UINT CALLBACK general_callback_proc(HWND hwnd, UINT msg,
974 PROPSHEETPAGEW *page)
976 struct IssuerStatement *issuerStatement;
978 switch (msg)
980 case PSPCB_RELEASE:
981 issuerStatement =
982 (struct IssuerStatement *)GetWindowLongPtrW(hwnd, DWLP_USER);
983 if (issuerStatement)
985 HeapFree(GetProcessHeap(), 0, issuerStatement->cps);
986 HeapFree(GetProcessHeap(), 0, issuerStatement->userNotice);
987 HeapFree(GetProcessHeap(), 0, issuerStatement);
989 break;
991 return 1;
994 static void init_general_page(PCCRYPTUI_VIEWCERTIFICATE_STRUCTW pCertViewInfo,
995 PROPSHEETPAGEW *page)
997 memset(page, 0, sizeof(PROPSHEETPAGEW));
998 page->dwSize = sizeof(PROPSHEETPAGEW);
999 page->dwFlags = PSP_USECALLBACK;
1000 page->pfnCallback = general_callback_proc;
1001 page->hInstance = hInstance;
1002 page->u.pszTemplate = MAKEINTRESOURCEW(IDD_GENERAL);
1003 page->pfnDlgProc = general_dlg_proc;
1004 page->lParam = (LPARAM)pCertViewInfo;
1007 typedef WCHAR * (*field_format_func)(PCCERT_CONTEXT cert);
1009 static WCHAR *field_format_version(PCCERT_CONTEXT cert)
1011 static const WCHAR fmt[] = { 'V','%','d',0 };
1012 WCHAR *buf = HeapAlloc(GetProcessHeap(), 0, 12 * sizeof(WCHAR));
1014 if (buf)
1015 sprintfW(buf, fmt, cert->pCertInfo->dwVersion);
1016 return buf;
1019 static WCHAR *format_hex_string(void *pb, DWORD cb)
1021 WCHAR *buf = HeapAlloc(GetProcessHeap(), 0, (cb * 3 + 1) * sizeof(WCHAR));
1023 if (buf)
1025 static const WCHAR fmt[] = { '%','0','2','x',' ',0 };
1026 DWORD i;
1027 WCHAR *ptr;
1029 for (i = 0, ptr = buf; i < cb; i++, ptr += 3)
1030 sprintfW(ptr, fmt, ((BYTE *)pb)[i]);
1032 return buf;
1035 static WCHAR *field_format_serial_number(PCCERT_CONTEXT cert)
1037 return format_hex_string(cert->pCertInfo->SerialNumber.pbData,
1038 cert->pCertInfo->SerialNumber.cbData);
1041 static WCHAR *field_format_issuer(PCCERT_CONTEXT cert)
1043 return get_cert_name_string(cert, CERT_NAME_SIMPLE_DISPLAY_TYPE,
1044 CERT_NAME_ISSUER_FLAG);
1047 static WCHAR *field_format_detailed_cert_name(PCERT_NAME_BLOB name)
1049 WCHAR *str = NULL;
1050 DWORD len = CertNameToStrW(X509_ASN_ENCODING, name,
1051 CERT_X500_NAME_STR | CERT_NAME_STR_CRLF_FLAG, NULL, 0);
1053 if (len)
1055 str = HeapAlloc(GetProcessHeap(), 0, len * sizeof(WCHAR));
1056 if (str)
1057 CertNameToStrW(X509_ASN_ENCODING, name,
1058 CERT_X500_NAME_STR | CERT_NAME_STR_CRLF_FLAG, str, len);
1060 return str;
1063 static WCHAR *field_format_detailed_issuer(PCCERT_CONTEXT cert, void *param)
1065 return field_format_detailed_cert_name(&cert->pCertInfo->Issuer);
1068 static WCHAR *field_format_subject(PCCERT_CONTEXT cert)
1070 return get_cert_name_string(cert, CERT_NAME_SIMPLE_DISPLAY_TYPE, 0);
1073 static WCHAR *field_format_detailed_subject(PCCERT_CONTEXT cert, void *param)
1075 return field_format_detailed_cert_name(&cert->pCertInfo->Subject);
1078 static WCHAR *format_long_date(const FILETIME *fileTime)
1080 WCHAR dateFmt[80]; /* long enough for LOCALE_SLONGDATE */
1081 DWORD len;
1082 WCHAR *buf = NULL;
1083 SYSTEMTIME sysTime;
1085 /* FIXME: format isn't quite right, want time too */
1086 GetLocaleInfoW(LOCALE_SYSTEM_DEFAULT, LOCALE_SLONGDATE, dateFmt,
1087 sizeof(dateFmt) / sizeof(dateFmt[0]));
1088 FileTimeToSystemTime(fileTime, &sysTime);
1089 len = GetDateFormatW(LOCALE_SYSTEM_DEFAULT, 0, &sysTime, dateFmt, NULL, 0);
1090 if (len)
1092 buf = HeapAlloc(GetProcessHeap(), 0, len * sizeof(WCHAR));
1093 if (buf)
1094 GetDateFormatW(LOCALE_SYSTEM_DEFAULT, 0, &sysTime, dateFmt, buf,
1095 len);
1097 return buf;
1100 static WCHAR *field_format_from_date(PCCERT_CONTEXT cert)
1102 return format_long_date(&cert->pCertInfo->NotBefore);
1105 static WCHAR *field_format_to_date(PCCERT_CONTEXT cert)
1107 return format_long_date(&cert->pCertInfo->NotAfter);
1110 static WCHAR *field_format_public_key(PCCERT_CONTEXT cert)
1112 PCCRYPT_OID_INFO oidInfo;
1113 WCHAR *buf = NULL;
1115 oidInfo = CryptFindOIDInfo(CRYPT_OID_INFO_OID_KEY,
1116 cert->pCertInfo->SubjectPublicKeyInfo.Algorithm.pszObjId, 0);
1117 if (oidInfo)
1119 WCHAR fmt[MAX_STRING_LEN];
1121 if (LoadStringW(hInstance, IDS_FIELD_PUBLIC_KEY_FORMAT, fmt,
1122 sizeof(fmt) / sizeof(fmt[0])))
1124 /* Allocate the output buffer. Use the number of bytes in the
1125 * public key as a conservative (high) estimate for the number of
1126 * digits in its output.
1127 * The output is of the form (in English)
1128 * "<public key algorithm> (<public key bit length> bits)".
1129 * Ordinarily having two positional parameters in a string is not a
1130 * good idea, but as this isn't a sentence fragment, it shouldn't
1131 * be word-order dependent.
1133 buf = HeapAlloc(GetProcessHeap(), 0,
1134 (strlenW(fmt) + strlenW(oidInfo->pwszName) +
1135 cert->pCertInfo->SubjectPublicKeyInfo.PublicKey.cbData * 8)
1136 * sizeof(WCHAR));
1137 if (buf)
1138 sprintfW(buf, fmt, oidInfo->pwszName,
1139 CertGetPublicKeyLength(X509_ASN_ENCODING,
1140 &cert->pCertInfo->SubjectPublicKeyInfo));
1143 return buf;
1146 static WCHAR *field_format_detailed_public_key(PCCERT_CONTEXT cert, void *param)
1148 return format_hex_string(
1149 cert->pCertInfo->SubjectPublicKeyInfo.PublicKey.pbData,
1150 cert->pCertInfo->SubjectPublicKeyInfo.PublicKey.cbData);
1153 struct field_value_data;
1154 struct detail_data
1156 PCCRYPTUI_VIEWCERTIFICATE_STRUCTW pCertViewInfo;
1157 BOOL *pfPropertiesChanged;
1158 int cFields;
1159 struct field_value_data *fields;
1160 HIMAGELIST imageList;
1163 typedef void (*add_fields_func)(HWND hwnd, struct detail_data *data);
1165 typedef WCHAR *(*create_detailed_value_func)(PCCERT_CONTEXT cert, void *param);
1167 struct field_value_data
1169 create_detailed_value_func create;
1170 LPWSTR detailed_value;
1171 void *param;
1174 static void add_field_value_data(struct detail_data *data,
1175 create_detailed_value_func create, void *param)
1177 if (data->cFields)
1178 data->fields = HeapReAlloc(GetProcessHeap(), 0, data->fields,
1179 (data->cFields + 1) * sizeof(struct field_value_data));
1180 else
1181 data->fields = HeapAlloc(GetProcessHeap(), 0,
1182 sizeof(struct field_value_data));
1183 if (data->fields)
1185 data->fields[data->cFields].create = create;
1186 data->fields[data->cFields].detailed_value = NULL;
1187 data->fields[data->cFields].param = param;
1188 data->cFields++;
1192 static void add_field_and_value_to_list(HWND hwnd, struct detail_data *data,
1193 LPWSTR field, LPWSTR value, create_detailed_value_func create, void *param)
1195 LVITEMW item;
1196 int iItem = SendMessageW(hwnd, LVM_GETITEMCOUNT, 0, 0);
1198 item.mask = LVIF_TEXT | LVIF_PARAM;
1199 item.iItem = iItem;
1200 item.iSubItem = 0;
1201 item.pszText = field;
1202 item.lParam = (LPARAM)data;
1203 SendMessageW(hwnd, LVM_INSERTITEMW, 0, (LPARAM)&item);
1204 if (value)
1206 item.pszText = value;
1207 item.iSubItem = 1;
1208 SendMessageW(hwnd, LVM_SETITEMTEXTW, iItem, (LPARAM)&item);
1210 add_field_value_data(data, create, param);
1213 static void add_string_id_and_value_to_list(HWND hwnd, struct detail_data *data,
1214 int id, LPWSTR value, create_detailed_value_func create, void *param)
1216 WCHAR buf[MAX_STRING_LEN];
1218 LoadStringW(hInstance, id, buf, sizeof(buf) / sizeof(buf[0]));
1219 add_field_and_value_to_list(hwnd, data, buf, value, create, param);
1222 struct v1_field
1224 int id;
1225 field_format_func format;
1226 create_detailed_value_func create_detailed_value;
1229 static void add_v1_field(HWND hwnd, struct detail_data *data,
1230 const struct v1_field *field)
1232 WCHAR *val = field->format(data->pCertViewInfo->pCertContext);
1234 if (val)
1236 add_string_id_and_value_to_list(hwnd, data, field->id, val,
1237 field->create_detailed_value, NULL);
1238 HeapFree(GetProcessHeap(), 0, val);
1242 static const struct v1_field v1_fields[] = {
1243 { IDS_FIELD_VERSION, field_format_version, NULL },
1244 { IDS_FIELD_SERIAL_NUMBER, field_format_serial_number, NULL },
1245 { IDS_FIELD_ISSUER, field_format_issuer, field_format_detailed_issuer },
1246 { IDS_FIELD_VALID_FROM, field_format_from_date, NULL },
1247 { IDS_FIELD_VALID_TO, field_format_to_date, NULL },
1248 { IDS_FIELD_SUBJECT, field_format_subject, field_format_detailed_subject },
1249 { IDS_FIELD_PUBLIC_KEY, field_format_public_key,
1250 field_format_detailed_public_key }
1253 static void add_v1_fields(HWND hwnd, struct detail_data *data)
1255 int i;
1256 PCCERT_CONTEXT cert = data->pCertViewInfo->pCertContext;
1258 /* The last item in v1_fields is the public key, which is not in the loop
1259 * because it's a special case.
1261 for (i = 0; i < sizeof(v1_fields) / sizeof(v1_fields[0]) - 1; i++)
1262 add_v1_field(hwnd, data, &v1_fields[i]);
1263 if (cert->pCertInfo->SubjectPublicKeyInfo.PublicKey.cbData)
1264 add_v1_field(hwnd, data, &v1_fields[i]);
1267 static WCHAR *crypt_format_extension(PCERT_EXTENSION ext, DWORD formatStrType)
1269 WCHAR *str = NULL;
1270 DWORD size;
1272 if (CryptFormatObject(X509_ASN_ENCODING, 0, formatStrType, NULL,
1273 ext->pszObjId, ext->Value.pbData, ext->Value.cbData, NULL, &size))
1275 str = HeapAlloc(GetProcessHeap(), 0, size);
1276 CryptFormatObject(X509_ASN_ENCODING, 0, formatStrType, NULL,
1277 ext->pszObjId, ext->Value.pbData, ext->Value.cbData, str, &size);
1279 return str;
1282 static WCHAR *field_format_extension_hex_with_ascii(PCERT_EXTENSION ext)
1284 WCHAR *str = NULL;
1286 if (ext->Value.cbData)
1288 /* The output is formatted as:
1289 * <hex bytes> <ascii bytes>\n
1290 * where <hex bytes> is a string of up to 8 bytes, output as %02x,
1291 * and <ascii bytes> is the ASCII equivalent of each byte, or '.' if
1292 * the byte is not printable.
1293 * So, for example, the extension value consisting of the following
1294 * bytes:
1295 * 0x30,0x14,0x31,0x12,0x30,0x10,0x06,0x03,0x55,0x04,0x03,
1296 * 0x13,0x09,0x4a,0x75,0x61,0x6e,0x20,0x4c,0x61,0x6e,0x67
1297 * is output as:
1298 * 30 14 31 12 30 10 06 03 0.1.0...
1299 * 55 04 03 13 09 4a 75 61 U....Jua
1300 * 6e 20 4c 61 6e 67 n Lang
1301 * The allocation size therefore requires:
1302 * - 4 characters per character in an 8-byte line
1303 * (2 for the hex format, one for the space, one for the ASCII value)
1304 * - 3 more characters per 8-byte line (two spaces and a newline)
1305 * - 1 character for the terminating nul
1306 * FIXME: should use a fixed-width font for this
1308 DWORD lines = (ext->Value.cbData + 7) / 8;
1310 str = HeapAlloc(GetProcessHeap(), 0,
1311 (lines * 8 * 4 + lines * 3 + 1) * sizeof(WCHAR));
1312 if (str)
1314 static const WCHAR fmt[] = { '%','0','2','x',' ',0 };
1315 DWORD i, j;
1316 WCHAR *ptr;
1318 for (i = 0, ptr = str; i < ext->Value.cbData; i += 8)
1320 /* Output as hex bytes first */
1321 for (j = i; j < min(i + 8, ext->Value.cbData); j++, ptr += 3)
1322 sprintfW(ptr, fmt, ext->Value.pbData[j]);
1323 /* Pad the hex output with spaces for alignment */
1324 if (j == ext->Value.cbData && j % 8)
1326 static const WCHAR pad[] = { ' ',' ',' ' };
1328 for (; j % 8; j++, ptr += sizeof(pad) / sizeof(pad[0]))
1329 memcpy(ptr, pad, sizeof(pad));
1331 /* The last sprintfW included a space, so just insert one
1332 * more space between the hex bytes and the ASCII output
1334 *ptr++ = ' ';
1335 /* Output as ASCII bytes */
1336 for (j = i; j < min(i + 8, ext->Value.cbData); j++, ptr++)
1338 if (isprintW(ext->Value.pbData[j]) &&
1339 !isspaceW(ext->Value.pbData[j]))
1340 *ptr = ext->Value.pbData[j];
1341 else
1342 *ptr = '.';
1344 *ptr++ = '\n';
1346 *ptr++ = '\0';
1349 return str;
1352 static WCHAR *field_format_detailed_extension(PCCERT_CONTEXT cert, void *param)
1354 PCERT_EXTENSION ext = param;
1355 LPWSTR str = crypt_format_extension(ext,
1356 CRYPT_FORMAT_STR_MULTI_LINE | CRYPT_FORMAT_STR_NO_HEX);
1358 if (!str)
1359 str = field_format_extension_hex_with_ascii(ext);
1360 return str;
1363 static void add_cert_extension_detail(HWND hwnd, struct detail_data *data,
1364 PCERT_EXTENSION ext)
1366 PCCRYPT_OID_INFO oidInfo = CryptFindOIDInfo(CRYPT_OID_INFO_OID_KEY,
1367 ext->pszObjId, 0);
1368 LPWSTR val = crypt_format_extension(ext, 0);
1370 if (oidInfo)
1371 add_field_and_value_to_list(hwnd, data, (LPWSTR)oidInfo->pwszName,
1372 val, field_format_detailed_extension, ext);
1373 else
1375 DWORD len = strlen(ext->pszObjId);
1376 LPWSTR oidW = HeapAlloc(GetProcessHeap(), 0, (len + 1) * sizeof(WCHAR));
1378 if (oidW)
1380 DWORD i;
1382 for (i = 0; i <= len; i++)
1383 oidW[i] = ext->pszObjId[i];
1384 add_field_and_value_to_list(hwnd, data, oidW, val,
1385 field_format_detailed_extension, ext);
1386 HeapFree(GetProcessHeap(), 0, oidW);
1389 HeapFree(GetProcessHeap(), 0, val);
1392 static void add_all_extensions(HWND hwnd, struct detail_data *data)
1394 DWORD i;
1395 PCCERT_CONTEXT cert = data->pCertViewInfo->pCertContext;
1397 for (i = 0; i < cert->pCertInfo->cExtension; i++)
1398 add_cert_extension_detail(hwnd, data, &cert->pCertInfo->rgExtension[i]);
1401 static void add_critical_extensions(HWND hwnd, struct detail_data *data)
1403 DWORD i;
1404 PCCERT_CONTEXT cert = data->pCertViewInfo->pCertContext;
1406 for (i = 0; i < cert->pCertInfo->cExtension; i++)
1407 if (cert->pCertInfo->rgExtension[i].fCritical)
1408 add_cert_extension_detail(hwnd, data,
1409 &cert->pCertInfo->rgExtension[i]);
1412 typedef WCHAR * (*prop_to_value_func)(void *pb, DWORD cb);
1414 struct prop_id_to_string_id
1416 DWORD prop;
1417 int id;
1418 BOOL prop_is_string;
1419 prop_to_value_func prop_to_value;
1422 static WCHAR *format_enhanced_key_usage_value(void *pb, DWORD cb)
1424 CERT_EXTENSION ext;
1426 ext.pszObjId = (LPSTR)X509_ENHANCED_KEY_USAGE;
1427 ext.fCritical = FALSE;
1428 ext.Value.pbData = pb;
1429 ext.Value.cbData = cb;
1430 return crypt_format_extension(&ext, 0);
1433 /* Logically the access state should also be checked, and IDC_EDITPROPERTIES
1434 * disabled for read-only certificates, but native doesn't appear to do that.
1436 static const struct prop_id_to_string_id prop_id_map[] = {
1437 { CERT_HASH_PROP_ID, IDS_PROP_HASH, FALSE, format_hex_string },
1438 { CERT_FRIENDLY_NAME_PROP_ID, IDS_PROP_FRIENDLY_NAME, TRUE, NULL },
1439 { CERT_DESCRIPTION_PROP_ID, IDS_PROP_DESCRIPTION, TRUE, NULL },
1440 { CERT_ENHKEY_USAGE_PROP_ID, IDS_PROP_ENHKEY_USAGE, FALSE,
1441 format_enhanced_key_usage_value },
1444 static void add_properties(HWND hwnd, struct detail_data *data)
1446 DWORD i;
1447 PCCERT_CONTEXT cert = data->pCertViewInfo->pCertContext;
1449 for (i = 0; i < sizeof(prop_id_map) / sizeof(prop_id_map[0]); i++)
1451 DWORD cb;
1453 if (CertGetCertificateContextProperty(cert, prop_id_map[i].prop, NULL,
1454 &cb))
1456 BYTE *pb;
1457 WCHAR *val = NULL;
1459 /* FIXME: MS adds a separate value for the signature hash
1460 * algorithm.
1462 pb = HeapAlloc(GetProcessHeap(), 0, cb);
1463 if (pb)
1465 if (CertGetCertificateContextProperty(cert,
1466 prop_id_map[i].prop, pb, &cb))
1468 if (prop_id_map[i].prop_is_string)
1470 val = (LPWSTR)pb;
1471 /* Don't double-free pb */
1472 pb = NULL;
1474 else
1475 val = prop_id_map[i].prop_to_value(pb, cb);
1477 HeapFree(GetProcessHeap(), 0, pb);
1479 add_string_id_and_value_to_list(hwnd, data, prop_id_map[i].id, val,
1480 NULL, NULL);
1485 static void add_all_fields(HWND hwnd, struct detail_data *data)
1487 add_v1_fields(hwnd, data);
1488 add_all_extensions(hwnd, data);
1489 add_properties(hwnd, data);
1492 struct selection_list_item
1494 int id;
1495 add_fields_func add;
1498 const struct selection_list_item listItems[] = {
1499 { IDS_FIELDS_ALL, add_all_fields },
1500 { IDS_FIELDS_V1, add_v1_fields },
1501 { IDS_FIELDS_EXTENSIONS, add_all_extensions },
1502 { IDS_FIELDS_CRITICAL_EXTENSIONS, add_critical_extensions },
1503 { IDS_FIELDS_PROPERTIES, add_properties },
1506 static void create_show_list(HWND hwnd, struct detail_data *data)
1508 HWND cb = GetDlgItem(hwnd, IDC_DETAIL_SELECT);
1509 WCHAR buf[MAX_STRING_LEN];
1510 int i;
1512 for (i = 0; i < sizeof(listItems) / sizeof(listItems[0]); i++)
1514 int index;
1516 LoadStringW(hInstance, listItems[i].id, buf,
1517 sizeof(buf) / sizeof(buf[0]));
1518 index = SendMessageW(cb, CB_INSERTSTRING, -1, (LPARAM)buf);
1519 SendMessageW(cb, CB_SETITEMDATA, index, (LPARAM)data);
1521 SendMessageW(cb, CB_SETCURSEL, 0, 0);
1524 static void create_listview_columns(HWND hwnd)
1526 HWND lv = GetDlgItem(hwnd, IDC_DETAIL_LIST);
1527 RECT rc;
1528 WCHAR buf[MAX_STRING_LEN];
1529 LVCOLUMNW column;
1531 SendMessageW(lv, LVM_SETEXTENDEDLISTVIEWSTYLE, 0, LVS_EX_FULLROWSELECT);
1532 GetWindowRect(lv, &rc);
1533 LoadStringW(hInstance, IDS_FIELD, buf, sizeof(buf) / sizeof(buf[0]));
1534 column.mask = LVCF_WIDTH | LVCF_TEXT;
1535 column.cx = (rc.right - rc.left) / 2 - 2;
1536 column.pszText = buf;
1537 SendMessageW(lv, LVM_INSERTCOLUMNW, 0, (LPARAM)&column);
1538 LoadStringW(hInstance, IDS_VALUE, buf, sizeof(buf) / sizeof(buf[0]));
1539 SendMessageW(lv, LVM_INSERTCOLUMNW, 1, (LPARAM)&column);
1542 static void set_fields_selection(HWND hwnd, struct detail_data *data, int sel)
1544 HWND list = GetDlgItem(hwnd, IDC_DETAIL_LIST);
1546 if (sel >= 0 && sel < sizeof(listItems) / sizeof(listItems[0]))
1548 SendMessageW(list, LVM_DELETEALLITEMS, 0, 0);
1549 listItems[sel].add(list, data);
1553 static void create_cert_details_list(HWND hwnd, struct detail_data *data)
1555 create_show_list(hwnd, data);
1556 create_listview_columns(hwnd);
1557 set_fields_selection(hwnd, data, 0);
1560 typedef enum {
1561 CheckBitmapIndexUnchecked = 1,
1562 CheckBitmapIndexChecked = 2,
1563 CheckBitmapIndexDisabledUnchecked = 3,
1564 CheckBitmapIndexDisabledChecked = 4
1565 } CheckBitmapIndex;
1567 static void add_purpose(HWND hwnd, LPCSTR oid)
1569 HWND lv = GetDlgItem(hwnd, IDC_CERTIFICATE_USAGES);
1570 PCRYPT_OID_INFO info = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY,
1571 sizeof(CRYPT_OID_INFO));
1573 if (info)
1575 char *oidCopy = HeapAlloc(GetProcessHeap(), 0, strlen(oid) + 1);
1577 if (oidCopy)
1579 LVITEMA item;
1581 strcpy(oidCopy, oid);
1582 info->cbSize = sizeof(CRYPT_OID_INFO);
1583 info->pszOID = oidCopy;
1584 item.mask = LVIF_TEXT | LVIF_STATE | LVIF_PARAM;
1585 item.state = INDEXTOSTATEIMAGEMASK(CheckBitmapIndexChecked);
1586 item.stateMask = LVIS_STATEIMAGEMASK;
1587 item.iItem = SendMessageW(lv, LVM_GETITEMCOUNT, 0, 0);
1588 item.iSubItem = 0;
1589 item.lParam = (LPARAM)info;
1590 item.pszText = oidCopy;
1591 SendMessageA(lv, LVM_INSERTITEMA, 0, (LPARAM)&item);
1593 else
1594 HeapFree(GetProcessHeap(), 0, info);
1598 static BOOL is_valid_oid(LPCSTR oid)
1600 BOOL ret;
1602 if (oid[0] != '0' && oid[0] != '1' && oid[0] != '2')
1603 ret = FALSE;
1604 else if (oid[1] != '.')
1605 ret = FALSE;
1606 else if (!oid[2])
1607 ret = FALSE;
1608 else
1610 const char *ptr;
1611 BOOL expectNum = TRUE;
1613 for (ptr = oid + 2, ret = TRUE; ret && *ptr; ptr++)
1615 if (expectNum)
1617 if (!isdigit(*ptr))
1618 ret = FALSE;
1619 else if (*(ptr + 1) == '.')
1620 expectNum = FALSE;
1622 else
1624 if (*ptr != '.')
1625 ret = FALSE;
1626 else if (!(*(ptr + 1)))
1627 ret = FALSE;
1628 else
1629 expectNum = TRUE;
1633 return ret;
1636 static BOOL is_oid_in_list(HWND hwnd, LPCSTR oid)
1638 HWND lv = GetDlgItem(hwnd, IDC_CERTIFICATE_USAGES);
1639 PCCRYPT_OID_INFO oidInfo = CryptFindOIDInfo(CRYPT_OID_INFO_OID_KEY,
1640 (void *)oid, CRYPT_ENHKEY_USAGE_OID_GROUP_ID);
1641 BOOL ret = FALSE;
1643 if (oidInfo)
1645 LVFINDINFOW findInfo;
1647 findInfo.flags = LVFI_PARAM;
1648 findInfo.lParam = (LPARAM)oidInfo;
1649 if (SendMessageW(lv, LVM_FINDITEMW, -1, (LPARAM)&findInfo) != -1)
1650 ret = TRUE;
1652 else
1654 LVFINDINFOA findInfo;
1656 findInfo.flags = LVFI_STRING;
1657 findInfo.psz = oid;
1658 if (SendMessageW(lv, LVM_FINDITEMA, -1, (LPARAM)&findInfo) != -1)
1659 ret = TRUE;
1661 return ret;
1664 #define MAX_PURPOSE 255
1666 static LRESULT CALLBACK add_purpose_dlg_proc(HWND hwnd, UINT msg,
1667 WPARAM wp, LPARAM lp)
1669 LRESULT ret = 0;
1670 char buf[MAX_PURPOSE + 1];
1672 switch (msg)
1674 case WM_INITDIALOG:
1675 SendMessageW(GetDlgItem(hwnd, IDC_NEW_PURPOSE), EM_SETLIMITTEXT,
1676 MAX_PURPOSE, 0);
1677 ShowScrollBar(GetDlgItem(hwnd, IDC_NEW_PURPOSE), SB_VERT, FALSE);
1678 SetWindowLongPtrW(hwnd, DWLP_USER, lp);
1679 break;
1680 case WM_COMMAND:
1681 switch (HIWORD(wp))
1683 case EN_CHANGE:
1684 if (LOWORD(wp) == IDC_NEW_PURPOSE)
1686 /* Show/hide scroll bar on description depending on how much
1687 * text it has.
1689 HWND description = GetDlgItem(hwnd, IDC_NEW_PURPOSE);
1690 int lines = SendMessageW(description, EM_GETLINECOUNT, 0, 0);
1692 ShowScrollBar(description, SB_VERT, lines > 1);
1694 break;
1695 case BN_CLICKED:
1696 switch (LOWORD(wp))
1698 case IDOK:
1699 SendMessageA(GetDlgItem(hwnd, IDC_NEW_PURPOSE), WM_GETTEXT,
1700 sizeof(buf) / sizeof(buf[0]), (LPARAM)buf);
1701 if (!buf[0])
1703 /* An empty purpose is the same as cancelling */
1704 EndDialog(hwnd, IDCANCEL);
1705 ret = TRUE;
1707 else if (!is_valid_oid(buf))
1709 WCHAR title[MAX_STRING_LEN], error[MAX_STRING_LEN];
1711 LoadStringW(hInstance, IDS_CERTIFICATE_PURPOSE_ERROR, error,
1712 sizeof(error) / sizeof(error[0]));
1713 LoadStringW(hInstance, IDS_CERTIFICATE_PROPERTIES, title,
1714 sizeof(title) / sizeof(title[0]));
1715 MessageBoxW(hwnd, error, title, MB_ICONERROR | MB_OK);
1717 else if (is_oid_in_list(
1718 (HWND)GetWindowLongPtrW(hwnd, DWLP_USER), buf))
1720 WCHAR title[MAX_STRING_LEN], error[MAX_STRING_LEN];
1722 LoadStringW(hInstance, IDS_CERTIFICATE_PURPOSE_EXISTS,
1723 error, sizeof(error) / sizeof(error[0]));
1724 LoadStringW(hInstance, IDS_CERTIFICATE_PROPERTIES, title,
1725 sizeof(title) / sizeof(title[0]));
1726 MessageBoxW(hwnd, error, title, MB_ICONEXCLAMATION | MB_OK);
1728 else
1730 HWND parent = (HWND)GetWindowLongPtrW(hwnd, DWLP_USER);
1732 add_purpose(parent, buf);
1733 EndDialog(hwnd, wp);
1734 ret = TRUE;
1736 break;
1737 case IDCANCEL:
1738 EndDialog(hwnd, wp);
1739 ret = TRUE;
1740 break;
1742 break;
1744 break;
1746 return ret;
1749 static WCHAR *get_cert_property_as_string(PCCERT_CONTEXT cert, DWORD prop)
1751 WCHAR *name = NULL;
1752 DWORD cb;
1754 if (CertGetCertificateContextProperty(cert, prop, NULL, &cb))
1756 name = HeapAlloc(GetProcessHeap(), 0, cb);
1757 if (name)
1759 if (!CertGetCertificateContextProperty(cert, prop, (LPBYTE)name,
1760 &cb))
1762 HeapFree(GetProcessHeap(), 0, name);
1763 name = NULL;
1767 return name;
1770 static void redraw_states(HWND list, BOOL enabled)
1772 int items = SendMessageW(list, LVM_GETITEMCOUNT, 0, 0), i;
1774 for (i = 0; i < items; i++)
1776 BOOL change = FALSE;
1777 int state;
1779 state = SendMessageW(list, LVM_GETITEMSTATE, i, LVIS_STATEIMAGEMASK);
1780 /* This reverses the INDEXTOSTATEIMAGEMASK shift. There doesn't appear
1781 * to be a handy macro for it.
1783 state >>= 12;
1784 if (enabled)
1786 if (state == CheckBitmapIndexDisabledChecked)
1788 state = CheckBitmapIndexChecked;
1789 change = TRUE;
1791 if (state == CheckBitmapIndexDisabledUnchecked)
1793 state = CheckBitmapIndexUnchecked;
1794 change = TRUE;
1797 else
1799 if (state == CheckBitmapIndexChecked)
1801 state = CheckBitmapIndexDisabledChecked;
1802 change = TRUE;
1804 if (state == CheckBitmapIndexUnchecked)
1806 state = CheckBitmapIndexDisabledUnchecked;
1807 change = TRUE;
1810 if (change)
1812 LVITEMW item;
1814 item.state = INDEXTOSTATEIMAGEMASK(state);
1815 item.stateMask = LVIS_STATEIMAGEMASK;
1816 SendMessageW(list, LVM_SETITEMSTATE, i, (LPARAM)&item);
1821 typedef enum {
1822 PurposeEnableAll = 0,
1823 PurposeDisableAll,
1824 PurposeEnableSelected
1825 } PurposeSelection;
1827 static void select_purposes(HWND hwnd, PurposeSelection selection)
1829 HWND lv = GetDlgItem(hwnd, IDC_CERTIFICATE_USAGES);
1831 switch (selection)
1833 case PurposeEnableAll:
1834 case PurposeDisableAll:
1835 EnableWindow(lv, FALSE);
1836 redraw_states(lv, FALSE);
1837 EnableWindow(GetDlgItem(hwnd, IDC_ADD_PURPOSE), FALSE);
1838 break;
1839 case PurposeEnableSelected:
1840 EnableWindow(lv, TRUE);
1841 redraw_states(lv, TRUE);
1842 EnableWindow(GetDlgItem(hwnd, IDC_ADD_PURPOSE), TRUE);
1846 extern BOOL WINAPI WTHelperGetKnownUsages(DWORD action,
1847 PCCRYPT_OID_INFO **usages);
1849 static void add_known_usage(HWND lv, PCCRYPT_OID_INFO info)
1851 LVITEMW item;
1853 item.mask = LVIF_TEXT | LVIF_STATE | LVIF_PARAM;
1854 item.state = INDEXTOSTATEIMAGEMASK(CheckBitmapIndexDisabledChecked);
1855 item.stateMask = LVIS_STATEIMAGEMASK;
1856 item.iItem = SendMessageW(lv, LVM_GETITEMCOUNT, 0, 0);
1857 item.iSubItem = 0;
1858 item.lParam = (LPARAM)info;
1859 item.pszText = (LPWSTR)info->pwszName;
1860 SendMessageW(lv, LVM_INSERTITEMW, 0, (LPARAM)&item);
1863 static void show_cert_usages(HWND hwnd, struct detail_data *data)
1865 PCCERT_CONTEXT cert = data->pCertViewInfo->pCertContext;
1866 HWND lv = GetDlgItem(hwnd, IDC_CERTIFICATE_USAGES);
1867 PCERT_ENHKEY_USAGE usage;
1868 DWORD size;
1869 PCCRYPT_OID_INFO *usages;
1870 RECT rc;
1871 LVCOLUMNW column;
1872 PurposeSelection purposeSelection;
1874 GetWindowRect(lv, &rc);
1875 column.mask = LVCF_WIDTH;
1876 column.cx = rc.right - rc.left;
1877 SendMessageW(lv, LVM_INSERTCOLUMNW, 0, (LPARAM)&column);
1878 SendMessageW(lv, LVM_SETIMAGELIST, LVSIL_STATE, (LPARAM)data->imageList);
1880 /* Get enhanced key usage. Have to check for a property and an extension
1881 * separately, because CertGetEnhancedKeyUsage will succeed and return an
1882 * empty usage if neither is set. Unfortunately an empty usage implies
1883 * no usage is allowed, so we have to distinguish between the two cases.
1885 if (CertGetEnhancedKeyUsage(cert, CERT_FIND_PROP_ONLY_ENHKEY_USAGE_FLAG,
1886 NULL, &size))
1888 usage = HeapAlloc(GetProcessHeap(), 0, size);
1889 if (!CertGetEnhancedKeyUsage(cert,
1890 CERT_FIND_PROP_ONLY_ENHKEY_USAGE_FLAG, usage, &size))
1892 HeapFree(GetProcessHeap(), 0, usage);
1893 usage = NULL;
1895 else if (usage->cUsageIdentifier)
1896 purposeSelection = PurposeEnableSelected;
1897 else
1898 purposeSelection = PurposeDisableAll;
1900 else if (CertGetEnhancedKeyUsage(cert, CERT_FIND_EXT_ONLY_ENHKEY_USAGE_FLAG,
1901 NULL, &size))
1903 usage = HeapAlloc(GetProcessHeap(), 0, size);
1904 if (!CertGetEnhancedKeyUsage(cert,
1905 CERT_FIND_EXT_ONLY_ENHKEY_USAGE_FLAG, usage, &size))
1907 HeapFree(GetProcessHeap(), 0, usage);
1908 usage = NULL;
1910 else if (usage->cUsageIdentifier)
1911 purposeSelection = PurposeEnableAll;
1912 else
1913 purposeSelection = PurposeDisableAll;
1915 else
1917 purposeSelection = PurposeEnableAll;
1918 usage = NULL;
1920 if (usage)
1922 DWORD i;
1924 for (i = 0; i < usage->cUsageIdentifier; i++)
1926 PCCRYPT_OID_INFO info = CryptFindOIDInfo(CRYPT_OID_INFO_OID_KEY,
1927 usage->rgpszUsageIdentifier[i], CRYPT_ENHKEY_USAGE_OID_GROUP_ID);
1929 if (info)
1930 add_known_usage(lv, info);
1931 else
1932 add_purpose(hwnd, usage->rgpszUsageIdentifier[i]);
1934 HeapFree(GetProcessHeap(), 0, usage);
1936 else
1938 if (WTHelperGetKnownUsages(1, &usages))
1940 PCCRYPT_OID_INFO *ptr;
1942 for (ptr = usages; *ptr; ptr++)
1943 add_known_usage(lv, *ptr);
1944 WTHelperGetKnownUsages(2, &usages);
1947 select_purposes(hwnd, purposeSelection);
1948 SendMessageW(GetDlgItem(hwnd, IDC_ENABLE_ALL_PURPOSES + purposeSelection),
1949 BM_CLICK, 0, 0);
1952 static void set_general_cert_properties(HWND hwnd, struct detail_data *data)
1954 PCCERT_CONTEXT cert = data->pCertViewInfo->pCertContext;
1955 WCHAR *str;
1957 if ((str = get_cert_property_as_string(cert, CERT_FRIENDLY_NAME_PROP_ID)))
1959 SendMessageW(GetDlgItem(hwnd, IDC_FRIENDLY_NAME), WM_SETTEXT, 0,
1960 (LPARAM)str);
1961 HeapFree(GetProcessHeap(), 0, str);
1963 if ((str = get_cert_property_as_string(cert, CERT_DESCRIPTION_PROP_ID)))
1965 SendMessageW(GetDlgItem(hwnd, IDC_DESCRIPTION), WM_SETTEXT, 0,
1966 (LPARAM)str);
1967 HeapFree(GetProcessHeap(), 0, str);
1969 show_cert_usages(hwnd, data);
1972 static void toggle_usage(HWND hwnd, int iItem)
1974 LVITEMW item;
1975 int res;
1976 HWND lv = GetDlgItem(hwnd, IDC_CERTIFICATE_USAGES);
1978 item.mask = LVIF_STATE;
1979 item.iItem = iItem;
1980 item.iSubItem = 0;
1981 item.stateMask = LVIS_STATEIMAGEMASK;
1982 res = SendMessageW(lv, LVM_GETITEMW, 0, (LPARAM)&item);
1983 if (res)
1985 int state = item.state >> 12;
1987 item.state = INDEXTOSTATEIMAGEMASK(
1988 state == CheckBitmapIndexChecked ? CheckBitmapIndexUnchecked :
1989 CheckBitmapIndexChecked);
1990 SendMessageW(lv, LVM_SETITEMSTATE, iItem, (LPARAM)&item);
1994 static void set_cert_string_property(PCCERT_CONTEXT cert, DWORD prop,
1995 LPWSTR str)
1997 if (str && strlenW(str))
1999 CRYPT_DATA_BLOB blob;
2001 blob.pbData = (BYTE *)str;
2002 blob.cbData = (strlenW(str) + 1) * sizeof(WCHAR);
2003 CertSetCertificateContextProperty(cert, prop, 0, &blob);
2005 else
2006 CertSetCertificateContextProperty(cert, prop, 0, NULL);
2009 #define WM_REFRESH_VIEW WM_USER + 0
2011 static BOOL CALLBACK refresh_propsheet_pages(HWND hwnd, LPARAM lParam)
2013 if ((GetClassLongW(hwnd, GCW_ATOM) == WC_DIALOG))
2014 SendMessageW(hwnd, WM_REFRESH_VIEW, 0, 0);
2015 return TRUE;
2018 #define MAX_FRIENDLY_NAME 40
2019 #define MAX_DESCRIPTION 255
2021 static void apply_general_changes(HWND hwnd)
2023 WCHAR buf[MAX_DESCRIPTION + 1];
2024 struct detail_data *data =
2025 (struct detail_data *)GetWindowLongPtrW(hwnd, DWLP_USER);
2027 SendMessageW(GetDlgItem(hwnd, IDC_FRIENDLY_NAME), WM_GETTEXT,
2028 sizeof(buf) / sizeof(buf[0]), (LPARAM)buf);
2029 set_cert_string_property(data->pCertViewInfo->pCertContext,
2030 CERT_FRIENDLY_NAME_PROP_ID, buf);
2031 SendMessageW(GetDlgItem(hwnd, IDC_DESCRIPTION), WM_GETTEXT,
2032 sizeof(buf) / sizeof(buf[0]), (LPARAM)buf);
2033 set_cert_string_property(data->pCertViewInfo->pCertContext,
2034 CERT_DESCRIPTION_PROP_ID, buf);
2035 if (IsDlgButtonChecked(hwnd, IDC_ENABLE_ALL_PURPOSES))
2037 /* Setting a NULL usage removes the enhanced key usage property. */
2038 CertSetEnhancedKeyUsage(data->pCertViewInfo->pCertContext, NULL);
2040 else if (IsDlgButtonChecked(hwnd, IDC_DISABLE_ALL_PURPOSES))
2042 CERT_ENHKEY_USAGE usage = { 0, NULL };
2044 CertSetEnhancedKeyUsage(data->pCertViewInfo->pCertContext, &usage);
2046 else if (IsDlgButtonChecked(hwnd, IDC_ENABLE_SELECTED_PURPOSES))
2048 HWND lv = GetDlgItem(hwnd, IDC_CERTIFICATE_USAGES);
2049 CERT_ENHKEY_USAGE usage = { 0, NULL };
2050 int purposes = SendMessageW(lv, LVM_GETITEMCOUNT, 0, 0), i;
2051 LVITEMW item;
2053 item.mask = LVIF_STATE | LVIF_PARAM;
2054 item.iSubItem = 0;
2055 item.stateMask = LVIS_STATEIMAGEMASK;
2056 for (i = 0; i < purposes; i++)
2058 item.iItem = i;
2059 if (SendMessageW(lv, LVM_GETITEMW, 0, (LPARAM)&item))
2061 int state = item.state >> 12;
2063 if (state == CheckBitmapIndexChecked)
2065 CRYPT_OID_INFO *info = (CRYPT_OID_INFO *)item.lParam;
2067 if (usage.cUsageIdentifier)
2068 usage.rgpszUsageIdentifier =
2069 HeapReAlloc(GetProcessHeap(), 0,
2070 usage.rgpszUsageIdentifier,
2071 (usage.cUsageIdentifier + 1) * sizeof(LPSTR));
2072 else
2073 usage.rgpszUsageIdentifier =
2074 HeapAlloc(GetProcessHeap(), 0, sizeof(LPSTR));
2075 if (usage.rgpszUsageIdentifier)
2076 usage.rgpszUsageIdentifier[usage.cUsageIdentifier++] =
2077 (LPSTR)info->pszOID;
2081 CertSetEnhancedKeyUsage(data->pCertViewInfo->pCertContext, &usage);
2082 HeapFree(GetProcessHeap(), 0, usage.rgpszUsageIdentifier);
2084 EnumChildWindows(GetParent(GetParent(hwnd)), refresh_propsheet_pages, 0);
2085 if (data->pfPropertiesChanged)
2086 *data->pfPropertiesChanged = TRUE;
2089 static LRESULT CALLBACK cert_properties_general_dlg_proc(HWND hwnd, UINT msg,
2090 WPARAM wp, LPARAM lp)
2092 PROPSHEETPAGEW *page;
2093 struct detail_data *data;
2095 TRACE("(%p, %08x, %08lx, %08lx)\n", hwnd, msg, wp, lp);
2097 switch (msg)
2099 case WM_INITDIALOG:
2101 HWND description = GetDlgItem(hwnd, IDC_DESCRIPTION);
2103 page = (PROPSHEETPAGEW *)lp;
2104 data = (struct detail_data *)page->lParam;
2105 SendMessageW(GetDlgItem(hwnd, IDC_FRIENDLY_NAME), EM_SETLIMITTEXT,
2106 MAX_FRIENDLY_NAME, 0);
2107 SendMessageW(description, EM_SETLIMITTEXT, MAX_DESCRIPTION, 0);
2108 ShowScrollBar(description, SB_VERT, FALSE);
2109 set_general_cert_properties(hwnd, data);
2110 SetWindowLongPtrW(hwnd, DWLP_USER, (LPARAM)data);
2111 break;
2113 case WM_NOTIFY:
2115 NMHDR *hdr = (NMHDR *)lp;
2116 NMITEMACTIVATE *nm;
2118 switch (hdr->code)
2120 case NM_CLICK:
2121 nm = (NMITEMACTIVATE *)lp;
2122 toggle_usage(hwnd, nm->iItem);
2123 SendMessageW(GetParent(hwnd), PSM_CHANGED, (WPARAM)hwnd, 0);
2124 break;
2125 case PSN_APPLY:
2126 apply_general_changes(hwnd);
2127 break;
2129 break;
2131 case WM_COMMAND:
2132 switch (HIWORD(wp))
2134 case EN_CHANGE:
2135 SendMessageW(GetParent(hwnd), PSM_CHANGED, (WPARAM)hwnd, 0);
2136 if (LOWORD(wp) == IDC_DESCRIPTION)
2138 /* Show/hide scroll bar on description depending on how much
2139 * text it has.
2141 HWND description = GetDlgItem(hwnd, IDC_DESCRIPTION);
2142 int lines = SendMessageW(description, EM_GETLINECOUNT, 0, 0);
2144 ShowScrollBar(description, SB_VERT, lines > 1);
2146 break;
2147 case BN_CLICKED:
2148 switch (LOWORD(wp))
2150 case IDC_ADD_PURPOSE:
2151 if (DialogBoxParamW(hInstance,
2152 MAKEINTRESOURCEW(IDD_ADD_CERT_PURPOSE), hwnd,
2153 add_purpose_dlg_proc, (LPARAM)hwnd) == IDOK)
2154 SendMessageW(GetParent(hwnd), PSM_CHANGED, (WPARAM)hwnd, 0);
2155 break;
2156 case IDC_ENABLE_ALL_PURPOSES:
2157 case IDC_DISABLE_ALL_PURPOSES:
2158 case IDC_ENABLE_SELECTED_PURPOSES:
2159 SendMessageW(GetParent(hwnd), PSM_CHANGED, (WPARAM)hwnd, 0);
2160 select_purposes(hwnd, LOWORD(wp) - IDC_ENABLE_ALL_PURPOSES);
2161 break;
2163 break;
2165 break;
2167 return 0;
2170 static UINT CALLBACK cert_properties_general_callback(HWND hwnd, UINT msg,
2171 PROPSHEETPAGEW *page)
2173 HWND lv;
2174 int cItem, i;
2176 switch (msg)
2178 case PSPCB_RELEASE:
2179 lv = GetDlgItem(hwnd, IDC_CERTIFICATE_USAGES);
2180 cItem = SendMessageW(lv, LVM_GETITEMCOUNT, 0, 0);
2181 for (i = 0; i < cItem; i++)
2183 LVITEMW item;
2185 item.mask = LVIF_PARAM;
2186 item.iItem = i;
2187 item.iSubItem = 0;
2188 if (SendMessageW(lv, LVM_GETITEMW, 0, (LPARAM)&item) && item.lParam)
2190 PCRYPT_OID_INFO info = (PCRYPT_OID_INFO)item.lParam;
2192 if (info->cbSize == sizeof(CRYPT_OID_INFO) && !info->dwGroupId)
2194 HeapFree(GetProcessHeap(), 0, (LPSTR)info->pszOID);
2195 HeapFree(GetProcessHeap(), 0, info);
2199 break;
2201 return 1;
2204 static void show_edit_cert_properties_dialog(HWND parent,
2205 struct detail_data *data)
2207 PROPSHEETHEADERW hdr;
2208 PROPSHEETPAGEW page; /* FIXME: need to add a cross-certificate page */
2210 TRACE("(%p)\n", data);
2212 memset(&page, 0, sizeof(PROPSHEETPAGEW));
2213 page.dwSize = sizeof(page);
2214 page.dwFlags = PSP_USECALLBACK;
2215 page.pfnCallback = cert_properties_general_callback;
2216 page.hInstance = hInstance;
2217 page.u.pszTemplate = MAKEINTRESOURCEW(IDD_CERT_PROPERTIES_GENERAL);
2218 page.pfnDlgProc = cert_properties_general_dlg_proc;
2219 page.lParam = (LPARAM)data;
2221 memset(&hdr, 0, sizeof(hdr));
2222 hdr.dwSize = sizeof(hdr);
2223 hdr.hwndParent = parent;
2224 hdr.dwFlags = PSH_PROPSHEETPAGE;
2225 hdr.hInstance = hInstance;
2226 hdr.pszCaption = MAKEINTRESOURCEW(IDS_CERTIFICATE_PROPERTIES);
2227 hdr.u3.ppsp = &page;
2228 hdr.nPages = 1;
2229 PropertySheetW(&hdr);
2232 static void free_detail_fields(struct detail_data *data)
2234 DWORD i;
2236 for (i = 0; i < data->cFields; i++)
2237 HeapFree(GetProcessHeap(), 0, data->fields[i].detailed_value);
2238 HeapFree(GetProcessHeap(), 0, data->fields);
2239 data->fields = NULL;
2240 data->cFields = 0;
2243 static void refresh_details_view(HWND hwnd)
2245 HWND cb = GetDlgItem(hwnd, IDC_DETAIL_SELECT);
2246 int curSel;
2247 struct detail_data *data;
2249 curSel = SendMessageW(cb, CB_GETCURSEL, 0, 0);
2250 /* Actually, any index will do, since they all store the same data value */
2251 data = (struct detail_data *)SendMessageW(cb, CB_GETITEMDATA, curSel, 0);
2252 free_detail_fields(data);
2253 set_fields_selection(hwnd, data, curSel);
2256 static LRESULT CALLBACK detail_dlg_proc(HWND hwnd, UINT msg, WPARAM wp,
2257 LPARAM lp)
2259 PROPSHEETPAGEW *page;
2260 struct detail_data *data;
2262 TRACE("(%p, %08x, %08lx, %08lx)\n", hwnd, msg, wp, lp);
2264 switch (msg)
2266 case WM_INITDIALOG:
2267 page = (PROPSHEETPAGEW *)lp;
2268 data = (struct detail_data *)page->lParam;
2269 create_cert_details_list(hwnd, data);
2270 if (!(data->pCertViewInfo->dwFlags & CRYPTUI_ENABLE_EDITPROPERTIES))
2271 EnableWindow(GetDlgItem(hwnd, IDC_EDITPROPERTIES), FALSE);
2272 if (data->pCertViewInfo->dwFlags & CRYPTUI_DISABLE_EXPORT)
2273 EnableWindow(GetDlgItem(hwnd, IDC_EXPORT), FALSE);
2274 break;
2275 case WM_NOTIFY:
2277 NMITEMACTIVATE *nm;
2278 HWND list = GetDlgItem(hwnd, IDC_DETAIL_LIST);
2280 nm = (NMITEMACTIVATE*)lp;
2281 if (nm->hdr.hwndFrom == list && nm->uNewState & LVN_ITEMACTIVATE
2282 && nm->hdr.code == LVN_ITEMCHANGED)
2284 data = (struct detail_data *)nm->lParam;
2285 if (nm->iItem >= 0 && data && nm->iItem < data->cFields)
2287 WCHAR buf[MAX_STRING_LEN], *val = NULL;
2288 HWND valueCtl = GetDlgItem(hwnd, IDC_DETAIL_VALUE);
2290 if (data->fields[nm->iItem].create)
2291 val = data->fields[nm->iItem].create(
2292 data->pCertViewInfo->pCertContext,
2293 data->fields[nm->iItem].param);
2294 else
2296 LVITEMW item;
2297 int res;
2299 item.cchTextMax = sizeof(buf) / sizeof(buf[0]);
2300 item.mask = LVIF_TEXT;
2301 item.pszText = buf;
2302 item.iItem = nm->iItem;
2303 item.iSubItem = 1;
2304 res = SendMessageW(list, LVM_GETITEMW, 0, (LPARAM)&item);
2305 if (res)
2306 val = buf;
2308 /* Select all the text in the control, the next update will
2309 * replace it
2311 SendMessageW(valueCtl, EM_SETSEL, 0, -1);
2312 add_unformatted_text_to_control(valueCtl, val,
2313 val ? strlenW(val) : 0);
2314 if (val != buf)
2315 HeapFree(GetProcessHeap(), 0, val);
2318 break;
2320 case WM_COMMAND:
2321 switch (wp)
2323 case IDC_EXPORT:
2324 FIXME("call CryptUIWizExport\n");
2325 break;
2326 case IDC_EDITPROPERTIES:
2328 HWND cb = GetDlgItem(hwnd, IDC_DETAIL_SELECT);
2329 int curSel;
2331 curSel = SendMessageW(cb, CB_GETCURSEL, 0, 0);
2332 /* Actually, any index will do, since they all store the same
2333 * data value
2335 data = (struct detail_data *)SendMessageW(cb, CB_GETITEMDATA,
2336 curSel, 0);
2337 show_edit_cert_properties_dialog(GetParent(hwnd), data);
2338 break;
2340 case ((CBN_SELCHANGE << 16) | IDC_DETAIL_SELECT):
2341 refresh_details_view(hwnd);
2342 break;
2344 break;
2345 case WM_REFRESH_VIEW:
2346 refresh_details_view(hwnd);
2347 break;
2349 return 0;
2352 static UINT CALLBACK detail_callback(HWND hwnd, UINT msg,
2353 PROPSHEETPAGEW *page)
2355 struct detail_data *data;
2357 switch (msg)
2359 case PSPCB_RELEASE:
2360 data = (struct detail_data *)page->lParam;
2361 free_detail_fields(data);
2362 ImageList_Destroy(data->imageList);
2363 HeapFree(GetProcessHeap(), 0, data);
2364 break;
2366 return 0;
2369 static BOOL init_detail_page(PCCRYPTUI_VIEWCERTIFICATE_STRUCTW pCertViewInfo,
2370 BOOL *pfPropertiesChanged, PROPSHEETPAGEW *page)
2372 BOOL ret;
2373 struct detail_data *data = HeapAlloc(GetProcessHeap(), 0,
2374 sizeof(struct detail_data));
2376 if (data)
2378 data->pCertViewInfo = pCertViewInfo;
2379 data->pfPropertiesChanged = pfPropertiesChanged;
2380 data->cFields = 0;
2381 data->fields = NULL;
2382 data->imageList = ImageList_Create(16, 16, ILC_COLOR4 | ILC_MASK, 4, 0);
2383 if (data->imageList)
2385 HBITMAP bmp;
2386 COLORREF backColor = RGB(255, 0, 255);
2388 bmp = LoadBitmapW(hInstance, MAKEINTRESOURCEW(IDB_CHECKS));
2389 ImageList_AddMasked(data->imageList, bmp, backColor);
2390 DeleteObject(bmp);
2391 ImageList_SetBkColor(data->imageList, CLR_NONE);
2393 memset(page, 0, sizeof(PROPSHEETPAGEW));
2394 page->dwSize = sizeof(PROPSHEETPAGEW);
2395 page->dwFlags = PSP_USECALLBACK;
2396 page->pfnCallback = detail_callback;
2397 page->hInstance = hInstance;
2398 page->u.pszTemplate = MAKEINTRESOURCEW(IDD_DETAIL);
2399 page->pfnDlgProc = detail_dlg_proc;
2400 page->lParam = (LPARAM)data;
2401 ret = TRUE;
2403 else
2404 ret = FALSE;
2405 return ret;
2408 struct hierarchy_data
2410 PCCRYPTUI_VIEWCERTIFICATE_STRUCTW pCertViewInfo;
2411 HIMAGELIST imageList;
2412 DWORD selectedCert;
2415 static LPARAM index_to_lparam(struct hierarchy_data *data, DWORD index)
2417 CRYPT_PROVIDER_SGNR *provSigner = WTHelperGetProvSignerFromChain(
2418 (CRYPT_PROVIDER_DATA *)data->pCertViewInfo->u.pCryptProviderData,
2419 data->pCertViewInfo->idxSigner, data->pCertViewInfo->fCounterSigner,
2420 data->pCertViewInfo->idxCounterSigner);
2422 /* Takes advantage of the fact that a pointer is 32-bit aligned, and
2423 * therefore always even.
2425 if (index == provSigner->csCertChain - 1)
2426 return (LPARAM)data;
2427 return index << 1 | 1;
2430 static inline DWORD lparam_to_index(struct hierarchy_data *data, LPARAM lp)
2432 CRYPT_PROVIDER_SGNR *provSigner = WTHelperGetProvSignerFromChain(
2433 (CRYPT_PROVIDER_DATA *)data->pCertViewInfo->u.pCryptProviderData,
2434 data->pCertViewInfo->idxSigner, data->pCertViewInfo->fCounterSigner,
2435 data->pCertViewInfo->idxCounterSigner);
2437 if (!(lp & 1))
2438 return provSigner->csCertChain - 1;
2439 return lp >> 1;
2442 static struct hierarchy_data *get_hierarchy_data_from_tree_item(HWND tree,
2443 HTREEITEM hItem)
2445 struct hierarchy_data *data = NULL;
2446 HTREEITEM root = NULL;
2448 do {
2449 HTREEITEM parent = (HTREEITEM)SendMessageW(tree, TVM_GETNEXTITEM,
2450 TVGN_PARENT, (LPARAM)hItem);
2452 if (!parent)
2453 root = hItem;
2454 hItem = parent;
2455 } while (hItem);
2456 if (root)
2458 TVITEMW item;
2460 item.mask = TVIF_PARAM;
2461 item.hItem = root;
2462 SendMessageW(tree, TVM_GETITEMW, 0, (LPARAM)&item);
2463 data = (struct hierarchy_data *)item.lParam;
2465 return data;
2468 static WCHAR *get_cert_display_name(PCCERT_CONTEXT cert)
2470 WCHAR *name = get_cert_property_as_string(cert, CERT_FRIENDLY_NAME_PROP_ID);
2472 if (!name)
2473 name = get_cert_name_string(cert, CERT_NAME_SIMPLE_DISPLAY_TYPE, 0);
2474 return name;
2477 static void show_cert_chain(HWND hwnd, struct hierarchy_data *data)
2479 HWND tree = GetDlgItem(hwnd, IDC_CERTPATH);
2480 CRYPT_PROVIDER_SGNR *provSigner = WTHelperGetProvSignerFromChain(
2481 (CRYPT_PROVIDER_DATA *)data->pCertViewInfo->u.pCryptProviderData,
2482 data->pCertViewInfo->idxSigner, data->pCertViewInfo->fCounterSigner,
2483 data->pCertViewInfo->idxCounterSigner);
2484 DWORD i;
2485 HTREEITEM parent = NULL;
2487 SendMessageW(tree, TVM_SETIMAGELIST, TVSIL_NORMAL, (LPARAM)data->imageList);
2488 for (i = provSigner->csCertChain; i; i--)
2490 LPWSTR name;
2492 name = get_cert_display_name(provSigner->pasCertChain[i - 1].pCert);
2493 if (name)
2495 TVINSERTSTRUCTW tvis;
2497 tvis.hParent = parent;
2498 tvis.hInsertAfter = TVI_LAST;
2499 tvis.u.item.mask = TVIF_TEXT | TVIF_STATE | TVIF_IMAGE |
2500 TVIF_SELECTEDIMAGE | TVIF_PARAM;
2501 tvis.u.item.pszText = name;
2502 tvis.u.item.state = TVIS_EXPANDED;
2503 tvis.u.item.stateMask = TVIS_EXPANDED;
2504 if (i == 1 &&
2505 (provSigner->pChainContext->TrustStatus.dwErrorStatus &
2506 CERT_TRUST_IS_PARTIAL_CHAIN))
2508 /* The root of the chain has a special case: if the chain is
2509 * a partial chain, the icon is a warning icon rather than an
2510 * error icon.
2512 tvis.u.item.iImage = 2;
2514 else if (provSigner->pasCertChain[i - 1].pChainElement->TrustStatus.
2515 dwErrorStatus == 0)
2516 tvis.u.item.iImage = 0;
2517 else
2518 tvis.u.item.iImage = 1;
2519 tvis.u.item.iSelectedImage = tvis.u.item.iImage;
2520 tvis.u.item.lParam = index_to_lparam(data, i - 1);
2521 parent = (HTREEITEM)SendMessageW(tree, TVM_INSERTITEMW, 0,
2522 (LPARAM)&tvis);
2523 HeapFree(GetProcessHeap(), 0, name);
2528 static void set_certificate_status(HWND hwnd, CRYPT_PROVIDER_CERT *cert)
2530 /* Select all the text in the control, the next update will replace it */
2531 SendMessageW(hwnd, EM_SETSEL, 0, -1);
2532 /* Set the highest priority error messages first. */
2533 if (!(cert->dwConfidence & CERT_CONFIDENCE_SIG))
2534 add_string_resource_to_control(hwnd, IDS_CERTIFICATE_BAD_SIGNATURE);
2535 else if (!(cert->dwConfidence & CERT_CONFIDENCE_TIME))
2536 add_string_resource_to_control(hwnd, IDS_CERTIFICATE_BAD_TIME);
2537 else if (!(cert->dwConfidence & CERT_CONFIDENCE_TIMENEST))
2538 add_string_resource_to_control(hwnd, IDS_CERTIFICATE_BAD_TIMENEST);
2539 else if (cert->dwRevokedReason)
2540 add_string_resource_to_control(hwnd, IDS_CERTIFICATE_REVOKED);
2541 else
2542 add_string_resource_to_control(hwnd, IDS_CERTIFICATE_VALID);
2545 static void set_certificate_status_for_end_cert(HWND hwnd,
2546 PCCRYPTUI_VIEWCERTIFICATE_STRUCTW pCertViewInfo)
2548 HWND status = GetDlgItem(hwnd, IDC_CERTIFICATESTATUSTEXT);
2549 CRYPT_PROVIDER_SGNR *provSigner = WTHelperGetProvSignerFromChain(
2550 (CRYPT_PROVIDER_DATA *)pCertViewInfo->u.pCryptProviderData,
2551 pCertViewInfo->idxSigner, pCertViewInfo->fCounterSigner,
2552 pCertViewInfo->idxCounterSigner);
2553 CRYPT_PROVIDER_CERT *provCert = WTHelperGetProvCertFromChain(provSigner,
2554 pCertViewInfo->idxCert);
2556 set_certificate_status(status, provCert);
2559 static void show_cert_hierarchy(HWND hwnd, struct hierarchy_data *data)
2561 /* Disable view certificate button until a certificate is selected */
2562 EnableWindow(GetDlgItem(hwnd, IDC_VIEWCERTIFICATE), FALSE);
2563 show_cert_chain(hwnd, data);
2564 set_certificate_status_for_end_cert(hwnd, data->pCertViewInfo);
2567 static void show_dialog_for_selected_cert(HWND hwnd)
2569 HWND tree = GetDlgItem(hwnd, IDC_CERTPATH);
2570 TVITEMW item;
2571 struct hierarchy_data *data;
2572 DWORD selection;
2574 memset(&item, 0, sizeof(item));
2575 item.mask = TVIF_HANDLE | TVIF_PARAM;
2576 item.hItem = (HTREEITEM)SendMessageW(tree, TVM_GETNEXTITEM, TVGN_CARET,
2577 (LPARAM)NULL);
2578 SendMessageW(tree, TVM_GETITEMW, 0, (LPARAM)&item);
2579 data = get_hierarchy_data_from_tree_item(tree, item.hItem);
2580 selection = lparam_to_index(data, item.lParam);
2581 if (selection != 0)
2583 CRYPT_PROVIDER_SGNR *provSigner;
2584 CRYPTUI_VIEWCERTIFICATE_STRUCTW viewInfo;
2585 BOOL changed = FALSE;
2587 provSigner = WTHelperGetProvSignerFromChain(
2588 (CRYPT_PROVIDER_DATA *)data->pCertViewInfo->u.pCryptProviderData,
2589 data->pCertViewInfo->idxSigner,
2590 data->pCertViewInfo->fCounterSigner,
2591 data->pCertViewInfo->idxCounterSigner);
2592 memset(&viewInfo, 0, sizeof(viewInfo));
2593 viewInfo.dwSize = sizeof(viewInfo);
2594 viewInfo.dwFlags = data->pCertViewInfo->dwFlags;
2595 viewInfo.szTitle = data->pCertViewInfo->szTitle;
2596 viewInfo.pCertContext = provSigner->pasCertChain[selection].pCert;
2597 viewInfo.cStores = data->pCertViewInfo->cStores;
2598 viewInfo.rghStores = data->pCertViewInfo->rghStores;
2599 viewInfo.cPropSheetPages = data->pCertViewInfo->cPropSheetPages;
2600 viewInfo.rgPropSheetPages = data->pCertViewInfo->rgPropSheetPages;
2601 viewInfo.nStartPage = data->pCertViewInfo->nStartPage;
2602 CryptUIDlgViewCertificateW(&viewInfo, &changed);
2603 if (changed)
2605 /* Delete the contents of the tree */
2606 SendMessageW(tree, TVM_DELETEITEM, 0, (LPARAM)TVI_ROOT);
2607 /* Reinitialize the tree */
2608 show_cert_hierarchy(hwnd, data);
2613 static LRESULT CALLBACK hierarchy_dlg_proc(HWND hwnd, UINT msg, WPARAM wp,
2614 LPARAM lp)
2616 PROPSHEETPAGEW *page;
2617 struct hierarchy_data *data;
2618 LRESULT ret = 0;
2619 HWND tree = GetDlgItem(hwnd, IDC_CERTPATH);
2621 TRACE("(%p, %08x, %08lx, %08lx)\n", hwnd, msg, wp, lp);
2623 switch (msg)
2625 case WM_INITDIALOG:
2626 page = (PROPSHEETPAGEW *)lp;
2627 data = (struct hierarchy_data *)page->lParam;
2628 show_cert_hierarchy(hwnd, data);
2629 break;
2630 case WM_NOTIFY:
2632 NMHDR *hdr;
2634 hdr = (NMHDR *)lp;
2635 switch (hdr->code)
2637 case TVN_SELCHANGEDW:
2639 NMTREEVIEWW *nm = (NMTREEVIEWW*)lp;
2640 DWORD selection;
2641 CRYPT_PROVIDER_SGNR *provSigner;
2643 data = get_hierarchy_data_from_tree_item(tree, nm->itemNew.hItem);
2644 selection = lparam_to_index(data, nm->itemNew.lParam);
2645 provSigner = WTHelperGetProvSignerFromChain(
2646 (CRYPT_PROVIDER_DATA *)data->pCertViewInfo->u.pCryptProviderData,
2647 data->pCertViewInfo->idxSigner,
2648 data->pCertViewInfo->fCounterSigner,
2649 data->pCertViewInfo->idxCounterSigner);
2650 EnableWindow(GetDlgItem(hwnd, IDC_VIEWCERTIFICATE), selection != 0);
2651 set_certificate_status(GetDlgItem(hwnd, IDC_CERTIFICATESTATUSTEXT),
2652 &provSigner->pasCertChain[selection]);
2653 break;
2655 case NM_DBLCLK:
2656 show_dialog_for_selected_cert(hwnd);
2657 SetWindowLongPtrW(hwnd, DWLP_MSGRESULT, 1);
2658 ret = 1;
2659 break;
2661 break;
2663 case WM_COMMAND:
2664 switch (wp)
2666 case IDC_VIEWCERTIFICATE:
2667 show_dialog_for_selected_cert(hwnd);
2668 break;
2670 break;
2671 case WM_REFRESH_VIEW:
2673 TVITEMW item;
2675 /* Get hierarchy data */
2676 memset(&item, 0, sizeof(item));
2677 item.mask = TVIF_HANDLE | TVIF_PARAM;
2678 item.hItem = (HTREEITEM)SendMessageW(tree, TVM_GETNEXTITEM, TVGN_ROOT,
2679 (LPARAM)NULL);
2680 data = get_hierarchy_data_from_tree_item(tree, item.hItem);
2681 /* Delete the contents of the tree */
2682 SendMessageW(tree, TVM_DELETEITEM, 0, (LPARAM)TVI_ROOT);
2683 /* Reinitialize the tree */
2684 show_cert_hierarchy(hwnd, data);
2685 break;
2688 return ret;
2691 static UINT CALLBACK hierarchy_callback(HWND hwnd, UINT msg,
2692 PROPSHEETPAGEW *page)
2694 struct hierarchy_data *data;
2696 switch (msg)
2698 case PSPCB_RELEASE:
2699 data = (struct hierarchy_data *)page->lParam;
2700 ImageList_Destroy(data->imageList);
2701 HeapFree(GetProcessHeap(), 0, data);
2702 break;
2704 return 0;
2707 static BOOL init_hierarchy_page(PCCRYPTUI_VIEWCERTIFICATE_STRUCTW pCertViewInfo,
2708 PROPSHEETPAGEW *page)
2710 struct hierarchy_data *data = HeapAlloc(GetProcessHeap(), 0,
2711 sizeof(struct hierarchy_data));
2712 BOOL ret = FALSE;
2714 if (data)
2716 data->imageList = ImageList_Create(16, 16, ILC_COLOR4 | ILC_MASK, 2, 0);
2717 if (data->imageList)
2719 HBITMAP bmp;
2720 COLORREF backColor = RGB(255, 0, 255);
2722 data->pCertViewInfo = pCertViewInfo;
2723 data->selectedCert = 0xffffffff;
2725 bmp = LoadBitmapW(hInstance, MAKEINTRESOURCEW(IDB_SMALL_ICONS));
2726 ImageList_AddMasked(data->imageList, bmp, backColor);
2727 DeleteObject(bmp);
2728 ImageList_SetBkColor(data->imageList, CLR_NONE);
2730 memset(page, 0, sizeof(PROPSHEETPAGEW));
2731 page->dwSize = sizeof(PROPSHEETPAGEW);
2732 page->dwFlags = PSP_USECALLBACK;
2733 page->hInstance = hInstance;
2734 page->u.pszTemplate = MAKEINTRESOURCEW(IDD_HIERARCHY);
2735 page->pfnDlgProc = hierarchy_dlg_proc;
2736 page->lParam = (LPARAM)data;
2737 page->pfnCallback = hierarchy_callback;
2738 ret = TRUE;
2740 else
2741 HeapFree(GetProcessHeap(), 0, data);
2743 return ret;
2746 static int CALLBACK cert_prop_sheet_proc(HWND hwnd, UINT msg, LPARAM lp)
2748 RECT rc;
2749 POINT topLeft;
2751 TRACE("(%p, %08x, %08lx)\n", hwnd, msg, lp);
2753 switch (msg)
2755 case PSCB_INITIALIZED:
2756 /* Get cancel button's position.. */
2757 GetWindowRect(GetDlgItem(hwnd, IDCANCEL), &rc);
2758 topLeft.x = rc.left;
2759 topLeft.y = rc.top;
2760 ScreenToClient(hwnd, &topLeft);
2761 /* hide the cancel button.. */
2762 ShowWindow(GetDlgItem(hwnd, IDCANCEL), FALSE);
2763 /* get the OK button's size.. */
2764 GetWindowRect(GetDlgItem(hwnd, IDOK), &rc);
2765 /* and move the OK button to the cancel button's original position. */
2766 MoveWindow(GetDlgItem(hwnd, IDOK), topLeft.x, topLeft.y,
2767 rc.right - rc.left, rc.bottom - rc.top, FALSE);
2768 GetWindowRect(GetDlgItem(hwnd, IDOK), &rc);
2769 break;
2771 return 0;
2774 static BOOL show_cert_dialog(PCCRYPTUI_VIEWCERTIFICATE_STRUCTW pCertViewInfo,
2775 CRYPT_PROVIDER_CERT *provCert, BOOL *pfPropertiesChanged)
2777 static const WCHAR riched[] = { 'r','i','c','h','e','d','2','0',0 };
2778 DWORD nPages;
2779 PROPSHEETPAGEW *pages;
2780 BOOL ret = FALSE;
2781 HMODULE lib = LoadLibraryW(riched);
2783 nPages = pCertViewInfo->cPropSheetPages + 1; /* one for the General tab */
2784 if (!(pCertViewInfo->dwFlags & CRYPTUI_HIDE_DETAILPAGE))
2785 nPages++;
2786 if (!(pCertViewInfo->dwFlags & CRYPTUI_HIDE_HIERARCHYPAGE))
2787 nPages++;
2788 pages = HeapAlloc(GetProcessHeap(), 0, nPages * sizeof(PROPSHEETPAGEW));
2789 if (pages)
2791 PROPSHEETHEADERW hdr;
2792 CRYPTUI_INITDIALOG_STRUCT *init = NULL;
2793 DWORD i;
2795 memset(&hdr, 0, sizeof(hdr));
2796 hdr.dwSize = sizeof(hdr);
2797 hdr.dwFlags = PSH_NOAPPLYNOW | PSH_PROPSHEETPAGE | PSH_USECALLBACK;
2798 hdr.hInstance = hInstance;
2799 if (pCertViewInfo->szTitle)
2800 hdr.pszCaption = pCertViewInfo->szTitle;
2801 else
2802 hdr.pszCaption = MAKEINTRESOURCEW(IDS_CERTIFICATE);
2803 init_general_page(pCertViewInfo, &pages[hdr.nPages++]);
2804 if (!(pCertViewInfo->dwFlags & CRYPTUI_HIDE_DETAILPAGE))
2806 if (init_detail_page(pCertViewInfo, pfPropertiesChanged,
2807 &pages[hdr.nPages]))
2808 hdr.nPages++;
2810 if (!(pCertViewInfo->dwFlags & CRYPTUI_HIDE_HIERARCHYPAGE))
2812 if (init_hierarchy_page(pCertViewInfo, &pages[hdr.nPages]))
2813 hdr.nPages++;
2815 /* Copy each additional page, and create the init dialog struct for it
2817 if (pCertViewInfo->cPropSheetPages)
2819 init = HeapAlloc(GetProcessHeap(), 0,
2820 pCertViewInfo->cPropSheetPages *
2821 sizeof(CRYPTUI_INITDIALOG_STRUCT));
2822 if (init)
2824 for (i = 0; i < pCertViewInfo->cPropSheetPages; i++)
2826 memcpy(&pages[hdr.nPages + i],
2827 &pCertViewInfo->rgPropSheetPages[i],
2828 sizeof(PROPSHEETPAGEW));
2829 init[i].lParam = pCertViewInfo->rgPropSheetPages[i].lParam;
2830 init[i].pCertContext = pCertViewInfo->pCertContext;
2831 pages[hdr.nPages + i].lParam = (LPARAM)&init[i];
2833 if (pCertViewInfo->nStartPage & 0x8000)
2835 /* Start page index is relative to the number of default
2836 * pages
2838 hdr.u2.nStartPage = pCertViewInfo->nStartPage + hdr.nPages;
2840 else
2841 hdr.u2.nStartPage = pCertViewInfo->nStartPage;
2842 hdr.nPages = nPages;
2843 ret = TRUE;
2845 else
2846 SetLastError(ERROR_OUTOFMEMORY);
2848 else
2850 /* Ignore the relative flag if there aren't any additional pages */
2851 hdr.u2.nStartPage = pCertViewInfo->nStartPage & 0x7fff;
2852 ret = TRUE;
2854 if (ret)
2856 INT_PTR l;
2858 hdr.u3.ppsp = pages;
2859 hdr.pfnCallback = cert_prop_sheet_proc;
2860 l = PropertySheetW(&hdr);
2861 if (l == 0)
2863 SetLastError(ERROR_CANCELLED);
2864 ret = FALSE;
2867 HeapFree(GetProcessHeap(), 0, init);
2868 HeapFree(GetProcessHeap(), 0, pages);
2870 else
2871 SetLastError(ERROR_OUTOFMEMORY);
2872 FreeLibrary(lib);
2873 return ret;
2876 /***********************************************************************
2877 * CryptUIDlgViewCertificateW (CRYPTUI.@)
2879 BOOL WINAPI CryptUIDlgViewCertificateW(
2880 PCCRYPTUI_VIEWCERTIFICATE_STRUCTW pCertViewInfo, BOOL *pfPropertiesChanged)
2882 static GUID generic_cert_verify = WINTRUST_ACTION_GENERIC_CERT_VERIFY;
2883 CRYPTUI_VIEWCERTIFICATE_STRUCTW viewInfo;
2884 WINTRUST_DATA wvt;
2885 WINTRUST_CERT_INFO cert;
2886 BOOL ret = FALSE;
2887 CRYPT_PROVIDER_SGNR *signer;
2888 CRYPT_PROVIDER_CERT *provCert = NULL;
2890 TRACE("(%p, %p)\n", pCertViewInfo, pfPropertiesChanged);
2892 if (pCertViewInfo->dwSize != sizeof(CRYPTUI_VIEWCERTIFICATE_STRUCTW))
2894 SetLastError(ERROR_INVALID_PARAMETER);
2895 return FALSE;
2897 /* Make a local copy in case we have to call WinVerifyTrust ourselves */
2898 memcpy(&viewInfo, pCertViewInfo, sizeof(viewInfo));
2899 if (!viewInfo.u.hWVTStateData)
2901 memset(&wvt, 0, sizeof(wvt));
2902 wvt.cbStruct = sizeof(wvt);
2903 wvt.dwUIChoice = WTD_UI_NONE;
2904 if (viewInfo.dwFlags &
2905 CRYPTUI_ENABLE_REVOCATION_CHECK_CHAIN_EXCLUDE_ROOT)
2906 wvt.fdwRevocationChecks |= WTD_REVOCATION_CHECK_CHAIN_EXCLUDE_ROOT;
2907 if (viewInfo.dwFlags & CRYPTUI_ENABLE_REVOCATION_CHECK_END_CERT)
2908 wvt.fdwRevocationChecks |= WTD_REVOCATION_CHECK_END_CERT;
2909 if (viewInfo.dwFlags & CRYPTUI_ENABLE_REVOCATION_CHECK_CHAIN)
2910 wvt.fdwRevocationChecks |= WTD_REVOCATION_CHECK_CHAIN;
2911 wvt.dwUnionChoice = WTD_CHOICE_CERT;
2912 memset(&cert, 0, sizeof(cert));
2913 cert.cbStruct = sizeof(cert);
2914 cert.psCertContext = (CERT_CONTEXT *)viewInfo.pCertContext;
2915 cert.chStores = viewInfo.cStores;
2916 cert.pahStores = viewInfo.rghStores;
2917 wvt.u.pCert = &cert;
2918 wvt.dwStateAction = WTD_STATEACTION_VERIFY;
2919 WinVerifyTrust(NULL, &generic_cert_verify, &wvt);
2920 viewInfo.u.pCryptProviderData =
2921 WTHelperProvDataFromStateData(wvt.hWVTStateData);
2922 signer = WTHelperGetProvSignerFromChain(
2923 (CRYPT_PROVIDER_DATA *)viewInfo.u.pCryptProviderData, 0, FALSE, 0);
2924 provCert = WTHelperGetProvCertFromChain(signer, 0);
2925 ret = TRUE;
2927 else
2929 viewInfo.u.pCryptProviderData =
2930 WTHelperProvDataFromStateData(viewInfo.u.hWVTStateData);
2931 signer = WTHelperGetProvSignerFromChain(
2932 (CRYPT_PROVIDER_DATA *)viewInfo.u.pCryptProviderData,
2933 viewInfo.idxSigner, viewInfo.fCounterSigner,
2934 viewInfo.idxCounterSigner);
2935 provCert = WTHelperGetProvCertFromChain(signer, viewInfo.idxCert);
2936 ret = TRUE;
2938 if (ret)
2940 ret = show_cert_dialog(&viewInfo, provCert, pfPropertiesChanged);
2941 if (!viewInfo.u.hWVTStateData)
2943 wvt.dwStateAction = WTD_STATEACTION_CLOSE;
2944 WinVerifyTrust(NULL, &generic_cert_verify, &wvt);
2947 return ret;
2950 /***********************************************************************
2951 * CryptUIDlgViewContext (CRYPTUI.@)
2953 BOOL WINAPI CryptUIDlgViewContext(DWORD dwContextType, LPVOID pvContext,
2954 HWND hwnd, LPCWSTR pwszTitle, DWORD dwFlags, LPVOID pvReserved)
2956 BOOL ret;
2958 TRACE("(%d, %p, %p, %s, %08x, %p)\n", dwContextType, pvContext, hwnd,
2959 debugstr_w(pwszTitle), dwFlags, pvReserved);
2961 switch (dwContextType)
2963 case CERT_STORE_CERTIFICATE_CONTEXT:
2965 CRYPTUI_VIEWCERTIFICATE_STRUCTW viewInfo;
2967 memset(&viewInfo, 0, sizeof(viewInfo));
2968 viewInfo.dwSize = sizeof(viewInfo);
2969 viewInfo.hwndParent = hwnd;
2970 viewInfo.szTitle = pwszTitle;
2971 viewInfo.pCertContext = pvContext;
2972 ret = CryptUIDlgViewCertificateW(&viewInfo, NULL);
2973 break;
2975 default:
2976 FIXME("unimplemented for context type %d\n", dwContextType);
2977 SetLastError(E_INVALIDARG);
2978 ret = FALSE;
2980 return ret;
2983 static PCCERT_CONTEXT make_cert_from_file(LPCWSTR fileName)
2985 HANDLE file;
2986 DWORD size, encoding = X509_ASN_ENCODING | PKCS_7_ASN_ENCODING;
2987 BYTE *buffer;
2988 PCCERT_CONTEXT cert;
2990 file = CreateFileW(fileName, GENERIC_READ, FILE_SHARE_READ, NULL,
2991 OPEN_EXISTING, 0, NULL);
2992 if (file == INVALID_HANDLE_VALUE)
2994 WARN("can't open certificate file %s\n", debugstr_w(fileName));
2995 return NULL;
2997 if ((size = GetFileSize(file, NULL)))
2999 if ((buffer = HeapAlloc(GetProcessHeap(), 0, size)))
3001 DWORD read;
3002 if (!ReadFile(file, buffer, size, &read, NULL) || read != size)
3004 WARN("can't read certificate file %s\n", debugstr_w(fileName));
3005 HeapFree(GetProcessHeap(), 0, buffer);
3006 CloseHandle(file);
3007 return NULL;
3011 else
3013 WARN("empty file %s\n", debugstr_w(fileName));
3014 CloseHandle(file);
3015 return NULL;
3017 CloseHandle(file);
3018 cert = CertCreateCertificateContext(encoding, buffer, size);
3019 HeapFree(GetProcessHeap(), 0, buffer);
3020 return cert;
3023 /* Decodes a cert's basic constraints extension (either szOID_BASIC_CONSTRAINTS
3024 * or szOID_BASIC_CONSTRAINTS2, whichever is present) to determine if it
3025 * should be a CA. If neither extension is present, returns
3026 * defaultIfNotSpecified.
3028 static BOOL is_ca_cert(PCCERT_CONTEXT cert, BOOL defaultIfNotSpecified)
3030 BOOL isCA = defaultIfNotSpecified;
3031 PCERT_EXTENSION ext = CertFindExtension(szOID_BASIC_CONSTRAINTS,
3032 cert->pCertInfo->cExtension, cert->pCertInfo->rgExtension);
3034 if (ext)
3036 CERT_BASIC_CONSTRAINTS_INFO *info;
3037 DWORD size = 0;
3039 if (CryptDecodeObjectEx(X509_ASN_ENCODING, szOID_BASIC_CONSTRAINTS,
3040 ext->Value.pbData, ext->Value.cbData, CRYPT_DECODE_ALLOC_FLAG,
3041 NULL, (LPBYTE)&info, &size))
3043 if (info->SubjectType.cbData == 1)
3044 isCA = info->SubjectType.pbData[0] & CERT_CA_SUBJECT_FLAG;
3045 LocalFree(info);
3048 else
3050 ext = CertFindExtension(szOID_BASIC_CONSTRAINTS2,
3051 cert->pCertInfo->cExtension, cert->pCertInfo->rgExtension);
3052 if (ext)
3054 CERT_BASIC_CONSTRAINTS2_INFO info;
3055 DWORD size = sizeof(CERT_BASIC_CONSTRAINTS2_INFO);
3057 if (CryptDecodeObjectEx(X509_ASN_ENCODING,
3058 szOID_BASIC_CONSTRAINTS2, ext->Value.pbData, ext->Value.cbData,
3059 0, NULL, &info, &size))
3060 isCA = info.fCA;
3063 return isCA;
3066 static HCERTSTORE choose_store_for_cert(PCCERT_CONTEXT cert)
3068 static const WCHAR AddressBook[] = { 'A','d','d','r','e','s','s',
3069 'B','o','o','k',0 };
3070 static const WCHAR CA[] = { 'C','A',0 };
3071 LPCWSTR storeName;
3073 if (is_ca_cert(cert, TRUE))
3074 storeName = CA;
3075 else
3076 storeName = AddressBook;
3077 return CertOpenStore(CERT_STORE_PROV_SYSTEM_W, 0, 0,
3078 CERT_SYSTEM_STORE_CURRENT_USER, storeName);
3081 BOOL WINAPI CryptUIWizImport(DWORD dwFlags, HWND hwndParent, LPCWSTR pwszWizardTitle,
3082 PCCRYPTUI_WIZ_IMPORT_SRC_INFO pImportSrc, HCERTSTORE hDestCertStore)
3084 BOOL ret;
3085 HCERTSTORE store;
3086 const CERT_CONTEXT *cert;
3087 BOOL freeCert = FALSE;
3089 TRACE("(0x%08x, %p, %s, %p, %p)\n", dwFlags, hwndParent, debugstr_w(pwszWizardTitle),
3090 pImportSrc, hDestCertStore);
3092 if (!(dwFlags & CRYPTUI_WIZ_NO_UI)) FIXME("UI not implemented\n");
3094 if (!pImportSrc ||
3095 pImportSrc->dwSize != sizeof(CRYPTUI_WIZ_IMPORT_SRC_INFO))
3097 SetLastError(E_INVALIDARG);
3098 return FALSE;
3101 switch (pImportSrc->dwSubjectChoice)
3103 case CRYPTUI_WIZ_IMPORT_SUBJECT_FILE:
3104 if (!(cert = make_cert_from_file(pImportSrc->u.pwszFileName)))
3106 WARN("unable to create certificate context\n");
3107 return FALSE;
3109 else
3110 freeCert = TRUE;
3111 break;
3112 case CRYPTUI_WIZ_IMPORT_SUBJECT_CERT_CONTEXT:
3113 cert = pImportSrc->u.pCertContext;
3114 if (!cert)
3116 SetLastError(E_INVALIDARG);
3117 return FALSE;
3119 break;
3120 default:
3121 FIXME("source type not implemented: %u\n", pImportSrc->dwSubjectChoice);
3122 SetLastError(E_INVALIDARG);
3123 return FALSE;
3125 if (hDestCertStore) store = hDestCertStore;
3126 else
3128 if (!(store = choose_store_for_cert(cert)))
3130 WARN("unable to open certificate store\n");
3131 CertFreeCertificateContext(cert);
3132 return FALSE;
3135 ret = CertAddCertificateContextToStore(store, cert, CERT_STORE_ADD_REPLACE_EXISTING, NULL);
3137 if (!hDestCertStore) CertCloseStore(store, 0);
3138 if (freeCert)
3139 CertFreeCertificateContext(cert);
3140 return ret;