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