wintrust: Return error directly from WINTRUST_VerifySigner.
[wine/hramrach.git] / dlls / wintrust / softpub.c
blob4a702c16edb3ec5cb99c6a3680fe71767283e7ca
1 /*
2 * Copyright 2007 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
18 #include <stdarg.h>
20 #define NONAMELESSUNION
22 #include "windef.h"
23 #include "winbase.h"
24 #include "wintrust.h"
25 #include "mssip.h"
26 #include "softpub.h"
27 #include "wine/debug.h"
29 WINE_DEFAULT_DEBUG_CHANNEL(wintrust);
31 HRESULT WINAPI SoftpubDefCertInit(CRYPT_PROVIDER_DATA *data)
33 HRESULT ret = S_FALSE;
35 TRACE("(%p)\n", data);
37 if (data->padwTrustStepErrors &&
38 !data->padwTrustStepErrors[TRUSTERROR_STEP_FINAL_WVTINIT])
39 ret = S_OK;
40 TRACE("returning %08x\n", ret);
41 return ret;
44 HRESULT WINAPI SoftpubInitialize(CRYPT_PROVIDER_DATA *data)
46 HRESULT ret = S_FALSE;
48 TRACE("(%p)\n", data);
50 if (data->padwTrustStepErrors &&
51 !data->padwTrustStepErrors[TRUSTERROR_STEP_FINAL_WVTINIT])
52 ret = S_OK;
53 TRACE("returning %08x\n", ret);
54 return ret;
57 HRESULT WINAPI DriverInitializePolicy(CRYPT_PROVIDER_DATA *data)
59 FIXME("stub\n");
60 return S_OK;
63 HRESULT WINAPI DriverCleanupPolicy(CRYPT_PROVIDER_DATA *data)
65 FIXME("stub\n");
66 return S_OK;
69 HRESULT WINAPI DriverFinalPolicy(CRYPT_PROVIDER_DATA *data)
71 FIXME("stub\n");
72 return S_OK;
75 /* Assumes data->pWintrustData->u.pFile exists. Makes sure a file handle is
76 * open for the file.
78 static DWORD SOFTPUB_OpenFile(CRYPT_PROVIDER_DATA *data)
80 DWORD err = ERROR_SUCCESS;
82 /* PSDK implies that all values should be initialized to NULL, so callers
83 * typically have hFile as NULL rather than INVALID_HANDLE_VALUE. Check
84 * for both.
86 if (!data->pWintrustData->u.pFile->hFile ||
87 data->pWintrustData->u.pFile->hFile == INVALID_HANDLE_VALUE)
89 data->pWintrustData->u.pFile->hFile =
90 CreateFileW(data->pWintrustData->u.pFile->pcwszFilePath, GENERIC_READ,
91 FILE_SHARE_READ, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
92 if (data->pWintrustData->u.pFile->hFile != INVALID_HANDLE_VALUE)
93 data->fOpenedFile = TRUE;
94 else
95 err = GetLastError();
97 if (!err)
98 GetFileTime(data->pWintrustData->u.pFile->hFile, &data->sftSystemTime,
99 NULL, NULL);
100 TRACE("returning %d\n", err);
101 return err;
104 /* Assumes data->pWintrustData->u.pFile exists. Sets data->pPDSip->gSubject to
105 * the file's subject GUID.
107 static DWORD SOFTPUB_GetFileSubject(CRYPT_PROVIDER_DATA *data)
109 DWORD err = ERROR_SUCCESS;
111 if (!WVT_ISINSTRUCT(WINTRUST_FILE_INFO,
112 data->pWintrustData->u.pFile->cbStruct, pgKnownSubject) ||
113 !data->pWintrustData->u.pFile->pgKnownSubject)
115 if (!CryptSIPRetrieveSubjectGuid(
116 data->pWintrustData->u.pFile->pcwszFilePath,
117 data->pWintrustData->u.pFile->hFile,
118 &data->u.pPDSip->gSubject))
119 err = GetLastError();
121 else
122 data->u.pPDSip->gSubject = *data->pWintrustData->u.pFile->pgKnownSubject;
123 TRACE("returning %d\n", err);
124 return err;
127 /* Assumes data->u.pPDSip exists, and its gSubject member set.
128 * Allocates data->u.pPDSip->pSip and loads it, if possible.
130 static DWORD SOFTPUB_GetSIP(CRYPT_PROVIDER_DATA *data)
132 DWORD err = ERROR_SUCCESS;
134 data->u.pPDSip->pSip = data->psPfns->pfnAlloc(sizeof(SIP_DISPATCH_INFO));
135 if (data->u.pPDSip->pSip)
137 if (!CryptSIPLoad(&data->u.pPDSip->gSubject, 0, data->u.pPDSip->pSip))
138 err = GetLastError();
140 else
141 err = ERROR_OUTOFMEMORY;
142 TRACE("returning %d\n", err);
143 return err;
146 /* Assumes data->u.pPDSip has been loaded, and data->u.pPDSip->pSip allocated.
147 * Calls data->u.pPDSip->pSip->pfGet to construct data->hMsg.
149 static DWORD SOFTPUB_GetMessageFromFile(CRYPT_PROVIDER_DATA *data, HANDLE file,
150 LPCWSTR filePath)
152 DWORD err = ERROR_SUCCESS;
153 BOOL ret;
154 LPBYTE buf = NULL;
155 DWORD size = 0;
157 data->u.pPDSip->psSipSubjectInfo =
158 data->psPfns->pfnAlloc(sizeof(SIP_SUBJECTINFO));
159 if (!data->u.pPDSip->psSipSubjectInfo)
160 return ERROR_OUTOFMEMORY;
162 data->u.pPDSip->psSipSubjectInfo->cbSize = sizeof(SIP_SUBJECTINFO);
163 data->u.pPDSip->psSipSubjectInfo->pgSubjectType = &data->u.pPDSip->gSubject;
164 data->u.pPDSip->psSipSubjectInfo->hFile = file;
165 data->u.pPDSip->psSipSubjectInfo->pwsFileName = filePath;
166 data->u.pPDSip->psSipSubjectInfo->hProv = data->hProv;
167 ret = data->u.pPDSip->pSip->pfGet(data->u.pPDSip->psSipSubjectInfo,
168 &data->dwEncoding, 0, &size, 0);
169 if (!ret)
170 return TRUST_E_NOSIGNATURE;
172 buf = data->psPfns->pfnAlloc(size);
173 if (!buf)
174 return ERROR_OUTOFMEMORY;
176 ret = data->u.pPDSip->pSip->pfGet(data->u.pPDSip->psSipSubjectInfo,
177 &data->dwEncoding, 0, &size, buf);
178 if (ret)
180 data->hMsg = CryptMsgOpenToDecode(data->dwEncoding, 0, 0, data->hProv,
181 NULL, NULL);
182 if (data->hMsg)
184 ret = CryptMsgUpdate(data->hMsg, buf, size, TRUE);
185 if (!ret)
186 err = GetLastError();
189 else
190 err = GetLastError();
192 data->psPfns->pfnFree(buf);
193 TRACE("returning %d\n", err);
194 return err;
197 static DWORD SOFTPUB_CreateStoreFromMessage(CRYPT_PROVIDER_DATA *data)
199 DWORD err = ERROR_SUCCESS;
200 HCERTSTORE store;
202 store = CertOpenStore(CERT_STORE_PROV_MSG, data->dwEncoding,
203 data->hProv, CERT_STORE_NO_CRYPT_RELEASE_FLAG, data->hMsg);
204 if (store)
206 if (!data->psPfns->pfnAddStore2Chain(data, store))
207 err = GetLastError();
208 CertCloseStore(store, 0);
210 else
211 err = GetLastError();
212 TRACE("returning %d\n", err);
213 return err;
216 static DWORD SOFTPUB_DecodeInnerContent(CRYPT_PROVIDER_DATA *data)
218 BOOL ret;
219 DWORD size, err = ERROR_SUCCESS;
220 LPSTR oid = NULL;
221 LPBYTE buf = NULL;
223 ret = CryptMsgGetParam(data->hMsg, CMSG_INNER_CONTENT_TYPE_PARAM, 0, NULL,
224 &size);
225 if (!ret)
227 err = GetLastError();
228 goto error;
230 oid = data->psPfns->pfnAlloc(size);
231 if (!oid)
233 err = ERROR_OUTOFMEMORY;
234 goto error;
236 ret = CryptMsgGetParam(data->hMsg, CMSG_INNER_CONTENT_TYPE_PARAM, 0, oid,
237 &size);
238 if (!ret)
240 err = GetLastError();
241 goto error;
243 ret = CryptMsgGetParam(data->hMsg, CMSG_CONTENT_PARAM, 0, NULL, &size);
244 if (!ret)
246 err = GetLastError();
247 goto error;
249 buf = data->psPfns->pfnAlloc(size);
250 if (!buf)
252 err = ERROR_OUTOFMEMORY;
253 goto error;
255 ret = CryptMsgGetParam(data->hMsg, CMSG_CONTENT_PARAM, 0, buf, &size);
256 if (!ret)
258 err = GetLastError();
259 goto error;
261 ret = CryptDecodeObject(data->dwEncoding, oid, buf, size, 0, NULL, &size);
262 if (!ret)
264 err = GetLastError();
265 goto error;
267 data->u.pPDSip->psIndirectData = data->psPfns->pfnAlloc(size);
268 if (!data->u.pPDSip->psIndirectData)
270 err = ERROR_OUTOFMEMORY;
271 goto error;
273 ret = CryptDecodeObject(data->dwEncoding, oid, buf, size, 0,
274 data->u.pPDSip->psIndirectData, &size);
275 if (!ret)
276 err = GetLastError();
278 error:
279 TRACE("returning %d\n", err);
280 data->psPfns->pfnFree(oid);
281 data->psPfns->pfnFree(buf);
282 return err;
285 static DWORD SOFTPUB_LoadCertMessage(CRYPT_PROVIDER_DATA *data)
287 DWORD err;
289 if (data->pWintrustData->u.pCert &&
290 WVT_IS_CBSTRUCT_GT_MEMBEROFFSET(WINTRUST_CERT_INFO,
291 data->pWintrustData->u.pCert->cbStruct, psCertContext))
293 if (data->psPfns)
295 CRYPT_PROVIDER_SGNR signer = { sizeof(signer), { 0 } };
296 DWORD i;
297 BOOL ret;
299 /* Add a signer with nothing but the time to verify, so we can
300 * add a cert to it
302 if (WVT_ISINSTRUCT(WINTRUST_CERT_INFO,
303 data->pWintrustData->u.pCert->cbStruct, psftVerifyAsOf) &&
304 data->pWintrustData->u.pCert->psftVerifyAsOf)
305 data->sftSystemTime = signer.sftVerifyAsOf;
306 else
308 SYSTEMTIME sysTime;
310 GetSystemTime(&sysTime);
311 SystemTimeToFileTime(&sysTime, &signer.sftVerifyAsOf);
313 ret = data->psPfns->pfnAddSgnr2Chain(data, FALSE, 0, &signer);
314 if (ret)
316 ret = data->psPfns->pfnAddCert2Chain(data, 0, FALSE, 0,
317 data->pWintrustData->u.pCert->psCertContext);
318 if (WVT_ISINSTRUCT(WINTRUST_CERT_INFO,
319 data->pWintrustData->u.pCert->cbStruct, pahStores))
320 for (i = 0;
321 ret && i < data->pWintrustData->u.pCert->chStores; i++)
322 ret = data->psPfns->pfnAddStore2Chain(data,
323 data->pWintrustData->u.pCert->pahStores[i]);
325 if (!ret)
326 err = GetLastError();
328 else
330 /* Do nothing!? See the tests */
331 err = ERROR_SUCCESS;
334 else
335 err = ERROR_INVALID_PARAMETER;
336 return err;
339 static DWORD SOFTPUB_LoadFileMessage(CRYPT_PROVIDER_DATA *data)
341 DWORD err = ERROR_SUCCESS;
343 if (!data->pWintrustData->u.pFile)
345 err = ERROR_INVALID_PARAMETER;
346 goto error;
348 err = SOFTPUB_OpenFile(data);
349 if (err)
350 goto error;
351 err = SOFTPUB_GetFileSubject(data);
352 if (err)
353 goto error;
354 err = SOFTPUB_GetSIP(data);
355 if (err)
356 goto error;
357 err = SOFTPUB_GetMessageFromFile(data, data->pWintrustData->u.pFile->hFile,
358 data->pWintrustData->u.pFile->pcwszFilePath);
359 if (err)
360 goto error;
361 err = SOFTPUB_CreateStoreFromMessage(data);
362 if (err)
363 goto error;
364 err = SOFTPUB_DecodeInnerContent(data);
366 error:
367 if (err && data->fOpenedFile && data->pWintrustData->u.pFile)
369 /* The caller won't expect the file to be open on failure, so close it.
371 CloseHandle(data->pWintrustData->u.pFile->hFile);
372 data->pWintrustData->u.pFile->hFile = INVALID_HANDLE_VALUE;
373 data->fOpenedFile = FALSE;
375 return err;
378 static DWORD SOFTPUB_LoadCatalogMessage(CRYPT_PROVIDER_DATA *data)
380 DWORD err;
381 HANDLE catalog = INVALID_HANDLE_VALUE;
383 if (!data->pWintrustData->u.pCatalog)
385 SetLastError(ERROR_INVALID_PARAMETER);
386 return FALSE;
388 catalog = CreateFileW(data->pWintrustData->u.pCatalog->pcwszCatalogFilePath,
389 GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL,
390 NULL);
391 if (catalog == INVALID_HANDLE_VALUE)
392 return GetLastError();
393 if (!CryptSIPRetrieveSubjectGuid(
394 data->pWintrustData->u.pCatalog->pcwszCatalogFilePath, catalog,
395 &data->u.pPDSip->gSubject))
397 err = GetLastError();
398 goto error;
400 err = SOFTPUB_GetSIP(data);
401 if (err)
402 goto error;
403 err = SOFTPUB_GetMessageFromFile(data, data->pWintrustData->u.pFile->hFile,
404 data->pWintrustData->u.pFile->pcwszFilePath);
405 if (err)
406 goto error;
407 err = SOFTPUB_CreateStoreFromMessage(data);
408 if (err)
409 goto error;
410 err = SOFTPUB_DecodeInnerContent(data);
411 /* FIXME: this loads the catalog file, but doesn't validate the member. */
412 error:
413 CloseHandle(catalog);
414 return err;
417 HRESULT WINAPI SoftpubLoadMessage(CRYPT_PROVIDER_DATA *data)
419 DWORD err = ERROR_SUCCESS;
421 TRACE("(%p)\n", data);
423 if (!data->padwTrustStepErrors)
424 return S_FALSE;
426 switch (data->pWintrustData->dwUnionChoice)
428 case WTD_CHOICE_CERT:
429 err = SOFTPUB_LoadCertMessage(data);
430 break;
431 case WTD_CHOICE_FILE:
432 err = SOFTPUB_LoadFileMessage(data);
433 break;
434 case WTD_CHOICE_CATALOG:
435 err = SOFTPUB_LoadCatalogMessage(data);
436 break;
437 default:
438 FIXME("unimplemented for %d\n", data->pWintrustData->dwUnionChoice);
439 err = ERROR_INVALID_PARAMETER;
442 if (err)
443 data->padwTrustStepErrors[TRUSTERROR_STEP_FINAL_OBJPROV] = err;
444 TRACE("returning %d (%08x)\n", !err ? S_OK : S_FALSE,
445 data->padwTrustStepErrors[TRUSTERROR_STEP_FINAL_OBJPROV]);
446 return !err ? S_OK : S_FALSE;
449 static CMSG_SIGNER_INFO *WINTRUST_GetSigner(CRYPT_PROVIDER_DATA *data,
450 DWORD signerIdx)
452 BOOL ret;
453 CMSG_SIGNER_INFO *signerInfo = NULL;
454 DWORD size;
456 ret = CryptMsgGetParam(data->hMsg, CMSG_SIGNER_INFO_PARAM, signerIdx,
457 NULL, &size);
458 if (ret)
460 signerInfo = data->psPfns->pfnAlloc(size);
461 if (signerInfo)
463 ret = CryptMsgGetParam(data->hMsg, CMSG_SIGNER_INFO_PARAM,
464 signerIdx, signerInfo, &size);
465 if (!ret)
467 data->psPfns->pfnFree(signerInfo);
468 signerInfo = NULL;
471 else
472 SetLastError(ERROR_OUTOFMEMORY);
474 return signerInfo;
477 static BOOL WINTRUST_SaveSigner(CRYPT_PROVIDER_DATA *data, DWORD signerIdx)
479 BOOL ret;
480 CMSG_SIGNER_INFO *signerInfo = WINTRUST_GetSigner(data, signerIdx);
482 if (signerInfo)
484 CRYPT_PROVIDER_SGNR sgnr = { sizeof(sgnr), { 0 } };
486 sgnr.psSigner = signerInfo;
487 sgnr.sftVerifyAsOf = data->sftSystemTime;
488 ret = data->psPfns->pfnAddSgnr2Chain(data, FALSE, signerIdx, &sgnr);
490 else
491 ret = FALSE;
492 return ret;
495 static CERT_INFO *WINTRUST_GetSignerCertInfo(CRYPT_PROVIDER_DATA *data,
496 DWORD signerIdx)
498 BOOL ret;
499 CERT_INFO *certInfo = NULL;
500 DWORD size;
502 ret = CryptMsgGetParam(data->hMsg, CMSG_SIGNER_CERT_INFO_PARAM, signerIdx,
503 NULL, &size);
504 if (ret)
506 certInfo = data->psPfns->pfnAlloc(size);
507 if (certInfo)
509 ret = CryptMsgGetParam(data->hMsg, CMSG_SIGNER_CERT_INFO_PARAM,
510 signerIdx, certInfo, &size);
511 if (!ret)
513 data->psPfns->pfnFree(certInfo);
514 certInfo = NULL;
517 else
518 SetLastError(ERROR_OUTOFMEMORY);
520 return certInfo;
523 static DWORD WINTRUST_VerifySigner(CRYPT_PROVIDER_DATA *data, DWORD signerIdx)
525 DWORD err;
526 CERT_INFO *certInfo = WINTRUST_GetSignerCertInfo(data, signerIdx);
528 if (certInfo)
530 PCCERT_CONTEXT subject = CertGetSubjectCertificateFromStore(
531 data->pahStores[0], data->dwEncoding, certInfo);
533 if (subject)
535 CMSG_CTRL_VERIFY_SIGNATURE_EX_PARA para = { sizeof(para), 0,
536 signerIdx, CMSG_VERIFY_SIGNER_CERT, (LPVOID)subject };
538 if (!CryptMsgControl(data->hMsg, 0, CMSG_CTRL_VERIFY_SIGNATURE_EX,
539 &para))
540 err = TRUST_E_CERT_SIGNATURE;
541 else
543 data->psPfns->pfnAddCert2Chain(data, signerIdx, FALSE, 0,
544 subject);
545 err = ERROR_SUCCESS;
547 CertFreeCertificateContext(subject);
549 else
550 err = TRUST_E_NO_SIGNER_CERT;
551 data->psPfns->pfnFree(certInfo);
553 else
554 err = GetLastError();
555 return err;
558 HRESULT WINAPI SoftpubLoadSignature(CRYPT_PROVIDER_DATA *data)
560 BOOL ret;
562 TRACE("(%p)\n", data);
564 if (!data->padwTrustStepErrors)
565 return S_FALSE;
567 if (data->hMsg)
569 DWORD signerCount, size;
571 size = sizeof(signerCount);
572 ret = CryptMsgGetParam(data->hMsg, CMSG_SIGNER_COUNT_PARAM, 0,
573 &signerCount, &size);
574 if (ret)
576 DWORD i;
578 for (i = 0; ret && i < signerCount; i++)
580 if ((ret = WINTRUST_SaveSigner(data, i)))
582 DWORD err;
584 err = WINTRUST_VerifySigner(data, i);
585 if (err)
587 SetLastError(err);
588 ret = FALSE;
593 else
594 SetLastError(TRUST_E_NOSIGNATURE);
596 else
597 ret = TRUE;
598 if (!ret)
599 data->padwTrustStepErrors[TRUSTERROR_STEP_FINAL_SIGPROV] =
600 GetLastError();
601 return ret ? S_OK : S_FALSE;
604 static DWORD WINTRUST_TrustStatusToConfidence(DWORD errorStatus)
606 DWORD confidence = 0;
608 confidence = 0;
609 if (!(errorStatus & CERT_TRUST_IS_NOT_SIGNATURE_VALID))
610 confidence |= CERT_CONFIDENCE_SIG;
611 if (!(errorStatus & CERT_TRUST_IS_NOT_TIME_VALID))
612 confidence |= CERT_CONFIDENCE_TIME;
613 if (!(errorStatus & CERT_TRUST_IS_NOT_TIME_NESTED))
614 confidence |= CERT_CONFIDENCE_TIMENEST;
615 return confidence;
618 BOOL WINAPI SoftpubCheckCert(CRYPT_PROVIDER_DATA *data, DWORD idxSigner,
619 BOOL fCounterSignerChain, DWORD idxCounterSigner)
621 BOOL ret;
623 TRACE("(%p, %d, %d, %d)\n", data, idxSigner, fCounterSignerChain,
624 idxCounterSigner);
626 if (fCounterSignerChain)
628 FIXME("unimplemented for counter signers\n");
629 ret = FALSE;
631 else
633 PCERT_SIMPLE_CHAIN simpleChain =
634 data->pasSigners[idxSigner].pChainContext->rgpChain[0];
635 DWORD i;
637 ret = TRUE;
638 for (i = 0; i < simpleChain->cElement; i++)
640 /* Set confidence */
641 data->pasSigners[idxSigner].pasCertChain[i].dwConfidence =
642 WINTRUST_TrustStatusToConfidence(
643 simpleChain->rgpElement[i]->TrustStatus.dwErrorStatus);
644 /* Set additional flags */
645 if (!(simpleChain->rgpElement[i]->TrustStatus.dwErrorStatus &
646 CERT_TRUST_IS_UNTRUSTED_ROOT))
647 data->pasSigners[idxSigner].pasCertChain[i].fTrustedRoot = TRUE;
648 if (simpleChain->rgpElement[i]->TrustStatus.dwInfoStatus &
649 CERT_TRUST_IS_SELF_SIGNED)
650 data->pasSigners[idxSigner].pasCertChain[i].fSelfSigned = TRUE;
651 if (simpleChain->rgpElement[i]->TrustStatus.dwErrorStatus &
652 CERT_TRUST_IS_CYCLIC)
653 data->pasSigners[idxSigner].pasCertChain[i].fIsCyclic = TRUE;
656 return ret;
659 static DWORD WINTRUST_TrustStatusToError(DWORD errorStatus)
661 DWORD error;
663 if (errorStatus & CERT_TRUST_IS_NOT_SIGNATURE_VALID)
664 error = TRUST_E_CERT_SIGNATURE;
665 else if (errorStatus & CERT_TRUST_IS_UNTRUSTED_ROOT)
666 error = CERT_E_UNTRUSTEDROOT;
667 else if (errorStatus & CERT_TRUST_IS_NOT_TIME_VALID)
668 error = CERT_E_EXPIRED;
669 else if (errorStatus & CERT_TRUST_IS_NOT_TIME_NESTED)
670 error = CERT_E_VALIDITYPERIODNESTING;
671 else if (errorStatus & CERT_TRUST_IS_REVOKED)
672 error = CERT_E_REVOKED;
673 else if (errorStatus & CERT_TRUST_IS_OFFLINE_REVOCATION ||
674 errorStatus & CERT_TRUST_REVOCATION_STATUS_UNKNOWN)
675 error = CERT_E_REVOCATION_FAILURE;
676 else if (errorStatus & CERT_TRUST_IS_NOT_VALID_FOR_USAGE)
677 error = CERT_E_WRONG_USAGE;
678 else if (errorStatus & CERT_TRUST_IS_CYCLIC)
679 error = CERT_E_CHAINING;
680 else if (errorStatus & CERT_TRUST_INVALID_EXTENSION)
681 error = CERT_E_CRITICAL;
682 else if (errorStatus & CERT_TRUST_INVALID_POLICY_CONSTRAINTS)
683 error = CERT_E_INVALID_POLICY;
684 else if (errorStatus & CERT_TRUST_INVALID_BASIC_CONSTRAINTS)
685 error = TRUST_E_BASIC_CONSTRAINTS;
686 else if (errorStatus & CERT_TRUST_INVALID_NAME_CONSTRAINTS ||
687 errorStatus & CERT_TRUST_HAS_NOT_SUPPORTED_NAME_CONSTRAINT ||
688 errorStatus & CERT_TRUST_HAS_NOT_DEFINED_NAME_CONSTRAINT ||
689 errorStatus & CERT_TRUST_HAS_NOT_PERMITTED_NAME_CONSTRAINT ||
690 errorStatus & CERT_TRUST_HAS_EXCLUDED_NAME_CONSTRAINT)
691 error = CERT_E_INVALID_NAME;
692 else if (errorStatus & CERT_TRUST_NO_ISSUANCE_CHAIN_POLICY)
693 error = CERT_E_INVALID_POLICY;
694 else if (errorStatus)
696 FIXME("unknown error status %08x\n", errorStatus);
697 error = TRUST_E_SYSTEM_ERROR;
699 else
700 error = S_OK;
701 return error;
704 static BOOL WINTRUST_CopyChain(CRYPT_PROVIDER_DATA *data, DWORD signerIdx)
706 BOOL ret;
707 PCERT_SIMPLE_CHAIN simpleChain =
708 data->pasSigners[signerIdx].pChainContext->rgpChain[0];
709 DWORD i;
711 data->pasSigners[signerIdx].pasCertChain[0].dwConfidence =
712 WINTRUST_TrustStatusToConfidence(
713 simpleChain->rgpElement[0]->TrustStatus.dwErrorStatus);
714 data->pasSigners[signerIdx].pasCertChain[0].pChainElement =
715 simpleChain->rgpElement[0];
716 ret = TRUE;
717 for (i = 1; ret && i < simpleChain->cElement; i++)
719 ret = data->psPfns->pfnAddCert2Chain(data, signerIdx, FALSE, 0,
720 simpleChain->rgpElement[i]->pCertContext);
721 if (ret)
723 data->pasSigners[signerIdx].pasCertChain[i].pChainElement =
724 simpleChain->rgpElement[i];
725 data->pasSigners[signerIdx].pasCertChain[i].dwConfidence =
726 WINTRUST_TrustStatusToConfidence(
727 simpleChain->rgpElement[i]->TrustStatus.dwErrorStatus);
730 data->pasSigners[signerIdx].pasCertChain[simpleChain->cElement - 1].dwError
731 = WINTRUST_TrustStatusToError(
732 simpleChain->rgpElement[simpleChain->cElement - 1]->
733 TrustStatus.dwErrorStatus);
734 return ret;
737 static void WINTRUST_CreateChainPolicyCreateInfo(
738 const CRYPT_PROVIDER_DATA *data, PWTD_GENERIC_CHAIN_POLICY_CREATE_INFO info,
739 PCERT_CHAIN_PARA chainPara)
741 chainPara->cbSize = sizeof(CERT_CHAIN_PARA);
742 if (data->pRequestUsage)
743 chainPara->RequestedUsage = *data->pRequestUsage;
744 else
746 chainPara->RequestedUsage.dwType = 0;
747 chainPara->RequestedUsage.Usage.cUsageIdentifier = 0;
749 info->u.cbSize = sizeof(WTD_GENERIC_CHAIN_POLICY_CREATE_INFO);
750 info->hChainEngine = NULL;
751 info->pChainPara = chainPara;
752 if (data->dwProvFlags & CPD_REVOCATION_CHECK_END_CERT)
753 info->dwFlags = CERT_CHAIN_REVOCATION_CHECK_END_CERT;
754 else if (data->dwProvFlags & CPD_REVOCATION_CHECK_CHAIN)
755 info->dwFlags = CERT_CHAIN_REVOCATION_CHECK_CHAIN;
756 else if (data->dwProvFlags & CPD_REVOCATION_CHECK_CHAIN_EXCLUDE_ROOT)
757 info->dwFlags = CERT_CHAIN_REVOCATION_CHECK_CHAIN_EXCLUDE_ROOT;
758 else
759 info->dwFlags = 0;
760 info->pvReserved = NULL;
763 static BOOL WINTRUST_CreateChainForSigner(CRYPT_PROVIDER_DATA *data,
764 DWORD signer, PWTD_GENERIC_CHAIN_POLICY_CREATE_INFO createInfo,
765 PCERT_CHAIN_PARA chainPara)
767 BOOL ret = TRUE;
768 HCERTSTORE store = NULL;
770 if (data->chStores)
772 store = CertOpenStore(CERT_STORE_PROV_COLLECTION, 0, 0,
773 CERT_STORE_CREATE_NEW_FLAG, NULL);
774 if (store)
776 DWORD i;
778 for (i = 0; i < data->chStores; i++)
779 CertAddStoreToCollection(store, data->pahStores[i], 0, 0);
782 /* Expect the end certificate for each signer to be the only cert in the
783 * chain:
785 if (data->pasSigners[signer].csCertChain)
787 /* Create a certificate chain for each signer */
788 ret = CertGetCertificateChain(createInfo->hChainEngine,
789 data->pasSigners[signer].pasCertChain[0].pCert,
790 &data->pasSigners[signer].sftVerifyAsOf, store,
791 chainPara, createInfo->dwFlags, createInfo->pvReserved,
792 &data->pasSigners[signer].pChainContext);
793 if (ret)
795 if (data->pasSigners[signer].pChainContext->cChain != 1)
797 FIXME("unimplemented for more than 1 simple chain\n");
798 ret = FALSE;
800 else
802 if ((ret = WINTRUST_CopyChain(data, signer)))
804 if (data->psPfns->pfnCertCheckPolicy)
805 ret = data->psPfns->pfnCertCheckPolicy(data, signer,
806 FALSE, 0);
807 else
808 TRACE("no cert check policy, skipping policy check\n");
813 CertCloseStore(store, 0);
814 return ret;
817 HRESULT WINAPI WintrustCertificateTrust(CRYPT_PROVIDER_DATA *data)
819 BOOL ret;
821 TRACE("(%p)\n", data);
823 if (!data->csSigners)
825 ret = FALSE;
826 SetLastError(TRUST_E_NOSIGNATURE);
828 else
830 DWORD i;
831 WTD_GENERIC_CHAIN_POLICY_CREATE_INFO createInfo;
832 CERT_CHAIN_PARA chainPara;
834 WINTRUST_CreateChainPolicyCreateInfo(data, &createInfo, &chainPara);
835 ret = TRUE;
836 for (i = 0; i < data->csSigners; i++)
837 ret = WINTRUST_CreateChainForSigner(data, i, &createInfo,
838 &chainPara);
840 if (!ret)
841 data->padwTrustStepErrors[TRUSTERROR_STEP_FINAL_CERTPROV] =
842 GetLastError();
843 TRACE("returning %d (%08x)\n", ret ? S_OK : S_FALSE,
844 data->padwTrustStepErrors[TRUSTERROR_STEP_FINAL_CERTPROV]);
845 return ret ? S_OK : S_FALSE;
848 HRESULT WINAPI GenericChainCertificateTrust(CRYPT_PROVIDER_DATA *data)
850 BOOL ret;
851 WTD_GENERIC_CHAIN_POLICY_DATA *policyData =
852 data->pWintrustData->pPolicyCallbackData;
854 TRACE("(%p)\n", data);
856 if (policyData && policyData->u.cbSize !=
857 sizeof(WTD_GENERIC_CHAIN_POLICY_CREATE_INFO))
859 SetLastError(ERROR_INVALID_PARAMETER);
860 ret = FALSE;
861 goto end;
863 if (!data->csSigners)
865 ret = FALSE;
866 SetLastError(TRUST_E_NOSIGNATURE);
868 else
870 DWORD i;
871 WTD_GENERIC_CHAIN_POLICY_CREATE_INFO createInfo, *pCreateInfo;
872 CERT_CHAIN_PARA chainPara, *pChainPara;
874 if (policyData)
876 pCreateInfo = policyData->pSignerChainInfo;
877 pChainPara = pCreateInfo->pChainPara;
879 else
881 WINTRUST_CreateChainPolicyCreateInfo(data, &createInfo, &chainPara);
882 pChainPara = &chainPara;
883 pCreateInfo = &createInfo;
885 ret = TRUE;
886 for (i = 0; i < data->csSigners; i++)
887 ret = WINTRUST_CreateChainForSigner(data, i, pCreateInfo,
888 pChainPara);
891 end:
892 if (!ret)
893 data->padwTrustStepErrors[TRUSTERROR_STEP_FINAL_CERTPROV] =
894 GetLastError();
895 TRACE("returning %d (%08x)\n", ret ? S_OK : S_FALSE,
896 data->padwTrustStepErrors[TRUSTERROR_STEP_FINAL_CERTPROV]);
897 return ret ? S_OK : S_FALSE;
900 HRESULT WINAPI SoftpubAuthenticode(CRYPT_PROVIDER_DATA *data)
902 BOOL ret;
903 CERT_CHAIN_POLICY_STATUS policyStatus = { sizeof(policyStatus), 0 };
905 TRACE("(%p)\n", data);
907 if (data->pWintrustData->dwUIChoice != WTD_UI_NONE)
908 FIXME("unimplemented for UI choice %d\n",
909 data->pWintrustData->dwUIChoice);
910 if (!data->csSigners)
912 ret = FALSE;
913 policyStatus.dwError = TRUST_E_NOSIGNATURE;
915 else
917 DWORD i;
919 ret = TRUE;
920 for (i = 0; ret && i < data->csSigners; i++)
922 BYTE hash[20];
923 DWORD size = sizeof(hash);
925 /* First make sure cert isn't disallowed */
926 if ((ret = CertGetCertificateContextProperty(
927 data->pasSigners[i].pasCertChain[0].pCert,
928 CERT_SIGNATURE_HASH_PROP_ID, hash, &size)))
930 static const WCHAR disallowedW[] =
931 { 'D','i','s','a','l','l','o','w','e','d',0 };
932 HCERTSTORE disallowed = CertOpenStore(CERT_STORE_PROV_SYSTEM_W,
933 X509_ASN_ENCODING, 0, CERT_SYSTEM_STORE_CURRENT_USER,
934 disallowedW);
936 if (disallowed)
938 PCCERT_CONTEXT found = CertFindCertificateInStore(
939 disallowed, X509_ASN_ENCODING, 0, CERT_FIND_SIGNATURE_HASH,
940 hash, NULL);
942 if (found)
944 /* Disallowed! Can't verify it. */
945 policyStatus.dwError = TRUST_E_SUBJECT_NOT_TRUSTED;
946 ret = FALSE;
947 CertFreeCertificateContext(found);
949 CertCloseStore(disallowed, 0);
952 if (ret)
954 CERT_CHAIN_POLICY_PARA policyPara = { sizeof(policyPara), 0 };
956 if (data->dwRegPolicySettings & WTPF_TRUSTTEST)
957 policyPara.dwFlags |= CERT_CHAIN_POLICY_TRUST_TESTROOT_FLAG;
958 if (data->dwRegPolicySettings & WTPF_TESTCANBEVALID)
959 policyPara.dwFlags |= CERT_CHAIN_POLICY_ALLOW_TESTROOT_FLAG;
960 if (data->dwRegPolicySettings & WTPF_IGNOREEXPIRATION)
961 policyPara.dwFlags |=
962 CERT_CHAIN_POLICY_IGNORE_NOT_TIME_VALID_FLAG |
963 CERT_CHAIN_POLICY_IGNORE_CTL_NOT_TIME_VALID_FLAG |
964 CERT_CHAIN_POLICY_IGNORE_NOT_TIME_NESTED_FLAG;
965 if (data->dwRegPolicySettings & WTPF_IGNOREREVOKATION)
966 policyPara.dwFlags |=
967 CERT_CHAIN_POLICY_IGNORE_END_REV_UNKNOWN_FLAG |
968 CERT_CHAIN_POLICY_IGNORE_CTL_SIGNER_REV_UNKNOWN_FLAG |
969 CERT_CHAIN_POLICY_IGNORE_CA_REV_UNKNOWN_FLAG |
970 CERT_CHAIN_POLICY_IGNORE_ROOT_REV_UNKNOWN_FLAG;
971 CertVerifyCertificateChainPolicy(CERT_CHAIN_POLICY_AUTHENTICODE,
972 data->pasSigners[i].pChainContext, &policyPara, &policyStatus);
973 if (policyStatus.dwError != NO_ERROR)
974 ret = FALSE;
978 if (!ret)
979 data->padwTrustStepErrors[TRUSTERROR_STEP_FINAL_POLICYPROV] =
980 policyStatus.dwError;
981 TRACE("returning %d (%08x)\n", ret ? S_OK : S_FALSE,
982 data->padwTrustStepErrors[TRUSTERROR_STEP_FINAL_POLICYPROV]);
983 return ret ? S_OK : S_FALSE;
986 static HRESULT WINAPI WINTRUST_DefaultPolicy(CRYPT_PROVIDER_DATA *pProvData,
987 DWORD dwStepError, DWORD dwRegPolicySettings, DWORD cSigner,
988 PWTD_GENERIC_CHAIN_POLICY_SIGNER_INFO rgpSigner, void *pvPolicyArg)
990 DWORD i;
991 CERT_CHAIN_POLICY_STATUS policyStatus = { sizeof(policyStatus), 0 };
993 for (i = 0; !policyStatus.dwError && i < cSigner; i++)
995 CERT_CHAIN_POLICY_PARA policyPara = { sizeof(policyPara), 0 };
997 if (dwRegPolicySettings & WTPF_IGNOREEXPIRATION)
998 policyPara.dwFlags |=
999 CERT_CHAIN_POLICY_IGNORE_NOT_TIME_VALID_FLAG |
1000 CERT_CHAIN_POLICY_IGNORE_CTL_NOT_TIME_VALID_FLAG |
1001 CERT_CHAIN_POLICY_IGNORE_NOT_TIME_NESTED_FLAG;
1002 if (dwRegPolicySettings & WTPF_IGNOREREVOKATION)
1003 policyPara.dwFlags |=
1004 CERT_CHAIN_POLICY_IGNORE_END_REV_UNKNOWN_FLAG |
1005 CERT_CHAIN_POLICY_IGNORE_CTL_SIGNER_REV_UNKNOWN_FLAG |
1006 CERT_CHAIN_POLICY_IGNORE_CA_REV_UNKNOWN_FLAG |
1007 CERT_CHAIN_POLICY_IGNORE_ROOT_REV_UNKNOWN_FLAG;
1008 CertVerifyCertificateChainPolicy(CERT_CHAIN_POLICY_BASE,
1009 rgpSigner[i].pChainContext, &policyPara, &policyStatus);
1011 return policyStatus.dwError;
1014 HRESULT WINAPI GenericChainFinalProv(CRYPT_PROVIDER_DATA *data)
1016 HRESULT err = NO_ERROR; /* not a typo, MS confused the types */
1017 WTD_GENERIC_CHAIN_POLICY_DATA *policyData =
1018 data->pWintrustData->pPolicyCallbackData;
1020 TRACE("(%p)\n", data);
1022 if (data->pWintrustData->dwUIChoice != WTD_UI_NONE)
1023 FIXME("unimplemented for UI choice %d\n",
1024 data->pWintrustData->dwUIChoice);
1025 if (!data->csSigners)
1026 err = TRUST_E_NOSIGNATURE;
1027 else
1029 PFN_WTD_GENERIC_CHAIN_POLICY_CALLBACK policyCallback;
1030 void *policyArg;
1031 WTD_GENERIC_CHAIN_POLICY_SIGNER_INFO *signers = NULL;
1033 if (policyData)
1035 policyCallback = policyData->pfnPolicyCallback;
1036 policyArg = policyData->pvPolicyArg;
1038 else
1040 policyCallback = WINTRUST_DefaultPolicy;
1041 policyArg = NULL;
1043 if (data->csSigners)
1045 DWORD i;
1047 signers = data->psPfns->pfnAlloc(
1048 data->csSigners * sizeof(WTD_GENERIC_CHAIN_POLICY_SIGNER_INFO));
1049 if (signers)
1051 for (i = 0; i < data->csSigners; i++)
1053 signers[i].u.cbSize =
1054 sizeof(WTD_GENERIC_CHAIN_POLICY_SIGNER_INFO);
1055 signers[i].pChainContext =
1056 data->pasSigners[i].pChainContext;
1057 signers[i].dwSignerType = data->pasSigners[i].dwSignerType;
1058 signers[i].pMsgSignerInfo = data->pasSigners[i].psSigner;
1059 signers[i].dwError = data->pasSigners[i].dwError;
1060 if (data->pasSigners[i].csCounterSigners)
1061 FIXME("unimplemented for counter signers\n");
1062 signers[i].cCounterSigner = 0;
1063 signers[i].rgpCounterSigner = NULL;
1066 else
1067 err = ERROR_OUTOFMEMORY;
1069 if (err == NO_ERROR)
1070 err = policyCallback(data, TRUSTERROR_STEP_FINAL_POLICYPROV,
1071 data->dwRegPolicySettings, data->csSigners, signers, policyArg);
1072 data->psPfns->pfnFree(signers);
1074 if (err != NO_ERROR)
1075 data->padwTrustStepErrors[TRUSTERROR_STEP_FINAL_POLICYPROV] = err;
1076 TRACE("returning %d (%08x)\n", err == NO_ERROR ? S_OK : S_FALSE,
1077 data->padwTrustStepErrors[TRUSTERROR_STEP_FINAL_POLICYPROV]);
1078 return err == NO_ERROR ? S_OK : S_FALSE;
1081 HRESULT WINAPI SoftpubCleanup(CRYPT_PROVIDER_DATA *data)
1083 DWORD i, j;
1085 for (i = 0; i < data->csSigners; i++)
1087 for (j = 0; j < data->pasSigners[i].csCertChain; j++)
1088 CertFreeCertificateContext(data->pasSigners[i].pasCertChain[j].pCert);
1089 data->psPfns->pfnFree(data->pasSigners[i].pasCertChain);
1090 data->psPfns->pfnFree(data->pasSigners[i].psSigner);
1091 CertFreeCertificateChain(data->pasSigners[i].pChainContext);
1093 data->psPfns->pfnFree(data->pasSigners);
1095 for (i = 0; i < data->chStores; i++)
1096 CertCloseStore(data->pahStores[i], 0);
1097 data->psPfns->pfnFree(data->pahStores);
1099 if (data->u.pPDSip)
1101 data->psPfns->pfnFree(data->u.pPDSip->pSip);
1102 data->psPfns->pfnFree(data->u.pPDSip->pCATSip);
1103 data->psPfns->pfnFree(data->u.pPDSip->psSipSubjectInfo);
1104 data->psPfns->pfnFree(data->u.pPDSip->psSipCATSubjectInfo);
1105 data->psPfns->pfnFree(data->u.pPDSip->psIndirectData);
1108 CryptMsgClose(data->hMsg);
1110 if (data->fOpenedFile &&
1111 data->pWintrustData->dwUnionChoice == WTD_CHOICE_FILE)
1112 CloseHandle(data->pWintrustData->u.pFile->hFile);
1114 return S_OK;
1117 HRESULT WINAPI HTTPSCertificateTrust(CRYPT_PROVIDER_DATA *data)
1119 FIXME("(%p)\n", data);
1120 return S_OK;
1123 HRESULT WINAPI HTTPSFinalProv(CRYPT_PROVIDER_DATA *data)
1125 FIXME("(%p)\n", data);
1126 return S_OK;