- Sync crypt32 and cryptui with Wine head
[reactos.git] / reactos / dll / win32 / crypt32 / chain.c
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 "wincrypt.h"
26 #include "wine/debug.h"
27 #include "wine/unicode.h"
28 #include "crypt32_private.h"
29
30 WINE_DEFAULT_DEBUG_CHANNEL(crypt);
31 WINE_DECLARE_DEBUG_CHANNEL(chain);
32
33 #define DEFAULT_CYCLE_MODULUS 7
34
35 static 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 */
42 typedef 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
53 static 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
62 static 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
70 static const WCHAR rootW[] = { 'R','o','o','t',0 };
71
72 static 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
112 HCERTCHAINENGINE 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 engine;
149 }
150
151 BOOL 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
186 VOID 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
200 static 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
217 void default_chain_engine_free(void)
218 {
219 CertFreeCertificateChainEngine(CRYPT_defaultChainEngine);
220 }
221
222 typedef struct _CertificateChain
223 {
224 CERT_CHAIN_CONTEXT context;
225 HCERTSTORE world;
226 LONG ref;
227 } CertificateChain, *PCertificateChain;
228
229 static inline BOOL CRYPT_IsCertificateSelfSigned(PCCERT_CONTEXT cert)
230 {
231 return CertCompareCertificateName(cert->dwCertEncodingType,
232 &cert->pCertInfo->Subject, &cert->pCertInfo->Issuer);
233 }
234
235 static void CRYPT_FreeChainElement(PCERT_CHAIN_ELEMENT element)
236 {
237 CertFreeCertificateContext(element->pCertContext);
238 CryptMemFree(element);
239 }
240
241 static 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 */
265 static 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
274 static 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
285 static 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
320 static 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
330 static 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
350 static 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_(chain)("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 */
372 static 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, &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. A root certificate is assumed to be
416 * allowed to be a CA whether or not the basic constraints extension is present,
417 * whereas an intermediate CA cert is not. This matches the expected usage in
418 * RFC 3280: a conforming intermediate CA MUST contain the basic constraints
419 * extension. It also appears to match Microsoft's implementation.
420 * Updates chainConstraints with the element's constraints, if:
421 * 1. chainConstraints doesn't have a path length constraint, or
422 * 2. element's path length constraint is smaller than chainConstraints's
423 * Sets *pathLengthConstraintViolated to TRUE if a path length violation
424 * occurs.
425 * Returns TRUE if the element can be a CA, and the length of the remaining
426 * chain is valid.
427 */
428 static BOOL CRYPT_CheckBasicConstraintsForCA(PCCERT_CONTEXT cert,
429 CERT_BASIC_CONSTRAINTS2_INFO *chainConstraints, DWORD remainingCAs,
430 BOOL isRoot, BOOL *pathLengthConstraintViolated)
431 {
432 BOOL validBasicConstraints;
433 CERT_BASIC_CONSTRAINTS2_INFO constraints;
434
435 if ((validBasicConstraints = CRYPT_DecodeBasicConstraints(cert,
436 &constraints, isRoot)))
437 {
438 if (!constraints.fCA)
439 {
440 TRACE_(chain)("chain element %d can't be a CA\n", remainingCAs + 1);
441 validBasicConstraints = FALSE;
442 }
443 else if (constraints.fPathLenConstraint)
444 {
445 /* If the element has path length constraints, they apply to the
446 * entire remaining chain.
447 */
448 if (!chainConstraints->fPathLenConstraint ||
449 constraints.dwPathLenConstraint <
450 chainConstraints->dwPathLenConstraint)
451 {
452 TRACE_(chain)("setting path length constraint to %d\n",
453 chainConstraints->dwPathLenConstraint);
454 chainConstraints->fPathLenConstraint = TRUE;
455 chainConstraints->dwPathLenConstraint =
456 constraints.dwPathLenConstraint;
457 }
458 }
459 }
460 if (chainConstraints->fPathLenConstraint &&
461 remainingCAs > chainConstraints->dwPathLenConstraint)
462 {
463 TRACE_(chain)("remaining CAs %d exceed max path length %d\n",
464 remainingCAs, chainConstraints->dwPathLenConstraint);
465 validBasicConstraints = FALSE;
466 *pathLengthConstraintViolated = TRUE;
467 }
468 return validBasicConstraints;
469 }
470
471 static BOOL url_matches(LPCWSTR constraint, LPCWSTR name,
472 DWORD *trustErrorStatus)
473 {
474 BOOL match = FALSE;
475
476 TRACE("%s, %s\n", debugstr_w(constraint), debugstr_w(name));
477
478 if (!constraint)
479 *trustErrorStatus |= CERT_TRUST_INVALID_NAME_CONSTRAINTS;
480 else if (!name)
481 ; /* no match */
482 else if (constraint[0] == '.')
483 {
484 if (lstrlenW(name) > lstrlenW(constraint))
485 match = !lstrcmpiW(name + lstrlenW(name) - lstrlenW(constraint),
486 constraint);
487 }
488 else
489 match = !lstrcmpiW(constraint, name);
490 return match;
491 }
492
493 static BOOL rfc822_name_matches(LPCWSTR constraint, LPCWSTR name,
494 DWORD *trustErrorStatus)
495 {
496 BOOL match = FALSE;
497 LPCWSTR at;
498
499 TRACE("%s, %s\n", debugstr_w(constraint), debugstr_w(name));
500
501 if (!constraint)
502 *trustErrorStatus |= CERT_TRUST_INVALID_NAME_CONSTRAINTS;
503 else if (!name)
504 ; /* no match */
505 else if ((at = strchrW(constraint, '@')))
506 match = !lstrcmpiW(constraint, name);
507 else
508 {
509 if ((at = strchrW(name, '@')))
510 match = url_matches(constraint, at + 1, trustErrorStatus);
511 else
512 match = !lstrcmpiW(constraint, name);
513 }
514 return match;
515 }
516
517 static BOOL dns_name_matches(LPCWSTR constraint, LPCWSTR name,
518 DWORD *trustErrorStatus)
519 {
520 BOOL match = FALSE;
521
522 TRACE("%s, %s\n", debugstr_w(constraint), debugstr_w(name));
523
524 if (!constraint)
525 *trustErrorStatus |= CERT_TRUST_INVALID_NAME_CONSTRAINTS;
526 else if (!name)
527 ; /* no match */
528 else if (lstrlenW(name) >= lstrlenW(constraint))
529 match = !lstrcmpiW(name + lstrlenW(name) - lstrlenW(constraint),
530 constraint);
531 /* else: name is too short, no match */
532
533 return match;
534 }
535
536 static BOOL ip_address_matches(const CRYPT_DATA_BLOB *constraint,
537 const CRYPT_DATA_BLOB *name, DWORD *trustErrorStatus)
538 {
539 BOOL match = FALSE;
540
541 TRACE("(%d, %p), (%d, %p)\n", constraint->cbData, constraint->pbData,
542 name->cbData, name->pbData);
543
544 if (constraint->cbData != sizeof(DWORD) * 2)
545 *trustErrorStatus |= CERT_TRUST_INVALID_NAME_CONSTRAINTS;
546 else if (name->cbData == sizeof(DWORD))
547 {
548 DWORD subnet, mask, addr;
549
550 memcpy(&subnet, constraint->pbData, sizeof(subnet));
551 memcpy(&mask, constraint->pbData + sizeof(subnet), sizeof(mask));
552 memcpy(&addr, name->pbData, sizeof(addr));
553 /* These are really in big-endian order, but for equality matching we
554 * don't need to swap to host order
555 */
556 match = (subnet & mask) == (addr & mask);
557 }
558 /* else: name is wrong size, no match */
559
560 return match;
561 }
562
563 static void CRYPT_FindMatchingNameEntry(const CERT_ALT_NAME_ENTRY *constraint,
564 const CERT_ALT_NAME_INFO *subjectName, DWORD *trustErrorStatus,
565 DWORD errorIfFound, DWORD errorIfNotFound)
566 {
567 DWORD i;
568 BOOL match = FALSE;
569
570 for (i = 0; i < subjectName->cAltEntry; i++)
571 {
572 if (subjectName->rgAltEntry[i].dwAltNameChoice ==
573 constraint->dwAltNameChoice)
574 {
575 switch (constraint->dwAltNameChoice)
576 {
577 case CERT_ALT_NAME_RFC822_NAME:
578 match = rfc822_name_matches(constraint->u.pwszURL,
579 subjectName->rgAltEntry[i].u.pwszURL, trustErrorStatus);
580 break;
581 case CERT_ALT_NAME_DNS_NAME:
582 match = dns_name_matches(constraint->u.pwszURL,
583 subjectName->rgAltEntry[i].u.pwszURL, trustErrorStatus);
584 break;
585 case CERT_ALT_NAME_URL:
586 match = url_matches(constraint->u.pwszURL,
587 subjectName->rgAltEntry[i].u.pwszURL, trustErrorStatus);
588 break;
589 case CERT_ALT_NAME_IP_ADDRESS:
590 match = ip_address_matches(&constraint->u.IPAddress,
591 &subjectName->rgAltEntry[i].u.IPAddress, trustErrorStatus);
592 break;
593 case CERT_ALT_NAME_DIRECTORY_NAME:
594 default:
595 ERR("name choice %d unsupported in this context\n",
596 constraint->dwAltNameChoice);
597 *trustErrorStatus |=
598 CERT_TRUST_HAS_NOT_SUPPORTED_NAME_CONSTRAINT;
599 }
600 }
601 }
602 *trustErrorStatus |= match ? errorIfFound : errorIfNotFound;
603 }
604
605 static void CRYPT_CheckNameConstraints(
606 const CERT_NAME_CONSTRAINTS_INFO *nameConstraints, const CERT_INFO *cert,
607 DWORD *trustErrorStatus)
608 {
609 /* If there aren't any existing constraints, don't bother checking */
610 if (nameConstraints->cPermittedSubtree || nameConstraints->cExcludedSubtree)
611 {
612 CERT_EXTENSION *ext;
613
614 if ((ext = CertFindExtension(szOID_SUBJECT_ALT_NAME, cert->cExtension,
615 cert->rgExtension)))
616 {
617 CERT_ALT_NAME_INFO *subjectName;
618 DWORD size;
619
620 if (CryptDecodeObjectEx(X509_ASN_ENCODING, X509_ALTERNATE_NAME,
621 ext->Value.pbData, ext->Value.cbData,
622 CRYPT_DECODE_ALLOC_FLAG | CRYPT_DECODE_NOCOPY_FLAG, NULL,
623 &subjectName, &size))
624 {
625 DWORD i;
626
627 for (i = 0; i < nameConstraints->cExcludedSubtree; i++)
628 CRYPT_FindMatchingNameEntry(
629 &nameConstraints->rgExcludedSubtree[i].Base, subjectName,
630 trustErrorStatus,
631 CERT_TRUST_HAS_EXCLUDED_NAME_CONSTRAINT, 0);
632 for (i = 0; i < nameConstraints->cPermittedSubtree; i++)
633 CRYPT_FindMatchingNameEntry(
634 &nameConstraints->rgPermittedSubtree[i].Base, subjectName,
635 trustErrorStatus,
636 0, CERT_TRUST_HAS_NOT_PERMITTED_NAME_CONSTRAINT);
637 LocalFree(subjectName);
638 }
639 }
640 else
641 {
642 if (nameConstraints->cPermittedSubtree)
643 *trustErrorStatus |=
644 CERT_TRUST_HAS_NOT_PERMITTED_NAME_CONSTRAINT;
645 if (nameConstraints->cExcludedSubtree)
646 *trustErrorStatus |=
647 CERT_TRUST_HAS_EXCLUDED_NAME_CONSTRAINT;
648 }
649 }
650 }
651
652 /* Gets cert's name constraints, if any. Free with LocalFree. */
653 static CERT_NAME_CONSTRAINTS_INFO *CRYPT_GetNameConstraints(CERT_INFO *cert)
654 {
655 CERT_NAME_CONSTRAINTS_INFO *info = NULL;
656
657 CERT_EXTENSION *ext;
658
659 if ((ext = CertFindExtension(szOID_NAME_CONSTRAINTS, cert->cExtension,
660 cert->rgExtension)))
661 {
662 DWORD size;
663
664 CryptDecodeObjectEx(X509_ASN_ENCODING, X509_NAME_CONSTRAINTS,
665 ext->Value.pbData, ext->Value.cbData,
666 CRYPT_DECODE_ALLOC_FLAG | CRYPT_DECODE_NOCOPY_FLAG, NULL, &info,
667 &size);
668 }
669 return info;
670 }
671
672 static void CRYPT_CheckChainNameConstraints(PCERT_SIMPLE_CHAIN chain)
673 {
674 int i, j;
675
676 /* Microsoft's implementation appears to violate RFC 3280: according to
677 * MSDN, the various CERT_TRUST_*_NAME_CONSTRAINT errors are set if a CA's
678 * name constraint is violated in the end cert. According to RFC 3280,
679 * the constraints should be checked against every subsequent certificate
680 * in the chain, not just the end cert.
681 * Microsoft's implementation also sets the name constraint errors on the
682 * certs whose constraints were violated, not on the certs that violated
683 * them.
684 * In order to be error-compatible with Microsoft's implementation, while
685 * still adhering to RFC 3280, I use a O(n ^ 2) algorithm to check name
686 * constraints.
687 */
688 for (i = chain->cElement - 1; i > 0; i--)
689 {
690 CERT_NAME_CONSTRAINTS_INFO *nameConstraints;
691
692 if ((nameConstraints = CRYPT_GetNameConstraints(
693 chain->rgpElement[i]->pCertContext->pCertInfo)))
694 {
695 for (j = i - 1; j >= 0; j--)
696 {
697 DWORD errorStatus = 0;
698
699 /* According to RFC 3280, self-signed certs don't have name
700 * constraints checked unless they're the end cert.
701 */
702 if (j == 0 || !CRYPT_IsCertificateSelfSigned(
703 chain->rgpElement[j]->pCertContext))
704 {
705 CRYPT_CheckNameConstraints(nameConstraints,
706 chain->rgpElement[i]->pCertContext->pCertInfo,
707 &errorStatus);
708 chain->rgpElement[i]->TrustStatus.dwErrorStatus |=
709 errorStatus;
710 }
711 }
712 LocalFree(nameConstraints);
713 }
714 }
715 }
716
717 static void dump_basic_constraints(PCERT_EXTENSION ext)
718 {
719 CERT_BASIC_CONSTRAINTS_INFO *info;
720 DWORD size = 0;
721
722 if (CryptDecodeObjectEx(X509_ASN_ENCODING, szOID_BASIC_CONSTRAINTS,
723 ext->Value.pbData, ext->Value.cbData, CRYPT_DECODE_ALLOC_FLAG,
724 NULL, &info, &size))
725 {
726 TRACE_(chain)("SubjectType: %02x\n", info->SubjectType.pbData[0]);
727 TRACE_(chain)("%s path length constraint\n",
728 info->fPathLenConstraint ? "has" : "doesn't have");
729 TRACE_(chain)("path length=%d\n", info->dwPathLenConstraint);
730 LocalFree(info);
731 }
732 }
733
734 static void dump_basic_constraints2(PCERT_EXTENSION ext)
735 {
736 CERT_BASIC_CONSTRAINTS2_INFO constraints;
737 DWORD size = sizeof(CERT_BASIC_CONSTRAINTS2_INFO);
738
739 if (CryptDecodeObjectEx(X509_ASN_ENCODING,
740 szOID_BASIC_CONSTRAINTS2, ext->Value.pbData, ext->Value.cbData,
741 0, NULL, &constraints, &size))
742 {
743 TRACE_(chain)("basic constraints:\n");
744 TRACE_(chain)("can%s be a CA\n", constraints.fCA ? "" : "not");
745 TRACE_(chain)("%s path length constraint\n",
746 constraints.fPathLenConstraint ? "has" : "doesn't have");
747 TRACE_(chain)("path length=%d\n", constraints.dwPathLenConstraint);
748 }
749 }
750
751 static void dump_extension(PCERT_EXTENSION ext)
752 {
753 TRACE_(chain)("%s (%scritical)\n", debugstr_a(ext->pszObjId),
754 ext->fCritical ? "" : "not ");
755 if (!strcmp(ext->pszObjId, szOID_BASIC_CONSTRAINTS))
756 dump_basic_constraints(ext);
757 else if (!strcmp(ext->pszObjId, szOID_BASIC_CONSTRAINTS2))
758 dump_basic_constraints2(ext);
759 }
760
761 static LPCWSTR filetime_to_str(const FILETIME *time)
762 {
763 static WCHAR date[80];
764 WCHAR dateFmt[80]; /* sufficient for all versions of LOCALE_SSHORTDATE */
765 SYSTEMTIME sysTime;
766
767 if (!time) return NULL;
768
769 GetLocaleInfoW(LOCALE_SYSTEM_DEFAULT, LOCALE_SSHORTDATE, dateFmt,
770 sizeof(dateFmt) / sizeof(dateFmt[0]));
771 FileTimeToSystemTime(time, &sysTime);
772 GetDateFormatW(LOCALE_SYSTEM_DEFAULT, 0, &sysTime, dateFmt, date,
773 sizeof(date) / sizeof(date[0]));
774 return date;
775 }
776
777 static void dump_element(PCCERT_CONTEXT cert)
778 {
779 LPWSTR name = NULL;
780 DWORD len, i;
781
782 TRACE_(chain)("%p\n", cert);
783 len = CertGetNameStringW(cert, CERT_NAME_SIMPLE_DISPLAY_TYPE,
784 CERT_NAME_ISSUER_FLAG, NULL, NULL, 0);
785 name = CryptMemAlloc(len * sizeof(WCHAR));
786 if (name)
787 {
788 CertGetNameStringW(cert, CERT_NAME_SIMPLE_DISPLAY_TYPE,
789 CERT_NAME_ISSUER_FLAG, NULL, name, len);
790 TRACE_(chain)("issued by %s\n", debugstr_w(name));
791 CryptMemFree(name);
792 }
793 len = CertGetNameStringW(cert, CERT_NAME_SIMPLE_DISPLAY_TYPE, 0, NULL,
794 NULL, 0);
795 name = CryptMemAlloc(len * sizeof(WCHAR));
796 if (name)
797 {
798 CertGetNameStringW(cert, CERT_NAME_SIMPLE_DISPLAY_TYPE, 0, NULL,
799 name, len);
800 TRACE_(chain)("issued to %s\n", debugstr_w(name));
801 CryptMemFree(name);
802 }
803 TRACE_(chain)("valid from %s to %s\n",
804 debugstr_w(filetime_to_str(&cert->pCertInfo->NotBefore)),
805 debugstr_w(filetime_to_str(&cert->pCertInfo->NotAfter)));
806 TRACE_(chain)("%d extensions\n", cert->pCertInfo->cExtension);
807 for (i = 0; i < cert->pCertInfo->cExtension; i++)
808 dump_extension(&cert->pCertInfo->rgExtension[i]);
809 }
810
811 static void CRYPT_CheckSimpleChain(PCertificateChainEngine engine,
812 PCERT_SIMPLE_CHAIN chain, LPFILETIME time)
813 {
814 PCERT_CHAIN_ELEMENT rootElement = chain->rgpElement[chain->cElement - 1];
815 int i;
816 BOOL pathLengthConstraintViolated = FALSE;
817 CERT_BASIC_CONSTRAINTS2_INFO constraints = { TRUE, FALSE, 0 };
818
819 TRACE_(chain)("checking chain with %d elements for time %s\n",
820 chain->cElement, debugstr_w(filetime_to_str(time)));
821 for (i = chain->cElement - 1; i >= 0; i--)
822 {
823 if (TRACE_ON(chain))
824 dump_element(chain->rgpElement[i]->pCertContext);
825 if (CertVerifyTimeValidity(time,
826 chain->rgpElement[i]->pCertContext->pCertInfo) != 0)
827 chain->rgpElement[i]->TrustStatus.dwErrorStatus |=
828 CERT_TRUST_IS_NOT_TIME_VALID;
829 if (i != 0)
830 {
831 BOOL isRoot;
832
833 if (i == chain->cElement - 1)
834 isRoot = CRYPT_IsCertificateSelfSigned(
835 chain->rgpElement[i]->pCertContext);
836 else
837 isRoot = FALSE;
838 /* Check the signature of the cert this issued */
839 if (!CryptVerifyCertificateSignatureEx(0, X509_ASN_ENCODING,
840 CRYPT_VERIFY_CERT_SIGN_SUBJECT_CERT,
841 (void *)chain->rgpElement[i - 1]->pCertContext,
842 CRYPT_VERIFY_CERT_SIGN_ISSUER_CERT,
843 (void *)chain->rgpElement[i]->pCertContext, 0, NULL))
844 chain->rgpElement[i - 1]->TrustStatus.dwErrorStatus |=
845 CERT_TRUST_IS_NOT_SIGNATURE_VALID;
846 /* Once a path length constraint has been violated, every remaining
847 * CA cert's basic constraints is considered invalid.
848 */
849 if (pathLengthConstraintViolated)
850 chain->rgpElement[i]->TrustStatus.dwErrorStatus |=
851 CERT_TRUST_INVALID_BASIC_CONSTRAINTS;
852 else if (!CRYPT_CheckBasicConstraintsForCA(
853 chain->rgpElement[i]->pCertContext, &constraints, i - 1,
854 isRoot, &pathLengthConstraintViolated))
855 chain->rgpElement[i]->TrustStatus.dwErrorStatus |=
856 CERT_TRUST_INVALID_BASIC_CONSTRAINTS;
857 else if (constraints.fPathLenConstraint &&
858 constraints.dwPathLenConstraint)
859 {
860 /* This one's valid - decrement max length */
861 constraints.dwPathLenConstraint--;
862 }
863 }
864 if (CRYPT_IsSimpleChainCyclic(chain))
865 {
866 /* If the chain is cyclic, then the path length constraints
867 * are violated, because the chain is infinitely long.
868 */
869 pathLengthConstraintViolated = TRUE;
870 chain->TrustStatus.dwErrorStatus |=
871 CERT_TRUST_IS_PARTIAL_CHAIN |
872 CERT_TRUST_INVALID_BASIC_CONSTRAINTS;
873 }
874 /* FIXME: check valid usages */
875 CRYPT_CombineTrustStatus(&chain->TrustStatus,
876 &chain->rgpElement[i]->TrustStatus);
877 }
878 CRYPT_CheckChainNameConstraints(chain);
879 if (CRYPT_IsCertificateSelfSigned(rootElement->pCertContext))
880 {
881 rootElement->TrustStatus.dwInfoStatus |=
882 CERT_TRUST_IS_SELF_SIGNED | CERT_TRUST_HAS_NAME_MATCH_ISSUER;
883 CRYPT_CheckRootCert(engine->hRoot, rootElement);
884 }
885 CRYPT_CombineTrustStatus(&chain->TrustStatus, &rootElement->TrustStatus);
886 }
887
888 static PCCERT_CONTEXT CRYPT_GetIssuer(HCERTSTORE store, PCCERT_CONTEXT subject,
889 PCCERT_CONTEXT prevIssuer, DWORD *infoStatus)
890 {
891 PCCERT_CONTEXT issuer = NULL;
892 PCERT_EXTENSION ext;
893 DWORD size;
894
895 *infoStatus = 0;
896 if ((ext = CertFindExtension(szOID_AUTHORITY_KEY_IDENTIFIER,
897 subject->pCertInfo->cExtension, subject->pCertInfo->rgExtension)))
898 {
899 CERT_AUTHORITY_KEY_ID_INFO *info;
900 BOOL ret;
901
902 ret = CryptDecodeObjectEx(subject->dwCertEncodingType,
903 X509_AUTHORITY_KEY_ID, ext->Value.pbData, ext->Value.cbData,
904 CRYPT_DECODE_ALLOC_FLAG | CRYPT_DECODE_NOCOPY_FLAG, NULL,
905 &info, &size);
906 if (ret)
907 {
908 CERT_ID id;
909
910 if (info->CertIssuer.cbData && info->CertSerialNumber.cbData)
911 {
912 id.dwIdChoice = CERT_ID_ISSUER_SERIAL_NUMBER;
913 memcpy(&id.u.IssuerSerialNumber.Issuer, &info->CertIssuer,
914 sizeof(CERT_NAME_BLOB));
915 memcpy(&id.u.IssuerSerialNumber.SerialNumber,
916 &info->CertSerialNumber, sizeof(CRYPT_INTEGER_BLOB));
917 issuer = CertFindCertificateInStore(store,
918 subject->dwCertEncodingType, 0, CERT_FIND_CERT_ID, &id,
919 prevIssuer);
920 if (issuer)
921 *infoStatus = CERT_TRUST_HAS_EXACT_MATCH_ISSUER;
922 }
923 else if (info->KeyId.cbData)
924 {
925 id.dwIdChoice = CERT_ID_KEY_IDENTIFIER;
926 memcpy(&id.u.KeyId, &info->KeyId, sizeof(CRYPT_HASH_BLOB));
927 issuer = CertFindCertificateInStore(store,
928 subject->dwCertEncodingType, 0, CERT_FIND_CERT_ID, &id,
929 prevIssuer);
930 if (issuer)
931 *infoStatus = CERT_TRUST_HAS_KEY_MATCH_ISSUER;
932 }
933 LocalFree(info);
934 }
935 }
936 else if ((ext = CertFindExtension(szOID_AUTHORITY_KEY_IDENTIFIER2,
937 subject->pCertInfo->cExtension, subject->pCertInfo->rgExtension)))
938 {
939 CERT_AUTHORITY_KEY_ID2_INFO *info;
940 BOOL ret;
941
942 ret = CryptDecodeObjectEx(subject->dwCertEncodingType,
943 X509_AUTHORITY_KEY_ID2, ext->Value.pbData, ext->Value.cbData,
944 CRYPT_DECODE_ALLOC_FLAG | CRYPT_DECODE_NOCOPY_FLAG, NULL,
945 &info, &size);
946 if (ret)
947 {
948 CERT_ID id;
949
950 if (info->AuthorityCertIssuer.cAltEntry &&
951 info->AuthorityCertSerialNumber.cbData)
952 {
953 PCERT_ALT_NAME_ENTRY directoryName = NULL;
954 DWORD i;
955
956 for (i = 0; !directoryName &&
957 i < info->AuthorityCertIssuer.cAltEntry; i++)
958 if (info->AuthorityCertIssuer.rgAltEntry[i].dwAltNameChoice
959 == CERT_ALT_NAME_DIRECTORY_NAME)
960 directoryName =
961 &info->AuthorityCertIssuer.rgAltEntry[i];
962 if (directoryName)
963 {
964 id.dwIdChoice = CERT_ID_ISSUER_SERIAL_NUMBER;
965 memcpy(&id.u.IssuerSerialNumber.Issuer,
966 &directoryName->u.DirectoryName, sizeof(CERT_NAME_BLOB));
967 memcpy(&id.u.IssuerSerialNumber.SerialNumber,
968 &info->AuthorityCertSerialNumber,
969 sizeof(CRYPT_INTEGER_BLOB));
970 issuer = CertFindCertificateInStore(store,
971 subject->dwCertEncodingType, 0, CERT_FIND_CERT_ID, &id,
972 prevIssuer);
973 if (issuer)
974 *infoStatus = CERT_TRUST_HAS_EXACT_MATCH_ISSUER;
975 }
976 else
977 FIXME("no supported name type in authority key id2\n");
978 }
979 else if (info->KeyId.cbData)
980 {
981 id.dwIdChoice = CERT_ID_KEY_IDENTIFIER;
982 memcpy(&id.u.KeyId, &info->KeyId, sizeof(CRYPT_HASH_BLOB));
983 issuer = CertFindCertificateInStore(store,
984 subject->dwCertEncodingType, 0, CERT_FIND_CERT_ID, &id,
985 prevIssuer);
986 if (issuer)
987 *infoStatus = CERT_TRUST_HAS_KEY_MATCH_ISSUER;
988 }
989 LocalFree(info);
990 }
991 }
992 else
993 {
994 issuer = CertFindCertificateInStore(store,
995 subject->dwCertEncodingType, 0, CERT_FIND_SUBJECT_NAME,
996 &subject->pCertInfo->Issuer, prevIssuer);
997 *infoStatus = CERT_TRUST_HAS_NAME_MATCH_ISSUER;
998 }
999 return issuer;
1000 }
1001
1002 /* Builds a simple chain by finding an issuer for the last cert in the chain,
1003 * until reaching a self-signed cert, or until no issuer can be found.
1004 */
1005 static BOOL CRYPT_BuildSimpleChain(PCertificateChainEngine engine,
1006 HCERTSTORE world, PCERT_SIMPLE_CHAIN chain)
1007 {
1008 BOOL ret = TRUE;
1009 PCCERT_CONTEXT cert = chain->rgpElement[chain->cElement - 1]->pCertContext;
1010
1011 while (ret && !CRYPT_IsSimpleChainCyclic(chain) &&
1012 !CRYPT_IsCertificateSelfSigned(cert))
1013 {
1014 PCCERT_CONTEXT issuer = CRYPT_GetIssuer(world, cert, NULL,
1015 &chain->rgpElement[chain->cElement - 1]->TrustStatus.dwInfoStatus);
1016
1017 if (issuer)
1018 {
1019 ret = CRYPT_AddCertToSimpleChain(engine, chain, issuer,
1020 chain->rgpElement[chain->cElement - 1]->TrustStatus.dwInfoStatus);
1021 /* CRYPT_AddCertToSimpleChain add-ref's the issuer, so free it to
1022 * close the enumeration that found it
1023 */
1024 CertFreeCertificateContext(issuer);
1025 cert = issuer;
1026 }
1027 else
1028 {
1029 TRACE_(chain)("Couldn't find issuer, halting chain creation\n");
1030 chain->TrustStatus.dwErrorStatus |= CERT_TRUST_IS_PARTIAL_CHAIN;
1031 break;
1032 }
1033 }
1034 return ret;
1035 }
1036
1037 static BOOL CRYPT_GetSimpleChainForCert(PCertificateChainEngine engine,
1038 HCERTSTORE world, PCCERT_CONTEXT cert, LPFILETIME pTime,
1039 PCERT_SIMPLE_CHAIN *ppChain)
1040 {
1041 BOOL ret = FALSE;
1042 PCERT_SIMPLE_CHAIN chain;
1043
1044 TRACE("(%p, %p, %p, %p)\n", engine, world, cert, pTime);
1045
1046 chain = CryptMemAlloc(sizeof(CERT_SIMPLE_CHAIN));
1047 if (chain)
1048 {
1049 memset(chain, 0, sizeof(CERT_SIMPLE_CHAIN));
1050 chain->cbSize = sizeof(CERT_SIMPLE_CHAIN);
1051 ret = CRYPT_AddCertToSimpleChain(engine, chain, cert, 0);
1052 if (ret)
1053 {
1054 ret = CRYPT_BuildSimpleChain(engine, world, chain);
1055 if (ret)
1056 CRYPT_CheckSimpleChain(engine, chain, pTime);
1057 }
1058 if (!ret)
1059 {
1060 CRYPT_FreeSimpleChain(chain);
1061 chain = NULL;
1062 }
1063 *ppChain = chain;
1064 }
1065 return ret;
1066 }
1067
1068 static BOOL CRYPT_BuildCandidateChainFromCert(HCERTCHAINENGINE hChainEngine,
1069 PCCERT_CONTEXT cert, LPFILETIME pTime, HCERTSTORE hAdditionalStore,
1070 PCertificateChain *ppChain)
1071 {
1072 PCertificateChainEngine engine = (PCertificateChainEngine)hChainEngine;
1073 PCERT_SIMPLE_CHAIN simpleChain = NULL;
1074 HCERTSTORE world;
1075 BOOL ret;
1076
1077 world = CertOpenStore(CERT_STORE_PROV_COLLECTION, 0, 0,
1078 CERT_STORE_CREATE_NEW_FLAG, NULL);
1079 CertAddStoreToCollection(world, engine->hWorld, 0, 0);
1080 if (hAdditionalStore)
1081 CertAddStoreToCollection(world, hAdditionalStore, 0, 0);
1082 /* FIXME: only simple chains are supported for now, as CTLs aren't
1083 * supported yet.
1084 */
1085 if ((ret = CRYPT_GetSimpleChainForCert(engine, world, cert, pTime,
1086 &simpleChain)))
1087 {
1088 PCertificateChain chain = CryptMemAlloc(sizeof(CertificateChain));
1089
1090 if (chain)
1091 {
1092 chain->ref = 1;
1093 chain->world = world;
1094 chain->context.cbSize = sizeof(CERT_CHAIN_CONTEXT);
1095 chain->context.TrustStatus = simpleChain->TrustStatus;
1096 chain->context.cChain = 1;
1097 chain->context.rgpChain = CryptMemAlloc(sizeof(PCERT_SIMPLE_CHAIN));
1098 chain->context.rgpChain[0] = simpleChain;
1099 chain->context.cLowerQualityChainContext = 0;
1100 chain->context.rgpLowerQualityChainContext = NULL;
1101 chain->context.fHasRevocationFreshnessTime = FALSE;
1102 chain->context.dwRevocationFreshnessTime = 0;
1103 }
1104 else
1105 ret = FALSE;
1106 *ppChain = chain;
1107 }
1108 return ret;
1109 }
1110
1111 /* Makes and returns a copy of chain, up to and including element iElement. */
1112 static PCERT_SIMPLE_CHAIN CRYPT_CopySimpleChainToElement(
1113 PCERT_SIMPLE_CHAIN chain, DWORD iElement)
1114 {
1115 PCERT_SIMPLE_CHAIN copy = CryptMemAlloc(sizeof(CERT_SIMPLE_CHAIN));
1116
1117 if (copy)
1118 {
1119 memset(copy, 0, sizeof(CERT_SIMPLE_CHAIN));
1120 copy->cbSize = sizeof(CERT_SIMPLE_CHAIN);
1121 copy->rgpElement =
1122 CryptMemAlloc((iElement + 1) * sizeof(PCERT_CHAIN_ELEMENT));
1123 if (copy->rgpElement)
1124 {
1125 DWORD i;
1126 BOOL ret = TRUE;
1127
1128 memset(copy->rgpElement, 0,
1129 (iElement + 1) * sizeof(PCERT_CHAIN_ELEMENT));
1130 for (i = 0; ret && i <= iElement; i++)
1131 {
1132 PCERT_CHAIN_ELEMENT element =
1133 CryptMemAlloc(sizeof(CERT_CHAIN_ELEMENT));
1134
1135 if (element)
1136 {
1137 *element = *chain->rgpElement[i];
1138 element->pCertContext = CertDuplicateCertificateContext(
1139 chain->rgpElement[i]->pCertContext);
1140 /* Reset the trust status of the copied element, it'll get
1141 * rechecked after the new chain is done.
1142 */
1143 memset(&element->TrustStatus, 0, sizeof(CERT_TRUST_STATUS));
1144 copy->rgpElement[copy->cElement++] = element;
1145 }
1146 else
1147 ret = FALSE;
1148 }
1149 if (!ret)
1150 {
1151 for (i = 0; i <= iElement; i++)
1152 CryptMemFree(copy->rgpElement[i]);
1153 CryptMemFree(copy->rgpElement);
1154 CryptMemFree(copy);
1155 copy = NULL;
1156 }
1157 }
1158 else
1159 {
1160 CryptMemFree(copy);
1161 copy = NULL;
1162 }
1163 }
1164 return copy;
1165 }
1166
1167 static void CRYPT_FreeLowerQualityChains(PCertificateChain chain)
1168 {
1169 DWORD i;
1170
1171 for (i = 0; i < chain->context.cLowerQualityChainContext; i++)
1172 CertFreeCertificateChain(chain->context.rgpLowerQualityChainContext[i]);
1173 CryptMemFree(chain->context.rgpLowerQualityChainContext);
1174 chain->context.cLowerQualityChainContext = 0;
1175 chain->context.rgpLowerQualityChainContext = NULL;
1176 }
1177
1178 static void CRYPT_FreeChainContext(PCertificateChain chain)
1179 {
1180 DWORD i;
1181
1182 CRYPT_FreeLowerQualityChains(chain);
1183 for (i = 0; i < chain->context.cChain; i++)
1184 CRYPT_FreeSimpleChain(chain->context.rgpChain[i]);
1185 CryptMemFree(chain->context.rgpChain);
1186 CertCloseStore(chain->world, 0);
1187 CryptMemFree(chain);
1188 }
1189
1190 /* Makes and returns a copy of chain, up to and including element iElement of
1191 * simple chain iChain.
1192 */
1193 static PCertificateChain CRYPT_CopyChainToElement(PCertificateChain chain,
1194 DWORD iChain, DWORD iElement)
1195 {
1196 PCertificateChain copy = CryptMemAlloc(sizeof(CertificateChain));
1197
1198 if (copy)
1199 {
1200 copy->ref = 1;
1201 copy->world = CertDuplicateStore(chain->world);
1202 copy->context.cbSize = sizeof(CERT_CHAIN_CONTEXT);
1203 /* Leave the trust status of the copied chain unset, it'll get
1204 * rechecked after the new chain is done.
1205 */
1206 memset(&copy->context.TrustStatus, 0, sizeof(CERT_TRUST_STATUS));
1207 copy->context.cLowerQualityChainContext = 0;
1208 copy->context.rgpLowerQualityChainContext = NULL;
1209 copy->context.fHasRevocationFreshnessTime = FALSE;
1210 copy->context.dwRevocationFreshnessTime = 0;
1211 copy->context.rgpChain = CryptMemAlloc(
1212 (iChain + 1) * sizeof(PCERT_SIMPLE_CHAIN));
1213 if (copy->context.rgpChain)
1214 {
1215 BOOL ret = TRUE;
1216 DWORD i;
1217
1218 memset(copy->context.rgpChain, 0,
1219 (iChain + 1) * sizeof(PCERT_SIMPLE_CHAIN));
1220 if (iChain)
1221 {
1222 for (i = 0; ret && iChain && i < iChain - 1; i++)
1223 {
1224 copy->context.rgpChain[i] =
1225 CRYPT_CopySimpleChainToElement(chain->context.rgpChain[i],
1226 chain->context.rgpChain[i]->cElement - 1);
1227 if (!copy->context.rgpChain[i])
1228 ret = FALSE;
1229 }
1230 }
1231 else
1232 i = 0;
1233 if (ret)
1234 {
1235 copy->context.rgpChain[i] =
1236 CRYPT_CopySimpleChainToElement(chain->context.rgpChain[i],
1237 iElement);
1238 if (!copy->context.rgpChain[i])
1239 ret = FALSE;
1240 }
1241 if (!ret)
1242 {
1243 CRYPT_FreeChainContext(copy);
1244 copy = NULL;
1245 }
1246 else
1247 copy->context.cChain = iChain + 1;
1248 }
1249 else
1250 {
1251 CryptMemFree(copy);
1252 copy = NULL;
1253 }
1254 }
1255 return copy;
1256 }
1257
1258 static PCertificateChain CRYPT_BuildAlternateContextFromChain(
1259 HCERTCHAINENGINE hChainEngine, LPFILETIME pTime, HCERTSTORE hAdditionalStore,
1260 PCertificateChain chain)
1261 {
1262 PCertificateChainEngine engine = (PCertificateChainEngine)hChainEngine;
1263 PCertificateChain alternate;
1264
1265 TRACE("(%p, %p, %p, %p)\n", hChainEngine, pTime, hAdditionalStore, chain);
1266
1267 /* Always start with the last "lower quality" chain to ensure a consistent
1268 * order of alternate creation:
1269 */
1270 if (chain->context.cLowerQualityChainContext)
1271 chain = (PCertificateChain)chain->context.rgpLowerQualityChainContext[
1272 chain->context.cLowerQualityChainContext - 1];
1273 /* A chain with only one element can't have any alternates */
1274 if (chain->context.cChain <= 1 && chain->context.rgpChain[0]->cElement <= 1)
1275 alternate = NULL;
1276 else
1277 {
1278 DWORD i, j, infoStatus;
1279 PCCERT_CONTEXT alternateIssuer = NULL;
1280
1281 alternate = NULL;
1282 for (i = 0; !alternateIssuer && i < chain->context.cChain; i++)
1283 for (j = 0; !alternateIssuer &&
1284 j < chain->context.rgpChain[i]->cElement - 1; j++)
1285 {
1286 PCCERT_CONTEXT subject =
1287 chain->context.rgpChain[i]->rgpElement[j]->pCertContext;
1288 PCCERT_CONTEXT prevIssuer = CertDuplicateCertificateContext(
1289 chain->context.rgpChain[i]->rgpElement[j + 1]->pCertContext);
1290
1291 alternateIssuer = CRYPT_GetIssuer(prevIssuer->hCertStore,
1292 subject, prevIssuer, &infoStatus);
1293 }
1294 if (alternateIssuer)
1295 {
1296 i--;
1297 j--;
1298 alternate = CRYPT_CopyChainToElement(chain, i, j);
1299 if (alternate)
1300 {
1301 BOOL ret = CRYPT_AddCertToSimpleChain(engine,
1302 alternate->context.rgpChain[i], alternateIssuer, infoStatus);
1303
1304 /* CRYPT_AddCertToSimpleChain add-ref's the issuer, so free it
1305 * to close the enumeration that found it
1306 */
1307 CertFreeCertificateContext(alternateIssuer);
1308 if (ret)
1309 {
1310 ret = CRYPT_BuildSimpleChain(engine, alternate->world,
1311 alternate->context.rgpChain[i]);
1312 if (ret)
1313 CRYPT_CheckSimpleChain(engine,
1314 alternate->context.rgpChain[i], pTime);
1315 CRYPT_CombineTrustStatus(&alternate->context.TrustStatus,
1316 &alternate->context.rgpChain[i]->TrustStatus);
1317 }
1318 if (!ret)
1319 {
1320 CRYPT_FreeChainContext(alternate);
1321 alternate = NULL;
1322 }
1323 }
1324 }
1325 }
1326 TRACE("%p\n", alternate);
1327 return alternate;
1328 }
1329
1330 #define CHAIN_QUALITY_SIGNATURE_VALID 8
1331 #define CHAIN_QUALITY_TIME_VALID 4
1332 #define CHAIN_QUALITY_COMPLETE_CHAIN 2
1333 #define CHAIN_QUALITY_TRUSTED_ROOT 1
1334
1335 #define CHAIN_QUALITY_HIGHEST \
1336 CHAIN_QUALITY_SIGNATURE_VALID | CHAIN_QUALITY_TIME_VALID | \
1337 CHAIN_QUALITY_COMPLETE_CHAIN | CHAIN_QUALITY_TRUSTED_ROOT
1338
1339 #define IS_TRUST_ERROR_SET(TrustStatus, bits) \
1340 (TrustStatus)->dwErrorStatus & (bits)
1341
1342 static DWORD CRYPT_ChainQuality(PCertificateChain chain)
1343 {
1344 DWORD quality = CHAIN_QUALITY_HIGHEST;
1345
1346 if (IS_TRUST_ERROR_SET(&chain->context.TrustStatus,
1347 CERT_TRUST_IS_UNTRUSTED_ROOT))
1348 quality &= ~CHAIN_QUALITY_TRUSTED_ROOT;
1349 if (IS_TRUST_ERROR_SET(&chain->context.TrustStatus,
1350 CERT_TRUST_IS_PARTIAL_CHAIN))
1351 if (chain->context.TrustStatus.dwErrorStatus & CERT_TRUST_IS_PARTIAL_CHAIN)
1352 quality &= ~CHAIN_QUALITY_COMPLETE_CHAIN;
1353 if (IS_TRUST_ERROR_SET(&chain->context.TrustStatus,
1354 CERT_TRUST_IS_NOT_TIME_VALID | CERT_TRUST_IS_NOT_TIME_NESTED))
1355 quality &= ~CHAIN_QUALITY_TIME_VALID;
1356 if (IS_TRUST_ERROR_SET(&chain->context.TrustStatus,
1357 CERT_TRUST_IS_NOT_SIGNATURE_VALID))
1358 quality &= ~CHAIN_QUALITY_SIGNATURE_VALID;
1359 return quality;
1360 }
1361
1362 /* Chooses the highest quality chain among chain and its "lower quality"
1363 * alternate chains. Returns the highest quality chain, with all other
1364 * chains as lower quality chains of it.
1365 */
1366 static PCertificateChain CRYPT_ChooseHighestQualityChain(
1367 PCertificateChain chain)
1368 {
1369 DWORD i;
1370
1371 /* There are always only two chains being considered: chain, and an
1372 * alternate at chain->rgpLowerQualityChainContext[i]. If the alternate
1373 * has a higher quality than chain, the alternate gets assigned the lower
1374 * quality contexts, with chain taking the alternate's place among the
1375 * lower quality contexts.
1376 */
1377 for (i = 0; i < chain->context.cLowerQualityChainContext; i++)
1378 {
1379 PCertificateChain alternate =
1380 (PCertificateChain)chain->context.rgpLowerQualityChainContext[i];
1381
1382 if (CRYPT_ChainQuality(alternate) > CRYPT_ChainQuality(chain))
1383 {
1384 alternate->context.cLowerQualityChainContext =
1385 chain->context.cLowerQualityChainContext;
1386 alternate->context.rgpLowerQualityChainContext =
1387 chain->context.rgpLowerQualityChainContext;
1388 alternate->context.rgpLowerQualityChainContext[i] =
1389 (PCCERT_CHAIN_CONTEXT)chain;
1390 chain->context.cLowerQualityChainContext = 0;
1391 chain->context.rgpLowerQualityChainContext = NULL;
1392 chain = alternate;
1393 }
1394 }
1395 return chain;
1396 }
1397
1398 static BOOL CRYPT_AddAlternateChainToChain(PCertificateChain chain,
1399 PCertificateChain alternate)
1400 {
1401 BOOL ret;
1402
1403 if (chain->context.cLowerQualityChainContext)
1404 chain->context.rgpLowerQualityChainContext =
1405 CryptMemRealloc(chain->context.rgpLowerQualityChainContext,
1406 (chain->context.cLowerQualityChainContext + 1) *
1407 sizeof(PCCERT_CHAIN_CONTEXT));
1408 else
1409 chain->context.rgpLowerQualityChainContext =
1410 CryptMemAlloc(sizeof(PCCERT_CHAIN_CONTEXT));
1411 if (chain->context.rgpLowerQualityChainContext)
1412 {
1413 chain->context.rgpLowerQualityChainContext[
1414 chain->context.cLowerQualityChainContext++] =
1415 (PCCERT_CHAIN_CONTEXT)alternate;
1416 ret = TRUE;
1417 }
1418 else
1419 ret = FALSE;
1420 return ret;
1421 }
1422
1423 static PCERT_CHAIN_ELEMENT CRYPT_FindIthElementInChain(
1424 PCERT_CHAIN_CONTEXT chain, DWORD i)
1425 {
1426 DWORD j, iElement;
1427 PCERT_CHAIN_ELEMENT element = NULL;
1428
1429 for (j = 0, iElement = 0; !element && j < chain->cChain; j++)
1430 {
1431 if (iElement + chain->rgpChain[j]->cElement < i)
1432 iElement += chain->rgpChain[j]->cElement;
1433 else
1434 element = chain->rgpChain[j]->rgpElement[i - iElement];
1435 }
1436 return element;
1437 }
1438
1439 typedef struct _CERT_CHAIN_PARA_NO_EXTRA_FIELDS {
1440 DWORD cbSize;
1441 CERT_USAGE_MATCH RequestedUsage;
1442 } CERT_CHAIN_PARA_NO_EXTRA_FIELDS, *PCERT_CHAIN_PARA_NO_EXTRA_FIELDS;
1443
1444 static void CRYPT_VerifyChainRevocation(PCERT_CHAIN_CONTEXT chain,
1445 LPFILETIME pTime, PCERT_CHAIN_PARA pChainPara, DWORD chainFlags)
1446 {
1447 DWORD cContext;
1448
1449 if (chainFlags & CERT_CHAIN_REVOCATION_CHECK_END_CERT)
1450 cContext = 1;
1451 else if ((chainFlags & CERT_CHAIN_REVOCATION_CHECK_CHAIN) ||
1452 (chainFlags & CERT_CHAIN_REVOCATION_CHECK_CHAIN_EXCLUDE_ROOT))
1453 {
1454 DWORD i;
1455
1456 for (i = 0, cContext = 0; i < chain->cChain; i++)
1457 {
1458 if (i < chain->cChain - 1 ||
1459 chainFlags & CERT_CHAIN_REVOCATION_CHECK_CHAIN)
1460 cContext += chain->rgpChain[i]->cElement;
1461 else
1462 cContext += chain->rgpChain[i]->cElement - 1;
1463 }
1464 }
1465 else
1466 cContext = 0;
1467 if (cContext)
1468 {
1469 PCCERT_CONTEXT *contexts =
1470 CryptMemAlloc(cContext * sizeof(PCCERT_CONTEXT *));
1471
1472 if (contexts)
1473 {
1474 DWORD i, j, iContext, revocationFlags;
1475 CERT_REVOCATION_PARA revocationPara = { sizeof(revocationPara), 0 };
1476 CERT_REVOCATION_STATUS revocationStatus =
1477 { sizeof(revocationStatus), 0 };
1478 BOOL ret;
1479
1480 for (i = 0, iContext = 0; iContext < cContext && i < chain->cChain;
1481 i++)
1482 {
1483 for (j = 0; iContext < cContext &&
1484 j < chain->rgpChain[i]->cElement; j++)
1485 contexts[iContext++] =
1486 chain->rgpChain[i]->rgpElement[j]->pCertContext;
1487 }
1488 revocationFlags = CERT_VERIFY_REV_CHAIN_FLAG;
1489 if (chainFlags & CERT_CHAIN_REVOCATION_CHECK_CACHE_ONLY)
1490 revocationFlags |= CERT_VERIFY_CACHE_ONLY_BASED_REVOCATION;
1491 if (chainFlags & CERT_CHAIN_REVOCATION_ACCUMULATIVE_TIMEOUT)
1492 revocationFlags |= CERT_VERIFY_REV_ACCUMULATIVE_TIMEOUT_FLAG;
1493 revocationPara.pftTimeToUse = pTime;
1494 if (pChainPara->cbSize == sizeof(CERT_CHAIN_PARA))
1495 {
1496 revocationPara.dwUrlRetrievalTimeout =
1497 pChainPara->dwUrlRetrievalTimeout;
1498 revocationPara.fCheckFreshnessTime =
1499 pChainPara->fCheckRevocationFreshnessTime;
1500 revocationPara.dwFreshnessTime =
1501 pChainPara->dwRevocationFreshnessTime;
1502 }
1503 ret = CertVerifyRevocation(X509_ASN_ENCODING,
1504 CERT_CONTEXT_REVOCATION_TYPE, cContext, (void **)contexts,
1505 revocationFlags, &revocationPara, &revocationStatus);
1506 if (!ret)
1507 {
1508 PCERT_CHAIN_ELEMENT element =
1509 CRYPT_FindIthElementInChain(chain, revocationStatus.dwIndex);
1510 DWORD error;
1511
1512 switch (revocationStatus.dwError)
1513 {
1514 case CRYPT_E_NO_REVOCATION_CHECK:
1515 case CRYPT_E_NO_REVOCATION_DLL:
1516 case CRYPT_E_NOT_IN_REVOCATION_DATABASE:
1517 error = CERT_TRUST_REVOCATION_STATUS_UNKNOWN;
1518 break;
1519 case CRYPT_E_REVOCATION_OFFLINE:
1520 error = CERT_TRUST_IS_OFFLINE_REVOCATION;
1521 break;
1522 case CRYPT_E_REVOKED:
1523 error = CERT_TRUST_IS_REVOKED;
1524 break;
1525 default:
1526 WARN("unmapped error %08x\n", revocationStatus.dwError);
1527 error = 0;
1528 }
1529 if (element)
1530 {
1531 /* FIXME: set element's pRevocationInfo member */
1532 element->TrustStatus.dwErrorStatus |= error;
1533 }
1534 chain->TrustStatus.dwErrorStatus |= error;
1535 }
1536 CryptMemFree(contexts);
1537 }
1538 }
1539 }
1540
1541 BOOL WINAPI CertGetCertificateChain(HCERTCHAINENGINE hChainEngine,
1542 PCCERT_CONTEXT pCertContext, LPFILETIME pTime, HCERTSTORE hAdditionalStore,
1543 PCERT_CHAIN_PARA pChainPara, DWORD dwFlags, LPVOID pvReserved,
1544 PCCERT_CHAIN_CONTEXT* ppChainContext)
1545 {
1546 BOOL ret;
1547 PCertificateChain chain = NULL;
1548
1549 TRACE("(%p, %p, %p, %p, %p, %08x, %p, %p)\n", hChainEngine, pCertContext,
1550 pTime, hAdditionalStore, pChainPara, dwFlags, pvReserved, ppChainContext);
1551
1552 if (ppChainContext)
1553 *ppChainContext = NULL;
1554 if (!pChainPara)
1555 {
1556 SetLastError(E_INVALIDARG);
1557 return FALSE;
1558 }
1559 if (!pCertContext->pCertInfo->SignatureAlgorithm.pszObjId)
1560 {
1561 SetLastError(ERROR_INVALID_DATA);
1562 return FALSE;
1563 }
1564 if (pChainPara->cbSize != sizeof(CERT_CHAIN_PARA_NO_EXTRA_FIELDS) &&
1565 pChainPara->cbSize != sizeof(CERT_CHAIN_PARA))
1566 {
1567 SetLastError(E_INVALIDARG);
1568 return FALSE;
1569 }
1570 if (!hChainEngine)
1571 hChainEngine = CRYPT_GetDefaultChainEngine();
1572 /* FIXME: what about HCCE_LOCAL_MACHINE? */
1573 ret = CRYPT_BuildCandidateChainFromCert(hChainEngine, pCertContext, pTime,
1574 hAdditionalStore, &chain);
1575 if (ret)
1576 {
1577 PCertificateChain alternate = NULL;
1578 PCERT_CHAIN_CONTEXT pChain;
1579
1580 do {
1581 alternate = CRYPT_BuildAlternateContextFromChain(hChainEngine,
1582 pTime, hAdditionalStore, chain);
1583
1584 /* Alternate contexts are added as "lower quality" contexts of
1585 * chain, to avoid loops in alternate chain creation.
1586 * The highest-quality chain is chosen at the end.
1587 */
1588 if (alternate)
1589 ret = CRYPT_AddAlternateChainToChain(chain, alternate);
1590 } while (ret && alternate);
1591 chain = CRYPT_ChooseHighestQualityChain(chain);
1592 if (!(dwFlags & CERT_CHAIN_RETURN_LOWER_QUALITY_CONTEXTS))
1593 CRYPT_FreeLowerQualityChains(chain);
1594 pChain = (PCERT_CHAIN_CONTEXT)chain;
1595 CRYPT_VerifyChainRevocation(pChain, pTime, pChainPara, dwFlags);
1596 if (ppChainContext)
1597 *ppChainContext = pChain;
1598 else
1599 CertFreeCertificateChain(pChain);
1600 }
1601 TRACE("returning %d\n", ret);
1602 return ret;
1603 }
1604
1605 PCCERT_CHAIN_CONTEXT WINAPI CertDuplicateCertificateChain(
1606 PCCERT_CHAIN_CONTEXT pChainContext)
1607 {
1608 PCertificateChain chain = (PCertificateChain)pChainContext;
1609
1610 TRACE("(%p)\n", pChainContext);
1611
1612 if (chain)
1613 InterlockedIncrement(&chain->ref);
1614 return pChainContext;
1615 }
1616
1617 VOID WINAPI CertFreeCertificateChain(PCCERT_CHAIN_CONTEXT pChainContext)
1618 {
1619 PCertificateChain chain = (PCertificateChain)pChainContext;
1620
1621 TRACE("(%p)\n", pChainContext);
1622
1623 if (chain)
1624 {
1625 if (InterlockedDecrement(&chain->ref) == 0)
1626 CRYPT_FreeChainContext(chain);
1627 }
1628 }
1629
1630 static void find_element_with_error(PCCERT_CHAIN_CONTEXT chain, DWORD error,
1631 LONG *iChain, LONG *iElement)
1632 {
1633 DWORD i, j;
1634
1635 for (i = 0; i < chain->cChain; i++)
1636 for (j = 0; j < chain->rgpChain[i]->cElement; j++)
1637 if (chain->rgpChain[i]->rgpElement[j]->TrustStatus.dwErrorStatus &
1638 error)
1639 {
1640 *iChain = i;
1641 *iElement = j;
1642 return;
1643 }
1644 }
1645
1646 static BOOL WINAPI verify_base_policy(LPCSTR szPolicyOID,
1647 PCCERT_CHAIN_CONTEXT pChainContext, PCERT_CHAIN_POLICY_PARA pPolicyPara,
1648 PCERT_CHAIN_POLICY_STATUS pPolicyStatus)
1649 {
1650 pPolicyStatus->lChainIndex = pPolicyStatus->lElementIndex = -1;
1651 if (pChainContext->TrustStatus.dwErrorStatus &
1652 CERT_TRUST_IS_NOT_SIGNATURE_VALID)
1653 {
1654 pPolicyStatus->dwError = TRUST_E_CERT_SIGNATURE;
1655 find_element_with_error(pChainContext,
1656 CERT_TRUST_IS_NOT_SIGNATURE_VALID, &pPolicyStatus->lChainIndex,
1657 &pPolicyStatus->lElementIndex);
1658 }
1659 else if (pChainContext->TrustStatus.dwErrorStatus &
1660 CERT_TRUST_IS_UNTRUSTED_ROOT)
1661 {
1662 pPolicyStatus->dwError = CERT_E_UNTRUSTEDROOT;
1663 find_element_with_error(pChainContext,
1664 CERT_TRUST_IS_UNTRUSTED_ROOT, &pPolicyStatus->lChainIndex,
1665 &pPolicyStatus->lElementIndex);
1666 }
1667 else if (pChainContext->TrustStatus.dwErrorStatus & CERT_TRUST_IS_CYCLIC)
1668 {
1669 pPolicyStatus->dwError = CERT_E_CHAINING;
1670 find_element_with_error(pChainContext, CERT_TRUST_IS_CYCLIC,
1671 &pPolicyStatus->lChainIndex, &pPolicyStatus->lElementIndex);
1672 /* For a cyclic chain, which element is a cycle isn't meaningful */
1673 pPolicyStatus->lElementIndex = -1;
1674 }
1675 else
1676 pPolicyStatus->dwError = NO_ERROR;
1677 return TRUE;
1678 }
1679
1680 static BYTE msTestPubKey1[] = {
1681 0x30,0x47,0x02,0x40,0x81,0x55,0x22,0xb9,0x8a,0xa4,0x6f,0xed,0xd6,0xe7,0xd9,
1682 0x66,0x0f,0x55,0xbc,0xd7,0xcd,0xd5,0xbc,0x4e,0x40,0x02,0x21,0xa2,0xb1,0xf7,
1683 0x87,0x30,0x85,0x5e,0xd2,0xf2,0x44,0xb9,0xdc,0x9b,0x75,0xb6,0xfb,0x46,0x5f,
1684 0x42,0xb6,0x9d,0x23,0x36,0x0b,0xde,0x54,0x0f,0xcd,0xbd,0x1f,0x99,0x2a,0x10,
1685 0x58,0x11,0xcb,0x40,0xcb,0xb5,0xa7,0x41,0x02,0x03,0x01,0x00,0x01 };
1686 static BYTE msTestPubKey2[] = {
1687 0x30,0x47,0x02,0x40,0x9c,0x50,0x05,0x1d,0xe2,0x0e,0x4c,0x53,0xd8,0xd9,0xb5,
1688 0xe5,0xfd,0xe9,0xe3,0xad,0x83,0x4b,0x80,0x08,0xd9,0xdc,0xe8,0xe8,0x35,0xf8,
1689 0x11,0xf1,0xe9,0x9b,0x03,0x7a,0x65,0x64,0x76,0x35,0xce,0x38,0x2c,0xf2,0xb6,
1690 0x71,0x9e,0x06,0xd9,0xbf,0xbb,0x31,0x69,0xa3,0xf6,0x30,0xa0,0x78,0x7b,0x18,
1691 0xdd,0x50,0x4d,0x79,0x1e,0xeb,0x61,0xc1,0x02,0x03,0x01,0x00,0x01 };
1692
1693 static BOOL WINAPI verify_authenticode_policy(LPCSTR szPolicyOID,
1694 PCCERT_CHAIN_CONTEXT pChainContext, PCERT_CHAIN_POLICY_PARA pPolicyPara,
1695 PCERT_CHAIN_POLICY_STATUS pPolicyStatus)
1696 {
1697 BOOL ret = verify_base_policy(szPolicyOID, pChainContext, pPolicyPara,
1698 pPolicyStatus);
1699
1700 if (ret && pPolicyStatus->dwError == CERT_E_UNTRUSTEDROOT)
1701 {
1702 CERT_PUBLIC_KEY_INFO msPubKey = { { 0 } };
1703 BOOL isMSTestRoot = FALSE;
1704 PCCERT_CONTEXT failingCert =
1705 pChainContext->rgpChain[pPolicyStatus->lChainIndex]->
1706 rgpElement[pPolicyStatus->lElementIndex]->pCertContext;
1707 DWORD i;
1708 CRYPT_DATA_BLOB keyBlobs[] = {
1709 { sizeof(msTestPubKey1), msTestPubKey1 },
1710 { sizeof(msTestPubKey2), msTestPubKey2 },
1711 };
1712
1713 /* Check whether the root is an MS test root */
1714 for (i = 0; !isMSTestRoot && i < sizeof(keyBlobs) / sizeof(keyBlobs[0]);
1715 i++)
1716 {
1717 msPubKey.PublicKey.cbData = keyBlobs[i].cbData;
1718 msPubKey.PublicKey.pbData = keyBlobs[i].pbData;
1719 if (CertComparePublicKeyInfo(
1720 X509_ASN_ENCODING | PKCS_7_ASN_ENCODING,
1721 &failingCert->pCertInfo->SubjectPublicKeyInfo, &msPubKey))
1722 isMSTestRoot = TRUE;
1723 }
1724 if (isMSTestRoot)
1725 pPolicyStatus->dwError = CERT_E_UNTRUSTEDTESTROOT;
1726 }
1727 return ret;
1728 }
1729
1730 static BOOL WINAPI verify_basic_constraints_policy(LPCSTR szPolicyOID,
1731 PCCERT_CHAIN_CONTEXT pChainContext, PCERT_CHAIN_POLICY_PARA pPolicyPara,
1732 PCERT_CHAIN_POLICY_STATUS pPolicyStatus)
1733 {
1734 pPolicyStatus->lChainIndex = pPolicyStatus->lElementIndex = -1;
1735 if (pChainContext->TrustStatus.dwErrorStatus &
1736 CERT_TRUST_INVALID_BASIC_CONSTRAINTS)
1737 {
1738 pPolicyStatus->dwError = TRUST_E_BASIC_CONSTRAINTS;
1739 find_element_with_error(pChainContext,
1740 CERT_TRUST_INVALID_BASIC_CONSTRAINTS, &pPolicyStatus->lChainIndex,
1741 &pPolicyStatus->lElementIndex);
1742 }
1743 else
1744 pPolicyStatus->dwError = NO_ERROR;
1745 return TRUE;
1746 }
1747
1748 static BYTE msPubKey1[] = {
1749 0x30,0x82,0x01,0x0a,0x02,0x82,0x01,0x01,0x00,0xdf,0x08,0xba,0xe3,0x3f,0x6e,
1750 0x64,0x9b,0xf5,0x89,0xaf,0x28,0x96,0x4a,0x07,0x8f,0x1b,0x2e,0x8b,0x3e,0x1d,
1751 0xfc,0xb8,0x80,0x69,0xa3,0xa1,0xce,0xdb,0xdf,0xb0,0x8e,0x6c,0x89,0x76,0x29,
1752 0x4f,0xca,0x60,0x35,0x39,0xad,0x72,0x32,0xe0,0x0b,0xae,0x29,0x3d,0x4c,0x16,
1753 0xd9,0x4b,0x3c,0x9d,0xda,0xc5,0xd3,0xd1,0x09,0xc9,0x2c,0x6f,0xa6,0xc2,0x60,
1754 0x53,0x45,0xdd,0x4b,0xd1,0x55,0xcd,0x03,0x1c,0xd2,0x59,0x56,0x24,0xf3,0xe5,
1755 0x78,0xd8,0x07,0xcc,0xd8,0xb3,0x1f,0x90,0x3f,0xc0,0x1a,0x71,0x50,0x1d,0x2d,
1756 0xa7,0x12,0x08,0x6d,0x7c,0xb0,0x86,0x6c,0xc7,0xba,0x85,0x32,0x07,0xe1,0x61,
1757 0x6f,0xaf,0x03,0xc5,0x6d,0xe5,0xd6,0xa1,0x8f,0x36,0xf6,0xc1,0x0b,0xd1,0x3e,
1758 0x69,0x97,0x48,0x72,0xc9,0x7f,0xa4,0xc8,0xc2,0x4a,0x4c,0x7e,0xa1,0xd1,0x94,
1759 0xa6,0xd7,0xdc,0xeb,0x05,0x46,0x2e,0xb8,0x18,0xb4,0x57,0x1d,0x86,0x49,0xdb,
1760 0x69,0x4a,0x2c,0x21,0xf5,0x5e,0x0f,0x54,0x2d,0x5a,0x43,0xa9,0x7a,0x7e,0x6a,
1761 0x8e,0x50,0x4d,0x25,0x57,0xa1,0xbf,0x1b,0x15,0x05,0x43,0x7b,0x2c,0x05,0x8d,
1762 0xbd,0x3d,0x03,0x8c,0x93,0x22,0x7d,0x63,0xea,0x0a,0x57,0x05,0x06,0x0a,0xdb,
1763 0x61,0x98,0x65,0x2d,0x47,0x49,0xa8,0xe7,0xe6,0x56,0x75,0x5c,0xb8,0x64,0x08,
1764 0x63,0xa9,0x30,0x40,0x66,0xb2,0xf9,0xb6,0xe3,0x34,0xe8,0x67,0x30,0xe1,0x43,
1765 0x0b,0x87,0xff,0xc9,0xbe,0x72,0x10,0x5e,0x23,0xf0,0x9b,0xa7,0x48,0x65,0xbf,
1766 0x09,0x88,0x7b,0xcd,0x72,0xbc,0x2e,0x79,0x9b,0x7b,0x02,0x03,0x01,0x00,0x01 };
1767 static BYTE msPubKey2[] = {
1768 0x30,0x82,0x01,0x0a,0x02,0x82,0x01,0x01,0x00,0xa9,0x02,0xbd,0xc1,0x70,0xe6,
1769 0x3b,0xf2,0x4e,0x1b,0x28,0x9f,0x97,0x78,0x5e,0x30,0xea,0xa2,0xa9,0x8d,0x25,
1770 0x5f,0xf8,0xfe,0x95,0x4c,0xa3,0xb7,0xfe,0x9d,0xa2,0x20,0x3e,0x7c,0x51,0xa2,
1771 0x9b,0xa2,0x8f,0x60,0x32,0x6b,0xd1,0x42,0x64,0x79,0xee,0xac,0x76,0xc9,0x54,
1772 0xda,0xf2,0xeb,0x9c,0x86,0x1c,0x8f,0x9f,0x84,0x66,0xb3,0xc5,0x6b,0x7a,0x62,
1773 0x23,0xd6,0x1d,0x3c,0xde,0x0f,0x01,0x92,0xe8,0x96,0xc4,0xbf,0x2d,0x66,0x9a,
1774 0x9a,0x68,0x26,0x99,0xd0,0x3a,0x2c,0xbf,0x0c,0xb5,0x58,0x26,0xc1,0x46,0xe7,
1775 0x0a,0x3e,0x38,0x96,0x2c,0xa9,0x28,0x39,0xa8,0xec,0x49,0x83,0x42,0xe3,0x84,
1776 0x0f,0xbb,0x9a,0x6c,0x55,0x61,0xac,0x82,0x7c,0xa1,0x60,0x2d,0x77,0x4c,0xe9,
1777 0x99,0xb4,0x64,0x3b,0x9a,0x50,0x1c,0x31,0x08,0x24,0x14,0x9f,0xa9,0xe7,0x91,
1778 0x2b,0x18,0xe6,0x3d,0x98,0x63,0x14,0x60,0x58,0x05,0x65,0x9f,0x1d,0x37,0x52,
1779 0x87,0xf7,0xa7,0xef,0x94,0x02,0xc6,0x1b,0xd3,0xbf,0x55,0x45,0xb3,0x89,0x80,
1780 0xbf,0x3a,0xec,0x54,0x94,0x4e,0xae,0xfd,0xa7,0x7a,0x6d,0x74,0x4e,0xaf,0x18,
1781 0xcc,0x96,0x09,0x28,0x21,0x00,0x57,0x90,0x60,0x69,0x37,0xbb,0x4b,0x12,0x07,
1782 0x3c,0x56,0xff,0x5b,0xfb,0xa4,0x66,0x0a,0x08,0xa6,0xd2,0x81,0x56,0x57,0xef,
1783 0xb6,0x3b,0x5e,0x16,0x81,0x77,0x04,0xda,0xf6,0xbe,0xae,0x80,0x95,0xfe,0xb0,
1784 0xcd,0x7f,0xd6,0xa7,0x1a,0x72,0x5c,0x3c,0xca,0xbc,0xf0,0x08,0xa3,0x22,0x30,
1785 0xb3,0x06,0x85,0xc9,0xb3,0x20,0x77,0x13,0x85,0xdf,0x02,0x03,0x01,0x00,0x01 };
1786 static BYTE msPubKey3[] = {
1787 0x30,0x82,0x02,0x0a,0x02,0x82,0x02,0x01,0x00,0xf3,0x5d,0xfa,0x80,0x67,0xd4,
1788 0x5a,0xa7,0xa9,0x0c,0x2c,0x90,0x20,0xd0,0x35,0x08,0x3c,0x75,0x84,0xcd,0xb7,
1789 0x07,0x89,0x9c,0x89,0xda,0xde,0xce,0xc3,0x60,0xfa,0x91,0x68,0x5a,0x9e,0x94,
1790 0x71,0x29,0x18,0x76,0x7c,0xc2,0xe0,0xc8,0x25,0x76,0x94,0x0e,0x58,0xfa,0x04,
1791 0x34,0x36,0xe6,0xdf,0xaf,0xf7,0x80,0xba,0xe9,0x58,0x0b,0x2b,0x93,0xe5,0x9d,
1792 0x05,0xe3,0x77,0x22,0x91,0xf7,0x34,0x64,0x3c,0x22,0x91,0x1d,0x5e,0xe1,0x09,
1793 0x90,0xbc,0x14,0xfe,0xfc,0x75,0x58,0x19,0xe1,0x79,0xb7,0x07,0x92,0xa3,0xae,
1794 0x88,0x59,0x08,0xd8,0x9f,0x07,0xca,0x03,0x58,0xfc,0x68,0x29,0x6d,0x32,0xd7,
1795 0xd2,0xa8,0xcb,0x4b,0xfc,0xe1,0x0b,0x48,0x32,0x4f,0xe6,0xeb,0xb8,0xad,0x4f,
1796 0xe4,0x5c,0x6f,0x13,0x94,0x99,0xdb,0x95,0xd5,0x75,0xdb,0xa8,0x1a,0xb7,0x94,
1797 0x91,0xb4,0x77,0x5b,0xf5,0x48,0x0c,0x8f,0x6a,0x79,0x7d,0x14,0x70,0x04,0x7d,
1798 0x6d,0xaf,0x90,0xf5,0xda,0x70,0xd8,0x47,0xb7,0xbf,0x9b,0x2f,0x6c,0xe7,0x05,
1799 0xb7,0xe1,0x11,0x60,0xac,0x79,0x91,0x14,0x7c,0xc5,0xd6,0xa6,0xe4,0xe1,0x7e,
1800 0xd5,0xc3,0x7e,0xe5,0x92,0xd2,0x3c,0x00,0xb5,0x36,0x82,0xde,0x79,0xe1,0x6d,
1801 0xf3,0xb5,0x6e,0xf8,0x9f,0x33,0xc9,0xcb,0x52,0x7d,0x73,0x98,0x36,0xdb,0x8b,
1802 0xa1,0x6b,0xa2,0x95,0x97,0x9b,0xa3,0xde,0xc2,0x4d,0x26,0xff,0x06,0x96,0x67,
1803 0x25,0x06,0xc8,0xe7,0xac,0xe4,0xee,0x12,0x33,0x95,0x31,0x99,0xc8,0x35,0x08,
1804 0x4e,0x34,0xca,0x79,0x53,0xd5,0xb5,0xbe,0x63,0x32,0x59,0x40,0x36,0xc0,0xa5,
1805 0x4e,0x04,0x4d,0x3d,0xdb,0x5b,0x07,0x33,0xe4,0x58,0xbf,0xef,0x3f,0x53,0x64,
1806 0xd8,0x42,0x59,0x35,0x57,0xfd,0x0f,0x45,0x7c,0x24,0x04,0x4d,0x9e,0xd6,0x38,
1807 0x74,0x11,0x97,0x22,0x90,0xce,0x68,0x44,0x74,0x92,0x6f,0xd5,0x4b,0x6f,0xb0,
1808 0x86,0xe3,0xc7,0x36,0x42,0xa0,0xd0,0xfc,0xc1,0xc0,0x5a,0xf9,0xa3,0x61,0xb9,
1809 0x30,0x47,0x71,0x96,0x0a,0x16,0xb0,0x91,0xc0,0x42,0x95,0xef,0x10,0x7f,0x28,
1810 0x6a,0xe3,0x2a,0x1f,0xb1,0xe4,0xcd,0x03,0x3f,0x77,0x71,0x04,0xc7,0x20,0xfc,
1811 0x49,0x0f,0x1d,0x45,0x88,0xa4,0xd7,0xcb,0x7e,0x88,0xad,0x8e,0x2d,0xec,0x45,
1812 0xdb,0xc4,0x51,0x04,0xc9,0x2a,0xfc,0xec,0x86,0x9e,0x9a,0x11,0x97,0x5b,0xde,
1813 0xce,0x53,0x88,0xe6,0xe2,0xb7,0xfd,0xac,0x95,0xc2,0x28,0x40,0xdb,0xef,0x04,
1814 0x90,0xdf,0x81,0x33,0x39,0xd9,0xb2,0x45,0xa5,0x23,0x87,0x06,0xa5,0x55,0x89,
1815 0x31,0xbb,0x06,0x2d,0x60,0x0e,0x41,0x18,0x7d,0x1f,0x2e,0xb5,0x97,0xcb,0x11,
1816 0xeb,0x15,0xd5,0x24,0xa5,0x94,0xef,0x15,0x14,0x89,0xfd,0x4b,0x73,0xfa,0x32,
1817 0x5b,0xfc,0xd1,0x33,0x00,0xf9,0x59,0x62,0x70,0x07,0x32,0xea,0x2e,0xab,0x40,
1818 0x2d,0x7b,0xca,0xdd,0x21,0x67,0x1b,0x30,0x99,0x8f,0x16,0xaa,0x23,0xa8,0x41,
1819 0xd1,0xb0,0x6e,0x11,0x9b,0x36,0xc4,0xde,0x40,0x74,0x9c,0xe1,0x58,0x65,0xc1,
1820 0x60,0x1e,0x7a,0x5b,0x38,0xc8,0x8f,0xbb,0x04,0x26,0x7c,0xd4,0x16,0x40,0xe5,
1821 0xb6,0x6b,0x6c,0xaa,0x86,0xfd,0x00,0xbf,0xce,0xc1,0x35,0x02,0x03,0x01,0x00,
1822 0x01 };
1823
1824 static BOOL WINAPI verify_ms_root_policy(LPCSTR szPolicyOID,
1825 PCCERT_CHAIN_CONTEXT pChainContext, PCERT_CHAIN_POLICY_PARA pPolicyPara,
1826 PCERT_CHAIN_POLICY_STATUS pPolicyStatus)
1827 {
1828 BOOL ret = verify_base_policy(szPolicyOID, pChainContext, pPolicyPara,
1829 pPolicyStatus);
1830
1831 if (ret && !pPolicyStatus->dwError)
1832 {
1833 CERT_PUBLIC_KEY_INFO msPubKey = { { 0 } };
1834 BOOL isMSRoot = FALSE;
1835 DWORD i;
1836 CRYPT_DATA_BLOB keyBlobs[] = {
1837 { sizeof(msPubKey1), msPubKey1 },
1838 { sizeof(msPubKey2), msPubKey2 },
1839 { sizeof(msPubKey3), msPubKey3 },
1840 };
1841 PCERT_SIMPLE_CHAIN rootChain =
1842 pChainContext->rgpChain[pChainContext->cChain -1 ];
1843 PCCERT_CONTEXT root =
1844 rootChain->rgpElement[rootChain->cElement - 1]->pCertContext;
1845
1846 for (i = 0; !isMSRoot && i < sizeof(keyBlobs) / sizeof(keyBlobs[0]);
1847 i++)
1848 {
1849 msPubKey.PublicKey.cbData = keyBlobs[i].cbData;
1850 msPubKey.PublicKey.pbData = keyBlobs[i].pbData;
1851 if (CertComparePublicKeyInfo(
1852 X509_ASN_ENCODING | PKCS_7_ASN_ENCODING,
1853 &root->pCertInfo->SubjectPublicKeyInfo, &msPubKey))
1854 isMSRoot = TRUE;
1855 }
1856 if (isMSRoot)
1857 pPolicyStatus->lChainIndex = pPolicyStatus->lElementIndex = 0;
1858 }
1859 return ret;
1860 }
1861
1862 typedef BOOL (WINAPI *CertVerifyCertificateChainPolicyFunc)(LPCSTR szPolicyOID,
1863 PCCERT_CHAIN_CONTEXT pChainContext, PCERT_CHAIN_POLICY_PARA pPolicyPara,
1864 PCERT_CHAIN_POLICY_STATUS pPolicyStatus);
1865
1866 BOOL WINAPI CertVerifyCertificateChainPolicy(LPCSTR szPolicyOID,
1867 PCCERT_CHAIN_CONTEXT pChainContext, PCERT_CHAIN_POLICY_PARA pPolicyPara,
1868 PCERT_CHAIN_POLICY_STATUS pPolicyStatus)
1869 {
1870 static HCRYPTOIDFUNCSET set = NULL;
1871 BOOL ret = FALSE;
1872 CertVerifyCertificateChainPolicyFunc verifyPolicy = NULL;
1873 HCRYPTOIDFUNCADDR hFunc = NULL;
1874
1875 TRACE("(%s, %p, %p, %p)\n", debugstr_a(szPolicyOID), pChainContext,
1876 pPolicyPara, pPolicyStatus);
1877
1878 if (!HIWORD(szPolicyOID))
1879 {
1880 switch (LOWORD(szPolicyOID))
1881 {
1882 case LOWORD(CERT_CHAIN_POLICY_BASE):
1883 verifyPolicy = verify_base_policy;
1884 break;
1885 case LOWORD(CERT_CHAIN_POLICY_AUTHENTICODE):
1886 verifyPolicy = verify_authenticode_policy;
1887 break;
1888 case LOWORD(CERT_CHAIN_POLICY_BASIC_CONSTRAINTS):
1889 verifyPolicy = verify_basic_constraints_policy;
1890 break;
1891 case LOWORD(CERT_CHAIN_POLICY_MICROSOFT_ROOT):
1892 verifyPolicy = verify_ms_root_policy;
1893 break;
1894 default:
1895 FIXME("unimplemented for %d\n", LOWORD(szPolicyOID));
1896 }
1897 }
1898 if (!verifyPolicy)
1899 {
1900 if (!set)
1901 set = CryptInitOIDFunctionSet(
1902 CRYPT_OID_VERIFY_CERTIFICATE_CHAIN_POLICY_FUNC, 0);
1903 CryptGetOIDFunctionAddress(set, X509_ASN_ENCODING, szPolicyOID, 0,
1904 (void **)&verifyPolicy, &hFunc);
1905 }
1906 if (verifyPolicy)
1907 ret = verifyPolicy(szPolicyOID, pChainContext, pPolicyPara,
1908 pPolicyStatus);
1909 if (hFunc)
1910 CryptFreeOIDFunctionAddress(hFunc, 0);
1911 return ret;
1912 }