Coverage Report

Created: 2021-01-13 07:05

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