aboutsummaryrefslogtreecommitdiffstats
path: root/src/lib/tls/tls_text_policy.cpp
blob: 94fc07b04656e30e6206c5a99d2326ef6e27d00d (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
/*
* Text-Based TLS Policy
* (C) 2016,2017 Jack Lloyd
*     2017 Harry Reimann, Rohde & Schwarz Cybersecurity
*
* Botan is released under the Simplified BSD License (see license.txt)
*/

#include <botan/tls_policy.h>
#include <botan/exceptn.h>
#include <botan/internal/parsing.h>
#include <optional>
#include <sstream>

namespace Botan::TLS {

std::vector<std::string> Text_Policy::allowed_ciphers() const
   {
   return get_list("ciphers", Policy::allowed_ciphers());
   }

std::vector<std::string> Text_Policy::allowed_signature_hashes() const
   {
   return get_list("signature_hashes", Policy::allowed_signature_hashes());
   }

std::vector<std::string> Text_Policy::allowed_macs() const
   {
   return get_list("macs", Policy::allowed_macs());
   }

std::vector<std::string> Text_Policy::allowed_key_exchange_methods() const
   {
   return get_list("key_exchange_methods", Policy::allowed_key_exchange_methods());
   }

std::vector<std::string> Text_Policy::allowed_signature_methods() const
   {
   return get_list("signature_methods", Policy::allowed_signature_methods());
   }

bool Text_Policy::use_ecc_point_compression() const
   {
   return get_bool("use_ecc_point_compression", Policy::use_ecc_point_compression());
   }

bool Text_Policy::allow_tls12() const
   {
   return get_bool("allow_tls12", Policy::allow_tls12());
   }

bool Text_Policy::allow_dtls12() const
   {
   return get_bool("allow_dtls12", Policy::allow_dtls12());
   }

bool Text_Policy::allow_insecure_renegotiation() const
   {
   return get_bool("allow_insecure_renegotiation", Policy::allow_insecure_renegotiation());
   }

bool Text_Policy::include_time_in_hello_random() const
   {
   return get_bool("include_time_in_hello_random", Policy::include_time_in_hello_random());
   }

bool Text_Policy::require_client_certificate_authentication() const
   {
   return get_bool("require_client_certificate_authentication", Policy::require_client_certificate_authentication());
   }

bool Text_Policy::allow_client_initiated_renegotiation() const
   {
   return get_bool("allow_client_initiated_renegotiation", Policy::allow_client_initiated_renegotiation());
   }

bool Text_Policy::allow_server_initiated_renegotiation() const
   {
   return get_bool("allow_server_initiated_renegotiation", Policy::allow_server_initiated_renegotiation());
   }

bool Text_Policy::server_uses_own_ciphersuite_preferences() const
   {
   return get_bool("server_uses_own_ciphersuite_preferences", Policy::server_uses_own_ciphersuite_preferences());
   }

bool Text_Policy::negotiate_encrypt_then_mac() const
   {
   return get_bool("negotiate_encrypt_then_mac", Policy::negotiate_encrypt_then_mac());
   }

bool Text_Policy::support_cert_status_message() const
   {
   return get_bool("support_cert_status_message", Policy::support_cert_status_message());
   }

std::vector<Group_Params> Text_Policy::key_exchange_groups() const
   {
   std::string group_str = get_str("key_exchange_groups");

   if(group_str.empty())
      {
      // fall back to previously used name
      group_str = get_str("groups");
      }

   if(group_str.empty())
      {
      return Policy::key_exchange_groups();
      }

   return read_group_list(group_str);
   }


size_t Text_Policy::minimum_ecdh_group_size() const
   {
   return get_len("minimum_ecdh_group_size", Policy::minimum_ecdh_group_size());
   }

size_t Text_Policy::minimum_ecdsa_group_size() const
   {
   return get_len("minimum_ecdsa_group_size", Policy::minimum_ecdsa_group_size());
   }

size_t Text_Policy::minimum_dh_group_size() const
   {
   return get_len("minimum_dh_group_size", Policy::minimum_dh_group_size());
   }

size_t Text_Policy::minimum_rsa_bits() const
   {
   return get_len("minimum_rsa_bits", Policy::minimum_rsa_bits());
   }

size_t Text_Policy::minimum_signature_strength() const
   {
   return get_len("minimum_signature_strength", Policy::minimum_signature_strength());
   }

size_t Text_Policy::dtls_default_mtu() const
   {
   return get_len("dtls_default_mtu", Policy::dtls_default_mtu());
   }

size_t Text_Policy::dtls_initial_timeout() const
   {
   return get_len("dtls_initial_timeout", Policy::dtls_initial_timeout());
   }

size_t Text_Policy::dtls_maximum_timeout() const
   {
   return get_len("dtls_maximum_timeout", Policy::dtls_maximum_timeout());
   }

bool Text_Policy::require_cert_revocation_info() const
   {
   return get_bool("require_cert_revocation_info", Policy::require_cert_revocation_info());
   }

bool Text_Policy::hide_unknown_users() const
   {
   return get_bool("hide_unknown_users", Policy::hide_unknown_users());
   }

uint32_t Text_Policy::session_ticket_lifetime() const
   {
   return static_cast<uint32_t>(get_len("session_ticket_lifetime", Policy::session_ticket_lifetime()));
   }

std::vector<uint16_t> Text_Policy::srtp_profiles() const
   {
   std::vector<uint16_t> r;
   for(const std::string& p : get_list("srtp_profiles", std::vector<std::string>()))
      {
      r.push_back(to_uint16(p));
      }
   return r;
   }

bool Text_Policy::hash_hello_random() const
   {
   return get_bool("hash_hello_random", Policy::hash_hello_random());
   }

void Text_Policy::set(const std::string& k, const std::string& v)
   {
   m_kv[k] = v;
   }

Text_Policy::Text_Policy(const std::string& s)
   {
   std::istringstream iss(s);
   m_kv = read_cfg(iss);
   }

Text_Policy::Text_Policy(std::istream& in) : m_kv(read_cfg(in))
   {}

std::vector<std::string>
Text_Policy::get_list(const std::string& key,
                      const std::vector<std::string>& def) const
   {
   const std::string v = get_str(key);

   if(v.empty())
      {
      return def;
      }

   return split_on(v, ' ');
   }

std::vector<Group_Params>
Text_Policy::read_group_list(const std::string &group_str) const
{
   std::vector<Group_Params> groups;
   for(const std::string& group_name : split_on(group_str, ' '))
      {
      Group_Params group_id = group_param_from_string(group_name);

#if !defined(BOTAN_HAS_CURVE_25519)
      if(group_id == Group_Params::X25519)
         continue;
#endif

      if(group_id == Group_Params::NONE)
         {
         try
            {
            size_t consumed = 0;
            unsigned long ll_id = std::stoul(group_name, &consumed, 0);
            if(consumed != group_name.size())
               continue; // some other cruft

            const uint16_t id = static_cast<uint16_t>(ll_id);

            if(id != ll_id)
               continue; // integer too large

            group_id = static_cast<Group_Params>(id);
            }
         catch(...)
            {
            continue;
            }
         }

      if(group_id != Group_Params::NONE)
         groups.push_back(group_id);
      }

   return groups;
}

size_t Text_Policy::get_len(const std::string& key, size_t def) const
   {
   const std::string v = get_str(key);

   if(v.empty())
      {
      return def;
      }

   return to_u32bit(v);
   }

bool Text_Policy::get_bool(const std::string& key, bool def) const
   {
   const std::string v = get_str(key);

   if(v.empty())
      {
      return def;
      }

   if(v == "true" || v == "True")
      {
      return true;
      }
   else if(v == "false" || v == "False")
      {
      return false;
      }
   else
      {
      throw Decoding_Error("Invalid boolean '" + v + "'");
      }
   }

std::string Text_Policy::get_str(const std::string& key, const std::string& def) const
   {
   auto i = m_kv.find(key);
   if(i == m_kv.end())
      {
      return def;
      }

   return i->second;
   }

bool Text_Policy::set_value(const std::string& key, const std::string& val, bool overwrite)
   {
   auto i = m_kv.find(key);

   if(overwrite == false && i != m_kv.end())
      return false;

   m_kv.insert(i, std::make_pair(key, val));
   return true;
   }

}