wintrust: Return error directly from WINTRUST_CopyChain.
[wine/hramrach.git] / dlls / wintrust / softpub.c
blob29d8523faf4081fe1891d416812fe1d152f0fda1
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 "wine/debug.h"
29 WINE_DEFAULT_DEBUG_CHANNEL(wintrust);
31 HRESULT WINAPI SoftpubDefCertInit(CRYPT_PROVIDER_DATA *data)
33 HRESULT ret = S_FALSE;
35 TRACE("(%p)\n", data);
37 if (data->padwTrustStepErrors &&
38 !data->padwTrustStepErrors[TRUSTERROR_STEP_FINAL_WVTINIT])
39 ret = S_OK;
40 TRACE("returning %08x\n", ret);
41 return ret;
44 HRESULT WINAPI SoftpubInitialize(CRYPT_PROVIDER_DATA *data)
46 HRESULT ret = S_FALSE;
48 TRACE("(%p)\n", data);
50 if (data->padwTrustStepErrors &&
51 !data->padwTrustStepErrors[TRUSTERROR_STEP_FINAL_WVTINIT])
52 ret = S_OK;
53 TRACE("returning %08x\n", ret);
54 return ret;
57 HRESULT WINAPI DriverInitializePolicy(CRYPT_PROVIDER_DATA *data)
59 FIXME("stub\n");
60 return S_OK;
63 HRESULT WINAPI DriverCleanupPolicy(CRYPT_PROVIDER_DATA *data)
65 FIXME("stub\n");
66 return S_OK;
69 HRESULT WINAPI DriverFinalPolicy(CRYPT_PROVIDER_DATA *data)
71 FIXME("stub\n");
72 return S_OK;
75 /* Assumes data->pWintrustData->u.pFile exists. Makes sure a file handle is
76 * open for the file.
78 static DWORD SOFTPUB_OpenFile(CRYPT_PROVIDER_DATA *data)
80 DWORD err = ERROR_SUCCESS;
82 /* PSDK implies that all values should be initialized to NULL, so callers
83 * typically have hFile as NULL rather than INVALID_HANDLE_VALUE. Check
84 * for both.
86 if (!data->pWintrustData->u.pFile->hFile ||
87 data->pWintrustData->u.pFile->hFile == INVALID_HANDLE_VALUE)
89 data->pWintrustData->u.pFile->hFile =
90 CreateFileW(data->pWintrustData->u.pFile->pcwszFilePath, GENERIC_READ,
91 FILE_SHARE_READ, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
92 if (data->pWintrustData->u.pFile->hFile != INVALID_HANDLE_VALUE)
93 data->fOpenedFile = TRUE;
94 else
95 err = GetLastError();
97 if (!err)
98 GetFileTime(data->pWintrustData->u.pFile->hFile, &data->sftSystemTime,
99 NULL, NULL);
100 TRACE("returning %d\n", err);
101 return err;
104 /* Assumes data->pWintrustData->u.pFile exists. Sets data->pPDSip->gSubject to
105 * the file's subject GUID.
107 static DWORD SOFTPUB_GetFileSubject(CRYPT_PROVIDER_DATA *data)
109 DWORD err = ERROR_SUCCESS;
111 if (!WVT_ISINSTRUCT(WINTRUST_FILE_INFO,
112 data->pWintrustData->u.pFile->cbStruct, pgKnownSubject) ||
113 !data->pWintrustData->u.pFile->pgKnownSubject)
115 if (!CryptSIPRetrieveSubjectGuid(
116 data->pWintrustData->u.pFile->pcwszFilePath,
117 data->pWintrustData->u.pFile->hFile,
118 &data->u.pPDSip->gSubject))
119 err = GetLastError();
121 else
122 data->u.pPDSip->gSubject = *data->pWintrustData->u.pFile->pgKnownSubject;
123 TRACE("returning %d\n", err);
124 return err;
127 /* Assumes data->u.pPDSip exists, and its gSubject member set.
128 * Allocates data->u.pPDSip->pSip and loads it, if possible.
130 static DWORD SOFTPUB_GetSIP(CRYPT_PROVIDER_DATA *data)
132 DWORD err = ERROR_SUCCESS;
134 data->u.pPDSip->pSip = data->psPfns->pfnAlloc(sizeof(SIP_DISPATCH_INFO));
135 if (data->u.pPDSip->pSip)
137 if (!CryptSIPLoad(&data->u.pPDSip->gSubject, 0, data->u.pPDSip->pSip))
138 err = GetLastError();
140 else
141 err = ERROR_OUTOFMEMORY;
142 TRACE("returning %d\n", err);
143 return err;
146 /* Assumes data->u.pPDSip has been loaded, and data->u.pPDSip->pSip allocated.
147 * Calls data->u.pPDSip->pSip->pfGet to construct data->hMsg.
149 static DWORD SOFTPUB_GetMessageFromFile(CRYPT_PROVIDER_DATA *data, HANDLE file,
150 LPCWSTR filePath)
152 DWORD err = ERROR_SUCCESS;
153 BOOL ret;
154 LPBYTE buf = NULL;
155 DWORD size = 0;
157 data->u.pPDSip->psSipSubjectInfo =
158 data->psPfns->pfnAlloc(sizeof(SIP_SUBJECTINFO));
159 if (!data->u.pPDSip->psSipSubjectInfo)
160 return ERROR_OUTOFMEMORY;
162 data->u.pPDSip->psSipSubjectInfo->cbSize = sizeof(SIP_SUBJECTINFO);
163 data->u.pPDSip->psSipSubjectInfo->pgSubjectType = &data->u.pPDSip->gSubject;
164 data->u.pPDSip->psSipSubjectInfo->hFile = file;
165 data->u.pPDSip->psSipSubjectInfo->pwsFileName = filePath;
166 data->u.pPDSip->psSipSubjectInfo->hProv = data->hProv;
167 ret = data->u.pPDSip->pSip->pfGet(data->u.pPDSip->psSipSubjectInfo,
168 &data->dwEncoding, 0, &size, 0);
169 if (!ret)
170 return TRUST_E_NOSIGNATURE;
172 buf = data->psPfns->pfnAlloc(size);
173 if (!buf)
174 return ERROR_OUTOFMEMORY;
176 ret = data->u.pPDSip->pSip->pfGet(data->u.pPDSip->psSipSubjectInfo,
177 &data->dwEncoding, 0, &size, buf);
178 if (ret)
180 data->hMsg = CryptMsgOpenToDecode(data->dwEncoding, 0, 0, data->hProv,
181 NULL, NULL);
182 if (data->hMsg)
184 ret = CryptMsgUpdate(data->hMsg, buf, size, TRUE);
185 if (!ret)
186 err = GetLastError();
189 else
190 err = GetLastError();
192 data->psPfns->pfnFree(buf);
193 TRACE("returning %d\n", err);
194 return err;
197 static DWORD SOFTPUB_CreateStoreFromMessage(CRYPT_PROVIDER_DATA *data)
199 DWORD err = ERROR_SUCCESS;
200 HCERTSTORE store;
202 store = CertOpenStore(CERT_STORE_PROV_MSG, data->dwEncoding,
203 data->hProv, CERT_STORE_NO_CRYPT_RELEASE_FLAG, data->hMsg);
204 if (store)
206 if (!data->psPfns->pfnAddStore2Chain(data, store))
207 err = GetLastError();
208 CertCloseStore(store, 0);
210 else
211 err = GetLastError();
212 TRACE("returning %d\n", err);
213 return err;
216 static DWORD SOFTPUB_DecodeInnerContent(CRYPT_PROVIDER_DATA *data)
218 BOOL ret;
219 DWORD size, err = ERROR_SUCCESS;
220 LPSTR oid = NULL;
221 LPBYTE buf = NULL;
223 ret = CryptMsgGetParam(data->hMsg, CMSG_INNER_CONTENT_TYPE_PARAM, 0, NULL,
224 &size);
225 if (!ret)
227 err = GetLastError();
228 goto error;
230 oid = data->psPfns->pfnAlloc(size);
231 if (!oid)
233 err = ERROR_OUTOFMEMORY;
234 goto error;
236 ret = CryptMsgGetParam(data->hMsg, CMSG_INNER_CONTENT_TYPE_PARAM, 0, oid,
237 &size);
238 if (!ret)
240 err = GetLastError();
241 goto error;
243 ret = CryptMsgGetParam(data->hMsg, CMSG_CONTENT_PARAM, 0, NULL, &size);
244 if (!ret)
246 err = GetLastError();
247 goto error;
249 buf = data->psPfns->pfnAlloc(size);
250 if (!buf)
252 err = ERROR_OUTOFMEMORY;
253 goto error;
255 ret = CryptMsgGetParam(data->hMsg, CMSG_CONTENT_PARAM, 0, buf, &size);
256 if (!ret)
258 err = GetLastError();
259 goto error;
261 ret = CryptDecodeObject(data->dwEncoding, oid, buf, size, 0, NULL, &size);
262 if (!ret)
264 err = GetLastError();
265 goto error;
267 data->u.pPDSip->psIndirectData = data->psPfns->pfnAlloc(size);
268 if (!data->u.pPDSip->psIndirectData)
270 err = ERROR_OUTOFMEMORY;
271 goto error;
273 ret = CryptDecodeObject(data->dwEncoding, oid, buf, size, 0,
274 data->u.pPDSip->psIndirectData, &size);
275 if (!ret)
276 err = GetLastError();
278 error:
279 TRACE("returning %d\n", err);
280 data->psPfns->pfnFree(oid);
281 data->psPfns->pfnFree(buf);
282 return err;
285 static DWORD SOFTPUB_LoadCertMessage(CRYPT_PROVIDER_DATA *data)
287 DWORD err;
289 if (data->pWintrustData->u.pCert &&
290 WVT_IS_CBSTRUCT_GT_MEMBEROFFSET(WINTRUST_CERT_INFO,
291 data->pWintrustData->u.pCert->cbStruct, psCertContext))
293 if (data->psPfns)
295 CRYPT_PROVIDER_SGNR signer = { sizeof(signer), { 0 } };
296 DWORD i;
297 BOOL ret;
299 /* Add a signer with nothing but the time to verify, so we can
300 * add a cert to it
302 if (WVT_ISINSTRUCT(WINTRUST_CERT_INFO,
303 data->pWintrustData->u.pCert->cbStruct, psftVerifyAsOf) &&
304 data->pWintrustData->u.pCert->psftVerifyAsOf)
305 data->sftSystemTime = signer.sftVerifyAsOf;
306 else
308 SYSTEMTIME sysTime;
310 GetSystemTime(&sysTime);
311 SystemTimeToFileTime(&sysTime, &signer.sftVerifyAsOf);
313 ret = data->psPfns->pfnAddSgnr2Chain(data, FALSE, 0, &signer);
314 if (ret)
316 ret = data->psPfns->pfnAddCert2Chain(data, 0, FALSE, 0,
317 data->pWintrustData->u.pCert->psCertContext);
318 if (WVT_ISINSTRUCT(WINTRUST_CERT_INFO,
319 data->pWintrustData->u.pCert->cbStruct, pahStores))
320 for (i = 0;
321 ret && i < data->pWintrustData->u.pCert->chStores; i++)
322 ret = data->psPfns->pfnAddStore2Chain(data,
323 data->pWintrustData->u.pCert->pahStores[i]);
325 if (!ret)
326 err = GetLastError();
328 else
330 /* Do nothing!? See the tests */
331 err = ERROR_SUCCESS;
334 else
335 err = ERROR_INVALID_PARAMETER;
336 return err;
339 static DWORD SOFTPUB_LoadFileMessage(CRYPT_PROVIDER_DATA *data)
341 DWORD err = ERROR_SUCCESS;
343 if (!data->pWintrustData->u.pFile)
345 err = ERROR_INVALID_PARAMETER;
346 goto error;
348 err = SOFTPUB_OpenFile(data);
349 if (err)
350 goto error;
351 err = SOFTPUB_GetFileSubject(data);
352 if (err)
353 goto error;
354 err = SOFTPUB_GetSIP(data);
355 if (err)
356 goto error;
357 err = SOFTPUB_GetMessageFromFile(data, data->pWintrustData->u.pFile->hFile,
358 data->pWintrustData->u.pFile->pcwszFilePath);
359 if (err)
360 goto error;
361 err = SOFTPUB_CreateStoreFromMessage(data);
362 if (err)
363 goto error;
364 err = SOFTPUB_DecodeInnerContent(data);
366 error:
367 if (err && data->fOpenedFile && data->pWintrustData->u.pFile)
369 /* The caller won't expect the file to be open on failure, so close it.
371 CloseHandle(data->pWintrustData->u.pFile->hFile);
372 data->pWintrustData->u.pFile->hFile = INVALID_HANDLE_VALUE;
373 data->fOpenedFile = FALSE;
375 return err;
378 static DWORD SOFTPUB_LoadCatalogMessage(CRYPT_PROVIDER_DATA *data)
380 DWORD err;
381 HANDLE catalog = INVALID_HANDLE_VALUE;
383 if (!data->pWintrustData->u.pCatalog)
385 SetLastError(ERROR_INVALID_PARAMETER);
386 return FALSE;
388 catalog = CreateFileW(data->pWintrustData->u.pCatalog->pcwszCatalogFilePath,
389 GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL,
390 NULL);
391 if (catalog == INVALID_HANDLE_VALUE)
392 return GetLastError();
393 if (!CryptSIPRetrieveSubjectGuid(
394 data->pWintrustData->u.pCatalog->pcwszCatalogFilePath, catalog,
395 &data->u.pPDSip->gSubject))
397 err = GetLastError();
398 goto error;
400 err = SOFTPUB_GetSIP(data);
401 if (err)
402 goto error;
403 err = SOFTPUB_GetMessageFromFile(data, data->pWintrustData->u.pFile->hFile,
404 data->pWintrustData->u.pFile->pcwszFilePath);
405 if (err)
406 goto error;
407 err = SOFTPUB_CreateStoreFromMessage(data);
408 if (err)
409 goto error;
410 err = SOFTPUB_DecodeInnerContent(data);
411 /* FIXME: this loads the catalog file, but doesn't validate the member. */
412 error:
413 CloseHandle(catalog);
414 return err;
417 HRESULT WINAPI SoftpubLoadMessage(CRYPT_PROVIDER_DATA *data)
419 DWORD err = ERROR_SUCCESS;
421 TRACE("(%p)\n", data);
423 if (!data->padwTrustStepErrors)
424 return S_FALSE;
426 switch (data->pWintrustData->dwUnionChoice)
428 case WTD_CHOICE_CERT:
429 err = SOFTPUB_LoadCertMessage(data);
430 break;
431 case WTD_CHOICE_FILE:
432 err = SOFTPUB_LoadFileMessage(data);
433 break;
434 case WTD_CHOICE_CATALOG:
435 err = SOFTPUB_LoadCatalogMessage(data);
436 break;
437 default:
438 FIXME("unimplemented for %d\n", data->pWintrustData->dwUnionChoice);
439 err = ERROR_INVALID_PARAMETER;
442 if (err)
443 data->padwTrustStepErrors[TRUSTERROR_STEP_FINAL_OBJPROV] = err;
444 TRACE("returning %d (%08x)\n", !err ? S_OK : S_FALSE,
445 data->padwTrustStepErrors[TRUSTERROR_STEP_FINAL_OBJPROV]);
446 return !err ? S_OK : S_FALSE;
449 static CMSG_SIGNER_INFO *WINTRUST_GetSigner(CRYPT_PROVIDER_DATA *data,
450 DWORD signerIdx)
452 BOOL ret;
453 CMSG_SIGNER_INFO *signerInfo = NULL;
454 DWORD size;
456 ret = CryptMsgGetParam(data->hMsg, CMSG_SIGNER_INFO_PARAM, signerIdx,
457 NULL, &size);
458 if (ret)
460 signerInfo = data->psPfns->pfnAlloc(size);
461 if (signerInfo)
463 ret = CryptMsgGetParam(data->hMsg, CMSG_SIGNER_INFO_PARAM,
464 signerIdx, signerInfo, &size);
465 if (!ret)
467 data->psPfns->pfnFree(signerInfo);
468 signerInfo = NULL;
471 else
472 SetLastError(ERROR_OUTOFMEMORY);
474 return signerInfo;
477 static DWORD WINTRUST_SaveSigner(CRYPT_PROVIDER_DATA *data, DWORD signerIdx)
479 DWORD err;
480 CMSG_SIGNER_INFO *signerInfo = WINTRUST_GetSigner(data, signerIdx);
482 if (signerInfo)
484 CRYPT_PROVIDER_SGNR sgnr = { sizeof(sgnr), { 0 } };
486 sgnr.psSigner = signerInfo;
487 sgnr.sftVerifyAsOf = data->sftSystemTime;
488 if (!data->psPfns->pfnAddSgnr2Chain(data, FALSE, signerIdx, &sgnr))
489 err = GetLastError();
490 else
491 err = ERROR_SUCCESS;
493 else
494 err = GetLastError();
495 return err;
498 static CERT_INFO *WINTRUST_GetSignerCertInfo(CRYPT_PROVIDER_DATA *data,
499 DWORD signerIdx)
501 BOOL ret;
502 CERT_INFO *certInfo = NULL;
503 DWORD size;
505 ret = CryptMsgGetParam(data->hMsg, CMSG_SIGNER_CERT_INFO_PARAM, signerIdx,
506 NULL, &size);
507 if (ret)
509 certInfo = data->psPfns->pfnAlloc(size);
510 if (certInfo)
512 ret = CryptMsgGetParam(data->hMsg, CMSG_SIGNER_CERT_INFO_PARAM,
513 signerIdx, certInfo, &size);
514 if (!ret)
516 data->psPfns->pfnFree(certInfo);
517 certInfo = NULL;
520 else
521 SetLastError(ERROR_OUTOFMEMORY);
523 return certInfo;
526 static DWORD WINTRUST_VerifySigner(CRYPT_PROVIDER_DATA *data, DWORD signerIdx)
528 DWORD err;
529 CERT_INFO *certInfo = WINTRUST_GetSignerCertInfo(data, signerIdx);
531 if (certInfo)
533 PCCERT_CONTEXT subject = CertGetSubjectCertificateFromStore(
534 data->pahStores[0], data->dwEncoding, certInfo);
536 if (subject)
538 CMSG_CTRL_VERIFY_SIGNATURE_EX_PARA para = { sizeof(para), 0,
539 signerIdx, CMSG_VERIFY_SIGNER_CERT, (LPVOID)subject };
541 if (!CryptMsgControl(data->hMsg, 0, CMSG_CTRL_VERIFY_SIGNATURE_EX,
542 &para))
543 err = TRUST_E_CERT_SIGNATURE;
544 else
546 data->psPfns->pfnAddCert2Chain(data, signerIdx, FALSE, 0,
547 subject);
548 err = ERROR_SUCCESS;
550 CertFreeCertificateContext(subject);
552 else
553 err = TRUST_E_NO_SIGNER_CERT;
554 data->psPfns->pfnFree(certInfo);
556 else
557 err = GetLastError();
558 return err;
561 HRESULT WINAPI SoftpubLoadSignature(CRYPT_PROVIDER_DATA *data)
563 DWORD err;
565 TRACE("(%p)\n", data);
567 if (!data->padwTrustStepErrors)
568 return S_FALSE;
570 if (data->hMsg)
572 DWORD signerCount, size;
574 size = sizeof(signerCount);
575 if (CryptMsgGetParam(data->hMsg, CMSG_SIGNER_COUNT_PARAM, 0,
576 &signerCount, &size))
578 DWORD i;
580 err = ERROR_SUCCESS;
581 for (i = 0; !err && i < signerCount; i++)
583 if (!(err = WINTRUST_SaveSigner(data, i)))
584 err = WINTRUST_VerifySigner(data, i);
587 else
588 err = TRUST_E_NOSIGNATURE;
590 else
591 err = ERROR_SUCCESS;
592 if (err)
593 data->padwTrustStepErrors[TRUSTERROR_STEP_FINAL_SIGPROV] = err;
594 return !err ? S_OK : S_FALSE;
597 static DWORD WINTRUST_TrustStatusToConfidence(DWORD errorStatus)
599 DWORD confidence = 0;
601 confidence = 0;
602 if (!(errorStatus & CERT_TRUST_IS_NOT_SIGNATURE_VALID))
603 confidence |= CERT_CONFIDENCE_SIG;
604 if (!(errorStatus & CERT_TRUST_IS_NOT_TIME_VALID))
605 confidence |= CERT_CONFIDENCE_TIME;
606 if (!(errorStatus & CERT_TRUST_IS_NOT_TIME_NESTED))
607 confidence |= CERT_CONFIDENCE_TIMENEST;
608 return confidence;
611 BOOL WINAPI SoftpubCheckCert(CRYPT_PROVIDER_DATA *data, DWORD idxSigner,
612 BOOL fCounterSignerChain, DWORD idxCounterSigner)
614 BOOL ret;
616 TRACE("(%p, %d, %d, %d)\n", data, idxSigner, fCounterSignerChain,
617 idxCounterSigner);
619 if (fCounterSignerChain)
621 FIXME("unimplemented for counter signers\n");
622 ret = FALSE;
624 else
626 PCERT_SIMPLE_CHAIN simpleChain =
627 data->pasSigners[idxSigner].pChainContext->rgpChain[0];
628 DWORD i;
630 ret = TRUE;
631 for (i = 0; i < simpleChain->cElement; i++)
633 /* Set confidence */
634 data->pasSigners[idxSigner].pasCertChain[i].dwConfidence =
635 WINTRUST_TrustStatusToConfidence(
636 simpleChain->rgpElement[i]->TrustStatus.dwErrorStatus);
637 /* Set additional flags */
638 if (!(simpleChain->rgpElement[i]->TrustStatus.dwErrorStatus &
639 CERT_TRUST_IS_UNTRUSTED_ROOT))
640 data->pasSigners[idxSigner].pasCertChain[i].fTrustedRoot = TRUE;
641 if (simpleChain->rgpElement[i]->TrustStatus.dwInfoStatus &
642 CERT_TRUST_IS_SELF_SIGNED)
643 data->pasSigners[idxSigner].pasCertChain[i].fSelfSigned = TRUE;
644 if (simpleChain->rgpElement[i]->TrustStatus.dwErrorStatus &
645 CERT_TRUST_IS_CYCLIC)
646 data->pasSigners[idxSigner].pasCertChain[i].fIsCyclic = TRUE;
649 return ret;
652 static DWORD WINTRUST_TrustStatusToError(DWORD errorStatus)
654 DWORD error;
656 if (errorStatus & CERT_TRUST_IS_NOT_SIGNATURE_VALID)
657 error = TRUST_E_CERT_SIGNATURE;
658 else if (errorStatus & CERT_TRUST_IS_UNTRUSTED_ROOT)
659 error = CERT_E_UNTRUSTEDROOT;
660 else if (errorStatus & CERT_TRUST_IS_NOT_TIME_VALID)
661 error = CERT_E_EXPIRED;
662 else if (errorStatus & CERT_TRUST_IS_NOT_TIME_NESTED)
663 error = CERT_E_VALIDITYPERIODNESTING;
664 else if (errorStatus & CERT_TRUST_IS_REVOKED)
665 error = CERT_E_REVOKED;
666 else if (errorStatus & CERT_TRUST_IS_OFFLINE_REVOCATION ||
667 errorStatus & CERT_TRUST_REVOCATION_STATUS_UNKNOWN)
668 error = CERT_E_REVOCATION_FAILURE;
669 else if (errorStatus & CERT_TRUST_IS_NOT_VALID_FOR_USAGE)
670 error = CERT_E_WRONG_USAGE;
671 else if (errorStatus & CERT_TRUST_IS_CYCLIC)
672 error = CERT_E_CHAINING;
673 else if (errorStatus & CERT_TRUST_INVALID_EXTENSION)
674 error = CERT_E_CRITICAL;
675 else if (errorStatus & CERT_TRUST_INVALID_POLICY_CONSTRAINTS)
676 error = CERT_E_INVALID_POLICY;
677 else if (errorStatus & CERT_TRUST_INVALID_BASIC_CONSTRAINTS)
678 error = TRUST_E_BASIC_CONSTRAINTS;
679 else if (errorStatus & CERT_TRUST_INVALID_NAME_CONSTRAINTS ||
680 errorStatus & CERT_TRUST_HAS_NOT_SUPPORTED_NAME_CONSTRAINT ||
681 errorStatus & CERT_TRUST_HAS_NOT_DEFINED_NAME_CONSTRAINT ||
682 errorStatus & CERT_TRUST_HAS_NOT_PERMITTED_NAME_CONSTRAINT ||
683 errorStatus & CERT_TRUST_HAS_EXCLUDED_NAME_CONSTRAINT)
684 error = CERT_E_INVALID_NAME;
685 else if (errorStatus & CERT_TRUST_NO_ISSUANCE_CHAIN_POLICY)
686 error = CERT_E_INVALID_POLICY;
687 else if (errorStatus)
689 FIXME("unknown error status %08x\n", errorStatus);
690 error = TRUST_E_SYSTEM_ERROR;
692 else
693 error = S_OK;
694 return error;
697 static DWORD WINTRUST_CopyChain(CRYPT_PROVIDER_DATA *data, DWORD signerIdx)
699 DWORD err, i;
700 PCERT_SIMPLE_CHAIN simpleChain =
701 data->pasSigners[signerIdx].pChainContext->rgpChain[0];
703 data->pasSigners[signerIdx].pasCertChain[0].dwConfidence =
704 WINTRUST_TrustStatusToConfidence(
705 simpleChain->rgpElement[0]->TrustStatus.dwErrorStatus);
706 data->pasSigners[signerIdx].pasCertChain[0].pChainElement =
707 simpleChain->rgpElement[0];
708 err = ERROR_SUCCESS;
709 for (i = 1; !err && i < simpleChain->cElement; i++)
711 if (data->psPfns->pfnAddCert2Chain(data, signerIdx, FALSE, 0,
712 simpleChain->rgpElement[i]->pCertContext))
714 data->pasSigners[signerIdx].pasCertChain[i].pChainElement =
715 simpleChain->rgpElement[i];
716 data->pasSigners[signerIdx].pasCertChain[i].dwConfidence =
717 WINTRUST_TrustStatusToConfidence(
718 simpleChain->rgpElement[i]->TrustStatus.dwErrorStatus);
720 else
721 err = GetLastError();
723 data->pasSigners[signerIdx].pasCertChain[simpleChain->cElement - 1].dwError
724 = WINTRUST_TrustStatusToError(
725 simpleChain->rgpElement[simpleChain->cElement - 1]->
726 TrustStatus.dwErrorStatus);
727 return err;
730 static void WINTRUST_CreateChainPolicyCreateInfo(
731 const CRYPT_PROVIDER_DATA *data, PWTD_GENERIC_CHAIN_POLICY_CREATE_INFO info,
732 PCERT_CHAIN_PARA chainPara)
734 chainPara->cbSize = sizeof(CERT_CHAIN_PARA);
735 if (data->pRequestUsage)
736 chainPara->RequestedUsage = *data->pRequestUsage;
737 else
739 chainPara->RequestedUsage.dwType = 0;
740 chainPara->RequestedUsage.Usage.cUsageIdentifier = 0;
742 info->u.cbSize = sizeof(WTD_GENERIC_CHAIN_POLICY_CREATE_INFO);
743 info->hChainEngine = NULL;
744 info->pChainPara = chainPara;
745 if (data->dwProvFlags & CPD_REVOCATION_CHECK_END_CERT)
746 info->dwFlags = CERT_CHAIN_REVOCATION_CHECK_END_CERT;
747 else if (data->dwProvFlags & CPD_REVOCATION_CHECK_CHAIN)
748 info->dwFlags = CERT_CHAIN_REVOCATION_CHECK_CHAIN;
749 else if (data->dwProvFlags & CPD_REVOCATION_CHECK_CHAIN_EXCLUDE_ROOT)
750 info->dwFlags = CERT_CHAIN_REVOCATION_CHECK_CHAIN_EXCLUDE_ROOT;
751 else
752 info->dwFlags = 0;
753 info->pvReserved = NULL;
756 static BOOL WINTRUST_CreateChainForSigner(CRYPT_PROVIDER_DATA *data,
757 DWORD signer, PWTD_GENERIC_CHAIN_POLICY_CREATE_INFO createInfo,
758 PCERT_CHAIN_PARA chainPara)
760 BOOL ret = TRUE;
761 HCERTSTORE store = NULL;
763 if (data->chStores)
765 store = CertOpenStore(CERT_STORE_PROV_COLLECTION, 0, 0,
766 CERT_STORE_CREATE_NEW_FLAG, NULL);
767 if (store)
769 DWORD i;
771 for (i = 0; i < data->chStores; i++)
772 CertAddStoreToCollection(store, data->pahStores[i], 0, 0);
775 /* Expect the end certificate for each signer to be the only cert in the
776 * chain:
778 if (data->pasSigners[signer].csCertChain)
780 /* Create a certificate chain for each signer */
781 ret = CertGetCertificateChain(createInfo->hChainEngine,
782 data->pasSigners[signer].pasCertChain[0].pCert,
783 &data->pasSigners[signer].sftVerifyAsOf, store,
784 chainPara, createInfo->dwFlags, createInfo->pvReserved,
785 &data->pasSigners[signer].pChainContext);
786 if (ret)
788 if (data->pasSigners[signer].pChainContext->cChain != 1)
790 FIXME("unimplemented for more than 1 simple chain\n");
791 ret = FALSE;
793 else
795 DWORD err;
797 if (!(err = WINTRUST_CopyChain(data, signer)))
799 if (data->psPfns->pfnCertCheckPolicy)
800 ret = data->psPfns->pfnCertCheckPolicy(data, signer,
801 FALSE, 0);
802 else
803 TRACE("no cert check policy, skipping policy check\n");
805 else
807 SetLastError(err);
808 ret = FALSE;
813 CertCloseStore(store, 0);
814 return ret;
817 HRESULT WINAPI WintrustCertificateTrust(CRYPT_PROVIDER_DATA *data)
819 BOOL ret;
821 TRACE("(%p)\n", data);
823 if (!data->csSigners)
825 ret = FALSE;
826 SetLastError(TRUST_E_NOSIGNATURE);
828 else
830 DWORD i;
831 WTD_GENERIC_CHAIN_POLICY_CREATE_INFO createInfo;
832 CERT_CHAIN_PARA chainPara;
834 WINTRUST_CreateChainPolicyCreateInfo(data, &createInfo, &chainPara);
835 ret = TRUE;
836 for (i = 0; i < data->csSigners; i++)
837 ret = WINTRUST_CreateChainForSigner(data, i, &createInfo,
838 &chainPara);
840 if (!ret)
841 data->padwTrustStepErrors[TRUSTERROR_STEP_FINAL_CERTPROV] =
842 GetLastError();
843 TRACE("returning %d (%08x)\n", ret ? S_OK : S_FALSE,
844 data->padwTrustStepErrors[TRUSTERROR_STEP_FINAL_CERTPROV]);
845 return ret ? S_OK : S_FALSE;
848 HRESULT WINAPI GenericChainCertificateTrust(CRYPT_PROVIDER_DATA *data)
850 BOOL ret;
851 WTD_GENERIC_CHAIN_POLICY_DATA *policyData =
852 data->pWintrustData->pPolicyCallbackData;
854 TRACE("(%p)\n", data);
856 if (policyData && policyData->u.cbSize !=
857 sizeof(WTD_GENERIC_CHAIN_POLICY_CREATE_INFO))
859 SetLastError(ERROR_INVALID_PARAMETER);
860 ret = FALSE;
861 goto end;
863 if (!data->csSigners)
865 ret = FALSE;
866 SetLastError(TRUST_E_NOSIGNATURE);
868 else
870 DWORD i;
871 WTD_GENERIC_CHAIN_POLICY_CREATE_INFO createInfo, *pCreateInfo;
872 CERT_CHAIN_PARA chainPara, *pChainPara;
874 if (policyData)
876 pCreateInfo = policyData->pSignerChainInfo;
877 pChainPara = pCreateInfo->pChainPara;
879 else
881 WINTRUST_CreateChainPolicyCreateInfo(data, &createInfo, &chainPara);
882 pChainPara = &chainPara;
883 pCreateInfo = &createInfo;
885 ret = TRUE;
886 for (i = 0; i < data->csSigners; i++)
887 ret = WINTRUST_CreateChainForSigner(data, i, pCreateInfo,
888 pChainPara);
891 end:
892 if (!ret)
893 data->padwTrustStepErrors[TRUSTERROR_STEP_FINAL_CERTPROV] =
894 GetLastError();
895 TRACE("returning %d (%08x)\n", ret ? S_OK : S_FALSE,
896 data->padwTrustStepErrors[TRUSTERROR_STEP_FINAL_CERTPROV]);
897 return ret ? S_OK : S_FALSE;
900 HRESULT WINAPI SoftpubAuthenticode(CRYPT_PROVIDER_DATA *data)
902 BOOL ret;
903 CERT_CHAIN_POLICY_STATUS policyStatus = { sizeof(policyStatus), 0 };
905 TRACE("(%p)\n", data);
907 if (data->pWintrustData->dwUIChoice != WTD_UI_NONE)
908 FIXME("unimplemented for UI choice %d\n",
909 data->pWintrustData->dwUIChoice);
910 if (!data->csSigners)
912 ret = FALSE;
913 policyStatus.dwError = TRUST_E_NOSIGNATURE;
915 else
917 DWORD i;
919 ret = TRUE;
920 for (i = 0; ret && i < data->csSigners; i++)
922 BYTE hash[20];
923 DWORD size = sizeof(hash);
925 /* First make sure cert isn't disallowed */
926 if ((ret = CertGetCertificateContextProperty(
927 data->pasSigners[i].pasCertChain[0].pCert,
928 CERT_SIGNATURE_HASH_PROP_ID, hash, &size)))
930 static const WCHAR disallowedW[] =
931 { 'D','i','s','a','l','l','o','w','e','d',0 };
932 HCERTSTORE disallowed = CertOpenStore(CERT_STORE_PROV_SYSTEM_W,
933 X509_ASN_ENCODING, 0, CERT_SYSTEM_STORE_CURRENT_USER,
934 disallowedW);
936 if (disallowed)
938 PCCERT_CONTEXT found = CertFindCertificateInStore(
939 disallowed, X509_ASN_ENCODING, 0, CERT_FIND_SIGNATURE_HASH,
940 hash, NULL);
942 if (found)
944 /* Disallowed! Can't verify it. */
945 policyStatus.dwError = TRUST_E_SUBJECT_NOT_TRUSTED;
946 ret = FALSE;
947 CertFreeCertificateContext(found);
949 CertCloseStore(disallowed, 0);
952 if (ret)
954 CERT_CHAIN_POLICY_PARA policyPara = { sizeof(policyPara), 0 };
956 if (data->dwRegPolicySettings & WTPF_TRUSTTEST)
957 policyPara.dwFlags |= CERT_CHAIN_POLICY_TRUST_TESTROOT_FLAG;
958 if (data->dwRegPolicySettings & WTPF_TESTCANBEVALID)
959 policyPara.dwFlags |= CERT_CHAIN_POLICY_ALLOW_TESTROOT_FLAG;
960 if (data->dwRegPolicySettings & WTPF_IGNOREEXPIRATION)
961 policyPara.dwFlags |=
962 CERT_CHAIN_POLICY_IGNORE_NOT_TIME_VALID_FLAG |
963 CERT_CHAIN_POLICY_IGNORE_CTL_NOT_TIME_VALID_FLAG |
964 CERT_CHAIN_POLICY_IGNORE_NOT_TIME_NESTED_FLAG;
965 if (data->dwRegPolicySettings & WTPF_IGNOREREVOKATION)
966 policyPara.dwFlags |=
967 CERT_CHAIN_POLICY_IGNORE_END_REV_UNKNOWN_FLAG |
968 CERT_CHAIN_POLICY_IGNORE_CTL_SIGNER_REV_UNKNOWN_FLAG |
969 CERT_CHAIN_POLICY_IGNORE_CA_REV_UNKNOWN_FLAG |
970 CERT_CHAIN_POLICY_IGNORE_ROOT_REV_UNKNOWN_FLAG;
971 CertVerifyCertificateChainPolicy(CERT_CHAIN_POLICY_AUTHENTICODE,
972 data->pasSigners[i].pChainContext, &policyPara, &policyStatus);
973 if (policyStatus.dwError != NO_ERROR)
974 ret = FALSE;
978 if (!ret)
979 data->padwTrustStepErrors[TRUSTERROR_STEP_FINAL_POLICYPROV] =
980 policyStatus.dwError;
981 TRACE("returning %d (%08x)\n", ret ? S_OK : S_FALSE,
982 data->padwTrustStepErrors[TRUSTERROR_STEP_FINAL_POLICYPROV]);
983 return ret ? S_OK : S_FALSE;
986 static HRESULT WINAPI WINTRUST_DefaultPolicy(CRYPT_PROVIDER_DATA *pProvData,
987 DWORD dwStepError, DWORD dwRegPolicySettings, DWORD cSigner,
988 PWTD_GENERIC_CHAIN_POLICY_SIGNER_INFO rgpSigner, void *pvPolicyArg)
990 DWORD i;
991 CERT_CHAIN_POLICY_STATUS policyStatus = { sizeof(policyStatus), 0 };
993 for (i = 0; !policyStatus.dwError && i < cSigner; i++)
995 CERT_CHAIN_POLICY_PARA policyPara = { sizeof(policyPara), 0 };
997 if (dwRegPolicySettings & WTPF_IGNOREEXPIRATION)
998 policyPara.dwFlags |=
999 CERT_CHAIN_POLICY_IGNORE_NOT_TIME_VALID_FLAG |
1000 CERT_CHAIN_POLICY_IGNORE_CTL_NOT_TIME_VALID_FLAG |
1001 CERT_CHAIN_POLICY_IGNORE_NOT_TIME_NESTED_FLAG;
1002 if (dwRegPolicySettings & WTPF_IGNOREREVOKATION)
1003 policyPara.dwFlags |=
1004 CERT_CHAIN_POLICY_IGNORE_END_REV_UNKNOWN_FLAG |
1005 CERT_CHAIN_POLICY_IGNORE_CTL_SIGNER_REV_UNKNOWN_FLAG |
1006 CERT_CHAIN_POLICY_IGNORE_CA_REV_UNKNOWN_FLAG |
1007 CERT_CHAIN_POLICY_IGNORE_ROOT_REV_UNKNOWN_FLAG;
1008 CertVerifyCertificateChainPolicy(CERT_CHAIN_POLICY_BASE,
1009 rgpSigner[i].pChainContext, &policyPara, &policyStatus);
1011 return policyStatus.dwError;
1014 HRESULT WINAPI GenericChainFinalProv(CRYPT_PROVIDER_DATA *data)
1016 HRESULT err = NO_ERROR; /* not a typo, MS confused the types */
1017 WTD_GENERIC_CHAIN_POLICY_DATA *policyData =
1018 data->pWintrustData->pPolicyCallbackData;
1020 TRACE("(%p)\n", data);
1022 if (data->pWintrustData->dwUIChoice != WTD_UI_NONE)
1023 FIXME("unimplemented for UI choice %d\n",
1024 data->pWintrustData->dwUIChoice);
1025 if (!data->csSigners)
1026 err = TRUST_E_NOSIGNATURE;
1027 else
1029 PFN_WTD_GENERIC_CHAIN_POLICY_CALLBACK policyCallback;
1030 void *policyArg;
1031 WTD_GENERIC_CHAIN_POLICY_SIGNER_INFO *signers = NULL;
1033 if (policyData)
1035 policyCallback = policyData->pfnPolicyCallback;
1036 policyArg = policyData->pvPolicyArg;
1038 else
1040 policyCallback = WINTRUST_DefaultPolicy;
1041 policyArg = NULL;
1043 if (data->csSigners)
1045 DWORD i;
1047 signers = data->psPfns->pfnAlloc(
1048 data->csSigners * sizeof(WTD_GENERIC_CHAIN_POLICY_SIGNER_INFO));
1049 if (signers)
1051 for (i = 0; i < data->csSigners; i++)
1053 signers[i].u.cbSize =
1054 sizeof(WTD_GENERIC_CHAIN_POLICY_SIGNER_INFO);
1055 signers[i].pChainContext =
1056 data->pasSigners[i].pChainContext;
1057 signers[i].dwSignerType = data->pasSigners[i].dwSignerType;
1058 signers[i].pMsgSignerInfo = data->pasSigners[i].psSigner;
1059 signers[i].dwError = data->pasSigners[i].dwError;
1060 if (data->pasSigners[i].csCounterSigners)
1061 FIXME("unimplemented for counter signers\n");
1062 signers[i].cCounterSigner = 0;
1063 signers[i].rgpCounterSigner = NULL;
1066 else
1067 err = ERROR_OUTOFMEMORY;
1069 if (err == NO_ERROR)
1070 err = policyCallback(data, TRUSTERROR_STEP_FINAL_POLICYPROV,
1071 data->dwRegPolicySettings, data->csSigners, signers, policyArg);
1072 data->psPfns->pfnFree(signers);
1074 if (err != NO_ERROR)
1075 data->padwTrustStepErrors[TRUSTERROR_STEP_FINAL_POLICYPROV] = err;
1076 TRACE("returning %d (%08x)\n", err == NO_ERROR ? S_OK : S_FALSE,
1077 data->padwTrustStepErrors[TRUSTERROR_STEP_FINAL_POLICYPROV]);
1078 return err == NO_ERROR ? S_OK : S_FALSE;
1081 HRESULT WINAPI SoftpubCleanup(CRYPT_PROVIDER_DATA *data)
1083 DWORD i, j;
1085 for (i = 0; i < data->csSigners; i++)
1087 for (j = 0; j < data->pasSigners[i].csCertChain; j++)
1088 CertFreeCertificateContext(data->pasSigners[i].pasCertChain[j].pCert);
1089 data->psPfns->pfnFree(data->pasSigners[i].pasCertChain);
1090 data->psPfns->pfnFree(data->pasSigners[i].psSigner);
1091 CertFreeCertificateChain(data->pasSigners[i].pChainContext);
1093 data->psPfns->pfnFree(data->pasSigners);
1095 for (i = 0; i < data->chStores; i++)
1096 CertCloseStore(data->pahStores[i], 0);
1097 data->psPfns->pfnFree(data->pahStores);
1099 if (data->u.pPDSip)
1101 data->psPfns->pfnFree(data->u.pPDSip->pSip);
1102 data->psPfns->pfnFree(data->u.pPDSip->pCATSip);
1103 data->psPfns->pfnFree(data->u.pPDSip->psSipSubjectInfo);
1104 data->psPfns->pfnFree(data->u.pPDSip->psSipCATSubjectInfo);
1105 data->psPfns->pfnFree(data->u.pPDSip->psIndirectData);
1108 CryptMsgClose(data->hMsg);
1110 if (data->fOpenedFile &&
1111 data->pWintrustData->dwUnionChoice == WTD_CHOICE_FILE)
1112 CloseHandle(data->pWintrustData->u.pFile->hFile);
1114 return S_OK;
1117 HRESULT WINAPI HTTPSCertificateTrust(CRYPT_PROVIDER_DATA *data)
1119 FIXME("(%p)\n", data);
1120 return S_OK;
1123 HRESULT WINAPI HTTPSFinalProv(CRYPT_PROVIDER_DATA *data)
1125 FIXME("(%p)\n", data);
1126 return S_OK;