windowscodecs: Add support for converting to 32bppPBGRA.
[wine.git] / dlls / wintrust / softpub.c
blobfc1bc13f5f7afbf2a7ce60b9371bb84daeb0d373
1 /*
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
18 #include <stdarg.h>
20 #define NONAMELESSUNION
22 #include "windef.h"
23 #include "winbase.h"
24 #include "wintrust.h"
25 #include "mssip.h"
26 #include "softpub.h"
27 #include "winnls.h"
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])
40 ret = S_OK;
41 TRACE("returning %08x\n", ret);
42 return 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])
53 ret = S_OK;
54 TRACE("returning %08x\n", ret);
55 return ret;
58 HRESULT WINAPI DriverInitializePolicy(CRYPT_PROVIDER_DATA *data)
60 FIXME("stub\n");
61 return S_OK;
64 HRESULT WINAPI DriverCleanupPolicy(CRYPT_PROVIDER_DATA *data)
66 FIXME("stub\n");
67 return S_OK;
70 HRESULT WINAPI DriverFinalPolicy(CRYPT_PROVIDER_DATA *data)
72 FIXME("stub\n");
73 return S_OK;
76 /* Assumes data->pWintrustData->u.pFile exists. Makes sure a file handle is
77 * open for the file.
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
85 * for both.
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;
95 else
96 err = GetLastError();
98 if (!err)
99 GetFileTime(data->pWintrustData->u.pFile->hFile, &data->sftSystemTime,
100 NULL, NULL);
101 TRACE("returning %d\n", err);
102 return 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();
122 else
123 data->u.pPDSip->gSubject = *data->pWintrustData->u.pFile->pgKnownSubject;
124 TRACE("returning %d\n", err);
125 return 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();
141 else
142 err = ERROR_OUTOFMEMORY;
143 TRACE("returning %d\n", err);
144 return 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,
151 LPCWSTR filePath)
153 DWORD err = ERROR_SUCCESS;
154 BOOL ret;
155 LPBYTE buf = NULL;
156 DWORD size = 0;
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);
170 if (!ret)
171 return TRUST_E_NOSIGNATURE;
173 buf = data->psPfns->pfnAlloc(size);
174 if (!buf)
175 return ERROR_OUTOFMEMORY;
177 ret = data->u.pPDSip->pSip->pfGet(data->u.pPDSip->psSipSubjectInfo,
178 &data->dwEncoding, 0, &size, buf);
179 if (ret)
181 data->hMsg = CryptMsgOpenToDecode(data->dwEncoding, 0, 0, data->hProv,
182 NULL, NULL);
183 if (data->hMsg)
185 ret = CryptMsgUpdate(data->hMsg, buf, size, TRUE);
186 if (!ret)
187 err = GetLastError();
190 else
191 err = GetLastError();
193 data->psPfns->pfnFree(buf);
194 TRACE("returning %d\n", err);
195 return err;
198 static DWORD SOFTPUB_CreateStoreFromMessage(CRYPT_PROVIDER_DATA *data)
200 DWORD err = ERROR_SUCCESS;
201 HCERTSTORE store;
203 store = CertOpenStore(CERT_STORE_PROV_MSG, data->dwEncoding,
204 data->hProv, CERT_STORE_NO_CRYPT_RELEASE_FLAG, data->hMsg);
205 if (store)
207 if (!data->psPfns->pfnAddStore2Chain(data, store))
208 err = GetLastError();
209 CertCloseStore(store, 0);
211 else
212 err = GetLastError();
213 TRACE("returning %d\n", err);
214 return err;
217 static DWORD SOFTPUB_DecodeInnerContent(CRYPT_PROVIDER_DATA *data)
219 BOOL ret;
220 DWORD size, err = ERROR_SUCCESS;
221 LPSTR oid = NULL;
222 LPBYTE buf = NULL;
224 ret = CryptMsgGetParam(data->hMsg, CMSG_INNER_CONTENT_TYPE_PARAM, 0, NULL,
225 &size);
226 if (!ret)
228 err = GetLastError();
229 goto error;
231 oid = data->psPfns->pfnAlloc(size);
232 if (!oid)
234 err = ERROR_OUTOFMEMORY;
235 goto error;
237 ret = CryptMsgGetParam(data->hMsg, CMSG_INNER_CONTENT_TYPE_PARAM, 0, oid,
238 &size);
239 if (!ret)
241 err = GetLastError();
242 goto error;
244 ret = CryptMsgGetParam(data->hMsg, CMSG_CONTENT_PARAM, 0, NULL, &size);
245 if (!ret)
247 err = GetLastError();
248 goto error;
250 buf = data->psPfns->pfnAlloc(size);
251 if (!buf)
253 err = ERROR_OUTOFMEMORY;
254 goto error;
256 ret = CryptMsgGetParam(data->hMsg, CMSG_CONTENT_PARAM, 0, buf, &size);
257 if (!ret)
259 err = GetLastError();
260 goto error;
262 ret = CryptDecodeObject(data->dwEncoding, oid, buf, size, 0, NULL, &size);
263 if (!ret)
265 err = GetLastError();
266 goto error;
268 data->u.pPDSip->psIndirectData = data->psPfns->pfnAlloc(size);
269 if (!data->u.pPDSip->psIndirectData)
271 err = ERROR_OUTOFMEMORY;
272 goto error;
274 ret = CryptDecodeObject(data->dwEncoding, oid, buf, size, 0,
275 data->u.pPDSip->psIndirectData, &size);
276 if (!ret)
277 err = GetLastError();
279 error:
280 TRACE("returning %d\n", err);
281 data->psPfns->pfnFree(oid);
282 data->psPfns->pfnFree(buf);
283 return err;
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))
294 if (data->psPfns)
296 CRYPT_PROVIDER_SGNR signer = { sizeof(signer), { 0 } };
297 DWORD i;
298 BOOL ret;
300 /* Add a signer with nothing but the time to verify, so we can
301 * add a cert to it
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;
307 else
309 SYSTEMTIME sysTime;
311 GetSystemTime(&sysTime);
312 SystemTimeToFileTime(&sysTime, &signer.sftVerifyAsOf);
314 ret = data->psPfns->pfnAddSgnr2Chain(data, FALSE, 0, &signer);
315 if (ret)
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))
321 for (i = 0;
322 ret && i < data->pWintrustData->u.pCert->chStores; i++)
323 ret = data->psPfns->pfnAddStore2Chain(data,
324 data->pWintrustData->u.pCert->pahStores[i]);
326 if (!ret)
327 err = GetLastError();
330 else
331 err = ERROR_INVALID_PARAMETER;
332 return err;
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;
342 goto error;
344 err = SOFTPUB_OpenFile(data);
345 if (err)
346 goto error;
347 err = SOFTPUB_GetFileSubject(data);
348 if (err)
349 goto error;
350 err = SOFTPUB_GetSIP(data);
351 if (err)
352 goto error;
353 err = SOFTPUB_GetMessageFromFile(data, data->pWintrustData->u.pFile->hFile,
354 data->pWintrustData->u.pFile->pcwszFilePath);
355 if (err)
356 goto error;
357 err = SOFTPUB_CreateStoreFromMessage(data);
358 if (err)
359 goto error;
360 err = SOFTPUB_DecodeInnerContent(data);
362 error:
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;
371 return err;
374 static DWORD SOFTPUB_LoadCatalogMessage(CRYPT_PROVIDER_DATA *data)
376 DWORD err;
377 HANDLE catalog = INVALID_HANDLE_VALUE;
379 if (!data->pWintrustData->u.pCatalog)
381 SetLastError(ERROR_INVALID_PARAMETER);
382 return FALSE;
384 catalog = CreateFileW(data->pWintrustData->u.pCatalog->pcwszCatalogFilePath,
385 GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL,
386 NULL);
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();
394 goto error;
396 err = SOFTPUB_GetSIP(data);
397 if (err)
398 goto error;
399 err = SOFTPUB_GetMessageFromFile(data, catalog,
400 data->pWintrustData->u.pCatalog->pcwszCatalogFilePath);
401 if (err)
402 goto error;
403 err = SOFTPUB_CreateStoreFromMessage(data);
404 if (err)
405 goto error;
406 err = SOFTPUB_DecodeInnerContent(data);
407 /* FIXME: this loads the catalog file, but doesn't validate the member. */
408 error:
409 CloseHandle(catalog);
410 return err;
413 HRESULT WINAPI SoftpubLoadMessage(CRYPT_PROVIDER_DATA *data)
415 DWORD err = ERROR_SUCCESS;
417 TRACE("(%p)\n", data);
419 if (!data->padwTrustStepErrors)
420 return S_FALSE;
422 switch (data->pWintrustData->dwUnionChoice)
424 case WTD_CHOICE_CERT:
425 err = SOFTPUB_LoadCertMessage(data);
426 break;
427 case WTD_CHOICE_FILE:
428 err = SOFTPUB_LoadFileMessage(data);
429 break;
430 case WTD_CHOICE_CATALOG:
431 err = SOFTPUB_LoadCatalogMessage(data);
432 break;
433 default:
434 FIXME("unimplemented for %d\n", data->pWintrustData->dwUnionChoice);
435 err = ERROR_INVALID_PARAMETER;
438 if (err)
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,
446 DWORD signerIdx)
448 BOOL ret;
449 CMSG_SIGNER_INFO *signerInfo = NULL;
450 DWORD size;
452 ret = CryptMsgGetParam(data->hMsg, CMSG_SIGNER_INFO_PARAM, signerIdx,
453 NULL, &size);
454 if (ret)
456 signerInfo = data->psPfns->pfnAlloc(size);
457 if (signerInfo)
459 ret = CryptMsgGetParam(data->hMsg, CMSG_SIGNER_INFO_PARAM,
460 signerIdx, signerInfo, &size);
461 if (!ret)
463 data->psPfns->pfnFree(signerInfo);
464 signerInfo = NULL;
467 else
468 SetLastError(ERROR_OUTOFMEMORY);
470 return signerInfo;
473 static BOOL WINTRUST_GetTimeFromCounterSigner(
474 const CMSG_CMS_SIGNER_INFO *counterSignerInfo, FILETIME *time)
476 DWORD i;
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];
486 DWORD j;
488 for (j = 0; !foundTimeStamp && j < attr->cValue; j++)
490 static const DWORD encoding = X509_ASN_ENCODING |
491 PKCS_7_ASN_ENCODING;
492 DWORD size = sizeof(FILETIME);
494 foundTimeStamp = CryptDecodeObjectEx(encoding,
495 X509_CHOICE_OF_TIME,
496 attr->rgValue[j].pbData, attr->rgValue[j].cbData, 0, NULL,
497 time, &size);
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 */
508 SYSTEMTIME sysTime;
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]));
517 return date;
520 static FILETIME WINTRUST_GetTimeFromSigner(const CRYPT_PROVIDER_DATA *data,
521 const CMSG_SIGNER_INFO *signerInfo)
523 DWORD i;
524 FILETIME time;
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];
533 DWORD j;
535 for (j = 0; j < attr->cValue; j++)
537 static const DWORD encoding = X509_ASN_ENCODING |
538 PKCS_7_ASN_ENCODING;
539 CMSG_CMS_SIGNER_INFO *counterSignerInfo;
540 DWORD size;
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);
544 if (ret)
546 /* FIXME: need to verify countersigner signature too */
547 foundTimeStamp = WINTRUST_GetTimeFromCounterSigner(
548 counterSignerInfo, &time);
549 LocalFree(counterSignerInfo);
554 if (!foundTimeStamp)
556 TRACE("returning system time %s\n",
557 filetime_to_str(&data->sftSystemTime));
558 time = data->sftSystemTime;
560 else
561 TRACE("returning time from message %s\n", filetime_to_str(&time));
562 return time;
565 static DWORD WINTRUST_SaveSigner(CRYPT_PROVIDER_DATA *data, DWORD signerIdx)
567 DWORD err;
568 CMSG_SIGNER_INFO *signerInfo = WINTRUST_GetSigner(data, signerIdx);
570 if (signerInfo)
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();
578 else
579 err = ERROR_SUCCESS;
581 else
582 err = GetLastError();
583 return err;
586 static CERT_INFO *WINTRUST_GetSignerCertInfo(CRYPT_PROVIDER_DATA *data,
587 DWORD signerIdx)
589 BOOL ret;
590 CERT_INFO *certInfo = NULL;
591 DWORD size;
593 ret = CryptMsgGetParam(data->hMsg, CMSG_SIGNER_CERT_INFO_PARAM, signerIdx,
594 NULL, &size);
595 if (ret)
597 certInfo = data->psPfns->pfnAlloc(size);
598 if (certInfo)
600 ret = CryptMsgGetParam(data->hMsg, CMSG_SIGNER_CERT_INFO_PARAM,
601 signerIdx, certInfo, &size);
602 if (!ret)
604 data->psPfns->pfnFree(certInfo);
605 certInfo = NULL;
608 else
609 SetLastError(ERROR_OUTOFMEMORY);
611 return certInfo;
614 static DWORD WINTRUST_VerifySigner(CRYPT_PROVIDER_DATA *data, DWORD signerIdx)
616 DWORD err;
617 CERT_INFO *certInfo = WINTRUST_GetSignerCertInfo(data, signerIdx);
619 if (certInfo)
621 PCCERT_CONTEXT subject = CertGetSubjectCertificateFromStore(
622 data->pahStores[0], data->dwEncoding, certInfo);
624 if (subject)
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,
630 &para))
631 err = TRUST_E_CERT_SIGNATURE;
632 else
634 data->psPfns->pfnAddCert2Chain(data, signerIdx, FALSE, 0,
635 subject);
636 err = ERROR_SUCCESS;
638 CertFreeCertificateContext(subject);
640 else
641 err = TRUST_E_NO_SIGNER_CERT;
642 data->psPfns->pfnFree(certInfo);
644 else
645 err = GetLastError();
646 return err;
649 HRESULT WINAPI SoftpubLoadSignature(CRYPT_PROVIDER_DATA *data)
651 DWORD err;
653 TRACE("(%p)\n", data);
655 if (!data->padwTrustStepErrors)
656 return S_FALSE;
658 if (data->hMsg)
660 DWORD signerCount, size;
662 size = sizeof(signerCount);
663 if (CryptMsgGetParam(data->hMsg, CMSG_SIGNER_COUNT_PARAM, 0,
664 &signerCount, &size))
666 DWORD i;
668 err = ERROR_SUCCESS;
669 for (i = 0; !err && i < signerCount; i++)
671 if (!(err = WINTRUST_SaveSigner(data, i)))
672 err = WINTRUST_VerifySigner(data, i);
675 else
676 err = TRUST_E_NOSIGNATURE;
678 else
679 err = ERROR_SUCCESS;
680 if (err)
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;
689 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;
696 return confidence;
699 BOOL WINAPI SoftpubCheckCert(CRYPT_PROVIDER_DATA *data, DWORD idxSigner,
700 BOOL fCounterSignerChain, DWORD idxCounterSigner)
702 BOOL ret;
704 TRACE("(%p, %d, %d, %d)\n", data, idxSigner, fCounterSignerChain,
705 idxCounterSigner);
707 if (fCounterSignerChain)
709 FIXME("unimplemented for counter signers\n");
710 ret = FALSE;
712 else
714 PCERT_SIMPLE_CHAIN simpleChain =
715 data->pasSigners[idxSigner].pChainContext->rgpChain[0];
716 DWORD i;
718 ret = TRUE;
719 for (i = 0; i < simpleChain->cElement; i++)
721 /* Set confidence */
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;
737 return ret;
740 static DWORD WINTRUST_TrustStatusToError(DWORD errorStatus)
742 DWORD error;
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;
780 else
781 error = S_OK;
782 return error;
785 static DWORD WINTRUST_CopyChain(CRYPT_PROVIDER_DATA *data, DWORD signerIdx)
787 DWORD err, i;
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];
796 err = ERROR_SUCCESS;
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);
808 else
809 err = GetLastError();
811 data->pasSigners[signerIdx].pasCertChain[simpleChain->cElement - 1].dwError
812 = WINTRUST_TrustStatusToError(
813 simpleChain->rgpElement[simpleChain->cElement - 1]->
814 TrustStatus.dwErrorStatus);
815 return err;
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;
825 else
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;
839 else
840 info->dwFlags = 0;
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;
851 if (data->chStores)
853 store = CertOpenStore(CERT_STORE_PROV_COLLECTION, 0, 0,
854 CERT_STORE_CREATE_NEW_FLAG, NULL);
855 if (store)
857 DWORD i;
859 for (i = 0; i < data->chStores; i++)
860 CertAddStoreToCollection(store, data->pahStores[i], 0, 0);
862 else
863 err = GetLastError();
865 if (!err)
867 /* Expect the end certificate for each signer to be the only cert in
868 * the chain:
870 if (data->pasSigners[signer].csCertChain)
872 BOOL ret;
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);
880 if (ret)
882 if (data->pasSigners[signer].pChainContext->cChain != 1)
884 FIXME("unimplemented for more than 1 simple chain\n");
885 err = E_NOTIMPL;
887 else
889 if (!(err = WINTRUST_CopyChain(data, signer)))
891 if (data->psPfns->pfnCertCheckPolicy)
893 ret = data->psPfns->pfnCertCheckPolicy(data, signer,
894 FALSE, 0);
895 if (!ret)
896 err = GetLastError();
898 else
899 TRACE(
900 "no cert check policy, skipping policy check\n");
904 else
905 err = GetLastError();
907 CertCloseStore(store, 0);
909 return err;
912 HRESULT WINAPI WintrustCertificateTrust(CRYPT_PROVIDER_DATA *data)
914 DWORD err;
916 TRACE("(%p)\n", data);
918 if (!data->csSigners)
919 err = TRUST_E_NOSIGNATURE;
920 else
922 DWORD i;
923 WTD_GENERIC_CHAIN_POLICY_CREATE_INFO createInfo;
924 CERT_CHAIN_PARA chainPara;
926 WINTRUST_CreateChainPolicyCreateInfo(data, &createInfo, &chainPara);
927 err = ERROR_SUCCESS;
928 for (i = 0; !err && i < data->csSigners; i++)
929 err = WINTRUST_CreateChainForSigner(data, i, &createInfo,
930 &chainPara);
932 if (err)
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)
941 DWORD err;
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;
951 goto end;
953 if (!data->csSigners)
954 err = TRUST_E_NOSIGNATURE;
955 else
957 DWORD i;
958 WTD_GENERIC_CHAIN_POLICY_CREATE_INFO createInfo, *pCreateInfo;
959 CERT_CHAIN_PARA chainPara, *pChainPara;
961 if (policyData)
963 pCreateInfo = policyData->pSignerChainInfo;
964 pChainPara = pCreateInfo->pChainPara;
966 else
968 WINTRUST_CreateChainPolicyCreateInfo(data, &createInfo, &chainPara);
969 pChainPara = &chainPara;
970 pCreateInfo = &createInfo;
972 err = ERROR_SUCCESS;
973 for (i = 0; !err && i < data->csSigners; i++)
974 err = WINTRUST_CreateChainForSigner(data, i, pCreateInfo,
975 pChainPara);
978 end:
979 if (err)
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)
988 BOOL ret;
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)
998 ret = FALSE;
999 policyStatus.dwError = TRUST_E_NOSIGNATURE;
1001 else
1003 DWORD i;
1005 ret = TRUE;
1006 for (i = 0; ret && i < data->csSigners; i++)
1008 BYTE hash[20];
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,
1020 disallowedW);
1022 if (disallowed)
1024 PCCERT_CONTEXT found = CertFindCertificateInStore(
1025 disallowed, X509_ASN_ENCODING, 0, CERT_FIND_SIGNATURE_HASH,
1026 hash, NULL);
1028 if (found)
1030 /* Disallowed! Can't verify it. */
1031 policyStatus.dwError = TRUST_E_SUBJECT_NOT_TRUSTED;
1032 ret = FALSE;
1033 CertFreeCertificateContext(found);
1035 CertCloseStore(disallowed, 0);
1038 if (ret)
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)
1060 ret = FALSE;
1064 if (!ret)
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)
1076 DWORD i;
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;
1113 else
1115 PFN_WTD_GENERIC_CHAIN_POLICY_CALLBACK policyCallback;
1116 void *policyArg;
1117 WTD_GENERIC_CHAIN_POLICY_SIGNER_INFO *signers = NULL;
1119 if (policyData)
1121 policyCallback = policyData->pfnPolicyCallback;
1122 policyArg = policyData->pvPolicyArg;
1124 else
1126 policyCallback = WINTRUST_DefaultPolicy;
1127 policyArg = NULL;
1129 if (data->csSigners)
1131 DWORD i;
1133 signers = data->psPfns->pfnAlloc(
1134 data->csSigners * sizeof(WTD_GENERIC_CHAIN_POLICY_SIGNER_INFO));
1135 if (signers)
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;
1152 else
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)
1169 DWORD i, j;
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);
1185 if (data->u.pPDSip)
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);
1200 return S_OK;
1203 HRESULT WINAPI HTTPSCertificateTrust(CRYPT_PROVIDER_DATA *data)
1205 FIXME("(%p)\n", data);
1206 return S_OK;
1209 HRESULT WINAPI HTTPSFinalProv(CRYPT_PROVIDER_DATA *data)
1211 FIXME("(%p)\n", data);
1212 return S_OK;