2 * Copyright 2006 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
23 #define CERT_CHAIN_PARA_HAS_EXTRA_FIELDS
24 #define CERT_REVOCATION_PARA_HAS_EXTRA_FIELDS
26 #include "wine/debug.h"
27 #include "wine/unicode.h"
28 #include "crypt32_private.h"
30 WINE_DEFAULT_DEBUG_CHANNEL(crypt
);
32 #define DEFAULT_CYCLE_MODULUS 7
34 static HCERTCHAINENGINE CRYPT_defaultChainEngine
;
36 /* This represents a subset of a certificate chain engine: it doesn't include
37 * the "hOther" store described by MSDN, because I'm not sure how that's used.
38 * It also doesn't include the "hTrust" store, because I don't yet implement
39 * CTLs or complex certificate chains.
41 typedef struct _CertificateChainEngine
47 DWORD dwUrlRetrievalTimeout
;
48 DWORD MaximumCachedCertificates
;
49 DWORD CycleDetectionModulus
;
50 } CertificateChainEngine
, *PCertificateChainEngine
;
52 static inline void CRYPT_AddStoresToCollection(HCERTSTORE collection
,
53 DWORD cStores
, HCERTSTORE
*stores
)
57 for (i
= 0; i
< cStores
; i
++)
58 CertAddStoreToCollection(collection
, stores
[i
], 0, 0);
61 static inline void CRYPT_CloseStores(DWORD cStores
, HCERTSTORE
*stores
)
65 for (i
= 0; i
< cStores
; i
++)
66 CertCloseStore(stores
[i
], 0);
69 static const WCHAR rootW
[] = { 'R','o','o','t',0 };
71 static BOOL
CRYPT_CheckRestrictedRoot(HCERTSTORE store
)
77 HCERTSTORE rootStore
= CertOpenSystemStoreW(0, rootW
);
78 PCCERT_CONTEXT cert
= NULL
, check
;
83 cert
= CertEnumCertificatesInStore(store
, cert
);
88 ret
= CertGetCertificateContextProperty(cert
, CERT_HASH_PROP_ID
,
92 CRYPT_HASH_BLOB blob
= { sizeof(hash
), hash
};
94 check
= CertFindCertificateInStore(rootStore
,
95 cert
->dwCertEncodingType
, 0, CERT_FIND_SHA1_HASH
, &blob
,
100 CertFreeCertificateContext(check
);
103 } while (ret
&& cert
);
105 CertFreeCertificateContext(cert
);
106 CertCloseStore(rootStore
, 0);
111 HCERTCHAINENGINE
CRYPT_CreateChainEngine(HCERTSTORE root
,
112 PCERT_CHAIN_ENGINE_CONFIG pConfig
)
114 static const WCHAR caW
[] = { 'C','A',0 };
115 static const WCHAR myW
[] = { 'M','y',0 };
116 static const WCHAR trustW
[] = { 'T','r','u','s','t',0 };
117 PCertificateChainEngine engine
=
118 CryptMemAlloc(sizeof(CertificateChainEngine
));
122 HCERTSTORE worldStores
[4];
125 engine
->hRoot
= root
;
126 engine
->hWorld
= CertOpenStore(CERT_STORE_PROV_COLLECTION
, 0, 0,
127 CERT_STORE_CREATE_NEW_FLAG
, NULL
);
128 worldStores
[0] = CertDuplicateStore(engine
->hRoot
);
129 worldStores
[1] = CertOpenSystemStoreW(0, caW
);
130 worldStores
[2] = CertOpenSystemStoreW(0, myW
);
131 worldStores
[3] = CertOpenSystemStoreW(0, trustW
);
132 CRYPT_AddStoresToCollection(engine
->hWorld
,
133 sizeof(worldStores
) / sizeof(worldStores
[0]), worldStores
);
134 CRYPT_AddStoresToCollection(engine
->hWorld
,
135 pConfig
->cAdditionalStore
, pConfig
->rghAdditionalStore
);
136 CRYPT_CloseStores(sizeof(worldStores
) / sizeof(worldStores
[0]),
138 engine
->dwFlags
= pConfig
->dwFlags
;
139 engine
->dwUrlRetrievalTimeout
= pConfig
->dwUrlRetrievalTimeout
;
140 engine
->MaximumCachedCertificates
=
141 pConfig
->MaximumCachedCertificates
;
142 if (pConfig
->CycleDetectionModulus
)
143 engine
->CycleDetectionModulus
= pConfig
->CycleDetectionModulus
;
145 engine
->CycleDetectionModulus
= DEFAULT_CYCLE_MODULUS
;
147 return (HCERTCHAINENGINE
)engine
;
150 BOOL WINAPI
CertCreateCertificateChainEngine(PCERT_CHAIN_ENGINE_CONFIG pConfig
,
151 HCERTCHAINENGINE
*phChainEngine
)
155 TRACE("(%p, %p)\n", pConfig
, phChainEngine
);
157 if (pConfig
->cbSize
!= sizeof(*pConfig
))
159 SetLastError(E_INVALIDARG
);
162 *phChainEngine
= NULL
;
163 ret
= CRYPT_CheckRestrictedRoot(pConfig
->hRestrictedRoot
);
167 HCERTCHAINENGINE engine
;
169 if (pConfig
->hRestrictedRoot
)
170 root
= CertDuplicateStore(pConfig
->hRestrictedRoot
);
172 root
= CertOpenSystemStoreW(0, rootW
);
173 engine
= CRYPT_CreateChainEngine(root
, pConfig
);
176 *phChainEngine
= engine
;
185 VOID WINAPI
CertFreeCertificateChainEngine(HCERTCHAINENGINE hChainEngine
)
187 PCertificateChainEngine engine
= (PCertificateChainEngine
)hChainEngine
;
189 TRACE("(%p)\n", hChainEngine
);
191 if (engine
&& InterlockedDecrement(&engine
->ref
) == 0)
193 CertCloseStore(engine
->hWorld
, 0);
194 CertCloseStore(engine
->hRoot
, 0);
195 CryptMemFree(engine
);
199 static HCERTCHAINENGINE
CRYPT_GetDefaultChainEngine(void)
201 if (!CRYPT_defaultChainEngine
)
203 CERT_CHAIN_ENGINE_CONFIG config
= { 0 };
204 HCERTCHAINENGINE engine
;
206 config
.cbSize
= sizeof(config
);
207 CertCreateCertificateChainEngine(&config
, &engine
);
208 InterlockedCompareExchangePointer(&CRYPT_defaultChainEngine
, engine
,
210 if (CRYPT_defaultChainEngine
!= engine
)
211 CertFreeCertificateChainEngine(engine
);
213 return CRYPT_defaultChainEngine
;
216 void default_chain_engine_free(void)
218 CertFreeCertificateChainEngine(CRYPT_defaultChainEngine
);
221 typedef struct _CertificateChain
223 CERT_CHAIN_CONTEXT context
;
226 } CertificateChain
, *PCertificateChain
;
228 static inline BOOL
CRYPT_IsCertificateSelfSigned(PCCERT_CONTEXT cert
)
230 return CertCompareCertificateName(cert
->dwCertEncodingType
,
231 &cert
->pCertInfo
->Subject
, &cert
->pCertInfo
->Issuer
);
234 static void CRYPT_FreeChainElement(PCERT_CHAIN_ELEMENT element
)
236 CertFreeCertificateContext(element
->pCertContext
);
237 CryptMemFree(element
);
240 static void CRYPT_CheckSimpleChainForCycles(PCERT_SIMPLE_CHAIN chain
)
242 DWORD i
, j
, cyclicCertIndex
= 0;
244 /* O(n^2) - I don't think there's a faster way */
245 for (i
= 0; !cyclicCertIndex
&& i
< chain
->cElement
; i
++)
246 for (j
= i
+ 1; !cyclicCertIndex
&& j
< chain
->cElement
; j
++)
247 if (CertCompareCertificate(X509_ASN_ENCODING
,
248 chain
->rgpElement
[i
]->pCertContext
->pCertInfo
,
249 chain
->rgpElement
[j
]->pCertContext
->pCertInfo
))
253 chain
->rgpElement
[cyclicCertIndex
]->TrustStatus
.dwErrorStatus
254 |= CERT_TRUST_IS_CYCLIC
| CERT_TRUST_INVALID_BASIC_CONSTRAINTS
;
255 /* Release remaining certs */
256 for (i
= cyclicCertIndex
+ 1; i
< chain
->cElement
; i
++)
257 CRYPT_FreeChainElement(chain
->rgpElement
[i
]);
259 chain
->cElement
= cyclicCertIndex
+ 1;
263 /* Checks whether the chain is cyclic by examining the last element's status */
264 static inline BOOL
CRYPT_IsSimpleChainCyclic(PCERT_SIMPLE_CHAIN chain
)
267 return chain
->rgpElement
[chain
->cElement
- 1]->TrustStatus
.dwErrorStatus
268 & CERT_TRUST_IS_CYCLIC
;
273 static inline void CRYPT_CombineTrustStatus(CERT_TRUST_STATUS
*chainStatus
,
274 CERT_TRUST_STATUS
*elementStatus
)
276 /* Any error that applies to an element also applies to a chain.. */
277 chainStatus
->dwErrorStatus
|= elementStatus
->dwErrorStatus
;
278 /* but the bottom nibble of an element's info status doesn't apply to the
281 chainStatus
->dwInfoStatus
|= (elementStatus
->dwInfoStatus
& 0xfffffff0);
284 static BOOL
CRYPT_AddCertToSimpleChain(PCertificateChainEngine engine
,
285 PCERT_SIMPLE_CHAIN chain
, PCCERT_CONTEXT cert
, DWORD subjectInfoStatus
)
288 PCERT_CHAIN_ELEMENT element
= CryptMemAlloc(sizeof(CERT_CHAIN_ELEMENT
));
292 if (!chain
->cElement
)
293 chain
->rgpElement
= CryptMemAlloc(sizeof(PCERT_CHAIN_ELEMENT
));
295 chain
->rgpElement
= CryptMemRealloc(chain
->rgpElement
,
296 (chain
->cElement
+ 1) * sizeof(PCERT_CHAIN_ELEMENT
));
297 if (chain
->rgpElement
)
299 chain
->rgpElement
[chain
->cElement
++] = element
;
300 memset(element
, 0, sizeof(CERT_CHAIN_ELEMENT
));
301 element
->cbSize
= sizeof(CERT_CHAIN_ELEMENT
);
302 element
->pCertContext
= CertDuplicateCertificateContext(cert
);
303 if (chain
->cElement
> 1)
304 chain
->rgpElement
[chain
->cElement
- 2]->TrustStatus
.dwInfoStatus
306 /* FIXME: initialize the rest of element */
307 if (!(chain
->cElement
% engine
->CycleDetectionModulus
))
308 CRYPT_CheckSimpleChainForCycles(chain
);
309 CRYPT_CombineTrustStatus(&chain
->TrustStatus
,
310 &element
->TrustStatus
);
314 CryptMemFree(element
);
319 static void CRYPT_FreeSimpleChain(PCERT_SIMPLE_CHAIN chain
)
323 for (i
= 0; i
< chain
->cElement
; i
++)
324 CRYPT_FreeChainElement(chain
->rgpElement
[i
]);
325 CryptMemFree(chain
->rgpElement
);
329 static void CRYPT_CheckTrustedStatus(HCERTSTORE hRoot
,
330 PCERT_CHAIN_ELEMENT rootElement
)
333 DWORD size
= sizeof(hash
);
334 CRYPT_HASH_BLOB blob
= { sizeof(hash
), hash
};
335 PCCERT_CONTEXT trustedRoot
;
337 CertGetCertificateContextProperty(rootElement
->pCertContext
,
338 CERT_HASH_PROP_ID
, hash
, &size
);
339 trustedRoot
= CertFindCertificateInStore(hRoot
,
340 rootElement
->pCertContext
->dwCertEncodingType
, 0, CERT_FIND_SHA1_HASH
,
343 rootElement
->TrustStatus
.dwErrorStatus
|=
344 CERT_TRUST_IS_UNTRUSTED_ROOT
;
346 CertFreeCertificateContext(trustedRoot
);
349 static void CRYPT_CheckRootCert(HCERTCHAINENGINE hRoot
,
350 PCERT_CHAIN_ELEMENT rootElement
)
352 PCCERT_CONTEXT root
= rootElement
->pCertContext
;
354 if (!CryptVerifyCertificateSignatureEx(0, root
->dwCertEncodingType
,
355 CRYPT_VERIFY_CERT_SIGN_SUBJECT_CERT
, (void *)root
,
356 CRYPT_VERIFY_CERT_SIGN_ISSUER_CERT
, (void *)root
, 0, NULL
))
358 TRACE("Last certificate's signature is invalid\n");
359 rootElement
->TrustStatus
.dwErrorStatus
|=
360 CERT_TRUST_IS_NOT_SIGNATURE_VALID
;
362 CRYPT_CheckTrustedStatus(hRoot
, rootElement
);
365 /* Decodes a cert's basic constraints extension (either szOID_BASIC_CONSTRAINTS
366 * or szOID_BASIC_CONSTRAINTS2, whichever is present) into a
367 * CERT_BASIC_CONSTRAINTS2_INFO. If it neither extension is present, sets
368 * constraints->fCA to defaultIfNotSpecified.
369 * Returns FALSE if the extension is present but couldn't be decoded.
371 static BOOL
CRYPT_DecodeBasicConstraints(PCCERT_CONTEXT cert
,
372 CERT_BASIC_CONSTRAINTS2_INFO
*constraints
, BOOL defaultIfNotSpecified
)
375 PCERT_EXTENSION ext
= CertFindExtension(szOID_BASIC_CONSTRAINTS
,
376 cert
->pCertInfo
->cExtension
, cert
->pCertInfo
->rgExtension
);
378 constraints
->fPathLenConstraint
= FALSE
;
381 CERT_BASIC_CONSTRAINTS_INFO
*info
;
384 ret
= CryptDecodeObjectEx(X509_ASN_ENCODING
, szOID_BASIC_CONSTRAINTS
,
385 ext
->Value
.pbData
, ext
->Value
.cbData
, CRYPT_DECODE_ALLOC_FLAG
,
386 NULL
, (LPBYTE
)&info
, &size
);
389 if (info
->SubjectType
.cbData
== 1)
391 info
->SubjectType
.pbData
[0] & CERT_CA_SUBJECT_FLAG
;
397 ext
= CertFindExtension(szOID_BASIC_CONSTRAINTS2
,
398 cert
->pCertInfo
->cExtension
, cert
->pCertInfo
->rgExtension
);
401 DWORD size
= sizeof(CERT_BASIC_CONSTRAINTS2_INFO
);
403 ret
= CryptDecodeObjectEx(X509_ASN_ENCODING
,
404 szOID_BASIC_CONSTRAINTS2
, ext
->Value
.pbData
, ext
->Value
.cbData
,
405 0, NULL
, constraints
, &size
);
408 constraints
->fCA
= defaultIfNotSpecified
;
413 /* Checks element's basic constraints to see if it can act as a CA, with
414 * remainingCAs CAs left in this chain. Updates chainConstraints with the
415 * element's constraints, if:
416 * 1. chainConstraints doesn't have a path length constraint, or
417 * 2. element's path length constraint is smaller than chainConstraints's
418 * Sets *pathLengthConstraintViolated to TRUE if a path length violation
420 * Returns TRUE if the element can be a CA, and the length of the remaining
423 static BOOL
CRYPT_CheckBasicConstraintsForCA(PCCERT_CONTEXT cert
,
424 CERT_BASIC_CONSTRAINTS2_INFO
*chainConstraints
, DWORD remainingCAs
,
425 BOOL
*pathLengthConstraintViolated
)
427 BOOL validBasicConstraints
;
428 CERT_BASIC_CONSTRAINTS2_INFO constraints
;
430 if ((validBasicConstraints
= CRYPT_DecodeBasicConstraints(cert
,
431 &constraints
, TRUE
)))
433 if (!constraints
.fCA
)
435 TRACE("chain element %d can't be a CA\n", remainingCAs
+ 1);
436 validBasicConstraints
= FALSE
;
438 else if (constraints
.fPathLenConstraint
)
440 /* If the element has path length constraints, they apply to the
441 * entire remaining chain.
443 if (!chainConstraints
->fPathLenConstraint
||
444 constraints
.dwPathLenConstraint
<
445 chainConstraints
->dwPathLenConstraint
)
447 TRACE("setting path length constraint to %d\n",
448 chainConstraints
->dwPathLenConstraint
);
449 chainConstraints
->fPathLenConstraint
= TRUE
;
450 chainConstraints
->dwPathLenConstraint
=
451 constraints
.dwPathLenConstraint
;
455 if (chainConstraints
->fPathLenConstraint
&&
456 remainingCAs
> chainConstraints
->dwPathLenConstraint
)
458 TRACE("remaining CAs %d exceed max path length %d\n", remainingCAs
,
459 chainConstraints
->dwPathLenConstraint
);
460 validBasicConstraints
= FALSE
;
461 *pathLengthConstraintViolated
= TRUE
;
463 return validBasicConstraints
;
466 static BOOL
url_matches(LPCWSTR constraint
, LPCWSTR name
,
467 DWORD
*trustErrorStatus
)
471 TRACE("%s, %s\n", debugstr_w(constraint
), debugstr_w(name
));
474 *trustErrorStatus
|= CERT_TRUST_INVALID_NAME_CONSTRAINTS
;
477 else if (constraint
[0] == '.')
479 if (lstrlenW(name
) > lstrlenW(constraint
))
480 match
= !lstrcmpiW(name
+ lstrlenW(name
) - lstrlenW(constraint
),
484 match
= !lstrcmpiW(constraint
, name
);
488 static BOOL
rfc822_name_matches(LPCWSTR constraint
, LPCWSTR name
,
489 DWORD
*trustErrorStatus
)
494 TRACE("%s, %s\n", debugstr_w(constraint
), debugstr_w(name
));
497 *trustErrorStatus
|= CERT_TRUST_INVALID_NAME_CONSTRAINTS
;
500 else if ((at
= strchrW(constraint
, '@')))
501 match
= !lstrcmpiW(constraint
, name
);
504 if ((at
= strchrW(name
, '@')))
505 match
= url_matches(constraint
, at
+ 1, trustErrorStatus
);
507 match
= !lstrcmpiW(constraint
, name
);
512 static BOOL
dns_name_matches(LPCWSTR constraint
, LPCWSTR name
,
513 DWORD
*trustErrorStatus
)
517 TRACE("%s, %s\n", debugstr_w(constraint
), debugstr_w(name
));
520 *trustErrorStatus
|= CERT_TRUST_INVALID_NAME_CONSTRAINTS
;
523 else if (lstrlenW(name
) >= lstrlenW(constraint
))
524 match
= !lstrcmpiW(name
+ lstrlenW(name
) - lstrlenW(constraint
),
526 /* else: name is too short, no match */
531 static BOOL
ip_address_matches(const CRYPT_DATA_BLOB
*constraint
,
532 const CRYPT_DATA_BLOB
*name
, DWORD
*trustErrorStatus
)
536 TRACE("(%d, %p), (%d, %p)\n", constraint
->cbData
, constraint
->pbData
,
537 name
->cbData
, name
->pbData
);
539 if (constraint
->cbData
!= sizeof(DWORD
) * 2)
540 *trustErrorStatus
|= CERT_TRUST_INVALID_NAME_CONSTRAINTS
;
541 else if (name
->cbData
== sizeof(DWORD
))
543 DWORD subnet
, mask
, addr
;
545 memcpy(&subnet
, constraint
->pbData
, sizeof(subnet
));
546 memcpy(&mask
, constraint
->pbData
+ sizeof(subnet
), sizeof(mask
));
547 memcpy(&addr
, name
->pbData
, sizeof(addr
));
548 /* These are really in big-endian order, but for equality matching we
549 * don't need to swap to host order
551 match
= (subnet
& mask
) == (addr
& mask
);
553 /* else: name is wrong size, no match */
558 static void CRYPT_FindMatchingNameEntry(const CERT_ALT_NAME_ENTRY
*constraint
,
559 const CERT_ALT_NAME_INFO
*subjectName
, DWORD
*trustErrorStatus
,
560 DWORD errorIfFound
, DWORD errorIfNotFound
)
565 for (i
= 0; i
< subjectName
->cAltEntry
; i
++)
567 if (subjectName
->rgAltEntry
[i
].dwAltNameChoice
==
568 constraint
->dwAltNameChoice
)
570 switch (constraint
->dwAltNameChoice
)
572 case CERT_ALT_NAME_RFC822_NAME
:
573 match
= rfc822_name_matches(constraint
->u
.pwszURL
,
574 subjectName
->rgAltEntry
[i
].u
.pwszURL
, trustErrorStatus
);
576 case CERT_ALT_NAME_DNS_NAME
:
577 match
= dns_name_matches(constraint
->u
.pwszURL
,
578 subjectName
->rgAltEntry
[i
].u
.pwszURL
, trustErrorStatus
);
580 case CERT_ALT_NAME_URL
:
581 match
= url_matches(constraint
->u
.pwszURL
,
582 subjectName
->rgAltEntry
[i
].u
.pwszURL
, trustErrorStatus
);
584 case CERT_ALT_NAME_IP_ADDRESS
:
585 match
= ip_address_matches(&constraint
->u
.IPAddress
,
586 &subjectName
->rgAltEntry
[i
].u
.IPAddress
, trustErrorStatus
);
588 case CERT_ALT_NAME_DIRECTORY_NAME
:
590 ERR("name choice %d unsupported in this context\n",
591 constraint
->dwAltNameChoice
);
593 CERT_TRUST_HAS_NOT_SUPPORTED_NAME_CONSTRAINT
;
597 *trustErrorStatus
|= match
? errorIfFound
: errorIfNotFound
;
600 static void CRYPT_CheckNameConstraints(
601 const CERT_NAME_CONSTRAINTS_INFO
*nameConstraints
, const CERT_INFO
*cert
,
602 DWORD
*trustErrorStatus
)
604 /* If there aren't any existing constraints, don't bother checking */
605 if (nameConstraints
->cPermittedSubtree
|| nameConstraints
->cExcludedSubtree
)
609 if ((ext
= CertFindExtension(szOID_SUBJECT_ALT_NAME
, cert
->cExtension
,
612 CERT_ALT_NAME_INFO
*subjectName
;
615 if (CryptDecodeObjectEx(X509_ASN_ENCODING
, X509_ALTERNATE_NAME
,
616 ext
->Value
.pbData
, ext
->Value
.cbData
,
617 CRYPT_DECODE_ALLOC_FLAG
| CRYPT_DECODE_NOCOPY_FLAG
, NULL
,
618 &subjectName
, &size
))
622 for (i
= 0; i
< nameConstraints
->cExcludedSubtree
; i
++)
623 CRYPT_FindMatchingNameEntry(
624 &nameConstraints
->rgExcludedSubtree
[i
].Base
, subjectName
,
626 CERT_TRUST_HAS_EXCLUDED_NAME_CONSTRAINT
, 0);
627 for (i
= 0; i
< nameConstraints
->cPermittedSubtree
; i
++)
628 CRYPT_FindMatchingNameEntry(
629 &nameConstraints
->rgPermittedSubtree
[i
].Base
, subjectName
,
631 0, CERT_TRUST_HAS_NOT_PERMITTED_NAME_CONSTRAINT
);
632 LocalFree(subjectName
);
637 if (nameConstraints
->cPermittedSubtree
)
639 CERT_TRUST_HAS_NOT_PERMITTED_NAME_CONSTRAINT
;
640 if (nameConstraints
->cExcludedSubtree
)
642 CERT_TRUST_HAS_EXCLUDED_NAME_CONSTRAINT
;
647 /* Gets cert's name constraints, if any. Free with LocalFree. */
648 static CERT_NAME_CONSTRAINTS_INFO
*CRYPT_GetNameConstraints(CERT_INFO
*cert
)
650 CERT_NAME_CONSTRAINTS_INFO
*info
= NULL
;
654 if ((ext
= CertFindExtension(szOID_NAME_CONSTRAINTS
, cert
->cExtension
,
659 CryptDecodeObjectEx(X509_ASN_ENCODING
, X509_NAME_CONSTRAINTS
,
660 ext
->Value
.pbData
, ext
->Value
.cbData
,
661 CRYPT_DECODE_ALLOC_FLAG
| CRYPT_DECODE_NOCOPY_FLAG
, NULL
, &info
,
667 static void CRYPT_CheckChainNameConstraints(PCERT_SIMPLE_CHAIN chain
)
671 /* Microsoft's implementation appears to violate RFC 3280: according to
672 * MSDN, the various CERT_TRUST_*_NAME_CONSTRAINT errors are set if a CA's
673 * name constraint is violated in the end cert. According to RFC 3280,
674 * the constraints should be checked against every subsequent certificate
675 * in the chain, not just the end cert.
676 * Microsoft's implementation also sets the name constraint errors on the
677 * certs whose constraints were violated, not on the certs that violated
679 * In order to be error-compatible with Microsoft's implementation, while
680 * still adhering to RFC 3280, I use a O(n ^ 2) algorithm to check name
683 for (i
= chain
->cElement
- 1; i
> 0; i
--)
685 CERT_NAME_CONSTRAINTS_INFO
*nameConstraints
;
687 if ((nameConstraints
= CRYPT_GetNameConstraints(
688 chain
->rgpElement
[i
]->pCertContext
->pCertInfo
)))
690 for (j
= i
- 1; j
>= 0; j
--)
692 DWORD errorStatus
= 0;
694 /* According to RFC 3280, self-signed certs don't have name
695 * constraints checked unless they're the end cert.
697 if (j
== 0 || !CRYPT_IsCertificateSelfSigned(
698 chain
->rgpElement
[j
]->pCertContext
))
700 CRYPT_CheckNameConstraints(nameConstraints
,
701 chain
->rgpElement
[i
]->pCertContext
->pCertInfo
,
703 chain
->rgpElement
[i
]->TrustStatus
.dwErrorStatus
|=
707 LocalFree(nameConstraints
);
712 static void CRYPT_CheckSimpleChain(PCertificateChainEngine engine
,
713 PCERT_SIMPLE_CHAIN chain
, LPFILETIME time
)
715 PCERT_CHAIN_ELEMENT rootElement
= chain
->rgpElement
[chain
->cElement
- 1];
717 BOOL pathLengthConstraintViolated
= FALSE
;
718 CERT_BASIC_CONSTRAINTS2_INFO constraints
= { TRUE
, FALSE
, 0 };
720 for (i
= chain
->cElement
- 1; i
>= 0; i
--)
722 if (CertVerifyTimeValidity(time
,
723 chain
->rgpElement
[i
]->pCertContext
->pCertInfo
) != 0)
724 chain
->rgpElement
[i
]->TrustStatus
.dwErrorStatus
|=
725 CERT_TRUST_IS_NOT_TIME_VALID
;
728 /* Check the signature of the cert this issued */
729 if (!CryptVerifyCertificateSignatureEx(0, X509_ASN_ENCODING
,
730 CRYPT_VERIFY_CERT_SIGN_SUBJECT_CERT
,
731 (void *)chain
->rgpElement
[i
- 1]->pCertContext
,
732 CRYPT_VERIFY_CERT_SIGN_ISSUER_CERT
,
733 (void *)chain
->rgpElement
[i
]->pCertContext
, 0, NULL
))
734 chain
->rgpElement
[i
- 1]->TrustStatus
.dwErrorStatus
|=
735 CERT_TRUST_IS_NOT_SIGNATURE_VALID
;
736 /* Once a path length constraint has been violated, every remaining
737 * CA cert's basic constraints is considered invalid.
739 if (pathLengthConstraintViolated
)
740 chain
->rgpElement
[i
]->TrustStatus
.dwErrorStatus
|=
741 CERT_TRUST_INVALID_BASIC_CONSTRAINTS
;
742 else if (!CRYPT_CheckBasicConstraintsForCA(
743 chain
->rgpElement
[i
]->pCertContext
, &constraints
, i
- 1,
744 &pathLengthConstraintViolated
))
745 chain
->rgpElement
[i
]->TrustStatus
.dwErrorStatus
|=
746 CERT_TRUST_INVALID_BASIC_CONSTRAINTS
;
747 else if (constraints
.fPathLenConstraint
&&
748 constraints
.dwPathLenConstraint
)
750 /* This one's valid - decrement max length */
751 constraints
.dwPathLenConstraint
--;
754 if (CRYPT_IsSimpleChainCyclic(chain
))
756 /* If the chain is cyclic, then the path length constraints
757 * are violated, because the chain is infinitely long.
759 pathLengthConstraintViolated
= TRUE
;
760 chain
->TrustStatus
.dwErrorStatus
|=
761 CERT_TRUST_IS_PARTIAL_CHAIN
|
762 CERT_TRUST_INVALID_BASIC_CONSTRAINTS
;
764 /* FIXME: check valid usages */
765 CRYPT_CombineTrustStatus(&chain
->TrustStatus
,
766 &chain
->rgpElement
[i
]->TrustStatus
);
768 CRYPT_CheckChainNameConstraints(chain
);
769 if (CRYPT_IsCertificateSelfSigned(rootElement
->pCertContext
))
771 rootElement
->TrustStatus
.dwInfoStatus
|=
772 CERT_TRUST_IS_SELF_SIGNED
| CERT_TRUST_HAS_NAME_MATCH_ISSUER
;
773 CRYPT_CheckRootCert(engine
->hRoot
, rootElement
);
775 CRYPT_CombineTrustStatus(&chain
->TrustStatus
, &rootElement
->TrustStatus
);
778 static PCCERT_CONTEXT
CRYPT_GetIssuer(HCERTSTORE store
, PCCERT_CONTEXT subject
,
779 PCCERT_CONTEXT prevIssuer
, DWORD
*infoStatus
)
781 PCCERT_CONTEXT issuer
= NULL
;
786 if ((ext
= CertFindExtension(szOID_AUTHORITY_KEY_IDENTIFIER
,
787 subject
->pCertInfo
->cExtension
, subject
->pCertInfo
->rgExtension
)))
789 CERT_AUTHORITY_KEY_ID_INFO
*info
;
792 ret
= CryptDecodeObjectEx(subject
->dwCertEncodingType
,
793 X509_AUTHORITY_KEY_ID
, ext
->Value
.pbData
, ext
->Value
.cbData
,
794 CRYPT_DECODE_ALLOC_FLAG
| CRYPT_DECODE_NOCOPY_FLAG
, NULL
,
800 if (info
->CertIssuer
.cbData
&& info
->CertSerialNumber
.cbData
)
802 id
.dwIdChoice
= CERT_ID_ISSUER_SERIAL_NUMBER
;
803 memcpy(&id
.u
.IssuerSerialNumber
.Issuer
, &info
->CertIssuer
,
804 sizeof(CERT_NAME_BLOB
));
805 memcpy(&id
.u
.IssuerSerialNumber
.SerialNumber
,
806 &info
->CertSerialNumber
, sizeof(CRYPT_INTEGER_BLOB
));
807 issuer
= CertFindCertificateInStore(store
,
808 subject
->dwCertEncodingType
, 0, CERT_FIND_CERT_ID
, &id
,
811 *infoStatus
= CERT_TRUST_HAS_EXACT_MATCH_ISSUER
;
813 else if (info
->KeyId
.cbData
)
815 id
.dwIdChoice
= CERT_ID_KEY_IDENTIFIER
;
816 memcpy(&id
.u
.KeyId
, &info
->KeyId
, sizeof(CRYPT_HASH_BLOB
));
817 issuer
= CertFindCertificateInStore(store
,
818 subject
->dwCertEncodingType
, 0, CERT_FIND_CERT_ID
, &id
,
821 *infoStatus
= CERT_TRUST_HAS_KEY_MATCH_ISSUER
;
826 else if ((ext
= CertFindExtension(szOID_AUTHORITY_KEY_IDENTIFIER2
,
827 subject
->pCertInfo
->cExtension
, subject
->pCertInfo
->rgExtension
)))
829 CERT_AUTHORITY_KEY_ID2_INFO
*info
;
832 ret
= CryptDecodeObjectEx(subject
->dwCertEncodingType
,
833 X509_AUTHORITY_KEY_ID2
, ext
->Value
.pbData
, ext
->Value
.cbData
,
834 CRYPT_DECODE_ALLOC_FLAG
| CRYPT_DECODE_NOCOPY_FLAG
, NULL
,
840 if (info
->AuthorityCertIssuer
.cAltEntry
&&
841 info
->AuthorityCertSerialNumber
.cbData
)
843 PCERT_ALT_NAME_ENTRY directoryName
= NULL
;
846 for (i
= 0; !directoryName
&&
847 i
< info
->AuthorityCertIssuer
.cAltEntry
; i
++)
848 if (info
->AuthorityCertIssuer
.rgAltEntry
[i
].dwAltNameChoice
849 == CERT_ALT_NAME_DIRECTORY_NAME
)
851 &info
->AuthorityCertIssuer
.rgAltEntry
[i
];
854 id
.dwIdChoice
= CERT_ID_ISSUER_SERIAL_NUMBER
;
855 memcpy(&id
.u
.IssuerSerialNumber
.Issuer
,
856 &directoryName
->u
.DirectoryName
, sizeof(CERT_NAME_BLOB
));
857 memcpy(&id
.u
.IssuerSerialNumber
.SerialNumber
,
858 &info
->AuthorityCertSerialNumber
,
859 sizeof(CRYPT_INTEGER_BLOB
));
860 issuer
= CertFindCertificateInStore(store
,
861 subject
->dwCertEncodingType
, 0, CERT_FIND_CERT_ID
, &id
,
864 *infoStatus
= CERT_TRUST_HAS_EXACT_MATCH_ISSUER
;
867 FIXME("no supported name type in authority key id2\n");
869 else if (info
->KeyId
.cbData
)
871 id
.dwIdChoice
= CERT_ID_KEY_IDENTIFIER
;
872 memcpy(&id
.u
.KeyId
, &info
->KeyId
, sizeof(CRYPT_HASH_BLOB
));
873 issuer
= CertFindCertificateInStore(store
,
874 subject
->dwCertEncodingType
, 0, CERT_FIND_CERT_ID
, &id
,
877 *infoStatus
= CERT_TRUST_HAS_KEY_MATCH_ISSUER
;
884 issuer
= CertFindCertificateInStore(store
,
885 subject
->dwCertEncodingType
, 0, CERT_FIND_SUBJECT_NAME
,
886 &subject
->pCertInfo
->Issuer
, prevIssuer
);
888 *infoStatus
= CERT_TRUST_HAS_NAME_MATCH_ISSUER
;
893 /* Builds a simple chain by finding an issuer for the last cert in the chain,
894 * until reaching a self-signed cert, or until no issuer can be found.
896 static BOOL
CRYPT_BuildSimpleChain(PCertificateChainEngine engine
,
897 HCERTSTORE world
, PCERT_SIMPLE_CHAIN chain
)
900 PCCERT_CONTEXT cert
= chain
->rgpElement
[chain
->cElement
- 1]->pCertContext
;
902 while (ret
&& !CRYPT_IsSimpleChainCyclic(chain
) &&
903 !CRYPT_IsCertificateSelfSigned(cert
))
906 PCCERT_CONTEXT issuer
= CRYPT_GetIssuer(world
, cert
, NULL
, &infoStatus
);
910 ret
= CRYPT_AddCertToSimpleChain(engine
, chain
, issuer
, infoStatus
);
911 /* CRYPT_AddCertToSimpleChain add-ref's the issuer, so free it to
912 * close the enumeration that found it
914 CertFreeCertificateContext(issuer
);
919 TRACE("Couldn't find issuer, halting chain creation\n");
920 chain
->TrustStatus
.dwErrorStatus
|= CERT_TRUST_IS_PARTIAL_CHAIN
;
927 static BOOL
CRYPT_GetSimpleChainForCert(PCertificateChainEngine engine
,
928 HCERTSTORE world
, PCCERT_CONTEXT cert
, LPFILETIME pTime
,
929 PCERT_SIMPLE_CHAIN
*ppChain
)
932 PCERT_SIMPLE_CHAIN chain
;
934 TRACE("(%p, %p, %p, %p)\n", engine
, world
, cert
, pTime
);
936 chain
= CryptMemAlloc(sizeof(CERT_SIMPLE_CHAIN
));
939 memset(chain
, 0, sizeof(CERT_SIMPLE_CHAIN
));
940 chain
->cbSize
= sizeof(CERT_SIMPLE_CHAIN
);
941 ret
= CRYPT_AddCertToSimpleChain(engine
, chain
, cert
, 0);
944 ret
= CRYPT_BuildSimpleChain(engine
, world
, chain
);
946 CRYPT_CheckSimpleChain(engine
, chain
, pTime
);
950 CRYPT_FreeSimpleChain(chain
);
958 static BOOL
CRYPT_BuildCandidateChainFromCert(HCERTCHAINENGINE hChainEngine
,
959 PCCERT_CONTEXT cert
, LPFILETIME pTime
, HCERTSTORE hAdditionalStore
,
960 PCertificateChain
*ppChain
)
962 PCertificateChainEngine engine
= (PCertificateChainEngine
)hChainEngine
;
963 PCERT_SIMPLE_CHAIN simpleChain
= NULL
;
967 world
= CertOpenStore(CERT_STORE_PROV_COLLECTION
, 0, 0,
968 CERT_STORE_CREATE_NEW_FLAG
, NULL
);
969 CertAddStoreToCollection(world
, engine
->hWorld
, 0, 0);
970 if (hAdditionalStore
)
971 CertAddStoreToCollection(world
, hAdditionalStore
, 0, 0);
972 /* FIXME: only simple chains are supported for now, as CTLs aren't
975 if ((ret
= CRYPT_GetSimpleChainForCert(engine
, world
, cert
, pTime
,
978 PCertificateChain chain
= CryptMemAlloc(sizeof(CertificateChain
));
983 chain
->world
= world
;
984 chain
->context
.cbSize
= sizeof(CERT_CHAIN_CONTEXT
);
985 chain
->context
.TrustStatus
= simpleChain
->TrustStatus
;
986 chain
->context
.cChain
= 1;
987 chain
->context
.rgpChain
= CryptMemAlloc(sizeof(PCERT_SIMPLE_CHAIN
));
988 chain
->context
.rgpChain
[0] = simpleChain
;
989 chain
->context
.cLowerQualityChainContext
= 0;
990 chain
->context
.rgpLowerQualityChainContext
= NULL
;
991 chain
->context
.fHasRevocationFreshnessTime
= FALSE
;
992 chain
->context
.dwRevocationFreshnessTime
= 0;
1001 /* Makes and returns a copy of chain, up to and including element iElement. */
1002 static PCERT_SIMPLE_CHAIN
CRYPT_CopySimpleChainToElement(
1003 PCERT_SIMPLE_CHAIN chain
, DWORD iElement
)
1005 PCERT_SIMPLE_CHAIN copy
= CryptMemAlloc(sizeof(CERT_SIMPLE_CHAIN
));
1009 memset(copy
, 0, sizeof(CERT_SIMPLE_CHAIN
));
1010 copy
->cbSize
= sizeof(CERT_SIMPLE_CHAIN
);
1012 CryptMemAlloc((iElement
+ 1) * sizeof(PCERT_CHAIN_ELEMENT
));
1013 if (copy
->rgpElement
)
1018 memset(copy
->rgpElement
, 0,
1019 (iElement
+ 1) * sizeof(PCERT_CHAIN_ELEMENT
));
1020 for (i
= 0; ret
&& i
<= iElement
; i
++)
1022 PCERT_CHAIN_ELEMENT element
=
1023 CryptMemAlloc(sizeof(CERT_CHAIN_ELEMENT
));
1027 *element
= *chain
->rgpElement
[i
];
1028 element
->pCertContext
= CertDuplicateCertificateContext(
1029 chain
->rgpElement
[i
]->pCertContext
);
1030 /* Reset the trust status of the copied element, it'll get
1031 * rechecked after the new chain is done.
1033 memset(&element
->TrustStatus
, 0, sizeof(CERT_TRUST_STATUS
));
1034 copy
->rgpElement
[copy
->cElement
++] = element
;
1041 for (i
= 0; i
<= iElement
; i
++)
1042 CryptMemFree(copy
->rgpElement
[i
]);
1043 CryptMemFree(copy
->rgpElement
);
1057 static void CRYPT_FreeLowerQualityChains(PCertificateChain chain
)
1061 for (i
= 0; i
< chain
->context
.cLowerQualityChainContext
; i
++)
1062 CertFreeCertificateChain(chain
->context
.rgpLowerQualityChainContext
[i
]);
1063 CryptMemFree(chain
->context
.rgpLowerQualityChainContext
);
1064 chain
->context
.cLowerQualityChainContext
= 0;
1065 chain
->context
.rgpLowerQualityChainContext
= NULL
;
1068 static void CRYPT_FreeChainContext(PCertificateChain chain
)
1072 CRYPT_FreeLowerQualityChains(chain
);
1073 for (i
= 0; i
< chain
->context
.cChain
; i
++)
1074 CRYPT_FreeSimpleChain(chain
->context
.rgpChain
[i
]);
1075 CryptMemFree(chain
->context
.rgpChain
);
1076 CertCloseStore(chain
->world
, 0);
1077 CryptMemFree(chain
);
1080 /* Makes and returns a copy of chain, up to and including element iElement of
1081 * simple chain iChain.
1083 static PCertificateChain
CRYPT_CopyChainToElement(PCertificateChain chain
,
1084 DWORD iChain
, DWORD iElement
)
1086 PCertificateChain copy
= CryptMemAlloc(sizeof(CertificateChain
));
1091 copy
->world
= CertDuplicateStore(chain
->world
);
1092 copy
->context
.cbSize
= sizeof(CERT_CHAIN_CONTEXT
);
1093 /* Leave the trust status of the copied chain unset, it'll get
1094 * rechecked after the new chain is done.
1096 memset(©
->context
.TrustStatus
, 0, sizeof(CERT_TRUST_STATUS
));
1097 copy
->context
.cLowerQualityChainContext
= 0;
1098 copy
->context
.rgpLowerQualityChainContext
= NULL
;
1099 copy
->context
.fHasRevocationFreshnessTime
= FALSE
;
1100 copy
->context
.dwRevocationFreshnessTime
= 0;
1101 copy
->context
.rgpChain
= CryptMemAlloc(
1102 (iChain
+ 1) * sizeof(PCERT_SIMPLE_CHAIN
));
1103 if (copy
->context
.rgpChain
)
1108 memset(copy
->context
.rgpChain
, 0,
1109 (iChain
+ 1) * sizeof(PCERT_SIMPLE_CHAIN
));
1112 for (i
= 0; ret
&& iChain
&& i
< iChain
- 1; i
++)
1114 copy
->context
.rgpChain
[i
] =
1115 CRYPT_CopySimpleChainToElement(chain
->context
.rgpChain
[i
],
1116 chain
->context
.rgpChain
[i
]->cElement
- 1);
1117 if (!copy
->context
.rgpChain
[i
])
1125 copy
->context
.rgpChain
[i
] =
1126 CRYPT_CopySimpleChainToElement(chain
->context
.rgpChain
[i
],
1128 if (!copy
->context
.rgpChain
[i
])
1133 CRYPT_FreeChainContext(copy
);
1137 copy
->context
.cChain
= iChain
+ 1;
1148 static PCertificateChain
CRYPT_BuildAlternateContextFromChain(
1149 HCERTCHAINENGINE hChainEngine
, LPFILETIME pTime
, HCERTSTORE hAdditionalStore
,
1150 PCertificateChain chain
)
1152 PCertificateChainEngine engine
= (PCertificateChainEngine
)hChainEngine
;
1153 PCertificateChain alternate
;
1155 TRACE("(%p, %p, %p, %p)\n", hChainEngine
, pTime
, hAdditionalStore
, chain
);
1157 /* Always start with the last "lower quality" chain to ensure a consistent
1158 * order of alternate creation:
1160 if (chain
->context
.cLowerQualityChainContext
)
1161 chain
= (PCertificateChain
)chain
->context
.rgpLowerQualityChainContext
[
1162 chain
->context
.cLowerQualityChainContext
- 1];
1163 /* A chain with only one element can't have any alternates */
1164 if (chain
->context
.cChain
<= 1 && chain
->context
.rgpChain
[0]->cElement
<= 1)
1168 DWORD i
, j
, infoStatus
;
1169 PCCERT_CONTEXT alternateIssuer
= NULL
;
1172 for (i
= 0; !alternateIssuer
&& i
< chain
->context
.cChain
; i
++)
1173 for (j
= 0; !alternateIssuer
&&
1174 j
< chain
->context
.rgpChain
[i
]->cElement
- 1; j
++)
1176 PCCERT_CONTEXT subject
=
1177 chain
->context
.rgpChain
[i
]->rgpElement
[j
]->pCertContext
;
1178 PCCERT_CONTEXT prevIssuer
= CertDuplicateCertificateContext(
1179 chain
->context
.rgpChain
[i
]->rgpElement
[j
+ 1]->pCertContext
);
1181 alternateIssuer
= CRYPT_GetIssuer(prevIssuer
->hCertStore
,
1182 subject
, prevIssuer
, &infoStatus
);
1184 if (alternateIssuer
)
1188 alternate
= CRYPT_CopyChainToElement(chain
, i
, j
);
1191 BOOL ret
= CRYPT_AddCertToSimpleChain(engine
,
1192 alternate
->context
.rgpChain
[i
], alternateIssuer
, infoStatus
);
1194 /* CRYPT_AddCertToSimpleChain add-ref's the issuer, so free it
1195 * to close the enumeration that found it
1197 CertFreeCertificateContext(alternateIssuer
);
1200 ret
= CRYPT_BuildSimpleChain(engine
, alternate
->world
,
1201 alternate
->context
.rgpChain
[i
]);
1203 CRYPT_CheckSimpleChain(engine
,
1204 alternate
->context
.rgpChain
[i
], pTime
);
1205 CRYPT_CombineTrustStatus(&alternate
->context
.TrustStatus
,
1206 &alternate
->context
.rgpChain
[i
]->TrustStatus
);
1210 CRYPT_FreeChainContext(alternate
);
1216 TRACE("%p\n", alternate
);
1220 #define CHAIN_QUALITY_SIGNATURE_VALID 8
1221 #define CHAIN_QUALITY_TIME_VALID 4
1222 #define CHAIN_QUALITY_COMPLETE_CHAIN 2
1223 #define CHAIN_QUALITY_TRUSTED_ROOT 1
1225 #define CHAIN_QUALITY_HIGHEST \
1226 CHAIN_QUALITY_SIGNATURE_VALID | CHAIN_QUALITY_TIME_VALID | \
1227 CHAIN_QUALITY_COMPLETE_CHAIN | CHAIN_QUALITY_TRUSTED_ROOT
1229 #define IS_TRUST_ERROR_SET(TrustStatus, bits) \
1230 (TrustStatus)->dwErrorStatus & (bits)
1232 static DWORD
CRYPT_ChainQuality(PCertificateChain chain
)
1234 DWORD quality
= CHAIN_QUALITY_HIGHEST
;
1236 if (IS_TRUST_ERROR_SET(&chain
->context
.TrustStatus
,
1237 CERT_TRUST_IS_UNTRUSTED_ROOT
))
1238 quality
&= ~CHAIN_QUALITY_TRUSTED_ROOT
;
1239 if (IS_TRUST_ERROR_SET(&chain
->context
.TrustStatus
,
1240 CERT_TRUST_IS_PARTIAL_CHAIN
))
1241 if (chain
->context
.TrustStatus
.dwErrorStatus
& CERT_TRUST_IS_PARTIAL_CHAIN
)
1242 quality
&= ~CHAIN_QUALITY_COMPLETE_CHAIN
;
1243 if (IS_TRUST_ERROR_SET(&chain
->context
.TrustStatus
,
1244 CERT_TRUST_IS_NOT_TIME_VALID
| CERT_TRUST_IS_NOT_TIME_NESTED
))
1245 quality
&= ~CHAIN_QUALITY_TIME_VALID
;
1246 if (IS_TRUST_ERROR_SET(&chain
->context
.TrustStatus
,
1247 CERT_TRUST_IS_NOT_SIGNATURE_VALID
))
1248 quality
&= ~CHAIN_QUALITY_SIGNATURE_VALID
;
1252 /* Chooses the highest quality chain among chain and its "lower quality"
1253 * alternate chains. Returns the highest quality chain, with all other
1254 * chains as lower quality chains of it.
1256 static PCertificateChain
CRYPT_ChooseHighestQualityChain(
1257 PCertificateChain chain
)
1261 /* There are always only two chains being considered: chain, and an
1262 * alternate at chain->rgpLowerQualityChainContext[i]. If the alternate
1263 * has a higher quality than chain, the alternate gets assigned the lower
1264 * quality contexts, with chain taking the alternate's place among the
1265 * lower quality contexts.
1267 for (i
= 0; i
< chain
->context
.cLowerQualityChainContext
; i
++)
1269 PCertificateChain alternate
=
1270 (PCertificateChain
)chain
->context
.rgpLowerQualityChainContext
[i
];
1272 if (CRYPT_ChainQuality(alternate
) > CRYPT_ChainQuality(chain
))
1274 alternate
->context
.cLowerQualityChainContext
=
1275 chain
->context
.cLowerQualityChainContext
;
1276 alternate
->context
.rgpLowerQualityChainContext
=
1277 chain
->context
.rgpLowerQualityChainContext
;
1278 alternate
->context
.rgpLowerQualityChainContext
[i
] =
1279 (PCCERT_CHAIN_CONTEXT
)chain
;
1280 chain
->context
.cLowerQualityChainContext
= 0;
1281 chain
->context
.rgpLowerQualityChainContext
= NULL
;
1288 static BOOL
CRYPT_AddAlternateChainToChain(PCertificateChain chain
,
1289 PCertificateChain alternate
)
1293 if (chain
->context
.cLowerQualityChainContext
)
1294 chain
->context
.rgpLowerQualityChainContext
=
1295 CryptMemRealloc(chain
->context
.rgpLowerQualityChainContext
,
1296 (chain
->context
.cLowerQualityChainContext
+ 1) *
1297 sizeof(PCCERT_CHAIN_CONTEXT
));
1299 chain
->context
.rgpLowerQualityChainContext
=
1300 CryptMemAlloc(sizeof(PCCERT_CHAIN_CONTEXT
));
1301 if (chain
->context
.rgpLowerQualityChainContext
)
1303 chain
->context
.rgpLowerQualityChainContext
[
1304 chain
->context
.cLowerQualityChainContext
++] =
1305 (PCCERT_CHAIN_CONTEXT
)alternate
;
1313 static PCERT_CHAIN_ELEMENT
CRYPT_FindIthElementInChain(
1314 PCERT_CHAIN_CONTEXT chain
, DWORD i
)
1317 PCERT_CHAIN_ELEMENT element
= NULL
;
1319 for (j
= 0, iElement
= 0; !element
&& j
< chain
->cChain
; j
++)
1321 if (iElement
+ chain
->rgpChain
[j
]->cElement
< i
)
1322 iElement
+= chain
->rgpChain
[j
]->cElement
;
1324 element
= chain
->rgpChain
[j
]->rgpElement
[i
- iElement
];
1329 typedef struct _CERT_CHAIN_PARA_NO_EXTRA_FIELDS
{
1331 CERT_USAGE_MATCH RequestedUsage
;
1332 } CERT_CHAIN_PARA_NO_EXTRA_FIELDS
, *PCERT_CHAIN_PARA_NO_EXTRA_FIELDS
;
1334 static void CRYPT_VerifyChainRevocation(PCERT_CHAIN_CONTEXT chain
,
1335 LPFILETIME pTime
, PCERT_CHAIN_PARA pChainPara
, DWORD chainFlags
)
1339 if (chainFlags
& CERT_CHAIN_REVOCATION_CHECK_END_CERT
)
1341 else if ((chainFlags
& CERT_CHAIN_REVOCATION_CHECK_CHAIN
) ||
1342 (chainFlags
& CERT_CHAIN_REVOCATION_CHECK_CHAIN_EXCLUDE_ROOT
))
1346 for (i
= 0, cContext
= 0; i
< chain
->cChain
; i
++)
1348 if (i
< chain
->cChain
- 1 ||
1349 chainFlags
& CERT_CHAIN_REVOCATION_CHECK_CHAIN
)
1350 cContext
+= chain
->rgpChain
[i
]->cElement
;
1352 cContext
+= chain
->rgpChain
[i
]->cElement
- 1;
1359 PCCERT_CONTEXT
*contexts
=
1360 CryptMemAlloc(cContext
* sizeof(PCCERT_CONTEXT
*));
1364 DWORD i
, j
, iContext
, revocationFlags
;
1365 CERT_REVOCATION_PARA revocationPara
= { sizeof(revocationPara
), 0 };
1366 CERT_REVOCATION_STATUS revocationStatus
=
1367 { sizeof(revocationStatus
), 0 };
1370 for (i
= 0, iContext
= 0; iContext
< cContext
&& i
< chain
->cChain
;
1373 for (j
= 0; iContext
< cContext
&&
1374 j
< chain
->rgpChain
[i
]->cElement
; j
++)
1375 contexts
[iContext
++] =
1376 chain
->rgpChain
[i
]->rgpElement
[j
]->pCertContext
;
1378 revocationFlags
= CERT_VERIFY_REV_CHAIN_FLAG
;
1379 if (chainFlags
& CERT_CHAIN_REVOCATION_CHECK_CACHE_ONLY
)
1380 revocationFlags
|= CERT_VERIFY_CACHE_ONLY_BASED_REVOCATION
;
1381 if (chainFlags
& CERT_CHAIN_REVOCATION_ACCUMULATIVE_TIMEOUT
)
1382 revocationFlags
|= CERT_VERIFY_REV_ACCUMULATIVE_TIMEOUT_FLAG
;
1383 revocationPara
.pftTimeToUse
= pTime
;
1384 if (pChainPara
->cbSize
== sizeof(CERT_CHAIN_PARA
))
1386 revocationPara
.dwUrlRetrievalTimeout
=
1387 pChainPara
->dwUrlRetrievalTimeout
;
1388 revocationPara
.fCheckFreshnessTime
=
1389 pChainPara
->fCheckRevocationFreshnessTime
;
1390 revocationPara
.dwFreshnessTime
=
1391 pChainPara
->dwRevocationFreshnessTime
;
1393 ret
= CertVerifyRevocation(X509_ASN_ENCODING
,
1394 CERT_CONTEXT_REVOCATION_TYPE
, cContext
, (void **)contexts
,
1395 revocationFlags
, &revocationPara
, &revocationStatus
);
1398 PCERT_CHAIN_ELEMENT element
=
1399 CRYPT_FindIthElementInChain(chain
, revocationStatus
.dwIndex
);
1402 switch (revocationStatus
.dwError
)
1404 case CRYPT_E_NO_REVOCATION_CHECK
:
1405 case CRYPT_E_NO_REVOCATION_DLL
:
1406 case CRYPT_E_NOT_IN_REVOCATION_DATABASE
:
1407 error
= CERT_TRUST_REVOCATION_STATUS_UNKNOWN
;
1409 case CRYPT_E_REVOCATION_OFFLINE
:
1410 error
= CERT_TRUST_IS_OFFLINE_REVOCATION
;
1412 case CRYPT_E_REVOKED
:
1413 error
= CERT_TRUST_IS_REVOKED
;
1416 WARN("unmapped error %08x\n", revocationStatus
.dwError
);
1421 /* FIXME: set element's pRevocationInfo member */
1422 element
->TrustStatus
.dwErrorStatus
|= error
;
1424 chain
->TrustStatus
.dwErrorStatus
|= error
;
1426 CryptMemFree(contexts
);
1431 BOOL WINAPI
CertGetCertificateChain(HCERTCHAINENGINE hChainEngine
,
1432 PCCERT_CONTEXT pCertContext
, LPFILETIME pTime
, HCERTSTORE hAdditionalStore
,
1433 PCERT_CHAIN_PARA pChainPara
, DWORD dwFlags
, LPVOID pvReserved
,
1434 PCCERT_CHAIN_CONTEXT
* ppChainContext
)
1437 PCertificateChain chain
= NULL
;
1439 TRACE("(%p, %p, %p, %p, %p, %08x, %p, %p)\n", hChainEngine
, pCertContext
,
1440 pTime
, hAdditionalStore
, pChainPara
, dwFlags
, pvReserved
, ppChainContext
);
1443 *ppChainContext
= NULL
;
1446 SetLastError(E_INVALIDARG
);
1449 if (!pCertContext
->pCertInfo
->SignatureAlgorithm
.pszObjId
)
1451 SetLastError(ERROR_INVALID_DATA
);
1454 if (pChainPara
->cbSize
!= sizeof(CERT_CHAIN_PARA_NO_EXTRA_FIELDS
) &&
1455 pChainPara
->cbSize
!= sizeof(CERT_CHAIN_PARA
))
1457 SetLastError(E_INVALIDARG
);
1461 hChainEngine
= CRYPT_GetDefaultChainEngine();
1462 /* FIXME: what about HCCE_LOCAL_MACHINE? */
1463 ret
= CRYPT_BuildCandidateChainFromCert(hChainEngine
, pCertContext
, pTime
,
1464 hAdditionalStore
, &chain
);
1467 PCertificateChain alternate
= NULL
;
1468 PCERT_CHAIN_CONTEXT pChain
;
1471 alternate
= CRYPT_BuildAlternateContextFromChain(hChainEngine
,
1472 pTime
, hAdditionalStore
, chain
);
1474 /* Alternate contexts are added as "lower quality" contexts of
1475 * chain, to avoid loops in alternate chain creation.
1476 * The highest-quality chain is chosen at the end.
1479 ret
= CRYPT_AddAlternateChainToChain(chain
, alternate
);
1480 } while (ret
&& alternate
);
1481 chain
= CRYPT_ChooseHighestQualityChain(chain
);
1482 if (!(dwFlags
& CERT_CHAIN_RETURN_LOWER_QUALITY_CONTEXTS
))
1483 CRYPT_FreeLowerQualityChains(chain
);
1484 pChain
= (PCERT_CHAIN_CONTEXT
)chain
;
1485 CRYPT_VerifyChainRevocation(pChain
, pTime
, pChainPara
, dwFlags
);
1487 *ppChainContext
= pChain
;
1489 CertFreeCertificateChain(pChain
);
1491 TRACE("returning %d\n", ret
);
1495 PCCERT_CHAIN_CONTEXT WINAPI
CertDuplicateCertificateChain(
1496 PCCERT_CHAIN_CONTEXT pChainContext
)
1498 PCertificateChain chain
= (PCertificateChain
)pChainContext
;
1500 TRACE("(%p)\n", pChainContext
);
1503 InterlockedIncrement(&chain
->ref
);
1504 return pChainContext
;
1507 VOID WINAPI
CertFreeCertificateChain(PCCERT_CHAIN_CONTEXT pChainContext
)
1509 PCertificateChain chain
= (PCertificateChain
)pChainContext
;
1511 TRACE("(%p)\n", pChainContext
);
1515 if (InterlockedDecrement(&chain
->ref
) == 0)
1516 CRYPT_FreeChainContext(chain
);
1520 static void find_element_with_error(PCCERT_CHAIN_CONTEXT chain
, DWORD error
,
1521 LONG
*iChain
, LONG
*iElement
)
1525 for (i
= 0; i
< chain
->cChain
; i
++)
1526 for (j
= 0; j
< chain
->rgpChain
[i
]->cElement
; j
++)
1527 if (chain
->rgpChain
[i
]->rgpElement
[j
]->TrustStatus
.dwErrorStatus
&
1536 static BOOL WINAPI
verify_base_policy(LPCSTR szPolicyOID
,
1537 PCCERT_CHAIN_CONTEXT pChainContext
, PCERT_CHAIN_POLICY_PARA pPolicyPara
,
1538 PCERT_CHAIN_POLICY_STATUS pPolicyStatus
)
1540 pPolicyStatus
->lChainIndex
= pPolicyStatus
->lElementIndex
= -1;
1541 if (pChainContext
->TrustStatus
.dwErrorStatus
&
1542 CERT_TRUST_IS_NOT_SIGNATURE_VALID
)
1544 pPolicyStatus
->dwError
= TRUST_E_CERT_SIGNATURE
;
1545 find_element_with_error(pChainContext
,
1546 CERT_TRUST_IS_NOT_SIGNATURE_VALID
, &pPolicyStatus
->lChainIndex
,
1547 &pPolicyStatus
->lElementIndex
);
1549 else if (pChainContext
->TrustStatus
.dwErrorStatus
&
1550 CERT_TRUST_IS_UNTRUSTED_ROOT
)
1552 pPolicyStatus
->dwError
= CERT_E_UNTRUSTEDROOT
;
1553 find_element_with_error(pChainContext
,
1554 CERT_TRUST_IS_UNTRUSTED_ROOT
, &pPolicyStatus
->lChainIndex
,
1555 &pPolicyStatus
->lElementIndex
);
1557 else if (pChainContext
->TrustStatus
.dwErrorStatus
& CERT_TRUST_IS_CYCLIC
)
1559 pPolicyStatus
->dwError
= CERT_E_CHAINING
;
1560 find_element_with_error(pChainContext
, CERT_TRUST_IS_CYCLIC
,
1561 &pPolicyStatus
->lChainIndex
, &pPolicyStatus
->lElementIndex
);
1562 /* For a cyclic chain, which element is a cycle isn't meaningful */
1563 pPolicyStatus
->lElementIndex
= -1;
1566 pPolicyStatus
->dwError
= NO_ERROR
;
1570 static BYTE msTestPubKey1
[] = {
1571 0x30,0x47,0x02,0x40,0x81,0x55,0x22,0xb9,0x8a,0xa4,0x6f,0xed,0xd6,0xe7,0xd9,
1572 0x66,0x0f,0x55,0xbc,0xd7,0xcd,0xd5,0xbc,0x4e,0x40,0x02,0x21,0xa2,0xb1,0xf7,
1573 0x87,0x30,0x85,0x5e,0xd2,0xf2,0x44,0xb9,0xdc,0x9b,0x75,0xb6,0xfb,0x46,0x5f,
1574 0x42,0xb6,0x9d,0x23,0x36,0x0b,0xde,0x54,0x0f,0xcd,0xbd,0x1f,0x99,0x2a,0x10,
1575 0x58,0x11,0xcb,0x40,0xcb,0xb5,0xa7,0x41,0x02,0x03,0x01,0x00,0x01 };
1576 static BYTE msTestPubKey2
[] = {
1577 0x30,0x47,0x02,0x40,0x9c,0x50,0x05,0x1d,0xe2,0x0e,0x4c,0x53,0xd8,0xd9,0xb5,
1578 0xe5,0xfd,0xe9,0xe3,0xad,0x83,0x4b,0x80,0x08,0xd9,0xdc,0xe8,0xe8,0x35,0xf8,
1579 0x11,0xf1,0xe9,0x9b,0x03,0x7a,0x65,0x64,0x76,0x35,0xce,0x38,0x2c,0xf2,0xb6,
1580 0x71,0x9e,0x06,0xd9,0xbf,0xbb,0x31,0x69,0xa3,0xf6,0x30,0xa0,0x78,0x7b,0x18,
1581 0xdd,0x50,0x4d,0x79,0x1e,0xeb,0x61,0xc1,0x02,0x03,0x01,0x00,0x01 };
1583 static BOOL WINAPI
verify_authenticode_policy(LPCSTR szPolicyOID
,
1584 PCCERT_CHAIN_CONTEXT pChainContext
, PCERT_CHAIN_POLICY_PARA pPolicyPara
,
1585 PCERT_CHAIN_POLICY_STATUS pPolicyStatus
)
1587 BOOL ret
= verify_base_policy(szPolicyOID
, pChainContext
, pPolicyPara
,
1590 if (ret
&& pPolicyStatus
->dwError
== CERT_E_UNTRUSTEDROOT
)
1592 CERT_PUBLIC_KEY_INFO msPubKey
= { { 0 } };
1593 BOOL isMSTestRoot
= FALSE
;
1594 PCCERT_CONTEXT failingCert
=
1595 pChainContext
->rgpChain
[pPolicyStatus
->lChainIndex
]->
1596 rgpElement
[pPolicyStatus
->lElementIndex
]->pCertContext
;
1598 CRYPT_DATA_BLOB keyBlobs
[] = {
1599 { sizeof(msTestPubKey1
), msTestPubKey1
},
1600 { sizeof(msTestPubKey2
), msTestPubKey2
},
1603 /* Check whether the root is an MS test root */
1604 for (i
= 0; !isMSTestRoot
&& i
< sizeof(keyBlobs
) / sizeof(keyBlobs
[0]);
1607 msPubKey
.PublicKey
.cbData
= keyBlobs
[i
].cbData
;
1608 msPubKey
.PublicKey
.pbData
= keyBlobs
[i
].pbData
;
1609 if (CertComparePublicKeyInfo(
1610 X509_ASN_ENCODING
| PKCS_7_ASN_ENCODING
,
1611 &failingCert
->pCertInfo
->SubjectPublicKeyInfo
, &msPubKey
))
1612 isMSTestRoot
= TRUE
;
1615 pPolicyStatus
->dwError
= CERT_E_UNTRUSTEDTESTROOT
;
1620 static BOOL WINAPI
verify_basic_constraints_policy(LPCSTR szPolicyOID
,
1621 PCCERT_CHAIN_CONTEXT pChainContext
, PCERT_CHAIN_POLICY_PARA pPolicyPara
,
1622 PCERT_CHAIN_POLICY_STATUS pPolicyStatus
)
1624 pPolicyStatus
->lChainIndex
= pPolicyStatus
->lElementIndex
= -1;
1625 if (pChainContext
->TrustStatus
.dwErrorStatus
&
1626 CERT_TRUST_INVALID_BASIC_CONSTRAINTS
)
1628 pPolicyStatus
->dwError
= TRUST_E_BASIC_CONSTRAINTS
;
1629 find_element_with_error(pChainContext
,
1630 CERT_TRUST_INVALID_BASIC_CONSTRAINTS
, &pPolicyStatus
->lChainIndex
,
1631 &pPolicyStatus
->lElementIndex
);
1634 pPolicyStatus
->dwError
= NO_ERROR
;
1638 static BYTE msPubKey1
[] = {
1639 0x30,0x82,0x01,0x0a,0x02,0x82,0x01,0x01,0x00,0xdf,0x08,0xba,0xe3,0x3f,0x6e,
1640 0x64,0x9b,0xf5,0x89,0xaf,0x28,0x96,0x4a,0x07,0x8f,0x1b,0x2e,0x8b,0x3e,0x1d,
1641 0xfc,0xb8,0x80,0x69,0xa3,0xa1,0xce,0xdb,0xdf,0xb0,0x8e,0x6c,0x89,0x76,0x29,
1642 0x4f,0xca,0x60,0x35,0x39,0xad,0x72,0x32,0xe0,0x0b,0xae,0x29,0x3d,0x4c,0x16,
1643 0xd9,0x4b,0x3c,0x9d,0xda,0xc5,0xd3,0xd1,0x09,0xc9,0x2c,0x6f,0xa6,0xc2,0x60,
1644 0x53,0x45,0xdd,0x4b,0xd1,0x55,0xcd,0x03,0x1c,0xd2,0x59,0x56,0x24,0xf3,0xe5,
1645 0x78,0xd8,0x07,0xcc,0xd8,0xb3,0x1f,0x90,0x3f,0xc0,0x1a,0x71,0x50,0x1d,0x2d,
1646 0xa7,0x12,0x08,0x6d,0x7c,0xb0,0x86,0x6c,0xc7,0xba,0x85,0x32,0x07,0xe1,0x61,
1647 0x6f,0xaf,0x03,0xc5,0x6d,0xe5,0xd6,0xa1,0x8f,0x36,0xf6,0xc1,0x0b,0xd1,0x3e,
1648 0x69,0x97,0x48,0x72,0xc9,0x7f,0xa4,0xc8,0xc2,0x4a,0x4c,0x7e,0xa1,0xd1,0x94,
1649 0xa6,0xd7,0xdc,0xeb,0x05,0x46,0x2e,0xb8,0x18,0xb4,0x57,0x1d,0x86,0x49,0xdb,
1650 0x69,0x4a,0x2c,0x21,0xf5,0x5e,0x0f,0x54,0x2d,0x5a,0x43,0xa9,0x7a,0x7e,0x6a,
1651 0x8e,0x50,0x4d,0x25,0x57,0xa1,0xbf,0x1b,0x15,0x05,0x43,0x7b,0x2c,0x05,0x8d,
1652 0xbd,0x3d,0x03,0x8c,0x93,0x22,0x7d,0x63,0xea,0x0a,0x57,0x05,0x06,0x0a,0xdb,
1653 0x61,0x98,0x65,0x2d,0x47,0x49,0xa8,0xe7,0xe6,0x56,0x75,0x5c,0xb8,0x64,0x08,
1654 0x63,0xa9,0x30,0x40,0x66,0xb2,0xf9,0xb6,0xe3,0x34,0xe8,0x67,0x30,0xe1,0x43,
1655 0x0b,0x87,0xff,0xc9,0xbe,0x72,0x10,0x5e,0x23,0xf0,0x9b,0xa7,0x48,0x65,0xbf,
1656 0x09,0x88,0x7b,0xcd,0x72,0xbc,0x2e,0x79,0x9b,0x7b,0x02,0x03,0x01,0x00,0x01 };
1657 static BYTE msPubKey2
[] = {
1658 0x30,0x82,0x01,0x0a,0x02,0x82,0x01,0x01,0x00,0xa9,0x02,0xbd,0xc1,0x70,0xe6,
1659 0x3b,0xf2,0x4e,0x1b,0x28,0x9f,0x97,0x78,0x5e,0x30,0xea,0xa2,0xa9,0x8d,0x25,
1660 0x5f,0xf8,0xfe,0x95,0x4c,0xa3,0xb7,0xfe,0x9d,0xa2,0x20,0x3e,0x7c,0x51,0xa2,
1661 0x9b,0xa2,0x8f,0x60,0x32,0x6b,0xd1,0x42,0x64,0x79,0xee,0xac,0x76,0xc9,0x54,
1662 0xda,0xf2,0xeb,0x9c,0x86,0x1c,0x8f,0x9f,0x84,0x66,0xb3,0xc5,0x6b,0x7a,0x62,
1663 0x23,0xd6,0x1d,0x3c,0xde,0x0f,0x01,0x92,0xe8,0x96,0xc4,0xbf,0x2d,0x66,0x9a,
1664 0x9a,0x68,0x26,0x99,0xd0,0x3a,0x2c,0xbf,0x0c,0xb5,0x58,0x26,0xc1,0x46,0xe7,
1665 0x0a,0x3e,0x38,0x96,0x2c,0xa9,0x28,0x39,0xa8,0xec,0x49,0x83,0x42,0xe3,0x84,
1666 0x0f,0xbb,0x9a,0x6c,0x55,0x61,0xac,0x82,0x7c,0xa1,0x60,0x2d,0x77,0x4c,0xe9,
1667 0x99,0xb4,0x64,0x3b,0x9a,0x50,0x1c,0x31,0x08,0x24,0x14,0x9f,0xa9,0xe7,0x91,
1668 0x2b,0x18,0xe6,0x3d,0x98,0x63,0x14,0x60,0x58,0x05,0x65,0x9f,0x1d,0x37,0x52,
1669 0x87,0xf7,0xa7,0xef,0x94,0x02,0xc6,0x1b,0xd3,0xbf,0x55,0x45,0xb3,0x89,0x80,
1670 0xbf,0x3a,0xec,0x54,0x94,0x4e,0xae,0xfd,0xa7,0x7a,0x6d,0x74,0x4e,0xaf,0x18,
1671 0xcc,0x96,0x09,0x28,0x21,0x00,0x57,0x90,0x60,0x69,0x37,0xbb,0x4b,0x12,0x07,
1672 0x3c,0x56,0xff,0x5b,0xfb,0xa4,0x66,0x0a,0x08,0xa6,0xd2,0x81,0x56,0x57,0xef,
1673 0xb6,0x3b,0x5e,0x16,0x81,0x77,0x04,0xda,0xf6,0xbe,0xae,0x80,0x95,0xfe,0xb0,
1674 0xcd,0x7f,0xd6,0xa7,0x1a,0x72,0x5c,0x3c,0xca,0xbc,0xf0,0x08,0xa3,0x22,0x30,
1675 0xb3,0x06,0x85,0xc9,0xb3,0x20,0x77,0x13,0x85,0xdf,0x02,0x03,0x01,0x00,0x01 };
1676 static BYTE msPubKey3
[] = {
1677 0x30,0x82,0x02,0x0a,0x02,0x82,0x02,0x01,0x00,0xf3,0x5d,0xfa,0x80,0x67,0xd4,
1678 0x5a,0xa7,0xa9,0x0c,0x2c,0x90,0x20,0xd0,0x35,0x08,0x3c,0x75,0x84,0xcd,0xb7,
1679 0x07,0x89,0x9c,0x89,0xda,0xde,0xce,0xc3,0x60,0xfa,0x91,0x68,0x5a,0x9e,0x94,
1680 0x71,0x29,0x18,0x76,0x7c,0xc2,0xe0,0xc8,0x25,0x76,0x94,0x0e,0x58,0xfa,0x04,
1681 0x34,0x36,0xe6,0xdf,0xaf,0xf7,0x80,0xba,0xe9,0x58,0x0b,0x2b,0x93,0xe5,0x9d,
1682 0x05,0xe3,0x77,0x22,0x91,0xf7,0x34,0x64,0x3c,0x22,0x91,0x1d,0x5e,0xe1,0x09,
1683 0x90,0xbc,0x14,0xfe,0xfc,0x75,0x58,0x19,0xe1,0x79,0xb7,0x07,0x92,0xa3,0xae,
1684 0x88,0x59,0x08,0xd8,0x9f,0x07,0xca,0x03,0x58,0xfc,0x68,0x29,0x6d,0x32,0xd7,
1685 0xd2,0xa8,0xcb,0x4b,0xfc,0xe1,0x0b,0x48,0x32,0x4f,0xe6,0xeb,0xb8,0xad,0x4f,
1686 0xe4,0x5c,0x6f,0x13,0x94,0x99,0xdb,0x95,0xd5,0x75,0xdb,0xa8,0x1a,0xb7,0x94,
1687 0x91,0xb4,0x77,0x5b,0xf5,0x48,0x0c,0x8f,0x6a,0x79,0x7d,0x14,0x70,0x04,0x7d,
1688 0x6d,0xaf,0x90,0xf5,0xda,0x70,0xd8,0x47,0xb7,0xbf,0x9b,0x2f,0x6c,0xe7,0x05,
1689 0xb7,0xe1,0x11,0x60,0xac,0x79,0x91,0x14,0x7c,0xc5,0xd6,0xa6,0xe4,0xe1,0x7e,
1690 0xd5,0xc3,0x7e,0xe5,0x92,0xd2,0x3c,0x00,0xb5,0x36,0x82,0xde,0x79,0xe1,0x6d,
1691 0xf3,0xb5,0x6e,0xf8,0x9f,0x33,0xc9,0xcb,0x52,0x7d,0x73,0x98,0x36,0xdb,0x8b,
1692 0xa1,0x6b,0xa2,0x95,0x97,0x9b,0xa3,0xde,0xc2,0x4d,0x26,0xff,0x06,0x96,0x67,
1693 0x25,0x06,0xc8,0xe7,0xac,0xe4,0xee,0x12,0x33,0x95,0x31,0x99,0xc8,0x35,0x08,
1694 0x4e,0x34,0xca,0x79,0x53,0xd5,0xb5,0xbe,0x63,0x32,0x59,0x40,0x36,0xc0,0xa5,
1695 0x4e,0x04,0x4d,0x3d,0xdb,0x5b,0x07,0x33,0xe4,0x58,0xbf,0xef,0x3f,0x53,0x64,
1696 0xd8,0x42,0x59,0x35,0x57,0xfd,0x0f,0x45,0x7c,0x24,0x04,0x4d,0x9e,0xd6,0x38,
1697 0x74,0x11,0x97,0x22,0x90,0xce,0x68,0x44,0x74,0x92,0x6f,0xd5,0x4b,0x6f,0xb0,
1698 0x86,0xe3,0xc7,0x36,0x42,0xa0,0xd0,0xfc,0xc1,0xc0,0x5a,0xf9,0xa3,0x61,0xb9,
1699 0x30,0x47,0x71,0x96,0x0a,0x16,0xb0,0x91,0xc0,0x42,0x95,0xef,0x10,0x7f,0x28,
1700 0x6a,0xe3,0x2a,0x1f,0xb1,0xe4,0xcd,0x03,0x3f,0x77,0x71,0x04,0xc7,0x20,0xfc,
1701 0x49,0x0f,0x1d,0x45,0x88,0xa4,0xd7,0xcb,0x7e,0x88,0xad,0x8e,0x2d,0xec,0x45,
1702 0xdb,0xc4,0x51,0x04,0xc9,0x2a,0xfc,0xec,0x86,0x9e,0x9a,0x11,0x97,0x5b,0xde,
1703 0xce,0x53,0x88,0xe6,0xe2,0xb7,0xfd,0xac,0x95,0xc2,0x28,0x40,0xdb,0xef,0x04,
1704 0x90,0xdf,0x81,0x33,0x39,0xd9,0xb2,0x45,0xa5,0x23,0x87,0x06,0xa5,0x55,0x89,
1705 0x31,0xbb,0x06,0x2d,0x60,0x0e,0x41,0x18,0x7d,0x1f,0x2e,0xb5,0x97,0xcb,0x11,
1706 0xeb,0x15,0xd5,0x24,0xa5,0x94,0xef,0x15,0x14,0x89,0xfd,0x4b,0x73,0xfa,0x32,
1707 0x5b,0xfc,0xd1,0x33,0x00,0xf9,0x59,0x62,0x70,0x07,0x32,0xea,0x2e,0xab,0x40,
1708 0x2d,0x7b,0xca,0xdd,0x21,0x67,0x1b,0x30,0x99,0x8f,0x16,0xaa,0x23,0xa8,0x41,
1709 0xd1,0xb0,0x6e,0x11,0x9b,0x36,0xc4,0xde,0x40,0x74,0x9c,0xe1,0x58,0x65,0xc1,
1710 0x60,0x1e,0x7a,0x5b,0x38,0xc8,0x8f,0xbb,0x04,0x26,0x7c,0xd4,0x16,0x40,0xe5,
1711 0xb6,0x6b,0x6c,0xaa,0x86,0xfd,0x00,0xbf,0xce,0xc1,0x35,0x02,0x03,0x01,0x00,
1714 static BOOL WINAPI
verify_ms_root_policy(LPCSTR szPolicyOID
,
1715 PCCERT_CHAIN_CONTEXT pChainContext
, PCERT_CHAIN_POLICY_PARA pPolicyPara
,
1716 PCERT_CHAIN_POLICY_STATUS pPolicyStatus
)
1718 BOOL ret
= verify_base_policy(szPolicyOID
, pChainContext
, pPolicyPara
,
1721 if (ret
&& !pPolicyStatus
->dwError
)
1723 CERT_PUBLIC_KEY_INFO msPubKey
= { { 0 } };
1724 BOOL isMSRoot
= FALSE
;
1726 CRYPT_DATA_BLOB keyBlobs
[] = {
1727 { sizeof(msPubKey1
), msPubKey1
},
1728 { sizeof(msPubKey2
), msPubKey2
},
1729 { sizeof(msPubKey3
), msPubKey3
},
1731 PCERT_SIMPLE_CHAIN rootChain
=
1732 pChainContext
->rgpChain
[pChainContext
->cChain
-1 ];
1733 PCCERT_CONTEXT root
=
1734 rootChain
->rgpElement
[rootChain
->cElement
- 1]->pCertContext
;
1736 for (i
= 0; !isMSRoot
&& i
< sizeof(keyBlobs
) / sizeof(keyBlobs
[0]);
1739 msPubKey
.PublicKey
.cbData
= keyBlobs
[i
].cbData
;
1740 msPubKey
.PublicKey
.pbData
= keyBlobs
[i
].pbData
;
1741 if (CertComparePublicKeyInfo(
1742 X509_ASN_ENCODING
| PKCS_7_ASN_ENCODING
,
1743 &root
->pCertInfo
->SubjectPublicKeyInfo
, &msPubKey
))
1747 pPolicyStatus
->lChainIndex
= pPolicyStatus
->lElementIndex
= 0;
1752 typedef BOOL (WINAPI
*CertVerifyCertificateChainPolicyFunc
)(LPCSTR szPolicyOID
,
1753 PCCERT_CHAIN_CONTEXT pChainContext
, PCERT_CHAIN_POLICY_PARA pPolicyPara
,
1754 PCERT_CHAIN_POLICY_STATUS pPolicyStatus
);
1756 BOOL WINAPI
CertVerifyCertificateChainPolicy(LPCSTR szPolicyOID
,
1757 PCCERT_CHAIN_CONTEXT pChainContext
, PCERT_CHAIN_POLICY_PARA pPolicyPara
,
1758 PCERT_CHAIN_POLICY_STATUS pPolicyStatus
)
1760 static HCRYPTOIDFUNCSET set
= NULL
;
1762 CertVerifyCertificateChainPolicyFunc verifyPolicy
= NULL
;
1763 HCRYPTOIDFUNCADDR hFunc
= NULL
;
1765 TRACE("(%s, %p, %p, %p)\n", debugstr_a(szPolicyOID
), pChainContext
,
1766 pPolicyPara
, pPolicyStatus
);
1768 if (!HIWORD(szPolicyOID
))
1770 switch (LOWORD(szPolicyOID
))
1772 case LOWORD(CERT_CHAIN_POLICY_BASE
):
1773 verifyPolicy
= verify_base_policy
;
1775 case LOWORD(CERT_CHAIN_POLICY_AUTHENTICODE
):
1776 verifyPolicy
= verify_authenticode_policy
;
1778 case LOWORD(CERT_CHAIN_POLICY_BASIC_CONSTRAINTS
):
1779 verifyPolicy
= verify_basic_constraints_policy
;
1781 case LOWORD(CERT_CHAIN_POLICY_MICROSOFT_ROOT
):
1782 verifyPolicy
= verify_ms_root_policy
;
1785 FIXME("unimplemented for %d\n", LOWORD(szPolicyOID
));
1791 set
= CryptInitOIDFunctionSet(
1792 CRYPT_OID_VERIFY_CERTIFICATE_CHAIN_POLICY_FUNC
, 0);
1793 CryptGetOIDFunctionAddress(set
, X509_ASN_ENCODING
, szPolicyOID
, 0,
1794 (void **)&verifyPolicy
, &hFunc
);
1797 ret
= verifyPolicy(szPolicyOID
, pChainContext
, pPolicyPara
,
1800 CryptFreeOIDFunctionAddress(hFunc
, 0);