2 * Copyright 2001 Rein Klazes
3 * Copyright 2007 Juan Lang
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation; either
8 * version 2.1 of the License, or (at your option) any later version.
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
15 * You should have received a copy of the GNU Lesser General Public
16 * License along with this library; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
24 #define NONAMELESSUNION
37 #include "cryptuiapi.h"
38 #include "wintrust_priv.h"
39 #include "wine/debug.h"
41 WINE_DEFAULT_DEBUG_CHANNEL(wintrust
);
44 /* Utility functions */
45 void * WINAPI
WINTRUST_Alloc(DWORD cb
)
47 return HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, cb
);
50 static void* WINTRUST_ReAlloc(void *ptr
, DWORD cb
) __WINE_ALLOC_SIZE(2);
51 static void* WINTRUST_ReAlloc(void *ptr
, DWORD cb
)
53 return HeapReAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, ptr
, cb
);
56 void WINAPI
WINTRUST_Free(void *p
)
58 HeapFree(GetProcessHeap(), 0, p
);
61 /***********************************************************************
62 * DllMain (WINTRUST.@)
64 BOOL WINAPI
DllMain( HINSTANCE inst
, DWORD reason
, LPVOID reserved
)
68 case DLL_PROCESS_ATTACH
:
69 DisableThreadLibraryCalls( inst
);
75 /***********************************************************************
76 * TrustIsCertificateSelfSigned (WINTRUST.@)
78 BOOL WINAPI
TrustIsCertificateSelfSigned( PCCERT_CONTEXT cert
)
85 if ((ext
= CertFindExtension(szOID_AUTHORITY_KEY_IDENTIFIER2
,
86 cert
->pCertInfo
->cExtension
, cert
->pCertInfo
->rgExtension
)))
88 CERT_AUTHORITY_KEY_ID2_INFO
*info
;
90 ret
= CryptDecodeObjectEx(cert
->dwCertEncodingType
,
91 X509_AUTHORITY_KEY_ID2
, ext
->Value
.pbData
, ext
->Value
.cbData
,
92 CRYPT_DECODE_ALLOC_FLAG
| CRYPT_DECODE_NOCOPY_FLAG
, NULL
,
96 if (info
->AuthorityCertIssuer
.cAltEntry
&&
97 info
->AuthorityCertSerialNumber
.cbData
)
99 PCERT_ALT_NAME_ENTRY directoryName
= NULL
;
102 for (i
= 0; !directoryName
&&
103 i
< info
->AuthorityCertIssuer
.cAltEntry
; i
++)
104 if (info
->AuthorityCertIssuer
.rgAltEntry
[i
].dwAltNameChoice
105 == CERT_ALT_NAME_DIRECTORY_NAME
)
107 &info
->AuthorityCertIssuer
.rgAltEntry
[i
];
110 ret
= CertCompareCertificateName(cert
->dwCertEncodingType
,
111 &directoryName
->u
.DirectoryName
, &cert
->pCertInfo
->Issuer
)
112 && CertCompareIntegerBlob(&info
->AuthorityCertSerialNumber
,
113 &cert
->pCertInfo
->SerialNumber
);
117 FIXME("no supported name type in authority key id2\n");
121 else if (info
->KeyId
.cbData
)
123 ret
= CertGetCertificateContextProperty(cert
,
124 CERT_KEY_IDENTIFIER_PROP_ID
, NULL
, &size
);
125 if (ret
&& size
== info
->KeyId
.cbData
)
127 LPBYTE buf
= CryptMemAlloc(size
);
131 CertGetCertificateContextProperty(cert
,
132 CERT_KEY_IDENTIFIER_PROP_ID
, buf
, &size
);
133 ret
= !memcmp(buf
, info
->KeyId
.pbData
, size
);
145 else if ((ext
= CertFindExtension(szOID_AUTHORITY_KEY_IDENTIFIER
,
146 cert
->pCertInfo
->cExtension
, cert
->pCertInfo
->rgExtension
)))
148 CERT_AUTHORITY_KEY_ID_INFO
*info
;
150 ret
= CryptDecodeObjectEx(cert
->dwCertEncodingType
,
151 X509_AUTHORITY_KEY_ID
, ext
->Value
.pbData
, ext
->Value
.cbData
,
152 CRYPT_DECODE_ALLOC_FLAG
| CRYPT_DECODE_NOCOPY_FLAG
, NULL
,
156 if (info
->CertIssuer
.cbData
&& info
->CertSerialNumber
.cbData
)
158 ret
= CertCompareCertificateName(cert
->dwCertEncodingType
,
159 &info
->CertIssuer
, &cert
->pCertInfo
->Issuer
) &&
160 CertCompareIntegerBlob(&info
->CertSerialNumber
,
161 &cert
->pCertInfo
->SerialNumber
);
163 else if (info
->KeyId
.cbData
)
165 ret
= CertGetCertificateContextProperty(cert
,
166 CERT_KEY_IDENTIFIER_PROP_ID
, NULL
, &size
);
167 if (ret
&& size
== info
->KeyId
.cbData
)
169 LPBYTE buf
= CryptMemAlloc(size
);
173 CertGetCertificateContextProperty(cert
,
174 CERT_KEY_IDENTIFIER_PROP_ID
, buf
, &size
);
175 ret
= !memcmp(buf
, info
->KeyId
.pbData
, size
);
190 ret
= CertCompareCertificateName(cert
->dwCertEncodingType
,
191 &cert
->pCertInfo
->Subject
, &cert
->pCertInfo
->Issuer
);
195 typedef HRESULT (WINAPI
*wintrust_step_func
)(CRYPT_PROVIDER_DATA
*data
);
199 wintrust_step_func func
;
203 static DWORD
WINTRUST_ExecuteSteps(const struct wintrust_step
*steps
,
204 DWORD numSteps
, CRYPT_PROVIDER_DATA
*provData
)
206 DWORD i
, err
= ERROR_SUCCESS
;
208 for (i
= 0; !err
&& i
< numSteps
; i
++)
210 err
= steps
[i
].func(provData
);
212 err
= provData
->padwTrustStepErrors
[steps
[i
].error_index
];
217 static CRYPT_PROVIDER_DATA
*WINTRUST_AllocateProviderData(void)
219 CRYPT_PROVIDER_DATA
*provData
;
221 provData
= WINTRUST_Alloc(sizeof(CRYPT_PROVIDER_DATA
));
224 provData
->cbStruct
= sizeof(CRYPT_PROVIDER_DATA
);
226 provData
->padwTrustStepErrors
=
227 WINTRUST_Alloc(TRUSTERROR_MAX_STEPS
* sizeof(DWORD
));
228 if (!provData
->padwTrustStepErrors
)
230 provData
->cdwTrustStepErrors
= TRUSTERROR_MAX_STEPS
;
232 provData
->u
.pPDSip
= WINTRUST_Alloc(sizeof(PROVDATA_SIP
));
233 if (!provData
->u
.pPDSip
)
235 provData
->u
.pPDSip
->cbStruct
= sizeof(PROVDATA_SIP
);
237 provData
->psPfns
= WINTRUST_Alloc(sizeof(CRYPT_PROVIDER_FUNCTIONS
));
238 if (!provData
->psPfns
)
240 provData
->psPfns
->cbStruct
= sizeof(CRYPT_PROVIDER_FUNCTIONS
);
246 WINTRUST_Free(provData
->padwTrustStepErrors
);
247 WINTRUST_Free(provData
->u
.pPDSip
);
248 WINTRUST_Free(provData
->psPfns
);
249 WINTRUST_Free(provData
);
254 /* Adds trust steps for each function in psPfns. Assumes steps has at least
255 * 5 entries. Returns the number of steps added.
257 static DWORD
WINTRUST_AddTrustStepsFromFunctions(struct wintrust_step
*steps
,
258 const CRYPT_PROVIDER_FUNCTIONS
*psPfns
)
262 if (psPfns
->pfnInitialize
)
264 steps
[numSteps
].func
= psPfns
->pfnInitialize
;
265 steps
[numSteps
++].error_index
= TRUSTERROR_STEP_FINAL_WVTINIT
;
267 if (psPfns
->pfnObjectTrust
)
269 steps
[numSteps
].func
= psPfns
->pfnObjectTrust
;
270 steps
[numSteps
++].error_index
= TRUSTERROR_STEP_FINAL_OBJPROV
;
272 if (psPfns
->pfnSignatureTrust
)
274 steps
[numSteps
].func
= psPfns
->pfnSignatureTrust
;
275 steps
[numSteps
++].error_index
= TRUSTERROR_STEP_FINAL_SIGPROV
;
277 if (psPfns
->pfnCertificateTrust
)
279 steps
[numSteps
].func
= psPfns
->pfnCertificateTrust
;
280 steps
[numSteps
++].error_index
= TRUSTERROR_STEP_FINAL_CERTPROV
;
282 if (psPfns
->pfnFinalPolicy
)
284 steps
[numSteps
].func
= psPfns
->pfnFinalPolicy
;
285 steps
[numSteps
++].error_index
= TRUSTERROR_STEP_FINAL_POLICYPROV
;
290 static LONG
WINTRUST_DefaultVerify(HWND hwnd
, GUID
*actionID
,
293 DWORD err
= ERROR_SUCCESS
, numSteps
= 0;
294 CRYPT_PROVIDER_DATA
*provData
;
296 struct wintrust_step verifySteps
[5];
298 TRACE("(%p, %s, %p)\n", hwnd
, debugstr_guid(actionID
), data
);
300 provData
= WINTRUST_AllocateProviderData();
302 return ERROR_OUTOFMEMORY
;
304 ret
= WintrustLoadFunctionPointers(actionID
, provData
->psPfns
);
307 err
= GetLastError();
311 data
->hWVTStateData
= provData
;
312 provData
->pWintrustData
= data
;
313 if (hwnd
== INVALID_HANDLE_VALUE
)
314 provData
->hWndParent
= GetDesktopWindow();
316 provData
->hWndParent
= hwnd
;
317 provData
->pgActionID
= actionID
;
318 WintrustGetRegPolicyFlags(&provData
->dwRegPolicySettings
);
320 numSteps
= WINTRUST_AddTrustStepsFromFunctions(verifySteps
,
322 err
= WINTRUST_ExecuteSteps(verifySteps
, numSteps
, provData
);
328 WINTRUST_Free(provData
->padwTrustStepErrors
);
329 WINTRUST_Free(provData
->u
.pPDSip
);
330 WINTRUST_Free(provData
->psPfns
);
331 WINTRUST_Free(provData
);
334 TRACE("returning %08x\n", err
);
338 static LONG
WINTRUST_DefaultClose(HWND hwnd
, GUID
*actionID
,
341 DWORD err
= ERROR_SUCCESS
;
342 CRYPT_PROVIDER_DATA
*provData
= data
->hWVTStateData
;
344 TRACE("(%p, %s, %p)\n", hwnd
, debugstr_guid(actionID
), data
);
348 if (provData
->psPfns
->pfnCleanupPolicy
)
349 err
= provData
->psPfns
->pfnCleanupPolicy(provData
);
351 WINTRUST_Free(provData
->padwTrustStepErrors
);
352 WINTRUST_Free(provData
->u
.pPDSip
);
353 WINTRUST_Free(provData
->psPfns
);
354 WINTRUST_Free(provData
);
355 data
->hWVTStateData
= NULL
;
357 TRACE("returning %08x\n", err
);
361 static LONG
WINTRUST_DefaultVerifyAndClose(HWND hwnd
, GUID
*actionID
,
366 TRACE("(%p, %s, %p)\n", hwnd
, debugstr_guid(actionID
), data
);
368 err
= WINTRUST_DefaultVerify(hwnd
, actionID
, data
);
369 WINTRUST_DefaultClose(hwnd
, actionID
, data
);
370 TRACE("returning %08x\n", err
);
374 static LONG
WINTRUST_PublishedSoftware(HWND hwnd
, GUID
*actionID
,
377 WINTRUST_DATA wintrust_data
= { sizeof(wintrust_data
), 0 };
378 /* Undocumented: the published software action is passed a path,
379 * and pSIPClientData points to a WIN_TRUST_SUBJECT_FILE.
381 LPWIN_TRUST_SUBJECT_FILE subjectFile
= data
->pSIPClientData
;
382 WINTRUST_FILE_INFO fileInfo
= { sizeof(fileInfo
), 0 };
384 TRACE("subjectFile->hFile: %p\n", subjectFile
->hFile
);
385 TRACE("subjectFile->lpPath: %s\n", debugstr_w(subjectFile
->lpPath
));
386 fileInfo
.pcwszFilePath
= subjectFile
->lpPath
;
387 fileInfo
.hFile
= subjectFile
->hFile
;
388 wintrust_data
.u
.pFile
= &fileInfo
;
389 wintrust_data
.dwUnionChoice
= WTD_CHOICE_FILE
;
390 wintrust_data
.dwUIChoice
= WTD_UI_NONE
;
392 return WINTRUST_DefaultVerifyAndClose(hwnd
, actionID
, &wintrust_data
);
395 /* Sadly, the function to load the cert for the CERT_CERTIFICATE_ACTION_VERIFY
396 * action is not stored in the registry and is located in wintrust, not in
397 * cryptdlg along with the rest of the implementation (verified by running the
398 * action with a native wintrust.dll.)
400 static HRESULT WINAPI
WINTRUST_CertVerifyObjTrust(CRYPT_PROVIDER_DATA
*data
)
404 TRACE("(%p)\n", data
);
406 if (!data
->padwTrustStepErrors
)
409 switch (data
->pWintrustData
->dwUnionChoice
)
411 case WTD_CHOICE_BLOB
:
412 if (data
->pWintrustData
->u
.pBlob
&&
413 WVT_IS_CBSTRUCT_GT_MEMBEROFFSET(WINTRUST_BLOB_INFO
,
414 data
->pWintrustData
->u
.pBlob
->cbStruct
, pbMemObject
) &&
415 data
->pWintrustData
->u
.pBlob
->cbMemObject
==
416 sizeof(CERT_VERIFY_CERTIFICATE_TRUST
) &&
417 data
->pWintrustData
->u
.pBlob
->pbMemObject
)
419 CERT_VERIFY_CERTIFICATE_TRUST
*pCert
=
420 (CERT_VERIFY_CERTIFICATE_TRUST
*)
421 data
->pWintrustData
->u
.pBlob
->pbMemObject
;
423 if (pCert
->cbSize
== sizeof(CERT_VERIFY_CERTIFICATE_TRUST
) &&
426 CRYPT_PROVIDER_SGNR signer
= { sizeof(signer
), { 0 } };
430 /* Add a signer with nothing but the time to verify, so we can
433 GetSystemTime(&sysTime
);
434 SystemTimeToFileTime(&sysTime
, &signer
.sftVerifyAsOf
);
435 ret
= data
->psPfns
->pfnAddSgnr2Chain(data
, FALSE
, 0, &signer
);
438 ret
= data
->psPfns
->pfnAddCert2Chain(data
, 0, FALSE
, 0,
442 for (i
= 0; ret
&& i
< pCert
->cRootStores
; i
++)
443 ret
= data
->psPfns
->pfnAddStore2Chain(data
,
444 pCert
->rghstoreRoots
[i
]);
445 for (i
= 0; ret
&& i
< pCert
->cStores
; i
++)
446 ret
= data
->psPfns
->pfnAddStore2Chain(data
,
447 pCert
->rghstoreCAs
[i
]);
448 for (i
= 0; ret
&& i
< pCert
->cTrustStores
; i
++)
449 ret
= data
->psPfns
->pfnAddStore2Chain(data
,
450 pCert
->rghstoreTrust
[i
]);
454 SetLastError(ERROR_INVALID_PARAMETER
);
460 SetLastError(ERROR_INVALID_PARAMETER
);
465 FIXME("unimplemented for %d\n", data
->pWintrustData
->dwUnionChoice
);
466 SetLastError(ERROR_INVALID_PARAMETER
);
472 data
->padwTrustStepErrors
[TRUSTERROR_STEP_FINAL_OBJPROV
] =
474 TRACE("returning %d (%08x)\n", ret
? S_OK
: S_FALSE
,
475 data
->padwTrustStepErrors
[TRUSTERROR_STEP_FINAL_OBJPROV
]);
476 return ret
? S_OK
: S_FALSE
;
479 static LONG
WINTRUST_CertVerify(HWND hwnd
, GUID
*actionID
,
482 DWORD err
= ERROR_SUCCESS
, numSteps
= 0;
483 CRYPT_PROVIDER_DATA
*provData
;
485 struct wintrust_step verifySteps
[5];
487 TRACE("(%p, %s, %p)\n", hwnd
, debugstr_guid(actionID
), data
);
489 provData
= WINTRUST_AllocateProviderData();
491 return ERROR_OUTOFMEMORY
;
493 ret
= WintrustLoadFunctionPointers(actionID
, provData
->psPfns
);
496 err
= GetLastError();
499 if (!provData
->psPfns
->pfnObjectTrust
)
500 provData
->psPfns
->pfnObjectTrust
= WINTRUST_CertVerifyObjTrust
;
501 /* Not sure why, but native skips the policy check */
502 provData
->psPfns
->pfnCertCheckPolicy
= NULL
;
504 data
->hWVTStateData
= provData
;
505 provData
->pWintrustData
= data
;
506 if (hwnd
== INVALID_HANDLE_VALUE
)
507 provData
->hWndParent
= GetDesktopWindow();
509 provData
->hWndParent
= hwnd
;
510 provData
->pgActionID
= actionID
;
511 WintrustGetRegPolicyFlags(&provData
->dwRegPolicySettings
);
513 numSteps
= WINTRUST_AddTrustStepsFromFunctions(verifySteps
,
515 err
= WINTRUST_ExecuteSteps(verifySteps
, numSteps
, provData
);
521 WINTRUST_Free(provData
->padwTrustStepErrors
);
522 WINTRUST_Free(provData
->u
.pPDSip
);
523 WINTRUST_Free(provData
->psPfns
);
524 WINTRUST_Free(provData
);
527 TRACE("returning %08x\n", err
);
531 static LONG
WINTRUST_CertVerifyAndClose(HWND hwnd
, GUID
*actionID
,
536 TRACE("(%p, %s, %p)\n", hwnd
, debugstr_guid(actionID
), data
);
538 err
= WINTRUST_CertVerify(hwnd
, actionID
, data
);
539 WINTRUST_DefaultClose(hwnd
, actionID
, data
);
540 TRACE("returning %08x\n", err
);
544 static LONG
WINTRUST_CertActionVerify(HWND hwnd
, GUID
*actionID
,
548 LONG err
= ERROR_SUCCESS
;
550 if (WVT_ISINSTRUCT(WINTRUST_DATA
, data
->cbStruct
, dwStateAction
))
551 stateAction
= data
->dwStateAction
;
554 TRACE("no dwStateAction, assuming WTD_STATEACTION_IGNORE\n");
555 stateAction
= WTD_STATEACTION_IGNORE
;
559 case WTD_STATEACTION_IGNORE
:
560 err
= WINTRUST_CertVerifyAndClose(hwnd
, actionID
, data
);
562 case WTD_STATEACTION_VERIFY
:
563 err
= WINTRUST_CertVerify(hwnd
, actionID
, data
);
565 case WTD_STATEACTION_CLOSE
:
566 err
= WINTRUST_DefaultClose(hwnd
, actionID
, data
);
569 FIXME("unimplemented for %d\n", data
->dwStateAction
);
574 static void dump_file_info(WINTRUST_FILE_INFO
*pFile
)
576 TRACE("%p\n", pFile
);
579 TRACE("cbStruct: %d\n", pFile
->cbStruct
);
580 TRACE("pcwszFilePath: %s\n", debugstr_w(pFile
->pcwszFilePath
));
581 TRACE("hFile: %p\n", pFile
->hFile
);
582 TRACE("pgKnownSubject: %s\n", debugstr_guid(pFile
->pgKnownSubject
));
586 static void dump_catalog_info(WINTRUST_CATALOG_INFO
*catalog
)
588 TRACE("%p\n", catalog
);
591 TRACE("cbStruct: %d\n", catalog
->cbStruct
);
592 TRACE("dwCatalogVersion: %d\n", catalog
->dwCatalogVersion
);
593 TRACE("pcwszCatalogFilePath: %s\n",
594 debugstr_w(catalog
->pcwszCatalogFilePath
));
595 TRACE("pcwszMemberTag: %s\n", debugstr_w(catalog
->pcwszMemberTag
));
596 TRACE("pcwszMemberFilePath: %s\n",
597 debugstr_w(catalog
->pcwszMemberFilePath
));
598 TRACE("hMemberFile: %p\n", catalog
->hMemberFile
);
599 TRACE("pbCalculatedFileHash: %p\n", catalog
->pbCalculatedFileHash
);
600 TRACE("cbCalculatedFileHash: %d\n", catalog
->cbCalculatedFileHash
);
601 TRACE("pcCatalogContext: %p\n", catalog
->pcCatalogContext
);
605 static void dump_blob_info(WINTRUST_BLOB_INFO
*blob
)
610 TRACE("cbStruct: %d\n", blob
->cbStruct
);
611 TRACE("gSubject: %s\n", debugstr_guid(&blob
->gSubject
));
612 TRACE("pcwszDisplayName: %s\n", debugstr_w(blob
->pcwszDisplayName
));
613 TRACE("cbMemObject: %d\n", blob
->cbMemObject
);
614 TRACE("pbMemObject: %p\n", blob
->pbMemObject
);
615 TRACE("cbMemSignedMsg: %d\n", blob
->cbMemSignedMsg
);
616 TRACE("pbMemSignedMsg: %p\n", blob
->pbMemSignedMsg
);
620 static void dump_sgnr_info(WINTRUST_SGNR_INFO
*sgnr
)
625 TRACE("cbStruct: %d\n", sgnr
->cbStruct
);
626 TRACE("pcwszDisplayName: %s\n", debugstr_w(sgnr
->pcwszDisplayName
));
627 TRACE("psSignerInfo: %p\n", sgnr
->psSignerInfo
);
628 TRACE("chStores: %d\n", sgnr
->chStores
);
632 static void dump_cert_info(WINTRUST_CERT_INFO
*cert
)
637 TRACE("cbStruct: %d\n", cert
->cbStruct
);
638 TRACE("pcwszDisplayName: %s\n", debugstr_w(cert
->pcwszDisplayName
));
639 TRACE("psCertContext: %p\n", cert
->psCertContext
);
640 TRACE("chStores: %d\n", cert
->chStores
);
641 TRACE("dwFlags: %08x\n", cert
->dwFlags
);
642 TRACE("psftVerifyAsOf: %p\n", cert
->psftVerifyAsOf
);
646 static void dump_wintrust_data(WINTRUST_DATA
*data
)
651 TRACE("cbStruct: %d\n", data
->cbStruct
);
652 TRACE("pPolicyCallbackData: %p\n", data
->pPolicyCallbackData
);
653 TRACE("pSIPClientData: %p\n", data
->pSIPClientData
);
654 TRACE("dwUIChoice: %d\n", data
->dwUIChoice
);
655 TRACE("fdwRevocationChecks: %08x\n", data
->fdwRevocationChecks
);
656 TRACE("dwUnionChoice: %d\n", data
->dwUnionChoice
);
657 switch (data
->dwUnionChoice
)
659 case WTD_CHOICE_FILE
:
660 dump_file_info(data
->u
.pFile
);
662 case WTD_CHOICE_CATALOG
:
663 dump_catalog_info(data
->u
.pCatalog
);
665 case WTD_CHOICE_BLOB
:
666 dump_blob_info(data
->u
.pBlob
);
668 case WTD_CHOICE_SIGNER
:
669 dump_sgnr_info(data
->u
.pSgnr
);
671 case WTD_CHOICE_CERT
:
672 dump_cert_info(data
->u
.pCert
);
675 TRACE("dwStateAction: %d\n", data
->dwStateAction
);
676 TRACE("hWVTStateData: %p\n", data
->hWVTStateData
);
677 TRACE("pwszURLReference: %s\n", debugstr_w(data
->pwszURLReference
));
678 TRACE("dwProvFlags: %08x\n", data
->dwProvFlags
);
679 TRACE("dwUIContext: %d\n", data
->dwUIContext
);
683 /***********************************************************************
684 * WinVerifyTrust (WINTRUST.@)
686 * Verifies an object by calling the specified trust provider.
689 * hwnd [I] Handle to a caller window.
690 * ActionID [I] Pointer to a GUID that identifies the action to perform.
691 * ActionData [I] Information used by the trust provider to verify the object.
695 * Failure: A TRUST_E_* error code.
698 * Trust providers can be found at:
699 * HKLM\SOFTWARE\Microsoft\Cryptography\Providers\Trust\
701 LONG WINAPI
WinVerifyTrust( HWND hwnd
, GUID
*ActionID
, LPVOID ActionData
)
703 static const GUID unknown
= { 0xC689AAB8, 0x8E78, 0x11D0, { 0x8C,0x47,
704 0x00,0xC0,0x4F,0xC2,0x95,0xEE } };
705 static const GUID published_software
= WIN_SPUB_ACTION_PUBLISHED_SOFTWARE
;
706 static const GUID generic_verify_v2
= WINTRUST_ACTION_GENERIC_VERIFY_V2
;
707 static const GUID generic_cert_verify
= WINTRUST_ACTION_GENERIC_CERT_VERIFY
;
708 static const GUID generic_chain_verify
= WINTRUST_ACTION_GENERIC_CHAIN_VERIFY
;
709 static const GUID cert_action_verify
= CERT_CERTIFICATE_ACTION_VERIFY
;
710 LONG err
= ERROR_SUCCESS
;
711 WINTRUST_DATA
*actionData
= ActionData
;
713 TRACE("(%p, %s, %p)\n", hwnd
, debugstr_guid(ActionID
), ActionData
);
714 dump_wintrust_data(ActionData
);
716 /* Support for known old-style callers: */
717 if (IsEqualGUID(ActionID
, &published_software
))
718 err
= WINTRUST_PublishedSoftware(hwnd
, ActionID
, ActionData
);
719 else if (IsEqualGUID(ActionID
, &cert_action_verify
))
720 err
= WINTRUST_CertActionVerify(hwnd
, ActionID
, ActionData
);
725 /* Check known actions to warn of possible problems */
726 if (!IsEqualGUID(ActionID
, &unknown
) &&
727 !IsEqualGUID(ActionID
, &generic_verify_v2
) &&
728 !IsEqualGUID(ActionID
, &generic_cert_verify
) &&
729 !IsEqualGUID(ActionID
, &generic_chain_verify
))
730 WARN("unknown action %s, default behavior may not be right\n",
731 debugstr_guid(ActionID
));
732 if (WVT_ISINSTRUCT(WINTRUST_DATA
, actionData
->cbStruct
, dwStateAction
))
733 stateAction
= actionData
->dwStateAction
;
736 TRACE("no dwStateAction, assuming WTD_STATEACTION_IGNORE\n");
737 stateAction
= WTD_STATEACTION_IGNORE
;
741 case WTD_STATEACTION_IGNORE
:
742 err
= WINTRUST_DefaultVerifyAndClose(hwnd
, ActionID
, ActionData
);
744 case WTD_STATEACTION_VERIFY
:
745 err
= WINTRUST_DefaultVerify(hwnd
, ActionID
, ActionData
);
747 case WTD_STATEACTION_CLOSE
:
748 err
= WINTRUST_DefaultClose(hwnd
, ActionID
, ActionData
);
751 FIXME("unimplemented for %d\n", actionData
->dwStateAction
);
755 TRACE("returning %08x\n", err
);
759 /***********************************************************************
760 * WinVerifyTrustEx (WINTRUST.@)
762 HRESULT WINAPI
WinVerifyTrustEx( HWND hwnd
, GUID
*ActionID
,
763 WINTRUST_DATA
* ActionData
)
765 return WinVerifyTrust(hwnd
, ActionID
, ActionData
);
768 /***********************************************************************
769 * WTHelperGetProvSignerFromChain (WINTRUST.@)
771 CRYPT_PROVIDER_SGNR
* WINAPI
WTHelperGetProvSignerFromChain(
772 CRYPT_PROVIDER_DATA
*pProvData
, DWORD idxSigner
, BOOL fCounterSigner
,
773 DWORD idxCounterSigner
)
775 CRYPT_PROVIDER_SGNR
*sgnr
;
777 TRACE("(%p %d %d %d)\n", pProvData
, idxSigner
, fCounterSigner
,
780 if (idxSigner
>= pProvData
->csSigners
|| !pProvData
->pasSigners
)
782 sgnr
= &pProvData
->pasSigners
[idxSigner
];
785 if (idxCounterSigner
>= sgnr
->csCounterSigners
||
786 !sgnr
->pasCounterSigners
)
788 sgnr
= &sgnr
->pasCounterSigners
[idxCounterSigner
];
790 TRACE("returning %p\n", sgnr
);
794 /***********************************************************************
795 * WTHelperGetProvCertFromChain (WINTRUST.@)
797 CRYPT_PROVIDER_CERT
* WINAPI
WTHelperGetProvCertFromChain(
798 CRYPT_PROVIDER_SGNR
*pSgnr
, DWORD idxCert
)
800 CRYPT_PROVIDER_CERT
*cert
;
802 TRACE("(%p %d)\n", pSgnr
, idxCert
);
804 if (idxCert
>= pSgnr
->csCertChain
|| !pSgnr
->pasCertChain
)
806 cert
= &pSgnr
->pasCertChain
[idxCert
];
807 TRACE("returning %p\n", cert
);
811 CRYPT_PROVIDER_PRIVDATA
*WINAPI
WTHelperGetProvPrivateDataFromChain(
812 CRYPT_PROVIDER_DATA
* pProvData
,
815 CRYPT_PROVIDER_PRIVDATA
*privdata
= NULL
;
818 TRACE("(%p, %s)\n", pProvData
, debugstr_guid(pgProviderID
));
820 for (i
= 0; i
< pProvData
->csProvPrivData
; i
++)
821 if (IsEqualGUID(pgProviderID
, &pProvData
->pasProvPrivData
[i
].gProviderID
))
823 privdata
= &pProvData
->pasProvPrivData
[i
];
830 /***********************************************************************
831 * WTHelperProvDataFromStateData (WINTRUST.@)
833 CRYPT_PROVIDER_DATA
* WINAPI
WTHelperProvDataFromStateData(HANDLE hStateData
)
835 TRACE("%p\n", hStateData
);
839 /***********************************************************************
840 * WTHelperGetFileName(WINTRUST.@)
842 LPCWSTR WINAPI
WTHelperGetFileName(WINTRUST_DATA
*data
)
845 if (data
->dwUnionChoice
== WTD_CHOICE_FILE
)
846 return data
->u
.pFile
->pcwszFilePath
;
851 /***********************************************************************
852 * WTHelperGetFileHandle(WINTRUST.@)
854 HANDLE WINAPI
WTHelperGetFileHandle(WINTRUST_DATA
*data
)
857 if (data
->dwUnionChoice
== WTD_CHOICE_FILE
)
858 return data
->u
.pFile
->hFile
;
860 return INVALID_HANDLE_VALUE
;
863 static BOOL WINAPI
WINTRUST_enumUsages(PCCRYPT_OID_INFO pInfo
, void *pvArg
)
865 PCCRYPT_OID_INFO
**usages
= pvArg
;
872 *usages
= WINTRUST_Alloc(2 * sizeof(PCCRYPT_OID_INFO
));
876 PCCRYPT_OID_INFO
*ptr
;
878 /* Count the existing usages.
879 * FIXME: make sure the new usage doesn't duplicate any in the list?
881 for (cUsages
= 0, ptr
= *usages
; *ptr
; ptr
++, cUsages
++)
883 *usages
= WINTRUST_ReAlloc(*usages
,
884 (cUsages
+ 2) * sizeof(PCCRYPT_OID_INFO
));
888 (*usages
)[cUsages
] = pInfo
;
889 (*usages
)[cUsages
+ 1] = NULL
;
894 SetLastError(ERROR_OUTOFMEMORY
);
900 /***********************************************************************
901 * WTHelperGetKnownUsages(WINTRUST.@)
903 * Enumerates the known enhanced key usages as an array of PCCRYPT_OID_INFOs.
906 * action [In] 1 => allocate and return known usages, 2 => free previously
908 * usages [In/Out] If action == 1, *usages is set to an array of
909 * PCCRYPT_OID_INFO *. The array is terminated with a NULL
911 * If action == 2, *usages is freed.
914 * TRUE on success, FALSE on failure.
916 BOOL WINAPI
WTHelperGetKnownUsages(DWORD action
, PCCRYPT_OID_INFO
**usages
)
920 TRACE("(%d, %p)\n", action
, usages
);
924 SetLastError(ERROR_INVALID_PARAMETER
);
931 ret
= CryptEnumOIDInfo(CRYPT_ENHKEY_USAGE_OID_GROUP_ID
, 0, usages
,
932 WINTRUST_enumUsages
);
934 else if (action
== 2)
936 WINTRUST_Free(*usages
);
942 WARN("unknown action %d\n", action
);
943 SetLastError(ERROR_INVALID_PARAMETER
);
949 static const WCHAR Software_Publishing
[] = {
950 'S','o','f','t','w','a','r','e','\\',
951 'M','i','c','r','o','s','o','f','t','\\',
952 'W','i','n','d','o','w','s','\\',
953 'C','u','r','r','e','n','t','V','e','r','s','i','o','n','\\',
954 'W','i','n','t','r','u','s','t','\\',
955 'T','r','u','s','t',' ','P','r','o','v','i','d','e','r','s','\\',
956 'S','o','f','t','w','a','r','e',' ',
957 'P','u','b','l','i','s','h','i','n','g',0 };
958 static const WCHAR State
[] = { 'S','t','a','t','e',0 };
960 /***********************************************************************
961 * WintrustGetRegPolicyFlags (WINTRUST.@)
963 void WINAPI
WintrustGetRegPolicyFlags( DWORD
* pdwPolicyFlags
)
968 TRACE("%p\n", pdwPolicyFlags
);
971 r
= RegCreateKeyExW(HKEY_CURRENT_USER
, Software_Publishing
, 0, NULL
, 0,
972 KEY_READ
, NULL
, &key
, NULL
);
975 DWORD size
= sizeof(DWORD
);
977 r
= RegQueryValueExW(key
, State
, NULL
, NULL
, (LPBYTE
)pdwPolicyFlags
,
982 /* Failed to query, create and return default value */
983 *pdwPolicyFlags
= WTPF_IGNOREREVOCATIONONTS
|
984 WTPF_OFFLINEOKNBU_COM
|
985 WTPF_OFFLINEOKNBU_IND
|
988 WintrustSetRegPolicyFlags(*pdwPolicyFlags
);
993 /***********************************************************************
994 * WintrustSetRegPolicyFlags (WINTRUST.@)
996 BOOL WINAPI
WintrustSetRegPolicyFlags( DWORD dwPolicyFlags
)
1001 TRACE("%x\n", dwPolicyFlags
);
1003 r
= RegCreateKeyExW(HKEY_CURRENT_USER
, Software_Publishing
, 0,
1004 NULL
, 0, KEY_WRITE
, NULL
, &key
, NULL
);
1007 r
= RegSetValueExW(key
, State
, 0, REG_DWORD
, (LPBYTE
)&dwPolicyFlags
,
1011 if (r
) SetLastError(r
);
1012 return r
== ERROR_SUCCESS
;
1015 /* Utility functions */
1017 BOOL WINAPI
WINTRUST_AddStore(CRYPT_PROVIDER_DATA
*data
, HCERTSTORE store
)
1021 TRACE("(%p, %p)\n", data
, store
);
1024 data
->pahStores
= WINTRUST_ReAlloc(data
->pahStores
,
1025 (data
->chStores
+ 1) * sizeof(HCERTSTORE
));
1028 data
->pahStores
= WINTRUST_Alloc(sizeof(HCERTSTORE
));
1031 if (data
->pahStores
)
1033 data
->pahStores
[data
->chStores
++] = CertDuplicateStore(store
);
1037 SetLastError(ERROR_OUTOFMEMORY
);
1041 BOOL WINAPI
WINTRUST_AddSgnr(CRYPT_PROVIDER_DATA
*data
,
1042 BOOL fCounterSigner
, DWORD idxSigner
, CRYPT_PROVIDER_SGNR
*sgnr
)
1046 TRACE("(%p, %d, %d, %p)\n", data
, fCounterSigner
, idxSigner
, sgnr
);
1048 if (sgnr
->cbStruct
> sizeof(CRYPT_PROVIDER_SGNR
))
1050 SetLastError(ERROR_INVALID_PARAMETER
);
1055 FIXME("unimplemented for counter signers\n");
1056 SetLastError(ERROR_INVALID_PARAMETER
);
1059 if (data
->csSigners
)
1060 data
->pasSigners
= WINTRUST_ReAlloc(data
->pasSigners
,
1061 (data
->csSigners
+ 1) * sizeof(CRYPT_PROVIDER_SGNR
));
1064 data
->pasSigners
= WINTRUST_Alloc(sizeof(CRYPT_PROVIDER_SGNR
));
1065 data
->csSigners
= 0;
1067 if (data
->pasSigners
)
1069 if (idxSigner
< data
->csSigners
)
1070 memmove(&data
->pasSigners
[idxSigner
],
1071 &data
->pasSigners
[idxSigner
+ 1],
1072 (data
->csSigners
- idxSigner
) * sizeof(CRYPT_PROVIDER_SGNR
));
1074 if (sgnr
->cbStruct
== sizeof(CRYPT_PROVIDER_SGNR
))
1076 /* The PSDK says psSigner should be allocated using pfnAlloc, but
1077 * it doesn't say anything about ownership. Since callers are
1078 * internal, assume ownership is passed, and just store the
1081 memcpy(&data
->pasSigners
[idxSigner
], sgnr
,
1082 sizeof(CRYPT_PROVIDER_SGNR
));
1085 memset(&data
->pasSigners
[idxSigner
], 0,
1086 sizeof(CRYPT_PROVIDER_SGNR
));
1090 SetLastError(ERROR_OUTOFMEMORY
);
1094 BOOL WINAPI
WINTRUST_AddCert(CRYPT_PROVIDER_DATA
*data
, DWORD idxSigner
,
1095 BOOL fCounterSigner
, DWORD idxCounterSigner
, PCCERT_CONTEXT pCert2Add
)
1099 TRACE("(%p, %d, %d, %d, %p)\n", data
, idxSigner
, fCounterSigner
,
1100 idxSigner
, pCert2Add
);
1104 FIXME("unimplemented for counter signers\n");
1105 SetLastError(ERROR_INVALID_PARAMETER
);
1108 if (data
->pasSigners
[idxSigner
].csCertChain
)
1109 data
->pasSigners
[idxSigner
].pasCertChain
=
1110 WINTRUST_ReAlloc(data
->pasSigners
[idxSigner
].pasCertChain
,
1111 (data
->pasSigners
[idxSigner
].csCertChain
+ 1) *
1112 sizeof(CRYPT_PROVIDER_CERT
));
1115 data
->pasSigners
[idxSigner
].pasCertChain
=
1116 WINTRUST_Alloc(sizeof(CRYPT_PROVIDER_CERT
));
1117 data
->pasSigners
[idxSigner
].csCertChain
= 0;
1119 if (data
->pasSigners
[idxSigner
].pasCertChain
)
1121 CRYPT_PROVIDER_CERT
*cert
= &data
->pasSigners
[idxSigner
].pasCertChain
[
1122 data
->pasSigners
[idxSigner
].csCertChain
];
1124 cert
->cbStruct
= sizeof(CRYPT_PROVIDER_CERT
);
1125 cert
->pCert
= CertDuplicateCertificateContext(pCert2Add
);
1126 data
->pasSigners
[idxSigner
].csCertChain
++;
1130 SetLastError(ERROR_OUTOFMEMORY
);
1134 BOOL WINAPI
WINTRUST_AddPrivData(CRYPT_PROVIDER_DATA
*data
,
1135 CRYPT_PROVIDER_PRIVDATA
*pPrivData2Add
)
1139 TRACE("(%p, %p)\n", data
, pPrivData2Add
);
1141 if (pPrivData2Add
->cbStruct
> sizeof(CRYPT_PROVIDER_PRIVDATA
))
1143 SetLastError(ERROR_INVALID_PARAMETER
);
1144 WARN("invalid struct size\n");
1147 if (data
->csProvPrivData
)
1148 data
->pasProvPrivData
= WINTRUST_ReAlloc(data
->pasProvPrivData
,
1149 (data
->csProvPrivData
+ 1) * sizeof(CRYPT_PROVIDER_SGNR
));
1152 data
->pasProvPrivData
= WINTRUST_Alloc(sizeof(CRYPT_PROVIDER_SGNR
));
1153 data
->csProvPrivData
= 0;
1155 if (data
->pasProvPrivData
)
1159 for (i
= 0; i
< data
->csProvPrivData
; i
++)
1160 if (IsEqualGUID(&pPrivData2Add
->gProviderID
, &data
->pasProvPrivData
[i
]))
1163 data
->pasProvPrivData
[i
] = *pPrivData2Add
;
1164 if (i
== data
->csProvPrivData
)
1165 data
->csProvPrivData
++;
1168 SetLastError(ERROR_OUTOFMEMORY
);
1172 /***********************************************************************
1173 * OpenPersonalTrustDBDialog (WINTRUST.@)
1175 * Opens the certificate manager dialog, showing only the stores that
1176 * contain trusted software publishers.
1179 * hwnd [I] handle of parent window
1182 * TRUE if the dialog could be opened, FALSE if not.
1184 BOOL WINAPI
OpenPersonalTrustDBDialog(HWND hwnd
)
1186 CRYPTUI_CERT_MGR_STRUCT uiCertMgr
;
1188 uiCertMgr
.dwSize
= sizeof(uiCertMgr
);
1189 uiCertMgr
.hwndParent
= hwnd
;
1190 uiCertMgr
.dwFlags
= CRYPTUI_CERT_MGR_PUBLISHER_TAB
;
1191 uiCertMgr
.pwszTitle
= NULL
;
1192 uiCertMgr
.pszInitUsageOID
= NULL
;
1193 return CryptUIDlgCertMgr(&uiCertMgr
);
1196 /***********************************************************************
1197 * WTHelperCertCheckValidSignature
1199 HRESULT WINAPI
WTHelperCertCheckValidSignature(CRYPT_PROVIDER_DATA
*pProvData
)