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