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