2 * Copyright (c) 2000-2004,2011-2012,2014 Apple Inc. All Rights Reserved.
4 * @APPLE_LICENSE_HEADER_START@
6 * This file contains Original Code and/or Modifications of Original Code
7 * as defined in and that are subject to the Apple Public Source License
8 * Version 2.0 (the 'License'). You may not use this file except in
9 * compliance with the License. Please obtain a copy of the License at
10 * http://www.opensource.apple.com/apsl/ and read it before using this
13 * The Original Code and all software distributed under the License are
14 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
15 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
16 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
18 * Please see the License for the specific language governing rights and
19 * limitations under the License.
21 * @APPLE_LICENSE_HEADER_END@
26 // context - manage CSSM (cryptographic) contexts every which way
28 #ifndef _H_CSSMCONTEXT
29 #define _H_CSSMCONTEXT
32 #include "cspattachment.h"
33 #include <security_cdsa_utilities/context.h>
35 #ifdef _CPP_CSSMCONTEXT
41 // A HandleContext adds handle semantics to the Context object.
42 // Note that not every Context is a HandleContext - the Contexts we hand
43 // to our API customers for fondling are not. Also note that a HandleContext
45 // HandleContext has an allocation method taking a Allocator. To destroy
46 // a HandleObject, call HandleObject::destroy(the-context, the-allocator).
47 // You are responsible for picking the same allocator used on construction.
49 // THREADS: HandleContexts are assumed to have single-thread use. That means that
50 // operations on HandleContexts are NOT interlocked automatically; two users of
51 // the same context must do any arbitration themselves. A HandleContext is howerver
52 // safely interlocked against other objects, in particular its CSPAttachment.
53 // The upshot is that you're safe using a HandleContext unless someone else is trying
54 // to use the same context in parallel.
56 class HandleContext
: public HandleObject
, public Context
{
58 HandleContext(CSPAttachment
&attach
,
59 CSSM_CONTEXT_TYPE type
,
60 CSSM_ALGORITHMS algorithmId
)
61 : Context(type
, algorithmId
), attachment(attach
), extent(0) { }
62 virtual ~HandleContext();
64 CSPAttachment
&attachment
;
66 using Context::find
; // guard against HandleObjec::find
68 void mergeAttributes(const CSSM_CONTEXT_ATTRIBUTE
*attributes
, uint32 count
);
69 CSSM_RETURN
validateChange(CSSM_CONTEXT_EVENT event
);
71 void *operator new (size_t size
, Allocator
&alloc
)
72 { return alloc
.malloc(size
); }
73 void operator delete (void *addr
, size_t, Allocator
&alloc
) _NOEXCEPT
74 { return alloc
.free(addr
); }
75 static void destroy(HandleContext
*context
, Allocator
&alloc
) _NOEXCEPT
76 { context
->~HandleContext(); alloc
.free(context
); }
78 class Maker
; // deluxe builder
82 void operator delete (void *addr
) _NOEXCEPT
{ assert(0); }
86 // Locking protocol, courtesy of HandleObject.
87 // This locks the underlying attachment.
92 void *extent
; // extra storage extent in use
95 inline HandleContext
&enterContext(CSSM_CC_HANDLE h
)
97 return HandleObject::findAndLock
<HandleContext
>((CSSM_HANDLE
)h
, CSSM_ERRCODE_INVALID_CONTEXT_HANDLE
);
102 // A Maker is a deluxe wrapper around Builder. It creates whole HandleContext
103 // objects in one swell foop, handling object locking, construction, error
104 // recovery, and all that jazz. A Maker cannot create plain Context objects.
106 class HandleContext::Maker
: public Context::Builder
{
108 Maker(CSSM_CSP_HANDLE handle
)
109 : Context::Builder(HandleObject::findAndLock
<CSPAttachment
>(handle
, CSSM_ERRCODE_INVALID_CSP_HANDLE
)),
110 attachment(static_cast<CSPAttachment
&>(allocator
)), // order dependency(!)
111 locker(attachment
, true)
112 { attachment
.finishEnter(); }
114 CSPAttachment
&attachment
;
116 CSSM_CC_HANDLE
operator () (CSSM_CONTEXT_TYPE type
,
117 CSSM_ALGORITHMS algorithm
);
120 StLock
<CountingMutex
, &CountingMutex::enter
, &CountingMutex::exit
> locker
;
123 #endif //_H_CSSMCONTEXT