Botan 3.4.0
Crypto and TLS for C&
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::vector< std::string > &protocols)
 
 Application_Layer_Protocol_Notification (std::string_view protocol)
 
 Application_Layer_Protocol_Notification (TLS_Data_Reader &reader, uint16_t extension_size, Connection_Side from)
 
bool empty () const override
 
virtual bool is_implemented () const
 
const std::vector< std::string > & protocols () const
 
std::vector< uint8_t > serialize (Connection_Side whoami) const override
 
std::string single_protocol () const
 
Extension_Code type () const override
 

Static Public Member Functions

static Extension_Code static_type ()
 

Detailed Description

ALPN (RFC 7301)

Definition at line 172 of file tls_extensions.h.

Constructor & Destructor Documentation

◆ Application_Layer_Protocol_Notification() [1/3]

Botan::TLS::Application_Layer_Protocol_Notification::Application_Layer_Protocol_Notification ( std::string_view protocol)
inlineexplicit

Single protocol, used by server

Definition at line 185 of file tls_extensions.h.

185 :
186 m_protocols(1, std::string(protocol)) {}

◆ Application_Layer_Protocol_Notification() [2/3]

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 191 of file tls_extensions.h.

191 :
192 m_protocols(protocols) {}
const std::vector< std::string > & protocols() const

◆ Application_Layer_Protocol_Notification() [3/3]

Botan::TLS::Application_Layer_Protocol_Notification::Application_Layer_Protocol_Notification ( TLS_Data_Reader & reader,
uint16_t extension_size,
Connection_Side from )

Definition at line 301 of file tls_extensions.cpp.

303 {
304 if(extension_size == 0) {
305 return; // empty extension
306 }
307
308 const uint16_t name_bytes = reader.get_uint16_t();
309
310 size_t bytes_remaining = extension_size - 2;
311
312 if(name_bytes != bytes_remaining) {
313 throw Decoding_Error("Bad encoding of ALPN extension, bad length field");
314 }
315
316 while(bytes_remaining) {
317 const std::string p = reader.get_string(1, 0, 255);
318
319 if(bytes_remaining < p.size() + 1) {
320 throw Decoding_Error("Bad encoding of ALPN, length field too long");
321 }
322
323 if(p.empty()) {
324 throw Decoding_Error("Empty ALPN protocol not allowed");
325 }
326
327 bytes_remaining -= (p.size() + 1);
328
329 m_protocols.push_back(p);
330 }
331
332 // RFC 7301 3.1
333 // The "extension_data" field of the [...] extension is structured the
334 // same as described above for the client "extension_data", except that
335 // the "ProtocolNameList" MUST contain exactly one "ProtocolName".
336 if(from == Connection_Side::Server && m_protocols.size() != 1) {
337 throw TLS_Exception(
338 Alert::DecodeError,
339 "Server sent " + std::to_string(m_protocols.size()) + " protocols in ALPN extension response");
340 }
341}

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

Member Function Documentation

◆ empty()

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 198 of file tls_extensions.h.

198{ return m_protocols.empty(); }

◆ is_implemented()

virtual bool Botan::TLS::Extension::is_implemented ( ) const
inlinevirtualinherited
Returns
true if this extension is known and implemented by Botan

Reimplemented in Botan::TLS::Unknown_Extension.

Definition at line 116 of file tls_extensions.h.

116{ return true; }

◆ protocols()

const std::vector< std::string > & Botan::TLS::Application_Layer_Protocol_Notification::protocols ( ) const
inline

Definition at line 178 of file tls_extensions.h.

178{ return m_protocols; }

◆ serialize()

std::vector< uint8_t > Botan::TLS::Application_Layer_Protocol_Notification::serialize ( Connection_Side whoami) const
overridevirtual
Returns
serialized binary for the extension

Implements Botan::TLS::Extension.

Definition at line 348 of file tls_extensions.cpp.

348 {
349 std::vector<uint8_t> buf(2);
350
351 for(auto&& p : m_protocols) {
352 if(p.length() >= 256) {
353 throw TLS_Exception(Alert::InternalError, "ALPN name too long");
354 }
355 if(!p.empty()) {
356 append_tls_length_value(buf, cast_char_ptr_to_uint8(p.data()), p.size(), 1);
357 }
358 }
359
360 buf[0] = get_byte<0>(static_cast<uint16_t>(buf.size() - 2));
361 buf[1] = get_byte<1>(static_cast<uint16_t>(buf.size() - 2));
362
363 return buf;
364}
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:180
const uint8_t * cast_char_ptr_to_uint8(const char *s)
Definition mem_ops.h:275

References Botan::TLS::append_tls_length_value(), and Botan::cast_char_ptr_to_uint8().

◆ single_protocol()

std::string Botan::TLS::Application_Layer_Protocol_Notification::single_protocol ( ) const

Definition at line 343 of file tls_extensions.cpp.

343 {
344 BOTAN_STATE_CHECK(m_protocols.size() == 1);
345 return m_protocols.front();
346}
#define BOTAN_STATE_CHECK(expr)
Definition assert.h:41

References BOTAN_STATE_CHECK.

◆ static_type()

static Extension_Code Botan::TLS::Application_Layer_Protocol_Notification::static_type ( )
inlinestatic

◆ type()

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

Implements Botan::TLS::Extension.

Definition at line 176 of file tls_extensions.h.

176{ return static_type(); }

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