Coverage Report

Created: 2020-11-21 08:34

/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
6.17k
   {
24
6.17k
   std::vector<Signature_Scheme> schemes;
25
26
6.17k
   for(Signature_Scheme scheme : all_signature_schemes())
27
92.5k
      {
28
92.5k
      if(signature_scheme_is_known(scheme) == false)
29
0
         continue;
30
92.5k
      const bool sig_allowed = allowed_signature_method(signature_algorithm_of_scheme(scheme));
31
92.5k
      const bool hash_allowed = allowed_signature_hash(hash_function_of_scheme(scheme));
32
33
92.5k
      if(sig_allowed && hash_allowed)
34
55.5k
         {
35
55.5k
         schemes.push_back(scheme);
36
55.5k
         }
37
92.5k
      }
38
39
6.17k
   return schemes;
40
6.17k
   }
41
42
std::vector<std::string> Policy::allowed_ciphers() const
43
0
   {
44
0
   return {
45
      //"AES-256/OCB(12)",
46
      //"AES-128/OCB(12)",
47
0
      "ChaCha20Poly1305",
48
0
      "AES-256/GCM",
49
0
      "AES-128/GCM",
50
      //"AES-256/CCM",
51
      //"AES-128/CCM",
52
      //"AES-256/CCM(8)",
53
      //"AES-128/CCM(8)",
54
      //"Camellia-256/GCM",
55
      //"Camellia-128/GCM",
56
      //"ARIA-256/GCM",
57
      //"ARIA-128/GCM",
58
      //"AES-256",
59
      //"AES-128",
60
      //"Camellia-256",
61
      //"Camellia-128",
62
      //"SEED",
63
      //"3DES",
64
0
      };
65
0
   }
66
67
std::vector<std::string> Policy::allowed_signature_hashes() const
68
94.2k
   {
69
94.2k
   return {
70
94.2k
      "SHA-512",
71
94.2k
      "SHA-384",
72
94.2k
      "SHA-256",
73
      //"SHA-1",
74
94.2k
      };
75
94.2k
   }
76
77
std::vector<std::string> Policy::allowed_macs() const
78
0
   {
79
   /*
80
   SHA-256 is preferred because the Lucky13 countermeasure works
81
   somewhat better for SHA-256 vs SHA-384:
82
   https://github.com/randombit/botan/pull/675
83
   */
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
      //"SRP_SHA",
96
      //"ECDHE_PSK",
97
      //"DHE_PSK",
98
      //"PSK",
99
0
      "CECPQ1",
100
0
      "ECDH",
101
0
      "DH",
102
      //"RSA",
103
0
      };
104
0
   }
105
106
std::vector<std::string> Policy::allowed_signature_methods() const
107
92.8k
   {
108
92.8k
   return {
109
92.8k
      "ECDSA",
110
92.8k
      "RSA",
111
      //"DSA",
112
      //"IMPLICIT",
113
      //"ANONYMOUS" (anon)
114
92.8k
      };
115
92.8k
   }
116
117
bool Policy::allowed_signature_method(const std::string& sig_method) const
118
92.8k
   {
119
92.8k
   return value_exists(allowed_signature_methods(), sig_method);
120
92.8k
   }
121
122
bool Policy::allowed_signature_hash(const std::string& sig_hash) const
123
94.2k
   {
124
94.2k
   return value_exists(allowed_signature_hashes(), sig_hash);
125
94.2k
   }
126
127
bool Policy::use_ecc_point_compression() const
128
6.88k
   {
129
6.88k
   return false;
130
6.88k
   }
131
132
Group_Params Policy::choose_key_exchange_group(const std::vector<Group_Params>& peer_groups) const
133
65.2k
   {
134
65.2k
   if(peer_groups.empty())
135
5.00k
      return Group_Params::NONE;
136
137
60.2k
   const std::vector<Group_Params> our_groups = key_exchange_groups();
138
139
60.2k
   for(auto g : our_groups)
140
341k
      {
141
341k
      if(value_exists(peer_groups, g))
142
58.5k
         return g;
143
341k
      }
144
145
1.70k
   return Group_Params::NONE;
146
60.2k
   }
147
148
Group_Params Policy::default_dh_group() const
149
4.20k
   {
150
   /*
151
   * Return the first listed or just default to 2048
152
   */
153
4.20k
   for(auto g : key_exchange_groups())
154
33.6k
      {
155
33.6k
      if(group_param_is_dh(g))
156
4.20k
         return g;
157
33.6k
      }
158
159
0
   return Group_Params::FFDHE_2048;
160
4.20k
   }
161
162
std::vector<Group_Params> Policy::key_exchange_groups() const
163
70.4k
   {
164
   // Default list is ordered by performance
165
70.4k
   return {
166
167
70.4k
#if defined(BOTAN_HAS_CURVE_25519)
168
70.4k
      Group_Params::X25519,
169
70.4k
#endif
170
171
70.4k
      Group_Params::SECP256R1,
172
70.4k
      Group_Params::BRAINPOOL256R1,
173
70.4k
      Group_Params::SECP384R1,
174
70.4k
      Group_Params::BRAINPOOL384R1,
175
70.4k
      Group_Params::SECP521R1,
176
70.4k
      Group_Params::BRAINPOOL512R1,
177
178
70.4k
      Group_Params::FFDHE_2048,
179
70.4k
      Group_Params::FFDHE_3072,
180
70.4k
      Group_Params::FFDHE_4096,
181
70.4k
      Group_Params::FFDHE_6144,
182
70.4k
      Group_Params::FFDHE_8192,
183
70.4k
      };
184
70.4k
   }
185
186
size_t Policy::minimum_dh_group_size() const
187
107
   {
188
107
   return 2048;
189
107
   }
190
191
size_t Policy::minimum_ecdsa_group_size() const
192
206
   {
193
   // Here we are at the mercy of whatever the CA signed, but most certs should be 256 bit by now
194
206
   return 256;
195
206
   }
196
197
size_t Policy::minimum_ecdh_group_size() const
198
540
   {
199
   // x25519 is smallest curve currently supported for TLS key exchange
200
540
   return 255;
201
540
   }
202
203
size_t Policy::minimum_signature_strength() const
204
526
   {
205
526
   return 110;
206
526
   }
207
208
bool Policy::require_cert_revocation_info() const
209
526
   {
210
526
   return true;
211
526
   }
212
213
size_t Policy::minimum_rsa_bits() const
214
41
   {
215
   /* Default assumption is all end-entity certificates should
216
      be at least 2048 bits these days.
217
218
      If you are connecting to arbitrary servers on the Internet
219
      (ie as a web browser or SMTP client) you'll probably have to reduce this
220
      to 1024 bits, or perhaps even lower.
221
   */
222
41
   return 2048;
223
41
   }
224
225
size_t Policy::minimum_dsa_group_size() const
226
0
   {
227
   // FIPS 186-3
228
0
   return 2048;
229
0
   }
230
231
void Policy::check_peer_key_acceptable(const Public_Key& public_key) const
232
894
   {
233
894
   const std::string algo_name = public_key.algo_name();
234
235
894
   const size_t keylength = public_key.key_length();
236
894
   size_t expected_keylength = 0;
237
238
894
   if(algo_name == "RSA")
239
41
      {
240
41
      expected_keylength = minimum_rsa_bits();
241
41
      }
242
853
   else if(algo_name == "DH")
243
107
      {
244
107
      expected_keylength = minimum_dh_group_size();
245
107
      }
246
746
   else if(algo_name == "DSA")
247
0
      {
248
0
      expected_keylength = minimum_dsa_group_size();
249
0
      }
250
746
   else if(algo_name == "ECDH" || algo_name == "Curve25519")
251
540
      {
252
540
      expected_keylength = minimum_ecdh_group_size();
253
540
      }
254
206
   else if(algo_name == "ECDSA")
255
206
      {
256
206
      expected_keylength = minimum_ecdsa_group_size();
257
206
      }
258
   // else some other algo, so leave expected_keylength as zero and the check is a no-op
259
260
894
   if(keylength < expected_keylength)
261
108
      throw TLS_Exception(Alert::INSUFFICIENT_SECURITY,
262
108
                          "Peer sent " +
263
108
                           std::to_string(keylength) + " bit " + algo_name + " key"
264
108
                           ", policy requires at least " +
265
108
                           std::to_string(expected_keylength));
266
894
   }
267
268
uint32_t Policy::session_ticket_lifetime() const
269
26.2k
   {
270
26.2k
   return 86400; // ~1 day
271
26.2k
   }
272
273
bool Policy::send_fallback_scsv(Protocol_Version version) const
274
6.01k
   {
275
6.01k
   return version != latest_supported_version(version.is_datagram_protocol());
276
6.01k
   }
277
278
bool Policy::acceptable_protocol_version(Protocol_Version version) const
279
55.6k
   {
280
55.6k
   if(version == Protocol_Version::TLS_V12 && allow_tls12())
281
42.5k
      return true;
282
283
13.1k
   if(version == Protocol_Version::DTLS_V12 && allow_dtls12())
284
13.0k
      return true;
285
286
106
#if defined(BOTAN_HAS_TLS_V10)
287
288
106
   if(version == Protocol_Version::TLS_V11 && allow_tls11())
289
0
      return true;
290
106
   if(version == Protocol_Version::TLS_V10 && allow_tls10())
291
0
      return true;
292
106
   if(version == Protocol_Version::DTLS_V10 && allow_dtls10())
293
0
      return true;
294
295
106
#endif
296
297
106
   return false;
298
106
   }
299
300
Protocol_Version Policy::latest_supported_version(bool datagram) const
301
38.6k
   {
302
38.6k
   if(datagram)
303
6.68k
      {
304
6.68k
      if(acceptable_protocol_version(Protocol_Version::DTLS_V12))
305
6.68k
         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
32.0k
   else
313
32.0k
      {
314
32.0k
      if(acceptable_protocol_version(Protocol_Version::TLS_V12))
315
32.0k
         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
38.6k
   }
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
33.3k
bool Policy::allow_insecure_renegotiation() const { return false; }
335
6.01k
bool Policy::allow_tls10()  const { return false; }
336
6.02k
bool Policy::allow_tls11()  const { return false; }
337
48.5k
bool Policy::allow_tls12()  const { return true; }
338
4
bool Policy::allow_dtls10() const { return false; }
339
13.0k
bool Policy::allow_dtls12() const { return true; }
340
57.3k
bool Policy::include_time_in_hello_random() const { return true; }
341
0
bool Policy::hide_unknown_users() const { return false; }
342
25.9k
bool Policy::server_uses_own_ciphersuite_preferences() const { return true; }
343
6.56k
bool Policy::negotiate_encrypt_then_mac() const { return true; }
344
6.33k
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
25.5k
bool Policy::require_client_certificate_authentication() const { return false; }
348
25.5k
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
640
bool Policy::allow_dtls_epoch0_restart() const { return false; }
351
352
2.88k
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
578
size_t Policy::dtls_initial_timeout() const { return 1*1000; }
356
578
size_t Policy::dtls_maximum_timeout() const { return 60*1000; }
357
358
size_t Policy::dtls_default_mtu() const
359
578
   {
360
   // default MTU is IPv6 min MTU minus UDP/IP headers
361
578
   return 1280 - 40 - 8;
362
578
   }
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
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
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
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
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
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
450
0
   std::vector<Ciphersuite> ciphersuites;
451
452
0
   for(auto&& suite : Ciphersuite::all_known_ciphersuites())
453
0
      {
454
      // Can we use it?
455
0
      if(!suite.valid())
456
0
         continue;
457
458
      // Can we use it in this version?
459
0
      if(!suite.usable_in_version(version))
460
0
         continue;
461
462
      // Is it acceptable to the policy?
463
0
      if(!this->acceptable_ciphersuite(suite))
464
0
         continue;
465
466
      // Are we doing SRP?
467
0
      if(!have_srp && suite.kex_method() == Kex_Algo::SRP_SHA)
468
0
         continue;
469
470
0
      if(!value_exists(kex, suite.kex_algo()))
471
0
         continue; // unsupported key exchange
472
473
0
      if(!value_exists(ciphers, suite.cipher_algo()))
474
0
         continue; // unsupported cipher
475
476
0
      if(!value_exists(macs, suite.mac_algo()))
477
0
         continue; // unsupported MAC algo
478
479
0
      if(!value_exists(sigs, suite.sig_algo()))
480
0
         {
481
         // 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
486
      /*
487
      CECPQ1 always uses x25519 for ECDH, so treat the applications
488
      removal of x25519 from the ECC curve list as equivalent to
489
      saying they do not trust CECPQ1
490
      */
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
497
      // OK, consider it
498
0
      ciphersuites.push_back(suite);
499
0
      }
500
501
0
   if(ciphersuites.empty())
502
0
      {
503
0
      throw Invalid_State("Policy does not allow any available cipher suite");
504
0
      }
505
506
0
   Ciphersuite_Preference_Ordering order(ciphers, macs, kex, sigs);
507
0
   std::sort(ciphersuites.begin(), ciphersuites.end(), order);
508
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
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
}