2 * Copyright (c) 2000-2004,2006 Apple Computer, Inc. All Rights Reserved.
4 * @APPLE_LICENSE_HEADER_START@
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
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.
21 * @APPLE_LICENSE_HEADER_END@
26 // cssmwalkers - walkers for standard CSSM datatypes and wrappers
28 #ifndef _H_CSSMWALKERS
29 #define _H_CSSMWALKERS
31 #include <security_cdsa_utilities/walkers.h>
32 #include <security_cdsa_utilities/cssmdata.h>
33 #include <security_cdsa_utilities/cssmpods.h>
34 #include <security_cdsa_utilities/cssmkey.h>
39 namespace DataWalkers
{
43 // There are lots of CSSM data structures that are variable-length records
44 // of the form { count, pointer-to-array }. If you have a regular PodWrapper
45 // for it, we can enumerate the array for you right here. Minimum requirement:
46 // size_t size() const;
47 // Element &operator [] (uint32 index);
48 // // and some Element *&foo() that returns a reference-to-array-pointer
49 // and a reference walker for the element type (as returned by operator []).
51 template <class Action
, class Record
, class Element
>
52 void enumerateArray(Action
&operate
, Record
&record
, Element
*& (Record::*pointer
)())
55 Element
*&root
= (record
.*pointer
)();
56 operate
.blob(root
, record
.size() * sizeof(Element
));
57 for (uint32 ix
= 0; ix
< record
.size(); ++ix
)
58 walk(operate
, record
[ix
]);
64 // The full set of walkers for CssmData in all its forms.
66 template <class Action
>
67 void walk(Action
&operate
, CssmData
&data
)
70 operate
.blob(data
.Data
, data
.Length
);
73 template <class Action
>
74 CssmData
*walk(Action
&operate
, CssmData
* &data
)
77 operate
.blob(data
->Data
, data
->Length
);
81 template <class Action
>
82 void walk(Action
&operate
, CSSM_DATA
&data
)
83 { walk(operate
, CssmData::overlay(data
)); }
85 template <class Action
>
86 CSSM_DATA
*walk(Action
&operate
, CSSM_DATA
* &data
)
87 { return walk(operate
, CssmData::overlayVar(data
)); }
92 // Walking a C string is almost regular (the size comes from strlen()).
93 // Just make sure you honor the needsSize preference of the operator.
95 template <class Action
>
96 char *walk(Action
&operate
, char * &s
)
99 operate(s
, operate
.needsSize
? (strlen(s
) + 1) : 0);
105 // Flattener functions for common CSSM data types that have internal structure.
107 template <class Action
>
108 CssmKey
*walk(Action
&operate
, CssmKey
* &key
)
111 walk(operate
, key
->keyData());
115 template <class Action
>
116 CSSM_KEY
*walk(Action
&operate
, CSSM_KEY
* &data
)
117 { return walk(operate
, CssmKey::overlayVar(data
)); }
119 template <class Action
>
120 CssmCryptoData
*walk(Action
&operate
, CssmCryptoData
* &data
)
123 walk(operate
, data
->param());
127 template <class Action
>
128 CSSM_CRYPTO_DATA
*walk(Action
&operate
, CSSM_CRYPTO_DATA
* &data
)
129 { return walk(operate
, CssmCryptoData::overlayVar(data
)); }
131 template <class Action
>
132 void walk(Action
&operate
, CSSM_PKCS5_PBKDF2_PARAMS
&data
)
135 walk(operate
, data
.Passphrase
);
139 // Walkers for flat datatypes
141 template <class Action
>
142 CSSM_DATE_PTR
walk(Action
&operate
, CSSM_DATE_PTR
&date
)
148 template <class Action
>
149 CSSM_RANGE_PTR
walk(Action
&operate
, CSSM_RANGE_PTR
&range
)
155 template <class Action
>
156 CSSM_VERSION_PTR
walk(Action
&operate
, CSSM_VERSION_PTR
&version
)
162 template <class Action
>
163 CSSM_DL_DB_HANDLE_PTR
walk(Action
&operate
, CSSM_DL_DB_HANDLE_PTR
&dlDbHandle
)
169 template <class Action
>
170 CssmSubserviceUid
*walk(Action
&operate
, CssmSubserviceUid
* &ssUid
)
178 // A synthetic variant of CssmData to model key derivation (input) parameters,
179 // which have algorithm dependent structure. This is not likely to be useful
180 // for anything else; but here's the common ancestor of all its users.
182 class CssmDeriveData
{
184 CssmDeriveData(const CssmData
&dat
, CSSM_ALGORITHMS alg
)
185 : baseData(dat
), algorithm(alg
) { }
188 CSSM_ALGORITHMS algorithm
;
190 template <class Action
>
191 void enumerate(Action
&operate
)
193 walk(operate
, baseData
);
195 case CSSM_ALGID_PKCS5_PBKDF2
:
197 walk(operate
, *baseData
.interpretedAs
<CSSM_PKCS5_PBKDF2_PARAMS
>
198 (CSSMERR_CSP_INVALID_ATTR_ALG_PARAMS
));
200 if (baseData
.length() != sizeof(CSSM_PKCS5_PBKDF2_PARAMS
))
201 CssmError::throwMe(CSSMERR_CSP_INVALID_ATTR_ALG_PARAMS
);
202 walk(operate
, *(CSSM_PKCS5_PBKDF2_PARAMS
*)baseData
.data());
212 template <class Action
>
213 void walk(Action
&operate
, CssmDeriveData
&data
)
216 data
.enumerate(operate
);
219 template <class Action
>
220 CssmDeriveData
*walk(Action
&operate
, CssmDeriveData
* &data
)
224 data
->enumerate(operate
);
230 } // end namespace DataWalkers
231 } // end namespace Security
233 #endif //_H_CSSMWALKERS