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

#include <tls_extensions.h>

Inheritance diagram for Botan::TLS::Server_Name_Indicator:
Botan::TLS::Extension

Public Member Functions

bool empty () const override
std::string host_name () const
virtual bool is_implemented () const
std::vector< uint8_t > serialize (Connection_Side whoami) const override
 Server_Name_Indicator (std::string_view host_name)
 Server_Name_Indicator (TLS_Data_Reader &reader, uint16_t extension_size, Connection_Side from)
Extension_Code type () const override

Static Public Member Functions

static bool hostname_acceptable_for_sni (std::string_view hostname)
static Extension_Code static_type ()

Detailed Description

Server Name Indicator extension (RFC 3546)

Definition at line 121 of file tls_extensions.h.

Constructor & Destructor Documentation

◆ Server_Name_Indicator() [1/2]

Botan::TLS::Server_Name_Indicator::Server_Name_Indicator ( std::string_view host_name)
inlineexplicit

Definition at line 127 of file tls_extensions.h.

127: m_sni_host_name(host_name) {}

References host_name().

◆ Server_Name_Indicator() [2/2]

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

Decode a server_name extension. On the server side the host_name must be a valid DNS name (RFC 6066 3) or the handshake is rejected with an illegal_parameter alert.

Definition at line 357 of file tls_extensions.cpp.

357 {
358 /*
359 RFC 6066 Section 3
360
361 A server that receives a client hello containing the "server_name"
362 extension MAY use the information contained in the extension to guide
363 its selection of an appropriate certificate to return to the client,
364 and/or other aspects of security policy. In this event, the server
365 SHALL include an extension of type "server_name" in the (extended)
366 server hello. The "extension_data" field of this extension SHALL be
367 empty.
368 */
369 if(from == Connection_Side::Server) {
370 if(extension_size != 0) {
371 throw TLS_Exception(Alert::IllegalParameter, "Server sent non-empty SNI extension");
372 }
373 } else {
374 // Clients are required to send at least one name in the SNI
375 if(extension_size == 0) {
376 throw TLS_Exception(Alert::IllegalParameter, "Client sent empty SNI extension");
377 }
378
379 const uint16_t name_bytes = reader.get_uint16_t();
380
381 // RFC 6066 3: a ServerName carrying a host_name (the only NameType
382 // currently defined and the only one this implementation acts on)
383 // requires at least 1 byte name_type + 2 byte length + 1 byte HostName.
384 if(name_bytes + 2 != extension_size || name_bytes < 4) {
385 throw Decoding_Error("Bad encoding of SNI extension");
386 }
387
388 BOTAN_ASSERT_NOMSG(reader.remaining_bytes() == name_bytes);
389
390 std::vector<uint8_t> seen_name_types;
391 while(reader.has_remaining()) {
392 const uint8_t name_type = reader.get_byte();
393
394 /*
395 RFC 6066 Section 3
396 The ServerNameList MUST NOT contain more than one name of the same name_type.
397 */
398 if(value_exists(seen_name_types, name_type)) {
399 throw Decoding_Error("TLS ServerNameIndicator contains more than one name of the same type");
400 }
401 seen_name_types.push_back(name_type);
402
403 if(name_type == 0) {
404 const std::string host_name = reader.get_string(2, 1, 65535);
405
406 /*
407 RFC 6066 Section 3
408 The hostname is represented as a byte string using ASCII
409 encoding without a trailing dot. [...] Literal IPv4 and IPv6
410 addresses are not permitted in "HostName".
411 */
412 if(auto dns_name = DNSName::from_string(host_name)) {
413 m_sni_host_name = dns_name->to_string();
414 } else {
415 throw TLS_Exception(Alert::IllegalParameter,
416 "TLS ServerNameIndicator host_name is not a valid DNS name");
417 }
418 } else {
419 /*
420 Unknown name type - skip its length-prefixed value and continue
421
422 RFC 6066 Section 3
423 For backward compatibility, all future data structures associated
424 with new NameTypes MUST begin with a 16-bit length field.
425 */
426 const uint16_t unknown_name_len = reader.get_uint16_t();
427 reader.discard_next(unknown_name_len);
428 }
429 }
430 }
431}
#define BOTAN_ASSERT_NOMSG(expr)
Definition assert.h:82
static std::optional< DNSName > from_string(std::string_view name)
Definition dns_name.cpp:134
bool value_exists(const std::vector< T > &vec, const V &val)
Definition stl_util.h:44

References BOTAN_ASSERT_NOMSG, Botan::TLS::TLS_Data_Reader::discard_next(), Botan::DNSName::from_string(), Botan::TLS::TLS_Data_Reader::get_byte(), Botan::TLS::TLS_Data_Reader::get_string(), Botan::TLS::TLS_Data_Reader::get_uint16_t(), Botan::TLS::TLS_Data_Reader::has_remaining(), host_name(), Botan::TLS::TLS_Data_Reader::remaining_bytes(), Botan::TLS::Server, and Botan::value_exists().

Member Function Documentation

◆ empty()

bool Botan::TLS::Server_Name_Indicator::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 144 of file tls_extensions.h.

144{ return false; }

◆ host_name()

std::string Botan::TLS::Server_Name_Indicator::host_name ( ) const
inline

Return the host_name. When decoded from a ClientHello this is the canonical (lowercased) form of the name.

Definition at line 140 of file tls_extensions.h.

140{ return m_sni_host_name; }

Referenced by Server_Name_Indicator(), and Server_Name_Indicator().

◆ hostname_acceptable_for_sni()

bool Botan::TLS::Server_Name_Indicator::hostname_acceptable_for_sni ( std::string_view hostname)
static

Definition at line 465 of file tls_extensions.cpp.

465 {
466 // Avoid sending an IPv4/IPv6 address in SNI as this is prohibited
467
468 if(hostname.empty() || hostname.size() > 255) {
469 return false;
470 }
471
472 if(const auto ipv4 = IPv4Address::from_string(hostname)) {
473 return false;
474 }
475
476 if(const auto ipv6 = IPv6Address::from_string(hostname)) {
477 return false;
478 }
479
480 if(const auto dns = DNSName::from_string(hostname)) {
481 return true;
482 } else {
483 return false;
484 }
485}
static std::optional< IPv4Address > from_string(std::string_view str)
static std::optional< IPv6Address > from_string(std::string_view str)

References Botan::DNSName::from_string(), Botan::IPv4Address::from_string(), and Botan::IPv6Address::from_string().

Referenced by Botan::TLS::Client_Hello_12::Client_Hello_12(), Botan::TLS::Client_Hello_12::Client_Hello_12(), and Botan::TLS::Client_Hello_13::Client_Hello_13().

◆ 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; }

◆ serialize()

std::vector< uint8_t > Botan::TLS::Server_Name_Indicator::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 433 of file tls_extensions.cpp.

433 {
434 // RFC 6066
435 // [...] the server SHALL include an extension of type "server_name" in
436 // the (extended) server hello. The "extension_data" field of this
437 // extension SHALL be empty.
438 if(whoami == Connection_Side::Server) {
439 return {};
440 }
441
442 std::vector<uint8_t> buf;
443
444 const size_t name_len = m_sni_host_name.size();
445
446 // RFC 6066 3: HostName<1..2^16-1>; the outer ServerNameList wraps a
447 // 1-byte name_type and a 2-byte length so the whole entry must fit in
448 // a uint16_t too.
449 if(name_len + 3 > 0xFFFF) {
450 throw Encoding_Error("TLS server name is too long to encode");
451 }
452
453 buf.push_back(get_byte<0>(static_cast<uint16_t>(name_len + 3)));
454 buf.push_back(get_byte<1>(static_cast<uint16_t>(name_len + 3)));
455 buf.push_back(0); // DNS
456
457 buf.push_back(get_byte<0>(static_cast<uint16_t>(name_len)));
458 buf.push_back(get_byte<1>(static_cast<uint16_t>(name_len)));
459
460 buf += as_span_of_bytes(m_sni_host_name);
461
462 return buf;
463}
constexpr uint8_t get_byte(T input)
Definition loadstor.h:79
std::span< const uint8_t > as_span_of_bytes(const char *s, size_t len)
Definition mem_utils.h:59

References Botan::as_span_of_bytes(), Botan::get_byte(), and Botan::TLS::Server.

◆ static_type()

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

Definition at line 123 of file tls_extensions.h.

References Botan::TLS::ServerNameIndication.

Referenced by type().

◆ type()

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

Return TLS extension code

Returns
code number of the extension

Implements Botan::TLS::Extension.

Definition at line 125 of file tls_extensions.h.

125{ return static_type(); }
static Extension_Code static_type()

References static_type().


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