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 <securityd/policytree.h>
47 #include <securityd/SecPolicyServer.h>
48 #include <securityd/SecCertificateServer.h>
49 #include <securityd/SecRevocationServer.h>
52 // MARK: SecCertificateVC
53 /********************************************************
54 ************* SecCertificateVC object ***************
55 ********************************************************/
57 struct SecCertificateVC
{
59 SecCertificateRef certificate
;
60 CFArrayRef usageConstraints
;
63 bool require_revocation_response
;
65 CFGiblisWithHashFor(SecCertificateVC
)
67 static void SecCertificateVCDestroy(CFTypeRef cf
) {
68 SecCertificateVCRef cvc
= (SecCertificateVCRef
) cf
;
69 CFReleaseNull(cvc
->certificate
);
70 CFReleaseNull(cvc
->usageConstraints
);
73 static Boolean
SecCertificateVCCompare(CFTypeRef cf1
, CFTypeRef cf2
) {
74 SecCertificateVCRef cv1
= (SecCertificateVCRef
) cf1
;
75 SecCertificateVCRef cv2
= (SecCertificateVCRef
) cf2
;
76 if (!CFEqual(cv1
->certificate
, cv2
->certificate
)) {
79 /* CertificateVCs are the same if either does not have usage constraints. */
80 if (cv1
->usageConstraints
&& cv2
->usageConstraints
&&
81 !CFEqual(cv1
->usageConstraints
, cv2
->usageConstraints
)) {
88 static CFHashCode
SecCertificateVCHash(CFTypeRef cf
) {
89 SecCertificateVCRef cvc
= (SecCertificateVCRef
) cf
;
90 CFHashCode hashCode
= 0;
91 hashCode
+= CFHash(cvc
->certificate
);
92 if (cvc
->usageConstraints
) {
93 hashCode
+= CFHash(cvc
->usageConstraints
);
98 static CFStringRef
SecCertificateVCCopyFormatDescription(CFTypeRef cf
, CFDictionaryRef formatOptions
) {
99 SecCertificateVCRef cvc
= (SecCertificateVCRef
)cf
;
100 return CFCopyDescription(cvc
->certificate
);
103 static bool SecCertificateVCCouldBeEV(SecCertificateRef certificate
) {
104 CFMutableDictionaryRef keySizes
= NULL
;
105 CFNumberRef rsaSize
= NULL
, ecSize
= NULL
;
108 /* 3. Subscriber Certificate. */
110 /* (a) certificate Policies */
111 const SecCECertificatePolicies
*cp
;
112 cp
= SecCertificateGetCertificatePolicies(certificate
);
113 require_quiet(cp
&& cp
->numPolicies
> 0, notEV
);
114 /* Now find at least one policy in here that has a qualifierID of id-qt 2
115 and a policyQualifier that is a URI to the CPS and an EV policy OID. */
117 bool found_ev_anchor_for_leaf_policy
= false;
118 for (ix
= 0; ix
< cp
->numPolicies
; ++ix
) {
119 if (SecPolicyIsEVPolicy(&cp
->policies
[ix
].policyIdentifier
)) {
120 found_ev_anchor_for_leaf_policy
= true;
123 require_quiet(found_ev_anchor_for_leaf_policy
, notEV
);
125 /* (b) cRLDistributionPoint
126 (c) authorityInformationAccess
127 BRv1.3.4: MUST be present with OCSP Responder unless stapled response.
130 /* (d) basicConstraints
131 If present, the cA field MUST be set false. */
132 const SecCEBasicConstraints
*bc
= SecCertificateGetBasicConstraints(certificate
);
134 require_action_quiet(bc
->isCA
== false, notEV
,
135 secnotice("ev", "Leaf has invalid basic constraints"));
139 SecKeyUsage ku
= SecCertificateGetKeyUsage(certificate
);
141 require_action_quiet((ku
& (kSecKeyUsageKeyCertSign
| kSecKeyUsageCRLSign
)) == 0, notEV
,
142 secnotice("ev", "Leaf has invalid key usage %u", ku
));
146 /* The EV Cert Spec errata specifies this, though this is a check for SSL
147 not specifically EV. */
151 Either the value id-kp-serverAuth [RFC5280] or id-kp-clientAuth [RFC5280] or both values MUST be present. Other values SHOULD NOT be present. */
152 SecCertificateCopyExtendedKeyUsage(certificate
);
155 /* 6.1.5 Key Sizes */
156 CFAbsoluteTime jan2014
= 410227200;
157 require_quiet(ecSize
= CFNumberCreateWithCFIndex(NULL
, 256), notEV
);
158 require_quiet(keySizes
= CFDictionaryCreateMutable(NULL
, 2, &kCFTypeDictionaryKeyCallBacks
,
159 &kCFTypeDictionaryValueCallBacks
), notEV
);
160 CFDictionaryAddValue(keySizes
, kSecAttrKeyTypeEC
, ecSize
);
161 if (SecCertificateNotValidBefore(certificate
) < jan2014
) {
162 /* At least RSA 1024 or ECC NIST P-256. */
163 require_quiet(rsaSize
= CFNumberCreateWithCFIndex(NULL
, 1024), notEV
);
164 CFDictionaryAddValue(keySizes
, kSecAttrKeyTypeRSA
, rsaSize
);
165 require_action_quiet(SecCertificateIsAtLeastMinKeySize(certificate
, keySizes
), notEV
,
166 secnotice("ev", "Leaf's public key is too small for issuance before 2014"));
168 /* At least RSA 2028 or ECC NIST P-256. */
169 require_quiet(rsaSize
= CFNumberCreateWithCFIndex(NULL
, 2048), notEV
);
170 CFDictionaryAddValue(keySizes
, kSecAttrKeyTypeRSA
, rsaSize
);
171 require_action_quiet(SecCertificateIsAtLeastMinKeySize(certificate
, keySizes
), notEV
,
172 secnotice("ev", "Leaf's public key is too small for issuance after 2013"));
175 /* 6.3.2 Validity Periods */
176 CFAbsoluteTime jul2016
= 489024000;
177 CFAbsoluteTime notAfter
= SecCertificateNotValidAfter(certificate
);
178 CFAbsoluteTime notBefore
= SecCertificateNotValidBefore(certificate
);
179 if (SecCertificateNotValidBefore(certificate
) < jul2016
) {
180 /* Validity Period no greater than 60 months.
181 60 months is no more than 5 years and 2 leap days. */
182 CFAbsoluteTime maxPeriod
= 60*60*24*(365*5+2);
183 require_action_quiet(notAfter
- notBefore
<= maxPeriod
, notEV
,
184 secnotice("ev", "Leaf's validity period is more than 60 months"));
186 /* Validity Period no greater than 39 months.
187 39 months is no more than 3 years, 2 31-day months,
188 1 30-day month, and 1 leap day */
189 CFAbsoluteTime maxPeriod
= 60*60*24*(365*3+2*31+30+1);
190 require_action_quiet(notAfter
- notBefore
<= maxPeriod
, notEV
,
191 secnotice("ev", "Leaf has validity period longer than 39 months and issued after 30 June 2016"));
194 /* 7.1.3 Algorithm Object Identifiers */
195 CFAbsoluteTime jan2016
= 473299200;
196 if (SecCertificateNotValidBefore(certificate
) > jan2016
) {
198 require_action_quiet(SecCertificateGetSignatureHashAlgorithm(certificate
) > kSecSignatureHashAlgorithmSHA1
,
199 notEV
, secnotice("ev", "Leaf was issued with SHA-1 after 2015"));
205 CFReleaseNull(rsaSize
);
206 CFReleaseNull(ecSize
);
207 CFReleaseNull(keySizes
);
212 SecCertificateVCRef
SecCertificateVCCreate(SecCertificateRef certificate
, CFArrayRef usageConstraints
) {
213 if (!certificate
) { return NULL
; }
214 CFIndex size
= sizeof(struct SecCertificateVC
);
215 SecCertificateVCRef result
=
216 (SecCertificateVCRef
)_CFRuntimeCreateInstance(kCFAllocatorDefault
,
217 SecCertificateVCGetTypeID(), size
- sizeof(CFRuntimeBase
), 0);
220 result
->certificate
= CFRetainSafe(certificate
);
221 result
->isWeakHash
= SecCertificateIsWeakHash(certificate
);
222 result
->optionallyEV
= SecCertificateVCCouldBeEV(certificate
);
224 CFArrayRef emptyArray
= NULL
;
225 if (!usageConstraints
) {
226 require_action_quiet(emptyArray
= CFArrayCreate(kCFAllocatorDefault
, NULL
, 0, &kCFTypeArrayCallBacks
), exit
, CFReleaseNull(result
));
227 usageConstraints
= emptyArray
;
229 result
->usageConstraints
= CFRetainSafe(usageConstraints
);
231 CFReleaseNull(emptyArray
);
236 // MARK: SecCertificatePathVC
237 /********************************************************
238 ************* SecCertificatePathVC object ***************
239 ********************************************************/
240 struct SecCertificatePathVC
{
244 /* Index of next parent source to search for parents. */
245 CFIndex nextParentSource
;
247 /* Index of last certificate in chain whose signature has been verified.
248 0 means nothing has been checked. 1 means the leaf has been verified
249 against its issuer, etc. */
250 CFIndex lastVerifiedSigner
;
252 /* Index of first self issued certificate in the chain. -1 mean there is
253 none. 0 means the leaf is self signed. */
256 /* True iff cert at index selfIssued does in fact self verify. */
259 /* True if the root of this path is an anchor. Trustedness of the
260 * anchor is determined by the PVC. */
263 policy_tree_t policy_tree
;
264 uint8_t policy_tree_verification_result
;
269 bool hasStrongHashes
;
274 /* This is the score of the path after determining acceptance. */
279 /* Enumerated value to determine whether CT is required for the leaf
280 * certificate (because a CA in the path has a require-ct constraint).
281 * If non-zero, CT is required; value indicates overridable status. */
282 SecPathCTPolicy requiresCT
;
284 SecCertificateVCRef certificates
[];
286 CFGiblisWithHashFor(SecCertificatePathVC
)
288 static void SecCertificatePathVCPrunePolicyTree(SecCertificatePathVCRef certificatePath
) {
289 if (certificatePath
->policy_tree
) {
290 policy_tree_prune(&certificatePath
->policy_tree
);
294 static void SecCertificatePathVCDeleteRVCs(SecCertificatePathVCRef path
) {
296 CFIndex certIX
, certCount
= path
->rvcCount
;
297 for (certIX
= 0; certIX
< certCount
; ++certIX
) {
298 SecRVCRef rvc
= &((SecRVCRef
)path
->rvcs
)[certIX
];
306 static void SecCertificatePathVCDestroy(CFTypeRef cf
) {
307 SecCertificatePathVCRef certificatePath
= (SecCertificatePathVCRef
) cf
;
309 SecCertificatePathVCDeleteRVCs(certificatePath
);
310 SecCertificatePathVCPrunePolicyTree(certificatePath
);
311 for (ix
= 0; ix
< certificatePath
->count
; ++ix
) {
312 CFReleaseNull(certificatePath
->certificates
[ix
]);
316 static Boolean
SecCertificatePathVCCompare(CFTypeRef cf1
, CFTypeRef cf2
) {
317 SecCertificatePathVCRef cp1
= (SecCertificatePathVCRef
) cf1
;
318 SecCertificatePathVCRef cp2
= (SecCertificatePathVCRef
) cf2
;
319 if (cp1
->count
!= cp2
->count
)
322 for (ix
= 0; ix
< cp1
->count
; ++ix
) {
323 if (!CFEqual(cp1
->certificates
[ix
], cp2
->certificates
[ix
]))
330 static CFHashCode
SecCertificatePathVCHash(CFTypeRef cf
) {
331 SecCertificatePathVCRef certificatePath
= (SecCertificatePathVCRef
) cf
;
332 CFHashCode hashCode
= 0;
334 for (ix
= 0; ix
< certificatePath
->count
; ++ix
) {
335 hashCode
+= CFHash(certificatePath
->certificates
[ix
]);
340 static CFStringRef
SecCertificatePathVCCopyFormatDescription(CFTypeRef cf
, CFDictionaryRef formatOptions
) {
341 SecCertificatePathVCRef certificatePath
= (SecCertificatePathVCRef
) cf
;
342 CFMutableStringRef desc
= CFStringCreateMutable(kCFAllocatorDefault
, 0);
343 CFStringRef typeStr
= CFCopyTypeIDDescription(CFGetTypeID(cf
));
344 CFStringAppendFormat(desc
, NULL
,
345 CFSTR("<%@ certs: "), typeStr
);
348 for (ix
= 0; ix
< certificatePath
->count
; ++ix
) {
350 CFStringAppend(desc
, CFSTR(", "));
352 CFStringRef str
= CFCopyDescription(certificatePath
->certificates
[ix
]);
353 CFStringAppend(desc
, str
);
356 CFStringAppend(desc
, CFSTR(" >"));
361 /* Create a new certificate path from an old one. */
362 SecCertificatePathVCRef
SecCertificatePathVCCreate(SecCertificatePathVCRef path
,
363 SecCertificateRef certificate
, CFArrayRef usageConstraints
) {
364 CFAllocatorRef allocator
= kCFAllocatorDefault
;
367 CFIndex selfIssued
, lastVerifiedSigner
;
370 count
= path
->count
+ 1;
371 lastVerifiedSigner
= path
->lastVerifiedSigner
;
372 selfIssued
= path
->selfIssued
;
373 isSelfSigned
= path
->isSelfSigned
;
376 lastVerifiedSigner
= 0;
378 isSelfSigned
= false;
381 CFIndex size
= sizeof(struct SecCertificatePathVC
) +
382 count
* sizeof(SecCertificateRef
);
383 SecCertificatePathVCRef result
=
384 (SecCertificatePathVCRef
)_CFRuntimeCreateInstance(allocator
,
385 SecCertificatePathVCGetTypeID(), size
- sizeof(CFRuntimeBase
), 0);
389 memset((char*)result
+ sizeof(result
->_base
), 0,
390 sizeof(*result
) - sizeof(result
->_base
));
392 result
->count
= count
;
393 result
->lastVerifiedSigner
= lastVerifiedSigner
;
394 result
->selfIssued
= selfIssued
;
395 result
->isSelfSigned
= isSelfSigned
;
397 for (ix
= 0; ix
< count
- 1; ++ix
) {
398 result
->certificates
[ix
] = path
->certificates
[ix
];
399 CFRetain(result
->certificates
[ix
]);
402 SecCertificateVCRef cvc
= SecCertificateVCCreate(certificate
, usageConstraints
);
403 result
->certificates
[count
- 1] = cvc
;
408 SecCertificatePathVCRef
SecCertificatePathVCCopyFromParent(
409 SecCertificatePathVCRef path
, CFIndex skipCount
) {
410 CFAllocatorRef allocator
= kCFAllocatorDefault
;
412 CFIndex selfIssued
, lastVerifiedSigner
;
415 /* Ensure we are at least returning a path of length 1. */
416 if (skipCount
< 0 || path
->count
< 1 + skipCount
)
419 count
= path
->count
- skipCount
;
420 lastVerifiedSigner
= path
->lastVerifiedSigner
> skipCount
421 ? path
->lastVerifiedSigner
- skipCount
: 0;
422 selfIssued
= path
->selfIssued
>= skipCount
423 ? path
->selfIssued
- skipCount
: -1;
424 isSelfSigned
= path
->selfIssued
>= 0 ? path
->isSelfSigned
: false;
426 CFIndex size
= sizeof(struct SecCertificatePathVC
) +
427 count
* sizeof(SecCertificateRef
);
428 SecCertificatePathVCRef result
=
429 (SecCertificatePathVCRef
)_CFRuntimeCreateInstance(allocator
,
430 SecCertificatePathVCGetTypeID(), size
- sizeof(CFRuntimeBase
), 0);
434 memset((char*)result
+ sizeof(result
->_base
), 0,
435 sizeof(*result
) - sizeof(result
->_base
));
437 result
->count
= count
;
438 result
->lastVerifiedSigner
= lastVerifiedSigner
;
439 result
->selfIssued
= selfIssued
;
440 result
->isSelfSigned
= isSelfSigned
;
441 result
->isAnchored
= path
->isAnchored
;
443 for (ix
= 0; ix
< count
; ++ix
) {
444 CFIndex pathIX
= ix
+ skipCount
;
445 result
->certificates
[ix
] = path
->certificates
[pathIX
];
446 CFRetain(result
->certificates
[ix
]);
452 SecCertificatePathVCRef
SecCertificatePathVCCopyAddingLeaf(SecCertificatePathVCRef path
,
453 SecCertificateRef leaf
) {
454 CFAllocatorRef allocator
= kCFAllocatorDefault
;
456 CFIndex selfIssued
, lastVerifiedSigner
;
459 /* First make sure the new leaf is signed by path's current leaf. */
460 SecKeyRef issuerKey
= SecCertificatePathVCCopyPublicKeyAtIndex(path
, 0);
463 OSStatus status
= SecCertificateIsSignedBy(leaf
, issuerKey
);
464 CFRelease(issuerKey
);
468 count
= path
->count
+ 1;
469 lastVerifiedSigner
= path
->lastVerifiedSigner
+ 1;
470 selfIssued
= path
->selfIssued
;
471 isSelfSigned
= path
->isSelfSigned
;
473 CFIndex size
= sizeof(struct SecCertificatePathVC
) +
474 count
* sizeof(SecCertificateRef
);
475 SecCertificatePathVCRef result
=
476 (SecCertificatePathVCRef
)_CFRuntimeCreateInstance(allocator
,
477 SecCertificatePathVCGetTypeID(), size
- sizeof(CFRuntimeBase
), 0);
481 memset((char*)result
+ sizeof(result
->_base
), 0,
482 sizeof(*result
) - sizeof(result
->_base
));
484 result
->count
= count
;
485 result
->lastVerifiedSigner
= lastVerifiedSigner
;
486 result
->selfIssued
= selfIssued
;
487 result
->isSelfSigned
= isSelfSigned
;
488 result
->isAnchored
= path
->isAnchored
;
491 for (ix
= 1; ix
< count
; ++ix
) {
492 result
->certificates
[ix
] = path
->certificates
[ix
- 1];
493 CFRetain(result
->certificates
[ix
]);
495 SecCertificateVCRef leafVC
= SecCertificateVCCreate(leaf
, NULL
);
496 result
->certificates
[0] = leafVC
;
501 CFArrayRef
SecCertificatePathVCCopyCertificates(SecCertificatePathVCRef path
) {
502 CFMutableArrayRef outCerts
= NULL
;
503 size_t count
= path
->count
;
504 require_quiet(outCerts
= CFArrayCreateMutable(NULL
, count
, &kCFTypeArrayCallBacks
), exit
);
505 SecCertificatePathVCForEachCertificate(path
, ^(SecCertificateRef cert
, bool * __unused stop
) {
506 CFArrayAppendValue(outCerts
, cert
);
512 CFArrayRef
SecCertificatePathVCCreateSerialized(SecCertificatePathVCRef path
) {
513 CFMutableArrayRef serializedCerts
= NULL
;
514 require_quiet(path
, exit
);
515 size_t count
= path
->count
;
516 require_quiet(serializedCerts
= CFArrayCreateMutable(NULL
, count
, &kCFTypeArrayCallBacks
), exit
);
517 SecCertificatePathVCForEachCertificate(path
, ^(SecCertificateRef cert
, bool * __unused stop
) {
518 CFDataRef certData
= SecCertificateCopyData(cert
);
520 CFArrayAppendValue(serializedCerts
, certData
);
525 return serializedCerts
;
529 /* Record the fact that we found our own root cert as our parent
531 void SecCertificatePathVCSetSelfIssued(
532 SecCertificatePathVCRef certificatePath
) {
533 if (certificatePath
->selfIssued
>= 0) {
534 secdebug("trust", "%@ is already issued at %" PRIdCFIndex
, certificatePath
,
535 certificatePath
->selfIssued
);
538 secdebug("trust", "%@ is self issued", certificatePath
);
539 certificatePath
->selfIssued
= certificatePath
->count
- 1;
541 /* now check that the selfIssued cert was actually self-signed */
542 if (certificatePath
->selfIssued
>= 0 && !certificatePath
->isSelfSigned
) {
543 SecCertificateVCRef certVC
= certificatePath
->certificates
[certificatePath
->selfIssued
];
544 Boolean isSelfSigned
= false;
545 OSStatus status
= SecCertificateIsSelfSigned(certVC
->certificate
, &isSelfSigned
);
546 if ((status
== errSecSuccess
) && isSelfSigned
) {
547 certificatePath
->isSelfSigned
= true;
549 certificatePath
->selfIssued
= -1;
554 void SecCertificatePathVCSetIsAnchored(
555 SecCertificatePathVCRef certificatePath
) {
556 secdebug("trust", "%@ is anchored", certificatePath
);
557 certificatePath
->isAnchored
= true;
559 /* Now check if that anchor (last cert) was actually self-signed.
560 * In the non-anchor case, this is handled by SecCertificatePathVCSetSelfIssued.
561 * Because anchored chains immediately go into the candidate bucket in the trust
562 * server, we need to ensure that the self-signed/self-issued members are set
563 * for the purposes of scoring. */
564 if (!certificatePath
->isSelfSigned
&& certificatePath
->count
> 0) {
565 SecCertificateVCRef certVC
= certificatePath
->certificates
[certificatePath
->count
- 1];
566 Boolean isSelfSigned
= false;
567 OSStatus status
= SecCertificateIsSelfSigned(certVC
->certificate
, &isSelfSigned
);
568 if ((status
== errSecSuccess
) && isSelfSigned
) {
569 certificatePath
->isSelfSigned
= true;
570 if (certificatePath
->selfIssued
== -1) {
571 certificatePath
->selfIssued
= certificatePath
->count
- 1;
577 /* Return the index of the first non anchor certificate in the chain that is
578 self signed counting from the leaf up. Return -1 if there is none. */
579 CFIndex
SecCertificatePathVCSelfSignedIndex(
580 SecCertificatePathVCRef certificatePath
) {
581 if (certificatePath
->isSelfSigned
)
582 return certificatePath
->selfIssued
;
586 Boolean
SecCertificatePathVCIsAnchored(
587 SecCertificatePathVCRef certificatePath
) {
588 return certificatePath
->isAnchored
;
592 void SecCertificatePathVCSetNextSourceIndex(
593 SecCertificatePathVCRef certificatePath
, CFIndex sourceIndex
) {
594 certificatePath
->nextParentSource
= sourceIndex
;
597 CFIndex
SecCertificatePathVCGetNextSourceIndex(
598 SecCertificatePathVCRef certificatePath
) {
599 return certificatePath
->nextParentSource
;
602 CFIndex
SecCertificatePathVCGetCount(
603 SecCertificatePathVCRef certificatePath
) {
604 check(certificatePath
);
605 return certificatePath
? certificatePath
->count
: 0;
608 SecCertificateRef
SecCertificatePathVCGetCertificateAtIndex(
609 SecCertificatePathVCRef certificatePath
, CFIndex ix
) {
610 if (!certificatePath
|| ix
< 0 || ix
>= certificatePath
->count
) {
613 SecCertificateVCRef cvc
= certificatePath
->certificates
[ix
];
614 return cvc
? cvc
->certificate
: NULL
;
617 void SecCertificatePathVCForEachCertificate(SecCertificatePathVCRef path
, void(^operation
)(SecCertificateRef certificate
, bool *stop
)) {
619 CFIndex ix
, count
= path
->count
;
620 for (ix
= 0; ix
< count
; ++ix
) {
621 SecCertificateVCRef cvc
= path
->certificates
[ix
];
622 operation(cvc
->certificate
, &stop
);
627 CFIndex
SecCertificatePathVCGetIndexOfCertificate(SecCertificatePathVCRef path
,
628 SecCertificateRef certificate
) {
629 CFIndex ix
, count
= path
->count
;
630 for (ix
= 0; ix
< count
; ++ix
) {
631 SecCertificateVCRef cvc
= path
->certificates
[ix
];
632 if (CFEqual(cvc
->certificate
, certificate
))
638 /* Return the root certificate for certificatePath. Note that root is just
639 the top of the path as far as it is constructed. It may or may not be
640 trusted or self signed. */
641 SecCertificateRef
SecCertificatePathVCGetRoot(
642 SecCertificatePathVCRef certificatePath
) {
643 return SecCertificatePathVCGetCertificateAtIndex(certificatePath
,
644 SecCertificatePathVCGetCount(certificatePath
) - 1);
647 SecKeyRef
SecCertificatePathVCCopyPublicKeyAtIndex(
648 SecCertificatePathVCRef certificatePath
, CFIndex ix
) {
649 SecCertificateRef certificate
=
650 SecCertificatePathVCGetCertificateAtIndex(certificatePath
, ix
);
652 return SecCertificateCopyPublicKey_ios(certificate
);
654 return SecCertificateCopyPublicKey(certificate
);
658 CFArrayRef
SecCertificatePathVCGetUsageConstraintsAtIndex(
659 SecCertificatePathVCRef certificatePath
, CFIndex ix
) {
660 SecCertificateVCRef cvc
= certificatePath
->certificates
[ix
];
661 return cvc
->usageConstraints
;
664 void SecCertificatePathVCSetUsageConstraintsAtIndex(SecCertificatePathVCRef certificatePath
,
665 CFArrayRef newConstraints
, CFIndex ix
) {
666 CFArrayRef emptyArray
= NULL
;
667 if (!newConstraints
) {
668 require_quiet(emptyArray
= CFArrayCreate(kCFAllocatorDefault
, NULL
, 0, &kCFTypeArrayCallBacks
), exit
);
669 newConstraints
= emptyArray
;
672 SecCertificateVCRef cvc
= certificatePath
->certificates
[ix
];
673 cvc
->usageConstraints
= CFRetainSafe(newConstraints
);
675 CFReleaseNull(emptyArray
);
679 SecPathVerifyStatus
SecCertificatePathVCVerify(SecCertificatePathVCRef certificatePath
) {
680 check(certificatePath
);
681 if (!certificatePath
)
682 return kSecPathVerifyFailed
;
684 certificatePath
->lastVerifiedSigner
< certificatePath
->count
- 1;
685 ++certificatePath
->lastVerifiedSigner
) {
686 SecKeyRef issuerKey
=
687 SecCertificatePathVCCopyPublicKeyAtIndex(certificatePath
,
688 certificatePath
->lastVerifiedSigner
+ 1);
690 return kSecPathVerifiesUnknown
;
691 SecCertificateVCRef cvc
= certificatePath
->certificates
[certificatePath
->lastVerifiedSigner
];
692 OSStatus status
= SecCertificateIsSignedBy(cvc
->certificate
,
694 CFRelease(issuerKey
);
696 return kSecPathVerifyFailed
;
700 return kSecPathVerifySuccess
;
703 /* Is the the issuer of the last cert a subject of a previous cert in the chain.See <rdar://33136765>. */
704 bool SecCertificatePathVCIsCycleInGraph(SecCertificatePathVCRef path
) {
705 bool isCircle
= false;
706 CFDataRef issuer
= SecCertificateGetNormalizedIssuerContent(SecCertificatePathVCGetRoot(path
));
707 if (!issuer
) { return isCircle
; }
708 CFIndex ix
= path
->count
- 2;
709 for (; ix
>= 0; ix
--) {
710 SecCertificateVCRef cvc
= path
->certificates
[ix
];
711 CFDataRef subject
= SecCertificateGetNormalizedSubjectContent(cvc
->certificate
);
712 if (subject
&& CFEqual(issuer
, subject
)) {
720 bool SecCertificatePathVCIsValid(SecCertificatePathVCRef certificatePath
, CFAbsoluteTime verifyTime
) {
721 __block
bool result
= true;
722 SecCertificatePathVCForEachCertificate(certificatePath
, ^(SecCertificateRef certificate
, bool *stop
) {
723 if (!SecCertificateIsValid(certificate
, verifyTime
)) {
730 bool SecCertificatePathVCHasWeakHash(SecCertificatePathVCRef certificatePath
) {
731 CFIndex ix
, count
= certificatePath
->count
;
733 if (certificatePath
->hasStrongHashes
) {
737 if (SecCertificatePathVCIsAnchored(certificatePath
)) {
738 /* For anchored paths, don't check the hash algorithm of the anchored cert,
739 * since we already decided to trust it. */
742 for (ix
= 0; ix
< count
; ++ix
) {
743 if (certificatePath
->certificates
[ix
]->isWeakHash
) {
747 certificatePath
->hasStrongHashes
= true;
751 bool SecCertificatePathVCHasWeakKeySize(SecCertificatePathVCRef certificatePath
) {
752 __block CFDictionaryRef keySizes
= NULL
;
753 CFNumberRef rsaSize
= NULL
, ecSize
= NULL
;
754 __block
bool result
= false;
756 /* RSA key sizes are 2048-bit or larger. EC key sizes are P-224 or larger. */
757 require(rsaSize
= CFNumberCreateWithCFIndex(NULL
, 2048), errOut
);
758 require(ecSize
= CFNumberCreateWithCFIndex(NULL
, 224), errOut
);
759 const void *keys
[] = { kSecAttrKeyTypeRSA
, kSecAttrKeyTypeEC
};
760 const void *values
[] = { rsaSize
, ecSize
};
761 require(keySizes
= CFDictionaryCreate(NULL
, keys
, values
, 2,
762 &kCFTypeDictionaryKeyCallBacks
, &kCFTypeDictionaryValueCallBacks
), errOut
);
763 SecCertificatePathVCForEachCertificate(certificatePath
, ^(SecCertificateRef certificate
, bool *stop
) {
764 if (!SecCertificateIsAtLeastMinKeySize(certificate
, keySizes
)) {
771 CFReleaseSafe(keySizes
);
772 CFReleaseSafe(rsaSize
);
773 CFReleaseSafe(ecSize
);
777 /* Return a score for this certificate chain. */
778 CFIndex
SecCertificatePathVCScore(SecCertificatePathVCRef certificatePath
, CFAbsoluteTime verifyTime
) {
781 /* Paths that don't verify score terribly.c */
782 if (certificatePath
->lastVerifiedSigner
!= certificatePath
->count
- 1) {
783 secdebug("trust", "lvs: %" PRIdCFIndex
" count: %" PRIdCFIndex
,
784 certificatePath
->lastVerifiedSigner
, certificatePath
->count
);
788 if (certificatePath
->isAnchored
) {
789 /* Anchored paths for the win! */
793 if (certificatePath
->isSelfSigned
&& (certificatePath
->selfIssued
== certificatePath
->count
- 1)) {
794 /* Chains that terminate in a self-signed certificate are preferred,
795 even if they don't end in an anchor. */
797 /* Shorter chains ending in a self-signed cert are preferred. */
798 score
-= 1 * certificatePath
->count
;
800 /* Longer chains are preferred when the chain doesn't end in a self-signed cert. */
801 score
+= 1 * certificatePath
->count
;
804 if (SecCertificatePathVCIsValid(certificatePath
, verifyTime
)) {
808 if (!SecCertificatePathVCHasWeakHash(certificatePath
)) {
812 if (!SecCertificatePathVCHasWeakKeySize(certificatePath
)) {
819 CFIndex
SecCertificatePathVCGetScore(SecCertificatePathVCRef certificatePath
) {
820 if (!certificatePath
) { return 0; }
821 return certificatePath
->score
;
824 void SecCertificatePathVCSetScore(SecCertificatePathVCRef certificatePath
, CFIndex score
) {
825 /* We may "score" the same path twice -- if we "accept" a path but then
826 * decide to keep looking for a better one, we we process the same path
827 * again in "reject" which creates a lower score. Don't replace a higher
828 * score with a lower score. Use reset below to post-reject a path. */
829 if (score
> certificatePath
->score
) {
830 certificatePath
->score
= score
;
834 void SecCertificatePathVCResetScore(SecCertificatePathVCRef certificatePath
) {
835 certificatePath
->score
= 0;
838 void *SecCertificatePathVCGetRVCAtIndex(SecCertificatePathVCRef certificatePath
, CFIndex ix
) {
839 if (ix
>= certificatePath
->rvcCount
) {
842 return &((SecRVCRef
)certificatePath
->rvcs
)[ix
];
845 bool SecCertificatePathVCIsRevocationDone(SecCertificatePathVCRef certificatePath
) {
846 return (bool)certificatePath
->rvcs
;
849 void SecCertificatePathVCAllocateRVCs(SecCertificatePathVCRef certificatePath
, CFIndex certCount
) {
850 certificatePath
->rvcs
= calloc(sizeof(struct OpaqueSecRVC
), certCount
);
851 certificatePath
->rvcCount
= certCount
;
854 /* Return 0 if any certs revocation checking failed, or the earliest date on
855 which one of the used revocation validation tokens (ocsp response or
857 /* This function returns 0 to indicate revocation checking was not completed
858 for this certificate chain, otherwise returns the date at which the first
859 piece of revocation checking info we used expires. */
860 CFAbsoluteTime
SecCertificatePathVCGetEarliestNextUpdate(SecCertificatePathVCRef path
) {
861 CFIndex certIX
, certCount
= path
->count
;
862 CFAbsoluteTime enu
= NULL_TIME
;
863 if (certCount
<= 1 || !path
->rvcs
) {
867 for (certIX
= 0; certIX
< path
->rvcCount
; ++certIX
) {
868 SecRVCRef rvc
= &((SecRVCRef
)path
->rvcs
)[certIX
];
869 CFAbsoluteTime thisCertNextUpdate
= SecRVCGetEarliestNextUpdate(rvc
);
870 if (thisCertNextUpdate
== 0) {
872 /* We allow for CA certs to not be revocation checked if they
873 have no ocspResponders nor CRLDPs to check against, but the leaf
874 must be checked in order for us to claim we did revocation
876 SecCertificateRef cert
= SecCertificatePathVCGetCertificateAtIndex(path
, rvc
->certIX
);
877 CFArrayRef ocspResponders
= NULL
;
878 ocspResponders
= SecCertificateGetOCSPResponders(cert
);
880 CFArrayRef crlDPs
= NULL
;
881 crlDPs
= SecCertificateGetCRLDistributionPoints(cert
);
883 if ((!ocspResponders
|| CFArrayGetCount(ocspResponders
) == 0)
885 && (!crlDPs
|| CFArrayGetCount(crlDPs
) == 0)
888 /* We can't check this cert so we don't consider it a soft
889 failure that we didn't. Ideally we should support crl
890 checking and remove this workaround, since that more
895 /* Make sure to always skip roots for whom we can't check revocation */
896 if (certIX
== certCount
- 1) {
899 secdebug("rvc", "revocation checking soft failure for cert: %ld",
901 enu
= thisCertNextUpdate
;
904 if (enu
== 0 || thisCertNextUpdate
< enu
) {
905 enu
= thisCertNextUpdate
;
909 secdebug("rvc", "revocation valid until: %lg", enu
);
913 bool SecCertificatePathVCIsRevocationRequiredForCertificateAtIndex(SecCertificatePathVCRef certificatePath
,
915 if (ix
> certificatePath
->count
- 1) { return false; }
916 SecCertificateVCRef cvc
= certificatePath
->certificates
[ix
];
917 return cvc
->require_revocation_response
;
920 void SecCertificatePathVCSetRevocationRequiredForCertificateAtIndex(SecCertificatePathVCRef certificatePath
,
922 if (ix
> certificatePath
->count
- 1) { return; }
923 SecCertificateVCRef cvc
= certificatePath
->certificates
[ix
];
924 cvc
->require_revocation_response
= true;
927 bool SecCertificatePathVCIsPathValidated(SecCertificatePathVCRef certificatePath
) {
928 if (!certificatePath
) { return false; }
929 return certificatePath
->pathValidated
;
932 void SecCertificatePathVCSetPathValidated(SecCertificatePathVCRef certificatePath
) {
933 certificatePath
->pathValidated
= true;
936 bool SecCertificatePathVCIsEV(SecCertificatePathVCRef certificatePath
) {
937 if (!certificatePath
) { return false; }
938 return certificatePath
->isEV
;
941 void SecCertificatePathVCSetIsEV(SecCertificatePathVCRef certificatePath
, bool isEV
) {
942 certificatePath
->isEV
= isEV
;
945 bool SecCertificatePathVCIsOptionallyEV(SecCertificatePathVCRef certificatePath
) {
946 if (!certificatePath
) { return false; }
947 return certificatePath
->certificates
[0]->optionallyEV
;
950 bool SecCertificatePathVCIsCT(SecCertificatePathVCRef certificatePath
) {
951 if (!certificatePath
) { return false; }
952 return certificatePath
->isCT
;
955 void SecCertificatePathVCSetIsCT(SecCertificatePathVCRef certificatePath
, bool isCT
) {
956 certificatePath
->isCT
= isCT
;
959 SecPathCTPolicy
SecCertificatePathVCRequiresCT(SecCertificatePathVCRef certificatePath
) {
960 if (!certificatePath
) { return kSecPathCTNotRequired
; }
961 return certificatePath
->requiresCT
;
964 void SecCertificatePathVCSetRequiresCT(SecCertificatePathVCRef certificatePath
, SecPathCTPolicy requiresCT
) {
965 if (certificatePath
->requiresCT
> requiresCT
) {
966 return; /* once set, CT policy may be only be changed to a more strict value */
968 certificatePath
->requiresCT
= requiresCT
;
971 bool SecCertificatePathVCIsAllowlisted(SecCertificatePathVCRef certificatePath
) {
972 if (!certificatePath
) { return false; }
973 return certificatePath
->is_allowlisted
;
976 void SecCertificatePathVCSetIsAllowlisted(SecCertificatePathVCRef certificatePath
, bool isAllowlisted
) {
977 certificatePath
->is_allowlisted
= isAllowlisted
;
980 /* MARK: policy_tree path verification */
981 struct policy_tree_add_ctx
{
983 policy_qualifier_t p_q
;
986 /* 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}. */
987 static bool policy_tree_add_if_match(policy_tree_t node
, void *ctx
) {
988 struct policy_tree_add_ctx
*info
= (struct policy_tree_add_ctx
*)ctx
;
989 policy_set_t policy_set
;
990 for (policy_set
= node
->expected_policy_set
;
992 policy_set
= policy_set
->oid_next
) {
993 if (oid_equal(policy_set
->oid
, info
->p_oid
)) {
994 policy_tree_add_child(node
, &info
->p_oid
, info
->p_q
);
1001 /* 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}. */
1002 static bool policy_tree_add_if_any(policy_tree_t node
, void *ctx
) {
1003 struct policy_tree_add_ctx
*info
= (struct policy_tree_add_ctx
*)ctx
;
1004 if (oid_equal(node
->valid_policy
, oidAnyPolicy
)) {
1005 policy_tree_add_child(node
, &info
->p_oid
, info
->p_q
);
1011 /* Return true iff node has a child with a valid_policy equal to oid. */
1012 static bool policy_tree_has_child_with_oid(policy_tree_t node
,
1014 policy_tree_t child
;
1015 for (child
= node
->children
; child
; child
= child
->siblings
) {
1016 if (oid_equal(child
->valid_policy
, (*oid
))) {
1023 /* 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. */
1024 static bool policy_tree_add_expected(policy_tree_t node
, void *ctx
) {
1025 policy_qualifier_t p_q
= (policy_qualifier_t
)ctx
;
1026 policy_set_t policy_set
;
1027 bool added_node
= false;
1028 for (policy_set
= node
->expected_policy_set
;
1030 policy_set
= policy_set
->oid_next
) {
1031 if (!policy_tree_has_child_with_oid(node
, &policy_set
->oid
)) {
1032 policy_tree_add_child(node
, &policy_set
->oid
, p_q
);
1039 /* 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. */
1040 static bool policy_tree_map_if_match(policy_tree_t node
, void *ctx
) {
1041 /* Can't map oidAnyPolicy. */
1042 if (oid_equal(node
->valid_policy
, oidAnyPolicy
))
1045 const SecCEPolicyMappings
*pm
= (const SecCEPolicyMappings
*)ctx
;
1046 size_t mapping_ix
, mapping_count
= pm
->numMappings
;
1047 policy_set_t policy_set
= NULL
;
1048 /* Generate the policy_set of sdps for matching idp */
1049 for (mapping_ix
= 0; mapping_ix
< mapping_count
; ++mapping_ix
) {
1050 const SecCEPolicyMapping
*mapping
= &pm
->mappings
[mapping_ix
];
1051 if (oid_equal(node
->valid_policy
, mapping
->issuerDomainPolicy
)) {
1052 policy_set_t p_node
= (policy_set_t
)malloc(sizeof(*policy_set
));
1053 p_node
->oid
= mapping
->subjectDomainPolicy
;
1054 p_node
->oid_next
= policy_set
? policy_set
: NULL
;
1055 policy_set
= p_node
;
1059 policy_tree_set_expected_policy(node
, policy_set
);
1065 /* 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:
1066 (i) set the valid_policy to ID-P;
1067 (ii) set the qualifier_set to the qualifier set of the policy anyPolicy in the certificate policies extension of certificate i; and
1068 (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. */
1069 static bool policy_tree_map_if_any(policy_tree_t node
, void *ctx
) {
1070 if (!oid_equal(node
->valid_policy
, oidAnyPolicy
)) {
1074 const SecCEPolicyMappings
*pm
= (const SecCEPolicyMappings
*)ctx
;
1075 size_t mapping_ix
, mapping_count
= pm
->numMappings
;
1076 CFMutableDictionaryRef mappings
= NULL
;
1077 CFDataRef idp
= NULL
;
1078 CFDataRef sdp
= NULL
;
1079 require_quiet(mappings
= CFDictionaryCreateMutable(NULL
, 0, &kCFTypeDictionaryKeyCallBacks
,
1080 &kCFTypeDictionaryValueCallBacks
),
1082 /* First we need to walk the mappings to generate the dictionary idp->sdps */
1083 for (mapping_ix
= 0; mapping_ix
< mapping_count
; mapping_ix
++) {
1084 oid_t issuerDomainPolicy
= pm
->mappings
[mapping_ix
].issuerDomainPolicy
;
1085 oid_t subjectDomainPolicy
= pm
->mappings
[mapping_ix
].subjectDomainPolicy
;
1086 idp
= CFDataCreateWithBytesNoCopy(NULL
, issuerDomainPolicy
.data
, issuerDomainPolicy
.length
, kCFAllocatorNull
);
1087 sdp
= CFDataCreateWithBytesNoCopy(NULL
, subjectDomainPolicy
.data
, subjectDomainPolicy
.length
, kCFAllocatorNull
);
1088 CFMutableArrayRef sdps
= (CFMutableArrayRef
)CFDictionaryGetValue(mappings
, idp
);
1090 CFArrayAppendValue(sdps
, sdp
);
1092 require_quiet(sdps
= CFArrayCreateMutable(kCFAllocatorDefault
, 0,
1093 &kCFTypeArrayCallBacks
), errOut
);
1094 CFArrayAppendValue(sdps
, sdp
);
1095 CFDictionarySetValue(mappings
, idp
, sdps
);
1102 /* Now we use the dictionary to generate the new nodes */
1103 CFDictionaryForEach(mappings
, ^(const void *key
, const void *value
) {
1104 CFDataRef idp
= key
;
1105 CFArrayRef sdps
= value
;
1107 /* (i) set the valid_policy to ID-P; */
1109 p_oid
.data
= (uint8_t *)CFDataGetBytePtr(idp
);
1110 p_oid
.length
= CFDataGetLength(idp
);
1112 /* (ii) set the qualifier_set to the qualifier set of the policy anyPolicy in the certificate policies extension of certificate i */
1113 policy_qualifier_t p_q
= node
->qualifier_set
;
1115 /* (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. */
1116 __block policy_set_t p_expected
= NULL
;
1117 CFArrayForEach(sdps
, ^(const void *value
) {
1118 policy_set_t p_node
= (policy_set_t
)malloc(sizeof(*p_expected
));
1119 p_node
->oid
.data
= (void *)CFDataGetBytePtr(value
);
1120 p_node
->oid
.length
= CFDataGetLength(value
);
1121 p_node
->oid_next
= p_expected
? p_expected
: NULL
;
1122 p_expected
= p_node
;
1125 policy_tree_add_sibling(node
, &p_oid
, p_q
, p_expected
);
1127 CFReleaseNull(mappings
);
1131 CFReleaseNull(mappings
);
1137 static bool policy_tree_map_delete_if_match(policy_tree_t node
, void *ctx
) {
1138 /* Can't map oidAnyPolicy. */
1139 if (oid_equal(node
->valid_policy
, oidAnyPolicy
))
1142 const SecCEPolicyMappings
*pm
= (const SecCEPolicyMappings
*)ctx
;
1143 size_t mapping_ix
, mapping_count
= pm
->numMappings
;
1144 /* If this node matches any of the idps, delete it. */
1145 for (mapping_ix
= 0; mapping_ix
< mapping_count
; ++mapping_ix
) {
1146 const SecCEPolicyMapping
*mapping
= &pm
->mappings
[mapping_ix
];
1147 if (oid_equal(node
->valid_policy
, mapping
->issuerDomainPolicy
)) {
1148 policy_tree_remove_node(&node
);
1155 bool SecCertificatePathVCIsCertificateAtIndexSelfIssued(SecCertificatePathVCRef path
, CFIndex ix
) {
1156 /* The SecCertificatePath only tells us the last self-issued cert.
1157 * The chain may have more than one self-issued cert, so we need to
1158 * do the comparison. */
1159 bool result
= false;
1160 SecCertificateRef cert
= SecCertificatePathVCGetCertificateAtIndex(path
, ix
);
1161 CFDataRef issuer
= SecCertificateCopyNormalizedIssuerSequence(cert
);
1162 CFDataRef subject
= SecCertificateCopyNormalizedSubjectSequence(cert
);
1163 if (issuer
&& subject
&& CFEqual(issuer
, subject
)) {
1166 CFReleaseNull(issuer
);
1167 CFReleaseNull(subject
);
1172 kSecPolicyTreeVerificationUnknown
= 0,
1173 kSecPolicyTreeVerificationFalse
,
1174 kSecPolicyTreeVerificationTrue
,
1177 /* RFC 5280 policy tree processing */
1178 bool SecCertificatePathVCVerifyPolicyTree(SecCertificatePathVCRef path
, bool anchor_trusted
) {
1179 if (!path
) { return false; }
1180 if (path
->policy_tree_verification_result
!= kSecPolicyTreeVerificationUnknown
) {
1181 return (path
->policy_tree_verification_result
== kSecPolicyTreeVerificationTrue
);
1184 /* Path Validation initialization */
1185 bool result
= false;
1186 path
->policy_tree_verification_result
= kSecPolicyTreeVerificationFalse
;
1187 bool initial_policy_mapping_inhibit
= false;
1188 bool initial_explicit_policy
= false;
1189 bool initial_any_policy_inhibit
= false;
1191 SecCertificatePathVCPrunePolicyTree(path
);
1192 path
->policy_tree
= policy_tree_create(&oidAnyPolicy
, NULL
);
1194 assert((unsigned long)path
->count
<=UINT32_MAX
); /* Debug check. Correct as long as CFIndex is long */
1195 uint32_t n
= (uint32_t)path
->count
;
1196 if (anchor_trusted
) {
1200 uint32_t explicit_policy
= initial_explicit_policy
? 0 : n
+ 1;
1201 uint32_t inhibit_any_policy
= initial_any_policy_inhibit
? 0 : n
+ 1;
1202 uint32_t policy_mapping
= initial_policy_mapping_inhibit
? 0 : n
+ 1;
1204 SecCertificateRef cert
= NULL
;
1206 for (i
= 1; i
<= n
; ++i
) {
1208 cert
= SecCertificatePathVCGetCertificateAtIndex(path
, n
- i
);
1209 bool is_self_issued
= SecCertificatePathVCIsCertificateAtIndexSelfIssued(path
, n
- i
);
1212 if (path
->policy_tree
) {
1213 const SecCECertificatePolicies
*cp
=
1214 SecCertificateGetCertificatePolicies(cert
);
1215 size_t policy_ix
, policy_count
= cp
? cp
->numPolicies
: 0;
1216 for (policy_ix
= 0; policy_ix
< policy_count
; ++policy_ix
) {
1217 const SecCEPolicyInformation
*policy
= &cp
->policies
[policy_ix
];
1218 oid_t p_oid
= policy
->policyIdentifier
;
1219 policy_qualifier_t p_q
= &policy
->policyQualifiers
;
1220 struct policy_tree_add_ctx ctx
= { p_oid
, p_q
};
1221 if (!oid_equal(p_oid
, oidAnyPolicy
)) {
1222 if (!policy_tree_walk_depth(path
->policy_tree
, i
- 1,
1223 policy_tree_add_if_match
, &ctx
)) {
1224 policy_tree_walk_depth(path
->policy_tree
, i
- 1,
1225 policy_tree_add_if_any
, &ctx
);
1229 /* The certificate policies extension includes the policy
1230 anyPolicy with the qualifier set AP-Q and either
1231 (a) inhibit_anyPolicy is greater than 0 or
1232 (b) i < n and the certificate is self-issued. */
1233 if (inhibit_any_policy
> 0 || (i
< n
&& is_self_issued
)) {
1234 for (policy_ix
= 0; policy_ix
< policy_count
; ++policy_ix
) {
1235 const SecCEPolicyInformation
*policy
= &cp
->policies
[policy_ix
];
1236 oid_t p_oid
= policy
->policyIdentifier
;
1237 policy_qualifier_t p_q
= &policy
->policyQualifiers
;
1238 if (oid_equal(p_oid
, oidAnyPolicy
)) {
1239 policy_tree_walk_depth(path
->policy_tree
, i
- 1,
1240 policy_tree_add_expected
, (void *)p_q
);
1245 policy_tree_prune_childless(&path
->policy_tree
, i
- 1);
1248 SecCertificatePathVCPrunePolicyTree(path
);
1252 /* (f) Verify that either explicit_policy is greater than 0 or the
1253 valid_policy_tree is not equal to NULL. */
1254 if (!path
->policy_tree
&& explicit_policy
== 0) {
1255 /* valid_policy_tree is empty and explicit policy is 0, illegal. */
1256 secnotice("policy", "policy tree failure on cert %u", n
- i
);
1259 /* If Last Cert in Path */
1263 /* Prepare for Next Cert */
1264 /* (a) verify that anyPolicy does not appear as an
1265 issuerDomainPolicy or a subjectDomainPolicy */
1266 const SecCEPolicyMappings
*pm
= SecCertificateGetPolicyMappings(cert
);
1267 if (pm
&& pm
->present
) {
1268 size_t mapping_ix
, mapping_count
= pm
->numMappings
;
1269 for (mapping_ix
= 0; mapping_ix
< mapping_count
; ++mapping_ix
) {
1270 const SecCEPolicyMapping
*mapping
= &pm
->mappings
[mapping_ix
];
1271 if (oid_equal(mapping
->issuerDomainPolicy
, oidAnyPolicy
)
1272 || oid_equal(mapping
->subjectDomainPolicy
, oidAnyPolicy
)) {
1273 /* Policy mapping uses anyPolicy, illegal. */
1274 secnotice("policy", "policy mapping anyPolicy failure %u", n
- i
);
1280 /* (1) If the policy_mapping variable is greater than 0 */
1281 if (policy_mapping
> 0 && path
->policy_tree
) {
1282 if (!policy_tree_walk_depth(path
->policy_tree
, i
,
1283 policy_tree_map_if_match
, (void *)pm
)) {
1284 /* 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. */
1285 policy_tree_walk_depth(path
->policy_tree
, i
, policy_tree_map_if_any
, (void *)pm
);
1287 } else if (path
->policy_tree
) {
1288 /* (i) delete each node of depth i in the valid_policy_tree
1289 where ID-P is the valid_policy. */
1290 policy_tree_walk_depth(path
->policy_tree
, i
,
1291 policy_tree_map_delete_if_match
, (void *)pm
);
1292 /* (ii) If there is a node in the valid_policy_tree of depth
1293 i-1 or less without any child nodes, delete that
1294 node. Repeat this step until there are no nodes of
1295 depth i-1 or less without children. */
1296 policy_tree_prune_childless(&path
->policy_tree
, i
- 1);
1301 if (!is_self_issued
) {
1302 if (explicit_policy
)
1306 if (inhibit_any_policy
)
1307 inhibit_any_policy
--;
1310 const SecCEPolicyConstraints
*pc
=
1311 SecCertificateGetPolicyConstraints(cert
);
1313 if (pc
->requireExplicitPolicyPresent
1314 && pc
->requireExplicitPolicy
< explicit_policy
) {
1315 explicit_policy
= pc
->requireExplicitPolicy
;
1317 if (pc
->inhibitPolicyMappingPresent
1318 && pc
->inhibitPolicyMapping
< policy_mapping
) {
1319 policy_mapping
= pc
->inhibitPolicyMapping
;
1323 const SecCEInhibitAnyPolicy
*iap
= SecCertificateGetInhibitAnyPolicySkipCerts(cert
);
1324 if (iap
&& iap
->skipCerts
< inhibit_any_policy
) {
1325 inhibit_any_policy
= iap
->skipCerts
;
1328 } /* end of path for loop */
1331 cert
= SecCertificatePathVCGetCertificateAtIndex(path
, 0);
1333 if (explicit_policy
)
1336 const SecCEPolicyConstraints
*pc
= SecCertificateGetPolicyConstraints(cert
);
1338 if (pc
->requireExplicitPolicyPresent
1339 && pc
->requireExplicitPolicy
== 0) {
1340 explicit_policy
= 0;
1344 /* (g) Calculate the intersection of the valid_policy_tree and the user-initial-policy-set, as follows */
1346 if (path
->policy_tree
) {
1347 #if !defined(NDEBUG)
1348 policy_tree_dump(path
->policy_tree
);
1351 //policy_tree_prune_childless(&pvc->valid_policy_tree, n - 1);
1354 /* If either (1) the value of explicit_policy variable is greater than
1355 zero or (2) the valid_policy_tree is not NULL, then path processing
1357 if (!path
->policy_tree
&& explicit_policy
== 0) {
1358 /* valid_policy_tree is empty and explicit policy is 0, illegal. */
1359 secnotice("policy", "policy tree failure on leaf");
1363 path
->policy_tree_verification_result
= kSecPolicyTreeVerificationTrue
;