Botan 3.14.1
Crypto and TLS for C&
Botan::TLS::Extensions Class Referencefinal

#include <tls_extensions.h>

Public Member Functions

void add (Extension *extn)
void add (std::unique_ptr< Extension > extn)
bool contains_implemented_extensions_other_than (const std::set< Extension_Code > &allowed_extensions) const
bool contains_other_than (const std::set< Extension_Code > &allowed_extensions, bool allow_unknown_extensions=false) const
void deserialize (TLS_Data_Reader &reader, Connection_Side from, Handshake_Type message_type)
bool empty () const
std::optional< std::vector< uint8_t > > extension_raw_bytes (Extension_Code type) const
std::set< Extension_Code > extension_types () const
 Extensions ()=default
 Extensions (const Extensions &)=delete
 Extensions (Extensions &&)=default
 Extensions (TLS_Data_Reader &reader, Connection_Side side, Handshake_Type message_type)
template<typename T>
T * get () const
Extension * get (Extension_Code type) const
template<typename T>
bool has () const
bool has (Extension_Code type) const
std::optional< Extension_Code > last_added () const
Extensions & operator= (const Extensions &)=delete
Extensions & operator= (Extensions &&)=default
bool remove_extension (Extension_Code type)
void reorder (std::span< const Extension_Code > order)
std::vector< uint8_t > serialize (Connection_Side whoami) const
size_t size () const
 ~Extensions ()

Detailed Description

Represents a block of extensions in a hello message

Definition at line 484 of file tls_extensions.h.

Constructor & Destructor Documentation

◆ Extensions() [1/4]

Botan::TLS::Extensions::Extensions ( )
default

◆ Extensions() [2/4]

Botan::TLS::Extensions::Extensions ( const Extensions & )
delete

References Extensions().

◆ Extensions() [3/4]

Botan::TLS::Extensions::Extensions ( Extensions && )
default

References Extensions().

◆ ~Extensions()

Botan::TLS::Extensions::~Extensions ( )
default

◆ Extensions() [4/4]

Botan::TLS::Extensions::Extensions ( TLS_Data_Reader & reader,
Connection_Side side,
Handshake_Type message_type )
inline

Definition at line 566 of file tls_extensions.h.

566 {
567 deserialize(reader, side, message_type);
568 }
void deserialize(TLS_Data_Reader &reader, Connection_Side from, Handshake_Type message_type)

References deserialize().

Member Function Documentation

◆ add() [1/2]

void Botan::TLS::Extensions::add ( Extension * extn)
inline

Definition at line 506 of file tls_extensions.h.

506{ add(std::unique_ptr<Extension>(extn)); }
void add(std::unique_ptr< Extension > extn)

References add().

Referenced by add().

◆ add() [2/2]

void Botan::TLS::Extensions::add ( std::unique_ptr< Extension > extn)

Definition at line 189 of file tls_extensions.cpp.

189 {
190 const auto type = extn->type();
191 if(has(type)) {
192 throw Invalid_Argument("cannot add the same extension twice: " + std::to_string(static_cast<uint16_t>(type)));
193 }
194
195 m_extension_codes.push_back(type);
196 m_extensions.emplace(type, std::move(extn));
197}

References has(), and Botan::TLS::Extension::type().

◆ contains_implemented_extensions_other_than()

bool Botan::TLS::Extensions::contains_implemented_extensions_other_than ( const std::set< Extension_Code > & allowed_extensions) const
inline
Parameters
allowed_extensionsextension types that are allowed
Returns
true if this contains any extensions implemented by Botan that are not contained in allowed_extensions.

Definition at line 527 of file tls_extensions.h.

527 {
528 return contains_other_than(allowed_extensions, true);
529 }
bool contains_other_than(const std::set< Extension_Code > &allowed_extensions, bool allow_unknown_extensions=false) const

References contains_other_than().

◆ contains_other_than()

bool Botan::TLS::Extensions::contains_other_than ( const std::set< Extension_Code > & allowed_extensions,
bool allow_unknown_extensions = false ) const
Parameters
allowed_extensionsextension types that are allowed
allow_unknown_extensionsif true, ignores unrecognized extensions
Returns
true if this contains any extensions that are not contained in allowed_extensions.

Definition at line 228 of file tls_extensions.cpp.

229 {
230 const auto found = extension_types();
231
232 std::vector<Extension_Code> diff;
233 std::set_difference(
234 found.cbegin(), found.end(), allowed_extensions.cbegin(), allowed_extensions.cend(), std::back_inserter(diff));
235
236 if(allow_unknown_extensions) {
237 // Go through the found unexpected extensions whether any of those
238 // is known to this TLS implementation.
239 const auto itr = std::find_if(diff.cbegin(), diff.cend(), [this](const auto ext_type) {
240 const auto ext = get(ext_type);
241 return ext && ext->is_implemented();
242 });
243
244 // ... if yes, `contains_other_than` is true
245 return itr != diff.cend();
246 }
247
248 return !diff.empty();
249}
std::set< Extension_Code > extension_types() const

References extension_types().

Referenced by contains_implemented_extensions_other_than().

◆ deserialize()

void Botan::TLS::Extensions::deserialize ( TLS_Data_Reader & reader,
Connection_Side from,
Handshake_Type message_type )

Definition at line 199 of file tls_extensions.cpp.

199 {
200 if(reader.has_remaining()) {
201 const uint16_t all_extn_size = reader.get_uint16_t();
202
203 if(reader.remaining_bytes() != all_extn_size) {
204 throw Decoding_Error("Bad extension size");
205 }
206
207 while(reader.has_remaining()) {
208 const uint16_t extension_code = reader.get_uint16_t();
209 const uint16_t extension_size = reader.get_uint16_t();
210
211 const auto type = static_cast<Extension_Code>(extension_code);
212
213 if(this->has(type)) {
214 throw TLS_Exception(TLS::Alert::DecodeError, "Peer sent duplicated extensions");
215 }
216
217 // TODO offer a function on reader that returns a byte range as a reference
218 // to avoid this copy of the extension data
219 const std::vector<uint8_t> extn_data = reader.get_fixed<uint8_t>(extension_size);
220 m_raw_extension_data[type] = extn_data;
221 TLS_Data_Reader extn_reader("Extension", extn_data);
222 this->add(make_extension(extn_reader, type, from, message_type));
223 extn_reader.assert_done();
224 }
225 }
226}

Referenced by Extensions().

◆ empty()

bool Botan::TLS::Extensions::empty ( ) const
inline

Definition at line 502 of file tls_extensions.h.

502{ return m_extensions.empty(); }

◆ extension_raw_bytes()

std::optional< std::vector< uint8_t > > Botan::TLS::Extensions::extension_raw_bytes ( Extension_Code type) const
inline
Returns
the raw bytes of the extension with the given type as they appeared on the wire during deserialization, or std::nullopt if the extension was not present or was added programmatically.

Definition at line 575 of file tls_extensions.h.

575 {
576 const auto it = m_raw_extension_data.find(type);
577 if(it != m_raw_extension_data.end()) {
578 return it->second;
579 }
580 return std::nullopt;
581 }

Referenced by Botan::TLS::Client_Hello_13::validate_updates().

◆ extension_types()

std::set< Extension_Code > Botan::TLS::Extensions::extension_types ( ) const

Definition at line 310 of file tls_extensions.cpp.

310 {
311 std::set<Extension_Code> offers;
312 for(const auto& [extn_type, extn] : m_extensions) {
313 // Consistent with serialize(): empty extensions are not placed on
314 // the wire so they must not appear in the "offered" set either. An
315 // extension received from the peer was on the wire, whatever it holds.
316 if(!extn->empty() || m_raw_extension_data.contains(extn_type)) {
317 offers.insert(extn_type);
318 }
319 }
320 return offers;
321}

Referenced by contains_other_than().

◆ get() [1/2]

template<typename T>
T * Botan::TLS::Extensions::get ( ) const
inline

◆ get() [2/2]

Extension * Botan::TLS::Extensions::get ( Extension_Code type) const

Definition at line 179 of file tls_extensions.cpp.

179 {
180 const auto i = m_extensions.find(type);
181
182 if(i == m_extensions.end()) {
183 return nullptr;
184 } else {
185 return i->second.get();
186 }
187}

◆ has() [1/2]

template<typename T>
bool Botan::TLS::Extensions::has ( ) const
inline

◆ has() [2/2]

bool Botan::TLS::Extensions::has ( Extension_Code type) const

Definition at line 175 of file tls_extensions.cpp.

175 {
176 return m_extensions.contains(type);
177}

◆ last_added()

std::optional< Extension_Code > Botan::TLS::Extensions::last_added ( ) const
inline

Return the code of the extension that appears last in the encoding This is used for checking the position of PSK extension in TLS 1.3

Definition at line 551 of file tls_extensions.h.

551 {
552 if(m_extension_codes.empty()) {
553 return {};
554 } else {
555 return m_extension_codes.back();
556 }
557 }

◆ operator=() [1/2]

Extensions & Botan::TLS::Extensions::operator= ( const Extensions & )
delete

References Extensions().

◆ operator=() [2/2]

Extensions & Botan::TLS::Extensions::operator= ( Extensions && )
default

References Extensions().

◆ remove_extension()

bool Botan::TLS::Extensions::remove_extension ( Extension_Code type)

Remove an extension from this extensions object, if it exists. Returns true if the extension existed (and thus is now removed), otherwise false (the extension wasn't set in the first place).

Note: not used internally, might be used in Callbacks::tls_modify_extensions()

Definition at line 251 of file tls_extensions.cpp.

251 {
252 const auto i = m_extensions.find(type);
253
254 if(i == m_extensions.end()) {
255 return false;
256 } else {
257 m_extensions.erase(i);
258 std::erase(m_extension_codes, type);
259 m_raw_extension_data.erase(type);
260 return true;
261 }
262}

◆ reorder()

void Botan::TLS::Extensions::reorder ( std::span< const Extension_Code > order)

Reorder extensions for serialization. Extensions not mentioned in order retain their relative position at the front; extensions in order are appended in the given order.

Definition at line 323 of file tls_extensions.cpp.

323 {
324 const std::set<Extension_Code> in_order(order.begin(), order.end());
325
326 std::vector<Extension_Code> new_codes;
327 new_codes.reserve(m_extension_codes.size());
328
329 // First: extensions not mentioned in the order (preserving their relative order)
330 for(const auto code : m_extension_codes) {
331 if(!in_order.contains(code)) {
332 new_codes.push_back(code);
333 }
334 }
335
336 // Then: extensions in the specified order. Deduplicate so a caller that
337 // accidentally lists the same code twice doesn't cause it to be
338 // serialized twice (which would also break peers that reject duplicate
339 // extension codes per RFC 8446 4.2 / RFC 5246 7.4.1.4).
340 std::unordered_set<Extension_Code> already_pushed;
341 for(const auto code : order) {
342 if(m_extensions.contains(code) && already_pushed.insert(code).second) {
343 new_codes.push_back(code);
344 }
345 }
346
347 m_extension_codes = std::move(new_codes);
348}

◆ serialize()

std::vector< uint8_t > Botan::TLS::Extensions::serialize ( Connection_Side whoami) const

Definition at line 264 of file tls_extensions.cpp.

264 {
265 std::vector<uint8_t> buf(2); // 2 bytes for length field
266
267 // Serialize in the order extensions were added, which matters for TLS 1.3
268 for(const auto extn_type : m_extension_codes) {
269 const auto& extn = m_extensions.at(extn_type);
270
271 if(extn->empty()) {
272 continue;
273 }
274
275 const uint16_t extn_code = static_cast<uint16_t>(extn_type);
276
277 const std::vector<uint8_t> extn_val = extn->serialize(whoami);
278
279 // Each extension carries a uint16 length prefix.
280 if(extn_val.size() > 0xFFFF) {
281 throw Encoding_Error("TLS extension is too large to encode");
282 }
283
284 buf.push_back(get_byte<0>(extn_code));
285 buf.push_back(get_byte<1>(extn_code));
286
287 buf.push_back(get_byte<0>(static_cast<uint16_t>(extn_val.size())));
288 buf.push_back(get_byte<1>(static_cast<uint16_t>(extn_val.size())));
289
290 buf += extn_val;
291 }
292
293 // The outer extensions block is itself uint16-length-prefixed.
294 if(buf.size() - 2 > 0xFFFF) {
295 throw Encoding_Error("TLS extensions are too large to encode");
296 }
297 const uint16_t extn_size = static_cast<uint16_t>(buf.size() - 2);
298
299 buf[0] = get_byte<0>(extn_size);
300 buf[1] = get_byte<1>(extn_size);
301
302 // avoid sending a completely empty extensions block
303 if(buf.size() == 2) {
304 return std::vector<uint8_t>();
305 }
306
307 return buf;
308}
constexpr uint8_t get_byte(T input)
Definition loadstor.h:79

References Botan::get_byte().

◆ size()

size_t Botan::TLS::Extensions::size ( ) const
inline

Definition at line 500 of file tls_extensions.h.

500{ return m_extensions.size(); }

The documentation for this class was generated from the following files: