From 2f3414c1bb2af4db4b03282b47decddf4f3e27e9 Mon Sep 17 00:00:00 2001 From: lloyd Date: Tue, 27 Jul 2010 20:58:47 +0000 Subject: Add a new utility class Dynamically_Loaded_Library which wraps around the system dynamic linker (if any). Currently it only supports dlopen, and is only enabled on Linux. It will almost certainly work on BSDs and Solaris as well, though, and should be easy to extend to support Win32-style dynamic loading. Also add a new engine, Dynamically_Loaded_Engine, which loads up a new Engine object from a shared library/DLL. --- src/engine/dyn_engine/dyn_engine.cpp | 67 +++++++++++++++++++++++ src/engine/dyn_engine/dyn_engine.h | 100 +++++++++++++++++++++++++++++++++++ src/engine/dyn_engine/info.txt | 18 +++++++ 3 files changed, 185 insertions(+) create mode 100644 src/engine/dyn_engine/dyn_engine.cpp create mode 100644 src/engine/dyn_engine/dyn_engine.h create mode 100644 src/engine/dyn_engine/info.txt (limited to 'src/engine/dyn_engine') diff --git a/src/engine/dyn_engine/dyn_engine.cpp b/src/engine/dyn_engine/dyn_engine.cpp new file mode 100644 index 000000000..2dc76d7d6 --- /dev/null +++ b/src/engine/dyn_engine/dyn_engine.cpp @@ -0,0 +1,67 @@ +/** +* Dynamically Loaded Engine +* (C) 2010 Jack Lloyd +* +* Distributed under the terms of the Botan license +*/ + +#include +#include + +namespace Botan { + +namespace { + +extern "C" { + typedef Engine* (*creator_function)(void); + typedef void (*destructor_function)(Engine*); +} + +} + +Dynamically_Loaded_Engine::Dynamically_Loaded_Engine( + const std::string& library_path) : + engine(0) + { + lib = new Dynamically_Loaded_Library(library_path); + + try + { + creator_function creator = lib->resolve("create_engine"); + + engine = creator(); + + if(!engine) + throw std::runtime_error("Creator function in " + library_path + " failed"); + } + catch(...) + { + delete lib; + lib = 0; + throw; + } + } + +Dynamically_Loaded_Engine::~Dynamically_Loaded_Engine() + { + if(lib && engine) + { + try + { + destructor_function destroy = + lib->resolve("destroy_engine"); + destroy(engine); + } + catch(...) + { + delete lib; + lib = 0; + throw; + } + } + + if(lib) + delete lib; + } + +} diff --git a/src/engine/dyn_engine/dyn_engine.h b/src/engine/dyn_engine/dyn_engine.h new file mode 100644 index 000000000..46752f5a9 --- /dev/null +++ b/src/engine/dyn_engine/dyn_engine.h @@ -0,0 +1,100 @@ +/** +* Dynamically Loaded Engine +* (C) 2010 Jack Lloyd +* +* Distributed under the terms of the Botan license +*/ + +#include + +namespace Botan { + +/** +* Dynamically_Loaded_Engine just proxies the requests to the underlying +* Engine object, and handles load/unload details +*/ +class BOTAN_DLL Dynamically_Loaded_Engine : public Engine + { + public: + /** + * @param lib_path full pathname to DLL to load + */ + Dynamically_Loaded_Engine(const std::string& lib_path); + + ~Dynamically_Loaded_Engine(); + + std::string provider_name() const { return engine->provider_name(); } + + BlockCipher* find_block_cipher(const SCAN_Name& algo_spec, + Algorithm_Factory& af) const + { + return engine->find_block_cipher(algo_spec, af); + } + + StreamCipher* find_stream_cipher(const SCAN_Name& algo_spec, + Algorithm_Factory& af) const + { + return engine->find_stream_cipher(algo_spec, af); + } + + HashFunction* find_hash(const SCAN_Name& algo_spec, + Algorithm_Factory& af) const + { + return engine->find_hash(algo_spec, af); + } + + MessageAuthenticationCode* find_mac(const SCAN_Name& algo_spec, + Algorithm_Factory& af) const + { + return engine->find_mac(algo_spec, af); + } + + Modular_Exponentiator* mod_exp(const BigInt& n, + Power_Mod::Usage_Hints hints) const + { + return engine->mod_exp(n, hints); + } + + Keyed_Filter* get_cipher(const std::string& algo_spec, + Cipher_Dir dir, + Algorithm_Factory& af) + { + return engine->get_cipher(algo_spec, dir, af); + } + + PK_Ops::Key_Agreement* + get_key_agreement_op(const Private_Key& key) const + { + return engine->get_key_agreement_op(key); + } + + PK_Ops::Signature* + get_signature_op(const Private_Key& key) const + { + return engine->get_signature_op(key); + } + + PK_Ops::Verification* + get_verify_op(const Public_Key& key) const + { + return engine->get_verify_op(key); + } + + PK_Ops::Encryption* + get_encryption_op(const Public_Key& key) const + { + return engine->get_encryption_op(key); + } + + PK_Ops::Decryption* + get_decryption_op(const Private_Key& key) const + { + return engine->get_decryption_op(key); + } + + private: + class Dynamically_Loaded_Library* lib; + Engine* engine; + }; + +} diff --git a/src/engine/dyn_engine/info.txt b/src/engine/dyn_engine/info.txt new file mode 100644 index 000000000..469583522 --- /dev/null +++ b/src/engine/dyn_engine/info.txt @@ -0,0 +1,18 @@ +define DYNAMICALLY_LOADED_ENGINE + + +dyn_engine.h + + + +dyn_engine.cpp + + + +engine +dyn_load + + + +linux -> dl + -- cgit v1.2.3