Coverage Report

Created: 2020-06-30 13:58

/src/botan/src/lib/tls/tls_policy.cpp
Line
Count
Source (jump to first uncovered line)
1
/*
2
* Policies for TLS
3
* (C) 2004-2010,2012,2015,2016 Jack Lloyd
4
*     2016 Christian Mainka
5
*     2017 Harry Reimann, Rohde & Schwarz Cybersecurity
6
*
7
* Botan is released under the Simplified BSD License (see license.txt)
8
*/
9
10
#include <botan/tls_policy.h>
11
#include <botan/tls_ciphersuite.h>
12
#include <botan/tls_algos.h>
13
#include <botan/tls_exceptn.h>
14
#include <botan/internal/stl_util.h>
15
#include <botan/pk_keys.h>
16
#include <sstream>
17
18
namespace Botan {
19
20
namespace TLS {
21
22
std::vector<Signature_Scheme> Policy::allowed_signature_schemes() const
23
5.88k
   {
24
5.88k
   std::vector<Signature_Scheme> schemes;
25
5.88k
26
5.88k
   for(Signature_Scheme scheme : all_signature_schemes())
27
88.3k
      {
28
88.3k
      if(signature_scheme_is_known(scheme) == false)
29
0
         continue;
30
88.3k
      const bool sig_allowed = allowed_signature_method(signature_algorithm_of_scheme(scheme));
31
88.3k
      const bool hash_allowed = allowed_signature_hash(hash_function_of_scheme(scheme));
32
88.3k
33
88.3k
      if(sig_allowed && hash_allowed)
34
52.9k
         {
35
52.9k
         schemes.push_back(scheme);
36
52.9k
         }
37
88.3k
      }
38
5.88k
39
5.88k
   return schemes;
40
5.88k
   }
41
42
std::vector<std::string> Policy::allowed_ciphers() const
43
0
   {
44
0
   return {
45
0
      //"AES-256/OCB(12)",
46
0
      //"AES-128/OCB(12)",
47
0
      "ChaCha20Poly1305",
48
0
      "AES-256/GCM",
49
0
      "AES-128/GCM",
50
0
      //"AES-256/CCM",
51
0
      //"AES-128/CCM",
52
0
      //"AES-256/CCM(8)",
53
0
      //"AES-128/CCM(8)",
54
0
      //"Camellia-256/GCM",
55
0
      //"Camellia-128/GCM",
56
0
      //"ARIA-256/GCM",
57
0
      //"ARIA-128/GCM",
58
0
      //"AES-256",
59
0
      //"AES-128",
60
0
      //"Camellia-256",
61
0
      //"Camellia-128",
62
0
      //"SEED",
63
0
      //"3DES",
64
0
      };
65
0
   }
66
67
std::vector<std::string> Policy::allowed_signature_hashes() const
68
90.2k
   {
69
90.2k
   return {
70
90.2k
      "SHA-512",
71
90.2k
      "SHA-384",
72
90.2k
      "SHA-256",
73
90.2k
      //"SHA-1",
74
90.2k
      };
75
90.2k
   }
76
77
std::vector<std::string> Policy::allowed_macs() const
78
0
   {
79
0
   /*
80
0
   SHA-256 is preferred because the Lucky13 countermeasure works
81
0
   somewhat better for SHA-256 vs SHA-384:
82
0
   https://github.com/randombit/botan/pull/675
83
0
   */
84
0
   return {
85
0
      "AEAD",
86
0
      "SHA-256",
87
0
      "SHA-384",
88
0
      "SHA-1",
89
0
      };
90
0
   }
91
92
std::vector<std::string> Policy::allowed_key_exchange_methods() const
93
0
   {
94
0
   return {
95
0
      //"SRP_SHA",
96
0
      //"ECDHE_PSK",
97
0
      //"DHE_PSK",
98
0
      //"PSK",
99
0
      "CECPQ1",
100
0
      "ECDH",
101
0
      "DH",
102
0
      //"RSA",
103
0
      };
104
0
   }
105
106
std::vector<std::string> Policy::allowed_signature_methods() const
107
88.6k
   {
108
88.6k
   return {
109
88.6k
      "ECDSA",
110
88.6k
      "RSA",
111
88.6k
      //"DSA",
112
88.6k
      //"IMPLICIT",
113
88.6k
      //"ANONYMOUS" (anon)
114
88.6k
      };
115
88.6k
   }
116
117
bool Policy::allowed_signature_method(const std::string& sig_method) const
118
88.6k
   {
119
88.6k
   return value_exists(allowed_signature_methods(), sig_method);
120
88.6k
   }
121
122
bool Policy::allowed_signature_hash(const std::string& sig_hash) const
123
90.2k
   {
124
90.2k
   return value_exists(allowed_signature_hashes(), sig_hash);
125
90.2k
   }
126
127
bool Policy::use_ecc_point_compression() const
128
6.47k
   {
129
6.47k
   return false;
130
6.47k
   }
131
132
Group_Params Policy::choose_key_exchange_group(const std::vector<Group_Params>& peer_groups) const
133
62.7k
   {
134
62.7k
   if(peer_groups.empty())
135
4.43k
      return Group_Params::NONE;
136
58.3k
137
58.3k
   const std::vector<Group_Params> our_groups = key_exchange_groups();
138
58.3k
139
58.3k
   for(auto g : our_groups)
140
319k
      {
141
319k
      if(value_exists(peer_groups, g))
142
56.7k
         return g;
143
319k
      }
144
58.3k
145
58.3k
   return Group_Params::NONE;
146
58.3k
   }
147
148
Group_Params Policy::default_dh_group() const
149
3.87k
   {
150
3.87k
   /*
151
3.87k
   * Return the first listed or just default to 2048
152
3.87k
   */
153
3.87k
   for(auto g : key_exchange_groups())
154
31.0k
      {
155
31.0k
      if(group_param_is_dh(g))
156
3.87k
         return g;
157
31.0k
      }
158
3.87k
159
3.87k
   return Group_Params::FFDHE_2048;
160
3.87k
   }
161
162
std::vector<Group_Params> Policy::key_exchange_groups() const
163
67.9k
   {
164
67.9k
   // Default list is ordered by performance
165
67.9k
   return {
166
67.9k
167
67.9k
#if defined(BOTAN_HAS_CURVE_25519)
168
67.9k
      Group_Params::X25519,
169
67.9k
#endif
170
67.9k
171
67.9k
      Group_Params::SECP256R1,
172
67.9k
      Group_Params::BRAINPOOL256R1,
173
67.9k
      Group_Params::SECP384R1,
174
67.9k
      Group_Params::BRAINPOOL384R1,
175
67.9k
      Group_Params::SECP521R1,
176
67.9k
      Group_Params::BRAINPOOL512R1,
177
67.9k
178
67.9k
      Group_Params::FFDHE_2048,
179
67.9k
      Group_Params::FFDHE_3072,
180
67.9k
      Group_Params::FFDHE_4096,
181
67.9k
      Group_Params::FFDHE_6144,
182
67.9k
      Group_Params::FFDHE_8192,
183
67.9k
      };
184
67.9k
   }
185
186
size_t Policy::minimum_dh_group_size() const
187
92
   {
188
92
   return 2048;
189
92
   }
190
191
size_t Policy::minimum_ecdsa_group_size() const
192
331
   {
193
331
   // Here we are at the mercy of whatever the CA signed, but most certs should be 256 bit by now
194
331
   return 256;
195
331
   }
196
197
size_t Policy::minimum_ecdh_group_size() const
198
188
   {
199
188
   // x25519 is smallest curve currently supported for TLS key exchange
200
188
   return 255;
201
188
   }
202
203
size_t Policy::minimum_signature_strength() const
204
687
   {
205
687
   return 110;
206
687
   }
207
208
bool Policy::require_cert_revocation_info() const
209
687
   {
210
687
   return true;
211
687
   }
212
213
size_t Policy::minimum_rsa_bits() const
214
38
   {
215
38
   /* Default assumption is all end-entity certificates should
216
38
      be at least 2048 bits these days.
217
38
218
38
      If you are connecting to arbitrary servers on the Internet
219
38
      (ie as a web browser or SMTP client) you'll probably have to reduce this
220
38
      to 1024 bits, or perhaps even lower.
221
38
   */
222
38
   return 2048;
223
38
   }
224
225
size_t Policy::minimum_dsa_group_size() const
226
0
   {
227
0
   // FIPS 186-3
228
0
   return 2048;
229
0
   }
230
231
void Policy::check_peer_key_acceptable(const Public_Key& public_key) const
232
649
   {
233
649
   const std::string algo_name = public_key.algo_name();
234
649
235
649
   const size_t keylength = public_key.key_length();
236
649
   size_t expected_keylength = 0;
237
649
238
649
   if(algo_name == "RSA")
239
38
      {
240
38
      expected_keylength = minimum_rsa_bits();
241
38
      }
242
611
   else if(algo_name == "DH")
243
92
      {
244
92
      expected_keylength = minimum_dh_group_size();
245
92
      }
246
519
   else if(algo_name == "DSA")
247
0
      {
248
0
      expected_keylength = minimum_dsa_group_size();
249
0
      }
250
519
   else if(algo_name == "ECDH" || algo_name == "Curve25519")
251
188
      {
252
188
      expected_keylength = minimum_ecdh_group_size();
253
188
      }
254
331
   else if(algo_name == "ECDSA")
255
331
      {
256
331
      expected_keylength = minimum_ecdsa_group_size();
257
331
      }
258
649
   // else some other algo, so leave expected_keylength as zero and the check is a no-op
259
649
260
649
   if(keylength < expected_keylength)
261
94
      throw TLS_Exception(Alert::INSUFFICIENT_SECURITY,
262
94
                          "Peer sent " +
263
94
                           std::to_string(keylength) + " bit " + algo_name + " key"
264
94
                           ", policy requires at least " +
265
94
                           std::to_string(expected_keylength));
266
649
   }
267
268
uint32_t Policy::session_ticket_lifetime() const
269
25.0k
   {
270
25.0k
   return 86400; // ~1 day
271
25.0k
   }
272
273
bool Policy::send_fallback_scsv(Protocol_Version version) const
274
5.71k
   {
275
5.71k
   return version != latest_supported_version(version.is_datagram_protocol());
276
5.71k
   }
277
278
bool Policy::acceptable_protocol_version(Protocol_Version version) const
279
53.6k
   {
280
53.6k
   if(version == Protocol_Version::TLS_V12 && allow_tls12())
281
40.5k
      return true;
282
13.1k
283
13.1k
   if(version == Protocol_Version::DTLS_V12 && allow_dtls12())
284
13.0k
      return true;
285
104
286
104
#if defined(BOTAN_HAS_TLS_V10)
287
104
288
104
   if(version == Protocol_Version::TLS_V11 && allow_tls11())
289
0
      return true;
290
104
   if(version == Protocol_Version::TLS_V10 && allow_tls10())
291
0
      return true;
292
104
   if(version == Protocol_Version::DTLS_V10 && allow_dtls10())
293
0
      return true;
294
104
295
104
#endif
296
104
297
104
   return false;
298
104
   }
299
300
Protocol_Version Policy::latest_supported_version(bool datagram) const
301
37.2k
   {
302
37.2k
   if(datagram)
303
6.69k
      {
304
6.69k
      if(acceptable_protocol_version(Protocol_Version::DTLS_V12))
305
6.69k
         return Protocol_Version::DTLS_V12;
306
0
#if defined(BOTAN_HAS_TLS_V10)
307
0
      if(acceptable_protocol_version(Protocol_Version::DTLS_V10))
308
0
         return Protocol_Version::DTLS_V10;
309
0
#endif
310
0
      throw Invalid_State("Policy forbids all available DTLS version");
311
0
      }
312
30.5k
   else
313
30.5k
      {
314
30.5k
      if(acceptable_protocol_version(Protocol_Version::TLS_V12))
315
30.5k
         return Protocol_Version::TLS_V12;
316
0
#if defined(BOTAN_HAS_TLS_V10)
317
0
      if(acceptable_protocol_version(Protocol_Version::TLS_V11))
318
0
         return Protocol_Version::TLS_V11;
319
0
      if(acceptable_protocol_version(Protocol_Version::TLS_V10))
320
0
         return Protocol_Version::TLS_V10;
321
0
#endif
322
0
      throw Invalid_State("Policy forbids all available TLS version");
323
0
      }
324
37.2k
   }
325
326
bool Policy::acceptable_ciphersuite(const Ciphersuite& ciphersuite) const
327
0
   {
328
0
   return value_exists(allowed_ciphers(), ciphersuite.cipher_algo()) &&
329
0
          value_exists(allowed_macs(), ciphersuite.mac_algo());
330
0
   }
331
332
0
bool Policy::allow_client_initiated_renegotiation() const { return false; }
333
0
bool Policy::allow_server_initiated_renegotiation() const { return false; }
334
32.1k
bool Policy::allow_insecure_renegotiation() const { return false; }
335
5.72k
bool Policy::allow_tls10()  const { return false; }
336
5.72k
bool Policy::allow_tls11()  const { return false; }
337
46.2k
bool Policy::allow_tls12()  const { return true; }
338
3
bool Policy::allow_dtls10() const { return false; }
339
13.0k
bool Policy::allow_dtls12() const { return true; }
340
54.6k
bool Policy::include_time_in_hello_random() const { return true; }
341
0
bool Policy::hide_unknown_users() const { return false; }
342
24.7k
bool Policy::server_uses_own_ciphersuite_preferences() const { return true; }
343
5.97k
bool Policy::negotiate_encrypt_then_mac() const { return true; }
344
6.68k
bool Policy::support_cert_status_message() const { return true; }
345
0
bool Policy::allow_resumption_for_renegotiation() const { return true; }
346
0
bool Policy::only_resume_with_exact_version() const { return true; }
347
24.3k
bool Policy::require_client_certificate_authentication() const { return false; }
348
24.3k
bool Policy::request_client_certificate_authentication() const { return require_client_certificate_authentication(); }
349
0
bool Policy::abort_connection_on_undesired_renegotiation() const { return false; }
350
676
bool Policy::allow_dtls_epoch0_restart() const { return false; }
351
352
3.13k
size_t Policy::maximum_certificate_chain_size() const { return 0; }
353
354
// 1 second initial timeout, 60 second max - see RFC 6347 sec 4.2.4.1
355
611
size_t Policy::dtls_initial_timeout() const { return 1*1000; }
356
611
size_t Policy::dtls_maximum_timeout() const { return 60*1000; }
357
358
size_t Policy::dtls_default_mtu() const
359
611
   {
360
611
   // default MTU is IPv6 min MTU minus UDP/IP headers
361
611
   return 1280 - 40 - 8;
362
611
   }
363
364
std::vector<uint16_t> Policy::srtp_profiles() const
365
0
   {
366
0
   return std::vector<uint16_t>();
367
0
   }
368
369
namespace {
370
371
class Ciphersuite_Preference_Ordering final
372
   {
373
   public:
374
      Ciphersuite_Preference_Ordering(const std::vector<std::string>& ciphers,
375
                                      const std::vector<std::string>& macs,
376
                                      const std::vector<std::string>& kex,
377
                                      const std::vector<std::string>& sigs) :
378
0
         m_ciphers(ciphers), m_macs(macs), m_kex(kex), m_sigs(sigs) {}
379
380
      bool operator()(const Ciphersuite& a, const Ciphersuite& b) const
381
0
         {
382
0
         if(a.kex_method() != b.kex_method())
383
0
            {
384
0
            for(size_t i = 0; i != m_kex.size(); ++i)
385
0
               {
386
0
               if(a.kex_algo() == m_kex[i])
387
0
                  return true;
388
0
               if(b.kex_algo() == m_kex[i])
389
0
                  return false;
390
0
               }
391
0
            }
392
0
393
0
         if(a.cipher_algo() != b.cipher_algo())
394
0
            {
395
0
            for(size_t i = 0; i != m_ciphers.size(); ++i)
396
0
               {
397
0
               if(a.cipher_algo() == m_ciphers[i])
398
0
                  return true;
399
0
               if(b.cipher_algo() == m_ciphers[i])
400
0
                  return false;
401
0
               }
402
0
            }
403
0
404
0
         if(a.cipher_keylen() != b.cipher_keylen())
405
0
            {
406
0
            if(a.cipher_keylen() < b.cipher_keylen())
407
0
               return false;
408
0
            if(a.cipher_keylen() > b.cipher_keylen())
409
0
               return true;
410
0
            }
411
0
412
0
         if(a.auth_method() != b.auth_method())
413
0
            {
414
0
            for(size_t i = 0; i != m_sigs.size(); ++i)
415
0
               {
416
0
               if(a.sig_algo() == m_sigs[i])
417
0
                  return true;
418
0
               if(b.sig_algo() == m_sigs[i])
419
0
                  return false;
420
0
               }
421
0
            }
422
0
423
0
         if(a.mac_algo() != b.mac_algo())
424
0
            {
425
0
            for(size_t i = 0; i != m_macs.size(); ++i)
426
0
               {
427
0
               if(a.mac_algo() == m_macs[i])
428
0
                  return true;
429
0
               if(b.mac_algo() == m_macs[i])
430
0
                  return false;
431
0
               }
432
0
            }
433
0
434
0
         return false; // equal (?!?)
435
0
         }
436
   private:
437
      std::vector<std::string> m_ciphers, m_macs, m_kex, m_sigs;
438
   };
439
440
}
441
442
std::vector<uint16_t> Policy::ciphersuite_list(Protocol_Version version,
443
                                               bool have_srp) const
444
0
   {
445
0
   const std::vector<std::string> ciphers = allowed_ciphers();
446
0
   const std::vector<std::string> macs = allowed_macs();
447
0
   const std::vector<std::string> kex = allowed_key_exchange_methods();
448
0
   const std::vector<std::string> sigs = allowed_signature_methods();
449
0
450
0
   std::vector<Ciphersuite> ciphersuites;
451
0
452
0
   for(auto&& suite : Ciphersuite::all_known_ciphersuites())
453
0
      {
454
0
      // Can we use it?
455
0
      if(!suite.valid())
456
0
         continue;
457
0
458
0
      // Can we use it in this version?
459
0
      if(!suite.usable_in_version(version))
460
0
         continue;
461
0
462
0
      // Is it acceptable to the policy?
463
0
      if(!this->acceptable_ciphersuite(suite))
464
0
         continue;
465
0
466
0
      // Are we doing SRP?
467
0
      if(!have_srp && suite.kex_method() == Kex_Algo::SRP_SHA)
468
0
         continue;
469
0
470
0
      if(!value_exists(kex, suite.kex_algo()))
471
0
         continue; // unsupported key exchange
472
0
473
0
      if(!value_exists(ciphers, suite.cipher_algo()))
474
0
         continue; // unsupported cipher
475
0
476
0
      if(!value_exists(macs, suite.mac_algo()))
477
0
         continue; // unsupported MAC algo
478
0
479
0
      if(!value_exists(sigs, suite.sig_algo()))
480
0
         {
481
0
         // allow if it's an empty sig algo and we want to use PSK
482
0
         if(suite.auth_method() != Auth_Method::IMPLICIT || !suite.psk_ciphersuite())
483
0
            continue;
484
0
         }
485
0
486
0
      /*
487
0
      CECPQ1 always uses x25519 for ECDH, so treat the applications
488
0
      removal of x25519 from the ECC curve list as equivalent to
489
0
      saying they do not trust CECPQ1
490
0
      */
491
0
      if(suite.kex_method() == Kex_Algo::CECPQ1)
492
0
         {
493
0
         if(value_exists(key_exchange_groups(), Group_Params::X25519) == false)
494
0
            continue;
495
0
         }
496
0
497
0
      // OK, consider it
498
0
      ciphersuites.push_back(suite);
499
0
      }
500
0
501
0
   if(ciphersuites.empty())
502
0
      {
503
0
      throw Invalid_State("Policy does not allow any available cipher suite");
504
0
      }
505
0
506
0
   Ciphersuite_Preference_Ordering order(ciphers, macs, kex, sigs);
507
0
   std::sort(ciphersuites.begin(), ciphersuites.end(), order);
508
0
509
0
   std::vector<uint16_t> ciphersuite_codes;
510
0
   for(auto i : ciphersuites)
511
0
      ciphersuite_codes.push_back(i.ciphersuite_code());
512
0
   return ciphersuite_codes;
513
0
   }
514
515
namespace {
516
517
void print_vec(std::ostream& o,
518
               const char* key,
519
               const std::vector<std::string>& v)
520
0
   {
521
0
   o << key << " = ";
522
0
   for(size_t i = 0; i != v.size(); ++i)
523
0
      {
524
0
      o << v[i];
525
0
      if(i != v.size() - 1)
526
0
         o << ' ';
527
0
      }
528
0
   o << '\n';
529
0
   }
530
531
void print_vec(std::ostream& o,
532
               const char* key,
533
               const std::vector<Group_Params>& v)
534
0
   {
535
0
   o << key << " = ";
536
0
   for(size_t i = 0; i != v.size(); ++i)
537
0
      {
538
0
      o << group_param_to_string(v[i]);
539
0
      if(i != v.size() - 1)
540
0
         o << ' ';
541
0
      }
542
0
   o << '\n';
543
0
   }
544
545
void print_bool(std::ostream& o,
546
                const char* key, bool b)
547
0
   {
548
0
   o << key << " = " << (b ? "true" : "false") << '\n';
549
0
   }
550
551
}
552
553
void Policy::print(std::ostream& o) const
554
0
   {
555
0
   print_bool(o, "allow_tls10", allow_tls10());
556
0
   print_bool(o, "allow_tls11", allow_tls11());
557
0
   print_bool(o, "allow_tls12", allow_tls12());
558
0
   print_bool(o, "allow_dtls10", allow_dtls10());
559
0
   print_bool(o, "allow_dtls12", allow_dtls12());
560
0
   print_vec(o, "ciphers", allowed_ciphers());
561
0
   print_vec(o, "macs", allowed_macs());
562
0
   print_vec(o, "signature_hashes", allowed_signature_hashes());
563
0
   print_vec(o, "signature_methods", allowed_signature_methods());
564
0
   print_vec(o, "key_exchange_methods", allowed_key_exchange_methods());
565
0
   print_vec(o, "key_exchange_groups", key_exchange_groups());
566
0
567
0
   print_bool(o, "allow_insecure_renegotiation", allow_insecure_renegotiation());
568
0
   print_bool(o, "include_time_in_hello_random", include_time_in_hello_random());
569
0
   print_bool(o, "allow_server_initiated_renegotiation", allow_server_initiated_renegotiation());
570
0
   print_bool(o, "hide_unknown_users", hide_unknown_users());
571
0
   print_bool(o, "server_uses_own_ciphersuite_preferences", server_uses_own_ciphersuite_preferences());
572
0
   print_bool(o, "negotiate_encrypt_then_mac", negotiate_encrypt_then_mac());
573
0
   print_bool(o, "support_cert_status_message", support_cert_status_message());
574
0
   o << "session_ticket_lifetime = " << session_ticket_lifetime() << '\n';
575
0
   o << "minimum_dh_group_size = " << minimum_dh_group_size() << '\n';
576
0
   o << "minimum_ecdh_group_size = " << minimum_ecdh_group_size() << '\n';
577
0
   o << "minimum_rsa_bits = " << minimum_rsa_bits() << '\n';
578
0
   o << "minimum_signature_strength = " << minimum_signature_strength() << '\n';
579
0
   }
580
581
std::string Policy::to_string() const
582
0
   {
583
0
   std::ostringstream oss;
584
0
   this->print(oss);
585
0
   return oss.str();
586
0
   }
587
588
std::vector<std::string> Strict_Policy::allowed_ciphers() const
589
0
   {
590
0
   return { "ChaCha20Poly1305", "AES-256/GCM", "AES-128/GCM" };
591
0
   }
592
593
std::vector<std::string> Strict_Policy::allowed_signature_hashes() const
594
0
   {
595
0
   return { "SHA-512", "SHA-384"};
596
0
   }
597
598
std::vector<std::string> Strict_Policy::allowed_macs() const
599
0
   {
600
0
   return { "AEAD" };
601
0
   }
602
603
std::vector<std::string> Strict_Policy::allowed_key_exchange_methods() const
604
0
   {
605
0
   return { "CECPQ1", "ECDH" };
606
0
   }
607
608
0
bool Strict_Policy::allow_tls10()  const { return false; }
609
0
bool Strict_Policy::allow_tls11()  const { return false; }
610
0
bool Strict_Policy::allow_tls12()  const { return true;  }
611
0
bool Strict_Policy::allow_dtls10() const { return false; }
612
0
bool Strict_Policy::allow_dtls12() const { return true;  }
613
614
}
615
616
}