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 /* Assumes data->pWintrustData->u.pFile exists. Makes sure a file handle is
60 static BOOL SOFTPUB_OpenFile(CRYPT_PROVIDER_DATA *data)
64 /* PSDK implies that all values should be initialized to NULL, so callers
65 * typically have hFile as NULL rather than INVALID_HANDLE_VALUE. Check
68 if (!data->pWintrustData->u.pFile->hFile ||
69 data->pWintrustData->u.pFile->hFile == INVALID_HANDLE_VALUE)
71 data->pWintrustData->u.pFile->hFile =
72 CreateFileW(data->pWintrustData->u.pFile->pcwszFilePath, GENERIC_READ,
73 FILE_SHARE_READ, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
74 if (data->pWintrustData->u.pFile->hFile != INVALID_HANDLE_VALUE)
75 data->fOpenedFile = TRUE;
80 GetFileTime(data->pWintrustData->u.pFile->hFile, &data->sftSystemTime,
82 TRACE("returning %d\n", ret);
86 /* Assumes data->pWintrustData->u.pFile exists. Sets data->pPDSip->gSubject to
87 * the file's subject GUID.
89 static BOOL SOFTPUB_GetFileSubject(CRYPT_PROVIDER_DATA *data)
93 if (!WVT_ISINSTRUCT(WINTRUST_FILE_INFO,
94 data->pWintrustData->u.pFile->cbStruct, pgKnownSubject) ||
95 !data->pWintrustData->u.pFile->pgKnownSubject)
97 ret = CryptSIPRetrieveSubjectGuid(
98 data->pWintrustData->u.pFile->pcwszFilePath,
99 data->pWintrustData->u.pFile->hFile,
100 &data->u.pPDSip->gSubject);
104 data->u.pPDSip->gSubject = *data->pWintrustData->u.pFile->pgKnownSubject;
107 TRACE("returning %d\n", ret);
111 /* Assumes data->u.pPDSip exists, and its gSubject member set.
112 * Allocates data->u.pPDSip->pSip and loads it, if possible.
114 static BOOL SOFTPUB_GetSIP(CRYPT_PROVIDER_DATA *data)
118 data->u.pPDSip->pSip = data->psPfns->pfnAlloc(sizeof(SIP_DISPATCH_INFO));
119 if (data->u.pPDSip->pSip)
120 ret = CryptSIPLoad(&data->u.pPDSip->gSubject, 0, data->u.pPDSip->pSip);
123 SetLastError(ERROR_OUTOFMEMORY);
126 TRACE("returning %d\n", ret);
130 /* Assumes data->u.pPDSip has been loaded, and data->u.pPDSip->pSip allocated.
131 * Calls data->u.pPDSip->pSip->pfGet to construct data->hMsg.
133 static BOOL SOFTPUB_GetMessageFromFile(CRYPT_PROVIDER_DATA *data, HANDLE file,
140 data->u.pPDSip->psSipSubjectInfo =
141 data->psPfns->pfnAlloc(sizeof(SIP_SUBJECTINFO));
142 if (!data->u.pPDSip->psSipSubjectInfo)
144 SetLastError(ERROR_OUTOFMEMORY);
148 data->u.pPDSip->psSipSubjectInfo->cbSize = sizeof(SIP_SUBJECTINFO);
149 data->u.pPDSip->psSipSubjectInfo->pgSubjectType = &data->u.pPDSip->gSubject;
150 data->u.pPDSip->psSipSubjectInfo->hFile = file;
151 data->u.pPDSip->psSipSubjectInfo->pwsFileName = filePath;
152 data->u.pPDSip->psSipSubjectInfo->hProv = data->hProv;
153 ret = data->u.pPDSip->pSip->pfGet(data->u.pPDSip->psSipSubjectInfo,
154 &data->dwEncoding, 0, &size, 0);
157 SetLastError(TRUST_E_NOSIGNATURE);
161 buf = data->psPfns->pfnAlloc(size);
164 SetLastError(ERROR_OUTOFMEMORY);
168 ret = data->u.pPDSip->pSip->pfGet(data->u.pPDSip->psSipSubjectInfo,
169 &data->dwEncoding, 0, &size, buf);
172 data->hMsg = CryptMsgOpenToDecode(data->dwEncoding, 0, 0, data->hProv,
175 ret = CryptMsgUpdate(data->hMsg, buf, size, TRUE);
178 data->psPfns->pfnFree(buf);
179 TRACE("returning %d\n", ret);
183 static BOOL SOFTPUB_CreateStoreFromMessage(CRYPT_PROVIDER_DATA *data)
188 store = CertOpenStore(CERT_STORE_PROV_MSG, data->dwEncoding,
189 data->hProv, CERT_STORE_NO_CRYPT_RELEASE_FLAG, data->hMsg);
192 ret = data->psPfns->pfnAddStore2Chain(data, store);
193 CertCloseStore(store, 0);
195 TRACE("returning %d\n", ret);
199 static DWORD SOFTPUB_DecodeInnerContent(CRYPT_PROVIDER_DATA *data)
206 ret = CryptMsgGetParam(data->hMsg, CMSG_INNER_CONTENT_TYPE_PARAM, 0, NULL,
210 oid = data->psPfns->pfnAlloc(size);
213 SetLastError(ERROR_OUTOFMEMORY);
217 ret = CryptMsgGetParam(data->hMsg, CMSG_INNER_CONTENT_TYPE_PARAM, 0, oid,
221 ret = CryptMsgGetParam(data->hMsg, CMSG_CONTENT_PARAM, 0, NULL, &size);
224 buf = data->psPfns->pfnAlloc(size);
227 SetLastError(ERROR_OUTOFMEMORY);
231 ret = CryptMsgGetParam(data->hMsg, CMSG_CONTENT_PARAM, 0, buf, &size);
234 ret = CryptDecodeObject(data->dwEncoding, oid, buf, size, 0, NULL, &size);
237 data->u.pPDSip->psIndirectData = data->psPfns->pfnAlloc(size);
238 if (!data->u.pPDSip->psIndirectData)
240 SetLastError(ERROR_OUTOFMEMORY);
244 ret = CryptDecodeObject(data->dwEncoding, oid, buf, size, 0,
245 data->u.pPDSip->psIndirectData, &size);
248 TRACE("returning %d\n", ret);
249 data->psPfns->pfnFree(oid);
250 data->psPfns->pfnFree(buf);
254 static BOOL SOFTPUB_LoadCertMessage(CRYPT_PROVIDER_DATA *data)
258 if (data->pWintrustData->u.pCert &&
259 WVT_IS_CBSTRUCT_GT_MEMBEROFFSET(WINTRUST_CERT_INFO,
260 data->pWintrustData->u.pCert->cbStruct, psCertContext))
264 CRYPT_PROVIDER_SGNR signer = { sizeof(signer), { 0 } };
267 /* Add a signer with nothing but the time to verify, so we can
270 if (WVT_ISINSTRUCT(WINTRUST_CERT_INFO,
271 data->pWintrustData->u.pCert->cbStruct, psftVerifyAsOf) &&
272 data->pWintrustData->u.pCert->psftVerifyAsOf)
273 data->sftSystemTime = signer.sftVerifyAsOf;
278 GetSystemTime(&sysTime);
279 SystemTimeToFileTime(&sysTime, &signer.sftVerifyAsOf);
281 ret = data->psPfns->pfnAddSgnr2Chain(data, FALSE, 0, &signer);
284 ret = data->psPfns->pfnAddCert2Chain(data, 0, FALSE, 0,
285 data->pWintrustData->u.pCert->psCertContext);
286 if (WVT_ISINSTRUCT(WINTRUST_CERT_INFO,
287 data->pWintrustData->u.pCert->cbStruct, pahStores))
289 ret && i < data->pWintrustData->u.pCert->chStores; i++)
290 ret = data->psPfns->pfnAddStore2Chain(data,
291 data->pWintrustData->u.pCert->pahStores[i]);
296 /* Do nothing!? See the tests */
302 SetLastError(ERROR_INVALID_PARAMETER);
308 static BOOL SOFTPUB_LoadFileMessage(CRYPT_PROVIDER_DATA *data)
312 if (!data->pWintrustData->u.pFile)
314 SetLastError(ERROR_INVALID_PARAMETER);
318 ret = SOFTPUB_OpenFile(data);
321 ret = SOFTPUB_GetFileSubject(data);
324 ret = SOFTPUB_GetSIP(data);
327 ret = SOFTPUB_GetMessageFromFile(data, data->pWintrustData->u.pFile->hFile,
328 data->pWintrustData->u.pFile->pcwszFilePath);
331 ret = SOFTPUB_CreateStoreFromMessage(data);
334 ret = SOFTPUB_DecodeInnerContent(data);
339 static BOOL SOFTPUB_LoadCatalogMessage(CRYPT_PROVIDER_DATA *data)
342 HANDLE catalog = INVALID_HANDLE_VALUE;
344 if (!data->pWintrustData->u.pCatalog)
346 SetLastError(ERROR_INVALID_PARAMETER);
349 catalog = CreateFileW(data->pWintrustData->u.pCatalog->pcwszCatalogFilePath,
350 GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL,
352 if (catalog == INVALID_HANDLE_VALUE)
354 ret = CryptSIPRetrieveSubjectGuid(
355 data->pWintrustData->u.pCatalog->pcwszCatalogFilePath, catalog,
356 &data->u.pPDSip->gSubject);
359 ret = SOFTPUB_GetSIP(data);
362 ret = SOFTPUB_GetMessageFromFile(data, catalog,
363 data->pWintrustData->u.pCatalog->pcwszCatalogFilePath);
366 ret = SOFTPUB_CreateStoreFromMessage(data);
369 ret = SOFTPUB_DecodeInnerContent(data);
370 /* FIXME: this loads the catalog file, but doesn't validate the member. */
372 CloseHandle(catalog);
376 HRESULT WINAPI SoftpubLoadMessage(CRYPT_PROVIDER_DATA *data)
380 TRACE("(%p)\n", data);
382 if (!data->padwTrustStepErrors)
385 switch (data->pWintrustData->dwUnionChoice)
387 case WTD_CHOICE_CERT:
388 ret = SOFTPUB_LoadCertMessage(data);
390 case WTD_CHOICE_FILE:
391 ret = SOFTPUB_LoadFileMessage(data);
393 case WTD_CHOICE_CATALOG:
394 ret = SOFTPUB_LoadCatalogMessage(data);
397 FIXME("unimplemented for %d\n", data->pWintrustData->dwUnionChoice);
398 SetLastError(ERROR_INVALID_PARAMETER);
403 data->padwTrustStepErrors[TRUSTERROR_STEP_FINAL_OBJPROV] =
405 TRACE("returning %d (%08x)\n", ret ? S_OK : S_FALSE,
406 data->padwTrustStepErrors[TRUSTERROR_STEP_FINAL_OBJPROV]);
407 return ret ? S_OK : S_FALSE;
410 static CMSG_SIGNER_INFO *WINTRUST_GetSigner(CRYPT_PROVIDER_DATA *data,
414 CMSG_SIGNER_INFO *signerInfo = NULL;
417 ret = CryptMsgGetParam(data->hMsg, CMSG_SIGNER_INFO_PARAM, signerIdx,
421 signerInfo = data->psPfns->pfnAlloc(size);
424 ret = CryptMsgGetParam(data->hMsg, CMSG_SIGNER_INFO_PARAM,
425 signerIdx, signerInfo, &size);
428 data->psPfns->pfnFree(signerInfo);
433 SetLastError(ERROR_OUTOFMEMORY);
438 static BOOL WINTRUST_SaveSigner(CRYPT_PROVIDER_DATA *data, DWORD signerIdx)
441 CMSG_SIGNER_INFO *signerInfo = WINTRUST_GetSigner(data, signerIdx);
445 CRYPT_PROVIDER_SGNR sgnr = { sizeof(sgnr), { 0 } };
447 sgnr.psSigner = signerInfo;
448 sgnr.sftVerifyAsOf = data->sftSystemTime;
449 ret = data->psPfns->pfnAddSgnr2Chain(data, FALSE, signerIdx, &sgnr);
456 static CERT_INFO *WINTRUST_GetSignerCertInfo(CRYPT_PROVIDER_DATA *data,
460 CERT_INFO *certInfo = NULL;
463 ret = CryptMsgGetParam(data->hMsg, CMSG_SIGNER_CERT_INFO_PARAM, signerIdx,
467 certInfo = data->psPfns->pfnAlloc(size);
470 ret = CryptMsgGetParam(data->hMsg, CMSG_SIGNER_CERT_INFO_PARAM,
471 signerIdx, certInfo, &size);
474 data->psPfns->pfnFree(certInfo);
479 SetLastError(ERROR_OUTOFMEMORY);
484 static BOOL WINTRUST_VerifySigner(CRYPT_PROVIDER_DATA *data, DWORD signerIdx)
487 CERT_INFO *certInfo = WINTRUST_GetSignerCertInfo(data, signerIdx);
491 PCCERT_CONTEXT subject = CertGetSubjectCertificateFromStore(
492 data->pahStores[0], data->dwEncoding, certInfo);
496 CMSG_CTRL_VERIFY_SIGNATURE_EX_PARA para = { sizeof(para), 0,
497 signerIdx, CMSG_VERIFY_SIGNER_CERT, (LPVOID)subject };
499 ret = CryptMsgControl(data->hMsg, 0, CMSG_CTRL_VERIFY_SIGNATURE_EX,
502 SetLastError(TRUST_E_CERT_SIGNATURE);
504 data->psPfns->pfnAddCert2Chain(data, signerIdx, FALSE, 0,
506 CertFreeCertificateContext(subject);
510 SetLastError(TRUST_E_NO_SIGNER_CERT);
513 data->psPfns->pfnFree(certInfo);
520 HRESULT WINAPI SoftpubLoadSignature(CRYPT_PROVIDER_DATA *data)
524 TRACE("(%p)\n", data);
526 if (!data->padwTrustStepErrors)
531 DWORD signerCount, size;
533 size = sizeof(signerCount);
534 ret = CryptMsgGetParam(data->hMsg, CMSG_SIGNER_COUNT_PARAM, 0,
535 &signerCount, &size);
540 for (i = 0; ret && i < signerCount; i++)
542 if ((ret = WINTRUST_SaveSigner(data, i)))
543 ret = WINTRUST_VerifySigner(data, i);
547 SetLastError(TRUST_E_NOSIGNATURE);
552 data->padwTrustStepErrors[TRUSTERROR_STEP_FINAL_SIGPROV] =
554 return ret ? S_OK : S_FALSE;
557 static DWORD WINTRUST_TrustStatusToConfidence(DWORD errorStatus)
559 DWORD confidence = 0;
562 if (!(errorStatus & CERT_TRUST_IS_NOT_SIGNATURE_VALID))
563 confidence |= CERT_CONFIDENCE_SIG;
564 if (!(errorStatus & CERT_TRUST_IS_NOT_TIME_VALID))
565 confidence |= CERT_CONFIDENCE_TIME;
566 if (!(errorStatus & CERT_TRUST_IS_NOT_TIME_NESTED))
567 confidence |= CERT_CONFIDENCE_TIMENEST;
571 BOOL WINAPI SoftpubCheckCert(CRYPT_PROVIDER_DATA *data, DWORD idxSigner,
572 BOOL fCounterSignerChain, DWORD idxCounterSigner)
576 TRACE("(%p, %d, %d, %d)\n", data, idxSigner, fCounterSignerChain,
579 if (fCounterSignerChain)
581 FIXME("unimplemented for counter signers\n");
586 PCERT_SIMPLE_CHAIN simpleChain =
587 data->pasSigners[idxSigner].pChainContext->rgpChain[0];
591 for (i = 0; i < simpleChain->cElement; i++)
594 data->pasSigners[idxSigner].pasCertChain[i].dwConfidence =
595 WINTRUST_TrustStatusToConfidence(
596 simpleChain->rgpElement[i]->TrustStatus.dwErrorStatus);
597 /* Set additional flags */
598 if (!(simpleChain->rgpElement[i]->TrustStatus.dwErrorStatus &
599 CERT_TRUST_IS_UNTRUSTED_ROOT))
600 data->pasSigners[idxSigner].pasCertChain[i].fTrustedRoot = TRUE;
601 if (simpleChain->rgpElement[i]->TrustStatus.dwInfoStatus &
602 CERT_TRUST_IS_SELF_SIGNED)
603 data->pasSigners[idxSigner].pasCertChain[i].fSelfSigned = TRUE;
604 if (simpleChain->rgpElement[i]->TrustStatus.dwErrorStatus &
605 CERT_TRUST_IS_CYCLIC)
606 data->pasSigners[idxSigner].pasCertChain[i].fIsCyclic = TRUE;
612 static DWORD WINTRUST_TrustStatusToError(DWORD errorStatus)
616 if (errorStatus & CERT_TRUST_IS_NOT_SIGNATURE_VALID)
617 error = TRUST_E_CERT_SIGNATURE;
618 else if (errorStatus & CERT_TRUST_IS_UNTRUSTED_ROOT)
619 error = CERT_E_UNTRUSTEDROOT;
620 else if (errorStatus & CERT_TRUST_IS_NOT_TIME_VALID)
621 error = CERT_E_EXPIRED;
622 else if (errorStatus & CERT_TRUST_IS_NOT_TIME_NESTED)
623 error = CERT_E_VALIDITYPERIODNESTING;
624 else if (errorStatus & CERT_TRUST_IS_REVOKED)
625 error = CERT_E_REVOKED;
626 else if (errorStatus & CERT_TRUST_IS_OFFLINE_REVOCATION ||
627 errorStatus & CERT_TRUST_REVOCATION_STATUS_UNKNOWN)
628 error = CERT_E_REVOCATION_FAILURE;
629 else if (errorStatus & CERT_TRUST_IS_NOT_VALID_FOR_USAGE)
630 error = CERT_E_WRONG_USAGE;
631 else if (errorStatus & CERT_TRUST_IS_CYCLIC)
632 error = CERT_E_CHAINING;
633 else if (errorStatus & CERT_TRUST_INVALID_EXTENSION)
634 error = CERT_E_CRITICAL;
635 else if (errorStatus & CERT_TRUST_INVALID_POLICY_CONSTRAINTS)
636 error = CERT_E_INVALID_POLICY;
637 else if (errorStatus & CERT_TRUST_INVALID_BASIC_CONSTRAINTS)
638 error = TRUST_E_BASIC_CONSTRAINTS;
639 else if (errorStatus & CERT_TRUST_INVALID_NAME_CONSTRAINTS ||
640 errorStatus & CERT_TRUST_HAS_NOT_SUPPORTED_NAME_CONSTRAINT ||
641 errorStatus & CERT_TRUST_HAS_NOT_DEFINED_NAME_CONSTRAINT ||
642 errorStatus & CERT_TRUST_HAS_NOT_PERMITTED_NAME_CONSTRAINT ||
643 errorStatus & CERT_TRUST_HAS_EXCLUDED_NAME_CONSTRAINT)
644 error = CERT_E_INVALID_NAME;
645 else if (errorStatus & CERT_TRUST_NO_ISSUANCE_CHAIN_POLICY)
646 error = CERT_E_INVALID_POLICY;
647 else if (errorStatus)
649 FIXME("unknown error status %08x\n", errorStatus);
650 error = TRUST_E_SYSTEM_ERROR;
657 static BOOL WINTRUST_CopyChain(CRYPT_PROVIDER_DATA *data, DWORD signerIdx)
660 PCERT_SIMPLE_CHAIN simpleChain =
661 data->pasSigners[signerIdx].pChainContext->rgpChain[0];
664 data->pasSigners[signerIdx].pasCertChain[0].dwConfidence =
665 WINTRUST_TrustStatusToConfidence(
666 simpleChain->rgpElement[0]->TrustStatus.dwErrorStatus);
667 data->pasSigners[signerIdx].pasCertChain[0].pChainElement =
668 simpleChain->rgpElement[0];
670 for (i = 1; ret && i < simpleChain->cElement; i++)
672 ret = data->psPfns->pfnAddCert2Chain(data, signerIdx, FALSE, 0,
673 simpleChain->rgpElement[i]->pCertContext);
676 data->pasSigners[signerIdx].pasCertChain[i].pChainElement =
677 simpleChain->rgpElement[i];
678 data->pasSigners[signerIdx].pasCertChain[i].dwConfidence =
679 WINTRUST_TrustStatusToConfidence(
680 simpleChain->rgpElement[i]->TrustStatus.dwErrorStatus);
683 data->pasSigners[signerIdx].pasCertChain[simpleChain->cElement - 1].dwError
684 = WINTRUST_TrustStatusToError(
685 simpleChain->rgpElement[simpleChain->cElement - 1]->
686 TrustStatus.dwErrorStatus);
690 static void WINTRUST_CreateChainPolicyCreateInfo(
691 const CRYPT_PROVIDER_DATA *data, PWTD_GENERIC_CHAIN_POLICY_CREATE_INFO info,
692 PCERT_CHAIN_PARA chainPara)
694 chainPara->cbSize = sizeof(CERT_CHAIN_PARA);
695 if (data->pRequestUsage)
696 chainPara->RequestedUsage = *data->pRequestUsage;
699 chainPara->RequestedUsage.dwType = 0;
700 chainPara->RequestedUsage.Usage.cUsageIdentifier = 0;
702 info->u.cbSize = sizeof(WTD_GENERIC_CHAIN_POLICY_CREATE_INFO);
703 info->hChainEngine = NULL;
704 info->pChainPara = chainPara;
705 if (data->dwProvFlags & CPD_REVOCATION_CHECK_END_CERT)
706 info->dwFlags = CERT_CHAIN_REVOCATION_CHECK_END_CERT;
707 else if (data->dwProvFlags & CPD_REVOCATION_CHECK_CHAIN)
708 info->dwFlags = CERT_CHAIN_REVOCATION_CHECK_CHAIN;
709 else if (data->dwProvFlags & CPD_REVOCATION_CHECK_CHAIN_EXCLUDE_ROOT)
710 info->dwFlags = CERT_CHAIN_REVOCATION_CHECK_CHAIN_EXCLUDE_ROOT;
713 info->pvReserved = NULL;
716 static BOOL WINTRUST_CreateChainForSigner(CRYPT_PROVIDER_DATA *data,
717 DWORD signer, PWTD_GENERIC_CHAIN_POLICY_CREATE_INFO createInfo,
718 PCERT_CHAIN_PARA chainPara)
721 HCERTSTORE store = NULL;
725 store = CertOpenStore(CERT_STORE_PROV_COLLECTION, 0, 0,
726 CERT_STORE_CREATE_NEW_FLAG, NULL);
731 for (i = 0; i < data->chStores; i++)
732 CertAddStoreToCollection(store, data->pahStores[i], 0, 0);
735 /* Expect the end certificate for each signer to be the only cert in the
738 if (data->pasSigners[signer].csCertChain)
740 /* Create a certificate chain for each signer */
741 ret = CertGetCertificateChain(createInfo->hChainEngine,
742 data->pasSigners[signer].pasCertChain[0].pCert,
743 &data->pasSigners[signer].sftVerifyAsOf, store,
744 chainPara, createInfo->dwFlags, createInfo->pvReserved,
745 &data->pasSigners[signer].pChainContext);
748 if (data->pasSigners[signer].pChainContext->cChain != 1)
750 FIXME("unimplemented for more than 1 simple chain\n");
755 if ((ret = WINTRUST_CopyChain(data, signer)))
757 if (data->psPfns->pfnCertCheckPolicy)
758 ret = data->psPfns->pfnCertCheckPolicy(data, signer,
761 TRACE("no cert check policy, skipping policy check\n");
766 CertCloseStore(store, 0);
770 HRESULT WINAPI WintrustCertificateTrust(CRYPT_PROVIDER_DATA *data)
774 TRACE("(%p)\n", data);
776 if (!data->csSigners)
779 SetLastError(TRUST_E_NOSIGNATURE);
784 WTD_GENERIC_CHAIN_POLICY_CREATE_INFO createInfo;
785 CERT_CHAIN_PARA chainPara;
787 WINTRUST_CreateChainPolicyCreateInfo(data, &createInfo, &chainPara);
789 for (i = 0; i < data->csSigners; i++)
790 ret = WINTRUST_CreateChainForSigner(data, i, &createInfo,
794 data->padwTrustStepErrors[TRUSTERROR_STEP_FINAL_CERTPROV] =
796 TRACE("returning %d (%08x)\n", ret ? S_OK : S_FALSE,
797 data->padwTrustStepErrors[TRUSTERROR_STEP_FINAL_CERTPROV]);
798 return ret ? S_OK : S_FALSE;
801 HRESULT WINAPI GenericChainCertificateTrust(CRYPT_PROVIDER_DATA *data)
804 WTD_GENERIC_CHAIN_POLICY_DATA *policyData =
805 data->pWintrustData->pPolicyCallbackData;
807 TRACE("(%p)\n", data);
809 if (policyData && policyData->u.cbSize !=
810 sizeof(WTD_GENERIC_CHAIN_POLICY_CREATE_INFO))
812 SetLastError(ERROR_INVALID_PARAMETER);
816 if (!data->csSigners)
819 SetLastError(TRUST_E_NOSIGNATURE);
824 WTD_GENERIC_CHAIN_POLICY_CREATE_INFO createInfo, *pCreateInfo;
825 CERT_CHAIN_PARA chainPara, *pChainPara;
829 pCreateInfo = policyData->pSignerChainInfo;
830 pChainPara = pCreateInfo->pChainPara;
834 WINTRUST_CreateChainPolicyCreateInfo(data, &createInfo, &chainPara);
835 pChainPara = &chainPara;
836 pCreateInfo = &createInfo;
839 for (i = 0; i < data->csSigners; i++)
840 ret = WINTRUST_CreateChainForSigner(data, i, pCreateInfo,
846 data->padwTrustStepErrors[TRUSTERROR_STEP_FINAL_CERTPROV] =
848 TRACE("returning %d (%08x)\n", ret ? S_OK : S_FALSE,
849 data->padwTrustStepErrors[TRUSTERROR_STEP_FINAL_CERTPROV]);
850 return ret ? S_OK : S_FALSE;
853 HRESULT WINAPI SoftpubAuthenticode(CRYPT_PROVIDER_DATA *data)
856 CERT_CHAIN_POLICY_STATUS policyStatus = { sizeof(policyStatus), 0 };
858 TRACE("(%p)\n", data);
860 if (data->pWintrustData->dwUIChoice != WTD_UI_NONE)
861 FIXME("unimplemented for UI choice %d\n",
862 data->pWintrustData->dwUIChoice);
863 if (!data->csSigners)
866 policyStatus.dwError = TRUST_E_NOSIGNATURE;
873 for (i = 0; ret && i < data->csSigners; i++)
876 DWORD size = sizeof(hash);
878 /* First make sure cert isn't disallowed */
879 if ((ret = CertGetCertificateContextProperty(
880 data->pasSigners[i].pasCertChain[0].pCert,
881 CERT_SIGNATURE_HASH_PROP_ID, hash, &size)))
883 static const WCHAR disallowedW[] =
884 { 'D','i','s','a','l','l','o','w','e','d',0 };
885 HCERTSTORE disallowed = CertOpenStore(CERT_STORE_PROV_SYSTEM_W,
886 X509_ASN_ENCODING, 0, CERT_SYSTEM_STORE_CURRENT_USER,
891 PCCERT_CONTEXT found = CertFindCertificateInStore(
892 disallowed, X509_ASN_ENCODING, 0, CERT_FIND_SIGNATURE_HASH,
897 /* Disallowed! Can't verify it. */
898 policyStatus.dwError = TRUST_E_SUBJECT_NOT_TRUSTED;
900 CertFreeCertificateContext(found);
902 CertCloseStore(disallowed, 0);
907 CERT_CHAIN_POLICY_PARA policyPara = { sizeof(policyPara), 0 };
909 if (data->dwRegPolicySettings & WTPF_TRUSTTEST)
910 policyPara.dwFlags |= CERT_CHAIN_POLICY_TRUST_TESTROOT_FLAG;
911 if (data->dwRegPolicySettings & WTPF_TESTCANBEVALID)
912 policyPara.dwFlags |= CERT_CHAIN_POLICY_ALLOW_TESTROOT_FLAG;
913 if (data->dwRegPolicySettings & WTPF_IGNOREEXPIRATION)
914 policyPara.dwFlags |=
915 CERT_CHAIN_POLICY_IGNORE_NOT_TIME_VALID_FLAG |
916 CERT_CHAIN_POLICY_IGNORE_CTL_NOT_TIME_VALID_FLAG |
917 CERT_CHAIN_POLICY_IGNORE_NOT_TIME_NESTED_FLAG;
918 if (data->dwRegPolicySettings & WTPF_IGNOREREVOKATION)
919 policyPara.dwFlags |=
920 CERT_CHAIN_POLICY_IGNORE_END_REV_UNKNOWN_FLAG |
921 CERT_CHAIN_POLICY_IGNORE_CTL_SIGNER_REV_UNKNOWN_FLAG |
922 CERT_CHAIN_POLICY_IGNORE_CA_REV_UNKNOWN_FLAG |
923 CERT_CHAIN_POLICY_IGNORE_ROOT_REV_UNKNOWN_FLAG;
924 CertVerifyCertificateChainPolicy(CERT_CHAIN_POLICY_AUTHENTICODE,
925 data->pasSigners[i].pChainContext, &policyPara, &policyStatus);
926 if (policyStatus.dwError != NO_ERROR)
932 data->padwTrustStepErrors[TRUSTERROR_STEP_FINAL_POLICYPROV] =
933 policyStatus.dwError;
934 TRACE("returning %d (%08x)\n", ret ? S_OK : S_FALSE,
935 data->padwTrustStepErrors[TRUSTERROR_STEP_FINAL_POLICYPROV]);
936 return ret ? S_OK : S_FALSE;
939 static HRESULT WINAPI WINTRUST_DefaultPolicy(CRYPT_PROVIDER_DATA *pProvData,
940 DWORD dwStepError, DWORD dwRegPolicySettings, DWORD cSigner,
941 PWTD_GENERIC_CHAIN_POLICY_SIGNER_INFO rgpSigner, void *pvPolicyArg)
944 CERT_CHAIN_POLICY_STATUS policyStatus = { sizeof(policyStatus), 0 };
946 for (i = 0; !policyStatus.dwError && i < cSigner; i++)
948 CERT_CHAIN_POLICY_PARA policyPara = { sizeof(policyPara), 0 };
950 if (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 (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_BASE,
962 rgpSigner[i].pChainContext, &policyPara, &policyStatus);
964 return policyStatus.dwError;
967 HRESULT WINAPI GenericChainFinalProv(CRYPT_PROVIDER_DATA *data)
969 HRESULT err = NO_ERROR; /* not a typo, MS confused the types */
970 WTD_GENERIC_CHAIN_POLICY_DATA *policyData =
971 data->pWintrustData->pPolicyCallbackData;
973 TRACE("(%p)\n", data);
975 if (data->pWintrustData->dwUIChoice != WTD_UI_NONE)
976 FIXME("unimplemented for UI choice %d\n",
977 data->pWintrustData->dwUIChoice);
978 if (!data->csSigners)
979 err = TRUST_E_NOSIGNATURE;
982 PFN_WTD_GENERIC_CHAIN_POLICY_CALLBACK policyCallback;
984 WTD_GENERIC_CHAIN_POLICY_SIGNER_INFO *signers = NULL;
988 policyCallback = policyData->pfnPolicyCallback;
989 policyArg = policyData->pvPolicyArg;
993 policyCallback = WINTRUST_DefaultPolicy;
1000 signers = data->psPfns->pfnAlloc(
1001 data->csSigners * sizeof(WTD_GENERIC_CHAIN_POLICY_SIGNER_INFO));
1004 for (i = 0; i < data->csSigners; i++)
1006 signers[i].u.cbSize =
1007 sizeof(WTD_GENERIC_CHAIN_POLICY_SIGNER_INFO);
1008 signers[i].pChainContext =
1009 data->pasSigners[i].pChainContext;
1010 signers[i].dwSignerType = data->pasSigners[i].dwSignerType;
1011 signers[i].pMsgSignerInfo = data->pasSigners[i].psSigner;
1012 signers[i].dwError = data->pasSigners[i].dwError;
1013 if (data->pasSigners[i].csCounterSigners)
1014 FIXME("unimplemented for counter signers\n");
1015 signers[i].cCounterSigner = 0;
1016 signers[i].rgpCounterSigner = NULL;
1020 err = ERROR_OUTOFMEMORY;
1022 if (err == NO_ERROR)
1023 err = policyCallback(data, TRUSTERROR_STEP_FINAL_POLICYPROV,
1024 data->dwRegPolicySettings, data->csSigners, signers, policyArg);
1025 data->psPfns->pfnFree(signers);
1027 if (err != NO_ERROR)
1028 data->padwTrustStepErrors[TRUSTERROR_STEP_FINAL_POLICYPROV] = err;
1029 TRACE("returning %d (%08x)\n", err == NO_ERROR ? S_OK : S_FALSE,
1030 data->padwTrustStepErrors[TRUSTERROR_STEP_FINAL_POLICYPROV]);
1031 return err == NO_ERROR ? S_OK : S_FALSE;
1034 HRESULT WINAPI SoftpubCleanup(CRYPT_PROVIDER_DATA *data)
1038 for (i = 0; i < data->csSigners; i++)
1040 for (j = 0; j < data->pasSigners[i].csCertChain; j++)
1041 CertFreeCertificateContext(data->pasSigners[i].pasCertChain[j].pCert);
1042 data->psPfns->pfnFree(data->pasSigners[i].pasCertChain);
1043 data->psPfns->pfnFree(data->pasSigners[i].psSigner);
1044 CertFreeCertificateChain(data->pasSigners[i].pChainContext);
1046 data->psPfns->pfnFree(data->pasSigners);
1048 for (i = 0; i < data->chStores; i++)
1049 CertCloseStore(data->pahStores[i], 0);
1050 data->psPfns->pfnFree(data->pahStores);
1054 data->psPfns->pfnFree(data->u.pPDSip->pSip);
1055 data->psPfns->pfnFree(data->u.pPDSip->pCATSip);
1056 data->psPfns->pfnFree(data->u.pPDSip->psSipSubjectInfo);
1057 data->psPfns->pfnFree(data->u.pPDSip->psSipCATSubjectInfo);
1058 data->psPfns->pfnFree(data->u.pPDSip->psIndirectData);
1061 CryptMsgClose(data->hMsg);
1063 if (data->fOpenedFile)
1064 CloseHandle(data->pWintrustData->u.pFile->hFile);
1069 HRESULT WINAPI HTTPSCertificateTrust(CRYPT_PROVIDER_DATA *data)
1071 FIXME("(%p)\n", data);
1075 HRESULT WINAPI HTTPSFinalProv(CRYPT_PROVIDER_DATA *data)
1077 FIXME("(%p)\n", data);