1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310
|
/*************************************************
* Initialization Function Source File *
* (C) 1999-2005 The Botan Project *
*************************************************/
#include <botan/init.h>
#include <botan/allocate.h>
#include <botan/parsing.h>
#include <botan/conf.h>
#include <botan/look_add.h>
#include <botan/mutex.h>
#include <botan/rng.h>
#include <botan/x931_rng.h>
#include <botan/fips140.h>
#include <botan/es_file.h>
#include <map>
#if defined(BOTAN_EXT_MUTEX_PTHREAD)
#include <botan/mux_pthr.h>
#elif defined(BOTAN_EXT_MUTEX_WIN32)
#include <botan/mux_win.h>
#elif defined(BOTAN_EXT_MUTEX_QT)
#include <botan/mux_qt.h>
#endif
#if defined(BOTAN_EXT_ALLOC_MMAP)
#include <botan/mmap_mem.h>
#endif
#if defined(BOTAN_EXT_TIMER_HARDWARE)
#include <botan/tm_hard.h>
#elif defined(BOTAN_EXT_TIMER_POSIX)
#include <botan/tm_posix.h>
#elif defined(BOTAN_EXT_TIMER_UNIX)
#include <botan/tm_unix.h>
#elif defined(BOTAN_EXT_TIMER_WIN32)
#include <botan/tm_win32.h>
#endif
#if defined(BOTAN_EXT_ENGINE_AEP)
#include <botan/eng_aep.h>
#endif
#if defined(BOTAN_EXT_ENGINE_GNU_MP)
#include <botan/eng_gmp.h>
#endif
#if defined(BOTAN_EXT_ENGINE_OPENSSL)
#include <botan/eng_ossl.h>
#endif
#if defined(BOTAN_EXT_ENTROPY_SRC_AEP)
#include <botan/es_aep.h>
#endif
#if defined(BOTAN_EXT_ENTROPY_SRC_EGD)
#include <botan/es_egd.h>
#endif
#if defined(BOTAN_EXT_ENTROPY_SRC_UNIX)
#include <botan/es_unix.h>
#endif
#if defined(BOTAN_EXT_ENTROPY_SRC_BEOS)
#include <botan/es_beos.h>
#endif
#if defined(BOTAN_EXT_ENTROPY_SRC_CAPI)
#include <botan/es_capi.h>
#endif
#if defined(BOTAN_EXT_ENTROPY_SRC_WIN32)
#include <botan/es_win32.h>
#endif
#if defined(BOTAN_EXT_ENTROPY_SRC_FTW)
#include <botan/es_ftw.h>
#endif
namespace Botan {
/*************************************************
* Library Initialization *
*************************************************/
LibraryInitializer::LibraryInitializer(const std::string& arg_string)
{
Init::initialize(arg_string);
}
/*************************************************
* Library Shutdown *
*************************************************/
LibraryInitializer::~LibraryInitializer()
{
Init::deinitialize();
}
namespace Init {
namespace {
/*************************************************
* Register a mutex type, if possible *
*************************************************/
void set_mutex()
{
#if defined(BOTAN_EXT_MUTEX_PTHREAD)
set_mutex_type(new Pthread_Mutex);
#elif defined(BOTAN_EXT_MUTEX_WIN32)
set_mutex_type(new Win32_Mutex);
#elif defined(BOTAN_EXT_MUTEX_QT)
set_mutex_type(new Qt_Mutex);
#else
throw Exception("LibraryInitializer: thread safety impossible");
#endif
}
/*************************************************
* Register a high resolution timer, if possible *
*************************************************/
void set_timer()
{
#if defined(BOTAN_EXT_TIMER_HARDWARE)
set_timer_type(new Hardware_Timer);
#elif defined(BOTAN_EXT_TIMER_POSIX)
set_timer_type(new POSIX_Timer);
#elif defined(BOTAN_EXT_TIMER_UNIX)
set_timer_type(new Unix_Timer);
#elif defined(BOTAN_EXT_TIMER_WIN32)
set_timer_type(new Win32_Timer);
#endif
}
/*************************************************
* Register any usable entropy sources *
*************************************************/
void add_entropy_sources()
{
Global_RNG::add_es(new File_EntropySource);
#if defined(BOTAN_EXT_ENTROPY_SRC_AEP)
Global_RNG::add_es(new AEP_EntropySource);
#endif
#if defined(BOTAN_EXT_ENTROPY_SRC_EGD)
Global_RNG::add_es(new EGD_EntropySource);
#endif
#if defined(BOTAN_EXT_ENTROPY_SRC_CAPI)
Global_RNG::add_es(new Win32_CAPI_EntropySource);
#endif
#if defined(BOTAN_EXT_ENTROPY_SRC_WIN32)
Global_RNG::add_es(new Win32_EntropySource);
#endif
#if defined(BOTAN_EXT_ENTROPY_SRC_UNIX)
Global_RNG::add_es(new Unix_EntropySource);
#endif
#if defined(BOTAN_EXT_ENTROPY_SRC_BEOS)
Global_RNG::add_es(new BeOS_EntropySource);
#endif
#if defined(BOTAN_EXT_ENTROPY_SRC_FTW)
Global_RNG::add_es(new FTW_EntropySource);
#endif
}
/*************************************************
* Register a more secure allocator, if possible *
*************************************************/
void set_safe_allocator()
{
#if defined(BOTAN_EXT_ALLOC_MMAP)
add_allocator_type("mmap", new MemoryMapping_Allocator);
set_default_allocator("mmap");
#endif
}
/*************************************************
* Register any usable engines *
*************************************************/
void set_engines()
{
#if defined(BOTAN_EXT_ENGINE_AEP)
Botan::Engine_Core::add_engine(new Botan::AEP_Engine);
#endif
#if defined(BOTAN_EXT_ENGINE_GNU_MP)
Botan::Engine_Core::add_engine(new Botan::GMP_Engine);
#endif
#if defined(BOTAN_EXT_ENGINE_OPENSSL)
Botan::Engine_Core::add_engine(new Botan::OpenSSL_Engine);
#endif
}
/*************************************************
* 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);
if(arg_set(args, "thread_safe"))
set_mutex();
startup_conf();
startup_oids();
set_default_options();
startup_memory_subsystem();
init_lookup_tables();
if(arg_set(args, "secure_memory"))
set_safe_allocator();
set_timer();
if(!arg_set(args, "no_aliases")) add_default_aliases();
if(!arg_set(args, "no_oids")) add_default_oids();
if(arg_set(args, "config") && args["config"] != "")
Config::load(args["config"]);
startup_engines();
if(arg_set(args, "use_engines"))
set_engines();
init_rng_subsystem();
set_global_rngs(new ANSI_X931_RNG, new ANSI_X931_RNG);
add_entropy_sources();
if(!FIPS140::passes_self_tests())
{
deinitialize();
throw Self_Test_Failure("FIPS-140 startup tests");
}
const u32bit min_entropy = Config::get_u32bit("rng/min_entropy");
if(min_entropy != 0 && !arg_set(args, "no_rng_seed"))
{
u32bit total_bits = 0;
for(u32bit j = 0; j != 4; j++)
{
total_bits += Global_RNG::seed(true, min_entropy - total_bits);
if(total_bits >= min_entropy)
break;
}
if(total_bits < min_entropy)
throw PRNG_Unseeded("Unable to collect sufficient entropy");
}
startup_dl_cache();
}
/*************************************************
* Library Shutdown *
*************************************************/
void deinitialize()
{
shutdown_engines();
shutdown_rng_subsystem();
destroy_lookup_tables();
shutdown_dl_cache();
shutdown_conf();
shutdown_oids();
set_timer_type(0);
set_mutex_type(0);
shutdown_memory_subsystem();
}
}
}
|