source: trunk/src/crypt32/chain.c@ 21329

Last change on this file since 21329 was 21311, checked in by vladest, 16 years ago

Added CRYPT32 and MSCMS APIs support

File size: 65.1 KB
Line 
1/*
2 * Copyright 2006 Juan Lang
3 *
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.
8 *
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.
13 *
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
17 *
18 */
19#include <stdarg.h>
20#define NONAMELESSUNION
21#include "windef.h"
22#include "winbase.h"
23#define CERT_CHAIN_PARA_HAS_EXTRA_FIELDS
24#define CERT_REVOCATION_PARA_HAS_EXTRA_FIELDS
25#include "winerror.h"
26#include "wincrypt.h"
27#include "wine/debug.h"
28#include "wine/unicode.h"
29#include "crypt32_private.h"
30
31WINE_DEFAULT_DEBUG_CHANNEL(crypt);
32
33#define DEFAULT_CYCLE_MODULUS 7
34
35static HCERTCHAINENGINE CRYPT_defaultChainEngine;
36
37/* This represents a subset of a certificate chain engine: it doesn't include
38 * the "hOther" store described by MSDN, because I'm not sure how that's used.
39 * It also doesn't include the "hTrust" store, because I don't yet implement
40 * CTLs or complex certificate chains.
41 */
42typedef struct _CertificateChainEngine
43{
44 LONG ref;
45 HCERTSTORE hRoot;
46 HCERTSTORE hWorld;
47 DWORD dwFlags;
48 DWORD dwUrlRetrievalTimeout;
49 DWORD MaximumCachedCertificates;
50 DWORD CycleDetectionModulus;
51} CertificateChainEngine, *PCertificateChainEngine;
52
53static inline void CRYPT_AddStoresToCollection(HCERTSTORE collection,
54 DWORD cStores, HCERTSTORE *stores)
55{
56 DWORD i;
57
58 for (i = 0; i < cStores; i++)
59 CertAddStoreToCollection(collection, stores[i], 0, 0);
60}
61
62static inline void CRYPT_CloseStores(DWORD cStores, HCERTSTORE *stores)
63{
64 DWORD i;
65
66 for (i = 0; i < cStores; i++)
67 CertCloseStore(stores[i], 0);
68}
69
70static const WCHAR rootW[] = { 'R','o','o','t',0 };
71
72static BOOL CRYPT_CheckRestrictedRoot(HCERTSTORE store)
73{
74 BOOL ret = TRUE;
75
76 if (store)
77 {
78 HCERTSTORE rootStore = CertOpenSystemStoreW(0, rootW);
79 PCCERT_CONTEXT cert = NULL, check;
80 BYTE hash[20];
81 DWORD size;
82
83 do {
84 cert = CertEnumCertificatesInStore(store, cert);
85 if (cert)
86 {
87 size = sizeof(hash);
88
89 ret = CertGetCertificateContextProperty(cert, CERT_HASH_PROP_ID,
90 hash, &size);
91 if (ret)
92 {
93 CRYPT_HASH_BLOB blob = { sizeof(hash), hash };
94
95 check = CertFindCertificateInStore(rootStore,
96 cert->dwCertEncodingType, 0, CERT_FIND_SHA1_HASH, &blob,
97 NULL);
98 if (!check)
99 ret = FALSE;
100 else
101 CertFreeCertificateContext(check);
102 }
103 }
104 } while (ret && cert);
105 if (cert)
106 CertFreeCertificateContext(cert);
107 CertCloseStore(rootStore, 0);
108 }
109 return ret;
110}
111
112HCERTCHAINENGINE CRYPT_CreateChainEngine(HCERTSTORE root,
113 PCERT_CHAIN_ENGINE_CONFIG pConfig)
114{
115 static const WCHAR caW[] = { 'C','A',0 };
116 static const WCHAR myW[] = { 'M','y',0 };
117 static const WCHAR trustW[] = { 'T','r','u','s','t',0 };
118 PCertificateChainEngine engine =
119 CryptMemAlloc(sizeof(CertificateChainEngine));
120
121 if (engine)
122 {
123 HCERTSTORE worldStores[4];
124
125 engine->ref = 1;
126 engine->hRoot = root;
127 engine->hWorld = CertOpenStore(CERT_STORE_PROV_COLLECTION, 0, 0,
128 CERT_STORE_CREATE_NEW_FLAG, NULL);
129 worldStores[0] = CertDuplicateStore(engine->hRoot);
130 worldStores[1] = CertOpenSystemStoreW(0, caW);
131 worldStores[2] = CertOpenSystemStoreW(0, myW);
132 worldStores[3] = CertOpenSystemStoreW(0, trustW);
133 CRYPT_AddStoresToCollection(engine->hWorld,
134 sizeof(worldStores) / sizeof(worldStores[0]), worldStores);
135 CRYPT_AddStoresToCollection(engine->hWorld,
136 pConfig->cAdditionalStore, pConfig->rghAdditionalStore);
137 CRYPT_CloseStores(sizeof(worldStores) / sizeof(worldStores[0]),
138 worldStores);
139 engine->dwFlags = pConfig->dwFlags;
140 engine->dwUrlRetrievalTimeout = pConfig->dwUrlRetrievalTimeout;
141 engine->MaximumCachedCertificates =
142 pConfig->MaximumCachedCertificates;
143 if (pConfig->CycleDetectionModulus)
144 engine->CycleDetectionModulus = pConfig->CycleDetectionModulus;
145 else
146 engine->CycleDetectionModulus = DEFAULT_CYCLE_MODULUS;
147 }
148 return (HCERTCHAINENGINE)engine;
149}
150
151BOOL WINAPI CertCreateCertificateChainEngine(PCERT_CHAIN_ENGINE_CONFIG pConfig,
152 HCERTCHAINENGINE *phChainEngine)
153{
154 BOOL ret;
155
156 TRACE("(%p, %p)\n", pConfig, phChainEngine);
157
158 if (pConfig->cbSize != sizeof(*pConfig))
159 {
160 SetLastError(E_INVALIDARG);
161 return FALSE;
162 }
163 *phChainEngine = NULL;
164 ret = CRYPT_CheckRestrictedRoot(pConfig->hRestrictedRoot);
165 if (ret)
166 {
167 HCERTSTORE root;
168 HCERTCHAINENGINE engine;
169
170 if (pConfig->hRestrictedRoot)
171 root = CertDuplicateStore(pConfig->hRestrictedRoot);
172 else
173 root = CertOpenSystemStoreW(0, rootW);
174 engine = CRYPT_CreateChainEngine(root, pConfig);
175 if (engine)
176 {
177 *phChainEngine = engine;
178 ret = TRUE;
179 }
180 else
181 ret = FALSE;
182 }
183 return ret;
184}
185
186VOID WINAPI CertFreeCertificateChainEngine(HCERTCHAINENGINE hChainEngine)
187{
188 PCertificateChainEngine engine = (PCertificateChainEngine)hChainEngine;
189
190 TRACE("(%p)\n", hChainEngine);
191
192 if (engine && InterlockedDecrement(&engine->ref) == 0)
193 {
194 CertCloseStore(engine->hWorld, 0);
195 CertCloseStore(engine->hRoot, 0);
196 CryptMemFree(engine);
197 }
198}
199
200static HCERTCHAINENGINE CRYPT_GetDefaultChainEngine(void)
201{
202 if (!CRYPT_defaultChainEngine)
203 {
204 CERT_CHAIN_ENGINE_CONFIG config = { 0 };
205 HCERTCHAINENGINE engine;
206
207 config.cbSize = sizeof(config);
208 CertCreateCertificateChainEngine(&config, &engine);
209 InterlockedCompareExchangePointer(&CRYPT_defaultChainEngine, engine,
210 NULL);
211 if (CRYPT_defaultChainEngine != engine)
212 CertFreeCertificateChainEngine(engine);
213 }
214 return CRYPT_defaultChainEngine;
215}
216
217void default_chain_engine_free(void)
218{
219 CertFreeCertificateChainEngine(CRYPT_defaultChainEngine);
220}
221
222typedef struct _CertificateChain
223{
224 CERT_CHAIN_CONTEXT context;
225 HCERTSTORE world;
226 LONG ref;
227} CertificateChain, *PCertificateChain;
228
229static inline BOOL CRYPT_IsCertificateSelfSigned(PCCERT_CONTEXT cert)
230{
231 return CertCompareCertificateName(cert->dwCertEncodingType,
232 &cert->pCertInfo->Subject, &cert->pCertInfo->Issuer);
233}
234
235static void CRYPT_FreeChainElement(PCERT_CHAIN_ELEMENT element)
236{
237 CertFreeCertificateContext(element->pCertContext);
238 CryptMemFree(element);
239}
240
241static void CRYPT_CheckSimpleChainForCycles(PCERT_SIMPLE_CHAIN chain)
242{
243 DWORD i, j, cyclicCertIndex = 0;
244
245 /* O(n^2) - I don't think there's a faster way */
246 for (i = 0; !cyclicCertIndex && i < chain->cElement; i++)
247 for (j = i + 1; !cyclicCertIndex && j < chain->cElement; j++)
248 if (CertCompareCertificate(X509_ASN_ENCODING,
249 chain->rgpElement[i]->pCertContext->pCertInfo,
250 chain->rgpElement[j]->pCertContext->pCertInfo))
251 cyclicCertIndex = j;
252 if (cyclicCertIndex)
253 {
254 chain->rgpElement[cyclicCertIndex]->TrustStatus.dwErrorStatus
255 |= CERT_TRUST_IS_CYCLIC | CERT_TRUST_INVALID_BASIC_CONSTRAINTS;
256 /* Release remaining certs */
257 for (i = cyclicCertIndex + 1; i < chain->cElement; i++)
258 CRYPT_FreeChainElement(chain->rgpElement[i]);
259 /* Truncate chain */
260 chain->cElement = cyclicCertIndex + 1;
261 }
262}
263
264/* Checks whether the chain is cyclic by examining the last element's status */
265static inline BOOL CRYPT_IsSimpleChainCyclic(PCERT_SIMPLE_CHAIN chain)
266{
267 if (chain->cElement)
268 return chain->rgpElement[chain->cElement - 1]->TrustStatus.dwErrorStatus
269 & CERT_TRUST_IS_CYCLIC;
270 else
271 return FALSE;
272}
273
274static inline void CRYPT_CombineTrustStatus(CERT_TRUST_STATUS *chainStatus,
275 CERT_TRUST_STATUS *elementStatus)
276{
277 /* Any error that applies to an element also applies to a chain.. */
278 chainStatus->dwErrorStatus |= elementStatus->dwErrorStatus;
279 /* but the bottom nibble of an element's info status doesn't apply to the
280 * chain.
281 */
282 chainStatus->dwInfoStatus |= (elementStatus->dwInfoStatus & 0xfffffff0);
283}
284
285static BOOL CRYPT_AddCertToSimpleChain(PCertificateChainEngine engine,
286 PCERT_SIMPLE_CHAIN chain, PCCERT_CONTEXT cert, DWORD subjectInfoStatus)
287{
288 BOOL ret = FALSE;
289 PCERT_CHAIN_ELEMENT element = CryptMemAlloc(sizeof(CERT_CHAIN_ELEMENT));
290
291 if (element)
292 {
293 if (!chain->cElement)
294 chain->rgpElement = CryptMemAlloc(sizeof(PCERT_CHAIN_ELEMENT));
295 else
296 chain->rgpElement = CryptMemRealloc(chain->rgpElement,
297 (chain->cElement + 1) * sizeof(PCERT_CHAIN_ELEMENT));
298 if (chain->rgpElement)
299 {
300 chain->rgpElement[chain->cElement++] = element;
301 memset(element, 0, sizeof(CERT_CHAIN_ELEMENT));
302 element->cbSize = sizeof(CERT_CHAIN_ELEMENT);
303 element->pCertContext = CertDuplicateCertificateContext(cert);
304 if (chain->cElement > 1)
305 chain->rgpElement[chain->cElement - 2]->TrustStatus.dwInfoStatus
306 = subjectInfoStatus;
307 /* FIXME: initialize the rest of element */
308 if (!(chain->cElement % engine->CycleDetectionModulus))
309 CRYPT_CheckSimpleChainForCycles(chain);
310 CRYPT_CombineTrustStatus(&chain->TrustStatus,
311 &element->TrustStatus);
312 ret = TRUE;
313 }
314 else
315 CryptMemFree(element);
316 }
317 return ret;
318}
319
320static void CRYPT_FreeSimpleChain(PCERT_SIMPLE_CHAIN chain)
321{
322 DWORD i;
323
324 for (i = 0; i < chain->cElement; i++)
325 CRYPT_FreeChainElement(chain->rgpElement[i]);
326 CryptMemFree(chain->rgpElement);
327 CryptMemFree(chain);
328}
329
330static void CRYPT_CheckTrustedStatus(HCERTSTORE hRoot,
331 PCERT_CHAIN_ELEMENT rootElement)
332{
333 BYTE hash[20];
334 DWORD size = sizeof(hash);
335 CRYPT_HASH_BLOB blob = { sizeof(hash), hash };
336 PCCERT_CONTEXT trustedRoot;
337
338 CertGetCertificateContextProperty(rootElement->pCertContext,
339 CERT_HASH_PROP_ID, hash, &size);
340 trustedRoot = CertFindCertificateInStore(hRoot,
341 rootElement->pCertContext->dwCertEncodingType, 0, CERT_FIND_SHA1_HASH,
342 &blob, NULL);
343 if (!trustedRoot)
344 rootElement->TrustStatus.dwErrorStatus |=
345 CERT_TRUST_IS_UNTRUSTED_ROOT;
346 else
347 CertFreeCertificateContext(trustedRoot);
348}
349
350static void CRYPT_CheckRootCert(HCERTCHAINENGINE hRoot,
351 PCERT_CHAIN_ELEMENT rootElement)
352{
353 PCCERT_CONTEXT root = rootElement->pCertContext;
354
355 if (!CryptVerifyCertificateSignatureEx(0, root->dwCertEncodingType,
356 CRYPT_VERIFY_CERT_SIGN_SUBJECT_CERT, (void *)root,
357 CRYPT_VERIFY_CERT_SIGN_ISSUER_CERT, (void *)root, 0, NULL))
358 {
359 TRACE("Last certificate's signature is invalid\n");
360 rootElement->TrustStatus.dwErrorStatus |=
361 CERT_TRUST_IS_NOT_SIGNATURE_VALID;
362 }
363 CRYPT_CheckTrustedStatus(hRoot, rootElement);
364}
365
366/* Decodes a cert's basic constraints extension (either szOID_BASIC_CONSTRAINTS
367 * or szOID_BASIC_CONSTRAINTS2, whichever is present) into a
368 * CERT_BASIC_CONSTRAINTS2_INFO. If it neither extension is present, sets
369 * constraints->fCA to defaultIfNotSpecified.
370 * Returns FALSE if the extension is present but couldn't be decoded.
371 */
372static BOOL CRYPT_DecodeBasicConstraints(PCCERT_CONTEXT cert,
373 CERT_BASIC_CONSTRAINTS2_INFO *constraints, BOOL defaultIfNotSpecified)
374{
375 BOOL ret = TRUE;
376 PCERT_EXTENSION ext = CertFindExtension(szOID_BASIC_CONSTRAINTS,
377 cert->pCertInfo->cExtension, cert->pCertInfo->rgExtension);
378
379 constraints->fPathLenConstraint = FALSE;
380 if (ext)
381 {
382 CERT_BASIC_CONSTRAINTS_INFO *info;
383 DWORD size = 0;
384
385 ret = CryptDecodeObjectEx(X509_ASN_ENCODING, szOID_BASIC_CONSTRAINTS,
386 ext->Value.pbData, ext->Value.cbData, CRYPT_DECODE_ALLOC_FLAG,
387 NULL, (LPBYTE)&info, &size);
388 if (ret)
389 {
390 if (info->SubjectType.cbData == 1)
391 constraints->fCA =
392 info->SubjectType.pbData[0] & CERT_CA_SUBJECT_FLAG;
393 LocalFree(info);
394 }
395 }
396 else
397 {
398 ext = CertFindExtension(szOID_BASIC_CONSTRAINTS2,
399 cert->pCertInfo->cExtension, cert->pCertInfo->rgExtension);
400 if (ext)
401 {
402 DWORD size = sizeof(CERT_BASIC_CONSTRAINTS2_INFO);
403
404 ret = CryptDecodeObjectEx(X509_ASN_ENCODING,
405 szOID_BASIC_CONSTRAINTS2, ext->Value.pbData, ext->Value.cbData,
406 0, NULL, constraints, &size);
407 }
408 else
409 constraints->fCA = defaultIfNotSpecified;
410 }
411 return ret;
412}
413
414/* Checks element's basic constraints to see if it can act as a CA, with
415 * remainingCAs CAs left in this chain. Updates chainConstraints with the
416 * element's constraints, if:
417 * 1. chainConstraints doesn't have a path length constraint, or
418 * 2. element's path length constraint is smaller than chainConstraints's
419 * Sets *pathLengthConstraintViolated to TRUE if a path length violation
420 * occurs.
421 * Returns TRUE if the element can be a CA, and the length of the remaining
422 * chain is valid.
423 */
424static BOOL CRYPT_CheckBasicConstraintsForCA(PCCERT_CONTEXT cert,
425 CERT_BASIC_CONSTRAINTS2_INFO *chainConstraints, DWORD remainingCAs,
426 BOOL *pathLengthConstraintViolated)
427{
428 BOOL validBasicConstraints;
429 CERT_BASIC_CONSTRAINTS2_INFO constraints;
430
431 if ((validBasicConstraints = CRYPT_DecodeBasicConstraints(cert,
432 &constraints, TRUE)))
433 {
434 if (!constraints.fCA)
435 {
436 TRACE("chain element %d can't be a CA\n", remainingCAs + 1);
437 validBasicConstraints = FALSE;
438 }
439 else if (constraints.fPathLenConstraint)
440 {
441 /* If the element has path length constraints, they apply to the
442 * entire remaining chain.
443 */
444 if (!chainConstraints->fPathLenConstraint ||
445 constraints.dwPathLenConstraint <
446 chainConstraints->dwPathLenConstraint)
447 {
448 TRACE("setting path length constraint to %d\n",
449 chainConstraints->dwPathLenConstraint);
450 chainConstraints->fPathLenConstraint = TRUE;
451 chainConstraints->dwPathLenConstraint =
452 constraints.dwPathLenConstraint;
453 }
454 }
455 }
456 if (chainConstraints->fPathLenConstraint &&
457 remainingCAs > chainConstraints->dwPathLenConstraint)
458 {
459 TRACE("remaining CAs %d exceed max path length %d\n", remainingCAs,
460 chainConstraints->dwPathLenConstraint);
461 validBasicConstraints = FALSE;
462 *pathLengthConstraintViolated = TRUE;
463 }
464 return validBasicConstraints;
465}
466
467static BOOL url_matches(LPCWSTR constraint, LPCWSTR name,
468 DWORD *trustErrorStatus)
469{
470 BOOL match = FALSE;
471
472 TRACE("%s, %s\n", debugstr_w(constraint), debugstr_w(name));
473
474 if (!constraint)
475 *trustErrorStatus |= CERT_TRUST_INVALID_NAME_CONSTRAINTS;
476 else if (!name)
477 ; /* no match */
478 else if (constraint[0] == '.')
479 {
480 if (lstrlenW(name) > lstrlenW(constraint))
481 match = !lstrcmpiW(name + lstrlenW(name) - lstrlenW(constraint),
482 constraint);
483 }
484 else
485 match = !lstrcmpiW(constraint, name);
486 return match;
487}
488
489static BOOL rfc822_name_matches(LPCWSTR constraint, LPCWSTR name,
490 DWORD *trustErrorStatus)
491{
492 BOOL match = FALSE;
493 LPCWSTR at;
494
495 TRACE("%s, %s\n", debugstr_w(constraint), debugstr_w(name));
496
497 if (!constraint)
498 *trustErrorStatus |= CERT_TRUST_INVALID_NAME_CONSTRAINTS;
499 else if (!name)
500 ; /* no match */
501 else if ((at = strchrW(constraint, '@')))
502 match = !lstrcmpiW(constraint, name);
503 else
504 {
505 if ((at = strchrW(name, '@')))
506 match = url_matches(constraint, at + 1, trustErrorStatus);
507 else
508 match = !lstrcmpiW(constraint, name);
509 }
510 return match;
511}
512
513static BOOL dns_name_matches(LPCWSTR constraint, LPCWSTR name,
514 DWORD *trustErrorStatus)
515{
516 BOOL match = FALSE;
517
518 TRACE("%s, %s\n", debugstr_w(constraint), debugstr_w(name));
519
520 if (!constraint)
521 *trustErrorStatus |= CERT_TRUST_INVALID_NAME_CONSTRAINTS;
522 else if (!name)
523 ; /* no match */
524 else if (lstrlenW(name) >= lstrlenW(constraint))
525 match = !lstrcmpiW(name + lstrlenW(name) - lstrlenW(constraint),
526 constraint);
527 /* else: name is too short, no match */
528
529 return match;
530}
531
532static BOOL ip_address_matches(const CRYPT_DATA_BLOB *constraint,
533 const CRYPT_DATA_BLOB *name, DWORD *trustErrorStatus)
534{
535 BOOL match = FALSE;
536
537 TRACE("(%d, %p), (%d, %p)\n", constraint->cbData, constraint->pbData,
538 name->cbData, name->pbData);
539
540 if (constraint->cbData != sizeof(DWORD) * 2)
541 *trustErrorStatus |= CERT_TRUST_INVALID_NAME_CONSTRAINTS;
542 else if (name->cbData == sizeof(DWORD))
543 {
544 DWORD subnet, mask, addr;
545
546 memcpy(&subnet, constraint->pbData, sizeof(subnet));
547 memcpy(&mask, constraint->pbData + sizeof(subnet), sizeof(mask));
548 memcpy(&addr, name->pbData, sizeof(addr));
549 /* These are really in big-endian order, but for equality matching we
550 * don't need to swap to host order
551 */
552 match = (subnet & mask) == (addr & mask);
553 }
554 /* else: name is wrong size, no match */
555
556 return match;
557}
558
559static void CRYPT_FindMatchingNameEntry(const CERT_ALT_NAME_ENTRY *constraint,
560 const CERT_ALT_NAME_INFO *subjectName, DWORD *trustErrorStatus,
561 DWORD errorIfFound, DWORD errorIfNotFound)
562{
563 DWORD i;
564 BOOL match = FALSE;
565
566 for (i = 0; i < subjectName->cAltEntry; i++)
567 {
568 if (subjectName->rgAltEntry[i].dwAltNameChoice ==
569 constraint->dwAltNameChoice)
570 {
571 switch (constraint->dwAltNameChoice)
572 {
573 case CERT_ALT_NAME_RFC822_NAME:
574 match = rfc822_name_matches(constraint->u.pwszURL,
575 subjectName->rgAltEntry[i].u.pwszURL, trustErrorStatus);
576 break;
577 case CERT_ALT_NAME_DNS_NAME:
578 match = dns_name_matches(constraint->u.pwszURL,
579 subjectName->rgAltEntry[i].u.pwszURL, trustErrorStatus);
580 break;
581 case CERT_ALT_NAME_URL:
582 match = url_matches(constraint->u.pwszURL,
583 subjectName->rgAltEntry[i].u.pwszURL, trustErrorStatus);
584 break;
585 case CERT_ALT_NAME_IP_ADDRESS:
586 match = ip_address_matches(&constraint->u.IPAddress,
587 &subjectName->rgAltEntry[i].u.IPAddress, trustErrorStatus);
588 break;
589 case CERT_ALT_NAME_DIRECTORY_NAME:
590 default:
591 ERR("name choice %d unsupported in this context\n",
592 constraint->dwAltNameChoice);
593 *trustErrorStatus |=
594 CERT_TRUST_HAS_NOT_SUPPORTED_NAME_CONSTRAINT;
595 }
596 }
597 }
598 *trustErrorStatus |= match ? errorIfFound : errorIfNotFound;
599}
600
601static void CRYPT_CheckNameConstraints(
602 const CERT_NAME_CONSTRAINTS_INFO *nameConstraints, const CERT_INFO *cert,
603 DWORD *trustErrorStatus)
604{
605 /* If there aren't any existing constraints, don't bother checking */
606 if (nameConstraints->cPermittedSubtree || nameConstraints->cExcludedSubtree)
607 {
608 CERT_EXTENSION *ext;
609
610 if ((ext = CertFindExtension(szOID_SUBJECT_ALT_NAME, cert->cExtension,
611 cert->rgExtension)))
612 {
613 CERT_ALT_NAME_INFO *subjectName;
614 DWORD size;
615
616 if (CryptDecodeObjectEx(X509_ASN_ENCODING, X509_ALTERNATE_NAME,
617 ext->Value.pbData, ext->Value.cbData,
618 CRYPT_DECODE_ALLOC_FLAG | CRYPT_DECODE_NOCOPY_FLAG, NULL,
619 &subjectName, &size))
620 {
621 DWORD i;
622
623 for (i = 0; i < nameConstraints->cExcludedSubtree; i++)
624 CRYPT_FindMatchingNameEntry(
625 &nameConstraints->rgExcludedSubtree[i].Base, subjectName,
626 trustErrorStatus,
627 CERT_TRUST_HAS_EXCLUDED_NAME_CONSTRAINT, 0);
628 for (i = 0; i < nameConstraints->cPermittedSubtree; i++)
629 CRYPT_FindMatchingNameEntry(
630 &nameConstraints->rgPermittedSubtree[i].Base, subjectName,
631 trustErrorStatus,
632 0, CERT_TRUST_HAS_NOT_PERMITTED_NAME_CONSTRAINT);
633 LocalFree(subjectName);
634 }
635 }
636 else
637 {
638 if (nameConstraints->cPermittedSubtree)
639 *trustErrorStatus |=
640 CERT_TRUST_HAS_NOT_PERMITTED_NAME_CONSTRAINT;
641 if (nameConstraints->cExcludedSubtree)
642 *trustErrorStatus |=
643 CERT_TRUST_HAS_EXCLUDED_NAME_CONSTRAINT;
644 }
645 }
646}
647
648/* Gets cert's name constraints, if any. Free with LocalFree. */
649static CERT_NAME_CONSTRAINTS_INFO *CRYPT_GetNameConstraints(CERT_INFO *cert)
650{
651 CERT_NAME_CONSTRAINTS_INFO *info = NULL;
652
653 CERT_EXTENSION *ext;
654
655 if ((ext = CertFindExtension(szOID_NAME_CONSTRAINTS, cert->cExtension,
656 cert->rgExtension)))
657 {
658 DWORD size;
659
660 CryptDecodeObjectEx(X509_ASN_ENCODING, X509_NAME_CONSTRAINTS,
661 ext->Value.pbData, ext->Value.cbData,
662 CRYPT_DECODE_ALLOC_FLAG | CRYPT_DECODE_NOCOPY_FLAG, NULL, &info,
663 &size);
664 }
665 return info;
666}
667
668static void CRYPT_CheckChainNameConstraints(PCERT_SIMPLE_CHAIN chain)
669{
670 int i, j;
671
672 /* Microsoft's implementation appears to violate RFC 3280: according to
673 * MSDN, the various CERT_TRUST_*_NAME_CONSTRAINT errors are set if a CA's
674 * name constraint is violated in the end cert. According to RFC 3280,
675 * the constraints should be checked against every subsequent certificate
676 * in the chain, not just the end cert.
677 * Microsoft's implementation also sets the name constraint errors on the
678 * certs whose constraints were violated, not on the certs that violated
679 * them.
680 * In order to be error-compatible with Microsoft's implementation, while
681 * still adhering to RFC 3280, I use a O(n ^ 2) algorithm to check name
682 * constraints.
683 */
684 for (i = chain->cElement - 1; i > 0; i--)
685 {
686 CERT_NAME_CONSTRAINTS_INFO *nameConstraints;
687
688 if ((nameConstraints = CRYPT_GetNameConstraints(
689 chain->rgpElement[i]->pCertContext->pCertInfo)))
690 {
691 for (j = i - 1; j >= 0; j--)
692 {
693 DWORD errorStatus = 0;
694
695 /* According to RFC 3280, self-signed certs don't have name
696 * constraints checked unless they're the end cert.
697 */
698 if (j == 0 || !CRYPT_IsCertificateSelfSigned(
699 chain->rgpElement[j]->pCertContext))
700 {
701 CRYPT_CheckNameConstraints(nameConstraints,
702 chain->rgpElement[i]->pCertContext->pCertInfo,
703 &errorStatus);
704 chain->rgpElement[i]->TrustStatus.dwErrorStatus |=
705 errorStatus;
706 }
707 }
708 LocalFree(nameConstraints);
709 }
710 }
711}
712
713static void CRYPT_CheckSimpleChain(PCertificateChainEngine engine,
714 PCERT_SIMPLE_CHAIN chain, LPFILETIME time)
715{
716 PCERT_CHAIN_ELEMENT rootElement = chain->rgpElement[chain->cElement - 1];
717 int i;
718 BOOL pathLengthConstraintViolated = FALSE;
719 CERT_BASIC_CONSTRAINTS2_INFO constraints = { TRUE, FALSE, 0 };
720
721 for (i = chain->cElement - 1; i >= 0; i--)
722 {
723 if (CertVerifyTimeValidity(time,
724 chain->rgpElement[i]->pCertContext->pCertInfo) != 0)
725 chain->rgpElement[i]->TrustStatus.dwErrorStatus |=
726 CERT_TRUST_IS_NOT_TIME_VALID;
727 if (i != 0)
728 {
729 /* Check the signature of the cert this issued */
730 if (!CryptVerifyCertificateSignatureEx(0, X509_ASN_ENCODING,
731 CRYPT_VERIFY_CERT_SIGN_SUBJECT_CERT,
732 (void *)chain->rgpElement[i - 1]->pCertContext,
733 CRYPT_VERIFY_CERT_SIGN_ISSUER_CERT,
734 (void *)chain->rgpElement[i]->pCertContext, 0, NULL))
735 chain->rgpElement[i - 1]->TrustStatus.dwErrorStatus |=
736 CERT_TRUST_IS_NOT_SIGNATURE_VALID;
737 /* Once a path length constraint has been violated, every remaining
738 * CA cert's basic constraints is considered invalid.
739 */
740 if (pathLengthConstraintViolated)
741 chain->rgpElement[i]->TrustStatus.dwErrorStatus |=
742 CERT_TRUST_INVALID_BASIC_CONSTRAINTS;
743 else if (!CRYPT_CheckBasicConstraintsForCA(
744 chain->rgpElement[i]->pCertContext, &constraints, i - 1,
745 &pathLengthConstraintViolated))
746 chain->rgpElement[i]->TrustStatus.dwErrorStatus |=
747 CERT_TRUST_INVALID_BASIC_CONSTRAINTS;
748 else if (constraints.fPathLenConstraint &&
749 constraints.dwPathLenConstraint)
750 {
751 /* This one's valid - decrement max length */
752 constraints.dwPathLenConstraint--;
753 }
754 }
755 if (CRYPT_IsSimpleChainCyclic(chain))
756 {
757 /* If the chain is cyclic, then the path length constraints
758 * are violated, because the chain is infinitely long.
759 */
760 pathLengthConstraintViolated = TRUE;
761 chain->TrustStatus.dwErrorStatus |=
762 CERT_TRUST_IS_PARTIAL_CHAIN |
763 CERT_TRUST_INVALID_BASIC_CONSTRAINTS;
764 }
765 /* FIXME: check valid usages */
766 CRYPT_CombineTrustStatus(&chain->TrustStatus,
767 &chain->rgpElement[i]->TrustStatus);
768 }
769 CRYPT_CheckChainNameConstraints(chain);
770 if (CRYPT_IsCertificateSelfSigned(rootElement->pCertContext))
771 {
772 rootElement->TrustStatus.dwInfoStatus |=
773 CERT_TRUST_IS_SELF_SIGNED | CERT_TRUST_HAS_NAME_MATCH_ISSUER;
774 CRYPT_CheckRootCert(engine->hRoot, rootElement);
775 }
776 CRYPT_CombineTrustStatus(&chain->TrustStatus, &rootElement->TrustStatus);
777}
778
779static PCCERT_CONTEXT CRYPT_GetIssuer(HCERTSTORE store, PCCERT_CONTEXT subject,
780 PCCERT_CONTEXT prevIssuer, DWORD *infoStatus)
781{
782 PCCERT_CONTEXT issuer = NULL;
783 PCERT_EXTENSION ext;
784 DWORD size;
785
786 *infoStatus = 0;
787 if ((ext = CertFindExtension(szOID_AUTHORITY_KEY_IDENTIFIER,
788 subject->pCertInfo->cExtension, subject->pCertInfo->rgExtension)))
789 {
790 CERT_AUTHORITY_KEY_ID_INFO *info;
791 BOOL ret;
792
793 ret = CryptDecodeObjectEx(subject->dwCertEncodingType,
794 X509_AUTHORITY_KEY_ID, ext->Value.pbData, ext->Value.cbData,
795 CRYPT_DECODE_ALLOC_FLAG | CRYPT_DECODE_NOCOPY_FLAG, NULL,
796 &info, &size);
797 if (ret)
798 {
799 CERT_ID id;
800
801 if (info->CertIssuer.cbData && info->CertSerialNumber.cbData)
802 {
803 id.dwIdChoice = CERT_ID_ISSUER_SERIAL_NUMBER;
804 memcpy(&id.u.IssuerSerialNumber.Issuer, &info->CertIssuer,
805 sizeof(CERT_NAME_BLOB));
806 memcpy(&id.u.IssuerSerialNumber.SerialNumber,
807 &info->CertSerialNumber, sizeof(CRYPT_INTEGER_BLOB));
808 issuer = CertFindCertificateInStore(store,
809 subject->dwCertEncodingType, 0, CERT_FIND_CERT_ID, &id,
810 prevIssuer);
811 if (issuer)
812 *infoStatus = CERT_TRUST_HAS_EXACT_MATCH_ISSUER;
813 }
814 else if (info->KeyId.cbData)
815 {
816 id.dwIdChoice = CERT_ID_KEY_IDENTIFIER;
817 memcpy(&id.u.KeyId, &info->KeyId, sizeof(CRYPT_HASH_BLOB));
818 issuer = CertFindCertificateInStore(store,
819 subject->dwCertEncodingType, 0, CERT_FIND_CERT_ID, &id,
820 prevIssuer);
821 if (issuer)
822 *infoStatus = CERT_TRUST_HAS_KEY_MATCH_ISSUER;
823 }
824 LocalFree(info);
825 }
826 }
827 else if ((ext = CertFindExtension(szOID_AUTHORITY_KEY_IDENTIFIER2,
828 subject->pCertInfo->cExtension, subject->pCertInfo->rgExtension)))
829 {
830 CERT_AUTHORITY_KEY_ID2_INFO *info;
831 BOOL ret;
832
833 ret = CryptDecodeObjectEx(subject->dwCertEncodingType,
834 X509_AUTHORITY_KEY_ID2, ext->Value.pbData, ext->Value.cbData,
835 CRYPT_DECODE_ALLOC_FLAG | CRYPT_DECODE_NOCOPY_FLAG, NULL,
836 &info, &size);
837 if (ret)
838 {
839 CERT_ID id;
840
841 if (info->AuthorityCertIssuer.cAltEntry &&
842 info->AuthorityCertSerialNumber.cbData)
843 {
844 PCERT_ALT_NAME_ENTRY directoryName = NULL;
845 DWORD i;
846
847 for (i = 0; !directoryName &&
848 i < info->AuthorityCertIssuer.cAltEntry; i++)
849 if (info->AuthorityCertIssuer.rgAltEntry[i].dwAltNameChoice
850 == CERT_ALT_NAME_DIRECTORY_NAME)
851 directoryName =
852 &info->AuthorityCertIssuer.rgAltEntry[i];
853 if (directoryName)
854 {
855 id.dwIdChoice = CERT_ID_ISSUER_SERIAL_NUMBER;
856 memcpy(&id.u.IssuerSerialNumber.Issuer,
857 &directoryName->u.DirectoryName, sizeof(CERT_NAME_BLOB));
858 memcpy(&id.u.IssuerSerialNumber.SerialNumber,
859 &info->AuthorityCertSerialNumber,
860 sizeof(CRYPT_INTEGER_BLOB));
861 issuer = CertFindCertificateInStore(store,
862 subject->dwCertEncodingType, 0, CERT_FIND_CERT_ID, &id,
863 prevIssuer);
864 if (issuer)
865 *infoStatus = CERT_TRUST_HAS_EXACT_MATCH_ISSUER;
866 }
867 else
868 FIXME("no supported name type in authority key id2\n");
869 }
870 else if (info->KeyId.cbData)
871 {
872 id.dwIdChoice = CERT_ID_KEY_IDENTIFIER;
873 memcpy(&id.u.KeyId, &info->KeyId, sizeof(CRYPT_HASH_BLOB));
874 issuer = CertFindCertificateInStore(store,
875 subject->dwCertEncodingType, 0, CERT_FIND_CERT_ID, &id,
876 prevIssuer);
877 if (issuer)
878 *infoStatus = CERT_TRUST_HAS_KEY_MATCH_ISSUER;
879 }
880 LocalFree(info);
881 }
882 }
883 else
884 {
885 issuer = CertFindCertificateInStore(store,
886 subject->dwCertEncodingType, 0, CERT_FIND_SUBJECT_NAME,
887 &subject->pCertInfo->Issuer, prevIssuer);
888 if (issuer)
889 *infoStatus = CERT_TRUST_HAS_NAME_MATCH_ISSUER;
890 }
891 return issuer;
892}
893
894/* Builds a simple chain by finding an issuer for the last cert in the chain,
895 * until reaching a self-signed cert, or until no issuer can be found.
896 */
897static BOOL CRYPT_BuildSimpleChain(PCertificateChainEngine engine,
898 HCERTSTORE world, PCERT_SIMPLE_CHAIN chain)
899{
900 BOOL ret = TRUE;
901 PCCERT_CONTEXT cert = chain->rgpElement[chain->cElement - 1]->pCertContext;
902
903 while (ret && !CRYPT_IsSimpleChainCyclic(chain) &&
904 !CRYPT_IsCertificateSelfSigned(cert))
905 {
906 DWORD infoStatus;
907 PCCERT_CONTEXT issuer = CRYPT_GetIssuer(world, cert, NULL, &infoStatus);
908
909 if (issuer)
910 {
911 ret = CRYPT_AddCertToSimpleChain(engine, chain, issuer, infoStatus);
912 /* CRYPT_AddCertToSimpleChain add-ref's the issuer, so free it to
913 * close the enumeration that found it
914 */
915 CertFreeCertificateContext(issuer);
916 cert = issuer;
917 }
918 else
919 {
920 TRACE("Couldn't find issuer, halting chain creation\n");
921 chain->TrustStatus.dwErrorStatus |= CERT_TRUST_IS_PARTIAL_CHAIN;
922 break;
923 }
924 }
925 return ret;
926}
927
928static BOOL CRYPT_GetSimpleChainForCert(PCertificateChainEngine engine,
929 HCERTSTORE world, PCCERT_CONTEXT cert, LPFILETIME pTime,
930 PCERT_SIMPLE_CHAIN *ppChain)
931{
932 BOOL ret = FALSE;
933 PCERT_SIMPLE_CHAIN chain;
934
935 TRACE("(%p, %p, %p, %p)\n", engine, world, cert, pTime);
936
937 chain = CryptMemAlloc(sizeof(CERT_SIMPLE_CHAIN));
938 if (chain)
939 {
940 memset(chain, 0, sizeof(CERT_SIMPLE_CHAIN));
941 chain->cbSize = sizeof(CERT_SIMPLE_CHAIN);
942 ret = CRYPT_AddCertToSimpleChain(engine, chain, cert, 0);
943 if (ret)
944 {
945 ret = CRYPT_BuildSimpleChain(engine, world, chain);
946 if (ret)
947 CRYPT_CheckSimpleChain(engine, chain, pTime);
948 }
949 if (!ret)
950 {
951 CRYPT_FreeSimpleChain(chain);
952 chain = NULL;
953 }
954 *ppChain = chain;
955 }
956 return ret;
957}
958
959static BOOL CRYPT_BuildCandidateChainFromCert(HCERTCHAINENGINE hChainEngine,
960 PCCERT_CONTEXT cert, LPFILETIME pTime, HCERTSTORE hAdditionalStore,
961 PCertificateChain *ppChain)
962{
963 PCertificateChainEngine engine = (PCertificateChainEngine)hChainEngine;
964 PCERT_SIMPLE_CHAIN simpleChain = NULL;
965 HCERTSTORE world;
966 BOOL ret;
967
968 world = CertOpenStore(CERT_STORE_PROV_COLLECTION, 0, 0,
969 CERT_STORE_CREATE_NEW_FLAG, NULL);
970 CertAddStoreToCollection(world, engine->hWorld, 0, 0);
971 if (hAdditionalStore)
972 CertAddStoreToCollection(world, hAdditionalStore, 0, 0);
973 /* FIXME: only simple chains are supported for now, as CTLs aren't
974 * supported yet.
975 */
976 if ((ret = CRYPT_GetSimpleChainForCert(engine, world, cert, pTime,
977 &simpleChain)))
978 {
979 PCertificateChain chain = CryptMemAlloc(sizeof(CertificateChain));
980
981 if (chain)
982 {
983 chain->ref = 1;
984 chain->world = world;
985 chain->context.cbSize = sizeof(CERT_CHAIN_CONTEXT);
986 chain->context.TrustStatus = simpleChain->TrustStatus;
987 chain->context.cChain = 1;
988 chain->context.rgpChain = CryptMemAlloc(sizeof(PCERT_SIMPLE_CHAIN));
989 chain->context.rgpChain[0] = simpleChain;
990 chain->context.cLowerQualityChainContext = 0;
991 chain->context.rgpLowerQualityChainContext = NULL;
992 chain->context.fHasRevocationFreshnessTime = FALSE;
993 chain->context.dwRevocationFreshnessTime = 0;
994 }
995 else
996 ret = FALSE;
997 *ppChain = chain;
998 }
999 return ret;
1000}
1001
1002/* Makes and returns a copy of chain, up to and including element iElement. */
1003static PCERT_SIMPLE_CHAIN CRYPT_CopySimpleChainToElement(
1004 PCERT_SIMPLE_CHAIN chain, DWORD iElement)
1005{
1006 PCERT_SIMPLE_CHAIN copy = CryptMemAlloc(sizeof(CERT_SIMPLE_CHAIN));
1007
1008 if (copy)
1009 {
1010 memset(copy, 0, sizeof(CERT_SIMPLE_CHAIN));
1011 copy->cbSize = sizeof(CERT_SIMPLE_CHAIN);
1012 copy->rgpElement =
1013 CryptMemAlloc((iElement + 1) * sizeof(PCERT_CHAIN_ELEMENT));
1014 if (copy->rgpElement)
1015 {
1016 DWORD i;
1017 BOOL ret = TRUE;
1018
1019 memset(copy->rgpElement, 0,
1020 (iElement + 1) * sizeof(PCERT_CHAIN_ELEMENT));
1021 for (i = 0; ret && i <= iElement; i++)
1022 {
1023 PCERT_CHAIN_ELEMENT element =
1024 CryptMemAlloc(sizeof(CERT_CHAIN_ELEMENT));
1025
1026 if (element)
1027 {
1028 *element = *chain->rgpElement[i];
1029 element->pCertContext = CertDuplicateCertificateContext(
1030 chain->rgpElement[i]->pCertContext);
1031 /* Reset the trust status of the copied element, it'll get
1032 * rechecked after the new chain is done.
1033 */
1034 memset(&element->TrustStatus, 0, sizeof(CERT_TRUST_STATUS));
1035 copy->rgpElement[copy->cElement++] = element;
1036 }
1037 else
1038 ret = FALSE;
1039 }
1040 if (!ret)
1041 {
1042 for (i = 0; i <= iElement; i++)
1043 CryptMemFree(copy->rgpElement[i]);
1044 CryptMemFree(copy->rgpElement);
1045 CryptMemFree(copy);
1046 copy = NULL;
1047 }
1048 }
1049 else
1050 {
1051 CryptMemFree(copy);
1052 copy = NULL;
1053 }
1054 }
1055 return copy;
1056}
1057
1058static void CRYPT_FreeLowerQualityChains(PCertificateChain chain)
1059{
1060 DWORD i;
1061
1062 for (i = 0; i < chain->context.cLowerQualityChainContext; i++)
1063 CertFreeCertificateChain(chain->context.rgpLowerQualityChainContext[i]);
1064 CryptMemFree(chain->context.rgpLowerQualityChainContext);
1065 chain->context.cLowerQualityChainContext = 0;
1066 chain->context.rgpLowerQualityChainContext = NULL;
1067}
1068
1069static void CRYPT_FreeChainContext(PCertificateChain chain)
1070{
1071 DWORD i;
1072
1073 CRYPT_FreeLowerQualityChains(chain);
1074 for (i = 0; i < chain->context.cChain; i++)
1075 CRYPT_FreeSimpleChain(chain->context.rgpChain[i]);
1076 CryptMemFree(chain->context.rgpChain);
1077 CertCloseStore(chain->world, 0);
1078 CryptMemFree(chain);
1079}
1080
1081/* Makes and returns a copy of chain, up to and including element iElement of
1082 * simple chain iChain.
1083 */
1084static PCertificateChain CRYPT_CopyChainToElement(PCertificateChain chain,
1085 DWORD iChain, DWORD iElement)
1086{
1087 PCertificateChain copy = CryptMemAlloc(sizeof(CertificateChain));
1088
1089 if (copy)
1090 {
1091 copy->ref = 1;
1092 copy->world = CertDuplicateStore(chain->world);
1093 copy->context.cbSize = sizeof(CERT_CHAIN_CONTEXT);
1094 /* Leave the trust status of the copied chain unset, it'll get
1095 * rechecked after the new chain is done.
1096 */
1097 memset(&copy->context.TrustStatus, 0, sizeof(CERT_TRUST_STATUS));
1098 copy->context.cLowerQualityChainContext = 0;
1099 copy->context.rgpLowerQualityChainContext = NULL;
1100 copy->context.fHasRevocationFreshnessTime = FALSE;
1101 copy->context.dwRevocationFreshnessTime = 0;
1102 copy->context.rgpChain = CryptMemAlloc(
1103 (iChain + 1) * sizeof(PCERT_SIMPLE_CHAIN));
1104 if (copy->context.rgpChain)
1105 {
1106 BOOL ret = TRUE;
1107 DWORD i;
1108
1109 memset(copy->context.rgpChain, 0,
1110 (iChain + 1) * sizeof(PCERT_SIMPLE_CHAIN));
1111 if (iChain)
1112 {
1113 for (i = 0; ret && iChain && i < iChain - 1; i++)
1114 {
1115 copy->context.rgpChain[i] =
1116 CRYPT_CopySimpleChainToElement(chain->context.rgpChain[i],
1117 chain->context.rgpChain[i]->cElement - 1);
1118 if (!copy->context.rgpChain[i])
1119 ret = FALSE;
1120 }
1121 }
1122 else
1123 i = 0;
1124 if (ret)
1125 {
1126 copy->context.rgpChain[i] =
1127 CRYPT_CopySimpleChainToElement(chain->context.rgpChain[i],
1128 iElement);
1129 if (!copy->context.rgpChain[i])
1130 ret = FALSE;
1131 }
1132 if (!ret)
1133 {
1134 CRYPT_FreeChainContext(copy);
1135 copy = NULL;
1136 }
1137 else
1138 copy->context.cChain = iChain + 1;
1139 }
1140 else
1141 {
1142 CryptMemFree(copy);
1143 copy = NULL;
1144 }
1145 }
1146 return copy;
1147}
1148
1149static PCertificateChain CRYPT_BuildAlternateContextFromChain(
1150 HCERTCHAINENGINE hChainEngine, LPFILETIME pTime, HCERTSTORE hAdditionalStore,
1151 PCertificateChain chain)
1152{
1153 PCertificateChainEngine engine = (PCertificateChainEngine)hChainEngine;
1154 PCertificateChain alternate;
1155
1156 TRACE("(%p, %p, %p, %p)\n", hChainEngine, pTime, hAdditionalStore, chain);
1157
1158 /* Always start with the last "lower quality" chain to ensure a consistent
1159 * order of alternate creation:
1160 */
1161 if (chain->context.cLowerQualityChainContext)
1162 chain = (PCertificateChain)chain->context.rgpLowerQualityChainContext[
1163 chain->context.cLowerQualityChainContext - 1];
1164 /* A chain with only one element can't have any alternates */
1165 if (chain->context.cChain <= 1 && chain->context.rgpChain[0]->cElement <= 1)
1166 alternate = NULL;
1167 else
1168 {
1169 DWORD i, j, infoStatus;
1170 PCCERT_CONTEXT alternateIssuer = NULL;
1171
1172 alternate = NULL;
1173 for (i = 0; !alternateIssuer && i < chain->context.cChain; i++)
1174 for (j = 0; !alternateIssuer &&
1175 j < chain->context.rgpChain[i]->cElement - 1; j++)
1176 {
1177 PCCERT_CONTEXT subject =
1178 chain->context.rgpChain[i]->rgpElement[j]->pCertContext;
1179 PCCERT_CONTEXT prevIssuer = CertDuplicateCertificateContext(
1180 chain->context.rgpChain[i]->rgpElement[j + 1]->pCertContext);
1181
1182 alternateIssuer = CRYPT_GetIssuer(prevIssuer->hCertStore,
1183 subject, prevIssuer, &infoStatus);
1184 }
1185 if (alternateIssuer)
1186 {
1187 i--;
1188 j--;
1189 alternate = CRYPT_CopyChainToElement(chain, i, j);
1190 if (alternate)
1191 {
1192 BOOL ret = CRYPT_AddCertToSimpleChain(engine,
1193 alternate->context.rgpChain[i], alternateIssuer, infoStatus);
1194
1195 /* CRYPT_AddCertToSimpleChain add-ref's the issuer, so free it
1196 * to close the enumeration that found it
1197 */
1198 CertFreeCertificateContext(alternateIssuer);
1199 if (ret)
1200 {
1201 ret = CRYPT_BuildSimpleChain(engine, alternate->world,
1202 alternate->context.rgpChain[i]);
1203 if (ret)
1204 CRYPT_CheckSimpleChain(engine,
1205 alternate->context.rgpChain[i], pTime);
1206 CRYPT_CombineTrustStatus(&alternate->context.TrustStatus,
1207 &alternate->context.rgpChain[i]->TrustStatus);
1208 }
1209 if (!ret)
1210 {
1211 CRYPT_FreeChainContext(alternate);
1212 alternate = NULL;
1213 }
1214 }
1215 }
1216 }
1217 TRACE("%p\n", alternate);
1218 return alternate;
1219}
1220
1221#define CHAIN_QUALITY_SIGNATURE_VALID 8
1222#define CHAIN_QUALITY_TIME_VALID 4
1223#define CHAIN_QUALITY_COMPLETE_CHAIN 2
1224#define CHAIN_QUALITY_TRUSTED_ROOT 1
1225
1226#define CHAIN_QUALITY_HIGHEST \
1227 CHAIN_QUALITY_SIGNATURE_VALID | CHAIN_QUALITY_TIME_VALID | \
1228 CHAIN_QUALITY_COMPLETE_CHAIN | CHAIN_QUALITY_TRUSTED_ROOT
1229
1230#define IS_TRUST_ERROR_SET(TrustStatus, bits) \
1231 (TrustStatus)->dwErrorStatus & (bits)
1232
1233static DWORD CRYPT_ChainQuality(PCertificateChain chain)
1234{
1235 DWORD quality = CHAIN_QUALITY_HIGHEST;
1236
1237 if (IS_TRUST_ERROR_SET(&chain->context.TrustStatus,
1238 CERT_TRUST_IS_UNTRUSTED_ROOT))
1239 quality &= ~CHAIN_QUALITY_TRUSTED_ROOT;
1240 if (IS_TRUST_ERROR_SET(&chain->context.TrustStatus,
1241 CERT_TRUST_IS_PARTIAL_CHAIN))
1242 if (chain->context.TrustStatus.dwErrorStatus & CERT_TRUST_IS_PARTIAL_CHAIN)
1243 quality &= ~CHAIN_QUALITY_COMPLETE_CHAIN;
1244 if (IS_TRUST_ERROR_SET(&chain->context.TrustStatus,
1245 CERT_TRUST_IS_NOT_TIME_VALID | CERT_TRUST_IS_NOT_TIME_NESTED))
1246 quality &= ~CHAIN_QUALITY_TIME_VALID;
1247 if (IS_TRUST_ERROR_SET(&chain->context.TrustStatus,
1248 CERT_TRUST_IS_NOT_SIGNATURE_VALID))
1249 quality &= ~CHAIN_QUALITY_SIGNATURE_VALID;
1250 return quality;
1251}
1252
1253/* Chooses the highest quality chain among chain and its "lower quality"
1254 * alternate chains. Returns the highest quality chain, with all other
1255 * chains as lower quality chains of it.
1256 */
1257static PCertificateChain CRYPT_ChooseHighestQualityChain(
1258 PCertificateChain chain)
1259{
1260 DWORD i;
1261
1262 /* There are always only two chains being considered: chain, and an
1263 * alternate at chain->rgpLowerQualityChainContext[i]. If the alternate
1264 * has a higher quality than chain, the alternate gets assigned the lower
1265 * quality contexts, with chain taking the alternate's place among the
1266 * lower quality contexts.
1267 */
1268 for (i = 0; i < chain->context.cLowerQualityChainContext; i++)
1269 {
1270 PCertificateChain alternate =
1271 (PCertificateChain)chain->context.rgpLowerQualityChainContext[i];
1272
1273 if (CRYPT_ChainQuality(alternate) > CRYPT_ChainQuality(chain))
1274 {
1275 alternate->context.cLowerQualityChainContext =
1276 chain->context.cLowerQualityChainContext;
1277 alternate->context.rgpLowerQualityChainContext =
1278 chain->context.rgpLowerQualityChainContext;
1279 alternate->context.rgpLowerQualityChainContext[i] =
1280 (PCCERT_CHAIN_CONTEXT)chain;
1281 chain->context.cLowerQualityChainContext = 0;
1282 chain->context.rgpLowerQualityChainContext = NULL;
1283 chain = alternate;
1284 }
1285 }
1286 return chain;
1287}
1288
1289static BOOL CRYPT_AddAlternateChainToChain(PCertificateChain chain,
1290 PCertificateChain alternate)
1291{
1292 BOOL ret;
1293
1294 if (chain->context.cLowerQualityChainContext)
1295 chain->context.rgpLowerQualityChainContext =
1296 CryptMemRealloc(chain->context.rgpLowerQualityChainContext,
1297 (chain->context.cLowerQualityChainContext + 1) *
1298 sizeof(PCCERT_CHAIN_CONTEXT));
1299 else
1300 chain->context.rgpLowerQualityChainContext =
1301 CryptMemAlloc(sizeof(PCCERT_CHAIN_CONTEXT));
1302 if (chain->context.rgpLowerQualityChainContext)
1303 {
1304 chain->context.rgpLowerQualityChainContext[
1305 chain->context.cLowerQualityChainContext++] =
1306 (PCCERT_CHAIN_CONTEXT)alternate;
1307 ret = TRUE;
1308 }
1309 else
1310 ret = FALSE;
1311 return ret;
1312}
1313
1314static PCERT_CHAIN_ELEMENT CRYPT_FindIthElementInChain(
1315 PCERT_CHAIN_CONTEXT chain, DWORD i)
1316{
1317 DWORD j, iElement;
1318 PCERT_CHAIN_ELEMENT element = NULL;
1319
1320 for (j = 0, iElement = 0; !element && j < chain->cChain; j++)
1321 {
1322 if (iElement + chain->rgpChain[j]->cElement < i)
1323 iElement += chain->rgpChain[j]->cElement;
1324 else
1325 element = chain->rgpChain[j]->rgpElement[i - iElement];
1326 }
1327 return element;
1328}
1329
1330typedef struct _CERT_CHAIN_PARA_NO_EXTRA_FIELDS {
1331 DWORD cbSize;
1332 CERT_USAGE_MATCH RequestedUsage;
1333} CERT_CHAIN_PARA_NO_EXTRA_FIELDS, *PCERT_CHAIN_PARA_NO_EXTRA_FIELDS;
1334
1335static void CRYPT_VerifyChainRevocation(PCERT_CHAIN_CONTEXT chain,
1336 LPFILETIME pTime, PCERT_CHAIN_PARA pChainPara, DWORD chainFlags)
1337{
1338 DWORD cContext;
1339
1340 if (chainFlags & CERT_CHAIN_REVOCATION_CHECK_END_CERT)
1341 cContext = 1;
1342 else if ((chainFlags & CERT_CHAIN_REVOCATION_CHECK_CHAIN) ||
1343 (chainFlags & CERT_CHAIN_REVOCATION_CHECK_CHAIN_EXCLUDE_ROOT))
1344 {
1345 DWORD i;
1346
1347 for (i = 0, cContext = 0; i < chain->cChain; i++)
1348 {
1349 if (i < chain->cChain - 1 ||
1350 chainFlags & CERT_CHAIN_REVOCATION_CHECK_CHAIN)
1351 cContext += chain->rgpChain[i]->cElement;
1352 else
1353 cContext += chain->rgpChain[i]->cElement - 1;
1354 }
1355 }
1356 else
1357 cContext = 0;
1358 if (cContext)
1359 {
1360 PCCERT_CONTEXT *contexts =
1361 CryptMemAlloc(cContext * sizeof(PCCERT_CONTEXT *));
1362
1363 if (contexts)
1364 {
1365 DWORD i, j, iContext, revocationFlags;
1366 CERT_REVOCATION_PARA revocationPara = { sizeof(revocationPara), 0 };
1367 CERT_REVOCATION_STATUS revocationStatus =
1368 { sizeof(revocationStatus), 0 };
1369 BOOL ret;
1370
1371 for (i = 0, iContext = 0; iContext < cContext && i < chain->cChain;
1372 i++)
1373 {
1374 for (j = 0; iContext < cContext &&
1375 j < chain->rgpChain[i]->cElement; j++)
1376 contexts[iContext++] =
1377 chain->rgpChain[i]->rgpElement[j]->pCertContext;
1378 }
1379 revocationFlags = CERT_VERIFY_REV_CHAIN_FLAG;
1380 if (chainFlags & CERT_CHAIN_REVOCATION_CHECK_CACHE_ONLY)
1381 revocationFlags |= CERT_VERIFY_CACHE_ONLY_BASED_REVOCATION;
1382 if (chainFlags & CERT_CHAIN_REVOCATION_ACCUMULATIVE_TIMEOUT)
1383 revocationFlags |= CERT_VERIFY_REV_ACCUMULATIVE_TIMEOUT_FLAG;
1384 revocationPara.pftTimeToUse = pTime;
1385 if (pChainPara->cbSize == sizeof(CERT_CHAIN_PARA))
1386 {
1387 revocationPara.dwUrlRetrievalTimeout =
1388 pChainPara->dwUrlRetrievalTimeout;
1389 revocationPara.fCheckFreshnessTime =
1390 pChainPara->fCheckRevocationFreshnessTime;
1391 revocationPara.dwFreshnessTime =
1392 pChainPara->dwRevocationFreshnessTime;
1393 }
1394 ret = CertVerifyRevocation(X509_ASN_ENCODING,
1395 CERT_CONTEXT_REVOCATION_TYPE, cContext, (void **)contexts,
1396 revocationFlags, &revocationPara, &revocationStatus);
1397 if (!ret)
1398 {
1399 PCERT_CHAIN_ELEMENT element =
1400 CRYPT_FindIthElementInChain(chain, revocationStatus.dwIndex);
1401 DWORD error;
1402
1403 switch (revocationStatus.dwError)
1404 {
1405 case CRYPT_E_NO_REVOCATION_CHECK:
1406 case CRYPT_E_NO_REVOCATION_DLL:
1407 case CRYPT_E_NOT_IN_REVOCATION_DATABASE:
1408 error = CERT_TRUST_REVOCATION_STATUS_UNKNOWN;
1409 break;
1410 case CRYPT_E_REVOCATION_OFFLINE:
1411 error = CERT_TRUST_IS_OFFLINE_REVOCATION;
1412 break;
1413 case CRYPT_E_REVOKED:
1414 error = CERT_TRUST_IS_REVOKED;
1415 break;
1416 default:
1417 WARN("unmapped error %08x\n", revocationStatus.dwError);
1418 error = 0;
1419 }
1420 if (element)
1421 {
1422 /* FIXME: set element's pRevocationInfo member */
1423 element->TrustStatus.dwErrorStatus |= error;
1424 }
1425 chain->TrustStatus.dwErrorStatus |= error;
1426 }
1427 CryptMemFree(contexts);
1428 }
1429 }
1430}
1431
1432BOOL WINAPI CertGetCertificateChain(HCERTCHAINENGINE hChainEngine,
1433 PCCERT_CONTEXT pCertContext, LPFILETIME pTime, HCERTSTORE hAdditionalStore,
1434 PCERT_CHAIN_PARA pChainPara, DWORD dwFlags, LPVOID pvReserved,
1435 PCCERT_CHAIN_CONTEXT* ppChainContext)
1436{
1437 BOOL ret;
1438 PCertificateChain chain = NULL;
1439
1440 TRACE("(%p, %p, %p, %p, %p, %08x, %p, %p)\n", hChainEngine, pCertContext,
1441 pTime, hAdditionalStore, pChainPara, dwFlags, pvReserved, ppChainContext);
1442
1443 if (ppChainContext)
1444 *ppChainContext = NULL;
1445 if (!pChainPara)
1446 {
1447 SetLastError(E_INVALIDARG);
1448 return FALSE;
1449 }
1450 if (!pCertContext->pCertInfo->SignatureAlgorithm.pszObjId)
1451 {
1452 SetLastError(ERROR_INVALID_DATA);
1453 return FALSE;
1454 }
1455 if (pChainPara->cbSize != sizeof(CERT_CHAIN_PARA_NO_EXTRA_FIELDS) &&
1456 pChainPara->cbSize != sizeof(CERT_CHAIN_PARA))
1457 {
1458 SetLastError(E_INVALIDARG);
1459 return FALSE;
1460 }
1461 if (!hChainEngine)
1462 hChainEngine = CRYPT_GetDefaultChainEngine();
1463 /* FIXME: what about HCCE_LOCAL_MACHINE? */
1464 ret = CRYPT_BuildCandidateChainFromCert(hChainEngine, pCertContext, pTime,
1465 hAdditionalStore, &chain);
1466 if (ret)
1467 {
1468 PCertificateChain alternate = NULL;
1469 PCERT_CHAIN_CONTEXT pChain;
1470
1471 do {
1472 alternate = CRYPT_BuildAlternateContextFromChain(hChainEngine,
1473 pTime, hAdditionalStore, chain);
1474
1475 /* Alternate contexts are added as "lower quality" contexts of
1476 * chain, to avoid loops in alternate chain creation.
1477 * The highest-quality chain is chosen at the end.
1478 */
1479 if (alternate)
1480 ret = CRYPT_AddAlternateChainToChain(chain, alternate);
1481 } while (ret && alternate);
1482 chain = CRYPT_ChooseHighestQualityChain(chain);
1483 if (!(dwFlags & CERT_CHAIN_RETURN_LOWER_QUALITY_CONTEXTS))
1484 CRYPT_FreeLowerQualityChains(chain);
1485 pChain = (PCERT_CHAIN_CONTEXT)chain;
1486 CRYPT_VerifyChainRevocation(pChain, pTime, pChainPara, dwFlags);
1487 if (ppChainContext)
1488 *ppChainContext = pChain;
1489 else
1490 CertFreeCertificateChain(pChain);
1491 }
1492 TRACE("returning %d\n", ret);
1493 return ret;
1494}
1495
1496PCCERT_CHAIN_CONTEXT WINAPI CertDuplicateCertificateChain(
1497 PCCERT_CHAIN_CONTEXT pChainContext)
1498{
1499 PCertificateChain chain = (PCertificateChain)pChainContext;
1500
1501 TRACE("(%p)\n", pChainContext);
1502
1503 if (chain)
1504 InterlockedIncrement(&chain->ref);
1505 return pChainContext;
1506}
1507
1508VOID WINAPI CertFreeCertificateChain(PCCERT_CHAIN_CONTEXT pChainContext)
1509{
1510 PCertificateChain chain = (PCertificateChain)pChainContext;
1511
1512 TRACE("(%p)\n", pChainContext);
1513
1514 if (chain)
1515 {
1516 if (InterlockedDecrement(&chain->ref) == 0)
1517 CRYPT_FreeChainContext(chain);
1518 }
1519}
1520
1521static void find_element_with_error(PCCERT_CHAIN_CONTEXT chain, DWORD error,
1522 LONG *iChain, LONG *iElement)
1523{
1524 DWORD i, j;
1525
1526 for (i = 0; i < chain->cChain; i++)
1527 for (j = 0; j < chain->rgpChain[i]->cElement; j++)
1528 if (chain->rgpChain[i]->rgpElement[j]->TrustStatus.dwErrorStatus &
1529 error)
1530 {
1531 *iChain = i;
1532 *iElement = j;
1533 return;
1534 }
1535}
1536
1537static BOOL WINAPI verify_base_policy(LPCSTR szPolicyOID,
1538 PCCERT_CHAIN_CONTEXT pChainContext, PCERT_CHAIN_POLICY_PARA pPolicyPara,
1539 PCERT_CHAIN_POLICY_STATUS pPolicyStatus)
1540{
1541 pPolicyStatus->lChainIndex = pPolicyStatus->lElementIndex = -1;
1542 if (pChainContext->TrustStatus.dwErrorStatus &
1543 CERT_TRUST_IS_NOT_SIGNATURE_VALID)
1544 {
1545 pPolicyStatus->dwError = TRUST_E_CERT_SIGNATURE;
1546 find_element_with_error(pChainContext,
1547 CERT_TRUST_IS_NOT_SIGNATURE_VALID, &pPolicyStatus->lChainIndex,
1548 &pPolicyStatus->lElementIndex);
1549 }
1550 else if (pChainContext->TrustStatus.dwErrorStatus &
1551 CERT_TRUST_IS_UNTRUSTED_ROOT)
1552 {
1553 pPolicyStatus->dwError = CERT_E_UNTRUSTEDROOT;
1554 find_element_with_error(pChainContext,
1555 CERT_TRUST_IS_UNTRUSTED_ROOT, &pPolicyStatus->lChainIndex,
1556 &pPolicyStatus->lElementIndex);
1557 }
1558 else if (pChainContext->TrustStatus.dwErrorStatus & CERT_TRUST_IS_CYCLIC)
1559 {
1560 pPolicyStatus->dwError = CERT_E_CHAINING;
1561 find_element_with_error(pChainContext, CERT_TRUST_IS_CYCLIC,
1562 &pPolicyStatus->lChainIndex, &pPolicyStatus->lElementIndex);
1563 /* For a cyclic chain, which element is a cycle isn't meaningful */
1564 pPolicyStatus->lElementIndex = -1;
1565 }
1566 else
1567 pPolicyStatus->dwError = NO_ERROR;
1568 return TRUE;
1569}
1570
1571static BYTE msTestPubKey1[] = {
15720x30,0x47,0x02,0x40,0x81,0x55,0x22,0xb9,0x8a,0xa4,0x6f,0xed,0xd6,0xe7,0xd9,
15730x66,0x0f,0x55,0xbc,0xd7,0xcd,0xd5,0xbc,0x4e,0x40,0x02,0x21,0xa2,0xb1,0xf7,
15740x87,0x30,0x85,0x5e,0xd2,0xf2,0x44,0xb9,0xdc,0x9b,0x75,0xb6,0xfb,0x46,0x5f,
15750x42,0xb6,0x9d,0x23,0x36,0x0b,0xde,0x54,0x0f,0xcd,0xbd,0x1f,0x99,0x2a,0x10,
15760x58,0x11,0xcb,0x40,0xcb,0xb5,0xa7,0x41,0x02,0x03,0x01,0x00,0x01 };
1577static BYTE msTestPubKey2[] = {
15780x30,0x47,0x02,0x40,0x9c,0x50,0x05,0x1d,0xe2,0x0e,0x4c,0x53,0xd8,0xd9,0xb5,
15790xe5,0xfd,0xe9,0xe3,0xad,0x83,0x4b,0x80,0x08,0xd9,0xdc,0xe8,0xe8,0x35,0xf8,
15800x11,0xf1,0xe9,0x9b,0x03,0x7a,0x65,0x64,0x76,0x35,0xce,0x38,0x2c,0xf2,0xb6,
15810x71,0x9e,0x06,0xd9,0xbf,0xbb,0x31,0x69,0xa3,0xf6,0x30,0xa0,0x78,0x7b,0x18,
15820xdd,0x50,0x4d,0x79,0x1e,0xeb,0x61,0xc1,0x02,0x03,0x01,0x00,0x01 };
1583
1584static BOOL WINAPI verify_authenticode_policy(LPCSTR szPolicyOID,
1585 PCCERT_CHAIN_CONTEXT pChainContext, PCERT_CHAIN_POLICY_PARA pPolicyPara,
1586 PCERT_CHAIN_POLICY_STATUS pPolicyStatus)
1587{
1588 BOOL ret = verify_base_policy(szPolicyOID, pChainContext, pPolicyPara,
1589 pPolicyStatus);
1590
1591 if (ret && pPolicyStatus->dwError == CERT_E_UNTRUSTEDROOT)
1592 {
1593 CERT_PUBLIC_KEY_INFO msPubKey = { { 0 } };
1594 BOOL isMSTestRoot = FALSE;
1595 PCCERT_CONTEXT failingCert =
1596 pChainContext->rgpChain[pPolicyStatus->lChainIndex]->
1597 rgpElement[pPolicyStatus->lElementIndex]->pCertContext;
1598 DWORD i;
1599 CRYPT_DATA_BLOB keyBlobs[] = {
1600 { sizeof(msTestPubKey1), msTestPubKey1 },
1601 { sizeof(msTestPubKey2), msTestPubKey2 },
1602 };
1603
1604 /* Check whether the root is an MS test root */
1605 for (i = 0; !isMSTestRoot && i < sizeof(keyBlobs) / sizeof(keyBlobs[0]);
1606 i++)
1607 {
1608 msPubKey.PublicKey.cbData = keyBlobs[i].cbData;
1609 msPubKey.PublicKey.pbData = keyBlobs[i].pbData;
1610 if (CertComparePublicKeyInfo(
1611 X509_ASN_ENCODING | PKCS_7_ASN_ENCODING,
1612 &failingCert->pCertInfo->SubjectPublicKeyInfo, &msPubKey))
1613 isMSTestRoot = TRUE;
1614 }
1615 if (isMSTestRoot)
1616 pPolicyStatus->dwError = CERT_E_UNTRUSTEDTESTROOT;
1617 }
1618 return ret;
1619}
1620
1621static BOOL WINAPI verify_basic_constraints_policy(LPCSTR szPolicyOID,
1622 PCCERT_CHAIN_CONTEXT pChainContext, PCERT_CHAIN_POLICY_PARA pPolicyPara,
1623 PCERT_CHAIN_POLICY_STATUS pPolicyStatus)
1624{
1625 pPolicyStatus->lChainIndex = pPolicyStatus->lElementIndex = -1;
1626 if (pChainContext->TrustStatus.dwErrorStatus &
1627 CERT_TRUST_INVALID_BASIC_CONSTRAINTS)
1628 {
1629 pPolicyStatus->dwError = TRUST_E_BASIC_CONSTRAINTS;
1630 find_element_with_error(pChainContext,
1631 CERT_TRUST_INVALID_BASIC_CONSTRAINTS, &pPolicyStatus->lChainIndex,
1632 &pPolicyStatus->lElementIndex);
1633 }
1634 else
1635 pPolicyStatus->dwError = NO_ERROR;
1636 return TRUE;
1637}
1638
1639static BYTE msPubKey1[] = {
16400x30,0x82,0x01,0x0a,0x02,0x82,0x01,0x01,0x00,0xdf,0x08,0xba,0xe3,0x3f,0x6e,
16410x64,0x9b,0xf5,0x89,0xaf,0x28,0x96,0x4a,0x07,0x8f,0x1b,0x2e,0x8b,0x3e,0x1d,
16420xfc,0xb8,0x80,0x69,0xa3,0xa1,0xce,0xdb,0xdf,0xb0,0x8e,0x6c,0x89,0x76,0x29,
16430x4f,0xca,0x60,0x35,0x39,0xad,0x72,0x32,0xe0,0x0b,0xae,0x29,0x3d,0x4c,0x16,
16440xd9,0x4b,0x3c,0x9d,0xda,0xc5,0xd3,0xd1,0x09,0xc9,0x2c,0x6f,0xa6,0xc2,0x60,
16450x53,0x45,0xdd,0x4b,0xd1,0x55,0xcd,0x03,0x1c,0xd2,0x59,0x56,0x24,0xf3,0xe5,
16460x78,0xd8,0x07,0xcc,0xd8,0xb3,0x1f,0x90,0x3f,0xc0,0x1a,0x71,0x50,0x1d,0x2d,
16470xa7,0x12,0x08,0x6d,0x7c,0xb0,0x86,0x6c,0xc7,0xba,0x85,0x32,0x07,0xe1,0x61,
16480x6f,0xaf,0x03,0xc5,0x6d,0xe5,0xd6,0xa1,0x8f,0x36,0xf6,0xc1,0x0b,0xd1,0x3e,
16490x69,0x97,0x48,0x72,0xc9,0x7f,0xa4,0xc8,0xc2,0x4a,0x4c,0x7e,0xa1,0xd1,0x94,
16500xa6,0xd7,0xdc,0xeb,0x05,0x46,0x2e,0xb8,0x18,0xb4,0x57,0x1d,0x86,0x49,0xdb,
16510x69,0x4a,0x2c,0x21,0xf5,0x5e,0x0f,0x54,0x2d,0x5a,0x43,0xa9,0x7a,0x7e,0x6a,
16520x8e,0x50,0x4d,0x25,0x57,0xa1,0xbf,0x1b,0x15,0x05,0x43,0x7b,0x2c,0x05,0x8d,
16530xbd,0x3d,0x03,0x8c,0x93,0x22,0x7d,0x63,0xea,0x0a,0x57,0x05,0x06,0x0a,0xdb,
16540x61,0x98,0x65,0x2d,0x47,0x49,0xa8,0xe7,0xe6,0x56,0x75,0x5c,0xb8,0x64,0x08,
16550x63,0xa9,0x30,0x40,0x66,0xb2,0xf9,0xb6,0xe3,0x34,0xe8,0x67,0x30,0xe1,0x43,
16560x0b,0x87,0xff,0xc9,0xbe,0x72,0x10,0x5e,0x23,0xf0,0x9b,0xa7,0x48,0x65,0xbf,
16570x09,0x88,0x7b,0xcd,0x72,0xbc,0x2e,0x79,0x9b,0x7b,0x02,0x03,0x01,0x00,0x01 };
1658static BYTE msPubKey2[] = {
16590x30,0x82,0x01,0x0a,0x02,0x82,0x01,0x01,0x00,0xa9,0x02,0xbd,0xc1,0x70,0xe6,
16600x3b,0xf2,0x4e,0x1b,0x28,0x9f,0x97,0x78,0x5e,0x30,0xea,0xa2,0xa9,0x8d,0x25,
16610x5f,0xf8,0xfe,0x95,0x4c,0xa3,0xb7,0xfe,0x9d,0xa2,0x20,0x3e,0x7c,0x51,0xa2,
16620x9b,0xa2,0x8f,0x60,0x32,0x6b,0xd1,0x42,0x64,0x79,0xee,0xac,0x76,0xc9,0x54,
16630xda,0xf2,0xeb,0x9c,0x86,0x1c,0x8f,0x9f,0x84,0x66,0xb3,0xc5,0x6b,0x7a,0x62,
16640x23,0xd6,0x1d,0x3c,0xde,0x0f,0x01,0x92,0xe8,0x96,0xc4,0xbf,0x2d,0x66,0x9a,
16650x9a,0x68,0x26,0x99,0xd0,0x3a,0x2c,0xbf,0x0c,0xb5,0x58,0x26,0xc1,0x46,0xe7,
16660x0a,0x3e,0x38,0x96,0x2c,0xa9,0x28,0x39,0xa8,0xec,0x49,0x83,0x42,0xe3,0x84,
16670x0f,0xbb,0x9a,0x6c,0x55,0x61,0xac,0x82,0x7c,0xa1,0x60,0x2d,0x77,0x4c,0xe9,
16680x99,0xb4,0x64,0x3b,0x9a,0x50,0x1c,0x31,0x08,0x24,0x14,0x9f,0xa9,0xe7,0x91,
16690x2b,0x18,0xe6,0x3d,0x98,0x63,0x14,0x60,0x58,0x05,0x65,0x9f,0x1d,0x37,0x52,
16700x87,0xf7,0xa7,0xef,0x94,0x02,0xc6,0x1b,0xd3,0xbf,0x55,0x45,0xb3,0x89,0x80,
16710xbf,0x3a,0xec,0x54,0x94,0x4e,0xae,0xfd,0xa7,0x7a,0x6d,0x74,0x4e,0xaf,0x18,
16720xcc,0x96,0x09,0x28,0x21,0x00,0x57,0x90,0x60,0x69,0x37,0xbb,0x4b,0x12,0x07,
16730x3c,0x56,0xff,0x5b,0xfb,0xa4,0x66,0x0a,0x08,0xa6,0xd2,0x81,0x56,0x57,0xef,
16740xb6,0x3b,0x5e,0x16,0x81,0x77,0x04,0xda,0xf6,0xbe,0xae,0x80,0x95,0xfe,0xb0,
16750xcd,0x7f,0xd6,0xa7,0x1a,0x72,0x5c,0x3c,0xca,0xbc,0xf0,0x08,0xa3,0x22,0x30,
16760xb3,0x06,0x85,0xc9,0xb3,0x20,0x77,0x13,0x85,0xdf,0x02,0x03,0x01,0x00,0x01 };
1677static BYTE msPubKey3[] = {
16780x30,0x82,0x02,0x0a,0x02,0x82,0x02,0x01,0x00,0xf3,0x5d,0xfa,0x80,0x67,0xd4,
16790x5a,0xa7,0xa9,0x0c,0x2c,0x90,0x20,0xd0,0x35,0x08,0x3c,0x75,0x84,0xcd,0xb7,
16800x07,0x89,0x9c,0x89,0xda,0xde,0xce,0xc3,0x60,0xfa,0x91,0x68,0x5a,0x9e,0x94,
16810x71,0x29,0x18,0x76,0x7c,0xc2,0xe0,0xc8,0x25,0x76,0x94,0x0e,0x58,0xfa,0x04,
16820x34,0x36,0xe6,0xdf,0xaf,0xf7,0x80,0xba,0xe9,0x58,0x0b,0x2b,0x93,0xe5,0x9d,
16830x05,0xe3,0x77,0x22,0x91,0xf7,0x34,0x64,0x3c,0x22,0x91,0x1d,0x5e,0xe1,0x09,
16840x90,0xbc,0x14,0xfe,0xfc,0x75,0x58,0x19,0xe1,0x79,0xb7,0x07,0x92,0xa3,0xae,
16850x88,0x59,0x08,0xd8,0x9f,0x07,0xca,0x03,0x58,0xfc,0x68,0x29,0x6d,0x32,0xd7,
16860xd2,0xa8,0xcb,0x4b,0xfc,0xe1,0x0b,0x48,0x32,0x4f,0xe6,0xeb,0xb8,0xad,0x4f,
16870xe4,0x5c,0x6f,0x13,0x94,0x99,0xdb,0x95,0xd5,0x75,0xdb,0xa8,0x1a,0xb7,0x94,
16880x91,0xb4,0x77,0x5b,0xf5,0x48,0x0c,0x8f,0x6a,0x79,0x7d,0x14,0x70,0x04,0x7d,
16890x6d,0xaf,0x90,0xf5,0xda,0x70,0xd8,0x47,0xb7,0xbf,0x9b,0x2f,0x6c,0xe7,0x05,
16900xb7,0xe1,0x11,0x60,0xac,0x79,0x91,0x14,0x7c,0xc5,0xd6,0xa6,0xe4,0xe1,0x7e,
16910xd5,0xc3,0x7e,0xe5,0x92,0xd2,0x3c,0x00,0xb5,0x36,0x82,0xde,0x79,0xe1,0x6d,
16920xf3,0xb5,0x6e,0xf8,0x9f,0x33,0xc9,0xcb,0x52,0x7d,0x73,0x98,0x36,0xdb,0x8b,
16930xa1,0x6b,0xa2,0x95,0x97,0x9b,0xa3,0xde,0xc2,0x4d,0x26,0xff,0x06,0x96,0x67,
16940x25,0x06,0xc8,0xe7,0xac,0xe4,0xee,0x12,0x33,0x95,0x31,0x99,0xc8,0x35,0x08,
16950x4e,0x34,0xca,0x79,0x53,0xd5,0xb5,0xbe,0x63,0x32,0x59,0x40,0x36,0xc0,0xa5,
16960x4e,0x04,0x4d,0x3d,0xdb,0x5b,0x07,0x33,0xe4,0x58,0xbf,0xef,0x3f,0x53,0x64,
16970xd8,0x42,0x59,0x35,0x57,0xfd,0x0f,0x45,0x7c,0x24,0x04,0x4d,0x9e,0xd6,0x38,
16980x74,0x11,0x97,0x22,0x90,0xce,0x68,0x44,0x74,0x92,0x6f,0xd5,0x4b,0x6f,0xb0,
16990x86,0xe3,0xc7,0x36,0x42,0xa0,0xd0,0xfc,0xc1,0xc0,0x5a,0xf9,0xa3,0x61,0xb9,
17000x30,0x47,0x71,0x96,0x0a,0x16,0xb0,0x91,0xc0,0x42,0x95,0xef,0x10,0x7f,0x28,
17010x6a,0xe3,0x2a,0x1f,0xb1,0xe4,0xcd,0x03,0x3f,0x77,0x71,0x04,0xc7,0x20,0xfc,
17020x49,0x0f,0x1d,0x45,0x88,0xa4,0xd7,0xcb,0x7e,0x88,0xad,0x8e,0x2d,0xec,0x45,
17030xdb,0xc4,0x51,0x04,0xc9,0x2a,0xfc,0xec,0x86,0x9e,0x9a,0x11,0x97,0x5b,0xde,
17040xce,0x53,0x88,0xe6,0xe2,0xb7,0xfd,0xac,0x95,0xc2,0x28,0x40,0xdb,0xef,0x04,
17050x90,0xdf,0x81,0x33,0x39,0xd9,0xb2,0x45,0xa5,0x23,0x87,0x06,0xa5,0x55,0x89,
17060x31,0xbb,0x06,0x2d,0x60,0x0e,0x41,0x18,0x7d,0x1f,0x2e,0xb5,0x97,0xcb,0x11,
17070xeb,0x15,0xd5,0x24,0xa5,0x94,0xef,0x15,0x14,0x89,0xfd,0x4b,0x73,0xfa,0x32,
17080x5b,0xfc,0xd1,0x33,0x00,0xf9,0x59,0x62,0x70,0x07,0x32,0xea,0x2e,0xab,0x40,
17090x2d,0x7b,0xca,0xdd,0x21,0x67,0x1b,0x30,0x99,0x8f,0x16,0xaa,0x23,0xa8,0x41,
17100xd1,0xb0,0x6e,0x11,0x9b,0x36,0xc4,0xde,0x40,0x74,0x9c,0xe1,0x58,0x65,0xc1,
17110x60,0x1e,0x7a,0x5b,0x38,0xc8,0x8f,0xbb,0x04,0x26,0x7c,0xd4,0x16,0x40,0xe5,
17120xb6,0x6b,0x6c,0xaa,0x86,0xfd,0x00,0xbf,0xce,0xc1,0x35,0x02,0x03,0x01,0x00,
17130x01 };
1714
1715static BOOL WINAPI verify_ms_root_policy(LPCSTR szPolicyOID,
1716 PCCERT_CHAIN_CONTEXT pChainContext, PCERT_CHAIN_POLICY_PARA pPolicyPara,
1717 PCERT_CHAIN_POLICY_STATUS pPolicyStatus)
1718{
1719 BOOL ret = verify_base_policy(szPolicyOID, pChainContext, pPolicyPara,
1720 pPolicyStatus);
1721
1722 if (ret && !pPolicyStatus->dwError)
1723 {
1724 CERT_PUBLIC_KEY_INFO msPubKey = { { 0 } };
1725 BOOL isMSRoot = FALSE;
1726 DWORD i;
1727 CRYPT_DATA_BLOB keyBlobs[] = {
1728 { sizeof(msPubKey1), msPubKey1 },
1729 { sizeof(msPubKey2), msPubKey2 },
1730 { sizeof(msPubKey3), msPubKey3 },
1731 };
1732 PCERT_SIMPLE_CHAIN rootChain =
1733 pChainContext->rgpChain[pChainContext->cChain -1 ];
1734 PCCERT_CONTEXT root =
1735 rootChain->rgpElement[rootChain->cElement - 1]->pCertContext;
1736
1737 for (i = 0; !isMSRoot && i < sizeof(keyBlobs) / sizeof(keyBlobs[0]);
1738 i++)
1739 {
1740 msPubKey.PublicKey.cbData = keyBlobs[i].cbData;
1741 msPubKey.PublicKey.pbData = keyBlobs[i].pbData;
1742 if (CertComparePublicKeyInfo(
1743 X509_ASN_ENCODING | PKCS_7_ASN_ENCODING,
1744 &root->pCertInfo->SubjectPublicKeyInfo, &msPubKey))
1745 isMSRoot = TRUE;
1746 }
1747 if (isMSRoot)
1748 pPolicyStatus->lChainIndex = pPolicyStatus->lElementIndex = 0;
1749 }
1750 return ret;
1751}
1752
1753typedef BOOL (*WINAPI CertVerifyCertificateChainPolicyFunc)(LPCSTR szPolicyOID,
1754 PCCERT_CHAIN_CONTEXT pChainContext, PCERT_CHAIN_POLICY_PARA pPolicyPara,
1755 PCERT_CHAIN_POLICY_STATUS pPolicyStatus);
1756
1757BOOL WINAPI CertVerifyCertificateChainPolicy(LPCSTR szPolicyOID,
1758 PCCERT_CHAIN_CONTEXT pChainContext, PCERT_CHAIN_POLICY_PARA pPolicyPara,
1759 PCERT_CHAIN_POLICY_STATUS pPolicyStatus)
1760{
1761 static HCRYPTOIDFUNCSET set = NULL;
1762 BOOL ret = FALSE;
1763 CertVerifyCertificateChainPolicyFunc verifyPolicy = NULL;
1764 HCRYPTOIDFUNCADDR hFunc = NULL;
1765
1766 TRACE("(%s, %p, %p, %p)\n", debugstr_a(szPolicyOID), pChainContext,
1767 pPolicyPara, pPolicyStatus);
1768
1769 if (!HIWORD(szPolicyOID))
1770 {
1771 switch (LOWORD(szPolicyOID))
1772 {
1773 case LOWORD(CERT_CHAIN_POLICY_BASE):
1774 verifyPolicy = verify_base_policy;
1775 break;
1776 case LOWORD(CERT_CHAIN_POLICY_AUTHENTICODE):
1777 verifyPolicy = verify_authenticode_policy;
1778 break;
1779 case LOWORD(CERT_CHAIN_POLICY_BASIC_CONSTRAINTS):
1780 verifyPolicy = verify_basic_constraints_policy;
1781 break;
1782 case LOWORD(CERT_CHAIN_POLICY_MICROSOFT_ROOT):
1783 verifyPolicy = verify_ms_root_policy;
1784 break;
1785 default:
1786 FIXME("unimplemented for %d\n", LOWORD(szPolicyOID));
1787 }
1788 }
1789 if (!verifyPolicy)
1790 {
1791 if (!set)
1792 set = CryptInitOIDFunctionSet(
1793 CRYPT_OID_VERIFY_CERTIFICATE_CHAIN_POLICY_FUNC, 0);
1794 CryptGetOIDFunctionAddress(set, X509_ASN_ENCODING, szPolicyOID, 0,
1795 (void **)&verifyPolicy, &hFunc);
1796 }
1797 if (verifyPolicy)
1798 ret = verifyPolicy(szPolicyOID, pChainContext, pPolicyPara,
1799 pPolicyStatus);
1800 if (hFunc)
1801 CryptFreeOIDFunctionAddress(hFunc, 0);
1802 return ret;
1803}
Note: See TracBrowser for help on using the repository browser.