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 BOOL SOFTPUB_OpenFile(CRYPT_PROVIDER_DATA *data)
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", ret);
104 /* Assumes data->pWintrustData->u.pFile exists. Sets data->pPDSip->gSubject to
105 * the file's subject GUID.
107 static BOOL SOFTPUB_GetFileSubject(CRYPT_PROVIDER_DATA *data)
111 if (!WVT_ISINSTRUCT(WINTRUST_FILE_INFO,
112 data->pWintrustData->u.pFile->cbStruct, pgKnownSubject) ||
113 !data->pWintrustData->u.pFile->pgKnownSubject)
115 ret = CryptSIPRetrieveSubjectGuid(
116 data->pWintrustData->u.pFile->pcwszFilePath,
117 data->pWintrustData->u.pFile->hFile,
118 &data->u.pPDSip->gSubject);
122 data->u.pPDSip->gSubject = *data->pWintrustData->u.pFile->pgKnownSubject;
125 TRACE("returning %d\n", ret);
129 /* Assumes data->u.pPDSip exists, and its gSubject member set.
130 * Allocates data->u.pPDSip->pSip and loads it, if possible.
132 static BOOL SOFTPUB_GetSIP(CRYPT_PROVIDER_DATA *data)
136 data->u.pPDSip->pSip = data->psPfns->pfnAlloc(sizeof(SIP_DISPATCH_INFO));
137 if (data->u.pPDSip->pSip)
138 ret = CryptSIPLoad(&data->u.pPDSip->gSubject, 0, data->u.pPDSip->pSip);
141 SetLastError(ERROR_OUTOFMEMORY);
144 TRACE("returning %d\n", ret);
148 /* Assumes data->u.pPDSip has been loaded, and data->u.pPDSip->pSip allocated.
149 * Calls data->u.pPDSip->pSip->pfGet to construct data->hMsg.
151 static BOOL SOFTPUB_GetMessageFromFile(CRYPT_PROVIDER_DATA *data, HANDLE file,
158 data->u.pPDSip->psSipSubjectInfo =
159 data->psPfns->pfnAlloc(sizeof(SIP_SUBJECTINFO));
160 if (!data->u.pPDSip->psSipSubjectInfo)
162 SetLastError(ERROR_OUTOFMEMORY);
166 data->u.pPDSip->psSipSubjectInfo->cbSize = sizeof(SIP_SUBJECTINFO);
167 data->u.pPDSip->psSipSubjectInfo->pgSubjectType = &data->u.pPDSip->gSubject;
168 data->u.pPDSip->psSipSubjectInfo->hFile = file;
169 data->u.pPDSip->psSipSubjectInfo->pwsFileName = filePath;
170 data->u.pPDSip->psSipSubjectInfo->hProv = data->hProv;
171 ret = data->u.pPDSip->pSip->pfGet(data->u.pPDSip->psSipSubjectInfo,
172 &data->dwEncoding, 0, &size, 0);
175 SetLastError(TRUST_E_NOSIGNATURE);
179 buf = data->psPfns->pfnAlloc(size);
182 SetLastError(ERROR_OUTOFMEMORY);
186 ret = data->u.pPDSip->pSip->pfGet(data->u.pPDSip->psSipSubjectInfo,
187 &data->dwEncoding, 0, &size, buf);
190 data->hMsg = CryptMsgOpenToDecode(data->dwEncoding, 0, 0, data->hProv,
193 ret = CryptMsgUpdate(data->hMsg, buf, size, TRUE);
196 data->psPfns->pfnFree(buf);
197 TRACE("returning %d\n", ret);
201 static BOOL SOFTPUB_CreateStoreFromMessage(CRYPT_PROVIDER_DATA *data)
206 store = CertOpenStore(CERT_STORE_PROV_MSG, data->dwEncoding,
207 data->hProv, CERT_STORE_NO_CRYPT_RELEASE_FLAG, data->hMsg);
210 ret = data->psPfns->pfnAddStore2Chain(data, store);
211 CertCloseStore(store, 0);
213 TRACE("returning %d\n", ret);
217 static DWORD SOFTPUB_DecodeInnerContent(CRYPT_PROVIDER_DATA *data)
224 ret = CryptMsgGetParam(data->hMsg, CMSG_INNER_CONTENT_TYPE_PARAM, 0, NULL,
228 oid = data->psPfns->pfnAlloc(size);
231 SetLastError(ERROR_OUTOFMEMORY);
235 ret = CryptMsgGetParam(data->hMsg, CMSG_INNER_CONTENT_TYPE_PARAM, 0, oid,
239 ret = CryptMsgGetParam(data->hMsg, CMSG_CONTENT_PARAM, 0, NULL, &size);
242 buf = data->psPfns->pfnAlloc(size);
245 SetLastError(ERROR_OUTOFMEMORY);
249 ret = CryptMsgGetParam(data->hMsg, CMSG_CONTENT_PARAM, 0, buf, &size);
252 ret = CryptDecodeObject(data->dwEncoding, oid, buf, size, 0, NULL, &size);
255 data->u.pPDSip->psIndirectData = data->psPfns->pfnAlloc(size);
256 if (!data->u.pPDSip->psIndirectData)
258 SetLastError(ERROR_OUTOFMEMORY);
262 ret = CryptDecodeObject(data->dwEncoding, oid, buf, size, 0,
263 data->u.pPDSip->psIndirectData, &size);
266 TRACE("returning %d\n", ret);
267 data->psPfns->pfnFree(oid);
268 data->psPfns->pfnFree(buf);
272 static BOOL SOFTPUB_LoadCertMessage(CRYPT_PROVIDER_DATA *data)
276 if (data->pWintrustData->u.pCert &&
277 WVT_IS_CBSTRUCT_GT_MEMBEROFFSET(WINTRUST_CERT_INFO,
278 data->pWintrustData->u.pCert->cbStruct, psCertContext))
282 CRYPT_PROVIDER_SGNR signer = { sizeof(signer), { 0 } };
285 /* Add a signer with nothing but the time to verify, so we can
288 if (WVT_ISINSTRUCT(WINTRUST_CERT_INFO,
289 data->pWintrustData->u.pCert->cbStruct, psftVerifyAsOf) &&
290 data->pWintrustData->u.pCert->psftVerifyAsOf)
291 data->sftSystemTime = signer.sftVerifyAsOf;
296 GetSystemTime(&sysTime);
297 SystemTimeToFileTime(&sysTime, &signer.sftVerifyAsOf);
299 ret = data->psPfns->pfnAddSgnr2Chain(data, FALSE, 0, &signer);
302 ret = data->psPfns->pfnAddCert2Chain(data, 0, FALSE, 0,
303 data->pWintrustData->u.pCert->psCertContext);
304 if (WVT_ISINSTRUCT(WINTRUST_CERT_INFO,
305 data->pWintrustData->u.pCert->cbStruct, pahStores))
307 ret && i < data->pWintrustData->u.pCert->chStores; i++)
308 ret = data->psPfns->pfnAddStore2Chain(data,
309 data->pWintrustData->u.pCert->pahStores[i]);
314 /* Do nothing!? See the tests */
320 SetLastError(ERROR_INVALID_PARAMETER);
326 static BOOL SOFTPUB_LoadFileMessage(CRYPT_PROVIDER_DATA *data)
330 if (!data->pWintrustData->u.pFile)
332 SetLastError(ERROR_INVALID_PARAMETER);
336 ret = SOFTPUB_OpenFile(data);
339 ret = SOFTPUB_GetFileSubject(data);
342 ret = SOFTPUB_GetSIP(data);
345 ret = SOFTPUB_GetMessageFromFile(data, data->pWintrustData->u.pFile->hFile,
346 data->pWintrustData->u.pFile->pcwszFilePath);
349 ret = SOFTPUB_CreateStoreFromMessage(data);
352 ret = SOFTPUB_DecodeInnerContent(data);
357 static BOOL SOFTPUB_LoadCatalogMessage(CRYPT_PROVIDER_DATA *data)
360 HANDLE catalog = INVALID_HANDLE_VALUE;
362 if (!data->pWintrustData->u.pCatalog)
364 SetLastError(ERROR_INVALID_PARAMETER);
367 catalog = CreateFileW(data->pWintrustData->u.pCatalog->pcwszCatalogFilePath,
368 GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL,
370 if (catalog == INVALID_HANDLE_VALUE)
372 ret = CryptSIPRetrieveSubjectGuid(
373 data->pWintrustData->u.pCatalog->pcwszCatalogFilePath, catalog,
374 &data->u.pPDSip->gSubject);
377 ret = SOFTPUB_GetSIP(data);
380 ret = SOFTPUB_GetMessageFromFile(data, catalog,
381 data->pWintrustData->u.pCatalog->pcwszCatalogFilePath);
384 ret = SOFTPUB_CreateStoreFromMessage(data);
387 ret = SOFTPUB_DecodeInnerContent(data);
388 /* FIXME: this loads the catalog file, but doesn't validate the member. */
390 CloseHandle(catalog);
394 HRESULT WINAPI SoftpubLoadMessage(CRYPT_PROVIDER_DATA *data)
398 TRACE("(%p)\n", data);
400 if (!data->padwTrustStepErrors)
403 switch (data->pWintrustData->dwUnionChoice)
405 case WTD_CHOICE_CERT:
406 ret = SOFTPUB_LoadCertMessage(data);
408 case WTD_CHOICE_FILE:
409 ret = SOFTPUB_LoadFileMessage(data);
411 case WTD_CHOICE_CATALOG:
412 ret = SOFTPUB_LoadCatalogMessage(data);
415 FIXME("unimplemented for %d\n", data->pWintrustData->dwUnionChoice);
416 SetLastError(ERROR_INVALID_PARAMETER);
421 data->padwTrustStepErrors[TRUSTERROR_STEP_FINAL_OBJPROV] =
423 TRACE("returning %d (%08x)\n", ret ? S_OK : S_FALSE,
424 data->padwTrustStepErrors[TRUSTERROR_STEP_FINAL_OBJPROV]);
425 return ret ? S_OK : S_FALSE;
428 static CMSG_SIGNER_INFO *WINTRUST_GetSigner(CRYPT_PROVIDER_DATA *data,
432 CMSG_SIGNER_INFO *signerInfo = NULL;
435 ret = CryptMsgGetParam(data->hMsg, CMSG_SIGNER_INFO_PARAM, signerIdx,
439 signerInfo = data->psPfns->pfnAlloc(size);
442 ret = CryptMsgGetParam(data->hMsg, CMSG_SIGNER_INFO_PARAM,
443 signerIdx, signerInfo, &size);
446 data->psPfns->pfnFree(signerInfo);
451 SetLastError(ERROR_OUTOFMEMORY);
456 static BOOL WINTRUST_SaveSigner(CRYPT_PROVIDER_DATA *data, DWORD signerIdx)
459 CMSG_SIGNER_INFO *signerInfo = WINTRUST_GetSigner(data, signerIdx);
463 CRYPT_PROVIDER_SGNR sgnr = { sizeof(sgnr), { 0 } };
465 sgnr.psSigner = signerInfo;
466 sgnr.sftVerifyAsOf = data->sftSystemTime;
467 ret = data->psPfns->pfnAddSgnr2Chain(data, FALSE, signerIdx, &sgnr);
474 static CERT_INFO *WINTRUST_GetSignerCertInfo(CRYPT_PROVIDER_DATA *data,
478 CERT_INFO *certInfo = NULL;
481 ret = CryptMsgGetParam(data->hMsg, CMSG_SIGNER_CERT_INFO_PARAM, signerIdx,
485 certInfo = data->psPfns->pfnAlloc(size);
488 ret = CryptMsgGetParam(data->hMsg, CMSG_SIGNER_CERT_INFO_PARAM,
489 signerIdx, certInfo, &size);
492 data->psPfns->pfnFree(certInfo);
497 SetLastError(ERROR_OUTOFMEMORY);
502 static BOOL WINTRUST_VerifySigner(CRYPT_PROVIDER_DATA *data, DWORD signerIdx)
505 CERT_INFO *certInfo = WINTRUST_GetSignerCertInfo(data, signerIdx);
509 PCCERT_CONTEXT subject = CertGetSubjectCertificateFromStore(
510 data->pahStores[0], data->dwEncoding, certInfo);
514 CMSG_CTRL_VERIFY_SIGNATURE_EX_PARA para = { sizeof(para), 0,
515 signerIdx, CMSG_VERIFY_SIGNER_CERT, (LPVOID)subject };
517 ret = CryptMsgControl(data->hMsg, 0, CMSG_CTRL_VERIFY_SIGNATURE_EX,
520 SetLastError(TRUST_E_CERT_SIGNATURE);
522 data->psPfns->pfnAddCert2Chain(data, signerIdx, FALSE, 0,
524 CertFreeCertificateContext(subject);
528 SetLastError(TRUST_E_NO_SIGNER_CERT);
531 data->psPfns->pfnFree(certInfo);
538 HRESULT WINAPI SoftpubLoadSignature(CRYPT_PROVIDER_DATA *data)
542 TRACE("(%p)\n", data);
544 if (!data->padwTrustStepErrors)
549 DWORD signerCount, size;
551 size = sizeof(signerCount);
552 ret = CryptMsgGetParam(data->hMsg, CMSG_SIGNER_COUNT_PARAM, 0,
553 &signerCount, &size);
558 for (i = 0; ret && i < signerCount; i++)
560 if ((ret = WINTRUST_SaveSigner(data, i)))
561 ret = WINTRUST_VerifySigner(data, i);
565 SetLastError(TRUST_E_NOSIGNATURE);
570 data->padwTrustStepErrors[TRUSTERROR_STEP_FINAL_SIGPROV] =
572 return ret ? S_OK : S_FALSE;
575 static DWORD WINTRUST_TrustStatusToConfidence(DWORD errorStatus)
577 DWORD confidence = 0;
580 if (!(errorStatus & CERT_TRUST_IS_NOT_SIGNATURE_VALID))
581 confidence |= CERT_CONFIDENCE_SIG;
582 if (!(errorStatus & CERT_TRUST_IS_NOT_TIME_VALID))
583 confidence |= CERT_CONFIDENCE_TIME;
584 if (!(errorStatus & CERT_TRUST_IS_NOT_TIME_NESTED))
585 confidence |= CERT_CONFIDENCE_TIMENEST;
589 BOOL WINAPI SoftpubCheckCert(CRYPT_PROVIDER_DATA *data, DWORD idxSigner,
590 BOOL fCounterSignerChain, DWORD idxCounterSigner)
594 TRACE("(%p, %d, %d, %d)\n", data, idxSigner, fCounterSignerChain,
597 if (fCounterSignerChain)
599 FIXME("unimplemented for counter signers\n");
604 PCERT_SIMPLE_CHAIN simpleChain =
605 data->pasSigners[idxSigner].pChainContext->rgpChain[0];
609 for (i = 0; i < simpleChain->cElement; i++)
612 data->pasSigners[idxSigner].pasCertChain[i].dwConfidence =
613 WINTRUST_TrustStatusToConfidence(
614 simpleChain->rgpElement[i]->TrustStatus.dwErrorStatus);
615 /* Set additional flags */
616 if (!(simpleChain->rgpElement[i]->TrustStatus.dwErrorStatus &
617 CERT_TRUST_IS_UNTRUSTED_ROOT))
618 data->pasSigners[idxSigner].pasCertChain[i].fTrustedRoot = TRUE;
619 if (simpleChain->rgpElement[i]->TrustStatus.dwInfoStatus &
620 CERT_TRUST_IS_SELF_SIGNED)
621 data->pasSigners[idxSigner].pasCertChain[i].fSelfSigned = TRUE;
622 if (simpleChain->rgpElement[i]->TrustStatus.dwErrorStatus &
623 CERT_TRUST_IS_CYCLIC)
624 data->pasSigners[idxSigner].pasCertChain[i].fIsCyclic = TRUE;
630 static DWORD WINTRUST_TrustStatusToError(DWORD errorStatus)
634 if (errorStatus & CERT_TRUST_IS_NOT_SIGNATURE_VALID)
635 error = TRUST_E_CERT_SIGNATURE;
636 else if (errorStatus & CERT_TRUST_IS_UNTRUSTED_ROOT)
637 error = CERT_E_UNTRUSTEDROOT;
638 else if (errorStatus & CERT_TRUST_IS_NOT_TIME_VALID)
639 error = CERT_E_EXPIRED;
640 else if (errorStatus & CERT_TRUST_IS_NOT_TIME_NESTED)
641 error = CERT_E_VALIDITYPERIODNESTING;
642 else if (errorStatus & CERT_TRUST_IS_REVOKED)
643 error = CERT_E_REVOKED;
644 else if (errorStatus & CERT_TRUST_IS_OFFLINE_REVOCATION ||
645 errorStatus & CERT_TRUST_REVOCATION_STATUS_UNKNOWN)
646 error = CERT_E_REVOCATION_FAILURE;
647 else if (errorStatus & CERT_TRUST_IS_NOT_VALID_FOR_USAGE)
648 error = CERT_E_WRONG_USAGE;
649 else if (errorStatus & CERT_TRUST_IS_CYCLIC)
650 error = CERT_E_CHAINING;
651 else if (errorStatus & CERT_TRUST_INVALID_EXTENSION)
652 error = CERT_E_CRITICAL;
653 else if (errorStatus & CERT_TRUST_INVALID_POLICY_CONSTRAINTS)
654 error = CERT_E_INVALID_POLICY;
655 else if (errorStatus & CERT_TRUST_INVALID_BASIC_CONSTRAINTS)
656 error = TRUST_E_BASIC_CONSTRAINTS;
657 else if (errorStatus & CERT_TRUST_INVALID_NAME_CONSTRAINTS ||
658 errorStatus & CERT_TRUST_HAS_NOT_SUPPORTED_NAME_CONSTRAINT ||
659 errorStatus & CERT_TRUST_HAS_NOT_DEFINED_NAME_CONSTRAINT ||
660 errorStatus & CERT_TRUST_HAS_NOT_PERMITTED_NAME_CONSTRAINT ||
661 errorStatus & CERT_TRUST_HAS_EXCLUDED_NAME_CONSTRAINT)
662 error = CERT_E_INVALID_NAME;
663 else if (errorStatus & CERT_TRUST_NO_ISSUANCE_CHAIN_POLICY)
664 error = CERT_E_INVALID_POLICY;
665 else if (errorStatus)
667 FIXME("unknown error status %08x\n", errorStatus);
668 error = TRUST_E_SYSTEM_ERROR;
675 static BOOL WINTRUST_CopyChain(CRYPT_PROVIDER_DATA *data, DWORD signerIdx)
678 PCERT_SIMPLE_CHAIN simpleChain =
679 data->pasSigners[signerIdx].pChainContext->rgpChain[0];
682 data->pasSigners[signerIdx].pasCertChain[0].dwConfidence =
683 WINTRUST_TrustStatusToConfidence(
684 simpleChain->rgpElement[0]->TrustStatus.dwErrorStatus);
685 data->pasSigners[signerIdx].pasCertChain[0].pChainElement =
686 simpleChain->rgpElement[0];
688 for (i = 1; ret && i < simpleChain->cElement; i++)
690 ret = data->psPfns->pfnAddCert2Chain(data, signerIdx, FALSE, 0,
691 simpleChain->rgpElement[i]->pCertContext);
694 data->pasSigners[signerIdx].pasCertChain[i].pChainElement =
695 simpleChain->rgpElement[i];
696 data->pasSigners[signerIdx].pasCertChain[i].dwConfidence =
697 WINTRUST_TrustStatusToConfidence(
698 simpleChain->rgpElement[i]->TrustStatus.dwErrorStatus);
701 data->pasSigners[signerIdx].pasCertChain[simpleChain->cElement - 1].dwError
702 = WINTRUST_TrustStatusToError(
703 simpleChain->rgpElement[simpleChain->cElement - 1]->
704 TrustStatus.dwErrorStatus);
708 static void WINTRUST_CreateChainPolicyCreateInfo(
709 const CRYPT_PROVIDER_DATA *data, PWTD_GENERIC_CHAIN_POLICY_CREATE_INFO info,
710 PCERT_CHAIN_PARA chainPara)
712 chainPara->cbSize = sizeof(CERT_CHAIN_PARA);
713 if (data->pRequestUsage)
714 chainPara->RequestedUsage = *data->pRequestUsage;
717 chainPara->RequestedUsage.dwType = 0;
718 chainPara->RequestedUsage.Usage.cUsageIdentifier = 0;
720 info->u.cbSize = sizeof(WTD_GENERIC_CHAIN_POLICY_CREATE_INFO);
721 info->hChainEngine = NULL;
722 info->pChainPara = chainPara;
723 if (data->dwProvFlags & CPD_REVOCATION_CHECK_END_CERT)
724 info->dwFlags = CERT_CHAIN_REVOCATION_CHECK_END_CERT;
725 else if (data->dwProvFlags & CPD_REVOCATION_CHECK_CHAIN)
726 info->dwFlags = CERT_CHAIN_REVOCATION_CHECK_CHAIN;
727 else if (data->dwProvFlags & CPD_REVOCATION_CHECK_CHAIN_EXCLUDE_ROOT)
728 info->dwFlags = CERT_CHAIN_REVOCATION_CHECK_CHAIN_EXCLUDE_ROOT;
731 info->pvReserved = NULL;
734 static BOOL WINTRUST_CreateChainForSigner(CRYPT_PROVIDER_DATA *data,
735 DWORD signer, PWTD_GENERIC_CHAIN_POLICY_CREATE_INFO createInfo,
736 PCERT_CHAIN_PARA chainPara)
739 HCERTSTORE store = NULL;
743 store = CertOpenStore(CERT_STORE_PROV_COLLECTION, 0, 0,
744 CERT_STORE_CREATE_NEW_FLAG, NULL);
749 for (i = 0; i < data->chStores; i++)
750 CertAddStoreToCollection(store, data->pahStores[i], 0, 0);
753 /* Expect the end certificate for each signer to be the only cert in the
756 if (data->pasSigners[signer].csCertChain)
758 /* Create a certificate chain for each signer */
759 ret = CertGetCertificateChain(createInfo->hChainEngine,
760 data->pasSigners[signer].pasCertChain[0].pCert,
761 &data->pasSigners[signer].sftVerifyAsOf, store,
762 chainPara, createInfo->dwFlags, createInfo->pvReserved,
763 &data->pasSigners[signer].pChainContext);
766 if (data->pasSigners[signer].pChainContext->cChain != 1)
768 FIXME("unimplemented for more than 1 simple chain\n");
773 if ((ret = WINTRUST_CopyChain(data, signer)))
775 if (data->psPfns->pfnCertCheckPolicy)
776 ret = data->psPfns->pfnCertCheckPolicy(data, signer,
779 TRACE("no cert check policy, skipping policy check\n");
784 CertCloseStore(store, 0);
788 HRESULT WINAPI WintrustCertificateTrust(CRYPT_PROVIDER_DATA *data)
792 TRACE("(%p)\n", data);
794 if (!data->csSigners)
797 SetLastError(TRUST_E_NOSIGNATURE);
802 WTD_GENERIC_CHAIN_POLICY_CREATE_INFO createInfo;
803 CERT_CHAIN_PARA chainPara;
805 WINTRUST_CreateChainPolicyCreateInfo(data, &createInfo, &chainPara);
807 for (i = 0; i < data->csSigners; i++)
808 ret = WINTRUST_CreateChainForSigner(data, i, &createInfo,
812 data->padwTrustStepErrors[TRUSTERROR_STEP_FINAL_CERTPROV] =
814 TRACE("returning %d (%08x)\n", ret ? S_OK : S_FALSE,
815 data->padwTrustStepErrors[TRUSTERROR_STEP_FINAL_CERTPROV]);
816 return ret ? S_OK : S_FALSE;
819 HRESULT WINAPI GenericChainCertificateTrust(CRYPT_PROVIDER_DATA *data)
822 WTD_GENERIC_CHAIN_POLICY_DATA *policyData =
823 data->pWintrustData->pPolicyCallbackData;
825 TRACE("(%p)\n", data);
827 if (policyData && policyData->u.cbSize !=
828 sizeof(WTD_GENERIC_CHAIN_POLICY_CREATE_INFO))
830 SetLastError(ERROR_INVALID_PARAMETER);
834 if (!data->csSigners)
837 SetLastError(TRUST_E_NOSIGNATURE);
842 WTD_GENERIC_CHAIN_POLICY_CREATE_INFO createInfo, *pCreateInfo;
843 CERT_CHAIN_PARA chainPara, *pChainPara;
847 pCreateInfo = policyData->pSignerChainInfo;
848 pChainPara = pCreateInfo->pChainPara;
852 WINTRUST_CreateChainPolicyCreateInfo(data, &createInfo, &chainPara);
853 pChainPara = &chainPara;
854 pCreateInfo = &createInfo;
857 for (i = 0; i < data->csSigners; i++)
858 ret = WINTRUST_CreateChainForSigner(data, i, pCreateInfo,
864 data->padwTrustStepErrors[TRUSTERROR_STEP_FINAL_CERTPROV] =
866 TRACE("returning %d (%08x)\n", ret ? S_OK : S_FALSE,
867 data->padwTrustStepErrors[TRUSTERROR_STEP_FINAL_CERTPROV]);
868 return ret ? S_OK : S_FALSE;
871 HRESULT WINAPI SoftpubAuthenticode(CRYPT_PROVIDER_DATA *data)
874 CERT_CHAIN_POLICY_STATUS policyStatus = { sizeof(policyStatus), 0 };
876 TRACE("(%p)\n", data);
878 if (data->pWintrustData->dwUIChoice != WTD_UI_NONE)
879 FIXME("unimplemented for UI choice %d\n",
880 data->pWintrustData->dwUIChoice);
881 if (!data->csSigners)
884 policyStatus.dwError = TRUST_E_NOSIGNATURE;
891 for (i = 0; ret && i < data->csSigners; i++)
894 DWORD size = sizeof(hash);
896 /* First make sure cert isn't disallowed */
897 if ((ret = CertGetCertificateContextProperty(
898 data->pasSigners[i].pasCertChain[0].pCert,
899 CERT_SIGNATURE_HASH_PROP_ID, hash, &size)))
901 static const WCHAR disallowedW[] =
902 { 'D','i','s','a','l','l','o','w','e','d',0 };
903 HCERTSTORE disallowed = CertOpenStore(CERT_STORE_PROV_SYSTEM_W,
904 X509_ASN_ENCODING, 0, CERT_SYSTEM_STORE_CURRENT_USER,
909 PCCERT_CONTEXT found = CertFindCertificateInStore(
910 disallowed, X509_ASN_ENCODING, 0, CERT_FIND_SIGNATURE_HASH,
915 /* Disallowed! Can't verify it. */
916 policyStatus.dwError = TRUST_E_SUBJECT_NOT_TRUSTED;
918 CertFreeCertificateContext(found);
920 CertCloseStore(disallowed, 0);
925 CERT_CHAIN_POLICY_PARA policyPara = { sizeof(policyPara), 0 };
927 if (data->dwRegPolicySettings & WTPF_TRUSTTEST)
928 policyPara.dwFlags |= CERT_CHAIN_POLICY_TRUST_TESTROOT_FLAG;
929 if (data->dwRegPolicySettings & WTPF_TESTCANBEVALID)
930 policyPara.dwFlags |= CERT_CHAIN_POLICY_ALLOW_TESTROOT_FLAG;
931 if (data->dwRegPolicySettings & WTPF_IGNOREEXPIRATION)
932 policyPara.dwFlags |=
933 CERT_CHAIN_POLICY_IGNORE_NOT_TIME_VALID_FLAG |
934 CERT_CHAIN_POLICY_IGNORE_CTL_NOT_TIME_VALID_FLAG |
935 CERT_CHAIN_POLICY_IGNORE_NOT_TIME_NESTED_FLAG;
936 if (data->dwRegPolicySettings & WTPF_IGNOREREVOKATION)
937 policyPara.dwFlags |=
938 CERT_CHAIN_POLICY_IGNORE_END_REV_UNKNOWN_FLAG |
939 CERT_CHAIN_POLICY_IGNORE_CTL_SIGNER_REV_UNKNOWN_FLAG |
940 CERT_CHAIN_POLICY_IGNORE_CA_REV_UNKNOWN_FLAG |
941 CERT_CHAIN_POLICY_IGNORE_ROOT_REV_UNKNOWN_FLAG;
942 CertVerifyCertificateChainPolicy(CERT_CHAIN_POLICY_AUTHENTICODE,
943 data->pasSigners[i].pChainContext, &policyPara, &policyStatus);
944 if (policyStatus.dwError != NO_ERROR)
950 data->padwTrustStepErrors[TRUSTERROR_STEP_FINAL_POLICYPROV] =
951 policyStatus.dwError;
952 TRACE("returning %d (%08x)\n", ret ? S_OK : S_FALSE,
953 data->padwTrustStepErrors[TRUSTERROR_STEP_FINAL_POLICYPROV]);
954 return ret ? S_OK : S_FALSE;
957 static HRESULT WINAPI WINTRUST_DefaultPolicy(CRYPT_PROVIDER_DATA *pProvData,
958 DWORD dwStepError, DWORD dwRegPolicySettings, DWORD cSigner,
959 PWTD_GENERIC_CHAIN_POLICY_SIGNER_INFO rgpSigner, void *pvPolicyArg)
962 CERT_CHAIN_POLICY_STATUS policyStatus = { sizeof(policyStatus), 0 };
964 for (i = 0; !policyStatus.dwError && i < cSigner; i++)
966 CERT_CHAIN_POLICY_PARA policyPara = { sizeof(policyPara), 0 };
968 if (dwRegPolicySettings & WTPF_IGNOREEXPIRATION)
969 policyPara.dwFlags |=
970 CERT_CHAIN_POLICY_IGNORE_NOT_TIME_VALID_FLAG |
971 CERT_CHAIN_POLICY_IGNORE_CTL_NOT_TIME_VALID_FLAG |
972 CERT_CHAIN_POLICY_IGNORE_NOT_TIME_NESTED_FLAG;
973 if (dwRegPolicySettings & WTPF_IGNOREREVOKATION)
974 policyPara.dwFlags |=
975 CERT_CHAIN_POLICY_IGNORE_END_REV_UNKNOWN_FLAG |
976 CERT_CHAIN_POLICY_IGNORE_CTL_SIGNER_REV_UNKNOWN_FLAG |
977 CERT_CHAIN_POLICY_IGNORE_CA_REV_UNKNOWN_FLAG |
978 CERT_CHAIN_POLICY_IGNORE_ROOT_REV_UNKNOWN_FLAG;
979 CertVerifyCertificateChainPolicy(CERT_CHAIN_POLICY_BASE,
980 rgpSigner[i].pChainContext, &policyPara, &policyStatus);
982 return policyStatus.dwError;
985 HRESULT WINAPI GenericChainFinalProv(CRYPT_PROVIDER_DATA *data)
987 HRESULT err = NO_ERROR; /* not a typo, MS confused the types */
988 WTD_GENERIC_CHAIN_POLICY_DATA *policyData =
989 data->pWintrustData->pPolicyCallbackData;
991 TRACE("(%p)\n", data);
993 if (data->pWintrustData->dwUIChoice != WTD_UI_NONE)
994 FIXME("unimplemented for UI choice %d\n",
995 data->pWintrustData->dwUIChoice);
996 if (!data->csSigners)
997 err = TRUST_E_NOSIGNATURE;
1000 PFN_WTD_GENERIC_CHAIN_POLICY_CALLBACK policyCallback;
1002 WTD_GENERIC_CHAIN_POLICY_SIGNER_INFO *signers = NULL;
1006 policyCallback = policyData->pfnPolicyCallback;
1007 policyArg = policyData->pvPolicyArg;
1011 policyCallback = WINTRUST_DefaultPolicy;
1014 if (data->csSigners)
1018 signers = data->psPfns->pfnAlloc(
1019 data->csSigners * sizeof(WTD_GENERIC_CHAIN_POLICY_SIGNER_INFO));
1022 for (i = 0; i < data->csSigners; i++)
1024 signers[i].u.cbSize =
1025 sizeof(WTD_GENERIC_CHAIN_POLICY_SIGNER_INFO);
1026 signers[i].pChainContext =
1027 data->pasSigners[i].pChainContext;
1028 signers[i].dwSignerType = data->pasSigners[i].dwSignerType;
1029 signers[i].pMsgSignerInfo = data->pasSigners[i].psSigner;
1030 signers[i].dwError = data->pasSigners[i].dwError;
1031 if (data->pasSigners[i].csCounterSigners)
1032 FIXME("unimplemented for counter signers\n");
1033 signers[i].cCounterSigner = 0;
1034 signers[i].rgpCounterSigner = NULL;
1038 err = ERROR_OUTOFMEMORY;
1040 if (err == NO_ERROR)
1041 err = policyCallback(data, TRUSTERROR_STEP_FINAL_POLICYPROV,
1042 data->dwRegPolicySettings, data->csSigners, signers, policyArg);
1043 data->psPfns->pfnFree(signers);
1045 if (err != NO_ERROR)
1046 data->padwTrustStepErrors[TRUSTERROR_STEP_FINAL_POLICYPROV] = err;
1047 TRACE("returning %d (%08x)\n", err == NO_ERROR ? S_OK : S_FALSE,
1048 data->padwTrustStepErrors[TRUSTERROR_STEP_FINAL_POLICYPROV]);
1049 return err == NO_ERROR ? S_OK : S_FALSE;
1052 HRESULT WINAPI SoftpubCleanup(CRYPT_PROVIDER_DATA *data)
1056 for (i = 0; i < data->csSigners; i++)
1058 for (j = 0; j < data->pasSigners[i].csCertChain; j++)
1059 CertFreeCertificateContext(data->pasSigners[i].pasCertChain[j].pCert);
1060 data->psPfns->pfnFree(data->pasSigners[i].pasCertChain);
1061 data->psPfns->pfnFree(data->pasSigners[i].psSigner);
1062 CertFreeCertificateChain(data->pasSigners[i].pChainContext);
1064 data->psPfns->pfnFree(data->pasSigners);
1066 for (i = 0; i < data->chStores; i++)
1067 CertCloseStore(data->pahStores[i], 0);
1068 data->psPfns->pfnFree(data->pahStores);
1072 data->psPfns->pfnFree(data->u.pPDSip->pSip);
1073 data->psPfns->pfnFree(data->u.pPDSip->pCATSip);
1074 data->psPfns->pfnFree(data->u.pPDSip->psSipSubjectInfo);
1075 data->psPfns->pfnFree(data->u.pPDSip->psSipCATSubjectInfo);
1076 data->psPfns->pfnFree(data->u.pPDSip->psIndirectData);
1079 CryptMsgClose(data->hMsg);
1081 if (data->fOpenedFile)
1082 CloseHandle(data->pWintrustData->u.pFile->hFile);
1087 HRESULT WINAPI HTTPSCertificateTrust(CRYPT_PROVIDER_DATA *data)
1089 FIXME("(%p)\n", data);
1093 HRESULT WINAPI HTTPSFinalProv(CRYPT_PROVIDER_DATA *data)
1095 FIXME("(%p)\n", data);