| 1 |
/* |
| 2 |
* Copyright (c) 2012 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 |
/***************************************************************************** |
| 29 |
HandleManager.cpp |
| 30 |
|
| 31 |
One of the most difficult problems to track down is when stale cryptoki handles |
| 32 |
for e.g. keys, objects and sessions get reused by a misbehaving application. |
| 33 |
Especialy when handles that became invalid have since been reused. |
| 34 |
A simple solution to this is to never reuse a handle once it has been issued |
| 35 |
and subsequently invalidated. |
| 36 |
|
| 37 |
The handle manager tracks issued handles along with what kind of object |
| 38 |
is presented by the handle and an actual pointer to the object in question. |
| 39 |
|
| 40 |
Issued handles are unique per application run. All session and object handles |
| 41 |
use the same handle manager and therefore there will never be e.g. a session |
| 42 |
with the same handle as an object. |
| 43 |
|
| 44 |
*****************************************************************************/ |
| 45 |
|
| 46 |
#include "HandleManager.h" |
| 47 |
#include "log.h" |
| 48 |
|
| 49 |
// Constructor |
| 50 |
HandleManager::HandleManager() |
| 51 |
{ |
| 52 |
handlesMutex = MutexFactory::i()->getMutex(); |
| 53 |
handleCounter = 0; |
| 54 |
} |
| 55 |
|
| 56 |
// Destructor |
| 57 |
HandleManager::~HandleManager() |
| 58 |
{ |
| 59 |
|
| 60 |
MutexFactory::i()->recycleMutex(handlesMutex); |
| 61 |
} |
| 62 |
|
| 63 |
CK_SESSION_HANDLE HandleManager::addSession(CK_SLOT_ID slotID, CK_VOID_PTR session) |
| 64 |
{ |
| 65 |
MutexLocker lock(handlesMutex); |
| 66 |
|
| 67 |
Handle h( CKH_SESSION, slotID ); |
| 68 |
h.object = session; |
| 69 |
handles[++handleCounter] = h; |
| 70 |
return (CK_SESSION_HANDLE)handleCounter; |
| 71 |
} |
| 72 |
|
| 73 |
CK_VOID_PTR HandleManager::getSession(const CK_SESSION_HANDLE hSession) |
| 74 |
{ |
| 75 |
MutexLocker lock(handlesMutex); |
| 76 |
|
| 77 |
std::map< CK_ULONG, Handle>::iterator it = handles.find(hSession); |
| 78 |
if (it == handles.end() || CKH_SESSION != it->second.kind) |
| 79 |
return NULL_PTR; |
| 80 |
return it->second.object; |
| 81 |
} |
| 82 |
|
| 83 |
CK_OBJECT_HANDLE HandleManager::addSessionObject(CK_SLOT_ID slotID, CK_SESSION_HANDLE hSession, bool isPrivate, CK_VOID_PTR object) |
| 84 |
{ |
| 85 |
MutexLocker lock(handlesMutex); |
| 86 |
|
| 87 |
// Return existing handle when the object has already been registered. |
| 88 |
std::map< CK_VOID_PTR, CK_ULONG>::iterator oit = objects.find(object); |
| 89 |
if (oit != objects.end()) { |
| 90 |
std::map< CK_ULONG, Handle>::iterator hit = handles.find(oit->second); |
| 91 |
if (hit == handles.end() || CKH_OBJECT != hit->second.kind || slotID != hit->second.slotID) { |
| 92 |
objects.erase(oit); |
| 93 |
return CK_INVALID_HANDLE; |
| 94 |
} else |
| 95 |
return oit->second; |
| 96 |
} |
| 97 |
|
| 98 |
Handle h( CKH_OBJECT, slotID, hSession ); |
| 99 |
h.isPrivate = isPrivate; |
| 100 |
h.object = object; |
| 101 |
handles[++handleCounter] = h; |
| 102 |
objects[object] = handleCounter; |
| 103 |
return (CK_OBJECT_HANDLE)handleCounter; |
| 104 |
} |
| 105 |
|
| 106 |
CK_OBJECT_HANDLE HandleManager::addTokenObject(CK_SLOT_ID slotID, bool isPrivate, CK_VOID_PTR object) |
| 107 |
{ |
| 108 |
MutexLocker lock(handlesMutex); |
| 109 |
|
| 110 |
// Return existing handle when the object has already been registered. |
| 111 |
std::map< CK_VOID_PTR, CK_ULONG>::iterator oit = objects.find(object); |
| 112 |
if (oit != objects.end()) { |
| 113 |
std::map< CK_ULONG, Handle>::iterator hit = handles.find(oit->second); |
| 114 |
if (hit == handles.end() || CKH_OBJECT != hit->second.kind || slotID != hit->second.slotID) { |
| 115 |
objects.erase(oit); |
| 116 |
return CK_INVALID_HANDLE; |
| 117 |
} else |
| 118 |
return oit->second; |
| 119 |
} |
| 120 |
|
| 121 |
// Token objects are not associated with a specific session. |
| 122 |
Handle h( CKH_OBJECT, slotID ); |
| 123 |
h.isPrivate = isPrivate; |
| 124 |
h.object = object; |
| 125 |
handles[++handleCounter] = h; |
| 126 |
objects[object] = handleCounter; |
| 127 |
return (CK_OBJECT_HANDLE)handleCounter; |
| 128 |
} |
| 129 |
|
| 130 |
CK_VOID_PTR HandleManager::getObject(const CK_OBJECT_HANDLE hObject) |
| 131 |
{ |
| 132 |
MutexLocker lock(handlesMutex); |
| 133 |
|
| 134 |
std::map< CK_ULONG, Handle>::iterator it = handles.find(hObject); |
| 135 |
if (it == handles.end() || CKH_OBJECT != it->second.kind ) |
| 136 |
return NULL_PTR; |
| 137 |
return it->second.object; |
| 138 |
} |
| 139 |
|
| 140 |
CK_OBJECT_HANDLE HandleManager::getObjectHandle(CK_VOID_PTR object) |
| 141 |
{ |
| 142 |
MutexLocker lock(handlesMutex); |
| 143 |
|
| 144 |
std::map< CK_VOID_PTR, CK_ULONG>::iterator it = objects.find(object); |
| 145 |
if (it == objects.end()) |
| 146 |
return CK_INVALID_HANDLE; |
| 147 |
return it->second; |
| 148 |
} |
| 149 |
|
| 150 |
void HandleManager::destroyObject(const CK_OBJECT_HANDLE hObject) |
| 151 |
{ |
| 152 |
MutexLocker lock(handlesMutex); |
| 153 |
|
| 154 |
std::map< CK_ULONG, Handle>::iterator it = handles.find(hObject); |
| 155 |
if (it != handles.end() && CKH_OBJECT == it->second.kind) { |
| 156 |
objects.erase(it->second.object); |
| 157 |
handles.erase(it); |
| 158 |
} |
| 159 |
} |
| 160 |
|
| 161 |
void HandleManager::sessionClosed(const CK_SESSION_HANDLE hSession) |
| 162 |
{ |
| 163 |
CK_SLOT_ID slotID; |
| 164 |
MutexLocker lock(handlesMutex); |
| 165 |
|
| 166 |
std::map< CK_ULONG, Handle>::iterator it = handles.find(hSession); |
| 167 |
if (it == handles.end() || CKH_SESSION != it->second.kind) |
| 168 |
return; // Unable to find the specified session. |
| 169 |
|
| 170 |
slotID = it->second.slotID; |
| 171 |
|
| 172 |
// session closed, so we can erase information about it. |
| 173 |
handles.erase(it); |
| 174 |
|
| 175 |
// Erase all session object handles associated with the given session handle. |
| 176 |
CK_ULONG openSessionCount = 0; |
| 177 |
for (it = handles.begin(); it != handles.end(); ) { |
| 178 |
Handle &h = it->second; |
| 179 |
if (CKH_SESSION == h.kind && slotID == h.slotID) { |
| 180 |
++openSessionCount; // another session is open for this slotID. |
| 181 |
} else { |
| 182 |
if (CKH_OBJECT == h.kind && hSession == h.hSession) { |
| 183 |
// A session object is present for the given session, so erase it. |
| 184 |
objects.erase(it->second.object); |
| 185 |
// Iterator post-incrementing (it++) will return a copy of the original it (which points to handle to be deleted). |
| 186 |
handles.erase(it++); |
| 187 |
continue; |
| 188 |
} |
| 189 |
} |
| 190 |
++it; |
| 191 |
} |
| 192 |
|
| 193 |
// We are done when there are still sessions open. |
| 194 |
if (openSessionCount) |
| 195 |
return; |
| 196 |
|
| 197 |
// No more sessions open for this token, so remove all object handles that are still valid for the given slotID. |
| 198 |
allSessionsClosed(slotID, true); |
| 199 |
} |
| 200 |
|
| 201 |
void HandleManager::allSessionsClosed(const CK_SLOT_ID slotID, bool isLocked) |
| 202 |
{ |
| 203 |
MutexLocker lock(isLocked ? NULL : handlesMutex); |
| 204 |
|
| 205 |
// Erase all "session", "session object" and "token object" handles for a given slot id. |
| 206 |
std::map< CK_ULONG, Handle>::iterator it; |
| 207 |
for (it = handles.begin(); it != handles.end(); ) { |
| 208 |
Handle &h = it->second; |
| 209 |
if (slotID == h.slotID) { |
| 210 |
if (CKH_OBJECT == it->second.kind) |
| 211 |
objects.erase(it->second.object); |
| 212 |
// Iterator post-incrementing (it++) will return a copy of the original it (which points to handle to be deleted). |
| 213 |
handles.erase(it++); |
| 214 |
continue; |
| 215 |
} |
| 216 |
++it; |
| 217 |
} |
| 218 |
} |
| 219 |
|
| 220 |
void HandleManager::tokenLoggedOut(const CK_SLOT_ID slotID) |
| 221 |
{ |
| 222 |
MutexLocker lock(handlesMutex); |
| 223 |
|
| 224 |
// Erase all private "token object" or "session object" handles for a given slot id. |
| 225 |
std::map< CK_ULONG, Handle>::iterator it; |
| 226 |
for (it = handles.begin(); it != handles.end(); ) { |
| 227 |
Handle &h = it->second; |
| 228 |
if (CKH_OBJECT == h.kind && slotID == h.slotID && h.isPrivate) { |
| 229 |
// A private object is present for the given slotID so we need to remove it. |
| 230 |
objects.erase(it->second.object); |
| 231 |
// Iterator post-incrementing (it++) will return a copy of the original it (which points to handle to be deleted). |
| 232 |
handles.erase(it++); |
| 233 |
continue; |
| 234 |
} |
| 235 |
++it; |
| 236 |
} |
| 237 |
} |