monotone

monotone Mtn Source Tree

Root/botan/conf.cpp

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

Archive Download this file

Branches

Tags

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