2 * Copyright (c) 2000-2001 Apple Computer, Inc. All Rights Reserved.
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
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.
20 // cssmcred - enhanced PodWrappers and construction aids for ACL credentials
25 #include <Security/utilities.h>
26 #include <Security/cssmlist.h>
27 #include <Security/cssmalloc.h>
34 // PodWrappers for samples and sample groups
36 class CssmSample
: public PodWrapper
<CssmSample
, CSSM_SAMPLE
> {
38 CssmSample(const TypedList
&list
)
39 { TypedSample
= list
; Verifier
= NULL
; }
40 CssmSample(const TypedList
&list
, const CssmSubserviceUid
&ver
)
41 { TypedSample
= list
; Verifier
= &ver
; }
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(); }
48 const CssmSubserviceUid
*verifier() const { return CssmSubserviceUid::overlay(Verifier
); }
49 const CssmSubserviceUid
* &verifier() { return CssmSubserviceUid::overlayVar(Verifier
); }
52 class SampleGroup
: public PodWrapper
<SampleGroup
, CSSM_SAMPLEGROUP
> {
54 uint32
length() const { return NumberOfSamples
; }
56 const CssmSample
&operator [] (uint32 n
) const
57 { assert(n
< length()); return CssmSample::overlay(Samples
[n
]); }
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;
67 // The PodWrapper for the top-level CSSM credentials structure
69 class AccessCredentials
: public PodWrapper
<AccessCredentials
, CSSM_ACCESS_CREDENTIALS
> {
71 AccessCredentials() { clearPod(); }
73 const char *tag() const { return EntryTag
; }
75 SampleGroup
&samples() { return SampleGroup::overlay(Samples
); }
76 const SampleGroup
&samples() const { return SampleGroup::overlay(Samples
); }
79 static const AccessCredentials
&null
; // all null credential
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
; }
88 // An AccessCredentials object with some construction help.
89 // Note that this is NOT a PodWrapper.
91 class AutoCredentials
: public AccessCredentials
{
93 AutoCredentials(CssmAllocator
&alloc
);
94 AutoCredentials(CssmAllocator
&alloc
, uint32 nSamples
);
96 CssmAllocator
&allocator
;
98 CssmSample
&sample(uint32 n
) { return getSample(n
); }
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(); }
107 CssmSample
&getSample(uint32 n
);
109 CssmSample
*sampleArray
;
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.
120 namespace DataWalkers
123 // CssmSample (with const override)
124 template <class Action
>
125 void walk(Action
&operate
, CssmSample
&sample
)
128 walk(operate
, sample
.value());
129 if (sample
.verifier())
130 walk(operate
, sample
.verifier());
133 template <class Action
>
134 void walk(Action
&operate
, const CssmSample
&sample
)
135 { walk(operate
, const_cast<CssmSample
&>(sample
)); }
138 template <class Action
>
139 void walk(Action
&operate
, SampleGroup
&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
]);
149 template <class Action
>
150 AccessCredentials
*walk(Action
&operate
, AccessCredentials
* &cred
)
153 //@@@ ignoring BaseCerts
154 walk(operate
, cred
->samples());
155 //@@@ ignoring challenge callback
159 template <class Action
>
160 CSSM_ACCESS_CREDENTIALS
*walk(Action
&operate
, CSSM_ACCESS_CREDENTIALS
* &cred
)
161 { return walk(operate
, AccessCredentials::overlayVar(cred
)); }
163 template <class Action
>
164 AutoCredentials
*walk(Action
&operate
, AutoCredentials
* &cred
)
165 { return (AutoCredentials
*)walk(operate
, (AccessCredentials
* &)cred
); }
168 } // end namespace DataWalkers
169 } // end namespace Security