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
20 #define NONAMELESSUNION
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])
40 TRACE("returning %08x\n", 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])
53 TRACE("returning %08x\n", ret);
57 HRESULT WINAPI DriverInitializePolicy(CRYPT_PROVIDER_DATA *data)
63 HRESULT WINAPI DriverCleanupPolicy(CRYPT_PROVIDER_DATA *data)
69 HRESULT WINAPI DriverFinalPolicy(CRYPT_PROVIDER_DATA *data)
75 /* Assumes data->pWintrustData->u.pFile exists. Makes sure a file handle is
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
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;
98 GetFileTime(data->pWintrustData->u.pFile->hFile, &data->sftSystemTime,
100 TRACE("returning %d\n", 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();
122 data->u.pPDSip->gSubject = *data->pWintrustData->u.pFile->pgKnownSubject;
123 TRACE("returning %d\n", 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 BOOL SOFTPUB_GetSIP(CRYPT_PROVIDER_DATA *data)
134 data->u.pPDSip->pSip = data->psPfns->pfnAlloc(sizeof(SIP_DISPATCH_INFO));
135 if (data->u.pPDSip->pSip)
136 ret = CryptSIPLoad(&data->u.pPDSip->gSubject, 0, data->u.pPDSip->pSip);
139 SetLastError(ERROR_OUTOFMEMORY);
142 TRACE("returning %d\n", ret);
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 BOOL SOFTPUB_GetMessageFromFile(CRYPT_PROVIDER_DATA *data, HANDLE file,
156 data->u.pPDSip->psSipSubjectInfo =
157 data->psPfns->pfnAlloc(sizeof(SIP_SUBJECTINFO));
158 if (!data->u.pPDSip->psSipSubjectInfo)
160 SetLastError(ERROR_OUTOFMEMORY);
164 data->u.pPDSip->psSipSubjectInfo->cbSize = sizeof(SIP_SUBJECTINFO);
165 data->u.pPDSip->psSipSubjectInfo->pgSubjectType = &data->u.pPDSip->gSubject;
166 data->u.pPDSip->psSipSubjectInfo->hFile = file;
167 data->u.pPDSip->psSipSubjectInfo->pwsFileName = filePath;
168 data->u.pPDSip->psSipSubjectInfo->hProv = data->hProv;
169 ret = data->u.pPDSip->pSip->pfGet(data->u.pPDSip->psSipSubjectInfo,
170 &data->dwEncoding, 0, &size, 0);
173 SetLastError(TRUST_E_NOSIGNATURE);
177 buf = data->psPfns->pfnAlloc(size);
180 SetLastError(ERROR_OUTOFMEMORY);
184 ret = data->u.pPDSip->pSip->pfGet(data->u.pPDSip->psSipSubjectInfo,
185 &data->dwEncoding, 0, &size, buf);
188 data->hMsg = CryptMsgOpenToDecode(data->dwEncoding, 0, 0, data->hProv,
191 ret = CryptMsgUpdate(data->hMsg, buf, size, TRUE);
194 data->psPfns->pfnFree(buf);
195 TRACE("returning %d\n", ret);
199 static BOOL SOFTPUB_CreateStoreFromMessage(CRYPT_PROVIDER_DATA *data)
204 store = CertOpenStore(CERT_STORE_PROV_MSG, data->dwEncoding,
205 data->hProv, CERT_STORE_NO_CRYPT_RELEASE_FLAG, data->hMsg);
208 ret = data->psPfns->pfnAddStore2Chain(data, store);
209 CertCloseStore(store, 0);
211 TRACE("returning %d\n", ret);
215 static DWORD SOFTPUB_DecodeInnerContent(CRYPT_PROVIDER_DATA *data)
222 ret = CryptMsgGetParam(data->hMsg, CMSG_INNER_CONTENT_TYPE_PARAM, 0, NULL,
226 oid = data->psPfns->pfnAlloc(size);
229 SetLastError(ERROR_OUTOFMEMORY);
233 ret = CryptMsgGetParam(data->hMsg, CMSG_INNER_CONTENT_TYPE_PARAM, 0, oid,
237 ret = CryptMsgGetParam(data->hMsg, CMSG_CONTENT_PARAM, 0, NULL, &size);
240 buf = data->psPfns->pfnAlloc(size);
243 SetLastError(ERROR_OUTOFMEMORY);
247 ret = CryptMsgGetParam(data->hMsg, CMSG_CONTENT_PARAM, 0, buf, &size);
250 ret = CryptDecodeObject(data->dwEncoding, oid, buf, size, 0, NULL, &size);
253 data->u.pPDSip->psIndirectData = data->psPfns->pfnAlloc(size);
254 if (!data->u.pPDSip->psIndirectData)
256 SetLastError(ERROR_OUTOFMEMORY);
260 ret = CryptDecodeObject(data->dwEncoding, oid, buf, size, 0,
261 data->u.pPDSip->psIndirectData, &size);
264 TRACE("returning %d\n", ret);
265 data->psPfns->pfnFree(oid);
266 data->psPfns->pfnFree(buf);
270 static BOOL SOFTPUB_LoadCertMessage(CRYPT_PROVIDER_DATA *data)
274 if (data->pWintrustData->u.pCert &&
275 WVT_IS_CBSTRUCT_GT_MEMBEROFFSET(WINTRUST_CERT_INFO,
276 data->pWintrustData->u.pCert->cbStruct, psCertContext))
280 CRYPT_PROVIDER_SGNR signer = { sizeof(signer), { 0 } };
283 /* Add a signer with nothing but the time to verify, so we can
286 if (WVT_ISINSTRUCT(WINTRUST_CERT_INFO,
287 data->pWintrustData->u.pCert->cbStruct, psftVerifyAsOf) &&
288 data->pWintrustData->u.pCert->psftVerifyAsOf)
289 data->sftSystemTime = signer.sftVerifyAsOf;
294 GetSystemTime(&sysTime);
295 SystemTimeToFileTime(&sysTime, &signer.sftVerifyAsOf);
297 ret = data->psPfns->pfnAddSgnr2Chain(data, FALSE, 0, &signer);
300 ret = data->psPfns->pfnAddCert2Chain(data, 0, FALSE, 0,
301 data->pWintrustData->u.pCert->psCertContext);
302 if (WVT_ISINSTRUCT(WINTRUST_CERT_INFO,
303 data->pWintrustData->u.pCert->cbStruct, pahStores))
305 ret && i < data->pWintrustData->u.pCert->chStores; i++)
306 ret = data->psPfns->pfnAddStore2Chain(data,
307 data->pWintrustData->u.pCert->pahStores[i]);
312 /* Do nothing!? See the tests */
318 SetLastError(ERROR_INVALID_PARAMETER);
324 static DWORD SOFTPUB_LoadFileMessage(CRYPT_PROVIDER_DATA *data)
326 DWORD err = ERROR_SUCCESS;
328 if (!data->pWintrustData->u.pFile)
330 err = ERROR_INVALID_PARAMETER;
333 err = SOFTPUB_OpenFile(data);
336 err = SOFTPUB_GetFileSubject(data);
339 if (!SOFTPUB_GetSIP(data))
341 err = GetLastError();
344 if (!SOFTPUB_GetMessageFromFile(data, data->pWintrustData->u.pFile->hFile,
345 data->pWintrustData->u.pFile->pcwszFilePath))
347 err = GetLastError();
350 if (!SOFTPUB_CreateStoreFromMessage(data))
352 err = GetLastError();
355 if (!SOFTPUB_DecodeInnerContent(data))
356 err = GetLastError();
361 static DWORD SOFTPUB_LoadCatalogMessage(CRYPT_PROVIDER_DATA *data)
364 HANDLE catalog = INVALID_HANDLE_VALUE;
366 if (!data->pWintrustData->u.pCatalog)
368 SetLastError(ERROR_INVALID_PARAMETER);
371 catalog = CreateFileW(data->pWintrustData->u.pCatalog->pcwszCatalogFilePath,
372 GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL,
374 if (catalog == INVALID_HANDLE_VALUE)
375 return GetLastError();
376 if (!CryptSIPRetrieveSubjectGuid(
377 data->pWintrustData->u.pCatalog->pcwszCatalogFilePath, catalog,
378 &data->u.pPDSip->gSubject))
380 err = GetLastError();
383 if (!SOFTPUB_GetSIP(data))
385 err = GetLastError();
388 if (!SOFTPUB_GetMessageFromFile(data, catalog,
389 data->pWintrustData->u.pCatalog->pcwszCatalogFilePath))
391 err = GetLastError();
394 if (!SOFTPUB_CreateStoreFromMessage(data))
396 err = GetLastError();
399 if (!SOFTPUB_DecodeInnerContent(data))
400 err = GetLastError();
401 /* FIXME: this loads the catalog file, but doesn't validate the member. */
403 CloseHandle(catalog);
407 HRESULT WINAPI SoftpubLoadMessage(CRYPT_PROVIDER_DATA *data)
410 DWORD err = ERROR_SUCCESS;
412 TRACE("(%p)\n", data);
414 if (!data->padwTrustStepErrors)
417 switch (data->pWintrustData->dwUnionChoice)
419 case WTD_CHOICE_CERT:
420 ret = SOFTPUB_LoadCertMessage(data);
422 case WTD_CHOICE_FILE:
423 err = SOFTPUB_LoadFileMessage(data);
425 case WTD_CHOICE_CATALOG:
426 err = SOFTPUB_LoadCatalogMessage(data);
429 FIXME("unimplemented for %d\n", data->pWintrustData->dwUnionChoice);
430 SetLastError(ERROR_INVALID_PARAMETER);
435 data->padwTrustStepErrors[TRUSTERROR_STEP_FINAL_OBJPROV] =
439 data->padwTrustStepErrors[TRUSTERROR_STEP_FINAL_OBJPROV] = err;
442 TRACE("returning %d (%08x)\n", ret ? S_OK : S_FALSE,
443 data->padwTrustStepErrors[TRUSTERROR_STEP_FINAL_OBJPROV]);
444 return ret ? S_OK : S_FALSE;
447 static CMSG_SIGNER_INFO *WINTRUST_GetSigner(CRYPT_PROVIDER_DATA *data,
451 CMSG_SIGNER_INFO *signerInfo = NULL;
454 ret = CryptMsgGetParam(data->hMsg, CMSG_SIGNER_INFO_PARAM, signerIdx,
458 signerInfo = data->psPfns->pfnAlloc(size);
461 ret = CryptMsgGetParam(data->hMsg, CMSG_SIGNER_INFO_PARAM,
462 signerIdx, signerInfo, &size);
465 data->psPfns->pfnFree(signerInfo);
470 SetLastError(ERROR_OUTOFMEMORY);
475 static BOOL WINTRUST_SaveSigner(CRYPT_PROVIDER_DATA *data, DWORD signerIdx)
478 CMSG_SIGNER_INFO *signerInfo = WINTRUST_GetSigner(data, signerIdx);
482 CRYPT_PROVIDER_SGNR sgnr = { sizeof(sgnr), { 0 } };
484 sgnr.psSigner = signerInfo;
485 sgnr.sftVerifyAsOf = data->sftSystemTime;
486 ret = data->psPfns->pfnAddSgnr2Chain(data, FALSE, signerIdx, &sgnr);
493 static CERT_INFO *WINTRUST_GetSignerCertInfo(CRYPT_PROVIDER_DATA *data,
497 CERT_INFO *certInfo = NULL;
500 ret = CryptMsgGetParam(data->hMsg, CMSG_SIGNER_CERT_INFO_PARAM, signerIdx,
504 certInfo = data->psPfns->pfnAlloc(size);
507 ret = CryptMsgGetParam(data->hMsg, CMSG_SIGNER_CERT_INFO_PARAM,
508 signerIdx, certInfo, &size);
511 data->psPfns->pfnFree(certInfo);
516 SetLastError(ERROR_OUTOFMEMORY);
521 static BOOL WINTRUST_VerifySigner(CRYPT_PROVIDER_DATA *data, DWORD signerIdx)
524 CERT_INFO *certInfo = WINTRUST_GetSignerCertInfo(data, signerIdx);
528 PCCERT_CONTEXT subject = CertGetSubjectCertificateFromStore(
529 data->pahStores[0], data->dwEncoding, certInfo);
533 CMSG_CTRL_VERIFY_SIGNATURE_EX_PARA para = { sizeof(para), 0,
534 signerIdx, CMSG_VERIFY_SIGNER_CERT, (LPVOID)subject };
536 ret = CryptMsgControl(data->hMsg, 0, CMSG_CTRL_VERIFY_SIGNATURE_EX,
539 SetLastError(TRUST_E_CERT_SIGNATURE);
541 data->psPfns->pfnAddCert2Chain(data, signerIdx, FALSE, 0,
543 CertFreeCertificateContext(subject);
547 SetLastError(TRUST_E_NO_SIGNER_CERT);
550 data->psPfns->pfnFree(certInfo);
557 HRESULT WINAPI SoftpubLoadSignature(CRYPT_PROVIDER_DATA *data)
561 TRACE("(%p)\n", data);
563 if (!data->padwTrustStepErrors)
568 DWORD signerCount, size;
570 size = sizeof(signerCount);
571 ret = CryptMsgGetParam(data->hMsg, CMSG_SIGNER_COUNT_PARAM, 0,
572 &signerCount, &size);
577 for (i = 0; ret && i < signerCount; i++)
579 if ((ret = WINTRUST_SaveSigner(data, i)))
580 ret = WINTRUST_VerifySigner(data, i);
584 SetLastError(TRUST_E_NOSIGNATURE);
589 data->padwTrustStepErrors[TRUSTERROR_STEP_FINAL_SIGPROV] =
591 return ret ? S_OK : S_FALSE;
594 static DWORD WINTRUST_TrustStatusToConfidence(DWORD errorStatus)
596 DWORD confidence = 0;
599 if (!(errorStatus & CERT_TRUST_IS_NOT_SIGNATURE_VALID))
600 confidence |= CERT_CONFIDENCE_SIG;
601 if (!(errorStatus & CERT_TRUST_IS_NOT_TIME_VALID))
602 confidence |= CERT_CONFIDENCE_TIME;
603 if (!(errorStatus & CERT_TRUST_IS_NOT_TIME_NESTED))
604 confidence |= CERT_CONFIDENCE_TIMENEST;
608 BOOL WINAPI SoftpubCheckCert(CRYPT_PROVIDER_DATA *data, DWORD idxSigner,
609 BOOL fCounterSignerChain, DWORD idxCounterSigner)
613 TRACE("(%p, %d, %d, %d)\n", data, idxSigner, fCounterSignerChain,
616 if (fCounterSignerChain)
618 FIXME("unimplemented for counter signers\n");
623 PCERT_SIMPLE_CHAIN simpleChain =
624 data->pasSigners[idxSigner].pChainContext->rgpChain[0];
628 for (i = 0; i < simpleChain->cElement; i++)
631 data->pasSigners[idxSigner].pasCertChain[i].dwConfidence =
632 WINTRUST_TrustStatusToConfidence(
633 simpleChain->rgpElement[i]->TrustStatus.dwErrorStatus);
634 /* Set additional flags */
635 if (!(simpleChain->rgpElement[i]->TrustStatus.dwErrorStatus &
636 CERT_TRUST_IS_UNTRUSTED_ROOT))
637 data->pasSigners[idxSigner].pasCertChain[i].fTrustedRoot = TRUE;
638 if (simpleChain->rgpElement[i]->TrustStatus.dwInfoStatus &
639 CERT_TRUST_IS_SELF_SIGNED)
640 data->pasSigners[idxSigner].pasCertChain[i].fSelfSigned = TRUE;
641 if (simpleChain->rgpElement[i]->TrustStatus.dwErrorStatus &
642 CERT_TRUST_IS_CYCLIC)
643 data->pasSigners[idxSigner].pasCertChain[i].fIsCyclic = TRUE;
649 static DWORD WINTRUST_TrustStatusToError(DWORD errorStatus)
653 if (errorStatus & CERT_TRUST_IS_NOT_SIGNATURE_VALID)
654 error = TRUST_E_CERT_SIGNATURE;
655 else if (errorStatus & CERT_TRUST_IS_UNTRUSTED_ROOT)
656 error = CERT_E_UNTRUSTEDROOT;
657 else if (errorStatus & CERT_TRUST_IS_NOT_TIME_VALID)
658 error = CERT_E_EXPIRED;
659 else if (errorStatus & CERT_TRUST_IS_NOT_TIME_NESTED)
660 error = CERT_E_VALIDITYPERIODNESTING;
661 else if (errorStatus & CERT_TRUST_IS_REVOKED)
662 error = CERT_E_REVOKED;
663 else if (errorStatus & CERT_TRUST_IS_OFFLINE_REVOCATION ||
664 errorStatus & CERT_TRUST_REVOCATION_STATUS_UNKNOWN)
665 error = CERT_E_REVOCATION_FAILURE;
666 else if (errorStatus & CERT_TRUST_IS_NOT_VALID_FOR_USAGE)
667 error = CERT_E_WRONG_USAGE;
668 else if (errorStatus & CERT_TRUST_IS_CYCLIC)
669 error = CERT_E_CHAINING;
670 else if (errorStatus & CERT_TRUST_INVALID_EXTENSION)
671 error = CERT_E_CRITICAL;
672 else if (errorStatus & CERT_TRUST_INVALID_POLICY_CONSTRAINTS)
673 error = CERT_E_INVALID_POLICY;
674 else if (errorStatus & CERT_TRUST_INVALID_BASIC_CONSTRAINTS)
675 error = TRUST_E_BASIC_CONSTRAINTS;
676 else if (errorStatus & CERT_TRUST_INVALID_NAME_CONSTRAINTS ||
677 errorStatus & CERT_TRUST_HAS_NOT_SUPPORTED_NAME_CONSTRAINT ||
678 errorStatus & CERT_TRUST_HAS_NOT_DEFINED_NAME_CONSTRAINT ||
679 errorStatus & CERT_TRUST_HAS_NOT_PERMITTED_NAME_CONSTRAINT ||
680 errorStatus & CERT_TRUST_HAS_EXCLUDED_NAME_CONSTRAINT)
681 error = CERT_E_INVALID_NAME;
682 else if (errorStatus & CERT_TRUST_NO_ISSUANCE_CHAIN_POLICY)
683 error = CERT_E_INVALID_POLICY;
684 else if (errorStatus)
686 FIXME("unknown error status %08x\n", errorStatus);
687 error = TRUST_E_SYSTEM_ERROR;
694 static BOOL WINTRUST_CopyChain(CRYPT_PROVIDER_DATA *data, DWORD signerIdx)
697 PCERT_SIMPLE_CHAIN simpleChain =
698 data->pasSigners[signerIdx].pChainContext->rgpChain[0];
701 data->pasSigners[signerIdx].pasCertChain[0].dwConfidence =
702 WINTRUST_TrustStatusToConfidence(
703 simpleChain->rgpElement[0]->TrustStatus.dwErrorStatus);
704 data->pasSigners[signerIdx].pasCertChain[0].pChainElement =
705 simpleChain->rgpElement[0];
707 for (i = 1; ret && i < simpleChain->cElement; i++)
709 ret = data->psPfns->pfnAddCert2Chain(data, signerIdx, FALSE, 0,
710 simpleChain->rgpElement[i]->pCertContext);
713 data->pasSigners[signerIdx].pasCertChain[i].pChainElement =
714 simpleChain->rgpElement[i];
715 data->pasSigners[signerIdx].pasCertChain[i].dwConfidence =
716 WINTRUST_TrustStatusToConfidence(
717 simpleChain->rgpElement[i]->TrustStatus.dwErrorStatus);
720 data->pasSigners[signerIdx].pasCertChain[simpleChain->cElement - 1].dwError
721 = WINTRUST_TrustStatusToError(
722 simpleChain->rgpElement[simpleChain->cElement - 1]->
723 TrustStatus.dwErrorStatus);
727 static void WINTRUST_CreateChainPolicyCreateInfo(
728 const CRYPT_PROVIDER_DATA *data, PWTD_GENERIC_CHAIN_POLICY_CREATE_INFO info,
729 PCERT_CHAIN_PARA chainPara)
731 chainPara->cbSize = sizeof(CERT_CHAIN_PARA);
732 if (data->pRequestUsage)
733 chainPara->RequestedUsage = *data->pRequestUsage;
736 chainPara->RequestedUsage.dwType = 0;
737 chainPara->RequestedUsage.Usage.cUsageIdentifier = 0;
739 info->u.cbSize = sizeof(WTD_GENERIC_CHAIN_POLICY_CREATE_INFO);
740 info->hChainEngine = NULL;
741 info->pChainPara = chainPara;
742 if (data->dwProvFlags & CPD_REVOCATION_CHECK_END_CERT)
743 info->dwFlags = CERT_CHAIN_REVOCATION_CHECK_END_CERT;
744 else if (data->dwProvFlags & CPD_REVOCATION_CHECK_CHAIN)
745 info->dwFlags = CERT_CHAIN_REVOCATION_CHECK_CHAIN;
746 else if (data->dwProvFlags & CPD_REVOCATION_CHECK_CHAIN_EXCLUDE_ROOT)
747 info->dwFlags = CERT_CHAIN_REVOCATION_CHECK_CHAIN_EXCLUDE_ROOT;
750 info->pvReserved = NULL;
753 static BOOL WINTRUST_CreateChainForSigner(CRYPT_PROVIDER_DATA *data,
754 DWORD signer, PWTD_GENERIC_CHAIN_POLICY_CREATE_INFO createInfo,
755 PCERT_CHAIN_PARA chainPara)
758 HCERTSTORE store = NULL;
762 store = CertOpenStore(CERT_STORE_PROV_COLLECTION, 0, 0,
763 CERT_STORE_CREATE_NEW_FLAG, NULL);
768 for (i = 0; i < data->chStores; i++)
769 CertAddStoreToCollection(store, data->pahStores[i], 0, 0);
772 /* Expect the end certificate for each signer to be the only cert in the
775 if (data->pasSigners[signer].csCertChain)
777 /* Create a certificate chain for each signer */
778 ret = CertGetCertificateChain(createInfo->hChainEngine,
779 data->pasSigners[signer].pasCertChain[0].pCert,
780 &data->pasSigners[signer].sftVerifyAsOf, store,
781 chainPara, createInfo->dwFlags, createInfo->pvReserved,
782 &data->pasSigners[signer].pChainContext);
785 if (data->pasSigners[signer].pChainContext->cChain != 1)
787 FIXME("unimplemented for more than 1 simple chain\n");
792 if ((ret = WINTRUST_CopyChain(data, signer)))
794 if (data->psPfns->pfnCertCheckPolicy)
795 ret = data->psPfns->pfnCertCheckPolicy(data, signer,
798 TRACE("no cert check policy, skipping policy check\n");
803 CertCloseStore(store, 0);
807 HRESULT WINAPI WintrustCertificateTrust(CRYPT_PROVIDER_DATA *data)
811 TRACE("(%p)\n", data);
813 if (!data->csSigners)
816 SetLastError(TRUST_E_NOSIGNATURE);
821 WTD_GENERIC_CHAIN_POLICY_CREATE_INFO createInfo;
822 CERT_CHAIN_PARA chainPara;
824 WINTRUST_CreateChainPolicyCreateInfo(data, &createInfo, &chainPara);
826 for (i = 0; i < data->csSigners; i++)
827 ret = WINTRUST_CreateChainForSigner(data, i, &createInfo,
831 data->padwTrustStepErrors[TRUSTERROR_STEP_FINAL_CERTPROV] =
833 TRACE("returning %d (%08x)\n", ret ? S_OK : S_FALSE,
834 data->padwTrustStepErrors[TRUSTERROR_STEP_FINAL_CERTPROV]);
835 return ret ? S_OK : S_FALSE;
838 HRESULT WINAPI GenericChainCertificateTrust(CRYPT_PROVIDER_DATA *data)
841 WTD_GENERIC_CHAIN_POLICY_DATA *policyData =
842 data->pWintrustData->pPolicyCallbackData;
844 TRACE("(%p)\n", data);
846 if (policyData && policyData->u.cbSize !=
847 sizeof(WTD_GENERIC_CHAIN_POLICY_CREATE_INFO))
849 SetLastError(ERROR_INVALID_PARAMETER);
853 if (!data->csSigners)
856 SetLastError(TRUST_E_NOSIGNATURE);
861 WTD_GENERIC_CHAIN_POLICY_CREATE_INFO createInfo, *pCreateInfo;
862 CERT_CHAIN_PARA chainPara, *pChainPara;
866 pCreateInfo = policyData->pSignerChainInfo;
867 pChainPara = pCreateInfo->pChainPara;
871 WINTRUST_CreateChainPolicyCreateInfo(data, &createInfo, &chainPara);
872 pChainPara = &chainPara;
873 pCreateInfo = &createInfo;
876 for (i = 0; i < data->csSigners; i++)
877 ret = WINTRUST_CreateChainForSigner(data, i, pCreateInfo,
883 data->padwTrustStepErrors[TRUSTERROR_STEP_FINAL_CERTPROV] =
885 TRACE("returning %d (%08x)\n", ret ? S_OK : S_FALSE,
886 data->padwTrustStepErrors[TRUSTERROR_STEP_FINAL_CERTPROV]);
887 return ret ? S_OK : S_FALSE;
890 HRESULT WINAPI SoftpubAuthenticode(CRYPT_PROVIDER_DATA *data)
893 CERT_CHAIN_POLICY_STATUS policyStatus = { sizeof(policyStatus), 0 };
895 TRACE("(%p)\n", data);
897 if (data->pWintrustData->dwUIChoice != WTD_UI_NONE)
898 FIXME("unimplemented for UI choice %d\n",
899 data->pWintrustData->dwUIChoice);
900 if (!data->csSigners)
903 policyStatus.dwError = TRUST_E_NOSIGNATURE;
910 for (i = 0; ret && i < data->csSigners; i++)
913 DWORD size = sizeof(hash);
915 /* First make sure cert isn't disallowed */
916 if ((ret = CertGetCertificateContextProperty(
917 data->pasSigners[i].pasCertChain[0].pCert,
918 CERT_SIGNATURE_HASH_PROP_ID, hash, &size)))
920 static const WCHAR disallowedW[] =
921 { 'D','i','s','a','l','l','o','w','e','d',0 };
922 HCERTSTORE disallowed = CertOpenStore(CERT_STORE_PROV_SYSTEM_W,
923 X509_ASN_ENCODING, 0, CERT_SYSTEM_STORE_CURRENT_USER,
928 PCCERT_CONTEXT found = CertFindCertificateInStore(
929 disallowed, X509_ASN_ENCODING, 0, CERT_FIND_SIGNATURE_HASH,
934 /* Disallowed! Can't verify it. */
935 policyStatus.dwError = TRUST_E_SUBJECT_NOT_TRUSTED;
937 CertFreeCertificateContext(found);
939 CertCloseStore(disallowed, 0);
944 CERT_CHAIN_POLICY_PARA policyPara = { sizeof(policyPara), 0 };
946 if (data->dwRegPolicySettings & WTPF_TRUSTTEST)
947 policyPara.dwFlags |= CERT_CHAIN_POLICY_TRUST_TESTROOT_FLAG;
948 if (data->dwRegPolicySettings & WTPF_TESTCANBEVALID)
949 policyPara.dwFlags |= CERT_CHAIN_POLICY_ALLOW_TESTROOT_FLAG;
950 if (data->dwRegPolicySettings & WTPF_IGNOREEXPIRATION)
951 policyPara.dwFlags |=
952 CERT_CHAIN_POLICY_IGNORE_NOT_TIME_VALID_FLAG |
953 CERT_CHAIN_POLICY_IGNORE_CTL_NOT_TIME_VALID_FLAG |
954 CERT_CHAIN_POLICY_IGNORE_NOT_TIME_NESTED_FLAG;
955 if (data->dwRegPolicySettings & WTPF_IGNOREREVOKATION)
956 policyPara.dwFlags |=
957 CERT_CHAIN_POLICY_IGNORE_END_REV_UNKNOWN_FLAG |
958 CERT_CHAIN_POLICY_IGNORE_CTL_SIGNER_REV_UNKNOWN_FLAG |
959 CERT_CHAIN_POLICY_IGNORE_CA_REV_UNKNOWN_FLAG |
960 CERT_CHAIN_POLICY_IGNORE_ROOT_REV_UNKNOWN_FLAG;
961 CertVerifyCertificateChainPolicy(CERT_CHAIN_POLICY_AUTHENTICODE,
962 data->pasSigners[i].pChainContext, &policyPara, &policyStatus);
963 if (policyStatus.dwError != NO_ERROR)
969 data->padwTrustStepErrors[TRUSTERROR_STEP_FINAL_POLICYPROV] =
970 policyStatus.dwError;
971 TRACE("returning %d (%08x)\n", ret ? S_OK : S_FALSE,
972 data->padwTrustStepErrors[TRUSTERROR_STEP_FINAL_POLICYPROV]);
973 return ret ? S_OK : S_FALSE;
976 static HRESULT WINAPI WINTRUST_DefaultPolicy(CRYPT_PROVIDER_DATA *pProvData,
977 DWORD dwStepError, DWORD dwRegPolicySettings, DWORD cSigner,
978 PWTD_GENERIC_CHAIN_POLICY_SIGNER_INFO rgpSigner, void *pvPolicyArg)
981 CERT_CHAIN_POLICY_STATUS policyStatus = { sizeof(policyStatus), 0 };
983 for (i = 0; !policyStatus.dwError && i < cSigner; i++)
985 CERT_CHAIN_POLICY_PARA policyPara = { sizeof(policyPara), 0 };
987 if (dwRegPolicySettings & WTPF_IGNOREEXPIRATION)
988 policyPara.dwFlags |=
989 CERT_CHAIN_POLICY_IGNORE_NOT_TIME_VALID_FLAG |
990 CERT_CHAIN_POLICY_IGNORE_CTL_NOT_TIME_VALID_FLAG |
991 CERT_CHAIN_POLICY_IGNORE_NOT_TIME_NESTED_FLAG;
992 if (dwRegPolicySettings & WTPF_IGNOREREVOKATION)
993 policyPara.dwFlags |=
994 CERT_CHAIN_POLICY_IGNORE_END_REV_UNKNOWN_FLAG |
995 CERT_CHAIN_POLICY_IGNORE_CTL_SIGNER_REV_UNKNOWN_FLAG |
996 CERT_CHAIN_POLICY_IGNORE_CA_REV_UNKNOWN_FLAG |
997 CERT_CHAIN_POLICY_IGNORE_ROOT_REV_UNKNOWN_FLAG;
998 CertVerifyCertificateChainPolicy(CERT_CHAIN_POLICY_BASE,
999 rgpSigner[i].pChainContext, &policyPara, &policyStatus);
1001 return policyStatus.dwError;
1004 HRESULT WINAPI GenericChainFinalProv(CRYPT_PROVIDER_DATA *data)
1006 HRESULT err = NO_ERROR; /* not a typo, MS confused the types */
1007 WTD_GENERIC_CHAIN_POLICY_DATA *policyData =
1008 data->pWintrustData->pPolicyCallbackData;
1010 TRACE("(%p)\n", data);
1012 if (data->pWintrustData->dwUIChoice != WTD_UI_NONE)
1013 FIXME("unimplemented for UI choice %d\n",
1014 data->pWintrustData->dwUIChoice);
1015 if (!data->csSigners)
1016 err = TRUST_E_NOSIGNATURE;
1019 PFN_WTD_GENERIC_CHAIN_POLICY_CALLBACK policyCallback;
1021 WTD_GENERIC_CHAIN_POLICY_SIGNER_INFO *signers = NULL;
1025 policyCallback = policyData->pfnPolicyCallback;
1026 policyArg = policyData->pvPolicyArg;
1030 policyCallback = WINTRUST_DefaultPolicy;
1033 if (data->csSigners)
1037 signers = data->psPfns->pfnAlloc(
1038 data->csSigners * sizeof(WTD_GENERIC_CHAIN_POLICY_SIGNER_INFO));
1041 for (i = 0; i < data->csSigners; i++)
1043 signers[i].u.cbSize =
1044 sizeof(WTD_GENERIC_CHAIN_POLICY_SIGNER_INFO);
1045 signers[i].pChainContext =
1046 data->pasSigners[i].pChainContext;
1047 signers[i].dwSignerType = data->pasSigners[i].dwSignerType;
1048 signers[i].pMsgSignerInfo = data->pasSigners[i].psSigner;
1049 signers[i].dwError = data->pasSigners[i].dwError;
1050 if (data->pasSigners[i].csCounterSigners)
1051 FIXME("unimplemented for counter signers\n");
1052 signers[i].cCounterSigner = 0;
1053 signers[i].rgpCounterSigner = NULL;
1057 err = ERROR_OUTOFMEMORY;
1059 if (err == NO_ERROR)
1060 err = policyCallback(data, TRUSTERROR_STEP_FINAL_POLICYPROV,
1061 data->dwRegPolicySettings, data->csSigners, signers, policyArg);
1062 data->psPfns->pfnFree(signers);
1064 if (err != NO_ERROR)
1065 data->padwTrustStepErrors[TRUSTERROR_STEP_FINAL_POLICYPROV] = err;
1066 TRACE("returning %d (%08x)\n", err == NO_ERROR ? S_OK : S_FALSE,
1067 data->padwTrustStepErrors[TRUSTERROR_STEP_FINAL_POLICYPROV]);
1068 return err == NO_ERROR ? S_OK : S_FALSE;
1071 HRESULT WINAPI SoftpubCleanup(CRYPT_PROVIDER_DATA *data)
1075 for (i = 0; i < data->csSigners; i++)
1077 for (j = 0; j < data->pasSigners[i].csCertChain; j++)
1078 CertFreeCertificateContext(data->pasSigners[i].pasCertChain[j].pCert);
1079 data->psPfns->pfnFree(data->pasSigners[i].pasCertChain);
1080 data->psPfns->pfnFree(data->pasSigners[i].psSigner);
1081 CertFreeCertificateChain(data->pasSigners[i].pChainContext);
1083 data->psPfns->pfnFree(data->pasSigners);
1085 for (i = 0; i < data->chStores; i++)
1086 CertCloseStore(data->pahStores[i], 0);
1087 data->psPfns->pfnFree(data->pahStores);
1091 data->psPfns->pfnFree(data->u.pPDSip->pSip);
1092 data->psPfns->pfnFree(data->u.pPDSip->pCATSip);
1093 data->psPfns->pfnFree(data->u.pPDSip->psSipSubjectInfo);
1094 data->psPfns->pfnFree(data->u.pPDSip->psSipCATSubjectInfo);
1095 data->psPfns->pfnFree(data->u.pPDSip->psIndirectData);
1098 CryptMsgClose(data->hMsg);
1100 if (data->fOpenedFile &&
1101 data->pWintrustData->dwUnionChoice == WTD_CHOICE_FILE)
1102 CloseHandle(data->pWintrustData->u.pFile->hFile);
1107 HRESULT WINAPI HTTPSCertificateTrust(CRYPT_PROVIDER_DATA *data)
1109 FIXME("(%p)\n", data);
1113 HRESULT WINAPI HTTPSFinalProv(CRYPT_PROVIDER_DATA *data)
1115 FIXME("(%p)\n", data);