diff options
-rw-r--r-- | include/modules.h | 42 | ||||
-rw-r--r-- | src/init_def.cpp | 78 | ||||
-rw-r--r-- | src/modules.cpp | 68 |
3 files changed, 101 insertions, 87 deletions
diff --git a/include/modules.h b/include/modules.h index e09c18e9b..7441134d1 100644 --- a/include/modules.h +++ b/include/modules.h @@ -6,32 +6,42 @@ #ifndef BOTAN_MODULE_FACTORIES_H__ #define BOTAN_MODULE_FACTORIES_H__ -#include <vector> #include <string> +#include <vector> namespace Botan { /************************************************* -* Forward Declarations * +* Module Builder Interface * *************************************************/ -class Mutex_Factory; -class Timer; -class EntropySource; -class Engine; -class Allocator; +class Modules + { + public: + void load(class Library_State&) const; + + virtual class Mutex_Factory* mutex_factory() const; + virtual class Timer* timer() const; -namespace Modules { + virtual std::vector<class Allocator*> allocators() const; + virtual std::vector<class EntropySource*> entropy_sources() const; + virtual std::vector<class Engine*> engines() const; + + virtual ~Modules() {} + }; /************************************************* -* Get the module objects * +* Built In Modules * *************************************************/ -class Mutex_Factory* get_mutex_factory(); -class Timer* get_timer(); -std::vector<EntropySource*> get_entropy_sources(); -std::vector<Engine*> get_engines(); -std::vector<Allocator*> get_allocators(); - -} +class Builtin_Modules : public Modules + { + public: + class Mutex_Factory* mutex_factory() const; + class Timer* timer() const; + + std::vector<class Allocator*> allocators() const; + std::vector<class EntropySource*> entropy_sources() const; + std::vector<class Engine*> engines() const; + }; } diff --git a/src/init_def.cpp b/src/init_def.cpp index 30f2228f5..6a5074665 100644 --- a/src/init_def.cpp +++ b/src/init_def.cpp @@ -7,9 +7,7 @@ #include <botan/libstate.h> #include <botan/modules.h> #include <botan/conf.h> -#include <botan/parsing.h> #include <botan/defalloc.h> -#include <botan/eng_def.h> #include <botan/fips140.h> #include <botan/x931_rng.h> #include <botan/def_char.h> @@ -34,90 +32,48 @@ LibraryInitializer::~LibraryInitializer() namespace Init { -namespace { - -/************************************************* -* Parse the options string * -*************************************************/ -std::map<std::string, std::string> parse_args(const std::string& arg_string) - { - std::map<std::string, std::string> arg_map; - std::vector<std::string> args = split_on(arg_string, ' '); - for(u32bit j = 0; j != args.size(); ++j) - { - if(args[j].find('=') == std::string::npos) - arg_map[args[j]] = ""; - else - { - std::vector<std::string> name_and_value = split_on(args[j], '='); - arg_map[name_and_value[0]] = name_and_value[1]; - } - } - - return arg_map; - } - -/************************************************* -* Check if an option is set in the argument * -*************************************************/ -bool arg_set(const std::map<std::string, std::string>& args, - const std::string& option) - { - return (args.find(option) != args.end()); - } - -} - /************************************************* * Library Initialization * *************************************************/ void initialize(const std::string& arg_string) { - std::map<std::string, std::string> args = parse_args(arg_string); + InitializerOptions args(arg_string); + Builtin_Modules modules; Mutex_Factory* mutex_factory = 0; - if(arg_set(args, "thread_safe")) + + if(args.thread_safe()) { - mutex_factory = Modules::get_mutex_factory(); + mutex_factory = modules.mutex_factory(); if(!mutex_factory) throw Exception("LibraryInitializer: thread safety impossible"); } - set_global_state(new Library_State(mutex_factory, - Modules::get_timer())); - - global_state().add_allocator(new Malloc_Allocator); - global_state().add_allocator(new Locking_Allocator); + set_global_state(new Library_State(mutex_factory)); - if(arg_set(args, "secure_memory")) - { - std::vector<Allocator*> allocators = Modules::get_allocators(); + global_state().set_timer(modules.timer()); - for(u32bit j = 0; j != allocators.size(); ++j) - global_state().add_allocator(allocators[j]); - } + std::vector<Allocator*> allocators = modules.allocators(); + for(u32bit j = 0; j != allocators.size(); ++j) + global_state().add_allocator(allocators[j]); - if(arg_set(args, "config") && args["config"] != "") - Config::load(args["config"], global_state()); + if(args.config_file() != "") + Config::load(args.config_file(), global_state()); - if(arg_set(args, "use_engines")) - { - std::vector<Engine*> engines = Modules::get_engines(); - for(u32bit j = 0; j != engines.size(); ++j) - global_state().add_engine(engines[j]); - } - global_state().add_engine(new Default_Engine); + std::vector<Engine*> engines = modules.engines(); + for(u32bit j = 0; j != engines.size(); ++j) + global_state().add_engine(engines[j]); global_state().set_transcoder(new Default_Charset_Transcoder); global_state().set_prng(new ANSI_X931_RNG); - std::vector<EntropySource*> sources = Modules::get_entropy_sources(); + std::vector<EntropySource*> sources = modules.entropy_sources(); for(u32bit j = 0; j != sources.size(); ++j) global_state().add_entropy_source(sources[j], true); const u32bit min_entropy = Config::get_u32bit("rng/min_entropy"); - if(min_entropy != 0 && !arg_set(args, "no_rng_seed")) + if(min_entropy != 0 && args.seed_rng()) { u32bit total_bits = 0; for(u32bit j = 0; j != 4; ++j) diff --git a/src/modules.cpp b/src/modules.cpp index ccbe65739..fdc4dac71 100644 --- a/src/modules.cpp +++ b/src/modules.cpp @@ -4,6 +4,9 @@ *************************************************/ #include <botan/modules.h> +#include <botan/libstate.h> +#include <botan/defalloc.h> +#include <botan/eng_def.h> #include <botan/es_file.h> #if defined(BOTAN_EXT_MUTEX_PTHREAD) @@ -70,12 +73,52 @@ namespace Botan { +/************************************************* +* Default No-Op Implementation * +*************************************************/ +class Mutex_Factory* Modules::mutex_factory() const + { + return 0; + } + +/************************************************* +* Default No-Op Implementation * +*************************************************/ +class Timer* Modules::timer() const + { + return 0; + } + +/************************************************* +* Default No-Op Implementation * +*************************************************/ +std::vector<Allocator*> Modules::allocators() const + { + return std::vector<Allocator*>(); + } + +/************************************************* +* Default No-Op Implementation * +*************************************************/ +std::vector<EntropySource*> Modules::entropy_sources() const + { + return std::vector<EntropySource*>(); + } + +/************************************************* +* Default No-Op Implementation * +*************************************************/ +std::vector<Engine*> Modules::engines() const + { + return std::vector<Engine*>(); + } + namespace Modules { /************************************************* -* Register a mutex type, if possible * +* Return a mutex factory, if available * *************************************************/ -Mutex_Factory* get_mutex_factory() +Mutex_Factory* Builtin_Modules::mutex_factory() const { #if defined(BOTAN_EXT_MUTEX_PTHREAD) return new Pthread_Mutex_Factory; @@ -83,15 +126,15 @@ Mutex_Factory* get_mutex_factory() return new Win32_Mutex_Factory; #elif defined(BOTAN_EXT_MUTEX_QT) return new Qt_Mutex_Factory; -#endif - +#else return 0; +#endif } /************************************************* * Find a high resolution timer, if possible * *************************************************/ -Timer* get_timer() +Timer* Builtin_Modules::timer() const { #if defined(BOTAN_EXT_TIMER_HARDWARE) return new Hardware_Timer; @@ -101,15 +144,15 @@ Timer* get_timer() return new Unix_Timer; #elif defined(BOTAN_EXT_TIMER_WIN32) return new Win32_Timer; -#endif - +#else return 0; +#endif } /************************************************* * Find any usable allocators * *************************************************/ -std::vector<Allocator*> get_allocators() +std::vector<Allocator*> Builtin_Modules::allocators() const { std::vector<Allocator*> allocators; @@ -117,13 +160,16 @@ std::vector<Allocator*> get_allocators() allocators.push_back(new MemoryMapping_Allocator); #endif + allocators.push_back(new Locking_Allocator); + allocators.push_back(new Malloc_Allocator); + return allocators; } /************************************************* * Register any usable entropy sources * *************************************************/ -std::vector<EntropySource*> get_entropy_sources() +std::vector<EntropySource*> Builtin_Modules::entropy_sources() const { std::vector<EntropySource*> sources; @@ -163,7 +209,7 @@ std::vector<EntropySource*> get_entropy_sources() /************************************************* * Find any usable engines * *************************************************/ -std::vector<Engine*> get_engines() +std::vector<Engine*> Builtin_Modules::engines() const { std::vector<Engine*> engines; @@ -179,6 +225,8 @@ std::vector<Engine*> get_engines() engines.push_back(new OpenSSL_Engine); #endif + engines.push_back(new Default_Engine); + return engines; } |