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
28 #include "wine/debug.h"
30 WINE_DEFAULT_DEBUG_CHANNEL(wintrust);
32 HRESULT WINAPI SoftpubDefCertInit(CRYPT_PROVIDER_DATA *data)
34 HRESULT ret = S_FALSE;
36 TRACE("(%p)\n", data);
38 if (data->padwTrustStepErrors &&
39 !data->padwTrustStepErrors[TRUSTERROR_STEP_FINAL_WVTINIT])
41 TRACE("returning %08x\n", ret);
45 HRESULT WINAPI SoftpubInitialize(CRYPT_PROVIDER_DATA *data)
47 HRESULT ret = S_FALSE;
49 TRACE("(%p)\n", data);
51 if (data->padwTrustStepErrors &&
52 !data->padwTrustStepErrors[TRUSTERROR_STEP_FINAL_WVTINIT])
54 TRACE("returning %08x\n", ret);
58 HRESULT WINAPI DriverInitializePolicy(CRYPT_PROVIDER_DATA *data)
64 HRESULT WINAPI DriverCleanupPolicy(CRYPT_PROVIDER_DATA *data)
70 HRESULT WINAPI DriverFinalPolicy(CRYPT_PROVIDER_DATA *data)
76 /* Assumes data->pWintrustData->u.pFile exists. Makes sure a file handle is
79 static DWORD SOFTPUB_OpenFile(CRYPT_PROVIDER_DATA *data)
81 DWORD err = ERROR_SUCCESS;
83 /* PSDK implies that all values should be initialized to NULL, so callers
84 * typically have hFile as NULL rather than INVALID_HANDLE_VALUE. Check
87 if (!data->pWintrustData->u.pFile->hFile ||
88 data->pWintrustData->u.pFile->hFile == INVALID_HANDLE_VALUE)
90 data->pWintrustData->u.pFile->hFile =
91 CreateFileW(data->pWintrustData->u.pFile->pcwszFilePath, GENERIC_READ,
92 FILE_SHARE_READ, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
93 if (data->pWintrustData->u.pFile->hFile != INVALID_HANDLE_VALUE)
94 data->fOpenedFile = TRUE;
99 GetFileTime(data->pWintrustData->u.pFile->hFile, &data->sftSystemTime,
101 TRACE("returning %d\n", err);
105 /* Assumes data->pWintrustData->u.pFile exists. Sets data->pPDSip->gSubject to
106 * the file's subject GUID.
108 static DWORD SOFTPUB_GetFileSubject(CRYPT_PROVIDER_DATA *data)
110 DWORD err = ERROR_SUCCESS;
112 if (!WVT_ISINSTRUCT(WINTRUST_FILE_INFO,
113 data->pWintrustData->u.pFile->cbStruct, pgKnownSubject) ||
114 !data->pWintrustData->u.pFile->pgKnownSubject)
116 if (!CryptSIPRetrieveSubjectGuid(
117 data->pWintrustData->u.pFile->pcwszFilePath,
118 data->pWintrustData->u.pFile->hFile,
119 &data->u.pPDSip->gSubject))
120 err = GetLastError();
123 data->u.pPDSip->gSubject = *data->pWintrustData->u.pFile->pgKnownSubject;
124 TRACE("returning %d\n", err);
128 /* Assumes data->u.pPDSip exists, and its gSubject member set.
129 * Allocates data->u.pPDSip->pSip and loads it, if possible.
131 static DWORD SOFTPUB_GetSIP(CRYPT_PROVIDER_DATA *data)
133 DWORD err = ERROR_SUCCESS;
135 data->u.pPDSip->pSip = data->psPfns->pfnAlloc(sizeof(SIP_DISPATCH_INFO));
136 if (data->u.pPDSip->pSip)
138 if (!CryptSIPLoad(&data->u.pPDSip->gSubject, 0, data->u.pPDSip->pSip))
139 err = GetLastError();
142 err = ERROR_OUTOFMEMORY;
143 TRACE("returning %d\n", err);
147 /* Assumes data->u.pPDSip has been loaded, and data->u.pPDSip->pSip allocated.
148 * Calls data->u.pPDSip->pSip->pfGet to construct data->hMsg.
150 static DWORD SOFTPUB_GetMessageFromFile(CRYPT_PROVIDER_DATA *data, HANDLE file,
153 DWORD err = ERROR_SUCCESS;
158 data->u.pPDSip->psSipSubjectInfo =
159 data->psPfns->pfnAlloc(sizeof(SIP_SUBJECTINFO));
160 if (!data->u.pPDSip->psSipSubjectInfo)
161 return ERROR_OUTOFMEMORY;
163 data->u.pPDSip->psSipSubjectInfo->cbSize = sizeof(SIP_SUBJECTINFO);
164 data->u.pPDSip->psSipSubjectInfo->pgSubjectType = &data->u.pPDSip->gSubject;
165 data->u.pPDSip->psSipSubjectInfo->hFile = file;
166 data->u.pPDSip->psSipSubjectInfo->pwsFileName = filePath;
167 data->u.pPDSip->psSipSubjectInfo->hProv = data->hProv;
168 ret = data->u.pPDSip->pSip->pfGet(data->u.pPDSip->psSipSubjectInfo,
169 &data->dwEncoding, 0, &size, 0);
171 return TRUST_E_NOSIGNATURE;
173 buf = data->psPfns->pfnAlloc(size);
175 return ERROR_OUTOFMEMORY;
177 ret = data->u.pPDSip->pSip->pfGet(data->u.pPDSip->psSipSubjectInfo,
178 &data->dwEncoding, 0, &size, buf);
181 data->hMsg = CryptMsgOpenToDecode(data->dwEncoding, 0, 0, data->hProv,
185 ret = CryptMsgUpdate(data->hMsg, buf, size, TRUE);
187 err = GetLastError();
191 err = GetLastError();
193 data->psPfns->pfnFree(buf);
194 TRACE("returning %d\n", err);
198 static DWORD SOFTPUB_CreateStoreFromMessage(CRYPT_PROVIDER_DATA *data)
200 DWORD err = ERROR_SUCCESS;
203 store = CertOpenStore(CERT_STORE_PROV_MSG, data->dwEncoding,
204 data->hProv, CERT_STORE_NO_CRYPT_RELEASE_FLAG, data->hMsg);
207 if (!data->psPfns->pfnAddStore2Chain(data, store))
208 err = GetLastError();
209 CertCloseStore(store, 0);
212 err = GetLastError();
213 TRACE("returning %d\n", err);
217 static DWORD SOFTPUB_DecodeInnerContent(CRYPT_PROVIDER_DATA *data)
220 DWORD size, err = ERROR_SUCCESS;
224 ret = CryptMsgGetParam(data->hMsg, CMSG_INNER_CONTENT_TYPE_PARAM, 0, NULL,
228 err = GetLastError();
231 oid = data->psPfns->pfnAlloc(size);
234 err = ERROR_OUTOFMEMORY;
237 ret = CryptMsgGetParam(data->hMsg, CMSG_INNER_CONTENT_TYPE_PARAM, 0, oid,
241 err = GetLastError();
244 ret = CryptMsgGetParam(data->hMsg, CMSG_CONTENT_PARAM, 0, NULL, &size);
247 err = GetLastError();
250 buf = data->psPfns->pfnAlloc(size);
253 err = ERROR_OUTOFMEMORY;
256 ret = CryptMsgGetParam(data->hMsg, CMSG_CONTENT_PARAM, 0, buf, &size);
259 err = GetLastError();
262 ret = CryptDecodeObject(data->dwEncoding, oid, buf, size, 0, NULL, &size);
265 err = GetLastError();
268 data->u.pPDSip->psIndirectData = data->psPfns->pfnAlloc(size);
269 if (!data->u.pPDSip->psIndirectData)
271 err = ERROR_OUTOFMEMORY;
274 ret = CryptDecodeObject(data->dwEncoding, oid, buf, size, 0,
275 data->u.pPDSip->psIndirectData, &size);
277 err = GetLastError();
280 TRACE("returning %d\n", err);
281 data->psPfns->pfnFree(oid);
282 data->psPfns->pfnFree(buf);
286 static DWORD SOFTPUB_LoadCertMessage(CRYPT_PROVIDER_DATA *data)
288 DWORD err = ERROR_SUCCESS;
290 if (data->pWintrustData->u.pCert &&
291 WVT_IS_CBSTRUCT_GT_MEMBEROFFSET(WINTRUST_CERT_INFO,
292 data->pWintrustData->u.pCert->cbStruct, psCertContext))
296 CRYPT_PROVIDER_SGNR signer = { sizeof(signer), { 0 } };
300 /* Add a signer with nothing but the time to verify, so we can
303 if (WVT_ISINSTRUCT(WINTRUST_CERT_INFO,
304 data->pWintrustData->u.pCert->cbStruct, psftVerifyAsOf) &&
305 data->pWintrustData->u.pCert->psftVerifyAsOf)
306 data->sftSystemTime = signer.sftVerifyAsOf;
311 GetSystemTime(&sysTime);
312 SystemTimeToFileTime(&sysTime, &signer.sftVerifyAsOf);
314 ret = data->psPfns->pfnAddSgnr2Chain(data, FALSE, 0, &signer);
317 ret = data->psPfns->pfnAddCert2Chain(data, 0, FALSE, 0,
318 data->pWintrustData->u.pCert->psCertContext);
319 if (WVT_ISINSTRUCT(WINTRUST_CERT_INFO,
320 data->pWintrustData->u.pCert->cbStruct, pahStores))
322 ret && i < data->pWintrustData->u.pCert->chStores; i++)
323 ret = data->psPfns->pfnAddStore2Chain(data,
324 data->pWintrustData->u.pCert->pahStores[i]);
327 err = GetLastError();
331 err = ERROR_INVALID_PARAMETER;
335 static DWORD SOFTPUB_LoadFileMessage(CRYPT_PROVIDER_DATA *data)
337 DWORD err = ERROR_SUCCESS;
339 if (!data->pWintrustData->u.pFile)
341 err = ERROR_INVALID_PARAMETER;
344 err = SOFTPUB_OpenFile(data);
347 err = SOFTPUB_GetFileSubject(data);
350 err = SOFTPUB_GetSIP(data);
353 err = SOFTPUB_GetMessageFromFile(data, data->pWintrustData->u.pFile->hFile,
354 data->pWintrustData->u.pFile->pcwszFilePath);
357 err = SOFTPUB_CreateStoreFromMessage(data);
360 err = SOFTPUB_DecodeInnerContent(data);
363 if (err && data->fOpenedFile && data->pWintrustData->u.pFile)
365 /* The caller won't expect the file to be open on failure, so close it.
367 CloseHandle(data->pWintrustData->u.pFile->hFile);
368 data->pWintrustData->u.pFile->hFile = INVALID_HANDLE_VALUE;
369 data->fOpenedFile = FALSE;
374 static DWORD SOFTPUB_LoadCatalogMessage(CRYPT_PROVIDER_DATA *data)
377 HANDLE catalog = INVALID_HANDLE_VALUE;
379 if (!data->pWintrustData->u.pCatalog)
381 SetLastError(ERROR_INVALID_PARAMETER);
384 catalog = CreateFileW(data->pWintrustData->u.pCatalog->pcwszCatalogFilePath,
385 GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL,
387 if (catalog == INVALID_HANDLE_VALUE)
388 return GetLastError();
389 if (!CryptSIPRetrieveSubjectGuid(
390 data->pWintrustData->u.pCatalog->pcwszCatalogFilePath, catalog,
391 &data->u.pPDSip->gSubject))
393 err = GetLastError();
396 err = SOFTPUB_GetSIP(data);
399 err = SOFTPUB_GetMessageFromFile(data, catalog,
400 data->pWintrustData->u.pCatalog->pcwszCatalogFilePath);
403 err = SOFTPUB_CreateStoreFromMessage(data);
406 err = SOFTPUB_DecodeInnerContent(data);
407 /* FIXME: this loads the catalog file, but doesn't validate the member. */
409 CloseHandle(catalog);
413 HRESULT WINAPI SoftpubLoadMessage(CRYPT_PROVIDER_DATA *data)
415 DWORD err = ERROR_SUCCESS;
417 TRACE("(%p)\n", data);
419 if (!data->padwTrustStepErrors)
422 switch (data->pWintrustData->dwUnionChoice)
424 case WTD_CHOICE_CERT:
425 err = SOFTPUB_LoadCertMessage(data);
427 case WTD_CHOICE_FILE:
428 err = SOFTPUB_LoadFileMessage(data);
430 case WTD_CHOICE_CATALOG:
431 err = SOFTPUB_LoadCatalogMessage(data);
434 FIXME("unimplemented for %d\n", data->pWintrustData->dwUnionChoice);
435 err = ERROR_INVALID_PARAMETER;
439 data->padwTrustStepErrors[TRUSTERROR_STEP_FINAL_OBJPROV] = err;
440 TRACE("returning %d (%08x)\n", !err ? S_OK : S_FALSE,
441 data->padwTrustStepErrors[TRUSTERROR_STEP_FINAL_OBJPROV]);
442 return !err ? S_OK : S_FALSE;
445 static CMSG_SIGNER_INFO *WINTRUST_GetSigner(CRYPT_PROVIDER_DATA *data,
449 CMSG_SIGNER_INFO *signerInfo = NULL;
452 ret = CryptMsgGetParam(data->hMsg, CMSG_SIGNER_INFO_PARAM, signerIdx,
456 signerInfo = data->psPfns->pfnAlloc(size);
459 ret = CryptMsgGetParam(data->hMsg, CMSG_SIGNER_INFO_PARAM,
460 signerIdx, signerInfo, &size);
463 data->psPfns->pfnFree(signerInfo);
468 SetLastError(ERROR_OUTOFMEMORY);
473 static BOOL WINTRUST_GetTimeFromCounterSigner(
474 const CMSG_CMS_SIGNER_INFO *counterSignerInfo, FILETIME *time)
477 BOOL foundTimeStamp = FALSE;
479 for (i = 0; !foundTimeStamp && i < counterSignerInfo->AuthAttrs.cAttr; i++)
481 if (!strcmp(counterSignerInfo->AuthAttrs.rgAttr[i].pszObjId,
482 szOID_RSA_signingTime))
484 const CRYPT_ATTRIBUTE *attr =
485 &counterSignerInfo->AuthAttrs.rgAttr[i];
488 for (j = 0; !foundTimeStamp && j < attr->cValue; j++)
490 static const DWORD encoding = X509_ASN_ENCODING |
492 DWORD size = sizeof(FILETIME);
494 foundTimeStamp = CryptDecodeObjectEx(encoding,
496 attr->rgValue[j].pbData, attr->rgValue[j].cbData, 0, NULL,
501 return foundTimeStamp;
504 static LPCSTR filetime_to_str(const FILETIME *time)
506 static char date[80];
507 char dateFmt[80]; /* sufficient for all versions of LOCALE_SSHORTDATE */
510 if (!time) return NULL;
512 GetLocaleInfoA(LOCALE_SYSTEM_DEFAULT, LOCALE_SSHORTDATE, dateFmt,
513 sizeof(dateFmt) / sizeof(dateFmt[0]));
514 FileTimeToSystemTime(time, &sysTime);
515 GetDateFormatA(LOCALE_SYSTEM_DEFAULT, 0, &sysTime, dateFmt, date,
516 sizeof(date) / sizeof(date[0]));
520 static FILETIME WINTRUST_GetTimeFromSigner(const CRYPT_PROVIDER_DATA *data,
521 const CMSG_SIGNER_INFO *signerInfo)
525 BOOL foundTimeStamp = FALSE;
527 for (i = 0; !foundTimeStamp && i < signerInfo->UnauthAttrs.cAttr; i++)
529 if (!strcmp(signerInfo->UnauthAttrs.rgAttr[i].pszObjId,
530 szOID_RSA_counterSign))
532 const CRYPT_ATTRIBUTE *attr = &signerInfo->UnauthAttrs.rgAttr[i];
535 for (j = 0; j < attr->cValue; j++)
537 static const DWORD encoding = X509_ASN_ENCODING |
539 CMSG_CMS_SIGNER_INFO *counterSignerInfo;
541 BOOL ret = CryptDecodeObjectEx(encoding, CMS_SIGNER_INFO,
542 attr->rgValue[j].pbData, attr->rgValue[j].cbData,
543 CRYPT_DECODE_ALLOC_FLAG, NULL, &counterSignerInfo, &size);
546 /* FIXME: need to verify countersigner signature too */
547 foundTimeStamp = WINTRUST_GetTimeFromCounterSigner(
548 counterSignerInfo, &time);
549 LocalFree(counterSignerInfo);
556 TRACE("returning system time %s\n",
557 filetime_to_str(&data->sftSystemTime));
558 time = data->sftSystemTime;
561 TRACE("returning time from message %s\n", filetime_to_str(&time));
565 static DWORD WINTRUST_SaveSigner(CRYPT_PROVIDER_DATA *data, DWORD signerIdx)
568 CMSG_SIGNER_INFO *signerInfo = WINTRUST_GetSigner(data, signerIdx);
572 CRYPT_PROVIDER_SGNR sgnr = { sizeof(sgnr), { 0 } };
574 sgnr.psSigner = signerInfo;
575 sgnr.sftVerifyAsOf = WINTRUST_GetTimeFromSigner(data, signerInfo);
576 if (!data->psPfns->pfnAddSgnr2Chain(data, FALSE, signerIdx, &sgnr))
577 err = GetLastError();
582 err = GetLastError();
586 static CERT_INFO *WINTRUST_GetSignerCertInfo(CRYPT_PROVIDER_DATA *data,
590 CERT_INFO *certInfo = NULL;
593 ret = CryptMsgGetParam(data->hMsg, CMSG_SIGNER_CERT_INFO_PARAM, signerIdx,
597 certInfo = data->psPfns->pfnAlloc(size);
600 ret = CryptMsgGetParam(data->hMsg, CMSG_SIGNER_CERT_INFO_PARAM,
601 signerIdx, certInfo, &size);
604 data->psPfns->pfnFree(certInfo);
609 SetLastError(ERROR_OUTOFMEMORY);
614 static DWORD WINTRUST_VerifySigner(CRYPT_PROVIDER_DATA *data, DWORD signerIdx)
617 CERT_INFO *certInfo = WINTRUST_GetSignerCertInfo(data, signerIdx);
621 PCCERT_CONTEXT subject = CertGetSubjectCertificateFromStore(
622 data->pahStores[0], data->dwEncoding, certInfo);
626 CMSG_CTRL_VERIFY_SIGNATURE_EX_PARA para = { sizeof(para), 0,
627 signerIdx, CMSG_VERIFY_SIGNER_CERT, (LPVOID)subject };
629 if (!CryptMsgControl(data->hMsg, 0, CMSG_CTRL_VERIFY_SIGNATURE_EX,
631 err = TRUST_E_CERT_SIGNATURE;
634 data->psPfns->pfnAddCert2Chain(data, signerIdx, FALSE, 0,
638 CertFreeCertificateContext(subject);
641 err = TRUST_E_NO_SIGNER_CERT;
642 data->psPfns->pfnFree(certInfo);
645 err = GetLastError();
649 HRESULT WINAPI SoftpubLoadSignature(CRYPT_PROVIDER_DATA *data)
653 TRACE("(%p)\n", data);
655 if (!data->padwTrustStepErrors)
660 DWORD signerCount, size;
662 size = sizeof(signerCount);
663 if (CryptMsgGetParam(data->hMsg, CMSG_SIGNER_COUNT_PARAM, 0,
664 &signerCount, &size))
669 for (i = 0; !err && i < signerCount; i++)
671 if (!(err = WINTRUST_SaveSigner(data, i)))
672 err = WINTRUST_VerifySigner(data, i);
676 err = TRUST_E_NOSIGNATURE;
681 data->padwTrustStepErrors[TRUSTERROR_STEP_FINAL_SIGPROV] = err;
682 return !err ? S_OK : S_FALSE;
685 static DWORD WINTRUST_TrustStatusToConfidence(DWORD errorStatus)
687 DWORD confidence = 0;
690 if (!(errorStatus & CERT_TRUST_IS_NOT_SIGNATURE_VALID))
691 confidence |= CERT_CONFIDENCE_SIG;
692 if (!(errorStatus & CERT_TRUST_IS_NOT_TIME_VALID))
693 confidence |= CERT_CONFIDENCE_TIME;
694 if (!(errorStatus & CERT_TRUST_IS_NOT_TIME_NESTED))
695 confidence |= CERT_CONFIDENCE_TIMENEST;
699 BOOL WINAPI SoftpubCheckCert(CRYPT_PROVIDER_DATA *data, DWORD idxSigner,
700 BOOL fCounterSignerChain, DWORD idxCounterSigner)
704 TRACE("(%p, %d, %d, %d)\n", data, idxSigner, fCounterSignerChain,
707 if (fCounterSignerChain)
709 FIXME("unimplemented for counter signers\n");
714 PCERT_SIMPLE_CHAIN simpleChain =
715 data->pasSigners[idxSigner].pChainContext->rgpChain[0];
719 for (i = 0; i < simpleChain->cElement; i++)
722 data->pasSigners[idxSigner].pasCertChain[i].dwConfidence =
723 WINTRUST_TrustStatusToConfidence(
724 simpleChain->rgpElement[i]->TrustStatus.dwErrorStatus);
725 /* Set additional flags */
726 if (!(simpleChain->rgpElement[i]->TrustStatus.dwErrorStatus &
727 CERT_TRUST_IS_UNTRUSTED_ROOT))
728 data->pasSigners[idxSigner].pasCertChain[i].fTrustedRoot = TRUE;
729 if (simpleChain->rgpElement[i]->TrustStatus.dwInfoStatus &
730 CERT_TRUST_IS_SELF_SIGNED)
731 data->pasSigners[idxSigner].pasCertChain[i].fSelfSigned = TRUE;
732 if (simpleChain->rgpElement[i]->TrustStatus.dwErrorStatus &
733 CERT_TRUST_IS_CYCLIC)
734 data->pasSigners[idxSigner].pasCertChain[i].fIsCyclic = TRUE;
740 static DWORD WINTRUST_TrustStatusToError(DWORD errorStatus)
744 if (errorStatus & CERT_TRUST_IS_NOT_SIGNATURE_VALID)
745 error = TRUST_E_CERT_SIGNATURE;
746 else if (errorStatus & CERT_TRUST_IS_UNTRUSTED_ROOT)
747 error = CERT_E_UNTRUSTEDROOT;
748 else if (errorStatus & CERT_TRUST_IS_NOT_TIME_VALID)
749 error = CERT_E_EXPIRED;
750 else if (errorStatus & CERT_TRUST_IS_NOT_TIME_NESTED)
751 error = CERT_E_VALIDITYPERIODNESTING;
752 else if (errorStatus & CERT_TRUST_IS_REVOKED)
753 error = CERT_E_REVOKED;
754 else if (errorStatus & CERT_TRUST_IS_OFFLINE_REVOCATION ||
755 errorStatus & CERT_TRUST_REVOCATION_STATUS_UNKNOWN)
756 error = CERT_E_REVOCATION_FAILURE;
757 else if (errorStatus & CERT_TRUST_IS_NOT_VALID_FOR_USAGE)
758 error = CERT_E_WRONG_USAGE;
759 else if (errorStatus & CERT_TRUST_IS_CYCLIC)
760 error = CERT_E_CHAINING;
761 else if (errorStatus & CERT_TRUST_INVALID_EXTENSION)
762 error = CERT_E_CRITICAL;
763 else if (errorStatus & CERT_TRUST_INVALID_POLICY_CONSTRAINTS)
764 error = CERT_E_INVALID_POLICY;
765 else if (errorStatus & CERT_TRUST_INVALID_BASIC_CONSTRAINTS)
766 error = TRUST_E_BASIC_CONSTRAINTS;
767 else if (errorStatus & CERT_TRUST_INVALID_NAME_CONSTRAINTS ||
768 errorStatus & CERT_TRUST_HAS_NOT_SUPPORTED_NAME_CONSTRAINT ||
769 errorStatus & CERT_TRUST_HAS_NOT_DEFINED_NAME_CONSTRAINT ||
770 errorStatus & CERT_TRUST_HAS_NOT_PERMITTED_NAME_CONSTRAINT ||
771 errorStatus & CERT_TRUST_HAS_EXCLUDED_NAME_CONSTRAINT)
772 error = CERT_E_INVALID_NAME;
773 else if (errorStatus & CERT_TRUST_NO_ISSUANCE_CHAIN_POLICY)
774 error = CERT_E_INVALID_POLICY;
775 else if (errorStatus)
777 FIXME("unknown error status %08x\n", errorStatus);
778 error = TRUST_E_SYSTEM_ERROR;
785 static DWORD WINTRUST_CopyChain(CRYPT_PROVIDER_DATA *data, DWORD signerIdx)
788 PCERT_SIMPLE_CHAIN simpleChain =
789 data->pasSigners[signerIdx].pChainContext->rgpChain[0];
791 data->pasSigners[signerIdx].pasCertChain[0].dwConfidence =
792 WINTRUST_TrustStatusToConfidence(
793 simpleChain->rgpElement[0]->TrustStatus.dwErrorStatus);
794 data->pasSigners[signerIdx].pasCertChain[0].pChainElement =
795 simpleChain->rgpElement[0];
797 for (i = 1; !err && i < simpleChain->cElement; i++)
799 if (data->psPfns->pfnAddCert2Chain(data, signerIdx, FALSE, 0,
800 simpleChain->rgpElement[i]->pCertContext))
802 data->pasSigners[signerIdx].pasCertChain[i].pChainElement =
803 simpleChain->rgpElement[i];
804 data->pasSigners[signerIdx].pasCertChain[i].dwConfidence =
805 WINTRUST_TrustStatusToConfidence(
806 simpleChain->rgpElement[i]->TrustStatus.dwErrorStatus);
809 err = GetLastError();
811 data->pasSigners[signerIdx].pasCertChain[simpleChain->cElement - 1].dwError
812 = WINTRUST_TrustStatusToError(
813 simpleChain->rgpElement[simpleChain->cElement - 1]->
814 TrustStatus.dwErrorStatus);
818 static void WINTRUST_CreateChainPolicyCreateInfo(
819 const CRYPT_PROVIDER_DATA *data, PWTD_GENERIC_CHAIN_POLICY_CREATE_INFO info,
820 PCERT_CHAIN_PARA chainPara)
822 chainPara->cbSize = sizeof(CERT_CHAIN_PARA);
823 if (data->pRequestUsage)
824 chainPara->RequestedUsage = *data->pRequestUsage;
827 chainPara->RequestedUsage.dwType = 0;
828 chainPara->RequestedUsage.Usage.cUsageIdentifier = 0;
830 info->u.cbSize = sizeof(WTD_GENERIC_CHAIN_POLICY_CREATE_INFO);
831 info->hChainEngine = NULL;
832 info->pChainPara = chainPara;
833 if (data->dwProvFlags & CPD_REVOCATION_CHECK_END_CERT)
834 info->dwFlags = CERT_CHAIN_REVOCATION_CHECK_END_CERT;
835 else if (data->dwProvFlags & CPD_REVOCATION_CHECK_CHAIN)
836 info->dwFlags = CERT_CHAIN_REVOCATION_CHECK_CHAIN;
837 else if (data->dwProvFlags & CPD_REVOCATION_CHECK_CHAIN_EXCLUDE_ROOT)
838 info->dwFlags = CERT_CHAIN_REVOCATION_CHECK_CHAIN_EXCLUDE_ROOT;
841 info->pvReserved = NULL;
844 static DWORD WINTRUST_CreateChainForSigner(CRYPT_PROVIDER_DATA *data,
845 DWORD signer, PWTD_GENERIC_CHAIN_POLICY_CREATE_INFO createInfo,
846 PCERT_CHAIN_PARA chainPara)
848 DWORD err = ERROR_SUCCESS;
849 HCERTSTORE store = NULL;
853 store = CertOpenStore(CERT_STORE_PROV_COLLECTION, 0, 0,
854 CERT_STORE_CREATE_NEW_FLAG, NULL);
859 for (i = 0; i < data->chStores; i++)
860 CertAddStoreToCollection(store, data->pahStores[i], 0, 0);
863 err = GetLastError();
867 /* Expect the end certificate for each signer to be the only cert in
870 if (data->pasSigners[signer].csCertChain)
874 /* Create a certificate chain for each signer */
875 ret = CertGetCertificateChain(createInfo->hChainEngine,
876 data->pasSigners[signer].pasCertChain[0].pCert,
877 &data->pasSigners[signer].sftVerifyAsOf, store,
878 chainPara, createInfo->dwFlags, createInfo->pvReserved,
879 &data->pasSigners[signer].pChainContext);
882 if (data->pasSigners[signer].pChainContext->cChain != 1)
884 FIXME("unimplemented for more than 1 simple chain\n");
889 if (!(err = WINTRUST_CopyChain(data, signer)))
891 if (data->psPfns->pfnCertCheckPolicy)
893 ret = data->psPfns->pfnCertCheckPolicy(data, signer,
896 err = GetLastError();
900 "no cert check policy, skipping policy check\n");
905 err = GetLastError();
907 CertCloseStore(store, 0);
912 HRESULT WINAPI WintrustCertificateTrust(CRYPT_PROVIDER_DATA *data)
916 TRACE("(%p)\n", data);
918 if (!data->csSigners)
919 err = TRUST_E_NOSIGNATURE;
923 WTD_GENERIC_CHAIN_POLICY_CREATE_INFO createInfo;
924 CERT_CHAIN_PARA chainPara;
926 WINTRUST_CreateChainPolicyCreateInfo(data, &createInfo, &chainPara);
928 for (i = 0; !err && i < data->csSigners; i++)
929 err = WINTRUST_CreateChainForSigner(data, i, &createInfo,
933 data->padwTrustStepErrors[TRUSTERROR_STEP_FINAL_CERTPROV] = err;
934 TRACE("returning %d (%08x)\n", !err ? S_OK : S_FALSE,
935 data->padwTrustStepErrors[TRUSTERROR_STEP_FINAL_CERTPROV]);
936 return !err ? S_OK : S_FALSE;
939 HRESULT WINAPI GenericChainCertificateTrust(CRYPT_PROVIDER_DATA *data)
942 WTD_GENERIC_CHAIN_POLICY_DATA *policyData =
943 data->pWintrustData->pPolicyCallbackData;
945 TRACE("(%p)\n", data);
947 if (policyData && policyData->u.cbSize !=
948 sizeof(WTD_GENERIC_CHAIN_POLICY_CREATE_INFO))
950 err = ERROR_INVALID_PARAMETER;
953 if (!data->csSigners)
954 err = TRUST_E_NOSIGNATURE;
958 WTD_GENERIC_CHAIN_POLICY_CREATE_INFO createInfo, *pCreateInfo;
959 CERT_CHAIN_PARA chainPara, *pChainPara;
963 pCreateInfo = policyData->pSignerChainInfo;
964 pChainPara = pCreateInfo->pChainPara;
968 WINTRUST_CreateChainPolicyCreateInfo(data, &createInfo, &chainPara);
969 pChainPara = &chainPara;
970 pCreateInfo = &createInfo;
973 for (i = 0; !err && i < data->csSigners; i++)
974 err = WINTRUST_CreateChainForSigner(data, i, pCreateInfo,
980 data->padwTrustStepErrors[TRUSTERROR_STEP_FINAL_CERTPROV] = err;
981 TRACE("returning %d (%08x)\n", !err ? S_OK : S_FALSE,
982 data->padwTrustStepErrors[TRUSTERROR_STEP_FINAL_CERTPROV]);
983 return !err ? S_OK : S_FALSE;
986 HRESULT WINAPI SoftpubAuthenticode(CRYPT_PROVIDER_DATA *data)
989 CERT_CHAIN_POLICY_STATUS policyStatus = { sizeof(policyStatus), 0 };
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)
999 policyStatus.dwError = TRUST_E_NOSIGNATURE;
1006 for (i = 0; ret && i < data->csSigners; i++)
1009 DWORD size = sizeof(hash);
1011 /* First make sure cert isn't disallowed */
1012 if ((ret = CertGetCertificateContextProperty(
1013 data->pasSigners[i].pasCertChain[0].pCert,
1014 CERT_SIGNATURE_HASH_PROP_ID, hash, &size)))
1016 static const WCHAR disallowedW[] =
1017 { 'D','i','s','a','l','l','o','w','e','d',0 };
1018 HCERTSTORE disallowed = CertOpenStore(CERT_STORE_PROV_SYSTEM_W,
1019 X509_ASN_ENCODING, 0, CERT_SYSTEM_STORE_CURRENT_USER,
1024 PCCERT_CONTEXT found = CertFindCertificateInStore(
1025 disallowed, X509_ASN_ENCODING, 0, CERT_FIND_SIGNATURE_HASH,
1030 /* Disallowed! Can't verify it. */
1031 policyStatus.dwError = TRUST_E_SUBJECT_NOT_TRUSTED;
1033 CertFreeCertificateContext(found);
1035 CertCloseStore(disallowed, 0);
1040 CERT_CHAIN_POLICY_PARA policyPara = { sizeof(policyPara), 0 };
1042 if (data->dwRegPolicySettings & WTPF_TRUSTTEST)
1043 policyPara.dwFlags |= CERT_CHAIN_POLICY_TRUST_TESTROOT_FLAG;
1044 if (data->dwRegPolicySettings & WTPF_TESTCANBEVALID)
1045 policyPara.dwFlags |= CERT_CHAIN_POLICY_ALLOW_TESTROOT_FLAG;
1046 if (data->dwRegPolicySettings & WTPF_IGNOREEXPIRATION)
1047 policyPara.dwFlags |=
1048 CERT_CHAIN_POLICY_IGNORE_NOT_TIME_VALID_FLAG |
1049 CERT_CHAIN_POLICY_IGNORE_CTL_NOT_TIME_VALID_FLAG |
1050 CERT_CHAIN_POLICY_IGNORE_NOT_TIME_NESTED_FLAG;
1051 if (data->dwRegPolicySettings & WTPF_IGNOREREVOKATION)
1052 policyPara.dwFlags |=
1053 CERT_CHAIN_POLICY_IGNORE_END_REV_UNKNOWN_FLAG |
1054 CERT_CHAIN_POLICY_IGNORE_CTL_SIGNER_REV_UNKNOWN_FLAG |
1055 CERT_CHAIN_POLICY_IGNORE_CA_REV_UNKNOWN_FLAG |
1056 CERT_CHAIN_POLICY_IGNORE_ROOT_REV_UNKNOWN_FLAG;
1057 CertVerifyCertificateChainPolicy(CERT_CHAIN_POLICY_AUTHENTICODE,
1058 data->pasSigners[i].pChainContext, &policyPara, &policyStatus);
1059 if (policyStatus.dwError != NO_ERROR)
1065 data->padwTrustStepErrors[TRUSTERROR_STEP_FINAL_POLICYPROV] =
1066 policyStatus.dwError;
1067 TRACE("returning %d (%08x)\n", ret ? S_OK : S_FALSE,
1068 data->padwTrustStepErrors[TRUSTERROR_STEP_FINAL_POLICYPROV]);
1069 return ret ? S_OK : S_FALSE;
1072 static HRESULT WINAPI WINTRUST_DefaultPolicy(CRYPT_PROVIDER_DATA *pProvData,
1073 DWORD dwStepError, DWORD dwRegPolicySettings, DWORD cSigner,
1074 PWTD_GENERIC_CHAIN_POLICY_SIGNER_INFO rgpSigner, void *pvPolicyArg)
1077 CERT_CHAIN_POLICY_STATUS policyStatus = { sizeof(policyStatus), 0 };
1079 for (i = 0; !policyStatus.dwError && i < cSigner; i++)
1081 CERT_CHAIN_POLICY_PARA policyPara = { sizeof(policyPara), 0 };
1083 if (dwRegPolicySettings & WTPF_IGNOREEXPIRATION)
1084 policyPara.dwFlags |=
1085 CERT_CHAIN_POLICY_IGNORE_NOT_TIME_VALID_FLAG |
1086 CERT_CHAIN_POLICY_IGNORE_CTL_NOT_TIME_VALID_FLAG |
1087 CERT_CHAIN_POLICY_IGNORE_NOT_TIME_NESTED_FLAG;
1088 if (dwRegPolicySettings & WTPF_IGNOREREVOKATION)
1089 policyPara.dwFlags |=
1090 CERT_CHAIN_POLICY_IGNORE_END_REV_UNKNOWN_FLAG |
1091 CERT_CHAIN_POLICY_IGNORE_CTL_SIGNER_REV_UNKNOWN_FLAG |
1092 CERT_CHAIN_POLICY_IGNORE_CA_REV_UNKNOWN_FLAG |
1093 CERT_CHAIN_POLICY_IGNORE_ROOT_REV_UNKNOWN_FLAG;
1094 CertVerifyCertificateChainPolicy(CERT_CHAIN_POLICY_BASE,
1095 rgpSigner[i].pChainContext, &policyPara, &policyStatus);
1097 return policyStatus.dwError;
1100 HRESULT WINAPI GenericChainFinalProv(CRYPT_PROVIDER_DATA *data)
1102 HRESULT err = NO_ERROR; /* not a typo, MS confused the types */
1103 WTD_GENERIC_CHAIN_POLICY_DATA *policyData =
1104 data->pWintrustData->pPolicyCallbackData;
1106 TRACE("(%p)\n", data);
1108 if (data->pWintrustData->dwUIChoice != WTD_UI_NONE)
1109 FIXME("unimplemented for UI choice %d\n",
1110 data->pWintrustData->dwUIChoice);
1111 if (!data->csSigners)
1112 err = TRUST_E_NOSIGNATURE;
1115 PFN_WTD_GENERIC_CHAIN_POLICY_CALLBACK policyCallback;
1117 WTD_GENERIC_CHAIN_POLICY_SIGNER_INFO *signers = NULL;
1121 policyCallback = policyData->pfnPolicyCallback;
1122 policyArg = policyData->pvPolicyArg;
1126 policyCallback = WINTRUST_DefaultPolicy;
1129 if (data->csSigners)
1133 signers = data->psPfns->pfnAlloc(
1134 data->csSigners * sizeof(WTD_GENERIC_CHAIN_POLICY_SIGNER_INFO));
1137 for (i = 0; i < data->csSigners; i++)
1139 signers[i].u.cbSize =
1140 sizeof(WTD_GENERIC_CHAIN_POLICY_SIGNER_INFO);
1141 signers[i].pChainContext =
1142 data->pasSigners[i].pChainContext;
1143 signers[i].dwSignerType = data->pasSigners[i].dwSignerType;
1144 signers[i].pMsgSignerInfo = data->pasSigners[i].psSigner;
1145 signers[i].dwError = data->pasSigners[i].dwError;
1146 if (data->pasSigners[i].csCounterSigners)
1147 FIXME("unimplemented for counter signers\n");
1148 signers[i].cCounterSigner = 0;
1149 signers[i].rgpCounterSigner = NULL;
1153 err = ERROR_OUTOFMEMORY;
1155 if (err == NO_ERROR)
1156 err = policyCallback(data, TRUSTERROR_STEP_FINAL_POLICYPROV,
1157 data->dwRegPolicySettings, data->csSigners, signers, policyArg);
1158 data->psPfns->pfnFree(signers);
1160 if (err != NO_ERROR)
1161 data->padwTrustStepErrors[TRUSTERROR_STEP_FINAL_POLICYPROV] = err;
1162 TRACE("returning %d (%08x)\n", err == NO_ERROR ? S_OK : S_FALSE,
1163 data->padwTrustStepErrors[TRUSTERROR_STEP_FINAL_POLICYPROV]);
1164 return err == NO_ERROR ? S_OK : S_FALSE;
1167 HRESULT WINAPI SoftpubCleanup(CRYPT_PROVIDER_DATA *data)
1171 for (i = 0; i < data->csSigners; i++)
1173 for (j = 0; j < data->pasSigners[i].csCertChain; j++)
1174 CertFreeCertificateContext(data->pasSigners[i].pasCertChain[j].pCert);
1175 data->psPfns->pfnFree(data->pasSigners[i].pasCertChain);
1176 data->psPfns->pfnFree(data->pasSigners[i].psSigner);
1177 CertFreeCertificateChain(data->pasSigners[i].pChainContext);
1179 data->psPfns->pfnFree(data->pasSigners);
1181 for (i = 0; i < data->chStores; i++)
1182 CertCloseStore(data->pahStores[i], 0);
1183 data->psPfns->pfnFree(data->pahStores);
1187 data->psPfns->pfnFree(data->u.pPDSip->pSip);
1188 data->psPfns->pfnFree(data->u.pPDSip->pCATSip);
1189 data->psPfns->pfnFree(data->u.pPDSip->psSipSubjectInfo);
1190 data->psPfns->pfnFree(data->u.pPDSip->psSipCATSubjectInfo);
1191 data->psPfns->pfnFree(data->u.pPDSip->psIndirectData);
1194 CryptMsgClose(data->hMsg);
1196 if (data->fOpenedFile &&
1197 data->pWintrustData->dwUnionChoice == WTD_CHOICE_FILE)
1198 CloseHandle(data->pWintrustData->u.pFile->hFile);
1203 HRESULT WINAPI HTTPSCertificateTrust(CRYPT_PROVIDER_DATA *data)
1205 FIXME("(%p)\n", data);
1209 HRESULT WINAPI HTTPSFinalProv(CRYPT_PROVIDER_DATA *data)
1211 FIXME("(%p)\n", data);