2 * Copyright (c) 2000-2001, 2011 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 temp 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
= subjectName
->Length
;
133 attrs
[0].data
= subjectName
->Data
;
134 attrs
[1].tag
= kSecSerialNumberItemAttr
;
135 attrs
[1].length
= 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 */
183 /*-----------------------------------------------------------------------------
187 * This function returns a pointer to a mallocd CSSM_CERTGROUP which
188 * refers to a mallocd list of raw ordered X.509 certs which verify back as
189 * far as the TP is able to go. The first cert of the returned list is the
190 * subject cert. The TP will attempt to search thru the DBs passed in
191 * DBList in order to complete the chain. The chain is completed when a
192 * self-signed (root) cert is found in the chain. The root cert may be
193 * present in the input CertGroupFrag, or it may have been obtained from
194 * one of the DBs passed in DBList. It is not an error if no root cert is
197 * The error conditions are:
198 * -- The first cert of CertGroupFrag is an invalid cert. NULL is returned,
199 * err = CSSM_TP_INVALID_CERTIFICATE.
200 * -- The root cert (if found) fails to verify. Valid certgroup is returned,
201 * err = CSSMERR_TP_VERIFICATION_FAILURE.
202 * -- Any cert in the (possibly partially) constructed chain has expired or
203 * isn't valid yet, err = CSSMERR_TP_CERT_EXPIRED or
204 * CSSMERR_TP_CERT_NOT_VALID_YET. A CertGroup is returned.
205 * -- CSSMERR_TP_CERT_EXPIRED and CSSMERR_TP_CERT_NOT_VALID_YET. If one of these
206 * conditions obtains for the first (leaf) cert, the function throws this
207 * error immediately and the outgoing cert group is empty. For subsequent certs,
208 * the temporal validity of a cert is only tested AFTER a cert successfully
209 * meets the cert chaining criteria (subject/issuer match and signature
210 * verify). A cert in a chain with this error is not added to the outgoing
212 * -- the usual errors like bad handle or memory failure.
215 * Two handles - to an open CL and CSP. The CSP must be capable of
216 * dealing with the signature algorithms used by the certs. The CL must be
219 * CertGroupFrag, an unordered array of raw X.509 certs in the form of a
220 * CSSM_CERTGROUP_PTR. The first cert of this list is the subject cert
221 * which is eventually to be verified. The other certs can be in any order
222 * and may not even have any relevance to the cert chain being constructed.
223 * They may also be invalid certs.
225 * DBList, a list of DB/DL handles which may contain certs necessary to
226 * complete the desired cert chain. (Not currently implemented.)
228 *---------------------------------------------------------------------------*/
231 void AppleTPSession::CertGroupConstruct(CSSM_CL_HANDLE clHand
,
232 CSSM_CSP_HANDLE cspHand
,
233 const CSSM_DL_DB_LIST
&DBList
,
234 const void *ConstructParams
,
235 const CSSM_CERTGROUP
&CertGroupFrag
,
236 CSSM_CERTGROUP_PTR
&CertGroup
)
238 TPCertGroup
outCertGroup(*this, TGO_Caller
);
239 TPCertGroup
inCertGroup(CertGroupFrag
,
244 true, // firstCertMustBeValid
247 /* set up for disposal of TPCertInfos created by CertGroupConstructPriv */
248 TPCertGroup
gatheredCerts(*this, TGO_Group
);
250 CSSM_RETURN constructReturn
= CSSM_OK
;
251 CSSM_APPLE_TP_ACTION_FLAGS actionFlags
= 0;
252 CSSM_BOOL verifiedToRoot
; // not used
253 CSSM_BOOL verifiedToAnchor
; // not used
254 CSSM_BOOL verifiedViaTrustSetting
; // not used
257 CertGroupConstructPriv(clHand
,
270 verifiedViaTrustSetting
,
273 catch(const CssmError
&cerr
) {
274 constructReturn
= cerr
.error
;
275 /* abort if no certs found */
276 if(outCertGroup
.numCerts() == 0) {
277 CssmError::throwMe(constructReturn
);
280 CertGroup
= outCertGroup
.buildCssmCertGroup();
281 /* caller of this function never gets evidence... */
282 outCertGroup
.freeDbRecords();
284 if(constructReturn
) {
285 CssmError::throwMe(constructReturn
);
291 * Private version of CertGroupConstruct, used by CertGroupConstruct and
292 * CertGroupVerify. Populates a TP-style TPCertGroup for further processing.
293 * This only throws CSSM-style exceptions in the following cases:
295 * -- input parameter errors
296 * -- the first (leaf) cert is bad (doesn't parse, expired, not valid yet).
297 * -- root found but it doesn't self-verify
299 * All other cert-related errors simply result in the bad cert being ignored.
300 * Other exceptions are gross system errors like malloc failure.
302 void AppleTPSession::CertGroupConstructPriv(CSSM_CL_HANDLE clHand
,
303 CSSM_CSP_HANDLE cspHand
,
304 TPCertGroup
&inCertGroup
,
305 const CSSM_DL_DB_LIST
*DBList
, // optional here
306 const char *cssmTimeStr
, // optional
308 /* trusted anchors, optional */
309 /* FIXME - maybe this should be a TPCertGroup */
310 uint32 numAnchorCerts
,
311 const CSSM_DATA
*anchorCerts
,
313 /* CSSM_TP_ACTION_FETCH_CERT_FROM_NET, CSSM_TP_ACTION_TRUST_SETTINGS */
314 CSSM_APPLE_TP_ACTION_FLAGS actionFlags
,
316 /* optional user trust parameters */
317 const CSSM_OID
*policyOid
,
318 const char *policyStr
,
320 SecTrustSettingsKeyUsage keyUse
,
323 * Certs to be freed by caller (i.e., TPCertInfo which we allocate
324 * as a result of using a cert from anchorCerts or dbList) are added
327 TPCertGroup
&certsToBeFreed
,
330 CSSM_BOOL
&verifiedToRoot
, // end of chain self-verifies
331 CSSM_BOOL
&verifiedToAnchor
, // end of chain in anchors
332 CSSM_BOOL
&verifiedViaTrustSetting
, // chain ends per User Trust setting
333 TPCertGroup
&outCertGroup
) // RETURNED
335 TPCertInfo
*subjectCert
; // the one we're working on
336 CSSM_RETURN outErr
= CSSM_OK
;
338 /* this'll be the first subject cert in the main loop */
339 subjectCert
= inCertGroup
.certAtIndex(0);
341 /* Append leaf cert to outCertGroup */
342 outCertGroup
.appendCert(subjectCert
);
343 subjectCert
->isLeaf(true);
344 subjectCert
->isFromInputCerts(true);
345 outCertGroup
.setAllUnused();
346 subjectCert
->used(true);
348 outErr
= outCertGroup
.buildCertGroup(
358 &certsToBeFreed
, // gatheredCerts to accumulate net/DB fetches
359 CSSM_TRUE
, // subjectIsInGroup - enables root check on
369 verifiedViaTrustSetting
);
371 CssmError::throwMe(outErr
);
376 * Map a policy OID to one of the standard (non-revocation) policies.
377 * Returns true if it's a standard policy.
379 static bool checkPolicyOid(
381 TPPolicy
&tpPolicy
) /* RETURNED */
383 if(tpCompareOids(&oid
, &CSSMOID_APPLE_TP_SSL
)) {
387 else if(tpCompareOids(&oid
, &CSSMOID_APPLE_X509_BASIC
)) {
388 tpPolicy
= kTPx509Basic
;
391 else if(tpCompareOids(&oid
, &CSSMOID_APPLE_TP_SMIME
)) {
392 tpPolicy
= kTP_SMIME
;
395 else if(tpCompareOids(&oid
, &CSSMOID_APPLE_TP_EAP
)) {
399 else if(tpCompareOids(&oid
, &CSSMOID_APPLE_TP_SW_UPDATE_SIGNING
)) {
400 /* note: this was CSSMOID_APPLE_TP_CODE_SIGN until 8/15/06 */
401 tpPolicy
= kTP_SWUpdateSign
;
404 else if(tpCompareOids(&oid
, &CSSMOID_APPLE_TP_RESOURCE_SIGN
)) {
405 tpPolicy
= kTP_ResourceSign
;
408 else if(tpCompareOids(&oid
, &CSSMOID_APPLE_TP_IP_SEC
)) {
409 tpPolicy
= kTP_IPSec
;
412 else if(tpCompareOids(&oid
, &CSSMOID_APPLE_TP_ICHAT
)) {
413 tpPolicy
= kTP_iChat
;
416 else if(tpCompareOids(&oid
, &CSSMOID_APPLE_ISIGN
)) {
420 else if(tpCompareOids(&oid
, &CSSMOID_APPLE_TP_PKINIT_CLIENT
)) {
421 tpPolicy
= kTP_PKINIT_Client
;
424 else if(tpCompareOids(&oid
, &CSSMOID_APPLE_TP_PKINIT_SERVER
)) {
425 tpPolicy
= kTP_PKINIT_Server
;
428 else if(tpCompareOids(&oid
, &CSSMOID_APPLE_TP_CODE_SIGNING
)) {
429 tpPolicy
= kTP_CodeSigning
;
432 else if(tpCompareOids(&oid
, &CSSMOID_APPLE_TP_PACKAGE_SIGNING
)) {
433 tpPolicy
= kTP_PackageSigning
;
436 else if(tpCompareOids(&oid
, &CSSMOID_APPLE_TP_MACAPPSTORE_RECEIPT
)) {
437 tpPolicy
= kTP_MacAppStoreRec
;
440 else if(tpCompareOids(&oid
, &CSSMOID_APPLE_TP_APPLEID_SHARING
)) {
441 tpPolicy
= kTP_AppleIDSharing
;
444 else if(tpCompareOids(&oid
, &CSSMOID_APPLE_TP_TIMESTAMPING
)) {
445 tpPolicy
= kTP_TimeStamping
;
451 /*-----------------------------------------------------------------------------
455 * -- Construct a cert chain using TP_CertGroupConstruct.
456 * -- Attempt to verify that cert chain against one of the known
457 * good certs passed in AnchorCerts.
458 * -- Optionally enforces additional policies (TBD) when verifying the cert chain.
459 * -- Optionally returns the entire cert chain constructed in
460 * TP_CertGroupConstruct and here, all the way to an anchor cert or as
461 * far as we were able to go, in *Evidence.
464 * Two handles - to an open CL and CSP. The CSP must be capable of
465 * dealing with the signature algorithms used by the certs. The CL must be
468 * RawCerts, an unordered array of raw certs in the form of a
469 * CSSM_CERTGROUP_PTR. The first cert of this list is the subject cert
470 * which is eventually to be verified. The other certs can be in any order
471 * and may not even have any relevance to the cert chain being constructed.
472 * They may also be invalid certs.
474 * DBList, a list of DB/DL handles which may contain certs necessary to
475 * complete the desired cert chain. (Currently not implemented.)
477 * AnchorCerts, a list of known trusted certs.
478 * NumberOfAnchorCerts, size of AnchorCerts array.
480 * PolicyIdentifiers, Optional policy OID. NULL indicates default
481 * X.509 trust policy.
483 * Supported Policies:
484 * CSSMOID_APPLE_ISIGN
485 * CSSMOID_APPLE_X509_BASIC
487 * For both of these, the associated FieldValue must be {0, NULL},
489 * NumberOfPolicyIdentifiers, size of PolicyIdentifiers array, must be
492 * All other arguments must be zero/NULL.
495 * CSSM_OK : cert chain verified all the way back to an AnchorCert.
496 * CSSMERR_TP_INVALID_ANCHOR_CERT : In this case, the cert chain
497 * was validated back to a self-signed (root) cert found in either
498 * CertToBeVerified or in one of the DBs in DBList, but that root cert
499 * was *NOT* found in the AnchorCert list.
500 * CSSMERR_TP_NOT_TRUSTED: no root cert was found and no AnchorCert
501 * verified the end of the constructed cert chain.
502 * CSSMERR_TP_VERIFICATION_FAILURE: a root cert was found which does
504 * CSSMERR_TP_VERIFY_ACTION_FAILED: indicates a failure of the requested
506 * CSSMERR_TP_INVALID_CERTIFICATE: indicates a bad leaf cert.
507 * CSSMERR_TP_INVALID_REQUEST_INPUTS : no incoming VerifyContext.
508 * CSSMERR_TP_CERT_EXPIRED and CSSMERR_TP_CERT_NOT_VALID_YET: see comments
509 * for CertGroupConstruct.
510 * CSSMERR_TP_CERTIFICATE_CANT_OPERATE : issuer cert was found with a partial
511 * public key, rendering full verification impossible.
512 * CSSMERR_TP_INVALID_CERT_AUTHORITY : issuer cert was found with a partial
513 * public key and which failed to perform subsequent signature
515 *---------------------------------------------------------------------------*/
517 void AppleTPSession::CertGroupVerify(CSSM_CL_HANDLE clHand
,
518 CSSM_CSP_HANDLE cspHand
,
519 const CSSM_CERTGROUP
&CertGroupToBeVerified
,
520 const CSSM_TP_VERIFY_CONTEXT
*VerifyContext
,
521 CSSM_TP_VERIFY_CONTEXT_RESULT_PTR VerifyContextResult
)
523 CSSM_BOOL verifiedToRoot
= CSSM_FALSE
;
524 CSSM_BOOL verifiedToAnchor
= CSSM_FALSE
;
525 CSSM_BOOL verifiedViaTrustSetting
= CSSM_FALSE
;
526 CSSM_RETURN constructReturn
= CSSM_OK
;
527 CSSM_RETURN policyReturn
= CSSM_OK
;
528 const CSSM_TP_CALLERAUTH_CONTEXT
*cred
;
529 /* declare volatile as compiler workaround to avoid caching in CR4 */
530 const CSSM_APPLE_TP_ACTION_DATA
* volatile actionData
= NULL
;
531 CSSM_TIMESTRING cssmTimeStr
;
532 CSSM_APPLE_TP_ACTION_FLAGS actionFlags
= 0;
533 CSSM_TP_STOP_ON tpStopOn
= 0;
535 /* keep track of whether we did policy checking; if not, we do defaults */
536 bool didCertPolicy
= false;
537 bool didRevokePolicy
= false;
539 /* user trust parameters */
540 CSSM_OID utNullPolicy
= {0, NULL
};
541 const CSSM_OID
*utPolicyOid
= NULL
;
542 const char *utPolicyStr
= NULL
;
543 uint32 utPolicyStrLen
= 0;
544 SecTrustSettingsKeyUsage utKeyUse
= 0;
545 bool utTrustSettingEnabled
= false;
547 if(VerifyContextResult
) {
548 memset(VerifyContextResult
, 0, sizeof(*VerifyContextResult
));
551 /* verify input args, skipping the ones checked by CertGroupConstruct */
552 if((VerifyContext
== NULL
) || (VerifyContext
->Cred
== NULL
)) {
553 /* the spec says that this is optional but we require it */
554 CssmError::throwMe(CSSMERR_TP_INVALID_REQUEST_INPUTS
);
556 cred
= VerifyContext
->Cred
;
558 /* Optional ActionData affecting all policies */
559 actionData
= (CSSM_APPLE_TP_ACTION_DATA
* volatile)VerifyContext
->ActionData
.Data
;
560 if(actionData
!= NULL
) {
561 switch(actionData
->Version
) {
562 case CSSM_APPLE_TP_ACTION_VERSION
:
563 if(VerifyContext
->ActionData
.Length
!=
564 sizeof(CSSM_APPLE_TP_ACTION_DATA
)) {
565 CssmError::throwMe(CSSMERR_TP_INVALID_ACTION_DATA
);
568 /* handle backwards versions here if we ever go beyond version 0 */
570 CssmError::throwMe(CSSMERR_TP_INVALID_ACTION_DATA
);
572 actionFlags
= actionData
->ActionFlags
;
573 if(actionFlags
& CSSM_TP_ACTION_TRUST_SETTINGS
) {
574 utTrustSettingEnabled
= true;
578 /* optional, may be NULL */
579 cssmTimeStr
= cred
->VerifyTime
;
581 tpStopOn
= cred
->VerificationAbortOn
;
583 /* the only two we support */
584 case CSSM_TP_STOP_ON_NONE
:
585 case CSSM_TP_STOP_ON_FIRST_FAIL
:
587 /* default maps to stop on first fail */
588 case CSSM_TP_STOP_ON_POLICY
:
589 tpStopOn
= CSSM_TP_STOP_ON_FIRST_FAIL
;
592 CssmError::throwMe(CSSMERR_TP_INVALID_STOP_ON_POLICY
);
595 /* now the args we can't deal with */
596 if(cred
->CallerCredentials
!= NULL
) {
597 CssmError::throwMe(CSSMERR_TP_INVALID_CALLERAUTH_CONTEXT_POINTER
);
601 /* set up for optional user trust evaluation */
602 if(utTrustSettingEnabled
) {
603 const CSSM_TP_POLICYINFO
*pinfo
= &cred
->Policy
;
604 TPPolicy utPolicy
= kTPx509Basic
;
606 /* default policy OID in case caller hasn't specified one */
607 utPolicyOid
= &utNullPolicy
;
608 if(pinfo
->NumberOfPolicyIds
== 0) {
609 tpTrustSettingsDbg("CertGroupVerify: User trust enabled but no policies (1)");
610 /* keep going, I guess - no policy-specific info - use kTPx509Basic */
613 CSSM_FIELD_PTR utPolicyField
= &pinfo
->PolicyIds
[0];
614 utPolicyOid
= &utPolicyField
->FieldOid
;
615 bool foundPolicy
= checkPolicyOid(*utPolicyOid
, utPolicy
);
617 tpTrustSettingsDbg("CertGroupVerify: User trust enabled but no policies");
618 /* keep going, I guess - no policy-specific info - use kTPx509Basic */
621 /* get policy-specific info */
622 tp_policyTrustSettingParams(utPolicy
, &utPolicyField
->FieldValue
,
623 &utPolicyStr
, &utPolicyStrLen
, &utKeyUse
);
628 /* get verified (possibly partial) outCertGroup - error is fatal */
629 /* BUT: we still return partial evidence if asked to...from now on. */
630 TPCertGroup
outCertGroup(*this,
631 TGO_Caller
); // certs are owned by inCertGroup
632 TPCertGroup
inCertGroup(CertGroupToBeVerified
, clHand
, cspHand
, *this,
633 cssmTimeStr
, // optional 'this' time
634 true, // firstCertMustBeValid
637 /* set up for disposal of TPCertInfos created by CertGroupConstructPriv */
638 TPCertGroup
gatheredCerts(*this, TGO_Group
);
641 CertGroupConstructPriv(
647 cred
->NumberOfAnchorCerts
,
657 verifiedViaTrustSetting
,
660 catch(const CssmError
&cerr
) {
661 constructReturn
= cerr
.error
;
662 /* abort if no certs found */
663 if(outCertGroup
.numCerts() == 0) {
664 CssmError::throwMe(constructReturn
);
666 /* else press on, collecting as much info as we can */
668 /* others are way fatal */
669 assert(outCertGroup
.numCerts() >= 1);
671 /* Infer interim status from return values */
672 switch(constructReturn
) {
673 /* these values do not get overridden */
674 case CSSMERR_TP_CERTIFICATE_CANT_OPERATE
:
675 case CSSMERR_TP_INVALID_CERT_AUTHORITY
:
676 case CSSMERR_APPLETP_TRUST_SETTING_DENY
:
677 case errSecInvalidTrustSettings
:
680 /* infer status from these values... */
681 if(verifiedToAnchor
|| verifiedViaTrustSetting
) {
682 /* full success; anchor doesn't have to be root */
683 constructReturn
= CSSM_OK
;
685 else if(verifiedToRoot
) {
686 if(actionFlags
& CSSM_TP_ACTION_IMPLICIT_ANCHORS
) {
687 constructReturn
= CSSM_OK
;
690 /* verified to root which is not an anchor */
691 constructReturn
= CSSMERR_TP_INVALID_ANCHOR_CERT
;
695 /* partial chain, no root, not verifiable by anchor */
696 constructReturn
= CSSMERR_TP_NOT_TRUSTED
;
700 * Those errors can be allowed, cert-chain-wide, per individual
701 * certs' allowedErrors
703 if((constructReturn
!= CSSM_OK
) &&
704 outCertGroup
.isAllowedError(constructReturn
)) {
705 constructReturn
= CSSM_OK
;
711 * Parameters passed to tp_policyVerify() and which vary per policy
715 const CSSM_APPLE_TP_SSL_OPTIONS
*sslOpts
;
716 CSSM_RETURN thisPolicyRtn
= CSSM_OK
; // returned from tp_policyVerify()
718 /* common CRL verify parameters */
719 TPCrlGroup
*crlGroup
= NULL
;
721 crlGroup
= new TPCrlGroup(&VerifyContext
->Crls
,
724 NULL
, // cssmTimeStr - we want CRLs that are valid 'now'
727 catch(const CssmError
&cerr
) {
728 CSSM_RETURN cr
= cerr
.error
;
729 /* I don't see a straightforward way to report this error,
730 * other than adding it to the leaf cert's status... */
731 outCertGroup
.certAtIndex(0)->addStatusCode(cr
);
732 tpDebug("CertGroupVerify: error constructing CrlGroup; continuing\n");
734 /* others are way fatal */
736 TPVerifyContext
revokeVfyContext(*this,
740 cred
->NumberOfAnchorCerts
,
745 * This may consist of certs gathered from the net (which is the purpose
746 * of this argument) and from DLDBs (a side-effect optimization).
750 kRevokeNone
, // policy
753 NULL
, // OCSP options
759 /* true if we're to execute tp_policyVerify at end of loop */
761 /* true if we're to execute a revocation policy at end of loop */
762 bool doRevocationPolicy
;
764 /* grind thru each policy */
765 for(uint32 polDex
=0; polDex
<cred
->Policy
.NumberOfPolicyIds
; polDex
++) {
766 if(cred
->Policy
.PolicyIds
== NULL
) {
767 policyReturn
= CSSMERR_TP_INVALID_POLICY_IDENTIFIERS
;
770 CSSM_FIELD_PTR policyId
= &cred
->Policy
.PolicyIds
[polDex
];
771 const CSSM_DATA
*fieldVal
= &policyId
->FieldValue
;
772 const CSSM_OID
*oid
= &policyId
->FieldOid
;
773 thisPolicyRtn
= CSSM_OK
;
774 doPolicyVerify
= false;
775 doRevocationPolicy
= false;
778 /* first the basic cert policies */
779 doPolicyVerify
= checkPolicyOid(*oid
, tpPolicy
);
781 /* some basic checks... */
782 bool policyAbort
= false;
786 case kTP_PKINIT_Client
:
787 case kTP_PKINIT_Server
:
788 if(fieldVal
->Data
!= NULL
) {
789 policyReturn
= CSSMERR_TP_INVALID_POLICY_IDENTIFIERS
;
800 #if TP_PKINIT_SERVER_HACK
801 if(tpPolicy
== kTP_PKINIT_Server
) {
802 /* possible override of "root not in anchors" */
803 if(constructReturn
== CSSMERR_TP_INVALID_ANCHOR_CERT
) {
804 if(tpCheckPkinitServerCert(outCertGroup
)) {
805 constructReturn
= CSSM_OK
;
809 #endif /* TP_PKINIT_SERVER_HACK */
813 * Now revocation policies. Note some fields in revokeVfyContext can
814 * accumulate across multiple policy calls, e.g., signerCerts.
816 else if(tpCompareOids(oid
, &CSSMOID_APPLE_TP_REVOCATION_CRL
)) {
817 /* CRL-specific options */
818 const CSSM_APPLE_TP_CRL_OPTIONS
*crlOpts
;
819 crlOpts
= (CSSM_APPLE_TP_CRL_OPTIONS
*)fieldVal
->Data
;
820 thisPolicyRtn
= CSSM_OK
;
821 if(crlOpts
!= NULL
) {
822 switch(crlOpts
->Version
) {
823 case CSSM_APPLE_TP_CRL_OPTS_VERSION
:
824 if(fieldVal
->Length
!=
825 sizeof(CSSM_APPLE_TP_CRL_OPTIONS
)) {
827 CSSMERR_TP_INVALID_POLICY_IDENTIFIERS
;
831 /* handle backwards compatibility here if necessary */
833 thisPolicyRtn
= CSSMERR_TP_INVALID_POLICY_IDENTIFIERS
;
836 if(thisPolicyRtn
!= CSSM_OK
) {
837 policyReturn
= thisPolicyRtn
;
841 revokeVfyContext
.policy
= kRevokeCrlBasic
;
842 revokeVfyContext
.crlOpts
= crlOpts
;
843 doRevocationPolicy
= true;
845 else if(tpCompareOids(oid
, &CSSMOID_APPLE_TP_REVOCATION_OCSP
)) {
846 /* OCSP-specific options */
847 const CSSM_APPLE_TP_OCSP_OPTIONS
*ocspOpts
;
848 ocspOpts
= (CSSM_APPLE_TP_OCSP_OPTIONS
*)fieldVal
->Data
;
849 thisPolicyRtn
= CSSM_OK
;
850 if(ocspOpts
!= NULL
) {
851 switch(ocspOpts
->Version
) {
852 case CSSM_APPLE_TP_OCSP_OPTS_VERSION
:
853 if(fieldVal
->Length
!=
854 sizeof(CSSM_APPLE_TP_OCSP_OPTIONS
)) {
856 CSSMERR_TP_INVALID_POLICY_IDENTIFIERS
;
860 /* handle backwards compatibility here if necessary */
862 thisPolicyRtn
= CSSMERR_TP_INVALID_POLICY_IDENTIFIERS
;
865 if(thisPolicyRtn
!= CSSM_OK
) {
866 policyReturn
= thisPolicyRtn
;
870 revokeVfyContext
.policy
= kRevokeOcsp
;
871 revokeVfyContext
.ocspOpts
= ocspOpts
;
872 doRevocationPolicy
= true;
874 /* etc. - add more policies here */
876 /* unknown TP policy OID */
877 policyReturn
= CSSMERR_TP_INVALID_POLICY_IDENTIFIERS
;
881 /* common cert policy call */
883 assert(!doRevocationPolicy
); // one at a time
884 thisPolicyRtn
= tp_policyVerify(tpPolicy
,
890 verifiedViaTrustSetting
,
893 cred
->Policy
.PolicyControl
); // not currently used
894 didCertPolicy
= true;
896 /* common revocation policy call */
897 if(doRevocationPolicy
) {
898 assert(!doPolicyVerify
); // one at a time
899 thisPolicyRtn
= tpRevocationPolicyVerify(revokeVfyContext
, outCertGroup
);
900 didRevokePolicy
= true;
902 /* See if possible error is allowed, cert-chain-wide. */
903 if((thisPolicyRtn
!= CSSM_OK
) &&
904 outCertGroup
.isAllowedError(thisPolicyRtn
)) {
905 thisPolicyRtn
= CSSM_OK
;
908 /* Now remember the error if it's the first policy
909 * error we've seen. */
910 if(policyReturn
== CSSM_OK
) {
911 policyReturn
= thisPolicyRtn
;
914 if(tpStopOn
== CSSM_TP_STOP_ON_FIRST_FAIL
) {
915 /* Nope; we're done with policy evaluation */
919 } /* for each policy */
922 * Upon completion of the above loop, perform default policy ops if
925 if((policyReturn
== CSSM_OK
) || (tpStopOn
== CSSM_TP_STOP_ON_NONE
)) {
927 policyReturn
= tp_policyVerify(kTPDefault
,
933 verifiedViaTrustSetting
,
935 NULL
, // policyFieldData
936 cred
->Policy
.PolicyControl
); // not currently used
937 /* See if error is allowed, cert-chain-wide. */
938 if((policyReturn
!= CSSM_OK
) &&
939 outCertGroup
.isAllowedError(policyReturn
)) {
940 policyReturn
= CSSM_OK
;
943 if( !didRevokePolicy
&& // no revoke policy yet
944 ( (policyReturn
== CSSM_OK
|| // default cert policy OK
945 (tpStopOn
== CSSM_TP_STOP_ON_NONE
)) // keep going anyway
948 revokeVfyContext
.policy
= TP_CRL_POLICY_DEFAULT
;
949 CSSM_RETURN thisPolicyRtn
= tpRevocationPolicyVerify(revokeVfyContext
,
951 if((thisPolicyRtn
!= CSSM_OK
) &&
952 outCertGroup
.isAllowedError(thisPolicyRtn
)) {
953 thisPolicyRtn
= CSSM_OK
;
955 if((thisPolicyRtn
!= CSSM_OK
) && (policyReturn
== CSSM_OK
)) {
956 policyReturn
= thisPolicyRtn
;
960 } /* default policy opts */
964 /* return evidence - i.e., constructed chain - if asked to */
965 if(VerifyContextResult
!= NULL
) {
967 * VerifyContextResult->Evidence[0] : CSSM_TP_APPLE_EVIDENCE_HEADER
968 * VerifyContextResult->Evidence[1] : CSSM_CERTGROUP
969 * VerifyContextResult->Evidence[2] : CSSM_TP_APPLE_EVIDENCE_INFO
971 VerifyContextResult
->NumberOfEvidences
= 3;
972 VerifyContextResult
->Evidence
=
973 (CSSM_EVIDENCE_PTR
)calloc(3, sizeof(CSSM_EVIDENCE
));
975 CSSM_TP_APPLE_EVIDENCE_HEADER
*hdr
=
976 (CSSM_TP_APPLE_EVIDENCE_HEADER
*)malloc(
977 sizeof(CSSM_TP_APPLE_EVIDENCE_HEADER
));
978 hdr
->Version
= CSSM_TP_APPLE_EVIDENCE_VERSION
;
979 CSSM_EVIDENCE_PTR ev
= &VerifyContextResult
->Evidence
[0];
980 ev
->EvidenceForm
= CSSM_EVIDENCE_FORM_APPLE_HEADER
;
983 ev
= &VerifyContextResult
->Evidence
[1];
984 ev
->EvidenceForm
= CSSM_EVIDENCE_FORM_APPLE_CERTGROUP
;
985 ev
->Evidence
= outCertGroup
.buildCssmCertGroup();
987 ev
= &VerifyContextResult
->Evidence
[2];
988 ev
->EvidenceForm
= CSSM_EVIDENCE_FORM_APPLE_CERT_INFO
;
989 ev
->Evidence
= outCertGroup
.buildCssmEvidenceInfo();
992 /* caller responsible for freeing these if they are for evidence.... */
993 outCertGroup
.freeDbRecords();
995 CSSM_RETURN outErr
= outCertGroup
.getReturnCode(constructReturn
, policyReturn
,
999 CssmError::throwMe(outErr
);