| 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 |
Slot.h |
| 29 |
|
| 30 |
This class represents a single PKCS #11 slot |
| 31 |
*****************************************************************************/ |
| 32 |
|
| 33 |
#include "config.h" |
| 34 |
#include "log.h" |
| 35 |
#include "SessionManager.h" |
| 36 |
#include "SlotManager.h" |
| 37 |
#include "Token.h" |
| 38 |
#include <stdio.h> |
| 39 |
#include <string.h> |
| 40 |
#include <sstream> |
| 41 |
|
| 42 |
// Constructor |
| 43 |
Slot::Slot(ObjectStore* inObjectStore, CK_SLOT_ID inSlotID, ObjectStoreToken* inToken /* = NULL */) |
| 44 |
{ |
| 45 |
objectStore = inObjectStore; |
| 46 |
slotID = inSlotID; |
| 47 |
|
| 48 |
if (inToken != NULL) |
| 49 |
{ |
| 50 |
token = new Token(inToken); |
| 51 |
} |
| 52 |
else |
| 53 |
{ |
| 54 |
token = new Token(); |
| 55 |
} |
| 56 |
} |
| 57 |
|
| 58 |
// Destructor |
| 59 |
Slot::~Slot() |
| 60 |
{ |
| 61 |
delete token; |
| 62 |
} |
| 63 |
|
| 64 |
// Retrieve the token in the slot |
| 65 |
Token* Slot::getToken() |
| 66 |
{ |
| 67 |
return token; |
| 68 |
} |
| 69 |
|
| 70 |
// Initialise the token in the slot |
| 71 |
CK_RV Slot::initToken(ByteString& soPIN, CK_UTF8CHAR_PTR label) |
| 72 |
{ |
| 73 |
return token->createToken(objectStore, soPIN, label); |
| 74 |
} |
| 75 |
|
| 76 |
// Retrieve slot information for the slot |
| 77 |
CK_RV Slot::getSlotInfo(CK_SLOT_INFO_PTR info) |
| 78 |
{ |
| 79 |
if (info == NULL) |
| 80 |
{ |
| 81 |
return CKR_ARGUMENTS_BAD; |
| 82 |
} |
| 83 |
|
| 84 |
std::ostringstream osDescription; |
| 85 |
osDescription << "SoftHSM slot ID 0x" << std::hex << slotID; |
| 86 |
const std::string sDescription(osDescription.str()); |
| 87 |
|
| 88 |
char mfgID[33]; |
| 89 |
snprintf(mfgID, 33, "SoftHSM project"); |
| 90 |
|
| 91 |
memset(info->slotDescription, ' ', 64); |
| 92 |
memset(info->manufacturerID, ' ', 32); |
| 93 |
memcpy(info->slotDescription, sDescription.data(), sDescription.size()); |
| 94 |
memcpy(info->manufacturerID, mfgID, strlen(mfgID)); |
| 95 |
|
| 96 |
info->flags = CKF_TOKEN_PRESENT; |
| 97 |
|
| 98 |
info->hardwareVersion.major = VERSION_MAJOR; |
| 99 |
info->hardwareVersion.minor = VERSION_MINOR; |
| 100 |
info->firmwareVersion.major = VERSION_MAJOR; |
| 101 |
info->firmwareVersion.minor = VERSION_MINOR; |
| 102 |
|
| 103 |
return CKR_OK; |
| 104 |
} |
| 105 |
|
| 106 |
// Get the slot ID |
| 107 |
CK_SLOT_ID Slot::getSlotID() |
| 108 |
{ |
| 109 |
return slotID; |
| 110 |
} |
| 111 |
|
| 112 |
// Is a token present? |
| 113 |
bool Slot::isTokenPresent() |
| 114 |
{ |
| 115 |
return true; |
| 116 |
} |