2 * Copyright (c) 2002 Apple Computer, 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 * TPCrlInfo.h - TP's private CRL and CRL group
22 * Written 9/30/2002 by Doug Mitchell.
25 #include "TPCrlInfo.h"
26 #include "tpdebugging.h"
27 #include "certGroupUtils.h"
28 #include "tpCrlVerify.h"
29 #include "tpPolicies.h"
30 #include <Security/cssmapi.h>
31 #include <Security/x509defs.h>
32 #include <Security/oidscert.h>
33 #include <Security/oidscrl.h>
34 #include <string.h> /* for memcmp */
35 #include <Security/cssmapple.h>
38 * Replacement for CSSM_CL_CrlGetFirstCachedFieldValue for use with
39 * TPCrlItemInfo's generic getFirstCachedField mechanism.
41 static CSSM_RETURN
tpGetFirstCachedFieldValue (CSSM_CL_HANDLE CLHandle
,
42 CSSM_HANDLE CrlHandle
,
43 const CSSM_OID
*CrlField
,
44 CSSM_HANDLE_PTR ResultsHandle
,
45 uint32
*NumberOfMatchedFields
,
48 return CSSM_CL_CrlGetFirstCachedFieldValue(CLHandle
,
50 NULL
, // const CSSM_DATA *CrlRecordIndex,
53 NumberOfMatchedFields
,
57 static const TPClItemCalls tpCrlClCalls
=
59 tpGetFirstCachedFieldValue
,
60 CSSM_CL_CrlAbortQuery
,
62 CSSM_CL_CrlAbortCache
,
64 &CSSMOID_X509V1CRLThisUpdate
,
65 &CSSMOID_X509V1CRLNextUpdate
,
66 CSSMERR_TP_INVALID_CRL_POINTER
,
67 CSSMERR_APPLETP_CRL_EXPIRED
,
68 CSSMERR_APPLETP_CRL_NOT_VALID_YET
73 * No default constructor - this is the only way.
74 * This caches the cert and fetches subjectName and issuerName
75 * to ensure the incoming certData is well-constructed.
78 CSSM_CL_HANDLE clHand
,
79 CSSM_CSP_HANDLE cspHand
,
80 const CSSM_DATA
*crlData
,
81 TPItemCopy copyCrlData
, // true: we copy, we free
82 // false - caller owns
83 const char *verifyTime
) // = NULL
85 : TPClItemInfo(clHand
, cspHand
, tpCrlClCalls
, crlData
,
86 copyCrlData
, verifyTime
),
89 mFromWhere(CFW_Nowhere
),
91 mCrlFieldToFree(NULL
),
92 mVerifyState(CVS_Unknown
),
93 mVerifyError(CSSMERR_TP_INTERNAL_ERROR
)
100 /* fetch parsed CRL */
101 crtn
= fetchField(&CSSMOID_X509V2CRLSignedCrlCStruct
, &mCrlFieldToFree
);
105 CssmError::throwMe(crtn
);
107 if(mCrlFieldToFree
->Length
!= sizeof(CSSM_X509_SIGNED_CRL
)) {
108 tpErrorLog("fetchField(SignedCrlCStruct) length error\n");
110 CssmError::throwMe(CSSMERR_TP_INTERNAL_ERROR
);
112 mX509Crl
= (CSSM_X509_SIGNED_CRL
*)mCrlFieldToFree
->Data
;
113 /* any other other commonly used fields? */
116 TPCrlInfo::~TPCrlInfo()
121 void TPCrlInfo::releaseResources()
123 if(mCrlFieldToFree
) {
124 freeField(&CSSMOID_X509V2CRLSignedCrlCStruct
, mCrlFieldToFree
);
125 mCrlFieldToFree
= NULL
;
128 CssmAllocator::standard().free(mUri
.Data
);
132 TPClItemInfo::releaseResources();
135 void TPCrlInfo::uri(const CSSM_DATA
&uri
)
137 tpCopyCssmData(CssmAllocator::standard(), &uri
, &mUri
);
141 * List of extensions we understand and can accept as critical.
143 static const CSSM_OID
*const TPGoodCrlExtens
[] =
146 /* Note NOT CSSMOID_DeltaCrlIndicator! That's fatal */
149 &CSSMOID_IssuingDistributionPoint
,
150 &CSSMOID_HoldInstructionCode
,
151 &CSSMOID_InvalidityDate
,
152 &CSSMOID_AuthorityKeyIdentifier
,
153 &CSSMOID_SubjectAltName
,
154 &CSSMOID_IssuerAltName
157 #define NUM_KNOWN_EXTENS (sizeof(TPGoodCrlExtens) / sizeof(CSSM_OID_PTR))
160 * Do our best to understand all the entries in a CSSM_X509_EXTENSIONS,
161 * which may be per-CRL or per-entry.
163 * For now, we just ensure that for every critical extension,
164 * we actually understand it and can deal it.
166 CSSM_RETURN
TPCrlInfo::parseExtensions(
167 TPCrlVerifyContext
&vfyCtx
,
169 uint32 entryIndex
, // if isPerEntry
170 const CSSM_X509_EXTENSIONS
&extens
,
171 TPCertInfo
*forCert
, // optional
172 bool &isIndirectCrl
) // RETURNED
174 isIndirectCrl
= false;
175 for(uint32 dex
=0; dex
<extens
.numberOfExtensions
; dex
++) {
176 CSSM_X509_EXTENSION_PTR exten
= &extens
.extensions
[dex
];
177 if(exten
->critical
) {
178 /* critical: is it in our list of understood extensions? */
180 for(i
=0; i
<NUM_KNOWN_EXTENS
; i
++) {
181 if(tpCompareOids(&exten
->extnId
, TPGoodCrlExtens
[i
])) {
182 /* we're cool with this one */
186 if(i
== NUM_KNOWN_EXTENS
) {
187 tpCrlDebug("parseExtensions: Unknown Critical Extension\n");
188 return CSSMERR_APPLETP_UNKNOWN_CRL_EXTEN
;
192 /* Specific extension handling. */
193 if(tpCompareOids(&exten
->extnId
,
194 &CSSMOID_IssuingDistributionPoint
)) {
196 * If this assertion fails, we're out of sync with the CL
198 assert(exten
->format
== CSSM_X509_DATAFORMAT_PAIR
);
199 CE_IssuingDistributionPoint
*idp
=
200 (CE_IssuingDistributionPoint
*)
201 exten
->value
.parsedValue
;
204 * Snag indirectCrl flag for caller in any case
206 if(idp
->indirectCrlPresent
&& idp
->indirectCrl
) {
207 isIndirectCrl
= true;
209 if(forCert
!= NULL
) {
210 /* If no target cert, i.e., we're just verifying a CRL,
211 * skip the remaining IDP checks. */
213 /* verify onlyCACerts/onlyUserCerts */
215 if(forCert
->isLeaf() &&
216 !(vfyCtx
.actionFlags
&& CSSM_TP_ACTION_LEAF_IS_CA
)) {
222 if((idp
->onlyUserCertsPresent
) && (idp
->onlyUserCerts
)) {
224 tpCrlDebug("parseExtensions: onlyUserCerts, "
226 return CSSMERR_APPLETP_IDP_FAIL
;
229 if((idp
->onlyCACertsPresent
) && (idp
->onlyCACerts
)) {
231 tpCrlDebug("parseExtensions: onlyCACerts, leaf\n");
232 return CSSMERR_APPLETP_IDP_FAIL
;
236 } /* have target cert */
243 * The heavyweight "perform full verification of this CRL" op.
244 * Must verify to an anchor cert in tpVerifyContext.
245 * Intermediate certs can come from signerCerts or dBList.
247 CSSM_RETURN
TPCrlInfo::verifyWithContext(
248 TPCrlVerifyContext
&tpVerifyContext
,
249 TPCertInfo
*forCert
, // optional
253 * Step 1: this CRL must be current. Caller might have re-evaluated
254 * expired/notValidYet since our construction via calculateCurrent().
257 return CSSMERR_APPLETP_CRL_EXPIRED
;
259 if(isNotValidYet()) {
260 return CSSMERR_APPLETP_CRL_NOT_VALID_YET
;
263 /* subsequent verify state is cached */
264 switch(mVerifyState
) {
272 tpErrorLog("verifyWithContext: bad verifyState\n");
273 return CSSMERR_TP_INTERNAL_ERROR
;
277 * Step 2: parse & understand all critical CRL extensions.
281 crtn
= parseExtensions(tpVerifyContext
,
284 mX509Crl
->tbsCertList
.extensions
,
288 mVerifyState
= CVS_Bad
;
290 forCert
->addStatusCode(crtn
);
294 CSSM_X509_REVOKED_CERT_LIST_PTR revoked
=
295 mX509Crl
->tbsCertList
.revokedCertificates
;
296 if(revoked
!= NULL
) {
297 for(uint32 dex
=0; dex
<revoked
->numberOfRevokedCertEntries
; dex
++) {
298 bool dummyIsIndirect
; // can't be set here
299 crtn
= parseExtensions(tpVerifyContext
,
302 revoked
->revokedCertEntry
[dex
].extensions
,
306 mVerifyState
= CVS_Bad
;
308 forCert
->addStatusCode(crtn
);
316 * Step 3: obtain a fully verified cert chain which verifies this CRL.
318 CSSM_BOOL verifiedToRoot
;
319 CSSM_BOOL verifiedToAnchor
;
321 TPCertGroup
outCertGroup(tpVerifyContext
.alloc
,
322 TGO_Caller
); // CRLs owned by inCertGroup
324 /* set up for disposal of TPCertInfos created by
325 * CertGroupConstructPriv */
326 TPCertGroup
certsToBeFreed(tpVerifyContext
.alloc
, TGO_Group
);
328 if(tpVerifyContext
.signerCerts
) {
329 /* start from scratch with this group */
330 tpVerifyContext
.signerCerts
->setAllUnused();
332 crtn
= outCertGroup
.buildCertGroup(
333 *this, // subject item
334 tpVerifyContext
.signerCerts
, // inCertGroup, optional
335 tpVerifyContext
.dbList
, // optional
336 tpVerifyContext
.clHand
,
337 tpVerifyContext
.cspHand
,
338 tpVerifyContext
.verifyTime
,
339 tpVerifyContext
.numAnchorCerts
,
340 tpVerifyContext
.anchorCerts
,
342 tpVerifyContext
.gatheredCerts
,
343 CSSM_FALSE
, // subjectIsInGroup
344 tpVerifyContext
.actionFlags
,
348 tpCrlDebug("TPCrlInfo::verifyWithContext buildCertGroup failure "
349 "index %u", index());
351 forCert
->addStatusCode(crtn
);
355 if(!verifiedToAnchor
) {
357 mVerifyState
= CVS_Bad
;
359 /* verified to root which is not an anchor */
360 tpCrlDebug("TPCrlInfo::verifyWithContext root, no anchor, "
361 "index %u", index());
362 crtn
= CSSMERR_APPLETP_CRL_INVALID_ANCHOR_CERT
;
365 /* partial chain, no root, not verifiable by anchor */
366 tpCrlDebug("TPCrlInfo::verifyWithContext no root, no anchor, "
367 "index %u", index());
368 crtn
= CSSMERR_APPLETP_CRL_NOT_TRUSTED
;
371 forCert
->addStatusCode(crtn
);
377 * Step 4: policy verification on the returned cert group
378 * We need to (temporarily) assert the "leaf cert is a CA" flag
381 outCertGroup
.certAtIndex(0)->isLeaf(true);
382 crtn
= tp_policyVerify(kCrlPolicy
,
383 tpVerifyContext
.alloc
,
384 tpVerifyContext
.clHand
,
385 tpVerifyContext
.cspHand
,
388 tpVerifyContext
.actionFlags
| CSSM_TP_ACTION_LEAF_IS_CA
,
390 NULL
); // policyOpts, not currently used
392 tpCrlDebug(" ...verifyWithContext policy FAILURE CRL %u",
395 forCert
->addStatusCode(CSSMERR_APPLETP_CRL_POLICY_FAIL
);
397 mVerifyState
= CVS_Bad
;
402 * Step 5: recursively perform CRL verification on the certs
403 * gathered to verify this CRL.
404 * Only performed if this CRL is an indirect CRL or the caller
405 * explicitly told us to do this (i.e., caller is verifying a
406 * CRL, not a cert chain).
408 if(isIndirectCrl
|| doCrlVerify
) {
409 tpCrlDebug("verifyWithContext recursing to "
410 "tpVerifyCertGroupWithCrls");
411 crtn
= tpVerifyCertGroupWithCrls(outCertGroup
,
414 tpCrlDebug(" ...verifyWithContext CRL reverify FAILURE CRL %u",
417 forCert
->addStatusCode(crtn
);
419 mVerifyState
= CVS_Bad
;
424 tpCrlDebug(" ...verifyWithContext CRL %u SUCCESS", index());
425 mVerifyState
= CVS_Good
;
430 * Do I have the same issuer as the specified subject cert? Returns
433 bool TPCrlInfo::hasSameIssuer(
434 const TPCertInfo
&subject
)
436 assert(subject
.issuerName() != NULL
);
437 if(tpCompareCssmData(issuerName(), subject
.issuerName())) {
446 * Determine if specified cert has been revoked. Assumes that
447 * the current CRL has been fully verified.
449 * Assumes current CRL is verified good and that issuer names of
450 * the cert and CRL match.
452 * This duplicates similar logic in the CL, but to avoid re-parsing
453 * the subject cert (which we have parsed and cached), we just do it
456 * Possible errors are
457 * CSSMERR_TP_CERT_REVOKED
458 * CSSMERR_TP_CERT_SUSPENDED
461 * Error status is added to subjectCert.
463 CSSM_RETURN
TPCrlInfo::isCertRevoked(
464 TPCertInfo
&subjectCert
)
466 assert(mVerifyState
== CVS_Good
);
467 CSSM_X509_TBS_CERTLIST_PTR tbs
= &mX509Crl
->tbsCertList
;
469 /* trivial case - empty CRL */
470 if((tbs
->revokedCertificates
== NULL
) ||
471 (tbs
->revokedCertificates
->numberOfRevokedCertEntries
== 0)) {
472 tpCrlDebug(" isCertRevoked: empty CRL at index %u", index());
476 /* is subject cert's serial number in this CRL? */
477 CSSM_DATA_PTR subjSerial
= NULL
;
479 crtn
= subjectCert
.fetchField(&CSSMOID_X509V1SerialNumber
, &subjSerial
);
481 /* should never happen */
482 tpErrorLog("TPCrlInfo:isCertRevoked: error fetching serial number\n");
483 subjectCert
.addStatusCode(crtn
);
486 /* subsequent errors to errOut: */
488 uint32 numEntries
= tbs
->revokedCertificates
->numberOfRevokedCertEntries
;
489 CSSM_X509_REVOKED_CERT_ENTRY_PTR entries
=
490 tbs
->revokedCertificates
->revokedCertEntry
;
492 for(uint32 dex
=0; dex
<numEntries
; dex
++) {
493 CSSM_X509_REVOKED_CERT_ENTRY_PTR entry
= &entries
[dex
];
494 if(tpCompareCssmData(subjSerial
, &entry
->certificateSerialNumber
)) {
497 * FIXME: we're assuming that we don't have to compare
498 * the "current verification time" (the verifyTime argument
499 * to both our and the TPCertInfo's constructor) to this
500 * entry's revocationDate. That would imply that a CRL could
501 * contain a future revocation, and I don't think that
502 * X509//RFC2459 intends this.
505 * REQUIRED TBD: parse the entry's extensions, specifically to
506 * get a reason. This will entail a bunch of new TP/cert specific
508 * For now, just flag it revoked.
510 crtn
= CSSMERR_TP_CERT_REVOKED
;
511 tpCrlDebug(" isCertRevoked: cert %u REVOKED by CRL %u",
512 subjectCert
.index(), index());
517 subjectCert
.freeField(&CSSMOID_X509V1SerialNumber
, subjSerial
);
519 subjectCert
.addStatusCode(crtn
);
528 /* build empty group */
529 TPCrlGroup::TPCrlGroup(
530 CssmAllocator
&alloc
,
531 TPGroupOwner whoOwns
) :
538 /* nothing for now */
542 * Construct from unordered, untrusted CSSM_CRLGROUP. Resulting
543 * TPCrlInfos are more or less in the same order as the incoming
544 * CRLs, though incoming CRLs are discarded if they don't parse.
545 * No verification of any sort is performed.
547 TPCrlGroup::TPCrlGroup(
548 const CSSM_CRLGROUP
*cssmCrlGroup
, // optional
549 CSSM_CL_HANDLE clHand
,
550 CSSM_CSP_HANDLE cspHand
,
551 CssmAllocator
&alloc
,
552 const char *verifyTime
, // may be NULL
553 TPGroupOwner whoOwns
) :
560 /* verify input args */
561 if((cssmCrlGroup
== NULL
) || (cssmCrlGroup
->NumberOfCrls
== 0)) {
564 if(cspHand
== CSSM_INVALID_HANDLE
) {
565 CssmError::throwMe(CSSMERR_TP_INVALID_CSP_HANDLE
);
567 if(clHand
== CSSM_INVALID_HANDLE
) {
568 CssmError::throwMe(CSSMERR_TP_INVALID_CL_HANDLE
);
570 if(cssmCrlGroup
->CrlGroupType
!= CSSM_CRLGROUP_DATA
) {
571 CssmError::throwMe(CSSMERR_TP_INVALID_CERTGROUP
);
573 switch(cssmCrlGroup
->CrlType
) {
574 case CSSM_CRL_TYPE_X_509v1
:
575 case CSSM_CRL_TYPE_X_509v2
:
578 CssmError::throwMe(CSSMERR_TP_UNKNOWN_FORMAT
);
580 switch(cssmCrlGroup
->CrlEncoding
) {
581 case CSSM_CRL_ENCODING_BER
:
582 case CSSM_CRL_ENCODING_DER
:
585 CssmError::throwMe(CSSMERR_TP_UNKNOWN_FORMAT
);
589 * Add remaining input certs to mCrlInfo.
591 TPCrlInfo
*crlInfo
= NULL
;
592 for(unsigned crlDex
=0; crlDex
<cssmCrlGroup
->NumberOfCrls
; crlDex
++) {
594 crlInfo
= new TPCrlInfo(clHand
,
596 &cssmCrlGroup
->GroupCrlList
.CrlList
[crlDex
],
597 TIC_NoCopy
, // don't copy data
601 /* just ignore this CRL */
604 crlInfo
->index(crlDex
);
610 * Deletes all TPCrlInfo's if appropriate.
612 TPCrlGroup::~TPCrlGroup()
614 if(mWhoOwns
== TGO_Group
) {
616 for(i
=0; i
<mNumCrls
; i
++) {
620 mAlloc
.free(mCrlInfo
);
623 /* add/remove/access TPTCrlInfo's. */
625 * NOTE: I am aware that most folks would just use an array<> here, but
626 * gdb is so lame that it doesn't even let one examine the contents
627 * of an array<> (or just about anything else in the STL). I prefer
628 * debuggability over saving a few lines of trivial code.
630 void TPCrlGroup::appendCrl(
633 if(mNumCrls
== mSizeofCrlInfo
) {
634 if(mSizeofCrlInfo
== 0) {
635 /* appending to empty array */
641 mCrlInfo
= (TPCrlInfo
**)mAlloc
.realloc(mCrlInfo
,
642 mSizeofCrlInfo
* sizeof(TPCrlInfo
*));
644 mCrlInfo
[mNumCrls
++] = &crlInfo
;
647 TPCrlInfo
*TPCrlGroup::crlAtIndex(
650 if(index
> (mNumCrls
- 1)) {
651 CssmError::throwMe(CSSMERR_TP_INTERNAL_ERROR
);
653 return mCrlInfo
[index
];
656 TPCrlInfo
&TPCrlGroup::removeCrlAtIndex(
657 unsigned index
) // doesn't delete the cert, just
658 // removes it from our list
660 if(index
> (mNumCrls
- 1)) {
661 CssmError::throwMe(CSSMERR_TP_INTERNAL_ERROR
);
663 TPCrlInfo
&rtn
= *mCrlInfo
[index
];
665 /* removed requested element and compact remaining array */
667 for(i
=index
; i
<(mNumCrls
- 1); i
++) {
668 mCrlInfo
[i
] = mCrlInfo
[i
+1];
674 void TPCrlGroup::removeCrl(
677 for(unsigned dex
=0; dex
<mNumCrls
; dex
++) {
678 if(mCrlInfo
[dex
] == &crlInfo
) {
679 removeCrlAtIndex(dex
);
683 tpErrorLog("TPCrlGroup::removeCrl: CRL NOT FOUND\n");
687 TPCrlInfo
*TPCrlGroup::firstCrl()
690 /* the caller really should not do this... */
691 CssmError::throwMe(CSSMERR_TP_INTERNAL_ERROR
);
698 TPCrlInfo
*TPCrlGroup::lastCrl()
701 /* the caller really should not do this... */
702 CssmError::throwMe(CSSMERR_TP_INTERNAL_ERROR
);
705 return mCrlInfo
[mNumCrls
- 1];
710 * Find a CRL whose issuer matches specified subject cert.
711 * Returned CRL has not necessarily been verified.
713 TPCrlInfo
*TPCrlGroup::findCrlForCert(
716 for(unsigned dex
=0; dex
<mNumCrls
; dex
++) {
717 TPCrlInfo
*crl
= mCrlInfo
[dex
];
718 if(crl
->hasSameIssuer(subject
)) {