]> git.saurik.com Git - apple/security.git/blob - cdsa/cdsa_utilities/cssmcred.h
Security-29.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(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() { memset(this, 0, sizeof(*this)); }
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
80
81 //
82 // An AccessCredentials object with some construction help.
83 // Note that this is NOT a PodWrapper.
84 //
85 class AutoCredentials : public AccessCredentials {
86 public:
87 AutoCredentials(CssmAllocator &alloc);
88 AutoCredentials(CssmAllocator &alloc, uint32 nSamples);
89
90 CssmAllocator &allocator;
91
92 CssmSample &sample(uint32 n) { return getSample(n); }
93
94 CssmSample &operator += (const CssmSample &sample)
95 { return getSample(samples().length()) = sample; }
96 TypedList &operator += (const TypedList &exhibit)
97 { return (getSample(samples().length()) = exhibit).value(); }
98
99 private:
100 void init();
101 CssmSample &getSample(uint32 n);
102
103 CssmSample *sampleArray;
104 uint32 nSamples;
105 };
106
107
108 //
109 // Walkers for the CSSM API structure types.
110 // Note that there are irrational "const"s strewn about the credential sub-structures.
111 // They make it essentially impossible to incrementally construction them without
112 // violating them. Since we know what we're doing, we do.
113 //
114 namespace DataWalkers
115 {
116
117 // CssmSample (with const override)
118 template <class Action>
119 void walk(Action &operate, CssmSample &sample)
120 {
121 walk(operate, sample.value());
122 if (sample.verifier())
123 walk(operate, sample.verifier());
124 }
125
126 template <class Action>
127 void walk(Action &operate, const CssmSample &sample)
128 { walk(operate, const_cast<CssmSample &>(sample)); }
129
130 // SampleGroup
131 template <class Action>
132 void walk(Action &operate, SampleGroup &samples)
133 {
134 operate(samples.Samples, samples.length() * sizeof(CssmSample));
135 for (uint32 n = 0; n < samples.length(); n++)
136 walk(operate, samples[n]);
137 }
138
139 // AccessCredentials
140 template <class Action>
141 AccessCredentials *walk(Action &operate, AccessCredentials * &cred)
142 {
143 operate(cred);
144 //@@@ ignoring BaseCerts
145 walk(operate, cred->samples());
146 //@@@ ignoring challenge callback
147 return cred;
148 }
149
150 template <class Action>
151 CSSM_ACCESS_CREDENTIALS *walk(Action &operate, CSSM_ACCESS_CREDENTIALS * &cred)
152 { return walk(operate, AccessCredentials::overlayVar(cred)); }
153
154
155 } // end namespace DataWalkers
156
157 } // end namespace Security
158
159 #ifdef _CPP_CSSMCRED
160 #pragma export off
161 #endif
162
163
164 #endif //_CSSMCRED