]> git.saurik.com Git - apple/security.git/blob - cdsa/cdsa_client/securestorage.h
Security-30.1.tar.gz
[apple/security.git] / cdsa / cdsa_client / securestorage.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 // securestorage - client interface to CSP DLs and their operations
21 //
22 #ifndef _H_CDSA_CLIENT_SECURESTORAGE
23 #define _H_CDSA_CLIENT_SECURESTORAGE 1
24
25 #include <Security/cspclient.h>
26 #include <Security/dlclient.h>
27 #include <Security/keyclient.h>
28
29 namespace Security
30 {
31
32 namespace CssmClient
33 {
34
35 //
36 // A CSP and a DL attachment of the same subservice
37 //
38 // This gives us 2 Object instances, but we make sure that have the same
39 // mImpl. Currently this class has no behaviour, but it will get some in
40 // the future.
41 //
42 class CSPDLImpl : public CSPImpl, public DLImpl
43 {
44 public:
45 CSPDLImpl(const Guid &guid);
46 CSPDLImpl(const Module &module);
47 virtual ~CSPDLImpl();
48
49 // Object methods.
50 bool isActive() const { return CSPImpl::isActive() || DLImpl::isActive(); }
51
52 virtual CssmAllocator &allocator() const;
53 virtual void allocator(CssmAllocator &alloc);
54
55 virtual bool operator <(const CSPDLImpl &other) const;
56 virtual bool operator ==(const CSPDLImpl &other) const;
57
58 // Attachment methods.
59 virtual CSSM_SERVICE_MASK subserviceMask() const;
60 virtual void subserviceId(uint32 id);
61
62 uint32 subserviceId() const { return CSPImpl::subserviceId(); }
63 CSSM_ATTACH_FLAGS cspFlags() const { return CSPImpl::flags(); }
64 void cspFlags(CSSM_ATTACH_FLAGS f) { CSPImpl::flags(f); }
65 CSSM_ATTACH_FLAGS dlFlags() const { return DLImpl::flags(); }
66 void dlFlags(CSSM_ATTACH_FLAGS f) { DLImpl::flags(f); }
67
68 void attach() { CSPImpl::attach(); DLImpl::attach(); }
69 void detach() { CSPImpl::detach(); DLImpl::detach(); }
70 bool attached() const { return CSPImpl::attached() || DLImpl::attached(); }
71
72 Module module() const { return CSPImpl::module(); }
73 const Guid &guid() const { return CSPImpl::guid(); }
74 CSSM_MODULE_HANDLE cspHandle() { return CSPImpl::handle(); }
75 CSSM_MODULE_HANDLE dlHandle() { return DLImpl::handle(); }
76
77 CssmSubserviceUid subserviceUid() const
78 { return CSPImpl::subserviceUid(); }
79
80 private:
81 };
82
83
84 class CSPDL : public CSP, public DL
85 {
86 public:
87 typedef CSPDLImpl Impl;
88
89 explicit CSPDL(Impl *impl) : CSP(impl), DL(impl) {}
90 CSPDL(const Guid &guid) : CSP(new Impl(guid)), DL(&CSP::impl<Impl>()) {}
91 CSPDL(const Module &module)
92 : CSP(new Impl(module)), DL(&CSP::impl<Impl>()) {}
93
94 //template <class _Impl> _Impl &impl() const
95 //{ return CSP::impl<_Impl>(); }
96
97 Impl *operator ->() const { return &CSP::impl<Impl>(); }
98 Impl &operator *() const { return CSP::impl<Impl>(); }
99
100 // Conversion operators must be here
101 bool operator !() const { return !&**this; }
102 operator bool() const { return &**this; }
103
104 bool operator <(const CSPDL &other) const
105 { return *this && other ? **this < *other : &**this < &*other; }
106 bool operator ==(const CSPDL &other) const
107 { return *this && other ? **this == *other : &**this == &*other; }
108 };
109
110
111 //
112 // SSCSPDL -- Secure storage class
113 //
114 class SSCSPDLImpl : public CSPDLImpl
115 {
116 public:
117 SSCSPDLImpl(const Guid &guid);
118 SSCSPDLImpl(const Module &module);
119 virtual ~SSCSPDLImpl();
120
121 // DbMaker
122 DbImpl *newDb(const char *inDbName, const CSSM_NET_ADDRESS *inDbLocation);
123 private:
124 };
125
126 class SSCSPDL : public CSPDL
127 {
128 public:
129 typedef SSCSPDLImpl Impl;
130
131 explicit SSCSPDL(Impl *impl) : CSPDL(impl) {}
132 SSCSPDL(const Guid &guid) : CSPDL(new Impl(guid)) {}
133 SSCSPDL(const Module &module) : CSPDL(new Impl(module)) {}
134
135 Impl *operator ->() const { return &CSP::impl<Impl>(); }
136 Impl &operator *() const { return CSP::impl<Impl>(); }
137 };
138
139
140 //
141 // SSDbImpl -- A Security Storage Db object.
142 //
143 class SSGroup;
144 class SSDbUniqueRecord;
145
146 class SSDbImpl : public DbImpl
147 {
148 public:
149 SSDbImpl(const SSCSPDL &cspdl,
150 const char *inDbName, const CSSM_NET_ADDRESS *inDbLocation);
151 virtual ~SSDbImpl();
152
153 void create();
154 void open();
155
156 SSDbUniqueRecord insert(CSSM_DB_RECORDTYPE recordType,
157 const CSSM_DB_RECORD_ATTRIBUTE_DATA *attributes,
158 const CSSM_DATA *data,
159 const CSSM_RESOURCE_CONTROL_CONTEXT *rc = NULL);
160
161 #if 0
162 SSDbUniqueRecord insert(CSSM_DB_RECORDTYPE recordType,
163 const CSSM_DB_RECORD_ATTRIBUTE_DATA *attributes,
164 const CSSM_DATA *data,
165 const AutoAclEntryInfoList &keyAcl);
166 #endif
167
168 SSDbUniqueRecord insert(CSSM_DB_RECORDTYPE recordType,
169 const CSSM_DB_RECORD_ATTRIBUTE_DATA *attributes,
170 const CSSM_DATA *data, const SSGroup &group,
171 const CSSM_ACCESS_CREDENTIALS *cred);
172
173 // DbCursorMaker
174 DbCursorImpl *newDbCursor(const CSSM_QUERY &query,
175 CssmAllocator &allocator);
176 DbCursorImpl *newDbCursor(uint32 capacity, CssmAllocator &allocator);
177
178 // SSDbUniqueRecordMaker
179 DbUniqueRecordImpl *newDbUniqueRecord();
180
181 CSP csp() { return parent<CSP>(); }
182 };
183
184 class SSDb : public Db
185 {
186 public:
187 typedef SSDbImpl Impl;
188
189 explicit SSDb(Impl *impl) : Db(impl) {}
190 SSDb(const SSCSPDL &cspdl, const char *inDbName,
191 const CSSM_NET_ADDRESS *inDbLocation)
192 : Db(cspdl->newDb(inDbName, inDbLocation)) {}
193
194 Impl *operator ->() const { return &impl<Impl>(); }
195 Impl &operator *() const { return impl<Impl>(); }
196 };
197
198
199 //
200 // SSGroup -- Group key with acl, used to protect a group of items.
201 //
202 class SSGroupImpl : public KeyImpl
203 {
204 public:
205 SSGroupImpl(const SSDb &ssDb, const CSSM_DATA &dataBlob);
206 SSGroupImpl(const SSDb &ssDb,
207 const CSSM_RESOURCE_CONTROL_CONTEXT *credAndAclEntry);
208
209 const CssmData label() const;
210 void decodeDataBlob(const CSSM_DATA &dataBlob,
211 const CSSM_ACCESS_CREDENTIALS *cred,
212 CssmAllocator &allocator, CSSM_DATA &data);
213 void encodeDataBlob(const CSSM_DATA *data,
214 const CSSM_ACCESS_CREDENTIALS *cred,
215 CssmDataContainer &dataBlob);
216
217 private:
218 // Constants
219 enum
220 {
221 // Label prefix for a secure storage group
222 kGroupMagic = FOUR_CHAR_CODE('ssgp'),
223
224 // Size of label (including prefix)
225 kLabelSize = 20,
226
227 // Size of IV
228 kIVSize = 8
229 };
230
231 CSSM_DB_ATTR_DECL(kLabel);
232
233 CssmDataContainer mLabel;
234 };
235
236 class SSGroup : public Key
237 {
238 public:
239 typedef SSGroupImpl Impl;
240 explicit SSGroup(Impl *impl) : Key(impl) {}
241
242 SSGroup() : Key(NULL) {}
243
244 // Create a new group.
245 SSGroup(const SSDb &ssDb,
246 const CSSM_RESOURCE_CONTROL_CONTEXT *credAndAclEntry)
247 : Key(new Impl(ssDb, credAndAclEntry)) {}
248
249 // Lookup an existing group based on a dataBlob.
250 SSGroup(const SSDb &ssDb, const CSSM_DATA &dataBlob)
251 : Key(new Impl(ssDb, dataBlob)) {}
252
253 Impl *operator ->() const { return &impl<Impl>(); }
254 Impl &operator *() const { return impl<Impl>(); }
255 };
256
257
258 //
259 // SSDbCursor -- Cursor for iterating over Securely Stored records (or keys)
260 //
261 class SSDbCursorImpl : public DbDbCursorImpl
262 {
263 public:
264 SSDbCursorImpl(const Db &db, const CSSM_QUERY &query,
265 CssmAllocator &allocator);
266 SSDbCursorImpl(const Db &db, uint32 capacity,
267 CssmAllocator &allocator);
268
269 bool next(DbAttributes *attributes, ::CssmDataContainer *data,
270 DbUniqueRecord &uniqueId);
271 bool next(DbAttributes *attributes, ::CssmDataContainer *data,
272 DbUniqueRecord &uniqueId, const CSSM_ACCESS_CREDENTIALS *cred);
273 bool nextKey(DbAttributes *attributes, Key &key, DbUniqueRecord &uniqueId);
274 //bool nextGroup(DbAttributes *attributes, SSGroup &group, DbUniqueRecord &uniqueId);
275
276 SSDb database() { return parent<SSDb>(); }
277 protected:
278 void activate();
279 void deactivate();
280 };
281
282 class SSDbCursor : public DbCursor
283 {
284 public:
285 typedef SSDbCursorImpl Impl;
286
287 explicit SSDbCursor(Impl *impl) : DbCursor(impl) {}
288 SSDbCursor(const SSDb &ssDb, const CSSM_QUERY &query,
289 CssmAllocator &allocator = CssmAllocator::standard())
290 : DbCursor(ssDb->newDbCursor(query, allocator)) {}
291 SSDbCursor(const SSDb &ssDb, const uint32 capacity = 0,
292 CssmAllocator &allocator = CssmAllocator::standard())
293 : DbCursor(ssDb->newDbCursor(capacity, allocator)) {}
294
295 Impl *operator ->() const { return &impl<Impl>(); }
296 Impl &operator *() const { return impl<Impl>(); }
297 };
298
299
300 //
301 // SSDbUniqueRecord
302 //
303 class SSDbUniqueRecordImpl : public DbUniqueRecordImpl
304 {
305 public:
306 SSDbUniqueRecordImpl(const Db &db);
307 virtual ~SSDbUniqueRecordImpl();
308
309 void deleteRecord();
310 void deleteRecord(const CSSM_ACCESS_CREDENTIALS *cred);
311 void modify(CSSM_DB_RECORDTYPE recordType,
312 const CSSM_DB_RECORD_ATTRIBUTE_DATA *attributes,
313 const CSSM_DATA *data,
314 CSSM_DB_MODIFY_MODE modifyMode);
315 void modify(CSSM_DB_RECORDTYPE recordType,
316 const CSSM_DB_RECORD_ATTRIBUTE_DATA *attributes,
317 const CSSM_DATA *data,
318 CSSM_DB_MODIFY_MODE modifyMode,
319 const CSSM_ACCESS_CREDENTIALS *cred);
320 void get(DbAttributes *attributes, ::CssmDataContainer *data);
321 void get(DbAttributes *attributes, ::CssmDataContainer *data,
322 const CSSM_ACCESS_CREDENTIALS *cred);
323
324 SSDb database() { return parent<SSDb>(); }
325
326 // Return the group that this record is in.
327 SSGroup group();
328 };
329
330 class SSDbUniqueRecord : public DbUniqueRecord
331 {
332 public:
333 typedef SSDbUniqueRecordImpl Impl;
334
335 explicit SSDbUniqueRecord(Impl *impl) : DbUniqueRecord(impl) {}
336 SSDbUniqueRecord(const SSDb &ssDb)
337 : DbUniqueRecord(ssDb->newDbUniqueRecord()) {}
338
339 Impl *operator ->() const { return &impl<Impl>(); }
340 Impl &operator *() const { return impl<Impl>(); }
341 };
342
343 }; // end namespace CssmClient
344
345 } // end namespace Security
346
347 #endif //_H_CDSA_CLIENT_SECURESTORAGE