2 * Copyright (c) 2000-2001,2011,2014 Apple 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 // cssmplugin - adapter framework for C++-based CDSA plugin modules
22 // A note on locking: Attachments are effectively reference counted in CSSM.
23 // CSSM will not let a client detach an attachment that has a(nother) thread
24 // active in its code. Thus, our locks merely protect global maps; they do not
25 // need (or try) to close the classic use-and-delete window.
27 #include <security_cdsa_plugin/cssmplugin.h>
28 #include <security_cdsa_plugin/pluginsession.h>
30 #include "LegacyAPICounts.h"
33 ModuleNexus
<CssmPlugin::SessionMap
> CssmPlugin::sessionMap
;
36 CssmPlugin::CssmPlugin()
41 CssmPlugin::~CssmPlugin()
43 // Note: if mLoaded, we're being unloaded forcibly.
44 // (CSSM wouldn't do this to us in normal operation.)
50 // CSSM only calls this once for a module, and multiplexes any additional
51 // CSSM_ModuleLoad calls internally. So this is only called when we have just
52 // been loaded (and not yet attached).
54 void CssmPlugin::moduleLoad(const Guid
&cssmGuid
,
55 const Guid
&moduleGuid
,
56 const ModuleCallback
&newCallback
)
58 static dispatch_once_t onceToken
;
59 countLegacyAPI(&onceToken
, "CssmPlugin::moduleLoad");
61 CssmError::throwMe(CSSM_ERRCODE_INTERNAL_ERROR
);
66 // let the implementation know that we're loading
70 mCallback
= newCallback
;
77 // The callback passed here will be the same passed to load.
78 // CSSM only calls this on a "final" CSSM_ModuleUnload, after all attachments
79 // are destroyed and (just) before we are physically unloaded.
81 void CssmPlugin::moduleUnload(const Guid
&cssmGuid
,
82 const Guid
&moduleGuid
,
83 const ModuleCallback
&oldCallback
)
85 // These are called from the public pluginspi.h
86 static dispatch_once_t onceToken
;
87 countLegacyAPI(&onceToken
, "CssmPlugin::moduleUnload");
88 // check the callback vector
89 if (!mLoaded
|| oldCallback
!= mCallback
) {
90 CssmError::throwMe(CSSM_ERRCODE_INTERNAL_ERROR
);
93 // tell our subclass that we're closing down
102 // Create one attachment session. This is what CSSM calls to process
103 // a CSSM_ModuleAttach call. moduleLoad() has already been called and has
104 // returned successfully.
106 void CssmPlugin::moduleAttach(CSSM_MODULE_HANDLE theHandle
,
107 const Guid
&newCssmGuid
,
108 const Guid
&moduleGuid
,
109 const Guid
&moduleManagerGuid
,
110 const Guid
&callerGuid
,
111 const CSSM_VERSION
&version
,
113 CSSM_SERVICE_TYPE subserviceType
,
114 CSSM_ATTACH_FLAGS attachFlags
,
115 CSSM_KEY_HIERARCHY keyHierarchy
,
116 const CSSM_UPCALLS
&upcalls
,
117 CSSM_MODULE_FUNCS_PTR
&funcTbl
)
119 static dispatch_once_t onceToken
;
120 countLegacyAPI(&onceToken
, "CssmPlugin::moduleAttach");
121 // basic (in)sanity checks
122 if (moduleGuid
!= mMyGuid
)
123 CssmError::throwMe(CSSM_ERRCODE_INVALID_GUID
);
125 // make the new session object, hanging in thin air
126 unique_ptr
<PluginSession
> session(this->makeSession(theHandle
,
128 subserviceId
, subserviceType
,
132 // haggle with the implementor
133 funcTbl
= session
->construct();
135 // commit this session creation
136 StLock
<Mutex
> _(sessionMap());
137 sessionMap()[theHandle
] = session
.release();
142 // Undo a (single) module attachment. This calls the detach() method on
143 // the Session object representing the attachment. This is only called
144 // if session->construct() has succeeded previously.
145 // If session->detach() fails, we do not destroy the session and it continues
146 // to live, though its handle may have (briefly) been invalid. This is for
147 // desperate "mustn't go right now" situations and should not be abused.
148 // CSSM always has the ability to ditch you without your consent if you are
151 void CssmPlugin::moduleDetach(CSSM_MODULE_HANDLE handle
)
153 static dispatch_once_t onceToken
;
154 countLegacyAPI(&onceToken
, "CssmPlugin::moduleDetach");
155 // locate the plugin and hold the sessionMapLock
156 PluginSession
*session
;
158 StLock
<Mutex
> _(sessionMap());
159 SessionMap::iterator it
= sessionMap().find(handle
);
160 if (it
== sessionMap().end())
161 CssmError::throwMe(CSSMERR_CSSM_INVALID_ADDIN_HANDLE
);
162 session
= it
->second
;
163 sessionMap().erase(it
);
166 // let the session know it is going away
171 // session detach failed - put the plugin back and fail
172 StLock
<Mutex
> _(sessionMap());
173 sessionMap()[handle
] = session
;
180 // Send an official CSSM module callback message upstream
182 void CssmPlugin::sendCallback(CSSM_MODULE_EVENT event
, uint32 ssid
,
183 CSSM_SERVICE_TYPE serviceType
) const
186 mCallback(event
, mMyGuid
, ssid
, serviceType
);
191 // Default subclass hooks.
192 // The default implementations succeed without doing anything.
194 void CssmPlugin::load() { }
196 void CssmPlugin::unload() { }