11#include <botan/tls_extensions.h>
14#include <botan/tls_callbacks.h>
15#include <botan/tls_exceptn.h>
16#include <botan/tls_policy.h>
17#include <botan/internal/ct_utils.h>
18#include <botan/internal/stl_util.h>
19#include <botan/internal/tls_reader.h>
25#if defined(BOTAN_HAS_X25519)
26 #include <botan/x25519.h>
29#if defined(BOTAN_HAS_X448)
30 #include <botan/x448.h>
34#include <botan/dl_group.h>
35#include <botan/ecdh.h>
41class Key_Share_Entry {
43 Key_Share_Entry(TLS_Data_Reader& reader) {
45 m_group =
static_cast<Named_Group>(reader.get_uint16_t());
46 m_key_exchange = reader.get_tls_length_value(2);
51 Key_Share_Entry(
const TLS::Group_Params group) : m_group(group) {}
53 Key_Share_Entry(
const TLS::Group_Params group, Callbacks& cb, RandomNumberGenerator& rng) :
54 m_group(group), m_private_key(cb.tls_kem_generate_key(group, rng)) {
56 throw TLS_Exception(Alert::InternalError,
"Application did not provide a suitable ephemeral key pair");
60 m_key_exchange = m_private_key->public_key_bits();
61 }
else if(group.is_ecdh_named_curve()) {
62 auto pkey = dynamic_cast<ECDH_PublicKey*>(m_private_key.get());
64 throw TLS_Exception(Alert::InternalError,
"Application did not provide a ECDH_PublicKey");
77 auto pkey =
dynamic_cast<PK_Key_Agreement_Key*
>(m_private_key.get());
79 throw TLS_Exception(Alert::InternalError,
"Application did not provide a key-agreement key");
82 m_key_exchange = pkey->public_value();
86 bool empty()
const {
return (m_group == Group_Params::NONE) && m_key_exchange.empty(); }
88 std::vector<uint8_t> serialize()
const {
89 std::vector<uint8_t> result;
90 result.reserve(m_key_exchange.size() + 4);
92 const uint16_t named_curve_id = m_group.wire_code();
103 const Policy& policy,
105 RandomNumberGenerator& rng) {
106 auto [encapsulated_shared_key, shared_key] =
108 m_key_exchange = std::move(encapsulated_shared_key);
109 return std::move(shared_key);
119 const Policy& policy,
121 RandomNumberGenerator& rng) {
122 auto scope = scoped_cleanup([&] { m_private_key.reset(); });
125 return cb.tls_kem_decapsulate(m_group, *m_private_key, received.m_key_exchange, rng, policy);
130 std::vector<uint8_t> m_key_exchange;
131 std::unique_ptr<Private_Key> m_private_key;
134class Key_Share_ClientHello;
136class Key_Share_ServerHello {
138 Key_Share_ServerHello(TLS_Data_Reader& reader, uint16_t) : m_server_share(reader) {}
141 const Key_Share_ClientHello& client_keyshare,
142 const Policy& policy,
144 RandomNumberGenerator& rng);
146 ~Key_Share_ServerHello() =
default;
148 Key_Share_ServerHello(
const Key_Share_ServerHello&) =
delete;
149 Key_Share_ServerHello& operator=(
const Key_Share_ServerHello&) =
delete;
151 Key_Share_ServerHello(Key_Share_ServerHello&&) =
default;
152 Key_Share_ServerHello& operator=(Key_Share_ServerHello&&) =
default;
154 std::vector<uint8_t> serialize()
const {
return m_server_share.serialize(); }
156 bool empty()
const {
return m_server_share.empty(); }
158 Key_Share_Entry& get_singleton_entry() {
return m_server_share; }
160 const Key_Share_Entry& get_singleton_entry()
const {
return m_server_share; }
162 std::vector<Named_Group> offered_groups()
const {
return {selected_group()}; }
164 Named_Group selected_group()
const {
return m_server_share.group(); }
168 return std::exchange(m_shared_secret, {});
172 Key_Share_Entry m_server_share;
176class Key_Share_ClientHello {
178 Key_Share_ClientHello(TLS_Data_Reader& reader, uint16_t ) {
188 const auto client_key_share_length = reader.get_uint16_t();
189 const auto read_bytes_so_far_begin = reader.read_so_far();
190 auto remaining = [&] {
191 const auto read_so_far = reader.read_so_far() - read_bytes_so_far_begin;
193 return client_key_share_length - read_so_far;
196 while(reader.has_remaining() && remaining() > 0) {
197 if(remaining() < 4) {
198 throw TLS_Exception(Alert::DecodeError,
"Not enough data to read another KeyShareEntry");
201 Key_Share_Entry new_entry(reader);
208 if(std::find_if(m_client_shares.begin(), m_client_shares.end(), [&](
const auto& entry) {
209 return entry.group() == new_entry.group();
210 }) != m_client_shares.end()) {
211 throw TLS_Exception(Alert::IllegalParameter,
"Received multiple key share entries for the same group");
214 m_client_shares.emplace_back(std::move(new_entry));
217 if((reader.read_so_far() - read_bytes_so_far_begin) != client_key_share_length) {
218 throw Decoding_Error(
"Read bytes are not equal client KeyShare length");
222 Key_Share_ClientHello(
const Policy& policy, Callbacks& cb, RandomNumberGenerator& rng) {
223 const auto supported = policy.key_exchange_groups();
224 const auto offers = policy.key_exchange_groups_to_offer();
237 for(
const auto group : supported) {
238 if(std::find(offers.begin(), offers.end(), group) == offers.end()) {
241 m_client_shares.emplace_back(group, cb, rng);
245 ~Key_Share_ClientHello() =
default;
247 Key_Share_ClientHello(
const Key_Share_ClientHello&) =
delete;
248 Key_Share_ClientHello& operator=(
const Key_Share_ClientHello&) =
delete;
250 Key_Share_ClientHello(Key_Share_ClientHello&&) =
default;
251 Key_Share_ClientHello& operator=(Key_Share_ClientHello&&) =
default;
253 void retry_offer(
const TLS::Group_Params to_offer, Callbacks& cb, RandomNumberGenerator& rng) {
257 if(std::find_if(m_client_shares.cbegin(), m_client_shares.cend(), [&](
const auto& kse) {
258 return kse.group() == to_offer;
259 }) != m_client_shares.cend()) {
260 throw TLS_Exception(Alert::IllegalParameter,
"group was already offered");
263 m_client_shares.clear();
264 m_client_shares.emplace_back(to_offer, cb, rng);
267 std::vector<Named_Group> offered_groups()
const {
268 std::vector<Named_Group> offered_groups;
269 offered_groups.reserve(m_client_shares.size());
270 for(
const auto& share : m_client_shares) {
271 offered_groups.push_back(share.group());
273 return offered_groups;
276 Named_Group selected_group()
const {
throw Invalid_Argument(
"Client Hello Key Share does not select a group"); }
278 std::vector<uint8_t> serialize()
const {
279 std::vector<uint8_t> shares;
280 for(
const auto& share : m_client_shares) {
281 const auto serialized_share = share.serialize();
282 shares.insert(shares.end(), serialized_share.cbegin(), serialized_share.cend());
285 std::vector<uint8_t> result;
299 const Policy& policy,
301 RandomNumberGenerator& rng)
const {
302 auto& server_selected = server_share.get_singleton_entry();
305 auto match = std::find_if(m_client_shares.begin(), m_client_shares.end(), [&](
const auto& offered) {
306 return offered.group() == server_selected.group();
314 return server_selected.encapsulate(*match, policy, cb, rng);
318 const Policy& policy,
320 RandomNumberGenerator& rng) {
321 const auto& server_selected = server_share.get_singleton_entry();
324 auto match = std::find_if(m_client_shares.begin(), m_client_shares.end(), [&](
const auto& offered) {
325 return offered.group() == server_selected.group();
332 if(match == m_client_shares.end()) {
333 throw TLS_Exception(Alert::IllegalParameter,
"Server selected a key exchange group we didn't offer.");
336 return match->decapsulate(server_selected, policy, cb, rng);
340 std::vector<Key_Share_Entry> m_client_shares;
343Key_Share_ServerHello::Key_Share_ServerHello(
Named_Group group,
344 const Key_Share_ClientHello& client_keyshare,
345 const Policy& policy,
347 RandomNumberGenerator& rng) :
348 m_server_share(group) {
349 m_shared_secret = client_keyshare.encapsulate(*
this, policy, cb, rng);
352class Key_Share_HelloRetryRequest {
354 Key_Share_HelloRetryRequest(TLS_Data_Reader& reader, uint16_t extension_size) {
355 constexpr auto sizeof_uint16_t =
sizeof(uint16_t);
357 if(extension_size != sizeof_uint16_t) {
358 throw Decoding_Error(
"Size of KeyShare extension in HelloRetryRequest must be " +
359 std::to_string(sizeof_uint16_t) +
" bytes");
362 m_selected_group =
static_cast<Named_Group>(reader.get_uint16_t());
365 Key_Share_HelloRetryRequest(Named_Group selected_group) : m_selected_group(selected_group) {}
367 ~Key_Share_HelloRetryRequest() =
default;
369 Key_Share_HelloRetryRequest(
const Key_Share_HelloRetryRequest&) =
delete;
370 Key_Share_HelloRetryRequest& operator=(
const Key_Share_HelloRetryRequest&) =
delete;
372 Key_Share_HelloRetryRequest(Key_Share_HelloRetryRequest&&) =
default;
373 Key_Share_HelloRetryRequest& operator=(Key_Share_HelloRetryRequest&&) =
default;
375 std::vector<uint8_t> serialize()
const {
376 auto code = m_selected_group.wire_code();
377 return {get_byte<0>(code), get_byte<1>(code)};
380 Named_Group selected_group()
const {
return m_selected_group; }
382 std::vector<Named_Group> offered_groups()
const {
383 throw Invalid_Argument(
"Hello Retry Request never offers any key exchange groups");
386 bool empty()
const {
return m_selected_group == Group_Params::NONE; }
394class Key_Share::Key_Share_Impl {
396 using Key_Share_Type = std::variant<Key_Share_ClientHello, Key_Share_ServerHello, Key_Share_HelloRetryRequest>;
398 Key_Share_Impl(Key_Share_Type ks) : key_share(std::move(ks)) {}
401 Key_Share_Type key_share;
405 if(message_type == Handshake_Type::ClientHello) {
406 m_impl = std::make_unique<Key_Share_Impl>(Key_Share_ClientHello(reader, extension_size));
407 }
else if(message_type == Handshake_Type::HelloRetryRequest) {
409 m_impl = std::make_unique<Key_Share_Impl>(Key_Share_HelloRetryRequest(reader, extension_size));
410 }
else if(message_type == Handshake_Type::ServerHello) {
412 m_impl = std::make_unique<Key_Share_Impl>(Key_Share_ServerHello(reader, extension_size));
414 throw Invalid_Argument(std::string(
"cannot create a Key_Share extension for message of type: ") +
421 m_impl(std::make_unique<Key_Share_Impl>(Key_Share_ClientHello(policy, cb, rng))) {}
425 m_impl(std::make_unique<Key_Share_Impl>(Key_Share_HelloRetryRequest(selected_group))) {}
433 m_impl(std::make_unique<Key_Share_Impl>(Key_Share_ServerHello(
434 selected_group, std::get<Key_Share_ClientHello>(client_keyshare.m_impl->key_share), policy, cb, rng))) {}
439 return std::visit([](
const auto& key_share) {
return key_share.serialize(); }, m_impl->key_share);
443 return std::visit([](
const auto& key_share) {
return key_share.empty(); }, m_impl->key_share);
458 return std::visit(
overloaded{[&](Key_Share_ClientHello& ch,
const Key_Share_ServerHello& sh) {
459 return ch.decapsulate(sh, policy, cb, rng);
463 "can only decapsulate in ClientHello Key_Share with a ServerHello Key_Share");
466 server_keyshare.m_impl->key_share);
470 return std::visit([](
const auto& keyshare) {
return keyshare.offered_groups(); }, m_impl->key_share);
474 return std::visit([](
const auto& keyshare) {
return keyshare.selected_group(); }, m_impl->key_share);
479 overloaded{[](Key_Share_ServerHello& server_keyshare) {
return server_keyshare.take_shared_secret(); },
481 throw Invalid_Argument(
"Only the key share in Server Hello contains a shared secret");
487 const std::vector<Named_Group>& supported_groups,
490 std::visit(
overloaded{[&](Key_Share_ClientHello& ch,
const Key_Share_HelloRetryRequest& hrr) {
491 auto selected = hrr.selected_group();
496 throw TLS_Exception(Alert::IllegalParameter,
"group was not advertised as supported");
499 return ch.retry_offer(selected, cb, rng);
501 [](
const auto&,
const auto&) {
502 throw Invalid_Argument(
"can only retry with HelloRetryRequest on a ClientHello Key_Share");
505 retry_request_keyshare.m_impl->key_share);
#define BOTAN_ASSERT_NOMSG(expr)
#define BOTAN_STATE_CHECK(expr)
static std::pair< std::vector< uint8_t >, secure_vector< uint8_t > > destructure(KEM_Encapsulation &&kem)
Named_Group selected_group() const
std::vector< Named_Group > offered_groups() const
Key_Share(TLS_Data_Reader &reader, uint16_t extension_size, Handshake_Type message_type)
secure_vector< uint8_t > decapsulate(const Key_Share &server_keyshare, const Policy &policy, Callbacks &cb, RandomNumberGenerator &rng)
void retry_offer(const Key_Share &retry_request_keyshare, const std::vector< Named_Group > &supported_groups, Callbacks &cb, RandomNumberGenerator &rng)
static std::unique_ptr< Key_Share > create_as_encapsulation(Group_Params selected_group, const Key_Share &client_keyshare, const Policy &policy, Callbacks &cb, RandomNumberGenerator &rng)
std::vector< uint8_t > serialize(Connection_Side whoami) const override
bool empty() const override
secure_vector< uint8_t > take_shared_secret()
const char * handshake_type_to_string(Handshake_Type type)
void append_tls_length_value(std::vector< uint8_t, Alloc > &buf, const T *vals, size_t vals_size, size_t tag_size)
constexpr uint8_t get_byte(T input)
bool value_exists(const std::vector< T > &vec, const OT &val)
std::vector< T, secure_allocator< T > > secure_vector