]> git.saurik.com Git - apple/security.git/blob - OSX/libsecurity_cssm/lib/cssmcontext.h
Security-59306.11.20.tar.gz
[apple/security.git] / OSX / libsecurity_cssm / lib / cssmcontext.h
1 /*
2 * Copyright (c) 2000-2004,2011-2012,2014 Apple Inc. All Rights Reserved.
3 *
4 * @APPLE_LICENSE_HEADER_START@
5 *
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
11 * file.
12 *
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.
20 *
21 * @APPLE_LICENSE_HEADER_END@
22 */
23
24
25 //
26 // context - manage CSSM (cryptographic) contexts every which way
27 //
28 #ifndef _H_CSSMCONTEXT
29 #define _H_CSSMCONTEXT
30
31 #include "cssmint.h"
32 #include "cspattachment.h"
33 #include <security_cdsa_utilities/context.h>
34
35 #ifdef _CPP_CSSMCONTEXT
36 # pragma export on
37 #endif
38
39
40 //
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
44 // not a PODWrapper.
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.
48 //
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.
55 //
56 class HandleContext : public HandleObject, public Context {
57 public:
58 HandleContext(CSPAttachment &attach,
59 CSSM_CONTEXT_TYPE type,
60 CSSM_ALGORITHMS algorithmId)
61 : Context(type, algorithmId), attachment(attach), extent(0) { }
62 virtual ~HandleContext();
63
64 CSPAttachment &attachment;
65
66 using Context::find; // guard against HandleObjec::find
67
68 void mergeAttributes(const CSSM_CONTEXT_ATTRIBUTE *attributes, uint32 count);
69 CSSM_RETURN validateChange(CSSM_CONTEXT_EVENT event);
70
71 void *operator new (size_t size, Allocator &alloc) throw(std::bad_alloc)
72 { return alloc.malloc(size); }
73 void operator delete (void *addr, size_t, Allocator &alloc) throw()
74 { return alloc.free(addr); }
75 static void destroy(HandleContext *context, Allocator &alloc) throw()
76 { context->~HandleContext(); alloc.free(context); }
77
78 class Maker; // deluxe builder
79
80 #if __GNUC__ > 2
81 private:
82 void operator delete (void *addr) throw() { assert(0); }
83 #endif
84
85 protected:
86 // Locking protocol, courtesy of HandleObject.
87 // This locks the underlying attachment.
88 void lock();
89 bool tryLock();
90
91 private:
92 void *extent; // extra storage extent in use
93 };
94
95 inline HandleContext &enterContext(CSSM_CC_HANDLE h)
96 {
97 return HandleObject::findAndLock<HandleContext>((CSSM_HANDLE)h, CSSM_ERRCODE_INVALID_CONTEXT_HANDLE);
98 }
99
100
101 //
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.
105 //
106 class HandleContext::Maker : public Context::Builder {
107 public:
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(); }
113
114 CSPAttachment &attachment;
115
116 CSSM_CC_HANDLE operator () (CSSM_CONTEXT_TYPE type,
117 CSSM_ALGORITHMS algorithm);
118
119 private:
120 StLock<CountingMutex, &CountingMutex::enter, &CountingMutex::exit> locker;
121 };
122
123 #endif //_H_CSSMCONTEXT