| 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 |
HandleManager.h |
| 29 |
|
| 30 |
Keeps track of the issued cryptoki handles within SoftHSM |
| 31 |
*****************************************************************************/ |
| 32 |
|
| 33 |
#ifndef _SOFTHSM_V2_HANDLEMANAGER_H |
| 34 |
#define _SOFTHSM_V2_HANDLEMANAGER_H |
| 35 |
|
| 36 |
#include "MutexFactory.h" |
| 37 |
#include "Handle.h" |
| 38 |
#include "cryptoki.h" |
| 39 |
|
| 40 |
#include <map> |
| 41 |
|
| 42 |
#define CK_INTERNAL_SESSION_HANDLE CK_SESSION_HANDLE |
| 43 |
|
| 44 |
class HandleManager |
| 45 |
{ |
| 46 |
public: |
| 47 |
HandleManager(); |
| 48 |
|
| 49 |
virtual ~HandleManager(); |
| 50 |
|
| 51 |
CK_SESSION_HANDLE addSession(CK_SLOT_ID slotID, CK_VOID_PTR session); |
| 52 |
CK_VOID_PTR getSession(const CK_SESSION_HANDLE hSession); |
| 53 |
|
| 54 |
// Add the session object and return a handle. For objects that have already been registered, check that the |
| 55 |
// slotID matches. The hSession may be different as the object may be added as part of a find objects operation. |
| 56 |
CK_OBJECT_HANDLE addSessionObject(CK_SLOT_ID slotID, CK_SESSION_HANDLE hSession, bool isPrivate, CK_VOID_PTR object); |
| 57 |
|
| 58 |
// Add the token object and return a handle. For objects that have already been registered, check that the |
| 59 |
// slotID mathces. |
| 60 |
CK_OBJECT_HANDLE addTokenObject(CK_SLOT_ID slotID, bool isPrivate, CK_VOID_PTR object); |
| 61 |
|
| 62 |
// Get the object pointer associated with the given object handle. |
| 63 |
CK_VOID_PTR getObject(const CK_OBJECT_HANDLE hObject); |
| 64 |
|
| 65 |
// Get the object handle for the object pointer that has been previously registered. |
| 66 |
// When the object is not found CK_INVALID_HANDLE is returned. |
| 67 |
CK_OBJECT_HANDLE getObjectHandle(CK_VOID_PTR object); |
| 68 |
|
| 69 |
// Remove the given object handle. |
| 70 |
void destroyObject(const CK_OBJECT_HANDLE hObject); |
| 71 |
|
| 72 |
// Remove the given session handle and all session object handles for the session. |
| 73 |
// The token object handles retrieved using the session will remain valid unless |
| 74 |
// this is the last session of a token being closed. In that case remove all token |
| 75 |
// object handles for the slot/token associated with the session. |
| 76 |
void sessionClosed(const CK_SESSION_HANDLE hSession); |
| 77 |
|
| 78 |
// Remove all session and object handles for the given slotID. |
| 79 |
// All handles for the given slotID will become invalid. |
| 80 |
void allSessionsClosed(const CK_SLOT_ID slotID, bool isLocked = false); |
| 81 |
|
| 82 |
// Remove all handles to private objects for the given slotID. |
| 83 |
// All handles to public objects for the given slotID remain valid. |
| 84 |
void tokenLoggedOut(const CK_SLOT_ID slotID); |
| 85 |
|
| 86 |
private: |
| 87 |
Mutex* handlesMutex; |
| 88 |
std::map< CK_ULONG, Handle> handles; |
| 89 |
std::map< CK_VOID_PTR, CK_ULONG> objects; |
| 90 |
CK_ULONG handleCounter; |
| 91 |
}; |
| 92 |
|
| 93 |
#endif // !_SOFTHSM_V2_HANDLEMANAGER_H |
| 94 |
|