Debian Codegraph @

src/lib/object_store/SessionObject.cpp

raw
1 /*
2 * Copyright (c) 2010 SURFnet bv
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 *
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
15 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
16 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
18 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
20 * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
21 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
22 * IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
23 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
24 * IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25 */
26
27 /*****************************************************************************
28 SessionObject.cpp
29
30 This class implements session objects (i.e. objects that are non-persistent)
31 *****************************************************************************/
32
33 #include "config.h"
34 #include "SessionObject.h"
35 #include "SessionObjectStore.h"
36
37 // Constructor
38 SessionObject::SessionObject(SessionObjectStore* inParent, CK_SLOT_ID inSlotID, CK_SESSION_HANDLE inHSession, bool inIsPrivate)
39 {
40 hSession = inHSession;
41 slotID = inSlotID;
42 isPrivate = inIsPrivate;
43 objectMutex = MutexFactory::i()->getMutex();
44 valid = (objectMutex != NULL);
45 parent = inParent;
46 }
47
48 // Destructor
49 SessionObject::~SessionObject()
50 {
51 discardAttributes();
52
53 MutexFactory::i()->recycleMutex(objectMutex);
54 }
55
56 // Check if the specified attribute exists
57 bool SessionObject::attributeExists(CK_ATTRIBUTE_TYPE type)
58 {
59 MutexLocker lock(objectMutex);
60
61 return valid && (attributes[type] != NULL);
62 }
63
64 // Retrieve the specified attribute
65 OSAttribute SessionObject::getAttribute(CK_ATTRIBUTE_TYPE type)
66 {
67 MutexLocker lock(objectMutex);
68
69 OSAttribute* attr = attributes[type];
70 if (attr == NULL)
71 {
72 ERROR_MSG("The attribute does not exist: 0x%08X", type);
73 return OSAttribute((unsigned long)0);
74 }
75
76 return *attr;
77 }
78
79 bool SessionObject::getBooleanValue(CK_ATTRIBUTE_TYPE type, bool val)
80 {
81 MutexLocker lock(objectMutex);
82
83 OSAttribute* attr = attributes[type];
84 if (attr == NULL)
85 {
86 ERROR_MSG("The attribute does not exist: 0x%08X", type);
87 return val;
88 }
89
90 if (attr->isBooleanAttribute())
91 {
92 return attr->getBooleanValue();
93 }
94 else
95 {
96 ERROR_MSG("The attribute is not a boolean: 0x%08X", type);
97 return val;
98 }
99 }
100
101 unsigned long SessionObject::getUnsignedLongValue(CK_ATTRIBUTE_TYPE type, unsigned long val)
102 {
103 MutexLocker lock(objectMutex);
104
105 OSAttribute* attr = attributes[type];
106 if (attr == NULL)
107 {
108 ERROR_MSG("The attribute does not exist: 0x%08X", type);
109 return val;
110 }
111
112 if (attr->isUnsignedLongAttribute())
113 {
114 return attr->getUnsignedLongValue();
115 }
116 else
117 {
118 ERROR_MSG("The attribute is not an unsigned long: 0x%08X", type);
119 return val;
120 }
121 }
122
123 ByteString SessionObject::getByteStringValue(CK_ATTRIBUTE_TYPE type)
124 {
125 MutexLocker lock(objectMutex);
126
127 ByteString val;
128
129 OSAttribute* attr = attributes[type];
130 if (attr == NULL)
131 {
132 ERROR_MSG("The attribute does not exist: 0x%08X", type);
133 return val;
134 }
135
136 if (attr->isByteStringAttribute())
137 {
138 return attr->getByteStringValue();
139 }
140 else
141 {
142 ERROR_MSG("The attribute is not a byte string: 0x%08X", type);
143 return val;
144 }
145 }
146
147 // Retrieve the next attribute type
148 CK_ATTRIBUTE_TYPE SessionObject::nextAttributeType(CK_ATTRIBUTE_TYPE type)
149 {
150 MutexLocker lock(objectMutex);
151
152 std::map<CK_ATTRIBUTE_TYPE, OSAttribute*>::iterator n = attributes.upper_bound(type);
153
154 // skip null attributes
155 while ((n != attributes.end()) && (n->second == NULL))
156 ++n;
157
158
159 // return type or CKA_CLASS (= 0)
160 if (n == attributes.end())
161 {
162 return CKA_CLASS;
163 }
164 else
165 {
166 return n->first;
167 }
168 }
169
170 // Set the specified attribute
171 bool SessionObject::setAttribute(CK_ATTRIBUTE_TYPE type, const OSAttribute& attribute)
172 {
173 MutexLocker lock(objectMutex);
174
175 if (!valid)
176 {
177 DEBUG_MSG("Cannot update invalid session object 0x%08X", this);
178
179 return false;
180 }
181
182 if (attributes[type] != NULL)
183 {
184 delete attributes[type];
185
186 attributes[type] = NULL;
187 }
188
189 attributes[type] = new OSAttribute(attribute);
190
191 return true;
192 }
193
194 // Delete the specified attribute
195 bool SessionObject::deleteAttribute(CK_ATTRIBUTE_TYPE type)
196 {
197 MutexLocker lock(objectMutex);
198
199 if (!valid)
200 {
201 DEBUG_MSG("Cannot update invalid session object 0x%08X", this);
202
203 return false;
204 }
205
206 if (attributes[type] == NULL)
207 {
208 DEBUG_MSG("Cannot delete attribute that doesn't exist in object 0x%08X", this);
209
210 return false;
211 }
212
213 delete attributes[type];
214 attributes.erase(type);
215
216 return true;
217 }
218
219 // The validity state of the object
220 bool SessionObject::isValid()
221 {
222 return valid;
223 }
224
225 bool SessionObject::hasSlotID(CK_SLOT_ID inSlotID)
226 {
227 return slotID == inSlotID;
228 }
229
230 // Called by the session object store when a session is closed. If it's the
231 // session this object was associated with, the function returns true and the
232 // object is invalidated
233 bool SessionObject::removeOnSessionClose(CK_SESSION_HANDLE inHSession)
234 {
235 if (hSession == inHSession)
236 {
237 // Save space
238 discardAttributes();
239
240 valid = false;
241
242 return true;
243 }
244
245 return false;
246 }
247
248 // Called by the session object store when a token is logged out.
249 // Remove when this session object is a private object for this token.
250 bool SessionObject::removeOnAllSessionsClose(CK_SLOT_ID inSlotID)
251 {
252 if (slotID == inSlotID)
253 {
254 discardAttributes();
255
256 valid = false;
257
258 return true;
259 }
260
261 return false;
262 }
263
264 // Called by the session object store when a token is logged out.
265 // Remove when this session object is a private object for this token.
266 bool SessionObject::removeOnTokenLogout(CK_SLOT_ID inSlotID)
267 {
268 if (slotID == inSlotID && isPrivate)
269 {
270 discardAttributes();
271
272 valid = false;
273
274 return true;
275 }
276
277 return false;
278 }
279
280 // Discard the object's attributes
281 void SessionObject::discardAttributes()
282 {
283 MutexLocker lock(objectMutex);
284
285 std::map<CK_ATTRIBUTE_TYPE, OSAttribute*> cleanUp = attributes;
286 attributes.clear();
287
288 for (std::map<CK_ATTRIBUTE_TYPE, OSAttribute*>::iterator i = cleanUp.begin(); i != cleanUp.end(); i++)
289 {
290 if (i->second == NULL)
291 {
292 continue;
293 }
294
295 delete i->second;
296 i->second = NULL;
297 }
298 }
299
300 // These functions are just stubs for session objects
301 bool SessionObject::startTransaction(Access)
302 {
303 return true;
304 }
305
306 bool SessionObject::commitTransaction()
307 {
308 return true;
309 }
310
311 bool SessionObject::abortTransaction()
312 {
313 return true;
314 }
315
316 bool SessionObject::destroyObject()
317 {
318 if (parent == NULL)
319 {
320 ERROR_MSG("Cannot destroy object that is not associated with a session object store");
321
322 return false;
323 }
324
325 return parent->deleteObject(this);
326 }
327
328 // Invalidate the object
329 void SessionObject::invalidate()
330 {
331 valid = false;
332 discardAttributes();
333 }
334