2 * Copyright 2007 Juan Lang
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2.1 of the License, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with this library; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
20 #define NONAMELESSUNION
27 #include "wine/debug.h"
29 WINE_DEFAULT_DEBUG_CHANNEL(wintrust
);
31 HRESULT WINAPI
SoftpubDefCertInit(CRYPT_PROVIDER_DATA
*data
)
33 HRESULT ret
= S_FALSE
;
35 TRACE("(%p)\n", data
);
37 if (data
->padwTrustStepErrors
&&
38 !data
->padwTrustStepErrors
[TRUSTERROR_STEP_FINAL_WVTINIT
])
40 TRACE("returning %08x\n", ret
);
44 HRESULT WINAPI
SoftpubInitialize(CRYPT_PROVIDER_DATA
*data
)
46 HRESULT ret
= S_FALSE
;
48 TRACE("(%p)\n", data
);
50 if (data
->padwTrustStepErrors
&&
51 !data
->padwTrustStepErrors
[TRUSTERROR_STEP_FINAL_WVTINIT
])
53 TRACE("returning %08x\n", ret
);
57 HRESULT WINAPI
DriverInitializePolicy(CRYPT_PROVIDER_DATA
*data
)
63 HRESULT WINAPI
DriverCleanupPolicy(CRYPT_PROVIDER_DATA
*data
)
69 HRESULT WINAPI
DriverFinalPolicy(CRYPT_PROVIDER_DATA
*data
)
75 /* Assumes data->pWintrustData->u.pFile exists. Makes sure a file handle is
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
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
;
98 GetFileTime(data
->pWintrustData
->u
.pFile
->hFile
, &data
->sftSystemTime
,
100 TRACE("returning %d\n", 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();
122 data
->u
.pPDSip
->gSubject
= *data
->pWintrustData
->u
.pFile
->pgKnownSubject
;
123 TRACE("returning %d\n", 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();
141 err
= ERROR_OUTOFMEMORY
;
142 TRACE("returning %d\n", 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
,
152 DWORD err
= ERROR_SUCCESS
;
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);
170 return TRUST_E_NOSIGNATURE
;
172 buf
= data
->psPfns
->pfnAlloc(size
);
174 return ERROR_OUTOFMEMORY
;
176 ret
= data
->u
.pPDSip
->pSip
->pfGet(data
->u
.pPDSip
->psSipSubjectInfo
,
177 &data
->dwEncoding
, 0, &size
, buf
);
180 data
->hMsg
= CryptMsgOpenToDecode(data
->dwEncoding
, 0, 0, data
->hProv
,
184 ret
= CryptMsgUpdate(data
->hMsg
, buf
, size
, TRUE
);
186 err
= GetLastError();
190 err
= GetLastError();
192 data
->psPfns
->pfnFree(buf
);
193 TRACE("returning %d\n", err
);
197 static DWORD
SOFTPUB_CreateStoreFromMessage(CRYPT_PROVIDER_DATA
*data
)
199 DWORD err
= ERROR_SUCCESS
;
202 store
= CertOpenStore(CERT_STORE_PROV_MSG
, data
->dwEncoding
,
203 data
->hProv
, CERT_STORE_NO_CRYPT_RELEASE_FLAG
, data
->hMsg
);
206 if (!data
->psPfns
->pfnAddStore2Chain(data
, store
))
207 err
= GetLastError();
208 CertCloseStore(store
, 0);
211 err
= GetLastError();
212 TRACE("returning %d\n", err
);
216 static DWORD
SOFTPUB_DecodeInnerContent(CRYPT_PROVIDER_DATA
*data
)
219 DWORD size
, err
= ERROR_SUCCESS
;
223 ret
= CryptMsgGetParam(data
->hMsg
, CMSG_INNER_CONTENT_TYPE_PARAM
, 0, NULL
,
227 err
= GetLastError();
230 oid
= data
->psPfns
->pfnAlloc(size
);
233 err
= ERROR_OUTOFMEMORY
;
236 ret
= CryptMsgGetParam(data
->hMsg
, CMSG_INNER_CONTENT_TYPE_PARAM
, 0, oid
,
240 err
= GetLastError();
243 ret
= CryptMsgGetParam(data
->hMsg
, CMSG_CONTENT_PARAM
, 0, NULL
, &size
);
246 err
= GetLastError();
249 buf
= data
->psPfns
->pfnAlloc(size
);
252 err
= ERROR_OUTOFMEMORY
;
255 ret
= CryptMsgGetParam(data
->hMsg
, CMSG_CONTENT_PARAM
, 0, buf
, &size
);
258 err
= GetLastError();
261 ret
= CryptDecodeObject(data
->dwEncoding
, oid
, buf
, size
, 0, NULL
, &size
);
264 err
= GetLastError();
267 data
->u
.pPDSip
->psIndirectData
= data
->psPfns
->pfnAlloc(size
);
268 if (!data
->u
.pPDSip
->psIndirectData
)
270 err
= ERROR_OUTOFMEMORY
;
273 ret
= CryptDecodeObject(data
->dwEncoding
, oid
, buf
, size
, 0,
274 data
->u
.pPDSip
->psIndirectData
, &size
);
276 err
= GetLastError();
279 TRACE("returning %d\n", err
);
280 data
->psPfns
->pfnFree(oid
);
281 data
->psPfns
->pfnFree(buf
);
285 static DWORD
SOFTPUB_LoadCertMessage(CRYPT_PROVIDER_DATA
*data
)
289 if (data
->pWintrustData
->u
.pCert
&&
290 WVT_IS_CBSTRUCT_GT_MEMBEROFFSET(WINTRUST_CERT_INFO
,
291 data
->pWintrustData
->u
.pCert
->cbStruct
, psCertContext
))
295 CRYPT_PROVIDER_SGNR signer
= { sizeof(signer
), { 0 } };
299 /* Add a signer with nothing but the time to verify, so we can
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
;
310 GetSystemTime(&sysTime
);
311 SystemTimeToFileTime(&sysTime
, &signer
.sftVerifyAsOf
);
313 ret
= data
->psPfns
->pfnAddSgnr2Chain(data
, FALSE
, 0, &signer
);
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
))
321 ret
&& i
< data
->pWintrustData
->u
.pCert
->chStores
; i
++)
322 ret
= data
->psPfns
->pfnAddStore2Chain(data
,
323 data
->pWintrustData
->u
.pCert
->pahStores
[i
]);
326 err
= GetLastError();
330 /* Do nothing!? See the tests */
335 err
= ERROR_INVALID_PARAMETER
;
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
;
348 err
= SOFTPUB_OpenFile(data
);
351 err
= SOFTPUB_GetFileSubject(data
);
354 err
= SOFTPUB_GetSIP(data
);
357 err
= SOFTPUB_GetMessageFromFile(data
, data
->pWintrustData
->u
.pFile
->hFile
,
358 data
->pWintrustData
->u
.pFile
->pcwszFilePath
);
361 err
= SOFTPUB_CreateStoreFromMessage(data
);
364 err
= SOFTPUB_DecodeInnerContent(data
);
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
;
378 static DWORD
SOFTPUB_LoadCatalogMessage(CRYPT_PROVIDER_DATA
*data
)
381 HANDLE catalog
= INVALID_HANDLE_VALUE
;
383 if (!data
->pWintrustData
->u
.pCatalog
)
385 SetLastError(ERROR_INVALID_PARAMETER
);
388 catalog
= CreateFileW(data
->pWintrustData
->u
.pCatalog
->pcwszCatalogFilePath
,
389 GENERIC_READ
, FILE_SHARE_READ
, NULL
, OPEN_EXISTING
, FILE_ATTRIBUTE_NORMAL
,
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();
400 err
= SOFTPUB_GetSIP(data
);
403 err
= SOFTPUB_GetMessageFromFile(data
, data
->pWintrustData
->u
.pFile
->hFile
,
404 data
->pWintrustData
->u
.pFile
->pcwszFilePath
);
407 err
= SOFTPUB_CreateStoreFromMessage(data
);
410 err
= SOFTPUB_DecodeInnerContent(data
);
411 /* FIXME: this loads the catalog file, but doesn't validate the member. */
413 CloseHandle(catalog
);
417 HRESULT WINAPI
SoftpubLoadMessage(CRYPT_PROVIDER_DATA
*data
)
419 DWORD err
= ERROR_SUCCESS
;
421 TRACE("(%p)\n", data
);
423 if (!data
->padwTrustStepErrors
)
426 switch (data
->pWintrustData
->dwUnionChoice
)
428 case WTD_CHOICE_CERT
:
429 err
= SOFTPUB_LoadCertMessage(data
);
431 case WTD_CHOICE_FILE
:
432 err
= SOFTPUB_LoadFileMessage(data
);
434 case WTD_CHOICE_CATALOG
:
435 err
= SOFTPUB_LoadCatalogMessage(data
);
438 FIXME("unimplemented for %d\n", data
->pWintrustData
->dwUnionChoice
);
439 err
= ERROR_INVALID_PARAMETER
;
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
,
453 CMSG_SIGNER_INFO
*signerInfo
= NULL
;
456 ret
= CryptMsgGetParam(data
->hMsg
, CMSG_SIGNER_INFO_PARAM
, signerIdx
,
460 signerInfo
= data
->psPfns
->pfnAlloc(size
);
463 ret
= CryptMsgGetParam(data
->hMsg
, CMSG_SIGNER_INFO_PARAM
,
464 signerIdx
, signerInfo
, &size
);
467 data
->psPfns
->pfnFree(signerInfo
);
472 SetLastError(ERROR_OUTOFMEMORY
);
477 static BOOL
WINTRUST_SaveSigner(CRYPT_PROVIDER_DATA
*data
, DWORD signerIdx
)
480 CMSG_SIGNER_INFO
*signerInfo
= WINTRUST_GetSigner(data
, signerIdx
);
484 CRYPT_PROVIDER_SGNR sgnr
= { sizeof(sgnr
), { 0 } };
486 sgnr
.psSigner
= signerInfo
;
487 sgnr
.sftVerifyAsOf
= data
->sftSystemTime
;
488 ret
= data
->psPfns
->pfnAddSgnr2Chain(data
, FALSE
, signerIdx
, &sgnr
);
495 static CERT_INFO
*WINTRUST_GetSignerCertInfo(CRYPT_PROVIDER_DATA
*data
,
499 CERT_INFO
*certInfo
= NULL
;
502 ret
= CryptMsgGetParam(data
->hMsg
, CMSG_SIGNER_CERT_INFO_PARAM
, signerIdx
,
506 certInfo
= data
->psPfns
->pfnAlloc(size
);
509 ret
= CryptMsgGetParam(data
->hMsg
, CMSG_SIGNER_CERT_INFO_PARAM
,
510 signerIdx
, certInfo
, &size
);
513 data
->psPfns
->pfnFree(certInfo
);
518 SetLastError(ERROR_OUTOFMEMORY
);
523 static DWORD
WINTRUST_VerifySigner(CRYPT_PROVIDER_DATA
*data
, DWORD signerIdx
)
526 CERT_INFO
*certInfo
= WINTRUST_GetSignerCertInfo(data
, signerIdx
);
530 PCCERT_CONTEXT subject
= CertGetSubjectCertificateFromStore(
531 data
->pahStores
[0], data
->dwEncoding
, certInfo
);
535 CMSG_CTRL_VERIFY_SIGNATURE_EX_PARA para
= { sizeof(para
), 0,
536 signerIdx
, CMSG_VERIFY_SIGNER_CERT
, (LPVOID
)subject
};
538 if (!CryptMsgControl(data
->hMsg
, 0, CMSG_CTRL_VERIFY_SIGNATURE_EX
,
540 err
= TRUST_E_CERT_SIGNATURE
;
543 data
->psPfns
->pfnAddCert2Chain(data
, signerIdx
, FALSE
, 0,
547 CertFreeCertificateContext(subject
);
550 err
= TRUST_E_NO_SIGNER_CERT
;
551 data
->psPfns
->pfnFree(certInfo
);
554 err
= GetLastError();
558 HRESULT WINAPI
SoftpubLoadSignature(CRYPT_PROVIDER_DATA
*data
)
562 TRACE("(%p)\n", data
);
564 if (!data
->padwTrustStepErrors
)
569 DWORD signerCount
, size
;
571 size
= sizeof(signerCount
);
572 ret
= CryptMsgGetParam(data
->hMsg
, CMSG_SIGNER_COUNT_PARAM
, 0,
573 &signerCount
, &size
);
578 for (i
= 0; ret
&& i
< signerCount
; i
++)
580 if ((ret
= WINTRUST_SaveSigner(data
, i
)))
584 err
= WINTRUST_VerifySigner(data
, i
);
594 SetLastError(TRUST_E_NOSIGNATURE
);
599 data
->padwTrustStepErrors
[TRUSTERROR_STEP_FINAL_SIGPROV
] =
601 return ret
? S_OK
: S_FALSE
;
604 static DWORD
WINTRUST_TrustStatusToConfidence(DWORD errorStatus
)
606 DWORD confidence
= 0;
609 if (!(errorStatus
& CERT_TRUST_IS_NOT_SIGNATURE_VALID
))
610 confidence
|= CERT_CONFIDENCE_SIG
;
611 if (!(errorStatus
& CERT_TRUST_IS_NOT_TIME_VALID
))
612 confidence
|= CERT_CONFIDENCE_TIME
;
613 if (!(errorStatus
& CERT_TRUST_IS_NOT_TIME_NESTED
))
614 confidence
|= CERT_CONFIDENCE_TIMENEST
;
618 BOOL WINAPI
SoftpubCheckCert(CRYPT_PROVIDER_DATA
*data
, DWORD idxSigner
,
619 BOOL fCounterSignerChain
, DWORD idxCounterSigner
)
623 TRACE("(%p, %d, %d, %d)\n", data
, idxSigner
, fCounterSignerChain
,
626 if (fCounterSignerChain
)
628 FIXME("unimplemented for counter signers\n");
633 PCERT_SIMPLE_CHAIN simpleChain
=
634 data
->pasSigners
[idxSigner
].pChainContext
->rgpChain
[0];
638 for (i
= 0; i
< simpleChain
->cElement
; i
++)
641 data
->pasSigners
[idxSigner
].pasCertChain
[i
].dwConfidence
=
642 WINTRUST_TrustStatusToConfidence(
643 simpleChain
->rgpElement
[i
]->TrustStatus
.dwErrorStatus
);
644 /* Set additional flags */
645 if (!(simpleChain
->rgpElement
[i
]->TrustStatus
.dwErrorStatus
&
646 CERT_TRUST_IS_UNTRUSTED_ROOT
))
647 data
->pasSigners
[idxSigner
].pasCertChain
[i
].fTrustedRoot
= TRUE
;
648 if (simpleChain
->rgpElement
[i
]->TrustStatus
.dwInfoStatus
&
649 CERT_TRUST_IS_SELF_SIGNED
)
650 data
->pasSigners
[idxSigner
].pasCertChain
[i
].fSelfSigned
= TRUE
;
651 if (simpleChain
->rgpElement
[i
]->TrustStatus
.dwErrorStatus
&
652 CERT_TRUST_IS_CYCLIC
)
653 data
->pasSigners
[idxSigner
].pasCertChain
[i
].fIsCyclic
= TRUE
;
659 static DWORD
WINTRUST_TrustStatusToError(DWORD errorStatus
)
663 if (errorStatus
& CERT_TRUST_IS_NOT_SIGNATURE_VALID
)
664 error
= TRUST_E_CERT_SIGNATURE
;
665 else if (errorStatus
& CERT_TRUST_IS_UNTRUSTED_ROOT
)
666 error
= CERT_E_UNTRUSTEDROOT
;
667 else if (errorStatus
& CERT_TRUST_IS_NOT_TIME_VALID
)
668 error
= CERT_E_EXPIRED
;
669 else if (errorStatus
& CERT_TRUST_IS_NOT_TIME_NESTED
)
670 error
= CERT_E_VALIDITYPERIODNESTING
;
671 else if (errorStatus
& CERT_TRUST_IS_REVOKED
)
672 error
= CERT_E_REVOKED
;
673 else if (errorStatus
& CERT_TRUST_IS_OFFLINE_REVOCATION
||
674 errorStatus
& CERT_TRUST_REVOCATION_STATUS_UNKNOWN
)
675 error
= CERT_E_REVOCATION_FAILURE
;
676 else if (errorStatus
& CERT_TRUST_IS_NOT_VALID_FOR_USAGE
)
677 error
= CERT_E_WRONG_USAGE
;
678 else if (errorStatus
& CERT_TRUST_IS_CYCLIC
)
679 error
= CERT_E_CHAINING
;
680 else if (errorStatus
& CERT_TRUST_INVALID_EXTENSION
)
681 error
= CERT_E_CRITICAL
;
682 else if (errorStatus
& CERT_TRUST_INVALID_POLICY_CONSTRAINTS
)
683 error
= CERT_E_INVALID_POLICY
;
684 else if (errorStatus
& CERT_TRUST_INVALID_BASIC_CONSTRAINTS
)
685 error
= TRUST_E_BASIC_CONSTRAINTS
;
686 else if (errorStatus
& CERT_TRUST_INVALID_NAME_CONSTRAINTS
||
687 errorStatus
& CERT_TRUST_HAS_NOT_SUPPORTED_NAME_CONSTRAINT
||
688 errorStatus
& CERT_TRUST_HAS_NOT_DEFINED_NAME_CONSTRAINT
||
689 errorStatus
& CERT_TRUST_HAS_NOT_PERMITTED_NAME_CONSTRAINT
||
690 errorStatus
& CERT_TRUST_HAS_EXCLUDED_NAME_CONSTRAINT
)
691 error
= CERT_E_INVALID_NAME
;
692 else if (errorStatus
& CERT_TRUST_NO_ISSUANCE_CHAIN_POLICY
)
693 error
= CERT_E_INVALID_POLICY
;
694 else if (errorStatus
)
696 FIXME("unknown error status %08x\n", errorStatus
);
697 error
= TRUST_E_SYSTEM_ERROR
;
704 static BOOL
WINTRUST_CopyChain(CRYPT_PROVIDER_DATA
*data
, DWORD signerIdx
)
707 PCERT_SIMPLE_CHAIN simpleChain
=
708 data
->pasSigners
[signerIdx
].pChainContext
->rgpChain
[0];
711 data
->pasSigners
[signerIdx
].pasCertChain
[0].dwConfidence
=
712 WINTRUST_TrustStatusToConfidence(
713 simpleChain
->rgpElement
[0]->TrustStatus
.dwErrorStatus
);
714 data
->pasSigners
[signerIdx
].pasCertChain
[0].pChainElement
=
715 simpleChain
->rgpElement
[0];
717 for (i
= 1; ret
&& i
< simpleChain
->cElement
; i
++)
719 ret
= data
->psPfns
->pfnAddCert2Chain(data
, signerIdx
, FALSE
, 0,
720 simpleChain
->rgpElement
[i
]->pCertContext
);
723 data
->pasSigners
[signerIdx
].pasCertChain
[i
].pChainElement
=
724 simpleChain
->rgpElement
[i
];
725 data
->pasSigners
[signerIdx
].pasCertChain
[i
].dwConfidence
=
726 WINTRUST_TrustStatusToConfidence(
727 simpleChain
->rgpElement
[i
]->TrustStatus
.dwErrorStatus
);
730 data
->pasSigners
[signerIdx
].pasCertChain
[simpleChain
->cElement
- 1].dwError
731 = WINTRUST_TrustStatusToError(
732 simpleChain
->rgpElement
[simpleChain
->cElement
- 1]->
733 TrustStatus
.dwErrorStatus
);
737 static void WINTRUST_CreateChainPolicyCreateInfo(
738 const CRYPT_PROVIDER_DATA
*data
, PWTD_GENERIC_CHAIN_POLICY_CREATE_INFO info
,
739 PCERT_CHAIN_PARA chainPara
)
741 chainPara
->cbSize
= sizeof(CERT_CHAIN_PARA
);
742 if (data
->pRequestUsage
)
743 chainPara
->RequestedUsage
= *data
->pRequestUsage
;
746 chainPara
->RequestedUsage
.dwType
= 0;
747 chainPara
->RequestedUsage
.Usage
.cUsageIdentifier
= 0;
749 info
->u
.cbSize
= sizeof(WTD_GENERIC_CHAIN_POLICY_CREATE_INFO
);
750 info
->hChainEngine
= NULL
;
751 info
->pChainPara
= chainPara
;
752 if (data
->dwProvFlags
& CPD_REVOCATION_CHECK_END_CERT
)
753 info
->dwFlags
= CERT_CHAIN_REVOCATION_CHECK_END_CERT
;
754 else if (data
->dwProvFlags
& CPD_REVOCATION_CHECK_CHAIN
)
755 info
->dwFlags
= CERT_CHAIN_REVOCATION_CHECK_CHAIN
;
756 else if (data
->dwProvFlags
& CPD_REVOCATION_CHECK_CHAIN_EXCLUDE_ROOT
)
757 info
->dwFlags
= CERT_CHAIN_REVOCATION_CHECK_CHAIN_EXCLUDE_ROOT
;
760 info
->pvReserved
= NULL
;
763 static BOOL
WINTRUST_CreateChainForSigner(CRYPT_PROVIDER_DATA
*data
,
764 DWORD signer
, PWTD_GENERIC_CHAIN_POLICY_CREATE_INFO createInfo
,
765 PCERT_CHAIN_PARA chainPara
)
768 HCERTSTORE store
= NULL
;
772 store
= CertOpenStore(CERT_STORE_PROV_COLLECTION
, 0, 0,
773 CERT_STORE_CREATE_NEW_FLAG
, NULL
);
778 for (i
= 0; i
< data
->chStores
; i
++)
779 CertAddStoreToCollection(store
, data
->pahStores
[i
], 0, 0);
782 /* Expect the end certificate for each signer to be the only cert in the
785 if (data
->pasSigners
[signer
].csCertChain
)
787 /* Create a certificate chain for each signer */
788 ret
= CertGetCertificateChain(createInfo
->hChainEngine
,
789 data
->pasSigners
[signer
].pasCertChain
[0].pCert
,
790 &data
->pasSigners
[signer
].sftVerifyAsOf
, store
,
791 chainPara
, createInfo
->dwFlags
, createInfo
->pvReserved
,
792 &data
->pasSigners
[signer
].pChainContext
);
795 if (data
->pasSigners
[signer
].pChainContext
->cChain
!= 1)
797 FIXME("unimplemented for more than 1 simple chain\n");
802 if ((ret
= WINTRUST_CopyChain(data
, signer
)))
804 if (data
->psPfns
->pfnCertCheckPolicy
)
805 ret
= data
->psPfns
->pfnCertCheckPolicy(data
, signer
,
808 TRACE("no cert check policy, skipping policy check\n");
813 CertCloseStore(store
, 0);
817 HRESULT WINAPI
WintrustCertificateTrust(CRYPT_PROVIDER_DATA
*data
)
821 TRACE("(%p)\n", data
);
823 if (!data
->csSigners
)
826 SetLastError(TRUST_E_NOSIGNATURE
);
831 WTD_GENERIC_CHAIN_POLICY_CREATE_INFO createInfo
;
832 CERT_CHAIN_PARA chainPara
;
834 WINTRUST_CreateChainPolicyCreateInfo(data
, &createInfo
, &chainPara
);
836 for (i
= 0; i
< data
->csSigners
; i
++)
837 ret
= WINTRUST_CreateChainForSigner(data
, i
, &createInfo
,
841 data
->padwTrustStepErrors
[TRUSTERROR_STEP_FINAL_CERTPROV
] =
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
)
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
);
863 if (!data
->csSigners
)
866 SetLastError(TRUST_E_NOSIGNATURE
);
871 WTD_GENERIC_CHAIN_POLICY_CREATE_INFO createInfo
, *pCreateInfo
;
872 CERT_CHAIN_PARA chainPara
, *pChainPara
;
876 pCreateInfo
= policyData
->pSignerChainInfo
;
877 pChainPara
= pCreateInfo
->pChainPara
;
881 WINTRUST_CreateChainPolicyCreateInfo(data
, &createInfo
, &chainPara
);
882 pChainPara
= &chainPara
;
883 pCreateInfo
= &createInfo
;
886 for (i
= 0; i
< data
->csSigners
; i
++)
887 ret
= WINTRUST_CreateChainForSigner(data
, i
, pCreateInfo
,
893 data
->padwTrustStepErrors
[TRUSTERROR_STEP_FINAL_CERTPROV
] =
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
)
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
)
913 policyStatus
.dwError
= TRUST_E_NOSIGNATURE
;
920 for (i
= 0; ret
&& i
< data
->csSigners
; i
++)
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
,
938 PCCERT_CONTEXT found
= CertFindCertificateInStore(
939 disallowed
, X509_ASN_ENCODING
, 0, CERT_FIND_SIGNATURE_HASH
,
944 /* Disallowed! Can't verify it. */
945 policyStatus
.dwError
= TRUST_E_SUBJECT_NOT_TRUSTED
;
947 CertFreeCertificateContext(found
);
949 CertCloseStore(disallowed
, 0);
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
)
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
)
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
;
1029 PFN_WTD_GENERIC_CHAIN_POLICY_CALLBACK policyCallback
;
1031 WTD_GENERIC_CHAIN_POLICY_SIGNER_INFO
*signers
= NULL
;
1035 policyCallback
= policyData
->pfnPolicyCallback
;
1036 policyArg
= policyData
->pvPolicyArg
;
1040 policyCallback
= WINTRUST_DefaultPolicy
;
1043 if (data
->csSigners
)
1047 signers
= data
->psPfns
->pfnAlloc(
1048 data
->csSigners
* sizeof(WTD_GENERIC_CHAIN_POLICY_SIGNER_INFO
));
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
;
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
)
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
);
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
);
1117 HRESULT WINAPI
HTTPSCertificateTrust(CRYPT_PROVIDER_DATA
*data
)
1119 FIXME("(%p)\n", data
);
1123 HRESULT WINAPI
HTTPSFinalProv(CRYPT_PROVIDER_DATA
*data
)
1125 FIXME("(%p)\n", data
);