2 * Copyright (c) 2000-2001,2011-2014 Apple Inc. All Rights Reserved.
4 * The contents of this file constitute Original Code as defined in and are
5 * subject to the Apple Public Source License Version 1.2 (the 'License').
6 * You may not use this file except in compliance with the License. Please obtain
7 * a copy of the License at http://www.apple.com/publicsource and read it before
10 * This Original Code and all software distributed under the License are
11 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESS
12 * OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES, INCLUDING WITHOUT
13 * LIMITATION, ANY WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
14 * PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT. Please see the License for the
15 * specific language governing rights and limitations under the License.
20 * tpCertGroup.cpp - Cert group functions (construct, verify)
23 #include "AppleTPSession.h"
24 #include "certGroupUtils.h"
25 #include "TPCertInfo.h"
26 #include "TPCrlInfo.h"
27 #include "tpPolicies.h"
28 #include "tpdebugging.h"
29 #include "tpCrlVerify.h"
30 #include <Security/oidsalg.h>
31 #include <Security/cssmapple.h>
34 * This is a temporary hack to allow verification of PKINIT server certs
35 * which are self-signed and not in the system anchors list. If the self-
36 * signed cert is in a magic keychain (whose location is not published),
37 * we'll allow it as if it were indeed a full-fledged anchor cert.
39 #define TP_PKINIT_SERVER_HACK 1
40 #if TP_PKINIT_SERVER_HACK
42 #include <Security/SecKeychain.h>
43 #include <Security/SecKeychainSearch.h>
44 #include <Security/SecCertificate.h>
45 #include <Security/oidscert.h>
46 #include <sys/types.h>
49 #define CFRELEASE(cf) if(cf) { CFRelease(cf); }
52 * Returns true if we are to allow/trust the specified
53 * cert as a PKINIT-only anchor.
55 static bool tpCheckPkinitServerCert(
56 TPCertGroup
&certGroup
)
59 * Basic requirement: exactly one cert, self-signed.
60 * The numCerts == 1 requirement might change...
62 unsigned numCerts
= certGroup
.numCerts();
64 tpDebug("tpCheckPkinitServerCert: too many certs");
68 TPCertInfo
*theCert
= certGroup
.certAtIndex(numCerts
- 1);
69 if(!theCert
->isSelfSigned()) {
70 tpDebug("tpCheckPkinitServerCert: 1 cert, not self-signed");
73 const CSSM_DATA
*subjectName
= theCert
->subjectName();
76 * Open the magic keychain.
77 * We're going up and over the Sec layer here, not generally
78 * kosher, but this is a hack.
81 SecKeychainRef kcRef
= NULL
;
83 const char *homeDir
= getenv("HOME");
86 // If $HOME is unset get the current user's home directory
87 // from the passwd file.
88 uid_t uid
= geteuid();
89 if (!uid
) uid
= getuid();
90 struct passwd
*pw
= getpwuid(uid
);
96 fullPathName
= homeDir
;
97 fullPathName
+= "/Library/Application Support/PKINIT/TrustedServers.keychain";
98 ortn
= SecKeychainOpen(fullPathName
.c_str(), &kcRef
);
100 tpDebug("tpCheckPkinitServerCert: keychain not found (1)");
103 /* subsequent errors to errOut: */
106 SecKeychainStatus kcStatus
;
107 CSSM_DATA_PTR subjSerial
= NULL
;
109 SecKeychainSearchRef srchRef
= NULL
;
110 SecKeychainAttributeList attrList
;
111 SecKeychainAttribute attrs
[2];
112 SecKeychainItemRef foundItem
= NULL
;
114 ortn
= SecKeychainGetStatus(kcRef
, &kcStatus
);
116 tpDebug("tpCheckPkinitServerCert: keychain not found (2)");
121 * We already have this cert's normalized name; get its
124 crtn
= theCert
->fetchField(&CSSMOID_X509V1SerialNumber
, &subjSerial
);
126 /* should never happen */
127 tpDebug("tpCheckPkinitServerCert: error fetching serial number");
131 attrs
[0].tag
= kSecSubjectItemAttr
;
132 attrs
[0].length
= (UInt32
)subjectName
->Length
;
133 attrs
[0].data
= subjectName
->Data
;
134 attrs
[1].tag
= kSecSerialNumberItemAttr
;
135 attrs
[1].length
= (UInt32
)subjSerial
->Length
;
136 attrs
[1].data
= subjSerial
->Data
;
138 attrList
.attr
= attrs
;
140 ortn
= SecKeychainSearchCreateFromAttributes(kcRef
,
141 kSecCertificateItemClass
,
145 tpDebug("tpCheckPkinitServerCert: search failure");
149 ortn
= SecKeychainSearchCopyNext(srchRef
, &foundItem
);
151 tpDebug("tpCheckPkinitServerCert: end search");
155 /* found a matching cert; do byte-for-byte compare */
157 ortn
= SecCertificateGetData((SecCertificateRef
)foundItem
, &certData
);
159 tpDebug("tpCheckPkinitServerCert: SecCertificateGetData failure");
162 if(tpCompareCssmData(&certData
, theCert
->itemData())){
163 tpDebug("tpCheckPkinitServerCert: FOUND CERT");
167 tpDebug("tpCheckPkinitServerCert: skipping matching cert");
168 CFRelease(foundItem
);
174 CFRELEASE(foundItem
);
175 if(subjSerial
!= NULL
) {
176 theCert
->freeField(&CSSMOID_X509V1SerialNumber
, subjSerial
);
180 #endif /* TP_PKINIT_SERVER_HACK */
182 /*-----------------------------------------------------------------------------
186 * This function returns a pointer to a mallocd CSSM_CERTGROUP which
187 * refers to a mallocd list of raw ordered X.509 certs which verify back as
188 * far as the TP is able to go. The first cert of the returned list is the
189 * subject cert. The TP will attempt to search thru the DBs passed in
190 * DBList in order to complete the chain. The chain is completed when a
191 * self-signed (root) cert is found in the chain. The root cert may be
192 * present in the input CertGroupFrag, or it may have been obtained from
193 * one of the DBs passed in DBList. It is not an error if no root cert is
196 * The error conditions are:
197 * -- The first cert of CertGroupFrag is an invalid cert. NULL is returned,
198 * err = CSSM_TP_INVALID_CERTIFICATE.
199 * -- The root cert (if found) fails to verify. Valid certgroup is returned,
200 * err = CSSMERR_TP_VERIFICATION_FAILURE.
201 * -- Any cert in the (possibly partially) constructed chain has expired or
202 * isn't valid yet, err = CSSMERR_TP_CERT_EXPIRED or
203 * CSSMERR_TP_CERT_NOT_VALID_YET. A CertGroup is returned.
204 * -- CSSMERR_TP_CERT_EXPIRED and CSSMERR_TP_CERT_NOT_VALID_YET. If one of these
205 * conditions obtains for the first (leaf) cert, the function throws this
206 * error immediately and the outgoing cert group is empty. For subsequent certs,
207 * the temporal validity of a cert is only tested AFTER a cert successfully
208 * meets the cert chaining criteria (subject/issuer match and signature
209 * verify). A cert in a chain with this error is not added to the outgoing
211 * -- the usual errors like bad handle or memory failure.
214 * Two handles - to an open CL and CSP. The CSP must be capable of
215 * dealing with the signature algorithms used by the certs. The CL must be
218 * CertGroupFrag, an unordered array of raw X.509 certs in the form of a
219 * CSSM_CERTGROUP_PTR. The first cert of this list is the subject cert
220 * which is eventually to be verified. The other certs can be in any order
221 * and may not even have any relevance to the cert chain being constructed.
222 * They may also be invalid certs.
224 * DBList, a list of DB/DL handles which may contain certs necessary to
225 * complete the desired cert chain. (Not currently implemented.)
227 *---------------------------------------------------------------------------*/
230 void AppleTPSession::CertGroupConstruct(CSSM_CL_HANDLE clHand
,
231 CSSM_CSP_HANDLE cspHand
,
232 const CSSM_DL_DB_LIST
&DBList
,
233 const void *ConstructParams
,
234 const CSSM_CERTGROUP
&CertGroupFrag
,
235 CSSM_CERTGROUP_PTR
&CertGroup
)
237 TPCertGroup
outCertGroup(*this, TGO_Caller
);
238 TPCertGroup
inCertGroup(CertGroupFrag
,
243 true, // firstCertMustBeValid
246 /* set up for disposal of TPCertInfos created by CertGroupConstructPriv */
247 TPCertGroup
gatheredCerts(*this, TGO_Group
);
249 CSSM_RETURN constructReturn
= CSSM_OK
;
250 CSSM_APPLE_TP_ACTION_FLAGS actionFlags
= 0;
251 CSSM_BOOL verifiedToRoot
; // not used
252 CSSM_BOOL verifiedToAnchor
; // not used
253 CSSM_BOOL verifiedViaTrustSetting
; // not used
256 CertGroupConstructPriv(clHand
,
269 verifiedViaTrustSetting
,
272 catch(const CssmError
&cerr
) {
273 constructReturn
= cerr
.error
;
274 /* abort if no certs found */
275 if(outCertGroup
.numCerts() == 0) {
276 CssmError::throwMe(constructReturn
);
279 CertGroup
= outCertGroup
.buildCssmCertGroup();
280 /* caller of this function never gets evidence... */
281 outCertGroup
.freeDbRecords();
283 if(constructReturn
) {
284 CssmError::throwMe(constructReturn
);
290 * Private version of CertGroupConstruct, used by CertGroupConstruct and
291 * CertGroupVerify. Populates a TP-style TPCertGroup for further processing.
292 * This only throws CSSM-style exceptions in the following cases:
294 * -- input parameter errors
295 * -- the first (leaf) cert is bad (doesn't parse, expired, not valid yet).
296 * -- root found but it doesn't self-verify
298 * All other cert-related errors simply result in the bad cert being ignored.
299 * Other exceptions are gross system errors like malloc failure.
301 void AppleTPSession::CertGroupConstructPriv(CSSM_CL_HANDLE clHand
,
302 CSSM_CSP_HANDLE cspHand
,
303 TPCertGroup
&inCertGroup
,
304 const CSSM_DL_DB_LIST
*DBList
, // optional here
305 const char *cssmTimeStr
, // optional
307 /* trusted anchors, optional */
308 /* FIXME - maybe this should be a TPCertGroup */
309 uint32 numAnchorCerts
,
310 const CSSM_DATA
*anchorCerts
,
312 /* CSSM_TP_ACTION_FETCH_CERT_FROM_NET, CSSM_TP_ACTION_TRUST_SETTINGS */
313 CSSM_APPLE_TP_ACTION_FLAGS actionFlags
,
315 /* optional user trust parameters */
316 const CSSM_OID
*policyOid
,
317 const char *policyStr
,
319 SecTrustSettingsKeyUsage keyUse
,
322 * Certs to be freed by caller (i.e., TPCertInfo which we allocate
323 * as a result of using a cert from anchorCerts or dbList) are added
326 TPCertGroup
&certsToBeFreed
,
329 CSSM_BOOL
&verifiedToRoot
, // end of chain self-verifies
330 CSSM_BOOL
&verifiedToAnchor
, // end of chain in anchors
331 CSSM_BOOL
&verifiedViaTrustSetting
, // chain ends per User Trust setting
332 TPCertGroup
&outCertGroup
) // RETURNED
334 TPCertInfo
*subjectCert
; // the one we're working on
335 CSSM_RETURN outErr
= CSSM_OK
;
337 /* this'll be the first subject cert in the main loop */
338 subjectCert
= inCertGroup
.certAtIndex(0);
340 /* Append leaf cert to outCertGroup */
341 outCertGroup
.appendCert(subjectCert
);
342 subjectCert
->isLeaf(true);
343 subjectCert
->isFromInputCerts(true);
344 outCertGroup
.setAllUnused();
345 subjectCert
->used(true);
347 outErr
= outCertGroup
.buildCertGroup(
357 &certsToBeFreed
, // gatheredCerts to accumulate net/DB fetches
358 CSSM_TRUE
, // subjectIsInGroup - enables root check on
368 verifiedViaTrustSetting
);
370 CssmError::throwMe(outErr
);
375 * Map a policy OID to one of the standard (non-revocation) policies.
376 * Returns true if it's a standard policy.
378 static bool checkPolicyOid(
380 TPPolicy
&tpPolicy
) /* RETURNED */
382 if(tpCompareOids(&oid
, &CSSMOID_APPLE_TP_SSL
)) {
386 else if(tpCompareOids(&oid
, &CSSMOID_APPLE_X509_BASIC
)) {
387 tpPolicy
= kTPx509Basic
;
390 else if(tpCompareOids(&oid
, &CSSMOID_APPLE_TP_SMIME
)) {
391 tpPolicy
= kTP_SMIME
;
394 else if(tpCompareOids(&oid
, &CSSMOID_APPLE_TP_EAP
)) {
398 else if(tpCompareOids(&oid
, &CSSMOID_APPLE_TP_SW_UPDATE_SIGNING
)) {
399 /* note: this was CSSMOID_APPLE_TP_CODE_SIGN until 8/15/06 */
400 tpPolicy
= kTP_SWUpdateSign
;
403 else if(tpCompareOids(&oid
, &CSSMOID_APPLE_TP_RESOURCE_SIGN
)) {
404 tpPolicy
= kTP_ResourceSign
;
407 else if(tpCompareOids(&oid
, &CSSMOID_APPLE_TP_IP_SEC
)) {
408 tpPolicy
= kTP_IPSec
;
411 else if(tpCompareOids(&oid
, &CSSMOID_APPLE_TP_ICHAT
)) {
412 tpPolicy
= kTP_iChat
;
415 else if(tpCompareOids(&oid
, &CSSMOID_APPLE_ISIGN
)) {
419 else if(tpCompareOids(&oid
, &CSSMOID_APPLE_TP_PKINIT_CLIENT
)) {
420 tpPolicy
= kTP_PKINIT_Client
;
423 else if(tpCompareOids(&oid
, &CSSMOID_APPLE_TP_PKINIT_SERVER
)) {
424 tpPolicy
= kTP_PKINIT_Server
;
427 else if(tpCompareOids(&oid
, &CSSMOID_APPLE_TP_CODE_SIGNING
)) {
428 tpPolicy
= kTP_CodeSigning
;
431 else if(tpCompareOids(&oid
, &CSSMOID_APPLE_TP_PACKAGE_SIGNING
)) {
432 tpPolicy
= kTP_PackageSigning
;
435 else if(tpCompareOids(&oid
, &CSSMOID_APPLE_TP_MACAPPSTORE_RECEIPT
)) {
436 tpPolicy
= kTP_MacAppStoreRec
;
439 else if(tpCompareOids(&oid
, &CSSMOID_APPLE_TP_APPLEID_SHARING
)) {
440 tpPolicy
= kTP_AppleIDSharing
;
443 else if(tpCompareOids(&oid
, &CSSMOID_APPLE_TP_TIMESTAMPING
)) {
444 tpPolicy
= kTP_TimeStamping
;
447 else if(tpCompareOids(&oid
, &CSSMOID_APPLE_TP_PASSBOOK_SIGNING
)) {
448 tpPolicy
= kTP_PassbookSigning
;
451 else if(tpCompareOids(&oid
, &CSSMOID_APPLE_TP_MOBILE_STORE
)) {
452 tpPolicy
= kTP_MobileStore
;
455 else if(tpCompareOids(&oid
, &CSSMOID_APPLE_TP_TEST_MOBILE_STORE
)) {
456 tpPolicy
= kTP_TestMobileStore
;
459 else if(tpCompareOids(&oid
, &CSSMOID_APPLE_TP_ESCROW_SERVICE
)) {
460 tpPolicy
= kTP_EscrowService
;
463 else if(tpCompareOids(&oid
, &CSSMOID_APPLE_TP_PROFILE_SIGNING
)) {
464 tpPolicy
= kTP_ProfileSigning
;
467 else if(tpCompareOids(&oid
, &CSSMOID_APPLE_TP_QA_PROFILE_SIGNING
)) {
468 tpPolicy
= kTP_QAProfileSigning
;
471 else if(tpCompareOids(&oid
, &CSSMOID_APPLE_TP_PCS_ESCROW_SERVICE
)) {
472 tpPolicy
= kTP_PCSEscrowService
;
475 else if(tpCompareOids(&oid
, &CSSMOID_APPLE_TP_PROVISIONING_PROFILE_SIGNING
)) {
476 tpPolicy
= kTP_ProvisioningProfileSigning
;
482 /*-----------------------------------------------------------------------------
486 * -- Construct a cert chain using TP_CertGroupConstruct.
487 * -- Attempt to verify that cert chain against one of the known
488 * good certs passed in AnchorCerts.
489 * -- Optionally enforces additional policies (TBD) when verifying the cert chain.
490 * -- Optionally returns the entire cert chain constructed in
491 * TP_CertGroupConstruct and here, all the way to an anchor cert or as
492 * far as we were able to go, in *Evidence.
495 * Two handles - to an open CL and CSP. The CSP must be capable of
496 * dealing with the signature algorithms used by the certs. The CL must be
499 * RawCerts, an unordered array of raw certs in the form of a
500 * CSSM_CERTGROUP_PTR. The first cert of this list is the subject cert
501 * which is eventually to be verified. The other certs can be in any order
502 * and may not even have any relevance to the cert chain being constructed.
503 * They may also be invalid certs.
505 * DBList, a list of DB/DL handles which may contain certs necessary to
506 * complete the desired cert chain. (Currently not implemented.)
508 * AnchorCerts, a list of known trusted certs.
509 * NumberOfAnchorCerts, size of AnchorCerts array.
511 * PolicyIdentifiers, Optional policy OID. NULL indicates default
512 * X.509 trust policy.
514 * Supported Policies:
515 * CSSMOID_APPLE_ISIGN
516 * CSSMOID_APPLE_X509_BASIC
518 * For both of these, the associated FieldValue must be {0, NULL},
520 * NumberOfPolicyIdentifiers, size of PolicyIdentifiers array, must be
523 * All other arguments must be zero/NULL.
526 * CSSM_OK : cert chain verified all the way back to an AnchorCert.
527 * CSSMERR_TP_INVALID_ANCHOR_CERT : In this case, the cert chain
528 * was validated back to a self-signed (root) cert found in either
529 * CertToBeVerified or in one of the DBs in DBList, but that root cert
530 * was *NOT* found in the AnchorCert list.
531 * CSSMERR_TP_NOT_TRUSTED: no root cert was found and no AnchorCert
532 * verified the end of the constructed cert chain.
533 * CSSMERR_TP_VERIFICATION_FAILURE: a root cert was found which does
535 * CSSMERR_TP_VERIFY_ACTION_FAILED: indicates a failure of the requested
537 * CSSMERR_TP_INVALID_CERTIFICATE: indicates a bad leaf cert.
538 * CSSMERR_TP_INVALID_REQUEST_INPUTS : no incoming VerifyContext.
539 * CSSMERR_TP_CERT_EXPIRED and CSSMERR_TP_CERT_NOT_VALID_YET: see comments
540 * for CertGroupConstruct.
541 * CSSMERR_TP_CERTIFICATE_CANT_OPERATE : issuer cert was found with a partial
542 * public key, rendering full verification impossible.
543 * CSSMERR_TP_INVALID_CERT_AUTHORITY : issuer cert was found with a partial
544 * public key and which failed to perform subsequent signature
546 *---------------------------------------------------------------------------*/
548 void AppleTPSession::CertGroupVerify(CSSM_CL_HANDLE clHand
,
549 CSSM_CSP_HANDLE cspHand
,
550 const CSSM_CERTGROUP
&CertGroupToBeVerified
,
551 const CSSM_TP_VERIFY_CONTEXT
*VerifyContext
,
552 CSSM_TP_VERIFY_CONTEXT_RESULT_PTR VerifyContextResult
)
554 CSSM_BOOL verifiedToRoot
= CSSM_FALSE
;
555 CSSM_BOOL verifiedToAnchor
= CSSM_FALSE
;
556 CSSM_BOOL verifiedViaTrustSetting
= CSSM_FALSE
;
557 CSSM_RETURN constructReturn
= CSSM_OK
;
558 CSSM_RETURN policyReturn
= CSSM_OK
;
559 const CSSM_TP_CALLERAUTH_CONTEXT
*cred
;
560 /* declare volatile as compiler workaround to avoid caching in CR4 */
561 const CSSM_APPLE_TP_ACTION_DATA
* volatile actionData
= NULL
;
562 CSSM_TIMESTRING cssmTimeStr
;
563 CSSM_APPLE_TP_ACTION_FLAGS actionFlags
= 0;
564 CSSM_TP_STOP_ON tpStopOn
= 0;
566 /* keep track of whether we did policy checking; if not, we do defaults */
567 bool didCertPolicy
= false;
568 bool didRevokePolicy
= false;
570 /* user trust parameters */
571 CSSM_OID utNullPolicy
= {0, NULL
};
572 const CSSM_OID
*utPolicyOid
= NULL
;
573 const char *utPolicyStr
= NULL
;
574 uint32 utPolicyStrLen
= 0;
575 SecTrustSettingsKeyUsage utKeyUse
= 0;
576 bool utTrustSettingEnabled
= false;
578 if(VerifyContextResult
) {
579 memset(VerifyContextResult
, 0, sizeof(*VerifyContextResult
));
582 /* verify input args, skipping the ones checked by CertGroupConstruct */
583 if((VerifyContext
== NULL
) || (VerifyContext
->Cred
== NULL
)) {
584 /* the spec says that this is optional but we require it */
585 CssmError::throwMe(CSSMERR_TP_INVALID_REQUEST_INPUTS
);
587 cred
= VerifyContext
->Cred
;
589 /* Optional ActionData affecting all policies */
590 actionData
= (CSSM_APPLE_TP_ACTION_DATA
* volatile)VerifyContext
->ActionData
.Data
;
591 if(actionData
!= NULL
) {
592 switch(actionData
->Version
) {
593 case CSSM_APPLE_TP_ACTION_VERSION
:
594 if(VerifyContext
->ActionData
.Length
!=
595 sizeof(CSSM_APPLE_TP_ACTION_DATA
)) {
596 CssmError::throwMe(CSSMERR_TP_INVALID_ACTION_DATA
);
599 /* handle backwards versions here if we ever go beyond version 0 */
601 CssmError::throwMe(CSSMERR_TP_INVALID_ACTION_DATA
);
603 actionFlags
= actionData
->ActionFlags
;
604 if(actionFlags
& CSSM_TP_ACTION_TRUST_SETTINGS
) {
605 utTrustSettingEnabled
= true;
609 /* optional, may be NULL */
610 cssmTimeStr
= cred
->VerifyTime
;
612 tpStopOn
= cred
->VerificationAbortOn
;
614 /* the only two we support */
615 case CSSM_TP_STOP_ON_NONE
:
616 case CSSM_TP_STOP_ON_FIRST_FAIL
:
618 /* default maps to stop on first fail */
619 case CSSM_TP_STOP_ON_POLICY
:
620 tpStopOn
= CSSM_TP_STOP_ON_FIRST_FAIL
;
623 CssmError::throwMe(CSSMERR_TP_INVALID_STOP_ON_POLICY
);
626 /* now the args we can't deal with */
627 if(cred
->CallerCredentials
!= NULL
) {
628 CssmError::throwMe(CSSMERR_TP_INVALID_CALLERAUTH_CONTEXT_POINTER
);
632 /* set up for optional user trust evaluation */
633 if(utTrustSettingEnabled
) {
634 const CSSM_TP_POLICYINFO
*pinfo
= &cred
->Policy
;
635 TPPolicy utPolicy
= kTPx509Basic
;
637 /* default policy OID in case caller hasn't specified one */
638 utPolicyOid
= &utNullPolicy
;
639 if(pinfo
->NumberOfPolicyIds
== 0) {
640 tpTrustSettingsDbg("CertGroupVerify: User trust enabled but no policies (1)");
641 /* keep going, I guess - no policy-specific info - use kTPx509Basic */
644 CSSM_FIELD_PTR utPolicyField
= &pinfo
->PolicyIds
[0];
645 utPolicyOid
= &utPolicyField
->FieldOid
;
646 bool foundPolicy
= checkPolicyOid(*utPolicyOid
, utPolicy
);
648 tpTrustSettingsDbg("CertGroupVerify: User trust enabled but no policies");
649 /* keep going, I guess - no policy-specific info - use kTPx509Basic */
652 /* get policy-specific info */
653 tp_policyTrustSettingParams(utPolicy
, &utPolicyField
->FieldValue
,
654 &utPolicyStr
, &utPolicyStrLen
, &utKeyUse
);
659 /* get verified (possibly partial) outCertGroup - error is fatal */
660 /* BUT: we still return partial evidence if asked to...from now on. */
661 TPCertGroup
outCertGroup(*this,
662 TGO_Caller
); // certs are owned by inCertGroup
663 TPCertGroup
inCertGroup(CertGroupToBeVerified
, clHand
, cspHand
, *this,
664 cssmTimeStr
, // optional 'this' time
665 true, // firstCertMustBeValid
668 /* set up for disposal of TPCertInfos created by CertGroupConstructPriv */
669 TPCertGroup
gatheredCerts(*this, TGO_Group
);
672 CertGroupConstructPriv(
678 cred
->NumberOfAnchorCerts
,
688 verifiedViaTrustSetting
,
691 catch(const CssmError
&cerr
) {
692 constructReturn
= cerr
.error
;
693 /* abort if no certs found */
694 if(outCertGroup
.numCerts() == 0) {
695 CssmError::throwMe(constructReturn
);
697 /* else press on, collecting as much info as we can */
699 /* others are way fatal */
700 assert(outCertGroup
.numCerts() >= 1);
702 /* Infer interim status from return values */
703 switch(constructReturn
) {
704 /* these values do not get overridden */
705 case CSSMERR_TP_CERTIFICATE_CANT_OPERATE
:
706 case CSSMERR_TP_INVALID_CERT_AUTHORITY
:
707 case CSSMERR_APPLETP_TRUST_SETTING_DENY
:
708 case errSecInvalidTrustSettings
:
711 /* infer status from these values... */
712 if(verifiedToAnchor
|| verifiedViaTrustSetting
) {
713 /* full success; anchor doesn't have to be root */
714 constructReturn
= CSSM_OK
;
716 else if(verifiedToRoot
) {
717 if(actionFlags
& CSSM_TP_ACTION_IMPLICIT_ANCHORS
) {
718 constructReturn
= CSSM_OK
;
721 /* verified to root which is not an anchor */
722 constructReturn
= CSSMERR_TP_INVALID_ANCHOR_CERT
;
726 /* partial chain, no root, not verifiable by anchor */
727 constructReturn
= CSSMERR_TP_NOT_TRUSTED
;
731 * Those errors can be allowed, cert-chain-wide, per individual
732 * certs' allowedErrors
734 if((constructReturn
!= CSSM_OK
) &&
735 outCertGroup
.isAllowedError(constructReturn
)) {
736 constructReturn
= CSSM_OK
;
743 * Parameters passed to tp_policyVerify() and which vary per policy
747 const CSSM_APPLE_TP_SSL_OPTIONS
*sslOpts
;
748 CSSM_RETURN thisPolicyRtn
= CSSM_OK
; // returned from tp_policyVerify()
750 /* common CRL verify parameters */
751 TPCrlGroup
*crlGroup
= NULL
;
753 crlGroup
= new TPCrlGroup(&VerifyContext
->Crls
,
756 NULL
, // cssmTimeStr - we want CRLs that are valid 'now'
759 catch(const CssmError
&cerr
) {
760 CSSM_RETURN cr
= cerr
.error
;
761 /* I don't see a straightforward way to report this error,
762 * other than adding it to the leaf cert's status... */
763 outCertGroup
.certAtIndex(0)->addStatusCode(cr
);
764 tpDebug("CertGroupVerify: error constructing CrlGroup; continuing\n");
766 /* others are way fatal */
768 TPVerifyContext
revokeVfyContext(*this,
772 cred
->NumberOfAnchorCerts
,
777 * This may consist of certs gathered from the net (which is the purpose
778 * of this argument) and from DLDBs (a side-effect optimization).
782 kRevokeNone
, // policy
785 NULL
, // OCSP options
791 /* true if we're to execute tp_policyVerify at end of loop */
793 /* true if we're to execute a revocation policy at end of loop */
794 bool doRevocationPolicy
;
796 /* grind thru each policy */
797 for(uint32 polDex
=0; polDex
<cred
->Policy
.NumberOfPolicyIds
; polDex
++) {
798 if(cred
->Policy
.PolicyIds
== NULL
) {
799 policyReturn
= CSSMERR_TP_INVALID_POLICY_IDENTIFIERS
;
802 CSSM_FIELD_PTR policyId
= &cred
->Policy
.PolicyIds
[polDex
];
803 const CSSM_DATA
*fieldVal
= &policyId
->FieldValue
;
804 const CSSM_OID
*oid
= &policyId
->FieldOid
;
805 thisPolicyRtn
= CSSM_OK
;
806 doPolicyVerify
= false;
807 doRevocationPolicy
= false;
810 /* first the basic cert policies */
811 doPolicyVerify
= checkPolicyOid(*oid
, tpPolicy
);
813 /* some basic checks... */
814 bool policyAbort
= false;
818 case kTP_PKINIT_Client
:
819 case kTP_PKINIT_Server
:
820 if(fieldVal
->Data
!= NULL
) {
821 policyReturn
= CSSMERR_TP_INVALID_POLICY_IDENTIFIERS
;
832 #if TP_PKINIT_SERVER_HACK
833 if(tpPolicy
== kTP_PKINIT_Server
) {
834 /* possible override of "root not in anchors" */
835 if(constructReturn
== CSSMERR_TP_INVALID_ANCHOR_CERT
) {
836 if(tpCheckPkinitServerCert(outCertGroup
)) {
837 constructReturn
= CSSM_OK
;
841 #endif /* TP_PKINIT_SERVER_HACK */
845 * Now revocation policies. Note some fields in revokeVfyContext can
846 * accumulate across multiple policy calls, e.g., signerCerts.
848 else if(tpCompareOids(oid
, &CSSMOID_APPLE_TP_REVOCATION_CRL
)) {
849 /* CRL-specific options */
850 const CSSM_APPLE_TP_CRL_OPTIONS
*crlOpts
;
851 crlOpts
= (CSSM_APPLE_TP_CRL_OPTIONS
*)fieldVal
->Data
;
852 thisPolicyRtn
= CSSM_OK
;
853 if(crlOpts
!= NULL
) {
854 switch(crlOpts
->Version
) {
855 case CSSM_APPLE_TP_CRL_OPTS_VERSION
:
856 if(fieldVal
->Length
!=
857 sizeof(CSSM_APPLE_TP_CRL_OPTIONS
)) {
859 CSSMERR_TP_INVALID_POLICY_IDENTIFIERS
;
863 /* handle backwards compatibility here if necessary */
865 thisPolicyRtn
= CSSMERR_TP_INVALID_POLICY_IDENTIFIERS
;
868 if(thisPolicyRtn
!= CSSM_OK
) {
869 policyReturn
= thisPolicyRtn
;
873 revokeVfyContext
.policy
= kRevokeCrlBasic
;
874 revokeVfyContext
.crlOpts
= crlOpts
;
875 doRevocationPolicy
= true;
877 else if(tpCompareOids(oid
, &CSSMOID_APPLE_TP_REVOCATION_OCSP
)) {
878 /* OCSP-specific options */
879 const CSSM_APPLE_TP_OCSP_OPTIONS
*ocspOpts
;
880 ocspOpts
= (CSSM_APPLE_TP_OCSP_OPTIONS
*)fieldVal
->Data
;
881 thisPolicyRtn
= CSSM_OK
;
882 if(ocspOpts
!= NULL
) {
883 switch(ocspOpts
->Version
) {
884 case CSSM_APPLE_TP_OCSP_OPTS_VERSION
:
885 if(fieldVal
->Length
!=
886 sizeof(CSSM_APPLE_TP_OCSP_OPTIONS
)) {
888 CSSMERR_TP_INVALID_POLICY_IDENTIFIERS
;
892 /* handle backwards compatibility here if necessary */
894 thisPolicyRtn
= CSSMERR_TP_INVALID_POLICY_IDENTIFIERS
;
897 if(thisPolicyRtn
!= CSSM_OK
) {
898 policyReturn
= thisPolicyRtn
;
902 revokeVfyContext
.policy
= kRevokeOcsp
;
903 revokeVfyContext
.ocspOpts
= ocspOpts
;
904 doRevocationPolicy
= true;
906 /* etc. - add more policies here */
908 /* unknown TP policy OID */
909 policyReturn
= CSSMERR_TP_INVALID_POLICY_IDENTIFIERS
;
913 /* common cert policy call */
915 assert(!doRevocationPolicy
); // one at a time
916 thisPolicyRtn
= tp_policyVerify(tpPolicy
,
922 verifiedViaTrustSetting
,
925 cred
->Policy
.PolicyControl
); // not currently used
926 didCertPolicy
= true;
928 /* common revocation policy call */
929 if(doRevocationPolicy
) {
930 assert(!doPolicyVerify
); // one at a time
931 thisPolicyRtn
= tpRevocationPolicyVerify(revokeVfyContext
, outCertGroup
);
932 didRevokePolicy
= true;
934 /* See if possible error is allowed, cert-chain-wide. */
935 if((thisPolicyRtn
!= CSSM_OK
) &&
936 outCertGroup
.isAllowedError(thisPolicyRtn
)) {
937 thisPolicyRtn
= CSSM_OK
;
940 /* Now remember the error if it's the first policy
941 * error we've seen. */
942 if(policyReturn
== CSSM_OK
) {
943 policyReturn
= thisPolicyRtn
;
946 if(tpStopOn
== CSSM_TP_STOP_ON_FIRST_FAIL
) {
947 /* Nope; we're done with policy evaluation */
951 } /* for each policy */
954 * Upon completion of the above loop, perform default policy ops if
957 if((policyReturn
== CSSM_OK
) || (tpStopOn
== CSSM_TP_STOP_ON_NONE
)) {
959 policyReturn
= tp_policyVerify(kTPDefault
,
965 verifiedViaTrustSetting
,
967 NULL
, // policyFieldData
968 cred
->Policy
.PolicyControl
); // not currently used
969 /* See if error is allowed, cert-chain-wide. */
970 if((policyReturn
!= CSSM_OK
) &&
971 outCertGroup
.isAllowedError(policyReturn
)) {
972 policyReturn
= CSSM_OK
;
975 if( !didRevokePolicy
&& // no revoke policy yet
976 ( (policyReturn
== CSSM_OK
|| // default cert policy OK
977 (tpStopOn
== CSSM_TP_STOP_ON_NONE
)) // keep going anyway
980 revokeVfyContext
.policy
= TP_CRL_POLICY_DEFAULT
;
981 CSSM_RETURN thisPolicyRtn
= tpRevocationPolicyVerify(revokeVfyContext
,
983 if((thisPolicyRtn
!= CSSM_OK
) &&
984 outCertGroup
.isAllowedError(thisPolicyRtn
)) {
985 thisPolicyRtn
= CSSM_OK
;
987 if((thisPolicyRtn
!= CSSM_OK
) && (policyReturn
== CSSM_OK
)) {
988 policyReturn
= thisPolicyRtn
;
992 } /* default policy opts */
996 /* return evidence - i.e., constructed chain - if asked to */
997 if(VerifyContextResult
!= NULL
) {
999 * VerifyContextResult->Evidence[0] : CSSM_TP_APPLE_EVIDENCE_HEADER
1000 * VerifyContextResult->Evidence[1] : CSSM_CERTGROUP
1001 * VerifyContextResult->Evidence[2] : CSSM_TP_APPLE_EVIDENCE_INFO
1003 VerifyContextResult
->NumberOfEvidences
= 3;
1004 VerifyContextResult
->Evidence
=
1005 (CSSM_EVIDENCE_PTR
)calloc(3, sizeof(CSSM_EVIDENCE
));
1007 CSSM_TP_APPLE_EVIDENCE_HEADER
*hdr
=
1008 (CSSM_TP_APPLE_EVIDENCE_HEADER
*)malloc(
1009 sizeof(CSSM_TP_APPLE_EVIDENCE_HEADER
));
1010 hdr
->Version
= CSSM_TP_APPLE_EVIDENCE_VERSION
;
1011 CSSM_EVIDENCE_PTR ev
= &VerifyContextResult
->Evidence
[0];
1012 ev
->EvidenceForm
= CSSM_EVIDENCE_FORM_APPLE_HEADER
;
1015 ev
= &VerifyContextResult
->Evidence
[1];
1016 ev
->EvidenceForm
= CSSM_EVIDENCE_FORM_APPLE_CERTGROUP
;
1017 ev
->Evidence
= outCertGroup
.buildCssmCertGroup();
1019 ev
= &VerifyContextResult
->Evidence
[2];
1020 ev
->EvidenceForm
= CSSM_EVIDENCE_FORM_APPLE_CERT_INFO
;
1021 ev
->Evidence
= outCertGroup
.buildCssmEvidenceInfo();
1024 /* caller responsible for freeing these if they are for evidence.... */
1025 outCertGroup
.freeDbRecords();
1027 CSSM_RETURN outErr
= outCertGroup
.getReturnCode(constructReturn
, policyReturn
,
1031 CssmError::throwMe(outErr
);