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 // Exceptions are based on the CssmError utility class. We add our own class of client-side exceptions.
69 class Error
: public CssmError
{
71 Error(int err
) : CssmError(err
) { }
72 CSSM_RETURN
cssmError() const;
73 virtual const char *what () const throw();
82 // The CssmObject abstract class models features common to different Cssm objects.
83 // It handles a tree hierarchy of objects (parent/children) safely.
87 class ObjectImpl
: virtual public RefCount
90 explicit ObjectImpl(); // Constructor for Impl objects without a parent.
91 explicit ObjectImpl(const Object
&parent
);
92 virtual ~ObjectImpl();
94 bool isActive() const { return mActive
; }
96 virtual CssmAllocator
&allocator() const;
97 virtual void allocator(CssmAllocator
&alloc
);
99 // Pointer comparison by default. Subclasses may override.
100 virtual bool operator <(const ObjectImpl
&other
) const;
101 virtual bool operator ==(const ObjectImpl
&other
) const;
103 static void check(CSSM_RETURN status
);
106 bool mActive
; // loaded, attached, etc.
107 mutable CssmAllocator
*mAllocator
; // allocator hierarchy (NULL => TBD)
109 template <class Obj
> Obj
parent() const
110 { assert(mParent
); return Obj(static_cast<typename
Obj::Impl
*>(&(*mParent
))); }
114 bool isIdle() const { return mChildCount
== 0; }
116 // {de,}activate() assume you have locked *this
117 virtual void activate() = 0;
118 virtual void deactivate() = 0;
121 RefPointer
<ObjectImpl
> mParent
; // parent object
122 AtomicCounter
<uint32
> mChildCount
;
128 friend class ObjectImpl
;
130 typedef ObjectImpl Impl
;
131 explicit Object(Impl
*impl
) : mImpl(impl
) {}
134 // @@@ CSPDL subclass breaks if the is a static_cast
135 template <class _Impl
> _Impl
&impl() const
136 { return dynamic_cast<_Impl
&>(*mImpl
); }
139 Impl
*operator ->() const { return &(*mImpl
); }
140 Impl
&operator *() const { return *mImpl
; }
142 // @@@ Why is this needed. DbCursor which inheirits from Object wants to call this.
143 template <class _Impl
> _Impl
&checkedImpl() const
144 { return dynamic_cast<_Impl
&>(*mImpl
); }
146 bool operator !() const { return !mImpl
; }
147 operator bool() const { return mImpl
; }
149 bool isActive() const { return mImpl
&& mImpl
->isActive(); }
150 CssmAllocator
&allocator() const { return mImpl
->allocator(); }
151 void release() { mImpl
= NULL
; }
153 bool operator <(const Object
&other
) const
154 { return mImpl
&& other
.mImpl
? *mImpl
< *other
.mImpl
: mImpl
< other
.mImpl
; }
155 bool operator ==(const Object
&other
) const
156 { return mImpl
&& other
.mImpl
? *mImpl
== *other
.mImpl
: mImpl
== other
.mImpl
; }
159 RefPointer
<Impl
> mImpl
;
164 // A CSSM loadable module.
165 // You rarely directly interact with these objects, but if you need to,
168 class ModuleImpl
: public ObjectImpl
, public HasGuid
171 ModuleImpl(const Guid
&guid
);
172 ModuleImpl(const Guid
&guid
, const Cssm
&session
);
173 virtual ~ModuleImpl();
175 void load() { activate(); }
176 void unload() { deactivate(); }
177 bool isLoaded() const { return isActive(); }
179 Cssm
session() const;
186 class Module
: public Object
189 typedef ModuleImpl Impl
;
190 explicit Module(Impl
*impl
) : Object(impl
) {}
191 Module() : Object(NULL
) {} // XXX This might break operator <
192 Module(const Guid
&guid
) : Object(new Impl(guid
)) {}
193 Module(const Guid
&guid
, const Cssm
&session
) : Object(new Impl(guid
, session
)) {}
195 Impl
*operator ->() const { return &impl
<Impl
>(); }
196 Impl
&operator *() const { return impl
<Impl
>(); }
201 // An Attachment object. This is the base class of all typed attachment classes.
203 class AttachmentImpl
: public ObjectImpl
206 AttachmentImpl(const Guid
&guid
, CSSM_SERVICE_TYPE subserviceType
);
207 AttachmentImpl(const Module
&module, CSSM_SERVICE_TYPE subserviceType
);
208 //AttachmentImpl(... mds reference ...);
209 virtual ~AttachmentImpl();
211 // Virtual so that subclasses can return there true mask.
212 virtual CSSM_SERVICE_MASK
subserviceMask() const;
214 CSSM_SERVICE_TYPE
subserviceType() const { return mSubserviceType
; }
215 CSSM_VERSION
version() const { return mVersion
; }
216 void version(const CSSM_VERSION
&v
) { mVersion
= v
; }
217 uint32
subserviceId() const { return mSubserviceId
; }
218 virtual void subserviceId(uint32 id
);
219 CSSM_ATTACH_FLAGS
flags() const { return mAttachFlags
; }
220 void flags(CSSM_ATTACH_FLAGS f
) { mAttachFlags
= f
; }
222 void attach() { activate(); }
223 void detach() { deactivate(); }
224 bool attached() const { return isActive(); }
226 Module
module() const;
227 const Guid
&guid() const { return module()->guid(); }
228 CSSM_MODULE_HANDLE
handle() { attach(); return mHandle
; }
230 CssmSubserviceUid
subserviceUid() const;
237 void make(CSSM_SERVICE_TYPE subserviceType
); // common constructor
239 CSSM_MODULE_HANDLE mHandle
;
241 CSSM_SERVICE_TYPE mSubserviceType
; // set by constructor
242 CSSM_VERSION mVersion
;
243 uint32 mSubserviceId
;
244 CSSM_ATTACH_FLAGS mAttachFlags
;
246 CssmAllocatorMemoryFunctions mMemoryFunctions
; // set on attach()
249 class Attachment
: public Object
252 typedef AttachmentImpl Impl
;
253 explicit Attachment(Impl
*impl
) : Object(impl
) {}
254 Attachment(const Guid
&guid
, CSSM_SERVICE_TYPE subserviceType
)
255 : Object(new Impl(guid
, subserviceType
)) {}
256 Attachment(const Module
&module, CSSM_SERVICE_TYPE subserviceType
)
257 : Object(new Impl(module, subserviceType
)) {}
258 //Attachment(... mds reference ...);
260 Impl
*operator ->() const { return &impl
<Impl
>(); }
261 Impl
&operator *() const { return impl
<Impl
>(); }
266 // A CSSM session object.
267 // You usually only have one per program, or library, or what-not.
271 class CssmImpl
: public ObjectImpl
{
272 class StandardCssm
; friend class StandardCssm
;
277 void init() { activate(); }
278 void terminate() { deactivate(); }
280 CSSM_PRIVILEGE_SCOPE
scope() const { return mScope
; }
281 void scope(CSSM_PRIVILEGE_SCOPE sc
) { mScope
= sc
; }
282 const Guid
&callerGuid() const { return mCallerGuid
; }
283 void callerGuid(const CSSM_GUID
&guid
) { mCallerGuid
= Guid::overlay(guid
); }
285 Module
autoModule(const Guid
&guid
);
288 explicit CssmImpl(bool); // internal constructor
290 void setup(); // constructor setup
296 // CSSM global configuration -- picked up on each Init
297 CSSM_VERSION mVersion
;
298 CSSM_PRIVILEGE_SCOPE mScope
;
301 // module repository: modules by guid (protected by self)
302 typedef map
<Guid
, Module
> ModuleMap
;
307 static Cssm
standard();
308 static void catchExit();
311 static void atExitHandler();
313 class StandardCssm
: public Mutex
{
315 StandardCssm() : mCssm(NULL
) { }
317 void setCssm(CssmImpl
*cssm
);
318 void unsetCssm(CssmImpl
*cssm
);
324 static ModuleNexus
<StandardCssm
> mStandard
;
327 class Cssm
: public Object
330 typedef CssmImpl Impl
;
331 explicit Cssm(Impl
*impl
) : Object(impl
) {}
332 explicit Cssm() : Object(new Impl()) {}
334 Impl
*operator ->() const { return &impl
<Impl
>(); }
335 Impl
&operator *() const { return impl
<Impl
>(); }
337 static Cssm
standard() { return CssmImpl::standard(); }
340 } // end namespace CssmClient
342 } // end namespace Security
344 #endif // _H_CDSA_CLIENT_CSSMCLIENT