Coverage Report

Created: 2023-02-13 06:21

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