13#include <botan/tls_extensions.h>
15#include <botan/ber_dec.h>
16#include <botan/der_enc.h>
17#include <botan/tls_exceptn.h>
18#include <botan/tls_policy.h>
19#include <botan/internal/stl_util.h>
20#include <botan/internal/tls_reader.h>
28std::unique_ptr<Extension> make_extension(TLS_Data_Reader& reader,
34 const uint16_t size =
static_cast<uint16_t
>(reader.remaining_bytes());
37 return std::make_unique<Server_Name_Indicator>(reader, size);
40 return std::make_unique<Supported_Groups>(reader, size);
43 return std::make_unique<Certificate_Status_Request>(reader, size, message_type, from);
46 return std::make_unique<Supported_Point_Formats>(reader, size);
49 return std::make_unique<Renegotiation_Extension>(reader, size);
52 return std::make_unique<Signature_Algorithms>(reader, size);
55 return std::make_unique<Signature_Algorithms_Cert>(reader, size);
58 return std::make_unique<SRTP_Protection_Profiles>(reader, size);
61 return std::make_unique<Application_Layer_Protocol_Notification>(reader, size, from);
64 return std::make_unique<Client_Certificate_Type>(reader, size, from);
67 return std::make_unique<Server_Certificate_Type>(reader, size, from);
70 return std::make_unique<Extended_Master_Secret>(reader, size);
73 return std::make_unique<Record_Size_Limit>(reader, size, from);
76 return std::make_unique<Encrypt_then_MAC>(reader, size);
79 return std::make_unique<Session_Ticket_Extension>(reader, size);
82 return std::make_unique<Supported_Versions>(reader, size, from);
84#if defined(BOTAN_HAS_TLS_13)
86 return std::make_unique<PSK>(reader, size, message_type);
89 return std::make_unique<EarlyDataIndication>(reader, size, message_type);
92 return std::make_unique<Cookie>(reader, size);
95 return std::make_unique<PSK_Key_Exchange_Modes>(reader, size);
98 return std::make_unique<Certificate_Authorities>(reader, size);
101 return std::make_unique<Key_Share>(reader, size, message_type);
105 return std::make_unique<Unknown_Extension>(code, reader, size);
111 if(
has(extn->type())) {
113 std::to_string(
static_cast<uint16_t
>(extn->type())));
116 m_extensions.emplace_back(extn.release());
133 if(this->
has(type)) {
134 throw TLS_Exception(TLS::Alert::DecodeError,
"Peer sent duplicated extensions");
139 const std::vector<uint8_t> extn_data = reader.
get_fixed<uint8_t>(extension_size);
141 this->
add(make_extension(extn_reader, type, from, message_type));
142 extn_reader.assert_done();
148 const bool allow_unknown_extensions)
const {
151 std::vector<Extension_Code> diff;
153 found.cbegin(), found.end(), allowed_extensions.cbegin(), allowed_extensions.cend(), std::back_inserter(diff));
155 if(allow_unknown_extensions) {
158 const auto itr = std::find_if(diff.cbegin(), diff.cend(), [
this](
const auto ext_type) {
159 const auto ext = get(ext_type);
160 return ext && ext->is_implemented();
164 return itr != diff.cend();
167 return !diff.empty();
172 std::find_if(m_extensions.begin(), m_extensions.end(), [type](
const auto& ext) { return ext->type() == type; });
174 std::unique_ptr<Extension> result;
175 if(i != m_extensions.end()) {
176 std::swap(result, *i);
177 m_extensions.erase(i);
184 std::vector<uint8_t> buf(2);
186 for(
const auto& extn : m_extensions) {
191 const uint16_t extn_code =
static_cast<uint16_t
>(extn->type());
193 const std::vector<uint8_t> extn_val = extn->serialize(whoami);
198 buf.push_back(
get_byte<0>(
static_cast<uint16_t
>(extn_val.size())));
199 buf.push_back(
get_byte<1>(
static_cast<uint16_t
>(extn_val.size())));
204 const uint16_t extn_size =
static_cast<uint16_t
>(buf.size() - 2);
210 if(buf.size() == 2) {
211 return std::vector<uint8_t>();
218 std::set<Extension_Code> offers;
220 m_extensions.cbegin(), m_extensions.cend(), std::inserter(offers, offers.begin()), [](
const auto& ext) {
227 m_type(type), m_value(reader.get_fixed<uint8_t>(extension_size)) {}
237 if(extension_size == 0) {
243 if(name_bytes + 2 != extension_size) {
248 uint8_t name_type = reader.
get_byte();
253 m_sni_host_name = reader.
get_string(2, 1, 65535);
254 name_bytes -=
static_cast<uint16_t
>(2 + m_sni_host_name.size());
272 std::vector<uint8_t> buf;
274 size_t name_len = m_sni_host_name.size();
276 buf.push_back(
get_byte<0>(
static_cast<uint16_t
>(name_len + 3)));
277 buf.push_back(
get_byte<1>(
static_cast<uint16_t
>(name_len + 3)));
280 buf.push_back(
get_byte<0>(
static_cast<uint16_t
>(name_len)));
281 buf.push_back(
get_byte<1>(
static_cast<uint16_t
>(name_len)));
289 m_reneg_data(reader.get_range<uint8_t>(1, 0, 255)) {
290 if(m_reneg_data.size() + 1 != extension_size) {
291 throw Decoding_Error(
"Bad encoding for secure renegotiation extn");
296 std::vector<uint8_t> buf;
302 uint16_t extension_size,
304 if(extension_size == 0) {
310 size_t bytes_remaining = extension_size - 2;
312 if(name_bytes != bytes_remaining) {
313 throw Decoding_Error(
"Bad encoding of ALPN extension, bad length field");
316 while(bytes_remaining) {
317 const std::string p = reader.
get_string(1, 0, 255);
319 if(bytes_remaining < p.size() + 1) {
320 throw Decoding_Error(
"Bad encoding of ALPN, length field too long");
327 bytes_remaining -= (p.size() + 1);
329 m_protocols.push_back(p);
339 "Server sent " + std::to_string(m_protocols.size()) +
" protocols in ALPN extension response");
345 return m_protocols.front();
349 std::vector<uint8_t> buf(2);
351 for(
auto&& p : m_protocols) {
352 if(p.length() >= 256) {
353 throw TLS_Exception(Alert::InternalError,
"ALPN name too long");
360 buf[0] =
get_byte<0>(
static_cast<uint16_t
>(buf.size() - 2));
361 buf[1] =
get_byte<1>(
static_cast<uint16_t
>(buf.size() - 2));
371 return "RawPublicKey";
378 if(type_str ==
"X509") {
380 }
else if(type_str ==
"RawPublicKey") {
389 BOTAN_ARG_CHECK(!m_certificate_types.empty(),
"at least one certificate type must be supported");
399 const std::vector<Certificate_Type>& server_preference) :
407 for(
const auto server_supported_cert_type : server_preference) {
408 if(
value_exists(certificate_type_from_client.m_certificate_types, server_supported_cert_type)) {
409 m_certificate_types.push_back(server_supported_cert_type);
419 throw TLS_Exception(Alert::UnsupportedCertificate,
"Failed to agree on certificate_type");
424 if(extension_size == 0) {
425 throw Decoding_Error(
"Certificate type extension cannot be empty");
430 if(
static_cast<size_t>(extension_size) != type_bytes.size() + 1) {
431 throw Decoding_Error(
"certificate type extension had inconsistent length");
434 type_bytes.begin(), type_bytes.end(), std::back_inserter(m_certificate_types), [](
const auto type_byte) {
435 return static_cast<Certificate_Type>(type_byte);
441 if(extension_size != 1) {
442 throw Decoding_Error(
"Server's certificate type extension must be of length 1");
444 const auto type_byte = reader.
get_byte();
450 std::vector<uint8_t> result;
452 std::vector<uint8_t> type_bytes;
454 m_certificate_types.begin(), m_certificate_types.end(), std::back_inserter(type_bytes), [](
const auto type) {
455 return static_cast<uint8_t>(type);
460 result.push_back(
static_cast<uint8_t
>(m_certificate_types.front()));
475 Botan::fmt(
"Selected certificate type was not offered: {}",
483 return m_certificate_types.front();
493 std::vector<Group_Params> ec;
494 for(
auto g : m_groups) {
495 if(g.is_pure_ecc_group()) {
503 std::vector<Group_Params> dh;
504 for(
auto g : m_groups) {
505 if(g.is_in_ffdhe_range()) {
513 std::vector<uint8_t> buf(2);
515 for(
auto g : m_groups) {
516 const uint16_t
id = g.wire_code();
524 buf[0] =
get_byte<0>(
static_cast<uint16_t
>(buf.size() - 2));
525 buf[1] =
get_byte<1>(
static_cast<uint16_t
>(buf.size() - 2));
533 if(len + 2 != extension_size) {
534 throw Decoding_Error(
"Inconsistent length field in supported groups list");
541 const size_t elems = len / 2;
543 for(
size_t i = 0; i != elems; ++i) {
547 m_groups.push_back(group);
554 if(m_prefers_compressed) {
564 if(len + 1 != extension_size) {
565 throw Decoding_Error(
"Inconsistent length field in supported point formats list");
568 bool includes_uncompressed =
false;
569 for(
size_t i = 0; i != len; ++i) {
573 m_prefers_compressed =
false;
577 m_prefers_compressed =
true;
578 std::vector<uint8_t> remaining_formats = reader.
get_fixed<uint8_t>(len - i - 1);
579 includes_uncompressed =
580 std::any_of(std::begin(remaining_formats), std::end(remaining_formats), [](uint8_t remaining_format) {
595 if(!includes_uncompressed) {
597 "Supported Point Formats Extension must contain the uncompressed point format");
603std::vector<uint8_t> serialize_signature_algorithms(
const std::vector<Signature_Scheme>& schemes) {
604 BOTAN_ASSERT(schemes.size() < 256,
"Too many signature schemes");
606 std::vector<uint8_t> buf;
608 const uint16_t len =
static_cast<uint16_t
>(schemes.size() * 2);
621std::vector<Signature_Scheme> parse_signature_algorithms(TLS_Data_Reader& reader, uint16_t extension_size) {
622 uint16_t len = reader.get_uint16_t();
624 if(len + 2 != extension_size || len % 2 == 1 || len == 0) {
625 throw Decoding_Error(
"Bad encoding on signature algorithms extension");
628 std::vector<Signature_Scheme> schemes;
629 schemes.reserve(len / 2);
631 schemes.emplace_back(reader.get_uint16_t());
641 return serialize_signature_algorithms(m_schemes);
645 m_schemes(parse_signature_algorithms(reader, extension_size)) {}
648 return serialize_signature_algorithms(m_schemes);
652 m_schemes(parse_signature_algorithms(reader, extension_size)) {}
655 m_ticket(
Session_Ticket(reader.get_elem<uint8_t, std::vector<uint8_t>>(extension_size))) {}
658 m_pp(reader.get_range<uint16_t>(2, 0, 65535)) {
659 const std::vector<uint8_t> mki = reader.
get_range<uint8_t>(1, 0, 255);
661 if(m_pp.size() * 2 + mki.size() + 3 != extension_size) {
662 throw Decoding_Error(
"Bad encoding for SRTP protection extension");
666 throw Decoding_Error(
"Unhandled non-empty MKI for SRTP protection extension");
671 std::vector<uint8_t> buf;
673 const uint16_t pp_len =
static_cast<uint16_t
>(m_pp.size() * 2);
677 for(uint16_t pp : m_pp) {
688 if(extension_size != 0) {
694 return std::vector<uint8_t>();
698 if(extension_size != 0) {
704 return std::vector<uint8_t>();
708 std::vector<uint8_t> buf;
712 buf.push_back(m_versions[0].major_version());
713 buf.push_back(m_versions[0].minor_version());
716 const uint8_t len =
static_cast<uint8_t
>(m_versions.size() * 2);
721 buf.push_back(version.major_version());
722 buf.push_back(version.minor_version());
731#if defined(BOTAN_HAS_TLS_12)
732 if(offer >= Protocol_Version::DTLS_V12 && policy.
allow_dtls12()) {
733 m_versions.push_back(Protocol_Version::DTLS_V12);
737#if defined(BOTAN_HAS_TLS_13)
738 if(offer >= Protocol_Version::TLS_V13 && policy.
allow_tls13()) {
739 m_versions.push_back(Protocol_Version::TLS_V13);
742#if defined(BOTAN_HAS_TLS_12)
743 if(offer >= Protocol_Version::TLS_V12 && policy.
allow_tls12()) {
744 m_versions.push_back(Protocol_Version::TLS_V12);
752 if(extension_size != 2) {
753 throw Decoding_Error(
"Server sent invalid supported_versions extension");
763 if(extension_size != 1 + 2 *
versions.size()) {
764 throw Decoding_Error(
"Client sent invalid supported_versions extension");
770 for(
auto v : m_versions) {
779 BOTAN_ASSERT(
limit >= 64,
"RFC 8449 does not allow record size limits smaller than 64 bytes");
781 "RFC 8449 does not allow record size limits larger than 2^14+1");
785 if(extension_size != 2) {
786 throw TLS_Exception(Alert::DecodeError,
"invalid record_size_limit extension");
806 "Server requested a record size limit larger than the protocol's maximum");
814 throw TLS_Exception(Alert::IllegalParameter,
"Received a record size limit smaller than 64 bytes");
819 std::vector<uint8_t> buf;
827#if defined(BOTAN_HAS_TLS_13)
831 if(extension_size == 0) {
843 throw Decoding_Error(
"Not enough bytes in the buffer to decode Cookie");
846 for(
size_t i = 0; i < len; ++i) {
847 m_cookie.push_back(reader.
get_byte());
852 std::vector<uint8_t> buf;
854 const uint16_t len =
static_cast<uint16_t
>(m_cookie.size());
859 for(
const auto& cookie_byte : m_cookie) {
860 buf.push_back(cookie_byte);
867 std::vector<uint8_t> buf;
870 buf.push_back(
static_cast<uint8_t
>(m_modes.size()));
871 for(
const auto& mode : m_modes) {
872 buf.push_back(
static_cast<uint8_t
>(mode));
879 if(extension_size < 2) {
880 throw Decoding_Error(
"Empty psk_key_exchange_modes extension is illegal");
883 const auto mode_count = reader.
get_byte();
884 for(uint16_t i = 0; i < mode_count; ++i) {
887 m_modes.push_back(mode);
893 std::vector<uint8_t> out;
894 std::vector<uint8_t> dn_list;
896 for(
const auto& dn : m_distinguished_names) {
897 std::vector<uint8_t> encoded_dn;
899 dn.encode_into(encoder);
909 if(extension_size < 2) {
910 throw Decoding_Error(
"Empty certificate_authorities extension is illegal");
916 throw Decoding_Error(
"Inconsistent length in certificate_authorities extension");
922 BER_Decoder decoder(name_bits.data(), name_bits.size());
923 m_distinguished_names.emplace_back();
924 decoder.
decode(m_distinguished_names.back());
929 m_distinguished_names(std::move(acceptable_DNs)) {}
932 std::vector<uint8_t> result;
933 if(m_max_early_data_size.has_value()) {
934 const auto max_data = m_max_early_data_size.value();
944 uint16_t extension_size,
947 if(extension_size != 4) {
949 "Received an early_data extension in a NewSessionTicket message "
950 "without maximum early data size indication");
954 }
else if(extension_size != 0) {
956 "Received an early_data extension containing an unexpected data "
#define BOTAN_ASSERT_NOMSG(expr)
#define BOTAN_STATE_CHECK(expr)
#define BOTAN_ARG_CHECK(expr, msg)
#define BOTAN_ASSERT(expr, assertion_made)
BER_Decoder & decode(bool &out)
Application_Layer_Protocol_Notification(std::string_view protocol)
std::string single_protocol() const
std::vector< uint8_t > serialize(Connection_Side whoami) const override
std::vector< uint8_t > serialize(Connection_Side whoami) const override
Certificate_Authorities(TLS_Data_Reader &reader, uint16_t extension_size)
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
Client_Certificate_Type(const Client_Certificate_Type &cct, const Policy &policy)
Cookie(const std::vector< uint8_t > &cookie)
std::vector< uint8_t > serialize(Connection_Side whoami) const override
std::vector< uint8_t > serialize(Connection_Side whoami) const override
EarlyDataIndication(TLS_Data_Reader &reader, uint16_t extension_size, Handshake_Type message_type)
bool empty() const override
Encrypt_then_MAC()=default
std::vector< uint8_t > serialize(Connection_Side whoami) const override
std::vector< uint8_t > serialize(Connection_Side whoami) const override
Extended_Master_Secret()=default
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
PSK_Key_Exchange_Modes(std::vector< PSK_Key_Exchange_Mode > modes)
std::vector< uint8_t > serialize(Connection_Side whoami) const override
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)
std::vector< uint8_t > serialize(Connection_Side whoami) const override
Renegotiation_Extension()=default
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)
std::vector< uint8_t > serialize(Connection_Side whoami) const override
Server_Name_Indicator(std::string_view host_name)
Session_Ticket_Extension()=default
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)
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
Certificate_Type certificate_type_from_string(const std::string &type_str)
constexpr uint8_t get_byte(T input)
std::string fmt(std::string_view format, const T &... args)
bool value_exists(const std::vector< T > &vec, const OT &val)
const uint8_t * cast_char_ptr_to_uint8(const char *s)