]> git.saurik.com Git - apple/security.git/blob - OSX/libsecurity_cssm/lib/cssm.cpp
Security-59754.41.1.tar.gz
[apple/security.git] / OSX / libsecurity_cssm / lib / cssm.cpp
1 /*
2 * Copyright (c) 2000-2001,2003-2004,2011,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 // This file contains the core CSSM public functions.
27 // Note that hardly anything happens in here; we just hand off requests
28 // to the various objects representing CSSM state.
29 //
30 #include "manager.h"
31 #include "module.h"
32 #include <security_utilities/globalizer.h>
33 #include <security_cdsa_utilities/cssmbridge.h>
34 #include "LegacyAPICounts.h"
35
36 //
37 // We currently use exactly one instance of CssmManager.
38 //
39 static ModuleNexus<CssmManager> gManager;
40
41
42 //
43 // Public API: (Re)Intiailize CSSM
44 //
45 CSSM_RETURN CSSMAPI
46 CSSM_Init (const CSSM_VERSION *Version,
47 CSSM_PRIVILEGE_SCOPE Scope,
48 const CSSM_GUID *CallerGuid,
49 CSSM_KEY_HIERARCHY KeyHierarchy,
50 CSSM_PVC_MODE *PvcPolicy,
51 const void *)
52 {
53 BEGIN_API
54 gManager().initialize(Required(Version),
55 Scope,
56 Guid::required(CallerGuid),
57 KeyHierarchy,
58 Required(PvcPolicy));
59 END_API(CSSM)
60 }
61
62
63 //
64 // Public API: Terminate CSSM.
65 //
66 CSSM_RETURN CSSMAPI
67 CSSM_Terminate (void)
68 {
69 BEGIN_API
70 gManager().terminate();
71 END_API(CSSM)
72 }
73
74
75 //
76 // Public API: Load a CSSM module
77 //
78 CSSM_RETURN CSSMAPI
79 CSSM_ModuleLoad (const CSSM_GUID *ModuleGuid,
80 CSSM_KEY_HIERARCHY KeyHierarchy,
81 CSSM_API_ModuleEventHandler AppNotifyCallback,
82 void *AppNotifyCallbackCtx)
83 {
84 BEGIN_API
85 gManager().loadModule(Guid::required(ModuleGuid),
86 KeyHierarchy,
87 ModuleCallback(AppNotifyCallback, AppNotifyCallbackCtx));
88 END_API(CSSM)
89 }
90
91
92 //
93 // Public API: Unload a module
94 //
95 CSSM_RETURN CSSMAPI
96 CSSM_ModuleUnload (const CSSM_GUID *ModuleGuid,
97 CSSM_API_ModuleEventHandler AppNotifyCallback,
98 void *AppNotifyCallbackCtx)
99 {
100 BEGIN_API
101 gManager().unloadModule(Guid::required(ModuleGuid),
102 ModuleCallback(AppNotifyCallback, AppNotifyCallbackCtx));
103 END_API(CSSM)
104 }
105
106
107 CSSM_RETURN CSSMAPI
108 CSSM_Introduce (const CSSM_GUID *ModuleID,
109 CSSM_KEY_HIERARCHY KeyHierarchy)
110 {
111 BEGIN_API
112 gManager().introduce(Guid::required(ModuleID), KeyHierarchy);
113 END_API(CSSM)
114 }
115
116 CSSM_RETURN CSSMAPI
117 CSSM_Unintroduce (const CSSM_GUID *ModuleID)
118 {
119 BEGIN_API
120 gManager().unIntroduce(Guid::required(ModuleID));
121 END_API(CSSM)
122 }
123
124
125 CSSM_RETURN CSSMAPI
126 CSSM_ModuleAttach (const CSSM_GUID *ModuleGuid,
127 const CSSM_VERSION *Version,
128 const CSSM_API_MEMORY_FUNCS *MemoryFuncs,
129 uint32 SubserviceID,
130 CSSM_SERVICE_TYPE SubServiceType,
131 CSSM_ATTACH_FLAGS AttachFlags,
132 CSSM_KEY_HIERARCHY KeyHierarchy,
133 CSSM_FUNC_NAME_ADDR *FunctionTable,
134 uint32 NumFunctionTable,
135 const void *,
136 CSSM_MODULE_HANDLE_PTR NewModuleHandle)
137 {
138 BEGIN_API
139 Required(NewModuleHandle) = gManager().getModule(Guid::required(ModuleGuid))->attach(
140 Required(Version),
141 SubserviceID, SubServiceType,
142 Required(MemoryFuncs),
143 AttachFlags,
144 KeyHierarchy,
145 FunctionTable, NumFunctionTable
146 );
147 END_API(CSSM)
148 }
149
150 CSSM_RETURN CSSMAPI
151 CSSM_ModuleDetach (CSSM_MODULE_HANDLE ModuleHandle)
152 {
153 BEGIN_API
154 Attachment *attachment = &HandleObject::findAndKill<Attachment>(ModuleHandle, CSSMERR_CSSM_INVALID_ADDIN_HANDLE);
155 attachment->detach(true); // expect locked, will unlock
156 // the attachment is now off the maps, known idle, and unhooked
157 delete attachment;
158 END_API(CSSM)
159 }
160
161
162 CSSM_RETURN CSSMAPI
163 CSSM_SetPrivilege (CSSM_PRIVILEGE Privilege)
164 {
165 BEGIN_API
166 gManager().setPrivilege(Privilege);
167 END_API(CSSM)
168 }
169
170 CSSM_RETURN CSSMAPI
171 CSSM_GetPrivilege (CSSM_PRIVILEGE *Privilege)
172 {
173 BEGIN_API
174 Required(Privilege) = gManager().getPrivilege();
175 END_API(CSSM)
176 }
177
178
179 CSSM_RETURN CSSMAPI
180 CSSM_GetModuleGUIDFromHandle (CSSM_MODULE_HANDLE ModuleHandle,
181 CSSM_GUID_PTR ModuleGUID)
182 {
183 BEGIN_API
184 Attachment &attachment = HandleObject::findAndLock<Attachment>(ModuleHandle, CSSMERR_CSSM_INVALID_ADDIN_HANDLE);
185 StLock<Mutex> _(attachment, true);
186 Required(ModuleGUID) = attachment.module.myGuid();
187 END_API(CSSM)
188 }
189
190 CSSM_RETURN CSSMAPI
191 CSSM_GetSubserviceUIDFromHandle (CSSM_MODULE_HANDLE ModuleHandle,
192 CSSM_SUBSERVICE_UID_PTR SubserviceUID)
193 {
194 BEGIN_API
195 Attachment &attachment = HandleObject::findAndLock<Attachment>(ModuleHandle, CSSMERR_CSSM_INVALID_ADDIN_HANDLE);
196 StLock<Mutex> _(attachment, true);
197 CSSM_SUBSERVICE_UID &result = Required(SubserviceUID);
198 result.Guid = attachment.module.myGuid();
199 result.Version = attachment.pluginVersion();
200 result.SubserviceId = attachment.subserviceId();
201 result.SubserviceType = attachment.subserviceType();
202 END_API(CSSM)
203 }
204
205
206 CSSM_RETURN CSSMAPI
207 CSSM_ListAttachedModuleManagers (uint32 *NumberOfModuleManagers,
208 CSSM_GUID_PTR)
209 {
210 BEGIN_API
211 *NumberOfModuleManagers = 0; // EMMs not implemented
212 END_API(CSSM)
213 }
214
215 CSSM_RETURN CSSMAPI
216 CSSM_GetAPIMemoryFunctions (CSSM_MODULE_HANDLE AddInHandle,
217 CSSM_API_MEMORY_FUNCS_PTR AppMemoryFuncs)
218 {
219 BEGIN_API
220 Attachment &attachment = HandleObject::findAndLock<Attachment>(AddInHandle, CSSMERR_CSSM_INVALID_ADDIN_HANDLE);
221 StLock<Mutex> _(attachment, true);
222 Required(AppMemoryFuncs) = attachment;
223 END_API(CSSM)
224 }