]> git.ipfire.org Git - thirdparty/strongswan.git/commitdiff
Moved PKCS#11 library loading to dedicated manager
authorMartin Willi <martin@revosec.ch>
Wed, 14 Jul 2010 09:15:22 +0000 (11:15 +0200)
committerMartin Willi <martin@revosec.ch>
Wed, 4 Aug 2010 07:26:19 +0000 (09:26 +0200)
src/libstrongswan/plugins/pkcs11/Makefile.am
src/libstrongswan/plugins/pkcs11/pkcs11_manager.c [new file with mode: 0644]
src/libstrongswan/plugins/pkcs11/pkcs11_manager.h [new file with mode: 0644]
src/libstrongswan/plugins/pkcs11/pkcs11_plugin.c

index 9df812e12bc6e208b5b5cbe986f8f54af079e39a..8babc34a5f38792fdf8de5b7d35e93951a14b819 100644 (file)
@@ -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 (file)
index 0000000..5d9f27f
--- /dev/null
@@ -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 <http://www.fsf.org/copyleft/gpl.txt>.
+ *
+ * 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 <debug.h>
+#include <utils/linked_list.h>
+
+#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 (file)
index 0000000..c89f251
--- /dev/null
@@ -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 <http://www.fsf.org/copyleft/gpl.txt>.
+ *
+ * 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_ @}*/
index 8393407343bb2a0cf82f785b63261920fa6e142d..1f682a33e3241925f6f4567a26adc2152fd3c1f1 100644 (file)
 #include "pkcs11_plugin.h"
 
 #include <library.h>
-#include <debug.h>
-#include <utils/linked_list.h>
 
-#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;
 }