]> git.saurik.com Git - apple/security.git/blob - cdsa/cdsa_utilities/cssmcred.h
Security-177.tar.gz
[apple/security.git] / cdsa / cdsa_utilities / cssmcred.h
1 /*
2 * Copyright (c) 2000-2001 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 // cssmcred - enhanced PodWrappers and construction aids for ACL credentials
21 //
22 #ifndef _CSSMCRED
23 #define _CSSMCRED
24
25 #include <Security/utilities.h>
26 #include <Security/cssmlist.h>
27 #include <Security/cssmalloc.h>
28 #include <list>
29
30 namespace Security {
31
32
33 //
34 // PodWrappers for samples and sample groups
35 //
36 class CssmSample : public PodWrapper<CssmSample, CSSM_SAMPLE> {
37 public:
38 CssmSample(const TypedList &list)
39 { TypedSample = list; Verifier = NULL; }
40 CssmSample(const TypedList &list, const CssmSubserviceUid &ver)
41 { TypedSample = list; Verifier = &ver; }
42
43 TypedList &value() { return TypedList::overlay(TypedSample); }
44 const TypedList &value() const { return TypedList::overlay(TypedSample); }
45 operator TypedList & () { return value(); }
46 operator const TypedList & () const { return value(); }
47
48 const CssmSubserviceUid *verifier() const { return CssmSubserviceUid::overlay(Verifier); }
49 const CssmSubserviceUid * &verifier() { return CssmSubserviceUid::overlayVar(Verifier); }
50 };
51
52 class SampleGroup : public PodWrapper<SampleGroup, CSSM_SAMPLEGROUP> {
53 public:
54 uint32 length() const { return NumberOfSamples; }
55
56 const CssmSample &operator [] (uint32 n) const
57 { assert(n < length()); return CssmSample::overlay(Samples[n]); }
58
59 public:
60 // extract all samples of a given sample type. return true if any found
61 // note that you get a shallow copy of the sample structures for temporary use ONLY
62 bool collect(CSSM_SAMPLE_TYPE sampleType, list<CssmSample> &samples) const;
63 };
64
65
66 //
67 // The PodWrapper for the top-level CSSM credentials structure
68 //
69 class AccessCredentials : public PodWrapper<AccessCredentials, CSSM_ACCESS_CREDENTIALS> {
70 public:
71 AccessCredentials() { clearPod(); }
72
73 const char *tag() const { return EntryTag; }
74
75 SampleGroup &samples() { return SampleGroup::overlay(Samples); }
76 const SampleGroup &samples() const { return SampleGroup::overlay(Samples); }
77
78 public:
79 static const AccessCredentials &null; // all null credential
80
81 // turn NULL into a null credential if needed
82 static const AccessCredentials *needed(const CSSM_ACCESS_CREDENTIALS *cred)
83 { return cred ? overlay(cred) : &null; }
84 };
85
86
87 //
88 // An AccessCredentials object with some construction help.
89 // Note that this is NOT a PodWrapper.
90 //
91 class AutoCredentials : public AccessCredentials {
92 public:
93 AutoCredentials(CssmAllocator &alloc);
94 AutoCredentials(CssmAllocator &alloc, uint32 nSamples);
95
96 CssmAllocator &allocator;
97
98 CssmSample &sample(uint32 n) { return getSample(n); }
99
100 CssmSample &operator += (const CssmSample &sample)
101 { return getSample(samples().length()) = sample; }
102 TypedList &operator += (const TypedList &exhibit)
103 { return (getSample(samples().length()) = exhibit).value(); }
104
105 private:
106 void init();
107 CssmSample &getSample(uint32 n);
108
109 CssmSample *sampleArray;
110 uint32 nSamples;
111 };
112
113
114 //
115 // Walkers for the CSSM API structure types.
116 // Note that there are irrational "const"s strewn about the credential sub-structures.
117 // They make it essentially impossible to incrementally construction them without
118 // violating them. Since we know what we're doing, we do.
119 //
120 namespace DataWalkers
121 {
122
123 // CssmSample (with const override)
124 template <class Action>
125 void walk(Action &operate, CssmSample &sample)
126 {
127 operate(sample);
128 walk(operate, sample.value());
129 if (sample.verifier())
130 walk(operate, sample.verifier());
131 }
132
133 template <class Action>
134 void walk(Action &operate, const CssmSample &sample)
135 { walk(operate, const_cast<CssmSample &>(sample)); }
136
137 // SampleGroup
138 template <class Action>
139 void walk(Action &operate, SampleGroup &samples)
140 {
141 operate(samples);
142 operate.blob(const_cast<CSSM_SAMPLE * &>(samples.Samples),
143 samples.length() * sizeof(CSSM_SAMPLE));
144 for (uint32 n = 0; n < samples.length(); n++)
145 walk(operate, samples[n]);
146 }
147
148 // AccessCredentials
149 template <class Action>
150 AccessCredentials *walk(Action &operate, AccessCredentials * &cred)
151 {
152 operate(cred);
153 //@@@ ignoring BaseCerts
154 walk(operate, cred->samples());
155 //@@@ ignoring challenge callback
156 return cred;
157 }
158
159 template <class Action>
160 CSSM_ACCESS_CREDENTIALS *walk(Action &operate, CSSM_ACCESS_CREDENTIALS * &cred)
161 { return walk(operate, AccessCredentials::overlayVar(cred)); }
162
163 template <class Action>
164 AutoCredentials *walk(Action &operate, AutoCredentials * &cred)
165 { return (AutoCredentials *)walk(operate, (AccessCredentials * &)cred); }
166
167
168 } // end namespace DataWalkers
169 } // end namespace Security
170
171
172 #endif //_CSSMCRED