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
36 #include "wintrust_priv.h"
37 #include "wine/debug.h"
39 WINE_DEFAULT_DEBUG_CHANNEL(wintrust);
42 /***********************************************************************
43 * DllMain (WINTRUST.@)
45 BOOL WINAPI DllMain( HINSTANCE inst, DWORD reason, LPVOID reserved )
49 case DLL_PROCESS_ATTACH:
50 DisableThreadLibraryCalls( inst );
56 /***********************************************************************
57 * TrustIsCertificateSelfSigned (WINTRUST.@)
59 BOOL WINAPI TrustIsCertificateSelfSigned( PCCERT_CONTEXT cert )
64 ret = CertCompareCertificateName(cert->dwCertEncodingType,
65 &cert->pCertInfo->Subject, &cert->pCertInfo->Issuer);
69 static LONG WINTRUST_DefaultVerify(HWND hwnd, GUID *actionID,
72 DWORD err = ERROR_SUCCESS;
73 CRYPT_PROVIDER_DATA *provData;
76 TRACE("(%p, %s, %p)\n", hwnd, debugstr_guid(actionID), data);
78 provData = WINTRUST_Alloc(sizeof(CRYPT_PROVIDER_DATA));
81 provData->cbStruct = sizeof(CRYPT_PROVIDER_DATA);
83 provData->padwTrustStepErrors =
84 WINTRUST_Alloc(TRUSTERROR_MAX_STEPS * sizeof(DWORD));
85 if (!provData->padwTrustStepErrors)
87 provData->cdwTrustStepErrors = TRUSTERROR_MAX_STEPS;
89 provData->u.pPDSip = WINTRUST_Alloc(sizeof(PROVDATA_SIP));
90 if (!provData->u.pPDSip)
92 provData->u.pPDSip->cbStruct = sizeof(PROVDATA_SIP);
94 provData->psPfns = WINTRUST_Alloc(sizeof(CRYPT_PROVIDER_FUNCTIONS));
95 if (!provData->psPfns)
97 provData->psPfns->cbStruct = sizeof(CRYPT_PROVIDER_FUNCTIONS);
98 ret = WintrustLoadFunctionPointers(actionID, provData->psPfns);
101 err = GetLastError();
105 data->hWVTStateData = (HANDLE)provData;
106 provData->pWintrustData = data;
107 if (hwnd == INVALID_HANDLE_VALUE)
108 provData->hWndParent = GetDesktopWindow();
110 provData->hWndParent = hwnd;
111 provData->pgActionID = actionID;
112 WintrustGetRegPolicyFlags(&provData->dwRegPolicySettings);
114 err = provData->psPfns->pfnInitialize(provData);
117 err = provData->psPfns->pfnObjectTrust(provData);
120 err = provData->psPfns->pfnSignatureTrust(provData);
123 err = provData->psPfns->pfnCertificateTrust(provData);
126 err = provData->psPfns->pfnFinalPolicy(provData);
130 err = ERROR_OUTOFMEMORY;
134 WINTRUST_Free(provData->padwTrustStepErrors);
135 WINTRUST_Free(provData->u.pPDSip);
136 WINTRUST_Free(provData->psPfns);
137 WINTRUST_Free(provData);
140 TRACE("returning %08x\n", err);
144 static LONG WINTRUST_DefaultClose(HWND hwnd, GUID *actionID,
147 DWORD err = ERROR_SUCCESS;
148 CRYPT_PROVIDER_DATA *provData = (CRYPT_PROVIDER_DATA *)data->hWVTStateData;
150 TRACE("(%p, %s, %p)\n", hwnd, debugstr_guid(actionID), data);
154 err = provData->psPfns->pfnCleanupPolicy(provData);
155 WINTRUST_Free(provData->padwTrustStepErrors);
156 WINTRUST_Free(provData->u.pPDSip);
157 WINTRUST_Free(provData->psPfns);
158 WINTRUST_Free(provData);
159 data->hWVTStateData = NULL;
161 TRACE("returning %08x\n", err);
165 static LONG WINTRUST_DefaultVerifyAndClose(HWND hwnd, GUID *actionID,
170 TRACE("(%p, %s, %p)\n", hwnd, debugstr_guid(actionID), data);
172 err = WINTRUST_DefaultVerify(hwnd, actionID, data);
173 WINTRUST_DefaultClose(hwnd, actionID, data);
174 TRACE("returning %08x\n", err);
178 static LONG WINTRUST_PublishedSoftware(HWND hwnd, GUID *actionID,
181 WINTRUST_DATA wintrust_data = { sizeof(wintrust_data), 0 };
182 /* Undocumented: the published software action is passed a path,
183 * and pSIPClientData points to a WIN_TRUST_SUBJECT_FILE.
185 LPCWSTR path = (LPCWSTR)data->u.pFile;
186 LPWIN_TRUST_SUBJECT_FILE subjectFile =
187 (LPWIN_TRUST_SUBJECT_FILE)data->pSIPClientData;
188 WINTRUST_FILE_INFO fileInfo = { sizeof(fileInfo), 0 };
190 TRACE("subjectFile->hFile: %p\n", subjectFile->hFile);
191 TRACE("subjectFile->lpPath: %s\n", debugstr_w(subjectFile->lpPath));
192 fileInfo.pcwszFilePath = path;
193 fileInfo.hFile = subjectFile->hFile;
194 wintrust_data.u.pFile = &fileInfo;
195 wintrust_data.dwUnionChoice = WTD_CHOICE_FILE;
196 wintrust_data.dwUIChoice = WTD_UI_NONE;
198 return WINTRUST_DefaultVerifyAndClose(hwnd, actionID, &wintrust_data);
201 static void dump_file_info(WINTRUST_FILE_INFO *pFile)
203 TRACE("%p\n", pFile);
206 TRACE("cbStruct: %d\n", pFile->cbStruct);
207 TRACE("pcwszFilePath: %s\n", debugstr_w(pFile->pcwszFilePath));
208 TRACE("hFile: %p\n", pFile->hFile);
209 TRACE("pgKnownSubject: %s\n", debugstr_guid(pFile->pgKnownSubject));
213 static void dump_catalog_info(WINTRUST_CATALOG_INFO *catalog)
215 TRACE("%p\n", catalog);
218 TRACE("cbStruct: %d\n", catalog->cbStruct);
219 TRACE("dwCatalogVersion: %d\n", catalog->dwCatalogVersion);
220 TRACE("pcwszCatalogFilePath: %s\n",
221 debugstr_w(catalog->pcwszCatalogFilePath));
222 TRACE("pcwszMemberTag: %s\n", debugstr_w(catalog->pcwszMemberTag));
223 TRACE("pcwszMemberFilePath: %s\n",
224 debugstr_w(catalog->pcwszMemberFilePath));
225 TRACE("hMemberFile: %p\n", catalog->hMemberFile);
226 TRACE("pbCalculatedFileHash: %p\n", catalog->pbCalculatedFileHash);
227 TRACE("cbCalculatedFileHash: %d\n", catalog->cbCalculatedFileHash);
228 TRACE("pcCatalogContext: %p\n", catalog->pcCatalogContext);
232 static void dump_blob_info(WINTRUST_BLOB_INFO *blob)
237 TRACE("cbStruct: %d\n", blob->cbStruct);
238 TRACE("gSubject: %s\n", debugstr_guid(&blob->gSubject));
239 TRACE("pcwszDisplayName: %s\n", debugstr_w(blob->pcwszDisplayName));
240 TRACE("cbMemObject: %d\n", blob->cbMemObject);
241 TRACE("pbMemObject: %p\n", blob->pbMemObject);
242 TRACE("cbMemSignedMsg: %d\n", blob->cbMemSignedMsg);
243 TRACE("pbMemSignedMsg: %p\n", blob->pbMemSignedMsg);
247 static void dump_sgnr_info(WINTRUST_SGNR_INFO *sgnr)
252 TRACE("cbStruct: %d\n", sgnr->cbStruct);
253 TRACE("pcwszDisplayName: %s\n", debugstr_w(sgnr->pcwszDisplayName));
254 TRACE("psSignerInfo: %p\n", sgnr->psSignerInfo);
255 TRACE("chStores: %d\n", sgnr->chStores);
259 static void dump_cert_info(WINTRUST_CERT_INFO *cert)
264 TRACE("cbStruct: %d\n", cert->cbStruct);
265 TRACE("pcwszDisplayName: %s\n", debugstr_w(cert->pcwszDisplayName));
266 TRACE("psCertContext: %p\n", cert->psCertContext);
267 TRACE("chStores: %d\n", cert->chStores);
268 TRACE("dwFlags: %08x\n", cert->dwFlags);
269 TRACE("psftVerifyAsOf: %p\n", cert->psftVerifyAsOf);
273 static void dump_wintrust_data(WINTRUST_DATA *data)
278 TRACE("cbStruct: %d\n", data->cbStruct);
279 TRACE("pPolicyCallbackData: %p\n", data->pPolicyCallbackData);
280 TRACE("pSIPClientData: %p\n", data->pSIPClientData);
281 TRACE("dwUIChoice: %d\n", data->dwUIChoice);
282 TRACE("fdwRevocationChecks: %08x\n", data->fdwRevocationChecks);
283 TRACE("dwUnionChoice: %d\n", data->dwUnionChoice);
284 switch (data->dwUnionChoice)
286 case WTD_CHOICE_FILE:
287 dump_file_info(data->u.pFile);
289 case WTD_CHOICE_CATALOG:
290 dump_catalog_info(data->u.pCatalog);
292 case WTD_CHOICE_BLOB:
293 dump_blob_info(data->u.pBlob);
295 case WTD_CHOICE_SIGNER:
296 dump_sgnr_info(data->u.pSgnr);
298 case WTD_CHOICE_CERT:
299 dump_cert_info(data->u.pCert);
302 TRACE("dwStateAction: %d\n", data->dwStateAction);
303 TRACE("hWVTStateData: %p\n", data->hWVTStateData);
304 TRACE("pwszURLReference: %s\n", debugstr_w(data->pwszURLReference));
305 TRACE("dwProvFlags: %08x\n", data->dwProvFlags);
306 TRACE("dwUIContext: %d\n", data->dwUIContext);
310 /***********************************************************************
311 * WinVerifyTrust (WINTRUST.@)
313 * Verifies an object by calling the specified trust provider.
316 * hwnd [I] Handle to a caller window.
317 * ActionID [I] Pointer to a GUID that identifies the action to perform.
318 * ActionData [I] Information used by the trust provider to verify the object.
322 * Failure: A TRUST_E_* error code.
325 * Trust providers can be found at:
326 * HKLM\SOFTWARE\Microsoft\Cryptography\Providers\Trust\
328 LONG WINAPI WinVerifyTrust( HWND hwnd, GUID *ActionID, LPVOID ActionData )
330 static const GUID unknown = { 0xC689AAB8, 0x8E78, 0x11D0, { 0x8C,0x47,
331 0x00,0xC0,0x4F,0xC2,0x95,0xEE } };
332 static const GUID published_software = WIN_SPUB_ACTION_PUBLISHED_SOFTWARE;
333 static const GUID generic_verify_v2 = WINTRUST_ACTION_GENERIC_VERIFY_V2;
334 static const GUID generic_cert_verify = WINTRUST_ACTION_GENERIC_CERT_VERIFY;
335 LONG err = ERROR_SUCCESS;
336 WINTRUST_DATA *actionData = (WINTRUST_DATA *)ActionData;
338 TRACE("(%p, %s, %p)\n", hwnd, debugstr_guid(ActionID), ActionData);
339 dump_wintrust_data(ActionData);
341 /* Support for known old-style callers: */
342 if (IsEqualGUID(ActionID, &published_software))
343 err = WINTRUST_PublishedSoftware(hwnd, ActionID, ActionData);
346 /* Check known actions to warn of possible problems */
347 if (!IsEqualGUID(ActionID, &unknown) &&
348 !IsEqualGUID(ActionID, &generic_verify_v2) &&
349 !IsEqualGUID(ActionID, &generic_cert_verify))
350 WARN("unknown action %s, default behavior may not be right\n",
351 debugstr_guid(ActionID));
352 switch (actionData->dwStateAction)
354 case WTD_STATEACTION_IGNORE:
355 err = WINTRUST_DefaultVerifyAndClose(hwnd, ActionID, ActionData);
357 case WTD_STATEACTION_VERIFY:
358 err = WINTRUST_DefaultVerify(hwnd, ActionID, ActionData);
360 case WTD_STATEACTION_CLOSE:
361 err = WINTRUST_DefaultClose(hwnd, ActionID, ActionData);
364 FIXME("unimplemented for %d\n", actionData->dwStateAction);
368 TRACE("returning %08x\n", err);
372 /***********************************************************************
373 * WinVerifyTrustEx (WINTRUST.@)
375 HRESULT WINAPI WinVerifyTrustEx( HWND hwnd, GUID *ActionID,
376 WINTRUST_DATA* ActionData )
378 return WinVerifyTrust(hwnd, ActionID, ActionData);
381 /***********************************************************************
382 * WTHelperGetProvSignerFromChain (WINTRUST.@)
384 CRYPT_PROVIDER_SGNR * WINAPI WTHelperGetProvSignerFromChain(
385 CRYPT_PROVIDER_DATA *pProvData, DWORD idxSigner, BOOL fCounterSigner,
386 DWORD idxCounterSigner)
388 CRYPT_PROVIDER_SGNR *sgnr;
390 TRACE("(%p %d %d %d)\n", pProvData, idxSigner, fCounterSigner,
393 if (idxSigner >= pProvData->csSigners || !pProvData->pasSigners)
395 sgnr = &pProvData->pasSigners[idxSigner];
398 if (idxCounterSigner >= sgnr->csCounterSigners ||
399 !sgnr->pasCounterSigners)
401 sgnr = &sgnr->pasCounterSigners[idxCounterSigner];
403 TRACE("returning %p\n", sgnr);
407 /***********************************************************************
408 * WTHelperGetProvCertFromChain (WINTRUST.@)
410 CRYPT_PROVIDER_CERT * WINAPI WTHelperGetProvCertFromChain(
411 CRYPT_PROVIDER_SGNR *pSgnr, DWORD idxCert)
413 CRYPT_PROVIDER_CERT *cert;
415 TRACE("(%p %d)\n", pSgnr, idxCert);
417 if (idxCert >= pSgnr->csCertChain || !pSgnr->pasCertChain)
419 cert = &pSgnr->pasCertChain[idxCert];
420 TRACE("returning %p\n", cert);
424 /***********************************************************************
425 * WTHelperProvDataFromStateData (WINTRUST.@)
427 CRYPT_PROVIDER_DATA * WINAPI WTHelperProvDataFromStateData(HANDLE hStateData)
429 TRACE("%p\n", hStateData);
430 return (CRYPT_PROVIDER_DATA *)hStateData;
433 static const WCHAR Software_Publishing[] = {
434 'S','o','f','t','w','a','r','e','\\',
435 'M','i','c','r','o','s','o','f','t','\\',
436 'W','i','n','d','o','w','s','\\',
437 'C','u','r','r','e','n','t','V','e','r','s','i','o','n','\\',
438 'W','i','n','t','r','u','s','t','\\',
439 'T','r','u','s','t',' ','P','r','o','v','i','d','e','r','s','\\',
440 'S','o','f','t','w','a','r','e',' ',
441 'P','u','b','l','i','s','h','i','n','g',0 };
442 static const WCHAR State[] = { 'S','t','a','t','e',0 };
444 /***********************************************************************
445 * WintrustGetRegPolicyFlags (WINTRUST.@)
447 void WINAPI WintrustGetRegPolicyFlags( DWORD* pdwPolicyFlags )
452 TRACE("%p\n", pdwPolicyFlags);
455 r = RegCreateKeyExW(HKEY_CURRENT_USER, Software_Publishing, 0, NULL, 0,
456 KEY_READ, NULL, &key, NULL);
459 DWORD size = sizeof(DWORD);
461 r = RegQueryValueExW(key, State, NULL, NULL, (LPBYTE)pdwPolicyFlags,
466 /* Failed to query, create and return default value */
467 *pdwPolicyFlags = WTPF_IGNOREREVOCATIONONTS |
468 WTPF_OFFLINEOKNBU_COM |
469 WTPF_OFFLINEOKNBU_IND |
472 WintrustSetRegPolicyFlags(*pdwPolicyFlags);
477 /***********************************************************************
478 * WintrustSetRegPolicyFlags (WINTRUST.@)
480 BOOL WINAPI WintrustSetRegPolicyFlags( DWORD dwPolicyFlags)
485 TRACE("%x\n", dwPolicyFlags);
487 r = RegCreateKeyExW(HKEY_CURRENT_USER, Software_Publishing, 0,
488 NULL, 0, KEY_WRITE, NULL, &key, NULL);
491 r = RegSetValueExW(key, State, 0, REG_DWORD, (LPBYTE)&dwPolicyFlags,
495 if (r) SetLastError(r);
496 return r == ERROR_SUCCESS;
499 /* Utility functions */
500 void * WINAPI WINTRUST_Alloc(DWORD cb)
502 return HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, cb);
505 void * WINAPI WINTRUST_ReAlloc(void *ptr, DWORD cb)
507 return HeapReAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, ptr, cb);
510 void WINAPI WINTRUST_Free(void *p)
512 HeapFree(GetProcessHeap(), 0, p);
515 BOOL WINAPI WINTRUST_AddStore(CRYPT_PROVIDER_DATA *data, HCERTSTORE store)
520 data->pahStores = WINTRUST_ReAlloc(data->pahStores,
521 (data->chStores + 1) * sizeof(HCERTSTORE));
524 data->pahStores = WINTRUST_Alloc(sizeof(HCERTSTORE));
529 data->pahStores[data->chStores++] = CertDuplicateStore(store);
533 SetLastError(ERROR_OUTOFMEMORY);
537 BOOL WINAPI WINTRUST_AddSgnr(CRYPT_PROVIDER_DATA *data,
538 BOOL fCounterSigner, DWORD idxSigner, CRYPT_PROVIDER_SGNR *sgnr)
542 if (sgnr->cbStruct > sizeof(CRYPT_PROVIDER_SGNR))
544 SetLastError(ERROR_INVALID_PARAMETER);
549 FIXME("unimplemented for counter signers\n");
550 SetLastError(ERROR_INVALID_PARAMETER);
554 data->pasSigners = WINTRUST_ReAlloc(data->pasSigners,
555 (data->csSigners + 1) * sizeof(CRYPT_PROVIDER_SGNR));
558 data->pasSigners = WINTRUST_Alloc(sizeof(CRYPT_PROVIDER_SGNR));
561 if (data->pasSigners)
563 if (idxSigner < data->csSigners)
564 memmove(&data->pasSigners[idxSigner],
565 &data->pasSigners[idxSigner + 1],
566 (data->csSigners - idxSigner) * sizeof(CRYPT_PROVIDER_SGNR));
568 if (sgnr->cbStruct == sizeof(CRYPT_PROVIDER_SGNR))
570 /* The PSDK says psSigner should be allocated using pfnAlloc, but
571 * it doesn't say anything about ownership. Since callers are
572 * internal, assume ownership is passed, and just store the
575 memcpy(&data->pasSigners[idxSigner], sgnr,
576 sizeof(CRYPT_PROVIDER_SGNR));
579 memset(&data->pasSigners[idxSigner], 0,
580 sizeof(CRYPT_PROVIDER_SGNR));
584 SetLastError(ERROR_OUTOFMEMORY);
588 BOOL WINAPI WINTRUST_AddCert(CRYPT_PROVIDER_DATA *data, DWORD idxSigner,
589 BOOL fCounterSigner, DWORD idxCounterSigner, PCCERT_CONTEXT pCert2Add)
595 FIXME("unimplemented for counter signers\n");
596 SetLastError(ERROR_INVALID_PARAMETER);
599 if (data->pasSigners[idxSigner].csCertChain)
600 data->pasSigners[idxSigner].pasCertChain =
601 WINTRUST_ReAlloc(data->pasSigners[idxSigner].pasCertChain,
602 (data->pasSigners[idxSigner].csCertChain + 1) *
603 sizeof(CRYPT_PROVIDER_CERT));
606 data->pasSigners[idxSigner].pasCertChain =
607 WINTRUST_Alloc(sizeof(CRYPT_PROVIDER_CERT));
608 data->pasSigners[idxSigner].csCertChain = 0;
610 if (data->pasSigners[idxSigner].pasCertChain)
612 CRYPT_PROVIDER_CERT *cert = &data->pasSigners[idxSigner].pasCertChain[
613 data->pasSigners[idxSigner].csCertChain];
615 cert->cbStruct = sizeof(CRYPT_PROVIDER_CERT);
616 cert->pCert = CertDuplicateCertificateContext(pCert2Add);
617 data->pasSigners[idxSigner].csCertChain++;
621 SetLastError(ERROR_OUTOFMEMORY);