1/*
2 * Copyright (c) 2002 Apple Computer, Inc. All Rights Reserved.
3 *
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
8 * using this file.
9 *
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.
16 */
17
18
19/*
20 * DecodedItem.cpp - class representing the common portions of NSS-style
21 * certs and CRLs, with extensions parsed and decoded (still in NSS
22 * format).
23 */
24
25#include "DecodedItem.h"
26#include "cldebugging.h"
27#include "AppleX509CLSession.h"
28#include "CSPAttacher.h"
29#include "CLFieldsCommon.h"
30#include "clNssUtils.h"
31#include <Security/cssmapple.h>
32
33
34DecodedItem::DecodedItem(
35	AppleX509CLSession	&session)
36	:	mState(IS_Empty),
37		mAlloc(session),
38		mSession(session),
39		mDecodedExtensions(mCoder, session)
40{
41}
42
43DecodedItem::~DecodedItem()
44{
45	/* nothing for now */
46}
47
48/*
49 * Search for DecodedExten by AsnOid or "any unknown extension".
50 * Called from getField*() and inferKeyUsage.
51 * Returns NULL if specified extension not found.
52 */
53const DecodedExten *DecodedItem::findDecodedExt(
54	const CSSM_OID		&extnId,		// for known extensions
55	bool				unknown,		// otherwise
56	uint32				index,
57	uint32				&numFields) const
58{
59	unsigned dex;
60	const DecodedExten *rtnExt = NULL;
61	unsigned found = 0;
62
63	for(dex=0; dex<mDecodedExtensions.numExtensions(); dex++) {
64		const DecodedExten *decodedExt = mDecodedExtensions.getExtension(dex);
65		/*
66		 * known extensions: OID match AND successful decode (In case
67		 *    we encountered a known extension which we couldn't
68		 *    decode and fell back to giving the app an unparsed
69		 *    BER blob).
70		 * unknown extensions: just know that we didn't decode it
71		 */
72		if( ( !unknown && !decodedExt->berEncoded() &&
73		      (clCompareCssmData(&decodedExt->extnId(), &extnId))
74			) ||
75		    (unknown && decodedExt->berEncoded())
76		   ) {
77
78			if(found++ == index) {
79				/* the one we want */
80				rtnExt = decodedExt;
81			}
82			if((rtnExt != NULL) && (index != 0)) {
83				/* only determine numFields on search for first one */
84				break;
85			}
86		}
87	}
88	if(rtnExt != NULL) {
89		/* sucessful return  */
90		if(index == 0) {
91			numFields = found;
92		}
93		return rtnExt;
94	}
95	else {
96		return NULL;
97	}
98}
99
100