Botan  2.1.0
Crypto and TLS for C++11
Public Member Functions | Static Public Member Functions | List of all members
Botan::TLS::Application_Layer_Protocol_Notification Class Referencefinal

#include <tls_extensions.h>

Inheritance diagram for Botan::TLS::Application_Layer_Protocol_Notification:
Botan::TLS::Extension

Public Member Functions

 Application_Layer_Protocol_Notification (const std::string &protocol)
 
 Application_Layer_Protocol_Notification (const std::vector< std::string > &protocols)
 
 Application_Layer_Protocol_Notification (TLS_Data_Reader &reader, uint16_t extension_size)
 
bool empty () const override
 
const std::vector< std::string > & protocols () const
 
std::vector< uint8_t > serialize () const override
 
const std::string & single_protocol () const
 
Handshake_Extension_Type type () const override
 

Static Public Member Functions

static Handshake_Extension_Type static_type ()
 

Detailed Description

ALPN (RFC 7301)

Definition at line 157 of file tls_extensions.h.

Constructor & Destructor Documentation

Botan::TLS::Application_Layer_Protocol_Notification::Application_Layer_Protocol_Notification ( const std::string &  protocol)
inlineexplicit

Single protocol, used by server

Definition at line 171 of file tls_extensions.h.

171  :
172  m_protocols(1, protocol) {}
Botan::TLS::Application_Layer_Protocol_Notification::Application_Layer_Protocol_Notification ( const std::vector< std::string > &  protocols)
inlineexplicit

List of protocols, used by client

Definition at line 177 of file tls_extensions.h.

177  :
178  m_protocols(protocols) {}
const std::vector< std::string > & protocols() const
Botan::TLS::Application_Layer_Protocol_Notification::Application_Layer_Protocol_Notification ( TLS_Data_Reader reader,
uint16_t  extension_size 
)

Definition at line 224 of file tls_extensions.cpp.

References Botan::TLS::TLS_Data_Reader::get_string(), and Botan::TLS::TLS_Data_Reader::get_uint16_t().

226  {
227  if(extension_size == 0)
228  return; // empty extension
229 
230  const uint16_t name_bytes = reader.get_uint16_t();
231 
232  size_t bytes_remaining = extension_size - 2;
233 
234  if(name_bytes != bytes_remaining)
235  throw Decoding_Error("Bad encoding of ALPN extension, bad length field");
236 
237  while(bytes_remaining)
238  {
239  const std::string p = reader.get_string(1, 0, 255);
240 
241  if(bytes_remaining < p.size() + 1)
242  throw Decoding_Error("Bad encoding of ALPN, length field too long");
243 
244  bytes_remaining -= (p.size() + 1);
245 
246  m_protocols.push_back(p);
247  }
248  }

Member Function Documentation

bool Botan::TLS::Application_Layer_Protocol_Notification::empty ( ) const
inlineoverridevirtual
Returns
if we should encode this extension or not

Implements Botan::TLS::Extension.

Definition at line 185 of file tls_extensions.h.

185 { return m_protocols.empty(); }
const std::vector<std::string>& Botan::TLS::Application_Layer_Protocol_Notification::protocols ( ) const
inline

Definition at line 164 of file tls_extensions.h.

164 { return m_protocols; }
std::vector< uint8_t > Botan::TLS::Application_Layer_Protocol_Notification::serialize ( ) const
overridevirtual
Returns
serialized binary for the extension

Implements Botan::TLS::Extension.

Definition at line 259 of file tls_extensions.cpp.

References Botan::TLS::append_tls_length_value(), Botan::get_byte(), and Botan::TLS::Alert::INTERNAL_ERROR.

260  {
261  std::vector<uint8_t> buf(2);
262 
263  for(auto&& p: m_protocols)
264  {
265  if(p.length() >= 256)
266  throw TLS_Exception(Alert::INTERNAL_ERROR, "ALPN name too long");
267  if(p != "")
269  reinterpret_cast<const uint8_t*>(p.data()),
270  p.size(),
271  1);
272  }
273 
274  buf[0] = get_byte(0, static_cast<uint16_t>(buf.size()-2));
275  buf[1] = get_byte(1, static_cast<uint16_t>(buf.size()-2));
276 
277  return buf;
278  }
uint8_t get_byte(size_t byte_num, T input)
Definition: loadstor.h:47
void append_tls_length_value(std::vector< uint8_t, Alloc > &buf, const T *vals, size_t vals_size, size_t tag_size)
Definition: tls_reader.h:185
const std::string & Botan::TLS::Application_Layer_Protocol_Notification::single_protocol ( ) const

Definition at line 250 of file tls_extensions.cpp.

References Botan::TLS::Alert::HANDSHAKE_FAILURE, and Botan::ASN1::to_string().

251  {
252  if(m_protocols.size() != 1)
253  throw TLS_Exception(Alert::HANDSHAKE_FAILURE,
254  "Server sent " + std::to_string(m_protocols.size()) +
255  " protocols in ALPN extension response");
256  return m_protocols[0];
257  }
std::string to_string(const BER_Object &obj)
Definition: asn1_obj.cpp:47
static Handshake_Extension_Type Botan::TLS::Application_Layer_Protocol_Notification::static_type ( )
inlinestatic

Definition at line 160 of file tls_extensions.h.

References Botan::TLS::TLSEXT_ALPN.

Referenced by type().

Handshake_Extension_Type Botan::TLS::Application_Layer_Protocol_Notification::type ( ) const
inlineoverridevirtual
Returns
code number of the extension

Implements Botan::TLS::Extension.

Definition at line 162 of file tls_extensions.h.

References static_type().

162 { return static_type(); }
static Handshake_Extension_Type static_type()

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