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))
121 LARGE_INTEGER fileSize;
122 DWORD sipError = GetLastError();
124 /* Special case for empty files: the error is expected to be
125 * TRUST_E_SUBJECT_FORM_UNKNOWN, rather than whatever
126 * CryptSIPRetrieveSubjectGuid returns.
128 if (GetFileSizeEx(data->pWintrustData->u.pFile->hFile, &fileSize)
129 && !fileSize.QuadPart)
130 err = TRUST_E_SUBJECT_FORM_UNKNOWN;
136 data->u.pPDSip->gSubject = *data->pWintrustData->u.pFile->pgKnownSubject;
137 TRACE("returning %d\n", err);
141 /* Assumes data->u.pPDSip exists, and its gSubject member set.
142 * Allocates data->u.pPDSip->pSip and loads it, if possible.
144 static DWORD SOFTPUB_GetSIP(CRYPT_PROVIDER_DATA *data)
146 DWORD err = ERROR_SUCCESS;
148 data->u.pPDSip->pSip = data->psPfns->pfnAlloc(sizeof(SIP_DISPATCH_INFO));
149 if (data->u.pPDSip->pSip)
151 if (!CryptSIPLoad(&data->u.pPDSip->gSubject, 0, data->u.pPDSip->pSip))
152 err = GetLastError();
155 err = ERROR_OUTOFMEMORY;
156 TRACE("returning %d\n", err);
160 /* Assumes data->u.pPDSip has been loaded, and data->u.pPDSip->pSip allocated.
161 * Calls data->u.pPDSip->pSip->pfGet to construct data->hMsg.
163 static DWORD SOFTPUB_GetMessageFromFile(CRYPT_PROVIDER_DATA *data, HANDLE file,
166 DWORD err = ERROR_SUCCESS;
171 data->u.pPDSip->psSipSubjectInfo =
172 data->psPfns->pfnAlloc(sizeof(SIP_SUBJECTINFO));
173 if (!data->u.pPDSip->psSipSubjectInfo)
174 return ERROR_OUTOFMEMORY;
176 data->u.pPDSip->psSipSubjectInfo->cbSize = sizeof(SIP_SUBJECTINFO);
177 data->u.pPDSip->psSipSubjectInfo->pgSubjectType = &data->u.pPDSip->gSubject;
178 data->u.pPDSip->psSipSubjectInfo->hFile = file;
179 data->u.pPDSip->psSipSubjectInfo->pwsFileName = filePath;
180 data->u.pPDSip->psSipSubjectInfo->hProv = data->hProv;
181 ret = data->u.pPDSip->pSip->pfGet(data->u.pPDSip->psSipSubjectInfo,
182 &data->dwEncoding, 0, &size, 0);
184 return TRUST_E_NOSIGNATURE;
186 buf = data->psPfns->pfnAlloc(size);
188 return ERROR_OUTOFMEMORY;
190 ret = data->u.pPDSip->pSip->pfGet(data->u.pPDSip->psSipSubjectInfo,
191 &data->dwEncoding, 0, &size, buf);
194 data->hMsg = CryptMsgOpenToDecode(data->dwEncoding, 0, 0, data->hProv,
198 ret = CryptMsgUpdate(data->hMsg, buf, size, TRUE);
200 err = GetLastError();
204 err = GetLastError();
206 data->psPfns->pfnFree(buf);
207 TRACE("returning %d\n", err);
211 static DWORD SOFTPUB_CreateStoreFromMessage(CRYPT_PROVIDER_DATA *data)
213 DWORD err = ERROR_SUCCESS;
216 store = CertOpenStore(CERT_STORE_PROV_MSG, data->dwEncoding,
217 data->hProv, CERT_STORE_NO_CRYPT_RELEASE_FLAG, data->hMsg);
220 if (!data->psPfns->pfnAddStore2Chain(data, store))
221 err = GetLastError();
222 CertCloseStore(store, 0);
225 err = GetLastError();
226 TRACE("returning %d\n", err);
230 static DWORD SOFTPUB_DecodeInnerContent(CRYPT_PROVIDER_DATA *data)
233 DWORD size, err = ERROR_SUCCESS;
237 ret = CryptMsgGetParam(data->hMsg, CMSG_INNER_CONTENT_TYPE_PARAM, 0, NULL,
241 err = GetLastError();
244 oid = data->psPfns->pfnAlloc(size);
247 err = ERROR_OUTOFMEMORY;
250 ret = CryptMsgGetParam(data->hMsg, CMSG_INNER_CONTENT_TYPE_PARAM, 0, oid,
254 err = GetLastError();
257 ret = CryptMsgGetParam(data->hMsg, CMSG_CONTENT_PARAM, 0, NULL, &size);
260 err = GetLastError();
263 buf = data->psPfns->pfnAlloc(size);
266 err = ERROR_OUTOFMEMORY;
269 ret = CryptMsgGetParam(data->hMsg, CMSG_CONTENT_PARAM, 0, buf, &size);
272 err = GetLastError();
275 ret = CryptDecodeObject(data->dwEncoding, oid, buf, size, 0, NULL, &size);
278 err = GetLastError();
281 data->u.pPDSip->psIndirectData = data->psPfns->pfnAlloc(size);
282 if (!data->u.pPDSip->psIndirectData)
284 err = ERROR_OUTOFMEMORY;
287 ret = CryptDecodeObject(data->dwEncoding, oid, buf, size, 0,
288 data->u.pPDSip->psIndirectData, &size);
290 err = GetLastError();
293 TRACE("returning %d\n", err);
294 data->psPfns->pfnFree(oid);
295 data->psPfns->pfnFree(buf);
299 static DWORD SOFTPUB_LoadCertMessage(CRYPT_PROVIDER_DATA *data)
301 DWORD err = ERROR_SUCCESS;
303 if (data->pWintrustData->u.pCert &&
304 WVT_IS_CBSTRUCT_GT_MEMBEROFFSET(WINTRUST_CERT_INFO,
305 data->pWintrustData->u.pCert->cbStruct, psCertContext))
309 CRYPT_PROVIDER_SGNR signer = { sizeof(signer), { 0 } };
313 /* Add a signer with nothing but the time to verify, so we can
316 if (WVT_ISINSTRUCT(WINTRUST_CERT_INFO,
317 data->pWintrustData->u.pCert->cbStruct, psftVerifyAsOf) &&
318 data->pWintrustData->u.pCert->psftVerifyAsOf)
319 data->sftSystemTime = signer.sftVerifyAsOf;
324 GetSystemTime(&sysTime);
325 SystemTimeToFileTime(&sysTime, &signer.sftVerifyAsOf);
327 ret = data->psPfns->pfnAddSgnr2Chain(data, FALSE, 0, &signer);
330 ret = data->psPfns->pfnAddCert2Chain(data, 0, FALSE, 0,
331 data->pWintrustData->u.pCert->psCertContext);
332 if (WVT_ISINSTRUCT(WINTRUST_CERT_INFO,
333 data->pWintrustData->u.pCert->cbStruct, pahStores))
335 ret && i < data->pWintrustData->u.pCert->chStores; i++)
336 ret = data->psPfns->pfnAddStore2Chain(data,
337 data->pWintrustData->u.pCert->pahStores[i]);
340 err = GetLastError();
344 err = ERROR_INVALID_PARAMETER;
348 static DWORD SOFTPUB_LoadFileMessage(CRYPT_PROVIDER_DATA *data)
350 DWORD err = ERROR_SUCCESS;
352 if (!data->pWintrustData->u.pFile)
354 err = ERROR_INVALID_PARAMETER;
357 err = SOFTPUB_OpenFile(data);
360 err = SOFTPUB_GetFileSubject(data);
363 err = SOFTPUB_GetSIP(data);
366 err = SOFTPUB_GetMessageFromFile(data, data->pWintrustData->u.pFile->hFile,
367 data->pWintrustData->u.pFile->pcwszFilePath);
370 err = SOFTPUB_CreateStoreFromMessage(data);
373 err = SOFTPUB_DecodeInnerContent(data);
376 if (err && data->fOpenedFile && data->pWintrustData->u.pFile)
378 /* The caller won't expect the file to be open on failure, so close it.
380 CloseHandle(data->pWintrustData->u.pFile->hFile);
381 data->pWintrustData->u.pFile->hFile = INVALID_HANDLE_VALUE;
382 data->fOpenedFile = FALSE;
387 static DWORD SOFTPUB_LoadCatalogMessage(CRYPT_PROVIDER_DATA *data)
390 HANDLE catalog = INVALID_HANDLE_VALUE;
392 if (!data->pWintrustData->u.pCatalog)
394 SetLastError(ERROR_INVALID_PARAMETER);
397 catalog = CreateFileW(data->pWintrustData->u.pCatalog->pcwszCatalogFilePath,
398 GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL,
400 if (catalog == INVALID_HANDLE_VALUE)
401 return GetLastError();
402 if (!CryptSIPRetrieveSubjectGuid(
403 data->pWintrustData->u.pCatalog->pcwszCatalogFilePath, catalog,
404 &data->u.pPDSip->gSubject))
406 err = GetLastError();
409 err = SOFTPUB_GetSIP(data);
412 err = SOFTPUB_GetMessageFromFile(data, catalog,
413 data->pWintrustData->u.pCatalog->pcwszCatalogFilePath);
416 err = SOFTPUB_CreateStoreFromMessage(data);
419 err = SOFTPUB_DecodeInnerContent(data);
420 /* FIXME: this loads the catalog file, but doesn't validate the member. */
422 CloseHandle(catalog);
426 HRESULT WINAPI SoftpubLoadMessage(CRYPT_PROVIDER_DATA *data)
428 DWORD err = ERROR_SUCCESS;
430 TRACE("(%p)\n", data);
432 if (!data->padwTrustStepErrors)
435 switch (data->pWintrustData->dwUnionChoice)
437 case WTD_CHOICE_CERT:
438 err = SOFTPUB_LoadCertMessage(data);
440 case WTD_CHOICE_FILE:
441 err = SOFTPUB_LoadFileMessage(data);
443 case WTD_CHOICE_CATALOG:
444 err = SOFTPUB_LoadCatalogMessage(data);
447 FIXME("unimplemented for %d\n", data->pWintrustData->dwUnionChoice);
448 err = ERROR_INVALID_PARAMETER;
452 data->padwTrustStepErrors[TRUSTERROR_STEP_FINAL_OBJPROV] = err;
453 TRACE("returning %d (%08x)\n", !err ? S_OK : S_FALSE,
454 data->padwTrustStepErrors[TRUSTERROR_STEP_FINAL_OBJPROV]);
455 return !err ? S_OK : S_FALSE;
458 static CMSG_SIGNER_INFO *WINTRUST_GetSigner(CRYPT_PROVIDER_DATA *data,
462 CMSG_SIGNER_INFO *signerInfo = NULL;
465 ret = CryptMsgGetParam(data->hMsg, CMSG_SIGNER_INFO_PARAM, signerIdx,
469 signerInfo = data->psPfns->pfnAlloc(size);
472 ret = CryptMsgGetParam(data->hMsg, CMSG_SIGNER_INFO_PARAM,
473 signerIdx, signerInfo, &size);
476 data->psPfns->pfnFree(signerInfo);
481 SetLastError(ERROR_OUTOFMEMORY);
486 static BOOL WINTRUST_GetTimeFromCounterSigner(
487 const CMSG_CMS_SIGNER_INFO *counterSignerInfo, FILETIME *time)
490 BOOL foundTimeStamp = FALSE;
492 for (i = 0; !foundTimeStamp && i < counterSignerInfo->AuthAttrs.cAttr; i++)
494 if (!strcmp(counterSignerInfo->AuthAttrs.rgAttr[i].pszObjId,
495 szOID_RSA_signingTime))
497 const CRYPT_ATTRIBUTE *attr =
498 &counterSignerInfo->AuthAttrs.rgAttr[i];
501 for (j = 0; !foundTimeStamp && j < attr->cValue; j++)
503 static const DWORD encoding = X509_ASN_ENCODING |
505 DWORD size = sizeof(FILETIME);
507 foundTimeStamp = CryptDecodeObjectEx(encoding,
509 attr->rgValue[j].pbData, attr->rgValue[j].cbData, 0, NULL,
514 return foundTimeStamp;
517 static LPCSTR filetime_to_str(const FILETIME *time)
519 static char date[80];
520 char dateFmt[80]; /* sufficient for all versions of LOCALE_SSHORTDATE */
523 if (!time) return NULL;
525 GetLocaleInfoA(LOCALE_SYSTEM_DEFAULT, LOCALE_SSHORTDATE, dateFmt,
526 sizeof(dateFmt) / sizeof(dateFmt[0]));
527 FileTimeToSystemTime(time, &sysTime);
528 GetDateFormatA(LOCALE_SYSTEM_DEFAULT, 0, &sysTime, dateFmt, date,
529 sizeof(date) / sizeof(date[0]));
533 static FILETIME WINTRUST_GetTimeFromSigner(const CRYPT_PROVIDER_DATA *data,
534 const CMSG_SIGNER_INFO *signerInfo)
538 BOOL foundTimeStamp = FALSE;
540 for (i = 0; !foundTimeStamp && i < signerInfo->UnauthAttrs.cAttr; i++)
542 if (!strcmp(signerInfo->UnauthAttrs.rgAttr[i].pszObjId,
543 szOID_RSA_counterSign))
545 const CRYPT_ATTRIBUTE *attr = &signerInfo->UnauthAttrs.rgAttr[i];
548 for (j = 0; j < attr->cValue; j++)
550 static const DWORD encoding = X509_ASN_ENCODING |
552 CMSG_CMS_SIGNER_INFO *counterSignerInfo;
554 BOOL ret = CryptDecodeObjectEx(encoding, CMS_SIGNER_INFO,
555 attr->rgValue[j].pbData, attr->rgValue[j].cbData,
556 CRYPT_DECODE_ALLOC_FLAG, NULL, &counterSignerInfo, &size);
559 /* FIXME: need to verify countersigner signature too */
560 foundTimeStamp = WINTRUST_GetTimeFromCounterSigner(
561 counterSignerInfo, &time);
562 LocalFree(counterSignerInfo);
569 TRACE("returning system time %s\n",
570 filetime_to_str(&data->sftSystemTime));
571 time = data->sftSystemTime;
574 TRACE("returning time from message %s\n", filetime_to_str(&time));
578 static DWORD WINTRUST_SaveSigner(CRYPT_PROVIDER_DATA *data, DWORD signerIdx)
581 CMSG_SIGNER_INFO *signerInfo = WINTRUST_GetSigner(data, signerIdx);
585 CRYPT_PROVIDER_SGNR sgnr = { sizeof(sgnr), { 0 } };
587 sgnr.psSigner = signerInfo;
588 sgnr.sftVerifyAsOf = WINTRUST_GetTimeFromSigner(data, signerInfo);
589 if (!data->psPfns->pfnAddSgnr2Chain(data, FALSE, signerIdx, &sgnr))
590 err = GetLastError();
595 err = GetLastError();
599 static CERT_INFO *WINTRUST_GetSignerCertInfo(CRYPT_PROVIDER_DATA *data,
603 CERT_INFO *certInfo = NULL;
606 ret = CryptMsgGetParam(data->hMsg, CMSG_SIGNER_CERT_INFO_PARAM, signerIdx,
610 certInfo = data->psPfns->pfnAlloc(size);
613 ret = CryptMsgGetParam(data->hMsg, CMSG_SIGNER_CERT_INFO_PARAM,
614 signerIdx, certInfo, &size);
617 data->psPfns->pfnFree(certInfo);
622 SetLastError(ERROR_OUTOFMEMORY);
627 static DWORD WINTRUST_VerifySigner(CRYPT_PROVIDER_DATA *data, DWORD signerIdx)
630 CERT_INFO *certInfo = WINTRUST_GetSignerCertInfo(data, signerIdx);
634 PCCERT_CONTEXT subject = CertGetSubjectCertificateFromStore(
635 data->pahStores[0], data->dwEncoding, certInfo);
639 CMSG_CTRL_VERIFY_SIGNATURE_EX_PARA para = { sizeof(para), 0,
640 signerIdx, CMSG_VERIFY_SIGNER_CERT, (LPVOID)subject };
642 if (!CryptMsgControl(data->hMsg, 0, CMSG_CTRL_VERIFY_SIGNATURE_EX,
644 err = TRUST_E_CERT_SIGNATURE;
647 data->psPfns->pfnAddCert2Chain(data, signerIdx, FALSE, 0,
651 CertFreeCertificateContext(subject);
654 err = TRUST_E_NO_SIGNER_CERT;
655 data->psPfns->pfnFree(certInfo);
658 err = GetLastError();
662 HRESULT WINAPI SoftpubLoadSignature(CRYPT_PROVIDER_DATA *data)
666 TRACE("(%p)\n", data);
668 if (!data->padwTrustStepErrors)
673 DWORD signerCount, size;
675 size = sizeof(signerCount);
676 if (CryptMsgGetParam(data->hMsg, CMSG_SIGNER_COUNT_PARAM, 0,
677 &signerCount, &size))
682 for (i = 0; !err && i < signerCount; i++)
684 if (!(err = WINTRUST_SaveSigner(data, i)))
685 err = WINTRUST_VerifySigner(data, i);
689 err = TRUST_E_NOSIGNATURE;
694 data->padwTrustStepErrors[TRUSTERROR_STEP_FINAL_SIGPROV] = err;
695 return !err ? S_OK : S_FALSE;
698 static DWORD WINTRUST_TrustStatusToConfidence(DWORD errorStatus)
700 DWORD confidence = 0;
703 if (!(errorStatus & CERT_TRUST_IS_NOT_SIGNATURE_VALID))
704 confidence |= CERT_CONFIDENCE_SIG;
705 if (!(errorStatus & CERT_TRUST_IS_NOT_TIME_VALID))
706 confidence |= CERT_CONFIDENCE_TIME;
707 if (!(errorStatus & CERT_TRUST_IS_NOT_TIME_NESTED))
708 confidence |= CERT_CONFIDENCE_TIMENEST;
712 BOOL WINAPI SoftpubCheckCert(CRYPT_PROVIDER_DATA *data, DWORD idxSigner,
713 BOOL fCounterSignerChain, DWORD idxCounterSigner)
717 TRACE("(%p, %d, %d, %d)\n", data, idxSigner, fCounterSignerChain,
720 if (fCounterSignerChain)
722 FIXME("unimplemented for counter signers\n");
727 PCERT_SIMPLE_CHAIN simpleChain =
728 data->pasSigners[idxSigner].pChainContext->rgpChain[0];
732 for (i = 0; i < simpleChain->cElement; i++)
735 data->pasSigners[idxSigner].pasCertChain[i].dwConfidence =
736 WINTRUST_TrustStatusToConfidence(
737 simpleChain->rgpElement[i]->TrustStatus.dwErrorStatus);
738 /* Set additional flags */
739 if (!(simpleChain->rgpElement[i]->TrustStatus.dwErrorStatus &
740 CERT_TRUST_IS_UNTRUSTED_ROOT))
741 data->pasSigners[idxSigner].pasCertChain[i].fTrustedRoot = TRUE;
742 if (simpleChain->rgpElement[i]->TrustStatus.dwInfoStatus &
743 CERT_TRUST_IS_SELF_SIGNED)
744 data->pasSigners[idxSigner].pasCertChain[i].fSelfSigned = TRUE;
745 if (simpleChain->rgpElement[i]->TrustStatus.dwErrorStatus &
746 CERT_TRUST_IS_CYCLIC)
747 data->pasSigners[idxSigner].pasCertChain[i].fIsCyclic = TRUE;
753 static DWORD WINTRUST_TrustStatusToError(DWORD errorStatus)
757 if (errorStatus & CERT_TRUST_IS_NOT_SIGNATURE_VALID)
758 error = TRUST_E_CERT_SIGNATURE;
759 else if (errorStatus & CERT_TRUST_IS_UNTRUSTED_ROOT)
760 error = CERT_E_UNTRUSTEDROOT;
761 else if (errorStatus & CERT_TRUST_IS_NOT_TIME_VALID)
762 error = CERT_E_EXPIRED;
763 else if (errorStatus & CERT_TRUST_IS_NOT_TIME_NESTED)
764 error = CERT_E_VALIDITYPERIODNESTING;
765 else if (errorStatus & CERT_TRUST_IS_REVOKED)
766 error = CERT_E_REVOKED;
767 else if (errorStatus & CERT_TRUST_IS_OFFLINE_REVOCATION ||
768 errorStatus & CERT_TRUST_REVOCATION_STATUS_UNKNOWN)
769 error = CERT_E_REVOCATION_FAILURE;
770 else if (errorStatus & CERT_TRUST_IS_NOT_VALID_FOR_USAGE)
771 error = CERT_E_WRONG_USAGE;
772 else if (errorStatus & CERT_TRUST_IS_CYCLIC)
773 error = CERT_E_CHAINING;
774 else if (errorStatus & CERT_TRUST_INVALID_EXTENSION)
775 error = CERT_E_CRITICAL;
776 else if (errorStatus & CERT_TRUST_INVALID_POLICY_CONSTRAINTS)
777 error = CERT_E_INVALID_POLICY;
778 else if (errorStatus & CERT_TRUST_INVALID_BASIC_CONSTRAINTS)
779 error = TRUST_E_BASIC_CONSTRAINTS;
780 else if (errorStatus & CERT_TRUST_INVALID_NAME_CONSTRAINTS ||
781 errorStatus & CERT_TRUST_HAS_NOT_SUPPORTED_NAME_CONSTRAINT ||
782 errorStatus & CERT_TRUST_HAS_NOT_DEFINED_NAME_CONSTRAINT ||
783 errorStatus & CERT_TRUST_HAS_NOT_PERMITTED_NAME_CONSTRAINT ||
784 errorStatus & CERT_TRUST_HAS_EXCLUDED_NAME_CONSTRAINT)
785 error = CERT_E_INVALID_NAME;
786 else if (errorStatus & CERT_TRUST_NO_ISSUANCE_CHAIN_POLICY)
787 error = CERT_E_INVALID_POLICY;
788 else if (errorStatus)
790 FIXME("unknown error status %08x\n", errorStatus);
791 error = TRUST_E_SYSTEM_ERROR;
798 static DWORD WINTRUST_CopyChain(CRYPT_PROVIDER_DATA *data, DWORD signerIdx)
801 PCERT_SIMPLE_CHAIN simpleChain =
802 data->pasSigners[signerIdx].pChainContext->rgpChain[0];
804 data->pasSigners[signerIdx].pasCertChain[0].dwConfidence =
805 WINTRUST_TrustStatusToConfidence(
806 simpleChain->rgpElement[0]->TrustStatus.dwErrorStatus);
807 data->pasSigners[signerIdx].pasCertChain[0].pChainElement =
808 simpleChain->rgpElement[0];
810 for (i = 1; !err && i < simpleChain->cElement; i++)
812 if (data->psPfns->pfnAddCert2Chain(data, signerIdx, FALSE, 0,
813 simpleChain->rgpElement[i]->pCertContext))
815 data->pasSigners[signerIdx].pasCertChain[i].pChainElement =
816 simpleChain->rgpElement[i];
817 data->pasSigners[signerIdx].pasCertChain[i].dwConfidence =
818 WINTRUST_TrustStatusToConfidence(
819 simpleChain->rgpElement[i]->TrustStatus.dwErrorStatus);
822 err = GetLastError();
824 data->pasSigners[signerIdx].pasCertChain[simpleChain->cElement - 1].dwError
825 = WINTRUST_TrustStatusToError(
826 simpleChain->rgpElement[simpleChain->cElement - 1]->
827 TrustStatus.dwErrorStatus);
831 static void WINTRUST_CreateChainPolicyCreateInfo(
832 const CRYPT_PROVIDER_DATA *data, PWTD_GENERIC_CHAIN_POLICY_CREATE_INFO info,
833 PCERT_CHAIN_PARA chainPara)
835 chainPara->cbSize = sizeof(CERT_CHAIN_PARA);
836 if (data->pRequestUsage)
837 chainPara->RequestedUsage = *data->pRequestUsage;
840 chainPara->RequestedUsage.dwType = 0;
841 chainPara->RequestedUsage.Usage.cUsageIdentifier = 0;
843 info->u.cbSize = sizeof(WTD_GENERIC_CHAIN_POLICY_CREATE_INFO);
844 info->hChainEngine = NULL;
845 info->pChainPara = chainPara;
846 if (data->dwProvFlags & CPD_REVOCATION_CHECK_END_CERT)
847 info->dwFlags = CERT_CHAIN_REVOCATION_CHECK_END_CERT;
848 else if (data->dwProvFlags & CPD_REVOCATION_CHECK_CHAIN)
849 info->dwFlags = CERT_CHAIN_REVOCATION_CHECK_CHAIN;
850 else if (data->dwProvFlags & CPD_REVOCATION_CHECK_CHAIN_EXCLUDE_ROOT)
851 info->dwFlags = CERT_CHAIN_REVOCATION_CHECK_CHAIN_EXCLUDE_ROOT;
854 info->pvReserved = NULL;
857 static DWORD WINTRUST_CreateChainForSigner(CRYPT_PROVIDER_DATA *data,
858 DWORD signer, PWTD_GENERIC_CHAIN_POLICY_CREATE_INFO createInfo,
859 PCERT_CHAIN_PARA chainPara)
861 DWORD err = ERROR_SUCCESS;
862 HCERTSTORE store = NULL;
866 store = CertOpenStore(CERT_STORE_PROV_COLLECTION, 0, 0,
867 CERT_STORE_CREATE_NEW_FLAG, NULL);
872 for (i = 0; i < data->chStores; i++)
873 CertAddStoreToCollection(store, data->pahStores[i], 0, 0);
876 err = GetLastError();
880 /* Expect the end certificate for each signer to be the only cert in
883 if (data->pasSigners[signer].csCertChain)
887 /* Create a certificate chain for each signer */
888 ret = CertGetCertificateChain(createInfo->hChainEngine,
889 data->pasSigners[signer].pasCertChain[0].pCert,
890 &data->pasSigners[signer].sftVerifyAsOf, store,
891 chainPara, createInfo->dwFlags, createInfo->pvReserved,
892 &data->pasSigners[signer].pChainContext);
895 if (data->pasSigners[signer].pChainContext->cChain != 1)
897 FIXME("unimplemented for more than 1 simple chain\n");
902 if (!(err = WINTRUST_CopyChain(data, signer)))
904 if (data->psPfns->pfnCertCheckPolicy)
906 ret = data->psPfns->pfnCertCheckPolicy(data, signer,
909 err = GetLastError();
913 "no cert check policy, skipping policy check\n");
918 err = GetLastError();
920 CertCloseStore(store, 0);
925 HRESULT WINAPI WintrustCertificateTrust(CRYPT_PROVIDER_DATA *data)
929 TRACE("(%p)\n", data);
931 if (!data->csSigners)
932 err = TRUST_E_NOSIGNATURE;
936 WTD_GENERIC_CHAIN_POLICY_CREATE_INFO createInfo;
937 CERT_CHAIN_PARA chainPara;
939 WINTRUST_CreateChainPolicyCreateInfo(data, &createInfo, &chainPara);
941 for (i = 0; !err && i < data->csSigners; i++)
942 err = WINTRUST_CreateChainForSigner(data, i, &createInfo,
946 data->padwTrustStepErrors[TRUSTERROR_STEP_FINAL_CERTPROV] = err;
947 TRACE("returning %d (%08x)\n", !err ? S_OK : S_FALSE,
948 data->padwTrustStepErrors[TRUSTERROR_STEP_FINAL_CERTPROV]);
949 return !err ? S_OK : S_FALSE;
952 HRESULT WINAPI GenericChainCertificateTrust(CRYPT_PROVIDER_DATA *data)
955 WTD_GENERIC_CHAIN_POLICY_DATA *policyData =
956 data->pWintrustData->pPolicyCallbackData;
958 TRACE("(%p)\n", data);
960 if (policyData && policyData->u.cbSize !=
961 sizeof(WTD_GENERIC_CHAIN_POLICY_CREATE_INFO))
963 err = ERROR_INVALID_PARAMETER;
966 if (!data->csSigners)
967 err = TRUST_E_NOSIGNATURE;
971 WTD_GENERIC_CHAIN_POLICY_CREATE_INFO createInfo, *pCreateInfo;
972 CERT_CHAIN_PARA chainPara, *pChainPara;
976 pCreateInfo = policyData->pSignerChainInfo;
977 pChainPara = pCreateInfo->pChainPara;
981 WINTRUST_CreateChainPolicyCreateInfo(data, &createInfo, &chainPara);
982 pChainPara = &chainPara;
983 pCreateInfo = &createInfo;
986 for (i = 0; !err && i < data->csSigners; i++)
987 err = WINTRUST_CreateChainForSigner(data, i, pCreateInfo,
993 data->padwTrustStepErrors[TRUSTERROR_STEP_FINAL_CERTPROV] = err;
994 TRACE("returning %d (%08x)\n", !err ? S_OK : S_FALSE,
995 data->padwTrustStepErrors[TRUSTERROR_STEP_FINAL_CERTPROV]);
996 return !err ? S_OK : S_FALSE;
999 HRESULT WINAPI SoftpubAuthenticode(CRYPT_PROVIDER_DATA *data)
1002 CERT_CHAIN_POLICY_STATUS policyStatus = { sizeof(policyStatus), 0 };
1004 TRACE("(%p)\n", data);
1006 if (data->pWintrustData->dwUIChoice != WTD_UI_NONE)
1007 FIXME("unimplemented for UI choice %d\n",
1008 data->pWintrustData->dwUIChoice);
1009 if (!data->csSigners)
1012 policyStatus.dwError = TRUST_E_NOSIGNATURE;
1019 for (i = 0; ret && i < data->csSigners; i++)
1022 DWORD size = sizeof(hash);
1024 /* First make sure cert isn't disallowed */
1025 if ((ret = CertGetCertificateContextProperty(
1026 data->pasSigners[i].pasCertChain[0].pCert,
1027 CERT_SIGNATURE_HASH_PROP_ID, hash, &size)))
1029 static const WCHAR disallowedW[] =
1030 { 'D','i','s','a','l','l','o','w','e','d',0 };
1031 HCERTSTORE disallowed = CertOpenStore(CERT_STORE_PROV_SYSTEM_W,
1032 X509_ASN_ENCODING, 0, CERT_SYSTEM_STORE_CURRENT_USER,
1037 PCCERT_CONTEXT found = CertFindCertificateInStore(
1038 disallowed, X509_ASN_ENCODING, 0, CERT_FIND_SIGNATURE_HASH,
1043 /* Disallowed! Can't verify it. */
1044 policyStatus.dwError = TRUST_E_SUBJECT_NOT_TRUSTED;
1046 CertFreeCertificateContext(found);
1048 CertCloseStore(disallowed, 0);
1053 CERT_CHAIN_POLICY_PARA policyPara = { sizeof(policyPara), 0 };
1055 if (data->dwRegPolicySettings & WTPF_TRUSTTEST)
1056 policyPara.dwFlags |= CERT_CHAIN_POLICY_TRUST_TESTROOT_FLAG;
1057 if (data->dwRegPolicySettings & WTPF_TESTCANBEVALID)
1058 policyPara.dwFlags |= CERT_CHAIN_POLICY_ALLOW_TESTROOT_FLAG;
1059 if (data->dwRegPolicySettings & WTPF_IGNOREEXPIRATION)
1060 policyPara.dwFlags |=
1061 CERT_CHAIN_POLICY_IGNORE_NOT_TIME_VALID_FLAG |
1062 CERT_CHAIN_POLICY_IGNORE_CTL_NOT_TIME_VALID_FLAG |
1063 CERT_CHAIN_POLICY_IGNORE_NOT_TIME_NESTED_FLAG;
1064 if (data->dwRegPolicySettings & WTPF_IGNOREREVOKATION)
1065 policyPara.dwFlags |=
1066 CERT_CHAIN_POLICY_IGNORE_END_REV_UNKNOWN_FLAG |
1067 CERT_CHAIN_POLICY_IGNORE_CTL_SIGNER_REV_UNKNOWN_FLAG |
1068 CERT_CHAIN_POLICY_IGNORE_CA_REV_UNKNOWN_FLAG |
1069 CERT_CHAIN_POLICY_IGNORE_ROOT_REV_UNKNOWN_FLAG;
1070 CertVerifyCertificateChainPolicy(CERT_CHAIN_POLICY_AUTHENTICODE,
1071 data->pasSigners[i].pChainContext, &policyPara, &policyStatus);
1072 if (policyStatus.dwError != NO_ERROR)
1078 data->padwTrustStepErrors[TRUSTERROR_STEP_FINAL_POLICYPROV] =
1079 policyStatus.dwError;
1080 TRACE("returning %d (%08x)\n", ret ? S_OK : S_FALSE,
1081 data->padwTrustStepErrors[TRUSTERROR_STEP_FINAL_POLICYPROV]);
1082 return ret ? S_OK : S_FALSE;
1085 static HRESULT WINAPI WINTRUST_DefaultPolicy(CRYPT_PROVIDER_DATA *pProvData,
1086 DWORD dwStepError, DWORD dwRegPolicySettings, DWORD cSigner,
1087 PWTD_GENERIC_CHAIN_POLICY_SIGNER_INFO rgpSigner, void *pvPolicyArg)
1090 CERT_CHAIN_POLICY_STATUS policyStatus = { sizeof(policyStatus), 0 };
1092 for (i = 0; !policyStatus.dwError && i < cSigner; i++)
1094 CERT_CHAIN_POLICY_PARA policyPara = { sizeof(policyPara), 0 };
1096 if (dwRegPolicySettings & WTPF_IGNOREEXPIRATION)
1097 policyPara.dwFlags |=
1098 CERT_CHAIN_POLICY_IGNORE_NOT_TIME_VALID_FLAG |
1099 CERT_CHAIN_POLICY_IGNORE_CTL_NOT_TIME_VALID_FLAG |
1100 CERT_CHAIN_POLICY_IGNORE_NOT_TIME_NESTED_FLAG;
1101 if (dwRegPolicySettings & WTPF_IGNOREREVOKATION)
1102 policyPara.dwFlags |=
1103 CERT_CHAIN_POLICY_IGNORE_END_REV_UNKNOWN_FLAG |
1104 CERT_CHAIN_POLICY_IGNORE_CTL_SIGNER_REV_UNKNOWN_FLAG |
1105 CERT_CHAIN_POLICY_IGNORE_CA_REV_UNKNOWN_FLAG |
1106 CERT_CHAIN_POLICY_IGNORE_ROOT_REV_UNKNOWN_FLAG;
1107 CertVerifyCertificateChainPolicy(CERT_CHAIN_POLICY_BASE,
1108 rgpSigner[i].pChainContext, &policyPara, &policyStatus);
1110 return policyStatus.dwError;
1113 HRESULT WINAPI GenericChainFinalProv(CRYPT_PROVIDER_DATA *data)
1115 HRESULT err = NO_ERROR; /* not a typo, MS confused the types */
1116 WTD_GENERIC_CHAIN_POLICY_DATA *policyData =
1117 data->pWintrustData->pPolicyCallbackData;
1119 TRACE("(%p)\n", data);
1121 if (data->pWintrustData->dwUIChoice != WTD_UI_NONE)
1122 FIXME("unimplemented for UI choice %d\n",
1123 data->pWintrustData->dwUIChoice);
1124 if (!data->csSigners)
1125 err = TRUST_E_NOSIGNATURE;
1128 PFN_WTD_GENERIC_CHAIN_POLICY_CALLBACK policyCallback;
1130 WTD_GENERIC_CHAIN_POLICY_SIGNER_INFO *signers = NULL;
1134 policyCallback = policyData->pfnPolicyCallback;
1135 policyArg = policyData->pvPolicyArg;
1139 policyCallback = WINTRUST_DefaultPolicy;
1142 if (data->csSigners)
1146 signers = data->psPfns->pfnAlloc(
1147 data->csSigners * sizeof(WTD_GENERIC_CHAIN_POLICY_SIGNER_INFO));
1150 for (i = 0; i < data->csSigners; i++)
1152 signers[i].u.cbSize =
1153 sizeof(WTD_GENERIC_CHAIN_POLICY_SIGNER_INFO);
1154 signers[i].pChainContext =
1155 data->pasSigners[i].pChainContext;
1156 signers[i].dwSignerType = data->pasSigners[i].dwSignerType;
1157 signers[i].pMsgSignerInfo = data->pasSigners[i].psSigner;
1158 signers[i].dwError = data->pasSigners[i].dwError;
1159 if (data->pasSigners[i].csCounterSigners)
1160 FIXME("unimplemented for counter signers\n");
1161 signers[i].cCounterSigner = 0;
1162 signers[i].rgpCounterSigner = NULL;
1166 err = ERROR_OUTOFMEMORY;
1168 if (err == NO_ERROR)
1169 err = policyCallback(data, TRUSTERROR_STEP_FINAL_POLICYPROV,
1170 data->dwRegPolicySettings, data->csSigners, signers, policyArg);
1171 data->psPfns->pfnFree(signers);
1173 if (err != NO_ERROR)
1174 data->padwTrustStepErrors[TRUSTERROR_STEP_FINAL_POLICYPROV] = err;
1175 TRACE("returning %d (%08x)\n", err == NO_ERROR ? S_OK : S_FALSE,
1176 data->padwTrustStepErrors[TRUSTERROR_STEP_FINAL_POLICYPROV]);
1177 return err == NO_ERROR ? S_OK : S_FALSE;
1180 HRESULT WINAPI SoftpubCleanup(CRYPT_PROVIDER_DATA *data)
1184 for (i = 0; i < data->csSigners; i++)
1186 for (j = 0; j < data->pasSigners[i].csCertChain; j++)
1187 CertFreeCertificateContext(data->pasSigners[i].pasCertChain[j].pCert);
1188 data->psPfns->pfnFree(data->pasSigners[i].pasCertChain);
1189 data->psPfns->pfnFree(data->pasSigners[i].psSigner);
1190 CertFreeCertificateChain(data->pasSigners[i].pChainContext);
1192 data->psPfns->pfnFree(data->pasSigners);
1194 for (i = 0; i < data->chStores; i++)
1195 CertCloseStore(data->pahStores[i], 0);
1196 data->psPfns->pfnFree(data->pahStores);
1200 data->psPfns->pfnFree(data->u.pPDSip->pSip);
1201 data->psPfns->pfnFree(data->u.pPDSip->pCATSip);
1202 data->psPfns->pfnFree(data->u.pPDSip->psSipSubjectInfo);
1203 data->psPfns->pfnFree(data->u.pPDSip->psSipCATSubjectInfo);
1204 data->psPfns->pfnFree(data->u.pPDSip->psIndirectData);
1207 CryptMsgClose(data->hMsg);
1209 if (data->fOpenedFile &&
1210 data->pWintrustData->dwUnionChoice == WTD_CHOICE_FILE &&
1211 data->pWintrustData->u.pFile)
1212 CloseHandle(data->pWintrustData->u.pFile->hFile);
1217 HRESULT WINAPI HTTPSCertificateTrust(CRYPT_PROVIDER_DATA *data)
1219 FIXME("(%p)\n", data);
1223 HRESULT WINAPI HTTPSFinalProv(CRYPT_PROVIDER_DATA *data)
1225 FIXME("(%p)\n", data);