| 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 |
SlotManager.cpp |
| 29 |
|
| 30 |
The slot manager is a class that forms part of the PKCS #11 core. It manages |
| 31 |
all the slots that SoftHSM is aware of. To make it possible to add new |
| 32 |
tokens, SoftHSM always has one slot available that contains an uninitialised |
| 33 |
token. Users can choose to initialise this token to create a new token. |
| 34 |
*****************************************************************************/ |
| 35 |
|
| 36 |
#include "config.h" |
| 37 |
#include "log.h" |
| 38 |
#include "SlotManager.h" |
| 39 |
#include <cassert> |
| 40 |
#include <stdexcept> |
| 41 |
typedef std::pair<CK_SLOT_ID, Slot*> SlotMapElement; |
| 42 |
typedef std::pair<SlotMap::iterator, bool> InsertResult; |
| 43 |
|
| 44 |
// Constructor |
| 45 |
SlotManager::SlotManager(ObjectStore*const objectStore) |
| 46 |
{ |
| 47 |
// Add a slot for each token that already exists |
| 48 |
for (size_t i = 0; i < objectStore->getTokenCount(); i++) |
| 49 |
{ |
| 50 |
ObjectStoreToken*const pToken(objectStore->getToken(i)); |
| 51 |
ByteString bs; |
| 52 |
pToken->getTokenSerial(bs); |
| 53 |
const std::string s((const char*)bs.const_byte_str(), bs.size()); |
| 54 |
|
| 55 |
// parse serial string that is expected to have only hex digits. |
| 56 |
CK_SLOT_ID l; |
| 57 |
if (s.size() < 8) |
| 58 |
{ |
| 59 |
l = strtoul(s.c_str(), NULL, 16); |
| 60 |
} |
| 61 |
else |
| 62 |
{ |
| 63 |
l = strtoul(s.substr(s.size() - 8).c_str(), NULL, 16); |
| 64 |
} |
| 65 |
|
| 66 |
// mask for 31 bits. |
| 67 |
// this since sunpkcs11 java wrapper is parsing the slot ID to a java int that needs to be positive. |
| 68 |
// java int is 32 bit and the the sign bit is removed. |
| 69 |
const CK_SLOT_ID mask( ((CK_SLOT_ID)1<<31)-1 ); |
| 70 |
const CK_SLOT_ID slotID(mask&l); |
| 71 |
|
| 72 |
insertToken(objectStore, slotID, pToken); |
| 73 |
} |
| 74 |
|
| 75 |
// Add an empty slot |
| 76 |
insertToken(objectStore, objectStore->getTokenCount(), NULL); |
| 77 |
} |
| 78 |
|
| 79 |
void SlotManager::insertToken(ObjectStore*const objectStore, const CK_SLOT_ID slotID, ObjectStoreToken*const pToken) { |
| 80 |
Slot*const newSlot( new Slot(objectStore, slotID, pToken) ); |
| 81 |
const InsertResult result( slots.insert(SlotMapElement(slotID, newSlot)) ); |
| 82 |
assert(result.second);// fails if there is already a token on this slot |
| 83 |
} |
| 84 |
|
| 85 |
// Destructor |
| 86 |
SlotManager::~SlotManager() |
| 87 |
{ |
| 88 |
SlotMap toDelete = slots; |
| 89 |
slots.clear(); |
| 90 |
|
| 91 |
for (SlotMap::iterator i = toDelete.begin(); i != toDelete.end(); i++) |
| 92 |
{ |
| 93 |
delete i->second; |
| 94 |
} |
| 95 |
} |
| 96 |
|
| 97 |
// Get the slot list |
| 98 |
CK_RV SlotManager::getSlotList(ObjectStore* objectStore, CK_BBOOL tokenPresent, CK_SLOT_ID_PTR pSlotList, CK_ULONG_PTR pulCount) |
| 99 |
{ |
| 100 |
size_t size( 0 ); |
| 101 |
|
| 102 |
if (pulCount == NULL) return CKR_ARGUMENTS_BAD; |
| 103 |
|
| 104 |
// Calculate the size of the list |
| 105 |
bool uninitialized = false; |
| 106 |
for (SlotMap::iterator i = slots.begin(); i != slots.end(); i++) |
| 107 |
{ |
| 108 |
if ((tokenPresent == CK_FALSE) || i->second->isTokenPresent()) |
| 109 |
{ |
| 110 |
size++; |
| 111 |
} |
| 112 |
|
| 113 |
if (i->second->getToken() != NULL && i->second->getToken()->isInitialized() == false) |
| 114 |
{ |
| 115 |
uninitialized = true; |
| 116 |
} |
| 117 |
} |
| 118 |
|
| 119 |
// The user wants the size of the list |
| 120 |
if (pSlotList == NULL) |
| 121 |
{ |
| 122 |
// Always have an uninitialized token |
| 123 |
if (uninitialized == false) |
| 124 |
{ |
| 125 |
insertToken(objectStore, objectStore->getTokenCount(), NULL); |
| 126 |
size++; |
| 127 |
} |
| 128 |
|
| 129 |
*pulCount = size; |
| 130 |
|
| 131 |
return CKR_OK; |
| 132 |
} |
| 133 |
|
| 134 |
// Is the given buffer too small? |
| 135 |
if (*pulCount < size) |
| 136 |
{ |
| 137 |
*pulCount = size; |
| 138 |
|
| 139 |
return CKR_BUFFER_TOO_SMALL; |
| 140 |
} |
| 141 |
|
| 142 |
size_t startIx( 0 ); |
| 143 |
size_t endIx( size-1 ); |
| 144 |
|
| 145 |
for (SlotMap::iterator i = slots.begin(); i != slots.end(); i++) |
| 146 |
{ |
| 147 |
if ((tokenPresent == CK_TRUE) && !i->second->isTokenPresent()) |
| 148 |
{// only show token if present on slot. But this slot has no token so we continue |
| 149 |
continue; |
| 150 |
} |
| 151 |
// put uninitialized last. After all initialized or slots without tokens. |
| 152 |
if ( i->second->isTokenPresent() && !i->second->getToken()->isInitialized() ) { |
| 153 |
pSlotList[endIx--] = i->second->getSlotID(); |
| 154 |
} else { |
| 155 |
pSlotList[startIx++] = i->second->getSlotID(); |
| 156 |
} |
| 157 |
} |
| 158 |
assert(startIx==endIx+1); |
| 159 |
*pulCount = size; |
| 160 |
|
| 161 |
return CKR_OK; |
| 162 |
} |
| 163 |
|
| 164 |
// Get the slots |
| 165 |
SlotMap SlotManager::getSlots() |
| 166 |
{ |
| 167 |
return slots; |
| 168 |
} |
| 169 |
|
| 170 |
// Get one slot |
| 171 |
Slot* SlotManager::getSlot(CK_SLOT_ID slotID) |
| 172 |
{ |
| 173 |
try { |
| 174 |
return slots.at(slotID); |
| 175 |
} catch( const std::out_of_range &oor) { |
| 176 |
DEBUG_MSG("slotID is out of range: %s", oor.what()); |
| 177 |
return NULL_PTR; |
| 178 |
} |
| 179 |
} |