| 1 |
/* |
| 2 |
* Copyright (c) 2010 .SE (The Internet Infrastructure Foundation) |
| 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 |
SessionManager.cpp |
| 29 |
|
| 30 |
Keeps track of the sessions within SoftHSM. The sessions are stored in a |
| 31 |
vector. When a session is closed, its spot in the vector will be replaced |
| 32 |
with NULL. Because we want to keep track of the session ID which is |
| 33 |
equal to its location in the vector. New sessions will first fill up the NULL |
| 34 |
locations and if there is no empty spots, then they are added to the end. |
| 35 |
*****************************************************************************/ |
| 36 |
|
| 37 |
#include "SessionManager.h" |
| 38 |
#include "log.h" |
| 39 |
|
| 40 |
// Constructor |
| 41 |
SessionManager::SessionManager() |
| 42 |
{ |
| 43 |
sessionsMutex = MutexFactory::i()->getMutex(); |
| 44 |
} |
| 45 |
|
| 46 |
// Destructor |
| 47 |
SessionManager::~SessionManager() |
| 48 |
{ |
| 49 |
std::vector<Session*> toDelete = sessions; |
| 50 |
sessions.clear(); |
| 51 |
|
| 52 |
for (std::vector<Session*>::iterator i = toDelete.begin(); i != toDelete.end(); i++) |
| 53 |
{ |
| 54 |
if (*i != NULL) delete *i; |
| 55 |
} |
| 56 |
|
| 57 |
MutexFactory::i()->recycleMutex(sessionsMutex); |
| 58 |
} |
| 59 |
|
| 60 |
// Open a new session |
| 61 |
CK_RV SessionManager::openSession |
| 62 |
( |
| 63 |
Slot* slot, |
| 64 |
CK_FLAGS flags, |
| 65 |
CK_VOID_PTR pApplication, |
| 66 |
CK_NOTIFY notify, |
| 67 |
CK_SESSION_HANDLE_PTR phSession |
| 68 |
) |
| 69 |
{ |
| 70 |
if (phSession == NULL_PTR) return CKR_ARGUMENTS_BAD; |
| 71 |
if (slot == NULL) return CKR_SLOT_ID_INVALID; |
| 72 |
if ((flags & CKF_SERIAL_SESSION) == 0) return CKR_SESSION_PARALLEL_NOT_SUPPORTED; |
| 73 |
|
| 74 |
// Lock access to the vector |
| 75 |
MutexLocker lock(sessionsMutex); |
| 76 |
|
| 77 |
// Get the token |
| 78 |
Token* token = slot->getToken(); |
| 79 |
if (token == NULL) return CKR_TOKEN_NOT_PRESENT; |
| 80 |
if (!token->isInitialized()) return CKR_TOKEN_NOT_RECOGNIZED; |
| 81 |
|
| 82 |
// Can not open a Read-Only session when in SO mode |
| 83 |
if ((flags & CKF_RW_SESSION) == 0 && token->isSOLoggedIn()) return CKR_SESSION_READ_WRITE_SO_EXISTS; |
| 84 |
|
| 85 |
// TODO: Do we want to check for maximum number of sessions? |
| 86 |
// return CKR_SESSION_COUNT |
| 87 |
|
| 88 |
// Create the session |
| 89 |
bool rwSession = ((flags & CKF_RW_SESSION) == CKF_RW_SESSION) ? true : false; |
| 90 |
Session* session = new Session(slot, rwSession, pApplication, notify); |
| 91 |
|
| 92 |
// First fill any empty spot in the list |
| 93 |
for (size_t i = 0; i < sessions.size(); i++) |
| 94 |
{ |
| 95 |
if (sessions[i] != NULL) |
| 96 |
{ |
| 97 |
continue; |
| 98 |
} |
| 99 |
|
| 100 |
sessions[i] = session; |
| 101 |
session->setHandle(i + 1); |
| 102 |
*phSession = session->getHandle(); |
| 103 |
|
| 104 |
return CKR_OK; |
| 105 |
} |
| 106 |
|
| 107 |
// Or add it to the end |
| 108 |
sessions.push_back(session); |
| 109 |
session->setHandle(sessions.size()); |
| 110 |
*phSession = session->getHandle(); |
| 111 |
|
| 112 |
return CKR_OK; |
| 113 |
} |
| 114 |
|
| 115 |
// Close a session |
| 116 |
CK_RV SessionManager::closeSession(CK_SESSION_HANDLE hSession) |
| 117 |
{ |
| 118 |
if (hSession == CK_INVALID_HANDLE) return CKR_SESSION_HANDLE_INVALID; |
| 119 |
|
| 120 |
// Lock access to the vector |
| 121 |
MutexLocker lock(sessionsMutex); |
| 122 |
|
| 123 |
// Check if we are out of range |
| 124 |
if (hSession > sessions.size()) return CKR_SESSION_HANDLE_INVALID; |
| 125 |
|
| 126 |
// Check if it is a closed session |
| 127 |
unsigned long sessionID = hSession - 1; |
| 128 |
if (sessions[sessionID] == NULL) return CKR_SESSION_HANDLE_INVALID; |
| 129 |
|
| 130 |
// Check if this is the last session on the token |
| 131 |
bool lastSession = true; |
| 132 |
const CK_SLOT_ID slotID( sessions[sessionID]->getSlot()->getSlotID() ); |
| 133 |
for (size_t i = 0; i < sessions.size(); i++) |
| 134 |
{ |
| 135 |
if (sessions[i] == NULL) continue; |
| 136 |
|
| 137 |
if (sessions[i]->getSlot()->getSlotID() == slotID && i != sessionID) |
| 138 |
{ |
| 139 |
lastSession = false; |
| 140 |
break; |
| 141 |
} |
| 142 |
} |
| 143 |
|
| 144 |
// Logout if this is the last session on the token |
| 145 |
if (lastSession) |
| 146 |
{ |
| 147 |
sessions[sessionID]->getSlot()->getToken()->logout(); |
| 148 |
} |
| 149 |
|
| 150 |
// Close the session |
| 151 |
delete sessions[sessionID]; |
| 152 |
sessions[sessionID] = NULL; |
| 153 |
|
| 154 |
return CKR_OK; |
| 155 |
} |
| 156 |
|
| 157 |
// Close all sessions |
| 158 |
CK_RV SessionManager::closeAllSessions(Slot* slot) |
| 159 |
{ |
| 160 |
if (slot == NULL) return CKR_SLOT_ID_INVALID; |
| 161 |
|
| 162 |
// Lock access to the vector |
| 163 |
MutexLocker lock(sessionsMutex); |
| 164 |
|
| 165 |
// Get the token |
| 166 |
Token* token = slot->getToken(); |
| 167 |
if (token == NULL) return CKR_TOKEN_NOT_PRESENT; |
| 168 |
|
| 169 |
// Close all sessions on this slot |
| 170 |
const CK_SLOT_ID slotID( slot->getSlotID() ); |
| 171 |
for (std::vector<Session*>::iterator i = sessions.begin(); i != sessions.end(); i++) |
| 172 |
{ |
| 173 |
if (*i == NULL) continue; |
| 174 |
|
| 175 |
if ((*i)->getSlot()->getSlotID() == slotID) |
| 176 |
{ |
| 177 |
delete *i; |
| 178 |
*i = NULL; |
| 179 |
} |
| 180 |
} |
| 181 |
|
| 182 |
// Logout from the token |
| 183 |
token->logout(); |
| 184 |
|
| 185 |
return CKR_OK; |
| 186 |
} |
| 187 |
|
| 188 |
// Get session info |
| 189 |
CK_RV SessionManager::getSessionInfo(CK_SESSION_HANDLE hSession, CK_SESSION_INFO_PTR pInfo) |
| 190 |
{ |
| 191 |
// Get the session |
| 192 |
Session* session = getSession(hSession); |
| 193 |
if (session == NULL) return CKR_SESSION_HANDLE_INVALID; |
| 194 |
|
| 195 |
return session->getInfo(pInfo); |
| 196 |
} |
| 197 |
|
| 198 |
// Get the session |
| 199 |
Session* SessionManager::getSession(CK_SESSION_HANDLE hSession) |
| 200 |
{ |
| 201 |
// Lock access to the vector |
| 202 |
MutexLocker lock(sessionsMutex); |
| 203 |
|
| 204 |
// We do not want to get a negative number below |
| 205 |
if (hSession == CK_INVALID_HANDLE) return NULL; |
| 206 |
|
| 207 |
// Check if we are out of range |
| 208 |
if (hSession > sessions.size()) return NULL; |
| 209 |
|
| 210 |
return sessions[hSession - 1]; |
| 211 |
} |
| 212 |
|
| 213 |
bool SessionManager::haveSession(CK_SLOT_ID slotID) |
| 214 |
{ |
| 215 |
// Lock access to the vector |
| 216 |
MutexLocker lock(sessionsMutex); |
| 217 |
|
| 218 |
for (std::vector<Session*>::iterator i = sessions.begin(); i != sessions.end(); i++) |
| 219 |
{ |
| 220 |
if (*i == NULL) continue; |
| 221 |
|
| 222 |
if ((*i)->getSlot()->getSlotID() == slotID) |
| 223 |
{ |
| 224 |
return true; |
| 225 |
} |
| 226 |
} |
| 227 |
|
| 228 |
return false; |
| 229 |
} |
| 230 |
|
| 231 |
bool SessionManager::haveROSession(CK_SLOT_ID slotID) |
| 232 |
{ |
| 233 |
// Lock access to the vector |
| 234 |
MutexLocker lock(sessionsMutex); |
| 235 |
|
| 236 |
for (std::vector<Session*>::iterator i = sessions.begin(); i != sessions.end(); i++) |
| 237 |
{ |
| 238 |
if (*i == NULL) continue; |
| 239 |
|
| 240 |
if ((*i)->getSlot()->getSlotID() != slotID) continue; |
| 241 |
|
| 242 |
if ((*i)->isRW() == false) return true; |
| 243 |
} |
| 244 |
|
| 245 |
return false; |
| 246 |
} |