1/*
2 * Copyright (c) 2004 Apple Computer, Inc. All Rights Reserved.
3 *
4 * @APPLE_LICENSE_HEADER_START@
5 *
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
11 * file.
12 *
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.
20 *
21 * @APPLE_LICENSE_HEADER_END@
22 */
23
24#ifndef __DSX509RELATION__
25#define __DSX509RELATION__
26
27#include "PartialRelation.h"
28// #include "DirectoryServices.h"
29#include "ODBridge.h"
30
31
32/*
33	These classes define the relationship between CDSA and Open Directory
34*/
35
36// relation column numbers
37enum {kCertTypeID = 0, kCertEncodingID, kCertPrintName, kCertAlias, kCertSubject, kCertIssuer, kCertSerialNumber,
38	  kCertSubjectKeyIdentifier, kCertPublicKeyHash};
39
40const int kNumberOfX509Attributes = kCertPublicKeyHash - kCertTypeID + 1;
41
42// the "tuple" we return
43class DSX509Tuple : public Tuple
44{
45protected:
46	int mNumberOfValues;							// number of attributes
47	Value** mValues;								// the attributes themselves
48	BlobValue *mData;								// the data for this tuple
49
50public:
51	DSX509Tuple (int numberOfValues);
52	virtual ~DSX509Tuple ();
53
54	void SetValue (int i, Value* v);				// set an attribute by column number
55
56	Value* GetValue (int i);						// get an attribute
57
58	int GetNumberOfValues ();						// number of attributes
59
60	void GetData (CSSM_DATA &data);					// get the data
61	void SetData (BlobValue *value);				// set the data
62};
63
64
65
66class DSX509Relation;
67
68// a class representing a single open directory record, and the method to serialize it as a tuple
69class DSX509Record
70{
71protected:
72	DSX509Relation *mRelation;
73
74public:
75	DSX509Record (DSX509Relation* relation) : mRelation (relation) {}
76	DSX509Tuple* GetTuple (CFDataRef certData, CFStringRef original_search, DSX509Tuple *tupleList[], int maxTuples);
77};
78
79
80// a class representing a unique identifier for a record (in the CDSA sense)
81class DSX509UniqueIdentifier : public UniqueIdentifier
82{
83protected:
84	DSX509Tuple *mTuple;
85
86public:
87	DSX509UniqueIdentifier (DSX509Tuple *t);
88	virtual ~DSX509UniqueIdentifier ();
89	virtual void Export (CSSM_DB_UNIQUE_RECORD &record);
90	DSX509Tuple* GetTuple ();
91};
92
93
94
95const int kMaxTuples = 10;
96
97// a class which converts between a CDSA query and an open directory lookup
98class DSX509Query : public Query
99{
100protected:
101	DirectoryService *mDirectoryService;								// the directory service instance from which we came
102	// DSContext *mDSContext;											// our current context
103	unsigned long mRecordCount;											// the record we are currently searching
104	unsigned long mCurrentItem;											// the item we are currently searching
105	CSSM_QUERY *queryBase;												// The original query
106	ODdl_results_handle mRecordList;									// the records we are searching
107	bool validQuery;
108	bool ValidateQueryString(CSSM_DATA mailAddr);
109	Tuple* MakeTupleFromRecord (CFDataRef record);						// convert a record to a tuple
110
111	DSX509Tuple* mTupleList[kMaxTuples];								// store tuples returned from a query
112	int mNumberOfTuples;												// number of tuples stored
113	int mNextTuple;														// next tuple to be returned
114
115public:
116	DSX509Query (DSX509Relation* relation, const CSSM_QUERY *queryBase);
117	virtual ~DSX509Query ();
118
119	virtual Tuple* GetNextTuple (UniqueIdentifier *&id);				// get a tuple and return an ID that identifies it
120};
121
122
123
124class DSX509Relation : public PartialRelation
125{
126protected:
127	CSSM_CL_HANDLE mCertificateLibrary;
128
129	void InitializeCertLibrary ();										// load the CL
130
131public:
132	DirectoryService *mDirectoryService;
133
134	DSX509Relation (CSSM_DB_RECORDTYPE recordType, int numberOfColumns, columnInfoLoader *theColumnInfo);
135	virtual ~DSX509Relation ();
136
137	Query* MakeQuery (const CSSM_QUERY* query);							// convert a CSSM_QUERY object to an internal form
138	Tuple* GetTupleFromUniqueIdentifier (UniqueIdentifier* uniqueID);	// get tuple by unique ID
139	UniqueIdentifier* ImportUniqueIdentifier (CSSM_DB_UNIQUE_RECORD *uniqueRecord);	// make a unique ID from an external form
140	CSSM_CL_HANDLE GetCLHandle ();										// get the CL handle -- initialize if necessary
141};
142
143
144
145#endif
146