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 // securestorage - client interface to CSP DLs and their operations
22 #ifndef _H_CDSA_CLIENT_SECURESTORAGE
23 #define _H_CDSA_CLIENT_SECURESTORAGE 1
25 #include <Security/cspclient.h>
26 #include <Security/dlclient.h>
27 #include <Security/keyclient.h>
36 // A CSP and a DL attachment of the same subservice
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
42 class CSPDLImpl
: public CSPImpl
, public DLImpl
45 CSPDLImpl(const Guid
&guid
);
46 CSPDLImpl(const Module
&module);
50 bool isActive() const { return CSPImpl::isActive() || DLImpl::isActive(); }
52 virtual CssmAllocator
&allocator() const;
53 virtual void allocator(CssmAllocator
&alloc
);
55 virtual bool operator <(const CSPDLImpl
&other
) const;
56 virtual bool operator ==(const CSPDLImpl
&other
) const;
58 // Attachment methods.
59 virtual CSSM_SERVICE_MASK
subserviceMask() const;
60 virtual void subserviceId(uint32 id
);
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
); }
68 void attach() { CSPImpl::attach(); DLImpl::attach(); }
69 void detach() { CSPImpl::detach(); DLImpl::detach(); }
70 bool attached() const { return CSPImpl::attached() || DLImpl::attached(); }
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(); }
77 CssmSubserviceUid
subserviceUid() const
78 { return CSPImpl::subserviceUid(); }
84 class CSPDL
: public CSP
, public DL
87 typedef CSPDLImpl Impl
;
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
>()) {}
94 //template <class _Impl> _Impl &impl() const
95 //{ return CSP::impl<_Impl>(); }
97 Impl
*get() const { return &CSP::impl
<Impl
>(); }
98 Impl
*operator ->() const { return &CSP::impl
<Impl
>(); }
99 Impl
&operator *() const { return CSP::impl
<Impl
>(); }
101 // Conversion operators must be here
102 bool operator !() const { return !get(); }
103 operator bool() const { return get(); }
105 bool operator <(const CSPDL
&other
) const
106 { return *this && other
? **this < *other
: get() < other
.get(); }
107 bool operator ==(const CSPDL
&other
) const
108 { return *this && other
? **this == *other
: get() == other
.get(); }
113 // SSCSPDL -- Secure storage class
115 class SSCSPDLImpl
: public CSPDLImpl
118 SSCSPDLImpl(const Guid
&guid
);
119 SSCSPDLImpl(const Module
&module);
120 virtual ~SSCSPDLImpl();
123 DbImpl
*newDb(const char *inDbName
, const CSSM_NET_ADDRESS
*inDbLocation
);
127 class SSCSPDL
: public CSPDL
130 typedef SSCSPDLImpl Impl
;
132 explicit SSCSPDL(Impl
*impl
) : CSPDL(impl
) {}
133 SSCSPDL(const Guid
&guid
) : CSPDL(new Impl(guid
)) {}
134 SSCSPDL(const Module
&module) : CSPDL(new Impl(module)) {}
136 Impl
*operator ->() const { return &CSP::impl
<Impl
>(); }
137 Impl
&operator *() const { return CSP::impl
<Impl
>(); }
142 // SSDbImpl -- A Security Storage Db object.
145 class SSDbUniqueRecord
;
147 class SSDbImpl
: public DbImpl
150 SSDbImpl(const SSCSPDL
&cspdl
,
151 const char *inDbName
, const CSSM_NET_ADDRESS
*inDbLocation
);
157 SSDbUniqueRecord
insert(CSSM_DB_RECORDTYPE recordType
,
158 const CSSM_DB_RECORD_ATTRIBUTE_DATA
*attributes
,
159 const CSSM_DATA
*data
,
160 const CSSM_RESOURCE_CONTROL_CONTEXT
*rc
= NULL
);
163 SSDbUniqueRecord
insert(CSSM_DB_RECORDTYPE recordType
,
164 const CSSM_DB_RECORD_ATTRIBUTE_DATA
*attributes
,
165 const CSSM_DATA
*data
,
166 const AutoAclEntryInfoList
&keyAcl
);
169 SSDbUniqueRecord
insert(CSSM_DB_RECORDTYPE recordType
,
170 const CSSM_DB_RECORD_ATTRIBUTE_DATA
*attributes
,
171 const CSSM_DATA
*data
, const SSGroup
&group
,
172 const CSSM_ACCESS_CREDENTIALS
*cred
);
175 DbCursorImpl
*newDbCursor(const CSSM_QUERY
&query
,
176 CssmAllocator
&allocator
);
177 DbCursorImpl
*newDbCursor(uint32 capacity
, CssmAllocator
&allocator
);
179 // SSDbUniqueRecordMaker
180 DbUniqueRecordImpl
*newDbUniqueRecord();
182 CSP
csp() { return parent
<CSP
>(); }
185 class SSDb
: public Db
188 typedef SSDbImpl Impl
;
190 explicit SSDb(Impl
*impl
) : Db(impl
) {}
191 SSDb(const SSCSPDL
&cspdl
, const char *inDbName
,
192 const CSSM_NET_ADDRESS
*inDbLocation
)
193 : Db(cspdl
->newDb(inDbName
, inDbLocation
)) {}
195 Impl
*operator ->() const { return &impl
<Impl
>(); }
196 Impl
&operator *() const { return impl
<Impl
>(); }
201 // SSGroup -- Group key with acl, used to protect a group of items.
203 class SSGroupImpl
: public KeyImpl
206 SSGroupImpl(const SSDb
&ssDb
, const CSSM_DATA
&dataBlob
);
207 SSGroupImpl(const SSDb
&ssDb
,
208 const CSSM_RESOURCE_CONTROL_CONTEXT
*credAndAclEntry
);
210 static bool isGroup(const CSSM_DATA
&dataBlob
);
212 const CssmData
label() const;
213 void decodeDataBlob(const CSSM_DATA
&dataBlob
,
214 const CSSM_ACCESS_CREDENTIALS
*cred
,
215 CssmAllocator
&allocator
, CSSM_DATA
&data
);
216 void encodeDataBlob(const CSSM_DATA
*data
,
217 const CSSM_ACCESS_CREDENTIALS
*cred
,
218 CssmDataContainer
&dataBlob
);
224 // Label prefix for a secure storage group
225 kGroupMagic
= FOUR_CHAR_CODE('ssgp'),
227 // Size of label (including prefix)
234 CSSM_DB_ATTR_DECL(kLabel
);
236 CssmDataContainer mLabel
;
239 class SSGroup
: public Key
242 typedef SSGroupImpl Impl
;
243 explicit SSGroup(Impl
*impl
) : Key(impl
) {}
245 SSGroup() : Key(NULL
) {}
247 // Create a new group.
248 SSGroup(const SSDb
&ssDb
,
249 const CSSM_RESOURCE_CONTROL_CONTEXT
*credAndAclEntry
)
250 : Key(new Impl(ssDb
, credAndAclEntry
)) {}
252 // Lookup an existing group based on a dataBlob.
253 SSGroup(const SSDb
&ssDb
, const CSSM_DATA
&dataBlob
)
254 : Key(new Impl(ssDb
, dataBlob
)) {}
256 Impl
*operator ->() const { return &impl
<Impl
>(); }
257 Impl
&operator *() const { return impl
<Impl
>(); }
262 // SSDbCursor -- Cursor for iterating over Securely Stored records (or keys)
264 class SSDbCursorImpl
: public DbDbCursorImpl
267 SSDbCursorImpl(const Db
&db
, const CSSM_QUERY
&query
,
268 CssmAllocator
&allocator
);
269 SSDbCursorImpl(const Db
&db
, uint32 capacity
,
270 CssmAllocator
&allocator
);
272 bool next(DbAttributes
*attributes
, ::CssmDataContainer
*data
,
273 DbUniqueRecord
&uniqueId
);
274 bool next(DbAttributes
*attributes
, ::CssmDataContainer
*data
,
275 DbUniqueRecord
&uniqueId
, const CSSM_ACCESS_CREDENTIALS
*cred
);
276 bool nextKey(DbAttributes
*attributes
, Key
&key
, DbUniqueRecord
&uniqueId
);
277 //bool nextGroup(DbAttributes *attributes, SSGroup &group, DbUniqueRecord &uniqueId);
279 SSDb
database() { return parent
<SSDb
>(); }
285 class SSDbCursor
: public DbCursor
288 typedef SSDbCursorImpl Impl
;
290 explicit SSDbCursor(Impl
*impl
) : DbCursor(impl
) {}
291 SSDbCursor(const SSDb
&ssDb
, const CSSM_QUERY
&query
,
292 CssmAllocator
&allocator
= CssmAllocator::standard())
293 : DbCursor(ssDb
->newDbCursor(query
, allocator
)) {}
294 SSDbCursor(const SSDb
&ssDb
, const uint32 capacity
= 0,
295 CssmAllocator
&allocator
= CssmAllocator::standard())
296 : DbCursor(ssDb
->newDbCursor(capacity
, allocator
)) {}
298 Impl
*operator ->() const { return &impl
<Impl
>(); }
299 Impl
&operator *() const { return impl
<Impl
>(); }
306 class SSDbUniqueRecordImpl
: public DbUniqueRecordImpl
309 SSDbUniqueRecordImpl(const Db
&db
);
310 virtual ~SSDbUniqueRecordImpl();
313 void deleteRecord(const CSSM_ACCESS_CREDENTIALS
*cred
);
314 void modify(CSSM_DB_RECORDTYPE recordType
,
315 const CSSM_DB_RECORD_ATTRIBUTE_DATA
*attributes
,
316 const CSSM_DATA
*data
,
317 CSSM_DB_MODIFY_MODE modifyMode
);
318 void modify(CSSM_DB_RECORDTYPE recordType
,
319 const CSSM_DB_RECORD_ATTRIBUTE_DATA
*attributes
,
320 const CSSM_DATA
*data
,
321 CSSM_DB_MODIFY_MODE modifyMode
,
322 const CSSM_ACCESS_CREDENTIALS
*cred
);
323 void get(DbAttributes
*attributes
, ::CssmDataContainer
*data
);
324 void get(DbAttributes
*attributes
, ::CssmDataContainer
*data
,
325 const CSSM_ACCESS_CREDENTIALS
*cred
);
327 SSDb
database() { return parent
<SSDb
>(); }
329 // Return the group that this record is in.
333 class SSDbUniqueRecord
: public DbUniqueRecord
336 typedef SSDbUniqueRecordImpl Impl
;
338 explicit SSDbUniqueRecord(Impl
*impl
) : DbUniqueRecord(impl
) {}
339 SSDbUniqueRecord(const SSDb
&ssDb
)
340 : DbUniqueRecord(ssDb
->newDbUniqueRecord()) {}
342 Impl
*operator ->() const { return &impl
<Impl
>(); }
343 Impl
&operator *() const { return impl
<Impl
>(); }
346 }; // end namespace CssmClient
348 } // end namespace Security
350 #endif //_H_CDSA_CLIENT_SECURESTORAGE