From: Martin Willi Date: Wed, 14 Jul 2010 09:15:22 +0000 (+0200) Subject: Moved PKCS#11 library loading to dedicated manager X-Git-Tag: 4.5.0~627 X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=2e209becbca011224d1484ea3dce49af552928b8;p=thirdparty%2Fstrongswan.git Moved PKCS#11 library loading to dedicated manager --- diff --git a/src/libstrongswan/plugins/pkcs11/Makefile.am b/src/libstrongswan/plugins/pkcs11/Makefile.am index 9df812e12b..8babc34a5f 100644 --- a/src/libstrongswan/plugins/pkcs11/Makefile.am +++ b/src/libstrongswan/plugins/pkcs11/Makefile.am @@ -11,6 +11,7 @@ endif libstrongswan_pkcs11_la_SOURCES = \ pkcs11_plugin.h pkcs11_plugin.c pkcs11.h \ - pkcs11_library.h pkcs11_library.c + pkcs11_library.h pkcs11_library.c \ + pkcs11_manager.h pkcs11_manager.c libstrongswan_pkcs11_la_LDFLAGS = -module -avoid-version diff --git a/src/libstrongswan/plugins/pkcs11/pkcs11_manager.c b/src/libstrongswan/plugins/pkcs11/pkcs11_manager.c new file mode 100644 index 0000000000..5d9f27f186 --- /dev/null +++ b/src/libstrongswan/plugins/pkcs11/pkcs11_manager.c @@ -0,0 +1,86 @@ +/* + * Copyright (C) 2010 Martin Willi + * Copyright (C) 2010 revosec AG + * + * This program is free software; you can redistribute it and/or modify it + * under the terms of the GNU General Public License as published by the + * Free Software Foundation; either version 2 of the License, or (at your + * option) any later version. See . + * + * This program is distributed in the hope that it will be useful, but + * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY + * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License + * for more details. + */ + +#include "pkcs11_manager.h" + +#include +#include + +#include "pkcs11_library.h" + +typedef struct private_pkcs11_manager_t private_pkcs11_manager_t; + +/** + * Private data of an pkcs11_manager_t object. + */ +struct private_pkcs11_manager_t { + + /** + * Public pkcs11_manager_t interface. + */ + pkcs11_manager_t public; + + /** + * List of loaded libraries + */ + linked_list_t *libs; +}; + + +METHOD(pkcs11_manager_t, destroy, void, + private_pkcs11_manager_t *this) +{ + this->libs->destroy_offset(this->libs, offsetof(pkcs11_library_t, destroy)); + free(this); +} + +/** + * See header + */ +pkcs11_manager_t *pkcs11_manager_create() +{ + private_pkcs11_manager_t *this; + enumerator_t *enumerator; + char *module, *path; + + INIT(this, + .public = { + .destroy = _destroy, + }, + .libs = linked_list_create(), + ); + + enumerator = lib->settings->create_section_enumerator(lib->settings, + "libstrongswan.plugins.pkcs11.modules"); + while (enumerator->enumerate(enumerator, &module)) + { + pkcs11_library_t *p11lib; + + path = lib->settings->get_str(lib->settings, + "libstrongswan.plugins.pkcs11.modules.%s.path", NULL, module); + if (!path) + { + DBG1(DBG_CFG, "PKCS11 module '%s' misses library path", module); + continue; + } + p11lib = pkcs11_library_create(module, path); + if (p11lib) + { + this->libs->insert_last(this->libs, p11lib); + } + } + enumerator->destroy(enumerator); + return &this->public; +} diff --git a/src/libstrongswan/plugins/pkcs11/pkcs11_manager.h b/src/libstrongswan/plugins/pkcs11/pkcs11_manager.h new file mode 100644 index 0000000000..c89f251e78 --- /dev/null +++ b/src/libstrongswan/plugins/pkcs11/pkcs11_manager.h @@ -0,0 +1,42 @@ +/* + * Copyright (C) 2010 Martin Willi + * Copyright (C) 2010 revosec AG + * + * This program is free software; you can redistribute it and/or modify it + * under the terms of the GNU General Public License as published by the + * Free Software Foundation; either version 2 of the License, or (at your + * option) any later version. See . + * + * This program is distributed in the hope that it will be useful, but + * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY + * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License + * for more details. + */ + +/** + * @defgroup pkcs11_manager pkcs11_manager + * @{ @ingroup pkcs11 + */ + +#ifndef PKCS11_MANAGER_H_ +#define PKCS11_MANAGER_H_ + +typedef struct pkcs11_manager_t pkcs11_manager_t; + +/** + * Manages multiple PKCS#11 libraries with hot pluggable slots + */ +struct pkcs11_manager_t { + + /** + * Destroy a pkcs11_manager_t. + */ + void (*destroy)(pkcs11_manager_t *this); +}; + +/** + * Create a pkcs11_manager instance. + */ +pkcs11_manager_t *pkcs11_manager_create(); + +#endif /** PKCS11_MANAGER_H_ @}*/ diff --git a/src/libstrongswan/plugins/pkcs11/pkcs11_plugin.c b/src/libstrongswan/plugins/pkcs11/pkcs11_plugin.c index 8393407343..1f682a33e3 100644 --- a/src/libstrongswan/plugins/pkcs11/pkcs11_plugin.c +++ b/src/libstrongswan/plugins/pkcs11/pkcs11_plugin.c @@ -16,10 +16,8 @@ #include "pkcs11_plugin.h" #include -#include -#include -#include "pkcs11_library.h" +#include "pkcs11_manager.h" typedef struct private_pkcs11_plugin_t private_pkcs11_plugin_t; @@ -34,15 +32,15 @@ struct private_pkcs11_plugin_t { pkcs11_plugin_t public; /** - * List of loaded libraries + * PKCS#11 library/slot manager */ - linked_list_t *libs; + pkcs11_manager_t *manager; }; METHOD(plugin_t, destroy, void, private_pkcs11_plugin_t *this) { - this->libs->destroy_offset(this->libs, offsetof(pkcs11_library_t, destroy)); + this->manager->destroy(this->manager); free(this); } @@ -52,33 +50,11 @@ METHOD(plugin_t, destroy, void, plugin_t *pkcs11_plugin_create() { private_pkcs11_plugin_t *this; - enumerator_t *enumerator; - char *module, *path; INIT(this, .public.plugin.destroy = _destroy, - .libs = linked_list_create(), + .manager = pkcs11_manager_create(), ); - enumerator = lib->settings->create_section_enumerator(lib->settings, - "libstrongswan.plugins.pkcs11.modules"); - while (enumerator->enumerate(enumerator, &module)) - { - pkcs11_library_t *p11lib; - - path = lib->settings->get_str(lib->settings, - "libstrongswan.plugins.pkcs11.modules.%s.path", NULL, module); - if (!path) - { - DBG1(DBG_CFG, "PKCS11 module '%s' misses library path", module); - continue; - } - p11lib = pkcs11_library_create(module, path); - if (p11lib) - { - this->libs->insert_last(this->libs, p11lib); - } - } - enumerator->destroy(enumerator); return &this->public.plugin; }