Botan 2.19.1
Crypto and TLS for C&
Public Member Functions | List of all members
Botan::TLS::Extensions Class Referencefinal

#include <tls_extensions.h>

Public Member Functions

void add (Extension *extn)
 
void deserialize (TLS_Data_Reader &reader, Connection_Side from)
 
std::set< Handshake_Extension_Typeextension_types () const
 
 Extensions ()=default
 
 Extensions (TLS_Data_Reader &reader, Connection_Side side)
 
template<typename T >
Tget () const
 
Extensionget (Handshake_Extension_Type type) const
 
template<typename T >
bool has () const
 
bool remove_extension (Handshake_Extension_Type typ)
 
std::vector< uint8_t > serialize (Connection_Side whoami) const
 

Detailed Description

Represents a block of extensions in a hello message

Definition at line 491 of file tls_extensions.h.

Constructor & Destructor Documentation

◆ Extensions() [1/2]

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

◆ Extensions() [2/2]

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

Definition at line 535 of file tls_extensions.h.

536 {
537 deserialize(reader, side);
538 }
void deserialize(TLS_Data_Reader &reader, Connection_Side from)

Member Function Documentation

◆ add()

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

Definition at line 508 of file tls_extensions.h.

509 {
510 m_extensions[extn->type()].reset(extn);
511 }

References Botan::TLS::Extension::type().

Referenced by Botan::TLS::Client_Hello::Client_Hello(), deserialize(), and Botan::TLS::Server_Hello::Server_Hello().

◆ deserialize()

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

Definition at line 72 of file tls_extensions.cpp.

73 {
74 if(reader.has_remaining())
75 {
76 const uint16_t all_extn_size = reader.get_uint16_t();
77
78 if(reader.remaining_bytes() != all_extn_size)
79 throw Decoding_Error("Bad extension size");
80
81 while(reader.has_remaining())
82 {
83 const uint16_t extension_code = reader.get_uint16_t();
84 const uint16_t extension_size = reader.get_uint16_t();
85
86 const auto type = static_cast<Handshake_Extension_Type>(extension_code);
87
88 if(m_extensions.find(type) != m_extensions.end())
89 throw TLS_Exception(TLS::Alert::DECODE_ERROR,
90 "Peer sent duplicated extensions");
91
92 Extension* extn = make_extension(
93 reader, extension_code, extension_size, from);
94
95 this->add(extn);
96 }
97 }
98 }
void add(Extension *extn)
MechanismType type

References add(), Botan::TLS::Alert::DECODE_ERROR, Botan::TLS::TLS_Data_Reader::get_uint16_t(), Botan::TLS::TLS_Data_Reader::has_remaining(), Botan::TLS::TLS_Data_Reader::remaining_bytes(), and type.

Referenced by Botan::TLS::Client_Hello::Client_Hello(), and Botan::TLS::Server_Hello::Server_Hello().

◆ extension_types()

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

Definition at line 143 of file tls_extensions.cpp.

144 {
145 std::set<Handshake_Extension_Type> offers;
146 for(auto i = m_extensions.begin(); i != m_extensions.end(); ++i)
147 offers.insert(i->first);
148 return offers;
149 }

◆ get() [1/2]

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

◆ get() [2/2]

Extension * Botan::TLS::Extensions::get ( Handshake_Extension_Type  type) const
inline

Definition at line 513 of file tls_extensions.h.

514 {
515 auto i = m_extensions.find(type);
516
517 if(i != m_extensions.end())
518 return i->second.get();
519 return nullptr;
520 }

References type.

◆ has()

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

◆ remove_extension()

bool Botan::TLS::Extensions::remove_extension ( Handshake_Extension_Type  typ)

Remvoe 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).

Definition at line 134 of file tls_extensions.cpp.

135 {
136 auto i = m_extensions.find(typ);
137 if(i == m_extensions.end())
138 return false;
139 m_extensions.erase(i);
140 return true;
141 }

◆ serialize()

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

Definition at line 100 of file tls_extensions.cpp.

101 {
102 std::vector<uint8_t> buf(2); // 2 bytes for length field
103
104 for(auto& extn : m_extensions)
105 {
106 if(extn.second->empty())
107 continue;
108
109 const uint16_t extn_code = static_cast<uint16_t>(extn.second->type());
110
111 const std::vector<uint8_t> extn_val = extn.second->serialize(whoami);
112
113 buf.push_back(get_byte(0, extn_code));
114 buf.push_back(get_byte(1, extn_code));
115
116 buf.push_back(get_byte(0, static_cast<uint16_t>(extn_val.size())));
117 buf.push_back(get_byte(1, static_cast<uint16_t>(extn_val.size())));
118
119 buf += extn_val;
120 }
121
122 const uint16_t extn_size = static_cast<uint16_t>(buf.size() - 2);
123
124 buf[0] = get_byte(0, extn_size);
125 buf[1] = get_byte(1, extn_size);
126
127 // avoid sending a completely empty extensions block
128 if(buf.size() == 2)
129 return std::vector<uint8_t>();
130
131 return buf;
132 }
constexpr uint8_t get_byte(size_t byte_num, T input)
Definition: loadstor.h:41

References Botan::get_byte().


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