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