Coverage Report

Created: 2022-06-23 06:44

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