aboutsummaryrefslogtreecommitdiffstats
path: root/SoftHSMv2/src/lib/object_store/ObjectStore.cpp
blob: 3855d9d34af6f78fea5cbdb25561681c0b30dd23 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
/*
 * Copyright (c) 2010 SURFnet bv
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
 * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
 * IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
 * IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */

/*****************************************************************************
 ObjectStore.h

 The object store manages the separate tokens that the SoftHSM supports. Each
 token is organised as a directory containing files that are contain the
 token's objects. The object store is initialised with a root directory from
 which it enumerates the tokens.
 *****************************************************************************/

#include "config.h"
#include "log.h"
#include "ObjectStore.h"
#include "Directory.h"
#include "ObjectStoreToken.h"
#include "OSPathSep.h"
#include "UUID.h"
#include <stdio.h>

// Constructor
ObjectStore::ObjectStore(std::string inStorePath)
{
	storePath = inStorePath;
	valid = false;
	storeMutex = MutexFactory::i()->getMutex();

	MutexLocker lock(storeMutex);

	// Find all tokens in the specified path
	Directory storeDir(storePath);

	if (!storeDir.isValid())
	{
		WARNING_MSG("Failed to enumerate object store in %s", storePath.c_str());

		return;
	}

	// Assume that all subdirectories are tokens
	std::vector<std::string> dirs = storeDir.getSubDirs();

	for (std::vector<std::string>::iterator i = dirs.begin(); i != dirs.end(); i++)
	{
		// Create a token instance
		ObjectStoreToken* token = ObjectStoreToken::accessToken(storePath, *i);

		if (!token->isValid())
		{
			ERROR_MSG("Failed to open token %s", i->c_str());

			delete token;

			// Silently ignore tokens that we do not have access to
			continue;
		}

		tokens.push_back(token);
		allTokens.push_back(token);
	}

	valid = true;
}

// Destructor
ObjectStore::~ObjectStore()
{
	{
		MutexLocker lock(storeMutex);

		// Clean up
		tokens.clear();

		for (std::vector<ObjectStoreToken*>::iterator i = allTokens.begin(); i != allTokens.end(); i++)
		{
			delete *i;
		}
	}

	MutexFactory::i()->recycleMutex(storeMutex);
}

// Check if the object store is valid
bool ObjectStore::isValid()
{
	return valid;
}

// Return the number of tokens that is present
size_t ObjectStore::getTokenCount()
{
	MutexLocker lock(storeMutex);

	return tokens.size();
}

// Return a pointer to the n-th token (counting starts at 0)
ObjectStoreToken* ObjectStore::getToken(size_t whichToken)
{
	MutexLocker lock(storeMutex);

	if (whichToken >= tokens.size())
	{
		return NULL;
	}

	return tokens[whichToken];
}

// Create a new token
ObjectStoreToken* ObjectStore::newToken(const ByteString& label)
{
	MutexLocker lock(storeMutex);

	// Generate a UUID for the token
	std::string tokenUUID = UUID::newUUID();

	// Convert the UUID to a serial number
	std::string serialNumber = tokenUUID.substr(19, 4) + tokenUUID.substr(24);
	ByteString serial((const unsigned char*) serialNumber.c_str(), serialNumber.size());

	// Create the token
	ObjectStoreToken* newToken = ObjectStoreToken::createToken(storePath, tokenUUID, label, serial);

	if (newToken != NULL)
	{
		tokens.push_back(newToken);
		allTokens.push_back(newToken);
	}

	return newToken;
}

// Destroy a token
bool ObjectStore::destroyToken(ObjectStoreToken *token)
{
	MutexLocker lock(storeMutex);

	// Find the token
	for (std::vector<ObjectStoreToken*>::iterator i = tokens.begin(); i != tokens.end(); i++)
	{
		if (*i == token)
		{
			// Found the token, now destroy the token
			if (!token->clearToken())
			{
				ERROR_MSG("Failed to clear token instance");

				return false;
			}

			// And remove it from the vector
			tokens.erase(i);

			return true;
		}
	}

	ERROR_MSG("Could not find the token instance to destroy");

	return false;
}