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 // context - manage CSSM (cryptographic) contexts every which way
22 #ifndef _H_CSSMCONTEXT
23 #define _H_CSSMCONTEXT
26 #include "cspattachment.h"
27 #include <Security/context.h>
29 #ifdef _CPP_CSSMCONTEXT
35 // A HandleContext adds handle semantics to the Context object.
36 // Note that not every Context is a HandleContext - the Contexts we hand
37 // to our API customers for fondling are not. Also note that a HandleContext
39 // HandleContext has an allocation method taking a CssmAllocator. To destroy
40 // a HandleObject, call HandleObject::destroy(the-context, the-allocator).
41 // You are responsible for picking the same allocator used on construction.
43 // THREADS: HandleContexts are assumed to have single-thread use. That means that
44 // operations on HandleContexts are NOT interlocked automatically; two users of
45 // the same context must do any arbitration themselves. A HandleContext is howerver
46 // safely interlocked against other objects, in particular its CSPAttachment.
47 // The upshot is that you're safe using a HandleContext unless someone else is trying
48 // to use the same context in parallel.
50 class HandleContext
: public HandleObject
, public Context
{
52 HandleContext(CSPAttachment
&attach
,
53 CSSM_CONTEXT_TYPE type
,
54 CSSM_ALGORITHMS algorithmId
)
55 : Context(type
, algorithmId
), attachment(attach
), extent(0) { }
56 virtual ~HandleContext();
58 CSPAttachment
&attachment
;
60 using Context::find
; // guard against HandleObjec::find
62 void mergeAttributes(const CSSM_CONTEXT_ATTRIBUTE
*attributes
, uint32 count
);
63 CSSM_RETURN
validateChange(CSSM_CONTEXT_EVENT event
);
65 void *operator new (size_t size
, CssmAllocator
&alloc
) throw(std::bad_alloc
)
66 { return alloc
.malloc(size
); }
67 void operator delete (void *addr
, size_t, CssmAllocator
&alloc
) throw()
68 { return alloc
.free(addr
); }
69 static void destroy(HandleContext
*context
, CssmAllocator
&alloc
) throw()
70 { context
->~HandleContext(); alloc
.free(context
); }
72 class Maker
; // deluxe builder
76 void operator delete (void *addr
) throw() { assert(0); }
80 // Locking protocol, courtesy of HandleObject.
81 // This locks the underlying attachment.
86 void *extent
; // extra storage extent in use
89 inline HandleContext
&enterContext(CSSM_CC_HANDLE h
)
91 return findHandleAndLock
<HandleContext
>(h
, CSSM_ERRCODE_INVALID_CONTEXT_HANDLE
);
96 // A Maker is a deluxe wrapper around Builder. It creates whole HandleContext
97 // objects in one swell foop, handling object locking, construction, error
98 // recovery, and all that jazz. A Maker cannot create plain Context objects.
100 class HandleContext::Maker
: public Context::Builder
{
102 Maker(CSSM_CSP_HANDLE handle
)
103 : Context::Builder(findHandleAndLock
<CSPAttachment
>(handle
, CSSM_ERRCODE_INVALID_CSP_HANDLE
)),
104 attachment(static_cast<CSPAttachment
&>(allocator
)), // order dependency(!)
105 locker(attachment
, true)
106 { attachment
.finishEnter(); }
108 CSPAttachment
&attachment
;
110 CSSM_CC_HANDLE
operator () (CSSM_CONTEXT_TYPE type
,
111 CSSM_ALGORITHMS algorithm
);
114 StLock
<CountingMutex
, &CountingMutex::enter
, &CountingMutex::exit
> locker
;
117 #endif //_H_CSSMCONTEXT