Debian Codegraph @

src/lib/object_store/SessionObjectStore.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 SessionObjectStore.cpp
29
30 The token class; a token is stored in a directory containing several files.
31 Each object is stored in a separate file and a token object is present that
32 has the token specific attributes
33 *****************************************************************************/
34
35 #include "config.h"
36 #include "log.h"
37 #include "OSAttributes.h"
38 #include "OSAttribute.h"
39 #include "SessionObject.h"
40 #include "cryptoki.h"
41 #include "SessionObjectStore.h"
42 #include <vector>
43 #include <string>
44 #include <set>
45 #include <map>
46 #include <list>
47
48 // Constructor
49 SessionObjectStore::SessionObjectStore()
50 {
51 storeMutex = MutexFactory::i()->getMutex();
52 }
53
54 // Destructor
55 SessionObjectStore::~SessionObjectStore()
56 {
57 // Clean up
58 objects.clear();
59 std::set<SessionObject*> cleanUp = allObjects;
60 allObjects.clear();
61
62 for (std::set<SessionObject*>::iterator i = cleanUp.begin(); i != cleanUp.end(); i++)
63 {
64 if ((*i) == NULL) continue;
65
66 SessionObject* that = *i;
67 delete that;
68 }
69
70 MutexFactory::i()->recycleMutex(storeMutex);
71 }
72
73 int SessionObjectStore::getObjectCount()
74 {
75 return objects.size();
76 }
77
78 void SessionObjectStore::getObjects(std::set<OSObject*> &inObjects)
79 {
80 // Make sure that no other thread is in the process of changing
81 // the object list when we return it
82 MutexLocker lock(storeMutex);
83
84 std::set<SessionObject*>::iterator it;
85 for (it=objects.begin(); it!=objects.end(); ++it) {
86 inObjects.insert(*it);
87 }
88 }
89
90 void SessionObjectStore::getObjects(CK_SLOT_ID slotID, std::set<OSObject*> &inObjects)
91 {
92 // Make sure that no other thread is in the process of changing
93 // the object list when we return it
94 MutexLocker lock(storeMutex);
95
96 std::set<SessionObject*>::iterator it;
97 for (it=objects.begin(); it!=objects.end(); ++it) {
98 if ((*it)->hasSlotID(slotID))
99 inObjects.insert(*it);
100 }
101 }
102
103 // Create a new object
104 SessionObject* SessionObjectStore::createObject(CK_SLOT_ID slotID, CK_SESSION_HANDLE hSession, bool isPrivate)
105 {
106 // Create the new object file
107 SessionObject* newObject = new SessionObject(this, slotID, hSession, isPrivate);
108
109 if (!newObject->isValid())
110 {
111 ERROR_MSG("Failed to create new object");
112
113 delete newObject;
114
115 return NULL;
116 }
117
118 // Now add it to the set of objects
119 MutexLocker lock(storeMutex);
120
121 objects.insert(newObject);
122 allObjects.insert(newObject);
123
124 DEBUG_MSG("(0x%08X) Created new object (0x%08X)", this, newObject);
125
126 return newObject;
127 }
128
129 // Delete an object
130 bool SessionObjectStore::deleteObject(SessionObject* object)
131 {
132 MutexLocker lock(storeMutex);
133
134 if (objects.find(object) == objects.end())
135 {
136 ERROR_MSG("Cannot delete non-existent object 0x%08X", object);
137
138 return false;
139 }
140
141 // Invalidate the object instance
142 object->invalidate();
143
144 objects.erase(object);
145
146 return true;
147 }
148
149 // Indicate that a session has been closed; invalidates all objects
150 // associated with this session
151 void SessionObjectStore::sessionClosed(CK_SESSION_HANDLE hSession)
152 {
153 MutexLocker lock(storeMutex);
154
155 std::set<SessionObject*> checkObjects = objects;
156
157 for (std::set<SessionObject*>::iterator i = checkObjects.begin(); i != checkObjects.end(); i++)
158 {
159 if ((*i)->removeOnSessionClose(hSession))
160 {
161 // Since the object remains in the allObjects set, any pointers to it will
162 // remain valid but it will no longer be returned when the set of objects
163 // is requested
164 objects.erase(*i);
165 }
166 }
167 }
168
169 void SessionObjectStore::allSessionsClosed(CK_SLOT_ID slotID)
170 {
171 MutexLocker lock(storeMutex);
172
173 std::set<SessionObject*> checkObjects = objects;
174
175 for (std::set<SessionObject*>::iterator i = checkObjects.begin(); i != checkObjects.end(); i++)
176 {
177 if ((*i)->removeOnAllSessionsClose(slotID))
178 {
179 // Since the object remains in the allObjects set, any pointers to it will
180 // remain valid but it will no longer be returned when the set of objects
181 // is requested
182 objects.erase(*i);
183 }
184 }
185 }
186
187 void SessionObjectStore::tokenLoggedOut(CK_SLOT_ID slotID)
188 {
189 MutexLocker lock(storeMutex);
190
191 std::set<SessionObject*> checkObjects = objects;
192
193 for (std::set<SessionObject*>::iterator i = checkObjects.begin(); i != checkObjects.end(); i++)
194 {
195 if ((*i)->removeOnTokenLogout(slotID))
196 {
197 // Since the object remains in the allObjects set, any pointers to it will
198 // remain valid but it will no longer be returned when the set of objects
199 // is requested
200 objects.erase(*i);
201 }
202 }
203 }
204
205 // Clear the whole store
206 void SessionObjectStore::clearStore()
207 {
208 MutexLocker lock(storeMutex);
209
210 objects.clear();
211 std::set<SessionObject*> clearObjects = allObjects;
212 allObjects.clear();
213
214 for (std::set<SessionObject*>::iterator i = clearObjects.begin(); i != clearObjects.end(); i++)
215 {
216 delete *i;
217 }
218 }