13#include <botan/tls_extensions.h>
15#include <botan/dns_name.h>
16#include <botan/exceptn.h>
17#include <botan/ipv4_address.h>
18#include <botan/ipv6_address.h>
19#include <botan/tls_exceptn.h>
20#include <botan/tls_policy.h>
21#include <botan/internal/fmt.h>
22#include <botan/internal/stl_util.h>
23#include <botan/internal/tls_messages_internal.h>
24#include <botan/internal/tls_reader.h>
26#include <unordered_set>
28#if defined(BOTAN_HAS_TLS_13)
29 #include <botan/tls_extensions_13.h>
32#if defined(BOTAN_HAS_TLS_12)
33 #include <botan/tls_extensions_12.h>
44class Padding_Extension final :
public Extension {
50 Padding_Extension(TLS_Data_Reader& reader, uint16_t extension_size) : m_size(extension_size) {
54 const auto padding = reader.get_fixed<uint8_t>(extension_size);
55 if(std::ranges::any_of(padding, [](uint8_t b) {
return b != 0; })) {
56 throw Decoding_Error(
"Padding extension contains non-zero bytes");
60 std::vector<uint8_t> serialize(
Connection_Side )
const override {
return std::vector<uint8_t>(m_size); }
62 bool empty()
const override {
77 const uint16_t size =
static_cast<uint16_t
>(reader.remaining_bytes());
80 return std::make_unique<Server_Name_Indicator>(reader, size, from);
83 return std::make_unique<Supported_Groups>(reader, size);
89 return std::make_unique<Signature_Algorithms>(reader, size);
92 return std::make_unique<Signature_Algorithms_Cert>(reader, size);
95 return std::make_unique<SRTP_Protection_Profiles>(reader, size, from);
98 return std::make_unique<Application_Layer_Protocol_Notification>(reader, size, from);
101 return std::make_unique<Client_Certificate_Type>(reader, size, from);
104 return std::make_unique<Server_Certificate_Type>(reader, size, from);
107 return std::make_unique<Record_Size_Limit>(reader, size, from);
110 return std::make_unique<Supported_Versions>(reader, size, from);
113 return std::make_unique<Padding_Extension>(reader, size);
115#if defined(BOTAN_HAS_TLS_12)
117 return std::make_unique<Supported_Point_Formats>(reader, size);
120 return std::make_unique<Renegotiation_Extension>(reader, size);
123 return std::make_unique<Extended_Master_Secret>(reader, size);
126 return std::make_unique<Encrypt_then_MAC>(reader, size);
129 return std::make_unique<Session_Ticket_Extension>(reader, size, from);
139#if defined(BOTAN_HAS_TLS_13)
141 return std::make_unique<PSK>(reader, size, message_type);
144 return std::make_unique<EarlyDataIndication>(reader, size, message_type);
147 return std::make_unique<Cookie>(reader, size);
150 return std::make_unique<PSK_Key_Exchange_Modes>(reader, size);
153 return std::make_unique<Certificate_Authorities>(reader, size);
156 return std::make_unique<Key_Share>(reader, size, message_type);
168 return std::make_unique<Unknown_Extension>(code, reader, size);
176 return m_extensions.contains(type);
180 const auto i = m_extensions.find(type);
182 if(i == m_extensions.end()) {
185 return i->second.get();
190 const auto type = extn->
type();
192 throw Invalid_Argument(
"cannot add the same extension twice: " + std::to_string(
static_cast<uint16_t
>(type)));
195 m_extension_codes.push_back(
type);
196 m_extensions.emplace(
type, std::move(extn));
213 if(this->
has(type)) {
214 throw TLS_Exception(TLS::Alert::DecodeError,
"Peer sent duplicated extensions");
219 const std::vector<uint8_t> extn_data = reader.
get_fixed<uint8_t>(extension_size);
220 m_raw_extension_data[type] = extn_data;
221 TLS_Data_Reader extn_reader(
"Extension", extn_data);
222 this->
add(make_extension(extn_reader, type, from, message_type));
223 extn_reader.assert_done();
229 const bool allow_unknown_extensions)
const {
232 std::vector<Extension_Code> diff;
234 found.cbegin(), found.end(), allowed_extensions.cbegin(), allowed_extensions.cend(), std::back_inserter(diff));
236 if(allow_unknown_extensions) {
239 const auto itr = std::find_if(diff.cbegin(), diff.cend(), [
this](
const auto ext_type) {
240 const auto ext = get(ext_type);
241 return ext && ext->is_implemented();
245 return itr != diff.cend();
248 return !diff.empty();
252 const auto i = m_extensions.find(type);
254 if(i == m_extensions.end()) {
257 m_extensions.erase(i);
258 std::erase(m_extension_codes, type);
259 m_raw_extension_data.erase(type);
265 std::vector<uint8_t> buf(2);
268 for(
const auto extn_type : m_extension_codes) {
269 const auto& extn = m_extensions.at(extn_type);
275 const uint16_t extn_code =
static_cast<uint16_t
>(extn_type);
277 const std::vector<uint8_t> extn_val = extn->serialize(whoami);
280 if(extn_val.size() > 0xFFFF) {
287 buf.push_back(
get_byte<0>(
static_cast<uint16_t
>(extn_val.size())));
288 buf.push_back(
get_byte<1>(
static_cast<uint16_t
>(extn_val.size())));
294 if(buf.size() - 2 > 0xFFFF) {
297 const uint16_t extn_size =
static_cast<uint16_t
>(buf.size() - 2);
303 if(buf.size() == 2) {
304 return std::vector<uint8_t>();
311 std::set<Extension_Code> offers;
312 for(
const auto& [extn_type, extn] : m_extensions) {
316 if(!extn->empty() || m_raw_extension_data.contains(extn_type)) {
317 offers.insert(extn_type);
324 const std::set<Extension_Code> in_order(order.begin(), order.end());
326 std::vector<Extension_Code> new_codes;
327 new_codes.reserve(m_extension_codes.size());
330 for(
const auto code : m_extension_codes) {
331 if(!in_order.contains(code)) {
332 new_codes.push_back(code);
340 std::unordered_set<Extension_Code> already_pushed;
341 for(
const auto code : order) {
342 if(m_extensions.contains(code) && already_pushed.insert(code).second) {
343 new_codes.push_back(code);
347 m_extension_codes = std::move(new_codes);
351 m_type(
type), m_value(reader.get_fixed<uint8_t>(extension_size)) {}
370 if(extension_size != 0) {
371 throw TLS_Exception(Alert::IllegalParameter,
"Server sent non-empty SNI extension");
375 if(extension_size == 0) {
376 throw TLS_Exception(Alert::IllegalParameter,
"Client sent empty SNI extension");
384 if(name_bytes + 2 != extension_size || name_bytes < 4) {
390 std::vector<uint8_t> seen_name_types;
392 const uint8_t name_type = reader.
get_byte();
399 throw Decoding_Error(
"TLS ServerNameIndicator contains more than one name of the same type");
401 seen_name_types.push_back(name_type);
413 m_sni_host_name = dns_name->to_string();
416 "TLS ServerNameIndicator host_name is not a valid DNS name");
426 const uint16_t unknown_name_len = reader.
get_uint16_t();
442 std::vector<uint8_t> buf;
444 const size_t name_len = m_sni_host_name.size();
449 if(name_len + 3 > 0xFFFF) {
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)));
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)));
468 if(hostname.empty() || hostname.size() > 255) {
488 BOTAN_ARG_CHECK(!protocol.empty(),
"ALPN protocol name must not be empty");
490 m_protocols.emplace_back(protocol);
495 for(
const auto& protocol : m_protocols) {
496 BOTAN_ARG_CHECK(!protocol.empty(),
"ALPN protocol name must not be empty");
502 uint16_t extension_size,
504 if(extension_size < 2) {
510 size_t bytes_remaining = extension_size - 2;
512 if(name_bytes != bytes_remaining) {
513 throw Decoding_Error(
"Bad encoding of ALPN extension, bad length field");
517 if(name_bytes == 0) {
518 throw Decoding_Error(
"Empty ALPN protocol_name_list not allowed");
521 while(bytes_remaining > 0) {
522 const std::string p = reader.
get_string(1, 0, 255);
524 if(bytes_remaining < p.size() + 1) {
525 throw Decoding_Error(
"Bad encoding of ALPN, length field too long");
532 bytes_remaining -= (p.size() + 1);
534 m_protocols.push_back(p);
544 "Server sent " + std::to_string(m_protocols.size()) +
" protocols in ALPN extension response");
550 return m_protocols.front();
554 std::vector<uint8_t> buf(2);
556 for(
auto&& proto : m_protocols) {
557 if(proto.length() >= 256) {
558 throw TLS_Exception(Alert::InternalError,
"ALPN name too long");
566 if(buf.size() - 2 > 0xFFFF) {
567 throw Encoding_Error(
"TLS ALPN protocol list is too long to encode");
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));
577 BOTAN_ARG_CHECK(!m_certificate_types.empty(),
"at least one certificate type must be supported");
587 std::span<const Certificate_Type> server_preference) :
595 for(
const auto server_supported_cert_type : server_preference) {
596 if(
value_exists(certificate_type_from_client.m_certificate_types, server_supported_cert_type)) {
597 m_certificate_types.push_back(server_supported_cert_type);
607 throw TLS_Exception(Alert::UnsupportedCertificate,
"Failed to agree on certificate_type");
612 if(extension_size == 0) {
613 throw Decoding_Error(
"Certificate type extension cannot be empty");
618 if(
static_cast<size_t>(extension_size) != type_bytes.size() + 1) {
619 throw Decoding_Error(
"certificate type extension had inconsistent length");
622 if(type_bytes.empty()) {
623 throw Decoding_Error(
"Certificate type extension contains no types");
626 type_bytes.begin(), type_bytes.end(), std::back_inserter(m_certificate_types), [](
const auto type_byte) {
627 return static_cast<Certificate_Type>(type_byte);
640 throw TLS_Exception(Alert::IllegalParameter,
"Certificate type extension contains only the default type");
646 if(extension_size != 1) {
647 throw Decoding_Error(
"Server's certificate type extension must be of length 1");
649 const auto type_byte = reader.
get_byte();
655 std::vector<uint8_t> result;
657 std::vector<uint8_t> type_bytes;
659 m_certificate_types.begin(), m_certificate_types.end(), std::back_inserter(type_bytes), [](
const auto type) {
660 return static_cast<uint8_t>(type);
665 result.push_back(
static_cast<uint8_t
>(m_certificate_types.front()));
680 Botan::fmt(
"Selected certificate type was not offered: {}",
688 return m_certificate_types.front();
698 std::vector<Group_Params> ec;
699 for(
const auto g : m_groups) {
708 std::vector<Group_Params> dh;
709 for(
const auto g : m_groups) {
710 if(g.is_in_ffdhe_range()) {
718 std::vector<uint8_t> buf(2);
720 for(
const auto g : m_groups) {
721 const uint16_t
id = g.wire_code();
731 buf[0] =
get_byte<0>(
static_cast<uint16_t
>(buf.size() - 2));
732 buf[1] =
get_byte<1>(
static_cast<uint16_t
>(buf.size() - 2));
740 if(len + 2 != extension_size) {
741 throw Decoding_Error(
"Inconsistent length field in supported groups list");
753 const size_t elems = len / 2;
755 std::unordered_set<uint16_t> seen;
756 for(
size_t i = 0; i != elems; ++i) {
759 if(seen.insert(group.wire_code()).second) {
760 m_groups.push_back(group);
767std::vector<uint8_t> serialize_signature_algorithms(
const std::vector<Signature_Scheme>& schemes) {
768 BOTAN_ASSERT(schemes.size() < 256,
"Too many signature schemes");
770 std::vector<uint8_t> buf;
772 const uint16_t len =
static_cast<uint16_t
>(schemes.size() * 2);
785std::vector<Signature_Scheme> parse_signature_algorithms(TLS_Data_Reader& reader, uint16_t extension_size) {
786 uint16_t len = reader.get_uint16_t();
788 if(len + 2 != extension_size || len % 2 == 1 || len == 0) {
789 throw Decoding_Error(
"Bad encoding on signature algorithms extension");
792 std::vector<Signature_Scheme> schemes;
793 schemes.reserve(len / 2);
795 schemes.emplace_back(reader.get_uint16_t());
805 return serialize_signature_algorithms(m_schemes);
809 m_schemes(parse_signature_algorithms(reader, extension_size)) {}
812 return serialize_signature_algorithms(m_schemes);
816 m_schemes(parse_signature_algorithms(reader, extension_size)) {}
819 uint16_t extension_size,
826 if(extension_size < 5) {
829 const size_t max_profile_pairs = (
static_cast<size_t>(extension_size) - 3) / 2;
830 m_pp = reader.
get_range<uint16_t>(2, 1, max_profile_pairs);
831 const std::vector<uint8_t> mki = reader.
get_range<uint8_t>(1, 0, 255);
833 if(m_pp.size() * 2 + mki.size() + 3 != extension_size) {
834 throw Decoding_Error(
"Bad encoding for SRTP protection extension");
842 throw TLS_Exception(Alert::DecodeError,
"Server sent malformed DTLS-SRTP extension");
857 throw TLS_Exception(Alert::IllegalParameter,
"Server selected a DTLS-SRTP MKI that was not offered");
862 std::vector<uint8_t> buf;
864 const uint16_t pp_len =
static_cast<uint16_t
>(m_pp.size() * 2);
868 for(
const uint16_t pp : m_pp) {
879 std::vector<uint8_t> buf;
883 buf.push_back(m_versions[0].major_version());
884 buf.push_back(m_versions[0].minor_version());
889 const uint8_t len =
static_cast<uint8_t
>(m_versions.size() * 2);
894 buf.push_back(version.major_version());
895 buf.push_back(version.minor_version());
910#if defined(BOTAN_HAS_TLS_13)
912 if(offer >= Protocol_Version::TLS_V13 && policy.
allow_tls13()) {
913 m_versions.push_back(Protocol_Version::TLS_V13);
918#if defined(BOTAN_HAS_TLS_12)
920 if(offer >= Protocol_Version::DTLS_V12 && policy.
allow_dtls12()) {
921 m_versions.push_back(Protocol_Version::DTLS_V12);
924 if(offer >= Protocol_Version::TLS_V12 && policy.
allow_tls12()) {
925 m_versions.push_back(Protocol_Version::TLS_V12);
936 if(extension_size != 2) {
937 throw Decoding_Error(
"Server sent invalid supported_versions extension");
947 if(extension_size != 1 + 2 *
versions.size()) {
948 throw Decoding_Error(
"Client sent invalid supported_versions extension");
954 for(
const auto v : m_versions) {
963 BOTAN_ARG_CHECK(
limit >= 64,
"RFC 8449 does not allow record size limits smaller than 64 bytes");
965 "RFC 8449 does not allow record size limits larger than 2^14+1");
969 if(extension_size != 2) {
970 throw TLS_Exception(Alert::DecodeError,
"invalid record_size_limit extension");
990 "Server requested a record size limit larger than the protocol's maximum");
998 throw TLS_Exception(Alert::IllegalParameter,
"Received a record size limit smaller than 64 bytes");
1003 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::string host_name() const
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
bool group_may_be_ecc(Group_Params group)
std::unique_ptr< Extension > make_certificate_status_request(TLS_Data_Reader &reader, uint16_t extension_size, Handshake_Type message_type, Connection_Side from)
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)