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 // cssmclient - common client interface to CSSM and MDS
22 #ifndef _H_CDSA_CLIENT_CSSMCLIENT
23 #define _H_CDSA_CLIENT_CSSMCLIENT 1
25 #include <Security/utilities.h>
26 #include <Security/threading.h>
27 #include <Security/globalizer.h>
28 #include <Security/cssmalloc.h>
29 #include <Security/refcount.h>
31 #include <stdio.h> // debug
40 // Forward declarations
48 // An mixin for objects that have (store) GUIDs.
49 // The GUID value is meant to be set-once constant, and can be locked handled accordingly.
53 HasGuid(const Guid
&guid
) { mGuid
= guid
; }
56 const Guid
&guid() const { return mGuid
; }
59 void setGuid(const Guid
&guid
) { mGuid
= guid
; }
67 // A CssmData initialized from a string constant.
68 // Note that the trailing null terminator is not part of the Data.
70 // @@@ This is obsoleted by CssmPolyData in <cdsa_utilities/cssmdata.h>
71 class StringData
: public CssmData
{
73 StringData(const char *s
) : CssmData(const_cast<char *>(s
), strlen(s
)) { }
74 operator char * () const { return CssmData::operator char * (); }
79 // Exceptions are based on the CssmError utility class. We add our own class of client-side exceptions.
81 class Error
: public CssmError
{
83 Error(int err
) : CssmError(err
) { }
84 CSSM_RETURN
cssmError() const;
85 virtual const char *what () const;
94 // A CssmData bundled up with a data buffer it refers to
96 template <size_t size
>
97 struct DataBuffer
: public CssmData
{
98 unsigned char buffer
[size
];
99 DataBuffer() : CssmData(buffer
, size
) { }
104 // The CssmObject abstract class models features common to different Cssm objects.
105 // It handles a tree hierarchy of objects (parent/children) safely.
109 class ObjectImpl
: virtual public RefCount
112 explicit ObjectImpl(); // Constructor for Impl objects without a parent.
113 explicit ObjectImpl(const Object
&parent
);
114 virtual ~ObjectImpl();
116 bool isActive() const { return mActive
; }
118 virtual CssmAllocator
&allocator() const;
119 virtual void allocator(CssmAllocator
&alloc
);
121 // Pointer comparison by default. Subclasses may override.
122 virtual bool operator <(const ObjectImpl
&other
) const;
123 virtual bool operator ==(const ObjectImpl
&other
) const;
125 static void check(CSSM_RETURN status
);
128 bool mActive
; // loaded, attached, etc.
129 mutable CssmAllocator
*mAllocator
; // allocator hierarchy (NULL => TBD)
131 template <class Obj
> Obj
parent() const
132 { assert(mParent
); return Obj(static_cast<typename
Obj::Impl
*>(&(*mParent
))); }
136 bool isIdle() const { return mChildCount
== 0; }
138 // {de,}allocate() assume you have locked *this
139 virtual void activate() = 0;
140 virtual void deactivate() = 0;
143 RefPointer
<ObjectImpl
> mParent
; // parent object
144 AtomicCounter
<uint32
> mChildCount
;
152 friend class ObjectImpl
;
154 typedef ObjectImpl Impl
;
155 explicit Object(Impl
*impl
) : mImpl(impl
) {}
158 // @@@ CSPDL subclass breaks if the is a static_cast
159 template <class _Impl
> _Impl
&impl() const
160 { return dynamic_cast<_Impl
&>(*mImpl
); }
163 Impl
*operator ->() const { return &(*mImpl
); }
164 Impl
&operator *() const { return *mImpl
; }
166 // @@@ Why is this needed. DbCursor which inheirits from Object wants to call this.
167 template <class _Impl
> _Impl
&checkedImpl() const
168 { return dynamic_cast<_Impl
&>(*mImpl
); }
170 bool operator !() const { return !mImpl
; }
171 operator bool() const { return mImpl
; }
173 bool operator <(const Object
&other
) const
174 { return mImpl
&& other
.mImpl
? *mImpl
< *other
.mImpl
: mImpl
< other
.mImpl
; }
175 bool operator ==(const Object
&other
) const
176 { return mImpl
&& other
.mImpl
? *mImpl
== *other
.mImpl
: mImpl
== other
.mImpl
; }
179 RefPointer
<Impl
> mImpl
;
184 // A CSSM loadable module.
185 // You rarely directly interact with these objects, but if you need to,
188 class ModuleImpl
: public ObjectImpl
, public HasGuid
191 ModuleImpl(const Guid
&guid
);
192 ModuleImpl(const Guid
&guid
, const Cssm
&session
);
193 virtual ~ModuleImpl();
195 void load() { activate(); }
196 void unload() { deactivate(); }
197 bool isLoaded() const { return isActive(); }
199 Cssm
session() const;
206 class Module
: public Object
209 typedef ModuleImpl Impl
;
210 explicit Module(Impl
*impl
) : Object(impl
) {}
211 Module() : Object(NULL
) {} // XXX This might break operator <
212 Module(const Guid
&guid
) : Object(new Impl(guid
)) {}
213 Module(const Guid
&guid
, const Cssm
&session
) : Object(new Impl(guid
, session
)) {}
215 Impl
*operator ->() const { return &impl
<Impl
>(); }
216 Impl
&operator *() const { return impl
<Impl
>(); }
221 // An Attachment object. This is the parent of all typed attachment classes.
223 class AttachmentImpl
: public ObjectImpl
226 AttachmentImpl(const Guid
&guid
, CSSM_SERVICE_TYPE subserviceType
);
227 AttachmentImpl(const Module
&module, CSSM_SERVICE_TYPE subserviceType
);
228 //AttachmentImpl(... mds reference ...);
229 virtual ~AttachmentImpl();
231 // Virtual so that subclasses can return there true mask.
232 virtual CSSM_SERVICE_MASK
subserviceMask() const;
234 CSSM_SERVICE_TYPE
subserviceType() const { return mSubserviceType
; }
235 CSSM_VERSION
version() const { return mVersion
; }
236 void version(const CSSM_VERSION
&v
) { mVersion
= v
; }
237 uint32
subserviceId() const { return mSubserviceId
; }
238 virtual void subserviceId(uint32 id
);
239 CSSM_ATTACH_FLAGS
flags() const { return mAttachFlags
; }
240 void flags(CSSM_ATTACH_FLAGS f
) { mAttachFlags
= f
; }
242 void attach() { activate(); }
243 void detach() { deactivate(); }
244 bool attached() const { return isActive(); }
246 Module
module() const;
247 const Guid
&guid() const { return module()->guid(); }
248 CSSM_MODULE_HANDLE
handle() { attach(); return mHandle
; }
250 CssmSubserviceUid
subserviceUid() const;
257 void make(CSSM_SERVICE_TYPE subserviceType
); // common constructor
259 CSSM_MODULE_HANDLE mHandle
;
261 CSSM_SERVICE_TYPE mSubserviceType
; // set by constructor
262 CSSM_VERSION mVersion
;
263 uint32 mSubserviceId
;
264 CSSM_ATTACH_FLAGS mAttachFlags
;
266 CssmAllocatorMemoryFunctions mMemoryFunctions
; // set on attach()
269 class Attachment
: public Object
272 typedef AttachmentImpl Impl
;
273 explicit Attachment(Impl
*impl
) : Object(impl
) {}
274 Attachment(const Guid
&guid
, CSSM_SERVICE_TYPE subserviceType
)
275 : Object(new Impl(guid
, subserviceType
)) {}
276 Attachment(const Module
&module, CSSM_SERVICE_TYPE subserviceType
)
277 : Object(new Impl(module, subserviceType
)) {}
278 //Attachment(... mds reference ...);
280 Impl
*operator ->() const { return &impl
<Impl
>(); }
281 Impl
&operator *() const { return impl
<Impl
>(); }
286 // A CSSM session object.
287 // You usually only have one per program, or library, or what-not.
291 class CssmImpl
: public ObjectImpl
{
292 class StandardCssm
; friend class StandardCssm
;
297 void init() { activate(); }
298 void terminate() { deactivate(); }
300 CSSM_PRIVILEGE_SCOPE
scope() const { return mScope
; }
301 void scope(CSSM_PRIVILEGE_SCOPE sc
) { mScope
= sc
; }
302 const Guid
&callerGuid() const { return mCallerGuid
; }
303 void callerGuid(const CSSM_GUID
&guid
) { mCallerGuid
= Guid::overlay(guid
); }
305 Module
autoModule(const Guid
&guid
);
308 explicit CssmImpl(bool); // internal constructor
310 void setup(); // constructor setup
316 // CSSM global configuration -- picked up on each Init
317 CSSM_VERSION mVersion
;
318 CSSM_PRIVILEGE_SCOPE mScope
;
321 // module repository: modules by guid (protected by self)
322 typedef map
<Guid
, Module
> ModuleMap
;
327 static Cssm
standard();
328 static void catchExit();
331 static void atExitHandler();
333 class StandardCssm
: public Mutex
{
335 StandardCssm() : mCssm(NULL
) { }
337 void setCssm(CssmImpl
*cssm
);
338 void unsetCssm(CssmImpl
*cssm
);
344 static ModuleNexus
<StandardCssm
> mStandard
;
347 class Cssm
: public Object
350 typedef CssmImpl Impl
;
351 explicit Cssm(Impl
*impl
) : Object(impl
) {}
352 explicit Cssm() : Object(new Impl()) {}
354 Impl
*operator ->() const { return &impl
<Impl
>(); }
355 Impl
&operator *() const { return impl
<Impl
>(); }
357 static Cssm
standard() { return CssmImpl::standard(); }
360 } // end namespace CssmClient
362 } // end namespace Security
364 #endif // _H_CDSA_CLIENT_CSSMCLIENT