| 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 |
OSToken.h |
| 29 |
|
| 30 |
The token class; a token is stored in a directory containing several files. |
| 31 |
Each object is stored in a separate file and a token object is present that |
| 32 |
has the token specific attributes |
| 33 |
*****************************************************************************/ |
| 34 |
|
| 35 |
#ifndef _SOFTHSM_V2_OSTOKEN_H |
| 36 |
#define _SOFTHSM_V2_OSTOKEN_H |
| 37 |
|
| 38 |
#include "config.h" |
| 39 |
#include "ObjectStoreToken.h" |
| 40 |
#include "OSAttribute.h" |
| 41 |
#include "ObjectFile.h" |
| 42 |
#include "Directory.h" |
| 43 |
#include "Generation.h" |
| 44 |
#include "UUID.h" |
| 45 |
#include "MutexFactory.h" |
| 46 |
#include "cryptoki.h" |
| 47 |
#include <string> |
| 48 |
#include <set> |
| 49 |
#include <map> |
| 50 |
#include <list> |
| 51 |
|
| 52 |
class OSToken : public ObjectStoreToken |
| 53 |
{ |
| 54 |
public: |
| 55 |
// Constructor |
| 56 |
OSToken(const std::string inTokenPath); |
| 57 |
|
| 58 |
// Create a new token |
| 59 |
static OSToken* createToken(const std::string basePath, const std::string tokenDir, const ByteString& label, const ByteString& serial); |
| 60 |
|
| 61 |
// Access an existing token |
| 62 |
static OSToken* accessToken(const std::string &basePath, const std::string &tokenDir); |
| 63 |
|
| 64 |
// Constructor for new tokens |
| 65 |
OSToken(const std::string tokenPath, const ByteString& label, const ByteString& serialNumber); |
| 66 |
|
| 67 |
// Set the SO PIN |
| 68 |
virtual bool setSOPIN(const ByteString& soPINBlob); |
| 69 |
|
| 70 |
// Get the SO PIN |
| 71 |
virtual bool getSOPIN(ByteString& soPINBlob); |
| 72 |
|
| 73 |
// Set the user PIN |
| 74 |
virtual bool setUserPIN(ByteString userPINBlob); |
| 75 |
|
| 76 |
// Get the user PIN |
| 77 |
virtual bool getUserPIN(ByteString& userPINBlob); |
| 78 |
|
| 79 |
// Get the token flags |
| 80 |
virtual bool getTokenFlags(CK_ULONG& flags); |
| 81 |
|
| 82 |
// Set the token flags |
| 83 |
virtual bool setTokenFlags(const CK_ULONG flags); |
| 84 |
|
| 85 |
// Retrieve the token label |
| 86 |
virtual bool getTokenLabel(ByteString& label); |
| 87 |
|
| 88 |
// Retrieve the token serial |
| 89 |
virtual bool getTokenSerial(ByteString& serial); |
| 90 |
|
| 91 |
// Retrieve objects |
| 92 |
virtual std::set<OSObject*> getObjects(); |
| 93 |
|
| 94 |
// Insert objects into the given set |
| 95 |
virtual void getObjects(std::set<OSObject*> &inObjects); |
| 96 |
|
| 97 |
// Create a new object |
| 98 |
virtual OSObject* createObject(); |
| 99 |
|
| 100 |
// Delete an object |
| 101 |
virtual bool deleteObject(OSObject* object); |
| 102 |
|
| 103 |
// Destructor |
| 104 |
virtual ~OSToken(); |
| 105 |
|
| 106 |
// Checks if the token is consistent |
| 107 |
virtual bool isValid(); |
| 108 |
|
| 109 |
// Invalidate the token (for instance if it is deleted) |
| 110 |
virtual void invalidate(); |
| 111 |
|
| 112 |
// Delete the token |
| 113 |
virtual bool clearToken(); |
| 114 |
|
| 115 |
// Reset the token |
| 116 |
virtual bool resetToken(const ByteString& label); |
| 117 |
|
| 118 |
private: |
| 119 |
// ObjectFile instances can call the index() function |
| 120 |
friend class ObjectFile; |
| 121 |
|
| 122 |
// Index the token |
| 123 |
bool index(bool isFirstTime = false); |
| 124 |
|
| 125 |
// Is the token consistent and valid? |
| 126 |
bool valid; |
| 127 |
|
| 128 |
// The token path |
| 129 |
std::string tokenPath; |
| 130 |
|
| 131 |
// The current objects of the token |
| 132 |
std::set<OSObject*> objects; |
| 133 |
|
| 134 |
// All the objects ever associated with this token |
| 135 |
// |
| 136 |
// This set is kept to be able to clean up when the token |
| 137 |
// instance is discarded; in case the contents of a token |
| 138 |
// change, some objects may disappear but we cannot simply |
| 139 |
// delete them since they may still be referenced from an |
| 140 |
// object outside of this class. |
| 141 |
std::set<OSObject*> allObjects; |
| 142 |
|
| 143 |
// The current list of files |
| 144 |
std::set<std::string> currentFiles; |
| 145 |
|
| 146 |
// The token object |
| 147 |
ObjectFile* tokenObject; |
| 148 |
|
| 149 |
// Generation control |
| 150 |
Generation* gen; |
| 151 |
|
| 152 |
// The directory object for this token |
| 153 |
Directory* tokenDir; |
| 154 |
|
| 155 |
// For thread safeness |
| 156 |
Mutex* tokenMutex; |
| 157 |
}; |
| 158 |
|
| 159 |
#endif // !_SOFTHSM_V2_OSTOKEN_H |
| 160 |
|