diff options
Diffstat (limited to 'SoftHSMv2/src/lib/common')
-rw-r--r-- | SoftHSMv2/src/lib/common/Configuration.cpp | 176 | ||||
-rw-r--r-- | SoftHSMv2/src/lib/common/Configuration.h | 117 | ||||
-rw-r--r-- | SoftHSMv2/src/lib/common/HandleFactory.h | 121 | ||||
-rw-r--r-- | SoftHSMv2/src/lib/common/Makefile.am | 28 | ||||
-rw-r--r-- | SoftHSMv2/src/lib/common/MutexFactory.cpp | 192 | ||||
-rw-r--r-- | SoftHSMv2/src/lib/common/MutexFactory.h | 133 | ||||
-rw-r--r-- | SoftHSMv2/src/lib/common/Serialisable.h | 52 | ||||
-rw-r--r-- | SoftHSMv2/src/lib/common/SimpleConfigLoader.cpp | 352 | ||||
-rw-r--r-- | SoftHSMv2/src/lib/common/SimpleConfigLoader.h | 63 | ||||
-rw-r--r-- | SoftHSMv2/src/lib/common/fatal.cpp | 64 | ||||
-rw-r--r-- | SoftHSMv2/src/lib/common/fatal.h | 52 | ||||
-rw-r--r-- | SoftHSMv2/src/lib/common/log.cpp | 109 | ||||
-rw-r--r-- | SoftHSMv2/src/lib/common/log.h | 85 | ||||
-rw-r--r-- | SoftHSMv2/src/lib/common/osmutex.cpp | 242 | ||||
-rw-r--r-- | SoftHSMv2/src/lib/common/osmutex.h | 47 | ||||
-rw-r--r-- | SoftHSMv2/src/lib/common/softhsm2.conf.5.in | 86 | ||||
-rw-r--r-- | SoftHSMv2/src/lib/common/softhsm2.conf.in | 10 |
17 files changed, 1929 insertions, 0 deletions
diff --git a/SoftHSMv2/src/lib/common/Configuration.cpp b/SoftHSMv2/src/lib/common/Configuration.cpp new file mode 100644 index 0000000..a7f6cc6 --- /dev/null +++ b/SoftHSMv2/src/lib/common/Configuration.cpp @@ -0,0 +1,176 @@ +/* + * 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. + */ + +/***************************************************************************** + Configuration.cpp + + Loads the configuration and supports retrieval of configuration information + *****************************************************************************/ + +#include <string> +#include <map> +#include "Configuration.h" +#include "log.h" + +// Initialise the one-and-only instance +#ifdef HAVE_CXX11 +std::unique_ptr<Configuration> Configuration::instance(nullptr); +#else +std::auto_ptr<Configuration> Configuration::instance(NULL); +#endif + +// Add all valid configurations +const struct config Configuration::valid_config[] = { + { "directories.tokendir", CONFIG_TYPE_STRING }, + { "objectstore.backend", CONFIG_TYPE_STRING }, + { "log.level", CONFIG_TYPE_STRING }, + { "slots.removable", CONFIG_TYPE_BOOL }, + { "", CONFIG_TYPE_UNSUPPORTED } +}; + +// Return the one-and-only instance +Configuration* Configuration::i() +{ + if (instance.get() == NULL) + { + instance.reset(new Configuration()); + } + + return instance.get(); +} + +// Constructor +Configuration::Configuration() +{ + configLoader = NULL; +} + +// Get the type of the configuration value +int Configuration::getType(std::string key) +{ + for (int i = 0; valid_config[i].key.compare("") != 0; i++) + { + if (valid_config[i].key.compare(key) == 0) + { + return valid_config[i].type; + } + } + + return CONFIG_TYPE_UNSUPPORTED; +} + +// Retrieve a string based configuration value +std::string Configuration::getString(std::string key, std::string ifEmpty /* = "" */) +{ + if (stringConfiguration.find(key) != stringConfiguration.end()) + { + return stringConfiguration[key]; + } + else + { + WARNING_MSG("Missing %s in configuration. Using default value: %s", key.c_str(), ifEmpty.c_str()); + return ifEmpty; + } +} + +// Retrieve an integer configuration value +int Configuration::getInt(std::string key, int ifEmpty /* = 0 */) +{ + if (integerConfiguration.find(key) != integerConfiguration.end()) + { + return integerConfiguration[key]; + } + else + { + WARNING_MSG("Missing %s in configuration. Using default value: %i", key.c_str(), ifEmpty); + return ifEmpty; + } +} + +// Retrieve a boolean configuration value +bool Configuration::getBool(std::string key, bool ifEmpty /* = false */) +{ + if (booleanConfiguration.find(key) != booleanConfiguration.end()) + { + return booleanConfiguration[key]; + } + else + { + WARNING_MSG("Missing %s in configuration. Using default value: %s", key.c_str(), ifEmpty ? "true" : "false"); + return ifEmpty; + } +} + +// Set a string based configuration value +void Configuration::setString(std::string key, std::string value) +{ + stringConfiguration[key] = value; +} + +// Set an integer based configuration value +void Configuration::setInt(std::string key, int value) +{ + integerConfiguration[key] = value; +} + +// Set a boolean configuration value +void Configuration::setBool(std::string key, bool value) +{ + booleanConfiguration[key] = value; +} + +// Reload the configuration +bool Configuration::reload() +{ + if (configLoader == NULL) + { + return false; + } + + // Discard the current configuration + stringConfiguration.clear(); + integerConfiguration.clear(); + booleanConfiguration.clear(); + + // Reload the configuration + if (!configLoader->loadConfiguration()) + { + ERROR_MSG("Failed to load the SoftHSM configuration"); + + return false; + } + + return true; +} + +// Reload the configuration using the specified configuration loader +bool Configuration::reload(ConfigLoader* inConfigLoader) +{ + configLoader = inConfigLoader; + + return reload(); +} + diff --git a/SoftHSMv2/src/lib/common/Configuration.h b/SoftHSMv2/src/lib/common/Configuration.h new file mode 100644 index 0000000..eacb493 --- /dev/null +++ b/SoftHSMv2/src/lib/common/Configuration.h @@ -0,0 +1,117 @@ +/* + * 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. + */ + +/***************************************************************************** + Configuration.h + + Loads the configuration and supports retrieval of configuration information + *****************************************************************************/ + +#ifndef _SOFTHSM_V2_CONFIGURATION_H +#define _SOFTHSM_V2_CONFIGURATION_H + +#include "config.h" +#include <string> +#include <map> +#include <memory> + +enum +{ + CONFIG_TYPE_UNSUPPORTED, + CONFIG_TYPE_STRING, + CONFIG_TYPE_INT, + CONFIG_TYPE_BOOL +}; + +struct config +{ + std::string key; + int type; +}; + +class ConfigLoader +{ +public: + virtual ~ConfigLoader() { } + + // Trigger loading of the configuration + virtual bool loadConfiguration() = 0; +}; + +class Configuration +{ +public: + static Configuration* i(); + + virtual ~Configuration() { } + + // Get the type of the configuration value + int getType(std::string key); + + // Retrieve a string based configuration value + std::string getString(std::string key, std::string ifEmpty = std::string("")); + + // Retrieve an integer configuration value + int getInt(std::string key, int ifEmpty = 0); + + // Retrieve a boolean configuration value + bool getBool(std::string key, bool ifEmpty = false); + + // Set a string based configuration value + void setString(std::string key, std::string value); + + // Set an integer based configuration value + void setInt(std::string key, int value); + + // Set a boolean configuration value + void setBool(std::string key, bool value); + + // Reload the configuration + bool reload(); + + // Reload the configuration using the specified configuration loader + bool reload(ConfigLoader* inConfigLoader); + +private: + Configuration(); + +#ifdef HAVE_CXX11 + static std::unique_ptr<Configuration> instance; +#else + static std::auto_ptr<Configuration> instance; +#endif + + std::map<std::string, std::string> stringConfiguration; + std::map<std::string, int> integerConfiguration; + std::map<std::string, bool> booleanConfiguration; + + ConfigLoader* configLoader; + + static const struct config valid_config[]; +}; + +#endif // !_SOFTHSM_V2_CONFIGURATION_H + diff --git a/SoftHSMv2/src/lib/common/HandleFactory.h b/SoftHSMv2/src/lib/common/HandleFactory.h new file mode 100644 index 0000000..0b9dc1f --- /dev/null +++ b/SoftHSMv2/src/lib/common/HandleFactory.h @@ -0,0 +1,121 @@ +/* + * 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. + */ + +/***************************************************************************** + HandleFactory.h + + This is a template class for handling handles ;-) + *****************************************************************************/ + +#ifndef _SOFTHSM_V2_HANDLEFACTORY_H +#define _SOFTHSM_V2_HANDLEFACTORY_H + +#include "config.h" +#include "log.h" +#include "MutexFactory.h" +#include <map> +#include <queue> + +template <class hType, class oType> class HandleFactory +{ +public: + // Constructor + HandleFactory() + { + nextFree = (hType) 1; + handleMutex = MutexFactory::i()->getMutex(); + } + + // Destructor + virtual ~HandleFactory() + { + MutexFactory::i()->recycleMutex(handleMutex); + } + + // Get a new handle for the specified object + hType getHandle(oType object) + { + MutexLocker lock(handleMutex); + + hType handle; + + if (!recycledHandles.empty()) + { + handle = recycledHandles.front(); + recycledHandles.pop(); + } + else + { + handle = nextFree++; + } + + handleMap[handle] = object; + + return handle; + } + + // Check whether the specified handle is valid + bool isValid(hType handle) + { + MutexLocker lock(handleMutex); + + return (handleMap.find(handle) != handleMap.end()); + } + + // Return the object for the specified handle + oType getObjectByHandle(hType handle) + { + MutexLocker lock(handleMutex); + + return handleMap[handle]; + } + + // Discard the specified handle + void deleteHandle(hType handle) + { + MutexLocker lock(handleMutex); + + handleMap.erase(handle); + + recycledHandles.push(handle); + } + +private: + // The handle map + std::map<hType, oType> handleMap; + + // The set of recycled handles + std::queue<hType> recycledHandles; + + // The next free handle + hType nextFree; + + // Cross-thread synchronisation + Mutex* handleMutex; +}; + +#endif // !_SOFTHSM_V2_HANDLEFACTORY_H + diff --git a/SoftHSMv2/src/lib/common/Makefile.am b/SoftHSMv2/src/lib/common/Makefile.am new file mode 100644 index 0000000..bf18b1a --- /dev/null +++ b/SoftHSMv2/src/lib/common/Makefile.am @@ -0,0 +1,28 @@ +MAINTAINERCLEANFILES = $(srcdir)/Makefile.in + +AM_CPPFLAGS = -I$(srcdir)/.. \ + -I$(srcdir)/../crypto \ + -I$(srcdir)/../data_mgr \ + -I$(srcdir)/../pkcs11 + +noinst_LTLIBRARIES = libsofthsm_common.la +libsofthsm_common_la_SOURCES = Configuration.cpp \ + fatal.cpp \ + log.cpp \ + osmutex.cpp \ + SimpleConfigLoader.cpp \ + MutexFactory.cpp + +man_MANS = softhsm2.conf.5 + +EXTRA_DIST = $(srcdir)/*.h \ + $(srcdir)/softhsm2.conf.5.in + +install-data-hook: + test -d ${DESTDIR}${sysconfdir} || \ + ${INSTALL} -d ${DESTDIR}${sysconfdir} + test -f ${DESTDIR}${sysconfdir}/softhsm2.conf || \ + ${INSTALL_DATA} ${top_builddir}/src/lib/common/softhsm2.conf ${DESTDIR}${sysconfdir} + ${INSTALL_DATA} ${top_builddir}/src/lib/common/softhsm2.conf ${DESTDIR}${sysconfdir}/softhsm2.conf.sample + test -d ${DESTDIR}${softhsmtokendir} || \ + ${INSTALL} -d -m 1777 ${DESTDIR}${softhsmtokendir} diff --git a/SoftHSMv2/src/lib/common/MutexFactory.cpp b/SoftHSMv2/src/lib/common/MutexFactory.cpp new file mode 100644 index 0000000..1cfc0da --- /dev/null +++ b/SoftHSMv2/src/lib/common/MutexFactory.cpp @@ -0,0 +1,192 @@ +/* + * 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. + */ + +/***************************************************************************** + MutexFactory.cpp + + This factory produces OS specific mutex objects + *****************************************************************************/ + +#include "config.h" +#include "MutexFactory.h" +#include "osmutex.h" +#include <memory> +#include <stddef.h> + +/***************************************************************************** + Mutex implementation + *****************************************************************************/ + +// Constructor +Mutex::Mutex() +{ + isValid = (MutexFactory::i()->CreateMutex(&handle) == CKR_OK); +} + +// Destructor +Mutex::~Mutex() +{ + if (isValid) + { + MutexFactory::i()->DestroyMutex(handle); + } +} + +// Lock the mutex +bool Mutex::lock() +{ + return (isValid && (MutexFactory::i()->LockMutex(handle) == CKR_OK)); +} + +// Unlock the mutex +void Mutex::unlock() +{ + if (isValid) + { + MutexFactory::i()->UnlockMutex(handle); + } +} + +/***************************************************************************** + MutexLocker implementation + *****************************************************************************/ + +// Constructor +MutexLocker::MutexLocker(Mutex* inMutex) +{ + mutex = inMutex; + + if (mutex != NULL) mutex->lock(); +} + +// Destructor +MutexLocker::~MutexLocker() +{ + if (mutex != NULL) mutex->unlock(); +} + +/***************************************************************************** + MutexFactory implementation + *****************************************************************************/ + +// Constructor +MutexFactory::MutexFactory() +{ + createMutex = OSCreateMutex; + destroyMutex = OSDestroyMutex; + lockMutex = OSLockMutex; + unlockMutex = OSUnlockMutex; + + enabled = true; +} + +// Destructor +MutexFactory::~MutexFactory() +{ +} + +// Return the one-and-only instance +MutexFactory* MutexFactory::i() +{ + if (!instance.get()) + { + instance.reset(new MutexFactory()); + } + + return instance.get(); +} + +// Get a mutex instance +Mutex* MutexFactory::getMutex() +{ + return new Mutex(); +} + +// Recycle a mutex instance +void MutexFactory::recycleMutex(Mutex* mutex) +{ + if (mutex != NULL) delete mutex; +} + +// Set the function pointers +void MutexFactory::setCreateMutex(CK_CREATEMUTEX inCreateMutex) +{ + createMutex = inCreateMutex; +} + +void MutexFactory::setDestroyMutex(CK_DESTROYMUTEX inDestroyMutex) +{ + destroyMutex = inDestroyMutex; +} + +void MutexFactory::setLockMutex(CK_LOCKMUTEX inLockMutex) +{ + lockMutex = inLockMutex; +} + +void MutexFactory::setUnlockMutex(CK_UNLOCKMUTEX inUnlockMutex) +{ + unlockMutex = inUnlockMutex; +} + +void MutexFactory::enable() +{ + enabled = true; +} + +void MutexFactory::disable() +{ + enabled = false; +} + +CK_RV MutexFactory::CreateMutex(CK_VOID_PTR_PTR newMutex) +{ + if (!enabled) return CKR_OK; + + return (this->createMutex)(newMutex); +} + +CK_RV MutexFactory::DestroyMutex(CK_VOID_PTR mutex) +{ + if (!enabled) return CKR_OK; + + return (this->destroyMutex)(mutex); +} + +CK_RV MutexFactory::LockMutex(CK_VOID_PTR mutex) +{ + if (!enabled) return CKR_OK; + + return (this->lockMutex)(mutex); +} + +CK_RV MutexFactory::UnlockMutex(CK_VOID_PTR mutex) +{ + if (!enabled) return CKR_OK; + + return (this->unlockMutex)(mutex); +} + diff --git a/SoftHSMv2/src/lib/common/MutexFactory.h b/SoftHSMv2/src/lib/common/MutexFactory.h new file mode 100644 index 0000000..167dc3d --- /dev/null +++ b/SoftHSMv2/src/lib/common/MutexFactory.h @@ -0,0 +1,133 @@ +/* + * 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. + */ + +/***************************************************************************** + MutexFactory.h + + This factory produces OS specific mutex objects + *****************************************************************************/ + +#ifndef _SOFTHSM_V2_MUTEXFACTORY_H +#define _SOFTHSM_V2_MUTEXFACTORY_H + +#include "config.h" +#include "osmutex.h" +#include "cryptoki.h" +#include <memory> + +class Mutex +{ +public: + // Constructor + Mutex(); + + // Destructor + virtual ~Mutex(); + + // Lock the mutex + bool lock(); + + // Unlock the mutex + void unlock(); + +private: + // The mutex handle + CK_VOID_PTR handle; + + // Is the mutex valid? + bool isValid; +}; + +class MutexLocker +{ +public: + // Constructor + MutexLocker(Mutex* inMutex); + + // Destructor + virtual ~MutexLocker(); + +private: + // The mutex to lock + Mutex* mutex; +}; + +class MutexFactory +{ +public: + // Return the one-and-only instance + static MutexFactory* i(); + + // Destructor + virtual ~MutexFactory(); + + // Get a mutex instance + Mutex* getMutex(); + + // Recycle a mutex instance + void recycleMutex(Mutex* mutex); + + // Set the function pointers + void setCreateMutex(CK_CREATEMUTEX inCreateMutex); + void setDestroyMutex(CK_DESTROYMUTEX inDestroyMutex); + void setLockMutex(CK_LOCKMUTEX inLockMutex); + void setUnlockMutex(CK_UNLOCKMUTEX inUnlockMutex); + + // Enable/disable mutex handling + void enable(); + void disable(); + +private: + // Constructor + MutexFactory(); + + // Mutex operations + friend class Mutex; + + CK_RV CreateMutex(CK_VOID_PTR_PTR newMutex); + CK_RV DestroyMutex(CK_VOID_PTR mutex); + CK_RV LockMutex(CK_VOID_PTR mutex); + CK_RV UnlockMutex(CK_VOID_PTR mutex); + + // The one-and-only instance +#ifdef HAVE_CXX11 + static std::unique_ptr<MutexFactory> instance; +#else + static std::auto_ptr<MutexFactory> instance; +#endif + + // The function pointers + CK_CREATEMUTEX createMutex; + CK_DESTROYMUTEX destroyMutex; + CK_LOCKMUTEX lockMutex; + CK_UNLOCKMUTEX unlockMutex; + + // Can we do mutex handling? + bool enabled; +}; + +#endif // !_SOFTHSM_V2_MUTEXFACTORY_H + diff --git a/SoftHSMv2/src/lib/common/Serialisable.h b/SoftHSMv2/src/lib/common/Serialisable.h new file mode 100644 index 0000000..819940e --- /dev/null +++ b/SoftHSMv2/src/lib/common/Serialisable.h @@ -0,0 +1,52 @@ +/* + * 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. + */ + +/***************************************************************************** + Serialisable.h + + Interface description for serialisable classes + *****************************************************************************/ + +#ifndef _SOFTHSM_V2_SERIALISABLE_H +#define _SOFTHSM_V2_SERIALISABLE_H + +#include "config.h" +#include "ByteString.h" + +class ByteString; + +class Serialisable +{ +public: + // Serialise the data content of a class + virtual ByteString serialise() const = 0; + + // Default destructor + virtual ~Serialisable() { } +}; + +#endif // !_SOFTHSM_V2_SERIALISABLE_H + diff --git a/SoftHSMv2/src/lib/common/SimpleConfigLoader.cpp b/SoftHSMv2/src/lib/common/SimpleConfigLoader.cpp new file mode 100644 index 0000000..3212d82 --- /dev/null +++ b/SoftHSMv2/src/lib/common/SimpleConfigLoader.cpp @@ -0,0 +1,352 @@ +/* + * Copyright (c) 2010 .SE, The Internet Infrastructure Foundation + * 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. + */ + +/***************************************************************************** + SimpleConfigLoader.cpp + + Loads the configuration from the configuration file. + *****************************************************************************/ + +#include <stdio.h> +#include <string.h> +#include <stdlib.h> +#include <algorithm> +#include <limits.h> +#ifdef _WIN32 +# include <io.h> +#else +# include <unistd.h> +#endif +#include "config.h" +#if defined(HAVE_GETPWUID_R) +# include <sys/types.h> +# include <pwd.h> +#endif +#include "SimpleConfigLoader.h" +#include "log.h" +#include "Configuration.h" + +// Initialise the one-and-only instance +#ifdef HAVE_CXX11 +std::unique_ptr<SimpleConfigLoader> SimpleConfigLoader::instance(nullptr); +#else +std::auto_ptr<SimpleConfigLoader> SimpleConfigLoader::instance(NULL); +#endif + +// Return the one-and-only instance +SimpleConfigLoader* SimpleConfigLoader::i() +{ + if (instance.get() == NULL) + { + instance.reset(new SimpleConfigLoader()); + } + + return instance.get(); +} + +// Constructor +SimpleConfigLoader::SimpleConfigLoader() +{ +} + +// Load the configuration +bool SimpleConfigLoader::loadConfiguration() +{ + char* configPath = getConfigPath(); + + FILE* fp = fopen(configPath,"r"); + + if (fp == NULL) + { + ERROR_MSG("Could not open the config file: %s", configPath); + free(configPath); + return false; + } + free(configPath); + + char fileBuf[1024]; + + // Format in config file + // + // <name> = <value> + // # Line is ignored + + size_t line = 0; + while (fgets(fileBuf, sizeof(fileBuf), fp) != NULL) + { + line++; + + // End the string at the first comment or newline + fileBuf[strcspn(fileBuf, "#\n\r")] = '\0'; + + // Skip empty lines + if (fileBuf[0] == '\0') + { + continue; + } + + // Get the first part of the line + char* name = strtok(fileBuf, "="); + if (name == NULL) + { + WARNING_MSG("Bad format on line %lu, skip", (unsigned long)line); + continue; + } + + // Trim the name + char* trimmedName = trimString(name); + if (trimmedName == NULL) + { + WARNING_MSG("Bad format on line %lu, skip", (unsigned long)line); + continue; + } + + // Get the second part of the line + char* value = strtok(NULL, "="); + if(value == NULL) { + WARNING_MSG("Bad format on line %lu, skip", (unsigned long)line); + free(trimmedName); + continue; + } + + // Trim the value + char* trimmedValue = trimString(value); + if (trimmedValue == NULL) + { + WARNING_MSG("Bad format on line %lu, skip", (unsigned long)line); + free(trimmedName); + continue; + } + + // Save name,value + std::string stringName(trimmedName); + std::string stringValue(trimmedValue); + free(trimmedName); + free(trimmedValue); + + switch (Configuration::i()->getType(stringName)) + { + case CONFIG_TYPE_STRING: + Configuration::i()->setString(stringName, stringValue); + break; + case CONFIG_TYPE_INT: + Configuration::i()->setInt(stringName, atoi(stringValue.c_str())); + break; + case CONFIG_TYPE_BOOL: + bool boolValue; + if (string2bool(stringValue, &boolValue)) + { + Configuration::i()->setBool(stringName, boolValue); + } + else + { + WARNING_MSG("The value %s is not a boolean", stringValue.c_str()); + } + break; + case CONFIG_TYPE_UNSUPPORTED: + default: + WARNING_MSG("The following configuration is not supported: %s = %s", + stringName.c_str(), stringValue.c_str()); + break; + } + } + + fclose(fp); + + return true; +} + +// Get the boolean value from a string +bool SimpleConfigLoader::string2bool(std::string stringValue, bool* boolValue) +{ + // Convert to lowercase + std::transform(stringValue.begin(), stringValue.end(), stringValue.begin(), tolower); + + if (stringValue.compare("true") == 0) + { + *boolValue = true; + return true; + } + + if (stringValue.compare("false") == 0) + { + *boolValue = false; + return true; + } + + return false; +} + +#define CONFIG_FILE ".config/softhsm2/softhsm2.conf" + +/* Returns a user-specific path for configuration. + */ +static char *get_user_path(void) +{ +#ifdef _WIN32 + char path[512]; + const char *home_drive = getenv("HOMEDRIVE"); + const char *home_path = getenv("HOMEPATH"); + + if (home_drive && home_path) { + snprintf(path, sizeof(path), "%s%s\\softhsm2.conf", home_drive, home_path); + + if (_access(path, 0) == 0) + return strdup(path); + } + goto fail; +#else + char path[_POSIX_PATH_MAX]; + const char *home_dir = getenv("HOME"); + + if (home_dir != NULL && home_dir[0] != 0) { + snprintf(path, sizeof(path), "%s/" CONFIG_FILE, home_dir); + if (access(path, R_OK) == 0) + return strdup(path); + else + goto fail; + } + +# if defined(HAVE_GETPWUID_R) + if (home_dir == NULL || home_dir[0] == '\0') { + struct passwd *pwd; + struct passwd _pwd; + int ret; + char tmp[512]; + + ret = getpwuid_r(getuid(), &_pwd, tmp, sizeof(tmp), &pwd); + if (ret == 0 && pwd != NULL) { + snprintf(path, sizeof(path), "%s/" CONFIG_FILE, pwd->pw_dir); + if (access(path, R_OK) == 0) + return strdup(path); + else + goto fail; + } + } +# endif +#endif + + fail: + return NULL; +} + +static char *get_env_var_path(void) +{ +#ifdef _WIN32 + + LPSTR value = NULL; + DWORD size = 0; + + size = GetEnvironmentVariableA("SOFTHSM2_CONF", value, size); + if (size == 0) { + return NULL; + } + + value = (LPSTR) malloc(size); + if (NULL == value) { + return NULL; + } + + if (GetEnvironmentVariableA("SOFTHSM2_CONF", value, size) != (size - 1)) { + free(value); + return NULL; + } + + return value; + +#else + + char *value = getenv("SOFTHSM2_CONF"); + + if (value == NULL) { + return value; + } else { + return strdup(value); + } + +#endif +} + +char* SimpleConfigLoader::getConfigPath() +{ + char* configPath = get_env_var_path(); + char* tpath; + + if (configPath != NULL) + { + return configPath; + } + else + { + tpath = get_user_path(); + if (tpath != NULL) + { + return tpath; + } + } + + return strdup(DEFAULT_SOFTHSM2_CONF); +} + +char* SimpleConfigLoader::trimString(char* text) +{ + if (text == NULL) + { + return NULL; + } + + int startPos = 0; + int endPos = strlen(text) - 1; + + // Find the first position without a space + while (startPos <= endPos && isspace((int)*(text + startPos))) + { + startPos++; + } + // Find the last position without a space + while (startPos <= endPos && isspace((int)*(text + endPos))) + { + endPos--; + } + + // We must have a valid string + int length = endPos - startPos + 1; + if (length <= 0) + { + return NULL; + } + + // Create the trimmed text + char* trimmedText = (char*)malloc(length + 1); + if (trimmedText == NULL) + { + return NULL; + } + trimmedText[length] = '\0'; + memcpy(trimmedText, text + startPos, length); + + return trimmedText; +} diff --git a/SoftHSMv2/src/lib/common/SimpleConfigLoader.h b/SoftHSMv2/src/lib/common/SimpleConfigLoader.h new file mode 100644 index 0000000..e992cfb --- /dev/null +++ b/SoftHSMv2/src/lib/common/SimpleConfigLoader.h @@ -0,0 +1,63 @@ +/* + * Copyright (c) 2010 .SE, The Internet Infrastructure Foundation + * 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. + */ + +/***************************************************************************** + SimpleConfigLoader.h + + Loads the configuration from the configuration file. + *****************************************************************************/ + +#ifndef _SOFTHSM_V2_SIMPLECONFIGLOADER_H +#define _SOFTHSM_V2_SIMPLECONFIGLOADER_H + +#include <memory> +#include "config.h" +#include "Configuration.h" + +class SimpleConfigLoader : public ConfigLoader +{ +public: + static SimpleConfigLoader* i(); + + virtual ~SimpleConfigLoader() { } + + virtual bool loadConfiguration(); + +private: + SimpleConfigLoader(); + char* getConfigPath(); + char* trimString(char* text); + bool string2bool(std::string stringValue, bool* boolValue); + +#ifdef HAVE_CXX11 + static std::unique_ptr<SimpleConfigLoader> instance; +#else + static std::auto_ptr<SimpleConfigLoader> instance; +#endif +}; + +#endif // !_SOFTHSM_V2_SIMPLECONFIGLOADER_H + diff --git a/SoftHSMv2/src/lib/common/fatal.cpp b/SoftHSMv2/src/lib/common/fatal.cpp new file mode 100644 index 0000000..4da26f4 --- /dev/null +++ b/SoftHSMv2/src/lib/common/fatal.cpp @@ -0,0 +1,64 @@ +/* + * 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. + */ + +/***************************************************************************** + fatal.cpp + + Implementens calls for handling fatal exceptions. When a fatal exception + occurs, this code ensures that as much of the securely allocated memory as + possible is wiped clean. + *****************************************************************************/ + +#include "config.h" +#include "log.h" +#include "fatal.h" +#include "SecureMemoryRegistry.h" +#include "cryptoki.h" + +void FatalException(void) +{ + try + { + DEBUG_MSG("Fatal exception handler called"); + } + catch (...) + { + } + + // Wipe as much of the securely allocated memory as possible + SecureMemoryRegistry::i()->wipe(); + + try + { + ERROR_MSG("A fatal exception occurred; exiting..."); + } + catch (...) + { + } + + exit(CKR_GENERAL_ERROR); +} + diff --git a/SoftHSMv2/src/lib/common/fatal.h b/SoftHSMv2/src/lib/common/fatal.h new file mode 100644 index 0000000..124265e --- /dev/null +++ b/SoftHSMv2/src/lib/common/fatal.h @@ -0,0 +1,52 @@ +/* + * 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. + */ + +/***************************************************************************** + fatal.h + + Implementens calls for handling fatal exceptions. When a fatal exception + occurs, this code ensures that as much of the securely allocated memory as + possible is wiped clean. + *****************************************************************************/ + +#ifndef _SOFTHSM_V2_FATAL_H +#define _SOFTHSM_V2_FATAL_H + +#include "config.h" +#include "log.h" + +#if defined(__cplusplus) +extern "C" { +#endif // __cplusplus + +void FatalException(void); + +#if defined(__cplusplus) +} +#endif + +#endif // !_SOFTHSM_V2_FATAL_H + diff --git a/SoftHSMv2/src/lib/common/log.cpp b/SoftHSMv2/src/lib/common/log.cpp new file mode 100644 index 0000000..7400f6a --- /dev/null +++ b/SoftHSMv2/src/lib/common/log.cpp @@ -0,0 +1,109 @@ +/* + * 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. + */ + +/***************************************************************************** + log.cpp + + Implements logging functions. This file is based on the concepts from + SoftHSM v1 but extends the logging functions with support for a variable + argument list as defined in stdarg (3). + *****************************************************************************/ + +#include "config.h" +#include <stdarg.h> +#include <syslog.h> +#include <stdio.h> +#include <sstream> +#include <vector> +#include "log.h" + +int softLogLevel = LOG_DEBUG; + +bool setLogLevel(const std::string &loglevel) +{ + if (loglevel == "ERROR") + { + softLogLevel = LOG_ERR; + } + else if (loglevel == "WARNING") + { + softLogLevel = LOG_WARNING; + } + else if (loglevel == "INFO") + { + softLogLevel = LOG_INFO; + } + else if (loglevel == "DEBUG") + { + softLogLevel = LOG_DEBUG; + } + else + { + ERROR_MSG("Unknown value (%s) for log.level in configuration", loglevel.c_str()); + return false; + } + + return true; +} + +void softHSMLog(const int loglevel, const char* functionName, const char* fileName, const int lineNo, const char* format, ...) +{ + if (loglevel > softLogLevel) return; + + std::stringstream prepend; + +#ifdef SOFTHSM_LOG_FILE_AND_LINE + prepend << fileName << "(" << lineNo << ")"; +#ifndef SOFTHSM_LOG_FUNCTION_NAME + (void) functionName; + prepend << ":"; +#endif // !SOFTHSM_LOG_FUNCTION_NAME + prepend << " "; +#endif // SOFTHSM_LOG_FILE_AND_LINE + +#ifdef SOFTHSM_LOG_FUNCTION_NAME + prepend << functionName << ": "; +#endif // SOFTHSM_LOG_FUNCTION_NAME + + // Print the format to a log message + std::vector<char> logMessage; + va_list args; + + logMessage.resize(4096); + + va_start(args, format); + vsnprintf(&logMessage[0], 4096, format, args); + va_end(args); + + // And log it + syslog(loglevel, "%s%s", prepend.str().c_str(), &logMessage[0]); + +#ifdef DEBUG_LOG_STDERR + fprintf(stderr, "%s%s\n", prepend.str().c_str(), &logMessage[0]); + fflush(stderr); +#endif // DEBUG_LOG_STDERR +} + diff --git a/SoftHSMv2/src/lib/common/log.h b/SoftHSMv2/src/lib/common/log.h new file mode 100644 index 0000000..cf91aaf --- /dev/null +++ b/SoftHSMv2/src/lib/common/log.h @@ -0,0 +1,85 @@ +/* + * 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. + */ + +/***************************************************************************** + log.h + + Implements logging functions. This file is based on the concepts from + SoftHSM v1 but extends the logging functions with support for a variable + argument list as defined in stdarg (3). + *****************************************************************************/ + +#ifndef _SOFTHSM_V2_LOG_H +#define _SOFTHSM_V2_LOG_H + +#include "config.h" + +#include <syslog.h> +#include <string> + +/* Unset this define if you don't want to log the source file name and line number */ +#define SOFTHSM_LOG_FILE_AND_LINE + +/* Set this define to log the function name */ +/* #define SOFTHSM_LOG_FUNCTION_NAME */ + +/* Define this symbol (either here or in the build setup) to log to stderr */ +/* #define DEBUG_LOG_STDERR */ + +/* Logging errors */ +#ifndef _WIN32 +#define ERROR_MSG(...) softHSMLog(LOG_ERR, __func__, __FILE__, __LINE__, __VA_ARGS__); +#else +#define ERROR_MSG(...) softHSMLog(LOG_ERR, __FUNCTION__, __FILE__, __LINE__, __VA_ARGS__); +#endif + +/* Logging warnings */ +#ifndef _WIN32 +#define WARNING_MSG(...) softHSMLog(LOG_WARNING, __func__, __FILE__, __LINE__, __VA_ARGS__); +#else +#define WARNING_MSG(...) softHSMLog(LOG_WARNING, __FUNCTION__, __FILE__, __LINE__, __VA_ARGS__); +#endif + +/* Logging information */ +#ifndef _WIN32 +#define INFO_MSG(...) softHSMLog(LOG_INFO, __func__, __FILE__, __LINE__, __VA_ARGS__); +#else +#define INFO_MSG(...) softHSMLog(LOG_INFO, __FUNCTION__, __FILE__, __LINE__, __VA_ARGS__); +#endif + +/* Logging debug information */ +#ifndef _WIN32 +#define DEBUG_MSG(...) softHSMLog(LOG_DEBUG, __func__, __FILE__, __LINE__, __VA_ARGS__); +#else +#define DEBUG_MSG(...) softHSMLog(LOG_DEBUG, __FUNCTION__, __FILE__, __LINE__, __VA_ARGS__); +#endif + +/* Function definitions */ +bool setLogLevel(const std::string &loglevel); +void softHSMLog(const int loglevel, const char* functionName, const char* fileName, const int lineNo, const char* format, ...); + +#endif /* !_SOFTHSM_V2_LOG_H */ + diff --git a/SoftHSMv2/src/lib/common/osmutex.cpp b/SoftHSMv2/src/lib/common/osmutex.cpp new file mode 100644 index 0000000..28db10e --- /dev/null +++ b/SoftHSMv2/src/lib/common/osmutex.cpp @@ -0,0 +1,242 @@ +/* + * Copyright (c) 2008-2010 .SE (The Internet Infrastructure Foundation). + * 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. + */ + +/***************************************************************************** + osmutex.cpp + + Contains OS-specific implementations of intraprocess mutex functions. This + implementation is based on SoftHSM v1 + *****************************************************************************/ + +#include "config.h" +#include "log.h" +#include "osmutex.h" + +#ifdef HAVE_PTHREAD_H + +#include <stdlib.h> +#include <pthread.h> + +CK_RV OSCreateMutex(CK_VOID_PTR_PTR newMutex) +{ + int rv; + + /* Allocate memory */ + pthread_mutex_t* pthreadMutex = (pthread_mutex_t*) malloc(sizeof(pthread_mutex_t)); + + if (pthreadMutex == NULL) + { + ERROR_MSG("Failed to allocate memory for a new mutex"); + + return CKR_HOST_MEMORY; + } + + /* Initialise the mutex */ + if ((rv = pthread_mutex_init(pthreadMutex, NULL)) != 0) + { + free(pthreadMutex); + + ERROR_MSG("Failed to initialise POSIX mutex (0x%08X)", rv); + + return CKR_GENERAL_ERROR; + } + + *newMutex = pthreadMutex; + + return CKR_OK; +} + +CK_RV OSDestroyMutex(CK_VOID_PTR mutex) +{ + int rv; + pthread_mutex_t* pthreadMutex = (pthread_mutex_t*) mutex; + + if (pthreadMutex == NULL) + { + ERROR_MSG("Cannot destroy NULL mutex"); + + return CKR_ARGUMENTS_BAD; + } + + if ((rv = pthread_mutex_destroy(pthreadMutex)) != 0) + { + ERROR_MSG("Failed to destroy POSIX mutex (0x%08X)", rv); + + return CKR_GENERAL_ERROR; + } + + free(pthreadMutex); + + return CKR_OK; +} + +CK_RV OSLockMutex(CK_VOID_PTR mutex) +{ + int rv; + pthread_mutex_t* pthreadMutex = (pthread_mutex_t*) mutex; + + if (pthreadMutex == NULL) + { + ERROR_MSG("Cannot lock NULL mutex"); + + return CKR_ARGUMENTS_BAD; + } + + if ((rv = pthread_mutex_lock(pthreadMutex)) != 0) + { + ERROR_MSG("Failed to lock POSIX mutex 0x%08X (0x%08X)", pthreadMutex, rv); + + return CKR_GENERAL_ERROR; + } + + return CKR_OK; +} + +CK_RV OSUnlockMutex(CK_VOID_PTR mutex) +{ + int rv; + pthread_mutex_t* pthreadMutex = (pthread_mutex_t*) mutex; + + if (pthreadMutex == NULL) + { + ERROR_MSG("Cannot unlock NULL mutex"); + + return CKR_ARGUMENTS_BAD; + } + + if ((rv = pthread_mutex_unlock(pthreadMutex)) != 0) + { + ERROR_MSG("Failed to unlock POSIX mutex 0x%08X (0x%08X)", pthreadMutex, rv); + + return CKR_GENERAL_ERROR; + } + + return CKR_OK; +} + +#elif _WIN32 + +CK_RV OSCreateMutex(CK_VOID_PTR_PTR newMutex) +{ + HANDLE hMutex; + + hMutex = CreateMutex(NULL, FALSE, NULL); + if (hMutex == NULL) + { + DWORD rv = GetLastError(); + + ERROR_MSG("Failed to initialise WIN32 mutex (0x%08X)", rv); + + return CKR_GENERAL_ERROR; + } + + *newMutex = hMutex; + + return CKR_OK; +} + +CK_RV OSDestroyMutex(CK_VOID_PTR mutex) +{ + HANDLE hMutex = (HANDLE) mutex; + + if (hMutex == NULL) + { + ERROR_MSG("Cannot destroy NULL mutex"); + + return CKR_ARGUMENTS_BAD; + } + + if (CloseHandle(hMutex) == 0) + { + DWORD rv = GetLastError(); + + ERROR_MSG("Failed to destroy WIN32 mutex (0x%08X)", rv); + + return CKR_GENERAL_ERROR; + } + + return CKR_OK; +} + +CK_RV OSLockMutex(CK_VOID_PTR mutex) +{ + DWORD rv; + HANDLE hMutex = (HANDLE) mutex; + + if (hMutex == NULL) + { + ERROR_MSG("Cannot lock NULL mutex"); + + return CKR_ARGUMENTS_BAD; + } + + rv = WaitForSingleObject(hMutex, INFINITE); + if (rv != WAIT_OBJECT_0) + { + // WAIT_ABANDONED 0x00000080 + // WAIT_OBJECT_0 0x00000000 + // WAIT_TIMEOUT 0x00000102 + // WAIT_FAILED 0xFFFFFFFF + + if (rv == WAIT_FAILED) + rv = GetLastError(); + + ERROR_MSG("Failed to lock WIN32 mutex 0x%08X (0x%08X)", hMutex, rv); + + return CKR_GENERAL_ERROR; + } + + return CKR_OK; +} + +CK_RV OSUnlockMutex(CK_VOID_PTR mutex) +{ + HANDLE hMutex = (HANDLE) mutex; + + if (hMutex == NULL) + { + ERROR_MSG("Cannot unlock NULL mutex"); + + return CKR_ARGUMENTS_BAD; + } + + if (ReleaseMutex(hMutex) == 0) + { + DWORD rv = GetLastError(); + + ERROR_MSG("Failed to unlock WIN32 mutex 0x%08X (0x%08X)", hMutex, rv); + + return CKR_GENERAL_ERROR; + } + + return CKR_OK; +} + +#else +#error "There are no mutex implementations for your operating system yet" +#endif + diff --git a/SoftHSMv2/src/lib/common/osmutex.h b/SoftHSMv2/src/lib/common/osmutex.h new file mode 100644 index 0000000..103dc67 --- /dev/null +++ b/SoftHSMv2/src/lib/common/osmutex.h @@ -0,0 +1,47 @@ +/* + * Copyright (c) 2008-2010 .SE (The Internet Infrastructure Foundation). + * 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. + */ + +/***************************************************************************** + osmutex.h + + Contains OS-specific implementations of intraprocess mutex functions. This + implementation is based on SoftHSM v1 + *****************************************************************************/ + +#ifndef _SOFTHSM_V2_OSMUTEX_H +#define _SOFTHSM_V2_OSMUTEX_H + +#include "config.h" +#include "cryptoki.h" + +CK_RV OSCreateMutex(CK_VOID_PTR_PTR newMutex); +CK_RV OSDestroyMutex(CK_VOID_PTR mutex); +CK_RV OSLockMutex(CK_VOID_PTR mutex); +CK_RV OSUnlockMutex(CK_VOID_PTR mutex); + +#endif /* !_SOFTHSM_V2_OSMUTEX_H */ + diff --git a/SoftHSMv2/src/lib/common/softhsm2.conf.5.in b/SoftHSMv2/src/lib/common/softhsm2.conf.5.in new file mode 100644 index 0000000..5291a09 --- /dev/null +++ b/SoftHSMv2/src/lib/common/softhsm2.conf.5.in @@ -0,0 +1,86 @@ +.TH softhsm2.conf 5 "30 October 2014" "SoftHSM" +.SH NAME +softhsm2.conf \- SoftHSM configuration file +.SH SYNOPSIS +.B softhsm2.conf +.SH DESCRIPTION +This is the configuration file for SoftHSM. It can be found on a +default location, but can also be relocated by using the +environment variable. Any configuration must be done according +to the file format found in this document. +.SH FILE FORMAT +Each configuration option is a pair of name and value separated by +a equality sign. The configuration option must be located on a single line. +.LP +.RS +.nf +<name> = <value> +.fi +.RE +.LP +It is also possible to add comments in the file by using the hash sign. +Anything after the hash sign will be ignored. +.LP +.RS +.nf +# A comment +.RE +.LP +Any empty lines or lines that does not have the correct format will be ignored. +.SH DIRECTORIES.TOKENDIR +The location where SoftHSM can store the tokens. +.LP +.RS +.nf +directories.tokendir = @softhsmtokendir@ +.fi +.RE +.LP +.SH OBJECTSTORE.BACKEND +The backend to use by SoftHSM to store token objects. Either "file" or "db" is supported. +In order to use the "db" backend, the SoftHSM build needs to be configured with "configure --with-objectstore-backend-db" +.LP +.RS +.nf +objectstore.backend = file +.fi +.RE +.LP +.SH LOG.LEVEL +The log level which can be set to ERROR, WARNING, INFO or DEBUG. +.LP +.RS +.nf +log.level = INFO +.fi +.RE +.LP +.SH SLOTS.REMOVABLE +If set to true CKF_REMOVABLE_DEVICE is set in the flags returned by C_GetSlotInfo. Default is false. +.LP +.RS +.nf +slots.removable = true +.fi +.RE +.LP +.SH ENVIRONMENT +.TP +SOFTHSM2_CONF +When defined, the value will be used as path to the configuration file. +.SH FILES +.TP +.I ~/.config/softhsm2/softhsm2.conf +default user-specific location of the SoftHSM configuration file; if it exists it will override the system wide configuration +.TP +.I @default_softhsm2_conf@ +default system-wide location of the SoftHSM configuration file +.TP +.I @default_softhsm2_conf@.sample +an example of a SoftHSM configuration file +.SH AUTHOR +Written by Rickard Bellgrim, Francis Dupont, René Post, and Roland van Rijswijk. +.SH "SEE ALSO" +.IR softhsm2-keyconv (1), +.IR softhsm2-migrate (1), +.IR softhsm2-util (1) diff --git a/SoftHSMv2/src/lib/common/softhsm2.conf.in b/SoftHSMv2/src/lib/common/softhsm2.conf.in new file mode 100644 index 0000000..3d5728d --- /dev/null +++ b/SoftHSMv2/src/lib/common/softhsm2.conf.in @@ -0,0 +1,10 @@ +# SoftHSM v2 configuration file + +directories.tokendir = @softhsmtokendir@ +objectstore.backend = file + +# ERROR, WARNING, INFO, DEBUG +log.level = ERROR + +# If CKF_REMOVABLE_DEVICE flag should be set +slots.removable = false |