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 )
83 ret = CertCompareCertificateName(cert->dwCertEncodingType,
84 &cert->pCertInfo->Subject, &cert->pCertInfo->Issuer);
88 typedef HRESULT (WINAPI *wintrust_step_func)(CRYPT_PROVIDER_DATA *data);
92 wintrust_step_func func;
96 static DWORD WINTRUST_ExecuteSteps(const struct wintrust_step *steps,
97 DWORD numSteps, CRYPT_PROVIDER_DATA *provData)
99 DWORD i, err = ERROR_SUCCESS;
101 for (i = 0; !err && i < numSteps; i++)
103 err = steps[i].func(provData);
105 err = provData->padwTrustStepErrors[steps[i].error_index];
110 static CRYPT_PROVIDER_DATA *WINTRUST_AllocateProviderData(void)
112 CRYPT_PROVIDER_DATA *provData;
114 provData = WINTRUST_Alloc(sizeof(CRYPT_PROVIDER_DATA));
117 provData->cbStruct = sizeof(CRYPT_PROVIDER_DATA);
119 provData->padwTrustStepErrors =
120 WINTRUST_Alloc(TRUSTERROR_MAX_STEPS * sizeof(DWORD));
121 if (!provData->padwTrustStepErrors)
123 provData->cdwTrustStepErrors = TRUSTERROR_MAX_STEPS;
125 provData->u.pPDSip = WINTRUST_Alloc(sizeof(PROVDATA_SIP));
126 if (!provData->u.pPDSip)
128 provData->u.pPDSip->cbStruct = sizeof(PROVDATA_SIP);
130 provData->psPfns = WINTRUST_Alloc(sizeof(CRYPT_PROVIDER_FUNCTIONS));
131 if (!provData->psPfns)
133 provData->psPfns->cbStruct = sizeof(CRYPT_PROVIDER_FUNCTIONS);
139 WINTRUST_Free(provData->padwTrustStepErrors);
140 WINTRUST_Free(provData->u.pPDSip);
141 WINTRUST_Free(provData->psPfns);
142 WINTRUST_Free(provData);
147 /* Adds trust steps for each function in psPfns. Assumes steps has at least
148 * 5 entries. Returns the number of steps added.
150 static DWORD WINTRUST_AddTrustStepsFromFunctions(struct wintrust_step *steps,
151 const CRYPT_PROVIDER_FUNCTIONS *psPfns)
155 if (psPfns->pfnInitialize)
157 steps[numSteps].func = psPfns->pfnInitialize;
158 steps[numSteps++].error_index = TRUSTERROR_STEP_FINAL_WVTINIT;
160 if (psPfns->pfnObjectTrust)
162 steps[numSteps].func = psPfns->pfnObjectTrust;
163 steps[numSteps++].error_index = TRUSTERROR_STEP_FINAL_OBJPROV;
165 if (psPfns->pfnSignatureTrust)
167 steps[numSteps].func = psPfns->pfnSignatureTrust;
168 steps[numSteps++].error_index = TRUSTERROR_STEP_FINAL_SIGPROV;
170 if (psPfns->pfnCertificateTrust)
172 steps[numSteps].func = psPfns->pfnCertificateTrust;
173 steps[numSteps++].error_index = TRUSTERROR_STEP_FINAL_CERTPROV;
175 if (psPfns->pfnFinalPolicy)
177 steps[numSteps].func = psPfns->pfnFinalPolicy;
178 steps[numSteps++].error_index = TRUSTERROR_STEP_FINAL_POLICYPROV;
183 static LONG WINTRUST_DefaultVerify(HWND hwnd, GUID *actionID,
186 DWORD err = ERROR_SUCCESS, numSteps = 0;
187 CRYPT_PROVIDER_DATA *provData;
189 struct wintrust_step verifySteps[5];
191 TRACE("(%p, %s, %p)\n", hwnd, debugstr_guid(actionID), data);
193 provData = WINTRUST_AllocateProviderData();
195 return ERROR_OUTOFMEMORY;
197 ret = WintrustLoadFunctionPointers(actionID, provData->psPfns);
200 err = GetLastError();
204 data->hWVTStateData = provData;
205 provData->pWintrustData = data;
206 if (hwnd == INVALID_HANDLE_VALUE)
207 provData->hWndParent = GetDesktopWindow();
209 provData->hWndParent = hwnd;
210 provData->pgActionID = actionID;
211 WintrustGetRegPolicyFlags(&provData->dwRegPolicySettings);
213 numSteps = WINTRUST_AddTrustStepsFromFunctions(verifySteps,
215 err = WINTRUST_ExecuteSteps(verifySteps, numSteps, provData);
221 WINTRUST_Free(provData->padwTrustStepErrors);
222 WINTRUST_Free(provData->u.pPDSip);
223 WINTRUST_Free(provData->psPfns);
224 WINTRUST_Free(provData);
227 TRACE("returning %08x\n", err);
231 static LONG WINTRUST_DefaultClose(HWND hwnd, GUID *actionID,
234 DWORD err = ERROR_SUCCESS;
235 CRYPT_PROVIDER_DATA *provData = data->hWVTStateData;
237 TRACE("(%p, %s, %p)\n", hwnd, debugstr_guid(actionID), data);
241 if (provData->psPfns->pfnCleanupPolicy)
242 err = provData->psPfns->pfnCleanupPolicy(provData);
244 WINTRUST_Free(provData->padwTrustStepErrors);
245 WINTRUST_Free(provData->u.pPDSip);
246 WINTRUST_Free(provData->psPfns);
247 WINTRUST_Free(provData);
248 data->hWVTStateData = NULL;
250 TRACE("returning %08x\n", err);
254 static LONG WINTRUST_DefaultVerifyAndClose(HWND hwnd, GUID *actionID,
259 TRACE("(%p, %s, %p)\n", hwnd, debugstr_guid(actionID), data);
261 err = WINTRUST_DefaultVerify(hwnd, actionID, data);
262 WINTRUST_DefaultClose(hwnd, actionID, data);
263 TRACE("returning %08x\n", err);
267 static LONG WINTRUST_PublishedSoftware(HWND hwnd, GUID *actionID,
270 WINTRUST_DATA wintrust_data = { sizeof(wintrust_data), 0 };
271 /* Undocumented: the published software action is passed a path,
272 * and pSIPClientData points to a WIN_TRUST_SUBJECT_FILE.
274 LPWIN_TRUST_SUBJECT_FILE subjectFile = data->pSIPClientData;
275 WINTRUST_FILE_INFO fileInfo = { sizeof(fileInfo), 0 };
277 TRACE("subjectFile->hFile: %p\n", subjectFile->hFile);
278 TRACE("subjectFile->lpPath: %s\n", debugstr_w(subjectFile->lpPath));
279 fileInfo.pcwszFilePath = subjectFile->lpPath;
280 fileInfo.hFile = subjectFile->hFile;
281 wintrust_data.u.pFile = &fileInfo;
282 wintrust_data.dwUnionChoice = WTD_CHOICE_FILE;
283 wintrust_data.dwUIChoice = WTD_UI_NONE;
285 return WINTRUST_DefaultVerifyAndClose(hwnd, actionID, &wintrust_data);
288 /* Sadly, the function to load the cert for the CERT_CERTIFICATE_ACTION_VERIFY
289 * action is not stored in the registry and is located in wintrust, not in
290 * cryptdlg along with the rest of the implementation (verified by running the
291 * action with a native wintrust.dll.)
293 static HRESULT WINAPI WINTRUST_CertVerifyObjTrust(CRYPT_PROVIDER_DATA *data)
297 TRACE("(%p)\n", data);
299 if (!data->padwTrustStepErrors)
302 switch (data->pWintrustData->dwUnionChoice)
304 case WTD_CHOICE_BLOB:
305 if (data->pWintrustData->u.pBlob &&
306 WVT_IS_CBSTRUCT_GT_MEMBEROFFSET(WINTRUST_BLOB_INFO,
307 data->pWintrustData->u.pBlob->cbStruct, pbMemObject) &&
308 data->pWintrustData->u.pBlob->cbMemObject ==
309 sizeof(CERT_VERIFY_CERTIFICATE_TRUST) &&
310 data->pWintrustData->u.pBlob->pbMemObject)
312 CERT_VERIFY_CERTIFICATE_TRUST *pCert =
313 (CERT_VERIFY_CERTIFICATE_TRUST *)
314 data->pWintrustData->u.pBlob->pbMemObject;
316 if (pCert->cbSize == sizeof(CERT_VERIFY_CERTIFICATE_TRUST) &&
319 CRYPT_PROVIDER_SGNR signer = { sizeof(signer), { 0 } };
323 /* Add a signer with nothing but the time to verify, so we can
326 GetSystemTime(&sysTime);
327 SystemTimeToFileTime(&sysTime, &signer.sftVerifyAsOf);
328 ret = data->psPfns->pfnAddSgnr2Chain(data, FALSE, 0, &signer);
331 ret = data->psPfns->pfnAddCert2Chain(data, 0, FALSE, 0,
335 for (i = 0; ret && i < pCert->cRootStores; i++)
336 ret = data->psPfns->pfnAddStore2Chain(data,
337 pCert->rghstoreRoots[i]);
338 for (i = 0; ret && i < pCert->cStores; i++)
339 ret = data->psPfns->pfnAddStore2Chain(data,
340 pCert->rghstoreCAs[i]);
341 for (i = 0; ret && i < pCert->cTrustStores; i++)
342 ret = data->psPfns->pfnAddStore2Chain(data,
343 pCert->rghstoreTrust[i]);
347 SetLastError(ERROR_INVALID_PARAMETER);
353 SetLastError(ERROR_INVALID_PARAMETER);
358 FIXME("unimplemented for %d\n", data->pWintrustData->dwUnionChoice);
359 SetLastError(ERROR_INVALID_PARAMETER);
365 data->padwTrustStepErrors[TRUSTERROR_STEP_FINAL_OBJPROV] =
367 TRACE("returning %d (%08x)\n", ret ? S_OK : S_FALSE,
368 data->padwTrustStepErrors[TRUSTERROR_STEP_FINAL_OBJPROV]);
369 return ret ? S_OK : S_FALSE;
372 static LONG WINTRUST_CertVerify(HWND hwnd, GUID *actionID,
375 DWORD err = ERROR_SUCCESS, numSteps = 0;
376 CRYPT_PROVIDER_DATA *provData;
378 struct wintrust_step verifySteps[5];
380 TRACE("(%p, %s, %p)\n", hwnd, debugstr_guid(actionID), data);
382 provData = WINTRUST_AllocateProviderData();
384 return ERROR_OUTOFMEMORY;
386 ret = WintrustLoadFunctionPointers(actionID, provData->psPfns);
389 err = GetLastError();
392 if (!provData->psPfns->pfnObjectTrust)
393 provData->psPfns->pfnObjectTrust = WINTRUST_CertVerifyObjTrust;
394 /* Not sure why, but native skips the policy check */
395 provData->psPfns->pfnCertCheckPolicy = NULL;
397 data->hWVTStateData = provData;
398 provData->pWintrustData = data;
399 if (hwnd == INVALID_HANDLE_VALUE)
400 provData->hWndParent = GetDesktopWindow();
402 provData->hWndParent = hwnd;
403 provData->pgActionID = actionID;
404 WintrustGetRegPolicyFlags(&provData->dwRegPolicySettings);
406 numSteps = WINTRUST_AddTrustStepsFromFunctions(verifySteps,
408 err = WINTRUST_ExecuteSteps(verifySteps, numSteps, provData);
414 WINTRUST_Free(provData->padwTrustStepErrors);
415 WINTRUST_Free(provData->u.pPDSip);
416 WINTRUST_Free(provData->psPfns);
417 WINTRUST_Free(provData);
420 TRACE("returning %08x\n", err);
424 static LONG WINTRUST_CertVerifyAndClose(HWND hwnd, GUID *actionID,
429 TRACE("(%p, %s, %p)\n", hwnd, debugstr_guid(actionID), data);
431 err = WINTRUST_CertVerify(hwnd, actionID, data);
432 WINTRUST_DefaultClose(hwnd, actionID, data);
433 TRACE("returning %08x\n", err);
437 static LONG WINTRUST_CertActionVerify(HWND hwnd, GUID *actionID,
441 LONG err = ERROR_SUCCESS;
443 if (WVT_ISINSTRUCT(WINTRUST_DATA, data->cbStruct, dwStateAction))
444 stateAction = data->dwStateAction;
447 TRACE("no dwStateAction, assuming WTD_STATEACTION_IGNORE\n");
448 stateAction = WTD_STATEACTION_IGNORE;
452 case WTD_STATEACTION_IGNORE:
453 err = WINTRUST_CertVerifyAndClose(hwnd, actionID, data);
455 case WTD_STATEACTION_VERIFY:
456 err = WINTRUST_CertVerify(hwnd, actionID, data);
458 case WTD_STATEACTION_CLOSE:
459 err = WINTRUST_DefaultClose(hwnd, actionID, data);
462 FIXME("unimplemented for %d\n", data->dwStateAction);
467 static void dump_file_info(WINTRUST_FILE_INFO *pFile)
469 TRACE("%p\n", pFile);
472 TRACE("cbStruct: %d\n", pFile->cbStruct);
473 TRACE("pcwszFilePath: %s\n", debugstr_w(pFile->pcwszFilePath));
474 TRACE("hFile: %p\n", pFile->hFile);
475 TRACE("pgKnownSubject: %s\n", debugstr_guid(pFile->pgKnownSubject));
479 static void dump_catalog_info(WINTRUST_CATALOG_INFO *catalog)
481 TRACE("%p\n", catalog);
484 TRACE("cbStruct: %d\n", catalog->cbStruct);
485 TRACE("dwCatalogVersion: %d\n", catalog->dwCatalogVersion);
486 TRACE("pcwszCatalogFilePath: %s\n",
487 debugstr_w(catalog->pcwszCatalogFilePath));
488 TRACE("pcwszMemberTag: %s\n", debugstr_w(catalog->pcwszMemberTag));
489 TRACE("pcwszMemberFilePath: %s\n",
490 debugstr_w(catalog->pcwszMemberFilePath));
491 TRACE("hMemberFile: %p\n", catalog->hMemberFile);
492 TRACE("pbCalculatedFileHash: %p\n", catalog->pbCalculatedFileHash);
493 TRACE("cbCalculatedFileHash: %d\n", catalog->cbCalculatedFileHash);
494 TRACE("pcCatalogContext: %p\n", catalog->pcCatalogContext);
498 static void dump_blob_info(WINTRUST_BLOB_INFO *blob)
503 TRACE("cbStruct: %d\n", blob->cbStruct);
504 TRACE("gSubject: %s\n", debugstr_guid(&blob->gSubject));
505 TRACE("pcwszDisplayName: %s\n", debugstr_w(blob->pcwszDisplayName));
506 TRACE("cbMemObject: %d\n", blob->cbMemObject);
507 TRACE("pbMemObject: %p\n", blob->pbMemObject);
508 TRACE("cbMemSignedMsg: %d\n", blob->cbMemSignedMsg);
509 TRACE("pbMemSignedMsg: %p\n", blob->pbMemSignedMsg);
513 static void dump_sgnr_info(WINTRUST_SGNR_INFO *sgnr)
518 TRACE("cbStruct: %d\n", sgnr->cbStruct);
519 TRACE("pcwszDisplayName: %s\n", debugstr_w(sgnr->pcwszDisplayName));
520 TRACE("psSignerInfo: %p\n", sgnr->psSignerInfo);
521 TRACE("chStores: %d\n", sgnr->chStores);
525 static void dump_cert_info(WINTRUST_CERT_INFO *cert)
530 TRACE("cbStruct: %d\n", cert->cbStruct);
531 TRACE("pcwszDisplayName: %s\n", debugstr_w(cert->pcwszDisplayName));
532 TRACE("psCertContext: %p\n", cert->psCertContext);
533 TRACE("chStores: %d\n", cert->chStores);
534 TRACE("dwFlags: %08x\n", cert->dwFlags);
535 TRACE("psftVerifyAsOf: %p\n", cert->psftVerifyAsOf);
539 static void dump_wintrust_data(WINTRUST_DATA *data)
544 TRACE("cbStruct: %d\n", data->cbStruct);
545 TRACE("pPolicyCallbackData: %p\n", data->pPolicyCallbackData);
546 TRACE("pSIPClientData: %p\n", data->pSIPClientData);
547 TRACE("dwUIChoice: %d\n", data->dwUIChoice);
548 TRACE("fdwRevocationChecks: %08x\n", data->fdwRevocationChecks);
549 TRACE("dwUnionChoice: %d\n", data->dwUnionChoice);
550 switch (data->dwUnionChoice)
552 case WTD_CHOICE_FILE:
553 dump_file_info(data->u.pFile);
555 case WTD_CHOICE_CATALOG:
556 dump_catalog_info(data->u.pCatalog);
558 case WTD_CHOICE_BLOB:
559 dump_blob_info(data->u.pBlob);
561 case WTD_CHOICE_SIGNER:
562 dump_sgnr_info(data->u.pSgnr);
564 case WTD_CHOICE_CERT:
565 dump_cert_info(data->u.pCert);
568 TRACE("dwStateAction: %d\n", data->dwStateAction);
569 TRACE("hWVTStateData: %p\n", data->hWVTStateData);
570 TRACE("pwszURLReference: %s\n", debugstr_w(data->pwszURLReference));
571 TRACE("dwProvFlags: %08x\n", data->dwProvFlags);
572 TRACE("dwUIContext: %d\n", data->dwUIContext);
576 /***********************************************************************
577 * WinVerifyTrust (WINTRUST.@)
579 * Verifies an object by calling the specified trust provider.
582 * hwnd [I] Handle to a caller window.
583 * ActionID [I] Pointer to a GUID that identifies the action to perform.
584 * ActionData [I] Information used by the trust provider to verify the object.
588 * Failure: A TRUST_E_* error code.
591 * Trust providers can be found at:
592 * HKLM\SOFTWARE\Microsoft\Cryptography\Providers\Trust\
594 LONG WINAPI WinVerifyTrust( HWND hwnd, GUID *ActionID, LPVOID ActionData )
596 static const GUID unknown = { 0xC689AAB8, 0x8E78, 0x11D0, { 0x8C,0x47,
597 0x00,0xC0,0x4F,0xC2,0x95,0xEE } };
598 static const GUID published_software = WIN_SPUB_ACTION_PUBLISHED_SOFTWARE;
599 static const GUID generic_verify_v2 = WINTRUST_ACTION_GENERIC_VERIFY_V2;
600 static const GUID generic_cert_verify = WINTRUST_ACTION_GENERIC_CERT_VERIFY;
601 static const GUID generic_chain_verify = WINTRUST_ACTION_GENERIC_CHAIN_VERIFY;
602 static const GUID cert_action_verify = CERT_CERTIFICATE_ACTION_VERIFY;
603 LONG err = ERROR_SUCCESS;
604 WINTRUST_DATA *actionData = ActionData;
606 TRACE("(%p, %s, %p)\n", hwnd, debugstr_guid(ActionID), ActionData);
607 dump_wintrust_data(ActionData);
609 /* Support for known old-style callers: */
610 if (IsEqualGUID(ActionID, &published_software))
611 err = WINTRUST_PublishedSoftware(hwnd, ActionID, ActionData);
612 else if (IsEqualGUID(ActionID, &cert_action_verify))
613 err = WINTRUST_CertActionVerify(hwnd, ActionID, ActionData);
618 /* Check known actions to warn of possible problems */
619 if (!IsEqualGUID(ActionID, &unknown) &&
620 !IsEqualGUID(ActionID, &generic_verify_v2) &&
621 !IsEqualGUID(ActionID, &generic_cert_verify) &&
622 !IsEqualGUID(ActionID, &generic_chain_verify))
623 WARN("unknown action %s, default behavior may not be right\n",
624 debugstr_guid(ActionID));
625 if (WVT_ISINSTRUCT(WINTRUST_DATA, actionData->cbStruct, dwStateAction))
626 stateAction = actionData->dwStateAction;
629 TRACE("no dwStateAction, assuming WTD_STATEACTION_IGNORE\n");
630 stateAction = WTD_STATEACTION_IGNORE;
634 case WTD_STATEACTION_IGNORE:
635 err = WINTRUST_DefaultVerifyAndClose(hwnd, ActionID, ActionData);
637 case WTD_STATEACTION_VERIFY:
638 err = WINTRUST_DefaultVerify(hwnd, ActionID, ActionData);
640 case WTD_STATEACTION_CLOSE:
641 err = WINTRUST_DefaultClose(hwnd, ActionID, ActionData);
644 FIXME("unimplemented for %d\n", actionData->dwStateAction);
648 TRACE("returning %08x\n", err);
652 /***********************************************************************
653 * WinVerifyTrustEx (WINTRUST.@)
655 HRESULT WINAPI WinVerifyTrustEx( HWND hwnd, GUID *ActionID,
656 WINTRUST_DATA* ActionData )
658 return WinVerifyTrust(hwnd, ActionID, ActionData);
661 /***********************************************************************
662 * WTHelperGetProvSignerFromChain (WINTRUST.@)
664 CRYPT_PROVIDER_SGNR * WINAPI WTHelperGetProvSignerFromChain(
665 CRYPT_PROVIDER_DATA *pProvData, DWORD idxSigner, BOOL fCounterSigner,
666 DWORD idxCounterSigner)
668 CRYPT_PROVIDER_SGNR *sgnr;
670 TRACE("(%p %d %d %d)\n", pProvData, idxSigner, fCounterSigner,
673 if (idxSigner >= pProvData->csSigners || !pProvData->pasSigners)
675 sgnr = &pProvData->pasSigners[idxSigner];
678 if (idxCounterSigner >= sgnr->csCounterSigners ||
679 !sgnr->pasCounterSigners)
681 sgnr = &sgnr->pasCounterSigners[idxCounterSigner];
683 TRACE("returning %p\n", sgnr);
687 /***********************************************************************
688 * WTHelperGetProvCertFromChain (WINTRUST.@)
690 CRYPT_PROVIDER_CERT * WINAPI WTHelperGetProvCertFromChain(
691 CRYPT_PROVIDER_SGNR *pSgnr, DWORD idxCert)
693 CRYPT_PROVIDER_CERT *cert;
695 TRACE("(%p %d)\n", pSgnr, idxCert);
697 if (idxCert >= pSgnr->csCertChain || !pSgnr->pasCertChain)
699 cert = &pSgnr->pasCertChain[idxCert];
700 TRACE("returning %p\n", cert);
704 CRYPT_PROVIDER_PRIVDATA *WINAPI WTHelperGetProvPrivateDataFromChain(
705 CRYPT_PROVIDER_DATA* pProvData,
708 CRYPT_PROVIDER_PRIVDATA *privdata = NULL;
711 TRACE("(%p, %s)\n", pProvData, debugstr_guid(pgProviderID));
713 for (i = 0; i < pProvData->csProvPrivData; i++)
714 if (IsEqualGUID(pgProviderID, &pProvData->pasProvPrivData[i].gProviderID))
716 privdata = &pProvData->pasProvPrivData[i];
723 /***********************************************************************
724 * WTHelperProvDataFromStateData (WINTRUST.@)
726 CRYPT_PROVIDER_DATA * WINAPI WTHelperProvDataFromStateData(HANDLE hStateData)
728 TRACE("%p\n", hStateData);
732 /***********************************************************************
733 * WTHelperGetFileName(WINTRUST.@)
735 LPCWSTR WINAPI WTHelperGetFileName(WINTRUST_DATA *data)
738 if (data->dwUnionChoice == WTD_CHOICE_FILE)
739 return data->u.pFile->pcwszFilePath;
744 /***********************************************************************
745 * WTHelperGetFileHandle(WINTRUST.@)
747 HANDLE WINAPI WTHelperGetFileHandle(WINTRUST_DATA *data)
750 if (data->dwUnionChoice == WTD_CHOICE_FILE)
751 return data->u.pFile->hFile;
753 return INVALID_HANDLE_VALUE;
756 static BOOL WINAPI WINTRUST_enumUsages(PCCRYPT_OID_INFO pInfo, void *pvArg)
758 PCCRYPT_OID_INFO **usages = pvArg;
765 *usages = WINTRUST_Alloc(2 * sizeof(PCCRYPT_OID_INFO));
769 PCCRYPT_OID_INFO *ptr;
771 /* Count the existing usages.
772 * FIXME: make sure the new usage doesn't duplicate any in the list?
774 for (cUsages = 0, ptr = *usages; *ptr; ptr++, cUsages++)
776 *usages = WINTRUST_ReAlloc(*usages,
777 (cUsages + 2) * sizeof(PCCRYPT_OID_INFO));
781 (*usages)[cUsages] = pInfo;
782 (*usages)[cUsages + 1] = NULL;
787 SetLastError(ERROR_OUTOFMEMORY);
793 /***********************************************************************
794 * WTHelperGetKnownUsages(WINTRUST.@)
796 * Enumerates the known enhanced key usages as an array of PCCRYPT_OID_INFOs.
799 * action [In] 1 => allocate and return known usages, 2 => free previously
801 * usages [In/Out] If action == 1, *usages is set to an array of
802 * PCCRYPT_OID_INFO *. The array is terminated with a NULL
804 * If action == 2, *usages is freed.
807 * TRUE on success, FALSE on failure.
809 BOOL WINAPI WTHelperGetKnownUsages(DWORD action, PCCRYPT_OID_INFO **usages)
813 TRACE("(%d, %p)\n", action, usages);
817 SetLastError(ERROR_INVALID_PARAMETER);
824 ret = CryptEnumOIDInfo(CRYPT_ENHKEY_USAGE_OID_GROUP_ID, 0, usages,
825 WINTRUST_enumUsages);
827 else if (action == 2)
829 WINTRUST_Free(*usages);
835 WARN("unknown action %d\n", action);
836 SetLastError(ERROR_INVALID_PARAMETER);
842 static const WCHAR Software_Publishing[] = {
843 'S','o','f','t','w','a','r','e','\\',
844 'M','i','c','r','o','s','o','f','t','\\',
845 'W','i','n','d','o','w','s','\\',
846 'C','u','r','r','e','n','t','V','e','r','s','i','o','n','\\',
847 'W','i','n','t','r','u','s','t','\\',
848 'T','r','u','s','t',' ','P','r','o','v','i','d','e','r','s','\\',
849 'S','o','f','t','w','a','r','e',' ',
850 'P','u','b','l','i','s','h','i','n','g',0 };
851 static const WCHAR State[] = { 'S','t','a','t','e',0 };
853 /***********************************************************************
854 * WintrustGetRegPolicyFlags (WINTRUST.@)
856 void WINAPI WintrustGetRegPolicyFlags( DWORD* pdwPolicyFlags )
861 TRACE("%p\n", pdwPolicyFlags);
864 r = RegCreateKeyExW(HKEY_CURRENT_USER, Software_Publishing, 0, NULL, 0,
865 KEY_READ, NULL, &key, NULL);
868 DWORD size = sizeof(DWORD);
870 r = RegQueryValueExW(key, State, NULL, NULL, (LPBYTE)pdwPolicyFlags,
875 /* Failed to query, create and return default value */
876 *pdwPolicyFlags = WTPF_IGNOREREVOCATIONONTS |
877 WTPF_OFFLINEOKNBU_COM |
878 WTPF_OFFLINEOKNBU_IND |
881 WintrustSetRegPolicyFlags(*pdwPolicyFlags);
886 /***********************************************************************
887 * WintrustSetRegPolicyFlags (WINTRUST.@)
889 BOOL WINAPI WintrustSetRegPolicyFlags( DWORD dwPolicyFlags)
894 TRACE("%x\n", dwPolicyFlags);
896 r = RegCreateKeyExW(HKEY_CURRENT_USER, Software_Publishing, 0,
897 NULL, 0, KEY_WRITE, NULL, &key, NULL);
900 r = RegSetValueExW(key, State, 0, REG_DWORD, (LPBYTE)&dwPolicyFlags,
904 if (r) SetLastError(r);
905 return r == ERROR_SUCCESS;
908 /* Utility functions */
910 BOOL WINAPI WINTRUST_AddStore(CRYPT_PROVIDER_DATA *data, HCERTSTORE store)
914 TRACE("(%p, %p)\n", data, store);
917 data->pahStores = WINTRUST_ReAlloc(data->pahStores,
918 (data->chStores + 1) * sizeof(HCERTSTORE));
921 data->pahStores = WINTRUST_Alloc(sizeof(HCERTSTORE));
926 data->pahStores[data->chStores++] = CertDuplicateStore(store);
930 SetLastError(ERROR_OUTOFMEMORY);
934 BOOL WINAPI WINTRUST_AddSgnr(CRYPT_PROVIDER_DATA *data,
935 BOOL fCounterSigner, DWORD idxSigner, CRYPT_PROVIDER_SGNR *sgnr)
939 TRACE("(%p, %d, %d, %p)\n", data, fCounterSigner, idxSigner, sgnr);
941 if (sgnr->cbStruct > sizeof(CRYPT_PROVIDER_SGNR))
943 SetLastError(ERROR_INVALID_PARAMETER);
948 FIXME("unimplemented for counter signers\n");
949 SetLastError(ERROR_INVALID_PARAMETER);
953 data->pasSigners = WINTRUST_ReAlloc(data->pasSigners,
954 (data->csSigners + 1) * sizeof(CRYPT_PROVIDER_SGNR));
957 data->pasSigners = WINTRUST_Alloc(sizeof(CRYPT_PROVIDER_SGNR));
960 if (data->pasSigners)
962 if (idxSigner < data->csSigners)
963 memmove(&data->pasSigners[idxSigner],
964 &data->pasSigners[idxSigner + 1],
965 (data->csSigners - idxSigner) * sizeof(CRYPT_PROVIDER_SGNR));
967 if (sgnr->cbStruct == sizeof(CRYPT_PROVIDER_SGNR))
969 /* The PSDK says psSigner should be allocated using pfnAlloc, but
970 * it doesn't say anything about ownership. Since callers are
971 * internal, assume ownership is passed, and just store the
974 memcpy(&data->pasSigners[idxSigner], sgnr,
975 sizeof(CRYPT_PROVIDER_SGNR));
978 memset(&data->pasSigners[idxSigner], 0,
979 sizeof(CRYPT_PROVIDER_SGNR));
983 SetLastError(ERROR_OUTOFMEMORY);
987 BOOL WINAPI WINTRUST_AddCert(CRYPT_PROVIDER_DATA *data, DWORD idxSigner,
988 BOOL fCounterSigner, DWORD idxCounterSigner, PCCERT_CONTEXT pCert2Add)
992 TRACE("(%p, %d, %d, %d, %p)\n", data, idxSigner, fCounterSigner,
993 idxSigner, pCert2Add);
997 FIXME("unimplemented for counter signers\n");
998 SetLastError(ERROR_INVALID_PARAMETER);
1001 if (data->pasSigners[idxSigner].csCertChain)
1002 data->pasSigners[idxSigner].pasCertChain =
1003 WINTRUST_ReAlloc(data->pasSigners[idxSigner].pasCertChain,
1004 (data->pasSigners[idxSigner].csCertChain + 1) *
1005 sizeof(CRYPT_PROVIDER_CERT));
1008 data->pasSigners[idxSigner].pasCertChain =
1009 WINTRUST_Alloc(sizeof(CRYPT_PROVIDER_CERT));
1010 data->pasSigners[idxSigner].csCertChain = 0;
1012 if (data->pasSigners[idxSigner].pasCertChain)
1014 CRYPT_PROVIDER_CERT *cert = &data->pasSigners[idxSigner].pasCertChain[
1015 data->pasSigners[idxSigner].csCertChain];
1017 cert->cbStruct = sizeof(CRYPT_PROVIDER_CERT);
1018 cert->pCert = CertDuplicateCertificateContext(pCert2Add);
1019 data->pasSigners[idxSigner].csCertChain++;
1023 SetLastError(ERROR_OUTOFMEMORY);
1027 BOOL WINAPI WINTRUST_AddPrivData(CRYPT_PROVIDER_DATA *data,
1028 CRYPT_PROVIDER_PRIVDATA *pPrivData2Add)
1032 TRACE("(%p, %p)\n", data, pPrivData2Add);
1034 if (pPrivData2Add->cbStruct > sizeof(CRYPT_PROVIDER_PRIVDATA))
1036 SetLastError(ERROR_INVALID_PARAMETER);
1037 WARN("invalid struct size\n");
1040 if (data->csProvPrivData)
1041 data->pasProvPrivData = WINTRUST_ReAlloc(data->pasProvPrivData,
1042 (data->csProvPrivData + 1) * sizeof(CRYPT_PROVIDER_SGNR));
1045 data->pasProvPrivData = WINTRUST_Alloc(sizeof(CRYPT_PROVIDER_SGNR));
1046 data->csProvPrivData = 0;
1048 if (data->pasProvPrivData)
1052 for (i = 0; i < data->csProvPrivData; i++)
1053 if (IsEqualGUID(&pPrivData2Add->gProviderID, &data->pasProvPrivData[i]))
1056 data->pasProvPrivData[i] = *pPrivData2Add;
1057 if (i == data->csProvPrivData)
1058 data->csProvPrivData++;
1061 SetLastError(ERROR_OUTOFMEMORY);
1065 /***********************************************************************
1066 * OpenPersonalTrustDBDialog (WINTRUST.@)
1068 * Opens the certificate manager dialog, showing only the stores that
1069 * contain trusted software publishers.
1072 * hwnd [I] handle of parent window
1075 * TRUE if the dialog could be opened, FALSE if not.
1077 BOOL WINAPI OpenPersonalTrustDBDialog(HWND hwnd)
1079 CRYPTUI_CERT_MGR_STRUCT uiCertMgr;
1081 uiCertMgr.dwSize = sizeof(uiCertMgr);
1082 uiCertMgr.hwndParent = hwnd;
1083 uiCertMgr.dwFlags = CRYPTUI_CERT_MGR_PUBLISHER_TAB;
1084 uiCertMgr.pwszTitle = NULL;
1085 uiCertMgr.pszInitUsageOID = NULL;
1086 return CryptUIDlgCertMgr(&uiCertMgr);
1089 /***********************************************************************
1090 * WTHelperCertCheckValidSignature
1092 HRESULT WINAPI WTHelperCertCheckValidSignature(CRYPT_PROVIDER_DATA *pProvData)