Coverage Report

Created: 2021-02-21 07:20

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