Botan 3.14.1
Crypto and TLS for C&
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 (std::string_view protocol)
 Application_Layer_Protocol_Notification (std::vector< std::string > protocols)
 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 155 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)
explicit

Single protocol, used by server

Definition at line 487 of file tls_extensions.cpp.

487 {
488 BOTAN_ARG_CHECK(!protocol.empty(), "ALPN protocol name must not be empty");
489 BOTAN_ARG_CHECK(protocol.size() < 256, "ALPN protocol name too long");
490 m_protocols.emplace_back(protocol);
491}
#define BOTAN_ARG_CHECK(expr, msg)
Definition assert.h:33

References BOTAN_ARG_CHECK.

◆ Application_Layer_Protocol_Notification() [2/3]

Botan::TLS::Application_Layer_Protocol_Notification::Application_Layer_Protocol_Notification ( std::vector< std::string > protocols)
explicit

List of protocols, used by client

Definition at line 493 of file tls_extensions.cpp.

493 :
494 m_protocols(std::move(protocols)) {
495 for(const auto& protocol : m_protocols) {
496 BOTAN_ARG_CHECK(!protocol.empty(), "ALPN protocol name must not be empty");
497 BOTAN_ARG_CHECK(protocol.size() < 256, "ALPN protocol name too long");
498 }
499}
const std::vector< std::string > & protocols() const

References BOTAN_ARG_CHECK, and protocols().

◆ 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 501 of file tls_extensions.cpp.

503 {
504 if(extension_size < 2) {
505 throw Decoding_Error("ALPN extension cannot be empty");
506 }
507
508 const uint16_t name_bytes = reader.get_uint16_t();
509
510 size_t bytes_remaining = extension_size - 2;
511
512 if(name_bytes != bytes_remaining) {
513 throw Decoding_Error("Bad encoding of ALPN extension, bad length field");
514 }
515
516 // RFC 7301 3.1: ProtocolName protocol_name_list<2..2^16-1>
517 if(name_bytes == 0) {
518 throw Decoding_Error("Empty ALPN protocol_name_list not allowed");
519 }
520
521 while(bytes_remaining > 0) {
522 const std::string p = reader.get_string(1, 0, 255);
523
524 if(bytes_remaining < p.size() + 1) {
525 throw Decoding_Error("Bad encoding of ALPN, length field too long");
526 }
527
528 if(p.empty()) {
529 throw Decoding_Error("Empty ALPN protocol not allowed");
530 }
531
532 bytes_remaining -= (p.size() + 1);
533
534 m_protocols.push_back(p);
535 }
536
537 // RFC 7301 3.1
538 // The "extension_data" field of the [...] extension is structured the
539 // same as described above for the client "extension_data", except that
540 // the "ProtocolNameList" MUST contain exactly one "ProtocolName".
541 if(from == Connection_Side::Server && m_protocols.size() != 1) {
542 throw TLS_Exception(
543 Alert::DecodeError,
544 "Server sent " + std::to_string(m_protocols.size()) + " protocols in ALPN extension response");
545 }
546}

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

Predicate if a TLS extension should be included or not

Returns
true if this extension should be encoded, otherwise false

Implements Botan::TLS::Extension.

Definition at line 179 of file tls_extensions.h.

179{ return m_protocols.empty(); }

◆ is_implemented()

virtual bool Botan::TLS::Extension::is_implemented ( ) const
inlinevirtualinherited

Predicate if the extension is known/implemented

Note
this exists primarily to support unknown extension handling and doesn't need to be overridden even in the custom extension case.
Returns
true if this extension is known

Reimplemented in Botan::TLS::Unknown_Extension.

Definition at line 113 of file tls_extensions.h.

113{ return true; }

◆ protocols()

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

Definition at line 161 of file tls_extensions.h.

161{ return m_protocols; }

Referenced by Application_Layer_Protocol_Notification().

◆ serialize()

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

Serialize a TLS extension

Parameters
whoamiwhich peer we are acting as in the protocol
Returns
serialized binary for the extension

Implements Botan::TLS::Extension.

Definition at line 553 of file tls_extensions.cpp.

553 {
554 std::vector<uint8_t> buf(2);
555
556 for(auto&& proto : m_protocols) {
557 if(proto.length() >= 256) {
558 throw TLS_Exception(Alert::InternalError, "ALPN name too long");
559 }
560 if(!proto.empty()) {
561 append_tls_length_value(buf, proto, 1);
562 }
563 }
564
565 // RFC 7301 3.1: ProtocolName protocol_name_list<2..2^16-1>;
566 if(buf.size() - 2 > 0xFFFF) {
567 throw Encoding_Error("TLS ALPN protocol list is too long to encode");
568 }
569 buf[0] = get_byte<0>(static_cast<uint16_t>(buf.size() - 2));
570 buf[1] = get_byte<1>(static_cast<uint16_t>(buf.size() - 2));
571
572 return buf;
573}
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:182
constexpr uint8_t get_byte(T input)
Definition loadstor.h:79

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

◆ single_protocol()

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

Definition at line 548 of file tls_extensions.cpp.

548 {
549 BOTAN_STATE_CHECK(m_protocols.size() == 1);
550 return m_protocols.front();
551}
#define BOTAN_STATE_CHECK(expr)
Definition assert.h:49

References BOTAN_STATE_CHECK.

◆ static_type()

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

◆ type()

Extension_Code Botan::TLS::Application_Layer_Protocol_Notification::type ( ) const
inlineoverridevirtual

Return TLS extension code

Returns
code number of the extension

Implements Botan::TLS::Extension.

Definition at line 159 of file tls_extensions.h.

159{ return static_type(); }

References static_type().


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