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 static const GUID generic_chain_verify
= WINTRUST_ACTION_GENERIC_CHAIN_VERIFY
;
336 LONG err
= ERROR_SUCCESS
;
337 WINTRUST_DATA
*actionData
= (WINTRUST_DATA
*)ActionData
;
339 TRACE("(%p, %s, %p)\n", hwnd
, debugstr_guid(ActionID
), ActionData
);
340 dump_wintrust_data(ActionData
);
342 /* Support for known old-style callers: */
343 if (IsEqualGUID(ActionID
, &published_software
))
344 err
= WINTRUST_PublishedSoftware(hwnd
, ActionID
, ActionData
);
347 /* Check known actions to warn of possible problems */
348 if (!IsEqualGUID(ActionID
, &unknown
) &&
349 !IsEqualGUID(ActionID
, &generic_verify_v2
) &&
350 !IsEqualGUID(ActionID
, &generic_cert_verify
) &&
351 !IsEqualGUID(ActionID
, &generic_chain_verify
))
352 WARN("unknown action %s, default behavior may not be right\n",
353 debugstr_guid(ActionID
));
354 switch (actionData
->dwStateAction
)
356 case WTD_STATEACTION_IGNORE
:
357 err
= WINTRUST_DefaultVerifyAndClose(hwnd
, ActionID
, ActionData
);
359 case WTD_STATEACTION_VERIFY
:
360 err
= WINTRUST_DefaultVerify(hwnd
, ActionID
, ActionData
);
362 case WTD_STATEACTION_CLOSE
:
363 err
= WINTRUST_DefaultClose(hwnd
, ActionID
, ActionData
);
366 FIXME("unimplemented for %d\n", actionData
->dwStateAction
);
370 TRACE("returning %08x\n", err
);
374 /***********************************************************************
375 * WinVerifyTrustEx (WINTRUST.@)
377 HRESULT WINAPI
WinVerifyTrustEx( HWND hwnd
, GUID
*ActionID
,
378 WINTRUST_DATA
* ActionData
)
380 return WinVerifyTrust(hwnd
, ActionID
, ActionData
);
383 /***********************************************************************
384 * WTHelperGetProvSignerFromChain (WINTRUST.@)
386 CRYPT_PROVIDER_SGNR
* WINAPI
WTHelperGetProvSignerFromChain(
387 CRYPT_PROVIDER_DATA
*pProvData
, DWORD idxSigner
, BOOL fCounterSigner
,
388 DWORD idxCounterSigner
)
390 CRYPT_PROVIDER_SGNR
*sgnr
;
392 TRACE("(%p %d %d %d)\n", pProvData
, idxSigner
, fCounterSigner
,
395 if (idxSigner
>= pProvData
->csSigners
|| !pProvData
->pasSigners
)
397 sgnr
= &pProvData
->pasSigners
[idxSigner
];
400 if (idxCounterSigner
>= sgnr
->csCounterSigners
||
401 !sgnr
->pasCounterSigners
)
403 sgnr
= &sgnr
->pasCounterSigners
[idxCounterSigner
];
405 TRACE("returning %p\n", sgnr
);
409 /***********************************************************************
410 * WTHelperGetProvCertFromChain (WINTRUST.@)
412 CRYPT_PROVIDER_CERT
* WINAPI
WTHelperGetProvCertFromChain(
413 CRYPT_PROVIDER_SGNR
*pSgnr
, DWORD idxCert
)
415 CRYPT_PROVIDER_CERT
*cert
;
417 TRACE("(%p %d)\n", pSgnr
, idxCert
);
419 if (idxCert
>= pSgnr
->csCertChain
|| !pSgnr
->pasCertChain
)
421 cert
= &pSgnr
->pasCertChain
[idxCert
];
422 TRACE("returning %p\n", cert
);
426 /***********************************************************************
427 * WTHelperProvDataFromStateData (WINTRUST.@)
429 CRYPT_PROVIDER_DATA
* WINAPI
WTHelperProvDataFromStateData(HANDLE hStateData
)
431 TRACE("%p\n", hStateData
);
432 return (CRYPT_PROVIDER_DATA
*)hStateData
;
435 /***********************************************************************
436 * WTHelperGetFileName(WINTRUST.@)
438 LPCWSTR WINAPI
WTHelperGetFileName(WINTRUST_DATA
*data
)
441 if (data
->dwUnionChoice
== WTD_CHOICE_FILE
)
442 return data
->u
.pFile
->pcwszFilePath
;
447 /***********************************************************************
448 * WTHelperGetFileHandle(WINTRUST.@)
450 HANDLE WINAPI
WTHelperGetFileHandle(WINTRUST_DATA
*data
)
453 if (data
->dwUnionChoice
== WTD_CHOICE_FILE
)
454 return data
->u
.pFile
->hFile
;
456 return INVALID_HANDLE_VALUE
;
459 static const WCHAR Software_Publishing
[] = {
460 'S','o','f','t','w','a','r','e','\\',
461 'M','i','c','r','o','s','o','f','t','\\',
462 'W','i','n','d','o','w','s','\\',
463 'C','u','r','r','e','n','t','V','e','r','s','i','o','n','\\',
464 'W','i','n','t','r','u','s','t','\\',
465 'T','r','u','s','t',' ','P','r','o','v','i','d','e','r','s','\\',
466 'S','o','f','t','w','a','r','e',' ',
467 'P','u','b','l','i','s','h','i','n','g',0 };
468 static const WCHAR State
[] = { 'S','t','a','t','e',0 };
470 /***********************************************************************
471 * WintrustGetRegPolicyFlags (WINTRUST.@)
473 void WINAPI
WintrustGetRegPolicyFlags( DWORD
* pdwPolicyFlags
)
478 TRACE("%p\n", pdwPolicyFlags
);
481 r
= RegCreateKeyExW(HKEY_CURRENT_USER
, Software_Publishing
, 0, NULL
, 0,
482 KEY_READ
, NULL
, &key
, NULL
);
485 DWORD size
= sizeof(DWORD
);
487 r
= RegQueryValueExW(key
, State
, NULL
, NULL
, (LPBYTE
)pdwPolicyFlags
,
492 /* Failed to query, create and return default value */
493 *pdwPolicyFlags
= WTPF_IGNOREREVOCATIONONTS
|
494 WTPF_OFFLINEOKNBU_COM
|
495 WTPF_OFFLINEOKNBU_IND
|
498 WintrustSetRegPolicyFlags(*pdwPolicyFlags
);
503 /***********************************************************************
504 * WintrustSetRegPolicyFlags (WINTRUST.@)
506 BOOL WINAPI
WintrustSetRegPolicyFlags( DWORD dwPolicyFlags
)
511 TRACE("%x\n", dwPolicyFlags
);
513 r
= RegCreateKeyExW(HKEY_CURRENT_USER
, Software_Publishing
, 0,
514 NULL
, 0, KEY_WRITE
, NULL
, &key
, NULL
);
517 r
= RegSetValueExW(key
, State
, 0, REG_DWORD
, (LPBYTE
)&dwPolicyFlags
,
521 if (r
) SetLastError(r
);
522 return r
== ERROR_SUCCESS
;
525 /* Utility functions */
526 void * WINAPI
WINTRUST_Alloc(DWORD cb
)
528 return HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, cb
);
531 void * WINAPI
WINTRUST_ReAlloc(void *ptr
, DWORD cb
)
533 return HeapReAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, ptr
, cb
);
536 void WINAPI
WINTRUST_Free(void *p
)
538 HeapFree(GetProcessHeap(), 0, p
);
541 BOOL WINAPI
WINTRUST_AddStore(CRYPT_PROVIDER_DATA
*data
, HCERTSTORE store
)
546 data
->pahStores
= WINTRUST_ReAlloc(data
->pahStores
,
547 (data
->chStores
+ 1) * sizeof(HCERTSTORE
));
550 data
->pahStores
= WINTRUST_Alloc(sizeof(HCERTSTORE
));
555 data
->pahStores
[data
->chStores
++] = CertDuplicateStore(store
);
559 SetLastError(ERROR_OUTOFMEMORY
);
563 BOOL WINAPI
WINTRUST_AddSgnr(CRYPT_PROVIDER_DATA
*data
,
564 BOOL fCounterSigner
, DWORD idxSigner
, CRYPT_PROVIDER_SGNR
*sgnr
)
568 if (sgnr
->cbStruct
> sizeof(CRYPT_PROVIDER_SGNR
))
570 SetLastError(ERROR_INVALID_PARAMETER
);
575 FIXME("unimplemented for counter signers\n");
576 SetLastError(ERROR_INVALID_PARAMETER
);
580 data
->pasSigners
= WINTRUST_ReAlloc(data
->pasSigners
,
581 (data
->csSigners
+ 1) * sizeof(CRYPT_PROVIDER_SGNR
));
584 data
->pasSigners
= WINTRUST_Alloc(sizeof(CRYPT_PROVIDER_SGNR
));
587 if (data
->pasSigners
)
589 if (idxSigner
< data
->csSigners
)
590 memmove(&data
->pasSigners
[idxSigner
],
591 &data
->pasSigners
[idxSigner
+ 1],
592 (data
->csSigners
- idxSigner
) * sizeof(CRYPT_PROVIDER_SGNR
));
594 if (sgnr
->cbStruct
== sizeof(CRYPT_PROVIDER_SGNR
))
596 /* The PSDK says psSigner should be allocated using pfnAlloc, but
597 * it doesn't say anything about ownership. Since callers are
598 * internal, assume ownership is passed, and just store the
601 memcpy(&data
->pasSigners
[idxSigner
], sgnr
,
602 sizeof(CRYPT_PROVIDER_SGNR
));
605 memset(&data
->pasSigners
[idxSigner
], 0,
606 sizeof(CRYPT_PROVIDER_SGNR
));
610 SetLastError(ERROR_OUTOFMEMORY
);
614 BOOL WINAPI
WINTRUST_AddCert(CRYPT_PROVIDER_DATA
*data
, DWORD idxSigner
,
615 BOOL fCounterSigner
, DWORD idxCounterSigner
, PCCERT_CONTEXT pCert2Add
)
621 FIXME("unimplemented for counter signers\n");
622 SetLastError(ERROR_INVALID_PARAMETER
);
625 if (data
->pasSigners
[idxSigner
].csCertChain
)
626 data
->pasSigners
[idxSigner
].pasCertChain
=
627 WINTRUST_ReAlloc(data
->pasSigners
[idxSigner
].pasCertChain
,
628 (data
->pasSigners
[idxSigner
].csCertChain
+ 1) *
629 sizeof(CRYPT_PROVIDER_CERT
));
632 data
->pasSigners
[idxSigner
].pasCertChain
=
633 WINTRUST_Alloc(sizeof(CRYPT_PROVIDER_CERT
));
634 data
->pasSigners
[idxSigner
].csCertChain
= 0;
636 if (data
->pasSigners
[idxSigner
].pasCertChain
)
638 CRYPT_PROVIDER_CERT
*cert
= &data
->pasSigners
[idxSigner
].pasCertChain
[
639 data
->pasSigners
[idxSigner
].csCertChain
];
641 cert
->cbStruct
= sizeof(CRYPT_PROVIDER_CERT
);
642 cert
->pCert
= CertDuplicateCertificateContext(pCert2Add
);
643 data
->pasSigners
[idxSigner
].csCertChain
++;
647 SetLastError(ERROR_OUTOFMEMORY
);