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
36 #include "cryptuiapi.h"
37 #include "cryptuires.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
);
53 case DLL_WINE_PREATTACH
:
54 return FALSE
; /* prefer native version */
55 case DLL_PROCESS_ATTACH
:
57 DisableThreadLibraryCalls(hinstDLL
);
59 case DLL_PROCESS_DETACH
:
67 /***********************************************************************
68 * CryptUIDlgCertMgr (CRYPTUI.@)
70 BOOL WINAPI
CryptUIDlgCertMgr(PCCRYPTUI_CERT_MGR_STRUCT pCryptUICertMgr
)
72 FIXME("(%p): stub\n", pCryptUICertMgr
);
76 /***********************************************************************
77 * CryptUIDlgViewCertificateA (CRYPTUI.@)
79 BOOL WINAPI
CryptUIDlgViewCertificateA(
80 PCCRYPTUI_VIEWCERTIFICATE_STRUCTA pCertViewInfo
, BOOL
*pfPropertiesChanged
)
82 CRYPTUI_VIEWCERTIFICATE_STRUCTW viewInfo
;
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,
94 title
= HeapAlloc(GetProcessHeap(), 0, len
* sizeof(WCHAR
));
97 MultiByteToWideChar(CP_ACP
, 0, pCertViewInfo
->szTitle
, -1, title
,
99 viewInfo
.szTitle
= title
;
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
);
118 struct ReadStringStruct
125 static DWORD CALLBACK
read_text_callback(DWORD_PTR dwCookie
, LPBYTE buf
,
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
));
135 *pcb
= cch
* sizeof(WCHAR
);
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
));
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
)
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
)
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
,
188 len
= CertGetNameStringW(pCertContext
, dwType
, dwFlags
, NULL
, NULL
, 0);
191 buf
= HeapAlloc(GetProcessHeap(), 0, len
* sizeof(WCHAR
));
193 CertGetNameStringW(pCertContext
, dwType
, dwFlags
, NULL
, buf
, len
);
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
);
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
)
216 LPRICHEDITOLE richEditOle
= NULL
;
217 LPOLEOBJECT object
= NULL
;
219 LPOLECACHE oleCache
= NULL
;
222 LPDATAOBJECT dataObject
= NULL
;
223 HBITMAP bitmap
= NULL
;
228 TRACE("(%p, %d)\n", hwnd
, id
);
230 SendMessageW(hwnd
, EM_GETOLEINTERFACE
, 0, (LPARAM
)&richEditOle
);
233 hr
= OleCreateDefaultHandler(&CLSID_NULL
, NULL
, &IID_IOleObject
,
237 hr
= IOleObject_GetUserClassID(object
, &clsid
);
240 hr
= IOleObject_QueryInterface(object
, &IID_IOleCache
, (void**)&oleCache
);
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
);
251 hr
= IOleObject_QueryInterface(object
, &IID_IDataObject
,
252 (void**)&dataObject
);
255 bitmap
= LoadImageW(hInstance
, MAKEINTRESOURCEW(id
), IMAGE_BITMAP
, 0, 0,
256 LR_DEFAULTSIZE
| LR_LOADTRANSPARENT
);
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
);
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;
280 IRichEditOle_InsertObject(richEditOle
, &reObject
);
284 IDataObject_Release(dataObject
);
286 IOleCache_Release(oleCache
);
288 IOleObject_Release(object
);
290 IRichEditOle_Release(richEditOle
);
293 #define MY_INDENT 200
295 static void add_oid_text_to_control(HWND hwnd
, char *oid
)
298 PCCRYPT_OID_INFO oidInfo
= CryptFindOIDInfo(CRYPT_OID_INFO_OID_KEY
, oid
, 0);
301 parFmt
.cbSize
= sizeof(parFmt
);
302 parFmt
.dwMask
= PFM_STARTINDENT
;
303 parFmt
.dxStartIndent
= MY_INDENT
* 3;
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
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)
390 cmp
= strcmp(oid
, oidMap
[i
].oid
);
401 static void add_local_oid_text_to_control(HWND text
, LPCSTR oid
)
403 struct OIDToString
*entry
;
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
;
416 len
= LoadStringW(hInstance
, entry
->id
, (LPWSTR
)&str
, 0);
419 if ((linebreak
= memchrW(ptr
, '\n', len
)))
421 WCHAR copy
[MAX_STRING_LEN
];
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
431 lstrcpynW(copy
, ptr
, linebreak
- ptr
+ 1);
432 add_text_with_paraformat_to_control(text
, copy
,
433 linebreak
- ptr
, &parFmt
);
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);
447 add_text_with_paraformat_to_control(text
, str
, len
, &parFmt
);
448 add_unformatted_text_to_control(text
, &nl
, 1);
453 WCHAR
*oidW
= HeapAlloc(GetProcessHeap(), 0,
454 (strlen(oid
) + 1) * sizeof(WCHAR
));
461 for (src
= oid
, dst
= oidW
; *src
; src
++, dst
++)
464 add_text_with_paraformat_to_control(text
, oidW
, lstrlenW(oidW
),
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
,
475 static char any_app_policy
[] = szOID_ANY_APPLICATION_POLICY
;
478 PCERT_EXTENSION policyExt
;
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
;
502 if (CryptDecodeObjectEx(X509_ASN_ENCODING
, X509_CERT_POLICIES
,
503 policyExt
->Value
.pbData
, policyExt
->Value
.cbData
,
504 CRYPT_DECODE_ALLOC_FLAG
, NULL
, &policies
, &size
))
508 for (i
= 0; i
< policies
->cPolicyInfo
; i
++)
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
);
521 add_oid_text_to_control(text
, any_app_policy
);
524 static BOOL
display_cert_usages(HWND text
, PCCERT_CONTEXT cert
,
529 BOOL badUsages
= FALSE
;
531 if (CertGetEnhancedKeyUsage(cert
, 0, NULL
, &size
))
534 static char any_cert_policy
[] = szOID_ANY_CERT_POLICY
;
535 PCERT_ENHKEY_USAGE usage
= HeapAlloc(GetProcessHeap(), 0, size
);
539 if (CertGetEnhancedKeyUsage(cert
, 0, usage
, &size
))
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
,
561 if (!usage
->cUsageIdentifier
)
562 add_oid_text_to_control(text
, any_cert_policy
);
564 for (i
= 0; i
< usage
->cUsageIdentifier
; i
++)
565 add_local_oid_text_to_control(text
,
566 usage
->rgpszUsageIdentifier
[i
]);
570 HeapFree(GetProcessHeap(), 0, usage
);
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
)
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
;
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
,
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
,
623 CRYPT_OBJID_BLOB
*ret
= NULL
;
626 for (i
= 0; !ret
&& i
< policies
->cPolicyInfo
; i
++)
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
].
639 static WCHAR
*get_cps_str_from_qualifier(CRYPT_OBJID_BLOB
*qualifier
)
641 LPWSTR qualifierStr
= NULL
;
642 CERT_NAME_VALUE
*qualifierValue
;
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
));
653 CertRDNValueToStrW(qualifierValue
->dwValueType
,
654 &qualifierValue
->Value
, qualifierStr
, size
);
655 LocalFree(qualifierValue
);
660 static WCHAR
*get_user_notice_from_qualifier(CRYPT_OBJID_BLOB
*qualifier
)
663 CERT_POLICY_QUALIFIER_USER_NOTICE
*qualifierValue
;
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
));
674 strcpyW(str
, qualifierValue
->pszDisplayText
);
675 LocalFree(qualifierValue
);
680 struct IssuerStatement
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
;
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
));
719 issuerStatement
->cps
= cps
;
720 issuerStatement
->userNotice
= userNotice
;
721 EnableWindow(GetDlgItem(hwnd
, IDC_ISSUERSTATEMENT
), TRUE
);
722 SetWindowLongPtrW(hwnd
, DWLP_USER
,
723 (ULONG_PTR
)issuerStatement
);
731 static void set_cert_info(HWND hwnd
,
732 PCCRYPTUI_VIEWCERTIFICATE_STRUCTW pCertViewInfo
)
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
->TrustStatus
.dwErrorStatus
&
746 CERT_TRUST_IS_PARTIAL_CHAIN
)
747 add_icon_to_control(icon
, IDB_CERT_WARNING
);
748 else if (!root
->fTrustedRoot
)
749 add_icon_to_control(icon
, IDB_CERT_ERROR
);
751 add_icon_to_control(icon
, IDB_CERT
);
753 memset(&charFmt
, 0, sizeof(charFmt
));
754 charFmt
.cbSize
= sizeof(charFmt
);
755 charFmt
.dwMask
= CFM_BOLD
;
756 charFmt
.dwEffects
= CFE_BOLD
;
757 SendMessageW(text
, EM_SETCHARFORMAT
, SCF_SELECTION
, (LPARAM
)&charFmt
);
758 /* FIXME: vertically center text */
759 parFmt
.cbSize
= sizeof(parFmt
);
760 parFmt
.dwMask
= PFM_STARTINDENT
;
761 parFmt
.dxStartIndent
= MY_INDENT
;
762 add_string_resource_with_paraformat_to_control(text
,
763 IDS_CERTIFICATEINFORMATION
, &parFmt
);
765 text
= GetDlgItem(hwnd
, IDC_CERTIFICATE_STATUS
);
766 SendMessageW(text
, EM_SETCHARFORMAT
, SCF_SELECTION
, (LPARAM
)&charFmt
);
767 if (provSigner
->dwError
== TRUST_E_CERT_SIGNATURE
)
768 add_string_resource_with_paraformat_to_control(text
,
769 IDS_CERT_INFO_BAD_SIG
, &parFmt
);
770 else if (provSigner
->pChainContext
->TrustStatus
.dwErrorStatus
&
771 CERT_TRUST_IS_PARTIAL_CHAIN
)
772 add_string_resource_with_paraformat_to_control(text
,
773 IDS_CERT_INFO_PARTIAL_CHAIN
, &parFmt
);
774 else if (!root
->fTrustedRoot
)
776 if (provSigner
->csCertChain
== 1 && root
->fSelfSigned
)
777 add_string_resource_with_paraformat_to_control(text
,
778 IDS_CERT_INFO_UNTRUSTED_CA
, &parFmt
);
780 add_string_resource_with_paraformat_to_control(text
,
781 IDS_CERT_INFO_UNTRUSTED_ROOT
, &parFmt
);
785 set_policy_text(text
, pCertViewInfo
);
786 set_issuer_statement(hwnd
, pCertViewInfo
);
790 static void set_cert_name_string(HWND hwnd
, PCCERT_CONTEXT cert
,
791 DWORD nameFlags
, int heading
)
794 HWND text
= GetDlgItem(hwnd
, IDC_CERTIFICATE_NAMES
);
798 memset(&charFmt
, 0, sizeof(charFmt
));
799 charFmt
.cbSize
= sizeof(charFmt
);
800 charFmt
.dwMask
= CFM_BOLD
;
801 charFmt
.dwEffects
= CFE_BOLD
;
802 SendMessageW(text
, EM_SETCHARFORMAT
, SCF_SELECTION
, (LPARAM
)&charFmt
);
803 parFmt
.cbSize
= sizeof(parFmt
);
804 parFmt
.dwMask
= PFM_STARTINDENT
;
805 parFmt
.dxStartIndent
= MY_INDENT
* 3;
806 add_string_resource_with_paraformat_to_control(text
, heading
, &parFmt
);
807 charFmt
.dwEffects
= 0;
808 SendMessageW(text
, EM_SETCHARFORMAT
, SCF_SELECTION
, (LPARAM
)&charFmt
);
809 add_cert_string_to_control(text
, cert
, CERT_NAME_SIMPLE_DISPLAY_TYPE
,
811 add_unformatted_text_to_control(text
, &nl
, 1);
812 add_unformatted_text_to_control(text
, &nl
, 1);
813 add_unformatted_text_to_control(text
, &nl
, 1);
817 static void add_date_string_to_control(HWND hwnd
, const FILETIME
*fileTime
)
819 WCHAR dateFmt
[80]; /* sufficient for all versions of LOCALE_SSHORTDATE */
823 GetLocaleInfoW(LOCALE_SYSTEM_DEFAULT
, LOCALE_SSHORTDATE
, dateFmt
,
824 sizeof(dateFmt
) / sizeof(dateFmt
[0]));
825 FileTimeToSystemTime(fileTime
, &sysTime
);
826 GetDateFormatW(LOCALE_SYSTEM_DEFAULT
, 0, &sysTime
, dateFmt
, date
,
827 sizeof(date
) / sizeof(date
[0]));
828 add_unformatted_text_to_control(hwnd
, date
, lstrlenW(date
));
831 static void set_cert_validity_period(HWND hwnd
, PCCERT_CONTEXT cert
)
834 HWND text
= GetDlgItem(hwnd
, IDC_CERTIFICATE_NAMES
);
838 memset(&charFmt
, 0, sizeof(charFmt
));
839 charFmt
.cbSize
= sizeof(charFmt
);
840 charFmt
.dwMask
= CFM_BOLD
;
841 charFmt
.dwEffects
= CFE_BOLD
;
842 SendMessageW(text
, EM_SETCHARFORMAT
, SCF_SELECTION
, (LPARAM
)&charFmt
);
843 parFmt
.cbSize
= sizeof(parFmt
);
844 parFmt
.dwMask
= PFM_STARTINDENT
;
845 parFmt
.dxStartIndent
= MY_INDENT
* 3;
846 add_string_resource_with_paraformat_to_control(text
, IDS_VALID_FROM
,
848 charFmt
.dwEffects
= 0;
849 SendMessageW(text
, EM_SETCHARFORMAT
, SCF_SELECTION
, (LPARAM
)&charFmt
);
850 add_date_string_to_control(text
, &cert
->pCertInfo
->NotBefore
);
851 charFmt
.dwEffects
= CFE_BOLD
;
852 SendMessageW(text
, EM_SETCHARFORMAT
, SCF_SELECTION
, (LPARAM
)&charFmt
);
853 add_string_resource_to_control(text
, IDS_VALID_TO
);
854 charFmt
.dwEffects
= 0;
855 SendMessageW(text
, EM_SETCHARFORMAT
, SCF_SELECTION
, (LPARAM
)&charFmt
);
856 add_date_string_to_control(text
, &cert
->pCertInfo
->NotAfter
);
857 add_unformatted_text_to_control(text
, &nl
, 1);
860 static void set_general_info(HWND hwnd
,
861 PCCRYPTUI_VIEWCERTIFICATE_STRUCTW pCertViewInfo
)
863 set_cert_info(hwnd
, pCertViewInfo
);
864 set_cert_name_string(hwnd
, pCertViewInfo
->pCertContext
, 0,
865 IDS_SUBJECT_HEADING
);
866 set_cert_name_string(hwnd
, pCertViewInfo
->pCertContext
,
867 CERT_NAME_ISSUER_FLAG
, IDS_ISSUER_HEADING
);
868 set_cert_validity_period(hwnd
, pCertViewInfo
->pCertContext
);
871 static LRESULT CALLBACK
user_notice_dlg_proc(HWND hwnd
, UINT msg
, WPARAM wp
,
876 struct IssuerStatement
*issuerStatement
;
881 text
= GetDlgItem(hwnd
, IDC_USERNOTICE
);
882 issuerStatement
= (struct IssuerStatement
*)lp
;
883 add_unformatted_text_to_control(text
, issuerStatement
->userNotice
,
884 strlenW(issuerStatement
->userNotice
));
885 if (issuerStatement
->cps
)
886 SetWindowLongPtrW(hwnd
, DWLP_USER
, (LPARAM
)issuerStatement
->cps
);
888 EnableWindow(GetDlgItem(hwnd
, IDC_CPS
), FALSE
);
894 EndDialog(hwnd
, IDOK
);
899 IBindCtx
*bctx
= NULL
;
902 CreateBindCtx(0, &bctx
);
903 cps
= (LPWSTR
)GetWindowLongPtrW(hwnd
, DWLP_USER
);
904 HlinkSimpleNavigateToString(cps
, NULL
, NULL
, NULL
, bctx
, NULL
,
905 HLNF_OPENINNEWWINDOW
, 0);
906 IBindCtx_Release(bctx
);
914 static void show_user_notice(HWND hwnd
, struct IssuerStatement
*issuerStatement
)
916 DialogBoxParamW(hInstance
, MAKEINTRESOURCEW(IDD_USERNOTICE
), hwnd
,
917 user_notice_dlg_proc
, (LPARAM
)issuerStatement
);
920 static LRESULT CALLBACK
general_dlg_proc(HWND hwnd
, UINT msg
, WPARAM wp
,
923 PROPSHEETPAGEW
*page
;
924 PCCRYPTUI_VIEWCERTIFICATE_STRUCTW pCertViewInfo
;
926 TRACE("(%p, %08x, %08lx, %08lx)\n", hwnd
, msg
, wp
, lp
);
931 page
= (PROPSHEETPAGEW
*)lp
;
932 pCertViewInfo
= (PCCRYPTUI_VIEWCERTIFICATE_STRUCTW
)page
->lParam
;
933 if (pCertViewInfo
->dwFlags
& CRYPTUI_DISABLE_ADDTOSTORE
)
934 ShowWindow(GetDlgItem(hwnd
, IDC_ADDTOSTORE
), FALSE
);
935 EnableWindow(GetDlgItem(hwnd
, IDC_ISSUERSTATEMENT
), FALSE
);
936 set_general_info(hwnd
, pCertViewInfo
);
942 FIXME("call CryptUIWizImport\n");
944 case IDC_ISSUERSTATEMENT
:
946 struct IssuerStatement
*issuerStatement
=
947 (struct IssuerStatement
*)GetWindowLongPtrW(hwnd
, DWLP_USER
);
951 if (issuerStatement
->userNotice
)
952 show_user_notice(hwnd
, issuerStatement
);
953 else if (issuerStatement
->cps
)
955 IBindCtx
*bctx
= NULL
;
957 CreateBindCtx(0, &bctx
);
958 HlinkSimpleNavigateToString(issuerStatement
->cps
, NULL
,
959 NULL
, NULL
, bctx
, NULL
, HLNF_OPENINNEWWINDOW
, 0);
960 IBindCtx_Release(bctx
);
971 static UINT CALLBACK
general_callback_proc(HWND hwnd
, UINT msg
,
972 PROPSHEETPAGEW
*page
)
974 struct IssuerStatement
*issuerStatement
;
980 (struct IssuerStatement
*)GetWindowLongPtrW(hwnd
, DWLP_USER
);
983 HeapFree(GetProcessHeap(), 0, issuerStatement
->cps
);
984 HeapFree(GetProcessHeap(), 0, issuerStatement
->userNotice
);
985 HeapFree(GetProcessHeap(), 0, issuerStatement
);
992 static void init_general_page(PCCRYPTUI_VIEWCERTIFICATE_STRUCTW pCertViewInfo
,
993 PROPSHEETPAGEW
*page
)
995 memset(page
, 0, sizeof(PROPSHEETPAGEW
));
996 page
->dwSize
= sizeof(PROPSHEETPAGEW
);
997 page
->dwFlags
= PSP_USECALLBACK
;
998 page
->pfnCallback
= general_callback_proc
;
999 page
->hInstance
= hInstance
;
1000 page
->u
.pszTemplate
= MAKEINTRESOURCEW(IDD_GENERAL
);
1001 page
->pfnDlgProc
= general_dlg_proc
;
1002 page
->lParam
= (LPARAM
)pCertViewInfo
;
1005 typedef WCHAR
* (*field_format_func
)(PCCERT_CONTEXT cert
);
1007 static WCHAR
*field_format_version(PCCERT_CONTEXT cert
)
1009 static const WCHAR fmt
[] = { 'V','%','d',0 };
1010 WCHAR
*buf
= HeapAlloc(GetProcessHeap(), 0, 12 * sizeof(WCHAR
));
1013 sprintfW(buf
, fmt
, cert
->pCertInfo
->dwVersion
);
1017 static WCHAR
*format_hex_string(void *pb
, DWORD cb
)
1019 WCHAR
*buf
= HeapAlloc(GetProcessHeap(), 0, (cb
* 3 + 1) * sizeof(WCHAR
));
1023 static const WCHAR fmt
[] = { '%','0','2','x',' ',0 };
1027 for (i
= 0, ptr
= buf
; i
< cb
; i
++, ptr
+= 3)
1028 sprintfW(ptr
, fmt
, ((BYTE
*)pb
)[i
]);
1033 static WCHAR
*field_format_serial_number(PCCERT_CONTEXT cert
)
1035 return format_hex_string(cert
->pCertInfo
->SerialNumber
.pbData
,
1036 cert
->pCertInfo
->SerialNumber
.cbData
);
1039 static WCHAR
*field_format_issuer(PCCERT_CONTEXT cert
)
1041 return get_cert_name_string(cert
, CERT_NAME_SIMPLE_DISPLAY_TYPE
,
1042 CERT_NAME_ISSUER_FLAG
);
1045 static WCHAR
*field_format_subject(PCCERT_CONTEXT cert
)
1047 return get_cert_name_string(cert
, CERT_NAME_SIMPLE_DISPLAY_TYPE
, 0);
1050 static WCHAR
*format_long_date(const FILETIME
*fileTime
)
1052 WCHAR dateFmt
[80]; /* long enough for LOCALE_SLONGDATE */
1057 /* FIXME: format isn't quite right, want time too */
1058 GetLocaleInfoW(LOCALE_SYSTEM_DEFAULT
, LOCALE_SLONGDATE
, dateFmt
,
1059 sizeof(dateFmt
) / sizeof(dateFmt
[0]));
1060 FileTimeToSystemTime(fileTime
, &sysTime
);
1061 len
= GetDateFormatW(LOCALE_SYSTEM_DEFAULT
, 0, &sysTime
, dateFmt
, NULL
, 0);
1064 buf
= HeapAlloc(GetProcessHeap(), 0, len
* sizeof(WCHAR
));
1066 GetDateFormatW(LOCALE_SYSTEM_DEFAULT
, 0, &sysTime
, dateFmt
, buf
,
1072 static WCHAR
*field_format_from_date(PCCERT_CONTEXT cert
)
1074 return format_long_date(&cert
->pCertInfo
->NotBefore
);
1077 static WCHAR
*field_format_to_date(PCCERT_CONTEXT cert
)
1079 return format_long_date(&cert
->pCertInfo
->NotAfter
);
1082 static WCHAR
*field_format_public_key(PCCERT_CONTEXT cert
)
1084 PCCRYPT_OID_INFO oidInfo
;
1087 oidInfo
= CryptFindOIDInfo(CRYPT_OID_INFO_OID_KEY
,
1088 cert
->pCertInfo
->SubjectPublicKeyInfo
.Algorithm
.pszObjId
, 0);
1091 WCHAR fmt
[MAX_STRING_LEN
];
1093 if (LoadStringW(hInstance
, IDS_FIELD_PUBLIC_KEY_FORMAT
, fmt
,
1094 sizeof(fmt
) / sizeof(fmt
[0])))
1096 /* Allocate the output buffer. Use the number of bytes in the
1097 * public key as a conservative (high) estimate for the number of
1098 * digits in its output.
1099 * The output is of the form (in English)
1100 * "<public key algorithm> (<public key bit length> bits)".
1101 * Ordinarily having two positional parameters in a string is not a
1102 * good idea, but as this isn't a sentence fragment, it shouldn't
1103 * be word-order dependent.
1105 buf
= HeapAlloc(GetProcessHeap(), 0,
1106 (strlenW(fmt
) + strlenW(oidInfo
->pwszName
) +
1107 cert
->pCertInfo
->SubjectPublicKeyInfo
.PublicKey
.cbData
* 8)
1110 sprintfW(buf
, fmt
, oidInfo
->pwszName
,
1111 CertGetPublicKeyLength(X509_ASN_ENCODING
,
1112 &cert
->pCertInfo
->SubjectPublicKeyInfo
));
1118 struct field_value_data
;
1121 PCCRYPTUI_VIEWCERTIFICATE_STRUCTW pCertViewInfo
;
1122 BOOL
*pfPropertiesChanged
;
1124 struct field_value_data
*fields
;
1127 typedef void (*add_fields_func
)(HWND hwnd
, struct detail_data
*data
);
1129 struct field_value_data
1134 static void add_field_value_data(struct detail_data
*data
, void *param
)
1137 data
->fields
= HeapReAlloc(GetProcessHeap(), 0, data
->fields
,
1138 (data
->cFields
+ 1) * sizeof(struct field_value_data
));
1140 data
->fields
= HeapAlloc(GetProcessHeap(), 0,
1141 sizeof(struct field_value_data
));
1144 data
->fields
[data
->cFields
].param
= param
;
1149 static void add_field_and_value_to_list(HWND hwnd
, struct detail_data
*data
,
1150 LPWSTR field
, LPWSTR value
, void *param
)
1153 int iItem
= SendMessageW(hwnd
, LVM_GETITEMCOUNT
, 0, 0);
1155 item
.mask
= LVIF_TEXT
| LVIF_PARAM
;
1158 item
.pszText
= field
;
1159 item
.lParam
= (LPARAM
)data
;
1160 SendMessageW(hwnd
, LVM_INSERTITEMW
, 0, (LPARAM
)&item
);
1163 item
.pszText
= value
;
1165 SendMessageW(hwnd
, LVM_SETITEMTEXTW
, iItem
, (LPARAM
)&item
);
1167 add_field_value_data(data
, param
);
1170 static void add_string_id_and_value_to_list(HWND hwnd
, struct detail_data
*data
,
1171 int id
, LPWSTR value
, void *param
)
1173 WCHAR buf
[MAX_STRING_LEN
];
1175 LoadStringW(hInstance
, id
, buf
, sizeof(buf
) / sizeof(buf
[0]));
1176 add_field_and_value_to_list(hwnd
, data
, buf
, value
, param
);
1182 field_format_func format
;
1185 static void add_v1_field(HWND hwnd
, struct detail_data
*data
,
1186 const struct v1_field
*field
)
1188 WCHAR
*val
= field
->format(data
->pCertViewInfo
->pCertContext
);
1192 add_string_id_and_value_to_list(hwnd
, data
, field
->id
, val
, NULL
);
1193 HeapFree(GetProcessHeap(), 0, val
);
1197 static const struct v1_field v1_fields
[] = {
1198 { IDS_FIELD_VERSION
, field_format_version
},
1199 { IDS_FIELD_SERIAL_NUMBER
, field_format_serial_number
},
1200 { IDS_FIELD_ISSUER
, field_format_issuer
},
1201 { IDS_FIELD_VALID_FROM
, field_format_from_date
},
1202 { IDS_FIELD_VALID_TO
, field_format_to_date
},
1203 { IDS_FIELD_SUBJECT
, field_format_subject
},
1204 { IDS_FIELD_PUBLIC_KEY
, field_format_public_key
}
1207 static void add_v1_fields(HWND hwnd
, struct detail_data
*data
)
1210 PCCERT_CONTEXT cert
= data
->pCertViewInfo
->pCertContext
;
1212 /* The last item in v1_fields is the public key, which is not in the loop
1213 * because it's a special case.
1215 for (i
= 0; i
< sizeof(v1_fields
) / sizeof(v1_fields
[0]) - 1; i
++)
1216 add_v1_field(hwnd
, data
, &v1_fields
[i
]);
1217 if (cert
->pCertInfo
->SubjectPublicKeyInfo
.PublicKey
.cbData
)
1218 add_v1_field(hwnd
, data
, &v1_fields
[i
]);
1221 static void add_all_fields(HWND hwnd
, struct detail_data
*data
)
1223 add_v1_fields(hwnd
, data
);
1224 FIXME("add extensions and properties\n");
1227 struct selection_list_item
1230 add_fields_func add
;
1233 const struct selection_list_item listItems
[] = {
1234 { IDS_FIELDS_ALL
, add_all_fields
},
1235 { IDS_FIELDS_V1
, add_v1_fields
},
1236 { IDS_FIELDS_EXTENSIONS
, NULL
},
1237 { IDS_FIELDS_CRITICAL_EXTENSIONS
, NULL
},
1238 { IDS_FIELDS_PROPERTIES
, NULL
},
1241 static void create_show_list(HWND hwnd
, struct detail_data
*data
)
1243 HWND cb
= GetDlgItem(hwnd
, IDC_DETAIL_SELECT
);
1244 WCHAR buf
[MAX_STRING_LEN
];
1247 for (i
= 0; i
< sizeof(listItems
) / sizeof(listItems
[0]); i
++)
1251 LoadStringW(hInstance
, listItems
[i
].id
, buf
,
1252 sizeof(buf
) / sizeof(buf
[0]));
1253 index
= SendMessageW(cb
, CB_INSERTSTRING
, -1, (LPARAM
)buf
);
1254 SendMessageW(cb
, CB_SETITEMDATA
, index
, (LPARAM
)data
);
1256 SendMessageW(cb
, CB_SETCURSEL
, 0, 0);
1259 static void create_listview_columns(HWND hwnd
)
1261 HWND lv
= GetDlgItem(hwnd
, IDC_DETAIL_LIST
);
1263 WCHAR buf
[MAX_STRING_LEN
];
1266 SendMessageW(lv
, LVM_SETEXTENDEDLISTVIEWSTYLE
, 0, LVS_EX_FULLROWSELECT
);
1267 GetWindowRect(lv
, &rc
);
1268 LoadStringW(hInstance
, IDS_FIELD
, buf
, sizeof(buf
) / sizeof(buf
[0]));
1269 column
.mask
= LVCF_WIDTH
| LVCF_TEXT
;
1270 column
.cx
= (rc
.right
- rc
.left
) / 2 - 2;
1271 column
.pszText
= buf
;
1272 SendMessageW(lv
, LVM_INSERTCOLUMNW
, 0, (LPARAM
)&column
);
1273 LoadStringW(hInstance
, IDS_VALUE
, buf
, sizeof(buf
) / sizeof(buf
[0]));
1274 SendMessageW(lv
, LVM_INSERTCOLUMNW
, 1, (LPARAM
)&column
);
1277 static void set_fields_selection(HWND hwnd
, struct detail_data
*data
, int sel
)
1279 HWND list
= GetDlgItem(hwnd
, IDC_DETAIL_LIST
);
1281 if (sel
>= 0 && sel
< sizeof(listItems
) / sizeof(listItems
[0]))
1283 SendMessageW(list
, LVM_DELETEALLITEMS
, 0, 0);
1284 if (listItems
[sel
].add
)
1285 listItems
[sel
].add(list
, data
);
1289 static void create_cert_details_list(HWND hwnd
, struct detail_data
*data
)
1291 create_show_list(hwnd
, data
);
1292 create_listview_columns(hwnd
);
1293 set_fields_selection(hwnd
, data
, 0);
1296 static void free_detail_fields(struct detail_data
*data
)
1298 HeapFree(GetProcessHeap(), 0, data
->fields
);
1299 data
->fields
= NULL
;
1303 static void refresh_details_view(HWND hwnd
)
1305 HWND cb
= GetDlgItem(hwnd
, IDC_DETAIL_SELECT
);
1307 struct detail_data
*data
;
1309 curSel
= SendMessageW(cb
, CB_GETCURSEL
, 0, 0);
1310 /* Actually, any index will do, since they all store the same data value */
1311 data
= (struct detail_data
*)SendMessageW(cb
, CB_GETITEMDATA
, curSel
, 0);
1312 free_detail_fields(data
);
1313 set_fields_selection(hwnd
, data
, curSel
);
1316 static LRESULT CALLBACK
detail_dlg_proc(HWND hwnd
, UINT msg
, WPARAM wp
,
1319 PROPSHEETPAGEW
*page
;
1320 struct detail_data
*data
;
1322 TRACE("(%p, %08x, %08lx, %08lx)\n", hwnd
, msg
, wp
, lp
);
1327 page
= (PROPSHEETPAGEW
*)lp
;
1328 data
= (struct detail_data
*)page
->lParam
;
1329 create_cert_details_list(hwnd
, data
);
1330 if (!(data
->pCertViewInfo
->dwFlags
& CRYPTUI_ENABLE_EDITPROPERTIES
))
1331 EnableWindow(GetDlgItem(hwnd
, IDC_EDITPROPERTIES
), FALSE
);
1332 if (data
->pCertViewInfo
->dwFlags
& CRYPTUI_DISABLE_EXPORT
)
1333 EnableWindow(GetDlgItem(hwnd
, IDC_EXPORT
), FALSE
);
1339 FIXME("call CryptUIWizExport\n");
1341 case IDC_EDITPROPERTIES
:
1342 FIXME("show edit properties dialog\n");
1344 case ((CBN_SELCHANGE
<< 16) | IDC_DETAIL_SELECT
):
1345 refresh_details_view(hwnd
);
1353 static BOOL
init_detail_page(PCCRYPTUI_VIEWCERTIFICATE_STRUCTW pCertViewInfo
,
1354 BOOL
*pfPropertiesChanged
, PROPSHEETPAGEW
*page
)
1357 struct detail_data
*data
= HeapAlloc(GetProcessHeap(), 0,
1358 sizeof(struct detail_data
));
1362 data
->pCertViewInfo
= pCertViewInfo
;
1363 data
->pfPropertiesChanged
= pfPropertiesChanged
;
1365 data
->fields
= NULL
;
1366 memset(page
, 0, sizeof(PROPSHEETPAGEW
));
1367 page
->dwSize
= sizeof(PROPSHEETPAGEW
);
1368 page
->hInstance
= hInstance
;
1369 page
->u
.pszTemplate
= MAKEINTRESOURCEW(IDD_DETAIL
);
1370 page
->pfnDlgProc
= detail_dlg_proc
;
1371 page
->lParam
= (LPARAM
)data
;
1379 static int CALLBACK
cert_prop_sheet_proc(HWND hwnd
, UINT msg
, LPARAM lp
)
1384 TRACE("(%p, %08x, %08lx)\n", hwnd
, msg
, lp
);
1388 case PSCB_INITIALIZED
:
1389 /* Get cancel button's position.. */
1390 GetWindowRect(GetDlgItem(hwnd
, IDCANCEL
), &rc
);
1391 topLeft
.x
= rc
.left
;
1393 ScreenToClient(hwnd
, &topLeft
);
1394 /* hide the cancel button.. */
1395 ShowWindow(GetDlgItem(hwnd
, IDCANCEL
), FALSE
);
1396 /* get the OK button's size.. */
1397 GetWindowRect(GetDlgItem(hwnd
, IDOK
), &rc
);
1398 /* and move the OK button to the cancel button's original position. */
1399 MoveWindow(GetDlgItem(hwnd
, IDOK
), topLeft
.x
, topLeft
.y
,
1400 rc
.right
- rc
.left
, rc
.bottom
- rc
.top
, FALSE
);
1401 GetWindowRect(GetDlgItem(hwnd
, IDOK
), &rc
);
1407 static BOOL
show_cert_dialog(PCCRYPTUI_VIEWCERTIFICATE_STRUCTW pCertViewInfo
,
1408 CRYPT_PROVIDER_CERT
*provCert
, BOOL
*pfPropertiesChanged
)
1410 static const WCHAR riched
[] = { 'r','i','c','h','e','d','2','0',0 };
1412 PROPSHEETPAGEW
*pages
;
1414 HMODULE lib
= LoadLibraryW(riched
);
1416 nPages
= pCertViewInfo
->cPropSheetPages
+ 1; /* one for the General tab */
1417 if (!(pCertViewInfo
->dwFlags
& CRYPTUI_HIDE_DETAILPAGE
))
1419 if (!(pCertViewInfo
->dwFlags
& CRYPTUI_HIDE_HIERARCHYPAGE
))
1420 FIXME("show hierarchy page\n");
1421 pages
= HeapAlloc(GetProcessHeap(), 0, nPages
* sizeof(PROPSHEETPAGEW
));
1424 PROPSHEETHEADERW hdr
;
1425 CRYPTUI_INITDIALOG_STRUCT
*init
= NULL
;
1428 memset(&hdr
, 0, sizeof(hdr
));
1429 hdr
.dwSize
= sizeof(hdr
);
1430 hdr
.dwFlags
= PSH_NOAPPLYNOW
| PSH_PROPSHEETPAGE
| PSH_USECALLBACK
;
1431 hdr
.hInstance
= hInstance
;
1432 if (pCertViewInfo
->szTitle
)
1433 hdr
.pszCaption
= pCertViewInfo
->szTitle
;
1435 hdr
.pszCaption
= MAKEINTRESOURCEW(IDS_CERTIFICATE
);
1436 init_general_page(pCertViewInfo
, &pages
[hdr
.nPages
++]);
1437 if (!(pCertViewInfo
->dwFlags
& CRYPTUI_HIDE_DETAILPAGE
))
1439 if (init_detail_page(pCertViewInfo
, pfPropertiesChanged
,
1440 &pages
[hdr
.nPages
]))
1443 /* Copy each additional page, and create the init dialog struct for it
1445 if (pCertViewInfo
->cPropSheetPages
)
1447 init
= HeapAlloc(GetProcessHeap(), 0,
1448 pCertViewInfo
->cPropSheetPages
*
1449 sizeof(CRYPTUI_INITDIALOG_STRUCT
));
1452 for (i
= 0; i
< pCertViewInfo
->cPropSheetPages
; i
++)
1454 memcpy(&pages
[hdr
.nPages
+ i
],
1455 &pCertViewInfo
->rgPropSheetPages
[i
],
1456 sizeof(PROPSHEETPAGEW
));
1457 init
[i
].lParam
= pCertViewInfo
->rgPropSheetPages
[i
].lParam
;
1458 init
[i
].pCertContext
= pCertViewInfo
->pCertContext
;
1459 pages
[hdr
.nPages
+ i
].lParam
= (LPARAM
)&init
[i
];
1461 if (pCertViewInfo
->nStartPage
& 0x8000)
1463 /* Start page index is relative to the number of default
1466 hdr
.u2
.nStartPage
= pCertViewInfo
->nStartPage
+ hdr
.nPages
;
1469 hdr
.u2
.nStartPage
= pCertViewInfo
->nStartPage
;
1470 hdr
.nPages
= nPages
;
1474 SetLastError(ERROR_OUTOFMEMORY
);
1478 /* Ignore the relative flag if there aren't any additional pages */
1479 hdr
.u2
.nStartPage
= pCertViewInfo
->nStartPage
& 0x7fff;
1486 hdr
.u3
.ppsp
= pages
;
1487 hdr
.pfnCallback
= cert_prop_sheet_proc
;
1488 l
= PropertySheetW(&hdr
);
1491 SetLastError(ERROR_CANCELLED
);
1495 HeapFree(GetProcessHeap(), 0, init
);
1496 HeapFree(GetProcessHeap(), 0, pages
);
1499 SetLastError(ERROR_OUTOFMEMORY
);
1504 /***********************************************************************
1505 * CryptUIDlgViewCertificateW (CRYPTUI.@)
1507 BOOL WINAPI
CryptUIDlgViewCertificateW(
1508 PCCRYPTUI_VIEWCERTIFICATE_STRUCTW pCertViewInfo
, BOOL
*pfPropertiesChanged
)
1510 static GUID generic_cert_verify
= WINTRUST_ACTION_GENERIC_CERT_VERIFY
;
1511 CRYPTUI_VIEWCERTIFICATE_STRUCTW viewInfo
;
1513 WINTRUST_CERT_INFO cert
;
1515 CRYPT_PROVIDER_SGNR
*signer
;
1516 CRYPT_PROVIDER_CERT
*provCert
= NULL
;
1518 TRACE("(%p, %p)\n", pCertViewInfo
, pfPropertiesChanged
);
1520 if (pCertViewInfo
->dwSize
!= sizeof(CRYPTUI_VIEWCERTIFICATE_STRUCTW
))
1522 SetLastError(ERROR_INVALID_PARAMETER
);
1525 /* Make a local copy in case we have to call WinVerifyTrust ourselves */
1526 memcpy(&viewInfo
, pCertViewInfo
, sizeof(viewInfo
));
1527 if (!viewInfo
.u
.hWVTStateData
)
1529 memset(&wvt
, 0, sizeof(wvt
));
1530 wvt
.cbStruct
= sizeof(wvt
);
1531 wvt
.dwUIChoice
= WTD_UI_NONE
;
1532 if (viewInfo
.dwFlags
&
1533 CRYPTUI_ENABLE_REVOCATION_CHECK_CHAIN_EXCLUDE_ROOT
)
1534 wvt
.fdwRevocationChecks
|= WTD_REVOCATION_CHECK_CHAIN_EXCLUDE_ROOT
;
1535 if (viewInfo
.dwFlags
& CRYPTUI_ENABLE_REVOCATION_CHECK_END_CERT
)
1536 wvt
.fdwRevocationChecks
|= WTD_REVOCATION_CHECK_END_CERT
;
1537 if (viewInfo
.dwFlags
& CRYPTUI_ENABLE_REVOCATION_CHECK_CHAIN
)
1538 wvt
.fdwRevocationChecks
|= WTD_REVOCATION_CHECK_CHAIN
;
1539 wvt
.dwUnionChoice
= WTD_CHOICE_CERT
;
1540 memset(&cert
, 0, sizeof(cert
));
1541 cert
.cbStruct
= sizeof(cert
);
1542 cert
.psCertContext
= (CERT_CONTEXT
*)viewInfo
.pCertContext
;
1543 cert
.chStores
= viewInfo
.cStores
;
1544 cert
.pahStores
= viewInfo
.rghStores
;
1545 wvt
.u
.pCert
= &cert
;
1546 wvt
.dwStateAction
= WTD_STATEACTION_VERIFY
;
1547 WinVerifyTrust(NULL
, &generic_cert_verify
, &wvt
);
1548 viewInfo
.u
.pCryptProviderData
=
1549 WTHelperProvDataFromStateData(wvt
.hWVTStateData
);
1550 signer
= WTHelperGetProvSignerFromChain(
1551 (CRYPT_PROVIDER_DATA
*)viewInfo
.u
.pCryptProviderData
, 0, FALSE
, 0);
1552 provCert
= WTHelperGetProvCertFromChain(signer
, 0);
1557 viewInfo
.u
.pCryptProviderData
=
1558 WTHelperProvDataFromStateData(viewInfo
.u
.hWVTStateData
);
1559 signer
= WTHelperGetProvSignerFromChain(
1560 (CRYPT_PROVIDER_DATA
*)viewInfo
.u
.pCryptProviderData
,
1561 viewInfo
.idxSigner
, viewInfo
.fCounterSigner
,
1562 viewInfo
.idxCounterSigner
);
1563 provCert
= WTHelperGetProvCertFromChain(signer
, viewInfo
.idxCert
);
1568 ret
= show_cert_dialog(&viewInfo
, provCert
, pfPropertiesChanged
);
1569 if (!viewInfo
.u
.hWVTStateData
)
1571 wvt
.dwStateAction
= WTD_STATEACTION_CLOSE
;
1572 WinVerifyTrust(NULL
, &generic_cert_verify
, &wvt
);
1578 static PCCERT_CONTEXT
make_cert_from_file(LPCWSTR fileName
)
1581 DWORD size
, encoding
= X509_ASN_ENCODING
| PKCS_7_ASN_ENCODING
;
1583 PCCERT_CONTEXT cert
;
1585 file
= CreateFileW(fileName
, GENERIC_READ
, FILE_SHARE_READ
, NULL
,
1586 OPEN_EXISTING
, 0, NULL
);
1587 if (file
== INVALID_HANDLE_VALUE
)
1589 WARN("can't open certificate file %s\n", debugstr_w(fileName
));
1592 if ((size
= GetFileSize(file
, NULL
)))
1594 if ((buffer
= HeapAlloc(GetProcessHeap(), 0, size
)))
1597 if (!ReadFile(file
, buffer
, size
, &read
, NULL
) || read
!= size
)
1599 WARN("can't read certificate file %s\n", debugstr_w(fileName
));
1600 HeapFree(GetProcessHeap(), 0, buffer
);
1608 WARN("empty file %s\n", debugstr_w(fileName
));
1613 cert
= CertCreateCertificateContext(encoding
, buffer
, size
);
1614 HeapFree(GetProcessHeap(), 0, buffer
);
1618 /* Decodes a cert's basic constraints extension (either szOID_BASIC_CONSTRAINTS
1619 * or szOID_BASIC_CONSTRAINTS2, whichever is present) to determine if it
1620 * should be a CA. If neither extension is present, returns
1621 * defaultIfNotSpecified.
1623 static BOOL
is_ca_cert(PCCERT_CONTEXT cert
, BOOL defaultIfNotSpecified
)
1625 BOOL isCA
= defaultIfNotSpecified
;
1626 PCERT_EXTENSION ext
= CertFindExtension(szOID_BASIC_CONSTRAINTS
,
1627 cert
->pCertInfo
->cExtension
, cert
->pCertInfo
->rgExtension
);
1631 CERT_BASIC_CONSTRAINTS_INFO
*info
;
1634 if (CryptDecodeObjectEx(X509_ASN_ENCODING
, szOID_BASIC_CONSTRAINTS
,
1635 ext
->Value
.pbData
, ext
->Value
.cbData
, CRYPT_DECODE_ALLOC_FLAG
,
1636 NULL
, (LPBYTE
)&info
, &size
))
1638 if (info
->SubjectType
.cbData
== 1)
1639 isCA
= info
->SubjectType
.pbData
[0] & CERT_CA_SUBJECT_FLAG
;
1645 ext
= CertFindExtension(szOID_BASIC_CONSTRAINTS2
,
1646 cert
->pCertInfo
->cExtension
, cert
->pCertInfo
->rgExtension
);
1649 CERT_BASIC_CONSTRAINTS2_INFO info
;
1650 DWORD size
= sizeof(CERT_BASIC_CONSTRAINTS2_INFO
);
1652 if (CryptDecodeObjectEx(X509_ASN_ENCODING
,
1653 szOID_BASIC_CONSTRAINTS2
, ext
->Value
.pbData
, ext
->Value
.cbData
,
1654 0, NULL
, &info
, &size
))
1661 static HCERTSTORE
choose_store_for_cert(PCCERT_CONTEXT cert
)
1663 static const WCHAR AddressBook
[] = { 'A','d','d','r','e','s','s',
1664 'B','o','o','k',0 };
1665 static const WCHAR CA
[] = { 'C','A',0 };
1668 if (is_ca_cert(cert
, TRUE
))
1671 storeName
= AddressBook
;
1672 return CertOpenStore(CERT_STORE_PROV_SYSTEM_W
, 0, 0,
1673 CERT_SYSTEM_STORE_CURRENT_USER
, storeName
);
1676 BOOL WINAPI
CryptUIWizImport(DWORD dwFlags
, HWND hwndParent
, LPCWSTR pwszWizardTitle
,
1677 PCCRYPTUI_WIZ_IMPORT_SRC_INFO pImportSrc
, HCERTSTORE hDestCertStore
)
1681 const CERT_CONTEXT
*cert
;
1682 BOOL freeCert
= FALSE
;
1684 TRACE("(0x%08x, %p, %s, %p, %p)\n", dwFlags
, hwndParent
, debugstr_w(pwszWizardTitle
),
1685 pImportSrc
, hDestCertStore
);
1687 if (!(dwFlags
& CRYPTUI_WIZ_NO_UI
)) FIXME("UI not implemented\n");
1690 pImportSrc
->dwSize
!= sizeof(CRYPTUI_WIZ_IMPORT_SRC_INFO
))
1692 SetLastError(E_INVALIDARG
);
1696 switch (pImportSrc
->dwSubjectChoice
)
1698 case CRYPTUI_WIZ_IMPORT_SUBJECT_FILE
:
1699 if (!(cert
= make_cert_from_file(pImportSrc
->u
.pwszFileName
)))
1701 WARN("unable to create certificate context\n");
1707 case CRYPTUI_WIZ_IMPORT_SUBJECT_CERT_CONTEXT
:
1708 cert
= pImportSrc
->u
.pCertContext
;
1711 SetLastError(E_INVALIDARG
);
1716 FIXME("source type not implemented: %u\n", pImportSrc
->dwSubjectChoice
);
1717 SetLastError(E_INVALIDARG
);
1720 if (hDestCertStore
) store
= hDestCertStore
;
1723 if (!(store
= choose_store_for_cert(cert
)))
1725 WARN("unable to open certificate store\n");
1726 CertFreeCertificateContext(cert
);
1730 ret
= CertAddCertificateContextToStore(store
, cert
, CERT_STORE_ADD_REPLACE_EXISTING
, NULL
);
1732 if (!hDestCertStore
) CertCloseStore(store
, 0);
1734 CertFreeCertificateContext(cert
);