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 /* Assumes data->pWintrustData->u.pFile exists. Makes sure a file handle is
60 static BOOL
SOFTPUB_OpenFile(CRYPT_PROVIDER_DATA
*data
)
64 /* PSDK implies that all values should be initialized to NULL, so callers
65 * typically have hFile as NULL rather than INVALID_HANDLE_VALUE. Check
68 if (!data
->pWintrustData
->u
.pFile
->hFile
||
69 data
->pWintrustData
->u
.pFile
->hFile
== INVALID_HANDLE_VALUE
)
71 data
->pWintrustData
->u
.pFile
->hFile
=
72 CreateFileW(data
->pWintrustData
->u
.pFile
->pcwszFilePath
, GENERIC_READ
,
73 FILE_SHARE_READ
, NULL
, OPEN_EXISTING
, FILE_ATTRIBUTE_NORMAL
, NULL
);
74 if (data
->pWintrustData
->u
.pFile
->hFile
!= INVALID_HANDLE_VALUE
)
75 data
->fOpenedFile
= TRUE
;
80 GetFileTime(data
->pWintrustData
->u
.pFile
->hFile
, &data
->sftSystemTime
,
82 TRACE("returning %d\n", ret
);
86 /* Assumes data->pWintrustData->u.pFile exists. Sets data->pPDSip->gSubject to
87 * the file's subject GUID.
89 static BOOL
SOFTPUB_GetFileSubject(CRYPT_PROVIDER_DATA
*data
)
93 if (!data
->pWintrustData
->u
.pFile
->pgKnownSubject
)
95 ret
= CryptSIPRetrieveSubjectGuid(
96 data
->pWintrustData
->u
.pFile
->pcwszFilePath
,
97 data
->pWintrustData
->u
.pFile
->hFile
,
98 &data
->u
.pPDSip
->gSubject
);
102 data
->u
.pPDSip
->gSubject
= *data
->pWintrustData
->u
.pFile
->pgKnownSubject
;
105 TRACE("returning %d\n", ret
);
109 /* Assumes data->u.pPDSip exists, and its gSubject member set.
110 * Allocates data->u.pPDSip->pSip and loads it, if possible.
112 static BOOL
SOFTPUB_GetSIP(CRYPT_PROVIDER_DATA
*data
)
116 data
->u
.pPDSip
->pSip
= data
->psPfns
->pfnAlloc(sizeof(SIP_DISPATCH_INFO
));
117 if (data
->u
.pPDSip
->pSip
)
118 ret
= CryptSIPLoad(&data
->u
.pPDSip
->gSubject
, 0, data
->u
.pPDSip
->pSip
);
121 SetLastError(ERROR_OUTOFMEMORY
);
124 TRACE("returning %d\n", ret
);
128 /* Assumes data->u.pPDSip has been loaded, and data->u.pPDSip->pSip allocated.
129 * Calls data->u.pPDSip->pSip->pfGet to construct data->hMsg.
131 static BOOL
SOFTPUB_GetMessageFromFile(CRYPT_PROVIDER_DATA
*data
, HANDLE file
,
138 data
->u
.pPDSip
->psSipSubjectInfo
=
139 data
->psPfns
->pfnAlloc(sizeof(SIP_SUBJECTINFO
));
140 if (!data
->u
.pPDSip
->psSipSubjectInfo
)
142 SetLastError(ERROR_OUTOFMEMORY
);
146 data
->u
.pPDSip
->psSipSubjectInfo
->cbSize
= sizeof(SIP_SUBJECTINFO
);
147 data
->u
.pPDSip
->psSipSubjectInfo
->pgSubjectType
= &data
->u
.pPDSip
->gSubject
;
148 data
->u
.pPDSip
->psSipSubjectInfo
->hFile
= file
;
149 data
->u
.pPDSip
->psSipSubjectInfo
->pwsFileName
= filePath
;
150 data
->u
.pPDSip
->psSipSubjectInfo
->hProv
= data
->hProv
;
151 ret
= data
->u
.pPDSip
->pSip
->pfGet(data
->u
.pPDSip
->psSipSubjectInfo
,
152 &data
->dwEncoding
, 0, &size
, 0);
155 SetLastError(TRUST_E_NOSIGNATURE
);
159 buf
= data
->psPfns
->pfnAlloc(size
);
162 SetLastError(ERROR_OUTOFMEMORY
);
166 ret
= data
->u
.pPDSip
->pSip
->pfGet(data
->u
.pPDSip
->psSipSubjectInfo
,
167 &data
->dwEncoding
, 0, &size
, buf
);
170 data
->hMsg
= CryptMsgOpenToDecode(data
->dwEncoding
, 0, 0, data
->hProv
,
173 ret
= CryptMsgUpdate(data
->hMsg
, buf
, size
, TRUE
);
176 data
->psPfns
->pfnFree(buf
);
177 TRACE("returning %d\n", ret
);
181 static BOOL
SOFTPUB_CreateStoreFromMessage(CRYPT_PROVIDER_DATA
*data
)
186 store
= CertOpenStore(CERT_STORE_PROV_MSG
, data
->dwEncoding
,
187 data
->hProv
, CERT_STORE_NO_CRYPT_RELEASE_FLAG
, data
->hMsg
);
190 ret
= data
->psPfns
->pfnAddStore2Chain(data
, store
);
191 CertCloseStore(store
, 0);
193 TRACE("returning %d\n", ret
);
197 static DWORD
SOFTPUB_DecodeInnerContent(CRYPT_PROVIDER_DATA
*data
)
204 ret
= CryptMsgGetParam(data
->hMsg
, CMSG_INNER_CONTENT_TYPE_PARAM
, 0, NULL
,
208 oid
= data
->psPfns
->pfnAlloc(size
);
211 SetLastError(ERROR_OUTOFMEMORY
);
215 ret
= CryptMsgGetParam(data
->hMsg
, CMSG_INNER_CONTENT_TYPE_PARAM
, 0, oid
,
219 ret
= CryptMsgGetParam(data
->hMsg
, CMSG_CONTENT_PARAM
, 0, NULL
, &size
);
222 buf
= data
->psPfns
->pfnAlloc(size
);
225 SetLastError(ERROR_OUTOFMEMORY
);
229 ret
= CryptMsgGetParam(data
->hMsg
, CMSG_CONTENT_PARAM
, 0, buf
, &size
);
232 ret
= CryptDecodeObject(data
->dwEncoding
, oid
, buf
, size
, 0, NULL
, &size
);
235 data
->u
.pPDSip
->psIndirectData
= data
->psPfns
->pfnAlloc(size
);
236 if (!data
->u
.pPDSip
->psIndirectData
)
238 SetLastError(ERROR_OUTOFMEMORY
);
242 ret
= CryptDecodeObject(data
->dwEncoding
, oid
, buf
, size
, 0,
243 data
->u
.pPDSip
->psIndirectData
, &size
);
246 TRACE("returning %d\n", ret
);
247 data
->psPfns
->pfnFree(oid
);
248 data
->psPfns
->pfnFree(buf
);
252 static BOOL
SOFTPUB_LoadCertMessage(CRYPT_PROVIDER_DATA
*data
)
256 if (data
->pWintrustData
->u
.pCert
&&
257 data
->pWintrustData
->u
.pCert
->cbStruct
== sizeof(WINTRUST_CERT_INFO
))
261 CRYPT_PROVIDER_SGNR signer
= { sizeof(signer
), { 0 } };
264 /* Add a signer with nothing but the time to verify, so we can
267 if (data
->pWintrustData
->u
.pCert
->psftVerifyAsOf
)
268 data
->sftSystemTime
= signer
.sftVerifyAsOf
;
273 GetSystemTime(&sysTime
);
274 SystemTimeToFileTime(&sysTime
, &signer
.sftVerifyAsOf
);
276 ret
= data
->psPfns
->pfnAddSgnr2Chain(data
, FALSE
, 0, &signer
);
279 ret
= data
->psPfns
->pfnAddCert2Chain(data
, 0, FALSE
, 0,
280 data
->pWintrustData
->u
.pCert
->psCertContext
);
281 for (i
= 0; ret
&& i
< data
->pWintrustData
->u
.pCert
->chStores
;
283 ret
= data
->psPfns
->pfnAddStore2Chain(data
,
284 data
->pWintrustData
->u
.pCert
->pahStores
[i
]);
289 /* Do nothing!? See the tests */
295 SetLastError(ERROR_INVALID_PARAMETER
);
301 static BOOL
SOFTPUB_LoadFileMessage(CRYPT_PROVIDER_DATA
*data
)
305 if (!data
->pWintrustData
->u
.pFile
)
307 SetLastError(ERROR_INVALID_PARAMETER
);
311 ret
= SOFTPUB_OpenFile(data
);
314 ret
= SOFTPUB_GetFileSubject(data
);
317 ret
= SOFTPUB_GetSIP(data
);
320 ret
= SOFTPUB_GetMessageFromFile(data
, data
->pWintrustData
->u
.pFile
->hFile
,
321 data
->pWintrustData
->u
.pFile
->pcwszFilePath
);
324 ret
= SOFTPUB_CreateStoreFromMessage(data
);
327 ret
= SOFTPUB_DecodeInnerContent(data
);
332 static BOOL
SOFTPUB_LoadCatalogMessage(CRYPT_PROVIDER_DATA
*data
)
335 HANDLE catalog
= INVALID_HANDLE_VALUE
;
337 if (!data
->pWintrustData
->u
.pCatalog
)
339 SetLastError(ERROR_INVALID_PARAMETER
);
342 catalog
= CreateFileW(data
->pWintrustData
->u
.pCatalog
->pcwszCatalogFilePath
,
343 GENERIC_READ
, FILE_SHARE_READ
, NULL
, OPEN_EXISTING
, FILE_ATTRIBUTE_NORMAL
,
345 if (catalog
== INVALID_HANDLE_VALUE
)
347 ret
= CryptSIPRetrieveSubjectGuid(
348 data
->pWintrustData
->u
.pCatalog
->pcwszCatalogFilePath
, catalog
,
349 &data
->u
.pPDSip
->gSubject
);
352 ret
= SOFTPUB_GetSIP(data
);
355 ret
= SOFTPUB_GetMessageFromFile(data
, catalog
,
356 data
->pWintrustData
->u
.pCatalog
->pcwszCatalogFilePath
);
359 ret
= SOFTPUB_CreateStoreFromMessage(data
);
362 ret
= SOFTPUB_DecodeInnerContent(data
);
363 /* FIXME: this loads the catalog file, but doesn't validate the member. */
365 CloseHandle(catalog
);
369 HRESULT WINAPI
SoftpubLoadMessage(CRYPT_PROVIDER_DATA
*data
)
373 TRACE("(%p)\n", data
);
375 if (!data
->padwTrustStepErrors
)
378 switch (data
->pWintrustData
->dwUnionChoice
)
380 case WTD_CHOICE_CERT
:
381 ret
= SOFTPUB_LoadCertMessage(data
);
383 case WTD_CHOICE_FILE
:
384 ret
= SOFTPUB_LoadFileMessage(data
);
386 case WTD_CHOICE_CATALOG
:
387 ret
= SOFTPUB_LoadCatalogMessage(data
);
390 FIXME("unimplemented for %d\n", data
->pWintrustData
->dwUnionChoice
);
391 SetLastError(ERROR_INVALID_PARAMETER
);
396 data
->padwTrustStepErrors
[TRUSTERROR_STEP_FINAL_OBJPROV
] =
398 TRACE("returning %d (%08x)\n", ret
? S_OK
: S_FALSE
,
399 data
->padwTrustStepErrors
[TRUSTERROR_STEP_FINAL_OBJPROV
]);
400 return ret
? S_OK
: S_FALSE
;
403 static CMSG_SIGNER_INFO
*WINTRUST_GetSigner(CRYPT_PROVIDER_DATA
*data
,
407 CMSG_SIGNER_INFO
*signerInfo
= NULL
;
410 ret
= CryptMsgGetParam(data
->hMsg
, CMSG_SIGNER_INFO_PARAM
, signerIdx
,
414 signerInfo
= data
->psPfns
->pfnAlloc(size
);
417 ret
= CryptMsgGetParam(data
->hMsg
, CMSG_SIGNER_INFO_PARAM
,
418 signerIdx
, signerInfo
, &size
);
421 data
->psPfns
->pfnFree(signerInfo
);
426 SetLastError(ERROR_OUTOFMEMORY
);
431 static BOOL
WINTRUST_SaveSigner(CRYPT_PROVIDER_DATA
*data
, DWORD signerIdx
)
434 CMSG_SIGNER_INFO
*signerInfo
= WINTRUST_GetSigner(data
, signerIdx
);
438 CRYPT_PROVIDER_SGNR sgnr
= { sizeof(sgnr
), { 0 } };
440 sgnr
.psSigner
= signerInfo
;
441 sgnr
.sftVerifyAsOf
= data
->sftSystemTime
;
442 ret
= data
->psPfns
->pfnAddSgnr2Chain(data
, FALSE
, signerIdx
, &sgnr
);
449 static CERT_INFO
*WINTRUST_GetSignerCertInfo(CRYPT_PROVIDER_DATA
*data
,
453 CERT_INFO
*certInfo
= NULL
;
456 ret
= CryptMsgGetParam(data
->hMsg
, CMSG_SIGNER_CERT_INFO_PARAM
, signerIdx
,
460 certInfo
= data
->psPfns
->pfnAlloc(size
);
463 ret
= CryptMsgGetParam(data
->hMsg
, CMSG_SIGNER_CERT_INFO_PARAM
,
464 signerIdx
, certInfo
, &size
);
467 data
->psPfns
->pfnFree(certInfo
);
472 SetLastError(ERROR_OUTOFMEMORY
);
477 static BOOL
WINTRUST_VerifySigner(CRYPT_PROVIDER_DATA
*data
, DWORD signerIdx
)
480 CERT_INFO
*certInfo
= WINTRUST_GetSignerCertInfo(data
, signerIdx
);
484 PCCERT_CONTEXT subject
= CertGetSubjectCertificateFromStore(
485 data
->pahStores
[0], data
->dwEncoding
, certInfo
);
489 CMSG_CTRL_VERIFY_SIGNATURE_EX_PARA para
= { sizeof(para
), 0,
490 signerIdx
, CMSG_VERIFY_SIGNER_CERT
, (LPVOID
)subject
};
492 ret
= CryptMsgControl(data
->hMsg
, 0, CMSG_CTRL_VERIFY_SIGNATURE_EX
,
495 SetLastError(TRUST_E_CERT_SIGNATURE
);
497 data
->psPfns
->pfnAddCert2Chain(data
, signerIdx
, FALSE
, 0,
499 CertFreeCertificateContext(subject
);
503 SetLastError(TRUST_E_NO_SIGNER_CERT
);
506 data
->psPfns
->pfnFree(certInfo
);
513 HRESULT WINAPI
SoftpubLoadSignature(CRYPT_PROVIDER_DATA
*data
)
517 TRACE("(%p)\n", data
);
519 if (!data
->padwTrustStepErrors
)
524 DWORD signerCount
, size
;
526 size
= sizeof(signerCount
);
527 ret
= CryptMsgGetParam(data
->hMsg
, CMSG_SIGNER_COUNT_PARAM
, 0,
528 &signerCount
, &size
);
533 for (i
= 0; ret
&& i
< signerCount
; i
++)
535 if ((ret
= WINTRUST_SaveSigner(data
, i
)))
536 ret
= WINTRUST_VerifySigner(data
, i
);
540 SetLastError(TRUST_E_NOSIGNATURE
);
545 data
->padwTrustStepErrors
[TRUSTERROR_STEP_FINAL_SIGPROV
] =
547 return ret
? S_OK
: S_FALSE
;
550 static DWORD
WINTRUST_TrustStatusToConfidence(DWORD errorStatus
)
552 DWORD confidence
= 0;
555 if (!(errorStatus
& CERT_TRUST_IS_NOT_SIGNATURE_VALID
))
556 confidence
|= CERT_CONFIDENCE_SIG
;
557 if (!(errorStatus
& CERT_TRUST_IS_NOT_TIME_VALID
))
558 confidence
|= CERT_CONFIDENCE_TIME
;
559 if (!(errorStatus
& CERT_TRUST_IS_NOT_TIME_NESTED
))
560 confidence
|= CERT_CONFIDENCE_TIMENEST
;
564 BOOL WINAPI
SoftpubCheckCert(CRYPT_PROVIDER_DATA
*data
, DWORD idxSigner
,
565 BOOL fCounterSignerChain
, DWORD idxCounterSigner
)
569 TRACE("(%p, %d, %d, %d)\n", data
, idxSigner
, fCounterSignerChain
,
572 if (fCounterSignerChain
)
574 FIXME("unimplemented for counter signers\n");
579 PCERT_SIMPLE_CHAIN simpleChain
=
580 data
->pasSigners
[idxSigner
].pChainContext
->rgpChain
[0];
584 for (i
= 0; i
< simpleChain
->cElement
; i
++)
587 data
->pasSigners
[idxSigner
].pasCertChain
[i
].dwConfidence
=
588 WINTRUST_TrustStatusToConfidence(
589 simpleChain
->rgpElement
[i
]->TrustStatus
.dwErrorStatus
);
590 /* Set additional flags */
591 if (!(simpleChain
->rgpElement
[i
]->TrustStatus
.dwErrorStatus
&
592 CERT_TRUST_IS_UNTRUSTED_ROOT
))
593 data
->pasSigners
[idxSigner
].pasCertChain
[i
].fTrustedRoot
= TRUE
;
594 if (simpleChain
->rgpElement
[i
]->TrustStatus
.dwInfoStatus
&
595 CERT_TRUST_IS_SELF_SIGNED
)
596 data
->pasSigners
[idxSigner
].pasCertChain
[i
].fSelfSigned
= TRUE
;
597 if (simpleChain
->rgpElement
[i
]->TrustStatus
.dwErrorStatus
&
598 CERT_TRUST_IS_CYCLIC
)
599 data
->pasSigners
[idxSigner
].pasCertChain
[i
].fIsCyclic
= TRUE
;
605 static DWORD
WINTRUST_TrustStatusToError(DWORD errorStatus
)
609 if (errorStatus
& CERT_TRUST_IS_NOT_SIGNATURE_VALID
)
610 error
= TRUST_E_CERT_SIGNATURE
;
611 else if (errorStatus
& CERT_TRUST_IS_UNTRUSTED_ROOT
)
612 error
= CERT_E_UNTRUSTEDROOT
;
613 else if (errorStatus
& CERT_TRUST_IS_NOT_TIME_VALID
)
614 error
= CERT_E_EXPIRED
;
615 else if (errorStatus
& CERT_TRUST_IS_NOT_TIME_NESTED
)
616 error
= CERT_E_VALIDITYPERIODNESTING
;
617 else if (errorStatus
& CERT_TRUST_IS_REVOKED
)
618 error
= CERT_E_REVOKED
;
619 else if (errorStatus
& CERT_TRUST_IS_OFFLINE_REVOCATION
||
620 errorStatus
& CERT_TRUST_REVOCATION_STATUS_UNKNOWN
)
621 error
= CERT_E_REVOCATION_FAILURE
;
622 else if (errorStatus
& CERT_TRUST_IS_NOT_VALID_FOR_USAGE
)
623 error
= CERT_E_WRONG_USAGE
;
624 else if (errorStatus
& CERT_TRUST_IS_CYCLIC
)
625 error
= CERT_E_CHAINING
;
626 else if (errorStatus
& CERT_TRUST_INVALID_EXTENSION
)
627 error
= CERT_E_CRITICAL
;
628 else if (errorStatus
& CERT_TRUST_INVALID_POLICY_CONSTRAINTS
)
629 error
= CERT_E_INVALID_POLICY
;
630 else if (errorStatus
& CERT_TRUST_INVALID_BASIC_CONSTRAINTS
)
631 error
= TRUST_E_BASIC_CONSTRAINTS
;
632 else if (errorStatus
& CERT_TRUST_INVALID_NAME_CONSTRAINTS
||
633 errorStatus
& CERT_TRUST_HAS_NOT_SUPPORTED_NAME_CONSTRAINT
||
634 errorStatus
& CERT_TRUST_HAS_NOT_DEFINED_NAME_CONSTRAINT
||
635 errorStatus
& CERT_TRUST_HAS_NOT_PERMITTED_NAME_CONSTRAINT
||
636 errorStatus
& CERT_TRUST_HAS_EXCLUDED_NAME_CONSTRAINT
)
637 error
= CERT_E_INVALID_NAME
;
638 else if (errorStatus
& CERT_TRUST_NO_ISSUANCE_CHAIN_POLICY
)
639 error
= CERT_E_INVALID_POLICY
;
640 else if (errorStatus
)
642 FIXME("unknown error status %08x\n", errorStatus
);
643 error
= TRUST_E_SYSTEM_ERROR
;
650 static BOOL
WINTRUST_CopyChain(CRYPT_PROVIDER_DATA
*data
, DWORD signerIdx
)
653 PCERT_SIMPLE_CHAIN simpleChain
=
654 data
->pasSigners
[signerIdx
].pChainContext
->rgpChain
[0];
657 data
->pasSigners
[signerIdx
].pasCertChain
[0].dwConfidence
=
658 WINTRUST_TrustStatusToConfidence(
659 simpleChain
->rgpElement
[0]->TrustStatus
.dwErrorStatus
);
660 data
->pasSigners
[signerIdx
].pasCertChain
[0].pChainElement
=
661 simpleChain
->rgpElement
[0];
663 for (i
= 1; ret
&& i
< simpleChain
->cElement
; i
++)
665 ret
= data
->psPfns
->pfnAddCert2Chain(data
, signerIdx
, FALSE
, 0,
666 simpleChain
->rgpElement
[i
]->pCertContext
);
669 data
->pasSigners
[signerIdx
].pasCertChain
[i
].pChainElement
=
670 simpleChain
->rgpElement
[i
];
671 data
->pasSigners
[signerIdx
].pasCertChain
[i
].dwConfidence
=
672 WINTRUST_TrustStatusToConfidence(
673 simpleChain
->rgpElement
[i
]->TrustStatus
.dwErrorStatus
);
676 data
->pasSigners
[signerIdx
].pasCertChain
[simpleChain
->cElement
- 1].dwError
677 = WINTRUST_TrustStatusToError(
678 simpleChain
->rgpElement
[simpleChain
->cElement
- 1]->
679 TrustStatus
.dwErrorStatus
);
683 static void WINTRUST_CreateChainPolicyCreateInfo(
684 const CRYPT_PROVIDER_DATA
*data
, PWTD_GENERIC_CHAIN_POLICY_CREATE_INFO info
,
685 PCERT_CHAIN_PARA chainPara
)
687 chainPara
->cbSize
= sizeof(CERT_CHAIN_PARA
);
688 if (data
->pRequestUsage
)
689 chainPara
->RequestedUsage
= *data
->pRequestUsage
;
692 chainPara
->RequestedUsage
.dwType
= 0;
693 chainPara
->RequestedUsage
.Usage
.cUsageIdentifier
= 0;
695 info
->u
.cbSize
= sizeof(WTD_GENERIC_CHAIN_POLICY_CREATE_INFO
);
696 info
->hChainEngine
= NULL
;
697 info
->pChainPara
= chainPara
;
698 if (data
->dwProvFlags
& CPD_REVOCATION_CHECK_END_CERT
)
699 info
->dwFlags
= CERT_CHAIN_REVOCATION_CHECK_END_CERT
;
700 else if (data
->dwProvFlags
& CPD_REVOCATION_CHECK_CHAIN
)
701 info
->dwFlags
= CERT_CHAIN_REVOCATION_CHECK_CHAIN
;
702 else if (data
->dwProvFlags
& CPD_REVOCATION_CHECK_CHAIN_EXCLUDE_ROOT
)
703 info
->dwFlags
= CERT_CHAIN_REVOCATION_CHECK_CHAIN_EXCLUDE_ROOT
;
706 info
->pvReserved
= NULL
;
709 static BOOL
WINTRUST_CreateChainForSigner(CRYPT_PROVIDER_DATA
*data
,
710 DWORD signer
, PWTD_GENERIC_CHAIN_POLICY_CREATE_INFO createInfo
,
711 PCERT_CHAIN_PARA chainPara
)
714 HCERTSTORE store
= NULL
;
718 store
= CertOpenStore(CERT_STORE_PROV_COLLECTION
, 0, 0,
719 CERT_STORE_CREATE_NEW_FLAG
, NULL
);
724 for (i
= 0; i
< data
->chStores
; i
++)
725 CertAddStoreToCollection(store
, data
->pahStores
[i
], 0, 0);
728 /* Expect the end certificate for each signer to be the only cert in the
731 if (data
->pasSigners
[signer
].csCertChain
)
733 /* Create a certificate chain for each signer */
734 ret
= CertGetCertificateChain(createInfo
->hChainEngine
,
735 data
->pasSigners
[signer
].pasCertChain
[0].pCert
,
736 &data
->pasSigners
[signer
].sftVerifyAsOf
, store
,
737 chainPara
, createInfo
->dwFlags
, createInfo
->pvReserved
,
738 &data
->pasSigners
[signer
].pChainContext
);
741 if (data
->pasSigners
[signer
].pChainContext
->cChain
!= 1)
743 FIXME("unimplemented for more than 1 simple chain\n");
748 if ((ret
= WINTRUST_CopyChain(data
, signer
)))
750 if (data
->psPfns
->pfnCertCheckPolicy
)
751 ret
= data
->psPfns
->pfnCertCheckPolicy(data
, signer
,
754 TRACE("no cert check policy, skipping policy check\n");
759 CertCloseStore(store
, 0);
763 HRESULT WINAPI
WintrustCertificateTrust(CRYPT_PROVIDER_DATA
*data
)
767 TRACE("(%p)\n", data
);
769 if (!data
->csSigners
)
772 SetLastError(TRUST_E_NOSIGNATURE
);
777 WTD_GENERIC_CHAIN_POLICY_CREATE_INFO createInfo
;
778 CERT_CHAIN_PARA chainPara
;
780 WINTRUST_CreateChainPolicyCreateInfo(data
, &createInfo
, &chainPara
);
782 for (i
= 0; i
< data
->csSigners
; i
++)
783 ret
= WINTRUST_CreateChainForSigner(data
, i
, &createInfo
,
787 data
->padwTrustStepErrors
[TRUSTERROR_STEP_FINAL_CERTPROV
] =
789 TRACE("returning %d (%08x)\n", ret
? S_OK
: S_FALSE
,
790 data
->padwTrustStepErrors
[TRUSTERROR_STEP_FINAL_CERTPROV
]);
791 return ret
? S_OK
: S_FALSE
;
794 HRESULT WINAPI
GenericChainCertificateTrust(CRYPT_PROVIDER_DATA
*data
)
797 WTD_GENERIC_CHAIN_POLICY_DATA
*policyData
=
798 data
->pWintrustData
->pPolicyCallbackData
;
800 TRACE("(%p)\n", data
);
802 if (policyData
&& policyData
->u
.cbSize
!=
803 sizeof(WTD_GENERIC_CHAIN_POLICY_CREATE_INFO
))
805 SetLastError(ERROR_INVALID_PARAMETER
);
809 if (!data
->csSigners
)
812 SetLastError(TRUST_E_NOSIGNATURE
);
817 WTD_GENERIC_CHAIN_POLICY_CREATE_INFO createInfo
, *pCreateInfo
;
818 CERT_CHAIN_PARA chainPara
, *pChainPara
;
822 pCreateInfo
= policyData
->pSignerChainInfo
;
823 pChainPara
= pCreateInfo
->pChainPara
;
827 WINTRUST_CreateChainPolicyCreateInfo(data
, &createInfo
, &chainPara
);
828 pChainPara
= &chainPara
;
829 pCreateInfo
= &createInfo
;
832 for (i
= 0; i
< data
->csSigners
; i
++)
833 ret
= WINTRUST_CreateChainForSigner(data
, i
, pCreateInfo
,
839 data
->padwTrustStepErrors
[TRUSTERROR_STEP_FINAL_CERTPROV
] =
841 TRACE("returning %d (%08x)\n", ret
? S_OK
: S_FALSE
,
842 data
->padwTrustStepErrors
[TRUSTERROR_STEP_FINAL_CERTPROV
]);
843 return ret
? S_OK
: S_FALSE
;
846 HRESULT WINAPI
SoftpubAuthenticode(CRYPT_PROVIDER_DATA
*data
)
849 CERT_CHAIN_POLICY_STATUS policyStatus
= { sizeof(policyStatus
), 0 };
851 TRACE("(%p)\n", data
);
853 if (data
->pWintrustData
->dwUIChoice
!= WTD_UI_NONE
)
854 FIXME("unimplemented for UI choice %d\n",
855 data
->pWintrustData
->dwUIChoice
);
856 if (!data
->csSigners
)
859 policyStatus
.dwError
= TRUST_E_NOSIGNATURE
;
866 for (i
= 0; ret
&& i
< data
->csSigners
; i
++)
869 DWORD size
= sizeof(hash
);
871 /* First make sure cert isn't disallowed */
872 if ((ret
= CertGetCertificateContextProperty(
873 data
->pasSigners
[i
].pasCertChain
[0].pCert
,
874 CERT_SIGNATURE_HASH_PROP_ID
, hash
, &size
)))
876 static const WCHAR disallowedW
[] =
877 { 'D','i','s','a','l','l','o','w','e','d',0 };
878 HCERTSTORE disallowed
= CertOpenStore(CERT_STORE_PROV_SYSTEM_W
,
879 X509_ASN_ENCODING
, 0, CERT_SYSTEM_STORE_CURRENT_USER
,
884 PCCERT_CONTEXT found
= CertFindCertificateInStore(
885 disallowed
, X509_ASN_ENCODING
, 0, CERT_FIND_SIGNATURE_HASH
,
890 /* Disallowed! Can't verify it. */
891 policyStatus
.dwError
= TRUST_E_SUBJECT_NOT_TRUSTED
;
893 CertFreeCertificateContext(found
);
895 CertCloseStore(disallowed
, 0);
900 CERT_CHAIN_POLICY_PARA policyPara
= { sizeof(policyPara
), 0 };
902 if (data
->dwRegPolicySettings
& WTPF_TRUSTTEST
)
903 policyPara
.dwFlags
|= CERT_CHAIN_POLICY_TRUST_TESTROOT_FLAG
;
904 if (data
->dwRegPolicySettings
& WTPF_TESTCANBEVALID
)
905 policyPara
.dwFlags
|= CERT_CHAIN_POLICY_ALLOW_TESTROOT_FLAG
;
906 if (data
->dwRegPolicySettings
& WTPF_IGNOREEXPIRATION
)
907 policyPara
.dwFlags
|=
908 CERT_CHAIN_POLICY_IGNORE_NOT_TIME_VALID_FLAG
|
909 CERT_CHAIN_POLICY_IGNORE_CTL_NOT_TIME_VALID_FLAG
|
910 CERT_CHAIN_POLICY_IGNORE_NOT_TIME_NESTED_FLAG
;
911 if (data
->dwRegPolicySettings
& WTPF_IGNOREREVOKATION
)
912 policyPara
.dwFlags
|=
913 CERT_CHAIN_POLICY_IGNORE_END_REV_UNKNOWN_FLAG
|
914 CERT_CHAIN_POLICY_IGNORE_CTL_SIGNER_REV_UNKNOWN_FLAG
|
915 CERT_CHAIN_POLICY_IGNORE_CA_REV_UNKNOWN_FLAG
|
916 CERT_CHAIN_POLICY_IGNORE_ROOT_REV_UNKNOWN_FLAG
;
917 CertVerifyCertificateChainPolicy(CERT_CHAIN_POLICY_AUTHENTICODE
,
918 data
->pasSigners
[i
].pChainContext
, &policyPara
, &policyStatus
);
919 if (policyStatus
.dwError
!= NO_ERROR
)
925 data
->padwTrustStepErrors
[TRUSTERROR_STEP_FINAL_POLICYPROV
] =
926 policyStatus
.dwError
;
927 TRACE("returning %d (%08x)\n", ret
? S_OK
: S_FALSE
,
928 data
->padwTrustStepErrors
[TRUSTERROR_STEP_FINAL_POLICYPROV
]);
929 return ret
? S_OK
: S_FALSE
;
932 static HRESULT WINAPI
WINTRUST_DefaultPolicy(CRYPT_PROVIDER_DATA
*pProvData
,
933 DWORD dwStepError
, DWORD dwRegPolicySettings
, DWORD cSigner
,
934 PWTD_GENERIC_CHAIN_POLICY_SIGNER_INFO rgpSigner
, void *pvPolicyArg
)
937 CERT_CHAIN_POLICY_STATUS policyStatus
= { sizeof(policyStatus
), 0 };
939 for (i
= 0; !policyStatus
.dwError
&& i
< cSigner
; i
++)
941 CERT_CHAIN_POLICY_PARA policyPara
= { sizeof(policyPara
), 0 };
943 if (dwRegPolicySettings
& WTPF_IGNOREEXPIRATION
)
944 policyPara
.dwFlags
|=
945 CERT_CHAIN_POLICY_IGNORE_NOT_TIME_VALID_FLAG
|
946 CERT_CHAIN_POLICY_IGNORE_CTL_NOT_TIME_VALID_FLAG
|
947 CERT_CHAIN_POLICY_IGNORE_NOT_TIME_NESTED_FLAG
;
948 if (dwRegPolicySettings
& WTPF_IGNOREREVOKATION
)
949 policyPara
.dwFlags
|=
950 CERT_CHAIN_POLICY_IGNORE_END_REV_UNKNOWN_FLAG
|
951 CERT_CHAIN_POLICY_IGNORE_CTL_SIGNER_REV_UNKNOWN_FLAG
|
952 CERT_CHAIN_POLICY_IGNORE_CA_REV_UNKNOWN_FLAG
|
953 CERT_CHAIN_POLICY_IGNORE_ROOT_REV_UNKNOWN_FLAG
;
954 CertVerifyCertificateChainPolicy(CERT_CHAIN_POLICY_BASE
,
955 rgpSigner
[i
].pChainContext
, &policyPara
, &policyStatus
);
957 return policyStatus
.dwError
;
960 HRESULT WINAPI
GenericChainFinalProv(CRYPT_PROVIDER_DATA
*data
)
962 HRESULT err
= NO_ERROR
; /* not a typo, MS confused the types */
963 WTD_GENERIC_CHAIN_POLICY_DATA
*policyData
=
964 data
->pWintrustData
->pPolicyCallbackData
;
966 TRACE("(%p)\n", data
);
968 if (data
->pWintrustData
->dwUIChoice
!= WTD_UI_NONE
)
969 FIXME("unimplemented for UI choice %d\n",
970 data
->pWintrustData
->dwUIChoice
);
971 if (!data
->csSigners
)
972 err
= TRUST_E_NOSIGNATURE
;
975 PFN_WTD_GENERIC_CHAIN_POLICY_CALLBACK policyCallback
;
977 WTD_GENERIC_CHAIN_POLICY_SIGNER_INFO
*signers
= NULL
;
981 policyCallback
= policyData
->pfnPolicyCallback
;
982 policyArg
= policyData
->pvPolicyArg
;
986 policyCallback
= WINTRUST_DefaultPolicy
;
993 signers
= data
->psPfns
->pfnAlloc(
994 data
->csSigners
* sizeof(WTD_GENERIC_CHAIN_POLICY_SIGNER_INFO
));
997 for (i
= 0; i
< data
->csSigners
; i
++)
999 signers
[i
].u
.cbSize
=
1000 sizeof(WTD_GENERIC_CHAIN_POLICY_SIGNER_INFO
);
1001 signers
[i
].pChainContext
=
1002 data
->pasSigners
[i
].pChainContext
;
1003 signers
[i
].dwSignerType
= data
->pasSigners
[i
].dwSignerType
;
1004 signers
[i
].pMsgSignerInfo
= data
->pasSigners
[i
].psSigner
;
1005 signers
[i
].dwError
= data
->pasSigners
[i
].dwError
;
1006 if (data
->pasSigners
[i
].csCounterSigners
)
1007 FIXME("unimplemented for counter signers\n");
1008 signers
[i
].cCounterSigner
= 0;
1009 signers
[i
].rgpCounterSigner
= NULL
;
1013 err
= ERROR_OUTOFMEMORY
;
1015 if (err
== NO_ERROR
)
1016 err
= policyCallback(data
, TRUSTERROR_STEP_FINAL_POLICYPROV
,
1017 data
->dwRegPolicySettings
, data
->csSigners
, signers
, policyArg
);
1018 data
->psPfns
->pfnFree(signers
);
1020 if (err
!= NO_ERROR
)
1021 data
->padwTrustStepErrors
[TRUSTERROR_STEP_FINAL_POLICYPROV
] = err
;
1022 TRACE("returning %d (%08x)\n", err
== NO_ERROR
? S_OK
: S_FALSE
,
1023 data
->padwTrustStepErrors
[TRUSTERROR_STEP_FINAL_POLICYPROV
]);
1024 return err
== NO_ERROR
? S_OK
: S_FALSE
;
1027 HRESULT WINAPI
SoftpubCleanup(CRYPT_PROVIDER_DATA
*data
)
1031 for (i
= 0; i
< data
->csSigners
; i
++)
1033 for (j
= 0; j
< data
->pasSigners
[i
].csCertChain
; j
++)
1034 CertFreeCertificateContext(data
->pasSigners
[i
].pasCertChain
[j
].pCert
);
1035 data
->psPfns
->pfnFree(data
->pasSigners
[i
].pasCertChain
);
1036 data
->psPfns
->pfnFree(data
->pasSigners
[i
].psSigner
);
1037 CertFreeCertificateChain(data
->pasSigners
[i
].pChainContext
);
1039 data
->psPfns
->pfnFree(data
->pasSigners
);
1041 for (i
= 0; i
< data
->chStores
; i
++)
1042 CertCloseStore(data
->pahStores
[i
], 0);
1043 data
->psPfns
->pfnFree(data
->pahStores
);
1047 data
->psPfns
->pfnFree(data
->u
.pPDSip
->pSip
);
1048 data
->psPfns
->pfnFree(data
->u
.pPDSip
->pCATSip
);
1049 data
->psPfns
->pfnFree(data
->u
.pPDSip
->psSipSubjectInfo
);
1050 data
->psPfns
->pfnFree(data
->u
.pPDSip
->psSipCATSubjectInfo
);
1051 data
->psPfns
->pfnFree(data
->u
.pPDSip
->psIndirectData
);
1054 CryptMsgClose(data
->hMsg
);
1056 if (data
->fOpenedFile
)
1057 CloseHandle(data
->pWintrustData
->u
.pFile
->hFile
);