13#include <botan/tls_extensions.h>
15#include <botan/tls_exceptn.h>
16#include <botan/tls_policy.h>
17#include <botan/internal/fmt.h>
18#include <botan/internal/parsing.h>
19#include <botan/internal/stl_util.h>
20#include <botan/internal/tls_reader.h>
23#if defined(BOTAN_HAS_TLS_13)
24 #include <botan/tls_extensions_13.h>
27#if defined(BOTAN_HAS_TLS_12)
28 #include <botan/tls_extensions_12.h>
41 const uint16_t size =
static_cast<uint16_t
>(reader.remaining_bytes());
44 return std::make_unique<Server_Name_Indicator>(reader, size);
47 return std::make_unique<Supported_Groups>(reader, size);
50 return std::make_unique<Certificate_Status_Request>(reader, size, message_type, from);
53 return std::make_unique<Signature_Algorithms>(reader, size);
56 return std::make_unique<Signature_Algorithms_Cert>(reader, size);
59 return std::make_unique<SRTP_Protection_Profiles>(reader, size);
62 return std::make_unique<Application_Layer_Protocol_Notification>(reader, size, from);
65 return std::make_unique<Client_Certificate_Type>(reader, size, from);
68 return std::make_unique<Server_Certificate_Type>(reader, size, from);
71 return std::make_unique<Record_Size_Limit>(reader, size, from);
74 return std::make_unique<Supported_Versions>(reader, size, from);
76#if defined(BOTAN_HAS_TLS_12)
78 return std::make_unique<Supported_Point_Formats>(reader, size);
81 return std::make_unique<Renegotiation_Extension>(reader, size);
84 return std::make_unique<Extended_Master_Secret>(reader, size);
87 return std::make_unique<Encrypt_then_MAC>(reader, size);
90 return std::make_unique<Session_Ticket_Extension>(reader, size);
100#if defined(BOTAN_HAS_TLS_13)
102 return std::make_unique<PSK>(reader, size, message_type);
105 return std::make_unique<EarlyDataIndication>(reader, size, message_type);
108 return std::make_unique<Cookie>(reader, size);
111 return std::make_unique<PSK_Key_Exchange_Modes>(reader, size);
114 return std::make_unique<Certificate_Authorities>(reader, size);
117 return std::make_unique<Key_Share>(reader, size, message_type);
129 return std::make_unique<Unknown_Extension>(code, reader, size);
138 std::find_if(m_extensions.cbegin(), m_extensions.cend(), [type](
const auto& ext) { return ext->type() == type; });
140 return (i != m_extensions.end()) ? i->get() :
nullptr;
144 if(
has(extn->type())) {
146 std::to_string(
static_cast<uint16_t
>(extn->type())));
149 m_extensions.emplace_back(extn.release());
166 if(this->
has(type)) {
167 throw TLS_Exception(TLS::Alert::DecodeError,
"Peer sent duplicated extensions");
172 const std::vector<uint8_t> extn_data = reader.
get_fixed<uint8_t>(extension_size);
174 this->
add(make_extension(extn_reader,
type, from, message_type));
175 extn_reader.assert_done();
181 const bool allow_unknown_extensions)
const {
184 std::vector<Extension_Code> diff;
186 found.cbegin(), found.end(), allowed_extensions.cbegin(), allowed_extensions.cend(), std::back_inserter(diff));
188 if(allow_unknown_extensions) {
191 const auto itr = std::find_if(diff.cbegin(), diff.cend(), [
this](
const auto ext_type) {
192 const auto ext = get(ext_type);
193 return ext && ext->is_implemented();
197 return itr != diff.cend();
200 return !diff.empty();
205 std::find_if(m_extensions.begin(), m_extensions.end(), [type](
const auto& ext) { return ext->type() == type; });
207 std::unique_ptr<Extension> result;
208 if(i != m_extensions.end()) {
209 std::swap(result, *i);
210 m_extensions.erase(i);
217 std::vector<uint8_t> buf(2);
219 for(
const auto& extn : m_extensions) {
224 const uint16_t extn_code =
static_cast<uint16_t
>(extn->type());
226 const std::vector<uint8_t> extn_val = extn->serialize(whoami);
231 buf.push_back(
get_byte<0>(
static_cast<uint16_t
>(extn_val.size())));
232 buf.push_back(
get_byte<1>(
static_cast<uint16_t
>(extn_val.size())));
237 const uint16_t extn_size =
static_cast<uint16_t
>(buf.size() - 2);
243 if(buf.size() == 2) {
244 return std::vector<uint8_t>();
251 std::set<Extension_Code> offers;
253 m_extensions.cbegin(), m_extensions.cend(), std::inserter(offers, offers.begin()), [](
const auto& ext) {
260 m_type(
type), m_value(reader.get_fixed<uint8_t>(extension_size)) {}
270 if(extension_size == 0) {
276 if(name_bytes + 2 != extension_size) {
280 while(name_bytes > 0) {
281 const uint8_t name_type = reader.
get_byte();
286 m_sni_host_name = reader.
get_string(2, 1, 65535);
287 name_bytes -=
static_cast<uint16_t
>(2 + m_sni_host_name.size());
305 std::vector<uint8_t> buf;
307 const size_t name_len = m_sni_host_name.size();
309 buf.push_back(
get_byte<0>(
static_cast<uint16_t
>(name_len + 3)));
310 buf.push_back(
get_byte<1>(
static_cast<uint16_t
>(name_len + 3)));
313 buf.push_back(
get_byte<0>(
static_cast<uint16_t
>(name_len)));
314 buf.push_back(
get_byte<1>(
static_cast<uint16_t
>(name_len)));
324 if(hostname.empty()) {
333 if(hostname.find(
':') != std::string_view::npos) {
341 uint16_t extension_size,
343 if(extension_size == 0) {
349 size_t bytes_remaining = extension_size - 2;
351 if(name_bytes != bytes_remaining) {
352 throw Decoding_Error(
"Bad encoding of ALPN extension, bad length field");
355 while(bytes_remaining > 0) {
356 const std::string p = reader.
get_string(1, 0, 255);
358 if(bytes_remaining < p.size() + 1) {
359 throw Decoding_Error(
"Bad encoding of ALPN, length field too long");
366 bytes_remaining -= (p.size() + 1);
368 m_protocols.push_back(p);
378 "Server sent " + std::to_string(m_protocols.size()) +
" protocols in ALPN extension response");
384 return m_protocols.front();
388 std::vector<uint8_t> buf(2);
390 for(
auto&& proto : m_protocols) {
391 if(proto.length() >= 256) {
392 throw TLS_Exception(Alert::InternalError,
"ALPN name too long");
399 buf[0] =
get_byte<0>(
static_cast<uint16_t
>(buf.size() - 2));
400 buf[1] =
get_byte<1>(
static_cast<uint16_t
>(buf.size() - 2));
407 BOTAN_ARG_CHECK(!m_certificate_types.empty(),
"at least one certificate type must be supported");
417 const std::vector<Certificate_Type>& server_preference) :
425 for(
const auto server_supported_cert_type : server_preference) {
426 if(
value_exists(certificate_type_from_client.m_certificate_types, server_supported_cert_type)) {
427 m_certificate_types.push_back(server_supported_cert_type);
437 throw TLS_Exception(Alert::UnsupportedCertificate,
"Failed to agree on certificate_type");
442 if(extension_size == 0) {
443 throw Decoding_Error(
"Certificate type extension cannot be empty");
448 if(
static_cast<size_t>(extension_size) != type_bytes.size() + 1) {
449 throw Decoding_Error(
"certificate type extension had inconsistent length");
452 type_bytes.begin(), type_bytes.end(), std::back_inserter(m_certificate_types), [](
const auto type_byte) {
453 return static_cast<Certificate_Type>(type_byte);
459 if(extension_size != 1) {
460 throw Decoding_Error(
"Server's certificate type extension must be of length 1");
462 const auto type_byte = reader.
get_byte();
468 std::vector<uint8_t> result;
470 std::vector<uint8_t> type_bytes;
472 m_certificate_types.begin(), m_certificate_types.end(), std::back_inserter(type_bytes), [](
const auto type) {
473 return static_cast<uint8_t>(type);
478 result.push_back(
static_cast<uint8_t
>(m_certificate_types.front()));
493 Botan::fmt(
"Selected certificate type was not offered: {}",
501 return m_certificate_types.front();
511 std::vector<Group_Params> ec;
512 for(
auto g : m_groups) {
513 if(g.is_pure_ecc_group()) {
521 std::vector<Group_Params> dh;
522 for(
auto g : m_groups) {
523 if(g.is_in_ffdhe_range()) {
531 std::vector<uint8_t> buf(2);
533 for(
auto g : m_groups) {
534 const uint16_t
id = g.wire_code();
542 buf[0] =
get_byte<0>(
static_cast<uint16_t
>(buf.size() - 2));
543 buf[1] =
get_byte<1>(
static_cast<uint16_t
>(buf.size() - 2));
551 if(len + 2 != extension_size) {
552 throw Decoding_Error(
"Inconsistent length field in supported groups list");
559 const size_t elems = len / 2;
561 for(
size_t i = 0; i != elems; ++i) {
565 m_groups.push_back(group);
572std::vector<uint8_t> serialize_signature_algorithms(
const std::vector<Signature_Scheme>& schemes) {
573 BOTAN_ASSERT(schemes.size() < 256,
"Too many signature schemes");
575 std::vector<uint8_t> buf;
577 const uint16_t len =
static_cast<uint16_t
>(schemes.size() * 2);
590std::vector<Signature_Scheme> parse_signature_algorithms(TLS_Data_Reader& reader, uint16_t extension_size) {
591 uint16_t len = reader.get_uint16_t();
593 if(len + 2 != extension_size || len % 2 == 1 || len == 0) {
594 throw Decoding_Error(
"Bad encoding on signature algorithms extension");
597 std::vector<Signature_Scheme> schemes;
598 schemes.reserve(len / 2);
600 schemes.emplace_back(reader.get_uint16_t());
610 return serialize_signature_algorithms(m_schemes);
614 m_schemes(parse_signature_algorithms(reader, extension_size)) {}
617 return serialize_signature_algorithms(m_schemes);
621 m_schemes(parse_signature_algorithms(reader, extension_size)) {}
624 m_pp(reader.get_range<uint16_t>(2, 0, 65535)) {
625 const std::vector<uint8_t> mki = reader.
get_range<uint8_t>(1, 0, 255);
627 if(m_pp.size() * 2 + mki.size() + 3 != extension_size) {
628 throw Decoding_Error(
"Bad encoding for SRTP protection extension");
632 throw Decoding_Error(
"Unhandled non-empty MKI for SRTP protection extension");
637 std::vector<uint8_t> buf;
639 const uint16_t pp_len =
static_cast<uint16_t
>(m_pp.size() * 2);
643 for(
const uint16_t pp : m_pp) {
654 std::vector<uint8_t> buf;
658 buf.push_back(m_versions[0].major_version());
659 buf.push_back(m_versions[0].minor_version());
662 const uint8_t len =
static_cast<uint8_t
>(m_versions.size() * 2);
667 buf.push_back(version.major_version());
668 buf.push_back(version.minor_version());
683#if defined(BOTAN_HAS_TLS_13)
685 if(offer >= Protocol_Version::TLS_V13 && policy.
allow_tls13()) {
686 m_versions.push_back(Protocol_Version::TLS_V13);
691#if defined(BOTAN_HAS_TLS_12)
693 if(offer >= Protocol_Version::DTLS_V12 && policy.
allow_dtls12()) {
694 m_versions.push_back(Protocol_Version::DTLS_V12);
697 if(offer >= Protocol_Version::TLS_V12 && policy.
allow_tls12()) {
698 m_versions.push_back(Protocol_Version::TLS_V12);
709 if(extension_size != 2) {
710 throw Decoding_Error(
"Server sent invalid supported_versions extension");
720 if(extension_size != 1 + 2 *
versions.size()) {
721 throw Decoding_Error(
"Client sent invalid supported_versions extension");
727 for(
auto v : m_versions) {
736 BOTAN_ASSERT(
limit >= 64,
"RFC 8449 does not allow record size limits smaller than 64 bytes");
738 "RFC 8449 does not allow record size limits larger than 2^14+1");
742 if(extension_size != 2) {
743 throw TLS_Exception(Alert::DecodeError,
"invalid record_size_limit extension");
763 "Server requested a record size limit larger than the protocol's maximum");
771 throw TLS_Exception(Alert::IllegalParameter,
"Received a record size limit smaller than 64 bytes");
776 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)
Application_Layer_Protocol_Notification(std::string_view protocol)
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 deserialize(TLS_Data_Reader &reader, Connection_Side from, Handshake_Type message_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(const 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(const 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)
std::optional< uint32_t > string_to_ipv4(std::string_view str)