VirtualBox

source: vbox/trunk/src/VBox/Runtime/common/crypto/x509-certpaths.cpp@ 62538

Last change on this file since 62538 was 62477, checked in by vboxsync, 9 years ago

(C) 2016

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 105.4 KB
Line 
1/* $Id: x509-certpaths.cpp 62477 2016-07-22 18:27:37Z vboxsync $ */
2/** @file
3 * IPRT - Crypto - X.509, Simple Certificate Path Builder & Validator.
4 */
5
6/*
7 * Copyright (C) 2006-2016 Oracle Corporation
8 *
9 * This file is part of VirtualBox Open Source Edition (OSE), as
10 * available from http://www.virtualbox.org. This file is free software;
11 * you can redistribute it and/or modify it under the terms of the GNU
12 * General Public License (GPL) as published by the Free Software
13 * Foundation, in version 2 as it comes in the "COPYING" file of the
14 * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
15 * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
16 *
17 * The contents of this file may alternatively be used under the terms
18 * of the Common Development and Distribution License Version 1.0
19 * (CDDL) only, as it comes in the "COPYING.CDDL" file of the
20 * VirtualBox OSE distribution, in which case the provisions of the
21 * CDDL are applicable instead of those of the GPL.
22 *
23 * You may elect to license modified versions of this file under the
24 * terms and conditions of either the GPL or the CDDL or both.
25 */
26
27
28/*********************************************************************************************************************************
29* Header Files *
30*********************************************************************************************************************************/
31#define LOG_GROUP RTLOGGROUP_CRYPTO
32#include "internal/iprt.h"
33#include <iprt/crypto/x509.h>
34
35#include <iprt/asm.h>
36#include <iprt/ctype.h>
37#include <iprt/err.h>
38#include <iprt/mem.h>
39#include <iprt/string.h>
40#include <iprt/list.h>
41#include <iprt/log.h>
42#include <iprt/time.h>
43#include <iprt/crypto/pkcs7.h> /* PCRTCRPKCS7SETOFCERTS */
44#include <iprt/crypto/store.h>
45
46#include "x509-internal.h"
47
48
49/*********************************************************************************************************************************
50* Structures and Typedefs *
51*********************************************************************************************************************************/
52/**
53 * X.509 certificate path node.
54 */
55typedef struct RTCRX509CERTPATHNODE
56{
57 /** Sibling list entry. */
58 RTLISTNODE SiblingEntry;
59 /** List of children or leaf list entry. */
60 RTLISTANCHOR ChildListOrLeafEntry;
61 /** Pointer to the parent node. NULL for root. */
62 struct RTCRX509CERTPATHNODE *pParent;
63
64 /** The distance between this node and the target. */
65 uint32_t uDepth : 8;
66 /** Indicates the source of this certificate. */
67 uint32_t uSrc : 3;
68 /** Set if this is a leaf node. */
69 uint32_t fLeaf : 1;
70 /** Makes sure it's a 32-bit bitfield. */
71 uint32_t uReserved : 20;
72
73 /** Leaf only: The result of the last path vertification. */
74 int rcVerify;
75
76 /** Pointer to the certificate. This can be NULL only for trust anchors. */
77 PCRTCRX509CERTIFICATE pCert;
78
79 /** If the certificate or trust anchor was obtained from a store, this is the
80 * associated certificate context (referenced of course). This is used to
81 * access the trust anchor information, if present.
82 *
83 * (If this is NULL it's from a certificate array or some such given directly to
84 * the path building code. It's assumed the caller doesn't free these until the
85 * path validation/whatever is done with and the paths destroyed.) */
86 PCRTCRCERTCTX pCertCtx;
87} RTCRX509CERTPATHNODE;
88/** Pointer to a X.509 path node. */
89typedef RTCRX509CERTPATHNODE *PRTCRX509CERTPATHNODE;
90
91/** @name RTCRX509CERTPATHNODE::uSrc values.
92 * The trusted and untrusted sources ordered in priority order, where higher
93 * number means high priority in case of duplicates.
94 * @{ */
95#define RTCRX509CERTPATHNODE_SRC_NONE 0
96#define RTCRX509CERTPATHNODE_SRC_TARGET 1
97#define RTCRX509CERTPATHNODE_SRC_UNTRUSTED_SET 2
98#define RTCRX509CERTPATHNODE_SRC_UNTRUSTED_ARRAY 3
99#define RTCRX509CERTPATHNODE_SRC_UNTRUSTED_STORE 4
100#define RTCRX509CERTPATHNODE_SRC_TRUSTED_STORE 5
101#define RTCRX509CERTPATHNODE_SRC_TRUSTED_CERT 6
102#define RTCRX509CERTPATHNODE_SRC_IS_TRUSTED(uSrc) ((uSrc) >= RTCRX509CERTPATHNODE_SRC_TRUSTED_STORE)
103/** @} */
104
105
106/**
107 * Policy tree node.
108 */
109typedef struct RTCRX509CERTPATHSPOLICYNODE
110{
111 /** Sibling list entry. */
112 RTLISTNODE SiblingEntry;
113 /** Tree depth list entry. */
114 RTLISTNODE DepthEntry;
115 /** List of children or leaf list entry. */
116 RTLISTANCHOR ChildList;
117 /** Pointer to the parent. */
118 struct RTCRX509CERTPATHSPOLICYNODE *pParent;
119
120 /** The policy object ID. */
121 PCRTASN1OBJID pValidPolicy;
122
123 /** Optional sequence of policy qualifiers. */
124 PCRTCRX509POLICYQUALIFIERINFOS pPolicyQualifiers;
125
126 /** The first policy ID in the exepcted policy set. */
127 PCRTASN1OBJID pExpectedPolicyFirst;
128 /** Set if we've already mapped pExpectedPolicyFirst. */
129 bool fAlreadyMapped;
130 /** Number of additional items in the expected policy set. */
131 uint32_t cMoreExpectedPolicySet;
132 /** Additional items in the expected policy set. */
133 PCRTASN1OBJID *papMoreExpectedPolicySet;
134} RTCRX509CERTPATHSPOLICYNODE;
135/** Pointer to a policy tree node. */
136typedef RTCRX509CERTPATHSPOLICYNODE *PRTCRX509CERTPATHSPOLICYNODE;
137
138
139/**
140 * Path builder and validator instance.
141 *
142 * The path builder creates a tree of certificates by forward searching from the
143 * end-entity towards a trusted source. The leaf nodes are inserted into list
144 * ordered by the source of the leaf certificate and the path length (i.e. tree
145 * depth).
146 *
147 * The path validator works the tree from the leaf end and validates each
148 * potential path found by the builder. It is generally happy with one working
149 * path, but may be told to verify all of them.
150 */
151typedef struct RTCRX509CERTPATHSINT
152{
153 /** Magic number. */
154 uint32_t u32Magic;
155 /** Reference counter. */
156 uint32_t volatile cRefs;
157
158 /** @name Input
159 * @{ */
160 /** The target certificate (end entity) to build a trusted path for. */
161 PCRTCRX509CERTIFICATE pTarget;
162
163 /** Lone trusted certificate. */
164 PCRTCRX509CERTIFICATE pTrustedCert;
165 /** Store of trusted certificates. */
166 RTCRSTORE hTrustedStore;
167
168 /** Store of untrusted certificates. */
169 RTCRSTORE hUntrustedStore;
170 /** Array of untrusted certificates, typically from the protocol. */
171 PCRTCRX509CERTIFICATE paUntrustedCerts;
172 /** Number of entries in paUntrusted. */
173 uint32_t cUntrustedCerts;
174 /** Set of untrusted PKCS \#7 / CMS certificatess. */
175 PCRTCRPKCS7SETOFCERTS pUntrustedCertsSet;
176
177 /** UTC time we're going to validate the path at, requires
178 * RTCRX509CERTPATHSINT_F_VALID_TIME to be set. */
179 RTTIMESPEC ValidTime;
180 /** Number of policy OIDs in the user initial policy set, 0 means anyPolicy. */
181 uint32_t cInitialUserPolicySet;
182 /** The user initial policy set. As with all other user provided data, we
183 * assume it's immutable and remains valid for the usage period of the path
184 * builder & validator. */
185 PCRTASN1OBJID *papInitialUserPolicySet;
186 /** Number of certificates before the user wants an explicit policy result.
187 * Set to UINT32_MAX no explicit policy restriction required by the user. */
188 uint32_t cInitialExplicitPolicy;
189 /** Number of certificates before the user wants policy mapping to be
190 * inhibited. Set to UINT32_MAX if no initial policy mapping inhibition
191 * desired by the user. */
192 uint32_t cInitialPolicyMappingInhibit;
193 /** Number of certificates before the user wants the anyPolicy to be rejected.
194 * Set to UINT32_MAX no explicit policy restriction required by the user. */
195 uint32_t cInitialInhibitAnyPolicy;
196 /** Initial name restriction: Permitted subtrees. */
197 PCRTCRX509GENERALSUBTREES pInitialPermittedSubtrees;
198 /** Initial name restriction: Excluded subtrees. */
199 PCRTCRX509GENERALSUBTREES pInitialExcludedSubtrees;
200
201 /** Flags RTCRX509CERTPATHSINT_F_XXX. */
202 uint32_t fFlags;
203 /** @} */
204
205 /** Sticky status for remembering allocation errors and the like. */
206 int32_t rc;
207 /** Where to store extended error info (optional). */
208 PRTERRINFO pErrInfo;
209
210 /** @name Path Builder Output
211 * @{ */
212 /** Pointer to the root of the tree. This will always be non-NULL after path
213 * building and thus can be reliably used to tell if path building has taken
214 * place or not. */
215 PRTCRX509CERTPATHNODE pRoot;
216 /** List of working leaf tree nodes. */
217 RTLISTANCHOR LeafList;
218 /** The number of paths (leafs). */
219 uint32_t cPaths;
220 /** @} */
221
222 /** Path Validator State. */
223 struct
224 {
225 /** Number of nodes in the certificate path we're validating (aka 'n'). */
226 uint32_t cNodes;
227 /** The current node (0 being the trust anchor). */
228 uint32_t iNode;
229
230 /** The root node of the valid policy tree. */
231 PRTCRX509CERTPATHSPOLICYNODE pValidPolicyTree;
232 /** An array of length cNodes + 1 which tracks all nodes at the given (index)
233 * tree depth via the RTCRX509CERTPATHSPOLICYNODE::DepthEntry member. */
234 PRTLISTANCHOR paValidPolicyDepthLists;
235
236 /** Number of entries in paPermittedSubtrees (name constraints).
237 * If zero, no permitted name constrains currently in effect. */
238 uint32_t cPermittedSubtrees;
239 /** The allocated size of papExcludedSubtrees */
240 uint32_t cPermittedSubtreesAlloc;
241 /** Array of permitted subtrees we've collected so far (name constraints). */
242 PCRTCRX509GENERALSUBTREE *papPermittedSubtrees;
243 /** Set if we end up with an empty set after calculating a name constraints
244 * union. */
245 bool fNoPermittedSubtrees;
246
247 /** Number of entries in paExcludedSubtrees (name constraints).
248 * If zero, no excluded name constrains currently in effect. */
249 uint32_t cExcludedSubtrees;
250 /** Array of excluded subtrees we've collected so far (name constraints). */
251 PCRTCRX509GENERALSUBTREES *papExcludedSubtrees;
252
253 /** Number of non-self-issued certificates to be processed before a non-NULL
254 * paValidPolicyTree is required. */
255 uint32_t cExplicitPolicy;
256 /** Number of non-self-issued certificates to be processed we stop processing
257 * policy mapping extensions. */
258 uint32_t cInhibitPolicyMapping;
259 /** Number of non-self-issued certificates to be processed before a the
260 * anyPolicy is rejected. */
261 uint32_t cInhibitAnyPolicy;
262 /** Number of non-self-issued certificates we're allowed to process. */
263 uint32_t cMaxPathLength;
264
265 /** The working issuer name. */
266 PCRTCRX509NAME pWorkingIssuer;
267 /** The working public key algorithm ID. */
268 PCRTASN1OBJID pWorkingPublicKeyAlgorithm;
269 /** The working public key algorithm parameters. */
270 PCRTASN1DYNTYPE pWorkingPublicKeyParameters;
271 /** A bit string containing the public key. */
272 PCRTASN1BITSTRING pWorkingPublicKey;
273 } v;
274
275 /** An object identifier initialized to anyPolicy. */
276 RTASN1OBJID AnyPolicyObjId;
277
278 /** Temporary scratch space. */
279 char szTmp[1024];
280} RTCRX509CERTPATHSINT;
281typedef RTCRX509CERTPATHSINT *PRTCRX509CERTPATHSINT;
282
283/** Magic value for RTCRX509CERTPATHSINT::u32Magic (Bruce Schneier). */
284#define RTCRX509CERTPATHSINT_MAGIC UINT32_C(0x19630115)
285
286/** @name RTCRX509CERTPATHSINT_F_XXX - Certificate path build flags.
287 * @{ */
288#define RTCRX509CERTPATHSINT_F_VALID_TIME RT_BIT_32(0)
289#define RTCRX509CERTPATHSINT_F_ELIMINATE_UNTRUSTED_PATHS RT_BIT_32(1)
290#define RTCRX509CERTPATHSINT_F_VALID_MASK UINT32_C(0x00000003)
291/** @} */
292
293
294/*********************************************************************************************************************************
295* Internal Functions *
296*********************************************************************************************************************************/
297static void rtCrX509CertPathsDestroyTree(PRTCRX509CERTPATHSINT pThis);
298static void rtCrX509CpvCleanup(PRTCRX509CERTPATHSINT pThis);
299
300
301/** @name Path Builder and Validator Config APIs
302 * @{
303 */
304
305RTDECL(int) RTCrX509CertPathsCreate(PRTCRX509CERTPATHS phCertPaths, PCRTCRX509CERTIFICATE pTarget)
306{
307 AssertPtrReturn(phCertPaths, VERR_INVALID_POINTER);
308
309 PRTCRX509CERTPATHSINT pThis = (PRTCRX509CERTPATHSINT)RTMemAllocZ(sizeof(*pThis));
310 if (pThis)
311 {
312 int rc = RTAsn1ObjId_InitFromString(&pThis->AnyPolicyObjId, RTCRX509_ID_CE_CP_ANY_POLICY_OID, &g_RTAsn1DefaultAllocator);
313 if (RT_SUCCESS(rc))
314 {
315 pThis->u32Magic = RTCRX509CERTPATHSINT_MAGIC;
316 pThis->cRefs = 1;
317 pThis->pTarget = pTarget;
318 pThis->hTrustedStore = NIL_RTCRSTORE;
319 pThis->hUntrustedStore = NIL_RTCRSTORE;
320 pThis->cInitialExplicitPolicy = UINT32_MAX;
321 pThis->cInitialPolicyMappingInhibit = UINT32_MAX;
322 pThis->cInitialInhibitAnyPolicy = UINT32_MAX;
323 pThis->rc = VINF_SUCCESS;
324 RTListInit(&pThis->LeafList);
325 *phCertPaths = pThis;
326 return VINF_SUCCESS;
327 }
328 return rc;
329 }
330 return VERR_NO_MEMORY;
331}
332
333
334RTDECL(uint32_t) RTCrX509CertPathsRetain(RTCRX509CERTPATHS hCertPaths)
335{
336 PRTCRX509CERTPATHSINT pThis = hCertPaths;
337 AssertPtrReturn(pThis, UINT32_MAX);
338
339 uint32_t cRefs = ASMAtomicIncU32(&pThis->cRefs);
340 Assert(cRefs > 0 && cRefs < 64);
341 return cRefs;
342}
343
344
345RTDECL(uint32_t) RTCrX509CertPathsRelease(RTCRX509CERTPATHS hCertPaths)
346{
347 uint32_t cRefs;
348 if (hCertPaths != NIL_RTCRX509CERTPATHS)
349 {
350 PRTCRX509CERTPATHSINT pThis = hCertPaths;
351 AssertPtrReturn(pThis, UINT32_MAX);
352 AssertReturn(pThis->u32Magic == RTCRX509CERTPATHSINT_MAGIC, UINT32_MAX);
353
354 cRefs = ASMAtomicDecU32(&pThis->cRefs);
355 Assert(cRefs < 64);
356 if (!cRefs)
357 {
358 /*
359 * No more references, destroy the whole thing.
360 */
361 ASMAtomicWriteU32(&pThis->u32Magic, ~RTCRX509CERTPATHSINT_MAGIC);
362
363 /* config */
364 pThis->pTarget = NULL; /* Referencing user memory. */
365 pThis->pTrustedCert = NULL; /* Referencing user memory. */
366 RTCrStoreRelease(pThis->hTrustedStore);
367 pThis->hTrustedStore = NIL_RTCRSTORE;
368 RTCrStoreRelease(pThis->hUntrustedStore);
369 pThis->hUntrustedStore = NIL_RTCRSTORE;
370 pThis->paUntrustedCerts = NULL; /* Referencing user memory. */
371 pThis->pUntrustedCertsSet = NULL; /* Referencing user memory. */
372 pThis->papInitialUserPolicySet = NULL; /* Referencing user memory. */
373 pThis->pInitialPermittedSubtrees = NULL; /* Referencing user memory. */
374 pThis->pInitialExcludedSubtrees = NULL; /* Referencing user memory. */
375
376 /* builder */
377 rtCrX509CertPathsDestroyTree(pThis);
378
379 /* validator */
380 rtCrX509CpvCleanup(pThis);
381
382 /* misc */
383 RTAsn1VtDelete(&pThis->AnyPolicyObjId.Asn1Core);
384
385 /* Finally, the instance itself. */
386 RTMemFree(pThis);
387 }
388 }
389 else
390 cRefs = 0;
391 return cRefs;
392}
393
394
395
396RTDECL(int) RTCrX509CertPathsSetTrustedStore(RTCRX509CERTPATHS hCertPaths, RTCRSTORE hTrustedStore)
397{
398 PRTCRX509CERTPATHSINT pThis = hCertPaths;
399 AssertPtrReturn(pThis, VERR_INVALID_HANDLE);
400 AssertReturn(pThis->u32Magic == RTCRX509CERTPATHSINT_MAGIC, VERR_INVALID_HANDLE);
401 AssertReturn(pThis->pRoot == NULL, VERR_WRONG_ORDER);
402
403 if (pThis->hTrustedStore != NIL_RTCRSTORE)
404 {
405 RTCrStoreRelease(pThis->hTrustedStore);
406 pThis->hTrustedStore = NIL_RTCRSTORE;
407 }
408 if (hTrustedStore != NIL_RTCRSTORE)
409 {
410 AssertReturn(RTCrStoreRetain(hTrustedStore) != UINT32_MAX, VERR_INVALID_HANDLE);
411 pThis->hTrustedStore = hTrustedStore;
412 }
413 return VINF_SUCCESS;
414}
415
416
417RTDECL(int) RTCrX509CertPathsSetUntrustedStore(RTCRX509CERTPATHS hCertPaths, RTCRSTORE hUntrustedStore)
418{
419 PRTCRX509CERTPATHSINT pThis = hCertPaths;
420 AssertPtrReturn(pThis, VERR_INVALID_HANDLE);
421 AssertReturn(pThis->u32Magic == RTCRX509CERTPATHSINT_MAGIC, VERR_INVALID_HANDLE);
422 AssertReturn(pThis->pRoot == NULL, VERR_WRONG_ORDER);
423
424 if (pThis->hUntrustedStore != NIL_RTCRSTORE)
425 {
426 RTCrStoreRelease(pThis->hUntrustedStore);
427 pThis->hUntrustedStore = NIL_RTCRSTORE;
428 }
429 if (hUntrustedStore != NIL_RTCRSTORE)
430 {
431 AssertReturn(RTCrStoreRetain(hUntrustedStore) != UINT32_MAX, VERR_INVALID_HANDLE);
432 pThis->hUntrustedStore = hUntrustedStore;
433 }
434 return VINF_SUCCESS;
435}
436
437
438RTDECL(int) RTCrX509CertPathsSetUntrustedArray(RTCRX509CERTPATHS hCertPaths, PCRTCRX509CERTIFICATE paCerts, uint32_t cCerts)
439{
440 PRTCRX509CERTPATHSINT pThis = hCertPaths;
441 AssertPtrReturn(pThis, VERR_INVALID_HANDLE);
442 AssertReturn(pThis->u32Magic == RTCRX509CERTPATHSINT_MAGIC, VERR_INVALID_HANDLE);
443
444 pThis->paUntrustedCerts = paCerts;
445 pThis->cUntrustedCerts = cCerts;
446 return VINF_SUCCESS;
447}
448
449
450RTDECL(int) RTCrX509CertPathsSetUntrustedSet(RTCRX509CERTPATHS hCertPaths, PCRTCRPKCS7SETOFCERTS pSetOfCerts)
451{
452 PRTCRX509CERTPATHSINT pThis = hCertPaths;
453 AssertPtrReturn(pThis, VERR_INVALID_HANDLE);
454 AssertReturn(pThis->u32Magic == RTCRX509CERTPATHSINT_MAGIC, VERR_INVALID_HANDLE);
455
456 pThis->pUntrustedCertsSet = pSetOfCerts;
457 return VINF_SUCCESS;
458}
459
460
461RTDECL(int) RTCrX509CertPathsSetValidTime(RTCRX509CERTPATHS hCertPaths, PCRTTIME pTime)
462{
463 PRTCRX509CERTPATHSINT pThis = hCertPaths;
464 AssertPtrReturn(pThis, VERR_INVALID_HANDLE);
465 AssertReturn(pThis->u32Magic == RTCRX509CERTPATHSINT_MAGIC, VERR_INVALID_HANDLE);
466
467 /* Allow this after building paths, as it's only used during verification. */
468
469 if (pTime)
470 {
471 if (RTTimeImplode(&pThis->ValidTime, pTime))
472 return VERR_INVALID_PARAMETER;
473 pThis->fFlags |= RTCRX509CERTPATHSINT_F_VALID_TIME;
474 }
475 else
476 pThis->fFlags &= ~RTCRX509CERTPATHSINT_F_VALID_TIME;
477 return VINF_SUCCESS;
478}
479
480
481RTDECL(int) RTCrX509CertPathsSetValidTimeSpec(RTCRX509CERTPATHS hCertPaths, PCRTTIMESPEC pTimeSpec)
482{
483 PRTCRX509CERTPATHSINT pThis = hCertPaths;
484 AssertPtrReturn(pThis, VERR_INVALID_HANDLE);
485 AssertReturn(pThis->u32Magic == RTCRX509CERTPATHSINT_MAGIC, VERR_INVALID_HANDLE);
486
487 /* Allow this after building paths, as it's only used during verification. */
488
489 if (pTimeSpec)
490 {
491 pThis->ValidTime = *pTimeSpec;
492 pThis->fFlags |= RTCRX509CERTPATHSINT_F_VALID_TIME;
493 }
494 else
495 pThis->fFlags &= ~RTCRX509CERTPATHSINT_F_VALID_TIME;
496 return VINF_SUCCESS;
497}
498
499
500RTDECL(int) RTCrX509CertPathsCreateEx(PRTCRX509CERTPATHS phCertPaths, PCRTCRX509CERTIFICATE pTarget, RTCRSTORE hTrustedStore,
501 RTCRSTORE hUntrustedStore, PCRTCRX509CERTIFICATE paUntrustedCerts, uint32_t cUntrustedCerts,
502 PCRTTIMESPEC pValidTime)
503{
504 int rc = RTCrX509CertPathsCreate(phCertPaths, pTarget);
505 if (RT_SUCCESS(rc))
506 {
507 PRTCRX509CERTPATHSINT pThis = *phCertPaths;
508
509 rc = RTCrX509CertPathsSetTrustedStore(pThis, hTrustedStore);
510 if (RT_SUCCESS(rc))
511 {
512 rc = RTCrX509CertPathsSetUntrustedStore(pThis, hUntrustedStore);
513 if (RT_SUCCESS(rc))
514 {
515 rc = RTCrX509CertPathsSetUntrustedArray(pThis, paUntrustedCerts, cUntrustedCerts);
516 if (RT_SUCCESS(rc))
517 {
518 rc = RTCrX509CertPathsSetValidTimeSpec(pThis, pValidTime);
519 if (RT_SUCCESS(rc))
520 {
521 return VINF_SUCCESS;
522 }
523 }
524 RTCrStoreRelease(pThis->hUntrustedStore);
525 }
526 RTCrStoreRelease(pThis->hTrustedStore);
527 }
528 RTMemFree(pThis);
529 *phCertPaths = NIL_RTCRX509CERTPATHS;
530 }
531 return rc;
532}
533
534/** @} */
535
536
537
538/** @name Path Builder and Validator Common Utility Functions.
539 * @{
540 */
541
542/**
543 * Checks if the certificate is self-issued.
544 *
545 * @returns true / false.
546 * @param pNode The path node to check..
547 */
548static bool rtCrX509CertPathsIsSelfIssued(PRTCRX509CERTPATHNODE pNode)
549{
550 return pNode->pCert
551 && RTCrX509Name_MatchByRfc5280(&pNode->pCert->TbsCertificate.Subject, &pNode->pCert->TbsCertificate.Issuer);
552}
553
554/** @} */
555
556
557
558/** @name Path Builder Functions.
559 * @{
560 */
561
562/**
563 *
564 * @returns
565 * @param pThis .
566 */
567static PRTCRX509CERTPATHNODE rtCrX509CertPathsNewNode(PRTCRX509CERTPATHSINT pThis)
568{
569 PRTCRX509CERTPATHNODE pNode = (PRTCRX509CERTPATHNODE)RTMemAllocZ(sizeof(*pNode));
570 if (RT_LIKELY(pNode))
571 {
572 RTListInit(&pNode->SiblingEntry);
573 RTListInit(&pNode->ChildListOrLeafEntry);
574 pNode->rcVerify = VERR_CR_X509_NOT_VERIFIED;
575
576 return pNode;
577 }
578
579 pThis->rc = RTErrInfoSet(pThis->pErrInfo, VERR_NO_MEMORY, "No memory for path node");
580 return NULL;
581}
582
583
584static void rtCrX509CertPathsDestroyNode(PRTCRX509CERTPATHNODE pNode)
585{
586 if (pNode->pCertCtx)
587 {
588 RTCrCertCtxRelease(pNode->pCertCtx);
589 pNode->pCertCtx = NULL;
590 }
591 RT_ZERO(*pNode);
592 RTMemFree(pNode);
593}
594
595
596static void rtCrX509CertPathsAddIssuer(PRTCRX509CERTPATHSINT pThis, PRTCRX509CERTPATHNODE pParent,
597 PCRTCRX509CERTIFICATE pCert, PCRTCRCERTCTX pCertCtx, uint8_t uSrc)
598{
599 /*
600 * Check if we've seen this certificate already in the current path or
601 * among the already gathered issuers.
602 */
603 if (pCert)
604 {
605 /* No duplicate certificates in the path. */
606 PRTCRX509CERTPATHNODE pTmpNode = pParent;
607 while (pTmpNode)
608 {
609 Assert(pTmpNode->pCert);
610 if ( pTmpNode->pCert == pCert
611 || RTCrX509Certificate_Compare(pTmpNode->pCert, pCert) == 0)
612 return;
613 pTmpNode = pTmpNode->pParent;
614 }
615
616 /* No duplicate tree branches. */
617 RTListForEach(&pParent->ChildListOrLeafEntry, pTmpNode, RTCRX509CERTPATHNODE, SiblingEntry)
618 {
619 if (RTCrX509Certificate_Compare(pTmpNode->pCert, pCert) == 0)
620 return;
621 }
622 }
623 else
624 Assert(pCertCtx);
625
626 /*
627 * Reference the context core before making the allocation.
628 */
629 if (pCertCtx)
630 AssertReturnVoidStmt(RTCrCertCtxRetain(pCertCtx) != UINT32_MAX,
631 pThis->rc = RTErrInfoSetF(pThis->pErrInfo, VERR_CR_X509_CPB_BAD_CERT_CTX,
632 "Bad pCertCtx=%p", pCertCtx));
633
634 /*
635 * We haven't see it, append it as a child.
636 */
637 PRTCRX509CERTPATHNODE pNew = rtCrX509CertPathsNewNode(pThis);
638 if (pNew)
639 {
640 pNew->pParent = pParent;
641 pNew->pCert = pCert;
642 pNew->pCertCtx = pCertCtx;
643 pNew->uSrc = uSrc;
644 pNew->uDepth = pParent->uDepth + 1;
645 RTListAppend(&pParent->ChildListOrLeafEntry, &pNew->SiblingEntry);
646 }
647 else
648 RTCrCertCtxRelease(pCertCtx);
649}
650
651
652static void rtCrX509CertPathsGetIssuersFromStore(PRTCRX509CERTPATHSINT pThis, PRTCRX509CERTPATHNODE pNode,
653 PCRTCRX509NAME pIssuer, RTCRSTORE hStore, uint8_t uSrc)
654{
655 RTCRSTORECERTSEARCH Search;
656 int rc = RTCrStoreCertFindBySubjectOrAltSubjectByRfc5280(hStore, pIssuer, &Search);
657 if (RT_SUCCESS(rc))
658 {
659 PCRTCRCERTCTX pCertCtx;
660 while ((pCertCtx = RTCrStoreCertSearchNext(hStore, &Search)) != NULL)
661 {
662 if ( pCertCtx->pCert
663 || ( RTCRX509CERTPATHNODE_SRC_IS_TRUSTED(uSrc)
664 && pCertCtx->pTaInfo) )
665 rtCrX509CertPathsAddIssuer(pThis, pNode, pCertCtx->pCert, pCertCtx, uSrc);
666 RTCrCertCtxRelease(pCertCtx);
667 }
668 RTCrStoreCertSearchDestroy(hStore, &Search);
669 }
670}
671
672
673static void rtCrX509CertPathsGetIssuers(PRTCRX509CERTPATHSINT pThis, PRTCRX509CERTPATHNODE pNode)
674{
675 Assert(RTListIsEmpty(&pNode->ChildListOrLeafEntry));
676 Assert(!pNode->fLeaf);
677 Assert(pNode->pCert);
678
679 /*
680 * Don't recurse infintely.
681 */
682 if (RT_UNLIKELY(pNode->uDepth >= 50))
683 return;
684
685 PCRTCRX509NAME const pIssuer = &pNode->pCert->TbsCertificate.Issuer;
686
687 /*
688 * Trusted certificate.
689 */
690 if ( pThis->pTrustedCert
691 && RTCrX509Certificate_MatchSubjectOrAltSubjectByRfc5280(pThis->pTrustedCert, pIssuer))
692 rtCrX509CertPathsAddIssuer(pThis, pNode, pThis->pTrustedCert, NULL, RTCRX509CERTPATHNODE_SRC_TRUSTED_CERT);
693
694 /*
695 * Trusted certificate store.
696 */
697 if (pThis->hTrustedStore != NIL_RTCRSTORE)
698 rtCrX509CertPathsGetIssuersFromStore(pThis, pNode, pIssuer, pThis->hTrustedStore,
699 RTCRX509CERTPATHNODE_SRC_TRUSTED_STORE);
700
701 /*
702 * Untrusted store.
703 */
704 if (pThis->hUntrustedStore != NIL_RTCRSTORE)
705 rtCrX509CertPathsGetIssuersFromStore(pThis, pNode, pIssuer, pThis->hTrustedStore,
706 RTCRX509CERTPATHNODE_SRC_UNTRUSTED_STORE);
707
708 /*
709 * Untrusted array.
710 */
711 if (pThis->paUntrustedCerts)
712 for (uint32_t i = 0; i < pThis->cUntrustedCerts; i++)
713 if (RTCrX509Certificate_MatchSubjectOrAltSubjectByRfc5280(&pThis->paUntrustedCerts[i], pIssuer))
714 rtCrX509CertPathsAddIssuer(pThis, pNode, &pThis->paUntrustedCerts[i], NULL,
715 RTCRX509CERTPATHNODE_SRC_UNTRUSTED_ARRAY);
716
717 /** @todo Rainy day: Should abstract the untrusted array and set so we don't get
718 * unnecessary PKCS7/CMS header dependencies. */
719
720 /*
721 * Untrusted set.
722 */
723 if (pThis->pUntrustedCertsSet)
724 {
725 uint32_t const cCerts = pThis->pUntrustedCertsSet->cItems;
726 PCRTCRPKCS7CERT paCerts = pThis->pUntrustedCertsSet->paItems;
727 for (uint32_t i = 0; i < cCerts; i++)
728 if ( paCerts[i].enmChoice == RTCRPKCS7CERTCHOICE_X509
729 && RTCrX509Certificate_MatchSubjectOrAltSubjectByRfc5280(paCerts[i].u.pX509Cert, pIssuer))
730 rtCrX509CertPathsAddIssuer(pThis, pNode, paCerts[i].u.pX509Cert, NULL, RTCRX509CERTPATHNODE_SRC_UNTRUSTED_SET);
731 }
732}
733
734
735static PRTCRX509CERTPATHNODE rtCrX509CertPathsGetNextRightUp(PRTCRX509CERTPATHSINT pThis, PRTCRX509CERTPATHNODE pNode)
736{
737 for (;;)
738 {
739 /* The root node has no siblings. */
740 PRTCRX509CERTPATHNODE pParent = pNode->pParent;
741 if (!pNode->pParent)
742 return NULL;
743
744 /* Try go to the right. */
745 PRTCRX509CERTPATHNODE pNext = RTListGetNext(&pParent->ChildListOrLeafEntry, pNode, RTCRX509CERTPATHNODE, SiblingEntry);
746 if (pNext)
747 return pNext;
748
749 /* Up. */
750 pNode = pParent;
751 }
752}
753
754
755static PRTCRX509CERTPATHNODE rtCrX509CertPathsEliminatePath(PRTCRX509CERTPATHSINT pThis, PRTCRX509CERTPATHNODE pNode)
756{
757 for (;;)
758 {
759 Assert(RTListIsEmpty(&pNode->ChildListOrLeafEntry));
760
761 /* Don't remove the root node. */
762 PRTCRX509CERTPATHNODE pParent = pNode->pParent;
763 if (!pParent)
764 return NULL;
765
766 /* Before removing and deleting the node check if there is sibling
767 right to it that we should continue processing from. */
768 PRTCRX509CERTPATHNODE pNext = RTListGetNext(&pParent->ChildListOrLeafEntry, pNode, RTCRX509CERTPATHNODE, SiblingEntry);
769 RTListNodeRemove(&pNode->SiblingEntry);
770 rtCrX509CertPathsDestroyNode(pNode);
771
772 if (pNext)
773 return pNext;
774
775 /* If the parent node cannot be removed, do a normal get-next-rigth-up
776 to find the continuation point for the tree loop. */
777 if (!RTListIsEmpty(&pParent->ChildListOrLeafEntry))
778 return rtCrX509CertPathsGetNextRightUp(pThis, pParent);
779
780 pNode = pParent;
781 }
782}
783
784
785/**
786 * Destroys the whole path tree.
787 *
788 * @param pThis The path builder and verifier instance.
789 */
790static void rtCrX509CertPathsDestroyTree(PRTCRX509CERTPATHSINT pThis)
791{
792 PRTCRX509CERTPATHNODE pNode, pNextLeaf;
793 RTListForEachSafe(&pThis->LeafList, pNode, pNextLeaf, RTCRX509CERTPATHNODE, ChildListOrLeafEntry)
794 {
795 RTListNodeRemove(&pNode->ChildListOrLeafEntry);
796 RTListInit(&pNode->ChildListOrLeafEntry);
797
798 for (;;)
799 {
800 PRTCRX509CERTPATHNODE pParent = pNode->pParent;
801
802 RTListNodeRemove(&pNode->SiblingEntry);
803 rtCrX509CertPathsDestroyNode(pNode);
804
805 if (!pParent)
806 {
807 pThis->pRoot = NULL;
808 break;
809 }
810
811 if (!RTListIsEmpty(&pParent->ChildListOrLeafEntry))
812 break;
813
814 pNode = pParent;
815 }
816 }
817 Assert(!pThis->pRoot);
818}
819
820
821/**
822 * Adds a leaf node.
823 *
824 * This should normally be a trusted certificate, but the caller can also
825 * request the incomplete paths, in which case this will be an untrusted
826 * certificate.
827 *
828 * @returns Pointer to the next node in the tree to process.
829 * @param pThis The path builder instance.
830 * @param pNode The leaf node.
831 */
832static PRTCRX509CERTPATHNODE rtCrX509CertPathsAddLeaf(PRTCRX509CERTPATHSINT pThis, PRTCRX509CERTPATHNODE pNode)
833{
834 pNode->fLeaf = true;
835
836 /*
837 * Priority insert by source and depth.
838 */
839 PRTCRX509CERTPATHNODE pCurLeaf;
840 RTListForEach(&pThis->LeafList, pCurLeaf, RTCRX509CERTPATHNODE, ChildListOrLeafEntry)
841 {
842 if ( pNode->uSrc > pCurLeaf->uSrc
843 || ( pNode->uSrc == pCurLeaf->uSrc
844 && pNode->uDepth < pCurLeaf->uDepth) )
845 {
846 RTListNodeInsertBefore(&pCurLeaf->ChildListOrLeafEntry, &pNode->ChildListOrLeafEntry);
847 pThis->cPaths++;
848 return rtCrX509CertPathsGetNextRightUp(pThis, pNode);
849 }
850 }
851
852 RTListAppend(&pThis->LeafList, &pNode->ChildListOrLeafEntry);
853 pThis->cPaths++;
854 return rtCrX509CertPathsGetNextRightUp(pThis, pNode);
855}
856
857
858
859RTDECL(int) RTCrX509CertPathsBuild(RTCRX509CERTPATHS hCertPaths, PRTERRINFO pErrInfo)
860{
861 /*
862 * Validate the input.
863 */
864 PRTCRX509CERTPATHSINT pThis = hCertPaths;
865 AssertPtrReturn(pThis, VERR_INVALID_HANDLE);
866 AssertReturn(pThis->u32Magic == RTCRX509CERTPATHSINT_MAGIC, VERR_INVALID_HANDLE);
867 AssertReturn(!(pThis->fFlags & ~RTCRX509CERTPATHSINT_F_VALID_MASK), VERR_INVALID_PARAMETER);
868 AssertReturn( (pThis->paUntrustedCerts == NULL && pThis->cUntrustedCerts == 0)
869 || (pThis->paUntrustedCerts != NULL && pThis->cUntrustedCerts > 0),
870 VERR_INVALID_PARAMETER);
871 AssertReturn(RTListIsEmpty(&pThis->LeafList), VERR_INVALID_PARAMETER);
872 AssertReturn(pThis->pRoot == NULL, VERR_INVALID_PARAMETER);
873 AssertReturn(pThis->rc == VINF_SUCCESS, pThis->rc);
874 AssertPtrReturn(pThis->pTarget, VERR_INVALID_PARAMETER);
875 Assert(RT_SUCCESS(RTCrX509Certificate_CheckSanity(pThis->pTarget, 0, NULL, NULL)));
876
877 /*
878 * Set up the target.
879 */
880 PRTCRX509CERTPATHNODE pCur;
881 pThis->pRoot = pCur = rtCrX509CertPathsNewNode(pThis);
882 if (pThis->pRoot)
883 {
884 pCur->pCert = pThis->pTarget;
885 pCur->uDepth = 0;
886 pCur->uSrc = RTCRX509CERTPATHNODE_SRC_TARGET;
887
888 pThis->pErrInfo = pErrInfo;
889
890 /*
891 * The tree construction loop.
892 * Walks down, up, and right as the tree is constructed.
893 */
894 do
895 {
896 /*
897 * Check for the two leaf cases first.
898 */
899 if (RTCRX509CERTPATHNODE_SRC_IS_TRUSTED(pCur->uSrc))
900 pCur = rtCrX509CertPathsAddLeaf(pThis, pCur);
901#if 0 /* This isn't right.*/
902 else if (rtCrX509CertPathsIsSelfIssued(pCur))
903 {
904 if (pThis->fFlags & RTCRX509CERTPATHSINT_F_ELIMINATE_UNTRUSTED_PATHS)
905 pCur = rtCrX509CertPathsEliminatePath(pThis, pCur);
906 else
907 pCur = rtCrX509CertPathsAddLeaf(pThis, pCur);
908 }
909#endif
910 /*
911 * Not a leaf, find all potential issuers and decend into these.
912 */
913 else
914 {
915 rtCrX509CertPathsGetIssuers(pThis, pCur);
916 if (RT_FAILURE(pThis->rc))
917 break;
918
919 if (!RTListIsEmpty(&pCur->ChildListOrLeafEntry))
920 pCur = RTListGetFirst(&pCur->ChildListOrLeafEntry, RTCRX509CERTPATHNODE, SiblingEntry);
921 else if (pThis->fFlags & RTCRX509CERTPATHSINT_F_ELIMINATE_UNTRUSTED_PATHS)
922 pCur = rtCrX509CertPathsEliminatePath(pThis, pCur);
923 else
924 pCur = rtCrX509CertPathsAddLeaf(pThis, pCur);
925 }
926 if (pCur)
927 Log2(("RTCrX509CertPathsBuild: pCur=%p fLeaf=%d pParent=%p pNext=%p pPrev=%p\n",
928 pCur, pCur->fLeaf, pCur->pParent,
929 pCur->pParent ? RTListGetNext(&pCur->pParent->ChildListOrLeafEntry, pCur, RTCRX509CERTPATHNODE, SiblingEntry) : NULL,
930 pCur->pParent ? RTListGetPrev(&pCur->pParent->ChildListOrLeafEntry, pCur, RTCRX509CERTPATHNODE, SiblingEntry) : NULL));
931 } while (pCur);
932
933 pThis->pErrInfo = NULL;
934 if (RT_SUCCESS(pThis->rc))
935 return VINF_SUCCESS;
936 }
937 else
938 Assert(RT_FAILURE_NP(pThis->rc));
939 return pThis->rc;
940}
941
942
943/**
944 * Looks up path by leaf/path index.
945 *
946 * @returns Pointer to the leaf node of the path.
947 * @param pThis The path builder & validator instance.
948 * @param iPath The oridnal of the path to get.
949 */
950static PRTCRX509CERTPATHNODE rtCrX509CertPathsGetLeafByIndex(PRTCRX509CERTPATHSINT pThis, uint32_t iPath)
951{
952 Assert(iPath < pThis->cPaths);
953
954 uint32_t iCurPath = 0;
955 PRTCRX509CERTPATHNODE pCurLeaf;
956 RTListForEach(&pThis->LeafList, pCurLeaf, RTCRX509CERTPATHNODE, ChildListOrLeafEntry)
957 {
958 if (iCurPath == iPath)
959 return pCurLeaf;
960 iCurPath++;
961 }
962
963 AssertFailedReturn(NULL);
964}
965
966
967static void rtDumpPrintf(PFNRTDUMPPRINTFV pfnPrintfV, void *pvUser, const char *pszFormat, ...)
968{
969 va_list va;
970 va_start(va, pszFormat);
971 pfnPrintfV(pvUser, pszFormat, va);
972 va_end(va);
973}
974
975
976static void rtDumpIndent(PFNRTDUMPPRINTFV pfnPrintfV, void *pvUser, uint32_t cchSpaces, const char *pszFormat, ...)
977{
978 static const char s_szSpaces[] = " ";
979 while (cchSpaces > 0)
980 {
981 uint32_t cchBurst = RT_MIN(sizeof(s_szSpaces) - 1, cchSpaces);
982 rtDumpPrintf(pfnPrintfV, pvUser, &s_szSpaces[sizeof(s_szSpaces) - cchBurst - 1]);
983 cchSpaces -= cchBurst;
984 }
985
986 va_list va;
987 va_start(va, pszFormat);
988 pfnPrintfV(pvUser, pszFormat, va);
989 va_end(va);
990}
991
992/** @name X.500 attribute types
993 * See RFC-4519 among others.
994 * @{ */
995#define RTCRX500_ID_AT_OBJECT_CLASS_OID "2.5.4.0"
996#define RTCRX500_ID_AT_ALIASED_ENTRY_NAME_OID "2.5.4.1"
997#define RTCRX500_ID_AT_KNOWLDGEINFORMATION_OID "2.5.4.2"
998#define RTCRX500_ID_AT_COMMON_NAME_OID "2.5.4.3"
999#define RTCRX500_ID_AT_SURNAME_OID "2.5.4.4"
1000#define RTCRX500_ID_AT_SERIAL_NUMBER_OID "2.5.4.5"
1001#define RTCRX500_ID_AT_COUNTRY_NAME_OID "2.5.4.6"
1002#define RTCRX500_ID_AT_LOCALITY_NAME_OID "2.5.4.7"
1003#define RTCRX500_ID_AT_STATE_OR_PROVINCE_NAME_OID "2.5.4.8"
1004#define RTCRX500_ID_AT_STREET_ADDRESS_OID "2.5.4.9"
1005#define RTCRX500_ID_AT_ORGANIZATION_NAME_OID "2.5.4.10"
1006#define RTCRX500_ID_AT_ORGANIZATION_UNIT_NAME_OID "2.5.4.11"
1007#define RTCRX500_ID_AT_TITLE_OID "2.5.4.12"
1008#define RTCRX500_ID_AT_DESCRIPTION_OID "2.5.4.13"
1009#define RTCRX500_ID_AT_SEARCH_GUIDE_OID "2.5.4.14"
1010#define RTCRX500_ID_AT_BUSINESS_CATEGORY_OID "2.5.4.15"
1011#define RTCRX500_ID_AT_POSTAL_ADDRESS_OID "2.5.4.16"
1012#define RTCRX500_ID_AT_POSTAL_CODE_OID "2.5.4.17"
1013#define RTCRX500_ID_AT_POST_OFFICE_BOX_OID "2.5.4.18"
1014#define RTCRX500_ID_AT_PHYSICAL_DELIVERY_OFFICE_NAME_OID "2.5.4.19"
1015#define RTCRX500_ID_AT_TELEPHONE_NUMBER_OID "2.5.4.20"
1016#define RTCRX500_ID_AT_TELEX_NUMBER_OID "2.5.4.21"
1017#define RTCRX500_ID_AT_TELETEX_TERMINAL_IDENTIFIER_OID "2.5.4.22"
1018#define RTCRX500_ID_AT_FACIMILE_TELEPHONE_NUMBER_OID "2.5.4.23"
1019#define RTCRX500_ID_AT_X121_ADDRESS_OID "2.5.4.24"
1020#define RTCRX500_ID_AT_INTERNATIONAL_ISDN_NUMBER_OID "2.5.4.25"
1021#define RTCRX500_ID_AT_REGISTERED_ADDRESS_OID "2.5.4.26"
1022#define RTCRX500_ID_AT_DESTINATION_INDICATOR_OID "2.5.4.27"
1023#define RTCRX500_ID_AT_PREFERRED_DELIVERY_METHOD_OID "2.5.4.28"
1024#define RTCRX500_ID_AT_PRESENTATION_ADDRESS_OID "2.5.4.29"
1025#define RTCRX500_ID_AT_SUPPORTED_APPLICATION_CONTEXT_OID "2.5.4.30"
1026#define RTCRX500_ID_AT_MEMBER_OID "2.5.4.31"
1027#define RTCRX500_ID_AT_OWNER_OID "2.5.4.32"
1028#define RTCRX500_ID_AT_ROLE_OCCUPANT_OID "2.5.4.33"
1029#define RTCRX500_ID_AT_SEE_ALSO_OID "2.5.4.34"
1030#define RTCRX500_ID_AT_USER_PASSWORD_OID "2.5.4.35"
1031#define RTCRX500_ID_AT_USER_CERTIFICATE_OID "2.5.4.36"
1032#define RTCRX500_ID_AT_CA_CERTIFICATE_OID "2.5.4.37"
1033#define RTCRX500_ID_AT_AUTHORITY_REVOCATION_LIST_OID "2.5.4.38"
1034#define RTCRX500_ID_AT_CERTIFICATE_REVOCATION_LIST_OID "2.5.4.39"
1035#define RTCRX500_ID_AT_CROSS_CERTIFICATE_PAIR_OID "2.5.4.40"
1036#define RTCRX500_ID_AT_NAME_OID "2.5.4.41"
1037#define RTCRX500_ID_AT_GIVEN_NAME_OID "2.5.4.42"
1038#define RTCRX500_ID_AT_INITIALS_OID "2.5.4.43"
1039#define RTCRX500_ID_AT_GENERATION_QUALIFIER_OID "2.5.4.44"
1040#define RTCRX500_ID_AT_UNIQUE_IDENTIFIER_OID "2.5.4.45"
1041#define RTCRX500_ID_AT_DN_QUALIFIER_OID "2.5.4.46"
1042#define RTCRX500_ID_AT_ENHANCHED_SEARCH_GUIDE_OID "2.5.4.47"
1043#define RTCRX500_ID_AT_PROTOCOL_INFORMATION_OID "2.5.4.48"
1044#define RTCRX500_ID_AT_DISTINGUISHED_NAME_OID "2.5.4.49"
1045#define RTCRX500_ID_AT_UNIQUE_MEMBER_OID "2.5.4.50"
1046#define RTCRX500_ID_AT_HOUSE_IDENTIFIER_OID "2.5.4.51"
1047#define RTCRX500_ID_AT_SUPPORTED_ALGORITHMS_OID "2.5.4.52"
1048#define RTCRX500_ID_AT_DELTA_REVOCATION_LIST_OID "2.5.4.53"
1049#define RTCRX500_ID_AT_ATTRIBUTE_CERTIFICATE_OID "2.5.4.58"
1050#define RTCRX500_ID_AT_PSEUDONYM_OID "2.5.4.65"
1051/** @} */
1052
1053
1054static void rtCrX509NameDump(PCRTCRX509NAME pName, PFNRTDUMPPRINTFV pfnPrintfV, void *pvUser)
1055{
1056 for (uint32_t i = 0; i < pName->cItems; i++)
1057 for (uint32_t j = 0; j < pName->paItems[i].cItems; j++)
1058 {
1059 PRTCRX509ATTRIBUTETYPEANDVALUE pAttrib = &pName->paItems[i].paItems[j];
1060
1061 const char *pszType = pAttrib->Type.szObjId;
1062 if ( !strncmp(pAttrib->Type.szObjId, "2.5.4.", 6)
1063 && (pAttrib->Type.szObjId[8] == '\0' || pAttrib->Type.szObjId[9] == '\0'))
1064 {
1065 switch (RTStrToUInt8(&pAttrib->Type.szObjId[6]))
1066 {
1067 case 3: pszType = "cn"; break;
1068 case 4: pszType = "sn"; break;
1069 case 5: pszType = "serialNumber"; break;
1070 case 6: pszType = "c"; break;
1071 case 7: pszType = "l"; break;
1072 case 8: pszType = "st"; break;
1073 case 9: pszType = "street"; break;
1074 case 10: pszType = "o"; break;
1075 case 11: pszType = "ou"; break;
1076 case 13: pszType = "description"; break;
1077 case 15: pszType = "businessCategory"; break;
1078 case 16: pszType = "postalAddress"; break;
1079 case 17: pszType = "postalCode"; break;
1080 case 18: pszType = "postOfficeBox"; break;
1081 case 20: pszType = "telephoneNumber"; break;
1082 case 26: pszType = "registeredAddress"; break;
1083 case 31: pszType = "member"; break;
1084 case 41: pszType = "name"; break;
1085 case 42: pszType = "givenName"; break;
1086 case 43: pszType = "initials"; break;
1087 case 45: pszType = "x500UniqueIdentifier"; break;
1088 case 50: pszType = "uniqueMember"; break;
1089 }
1090 }
1091 rtDumpPrintf(pfnPrintfV, pvUser, "/%s=", pszType);
1092 if (pAttrib->Value.enmType == RTASN1TYPE_STRING)
1093 {
1094 if (pAttrib->Value.u.String.pszUtf8)
1095 rtDumpPrintf(pfnPrintfV, pvUser, "%s", pAttrib->Value.u.String.pszUtf8);
1096 else
1097 {
1098 const char *pch = pAttrib->Value.u.String.Asn1Core.uData.pch;
1099 uint32_t cch = pAttrib->Value.u.String.Asn1Core.cb;
1100 int rc = RTStrValidateEncodingEx(pch, cch, 0);
1101 if (RT_SUCCESS(rc) && cch)
1102 rtDumpPrintf(pfnPrintfV, pvUser, "%.*s", (size_t)cch, pch);
1103 else
1104 while (cch > 0)
1105 {
1106 if (RT_C_IS_PRINT(*pch))
1107 rtDumpPrintf(pfnPrintfV, pvUser, "%c", *pch);
1108 else
1109 rtDumpPrintf(pfnPrintfV, pvUser, "\\x%02x", *pch);
1110 cch--;
1111 pch++;
1112 }
1113 }
1114 }
1115 else
1116 rtDumpPrintf(pfnPrintfV, pvUser, "<not-string: uTag=%#x>", pAttrib->Value.u.Core.uTag);
1117 }
1118}
1119
1120
1121static const char *rtCrX509CertPathsNodeGetSourceName(PRTCRX509CERTPATHNODE pNode)
1122{
1123 switch (pNode->uSrc)
1124 {
1125 case RTCRX509CERTPATHNODE_SRC_TARGET: return "target";
1126 case RTCRX509CERTPATHNODE_SRC_UNTRUSTED_SET: return "untrusted_set";
1127 case RTCRX509CERTPATHNODE_SRC_UNTRUSTED_ARRAY: return "untrusted_array";
1128 case RTCRX509CERTPATHNODE_SRC_UNTRUSTED_STORE: return "untrusted_store";
1129 case RTCRX509CERTPATHNODE_SRC_TRUSTED_STORE: return "trusted_store";
1130 case RTCRX509CERTPATHNODE_SRC_TRUSTED_CERT: return "trusted_cert";
1131 default: return "invalid";
1132 }
1133}
1134
1135
1136static void rtCrX509CertPathsDumpOneWorker(PRTCRX509CERTPATHSINT pThis, uint32_t iPath, PRTCRX509CERTPATHNODE pCurLeaf,
1137 uint32_t uVerbosity, PFNRTDUMPPRINTFV pfnPrintfV, void *pvUser)
1138{
1139 rtDumpPrintf(pfnPrintfV, pvUser, "Path #%u: %s, %u deep, rcVerify=%Rrc\n",
1140 iPath, RTCRX509CERTPATHNODE_SRC_IS_TRUSTED(pCurLeaf->uSrc) ? "trusted" : "untrusted", pCurLeaf->uDepth,
1141 pCurLeaf->rcVerify);
1142
1143 for (uint32_t iIndent = 2; pCurLeaf; iIndent += 2, pCurLeaf = pCurLeaf->pParent)
1144 {
1145 if (pCurLeaf->pCert)
1146 {
1147 rtDumpIndent(pfnPrintfV, pvUser, iIndent, "Issuer : ");
1148 rtCrX509NameDump(&pCurLeaf->pCert->TbsCertificate.Issuer, pfnPrintfV, pvUser);
1149 rtDumpPrintf(pfnPrintfV, pvUser, "\n");
1150
1151 rtDumpIndent(pfnPrintfV, pvUser, iIndent, "Subject: ");
1152 rtCrX509NameDump(&pCurLeaf->pCert->TbsCertificate.Subject, pfnPrintfV, pvUser);
1153 rtDumpPrintf(pfnPrintfV, pvUser, "\n");
1154
1155 if (uVerbosity >= 4)
1156 RTAsn1Dump(&pCurLeaf->pCert->SeqCore.Asn1Core, 0, iIndent, pfnPrintfV, pvUser);
1157 else if (uVerbosity >= 3)
1158 RTAsn1Dump(&pCurLeaf->pCert->TbsCertificate.T3.Extensions.SeqCore.Asn1Core, 0, iIndent, pfnPrintfV, pvUser);
1159 }
1160 else
1161 {
1162 Assert(pCurLeaf->pCertCtx); Assert(pCurLeaf->pCertCtx->pTaInfo);
1163 rtDumpIndent(pfnPrintfV, pvUser, iIndent, "Subject: ");
1164 rtCrX509NameDump(&pCurLeaf->pCertCtx->pTaInfo->CertPath.TaName, pfnPrintfV, pvUser);
1165
1166 if (uVerbosity >= 4)
1167 RTAsn1Dump(&pCurLeaf->pCertCtx->pTaInfo->SeqCore.Asn1Core, 0, iIndent, pfnPrintfV, pvUser);
1168 }
1169
1170 const char *pszSrc = rtCrX509CertPathsNodeGetSourceName(pCurLeaf);
1171 rtDumpIndent(pfnPrintfV, pvUser, iIndent, "Source : %s\n", pszSrc);
1172 }
1173}
1174
1175
1176RTDECL(int) RTCrX509CertPathsDumpOne(RTCRX509CERTPATHS hCertPaths, uint32_t iPath, uint32_t uVerbosity,
1177 PFNRTDUMPPRINTFV pfnPrintfV, void *pvUser)
1178{
1179 /*
1180 * Validate the input.
1181 */
1182 PRTCRX509CERTPATHSINT pThis = hCertPaths;
1183 AssertPtrReturn(pThis, VERR_INVALID_HANDLE);
1184 AssertReturn(pThis->u32Magic == RTCRX509CERTPATHSINT_MAGIC, VERR_INVALID_HANDLE);
1185 AssertPtrReturn(pfnPrintfV, VERR_INVALID_POINTER);
1186 int rc;
1187 if (iPath < pThis->cPaths)
1188 {
1189 PRTCRX509CERTPATHNODE pLeaf = rtCrX509CertPathsGetLeafByIndex(pThis, iPath);
1190 if (pLeaf)
1191 {
1192 rtCrX509CertPathsDumpOneWorker(pThis, iPath, pLeaf, uVerbosity, pfnPrintfV, pvUser);
1193 rc = VINF_SUCCESS;
1194 }
1195 else
1196 rc = VERR_CR_X509_CERTPATHS_INTERNAL_ERROR;
1197 }
1198 else
1199 rc = VERR_NOT_FOUND;
1200 return rc;
1201}
1202
1203
1204RTDECL(int) RTCrX509CertPathsDumpAll(RTCRX509CERTPATHS hCertPaths, uint32_t uVerbosity, PFNRTDUMPPRINTFV pfnPrintfV, void *pvUser)
1205{
1206 /*
1207 * Validate the input.
1208 */
1209 PRTCRX509CERTPATHSINT pThis = hCertPaths;
1210 AssertPtrReturn(pThis, VERR_INVALID_HANDLE);
1211 AssertReturn(pThis->u32Magic == RTCRX509CERTPATHSINT_MAGIC, VERR_INVALID_HANDLE);
1212 AssertPtrReturn(pfnPrintfV, VERR_INVALID_POINTER);
1213
1214 /*
1215 * Dump all the paths.
1216 */
1217 rtDumpPrintf(pfnPrintfV, pvUser, "%u paths, rc=%Rrc\n", pThis->cPaths, pThis->rc);
1218 uint32_t iPath = 0;
1219 PRTCRX509CERTPATHNODE pCurLeaf, pNextLeaf;
1220 RTListForEachSafe(&pThis->LeafList, pCurLeaf, pNextLeaf, RTCRX509CERTPATHNODE, ChildListOrLeafEntry)
1221 {
1222 rtCrX509CertPathsDumpOneWorker(pThis, iPath, pCurLeaf, uVerbosity, pfnPrintfV, pvUser);
1223 iPath++;
1224 }
1225
1226 return VINF_SUCCESS;
1227}
1228
1229
1230/** @} */
1231
1232
1233/** @name Path Validator Functions.
1234 * @{
1235 */
1236
1237
1238static void *rtCrX509CpvAllocZ(PRTCRX509CERTPATHSINT pThis, size_t cb, const char *pszWhat)
1239{
1240 void *pv = RTMemAllocZ(cb);
1241 if (!pv)
1242 pThis->rc = RTErrInfoSetF(pThis->pErrInfo, VERR_NO_MEMORY, "Failed to allocate %zu bytes for %s", cb, pszWhat);
1243 return pv;
1244}
1245
1246
1247DECL_NO_INLINE(static, bool) rtCrX509CpvFailed(PRTCRX509CERTPATHSINT pThis, int rc, const char *pszFormat, ...)
1248{
1249 va_list va;
1250 va_start(va, pszFormat);
1251 pThis->rc = RTErrInfoSetV(pThis->pErrInfo, rc, pszFormat, va);
1252 va_end(va);
1253 return false;
1254}
1255
1256
1257/**
1258 * Adds a sequence of excluded sub-trees.
1259 *
1260 * Don't waste time optimizing the output if this is supposed to be a union.
1261 * Unless the path is very long, it's a lot more work to optimize and the result
1262 * will be the same anyway.
1263 *
1264 * @returns success indicator.
1265 * @param pThis The validator instance.
1266 * @param pSubtrees The sequence of sub-trees to add.
1267 */
1268static bool rtCrX509CpvAddExcludedSubtrees(PRTCRX509CERTPATHSINT pThis, PCRTCRX509GENERALSUBTREES pSubtrees)
1269{
1270 if (((pThis->v.cExcludedSubtrees + 1) & 0xf) == 0)
1271 {
1272 void *pvNew = RTMemRealloc(pThis->v.papExcludedSubtrees,
1273 (pThis->v.cExcludedSubtrees + 16) * sizeof(pThis->v.papExcludedSubtrees[0]));
1274 if (RT_UNLIKELY(!pvNew))
1275 return rtCrX509CpvFailed(pThis, VERR_NO_MEMORY, "Error growing subtrees pointer array to %u elements",
1276 pThis->v.cExcludedSubtrees + 16);
1277 pThis->v.papExcludedSubtrees = (PCRTCRX509GENERALSUBTREES *)pvNew;
1278 }
1279 pThis->v.papExcludedSubtrees[pThis->v.cExcludedSubtrees] = pSubtrees;
1280 pThis->v.cExcludedSubtrees++;
1281 return true;
1282}
1283
1284
1285/**
1286 * Checks if a sub-tree is according to RFC-5280.
1287 *
1288 * @returns Success indiciator.
1289 * @param pThis The validator instance.
1290 * @param pSubtree The subtree to check.
1291 */
1292static bool rtCrX509CpvCheckSubtreeValidity(PRTCRX509CERTPATHSINT pThis, PCRTCRX509GENERALSUBTREE pSubtree)
1293{
1294 if ( pSubtree->Base.enmChoice <= RTCRX509GENERALNAMECHOICE_INVALID
1295 || pSubtree->Base.enmChoice >= RTCRX509GENERALNAMECHOICE_END)
1296 return rtCrX509CpvFailed(pThis, VERR_CR_X509_CPV_UNEXP_GENERAL_SUBTREE_CHOICE,
1297 "Unexpected GeneralSubtree choice %#x", pSubtree->Base.enmChoice);
1298
1299 if (RTAsn1Integer_UnsignedCompareWithU32(&pSubtree->Minimum, 0) != 0)
1300 return rtCrX509CpvFailed(pThis, VERR_CR_X509_CPV_UNEXP_GENERAL_SUBTREE_MIN,
1301 "Unexpected GeneralSubtree Minimum value: %#llx",
1302 pSubtree->Minimum.uValue);
1303
1304 if (RTAsn1Integer_IsPresent(&pSubtree->Maximum))
1305 return rtCrX509CpvFailed(pThis, VERR_CR_X509_CPV_UNEXP_GENERAL_SUBTREE_MAX,
1306 "Unexpected GeneralSubtree Maximum value: %#llx",
1307 pSubtree->Maximum.uValue);
1308
1309 return true;
1310}
1311
1312
1313/**
1314 * Grows the array of permitted sub-trees.
1315 *
1316 * @returns success indiciator.
1317 * @param pThis The validator instance.
1318 * @param cAdding The number of subtrees we should grow by
1319 * (relative to the current number of valid
1320 * entries).
1321 */
1322static bool rtCrX509CpvGrowPermittedSubtrees(PRTCRX509CERTPATHSINT pThis, uint32_t cAdding)
1323{
1324 uint32_t cNew = RT_ALIGN_32(pThis->v.cPermittedSubtrees + cAdding, 16);
1325 if (cNew > pThis->v.cPermittedSubtreesAlloc)
1326 {
1327 if (cNew >= _4K)
1328 return rtCrX509CpvFailed(pThis, VERR_NO_MEMORY, "Too many permitted subtrees: %u (cur %u)",
1329 cNew, pThis->v.cPermittedSubtrees);
1330 void *pvNew = RTMemRealloc(pThis->v.papPermittedSubtrees, cNew * sizeof(pThis->v.papPermittedSubtrees[0]));
1331 if (RT_UNLIKELY(!pvNew))
1332 return rtCrX509CpvFailed(pThis, VERR_NO_MEMORY, "Error growing subtrees pointer array from %u to %u elements",
1333 pThis->v.cPermittedSubtreesAlloc, cNew);
1334 pThis->v.papPermittedSubtrees = (PCRTCRX509GENERALSUBTREE *)pvNew;
1335 }
1336 return true;
1337}
1338
1339
1340/**
1341 * Adds a sequence of permitted sub-trees.
1342 *
1343 * We store reference to each individual sub-tree because we must support
1344 * intersection calculation.
1345 *
1346 * @returns success indiciator.
1347 * @param pThis The validator instance.
1348 * @param cSubtrees The number of sub-trees to add.
1349 * @param paSubtrees Array of sub-trees to add.
1350 */
1351static bool rtCrX509CpvAddPermittedSubtrees(PRTCRX509CERTPATHSINT pThis, uint32_t cSubtrees, PCRTCRX509GENERALSUBTREE paSubtrees)
1352{
1353 /*
1354 * If the array is empty, assume no permitted names.
1355 */
1356 if (!cSubtrees)
1357 {
1358 pThis->v.fNoPermittedSubtrees = true;
1359 return true;
1360 }
1361
1362 /*
1363 * Grow the array if necessary.
1364 */
1365 if (!rtCrX509CpvGrowPermittedSubtrees(pThis, cSubtrees))
1366 return false;
1367
1368 /*
1369 * Append each subtree to the array.
1370 */
1371 uint32_t iDst = pThis->v.cPermittedSubtrees;
1372 for (uint32_t iSrc = 0; iSrc < cSubtrees; iSrc++)
1373 {
1374 if (!rtCrX509CpvCheckSubtreeValidity(pThis, &paSubtrees[iSrc]))
1375 return false;
1376 pThis->v.papPermittedSubtrees[iDst] = &paSubtrees[iSrc];
1377 iDst++;
1378 }
1379 pThis->v.cPermittedSubtrees = iDst;
1380
1381 return true;
1382}
1383
1384
1385/**
1386 * Calculates the intersection between @a pSubtrees and the current permitted
1387 * sub-trees.
1388 *
1389 * @returns Success indicator.
1390 * @param pThis The validator instance.
1391 * @param pSubtrees The sub-tree sequence to intersect with.
1392 */
1393static bool rtCrX509CpvIntersectionPermittedSubtrees(PRTCRX509CERTPATHSINT pThis, PCRTCRX509GENERALSUBTREES pSubtrees)
1394{
1395 /*
1396 * Deal with special cases first.
1397 */
1398 if (pThis->v.fNoPermittedSubtrees)
1399 {
1400 Assert(pThis->v.cPermittedSubtrees == 0);
1401 return true;
1402 }
1403
1404 uint32_t cRight = pSubtrees->cItems;
1405 PCRTCRX509GENERALSUBTREE paRight = pSubtrees->paItems;
1406 if (cRight == 0)
1407 {
1408 pThis->v.cPermittedSubtrees = 0;
1409 pThis->v.fNoPermittedSubtrees = true;
1410 return true;
1411 }
1412
1413 uint32_t cLeft = pThis->v.cPermittedSubtrees;
1414 PCRTCRX509GENERALSUBTREE *papLeft = pThis->v.papPermittedSubtrees;
1415 if (!cLeft) /* first name constraint, no initial constraint */
1416 return rtCrX509CpvAddPermittedSubtrees(pThis, cRight, paRight);
1417
1418 /*
1419 * Create a new array with the intersection, freeing the old (left) array
1420 * once we're done.
1421 */
1422 bool afRightTags[RTCRX509GENERALNAMECHOICE_END] = { 0, 0, 0, 0, 0, 0, 0, 0, 0 };
1423
1424 pThis->v.cPermittedSubtrees = 0;
1425 pThis->v.cPermittedSubtreesAlloc = 0;
1426 pThis->v.papPermittedSubtrees = NULL;
1427
1428 for (uint32_t iRight = 0; iRight < cRight; iRight++)
1429 {
1430 if (!rtCrX509CpvCheckSubtreeValidity(pThis, &paRight[iRight]))
1431 return false;
1432
1433 RTCRX509GENERALNAMECHOICE const enmRightChoice = paRight[iRight].Base.enmChoice;
1434 afRightTags[enmRightChoice] = true;
1435
1436 bool fHaveRight = false;
1437 for (uint32_t iLeft = 0; iLeft < cLeft; iLeft++)
1438 if (papLeft[iLeft]->Base.enmChoice == enmRightChoice)
1439 {
1440 if (RTCrX509GeneralSubtree_Compare(papLeft[iLeft], &paRight[iRight]) == 0)
1441 {
1442 if (!fHaveRight)
1443 {
1444 fHaveRight = true;
1445 rtCrX509CpvAddPermittedSubtrees(pThis, 1, papLeft[iLeft]);
1446 }
1447 }
1448 else if (RTCrX509GeneralSubtree_ConstraintMatch(papLeft[iLeft], &paRight[iRight]))
1449 {
1450 if (!fHaveRight)
1451 {
1452 fHaveRight = true;
1453 rtCrX509CpvAddPermittedSubtrees(pThis, 1, &paRight[iRight]);
1454 }
1455 }
1456 else if (RTCrX509GeneralSubtree_ConstraintMatch(&paRight[iRight], papLeft[iLeft]))
1457 rtCrX509CpvAddPermittedSubtrees(pThis, 1, papLeft[iLeft]);
1458 }
1459 }
1460
1461 /*
1462 * Add missing types not specified in the right set.
1463 */
1464 for (uint32_t iLeft = 0; iLeft < cLeft; iLeft++)
1465 if (!afRightTags[papLeft[iLeft]->Base.enmChoice])
1466 rtCrX509CpvAddPermittedSubtrees(pThis, 1, papLeft[iLeft]);
1467
1468 /*
1469 * If we ended up with an empty set, no names are permitted any more.
1470 */
1471 if (pThis->v.cPermittedSubtrees == 0)
1472 pThis->v.fNoPermittedSubtrees = true;
1473
1474 RTMemFree(papLeft);
1475 return RT_SUCCESS(pThis->rc);
1476}
1477
1478
1479/**
1480 * Check if the given X.509 name is permitted by current name constraints.
1481 *
1482 * @returns true is permitteded, false if not (caller set error info).
1483 * @param pThis The validator instance.
1484 * @param pName The name to match.
1485 */
1486static bool rtCrX509CpvIsNamePermitted(PRTCRX509CERTPATHSINT pThis, PCRTCRX509NAME pName)
1487{
1488 uint32_t i = pThis->v.cPermittedSubtrees;
1489 if (i == 0)
1490 return !pThis->v.fNoPermittedSubtrees;
1491
1492 while (i-- > 0)
1493 {
1494 PCRTCRX509GENERALSUBTREE pConstraint = pThis->v.papPermittedSubtrees[i];
1495 if ( RTCRX509GENERALNAME_IS_DIRECTORY_NAME(&pConstraint->Base)
1496 && RTCrX509Name_ConstraintMatch(&pConstraint->Base.u.pT4->DirectoryName, pName))
1497 return true;
1498 }
1499 return false;
1500}
1501
1502
1503/**
1504 * Check if the given X.509 general name is permitted by current name
1505 * constraints.
1506 *
1507 * @returns true is permitteded, false if not (caller sets error info).
1508 * @param pThis The validator instance.
1509 * @param pGeneralName The name to match.
1510 */
1511static bool rtCrX509CpvIsGeneralNamePermitted(PRTCRX509CERTPATHSINT pThis, PCRTCRX509GENERALNAME pGeneralName)
1512{
1513 uint32_t i = pThis->v.cPermittedSubtrees;
1514 if (i == 0)
1515 return !pThis->v.fNoPermittedSubtrees;
1516
1517 while (i-- > 0)
1518 if (RTCrX509GeneralName_ConstraintMatch(&pThis->v.papPermittedSubtrees[i]->Base, pGeneralName))
1519 return true;
1520 return false;
1521}
1522
1523
1524/**
1525 * Check if the given X.509 name is excluded by current name constraints.
1526 *
1527 * @returns true if excluded (caller sets error info), false if not explicitly
1528 * excluded.
1529 * @param pThis The validator instance.
1530 * @param pName The name to match.
1531 */
1532static bool rtCrX509CpvIsNameExcluded(PRTCRX509CERTPATHSINT pThis, PCRTCRX509NAME pName)
1533{
1534 uint32_t i = pThis->v.cExcludedSubtrees;
1535 while (i-- > 0)
1536 {
1537 PCRTCRX509GENERALSUBTREES pSubTrees = pThis->v.papExcludedSubtrees[i];
1538 uint32_t j = pSubTrees->cItems;
1539 while (j-- > 0)
1540 if ( RTCRX509GENERALNAME_IS_DIRECTORY_NAME(&pSubTrees->paItems[j].Base)
1541 && RTCrX509Name_ConstraintMatch(&pSubTrees->paItems[j].Base.u.pT4->DirectoryName, pName))
1542 return true;
1543 }
1544 return false;
1545}
1546
1547
1548/**
1549 * Check if the given X.509 general name is excluded by current name
1550 * constraints.
1551 *
1552 * @returns true if excluded (caller sets error info), false if not explicitly
1553 * excluded.
1554 * @param pThis The validator instance.
1555 * @param pGeneralName The name to match.
1556 */
1557static bool rtCrX509CpvIsGeneralNameExcluded(PRTCRX509CERTPATHSINT pThis, PCRTCRX509GENERALNAME pGeneralName)
1558{
1559 uint32_t i = pThis->v.cExcludedSubtrees;
1560 while (i-- > 0)
1561 {
1562 PCRTCRX509GENERALSUBTREES pSubTrees = pThis->v.papExcludedSubtrees[i];
1563 uint32_t j = pSubTrees->cItems;
1564 while (j-- > 0)
1565 if (RTCrX509GeneralName_ConstraintMatch(&pSubTrees->paItems[j].Base, pGeneralName))
1566 return true;
1567 }
1568 return false;
1569}
1570
1571
1572/**
1573 * Creates a new node and inserts it.
1574 *
1575 * @param pThis The path builder & validator instance.
1576 * @param pParent The parent node. NULL for the root node.
1577 * @param iDepth The tree depth to insert at.
1578 * @param pValidPolicy The valid policy of the new node.
1579 * @param pQualifiers The qualifiers of the new node.
1580 * @param pExpectedPolicy The (first) expected polcy of the new node.
1581 */
1582static bool rtCrX509CpvPolicyTreeInsertNew(PRTCRX509CERTPATHSINT pThis, PRTCRX509CERTPATHSPOLICYNODE pParent, uint32_t iDepth,
1583 PCRTASN1OBJID pValidPolicy, PCRTCRX509POLICYQUALIFIERINFOS pQualifiers,
1584 PCRTASN1OBJID pExpectedPolicy)
1585{
1586 Assert(iDepth <= pThis->v.cNodes);
1587
1588 PRTCRX509CERTPATHSPOLICYNODE pNode;
1589 pNode = (PRTCRX509CERTPATHSPOLICYNODE)rtCrX509CpvAllocZ(pThis, sizeof(*pNode), "policy tree node");
1590 if (pNode)
1591 {
1592 pNode->pParent = pParent;
1593 if (pParent)
1594 RTListAppend(&pParent->ChildList, &pNode->SiblingEntry);
1595 else
1596 {
1597 Assert(pThis->v.pValidPolicyTree == NULL);
1598 pThis->v.pValidPolicyTree = pNode;
1599 RTListInit(&pNode->SiblingEntry);
1600 }
1601 RTListInit(&pNode->ChildList);
1602 RTListAppend(&pThis->v.paValidPolicyDepthLists[iDepth], &pNode->DepthEntry);
1603
1604 pNode->pValidPolicy = pValidPolicy;
1605 pNode->pPolicyQualifiers = pQualifiers;
1606 pNode->pExpectedPolicyFirst = pExpectedPolicy;
1607 pNode->cMoreExpectedPolicySet = 0;
1608 pNode->papMoreExpectedPolicySet = NULL;
1609 return true;
1610 }
1611 return false;
1612}
1613
1614
1615/**
1616 * Unlinks and frees a node in the valid policy tree.
1617 *
1618 * @param pThis The path builder & validator instance.
1619 * @param pNode The node to destroy.
1620 */
1621static void rtCrX509CpvPolicyTreeDestroyNode(PRTCRX509CERTPATHSINT pThis, PRTCRX509CERTPATHSPOLICYNODE pNode)
1622{
1623 Assert(RTListIsEmpty(&pNode->ChildList));
1624 if (pNode->pParent)
1625 RTListNodeRemove(&pNode->SiblingEntry);
1626 else
1627 pThis->v.pValidPolicyTree = NULL;
1628 RTListNodeRemove(&pNode->DepthEntry);
1629 pNode->pParent = NULL;
1630
1631 if (pNode->papMoreExpectedPolicySet)
1632 {
1633 RTMemFree(pNode->papMoreExpectedPolicySet);
1634 pNode->papMoreExpectedPolicySet = NULL;
1635 }
1636 RTMemFree(pNode);
1637}
1638
1639
1640/**
1641 * Unlinks and frees a sub-tree in the valid policy tree.
1642 *
1643 * @param pThis The path builder & validator instance.
1644 * @param pNode The node that is the root of the subtree.
1645 */
1646static void rtCrX509CpvPolicyTreeDestroySubtree(PRTCRX509CERTPATHSINT pThis, PRTCRX509CERTPATHSPOLICYNODE pNode)
1647{
1648 if (!RTListIsEmpty(&pNode->ChildList))
1649 {
1650 PRTCRX509CERTPATHSPOLICYNODE pCur = pNode;
1651 do
1652 {
1653 Assert(!RTListIsEmpty(&pCur->ChildList));
1654
1655 /* Decend until we find a leaf. */
1656 do
1657 pCur = RTListGetFirst(&pCur->ChildList, RTCRX509CERTPATHSPOLICYNODE, SiblingEntry);
1658 while (!RTListIsEmpty(&pCur->ChildList));
1659
1660 /* Remove it and all leafy siblings. */
1661 PRTCRX509CERTPATHSPOLICYNODE pParent = pCur->pParent;
1662 do
1663 {
1664 Assert(pCur != pNode);
1665 rtCrX509CpvPolicyTreeDestroyNode(pThis, pCur);
1666 pCur = RTListGetFirst(&pParent->ChildList, RTCRX509CERTPATHSPOLICYNODE, SiblingEntry);
1667 if (!pCur)
1668 {
1669 pCur = pParent;
1670 pParent = pParent->pParent;
1671 }
1672 } while (RTListIsEmpty(&pCur->ChildList) && pCur != pNode);
1673 } while (pCur != pNode);
1674 }
1675
1676 rtCrX509CpvPolicyTreeDestroyNode(pThis, pNode);
1677}
1678
1679
1680
1681/**
1682 * Destroys the entire policy tree.
1683 *
1684 * @param pThis The path builder & validator instance.
1685 */
1686static void rtCrX509CpvPolicyTreeDestroy(PRTCRX509CERTPATHSINT pThis)
1687{
1688 uint32_t i = pThis->v.cNodes + 1;
1689 while (i-- > 0)
1690 {
1691 PRTCRX509CERTPATHSPOLICYNODE pCur, pNext;
1692 RTListForEachSafe(&pThis->v.paValidPolicyDepthLists[i], pCur, pNext, RTCRX509CERTPATHSPOLICYNODE, DepthEntry)
1693 {
1694 rtCrX509CpvPolicyTreeDestroyNode(pThis, pCur);
1695 }
1696 }
1697}
1698
1699
1700/**
1701 * Removes all leaf nodes at level @a iDepth and above.
1702 *
1703 * @param pThis The path builder & validator instance.
1704 * @param iDepth The depth to start pruning at.
1705 */
1706static void rtCrX509CpvPolicyTreePrune(PRTCRX509CERTPATHSINT pThis, uint32_t iDepth)
1707{
1708 do
1709 {
1710 PRTLISTANCHOR pList = &pThis->v.paValidPolicyDepthLists[iDepth];
1711 PRTCRX509CERTPATHSPOLICYNODE pCur, pNext;
1712 RTListForEachSafe(pList, pCur, pNext, RTCRX509CERTPATHSPOLICYNODE, DepthEntry)
1713 {
1714 if (RTListIsEmpty(&pCur->ChildList))
1715 rtCrX509CpvPolicyTreeDestroyNode(pThis, pCur);
1716 }
1717
1718 } while (iDepth-- > 0);
1719}
1720
1721
1722/**
1723 * Checks if @a pPolicy is the valid policy of a child of @a pNode.
1724 *
1725 * @returns true if in child node, false if not.
1726 * @param pNode The node which children to check.
1727 * @param pPolicy The valid policy to look for among the children.
1728 */
1729static bool rtCrX509CpvPolicyTreeIsChild(PRTCRX509CERTPATHSPOLICYNODE pNode, PCRTASN1OBJID pPolicy)
1730{
1731 PRTCRX509CERTPATHSPOLICYNODE pChild;
1732 RTListForEach(&pNode->ChildList, pChild, RTCRX509CERTPATHSPOLICYNODE, SiblingEntry)
1733 {
1734 if (RTAsn1ObjId_Compare(pChild->pValidPolicy, pPolicy) == 0)
1735 return true;
1736 }
1737 return true;
1738}
1739
1740
1741/**
1742 * Prunes the valid policy tree according to the specified user policy set.
1743 *
1744 * @returns Pointer to the policy object from @a papPolicies if found, NULL if
1745 * no match.
1746 * @param pObjId The object ID to locate at match in the set.
1747 * @param cPolicies The number of policies in @a papPolicies.
1748 * @param papPolicies The policy set to search.
1749 */
1750static PCRTASN1OBJID rtCrX509CpvFindObjIdInPolicySet(PCRTASN1OBJID pObjId, uint32_t cPolicies, PCRTASN1OBJID *papPolicies)
1751{
1752 uint32_t i = cPolicies;
1753 while (i-- > 0)
1754 if (RTAsn1ObjId_Compare(pObjId, papPolicies[i]) == 0)
1755 return papPolicies[i];
1756 return NULL;
1757}
1758
1759
1760/**
1761 * Prunes the valid policy tree according to the specified user policy set.
1762 *
1763 * @returns success indicator (allocates memory)
1764 * @param pThis The path builder & validator instance.
1765 * @param cPolicies The number of policies in @a papPolicies.
1766 * @param papPolicies The user initial policies.
1767 */
1768static bool rtCrX509CpvPolicyTreeIntersect(PRTCRX509CERTPATHSINT pThis, uint32_t cPolicies, PCRTASN1OBJID *papPolicies)
1769{
1770 /*
1771 * 4.1.6.g.i - NULL tree remains NULL.
1772 */
1773 if (!pThis->v.pValidPolicyTree)
1774 return true;
1775
1776 /*
1777 * 4.1.6.g.ii - If the user set includes anyPolicy, the whole tree is the
1778 * result of the intersection.
1779 */
1780 uint32_t i = cPolicies;
1781 while (i-- > 0)
1782 if (RTAsn1ObjId_CompareWithString(papPolicies[i], RTCRX509_ID_CE_CP_ANY_POLICY_OID) == 0)
1783 return true;
1784
1785 /*
1786 * 4.1.6.g.iii - Complicated.
1787 */
1788 PRTCRX509CERTPATHSPOLICYNODE pCur, pNext;
1789 PRTLISTANCHOR pList;
1790
1791 /* 1 & 2: Delete nodes which parent has valid policy == anyPolicy and which
1792 valid policy is neither anyPolicy nor a member of papszPolicies.
1793 While doing so, construct a set of unused user policies that
1794 we'll replace anyPolicy nodes with in step 3. */
1795 uint32_t cPoliciesLeft = 0;
1796 PCRTASN1OBJID *papPoliciesLeft = NULL;
1797 if (cPolicies)
1798 {
1799 papPoliciesLeft = (PCRTASN1OBJID *)rtCrX509CpvAllocZ(pThis, cPolicies * sizeof(papPoliciesLeft[0]), "papPoliciesLeft");
1800 if (!papPoliciesLeft)
1801 return false;
1802 for (i = 0; i < cPolicies; i++)
1803 papPoliciesLeft[i] = papPolicies[i];
1804 }
1805
1806 for (uint32_t iDepth = 1; iDepth <= pThis->v.cNodes; iDepth++)
1807 {
1808 pList = &pThis->v.paValidPolicyDepthLists[iDepth];
1809 RTListForEachSafe(pList, pCur, pNext, RTCRX509CERTPATHSPOLICYNODE, DepthEntry)
1810 {
1811 Assert(pCur->pParent);
1812 if ( RTAsn1ObjId_CompareWithString(pCur->pParent->pValidPolicy, RTCRX509_ID_CE_CP_ANY_POLICY_OID) == 0
1813 && RTAsn1ObjId_CompareWithString(pCur->pValidPolicy, RTCRX509_ID_CE_CP_ANY_POLICY_OID) != 0)
1814 {
1815 PCRTASN1OBJID pFound = rtCrX509CpvFindObjIdInPolicySet(pCur->pValidPolicy, cPolicies, papPolicies);
1816 if (!pFound)
1817 rtCrX509CpvPolicyTreeDestroySubtree(pThis, pCur);
1818 else
1819 for (i = 0; i < cPoliciesLeft; i++)
1820 if (papPoliciesLeft[i] == pFound)
1821 {
1822 cPoliciesLeft--;
1823 if (i < cPoliciesLeft)
1824 papPoliciesLeft[i] = papPoliciesLeft[cPoliciesLeft];
1825 papPoliciesLeft[cPoliciesLeft] = NULL;
1826 break;
1827 }
1828 }
1829 }
1830 }
1831
1832 /*
1833 * 4.1.5.g.iii.3 - Replace anyPolicy nodes on the final tree depth with
1834 * the policies in papPoliciesLeft.
1835 */
1836 pList = &pThis->v.paValidPolicyDepthLists[pThis->v.cNodes];
1837 RTListForEachSafe(pList, pCur, pNext, RTCRX509CERTPATHSPOLICYNODE, DepthEntry)
1838 {
1839 if (RTAsn1ObjId_CompareWithString(pCur->pValidPolicy, RTCRX509_ID_CE_CP_ANY_POLICY_OID) == 0)
1840 {
1841 for (i = 0; i < cPoliciesLeft; i++)
1842 rtCrX509CpvPolicyTreeInsertNew(pThis, pCur->pParent, pThis->v.cNodes - 1,
1843 papPoliciesLeft[i], pCur->pPolicyQualifiers, papPoliciesLeft[i]);
1844 rtCrX509CpvPolicyTreeDestroyNode(pThis, pCur);
1845 }
1846 }
1847
1848 RTMemFree(papPoliciesLeft);
1849
1850 /*
1851 * 4.1.5.g.iii.4 - Prune the tree
1852 */
1853 rtCrX509CpvPolicyTreePrune(pThis, pThis->v.cNodes - 1);
1854
1855 return RT_SUCCESS(pThis->rc);
1856}
1857
1858
1859
1860/**
1861 * Frees the path validator state.
1862 *
1863 * @param pThis The path builder & validator instance.
1864 */
1865static void rtCrX509CpvCleanup(PRTCRX509CERTPATHSINT pThis)
1866{
1867 /*
1868 * Destroy the policy tree and all its nodes. We do this from the bottom
1869 * up via the depth lists, saving annoying tree traversal.
1870 */
1871 if (pThis->v.paValidPolicyDepthLists)
1872 {
1873 rtCrX509CpvPolicyTreeDestroy(pThis);
1874
1875 RTMemFree(pThis->v.paValidPolicyDepthLists);
1876 pThis->v.paValidPolicyDepthLists = NULL;
1877 }
1878
1879 Assert(pThis->v.pValidPolicyTree == NULL);
1880 pThis->v.pValidPolicyTree = NULL;
1881
1882 /*
1883 * Destroy the name constraint arrays.
1884 */
1885 if (pThis->v.papPermittedSubtrees)
1886 {
1887 RTMemFree(pThis->v.papPermittedSubtrees);
1888 pThis->v.papPermittedSubtrees = NULL;
1889 }
1890 pThis->v.cPermittedSubtrees = 0;
1891 pThis->v.cPermittedSubtreesAlloc = 0;
1892 pThis->v.fNoPermittedSubtrees = false;
1893
1894 if (pThis->v.papExcludedSubtrees)
1895 {
1896 RTMemFree(pThis->v.papExcludedSubtrees);
1897 pThis->v.papExcludedSubtrees = NULL;
1898 }
1899 pThis->v.cExcludedSubtrees = 0;
1900
1901 /*
1902 * Clear other pointers.
1903 */
1904 pThis->v.pWorkingIssuer = NULL;
1905 pThis->v.pWorkingPublicKey = NULL;
1906 pThis->v.pWorkingPublicKeyAlgorithm = NULL;
1907 pThis->v.pWorkingPublicKeyParameters = NULL;
1908}
1909
1910
1911
1912/**
1913 * Initializes the state.
1914 *
1915 * Caller must check pThis->rc.
1916 *
1917 * @param pThis The path builder & validator instance.
1918 * @param pTrustAnchor The trust anchor node for the path that we're about
1919 * to validate.
1920 */
1921static void rtCrX509CpvInit(PRTCRX509CERTPATHSINT pThis, PRTCRX509CERTPATHNODE pTrustAnchor)
1922{
1923 rtCrX509CpvCleanup(pThis);
1924
1925 /*
1926 * The node count does not include the trust anchor.
1927 */
1928 pThis->v.cNodes = pTrustAnchor->uDepth;
1929
1930 /*
1931 * Valid policy tree starts with an anyPolicy node.
1932 */
1933 uint32_t i = pThis->v.cNodes + 1;
1934 pThis->v.paValidPolicyDepthLists = (PRTLISTANCHOR)rtCrX509CpvAllocZ(pThis, i * sizeof(RTLISTANCHOR),
1935 "paValidPolicyDepthLists");
1936 if (RT_UNLIKELY(!pThis->v.paValidPolicyDepthLists))
1937 return;
1938 while (i-- > 0)
1939 RTListInit(&pThis->v.paValidPolicyDepthLists[i]);
1940
1941 if (!rtCrX509CpvPolicyTreeInsertNew(pThis, NULL, 0 /* iDepth*/, &pThis->AnyPolicyObjId, NULL, &pThis->AnyPolicyObjId))
1942 return;
1943 Assert(!RTListIsEmpty(&pThis->v.paValidPolicyDepthLists[0])); Assert(pThis->v.pValidPolicyTree);
1944
1945 /*
1946 * Name constrains.
1947 */
1948 if (pThis->pInitialPermittedSubtrees)
1949 rtCrX509CpvAddPermittedSubtrees(pThis, pThis->pInitialPermittedSubtrees->cItems,
1950 pThis->pInitialPermittedSubtrees->paItems);
1951 if (pThis->pInitialExcludedSubtrees)
1952 rtCrX509CpvAddExcludedSubtrees(pThis, pThis->pInitialExcludedSubtrees);
1953
1954 /*
1955 * Counters.
1956 */
1957 pThis->v.cExplicitPolicy = pThis->cInitialExplicitPolicy;
1958 pThis->v.cInhibitPolicyMapping = pThis->cInitialPolicyMappingInhibit;
1959 pThis->v.cInhibitAnyPolicy = pThis->cInitialInhibitAnyPolicy;
1960 pThis->v.cMaxPathLength = pThis->v.cNodes;
1961
1962 /*
1963 * Certificate info from the trust anchor.
1964 */
1965 if (pTrustAnchor->pCert)
1966 {
1967 PCRTCRX509TBSCERTIFICATE const pTbsCert = &pTrustAnchor->pCert->TbsCertificate;
1968 pThis->v.pWorkingIssuer = &pTbsCert->Subject;
1969 pThis->v.pWorkingPublicKey = &pTbsCert->SubjectPublicKeyInfo.SubjectPublicKey;
1970 pThis->v.pWorkingPublicKeyAlgorithm = &pTbsCert->SubjectPublicKeyInfo.Algorithm.Algorithm;
1971 pThis->v.pWorkingPublicKeyParameters = &pTbsCert->SubjectPublicKeyInfo.Algorithm.Parameters;
1972 }
1973 else
1974 {
1975 Assert(pTrustAnchor->pCertCtx); Assert(pTrustAnchor->pCertCtx->pTaInfo);
1976
1977 PCRTCRTAFTRUSTANCHORINFO const pTaInfo = pTrustAnchor->pCertCtx->pTaInfo;
1978 pThis->v.pWorkingIssuer = &pTaInfo->CertPath.TaName;
1979 pThis->v.pWorkingPublicKey = &pTaInfo->PubKey.SubjectPublicKey;
1980 pThis->v.pWorkingPublicKeyAlgorithm = &pTaInfo->PubKey.Algorithm.Algorithm;
1981 pThis->v.pWorkingPublicKeyParameters = &pTaInfo->PubKey.Algorithm.Parameters;
1982 }
1983 if ( !RTASN1CORE_IS_PRESENT(&pThis->v.pWorkingPublicKeyParameters->u.Core)
1984 || pThis->v.pWorkingPublicKeyParameters->enmType == RTASN1TYPE_NULL)
1985 pThis->v.pWorkingPublicKeyParameters = NULL;
1986}
1987
1988
1989/**
1990 * Step 6.1.3.a.
1991 */
1992static bool rtCrX509CpvCheckBasicCertInfo(PRTCRX509CERTPATHSINT pThis, PRTCRX509CERTPATHNODE pNode)
1993{
1994 /*
1995 * 6.1.3.a.1 - Verify the certificate signature.
1996 */
1997 int rc = RTCrX509Certificate_VerifySignature(pNode->pCert, pThis->v.pWorkingPublicKeyAlgorithm,
1998 pThis->v.pWorkingPublicKeyParameters, pThis->v.pWorkingPublicKey,
1999 pThis->pErrInfo);
2000 if (RT_FAILURE(rc))
2001 {
2002 pThis->rc = rc;
2003 return false;
2004 }
2005
2006 /*
2007 * 6.1.3.a.2 - Verify that the certificate is valid at the specified time.
2008 */
2009 AssertCompile(sizeof(pThis->szTmp) >= 36 * 3);
2010 if ( (pThis->fFlags & RTCRX509CERTPATHSINT_F_VALID_TIME)
2011 && !RTCrX509Validity_IsValidAtTimeSpec(&pNode->pCert->TbsCertificate.Validity, &pThis->ValidTime))
2012 return rtCrX509CpvFailed(pThis, VERR_CR_X509_CPV_NOT_VALID_AT_TIME,
2013 "Certificate is not valid (ValidTime=%s Validity=[%s...%s])",
2014 RTTimeSpecToString(&pThis->ValidTime, &pThis->szTmp[0], 36),
2015 RTTimeToString(&pNode->pCert->TbsCertificate.Validity.NotBefore.Time, &pThis->szTmp[36], 36),
2016 RTTimeToString(&pNode->pCert->TbsCertificate.Validity.NotAfter.Time, &pThis->szTmp[2*36], 36) );
2017
2018 /*
2019 * 6.1.3.a.3 - Verified that the certficiate is not revoked.
2020 */
2021 /** @todo rainy day. */
2022
2023 /*
2024 * 6.1.3.a.4 - Check the issuer name.
2025 */
2026 if (!RTCrX509Name_MatchByRfc5280(&pNode->pCert->TbsCertificate.Issuer, pThis->v.pWorkingIssuer))
2027 return rtCrX509CpvFailed(pThis, VERR_CR_X509_CPV_ISSUER_MISMATCH, "Issuer mismatch");
2028
2029 return true;
2030}
2031
2032
2033/**
2034 * Step 6.1.3.b-c.
2035 */
2036static bool rtCrX509CpvCheckNameConstraints(PRTCRX509CERTPATHSINT pThis, PRTCRX509CERTPATHNODE pNode)
2037{
2038 if (pThis->v.fNoPermittedSubtrees)
2039 return rtCrX509CpvFailed(pThis, VERR_CR_X509_CPV_NO_PERMITTED_NAMES, "No permitted subtrees");
2040
2041 if ( pNode->pCert->TbsCertificate.Subject.cItems > 0
2042 && ( !rtCrX509CpvIsNamePermitted(pThis, &pNode->pCert->TbsCertificate.Subject)
2043 || rtCrX509CpvIsNameExcluded(pThis, &pNode->pCert->TbsCertificate.Subject)) )
2044 return rtCrX509CpvFailed(pThis, VERR_CR_X509_CPV_NAME_NOT_PERMITTED,
2045 "Subject name is not permitted by current name constraints");
2046
2047 PCRTCRX509GENERALNAMES pAltSubjectName = pNode->pCert->TbsCertificate.T3.pAltSubjectName;
2048 if (pAltSubjectName)
2049 {
2050 uint32_t i = pAltSubjectName->cItems;
2051 while (i-- > 0)
2052 if ( !rtCrX509CpvIsGeneralNamePermitted(pThis, &pAltSubjectName->paItems[i])
2053 || rtCrX509CpvIsGeneralNameExcluded(pThis, &pAltSubjectName->paItems[i]))
2054 return rtCrX509CpvFailed(pThis, VERR_CR_X509_CPV_ALT_NAME_NOT_PERMITTED,
2055 "Alternative name #%u is is not permitted by current name constraints", i);
2056 }
2057
2058 return true;
2059}
2060
2061
2062/**
2063 * Step 6.1.3.d-f.
2064 */
2065static bool rtCrX509CpvWorkValidPolicyTree(PRTCRX509CERTPATHSINT pThis, uint32_t iDepth, PRTCRX509CERTPATHNODE pNode,
2066 bool fSelfIssued)
2067{
2068 PCRTCRX509CERTIFICATEPOLICIES pPolicies = pNode->pCert->TbsCertificate.T3.pCertificatePolicies;
2069 if (pPolicies)
2070 {
2071 /*
2072 * 6.1.3.d.1 - Work the certiciate policies into the tree.
2073 */
2074 PRTCRX509CERTPATHSPOLICYNODE pCur;
2075 PRTLISTANCHOR pListAbove = &pThis->v.paValidPolicyDepthLists[iDepth - 1];
2076 uint32_t iAnyPolicy = UINT32_MAX;
2077 uint32_t i = pPolicies->cItems;
2078 while (i-- > 0)
2079 {
2080 PCRTCRX509POLICYQUALIFIERINFOS const pQualifiers = &pPolicies->paItems[i].PolicyQualifiers;
2081 PCRTASN1OBJID const pIdP = &pPolicies->paItems[i].PolicyIdentifier;
2082 if (RTAsn1ObjId_CompareWithString(pIdP, RTCRX509_ID_CE_CP_ANY_POLICY_OID) == 0)
2083 {
2084 iAnyPolicy++;
2085 continue;
2086 }
2087
2088 /*
2089 * 6.1.3.d.1.i - Create children for matching policies.
2090 */
2091 uint32_t cMatches = 0;
2092 RTListForEach(pListAbove, pCur, RTCRX509CERTPATHSPOLICYNODE, DepthEntry)
2093 {
2094 bool fMatch = RTAsn1ObjId_Compare(pCur->pExpectedPolicyFirst, pIdP) == 0;
2095 if (!fMatch && pCur->cMoreExpectedPolicySet)
2096 for (uint32_t j = 0; !fMatch && j < pCur->cMoreExpectedPolicySet; j++)
2097 fMatch = RTAsn1ObjId_Compare(pCur->papMoreExpectedPolicySet[j], pIdP) == 0;
2098 if (fMatch)
2099 {
2100 if (!rtCrX509CpvPolicyTreeInsertNew(pThis, pCur, iDepth, pIdP, pQualifiers, pIdP))
2101 return false;
2102 cMatches++;
2103 }
2104 }
2105
2106 /*
2107 * 6.1.3.d.1.ii - If no matches above do the same for anyPolicy
2108 * nodes, only match with valid policy this time.
2109 */
2110 if (cMatches == 0)
2111 {
2112 RTListForEach(pListAbove, pCur, RTCRX509CERTPATHSPOLICYNODE, DepthEntry)
2113 {
2114 if (RTAsn1ObjId_CompareWithString(pCur->pExpectedPolicyFirst, RTCRX509_ID_CE_CP_ANY_POLICY_OID) == 0)
2115 {
2116 if (!rtCrX509CpvPolicyTreeInsertNew(pThis, pCur, iDepth, pIdP, pQualifiers, pIdP))
2117 return false;
2118 }
2119 }
2120 }
2121 }
2122
2123 /*
2124 * 6.1.3.d.2 - If anyPolicy present, make sure all expected policies
2125 * are propagated to the current depth.
2126 */
2127 if ( iAnyPolicy < pPolicies->cItems
2128 && ( pThis->v.cInhibitAnyPolicy > 0
2129 || (pNode->pParent && fSelfIssued) ) )
2130 {
2131 PCRTCRX509POLICYQUALIFIERINFOS pApQ = &pPolicies->paItems[iAnyPolicy].PolicyQualifiers;
2132 RTListForEach(pListAbove, pCur, RTCRX509CERTPATHSPOLICYNODE, DepthEntry)
2133 {
2134 if (!rtCrX509CpvPolicyTreeIsChild(pCur, pCur->pExpectedPolicyFirst))
2135 rtCrX509CpvPolicyTreeInsertNew(pThis, pCur, iDepth, pCur->pExpectedPolicyFirst, pApQ,
2136 pCur->pExpectedPolicyFirst);
2137 for (uint32_t j = 0; j < pCur->cMoreExpectedPolicySet; j++)
2138 if (!rtCrX509CpvPolicyTreeIsChild(pCur, pCur->papMoreExpectedPolicySet[j]))
2139 rtCrX509CpvPolicyTreeInsertNew(pThis, pCur, iDepth, pCur->papMoreExpectedPolicySet[j], pApQ,
2140 pCur->papMoreExpectedPolicySet[j]);
2141 }
2142 }
2143 /*
2144 * 6.1.3.d.3 - Prune the tree.
2145 */
2146 else
2147 rtCrX509CpvPolicyTreePrune(pThis, iDepth - 1);
2148 }
2149 else
2150 {
2151 /*
2152 * 6.1.3.e - No policy extension present, set tree to NULL.
2153 */
2154 rtCrX509CpvPolicyTreeDestroy(pThis);
2155 }
2156
2157 /*
2158 * 6.1.3.f - NULL tree check.
2159 */
2160 if ( pThis->v.pValidPolicyTree == NULL
2161 && pThis->v.cExplicitPolicy == 0)
2162 return rtCrX509CpvFailed(pThis, VERR_CR_X509_CPV_NO_VALID_POLICY,
2163 "An explicit policy is called for but the valid policy tree is NULL.");
2164 return RT_SUCCESS(pThis->rc);
2165}
2166
2167
2168/**
2169 * Step 6.1.4.a-b.
2170 */
2171static bool rtCrX509CpvSoakUpPolicyMappings(PRTCRX509CERTPATHSINT pThis, uint32_t iDepth,
2172 PCRTCRX509POLICYMAPPINGS pPolicyMappings)
2173{
2174 /*
2175 * 6.1.4.a - The anyPolicy is not allowed in policy mappings as it would
2176 * allow an evil intermediate certificate to expand the policy
2177 * scope of a certiciate chain without regard to upstream.
2178 */
2179 uint32_t i = pPolicyMappings->cItems;
2180 while (i-- > 0)
2181 {
2182 if (RTAsn1ObjId_CompareWithString(&pPolicyMappings->paItems[i].IssuerDomainPolicy, RTCRX509_ID_CE_CP_ANY_POLICY_OID) == 0)
2183 return rtCrX509CpvFailed(pThis, VERR_CR_X509_CPV_INVALID_POLICY_MAPPING,
2184 "Invalid policy mapping %#u: IssuerDomainPolicy is anyPolicy.", i);
2185
2186 if (RTAsn1ObjId_CompareWithString(&pPolicyMappings->paItems[i].SubjectDomainPolicy, RTCRX509_ID_CE_CP_ANY_POLICY_OID) == 0)
2187 return rtCrX509CpvFailed(pThis, VERR_CR_X509_CPV_INVALID_POLICY_MAPPING,
2188 "Invalid policy mapping %#u: SubjectDomainPolicy is anyPolicy.", i);
2189 }
2190
2191 PRTCRX509CERTPATHSPOLICYNODE pCur, pNext;
2192 if (pThis->v.cInhibitPolicyMapping > 0)
2193 {
2194 /*
2195 * 6.1.4.b.1 - Do the policy mapping.
2196 */
2197 i = pPolicyMappings->cItems;
2198 while (i-- > 0)
2199 {
2200 uint32_t cFound = 0;
2201 RTListForEach(&pThis->v.paValidPolicyDepthLists[iDepth], pCur, RTCRX509CERTPATHSPOLICYNODE, DepthEntry)
2202 {
2203 if (RTAsn1ObjId_Compare(pCur->pValidPolicy, &pPolicyMappings->paItems[i].IssuerDomainPolicy))
2204 {
2205 if (!pCur->fAlreadyMapped)
2206 {
2207 pCur->fAlreadyMapped = true;
2208 pCur->pExpectedPolicyFirst = &pPolicyMappings->paItems[i].SubjectDomainPolicy;
2209 }
2210 else
2211 {
2212 uint32_t iExpected = pCur->cMoreExpectedPolicySet;
2213 void *pvNew = RTMemRealloc(pCur->papMoreExpectedPolicySet,
2214 sizeof(pCur->papMoreExpectedPolicySet[0]) * (iExpected + 1));
2215 if (!pvNew)
2216 return rtCrX509CpvFailed(pThis, VERR_NO_MEMORY,
2217 "Error growing papMoreExpectedPolicySet array (cur %u, depth %u)",
2218 pCur->cMoreExpectedPolicySet, iDepth);
2219 pCur->papMoreExpectedPolicySet = (PCRTASN1OBJID *)pvNew;
2220 pCur->papMoreExpectedPolicySet[iExpected] = &pPolicyMappings->paItems[i].SubjectDomainPolicy;
2221 pCur->cMoreExpectedPolicySet = iExpected + 1;
2222 }
2223 cFound++;
2224 }
2225 }
2226
2227 /*
2228 * If no mapping took place, look for an anyPolicy node.
2229 */
2230 if (!cFound)
2231 {
2232 RTListForEach(&pThis->v.paValidPolicyDepthLists[iDepth], pCur, RTCRX509CERTPATHSPOLICYNODE, DepthEntry)
2233 {
2234 if (RTAsn1ObjId_CompareWithString(pCur->pValidPolicy, RTCRX509_ID_CE_CP_ANY_POLICY_OID) == 0)
2235 {
2236 if (!rtCrX509CpvPolicyTreeInsertNew(pThis, pCur->pParent, iDepth,
2237 &pPolicyMappings->paItems[i].IssuerDomainPolicy,
2238 pCur->pPolicyQualifiers,
2239 &pPolicyMappings->paItems[i].SubjectDomainPolicy))
2240 return false;
2241 break;
2242 }
2243 }
2244 }
2245 }
2246 }
2247 else
2248 {
2249 /*
2250 * 6.1.4.b.2 - Remove matching policies from the tree if mapping is
2251 * inhibited and prune the tree.
2252 */
2253 uint32_t cRemoved = 0;
2254 i = pPolicyMappings->cItems;
2255 while (i-- > 0)
2256 {
2257 RTListForEachSafe(&pThis->v.paValidPolicyDepthLists[iDepth], pCur, pNext, RTCRX509CERTPATHSPOLICYNODE, DepthEntry)
2258 {
2259 if (RTAsn1ObjId_Compare(pCur->pValidPolicy, &pPolicyMappings->paItems[i].IssuerDomainPolicy))
2260 {
2261 rtCrX509CpvPolicyTreeDestroyNode(pThis, pCur);
2262 cRemoved++;
2263 }
2264 }
2265 }
2266 if (cRemoved)
2267 rtCrX509CpvPolicyTreePrune(pThis, iDepth - 1);
2268 }
2269
2270 return true;
2271}
2272
2273
2274/**
2275 * Step 6.1.4.d-f & 6.1.5.c-e.
2276 */
2277static void rtCrX509CpvSetWorkingPublicKeyInfo(PRTCRX509CERTPATHSINT pThis, PRTCRX509CERTPATHNODE pNode)
2278{
2279 PCRTCRX509TBSCERTIFICATE const pTbsCert = &pNode->pCert->TbsCertificate;
2280
2281 /*
2282 * 6.1.4.d - The public key.
2283 */
2284 pThis->v.pWorkingPublicKey = &pTbsCert->SubjectPublicKeyInfo.SubjectPublicKey;
2285
2286 /*
2287 * 6.1.4.e - The public key parameters. Use new ones if present, keep old
2288 * if the algorithm remains the same.
2289 */
2290 if ( RTASN1CORE_IS_PRESENT(&pTbsCert->SubjectPublicKeyInfo.Algorithm.Parameters.u.Core)
2291 && pTbsCert->SubjectPublicKeyInfo.Algorithm.Parameters.enmType != RTASN1TYPE_NULL)
2292 pThis->v.pWorkingPublicKeyParameters = &pTbsCert->SubjectPublicKeyInfo.Algorithm.Parameters;
2293 else if ( pThis->v.pWorkingPublicKeyParameters
2294 && RTAsn1ObjId_Compare(pThis->v.pWorkingPublicKeyAlgorithm, &pTbsCert->SubjectPublicKeyInfo.Algorithm.Algorithm) != 0)
2295 pThis->v.pWorkingPublicKeyParameters = NULL;
2296
2297 /*
2298 * 6.1.4.f - The public algorithm.
2299 */
2300 pThis->v.pWorkingPublicKeyAlgorithm = &pTbsCert->SubjectPublicKeyInfo.Algorithm.Algorithm;
2301}
2302
2303
2304/**
2305 * Step 6.1.4.g.
2306 */
2307static bool rtCrX509CpvSoakUpNameConstraints(PRTCRX509CERTPATHSINT pThis, PCRTCRX509NAMECONSTRAINTS pNameConstraints)
2308{
2309 if (pNameConstraints->T0.PermittedSubtrees.cItems > 0)
2310 if (!rtCrX509CpvIntersectionPermittedSubtrees(pThis, &pNameConstraints->T0.PermittedSubtrees))
2311 return false;
2312
2313 if (pNameConstraints->T1.ExcludedSubtrees.cItems > 0)
2314 if (!rtCrX509CpvAddExcludedSubtrees(pThis, &pNameConstraints->T1.ExcludedSubtrees))
2315 return false;
2316
2317 return true;
2318}
2319
2320
2321/**
2322 * Step 6.1.4.i.
2323 */
2324static bool rtCrX509CpvSoakUpPolicyConstraints(PRTCRX509CERTPATHSINT pThis, PCRTCRX509POLICYCONSTRAINTS pPolicyConstraints)
2325{
2326 if (RTAsn1Integer_IsPresent(&pPolicyConstraints->RequireExplicitPolicy))
2327 {
2328 if (RTAsn1Integer_UnsignedCompareWithU32(&pPolicyConstraints->RequireExplicitPolicy, pThis->v.cExplicitPolicy) < 0)
2329 pThis->v.cExplicitPolicy = pPolicyConstraints->RequireExplicitPolicy.uValue.s.Lo;
2330 }
2331
2332 if (RTAsn1Integer_IsPresent(&pPolicyConstraints->InhibitPolicyMapping))
2333 {
2334 if (RTAsn1Integer_UnsignedCompareWithU32(&pPolicyConstraints->InhibitPolicyMapping, pThis->v.cInhibitPolicyMapping) < 0)
2335 pThis->v.cInhibitPolicyMapping = pPolicyConstraints->InhibitPolicyMapping.uValue.s.Lo;
2336 }
2337 return true;
2338}
2339
2340
2341/**
2342 * Step 6.1.4.j.
2343 */
2344static bool rtCrX509CpvSoakUpInhibitAnyPolicy(PRTCRX509CERTPATHSINT pThis, PCRTASN1INTEGER pInhibitAnyPolicy)
2345{
2346 if (RTAsn1Integer_UnsignedCompareWithU32(pInhibitAnyPolicy, pThis->v.cInhibitAnyPolicy) < 0)
2347 pThis->v.cInhibitAnyPolicy = pInhibitAnyPolicy->uValue.s.Lo;
2348 return true;
2349}
2350
2351
2352/**
2353 * Steps 6.1.4.k, 6.1.4.l, 6.1.4.m, and 6.1.4.n.
2354 */
2355static bool rtCrX509CpvCheckAndSoakUpBasicConstraintsAndKeyUsage(PRTCRX509CERTPATHSINT pThis, PRTCRX509CERTPATHNODE pNode,
2356 bool fSelfIssued)
2357{
2358 /* 6.1.4.k - If basic constraints present, CA must be set. */
2359 if (RTAsn1Integer_UnsignedCompareWithU32(&pNode->pCert->TbsCertificate.T0.Version, RTCRX509TBSCERTIFICATE_V3) != 0)
2360 {
2361 /* Note! Add flags if support for older certificates is needed later. */
2362 return rtCrX509CpvFailed(pThis, VERR_CR_X509_CPV_NOT_V3_CERT,
2363 "Only version 3 certificates are supported (Version=%llu)",
2364 pNode->pCert->TbsCertificate.T0.Version.uValue);
2365 }
2366 PCRTCRX509BASICCONSTRAINTS pBasicConstraints = pNode->pCert->TbsCertificate.T3.pBasicConstraints;
2367 if (pBasicConstraints)
2368 {
2369 if (!pBasicConstraints->CA.fValue)
2370 return rtCrX509CpvFailed(pThis, VERR_CR_X509_CPV_NOT_CA_CERT,
2371 "Intermediate certificate (#%u) is not marked as a CA", pThis->v.iNode);
2372 }
2373
2374 /* 6.1.4.l - Work cMaxPathLength. */
2375 if (!fSelfIssued)
2376 {
2377 if (pThis->v.cMaxPathLength > 0)
2378 pThis->v.cMaxPathLength--;
2379 else
2380 return rtCrX509CpvFailed(pThis, VERR_CR_X509_CPV_MAX_PATH_LENGTH,
2381 "Hit max path length at node #%u", pThis->v.iNode);
2382 }
2383
2384 /* 6.1.4.m - Update cMaxPathLength if basic constrain field is present and smaller. */
2385 if (pBasicConstraints)
2386 {
2387 if (RTAsn1Integer_IsPresent(&pBasicConstraints->PathLenConstraint))
2388 if (RTAsn1Integer_UnsignedCompareWithU32(&pBasicConstraints->PathLenConstraint, pThis->v.cMaxPathLength) < 0)
2389 pThis->v.cMaxPathLength = pBasicConstraints->PathLenConstraint.uValue.s.Lo;
2390 }
2391
2392 /* 6.1.4.n - Require keyCertSign in key usage if the extension is present. */
2393 PCRTCRX509TBSCERTIFICATE const pTbsCert = &pNode->pCert->TbsCertificate;
2394 if ( (pTbsCert->T3.fFlags & RTCRX509TBSCERTIFICATE_F_PRESENT_KEY_USAGE)
2395 && !(pTbsCert->T3.fKeyUsage & RTCRX509CERT_KEY_USAGE_F_KEY_CERT_SIGN))
2396 return rtCrX509CpvFailed(pThis, VERR_CR_X509_CPV_MISSING_KEY_CERT_SIGN,
2397 "Node #%u does not have KeyCertSign set (keyUsage=%#x)",
2398 pThis->v.iNode, pTbsCert->T3.fKeyUsage);
2399
2400 return true;
2401}
2402
2403
2404/**
2405 * Step 6.1.4.o - check out critical extensions.
2406 */
2407static bool rtCrX509CpvCheckCriticalExtensions(PRTCRX509CERTPATHSINT pThis, PRTCRX509CERTPATHNODE pNode)
2408{
2409 uint32_t cLeft = pNode->pCert->TbsCertificate.T3.Extensions.cItems;
2410 PCRTCRX509EXTENSION pCur = pNode->pCert->TbsCertificate.T3.Extensions.paItems;
2411 while (cLeft-- > 0)
2412 {
2413 if (pCur->Critical.fValue)
2414 {
2415 if ( RTAsn1ObjId_CompareWithString(&pCur->ExtnId, RTCRX509_ID_CE_KEY_USAGE_OID) != 0
2416 && RTAsn1ObjId_CompareWithString(&pCur->ExtnId, RTCRX509_ID_CE_SUBJECT_ALT_NAME_OID) != 0
2417 && RTAsn1ObjId_CompareWithString(&pCur->ExtnId, RTCRX509_ID_CE_ISSUER_ALT_NAME_OID) != 0
2418 && RTAsn1ObjId_CompareWithString(&pCur->ExtnId, RTCRX509_ID_CE_BASIC_CONSTRAINTS_OID) != 0
2419 && RTAsn1ObjId_CompareWithString(&pCur->ExtnId, RTCRX509_ID_CE_NAME_CONSTRAINTS_OID) != 0
2420 && RTAsn1ObjId_CompareWithString(&pCur->ExtnId, RTCRX509_ID_CE_CERTIFICATE_POLICIES_OID) != 0
2421 && RTAsn1ObjId_CompareWithString(&pCur->ExtnId, RTCRX509_ID_CE_POLICY_MAPPINGS_OID) != 0
2422 && RTAsn1ObjId_CompareWithString(&pCur->ExtnId, RTCRX509_ID_CE_POLICY_CONSTRAINTS_OID) != 0
2423 && RTAsn1ObjId_CompareWithString(&pCur->ExtnId, RTCRX509_ID_CE_EXT_KEY_USAGE_OID) != 0
2424 && RTAsn1ObjId_CompareWithString(&pCur->ExtnId, RTCRX509_ID_CE_INHIBIT_ANY_POLICY_OID) != 0
2425 )
2426 return rtCrX509CpvFailed(pThis, VERR_CR_X509_CPV_UNKNOWN_CRITICAL_EXTENSION,
2427 "Node #%u has an unknown critical extension: %s", pThis->v.iNode, pCur->ExtnId.szObjId);
2428 }
2429
2430 pCur++;
2431 }
2432
2433 return true;
2434}
2435
2436
2437/**
2438 * Step 6.1.5 - The wrapping up.
2439 */
2440static bool rtCrX509CpvWrapUp(PRTCRX509CERTPATHSINT pThis, PRTCRX509CERTPATHNODE pNode)
2441{
2442 Assert(!pNode->pParent); Assert(pThis->pTarget == pNode->pCert);
2443
2444 /*
2445 * 6.1.5.a - Decrement explicit policy.
2446 */
2447 if (pThis->v.cExplicitPolicy > 0)
2448 pThis->v.cExplicitPolicy--;
2449
2450 /*
2451 * 6.1.5.b - Policy constraints and explicit policy.
2452 */
2453 PCRTCRX509POLICYCONSTRAINTS pPolicyConstraints = pNode->pCert->TbsCertificate.T3.pPolicyConstraints;
2454 if ( pPolicyConstraints
2455 && RTAsn1Integer_IsPresent(&pPolicyConstraints->RequireExplicitPolicy)
2456 && RTAsn1Integer_UnsignedCompareWithU32(&pPolicyConstraints->RequireExplicitPolicy, 0) == 0)
2457 pThis->v.cExplicitPolicy = 0;
2458
2459 /*
2460 * 6.1.5.c-e - Update working public key info.
2461 */
2462 rtCrX509CpvSetWorkingPublicKeyInfo(pThis, pNode);
2463
2464 /*
2465 * 6.1.5.f - Critical extensions.
2466 */
2467 if (!rtCrX509CpvCheckCriticalExtensions(pThis, pNode))
2468 return false;
2469
2470 /*
2471 * 6.1.5.g - Calculate the intersection between the user initial policy set
2472 * and the valid policy tree.
2473 */
2474 rtCrX509CpvPolicyTreeIntersect(pThis, pThis->cInitialUserPolicySet, pThis->papInitialUserPolicySet);
2475
2476 if ( pThis->v.cExplicitPolicy == 0
2477 && pThis->v.pValidPolicyTree == NULL)
2478 return rtCrX509CpvFailed(pThis, VERR_CR_X509_CPV_NO_VALID_POLICY, "No valid policy (wrap-up).");
2479
2480 return true;
2481}
2482
2483
2484/**
2485 * Worker that validates one path.
2486 *
2487 * This implements the the algorithm in RFC-5280, section 6.1, with exception of
2488 * the CRL checks in 6.1.3.a.3.
2489 *
2490 * @returns success indicator.
2491 * @param pThis The path builder & validator instance.
2492 * @param pTrustAnchor The trust anchor node.
2493 */
2494static bool rtCrX509CpvOneWorker(PRTCRX509CERTPATHSINT pThis, PRTCRX509CERTPATHNODE pTrustAnchor)
2495{
2496 /*
2497 * Special case, target certificate is trusted.
2498 */
2499 if (!pTrustAnchor->pParent)
2500 return rtCrX509CpvFailed(pThis, VERR_CR_X509_CERTPATHS_INTERNAL_ERROR, "Target certificate is trusted.");
2501
2502 /*
2503 * Normal processing.
2504 */
2505 rtCrX509CpvInit(pThis, pTrustAnchor);
2506 if (RT_SUCCESS(pThis->rc))
2507 {
2508 PRTCRX509CERTPATHNODE pNode = pTrustAnchor->pParent;
2509 uint32_t iNode = pThis->v.iNode = 1; /* We count to cNode (inclusive). Same a validation tree depth. */
2510 while (pNode && RT_SUCCESS(pThis->rc))
2511 {
2512 /*
2513 * Basic certificate processing.
2514 */
2515 if (!rtCrX509CpvCheckBasicCertInfo(pThis, pNode)) /* Step 6.1.3.a */
2516 break;
2517
2518 bool const fSelfIssued = rtCrX509CertPathsIsSelfIssued(pNode);
2519 if (!fSelfIssued || !pNode->pParent) /* Step 6.1.3.b-c */
2520 if (!rtCrX509CpvCheckNameConstraints(pThis, pNode))
2521 break;
2522
2523 if (!rtCrX509CpvWorkValidPolicyTree(pThis, iNode, pNode, fSelfIssued)) /* Step 6.1.3.d-f */
2524 break;
2525
2526 /*
2527 * If it's the last certificate in the path, do wrap-ups.
2528 */
2529 if (!pNode->pParent) /* Step 6.1.5 */
2530 {
2531 Assert(iNode == pThis->v.cNodes);
2532 if (!rtCrX509CpvWrapUp(pThis, pNode))
2533 break;
2534 AssertRCBreak(pThis->rc);
2535 return true;
2536 }
2537
2538 /*
2539 * Preparations for the next certificate.
2540 */
2541 PCRTCRX509TBSCERTIFICATE const pTbsCert = &pNode->pCert->TbsCertificate;
2542 if ( pTbsCert->T3.pPolicyMappings
2543 && !rtCrX509CpvSoakUpPolicyMappings(pThis, iNode, pTbsCert->T3.pPolicyMappings)) /* Step 6.1.4.a-b */
2544 break;
2545
2546 pThis->v.pWorkingIssuer = &pTbsCert->Subject; /* Step 6.1.4.c */
2547
2548 rtCrX509CpvSetWorkingPublicKeyInfo(pThis, pNode); /* Step 6.1.4.d-f */
2549
2550 if ( pTbsCert->T3.pNameConstraints /* Step 6.1.4.g */
2551 && !rtCrX509CpvSoakUpNameConstraints(pThis, pTbsCert->T3.pNameConstraints))
2552 break;
2553
2554 if (!fSelfIssued) /* Step 6.1.4.h */
2555 {
2556 if (pThis->v.cExplicitPolicy > 0)
2557 pThis->v.cExplicitPolicy--;
2558 if (pThis->v.cInhibitPolicyMapping > 0)
2559 pThis->v.cInhibitPolicyMapping--;
2560 if (pThis->v.cInhibitAnyPolicy > 0)
2561 pThis->v.cInhibitAnyPolicy--;
2562 }
2563
2564 if ( pTbsCert->T3.pPolicyConstraints /* Step 6.1.4.j */
2565 && !rtCrX509CpvSoakUpPolicyConstraints(pThis, pTbsCert->T3.pPolicyConstraints))
2566 break;
2567
2568 if ( pTbsCert->T3.pInhibitAnyPolicy /* Step 6.1.4.j */
2569 && !rtCrX509CpvSoakUpInhibitAnyPolicy(pThis, pTbsCert->T3.pInhibitAnyPolicy))
2570 break;
2571
2572 if (!rtCrX509CpvCheckAndSoakUpBasicConstraintsAndKeyUsage(pThis, pNode, fSelfIssued)) /* Step 6.1.4.k-n */
2573 break;
2574
2575 if (!rtCrX509CpvCheckCriticalExtensions(pThis, pNode)) /* Step 6.1.4.o */
2576 break;
2577
2578 /*
2579 * Advance to the next certificate.
2580 */
2581 pNode = pNode->pParent;
2582 pThis->v.iNode = ++iNode;
2583 }
2584 AssertStmt(RT_FAILURE_NP(pThis->rc), pThis->rc = VERR_CR_X509_CERTPATHS_INTERNAL_ERROR);
2585 }
2586 return false;
2587}
2588
2589
2590RTDECL(int) RTCrX509CertPathsValidateOne(RTCRX509CERTPATHS hCertPaths, uint32_t iPath, PRTERRINFO pErrInfo)
2591{
2592 /*
2593 * Validate the input.
2594 */
2595 PRTCRX509CERTPATHSINT pThis = hCertPaths;
2596 AssertPtrReturn(pThis, VERR_INVALID_HANDLE);
2597 AssertReturn(pThis->u32Magic == RTCRX509CERTPATHSINT_MAGIC, VERR_INVALID_HANDLE);
2598 AssertReturn(!(pThis->fFlags & ~RTCRX509CERTPATHSINT_F_VALID_MASK), VERR_INVALID_PARAMETER);
2599 AssertPtrReturn(pThis->pTarget, VERR_INVALID_PARAMETER);
2600 AssertPtrReturn(pThis->pRoot, VERR_INVALID_PARAMETER);
2601 AssertReturn(pThis->rc == VINF_SUCCESS, VERR_INVALID_PARAMETER);
2602
2603 /*
2604 * Locate the path and validate it.
2605 */
2606 int rc;
2607 if (iPath < pThis->cPaths)
2608 {
2609 PRTCRX509CERTPATHNODE pLeaf = rtCrX509CertPathsGetLeafByIndex(pThis, iPath);
2610 if (pLeaf)
2611 {
2612 if (RTCRX509CERTPATHNODE_SRC_IS_TRUSTED(pLeaf->uSrc))
2613 {
2614 pThis->pErrInfo = pErrInfo;
2615 rtCrX509CpvOneWorker(pThis, pLeaf);
2616 pThis->pErrInfo = NULL;
2617 rc = pThis->rc;
2618 pThis->rc = VINF_SUCCESS;
2619 }
2620 else
2621 rc = RTErrInfoSetF(pErrInfo, VERR_CR_X509_NO_TRUST_ANCHOR, "Path #%u is does not have a trust anchor: uSrc=%s",
2622 iPath, rtCrX509CertPathsNodeGetSourceName(pLeaf));
2623 pLeaf->rcVerify = rc;
2624 }
2625 else
2626 rc = VERR_CR_X509_CERTPATHS_INTERNAL_ERROR;
2627 }
2628 else
2629 rc = VERR_NOT_FOUND;
2630 return rc;
2631}
2632
2633
2634RTDECL(int) RTCrX509CertPathsValidateAll(RTCRX509CERTPATHS hCertPaths, uint32_t *pcValidPaths, PRTERRINFO pErrInfo)
2635{
2636 /*
2637 * Validate the input.
2638 */
2639 PRTCRX509CERTPATHSINT pThis = hCertPaths;
2640 AssertPtrReturn(pThis, VERR_INVALID_HANDLE);
2641 AssertReturn(pThis->u32Magic == RTCRX509CERTPATHSINT_MAGIC, VERR_INVALID_HANDLE);
2642 AssertReturn(!(pThis->fFlags & ~RTCRX509CERTPATHSINT_F_VALID_MASK), VERR_INVALID_PARAMETER);
2643 AssertPtrReturn(pThis->pTarget, VERR_INVALID_PARAMETER);
2644 AssertPtrReturn(pThis->pRoot, VERR_INVALID_PARAMETER);
2645 AssertReturn(pThis->rc == VINF_SUCCESS, VERR_INVALID_PARAMETER);
2646 AssertPtrNullReturn(pcValidPaths, VERR_INVALID_POINTER);
2647
2648 /*
2649 * Validate the paths.
2650 */
2651 pThis->pErrInfo = pErrInfo;
2652
2653 int rcLastFailure = VINF_SUCCESS;
2654 uint32_t cValidPaths = 0;
2655 PRTCRX509CERTPATHNODE pCurLeaf;
2656 RTListForEach(&pThis->LeafList, pCurLeaf, RTCRX509CERTPATHNODE, ChildListOrLeafEntry)
2657 {
2658 if (RTCRX509CERTPATHNODE_SRC_IS_TRUSTED(pCurLeaf->uSrc))
2659 {
2660 rtCrX509CpvOneWorker(hCertPaths, pCurLeaf);
2661 if (RT_SUCCESS(pThis->rc))
2662 cValidPaths++;
2663 else
2664 rcLastFailure = pThis->rc;
2665 pCurLeaf->rcVerify = pThis->rc;
2666 pThis->rc = VINF_SUCCESS;
2667 }
2668 else
2669 pCurLeaf->rcVerify = VERR_CR_X509_NO_TRUST_ANCHOR;
2670 }
2671
2672 pThis->pErrInfo = NULL;
2673
2674 if (pcValidPaths)
2675 *pcValidPaths = cValidPaths;
2676 if (cValidPaths > 0)
2677 return VINF_SUCCESS;
2678 if (RT_SUCCESS_NP(rcLastFailure))
2679 return RTErrInfoSetF(pErrInfo, VERR_CR_X509_CPV_NO_TRUSTED_PATHS,
2680 "None of the %u path(s) have a trust anchor.", pThis->cPaths);
2681 return rcLastFailure;
2682}
2683
2684
2685RTDECL(uint32_t) RTCrX509CertPathsGetPathCount(RTCRX509CERTPATHS hCertPaths)
2686{
2687 /*
2688 * Validate the input.
2689 */
2690 PRTCRX509CERTPATHSINT pThis = hCertPaths;
2691 AssertPtrReturn(pThis, UINT32_MAX);
2692 AssertReturn(pThis->u32Magic == RTCRX509CERTPATHSINT_MAGIC, UINT32_MAX);
2693 AssertPtrReturn(pThis->pRoot, UINT32_MAX);
2694
2695 /*
2696 * Return data.
2697 */
2698 return pThis->cPaths;
2699}
2700
2701
2702RTDECL(int) RTCrX509CertPathsQueryPathInfo(RTCRX509CERTPATHS hCertPaths, uint32_t iPath,
2703 bool *pfTrusted, uint32_t *pcNodes, PCRTCRX509NAME *ppSubject,
2704 PCRTCRX509SUBJECTPUBLICKEYINFO *ppPublicKeyInfo,
2705 PCRTCRX509CERTIFICATE *ppCert, PCRTCRCERTCTX *ppCertCtx,
2706 int *prcVerify)
2707{
2708 /*
2709 * Validate the input.
2710 */
2711 PRTCRX509CERTPATHSINT pThis = hCertPaths;
2712 AssertPtrReturn(pThis, VERR_INVALID_HANDLE);
2713 AssertReturn(pThis->u32Magic == RTCRX509CERTPATHSINT_MAGIC, VERR_INVALID_HANDLE);
2714 AssertPtrReturn(pThis->pRoot, VERR_WRONG_ORDER);
2715 AssertReturn(iPath < pThis->cPaths, VERR_NOT_FOUND);
2716
2717 /*
2718 * Get the data.
2719 */
2720 PRTCRX509CERTPATHNODE pLeaf = rtCrX509CertPathsGetLeafByIndex(pThis, iPath);
2721 AssertReturn(pLeaf, VERR_CR_X509_INTERNAL_ERROR);
2722
2723 if (pfTrusted)
2724 *pfTrusted = RTCRX509CERTPATHNODE_SRC_IS_TRUSTED(pLeaf->uSrc);
2725
2726 if (pcNodes)
2727 *pcNodes = pLeaf->uDepth + 1; /* Includes both trust anchor and target. */
2728
2729 if (ppSubject)
2730 *ppSubject = pLeaf->pCert ? &pLeaf->pCert->TbsCertificate.Subject : &pLeaf->pCertCtx->pTaInfo->CertPath.TaName;
2731
2732 if (ppPublicKeyInfo)
2733 *ppPublicKeyInfo = pLeaf->pCert ? &pLeaf->pCert->TbsCertificate.SubjectPublicKeyInfo : &pLeaf->pCertCtx->pTaInfo->PubKey;
2734
2735 if (ppCert)
2736 *ppCert = pLeaf->pCert;
2737
2738 if (ppCertCtx)
2739 {
2740 if (pLeaf->pCertCtx)
2741 {
2742 uint32_t cRefs = RTCrCertCtxRetain(pLeaf->pCertCtx);
2743 AssertReturn(cRefs != UINT32_MAX, VERR_CR_X509_INTERNAL_ERROR);
2744 }
2745 *ppCertCtx = pLeaf->pCertCtx;
2746 }
2747
2748 if (prcVerify)
2749 *prcVerify = pLeaf->rcVerify;
2750
2751 return VINF_SUCCESS;
2752}
2753
2754
2755RTDECL(uint32_t) RTCrX509CertPathsGetPathLength(RTCRX509CERTPATHS hCertPaths, uint32_t iPath)
2756{
2757 /*
2758 * Validate the input.
2759 */
2760 PRTCRX509CERTPATHSINT pThis = hCertPaths;
2761 AssertPtrReturn(pThis, UINT32_MAX);
2762 AssertReturn(pThis->u32Magic == RTCRX509CERTPATHSINT_MAGIC, UINT32_MAX);
2763 AssertPtrReturn(pThis->pRoot, UINT32_MAX);
2764 AssertReturn(iPath < pThis->cPaths, UINT32_MAX);
2765
2766 /*
2767 * Get the data.
2768 */
2769 PRTCRX509CERTPATHNODE pLeaf = rtCrX509CertPathsGetLeafByIndex(pThis, iPath);
2770 AssertReturn(pLeaf, UINT32_MAX);
2771 return pLeaf->uDepth + 1;
2772}
2773
2774
2775RTDECL(int) RTCrX509CertPathsGetPathVerifyResult(RTCRX509CERTPATHS hCertPaths, uint32_t iPath)
2776{
2777 /*
2778 * Validate the input.
2779 */
2780 PRTCRX509CERTPATHSINT pThis = hCertPaths;
2781 AssertPtrReturn(pThis, VERR_INVALID_HANDLE);
2782 AssertReturn(pThis->u32Magic == RTCRX509CERTPATHSINT_MAGIC, VERR_INVALID_HANDLE);
2783 AssertPtrReturn(pThis->pRoot, VERR_WRONG_ORDER);
2784 AssertReturn(iPath < pThis->cPaths, VERR_NOT_FOUND);
2785
2786 /*
2787 * Get the data.
2788 */
2789 PRTCRX509CERTPATHNODE pLeaf = rtCrX509CertPathsGetLeafByIndex(pThis, iPath);
2790 AssertReturn(pLeaf, VERR_CR_X509_INTERNAL_ERROR);
2791
2792 return pLeaf->rcVerify;
2793}
2794
2795
2796static PRTCRX509CERTPATHNODE rtCrX509CertPathsGetPathNodeByIndexes(PRTCRX509CERTPATHSINT pThis, uint32_t iPath, uint32_t iNode)
2797{
2798 PRTCRX509CERTPATHNODE pNode = rtCrX509CertPathsGetLeafByIndex(pThis, iPath);
2799 Assert(pNode);
2800 if (pNode)
2801 {
2802 if (iNode <= pNode->uDepth)
2803 {
2804 uint32_t uCertDepth = pNode->uDepth - iNode;
2805 while (pNode->uDepth > uCertDepth)
2806 pNode = pNode->pParent;
2807 Assert(pNode);
2808 Assert(pNode && pNode->uDepth == uCertDepth);
2809 return pNode;
2810 }
2811 }
2812
2813 return NULL;
2814}
2815
2816
2817RTDECL(PCRTCRX509CERTIFICATE) RTCrX509CertPathsGetPathNodeCert(RTCRX509CERTPATHS hCertPaths, uint32_t iPath, uint32_t iNode)
2818{
2819 /*
2820 * Validate the input.
2821 */
2822 PRTCRX509CERTPATHSINT pThis = hCertPaths;
2823 AssertPtrReturn(pThis, NULL);
2824 AssertReturn(pThis->u32Magic == RTCRX509CERTPATHSINT_MAGIC, NULL);
2825 AssertPtrReturn(pThis->pRoot, NULL);
2826 AssertReturn(iPath < pThis->cPaths, NULL);
2827
2828 /*
2829 * Get the data.
2830 */
2831 PRTCRX509CERTPATHNODE pNode = rtCrX509CertPathsGetPathNodeByIndexes(pThis, iPath, iNode);
2832 if (pNode)
2833 return pNode->pCert;
2834 return NULL;
2835}
2836
2837
2838/** @} */
2839
Note: See TracBrowser for help on using the repository browser.

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette