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(); }
152 bool operator <(const Object
&other
) const
153 { return mImpl
&& other
.mImpl
? *mImpl
< *other
.mImpl
: mImpl
< other
.mImpl
; }
154 bool operator ==(const Object
&other
) const
155 { return mImpl
&& other
.mImpl
? *mImpl
== *other
.mImpl
: mImpl
== other
.mImpl
; }
158 RefPointer
<Impl
> mImpl
;
163 // A CSSM loadable module.
164 // You rarely directly interact with these objects, but if you need to,
167 class ModuleImpl
: public ObjectImpl
, public HasGuid
170 ModuleImpl(const Guid
&guid
);
171 ModuleImpl(const Guid
&guid
, const Cssm
&session
);
172 virtual ~ModuleImpl();
174 void load() { activate(); }
175 void unload() { deactivate(); }
176 bool isLoaded() const { return isActive(); }
178 Cssm
session() const;
185 class Module
: public Object
188 typedef ModuleImpl Impl
;
189 explicit Module(Impl
*impl
) : Object(impl
) {}
190 Module() : Object(NULL
) {} // XXX This might break operator <
191 Module(const Guid
&guid
) : Object(new Impl(guid
)) {}
192 Module(const Guid
&guid
, const Cssm
&session
) : Object(new Impl(guid
, session
)) {}
194 Impl
*operator ->() const { return &impl
<Impl
>(); }
195 Impl
&operator *() const { return impl
<Impl
>(); }
200 // An Attachment object. This is the base class of all typed attachment classes.
202 class AttachmentImpl
: public ObjectImpl
205 AttachmentImpl(const Guid
&guid
, CSSM_SERVICE_TYPE subserviceType
);
206 AttachmentImpl(const Module
&module, CSSM_SERVICE_TYPE subserviceType
);
207 //AttachmentImpl(... mds reference ...);
208 virtual ~AttachmentImpl();
210 // Virtual so that subclasses can return there true mask.
211 virtual CSSM_SERVICE_MASK
subserviceMask() const;
213 CSSM_SERVICE_TYPE
subserviceType() const { return mSubserviceType
; }
214 CSSM_VERSION
version() const { return mVersion
; }
215 void version(const CSSM_VERSION
&v
) { mVersion
= v
; }
216 uint32
subserviceId() const { return mSubserviceId
; }
217 virtual void subserviceId(uint32 id
);
218 CSSM_ATTACH_FLAGS
flags() const { return mAttachFlags
; }
219 void flags(CSSM_ATTACH_FLAGS f
) { mAttachFlags
= f
; }
221 void attach() { activate(); }
222 void detach() { deactivate(); }
223 bool attached() const { return isActive(); }
225 Module
module() const;
226 const Guid
&guid() const { return module()->guid(); }
227 CSSM_MODULE_HANDLE
handle() { attach(); return mHandle
; }
229 CssmSubserviceUid
subserviceUid() const;
236 void make(CSSM_SERVICE_TYPE subserviceType
); // common constructor
238 CSSM_MODULE_HANDLE mHandle
;
240 CSSM_SERVICE_TYPE mSubserviceType
; // set by constructor
241 CSSM_VERSION mVersion
;
242 uint32 mSubserviceId
;
243 CSSM_ATTACH_FLAGS mAttachFlags
;
245 CssmAllocatorMemoryFunctions mMemoryFunctions
; // set on attach()
248 class Attachment
: public Object
251 typedef AttachmentImpl Impl
;
252 explicit Attachment(Impl
*impl
) : Object(impl
) {}
253 Attachment(const Guid
&guid
, CSSM_SERVICE_TYPE subserviceType
)
254 : Object(new Impl(guid
, subserviceType
)) {}
255 Attachment(const Module
&module, CSSM_SERVICE_TYPE subserviceType
)
256 : Object(new Impl(module, subserviceType
)) {}
257 //Attachment(... mds reference ...);
259 Impl
*operator ->() const { return &impl
<Impl
>(); }
260 Impl
&operator *() const { return impl
<Impl
>(); }
265 // A CSSM session object.
266 // You usually only have one per program, or library, or what-not.
270 class CssmImpl
: public ObjectImpl
{
271 class StandardCssm
; friend class StandardCssm
;
276 void init() { activate(); }
277 void terminate() { deactivate(); }
279 CSSM_PRIVILEGE_SCOPE
scope() const { return mScope
; }
280 void scope(CSSM_PRIVILEGE_SCOPE sc
) { mScope
= sc
; }
281 const Guid
&callerGuid() const { return mCallerGuid
; }
282 void callerGuid(const CSSM_GUID
&guid
) { mCallerGuid
= Guid::overlay(guid
); }
284 Module
autoModule(const Guid
&guid
);
287 explicit CssmImpl(bool); // internal constructor
289 void setup(); // constructor setup
295 // CSSM global configuration -- picked up on each Init
296 CSSM_VERSION mVersion
;
297 CSSM_PRIVILEGE_SCOPE mScope
;
300 // module repository: modules by guid (protected by self)
301 typedef map
<Guid
, Module
> ModuleMap
;
306 static Cssm
standard();
307 static void catchExit();
310 static void atExitHandler();
312 class StandardCssm
: public Mutex
{
314 StandardCssm() : mCssm(NULL
) { }
316 void setCssm(CssmImpl
*cssm
);
317 void unsetCssm(CssmImpl
*cssm
);
323 static ModuleNexus
<StandardCssm
> mStandard
;
326 class Cssm
: public Object
329 typedef CssmImpl Impl
;
330 explicit Cssm(Impl
*impl
) : Object(impl
) {}
331 explicit Cssm() : Object(new Impl()) {}
333 Impl
*operator ->() const { return &impl
<Impl
>(); }
334 Impl
&operator *() const { return impl
<Impl
>(); }
336 static Cssm
standard() { return CssmImpl::standard(); }
339 } // end namespace CssmClient
341 } // end namespace Security
343 #endif // _H_CDSA_CLIENT_CSSMCLIENT