]> git.saurik.com Git - apple/security.git/blob - cdsa/cssm/attachment.cpp
Security-54.1.3.tar.gz
[apple/security.git] / cdsa / cssm / attachment.cpp
1 /*
2 * Copyright (c) 2000-2001 Apple Computer, Inc. All Rights Reserved.
3 *
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
8 * using this file.
9 *
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.
16 */
17
18
19 //
20 // attachment - CSSM module attachment objects
21 //
22 #ifdef __MWERKS__
23 #define _CPP_ATTACHMENT
24 #endif
25 #include "attachment.h"
26 #include "module.h"
27 #include "manager.h"
28 #include "cssmcontext.h"
29
30 //
31 // Construct an Attachment object.
32 // This constructor does almost all the work: it initializes the Attachment
33 // object, calls the plugin's attach function, and initializes everything.
34 // The only job left for the subclass's constructor is to take the spiFunctionTable
35 // field and extract from it the plugin's dispatch table in suitable form.
36 //
37 Attachment::Attachment(Module *parent,
38 const CSSM_VERSION &version,
39 uint32 ssId,
40 CSSM_SERVICE_TYPE ssType,
41 const CSSM_API_MEMORY_FUNCS &memoryOps,
42 CSSM_ATTACH_FLAGS attachFlags,
43 CSSM_KEY_HIERARCHY keyHierarchy)
44 : CssmMemoryFunctionsAllocator(memoryOps), module(*parent)
45 {
46 // record our origins
47 mVersion = version;
48 mSubserviceId = ssId;
49 mSubserviceType = ssType;
50 mAttachFlags = attachFlags;
51 mKeyHierarchy = keyHierarchy;
52
53 // we are not (yet) attached to our plugin
54 mIsActive = false;
55
56 // build the upcalls table
57 // (we could do this once in a static, but then we'd have to lock on it)
58 upcalls.malloc_func = upcallMalloc;
59 upcalls.free_func = upcallFree;
60 upcalls.realloc_func = upcallRealloc;
61 upcalls.calloc_func = upcallCalloc;
62 upcalls.CcToHandle_func = upcallCcToHandle;
63 upcalls.GetModuleInfo_func = upcallGetModuleInfo;
64
65 // tell the module to create an attachment
66 spiFunctionTable = NULL; // preset invalid
67 if (CSSM_RETURN err = module.plugin->CSSM_SPI_ModuleAttach(&module.myGuid(),
68 &mVersion,
69 mSubserviceId,
70 mSubserviceType,
71 mAttachFlags,
72 handle(),
73 mKeyHierarchy,
74 &module.cssm.myGuid(), // CSSM's Guid
75 &module.cssm.myGuid(), // module manager Guid
76 &module.cssm.callerGuid(), // caller Guid
77 &upcalls,
78 &spiFunctionTable)) {
79 // attach rejected by module
80 CssmError::throwMe(err);
81 }
82 try {
83 if (spiFunctionTable == NULL || spiFunctionTable->ServiceType != subserviceType())
84 CssmError::throwMe(CSSMERR_CSSM_INVALID_ADDIN_FUNCTION_TABLE);
85 mIsActive = true; // now officially attached to plugin
86 // subclass is responsible for taking spiFunctionTable and build
87 // whatever dispatch is needed
88 } catch (...) {
89 module.plugin->CSSM_SPI_ModuleDetach(handle()); // with extreme prejudice
90 throw;
91 }
92 }
93
94
95 //
96 // Detach an attachment.
97 // This is the polite way to detach from the plugin. It may be refused safely
98 // (though perhaps not meaningfully).
99 // THREADS: mLock is locked on entry IFF isLocked, and will be unlocked on exit.
100 //
101 void Attachment::detach(bool isLocked)
102 {
103 StLock<Mutex> locker(*this, isLocked); // pre-state locker
104 locker.lock(); // make sure it's locked
105
106 if (mIsActive) {
107 if (!isIdle())
108 CssmError::throwMe(CSSM_ERRCODE_FUNCTION_FAILED); //@#attachment busy
109 if (CSSM_RETURN error = module.plugin->CSSM_SPI_ModuleDetach(handle()))
110 CssmError::throwMe(error); // I'm sorry Dave, ...
111 mIsActive = false;
112 module.detach(this);
113 }
114 }
115
116
117 //
118 // Destroy the Attachment object
119 //
120 Attachment::~Attachment()
121 {
122 try {
123 detach(false);
124 } catch (...) {
125 // too bad - you're dead
126 }
127 }
128
129
130 //
131 // Upcall relays.
132 // These do not lock the attachment object. The attachment can't go away
133 // because we incremented the busy count on entry to the plugin; and these
134 // fields are quite constant for the life of the Attachment.
135 //
136 void *Attachment::upcallMalloc(CSSM_HANDLE handle, uint32 size)
137 {
138 BEGIN_API
139 return findHandle<Attachment>(handle).malloc(size);
140 END_API1(NULL)
141 }
142
143 void Attachment::upcallFree(CSSM_HANDLE handle, void *mem)
144 {
145 BEGIN_API
146 return findHandle<Attachment>(handle).free(mem);
147 END_API0
148 }
149
150 void *Attachment::upcallRealloc(CSSM_HANDLE handle, void *mem, uint32 size)
151 {
152 BEGIN_API
153 return findHandle<Attachment>(handle).realloc(mem, size);
154 END_API1(NULL)
155 }
156
157 void *Attachment::upcallCalloc(CSSM_HANDLE handle, uint32 num, uint32 size)
158 {
159 BEGIN_API
160 return findHandle<Attachment>(handle).calloc(num, size);
161 END_API1(NULL)
162 }
163
164 CSSM_RETURN Attachment::upcallCcToHandle(CSSM_CC_HANDLE handle,
165 CSSM_MODULE_HANDLE *modHandle)
166 {
167 BEGIN_API
168 Required(modHandle) = findHandle<HandleContext>(handle).attachment.handle();
169 END_API(CSP)
170 }
171
172 CSSM_RETURN Attachment::upcallGetModuleInfo(CSSM_MODULE_HANDLE handle,
173 CSSM_GUID_PTR guid,
174 CSSM_VERSION_PTR version,
175 uint32 *subserviceId,
176 CSSM_SERVICE_TYPE *subserviceType,
177 CSSM_ATTACH_FLAGS *attachFlags,
178 CSSM_KEY_HIERARCHY *keyHierarchy,
179 CSSM_API_MEMORY_FUNCS_PTR memoryOps,
180 CSSM_FUNC_NAME_ADDR_PTR FunctionTable,
181 uint32 NumFunctions)
182 {
183 BEGIN_API
184 Attachment &attachment = findHandle<Attachment>(handle);
185 Required(guid) = attachment.myGuid();
186 Required(version) = attachment.mVersion;
187 Required(subserviceId) = attachment.mSubserviceId;
188 Required(subserviceType) = attachment.mSubserviceType;
189 Required(attachFlags) = attachment.mAttachFlags;
190 Required(keyHierarchy) = attachment.mKeyHierarchy;
191 Required(memoryOps) = attachment;
192 if (FunctionTable)
193 attachment.resolveSymbols(FunctionTable, NumFunctions);
194 END_API(CSSM)
195 }