aboutsummaryrefslogtreecommitdiffstats
path: root/src/libstate.cpp
blob: 92dfd2913617be4d29e598435c98670913b891ae (plain)
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
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
/*************************************************
* Library Internal/Global State Source File      *
* (C) 1999-2006 The Botan Project                *
*************************************************/

#include <botan/libstate.h>
#include <botan/engine.h>
#include <botan/x509stat.h>
#include <botan/stl_util.h>
#include <botan/mutex.h>
#include <botan/timers.h>
#include <botan/charset.h>

namespace Botan {

/*************************************************
* Botan's global state                           *
*************************************************/
namespace {

Library_State* global_lib_state = 0;

}

Library_State& global_state()
   {
   if(!global_lib_state)
      {
      abort();
      throw Invalid_State("Library was not initialized correctly");
      }
   return (*global_lib_state);
   }

void set_global_state(Library_State* new_state)
   {
   delete swap_global_state(new_state);
   }

Library_State* swap_global_state(Library_State* new_state)
   {
   Library_State* old_state = global_lib_state;
   global_lib_state = new_state;
   return old_state;
   }

/*************************************************
* Increment the Engine iterator                  *
*************************************************/
Engine* Library_State::Engine_Iterator::next()
   {
   return lib.get_engine_n(n++);
   }

/*************************************************
* Get a new mutex object                         *
*************************************************/
Mutex* Library_State::get_mutex() const
   {
   return mutex_factory->make();
   }

/*************************************************
* Get a persistent named mutex object            *
*************************************************/
Mutex* Library_State::get_named_mutex(const std::string& name)
   {
   Mutex* mux = search_map<std::string, Mutex*>(locks, name, 0);
   if(mux)
      return mux;
   return (locks[name] = get_mutex());
   }

/*************************************************
* Get an allocator by its name                   *
*************************************************/
Allocator* Library_State::get_allocator(const std::string& type) const
   {
   Named_Mutex_Holder lock("allocator");

   if(type != "")
      return search_map<std::string, Allocator*>(alloc_factory, type, 0);

   if(!cached_default_allocator)
      {
      const std::string key_name = "conf/base/default_allocator";

      Named_Mutex_Holder lock("settings");
      std::string chosen = search_map(settings, key_name);

      if(chosen == "")
         chosen = "malloc";

      cached_default_allocator =
         search_map<std::string, Allocator*>(alloc_factory, chosen, 0);
      }

   return cached_default_allocator;
   }

/*************************************************
* Create a new name to object mapping            *
*************************************************/
void Library_State::add_allocator(Allocator* allocator,
                                  bool set_as_default)
   {
   Named_Mutex_Holder lock("allocator");

   allocator->init();

   const std::string type = allocator->type();
   if(alloc_factory[type])
      delete alloc_factory[type];

   alloc_factory[type] = allocator;

   if(set_as_default)
      set_option("conf", "base/default_allocator", type);
   }

/*************************************************
* Set the high resolution clock implementation   *
*************************************************/
void Library_State::set_timer(Timer* new_timer)
   {
   if(new_timer)
      {
      delete timer;
      timer = new_timer;
      }
   }

/*************************************************
* Read a high resolution clock                   *
*************************************************/
u64bit Library_State::system_clock() const
   {
   return (timer) ? timer->clock() : 0;
   }

/*************************************************
* Set the global PRNG                            *
*************************************************/
void Library_State::set_prng(RandomNumberGenerator* new_rng)
   {
   Named_Mutex_Holder lock("rng");

   delete rng;
   rng = new_rng;
   }

/*************************************************
* Get bytes from the global PRNG                 *
*************************************************/
void Library_State::randomize(byte out[], u32bit length)
   {
   Named_Mutex_Holder lock("rng");

   rng->randomize(out, length);
   }

/*************************************************
* Add a new entropy source to use                *
*************************************************/
void Library_State::add_entropy_source(EntropySource* src, bool last_in_list)
   {
   Named_Mutex_Holder lock("rng");

   if(last_in_list)
      entropy_sources.push_back(src);
   else
      entropy_sources.insert(entropy_sources.begin(), src);
   }

/*************************************************
* Add some bytes of entropy to the global PRNG   *
*************************************************/
void Library_State::add_entropy(const byte in[], u32bit length)
   {
   Named_Mutex_Holder lock("rng");

   rng->add_entropy(in, length);
   }

/*************************************************
* Add some bytes of entropy to the global PRNG   *
*************************************************/
void Library_State::add_entropy(EntropySource& source, bool slow_poll)
   {
   Named_Mutex_Holder lock("rng");

   rng->add_entropy(source, slow_poll);
   }

/*************************************************
* Gather entropy for our PRNG object             *
*************************************************/
u32bit Library_State::seed_prng(bool slow_poll, u32bit bits_to_get)
   {
   Named_Mutex_Holder lock("rng");

   u32bit bits = 0;
   for(u32bit j = 0; j != entropy_sources.size(); ++j)
      {
      bits += rng->add_entropy(*(entropy_sources[j]), slow_poll);

      if(bits_to_get && bits >= bits_to_get)
         return bits;
      }

   return bits;
   }

/*************************************************
* Set a named option                             *
*************************************************/
void Library_State::set_option(const std::string& section,
                               const std::string& name,
                               const std::string& value,
                               bool overwrite)
   {
   Named_Mutex_Holder lock("settings");

   std::map<std::string, std::string>::const_iterator i = settings.find(name);

   if(overwrite || i == settings.end() || i->second == "")
      {
      const std::string full_name = section + "/" + name;
      settings[full_name] = value;

      if(full_name == "base/default_allocator")
         cached_default_allocator = 0;
      }
   }

/*************************************************
* Get the value of the named option              *
*************************************************/
std::string Library_State::get_option(const std::string& section,
                                      const std::string& name) const
   {
   Named_Mutex_Holder lock("settings");

   return search_map<std::string, std::string>(settings,
                                               section + "/" + name, "");
   }

/*************************************************
* See if a particular option has been set        *
*************************************************/
bool Library_State::option_set(const std::string& section,
                               const std::string& name) const
   {
   Named_Mutex_Holder lock("settings");

   return search_map(settings, section + "/" + name, false, true);
   }

/*************************************************
* Get an engine out of the list                  *
*************************************************/
Engine* Library_State::get_engine_n(u32bit n) const
   {
   Named_Mutex_Holder lock("engine");

   if(n >= engines.size())
      return 0;
   return engines[n];
   }

/*************************************************
* Add a new engine to the list                   *
*************************************************/
void Library_State::add_engine(Engine* engine)
   {
   Named_Mutex_Holder lock("engine");
   engines.push_back(engine);
   }

/*************************************************
* Set the character set transcoder object        *
*************************************************/
void Library_State::set_transcoder(class Charset_Transcoder* transcoder)
   {
   if(this->transcoder)
      delete this->transcoder;
   this->transcoder = transcoder;
   }

/*************************************************
* Transcode a string from one charset to another *
*************************************************/
std::string Library_State::transcode(const std::string str,
                                     Character_Set to,
                                     Character_Set from) const
   {
   if(!transcoder)
      throw Invalid_State("Library_State::transcode: No transcoder set");

   return transcoder->transcode(str, to, from);
   }

/*************************************************
* Set the X509 global state class                *
*************************************************/
void Library_State::set_x509_state(X509_GlobalState* new_x509_state_obj)
   {
   delete x509_state_obj;
   x509_state_obj = new_x509_state_obj;
   }

/*************************************************
* Set the X509 global state class                *
*************************************************/
X509_GlobalState& Library_State::x509_state()
   {
   if(!x509_state_obj)
      x509_state_obj = new X509_GlobalState();

   return (*x509_state_obj);
   }

/*************************************************
* Library_State Constructor                      *
*************************************************/
Library_State::Library_State(Mutex_Factory* mutex_factory)
   {
   if(!mutex_factory)
      mutex_factory = new Mutex_Factory;

   this->mutex_factory = mutex_factory;
   this->timer = new Timer();
   this->transcoder = 0;

   locks["settings"] = get_mutex();
   locks["allocator"] = get_mutex();
   locks["rng"] = get_mutex();
   locks["engine"] = get_mutex();
   rng = 0;
   cached_default_allocator = 0;
   x509_state_obj = 0;
   }

/*************************************************
* Library_State Destructor                       *
*************************************************/
Library_State::~Library_State()
   {
   delete x509_state_obj;
   delete transcoder;
   for(u32bit j = 0; j != entropy_sources.size(); ++j)
      delete entropy_sources[j];

   delete rng;
   delete timer;

   for(u32bit j = 0; j != engines.size(); ++j)
      delete engines[j];

   cached_default_allocator = 0;
   for(std::map<std::string, Allocator*>::iterator j = alloc_factory.begin();
       j != alloc_factory.end(); ++j)
      {
      j->second->destroy();
      delete j->second;
      }

   for(std::map<std::string, Mutex*>::iterator j = locks.begin();
       j != locks.end(); ++j)
      delete j->second;

   delete mutex_factory;
   }

}