]>
Commit | Line | Data |
---|---|---|
b1ab9ed8 A |
1 | /* |
2 | * Copyright (c) 2000-2004,2006 Apple Computer, Inc. All Rights Reserved. | |
3 | * | |
4 | * @APPLE_LICENSE_HEADER_START@ | |
5 | * | |
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 | |
11 | * file. | |
12 | * | |
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. | |
20 | * | |
21 | * @APPLE_LICENSE_HEADER_END@ | |
22 | */ | |
23 | ||
24 | ||
25 | // | |
26 | // Miscellaneous CSSM PODWrappers | |
27 | // | |
28 | #ifndef _H_CSSMPODS | |
29 | #define _H_CSSMPODS | |
30 | ||
31 | #include <security_utilities/utilities.h> | |
32 | #include <security_cdsa_utilities/cssmdata.h> | |
33 | #include <string> | |
34 | ||
35 | ||
36 | namespace Security { | |
37 | ||
38 | ||
39 | // | |
40 | // User-friendly GUIDs | |
41 | // | |
42 | class Guid : public PodWrapper<Guid, CSSM_GUID> { | |
43 | public: | |
44 | Guid() { /*IFDEBUG(*/ memset(this, 0, sizeof(*this)) /*)*/ ; } | |
45 | Guid(const CSSM_GUID &rGuid) { memcpy(this, &rGuid, sizeof(*this)); } | |
46 | Guid(const char *string); | |
47 | Guid(const std::string &s); | |
48 | ||
49 | Guid &operator = (const CSSM_GUID &rGuid) | |
50 | { memcpy(this, &rGuid, sizeof(CSSM_GUID)); return *this; } | |
51 | ||
52 | bool operator == (const CSSM_GUID &other) const | |
53 | { return (this == &other) || !memcmp(this, &other, sizeof(CSSM_GUID)); } | |
54 | bool operator != (const CSSM_GUID &other) const | |
55 | { return (this != &other) && memcmp(this, &other, sizeof(CSSM_GUID)); } | |
56 | bool operator < (const CSSM_GUID &other) const | |
57 | { return memcmp(this, &other, sizeof(CSSM_GUID)) < 0; } | |
58 | size_t hash() const { //@@@ revisit this hash | |
427c49bc | 59 | return Data1 + (Data2 << 3) + (Data3 << 11) + (Data4[3]) + (Data4[6] << 22); |
b1ab9ed8 A |
60 | } |
61 | ||
62 | static const unsigned stringRepLength = 38; // "{x8-x4-x4-x4-x12}" | |
63 | char *toString(char buffer[stringRepLength+1]) const; // will append \0 | |
64 | string toString() const; // make std::string | |
65 | ||
66 | private: | |
67 | void parseGuid(const char *string); | |
68 | }; | |
69 | ||
70 | class CssmGuidData : public CssmData { | |
71 | public: | |
72 | CssmGuidData(const CSSM_GUID &guid); | |
73 | ||
74 | private: | |
75 | char buffer[Guid::stringRepLength + 1]; | |
76 | }; | |
77 | ||
78 | ||
79 | // | |
80 | // User-friendly CSSM_SUBSERVICE_UIDs | |
81 | // | |
82 | class CssmSubserviceUid : public PodWrapper<CssmSubserviceUid, CSSM_SUBSERVICE_UID> { | |
83 | public: | |
84 | CssmSubserviceUid() { clearPod(); } | |
85 | CssmSubserviceUid(const CSSM_SUBSERVICE_UID &rSSuid) { memcpy(this, &rSSuid, sizeof(*this)); } | |
86 | ||
87 | CssmSubserviceUid &operator = (const CSSM_SUBSERVICE_UID &rSSuid) | |
88 | { memcpy(this, &rSSuid, sizeof(CSSM_SUBSERVICE_UID)); return *this; } | |
89 | ||
90 | bool operator == (const CSSM_SUBSERVICE_UID &other) const; | |
91 | bool operator != (const CSSM_SUBSERVICE_UID &other) const { return !(*this == other); } | |
92 | bool operator < (const CSSM_SUBSERVICE_UID &other) const; | |
93 | ||
94 | CssmSubserviceUid(const CSSM_GUID &guid, const CSSM_VERSION *version = NULL, | |
95 | uint32 subserviceId = 0, | |
96 | CSSM_SERVICE_TYPE subserviceType = CSSM_SERVICE_DL); | |
97 | ||
98 | const ::Guid &guid() const { return ::Guid::overlay(Guid); } | |
99 | uint32 subserviceId() const { return SubserviceId; } | |
100 | CSSM_SERVICE_TYPE subserviceType() const { return SubserviceType; } | |
101 | CSSM_VERSION version() const { return Version; } | |
102 | }; | |
103 | ||
104 | ||
105 | // | |
106 | // User-friendler CSSM_CRYPTO_DATA objects | |
107 | // | |
108 | class CryptoCallback { | |
109 | public: | |
110 | CryptoCallback(CSSM_CALLBACK func, void *ctx = NULL) : mFunction(func), mCtx(ctx) { } | |
111 | CSSM_CALLBACK function() const { return mFunction; } | |
112 | void *context() const { return mCtx; } | |
113 | ||
114 | CssmData operator () () const | |
115 | { | |
116 | CssmData output; | |
117 | if (CSSM_RETURN err = mFunction(&output, mCtx)) | |
118 | CssmError::throwMe(err); | |
119 | return output; | |
120 | } | |
121 | ||
122 | private: | |
123 | CSSM_CALLBACK mFunction; | |
124 | void *mCtx; | |
125 | }; | |
126 | ||
127 | class CssmCryptoData : public PodWrapper<CssmCryptoData, CSSM_CRYPTO_DATA> { | |
128 | public: | |
129 | CssmCryptoData() { } | |
130 | ||
131 | CssmCryptoData(const CssmData ¶m, CSSM_CALLBACK callback = NULL, void *ctx = NULL) | |
132 | { Param = const_cast<CssmData &>(param); Callback = callback; CallerCtx = ctx; } | |
133 | ||
134 | CssmCryptoData(const CssmData ¶m, CryptoCallback &cb) | |
135 | { Param = const_cast<CssmData &>(param); Callback = cb.function(); CallerCtx = cb.context(); } | |
136 | ||
137 | CssmCryptoData(CSSM_CALLBACK callback, void *ctx = NULL) | |
138 | { /* ignore Param */ Callback = callback; CallerCtx = ctx; } | |
139 | ||
140 | explicit CssmCryptoData(CryptoCallback &cb) | |
141 | { /* ignore Param */ Callback = cb.function(); CallerCtx = cb.context(); } | |
142 | ||
143 | // member access | |
144 | CssmData ¶m() { return CssmData::overlay(Param); } | |
145 | const CssmData ¶m() const { return CssmData::overlay(Param); } | |
146 | bool hasCallback() const { return Callback != NULL; } | |
147 | CryptoCallback callback() const { return CryptoCallback(Callback, CallerCtx); } | |
148 | ||
149 | // get the value, whichever way is appropriate | |
150 | CssmData operator () () const | |
151 | { return hasCallback() ? callback() () : param(); } | |
152 | }; | |
153 | ||
154 | // a CssmCryptoContext whose callback is a virtual class member | |
155 | class CryptoDataClass : public CssmCryptoData { | |
156 | public: | |
157 | CryptoDataClass() : CssmCryptoData(callbackShim, this) { } | |
158 | virtual ~CryptoDataClass(); | |
159 | ||
160 | protected: | |
161 | virtual CssmData yield() = 0; // must subclass and implement this | |
162 | ||
163 | private: | |
164 | static CSSM_RETURN callbackShim(CSSM_DATA *output, void *ctx); | |
165 | }; | |
166 | ||
167 | ||
168 | // | |
169 | // Other PodWrappers for stuff that is barely useful... | |
170 | // | |
171 | class CssmKeySize : public PodWrapper<CssmKeySize, CSSM_KEY_SIZE> { | |
172 | public: | |
173 | CssmKeySize() { } | |
174 | CssmKeySize(uint32 nom, uint32 eff) { LogicalKeySizeInBits = nom; EffectiveKeySizeInBits = eff; } | |
175 | CssmKeySize(uint32 size) { LogicalKeySizeInBits = EffectiveKeySizeInBits = size; } | |
176 | ||
177 | uint32 logical() const { return LogicalKeySizeInBits; } | |
178 | uint32 effective() const { return EffectiveKeySizeInBits; } | |
179 | operator uint32 () const { return effective(); } | |
180 | }; | |
181 | ||
182 | inline bool operator == (const CSSM_KEY_SIZE &s1, const CSSM_KEY_SIZE &s2) | |
183 | { | |
184 | return s1.LogicalKeySizeInBits == s2.LogicalKeySizeInBits | |
185 | && s1.EffectiveKeySizeInBits == s2.EffectiveKeySizeInBits; | |
186 | } | |
187 | ||
188 | inline bool operator != (const CSSM_KEY_SIZE &s1, const CSSM_KEY_SIZE &s2) | |
189 | { return !(s1 == s2); } | |
190 | ||
191 | ||
192 | class QuerySizeData : public PodWrapper<QuerySizeData, CSSM_QUERY_SIZE_DATA> { | |
193 | public: | |
194 | QuerySizeData() { } | |
195 | QuerySizeData(uint32 in) { SizeInputBlock = in; SizeOutputBlock = 0; } | |
196 | ||
197 | uint32 inputSize() const { return SizeInputBlock; } | |
198 | uint32 inputSize(uint32 size) { return SizeInputBlock = size; } | |
199 | uint32 outputSize() const { return SizeOutputBlock; } | |
200 | }; | |
201 | ||
202 | inline bool operator == (const CSSM_QUERY_SIZE_DATA &s1, const CSSM_QUERY_SIZE_DATA &s2) | |
203 | { | |
204 | return s1.SizeInputBlock == s2.SizeInputBlock | |
205 | && s1.SizeOutputBlock == s2.SizeOutputBlock; | |
206 | } | |
207 | ||
208 | inline bool operator != (const CSSM_QUERY_SIZE_DATA &s1, const CSSM_QUERY_SIZE_DATA &s2) | |
209 | { return !(s1 == s2); } | |
210 | ||
211 | ||
212 | class CSPOperationalStatistics : | |
213 | public PodWrapper<CSPOperationalStatistics, CSSM_CSP_OPERATIONAL_STATISTICS> { | |
214 | public: | |
215 | }; | |
216 | ||
217 | ||
218 | } // end namespace Security | |
219 | ||
220 | ||
221 | #endif //_H_CSSMPODS |