monotone

monotone Mtn Source Tree

Root/botan/config.cpp

1/*************************************************
2* Configuration Handling Source File *
3* (C) 1999-2006 The Botan Project *
4*************************************************/
5
6#include <botan/config.h>
7#include <botan/libstate.h>
8#include <botan/lookup.h>
9#include <botan/charset.h>
10#include <botan/parsing.h>
11#include <botan/stl_util.h>
12#include <botan/mutex.h>
13#include <string>
14
15namespace Botan {
16
17/*************************************************
18* Get the global configuration object *
19*************************************************/
20Config& global_config()
21 {
22 return global_state().config();
23 }
24
25/*************************************************
26* Get a configuration value *
27*************************************************/
28std::string Config::get(const std::string& section,
29 const std::string& key) const
30 {
31 Named_Mutex_Holder lock("config");
32
33 return search_map<std::string, std::string>(settings,
34 section + "/" + key, "");
35 }
36
37/*************************************************
38* See if a particular option has been set *
39*************************************************/
40bool Config::is_set(const std::string& section,
41 const std::string& key) const
42 {
43 Named_Mutex_Holder lock("config");
44
45 return search_map(settings, section + "/" + key, false, true);
46 }
47
48/*************************************************
49* Set a configuration value *
50*************************************************/
51void Config::set(const std::string& section, const std::string& key,
52 const std::string& value, bool overwrite)
53 {
54 Named_Mutex_Holder lock("config");
55
56 std::string full_key = section + "/" + key;
57
58 std::map<std::string, std::string>::const_iterator i =
59 settings.find(full_key);
60
61 if(overwrite || i == settings.end() || i->second == "")
62 settings[full_key] = value;
63 }
64
65/*************************************************
66* Add an alias *
67*************************************************/
68void Config::add_alias(const std::string& key, const std::string& value)
69 {
70 set("alias", key, value);
71 }
72
73/*************************************************
74* Dereference an alias to a fixed name *
75*************************************************/
76std::string Config::deref_alias(const std::string& key) const
77 {
78 std::string result = key;
79 while(is_set("alias", result))
80 result = get("alias", result);
81 return result;
82 }
83
84/*************************************************
85* Set/Add an option *
86*************************************************/
87void Config::set_option(const std::string key, const std::string& value)
88 {
89 set("conf", key, value);
90 }
91
92/*************************************************
93* Get an option value *
94*************************************************/
95std::string Config::option(const std::string& key) const
96 {
97 return get("conf", key);
98 }
99
100/*************************************************
101* Get the config setting as a list of strings *
102*************************************************/
103std::vector<std::string> Config::option_as_list(const std::string& key) const
104 {
105 return split_on(option(key), ':');
106 }
107
108/*************************************************
109* Get the config setting as a u32bit *
110*************************************************/
111u32bit Config::option_as_u32bit(const std::string& key) const
112 {
113 return parse_expr(option(key));
114 }
115
116/*************************************************
117* Get the config setting as a time *
118*************************************************/
119u32bit Config::option_as_time(const std::string& key) const
120 {
121 const std::string timespec = option(key);
122 if(timespec == "")
123 return 0;
124
125 const char suffix = timespec[timespec.size()-1];
126 std::string value = timespec.substr(0, timespec.size()-1);
127
128 u32bit scale = 1;
129
130 if(Charset::is_digit(suffix))
131 value += suffix;
132 else if(suffix == 's')
133 scale = 1;
134 else if(suffix == 'm')
135 scale = 60;
136 else if(suffix == 'h')
137 scale = 60 * 60;
138 else if(suffix == 'd')
139 scale = 24 * 60 * 60;
140 else if(suffix == 'y')
141 scale = 365 * 24 * 60 * 60;
142 else
143 throw Decoding_Error(
144 "Config::option_as_time: Unknown time value " + value
145 );
146
147 return scale * to_u32bit(value);
148 }
149
150/*************************************************
151* Get the config setting as a boolean *
152*************************************************/
153bool Config::option_as_bool(const std::string& key) const
154 {
155 const std::string value = option(key);
156 if(value == "0" || value == "false")
157 return false;
158 if(value == "1" || value == "true")
159 return true;
160
161 throw Decoding_Error(
162 "Config::option_as_bool: Unknown boolean value " + value
163 );
164 }
165
166/*************************************************
167* Choose the signature format for a PK algorithm *
168*************************************************/
169void Config::choose_sig_format(const std::string& algo_name,
170 std::string& padding,
171 Signature_Format& format)
172 {
173 if(algo_name == "RSA")
174 {
175 std::string hash = global_state().config().option("x509/ca/rsa_hash");
176
177 if(hash == "")
178 throw Invalid_State("No value set for x509/ca/rsa_hash");
179
180 hash = global_state().config().deref_alias(hash);
181
182 padding = "EMSA3(" + hash + ")";
183 format = IEEE_1363;
184 }
185 else if(algo_name == "DSA")
186 {
187 std::string hash = global_state().config().deref_alias("SHA-1");
188 padding = "EMSA1(" + hash + ")";
189 format = DER_SEQUENCE;
190 }
191 else
192 throw Invalid_Argument("Unknown X.509 signing key type: " + algo_name);
193 }
194
195/*************************************************
196* Dereference an alias *
197*************************************************/
198std::string deref_alias(const std::string& name)
199 {
200 return global_config().deref_alias(name);
201 }
202
203}

Archive Download this file

Branches

Tags

Quick Links:     www.monotone.ca    -     Downloads    -     Documentation    -     Wiki    -     Code Forge    -     Build Status