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
22 #define NONAMELESSUNION
26 #define CERT_CHAIN_PARA_HAS_EXTRA_FIELDS
27 #define CERT_REVOCATION_PARA_HAS_EXTRA_FIELDS
30 #include "wine/debug.h"
31 #include "crypt32_private.h"
33 WINE_DEFAULT_DEBUG_CHANNEL(crypt
);
34 WINE_DECLARE_DEBUG_CHANNEL(chain
);
36 #define DEFAULT_CYCLE_MODULUS 7
38 /* This represents a subset of a certificate chain engine: it doesn't include
39 * the "hOther" store described by MSDN, because I'm not sure how that's used.
40 * It also doesn't include the "hTrust" store, because I don't yet implement
41 * CTLs or complex certificate chains.
43 typedef struct _CertificateChainEngine
49 DWORD dwUrlRetrievalTimeout
;
50 DWORD MaximumCachedCertificates
;
51 DWORD CycleDetectionModulus
;
52 } CertificateChainEngine
;
54 static inline void CRYPT_AddStoresToCollection(HCERTSTORE collection
,
55 DWORD cStores
, HCERTSTORE
*stores
)
59 for (i
= 0; i
< cStores
; i
++)
60 CertAddStoreToCollection(collection
, stores
[i
], 0, 0);
63 static inline void CRYPT_CloseStores(DWORD cStores
, HCERTSTORE
*stores
)
67 for (i
= 0; i
< cStores
; i
++)
68 CertCloseStore(stores
[i
], 0);
71 /* Finds cert in store by comparing the cert's hashes. */
72 static PCCERT_CONTEXT
CRYPT_FindCertInStore(HCERTSTORE store
,
75 PCCERT_CONTEXT matching
= NULL
;
77 DWORD size
= sizeof(hash
);
79 if (CertGetCertificateContextProperty(cert
, CERT_HASH_PROP_ID
, hash
, &size
))
81 CRYPT_HASH_BLOB blob
= { sizeof(hash
), hash
};
83 matching
= CertFindCertificateInStore(store
, cert
->dwCertEncodingType
,
84 0, CERT_FIND_SHA1_HASH
, &blob
, NULL
);
89 static BOOL
CRYPT_CheckRestrictedRoot(HCERTSTORE store
)
95 HCERTSTORE rootStore
= CertOpenSystemStoreW(0, L
"Root");
96 PCCERT_CONTEXT cert
= NULL
, check
;
99 cert
= CertEnumCertificatesInStore(store
, cert
);
102 if (!(check
= CRYPT_FindCertInStore(rootStore
, cert
)))
105 CertFreeCertificateContext(check
);
107 } while (ret
&& cert
);
109 CertFreeCertificateContext(cert
);
110 CertCloseStore(rootStore
, 0);
115 HCERTCHAINENGINE
CRYPT_CreateChainEngine(HCERTSTORE root
, DWORD system_store
, const CERT_CHAIN_ENGINE_CONFIG
*config
)
117 CertificateChainEngine
*engine
;
118 HCERTSTORE worldStores
[4];
121 if(config
->cbSize
>= sizeof(CERT_CHAIN_ENGINE_CONFIG
) && config
->hExclusiveRoot
)
122 root
= CertDuplicateStore(config
->hExclusiveRoot
);
123 else if (config
->hRestrictedRoot
)
124 root
= CertDuplicateStore(config
->hRestrictedRoot
);
126 root
= CertOpenStore(CERT_STORE_PROV_SYSTEM_W
, 0, 0, system_store
, L
"Root");
131 engine
= CryptMemAlloc(sizeof(CertificateChainEngine
));
133 CertCloseStore(root
, 0);
138 engine
->hRoot
= root
;
139 engine
->hWorld
= CertOpenStore(CERT_STORE_PROV_COLLECTION
, 0, 0, CERT_STORE_CREATE_NEW_FLAG
, NULL
);
140 worldStores
[0] = CertDuplicateStore(engine
->hRoot
);
141 worldStores
[1] = CertOpenStore(CERT_STORE_PROV_SYSTEM_W
, 0, 0, system_store
, L
"CA");
142 worldStores
[2] = CertOpenStore(CERT_STORE_PROV_SYSTEM_W
, 0, 0, system_store
, L
"My");
143 worldStores
[3] = CertOpenStore(CERT_STORE_PROV_SYSTEM_W
, 0, 0, system_store
, L
"Trust");
145 CRYPT_AddStoresToCollection(engine
->hWorld
, ARRAY_SIZE(worldStores
), worldStores
);
146 CRYPT_AddStoresToCollection(engine
->hWorld
, config
->cAdditionalStore
, config
->rghAdditionalStore
);
147 CRYPT_CloseStores(ARRAY_SIZE(worldStores
), worldStores
);
149 engine
->dwFlags
= config
->dwFlags
;
150 engine
->dwUrlRetrievalTimeout
= config
->dwUrlRetrievalTimeout
;
151 engine
->MaximumCachedCertificates
= config
->MaximumCachedCertificates
;
152 if(config
->CycleDetectionModulus
)
153 engine
->CycleDetectionModulus
= config
->CycleDetectionModulus
;
155 engine
->CycleDetectionModulus
= DEFAULT_CYCLE_MODULUS
;
160 static CertificateChainEngine
*default_cu_engine
, *default_lm_engine
;
162 static CertificateChainEngine
*get_chain_engine(HCERTCHAINENGINE handle
, BOOL allow_default
)
164 const CERT_CHAIN_ENGINE_CONFIG config
= { sizeof(config
) };
166 if(handle
== HCCE_CURRENT_USER
) {
170 if(!default_cu_engine
) {
171 handle
= CRYPT_CreateChainEngine(NULL
, CERT_SYSTEM_STORE_CURRENT_USER
, &config
);
172 InterlockedCompareExchangePointer((void**)&default_cu_engine
, handle
, NULL
);
173 if(default_cu_engine
!= handle
)
174 CertFreeCertificateChainEngine(handle
);
177 return default_cu_engine
;
180 if(handle
== HCCE_LOCAL_MACHINE
) {
184 if(!default_lm_engine
) {
185 handle
= CRYPT_CreateChainEngine(NULL
, CERT_SYSTEM_STORE_LOCAL_MACHINE
, &config
);
186 InterlockedCompareExchangePointer((void**)&default_lm_engine
, handle
, NULL
);
187 if(default_lm_engine
!= handle
)
188 CertFreeCertificateChainEngine(handle
);
191 return default_lm_engine
;
194 return (CertificateChainEngine
*)handle
;
197 static void free_chain_engine(CertificateChainEngine
*engine
)
199 if(!engine
|| InterlockedDecrement(&engine
->ref
))
202 CertCloseStore(engine
->hWorld
, 0);
203 CertCloseStore(engine
->hRoot
, 0);
204 CryptMemFree(engine
);
207 typedef struct _CERT_CHAIN_ENGINE_CONFIG_NO_EXCLUSIVE_ROOT
210 HCERTSTORE hRestrictedRoot
;
211 HCERTSTORE hRestrictedTrust
;
212 HCERTSTORE hRestrictedOther
;
213 DWORD cAdditionalStore
;
214 HCERTSTORE
*rghAdditionalStore
;
216 DWORD dwUrlRetrievalTimeout
;
217 DWORD MaximumCachedCertificates
;
218 DWORD CycleDetectionModulus
;
219 } CERT_CHAIN_ENGINE_CONFIG_NO_EXCLUSIVE_ROOT
;
221 BOOL WINAPI
CertCreateCertificateChainEngine(PCERT_CHAIN_ENGINE_CONFIG pConfig
,
222 HCERTCHAINENGINE
*phChainEngine
)
226 TRACE("(%p, %p)\n", pConfig
, phChainEngine
);
228 if (pConfig
->cbSize
!= sizeof(CERT_CHAIN_ENGINE_CONFIG_NO_EXCLUSIVE_ROOT
)
229 && pConfig
->cbSize
!= sizeof(CERT_CHAIN_ENGINE_CONFIG
))
231 SetLastError(E_INVALIDARG
);
234 ret
= CRYPT_CheckRestrictedRoot(pConfig
->hRestrictedRoot
);
237 *phChainEngine
= NULL
;
241 *phChainEngine
= CRYPT_CreateChainEngine(NULL
, CERT_SYSTEM_STORE_CURRENT_USER
, pConfig
);
242 return *phChainEngine
!= NULL
;
245 void WINAPI
CertFreeCertificateChainEngine(HCERTCHAINENGINE hChainEngine
)
247 TRACE("(%p)\n", hChainEngine
);
248 free_chain_engine(get_chain_engine(hChainEngine
, FALSE
));
251 void default_chain_engine_free(void)
253 free_chain_engine(default_cu_engine
);
254 free_chain_engine(default_lm_engine
);
257 typedef struct _CertificateChain
259 CERT_CHAIN_CONTEXT context
;
264 DWORD
CRYPT_IsCertificateSelfSigned(const CERT_CONTEXT
*cert
)
266 DWORD size
, status
= 0;
270 if ((ext
= CertFindExtension(szOID_AUTHORITY_KEY_IDENTIFIER2
,
271 cert
->pCertInfo
->cExtension
, cert
->pCertInfo
->rgExtension
)))
273 CERT_AUTHORITY_KEY_ID2_INFO
*info
;
275 ret
= CryptDecodeObjectEx(cert
->dwCertEncodingType
,
276 X509_AUTHORITY_KEY_ID2
, ext
->Value
.pbData
, ext
->Value
.cbData
,
277 CRYPT_DECODE_ALLOC_FLAG
| CRYPT_DECODE_NOCOPY_FLAG
, NULL
,
281 if (info
->AuthorityCertIssuer
.cAltEntry
&&
282 info
->AuthorityCertSerialNumber
.cbData
)
284 PCERT_ALT_NAME_ENTRY directoryName
= NULL
;
287 for (i
= 0; !directoryName
&&
288 i
< info
->AuthorityCertIssuer
.cAltEntry
; i
++)
289 if (info
->AuthorityCertIssuer
.rgAltEntry
[i
].dwAltNameChoice
290 == CERT_ALT_NAME_DIRECTORY_NAME
)
292 &info
->AuthorityCertIssuer
.rgAltEntry
[i
];
295 if (CertCompareCertificateName(cert
->dwCertEncodingType
, &directoryName
->u
.DirectoryName
, &cert
->pCertInfo
->Issuer
)
296 && CertCompareIntegerBlob(&info
->AuthorityCertSerialNumber
, &cert
->pCertInfo
->SerialNumber
))
297 status
= CERT_TRUST_HAS_NAME_MATCH_ISSUER
;
301 FIXME("no supported name type in authority key id2\n");
305 else if (info
->KeyId
.cbData
)
307 ret
= CertGetCertificateContextProperty(cert
,
308 CERT_KEY_IDENTIFIER_PROP_ID
, NULL
, &size
);
309 if (ret
&& size
== info
->KeyId
.cbData
)
311 LPBYTE buf
= CryptMemAlloc(size
);
315 CertGetCertificateContextProperty(cert
, CERT_KEY_IDENTIFIER_PROP_ID
, buf
, &size
);
316 if (!memcmp(buf
, info
->KeyId
.pbData
, size
))
317 status
= CERT_TRUST_HAS_KEY_MATCH_ISSUER
;
325 else if ((ext
= CertFindExtension(szOID_AUTHORITY_KEY_IDENTIFIER
,
326 cert
->pCertInfo
->cExtension
, cert
->pCertInfo
->rgExtension
)))
328 CERT_AUTHORITY_KEY_ID_INFO
*info
;
330 ret
= CryptDecodeObjectEx(cert
->dwCertEncodingType
,
331 X509_AUTHORITY_KEY_ID
, ext
->Value
.pbData
, ext
->Value
.cbData
,
332 CRYPT_DECODE_ALLOC_FLAG
| CRYPT_DECODE_NOCOPY_FLAG
, NULL
,
336 if (info
->CertIssuer
.cbData
&& info
->CertSerialNumber
.cbData
)
338 if (CertCompareCertificateName(cert
->dwCertEncodingType
, &info
->CertIssuer
, &cert
->pCertInfo
->Issuer
)
339 && CertCompareIntegerBlob(&info
->CertSerialNumber
, &cert
->pCertInfo
->SerialNumber
))
340 status
= CERT_TRUST_HAS_NAME_MATCH_ISSUER
;
342 else if (info
->KeyId
.cbData
)
344 ret
= CertGetCertificateContextProperty(cert
,
345 CERT_KEY_IDENTIFIER_PROP_ID
, NULL
, &size
);
346 if (ret
&& size
== info
->KeyId
.cbData
)
348 LPBYTE buf
= CryptMemAlloc(size
);
352 CertGetCertificateContextProperty(cert
,
353 CERT_KEY_IDENTIFIER_PROP_ID
, buf
, &size
);
354 if (!memcmp(buf
, info
->KeyId
.pbData
, size
))
355 status
= CERT_TRUST_HAS_KEY_MATCH_ISSUER
;
364 if (CertCompareCertificateName(cert
->dwCertEncodingType
, &cert
->pCertInfo
->Subject
, &cert
->pCertInfo
->Issuer
))
365 status
= CERT_TRUST_HAS_NAME_MATCH_ISSUER
;
368 status
|= CERT_TRUST_IS_SELF_SIGNED
;
373 static void CRYPT_FreeChainElement(PCERT_CHAIN_ELEMENT element
)
375 CertFreeCertificateContext(element
->pCertContext
);
376 CryptMemFree(element
);
379 static void CRYPT_CheckSimpleChainForCycles(PCERT_SIMPLE_CHAIN chain
)
381 DWORD i
, j
, cyclicCertIndex
= 0;
383 /* O(n^2) - I don't think there's a faster way */
384 for (i
= 0; !cyclicCertIndex
&& i
< chain
->cElement
; i
++)
385 for (j
= i
+ 1; !cyclicCertIndex
&& j
< chain
->cElement
; j
++)
386 if (CertCompareCertificate(X509_ASN_ENCODING
,
387 chain
->rgpElement
[i
]->pCertContext
->pCertInfo
,
388 chain
->rgpElement
[j
]->pCertContext
->pCertInfo
))
392 chain
->rgpElement
[cyclicCertIndex
]->TrustStatus
.dwErrorStatus
393 |= CERT_TRUST_IS_CYCLIC
| CERT_TRUST_INVALID_BASIC_CONSTRAINTS
;
394 /* Release remaining certs */
395 for (i
= cyclicCertIndex
+ 1; i
< chain
->cElement
; i
++)
396 CRYPT_FreeChainElement(chain
->rgpElement
[i
]);
398 chain
->cElement
= cyclicCertIndex
+ 1;
402 /* Checks whether the chain is cyclic by examining the last element's status */
403 static inline BOOL
CRYPT_IsSimpleChainCyclic(const CERT_SIMPLE_CHAIN
*chain
)
406 return chain
->rgpElement
[chain
->cElement
- 1]->TrustStatus
.dwErrorStatus
407 & CERT_TRUST_IS_CYCLIC
;
412 static inline void CRYPT_CombineTrustStatus(CERT_TRUST_STATUS
*chainStatus
,
413 const CERT_TRUST_STATUS
*elementStatus
)
415 /* Any error that applies to an element also applies to a chain.. */
416 chainStatus
->dwErrorStatus
|= elementStatus
->dwErrorStatus
;
417 /* but the bottom nibble of an element's info status doesn't apply to the
420 chainStatus
->dwInfoStatus
|= (elementStatus
->dwInfoStatus
& 0xfffffff0);
423 static BOOL
CRYPT_AddCertToSimpleChain(const CertificateChainEngine
*engine
,
424 PCERT_SIMPLE_CHAIN chain
, PCCERT_CONTEXT cert
, DWORD subjectInfoStatus
)
427 PCERT_CHAIN_ELEMENT element
= CryptMemAlloc(sizeof(CERT_CHAIN_ELEMENT
));
431 if (!chain
->cElement
)
432 chain
->rgpElement
= CryptMemAlloc(sizeof(PCERT_CHAIN_ELEMENT
));
434 chain
->rgpElement
= CryptMemRealloc(chain
->rgpElement
,
435 (chain
->cElement
+ 1) * sizeof(PCERT_CHAIN_ELEMENT
));
436 if (chain
->rgpElement
)
438 chain
->rgpElement
[chain
->cElement
++] = element
;
439 memset(element
, 0, sizeof(CERT_CHAIN_ELEMENT
));
440 element
->cbSize
= sizeof(CERT_CHAIN_ELEMENT
);
441 element
->pCertContext
= CertDuplicateCertificateContext(cert
);
442 if (chain
->cElement
> 1)
443 chain
->rgpElement
[chain
->cElement
- 2]->TrustStatus
.dwInfoStatus
445 /* FIXME: initialize the rest of element */
446 if (!(chain
->cElement
% engine
->CycleDetectionModulus
))
448 CRYPT_CheckSimpleChainForCycles(chain
);
449 /* Reinitialize the element pointer in case the chain is
450 * cyclic, in which case the chain is truncated.
452 element
= chain
->rgpElement
[chain
->cElement
- 1];
454 CRYPT_CombineTrustStatus(&chain
->TrustStatus
,
455 &element
->TrustStatus
);
459 CryptMemFree(element
);
464 static void CRYPT_FreeSimpleChain(PCERT_SIMPLE_CHAIN chain
)
468 for (i
= 0; i
< chain
->cElement
; i
++)
469 CRYPT_FreeChainElement(chain
->rgpElement
[i
]);
470 CryptMemFree(chain
->rgpElement
);
474 static void CRYPT_CheckTrustedStatus(HCERTSTORE hRoot
,
475 PCERT_CHAIN_ELEMENT rootElement
)
477 PCCERT_CONTEXT trustedRoot
= CRYPT_FindCertInStore(hRoot
,
478 rootElement
->pCertContext
);
481 rootElement
->TrustStatus
.dwErrorStatus
|=
482 CERT_TRUST_IS_UNTRUSTED_ROOT
;
484 CertFreeCertificateContext(trustedRoot
);
487 static void CRYPT_CheckRootCert(HCERTSTORE hRoot
,
488 PCERT_CHAIN_ELEMENT rootElement
)
490 PCCERT_CONTEXT root
= rootElement
->pCertContext
;
492 if (!CryptVerifyCertificateSignatureEx(0, root
->dwCertEncodingType
,
493 CRYPT_VERIFY_CERT_SIGN_SUBJECT_CERT
, (void *)root
,
494 CRYPT_VERIFY_CERT_SIGN_ISSUER_CERT
, (void *)root
, 0, NULL
))
496 TRACE_(chain
)("Last certificate's signature is invalid\n");
497 rootElement
->TrustStatus
.dwErrorStatus
|=
498 CERT_TRUST_IS_NOT_SIGNATURE_VALID
;
500 CRYPT_CheckTrustedStatus(hRoot
, rootElement
);
503 /* Decodes a cert's basic constraints extension (either szOID_BASIC_CONSTRAINTS
504 * or szOID_BASIC_CONSTRAINTS2, whichever is present) into a
505 * CERT_BASIC_CONSTRAINTS2_INFO. If it neither extension is present, sets
506 * constraints->fCA to defaultIfNotSpecified.
507 * Returns FALSE if the extension is present but couldn't be decoded.
509 static BOOL
CRYPT_DecodeBasicConstraints(PCCERT_CONTEXT cert
,
510 CERT_BASIC_CONSTRAINTS2_INFO
*constraints
, BOOL defaultIfNotSpecified
)
513 PCERT_EXTENSION ext
= CertFindExtension(szOID_BASIC_CONSTRAINTS
,
514 cert
->pCertInfo
->cExtension
, cert
->pCertInfo
->rgExtension
);
516 constraints
->fPathLenConstraint
= FALSE
;
519 CERT_BASIC_CONSTRAINTS_INFO
*info
;
522 ret
= CryptDecodeObjectEx(X509_ASN_ENCODING
, szOID_BASIC_CONSTRAINTS
,
523 ext
->Value
.pbData
, ext
->Value
.cbData
, CRYPT_DECODE_ALLOC_FLAG
,
527 if (info
->SubjectType
.cbData
== 1)
529 info
->SubjectType
.pbData
[0] & CERT_CA_SUBJECT_FLAG
;
535 ext
= CertFindExtension(szOID_BASIC_CONSTRAINTS2
,
536 cert
->pCertInfo
->cExtension
, cert
->pCertInfo
->rgExtension
);
539 DWORD size
= sizeof(CERT_BASIC_CONSTRAINTS2_INFO
);
541 ret
= CryptDecodeObjectEx(X509_ASN_ENCODING
,
542 szOID_BASIC_CONSTRAINTS2
, ext
->Value
.pbData
, ext
->Value
.cbData
,
543 0, NULL
, constraints
, &size
);
546 constraints
->fCA
= defaultIfNotSpecified
;
551 /* Checks element's basic constraints to see if it can act as a CA, with
552 * remainingCAs CAs left in this chain. In general, a cert must include the
553 * basic constraints extension, with the CA flag asserted, in order to be
554 * allowed to be a CA. A V1 or V2 cert, which has no extensions, is also
555 * allowed to be a CA if it's installed locally (in the engine's world store.)
556 * This matches the expected usage in RFC 5280, section 4.2.1.9: a conforming
557 * CA MUST include the basic constraints extension in all certificates that are
558 * used to validate digital signatures on certificates. It also matches
559 * section 6.1.4(k): "If a certificate is a v1 or v2 certificate, then the
560 * application MUST either verify that the certificate is a CA certificate
561 * through out-of-band means or reject the certificate." Rejecting the
562 * certificate prohibits a large number of commonly used certificates, so
563 * accepting locally installed ones is a compromise.
564 * Root certificates are also allowed to be CAs even without a basic
565 * constraints extension. This is implied by RFC 5280, section 6.1: the
566 * root of a certificate chain's only requirement is that it was used to issue
567 * the next certificate in the chain.
568 * Updates chainConstraints with the element's constraints, if:
569 * 1. chainConstraints doesn't have a path length constraint, or
570 * 2. element's path length constraint is smaller than chainConstraints's
571 * Sets *pathLengthConstraintViolated to TRUE if a path length violation
573 * Returns TRUE if the element can be a CA, and the length of the remaining
576 static BOOL
CRYPT_CheckBasicConstraintsForCA(CertificateChainEngine
*engine
,
577 PCCERT_CONTEXT cert
, CERT_BASIC_CONSTRAINTS2_INFO
*chainConstraints
,
578 DWORD remainingCAs
, BOOL isRoot
, BOOL
*pathLengthConstraintViolated
)
580 BOOL validBasicConstraints
, implicitCA
= FALSE
;
581 CERT_BASIC_CONSTRAINTS2_INFO constraints
;
585 else if (cert
->pCertInfo
->dwVersion
== CERT_V1
||
586 cert
->pCertInfo
->dwVersion
== CERT_V2
)
589 DWORD size
= sizeof(hash
);
591 if (CertGetCertificateContextProperty(cert
, CERT_HASH_PROP_ID
,
594 CRYPT_HASH_BLOB blob
= { sizeof(hash
), hash
};
595 PCCERT_CONTEXT localCert
= CertFindCertificateInStore(
596 engine
->hWorld
, cert
->dwCertEncodingType
, 0, CERT_FIND_SHA1_HASH
,
601 CertFreeCertificateContext(localCert
);
606 if ((validBasicConstraints
= CRYPT_DecodeBasicConstraints(cert
,
607 &constraints
, implicitCA
)))
609 chainConstraints
->fCA
= constraints
.fCA
;
610 if (!constraints
.fCA
)
612 TRACE_(chain
)("chain element %d can't be a CA\n", remainingCAs
+ 1);
613 validBasicConstraints
= FALSE
;
615 else if (constraints
.fPathLenConstraint
)
617 /* If the element has path length constraints, they apply to the
618 * entire remaining chain.
620 if (!chainConstraints
->fPathLenConstraint
||
621 constraints
.dwPathLenConstraint
<
622 chainConstraints
->dwPathLenConstraint
)
624 TRACE_(chain
)("setting path length constraint to %d\n",
625 chainConstraints
->dwPathLenConstraint
);
626 chainConstraints
->fPathLenConstraint
= TRUE
;
627 chainConstraints
->dwPathLenConstraint
=
628 constraints
.dwPathLenConstraint
;
632 if (chainConstraints
->fPathLenConstraint
&&
633 remainingCAs
> chainConstraints
->dwPathLenConstraint
)
635 TRACE_(chain
)("remaining CAs %d exceed max path length %d\n",
636 remainingCAs
, chainConstraints
->dwPathLenConstraint
);
637 validBasicConstraints
= FALSE
;
638 *pathLengthConstraintViolated
= TRUE
;
640 return validBasicConstraints
;
643 static BOOL
domain_name_matches(LPCWSTR constraint
, LPCWSTR name
)
647 /* RFC 5280, section 4.2.1.10:
648 * "For URIs, the constraint applies to the host part of the name...
649 * When the constraint begins with a period, it MAY be expanded with one
650 * or more labels. That is, the constraint ".example.com" is satisfied by
651 * both host.example.com and my.host.example.com. However, the constraint
652 * ".example.com" is not satisfied by "example.com". When the constraint
653 * does not begin with a period, it specifies a host."
654 * and for email addresses,
655 * "To indicate all Internet mail addresses on a particular host, the
656 * constraint is specified as the host name. For example, the constraint
657 * "example.com" is satisfied by any mail address at the host
658 * "example.com". To specify any address within a domain, the constraint
659 * is specified with a leading period (as with URIs)."
661 if (constraint
[0] == '.')
663 /* Must be strictly greater than, a name can't begin with '.' */
664 if (lstrlenW(name
) > lstrlenW(constraint
))
665 match
= !lstrcmpiW(name
+ lstrlenW(name
) - lstrlenW(constraint
),
669 /* name is too short, no match */
674 match
= !lstrcmpiW(name
, constraint
);
678 static BOOL
url_matches(LPCWSTR constraint
, LPCWSTR name
,
679 DWORD
*trustErrorStatus
)
683 TRACE("%s, %s\n", debugstr_w(constraint
), debugstr_w(name
));
686 *trustErrorStatus
|= CERT_TRUST_INVALID_NAME_CONSTRAINTS
;
691 LPCWSTR colon
, authority_end
, at
, hostname
= NULL
;
692 /* The maximum length for a hostname is 254 in the DNS, see RFC 1034 */
693 WCHAR hostname_buf
[255];
695 /* RFC 5280: only the hostname portion of the URL is compared. From
697 * "For URIs, the constraint applies to the host part of the name.
698 * The constraint MUST be specified as a fully qualified domain name
699 * and MAY specify a host or a domain."
700 * The format for URIs is in RFC 2396.
702 * First, remove any scheme that's present. */
703 colon
= wcschr(name
, ':');
704 if (colon
&& *(colon
+ 1) == '/' && *(colon
+ 2) == '/')
706 /* Next, find the end of the authority component. (The authority is
707 * generally just the hostname, but it may contain a username or a port.
708 * Those are removed next.)
710 authority_end
= wcschr(name
, '/');
712 authority_end
= wcschr(name
, '?');
714 authority_end
= name
+ lstrlenW(name
);
715 /* Remove any port number from the authority. The userinfo portion
716 * of an authority may contain a colon, so stop if a userinfo portion
717 * is found (indicated by '@').
719 for (colon
= authority_end
; colon
>= name
&& *colon
!= ':' &&
720 *colon
!= '@'; colon
--)
723 authority_end
= colon
;
724 /* Remove any username from the authority */
725 if ((at
= wcschr(name
, '@')))
727 /* Ignore any path or query portion of the URL. */
730 if (authority_end
- name
< ARRAY_SIZE(hostname_buf
))
732 memcpy(hostname_buf
, name
,
733 (authority_end
- name
) * sizeof(WCHAR
));
734 hostname_buf
[authority_end
- name
] = 0;
735 hostname
= hostname_buf
;
737 /* else: Hostname is too long, not a match */
742 match
= domain_name_matches(constraint
, hostname
);
747 static BOOL
rfc822_name_matches(LPCWSTR constraint
, LPCWSTR name
,
748 DWORD
*trustErrorStatus
)
753 TRACE("%s, %s\n", debugstr_w(constraint
), debugstr_w(name
));
756 *trustErrorStatus
|= CERT_TRUST_INVALID_NAME_CONSTRAINTS
;
759 else if (wcschr(constraint
, '@'))
760 match
= !lstrcmpiW(constraint
, name
);
763 if ((at
= wcschr(name
, '@')))
764 match
= domain_name_matches(constraint
, at
+ 1);
766 match
= !lstrcmpiW(constraint
, name
);
771 static BOOL
dns_name_matches(LPCWSTR constraint
, LPCWSTR name
,
772 DWORD
*trustErrorStatus
)
776 TRACE("%s, %s\n", debugstr_w(constraint
), debugstr_w(name
));
779 *trustErrorStatus
|= CERT_TRUST_INVALID_NAME_CONSTRAINTS
;
782 /* RFC 5280, section 4.2.1.10:
783 * "DNS name restrictions are expressed as host.example.com. Any DNS name
784 * that can be constructed by simply adding zero or more labels to the
785 * left-hand side of the name satisfies the name constraint. For example,
786 * www.host.example.com would satisfy the constraint but host1.example.com
789 else if (lstrlenW(name
) == lstrlenW(constraint
))
790 match
= !lstrcmpiW(name
, constraint
);
791 else if (lstrlenW(name
) > lstrlenW(constraint
))
793 match
= !lstrcmpiW(name
+ lstrlenW(name
) - lstrlenW(constraint
),
800 /* This only matches if name is a subdomain of constraint, i.e.
801 * there's a '.' between the beginning of the name and the
802 * matching portion of the name.
804 for (ptr
= name
+ lstrlenW(name
) - lstrlenW(constraint
);
805 !dot
&& ptr
>= name
; ptr
--)
811 /* else: name is too short, no match */
816 static BOOL
ip_address_matches(const CRYPT_DATA_BLOB
*constraint
,
817 const CRYPT_DATA_BLOB
*name
, DWORD
*trustErrorStatus
)
821 TRACE("(%d, %p), (%d, %p)\n", constraint
->cbData
, constraint
->pbData
,
822 name
->cbData
, name
->pbData
);
824 /* RFC5280, section 4.2.1.10, iPAddress syntax: either 8 or 32 bytes, for
825 * IPv4 or IPv6 addresses, respectively.
827 if (constraint
->cbData
!= sizeof(DWORD
) * 2 && constraint
->cbData
!= 32)
828 *trustErrorStatus
|= CERT_TRUST_INVALID_NAME_CONSTRAINTS
;
829 else if (name
->cbData
== sizeof(DWORD
) &&
830 constraint
->cbData
== sizeof(DWORD
) * 2)
832 DWORD subnet
, mask
, addr
;
834 memcpy(&subnet
, constraint
->pbData
, sizeof(subnet
));
835 memcpy(&mask
, constraint
->pbData
+ sizeof(subnet
), sizeof(mask
));
836 memcpy(&addr
, name
->pbData
, sizeof(addr
));
837 /* These are really in big-endian order, but for equality matching we
838 * don't need to swap to host order
840 match
= (subnet
& mask
) == (addr
& mask
);
842 else if (name
->cbData
== 16 && constraint
->cbData
== 32)
844 const BYTE
*subnet
, *mask
, *addr
;
847 subnet
= constraint
->pbData
;
848 mask
= constraint
->pbData
+ 16;
851 for (i
= 0; match
&& i
< 16; i
++)
852 if ((subnet
[i
] & mask
[i
]) != (addr
[i
] & mask
[i
]))
855 /* else: name is wrong size, no match */
860 static BOOL
directory_name_matches(const CERT_NAME_BLOB
*constraint
,
861 const CERT_NAME_BLOB
*name
)
863 CERT_NAME_INFO
*constraintName
;
867 if (CryptDecodeObjectEx(X509_ASN_ENCODING
, X509_NAME
, constraint
->pbData
,
868 constraint
->cbData
, CRYPT_DECODE_ALLOC_FLAG
, NULL
, &constraintName
, &size
))
873 for (i
= 0; match
&& i
< constraintName
->cRDN
; i
++)
874 match
= CertIsRDNAttrsInCertificateName(X509_ASN_ENCODING
,
875 CERT_CASE_INSENSITIVE_IS_RDN_ATTRS_FLAG
,
876 (CERT_NAME_BLOB
*)name
, &constraintName
->rgRDN
[i
]);
877 LocalFree(constraintName
);
882 static BOOL
alt_name_matches(const CERT_ALT_NAME_ENTRY
*name
,
883 const CERT_ALT_NAME_ENTRY
*constraint
, DWORD
*trustErrorStatus
, BOOL
*present
)
887 if (name
->dwAltNameChoice
== constraint
->dwAltNameChoice
)
891 switch (constraint
->dwAltNameChoice
)
893 case CERT_ALT_NAME_RFC822_NAME
:
894 match
= rfc822_name_matches(constraint
->u
.pwszURL
,
895 name
->u
.pwszURL
, trustErrorStatus
);
897 case CERT_ALT_NAME_DNS_NAME
:
898 match
= dns_name_matches(constraint
->u
.pwszURL
,
899 name
->u
.pwszURL
, trustErrorStatus
);
901 case CERT_ALT_NAME_URL
:
902 match
= url_matches(constraint
->u
.pwszURL
,
903 name
->u
.pwszURL
, trustErrorStatus
);
905 case CERT_ALT_NAME_IP_ADDRESS
:
906 match
= ip_address_matches(&constraint
->u
.IPAddress
,
907 &name
->u
.IPAddress
, trustErrorStatus
);
909 case CERT_ALT_NAME_DIRECTORY_NAME
:
910 match
= directory_name_matches(&constraint
->u
.DirectoryName
,
911 &name
->u
.DirectoryName
);
914 ERR("name choice %d unsupported in this context\n",
915 constraint
->dwAltNameChoice
);
917 CERT_TRUST_HAS_NOT_SUPPORTED_NAME_CONSTRAINT
;
925 static BOOL
alt_name_matches_excluded_name(const CERT_ALT_NAME_ENTRY
*name
,
926 const CERT_NAME_CONSTRAINTS_INFO
*nameConstraints
, DWORD
*trustErrorStatus
)
931 for (i
= 0; !match
&& i
< nameConstraints
->cExcludedSubtree
; i
++)
932 match
= alt_name_matches(name
,
933 &nameConstraints
->rgExcludedSubtree
[i
].Base
, trustErrorStatus
, NULL
);
937 static BOOL
alt_name_matches_permitted_name(const CERT_ALT_NAME_ENTRY
*name
,
938 const CERT_NAME_CONSTRAINTS_INFO
*nameConstraints
, DWORD
*trustErrorStatus
,
944 for (i
= 0; !match
&& i
< nameConstraints
->cPermittedSubtree
; i
++)
945 match
= alt_name_matches(name
,
946 &nameConstraints
->rgPermittedSubtree
[i
].Base
, trustErrorStatus
,
951 static inline PCERT_EXTENSION
get_subject_alt_name_ext(const CERT_INFO
*cert
)
955 ext
= CertFindExtension(szOID_SUBJECT_ALT_NAME2
,
956 cert
->cExtension
, cert
->rgExtension
);
958 ext
= CertFindExtension(szOID_SUBJECT_ALT_NAME
,
959 cert
->cExtension
, cert
->rgExtension
);
963 static void compare_alt_name_with_constraints(const CERT_EXTENSION
*altNameExt
,
964 const CERT_NAME_CONSTRAINTS_INFO
*nameConstraints
, DWORD
*trustErrorStatus
)
966 CERT_ALT_NAME_INFO
*subjectAltName
;
969 if (CryptDecodeObjectEx(X509_ASN_ENCODING
, X509_ALTERNATE_NAME
,
970 altNameExt
->Value
.pbData
, altNameExt
->Value
.cbData
,
971 CRYPT_DECODE_ALLOC_FLAG
| CRYPT_DECODE_NOCOPY_FLAG
, NULL
,
972 &subjectAltName
, &size
))
976 for (i
= 0; i
< subjectAltName
->cAltEntry
; i
++)
978 BOOL nameFormPresent
;
980 /* A name constraint only applies if the name form is present.
981 * From RFC 5280, section 4.2.1.10:
982 * "Restrictions apply only when the specified name form is
983 * present. If no name of the type is in the certificate,
984 * the certificate is acceptable."
986 if (alt_name_matches_excluded_name(
987 &subjectAltName
->rgAltEntry
[i
], nameConstraints
,
990 TRACE_(chain
)("subject alternate name form %d excluded\n",
991 subjectAltName
->rgAltEntry
[i
].dwAltNameChoice
);
993 CERT_TRUST_HAS_EXCLUDED_NAME_CONSTRAINT
;
995 nameFormPresent
= FALSE
;
996 if (!alt_name_matches_permitted_name(
997 &subjectAltName
->rgAltEntry
[i
], nameConstraints
,
998 trustErrorStatus
, &nameFormPresent
) && nameFormPresent
)
1000 TRACE_(chain
)("subject alternate name form %d not permitted\n",
1001 subjectAltName
->rgAltEntry
[i
].dwAltNameChoice
);
1002 *trustErrorStatus
|=
1003 CERT_TRUST_HAS_NOT_PERMITTED_NAME_CONSTRAINT
;
1006 LocalFree(subjectAltName
);
1009 *trustErrorStatus
|=
1010 CERT_TRUST_INVALID_EXTENSION
| CERT_TRUST_INVALID_NAME_CONSTRAINTS
;
1013 static BOOL
rfc822_attr_matches_excluded_name(const CERT_RDN_ATTR
*attr
,
1014 const CERT_NAME_CONSTRAINTS_INFO
*nameConstraints
, DWORD
*trustErrorStatus
)
1019 for (i
= 0; !match
&& i
< nameConstraints
->cExcludedSubtree
; i
++)
1021 const CERT_ALT_NAME_ENTRY
*constraint
=
1022 &nameConstraints
->rgExcludedSubtree
[i
].Base
;
1024 if (constraint
->dwAltNameChoice
== CERT_ALT_NAME_RFC822_NAME
)
1025 match
= rfc822_name_matches(constraint
->u
.pwszRfc822Name
,
1026 (LPCWSTR
)attr
->Value
.pbData
, trustErrorStatus
);
1031 static BOOL
rfc822_attr_matches_permitted_name(const CERT_RDN_ATTR
*attr
,
1032 const CERT_NAME_CONSTRAINTS_INFO
*nameConstraints
, DWORD
*trustErrorStatus
,
1038 for (i
= 0; !match
&& i
< nameConstraints
->cPermittedSubtree
; i
++)
1040 const CERT_ALT_NAME_ENTRY
*constraint
=
1041 &nameConstraints
->rgPermittedSubtree
[i
].Base
;
1043 if (constraint
->dwAltNameChoice
== CERT_ALT_NAME_RFC822_NAME
)
1046 match
= rfc822_name_matches(constraint
->u
.pwszRfc822Name
,
1047 (LPCWSTR
)attr
->Value
.pbData
, trustErrorStatus
);
1053 static void compare_subject_with_email_constraints(
1054 const CERT_NAME_BLOB
*subjectName
,
1055 const CERT_NAME_CONSTRAINTS_INFO
*nameConstraints
, DWORD
*trustErrorStatus
)
1057 CERT_NAME_INFO
*name
;
1060 if (CryptDecodeObjectEx(X509_ASN_ENCODING
, X509_UNICODE_NAME
,
1061 subjectName
->pbData
, subjectName
->cbData
,
1062 CRYPT_DECODE_ALLOC_FLAG
| CRYPT_DECODE_NOCOPY_FLAG
, NULL
, &name
, &size
))
1066 for (i
= 0; i
< name
->cRDN
; i
++)
1067 for (j
= 0; j
< name
->rgRDN
[i
].cRDNAttr
; j
++)
1068 if (!strcmp(name
->rgRDN
[i
].rgRDNAttr
[j
].pszObjId
,
1069 szOID_RSA_emailAddr
))
1071 BOOL nameFormPresent
;
1073 /* A name constraint only applies if the name form is
1074 * present. From RFC 5280, section 4.2.1.10:
1075 * "Restrictions apply only when the specified name form is
1076 * present. If no name of the type is in the certificate,
1077 * the certificate is acceptable."
1079 if (rfc822_attr_matches_excluded_name(
1080 &name
->rgRDN
[i
].rgRDNAttr
[j
], nameConstraints
,
1084 "email address in subject name is excluded\n");
1085 *trustErrorStatus
|=
1086 CERT_TRUST_HAS_EXCLUDED_NAME_CONSTRAINT
;
1088 nameFormPresent
= FALSE
;
1089 if (!rfc822_attr_matches_permitted_name(
1090 &name
->rgRDN
[i
].rgRDNAttr
[j
], nameConstraints
,
1091 trustErrorStatus
, &nameFormPresent
) && nameFormPresent
)
1094 "email address in subject name is not permitted\n");
1095 *trustErrorStatus
|=
1096 CERT_TRUST_HAS_NOT_PERMITTED_NAME_CONSTRAINT
;
1102 *trustErrorStatus
|=
1103 CERT_TRUST_INVALID_EXTENSION
| CERT_TRUST_INVALID_NAME_CONSTRAINTS
;
1106 static BOOL
CRYPT_IsEmptyName(const CERT_NAME_BLOB
*name
)
1112 else if (name
->cbData
== 2 && name
->pbData
[1] == 0)
1114 /* An empty sequence is also empty */
1122 static void compare_subject_with_constraints(const CERT_NAME_BLOB
*subjectName
,
1123 const CERT_NAME_CONSTRAINTS_INFO
*nameConstraints
, DWORD
*trustErrorStatus
)
1125 BOOL hasEmailConstraint
= FALSE
;
1128 /* In general, a subject distinguished name only matches a directory name
1129 * constraint. However, an exception exists for email addresses.
1130 * From RFC 5280, section 4.2.1.6:
1131 * "Legacy implementations exist where an electronic mail address is
1132 * embedded in the subject distinguished name as an emailAddress
1133 * attribute [RFC2985]."
1134 * If an email address constraint exists, check that constraint separately.
1136 for (i
= 0; !hasEmailConstraint
&& i
< nameConstraints
->cExcludedSubtree
;
1138 if (nameConstraints
->rgExcludedSubtree
[i
].Base
.dwAltNameChoice
==
1139 CERT_ALT_NAME_RFC822_NAME
)
1140 hasEmailConstraint
= TRUE
;
1141 for (i
= 0; !hasEmailConstraint
&& i
< nameConstraints
->cPermittedSubtree
;
1143 if (nameConstraints
->rgPermittedSubtree
[i
].Base
.dwAltNameChoice
==
1144 CERT_ALT_NAME_RFC822_NAME
)
1145 hasEmailConstraint
= TRUE
;
1146 if (hasEmailConstraint
)
1147 compare_subject_with_email_constraints(subjectName
, nameConstraints
,
1149 for (i
= 0; i
< nameConstraints
->cExcludedSubtree
; i
++)
1151 CERT_ALT_NAME_ENTRY
*constraint
=
1152 &nameConstraints
->rgExcludedSubtree
[i
].Base
;
1154 if (constraint
->dwAltNameChoice
== CERT_ALT_NAME_DIRECTORY_NAME
&&
1155 directory_name_matches(&constraint
->u
.DirectoryName
, subjectName
))
1157 TRACE_(chain
)("subject name is excluded\n");
1158 *trustErrorStatus
|=
1159 CERT_TRUST_HAS_EXCLUDED_NAME_CONSTRAINT
;
1162 /* RFC 5280, section 4.2.1.10:
1163 * "Restrictions apply only when the specified name form is present.
1164 * If no name of the type is in the certificate, the certificate is
1166 * An empty name can't have the name form present, so don't check it.
1168 if (nameConstraints
->cPermittedSubtree
&& !CRYPT_IsEmptyName(subjectName
))
1170 BOOL match
= FALSE
, hasDirectoryConstraint
= FALSE
;
1172 for (i
= 0; !match
&& i
< nameConstraints
->cPermittedSubtree
; i
++)
1174 CERT_ALT_NAME_ENTRY
*constraint
=
1175 &nameConstraints
->rgPermittedSubtree
[i
].Base
;
1177 if (constraint
->dwAltNameChoice
== CERT_ALT_NAME_DIRECTORY_NAME
)
1179 hasDirectoryConstraint
= TRUE
;
1180 match
= directory_name_matches(&constraint
->u
.DirectoryName
,
1184 if (hasDirectoryConstraint
&& !match
)
1186 TRACE_(chain
)("subject name is not permitted\n");
1187 *trustErrorStatus
|= CERT_TRUST_HAS_NOT_PERMITTED_NAME_CONSTRAINT
;
1192 static void CRYPT_CheckNameConstraints(
1193 const CERT_NAME_CONSTRAINTS_INFO
*nameConstraints
, const CERT_INFO
*cert
,
1194 DWORD
*trustErrorStatus
)
1196 CERT_EXTENSION
*ext
= get_subject_alt_name_ext(cert
);
1199 compare_alt_name_with_constraints(ext
, nameConstraints
,
1201 /* Name constraints apply to the subject alternative name as well as the
1202 * subject name. From RFC 5280, section 4.2.1.10:
1203 * "Restrictions apply to the subject distinguished name and apply to
1204 * subject alternative names."
1206 compare_subject_with_constraints(&cert
->Subject
, nameConstraints
,
1210 /* Gets cert's name constraints, if any. Free with LocalFree. */
1211 static CERT_NAME_CONSTRAINTS_INFO
*CRYPT_GetNameConstraints(CERT_INFO
*cert
)
1213 CERT_NAME_CONSTRAINTS_INFO
*info
= NULL
;
1215 CERT_EXTENSION
*ext
;
1217 if ((ext
= CertFindExtension(szOID_NAME_CONSTRAINTS
, cert
->cExtension
,
1218 cert
->rgExtension
)))
1222 CryptDecodeObjectEx(X509_ASN_ENCODING
, X509_NAME_CONSTRAINTS
,
1223 ext
->Value
.pbData
, ext
->Value
.cbData
,
1224 CRYPT_DECODE_ALLOC_FLAG
| CRYPT_DECODE_NOCOPY_FLAG
, NULL
, &info
,
1230 static BOOL
CRYPT_IsValidNameConstraint(const CERT_NAME_CONSTRAINTS_INFO
*info
)
1235 /* Make sure at least one permitted or excluded subtree is present. From
1236 * RFC 5280, section 4.2.1.10:
1237 * "Conforming CAs MUST NOT issue certificates where name constraints is an
1238 * empty sequence. That is, either the permittedSubtrees field or the
1239 * excludedSubtrees MUST be present."
1241 if (!info
->cPermittedSubtree
&& !info
->cExcludedSubtree
)
1243 WARN_(chain
)("constraints contain no permitted nor excluded subtree\n");
1246 /* Check that none of the constraints specifies a minimum or a maximum.
1247 * See RFC 5280, section 4.2.1.10:
1248 * "Within this profile, the minimum and maximum fields are not used with
1249 * any name forms, thus, the minimum MUST be zero, and maximum MUST be
1250 * absent. However, if an application encounters a critical name
1251 * constraints extension that specifies other values for minimum or
1252 * maximum for a name form that appears in a subsequent certificate, the
1253 * application MUST either process these fields or reject the
1255 * Since it gives no guidance as to how to process these fields, we
1256 * reject any name constraint that contains them.
1258 for (i
= 0; ret
&& i
< info
->cPermittedSubtree
; i
++)
1259 if (info
->rgPermittedSubtree
[i
].dwMinimum
||
1260 info
->rgPermittedSubtree
[i
].fMaximum
)
1262 TRACE_(chain
)("found a minimum or maximum in permitted subtrees\n");
1265 for (i
= 0; ret
&& i
< info
->cExcludedSubtree
; i
++)
1266 if (info
->rgExcludedSubtree
[i
].dwMinimum
||
1267 info
->rgExcludedSubtree
[i
].fMaximum
)
1269 TRACE_(chain
)("found a minimum or maximum in excluded subtrees\n");
1275 static void CRYPT_CheckChainNameConstraints(PCERT_SIMPLE_CHAIN chain
)
1279 /* Microsoft's implementation appears to violate RFC 3280: according to
1280 * MSDN, the various CERT_TRUST_*_NAME_CONSTRAINT errors are set if a CA's
1281 * name constraint is violated in the end cert. According to RFC 3280,
1282 * the constraints should be checked against every subsequent certificate
1283 * in the chain, not just the end cert.
1284 * Microsoft's implementation also sets the name constraint errors on the
1285 * certs whose constraints were violated, not on the certs that violated
1287 * In order to be error-compatible with Microsoft's implementation, while
1288 * still adhering to RFC 3280, I use a O(n ^ 2) algorithm to check name
1291 for (i
= chain
->cElement
- 1; i
> 0; i
--)
1293 CERT_NAME_CONSTRAINTS_INFO
*nameConstraints
;
1295 if ((nameConstraints
= CRYPT_GetNameConstraints(
1296 chain
->rgpElement
[i
]->pCertContext
->pCertInfo
)))
1298 if (!CRYPT_IsValidNameConstraint(nameConstraints
))
1299 chain
->rgpElement
[i
]->TrustStatus
.dwErrorStatus
|=
1300 CERT_TRUST_HAS_NOT_SUPPORTED_NAME_CONSTRAINT
;
1303 for (j
= i
- 1; j
>= 0; j
--)
1305 DWORD errorStatus
= 0;
1307 /* According to RFC 3280, self-signed certs don't have name
1308 * constraints checked unless they're the end cert.
1310 if (j
== 0 || !CRYPT_IsCertificateSelfSigned(
1311 chain
->rgpElement
[j
]->pCertContext
))
1313 CRYPT_CheckNameConstraints(nameConstraints
,
1314 chain
->rgpElement
[j
]->pCertContext
->pCertInfo
,
1318 chain
->rgpElement
[i
]->TrustStatus
.dwErrorStatus
|=
1320 CRYPT_CombineTrustStatus(&chain
->TrustStatus
,
1321 &chain
->rgpElement
[i
]->TrustStatus
);
1324 chain
->rgpElement
[i
]->TrustStatus
.dwInfoStatus
|=
1325 CERT_TRUST_HAS_VALID_NAME_CONSTRAINTS
;
1329 LocalFree(nameConstraints
);
1334 /* Gets cert's policies info, if any. Free with LocalFree. */
1335 static CERT_POLICIES_INFO
*CRYPT_GetPolicies(PCCERT_CONTEXT cert
)
1337 PCERT_EXTENSION ext
;
1338 CERT_POLICIES_INFO
*policies
= NULL
;
1340 ext
= CertFindExtension(szOID_KEY_USAGE
, cert
->pCertInfo
->cExtension
,
1341 cert
->pCertInfo
->rgExtension
);
1346 CryptDecodeObjectEx(X509_ASN_ENCODING
, X509_CERT_POLICIES
,
1347 ext
->Value
.pbData
, ext
->Value
.cbData
, CRYPT_DECODE_ALLOC_FLAG
, NULL
,
1353 static void CRYPT_CheckPolicies(const CERT_POLICIES_INFO
*policies
, CERT_INFO
*cert
,
1358 for (i
= 0; i
< policies
->cPolicyInfo
; i
++)
1360 /* For now, the only accepted policy identifier is the anyPolicy
1362 * FIXME: the policy identifiers should be compared against the
1363 * cert's certificate policies extension, subject to the policy
1364 * mappings extension, and the policy constraints extension.
1365 * See RFC 5280, sections 4.2.1.4, 4.2.1.5, and 4.2.1.11.
1367 if (strcmp(policies
->rgPolicyInfo
[i
].pszPolicyIdentifier
,
1368 szOID_ANY_CERT_POLICY
))
1370 FIXME("unsupported policy %s\n",
1371 policies
->rgPolicyInfo
[i
].pszPolicyIdentifier
);
1372 *errorStatus
|= CERT_TRUST_INVALID_POLICY_CONSTRAINTS
;
1377 static void CRYPT_CheckChainPolicies(PCERT_SIMPLE_CHAIN chain
)
1381 for (i
= chain
->cElement
- 1; i
> 0; i
--)
1383 CERT_POLICIES_INFO
*policies
;
1385 if ((policies
= CRYPT_GetPolicies(chain
->rgpElement
[i
]->pCertContext
)))
1387 for (j
= i
- 1; j
>= 0; j
--)
1389 DWORD errorStatus
= 0;
1391 CRYPT_CheckPolicies(policies
,
1392 chain
->rgpElement
[j
]->pCertContext
->pCertInfo
, &errorStatus
);
1395 chain
->rgpElement
[i
]->TrustStatus
.dwErrorStatus
|=
1397 CRYPT_CombineTrustStatus(&chain
->TrustStatus
,
1398 &chain
->rgpElement
[i
]->TrustStatus
);
1401 LocalFree(policies
);
1406 static LPWSTR
name_value_to_str(const CERT_NAME_BLOB
*name
)
1408 DWORD len
= cert_name_to_str_with_indent(X509_ASN_ENCODING
, 0, name
,
1409 CERT_SIMPLE_NAME_STR
, NULL
, 0);
1414 str
= CryptMemAlloc(len
* sizeof(WCHAR
));
1416 cert_name_to_str_with_indent(X509_ASN_ENCODING
, 0, name
,
1417 CERT_SIMPLE_NAME_STR
, str
, len
);
1422 static void dump_alt_name_entry(const CERT_ALT_NAME_ENTRY
*entry
)
1426 switch (entry
->dwAltNameChoice
)
1428 case CERT_ALT_NAME_OTHER_NAME
:
1429 TRACE_(chain
)("CERT_ALT_NAME_OTHER_NAME, oid = %s\n",
1430 debugstr_a(entry
->u
.pOtherName
->pszObjId
));
1432 case CERT_ALT_NAME_RFC822_NAME
:
1433 TRACE_(chain
)("CERT_ALT_NAME_RFC822_NAME: %s\n",
1434 debugstr_w(entry
->u
.pwszRfc822Name
));
1436 case CERT_ALT_NAME_DNS_NAME
:
1437 TRACE_(chain
)("CERT_ALT_NAME_DNS_NAME: %s\n",
1438 debugstr_w(entry
->u
.pwszDNSName
));
1440 case CERT_ALT_NAME_DIRECTORY_NAME
:
1441 str
= name_value_to_str(&entry
->u
.DirectoryName
);
1442 TRACE_(chain
)("CERT_ALT_NAME_DIRECTORY_NAME: %s\n", debugstr_w(str
));
1445 case CERT_ALT_NAME_URL
:
1446 TRACE_(chain
)("CERT_ALT_NAME_URL: %s\n", debugstr_w(entry
->u
.pwszURL
));
1448 case CERT_ALT_NAME_IP_ADDRESS
:
1449 TRACE_(chain
)("CERT_ALT_NAME_IP_ADDRESS: %d bytes\n",
1450 entry
->u
.IPAddress
.cbData
);
1452 case CERT_ALT_NAME_REGISTERED_ID
:
1453 TRACE_(chain
)("CERT_ALT_NAME_REGISTERED_ID: %s\n",
1454 debugstr_a(entry
->u
.pszRegisteredID
));
1457 TRACE_(chain
)("dwAltNameChoice = %d\n", entry
->dwAltNameChoice
);
1461 static void dump_alt_name(LPCSTR type
, const CERT_EXTENSION
*ext
)
1463 CERT_ALT_NAME_INFO
*name
;
1466 TRACE_(chain
)("%s:\n", type
);
1467 if (CryptDecodeObjectEx(X509_ASN_ENCODING
, X509_ALTERNATE_NAME
,
1468 ext
->Value
.pbData
, ext
->Value
.cbData
,
1469 CRYPT_DECODE_ALLOC_FLAG
| CRYPT_DECODE_NOCOPY_FLAG
, NULL
, &name
, &size
))
1473 TRACE_(chain
)("%d alt name entries:\n", name
->cAltEntry
);
1474 for (i
= 0; i
< name
->cAltEntry
; i
++)
1475 dump_alt_name_entry(&name
->rgAltEntry
[i
]);
1480 static void dump_basic_constraints(const CERT_EXTENSION
*ext
)
1482 CERT_BASIC_CONSTRAINTS_INFO
*info
;
1485 if (CryptDecodeObjectEx(X509_ASN_ENCODING
, szOID_BASIC_CONSTRAINTS
,
1486 ext
->Value
.pbData
, ext
->Value
.cbData
, CRYPT_DECODE_ALLOC_FLAG
,
1487 NULL
, &info
, &size
))
1489 TRACE_(chain
)("SubjectType: %02x\n", info
->SubjectType
.pbData
[0]);
1490 TRACE_(chain
)("%s path length constraint\n",
1491 info
->fPathLenConstraint
? "has" : "doesn't have");
1492 TRACE_(chain
)("path length=%d\n", info
->dwPathLenConstraint
);
1497 static void dump_basic_constraints2(const CERT_EXTENSION
*ext
)
1499 CERT_BASIC_CONSTRAINTS2_INFO constraints
;
1500 DWORD size
= sizeof(CERT_BASIC_CONSTRAINTS2_INFO
);
1502 if (CryptDecodeObjectEx(X509_ASN_ENCODING
,
1503 szOID_BASIC_CONSTRAINTS2
, ext
->Value
.pbData
, ext
->Value
.cbData
,
1504 0, NULL
, &constraints
, &size
))
1506 TRACE_(chain
)("basic constraints:\n");
1507 TRACE_(chain
)("can%s be a CA\n", constraints
.fCA
? "" : "not");
1508 TRACE_(chain
)("%s path length constraint\n",
1509 constraints
.fPathLenConstraint
? "has" : "doesn't have");
1510 TRACE_(chain
)("path length=%d\n", constraints
.dwPathLenConstraint
);
1514 static void dump_key_usage(const CERT_EXTENSION
*ext
)
1516 CRYPT_BIT_BLOB usage
;
1517 DWORD size
= sizeof(usage
);
1519 if (CryptDecodeObjectEx(X509_ASN_ENCODING
, X509_BITS
, ext
->Value
.pbData
,
1520 ext
->Value
.cbData
, CRYPT_DECODE_NOCOPY_FLAG
, NULL
, &usage
, &size
))
1522 #define trace_usage_bit(bits, bit) \
1523 if ((bits) & (bit)) TRACE_(chain)("%s\n", #bit)
1526 trace_usage_bit(usage
.pbData
[0], CERT_DIGITAL_SIGNATURE_KEY_USAGE
);
1527 trace_usage_bit(usage
.pbData
[0], CERT_NON_REPUDIATION_KEY_USAGE
);
1528 trace_usage_bit(usage
.pbData
[0], CERT_KEY_ENCIPHERMENT_KEY_USAGE
);
1529 trace_usage_bit(usage
.pbData
[0], CERT_DATA_ENCIPHERMENT_KEY_USAGE
);
1530 trace_usage_bit(usage
.pbData
[0], CERT_KEY_AGREEMENT_KEY_USAGE
);
1531 trace_usage_bit(usage
.pbData
[0], CERT_KEY_CERT_SIGN_KEY_USAGE
);
1532 trace_usage_bit(usage
.pbData
[0], CERT_CRL_SIGN_KEY_USAGE
);
1533 trace_usage_bit(usage
.pbData
[0], CERT_ENCIPHER_ONLY_KEY_USAGE
);
1535 #undef trace_usage_bit
1536 if (usage
.cbData
> 1 && usage
.pbData
[1] & CERT_DECIPHER_ONLY_KEY_USAGE
)
1537 TRACE_(chain
)("CERT_DECIPHER_ONLY_KEY_USAGE\n");
1541 static void dump_general_subtree(const CERT_GENERAL_SUBTREE
*subtree
)
1543 dump_alt_name_entry(&subtree
->Base
);
1544 TRACE_(chain
)("dwMinimum = %d, fMaximum = %d, dwMaximum = %d\n",
1545 subtree
->dwMinimum
, subtree
->fMaximum
, subtree
->dwMaximum
);
1548 static void dump_name_constraints(const CERT_EXTENSION
*ext
)
1550 CERT_NAME_CONSTRAINTS_INFO
*nameConstraints
;
1553 if (CryptDecodeObjectEx(X509_ASN_ENCODING
, X509_NAME_CONSTRAINTS
,
1554 ext
->Value
.pbData
, ext
->Value
.cbData
,
1555 CRYPT_DECODE_ALLOC_FLAG
| CRYPT_DECODE_NOCOPY_FLAG
, NULL
, &nameConstraints
,
1560 TRACE_(chain
)("%d permitted subtrees:\n",
1561 nameConstraints
->cPermittedSubtree
);
1562 for (i
= 0; i
< nameConstraints
->cPermittedSubtree
; i
++)
1563 dump_general_subtree(&nameConstraints
->rgPermittedSubtree
[i
]);
1564 TRACE_(chain
)("%d excluded subtrees:\n",
1565 nameConstraints
->cExcludedSubtree
);
1566 for (i
= 0; i
< nameConstraints
->cExcludedSubtree
; i
++)
1567 dump_general_subtree(&nameConstraints
->rgExcludedSubtree
[i
]);
1568 LocalFree(nameConstraints
);
1572 static void dump_cert_policies(const CERT_EXTENSION
*ext
)
1574 CERT_POLICIES_INFO
*policies
;
1577 if (CryptDecodeObjectEx(X509_ASN_ENCODING
, X509_CERT_POLICIES
,
1578 ext
->Value
.pbData
, ext
->Value
.cbData
, CRYPT_DECODE_ALLOC_FLAG
, NULL
,
1583 TRACE_(chain
)("%d policies:\n", policies
->cPolicyInfo
);
1584 for (i
= 0; i
< policies
->cPolicyInfo
; i
++)
1586 TRACE_(chain
)("policy identifier: %s\n",
1587 debugstr_a(policies
->rgPolicyInfo
[i
].pszPolicyIdentifier
));
1588 TRACE_(chain
)("%d policy qualifiers:\n",
1589 policies
->rgPolicyInfo
[i
].cPolicyQualifier
);
1590 for (j
= 0; j
< policies
->rgPolicyInfo
[i
].cPolicyQualifier
; j
++)
1591 TRACE_(chain
)("%s\n", debugstr_a(
1592 policies
->rgPolicyInfo
[i
].rgPolicyQualifier
[j
].
1593 pszPolicyQualifierId
));
1595 LocalFree(policies
);
1599 static void dump_enhanced_key_usage(const CERT_EXTENSION
*ext
)
1601 CERT_ENHKEY_USAGE
*usage
;
1604 if (CryptDecodeObjectEx(X509_ASN_ENCODING
, X509_ENHANCED_KEY_USAGE
,
1605 ext
->Value
.pbData
, ext
->Value
.cbData
, CRYPT_DECODE_ALLOC_FLAG
, NULL
,
1610 TRACE_(chain
)("%d usages:\n", usage
->cUsageIdentifier
);
1611 for (i
= 0; i
< usage
->cUsageIdentifier
; i
++)
1612 TRACE_(chain
)("%s\n", usage
->rgpszUsageIdentifier
[i
]);
1617 static void dump_netscape_cert_type(const CERT_EXTENSION
*ext
)
1619 CRYPT_BIT_BLOB usage
;
1620 DWORD size
= sizeof(usage
);
1622 if (CryptDecodeObjectEx(X509_ASN_ENCODING
, X509_BITS
, ext
->Value
.pbData
,
1623 ext
->Value
.cbData
, CRYPT_DECODE_NOCOPY_FLAG
, NULL
, &usage
, &size
))
1625 #define trace_cert_type_bit(bits, bit) \
1626 if ((bits) & (bit)) TRACE_(chain)("%s\n", #bit)
1629 trace_cert_type_bit(usage
.pbData
[0],
1630 NETSCAPE_SSL_CLIENT_AUTH_CERT_TYPE
);
1631 trace_cert_type_bit(usage
.pbData
[0],
1632 NETSCAPE_SSL_SERVER_AUTH_CERT_TYPE
);
1633 trace_cert_type_bit(usage
.pbData
[0], NETSCAPE_SMIME_CERT_TYPE
);
1634 trace_cert_type_bit(usage
.pbData
[0], NETSCAPE_SIGN_CERT_TYPE
);
1635 trace_cert_type_bit(usage
.pbData
[0], NETSCAPE_SSL_CA_CERT_TYPE
);
1636 trace_cert_type_bit(usage
.pbData
[0], NETSCAPE_SMIME_CA_CERT_TYPE
);
1637 trace_cert_type_bit(usage
.pbData
[0], NETSCAPE_SIGN_CA_CERT_TYPE
);
1639 #undef trace_cert_type_bit
1643 static void dump_extension(const CERT_EXTENSION
*ext
)
1645 TRACE_(chain
)("%s (%scritical)\n", debugstr_a(ext
->pszObjId
),
1646 ext
->fCritical
? "" : "not ");
1647 if (!strcmp(ext
->pszObjId
, szOID_SUBJECT_ALT_NAME
))
1648 dump_alt_name("subject alt name", ext
);
1649 else if (!strcmp(ext
->pszObjId
, szOID_ISSUER_ALT_NAME
))
1650 dump_alt_name("issuer alt name", ext
);
1651 else if (!strcmp(ext
->pszObjId
, szOID_BASIC_CONSTRAINTS
))
1652 dump_basic_constraints(ext
);
1653 else if (!strcmp(ext
->pszObjId
, szOID_KEY_USAGE
))
1654 dump_key_usage(ext
);
1655 else if (!strcmp(ext
->pszObjId
, szOID_SUBJECT_ALT_NAME2
))
1656 dump_alt_name("subject alt name 2", ext
);
1657 else if (!strcmp(ext
->pszObjId
, szOID_ISSUER_ALT_NAME2
))
1658 dump_alt_name("issuer alt name 2", ext
);
1659 else if (!strcmp(ext
->pszObjId
, szOID_BASIC_CONSTRAINTS2
))
1660 dump_basic_constraints2(ext
);
1661 else if (!strcmp(ext
->pszObjId
, szOID_NAME_CONSTRAINTS
))
1662 dump_name_constraints(ext
);
1663 else if (!strcmp(ext
->pszObjId
, szOID_CERT_POLICIES
))
1664 dump_cert_policies(ext
);
1665 else if (!strcmp(ext
->pszObjId
, szOID_ENHANCED_KEY_USAGE
))
1666 dump_enhanced_key_usage(ext
);
1667 else if (!strcmp(ext
->pszObjId
, szOID_NETSCAPE_CERT_TYPE
))
1668 dump_netscape_cert_type(ext
);
1671 static LPCSTR
filetime_to_str(const FILETIME
*time
)
1674 char dateFmt
[80]; /* sufficient for all versions of LOCALE_SSHORTDATE */
1677 if (!time
) return "(null)";
1679 GetLocaleInfoA(LOCALE_SYSTEM_DEFAULT
, LOCALE_SSHORTDATE
, dateFmt
, ARRAY_SIZE(dateFmt
));
1680 FileTimeToSystemTime(time
, &sysTime
);
1681 GetDateFormatA(LOCALE_SYSTEM_DEFAULT
, 0, &sysTime
, dateFmt
, date
, ARRAY_SIZE(date
));
1682 return wine_dbg_sprintf("%s", date
);
1685 static void dump_element(PCCERT_CONTEXT cert
)
1690 TRACE_(chain
)("%p: version %d\n", cert
, cert
->pCertInfo
->dwVersion
);
1691 len
= CertGetNameStringW(cert
, CERT_NAME_SIMPLE_DISPLAY_TYPE
,
1692 CERT_NAME_ISSUER_FLAG
, NULL
, NULL
, 0);
1693 name
= CryptMemAlloc(len
* sizeof(WCHAR
));
1696 CertGetNameStringW(cert
, CERT_NAME_SIMPLE_DISPLAY_TYPE
,
1697 CERT_NAME_ISSUER_FLAG
, NULL
, name
, len
);
1698 TRACE_(chain
)("issued by %s\n", debugstr_w(name
));
1701 len
= CertGetNameStringW(cert
, CERT_NAME_SIMPLE_DISPLAY_TYPE
, 0, NULL
,
1703 name
= CryptMemAlloc(len
* sizeof(WCHAR
));
1706 CertGetNameStringW(cert
, CERT_NAME_SIMPLE_DISPLAY_TYPE
, 0, NULL
,
1708 TRACE_(chain
)("issued to %s\n", debugstr_w(name
));
1711 TRACE_(chain
)("valid from %s to %s\n",
1712 filetime_to_str(&cert
->pCertInfo
->NotBefore
),
1713 filetime_to_str(&cert
->pCertInfo
->NotAfter
));
1714 TRACE_(chain
)("%d extensions\n", cert
->pCertInfo
->cExtension
);
1715 for (i
= 0; i
< cert
->pCertInfo
->cExtension
; i
++)
1716 dump_extension(&cert
->pCertInfo
->rgExtension
[i
]);
1719 static BOOL
CRYPT_KeyUsageValid(CertificateChainEngine
*engine
,
1720 PCCERT_CONTEXT cert
, BOOL isRoot
, BOOL isCA
, DWORD index
)
1722 PCERT_EXTENSION ext
;
1726 ext
= CertFindExtension(szOID_KEY_USAGE
, cert
->pCertInfo
->cExtension
,
1727 cert
->pCertInfo
->rgExtension
);
1730 CRYPT_BIT_BLOB usage
;
1731 DWORD size
= sizeof(usage
);
1733 ret
= CryptDecodeObjectEx(cert
->dwCertEncodingType
, X509_BITS
,
1734 ext
->Value
.pbData
, ext
->Value
.cbData
, CRYPT_DECODE_NOCOPY_FLAG
, NULL
,
1738 else if (usage
.cbData
> 2)
1740 /* The key usage extension only defines 9 bits => no more than 2
1741 * bytes are needed to encode all known usages.
1747 /* The only bit relevant to chain validation is the keyCertSign
1748 * bit, which is always in the least significant byte of the
1751 usageBits
= usage
.pbData
[usage
.cbData
- 1];
1758 /* MS appears to violate RFC 5280, section 4.2.1.3 (Key Usage)
1759 * here. Quoting the RFC:
1760 * "This [key usage] extension MUST appear in certificates that
1761 * contain public keys that are used to validate digital signatures
1762 * on other public key certificates or CRLs."
1763 * MS appears to accept certs that do not contain key usage
1764 * extensions as CA certs. V1 and V2 certificates did not have
1765 * extensions, and many root certificates are V1 certificates, so
1766 * perhaps this is prudent. On the other hand, MS also accepts V3
1767 * certs without key usage extensions. Because some CAs, e.g.
1768 * Certum, also do not include key usage extensions in their
1769 * intermediate certificates, we are forced to accept V3
1770 * certificates without key usage extensions as well.
1776 if (!(usageBits
& CERT_KEY_CERT_SIGN_KEY_USAGE
))
1778 WARN_(chain
)("keyCertSign not asserted on a CA cert\n");
1787 if (ext
&& (usageBits
& CERT_KEY_CERT_SIGN_KEY_USAGE
))
1789 WARN_(chain
)("keyCertSign asserted on a non-CA cert\n");
1798 static BOOL
CRYPT_CriticalExtensionsSupported(PCCERT_CONTEXT cert
)
1803 for (i
= 0; ret
&& i
< cert
->pCertInfo
->cExtension
; i
++)
1805 if (cert
->pCertInfo
->rgExtension
[i
].fCritical
)
1807 LPCSTR oid
= cert
->pCertInfo
->rgExtension
[i
].pszObjId
;
1809 if (!strcmp(oid
, szOID_BASIC_CONSTRAINTS
))
1811 else if (!strcmp(oid
, szOID_BASIC_CONSTRAINTS2
))
1813 else if (!strcmp(oid
, szOID_NAME_CONSTRAINTS
))
1815 else if (!strcmp(oid
, szOID_KEY_USAGE
))
1817 else if (!strcmp(oid
, szOID_SUBJECT_ALT_NAME
))
1819 else if (!strcmp(oid
, szOID_SUBJECT_ALT_NAME2
))
1821 else if (!strcmp(oid
, szOID_CERT_POLICIES
))
1823 else if (!strcmp(oid
, szOID_ENHANCED_KEY_USAGE
))
1827 FIXME("unsupported critical extension %s\n",
1836 static BOOL
CRYPT_IsCertVersionValid(PCCERT_CONTEXT cert
)
1840 /* Checks whether the contents of the cert match the cert's version. */
1841 switch (cert
->pCertInfo
->dwVersion
)
1844 /* A V1 cert may not contain unique identifiers. See RFC 5280,
1846 * "These fields MUST only appear if the version is 2 or 3 (Section
1847 * 4.1.2.1). These fields MUST NOT appear if the version is 1."
1849 if (cert
->pCertInfo
->IssuerUniqueId
.cbData
||
1850 cert
->pCertInfo
->SubjectUniqueId
.cbData
)
1852 /* A V1 cert may not contain extensions. See RFC 5280, section 4.1.2.9:
1853 * "This field MUST only appear if the version is 3 (Section 4.1.2.1)."
1855 if (cert
->pCertInfo
->cExtension
)
1859 /* A V2 cert may not contain extensions. See RFC 5280, section 4.1.2.9:
1860 * "This field MUST only appear if the version is 3 (Section 4.1.2.1)."
1862 if (cert
->pCertInfo
->cExtension
)
1866 /* Do nothing, all fields are allowed for V3 certs */
1869 WARN_(chain
)("invalid cert version %d\n", cert
->pCertInfo
->dwVersion
);
1875 static void CRYPT_CheckSimpleChain(CertificateChainEngine
*engine
,
1876 PCERT_SIMPLE_CHAIN chain
, LPFILETIME time
)
1878 PCERT_CHAIN_ELEMENT rootElement
= chain
->rgpElement
[chain
->cElement
- 1];
1880 BOOL pathLengthConstraintViolated
= FALSE
;
1881 CERT_BASIC_CONSTRAINTS2_INFO constraints
= { FALSE
, FALSE
, 0 };
1884 TRACE_(chain
)("checking chain with %d elements for time %s\n",
1885 chain
->cElement
, filetime_to_str(time
));
1886 for (i
= chain
->cElement
- 1; i
>= 0; i
--)
1890 if (TRACE_ON(chain
))
1891 dump_element(chain
->rgpElement
[i
]->pCertContext
);
1892 if (i
== chain
->cElement
- 1)
1893 isRoot
= CRYPT_IsCertificateSelfSigned(
1894 chain
->rgpElement
[i
]->pCertContext
);
1897 if (!CRYPT_IsCertVersionValid(chain
->rgpElement
[i
]->pCertContext
))
1899 /* MS appears to accept certs whose versions don't match their
1900 * contents, so there isn't an appropriate error code.
1902 chain
->rgpElement
[i
]->TrustStatus
.dwErrorStatus
|=
1903 CERT_TRUST_INVALID_EXTENSION
;
1905 if (CertVerifyTimeValidity(time
,
1906 chain
->rgpElement
[i
]->pCertContext
->pCertInfo
) != 0)
1907 chain
->rgpElement
[i
]->TrustStatus
.dwErrorStatus
|=
1908 CERT_TRUST_IS_NOT_TIME_VALID
;
1911 /* Check the signature of the cert this issued */
1912 if (!CryptVerifyCertificateSignatureEx(0, X509_ASN_ENCODING
,
1913 CRYPT_VERIFY_CERT_SIGN_SUBJECT_CERT
,
1914 (void *)chain
->rgpElement
[i
- 1]->pCertContext
,
1915 CRYPT_VERIFY_CERT_SIGN_ISSUER_CERT
,
1916 (void *)chain
->rgpElement
[i
]->pCertContext
, 0, NULL
))
1917 chain
->rgpElement
[i
- 1]->TrustStatus
.dwErrorStatus
|=
1918 CERT_TRUST_IS_NOT_SIGNATURE_VALID
;
1919 /* Once a path length constraint has been violated, every remaining
1920 * CA cert's basic constraints is considered invalid.
1922 if (pathLengthConstraintViolated
)
1923 chain
->rgpElement
[i
]->TrustStatus
.dwErrorStatus
|=
1924 CERT_TRUST_INVALID_BASIC_CONSTRAINTS
;
1925 else if (!CRYPT_CheckBasicConstraintsForCA(engine
,
1926 chain
->rgpElement
[i
]->pCertContext
, &constraints
, i
- 1, isRoot
,
1927 &pathLengthConstraintViolated
))
1928 chain
->rgpElement
[i
]->TrustStatus
.dwErrorStatus
|=
1929 CERT_TRUST_INVALID_BASIC_CONSTRAINTS
;
1930 else if (constraints
.fPathLenConstraint
&&
1931 constraints
.dwPathLenConstraint
)
1933 /* This one's valid - decrement max length */
1934 constraints
.dwPathLenConstraint
--;
1939 /* Check whether end cert has a basic constraints extension */
1940 if (!CRYPT_DecodeBasicConstraints(
1941 chain
->rgpElement
[i
]->pCertContext
, &constraints
, FALSE
))
1942 chain
->rgpElement
[i
]->TrustStatus
.dwErrorStatus
|=
1943 CERT_TRUST_INVALID_BASIC_CONSTRAINTS
;
1945 if (!CRYPT_KeyUsageValid(engine
, chain
->rgpElement
[i
]->pCertContext
,
1946 isRoot
, constraints
.fCA
, i
))
1947 chain
->rgpElement
[i
]->TrustStatus
.dwErrorStatus
|=
1948 CERT_TRUST_IS_NOT_VALID_FOR_USAGE
;
1949 if (CRYPT_IsSimpleChainCyclic(chain
))
1951 /* If the chain is cyclic, then the path length constraints
1952 * are violated, because the chain is infinitely long.
1954 pathLengthConstraintViolated
= TRUE
;
1955 chain
->TrustStatus
.dwErrorStatus
|=
1956 CERT_TRUST_IS_PARTIAL_CHAIN
|
1957 CERT_TRUST_INVALID_BASIC_CONSTRAINTS
;
1959 /* Check whether every critical extension is supported */
1960 if (!CRYPT_CriticalExtensionsSupported(
1961 chain
->rgpElement
[i
]->pCertContext
))
1962 chain
->rgpElement
[i
]->TrustStatus
.dwErrorStatus
|=
1963 CERT_TRUST_INVALID_EXTENSION
|
1964 CERT_TRUST_HAS_NOT_SUPPORTED_CRITICAL_EXT
;
1965 CRYPT_CombineTrustStatus(&chain
->TrustStatus
,
1966 &chain
->rgpElement
[i
]->TrustStatus
);
1968 CRYPT_CheckChainNameConstraints(chain
);
1969 CRYPT_CheckChainPolicies(chain
);
1970 if ((status
= CRYPT_IsCertificateSelfSigned(rootElement
->pCertContext
)))
1972 rootElement
->TrustStatus
.dwInfoStatus
|= status
;
1973 CRYPT_CheckRootCert(engine
->hRoot
, rootElement
);
1975 CRYPT_CombineTrustStatus(&chain
->TrustStatus
, &rootElement
->TrustStatus
);
1978 static PCCERT_CONTEXT
CRYPT_FindIssuer(const CertificateChainEngine
*engine
, const CERT_CONTEXT
*cert
,
1979 HCERTSTORE store
, DWORD type
, void *para
, DWORD flags
, PCCERT_CONTEXT prev_issuer
)
1981 CRYPT_URL_ARRAY
*urls
;
1982 PCCERT_CONTEXT issuer
;
1986 issuer
= CertFindCertificateInStore(store
, cert
->dwCertEncodingType
, 0, type
, para
, prev_issuer
);
1988 TRACE("Found in store %p\n", issuer
);
1992 /* FIXME: For alternate issuers, we don't search world store nor try to retrieve issuer from URL.
1993 * This needs more tests.
1998 if(engine
->hWorld
) {
1999 issuer
= CertFindCertificateInStore(engine
->hWorld
, cert
->dwCertEncodingType
, 0, type
, para
, NULL
);
2001 TRACE("Found in world %p\n", issuer
);
2006 res
= CryptGetObjectUrl(URL_OID_CERTIFICATE_ISSUER
, (void*)cert
, 0, NULL
, &size
, NULL
, NULL
, NULL
);
2010 urls
= HeapAlloc(GetProcessHeap(), 0, size
);
2014 res
= CryptGetObjectUrl(URL_OID_CERTIFICATE_ISSUER
, (void*)cert
, 0, urls
, &size
, NULL
, NULL
, NULL
);
2017 CERT_CONTEXT
*new_cert
;
2018 HCERTSTORE new_store
;
2021 for(i
=0; i
< urls
->cUrl
; i
++)
2023 TRACE("Trying URL %s\n", debugstr_w(urls
->rgwszUrl
[i
]));
2025 res
= CryptRetrieveObjectByUrlW(urls
->rgwszUrl
[i
], CONTEXT_OID_CERTIFICATE
,
2026 (flags
& CERT_CHAIN_CACHE_ONLY_URL_RETRIEVAL
) ? CRYPT_CACHE_ONLY_RETRIEVAL
: CRYPT_AIA_RETRIEVAL
,
2027 0, (void**)&new_cert
, NULL
, NULL
, NULL
, NULL
);
2030 TRACE("CryptRetrieveObjectByUrlW failed: %u\n", GetLastError());
2034 /* FIXME: Use new_cert->hCertStore once cert ref count bug is fixed. */
2035 new_store
= CertOpenStore(CERT_STORE_PROV_MEMORY
, 0, 0, CERT_STORE_CREATE_NEW_FLAG
, NULL
);
2036 CertAddCertificateContextToStore(new_store
, new_cert
, CERT_STORE_ADD_NEW
, NULL
);
2037 issuer
= CertFindCertificateInStore(new_store
, cert
->dwCertEncodingType
, 0, type
, para
, NULL
);
2038 CertFreeCertificateContext(new_cert
);
2039 CertCloseStore(new_store
, 0);
2042 TRACE("Found downloaded issuer %p\n", issuer
);
2048 HeapFree(GetProcessHeap(), 0, urls
);
2052 static PCCERT_CONTEXT
CRYPT_GetIssuer(const CertificateChainEngine
*engine
,
2053 HCERTSTORE store
, PCCERT_CONTEXT subject
, PCCERT_CONTEXT prevIssuer
,
2054 DWORD flags
, DWORD
*infoStatus
)
2056 PCCERT_CONTEXT issuer
= NULL
;
2057 PCERT_EXTENSION ext
;
2061 if ((ext
= CertFindExtension(szOID_AUTHORITY_KEY_IDENTIFIER
,
2062 subject
->pCertInfo
->cExtension
, subject
->pCertInfo
->rgExtension
)))
2064 CERT_AUTHORITY_KEY_ID_INFO
*info
;
2067 ret
= CryptDecodeObjectEx(subject
->dwCertEncodingType
,
2068 X509_AUTHORITY_KEY_ID
, ext
->Value
.pbData
, ext
->Value
.cbData
,
2069 CRYPT_DECODE_ALLOC_FLAG
| CRYPT_DECODE_NOCOPY_FLAG
, NULL
,
2075 if (info
->CertIssuer
.cbData
&& info
->CertSerialNumber
.cbData
)
2077 id
.dwIdChoice
= CERT_ID_ISSUER_SERIAL_NUMBER
;
2078 memcpy(&id
.u
.IssuerSerialNumber
.Issuer
, &info
->CertIssuer
,
2079 sizeof(CERT_NAME_BLOB
));
2080 memcpy(&id
.u
.IssuerSerialNumber
.SerialNumber
,
2081 &info
->CertSerialNumber
, sizeof(CRYPT_INTEGER_BLOB
));
2083 issuer
= CRYPT_FindIssuer(engine
, subject
, store
, CERT_FIND_CERT_ID
, &id
, flags
, prevIssuer
);
2086 TRACE_(chain
)("issuer found by issuer/serial number\n");
2087 *infoStatus
= CERT_TRUST_HAS_EXACT_MATCH_ISSUER
;
2090 else if (info
->KeyId
.cbData
)
2092 id
.dwIdChoice
= CERT_ID_KEY_IDENTIFIER
;
2094 memcpy(&id
.u
.KeyId
, &info
->KeyId
, sizeof(CRYPT_HASH_BLOB
));
2095 issuer
= CRYPT_FindIssuer(engine
, subject
, store
, CERT_FIND_CERT_ID
, &id
, flags
, prevIssuer
);
2098 TRACE_(chain
)("issuer found by key id\n");
2099 *infoStatus
= CERT_TRUST_HAS_KEY_MATCH_ISSUER
;
2105 else if ((ext
= CertFindExtension(szOID_AUTHORITY_KEY_IDENTIFIER2
,
2106 subject
->pCertInfo
->cExtension
, subject
->pCertInfo
->rgExtension
)))
2108 CERT_AUTHORITY_KEY_ID2_INFO
*info
;
2111 ret
= CryptDecodeObjectEx(subject
->dwCertEncodingType
,
2112 X509_AUTHORITY_KEY_ID2
, ext
->Value
.pbData
, ext
->Value
.cbData
,
2113 CRYPT_DECODE_ALLOC_FLAG
| CRYPT_DECODE_NOCOPY_FLAG
, NULL
,
2119 if (info
->AuthorityCertIssuer
.cAltEntry
&&
2120 info
->AuthorityCertSerialNumber
.cbData
)
2122 PCERT_ALT_NAME_ENTRY directoryName
= NULL
;
2125 for (i
= 0; !directoryName
&&
2126 i
< info
->AuthorityCertIssuer
.cAltEntry
; i
++)
2127 if (info
->AuthorityCertIssuer
.rgAltEntry
[i
].dwAltNameChoice
2128 == CERT_ALT_NAME_DIRECTORY_NAME
)
2130 &info
->AuthorityCertIssuer
.rgAltEntry
[i
];
2133 id
.dwIdChoice
= CERT_ID_ISSUER_SERIAL_NUMBER
;
2134 memcpy(&id
.u
.IssuerSerialNumber
.Issuer
,
2135 &directoryName
->u
.DirectoryName
, sizeof(CERT_NAME_BLOB
));
2136 memcpy(&id
.u
.IssuerSerialNumber
.SerialNumber
,
2137 &info
->AuthorityCertSerialNumber
,
2138 sizeof(CRYPT_INTEGER_BLOB
));
2140 issuer
= CRYPT_FindIssuer(engine
, subject
, store
, CERT_FIND_CERT_ID
, &id
, flags
, prevIssuer
);
2143 TRACE_(chain
)("issuer found by directory name\n");
2144 *infoStatus
= CERT_TRUST_HAS_EXACT_MATCH_ISSUER
;
2148 FIXME("no supported name type in authority key id2\n");
2150 else if (info
->KeyId
.cbData
)
2152 id
.dwIdChoice
= CERT_ID_KEY_IDENTIFIER
;
2153 memcpy(&id
.u
.KeyId
, &info
->KeyId
, sizeof(CRYPT_HASH_BLOB
));
2154 issuer
= CRYPT_FindIssuer(engine
, subject
, store
, CERT_FIND_CERT_ID
, &id
, flags
, prevIssuer
);
2157 TRACE_(chain
)("issuer found by key id\n");
2158 *infoStatus
= CERT_TRUST_HAS_KEY_MATCH_ISSUER
;
2166 issuer
= CRYPT_FindIssuer(engine
, subject
, store
, CERT_FIND_SUBJECT_NAME
,
2167 &subject
->pCertInfo
->Issuer
, flags
, prevIssuer
);
2168 TRACE_(chain
)("issuer found by name\n");
2169 *infoStatus
= CERT_TRUST_HAS_NAME_MATCH_ISSUER
;
2174 /* Builds a simple chain by finding an issuer for the last cert in the chain,
2175 * until reaching a self-signed cert, or until no issuer can be found.
2177 static BOOL
CRYPT_BuildSimpleChain(const CertificateChainEngine
*engine
,
2178 HCERTSTORE world
, DWORD flags
, PCERT_SIMPLE_CHAIN chain
)
2181 PCCERT_CONTEXT cert
= chain
->rgpElement
[chain
->cElement
- 1]->pCertContext
;
2183 while (ret
&& !CRYPT_IsSimpleChainCyclic(chain
) &&
2184 !CRYPT_IsCertificateSelfSigned(cert
))
2186 PCCERT_CONTEXT issuer
= CRYPT_GetIssuer(engine
, world
, cert
, NULL
, flags
,
2187 &chain
->rgpElement
[chain
->cElement
- 1]->TrustStatus
.dwInfoStatus
);
2191 ret
= CRYPT_AddCertToSimpleChain(engine
, chain
, issuer
,
2192 chain
->rgpElement
[chain
->cElement
- 1]->TrustStatus
.dwInfoStatus
);
2193 /* CRYPT_AddCertToSimpleChain add-ref's the issuer, so free it to
2194 * close the enumeration that found it
2196 CertFreeCertificateContext(issuer
);
2201 TRACE_(chain
)("Couldn't find issuer, halting chain creation\n");
2202 chain
->TrustStatus
.dwErrorStatus
|= CERT_TRUST_IS_PARTIAL_CHAIN
;
2209 static LPCSTR
debugstr_filetime(LPFILETIME pTime
)
2213 return wine_dbg_sprintf("%p (%s)", pTime
, filetime_to_str(pTime
));
2216 static BOOL
CRYPT_GetSimpleChainForCert(CertificateChainEngine
*engine
,
2217 HCERTSTORE world
, PCCERT_CONTEXT cert
, LPFILETIME pTime
, DWORD flags
,
2218 PCERT_SIMPLE_CHAIN
*ppChain
)
2221 PCERT_SIMPLE_CHAIN chain
;
2223 TRACE("(%p, %p, %p, %s)\n", engine
, world
, cert
, debugstr_filetime(pTime
));
2225 chain
= CryptMemAlloc(sizeof(CERT_SIMPLE_CHAIN
));
2228 memset(chain
, 0, sizeof(CERT_SIMPLE_CHAIN
));
2229 chain
->cbSize
= sizeof(CERT_SIMPLE_CHAIN
);
2230 ret
= CRYPT_AddCertToSimpleChain(engine
, chain
, cert
, 0);
2233 ret
= CRYPT_BuildSimpleChain(engine
, world
, flags
, chain
);
2235 CRYPT_CheckSimpleChain(engine
, chain
, pTime
);
2239 CRYPT_FreeSimpleChain(chain
);
2247 static BOOL
CRYPT_BuildCandidateChainFromCert(CertificateChainEngine
*engine
,
2248 PCCERT_CONTEXT cert
, LPFILETIME pTime
, HCERTSTORE hAdditionalStore
, DWORD flags
,
2249 CertificateChain
**ppChain
)
2251 PCERT_SIMPLE_CHAIN simpleChain
= NULL
;
2255 world
= CertOpenStore(CERT_STORE_PROV_COLLECTION
, 0, 0,
2256 CERT_STORE_CREATE_NEW_FLAG
, NULL
);
2257 CertAddStoreToCollection(world
, engine
->hWorld
, 0, 0);
2258 if (hAdditionalStore
)
2259 CertAddStoreToCollection(world
, hAdditionalStore
, 0, 0);
2260 /* FIXME: only simple chains are supported for now, as CTLs aren't
2263 if ((ret
= CRYPT_GetSimpleChainForCert(engine
, world
, cert
, pTime
, flags
, &simpleChain
)))
2265 CertificateChain
*chain
= CryptMemAlloc(sizeof(CertificateChain
));
2270 chain
->world
= world
;
2271 chain
->context
.cbSize
= sizeof(CERT_CHAIN_CONTEXT
);
2272 chain
->context
.TrustStatus
= simpleChain
->TrustStatus
;
2273 chain
->context
.cChain
= 1;
2274 chain
->context
.rgpChain
= CryptMemAlloc(sizeof(PCERT_SIMPLE_CHAIN
));
2275 chain
->context
.rgpChain
[0] = simpleChain
;
2276 chain
->context
.cLowerQualityChainContext
= 0;
2277 chain
->context
.rgpLowerQualityChainContext
= NULL
;
2278 chain
->context
.fHasRevocationFreshnessTime
= FALSE
;
2279 chain
->context
.dwRevocationFreshnessTime
= 0;
2283 CRYPT_FreeSimpleChain(simpleChain
);
2291 /* Makes and returns a copy of chain, up to and including element iElement. */
2292 static PCERT_SIMPLE_CHAIN
CRYPT_CopySimpleChainToElement(
2293 const CERT_SIMPLE_CHAIN
*chain
, DWORD iElement
)
2295 PCERT_SIMPLE_CHAIN copy
= CryptMemAlloc(sizeof(CERT_SIMPLE_CHAIN
));
2299 memset(copy
, 0, sizeof(CERT_SIMPLE_CHAIN
));
2300 copy
->cbSize
= sizeof(CERT_SIMPLE_CHAIN
);
2302 CryptMemAlloc((iElement
+ 1) * sizeof(PCERT_CHAIN_ELEMENT
));
2303 if (copy
->rgpElement
)
2308 memset(copy
->rgpElement
, 0,
2309 (iElement
+ 1) * sizeof(PCERT_CHAIN_ELEMENT
));
2310 for (i
= 0; ret
&& i
<= iElement
; i
++)
2312 PCERT_CHAIN_ELEMENT element
=
2313 CryptMemAlloc(sizeof(CERT_CHAIN_ELEMENT
));
2317 *element
= *chain
->rgpElement
[i
];
2318 element
->pCertContext
= CertDuplicateCertificateContext(
2319 chain
->rgpElement
[i
]->pCertContext
);
2320 /* Reset the trust status of the copied element, it'll get
2321 * rechecked after the new chain is done.
2323 memset(&element
->TrustStatus
, 0, sizeof(CERT_TRUST_STATUS
));
2324 copy
->rgpElement
[copy
->cElement
++] = element
;
2331 for (i
= 0; i
<= iElement
; i
++)
2332 CryptMemFree(copy
->rgpElement
[i
]);
2333 CryptMemFree(copy
->rgpElement
);
2347 static void CRYPT_FreeLowerQualityChains(CertificateChain
*chain
)
2351 for (i
= 0; i
< chain
->context
.cLowerQualityChainContext
; i
++)
2352 CertFreeCertificateChain(chain
->context
.rgpLowerQualityChainContext
[i
]);
2353 CryptMemFree(chain
->context
.rgpLowerQualityChainContext
);
2354 chain
->context
.cLowerQualityChainContext
= 0;
2355 chain
->context
.rgpLowerQualityChainContext
= NULL
;
2358 static void CRYPT_FreeChainContext(CertificateChain
*chain
)
2362 CRYPT_FreeLowerQualityChains(chain
);
2363 for (i
= 0; i
< chain
->context
.cChain
; i
++)
2364 CRYPT_FreeSimpleChain(chain
->context
.rgpChain
[i
]);
2365 CryptMemFree(chain
->context
.rgpChain
);
2366 CertCloseStore(chain
->world
, 0);
2367 CryptMemFree(chain
);
2370 /* Makes and returns a copy of chain, up to and including element iElement of
2371 * simple chain iChain.
2373 static CertificateChain
*CRYPT_CopyChainToElement(CertificateChain
*chain
,
2374 DWORD iChain
, DWORD iElement
)
2376 CertificateChain
*copy
= CryptMemAlloc(sizeof(CertificateChain
));
2381 copy
->world
= CertDuplicateStore(chain
->world
);
2382 copy
->context
.cbSize
= sizeof(CERT_CHAIN_CONTEXT
);
2383 /* Leave the trust status of the copied chain unset, it'll get
2384 * rechecked after the new chain is done.
2386 memset(©
->context
.TrustStatus
, 0, sizeof(CERT_TRUST_STATUS
));
2387 copy
->context
.cLowerQualityChainContext
= 0;
2388 copy
->context
.rgpLowerQualityChainContext
= NULL
;
2389 copy
->context
.fHasRevocationFreshnessTime
= FALSE
;
2390 copy
->context
.dwRevocationFreshnessTime
= 0;
2391 copy
->context
.rgpChain
= CryptMemAlloc(
2392 (iChain
+ 1) * sizeof(PCERT_SIMPLE_CHAIN
));
2393 if (copy
->context
.rgpChain
)
2398 memset(copy
->context
.rgpChain
, 0,
2399 (iChain
+ 1) * sizeof(PCERT_SIMPLE_CHAIN
));
2402 for (i
= 0; ret
&& iChain
&& i
< iChain
- 1; i
++)
2404 copy
->context
.rgpChain
[i
] =
2405 CRYPT_CopySimpleChainToElement(chain
->context
.rgpChain
[i
],
2406 chain
->context
.rgpChain
[i
]->cElement
- 1);
2407 if (!copy
->context
.rgpChain
[i
])
2415 copy
->context
.rgpChain
[i
] =
2416 CRYPT_CopySimpleChainToElement(chain
->context
.rgpChain
[i
],
2418 if (!copy
->context
.rgpChain
[i
])
2423 CRYPT_FreeChainContext(copy
);
2427 copy
->context
.cChain
= iChain
+ 1;
2438 static CertificateChain
*CRYPT_BuildAlternateContextFromChain(
2439 CertificateChainEngine
*engine
, LPFILETIME pTime
, HCERTSTORE hAdditionalStore
,
2440 DWORD flags
, CertificateChain
*chain
)
2442 CertificateChain
*alternate
;
2444 TRACE("(%p, %s, %p, %p)\n", engine
, debugstr_filetime(pTime
),
2445 hAdditionalStore
, chain
);
2447 /* Always start with the last "lower quality" chain to ensure a consistent
2448 * order of alternate creation:
2450 if (chain
->context
.cLowerQualityChainContext
)
2451 chain
= (CertificateChain
*)chain
->context
.rgpLowerQualityChainContext
[
2452 chain
->context
.cLowerQualityChainContext
- 1];
2453 /* A chain with only one element can't have any alternates */
2454 if (chain
->context
.cChain
<= 1 && chain
->context
.rgpChain
[0]->cElement
<= 1)
2458 DWORD i
, j
, infoStatus
;
2459 PCCERT_CONTEXT alternateIssuer
= NULL
;
2462 for (i
= 0; !alternateIssuer
&& i
< chain
->context
.cChain
; i
++)
2463 for (j
= 0; !alternateIssuer
&&
2464 j
< chain
->context
.rgpChain
[i
]->cElement
- 1; j
++)
2466 PCCERT_CONTEXT subject
=
2467 chain
->context
.rgpChain
[i
]->rgpElement
[j
]->pCertContext
;
2468 PCCERT_CONTEXT prevIssuer
= CertDuplicateCertificateContext(
2469 chain
->context
.rgpChain
[i
]->rgpElement
[j
+ 1]->pCertContext
);
2471 alternateIssuer
= CRYPT_GetIssuer(engine
, prevIssuer
->hCertStore
,
2472 subject
, prevIssuer
, flags
, &infoStatus
);
2474 if (alternateIssuer
)
2478 alternate
= CRYPT_CopyChainToElement(chain
, i
, j
);
2481 BOOL ret
= CRYPT_AddCertToSimpleChain(engine
,
2482 alternate
->context
.rgpChain
[i
], alternateIssuer
, infoStatus
);
2484 /* CRYPT_AddCertToSimpleChain add-ref's the issuer, so free it
2485 * to close the enumeration that found it
2487 CertFreeCertificateContext(alternateIssuer
);
2490 ret
= CRYPT_BuildSimpleChain(engine
, alternate
->world
,
2491 flags
, alternate
->context
.rgpChain
[i
]);
2493 CRYPT_CheckSimpleChain(engine
,
2494 alternate
->context
.rgpChain
[i
], pTime
);
2495 CRYPT_CombineTrustStatus(&alternate
->context
.TrustStatus
,
2496 &alternate
->context
.rgpChain
[i
]->TrustStatus
);
2500 CRYPT_FreeChainContext(alternate
);
2506 TRACE("%p\n", alternate
);
2510 #define CHAIN_QUALITY_SIGNATURE_VALID 0x16
2511 #define CHAIN_QUALITY_TIME_VALID 8
2512 #define CHAIN_QUALITY_COMPLETE_CHAIN 4
2513 #define CHAIN_QUALITY_BASIC_CONSTRAINTS 2
2514 #define CHAIN_QUALITY_TRUSTED_ROOT 1
2516 #define CHAIN_QUALITY_HIGHEST \
2517 CHAIN_QUALITY_SIGNATURE_VALID | CHAIN_QUALITY_TIME_VALID | \
2518 CHAIN_QUALITY_COMPLETE_CHAIN | CHAIN_QUALITY_BASIC_CONSTRAINTS | \
2519 CHAIN_QUALITY_TRUSTED_ROOT
2521 #define IS_TRUST_ERROR_SET(TrustStatus, bits) \
2522 (TrustStatus)->dwErrorStatus & (bits)
2524 static DWORD
CRYPT_ChainQuality(const CertificateChain
*chain
)
2526 DWORD quality
= CHAIN_QUALITY_HIGHEST
;
2528 if (IS_TRUST_ERROR_SET(&chain
->context
.TrustStatus
,
2529 CERT_TRUST_IS_UNTRUSTED_ROOT
))
2530 quality
&= ~CHAIN_QUALITY_TRUSTED_ROOT
;
2531 if (IS_TRUST_ERROR_SET(&chain
->context
.TrustStatus
,
2532 CERT_TRUST_INVALID_BASIC_CONSTRAINTS
))
2533 quality
&= ~CHAIN_QUALITY_BASIC_CONSTRAINTS
;
2534 if (IS_TRUST_ERROR_SET(&chain
->context
.TrustStatus
,
2535 CERT_TRUST_IS_PARTIAL_CHAIN
))
2536 quality
&= ~CHAIN_QUALITY_COMPLETE_CHAIN
;
2537 if (IS_TRUST_ERROR_SET(&chain
->context
.TrustStatus
,
2538 CERT_TRUST_IS_NOT_TIME_VALID
| CERT_TRUST_IS_NOT_TIME_NESTED
))
2539 quality
&= ~CHAIN_QUALITY_TIME_VALID
;
2540 if (IS_TRUST_ERROR_SET(&chain
->context
.TrustStatus
,
2541 CERT_TRUST_IS_NOT_SIGNATURE_VALID
))
2542 quality
&= ~CHAIN_QUALITY_SIGNATURE_VALID
;
2546 /* Chooses the highest quality chain among chain and its "lower quality"
2547 * alternate chains. Returns the highest quality chain, with all other
2548 * chains as lower quality chains of it.
2550 static CertificateChain
*CRYPT_ChooseHighestQualityChain(
2551 CertificateChain
*chain
)
2555 /* There are always only two chains being considered: chain, and an
2556 * alternate at chain->rgpLowerQualityChainContext[i]. If the alternate
2557 * has a higher quality than chain, the alternate gets assigned the lower
2558 * quality contexts, with chain taking the alternate's place among the
2559 * lower quality contexts.
2561 for (i
= 0; i
< chain
->context
.cLowerQualityChainContext
; i
++)
2563 CertificateChain
*alternate
=
2564 (CertificateChain
*)chain
->context
.rgpLowerQualityChainContext
[i
];
2566 if (CRYPT_ChainQuality(alternate
) > CRYPT_ChainQuality(chain
))
2568 alternate
->context
.cLowerQualityChainContext
=
2569 chain
->context
.cLowerQualityChainContext
;
2570 alternate
->context
.rgpLowerQualityChainContext
=
2571 chain
->context
.rgpLowerQualityChainContext
;
2572 alternate
->context
.rgpLowerQualityChainContext
[i
] =
2573 (PCCERT_CHAIN_CONTEXT
)chain
;
2574 chain
->context
.cLowerQualityChainContext
= 0;
2575 chain
->context
.rgpLowerQualityChainContext
= NULL
;
2582 static BOOL
CRYPT_AddAlternateChainToChain(CertificateChain
*chain
,
2583 const CertificateChain
*alternate
)
2587 if (chain
->context
.cLowerQualityChainContext
)
2588 chain
->context
.rgpLowerQualityChainContext
=
2589 CryptMemRealloc(chain
->context
.rgpLowerQualityChainContext
,
2590 (chain
->context
.cLowerQualityChainContext
+ 1) *
2591 sizeof(PCCERT_CHAIN_CONTEXT
));
2593 chain
->context
.rgpLowerQualityChainContext
=
2594 CryptMemAlloc(sizeof(PCCERT_CHAIN_CONTEXT
));
2595 if (chain
->context
.rgpLowerQualityChainContext
)
2597 chain
->context
.rgpLowerQualityChainContext
[
2598 chain
->context
.cLowerQualityChainContext
++] =
2599 (PCCERT_CHAIN_CONTEXT
)alternate
;
2607 static PCERT_CHAIN_ELEMENT
CRYPT_FindIthElementInChain(
2608 const CERT_CHAIN_CONTEXT
*chain
, DWORD i
)
2611 PCERT_CHAIN_ELEMENT element
= NULL
;
2613 for (j
= 0, iElement
= 0; !element
&& j
< chain
->cChain
; j
++)
2615 if (iElement
+ chain
->rgpChain
[j
]->cElement
< i
)
2616 iElement
+= chain
->rgpChain
[j
]->cElement
;
2618 element
= chain
->rgpChain
[j
]->rgpElement
[i
- iElement
];
2623 typedef struct _CERT_CHAIN_PARA_NO_EXTRA_FIELDS
{
2625 CERT_USAGE_MATCH RequestedUsage
;
2626 } CERT_CHAIN_PARA_NO_EXTRA_FIELDS
;
2628 static void CRYPT_VerifyChainRevocation(PCERT_CHAIN_CONTEXT chain
,
2629 LPFILETIME pTime
, HCERTSTORE hAdditionalStore
,
2630 const CERT_CHAIN_PARA
*pChainPara
, DWORD chainFlags
)
2634 if (chainFlags
& CERT_CHAIN_REVOCATION_CHECK_END_CERT
)
2636 else if ((chainFlags
& CERT_CHAIN_REVOCATION_CHECK_CHAIN
) ||
2637 (chainFlags
& CERT_CHAIN_REVOCATION_CHECK_CHAIN_EXCLUDE_ROOT
))
2641 for (i
= 0, cContext
= 0; i
< chain
->cChain
; i
++)
2643 if (i
< chain
->cChain
- 1 ||
2644 chainFlags
& CERT_CHAIN_REVOCATION_CHECK_CHAIN
)
2645 cContext
+= chain
->rgpChain
[i
]->cElement
;
2647 cContext
+= chain
->rgpChain
[i
]->cElement
- 1;
2654 DWORD i
, j
, iContext
, revocationFlags
;
2655 CERT_REVOCATION_PARA revocationPara
= { sizeof(revocationPara
), 0 };
2656 CERT_REVOCATION_STATUS revocationStatus
=
2657 { sizeof(revocationStatus
), 0 };
2660 revocationFlags
= CERT_VERIFY_REV_CHAIN_FLAG
;
2661 if (chainFlags
& CERT_CHAIN_REVOCATION_CHECK_CACHE_ONLY
)
2662 revocationFlags
|= CERT_VERIFY_CACHE_ONLY_BASED_REVOCATION
;
2663 if (chainFlags
& CERT_CHAIN_REVOCATION_ACCUMULATIVE_TIMEOUT
)
2664 revocationFlags
|= CERT_VERIFY_REV_ACCUMULATIVE_TIMEOUT_FLAG
;
2665 revocationPara
.pftTimeToUse
= pTime
;
2666 if (hAdditionalStore
)
2668 revocationPara
.cCertStore
= 1;
2669 revocationPara
.rgCertStore
= &hAdditionalStore
;
2670 revocationPara
.hCrlStore
= hAdditionalStore
;
2672 if (pChainPara
->cbSize
== sizeof(CERT_CHAIN_PARA
))
2674 revocationPara
.dwUrlRetrievalTimeout
=
2675 pChainPara
->dwUrlRetrievalTimeout
;
2676 revocationPara
.fCheckFreshnessTime
=
2677 pChainPara
->fCheckRevocationFreshnessTime
;
2678 revocationPara
.dwFreshnessTime
=
2679 pChainPara
->dwRevocationFreshnessTime
;
2681 for (i
= 0, iContext
= 0; iContext
< cContext
&& i
< chain
->cChain
; i
++)
2683 for (j
= 0; iContext
< cContext
&&
2684 j
< chain
->rgpChain
[i
]->cElement
; j
++, iContext
++)
2686 PCCERT_CONTEXT certToCheck
=
2687 chain
->rgpChain
[i
]->rgpElement
[j
]->pCertContext
;
2689 if (j
< chain
->rgpChain
[i
]->cElement
- 1)
2690 revocationPara
.pIssuerCert
=
2691 chain
->rgpChain
[i
]->rgpElement
[j
+ 1]->pCertContext
;
2693 revocationPara
.pIssuerCert
= NULL
;
2694 ret
= CertVerifyRevocation(X509_ASN_ENCODING
,
2695 CERT_CONTEXT_REVOCATION_TYPE
, 1, (void **)&certToCheck
,
2696 revocationFlags
, &revocationPara
, &revocationStatus
);
2698 if (!ret
&& chainFlags
& CERT_CHAIN_REVOCATION_CHECK_CHAIN
2699 && revocationStatus
.dwError
== CRYPT_E_NO_REVOCATION_CHECK
&& revocationPara
.pIssuerCert
== NULL
)
2704 PCERT_CHAIN_ELEMENT element
= CRYPT_FindIthElementInChain(
2708 switch (revocationStatus
.dwError
)
2710 case CRYPT_E_NO_REVOCATION_CHECK
:
2711 case CRYPT_E_NO_REVOCATION_DLL
:
2712 case CRYPT_E_NOT_IN_REVOCATION_DATABASE
:
2713 /* If the revocation status is unknown, it's assumed
2714 * to be offline too.
2716 error
= CERT_TRUST_REVOCATION_STATUS_UNKNOWN
|
2717 CERT_TRUST_IS_OFFLINE_REVOCATION
;
2719 case CRYPT_E_REVOCATION_OFFLINE
:
2720 error
= CERT_TRUST_IS_OFFLINE_REVOCATION
;
2722 case CRYPT_E_REVOKED
:
2723 error
= CERT_TRUST_IS_REVOKED
;
2726 WARN("unmapped error %08x\n", revocationStatus
.dwError
);
2731 /* FIXME: set element's pRevocationInfo member */
2732 element
->TrustStatus
.dwErrorStatus
|= error
;
2734 chain
->TrustStatus
.dwErrorStatus
|= error
;
2741 static void CRYPT_CheckUsages(PCERT_CHAIN_CONTEXT chain
,
2742 const CERT_CHAIN_PARA
*pChainPara
)
2744 if (pChainPara
->cbSize
>= sizeof(CERT_CHAIN_PARA_NO_EXTRA_FIELDS
) &&
2745 pChainPara
->RequestedUsage
.Usage
.cUsageIdentifier
)
2747 PCCERT_CONTEXT endCert
;
2748 PCERT_EXTENSION ext
;
2751 /* A chain, if created, always includes the end certificate */
2752 endCert
= chain
->rgpChain
[0]->rgpElement
[0]->pCertContext
;
2753 /* The extended key usage extension specifies how a certificate's
2754 * public key may be used. From RFC 5280, section 4.2.1.12:
2755 * "This extension indicates one or more purposes for which the
2756 * certified public key may be used, in addition to or in place of the
2757 * basic purposes indicated in the key usage extension."
2758 * If the extension is present, it only satisfies the requested usage
2759 * if that usage is included in the extension:
2760 * "If the extension is present, then the certificate MUST only be used
2761 * for one of the purposes indicated."
2762 * There is also the special anyExtendedKeyUsage OID, but it doesn't
2763 * have to be respected:
2764 * "Applications that require the presence of a particular purpose
2765 * MAY reject certificates that include the anyExtendedKeyUsage OID
2766 * but not the particular OID expected for the application."
2767 * For now, I'm being more conservative and ignoring the presence of
2768 * the anyExtendedKeyUsage OID.
2770 if ((ext
= CertFindExtension(szOID_ENHANCED_KEY_USAGE
,
2771 endCert
->pCertInfo
->cExtension
, endCert
->pCertInfo
->rgExtension
)))
2773 const CERT_ENHKEY_USAGE
*requestedUsage
=
2774 &pChainPara
->RequestedUsage
.Usage
;
2775 CERT_ENHKEY_USAGE
*usage
;
2778 if (CryptDecodeObjectEx(X509_ASN_ENCODING
,
2779 X509_ENHANCED_KEY_USAGE
, ext
->Value
.pbData
, ext
->Value
.cbData
,
2780 CRYPT_DECODE_ALLOC_FLAG
, NULL
, &usage
, &size
))
2782 if (pChainPara
->RequestedUsage
.dwType
== USAGE_MATCH_TYPE_AND
)
2786 /* For AND matches, all usages must be present */
2787 validForUsage
= TRUE
;
2788 for (i
= 0; validForUsage
&&
2789 i
< requestedUsage
->cUsageIdentifier
; i
++)
2793 for (j
= 0; !match
&& j
< usage
->cUsageIdentifier
; j
++)
2794 match
= !strcmp(usage
->rgpszUsageIdentifier
[j
],
2795 requestedUsage
->rgpszUsageIdentifier
[i
]);
2797 validForUsage
= FALSE
;
2804 /* For OR matches, any matching usage suffices */
2805 validForUsage
= FALSE
;
2806 for (i
= 0; !validForUsage
&&
2807 i
< requestedUsage
->cUsageIdentifier
; i
++)
2809 for (j
= 0; !validForUsage
&&
2810 j
< usage
->cUsageIdentifier
; j
++)
2812 !strcmp(usage
->rgpszUsageIdentifier
[j
],
2813 requestedUsage
->rgpszUsageIdentifier
[i
]);
2819 validForUsage
= FALSE
;
2823 /* If the extension isn't present, any interpretation is valid:
2824 * "Certificate using applications MAY require that the extended
2825 * key usage extension be present and that a particular purpose
2826 * be indicated in order for the certificate to be acceptable to
2827 * that application."
2828 * Not all web sites include the extended key usage extension, so
2829 * accept chains without it.
2831 TRACE_(chain
)("requested usage from certificate with no usages\n");
2832 validForUsage
= TRUE
;
2836 chain
->TrustStatus
.dwErrorStatus
|=
2837 CERT_TRUST_IS_NOT_VALID_FOR_USAGE
;
2838 chain
->rgpChain
[0]->rgpElement
[0]->TrustStatus
.dwErrorStatus
|=
2839 CERT_TRUST_IS_NOT_VALID_FOR_USAGE
;
2842 if (pChainPara
->cbSize
>= sizeof(CERT_CHAIN_PARA
) &&
2843 pChainPara
->RequestedIssuancePolicy
.Usage
.cUsageIdentifier
)
2844 FIXME("unimplemented for RequestedIssuancePolicy\n");
2847 static void dump_usage_match(LPCSTR name
, const CERT_USAGE_MATCH
*usageMatch
)
2849 if (usageMatch
->Usage
.cUsageIdentifier
)
2853 TRACE_(chain
)("%s: %s\n", name
,
2854 usageMatch
->dwType
== USAGE_MATCH_TYPE_AND
? "AND" : "OR");
2855 for (i
= 0; i
< usageMatch
->Usage
.cUsageIdentifier
; i
++)
2856 TRACE_(chain
)("%s\n", usageMatch
->Usage
.rgpszUsageIdentifier
[i
]);
2860 static void dump_chain_para(const CERT_CHAIN_PARA
*pChainPara
)
2862 TRACE_(chain
)("%d\n", pChainPara
->cbSize
);
2863 if (pChainPara
->cbSize
>= sizeof(CERT_CHAIN_PARA_NO_EXTRA_FIELDS
))
2864 dump_usage_match("RequestedUsage", &pChainPara
->RequestedUsage
);
2865 if (pChainPara
->cbSize
>= sizeof(CERT_CHAIN_PARA
))
2867 dump_usage_match("RequestedIssuancePolicy",
2868 &pChainPara
->RequestedIssuancePolicy
);
2869 TRACE_(chain
)("%d\n", pChainPara
->dwUrlRetrievalTimeout
);
2870 TRACE_(chain
)("%d\n", pChainPara
->fCheckRevocationFreshnessTime
);
2871 TRACE_(chain
)("%d\n", pChainPara
->dwRevocationFreshnessTime
);
2875 BOOL WINAPI
CertGetCertificateChain(HCERTCHAINENGINE hChainEngine
,
2876 PCCERT_CONTEXT pCertContext
, LPFILETIME pTime
, HCERTSTORE hAdditionalStore
,
2877 PCERT_CHAIN_PARA pChainPara
, DWORD dwFlags
, LPVOID pvReserved
,
2878 PCCERT_CHAIN_CONTEXT
* ppChainContext
)
2880 CertificateChainEngine
*engine
;
2882 CertificateChain
*chain
= NULL
;
2884 TRACE("(%p, %p, %s, %p, %p, %08x, %p, %p)\n", hChainEngine
, pCertContext
,
2885 debugstr_filetime(pTime
), hAdditionalStore
, pChainPara
, dwFlags
,
2886 pvReserved
, ppChainContext
);
2888 engine
= get_chain_engine(hChainEngine
, TRUE
);
2893 *ppChainContext
= NULL
;
2896 SetLastError(E_INVALIDARG
);
2899 if (!pCertContext
->pCertInfo
->SignatureAlgorithm
.pszObjId
)
2901 SetLastError(ERROR_INVALID_DATA
);
2905 if (TRACE_ON(chain
))
2906 dump_chain_para(pChainPara
);
2907 /* FIXME: what about HCCE_LOCAL_MACHINE? */
2908 ret
= CRYPT_BuildCandidateChainFromCert(engine
, pCertContext
, pTime
,
2909 hAdditionalStore
, dwFlags
, &chain
);
2912 CertificateChain
*alternate
= NULL
;
2913 PCERT_CHAIN_CONTEXT pChain
;
2916 alternate
= CRYPT_BuildAlternateContextFromChain(engine
,
2917 pTime
, hAdditionalStore
, dwFlags
, chain
);
2919 /* Alternate contexts are added as "lower quality" contexts of
2920 * chain, to avoid loops in alternate chain creation.
2921 * The highest-quality chain is chosen at the end.
2924 ret
= CRYPT_AddAlternateChainToChain(chain
, alternate
);
2925 } while (ret
&& alternate
);
2926 chain
= CRYPT_ChooseHighestQualityChain(chain
);
2927 if (!(dwFlags
& CERT_CHAIN_RETURN_LOWER_QUALITY_CONTEXTS
))
2928 CRYPT_FreeLowerQualityChains(chain
);
2929 pChain
= (PCERT_CHAIN_CONTEXT
)chain
;
2930 CRYPT_VerifyChainRevocation(pChain
, pTime
, hAdditionalStore
,
2931 pChainPara
, dwFlags
);
2932 CRYPT_CheckUsages(pChain
, pChainPara
);
2933 TRACE_(chain
)("error status: %08x\n",
2934 pChain
->TrustStatus
.dwErrorStatus
);
2936 *ppChainContext
= pChain
;
2938 CertFreeCertificateChain(pChain
);
2940 TRACE("returning %d\n", ret
);
2944 PCCERT_CHAIN_CONTEXT WINAPI
CertDuplicateCertificateChain(
2945 PCCERT_CHAIN_CONTEXT pChainContext
)
2947 CertificateChain
*chain
= (CertificateChain
*)pChainContext
;
2949 TRACE("(%p)\n", pChainContext
);
2952 InterlockedIncrement(&chain
->ref
);
2953 return pChainContext
;
2956 VOID WINAPI
CertFreeCertificateChain(PCCERT_CHAIN_CONTEXT pChainContext
)
2958 CertificateChain
*chain
= (CertificateChain
*)pChainContext
;
2960 TRACE("(%p)\n", pChainContext
);
2964 if (InterlockedDecrement(&chain
->ref
) == 0)
2965 CRYPT_FreeChainContext(chain
);
2969 PCCERT_CHAIN_CONTEXT WINAPI
CertFindChainInStore(HCERTSTORE store
,
2970 DWORD certEncodingType
, DWORD findFlags
, DWORD findType
,
2971 const void *findPara
, PCCERT_CHAIN_CONTEXT prevChainContext
)
2973 FIXME("(%p, %08x, %08x, %d, %p, %p): stub\n", store
, certEncodingType
,
2974 findFlags
, findType
, findPara
, prevChainContext
);
2978 static void find_element_with_error(PCCERT_CHAIN_CONTEXT chain
, DWORD error
,
2979 LONG
*iChain
, LONG
*iElement
)
2983 for (i
= 0; i
< chain
->cChain
; i
++)
2984 for (j
= 0; j
< chain
->rgpChain
[i
]->cElement
; j
++)
2985 if (chain
->rgpChain
[i
]->rgpElement
[j
]->TrustStatus
.dwErrorStatus
&
2994 static BOOL WINAPI
verify_base_policy(LPCSTR szPolicyOID
,
2995 PCCERT_CHAIN_CONTEXT pChainContext
, PCERT_CHAIN_POLICY_PARA pPolicyPara
,
2996 PCERT_CHAIN_POLICY_STATUS pPolicyStatus
)
3001 checks
= pPolicyPara
->dwFlags
;
3002 pPolicyStatus
->lChainIndex
= pPolicyStatus
->lElementIndex
= -1;
3003 pPolicyStatus
->dwError
= NO_ERROR
;
3004 if (pChainContext
->TrustStatus
.dwErrorStatus
&
3005 CERT_TRUST_IS_NOT_SIGNATURE_VALID
)
3007 pPolicyStatus
->dwError
= TRUST_E_CERT_SIGNATURE
;
3008 find_element_with_error(pChainContext
,
3009 CERT_TRUST_IS_NOT_SIGNATURE_VALID
, &pPolicyStatus
->lChainIndex
,
3010 &pPolicyStatus
->lElementIndex
);
3012 else if (pChainContext
->TrustStatus
.dwErrorStatus
& CERT_TRUST_IS_CYCLIC
)
3014 pPolicyStatus
->dwError
= CERT_E_CHAINING
;
3015 find_element_with_error(pChainContext
, CERT_TRUST_IS_CYCLIC
,
3016 &pPolicyStatus
->lChainIndex
, &pPolicyStatus
->lElementIndex
);
3017 /* For a cyclic chain, which element is a cycle isn't meaningful */
3018 pPolicyStatus
->lElementIndex
= -1;
3020 if (!pPolicyStatus
->dwError
&&
3021 pChainContext
->TrustStatus
.dwErrorStatus
& CERT_TRUST_IS_UNTRUSTED_ROOT
&&
3022 !(checks
& CERT_CHAIN_POLICY_ALLOW_UNKNOWN_CA_FLAG
))
3024 pPolicyStatus
->dwError
= CERT_E_UNTRUSTEDROOT
;
3025 find_element_with_error(pChainContext
,
3026 CERT_TRUST_IS_UNTRUSTED_ROOT
, &pPolicyStatus
->lChainIndex
,
3027 &pPolicyStatus
->lElementIndex
);
3029 if (!pPolicyStatus
->dwError
&&
3030 pChainContext
->TrustStatus
.dwErrorStatus
& CERT_TRUST_IS_NOT_TIME_VALID
&&
3031 !(checks
& CERT_CHAIN_POLICY_IGNORE_NOT_TIME_VALID_FLAG
))
3033 pPolicyStatus
->dwError
= CERT_E_EXPIRED
;
3034 find_element_with_error(pChainContext
,
3035 CERT_TRUST_IS_NOT_TIME_VALID
, &pPolicyStatus
->lChainIndex
,
3036 &pPolicyStatus
->lElementIndex
);
3038 if (!pPolicyStatus
->dwError
&&
3039 pChainContext
->TrustStatus
.dwErrorStatus
&
3040 CERT_TRUST_IS_NOT_VALID_FOR_USAGE
&&
3041 !(checks
& CERT_CHAIN_POLICY_IGNORE_WRONG_USAGE_FLAG
))
3043 pPolicyStatus
->dwError
= CERT_E_WRONG_USAGE
;
3044 find_element_with_error(pChainContext
,
3045 CERT_TRUST_IS_NOT_VALID_FOR_USAGE
, &pPolicyStatus
->lChainIndex
,
3046 &pPolicyStatus
->lElementIndex
);
3048 if (!pPolicyStatus
->dwError
&&
3049 pChainContext
->TrustStatus
.dwErrorStatus
&
3050 CERT_TRUST_HAS_NOT_SUPPORTED_CRITICAL_EXT
&&
3051 !(checks
& CERT_CHAIN_POLICY_IGNORE_NOT_SUPPORTED_CRITICAL_EXT_FLAG
))
3053 pPolicyStatus
->dwError
= CERT_E_CRITICAL
;
3054 find_element_with_error(pChainContext
,
3055 CERT_TRUST_HAS_NOT_SUPPORTED_CRITICAL_EXT
, &pPolicyStatus
->lChainIndex
,
3056 &pPolicyStatus
->lElementIndex
);
3061 static BYTE msTestPubKey1
[] = {
3062 0x30,0x47,0x02,0x40,0x81,0x55,0x22,0xb9,0x8a,0xa4,0x6f,0xed,0xd6,0xe7,0xd9,
3063 0x66,0x0f,0x55,0xbc,0xd7,0xcd,0xd5,0xbc,0x4e,0x40,0x02,0x21,0xa2,0xb1,0xf7,
3064 0x87,0x30,0x85,0x5e,0xd2,0xf2,0x44,0xb9,0xdc,0x9b,0x75,0xb6,0xfb,0x46,0x5f,
3065 0x42,0xb6,0x9d,0x23,0x36,0x0b,0xde,0x54,0x0f,0xcd,0xbd,0x1f,0x99,0x2a,0x10,
3066 0x58,0x11,0xcb,0x40,0xcb,0xb5,0xa7,0x41,0x02,0x03,0x01,0x00,0x01 };
3067 static BYTE msTestPubKey2
[] = {
3068 0x30,0x47,0x02,0x40,0x9c,0x50,0x05,0x1d,0xe2,0x0e,0x4c,0x53,0xd8,0xd9,0xb5,
3069 0xe5,0xfd,0xe9,0xe3,0xad,0x83,0x4b,0x80,0x08,0xd9,0xdc,0xe8,0xe8,0x35,0xf8,
3070 0x11,0xf1,0xe9,0x9b,0x03,0x7a,0x65,0x64,0x76,0x35,0xce,0x38,0x2c,0xf2,0xb6,
3071 0x71,0x9e,0x06,0xd9,0xbf,0xbb,0x31,0x69,0xa3,0xf6,0x30,0xa0,0x78,0x7b,0x18,
3072 0xdd,0x50,0x4d,0x79,0x1e,0xeb,0x61,0xc1,0x02,0x03,0x01,0x00,0x01 };
3074 static void dump_authenticode_extra_chain_policy_para(
3075 AUTHENTICODE_EXTRA_CERT_CHAIN_POLICY_PARA
*extraPara
)
3079 TRACE_(chain
)("cbSize = %d\n", extraPara
->cbSize
);
3080 TRACE_(chain
)("dwRegPolicySettings = %08x\n",
3081 extraPara
->dwRegPolicySettings
);
3082 TRACE_(chain
)("pSignerInfo = %p\n", extraPara
->pSignerInfo
);
3086 static BOOL WINAPI
verify_authenticode_policy(LPCSTR szPolicyOID
,
3087 PCCERT_CHAIN_CONTEXT pChainContext
, PCERT_CHAIN_POLICY_PARA pPolicyPara
,
3088 PCERT_CHAIN_POLICY_STATUS pPolicyStatus
)
3090 BOOL ret
= verify_base_policy(szPolicyOID
, pChainContext
, pPolicyPara
,
3092 AUTHENTICODE_EXTRA_CERT_CHAIN_POLICY_PARA
*extraPara
= NULL
;
3095 extraPara
= pPolicyPara
->pvExtraPolicyPara
;
3096 if (TRACE_ON(chain
))
3097 dump_authenticode_extra_chain_policy_para(extraPara
);
3098 if (ret
&& pPolicyStatus
->dwError
== CERT_E_UNTRUSTEDROOT
)
3100 CERT_PUBLIC_KEY_INFO msPubKey
= { { 0 } };
3101 BOOL isMSTestRoot
= FALSE
;
3102 PCCERT_CONTEXT failingCert
=
3103 pChainContext
->rgpChain
[pPolicyStatus
->lChainIndex
]->
3104 rgpElement
[pPolicyStatus
->lElementIndex
]->pCertContext
;
3106 CRYPT_DATA_BLOB keyBlobs
[] = {
3107 { sizeof(msTestPubKey1
), msTestPubKey1
},
3108 { sizeof(msTestPubKey2
), msTestPubKey2
},
3111 /* Check whether the root is an MS test root */
3112 for (i
= 0; !isMSTestRoot
&& i
< ARRAY_SIZE(keyBlobs
); i
++)
3114 msPubKey
.PublicKey
.cbData
= keyBlobs
[i
].cbData
;
3115 msPubKey
.PublicKey
.pbData
= keyBlobs
[i
].pbData
;
3116 if (CertComparePublicKeyInfo(
3117 X509_ASN_ENCODING
| PKCS_7_ASN_ENCODING
,
3118 &failingCert
->pCertInfo
->SubjectPublicKeyInfo
, &msPubKey
))
3119 isMSTestRoot
= TRUE
;
3122 pPolicyStatus
->dwError
= CERT_E_UNTRUSTEDTESTROOT
;
3127 static BOOL WINAPI
verify_basic_constraints_policy(LPCSTR szPolicyOID
,
3128 PCCERT_CHAIN_CONTEXT pChainContext
, PCERT_CHAIN_POLICY_PARA pPolicyPara
,
3129 PCERT_CHAIN_POLICY_STATUS pPolicyStatus
)
3131 pPolicyStatus
->lChainIndex
= pPolicyStatus
->lElementIndex
= -1;
3132 if (pChainContext
->TrustStatus
.dwErrorStatus
&
3133 CERT_TRUST_INVALID_BASIC_CONSTRAINTS
)
3135 pPolicyStatus
->dwError
= TRUST_E_BASIC_CONSTRAINTS
;
3136 find_element_with_error(pChainContext
,
3137 CERT_TRUST_INVALID_BASIC_CONSTRAINTS
, &pPolicyStatus
->lChainIndex
,
3138 &pPolicyStatus
->lElementIndex
);
3141 pPolicyStatus
->dwError
= NO_ERROR
;
3145 static BOOL
match_dns_to_subject_alt_name(const CERT_EXTENSION
*ext
,
3146 LPCWSTR server_name
)
3148 BOOL matches
= FALSE
;
3149 CERT_ALT_NAME_INFO
*subjectName
;
3152 TRACE_(chain
)("%s\n", debugstr_w(server_name
));
3153 /* This could be spoofed by the embedded NULL vulnerability, since the
3154 * returned CERT_ALT_NAME_INFO doesn't have a way to indicate the
3155 * encoded length of a name. Fortunately CryptDecodeObjectEx fails if
3156 * the encoded form of the name contains a NULL.
3158 if (CryptDecodeObjectEx(X509_ASN_ENCODING
, X509_ALTERNATE_NAME
,
3159 ext
->Value
.pbData
, ext
->Value
.cbData
,
3160 CRYPT_DECODE_ALLOC_FLAG
| CRYPT_DECODE_NOCOPY_FLAG
, NULL
,
3161 &subjectName
, &size
))
3165 /* RFC 5280 states that multiple instances of each name type may exist,
3166 * in section 4.2.1.6:
3167 * "Multiple name forms, and multiple instances of each name form,
3169 * It doesn't specify the behavior in such cases, but both RFC 2818
3170 * and RFC 2595 explicitly accept a certificate if any name matches.
3172 for (i
= 0; !matches
&& i
< subjectName
->cAltEntry
; i
++)
3174 if (subjectName
->rgAltEntry
[i
].dwAltNameChoice
==
3175 CERT_ALT_NAME_DNS_NAME
)
3177 TRACE_(chain
)("dNSName: %s\n", debugstr_w(
3178 subjectName
->rgAltEntry
[i
].u
.pwszDNSName
));
3179 if (subjectName
->rgAltEntry
[i
].u
.pwszDNSName
[0] == '*')
3181 LPCWSTR server_name_dot
;
3183 /* Matching a wildcard: a wildcard matches a single name
3184 * component, which is terminated by a dot. RFC 1034
3185 * doesn't define whether multiple wildcards are allowed,
3186 * but I will assume that they are not until proven
3187 * otherwise. RFC 1034 also states that 'the "*" label
3188 * always matches at least one whole label and sometimes
3189 * more, but always whole labels.' Native crypt32 does not
3190 * match more than one label with a wildcard, so I do the
3191 * same here. Thus, a wildcard only accepts the first
3192 * label, then requires an exact match of the remaining
3195 server_name_dot
= wcschr(server_name
, '.');
3196 if (server_name_dot
)
3198 if (!wcsicmp(server_name_dot
,
3199 subjectName
->rgAltEntry
[i
].u
.pwszDNSName
+ 1))
3203 else if (!wcsicmp(server_name
,
3204 subjectName
->rgAltEntry
[i
].u
.pwszDNSName
))
3208 LocalFree(subjectName
);
3213 static BOOL
find_matching_domain_component(const CERT_NAME_INFO
*name
,
3214 const WCHAR
*component
, size_t len
)
3218 for (i
= 0; i
< name
->cRDN
; i
++)
3219 for (j
= 0; j
< name
->rgRDN
[i
].cRDNAttr
; j
++)
3220 if (!strcmp(szOID_DOMAIN_COMPONENT
,
3221 name
->rgRDN
[i
].rgRDNAttr
[j
].pszObjId
))
3223 const CERT_RDN_ATTR
*attr
;
3225 attr
= &name
->rgRDN
[i
].rgRDNAttr
[j
];
3226 /* Compare with wcsnicmp rather than wcsicmp in order to avoid
3227 * a match with a string with an embedded NULL. The component
3228 * must match one domain component attribute's entire string
3229 * value with a case-insensitive match.
3231 if ((len
== attr
->Value
.cbData
/ sizeof(WCHAR
)) &&
3232 !wcsnicmp(component
, (LPCWSTR
)attr
->Value
.pbData
, len
))
3238 static BOOL
match_domain_component(LPCWSTR allowed_component
, DWORD allowed_len
,
3239 LPCWSTR server_component
, DWORD server_len
, BOOL allow_wildcards
,
3242 LPCWSTR allowed_ptr
, server_ptr
;
3243 BOOL matches
= TRUE
;
3245 *see_wildcard
= FALSE
;
3247 if (server_len
< allowed_len
)
3249 WARN_(chain
)("domain component %s too short for %s\n",
3250 debugstr_wn(server_component
, server_len
),
3251 debugstr_wn(allowed_component
, allowed_len
));
3252 /* A domain component can't contain a wildcard character, so a domain
3253 * component shorter than the allowed string can't produce a match.
3257 for (allowed_ptr
= allowed_component
, server_ptr
= server_component
;
3258 matches
&& allowed_ptr
- allowed_component
< allowed_len
;
3259 allowed_ptr
++, server_ptr
++)
3261 if (*allowed_ptr
== '*')
3263 if (allowed_ptr
- allowed_component
< allowed_len
- 1)
3265 WARN_(chain
)("non-wildcard characters after wildcard not supported\n");
3268 else if (!allow_wildcards
)
3270 WARN_(chain
)("wildcard after non-wildcard component\n");
3275 /* the preceding characters must have matched, so the rest of
3276 * the component also matches.
3278 *see_wildcard
= TRUE
;
3283 matches
= towlower(*allowed_ptr
) == towlower(*server_ptr
);
3285 if (matches
&& server_ptr
- server_component
< server_len
)
3287 /* If there are unmatched characters in the server domain component,
3288 * the server domain only matches if the allowed string ended in a '*'.
3290 matches
= *allowed_ptr
== '*';
3295 static BOOL
match_common_name(LPCWSTR server_name
, const CERT_RDN_ATTR
*nameAttr
)
3297 LPCWSTR allowed
= (LPCWSTR
)nameAttr
->Value
.pbData
;
3298 LPCWSTR allowed_component
= allowed
;
3299 DWORD allowed_len
= nameAttr
->Value
.cbData
/ sizeof(WCHAR
);
3300 LPCWSTR server_component
= server_name
;
3301 DWORD server_len
= lstrlenW(server_name
);
3302 BOOL matches
= TRUE
, allow_wildcards
= TRUE
;
3304 TRACE_(chain
)("CN = %s\n", debugstr_wn(allowed_component
, allowed_len
));
3306 /* Remove trailing NULLs from the allowed name; while they shouldn't appear
3307 * in a certificate in the first place, they sometimes do, and they should
3310 while (allowed_len
&& allowed_component
[allowed_len
- 1] == 0)
3313 /* From RFC 2818 (HTTP over TLS), section 3.1:
3314 * "Names may contain the wildcard character * which is considered to match
3315 * any single domain name component or component fragment. E.g.,
3316 * *.a.com matches foo.a.com but not bar.foo.a.com. f*.com matches foo.com
3319 * And from RFC 2595 (Using TLS with IMAP, POP3 and ACAP), section 2.4:
3320 * "A "*" wildcard character MAY be used as the left-most name component in
3321 * the certificate. For example, *.example.com would match a.example.com,
3322 * foo.example.com, etc. but would not match example.com."
3324 * There are other protocols which use TLS, and none of them is
3325 * authoritative. This accepts certificates in common usage, e.g.
3326 * *.domain.com matches www.domain.com but not domain.com, and
3327 * www*.domain.com matches www1.domain.com but not mail.domain.com.
3330 LPCWSTR allowed_dot
, server_dot
;
3332 allowed_dot
= wmemchr(allowed_component
, '.',
3333 allowed_len
- (allowed_component
- allowed
));
3334 server_dot
= wmemchr(server_component
, '.',
3335 server_len
- (server_component
- server_name
));
3336 /* The number of components must match */
3337 if ((!allowed_dot
&& server_dot
) || (allowed_dot
&& !server_dot
))
3340 WARN_(chain
)("%s: too many components for CN=%s\n",
3341 debugstr_w(server_name
), debugstr_wn(allowed
, allowed_len
));
3343 WARN_(chain
)("%s: not enough components for CN=%s\n",
3344 debugstr_w(server_name
), debugstr_wn(allowed
, allowed_len
));
3349 LPCWSTR allowed_end
, server_end
;
3352 allowed_end
= allowed_dot
? allowed_dot
: allowed
+ allowed_len
;
3353 server_end
= server_dot
? server_dot
: server_name
+ server_len
;
3354 matches
= match_domain_component(allowed_component
,
3355 allowed_end
- allowed_component
, server_component
,
3356 server_end
- server_component
, allow_wildcards
, &has_wildcard
);
3357 /* Once a non-wildcard component is seen, no wildcard components
3361 allow_wildcards
= FALSE
;
3364 allowed_component
= allowed_dot
? allowed_dot
+ 1 : allowed_end
;
3365 server_component
= server_dot
? server_dot
+ 1 : server_end
;
3368 } while (matches
&& allowed_component
&&
3369 allowed_component
- allowed
< allowed_len
&&
3370 server_component
&& server_component
- server_name
< server_len
);
3371 TRACE_(chain
)("returning %d\n", matches
);
3375 static BOOL
match_dns_to_subject_dn(PCCERT_CONTEXT cert
, LPCWSTR server_name
)
3377 BOOL matches
= FALSE
;
3378 CERT_NAME_INFO
*name
;
3381 TRACE_(chain
)("%s\n", debugstr_w(server_name
));
3382 if (CryptDecodeObjectEx(X509_ASN_ENCODING
, X509_UNICODE_NAME
,
3383 cert
->pCertInfo
->Subject
.pbData
, cert
->pCertInfo
->Subject
.cbData
,
3384 CRYPT_DECODE_ALLOC_FLAG
| CRYPT_DECODE_NOCOPY_FLAG
, NULL
,
3387 /* If the subject distinguished name contains any name components,
3388 * make sure all of them are present.
3390 if (CertFindRDNAttr(szOID_DOMAIN_COMPONENT
, name
))
3392 LPCWSTR ptr
= server_name
;
3395 LPCWSTR dot
= wcschr(ptr
, '.'), end
;
3396 /* 254 is the maximum DNS label length, see RFC 1035 */
3399 end
= dot
? dot
: ptr
+ lstrlenW(ptr
);
3403 WARN_(chain
)("domain component %s too long\n",
3404 debugstr_wn(ptr
, len
));
3407 else matches
= find_matching_domain_component(name
, ptr
, len
);
3409 ptr
= dot
? dot
+ 1 : end
;
3410 } while (matches
&& ptr
&& *ptr
);
3416 /* If the certificate isn't using a DN attribute in the name, make
3417 * make sure at least one common name matches. From RFC 2818,
3419 * "If more than one identity of a given type is present in the
3420 * certificate (e.g., more than one dNSName name, a match in any
3421 * one of the set is considered acceptable.)"
3423 for (i
= 0; !matches
&& i
< name
->cRDN
; i
++)
3424 for (j
= 0; !matches
&& j
< name
->rgRDN
[i
].cRDNAttr
; j
++)
3426 PCERT_RDN_ATTR attr
= &name
->rgRDN
[i
].rgRDNAttr
[j
];
3428 if (attr
->pszObjId
&& !strcmp(szOID_COMMON_NAME
,
3430 matches
= match_common_name(server_name
, attr
);
3438 static void dump_ssl_extra_chain_policy_para(HTTPSPolicyCallbackData
*sslPara
)
3442 TRACE_(chain
)("cbSize = %d\n", sslPara
->u
.cbSize
);
3443 TRACE_(chain
)("dwAuthType = %d\n", sslPara
->dwAuthType
);
3444 TRACE_(chain
)("fdwChecks = %08x\n", sslPara
->fdwChecks
);
3445 TRACE_(chain
)("pwszServerName = %s\n",
3446 debugstr_w(sslPara
->pwszServerName
));
3450 static BOOL WINAPI
verify_ssl_policy(LPCSTR szPolicyOID
,
3451 PCCERT_CHAIN_CONTEXT pChainContext
, PCERT_CHAIN_POLICY_PARA pPolicyPara
,
3452 PCERT_CHAIN_POLICY_STATUS pPolicyStatus
)
3454 HTTPSPolicyCallbackData
*sslPara
= NULL
;
3455 DWORD checks
= 0, baseChecks
= 0;
3459 baseChecks
= pPolicyPara
->dwFlags
;
3460 sslPara
= pPolicyPara
->pvExtraPolicyPara
;
3462 if (TRACE_ON(chain
))
3463 dump_ssl_extra_chain_policy_para(sslPara
);
3464 if (sslPara
&& sslPara
->u
.cbSize
>= sizeof(HTTPSPolicyCallbackData
))
3465 checks
= sslPara
->fdwChecks
;
3466 pPolicyStatus
->lChainIndex
= pPolicyStatus
->lElementIndex
= -1;
3467 if (pChainContext
->TrustStatus
.dwErrorStatus
&
3468 CERT_TRUST_IS_NOT_SIGNATURE_VALID
)
3470 pPolicyStatus
->dwError
= TRUST_E_CERT_SIGNATURE
;
3471 find_element_with_error(pChainContext
,
3472 CERT_TRUST_IS_NOT_SIGNATURE_VALID
, &pPolicyStatus
->lChainIndex
,
3473 &pPolicyStatus
->lElementIndex
);
3475 else if (pChainContext
->TrustStatus
.dwErrorStatus
&
3476 CERT_TRUST_IS_UNTRUSTED_ROOT
&&
3477 !(checks
& SECURITY_FLAG_IGNORE_UNKNOWN_CA
) &&
3478 !(baseChecks
& CERT_CHAIN_POLICY_ALLOW_UNKNOWN_CA_FLAG
))
3480 pPolicyStatus
->dwError
= CERT_E_UNTRUSTEDROOT
;
3481 find_element_with_error(pChainContext
,
3482 CERT_TRUST_IS_UNTRUSTED_ROOT
, &pPolicyStatus
->lChainIndex
,
3483 &pPolicyStatus
->lElementIndex
);
3485 else if (pChainContext
->TrustStatus
.dwErrorStatus
& CERT_TRUST_IS_CYCLIC
)
3487 pPolicyStatus
->dwError
= CERT_E_UNTRUSTEDROOT
;
3488 find_element_with_error(pChainContext
,
3489 CERT_TRUST_IS_CYCLIC
, &pPolicyStatus
->lChainIndex
,
3490 &pPolicyStatus
->lElementIndex
);
3491 /* For a cyclic chain, which element is a cycle isn't meaningful */
3492 pPolicyStatus
->lElementIndex
= -1;
3494 else if (pChainContext
->TrustStatus
.dwErrorStatus
&
3495 CERT_TRUST_IS_NOT_TIME_VALID
&&
3496 !(checks
& SECURITY_FLAG_IGNORE_CERT_DATE_INVALID
) &&
3497 !(baseChecks
& CERT_CHAIN_POLICY_IGNORE_NOT_TIME_VALID_FLAG
))
3499 pPolicyStatus
->dwError
= CERT_E_EXPIRED
;
3500 find_element_with_error(pChainContext
,
3501 CERT_TRUST_IS_NOT_TIME_VALID
, &pPolicyStatus
->lChainIndex
,
3502 &pPolicyStatus
->lElementIndex
);
3504 else if (pChainContext
->TrustStatus
.dwErrorStatus
&
3505 CERT_TRUST_IS_NOT_VALID_FOR_USAGE
&&
3506 !(checks
& SECURITY_FLAG_IGNORE_WRONG_USAGE
))
3508 pPolicyStatus
->dwError
= CERT_E_WRONG_USAGE
;
3509 find_element_with_error(pChainContext
,
3510 CERT_TRUST_IS_NOT_VALID_FOR_USAGE
, &pPolicyStatus
->lChainIndex
,
3511 &pPolicyStatus
->lElementIndex
);
3513 else if (pChainContext
->TrustStatus
.dwErrorStatus
&
3514 CERT_TRUST_IS_REVOKED
&& !(checks
& SECURITY_FLAG_IGNORE_REVOCATION
))
3516 pPolicyStatus
->dwError
= CERT_E_REVOKED
;
3517 find_element_with_error(pChainContext
,
3518 CERT_TRUST_IS_REVOKED
, &pPolicyStatus
->lChainIndex
,
3519 &pPolicyStatus
->lElementIndex
);
3521 else if (pChainContext
->TrustStatus
.dwErrorStatus
&
3522 CERT_TRUST_IS_OFFLINE_REVOCATION
&&
3523 !(checks
& SECURITY_FLAG_IGNORE_REVOCATION
))
3525 pPolicyStatus
->dwError
= CERT_E_REVOCATION_FAILURE
;
3526 find_element_with_error(pChainContext
,
3527 CERT_TRUST_IS_OFFLINE_REVOCATION
, &pPolicyStatus
->lChainIndex
,
3528 &pPolicyStatus
->lElementIndex
);
3530 else if (pChainContext
->TrustStatus
.dwErrorStatus
&
3531 CERT_TRUST_HAS_NOT_SUPPORTED_CRITICAL_EXT
)
3533 pPolicyStatus
->dwError
= CERT_E_CRITICAL
;
3534 find_element_with_error(pChainContext
,
3535 CERT_TRUST_HAS_NOT_SUPPORTED_CRITICAL_EXT
, &pPolicyStatus
->lChainIndex
,
3536 &pPolicyStatus
->lElementIndex
);
3539 pPolicyStatus
->dwError
= NO_ERROR
;
3540 /* We only need bother checking whether the name in the end certificate
3541 * matches if the chain is otherwise okay.
3543 if (!pPolicyStatus
->dwError
&& pPolicyPara
&&
3544 pPolicyPara
->cbSize
>= sizeof(CERT_CHAIN_POLICY_PARA
))
3546 if (sslPara
&& sslPara
->u
.cbSize
>= sizeof(HTTPSPolicyCallbackData
))
3548 if (sslPara
->dwAuthType
== AUTHTYPE_SERVER
&&
3549 sslPara
->pwszServerName
&&
3550 !(checks
& SECURITY_FLAG_IGNORE_CERT_CN_INVALID
))
3552 PCCERT_CONTEXT cert
;
3553 PCERT_EXTENSION altNameExt
;
3556 cert
= pChainContext
->rgpChain
[0]->rgpElement
[0]->pCertContext
;
3557 altNameExt
= get_subject_alt_name_ext(cert
->pCertInfo
);
3558 /* If the alternate name extension exists, the name it contains
3559 * is bound to the certificate, so make sure the name matches
3560 * it. Otherwise, look for the server name in the subject
3561 * distinguished name. RFC5280, section 4.2.1.6:
3562 * "Whenever such identities are to be bound into a
3563 * certificate, the subject alternative name (or issuer
3564 * alternative name) extension MUST be used; however, a DNS
3565 * name MAY also be represented in the subject field using the
3566 * domainComponent attribute."
3569 matches
= match_dns_to_subject_alt_name(altNameExt
,
3570 sslPara
->pwszServerName
);
3572 matches
= match_dns_to_subject_dn(cert
,
3573 sslPara
->pwszServerName
);
3576 pPolicyStatus
->dwError
= CERT_E_CN_NO_MATCH
;
3577 pPolicyStatus
->lChainIndex
= 0;
3578 pPolicyStatus
->lElementIndex
= 0;
3586 static BYTE msPubKey1
[] = {
3587 0x30,0x82,0x01,0x0a,0x02,0x82,0x01,0x01,0x00,0xdf,0x08,0xba,0xe3,0x3f,0x6e,
3588 0x64,0x9b,0xf5,0x89,0xaf,0x28,0x96,0x4a,0x07,0x8f,0x1b,0x2e,0x8b,0x3e,0x1d,
3589 0xfc,0xb8,0x80,0x69,0xa3,0xa1,0xce,0xdb,0xdf,0xb0,0x8e,0x6c,0x89,0x76,0x29,
3590 0x4f,0xca,0x60,0x35,0x39,0xad,0x72,0x32,0xe0,0x0b,0xae,0x29,0x3d,0x4c,0x16,
3591 0xd9,0x4b,0x3c,0x9d,0xda,0xc5,0xd3,0xd1,0x09,0xc9,0x2c,0x6f,0xa6,0xc2,0x60,
3592 0x53,0x45,0xdd,0x4b,0xd1,0x55,0xcd,0x03,0x1c,0xd2,0x59,0x56,0x24,0xf3,0xe5,
3593 0x78,0xd8,0x07,0xcc,0xd8,0xb3,0x1f,0x90,0x3f,0xc0,0x1a,0x71,0x50,0x1d,0x2d,
3594 0xa7,0x12,0x08,0x6d,0x7c,0xb0,0x86,0x6c,0xc7,0xba,0x85,0x32,0x07,0xe1,0x61,
3595 0x6f,0xaf,0x03,0xc5,0x6d,0xe5,0xd6,0xa1,0x8f,0x36,0xf6,0xc1,0x0b,0xd1,0x3e,
3596 0x69,0x97,0x48,0x72,0xc9,0x7f,0xa4,0xc8,0xc2,0x4a,0x4c,0x7e,0xa1,0xd1,0x94,
3597 0xa6,0xd7,0xdc,0xeb,0x05,0x46,0x2e,0xb8,0x18,0xb4,0x57,0x1d,0x86,0x49,0xdb,
3598 0x69,0x4a,0x2c,0x21,0xf5,0x5e,0x0f,0x54,0x2d,0x5a,0x43,0xa9,0x7a,0x7e,0x6a,
3599 0x8e,0x50,0x4d,0x25,0x57,0xa1,0xbf,0x1b,0x15,0x05,0x43,0x7b,0x2c,0x05,0x8d,
3600 0xbd,0x3d,0x03,0x8c,0x93,0x22,0x7d,0x63,0xea,0x0a,0x57,0x05,0x06,0x0a,0xdb,
3601 0x61,0x98,0x65,0x2d,0x47,0x49,0xa8,0xe7,0xe6,0x56,0x75,0x5c,0xb8,0x64,0x08,
3602 0x63,0xa9,0x30,0x40,0x66,0xb2,0xf9,0xb6,0xe3,0x34,0xe8,0x67,0x30,0xe1,0x43,
3603 0x0b,0x87,0xff,0xc9,0xbe,0x72,0x10,0x5e,0x23,0xf0,0x9b,0xa7,0x48,0x65,0xbf,
3604 0x09,0x88,0x7b,0xcd,0x72,0xbc,0x2e,0x79,0x9b,0x7b,0x02,0x03,0x01,0x00,0x01 };
3605 static BYTE msPubKey2
[] = {
3606 0x30,0x82,0x01,0x0a,0x02,0x82,0x01,0x01,0x00,0xa9,0x02,0xbd,0xc1,0x70,0xe6,
3607 0x3b,0xf2,0x4e,0x1b,0x28,0x9f,0x97,0x78,0x5e,0x30,0xea,0xa2,0xa9,0x8d,0x25,
3608 0x5f,0xf8,0xfe,0x95,0x4c,0xa3,0xb7,0xfe,0x9d,0xa2,0x20,0x3e,0x7c,0x51,0xa2,
3609 0x9b,0xa2,0x8f,0x60,0x32,0x6b,0xd1,0x42,0x64,0x79,0xee,0xac,0x76,0xc9,0x54,
3610 0xda,0xf2,0xeb,0x9c,0x86,0x1c,0x8f,0x9f,0x84,0x66,0xb3,0xc5,0x6b,0x7a,0x62,
3611 0x23,0xd6,0x1d,0x3c,0xde,0x0f,0x01,0x92,0xe8,0x96,0xc4,0xbf,0x2d,0x66,0x9a,
3612 0x9a,0x68,0x26,0x99,0xd0,0x3a,0x2c,0xbf,0x0c,0xb5,0x58,0x26,0xc1,0x46,0xe7,
3613 0x0a,0x3e,0x38,0x96,0x2c,0xa9,0x28,0x39,0xa8,0xec,0x49,0x83,0x42,0xe3,0x84,
3614 0x0f,0xbb,0x9a,0x6c,0x55,0x61,0xac,0x82,0x7c,0xa1,0x60,0x2d,0x77,0x4c,0xe9,
3615 0x99,0xb4,0x64,0x3b,0x9a,0x50,0x1c,0x31,0x08,0x24,0x14,0x9f,0xa9,0xe7,0x91,
3616 0x2b,0x18,0xe6,0x3d,0x98,0x63,0x14,0x60,0x58,0x05,0x65,0x9f,0x1d,0x37,0x52,
3617 0x87,0xf7,0xa7,0xef,0x94,0x02,0xc6,0x1b,0xd3,0xbf,0x55,0x45,0xb3,0x89,0x80,
3618 0xbf,0x3a,0xec,0x54,0x94,0x4e,0xae,0xfd,0xa7,0x7a,0x6d,0x74,0x4e,0xaf,0x18,
3619 0xcc,0x96,0x09,0x28,0x21,0x00,0x57,0x90,0x60,0x69,0x37,0xbb,0x4b,0x12,0x07,
3620 0x3c,0x56,0xff,0x5b,0xfb,0xa4,0x66,0x0a,0x08,0xa6,0xd2,0x81,0x56,0x57,0xef,
3621 0xb6,0x3b,0x5e,0x16,0x81,0x77,0x04,0xda,0xf6,0xbe,0xae,0x80,0x95,0xfe,0xb0,
3622 0xcd,0x7f,0xd6,0xa7,0x1a,0x72,0x5c,0x3c,0xca,0xbc,0xf0,0x08,0xa3,0x22,0x30,
3623 0xb3,0x06,0x85,0xc9,0xb3,0x20,0x77,0x13,0x85,0xdf,0x02,0x03,0x01,0x00,0x01 };
3624 static BYTE msPubKey3
[] = {
3625 0x30,0x82,0x02,0x0a,0x02,0x82,0x02,0x01,0x00,0xf3,0x5d,0xfa,0x80,0x67,0xd4,
3626 0x5a,0xa7,0xa9,0x0c,0x2c,0x90,0x20,0xd0,0x35,0x08,0x3c,0x75,0x84,0xcd,0xb7,
3627 0x07,0x89,0x9c,0x89,0xda,0xde,0xce,0xc3,0x60,0xfa,0x91,0x68,0x5a,0x9e,0x94,
3628 0x71,0x29,0x18,0x76,0x7c,0xc2,0xe0,0xc8,0x25,0x76,0x94,0x0e,0x58,0xfa,0x04,
3629 0x34,0x36,0xe6,0xdf,0xaf,0xf7,0x80,0xba,0xe9,0x58,0x0b,0x2b,0x93,0xe5,0x9d,
3630 0x05,0xe3,0x77,0x22,0x91,0xf7,0x34,0x64,0x3c,0x22,0x91,0x1d,0x5e,0xe1,0x09,
3631 0x90,0xbc,0x14,0xfe,0xfc,0x75,0x58,0x19,0xe1,0x79,0xb7,0x07,0x92,0xa3,0xae,
3632 0x88,0x59,0x08,0xd8,0x9f,0x07,0xca,0x03,0x58,0xfc,0x68,0x29,0x6d,0x32,0xd7,
3633 0xd2,0xa8,0xcb,0x4b,0xfc,0xe1,0x0b,0x48,0x32,0x4f,0xe6,0xeb,0xb8,0xad,0x4f,
3634 0xe4,0x5c,0x6f,0x13,0x94,0x99,0xdb,0x95,0xd5,0x75,0xdb,0xa8,0x1a,0xb7,0x94,
3635 0x91,0xb4,0x77,0x5b,0xf5,0x48,0x0c,0x8f,0x6a,0x79,0x7d,0x14,0x70,0x04,0x7d,
3636 0x6d,0xaf,0x90,0xf5,0xda,0x70,0xd8,0x47,0xb7,0xbf,0x9b,0x2f,0x6c,0xe7,0x05,
3637 0xb7,0xe1,0x11,0x60,0xac,0x79,0x91,0x14,0x7c,0xc5,0xd6,0xa6,0xe4,0xe1,0x7e,
3638 0xd5,0xc3,0x7e,0xe5,0x92,0xd2,0x3c,0x00,0xb5,0x36,0x82,0xde,0x79,0xe1,0x6d,
3639 0xf3,0xb5,0x6e,0xf8,0x9f,0x33,0xc9,0xcb,0x52,0x7d,0x73,0x98,0x36,0xdb,0x8b,
3640 0xa1,0x6b,0xa2,0x95,0x97,0x9b,0xa3,0xde,0xc2,0x4d,0x26,0xff,0x06,0x96,0x67,
3641 0x25,0x06,0xc8,0xe7,0xac,0xe4,0xee,0x12,0x33,0x95,0x31,0x99,0xc8,0x35,0x08,
3642 0x4e,0x34,0xca,0x79,0x53,0xd5,0xb5,0xbe,0x63,0x32,0x59,0x40,0x36,0xc0,0xa5,
3643 0x4e,0x04,0x4d,0x3d,0xdb,0x5b,0x07,0x33,0xe4,0x58,0xbf,0xef,0x3f,0x53,0x64,
3644 0xd8,0x42,0x59,0x35,0x57,0xfd,0x0f,0x45,0x7c,0x24,0x04,0x4d,0x9e,0xd6,0x38,
3645 0x74,0x11,0x97,0x22,0x90,0xce,0x68,0x44,0x74,0x92,0x6f,0xd5,0x4b,0x6f,0xb0,
3646 0x86,0xe3,0xc7,0x36,0x42,0xa0,0xd0,0xfc,0xc1,0xc0,0x5a,0xf9,0xa3,0x61,0xb9,
3647 0x30,0x47,0x71,0x96,0x0a,0x16,0xb0,0x91,0xc0,0x42,0x95,0xef,0x10,0x7f,0x28,
3648 0x6a,0xe3,0x2a,0x1f,0xb1,0xe4,0xcd,0x03,0x3f,0x77,0x71,0x04,0xc7,0x20,0xfc,
3649 0x49,0x0f,0x1d,0x45,0x88,0xa4,0xd7,0xcb,0x7e,0x88,0xad,0x8e,0x2d,0xec,0x45,
3650 0xdb,0xc4,0x51,0x04,0xc9,0x2a,0xfc,0xec,0x86,0x9e,0x9a,0x11,0x97,0x5b,0xde,
3651 0xce,0x53,0x88,0xe6,0xe2,0xb7,0xfd,0xac,0x95,0xc2,0x28,0x40,0xdb,0xef,0x04,
3652 0x90,0xdf,0x81,0x33,0x39,0xd9,0xb2,0x45,0xa5,0x23,0x87,0x06,0xa5,0x55,0x89,
3653 0x31,0xbb,0x06,0x2d,0x60,0x0e,0x41,0x18,0x7d,0x1f,0x2e,0xb5,0x97,0xcb,0x11,
3654 0xeb,0x15,0xd5,0x24,0xa5,0x94,0xef,0x15,0x14,0x89,0xfd,0x4b,0x73,0xfa,0x32,
3655 0x5b,0xfc,0xd1,0x33,0x00,0xf9,0x59,0x62,0x70,0x07,0x32,0xea,0x2e,0xab,0x40,
3656 0x2d,0x7b,0xca,0xdd,0x21,0x67,0x1b,0x30,0x99,0x8f,0x16,0xaa,0x23,0xa8,0x41,
3657 0xd1,0xb0,0x6e,0x11,0x9b,0x36,0xc4,0xde,0x40,0x74,0x9c,0xe1,0x58,0x65,0xc1,
3658 0x60,0x1e,0x7a,0x5b,0x38,0xc8,0x8f,0xbb,0x04,0x26,0x7c,0xd4,0x16,0x40,0xe5,
3659 0xb6,0x6b,0x6c,0xaa,0x86,0xfd,0x00,0xbf,0xce,0xc1,0x35,0x02,0x03,0x01,0x00,
3661 /* from Microsoft Root Certificate Authority 2010 */
3662 static BYTE msPubKey4
[] = {
3663 0x30,0x82,0x02,0x0a,0x02,0x82,0x02,0x01,0x00,0xb9,0x08,0x9e,0x28,0xe4,0xe4,
3664 0xec,0x06,0x4e,0x50,0x68,0xb3,0x41,0xc5,0x7b,0xeb,0xae,0xb6,0x8e,0xaf,0x81,
3665 0xba,0x22,0x44,0x1f,0x65,0x34,0x69,0x4c,0xbe,0x70,0x40,0x17,0xf2,0x16,0x7b,
3666 0xe2,0x79,0xfd,0x86,0xed,0x0d,0x39,0xf4,0x1b,0xa8,0xad,0x92,0x90,0x1e,0xcb,
3667 0x3d,0x76,0x8f,0x5a,0xd9,0xb5,0x91,0x10,0x2e,0x3c,0x05,0x8d,0x8a,0x6d,0x24,
3668 0x54,0xe7,0x1f,0xed,0x56,0xad,0x83,0xb4,0x50,0x9c,0x15,0xa5,0x17,0x74,0x88,
3669 0x59,0x20,0xfc,0x08,0xc5,0x84,0x76,0xd3,0x68,0xd4,0x6f,0x28,0x78,0xce,0x5c,
3670 0xb8,0xf3,0x50,0x90,0x44,0xff,0xe3,0x63,0x5f,0xbe,0xa1,0x9a,0x2c,0x96,0x15,
3671 0x04,0xd6,0x07,0xfe,0x1e,0x84,0x21,0xe0,0x42,0x31,0x11,0xc4,0x28,0x36,0x94,
3672 0xcf,0x50,0xa4,0x62,0x9e,0xc9,0xd6,0xab,0x71,0x00,0xb2,0x5b,0x0c,0xe6,0x96,
3673 0xd4,0x0a,0x24,0x96,0xf5,0xff,0xc6,0xd5,0xb7,0x1b,0xd7,0xcb,0xb7,0x21,0x62,
3674 0xaf,0x12,0xdc,0xa1,0x5d,0x37,0xe3,0x1a,0xfb,0x1a,0x46,0x98,0xc0,0x9b,0xc0,
3675 0xe7,0x63,0x1f,0x2a,0x08,0x93,0x02,0x7e,0x1e,0x6a,0x8e,0xf2,0x9f,0x18,0x89,
3676 0xe4,0x22,0x85,0xa2,0xb1,0x84,0x57,0x40,0xff,0xf5,0x0e,0xd8,0x6f,0x9c,0xed,
3677 0xe2,0x45,0x31,0x01,0xcd,0x17,0xe9,0x7f,0xb0,0x81,0x45,0xe3,0xaa,0x21,0x40,
3678 0x26,0xa1,0x72,0xaa,0xa7,0x4f,0x3c,0x01,0x05,0x7e,0xee,0x83,0x58,0xb1,0x5e,
3679 0x06,0x63,0x99,0x62,0x91,0x78,0x82,0xb7,0x0d,0x93,0x0c,0x24,0x6a,0xb4,0x1b,
3680 0xdb,0x27,0xec,0x5f,0x95,0x04,0x3f,0x93,0x4a,0x30,0xf5,0x97,0x18,0xb3,0xa7,
3681 0xf9,0x19,0xa7,0x93,0x33,0x1d,0x01,0xc8,0xdb,0x22,0x52,0x5c,0xd7,0x25,0xc9,
3682 0x46,0xf9,0xa2,0xfb,0x87,0x59,0x43,0xbe,0x9b,0x62,0xb1,0x8d,0x2d,0x86,0x44,
3683 0x1a,0x46,0xac,0x78,0x61,0x7e,0x30,0x09,0xfa,0xae,0x89,0xc4,0x41,0x2a,0x22,
3684 0x66,0x03,0x91,0x39,0x45,0x9c,0xc7,0x8b,0x0c,0xa8,0xca,0x0d,0x2f,0xfb,0x52,
3685 0xea,0x0c,0xf7,0x63,0x33,0x23,0x9d,0xfe,0xb0,0x1f,0xad,0x67,0xd6,0xa7,0x50,
3686 0x03,0xc6,0x04,0x70,0x63,0xb5,0x2c,0xb1,0x86,0x5a,0x43,0xb7,0xfb,0xae,0xf9,
3687 0x6e,0x29,0x6e,0x21,0x21,0x41,0x26,0x06,0x8c,0xc9,0xc3,0xee,0xb0,0xc2,0x85,
3688 0x93,0xa1,0xb9,0x85,0xd9,0xe6,0x32,0x6c,0x4b,0x4c,0x3f,0xd6,0x5d,0xa3,0xe5,
3689 0xb5,0x9d,0x77,0xc3,0x9c,0xc0,0x55,0xb7,0x74,0x00,0xe3,0xb8,0x38,0xab,0x83,
3690 0x97,0x50,0xe1,0x9a,0x42,0x24,0x1d,0xc6,0xc0,0xa3,0x30,0xd1,0x1a,0x5a,0xc8,
3691 0x52,0x34,0xf7,0x73,0xf1,0xc7,0x18,0x1f,0x33,0xad,0x7a,0xec,0xcb,0x41,0x60,
3692 0xf3,0x23,0x94,0x20,0xc2,0x48,0x45,0xac,0x5c,0x51,0xc6,0x2e,0x80,0xc2,0xe2,
3693 0x77,0x15,0xbd,0x85,0x87,0xed,0x36,0x9d,0x96,0x91,0xee,0x00,0xb5,0xa3,0x70,
3694 0xec,0x9f,0xe3,0x8d,0x80,0x68,0x83,0x76,0xba,0xaf,0x5d,0x70,0x52,0x22,0x16,
3695 0xe2,0x66,0xfb,0xba,0xb3,0xc5,0xc2,0xf7,0x3e,0x2f,0x77,0xa6,0xca,0xde,0xc1,
3696 0xa6,0xc6,0x48,0x4c,0xc3,0x37,0x51,0x23,0xd3,0x27,0xd7,0xb8,0x4e,0x70,0x96,
3697 0xf0,0xa1,0x44,0x76,0xaf,0x78,0xcf,0x9a,0xe1,0x66,0x13,0x02,0x03,0x01,0x00,
3700 static BOOL WINAPI
verify_ms_root_policy(LPCSTR szPolicyOID
,
3701 PCCERT_CHAIN_CONTEXT pChainContext
, PCERT_CHAIN_POLICY_PARA pPolicyPara
,
3702 PCERT_CHAIN_POLICY_STATUS pPolicyStatus
)
3704 BOOL isMSRoot
= FALSE
;
3706 CERT_PUBLIC_KEY_INFO msPubKey
= { { 0 } };
3708 CRYPT_DATA_BLOB keyBlobs
[] = {
3709 { sizeof(msPubKey1
), msPubKey1
},
3710 { sizeof(msPubKey2
), msPubKey2
},
3711 { sizeof(msPubKey3
), msPubKey3
},
3712 { sizeof(msPubKey4
), msPubKey4
},
3714 PCERT_SIMPLE_CHAIN rootChain
=
3715 pChainContext
->rgpChain
[pChainContext
->cChain
- 1];
3716 PCCERT_CONTEXT root
=
3717 rootChain
->rgpElement
[rootChain
->cElement
- 1]->pCertContext
;
3719 for (i
= 0; !isMSRoot
&& i
< ARRAY_SIZE(keyBlobs
); i
++)
3721 msPubKey
.PublicKey
.cbData
= keyBlobs
[i
].cbData
;
3722 msPubKey
.PublicKey
.pbData
= keyBlobs
[i
].pbData
;
3723 if (CertComparePublicKeyInfo(X509_ASN_ENCODING
| PKCS_7_ASN_ENCODING
,
3724 &root
->pCertInfo
->SubjectPublicKeyInfo
, &msPubKey
)) isMSRoot
= TRUE
;
3727 pPolicyStatus
->lChainIndex
= pPolicyStatus
->lElementIndex
= 0;
3730 pPolicyStatus
->dwError
= 0;
3732 pPolicyStatus
->dwError
= CERT_E_UNTRUSTEDROOT
;
3737 typedef BOOL (WINAPI
*CertVerifyCertificateChainPolicyFunc
)(LPCSTR szPolicyOID
,
3738 PCCERT_CHAIN_CONTEXT pChainContext
, PCERT_CHAIN_POLICY_PARA pPolicyPara
,
3739 PCERT_CHAIN_POLICY_STATUS pPolicyStatus
);
3741 static void dump_policy_para(PCERT_CHAIN_POLICY_PARA para
)
3745 TRACE_(chain
)("cbSize = %d\n", para
->cbSize
);
3746 TRACE_(chain
)("dwFlags = %08x\n", para
->dwFlags
);
3747 TRACE_(chain
)("pvExtraPolicyPara = %p\n", para
->pvExtraPolicyPara
);
3751 BOOL WINAPI
CertVerifyCertificateChainPolicy(LPCSTR szPolicyOID
,
3752 PCCERT_CHAIN_CONTEXT pChainContext
, PCERT_CHAIN_POLICY_PARA pPolicyPara
,
3753 PCERT_CHAIN_POLICY_STATUS pPolicyStatus
)
3755 static HCRYPTOIDFUNCSET set
= NULL
;
3757 CertVerifyCertificateChainPolicyFunc verifyPolicy
= NULL
;
3758 HCRYPTOIDFUNCADDR hFunc
= NULL
;
3760 TRACE("(%s, %p, %p, %p)\n", debugstr_a(szPolicyOID
), pChainContext
,
3761 pPolicyPara
, pPolicyStatus
);
3762 if (TRACE_ON(chain
))
3763 dump_policy_para(pPolicyPara
);
3765 if (IS_INTOID(szPolicyOID
))
3767 switch (LOWORD(szPolicyOID
))
3769 case LOWORD(CERT_CHAIN_POLICY_BASE
):
3770 verifyPolicy
= verify_base_policy
;
3772 case LOWORD(CERT_CHAIN_POLICY_AUTHENTICODE
):
3773 verifyPolicy
= verify_authenticode_policy
;
3775 case LOWORD(CERT_CHAIN_POLICY_SSL
):
3776 verifyPolicy
= verify_ssl_policy
;
3778 case LOWORD(CERT_CHAIN_POLICY_BASIC_CONSTRAINTS
):
3779 verifyPolicy
= verify_basic_constraints_policy
;
3781 case LOWORD(CERT_CHAIN_POLICY_MICROSOFT_ROOT
):
3782 verifyPolicy
= verify_ms_root_policy
;
3785 FIXME("unimplemented for %d\n", LOWORD(szPolicyOID
));
3791 set
= CryptInitOIDFunctionSet(
3792 CRYPT_OID_VERIFY_CERTIFICATE_CHAIN_POLICY_FUNC
, 0);
3793 CryptGetOIDFunctionAddress(set
, X509_ASN_ENCODING
, szPolicyOID
, 0,
3794 (void **)&verifyPolicy
, &hFunc
);
3797 ret
= verifyPolicy(szPolicyOID
, pChainContext
, pPolicyPara
,
3800 CryptFreeOIDFunctionAddress(hFunc
, 0);
3801 TRACE("returning %d (%08x)\n", ret
, pPolicyStatus
->dwError
);