Botan 3.12.0
Crypto and TLS for C&
tls_extensions.h
Go to the documentation of this file.
1/*
2* TLS Extensions
3* (C) 2011,2012,2016,2018,2019 Jack Lloyd
4* (C) 2016 Juraj Somorovsky
5* (C) 2016 Matthias Gierlings
6* (C) 2021 Elektrobit Automotive GmbH
7* (C) 2022 René Meusel, Hannes Rantzsch - neXenio GmbH
8* (C) 2023 Fabian Albert, René Meusel - Rohde & Schwarz Cybersecurity
9*
10* Botan is released under the Simplified BSD License (see license.txt)
11*/
12
13#ifndef BOTAN_TLS_EXTENSIONS_H_
14#define BOTAN_TLS_EXTENSIONS_H_
15
16#include <botan/assert.h>
17#include <botan/tls_algos.h>
18#include <botan/tls_magic.h>
19#include <botan/tls_signature_scheme.h>
20#include <botan/tls_version.h>
21
22#include <map>
23#include <memory>
24#include <optional>
25#include <set>
26
27namespace Botan {
28
31class X509_DN;
32
33namespace TLS {
34
35class Policy;
36class TLS_Data_Reader;
37
38enum class Extension_Code : uint16_t {
41
43 EcPointFormats = 11, // TLS 1.2 exclusive
46 UseSrtp = 14,
48
49 // SignedCertificateTimestamp = 18, // NYI
50
51 // RFC 7250 (Raw Public Keys in TLS)
54
55 Padding = 21, // RFC 7685; not implemented but recognized so it can be
56 // explicitly carved out of strict-mutation checks.
57
58 EncryptThenMac = 22, // TLS 1.2 exclusive
59 ExtendedMasterSecret = 23, // TLS 1.2 exclusive
60
62
63 SessionTicket = 35, // TLS 1.2 exclusive
64
66
67 PresharedKey = 41, // TLS 1.3 exclusive
68 EarlyData = 42, // TLS 1.3 exclusive
69 Cookie = 44, // TLS 1.3 exclusive
70 PskKeyExchangeModes = 45, // TLS 1.3 exclusive
71 CertificateAuthorities = 47, // TLS 1.3 exclusive
72 KeyShare = 51, // TLS 1.3 exclusive
73
74 SafeRenegotiation = 65281, // TLS 1.2 exclusive
75};
76
77/**
78* Base class representing a TLS extension of some kind
79*/
80class BOTAN_UNSTABLE_API Extension /* NOLINT(*-special-member-functions) */ {
81 public:
82 /**
83 * @return code number of the extension
84 */
85 virtual Extension_Code type() const = 0;
86
87 /**
88 * @return serialized binary for the extension
89 */
90 virtual std::vector<uint8_t> serialize(Connection_Side whoami) const = 0;
91
92 /**
93 * @return if we should encode this extension or not
94 */
95 virtual bool empty() const = 0;
96
97 /**
98 * @return true if this extension is known and implemented by Botan
99 */
100 virtual bool is_implemented() const { return true; }
101
102 virtual ~Extension() = default;
103};
104
105/**
106* Server Name Indicator extension (RFC 3546)
107*/
109 public:
111
112 Extension_Code type() const override { return static_type(); }
113
114 explicit Server_Name_Indicator(std::string_view host_name) : m_sni_host_name(host_name) {}
115
116 Server_Name_Indicator(TLS_Data_Reader& reader, uint16_t extension_size, Connection_Side from);
117
118 std::string host_name() const { return m_sni_host_name; }
119
120 std::vector<uint8_t> serialize(Connection_Side whoami) const override;
121
122 bool empty() const override { return false; }
123
124 static bool hostname_acceptable_for_sni(std::string_view hostname);
125
126 private:
127 std::string m_sni_host_name;
128};
129
130/**
131* ALPN (RFC 7301)
132*/
134 public:
136
137 Extension_Code type() const override { return static_type(); }
138
139 const std::vector<std::string>& protocols() const { return m_protocols; }
140
141 std::string single_protocol() const;
142
143 /**
144 * Single protocol, used by server
145 */
146 explicit Application_Layer_Protocol_Notification(std::string_view protocol);
147
148 /**
149 * List of protocols, used by client
150 */
151 explicit Application_Layer_Protocol_Notification(const std::vector<std::string>& protocols);
152
153 Application_Layer_Protocol_Notification(TLS_Data_Reader& reader, uint16_t extension_size, Connection_Side from);
154
155 std::vector<uint8_t> serialize(Connection_Side whoami) const override;
156
157 bool empty() const override { return m_protocols.empty(); }
158
159 private:
160 std::vector<std::string> m_protocols;
161};
162
163/**
164 * RFC 7250
165 * Base class for 'client_certificate_type' and 'server_certificate_type' extensions.
166 */
168 public:
169 /**
170 * Called by the client to advertise support for a number of cert types.
171 */
172 explicit Certificate_Type_Base(std::vector<Certificate_Type> supported_cert_types);
173
174 protected:
175 /**
176 * Called by the server to select a cert type to be used in the handshake.
177 */
178 Certificate_Type_Base(const Certificate_Type_Base& certificate_type_from_client,
179 const std::vector<Certificate_Type>& server_preference);
180
181 public:
182 Certificate_Type_Base(TLS_Data_Reader& reader, uint16_t extension_size, Connection_Side from);
183
184 std::vector<uint8_t> serialize(Connection_Side whoami) const override;
185
186 void validate_selection(const Certificate_Type_Base& from_server) const;
188
189 bool empty() const override {
190 // RFC 7250 4.1
191 // If the client has no remaining certificate types to send in the
192 // client hello, other than the default X.509 type, it MUST omit the
193 // entire client[/server]_certificate_type extension [...].
194 return m_from == Connection_Side::Client && m_certificate_types.size() == 1 &&
195 m_certificate_types.front() == Certificate_Type::X509;
196 }
197
198 private:
199 std::vector<Certificate_Type> m_certificate_types;
200 Connection_Side m_from;
201};
202
204 public:
206
207 /**
208 * Creates the Server Hello extension from the received client preferences.
209 */
210 Client_Certificate_Type(const Client_Certificate_Type& cct, const Policy& policy);
211
213
214 Extension_Code type() const override { return static_type(); }
215};
216
218 public:
220
221 /**
222 * Creates the Server Hello extension from the received client preferences.
223 */
224 Server_Certificate_Type(const Server_Certificate_Type& sct, const Policy& policy);
225
227
228 Extension_Code type() const override { return static_type(); }
229};
230
231/**
232* Supported Groups Extension (RFC 7919)
233*/
235 public:
237
238 Extension_Code type() const override { return static_type(); }
239
240 const std::vector<Group_Params>& groups() const;
241
242 // Returns the list of groups we recognize as ECDH curves
243 std::vector<Group_Params> ec_groups() const;
244
245 // Returns the list of any groups in the FFDHE range
246 std::vector<Group_Params> dh_groups() const;
247
248 std::vector<uint8_t> serialize(Connection_Side whoami) const override;
249
250 explicit Supported_Groups(const std::vector<Group_Params>& groups);
251
252 Supported_Groups(TLS_Data_Reader& reader, uint16_t extension_size);
253
254 bool empty() const override { return m_groups.empty(); }
255
256 private:
257 std::vector<Group_Params> m_groups;
258};
259
260/**
261* Signature Algorithms Extension for TLS 1.2 (RFC 5246)
262*/
264 public:
266
267 Extension_Code type() const override { return static_type(); }
268
269 const std::vector<Signature_Scheme>& supported_schemes() const { return m_schemes; }
270
271 std::vector<uint8_t> serialize(Connection_Side whoami) const override;
272
273 bool empty() const override { return m_schemes.empty(); }
274
275 explicit Signature_Algorithms(std::vector<Signature_Scheme> schemes) : m_schemes(std::move(schemes)) {}
276
277 Signature_Algorithms(TLS_Data_Reader& reader, uint16_t extension_size);
278
279 private:
280 std::vector<Signature_Scheme> m_schemes;
281};
282
283/**
284* Signature_Algorithms_Cert for TLS 1.3 (RFC 8446)
285*
286* RFC 8446 4.2.3
287* TLS 1.3 provides two extensions for indicating which signature algorithms
288* may be used in digital signatures. The "signature_algorithms_cert"
289* extension applies to signatures in certificates, and the
290* "signature_algorithms" extension, which originally appeared in TLS 1.2,
291* applies to signatures in CertificateVerify messages.
292*
293* RFC 8446 4.2.3
294* TLS 1.2 implementations SHOULD also process this extension.
295*/
297 public:
299
300 Extension_Code type() const override { return static_type(); }
301
302 const std::vector<Signature_Scheme>& supported_schemes() const { return m_schemes; }
303
304 std::vector<uint8_t> serialize(Connection_Side whoami) const override;
305
306 bool empty() const override { return m_schemes.empty(); }
307
308 explicit Signature_Algorithms_Cert(std::vector<Signature_Scheme> schemes) : m_schemes(std::move(schemes)) {}
309
310 Signature_Algorithms_Cert(TLS_Data_Reader& reader, uint16_t extension_size);
311
312 private:
313 std::vector<Signature_Scheme> m_schemes;
314};
315
316/**
317* Used to indicate SRTP algorithms for DTLS (RFC 5764)
318*/
320 public:
322
323 Extension_Code type() const override { return static_type(); }
324
325 const std::vector<uint16_t>& profiles() const { return m_pp; }
326
327 std::vector<uint8_t> serialize(Connection_Side whoami) const override;
328
329 bool empty() const override { return m_pp.empty(); }
330
331 explicit SRTP_Protection_Profiles(const std::vector<uint16_t>& pp) : m_pp(pp) {}
332
333 explicit SRTP_Protection_Profiles(uint16_t pp) : m_pp(1, pp) {}
334
335 SRTP_Protection_Profiles(TLS_Data_Reader& reader, uint16_t extension_size);
336
337 private:
338 std::vector<uint16_t> m_pp;
339};
340
341class Certificate_Status_Request_Internal;
342
343/**
344* Certificate Status Request (RFC 6066)
345*/
346class BOTAN_UNSTABLE_API Certificate_Status_Request final : public Extension /* NOLINT(*-special-member-functions) */ {
347 public:
349
350 Extension_Code type() const override { return static_type(); }
351
352 std::vector<uint8_t> serialize(Connection_Side whoami) const override;
353
354 bool empty() const override { return false; }
355
356 const std::vector<uint8_t>& get_responder_id_list() const;
357 const std::vector<uint8_t>& get_request_extensions() const;
358 const std::vector<uint8_t>& get_ocsp_response() const;
359
360 // TLS 1.2 Server generated version: empty
362
363 // TLS 1.2 Client version, both lists can be empty
364 Certificate_Status_Request(std::vector<uint8_t> ocsp_responder_ids,
365 std::vector<std::vector<uint8_t>> ocsp_key_ids);
366
367 // TLS 1.3 version
368 explicit Certificate_Status_Request(std::vector<uint8_t> response);
369
371 uint16_t extension_size,
372 Handshake_Type message_type,
373 Connection_Side from);
374
376
377 private:
378 std::unique_ptr<Certificate_Status_Request_Internal> m_impl;
379};
380
381/**
382* Supported Versions from RFC 8446
383*/
385 public:
387
388 Extension_Code type() const override { return static_type(); }
389
390 std::vector<uint8_t> serialize(Connection_Side whoami) const override;
391
392 bool empty() const override { return m_versions.empty(); }
393
394 Supported_Versions(Protocol_Version version, const Policy& policy);
395
396 explicit Supported_Versions(Protocol_Version version) { m_versions.push_back(version); }
397
398 Supported_Versions(TLS_Data_Reader& reader, uint16_t extension_size, Connection_Side from);
399
400 bool supports(Protocol_Version version) const;
401
402 const std::vector<Protocol_Version>& versions() const { return m_versions; }
403
404 private:
405 std::vector<Protocol_Version> m_versions;
406};
407
409
410/**
411* Record Size Limit (RFC 8449)
412*
413* TODO: the record size limit is currently not honored by the TLS 1.2 stack
414*/
416 public:
418
419 Extension_Code type() const override { return static_type(); }
420
421 explicit Record_Size_Limit(uint16_t limit);
422
423 Record_Size_Limit(TLS_Data_Reader& reader, uint16_t extension_size, Connection_Side from);
424
425 uint16_t limit() const { return m_limit; }
426
427 std::vector<uint8_t> serialize(Connection_Side whoami) const override;
428
429 bool empty() const override { return m_limit == 0; }
430
431 private:
432 uint16_t m_limit;
433};
434
435/**
436* Unknown extensions are deserialized as this type
437*/
439 public:
440 Unknown_Extension(Extension_Code type, TLS_Data_Reader& reader, uint16_t extension_size);
441
442 std::vector<uint8_t> serialize(Connection_Side whoami) const override;
443
444 const std::vector<uint8_t>& value() { return m_value; }
445
446 bool empty() const override { return false; }
447
448 Extension_Code type() const override { return m_type; }
449
450 bool is_implemented() const override { return false; }
451
452 private:
453 Extension_Code m_type;
454 std::vector<uint8_t> m_value;
455};
456
457/**
458* Represents a block of extensions in a hello message
459*/
461 public:
462 std::set<Extension_Code> extension_types() const;
463
464 template <typename T>
465 T* get() const {
466 return dynamic_cast<T*>(get(T::static_type()));
467 }
468
469 template <typename T>
470 bool has() const {
471 return get<T>() != nullptr;
472 }
473
474 bool has(Extension_Code type) const;
475
476 size_t size() const { return m_extensions.size(); }
477
478 bool empty() const { return m_extensions.empty(); }
479
480 void add(std::unique_ptr<Extension> extn);
481
482 void add(Extension* extn) { add(std::unique_ptr<Extension>(extn)); }
483
484 Extension* get(Extension_Code type) const;
485
486 std::vector<uint8_t> serialize(Connection_Side whoami) const;
487
488 void deserialize(TLS_Data_Reader& reader, Connection_Side from, Handshake_Type message_type);
489
490 /**
491 * @param allowed_extensions extension types that are allowed
492 * @param allow_unknown_extensions if true, ignores unrecognized extensions
493 * @returns true if this contains any extensions that are not contained in @p allowed_extensions.
494 */
495 bool contains_other_than(const std::set<Extension_Code>& allowed_extensions,
496 bool allow_unknown_extensions = false) const;
497
498 /**
499 * @param allowed_extensions extension types that are allowed
500 * @returns true if this contains any extensions implemented by Botan that
501 * are not contained in @p allowed_extensions.
502 */
503 bool contains_implemented_extensions_other_than(const std::set<Extension_Code>& allowed_extensions) const {
504 return contains_other_than(allowed_extensions, true);
505 }
506
507 /**
508 * Remove an extension from this extensions object, if it exists.
509 * Returns true if the extension existed (and thus is now removed),
510 * otherwise false (the extension wasn't set in the first place).
511 *
512 * Note: not used internally, might be used in Callbacks::tls_modify_extensions()
513 */
514 bool remove_extension(Extension_Code type);
515
516 /**
517 * Reorder extensions for serialization. Extensions not mentioned in
518 * @p order retain their relative position at the front; extensions in
519 * @p order are appended in the given order.
520 */
521 void reorder(const std::vector<Extension_Code>& order);
522
523 /**
524 * Return the code of the extension that appears last in the encoding
525 * This is used for checking the position of PSK extension in TLS 1.3
526 */
527 std::optional<Extension_Code> last_added() const {
528 if(m_extension_codes.empty()) {
529 return {};
530 } else {
531 return m_extension_codes.back();
532 }
533 }
534
535 Extensions() = default;
536 Extensions(const Extensions&) = delete;
537 Extensions& operator=(const Extensions&) = delete;
538 Extensions(Extensions&&) = default;
541
543 deserialize(reader, side, message_type);
544 }
545
546 /**
547 * @returns the raw bytes of the extension with the given type as they
548 * appeared on the wire during deserialization, or std::nullopt
549 * if the extension was not present or was added programmatically.
550 */
551 std::optional<std::vector<uint8_t>> extension_raw_bytes(Extension_Code type) const {
552 auto it = m_raw_extension_data.find(type);
553 if(it != m_raw_extension_data.end()) {
554 return it->second;
555 }
556 return std::nullopt;
557 }
558
559 private:
560 // Kept in the order they were added
561 std::vector<Extension_Code> m_extension_codes;
562 std::map<Extension_Code, std::unique_ptr<Extension>> m_extensions;
563 std::map<Extension_Code, std::vector<uint8_t>> m_raw_extension_data;
564};
565
566} // namespace TLS
567
568} // namespace Botan
569
570#endif
#define BOTAN_UNSTABLE_API
Definition api.h:34
Application_Layer_Protocol_Notification(std::string_view protocol)
const std::vector< std::string > & protocols() const
const std::vector< uint8_t > & get_request_extensions() const
const std::vector< uint8_t > & get_responder_id_list() const
const std::vector< uint8_t > & get_ocsp_response() const
Extension_Code type() const override
Certificate_Type selected_certificate_type() const
Certificate_Type_Base(std::vector< Certificate_Type > supported_cert_types)
void validate_selection(const Certificate_Type_Base &from_server) const
std::vector< uint8_t > serialize(Connection_Side whoami) const override
Extension_Code type() const override
Client_Certificate_Type(const Client_Certificate_Type &cct, const Policy &policy)
Certificate_Type_Base(std::vector< Certificate_Type > supported_cert_types)
static Extension_Code static_type()
virtual std::vector< uint8_t > serialize(Connection_Side whoami) const =0
virtual bool is_implemented() const
virtual Extension_Code type() const =0
virtual bool empty() const =0
virtual ~Extension()=default
Extensions(Extensions &&)=default
void add(Extension *extn)
bool contains_implemented_extensions_other_than(const std::set< Extension_Code > &allowed_extensions) const
Extensions & operator=(const Extensions &)=delete
std::optional< std::vector< uint8_t > > extension_raw_bytes(Extension_Code type) const
Extensions(const Extensions &)=delete
void deserialize(TLS_Data_Reader &reader, Connection_Side from, Handshake_Type message_type)
std::optional< Extension_Code > last_added() const
Extensions(TLS_Data_Reader &reader, Connection_Side side, Handshake_Type message_type)
Extensions & operator=(Extensions &&)=default
std::set< Extension_Code > extension_types() const
bool contains_other_than(const std::set< Extension_Code > &allowed_extensions, bool allow_unknown_extensions=false) const
Extension_Code type() const override
static Extension_Code static_type()
bool empty() const override
Extension_Code type() const override
SRTP_Protection_Profiles(const std::vector< uint16_t > &pp)
const std::vector< uint16_t > & profiles() const
static Extension_Code static_type()
static Extension_Code static_type()
Server_Certificate_Type(const Server_Certificate_Type &sct, const Policy &policy)
Certificate_Type_Base(std::vector< Certificate_Type > supported_cert_types)
Extension_Code type() const override
static Extension_Code static_type()
Extension_Code type() const override
Server_Name_Indicator(std::string_view host_name)
Signature_Algorithms_Cert(std::vector< Signature_Scheme > schemes)
const std::vector< Signature_Scheme > & supported_schemes() const
Extension_Code type() const override
Signature_Algorithms(std::vector< Signature_Scheme > schemes)
const std::vector< Signature_Scheme > & supported_schemes() const
static Extension_Code static_type()
Extension_Code type() const override
bool empty() const override
Supported_Groups(const std::vector< Group_Params > &groups)
Extension_Code type() const override
static Extension_Code static_type()
static Extension_Code static_type()
Supported_Versions(Protocol_Version version, const Policy &policy)
const std::vector< Protocol_Version > & versions() const
bool empty() const override
Supported_Versions(Protocol_Version version)
Extension_Code type() const override
bool empty() const override
std::vector< uint8_t > serialize(Connection_Side whoami) const override
Unknown_Extension(Extension_Code type, TLS_Data_Reader &reader, uint16_t extension_size)
const std::vector< uint8_t > & value()
bool is_implemented() const override
Extension_Code type() const override
Group_Params Named_Group