Coverage Report

Created: 2021-01-13 07:05

/src/botan/build/include/botan/tls_policy.h
Line
Count
Source (jump to first uncovered line)
1
/*
2
* Hooks for application level policies on TLS connections
3
* (C) 2004-2006,2013 Jack Lloyd
4
*     2017 Harry Reimann, Rohde & Schwarz Cybersecurity
5
*
6
* Botan is released under the Simplified BSD License (see license.txt)
7
*/
8
9
#ifndef BOTAN_TLS_POLICY_H_
10
#define BOTAN_TLS_POLICY_H_
11
12
#include <botan/tls_version.h>
13
#include <botan/tls_algos.h>
14
#include <botan/tls_ciphersuite.h>
15
#include <vector>
16
#include <map>
17
18
namespace Botan {
19
20
class Public_Key;
21
22
namespace TLS {
23
24
/**
25
* TLS Policy Base Class
26
* Inherit and overload as desired to suit local policy concerns
27
*/
28
class BOTAN_PUBLIC_API(2,0) Policy
29
   {
30
   public:
31
32
      /**
33
      * Returns a list of ciphers we are willing to negotiate, in
34
      * order of preference.
35
      */
36
      virtual std::vector<std::string> allowed_ciphers() const;
37
38
      /**
39
      * Returns a list of hash algorithms we are willing to use for
40
      * signatures, in order of preference.
41
      */
42
      virtual std::vector<std::string> allowed_signature_hashes() const;
43
44
      /**
45
      * Returns a list of MAC algorithms we are willing to use.
46
      */
47
      virtual std::vector<std::string> allowed_macs() const;
48
49
      /**
50
      * Returns a list of key exchange algorithms we are willing to
51
      * use, in order of preference. Allowed values: DH, empty string
52
      * (representing RSA using server certificate key)
53
      */
54
      virtual std::vector<std::string> allowed_key_exchange_methods() const;
55
56
      /**
57
      * Returns a list of signature algorithms we are willing to
58
      * use, in order of preference.
59
      */
60
      virtual std::vector<std::string> allowed_signature_methods() const;
61
62
      virtual std::vector<Signature_Scheme> allowed_signature_schemes() const;
63
64
      /**
65
      * Return a list of schemes we are willing to accept
66
      */
67
      virtual std::vector<Signature_Scheme> acceptable_signature_schemes() const;
68
69
      /**
70
      * The minimum signature strength we will accept
71
      * Returning 80 allows RSA 1024 and SHA-1. Values larger than 80 disable SHA-1 support.
72
      * Returning 110 allows RSA 2048.
73
      * Return 128 to force ECC (P-256) or large (~3000 bit) RSA keys.
74
      * Default is 110
75
      */
76
      virtual size_t minimum_signature_strength() const;
77
78
      /**
79
      * Return if cert revocation info (CRL/OCSP) is required
80
      * If true, validation will fail unless a valid CRL or OCSP response
81
      * was examined.
82
      */
83
      virtual bool require_cert_revocation_info() const;
84
85
      bool allowed_signature_method(const std::string& sig_method) const;
86
      bool allowed_signature_hash(const std::string& hash) const;
87
88
      /**
89
      * Return list of ECC curves and FFDHE groups we are willing to
90
      * use in order of preference.
91
      */
92
      virtual std::vector<Group_Params> key_exchange_groups() const;
93
94
      /**
95
      * Request that ECC curve points are sent compressed
96
      */
97
      virtual bool use_ecc_point_compression() const;
98
99
      /**
100
      * Select a key exchange group to use, from the list of groups sent by the
101
      * peer. If none are acceptable, return Group_Params::NONE
102
      */
103
      virtual Group_Params choose_key_exchange_group(const std::vector<Group_Params>& peer_groups) const;
104
105
      /**
106
      * Allow renegotiation even if the counterparty doesn't
107
      * support the secure renegotiation extension.
108
      *
109
      * @warning Changing this to true exposes you to injected
110
      * plaintext attacks. Read RFC 5746 for background.
111
      */
112
      virtual bool allow_insecure_renegotiation() const;
113
114
      /**
115
      * The protocol dictates that the first 32 bits of the random
116
      * field are the current time in seconds. However this allows
117
      * client fingerprinting attacks. Set to false to disable, in
118
      * which case random bytes will be used instead.
119
      */
120
      virtual bool include_time_in_hello_random() const;
121
122
      /**
123
      * Consulted by server side. If true, allows clients to initiate a new handshake
124
      */
125
      virtual bool allow_client_initiated_renegotiation() const;
126
127
      /**
128
      * Consulted by client side. If true, allows servers to initiate a new handshake
129
      */
130
      virtual bool allow_server_initiated_renegotiation() const;
131
132
      /**
133
      * If true, a request to renegotiate will close the connection with
134
      * a fatal alert. Otherwise, a warning alert is sent.
135
      */
136
      virtual bool abort_connection_on_undesired_renegotiation() const;
137
138
      virtual bool only_resume_with_exact_version() const;
139
140
      /**
141
      * Allow TLS v1.0
142
      */
143
      virtual bool allow_tls10() const;
144
145
      /**
146
      * Allow TLS v1.1
147
      */
148
      virtual bool allow_tls11() const;
149
150
      /**
151
      * Allow TLS v1.2
152
      */
153
      virtual bool allow_tls12() const;
154
155
      /**
156
      * Allow DTLS v1.0
157
      */
158
      virtual bool allow_dtls10() const;
159
160
      /**
161
      * Allow DTLS v1.2
162
      */
163
      virtual bool allow_dtls12() const;
164
165
      virtual Group_Params default_dh_group() const;
166
167
      /**
168
      * Return the minimum DH group size we're willing to use
169
      * Default is currently 1024 (insecure), should be 2048
170
      */
171
      virtual size_t minimum_dh_group_size() const;
172
173
      /**
174
      * For ECDSA authenticated ciphersuites, the smallest key size the
175
      * client will accept.
176
      * This policy is currently only enforced on the server by the client.
177
      */
178
      virtual size_t minimum_ecdsa_group_size() const;
179
180
      /**
181
      * Return the minimum ECDH group size we're willing to use
182
      * for key exchange
183
      *
184
      * Default 255, allowing x25519 and larger
185
      * x25519 is the smallest curve we will negotiate
186
      * P-521 is the largest
187
      */
188
      virtual size_t minimum_ecdh_group_size() const;
189
190
      /**
191
      * Return the minimum bit size we're willing to accept for RSA
192
      * key exchange or server signatures.
193
      *
194
      * It does not place any requirements on the size of any RSA signature(s)
195
      * which were used to check the server certificate. This is only
196
      * concerned with the server's public key.
197
      *
198
      * Default is 2048 which is smallest RSA key size still secure
199
      * for medium term security.
200
      */
201
      virtual size_t minimum_rsa_bits() const;
202
203
      /**
204
      * Throw an exception if you don't like the peer's key.
205
      * Default impl checks the key size against minimum_rsa_bits, minimum_ecdsa_group_size,
206
      * or minimum_ecdh_group_size depending on the key's type.
207
      * Override if you'd like to perform some other kind of test on
208
      * (or logging of) the peer's keys.
209
      */
210
      virtual void check_peer_key_acceptable(const Public_Key& public_key) const;
211
212
      /**
213
      * If this function returns false, unknown PSK identifiers
214
      * will be rejected with an unknown_psk_identifier alert as soon
215
      * as the non-existence is identified. Otherwise, a false
216
      * identifier value will be used and the protocol allowed to
217
      * proceed, causing the handshake to eventually fail without
218
      * revealing that the username does not exist on this system.
219
      */
220
      virtual bool hide_unknown_users() const;
221
222
      /**
223
      * Return the allowed lifetime of a session ticket. If 0, session
224
      * tickets do not expire until the session ticket key rolls over.
225
      * Expired session tickets cannot be used to resume a session.
226
      */
227
      virtual uint32_t session_ticket_lifetime() const;
228
229
      /**
230
      * If this returns a non-empty vector, and DTLS is negotiated,
231
      * then we will also attempt to negotiate the SRTP extension from
232
      * RFC 5764 using the returned values as the profile ids.
233
      */
234
      virtual std::vector<uint16_t> srtp_profiles() const;
235
236
      /**
237
      * @return true if and only if we are willing to accept this version
238
      * Default accepts TLS v1.0 and later or DTLS v1.2 or later.
239
      */
240
      virtual bool acceptable_protocol_version(Protocol_Version version) const;
241
242
      /**
243
      * Returns the more recent protocol version we are willing to
244
      * use, for either TLS or DTLS depending on datagram param.
245
      * Shouldn't ever need to override this unless you want to allow
246
      * a user to disable use of TLS v1.2 (which is *not recommended*)
247
      */
248
      virtual Protocol_Version latest_supported_version(bool datagram) const;
249
250
      /**
251
      * When offering this version, should we send a fallback SCSV?
252
      * Default returns true iff version is not the latest version the
253
      * policy allows, exists to allow override in case of interop problems.
254
      */
255
      virtual bool send_fallback_scsv(Protocol_Version version) const;
256
257
      /**
258
      * Allows policy to reject any ciphersuites which are undesirable
259
      * for whatever reason without having to reimplement ciphersuite_list
260
      */
261
      virtual bool acceptable_ciphersuite(const Ciphersuite& suite) const;
262
263
      /**
264
      * @return true if servers should choose the ciphersuite matching
265
      *         their highest preference, rather than the clients.
266
      *         Has no effect on client side.
267
      */
268
      virtual bool server_uses_own_ciphersuite_preferences() const;
269
270
      /**
271
      * Indicates whether the encrypt-then-MAC extension should be negotiated
272
      * (RFC 7366)
273
      */
274
      virtual bool negotiate_encrypt_then_mac() const;
275
276
      /**
277
      * Indicates whether certificate status messages should be supported
278
      */
279
      virtual bool support_cert_status_message() const;
280
281
      /**
282
      * Indicate if client certificate authentication is required.
283
      * If true, then a cert will be requested and if the client does
284
      * not send a certificate the connection will be closed.
285
      */
286
      virtual bool require_client_certificate_authentication() const;
287
288
      /**
289
      * Indicate if client certificate authentication is requested.
290
      * If true, then a cert will be requested.
291
      */
292
      virtual bool request_client_certificate_authentication() const;
293
294
      /**
295
      * If true, then allow a DTLS client to restart a connection to the
296
      * same server association as described in section 4.2.8 of the DTLS RFC
297
      */
298
      virtual bool allow_dtls_epoch0_restart() const;
299
300
      /**
301
      * Return allowed ciphersuites, in order of preference
302
      */
303
      virtual std::vector<uint16_t> ciphersuite_list(Protocol_Version version) const;
304
305
      /**
306
      * @return the default MTU for DTLS
307
      */
308
      virtual size_t dtls_default_mtu() const;
309
310
      /**
311
      * @return the initial timeout for DTLS
312
      */
313
      virtual size_t dtls_initial_timeout() const;
314
315
      /**
316
      * @return the maximum timeout for DTLS
317
      */
318
      virtual size_t dtls_maximum_timeout() const;
319
320
      /**
321
      * @return the maximum size of the certificate chain, in bytes.
322
      * Return 0 to disable this and accept any size.
323
      */
324
      virtual size_t maximum_certificate_chain_size() const;
325
326
      virtual bool allow_resumption_for_renegotiation() const;
327
328
      /**
329
      * Convert this policy to a printable format.
330
      * @param o stream to be printed to
331
      */
332
      virtual void print(std::ostream& o) const;
333
334
      /**
335
      * Convert this policy to a printable format.
336
      * Same as calling `print` on a ostringstream and reading o.str()
337
      */
338
      std::string to_string() const;
339
340
11.8k
      virtual ~Policy() = default;
341
   };
342
343
typedef Policy Default_Policy;
344
345
/**
346
* NSA Suite B 128-bit security level (RFC 6460)
347
*
348
* @warning As of August 2015 NSA indicated only the 192-bit Suite B
349
* should be used for all classification levels.
350
*/
351
class BOTAN_PUBLIC_API(2,0) NSA_Suite_B_128 : public Policy
352
   {
353
   public:
354
      std::vector<std::string> allowed_ciphers() const override
355
0
         { return std::vector<std::string>({"AES-128/GCM"}); }
356
357
      std::vector<std::string> allowed_signature_hashes() const override
358
0
         { return std::vector<std::string>({"SHA-256"}); }
359
360
      std::vector<std::string> allowed_macs() const override
361
0
         { return std::vector<std::string>({"AEAD"}); }
362
363
      std::vector<std::string> allowed_key_exchange_methods() const override
364
0
         { return std::vector<std::string>({"ECDH"}); }
365
366
      std::vector<std::string> allowed_signature_methods() const override
367
0
         { return std::vector<std::string>({"ECDSA"}); }
368
369
      std::vector<Group_Params> key_exchange_groups() const override
370
0
         { return {Group_Params::SECP256R1}; }
371
372
0
      size_t minimum_signature_strength() const override { return 128; }
373
374
0
      bool allow_tls10()  const override { return false; }
375
0
      bool allow_tls11()  const override { return false; }
376
0
      bool allow_tls12()  const override { return true;  }
377
0
      bool allow_dtls10() const override { return false; }
378
0
      bool allow_dtls12() const override { return false; }
379
   };
380
381
/**
382
* NSA Suite B 192-bit security level (RFC 6460)
383
*/
384
class BOTAN_PUBLIC_API(2,7) NSA_Suite_B_192 : public Policy
385
   {
386
   public:
387
      std::vector<std::string> allowed_ciphers() const override
388
0
         { return std::vector<std::string>({"AES-256/GCM"}); }
389
390
      std::vector<std::string> allowed_signature_hashes() const override
391
0
         { return std::vector<std::string>({"SHA-384"}); }
392
393
      std::vector<std::string> allowed_macs() const override
394
0
         { return std::vector<std::string>({"AEAD"}); }
395
396
      std::vector<std::string> allowed_key_exchange_methods() const override
397
0
         { return std::vector<std::string>({"ECDH"}); }
398
399
      std::vector<std::string> allowed_signature_methods() const override
400
0
         { return std::vector<std::string>({"ECDSA"}); }
401
402
      std::vector<Group_Params> key_exchange_groups() const override
403
0
         { return {Group_Params::SECP384R1}; }
404
405
0
      size_t minimum_signature_strength() const override { return 192; }
406
407
0
      bool allow_tls10()  const override { return false; }
408
0
      bool allow_tls11()  const override { return false; }
409
0
      bool allow_tls12()  const override { return true;  }
410
0
      bool allow_dtls10() const override { return false; }
411
0
      bool allow_dtls12() const override { return false; }
412
   };
413
414
/**
415
* BSI TR-02102-2 Policy
416
*/
417
class BOTAN_PUBLIC_API(2,0) BSI_TR_02102_2 : public Policy
418
   {
419
   public:
420
      std::vector<std::string> allowed_ciphers() const override
421
0
         {
422
0
         return std::vector<std::string>({"AES-256/GCM", "AES-128/GCM", "AES-256/CCM", "AES-128/CCM", "AES-256", "AES-128"});
423
0
         }
424
425
      std::vector<std::string> allowed_signature_hashes() const override
426
0
         {
427
0
         return std::vector<std::string>({"SHA-512", "SHA-384", "SHA-256"});
428
0
         }
429
430
      std::vector<std::string> allowed_macs() const override
431
0
         {
432
0
         return std::vector<std::string>({"AEAD", "SHA-384", "SHA-256"});
433
0
         }
434
435
      std::vector<std::string> allowed_key_exchange_methods() const override
436
0
         {
437
0
         return std::vector<std::string>({"ECDH", "DH", "ECDHE_PSK" });
438
0
         }
439
440
      std::vector<std::string> allowed_signature_methods() const override
441
0
         {
442
0
         return std::vector<std::string>({"ECDSA", "RSA", "DSA"});
443
0
         }
444
445
      std::vector<Group_Params> key_exchange_groups() const override
446
0
         {
447
0
         return std::vector<Group_Params>({
448
0
            Group_Params::BRAINPOOL512R1,
449
0
            Group_Params::BRAINPOOL384R1,
450
0
            Group_Params::BRAINPOOL256R1,
451
0
            Group_Params::SECP384R1,
452
0
            Group_Params::SECP256R1,
453
0
            Group_Params::FFDHE_4096,
454
0
            Group_Params::FFDHE_3072,
455
0
            Group_Params::FFDHE_2048
456
0
            });
457
0
         }
458
459
0
      bool allow_insecure_renegotiation() const override { return false; }
460
0
      bool allow_server_initiated_renegotiation() const override { return true; }
461
0
      bool server_uses_own_ciphersuite_preferences() const override { return true; }
462
0
      bool negotiate_encrypt_then_mac() const override { return true; }
463
464
0
      size_t minimum_rsa_bits() const override { return 2000; }
465
0
      size_t minimum_dh_group_size() const override { return 2000; }
466
467
0
      size_t minimum_ecdh_group_size() const override { return 250; }
468
0
      size_t minimum_ecdsa_group_size() const override { return 250; }
469
470
0
      bool allow_tls10()  const override { return false; }
471
0
      bool allow_tls11()  const override { return false; }
472
0
      bool allow_tls12()  const override { return true;  }
473
0
      bool allow_dtls10() const override { return false; }
474
0
      bool allow_dtls12() const override { return false; }
475
   };
476
477
/**
478
* Policy for DTLS. We require DTLS v1.2 and an AEAD mode.
479
*/
480
class BOTAN_PUBLIC_API(2,0) Datagram_Policy : public Policy
481
   {
482
   public:
483
      std::vector<std::string> allowed_macs() const override
484
0
         { return std::vector<std::string>({"AEAD"}); }
485
486
0
      bool allow_tls10()  const override { return false; }
487
0
      bool allow_tls11()  const override { return false; }
488
0
      bool allow_tls12()  const override { return false; }
489
0
      bool allow_dtls10() const override { return false; }
490
0
      bool allow_dtls12() const override { return true;  }
491
   };
492
493
/*
494
* This policy requires a secure version of TLS and disables all insecure
495
* algorithms. It is compatible with other botan TLSes (including those using the
496
* default policy) and with many other recent implementations. It is a great idea
497
* to use if you control both sides of the protocol and don't have to worry
498
* about ancient and/or bizarre TLS implementations.
499
*/
500
class BOTAN_PUBLIC_API(2,0) Strict_Policy : public Policy
501
   {
502
   public:
503
      std::vector<std::string> allowed_ciphers() const override;
504
505
      std::vector<std::string> allowed_signature_hashes() const override;
506
507
      std::vector<std::string> allowed_macs() const override;
508
509
      std::vector<std::string> allowed_key_exchange_methods() const override;
510
511
      bool allow_tls10()  const override;
512
      bool allow_tls11()  const override;
513
      bool allow_tls12()  const override;
514
      bool allow_dtls10() const override;
515
      bool allow_dtls12() const override;
516
   };
517
518
class BOTAN_PUBLIC_API(2,0) Text_Policy : public Policy
519
   {
520
   public:
521
522
      std::vector<std::string> allowed_ciphers() const override;
523
524
      std::vector<std::string> allowed_signature_hashes() const override;
525
526
      std::vector<std::string> allowed_macs() const override;
527
528
      std::vector<std::string> allowed_key_exchange_methods() const override;
529
530
      std::vector<std::string> allowed_signature_methods() const override;
531
532
      std::vector<Group_Params> key_exchange_groups() const override;
533
534
      bool use_ecc_point_compression() const override;
535
536
      bool allow_tls10() const override;
537
538
      bool allow_tls11() const override;
539
540
      bool allow_tls12() const override;
541
542
      bool allow_dtls10() const override;
543
544
      bool allow_dtls12() const override;
545
546
      bool allow_insecure_renegotiation() const override;
547
548
      bool include_time_in_hello_random() const override;
549
550
      bool allow_client_initiated_renegotiation() const override;
551
      bool allow_server_initiated_renegotiation() const override;
552
553
      bool server_uses_own_ciphersuite_preferences() const override;
554
555
      bool negotiate_encrypt_then_mac() const override;
556
557
      bool support_cert_status_message() const override;
558
559
      bool require_client_certificate_authentication() const override;
560
561
      size_t minimum_ecdh_group_size() const override;
562
563
      size_t minimum_ecdsa_group_size() const override;
564
565
      size_t minimum_dh_group_size() const override;
566
567
      size_t minimum_rsa_bits() const override;
568
569
      size_t minimum_signature_strength() const override;
570
571
      size_t dtls_default_mtu() const override;
572
573
      size_t dtls_initial_timeout() const override;
574
575
      size_t dtls_maximum_timeout() const override;
576
577
      bool require_cert_revocation_info() const override;
578
579
      bool hide_unknown_users() const override;
580
581
      uint32_t session_ticket_lifetime() const override;
582
583
      bool send_fallback_scsv(Protocol_Version version) const override;
584
585
      std::vector<uint16_t> srtp_profiles() const override;
586
587
      void set(const std::string& k, const std::string& v);
588
589
      explicit Text_Policy(const std::string& s);
590
591
      explicit Text_Policy(std::istream& in);
592
593
   protected:
594
595
      std::vector<std::string> get_list(const std::string& key,
596
                                        const std::vector<std::string>& def) const;
597
598
      size_t get_len(const std::string& key, size_t def) const;
599
600
      bool get_bool(const std::string& key, bool def) const;
601
602
      std::string get_str(const std::string& key, const std::string& def = "") const;
603
604
      bool set_value(const std::string& key, const std::string& val, bool overwrite);
605
606
   private:
607
      std::map<std::string, std::string> m_kv;
608
   };
609
610
}
611
612
}
613
614
#endif