aboutsummaryrefslogtreecommitdiffstats
path: root/src/init_opt.cpp
blob: f84ac83c29a02f53811e57695c40902c5e563d70 (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
/*************************************************
* Initialization Options Source File             *
* (C) 1999-2007 Jack Lloyd                       *
*************************************************/

#include <botan/init.h>
#include <botan/parsing.h>
#include <botan/stl_util.h>
#include <botan/exceptn.h>

namespace Botan {

namespace {

/*************************************************
* Check for an arbitrary boolean-valued option   *
*************************************************/
bool boolean_arg(const std::map<std::string, std::string>& args,
                 const std::string& key, bool not_found = false)
   {
   std::map<std::string, std::string>::const_iterator i = args.find(key);
   if(i == args.end())
      return not_found;

   std::string value = i->second;

   if(value == "1" || value == "true" || value == "yes" || value == "on")
      return true;
   if(value == "0" || value == "false" || value == "no" || value == "off")
      return false;
   if(value == "default")
      return not_found;

   throw Invalid_Argument("InitializerOptions: Bad argument for boolean " +
                          key + " of '" + value + "'");
   }

}

/*************************************************
* Check if thread safety was requested           *
*************************************************/
bool InitializerOptions::thread_safe() const
   {
   return boolean_arg(args, "thread_safe");
   }

/*************************************************
* Check if secure allocation was requested       *
*************************************************/
bool InitializerOptions::secure_memory() const
   {
   return boolean_arg(args, "secure_memory");
   }

/*************************************************
* Check if using engines was requested           *
*************************************************/
bool InitializerOptions::use_engines() const
   {
   return boolean_arg(args, "use_engines");
   }

/*************************************************
* Check if RNG seeding should be enabled         *
*************************************************/
bool InitializerOptions::seed_rng() const
   {
   return boolean_arg(args, "seed_rng", true);
   }

/*************************************************
* Check if FIPS mode was requested               *
*************************************************/
bool InitializerOptions::fips_mode() const
   {
   return boolean_arg(args, "fips140");
   }

/*************************************************
* Check if startup self tests were requested     *
*************************************************/
bool InitializerOptions::self_test() const
   {
   return boolean_arg(args, "selftest", true);
   }

/*************************************************
* Setup an InitializerOptions                    *
*************************************************/
InitializerOptions::InitializerOptions(const std::string& arg_string)
   {
   const std::vector<std::string> arg_list = split_on(arg_string, ' ');

   for(u32bit j = 0; j != arg_list.size(); ++j)
      {
      if(arg_list[j].size() == 0)
         continue;

      if(arg_list[j].find('=') == std::string::npos)
         args[arg_list[j]] = "true";
      else
         {
         std::vector<std::string> name_and_value = split_on(arg_list[j], '=');
         args[name_and_value[0]] = name_and_value[1];
         }
      }
   }

}