2 * Copyright (c) 2017 Apple Inc. All Rights Reserved.
4 * @APPLE_LICENSE_HEADER_START@
6 * This file contains Original Code and/or Modifications of Original Code
7 * as defined in and that are subject to the Apple Public Source License
8 * Version 2.0 (the 'License'). You may not use this file except in
9 * compliance with the License. Please obtain a copy of the License at
10 * http://www.opensource.apple.com/apsl/ and read it before using this
13 * The Original Code and all software distributed under the License are
14 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
15 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
16 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
18 * Please see the License for the specific language governing rights and
19 * limitations under the License.
21 * @APPLE_LICENSE_HEADER_END@
25 * SecCertificateServer.c - SecCertificate and SecCertificatePathVC types
26 * with additonal validation context.
29 #include <CoreFoundation/CoreFoundation.h>
30 #include <AssertMacros.h>
32 #include <libDER/libDER.h>
33 #include <libDER/oids.h>
35 #include <Security/SecCertificate.h>
36 #include <Security/SecCertificatePriv.h>
37 #include <Security/SecCertificateInternal.h>
38 #include <Security/SecItem.h>
39 #include <Security/SecInternal.h>
41 #include <utilities/SecIOFormat.h>
42 #include <utilities/SecCFError.h>
43 #include <utilities/SecCFWrappers.h>
44 #include <utilities/debugging.h>
46 #include "trust/trustd/policytree.h"
47 #include "trust/trustd/SecPolicyServer.h"
48 #include "trust/trustd/SecCertificateServer.h"
49 #include "trust/trustd/SecRevocationServer.h"
52 // MARK: SecCertificateVC
53 /********************************************************
54 ************* SecCertificateVC object ***************
55 ********************************************************/
57 struct SecCertificateVC
{
59 SecCertificateRef certificate
;
60 CFArrayRef usageConstraints
;
61 CFNumberRef revocationReason
;
64 bool require_revocation_response
;
66 CFGiblisWithHashFor(SecCertificateVC
)
68 static void SecCertificateVCDestroy(CFTypeRef cf
) {
69 SecCertificateVCRef cvc
= (SecCertificateVCRef
) cf
;
70 CFReleaseNull(cvc
->certificate
);
71 CFReleaseNull(cvc
->usageConstraints
);
72 CFReleaseNull(cvc
->revocationReason
);
75 static Boolean
SecCertificateVCCompare(CFTypeRef cf1
, CFTypeRef cf2
) {
76 SecCertificateVCRef cv1
= (SecCertificateVCRef
) cf1
;
77 SecCertificateVCRef cv2
= (SecCertificateVCRef
) cf2
;
78 if (!CFEqual(cv1
->certificate
, cv2
->certificate
)) {
81 /* CertificateVCs are the same if either does not have usage constraints. */
82 if (cv1
->usageConstraints
&& cv2
->usageConstraints
&&
83 !CFEqual(cv1
->usageConstraints
, cv2
->usageConstraints
)) {
90 static CFHashCode
SecCertificateVCHash(CFTypeRef cf
) {
91 SecCertificateVCRef cvc
= (SecCertificateVCRef
) cf
;
92 CFHashCode hashCode
= 0;
93 hashCode
+= CFHash(cvc
->certificate
);
94 if (cvc
->usageConstraints
) {
95 hashCode
+= CFHash(cvc
->usageConstraints
);
100 static CFStringRef
SecCertificateVCCopyFormatDescription(CFTypeRef cf
, CFDictionaryRef formatOptions
) {
101 SecCertificateVCRef cvc
= (SecCertificateVCRef
)cf
;
102 return CFCopyDescription(cvc
->certificate
);
105 static bool SecCertificateVCCouldBeEV(SecCertificateRef certificate
) {
106 CFMutableDictionaryRef keySizes
= NULL
;
107 CFNumberRef rsaSize
= NULL
, ecSize
= NULL
;
110 /* 3. Subscriber Certificate. */
112 /* (a) certificate Policies */
113 const SecCECertificatePolicies
*cp
;
114 cp
= SecCertificateGetCertificatePolicies(certificate
);
115 require_quiet(cp
&& cp
->numPolicies
> 0, notEV
);
116 /* Now find at least one policy in here that has a qualifierID of id-qt 2
117 and a policyQualifier that is a URI to the CPS and an EV policy OID. */
119 bool found_ev_anchor_for_leaf_policy
= false;
120 for (ix
= 0; ix
< cp
->numPolicies
; ++ix
) {
121 if (SecPolicyIsEVPolicy(&cp
->policies
[ix
].policyIdentifier
)) {
122 found_ev_anchor_for_leaf_policy
= true;
125 require_quiet(found_ev_anchor_for_leaf_policy
, notEV
);
127 /* (b) cRLDistributionPoint
128 (c) authorityInformationAccess
129 BRv1.3.4: MUST be present with OCSP Responder unless stapled response.
132 /* (d) basicConstraints
133 If present, the cA field MUST be set false. */
134 const SecCEBasicConstraints
*bc
= SecCertificateGetBasicConstraints(certificate
);
136 require_action_quiet(bc
->isCA
== false, notEV
,
137 secnotice("ev", "Leaf has invalid basic constraints"));
141 SecKeyUsage ku
= SecCertificateGetKeyUsage(certificate
);
143 require_action_quiet((ku
& (kSecKeyUsageKeyCertSign
| kSecKeyUsageCRLSign
)) == 0, notEV
,
144 secnotice("ev", "Leaf has invalid key usage %u", ku
));
148 /* The EV Cert Spec errata specifies this, though this is a check for SSL
149 not specifically EV. */
153 Either the value id-kp-serverAuth [RFC5280] or id-kp-clientAuth [RFC5280] or both values MUST be present. Other values SHOULD NOT be present. */
154 SecCertificateCopyExtendedKeyUsage(certificate
);
157 /* 6.1.5 Key Sizes */
158 CFAbsoluteTime jan2014
= 410227200;
159 require_quiet(ecSize
= CFNumberCreateWithCFIndex(NULL
, 256), notEV
);
160 require_quiet(keySizes
= CFDictionaryCreateMutable(NULL
, 2, &kCFTypeDictionaryKeyCallBacks
,
161 &kCFTypeDictionaryValueCallBacks
), notEV
);
162 CFDictionaryAddValue(keySizes
, kSecAttrKeyTypeEC
, ecSize
);
163 if (SecCertificateNotValidBefore(certificate
) < jan2014
) {
164 /* At least RSA 1024 or ECC NIST P-256. */
165 require_quiet(rsaSize
= CFNumberCreateWithCFIndex(NULL
, 1024), notEV
);
166 CFDictionaryAddValue(keySizes
, kSecAttrKeyTypeRSA
, rsaSize
);
167 require_action_quiet(SecCertificateIsAtLeastMinKeySize(certificate
, keySizes
), notEV
,
168 secnotice("ev", "Leaf's public key is too small for issuance before 2014"));
170 /* At least RSA 2028 or ECC NIST P-256. */
171 require_quiet(rsaSize
= CFNumberCreateWithCFIndex(NULL
, 2048), notEV
);
172 CFDictionaryAddValue(keySizes
, kSecAttrKeyTypeRSA
, rsaSize
);
173 require_action_quiet(SecCertificateIsAtLeastMinKeySize(certificate
, keySizes
), notEV
,
174 secnotice("ev", "Leaf's public key is too small for issuance after 2013"));
177 /* 6.3.2 Validity Periods */
178 // Will be checked by the policy server (see SecPolicyCheckValidityPeriodMaximums)
180 /* 7.1.3 Algorithm Object Identifiers */
181 CFAbsoluteTime jan2016
= 473299200;
182 if (SecCertificateNotValidBefore(certificate
) > jan2016
) {
184 require_action_quiet(SecCertificateGetSignatureHashAlgorithm(certificate
) > kSecSignatureHashAlgorithmSHA1
,
185 notEV
, secnotice("ev", "Leaf was issued with SHA-1 after 2015"));
191 CFReleaseNull(rsaSize
);
192 CFReleaseNull(ecSize
);
193 CFReleaseNull(keySizes
);
198 SecCertificateVCRef
SecCertificateVCCreate(SecCertificateRef certificate
, CFArrayRef usageConstraints
) {
199 if (!certificate
) { return NULL
; }
200 CFIndex size
= sizeof(struct SecCertificateVC
);
201 SecCertificateVCRef result
=
202 (SecCertificateVCRef
)_CFRuntimeCreateInstance(kCFAllocatorDefault
,
203 SecCertificateVCGetTypeID(), size
- sizeof(CFRuntimeBase
), 0);
206 result
->certificate
= CFRetainSafe(certificate
);
207 result
->isWeakHash
= SecCertificateIsWeakHash(certificate
);
208 result
->optionallyEV
= SecCertificateVCCouldBeEV(certificate
);
210 CFArrayRef emptyArray
= NULL
;
211 if (!usageConstraints
) {
212 require_action_quiet(emptyArray
= CFArrayCreate(kCFAllocatorDefault
, NULL
, 0, &kCFTypeArrayCallBacks
), exit
, CFReleaseNull(result
));
213 usageConstraints
= emptyArray
;
215 result
->usageConstraints
= CFRetainSafe(usageConstraints
);
217 CFReleaseNull(emptyArray
);
222 // MARK: SecCertificatePathVC
223 /********************************************************
224 ************* SecCertificatePathVC object ***************
225 ********************************************************/
226 struct SecCertificatePathVC
{
230 /* Index of next parent source to search for parents. */
231 CFIndex nextParentSource
;
233 /* Index of last certificate in chain whose signature has been verified.
234 0 means nothing has been checked. 1 means the leaf has been verified
235 against its issuer, etc. */
236 CFIndex lastVerifiedSigner
;
238 /* Index of first self issued certificate in the chain. -1 mean there is
239 none. 0 means the leaf is self signed. */
242 /* True iff cert at index selfIssued does in fact self verify. */
245 /* True if the root of this path is an anchor. Trustedness of the
246 * anchor is determined by the PVC. */
249 policy_tree_t policy_tree
;
250 uint8_t policy_tree_verification_result
;
255 bool hasStrongHashes
;
260 /* This is the score of the path after determining acceptance. */
265 /* If checkedIssuers is true, then the value of unknownCAIndex contains
266 * the index of the first CA which violates known-only constraints, or
267 * -1 if all CA certificates are either known or not constrained. */
269 CFIndex unknownCAIndex
;
271 /* Enumerated value to determine whether CT is required for the leaf
272 * certificate (because a CA in the path has a require-ct constraint).
273 * If non-zero, CT is required; value indicates overridable status. */
274 SecPathCTPolicy requiresCT
;
276 /* Issuance time, as determined by earliest SCT timestamp for leaf. */
277 CFAbsoluteTime issuanceTime
;
279 SecCertificateVCRef certificates
[];
281 CFGiblisWithHashFor(SecCertificatePathVC
)
283 static void SecCertificatePathVCPrunePolicyTree(SecCertificatePathVCRef certificatePath
) {
284 if (certificatePath
->policy_tree
) {
285 policy_tree_prune(&certificatePath
->policy_tree
);
289 void SecCertificatePathVCDeleteRVCs(SecCertificatePathVCRef path
) {
291 CFIndex certIX
, certCount
= path
->rvcCount
;
292 for (certIX
= 0; certIX
< certCount
; ++certIX
) {
293 SecRVCRef rvc
= &((SecRVCRef
)path
->rvcs
)[certIX
];
301 static void SecCertificatePathVCDestroy(CFTypeRef cf
) {
302 SecCertificatePathVCRef certificatePath
= (SecCertificatePathVCRef
) cf
;
304 SecCertificatePathVCDeleteRVCs(certificatePath
);
305 SecCertificatePathVCPrunePolicyTree(certificatePath
);
306 for (ix
= 0; ix
< certificatePath
->count
; ++ix
) {
307 CFReleaseNull(certificatePath
->certificates
[ix
]);
311 static Boolean
SecCertificatePathVCCompare(CFTypeRef cf1
, CFTypeRef cf2
) {
312 SecCertificatePathVCRef cp1
= (SecCertificatePathVCRef
) cf1
;
313 SecCertificatePathVCRef cp2
= (SecCertificatePathVCRef
) cf2
;
314 if (cp1
->count
!= cp2
->count
)
317 for (ix
= 0; ix
< cp1
->count
; ++ix
) {
318 if (!CFEqual(cp1
->certificates
[ix
], cp2
->certificates
[ix
]))
325 static CFHashCode
SecCertificatePathVCHash(CFTypeRef cf
) {
326 SecCertificatePathVCRef certificatePath
= (SecCertificatePathVCRef
) cf
;
327 CFHashCode hashCode
= 0;
329 for (ix
= 0; ix
< certificatePath
->count
; ++ix
) {
330 hashCode
+= CFHash(certificatePath
->certificates
[ix
]);
335 static CFStringRef
SecCertificatePathVCCopyFormatDescription(CFTypeRef cf
, CFDictionaryRef formatOptions
) {
336 SecCertificatePathVCRef certificatePath
= (SecCertificatePathVCRef
) cf
;
337 CFMutableStringRef desc
= CFStringCreateMutable(kCFAllocatorDefault
, 0);
338 CFStringRef typeStr
= CFCopyTypeIDDescription(CFGetTypeID(cf
));
339 CFStringAppendFormat(desc
, NULL
,
340 CFSTR("<%@ certs: "), typeStr
);
343 for (ix
= 0; ix
< certificatePath
->count
; ++ix
) {
345 CFStringAppend(desc
, CFSTR(", "));
347 CFStringRef str
= CFCopyDescription(certificatePath
->certificates
[ix
]);
348 CFStringAppend(desc
, str
);
351 CFStringAppend(desc
, CFSTR(" >"));
356 /* Create a new certificate path from an old one. */
357 SecCertificatePathVCRef
SecCertificatePathVCCreate(SecCertificatePathVCRef path
,
358 SecCertificateRef certificate
, CFArrayRef usageConstraints
) {
359 CFAllocatorRef allocator
= kCFAllocatorDefault
;
362 CFIndex selfIssued
, lastVerifiedSigner
;
365 count
= path
->count
+ 1;
366 lastVerifiedSigner
= path
->lastVerifiedSigner
;
367 selfIssued
= path
->selfIssued
;
368 isSelfSigned
= path
->isSelfSigned
;
371 lastVerifiedSigner
= 0;
373 isSelfSigned
= false;
376 CFIndex size
= sizeof(struct SecCertificatePathVC
) +
377 count
* sizeof(SecCertificateRef
);
378 SecCertificatePathVCRef result
=
379 (SecCertificatePathVCRef
)_CFRuntimeCreateInstance(allocator
,
380 SecCertificatePathVCGetTypeID(), size
- sizeof(CFRuntimeBase
), 0);
384 memset((char*)result
+ sizeof(result
->_base
), 0,
385 sizeof(*result
) - sizeof(result
->_base
));
387 result
->count
= count
;
388 result
->lastVerifiedSigner
= lastVerifiedSigner
;
389 result
->selfIssued
= selfIssued
;
390 result
->isSelfSigned
= isSelfSigned
;
392 for (ix
= 0; ix
< count
- 1; ++ix
) {
393 result
->certificates
[ix
] = path
->certificates
[ix
];
394 CFRetain(result
->certificates
[ix
]);
397 SecCertificateVCRef cvc
= SecCertificateVCCreate(certificate
, usageConstraints
);
398 result
->certificates
[count
- 1] = cvc
;
403 SecCertificatePathVCRef
SecCertificatePathVCCopyFromParent(
404 SecCertificatePathVCRef path
, CFIndex skipCount
) {
405 CFAllocatorRef allocator
= kCFAllocatorDefault
;
407 CFIndex selfIssued
, lastVerifiedSigner
;
410 /* Ensure we are at least returning a path of length 1. */
411 if (skipCount
< 0 || path
->count
< 1 + skipCount
)
414 count
= path
->count
- skipCount
;
415 lastVerifiedSigner
= path
->lastVerifiedSigner
> skipCount
416 ? path
->lastVerifiedSigner
- skipCount
: 0;
417 selfIssued
= path
->selfIssued
>= skipCount
418 ? path
->selfIssued
- skipCount
: -1;
419 isSelfSigned
= path
->selfIssued
>= 0 ? path
->isSelfSigned
: false;
421 CFIndex size
= sizeof(struct SecCertificatePathVC
) +
422 count
* sizeof(SecCertificateRef
);
423 SecCertificatePathVCRef result
=
424 (SecCertificatePathVCRef
)_CFRuntimeCreateInstance(allocator
,
425 SecCertificatePathVCGetTypeID(), size
- sizeof(CFRuntimeBase
), 0);
429 memset((char*)result
+ sizeof(result
->_base
), 0,
430 sizeof(*result
) - sizeof(result
->_base
));
432 result
->count
= count
;
433 result
->lastVerifiedSigner
= lastVerifiedSigner
;
434 result
->selfIssued
= selfIssued
;
435 result
->isSelfSigned
= isSelfSigned
;
436 result
->isAnchored
= path
->isAnchored
;
438 for (ix
= 0; ix
< count
; ++ix
) {
439 CFIndex pathIX
= ix
+ skipCount
;
440 result
->certificates
[ix
] = path
->certificates
[pathIX
];
441 CFRetain(result
->certificates
[ix
]);
447 SecCertificatePathVCRef
SecCertificatePathVCCopyAddingLeaf(SecCertificatePathVCRef path
,
448 SecCertificateRef leaf
) {
449 CFAllocatorRef allocator
= kCFAllocatorDefault
;
451 CFIndex selfIssued
, lastVerifiedSigner
;
454 /* First make sure the new leaf is signed by path's current leaf. */
455 SecKeyRef issuerKey
= SecCertificatePathVCCopyPublicKeyAtIndex(path
, 0);
458 OSStatus status
= SecCertificateIsSignedBy(leaf
, issuerKey
);
459 CFRelease(issuerKey
);
463 count
= path
->count
+ 1;
464 lastVerifiedSigner
= path
->lastVerifiedSigner
+ 1;
465 selfIssued
= path
->selfIssued
;
466 isSelfSigned
= path
->isSelfSigned
;
468 CFIndex size
= sizeof(struct SecCertificatePathVC
) +
469 count
* sizeof(SecCertificateRef
);
470 SecCertificatePathVCRef result
=
471 (SecCertificatePathVCRef
)_CFRuntimeCreateInstance(allocator
,
472 SecCertificatePathVCGetTypeID(), size
- sizeof(CFRuntimeBase
), 0);
476 memset((char*)result
+ sizeof(result
->_base
), 0,
477 sizeof(*result
) - sizeof(result
->_base
));
479 result
->count
= count
;
480 result
->lastVerifiedSigner
= lastVerifiedSigner
;
481 result
->selfIssued
= selfIssued
;
482 result
->isSelfSigned
= isSelfSigned
;
483 result
->isAnchored
= path
->isAnchored
;
486 for (ix
= 1; ix
< count
; ++ix
) {
487 result
->certificates
[ix
] = path
->certificates
[ix
- 1];
488 CFRetain(result
->certificates
[ix
]);
490 SecCertificateVCRef leafVC
= SecCertificateVCCreate(leaf
, NULL
);
491 result
->certificates
[0] = leafVC
;
496 CFArrayRef
SecCertificatePathVCCopyCertificates(SecCertificatePathVCRef path
) {
497 CFMutableArrayRef outCerts
= NULL
;
498 size_t count
= path
->count
;
499 require_quiet(outCerts
= CFArrayCreateMutable(NULL
, count
, &kCFTypeArrayCallBacks
), exit
);
500 SecCertificatePathVCForEachCertificate(path
, ^(SecCertificateRef cert
, bool * __unused stop
) {
501 CFArrayAppendValue(outCerts
, cert
);
507 CFArrayRef
SecCertificatePathVCCreateSerialized(SecCertificatePathVCRef path
) {
508 CFMutableArrayRef serializedCerts
= NULL
;
509 require_quiet(path
, exit
);
510 size_t count
= path
->count
;
511 require_quiet(serializedCerts
= CFArrayCreateMutable(NULL
, count
, &kCFTypeArrayCallBacks
), exit
);
512 SecCertificatePathVCForEachCertificate(path
, ^(SecCertificateRef cert
, bool * __unused stop
) {
513 CFDataRef certData
= SecCertificateCopyData(cert
);
515 CFArrayAppendValue(serializedCerts
, certData
);
520 return serializedCerts
;
524 /* Record the fact that we found our own root cert as our parent
526 void SecCertificatePathVCSetSelfIssued(
527 SecCertificatePathVCRef certificatePath
) {
528 if (certificatePath
->selfIssued
>= 0) {
529 secdebug("trust", "%@ is already issued at %" PRIdCFIndex
, certificatePath
,
530 certificatePath
->selfIssued
);
533 secdebug("trust", "%@ is self issued", certificatePath
);
534 certificatePath
->selfIssued
= certificatePath
->count
- 1;
536 /* now check that the selfIssued cert was actually self-signed */
537 if (certificatePath
->selfIssued
>= 0 && !certificatePath
->isSelfSigned
) {
538 SecCertificateVCRef certVC
= certificatePath
->certificates
[certificatePath
->selfIssued
];
539 Boolean isSelfSigned
= false;
540 OSStatus status
= SecCertificateIsSelfSigned(certVC
->certificate
, &isSelfSigned
);
541 if ((status
== errSecSuccess
) && isSelfSigned
) {
542 certificatePath
->isSelfSigned
= true;
544 certificatePath
->selfIssued
= -1;
549 void SecCertificatePathVCSetIsAnchored(
550 SecCertificatePathVCRef certificatePath
) {
551 secdebug("trust", "%@ is anchored", certificatePath
);
552 certificatePath
->isAnchored
= true;
554 /* Now check if that anchor (last cert) was actually self-signed.
555 * In the non-anchor case, this is handled by SecCertificatePathVCSetSelfIssued.
556 * Because anchored chains immediately go into the candidate bucket in the trust
557 * server, we need to ensure that the self-signed/self-issued members are set
558 * for the purposes of scoring. */
559 if (!certificatePath
->isSelfSigned
&& certificatePath
->count
> 0) {
560 SecCertificateVCRef certVC
= certificatePath
->certificates
[certificatePath
->count
- 1];
561 Boolean isSelfSigned
= false;
562 OSStatus status
= SecCertificateIsSelfSigned(certVC
->certificate
, &isSelfSigned
);
563 if ((status
== errSecSuccess
) && isSelfSigned
) {
564 certificatePath
->isSelfSigned
= true;
565 if (certificatePath
->selfIssued
== -1) {
566 certificatePath
->selfIssued
= certificatePath
->count
- 1;
572 /* Return the index of the first non anchor certificate in the chain that is
573 self signed counting from the leaf up. Return -1 if there is none. */
574 CFIndex
SecCertificatePathVCSelfSignedIndex(
575 SecCertificatePathVCRef certificatePath
) {
576 if (certificatePath
->isSelfSigned
)
577 return certificatePath
->selfIssued
;
581 Boolean
SecCertificatePathVCIsAnchored(
582 SecCertificatePathVCRef certificatePath
) {
583 return certificatePath
->isAnchored
;
587 void SecCertificatePathVCSetNextSourceIndex(
588 SecCertificatePathVCRef certificatePath
, CFIndex sourceIndex
) {
589 certificatePath
->nextParentSource
= sourceIndex
;
592 CFIndex
SecCertificatePathVCGetNextSourceIndex(
593 SecCertificatePathVCRef certificatePath
) {
594 return certificatePath
->nextParentSource
;
597 CFIndex
SecCertificatePathVCGetCount(
598 SecCertificatePathVCRef certificatePath
) {
599 check(certificatePath
);
600 return certificatePath
? certificatePath
->count
: 0;
603 SecCertificateRef
SecCertificatePathVCGetCertificateAtIndex(
604 SecCertificatePathVCRef certificatePath
, CFIndex ix
) {
605 if (!certificatePath
|| ix
< 0 || ix
>= certificatePath
->count
) {
608 SecCertificateVCRef cvc
= certificatePath
->certificates
[ix
];
609 return cvc
? cvc
->certificate
: NULL
;
612 void SecCertificatePathVCForEachCertificate(SecCertificatePathVCRef path
, void(^operation
)(SecCertificateRef certificate
, bool *stop
)) {
614 CFIndex ix
, count
= path
->count
;
615 for (ix
= 0; ix
< count
; ++ix
) {
616 SecCertificateVCRef cvc
= path
->certificates
[ix
];
617 operation(cvc
->certificate
, &stop
);
622 CFIndex
SecCertificatePathVCGetIndexOfCertificate(SecCertificatePathVCRef path
,
623 SecCertificateRef certificate
) {
624 CFIndex ix
, count
= path
->count
;
625 for (ix
= 0; ix
< count
; ++ix
) {
626 SecCertificateVCRef cvc
= path
->certificates
[ix
];
627 if (CFEqual(cvc
->certificate
, certificate
))
633 /* Return the root certificate for certificatePath. Note that root is just
634 the top of the path as far as it is constructed. It may or may not be
635 trusted or self signed. */
636 SecCertificateRef
SecCertificatePathVCGetRoot(
637 SecCertificatePathVCRef certificatePath
) {
638 return SecCertificatePathVCGetCertificateAtIndex(certificatePath
,
639 SecCertificatePathVCGetCount(certificatePath
) - 1);
642 SecKeyRef
SecCertificatePathVCCopyPublicKeyAtIndex(
643 SecCertificatePathVCRef certificatePath
, CFIndex ix
) {
644 SecCertificateRef certificate
=
645 SecCertificatePathVCGetCertificateAtIndex(certificatePath
, ix
);
646 return SecCertificateCopyKey(certificate
);
649 CFArrayRef
SecCertificatePathVCGetUsageConstraintsAtIndex(
650 SecCertificatePathVCRef certificatePath
, CFIndex ix
) {
651 SecCertificateVCRef cvc
= certificatePath
->certificates
[ix
];
652 return cvc
->usageConstraints
;
655 void SecCertificatePathVCSetUsageConstraintsAtIndex(SecCertificatePathVCRef certificatePath
,
656 CFArrayRef newConstraints
, CFIndex ix
) {
657 CFArrayRef emptyArray
= NULL
;
658 if (!newConstraints
) {
659 require_quiet(emptyArray
= CFArrayCreate(kCFAllocatorDefault
, NULL
, 0, &kCFTypeArrayCallBacks
), exit
);
660 newConstraints
= emptyArray
;
663 SecCertificateVCRef cvc
= certificatePath
->certificates
[ix
];
664 cvc
->usageConstraints
= CFRetainSafe(newConstraints
);
666 CFReleaseNull(emptyArray
);
670 SecPathVerifyStatus
SecCertificatePathVCVerify(SecCertificatePathVCRef certificatePath
) {
671 check(certificatePath
);
672 if (!certificatePath
)
673 return kSecPathVerifyFailed
;
675 certificatePath
->lastVerifiedSigner
< certificatePath
->count
- 1;
676 ++certificatePath
->lastVerifiedSigner
) {
677 SecKeyRef issuerKey
=
678 SecCertificatePathVCCopyPublicKeyAtIndex(certificatePath
,
679 certificatePath
->lastVerifiedSigner
+ 1);
681 return kSecPathVerifiesUnknown
;
682 SecCertificateVCRef cvc
= certificatePath
->certificates
[certificatePath
->lastVerifiedSigner
];
683 OSStatus status
= SecCertificateIsSignedBy(cvc
->certificate
,
685 CFRelease(issuerKey
);
687 return kSecPathVerifyFailed
;
691 return kSecPathVerifySuccess
;
694 /* Is the the issuer of the last cert a subject of a previous cert in the chain.See <rdar://33136765>. */
695 bool SecCertificatePathVCIsCycleInGraph(SecCertificatePathVCRef path
) {
696 bool isCircle
= false;
697 CFDataRef issuer
= SecCertificateGetNormalizedIssuerContent(SecCertificatePathVCGetRoot(path
));
698 if (!issuer
) { return isCircle
; }
699 CFIndex ix
= path
->count
- 2;
700 for (; ix
>= 0; ix
--) {
701 SecCertificateVCRef cvc
= path
->certificates
[ix
];
702 CFDataRef subject
= SecCertificateGetNormalizedSubjectContent(cvc
->certificate
);
703 if (subject
&& CFEqual(issuer
, subject
)) {
711 bool SecCertificatePathVCIsValid(SecCertificatePathVCRef certificatePath
, CFAbsoluteTime verifyTime
) {
712 __block
bool result
= true;
713 SecCertificatePathVCForEachCertificate(certificatePath
, ^(SecCertificateRef certificate
, bool *stop
) {
714 if (!SecCertificateIsValid(certificate
, verifyTime
)) {
721 bool SecCertificatePathVCHasWeakHash(SecCertificatePathVCRef certificatePath
) {
722 CFIndex ix
, count
= certificatePath
->count
;
724 if (certificatePath
->hasStrongHashes
) {
728 if (SecCertificatePathVCIsAnchored(certificatePath
)) {
729 /* For anchored paths, don't check the hash algorithm of the anchored cert,
730 * since we already decided to trust it. */
733 for (ix
= 0; ix
< count
; ++ix
) {
734 if (certificatePath
->certificates
[ix
]->isWeakHash
) {
738 certificatePath
->hasStrongHashes
= true;
742 bool SecCertificatePathVCHasWeakKeySize(SecCertificatePathVCRef certificatePath
) {
743 __block CFDictionaryRef keySizes
= NULL
;
744 CFNumberRef rsaSize
= NULL
, ecSize
= NULL
;
745 __block
bool result
= false;
747 /* RSA key sizes are 2048-bit or larger. EC key sizes are P-224 or larger. */
748 require(rsaSize
= CFNumberCreateWithCFIndex(NULL
, 2048), errOut
);
749 require(ecSize
= CFNumberCreateWithCFIndex(NULL
, 224), errOut
);
750 const void *keys
[] = { kSecAttrKeyTypeRSA
, kSecAttrKeyTypeEC
};
751 const void *values
[] = { rsaSize
, ecSize
};
752 require(keySizes
= CFDictionaryCreate(NULL
, keys
, values
, 2,
753 &kCFTypeDictionaryKeyCallBacks
, &kCFTypeDictionaryValueCallBacks
), errOut
);
754 SecCertificatePathVCForEachCertificate(certificatePath
, ^(SecCertificateRef certificate
, bool *stop
) {
755 if (!SecCertificateIsAtLeastMinKeySize(certificate
, keySizes
)) {
762 CFReleaseSafe(keySizes
);
763 CFReleaseSafe(rsaSize
);
764 CFReleaseSafe(ecSize
);
768 /* Return a score for this certificate chain. */
769 CFIndex
SecCertificatePathVCScore(SecCertificatePathVCRef certificatePath
, CFAbsoluteTime verifyTime
) {
772 /* Paths that don't verify score terribly.c */
773 if (certificatePath
->lastVerifiedSigner
!= certificatePath
->count
- 1) {
774 secdebug("trust", "lvs: %" PRIdCFIndex
" count: %" PRIdCFIndex
,
775 certificatePath
->lastVerifiedSigner
, certificatePath
->count
);
779 if (certificatePath
->isAnchored
) {
780 /* Anchored paths for the win! */
784 if (certificatePath
->isSelfSigned
&& (certificatePath
->selfIssued
== certificatePath
->count
- 1)) {
785 /* Chains that terminate in a self-signed certificate are preferred,
786 even if they don't end in an anchor. */
788 /* Shorter chains ending in a self-signed cert are preferred. */
789 score
-= 1 * certificatePath
->count
;
791 /* Longer chains are preferred when the chain doesn't end in a self-signed cert. */
792 score
+= 1 * certificatePath
->count
;
795 if (SecCertificatePathVCIsValid(certificatePath
, verifyTime
)) {
799 if (!SecCertificatePathVCHasWeakHash(certificatePath
)) {
803 if (!SecCertificatePathVCHasWeakKeySize(certificatePath
)) {
810 CFIndex
SecCertificatePathVCGetScore(SecCertificatePathVCRef certificatePath
) {
811 if (!certificatePath
) { return 0; }
812 return certificatePath
->score
;
815 void SecCertificatePathVCSetScore(SecCertificatePathVCRef certificatePath
, CFIndex score
) {
816 /* We may "score" the same path twice -- if we "accept" a path but then
817 * decide to keep looking for a better one, we we process the same path
818 * again in "reject" which creates a lower score. Don't replace a higher
819 * score with a lower score. Use reset below to post-reject a path. */
820 if (score
> certificatePath
->score
) {
821 certificatePath
->score
= score
;
825 void SecCertificatePathVCResetScore(SecCertificatePathVCRef certificatePath
) {
826 certificatePath
->score
= 0;
829 void *SecCertificatePathVCGetRVCAtIndex(SecCertificatePathVCRef certificatePath
, CFIndex ix
) {
830 if (ix
>= certificatePath
->rvcCount
) {
833 return &((SecRVCRef
)certificatePath
->rvcs
)[ix
];
836 bool SecCertificatePathVCIsRevocationDone(SecCertificatePathVCRef certificatePath
) {
837 return (bool)certificatePath
->rvcs
;
840 void SecCertificatePathVCAllocateRVCs(SecCertificatePathVCRef certificatePath
, CFIndex certCount
) {
841 certificatePath
->rvcs
= calloc(sizeof(struct OpaqueSecRVC
), certCount
);
842 certificatePath
->rvcCount
= certCount
;
845 /* Return 0 if any certs revocation checking failed, or the earliest date on
846 which one of the used revocation validation tokens (ocsp response or
848 /* This function returns 0 to indicate revocation checking was not completed
849 for this certificate chain, otherwise returns the date at which the first
850 piece of revocation checking info we used expires. */
851 CFAbsoluteTime
SecCertificatePathVCGetEarliestNextUpdate(SecCertificatePathVCRef path
) {
852 CFIndex certIX
, certCount
= path
->count
;
853 CFAbsoluteTime enu
= NULL_TIME
;
854 if (certCount
<= 1 || !path
->rvcs
) {
858 for (certIX
= 0; certIX
< path
->rvcCount
; ++certIX
) {
859 SecRVCRef rvc
= &((SecRVCRef
)path
->rvcs
)[certIX
];
860 CFAbsoluteTime thisCertNextUpdate
= SecRVCGetEarliestNextUpdate(rvc
);
861 if (thisCertNextUpdate
== 0) {
863 /* We allow for CA certs to not be revocation checked if they
864 have no ocspResponders to check against, but the leaf
865 must be checked in order for us to claim we did revocation
867 SecCertificateRef cert
= SecCertificatePathVCGetCertificateAtIndex(path
, rvc
->certIX
);
868 CFArrayRef ocspResponders
= NULL
;
869 ocspResponders
= SecCertificateGetOCSPResponders(cert
);
870 if (!ocspResponders
|| CFArrayGetCount(ocspResponders
) == 0) {
871 /* We can't check this cert so we don't consider it a soft
872 failure that we didn't. */
876 /* Make sure to always skip roots for whom we can't check revocation */
877 if (certIX
== certCount
- 1) {
880 secdebug("rvc", "revocation checking soft failure for cert: %ld",
882 enu
= thisCertNextUpdate
;
885 if (enu
== 0 || thisCertNextUpdate
< enu
) {
886 enu
= thisCertNextUpdate
;
890 secdebug("rvc", "revocation valid until: %lg", enu
);
894 bool SecCertificatePathVCRevocationCheckedAllCerts(SecCertificatePathVCRef path
) {
895 CFIndex certIX
, certCount
= path
->count
;
896 if (certCount
<= 1 || !path
->rvcs
) {
897 /* If there is only one certificate, it's the root, so revocation checking is irrelevant. */
901 for (certIX
= 0; certIX
< path
->rvcCount
- 1; ++certIX
) {
902 SecRVCRef rvc
= &((SecRVCRef
)path
->rvcs
)[certIX
];
903 if (!SecRVCRevocationChecked(rvc
)) {
904 secdebug("rvc", "revocation has not been checked for all certs (not checked for cert %ld)", certIX
);
909 secdebug("rvc", "revocation has been checked for all certs");
913 void SecCertificatePathVCSetRevocationReasonForCertificateAtIndex(SecCertificatePathVCRef certificatePath
,
914 CFIndex ix
, CFNumberRef revocationReason
) {
915 if (ix
> certificatePath
->count
- 1) { return; }
916 SecCertificateVCRef cvc
= certificatePath
->certificates
[ix
];
917 cvc
->revocationReason
= CFRetainSafe(revocationReason
);
920 CFNumberRef
SecCertificatePathVCGetRevocationReason(SecCertificatePathVCRef certificatePath
) {
921 for (CFIndex ix
= 0; ix
< certificatePath
->count
; ix
++) {
922 SecCertificateVCRef cvc
= certificatePath
->certificates
[ix
];
923 if (cvc
->revocationReason
) {
924 return cvc
->revocationReason
;
930 bool SecCertificatePathVCIsRevocationRequiredForCertificateAtIndex(SecCertificatePathVCRef certificatePath
,
932 if (ix
> certificatePath
->count
- 1) { return false; }
933 SecCertificateVCRef cvc
= certificatePath
->certificates
[ix
];
934 return cvc
->require_revocation_response
;
937 void SecCertificatePathVCSetRevocationRequiredForCertificateAtIndex(SecCertificatePathVCRef certificatePath
,
939 if (ix
> certificatePath
->count
- 1) { return; }
940 SecCertificateVCRef cvc
= certificatePath
->certificates
[ix
];
941 cvc
->require_revocation_response
= true;
944 bool SecCertificatePathVCCheckedIssuers(SecCertificatePathVCRef certificatePath
) {
945 return certificatePath
->checkedIssuers
;
948 void SecCertificatePathVCSetCheckedIssuers(SecCertificatePathVCRef certificatePath
, bool checked
) {
949 certificatePath
->checkedIssuers
= checked
;
952 CFIndex
SecCertificatePathVCUnknownCAIndex(SecCertificatePathVCRef certificatePath
) {
953 return certificatePath
->unknownCAIndex
;
956 void SecCertificatePathVCSetUnknownCAIndex(SecCertificatePathVCRef certificatePath
, CFIndex index
) {
957 certificatePath
->unknownCAIndex
= index
;
960 bool SecCertificatePathVCIsPathValidated(SecCertificatePathVCRef certificatePath
) {
961 if (!certificatePath
) { return false; }
962 return certificatePath
->pathValidated
;
965 void SecCertificatePathVCSetPathValidated(SecCertificatePathVCRef certificatePath
) {
966 certificatePath
->pathValidated
= true;
969 bool SecCertificatePathVCIsEV(SecCertificatePathVCRef certificatePath
) {
970 if (!certificatePath
) { return false; }
971 return certificatePath
->isEV
;
974 void SecCertificatePathVCSetIsEV(SecCertificatePathVCRef certificatePath
, bool isEV
) {
975 certificatePath
->isEV
= isEV
;
978 bool SecCertificatePathVCIsOptionallyEV(SecCertificatePathVCRef certificatePath
) {
979 if (!certificatePath
) { return false; }
980 return certificatePath
->certificates
[0]->optionallyEV
;
983 bool SecCertificatePathVCIsCT(SecCertificatePathVCRef certificatePath
) {
984 if (!certificatePath
) { return false; }
985 return certificatePath
->isCT
;
988 void SecCertificatePathVCSetIsCT(SecCertificatePathVCRef certificatePath
, bool isCT
) {
989 certificatePath
->isCT
= isCT
;
992 SecPathCTPolicy
SecCertificatePathVCRequiresCT(SecCertificatePathVCRef certificatePath
) {
993 if (!certificatePath
) { return kSecPathCTNotRequired
; }
994 return certificatePath
->requiresCT
;
997 void SecCertificatePathVCSetRequiresCT(SecCertificatePathVCRef certificatePath
, SecPathCTPolicy requiresCT
) {
998 if (certificatePath
->requiresCT
> requiresCT
) {
999 return; /* once set, CT policy may be only be changed to a more strict value */
1001 certificatePath
->requiresCT
= requiresCT
;
1004 CFAbsoluteTime
SecCertificatePathVCIssuanceTime(SecCertificatePathVCRef certificatePath
) {
1005 if (!certificatePath
) { return 0; }
1006 return certificatePath
->issuanceTime
;
1009 void SecCertificatePathVCSetIssuanceTime(SecCertificatePathVCRef certificatePath
, CFAbsoluteTime issuanceTime
) {
1010 certificatePath
->issuanceTime
= issuanceTime
;
1013 bool SecCertificatePathVCIsAllowlisted(SecCertificatePathVCRef certificatePath
) {
1014 if (!certificatePath
) { return false; }
1015 return certificatePath
->is_allowlisted
;
1018 void SecCertificatePathVCSetIsAllowlisted(SecCertificatePathVCRef certificatePath
, bool isAllowlisted
) {
1019 certificatePath
->is_allowlisted
= isAllowlisted
;
1022 /* MARK: policy_tree path verification */
1023 struct policy_tree_add_ctx
{
1025 policy_qualifier_t p_q
;
1028 /* For each node of depth i-1 in the valid_policy_tree where P-OID is in the expected_policy_set, create a child node as follows: set the valid_policy to P-OID, set the qualifier_set to P-Q, and set the expected_policy_set to {P-OID}. */
1029 static bool policy_tree_add_if_match(policy_tree_t node
, void *ctx
) {
1030 struct policy_tree_add_ctx
*info
= (struct policy_tree_add_ctx
*)ctx
;
1031 policy_set_t policy_set
;
1032 for (policy_set
= node
->expected_policy_set
;
1034 policy_set
= policy_set
->oid_next
) {
1035 if (oid_equal(policy_set
->oid
, info
->p_oid
)) {
1036 policy_tree_add_child(node
, &info
->p_oid
, info
->p_q
);
1043 /* If the valid_policy_tree includes a node of depth i-1 with the valid_policy anyPolicy, generate a child node with the following values: set the valid_policy to P-OID, set the qualifier_set to P-Q, and set the expected_policy_set to {P-OID}. */
1044 static bool policy_tree_add_if_any(policy_tree_t node
, void *ctx
) {
1045 struct policy_tree_add_ctx
*info
= (struct policy_tree_add_ctx
*)ctx
;
1046 if (oid_equal(node
->valid_policy
, oidAnyPolicy
)) {
1047 policy_tree_add_child(node
, &info
->p_oid
, info
->p_q
);
1053 /* Return true iff node has a child with a valid_policy equal to oid. */
1054 static bool policy_tree_has_child_with_oid(policy_tree_t node
,
1056 policy_tree_t child
;
1057 for (child
= node
->children
; child
; child
= child
->siblings
) {
1058 if (oid_equal(child
->valid_policy
, (*oid
))) {
1065 /* For each node in the valid_policy_tree of depth i-1, for each value in the expected_policy_set (including anyPolicy) that does not appear in a child node, create a child node with the following values: set the valid_policy to the value from the expected_policy_set in the parent node, set the qualifier_set to AP-Q, and set the expected_policy_set to the value in the valid_policy from this node. */
1066 static bool policy_tree_add_expected(policy_tree_t node
, void *ctx
) {
1067 policy_qualifier_t p_q
= (policy_qualifier_t
)ctx
;
1068 policy_set_t policy_set
;
1069 bool added_node
= false;
1070 for (policy_set
= node
->expected_policy_set
;
1072 policy_set
= policy_set
->oid_next
) {
1073 if (!policy_tree_has_child_with_oid(node
, &policy_set
->oid
)) {
1074 policy_tree_add_child(node
, &policy_set
->oid
, p_q
);
1081 /* For each node where ID-P is the valid_policy, set expected_policy_set to the set of subjectDomainPolicy values that are specified as equivalent to ID-P by the policy mappings extension. */
1082 static bool policy_tree_map_if_match(policy_tree_t node
, void *ctx
) {
1083 /* Can't map oidAnyPolicy. */
1084 if (oid_equal(node
->valid_policy
, oidAnyPolicy
))
1087 const SecCEPolicyMappings
*pm
= (const SecCEPolicyMappings
*)ctx
;
1088 size_t mapping_ix
, mapping_count
= pm
->numMappings
;
1089 policy_set_t policy_set
= NULL
;
1090 /* Generate the policy_set of sdps for matching idp */
1091 for (mapping_ix
= 0; mapping_ix
< mapping_count
; ++mapping_ix
) {
1092 const SecCEPolicyMapping
*mapping
= &pm
->mappings
[mapping_ix
];
1093 if (oid_equal(node
->valid_policy
, mapping
->issuerDomainPolicy
)) {
1094 policy_set_t p_node
= (policy_set_t
)malloc(sizeof(*policy_set
));
1095 p_node
->oid
= mapping
->subjectDomainPolicy
;
1096 p_node
->oid_next
= policy_set
? policy_set
: NULL
;
1097 policy_set
= p_node
;
1101 policy_tree_set_expected_policy(node
, policy_set
);
1107 /* If no node of depth i in the valid_policy_tree has a valid_policy of ID-P but there is a node of depth i with a valid_policy of anyPolicy, then generate a child node of the node of depth i-1 that has a valid_policy of anyPolicy as follows:
1108 (i) set the valid_policy to ID-P;
1109 (ii) set the qualifier_set to the qualifier set of the policy anyPolicy in the certificate policies extension of certificate i; and
1110 (iii) set the expected_policy_set to the set of subjectDomainPolicy values that are specified as equivalent to ID-P by the policy mappings extension. */
1111 static bool policy_tree_map_if_any(policy_tree_t node
, void *ctx
) {
1112 if (!oid_equal(node
->valid_policy
, oidAnyPolicy
)) {
1116 const SecCEPolicyMappings
*pm
= (const SecCEPolicyMappings
*)ctx
;
1117 size_t mapping_ix
, mapping_count
= pm
->numMappings
;
1118 CFMutableDictionaryRef mappings
= NULL
;
1119 CFDataRef idp
= NULL
;
1120 CFDataRef sdp
= NULL
;
1121 require_quiet(mappings
= CFDictionaryCreateMutable(NULL
, 0, &kCFTypeDictionaryKeyCallBacks
,
1122 &kCFTypeDictionaryValueCallBacks
),
1124 /* First we need to walk the mappings to generate the dictionary idp->sdps */
1125 for (mapping_ix
= 0; mapping_ix
< mapping_count
; mapping_ix
++) {
1126 oid_t issuerDomainPolicy
= pm
->mappings
[mapping_ix
].issuerDomainPolicy
;
1127 oid_t subjectDomainPolicy
= pm
->mappings
[mapping_ix
].subjectDomainPolicy
;
1128 idp
= CFDataCreateWithBytesNoCopy(NULL
, issuerDomainPolicy
.data
, issuerDomainPolicy
.length
, kCFAllocatorNull
);
1129 sdp
= CFDataCreateWithBytesNoCopy(NULL
, subjectDomainPolicy
.data
, subjectDomainPolicy
.length
, kCFAllocatorNull
);
1130 CFMutableArrayRef sdps
= (CFMutableArrayRef
)CFDictionaryGetValue(mappings
, idp
);
1132 CFArrayAppendValue(sdps
, sdp
);
1134 require_quiet(sdps
= CFArrayCreateMutable(kCFAllocatorDefault
, 0,
1135 &kCFTypeArrayCallBacks
), errOut
);
1136 CFArrayAppendValue(sdps
, sdp
);
1137 CFDictionarySetValue(mappings
, idp
, sdps
);
1144 /* Now we use the dictionary to generate the new nodes */
1145 CFDictionaryForEach(mappings
, ^(const void *key
, const void *value
) {
1146 CFDataRef idp
= key
;
1147 CFArrayRef sdps
= value
;
1149 /* (i) set the valid_policy to ID-P; */
1151 p_oid
.data
= (uint8_t *)CFDataGetBytePtr(idp
);
1152 p_oid
.length
= CFDataGetLength(idp
);
1154 /* (ii) set the qualifier_set to the qualifier set of the policy anyPolicy in the certificate policies extension of certificate i */
1155 policy_qualifier_t p_q
= node
->qualifier_set
;
1157 /* (iii) set the expected_policy_set to the set of subjectDomainPolicy values that are specified as equivalent to ID-P by the policy mappings extension. */
1158 __block policy_set_t p_expected
= NULL
;
1159 CFArrayForEach(sdps
, ^(const void *value
) {
1160 policy_set_t p_node
= (policy_set_t
)malloc(sizeof(*p_expected
));
1161 p_node
->oid
.data
= (void *)CFDataGetBytePtr(value
);
1162 p_node
->oid
.length
= CFDataGetLength(value
);
1163 p_node
->oid_next
= p_expected
? p_expected
: NULL
;
1164 p_expected
= p_node
;
1167 policy_tree_add_sibling(node
, &p_oid
, p_q
, p_expected
);
1169 CFReleaseNull(mappings
);
1173 CFReleaseNull(mappings
);
1179 static bool policy_tree_map_delete_if_match(policy_tree_t node
, void *ctx
) {
1180 /* Can't map oidAnyPolicy. */
1181 if (oid_equal(node
->valid_policy
, oidAnyPolicy
))
1184 const SecCEPolicyMappings
*pm
= (const SecCEPolicyMappings
*)ctx
;
1185 size_t mapping_ix
, mapping_count
= pm
->numMappings
;
1186 /* If this node matches any of the idps, delete it. */
1187 for (mapping_ix
= 0; mapping_ix
< mapping_count
; ++mapping_ix
) {
1188 const SecCEPolicyMapping
*mapping
= &pm
->mappings
[mapping_ix
];
1189 if (oid_equal(node
->valid_policy
, mapping
->issuerDomainPolicy
)) {
1190 policy_tree_remove_node(&node
);
1197 bool SecCertificatePathVCIsCertificateAtIndexSelfIssued(SecCertificatePathVCRef path
, CFIndex ix
) {
1198 /* The SecCertificatePath only tells us the last self-issued cert.
1199 * The chain may have more than one self-issued cert, so we need to
1200 * do the comparison. */
1201 bool result
= false;
1202 SecCertificateRef cert
= SecCertificatePathVCGetCertificateAtIndex(path
, ix
);
1203 CFDataRef issuer
= SecCertificateCopyNormalizedIssuerSequence(cert
);
1204 CFDataRef subject
= SecCertificateCopyNormalizedSubjectSequence(cert
);
1205 if (issuer
&& subject
&& CFEqual(issuer
, subject
)) {
1208 CFReleaseNull(issuer
);
1209 CFReleaseNull(subject
);
1214 kSecPolicyTreeVerificationUnknown
= 0,
1215 kSecPolicyTreeVerificationFalse
,
1216 kSecPolicyTreeVerificationTrue
,
1219 /* RFC 5280 policy tree processing */
1220 bool SecCertificatePathVCVerifyPolicyTree(SecCertificatePathVCRef path
, bool anchor_trusted
) {
1221 if (!path
) { return false; }
1222 if (path
->policy_tree_verification_result
!= kSecPolicyTreeVerificationUnknown
) {
1223 return (path
->policy_tree_verification_result
== kSecPolicyTreeVerificationTrue
);
1226 /* Path Validation initialization */
1227 bool result
= false;
1228 path
->policy_tree_verification_result
= kSecPolicyTreeVerificationFalse
;
1229 bool initial_policy_mapping_inhibit
= false;
1230 bool initial_explicit_policy
= false;
1231 bool initial_any_policy_inhibit
= false;
1233 SecCertificatePathVCPrunePolicyTree(path
);
1234 path
->policy_tree
= policy_tree_create(&oidAnyPolicy
, NULL
);
1236 assert((unsigned long)path
->count
<=UINT32_MAX
); /* Debug check. Correct as long as CFIndex is long */
1237 uint32_t n
= (uint32_t)path
->count
;
1238 if (anchor_trusted
) {
1242 uint32_t explicit_policy
= initial_explicit_policy
? 0 : n
+ 1;
1243 uint32_t inhibit_any_policy
= initial_any_policy_inhibit
? 0 : n
+ 1;
1244 uint32_t policy_mapping
= initial_policy_mapping_inhibit
? 0 : n
+ 1;
1246 SecCertificateRef cert
= NULL
;
1248 for (i
= 1; i
<= n
; ++i
) {
1250 cert
= SecCertificatePathVCGetCertificateAtIndex(path
, n
- i
);
1251 bool is_self_issued
= SecCertificatePathVCIsCertificateAtIndexSelfIssued(path
, n
- i
);
1254 if (path
->policy_tree
) {
1255 const SecCECertificatePolicies
*cp
=
1256 SecCertificateGetCertificatePolicies(cert
);
1257 size_t policy_ix
, policy_count
= cp
? cp
->numPolicies
: 0;
1258 for (policy_ix
= 0; policy_ix
< policy_count
; ++policy_ix
) {
1259 const SecCEPolicyInformation
*policy
= &cp
->policies
[policy_ix
];
1260 oid_t p_oid
= policy
->policyIdentifier
;
1261 policy_qualifier_t p_q
= &policy
->policyQualifiers
;
1262 struct policy_tree_add_ctx ctx
= { p_oid
, p_q
};
1263 if (!oid_equal(p_oid
, oidAnyPolicy
)) {
1264 if (!policy_tree_walk_depth(path
->policy_tree
, i
- 1,
1265 policy_tree_add_if_match
, &ctx
)) {
1266 policy_tree_walk_depth(path
->policy_tree
, i
- 1,
1267 policy_tree_add_if_any
, &ctx
);
1271 /* The certificate policies extension includes the policy
1272 anyPolicy with the qualifier set AP-Q and either
1273 (a) inhibit_anyPolicy is greater than 0 or
1274 (b) i < n and the certificate is self-issued. */
1275 if (inhibit_any_policy
> 0 || (i
< n
&& is_self_issued
)) {
1276 for (policy_ix
= 0; policy_ix
< policy_count
; ++policy_ix
) {
1277 const SecCEPolicyInformation
*policy
= &cp
->policies
[policy_ix
];
1278 oid_t p_oid
= policy
->policyIdentifier
;
1279 policy_qualifier_t p_q
= &policy
->policyQualifiers
;
1280 if (oid_equal(p_oid
, oidAnyPolicy
)) {
1281 policy_tree_walk_depth(path
->policy_tree
, i
- 1,
1282 policy_tree_add_expected
, (void *)p_q
);
1287 policy_tree_prune_childless(&path
->policy_tree
, i
- 1);
1290 SecCertificatePathVCPrunePolicyTree(path
);
1294 /* (f) Verify that either explicit_policy is greater than 0 or the
1295 valid_policy_tree is not equal to NULL. */
1296 if (!path
->policy_tree
&& explicit_policy
== 0) {
1297 /* valid_policy_tree is empty and explicit policy is 0, illegal. */
1298 secnotice("policy", "policy tree failure on cert %u", n
- i
);
1301 /* If Last Cert in Path */
1305 /* Prepare for Next Cert */
1306 /* (a) verify that anyPolicy does not appear as an
1307 issuerDomainPolicy or a subjectDomainPolicy */
1308 const SecCEPolicyMappings
*pm
= SecCertificateGetPolicyMappings(cert
);
1309 if (pm
&& pm
->present
) {
1310 size_t mapping_ix
, mapping_count
= pm
->numMappings
;
1311 for (mapping_ix
= 0; mapping_ix
< mapping_count
; ++mapping_ix
) {
1312 const SecCEPolicyMapping
*mapping
= &pm
->mappings
[mapping_ix
];
1313 if (oid_equal(mapping
->issuerDomainPolicy
, oidAnyPolicy
)
1314 || oid_equal(mapping
->subjectDomainPolicy
, oidAnyPolicy
)) {
1315 /* Policy mapping uses anyPolicy, illegal. */
1316 secnotice("policy", "policy mapping anyPolicy failure %u", n
- i
);
1322 /* (1) If the policy_mapping variable is greater than 0 */
1323 if (policy_mapping
> 0 && path
->policy_tree
) {
1324 if (!policy_tree_walk_depth(path
->policy_tree
, i
,
1325 policy_tree_map_if_match
, (void *)pm
)) {
1326 /* If no node of depth i in the valid_policy_tree has a valid_policy of ID-P but there is a node of depth i with a valid_policy of anyPolicy, then generate a child node of the node of depth i-1. */
1327 policy_tree_walk_depth(path
->policy_tree
, i
, policy_tree_map_if_any
, (void *)pm
);
1329 } else if (path
->policy_tree
) {
1330 /* (i) delete each node of depth i in the valid_policy_tree
1331 where ID-P is the valid_policy. */
1332 policy_tree_walk_depth(path
->policy_tree
, i
,
1333 policy_tree_map_delete_if_match
, (void *)pm
);
1334 /* (ii) If there is a node in the valid_policy_tree of depth
1335 i-1 or less without any child nodes, delete that
1336 node. Repeat this step until there are no nodes of
1337 depth i-1 or less without children. */
1338 policy_tree_prune_childless(&path
->policy_tree
, i
- 1);
1343 if (!is_self_issued
) {
1344 if (explicit_policy
)
1348 if (inhibit_any_policy
)
1349 inhibit_any_policy
--;
1352 const SecCEPolicyConstraints
*pc
=
1353 SecCertificateGetPolicyConstraints(cert
);
1355 if (pc
->requireExplicitPolicyPresent
1356 && pc
->requireExplicitPolicy
< explicit_policy
) {
1357 explicit_policy
= pc
->requireExplicitPolicy
;
1359 if (pc
->inhibitPolicyMappingPresent
1360 && pc
->inhibitPolicyMapping
< policy_mapping
) {
1361 policy_mapping
= pc
->inhibitPolicyMapping
;
1365 const SecCEInhibitAnyPolicy
*iap
= SecCertificateGetInhibitAnyPolicySkipCerts(cert
);
1366 if (iap
&& iap
->skipCerts
< inhibit_any_policy
) {
1367 inhibit_any_policy
= iap
->skipCerts
;
1370 } /* end of path for loop */
1373 cert
= SecCertificatePathVCGetCertificateAtIndex(path
, 0);
1375 if (explicit_policy
)
1378 const SecCEPolicyConstraints
*pc
= SecCertificateGetPolicyConstraints(cert
);
1380 if (pc
->requireExplicitPolicyPresent
1381 && pc
->requireExplicitPolicy
== 0) {
1382 explicit_policy
= 0;
1386 /* (g) Calculate the intersection of the valid_policy_tree and the user-initial-policy-set, as follows */
1388 if (path
->policy_tree
) {
1389 #if !defined(NDEBUG)
1390 policy_tree_dump(path
->policy_tree
);
1393 //policy_tree_prune_childless(&pvc->valid_policy_tree, n - 1);
1396 /* If either (1) the value of explicit_policy variable is greater than
1397 zero or (2) the valid_policy_tree is not NULL, then path processing
1399 if (!path
->policy_tree
&& explicit_policy
== 0) {
1400 /* valid_policy_tree is empty and explicit policy is 0, illegal. */
1401 secnotice("policy", "policy tree failure on leaf");
1405 path
->policy_tree_verification_result
= kSecPolicyTreeVerificationTrue
;