13#include <botan/tls_extensions.h>
15#include <botan/dns_name.h>
16#include <botan/ipv4_address.h>
17#include <botan/ipv6_address.h>
18#include <botan/tls_exceptn.h>
19#include <botan/tls_policy.h>
20#include <botan/internal/fmt.h>
21#include <botan/internal/parsing.h>
22#include <botan/internal/stl_util.h>
23#include <botan/internal/tls_reader.h>
25#include <unordered_set>
27#if defined(BOTAN_HAS_TLS_13)
28 #include <botan/tls_extensions_13.h>
31#if defined(BOTAN_HAS_TLS_12)
32 #include <botan/tls_extensions_12.h>
45 const uint16_t size =
static_cast<uint16_t
>(reader.remaining_bytes());
48 return std::make_unique<Server_Name_Indicator>(reader, size, from);
51 return std::make_unique<Supported_Groups>(reader, size);
54 return std::make_unique<Certificate_Status_Request>(reader, size, message_type, from);
57 return std::make_unique<Signature_Algorithms>(reader, size);
60 return std::make_unique<Signature_Algorithms_Cert>(reader, size);
63 return std::make_unique<SRTP_Protection_Profiles>(reader, size);
66 return std::make_unique<Application_Layer_Protocol_Notification>(reader, size, from);
69 return std::make_unique<Client_Certificate_Type>(reader, size, from);
72 return std::make_unique<Server_Certificate_Type>(reader, size, from);
75 return std::make_unique<Record_Size_Limit>(reader, size, from);
78 return std::make_unique<Supported_Versions>(reader, size, from);
83#if defined(BOTAN_HAS_TLS_12)
85 return std::make_unique<Supported_Point_Formats>(reader, size);
88 return std::make_unique<Renegotiation_Extension>(reader, size);
91 return std::make_unique<Extended_Master_Secret>(reader, size);
94 return std::make_unique<Encrypt_then_MAC>(reader, size);
97 return std::make_unique<Session_Ticket_Extension>(reader, size, from);
107#if defined(BOTAN_HAS_TLS_13)
109 return std::make_unique<PSK>(reader, size, message_type);
112 return std::make_unique<EarlyDataIndication>(reader, size, message_type);
115 return std::make_unique<Cookie>(reader, size);
118 return std::make_unique<PSK_Key_Exchange_Modes>(reader, size);
121 return std::make_unique<Certificate_Authorities>(reader, size);
124 return std::make_unique<Key_Share>(reader, size, message_type);
136 return std::make_unique<Unknown_Extension>(code, reader, size);
144 return m_extensions.contains(type);
148 const auto i = m_extensions.find(type);
150 if(i == m_extensions.end()) {
153 return i->second.get();
158 const auto type = extn->
type();
160 throw Invalid_Argument(
"cannot add the same extension twice: " + std::to_string(
static_cast<uint16_t
>(type)));
163 m_extension_codes.push_back(type);
164 m_extensions.emplace(type, std::move(extn));
181 if(this->
has(type)) {
182 throw TLS_Exception(TLS::Alert::DecodeError,
"Peer sent duplicated extensions");
187 const std::vector<uint8_t> extn_data = reader.
get_fixed<uint8_t>(extension_size);
188 m_raw_extension_data[
type] = extn_data;
190 this->
add(make_extension(extn_reader,
type, from, message_type));
191 extn_reader.assert_done();
197 const bool allow_unknown_extensions)
const {
200 std::vector<Extension_Code> diff;
202 found.cbegin(), found.end(), allowed_extensions.cbegin(), allowed_extensions.cend(), std::back_inserter(diff));
204 if(allow_unknown_extensions) {
207 const auto itr = std::find_if(diff.cbegin(), diff.cend(), [
this](
const auto ext_type) {
208 const auto ext = get(ext_type);
209 return ext && ext->is_implemented();
213 return itr != diff.cend();
216 return !diff.empty();
220 auto i = m_extensions.find(type);
222 if(i == m_extensions.end()) {
225 m_extensions.erase(i);
226 std::erase(m_extension_codes, type);
227 m_raw_extension_data.erase(type);
233 std::vector<uint8_t> buf(2);
236 for(
const auto extn_type : m_extension_codes) {
237 const auto& extn = m_extensions.at(extn_type);
243 const uint16_t extn_code =
static_cast<uint16_t
>(extn_type);
245 const std::vector<uint8_t> extn_val = extn->serialize(whoami);
253 buf.push_back(
get_byte<0>(
static_cast<uint16_t
>(extn_val.size())));
254 buf.push_back(
get_byte<1>(
static_cast<uint16_t
>(extn_val.size())));
261 const uint16_t extn_size =
static_cast<uint16_t
>(buf.size() - 2);
267 if(buf.size() == 2) {
268 return std::vector<uint8_t>();
275 std::set<Extension_Code> offers;
276 for(
const auto& [extn_type, extn] : m_extensions) {
280 offers.insert(extn_type);
287 const std::set<Extension_Code> in_order(order.begin(), order.end());
289 std::vector<Extension_Code> new_codes;
290 new_codes.reserve(m_extension_codes.size());
293 for(
auto code : m_extension_codes) {
294 if(!in_order.contains(code)) {
295 new_codes.push_back(code);
303 std::unordered_set<Extension_Code> already_pushed;
304 for(
auto code : order) {
305 if(m_extensions.contains(code) && already_pushed.insert(code).second) {
306 new_codes.push_back(code);
310 m_extension_codes = std::move(new_codes);
314 m_type(
type), m_value(reader.get_fixed<uint8_t>(extension_size)) {}
333 if(extension_size != 0) {
334 throw TLS_Exception(Alert::IllegalParameter,
"Server sent non-empty SNI extension");
338 if(extension_size == 0) {
339 throw TLS_Exception(Alert::IllegalParameter,
"Client sent empty SNI extension");
347 if(name_bytes + 2 != extension_size || name_bytes < 4) {
354 const uint8_t name_type = reader.
get_byte();
361 if(!m_sni_host_name.empty()) {
362 throw Decoding_Error(
"TLS ServerNameIndicator contains more than one host_name");
364 m_sni_host_name = reader.
get_string(2, 1, 65535);
373 const uint16_t unknown_name_len = reader.
get_uint16_t();
389 std::vector<uint8_t> buf;
391 const size_t name_len = m_sni_host_name.size();
398 buf.push_back(
get_byte<0>(
static_cast<uint16_t
>(name_len + 3)));
399 buf.push_back(
get_byte<1>(
static_cast<uint16_t
>(name_len + 3)));
402 buf.push_back(
get_byte<0>(
static_cast<uint16_t
>(name_len)));
403 buf.push_back(
get_byte<1>(
static_cast<uint16_t
>(name_len)));
413 if(hostname.empty() || hostname.size() > 255) {
433 BOTAN_ARG_CHECK(!protocol.empty(),
"ALPN protocol name must not be empty");
435 m_protocols.emplace_back(protocol);
440 for(
const auto& protocol : m_protocols) {
441 BOTAN_ARG_CHECK(!protocol.empty(),
"ALPN protocol name must not be empty");
447 uint16_t extension_size,
449 if(extension_size < 2) {
455 size_t bytes_remaining = extension_size - 2;
457 if(name_bytes != bytes_remaining) {
458 throw Decoding_Error(
"Bad encoding of ALPN extension, bad length field");
462 if(name_bytes == 0) {
463 throw Decoding_Error(
"Empty ALPN protocol_name_list not allowed");
466 while(bytes_remaining > 0) {
467 const std::string p = reader.
get_string(1, 0, 255);
469 if(bytes_remaining < p.size() + 1) {
470 throw Decoding_Error(
"Bad encoding of ALPN, length field too long");
477 bytes_remaining -= (p.size() + 1);
479 m_protocols.push_back(p);
489 "Server sent " + std::to_string(m_protocols.size()) +
" protocols in ALPN extension response");
495 return m_protocols.front();
499 std::vector<uint8_t> buf(2);
501 for(
auto&& proto : m_protocols) {
502 if(proto.length() >= 256) {
503 throw TLS_Exception(Alert::InternalError,
"ALPN name too long");
512 buf[0] =
get_byte<0>(
static_cast<uint16_t
>(buf.size() - 2));
513 buf[1] =
get_byte<1>(
static_cast<uint16_t
>(buf.size() - 2));
520 BOTAN_ARG_CHECK(!m_certificate_types.empty(),
"at least one certificate type must be supported");
530 std::span<const Certificate_Type> server_preference) :
538 for(
const auto server_supported_cert_type : server_preference) {
539 if(
value_exists(certificate_type_from_client.m_certificate_types, server_supported_cert_type)) {
540 m_certificate_types.push_back(server_supported_cert_type);
550 throw TLS_Exception(Alert::UnsupportedCertificate,
"Failed to agree on certificate_type");
555 if(extension_size == 0) {
556 throw Decoding_Error(
"Certificate type extension cannot be empty");
561 if(
static_cast<size_t>(extension_size) != type_bytes.size() + 1) {
562 throw Decoding_Error(
"certificate type extension had inconsistent length");
565 if(type_bytes.empty()) {
566 throw Decoding_Error(
"Certificate type extension contains no types");
569 type_bytes.begin(), type_bytes.end(), std::back_inserter(m_certificate_types), [](
const auto type_byte) {
570 return static_cast<Certificate_Type>(type_byte);
576 if(extension_size != 1) {
577 throw Decoding_Error(
"Server's certificate type extension must be of length 1");
579 const auto type_byte = reader.
get_byte();
585 std::vector<uint8_t> result;
587 std::vector<uint8_t> type_bytes;
589 m_certificate_types.begin(), m_certificate_types.end(), std::back_inserter(type_bytes), [](
const auto type) {
590 return static_cast<uint8_t>(type);
595 result.push_back(
static_cast<uint8_t
>(m_certificate_types.front()));
610 Botan::fmt(
"Selected certificate type was not offered: {}",
618 return m_certificate_types.front();
628 std::vector<Group_Params> ec;
629 for(
auto g : m_groups) {
630 if(g.is_pure_ecc_group()) {
638 std::vector<Group_Params> dh;
639 for(
auto g : m_groups) {
640 if(g.is_in_ffdhe_range()) {
648 std::vector<uint8_t> buf(2);
650 for(
auto g : m_groups) {
651 const uint16_t
id = g.wire_code();
661 buf[0] =
get_byte<0>(
static_cast<uint16_t
>(buf.size() - 2));
662 buf[1] =
get_byte<1>(
static_cast<uint16_t
>(buf.size() - 2));
670 if(len + 2 != extension_size) {
671 throw Decoding_Error(
"Inconsistent length field in supported groups list");
683 const size_t elems = len / 2;
685 std::unordered_set<uint16_t> seen;
686 for(
size_t i = 0; i != elems; ++i) {
689 if(seen.insert(group.wire_code()).second) {
690 m_groups.push_back(group);
697std::vector<uint8_t> serialize_signature_algorithms(
const std::vector<Signature_Scheme>& schemes) {
698 BOTAN_ASSERT(schemes.size() < 256,
"Too many signature schemes");
700 std::vector<uint8_t> buf;
702 const uint16_t len =
static_cast<uint16_t
>(schemes.size() * 2);
715std::vector<Signature_Scheme> parse_signature_algorithms(TLS_Data_Reader& reader, uint16_t extension_size) {
716 uint16_t len = reader.get_uint16_t();
718 if(len + 2 != extension_size || len % 2 == 1 || len == 0) {
719 throw Decoding_Error(
"Bad encoding on signature algorithms extension");
722 std::vector<Signature_Scheme> schemes;
723 schemes.reserve(len / 2);
725 schemes.emplace_back(reader.get_uint16_t());
735 return serialize_signature_algorithms(m_schemes);
739 m_schemes(parse_signature_algorithms(reader, extension_size)) {}
742 return serialize_signature_algorithms(m_schemes);
746 m_schemes(parse_signature_algorithms(reader, extension_size)) {}
754 if(extension_size < 5) {
757 const size_t max_profile_pairs = (
static_cast<size_t>(extension_size) - 3) / 2;
758 m_pp = reader.
get_range<uint16_t>(2, 1, max_profile_pairs);
759 const std::vector<uint8_t> mki = reader.
get_range<uint8_t>(1, 0, 255);
761 if(m_pp.size() * 2 + mki.size() + 3 != extension_size) {
762 throw Decoding_Error(
"Bad encoding for SRTP protection extension");
766 throw Decoding_Error(
"Unhandled non-empty MKI for SRTP protection extension");
771 std::vector<uint8_t> buf;
773 const uint16_t pp_len =
static_cast<uint16_t
>(m_pp.size() * 2);
777 for(
const uint16_t pp : m_pp) {
788 std::vector<uint8_t> buf;
792 buf.push_back(m_versions[0].major_version());
793 buf.push_back(m_versions[0].minor_version());
798 const uint8_t len =
static_cast<uint8_t
>(m_versions.size() * 2);
803 buf.push_back(version.major_version());
804 buf.push_back(version.minor_version());
819#if defined(BOTAN_HAS_TLS_13)
821 if(offer >= Protocol_Version::TLS_V13 && policy.
allow_tls13()) {
822 m_versions.push_back(Protocol_Version::TLS_V13);
827#if defined(BOTAN_HAS_TLS_12)
829 if(offer >= Protocol_Version::DTLS_V12 && policy.
allow_dtls12()) {
830 m_versions.push_back(Protocol_Version::DTLS_V12);
833 if(offer >= Protocol_Version::TLS_V12 && policy.
allow_tls12()) {
834 m_versions.push_back(Protocol_Version::TLS_V12);
845 if(extension_size != 2) {
846 throw Decoding_Error(
"Server sent invalid supported_versions extension");
856 if(extension_size != 1 + 2 *
versions.size()) {
857 throw Decoding_Error(
"Client sent invalid supported_versions extension");
863 for(
auto v : m_versions) {
872 BOTAN_ARG_CHECK(
limit >= 64,
"RFC 8449 does not allow record size limits smaller than 64 bytes");
874 "RFC 8449 does not allow record size limits larger than 2^14+1");
878 if(extension_size != 2) {
879 throw TLS_Exception(Alert::DecodeError,
"invalid record_size_limit extension");
899 "Server requested a record size limit larger than the protocol's maximum");
907 throw TLS_Exception(Alert::IllegalParameter,
"Received a record size limit smaller than 64 bytes");
912 std::vector<uint8_t> buf;
#define BOTAN_ASSERT_NOMSG(expr)
#define BOTAN_STATE_CHECK(expr)
#define BOTAN_ARG_CHECK(expr, msg)
#define BOTAN_ASSERT(expr, assertion_made)
static std::optional< DNSName > from_string(std::string_view name)
static std::optional< IPv4Address > from_string(std::string_view str)
static std::optional< IPv6Address > from_string(std::string_view str)
Application_Layer_Protocol_Notification(std::string_view protocol)
const std::vector< std::string > & protocols() const
std::string single_protocol() const
std::vector< uint8_t > serialize(Connection_Side whoami) const override
Certificate_Type selected_certificate_type() const
Certificate_Type_Base(std::vector< Certificate_Type > supported_cert_types)
void validate_selection(const Certificate_Type_Base &from_server) const
std::vector< uint8_t > serialize(Connection_Side whoami) const override
Extension_Code type() const override
Client_Certificate_Type(const Client_Certificate_Type &cct, const Policy &policy)
Certificate_Type_Base(std::vector< Certificate_Type > supported_cert_types)
virtual Extension_Code type() const =0
std::vector< uint8_t > serialize(Connection_Side whoami) const
void reorder(std::span< const Extension_Code > order)
void deserialize(TLS_Data_Reader &reader, Connection_Side from, Handshake_Type message_type)
bool remove_extension(Extension_Code type)
std::set< Extension_Code > extension_types() const
void add(std::unique_ptr< Extension > extn)
bool contains_other_than(const std::set< Extension_Code > &allowed_extensions, bool allow_unknown_extensions=false) const
virtual bool allow_tls12() const
virtual bool allow_tls13() const
virtual bool allow_dtls12() const
bool is_datagram_protocol() const
std::vector< uint8_t > serialize(Connection_Side whoami) const override
Record_Size_Limit(uint16_t limit)
SRTP_Protection_Profiles(std::vector< uint16_t > pp)
std::vector< uint8_t > serialize(Connection_Side whoami) const override
Server_Certificate_Type(const Server_Certificate_Type &sct, const Policy &policy)
Certificate_Type_Base(std::vector< Certificate_Type > supported_cert_types)
std::vector< uint8_t > serialize(Connection_Side whoami) const override
static bool hostname_acceptable_for_sni(std::string_view hostname)
Server_Name_Indicator(std::string_view host_name)
Signature_Algorithms_Cert(std::vector< Signature_Scheme > schemes)
std::vector< uint8_t > serialize(Connection_Side whoami) const override
Signature_Algorithms(std::vector< Signature_Scheme > schemes)
std::vector< uint8_t > serialize(Connection_Side whoami) const override
Supported_Groups(std::vector< Group_Params > groups)
std::vector< Group_Params > ec_groups() const
const std::vector< Group_Params > & groups() const
std::vector< uint8_t > serialize(Connection_Side whoami) const override
std::vector< Group_Params > dh_groups() const
bool supports(Protocol_Version version) const
Supported_Versions(Protocol_Version version, const Policy &policy)
const std::vector< Protocol_Version > & versions() const
std::vector< uint8_t > serialize(Connection_Side whoami) const override
std::string get_string(size_t len_bytes, size_t min_bytes, size_t max_bytes)
bool has_remaining() const
void discard_next(size_t bytes)
std::vector< T > get_range(size_t len_bytes, size_t min_elems, size_t max_elems)
size_t remaining_bytes() const
std::vector< uint8_t > get_tls_length_value(size_t len_bytes)
std::vector< T > get_fixed(size_t size)
std::vector< uint8_t > serialize(Connection_Side whoami) const override
Unknown_Extension(Extension_Code type, TLS_Data_Reader &reader, uint16_t extension_size)
Extension_Code type() const override
std::string certificate_type_to_string(Certificate_Type type)
void append_tls_length_value(std::vector< uint8_t, Alloc > &buf, const T *vals, size_t vals_size, size_t tag_size)
@ CertSignatureAlgorithms
@ ApplicationLayerProtocolNegotiation
@ CertificateStatusRequest
constexpr uint8_t get_byte(T input)
std::span< const uint8_t > as_span_of_bytes(const char *s, size_t len)
std::string fmt(std::string_view format, const T &... args)
bool value_exists(const std::vector< T > &vec, const V &val)